11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2d6e0175cSChristoph Hellwig /* 3d6e0175cSChristoph Hellwig * SCSI Block Commands (SBC) parsing and emulation. 4d6e0175cSChristoph Hellwig * 54c76251eSNicholas Bellinger * (c) Copyright 2002-2013 Datera, Inc. 6d6e0175cSChristoph Hellwig * 7d6e0175cSChristoph Hellwig * Nicholas A. Bellinger <nab@kernel.org> 8d6e0175cSChristoph Hellwig */ 9d6e0175cSChristoph Hellwig 10d6e0175cSChristoph Hellwig #include <linux/kernel.h> 11d6e0175cSChristoph Hellwig #include <linux/module.h> 12d6e0175cSChristoph Hellwig #include <linux/ratelimit.h> 1341861fa8SNicholas Bellinger #include <linux/crc-t10dif.h> 148dcf07beSBart Van Assche #include <linux/t10-pi.h> 15d6e0175cSChristoph Hellwig #include <asm/unaligned.h> 16ba929992SBart Van Assche #include <scsi/scsi_proto.h> 1768ff9b9bSNicholas Bellinger #include <scsi/scsi_tcq.h> 18d6e0175cSChristoph Hellwig 19d6e0175cSChristoph Hellwig #include <target/target_core_base.h> 20d6e0175cSChristoph Hellwig #include <target/target_core_backend.h> 21d6e0175cSChristoph Hellwig #include <target/target_core_fabric.h> 22d6e0175cSChristoph Hellwig 23d6e0175cSChristoph Hellwig #include "target_core_internal.h" 24d6e0175cSChristoph Hellwig #include "target_core_ua.h" 25c66094bfSHannes Reinecke #include "target_core_alua.h" 26d6e0175cSChristoph Hellwig 27de103c93SChristoph Hellwig static sense_reason_t 286d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char, u32, bool); 2962e46942SChristoph Hellwig static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd); 30afd73f1bSNicholas Bellinger 31afd73f1bSNicholas Bellinger static sense_reason_t 32de103c93SChristoph Hellwig sbc_emulate_readcapacity(struct se_cmd *cmd) 331fd032eeSChristoph Hellwig { 341fd032eeSChristoph Hellwig struct se_device *dev = cmd->se_dev; 358dc8632aSRoland Dreier unsigned char *cdb = cmd->t_task_cdb; 361fd032eeSChristoph Hellwig unsigned long long blocks_long = dev->transport->get_blocks(dev); 37a50da144SPaolo Bonzini unsigned char *rbuf; 38a50da144SPaolo Bonzini unsigned char buf[8]; 391fd032eeSChristoph Hellwig u32 blocks; 401fd032eeSChristoph Hellwig 418dc8632aSRoland Dreier /* 428dc8632aSRoland Dreier * SBC-2 says: 438dc8632aSRoland Dreier * If the PMI bit is set to zero and the LOGICAL BLOCK 448dc8632aSRoland Dreier * ADDRESS field is not set to zero, the device server shall 458dc8632aSRoland Dreier * terminate the command with CHECK CONDITION status with 468dc8632aSRoland Dreier * the sense key set to ILLEGAL REQUEST and the additional 478dc8632aSRoland Dreier * sense code set to INVALID FIELD IN CDB. 488dc8632aSRoland Dreier * 498dc8632aSRoland Dreier * In SBC-3, these fields are obsolete, but some SCSI 508dc8632aSRoland Dreier * compliance tests actually check this, so we might as well 518dc8632aSRoland Dreier * follow SBC-2. 528dc8632aSRoland Dreier */ 538dc8632aSRoland Dreier if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5])) 548dc8632aSRoland Dreier return TCM_INVALID_CDB_FIELD; 558dc8632aSRoland Dreier 561fd032eeSChristoph Hellwig if (blocks_long >= 0x00000000ffffffff) 571fd032eeSChristoph Hellwig blocks = 0xffffffff; 581fd032eeSChristoph Hellwig else 591fd032eeSChristoph Hellwig blocks = (u32)blocks_long; 601fd032eeSChristoph Hellwig 61a85d667eSBart Van Assche put_unaligned_be32(blocks, &buf[0]); 62a85d667eSBart Van Assche put_unaligned_be32(dev->dev_attrib.block_size, &buf[4]); 631fd032eeSChristoph Hellwig 64a50da144SPaolo Bonzini rbuf = transport_kmap_data_sg(cmd); 658b4b0dcbSNicholas Bellinger if (rbuf) { 66a50da144SPaolo Bonzini memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 671fd032eeSChristoph Hellwig transport_kunmap_data_sg(cmd); 688b4b0dcbSNicholas Bellinger } 691fd032eeSChristoph Hellwig 7014b40c1eSHannes Reinecke target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 8); 711fd032eeSChristoph Hellwig return 0; 721fd032eeSChristoph Hellwig } 731fd032eeSChristoph Hellwig 74de103c93SChristoph Hellwig static sense_reason_t 75de103c93SChristoph Hellwig sbc_emulate_readcapacity_16(struct se_cmd *cmd) 761fd032eeSChristoph Hellwig { 771fd032eeSChristoph Hellwig struct se_device *dev = cmd->se_dev; 782d335983SNicholas Bellinger struct se_session *sess = cmd->se_sess; 799ef5466eSNicholas Bellinger int pi_prot_type = dev->dev_attrib.pi_prot_type; 809ef5466eSNicholas Bellinger 81a50da144SPaolo Bonzini unsigned char *rbuf; 82a50da144SPaolo Bonzini unsigned char buf[32]; 831fd032eeSChristoph Hellwig unsigned long long blocks = dev->transport->get_blocks(dev); 841fd032eeSChristoph Hellwig 85a50da144SPaolo Bonzini memset(buf, 0, sizeof(buf)); 86a85d667eSBart Van Assche put_unaligned_be64(blocks, &buf[0]); 87a85d667eSBart Van Assche put_unaligned_be32(dev->dev_attrib.block_size, &buf[8]); 8856dac14cSNicholas Bellinger /* 8956dac14cSNicholas Bellinger * Set P_TYPE and PROT_EN bits for DIF support 9056dac14cSNicholas Bellinger */ 912d335983SNicholas Bellinger if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { 929ef5466eSNicholas Bellinger /* 939ef5466eSNicholas Bellinger * Only override a device's pi_prot_type if no T10-PI is 949ef5466eSNicholas Bellinger * available, and sess_prot_type has been explicitly enabled. 959ef5466eSNicholas Bellinger */ 969ef5466eSNicholas Bellinger if (!pi_prot_type) 979ef5466eSNicholas Bellinger pi_prot_type = sess->sess_prot_type; 989ef5466eSNicholas Bellinger 999ef5466eSNicholas Bellinger if (pi_prot_type) 1009ef5466eSNicholas Bellinger buf[12] = (pi_prot_type - 1) << 1 | 0x1; 1012d335983SNicholas Bellinger } 1027f7caf6aSAndy Grover 1037f7caf6aSAndy Grover if (dev->transport->get_lbppbe) 1047f7caf6aSAndy Grover buf[13] = dev->transport->get_lbppbe(dev) & 0x0f; 1057f7caf6aSAndy Grover 1067f7caf6aSAndy Grover if (dev->transport->get_alignment_offset_lbas) { 1077f7caf6aSAndy Grover u16 lalba = dev->transport->get_alignment_offset_lbas(dev); 108a85d667eSBart Van Assche 109a85d667eSBart Van Assche put_unaligned_be16(lalba, &buf[14]); 1107f7caf6aSAndy Grover } 1117f7caf6aSAndy Grover 1121fd032eeSChristoph Hellwig /* 1131fd032eeSChristoph Hellwig * Set Thin Provisioning Enable bit following sbc3r22 in section 1141fd032eeSChristoph Hellwig * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled. 1151fd032eeSChristoph Hellwig */ 116e6f41633SJamie Pocas if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) { 1177f7caf6aSAndy Grover buf[14] |= 0x80; 1181fd032eeSChristoph Hellwig 119e6f41633SJamie Pocas /* 120e6f41633SJamie Pocas * LBPRZ signifies that zeroes will be read back from an LBA after 121e6f41633SJamie Pocas * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2) 122e6f41633SJamie Pocas */ 123e6f41633SJamie Pocas if (dev->dev_attrib.unmap_zeroes_data) 124e6f41633SJamie Pocas buf[14] |= 0x40; 125e6f41633SJamie Pocas } 126e6f41633SJamie Pocas 127a50da144SPaolo Bonzini rbuf = transport_kmap_data_sg(cmd); 1288b4b0dcbSNicholas Bellinger if (rbuf) { 129a50da144SPaolo Bonzini memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 1301fd032eeSChristoph Hellwig transport_kunmap_data_sg(cmd); 1318b4b0dcbSNicholas Bellinger } 1321fd032eeSChristoph Hellwig 13314b40c1eSHannes Reinecke target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 32); 1341fd032eeSChristoph Hellwig return 0; 1351fd032eeSChristoph Hellwig } 1361fd032eeSChristoph Hellwig 13745182ed5SBrian Bunker static sense_reason_t 13845182ed5SBrian Bunker sbc_emulate_startstop(struct se_cmd *cmd) 13945182ed5SBrian Bunker { 14045182ed5SBrian Bunker unsigned char *cdb = cmd->t_task_cdb; 14145182ed5SBrian Bunker 14245182ed5SBrian Bunker /* 14345182ed5SBrian Bunker * See sbc3r36 section 5.25 14445182ed5SBrian Bunker * Immediate bit should be set since there is nothing to complete 14545182ed5SBrian Bunker * POWER CONDITION MODIFIER 0h 14645182ed5SBrian Bunker */ 14745182ed5SBrian Bunker if (!(cdb[1] & 1) || cdb[2] || cdb[3]) 14845182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 14945182ed5SBrian Bunker 15045182ed5SBrian Bunker /* 15145182ed5SBrian Bunker * See sbc3r36 section 5.25 15245182ed5SBrian Bunker * POWER CONDITION 0h START_VALID - process START and LOEJ 15345182ed5SBrian Bunker */ 15445182ed5SBrian Bunker if (cdb[4] >> 4 & 0xf) 15545182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 15645182ed5SBrian Bunker 15745182ed5SBrian Bunker /* 15845182ed5SBrian Bunker * See sbc3r36 section 5.25 15945182ed5SBrian Bunker * LOEJ 0h - nothing to load or unload 16045182ed5SBrian Bunker * START 1h - we are ready 16145182ed5SBrian Bunker */ 16245182ed5SBrian Bunker if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4)) 16345182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 16445182ed5SBrian Bunker 16545182ed5SBrian Bunker target_complete_cmd(cmd, SAM_STAT_GOOD); 16645182ed5SBrian Bunker return 0; 16745182ed5SBrian Bunker } 16845182ed5SBrian Bunker 169972b29c8SRoland Dreier sector_t sbc_get_write_same_sectors(struct se_cmd *cmd) 1701fd032eeSChristoph Hellwig { 1711fd032eeSChristoph Hellwig u32 num_blocks; 1721fd032eeSChristoph Hellwig 1731fd032eeSChristoph Hellwig if (cmd->t_task_cdb[0] == WRITE_SAME) 1741fd032eeSChristoph Hellwig num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]); 1751fd032eeSChristoph Hellwig else if (cmd->t_task_cdb[0] == WRITE_SAME_16) 1761fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]); 1771fd032eeSChristoph Hellwig else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */ 1781fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]); 1791fd032eeSChristoph Hellwig 1801fd032eeSChristoph Hellwig /* 1811fd032eeSChristoph Hellwig * Use the explicit range when non zero is supplied, otherwise calculate 1821fd032eeSChristoph Hellwig * the remaining range based on ->get_blocks() - starting LBA. 1831fd032eeSChristoph Hellwig */ 1846f974e8cSChristoph Hellwig if (num_blocks) 1856f974e8cSChristoph Hellwig return num_blocks; 1861fd032eeSChristoph Hellwig 1876f974e8cSChristoph Hellwig return cmd->se_dev->transport->get_blocks(cmd->se_dev) - 1886f974e8cSChristoph Hellwig cmd->t_task_lba + 1; 1891fd032eeSChristoph Hellwig } 190972b29c8SRoland Dreier EXPORT_SYMBOL(sbc_get_write_same_sectors); 1911fd032eeSChristoph Hellwig 192de103c93SChristoph Hellwig static sense_reason_t 193b753d643SChristoph Hellwig sbc_execute_write_same_unmap(struct se_cmd *cmd) 194b753d643SChristoph Hellwig { 195b753d643SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 196b753d643SChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 197b753d643SChristoph Hellwig sense_reason_t ret; 198b753d643SChristoph Hellwig 199b753d643SChristoph Hellwig if (nolb) { 200b753d643SChristoph Hellwig ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb); 201b753d643SChristoph Hellwig if (ret) 202b753d643SChristoph Hellwig return ret; 203b753d643SChristoph Hellwig } 204b753d643SChristoph Hellwig 20514b40c1eSHannes Reinecke target_complete_cmd(cmd, SAM_STAT_GOOD); 206b753d643SChristoph Hellwig return 0; 207b753d643SChristoph Hellwig } 208b753d643SChristoph Hellwig 209b753d643SChristoph Hellwig static sense_reason_t 2101920ed61SNicholas Bellinger sbc_emulate_noop(struct se_cmd *cmd) 2111a1ff38cSBernhard Kohl { 21214b40c1eSHannes Reinecke target_complete_cmd(cmd, SAM_STAT_GOOD); 2131a1ff38cSBernhard Kohl return 0; 2141a1ff38cSBernhard Kohl } 2151a1ff38cSBernhard Kohl 216d6e0175cSChristoph Hellwig static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) 217d6e0175cSChristoph Hellwig { 2180fd97ccfSChristoph Hellwig return cmd->se_dev->dev_attrib.block_size * sectors; 219d6e0175cSChristoph Hellwig } 220d6e0175cSChristoph Hellwig 221d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_6(unsigned char *cdb) 222d6e0175cSChristoph Hellwig { 223d6e0175cSChristoph Hellwig /* 224d6e0175cSChristoph Hellwig * Use 8-bit sector value. SBC-3 says: 225d6e0175cSChristoph Hellwig * 226d6e0175cSChristoph Hellwig * A TRANSFER LENGTH field set to zero specifies that 256 227d6e0175cSChristoph Hellwig * logical blocks shall be written. Any other value 228d6e0175cSChristoph Hellwig * specifies the number of logical blocks that shall be 229d6e0175cSChristoph Hellwig * written. 230d6e0175cSChristoph Hellwig */ 231d6e0175cSChristoph Hellwig return cdb[4] ? : 256; 232d6e0175cSChristoph Hellwig } 233d6e0175cSChristoph Hellwig 234d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_10(unsigned char *cdb) 235d6e0175cSChristoph Hellwig { 236a85d667eSBart Van Assche return get_unaligned_be16(&cdb[7]); 237d6e0175cSChristoph Hellwig } 238d6e0175cSChristoph Hellwig 239d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_12(unsigned char *cdb) 240d6e0175cSChristoph Hellwig { 241a85d667eSBart Van Assche return get_unaligned_be32(&cdb[6]); 242d6e0175cSChristoph Hellwig } 243d6e0175cSChristoph Hellwig 244d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_16(unsigned char *cdb) 245d6e0175cSChristoph Hellwig { 246a85d667eSBart Van Assche return get_unaligned_be32(&cdb[10]); 247d6e0175cSChristoph Hellwig } 248d6e0175cSChristoph Hellwig 249d6e0175cSChristoph Hellwig /* 250d6e0175cSChristoph Hellwig * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants 251d6e0175cSChristoph Hellwig */ 252d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_32(unsigned char *cdb) 253d6e0175cSChristoph Hellwig { 254a85d667eSBart Van Assche return get_unaligned_be32(&cdb[28]); 255d6e0175cSChristoph Hellwig 256d6e0175cSChristoph Hellwig } 257d6e0175cSChristoph Hellwig 258d6e0175cSChristoph Hellwig static inline u32 transport_lba_21(unsigned char *cdb) 259d6e0175cSChristoph Hellwig { 260a85d667eSBart Van Assche return get_unaligned_be24(&cdb[1]) & 0x1fffff; 261d6e0175cSChristoph Hellwig } 262d6e0175cSChristoph Hellwig 263d6e0175cSChristoph Hellwig static inline u32 transport_lba_32(unsigned char *cdb) 264d6e0175cSChristoph Hellwig { 265a85d667eSBart Van Assche return get_unaligned_be32(&cdb[2]); 266d6e0175cSChristoph Hellwig } 267d6e0175cSChristoph Hellwig 268d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64(unsigned char *cdb) 269d6e0175cSChristoph Hellwig { 270a85d667eSBart Van Assche return get_unaligned_be64(&cdb[2]); 271d6e0175cSChristoph Hellwig } 272d6e0175cSChristoph Hellwig 273d6e0175cSChristoph Hellwig /* 274d6e0175cSChristoph Hellwig * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs 275d6e0175cSChristoph Hellwig */ 276d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) 277d6e0175cSChristoph Hellwig { 278a85d667eSBart Van Assche return get_unaligned_be64(&cdb[12]); 279d6e0175cSChristoph Hellwig } 280d6e0175cSChristoph Hellwig 281cd063befSNicholas Bellinger static sense_reason_t 2826d8e7e7cSDmitry Bogdanov sbc_setup_write_same(struct se_cmd *cmd, unsigned char flags, struct sbc_ops *ops) 283d6e0175cSChristoph Hellwig { 2848e575c50SNicholas Bellinger struct se_device *dev = cmd->se_dev; 2858e575c50SNicholas Bellinger sector_t end_lba = dev->transport->get_blocks(dev) + 1; 286972b29c8SRoland Dreier unsigned int sectors = sbc_get_write_same_sectors(cmd); 287afd73f1bSNicholas Bellinger sense_reason_t ret; 288773cbaf7SNicholas Bellinger 2896d8e7e7cSDmitry Bogdanov if ((flags & 0x04) || (flags & 0x02)) { 290d6e0175cSChristoph Hellwig pr_err("WRITE_SAME PBDATA and LBDATA" 291d6e0175cSChristoph Hellwig " bits not supported for Block Discard" 292d6e0175cSChristoph Hellwig " Emulation\n"); 293cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 294d6e0175cSChristoph Hellwig } 295773cbaf7SNicholas Bellinger if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) { 296773cbaf7SNicholas Bellinger pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n", 297773cbaf7SNicholas Bellinger sectors, cmd->se_dev->dev_attrib.max_write_same_len); 298773cbaf7SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 299773cbaf7SNicholas Bellinger } 3008e575c50SNicholas Bellinger /* 3018e575c50SNicholas Bellinger * Sanity check for LBA wrap and request past end of device. 3028e575c50SNicholas Bellinger */ 3038e575c50SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 3048e575c50SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 3058e575c50SNicholas Bellinger pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n", 3068e575c50SNicholas Bellinger (unsigned long long)end_lba, cmd->t_task_lba, sectors); 3078e575c50SNicholas Bellinger return TCM_ADDRESS_OUT_OF_RANGE; 3088e575c50SNicholas Bellinger } 3098e575c50SNicholas Bellinger 3105cb770bfSRoland Dreier /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */ 3116d8e7e7cSDmitry Bogdanov if (flags & 0x10) { 3125cb770bfSRoland Dreier pr_warn("WRITE SAME with ANCHOR not supported\n"); 3135cb770bfSRoland Dreier return TCM_INVALID_CDB_FIELD; 3145cb770bfSRoland Dreier } 315*ccd3f449SMike Christie 316*ccd3f449SMike Christie if (flags & 0x01) { 317*ccd3f449SMike Christie pr_warn("WRITE SAME with NDOB not supported\n"); 318*ccd3f449SMike Christie return TCM_INVALID_CDB_FIELD; 319*ccd3f449SMike Christie } 320*ccd3f449SMike Christie 321d6e0175cSChristoph Hellwig /* 322cd063befSNicholas Bellinger * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting 323cd063befSNicholas Bellinger * translated into block discard requests within backend code. 324d6e0175cSChristoph Hellwig */ 3256d8e7e7cSDmitry Bogdanov if (flags & 0x08) { 326b753d643SChristoph Hellwig if (!ops->execute_unmap) 327cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 328d6e0175cSChristoph Hellwig 329d0a91295SNicholas Bellinger if (!dev->dev_attrib.emulate_tpws) { 330d0a91295SNicholas Bellinger pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device" 331d0a91295SNicholas Bellinger " has emulate_tpws disabled\n"); 332d0a91295SNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 333d0a91295SNicholas Bellinger } 334b753d643SChristoph Hellwig cmd->execute_cmd = sbc_execute_write_same_unmap; 335cd063befSNicholas Bellinger return 0; 336cd063befSNicholas Bellinger } 337cd063befSNicholas Bellinger if (!ops->execute_write_same) 338cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 339cd063befSNicholas Bellinger 3406d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, flags >> 5, sectors, true); 341afd73f1bSNicholas Bellinger if (ret) 342afd73f1bSNicholas Bellinger return ret; 343afd73f1bSNicholas Bellinger 344cd063befSNicholas Bellinger cmd->execute_cmd = ops->execute_write_same; 345d6e0175cSChristoph Hellwig return 0; 346d6e0175cSChristoph Hellwig } 347d6e0175cSChristoph Hellwig 348057085e5SNicholas Bellinger static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success, 349057085e5SNicholas Bellinger int *post_ret) 350d6e0175cSChristoph Hellwig { 351d6e0175cSChristoph Hellwig unsigned char *buf, *addr; 352d6e0175cSChristoph Hellwig struct scatterlist *sg; 353d6e0175cSChristoph Hellwig unsigned int offset; 354a6b0133cSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 355a6b0133cSNicholas Bellinger int i, count; 356aa73237dSBart Van Assche 357aa73237dSBart Van Assche if (!success) 358aa73237dSBart Van Assche return 0; 359aa73237dSBart Van Assche 360d6e0175cSChristoph Hellwig /* 361d6e0175cSChristoph Hellwig * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command 362d6e0175cSChristoph Hellwig * 363d6e0175cSChristoph Hellwig * 1) read the specified logical block(s); 364d6e0175cSChristoph Hellwig * 2) transfer logical blocks from the data-out buffer; 365d6e0175cSChristoph Hellwig * 3) XOR the logical blocks transferred from the data-out buffer with 366d6e0175cSChristoph Hellwig * the logical blocks read, storing the resulting XOR data in a buffer; 367d6e0175cSChristoph Hellwig * 4) if the DISABLE WRITE bit is set to zero, then write the logical 368d6e0175cSChristoph Hellwig * blocks transferred from the data-out buffer; and 369d6e0175cSChristoph Hellwig * 5) transfer the resulting XOR data to the data-in buffer. 370d6e0175cSChristoph Hellwig */ 371d6e0175cSChristoph Hellwig buf = kmalloc(cmd->data_length, GFP_KERNEL); 372d6e0175cSChristoph Hellwig if (!buf) { 373d6e0175cSChristoph Hellwig pr_err("Unable to allocate xor_callback buf\n"); 374a6b0133cSNicholas Bellinger return TCM_OUT_OF_RESOURCES; 375d6e0175cSChristoph Hellwig } 376d6e0175cSChristoph Hellwig /* 377d6e0175cSChristoph Hellwig * Copy the scatterlist WRITE buffer located at cmd->t_data_sg 378d6e0175cSChristoph Hellwig * into the locally allocated *buf 379d6e0175cSChristoph Hellwig */ 380d6e0175cSChristoph Hellwig sg_copy_to_buffer(cmd->t_data_sg, 381d6e0175cSChristoph Hellwig cmd->t_data_nents, 382d6e0175cSChristoph Hellwig buf, 383d6e0175cSChristoph Hellwig cmd->data_length); 384d6e0175cSChristoph Hellwig 385d6e0175cSChristoph Hellwig /* 386d6e0175cSChristoph Hellwig * Now perform the XOR against the BIDI read memory located at 387d6e0175cSChristoph Hellwig * cmd->t_mem_bidi_list 388d6e0175cSChristoph Hellwig */ 389d6e0175cSChristoph Hellwig 390d6e0175cSChristoph Hellwig offset = 0; 391d6e0175cSChristoph Hellwig for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { 392d6e0175cSChristoph Hellwig addr = kmap_atomic(sg_page(sg)); 393a6b0133cSNicholas Bellinger if (!addr) { 394a6b0133cSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 395d6e0175cSChristoph Hellwig goto out; 396a6b0133cSNicholas Bellinger } 397d6e0175cSChristoph Hellwig 398d6e0175cSChristoph Hellwig for (i = 0; i < sg->length; i++) 399d6e0175cSChristoph Hellwig *(addr + sg->offset + i) ^= *(buf + offset + i); 400d6e0175cSChristoph Hellwig 401d6e0175cSChristoph Hellwig offset += sg->length; 402d6e0175cSChristoph Hellwig kunmap_atomic(addr); 403d6e0175cSChristoph Hellwig } 404d6e0175cSChristoph Hellwig 405d6e0175cSChristoph Hellwig out: 406d6e0175cSChristoph Hellwig kfree(buf); 407a6b0133cSNicholas Bellinger return ret; 408d6e0175cSChristoph Hellwig } 409d6e0175cSChristoph Hellwig 410a82a9538SNicholas Bellinger static sense_reason_t 411a82a9538SNicholas Bellinger sbc_execute_rw(struct se_cmd *cmd) 412a82a9538SNicholas Bellinger { 4137a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 4147a971b1bSChristoph Hellwig 4157a971b1bSChristoph Hellwig return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents, 416a82a9538SNicholas Bellinger cmd->data_direction); 417a82a9538SNicholas Bellinger } 418a82a9538SNicholas Bellinger 419057085e5SNicholas Bellinger static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success, 420057085e5SNicholas Bellinger int *post_ret) 42168ff9b9bSNicholas Bellinger { 42268ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 4239b2792c3SNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 42468ff9b9bSNicholas Bellinger 425d8855c15SNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 426aa73237dSBart Van Assche if (success) { 427057085e5SNicholas Bellinger *post_ret = 1; 4289b2792c3SNicholas Bellinger 4299b2792c3SNicholas Bellinger if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION) 4309b2792c3SNicholas Bellinger ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 431057085e5SNicholas Bellinger } 432d8855c15SNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 433d8855c15SNicholas Bellinger 43468ff9b9bSNicholas Bellinger /* 43568ff9b9bSNicholas Bellinger * Unlock ->caw_sem originally obtained during sbc_compare_and_write() 43668ff9b9bSNicholas Bellinger * before the original READ I/O submission. 43768ff9b9bSNicholas Bellinger */ 43868ff9b9bSNicholas Bellinger up(&dev->caw_sem); 43968ff9b9bSNicholas Bellinger 4409b2792c3SNicholas Bellinger return ret; 44168ff9b9bSNicholas Bellinger } 44268ff9b9bSNicholas Bellinger 443ab628b9fSDavid Disseldorp /* 444749c226cSDavid Disseldorp * compare @cmp_len bytes of @read_sgl with @cmp_sgl. On miscompare, fill 445749c226cSDavid Disseldorp * @miscmp_off and return TCM_MISCOMPARE_VERIFY. 446ab628b9fSDavid Disseldorp */ 447ab628b9fSDavid Disseldorp static sense_reason_t 448ab628b9fSDavid Disseldorp compare_and_write_do_cmp(struct scatterlist *read_sgl, unsigned int read_nents, 449ab628b9fSDavid Disseldorp struct scatterlist *cmp_sgl, unsigned int cmp_nents, 450749c226cSDavid Disseldorp unsigned int cmp_len, unsigned int *miscmp_off) 451ab628b9fSDavid Disseldorp { 452ab628b9fSDavid Disseldorp unsigned char *buf = NULL; 453ab628b9fSDavid Disseldorp struct scatterlist *sg; 454ab628b9fSDavid Disseldorp sense_reason_t ret; 455ab628b9fSDavid Disseldorp unsigned int offset; 456ab628b9fSDavid Disseldorp size_t rc; 4575cfb5b02SChaitanya Kulkarni int sg_cnt; 458ab628b9fSDavid Disseldorp 459ab628b9fSDavid Disseldorp buf = kzalloc(cmp_len, GFP_KERNEL); 460ab628b9fSDavid Disseldorp if (!buf) { 461ab628b9fSDavid Disseldorp ret = TCM_OUT_OF_RESOURCES; 462ab628b9fSDavid Disseldorp goto out; 463ab628b9fSDavid Disseldorp } 464ab628b9fSDavid Disseldorp 465ab628b9fSDavid Disseldorp rc = sg_copy_to_buffer(cmp_sgl, cmp_nents, buf, cmp_len); 466ab628b9fSDavid Disseldorp if (!rc) { 467ab628b9fSDavid Disseldorp pr_err("sg_copy_to_buffer() failed for compare_and_write\n"); 468ab628b9fSDavid Disseldorp ret = TCM_OUT_OF_RESOURCES; 469ab628b9fSDavid Disseldorp goto out; 470ab628b9fSDavid Disseldorp } 471ab628b9fSDavid Disseldorp /* 472ab628b9fSDavid Disseldorp * Compare SCSI READ payload against verify payload 473ab628b9fSDavid Disseldorp */ 474ab628b9fSDavid Disseldorp offset = 0; 475749c226cSDavid Disseldorp ret = TCM_NO_SENSE; 4765cfb5b02SChaitanya Kulkarni for_each_sg(read_sgl, sg, read_nents, sg_cnt) { 477ab628b9fSDavid Disseldorp unsigned int len = min(sg->length, cmp_len); 478ab628b9fSDavid Disseldorp unsigned char *addr = kmap_atomic(sg_page(sg)); 479ab628b9fSDavid Disseldorp 480ab628b9fSDavid Disseldorp if (memcmp(addr, buf + offset, len)) { 481749c226cSDavid Disseldorp unsigned int i; 482749c226cSDavid Disseldorp 483749c226cSDavid Disseldorp for (i = 0; i < len && addr[i] == buf[offset + i]; i++) 484749c226cSDavid Disseldorp ; 485749c226cSDavid Disseldorp *miscmp_off = offset + i; 486749c226cSDavid Disseldorp pr_warn("Detected MISCOMPARE at offset %u\n", 487749c226cSDavid Disseldorp *miscmp_off); 488ab628b9fSDavid Disseldorp ret = TCM_MISCOMPARE_VERIFY; 489ab628b9fSDavid Disseldorp } 490ab628b9fSDavid Disseldorp kunmap_atomic(addr); 491749c226cSDavid Disseldorp if (ret != TCM_NO_SENSE) 492749c226cSDavid Disseldorp goto out; 493ab628b9fSDavid Disseldorp 494ab628b9fSDavid Disseldorp offset += len; 495ab628b9fSDavid Disseldorp cmp_len -= len; 496ab628b9fSDavid Disseldorp if (!cmp_len) 497ab628b9fSDavid Disseldorp break; 498ab628b9fSDavid Disseldorp } 499ab628b9fSDavid Disseldorp pr_debug("COMPARE AND WRITE read data matches compare data\n"); 500ab628b9fSDavid Disseldorp out: 501ab628b9fSDavid Disseldorp kfree(buf); 502ab628b9fSDavid Disseldorp return ret; 503ab628b9fSDavid Disseldorp } 504ab628b9fSDavid Disseldorp 505057085e5SNicholas Bellinger static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success, 506057085e5SNicholas Bellinger int *post_ret) 50768ff9b9bSNicholas Bellinger { 50868ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 50981b6ca6dSBart Van Assche struct sg_table write_tbl = { }; 510ab628b9fSDavid Disseldorp struct scatterlist *write_sg; 51168ff9b9bSNicholas Bellinger struct sg_mapping_iter m; 512ab628b9fSDavid Disseldorp unsigned int len; 51368ff9b9bSNicholas Bellinger unsigned int block_size = dev->dev_attrib.block_size; 514ab628b9fSDavid Disseldorp unsigned int compare_len = (cmd->t_task_nolb * block_size); 515749c226cSDavid Disseldorp unsigned int miscmp_off = 0; 51668ff9b9bSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 517ab628b9fSDavid Disseldorp int i; 51868ff9b9bSNicholas Bellinger 519cf6d1f09SNicholas Bellinger /* 520cf6d1f09SNicholas Bellinger * Handle early failure in transport_generic_request_failure(), 521c8e63985SNicholas Bellinger * which will not have taken ->caw_sem yet.. 522cf6d1f09SNicholas Bellinger */ 523c8e63985SNicholas Bellinger if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg)) 524cf6d1f09SNicholas Bellinger return TCM_NO_SENSE; 525db60df88SNicholas Bellinger /* 526c8e63985SNicholas Bellinger * Handle special case for zero-length COMPARE_AND_WRITE 527c8e63985SNicholas Bellinger */ 528c8e63985SNicholas Bellinger if (!cmd->data_length) 529c8e63985SNicholas Bellinger goto out; 530c8e63985SNicholas Bellinger /* 531db60df88SNicholas Bellinger * Immediately exit + release dev->caw_sem if command has already 532db60df88SNicholas Bellinger * been failed with a non-zero SCSI status. 533db60df88SNicholas Bellinger */ 534db60df88SNicholas Bellinger if (cmd->scsi_status) { 535a71a5dc7SNicholas Bellinger pr_debug("compare_and_write_callback: non zero scsi_status:" 536db60df88SNicholas Bellinger " 0x%02x\n", cmd->scsi_status); 537a71a5dc7SNicholas Bellinger *post_ret = 1; 538a71a5dc7SNicholas Bellinger if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION) 539a71a5dc7SNicholas Bellinger ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 540db60df88SNicholas Bellinger goto out; 541db60df88SNicholas Bellinger } 542cf6d1f09SNicholas Bellinger 543ab628b9fSDavid Disseldorp ret = compare_and_write_do_cmp(cmd->t_bidi_data_sg, 544ab628b9fSDavid Disseldorp cmd->t_bidi_data_nents, 545ab628b9fSDavid Disseldorp cmd->t_data_sg, 546ab628b9fSDavid Disseldorp cmd->t_data_nents, 547749c226cSDavid Disseldorp compare_len, 548749c226cSDavid Disseldorp &miscmp_off); 549749c226cSDavid Disseldorp if (ret == TCM_MISCOMPARE_VERIFY) { 550749c226cSDavid Disseldorp /* 551749c226cSDavid Disseldorp * SBC-4 r15: 5.3 COMPARE AND WRITE command 552749c226cSDavid Disseldorp * In the sense data (see 4.18 and SPC-5) the offset from the 553749c226cSDavid Disseldorp * start of the Data-Out Buffer to the first byte of data that 554749c226cSDavid Disseldorp * was not equal shall be reported in the INFORMATION field. 555749c226cSDavid Disseldorp */ 556749c226cSDavid Disseldorp cmd->sense_info = miscmp_off; 557749c226cSDavid Disseldorp goto out; 558749c226cSDavid Disseldorp } else if (ret) 559a2890087SNicholas Bellinger goto out; 56068ff9b9bSNicholas Bellinger 56181b6ca6dSBart Van Assche if (sg_alloc_table(&write_tbl, cmd->t_data_nents, GFP_KERNEL) < 0) { 56268ff9b9bSNicholas Bellinger pr_err("Unable to allocate compare_and_write sg\n"); 56368ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 56468ff9b9bSNicholas Bellinger goto out; 56568ff9b9bSNicholas Bellinger } 56681b6ca6dSBart Van Assche write_sg = write_tbl.sgl; 56768ff9b9bSNicholas Bellinger 56868ff9b9bSNicholas Bellinger i = 0; 569ab628b9fSDavid Disseldorp len = compare_len; 57068ff9b9bSNicholas Bellinger sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG); 57168ff9b9bSNicholas Bellinger /* 57268ff9b9bSNicholas Bellinger * Currently assumes NoLB=1 and SGLs are PAGE_SIZE.. 57368ff9b9bSNicholas Bellinger */ 57468ff9b9bSNicholas Bellinger while (len) { 57568ff9b9bSNicholas Bellinger sg_miter_next(&m); 57668ff9b9bSNicholas Bellinger 57768ff9b9bSNicholas Bellinger if (block_size < PAGE_SIZE) { 57868ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 579d94e5a61SJan Engelhardt m.piter.sg->offset + block_size); 58068ff9b9bSNicholas Bellinger } else { 58168ff9b9bSNicholas Bellinger sg_miter_next(&m); 58268ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 583d94e5a61SJan Engelhardt m.piter.sg->offset); 58468ff9b9bSNicholas Bellinger } 58568ff9b9bSNicholas Bellinger len -= block_size; 58668ff9b9bSNicholas Bellinger i++; 58768ff9b9bSNicholas Bellinger } 58868ff9b9bSNicholas Bellinger sg_miter_stop(&m); 58968ff9b9bSNicholas Bellinger /* 59068ff9b9bSNicholas Bellinger * Save the original SGL + nents values before updating to new 59168ff9b9bSNicholas Bellinger * assignments, to be released in transport_free_pages() -> 59268ff9b9bSNicholas Bellinger * transport_reset_sgl_orig() 59368ff9b9bSNicholas Bellinger */ 59468ff9b9bSNicholas Bellinger cmd->t_data_sg_orig = cmd->t_data_sg; 59568ff9b9bSNicholas Bellinger cmd->t_data_sg = write_sg; 59668ff9b9bSNicholas Bellinger cmd->t_data_nents_orig = cmd->t_data_nents; 59768ff9b9bSNicholas Bellinger cmd->t_data_nents = 1; 59868ff9b9bSNicholas Bellinger 59968d81f40SChristoph Hellwig cmd->sam_task_attr = TCM_HEAD_TAG; 60068ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_post; 60168ff9b9bSNicholas Bellinger /* 60268ff9b9bSNicholas Bellinger * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler 60368ff9b9bSNicholas Bellinger * for submitting the adjusted SGL to write instance user-data. 60468ff9b9bSNicholas Bellinger */ 60568ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 60668ff9b9bSNicholas Bellinger 60768ff9b9bSNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 60868ff9b9bSNicholas Bellinger cmd->t_state = TRANSPORT_PROCESSING; 609fd5e64deSBart Van Assche cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT; 61068ff9b9bSNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 61168ff9b9bSNicholas Bellinger 612dff0ca9eSNicholas Bellinger __target_execute_cmd(cmd, false); 61368ff9b9bSNicholas Bellinger 61468ff9b9bSNicholas Bellinger return ret; 61568ff9b9bSNicholas Bellinger 61668ff9b9bSNicholas Bellinger out: 61768ff9b9bSNicholas Bellinger /* 61868ff9b9bSNicholas Bellinger * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in 61968ff9b9bSNicholas Bellinger * sbc_compare_and_write() before the original READ I/O submission. 62068ff9b9bSNicholas Bellinger */ 62168ff9b9bSNicholas Bellinger up(&dev->caw_sem); 62281b6ca6dSBart Van Assche sg_free_table(&write_tbl); 62368ff9b9bSNicholas Bellinger return ret; 62468ff9b9bSNicholas Bellinger } 62568ff9b9bSNicholas Bellinger 62668ff9b9bSNicholas Bellinger static sense_reason_t 62768ff9b9bSNicholas Bellinger sbc_compare_and_write(struct se_cmd *cmd) 62868ff9b9bSNicholas Bellinger { 6297a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 63068ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 63168ff9b9bSNicholas Bellinger sense_reason_t ret; 63268ff9b9bSNicholas Bellinger int rc; 63368ff9b9bSNicholas Bellinger /* 63468ff9b9bSNicholas Bellinger * Submit the READ first for COMPARE_AND_WRITE to perform the 63568ff9b9bSNicholas Bellinger * comparision using SGLs at cmd->t_bidi_data_sg.. 63668ff9b9bSNicholas Bellinger */ 63768ff9b9bSNicholas Bellinger rc = down_interruptible(&dev->caw_sem); 638ee7619f2SNicholas Bellinger if (rc != 0) { 63968ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 64068ff9b9bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 64168ff9b9bSNicholas Bellinger } 642b7191253SNicholas Bellinger /* 643b7191253SNicholas Bellinger * Reset cmd->data_length to individual block_size in order to not 644b7191253SNicholas Bellinger * confuse backend drivers that depend on this value matching the 645b7191253SNicholas Bellinger * size of the I/O being submitted. 646b7191253SNicholas Bellinger */ 647b7191253SNicholas Bellinger cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size; 64868ff9b9bSNicholas Bellinger 6497a971b1bSChristoph Hellwig ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents, 65068ff9b9bSNicholas Bellinger DMA_FROM_DEVICE); 65168ff9b9bSNicholas Bellinger if (ret) { 65268ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 65368ff9b9bSNicholas Bellinger up(&dev->caw_sem); 65468ff9b9bSNicholas Bellinger return ret; 65568ff9b9bSNicholas Bellinger } 65668ff9b9bSNicholas Bellinger /* 65768ff9b9bSNicholas Bellinger * Unlock of dev->caw_sem to occur in compare_and_write_callback() 65868ff9b9bSNicholas Bellinger * upon MISCOMPARE, or in compare_and_write_done() upon completion 65968ff9b9bSNicholas Bellinger * of WRITE instance user-data. 66068ff9b9bSNicholas Bellinger */ 66168ff9b9bSNicholas Bellinger return TCM_NO_SENSE; 66268ff9b9bSNicholas Bellinger } 66368ff9b9bSNicholas Bellinger 66419f9361aSSagi Grimberg static int 66538b57f82SNicholas Bellinger sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type, 66619f9361aSSagi Grimberg bool is_write, struct se_cmd *cmd) 66719f9361aSSagi Grimberg { 66819f9361aSSagi Grimberg if (is_write) { 66938b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP : 67038b57f82SNicholas Bellinger protect ? TARGET_PROT_DOUT_PASS : 67119f9361aSSagi Grimberg TARGET_PROT_DOUT_INSERT; 67219f9361aSSagi Grimberg switch (protect) { 67319f9361aSSagi Grimberg case 0x0: 67419f9361aSSagi Grimberg case 0x3: 67519f9361aSSagi Grimberg cmd->prot_checks = 0; 67619f9361aSSagi Grimberg break; 67719f9361aSSagi Grimberg case 0x1: 67819f9361aSSagi Grimberg case 0x5: 67919f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 68019f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 68119f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 68219f9361aSSagi Grimberg break; 68319f9361aSSagi Grimberg case 0x2: 68419f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 68519f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 68619f9361aSSagi Grimberg break; 68719f9361aSSagi Grimberg case 0x4: 68819f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 68919f9361aSSagi Grimberg break; 69019f9361aSSagi Grimberg default: 69119f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 69219f9361aSSagi Grimberg return -EINVAL; 69319f9361aSSagi Grimberg } 69419f9361aSSagi Grimberg } else { 69538b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT : 69638b57f82SNicholas Bellinger protect ? TARGET_PROT_DIN_PASS : 69719f9361aSSagi Grimberg TARGET_PROT_DIN_STRIP; 69819f9361aSSagi Grimberg switch (protect) { 69919f9361aSSagi Grimberg case 0x0: 70019f9361aSSagi Grimberg case 0x1: 70119f9361aSSagi Grimberg case 0x5: 70219f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 70319f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 70419f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 70519f9361aSSagi Grimberg break; 70619f9361aSSagi Grimberg case 0x2: 70719f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 70819f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 70919f9361aSSagi Grimberg break; 71019f9361aSSagi Grimberg case 0x3: 71119f9361aSSagi Grimberg cmd->prot_checks = 0; 71219f9361aSSagi Grimberg break; 71319f9361aSSagi Grimberg case 0x4: 71419f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 71519f9361aSSagi Grimberg break; 71619f9361aSSagi Grimberg default: 71719f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 71819f9361aSSagi Grimberg return -EINVAL; 71919f9361aSSagi Grimberg } 72019f9361aSSagi Grimberg } 72119f9361aSSagi Grimberg 72219f9361aSSagi Grimberg return 0; 72319f9361aSSagi Grimberg } 72419f9361aSSagi Grimberg 725f7b7c06fSNicholas Bellinger static sense_reason_t 7266d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char protect, 72719f9361aSSagi Grimberg u32 sectors, bool is_write) 728499bf77bSNicholas Bellinger { 72938b57f82SNicholas Bellinger int sp_ops = cmd->se_sess->sup_prot_ops; 73038b57f82SNicholas Bellinger int pi_prot_type = dev->dev_attrib.pi_prot_type; 73138b57f82SNicholas Bellinger bool fabric_prot = false; 73219f9361aSSagi Grimberg 733f7b7c06fSNicholas Bellinger if (!cmd->t_prot_sg || !cmd->t_prot_nents) { 73438b57f82SNicholas Bellinger if (unlikely(protect && 73538b57f82SNicholas Bellinger !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) { 73638b57f82SNicholas Bellinger pr_err("CDB contains protect bit, but device + fabric does" 73738b57f82SNicholas Bellinger " not advertise PROTECT=1 feature bit\n"); 738f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 739f7b7c06fSNicholas Bellinger } 740f7b7c06fSNicholas Bellinger if (cmd->prot_pto) 741f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 742f7b7c06fSNicholas Bellinger } 743499bf77bSNicholas Bellinger 744499bf77bSNicholas Bellinger switch (dev->dev_attrib.pi_prot_type) { 745499bf77bSNicholas Bellinger case TARGET_DIF_TYPE3_PROT: 746499bf77bSNicholas Bellinger cmd->reftag_seed = 0xffffffff; 747499bf77bSNicholas Bellinger break; 748499bf77bSNicholas Bellinger case TARGET_DIF_TYPE2_PROT: 74919f9361aSSagi Grimberg if (protect) 750f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 751499bf77bSNicholas Bellinger 752499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 753499bf77bSNicholas Bellinger break; 754499bf77bSNicholas Bellinger case TARGET_DIF_TYPE1_PROT: 755499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 756499bf77bSNicholas Bellinger break; 757499bf77bSNicholas Bellinger case TARGET_DIF_TYPE0_PROT: 75838b57f82SNicholas Bellinger /* 75938b57f82SNicholas Bellinger * See if the fabric supports T10-PI, and the session has been 76038b57f82SNicholas Bellinger * configured to allow export PROTECT=1 feature bit with backend 76138b57f82SNicholas Bellinger * devices that don't support T10-PI. 76238b57f82SNicholas Bellinger */ 76338b57f82SNicholas Bellinger fabric_prot = is_write ? 76438b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) : 76538b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT)); 76638b57f82SNicholas Bellinger 76738b57f82SNicholas Bellinger if (fabric_prot && cmd->se_sess->sess_prot_type) { 76838b57f82SNicholas Bellinger pi_prot_type = cmd->se_sess->sess_prot_type; 76938b57f82SNicholas Bellinger break; 77038b57f82SNicholas Bellinger } 771cceca4a6SNicholas Bellinger if (!protect) 772f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 773df561f66SGustavo A. R. Silva fallthrough; 774499bf77bSNicholas Bellinger default: 775cceca4a6SNicholas Bellinger pr_err("Unable to determine pi_prot_type for CDB: 0x%02x " 7766d8e7e7cSDmitry Bogdanov "PROTECT: 0x%02x\n", cmd->t_task_cdb[0], protect); 777cceca4a6SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 778499bf77bSNicholas Bellinger } 779499bf77bSNicholas Bellinger 78038b57f82SNicholas Bellinger if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd)) 781f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 78219f9361aSSagi Grimberg 78338b57f82SNicholas Bellinger cmd->prot_type = pi_prot_type; 784499bf77bSNicholas Bellinger cmd->prot_length = dev->prot_length * sectors; 785e2a4f55cSSagi Grimberg 786e2a4f55cSSagi Grimberg /** 787e2a4f55cSSagi Grimberg * In case protection information exists over the wire 788e2a4f55cSSagi Grimberg * we modify command data length to describe pure data. 789e2a4f55cSSagi Grimberg * The actual transfer length is data length + protection 790e2a4f55cSSagi Grimberg * length 791e2a4f55cSSagi Grimberg **/ 792e2a4f55cSSagi Grimberg if (protect) 793e2a4f55cSSagi Grimberg cmd->data_length = sectors * dev->dev_attrib.block_size; 794e2a4f55cSSagi Grimberg 795e2a4f55cSSagi Grimberg pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d " 796e2a4f55cSSagi Grimberg "prot_op=%d prot_checks=%d\n", 797e2a4f55cSSagi Grimberg __func__, cmd->prot_type, cmd->data_length, cmd->prot_length, 79803abad9eSSagi Grimberg cmd->prot_op, cmd->prot_checks); 799499bf77bSNicholas Bellinger 800f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 801499bf77bSNicholas Bellinger } 802499bf77bSNicholas Bellinger 803fde9f50fSNicholas Bellinger static int 804fde9f50fSNicholas Bellinger sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb) 805fde9f50fSNicholas Bellinger { 806fde9f50fSNicholas Bellinger if (cdb[1] & 0x10) { 807814e5b45SChristoph Hellwig /* see explanation in spc_emulate_modesense */ 808814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 809fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with DPO bit set, but device" 810fde9f50fSNicholas Bellinger " does not advertise support for DPO\n", cdb[0]); 811fde9f50fSNicholas Bellinger return -EINVAL; 812fde9f50fSNicholas Bellinger } 813fde9f50fSNicholas Bellinger } 814fde9f50fSNicholas Bellinger if (cdb[1] & 0x8) { 815814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 816fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with FUA bit set, but device" 817fde9f50fSNicholas Bellinger " does not advertise support for FUA write\n", 818fde9f50fSNicholas Bellinger cdb[0]); 819fde9f50fSNicholas Bellinger return -EINVAL; 820fde9f50fSNicholas Bellinger } 821fde9f50fSNicholas Bellinger cmd->se_cmd_flags |= SCF_FUA; 822fde9f50fSNicholas Bellinger } 823fde9f50fSNicholas Bellinger return 0; 824d6e0175cSChristoph Hellwig } 825d6e0175cSChristoph Hellwig 826de103c93SChristoph Hellwig sense_reason_t 827de103c93SChristoph Hellwig sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) 828d6e0175cSChristoph Hellwig { 829d6e0175cSChristoph Hellwig struct se_device *dev = cmd->se_dev; 830d6e0175cSChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 8311fd032eeSChristoph Hellwig unsigned int size; 832d6e0175cSChristoph Hellwig u32 sectors = 0; 833de103c93SChristoph Hellwig sense_reason_t ret; 834d6e0175cSChristoph Hellwig 8357a971b1bSChristoph Hellwig cmd->protocol_data = ops; 8367a971b1bSChristoph Hellwig 837d6e0175cSChristoph Hellwig switch (cdb[0]) { 838d6e0175cSChristoph Hellwig case READ_6: 839d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 840d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 841d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 842a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 843d6e0175cSChristoph Hellwig break; 844d6e0175cSChristoph Hellwig case READ_10: 845d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 846d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 847499bf77bSNicholas Bellinger 848fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 849fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 850fde9f50fSNicholas Bellinger 8516d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false); 852f7b7c06fSNicholas Bellinger if (ret) 853f7b7c06fSNicholas Bellinger return ret; 854499bf77bSNicholas Bellinger 855d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 856a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 857d6e0175cSChristoph Hellwig break; 858d6e0175cSChristoph Hellwig case READ_12: 859d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 860d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 861499bf77bSNicholas Bellinger 862fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 863fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 864fde9f50fSNicholas Bellinger 8656d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false); 866f7b7c06fSNicholas Bellinger if (ret) 867f7b7c06fSNicholas Bellinger return ret; 868499bf77bSNicholas Bellinger 869d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 870a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 871d6e0175cSChristoph Hellwig break; 872d6e0175cSChristoph Hellwig case READ_16: 873d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 874d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 875499bf77bSNicholas Bellinger 876fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 877fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 878fde9f50fSNicholas Bellinger 8796d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false); 880f7b7c06fSNicholas Bellinger if (ret) 881f7b7c06fSNicholas Bellinger return ret; 882499bf77bSNicholas Bellinger 883d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 884a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 885d6e0175cSChristoph Hellwig break; 886d6e0175cSChristoph Hellwig case WRITE_6: 887d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 888d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 889d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 890a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 891d6e0175cSChristoph Hellwig break; 892d6e0175cSChristoph Hellwig case WRITE_10: 893984a9d4cSNicholas Bellinger case WRITE_VERIFY: 894d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 895d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 896499bf77bSNicholas Bellinger 897fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 898fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 899499bf77bSNicholas Bellinger 9006d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true); 901f7b7c06fSNicholas Bellinger if (ret) 902f7b7c06fSNicholas Bellinger return ret; 903d6e0175cSChristoph Hellwig 904d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 905a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 906d6e0175cSChristoph Hellwig break; 907d6e0175cSChristoph Hellwig case WRITE_12: 908d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 909d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 910499bf77bSNicholas Bellinger 911fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 912fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 913499bf77bSNicholas Bellinger 9146d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true); 915f7b7c06fSNicholas Bellinger if (ret) 916f7b7c06fSNicholas Bellinger return ret; 917d6e0175cSChristoph Hellwig 918d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 919a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 920d6e0175cSChristoph Hellwig break; 921d6e0175cSChristoph Hellwig case WRITE_16: 922984a9d4cSNicholas Bellinger case WRITE_VERIFY_16: 923d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 924d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 925499bf77bSNicholas Bellinger 926fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 927fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 928499bf77bSNicholas Bellinger 9296d8e7e7cSDmitry Bogdanov ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true); 930f7b7c06fSNicholas Bellinger if (ret) 931f7b7c06fSNicholas Bellinger return ret; 932cd063befSNicholas Bellinger 933d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 934d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 935d6e0175cSChristoph Hellwig break; 936d6e0175cSChristoph Hellwig case XDWRITEREAD_10: 937d6e0175cSChristoph Hellwig if (cmd->data_direction != DMA_TO_DEVICE || 938d6e0175cSChristoph Hellwig !(cmd->se_cmd_flags & SCF_BIDI)) 939d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 940d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 941d6e0175cSChristoph Hellwig 942fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 943fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 944fde9f50fSNicholas Bellinger 945d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 946d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 947d6e0175cSChristoph Hellwig 948d6e0175cSChristoph Hellwig /* 949d6e0175cSChristoph Hellwig * Setup BIDI XOR callback to be run after I/O completion. 950d6e0175cSChristoph Hellwig */ 951d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 952d6e0175cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 9530c2ad7d1SChristoph Hellwig break; 954d6e0175cSChristoph Hellwig case VARIABLE_LENGTH_CMD: 955d6e0175cSChristoph Hellwig { 956d6e0175cSChristoph Hellwig u16 service_action = get_unaligned_be16(&cdb[8]); 957d6e0175cSChristoph Hellwig switch (service_action) { 958d6e0175cSChristoph Hellwig case XDWRITEREAD_32: 9596f974e8cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 960de103c93SChristoph Hellwig 961fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 962fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 9636f974e8cSChristoph Hellwig /* 964d6e0175cSChristoph Hellwig * Use WRITE_32 and READ_32 opcodes for the emulated 965d6e0175cSChristoph Hellwig * XDWRITE_READ_32 logic. 966d6e0175cSChristoph Hellwig */ 967d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64_ext(cdb); 968de103c93SChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 969d6e0175cSChristoph Hellwig 970d6e0175cSChristoph Hellwig /* 9711fd032eeSChristoph Hellwig * Setup BIDI XOR callback to be run during after I/O 972d6e0175cSChristoph Hellwig * completion. 973d6e0175cSChristoph Hellwig */ 974de103c93SChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 9756f974e8cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 976d6e0175cSChristoph Hellwig break; 977d6e0175cSChristoph Hellwig case WRITE_SAME_32: 978d6e0175cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 979d6e0175cSChristoph Hellwig if (!sectors) { 980d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" 981d6e0175cSChristoph Hellwig " supported\n"); 982d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 983d6e0175cSChristoph Hellwig } 984d6e0175cSChristoph Hellwig 985d6e0175cSChristoph Hellwig size = sbc_get_size(cmd, 1); 986d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[12]); 987d6e0175cSChristoph Hellwig 9886d8e7e7cSDmitry Bogdanov ret = sbc_setup_write_same(cmd, cdb[10], ops); 989d6e0175cSChristoph Hellwig if (ret) 9901fd032eeSChristoph Hellwig return ret; 991d6e0175cSChristoph Hellwig break; 992d6e0175cSChristoph Hellwig default: 993d6e0175cSChristoph Hellwig pr_err("VARIABLE_LENGTH_CMD service action" 994d6e0175cSChristoph Hellwig " 0x%04x not supported\n", service_action); 995de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 996d6e0175cSChristoph Hellwig } 997d6e0175cSChristoph Hellwig break; 998d6e0175cSChristoph Hellwig } 99968ff9b9bSNicholas Bellinger case COMPARE_AND_WRITE: 100012f66e4aSJiang Yi if (!dev->dev_attrib.emulate_caw) { 10016f3bf5a2SBart Van Assche pr_err_ratelimited("se_device %s/%s (vpd_unit_serial %s) reject COMPARE_AND_WRITE\n", 10026f3bf5a2SBart Van Assche dev->se_hba->backend->ops->name, 10036f3bf5a2SBart Van Assche config_item_name(&dev->dev_group.cg_item), 10046f3bf5a2SBart Van Assche dev->t10_wwn.unit_serial); 100512f66e4aSJiang Yi return TCM_UNSUPPORTED_SCSI_OPCODE; 100612f66e4aSJiang Yi } 100768ff9b9bSNicholas Bellinger sectors = cdb[13]; 100868ff9b9bSNicholas Bellinger /* 100968ff9b9bSNicholas Bellinger * Currently enforce COMPARE_AND_WRITE for a single sector 101068ff9b9bSNicholas Bellinger */ 101168ff9b9bSNicholas Bellinger if (sectors > 1) { 101268ff9b9bSNicholas Bellinger pr_err("COMPARE_AND_WRITE contains NoLB: %u greater" 101368ff9b9bSNicholas Bellinger " than 1\n", sectors); 101468ff9b9bSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 101568ff9b9bSNicholas Bellinger } 1016ab81a5e0SDavid Disseldorp if (sbc_check_dpofua(dev, cmd, cdb)) 1017ab81a5e0SDavid Disseldorp return TCM_INVALID_CDB_FIELD; 1018ab81a5e0SDavid Disseldorp 101968ff9b9bSNicholas Bellinger /* 102068ff9b9bSNicholas Bellinger * Double size because we have two buffers, note that 102168ff9b9bSNicholas Bellinger * zero is not an error.. 102268ff9b9bSNicholas Bellinger */ 102368ff9b9bSNicholas Bellinger size = 2 * sbc_get_size(cmd, sectors); 102468ff9b9bSNicholas Bellinger cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 102568ff9b9bSNicholas Bellinger cmd->t_task_nolb = sectors; 102668ff9b9bSNicholas Bellinger cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE; 102768ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_compare_and_write; 102868ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_callback; 102968ff9b9bSNicholas Bellinger break; 1030d6e0175cSChristoph Hellwig case READ_CAPACITY: 10311fd032eeSChristoph Hellwig size = READ_CAP_LEN; 10321fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity; 1033d6e0175cSChristoph Hellwig break; 1034eb846d9fSHannes Reinecke case SERVICE_ACTION_IN_16: 1035d6e0175cSChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 1036d6e0175cSChristoph Hellwig case SAI_READ_CAPACITY_16: 10371fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity_16; 1038d6e0175cSChristoph Hellwig break; 1039c66094bfSHannes Reinecke case SAI_REPORT_REFERRALS: 1040c66094bfSHannes Reinecke cmd->execute_cmd = target_emulate_report_referrals; 1041c66094bfSHannes Reinecke break; 1042d6e0175cSChristoph Hellwig default: 1043d6e0175cSChristoph Hellwig pr_err("Unsupported SA: 0x%02x\n", 1044d6e0175cSChristoph Hellwig cmd->t_task_cdb[1] & 0x1f); 1045de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1046d6e0175cSChristoph Hellwig } 1047a85d667eSBart Van Assche size = get_unaligned_be32(&cdb[10]); 1048d6e0175cSChristoph Hellwig break; 1049d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE: 1050d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE_16: 1051d6e0175cSChristoph Hellwig if (cdb[0] == SYNCHRONIZE_CACHE) { 1052d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1053d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 1054d6e0175cSChristoph Hellwig } else { 1055d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1056d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 1057d6e0175cSChristoph Hellwig } 10586ef31dc7SChristophe Vu-Brugier if (ops->execute_sync_cache) { 1059ad67f0d9SChristoph Hellwig cmd->execute_cmd = ops->execute_sync_cache; 10606ef31dc7SChristophe Vu-Brugier goto check_lba; 10616ef31dc7SChristophe Vu-Brugier } 10626ef31dc7SChristophe Vu-Brugier size = 0; 10636ef31dc7SChristophe Vu-Brugier cmd->execute_cmd = sbc_emulate_noop; 1064d6e0175cSChristoph Hellwig break; 1065d6e0175cSChristoph Hellwig case UNMAP: 106614150a6bSChristoph Hellwig if (!ops->execute_unmap) 1067de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 106814150a6bSChristoph Hellwig 106961fdb4acSNicholas Bellinger if (!dev->dev_attrib.emulate_tpu) { 107061fdb4acSNicholas Bellinger pr_err("Got UNMAP, but backend device has" 107161fdb4acSNicholas Bellinger " emulate_tpu disabled\n"); 107261fdb4acSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 107361fdb4acSNicholas Bellinger } 10741fd032eeSChristoph Hellwig size = get_unaligned_be16(&cdb[7]); 107562e46942SChristoph Hellwig cmd->execute_cmd = sbc_execute_unmap; 1076d6e0175cSChristoph Hellwig break; 1077d6e0175cSChristoph Hellwig case WRITE_SAME_16: 1078d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1079d6e0175cSChristoph Hellwig if (!sectors) { 1080d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1081de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1082d6e0175cSChristoph Hellwig } 1083d6e0175cSChristoph Hellwig 10841fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1085d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 1086d6e0175cSChristoph Hellwig 10876d8e7e7cSDmitry Bogdanov ret = sbc_setup_write_same(cmd, cdb[1], ops); 10886b64e1feSDan Carpenter if (ret) 1089cd063befSNicholas Bellinger return ret; 1090d6e0175cSChristoph Hellwig break; 1091d6e0175cSChristoph Hellwig case WRITE_SAME: 1092d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1093d6e0175cSChristoph Hellwig if (!sectors) { 1094d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1095de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1096d6e0175cSChristoph Hellwig } 1097d6e0175cSChristoph Hellwig 10981fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1099d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be32(&cdb[2]); 1100d6e0175cSChristoph Hellwig 1101d6e0175cSChristoph Hellwig /* 1102d6e0175cSChristoph Hellwig * Follow sbcr26 with WRITE_SAME (10) and check for the existence 1103d6e0175cSChristoph Hellwig * of byte 1 bit 3 UNMAP instead of original reserved field 1104d6e0175cSChristoph Hellwig */ 11056d8e7e7cSDmitry Bogdanov ret = sbc_setup_write_same(cmd, cdb[1], ops); 11066b64e1feSDan Carpenter if (ret) 1107cd063befSNicholas Bellinger return ret; 1108d6e0175cSChristoph Hellwig break; 1109d6e0175cSChristoph Hellwig case VERIFY: 111013603685SMax Lohrmann case VERIFY_16: 1111984a9d4cSNicholas Bellinger size = 0; 1112984a9d4cSNicholas Bellinger if (cdb[0] == VERIFY) { 1113984a9d4cSNicholas Bellinger sectors = transport_get_sectors_10(cdb); 1114984a9d4cSNicholas Bellinger cmd->t_task_lba = transport_lba_32(cdb); 1115984a9d4cSNicholas Bellinger } else { 1116984a9d4cSNicholas Bellinger sectors = transport_get_sectors_16(cdb); 1117984a9d4cSNicholas Bellinger cmd->t_task_lba = transport_lba_64(cdb); 1118984a9d4cSNicholas Bellinger } 11191920ed61SNicholas Bellinger cmd->execute_cmd = sbc_emulate_noop; 1120c52716deSChristophe Vu-Brugier goto check_lba; 11211a1ff38cSBernhard Kohl case REZERO_UNIT: 11221a1ff38cSBernhard Kohl case SEEK_6: 11231a1ff38cSBernhard Kohl case SEEK_10: 11241a1ff38cSBernhard Kohl /* 11251a1ff38cSBernhard Kohl * There are still clients out there which use these old SCSI-2 11261a1ff38cSBernhard Kohl * commands. This mainly happens when running VMs with legacy 11271a1ff38cSBernhard Kohl * guest systems, connected via SCSI command pass-through to 11281a1ff38cSBernhard Kohl * iSCSI targets. Make them happy and return status GOOD. 11291a1ff38cSBernhard Kohl */ 11301a1ff38cSBernhard Kohl size = 0; 11311a1ff38cSBernhard Kohl cmd->execute_cmd = sbc_emulate_noop; 11321a1ff38cSBernhard Kohl break; 113345182ed5SBrian Bunker case START_STOP: 113445182ed5SBrian Bunker size = 0; 113545182ed5SBrian Bunker cmd->execute_cmd = sbc_emulate_startstop; 113645182ed5SBrian Bunker break; 1137d6e0175cSChristoph Hellwig default: 11381fd032eeSChristoph Hellwig ret = spc_parse_cdb(cmd, &size); 1139d6e0175cSChristoph Hellwig if (ret) 1140d6e0175cSChristoph Hellwig return ret; 1141d6e0175cSChristoph Hellwig } 1142d6e0175cSChristoph Hellwig 1143d6e0175cSChristoph Hellwig /* reject any command that we don't have a handler for */ 114420959c4bSAndy Grover if (!cmd->execute_cmd) 1145de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 1146d6e0175cSChristoph Hellwig 1147d6e0175cSChristoph Hellwig if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { 11481fd032eeSChristoph Hellwig unsigned long long end_lba; 11496ef31dc7SChristophe Vu-Brugier check_lba: 11501fd032eeSChristoph Hellwig end_lba = dev->transport->get_blocks(dev) + 1; 1151aa179935SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 1152aa179935SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 11531fd032eeSChristoph Hellwig pr_err("cmd exceeds last lba %llu " 11541fd032eeSChristoph Hellwig "(lba %llu, sectors %u)\n", 11551fd032eeSChristoph Hellwig end_lba, cmd->t_task_lba, sectors); 115609ceadc7SRoland Dreier return TCM_ADDRESS_OUT_OF_RANGE; 1157d6e0175cSChristoph Hellwig } 1158d6e0175cSChristoph Hellwig 115968ff9b9bSNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) 11601fd032eeSChristoph Hellwig size = sbc_get_size(cmd, sectors); 11611fd032eeSChristoph Hellwig } 11621fd032eeSChristoph Hellwig 1163de103c93SChristoph Hellwig return target_cmd_size_check(cmd, size); 1164d6e0175cSChristoph Hellwig } 1165d6e0175cSChristoph Hellwig EXPORT_SYMBOL(sbc_parse_cdb); 11666f23ac8aSChristoph Hellwig 11676f23ac8aSChristoph Hellwig u32 sbc_get_device_type(struct se_device *dev) 11686f23ac8aSChristoph Hellwig { 11696f23ac8aSChristoph Hellwig return TYPE_DISK; 11706f23ac8aSChristoph Hellwig } 11716f23ac8aSChristoph Hellwig EXPORT_SYMBOL(sbc_get_device_type); 117286d71829SAsias He 117362e46942SChristoph Hellwig static sense_reason_t 117462e46942SChristoph Hellwig sbc_execute_unmap(struct se_cmd *cmd) 117586d71829SAsias He { 117662e46942SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 117786d71829SAsias He struct se_device *dev = cmd->se_dev; 117886d71829SAsias He unsigned char *buf, *ptr = NULL; 117986d71829SAsias He sector_t lba; 118086d71829SAsias He int size; 118186d71829SAsias He u32 range; 118286d71829SAsias He sense_reason_t ret = 0; 118386d71829SAsias He int dl, bd_dl; 118486d71829SAsias He 118586d71829SAsias He /* We never set ANC_SUP */ 118686d71829SAsias He if (cmd->t_task_cdb[1]) 118786d71829SAsias He return TCM_INVALID_CDB_FIELD; 118886d71829SAsias He 118986d71829SAsias He if (cmd->data_length == 0) { 119086d71829SAsias He target_complete_cmd(cmd, SAM_STAT_GOOD); 119186d71829SAsias He return 0; 119286d71829SAsias He } 119386d71829SAsias He 119486d71829SAsias He if (cmd->data_length < 8) { 119586d71829SAsias He pr_warn("UNMAP parameter list length %u too small\n", 119686d71829SAsias He cmd->data_length); 119786d71829SAsias He return TCM_PARAMETER_LIST_LENGTH_ERROR; 119886d71829SAsias He } 119986d71829SAsias He 120086d71829SAsias He buf = transport_kmap_data_sg(cmd); 120186d71829SAsias He if (!buf) 120286d71829SAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 120386d71829SAsias He 120486d71829SAsias He dl = get_unaligned_be16(&buf[0]); 120586d71829SAsias He bd_dl = get_unaligned_be16(&buf[2]); 120686d71829SAsias He 120786d71829SAsias He size = cmd->data_length - 8; 120886d71829SAsias He if (bd_dl > size) 120986d71829SAsias He pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n", 121086d71829SAsias He cmd->data_length, bd_dl); 121186d71829SAsias He else 121286d71829SAsias He size = bd_dl; 121386d71829SAsias He 121486d71829SAsias He if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) { 121586d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 121686d71829SAsias He goto err; 121786d71829SAsias He } 121886d71829SAsias He 121986d71829SAsias He /* First UNMAP block descriptor starts at 8 byte offset */ 122086d71829SAsias He ptr = &buf[8]; 122186d71829SAsias He pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u" 122286d71829SAsias He " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); 122386d71829SAsias He 122486d71829SAsias He while (size >= 16) { 122586d71829SAsias He lba = get_unaligned_be64(&ptr[0]); 122686d71829SAsias He range = get_unaligned_be32(&ptr[8]); 122786d71829SAsias He pr_debug("UNMAP: Using lba: %llu and range: %u\n", 122886d71829SAsias He (unsigned long long)lba, range); 122986d71829SAsias He 123086d71829SAsias He if (range > dev->dev_attrib.max_unmap_lba_count) { 123186d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 123286d71829SAsias He goto err; 123386d71829SAsias He } 123486d71829SAsias He 123586d71829SAsias He if (lba + range > dev->transport->get_blocks(dev) + 1) { 123686d71829SAsias He ret = TCM_ADDRESS_OUT_OF_RANGE; 123786d71829SAsias He goto err; 123886d71829SAsias He } 123986d71829SAsias He 12409960f851SAndrei Vagin if (range) { 124162e46942SChristoph Hellwig ret = ops->execute_unmap(cmd, lba, range); 124286d71829SAsias He if (ret) 124386d71829SAsias He goto err; 12449960f851SAndrei Vagin } 124586d71829SAsias He 124686d71829SAsias He ptr += 16; 124786d71829SAsias He size -= 16; 124886d71829SAsias He } 124986d71829SAsias He 125086d71829SAsias He err: 125186d71829SAsias He transport_kunmap_data_sg(cmd); 125286d71829SAsias He if (!ret) 125314b40c1eSHannes Reinecke target_complete_cmd(cmd, SAM_STAT_GOOD); 125486d71829SAsias He return ret; 125586d71829SAsias He } 125641861fa8SNicholas Bellinger 125766a3d5bcSNicholas Bellinger void 125866a3d5bcSNicholas Bellinger sbc_dif_generate(struct se_cmd *cmd) 125966a3d5bcSNicholas Bellinger { 126066a3d5bcSNicholas Bellinger struct se_device *dev = cmd->se_dev; 1261fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 126218213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg, *psg; 126366a3d5bcSNicholas Bellinger sector_t sector = cmd->t_task_lba; 126466a3d5bcSNicholas Bellinger void *daddr, *paddr; 126566a3d5bcSNicholas Bellinger int i, j, offset = 0; 126618213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 126766a3d5bcSNicholas Bellinger 126818213afbSAkinobu Mita for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 126918213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 127066a3d5bcSNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 127166a3d5bcSNicholas Bellinger 127218213afbSAkinobu Mita for (j = 0; j < psg->length; 1273fe052a18SSagi Grimberg j += sizeof(*sdt)) { 127418213afbSAkinobu Mita __u16 crc; 127518213afbSAkinobu Mita unsigned int avail; 127666a3d5bcSNicholas Bellinger 127718213afbSAkinobu Mita if (offset >= dsg->length) { 127818213afbSAkinobu Mita offset -= dsg->length; 127918213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 128018213afbSAkinobu Mita dsg = sg_next(dsg); 128118213afbSAkinobu Mita if (!dsg) { 128218213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 128318213afbSAkinobu Mita return; 128418213afbSAkinobu Mita } 128518213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 128666a3d5bcSNicholas Bellinger } 128766a3d5bcSNicholas Bellinger 128818213afbSAkinobu Mita sdt = paddr + j; 128918213afbSAkinobu Mita avail = min(block_size, dsg->length - offset); 129018213afbSAkinobu Mita crc = crc_t10dif(daddr + offset, avail); 129118213afbSAkinobu Mita if (avail < block_size) { 129218213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 129318213afbSAkinobu Mita dsg = sg_next(dsg); 129418213afbSAkinobu Mita if (!dsg) { 129518213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 129618213afbSAkinobu Mita return; 129718213afbSAkinobu Mita } 129818213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 129918213afbSAkinobu Mita offset = block_size - avail; 130018213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, offset); 130118213afbSAkinobu Mita } else { 130218213afbSAkinobu Mita offset += block_size; 130318213afbSAkinobu Mita } 130418213afbSAkinobu Mita 130518213afbSAkinobu Mita sdt->guard_tag = cpu_to_be16(crc); 1306823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT) 130766a3d5bcSNicholas Bellinger sdt->ref_tag = cpu_to_be32(sector & 0xffffffff); 130866a3d5bcSNicholas Bellinger sdt->app_tag = 0; 130966a3d5bcSNicholas Bellinger 13106ae50408SNicholas Bellinger pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x" 131166a3d5bcSNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 13126ae50408SNicholas Bellinger (cmd->data_direction == DMA_TO_DEVICE) ? 13136ae50408SNicholas Bellinger "WRITE" : "READ", (unsigned long long)sector, 13146ae50408SNicholas Bellinger sdt->guard_tag, sdt->app_tag, 13156ae50408SNicholas Bellinger be32_to_cpu(sdt->ref_tag)); 131666a3d5bcSNicholas Bellinger 131766a3d5bcSNicholas Bellinger sector++; 131866a3d5bcSNicholas Bellinger } 131966a3d5bcSNicholas Bellinger 132018213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 132118213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 132266a3d5bcSNicholas Bellinger } 132366a3d5bcSNicholas Bellinger } 132466a3d5bcSNicholas Bellinger 132541861fa8SNicholas Bellinger static sense_reason_t 1326fe052a18SSagi Grimberg sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt, 132718213afbSAkinobu Mita __u16 crc, sector_t sector, unsigned int ei_lba) 132841861fa8SNicholas Bellinger { 132941861fa8SNicholas Bellinger __be16 csum; 133041861fa8SNicholas Bellinger 1331d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD)) 1332d7a463b0SNicholas Bellinger goto check_ref; 1333d7a463b0SNicholas Bellinger 133418213afbSAkinobu Mita csum = cpu_to_be16(crc); 133541861fa8SNicholas Bellinger 133641861fa8SNicholas Bellinger if (sdt->guard_tag != csum) { 133741861fa8SNicholas Bellinger pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x" 133841861fa8SNicholas Bellinger " csum 0x%04x\n", (unsigned long long)sector, 133941861fa8SNicholas Bellinger be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); 134041861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 134141861fa8SNicholas Bellinger } 134241861fa8SNicholas Bellinger 1343d7a463b0SNicholas Bellinger check_ref: 1344d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)) 1345d7a463b0SNicholas Bellinger return 0; 1346d7a463b0SNicholas Bellinger 1347823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT && 134841861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { 134941861fa8SNicholas Bellinger pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x" 135041861fa8SNicholas Bellinger " sector MSB: 0x%08x\n", (unsigned long long)sector, 135141861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff)); 135241861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 135341861fa8SNicholas Bellinger } 135441861fa8SNicholas Bellinger 1355823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE2_PROT && 135641861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != ei_lba) { 135741861fa8SNicholas Bellinger pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x" 135841861fa8SNicholas Bellinger " ei_lba: 0x%08x\n", (unsigned long long)sector, 135941861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), ei_lba); 136041861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 136141861fa8SNicholas Bellinger } 136241861fa8SNicholas Bellinger 136341861fa8SNicholas Bellinger return 0; 136441861fa8SNicholas Bellinger } 136541861fa8SNicholas Bellinger 1366f75b6faeSSagi Grimberg void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read, 136741861fa8SNicholas Bellinger struct scatterlist *sg, int sg_off) 136841861fa8SNicholas Bellinger { 136941861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 137041861fa8SNicholas Bellinger struct scatterlist *psg; 137141861fa8SNicholas Bellinger void *paddr, *addr; 137241861fa8SNicholas Bellinger unsigned int i, len, left; 137310762e80SNicholas Bellinger unsigned int offset = sg_off; 137441861fa8SNicholas Bellinger 137538b57f82SNicholas Bellinger if (!sg) 137638b57f82SNicholas Bellinger return; 137738b57f82SNicholas Bellinger 137841861fa8SNicholas Bellinger left = sectors * dev->prot_length; 137941861fa8SNicholas Bellinger 138041861fa8SNicholas Bellinger for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 138116c0ae02SSagi Grimberg unsigned int psg_len, copied = 0; 138241861fa8SNicholas Bellinger 138316c0ae02SSagi Grimberg paddr = kmap_atomic(sg_page(psg)) + psg->offset; 138416c0ae02SSagi Grimberg psg_len = min(left, psg->length); 138516c0ae02SSagi Grimberg while (psg_len) { 138616c0ae02SSagi Grimberg len = min(psg_len, sg->length - offset); 138716c0ae02SSagi Grimberg addr = kmap_atomic(sg_page(sg)) + sg->offset + offset; 138816c0ae02SSagi Grimberg 138916c0ae02SSagi Grimberg if (read) 139016c0ae02SSagi Grimberg memcpy(paddr + copied, addr, len); 139116c0ae02SSagi Grimberg else 139216c0ae02SSagi Grimberg memcpy(addr, paddr + copied, len); 139316c0ae02SSagi Grimberg 139416c0ae02SSagi Grimberg left -= len; 139516c0ae02SSagi Grimberg offset += len; 139616c0ae02SSagi Grimberg copied += len; 139716c0ae02SSagi Grimberg psg_len -= len; 139816c0ae02SSagi Grimberg 139957636388SAkinobu Mita kunmap_atomic(addr - sg->offset - offset); 140057636388SAkinobu Mita 1401d6a65fdcSSagi Grimberg if (offset >= sg->length) { 1402d6a65fdcSSagi Grimberg sg = sg_next(sg); 1403d6a65fdcSSagi Grimberg offset = 0; 1404d6a65fdcSSagi Grimberg } 140541861fa8SNicholas Bellinger } 140657636388SAkinobu Mita kunmap_atomic(paddr - psg->offset); 140716c0ae02SSagi Grimberg } 140841861fa8SNicholas Bellinger } 1409f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_copy_prot); 141041861fa8SNicholas Bellinger 141141861fa8SNicholas Bellinger sense_reason_t 1412f75b6faeSSagi Grimberg sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors, 1413414e4627SSagi Grimberg unsigned int ei_lba, struct scatterlist *psg, int psg_off) 141441861fa8SNicholas Bellinger { 141541861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 1416fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 141718213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg; 141841861fa8SNicholas Bellinger sector_t sector = start; 141941861fa8SNicholas Bellinger void *daddr, *paddr; 142018213afbSAkinobu Mita int i; 142141861fa8SNicholas Bellinger sense_reason_t rc; 142218213afbSAkinobu Mita int dsg_off = 0; 142318213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 142441861fa8SNicholas Bellinger 142518213afbSAkinobu Mita for (; psg && sector < start + sectors; psg = sg_next(psg)) { 142618213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 142741861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 142841861fa8SNicholas Bellinger 142918213afbSAkinobu Mita for (i = psg_off; i < psg->length && 143018213afbSAkinobu Mita sector < start + sectors; 1431fe052a18SSagi Grimberg i += sizeof(*sdt)) { 143218213afbSAkinobu Mita __u16 crc; 143318213afbSAkinobu Mita unsigned int avail; 143441861fa8SNicholas Bellinger 143518213afbSAkinobu Mita if (dsg_off >= dsg->length) { 143618213afbSAkinobu Mita dsg_off -= dsg->length; 143718213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 143818213afbSAkinobu Mita dsg = sg_next(dsg); 143918213afbSAkinobu Mita if (!dsg) { 1440414e4627SSagi Grimberg kunmap_atomic(paddr - psg->offset); 144141861fa8SNicholas Bellinger return 0; 144241861fa8SNicholas Bellinger } 144341861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 144441861fa8SNicholas Bellinger } 144541861fa8SNicholas Bellinger 144618213afbSAkinobu Mita sdt = paddr + i; 144741861fa8SNicholas Bellinger 144841861fa8SNicholas Bellinger pr_debug("DIF READ sector: %llu guard_tag: 0x%04x" 144941861fa8SNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 145041861fa8SNicholas Bellinger (unsigned long long)sector, sdt->guard_tag, 145141861fa8SNicholas Bellinger sdt->app_tag, be32_to_cpu(sdt->ref_tag)); 145241861fa8SNicholas Bellinger 1453128b6f9fSDmitry Monakhov if (sdt->app_tag == T10_PI_APP_ESCAPE) { 145418213afbSAkinobu Mita dsg_off += block_size; 145518213afbSAkinobu Mita goto next; 145641861fa8SNicholas Bellinger } 145741861fa8SNicholas Bellinger 145818213afbSAkinobu Mita avail = min(block_size, dsg->length - dsg_off); 145918213afbSAkinobu Mita crc = crc_t10dif(daddr + dsg_off, avail); 146018213afbSAkinobu Mita if (avail < block_size) { 1461414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 146218213afbSAkinobu Mita dsg = sg_next(dsg); 146318213afbSAkinobu Mita if (!dsg) { 146418213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 146518213afbSAkinobu Mita return 0; 146618213afbSAkinobu Mita } 146718213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 146818213afbSAkinobu Mita dsg_off = block_size - avail; 146918213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, dsg_off); 147018213afbSAkinobu Mita } else { 147118213afbSAkinobu Mita dsg_off += block_size; 147218213afbSAkinobu Mita } 147318213afbSAkinobu Mita 147418213afbSAkinobu Mita rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba); 147541861fa8SNicholas Bellinger if (rc) { 147618213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 147718213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 14788dd992fbSDavid Disseldorp cmd->sense_info = sector; 147941861fa8SNicholas Bellinger return rc; 148041861fa8SNicholas Bellinger } 148118213afbSAkinobu Mita next: 148241861fa8SNicholas Bellinger sector++; 148341861fa8SNicholas Bellinger ei_lba++; 148441861fa8SNicholas Bellinger } 148541861fa8SNicholas Bellinger 148618213afbSAkinobu Mita psg_off = 0; 1487414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 148818213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 148941861fa8SNicholas Bellinger } 149041861fa8SNicholas Bellinger 149141861fa8SNicholas Bellinger return 0; 149241861fa8SNicholas Bellinger } 1493f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_verify); 1494