2 * SCSI Block Commands (SBC) parsing and emulation.
4 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
5 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
6 * Copyright (c) 2007-2010 Rising Tide Systems
7 * Copyright (c) 2008-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/ratelimit.h>
29 #include <asm/unaligned.h>
30 #include <scsi/scsi.h>
32 #include <target/target_core_base.h>
33 #include <target/target_core_backend.h>
34 #include <target/target_core_fabric.h>
36 #include "target_core_internal.h"
37 #include "target_core_ua.h"
40 static int sbc_emulate_readcapacity(struct se_cmd *cmd)
42 struct se_device *dev = cmd->se_dev;
44 unsigned long long blocks_long = dev->transport->get_blocks(dev);
47 if (blocks_long >= 0x00000000ffffffff)
50 blocks = (u32)blocks_long;
52 buf = transport_kmap_data_sg(cmd);
54 buf[0] = (blocks >> 24) & 0xff;
55 buf[1] = (blocks >> 16) & 0xff;
56 buf[2] = (blocks >> 8) & 0xff;
57 buf[3] = blocks & 0xff;
58 buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
59 buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
60 buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
61 buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
63 transport_kunmap_data_sg(cmd);
65 target_complete_cmd(cmd, GOOD);
69 static int sbc_emulate_readcapacity_16(struct se_cmd *cmd)
71 struct se_device *dev = cmd->se_dev;
73 unsigned long long blocks = dev->transport->get_blocks(dev);
75 buf = transport_kmap_data_sg(cmd);
77 buf[0] = (blocks >> 56) & 0xff;
78 buf[1] = (blocks >> 48) & 0xff;
79 buf[2] = (blocks >> 40) & 0xff;
80 buf[3] = (blocks >> 32) & 0xff;
81 buf[4] = (blocks >> 24) & 0xff;
82 buf[5] = (blocks >> 16) & 0xff;
83 buf[6] = (blocks >> 8) & 0xff;
84 buf[7] = blocks & 0xff;
85 buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
86 buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
87 buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
88 buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
90 * Set Thin Provisioning Enable bit following sbc3r22 in section
91 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
93 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
96 transport_kunmap_data_sg(cmd);
98 target_complete_cmd(cmd, GOOD);
103 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
104 * Note this is not used for TCM/pSCSI passthrough
106 static int sbc_emulate_unmap(struct se_cmd *cmd)
108 struct se_device *dev = cmd->se_dev;
109 unsigned char *buf, *ptr = NULL;
110 unsigned char *cdb = &cmd->t_task_cdb[0];
112 unsigned int size = cmd->data_length, range;
114 unsigned short dl, bd_dl;
116 if (!dev->transport->do_discard) {
117 pr_err("UNMAP emulation not supported for: %s\n",
118 dev->transport->name);
119 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
123 /* First UNMAP block descriptor starts at 8 byte offset */
126 dl = get_unaligned_be16(&cdb[0]);
127 bd_dl = get_unaligned_be16(&cdb[2]);
129 buf = transport_kmap_data_sg(cmd);
132 pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
133 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
136 lba = get_unaligned_be64(&ptr[0]);
137 range = get_unaligned_be32(&ptr[8]);
138 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
139 (unsigned long long)lba, range);
141 ret = dev->transport->do_discard(dev, lba, range);
143 pr_err("blkdev_issue_discard() failed: %d\n",
153 transport_kunmap_data_sg(cmd);
155 target_complete_cmd(cmd, GOOD);
160 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
161 * Note this is not used for TCM/pSCSI passthrough
163 static int sbc_emulate_write_same(struct se_cmd *cmd)
165 struct se_device *dev = cmd->se_dev;
167 sector_t lba = cmd->t_task_lba;
171 if (!dev->transport->do_discard) {
172 pr_err("WRITE_SAME emulation not supported"
173 " for: %s\n", dev->transport->name);
174 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
178 if (cmd->t_task_cdb[0] == WRITE_SAME)
179 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
180 else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
181 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
182 else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
183 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
186 * Use the explicit range when non zero is supplied, otherwise calculate
187 * the remaining range based on ->get_blocks() - starting LBA.
192 range = (dev->transport->get_blocks(dev) - lba) + 1;
194 pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
195 (unsigned long long)lba, (unsigned long long)range);
197 ret = dev->transport->do_discard(dev, lba, range);
199 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
203 target_complete_cmd(cmd, GOOD);
207 static int sbc_emulate_synchronize_cache(struct se_cmd *cmd)
209 if (!cmd->se_dev->transport->do_sync_cache) {
210 pr_err("SYNCHRONIZE_CACHE emulation not supported"
211 " for: %s\n", cmd->se_dev->transport->name);
212 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
216 cmd->se_dev->transport->do_sync_cache(cmd);
220 static int sbc_emulate_verify(struct se_cmd *cmd)
222 target_complete_cmd(cmd, GOOD);
226 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
228 return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors;
231 static int sbc_check_valid_sectors(struct se_cmd *cmd)
233 struct se_device *dev = cmd->se_dev;
234 unsigned long long end_lba;
237 sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size;
238 end_lba = dev->transport->get_blocks(dev) + 1;
240 if (cmd->t_task_lba + sectors > end_lba) {
241 pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
242 cmd->t_task_lba, sectors, end_lba);
249 static inline u32 transport_get_sectors_6(unsigned char *cdb)
252 * Use 8-bit sector value. SBC-3 says:
254 * A TRANSFER LENGTH field set to zero specifies that 256
255 * logical blocks shall be written. Any other value
256 * specifies the number of logical blocks that shall be
259 return cdb[4] ? : 256;
262 static inline u32 transport_get_sectors_10(unsigned char *cdb)
264 return (u32)(cdb[7] << 8) + cdb[8];
267 static inline u32 transport_get_sectors_12(unsigned char *cdb)
269 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
272 static inline u32 transport_get_sectors_16(unsigned char *cdb)
274 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
275 (cdb[12] << 8) + cdb[13];
279 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
281 static inline u32 transport_get_sectors_32(unsigned char *cdb)
283 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
284 (cdb[30] << 8) + cdb[31];
288 static inline u32 transport_lba_21(unsigned char *cdb)
290 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
293 static inline u32 transport_lba_32(unsigned char *cdb)
295 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
298 static inline unsigned long long transport_lba_64(unsigned char *cdb)
300 unsigned int __v1, __v2;
302 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
303 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
305 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
309 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
311 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
313 unsigned int __v1, __v2;
315 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
316 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
318 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
321 static int sbc_write_same_supported(struct se_device *dev,
322 unsigned char *flags)
324 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
325 pr_err("WRITE_SAME PBDATA and LBDATA"
326 " bits not supported for Block Discard"
332 * Currently for the emulated case we only accept
333 * tpws with the UNMAP=1 bit set.
335 if (!(flags[0] & 0x08)) {
336 pr_err("WRITE_SAME w/o UNMAP bit not"
337 " supported for Block Discard Emulation\n");
344 static void xdreadwrite_callback(struct se_cmd *cmd)
346 unsigned char *buf, *addr;
347 struct scatterlist *sg;
352 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
354 * 1) read the specified logical block(s);
355 * 2) transfer logical blocks from the data-out buffer;
356 * 3) XOR the logical blocks transferred from the data-out buffer with
357 * the logical blocks read, storing the resulting XOR data in a buffer;
358 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
359 * blocks transferred from the data-out buffer; and
360 * 5) transfer the resulting XOR data to the data-in buffer.
362 buf = kmalloc(cmd->data_length, GFP_KERNEL);
364 pr_err("Unable to allocate xor_callback buf\n");
368 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
369 * into the locally allocated *buf
371 sg_copy_to_buffer(cmd->t_data_sg,
377 * Now perform the XOR against the BIDI read memory located at
378 * cmd->t_mem_bidi_list
382 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
383 addr = kmap_atomic(sg_page(sg));
387 for (i = 0; i < sg->length; i++)
388 *(addr + sg->offset + i) ^= *(buf + offset + i);
390 offset += sg->length;
398 int sbc_parse_cdb(struct se_cmd *cmd)
400 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
401 struct se_device *dev = cmd->se_dev;
402 unsigned char *cdb = cmd->t_task_cdb;
409 sectors = transport_get_sectors_6(cdb);
410 cmd->t_task_lba = transport_lba_21(cdb);
411 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
414 sectors = transport_get_sectors_10(cdb);
415 cmd->t_task_lba = transport_lba_32(cdb);
416 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
419 sectors = transport_get_sectors_12(cdb);
420 cmd->t_task_lba = transport_lba_32(cdb);
421 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
424 sectors = transport_get_sectors_16(cdb);
425 cmd->t_task_lba = transport_lba_64(cdb);
426 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
429 sectors = transport_get_sectors_6(cdb);
430 cmd->t_task_lba = transport_lba_21(cdb);
431 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
435 sectors = transport_get_sectors_10(cdb);
436 cmd->t_task_lba = transport_lba_32(cdb);
438 cmd->se_cmd_flags |= SCF_FUA;
439 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
442 sectors = transport_get_sectors_12(cdb);
443 cmd->t_task_lba = transport_lba_32(cdb);
445 cmd->se_cmd_flags |= SCF_FUA;
446 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
449 sectors = transport_get_sectors_16(cdb);
450 cmd->t_task_lba = transport_lba_64(cdb);
452 cmd->se_cmd_flags |= SCF_FUA;
453 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
456 if ((cmd->data_direction != DMA_TO_DEVICE) ||
457 !(cmd->se_cmd_flags & SCF_BIDI))
458 goto out_invalid_cdb_field;
459 sectors = transport_get_sectors_10(cdb);
461 cmd->t_task_lba = transport_lba_32(cdb);
462 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
465 * Setup BIDI XOR callback to be run after I/O completion.
467 cmd->transport_complete_callback = &xdreadwrite_callback;
469 cmd->se_cmd_flags |= SCF_FUA;
471 case VARIABLE_LENGTH_CMD:
473 u16 service_action = get_unaligned_be16(&cdb[8]);
474 switch (service_action) {
476 sectors = transport_get_sectors_32(cdb);
479 * Use WRITE_32 and READ_32 opcodes for the emulated
480 * XDWRITE_READ_32 logic.
482 cmd->t_task_lba = transport_lba_64_ext(cdb);
483 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
486 * Setup BIDI XOR callback to be run during after I/O
489 cmd->transport_complete_callback = &xdreadwrite_callback;
491 cmd->se_cmd_flags |= SCF_FUA;
494 sectors = transport_get_sectors_32(cdb);
496 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
498 goto out_invalid_cdb_field;
501 size = sbc_get_size(cmd, 1);
502 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
504 if (sbc_write_same_supported(dev, &cdb[10]) < 0)
505 goto out_unsupported_cdb;
506 cmd->execute_cmd = sbc_emulate_write_same;
509 pr_err("VARIABLE_LENGTH_CMD service action"
510 " 0x%04x not supported\n", service_action);
511 goto out_unsupported_cdb;
517 cmd->execute_cmd = sbc_emulate_readcapacity;
519 case SERVICE_ACTION_IN:
520 switch (cmd->t_task_cdb[1] & 0x1f) {
521 case SAI_READ_CAPACITY_16:
522 cmd->execute_cmd = sbc_emulate_readcapacity_16;
525 pr_err("Unsupported SA: 0x%02x\n",
526 cmd->t_task_cdb[1] & 0x1f);
527 goto out_invalid_cdb_field;
529 size = (cdb[10] << 24) | (cdb[11] << 16) |
530 (cdb[12] << 8) | cdb[13];
532 case SYNCHRONIZE_CACHE:
533 case SYNCHRONIZE_CACHE_16:
535 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
537 if (cdb[0] == SYNCHRONIZE_CACHE) {
538 sectors = transport_get_sectors_10(cdb);
539 cmd->t_task_lba = transport_lba_32(cdb);
541 sectors = transport_get_sectors_16(cdb);
542 cmd->t_task_lba = transport_lba_64(cdb);
545 size = sbc_get_size(cmd, sectors);
548 * Check to ensure that LBA + Range does not exceed past end of
549 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
551 if (cmd->t_task_lba || sectors) {
552 if (sbc_check_valid_sectors(cmd) < 0)
553 goto out_invalid_cdb_field;
555 cmd->execute_cmd = sbc_emulate_synchronize_cache;
558 size = get_unaligned_be16(&cdb[7]);
559 cmd->execute_cmd = sbc_emulate_unmap;
562 sectors = transport_get_sectors_16(cdb);
564 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
565 goto out_invalid_cdb_field;
568 size = sbc_get_size(cmd, 1);
569 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
571 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
572 goto out_unsupported_cdb;
573 cmd->execute_cmd = sbc_emulate_write_same;
576 sectors = transport_get_sectors_10(cdb);
578 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
579 goto out_invalid_cdb_field;
582 size = sbc_get_size(cmd, 1);
583 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
586 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
587 * of byte 1 bit 3 UNMAP instead of original reserved field
589 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
590 goto out_unsupported_cdb;
591 cmd->execute_cmd = sbc_emulate_write_same;
595 cmd->execute_cmd = sbc_emulate_verify;
598 ret = spc_parse_cdb(cmd, &size);
603 /* reject any command that we don't have a handler for */
604 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
605 goto out_unsupported_cdb;
607 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
608 unsigned long long end_lba;
610 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
611 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
612 " big sectors %u exceeds fabric_max_sectors:"
613 " %u\n", cdb[0], sectors,
614 su_dev->se_dev_attrib.fabric_max_sectors);
615 goto out_invalid_cdb_field;
617 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
618 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
619 " big sectors %u exceeds backend hw_max_sectors:"
620 " %u\n", cdb[0], sectors,
621 su_dev->se_dev_attrib.hw_max_sectors);
622 goto out_invalid_cdb_field;
625 end_lba = dev->transport->get_blocks(dev) + 1;
626 if (cmd->t_task_lba + sectors > end_lba) {
627 pr_err("cmd exceeds last lba %llu "
628 "(lba %llu, sectors %u)\n",
629 end_lba, cmd->t_task_lba, sectors);
630 goto out_invalid_cdb_field;
633 size = sbc_get_size(cmd, sectors);
636 ret = target_cmd_size_check(cmd, size);
643 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
644 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
646 out_invalid_cdb_field:
647 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
648 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
651 EXPORT_SYMBOL(sbc_parse_cdb);