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;
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->dev_attrib.block_size >> 24) & 0xff;
58 buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff;
59 buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff;
60 buf[7] = dev->dev_attrib.block_size & 0xff;
62 rbuf = transport_kmap_data_sg(cmd);
64 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
65 transport_kunmap_data_sg(cmd);
68 target_complete_cmd(cmd, GOOD);
72 static int sbc_emulate_readcapacity_16(struct se_cmd *cmd)
74 struct se_device *dev = cmd->se_dev;
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->dev_attrib.block_size >> 24) & 0xff;
89 buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff;
90 buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff;
91 buf[11] = dev->dev_attrib.block_size & 0xff;
93 * Set Thin Provisioning Enable bit following sbc3r22 in section
94 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
96 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
99 rbuf = transport_kmap_data_sg(cmd);
101 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
102 transport_kunmap_data_sg(cmd);
105 target_complete_cmd(cmd, GOOD);
109 int spc_get_write_same_sectors(struct se_cmd *cmd)
113 if (cmd->t_task_cdb[0] == WRITE_SAME)
114 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
115 else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
116 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
117 else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
118 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
121 * Use the explicit range when non zero is supplied, otherwise calculate
122 * the remaining range based on ->get_blocks() - starting LBA.
127 return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
130 EXPORT_SYMBOL(spc_get_write_same_sectors);
132 static int sbc_emulate_verify(struct se_cmd *cmd)
134 target_complete_cmd(cmd, GOOD);
138 static int sbc_emulate_noop(struct se_cmd *cmd)
140 target_complete_cmd(cmd, GOOD);
144 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
146 return cmd->se_dev->dev_attrib.block_size * sectors;
149 static int sbc_check_valid_sectors(struct se_cmd *cmd)
151 struct se_device *dev = cmd->se_dev;
152 unsigned long long end_lba;
155 sectors = cmd->data_length / dev->dev_attrib.block_size;
156 end_lba = dev->transport->get_blocks(dev) + 1;
158 if (cmd->t_task_lba + sectors > end_lba) {
159 pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
160 cmd->t_task_lba, sectors, end_lba);
167 static inline u32 transport_get_sectors_6(unsigned char *cdb)
170 * Use 8-bit sector value. SBC-3 says:
172 * A TRANSFER LENGTH field set to zero specifies that 256
173 * logical blocks shall be written. Any other value
174 * specifies the number of logical blocks that shall be
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];
197 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
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;
227 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
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"
250 * Currently for the emulated case we only accept
251 * tpws with the UNMAP=1 bit set.
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)
264 unsigned char *buf, *addr;
265 struct scatterlist *sg;
270 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
272 * 1) read the specified logical block(s);
273 * 2) transfer logical blocks from the data-out buffer;
274 * 3) XOR the logical blocks transferred from the data-out buffer with
275 * the logical blocks read, storing the resulting XOR data in a buffer;
276 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
277 * blocks transferred from the data-out buffer; and
278 * 5) transfer the resulting XOR data to the data-in buffer.
280 buf = kmalloc(cmd->data_length, GFP_KERNEL);
282 pr_err("Unable to allocate xor_callback buf\n");
286 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
287 * into the locally allocated *buf
289 sg_copy_to_buffer(cmd->t_data_sg,
295 * Now perform the XOR against the BIDI read memory located at
296 * cmd->t_mem_bidi_list
300 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
301 addr = kmap_atomic(sg_page(sg));
305 for (i = 0; i < sg->length; i++)
306 *(addr + sg->offset + i) ^= *(buf + offset + i);
308 offset += sg->length;
316 int sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
318 struct se_device *dev = cmd->se_dev;
319 unsigned char *cdb = cmd->t_task_cdb;
326 sectors = transport_get_sectors_6(cdb);
327 cmd->t_task_lba = transport_lba_21(cdb);
328 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
329 cmd->execute_cmd = ops->execute_rw;
332 sectors = transport_get_sectors_10(cdb);
333 cmd->t_task_lba = transport_lba_32(cdb);
334 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
335 cmd->execute_cmd = ops->execute_rw;
338 sectors = transport_get_sectors_12(cdb);
339 cmd->t_task_lba = transport_lba_32(cdb);
340 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
341 cmd->execute_cmd = ops->execute_rw;
344 sectors = transport_get_sectors_16(cdb);
345 cmd->t_task_lba = transport_lba_64(cdb);
346 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
347 cmd->execute_cmd = ops->execute_rw;
350 sectors = transport_get_sectors_6(cdb);
351 cmd->t_task_lba = transport_lba_21(cdb);
352 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
353 cmd->execute_cmd = ops->execute_rw;
357 sectors = transport_get_sectors_10(cdb);
358 cmd->t_task_lba = transport_lba_32(cdb);
360 cmd->se_cmd_flags |= SCF_FUA;
361 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
362 cmd->execute_cmd = ops->execute_rw;
365 sectors = transport_get_sectors_12(cdb);
366 cmd->t_task_lba = transport_lba_32(cdb);
368 cmd->se_cmd_flags |= SCF_FUA;
369 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
370 cmd->execute_cmd = ops->execute_rw;
373 sectors = transport_get_sectors_16(cdb);
374 cmd->t_task_lba = transport_lba_64(cdb);
376 cmd->se_cmd_flags |= SCF_FUA;
377 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
378 cmd->execute_cmd = ops->execute_rw;
381 if ((cmd->data_direction != DMA_TO_DEVICE) ||
382 !(cmd->se_cmd_flags & SCF_BIDI))
383 goto out_invalid_cdb_field;
384 sectors = transport_get_sectors_10(cdb);
386 cmd->t_task_lba = transport_lba_32(cdb);
387 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
390 * Setup BIDI XOR callback to be run after I/O completion.
392 cmd->execute_cmd = ops->execute_rw;
393 cmd->transport_complete_callback = &xdreadwrite_callback;
395 cmd->se_cmd_flags |= SCF_FUA;
397 case VARIABLE_LENGTH_CMD:
399 u16 service_action = get_unaligned_be16(&cdb[8]);
400 switch (service_action) {
402 sectors = transport_get_sectors_32(cdb);
405 * Use WRITE_32 and READ_32 opcodes for the emulated
406 * XDWRITE_READ_32 logic.
408 cmd->t_task_lba = transport_lba_64_ext(cdb);
409 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
412 * Setup BIDI XOR callback to be run during after I/O
415 cmd->execute_cmd = ops->execute_rw;
416 cmd->transport_complete_callback = &xdreadwrite_callback;
418 cmd->se_cmd_flags |= SCF_FUA;
421 if (!ops->execute_write_same)
422 goto out_unsupported_cdb;
424 sectors = transport_get_sectors_32(cdb);
426 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
428 goto out_invalid_cdb_field;
431 size = sbc_get_size(cmd, 1);
432 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
434 if (sbc_write_same_supported(dev, &cdb[10]) < 0)
435 goto out_unsupported_cdb;
436 cmd->execute_cmd = ops->execute_write_same;
439 pr_err("VARIABLE_LENGTH_CMD service action"
440 " 0x%04x not supported\n", service_action);
441 goto out_unsupported_cdb;
447 cmd->execute_cmd = sbc_emulate_readcapacity;
449 case SERVICE_ACTION_IN:
450 switch (cmd->t_task_cdb[1] & 0x1f) {
451 case SAI_READ_CAPACITY_16:
452 cmd->execute_cmd = sbc_emulate_readcapacity_16;
455 pr_err("Unsupported SA: 0x%02x\n",
456 cmd->t_task_cdb[1] & 0x1f);
457 goto out_invalid_cdb_field;
459 size = (cdb[10] << 24) | (cdb[11] << 16) |
460 (cdb[12] << 8) | cdb[13];
462 case SYNCHRONIZE_CACHE:
463 case SYNCHRONIZE_CACHE_16:
464 if (!ops->execute_sync_cache)
465 goto out_unsupported_cdb;
468 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
470 if (cdb[0] == SYNCHRONIZE_CACHE) {
471 sectors = transport_get_sectors_10(cdb);
472 cmd->t_task_lba = transport_lba_32(cdb);
474 sectors = transport_get_sectors_16(cdb);
475 cmd->t_task_lba = transport_lba_64(cdb);
478 size = sbc_get_size(cmd, sectors);
481 * Check to ensure that LBA + Range does not exceed past end of
482 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
484 if (cmd->t_task_lba || sectors) {
485 if (sbc_check_valid_sectors(cmd) < 0)
486 goto out_invalid_cdb_field;
488 cmd->execute_cmd = ops->execute_sync_cache;
491 if (!ops->execute_unmap)
492 goto out_unsupported_cdb;
494 size = get_unaligned_be16(&cdb[7]);
495 cmd->execute_cmd = ops->execute_unmap;
498 if (!ops->execute_write_same)
499 goto out_unsupported_cdb;
501 sectors = transport_get_sectors_16(cdb);
503 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
504 goto out_invalid_cdb_field;
507 size = sbc_get_size(cmd, 1);
508 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
510 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
511 goto out_unsupported_cdb;
512 cmd->execute_cmd = ops->execute_write_same;
515 if (!ops->execute_write_same)
516 goto out_unsupported_cdb;
518 sectors = transport_get_sectors_10(cdb);
520 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
521 goto out_invalid_cdb_field;
524 size = sbc_get_size(cmd, 1);
525 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
528 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
529 * of byte 1 bit 3 UNMAP instead of original reserved field
531 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
532 goto out_unsupported_cdb;
533 cmd->execute_cmd = ops->execute_write_same;
537 cmd->execute_cmd = sbc_emulate_verify;
543 * There are still clients out there which use these old SCSI-2
544 * commands. This mainly happens when running VMs with legacy
545 * guest systems, connected via SCSI command pass-through to
546 * iSCSI targets. Make them happy and return status GOOD.
549 cmd->execute_cmd = sbc_emulate_noop;
552 ret = spc_parse_cdb(cmd, &size);
557 /* reject any command that we don't have a handler for */
558 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
559 goto out_unsupported_cdb;
561 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
562 unsigned long long end_lba;
564 if (sectors > dev->dev_attrib.fabric_max_sectors) {
565 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
566 " big sectors %u exceeds fabric_max_sectors:"
567 " %u\n", cdb[0], sectors,
568 dev->dev_attrib.fabric_max_sectors);
569 goto out_invalid_cdb_field;
571 if (sectors > dev->dev_attrib.hw_max_sectors) {
572 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
573 " big sectors %u exceeds backend hw_max_sectors:"
574 " %u\n", cdb[0], sectors,
575 dev->dev_attrib.hw_max_sectors);
576 goto out_invalid_cdb_field;
579 end_lba = dev->transport->get_blocks(dev) + 1;
580 if (cmd->t_task_lba + sectors > end_lba) {
581 pr_err("cmd exceeds last lba %llu "
582 "(lba %llu, sectors %u)\n",
583 end_lba, cmd->t_task_lba, sectors);
584 goto out_invalid_cdb_field;
587 size = sbc_get_size(cmd, sectors);
590 ret = target_cmd_size_check(cmd, size);
597 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
598 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
600 out_invalid_cdb_field:
601 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
602 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
605 EXPORT_SYMBOL(sbc_parse_cdb);
607 u32 sbc_get_device_rev(struct se_device *dev)
609 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
611 EXPORT_SYMBOL(sbc_get_device_rev);
613 u32 sbc_get_device_type(struct se_device *dev)
617 EXPORT_SYMBOL(sbc_get_device_type);