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