2 * SCSI Block Commands (SBC) parsing and emulation.
4 * (c) Copyright 2002-2013 Datera, Inc.
6 * Nicholas A. Bellinger <nab@kernel.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/ratelimit.h>
26 #include <linux/crc-t10dif.h>
27 #include <linux/t10-pi.h>
28 #include <asm/unaligned.h>
29 #include <scsi/scsi_proto.h>
30 #include <scsi/scsi_tcq.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"
38 #include "target_core_alua.h"
41 sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char *, u32, bool);
42 static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd);
45 sbc_emulate_readcapacity(struct se_cmd *cmd)
47 struct se_device *dev = cmd->se_dev;
48 unsigned char *cdb = cmd->t_task_cdb;
49 unsigned long long blocks_long = dev->transport->get_blocks(dev);
56 * If the PMI bit is set to zero and the LOGICAL BLOCK
57 * ADDRESS field is not set to zero, the device server shall
58 * terminate the command with CHECK CONDITION status with
59 * the sense key set to ILLEGAL REQUEST and the additional
60 * sense code set to INVALID FIELD IN CDB.
62 * In SBC-3, these fields are obsolete, but some SCSI
63 * compliance tests actually check this, so we might as well
66 if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5]))
67 return TCM_INVALID_CDB_FIELD;
69 if (blocks_long >= 0x00000000ffffffff)
72 blocks = (u32)blocks_long;
74 buf[0] = (blocks >> 24) & 0xff;
75 buf[1] = (blocks >> 16) & 0xff;
76 buf[2] = (blocks >> 8) & 0xff;
77 buf[3] = blocks & 0xff;
78 buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff;
79 buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff;
80 buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff;
81 buf[7] = dev->dev_attrib.block_size & 0xff;
83 rbuf = transport_kmap_data_sg(cmd);
85 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
86 transport_kunmap_data_sg(cmd);
89 target_complete_cmd_with_length(cmd, GOOD, 8);
94 sbc_emulate_readcapacity_16(struct se_cmd *cmd)
96 struct se_device *dev = cmd->se_dev;
97 struct se_session *sess = cmd->se_sess;
98 int pi_prot_type = dev->dev_attrib.pi_prot_type;
101 unsigned char buf[32];
102 unsigned long long blocks = dev->transport->get_blocks(dev);
104 memset(buf, 0, sizeof(buf));
105 buf[0] = (blocks >> 56) & 0xff;
106 buf[1] = (blocks >> 48) & 0xff;
107 buf[2] = (blocks >> 40) & 0xff;
108 buf[3] = (blocks >> 32) & 0xff;
109 buf[4] = (blocks >> 24) & 0xff;
110 buf[5] = (blocks >> 16) & 0xff;
111 buf[6] = (blocks >> 8) & 0xff;
112 buf[7] = blocks & 0xff;
113 buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff;
114 buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff;
115 buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff;
116 buf[11] = dev->dev_attrib.block_size & 0xff;
118 * Set P_TYPE and PROT_EN bits for DIF support
120 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
122 * Only override a device's pi_prot_type if no T10-PI is
123 * available, and sess_prot_type has been explicitly enabled.
126 pi_prot_type = sess->sess_prot_type;
129 buf[12] = (pi_prot_type - 1) << 1 | 0x1;
132 if (dev->transport->get_lbppbe)
133 buf[13] = dev->transport->get_lbppbe(dev) & 0x0f;
135 if (dev->transport->get_alignment_offset_lbas) {
136 u16 lalba = dev->transport->get_alignment_offset_lbas(dev);
137 buf[14] = (lalba >> 8) & 0x3f;
138 buf[15] = lalba & 0xff;
142 * Set Thin Provisioning Enable bit following sbc3r22 in section
143 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
145 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) {
149 * LBPRZ signifies that zeroes will be read back from an LBA after
150 * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2)
152 if (dev->dev_attrib.unmap_zeroes_data)
156 rbuf = transport_kmap_data_sg(cmd);
158 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
159 transport_kunmap_data_sg(cmd);
162 target_complete_cmd_with_length(cmd, GOOD, 32);
166 static sense_reason_t
167 sbc_emulate_startstop(struct se_cmd *cmd)
169 unsigned char *cdb = cmd->t_task_cdb;
172 * See sbc3r36 section 5.25
173 * Immediate bit should be set since there is nothing to complete
174 * POWER CONDITION MODIFIER 0h
176 if (!(cdb[1] & 1) || cdb[2] || cdb[3])
177 return TCM_INVALID_CDB_FIELD;
180 * See sbc3r36 section 5.25
181 * POWER CONDITION 0h START_VALID - process START and LOEJ
183 if (cdb[4] >> 4 & 0xf)
184 return TCM_INVALID_CDB_FIELD;
187 * See sbc3r36 section 5.25
188 * LOEJ 0h - nothing to load or unload
189 * START 1h - we are ready
191 if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4))
192 return TCM_INVALID_CDB_FIELD;
194 target_complete_cmd(cmd, SAM_STAT_GOOD);
198 sector_t sbc_get_write_same_sectors(struct se_cmd *cmd)
202 if (cmd->t_task_cdb[0] == WRITE_SAME)
203 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
204 else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
205 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
206 else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
207 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
210 * Use the explicit range when non zero is supplied, otherwise calculate
211 * the remaining range based on ->get_blocks() - starting LBA.
216 return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
219 EXPORT_SYMBOL(sbc_get_write_same_sectors);
221 static sense_reason_t
222 sbc_execute_write_same_unmap(struct se_cmd *cmd)
224 struct sbc_ops *ops = cmd->protocol_data;
225 sector_t nolb = sbc_get_write_same_sectors(cmd);
229 ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb);
234 target_complete_cmd(cmd, GOOD);
238 static sense_reason_t
239 sbc_emulate_noop(struct se_cmd *cmd)
241 target_complete_cmd(cmd, GOOD);
245 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
247 return cmd->se_dev->dev_attrib.block_size * sectors;
250 static inline u32 transport_get_sectors_6(unsigned char *cdb)
253 * Use 8-bit sector value. SBC-3 says:
255 * A TRANSFER LENGTH field set to zero specifies that 256
256 * logical blocks shall be written. Any other value
257 * specifies the number of logical blocks that shall be
260 return cdb[4] ? : 256;
263 static inline u32 transport_get_sectors_10(unsigned char *cdb)
265 return (u32)(cdb[7] << 8) + cdb[8];
268 static inline u32 transport_get_sectors_12(unsigned char *cdb)
270 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
273 static inline u32 transport_get_sectors_16(unsigned char *cdb)
275 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
276 (cdb[12] << 8) + cdb[13];
280 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
282 static inline u32 transport_get_sectors_32(unsigned char *cdb)
284 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
285 (cdb[30] << 8) + cdb[31];
289 static inline u32 transport_lba_21(unsigned char *cdb)
291 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
294 static inline u32 transport_lba_32(unsigned char *cdb)
296 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
299 static inline unsigned long long transport_lba_64(unsigned char *cdb)
301 unsigned int __v1, __v2;
303 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
304 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
306 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
310 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
312 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
314 unsigned int __v1, __v2;
316 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
317 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
319 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
322 static sense_reason_t
323 sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops)
325 struct se_device *dev = cmd->se_dev;
326 sector_t end_lba = dev->transport->get_blocks(dev) + 1;
327 unsigned int sectors = sbc_get_write_same_sectors(cmd);
330 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
331 pr_err("WRITE_SAME PBDATA and LBDATA"
332 " bits not supported for Block Discard"
334 return TCM_UNSUPPORTED_SCSI_OPCODE;
336 if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) {
337 pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n",
338 sectors, cmd->se_dev->dev_attrib.max_write_same_len);
339 return TCM_INVALID_CDB_FIELD;
342 * Sanity check for LBA wrap and request past end of device.
344 if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
345 ((cmd->t_task_lba + sectors) > end_lba)) {
346 pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n",
347 (unsigned long long)end_lba, cmd->t_task_lba, sectors);
348 return TCM_ADDRESS_OUT_OF_RANGE;
351 /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */
352 if (flags[0] & 0x10) {
353 pr_warn("WRITE SAME with ANCHOR not supported\n");
354 return TCM_INVALID_CDB_FIELD;
357 * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting
358 * translated into block discard requests within backend code.
360 if (flags[0] & 0x08) {
361 if (!ops->execute_unmap)
362 return TCM_UNSUPPORTED_SCSI_OPCODE;
364 if (!dev->dev_attrib.emulate_tpws) {
365 pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device"
366 " has emulate_tpws disabled\n");
367 return TCM_UNSUPPORTED_SCSI_OPCODE;
369 cmd->execute_cmd = sbc_execute_write_same_unmap;
372 if (!ops->execute_write_same)
373 return TCM_UNSUPPORTED_SCSI_OPCODE;
375 ret = sbc_check_prot(dev, cmd, &cmd->t_task_cdb[0], sectors, true);
379 cmd->execute_cmd = ops->execute_write_same;
383 static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success,
386 unsigned char *buf, *addr;
387 struct scatterlist *sg;
389 sense_reason_t ret = TCM_NO_SENSE;
392 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
394 * 1) read the specified logical block(s);
395 * 2) transfer logical blocks from the data-out buffer;
396 * 3) XOR the logical blocks transferred from the data-out buffer with
397 * the logical blocks read, storing the resulting XOR data in a buffer;
398 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
399 * blocks transferred from the data-out buffer; and
400 * 5) transfer the resulting XOR data to the data-in buffer.
402 buf = kmalloc(cmd->data_length, GFP_KERNEL);
404 pr_err("Unable to allocate xor_callback buf\n");
405 return TCM_OUT_OF_RESOURCES;
408 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
409 * into the locally allocated *buf
411 sg_copy_to_buffer(cmd->t_data_sg,
417 * Now perform the XOR against the BIDI read memory located at
418 * cmd->t_mem_bidi_list
422 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
423 addr = kmap_atomic(sg_page(sg));
425 ret = TCM_OUT_OF_RESOURCES;
429 for (i = 0; i < sg->length; i++)
430 *(addr + sg->offset + i) ^= *(buf + offset + i);
432 offset += sg->length;
441 static sense_reason_t
442 sbc_execute_rw(struct se_cmd *cmd)
444 struct sbc_ops *ops = cmd->protocol_data;
446 return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents,
447 cmd->data_direction);
450 static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success,
453 struct se_device *dev = cmd->se_dev;
454 sense_reason_t ret = TCM_NO_SENSE;
457 * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through
458 * within target_complete_ok_work() if the command was successfully
459 * sent to the backend driver.
461 spin_lock_irq(&cmd->t_state_lock);
462 if (cmd->transport_state & CMD_T_SENT) {
463 cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST;
466 if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
467 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
469 spin_unlock_irq(&cmd->t_state_lock);
472 * Unlock ->caw_sem originally obtained during sbc_compare_and_write()
473 * before the original READ I/O submission.
480 static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success,
483 struct se_device *dev = cmd->se_dev;
484 struct scatterlist *write_sg = NULL, *sg;
485 unsigned char *buf = NULL, *addr;
486 struct sg_mapping_iter m;
487 unsigned int offset = 0, len;
488 unsigned int nlbas = cmd->t_task_nolb;
489 unsigned int block_size = dev->dev_attrib.block_size;
490 unsigned int compare_len = (nlbas * block_size);
491 sense_reason_t ret = TCM_NO_SENSE;
495 * Handle early failure in transport_generic_request_failure(),
496 * which will not have taken ->caw_sem yet..
498 if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg))
501 * Handle special case for zero-length COMPARE_AND_WRITE
503 if (!cmd->data_length)
506 * Immediately exit + release dev->caw_sem if command has already
507 * been failed with a non-zero SCSI status.
509 if (cmd->scsi_status) {
510 pr_debug("compare_and_write_callback: non zero scsi_status:"
511 " 0x%02x\n", cmd->scsi_status);
513 if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
514 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
518 buf = kzalloc(cmd->data_length, GFP_KERNEL);
520 pr_err("Unable to allocate compare_and_write buf\n");
521 ret = TCM_OUT_OF_RESOURCES;
525 write_sg = kmalloc_array(cmd->t_data_nents, sizeof(*write_sg),
528 pr_err("Unable to allocate compare_and_write sg\n");
529 ret = TCM_OUT_OF_RESOURCES;
532 sg_init_table(write_sg, cmd->t_data_nents);
534 * Setup verify and write data payloads from total NumberLBAs.
536 rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf,
539 pr_err("sg_copy_to_buffer() failed for compare_and_write\n");
540 ret = TCM_OUT_OF_RESOURCES;
544 * Compare against SCSI READ payload against verify payload
546 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) {
547 addr = (unsigned char *)kmap_atomic(sg_page(sg));
549 ret = TCM_OUT_OF_RESOURCES;
553 len = min(sg->length, compare_len);
555 if (memcmp(addr, buf + offset, len)) {
556 pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n",
570 len = cmd->t_task_nolb * block_size;
571 sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG);
573 * Currently assumes NoLB=1 and SGLs are PAGE_SIZE..
578 if (block_size < PAGE_SIZE) {
579 sg_set_page(&write_sg[i], m.page, block_size,
580 m.piter.sg->offset + block_size);
583 sg_set_page(&write_sg[i], m.page, block_size,
591 * Save the original SGL + nents values before updating to new
592 * assignments, to be released in transport_free_pages() ->
593 * transport_reset_sgl_orig()
595 cmd->t_data_sg_orig = cmd->t_data_sg;
596 cmd->t_data_sg = write_sg;
597 cmd->t_data_nents_orig = cmd->t_data_nents;
598 cmd->t_data_nents = 1;
600 cmd->sam_task_attr = TCM_HEAD_TAG;
601 cmd->transport_complete_callback = compare_and_write_post;
603 * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler
604 * for submitting the adjusted SGL to write instance user-data.
606 cmd->execute_cmd = sbc_execute_rw;
608 spin_lock_irq(&cmd->t_state_lock);
609 cmd->t_state = TRANSPORT_PROCESSING;
610 cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
611 spin_unlock_irq(&cmd->t_state_lock);
613 __target_execute_cmd(cmd, false);
619 pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n",
620 dev->transport->name);
621 ret = TCM_MISCOMPARE_VERIFY;
624 * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in
625 * sbc_compare_and_write() before the original READ I/O submission.
633 static sense_reason_t
634 sbc_compare_and_write(struct se_cmd *cmd)
636 struct sbc_ops *ops = cmd->protocol_data;
637 struct se_device *dev = cmd->se_dev;
641 * Submit the READ first for COMPARE_AND_WRITE to perform the
642 * comparision using SGLs at cmd->t_bidi_data_sg..
644 rc = down_interruptible(&dev->caw_sem);
646 cmd->transport_complete_callback = NULL;
647 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
650 * Reset cmd->data_length to individual block_size in order to not
651 * confuse backend drivers that depend on this value matching the
652 * size of the I/O being submitted.
654 cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size;
656 ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents,
659 cmd->transport_complete_callback = NULL;
664 * Unlock of dev->caw_sem to occur in compare_and_write_callback()
665 * upon MISCOMPARE, or in compare_and_write_done() upon completion
666 * of WRITE instance user-data.
672 sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type,
673 bool is_write, struct se_cmd *cmd)
676 cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP :
677 protect ? TARGET_PROT_DOUT_PASS :
678 TARGET_PROT_DOUT_INSERT;
682 cmd->prot_checks = 0;
686 cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
687 if (prot_type == TARGET_DIF_TYPE1_PROT)
688 cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
691 if (prot_type == TARGET_DIF_TYPE1_PROT)
692 cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
695 cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
698 pr_err("Unsupported protect field %d\n", protect);
702 cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT :
703 protect ? TARGET_PROT_DIN_PASS :
704 TARGET_PROT_DIN_STRIP;
709 cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
710 if (prot_type == TARGET_DIF_TYPE1_PROT)
711 cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
714 if (prot_type == TARGET_DIF_TYPE1_PROT)
715 cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
718 cmd->prot_checks = 0;
721 cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
724 pr_err("Unsupported protect field %d\n", protect);
732 static sense_reason_t
733 sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb,
734 u32 sectors, bool is_write)
736 u8 protect = cdb[1] >> 5;
737 int sp_ops = cmd->se_sess->sup_prot_ops;
738 int pi_prot_type = dev->dev_attrib.pi_prot_type;
739 bool fabric_prot = false;
741 if (!cmd->t_prot_sg || !cmd->t_prot_nents) {
742 if (unlikely(protect &&
743 !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) {
744 pr_err("CDB contains protect bit, but device + fabric does"
745 " not advertise PROTECT=1 feature bit\n");
746 return TCM_INVALID_CDB_FIELD;
752 switch (dev->dev_attrib.pi_prot_type) {
753 case TARGET_DIF_TYPE3_PROT:
754 cmd->reftag_seed = 0xffffffff;
756 case TARGET_DIF_TYPE2_PROT:
758 return TCM_INVALID_CDB_FIELD;
760 cmd->reftag_seed = cmd->t_task_lba;
762 case TARGET_DIF_TYPE1_PROT:
763 cmd->reftag_seed = cmd->t_task_lba;
765 case TARGET_DIF_TYPE0_PROT:
767 * See if the fabric supports T10-PI, and the session has been
768 * configured to allow export PROTECT=1 feature bit with backend
769 * devices that don't support T10-PI.
771 fabric_prot = is_write ?
772 !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) :
773 !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT));
775 if (fabric_prot && cmd->se_sess->sess_prot_type) {
776 pi_prot_type = cmd->se_sess->sess_prot_type;
783 pr_err("Unable to determine pi_prot_type for CDB: 0x%02x "
784 "PROTECT: 0x%02x\n", cdb[0], protect);
785 return TCM_INVALID_CDB_FIELD;
788 if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd))
789 return TCM_INVALID_CDB_FIELD;
791 cmd->prot_type = pi_prot_type;
792 cmd->prot_length = dev->prot_length * sectors;
795 * In case protection information exists over the wire
796 * we modify command data length to describe pure data.
797 * The actual transfer length is data length + protection
801 cmd->data_length = sectors * dev->dev_attrib.block_size;
803 pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d "
804 "prot_op=%d prot_checks=%d\n",
805 __func__, cmd->prot_type, cmd->data_length, cmd->prot_length,
806 cmd->prot_op, cmd->prot_checks);
812 sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb)
815 /* see explanation in spc_emulate_modesense */
816 if (!target_check_fua(dev)) {
817 pr_err("Got CDB: 0x%02x with DPO bit set, but device"
818 " does not advertise support for DPO\n", cdb[0]);
823 if (!target_check_fua(dev)) {
824 pr_err("Got CDB: 0x%02x with FUA bit set, but device"
825 " does not advertise support for FUA write\n",
829 cmd->se_cmd_flags |= SCF_FUA;
835 sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
837 struct se_device *dev = cmd->se_dev;
838 unsigned char *cdb = cmd->t_task_cdb;
843 cmd->protocol_data = ops;
847 sectors = transport_get_sectors_6(cdb);
848 cmd->t_task_lba = transport_lba_21(cdb);
849 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
850 cmd->execute_cmd = sbc_execute_rw;
853 sectors = transport_get_sectors_10(cdb);
854 cmd->t_task_lba = transport_lba_32(cdb);
856 if (sbc_check_dpofua(dev, cmd, cdb))
857 return TCM_INVALID_CDB_FIELD;
859 ret = sbc_check_prot(dev, cmd, cdb, sectors, false);
863 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
864 cmd->execute_cmd = sbc_execute_rw;
867 sectors = transport_get_sectors_12(cdb);
868 cmd->t_task_lba = transport_lba_32(cdb);
870 if (sbc_check_dpofua(dev, cmd, cdb))
871 return TCM_INVALID_CDB_FIELD;
873 ret = sbc_check_prot(dev, cmd, cdb, sectors, false);
877 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
878 cmd->execute_cmd = sbc_execute_rw;
881 sectors = transport_get_sectors_16(cdb);
882 cmd->t_task_lba = transport_lba_64(cdb);
884 if (sbc_check_dpofua(dev, cmd, cdb))
885 return TCM_INVALID_CDB_FIELD;
887 ret = sbc_check_prot(dev, cmd, cdb, sectors, false);
891 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
892 cmd->execute_cmd = sbc_execute_rw;
895 sectors = transport_get_sectors_6(cdb);
896 cmd->t_task_lba = transport_lba_21(cdb);
897 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
898 cmd->execute_cmd = sbc_execute_rw;
902 sectors = transport_get_sectors_10(cdb);
903 cmd->t_task_lba = transport_lba_32(cdb);
905 if (sbc_check_dpofua(dev, cmd, cdb))
906 return TCM_INVALID_CDB_FIELD;
908 ret = sbc_check_prot(dev, cmd, cdb, sectors, true);
912 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
913 cmd->execute_cmd = sbc_execute_rw;
916 sectors = transport_get_sectors_12(cdb);
917 cmd->t_task_lba = transport_lba_32(cdb);
919 if (sbc_check_dpofua(dev, cmd, cdb))
920 return TCM_INVALID_CDB_FIELD;
922 ret = sbc_check_prot(dev, cmd, cdb, sectors, true);
926 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
927 cmd->execute_cmd = sbc_execute_rw;
930 case WRITE_VERIFY_16:
931 sectors = transport_get_sectors_16(cdb);
932 cmd->t_task_lba = transport_lba_64(cdb);
934 if (sbc_check_dpofua(dev, cmd, cdb))
935 return TCM_INVALID_CDB_FIELD;
937 ret = sbc_check_prot(dev, cmd, cdb, sectors, true);
941 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
942 cmd->execute_cmd = sbc_execute_rw;
945 if (cmd->data_direction != DMA_TO_DEVICE ||
946 !(cmd->se_cmd_flags & SCF_BIDI))
947 return TCM_INVALID_CDB_FIELD;
948 sectors = transport_get_sectors_10(cdb);
950 if (sbc_check_dpofua(dev, cmd, cdb))
951 return TCM_INVALID_CDB_FIELD;
953 cmd->t_task_lba = transport_lba_32(cdb);
954 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
957 * Setup BIDI XOR callback to be run after I/O completion.
959 cmd->execute_cmd = sbc_execute_rw;
960 cmd->transport_complete_callback = &xdreadwrite_callback;
962 case VARIABLE_LENGTH_CMD:
964 u16 service_action = get_unaligned_be16(&cdb[8]);
965 switch (service_action) {
967 sectors = transport_get_sectors_32(cdb);
969 if (sbc_check_dpofua(dev, cmd, cdb))
970 return TCM_INVALID_CDB_FIELD;
972 * Use WRITE_32 and READ_32 opcodes for the emulated
973 * XDWRITE_READ_32 logic.
975 cmd->t_task_lba = transport_lba_64_ext(cdb);
976 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
979 * Setup BIDI XOR callback to be run during after I/O
982 cmd->execute_cmd = sbc_execute_rw;
983 cmd->transport_complete_callback = &xdreadwrite_callback;
986 sectors = transport_get_sectors_32(cdb);
988 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
990 return TCM_INVALID_CDB_FIELD;
993 size = sbc_get_size(cmd, 1);
994 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
996 ret = sbc_setup_write_same(cmd, &cdb[10], ops);
1001 pr_err("VARIABLE_LENGTH_CMD service action"
1002 " 0x%04x not supported\n", service_action);
1003 return TCM_UNSUPPORTED_SCSI_OPCODE;
1007 case COMPARE_AND_WRITE:
1010 * Currently enforce COMPARE_AND_WRITE for a single sector
1013 pr_err("COMPARE_AND_WRITE contains NoLB: %u greater"
1014 " than 1\n", sectors);
1015 return TCM_INVALID_CDB_FIELD;
1017 if (sbc_check_dpofua(dev, cmd, cdb))
1018 return TCM_INVALID_CDB_FIELD;
1021 * Double size because we have two buffers, note that
1022 * zero is not an error..
1024 size = 2 * sbc_get_size(cmd, sectors);
1025 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
1026 cmd->t_task_nolb = sectors;
1027 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE;
1028 cmd->execute_cmd = sbc_compare_and_write;
1029 cmd->transport_complete_callback = compare_and_write_callback;
1032 size = READ_CAP_LEN;
1033 cmd->execute_cmd = sbc_emulate_readcapacity;
1035 case SERVICE_ACTION_IN_16:
1036 switch (cmd->t_task_cdb[1] & 0x1f) {
1037 case SAI_READ_CAPACITY_16:
1038 cmd->execute_cmd = sbc_emulate_readcapacity_16;
1040 case SAI_REPORT_REFERRALS:
1041 cmd->execute_cmd = target_emulate_report_referrals;
1044 pr_err("Unsupported SA: 0x%02x\n",
1045 cmd->t_task_cdb[1] & 0x1f);
1046 return TCM_INVALID_CDB_FIELD;
1048 size = (cdb[10] << 24) | (cdb[11] << 16) |
1049 (cdb[12] << 8) | cdb[13];
1051 case SYNCHRONIZE_CACHE:
1052 case SYNCHRONIZE_CACHE_16:
1053 if (cdb[0] == SYNCHRONIZE_CACHE) {
1054 sectors = transport_get_sectors_10(cdb);
1055 cmd->t_task_lba = transport_lba_32(cdb);
1057 sectors = transport_get_sectors_16(cdb);
1058 cmd->t_task_lba = transport_lba_64(cdb);
1060 if (ops->execute_sync_cache) {
1061 cmd->execute_cmd = ops->execute_sync_cache;
1065 cmd->execute_cmd = sbc_emulate_noop;
1068 if (!ops->execute_unmap)
1069 return TCM_UNSUPPORTED_SCSI_OPCODE;
1071 if (!dev->dev_attrib.emulate_tpu) {
1072 pr_err("Got UNMAP, but backend device has"
1073 " emulate_tpu disabled\n");
1074 return TCM_UNSUPPORTED_SCSI_OPCODE;
1076 size = get_unaligned_be16(&cdb[7]);
1077 cmd->execute_cmd = sbc_execute_unmap;
1080 sectors = transport_get_sectors_16(cdb);
1082 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
1083 return TCM_INVALID_CDB_FIELD;
1086 size = sbc_get_size(cmd, 1);
1087 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
1089 ret = sbc_setup_write_same(cmd, &cdb[1], ops);
1094 sectors = transport_get_sectors_10(cdb);
1096 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
1097 return TCM_INVALID_CDB_FIELD;
1100 size = sbc_get_size(cmd, 1);
1101 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
1104 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
1105 * of byte 1 bit 3 UNMAP instead of original reserved field
1107 ret = sbc_setup_write_same(cmd, &cdb[1], ops);
1114 if (cdb[0] == VERIFY) {
1115 sectors = transport_get_sectors_10(cdb);
1116 cmd->t_task_lba = transport_lba_32(cdb);
1118 sectors = transport_get_sectors_16(cdb);
1119 cmd->t_task_lba = transport_lba_64(cdb);
1121 cmd->execute_cmd = sbc_emulate_noop;
1127 * There are still clients out there which use these old SCSI-2
1128 * commands. This mainly happens when running VMs with legacy
1129 * guest systems, connected via SCSI command pass-through to
1130 * iSCSI targets. Make them happy and return status GOOD.
1133 cmd->execute_cmd = sbc_emulate_noop;
1137 cmd->execute_cmd = sbc_emulate_startstop;
1140 ret = spc_parse_cdb(cmd, &size);
1145 /* reject any command that we don't have a handler for */
1146 if (!cmd->execute_cmd)
1147 return TCM_UNSUPPORTED_SCSI_OPCODE;
1149 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
1150 unsigned long long end_lba;
1152 end_lba = dev->transport->get_blocks(dev) + 1;
1153 if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
1154 ((cmd->t_task_lba + sectors) > end_lba)) {
1155 pr_err("cmd exceeds last lba %llu "
1156 "(lba %llu, sectors %u)\n",
1157 end_lba, cmd->t_task_lba, sectors);
1158 return TCM_ADDRESS_OUT_OF_RANGE;
1161 if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE))
1162 size = sbc_get_size(cmd, sectors);
1165 return target_cmd_size_check(cmd, size);
1167 EXPORT_SYMBOL(sbc_parse_cdb);
1169 u32 sbc_get_device_type(struct se_device *dev)
1173 EXPORT_SYMBOL(sbc_get_device_type);
1175 static sense_reason_t
1176 sbc_execute_unmap(struct se_cmd *cmd)
1178 struct sbc_ops *ops = cmd->protocol_data;
1179 struct se_device *dev = cmd->se_dev;
1180 unsigned char *buf, *ptr = NULL;
1184 sense_reason_t ret = 0;
1187 /* We never set ANC_SUP */
1188 if (cmd->t_task_cdb[1])
1189 return TCM_INVALID_CDB_FIELD;
1191 if (cmd->data_length == 0) {
1192 target_complete_cmd(cmd, SAM_STAT_GOOD);
1196 if (cmd->data_length < 8) {
1197 pr_warn("UNMAP parameter list length %u too small\n",
1199 return TCM_PARAMETER_LIST_LENGTH_ERROR;
1202 buf = transport_kmap_data_sg(cmd);
1204 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1206 dl = get_unaligned_be16(&buf[0]);
1207 bd_dl = get_unaligned_be16(&buf[2]);
1209 size = cmd->data_length - 8;
1211 pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
1212 cmd->data_length, bd_dl);
1216 if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
1217 ret = TCM_INVALID_PARAMETER_LIST;
1221 /* First UNMAP block descriptor starts at 8 byte offset */
1223 pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
1224 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
1226 while (size >= 16) {
1227 lba = get_unaligned_be64(&ptr[0]);
1228 range = get_unaligned_be32(&ptr[8]);
1229 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
1230 (unsigned long long)lba, range);
1232 if (range > dev->dev_attrib.max_unmap_lba_count) {
1233 ret = TCM_INVALID_PARAMETER_LIST;
1237 if (lba + range > dev->transport->get_blocks(dev) + 1) {
1238 ret = TCM_ADDRESS_OUT_OF_RANGE;
1242 ret = ops->execute_unmap(cmd, lba, range);
1251 transport_kunmap_data_sg(cmd);
1253 target_complete_cmd(cmd, GOOD);
1258 sbc_dif_generate(struct se_cmd *cmd)
1260 struct se_device *dev = cmd->se_dev;
1261 struct t10_pi_tuple *sdt;
1262 struct scatterlist *dsg = cmd->t_data_sg, *psg;
1263 sector_t sector = cmd->t_task_lba;
1264 void *daddr, *paddr;
1265 int i, j, offset = 0;
1266 unsigned int block_size = dev->dev_attrib.block_size;
1268 for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
1269 paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1270 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1272 for (j = 0; j < psg->length;
1273 j += sizeof(*sdt)) {
1277 if (offset >= dsg->length) {
1278 offset -= dsg->length;
1279 kunmap_atomic(daddr - dsg->offset);
1282 kunmap_atomic(paddr - psg->offset);
1285 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1289 avail = min(block_size, dsg->length - offset);
1290 crc = crc_t10dif(daddr + offset, avail);
1291 if (avail < block_size) {
1292 kunmap_atomic(daddr - dsg->offset);
1295 kunmap_atomic(paddr - psg->offset);
1298 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1299 offset = block_size - avail;
1300 crc = crc_t10dif_update(crc, daddr, offset);
1302 offset += block_size;
1305 sdt->guard_tag = cpu_to_be16(crc);
1306 if (cmd->prot_type == TARGET_DIF_TYPE1_PROT)
1307 sdt->ref_tag = cpu_to_be32(sector & 0xffffffff);
1310 pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x"
1311 " app_tag: 0x%04x ref_tag: %u\n",
1312 (cmd->data_direction == DMA_TO_DEVICE) ?
1313 "WRITE" : "READ", (unsigned long long)sector,
1314 sdt->guard_tag, sdt->app_tag,
1315 be32_to_cpu(sdt->ref_tag));
1320 kunmap_atomic(daddr - dsg->offset);
1321 kunmap_atomic(paddr - psg->offset);
1325 static sense_reason_t
1326 sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt,
1327 __u16 crc, sector_t sector, unsigned int ei_lba)
1331 if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
1334 csum = cpu_to_be16(crc);
1336 if (sdt->guard_tag != csum) {
1337 pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x"
1338 " csum 0x%04x\n", (unsigned long long)sector,
1339 be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
1340 return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1344 if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG))
1347 if (cmd->prot_type == TARGET_DIF_TYPE1_PROT &&
1348 be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
1349 pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x"
1350 " sector MSB: 0x%08x\n", (unsigned long long)sector,
1351 be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff));
1352 return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1355 if (cmd->prot_type == TARGET_DIF_TYPE2_PROT &&
1356 be32_to_cpu(sdt->ref_tag) != ei_lba) {
1357 pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x"
1358 " ei_lba: 0x%08x\n", (unsigned long long)sector,
1359 be32_to_cpu(sdt->ref_tag), ei_lba);
1360 return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1366 void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read,
1367 struct scatterlist *sg, int sg_off)
1369 struct se_device *dev = cmd->se_dev;
1370 struct scatterlist *psg;
1372 unsigned int i, len, left;
1373 unsigned int offset = sg_off;
1378 left = sectors * dev->prot_length;
1380 for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
1381 unsigned int psg_len, copied = 0;
1383 paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1384 psg_len = min(left, psg->length);
1386 len = min(psg_len, sg->length - offset);
1387 addr = kmap_atomic(sg_page(sg)) + sg->offset + offset;
1390 memcpy(paddr + copied, addr, len);
1392 memcpy(addr, paddr + copied, len);
1399 kunmap_atomic(addr - sg->offset - offset);
1401 if (offset >= sg->length) {
1406 kunmap_atomic(paddr - psg->offset);
1409 EXPORT_SYMBOL(sbc_dif_copy_prot);
1412 sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors,
1413 unsigned int ei_lba, struct scatterlist *psg, int psg_off)
1415 struct se_device *dev = cmd->se_dev;
1416 struct t10_pi_tuple *sdt;
1417 struct scatterlist *dsg = cmd->t_data_sg;
1418 sector_t sector = start;
1419 void *daddr, *paddr;
1423 unsigned int block_size = dev->dev_attrib.block_size;
1425 for (; psg && sector < start + sectors; psg = sg_next(psg)) {
1426 paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1427 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1429 for (i = psg_off; i < psg->length &&
1430 sector < start + sectors;
1431 i += sizeof(*sdt)) {
1435 if (dsg_off >= dsg->length) {
1436 dsg_off -= dsg->length;
1437 kunmap_atomic(daddr - dsg->offset);
1440 kunmap_atomic(paddr - psg->offset);
1443 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1448 pr_debug("DIF READ sector: %llu guard_tag: 0x%04x"
1449 " app_tag: 0x%04x ref_tag: %u\n",
1450 (unsigned long long)sector, sdt->guard_tag,
1451 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
1453 if (sdt->app_tag == cpu_to_be16(0xffff)) {
1454 dsg_off += block_size;
1458 avail = min(block_size, dsg->length - dsg_off);
1459 crc = crc_t10dif(daddr + dsg_off, avail);
1460 if (avail < block_size) {
1461 kunmap_atomic(daddr - dsg->offset);
1464 kunmap_atomic(paddr - psg->offset);
1467 daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1468 dsg_off = block_size - avail;
1469 crc = crc_t10dif_update(crc, daddr, dsg_off);
1471 dsg_off += block_size;
1474 rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba);
1476 kunmap_atomic(daddr - dsg->offset);
1477 kunmap_atomic(paddr - psg->offset);
1478 cmd->bad_sector = sector;
1487 kunmap_atomic(daddr - dsg->offset);
1488 kunmap_atomic(paddr - psg->offset);
1493 EXPORT_SYMBOL(sbc_dif_verify);