[SCSI] libosd: OSD version 2 Support
[safe/jmp/linux-2.6] / include / scsi / osd_initiator.h
1 /*
2  * osd_initiator.h - OSD initiator API definition
3  *
4  * Copyright (C) 2008 Panasas Inc.  All rights reserved.
5  *
6  * Authors:
7  *   Boaz Harrosh <bharrosh@panasas.com>
8  *   Benny Halevy <bhalevy@panasas.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  *
13  */
14 #ifndef __OSD_INITIATOR_H__
15 #define __OSD_INITIATOR_H__
16
17 #include "osd_protocol.h"
18 #include "osd_types.h"
19
20 #include <linux/blkdev.h>
21
22 /* Note: "NI" in comments below means "Not Implemented yet" */
23
24 /* Configure of code:
25  * #undef if you *don't* want OSD v1 support in runtime.
26  * If #defined the initiator will dynamically configure to encode OSD v1
27  * CDB's if the target is detected to be OSD v1 only.
28  * OSD v2 only commands, options, and attributes will be ignored if target
29  * is v1 only.
30  * If #defined will result in bigger/slower code (OK Slower maybe not)
31  * Q: Should this be CONFIG_SCSI_OSD_VER1_SUPPORT and set from Kconfig?
32  */
33 #define OSD_VER1_SUPPORT y
34
35 enum osd_std_version {
36         OSD_VER_NONE = 0,
37         OSD_VER1 = 1,
38         OSD_VER2 = 2,
39 };
40
41 /*
42  * Object-based Storage Device.
43  * This object represents an OSD device.
44  * It is not a full linux device in any way. It is only
45  * a place to hang resources associated with a Linux
46  * request Q and some default properties.
47  */
48 struct osd_dev {
49         struct scsi_device *scsi_device;
50         unsigned def_timeout;
51
52 #ifdef OSD_VER1_SUPPORT
53         enum osd_std_version version;
54 #endif
55 };
56
57 /* Retrieve/return osd_dev(s) for use by Kernel clients */
58 struct osd_dev *osduld_path_lookup(const char *dev_name); /*Use IS_ERR/ERR_PTR*/
59 void osduld_put_device(struct osd_dev *od);
60
61 /* Add/remove test ioctls from external modules */
62 typedef int (do_test_fn)(struct osd_dev *od, unsigned cmd, unsigned long arg);
63 int osduld_register_test(unsigned ioctl, do_test_fn *do_test);
64 void osduld_unregister_test(unsigned ioctl);
65
66 /* These are called by uld at probe time */
67 void osd_dev_init(struct osd_dev *od, struct scsi_device *scsi_device);
68 void osd_dev_fini(struct osd_dev *od);
69
70 /* we might want to use function vector in the future */
71 static inline void osd_dev_set_ver(struct osd_dev *od, enum osd_std_version v)
72 {
73 #ifdef OSD_VER1_SUPPORT
74         od->version = v;
75 #endif
76 }
77
78 struct osd_request;
79 typedef void (osd_req_done_fn)(struct osd_request *or, void *private);
80
81 struct osd_request {
82         struct osd_cdb cdb;
83         struct osd_data_out_integrity_info out_data_integ;
84         struct osd_data_in_integrity_info in_data_integ;
85
86         struct osd_dev *osd_dev;
87         struct request *request;
88
89         struct _osd_req_data_segment {
90                 void *buff;
91                 unsigned alloc_size; /* 0 here means: don't call kfree */
92                 unsigned total_bytes;
93         } set_attr, enc_get_attr, get_attr;
94
95         struct _osd_io_info {
96                 struct bio *bio;
97                 u64 total_bytes;
98                 struct request *req;
99                 struct _osd_req_data_segment *last_seg;
100                 u8 *pad_buff;
101         } out, in;
102
103         gfp_t alloc_flags;
104         unsigned timeout;
105         unsigned retries;
106         u8 sense[OSD_MAX_SENSE_LEN];
107         enum osd_attributes_mode attributes_mode;
108
109         osd_req_done_fn *async_done;
110         void *async_private;
111         int async_error;
112 };
113
114 /* OSD Version control */
115 static inline bool osd_req_is_ver1(struct osd_request *or)
116 {
117 #ifdef OSD_VER1_SUPPORT
118         return or->osd_dev->version == OSD_VER1;
119 #else
120         return false;
121 #endif
122 }
123
124 /*
125  * How to use the osd library:
126  *
127  * osd_start_request
128  *      Allocates a request.
129  *
130  * osd_req_*
131  *      Call one of, to encode the desired operation.
132  *
133  * osd_add_{get,set}_attr
134  *      Optionally add attributes to the CDB, list or page mode.
135  *
136  * osd_finalize_request
137  *      Computes final data out/in offsets and signs the request,
138  *      making it ready for execution.
139  *
140  * osd_execute_request
141  *      May be called to execute it through the block layer. Other wise submit
142  *      the associated block request in some other way.
143  *
144  * After execution:
145  * osd_req_decode_sense
146  *      Decodes sense information to verify execution results.
147  *
148  * osd_req_decode_get_attr
149  *      Retrieve osd_add_get_attr_list() values if used.
150  *
151  * osd_end_request
152  *      Must be called to deallocate the request.
153  */
154
155 /**
156  * osd_start_request - Allocate and initialize an osd_request
157  *
158  * @osd_dev:    OSD device that holds the scsi-device and default values
159  *              that the request is associated with.
160  * @gfp:        The allocation flags to use for request allocation, and all
161  *              subsequent allocations. This will be stored at
162  *              osd_request->alloc_flags, can be changed by user later
163  *
164  * Allocate osd_request and initialize all members to the
165  * default/initial state.
166  */
167 struct osd_request *osd_start_request(struct osd_dev *od, gfp_t gfp);
168
169 enum osd_req_options {
170         OSD_REQ_FUA = 0x08,     /* Force Unit Access */
171         OSD_REQ_DPO = 0x10,     /* Disable Page Out */
172
173         OSD_REQ_BYPASS_TIMESTAMPS = 0x80,
174 };
175
176 /**
177  * osd_finalize_request - Sign request and prepare request for execution
178  *
179  * @or:         osd_request to prepare
180  * @options:    combination of osd_req_options bit flags or 0.
181  * @cap:        A Pointer to an OSD_CAP_LEN bytes buffer that is received from
182  *              The security manager as capabilities for this cdb.
183  * @cap_key:    The cryptographic key used to sign the cdb/data. Can be null
184  *              if NOSEC is used.
185  *
186  * The actual request and bios are only allocated here, so are the get_attr
187  * buffers that will receive the returned attributes. Copy's @cap to cdb.
188  * Sign the cdb/data with @cap_key.
189  */
190 int osd_finalize_request(struct osd_request *or,
191         u8 options, const void *cap, const u8 *cap_key);
192
193 /**
194  * osd_execute_request - Execute the request synchronously through block-layer
195  *
196  * @or:         osd_request to Executed
197  *
198  * Calls blk_execute_rq to q the command and waits for completion.
199  */
200 int osd_execute_request(struct osd_request *or);
201
202 /**
203  * osd_execute_request_async - Execute the request without waitting.
204  *
205  * @or:                      - osd_request to Executed
206  * @done: (Optional)         - Called at end of execution
207  * @private:                 - Will be passed to @done function
208  *
209  * Calls blk_execute_rq_nowait to queue the command. When execution is done
210  * optionally calls @done with @private as parameter. @or->async_error will
211  * have the return code
212  */
213 int osd_execute_request_async(struct osd_request *or,
214         osd_req_done_fn *done, void *private);
215
216 /**
217  * osd_end_request - return osd_request to free store
218  *
219  * @or:         osd_request to free
220  *
221  * Deallocate all osd_request resources (struct req's, BIOs, buffers, etc.)
222  */
223 void osd_end_request(struct osd_request *or);
224
225 /*
226  * CDB Encoding
227  *
228  * Note: call only one of the following methods.
229  */
230
231 /*
232  * Device commands
233  */
234 void osd_req_set_master_seed_xchg(struct osd_request *or, ...);/* NI */
235 void osd_req_set_master_key(struct osd_request *or, ...);/* NI */
236
237 void osd_req_format(struct osd_request *or, u64 tot_capacity);
238
239 /* list all partitions
240  * @list header must be initialized to zero on first run.
241  *
242  * Call osd_is_obj_list_done() to find if we got the complete list.
243  */
244 int osd_req_list_dev_partitions(struct osd_request *or,
245         osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem);
246
247 void osd_req_flush_obsd(struct osd_request *or,
248         enum osd_options_flush_scope_values);
249
250 void osd_req_perform_scsi_command(struct osd_request *or,
251         const u8 *cdb, ...);/* NI */
252 void osd_req_task_management(struct osd_request *or, ...);/* NI */
253
254 /*
255  * Partition commands
256  */
257 void osd_req_create_partition(struct osd_request *or, osd_id partition);
258 void osd_req_remove_partition(struct osd_request *or, osd_id partition);
259
260 void osd_req_set_partition_key(struct osd_request *or,
261         osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
262         u8 seed[OSD_CRYPTO_SEED_SIZE]);/* NI */
263
264 /* list all collections in the partition
265  * @list header must be init to zero on first run.
266  *
267  * Call osd_is_obj_list_done() to find if we got the complete list.
268  */
269 int osd_req_list_partition_collections(struct osd_request *or,
270         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
271         unsigned nelem);
272
273 /* list all objects in the partition
274  * @list header must be init to zero on first run.
275  *
276  * Call osd_is_obj_list_done() to find if we got the complete list.
277  */
278 int osd_req_list_partition_objects(struct osd_request *or,
279         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
280         unsigned nelem);
281
282 void osd_req_flush_partition(struct osd_request *or,
283         osd_id partition, enum osd_options_flush_scope_values);
284
285 /*
286  * Collection commands
287  */
288 void osd_req_create_collection(struct osd_request *or,
289         const struct osd_obj_id *);/* NI */
290 void osd_req_remove_collection(struct osd_request *or,
291         const struct osd_obj_id *);/* NI */
292
293 /* list all objects in the collection */
294 int osd_req_list_collection_objects(struct osd_request *or,
295         const struct osd_obj_id *, osd_id initial_id,
296         struct osd_obj_id_list *list, unsigned nelem);
297
298 /* V2 only filtered list of objects in the collection */
299 void osd_req_query(struct osd_request *or, ...);/* NI */
300
301 void osd_req_flush_collection(struct osd_request *or,
302         const struct osd_obj_id *, enum osd_options_flush_scope_values);
303
304 void osd_req_get_member_attrs(struct osd_request *or, ...);/* V2-only NI */
305 void osd_req_set_member_attrs(struct osd_request *or, ...);/* V2-only NI */
306
307 /*
308  * Object commands
309  */
310 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *);
311 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *);
312
313 void osd_req_write(struct osd_request *or,
314         const struct osd_obj_id *, struct bio *data_out, u64 offset);
315 void osd_req_append(struct osd_request *or,
316         const struct osd_obj_id *, struct bio *data_out);/* NI */
317 void osd_req_create_write(struct osd_request *or,
318         const struct osd_obj_id *, struct bio *data_out, u64 offset);/* NI */
319 void osd_req_clear(struct osd_request *or,
320         const struct osd_obj_id *, u64 offset, u64 len);/* NI */
321 void osd_req_punch(struct osd_request *or,
322         const struct osd_obj_id *, u64 offset, u64 len);/* V2-only NI */
323
324 void osd_req_flush_object(struct osd_request *or,
325         const struct osd_obj_id *, enum osd_options_flush_scope_values,
326         /*V2*/ u64 offset, /*V2*/ u64 len);
327
328 void osd_req_read(struct osd_request *or,
329         const struct osd_obj_id *, struct bio *data_in, u64 offset);
330
331 /*
332  * Root/Partition/Collection/Object Attributes commands
333  */
334
335 /* get before set */
336 void osd_req_get_attributes(struct osd_request *or, const struct osd_obj_id *);
337
338 /* set before get */
339 void osd_req_set_attributes(struct osd_request *or, const struct osd_obj_id *);
340
341 /*
342  * Attributes appended to most commands
343  */
344
345 /* Attributes List mode (or V2 CDB) */
346   /*
347    * TODO: In ver2 if at finalize time only one attr was set and no gets,
348    * then the Attributes CDB mode is used automatically to save IO.
349    */
350
351 /* set a list of attributes. */
352 int osd_req_add_set_attr_list(struct osd_request *or,
353         const struct osd_attr *, unsigned nelem);
354
355 /* get a list of attributes */
356 int osd_req_add_get_attr_list(struct osd_request *or,
357         const struct osd_attr *, unsigned nelem);
358
359 /*
360  * Attributes list decoding
361  * Must be called after osd_request.request was executed
362  * It is called in a loop to decode the returned get_attr
363  * (see osd_add_get_attr)
364  */
365 int osd_req_decode_get_attr_list(struct osd_request *or,
366         struct osd_attr *, int *nelem, void **iterator);
367
368 /* Attributes Page mode */
369
370 /*
371  * Read an attribute page and optionally set one attribute
372  *
373  * Retrieves the attribute page directly to a user buffer.
374  * @attr_page_data shall stay valid until end of execution.
375  * See osd_attributes.h for common page structures
376  */
377 int osd_req_add_get_attr_page(struct osd_request *or,
378         u32 page_id, void *attr_page_data, unsigned max_page_len,
379         const struct osd_attr *set_one);
380
381 #endif /* __OSD_LIB_H__ */