42 #include <linux/slab.h>
43 #include <linux/module.h>
55 # define __unused __attribute__((unused))
64 static inline void build_test(
void)
74 return osd_req_is_ver1(or) ?
"OSD1" :
"OSD2";
77 #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
79 static int _osd_get_print_system_info(
struct osd_dev *od,
97 void *iter =
NULL, *pFirst;
115 OSD_ERR(
"Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
125 OSD_INFO(
"VENDOR_IDENTIFICATION [%s]\n",
129 OSD_INFO(
"PRODUCT_IDENTIFICATION [%s]\n",
137 OSD_INFO(
"PRODUCT_REVISION_LEVEL [%u]\n",
141 OSD_INFO(
"PRODUCT_SERIAL_NUMBER [%s]\n",
152 pFirst = get_attrs[a++].
val_ptr;
153 OSD_INFO(
"TOTAL_CAPACITY [0x%llx]\n",
156 pFirst = get_attrs[a++].
val_ptr;
157 OSD_INFO(
"USED_CAPACITY [0x%llx]\n",
160 pFirst = get_attrs[a++].
val_ptr;
161 OSD_INFO(
"NUMBER_OF_PARTITIONS [%llu]\n",
168 pFirst = get_attrs[a++].
val_ptr;
169 OSD_INFO(
"CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
170 ((
char *)pFirst)[0], ((
char *)pFirst)[1],
171 ((
char *)pFirst)[2], ((
char *)pFirst)[3],
172 ((
char *)pFirst)[4], ((
char *)pFirst)[5]);
175 unsigned len = get_attrs[
a].
len;
176 char sid_dump[32*4 + 2];
179 sid_dump,
sizeof(sid_dump),
true);
181 " [%s]\n", len, sid_dump);
184 OSD_ERR(
"OSD Target error: OSD_SYSTEM_ID too long(%d). "
185 "device idetification might not work\n", len);
203 ret = _osd_get_print_system_info(od, caps, odi);
207 ret = _osd_get_print_system_info(od, caps, odi);
219 static unsigned _osd_req_alist_elem_size(
struct osd_request *
or,
unsigned len)
221 return osd_req_is_ver1(or) ?
222 osdv1_attr_list_elem_size(len) :
223 osdv2_attr_list_elem_size(len);
226 static void _osd_req_alist_elem_encode(
struct osd_request *or,
227 void *attr_last,
const struct osd_attr *oa)
229 if (osd_req_is_ver1(or)) {
246 static int _osd_req_alist_elem_decode(
struct osd_request *or,
250 if (osd_req_is_ver1(or)) {
253 if (max_bytes <
sizeof(*attr))
257 inc = _osd_req_alist_elem_size(or, oa->
len);
273 if (max_bytes <
sizeof(*attr))
277 inc = _osd_req_alist_elem_size(or, oa->
len);
296 return osd_req_is_ver1(or) ?
297 osdv1_list_size(list_head) :
298 osdv2_list_size(list_head);
301 static unsigned _osd_req_sizeof_alist_header(
struct osd_request *or)
303 return osd_req_is_ver1(or) ?
305 sizeof(struct osdv2_attributes_list_header);
308 static
void _osd_req_set_alist_type(struct osd_request *or,
309 void *list, int list_type)
311 if (osd_req_is_ver1(or)) {
314 memset(attr_list, 0,
sizeof(*attr_list));
315 attr_list->
type = list_type;
319 memset(attr_list, 0,
sizeof(*attr_list));
320 attr_list->
type = list_type;
324 static bool _osd_req_is_alist_type(
struct osd_request *or,
325 void *
list,
int list_type)
330 if (osd_req_is_ver1(or)) {
333 return attr_list->
type == list_type;
337 return attr_list->
type == list_type;
342 static void _osd_req_encode_olist(
struct osd_request *or,
347 if (osd_req_is_ver1(or)) {
360 osd_req_is_ver1(or) ?
370 if (osd_req_is_ver1(or))
378 memset(osdd, 0,
sizeof(*osdd));
381 #ifdef OSD_VER1_SUPPORT
401 or = kzalloc(
sizeof(*or), gfp);
405 static void _osd_request_free(
struct osd_request *or)
414 or = _osd_request_alloc(gfp);
428 struct _osd_req_data_segment *
seg)
430 if (!seg->buff || !seg->alloc_size)
438 static void _put_request(
struct request *
rq)
458 _put_request(rq->next_rq);
470 _osd_request_free(or);
481 or->
out.residual = or->
out.req->resid_len;
483 or->
in.residual = or->
in.req->resid_len;
490 _set_error_resid(or, or->
request, error);
495 static void osd_request_async_done(
struct request *req,
int error)
499 _set_error_resid(or, req, error);
524 osd_request_async_done);
532 static int _osd_realloc_seg(
struct osd_request *or,
533 struct _osd_req_data_segment *seg,
unsigned max_bytes)
537 if (seg->alloc_size >= max_bytes)
542 OSD_ERR(
"Failed to Realloc %d-bytes was-%d\n", max_bytes,
547 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
555 OSD_DEBUG(
"total_bytes=%d\n", total_bytes);
556 return _osd_realloc_seg(or, &or->
cdb_cont, total_bytes);
559 static int _alloc_set_attr_list(
struct osd_request *or,
560 const struct osd_attr *oa,
unsigned nelem,
unsigned add_bytes)
565 total_bytes += _osd_req_alist_elem_size(or, oa->
len);
567 OSD_DEBUG(
"total_bytes=%d\n", total_bytes);
568 return _osd_realloc_seg(or, &or->
set_attr, total_bytes);
571 static int _alloc_get_attr_desc(
struct osd_request *or,
unsigned max_bytes)
573 OSD_DEBUG(
"total_bytes=%d\n", max_bytes);
574 return _osd_realloc_seg(or, &or->
enc_get_attr, max_bytes);
577 static int _alloc_get_attr_list(
struct osd_request *or)
587 static void _osdv1_req_encode_common(
struct osd_request *or,
604 ocdb->
h.varlen_cdb.service_action = act;
612 static void _osdv2_req_encode_common(
struct osd_request *or,
621 ocdb->
h.varlen_cdb.service_action = act;
629 static void _osd_req_encode_common(
struct osd_request *or,
632 if (osd_req_is_ver1(or))
633 _osdv1_req_encode_common(or, act, obj, offset, len);
635 _osdv2_req_encode_common(or, act, obj, offset, len);
646 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
658 static void _osd_req_encode_flush(
struct osd_request *or,
669 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
670 _osd_req_encode_flush(or, op);
681 static void _osd_req_encode_partition(
struct osd_request *or,
689 _osd_req_encode_common(or, act, &par, 0, 0);
694 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
700 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
708 static int _osd_req_list_objects(
struct osd_request *or,
713 u64 len = nelem *
sizeof(
osd_id) +
sizeof(*list);
716 _osd_req_encode_common(or, action, obj, (
u64)initial_id, len);
719 _osd_req_encode_olist(or, list);
724 OSD_ERR(
"!!! Failed to allocate list_objects BIO\n");
730 or->
in.total_bytes = bio->bi_size;
757 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
765 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
766 _osd_req_encode_flush(or, op);
782 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
783 initial_id, list, nelem);
792 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
793 _osd_req_encode_flush(or, op);
805 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
811 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
822 struct bio *bio,
u64 len)
824 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
828 or->
out.total_bytes = len;
860 if (
unlikely(osd_req_is_ver1(or) && (offset || len))) {
861 OSD_DEBUG(
"OSD Ver1 flush on specific range ignored\n");
866 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
867 _osd_req_encode_flush(or, op);
873 struct bio *bio,
u64 len)
875 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
879 or->
in.total_bytes = len;
897 static int _add_sg_continuation_descriptor(
struct osd_request *or,
905 oscd_size =
sizeof(*oscd) + numentries *
sizeof(oscd->
entries[0]);
915 ret = _alloc_cdb_cont(or, or->
cdb_cont.total_bytes + oscd_size);
921 oscd->
hdr.pad_length = 0;
926 for (i = 0; i < numentries; i++) {
929 *len += sglist[
i].
len;
932 or->
cdb_cont.total_bytes += oscd_size;
933 OSD_DEBUG(
"total_bytes=%d oscd_size=%d numentries=%d\n",
934 or->
cdb_cont.total_bytes, oscd_size, numentries);
938 static int _osd_req_finalize_cdb_cont(
struct osd_request *or,
const u8 *cap_key)
973 bio->bi_next = or->
out.bio;
987 const struct osd_obj_id *obj,
struct bio *bio,
991 int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len);
1005 const struct osd_obj_id *obj,
struct bio *bio,
1006 const struct osd_sg_entry *sglist,
unsigned numentries)
1012 if (numentries > 1) {
1014 ret = _add_sg_continuation_descriptor(or, sglist, numentries,
1039 static struct bio *_create_sg_bios(
struct osd_request *or,
1040 void **buff,
const struct osd_sg_entry *sglist,
unsigned numentries)
1048 OSD_DEBUG(
"Faild to allocate BIO size=%u\n", numentries);
1052 for (i = 0; i < numentries; i++) {
1055 unsigned len = sglist[
i].
len;
1061 OSD_DEBUG(
"bio_add_pc_page len(%d) != added_len(%d)\n",
1073 const struct osd_sg_entry *sglist,
unsigned numentries)
1075 struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
1077 return PTR_ERR(bio);
1088 const struct osd_sg_entry *sglist,
unsigned numentries)
1090 struct bio *bio = _create_sg_bios(or, buff, sglist, numentries);
1092 return PTR_ERR(bio);
1105 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
1112 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
1121 const struct osd_attr *oa,
unsigned nelem)
1123 unsigned total_bytes = or->
set_attr.total_bytes;
1135 total_bytes = _osd_req_sizeof_alist_header(or);
1136 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1139 _osd_req_set_alist_type(or, or->
set_attr.buff,
1145 unsigned elem_size = _osd_req_alist_elem_size(or, oa->
len);
1147 total_bytes += elem_size;
1149 or->
set_attr.total_bytes = total_bytes - elem_size;
1150 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1157 _osd_req_alist_elem_encode(or, attr_last, oa);
1159 attr_last += elem_size;
1168 static int _req_append_segment(
struct osd_request *or,
1169 unsigned padding,
struct _osd_req_data_segment *seg,
1170 struct _osd_req_data_segment *last_seg,
struct _osd_io_info *
io)
1178 (padding <= last_seg->alloc_size - last_seg->total_bytes))
1179 pad_buff = last_seg->buff + last_seg->total_bytes;
1181 pad_buff = io->pad_buff;
1190 ret =
blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
1195 io->total_bytes += seg->total_bytes;
1196 OSD_DEBUG(
"padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
1201 static int _osd_req_finalize_set_attr_list(
struct osd_request *or)
1214 osd_req_encode_offset(or, or->
out.total_bytes, &padding);
1216 ret = _req_append_segment(or, padding, &or->
set_attr,
1217 or->
out.last_seg, &or->
out);
1226 const struct osd_attr *oa,
unsigned nelem)
1241 or->
get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
1247 total_bytes = _osd_req_sizeof_alist_header(or);
1248 max_bytes = total_bytes +
1250 ret = _alloc_get_attr_desc(or, max_bytes);
1261 const unsigned cur_size =
sizeof(*attrid);
1263 total_bytes += cur_size;
1266 ret = _alloc_get_attr_desc(or,
1267 total_bytes + nelem *
sizeof(*attrid));
1278 attr_last += cur_size;
1282 _osd_req_alist_elem_size(or, oa->
len);
1289 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1291 or->
get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1293 (or->
enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1300 static int _osd_req_finalize_get_attr_list(
struct osd_request *or)
1303 unsigned out_padding;
1304 unsigned in_padding;
1313 ret = _alloc_get_attr_list(or);
1323 osd_req_encode_offset(or, or->
out.total_bytes, &out_padding);
1325 ret = _req_append_segment(or, out_padding, &or->
enc_get_attr,
1326 or->
out.last_seg, &or->
out);
1337 osd_req_encode_offset(or, or->
in.total_bytes, &in_padding);
1339 ret = _req_append_segment(or, in_padding, &or->
get_attr,
NULL,
1349 struct osd_attr *oa,
int *nelem,
void **iterator)
1351 unsigned cur_bytes, returned_bytes;
1353 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1356 if (!_osd_req_is_alist_type(or, or->
get_attr.buff,
1367 BUG_ON((*iterator < or->get_attr.buff) ||
1370 cur_bytes = (*iterator - or->
get_attr.buff) - sizeof_attr_list;
1371 returned_bytes = or->
get_attr.total_bytes;
1373 cur_bytes = sizeof_attr_list;
1374 returned_bytes = _osd_req_alist_size(or, or->
get_attr.buff) +
1377 cur_p = or->
get_attr.buff + sizeof_attr_list;
1379 if (returned_bytes > or->
get_attr.alloc_size) {
1380 OSD_DEBUG(
"target report: space was not big enough! "
1381 "Allocate=%u Needed=%u\n",
1383 returned_bytes + sizeof_attr_list);
1386 or->
get_attr.alloc_size - sizeof_attr_list;
1388 or->
get_attr.total_bytes = returned_bytes;
1391 for (n = 0; (n < *
nelem) && (cur_bytes < returned_bytes); ++
n) {
1392 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1393 returned_bytes - cur_bytes);
1396 OSD_ERR(
"BAD FOOD from target. list not valid!"
1398 cur_bytes, returned_bytes, n);
1400 cur_bytes = returned_bytes;
1409 *iterator = (returned_bytes - cur_bytes) ? cur_p :
NULL;
1411 return returned_bytes - cur_bytes;
1420 u32 page_id,
void *attar_page,
unsigned max_page_len,
1421 const struct osd_attr *set_one_attr)
1433 or->
get_attr.total_bytes = max_page_len;
1438 if (!set_one_attr || !set_one_attr->
attr_page)
1451 static int _osd_req_finalize_attr_page(
struct osd_request *or)
1454 unsigned in_padding, out_padding;
1459 osd_req_encode_offset(or, or->
in.total_bytes, &in_padding);
1461 ret = _req_append_segment(or, in_padding, &or->
get_attr,
NULL,
1471 osd_req_encode_offset(or, or->
out.total_bytes, &out_padding);
1473 ret = _req_append_segment(or, out_padding, &or->
set_attr,
NULL,
1478 static inline void osd_sec_parms_set_out_offset(
bool is_v1,
1482 sec_parms->
v1.data_out_integrity_check_offset =
offset;
1484 sec_parms->
v2.data_out_integrity_check_offset =
offset;
1487 static inline void osd_sec_parms_set_in_offset(
bool is_v1,
1491 sec_parms->
v1.data_in_integrity_check_offset =
offset;
1493 sec_parms->
v2.data_in_integrity_check_offset =
offset;
1496 static int _osd_req_finalize_data_integrity(
struct osd_request *or,
1497 bool has_in,
bool has_out,
struct bio *out_data_bio,
u64 out_data_bytes,
1507 struct _osd_req_data_segment seg = {
1519 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1520 osd_req_encode_offset(or, or->
out.total_bytes, &pad));
1522 ret = _req_append_segment(or, pad, &seg, or->
out.last_seg,
1534 struct _osd_req_data_segment seg = {
1540 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1541 osd_req_encode_offset(or, or->
in.total_bytes, &pad));
1543 ret = _req_append_segment(or, pad, &seg, or->
in.last_seg,
1573 static int _init_blk_request(
struct osd_request *or,
1574 bool has_in,
bool has_out)
1582 req = _make_request(q, has_out, has_out ? &or->
out : &or->
in, flags);
1589 req->cmd_type = REQ_TYPE_BLOCK_PC;
1594 req->sense = or->
sense;
1601 req = _make_request(q,
false, &or->
in, flags);
1603 OSD_DEBUG(
"blk_get_request for bidi failed\n");
1607 req->cmd_type = REQ_TYPE_BLOCK_PC;
1615 OSD_DEBUG(
"or=%p has_in=%d has_out=%d => %d, %p\n",
1616 or, has_in, has_out, ret, or->
request);
1624 bool has_in, has_out;
1626 struct bio *out_data_bio = or->
out.bio;
1627 u64 out_data_bytes = or->
out.total_bytes;
1641 has_in = or->
in.bio || or->
get_attr.total_bytes;
1642 has_out = or->
out.bio || or->
cdb_cont.total_bytes ||
1645 ret = _osd_req_finalize_cdb_cont(or, cap_key);
1647 OSD_DEBUG(
"_osd_req_finalize_cdb_cont failed\n");
1650 ret = _init_blk_request(or, has_in, has_out);
1652 OSD_DEBUG(
"_init_blk_request failed\n");
1663 ret = _osd_req_finalize_attr_page(or);
1665 OSD_DEBUG(
"_osd_req_finalize_attr_page failed\n");
1673 ret = _osd_req_finalize_set_attr_list(or);
1675 OSD_DEBUG(
"_osd_req_finalize_set_attr_list failed\n");
1679 ret = _osd_req_finalize_get_attr_list(or);
1681 OSD_DEBUG(
"_osd_req_finalize_get_attr_list failed\n");
1686 ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
1687 out_data_bio, out_data_bytes,
1695 or->
request->cmd_len = _osd_req_cdb_len(or);
1701 static bool _is_osd_security_code(
int code)
1710 #define OSD_SENSE_PRINT1(fmt, a...) \
1712 if (__cur_sense_need_output) \
1713 OSD_ERR(fmt, ##a); \
1716 #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1720 struct osd_obj_id *bad_obj_list __unused,
int max_obj __unused,
1721 struct osd_attr *bad_attr_list,
int max_attr)
1726 void *cur_descriptor;
1727 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1728 const bool __cur_sense_need_output =
false;
1730 bool __cur_sense_need_output = !silent;
1737 osi = osi ? : &local_osi;
1738 memset(osi, 0,
sizeof(*osi));
1742 if ((sense_len < (
int)
sizeof(*ssdb) || !ssdb->
sense_key)) {
1743 OSD_ERR(
"Block-layer returned error(0x%x) but "
1744 "sense_len(%u) || key(%d) is empty\n",
1750 OSD_ERR(
"Unrecognized scsi sense: rcode=%x length=%d\n",
1759 #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1760 if (__cur_sense_need_output)
1764 "additional_code=0x%x async_error=%d errors=0x%x\n",
1765 osi->
key, original_sense_len, sense_len,
1769 if (original_sense_len < sense_len)
1770 sense_len = original_sense_len;
1772 cur_descriptor = ssdb->
ssd;
1773 sense_len -=
sizeof(*ssdb);
1774 while (sense_len > 0) {
1778 sense_len -= cur_len;
1788 *sscd = cur_descriptor;
1793 "command_specific_information 0x%llx \n",
1800 *ssks = cur_descriptor;
1804 "sense_key_specific_information %u"
1805 "sksv_cd_bpv_bp (0x%x)\n",
1812 *osidd = cur_descriptor;
1821 "object_identification pid=0x%llx oid=0x%llx\n",
1824 "not_initiated_bits(%x) "
1825 "completed_command_bits(%x)\n",
1833 *osricd = cur_descriptor;
1834 const unsigned len =
1836 char key_dump[len*4 + 2];
1839 32, 1, key_dump,
sizeof(key_dump),
true);
1845 *osadd = cur_descriptor;
1846 unsigned len =
min(cur_len, sense_len);
1847 struct osd_sense_attr *pattr = osadd->
sense_attrs;
1849 while (len >=
sizeof(*pattr)) {
1853 if (!osi->
attr.attr_page) {
1854 osi->
attr.attr_page = attr_page;
1858 if (bad_attr_list && max_attr) {
1865 len -=
sizeof(*pattr);
1867 "osd_sense_attribute_identification"
1868 "attr_page=0x%x attr_id=0x%x\n",
1869 attr_page, attr_id);
1888 "scsi_sense Reserved descriptor (0x%x)",
1892 "scsi_sense Vendor descriptor (0x%x)",
1896 cur_descriptor += cur_len;
1937 if (!or->
out.residual)
1938 or->
out.residual = or->
out.total_bytes;
1939 if (!or->
in.residual)
1940 or->
in.residual = or->
in.total_bytes;
1963 const struct osd_obj_id *obj,
bool is_collection,
const bool is_v1)
1985 memset(cap, 0,
sizeof(*cap));
1988 cap->
h.integrity_algorithm__key_version = 0;
1994 cap->
h.object_type =
type;
1996 cap->
h.object_descriptor_type = descriptor_type;
1997 cap->
od.obj_desc.policy_access_tag = 0;
2008 bool is_ver1 =
true;
2023 struct bio *bio __unused,
const u8 *cap_key __unused)
2035 u64 offset,
unsigned *padding,
int min_shift,
int max_shift)
2045 for (shift = min_shift; shift < max_shift; ++shift) {
2046 try_offset = offset >> shift;
2051 BUG_ON(shift == max_shift);
2054 mod = offset & (align - 1);
2056 *padding = align -
mod;
2060 try_offset |= ((shift - 8) & 0xf) << 28;
2063 OSD_DEBUG(
"offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
2064 _LLU(offset),
_LLU(try_offset & 0x0FFFFFFF), shift,
2065 be32_offset, *padding);