2 * osd_initiator - Main body of the osd initiator library.
4 * Note: The file does not contain the advanced security functionality which
5 * is only needed by the security_manager's initiators.
7 * Copyright (C) 2008 Panasas Inc. All rights reserved.
10 * Boaz Harrosh <bharrosh@panasas.com>
11 * Benny Halevy <bhalevy@panasas.com>
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
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
42 #include <scsi/osd_initiator.h>
43 #include <scsi/osd_sec.h>
44 #include <scsi/osd_attributes.h>
45 #include <scsi/osd_sense.h>
47 #include <scsi/scsi_device.h>
49 #include "osd_debug.h"
52 # define __unused __attribute__((unused))
55 enum { OSD_REQ_RETRIES = 1 };
57 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
58 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
59 MODULE_LICENSE("GPL");
61 static inline void build_test(void)
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);
69 static const char *_osd_ver_desc(struct osd_request *or)
71 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
74 #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
76 static int _osd_print_system_info(struct osd_dev *od, void *caps)
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),
93 void *iter = NULL, *pFirst;
94 int nelem = ARRAY_SIZE(get_attrs), a = 0;
97 or = osd_start_request(od, GFP_KERNEL);
102 osd_req_get_attributes(or, &osd_root_object);
103 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
105 ret = osd_finalize_request(or, 0, caps, NULL);
109 ret = osd_execute_request(or);
111 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
115 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
117 OSD_INFO("Detected %s device\n",
120 pFirst = get_attrs[a++].val_ptr;
121 OSD_INFO("OSD_ATTR_RI_VENDOR_IDENTIFICATION [%s]\n",
124 pFirst = get_attrs[a++].val_ptr;
125 OSD_INFO("OSD_ATTR_RI_PRODUCT_IDENTIFICATION [%s]\n",
128 pFirst = get_attrs[a++].val_ptr;
129 OSD_INFO("OSD_ATTR_RI_PRODUCT_MODEL [%s]\n",
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);
136 pFirst = get_attrs[a++].val_ptr;
137 OSD_INFO("OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER [%s]\n",
140 pFirst = get_attrs[a].val_ptr;
141 OSD_INFO("OSD_ATTR_RI_OSD_NAME [%s]\n", (char *)pFirst);
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);
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);
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);
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]);
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 */
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);
180 int osd_auto_detect_ver(struct osd_dev *od, void *caps)
184 /* Auto-detect the osd version */
185 ret = _osd_print_system_info(od, caps);
187 osd_dev_set_ver(od, OSD_VER1);
188 OSD_DEBUG("converting to OSD1\n");
189 ret = _osd_print_system_info(od, caps);
194 EXPORT_SYMBOL(osd_auto_detect_ver);
196 static unsigned _osd_req_cdb_len(struct osd_request *or)
198 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
201 static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
203 return osd_req_is_ver1(or) ?
204 osdv1_attr_list_elem_size(len) :
205 osdv2_attr_list_elem_size(len);
208 static void _osd_req_alist_elem_encode(struct osd_request *or,
209 void *attr_last, const struct osd_attr *oa)
211 if (osd_req_is_ver1(or)) {
212 struct osdv1_attributes_list_element *attr = attr_last;
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);
219 struct osdv2_attributes_list_element *attr = attr_last;
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);
228 static int _osd_req_alist_elem_decode(struct osd_request *or,
229 void *cur_p, struct osd_attr *oa, unsigned max_bytes)
232 if (osd_req_is_ver1(or)) {
233 struct osdv1_attributes_list_element *attr = cur_p;
235 if (max_bytes < sizeof(*attr))
238 oa->len = be16_to_cpu(attr->attr_bytes);
239 inc = _osd_req_alist_elem_size(or, oa->len);
243 oa->attr_page = be32_to_cpu(attr->attr_page);
244 oa->attr_id = be32_to_cpu(attr->attr_id);
246 /* OSD1: On empty attributes we return a pointer to 2 bytes
247 * of zeros. This keeps similar behaviour with OSD2.
250 oa->val_ptr = likely(oa->len) ? attr->attr_val :
251 (u8 *)&attr->attr_bytes;
253 struct osdv2_attributes_list_element *attr = cur_p;
255 if (max_bytes < sizeof(*attr))
258 oa->len = be16_to_cpu(attr->attr_bytes);
259 inc = _osd_req_alist_elem_size(or, oa->len);
263 oa->attr_page = be32_to_cpu(attr->attr_page);
264 oa->attr_id = be32_to_cpu(attr->attr_id);
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.
271 oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
276 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
278 return osd_req_is_ver1(or) ?
279 osdv1_list_size(list_head) :
280 osdv2_list_size(list_head);
283 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
285 return osd_req_is_ver1(or) ?
286 sizeof(struct osdv1_attributes_list_header) :
287 sizeof(struct osdv2_attributes_list_header);
290 static void _osd_req_set_alist_type(struct osd_request *or,
291 void *list, int list_type)
293 if (osd_req_is_ver1(or)) {
294 struct osdv1_attributes_list_header *attr_list = list;
296 memset(attr_list, 0, sizeof(*attr_list));
297 attr_list->type = list_type;
299 struct osdv2_attributes_list_header *attr_list = list;
301 memset(attr_list, 0, sizeof(*attr_list));
302 attr_list->type = list_type;
306 static bool _osd_req_is_alist_type(struct osd_request *or,
307 void *list, int list_type)
312 if (osd_req_is_ver1(or)) {
313 struct osdv1_attributes_list_header *attr_list = list;
315 return attr_list->type == list_type;
317 struct osdv2_attributes_list_header *attr_list = list;
319 return attr_list->type == list_type;
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)
327 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
329 if (osd_req_is_ver1(or)) {
330 cdbh->v1.list_identifier = list->list_identifier;
331 cdbh->v1.start_address = list->continuation_id;
333 cdbh->v2.list_identifier = list->list_identifier;
334 cdbh->v2.start_address = list->continuation_id;
338 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
339 u64 offset, unsigned *padding)
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);
347 static struct osd_security_parameters *
348 _osd_req_sec_params(struct osd_request *or)
350 struct osd_cdb *ocdb = &or->cdb;
352 if (osd_req_is_ver1(or))
353 return (struct osd_security_parameters *)&ocdb->v1.sec_params;
355 return (struct osd_security_parameters *)&ocdb->v2.sec_params;
358 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
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;
366 /* TODO: Allocate pools for osd_request attributes ... */
368 EXPORT_SYMBOL(osd_dev_init);
370 void osd_dev_fini(struct osd_dev *osdd)
372 /* TODO: De-allocate pools */
374 osdd->scsi_device = NULL;
376 EXPORT_SYMBOL(osd_dev_fini);
378 static struct osd_request *_osd_request_alloc(gfp_t gfp)
380 struct osd_request *or;
382 /* TODO: Use mempool with one saved request */
383 or = kzalloc(sizeof(*or), gfp);
387 static void _osd_request_free(struct osd_request *or)
392 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
394 struct osd_request *or;
396 or = _osd_request_alloc(gfp);
401 or->alloc_flags = gfp;
402 or->timeout = dev->def_timeout;
403 or->retries = OSD_REQ_RETRIES;
407 EXPORT_SYMBOL(osd_start_request);
409 static void _osd_free_seg(struct osd_request *or __unused,
410 struct _osd_req_data_segment *seg)
412 if (!seg->buff || !seg->alloc_size)
420 static void _put_request(struct request *rq , bool is_async)
424 __blk_put_request(rq->q, rq);
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
432 if (unlikely(rq->bio))
433 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
439 void osd_end_request(struct osd_request *or)
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);
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);
451 _put_request(rq->next_rq, is_async);
455 _put_request(rq, is_async);
457 _osd_request_free(or);
459 EXPORT_SYMBOL(osd_end_request);
461 int osd_execute_request(struct osd_request *or)
463 return blk_execute_rq(or->request->q, NULL, or->request, 0);
465 EXPORT_SYMBOL(osd_execute_request);
467 static void osd_request_async_done(struct request *req, int error)
469 struct osd_request *or = req->end_io_data;
471 or->async_error = error;
474 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
477 or->async_done(or, or->async_private);
482 int osd_execute_request_async(struct osd_request *or,
483 osd_req_done_fn *done, void *private)
485 or->request->end_io_data = or;
486 or->async_private = private;
487 or->async_done = done;
489 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
490 osd_request_async_done);
493 EXPORT_SYMBOL(osd_execute_request_async);
495 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
496 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
498 static int _osd_realloc_seg(struct osd_request *or,
499 struct _osd_req_data_segment *seg, unsigned max_bytes)
503 if (seg->alloc_size >= max_bytes)
506 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
508 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
513 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
515 seg->alloc_size = max_bytes;
519 static int _alloc_set_attr_list(struct osd_request *or,
520 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
522 unsigned total_bytes = add_bytes;
524 for (; nelem; --nelem, ++oa)
525 total_bytes += _osd_req_alist_elem_size(or, oa->len);
527 OSD_DEBUG("total_bytes=%d\n", total_bytes);
528 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
531 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
533 OSD_DEBUG("total_bytes=%d\n", max_bytes);
534 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
537 static int _alloc_get_attr_list(struct osd_request *or)
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);
544 * Common to all OSD commands
547 static void _osdv1_req_encode_common(struct osd_request *or,
548 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
550 struct osdv1_cdb *ocdb = &or->cdb.v1;
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
558 act &= cpu_to_be16(~0x0080); /* V1 action code */
560 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
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;
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);
572 static void _osdv2_req_encode_common(struct osd_request *or,
573 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
575 struct osdv2_cdb *ocdb = &or->cdb.v2;
577 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
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;
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);
589 static void _osd_req_encode_common(struct osd_request *or,
590 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
592 if (osd_req_is_ver1(or))
593 _osdv1_req_encode_common(or, act, obj, offset, len);
595 _osdv2_req_encode_common(or, act, obj, offset, len);
601 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
602 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
604 void osd_req_format(struct osd_request *or, u64 tot_capacity)
606 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
609 EXPORT_SYMBOL(osd_req_format);
611 int osd_req_list_dev_partitions(struct osd_request *or,
612 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
614 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
616 EXPORT_SYMBOL(osd_req_list_dev_partitions);
618 static void _osd_req_encode_flush(struct osd_request *or,
619 enum osd_options_flush_scope_values op)
621 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
623 ocdb->command_specific_options = op;
626 void osd_req_flush_obsd(struct osd_request *or,
627 enum osd_options_flush_scope_values op)
629 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
630 _osd_req_encode_flush(or, op);
632 EXPORT_SYMBOL(osd_req_flush_obsd);
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 *, ...); */
641 static void _osd_req_encode_partition(struct osd_request *or,
642 __be16 act, osd_id partition)
644 struct osd_obj_id par = {
645 .partition = partition,
649 _osd_req_encode_common(or, act, &par, 0, 0);
652 void osd_req_create_partition(struct osd_request *or, osd_id partition)
654 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
656 EXPORT_SYMBOL(osd_req_create_partition);
658 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
660 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
662 EXPORT_SYMBOL(osd_req_remove_partition);
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]); */
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)
672 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
673 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
676 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
678 if (list->list_identifier)
679 _osd_req_encode_olist(or, list);
682 bio = bio_map_kern(q, list, len, or->alloc_flags);
684 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
688 bio->bi_rw &= ~(1 << BIO_RW);
690 or->in.total_bytes = bio->bi_size;
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,
698 struct osd_obj_id par = {
699 .partition = partition,
703 return osd_req_list_collection_objects(or, &par, initial_id, list,
706 EXPORT_SYMBOL(osd_req_list_partition_collections);
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,
712 struct osd_obj_id par = {
713 .partition = partition,
717 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
720 EXPORT_SYMBOL(osd_req_list_partition_objects);
722 void osd_req_flush_partition(struct osd_request *or,
723 osd_id partition, enum osd_options_flush_scope_values op)
725 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
726 _osd_req_encode_flush(or, op);
728 EXPORT_SYMBOL(osd_req_flush_partition);
731 * Collection commands
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 *); */
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)
742 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
743 initial_id, list, nelem);
745 EXPORT_SYMBOL(osd_req_list_collection_objects);
747 /*TODO: void query(struct osd_request *, ...); V2 */
749 void osd_req_flush_collection(struct osd_request *or,
750 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
752 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
753 _osd_req_encode_flush(or, op);
755 EXPORT_SYMBOL(osd_req_flush_collection);
757 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
758 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
763 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
765 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
767 EXPORT_SYMBOL(osd_req_create_object);
769 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
771 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
773 EXPORT_SYMBOL(osd_req_remove_object);
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);
780 void osd_req_write(struct osd_request *or,
781 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
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);
787 or->out.total_bytes = bio->bi_size;
789 EXPORT_SYMBOL(osd_req_write);
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 */
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)
804 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
805 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
810 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
811 _osd_req_encode_flush(or, op);
813 EXPORT_SYMBOL(osd_req_flush_object);
815 void osd_req_read(struct osd_request *or,
816 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
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);
822 or->in.total_bytes = bio->bi_size;
824 EXPORT_SYMBOL(osd_req_read);
826 void osd_req_get_attributes(struct osd_request *or,
827 const struct osd_obj_id *obj)
829 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
831 EXPORT_SYMBOL(osd_req_get_attributes);
833 void osd_req_set_attributes(struct osd_request *or,
834 const struct osd_obj_id *obj)
836 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
838 EXPORT_SYMBOL(osd_req_set_attributes);
841 * Attributes List-mode
844 int osd_req_add_set_attr_list(struct osd_request *or,
845 const struct osd_attr *oa, unsigned nelem)
847 unsigned total_bytes = or->set_attr.total_bytes;
851 if (or->attributes_mode &&
852 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
856 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
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);
863 _osd_req_set_alist_type(or, or->set_attr.buff,
864 OSD_ATTR_LIST_SET_RETRIEVE);
866 attr_last = or->set_attr.buff + total_bytes;
868 for (; nelem; --nelem) {
869 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
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);
878 or->set_attr.buff + or->set_attr.total_bytes;
881 _osd_req_alist_elem_encode(or, attr_last, oa);
883 attr_last += elem_size;
887 or->set_attr.total_bytes = total_bytes;
890 EXPORT_SYMBOL(osd_req_add_set_attr_list);
892 static int _append_map_kern(struct request *req,
893 void *buff, unsigned len, gfp_t flags)
898 bio = bio_map_kern(req->q, buff, len, flags);
900 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
904 ret = blk_rq_append_bio(req->q, req, bio);
906 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
912 static int _req_append_segment(struct osd_request *or,
913 unsigned padding, struct _osd_req_data_segment *seg,
914 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
920 /* check if we can just add it to last buffer */
922 (padding <= last_seg->alloc_size - last_seg->total_bytes))
923 pad_buff = last_seg->buff + last_seg->total_bytes;
925 pad_buff = io->pad_buff;
927 ret = _append_map_kern(io->req, pad_buff, padding,
931 io->total_bytes += padding;
934 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
939 io->total_bytes += seg->total_bytes;
940 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
945 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
947 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
951 if (!or->set_attr.total_bytes) {
952 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
956 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
957 cdbh->attrs_list.set_attr_offset =
958 osd_req_encode_offset(or, or->out.total_bytes, &padding);
960 ret = _req_append_segment(or, padding, &or->set_attr,
961 or->out.last_seg, &or->out);
965 or->out.last_seg = &or->set_attr;
969 int osd_req_add_get_attr_list(struct osd_request *or,
970 const struct osd_attr *oa, unsigned nelem)
972 unsigned total_bytes = or->enc_get_attr.total_bytes;
976 if (or->attributes_mode &&
977 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
981 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
983 /* first time calc data-in list header size */
984 if (!or->get_attr.total_bytes)
985 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
987 /* calc data-out info */
988 if (!total_bytes) { /* first-time: allocate and put list header */
991 total_bytes = _osd_req_sizeof_alist_header(or);
992 max_bytes = total_bytes +
993 nelem * sizeof(struct osd_attributes_list_attrid);
994 ret = _alloc_get_attr_desc(or, max_bytes);
998 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
1001 attr_last = or->enc_get_attr.buff + total_bytes;
1003 for (; nelem; --nelem) {
1004 struct osd_attributes_list_attrid *attrid;
1005 const unsigned cur_size = sizeof(*attrid);
1007 total_bytes += cur_size;
1008 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
1009 or->enc_get_attr.total_bytes = total_bytes - cur_size;
1010 ret = _alloc_get_attr_desc(or,
1011 total_bytes + nelem * sizeof(*attrid));
1014 attr_last = or->enc_get_attr.buff +
1015 or->enc_get_attr.total_bytes;
1019 attrid->attr_page = cpu_to_be32(oa->attr_page);
1020 attrid->attr_id = cpu_to_be32(oa->attr_id);
1022 attr_last += cur_size;
1024 /* calc data-in size */
1025 or->get_attr.total_bytes +=
1026 _osd_req_alist_elem_size(or, oa->len);
1030 or->enc_get_attr.total_bytes = total_bytes;
1033 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1034 or->get_attr.total_bytes,
1035 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1036 or->enc_get_attr.total_bytes,
1037 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1038 / sizeof(struct osd_attributes_list_attrid));
1042 EXPORT_SYMBOL(osd_req_add_get_attr_list);
1044 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1046 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1047 unsigned out_padding;
1048 unsigned in_padding;
1051 if (!or->enc_get_attr.total_bytes) {
1052 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
1053 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
1057 ret = _alloc_get_attr_list(or);
1061 /* The out-going buffer info update */
1062 OSD_DEBUG("out-going\n");
1063 cdbh->attrs_list.get_attr_desc_bytes =
1064 cpu_to_be32(or->enc_get_attr.total_bytes);
1066 cdbh->attrs_list.get_attr_desc_offset =
1067 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1069 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1070 or->out.last_seg, &or->out);
1073 or->out.last_seg = &or->enc_get_attr;
1075 /* The incoming buffer info update */
1076 OSD_DEBUG("in-coming\n");
1077 cdbh->attrs_list.get_attr_alloc_length =
1078 cpu_to_be32(or->get_attr.total_bytes);
1080 cdbh->attrs_list.get_attr_offset =
1081 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1083 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1087 or->in.last_seg = &or->get_attr;
1092 int osd_req_decode_get_attr_list(struct osd_request *or,
1093 struct osd_attr *oa, int *nelem, void **iterator)
1095 unsigned cur_bytes, returned_bytes;
1097 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1100 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1101 OSD_ATTR_LIST_SET_RETRIEVE)) {
1111 BUG_ON((*iterator < or->get_attr.buff) ||
1112 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1114 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1115 returned_bytes = or->get_attr.total_bytes;
1116 } else { /* first time decode the list header */
1117 cur_bytes = sizeof_attr_list;
1118 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1121 cur_p = or->get_attr.buff + sizeof_attr_list;
1123 if (returned_bytes > or->get_attr.alloc_size) {
1124 OSD_DEBUG("target report: space was not big enough! "
1125 "Allocate=%u Needed=%u\n",
1126 or->get_attr.alloc_size,
1127 returned_bytes + sizeof_attr_list);
1130 or->get_attr.alloc_size - sizeof_attr_list;
1132 or->get_attr.total_bytes = returned_bytes;
1135 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
1136 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1137 returned_bytes - cur_bytes);
1140 OSD_ERR("BAD FOOD from target. list not valid!"
1142 cur_bytes, returned_bytes, n);
1152 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1154 return returned_bytes - cur_bytes;
1156 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1159 * Attributes Page-mode
1162 int osd_req_add_get_attr_page(struct osd_request *or,
1163 u32 page_id, void *attar_page, unsigned max_page_len,
1164 const struct osd_attr *set_one_attr)
1166 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1168 if (or->attributes_mode &&
1169 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1173 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1175 or->get_attr.buff = attar_page;
1176 or->get_attr.total_bytes = max_page_len;
1178 or->set_attr.buff = set_one_attr->val_ptr;
1179 or->set_attr.total_bytes = set_one_attr->len;
1181 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1182 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1183 /* ocdb->attrs_page.get_attr_offset; */
1185 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1186 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1187 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1188 /* ocdb->attrs_page.set_attr_offset; */
1191 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1193 static int _osd_req_finalize_attr_page(struct osd_request *or)
1195 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1196 unsigned in_padding, out_padding;
1200 cdbh->attrs_page.get_attr_offset =
1201 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1203 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1209 cdbh->attrs_page.set_attr_offset =
1210 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1212 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1217 static inline void osd_sec_parms_set_out_offset(bool is_v1,
1218 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1221 sec_parms->v1.data_out_integrity_check_offset = offset;
1223 sec_parms->v2.data_out_integrity_check_offset = offset;
1226 static inline void osd_sec_parms_set_in_offset(bool is_v1,
1227 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1230 sec_parms->v1.data_in_integrity_check_offset = offset;
1232 sec_parms->v2.data_in_integrity_check_offset = offset;
1235 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1236 bool has_in, bool has_out, const u8 *cap_key)
1238 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1241 if (!osd_is_sec_alldata(sec_parms))
1245 struct _osd_req_data_segment seg = {
1246 .buff = &or->out_data_integ,
1247 .total_bytes = sizeof(or->out_data_integ),
1251 or->out_data_integ.data_bytes = cpu_to_be64(
1252 or->out.bio ? or->out.bio->bi_size : 0);
1253 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1254 or->set_attr.total_bytes);
1255 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1256 or->enc_get_attr.total_bytes);
1258 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1259 osd_req_encode_offset(or, or->out.total_bytes, &pad));
1261 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1265 or->out.last_seg = NULL;
1267 /* they are now all chained to request sign them all together */
1268 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1273 struct _osd_req_data_segment seg = {
1274 .buff = &or->in_data_integ,
1275 .total_bytes = sizeof(or->in_data_integ),
1279 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1280 osd_req_encode_offset(or, or->in.total_bytes, &pad));
1282 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1287 or->in.last_seg = NULL;
1294 * osd_finalize_request and helpers
1297 static int _init_blk_request(struct osd_request *or,
1298 bool has_in, bool has_out)
1300 gfp_t flags = or->alloc_flags;
1301 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1302 struct request_queue *q = scsi_device->request_queue;
1303 struct request *req;
1306 req = blk_get_request(q, has_out, flags);
1311 req->cmd_type = REQ_TYPE_BLOCK_PC;
1312 req->timeout = or->timeout;
1313 req->retries = or->retries;
1314 req->sense = or->sense;
1320 /* allocate bidi request */
1321 req = blk_get_request(q, READ, flags);
1323 OSD_DEBUG("blk_get_request for bidi failed\n");
1326 req->cmd_type = REQ_TYPE_BLOCK_PC;
1327 or->in.req = or->request->next_rq = req;
1334 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1335 or, has_in, has_out, ret, or->request);
1339 int osd_finalize_request(struct osd_request *or,
1340 u8 options, const void *cap, const u8 *cap_key)
1342 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1343 bool has_in, has_out;
1346 if (options & OSD_REQ_FUA)
1347 cdbh->options |= OSD_CDB_FUA;
1349 if (options & OSD_REQ_DPO)
1350 cdbh->options |= OSD_CDB_DPO;
1352 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1353 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1355 osd_set_caps(&or->cdb, cap);
1357 has_in = or->in.bio || or->get_attr.total_bytes;
1358 has_out = or->out.bio || or->set_attr.total_bytes ||
1359 or->enc_get_attr.total_bytes;
1361 ret = _init_blk_request(or, has_in, has_out);
1363 OSD_DEBUG("_init_blk_request failed\n");
1368 ret = blk_rq_append_bio(or->request->q, or->out.req,
1371 OSD_DEBUG("blk_rq_append_bio out failed\n");
1374 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1375 _LLU(or->out.total_bytes), or->out.req->data_len);
1378 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1380 OSD_DEBUG("blk_rq_append_bio in failed\n");
1383 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1384 _LLU(or->in.total_bytes), or->in.req->data_len);
1387 or->out.pad_buff = sg_out_pad_buffer;
1388 or->in.pad_buff = sg_in_pad_buffer;
1390 if (!or->attributes_mode)
1391 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1392 cdbh->command_specific_options |= or->attributes_mode;
1393 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1394 ret = _osd_req_finalize_attr_page(or);
1396 /* TODO: I think that for the GET_ATTR command these 2 should
1397 * be reversed to keep them in execution order (for embeded
1398 * targets with low memory footprint)
1400 ret = _osd_req_finalize_set_attr_list(or);
1402 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1406 ret = _osd_req_finalize_get_attr_list(or);
1408 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1413 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1417 osd_sec_sign_cdb(&or->cdb, cap_key);
1419 or->request->cmd = or->cdb.buff;
1420 or->request->cmd_len = _osd_req_cdb_len(or);
1424 EXPORT_SYMBOL(osd_finalize_request);
1426 #define OSD_SENSE_PRINT1(fmt, a...) \
1428 if (__cur_sense_need_output) \
1429 OSD_ERR(fmt, ##a); \
1432 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1434 int osd_req_decode_sense_full(struct osd_request *or,
1435 struct osd_sense_info *osi, bool silent,
1436 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1437 struct osd_attr *bad_attr_list, int max_attr)
1439 int sense_len, original_sense_len;
1440 struct osd_sense_info local_osi;
1441 struct scsi_sense_descriptor_based *ssdb;
1442 void *cur_descriptor;
1443 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1444 const bool __cur_sense_need_output = false;
1446 bool __cur_sense_need_output = !silent;
1449 if (!or->request->errors)
1452 ssdb = or->request->sense;
1453 sense_len = or->request->sense_len;
1454 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1455 OSD_ERR("Block-layer returned error(0x%x) but "
1456 "sense_len(%u) || key(%d) is empty\n",
1457 or->request->errors, sense_len, ssdb->sense_key);
1461 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1462 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1463 ssdb->response_code, sense_len);
1467 osi = osi ? : &local_osi;
1468 memset(osi, 0, sizeof(*osi));
1469 osi->key = ssdb->sense_key;
1470 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1471 original_sense_len = ssdb->additional_sense_length + 8;
1473 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1474 if (__cur_sense_need_output)
1475 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1477 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1478 "additional_code=0x%x\n",
1479 osi->key, original_sense_len, sense_len,
1480 osi->additional_code);
1482 if (original_sense_len < sense_len)
1483 sense_len = original_sense_len;
1485 cur_descriptor = ssdb->ssd;
1486 sense_len -= sizeof(*ssdb);
1487 while (sense_len > 0) {
1488 struct scsi_sense_descriptor *ssd = cur_descriptor;
1489 int cur_len = ssd->additional_length + 2;
1491 sense_len -= cur_len;
1494 break; /* sense was truncated */
1496 switch (ssd->descriptor_type) {
1497 case scsi_sense_information:
1498 case scsi_sense_command_specific_information:
1500 struct scsi_sense_command_specific_data_descriptor
1501 *sscd = cur_descriptor;
1504 get_unaligned_be64(&sscd->information) ;
1506 "command_specific_information 0x%llx \n",
1507 _LLU(osi->command_info));
1510 case scsi_sense_key_specific:
1512 struct scsi_sense_key_specific_data_descriptor
1513 *ssks = cur_descriptor;
1515 osi->sense_info = get_unaligned_be16(&ssks->value);
1517 "sense_key_specific_information %u"
1518 "sksv_cd_bpv_bp (0x%x)\n",
1519 osi->sense_info, ssks->sksv_cd_bpv_bp);
1522 case osd_sense_object_identification:
1523 { /*FIXME: Keep first not last, Store in array*/
1524 struct osd_sense_identification_data_descriptor
1525 *osidd = cur_descriptor;
1527 osi->not_initiated_command_functions =
1528 le32_to_cpu(osidd->not_initiated_functions);
1529 osi->completed_command_functions =
1530 le32_to_cpu(osidd->completed_functions);
1531 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1532 osi->obj.id = be64_to_cpu(osidd->object_id);
1534 "object_identification pid=0x%llx oid=0x%llx\n",
1535 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1537 "not_initiated_bits(%x) "
1538 "completed_command_bits(%x)\n",
1539 osi->not_initiated_command_functions,
1540 osi->completed_command_functions);
1543 case osd_sense_response_integrity_check:
1545 struct osd_sense_response_integrity_check_descriptor
1546 *osricd = cur_descriptor;
1547 const unsigned len =
1548 sizeof(osricd->integrity_check_value);
1549 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1551 hex_dump_to_buffer(osricd->integrity_check_value, len,
1552 32, 1, key_dump, sizeof(key_dump), true);
1553 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1555 case osd_sense_attribute_identification:
1557 struct osd_sense_attributes_data_descriptor
1558 *osadd = cur_descriptor;
1559 int len = min(cur_len, sense_len);
1561 struct osd_sense_attr *pattr = osadd->sense_attrs;
1564 u32 attr_page = be32_to_cpu(pattr->attr_page);
1565 u32 attr_id = be32_to_cpu(pattr->attr_id);
1568 osi->attr.attr_page = attr_page;
1569 osi->attr.attr_id = attr_id;
1572 if (bad_attr_list && max_attr) {
1573 bad_attr_list->attr_page = attr_page;
1574 bad_attr_list->attr_id = attr_id;
1579 "osd_sense_attribute_identification"
1580 "attr_page=0x%x attr_id=0x%x\n",
1581 attr_page, attr_id);
1584 /*These are not legal for OSD*/
1585 case scsi_sense_field_replaceable_unit:
1586 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1588 case scsi_sense_stream_commands:
1589 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1591 case scsi_sense_block_commands:
1592 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1594 case scsi_sense_ata_return:
1595 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1598 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1600 "scsi_sense Reserved descriptor (0x%x)",
1601 ssd->descriptor_type);
1604 "scsi_sense Vendor descriptor (0x%x)",
1605 ssd->descriptor_type);
1608 cur_descriptor += cur_len;
1611 return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1613 EXPORT_SYMBOL(osd_req_decode_sense_full);
1616 * Implementation of osd_sec.h API
1617 * TODO: Move to a separate osd_sec.c file at a later stage.
1620 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1621 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1622 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1623 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1624 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1627 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1628 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1631 void osd_sec_init_nosec_doall_caps(void *caps,
1632 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1634 struct osd_capability *cap = caps;
1638 if (likely(obj->id)) {
1639 if (unlikely(is_collection)) {
1640 type = OSD_SEC_OBJ_COLLECTION;
1641 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1642 OSD_SEC_OBJ_DESC_COL;
1644 type = OSD_SEC_OBJ_USER;
1645 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1647 WARN_ON(!obj->partition);
1649 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1651 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1654 memset(cap, 0, sizeof(*cap));
1656 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1657 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1658 cap->h.security_method = OSD_SEC_NOSEC;
1659 /* cap->expiration_time;
1661 cap->discriminator[42-30];
1662 cap->object_created_time; */
1663 cap->h.object_type = type;
1664 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1665 cap->h.object_descriptor_type = descriptor_type;
1666 cap->od.obj_desc.policy_access_tag = 0;
1667 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1668 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1670 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1672 /* FIXME: Extract version from caps pointer.
1673 * Also Pete's target only supports caps from OSDv1 for now
1675 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1677 bool is_ver1 = true;
1678 /* NOTE: They start at same address */
1679 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
1682 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1687 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1691 void osd_sec_sign_data(void *data_integ __unused,
1692 struct bio *bio __unused, const u8 *cap_key __unused)
1697 * Declared in osd_protocol.h
1698 * 4.12.5 Data-In and Data-Out buffer offsets
1699 * byte offset = mantissa * (2^(exponent+8))
1700 * Returns the smallest allowed encoded offset that contains given @offset
1701 * The actual encoded offset returned is @offset + *@padding.
1703 osd_cdb_offset __osd_encode_offset(
1704 u64 offset, unsigned *padding, int min_shift, int max_shift)
1706 u64 try_offset = -1, mod, align;
1707 osd_cdb_offset be32_offset;
1714 for (shift = min_shift; shift < max_shift; ++shift) {
1715 try_offset = offset >> shift;
1716 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1720 BUG_ON(shift == max_shift);
1723 mod = offset & (align - 1);
1725 *padding = align - mod;
1729 try_offset |= ((shift - 8) & 0xf) << 28;
1730 be32_offset = cpu_to_be32((u32)try_offset);
1732 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1733 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1734 be32_offset, *padding);