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);
102 int spc_get_write_same_sectors(struct se_cmd *cmd)
106 if (cmd->t_task_cdb[0] == WRITE_SAME)
107 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
108 else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
109 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
110 else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
111 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
114 * Use the explicit range when non zero is supplied, otherwise calculate
115 * the remaining range based on ->get_blocks() - starting LBA.
120 return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
123 EXPORT_SYMBOL(spc_get_write_same_sectors);
125 static int sbc_emulate_verify(struct se_cmd *cmd)
127 target_complete_cmd(cmd, GOOD);
131 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
133 return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors;
136 static int sbc_check_valid_sectors(struct se_cmd *cmd)
138 struct se_device *dev = cmd->se_dev;
139 unsigned long long end_lba;
142 sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size;
143 end_lba = dev->transport->get_blocks(dev) + 1;
145 if (cmd->t_task_lba + sectors > end_lba) {
146 pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
147 cmd->t_task_lba, sectors, end_lba);
154 static inline u32 transport_get_sectors_6(unsigned char *cdb)
157 * Use 8-bit sector value. SBC-3 says:
159 * A TRANSFER LENGTH field set to zero specifies that 256
160 * logical blocks shall be written. Any other value
161 * specifies the number of logical blocks that shall be
164 return cdb[4] ? : 256;
167 static inline u32 transport_get_sectors_10(unsigned char *cdb)
169 return (u32)(cdb[7] << 8) + cdb[8];
172 static inline u32 transport_get_sectors_12(unsigned char *cdb)
174 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
177 static inline u32 transport_get_sectors_16(unsigned char *cdb)
179 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
180 (cdb[12] << 8) + cdb[13];
184 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
186 static inline u32 transport_get_sectors_32(unsigned char *cdb)
188 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
189 (cdb[30] << 8) + cdb[31];
193 static inline u32 transport_lba_21(unsigned char *cdb)
195 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
198 static inline u32 transport_lba_32(unsigned char *cdb)
200 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
203 static inline unsigned long long transport_lba_64(unsigned char *cdb)
205 unsigned int __v1, __v2;
207 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
208 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
210 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
214 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
216 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
218 unsigned int __v1, __v2;
220 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
221 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
223 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
226 static int sbc_write_same_supported(struct se_device *dev,
227 unsigned char *flags)
229 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
230 pr_err("WRITE_SAME PBDATA and LBDATA"
231 " bits not supported for Block Discard"
237 * Currently for the emulated case we only accept
238 * tpws with the UNMAP=1 bit set.
240 if (!(flags[0] & 0x08)) {
241 pr_err("WRITE_SAME w/o UNMAP bit not"
242 " supported for Block Discard Emulation\n");
249 static void xdreadwrite_callback(struct se_cmd *cmd)
251 unsigned char *buf, *addr;
252 struct scatterlist *sg;
257 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
259 * 1) read the specified logical block(s);
260 * 2) transfer logical blocks from the data-out buffer;
261 * 3) XOR the logical blocks transferred from the data-out buffer with
262 * the logical blocks read, storing the resulting XOR data in a buffer;
263 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
264 * blocks transferred from the data-out buffer; and
265 * 5) transfer the resulting XOR data to the data-in buffer.
267 buf = kmalloc(cmd->data_length, GFP_KERNEL);
269 pr_err("Unable to allocate xor_callback buf\n");
273 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
274 * into the locally allocated *buf
276 sg_copy_to_buffer(cmd->t_data_sg,
282 * Now perform the XOR against the BIDI read memory located at
283 * cmd->t_mem_bidi_list
287 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
288 addr = kmap_atomic(sg_page(sg));
292 for (i = 0; i < sg->length; i++)
293 *(addr + sg->offset + i) ^= *(buf + offset + i);
295 offset += sg->length;
303 int sbc_parse_cdb(struct se_cmd *cmd, struct spc_ops *ops)
305 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
306 struct se_device *dev = cmd->se_dev;
307 unsigned char *cdb = cmd->t_task_cdb;
314 sectors = transport_get_sectors_6(cdb);
315 cmd->t_task_lba = transport_lba_21(cdb);
316 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
317 cmd->execute_cmd = ops->execute_rw;
320 sectors = transport_get_sectors_10(cdb);
321 cmd->t_task_lba = transport_lba_32(cdb);
322 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
323 cmd->execute_cmd = ops->execute_rw;
326 sectors = transport_get_sectors_12(cdb);
327 cmd->t_task_lba = transport_lba_32(cdb);
328 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
329 cmd->execute_cmd = ops->execute_rw;
332 sectors = transport_get_sectors_16(cdb);
333 cmd->t_task_lba = transport_lba_64(cdb);
334 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
335 cmd->execute_cmd = ops->execute_rw;
338 sectors = transport_get_sectors_6(cdb);
339 cmd->t_task_lba = transport_lba_21(cdb);
340 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
341 cmd->execute_cmd = ops->execute_rw;
345 sectors = transport_get_sectors_10(cdb);
346 cmd->t_task_lba = transport_lba_32(cdb);
348 cmd->se_cmd_flags |= SCF_FUA;
349 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
350 cmd->execute_cmd = ops->execute_rw;
353 sectors = transport_get_sectors_12(cdb);
354 cmd->t_task_lba = transport_lba_32(cdb);
356 cmd->se_cmd_flags |= SCF_FUA;
357 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
358 cmd->execute_cmd = ops->execute_rw;
361 sectors = transport_get_sectors_16(cdb);
362 cmd->t_task_lba = transport_lba_64(cdb);
364 cmd->se_cmd_flags |= SCF_FUA;
365 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
366 cmd->execute_cmd = ops->execute_rw;
369 if ((cmd->data_direction != DMA_TO_DEVICE) ||
370 !(cmd->se_cmd_flags & SCF_BIDI))
371 goto out_invalid_cdb_field;
372 sectors = transport_get_sectors_10(cdb);
374 cmd->t_task_lba = transport_lba_32(cdb);
375 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
378 * Setup BIDI XOR callback to be run after I/O completion.
380 cmd->execute_cmd = ops->execute_rw;
381 cmd->transport_complete_callback = &xdreadwrite_callback;
383 cmd->se_cmd_flags |= SCF_FUA;
385 case VARIABLE_LENGTH_CMD:
387 u16 service_action = get_unaligned_be16(&cdb[8]);
388 switch (service_action) {
390 sectors = transport_get_sectors_32(cdb);
393 * Use WRITE_32 and READ_32 opcodes for the emulated
394 * XDWRITE_READ_32 logic.
396 cmd->t_task_lba = transport_lba_64_ext(cdb);
397 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
400 * Setup BIDI XOR callback to be run during after I/O
403 cmd->execute_cmd = ops->execute_rw;
404 cmd->transport_complete_callback = &xdreadwrite_callback;
406 cmd->se_cmd_flags |= SCF_FUA;
409 if (!ops->execute_write_same)
410 goto out_unsupported_cdb;
412 sectors = transport_get_sectors_32(cdb);
414 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
416 goto out_invalid_cdb_field;
419 size = sbc_get_size(cmd, 1);
420 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
422 if (sbc_write_same_supported(dev, &cdb[10]) < 0)
423 goto out_unsupported_cdb;
424 cmd->execute_cmd = ops->execute_write_same;
427 pr_err("VARIABLE_LENGTH_CMD service action"
428 " 0x%04x not supported\n", service_action);
429 goto out_unsupported_cdb;
435 cmd->execute_cmd = sbc_emulate_readcapacity;
437 case SERVICE_ACTION_IN:
438 switch (cmd->t_task_cdb[1] & 0x1f) {
439 case SAI_READ_CAPACITY_16:
440 cmd->execute_cmd = sbc_emulate_readcapacity_16;
443 pr_err("Unsupported SA: 0x%02x\n",
444 cmd->t_task_cdb[1] & 0x1f);
445 goto out_invalid_cdb_field;
447 size = (cdb[10] << 24) | (cdb[11] << 16) |
448 (cdb[12] << 8) | cdb[13];
450 case SYNCHRONIZE_CACHE:
451 case SYNCHRONIZE_CACHE_16:
452 if (!ops->execute_sync_cache)
453 goto out_unsupported_cdb;
456 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
458 if (cdb[0] == SYNCHRONIZE_CACHE) {
459 sectors = transport_get_sectors_10(cdb);
460 cmd->t_task_lba = transport_lba_32(cdb);
462 sectors = transport_get_sectors_16(cdb);
463 cmd->t_task_lba = transport_lba_64(cdb);
466 size = sbc_get_size(cmd, sectors);
469 * Check to ensure that LBA + Range does not exceed past end of
470 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
472 if (cmd->t_task_lba || sectors) {
473 if (sbc_check_valid_sectors(cmd) < 0)
474 goto out_invalid_cdb_field;
476 cmd->execute_cmd = ops->execute_sync_cache;
479 if (!ops->execute_unmap)
480 goto out_unsupported_cdb;
482 size = get_unaligned_be16(&cdb[7]);
483 cmd->execute_cmd = ops->execute_unmap;
486 if (!ops->execute_write_same)
487 goto out_unsupported_cdb;
489 sectors = transport_get_sectors_16(cdb);
491 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
492 goto out_invalid_cdb_field;
495 size = sbc_get_size(cmd, 1);
496 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
498 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
499 goto out_unsupported_cdb;
500 cmd->execute_cmd = ops->execute_write_same;
503 if (!ops->execute_write_same)
504 goto out_unsupported_cdb;
506 sectors = transport_get_sectors_10(cdb);
508 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
509 goto out_invalid_cdb_field;
512 size = sbc_get_size(cmd, 1);
513 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
516 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
517 * of byte 1 bit 3 UNMAP instead of original reserved field
519 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
520 goto out_unsupported_cdb;
521 cmd->execute_cmd = ops->execute_write_same;
525 cmd->execute_cmd = sbc_emulate_verify;
528 ret = spc_parse_cdb(cmd, &size);
533 /* reject any command that we don't have a handler for */
534 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
535 goto out_unsupported_cdb;
537 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
538 unsigned long long end_lba;
540 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
541 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
542 " big sectors %u exceeds fabric_max_sectors:"
543 " %u\n", cdb[0], sectors,
544 su_dev->se_dev_attrib.fabric_max_sectors);
545 goto out_invalid_cdb_field;
547 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
548 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
549 " big sectors %u exceeds backend hw_max_sectors:"
550 " %u\n", cdb[0], sectors,
551 su_dev->se_dev_attrib.hw_max_sectors);
552 goto out_invalid_cdb_field;
555 end_lba = dev->transport->get_blocks(dev) + 1;
556 if (cmd->t_task_lba + sectors > end_lba) {
557 pr_err("cmd exceeds last lba %llu "
558 "(lba %llu, sectors %u)\n",
559 end_lba, cmd->t_task_lba, sectors);
560 goto out_invalid_cdb_field;
563 size = sbc_get_size(cmd, sectors);
566 ret = target_cmd_size_check(cmd, size);
573 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
574 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
576 out_invalid_cdb_field:
577 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
578 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
581 EXPORT_SYMBOL(sbc_parse_cdb);