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("VENDOR_IDENTIFICATION [%s]\n",
124 pFirst
= get_attrs
[a
++].val_ptr
;
125 OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
128 pFirst
= get_attrs
[a
++].val_ptr
;
129 OSD_INFO("PRODUCT_MODEL [%s]\n",
132 pFirst
= get_attrs
[a
++].val_ptr
;
133 OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
134 pFirst
? get_unaligned_be32(pFirst
) : ~0U);
136 pFirst
= get_attrs
[a
++].val_ptr
;
137 OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
140 pFirst
= get_attrs
[a
].val_ptr
;
141 OSD_INFO("OSD_NAME [%s]\n", (char *)pFirst
);
144 pFirst
= get_attrs
[a
++].val_ptr
;
145 OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
146 pFirst
? _LLU(get_unaligned_be64(pFirst
)) : ~0ULL);
148 pFirst
= get_attrs
[a
++].val_ptr
;
149 OSD_INFO("USED_CAPACITY [0x%llx]\n",
150 pFirst
? _LLU(get_unaligned_be64(pFirst
)) : ~0ULL);
152 pFirst
= get_attrs
[a
++].val_ptr
;
153 OSD_INFO("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("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_SYSTEM_ID(%d)\n"
173 " [%s]\n", len
, sid_dump
);
181 int osd_auto_detect_ver(struct osd_dev
*od
, void *caps
)
185 /* Auto-detect the osd version */
186 ret
= _osd_print_system_info(od
, caps
);
188 osd_dev_set_ver(od
, OSD_VER1
);
189 OSD_DEBUG("converting to OSD1\n");
190 ret
= _osd_print_system_info(od
, caps
);
195 EXPORT_SYMBOL(osd_auto_detect_ver
);
197 static unsigned _osd_req_cdb_len(struct osd_request
*or)
199 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN
: OSD_TOTAL_CDB_LEN
;
202 static unsigned _osd_req_alist_elem_size(struct osd_request
*or, unsigned len
)
204 return osd_req_is_ver1(or) ?
205 osdv1_attr_list_elem_size(len
) :
206 osdv2_attr_list_elem_size(len
);
209 static void _osd_req_alist_elem_encode(struct osd_request
*or,
210 void *attr_last
, const struct osd_attr
*oa
)
212 if (osd_req_is_ver1(or)) {
213 struct osdv1_attributes_list_element
*attr
= attr_last
;
215 attr
->attr_page
= cpu_to_be32(oa
->attr_page
);
216 attr
->attr_id
= cpu_to_be32(oa
->attr_id
);
217 attr
->attr_bytes
= cpu_to_be16(oa
->len
);
218 memcpy(attr
->attr_val
, oa
->val_ptr
, oa
->len
);
220 struct osdv2_attributes_list_element
*attr
= attr_last
;
222 attr
->attr_page
= cpu_to_be32(oa
->attr_page
);
223 attr
->attr_id
= cpu_to_be32(oa
->attr_id
);
224 attr
->attr_bytes
= cpu_to_be16(oa
->len
);
225 memcpy(attr
->attr_val
, oa
->val_ptr
, oa
->len
);
229 static int _osd_req_alist_elem_decode(struct osd_request
*or,
230 void *cur_p
, struct osd_attr
*oa
, unsigned max_bytes
)
233 if (osd_req_is_ver1(or)) {
234 struct osdv1_attributes_list_element
*attr
= cur_p
;
236 if (max_bytes
< sizeof(*attr
))
239 oa
->len
= be16_to_cpu(attr
->attr_bytes
);
240 inc
= _osd_req_alist_elem_size(or, oa
->len
);
244 oa
->attr_page
= be32_to_cpu(attr
->attr_page
);
245 oa
->attr_id
= be32_to_cpu(attr
->attr_id
);
247 /* OSD1: On empty attributes we return a pointer to 2 bytes
248 * of zeros. This keeps similar behaviour with OSD2.
251 oa
->val_ptr
= likely(oa
->len
) ? attr
->attr_val
:
252 (u8
*)&attr
->attr_bytes
;
254 struct osdv2_attributes_list_element
*attr
= cur_p
;
256 if (max_bytes
< sizeof(*attr
))
259 oa
->len
= be16_to_cpu(attr
->attr_bytes
);
260 inc
= _osd_req_alist_elem_size(or, oa
->len
);
264 oa
->attr_page
= be32_to_cpu(attr
->attr_page
);
265 oa
->attr_id
= be32_to_cpu(attr
->attr_id
);
267 /* OSD2: For convenience, on empty attributes, we return 8 bytes
268 * of zeros here. This keeps the same behaviour with OSD2r04,
269 * and is nice with null terminating ASCII fields.
270 * oa->val_ptr == NULL marks the end-of-list, or error.
272 oa
->val_ptr
= likely(oa
->len
) ? attr
->attr_val
: attr
->reserved
;
277 static unsigned _osd_req_alist_size(struct osd_request
*or, void *list_head
)
279 return osd_req_is_ver1(or) ?
280 osdv1_list_size(list_head
) :
281 osdv2_list_size(list_head
);
284 static unsigned _osd_req_sizeof_alist_header(struct osd_request
*or)
286 return osd_req_is_ver1(or) ?
287 sizeof(struct osdv1_attributes_list_header
) :
288 sizeof(struct osdv2_attributes_list_header
);
291 static void _osd_req_set_alist_type(struct osd_request
*or,
292 void *list
, int list_type
)
294 if (osd_req_is_ver1(or)) {
295 struct osdv1_attributes_list_header
*attr_list
= list
;
297 memset(attr_list
, 0, sizeof(*attr_list
));
298 attr_list
->type
= list_type
;
300 struct osdv2_attributes_list_header
*attr_list
= list
;
302 memset(attr_list
, 0, sizeof(*attr_list
));
303 attr_list
->type
= list_type
;
307 static bool _osd_req_is_alist_type(struct osd_request
*or,
308 void *list
, int list_type
)
313 if (osd_req_is_ver1(or)) {
314 struct osdv1_attributes_list_header
*attr_list
= list
;
316 return attr_list
->type
== list_type
;
318 struct osdv2_attributes_list_header
*attr_list
= list
;
320 return attr_list
->type
== list_type
;
324 /* This is for List-objects not Attributes-Lists */
325 static void _osd_req_encode_olist(struct osd_request
*or,
326 struct osd_obj_id_list
*list
)
328 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
330 if (osd_req_is_ver1(or)) {
331 cdbh
->v1
.list_identifier
= list
->list_identifier
;
332 cdbh
->v1
.start_address
= list
->continuation_id
;
334 cdbh
->v2
.list_identifier
= list
->list_identifier
;
335 cdbh
->v2
.start_address
= list
->continuation_id
;
339 static osd_cdb_offset
osd_req_encode_offset(struct osd_request
*or,
340 u64 offset
, unsigned *padding
)
342 return __osd_encode_offset(offset
, padding
,
343 osd_req_is_ver1(or) ?
344 OSDv1_OFFSET_MIN_SHIFT
: OSD_OFFSET_MIN_SHIFT
,
345 OSD_OFFSET_MAX_SHIFT
);
348 static struct osd_security_parameters
*
349 _osd_req_sec_params(struct osd_request
*or)
351 struct osd_cdb
*ocdb
= &or->cdb
;
353 if (osd_req_is_ver1(or))
354 return (struct osd_security_parameters
*)&ocdb
->v1
.sec_params
;
356 return (struct osd_security_parameters
*)&ocdb
->v2
.sec_params
;
359 void osd_dev_init(struct osd_dev
*osdd
, struct scsi_device
*scsi_device
)
361 memset(osdd
, 0, sizeof(*osdd
));
362 osdd
->scsi_device
= scsi_device
;
363 osdd
->def_timeout
= BLK_DEFAULT_SG_TIMEOUT
;
364 #ifdef OSD_VER1_SUPPORT
365 osdd
->version
= OSD_VER2
;
367 /* TODO: Allocate pools for osd_request attributes ... */
369 EXPORT_SYMBOL(osd_dev_init
);
371 void osd_dev_fini(struct osd_dev
*osdd
)
373 /* TODO: De-allocate pools */
375 osdd
->scsi_device
= NULL
;
377 EXPORT_SYMBOL(osd_dev_fini
);
379 static struct osd_request
*_osd_request_alloc(gfp_t gfp
)
381 struct osd_request
*or;
383 /* TODO: Use mempool with one saved request */
384 or = kzalloc(sizeof(*or), gfp
);
388 static void _osd_request_free(struct osd_request
*or)
393 struct osd_request
*osd_start_request(struct osd_dev
*dev
, gfp_t gfp
)
395 struct osd_request
*or;
397 or = _osd_request_alloc(gfp
);
402 or->alloc_flags
= gfp
;
403 or->timeout
= dev
->def_timeout
;
404 or->retries
= OSD_REQ_RETRIES
;
408 EXPORT_SYMBOL(osd_start_request
);
410 static void _osd_free_seg(struct osd_request
*or __unused
,
411 struct _osd_req_data_segment
*seg
)
413 if (!seg
->buff
|| !seg
->alloc_size
)
421 static void _put_request(struct request
*rq
, bool is_async
)
425 __blk_put_request(rq
->q
, rq
);
428 * If osd_finalize_request() was called but the request was not
429 * executed through the block layer, then we must release BIOs.
430 * TODO: Keep error code in or->async_error. Need to audit all
433 if (unlikely(rq
->bio
))
434 blk_end_request(rq
, -ENOMEM
, blk_rq_bytes(rq
));
440 void osd_end_request(struct osd_request
*or)
442 struct request
*rq
= or->request
;
443 /* IMPORTANT: make sure this agrees with osd_execute_request_async */
444 bool is_async
= (or->request
->end_io_data
== or);
446 _osd_free_seg(or, &or->set_attr
);
447 _osd_free_seg(or, &or->enc_get_attr
);
448 _osd_free_seg(or, &or->get_attr
);
452 _put_request(rq
->next_rq
, is_async
);
456 _put_request(rq
, is_async
);
458 _osd_request_free(or);
460 EXPORT_SYMBOL(osd_end_request
);
462 int osd_execute_request(struct osd_request
*or)
464 return blk_execute_rq(or->request
->q
, NULL
, or->request
, 0);
466 EXPORT_SYMBOL(osd_execute_request
);
468 static void osd_request_async_done(struct request
*req
, int error
)
470 struct osd_request
*or = req
->end_io_data
;
472 or->async_error
= error
;
475 OSD_DEBUG("osd_request_async_done error recieved %d\n", error
);
478 or->async_done(or, or->async_private
);
483 int osd_execute_request_async(struct osd_request
*or,
484 osd_req_done_fn
*done
, void *private)
486 or->request
->end_io_data
= or;
487 or->async_private
= private;
488 or->async_done
= done
;
490 blk_execute_rq_nowait(or->request
->q
, NULL
, or->request
, 0,
491 osd_request_async_done
);
494 EXPORT_SYMBOL(osd_execute_request_async
);
496 u8 sg_out_pad_buffer
[1 << OSDv1_OFFSET_MIN_SHIFT
];
497 u8 sg_in_pad_buffer
[1 << OSDv1_OFFSET_MIN_SHIFT
];
499 static int _osd_realloc_seg(struct osd_request
*or,
500 struct _osd_req_data_segment
*seg
, unsigned max_bytes
)
504 if (seg
->alloc_size
>= max_bytes
)
507 buff
= krealloc(seg
->buff
, max_bytes
, or->alloc_flags
);
509 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes
,
514 memset(buff
+ seg
->alloc_size
, 0, max_bytes
- seg
->alloc_size
);
516 seg
->alloc_size
= max_bytes
;
520 static int _alloc_set_attr_list(struct osd_request
*or,
521 const struct osd_attr
*oa
, unsigned nelem
, unsigned add_bytes
)
523 unsigned total_bytes
= add_bytes
;
525 for (; nelem
; --nelem
, ++oa
)
526 total_bytes
+= _osd_req_alist_elem_size(or, oa
->len
);
528 OSD_DEBUG("total_bytes=%d\n", total_bytes
);
529 return _osd_realloc_seg(or, &or->set_attr
, total_bytes
);
532 static int _alloc_get_attr_desc(struct osd_request
*or, unsigned max_bytes
)
534 OSD_DEBUG("total_bytes=%d\n", max_bytes
);
535 return _osd_realloc_seg(or, &or->enc_get_attr
, max_bytes
);
538 static int _alloc_get_attr_list(struct osd_request
*or)
540 OSD_DEBUG("total_bytes=%d\n", or->get_attr
.total_bytes
);
541 return _osd_realloc_seg(or, &or->get_attr
, or->get_attr
.total_bytes
);
545 * Common to all OSD commands
548 static void _osdv1_req_encode_common(struct osd_request
*or,
549 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
551 struct osdv1_cdb
*ocdb
= &or->cdb
.v1
;
554 * For speed, the commands
555 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
556 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
557 * are not supported here. Should pass zero and set after the call
559 act
&= cpu_to_be16(~0x0080); /* V1 action code */
561 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act
));
563 ocdb
->h
.varlen_cdb
.opcode
= VARIABLE_LENGTH_CMD
;
564 ocdb
->h
.varlen_cdb
.additional_cdb_length
= OSD_ADDITIONAL_CDB_LENGTH
;
565 ocdb
->h
.varlen_cdb
.service_action
= act
;
567 ocdb
->h
.partition
= cpu_to_be64(obj
->partition
);
568 ocdb
->h
.object
= cpu_to_be64(obj
->id
);
569 ocdb
->h
.v1
.length
= cpu_to_be64(len
);
570 ocdb
->h
.v1
.start_address
= cpu_to_be64(offset
);
573 static void _osdv2_req_encode_common(struct osd_request
*or,
574 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
576 struct osdv2_cdb
*ocdb
= &or->cdb
.v2
;
578 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act
));
580 ocdb
->h
.varlen_cdb
.opcode
= VARIABLE_LENGTH_CMD
;
581 ocdb
->h
.varlen_cdb
.additional_cdb_length
= OSD_ADDITIONAL_CDB_LENGTH
;
582 ocdb
->h
.varlen_cdb
.service_action
= act
;
584 ocdb
->h
.partition
= cpu_to_be64(obj
->partition
);
585 ocdb
->h
.object
= cpu_to_be64(obj
->id
);
586 ocdb
->h
.v2
.length
= cpu_to_be64(len
);
587 ocdb
->h
.v2
.start_address
= cpu_to_be64(offset
);
590 static void _osd_req_encode_common(struct osd_request
*or,
591 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
593 if (osd_req_is_ver1(or))
594 _osdv1_req_encode_common(or, act
, obj
, offset
, len
);
596 _osdv2_req_encode_common(or, act
, obj
, offset
, len
);
602 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
603 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
605 void osd_req_format(struct osd_request
*or, u64 tot_capacity
)
607 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD
, &osd_root_object
, 0,
610 EXPORT_SYMBOL(osd_req_format
);
612 int osd_req_list_dev_partitions(struct osd_request
*or,
613 osd_id initial_id
, struct osd_obj_id_list
*list
, unsigned nelem
)
615 return osd_req_list_partition_objects(or, 0, initial_id
, list
, nelem
);
617 EXPORT_SYMBOL(osd_req_list_dev_partitions
);
619 static void _osd_req_encode_flush(struct osd_request
*or,
620 enum osd_options_flush_scope_values op
)
622 struct osd_cdb_head
*ocdb
= osd_cdb_head(&or->cdb
);
624 ocdb
->command_specific_options
= op
;
627 void osd_req_flush_obsd(struct osd_request
*or,
628 enum osd_options_flush_scope_values op
)
630 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD
, &osd_root_object
, 0, 0);
631 _osd_req_encode_flush(or, op
);
633 EXPORT_SYMBOL(osd_req_flush_obsd
);
635 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
636 const u8 *cdb, ...); */
637 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
642 static void _osd_req_encode_partition(struct osd_request
*or,
643 __be16 act
, osd_id partition
)
645 struct osd_obj_id par
= {
646 .partition
= partition
,
650 _osd_req_encode_common(or, act
, &par
, 0, 0);
653 void osd_req_create_partition(struct osd_request
*or, osd_id partition
)
655 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION
, partition
);
657 EXPORT_SYMBOL(osd_req_create_partition
);
659 void osd_req_remove_partition(struct osd_request
*or, osd_id partition
)
661 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION
, partition
);
663 EXPORT_SYMBOL(osd_req_remove_partition
);
665 /*TODO: void osd_req_set_partition_key(struct osd_request *,
666 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
667 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
669 static int _osd_req_list_objects(struct osd_request
*or,
670 __be16 action
, const struct osd_obj_id
*obj
, osd_id initial_id
,
671 struct osd_obj_id_list
*list
, unsigned nelem
)
673 struct request_queue
*q
= osd_request_queue(or->osd_dev
);
674 u64 len
= nelem
* sizeof(osd_id
) + sizeof(*list
);
677 _osd_req_encode_common(or, action
, obj
, (u64
)initial_id
, len
);
679 if (list
->list_identifier
)
680 _osd_req_encode_olist(or, list
);
683 bio
= bio_map_kern(q
, list
, len
, or->alloc_flags
);
685 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
689 bio
->bi_rw
&= ~(1 << BIO_RW
);
691 or->in
.total_bytes
= bio
->bi_size
;
695 int osd_req_list_partition_collections(struct osd_request
*or,
696 osd_id partition
, osd_id initial_id
, struct osd_obj_id_list
*list
,
699 struct osd_obj_id par
= {
700 .partition
= partition
,
704 return osd_req_list_collection_objects(or, &par
, initial_id
, list
,
707 EXPORT_SYMBOL(osd_req_list_partition_collections
);
709 int osd_req_list_partition_objects(struct osd_request
*or,
710 osd_id partition
, osd_id initial_id
, struct osd_obj_id_list
*list
,
713 struct osd_obj_id par
= {
714 .partition
= partition
,
718 return _osd_req_list_objects(or, OSD_ACT_LIST
, &par
, initial_id
, list
,
721 EXPORT_SYMBOL(osd_req_list_partition_objects
);
723 void osd_req_flush_partition(struct osd_request
*or,
724 osd_id partition
, enum osd_options_flush_scope_values op
)
726 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION
, partition
);
727 _osd_req_encode_flush(or, op
);
729 EXPORT_SYMBOL(osd_req_flush_partition
);
732 * Collection commands
734 /*TODO: void osd_req_create_collection(struct osd_request *,
735 const struct osd_obj_id *); */
736 /*TODO: void osd_req_remove_collection(struct osd_request *,
737 const struct osd_obj_id *); */
739 int osd_req_list_collection_objects(struct osd_request
*or,
740 const struct osd_obj_id
*obj
, osd_id initial_id
,
741 struct osd_obj_id_list
*list
, unsigned nelem
)
743 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION
, obj
,
744 initial_id
, list
, nelem
);
746 EXPORT_SYMBOL(osd_req_list_collection_objects
);
748 /*TODO: void query(struct osd_request *, ...); V2 */
750 void osd_req_flush_collection(struct osd_request
*or,
751 const struct osd_obj_id
*obj
, enum osd_options_flush_scope_values op
)
753 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION
, obj
, 0, 0);
754 _osd_req_encode_flush(or, op
);
756 EXPORT_SYMBOL(osd_req_flush_collection
);
758 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
759 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
764 void osd_req_create_object(struct osd_request
*or, struct osd_obj_id
*obj
)
766 _osd_req_encode_common(or, OSD_ACT_CREATE
, obj
, 0, 0);
768 EXPORT_SYMBOL(osd_req_create_object
);
770 void osd_req_remove_object(struct osd_request
*or, struct osd_obj_id
*obj
)
772 _osd_req_encode_common(or, OSD_ACT_REMOVE
, obj
, 0, 0);
774 EXPORT_SYMBOL(osd_req_remove_object
);
777 /*TODO: void osd_req_create_multi(struct osd_request *or,
778 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
781 void osd_req_write(struct osd_request
*or,
782 const struct osd_obj_id
*obj
, u64 offset
,
783 struct bio
*bio
, u64 len
)
785 _osd_req_encode_common(or, OSD_ACT_WRITE
, obj
, offset
, len
);
786 WARN_ON(or->out
.bio
|| or->out
.total_bytes
);
787 WARN_ON(0 == bio_rw_flagged(bio
, BIO_RW
));
789 or->out
.total_bytes
= len
;
791 EXPORT_SYMBOL(osd_req_write
);
793 int osd_req_write_kern(struct osd_request
*or,
794 const struct osd_obj_id
*obj
, u64 offset
, void* buff
, u64 len
)
796 struct request_queue
*req_q
= osd_request_queue(or->osd_dev
);
797 struct bio
*bio
= bio_map_kern(req_q
, buff
, len
, GFP_KERNEL
);
802 bio
->bi_rw
|= (1 << BIO_RW
); /* FIXME: bio_set_dir() */
803 osd_req_write(or, obj
, offset
, bio
, len
);
806 EXPORT_SYMBOL(osd_req_write_kern
);
808 /*TODO: void osd_req_append(struct osd_request *,
809 const struct osd_obj_id *, struct bio *data_out); */
810 /*TODO: void osd_req_create_write(struct osd_request *,
811 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
812 /*TODO: void osd_req_clear(struct osd_request *,
813 const struct osd_obj_id *, u64 offset, u64 len); */
814 /*TODO: void osd_req_punch(struct osd_request *,
815 const struct osd_obj_id *, u64 offset, u64 len); V2 */
817 void osd_req_flush_object(struct osd_request
*or,
818 const struct osd_obj_id
*obj
, enum osd_options_flush_scope_values op
,
819 /*V2*/ u64 offset
, /*V2*/ u64 len
)
821 if (unlikely(osd_req_is_ver1(or) && (offset
|| len
))) {
822 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
827 _osd_req_encode_common(or, OSD_ACT_FLUSH
, obj
, offset
, len
);
828 _osd_req_encode_flush(or, op
);
830 EXPORT_SYMBOL(osd_req_flush_object
);
832 void osd_req_read(struct osd_request
*or,
833 const struct osd_obj_id
*obj
, u64 offset
,
834 struct bio
*bio
, u64 len
)
836 _osd_req_encode_common(or, OSD_ACT_READ
, obj
, offset
, len
);
837 WARN_ON(or->in
.bio
|| or->in
.total_bytes
);
838 WARN_ON(1 == bio_rw_flagged(bio
, BIO_RW
));
840 or->in
.total_bytes
= len
;
842 EXPORT_SYMBOL(osd_req_read
);
844 int osd_req_read_kern(struct osd_request
*or,
845 const struct osd_obj_id
*obj
, u64 offset
, void* buff
, u64 len
)
847 struct request_queue
*req_q
= osd_request_queue(or->osd_dev
);
848 struct bio
*bio
= bio_map_kern(req_q
, buff
, len
, GFP_KERNEL
);
853 osd_req_read(or, obj
, offset
, bio
, len
);
856 EXPORT_SYMBOL(osd_req_read_kern
);
858 void osd_req_get_attributes(struct osd_request
*or,
859 const struct osd_obj_id
*obj
)
861 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES
, obj
, 0, 0);
863 EXPORT_SYMBOL(osd_req_get_attributes
);
865 void osd_req_set_attributes(struct osd_request
*or,
866 const struct osd_obj_id
*obj
)
868 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES
, obj
, 0, 0);
870 EXPORT_SYMBOL(osd_req_set_attributes
);
873 * Attributes List-mode
876 int osd_req_add_set_attr_list(struct osd_request
*or,
877 const struct osd_attr
*oa
, unsigned nelem
)
879 unsigned total_bytes
= or->set_attr
.total_bytes
;
883 if (or->attributes_mode
&&
884 or->attributes_mode
!= OSD_CDB_GET_SET_ATTR_LISTS
) {
888 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
890 if (!total_bytes
) { /* first-time: allocate and put list header */
891 total_bytes
= _osd_req_sizeof_alist_header(or);
892 ret
= _alloc_set_attr_list(or, oa
, nelem
, total_bytes
);
895 _osd_req_set_alist_type(or, or->set_attr
.buff
,
896 OSD_ATTR_LIST_SET_RETRIEVE
);
898 attr_last
= or->set_attr
.buff
+ total_bytes
;
900 for (; nelem
; --nelem
) {
901 unsigned elem_size
= _osd_req_alist_elem_size(or, oa
->len
);
903 total_bytes
+= elem_size
;
904 if (unlikely(or->set_attr
.alloc_size
< total_bytes
)) {
905 or->set_attr
.total_bytes
= total_bytes
- elem_size
;
906 ret
= _alloc_set_attr_list(or, oa
, nelem
, total_bytes
);
910 or->set_attr
.buff
+ or->set_attr
.total_bytes
;
913 _osd_req_alist_elem_encode(or, attr_last
, oa
);
915 attr_last
+= elem_size
;
919 or->set_attr
.total_bytes
= total_bytes
;
922 EXPORT_SYMBOL(osd_req_add_set_attr_list
);
924 static int _req_append_segment(struct osd_request
*or,
925 unsigned padding
, struct _osd_req_data_segment
*seg
,
926 struct _osd_req_data_segment
*last_seg
, struct _osd_io_info
*io
)
932 /* check if we can just add it to last buffer */
934 (padding
<= last_seg
->alloc_size
- last_seg
->total_bytes
))
935 pad_buff
= last_seg
->buff
+ last_seg
->total_bytes
;
937 pad_buff
= io
->pad_buff
;
939 ret
= blk_rq_map_kern(io
->req
->q
, io
->req
, pad_buff
, padding
,
943 io
->total_bytes
+= padding
;
946 ret
= blk_rq_map_kern(io
->req
->q
, io
->req
, seg
->buff
, seg
->total_bytes
,
951 io
->total_bytes
+= seg
->total_bytes
;
952 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding
, seg
->buff
,
957 static int _osd_req_finalize_set_attr_list(struct osd_request
*or)
959 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
963 if (!or->set_attr
.total_bytes
) {
964 cdbh
->attrs_list
.set_attr_offset
= OSD_OFFSET_UNUSED
;
968 cdbh
->attrs_list
.set_attr_bytes
= cpu_to_be32(or->set_attr
.total_bytes
);
969 cdbh
->attrs_list
.set_attr_offset
=
970 osd_req_encode_offset(or, or->out
.total_bytes
, &padding
);
972 ret
= _req_append_segment(or, padding
, &or->set_attr
,
973 or->out
.last_seg
, &or->out
);
977 or->out
.last_seg
= &or->set_attr
;
981 int osd_req_add_get_attr_list(struct osd_request
*or,
982 const struct osd_attr
*oa
, unsigned nelem
)
984 unsigned total_bytes
= or->enc_get_attr
.total_bytes
;
988 if (or->attributes_mode
&&
989 or->attributes_mode
!= OSD_CDB_GET_SET_ATTR_LISTS
) {
993 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
995 /* first time calc data-in list header size */
996 if (!or->get_attr
.total_bytes
)
997 or->get_attr
.total_bytes
= _osd_req_sizeof_alist_header(or);
999 /* calc data-out info */
1000 if (!total_bytes
) { /* first-time: allocate and put list header */
1003 total_bytes
= _osd_req_sizeof_alist_header(or);
1004 max_bytes
= total_bytes
+
1005 nelem
* sizeof(struct osd_attributes_list_attrid
);
1006 ret
= _alloc_get_attr_desc(or, max_bytes
);
1010 _osd_req_set_alist_type(or, or->enc_get_attr
.buff
,
1013 attr_last
= or->enc_get_attr
.buff
+ total_bytes
;
1015 for (; nelem
; --nelem
) {
1016 struct osd_attributes_list_attrid
*attrid
;
1017 const unsigned cur_size
= sizeof(*attrid
);
1019 total_bytes
+= cur_size
;
1020 if (unlikely(or->enc_get_attr
.alloc_size
< total_bytes
)) {
1021 or->enc_get_attr
.total_bytes
= total_bytes
- cur_size
;
1022 ret
= _alloc_get_attr_desc(or,
1023 total_bytes
+ nelem
* sizeof(*attrid
));
1026 attr_last
= or->enc_get_attr
.buff
+
1027 or->enc_get_attr
.total_bytes
;
1031 attrid
->attr_page
= cpu_to_be32(oa
->attr_page
);
1032 attrid
->attr_id
= cpu_to_be32(oa
->attr_id
);
1034 attr_last
+= cur_size
;
1036 /* calc data-in size */
1037 or->get_attr
.total_bytes
+=
1038 _osd_req_alist_elem_size(or, oa
->len
);
1042 or->enc_get_attr
.total_bytes
= total_bytes
;
1045 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1046 or->get_attr
.total_bytes
,
1047 or->get_attr
.total_bytes
- _osd_req_sizeof_alist_header(or),
1048 or->enc_get_attr
.total_bytes
,
1049 (or->enc_get_attr
.total_bytes
- _osd_req_sizeof_alist_header(or))
1050 / sizeof(struct osd_attributes_list_attrid
));
1054 EXPORT_SYMBOL(osd_req_add_get_attr_list
);
1056 static int _osd_req_finalize_get_attr_list(struct osd_request
*or)
1058 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1059 unsigned out_padding
;
1060 unsigned in_padding
;
1063 if (!or->enc_get_attr
.total_bytes
) {
1064 cdbh
->attrs_list
.get_attr_desc_offset
= OSD_OFFSET_UNUSED
;
1065 cdbh
->attrs_list
.get_attr_offset
= OSD_OFFSET_UNUSED
;
1069 ret
= _alloc_get_attr_list(or);
1073 /* The out-going buffer info update */
1074 OSD_DEBUG("out-going\n");
1075 cdbh
->attrs_list
.get_attr_desc_bytes
=
1076 cpu_to_be32(or->enc_get_attr
.total_bytes
);
1078 cdbh
->attrs_list
.get_attr_desc_offset
=
1079 osd_req_encode_offset(or, or->out
.total_bytes
, &out_padding
);
1081 ret
= _req_append_segment(or, out_padding
, &or->enc_get_attr
,
1082 or->out
.last_seg
, &or->out
);
1085 or->out
.last_seg
= &or->enc_get_attr
;
1087 /* The incoming buffer info update */
1088 OSD_DEBUG("in-coming\n");
1089 cdbh
->attrs_list
.get_attr_alloc_length
=
1090 cpu_to_be32(or->get_attr
.total_bytes
);
1092 cdbh
->attrs_list
.get_attr_offset
=
1093 osd_req_encode_offset(or, or->in
.total_bytes
, &in_padding
);
1095 ret
= _req_append_segment(or, in_padding
, &or->get_attr
, NULL
,
1099 or->in
.last_seg
= &or->get_attr
;
1104 int osd_req_decode_get_attr_list(struct osd_request
*or,
1105 struct osd_attr
*oa
, int *nelem
, void **iterator
)
1107 unsigned cur_bytes
, returned_bytes
;
1109 const unsigned sizeof_attr_list
= _osd_req_sizeof_alist_header(or);
1112 if (!_osd_req_is_alist_type(or, or->get_attr
.buff
,
1113 OSD_ATTR_LIST_SET_RETRIEVE
)) {
1123 BUG_ON((*iterator
< or->get_attr
.buff
) ||
1124 (or->get_attr
.buff
+ or->get_attr
.alloc_size
< *iterator
));
1126 cur_bytes
= (*iterator
- or->get_attr
.buff
) - sizeof_attr_list
;
1127 returned_bytes
= or->get_attr
.total_bytes
;
1128 } else { /* first time decode the list header */
1129 cur_bytes
= sizeof_attr_list
;
1130 returned_bytes
= _osd_req_alist_size(or, or->get_attr
.buff
) +
1133 cur_p
= or->get_attr
.buff
+ sizeof_attr_list
;
1135 if (returned_bytes
> or->get_attr
.alloc_size
) {
1136 OSD_DEBUG("target report: space was not big enough! "
1137 "Allocate=%u Needed=%u\n",
1138 or->get_attr
.alloc_size
,
1139 returned_bytes
+ sizeof_attr_list
);
1142 or->get_attr
.alloc_size
- sizeof_attr_list
;
1144 or->get_attr
.total_bytes
= returned_bytes
;
1147 for (n
= 0; (n
< *nelem
) && (cur_bytes
< returned_bytes
); ++n
) {
1148 int inc
= _osd_req_alist_elem_decode(or, cur_p
, oa
,
1149 returned_bytes
- cur_bytes
);
1152 OSD_ERR("BAD FOOD from target. list not valid!"
1154 cur_bytes
, returned_bytes
, n
);
1164 *iterator
= (returned_bytes
- cur_bytes
) ? cur_p
: NULL
;
1166 return returned_bytes
- cur_bytes
;
1168 EXPORT_SYMBOL(osd_req_decode_get_attr_list
);
1171 * Attributes Page-mode
1174 int osd_req_add_get_attr_page(struct osd_request
*or,
1175 u32 page_id
, void *attar_page
, unsigned max_page_len
,
1176 const struct osd_attr
*set_one_attr
)
1178 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1180 if (or->attributes_mode
&&
1181 or->attributes_mode
!= OSD_CDB_GET_ATTR_PAGE_SET_ONE
) {
1185 or->attributes_mode
= OSD_CDB_GET_ATTR_PAGE_SET_ONE
;
1187 or->get_attr
.buff
= attar_page
;
1188 or->get_attr
.total_bytes
= max_page_len
;
1190 or->set_attr
.buff
= set_one_attr
->val_ptr
;
1191 or->set_attr
.total_bytes
= set_one_attr
->len
;
1193 cdbh
->attrs_page
.get_attr_page
= cpu_to_be32(page_id
);
1194 cdbh
->attrs_page
.get_attr_alloc_length
= cpu_to_be32(max_page_len
);
1195 /* ocdb->attrs_page.get_attr_offset; */
1197 cdbh
->attrs_page
.set_attr_page
= cpu_to_be32(set_one_attr
->attr_page
);
1198 cdbh
->attrs_page
.set_attr_id
= cpu_to_be32(set_one_attr
->attr_id
);
1199 cdbh
->attrs_page
.set_attr_length
= cpu_to_be32(set_one_attr
->len
);
1200 /* ocdb->attrs_page.set_attr_offset; */
1203 EXPORT_SYMBOL(osd_req_add_get_attr_page
);
1205 static int _osd_req_finalize_attr_page(struct osd_request
*or)
1207 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1208 unsigned in_padding
, out_padding
;
1212 cdbh
->attrs_page
.get_attr_offset
=
1213 osd_req_encode_offset(or, or->in
.total_bytes
, &in_padding
);
1215 ret
= _req_append_segment(or, in_padding
, &or->get_attr
, NULL
,
1221 cdbh
->attrs_page
.set_attr_offset
=
1222 osd_req_encode_offset(or, or->out
.total_bytes
, &out_padding
);
1224 ret
= _req_append_segment(or, out_padding
, &or->enc_get_attr
, NULL
,
1229 static inline void osd_sec_parms_set_out_offset(bool is_v1
,
1230 struct osd_security_parameters
*sec_parms
, osd_cdb_offset offset
)
1233 sec_parms
->v1
.data_out_integrity_check_offset
= offset
;
1235 sec_parms
->v2
.data_out_integrity_check_offset
= offset
;
1238 static inline void osd_sec_parms_set_in_offset(bool is_v1
,
1239 struct osd_security_parameters
*sec_parms
, osd_cdb_offset offset
)
1242 sec_parms
->v1
.data_in_integrity_check_offset
= offset
;
1244 sec_parms
->v2
.data_in_integrity_check_offset
= offset
;
1247 static int _osd_req_finalize_data_integrity(struct osd_request
*or,
1248 bool has_in
, bool has_out
, u64 out_data_bytes
, const u8
*cap_key
)
1250 struct osd_security_parameters
*sec_parms
= _osd_req_sec_params(or);
1253 if (!osd_is_sec_alldata(sec_parms
))
1257 struct _osd_req_data_segment seg
= {
1258 .buff
= &or->out_data_integ
,
1259 .total_bytes
= sizeof(or->out_data_integ
),
1263 or->out_data_integ
.data_bytes
= cpu_to_be64(out_data_bytes
);
1264 or->out_data_integ
.set_attributes_bytes
= cpu_to_be64(
1265 or->set_attr
.total_bytes
);
1266 or->out_data_integ
.get_attributes_bytes
= cpu_to_be64(
1267 or->enc_get_attr
.total_bytes
);
1269 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms
,
1270 osd_req_encode_offset(or, or->out
.total_bytes
, &pad
));
1272 ret
= _req_append_segment(or, pad
, &seg
, or->out
.last_seg
,
1276 or->out
.last_seg
= NULL
;
1278 /* they are now all chained to request sign them all together */
1279 osd_sec_sign_data(&or->out_data_integ
, or->out
.req
->bio
,
1284 struct _osd_req_data_segment seg
= {
1285 .buff
= &or->in_data_integ
,
1286 .total_bytes
= sizeof(or->in_data_integ
),
1290 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms
,
1291 osd_req_encode_offset(or, or->in
.total_bytes
, &pad
));
1293 ret
= _req_append_segment(or, pad
, &seg
, or->in
.last_seg
,
1298 or->in
.last_seg
= NULL
;
1305 * osd_finalize_request and helpers
1307 static struct request
*_make_request(struct request_queue
*q
, bool has_write
,
1308 struct _osd_io_info
*oii
, gfp_t flags
)
1311 return blk_make_request(q
, oii
->bio
, flags
);
1313 struct request
*req
;
1315 req
= blk_get_request(q
, has_write
? WRITE
: READ
, flags
);
1317 return ERR_PTR(-ENOMEM
);
1323 static int _init_blk_request(struct osd_request
*or,
1324 bool has_in
, bool has_out
)
1326 gfp_t flags
= or->alloc_flags
;
1327 struct scsi_device
*scsi_device
= or->osd_dev
->scsi_device
;
1328 struct request_queue
*q
= scsi_device
->request_queue
;
1329 struct request
*req
;
1332 req
= _make_request(q
, has_out
, has_out
? &or->out
: &or->in
, flags
);
1339 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1340 req
->cmd_flags
|= REQ_QUIET
;
1342 req
->timeout
= or->timeout
;
1343 req
->retries
= or->retries
;
1344 req
->sense
= or->sense
;
1350 /* allocate bidi request */
1351 req
= _make_request(q
, false, &or->in
, flags
);
1353 OSD_DEBUG("blk_get_request for bidi failed\n");
1357 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1358 or->in
.req
= or->request
->next_rq
= req
;
1365 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1366 or, has_in
, has_out
, ret
, or->request
);
1370 int osd_finalize_request(struct osd_request
*or,
1371 u8 options
, const void *cap
, const u8
*cap_key
)
1373 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1374 bool has_in
, has_out
;
1375 u64 out_data_bytes
= or->out
.total_bytes
;
1378 if (options
& OSD_REQ_FUA
)
1379 cdbh
->options
|= OSD_CDB_FUA
;
1381 if (options
& OSD_REQ_DPO
)
1382 cdbh
->options
|= OSD_CDB_DPO
;
1384 if (options
& OSD_REQ_BYPASS_TIMESTAMPS
)
1385 cdbh
->timestamp_control
= OSD_CDB_BYPASS_TIMESTAMPS
;
1387 osd_set_caps(&or->cdb
, cap
);
1389 has_in
= or->in
.bio
|| or->get_attr
.total_bytes
;
1390 has_out
= or->out
.bio
|| or->set_attr
.total_bytes
||
1391 or->enc_get_attr
.total_bytes
;
1393 ret
= _init_blk_request(or, has_in
, has_out
);
1395 OSD_DEBUG("_init_blk_request failed\n");
1399 or->out
.pad_buff
= sg_out_pad_buffer
;
1400 or->in
.pad_buff
= sg_in_pad_buffer
;
1402 if (!or->attributes_mode
)
1403 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
1404 cdbh
->command_specific_options
|= or->attributes_mode
;
1405 if (or->attributes_mode
== OSD_CDB_GET_ATTR_PAGE_SET_ONE
) {
1406 ret
= _osd_req_finalize_attr_page(or);
1408 /* TODO: I think that for the GET_ATTR command these 2 should
1409 * be reversed to keep them in execution order (for embeded
1410 * targets with low memory footprint)
1412 ret
= _osd_req_finalize_set_attr_list(or);
1414 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1418 ret
= _osd_req_finalize_get_attr_list(or);
1420 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1425 ret
= _osd_req_finalize_data_integrity(or, has_in
, has_out
,
1426 out_data_bytes
, cap_key
);
1430 osd_sec_sign_cdb(&or->cdb
, cap_key
);
1432 or->request
->cmd
= or->cdb
.buff
;
1433 or->request
->cmd_len
= _osd_req_cdb_len(or);
1437 EXPORT_SYMBOL(osd_finalize_request
);
1439 #define OSD_SENSE_PRINT1(fmt, a...) \
1441 if (__cur_sense_need_output) \
1442 OSD_ERR(fmt, ##a); \
1445 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1447 int osd_req_decode_sense_full(struct osd_request
*or,
1448 struct osd_sense_info
*osi
, bool silent
,
1449 struct osd_obj_id
*bad_obj_list __unused
, int max_obj __unused
,
1450 struct osd_attr
*bad_attr_list
, int max_attr
)
1452 int sense_len
, original_sense_len
;
1453 struct osd_sense_info local_osi
;
1454 struct scsi_sense_descriptor_based
*ssdb
;
1455 void *cur_descriptor
;
1456 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1457 const bool __cur_sense_need_output
= false;
1459 bool __cur_sense_need_output
= !silent
;
1462 if (!or->request
->errors
)
1465 ssdb
= or->request
->sense
;
1466 sense_len
= or->request
->sense_len
;
1467 if ((sense_len
< (int)sizeof(*ssdb
) || !ssdb
->sense_key
)) {
1468 OSD_ERR("Block-layer returned error(0x%x) but "
1469 "sense_len(%u) || key(%d) is empty\n",
1470 or->request
->errors
, sense_len
, ssdb
->sense_key
);
1474 if ((ssdb
->response_code
!= 0x72) && (ssdb
->response_code
!= 0x73)) {
1475 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1476 ssdb
->response_code
, sense_len
);
1480 osi
= osi
? : &local_osi
;
1481 memset(osi
, 0, sizeof(*osi
));
1482 osi
->key
= ssdb
->sense_key
;
1483 osi
->additional_code
= be16_to_cpu(ssdb
->additional_sense_code
);
1484 original_sense_len
= ssdb
->additional_sense_length
+ 8;
1486 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1487 if (__cur_sense_need_output
)
1488 __cur_sense_need_output
= (osi
->key
> scsi_sk_recovered_error
);
1490 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1491 "additional_code=0x%x\n",
1492 osi
->key
, original_sense_len
, sense_len
,
1493 osi
->additional_code
);
1495 if (original_sense_len
< sense_len
)
1496 sense_len
= original_sense_len
;
1498 cur_descriptor
= ssdb
->ssd
;
1499 sense_len
-= sizeof(*ssdb
);
1500 while (sense_len
> 0) {
1501 struct scsi_sense_descriptor
*ssd
= cur_descriptor
;
1502 int cur_len
= ssd
->additional_length
+ 2;
1504 sense_len
-= cur_len
;
1507 break; /* sense was truncated */
1509 switch (ssd
->descriptor_type
) {
1510 case scsi_sense_information
:
1511 case scsi_sense_command_specific_information
:
1513 struct scsi_sense_command_specific_data_descriptor
1514 *sscd
= cur_descriptor
;
1517 get_unaligned_be64(&sscd
->information
) ;
1519 "command_specific_information 0x%llx \n",
1520 _LLU(osi
->command_info
));
1523 case scsi_sense_key_specific
:
1525 struct scsi_sense_key_specific_data_descriptor
1526 *ssks
= cur_descriptor
;
1528 osi
->sense_info
= get_unaligned_be16(&ssks
->value
);
1530 "sense_key_specific_information %u"
1531 "sksv_cd_bpv_bp (0x%x)\n",
1532 osi
->sense_info
, ssks
->sksv_cd_bpv_bp
);
1535 case osd_sense_object_identification
:
1536 { /*FIXME: Keep first not last, Store in array*/
1537 struct osd_sense_identification_data_descriptor
1538 *osidd
= cur_descriptor
;
1540 osi
->not_initiated_command_functions
=
1541 le32_to_cpu(osidd
->not_initiated_functions
);
1542 osi
->completed_command_functions
=
1543 le32_to_cpu(osidd
->completed_functions
);
1544 osi
->obj
.partition
= be64_to_cpu(osidd
->partition_id
);
1545 osi
->obj
.id
= be64_to_cpu(osidd
->object_id
);
1547 "object_identification pid=0x%llx oid=0x%llx\n",
1548 _LLU(osi
->obj
.partition
), _LLU(osi
->obj
.id
));
1550 "not_initiated_bits(%x) "
1551 "completed_command_bits(%x)\n",
1552 osi
->not_initiated_command_functions
,
1553 osi
->completed_command_functions
);
1556 case osd_sense_response_integrity_check
:
1558 struct osd_sense_response_integrity_check_descriptor
1559 *osricd
= cur_descriptor
;
1560 const unsigned len
=
1561 sizeof(osricd
->integrity_check_value
);
1562 char key_dump
[len
*4 + 2]; /* 2nibbles+space+ASCII */
1564 hex_dump_to_buffer(osricd
->integrity_check_value
, len
,
1565 32, 1, key_dump
, sizeof(key_dump
), true);
1566 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump
);
1568 case osd_sense_attribute_identification
:
1570 struct osd_sense_attributes_data_descriptor
1571 *osadd
= cur_descriptor
;
1572 int len
= min(cur_len
, sense_len
);
1574 struct osd_sense_attr
*pattr
= osadd
->sense_attrs
;
1577 u32 attr_page
= be32_to_cpu(pattr
->attr_page
);
1578 u32 attr_id
= be32_to_cpu(pattr
->attr_id
);
1581 osi
->attr
.attr_page
= attr_page
;
1582 osi
->attr
.attr_id
= attr_id
;
1585 if (bad_attr_list
&& max_attr
) {
1586 bad_attr_list
->attr_page
= attr_page
;
1587 bad_attr_list
->attr_id
= attr_id
;
1592 "osd_sense_attribute_identification"
1593 "attr_page=0x%x attr_id=0x%x\n",
1594 attr_page
, attr_id
);
1597 /*These are not legal for OSD*/
1598 case scsi_sense_field_replaceable_unit
:
1599 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1601 case scsi_sense_stream_commands
:
1602 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1604 case scsi_sense_block_commands
:
1605 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1607 case scsi_sense_ata_return
:
1608 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1611 if (ssd
->descriptor_type
<= scsi_sense_Reserved_last
)
1613 "scsi_sense Reserved descriptor (0x%x)",
1614 ssd
->descriptor_type
);
1617 "scsi_sense Vendor descriptor (0x%x)",
1618 ssd
->descriptor_type
);
1621 cur_descriptor
+= cur_len
;
1624 return (osi
->key
> scsi_sk_recovered_error
) ? -EIO
: 0;
1626 EXPORT_SYMBOL(osd_req_decode_sense_full
);
1629 * Implementation of osd_sec.h API
1630 * TODO: Move to a separate osd_sec.c file at a later stage.
1633 enum { OSD_SEC_CAP_V1_ALL_CAPS
=
1634 OSD_SEC_CAP_APPEND
| OSD_SEC_CAP_OBJ_MGMT
| OSD_SEC_CAP_REMOVE
|
1635 OSD_SEC_CAP_CREATE
| OSD_SEC_CAP_SET_ATTR
| OSD_SEC_CAP_GET_ATTR
|
1636 OSD_SEC_CAP_WRITE
| OSD_SEC_CAP_READ
| OSD_SEC_CAP_POL_SEC
|
1637 OSD_SEC_CAP_GLOBAL
| OSD_SEC_CAP_DEV_MGMT
1640 enum { OSD_SEC_CAP_V2_ALL_CAPS
=
1641 OSD_SEC_CAP_V1_ALL_CAPS
| OSD_SEC_CAP_QUERY
| OSD_SEC_CAP_M_OBJECT
1644 void osd_sec_init_nosec_doall_caps(void *caps
,
1645 const struct osd_obj_id
*obj
, bool is_collection
, const bool is_v1
)
1647 struct osd_capability
*cap
= caps
;
1651 if (likely(obj
->id
)) {
1652 if (unlikely(is_collection
)) {
1653 type
= OSD_SEC_OBJ_COLLECTION
;
1654 descriptor_type
= is_v1
? OSD_SEC_OBJ_DESC_OBJ
:
1655 OSD_SEC_OBJ_DESC_COL
;
1657 type
= OSD_SEC_OBJ_USER
;
1658 descriptor_type
= OSD_SEC_OBJ_DESC_OBJ
;
1660 WARN_ON(!obj
->partition
);
1662 type
= obj
->partition
? OSD_SEC_OBJ_PARTITION
:
1664 descriptor_type
= OSD_SEC_OBJ_DESC_PAR
;
1667 memset(cap
, 0, sizeof(*cap
));
1669 cap
->h
.format
= OSD_SEC_CAP_FORMAT_VER1
;
1670 cap
->h
.integrity_algorithm__key_version
= 0; /* MAKE_BYTE(0, 0); */
1671 cap
->h
.security_method
= OSD_SEC_NOSEC
;
1672 /* cap->expiration_time;
1674 cap->discriminator[42-30];
1675 cap->object_created_time; */
1676 cap
->h
.object_type
= type
;
1677 osd_sec_set_caps(&cap
->h
, OSD_SEC_CAP_V1_ALL_CAPS
);
1678 cap
->h
.object_descriptor_type
= descriptor_type
;
1679 cap
->od
.obj_desc
.policy_access_tag
= 0;
1680 cap
->od
.obj_desc
.allowed_partition_id
= cpu_to_be64(obj
->partition
);
1681 cap
->od
.obj_desc
.allowed_object_id
= cpu_to_be64(obj
->id
);
1683 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps
);
1685 /* FIXME: Extract version from caps pointer.
1686 * Also Pete's target only supports caps from OSDv1 for now
1688 void osd_set_caps(struct osd_cdb
*cdb
, const void *caps
)
1690 bool is_ver1
= true;
1691 /* NOTE: They start at same address */
1692 memcpy(&cdb
->v1
.caps
, caps
, is_ver1
? OSDv1_CAP_LEN
: OSD_CAP_LEN
);
1695 bool osd_is_sec_alldata(struct osd_security_parameters
*sec_parms __unused
)
1700 void osd_sec_sign_cdb(struct osd_cdb
*ocdb __unused
, const u8
*cap_key __unused
)
1704 void osd_sec_sign_data(void *data_integ __unused
,
1705 struct bio
*bio __unused
, const u8
*cap_key __unused
)
1710 * Declared in osd_protocol.h
1711 * 4.12.5 Data-In and Data-Out buffer offsets
1712 * byte offset = mantissa * (2^(exponent+8))
1713 * Returns the smallest allowed encoded offset that contains given @offset
1714 * The actual encoded offset returned is @offset + *@padding.
1716 osd_cdb_offset
__osd_encode_offset(
1717 u64 offset
, unsigned *padding
, int min_shift
, int max_shift
)
1719 u64 try_offset
= -1, mod
, align
;
1720 osd_cdb_offset be32_offset
;
1727 for (shift
= min_shift
; shift
< max_shift
; ++shift
) {
1728 try_offset
= offset
>> shift
;
1729 if (try_offset
< (1 << OSD_OFFSET_MAX_BITS
))
1733 BUG_ON(shift
== max_shift
);
1736 mod
= offset
& (align
- 1);
1738 *padding
= align
- mod
;
1742 try_offset
|= ((shift
- 8) & 0xf) << 28;
1743 be32_offset
= cpu_to_be32((u32
)try_offset
);
1745 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1746 _LLU(offset
), _LLU(try_offset
& 0x0FFFFFFF), shift
,
1747 be32_offset
, *padding
);