af_ieee802154: add support for WANT_ACK socket option
[pandora-kernel.git] / 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 #include <scsi/scsi_device.h>
22
23 /* Note: "NI" in comments below means "Not Implemented yet" */
24
25 /* Configure of code:
26  * #undef if you *don't* want OSD v1 support in runtime.
27  * If #defined the initiator will dynamically configure to encode OSD v1
28  * CDB's if the target is detected to be OSD v1 only.
29  * OSD v2 only commands, options, and attributes will be ignored if target
30  * is v1 only.
31  * If #defined will result in bigger/slower code (OK Slower maybe not)
32  * Q: Should this be CONFIG_SCSI_OSD_VER1_SUPPORT and set from Kconfig?
33  */
34 #define OSD_VER1_SUPPORT y
35
36 enum osd_std_version {
37         OSD_VER_NONE = 0,
38         OSD_VER1 = 1,
39         OSD_VER2 = 2,
40 };
41
42 /*
43  * Object-based Storage Device.
44  * This object represents an OSD device.
45  * It is not a full linux device in any way. It is only
46  * a place to hang resources associated with a Linux
47  * request Q and some default properties.
48  */
49 struct osd_dev {
50         struct scsi_device *scsi_device;
51         struct file *file;
52         unsigned def_timeout;
53
54 #ifdef OSD_VER1_SUPPORT
55         enum osd_std_version version;
56 #endif
57 };
58
59 /* Retrieve/return osd_dev(s) for use by Kernel clients */
60 struct osd_dev *osduld_path_lookup(const char *dev_name); /*Use IS_ERR/ERR_PTR*/
61 void osduld_put_device(struct osd_dev *od);
62
63 /* Add/remove test ioctls from external modules */
64 typedef int (do_test_fn)(struct osd_dev *od, unsigned cmd, unsigned long arg);
65 int osduld_register_test(unsigned ioctl, do_test_fn *do_test);
66 void osduld_unregister_test(unsigned ioctl);
67
68 /* These are called by uld at probe time */
69 void osd_dev_init(struct osd_dev *od, struct scsi_device *scsi_device);
70 void osd_dev_fini(struct osd_dev *od);
71
72 /* some hi level device operations */
73 int osd_auto_detect_ver(struct osd_dev *od, void *caps);    /* GFP_KERNEL */
74 static inline struct request_queue *osd_request_queue(struct osd_dev *od)
75 {
76         return od->scsi_device->request_queue;
77 }
78
79 /* we might want to use function vector in the future */
80 static inline void osd_dev_set_ver(struct osd_dev *od, enum osd_std_version v)
81 {
82 #ifdef OSD_VER1_SUPPORT
83         od->version = v;
84 #endif
85 }
86
87 struct osd_request;
88 typedef void (osd_req_done_fn)(struct osd_request *or, void *private);
89
90 struct osd_request {
91         struct osd_cdb cdb;
92         struct osd_data_out_integrity_info out_data_integ;
93         struct osd_data_in_integrity_info in_data_integ;
94
95         struct osd_dev *osd_dev;
96         struct request *request;
97
98         struct _osd_req_data_segment {
99                 void *buff;
100                 unsigned alloc_size; /* 0 here means: don't call kfree */
101                 unsigned total_bytes;
102         } set_attr, enc_get_attr, get_attr;
103
104         struct _osd_io_info {
105                 struct bio *bio;
106                 u64 total_bytes;
107                 struct request *req;
108                 struct _osd_req_data_segment *last_seg;
109                 u8 *pad_buff;
110         } out, in;
111
112         gfp_t alloc_flags;
113         unsigned timeout;
114         unsigned retries;
115         u8 sense[OSD_MAX_SENSE_LEN];
116         enum osd_attributes_mode attributes_mode;
117
118         osd_req_done_fn *async_done;
119         void *async_private;
120         int async_error;
121 };
122
123 /* OSD Version control */
124 static inline bool osd_req_is_ver1(struct osd_request *or)
125 {
126 #ifdef OSD_VER1_SUPPORT
127         return or->osd_dev->version == OSD_VER1;
128 #else
129         return false;
130 #endif
131 }
132
133 /*
134  * How to use the osd library:
135  *
136  * osd_start_request
137  *      Allocates a request.
138  *
139  * osd_req_*
140  *      Call one of, to encode the desired operation.
141  *
142  * osd_add_{get,set}_attr
143  *      Optionally add attributes to the CDB, list or page mode.
144  *
145  * osd_finalize_request
146  *      Computes final data out/in offsets and signs the request,
147  *      making it ready for execution.
148  *
149  * osd_execute_request
150  *      May be called to execute it through the block layer. Other wise submit
151  *      the associated block request in some other way.
152  *
153  * After execution:
154  * osd_req_decode_sense
155  *      Decodes sense information to verify execution results.
156  *
157  * osd_req_decode_get_attr
158  *      Retrieve osd_add_get_attr_list() values if used.
159  *
160  * osd_end_request
161  *      Must be called to deallocate the request.
162  */
163
164 /**
165  * osd_start_request - Allocate and initialize an osd_request
166  *
167  * @osd_dev:    OSD device that holds the scsi-device and default values
168  *              that the request is associated with.
169  * @gfp:        The allocation flags to use for request allocation, and all
170  *              subsequent allocations. This will be stored at
171  *              osd_request->alloc_flags, can be changed by user later
172  *
173  * Allocate osd_request and initialize all members to the
174  * default/initial state.
175  */
176 struct osd_request *osd_start_request(struct osd_dev *od, gfp_t gfp);
177
178 enum osd_req_options {
179         OSD_REQ_FUA = 0x08,     /* Force Unit Access */
180         OSD_REQ_DPO = 0x10,     /* Disable Page Out */
181
182         OSD_REQ_BYPASS_TIMESTAMPS = 0x80,
183 };
184
185 /**
186  * osd_finalize_request - Sign request and prepare request for execution
187  *
188  * @or:         osd_request to prepare
189  * @options:    combination of osd_req_options bit flags or 0.
190  * @cap:        A Pointer to an OSD_CAP_LEN bytes buffer that is received from
191  *              The security manager as capabilities for this cdb.
192  * @cap_key:    The cryptographic key used to sign the cdb/data. Can be null
193  *              if NOSEC is used.
194  *
195  * The actual request and bios are only allocated here, so are the get_attr
196  * buffers that will receive the returned attributes. Copy's @cap to cdb.
197  * Sign the cdb/data with @cap_key.
198  */
199 int osd_finalize_request(struct osd_request *or,
200         u8 options, const void *cap, const u8 *cap_key);
201
202 /**
203  * osd_execute_request - Execute the request synchronously through block-layer
204  *
205  * @or:         osd_request to Executed
206  *
207  * Calls blk_execute_rq to q the command and waits for completion.
208  */
209 int osd_execute_request(struct osd_request *or);
210
211 /**
212  * osd_execute_request_async - Execute the request without waitting.
213  *
214  * @or:                      - osd_request to Executed
215  * @done: (Optional)         - Called at end of execution
216  * @private:                 - Will be passed to @done function
217  *
218  * Calls blk_execute_rq_nowait to queue the command. When execution is done
219  * optionally calls @done with @private as parameter. @or->async_error will
220  * have the return code
221  */
222 int osd_execute_request_async(struct osd_request *or,
223         osd_req_done_fn *done, void *private);
224
225 /**
226  * osd_req_decode_sense_full - Decode sense information after execution.
227  *
228  * @or:           - osd_request to examine
229  * @osi           - Recievs a more detailed error report information (optional).
230  * @silent        - Do not print to dmsg (Even if enabled)
231  * @bad_obj_list  - Some commands act on multiple objects. Failed objects will
232  *                  be recieved here (optional)
233  * @max_obj       - Size of @bad_obj_list.
234  * @bad_attr_list - List of failing attributes (optional)
235  * @max_attr      - Size of @bad_attr_list.
236  *
237  * After execution, sense + return code can be analyzed using this function. The
238  * return code is the final disposition on the error. So it is possible that a
239  * CHECK_CONDITION was returned from target but this will return NO_ERROR, for
240  * example on recovered errors. All parameters are optional if caller does
241  * not need any returned information.
242  * Note: This function will also dump the error to dmsg according to settings
243  * of the SCSI_OSD_DPRINT_SENSE Kconfig value. Set @silent if you know the
244  * command would routinely fail, to not spam the dmsg file.
245  */
246 struct osd_sense_info {
247         int key;                /* one of enum scsi_sense_keys */
248         int additional_code ;   /* enum osd_additional_sense_codes */
249         union { /* Sense specific information */
250                 u16 sense_info;
251                 u16 cdb_field_offset;   /* scsi_invalid_field_in_cdb */
252         };
253         union { /* Command specific information */
254                 u64 command_info;
255         };
256
257         u32 not_initiated_command_functions; /* osd_command_functions_bits */
258         u32 completed_command_functions; /* osd_command_functions_bits */
259         struct osd_obj_id obj;
260         struct osd_attr attr;
261 };
262
263 int osd_req_decode_sense_full(struct osd_request *or,
264         struct osd_sense_info *osi, bool silent,
265         struct osd_obj_id *bad_obj_list, int max_obj,
266         struct osd_attr *bad_attr_list, int max_attr);
267
268 static inline int osd_req_decode_sense(struct osd_request *or,
269         struct osd_sense_info *osi)
270 {
271         return osd_req_decode_sense_full(or, osi, false, NULL, 0, NULL, 0);
272 }
273
274 /**
275  * osd_end_request - return osd_request to free store
276  *
277  * @or:         osd_request to free
278  *
279  * Deallocate all osd_request resources (struct req's, BIOs, buffers, etc.)
280  */
281 void osd_end_request(struct osd_request *or);
282
283 /*
284  * CDB Encoding
285  *
286  * Note: call only one of the following methods.
287  */
288
289 /*
290  * Device commands
291  */
292 void osd_req_set_master_seed_xchg(struct osd_request *or, ...);/* NI */
293 void osd_req_set_master_key(struct osd_request *or, ...);/* NI */
294
295 void osd_req_format(struct osd_request *or, u64 tot_capacity);
296
297 /* list all partitions
298  * @list header must be initialized to zero on first run.
299  *
300  * Call osd_is_obj_list_done() to find if we got the complete list.
301  */
302 int osd_req_list_dev_partitions(struct osd_request *or,
303         osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem);
304
305 void osd_req_flush_obsd(struct osd_request *or,
306         enum osd_options_flush_scope_values);
307
308 void osd_req_perform_scsi_command(struct osd_request *or,
309         const u8 *cdb, ...);/* NI */
310 void osd_req_task_management(struct osd_request *or, ...);/* NI */
311
312 /*
313  * Partition commands
314  */
315 void osd_req_create_partition(struct osd_request *or, osd_id partition);
316 void osd_req_remove_partition(struct osd_request *or, osd_id partition);
317
318 void osd_req_set_partition_key(struct osd_request *or,
319         osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
320         u8 seed[OSD_CRYPTO_SEED_SIZE]);/* NI */
321
322 /* list all collections in the partition
323  * @list header must be init to zero on first run.
324  *
325  * Call osd_is_obj_list_done() to find if we got the complete list.
326  */
327 int osd_req_list_partition_collections(struct osd_request *or,
328         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
329         unsigned nelem);
330
331 /* list all objects in the partition
332  * @list header must be init to zero on first run.
333  *
334  * Call osd_is_obj_list_done() to find if we got the complete list.
335  */
336 int osd_req_list_partition_objects(struct osd_request *or,
337         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
338         unsigned nelem);
339
340 void osd_req_flush_partition(struct osd_request *or,
341         osd_id partition, enum osd_options_flush_scope_values);
342
343 /*
344  * Collection commands
345  */
346 void osd_req_create_collection(struct osd_request *or,
347         const struct osd_obj_id *);/* NI */
348 void osd_req_remove_collection(struct osd_request *or,
349         const struct osd_obj_id *);/* NI */
350
351 /* list all objects in the collection */
352 int osd_req_list_collection_objects(struct osd_request *or,
353         const struct osd_obj_id *, osd_id initial_id,
354         struct osd_obj_id_list *list, unsigned nelem);
355
356 /* V2 only filtered list of objects in the collection */
357 void osd_req_query(struct osd_request *or, ...);/* NI */
358
359 void osd_req_flush_collection(struct osd_request *or,
360         const struct osd_obj_id *, enum osd_options_flush_scope_values);
361
362 void osd_req_get_member_attrs(struct osd_request *or, ...);/* V2-only NI */
363 void osd_req_set_member_attrs(struct osd_request *or, ...);/* V2-only NI */
364
365 /*
366  * Object commands
367  */
368 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *);
369 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *);
370
371 void osd_req_write(struct osd_request *or,
372         const struct osd_obj_id *obj, u64 offset, struct bio *bio, u64 len);
373 int osd_req_write_kern(struct osd_request *or,
374         const struct osd_obj_id *obj, u64 offset, void *buff, u64 len);
375 void osd_req_append(struct osd_request *or,
376         const struct osd_obj_id *, struct bio *data_out);/* NI */
377 void osd_req_create_write(struct osd_request *or,
378         const struct osd_obj_id *, struct bio *data_out, u64 offset);/* NI */
379 void osd_req_clear(struct osd_request *or,
380         const struct osd_obj_id *, u64 offset, u64 len);/* NI */
381 void osd_req_punch(struct osd_request *or,
382         const struct osd_obj_id *, u64 offset, u64 len);/* V2-only NI */
383
384 void osd_req_flush_object(struct osd_request *or,
385         const struct osd_obj_id *, enum osd_options_flush_scope_values,
386         /*V2*/ u64 offset, /*V2*/ u64 len);
387
388 void osd_req_read(struct osd_request *or,
389         const struct osd_obj_id *obj, u64 offset, struct bio *bio, u64 len);
390 int osd_req_read_kern(struct osd_request *or,
391         const struct osd_obj_id *obj, u64 offset, void *buff, u64 len);
392
393 /*
394  * Root/Partition/Collection/Object Attributes commands
395  */
396
397 /* get before set */
398 void osd_req_get_attributes(struct osd_request *or, const struct osd_obj_id *);
399
400 /* set before get */
401 void osd_req_set_attributes(struct osd_request *or, const struct osd_obj_id *);
402
403 /*
404  * Attributes appended to most commands
405  */
406
407 /* Attributes List mode (or V2 CDB) */
408   /*
409    * TODO: In ver2 if at finalize time only one attr was set and no gets,
410    * then the Attributes CDB mode is used automatically to save IO.
411    */
412
413 /* set a list of attributes. */
414 int osd_req_add_set_attr_list(struct osd_request *or,
415         const struct osd_attr *, unsigned nelem);
416
417 /* get a list of attributes */
418 int osd_req_add_get_attr_list(struct osd_request *or,
419         const struct osd_attr *, unsigned nelem);
420
421 /*
422  * Attributes list decoding
423  * Must be called after osd_request.request was executed
424  * It is called in a loop to decode the returned get_attr
425  * (see osd_add_get_attr)
426  */
427 int osd_req_decode_get_attr_list(struct osd_request *or,
428         struct osd_attr *, int *nelem, void **iterator);
429
430 /* Attributes Page mode */
431
432 /*
433  * Read an attribute page and optionally set one attribute
434  *
435  * Retrieves the attribute page directly to a user buffer.
436  * @attr_page_data shall stay valid until end of execution.
437  * See osd_attributes.h for common page structures
438  */
439 int osd_req_add_get_attr_page(struct osd_request *or,
440         u32 page_id, void *attr_page_data, unsigned max_page_len,
441         const struct osd_attr *set_one);
442
443 #endif /* __OSD_LIB_H__ */