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