]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/scsi/osd/osd_initiator.c
0bbbf271fbb06c1069ec575efde2181f3c5496a1
[net-next-2.6.git] / drivers / scsi / osd / osd_initiator.c
1 /*
2  * osd_initiator - Main body of the osd initiator library.
3  *
4  * Note: The file does not contain the advanced security functionality which
5  * is only needed by the security_manager's initiators.
6  *
7  * Copyright (C) 2008 Panasas Inc.  All rights reserved.
8  *
9  * Authors:
10  *   Boaz Harrosh <bharrosh@panasas.com>
11  *   Benny Halevy <bhalevy@panasas.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  *  1. Redistributions of source code must retain the above copyright
21  *     notice, this list of conditions and the following disclaimer.
22  *  2. Redistributions in binary form must reproduce the above copyright
23  *     notice, this list of conditions and the following disclaimer in the
24  *     documentation and/or other materials provided with the distribution.
25  *  3. Neither the name of the Panasas company nor the names of its
26  *     contributors may be used to endorse or promote products derived
27  *     from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include <scsi/osd_initiator.h>
43 #include <scsi/osd_sec.h>
44 #include <scsi/osd_attributes.h>
45 #include <scsi/osd_sense.h>
46
47 #include <scsi/scsi_device.h>
48
49 #include "osd_debug.h"
50
51 #ifndef __unused
52 #    define __unused                    __attribute__((unused))
53 #endif
54
55 enum { OSD_REQ_RETRIES = 1 };
56
57 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
58 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
59 MODULE_LICENSE("GPL");
60
61 static inline void build_test(void)
62 {
63         /* structures were not packed */
64         BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
65         BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
66         BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
67 }
68
69 static const char *_osd_ver_desc(struct osd_request *or)
70 {
71         return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
72 }
73
74 #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
75
76 static int _osd_print_system_info(struct osd_dev *od, void *caps)
77 {
78         struct osd_request *or;
79         struct osd_attr get_attrs[] = {
80                 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
81                 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
82                 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
83                 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
84                 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
85                 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
86                 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
87                 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
88                 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
89                 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
90                 /* IBM-OSD-SIM Has a bug with this one put it last */
91                 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
92         };
93         void *iter = NULL, *pFirst;
94         int nelem = ARRAY_SIZE(get_attrs), a = 0;
95         int ret;
96
97         or = osd_start_request(od, GFP_KERNEL);
98         if (!or)
99                 return -ENOMEM;
100
101         /* get attrs */
102         osd_req_get_attributes(or, &osd_root_object);
103         osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
104
105         ret = osd_finalize_request(or, 0, caps, NULL);
106         if (ret)
107                 goto out;
108
109         ret = osd_execute_request(or);
110         if (ret) {
111                 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
112                 goto out;
113         }
114
115         osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
116
117         OSD_INFO("Detected %s device\n",
118                 _osd_ver_desc(or));
119
120         pFirst = get_attrs[a++].val_ptr;
121         OSD_INFO("OSD_ATTR_RI_VENDOR_IDENTIFICATION [%s]\n",
122                 (char *)pFirst);
123
124         pFirst = get_attrs[a++].val_ptr;
125         OSD_INFO("OSD_ATTR_RI_PRODUCT_IDENTIFICATION [%s]\n",
126                 (char *)pFirst);
127
128         pFirst = get_attrs[a++].val_ptr;
129         OSD_INFO("OSD_ATTR_RI_PRODUCT_MODEL [%s]\n",
130                 (char *)pFirst);
131
132         pFirst = get_attrs[a++].val_ptr;
133         OSD_INFO("OSD_ATTR_RI_PRODUCT_REVISION_LEVEL [%u]\n",
134                 get_unaligned_be32(pFirst));
135
136         pFirst = get_attrs[a++].val_ptr;
137         OSD_INFO("OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER [%s]\n",
138                 (char *)pFirst);
139
140         pFirst = get_attrs[a].val_ptr;
141         OSD_INFO("OSD_ATTR_RI_OSD_NAME [%s]\n", (char *)pFirst);
142         a++;
143
144         pFirst = get_attrs[a++].val_ptr;
145         OSD_INFO("OSD_ATTR_RI_TOTAL_CAPACITY [0x%llx]\n",
146                 _LLU(get_unaligned_be64(pFirst)));
147
148         pFirst = get_attrs[a++].val_ptr;
149         OSD_INFO("OSD_ATTR_RI_USED_CAPACITY [0x%llx]\n",
150                 _LLU(get_unaligned_be64(pFirst)));
151
152         pFirst = get_attrs[a++].val_ptr;
153         OSD_INFO("OSD_ATTR_RI_NUMBER_OF_PARTITIONS [%llu]\n",
154                 _LLU(get_unaligned_be64(pFirst)));
155
156         /* FIXME: Where are the time utilities */
157         pFirst = get_attrs[a++].val_ptr;
158         OSD_INFO("OSD_ATTR_RI_CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
159                 ((char *)pFirst)[0], ((char *)pFirst)[1],
160                 ((char *)pFirst)[2], ((char *)pFirst)[3],
161                 ((char *)pFirst)[4], ((char *)pFirst)[5]);
162
163         if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
164                 unsigned len = get_attrs[a].len;
165                 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
166
167                 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
168                                    sid_dump, sizeof(sid_dump), true);
169                 OSD_INFO("OSD_ATTR_RI_OSD_SYSTEM_ID(%d) [%s]\n", len, sid_dump);
170                 a++;
171         }
172 out:
173         osd_end_request(or);
174         return ret;
175 }
176
177 int osd_auto_detect_ver(struct osd_dev *od, void *caps)
178 {
179         int ret;
180
181         /* Auto-detect the osd version */
182         ret = _osd_print_system_info(od, caps);
183         if (ret) {
184                 osd_dev_set_ver(od, OSD_VER1);
185                 OSD_DEBUG("converting to OSD1\n");
186                 ret = _osd_print_system_info(od, caps);
187         }
188
189         return ret;
190 }
191 EXPORT_SYMBOL(osd_auto_detect_ver);
192
193 static unsigned _osd_req_cdb_len(struct osd_request *or)
194 {
195         return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
196 }
197
198 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
199 {
200         return osd_req_is_ver1(or) ?
201                 osdv1_attr_list_elem_size(len) :
202                 osdv2_attr_list_elem_size(len);
203 }
204
205 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
206 {
207         return osd_req_is_ver1(or) ?
208                 osdv1_list_size(list_head) :
209                 osdv2_list_size(list_head);
210 }
211
212 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
213 {
214         return osd_req_is_ver1(or) ?
215                 sizeof(struct osdv1_attributes_list_header) :
216                 sizeof(struct osdv2_attributes_list_header);
217 }
218
219 static void _osd_req_set_alist_type(struct osd_request *or,
220         void *list, int list_type)
221 {
222         if (osd_req_is_ver1(or)) {
223                 struct osdv1_attributes_list_header *attr_list = list;
224
225                 memset(attr_list, 0, sizeof(*attr_list));
226                 attr_list->type = list_type;
227         } else {
228                 struct osdv2_attributes_list_header *attr_list = list;
229
230                 memset(attr_list, 0, sizeof(*attr_list));
231                 attr_list->type = list_type;
232         }
233 }
234
235 static bool _osd_req_is_alist_type(struct osd_request *or,
236         void *list, int list_type)
237 {
238         if (!list)
239                 return false;
240
241         if (osd_req_is_ver1(or)) {
242                 struct osdv1_attributes_list_header *attr_list = list;
243
244                 return attr_list->type == list_type;
245         } else {
246                 struct osdv2_attributes_list_header *attr_list = list;
247
248                 return attr_list->type == list_type;
249         }
250 }
251
252 /* This is for List-objects not Attributes-Lists */
253 static void _osd_req_encode_olist(struct osd_request *or,
254         struct osd_obj_id_list *list)
255 {
256         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
257
258         if (osd_req_is_ver1(or)) {
259                 cdbh->v1.list_identifier = list->list_identifier;
260                 cdbh->v1.start_address = list->continuation_id;
261         } else {
262                 cdbh->v2.list_identifier = list->list_identifier;
263                 cdbh->v2.start_address = list->continuation_id;
264         }
265 }
266
267 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
268         u64 offset, unsigned *padding)
269 {
270         return __osd_encode_offset(offset, padding,
271                         osd_req_is_ver1(or) ?
272                                 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
273                         OSD_OFFSET_MAX_SHIFT);
274 }
275
276 static struct osd_security_parameters *
277 _osd_req_sec_params(struct osd_request *or)
278 {
279         struct osd_cdb *ocdb = &or->cdb;
280
281         if (osd_req_is_ver1(or))
282                 return &ocdb->v1.sec_params;
283         else
284                 return &ocdb->v2.sec_params;
285 }
286
287 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
288 {
289         memset(osdd, 0, sizeof(*osdd));
290         osdd->scsi_device = scsi_device;
291         osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
292 #ifdef OSD_VER1_SUPPORT
293         osdd->version = OSD_VER2;
294 #endif
295         /* TODO: Allocate pools for osd_request attributes ... */
296 }
297 EXPORT_SYMBOL(osd_dev_init);
298
299 void osd_dev_fini(struct osd_dev *osdd)
300 {
301         /* TODO: De-allocate pools */
302
303         osdd->scsi_device = NULL;
304 }
305 EXPORT_SYMBOL(osd_dev_fini);
306
307 static struct osd_request *_osd_request_alloc(gfp_t gfp)
308 {
309         struct osd_request *or;
310
311         /* TODO: Use mempool with one saved request */
312         or = kzalloc(sizeof(*or), gfp);
313         return or;
314 }
315
316 static void _osd_request_free(struct osd_request *or)
317 {
318         kfree(or);
319 }
320
321 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
322 {
323         struct osd_request *or;
324
325         or = _osd_request_alloc(gfp);
326         if (!or)
327                 return NULL;
328
329         or->osd_dev = dev;
330         or->alloc_flags = gfp;
331         or->timeout = dev->def_timeout;
332         or->retries = OSD_REQ_RETRIES;
333
334         return or;
335 }
336 EXPORT_SYMBOL(osd_start_request);
337
338 /*
339  * If osd_finalize_request() was called but the request was not executed through
340  * the block layer, then we must release BIOs.
341  */
342 static void _abort_unexecuted_bios(struct request *rq)
343 {
344         struct bio *bio;
345
346         while ((bio = rq->bio) != NULL) {
347                 rq->bio = bio->bi_next;
348                 bio_endio(bio, 0);
349         }
350 }
351
352 static void _osd_free_seg(struct osd_request *or __unused,
353         struct _osd_req_data_segment *seg)
354 {
355         if (!seg->buff || !seg->alloc_size)
356                 return;
357
358         kfree(seg->buff);
359         seg->buff = NULL;
360         seg->alloc_size = 0;
361 }
362
363 void osd_end_request(struct osd_request *or)
364 {
365         struct request *rq = or->request;
366
367         _osd_free_seg(or, &or->set_attr);
368         _osd_free_seg(or, &or->enc_get_attr);
369         _osd_free_seg(or, &or->get_attr);
370
371         if (rq) {
372                 if (rq->next_rq) {
373                         _abort_unexecuted_bios(rq->next_rq);
374                         blk_put_request(rq->next_rq);
375                 }
376
377                 _abort_unexecuted_bios(rq);
378                 blk_put_request(rq);
379         }
380         _osd_request_free(or);
381 }
382 EXPORT_SYMBOL(osd_end_request);
383
384 int osd_execute_request(struct osd_request *or)
385 {
386         return blk_execute_rq(or->request->q, NULL, or->request, 0);
387 }
388 EXPORT_SYMBOL(osd_execute_request);
389
390 static void osd_request_async_done(struct request *req, int error)
391 {
392         struct osd_request *or = req->end_io_data;
393
394         or->async_error = error;
395
396         if (error)
397                 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
398
399         if (or->async_done)
400                 or->async_done(or, or->async_private);
401         else
402                 osd_end_request(or);
403 }
404
405 int osd_execute_request_async(struct osd_request *or,
406         osd_req_done_fn *done, void *private)
407 {
408         or->request->end_io_data = or;
409         or->async_private = private;
410         or->async_done = done;
411
412         blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
413                               osd_request_async_done);
414         return 0;
415 }
416 EXPORT_SYMBOL(osd_execute_request_async);
417
418 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
419 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
420
421 static int _osd_realloc_seg(struct osd_request *or,
422         struct _osd_req_data_segment *seg, unsigned max_bytes)
423 {
424         void *buff;
425
426         if (seg->alloc_size >= max_bytes)
427                 return 0;
428
429         buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
430         if (!buff) {
431                 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
432                         seg->alloc_size);
433                 return -ENOMEM;
434         }
435
436         memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
437         seg->buff = buff;
438         seg->alloc_size = max_bytes;
439         return 0;
440 }
441
442 static int _alloc_set_attr_list(struct osd_request *or,
443         const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
444 {
445         unsigned total_bytes = add_bytes;
446
447         for (; nelem; --nelem, ++oa)
448                 total_bytes += _osd_req_alist_elem_size(or, oa->len);
449
450         OSD_DEBUG("total_bytes=%d\n", total_bytes);
451         return _osd_realloc_seg(or, &or->set_attr, total_bytes);
452 }
453
454 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
455 {
456         OSD_DEBUG("total_bytes=%d\n", max_bytes);
457         return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
458 }
459
460 static int _alloc_get_attr_list(struct osd_request *or)
461 {
462         OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
463         return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
464 }
465
466 /*
467  * Common to all OSD commands
468  */
469
470 static void _osdv1_req_encode_common(struct osd_request *or,
471         __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
472 {
473         struct osdv1_cdb *ocdb = &or->cdb.v1;
474
475         /*
476          * For speed, the commands
477          *      OSD_ACT_PERFORM_SCSI_COMMAND    , V1 0x8F7E, V2 0x8F7C
478          *      OSD_ACT_SCSI_TASK_MANAGEMENT    , V1 0x8F7F, V2 0x8F7D
479          * are not supported here. Should pass zero and set after the call
480          */
481         act &= cpu_to_be16(~0x0080); /* V1 action code */
482
483         OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
484
485         ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
486         ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
487         ocdb->h.varlen_cdb.service_action = act;
488
489         ocdb->h.partition = cpu_to_be64(obj->partition);
490         ocdb->h.object = cpu_to_be64(obj->id);
491         ocdb->h.v1.length = cpu_to_be64(len);
492         ocdb->h.v1.start_address = cpu_to_be64(offset);
493 }
494
495 static void _osdv2_req_encode_common(struct osd_request *or,
496          __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
497 {
498         struct osdv2_cdb *ocdb = &or->cdb.v2;
499
500         OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
501
502         ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
503         ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
504         ocdb->h.varlen_cdb.service_action = act;
505
506         ocdb->h.partition = cpu_to_be64(obj->partition);
507         ocdb->h.object = cpu_to_be64(obj->id);
508         ocdb->h.v2.length = cpu_to_be64(len);
509         ocdb->h.v2.start_address = cpu_to_be64(offset);
510 }
511
512 static void _osd_req_encode_common(struct osd_request *or,
513         __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
514 {
515         if (osd_req_is_ver1(or))
516                 _osdv1_req_encode_common(or, act, obj, offset, len);
517         else
518                 _osdv2_req_encode_common(or, act, obj, offset, len);
519 }
520
521 /*
522  * Device commands
523  */
524 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
525 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
526
527 void osd_req_format(struct osd_request *or, u64 tot_capacity)
528 {
529         _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
530                                 tot_capacity);
531 }
532 EXPORT_SYMBOL(osd_req_format);
533
534 int osd_req_list_dev_partitions(struct osd_request *or,
535         osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
536 {
537         return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
538 }
539 EXPORT_SYMBOL(osd_req_list_dev_partitions);
540
541 static void _osd_req_encode_flush(struct osd_request *or,
542         enum osd_options_flush_scope_values op)
543 {
544         struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
545
546         ocdb->command_specific_options = op;
547 }
548
549 void osd_req_flush_obsd(struct osd_request *or,
550         enum osd_options_flush_scope_values op)
551 {
552         _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
553         _osd_req_encode_flush(or, op);
554 }
555 EXPORT_SYMBOL(osd_req_flush_obsd);
556
557 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
558         const u8 *cdb, ...); */
559 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
560
561 /*
562  * Partition commands
563  */
564 static void _osd_req_encode_partition(struct osd_request *or,
565         __be16 act, osd_id partition)
566 {
567         struct osd_obj_id par = {
568                 .partition = partition,
569                 .id = 0,
570         };
571
572         _osd_req_encode_common(or, act, &par, 0, 0);
573 }
574
575 void osd_req_create_partition(struct osd_request *or, osd_id partition)
576 {
577         _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
578 }
579 EXPORT_SYMBOL(osd_req_create_partition);
580
581 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
582 {
583         _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
584 }
585 EXPORT_SYMBOL(osd_req_remove_partition);
586
587 /*TODO: void osd_req_set_partition_key(struct osd_request *,
588         osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
589         u8 seed[OSD_CRYPTO_SEED_SIZE]); */
590
591 static int _osd_req_list_objects(struct osd_request *or,
592         __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
593         struct osd_obj_id_list *list, unsigned nelem)
594 {
595         struct request_queue *q = or->osd_dev->scsi_device->request_queue;
596         u64 len = nelem * sizeof(osd_id) + sizeof(*list);
597         struct bio *bio;
598
599         _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
600
601         if (list->list_identifier)
602                 _osd_req_encode_olist(or, list);
603
604         WARN_ON(or->in.bio);
605         bio = bio_map_kern(q, list, len, or->alloc_flags);
606         if (!bio) {
607                 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
608                 return -ENOMEM;
609         }
610
611         bio->bi_rw &= ~(1 << BIO_RW);
612         or->in.bio = bio;
613         or->in.total_bytes = bio->bi_size;
614         return 0;
615 }
616
617 int osd_req_list_partition_collections(struct osd_request *or,
618         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
619         unsigned nelem)
620 {
621         struct osd_obj_id par = {
622                 .partition = partition,
623                 .id = 0,
624         };
625
626         return osd_req_list_collection_objects(or, &par, initial_id, list,
627                                                nelem);
628 }
629 EXPORT_SYMBOL(osd_req_list_partition_collections);
630
631 int osd_req_list_partition_objects(struct osd_request *or,
632         osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
633         unsigned nelem)
634 {
635         struct osd_obj_id par = {
636                 .partition = partition,
637                 .id = 0,
638         };
639
640         return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
641                                      nelem);
642 }
643 EXPORT_SYMBOL(osd_req_list_partition_objects);
644
645 void osd_req_flush_partition(struct osd_request *or,
646         osd_id partition, enum osd_options_flush_scope_values op)
647 {
648         _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
649         _osd_req_encode_flush(or, op);
650 }
651 EXPORT_SYMBOL(osd_req_flush_partition);
652
653 /*
654  * Collection commands
655  */
656 /*TODO: void osd_req_create_collection(struct osd_request *,
657         const struct osd_obj_id *); */
658 /*TODO: void osd_req_remove_collection(struct osd_request *,
659         const struct osd_obj_id *); */
660
661 int osd_req_list_collection_objects(struct osd_request *or,
662         const struct osd_obj_id *obj, osd_id initial_id,
663         struct osd_obj_id_list *list, unsigned nelem)
664 {
665         return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
666                                      initial_id, list, nelem);
667 }
668 EXPORT_SYMBOL(osd_req_list_collection_objects);
669
670 /*TODO: void query(struct osd_request *, ...); V2 */
671
672 void osd_req_flush_collection(struct osd_request *or,
673         const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
674 {
675         _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
676         _osd_req_encode_flush(or, op);
677 }
678 EXPORT_SYMBOL(osd_req_flush_collection);
679
680 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
681 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
682
683 /*
684  * Object commands
685  */
686 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
687 {
688         _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
689 }
690 EXPORT_SYMBOL(osd_req_create_object);
691
692 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
693 {
694         _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
695 }
696 EXPORT_SYMBOL(osd_req_remove_object);
697
698
699 /*TODO: void osd_req_create_multi(struct osd_request *or,
700         struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
701 */
702
703 void osd_req_write(struct osd_request *or,
704         const struct osd_obj_id *obj, struct bio *bio, u64 offset)
705 {
706         _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
707         WARN_ON(or->out.bio || or->out.total_bytes);
708         bio->bi_rw |= (1 << BIO_RW);
709         or->out.bio = bio;
710         or->out.total_bytes = bio->bi_size;
711 }
712 EXPORT_SYMBOL(osd_req_write);
713
714 /*TODO: void osd_req_append(struct osd_request *,
715         const struct osd_obj_id *, struct bio *data_out); */
716 /*TODO: void osd_req_create_write(struct osd_request *,
717         const struct osd_obj_id *, struct bio *data_out, u64 offset); */
718 /*TODO: void osd_req_clear(struct osd_request *,
719         const struct osd_obj_id *, u64 offset, u64 len); */
720 /*TODO: void osd_req_punch(struct osd_request *,
721         const struct osd_obj_id *, u64 offset, u64 len); V2 */
722
723 void osd_req_flush_object(struct osd_request *or,
724         const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
725         /*V2*/ u64 offset, /*V2*/ u64 len)
726 {
727         if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
728                 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
729                 offset = 0;
730                 len = 0;
731         }
732
733         _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
734         _osd_req_encode_flush(or, op);
735 }
736 EXPORT_SYMBOL(osd_req_flush_object);
737
738 void osd_req_read(struct osd_request *or,
739         const struct osd_obj_id *obj, struct bio *bio, u64 offset)
740 {
741         _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
742         WARN_ON(or->in.bio || or->in.total_bytes);
743         bio->bi_rw &= ~(1 << BIO_RW);
744         or->in.bio = bio;
745         or->in.total_bytes = bio->bi_size;
746 }
747 EXPORT_SYMBOL(osd_req_read);
748
749 void osd_req_get_attributes(struct osd_request *or,
750         const struct osd_obj_id *obj)
751 {
752         _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
753 }
754 EXPORT_SYMBOL(osd_req_get_attributes);
755
756 void osd_req_set_attributes(struct osd_request *or,
757         const struct osd_obj_id *obj)
758 {
759         _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
760 }
761 EXPORT_SYMBOL(osd_req_set_attributes);
762
763 /*
764  * Attributes List-mode
765  */
766
767 int osd_req_add_set_attr_list(struct osd_request *or,
768         const struct osd_attr *oa, unsigned nelem)
769 {
770         unsigned total_bytes = or->set_attr.total_bytes;
771         void *attr_last;
772         int ret;
773
774         if (or->attributes_mode &&
775             or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
776                 WARN_ON(1);
777                 return -EINVAL;
778         }
779         or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
780
781         if (!total_bytes) { /* first-time: allocate and put list header */
782                 total_bytes = _osd_req_sizeof_alist_header(or);
783                 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
784                 if (ret)
785                         return ret;
786                 _osd_req_set_alist_type(or, or->set_attr.buff,
787                                         OSD_ATTR_LIST_SET_RETRIEVE);
788         }
789         attr_last = or->set_attr.buff + total_bytes;
790
791         for (; nelem; --nelem) {
792                 struct osd_attributes_list_element *attr;
793                 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
794
795                 total_bytes += elem_size;
796                 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
797                         or->set_attr.total_bytes = total_bytes - elem_size;
798                         ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
799                         if (ret)
800                                 return ret;
801                         attr_last =
802                                 or->set_attr.buff + or->set_attr.total_bytes;
803                 }
804
805                 attr = attr_last;
806                 attr->attr_page = cpu_to_be32(oa->attr_page);
807                 attr->attr_id = cpu_to_be32(oa->attr_id);
808                 attr->attr_bytes = cpu_to_be16(oa->len);
809                 memcpy(attr->attr_val, oa->val_ptr, oa->len);
810
811                 attr_last += elem_size;
812                 ++oa;
813         }
814
815         or->set_attr.total_bytes = total_bytes;
816         return 0;
817 }
818 EXPORT_SYMBOL(osd_req_add_set_attr_list);
819
820 static int _append_map_kern(struct request *req,
821         void *buff, unsigned len, gfp_t flags)
822 {
823         struct bio *bio;
824         int ret;
825
826         bio = bio_map_kern(req->q, buff, len, flags);
827         if (IS_ERR(bio)) {
828                 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
829                         PTR_ERR(bio));
830                 return PTR_ERR(bio);
831         }
832         ret = blk_rq_append_bio(req->q, req, bio);
833         if (ret) {
834                 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
835                 bio_put(bio);
836         }
837         return ret;
838 }
839
840 static int _req_append_segment(struct osd_request *or,
841         unsigned padding, struct _osd_req_data_segment *seg,
842         struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
843 {
844         void *pad_buff;
845         int ret;
846
847         if (padding) {
848                 /* check if we can just add it to last buffer */
849                 if (last_seg &&
850                     (padding <= last_seg->alloc_size - last_seg->total_bytes))
851                         pad_buff = last_seg->buff + last_seg->total_bytes;
852                 else
853                         pad_buff = io->pad_buff;
854
855                 ret = _append_map_kern(io->req, pad_buff, padding,
856                                        or->alloc_flags);
857                 if (ret)
858                         return ret;
859                 io->total_bytes += padding;
860         }
861
862         ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
863                                or->alloc_flags);
864         if (ret)
865                 return ret;
866
867         io->total_bytes += seg->total_bytes;
868         OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
869                   seg->total_bytes);
870         return 0;
871 }
872
873 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
874 {
875         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
876         unsigned padding;
877         int ret;
878
879         if (!or->set_attr.total_bytes) {
880                 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
881                 return 0;
882         }
883
884         cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
885         cdbh->attrs_list.set_attr_offset =
886                 osd_req_encode_offset(or, or->out.total_bytes, &padding);
887
888         ret = _req_append_segment(or, padding, &or->set_attr,
889                                   or->out.last_seg, &or->out);
890         if (ret)
891                 return ret;
892
893         or->out.last_seg = &or->set_attr;
894         return 0;
895 }
896
897 int osd_req_add_get_attr_list(struct osd_request *or,
898         const struct osd_attr *oa, unsigned nelem)
899 {
900         unsigned total_bytes = or->enc_get_attr.total_bytes;
901         void *attr_last;
902         int ret;
903
904         if (or->attributes_mode &&
905             or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
906                 WARN_ON(1);
907                 return -EINVAL;
908         }
909         or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
910
911         /* first time calc data-in list header size */
912         if (!or->get_attr.total_bytes)
913                 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
914
915         /* calc data-out info */
916         if (!total_bytes) { /* first-time: allocate and put list header */
917                 unsigned max_bytes;
918
919                 total_bytes = _osd_req_sizeof_alist_header(or);
920                 max_bytes = total_bytes +
921                         nelem * sizeof(struct osd_attributes_list_attrid);
922                 ret = _alloc_get_attr_desc(or, max_bytes);
923                 if (ret)
924                         return ret;
925
926                 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
927                                         OSD_ATTR_LIST_GET);
928         }
929         attr_last = or->enc_get_attr.buff + total_bytes;
930
931         for (; nelem; --nelem) {
932                 struct osd_attributes_list_attrid *attrid;
933                 const unsigned cur_size = sizeof(*attrid);
934
935                 total_bytes += cur_size;
936                 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
937                         or->enc_get_attr.total_bytes = total_bytes - cur_size;
938                         ret = _alloc_get_attr_desc(or,
939                                         total_bytes + nelem * sizeof(*attrid));
940                         if (ret)
941                                 return ret;
942                         attr_last = or->enc_get_attr.buff +
943                                 or->enc_get_attr.total_bytes;
944                 }
945
946                 attrid = attr_last;
947                 attrid->attr_page = cpu_to_be32(oa->attr_page);
948                 attrid->attr_id = cpu_to_be32(oa->attr_id);
949
950                 attr_last += cur_size;
951
952                 /* calc data-in size */
953                 or->get_attr.total_bytes +=
954                         _osd_req_alist_elem_size(or, oa->len);
955                 ++oa;
956         }
957
958         or->enc_get_attr.total_bytes = total_bytes;
959
960         OSD_DEBUG(
961                "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
962                or->get_attr.total_bytes,
963                or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
964                or->enc_get_attr.total_bytes,
965                (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
966                         / sizeof(struct osd_attributes_list_attrid));
967
968         return 0;
969 }
970 EXPORT_SYMBOL(osd_req_add_get_attr_list);
971
972 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
973 {
974         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
975         unsigned out_padding;
976         unsigned in_padding;
977         int ret;
978
979         if (!or->enc_get_attr.total_bytes) {
980                 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
981                 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
982                 return 0;
983         }
984
985         ret = _alloc_get_attr_list(or);
986         if (ret)
987                 return ret;
988
989         /* The out-going buffer info update */
990         OSD_DEBUG("out-going\n");
991         cdbh->attrs_list.get_attr_desc_bytes =
992                 cpu_to_be32(or->enc_get_attr.total_bytes);
993
994         cdbh->attrs_list.get_attr_desc_offset =
995                 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
996
997         ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
998                                   or->out.last_seg, &or->out);
999         if (ret)
1000                 return ret;
1001         or->out.last_seg = &or->enc_get_attr;
1002
1003         /* The incoming buffer info update */
1004         OSD_DEBUG("in-coming\n");
1005         cdbh->attrs_list.get_attr_alloc_length =
1006                 cpu_to_be32(or->get_attr.total_bytes);
1007
1008         cdbh->attrs_list.get_attr_offset =
1009                 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1010
1011         ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1012                                   &or->in);
1013         if (ret)
1014                 return ret;
1015         or->in.last_seg = &or->get_attr;
1016
1017         return 0;
1018 }
1019
1020 int osd_req_decode_get_attr_list(struct osd_request *or,
1021         struct osd_attr *oa, int *nelem, void **iterator)
1022 {
1023         unsigned cur_bytes, returned_bytes;
1024         int n;
1025         const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1026         void *cur_p;
1027
1028         if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1029                                     OSD_ATTR_LIST_SET_RETRIEVE)) {
1030                 oa->attr_page = 0;
1031                 oa->attr_id = 0;
1032                 oa->val_ptr = NULL;
1033                 oa->len = 0;
1034                 *iterator = NULL;
1035                 return 0;
1036         }
1037
1038         if (*iterator) {
1039                 BUG_ON((*iterator < or->get_attr.buff) ||
1040                      (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1041                 cur_p = *iterator;
1042                 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1043                 returned_bytes = or->get_attr.total_bytes;
1044         } else { /* first time decode the list header */
1045                 cur_bytes = sizeof_attr_list;
1046                 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1047                                         sizeof_attr_list;
1048
1049                 cur_p = or->get_attr.buff + sizeof_attr_list;
1050
1051                 if (returned_bytes > or->get_attr.alloc_size) {
1052                         OSD_DEBUG("target report: space was not big enough! "
1053                                   "Allocate=%u Needed=%u\n",
1054                                   or->get_attr.alloc_size,
1055                                   returned_bytes + sizeof_attr_list);
1056
1057                         returned_bytes =
1058                                 or->get_attr.alloc_size - sizeof_attr_list;
1059                 }
1060                 or->get_attr.total_bytes = returned_bytes;
1061         }
1062
1063         for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
1064                 struct osd_attributes_list_element *attr = cur_p;
1065                 unsigned inc;
1066
1067                 oa->len = be16_to_cpu(attr->attr_bytes);
1068                 inc = _osd_req_alist_elem_size(or, oa->len);
1069                 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
1070                           oa->len, inc, cur_bytes);
1071                 cur_bytes += inc;
1072                 if (cur_bytes > returned_bytes) {
1073                         OSD_ERR("BAD FOOD from target. list not valid!"
1074                                 "c=%d r=%d n=%d\n",
1075                                 cur_bytes, returned_bytes, n);
1076                         oa->val_ptr = NULL;
1077                         break;
1078                 }
1079
1080                 oa->attr_page = be32_to_cpu(attr->attr_page);
1081                 oa->attr_id = be32_to_cpu(attr->attr_id);
1082                 oa->val_ptr = attr->attr_val;
1083
1084                 cur_p += inc;
1085                 ++oa;
1086         }
1087
1088         *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1089         *nelem = n;
1090         return returned_bytes - cur_bytes;
1091 }
1092 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1093
1094 /*
1095  * Attributes Page-mode
1096  */
1097
1098 int osd_req_add_get_attr_page(struct osd_request *or,
1099         u32 page_id, void *attar_page, unsigned max_page_len,
1100         const struct osd_attr *set_one_attr)
1101 {
1102         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1103
1104         if (or->attributes_mode &&
1105             or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1106                 WARN_ON(1);
1107                 return -EINVAL;
1108         }
1109         or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1110
1111         or->get_attr.buff = attar_page;
1112         or->get_attr.total_bytes = max_page_len;
1113
1114         or->set_attr.buff = set_one_attr->val_ptr;
1115         or->set_attr.total_bytes = set_one_attr->len;
1116
1117         cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1118         cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1119         /* ocdb->attrs_page.get_attr_offset; */
1120
1121         cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1122         cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1123         cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1124         /* ocdb->attrs_page.set_attr_offset; */
1125         return 0;
1126 }
1127 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1128
1129 static int _osd_req_finalize_attr_page(struct osd_request *or)
1130 {
1131         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1132         unsigned in_padding, out_padding;
1133         int ret;
1134
1135         /* returned page */
1136         cdbh->attrs_page.get_attr_offset =
1137                 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1138
1139         ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1140                                   &or->in);
1141         if (ret)
1142                 return ret;
1143
1144         /* set one value */
1145         cdbh->attrs_page.set_attr_offset =
1146                 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1147
1148         ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1149                                   &or->out);
1150         return ret;
1151 }
1152
1153 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1154         bool has_in, bool has_out, const u8 *cap_key)
1155 {
1156         struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1157         int ret;
1158
1159         if (!osd_is_sec_alldata(sec_parms))
1160                 return 0;
1161
1162         if (has_out) {
1163                 struct _osd_req_data_segment seg = {
1164                         .buff = &or->out_data_integ,
1165                         .total_bytes = sizeof(or->out_data_integ),
1166                 };
1167                 unsigned pad;
1168
1169                 or->out_data_integ.data_bytes = cpu_to_be64(
1170                         or->out.bio ? or->out.bio->bi_size : 0);
1171                 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1172                         or->set_attr.total_bytes);
1173                 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1174                         or->enc_get_attr.total_bytes);
1175
1176                 sec_parms->data_out_integrity_check_offset =
1177                         osd_req_encode_offset(or, or->out.total_bytes, &pad);
1178
1179                 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1180                                           &or->out);
1181                 if (ret)
1182                         return ret;
1183                 or->out.last_seg = NULL;
1184
1185                 /* they are now all chained to request sign them all together */
1186                 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1187                                   cap_key);
1188         }
1189
1190         if (has_in) {
1191                 struct _osd_req_data_segment seg = {
1192                         .buff = &or->in_data_integ,
1193                         .total_bytes = sizeof(or->in_data_integ),
1194                 };
1195                 unsigned pad;
1196
1197                 sec_parms->data_in_integrity_check_offset =
1198                         osd_req_encode_offset(or, or->in.total_bytes, &pad);
1199
1200                 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1201                                           &or->in);
1202                 if (ret)
1203                         return ret;
1204
1205                 or->in.last_seg = NULL;
1206         }
1207
1208         return 0;
1209 }
1210
1211 /*
1212  * osd_finalize_request and helpers
1213  */
1214
1215 static int _init_blk_request(struct osd_request *or,
1216         bool has_in, bool has_out)
1217 {
1218         gfp_t flags = or->alloc_flags;
1219         struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1220         struct request_queue *q = scsi_device->request_queue;
1221         struct request *req;
1222         int ret = -ENOMEM;
1223
1224         req = blk_get_request(q, has_out, flags);
1225         if (!req)
1226                 goto out;
1227
1228         or->request = req;
1229         req->cmd_type = REQ_TYPE_BLOCK_PC;
1230         req->timeout = or->timeout;
1231         req->retries = or->retries;
1232         req->sense = or->sense;
1233         req->sense_len = 0;
1234
1235         if (has_out) {
1236                 or->out.req = req;
1237                 if (has_in) {
1238                         /* allocate bidi request */
1239                         req = blk_get_request(q, READ, flags);
1240                         if (!req) {
1241                                 OSD_DEBUG("blk_get_request for bidi failed\n");
1242                                 goto out;
1243                         }
1244                         req->cmd_type = REQ_TYPE_BLOCK_PC;
1245                         or->in.req = or->request->next_rq = req;
1246                 }
1247         } else if (has_in)
1248                 or->in.req = req;
1249
1250         ret = 0;
1251 out:
1252         OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1253                         or, has_in, has_out, ret, or->request);
1254         return ret;
1255 }
1256
1257 int osd_finalize_request(struct osd_request *or,
1258         u8 options, const void *cap, const u8 *cap_key)
1259 {
1260         struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1261         bool has_in, has_out;
1262         int ret;
1263
1264         if (options & OSD_REQ_FUA)
1265                 cdbh->options |= OSD_CDB_FUA;
1266
1267         if (options & OSD_REQ_DPO)
1268                 cdbh->options |= OSD_CDB_DPO;
1269
1270         if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1271                 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1272
1273         osd_set_caps(&or->cdb, cap);
1274
1275         has_in = or->in.bio || or->get_attr.total_bytes;
1276         has_out = or->out.bio || or->set_attr.total_bytes ||
1277                 or->enc_get_attr.total_bytes;
1278
1279         ret = _init_blk_request(or, has_in, has_out);
1280         if (ret) {
1281                 OSD_DEBUG("_init_blk_request failed\n");
1282                 return ret;
1283         }
1284
1285         if (or->out.bio) {
1286                 ret = blk_rq_append_bio(or->request->q, or->out.req,
1287                                         or->out.bio);
1288                 if (ret) {
1289                         OSD_DEBUG("blk_rq_append_bio out failed\n");
1290                         return ret;
1291                 }
1292                 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1293                         _LLU(or->out.total_bytes), or->out.req->data_len);
1294         }
1295         if (or->in.bio) {
1296                 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1297                 if (ret) {
1298                         OSD_DEBUG("blk_rq_append_bio in failed\n");
1299                         return ret;
1300                 }
1301                 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1302                         _LLU(or->in.total_bytes), or->in.req->data_len);
1303         }
1304
1305         or->out.pad_buff = sg_out_pad_buffer;
1306         or->in.pad_buff = sg_in_pad_buffer;
1307
1308         if (!or->attributes_mode)
1309                 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1310         cdbh->command_specific_options |= or->attributes_mode;
1311         if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1312                 ret = _osd_req_finalize_attr_page(or);
1313         } else {
1314                 /* TODO: I think that for the GET_ATTR command these 2 should
1315                  * be reversed to keep them in execution order (for embeded
1316                  * targets with low memory footprint)
1317                  */
1318                 ret = _osd_req_finalize_set_attr_list(or);
1319                 if (ret) {
1320                         OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1321                         return ret;
1322                 }
1323
1324                 ret = _osd_req_finalize_get_attr_list(or);
1325                 if (ret) {
1326                         OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1327                         return ret;
1328                 }
1329         }
1330
1331         ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1332         if (ret)
1333                 return ret;
1334
1335         osd_sec_sign_cdb(&or->cdb, cap_key);
1336
1337         or->request->cmd = or->cdb.buff;
1338         or->request->cmd_len = _osd_req_cdb_len(or);
1339
1340         return 0;
1341 }
1342 EXPORT_SYMBOL(osd_finalize_request);
1343
1344 #define OSD_SENSE_PRINT1(fmt, a...) \
1345         do { \
1346                 if (__cur_sense_need_output) \
1347                         OSD_ERR(fmt, ##a); \
1348         } while (0)
1349
1350 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1("    " fmt, ##a)
1351
1352 int osd_req_decode_sense_full(struct osd_request *or,
1353         struct osd_sense_info *osi, bool silent,
1354         struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1355         struct osd_attr *bad_attr_list, int max_attr)
1356 {
1357         int sense_len, original_sense_len;
1358         struct osd_sense_info local_osi;
1359         struct scsi_sense_descriptor_based *ssdb;
1360         void *cur_descriptor;
1361 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1362         const bool __cur_sense_need_output = false;
1363 #else
1364         bool __cur_sense_need_output = !silent;
1365 #endif
1366
1367         if (!or->request->errors)
1368                 return 0;
1369
1370         ssdb = or->request->sense;
1371         sense_len = or->request->sense_len;
1372         if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1373                 OSD_ERR("Block-layer returned error(0x%x) but "
1374                         "sense_len(%u) || key(%d) is empty\n",
1375                         or->request->errors, sense_len, ssdb->sense_key);
1376                 return -EIO;
1377         }
1378
1379         if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1380                 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1381                         ssdb->response_code, sense_len);
1382                 return -EIO;
1383         }
1384
1385         osi = osi ? : &local_osi;
1386         memset(osi, 0, sizeof(*osi));
1387         osi->key = ssdb->sense_key;
1388         osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1389         original_sense_len = ssdb->additional_sense_length + 8;
1390
1391 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1392         if (__cur_sense_need_output)
1393                 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1394 #endif
1395         OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1396                         "additional_code=0x%x\n",
1397                         osi->key, original_sense_len, sense_len,
1398                         osi->additional_code);
1399
1400         if (original_sense_len < sense_len)
1401                 sense_len = original_sense_len;
1402
1403         cur_descriptor = ssdb->ssd;
1404         sense_len -= sizeof(*ssdb);
1405         while (sense_len > 0) {
1406                 struct scsi_sense_descriptor *ssd = cur_descriptor;
1407                 int cur_len = ssd->additional_length + 2;
1408
1409                 sense_len -= cur_len;
1410
1411                 if (sense_len < 0)
1412                         break; /* sense was truncated */
1413
1414                 switch (ssd->descriptor_type) {
1415                 case scsi_sense_information:
1416                 case scsi_sense_command_specific_information:
1417                 {
1418                         struct scsi_sense_command_specific_data_descriptor
1419                                 *sscd = cur_descriptor;
1420
1421                         osi->command_info =
1422                                 get_unaligned_be64(&sscd->information) ;
1423                         OSD_SENSE_PRINT2(
1424                                 "command_specific_information 0x%llx \n",
1425                                 _LLU(osi->command_info));
1426                         break;
1427                 }
1428                 case scsi_sense_key_specific:
1429                 {
1430                         struct scsi_sense_key_specific_data_descriptor
1431                                 *ssks = cur_descriptor;
1432
1433                         osi->sense_info = get_unaligned_be16(&ssks->value);
1434                         OSD_SENSE_PRINT2(
1435                                 "sense_key_specific_information %u"
1436                                 "sksv_cd_bpv_bp (0x%x)\n",
1437                                 osi->sense_info, ssks->sksv_cd_bpv_bp);
1438                         break;
1439                 }
1440                 case osd_sense_object_identification:
1441                 { /*FIXME: Keep first not last, Store in array*/
1442                         struct osd_sense_identification_data_descriptor
1443                                 *osidd = cur_descriptor;
1444
1445                         osi->not_initiated_command_functions =
1446                                 le32_to_cpu(osidd->not_initiated_functions);
1447                         osi->completed_command_functions =
1448                                 le32_to_cpu(osidd->completed_functions);
1449                         osi->obj.partition = be64_to_cpu(osidd->partition_id);
1450                         osi->obj.id = be64_to_cpu(osidd->object_id);
1451                         OSD_SENSE_PRINT2(
1452                                 "object_identification pid=0x%llx oid=0x%llx\n",
1453                                 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1454                         OSD_SENSE_PRINT2(
1455                                 "not_initiated_bits(%x) "
1456                                 "completed_command_bits(%x)\n",
1457                                 osi->not_initiated_command_functions,
1458                                 osi->completed_command_functions);
1459                         break;
1460                 }
1461                 case osd_sense_response_integrity_check:
1462                 {
1463                         struct osd_sense_response_integrity_check_descriptor
1464                                 *osricd = cur_descriptor;
1465                         const unsigned len =
1466                                           sizeof(osricd->integrity_check_value);
1467                         char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1468
1469                         hex_dump_to_buffer(osricd->integrity_check_value, len,
1470                                        32, 1, key_dump, sizeof(key_dump), true);
1471                         OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1472                 }
1473                 case osd_sense_attribute_identification:
1474                 {
1475                         struct osd_sense_attributes_data_descriptor
1476                                 *osadd = cur_descriptor;
1477                         int len = min(cur_len, sense_len);
1478                         int i = 0;
1479                         struct osd_sense_attr *pattr = osadd->sense_attrs;
1480
1481                         while (len < 0) {
1482                                 u32 attr_page = be32_to_cpu(pattr->attr_page);
1483                                 u32 attr_id = be32_to_cpu(pattr->attr_id);
1484
1485                                 if (i++ == 0) {
1486                                         osi->attr.attr_page = attr_page;
1487                                         osi->attr.attr_id = attr_id;
1488                                 }
1489
1490                                 if (bad_attr_list && max_attr) {
1491                                         bad_attr_list->attr_page = attr_page;
1492                                         bad_attr_list->attr_id = attr_id;
1493                                         bad_attr_list++;
1494                                         max_attr--;
1495                                 }
1496                                 OSD_SENSE_PRINT2(
1497                                         "osd_sense_attribute_identification"
1498                                         "attr_page=0x%x attr_id=0x%x\n",
1499                                         attr_page, attr_id);
1500                         }
1501                 }
1502                 /*These are not legal for OSD*/
1503                 case scsi_sense_field_replaceable_unit:
1504                         OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1505                         break;
1506                 case scsi_sense_stream_commands:
1507                         OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1508                         break;
1509                 case scsi_sense_block_commands:
1510                         OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1511                         break;
1512                 case scsi_sense_ata_return:
1513                         OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1514                         break;
1515                 default:
1516                         if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1517                                 OSD_SENSE_PRINT2(
1518                                         "scsi_sense Reserved descriptor (0x%x)",
1519                                         ssd->descriptor_type);
1520                         else
1521                                 OSD_SENSE_PRINT2(
1522                                         "scsi_sense Vendor descriptor (0x%x)",
1523                                         ssd->descriptor_type);
1524                 }
1525
1526                 cur_descriptor += cur_len;
1527         }
1528
1529         return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1530 }
1531 EXPORT_SYMBOL(osd_req_decode_sense_full);
1532
1533 /*
1534  * Implementation of osd_sec.h API
1535  * TODO: Move to a separate osd_sec.c file at a later stage.
1536  */
1537
1538 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1539         OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE   |
1540         OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1541         OSD_SEC_CAP_WRITE  | OSD_SEC_CAP_READ     | OSD_SEC_CAP_POL_SEC  |
1542         OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1543 };
1544
1545 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1546         OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1547 };
1548
1549 void osd_sec_init_nosec_doall_caps(void *caps,
1550         const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1551 {
1552         struct osd_capability *cap = caps;
1553         u8 type;
1554         u8 descriptor_type;
1555
1556         if (likely(obj->id)) {
1557                 if (unlikely(is_collection)) {
1558                         type = OSD_SEC_OBJ_COLLECTION;
1559                         descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1560                                                   OSD_SEC_OBJ_DESC_COL;
1561                 } else {
1562                         type = OSD_SEC_OBJ_USER;
1563                         descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1564                 }
1565                 WARN_ON(!obj->partition);
1566         } else {
1567                 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1568                                         OSD_SEC_OBJ_ROOT;
1569                 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1570         }
1571
1572         memset(cap, 0, sizeof(*cap));
1573
1574         cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1575         cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1576         cap->h.security_method = OSD_SEC_NOSEC;
1577 /*      cap->expiration_time;
1578         cap->AUDIT[30-10];
1579         cap->discriminator[42-30];
1580         cap->object_created_time; */
1581         cap->h.object_type = type;
1582         osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1583         cap->h.object_descriptor_type = descriptor_type;
1584         cap->od.obj_desc.policy_access_tag = 0;
1585         cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1586         cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1587 }
1588 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1589
1590 /* FIXME: Extract version from caps pointer.
1591  *        Also Pete's target only supports caps from OSDv1 for now
1592  */
1593 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1594 {
1595         bool is_ver1 = true;
1596         /* NOTE: They start at same address */
1597         memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
1598 }
1599
1600 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1601 {
1602         return false;
1603 }
1604
1605 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1606 {
1607 }
1608
1609 void osd_sec_sign_data(void *data_integ __unused,
1610                        struct bio *bio __unused, const u8 *cap_key __unused)
1611 {
1612 }
1613
1614 /*
1615  * Declared in osd_protocol.h
1616  * 4.12.5 Data-In and Data-Out buffer offsets
1617  * byte offset = mantissa * (2^(exponent+8))
1618  * Returns the smallest allowed encoded offset that contains given @offset
1619  * The actual encoded offset returned is @offset + *@padding.
1620  */
1621 osd_cdb_offset __osd_encode_offset(
1622         u64 offset, unsigned *padding, int min_shift, int max_shift)
1623 {
1624         u64 try_offset = -1, mod, align;
1625         osd_cdb_offset be32_offset;
1626         int shift;
1627
1628         *padding = 0;
1629         if (!offset)
1630                 return 0;
1631
1632         for (shift = min_shift; shift < max_shift; ++shift) {
1633                 try_offset = offset >> shift;
1634                 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1635                         break;
1636         }
1637
1638         BUG_ON(shift == max_shift);
1639
1640         align = 1 << shift;
1641         mod = offset & (align - 1);
1642         if (mod) {
1643                 *padding = align - mod;
1644                 try_offset += 1;
1645         }
1646
1647         try_offset |= ((shift - 8) & 0xf) << 28;
1648         be32_offset = cpu_to_be32((u32)try_offset);
1649
1650         OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1651                  _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1652                  be32_offset, *padding);
1653         return be32_offset;
1654 }