26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include <asm/unaligned.h>
30 #include <scsi/scsi.h>
40 static int sbc_emulate_readcapacity(
struct se_cmd *
cmd)
43 unsigned long long blocks_long = dev->
transport->get_blocks(dev);
48 if (blocks_long >= 0x00000000ffffffff)
51 blocks = (
u32)blocks_long;
53 buf[0] = (blocks >> 24) & 0xff;
54 buf[1] = (blocks >> 16) & 0xff;
55 buf[2] = (blocks >> 8) & 0xff;
56 buf[3] = blocks & 0xff;
57 buf[4] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
58 buf[5] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
59 buf[6] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
60 buf[7] = dev->
se_sub_dev->se_dev_attrib.block_size & 0xff;
72 static int sbc_emulate_readcapacity_16(
struct se_cmd *cmd)
76 unsigned char buf[32];
77 unsigned long long blocks = dev->
transport->get_blocks(dev);
79 memset(buf, 0,
sizeof(buf));
80 buf[0] = (blocks >> 56) & 0xff;
81 buf[1] = (blocks >> 48) & 0xff;
82 buf[2] = (blocks >> 40) & 0xff;
83 buf[3] = (blocks >> 32) & 0xff;
84 buf[4] = (blocks >> 24) & 0xff;
85 buf[5] = (blocks >> 16) & 0xff;
86 buf[6] = (blocks >> 8) & 0xff;
87 buf[7] = blocks & 0xff;
88 buf[8] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
89 buf[9] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
90 buf[10] = (dev->
se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
91 buf[11] = dev->
se_sub_dev->se_dev_attrib.block_size & 0xff;
114 num_blocks = get_unaligned_be16(&cmd->
t_task_cdb[7]);
127 return cmd->
se_dev->transport->get_blocks(cmd->
se_dev) -
132 static int sbc_emulate_verify(
struct se_cmd *cmd)
138 static int sbc_emulate_noop(
struct se_cmd *cmd)
146 return cmd->
se_dev->se_sub_dev->se_dev_attrib.block_size *
sectors;
149 static int sbc_check_valid_sectors(
struct se_cmd *cmd)
152 unsigned long long end_lba;
156 end_lba = dev->
transport->get_blocks(dev) + 1;
159 pr_err(
"target: lba %llu, sectors %u exceeds end lba %llu\n",
167 static inline u32 transport_get_sectors_6(
unsigned char *
cdb)
177 return cdb[4] ? : 256;
180 static inline u32 transport_get_sectors_10(
unsigned char *
cdb)
182 return (
u32)(cdb[7] << 8) + cdb[8];
185 static inline u32 transport_get_sectors_12(
unsigned char *cdb)
187 return (
u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
190 static inline u32 transport_get_sectors_16(
unsigned char *cdb)
192 return (
u32)(cdb[10] << 24) + (cdb[11] << 16) +
193 (cdb[12] << 8) + cdb[13];
199 static inline u32 transport_get_sectors_32(
unsigned char *cdb)
201 return (
u32)(cdb[28] << 24) + (cdb[29] << 16) +
202 (cdb[30] << 8) + cdb[31];
206 static inline u32 transport_lba_21(
unsigned char *cdb)
208 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
211 static inline u32 transport_lba_32(
unsigned char *cdb)
213 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
216 static inline unsigned long long transport_lba_64(
unsigned char *cdb)
218 unsigned int __v1, __v2;
220 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
221 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
223 return ((
unsigned long long)__v2) | (
unsigned long long)__v1 << 32;
229 static inline unsigned long long transport_lba_64_ext(
unsigned char *cdb)
231 unsigned int __v1, __v2;
233 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
234 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
236 return ((
unsigned long long)__v2) | (
unsigned long long)__v1 << 32;
239 static int sbc_write_same_supported(
struct se_device *dev,
240 unsigned char *
flags)
242 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
243 pr_err(
"WRITE_SAME PBDATA and LBDATA"
244 " bits not supported for Block Discard"
253 if (!(flags[0] & 0x08)) {
254 pr_err(
"WRITE_SAME w/o UNMAP bit not"
255 " supported for Block Discard Emulation\n");
262 static void xdreadwrite_callback(
struct se_cmd *cmd)
282 pr_err(
"Unable to allocate xor_callback buf\n");
305 for (i = 0; i < sg->
length; i++)
306 *(addr + sg->
offset + i) ^= *(buf + offset +
i);
327 sectors = transport_get_sectors_6(cdb);
333 sectors = transport_get_sectors_10(cdb);
339 sectors = transport_get_sectors_12(cdb);
345 sectors = transport_get_sectors_16(cdb);
351 sectors = transport_get_sectors_6(cdb);
358 sectors = transport_get_sectors_10(cdb);
366 sectors = transport_get_sectors_12(cdb);
374 sectors = transport_get_sectors_16(cdb);
384 goto out_invalid_cdb_field;
385 sectors = transport_get_sectors_10(cdb);
400 u16 service_action = get_unaligned_be16(&cdb[8]);
401 switch (service_action) {
403 sectors = transport_get_sectors_32(cdb);
423 goto out_unsupported_cdb;
425 sectors = transport_get_sectors_32(cdb);
427 pr_err(
"WSNZ=1, WRITE_SAME w/sectors=0 not"
429 goto out_invalid_cdb_field;
432 size = sbc_get_size(cmd, 1);
435 if (sbc_write_same_supported(dev, &cdb[10]) < 0)
436 goto out_unsupported_cdb;
440 pr_err(
"VARIABLE_LENGTH_CMD service action"
441 " 0x%04x not supported\n", service_action);
442 goto out_unsupported_cdb;
456 pr_err(
"Unsupported SA: 0x%02x\n",
458 goto out_invalid_cdb_field;
460 size = (cdb[10] << 24) | (cdb[11] << 16) |
461 (cdb[12] << 8) | cdb[13];
466 goto out_unsupported_cdb;
472 sectors = transport_get_sectors_10(cdb);
475 sectors = transport_get_sectors_16(cdb);
479 size = sbc_get_size(cmd, sectors);
486 if (sbc_check_valid_sectors(cmd) < 0)
487 goto out_invalid_cdb_field;
493 goto out_unsupported_cdb;
495 size = get_unaligned_be16(&cdb[7]);
500 goto out_unsupported_cdb;
502 sectors = transport_get_sectors_16(cdb);
504 pr_err(
"WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
505 goto out_invalid_cdb_field;
508 size = sbc_get_size(cmd, 1);
511 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
512 goto out_unsupported_cdb;
517 goto out_unsupported_cdb;
519 sectors = transport_get_sectors_10(cdb);
521 pr_err(
"WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
522 goto out_invalid_cdb_field;
525 size = sbc_get_size(cmd, 1);
532 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
533 goto out_unsupported_cdb;
560 goto out_unsupported_cdb;
563 unsigned long long end_lba;
567 " big sectors %u exceeds fabric_max_sectors:"
568 " %u\n", cdb[0], sectors,
570 goto out_invalid_cdb_field;
574 " big sectors %u exceeds backend hw_max_sectors:"
575 " %u\n", cdb[0], sectors,
577 goto out_invalid_cdb_field;
580 end_lba = dev->
transport->get_blocks(dev) + 1;
582 pr_err(
"cmd exceeds last lba %llu "
583 "(lba %llu, sectors %u)\n",
585 goto out_invalid_cdb_field;
588 size = sbc_get_size(cmd, sectors);
601 out_invalid_cdb_field: