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