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 oa->val_ptr = attr->attr_val;
271 static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
273 return osd_req_is_ver1(or) ?
274 osdv1_list_size(list_head) :
275 osdv2_list_size(list_head);
278 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
280 return osd_req_is_ver1(or) ?
281 sizeof(struct osdv1_attributes_list_header) :
282 sizeof(struct osdv2_attributes_list_header);
285 static void _osd_req_set_alist_type(struct osd_request *or,
286 void *list, int list_type)
288 if (osd_req_is_ver1(or)) {
289 struct osdv1_attributes_list_header *attr_list = list;
291 memset(attr_list, 0, sizeof(*attr_list));
292 attr_list->type = list_type;
294 struct osdv2_attributes_list_header *attr_list = list;
296 memset(attr_list, 0, sizeof(*attr_list));
297 attr_list->type = list_type;
301 static bool _osd_req_is_alist_type(struct osd_request *or,
302 void *list, int list_type)
307 if (osd_req_is_ver1(or)) {
308 struct osdv1_attributes_list_header *attr_list = list;
310 return attr_list->type == list_type;
312 struct osdv2_attributes_list_header *attr_list = list;
314 return attr_list->type == list_type;
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)
322 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
324 if (osd_req_is_ver1(or)) {
325 cdbh->v1.list_identifier = list->list_identifier;
326 cdbh->v1.start_address = list->continuation_id;
328 cdbh->v2.list_identifier = list->list_identifier;
329 cdbh->v2.start_address = list->continuation_id;
333 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
334 u64 offset, unsigned *padding)
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);
342 static struct osd_security_parameters *
343 _osd_req_sec_params(struct osd_request *or)
345 struct osd_cdb *ocdb = &or->cdb;
347 if (osd_req_is_ver1(or))
348 return &ocdb->v1.sec_params;
350 return &ocdb->v2.sec_params;
353 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
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;
361 /* TODO: Allocate pools for osd_request attributes ... */
363 EXPORT_SYMBOL(osd_dev_init);
365 void osd_dev_fini(struct osd_dev *osdd)
367 /* TODO: De-allocate pools */
369 osdd->scsi_device = NULL;
371 EXPORT_SYMBOL(osd_dev_fini);
373 static struct osd_request *_osd_request_alloc(gfp_t gfp)
375 struct osd_request *or;
377 /* TODO: Use mempool with one saved request */
378 or = kzalloc(sizeof(*or), gfp);
382 static void _osd_request_free(struct osd_request *or)
387 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
389 struct osd_request *or;
391 or = _osd_request_alloc(gfp);
396 or->alloc_flags = gfp;
397 or->timeout = dev->def_timeout;
398 or->retries = OSD_REQ_RETRIES;
402 EXPORT_SYMBOL(osd_start_request);
404 static void _osd_free_seg(struct osd_request *or __unused,
405 struct _osd_req_data_segment *seg)
407 if (!seg->buff || !seg->alloc_size)
415 static void _put_request(struct request *rq , bool is_async)
419 __blk_put_request(rq->q, rq);
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
427 if (unlikely(rq->bio))
428 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
434 void osd_end_request(struct osd_request *or)
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);
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);
446 _put_request(rq->next_rq, is_async);
450 _put_request(rq, is_async);
452 _osd_request_free(or);
454 EXPORT_SYMBOL(osd_end_request);
456 int osd_execute_request(struct osd_request *or)
458 return blk_execute_rq(or->request->q, NULL, or->request, 0);
460 EXPORT_SYMBOL(osd_execute_request);
462 static void osd_request_async_done(struct request *req, int error)
464 struct osd_request *or = req->end_io_data;
466 or->async_error = error;
469 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
472 or->async_done(or, or->async_private);
477 int osd_execute_request_async(struct osd_request *or,
478 osd_req_done_fn *done, void *private)
480 or->request->end_io_data = or;
481 or->async_private = private;
482 or->async_done = done;
484 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
485 osd_request_async_done);
488 EXPORT_SYMBOL(osd_execute_request_async);
490 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
491 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
493 static int _osd_realloc_seg(struct osd_request *or,
494 struct _osd_req_data_segment *seg, unsigned max_bytes)
498 if (seg->alloc_size >= max_bytes)
501 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
503 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
508 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
510 seg->alloc_size = max_bytes;
514 static int _alloc_set_attr_list(struct osd_request *or,
515 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
517 unsigned total_bytes = add_bytes;
519 for (; nelem; --nelem, ++oa)
520 total_bytes += _osd_req_alist_elem_size(or, oa->len);
522 OSD_DEBUG("total_bytes=%d\n", total_bytes);
523 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
526 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
528 OSD_DEBUG("total_bytes=%d\n", max_bytes);
529 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
532 static int _alloc_get_attr_list(struct osd_request *or)
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);
539 * Common to all OSD commands
542 static void _osdv1_req_encode_common(struct osd_request *or,
543 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
545 struct osdv1_cdb *ocdb = &or->cdb.v1;
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
553 act &= cpu_to_be16(~0x0080); /* V1 action code */
555 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
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;
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);
567 static void _osdv2_req_encode_common(struct osd_request *or,
568 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
570 struct osdv2_cdb *ocdb = &or->cdb.v2;
572 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
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;
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);
584 static void _osd_req_encode_common(struct osd_request *or,
585 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
587 if (osd_req_is_ver1(or))
588 _osdv1_req_encode_common(or, act, obj, offset, len);
590 _osdv2_req_encode_common(or, act, obj, offset, len);
596 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
597 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
599 void osd_req_format(struct osd_request *or, u64 tot_capacity)
601 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
604 EXPORT_SYMBOL(osd_req_format);
606 int osd_req_list_dev_partitions(struct osd_request *or,
607 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
609 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
611 EXPORT_SYMBOL(osd_req_list_dev_partitions);
613 static void _osd_req_encode_flush(struct osd_request *or,
614 enum osd_options_flush_scope_values op)
616 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
618 ocdb->command_specific_options = op;
621 void osd_req_flush_obsd(struct osd_request *or,
622 enum osd_options_flush_scope_values op)
624 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
625 _osd_req_encode_flush(or, op);
627 EXPORT_SYMBOL(osd_req_flush_obsd);
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 *, ...); */
636 static void _osd_req_encode_partition(struct osd_request *or,
637 __be16 act, osd_id partition)
639 struct osd_obj_id par = {
640 .partition = partition,
644 _osd_req_encode_common(or, act, &par, 0, 0);
647 void osd_req_create_partition(struct osd_request *or, osd_id partition)
649 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
651 EXPORT_SYMBOL(osd_req_create_partition);
653 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
655 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
657 EXPORT_SYMBOL(osd_req_remove_partition);
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]); */
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)
667 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
668 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
671 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
673 if (list->list_identifier)
674 _osd_req_encode_olist(or, list);
677 bio = bio_map_kern(q, list, len, or->alloc_flags);
679 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
683 bio->bi_rw &= ~(1 << BIO_RW);
685 or->in.total_bytes = bio->bi_size;
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,
693 struct osd_obj_id par = {
694 .partition = partition,
698 return osd_req_list_collection_objects(or, &par, initial_id, list,
701 EXPORT_SYMBOL(osd_req_list_partition_collections);
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,
707 struct osd_obj_id par = {
708 .partition = partition,
712 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
715 EXPORT_SYMBOL(osd_req_list_partition_objects);
717 void osd_req_flush_partition(struct osd_request *or,
718 osd_id partition, enum osd_options_flush_scope_values op)
720 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
721 _osd_req_encode_flush(or, op);
723 EXPORT_SYMBOL(osd_req_flush_partition);
726 * Collection commands
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 *); */
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)
737 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
738 initial_id, list, nelem);
740 EXPORT_SYMBOL(osd_req_list_collection_objects);
742 /*TODO: void query(struct osd_request *, ...); V2 */
744 void osd_req_flush_collection(struct osd_request *or,
745 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
747 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
748 _osd_req_encode_flush(or, op);
750 EXPORT_SYMBOL(osd_req_flush_collection);
752 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
753 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
758 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
760 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
762 EXPORT_SYMBOL(osd_req_create_object);
764 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
766 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
768 EXPORT_SYMBOL(osd_req_remove_object);
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);
775 void osd_req_write(struct osd_request *or,
776 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
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);
782 or->out.total_bytes = bio->bi_size;
784 EXPORT_SYMBOL(osd_req_write);
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 */
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)
799 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
800 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
805 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
806 _osd_req_encode_flush(or, op);
808 EXPORT_SYMBOL(osd_req_flush_object);
810 void osd_req_read(struct osd_request *or,
811 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
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);
817 or->in.total_bytes = bio->bi_size;
819 EXPORT_SYMBOL(osd_req_read);
821 void osd_req_get_attributes(struct osd_request *or,
822 const struct osd_obj_id *obj)
824 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
826 EXPORT_SYMBOL(osd_req_get_attributes);
828 void osd_req_set_attributes(struct osd_request *or,
829 const struct osd_obj_id *obj)
831 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
833 EXPORT_SYMBOL(osd_req_set_attributes);
836 * Attributes List-mode
839 int osd_req_add_set_attr_list(struct osd_request *or,
840 const struct osd_attr *oa, unsigned nelem)
842 unsigned total_bytes = or->set_attr.total_bytes;
846 if (or->attributes_mode &&
847 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
851 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
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);
858 _osd_req_set_alist_type(or, or->set_attr.buff,
859 OSD_ATTR_LIST_SET_RETRIEVE);
861 attr_last = or->set_attr.buff + total_bytes;
863 for (; nelem; --nelem) {
864 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
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);
873 or->set_attr.buff + or->set_attr.total_bytes;
876 _osd_req_alist_elem_encode(or, attr_last, oa);
878 attr_last += elem_size;
882 or->set_attr.total_bytes = total_bytes;
885 EXPORT_SYMBOL(osd_req_add_set_attr_list);
887 static int _append_map_kern(struct request *req,
888 void *buff, unsigned len, gfp_t flags)
893 bio = bio_map_kern(req->q, buff, len, flags);
895 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
899 ret = blk_rq_append_bio(req->q, req, bio);
901 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
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)
915 /* check if we can just add it to last buffer */
917 (padding <= last_seg->alloc_size - last_seg->total_bytes))
918 pad_buff = last_seg->buff + last_seg->total_bytes;
920 pad_buff = io->pad_buff;
922 ret = _append_map_kern(io->req, pad_buff, padding,
926 io->total_bytes += padding;
929 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
934 io->total_bytes += seg->total_bytes;
935 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
940 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
942 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
946 if (!or->set_attr.total_bytes) {
947 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
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);
955 ret = _req_append_segment(or, padding, &or->set_attr,
956 or->out.last_seg, &or->out);
960 or->out.last_seg = &or->set_attr;
964 int osd_req_add_get_attr_list(struct osd_request *or,
965 const struct osd_attr *oa, unsigned nelem)
967 unsigned total_bytes = or->enc_get_attr.total_bytes;
971 if (or->attributes_mode &&
972 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
976 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
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);
982 /* calc data-out info */
983 if (!total_bytes) { /* first-time: allocate and put list header */
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);
993 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
996 attr_last = or->enc_get_attr.buff + total_bytes;
998 for (; nelem; --nelem) {
999 struct osd_attributes_list_attrid *attrid;
1000 const unsigned cur_size = sizeof(*attrid);
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));
1009 attr_last = or->enc_get_attr.buff +
1010 or->enc_get_attr.total_bytes;
1014 attrid->attr_page = cpu_to_be32(oa->attr_page);
1015 attrid->attr_id = cpu_to_be32(oa->attr_id);
1017 attr_last += cur_size;
1019 /* calc data-in size */
1020 or->get_attr.total_bytes +=
1021 _osd_req_alist_elem_size(or, oa->len);
1025 or->enc_get_attr.total_bytes = total_bytes;
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));
1037 EXPORT_SYMBOL(osd_req_add_get_attr_list);
1039 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1041 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1042 unsigned out_padding;
1043 unsigned in_padding;
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;
1052 ret = _alloc_get_attr_list(or);
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);
1061 cdbh->attrs_list.get_attr_desc_offset =
1062 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1064 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1065 or->out.last_seg, &or->out);
1068 or->out.last_seg = &or->enc_get_attr;
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);
1075 cdbh->attrs_list.get_attr_offset =
1076 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1078 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1082 or->in.last_seg = &or->get_attr;
1087 int osd_req_decode_get_attr_list(struct osd_request *or,
1088 struct osd_attr *oa, int *nelem, void **iterator)
1090 unsigned cur_bytes, returned_bytes;
1092 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1095 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1096 OSD_ATTR_LIST_SET_RETRIEVE)) {
1106 BUG_ON((*iterator < or->get_attr.buff) ||
1107 (or->get_attr.buff + or->get_attr.alloc_size < *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) +
1116 cur_p = or->get_attr.buff + sizeof_attr_list;
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);
1125 or->get_attr.alloc_size - sizeof_attr_list;
1127 or->get_attr.total_bytes = returned_bytes;
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);
1135 OSD_ERR("BAD FOOD from target. list not valid!"
1137 cur_bytes, returned_bytes, n);
1147 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1149 return returned_bytes - cur_bytes;
1151 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1154 * Attributes Page-mode
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)
1161 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1163 if (or->attributes_mode &&
1164 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1168 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1170 or->get_attr.buff = attar_page;
1171 or->get_attr.total_bytes = max_page_len;
1173 or->set_attr.buff = set_one_attr->val_ptr;
1174 or->set_attr.total_bytes = set_one_attr->len;
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; */
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; */
1186 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1188 static int _osd_req_finalize_attr_page(struct osd_request *or)
1190 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1191 unsigned in_padding, out_padding;
1195 cdbh->attrs_page.get_attr_offset =
1196 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1198 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1204 cdbh->attrs_page.set_attr_offset =
1205 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1207 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1212 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1213 bool has_in, bool has_out, const u8 *cap_key)
1215 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1218 if (!osd_is_sec_alldata(sec_parms))
1222 struct _osd_req_data_segment seg = {
1223 .buff = &or->out_data_integ,
1224 .total_bytes = sizeof(or->out_data_integ),
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);
1235 sec_parms->data_out_integrity_check_offset =
1236 osd_req_encode_offset(or, or->out.total_bytes, &pad);
1238 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1242 or->out.last_seg = NULL;
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,
1250 struct _osd_req_data_segment seg = {
1251 .buff = &or->in_data_integ,
1252 .total_bytes = sizeof(or->in_data_integ),
1256 sec_parms->data_in_integrity_check_offset =
1257 osd_req_encode_offset(or, or->in.total_bytes, &pad);
1259 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1264 or->in.last_seg = NULL;
1271 * osd_finalize_request and helpers
1274 static int _init_blk_request(struct osd_request *or,
1275 bool has_in, bool has_out)
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;
1283 req = blk_get_request(q, has_out, flags);
1288 req->cmd_type = REQ_TYPE_BLOCK_PC;
1289 req->timeout = or->timeout;
1290 req->retries = or->retries;
1291 req->sense = or->sense;
1297 /* allocate bidi request */
1298 req = blk_get_request(q, READ, flags);
1300 OSD_DEBUG("blk_get_request for bidi failed\n");
1303 req->cmd_type = REQ_TYPE_BLOCK_PC;
1304 or->in.req = or->request->next_rq = req;
1311 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1312 or, has_in, has_out, ret, or->request);
1316 int osd_finalize_request(struct osd_request *or,
1317 u8 options, const void *cap, const u8 *cap_key)
1319 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1320 bool has_in, has_out;
1323 if (options & OSD_REQ_FUA)
1324 cdbh->options |= OSD_CDB_FUA;
1326 if (options & OSD_REQ_DPO)
1327 cdbh->options |= OSD_CDB_DPO;
1329 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1330 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1332 osd_set_caps(&or->cdb, cap);
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;
1338 ret = _init_blk_request(or, has_in, has_out);
1340 OSD_DEBUG("_init_blk_request failed\n");
1345 ret = blk_rq_append_bio(or->request->q, or->out.req,
1348 OSD_DEBUG("blk_rq_append_bio out failed\n");
1351 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1352 _LLU(or->out.total_bytes), or->out.req->data_len);
1355 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1357 OSD_DEBUG("blk_rq_append_bio in failed\n");
1360 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1361 _LLU(or->in.total_bytes), or->in.req->data_len);
1364 or->out.pad_buff = sg_out_pad_buffer;
1365 or->in.pad_buff = sg_in_pad_buffer;
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);
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)
1377 ret = _osd_req_finalize_set_attr_list(or);
1379 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1383 ret = _osd_req_finalize_get_attr_list(or);
1385 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1390 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1394 osd_sec_sign_cdb(&or->cdb, cap_key);
1396 or->request->cmd = or->cdb.buff;
1397 or->request->cmd_len = _osd_req_cdb_len(or);
1401 EXPORT_SYMBOL(osd_finalize_request);
1403 #define OSD_SENSE_PRINT1(fmt, a...) \
1405 if (__cur_sense_need_output) \
1406 OSD_ERR(fmt, ##a); \
1409 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
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)
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;
1423 bool __cur_sense_need_output = !silent;
1426 if (!or->request->errors)
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);
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);
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;
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);
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);
1459 if (original_sense_len < sense_len)
1460 sense_len = original_sense_len;
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;
1468 sense_len -= cur_len;
1471 break; /* sense was truncated */
1473 switch (ssd->descriptor_type) {
1474 case scsi_sense_information:
1475 case scsi_sense_command_specific_information:
1477 struct scsi_sense_command_specific_data_descriptor
1478 *sscd = cur_descriptor;
1481 get_unaligned_be64(&sscd->information) ;
1483 "command_specific_information 0x%llx \n",
1484 _LLU(osi->command_info));
1487 case scsi_sense_key_specific:
1489 struct scsi_sense_key_specific_data_descriptor
1490 *ssks = cur_descriptor;
1492 osi->sense_info = get_unaligned_be16(&ssks->value);
1494 "sense_key_specific_information %u"
1495 "sksv_cd_bpv_bp (0x%x)\n",
1496 osi->sense_info, ssks->sksv_cd_bpv_bp);
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;
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);
1511 "object_identification pid=0x%llx oid=0x%llx\n",
1512 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1514 "not_initiated_bits(%x) "
1515 "completed_command_bits(%x)\n",
1516 osi->not_initiated_command_functions,
1517 osi->completed_command_functions);
1520 case osd_sense_response_integrity_check:
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 */
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);
1532 case osd_sense_attribute_identification:
1534 struct osd_sense_attributes_data_descriptor
1535 *osadd = cur_descriptor;
1536 int len = min(cur_len, sense_len);
1538 struct osd_sense_attr *pattr = osadd->sense_attrs;
1541 u32 attr_page = be32_to_cpu(pattr->attr_page);
1542 u32 attr_id = be32_to_cpu(pattr->attr_id);
1545 osi->attr.attr_page = attr_page;
1546 osi->attr.attr_id = attr_id;
1549 if (bad_attr_list && max_attr) {
1550 bad_attr_list->attr_page = attr_page;
1551 bad_attr_list->attr_id = attr_id;
1556 "osd_sense_attribute_identification"
1557 "attr_page=0x%x attr_id=0x%x\n",
1558 attr_page, attr_id);
1561 /*These are not legal for OSD*/
1562 case scsi_sense_field_replaceable_unit:
1563 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1565 case scsi_sense_stream_commands:
1566 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1568 case scsi_sense_block_commands:
1569 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1571 case scsi_sense_ata_return:
1572 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1575 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1577 "scsi_sense Reserved descriptor (0x%x)",
1578 ssd->descriptor_type);
1581 "scsi_sense Vendor descriptor (0x%x)",
1582 ssd->descriptor_type);
1585 cur_descriptor += cur_len;
1588 return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1590 EXPORT_SYMBOL(osd_req_decode_sense_full);
1593 * Implementation of osd_sec.h API
1594 * TODO: Move to a separate osd_sec.c file at a later stage.
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
1604 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1605 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1608 void osd_sec_init_nosec_doall_caps(void *caps,
1609 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1611 struct osd_capability *cap = caps;
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;
1621 type = OSD_SEC_OBJ_USER;
1622 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1624 WARN_ON(!obj->partition);
1626 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1628 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1631 memset(cap, 0, sizeof(*cap));
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;
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);
1647 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1649 /* FIXME: Extract version from caps pointer.
1650 * Also Pete's target only supports caps from OSDv1 for now
1652 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
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);
1659 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1664 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1668 void osd_sec_sign_data(void *data_integ __unused,
1669 struct bio *bio __unused, const u8 *cap_key __unused)
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.
1680 osd_cdb_offset __osd_encode_offset(
1681 u64 offset, unsigned *padding, int min_shift, int max_shift)
1683 u64 try_offset = -1, mod, align;
1684 osd_cdb_offset be32_offset;
1691 for (shift = min_shift; shift < max_shift; ++shift) {
1692 try_offset = offset >> shift;
1693 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1697 BUG_ON(shift == max_shift);
1700 mod = offset & (align - 1);
1702 *padding = align - mod;
1706 try_offset |= ((shift - 8) & 0xf) << 28;
1707 be32_offset = cpu_to_be32((u32)try_offset);
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);