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 inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
42 return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors;
45 static int sbc_check_valid_sectors(struct se_cmd *cmd)
47 struct se_device *dev = cmd->se_dev;
48 unsigned long long end_lba;
51 sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size;
52 end_lba = dev->transport->get_blocks(dev) + 1;
54 if (cmd->t_task_lba + sectors > end_lba) {
55 pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
56 cmd->t_task_lba, sectors, end_lba);
63 static inline u32 transport_get_sectors_6(unsigned char *cdb)
66 * Use 8-bit sector value. SBC-3 says:
68 * A TRANSFER LENGTH field set to zero specifies that 256
69 * logical blocks shall be written. Any other value
70 * specifies the number of logical blocks that shall be
73 return cdb[4] ? : 256;
76 static inline u32 transport_get_sectors_10(unsigned char *cdb)
78 return (u32)(cdb[7] << 8) + cdb[8];
81 static inline u32 transport_get_sectors_12(unsigned char *cdb)
83 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
86 static inline u32 transport_get_sectors_16(unsigned char *cdb)
88 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
89 (cdb[12] << 8) + cdb[13];
93 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
95 static inline u32 transport_get_sectors_32(unsigned char *cdb)
97 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
98 (cdb[30] << 8) + cdb[31];
102 static inline u32 transport_lba_21(unsigned char *cdb)
104 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
107 static inline u32 transport_lba_32(unsigned char *cdb)
109 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
112 static inline unsigned long long transport_lba_64(unsigned char *cdb)
114 unsigned int __v1, __v2;
116 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
117 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
119 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
123 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
125 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
127 unsigned int __v1, __v2;
129 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
130 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
132 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
135 static int sbc_write_same_supported(struct se_device *dev,
136 unsigned char *flags)
138 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
139 pr_err("WRITE_SAME PBDATA and LBDATA"
140 " bits not supported for Block Discard"
146 * Currently for the emulated case we only accept
147 * tpws with the UNMAP=1 bit set.
149 if (!(flags[0] & 0x08)) {
150 pr_err("WRITE_SAME w/o UNMAP bit not"
151 " supported for Block Discard Emulation\n");
158 static void xdreadwrite_callback(struct se_cmd *cmd)
160 unsigned char *buf, *addr;
161 struct scatterlist *sg;
166 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
168 * 1) read the specified logical block(s);
169 * 2) transfer logical blocks from the data-out buffer;
170 * 3) XOR the logical blocks transferred from the data-out buffer with
171 * the logical blocks read, storing the resulting XOR data in a buffer;
172 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
173 * blocks transferred from the data-out buffer; and
174 * 5) transfer the resulting XOR data to the data-in buffer.
176 buf = kmalloc(cmd->data_length, GFP_KERNEL);
178 pr_err("Unable to allocate xor_callback buf\n");
182 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
183 * into the locally allocated *buf
185 sg_copy_to_buffer(cmd->t_data_sg,
191 * Now perform the XOR against the BIDI read memory located at
192 * cmd->t_mem_bidi_list
196 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
197 addr = kmap_atomic(sg_page(sg));
201 for (i = 0; i < sg->length; i++)
202 *(addr + sg->offset + i) ^= *(buf + offset + i);
204 offset += sg->length;
212 int sbc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
214 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
215 struct se_device *dev = cmd->se_dev;
216 unsigned char *cdb = cmd->t_task_cdb;
222 sectors = transport_get_sectors_6(cdb);
223 cmd->t_task_lba = transport_lba_21(cdb);
224 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
227 sectors = transport_get_sectors_10(cdb);
228 cmd->t_task_lba = transport_lba_32(cdb);
229 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
232 sectors = transport_get_sectors_12(cdb);
233 cmd->t_task_lba = transport_lba_32(cdb);
234 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
237 sectors = transport_get_sectors_16(cdb);
238 cmd->t_task_lba = transport_lba_64(cdb);
239 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
242 sectors = transport_get_sectors_6(cdb);
243 cmd->t_task_lba = transport_lba_21(cdb);
244 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
248 sectors = transport_get_sectors_10(cdb);
249 cmd->t_task_lba = transport_lba_32(cdb);
251 cmd->se_cmd_flags |= SCF_FUA;
252 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
255 sectors = transport_get_sectors_12(cdb);
256 cmd->t_task_lba = transport_lba_32(cdb);
258 cmd->se_cmd_flags |= SCF_FUA;
259 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
262 sectors = transport_get_sectors_16(cdb);
263 cmd->t_task_lba = transport_lba_64(cdb);
265 cmd->se_cmd_flags |= SCF_FUA;
266 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
269 if ((cmd->data_direction != DMA_TO_DEVICE) ||
270 !(cmd->se_cmd_flags & SCF_BIDI))
271 goto out_invalid_cdb_field;
272 sectors = transport_get_sectors_10(cdb);
274 cmd->t_task_lba = transport_lba_32(cdb);
275 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
278 * Setup BIDI XOR callback to be run after I/O completion.
280 cmd->transport_complete_callback = &xdreadwrite_callback;
282 cmd->se_cmd_flags |= SCF_FUA;
284 case VARIABLE_LENGTH_CMD:
286 u16 service_action = get_unaligned_be16(&cdb[8]);
287 switch (service_action) {
289 sectors = transport_get_sectors_32(cdb);
292 * Use WRITE_32 and READ_32 opcodes for the emulated
293 * XDWRITE_READ_32 logic.
295 cmd->t_task_lba = transport_lba_64_ext(cdb);
296 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
299 * Setup BIDI XOR callback to be run during after I/O
302 cmd->transport_complete_callback = &xdreadwrite_callback;
304 cmd->se_cmd_flags |= SCF_FUA;
307 sectors = transport_get_sectors_32(cdb);
309 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
311 goto out_invalid_cdb_field;
314 *size = sbc_get_size(cmd, 1);
315 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
317 if (sbc_write_same_supported(dev, &cdb[10]) < 0)
318 goto out_unsupported_cdb;
319 cmd->execute_cmd = target_emulate_write_same;
322 pr_err("VARIABLE_LENGTH_CMD service action"
323 " 0x%04x not supported\n", service_action);
324 goto out_unsupported_cdb;
329 *size = READ_CAP_LEN;
330 cmd->execute_cmd = target_emulate_readcapacity;
332 case SERVICE_ACTION_IN:
333 switch (cmd->t_task_cdb[1] & 0x1f) {
334 case SAI_READ_CAPACITY_16:
335 cmd->execute_cmd = target_emulate_readcapacity_16;
338 pr_err("Unsupported SA: 0x%02x\n",
339 cmd->t_task_cdb[1] & 0x1f);
340 goto out_invalid_cdb_field;
342 *size = (cdb[10] << 24) | (cdb[11] << 16) |
343 (cdb[12] << 8) | cdb[13];
345 case SYNCHRONIZE_CACHE:
346 case SYNCHRONIZE_CACHE_16:
348 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
350 if (cdb[0] == SYNCHRONIZE_CACHE) {
351 sectors = transport_get_sectors_10(cdb);
352 cmd->t_task_lba = transport_lba_32(cdb);
354 sectors = transport_get_sectors_16(cdb);
355 cmd->t_task_lba = transport_lba_64(cdb);
358 *size = sbc_get_size(cmd, sectors);
361 * Check to ensure that LBA + Range does not exceed past end of
362 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
364 if (cmd->t_task_lba || sectors) {
365 if (sbc_check_valid_sectors(cmd) < 0)
366 goto out_invalid_cdb_field;
368 cmd->execute_cmd = target_emulate_synchronize_cache;
371 *size = get_unaligned_be16(&cdb[7]);
372 cmd->execute_cmd = target_emulate_unmap;
375 sectors = transport_get_sectors_16(cdb);
377 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
378 goto out_invalid_cdb_field;
381 *size = sbc_get_size(cmd, 1);
382 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
384 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
385 goto out_unsupported_cdb;
386 cmd->execute_cmd = target_emulate_write_same;
389 sectors = transport_get_sectors_10(cdb);
391 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
392 goto out_invalid_cdb_field;
395 *size = sbc_get_size(cmd, 1);
396 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
399 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
400 * of byte 1 bit 3 UNMAP instead of original reserved field
402 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
403 goto out_unsupported_cdb;
404 cmd->execute_cmd = target_emulate_write_same;
408 cmd->execute_cmd = target_emulate_noop;
411 ret = spc_parse_cdb(cmd, size, false);
416 /* reject any command that we don't have a handler for */
417 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
418 goto out_unsupported_cdb;
420 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
421 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
422 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
423 " big sectors %u exceeds fabric_max_sectors:"
424 " %u\n", cdb[0], sectors,
425 su_dev->se_dev_attrib.fabric_max_sectors);
426 goto out_invalid_cdb_field;
428 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
429 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
430 " big sectors %u exceeds backend hw_max_sectors:"
431 " %u\n", cdb[0], sectors,
432 su_dev->se_dev_attrib.hw_max_sectors);
433 goto out_invalid_cdb_field;
436 *size = sbc_get_size(cmd, sectors);
442 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
443 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
445 out_invalid_cdb_field:
446 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
447 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
450 EXPORT_SYMBOL(sbc_parse_cdb);