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 <linux/slab.h>
44 #include <scsi/osd_initiator.h>
45 #include <scsi/osd_sec.h>
46 #include <scsi/osd_attributes.h>
47 #include <scsi/osd_sense.h>
49 #include <scsi/scsi_device.h>
51 #include "osd_debug.h"
54 # define __unused __attribute__((unused))
57 enum { OSD_REQ_RETRIES
= 1 };
59 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
60 MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
61 MODULE_LICENSE("GPL");
63 static inline void build_test(void)
65 /* structures were not packed */
66 BUILD_BUG_ON(sizeof(struct osd_capability
) != OSD_CAP_LEN
);
67 BUILD_BUG_ON(sizeof(struct osdv2_cdb
) != OSD_TOTAL_CDB_LEN
);
68 BUILD_BUG_ON(sizeof(struct osdv1_cdb
) != OSDv1_TOTAL_CDB_LEN
);
71 static const char *_osd_ver_desc(struct osd_request
*or)
73 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
76 #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
78 static int _osd_get_print_system_info(struct osd_dev
*od
,
79 void *caps
, struct osd_dev_info
*odi
)
81 struct osd_request
*or;
82 struct osd_attr get_attrs
[] = {
83 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION
, 8),
84 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION
, 16),
85 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL
, 32),
86 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL
, 4),
87 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER
, 64 /*variable*/),
88 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME
, 64 /*variable*/),
89 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY
, 8),
90 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY
, 8),
91 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS
, 8),
92 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK
, 6),
93 /* IBM-OSD-SIM Has a bug with this one put it last */
94 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID
, 20),
96 void *iter
= NULL
, *pFirst
;
97 int nelem
= ARRAY_SIZE(get_attrs
), a
= 0;
100 or = osd_start_request(od
, GFP_KERNEL
);
105 osd_req_get_attributes(or, &osd_root_object
);
106 osd_req_add_get_attr_list(or, get_attrs
, ARRAY_SIZE(get_attrs
));
108 ret
= osd_finalize_request(or, 0, caps
, NULL
);
112 ret
= osd_execute_request(or);
114 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret
);
118 osd_req_decode_get_attr_list(or, get_attrs
, &nelem
, &iter
);
120 OSD_INFO("Detected %s device\n",
123 pFirst
= get_attrs
[a
++].val_ptr
;
124 OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
127 pFirst
= get_attrs
[a
++].val_ptr
;
128 OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
131 pFirst
= get_attrs
[a
++].val_ptr
;
132 OSD_INFO("PRODUCT_MODEL [%s]\n",
135 pFirst
= get_attrs
[a
++].val_ptr
;
136 OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
137 pFirst
? get_unaligned_be32(pFirst
) : ~0U);
139 pFirst
= get_attrs
[a
++].val_ptr
;
140 OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
143 odi
->osdname_len
= get_attrs
[a
].len
;
144 /* Avoid NULL for memcmp optimization 0-length is good enough */
145 odi
->osdname
= kzalloc(odi
->osdname_len
+ 1, GFP_KERNEL
);
146 if (odi
->osdname_len
)
147 memcpy(odi
->osdname
, get_attrs
[a
].val_ptr
, odi
->osdname_len
);
148 OSD_INFO("OSD_NAME [%s]\n", odi
->osdname
);
151 pFirst
= get_attrs
[a
++].val_ptr
;
152 OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
153 pFirst
? _LLU(get_unaligned_be64(pFirst
)) : ~0ULL);
155 pFirst
= get_attrs
[a
++].val_ptr
;
156 OSD_INFO("USED_CAPACITY [0x%llx]\n",
157 pFirst
? _LLU(get_unaligned_be64(pFirst
)) : ~0ULL);
159 pFirst
= get_attrs
[a
++].val_ptr
;
160 OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
161 pFirst
? _LLU(get_unaligned_be64(pFirst
)) : ~0ULL);
166 pFirst
= get_attrs
[a
++].val_ptr
;
167 OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
168 ((char *)pFirst
)[0], ((char *)pFirst
)[1],
169 ((char *)pFirst
)[2], ((char *)pFirst
)[3],
170 ((char *)pFirst
)[4], ((char *)pFirst
)[5]);
172 if (a
< nelem
) { /* IBM-OSD-SIM bug, Might not have it */
173 unsigned len
= get_attrs
[a
].len
;
174 char sid_dump
[32*4 + 2]; /* 2nibbles+space+ASCII */
176 hex_dump_to_buffer(get_attrs
[a
].val_ptr
, len
, 32, 1,
177 sid_dump
, sizeof(sid_dump
), true);
178 OSD_INFO("OSD_SYSTEM_ID(%d)\n"
179 " [%s]\n", len
, sid_dump
);
181 if (unlikely(len
> sizeof(odi
->systemid
))) {
182 OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
183 "device idetification might not work\n", len
);
184 len
= sizeof(odi
->systemid
);
186 odi
->systemid_len
= len
;
187 memcpy(odi
->systemid
, get_attrs
[a
].val_ptr
, len
);
195 int osd_auto_detect_ver(struct osd_dev
*od
,
196 void *caps
, struct osd_dev_info
*odi
)
200 /* Auto-detect the osd version */
201 ret
= _osd_get_print_system_info(od
, caps
, odi
);
203 osd_dev_set_ver(od
, OSD_VER1
);
204 OSD_DEBUG("converting to OSD1\n");
205 ret
= _osd_get_print_system_info(od
, caps
, odi
);
210 EXPORT_SYMBOL(osd_auto_detect_ver
);
212 static unsigned _osd_req_cdb_len(struct osd_request
*or)
214 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN
: OSD_TOTAL_CDB_LEN
;
217 static unsigned _osd_req_alist_elem_size(struct osd_request
*or, unsigned len
)
219 return osd_req_is_ver1(or) ?
220 osdv1_attr_list_elem_size(len
) :
221 osdv2_attr_list_elem_size(len
);
224 static void _osd_req_alist_elem_encode(struct osd_request
*or,
225 void *attr_last
, const struct osd_attr
*oa
)
227 if (osd_req_is_ver1(or)) {
228 struct osdv1_attributes_list_element
*attr
= attr_last
;
230 attr
->attr_page
= cpu_to_be32(oa
->attr_page
);
231 attr
->attr_id
= cpu_to_be32(oa
->attr_id
);
232 attr
->attr_bytes
= cpu_to_be16(oa
->len
);
233 memcpy(attr
->attr_val
, oa
->val_ptr
, oa
->len
);
235 struct osdv2_attributes_list_element
*attr
= attr_last
;
237 attr
->attr_page
= cpu_to_be32(oa
->attr_page
);
238 attr
->attr_id
= cpu_to_be32(oa
->attr_id
);
239 attr
->attr_bytes
= cpu_to_be16(oa
->len
);
240 memcpy(attr
->attr_val
, oa
->val_ptr
, oa
->len
);
244 static int _osd_req_alist_elem_decode(struct osd_request
*or,
245 void *cur_p
, struct osd_attr
*oa
, unsigned max_bytes
)
248 if (osd_req_is_ver1(or)) {
249 struct osdv1_attributes_list_element
*attr
= cur_p
;
251 if (max_bytes
< sizeof(*attr
))
254 oa
->len
= be16_to_cpu(attr
->attr_bytes
);
255 inc
= _osd_req_alist_elem_size(or, oa
->len
);
259 oa
->attr_page
= be32_to_cpu(attr
->attr_page
);
260 oa
->attr_id
= be32_to_cpu(attr
->attr_id
);
262 /* OSD1: On empty attributes we return a pointer to 2 bytes
263 * of zeros. This keeps similar behaviour with OSD2.
266 oa
->val_ptr
= likely(oa
->len
) ? attr
->attr_val
:
267 (u8
*)&attr
->attr_bytes
;
269 struct osdv2_attributes_list_element
*attr
= cur_p
;
271 if (max_bytes
< sizeof(*attr
))
274 oa
->len
= be16_to_cpu(attr
->attr_bytes
);
275 inc
= _osd_req_alist_elem_size(or, oa
->len
);
279 oa
->attr_page
= be32_to_cpu(attr
->attr_page
);
280 oa
->attr_id
= be32_to_cpu(attr
->attr_id
);
282 /* OSD2: For convenience, on empty attributes, we return 8 bytes
283 * of zeros here. This keeps the same behaviour with OSD2r04,
284 * and is nice with null terminating ASCII fields.
285 * oa->val_ptr == NULL marks the end-of-list, or error.
287 oa
->val_ptr
= likely(oa
->len
) ? attr
->attr_val
: attr
->reserved
;
292 static unsigned _osd_req_alist_size(struct osd_request
*or, void *list_head
)
294 return osd_req_is_ver1(or) ?
295 osdv1_list_size(list_head
) :
296 osdv2_list_size(list_head
);
299 static unsigned _osd_req_sizeof_alist_header(struct osd_request
*or)
301 return osd_req_is_ver1(or) ?
302 sizeof(struct osdv1_attributes_list_header
) :
303 sizeof(struct osdv2_attributes_list_header
);
306 static void _osd_req_set_alist_type(struct osd_request
*or,
307 void *list
, int list_type
)
309 if (osd_req_is_ver1(or)) {
310 struct osdv1_attributes_list_header
*attr_list
= list
;
312 memset(attr_list
, 0, sizeof(*attr_list
));
313 attr_list
->type
= list_type
;
315 struct osdv2_attributes_list_header
*attr_list
= list
;
317 memset(attr_list
, 0, sizeof(*attr_list
));
318 attr_list
->type
= list_type
;
322 static bool _osd_req_is_alist_type(struct osd_request
*or,
323 void *list
, int list_type
)
328 if (osd_req_is_ver1(or)) {
329 struct osdv1_attributes_list_header
*attr_list
= list
;
331 return attr_list
->type
== list_type
;
333 struct osdv2_attributes_list_header
*attr_list
= list
;
335 return attr_list
->type
== list_type
;
339 /* This is for List-objects not Attributes-Lists */
340 static void _osd_req_encode_olist(struct osd_request
*or,
341 struct osd_obj_id_list
*list
)
343 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
345 if (osd_req_is_ver1(or)) {
346 cdbh
->v1
.list_identifier
= list
->list_identifier
;
347 cdbh
->v1
.start_address
= list
->continuation_id
;
349 cdbh
->v2
.list_identifier
= list
->list_identifier
;
350 cdbh
->v2
.start_address
= list
->continuation_id
;
354 static osd_cdb_offset
osd_req_encode_offset(struct osd_request
*or,
355 u64 offset
, unsigned *padding
)
357 return __osd_encode_offset(offset
, padding
,
358 osd_req_is_ver1(or) ?
359 OSDv1_OFFSET_MIN_SHIFT
: OSD_OFFSET_MIN_SHIFT
,
360 OSD_OFFSET_MAX_SHIFT
);
363 static struct osd_security_parameters
*
364 _osd_req_sec_params(struct osd_request
*or)
366 struct osd_cdb
*ocdb
= &or->cdb
;
368 if (osd_req_is_ver1(or))
369 return (struct osd_security_parameters
*)&ocdb
->v1
.sec_params
;
371 return (struct osd_security_parameters
*)&ocdb
->v2
.sec_params
;
374 void osd_dev_init(struct osd_dev
*osdd
, struct scsi_device
*scsi_device
)
376 memset(osdd
, 0, sizeof(*osdd
));
377 osdd
->scsi_device
= scsi_device
;
378 osdd
->def_timeout
= BLK_DEFAULT_SG_TIMEOUT
;
379 #ifdef OSD_VER1_SUPPORT
380 osdd
->version
= OSD_VER2
;
382 /* TODO: Allocate pools for osd_request attributes ... */
384 EXPORT_SYMBOL(osd_dev_init
);
386 void osd_dev_fini(struct osd_dev
*osdd
)
388 /* TODO: De-allocate pools */
390 osdd
->scsi_device
= NULL
;
392 EXPORT_SYMBOL(osd_dev_fini
);
394 static struct osd_request
*_osd_request_alloc(gfp_t gfp
)
396 struct osd_request
*or;
398 /* TODO: Use mempool with one saved request */
399 or = kzalloc(sizeof(*or), gfp
);
403 static void _osd_request_free(struct osd_request
*or)
408 struct osd_request
*osd_start_request(struct osd_dev
*dev
, gfp_t gfp
)
410 struct osd_request
*or;
412 or = _osd_request_alloc(gfp
);
417 or->alloc_flags
= gfp
;
418 or->timeout
= dev
->def_timeout
;
419 or->retries
= OSD_REQ_RETRIES
;
423 EXPORT_SYMBOL(osd_start_request
);
425 static void _osd_free_seg(struct osd_request
*or __unused
,
426 struct _osd_req_data_segment
*seg
)
428 if (!seg
->buff
|| !seg
->alloc_size
)
436 static void _put_request(struct request
*rq
)
439 * If osd_finalize_request() was called but the request was not
440 * executed through the block layer, then we must release BIOs.
441 * TODO: Keep error code in or->async_error. Need to audit all
444 if (unlikely(rq
->bio
))
445 blk_end_request(rq
, -ENOMEM
, blk_rq_bytes(rq
));
450 void osd_end_request(struct osd_request
*or)
452 struct request
*rq
= or->request
;
454 _osd_free_seg(or, &or->set_attr
);
455 _osd_free_seg(or, &or->enc_get_attr
);
456 _osd_free_seg(or, &or->get_attr
);
460 _put_request(rq
->next_rq
);
466 _osd_request_free(or);
468 EXPORT_SYMBOL(osd_end_request
);
470 static void _set_error_resid(struct osd_request
*or, struct request
*req
,
473 or->async_error
= error
;
474 or->req_errors
= req
->errors
? : error
;
475 or->sense_len
= req
->sense_len
;
477 or->out
.residual
= or->out
.req
->resid_len
;
479 or->in
.residual
= or->in
.req
->resid_len
;
482 int osd_execute_request(struct osd_request
*or)
484 int error
= blk_execute_rq(or->request
->q
, NULL
, or->request
, 0);
486 _set_error_resid(or, or->request
, error
);
489 EXPORT_SYMBOL(osd_execute_request
);
491 static void osd_request_async_done(struct request
*req
, int error
)
493 struct osd_request
*or = req
->end_io_data
;
495 _set_error_resid(or, req
, error
);
497 __blk_put_request(req
->q
, req
->next_rq
);
501 __blk_put_request(req
->q
, req
);
507 or->async_done(or, or->async_private
);
512 int osd_execute_request_async(struct osd_request
*or,
513 osd_req_done_fn
*done
, void *private)
515 or->request
->end_io_data
= or;
516 or->async_private
= private;
517 or->async_done
= done
;
519 blk_execute_rq_nowait(or->request
->q
, NULL
, or->request
, 0,
520 osd_request_async_done
);
523 EXPORT_SYMBOL(osd_execute_request_async
);
525 u8 sg_out_pad_buffer
[1 << OSDv1_OFFSET_MIN_SHIFT
];
526 u8 sg_in_pad_buffer
[1 << OSDv1_OFFSET_MIN_SHIFT
];
528 static int _osd_realloc_seg(struct osd_request
*or,
529 struct _osd_req_data_segment
*seg
, unsigned max_bytes
)
533 if (seg
->alloc_size
>= max_bytes
)
536 buff
= krealloc(seg
->buff
, max_bytes
, or->alloc_flags
);
538 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes
,
543 memset(buff
+ seg
->alloc_size
, 0, max_bytes
- seg
->alloc_size
);
545 seg
->alloc_size
= max_bytes
;
549 static int _alloc_set_attr_list(struct osd_request
*or,
550 const struct osd_attr
*oa
, unsigned nelem
, unsigned add_bytes
)
552 unsigned total_bytes
= add_bytes
;
554 for (; nelem
; --nelem
, ++oa
)
555 total_bytes
+= _osd_req_alist_elem_size(or, oa
->len
);
557 OSD_DEBUG("total_bytes=%d\n", total_bytes
);
558 return _osd_realloc_seg(or, &or->set_attr
, total_bytes
);
561 static int _alloc_get_attr_desc(struct osd_request
*or, unsigned max_bytes
)
563 OSD_DEBUG("total_bytes=%d\n", max_bytes
);
564 return _osd_realloc_seg(or, &or->enc_get_attr
, max_bytes
);
567 static int _alloc_get_attr_list(struct osd_request
*or)
569 OSD_DEBUG("total_bytes=%d\n", or->get_attr
.total_bytes
);
570 return _osd_realloc_seg(or, &or->get_attr
, or->get_attr
.total_bytes
);
574 * Common to all OSD commands
577 static void _osdv1_req_encode_common(struct osd_request
*or,
578 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
580 struct osdv1_cdb
*ocdb
= &or->cdb
.v1
;
583 * For speed, the commands
584 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
585 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
586 * are not supported here. Should pass zero and set after the call
588 act
&= cpu_to_be16(~0x0080); /* V1 action code */
590 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act
));
592 ocdb
->h
.varlen_cdb
.opcode
= VARIABLE_LENGTH_CMD
;
593 ocdb
->h
.varlen_cdb
.additional_cdb_length
= OSD_ADDITIONAL_CDB_LENGTH
;
594 ocdb
->h
.varlen_cdb
.service_action
= act
;
596 ocdb
->h
.partition
= cpu_to_be64(obj
->partition
);
597 ocdb
->h
.object
= cpu_to_be64(obj
->id
);
598 ocdb
->h
.v1
.length
= cpu_to_be64(len
);
599 ocdb
->h
.v1
.start_address
= cpu_to_be64(offset
);
602 static void _osdv2_req_encode_common(struct osd_request
*or,
603 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
605 struct osdv2_cdb
*ocdb
= &or->cdb
.v2
;
607 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act
));
609 ocdb
->h
.varlen_cdb
.opcode
= VARIABLE_LENGTH_CMD
;
610 ocdb
->h
.varlen_cdb
.additional_cdb_length
= OSD_ADDITIONAL_CDB_LENGTH
;
611 ocdb
->h
.varlen_cdb
.service_action
= act
;
613 ocdb
->h
.partition
= cpu_to_be64(obj
->partition
);
614 ocdb
->h
.object
= cpu_to_be64(obj
->id
);
615 ocdb
->h
.v2
.length
= cpu_to_be64(len
);
616 ocdb
->h
.v2
.start_address
= cpu_to_be64(offset
);
619 static void _osd_req_encode_common(struct osd_request
*or,
620 __be16 act
, const struct osd_obj_id
*obj
, u64 offset
, u64 len
)
622 if (osd_req_is_ver1(or))
623 _osdv1_req_encode_common(or, act
, obj
, offset
, len
);
625 _osdv2_req_encode_common(or, act
, obj
, offset
, len
);
631 /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
632 /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
634 void osd_req_format(struct osd_request
*or, u64 tot_capacity
)
636 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD
, &osd_root_object
, 0,
639 EXPORT_SYMBOL(osd_req_format
);
641 int osd_req_list_dev_partitions(struct osd_request
*or,
642 osd_id initial_id
, struct osd_obj_id_list
*list
, unsigned nelem
)
644 return osd_req_list_partition_objects(or, 0, initial_id
, list
, nelem
);
646 EXPORT_SYMBOL(osd_req_list_dev_partitions
);
648 static void _osd_req_encode_flush(struct osd_request
*or,
649 enum osd_options_flush_scope_values op
)
651 struct osd_cdb_head
*ocdb
= osd_cdb_head(&or->cdb
);
653 ocdb
->command_specific_options
= op
;
656 void osd_req_flush_obsd(struct osd_request
*or,
657 enum osd_options_flush_scope_values op
)
659 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD
, &osd_root_object
, 0, 0);
660 _osd_req_encode_flush(or, op
);
662 EXPORT_SYMBOL(osd_req_flush_obsd
);
664 /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
665 const u8 *cdb, ...); */
666 /*TODO: void osd_req_task_management(struct osd_request *, ...); */
671 static void _osd_req_encode_partition(struct osd_request
*or,
672 __be16 act
, osd_id partition
)
674 struct osd_obj_id par
= {
675 .partition
= partition
,
679 _osd_req_encode_common(or, act
, &par
, 0, 0);
682 void osd_req_create_partition(struct osd_request
*or, osd_id partition
)
684 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION
, partition
);
686 EXPORT_SYMBOL(osd_req_create_partition
);
688 void osd_req_remove_partition(struct osd_request
*or, osd_id partition
)
690 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION
, partition
);
692 EXPORT_SYMBOL(osd_req_remove_partition
);
694 /*TODO: void osd_req_set_partition_key(struct osd_request *,
695 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
696 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
698 static int _osd_req_list_objects(struct osd_request
*or,
699 __be16 action
, const struct osd_obj_id
*obj
, osd_id initial_id
,
700 struct osd_obj_id_list
*list
, unsigned nelem
)
702 struct request_queue
*q
= osd_request_queue(or->osd_dev
);
703 u64 len
= nelem
* sizeof(osd_id
) + sizeof(*list
);
706 _osd_req_encode_common(or, action
, obj
, (u64
)initial_id
, len
);
708 if (list
->list_identifier
)
709 _osd_req_encode_olist(or, list
);
712 bio
= bio_map_kern(q
, list
, len
, or->alloc_flags
);
714 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
718 bio
->bi_rw
&= ~REQ_WRITE
;
720 or->in
.total_bytes
= bio
->bi_size
;
724 int osd_req_list_partition_collections(struct osd_request
*or,
725 osd_id partition
, osd_id initial_id
, struct osd_obj_id_list
*list
,
728 struct osd_obj_id par
= {
729 .partition
= partition
,
733 return osd_req_list_collection_objects(or, &par
, initial_id
, list
,
736 EXPORT_SYMBOL(osd_req_list_partition_collections
);
738 int osd_req_list_partition_objects(struct osd_request
*or,
739 osd_id partition
, osd_id initial_id
, struct osd_obj_id_list
*list
,
742 struct osd_obj_id par
= {
743 .partition
= partition
,
747 return _osd_req_list_objects(or, OSD_ACT_LIST
, &par
, initial_id
, list
,
750 EXPORT_SYMBOL(osd_req_list_partition_objects
);
752 void osd_req_flush_partition(struct osd_request
*or,
753 osd_id partition
, enum osd_options_flush_scope_values op
)
755 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION
, partition
);
756 _osd_req_encode_flush(or, op
);
758 EXPORT_SYMBOL(osd_req_flush_partition
);
761 * Collection commands
763 /*TODO: void osd_req_create_collection(struct osd_request *,
764 const struct osd_obj_id *); */
765 /*TODO: void osd_req_remove_collection(struct osd_request *,
766 const struct osd_obj_id *); */
768 int osd_req_list_collection_objects(struct osd_request
*or,
769 const struct osd_obj_id
*obj
, osd_id initial_id
,
770 struct osd_obj_id_list
*list
, unsigned nelem
)
772 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION
, obj
,
773 initial_id
, list
, nelem
);
775 EXPORT_SYMBOL(osd_req_list_collection_objects
);
777 /*TODO: void query(struct osd_request *, ...); V2 */
779 void osd_req_flush_collection(struct osd_request
*or,
780 const struct osd_obj_id
*obj
, enum osd_options_flush_scope_values op
)
782 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION
, obj
, 0, 0);
783 _osd_req_encode_flush(or, op
);
785 EXPORT_SYMBOL(osd_req_flush_collection
);
787 /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
788 /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
793 void osd_req_create_object(struct osd_request
*or, struct osd_obj_id
*obj
)
795 _osd_req_encode_common(or, OSD_ACT_CREATE
, obj
, 0, 0);
797 EXPORT_SYMBOL(osd_req_create_object
);
799 void osd_req_remove_object(struct osd_request
*or, struct osd_obj_id
*obj
)
801 _osd_req_encode_common(or, OSD_ACT_REMOVE
, obj
, 0, 0);
803 EXPORT_SYMBOL(osd_req_remove_object
);
806 /*TODO: void osd_req_create_multi(struct osd_request *or,
807 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
810 void osd_req_write(struct osd_request
*or,
811 const struct osd_obj_id
*obj
, u64 offset
,
812 struct bio
*bio
, u64 len
)
814 _osd_req_encode_common(or, OSD_ACT_WRITE
, obj
, offset
, len
);
815 WARN_ON(or->out
.bio
|| or->out
.total_bytes
);
816 WARN_ON(0 == (bio
->bi_rw
& REQ_WRITE
));
818 or->out
.total_bytes
= len
;
820 EXPORT_SYMBOL(osd_req_write
);
822 int osd_req_write_kern(struct osd_request
*or,
823 const struct osd_obj_id
*obj
, u64 offset
, void* buff
, u64 len
)
825 struct request_queue
*req_q
= osd_request_queue(or->osd_dev
);
826 struct bio
*bio
= bio_map_kern(req_q
, buff
, len
, GFP_KERNEL
);
831 bio
->bi_rw
|= REQ_WRITE
;
832 osd_req_write(or, obj
, offset
, bio
, len
);
835 EXPORT_SYMBOL(osd_req_write_kern
);
837 /*TODO: void osd_req_append(struct osd_request *,
838 const struct osd_obj_id *, struct bio *data_out); */
839 /*TODO: void osd_req_create_write(struct osd_request *,
840 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
841 /*TODO: void osd_req_clear(struct osd_request *,
842 const struct osd_obj_id *, u64 offset, u64 len); */
843 /*TODO: void osd_req_punch(struct osd_request *,
844 const struct osd_obj_id *, u64 offset, u64 len); V2 */
846 void osd_req_flush_object(struct osd_request
*or,
847 const struct osd_obj_id
*obj
, enum osd_options_flush_scope_values op
,
848 /*V2*/ u64 offset
, /*V2*/ u64 len
)
850 if (unlikely(osd_req_is_ver1(or) && (offset
|| len
))) {
851 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
856 _osd_req_encode_common(or, OSD_ACT_FLUSH
, obj
, offset
, len
);
857 _osd_req_encode_flush(or, op
);
859 EXPORT_SYMBOL(osd_req_flush_object
);
861 void osd_req_read(struct osd_request
*or,
862 const struct osd_obj_id
*obj
, u64 offset
,
863 struct bio
*bio
, u64 len
)
865 _osd_req_encode_common(or, OSD_ACT_READ
, obj
, offset
, len
);
866 WARN_ON(or->in
.bio
|| or->in
.total_bytes
);
867 WARN_ON(bio
->bi_rw
& REQ_WRITE
);
869 or->in
.total_bytes
= len
;
871 EXPORT_SYMBOL(osd_req_read
);
873 int osd_req_read_kern(struct osd_request
*or,
874 const struct osd_obj_id
*obj
, u64 offset
, void* buff
, u64 len
)
876 struct request_queue
*req_q
= osd_request_queue(or->osd_dev
);
877 struct bio
*bio
= bio_map_kern(req_q
, buff
, len
, GFP_KERNEL
);
882 osd_req_read(or, obj
, offset
, bio
, len
);
885 EXPORT_SYMBOL(osd_req_read_kern
);
887 void osd_req_get_attributes(struct osd_request
*or,
888 const struct osd_obj_id
*obj
)
890 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES
, obj
, 0, 0);
892 EXPORT_SYMBOL(osd_req_get_attributes
);
894 void osd_req_set_attributes(struct osd_request
*or,
895 const struct osd_obj_id
*obj
)
897 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES
, obj
, 0, 0);
899 EXPORT_SYMBOL(osd_req_set_attributes
);
902 * Attributes List-mode
905 int osd_req_add_set_attr_list(struct osd_request
*or,
906 const struct osd_attr
*oa
, unsigned nelem
)
908 unsigned total_bytes
= or->set_attr
.total_bytes
;
912 if (or->attributes_mode
&&
913 or->attributes_mode
!= OSD_CDB_GET_SET_ATTR_LISTS
) {
917 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
919 if (!total_bytes
) { /* first-time: allocate and put list header */
920 total_bytes
= _osd_req_sizeof_alist_header(or);
921 ret
= _alloc_set_attr_list(or, oa
, nelem
, total_bytes
);
924 _osd_req_set_alist_type(or, or->set_attr
.buff
,
925 OSD_ATTR_LIST_SET_RETRIEVE
);
927 attr_last
= or->set_attr
.buff
+ total_bytes
;
929 for (; nelem
; --nelem
) {
930 unsigned elem_size
= _osd_req_alist_elem_size(or, oa
->len
);
932 total_bytes
+= elem_size
;
933 if (unlikely(or->set_attr
.alloc_size
< total_bytes
)) {
934 or->set_attr
.total_bytes
= total_bytes
- elem_size
;
935 ret
= _alloc_set_attr_list(or, oa
, nelem
, total_bytes
);
939 or->set_attr
.buff
+ or->set_attr
.total_bytes
;
942 _osd_req_alist_elem_encode(or, attr_last
, oa
);
944 attr_last
+= elem_size
;
948 or->set_attr
.total_bytes
= total_bytes
;
951 EXPORT_SYMBOL(osd_req_add_set_attr_list
);
953 static int _req_append_segment(struct osd_request
*or,
954 unsigned padding
, struct _osd_req_data_segment
*seg
,
955 struct _osd_req_data_segment
*last_seg
, struct _osd_io_info
*io
)
961 /* check if we can just add it to last buffer */
963 (padding
<= last_seg
->alloc_size
- last_seg
->total_bytes
))
964 pad_buff
= last_seg
->buff
+ last_seg
->total_bytes
;
966 pad_buff
= io
->pad_buff
;
968 ret
= blk_rq_map_kern(io
->req
->q
, io
->req
, pad_buff
, padding
,
972 io
->total_bytes
+= padding
;
975 ret
= blk_rq_map_kern(io
->req
->q
, io
->req
, seg
->buff
, seg
->total_bytes
,
980 io
->total_bytes
+= seg
->total_bytes
;
981 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding
, seg
->buff
,
986 static int _osd_req_finalize_set_attr_list(struct osd_request
*or)
988 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
992 if (!or->set_attr
.total_bytes
) {
993 cdbh
->attrs_list
.set_attr_offset
= OSD_OFFSET_UNUSED
;
997 cdbh
->attrs_list
.set_attr_bytes
= cpu_to_be32(or->set_attr
.total_bytes
);
998 cdbh
->attrs_list
.set_attr_offset
=
999 osd_req_encode_offset(or, or->out
.total_bytes
, &padding
);
1001 ret
= _req_append_segment(or, padding
, &or->set_attr
,
1002 or->out
.last_seg
, &or->out
);
1006 or->out
.last_seg
= &or->set_attr
;
1010 int osd_req_add_get_attr_list(struct osd_request
*or,
1011 const struct osd_attr
*oa
, unsigned nelem
)
1013 unsigned total_bytes
= or->enc_get_attr
.total_bytes
;
1017 if (or->attributes_mode
&&
1018 or->attributes_mode
!= OSD_CDB_GET_SET_ATTR_LISTS
) {
1022 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
1024 /* first time calc data-in list header size */
1025 if (!or->get_attr
.total_bytes
)
1026 or->get_attr
.total_bytes
= _osd_req_sizeof_alist_header(or);
1028 /* calc data-out info */
1029 if (!total_bytes
) { /* first-time: allocate and put list header */
1032 total_bytes
= _osd_req_sizeof_alist_header(or);
1033 max_bytes
= total_bytes
+
1034 nelem
* sizeof(struct osd_attributes_list_attrid
);
1035 ret
= _alloc_get_attr_desc(or, max_bytes
);
1039 _osd_req_set_alist_type(or, or->enc_get_attr
.buff
,
1042 attr_last
= or->enc_get_attr
.buff
+ total_bytes
;
1044 for (; nelem
; --nelem
) {
1045 struct osd_attributes_list_attrid
*attrid
;
1046 const unsigned cur_size
= sizeof(*attrid
);
1048 total_bytes
+= cur_size
;
1049 if (unlikely(or->enc_get_attr
.alloc_size
< total_bytes
)) {
1050 or->enc_get_attr
.total_bytes
= total_bytes
- cur_size
;
1051 ret
= _alloc_get_attr_desc(or,
1052 total_bytes
+ nelem
* sizeof(*attrid
));
1055 attr_last
= or->enc_get_attr
.buff
+
1056 or->enc_get_attr
.total_bytes
;
1060 attrid
->attr_page
= cpu_to_be32(oa
->attr_page
);
1061 attrid
->attr_id
= cpu_to_be32(oa
->attr_id
);
1063 attr_last
+= cur_size
;
1065 /* calc data-in size */
1066 or->get_attr
.total_bytes
+=
1067 _osd_req_alist_elem_size(or, oa
->len
);
1071 or->enc_get_attr
.total_bytes
= total_bytes
;
1074 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1075 or->get_attr
.total_bytes
,
1076 or->get_attr
.total_bytes
- _osd_req_sizeof_alist_header(or),
1077 or->enc_get_attr
.total_bytes
,
1078 (or->enc_get_attr
.total_bytes
- _osd_req_sizeof_alist_header(or))
1079 / sizeof(struct osd_attributes_list_attrid
));
1083 EXPORT_SYMBOL(osd_req_add_get_attr_list
);
1085 static int _osd_req_finalize_get_attr_list(struct osd_request
*or)
1087 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1088 unsigned out_padding
;
1089 unsigned in_padding
;
1092 if (!or->enc_get_attr
.total_bytes
) {
1093 cdbh
->attrs_list
.get_attr_desc_offset
= OSD_OFFSET_UNUSED
;
1094 cdbh
->attrs_list
.get_attr_offset
= OSD_OFFSET_UNUSED
;
1098 ret
= _alloc_get_attr_list(or);
1102 /* The out-going buffer info update */
1103 OSD_DEBUG("out-going\n");
1104 cdbh
->attrs_list
.get_attr_desc_bytes
=
1105 cpu_to_be32(or->enc_get_attr
.total_bytes
);
1107 cdbh
->attrs_list
.get_attr_desc_offset
=
1108 osd_req_encode_offset(or, or->out
.total_bytes
, &out_padding
);
1110 ret
= _req_append_segment(or, out_padding
, &or->enc_get_attr
,
1111 or->out
.last_seg
, &or->out
);
1114 or->out
.last_seg
= &or->enc_get_attr
;
1116 /* The incoming buffer info update */
1117 OSD_DEBUG("in-coming\n");
1118 cdbh
->attrs_list
.get_attr_alloc_length
=
1119 cpu_to_be32(or->get_attr
.total_bytes
);
1121 cdbh
->attrs_list
.get_attr_offset
=
1122 osd_req_encode_offset(or, or->in
.total_bytes
, &in_padding
);
1124 ret
= _req_append_segment(or, in_padding
, &or->get_attr
, NULL
,
1128 or->in
.last_seg
= &or->get_attr
;
1133 int osd_req_decode_get_attr_list(struct osd_request
*or,
1134 struct osd_attr
*oa
, int *nelem
, void **iterator
)
1136 unsigned cur_bytes
, returned_bytes
;
1138 const unsigned sizeof_attr_list
= _osd_req_sizeof_alist_header(or);
1141 if (!_osd_req_is_alist_type(or, or->get_attr
.buff
,
1142 OSD_ATTR_LIST_SET_RETRIEVE
)) {
1152 BUG_ON((*iterator
< or->get_attr
.buff
) ||
1153 (or->get_attr
.buff
+ or->get_attr
.alloc_size
< *iterator
));
1155 cur_bytes
= (*iterator
- or->get_attr
.buff
) - sizeof_attr_list
;
1156 returned_bytes
= or->get_attr
.total_bytes
;
1157 } else { /* first time decode the list header */
1158 cur_bytes
= sizeof_attr_list
;
1159 returned_bytes
= _osd_req_alist_size(or, or->get_attr
.buff
) +
1162 cur_p
= or->get_attr
.buff
+ sizeof_attr_list
;
1164 if (returned_bytes
> or->get_attr
.alloc_size
) {
1165 OSD_DEBUG("target report: space was not big enough! "
1166 "Allocate=%u Needed=%u\n",
1167 or->get_attr
.alloc_size
,
1168 returned_bytes
+ sizeof_attr_list
);
1171 or->get_attr
.alloc_size
- sizeof_attr_list
;
1173 or->get_attr
.total_bytes
= returned_bytes
;
1176 for (n
= 0; (n
< *nelem
) && (cur_bytes
< returned_bytes
); ++n
) {
1177 int inc
= _osd_req_alist_elem_decode(or, cur_p
, oa
,
1178 returned_bytes
- cur_bytes
);
1181 OSD_ERR("BAD FOOD from target. list not valid!"
1183 cur_bytes
, returned_bytes
, n
);
1185 cur_bytes
= returned_bytes
; /* break the caller loop */
1194 *iterator
= (returned_bytes
- cur_bytes
) ? cur_p
: NULL
;
1196 return returned_bytes
- cur_bytes
;
1198 EXPORT_SYMBOL(osd_req_decode_get_attr_list
);
1201 * Attributes Page-mode
1204 int osd_req_add_get_attr_page(struct osd_request
*or,
1205 u32 page_id
, void *attar_page
, unsigned max_page_len
,
1206 const struct osd_attr
*set_one_attr
)
1208 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1210 if (or->attributes_mode
&&
1211 or->attributes_mode
!= OSD_CDB_GET_ATTR_PAGE_SET_ONE
) {
1215 or->attributes_mode
= OSD_CDB_GET_ATTR_PAGE_SET_ONE
;
1217 or->get_attr
.buff
= attar_page
;
1218 or->get_attr
.total_bytes
= max_page_len
;
1220 or->set_attr
.buff
= set_one_attr
->val_ptr
;
1221 or->set_attr
.total_bytes
= set_one_attr
->len
;
1223 cdbh
->attrs_page
.get_attr_page
= cpu_to_be32(page_id
);
1224 cdbh
->attrs_page
.get_attr_alloc_length
= cpu_to_be32(max_page_len
);
1225 /* ocdb->attrs_page.get_attr_offset; */
1227 cdbh
->attrs_page
.set_attr_page
= cpu_to_be32(set_one_attr
->attr_page
);
1228 cdbh
->attrs_page
.set_attr_id
= cpu_to_be32(set_one_attr
->attr_id
);
1229 cdbh
->attrs_page
.set_attr_length
= cpu_to_be32(set_one_attr
->len
);
1230 /* ocdb->attrs_page.set_attr_offset; */
1233 EXPORT_SYMBOL(osd_req_add_get_attr_page
);
1235 static int _osd_req_finalize_attr_page(struct osd_request
*or)
1237 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1238 unsigned in_padding
, out_padding
;
1242 cdbh
->attrs_page
.get_attr_offset
=
1243 osd_req_encode_offset(or, or->in
.total_bytes
, &in_padding
);
1245 ret
= _req_append_segment(or, in_padding
, &or->get_attr
, NULL
,
1251 cdbh
->attrs_page
.set_attr_offset
=
1252 osd_req_encode_offset(or, or->out
.total_bytes
, &out_padding
);
1254 ret
= _req_append_segment(or, out_padding
, &or->enc_get_attr
, NULL
,
1259 static inline void osd_sec_parms_set_out_offset(bool is_v1
,
1260 struct osd_security_parameters
*sec_parms
, osd_cdb_offset offset
)
1263 sec_parms
->v1
.data_out_integrity_check_offset
= offset
;
1265 sec_parms
->v2
.data_out_integrity_check_offset
= offset
;
1268 static inline void osd_sec_parms_set_in_offset(bool is_v1
,
1269 struct osd_security_parameters
*sec_parms
, osd_cdb_offset offset
)
1272 sec_parms
->v1
.data_in_integrity_check_offset
= offset
;
1274 sec_parms
->v2
.data_in_integrity_check_offset
= offset
;
1277 static int _osd_req_finalize_data_integrity(struct osd_request
*or,
1278 bool has_in
, bool has_out
, u64 out_data_bytes
, const u8
*cap_key
)
1280 struct osd_security_parameters
*sec_parms
= _osd_req_sec_params(or);
1283 if (!osd_is_sec_alldata(sec_parms
))
1287 struct _osd_req_data_segment seg
= {
1288 .buff
= &or->out_data_integ
,
1289 .total_bytes
= sizeof(or->out_data_integ
),
1293 or->out_data_integ
.data_bytes
= cpu_to_be64(out_data_bytes
);
1294 or->out_data_integ
.set_attributes_bytes
= cpu_to_be64(
1295 or->set_attr
.total_bytes
);
1296 or->out_data_integ
.get_attributes_bytes
= cpu_to_be64(
1297 or->enc_get_attr
.total_bytes
);
1299 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms
,
1300 osd_req_encode_offset(or, or->out
.total_bytes
, &pad
));
1302 ret
= _req_append_segment(or, pad
, &seg
, or->out
.last_seg
,
1306 or->out
.last_seg
= NULL
;
1308 /* they are now all chained to request sign them all together */
1309 osd_sec_sign_data(&or->out_data_integ
, or->out
.req
->bio
,
1314 struct _osd_req_data_segment seg
= {
1315 .buff
= &or->in_data_integ
,
1316 .total_bytes
= sizeof(or->in_data_integ
),
1320 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms
,
1321 osd_req_encode_offset(or, or->in
.total_bytes
, &pad
));
1323 ret
= _req_append_segment(or, pad
, &seg
, or->in
.last_seg
,
1328 or->in
.last_seg
= NULL
;
1335 * osd_finalize_request and helpers
1337 static struct request
*_make_request(struct request_queue
*q
, bool has_write
,
1338 struct _osd_io_info
*oii
, gfp_t flags
)
1341 return blk_make_request(q
, oii
->bio
, flags
);
1343 struct request
*req
;
1345 req
= blk_get_request(q
, has_write
? WRITE
: READ
, flags
);
1347 return ERR_PTR(-ENOMEM
);
1353 static int _init_blk_request(struct osd_request
*or,
1354 bool has_in
, bool has_out
)
1356 gfp_t flags
= or->alloc_flags
;
1357 struct scsi_device
*scsi_device
= or->osd_dev
->scsi_device
;
1358 struct request_queue
*q
= scsi_device
->request_queue
;
1359 struct request
*req
;
1362 req
= _make_request(q
, has_out
, has_out
? &or->out
: &or->in
, flags
);
1369 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1370 req
->cmd_flags
|= REQ_QUIET
;
1372 req
->timeout
= or->timeout
;
1373 req
->retries
= or->retries
;
1374 req
->sense
= or->sense
;
1380 /* allocate bidi request */
1381 req
= _make_request(q
, false, &or->in
, flags
);
1383 OSD_DEBUG("blk_get_request for bidi failed\n");
1387 req
->cmd_type
= REQ_TYPE_BLOCK_PC
;
1388 or->in
.req
= or->request
->next_rq
= req
;
1395 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1396 or, has_in
, has_out
, ret
, or->request
);
1400 int osd_finalize_request(struct osd_request
*or,
1401 u8 options
, const void *cap
, const u8
*cap_key
)
1403 struct osd_cdb_head
*cdbh
= osd_cdb_head(&or->cdb
);
1404 bool has_in
, has_out
;
1405 u64 out_data_bytes
= or->out
.total_bytes
;
1408 if (options
& OSD_REQ_FUA
)
1409 cdbh
->options
|= OSD_CDB_FUA
;
1411 if (options
& OSD_REQ_DPO
)
1412 cdbh
->options
|= OSD_CDB_DPO
;
1414 if (options
& OSD_REQ_BYPASS_TIMESTAMPS
)
1415 cdbh
->timestamp_control
= OSD_CDB_BYPASS_TIMESTAMPS
;
1417 osd_set_caps(&or->cdb
, cap
);
1419 has_in
= or->in
.bio
|| or->get_attr
.total_bytes
;
1420 has_out
= or->out
.bio
|| or->set_attr
.total_bytes
||
1421 or->enc_get_attr
.total_bytes
;
1423 ret
= _init_blk_request(or, has_in
, has_out
);
1425 OSD_DEBUG("_init_blk_request failed\n");
1429 or->out
.pad_buff
= sg_out_pad_buffer
;
1430 or->in
.pad_buff
= sg_in_pad_buffer
;
1432 if (!or->attributes_mode
)
1433 or->attributes_mode
= OSD_CDB_GET_SET_ATTR_LISTS
;
1434 cdbh
->command_specific_options
|= or->attributes_mode
;
1435 if (or->attributes_mode
== OSD_CDB_GET_ATTR_PAGE_SET_ONE
) {
1436 ret
= _osd_req_finalize_attr_page(or);
1438 OSD_DEBUG("_osd_req_finalize_attr_page failed\n");
1442 /* TODO: I think that for the GET_ATTR command these 2 should
1443 * be reversed to keep them in execution order (for embeded
1444 * targets with low memory footprint)
1446 ret
= _osd_req_finalize_set_attr_list(or);
1448 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1452 ret
= _osd_req_finalize_get_attr_list(or);
1454 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1459 ret
= _osd_req_finalize_data_integrity(or, has_in
, has_out
,
1460 out_data_bytes
, cap_key
);
1464 osd_sec_sign_cdb(&or->cdb
, cap_key
);
1466 or->request
->cmd
= or->cdb
.buff
;
1467 or->request
->cmd_len
= _osd_req_cdb_len(or);
1471 EXPORT_SYMBOL(osd_finalize_request
);
1473 static bool _is_osd_security_code(int code
)
1475 return (code
== osd_security_audit_value_frozen
) ||
1476 (code
== osd_security_working_key_frozen
) ||
1477 (code
== osd_nonce_not_unique
) ||
1478 (code
== osd_nonce_timestamp_out_of_range
) ||
1479 (code
== osd_invalid_dataout_buffer_integrity_check_value
);
1482 #define OSD_SENSE_PRINT1(fmt, a...) \
1484 if (__cur_sense_need_output) \
1485 OSD_ERR(fmt, ##a); \
1488 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1490 int osd_req_decode_sense_full(struct osd_request
*or,
1491 struct osd_sense_info
*osi
, bool silent
,
1492 struct osd_obj_id
*bad_obj_list __unused
, int max_obj __unused
,
1493 struct osd_attr
*bad_attr_list
, int max_attr
)
1495 int sense_len
, original_sense_len
;
1496 struct osd_sense_info local_osi
;
1497 struct scsi_sense_descriptor_based
*ssdb
;
1498 void *cur_descriptor
;
1499 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1500 const bool __cur_sense_need_output
= false;
1502 bool __cur_sense_need_output
= !silent
;
1506 if (likely(!or->req_errors
))
1509 osi
= osi
? : &local_osi
;
1510 memset(osi
, 0, sizeof(*osi
));
1512 ssdb
= (typeof(ssdb
))or->sense
;
1513 sense_len
= or->sense_len
;
1514 if ((sense_len
< (int)sizeof(*ssdb
) || !ssdb
->sense_key
)) {
1515 OSD_ERR("Block-layer returned error(0x%x) but "
1516 "sense_len(%u) || key(%d) is empty\n",
1517 or->req_errors
, sense_len
, ssdb
->sense_key
);
1521 if ((ssdb
->response_code
!= 0x72) && (ssdb
->response_code
!= 0x73)) {
1522 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1523 ssdb
->response_code
, sense_len
);
1527 osi
->key
= ssdb
->sense_key
;
1528 osi
->additional_code
= be16_to_cpu(ssdb
->additional_sense_code
);
1529 original_sense_len
= ssdb
->additional_sense_length
+ 8;
1531 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1532 if (__cur_sense_need_output
)
1533 __cur_sense_need_output
= (osi
->key
> scsi_sk_recovered_error
);
1535 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1536 "additional_code=0x%x async_error=%d errors=0x%x\n",
1537 osi
->key
, original_sense_len
, sense_len
,
1538 osi
->additional_code
, or->async_error
,
1541 if (original_sense_len
< sense_len
)
1542 sense_len
= original_sense_len
;
1544 cur_descriptor
= ssdb
->ssd
;
1545 sense_len
-= sizeof(*ssdb
);
1546 while (sense_len
> 0) {
1547 struct scsi_sense_descriptor
*ssd
= cur_descriptor
;
1548 int cur_len
= ssd
->additional_length
+ 2;
1550 sense_len
-= cur_len
;
1553 break; /* sense was truncated */
1555 switch (ssd
->descriptor_type
) {
1556 case scsi_sense_information
:
1557 case scsi_sense_command_specific_information
:
1559 struct scsi_sense_command_specific_data_descriptor
1560 *sscd
= cur_descriptor
;
1563 get_unaligned_be64(&sscd
->information
) ;
1565 "command_specific_information 0x%llx \n",
1566 _LLU(osi
->command_info
));
1569 case scsi_sense_key_specific
:
1571 struct scsi_sense_key_specific_data_descriptor
1572 *ssks
= cur_descriptor
;
1574 osi
->sense_info
= get_unaligned_be16(&ssks
->value
);
1576 "sense_key_specific_information %u"
1577 "sksv_cd_bpv_bp (0x%x)\n",
1578 osi
->sense_info
, ssks
->sksv_cd_bpv_bp
);
1581 case osd_sense_object_identification
:
1583 struct osd_sense_identification_data_descriptor
1584 *osidd
= cur_descriptor
;
1586 osi
->not_initiated_command_functions
=
1587 le32_to_cpu(osidd
->not_initiated_functions
);
1588 osi
->completed_command_functions
=
1589 le32_to_cpu(osidd
->completed_functions
);
1590 osi
->obj
.partition
= be64_to_cpu(osidd
->partition_id
);
1591 osi
->obj
.id
= be64_to_cpu(osidd
->object_id
);
1593 "object_identification pid=0x%llx oid=0x%llx\n",
1594 _LLU(osi
->obj
.partition
), _LLU(osi
->obj
.id
));
1596 "not_initiated_bits(%x) "
1597 "completed_command_bits(%x)\n",
1598 osi
->not_initiated_command_functions
,
1599 osi
->completed_command_functions
);
1602 case osd_sense_response_integrity_check
:
1604 struct osd_sense_response_integrity_check_descriptor
1605 *osricd
= cur_descriptor
;
1606 const unsigned len
=
1607 sizeof(osricd
->integrity_check_value
);
1608 char key_dump
[len
*4 + 2]; /* 2nibbles+space+ASCII */
1610 hex_dump_to_buffer(osricd
->integrity_check_value
, len
,
1611 32, 1, key_dump
, sizeof(key_dump
), true);
1612 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump
);
1614 case osd_sense_attribute_identification
:
1616 struct osd_sense_attributes_data_descriptor
1617 *osadd
= cur_descriptor
;
1618 unsigned len
= min(cur_len
, sense_len
);
1619 struct osd_sense_attr
*pattr
= osadd
->sense_attrs
;
1621 while (len
>= sizeof(*pattr
)) {
1622 u32 attr_page
= be32_to_cpu(pattr
->attr_page
);
1623 u32 attr_id
= be32_to_cpu(pattr
->attr_id
);
1625 if (!osi
->attr
.attr_page
) {
1626 osi
->attr
.attr_page
= attr_page
;
1627 osi
->attr
.attr_id
= attr_id
;
1630 if (bad_attr_list
&& max_attr
) {
1631 bad_attr_list
->attr_page
= attr_page
;
1632 bad_attr_list
->attr_id
= attr_id
;
1637 len
-= sizeof(*pattr
);
1639 "osd_sense_attribute_identification"
1640 "attr_page=0x%x attr_id=0x%x\n",
1641 attr_page
, attr_id
);
1644 /*These are not legal for OSD*/
1645 case scsi_sense_field_replaceable_unit
:
1646 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1648 case scsi_sense_stream_commands
:
1649 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1651 case scsi_sense_block_commands
:
1652 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1654 case scsi_sense_ata_return
:
1655 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1658 if (ssd
->descriptor_type
<= scsi_sense_Reserved_last
)
1660 "scsi_sense Reserved descriptor (0x%x)",
1661 ssd
->descriptor_type
);
1664 "scsi_sense Vendor descriptor (0x%x)",
1665 ssd
->descriptor_type
);
1668 cur_descriptor
+= cur_len
;
1673 /* scsi sense is Empty, the request was never issued to target
1674 * linux return code might tell us what happened.
1676 if (or->async_error
== -ENOMEM
)
1677 osi
->osd_err_pri
= OSD_ERR_PRI_RESOURCE
;
1679 osi
->osd_err_pri
= OSD_ERR_PRI_UNREACHABLE
;
1680 ret
= or->async_error
;
1681 } else if (osi
->key
<= scsi_sk_recovered_error
) {
1682 osi
->osd_err_pri
= 0;
1684 } else if (osi
->additional_code
== scsi_invalid_field_in_cdb
) {
1685 if (osi
->cdb_field_offset
== OSD_CFO_STARTING_BYTE
) {
1686 osi
->osd_err_pri
= OSD_ERR_PRI_CLEAR_PAGES
;
1687 ret
= -EFAULT
; /* caller should recover from this */
1688 } else if (osi
->cdb_field_offset
== OSD_CFO_OBJECT_ID
) {
1689 osi
->osd_err_pri
= OSD_ERR_PRI_NOT_FOUND
;
1691 } else if (osi
->cdb_field_offset
== OSD_CFO_PERMISSIONS
) {
1692 osi
->osd_err_pri
= OSD_ERR_PRI_NO_ACCESS
;
1695 osi
->osd_err_pri
= OSD_ERR_PRI_BAD_CRED
;
1698 } else if (osi
->additional_code
== osd_quota_error
) {
1699 osi
->osd_err_pri
= OSD_ERR_PRI_NO_SPACE
;
1701 } else if (_is_osd_security_code(osi
->additional_code
)) {
1702 osi
->osd_err_pri
= OSD_ERR_PRI_BAD_CRED
;
1705 osi
->osd_err_pri
= OSD_ERR_PRI_EIO
;
1709 if (!or->out
.residual
)
1710 or->out
.residual
= or->out
.total_bytes
;
1711 if (!or->in
.residual
)
1712 or->in
.residual
= or->in
.total_bytes
;
1716 EXPORT_SYMBOL(osd_req_decode_sense_full
);
1719 * Implementation of osd_sec.h API
1720 * TODO: Move to a separate osd_sec.c file at a later stage.
1723 enum { OSD_SEC_CAP_V1_ALL_CAPS
=
1724 OSD_SEC_CAP_APPEND
| OSD_SEC_CAP_OBJ_MGMT
| OSD_SEC_CAP_REMOVE
|
1725 OSD_SEC_CAP_CREATE
| OSD_SEC_CAP_SET_ATTR
| OSD_SEC_CAP_GET_ATTR
|
1726 OSD_SEC_CAP_WRITE
| OSD_SEC_CAP_READ
| OSD_SEC_CAP_POL_SEC
|
1727 OSD_SEC_CAP_GLOBAL
| OSD_SEC_CAP_DEV_MGMT
1730 enum { OSD_SEC_CAP_V2_ALL_CAPS
=
1731 OSD_SEC_CAP_V1_ALL_CAPS
| OSD_SEC_CAP_QUERY
| OSD_SEC_CAP_M_OBJECT
1734 void osd_sec_init_nosec_doall_caps(void *caps
,
1735 const struct osd_obj_id
*obj
, bool is_collection
, const bool is_v1
)
1737 struct osd_capability
*cap
= caps
;
1741 if (likely(obj
->id
)) {
1742 if (unlikely(is_collection
)) {
1743 type
= OSD_SEC_OBJ_COLLECTION
;
1744 descriptor_type
= is_v1
? OSD_SEC_OBJ_DESC_OBJ
:
1745 OSD_SEC_OBJ_DESC_COL
;
1747 type
= OSD_SEC_OBJ_USER
;
1748 descriptor_type
= OSD_SEC_OBJ_DESC_OBJ
;
1750 WARN_ON(!obj
->partition
);
1752 type
= obj
->partition
? OSD_SEC_OBJ_PARTITION
:
1754 descriptor_type
= OSD_SEC_OBJ_DESC_PAR
;
1757 memset(cap
, 0, sizeof(*cap
));
1759 cap
->h
.format
= OSD_SEC_CAP_FORMAT_VER1
;
1760 cap
->h
.integrity_algorithm__key_version
= 0; /* MAKE_BYTE(0, 0); */
1761 cap
->h
.security_method
= OSD_SEC_NOSEC
;
1762 /* cap->expiration_time;
1764 cap->discriminator[42-30];
1765 cap->object_created_time; */
1766 cap
->h
.object_type
= type
;
1767 osd_sec_set_caps(&cap
->h
, OSD_SEC_CAP_V1_ALL_CAPS
);
1768 cap
->h
.object_descriptor_type
= descriptor_type
;
1769 cap
->od
.obj_desc
.policy_access_tag
= 0;
1770 cap
->od
.obj_desc
.allowed_partition_id
= cpu_to_be64(obj
->partition
);
1771 cap
->od
.obj_desc
.allowed_object_id
= cpu_to_be64(obj
->id
);
1773 EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps
);
1775 void osd_set_caps(struct osd_cdb
*cdb
, const void *caps
)
1777 bool is_ver1
= true;
1778 /* NOTE: They start at same address */
1779 memcpy(&cdb
->v1
.caps
, caps
, is_ver1
? OSDv1_CAP_LEN
: OSD_CAP_LEN
);
1782 bool osd_is_sec_alldata(struct osd_security_parameters
*sec_parms __unused
)
1787 void osd_sec_sign_cdb(struct osd_cdb
*ocdb __unused
, const u8
*cap_key __unused
)
1791 void osd_sec_sign_data(void *data_integ __unused
,
1792 struct bio
*bio __unused
, const u8
*cap_key __unused
)
1797 * Declared in osd_protocol.h
1798 * 4.12.5 Data-In and Data-Out buffer offsets
1799 * byte offset = mantissa * (2^(exponent+8))
1800 * Returns the smallest allowed encoded offset that contains given @offset
1801 * The actual encoded offset returned is @offset + *@padding.
1803 osd_cdb_offset
__osd_encode_offset(
1804 u64 offset
, unsigned *padding
, int min_shift
, int max_shift
)
1806 u64 try_offset
= -1, mod
, align
;
1807 osd_cdb_offset be32_offset
;
1814 for (shift
= min_shift
; shift
< max_shift
; ++shift
) {
1815 try_offset
= offset
>> shift
;
1816 if (try_offset
< (1 << OSD_OFFSET_MAX_BITS
))
1820 BUG_ON(shift
== max_shift
);
1823 mod
= offset
& (align
- 1);
1825 *padding
= align
- mod
;
1829 try_offset
|= ((shift
- 8) & 0xf) << 28;
1830 be32_offset
= cpu_to_be32((u32
)try_offset
);
1832 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1833 _LLU(offset
), _LLU(try_offset
& 0x0FFFFFFF), shift
,
1834 be32_offset
, *padding
);