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 unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
210 return osd_req_is_ver1(or) ?
211 osdv1_list_size(list_head) :
212 osdv2_list_size(list_head);
215 static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
217 return osd_req_is_ver1(or) ?
218 sizeof(struct osdv1_attributes_list_header) :
219 sizeof(struct osdv2_attributes_list_header);
222 static void _osd_req_set_alist_type(struct osd_request *or,
223 void *list, int list_type)
225 if (osd_req_is_ver1(or)) {
226 struct osdv1_attributes_list_header *attr_list = list;
228 memset(attr_list, 0, sizeof(*attr_list));
229 attr_list->type = list_type;
231 struct osdv2_attributes_list_header *attr_list = list;
233 memset(attr_list, 0, sizeof(*attr_list));
234 attr_list->type = list_type;
238 static bool _osd_req_is_alist_type(struct osd_request *or,
239 void *list, int list_type)
244 if (osd_req_is_ver1(or)) {
245 struct osdv1_attributes_list_header *attr_list = list;
247 return attr_list->type == list_type;
249 struct osdv2_attributes_list_header *attr_list = list;
251 return attr_list->type == list_type;
255 /* This is for List-objects not Attributes-Lists */
256 static void _osd_req_encode_olist(struct osd_request *or,
257 struct osd_obj_id_list *list)
259 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
261 if (osd_req_is_ver1(or)) {
262 cdbh->v1.list_identifier = list->list_identifier;
263 cdbh->v1.start_address = list->continuation_id;
265 cdbh->v2.list_identifier = list->list_identifier;
266 cdbh->v2.start_address = list->continuation_id;
270 static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
271 u64 offset, unsigned *padding)
273 return __osd_encode_offset(offset, padding,
274 osd_req_is_ver1(or) ?
275 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
276 OSD_OFFSET_MAX_SHIFT);
279 static struct osd_security_parameters *
280 _osd_req_sec_params(struct osd_request *or)
282 struct osd_cdb *ocdb = &or->cdb;
284 if (osd_req_is_ver1(or))
285 return &ocdb->v1.sec_params;
287 return &ocdb->v2.sec_params;
290 void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
292 memset(osdd, 0, sizeof(*osdd));
293 osdd->scsi_device = scsi_device;
294 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
295 #ifdef OSD_VER1_SUPPORT
296 osdd->version = OSD_VER2;
298 /* TODO: Allocate pools for osd_request attributes ... */
300 EXPORT_SYMBOL(osd_dev_init);
302 void osd_dev_fini(struct osd_dev *osdd)
304 /* TODO: De-allocate pools */
306 osdd->scsi_device = NULL;
308 EXPORT_SYMBOL(osd_dev_fini);
310 static struct osd_request *_osd_request_alloc(gfp_t gfp)
312 struct osd_request *or;
314 /* TODO: Use mempool with one saved request */
315 or = kzalloc(sizeof(*or), gfp);
319 static void _osd_request_free(struct osd_request *or)
324 struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
326 struct osd_request *or;
328 or = _osd_request_alloc(gfp);
333 or->alloc_flags = gfp;
334 or->timeout = dev->def_timeout;
335 or->retries = OSD_REQ_RETRIES;
339 EXPORT_SYMBOL(osd_start_request);
341 static void _osd_free_seg(struct osd_request *or __unused,
342 struct _osd_req_data_segment *seg)
344 if (!seg->buff || !seg->alloc_size)
352 static void _put_request(struct request *rq , bool is_async)
356 __blk_put_request(rq->q, rq);
359 * If osd_finalize_request() was called but the request was not
360 * executed through the block layer, then we must release BIOs.
361 * TODO: Keep error code in or->async_error. Need to audit all
364 if (unlikely(rq->bio))
365 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
371 void osd_end_request(struct osd_request *or)
373 struct request *rq = or->request;
374 /* IMPORTANT: make sure this agrees with osd_execute_request_async */
375 bool is_async = (or->request->end_io_data == or);
377 _osd_free_seg(or, &or->set_attr);
378 _osd_free_seg(or, &or->enc_get_attr);
379 _osd_free_seg(or, &or->get_attr);
383 _put_request(rq->next_rq, is_async);
387 _put_request(rq, is_async);
389 _osd_request_free(or);
391 EXPORT_SYMBOL(osd_end_request);
393 int osd_execute_request(struct osd_request *or)
395 return blk_execute_rq(or->request->q, NULL, or->request, 0);
397 EXPORT_SYMBOL(osd_execute_request);
399 static void osd_request_async_done(struct request *req, int error)
401 struct osd_request *or = req->end_io_data;
403 or->async_error = error;
406 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
409 or->async_done(or, or->async_private);
414 int osd_execute_request_async(struct osd_request *or,
415 osd_req_done_fn *done, void *private)
417 or->request->end_io_data = or;
418 or->async_private = private;
419 or->async_done = done;
421 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
422 osd_request_async_done);
425 EXPORT_SYMBOL(osd_execute_request_async);
427 u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
428 u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
430 static int _osd_realloc_seg(struct osd_request *or,
431 struct _osd_req_data_segment *seg, unsigned max_bytes)
435 if (seg->alloc_size >= max_bytes)
438 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
440 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
445 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
447 seg->alloc_size = max_bytes;
451 static int _alloc_set_attr_list(struct osd_request *or,
452 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
454 unsigned total_bytes = add_bytes;
456 for (; nelem; --nelem, ++oa)
457 total_bytes += _osd_req_alist_elem_size(or, oa->len);
459 OSD_DEBUG("total_bytes=%d\n", total_bytes);
460 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
463 static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
465 OSD_DEBUG("total_bytes=%d\n", max_bytes);
466 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
469 static int _alloc_get_attr_list(struct osd_request *or)
471 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
472 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
476 * Common to all OSD commands
479 static void _osdv1_req_encode_common(struct osd_request *or,
480 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
482 struct osdv1_cdb *ocdb = &or->cdb.v1;
485 * For speed, the commands
486 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
487 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
488 * are not supported here. Should pass zero and set after the call
490 act &= cpu_to_be16(~0x0080); /* V1 action code */
492 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
494 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
495 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
496 ocdb->h.varlen_cdb.service_action = act;
498 ocdb->h.partition = cpu_to_be64(obj->partition);
499 ocdb->h.object = cpu_to_be64(obj->id);
500 ocdb->h.v1.length = cpu_to_be64(len);
501 ocdb->h.v1.start_address = cpu_to_be64(offset);
504 static void _osdv2_req_encode_common(struct osd_request *or,
505 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
507 struct osdv2_cdb *ocdb = &or->cdb.v2;
509 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
511 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
512 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
513 ocdb->h.varlen_cdb.service_action = act;
515 ocdb->h.partition = cpu_to_be64(obj->partition);
516 ocdb->h.object = cpu_to_be64(obj->id);
517 ocdb->h.v2.length = cpu_to_be64(len);
518 ocdb->h.v2.start_address = cpu_to_be64(offset);
521 static void _osd_req_encode_common(struct osd_request *or,
522 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
524 if (osd_req_is_ver1(or))
525 _osdv1_req_encode_common(or, act, obj, offset, len);
527 _osdv2_req_encode_common(or, act, obj, offset, len);
533 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
534 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
536 void osd_req_format(struct osd_request *or, u64 tot_capacity)
538 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
541 EXPORT_SYMBOL(osd_req_format);
543 int osd_req_list_dev_partitions(struct osd_request *or,
544 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
546 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
548 EXPORT_SYMBOL(osd_req_list_dev_partitions);
550 static void _osd_req_encode_flush(struct osd_request *or,
551 enum osd_options_flush_scope_values op)
553 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
555 ocdb->command_specific_options = op;
558 void osd_req_flush_obsd(struct osd_request *or,
559 enum osd_options_flush_scope_values op)
561 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
562 _osd_req_encode_flush(or, op);
564 EXPORT_SYMBOL(osd_req_flush_obsd);
566 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
567 const u8 *cdb, ...); */
568 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
573 static void _osd_req_encode_partition(struct osd_request *or,
574 __be16 act, osd_id partition)
576 struct osd_obj_id par = {
577 .partition = partition,
581 _osd_req_encode_common(or, act, &par, 0, 0);
584 void osd_req_create_partition(struct osd_request *or, osd_id partition)
586 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
588 EXPORT_SYMBOL(osd_req_create_partition);
590 void osd_req_remove_partition(struct osd_request *or, osd_id partition)
592 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
594 EXPORT_SYMBOL(osd_req_remove_partition);
596 /*TODO: void osd_req_set_partition_key(struct osd_request *,
597 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
598 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
600 static int _osd_req_list_objects(struct osd_request *or,
601 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
602 struct osd_obj_id_list *list, unsigned nelem)
604 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
605 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
608 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
610 if (list->list_identifier)
611 _osd_req_encode_olist(or, list);
614 bio = bio_map_kern(q, list, len, or->alloc_flags);
616 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
620 bio->bi_rw &= ~(1 << BIO_RW);
622 or->in.total_bytes = bio->bi_size;
626 int osd_req_list_partition_collections(struct osd_request *or,
627 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
630 struct osd_obj_id par = {
631 .partition = partition,
635 return osd_req_list_collection_objects(or, &par, initial_id, list,
638 EXPORT_SYMBOL(osd_req_list_partition_collections);
640 int osd_req_list_partition_objects(struct osd_request *or,
641 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
644 struct osd_obj_id par = {
645 .partition = partition,
649 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
652 EXPORT_SYMBOL(osd_req_list_partition_objects);
654 void osd_req_flush_partition(struct osd_request *or,
655 osd_id partition, enum osd_options_flush_scope_values op)
657 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
658 _osd_req_encode_flush(or, op);
660 EXPORT_SYMBOL(osd_req_flush_partition);
663 * Collection commands
665 /*TODO: void osd_req_create_collection(struct osd_request *,
666 const struct osd_obj_id *); */
667 /*TODO: void osd_req_remove_collection(struct osd_request *,
668 const struct osd_obj_id *); */
670 int osd_req_list_collection_objects(struct osd_request *or,
671 const struct osd_obj_id *obj, osd_id initial_id,
672 struct osd_obj_id_list *list, unsigned nelem)
674 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
675 initial_id, list, nelem);
677 EXPORT_SYMBOL(osd_req_list_collection_objects);
679 /*TODO: void query(struct osd_request *, ...); V2 */
681 void osd_req_flush_collection(struct osd_request *or,
682 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
684 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
685 _osd_req_encode_flush(or, op);
687 EXPORT_SYMBOL(osd_req_flush_collection);
689 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
690 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
695 void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
697 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
699 EXPORT_SYMBOL(osd_req_create_object);
701 void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
703 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
705 EXPORT_SYMBOL(osd_req_remove_object);
708 /*TODO: void osd_req_create_multi(struct osd_request *or,
709 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
712 void osd_req_write(struct osd_request *or,
713 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
715 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
716 WARN_ON(or->out.bio || or->out.total_bytes);
717 bio->bi_rw |= (1 << BIO_RW);
719 or->out.total_bytes = bio->bi_size;
721 EXPORT_SYMBOL(osd_req_write);
723 /*TODO: void osd_req_append(struct osd_request *,
724 const struct osd_obj_id *, struct bio *data_out); */
725 /*TODO: void osd_req_create_write(struct osd_request *,
726 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
727 /*TODO: void osd_req_clear(struct osd_request *,
728 const struct osd_obj_id *, u64 offset, u64 len); */
729 /*TODO: void osd_req_punch(struct osd_request *,
730 const struct osd_obj_id *, u64 offset, u64 len); V2 */
732 void osd_req_flush_object(struct osd_request *or,
733 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
734 /*V2*/ u64 offset, /*V2*/ u64 len)
736 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
737 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
742 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
743 _osd_req_encode_flush(or, op);
745 EXPORT_SYMBOL(osd_req_flush_object);
747 void osd_req_read(struct osd_request *or,
748 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
750 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
751 WARN_ON(or->in.bio || or->in.total_bytes);
752 bio->bi_rw &= ~(1 << BIO_RW);
754 or->in.total_bytes = bio->bi_size;
756 EXPORT_SYMBOL(osd_req_read);
758 void osd_req_get_attributes(struct osd_request *or,
759 const struct osd_obj_id *obj)
761 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
763 EXPORT_SYMBOL(osd_req_get_attributes);
765 void osd_req_set_attributes(struct osd_request *or,
766 const struct osd_obj_id *obj)
768 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
770 EXPORT_SYMBOL(osd_req_set_attributes);
773 * Attributes List-mode
776 int osd_req_add_set_attr_list(struct osd_request *or,
777 const struct osd_attr *oa, unsigned nelem)
779 unsigned total_bytes = or->set_attr.total_bytes;
783 if (or->attributes_mode &&
784 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
788 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
790 if (!total_bytes) { /* first-time: allocate and put list header */
791 total_bytes = _osd_req_sizeof_alist_header(or);
792 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
795 _osd_req_set_alist_type(or, or->set_attr.buff,
796 OSD_ATTR_LIST_SET_RETRIEVE);
798 attr_last = or->set_attr.buff + total_bytes;
800 for (; nelem; --nelem) {
801 struct osd_attributes_list_element *attr;
802 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
804 total_bytes += elem_size;
805 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
806 or->set_attr.total_bytes = total_bytes - elem_size;
807 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
811 or->set_attr.buff + or->set_attr.total_bytes;
815 attr->attr_page = cpu_to_be32(oa->attr_page);
816 attr->attr_id = cpu_to_be32(oa->attr_id);
817 attr->attr_bytes = cpu_to_be16(oa->len);
818 memcpy(attr->attr_val, oa->val_ptr, oa->len);
820 attr_last += elem_size;
824 or->set_attr.total_bytes = total_bytes;
827 EXPORT_SYMBOL(osd_req_add_set_attr_list);
829 static int _append_map_kern(struct request *req,
830 void *buff, unsigned len, gfp_t flags)
835 bio = bio_map_kern(req->q, buff, len, flags);
837 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
841 ret = blk_rq_append_bio(req->q, req, bio);
843 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
849 static int _req_append_segment(struct osd_request *or,
850 unsigned padding, struct _osd_req_data_segment *seg,
851 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
857 /* check if we can just add it to last buffer */
859 (padding <= last_seg->alloc_size - last_seg->total_bytes))
860 pad_buff = last_seg->buff + last_seg->total_bytes;
862 pad_buff = io->pad_buff;
864 ret = _append_map_kern(io->req, pad_buff, padding,
868 io->total_bytes += padding;
871 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
876 io->total_bytes += seg->total_bytes;
877 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
882 static int _osd_req_finalize_set_attr_list(struct osd_request *or)
884 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
888 if (!or->set_attr.total_bytes) {
889 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
893 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
894 cdbh->attrs_list.set_attr_offset =
895 osd_req_encode_offset(or, or->out.total_bytes, &padding);
897 ret = _req_append_segment(or, padding, &or->set_attr,
898 or->out.last_seg, &or->out);
902 or->out.last_seg = &or->set_attr;
906 int osd_req_add_get_attr_list(struct osd_request *or,
907 const struct osd_attr *oa, unsigned nelem)
909 unsigned total_bytes = or->enc_get_attr.total_bytes;
913 if (or->attributes_mode &&
914 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
918 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
920 /* first time calc data-in list header size */
921 if (!or->get_attr.total_bytes)
922 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
924 /* calc data-out info */
925 if (!total_bytes) { /* first-time: allocate and put list header */
928 total_bytes = _osd_req_sizeof_alist_header(or);
929 max_bytes = total_bytes +
930 nelem * sizeof(struct osd_attributes_list_attrid);
931 ret = _alloc_get_attr_desc(or, max_bytes);
935 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
938 attr_last = or->enc_get_attr.buff + total_bytes;
940 for (; nelem; --nelem) {
941 struct osd_attributes_list_attrid *attrid;
942 const unsigned cur_size = sizeof(*attrid);
944 total_bytes += cur_size;
945 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
946 or->enc_get_attr.total_bytes = total_bytes - cur_size;
947 ret = _alloc_get_attr_desc(or,
948 total_bytes + nelem * sizeof(*attrid));
951 attr_last = or->enc_get_attr.buff +
952 or->enc_get_attr.total_bytes;
956 attrid->attr_page = cpu_to_be32(oa->attr_page);
957 attrid->attr_id = cpu_to_be32(oa->attr_id);
959 attr_last += cur_size;
961 /* calc data-in size */
962 or->get_attr.total_bytes +=
963 _osd_req_alist_elem_size(or, oa->len);
967 or->enc_get_attr.total_bytes = total_bytes;
970 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
971 or->get_attr.total_bytes,
972 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
973 or->enc_get_attr.total_bytes,
974 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
975 / sizeof(struct osd_attributes_list_attrid));
979 EXPORT_SYMBOL(osd_req_add_get_attr_list);
981 static int _osd_req_finalize_get_attr_list(struct osd_request *or)
983 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
984 unsigned out_padding;
988 if (!or->enc_get_attr.total_bytes) {
989 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
990 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
994 ret = _alloc_get_attr_list(or);
998 /* The out-going buffer info update */
999 OSD_DEBUG("out-going\n");
1000 cdbh->attrs_list.get_attr_desc_bytes =
1001 cpu_to_be32(or->enc_get_attr.total_bytes);
1003 cdbh->attrs_list.get_attr_desc_offset =
1004 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1006 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1007 or->out.last_seg, &or->out);
1010 or->out.last_seg = &or->enc_get_attr;
1012 /* The incoming buffer info update */
1013 OSD_DEBUG("in-coming\n");
1014 cdbh->attrs_list.get_attr_alloc_length =
1015 cpu_to_be32(or->get_attr.total_bytes);
1017 cdbh->attrs_list.get_attr_offset =
1018 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1020 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1024 or->in.last_seg = &or->get_attr;
1029 int osd_req_decode_get_attr_list(struct osd_request *or,
1030 struct osd_attr *oa, int *nelem, void **iterator)
1032 unsigned cur_bytes, returned_bytes;
1034 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1037 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1038 OSD_ATTR_LIST_SET_RETRIEVE)) {
1048 BUG_ON((*iterator < or->get_attr.buff) ||
1049 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1051 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1052 returned_bytes = or->get_attr.total_bytes;
1053 } else { /* first time decode the list header */
1054 cur_bytes = sizeof_attr_list;
1055 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1058 cur_p = or->get_attr.buff + sizeof_attr_list;
1060 if (returned_bytes > or->get_attr.alloc_size) {
1061 OSD_DEBUG("target report: space was not big enough! "
1062 "Allocate=%u Needed=%u\n",
1063 or->get_attr.alloc_size,
1064 returned_bytes + sizeof_attr_list);
1067 or->get_attr.alloc_size - sizeof_attr_list;
1069 or->get_attr.total_bytes = returned_bytes;
1072 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
1073 struct osd_attributes_list_element *attr = cur_p;
1076 oa->len = be16_to_cpu(attr->attr_bytes);
1077 inc = _osd_req_alist_elem_size(or, oa->len);
1078 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
1079 oa->len, inc, cur_bytes);
1081 if (cur_bytes > returned_bytes) {
1082 OSD_ERR("BAD FOOD from target. list not valid!"
1084 cur_bytes, returned_bytes, n);
1089 oa->attr_page = be32_to_cpu(attr->attr_page);
1090 oa->attr_id = be32_to_cpu(attr->attr_id);
1091 oa->val_ptr = attr->attr_val;
1097 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1099 return returned_bytes - cur_bytes;
1101 EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1104 * Attributes Page-mode
1107 int osd_req_add_get_attr_page(struct osd_request *or,
1108 u32 page_id, void *attar_page, unsigned max_page_len,
1109 const struct osd_attr *set_one_attr)
1111 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1113 if (or->attributes_mode &&
1114 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1118 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1120 or->get_attr.buff = attar_page;
1121 or->get_attr.total_bytes = max_page_len;
1123 or->set_attr.buff = set_one_attr->val_ptr;
1124 or->set_attr.total_bytes = set_one_attr->len;
1126 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1127 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1128 /* ocdb->attrs_page.get_attr_offset; */
1130 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1131 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1132 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1133 /* ocdb->attrs_page.set_attr_offset; */
1136 EXPORT_SYMBOL(osd_req_add_get_attr_page);
1138 static int _osd_req_finalize_attr_page(struct osd_request *or)
1140 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1141 unsigned in_padding, out_padding;
1145 cdbh->attrs_page.get_attr_offset =
1146 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1148 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1154 cdbh->attrs_page.set_attr_offset =
1155 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1157 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1162 static int _osd_req_finalize_data_integrity(struct osd_request *or,
1163 bool has_in, bool has_out, const u8 *cap_key)
1165 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1168 if (!osd_is_sec_alldata(sec_parms))
1172 struct _osd_req_data_segment seg = {
1173 .buff = &or->out_data_integ,
1174 .total_bytes = sizeof(or->out_data_integ),
1178 or->out_data_integ.data_bytes = cpu_to_be64(
1179 or->out.bio ? or->out.bio->bi_size : 0);
1180 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1181 or->set_attr.total_bytes);
1182 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1183 or->enc_get_attr.total_bytes);
1185 sec_parms->data_out_integrity_check_offset =
1186 osd_req_encode_offset(or, or->out.total_bytes, &pad);
1188 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1192 or->out.last_seg = NULL;
1194 /* they are now all chained to request sign them all together */
1195 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1200 struct _osd_req_data_segment seg = {
1201 .buff = &or->in_data_integ,
1202 .total_bytes = sizeof(or->in_data_integ),
1206 sec_parms->data_in_integrity_check_offset =
1207 osd_req_encode_offset(or, or->in.total_bytes, &pad);
1209 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1214 or->in.last_seg = NULL;
1221 * osd_finalize_request and helpers
1224 static int _init_blk_request(struct osd_request *or,
1225 bool has_in, bool has_out)
1227 gfp_t flags = or->alloc_flags;
1228 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1229 struct request_queue *q = scsi_device->request_queue;
1230 struct request *req;
1233 req = blk_get_request(q, has_out, flags);
1238 req->cmd_type = REQ_TYPE_BLOCK_PC;
1239 req->timeout = or->timeout;
1240 req->retries = or->retries;
1241 req->sense = or->sense;
1247 /* allocate bidi request */
1248 req = blk_get_request(q, READ, flags);
1250 OSD_DEBUG("blk_get_request for bidi failed\n");
1253 req->cmd_type = REQ_TYPE_BLOCK_PC;
1254 or->in.req = or->request->next_rq = req;
1261 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1262 or, has_in, has_out, ret, or->request);
1266 int osd_finalize_request(struct osd_request *or,
1267 u8 options, const void *cap, const u8 *cap_key)
1269 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1270 bool has_in, has_out;
1273 if (options & OSD_REQ_FUA)
1274 cdbh->options |= OSD_CDB_FUA;
1276 if (options & OSD_REQ_DPO)
1277 cdbh->options |= OSD_CDB_DPO;
1279 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1280 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1282 osd_set_caps(&or->cdb, cap);
1284 has_in = or->in.bio || or->get_attr.total_bytes;
1285 has_out = or->out.bio || or->set_attr.total_bytes ||
1286 or->enc_get_attr.total_bytes;
1288 ret = _init_blk_request(or, has_in, has_out);
1290 OSD_DEBUG("_init_blk_request failed\n");
1295 ret = blk_rq_append_bio(or->request->q, or->out.req,
1298 OSD_DEBUG("blk_rq_append_bio out failed\n");
1301 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1302 _LLU(or->out.total_bytes), or->out.req->data_len);
1305 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1307 OSD_DEBUG("blk_rq_append_bio in failed\n");
1310 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1311 _LLU(or->in.total_bytes), or->in.req->data_len);
1314 or->out.pad_buff = sg_out_pad_buffer;
1315 or->in.pad_buff = sg_in_pad_buffer;
1317 if (!or->attributes_mode)
1318 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1319 cdbh->command_specific_options |= or->attributes_mode;
1320 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1321 ret = _osd_req_finalize_attr_page(or);
1323 /* TODO: I think that for the GET_ATTR command these 2 should
1324 * be reversed to keep them in execution order (for embeded
1325 * targets with low memory footprint)
1327 ret = _osd_req_finalize_set_attr_list(or);
1329 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1333 ret = _osd_req_finalize_get_attr_list(or);
1335 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1340 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1344 osd_sec_sign_cdb(&or->cdb, cap_key);
1346 or->request->cmd = or->cdb.buff;
1347 or->request->cmd_len = _osd_req_cdb_len(or);
1351 EXPORT_SYMBOL(osd_finalize_request);
1353 #define OSD_SENSE_PRINT1(fmt, a...) \
1355 if (__cur_sense_need_output) \
1356 OSD_ERR(fmt, ##a); \
1359 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1361 int osd_req_decode_sense_full(struct osd_request *or,
1362 struct osd_sense_info *osi, bool silent,
1363 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1364 struct osd_attr *bad_attr_list, int max_attr)
1366 int sense_len, original_sense_len;
1367 struct osd_sense_info local_osi;
1368 struct scsi_sense_descriptor_based *ssdb;
1369 void *cur_descriptor;
1370 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1371 const bool __cur_sense_need_output = false;
1373 bool __cur_sense_need_output = !silent;
1376 if (!or->request->errors)
1379 ssdb = or->request->sense;
1380 sense_len = or->request->sense_len;
1381 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1382 OSD_ERR("Block-layer returned error(0x%x) but "
1383 "sense_len(%u) || key(%d) is empty\n",
1384 or->request->errors, sense_len, ssdb->sense_key);
1388 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1389 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1390 ssdb->response_code, sense_len);
1394 osi = osi ? : &local_osi;
1395 memset(osi, 0, sizeof(*osi));
1396 osi->key = ssdb->sense_key;
1397 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1398 original_sense_len = ssdb->additional_sense_length + 8;
1400 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1401 if (__cur_sense_need_output)
1402 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1404 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1405 "additional_code=0x%x\n",
1406 osi->key, original_sense_len, sense_len,
1407 osi->additional_code);
1409 if (original_sense_len < sense_len)
1410 sense_len = original_sense_len;
1412 cur_descriptor = ssdb->ssd;
1413 sense_len -= sizeof(*ssdb);
1414 while (sense_len > 0) {
1415 struct scsi_sense_descriptor *ssd = cur_descriptor;
1416 int cur_len = ssd->additional_length + 2;
1418 sense_len -= cur_len;
1421 break; /* sense was truncated */
1423 switch (ssd->descriptor_type) {
1424 case scsi_sense_information:
1425 case scsi_sense_command_specific_information:
1427 struct scsi_sense_command_specific_data_descriptor
1428 *sscd = cur_descriptor;
1431 get_unaligned_be64(&sscd->information) ;
1433 "command_specific_information 0x%llx \n",
1434 _LLU(osi->command_info));
1437 case scsi_sense_key_specific:
1439 struct scsi_sense_key_specific_data_descriptor
1440 *ssks = cur_descriptor;
1442 osi->sense_info = get_unaligned_be16(&ssks->value);
1444 "sense_key_specific_information %u"
1445 "sksv_cd_bpv_bp (0x%x)\n",
1446 osi->sense_info, ssks->sksv_cd_bpv_bp);
1449 case osd_sense_object_identification:
1450 { /*FIXME: Keep first not last, Store in array*/
1451 struct osd_sense_identification_data_descriptor
1452 *osidd = cur_descriptor;
1454 osi->not_initiated_command_functions =
1455 le32_to_cpu(osidd->not_initiated_functions);
1456 osi->completed_command_functions =
1457 le32_to_cpu(osidd->completed_functions);
1458 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1459 osi->obj.id = be64_to_cpu(osidd->object_id);
1461 "object_identification pid=0x%llx oid=0x%llx\n",
1462 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1464 "not_initiated_bits(%x) "
1465 "completed_command_bits(%x)\n",
1466 osi->not_initiated_command_functions,
1467 osi->completed_command_functions);
1470 case osd_sense_response_integrity_check:
1472 struct osd_sense_response_integrity_check_descriptor
1473 *osricd = cur_descriptor;
1474 const unsigned len =
1475 sizeof(osricd->integrity_check_value);
1476 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1478 hex_dump_to_buffer(osricd->integrity_check_value, len,
1479 32, 1, key_dump, sizeof(key_dump), true);
1480 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1482 case osd_sense_attribute_identification:
1484 struct osd_sense_attributes_data_descriptor
1485 *osadd = cur_descriptor;
1486 int len = min(cur_len, sense_len);
1488 struct osd_sense_attr *pattr = osadd->sense_attrs;
1491 u32 attr_page = be32_to_cpu(pattr->attr_page);
1492 u32 attr_id = be32_to_cpu(pattr->attr_id);
1495 osi->attr.attr_page = attr_page;
1496 osi->attr.attr_id = attr_id;
1499 if (bad_attr_list && max_attr) {
1500 bad_attr_list->attr_page = attr_page;
1501 bad_attr_list->attr_id = attr_id;
1506 "osd_sense_attribute_identification"
1507 "attr_page=0x%x attr_id=0x%x\n",
1508 attr_page, attr_id);
1511 /*These are not legal for OSD*/
1512 case scsi_sense_field_replaceable_unit:
1513 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1515 case scsi_sense_stream_commands:
1516 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1518 case scsi_sense_block_commands:
1519 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1521 case scsi_sense_ata_return:
1522 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1525 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1527 "scsi_sense Reserved descriptor (0x%x)",
1528 ssd->descriptor_type);
1531 "scsi_sense Vendor descriptor (0x%x)",
1532 ssd->descriptor_type);
1535 cur_descriptor += cur_len;
1538 return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1540 EXPORT_SYMBOL(osd_req_decode_sense_full);
1543 * Implementation of osd_sec.h API
1544 * TODO: Move to a separate osd_sec.c file at a later stage.
1547 enum { OSD_SEC_CAP_V1_ALL_CAPS =
1548 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1549 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1550 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1551 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1554 enum { OSD_SEC_CAP_V2_ALL_CAPS =
1555 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1558 void osd_sec_init_nosec_doall_caps(void *caps,
1559 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1561 struct osd_capability *cap = caps;
1565 if (likely(obj->id)) {
1566 if (unlikely(is_collection)) {
1567 type = OSD_SEC_OBJ_COLLECTION;
1568 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1569 OSD_SEC_OBJ_DESC_COL;
1571 type = OSD_SEC_OBJ_USER;
1572 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1574 WARN_ON(!obj->partition);
1576 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1578 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1581 memset(cap, 0, sizeof(*cap));
1583 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1584 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1585 cap->h.security_method = OSD_SEC_NOSEC;
1586 /* cap->expiration_time;
1588 cap->discriminator[42-30];
1589 cap->object_created_time; */
1590 cap->h.object_type = type;
1591 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1592 cap->h.object_descriptor_type = descriptor_type;
1593 cap->od.obj_desc.policy_access_tag = 0;
1594 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1595 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1597 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1599 /* FIXME: Extract version from caps pointer.
1600 * Also Pete's target only supports caps from OSDv1 for now
1602 void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1604 bool is_ver1 = true;
1605 /* NOTE: They start at same address */
1606 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
1609 bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1614 void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1618 void osd_sec_sign_data(void *data_integ __unused,
1619 struct bio *bio __unused, const u8 *cap_key __unused)
1624 * Declared in osd_protocol.h
1625 * 4.12.5 Data-In and Data-Out buffer offsets
1626 * byte offset = mantissa * (2^(exponent+8))
1627 * Returns the smallest allowed encoded offset that contains given @offset
1628 * The actual encoded offset returned is @offset + *@padding.
1630 osd_cdb_offset __osd_encode_offset(
1631 u64 offset, unsigned *padding, int min_shift, int max_shift)
1633 u64 try_offset = -1, mod, align;
1634 osd_cdb_offset be32_offset;
1641 for (shift = min_shift; shift < max_shift; ++shift) {
1642 try_offset = offset >> shift;
1643 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1647 BUG_ON(shift == max_shift);
1650 mod = offset & (align - 1);
1652 *padding = align - mod;
1656 try_offset |= ((shift - 8) & 0xf) << 28;
1657 be32_offset = cpu_to_be32((u32)try_offset);
1659 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1660 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1661 be32_offset, *padding);