1d6e0175cSChristoph Hellwig /* 2d6e0175cSChristoph Hellwig * SCSI Block Commands (SBC) parsing and emulation. 3d6e0175cSChristoph Hellwig * 44c76251eSNicholas Bellinger * (c) Copyright 2002-2013 Datera, Inc. 5d6e0175cSChristoph Hellwig * 6d6e0175cSChristoph Hellwig * Nicholas A. Bellinger <nab@kernel.org> 7d6e0175cSChristoph Hellwig * 8d6e0175cSChristoph Hellwig * This program is free software; you can redistribute it and/or modify 9d6e0175cSChristoph Hellwig * it under the terms of the GNU General Public License as published by 10d6e0175cSChristoph Hellwig * the Free Software Foundation; either version 2 of the License, or 11d6e0175cSChristoph Hellwig * (at your option) any later version. 12d6e0175cSChristoph Hellwig * 13d6e0175cSChristoph Hellwig * This program is distributed in the hope that it will be useful, 14d6e0175cSChristoph Hellwig * but WITHOUT ANY WARRANTY; without even the implied warranty of 15d6e0175cSChristoph Hellwig * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16d6e0175cSChristoph Hellwig * GNU General Public License for more details. 17d6e0175cSChristoph Hellwig * 18d6e0175cSChristoph Hellwig * You should have received a copy of the GNU General Public License 19d6e0175cSChristoph Hellwig * along with this program; if not, write to the Free Software 20d6e0175cSChristoph Hellwig * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 21d6e0175cSChristoph Hellwig */ 22d6e0175cSChristoph Hellwig 23d6e0175cSChristoph Hellwig #include <linux/kernel.h> 24d6e0175cSChristoph Hellwig #include <linux/module.h> 25d6e0175cSChristoph Hellwig #include <linux/ratelimit.h> 2641861fa8SNicholas Bellinger #include <linux/crc-t10dif.h> 27d6e0175cSChristoph Hellwig #include <asm/unaligned.h> 28ba929992SBart Van Assche #include <scsi/scsi_proto.h> 2968ff9b9bSNicholas Bellinger #include <scsi/scsi_tcq.h> 30d6e0175cSChristoph Hellwig 31d6e0175cSChristoph Hellwig #include <target/target_core_base.h> 32d6e0175cSChristoph Hellwig #include <target/target_core_backend.h> 33d6e0175cSChristoph Hellwig #include <target/target_core_fabric.h> 34d6e0175cSChristoph Hellwig 35d6e0175cSChristoph Hellwig #include "target_core_internal.h" 36d6e0175cSChristoph Hellwig #include "target_core_ua.h" 37c66094bfSHannes Reinecke #include "target_core_alua.h" 38d6e0175cSChristoph Hellwig 39de103c93SChristoph Hellwig static sense_reason_t 40afd73f1bSNicholas Bellinger sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char *, u32, bool); 4162e46942SChristoph Hellwig static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd); 42afd73f1bSNicholas Bellinger 43afd73f1bSNicholas Bellinger static sense_reason_t 44de103c93SChristoph Hellwig sbc_emulate_readcapacity(struct se_cmd *cmd) 451fd032eeSChristoph Hellwig { 461fd032eeSChristoph Hellwig struct se_device *dev = cmd->se_dev; 478dc8632aSRoland Dreier unsigned char *cdb = cmd->t_task_cdb; 481fd032eeSChristoph Hellwig unsigned long long blocks_long = dev->transport->get_blocks(dev); 49a50da144SPaolo Bonzini unsigned char *rbuf; 50a50da144SPaolo Bonzini unsigned char buf[8]; 511fd032eeSChristoph Hellwig u32 blocks; 521fd032eeSChristoph Hellwig 538dc8632aSRoland Dreier /* 548dc8632aSRoland Dreier * SBC-2 says: 558dc8632aSRoland Dreier * If the PMI bit is set to zero and the LOGICAL BLOCK 568dc8632aSRoland Dreier * ADDRESS field is not set to zero, the device server shall 578dc8632aSRoland Dreier * terminate the command with CHECK CONDITION status with 588dc8632aSRoland Dreier * the sense key set to ILLEGAL REQUEST and the additional 598dc8632aSRoland Dreier * sense code set to INVALID FIELD IN CDB. 608dc8632aSRoland Dreier * 618dc8632aSRoland Dreier * In SBC-3, these fields are obsolete, but some SCSI 628dc8632aSRoland Dreier * compliance tests actually check this, so we might as well 638dc8632aSRoland Dreier * follow SBC-2. 648dc8632aSRoland Dreier */ 658dc8632aSRoland Dreier if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5])) 668dc8632aSRoland Dreier return TCM_INVALID_CDB_FIELD; 678dc8632aSRoland Dreier 681fd032eeSChristoph Hellwig if (blocks_long >= 0x00000000ffffffff) 691fd032eeSChristoph Hellwig blocks = 0xffffffff; 701fd032eeSChristoph Hellwig else 711fd032eeSChristoph Hellwig blocks = (u32)blocks_long; 721fd032eeSChristoph Hellwig 731fd032eeSChristoph Hellwig buf[0] = (blocks >> 24) & 0xff; 741fd032eeSChristoph Hellwig buf[1] = (blocks >> 16) & 0xff; 751fd032eeSChristoph Hellwig buf[2] = (blocks >> 8) & 0xff; 761fd032eeSChristoph Hellwig buf[3] = blocks & 0xff; 770fd97ccfSChristoph Hellwig buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff; 780fd97ccfSChristoph Hellwig buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff; 790fd97ccfSChristoph Hellwig buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff; 800fd97ccfSChristoph Hellwig buf[7] = dev->dev_attrib.block_size & 0xff; 811fd032eeSChristoph Hellwig 82a50da144SPaolo Bonzini rbuf = transport_kmap_data_sg(cmd); 838b4b0dcbSNicholas Bellinger if (rbuf) { 84a50da144SPaolo Bonzini memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 851fd032eeSChristoph Hellwig transport_kunmap_data_sg(cmd); 868b4b0dcbSNicholas Bellinger } 871fd032eeSChristoph Hellwig 882426bd45SRoland Dreier target_complete_cmd_with_length(cmd, GOOD, 8); 891fd032eeSChristoph Hellwig return 0; 901fd032eeSChristoph Hellwig } 911fd032eeSChristoph Hellwig 92de103c93SChristoph Hellwig static sense_reason_t 93de103c93SChristoph Hellwig sbc_emulate_readcapacity_16(struct se_cmd *cmd) 941fd032eeSChristoph Hellwig { 951fd032eeSChristoph Hellwig struct se_device *dev = cmd->se_dev; 962d335983SNicholas Bellinger struct se_session *sess = cmd->se_sess; 979ef5466eSNicholas Bellinger int pi_prot_type = dev->dev_attrib.pi_prot_type; 989ef5466eSNicholas Bellinger 99a50da144SPaolo Bonzini unsigned char *rbuf; 100a50da144SPaolo Bonzini unsigned char buf[32]; 1011fd032eeSChristoph Hellwig unsigned long long blocks = dev->transport->get_blocks(dev); 1021fd032eeSChristoph Hellwig 103a50da144SPaolo Bonzini memset(buf, 0, sizeof(buf)); 1041fd032eeSChristoph Hellwig buf[0] = (blocks >> 56) & 0xff; 1051fd032eeSChristoph Hellwig buf[1] = (blocks >> 48) & 0xff; 1061fd032eeSChristoph Hellwig buf[2] = (blocks >> 40) & 0xff; 1071fd032eeSChristoph Hellwig buf[3] = (blocks >> 32) & 0xff; 1081fd032eeSChristoph Hellwig buf[4] = (blocks >> 24) & 0xff; 1091fd032eeSChristoph Hellwig buf[5] = (blocks >> 16) & 0xff; 1101fd032eeSChristoph Hellwig buf[6] = (blocks >> 8) & 0xff; 1111fd032eeSChristoph Hellwig buf[7] = blocks & 0xff; 1120fd97ccfSChristoph Hellwig buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff; 1130fd97ccfSChristoph Hellwig buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff; 1140fd97ccfSChristoph Hellwig buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff; 1150fd97ccfSChristoph Hellwig buf[11] = dev->dev_attrib.block_size & 0xff; 11656dac14cSNicholas Bellinger /* 11756dac14cSNicholas Bellinger * Set P_TYPE and PROT_EN bits for DIF support 11856dac14cSNicholas Bellinger */ 1192d335983SNicholas Bellinger if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { 1209ef5466eSNicholas Bellinger /* 1219ef5466eSNicholas Bellinger * Only override a device's pi_prot_type if no T10-PI is 1229ef5466eSNicholas Bellinger * available, and sess_prot_type has been explicitly enabled. 1239ef5466eSNicholas Bellinger */ 1249ef5466eSNicholas Bellinger if (!pi_prot_type) 1259ef5466eSNicholas Bellinger pi_prot_type = sess->sess_prot_type; 1269ef5466eSNicholas Bellinger 1279ef5466eSNicholas Bellinger if (pi_prot_type) 1289ef5466eSNicholas Bellinger buf[12] = (pi_prot_type - 1) << 1 | 0x1; 1292d335983SNicholas Bellinger } 1307f7caf6aSAndy Grover 1317f7caf6aSAndy Grover if (dev->transport->get_lbppbe) 1327f7caf6aSAndy Grover buf[13] = dev->transport->get_lbppbe(dev) & 0x0f; 1337f7caf6aSAndy Grover 1347f7caf6aSAndy Grover if (dev->transport->get_alignment_offset_lbas) { 1357f7caf6aSAndy Grover u16 lalba = dev->transport->get_alignment_offset_lbas(dev); 1367f7caf6aSAndy Grover buf[14] = (lalba >> 8) & 0x3f; 1377f7caf6aSAndy Grover buf[15] = lalba & 0xff; 1387f7caf6aSAndy Grover } 1397f7caf6aSAndy Grover 1401fd032eeSChristoph Hellwig /* 1411fd032eeSChristoph Hellwig * Set Thin Provisioning Enable bit following sbc3r22 in section 1421fd032eeSChristoph Hellwig * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled. 1431fd032eeSChristoph Hellwig */ 144e6f41633SJamie Pocas if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) { 1457f7caf6aSAndy Grover buf[14] |= 0x80; 1461fd032eeSChristoph Hellwig 147e6f41633SJamie Pocas /* 148e6f41633SJamie Pocas * LBPRZ signifies that zeroes will be read back from an LBA after 149e6f41633SJamie Pocas * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2) 150e6f41633SJamie Pocas */ 151e6f41633SJamie Pocas if (dev->dev_attrib.unmap_zeroes_data) 152e6f41633SJamie Pocas buf[14] |= 0x40; 153e6f41633SJamie Pocas } 154e6f41633SJamie Pocas 155a50da144SPaolo Bonzini rbuf = transport_kmap_data_sg(cmd); 1568b4b0dcbSNicholas Bellinger if (rbuf) { 157a50da144SPaolo Bonzini memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 1581fd032eeSChristoph Hellwig transport_kunmap_data_sg(cmd); 1598b4b0dcbSNicholas Bellinger } 1601fd032eeSChristoph Hellwig 1612426bd45SRoland Dreier target_complete_cmd_with_length(cmd, GOOD, 32); 1621fd032eeSChristoph Hellwig return 0; 1631fd032eeSChristoph Hellwig } 1641fd032eeSChristoph Hellwig 16545182ed5SBrian Bunker static sense_reason_t 16645182ed5SBrian Bunker sbc_emulate_startstop(struct se_cmd *cmd) 16745182ed5SBrian Bunker { 16845182ed5SBrian Bunker unsigned char *cdb = cmd->t_task_cdb; 16945182ed5SBrian Bunker 17045182ed5SBrian Bunker /* 17145182ed5SBrian Bunker * See sbc3r36 section 5.25 17245182ed5SBrian Bunker * Immediate bit should be set since there is nothing to complete 17345182ed5SBrian Bunker * POWER CONDITION MODIFIER 0h 17445182ed5SBrian Bunker */ 17545182ed5SBrian Bunker if (!(cdb[1] & 1) || cdb[2] || cdb[3]) 17645182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 17745182ed5SBrian Bunker 17845182ed5SBrian Bunker /* 17945182ed5SBrian Bunker * See sbc3r36 section 5.25 18045182ed5SBrian Bunker * POWER CONDITION 0h START_VALID - process START and LOEJ 18145182ed5SBrian Bunker */ 18245182ed5SBrian Bunker if (cdb[4] >> 4 & 0xf) 18345182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 18445182ed5SBrian Bunker 18545182ed5SBrian Bunker /* 18645182ed5SBrian Bunker * See sbc3r36 section 5.25 18745182ed5SBrian Bunker * LOEJ 0h - nothing to load or unload 18845182ed5SBrian Bunker * START 1h - we are ready 18945182ed5SBrian Bunker */ 19045182ed5SBrian Bunker if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4)) 19145182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 19245182ed5SBrian Bunker 19345182ed5SBrian Bunker target_complete_cmd(cmd, SAM_STAT_GOOD); 19445182ed5SBrian Bunker return 0; 19545182ed5SBrian Bunker } 19645182ed5SBrian Bunker 197972b29c8SRoland Dreier sector_t sbc_get_write_same_sectors(struct se_cmd *cmd) 1981fd032eeSChristoph Hellwig { 1991fd032eeSChristoph Hellwig u32 num_blocks; 2001fd032eeSChristoph Hellwig 2011fd032eeSChristoph Hellwig if (cmd->t_task_cdb[0] == WRITE_SAME) 2021fd032eeSChristoph Hellwig num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]); 2031fd032eeSChristoph Hellwig else if (cmd->t_task_cdb[0] == WRITE_SAME_16) 2041fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]); 2051fd032eeSChristoph Hellwig else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */ 2061fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]); 2071fd032eeSChristoph Hellwig 2081fd032eeSChristoph Hellwig /* 2091fd032eeSChristoph Hellwig * Use the explicit range when non zero is supplied, otherwise calculate 2101fd032eeSChristoph Hellwig * the remaining range based on ->get_blocks() - starting LBA. 2111fd032eeSChristoph Hellwig */ 2126f974e8cSChristoph Hellwig if (num_blocks) 2136f974e8cSChristoph Hellwig return num_blocks; 2141fd032eeSChristoph Hellwig 2156f974e8cSChristoph Hellwig return cmd->se_dev->transport->get_blocks(cmd->se_dev) - 2166f974e8cSChristoph Hellwig cmd->t_task_lba + 1; 2171fd032eeSChristoph Hellwig } 218972b29c8SRoland Dreier EXPORT_SYMBOL(sbc_get_write_same_sectors); 2191fd032eeSChristoph Hellwig 220de103c93SChristoph Hellwig static sense_reason_t 221b753d643SChristoph Hellwig sbc_execute_write_same_unmap(struct se_cmd *cmd) 222b753d643SChristoph Hellwig { 223b753d643SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 224b753d643SChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 225b753d643SChristoph Hellwig sense_reason_t ret; 226b753d643SChristoph Hellwig 227b753d643SChristoph Hellwig if (nolb) { 228b753d643SChristoph Hellwig ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb); 229b753d643SChristoph Hellwig if (ret) 230b753d643SChristoph Hellwig return ret; 231b753d643SChristoph Hellwig } 232b753d643SChristoph Hellwig 233b753d643SChristoph Hellwig target_complete_cmd(cmd, GOOD); 234b753d643SChristoph Hellwig return 0; 235b753d643SChristoph Hellwig } 236b753d643SChristoph Hellwig 237b753d643SChristoph Hellwig static sense_reason_t 2381920ed61SNicholas Bellinger sbc_emulate_noop(struct se_cmd *cmd) 2391a1ff38cSBernhard Kohl { 2401a1ff38cSBernhard Kohl target_complete_cmd(cmd, GOOD); 2411a1ff38cSBernhard Kohl return 0; 2421a1ff38cSBernhard Kohl } 2431a1ff38cSBernhard Kohl 244d6e0175cSChristoph Hellwig static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) 245d6e0175cSChristoph Hellwig { 2460fd97ccfSChristoph Hellwig return cmd->se_dev->dev_attrib.block_size * sectors; 247d6e0175cSChristoph Hellwig } 248d6e0175cSChristoph Hellwig 249d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_6(unsigned char *cdb) 250d6e0175cSChristoph Hellwig { 251d6e0175cSChristoph Hellwig /* 252d6e0175cSChristoph Hellwig * Use 8-bit sector value. SBC-3 says: 253d6e0175cSChristoph Hellwig * 254d6e0175cSChristoph Hellwig * A TRANSFER LENGTH field set to zero specifies that 256 255d6e0175cSChristoph Hellwig * logical blocks shall be written. Any other value 256d6e0175cSChristoph Hellwig * specifies the number of logical blocks that shall be 257d6e0175cSChristoph Hellwig * written. 258d6e0175cSChristoph Hellwig */ 259d6e0175cSChristoph Hellwig return cdb[4] ? : 256; 260d6e0175cSChristoph Hellwig } 261d6e0175cSChristoph Hellwig 262d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_10(unsigned char *cdb) 263d6e0175cSChristoph Hellwig { 264d6e0175cSChristoph Hellwig return (u32)(cdb[7] << 8) + cdb[8]; 265d6e0175cSChristoph Hellwig } 266d6e0175cSChristoph Hellwig 267d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_12(unsigned char *cdb) 268d6e0175cSChristoph Hellwig { 269d6e0175cSChristoph Hellwig return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; 270d6e0175cSChristoph Hellwig } 271d6e0175cSChristoph Hellwig 272d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_16(unsigned char *cdb) 273d6e0175cSChristoph Hellwig { 274d6e0175cSChristoph Hellwig return (u32)(cdb[10] << 24) + (cdb[11] << 16) + 275d6e0175cSChristoph Hellwig (cdb[12] << 8) + cdb[13]; 276d6e0175cSChristoph Hellwig } 277d6e0175cSChristoph Hellwig 278d6e0175cSChristoph Hellwig /* 279d6e0175cSChristoph Hellwig * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants 280d6e0175cSChristoph Hellwig */ 281d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_32(unsigned char *cdb) 282d6e0175cSChristoph Hellwig { 283d6e0175cSChristoph Hellwig return (u32)(cdb[28] << 24) + (cdb[29] << 16) + 284d6e0175cSChristoph Hellwig (cdb[30] << 8) + cdb[31]; 285d6e0175cSChristoph Hellwig 286d6e0175cSChristoph Hellwig } 287d6e0175cSChristoph Hellwig 288d6e0175cSChristoph Hellwig static inline u32 transport_lba_21(unsigned char *cdb) 289d6e0175cSChristoph Hellwig { 290d6e0175cSChristoph Hellwig return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; 291d6e0175cSChristoph Hellwig } 292d6e0175cSChristoph Hellwig 293d6e0175cSChristoph Hellwig static inline u32 transport_lba_32(unsigned char *cdb) 294d6e0175cSChristoph Hellwig { 295d6e0175cSChristoph Hellwig return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; 296d6e0175cSChristoph Hellwig } 297d6e0175cSChristoph Hellwig 298d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64(unsigned char *cdb) 299d6e0175cSChristoph Hellwig { 300d6e0175cSChristoph Hellwig unsigned int __v1, __v2; 301d6e0175cSChristoph Hellwig 302d6e0175cSChristoph Hellwig __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; 303d6e0175cSChristoph Hellwig __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; 304d6e0175cSChristoph Hellwig 305d6e0175cSChristoph Hellwig return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 306d6e0175cSChristoph Hellwig } 307d6e0175cSChristoph Hellwig 308d6e0175cSChristoph Hellwig /* 309d6e0175cSChristoph Hellwig * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs 310d6e0175cSChristoph Hellwig */ 311d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) 312d6e0175cSChristoph Hellwig { 313d6e0175cSChristoph Hellwig unsigned int __v1, __v2; 314d6e0175cSChristoph Hellwig 315d6e0175cSChristoph Hellwig __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; 316d6e0175cSChristoph Hellwig __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; 317d6e0175cSChristoph Hellwig 318d6e0175cSChristoph Hellwig return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 319d6e0175cSChristoph Hellwig } 320d6e0175cSChristoph Hellwig 321cd063befSNicholas Bellinger static sense_reason_t 322cd063befSNicholas Bellinger sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops) 323d6e0175cSChristoph Hellwig { 3248e575c50SNicholas Bellinger struct se_device *dev = cmd->se_dev; 3258e575c50SNicholas Bellinger sector_t end_lba = dev->transport->get_blocks(dev) + 1; 326972b29c8SRoland Dreier unsigned int sectors = sbc_get_write_same_sectors(cmd); 327afd73f1bSNicholas Bellinger sense_reason_t ret; 328773cbaf7SNicholas Bellinger 329d6e0175cSChristoph Hellwig if ((flags[0] & 0x04) || (flags[0] & 0x02)) { 330d6e0175cSChristoph Hellwig pr_err("WRITE_SAME PBDATA and LBDATA" 331d6e0175cSChristoph Hellwig " bits not supported for Block Discard" 332d6e0175cSChristoph Hellwig " Emulation\n"); 333cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 334d6e0175cSChristoph Hellwig } 335773cbaf7SNicholas Bellinger if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) { 336773cbaf7SNicholas Bellinger pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n", 337773cbaf7SNicholas Bellinger sectors, cmd->se_dev->dev_attrib.max_write_same_len); 338773cbaf7SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 339773cbaf7SNicholas Bellinger } 3408e575c50SNicholas Bellinger /* 3418e575c50SNicholas Bellinger * Sanity check for LBA wrap and request past end of device. 3428e575c50SNicholas Bellinger */ 3438e575c50SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 3448e575c50SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 3458e575c50SNicholas Bellinger pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n", 3468e575c50SNicholas Bellinger (unsigned long long)end_lba, cmd->t_task_lba, sectors); 3478e575c50SNicholas Bellinger return TCM_ADDRESS_OUT_OF_RANGE; 3488e575c50SNicholas Bellinger } 3498e575c50SNicholas Bellinger 3505cb770bfSRoland Dreier /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */ 3515cb770bfSRoland Dreier if (flags[0] & 0x10) { 3525cb770bfSRoland Dreier pr_warn("WRITE SAME with ANCHOR not supported\n"); 3535cb770bfSRoland Dreier return TCM_INVALID_CDB_FIELD; 3545cb770bfSRoland Dreier } 355d6e0175cSChristoph Hellwig /* 356cd063befSNicholas Bellinger * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting 357cd063befSNicholas Bellinger * translated into block discard requests within backend code. 358d6e0175cSChristoph Hellwig */ 359cd063befSNicholas Bellinger if (flags[0] & 0x08) { 360b753d643SChristoph Hellwig if (!ops->execute_unmap) 361cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 362d6e0175cSChristoph Hellwig 363d0a91295SNicholas Bellinger if (!dev->dev_attrib.emulate_tpws) { 364d0a91295SNicholas Bellinger pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device" 365d0a91295SNicholas Bellinger " has emulate_tpws disabled\n"); 366d0a91295SNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 367d0a91295SNicholas Bellinger } 368b753d643SChristoph Hellwig cmd->execute_cmd = sbc_execute_write_same_unmap; 369cd063befSNicholas Bellinger return 0; 370cd063befSNicholas Bellinger } 371cd063befSNicholas Bellinger if (!ops->execute_write_same) 372cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 373cd063befSNicholas Bellinger 374afd73f1bSNicholas Bellinger ret = sbc_check_prot(dev, cmd, &cmd->t_task_cdb[0], sectors, true); 375afd73f1bSNicholas Bellinger if (ret) 376afd73f1bSNicholas Bellinger return ret; 377afd73f1bSNicholas Bellinger 378cd063befSNicholas Bellinger cmd->execute_cmd = ops->execute_write_same; 379d6e0175cSChristoph Hellwig return 0; 380d6e0175cSChristoph Hellwig } 381d6e0175cSChristoph Hellwig 382c8e63985SNicholas Bellinger static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success) 383d6e0175cSChristoph Hellwig { 384d6e0175cSChristoph Hellwig unsigned char *buf, *addr; 385d6e0175cSChristoph Hellwig struct scatterlist *sg; 386d6e0175cSChristoph Hellwig unsigned int offset; 387a6b0133cSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 388a6b0133cSNicholas Bellinger int i, count; 389d6e0175cSChristoph Hellwig /* 390d6e0175cSChristoph Hellwig * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command 391d6e0175cSChristoph Hellwig * 392d6e0175cSChristoph Hellwig * 1) read the specified logical block(s); 393d6e0175cSChristoph Hellwig * 2) transfer logical blocks from the data-out buffer; 394d6e0175cSChristoph Hellwig * 3) XOR the logical blocks transferred from the data-out buffer with 395d6e0175cSChristoph Hellwig * the logical blocks read, storing the resulting XOR data in a buffer; 396d6e0175cSChristoph Hellwig * 4) if the DISABLE WRITE bit is set to zero, then write the logical 397d6e0175cSChristoph Hellwig * blocks transferred from the data-out buffer; and 398d6e0175cSChristoph Hellwig * 5) transfer the resulting XOR data to the data-in buffer. 399d6e0175cSChristoph Hellwig */ 400d6e0175cSChristoph Hellwig buf = kmalloc(cmd->data_length, GFP_KERNEL); 401d6e0175cSChristoph Hellwig if (!buf) { 402d6e0175cSChristoph Hellwig pr_err("Unable to allocate xor_callback buf\n"); 403a6b0133cSNicholas Bellinger return TCM_OUT_OF_RESOURCES; 404d6e0175cSChristoph Hellwig } 405d6e0175cSChristoph Hellwig /* 406d6e0175cSChristoph Hellwig * Copy the scatterlist WRITE buffer located at cmd->t_data_sg 407d6e0175cSChristoph Hellwig * into the locally allocated *buf 408d6e0175cSChristoph Hellwig */ 409d6e0175cSChristoph Hellwig sg_copy_to_buffer(cmd->t_data_sg, 410d6e0175cSChristoph Hellwig cmd->t_data_nents, 411d6e0175cSChristoph Hellwig buf, 412d6e0175cSChristoph Hellwig cmd->data_length); 413d6e0175cSChristoph Hellwig 414d6e0175cSChristoph Hellwig /* 415d6e0175cSChristoph Hellwig * Now perform the XOR against the BIDI read memory located at 416d6e0175cSChristoph Hellwig * cmd->t_mem_bidi_list 417d6e0175cSChristoph Hellwig */ 418d6e0175cSChristoph Hellwig 419d6e0175cSChristoph Hellwig offset = 0; 420d6e0175cSChristoph Hellwig for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { 421d6e0175cSChristoph Hellwig addr = kmap_atomic(sg_page(sg)); 422a6b0133cSNicholas Bellinger if (!addr) { 423a6b0133cSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 424d6e0175cSChristoph Hellwig goto out; 425a6b0133cSNicholas Bellinger } 426d6e0175cSChristoph Hellwig 427d6e0175cSChristoph Hellwig for (i = 0; i < sg->length; i++) 428d6e0175cSChristoph Hellwig *(addr + sg->offset + i) ^= *(buf + offset + i); 429d6e0175cSChristoph Hellwig 430d6e0175cSChristoph Hellwig offset += sg->length; 431d6e0175cSChristoph Hellwig kunmap_atomic(addr); 432d6e0175cSChristoph Hellwig } 433d6e0175cSChristoph Hellwig 434d6e0175cSChristoph Hellwig out: 435d6e0175cSChristoph Hellwig kfree(buf); 436a6b0133cSNicholas Bellinger return ret; 437d6e0175cSChristoph Hellwig } 438d6e0175cSChristoph Hellwig 439a82a9538SNicholas Bellinger static sense_reason_t 440a82a9538SNicholas Bellinger sbc_execute_rw(struct se_cmd *cmd) 441a82a9538SNicholas Bellinger { 4427a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 4437a971b1bSChristoph Hellwig 4447a971b1bSChristoph Hellwig return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents, 445a82a9538SNicholas Bellinger cmd->data_direction); 446a82a9538SNicholas Bellinger } 447a82a9538SNicholas Bellinger 448c8e63985SNicholas Bellinger static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success) 44968ff9b9bSNicholas Bellinger { 45068ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 45168ff9b9bSNicholas Bellinger 452d8855c15SNicholas Bellinger /* 453d8855c15SNicholas Bellinger * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through 454d8855c15SNicholas Bellinger * within target_complete_ok_work() if the command was successfully 455d8855c15SNicholas Bellinger * sent to the backend driver. 456d8855c15SNicholas Bellinger */ 457d8855c15SNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 458d8855c15SNicholas Bellinger if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) 45968ff9b9bSNicholas Bellinger cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST; 460d8855c15SNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 461d8855c15SNicholas Bellinger 46268ff9b9bSNicholas Bellinger /* 46368ff9b9bSNicholas Bellinger * Unlock ->caw_sem originally obtained during sbc_compare_and_write() 46468ff9b9bSNicholas Bellinger * before the original READ I/O submission. 46568ff9b9bSNicholas Bellinger */ 46668ff9b9bSNicholas Bellinger up(&dev->caw_sem); 46768ff9b9bSNicholas Bellinger 46868ff9b9bSNicholas Bellinger return TCM_NO_SENSE; 46968ff9b9bSNicholas Bellinger } 47068ff9b9bSNicholas Bellinger 471c8e63985SNicholas Bellinger static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success) 47268ff9b9bSNicholas Bellinger { 47368ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 47468ff9b9bSNicholas Bellinger struct scatterlist *write_sg = NULL, *sg; 475db60df88SNicholas Bellinger unsigned char *buf = NULL, *addr; 47668ff9b9bSNicholas Bellinger struct sg_mapping_iter m; 47768ff9b9bSNicholas Bellinger unsigned int offset = 0, len; 47868ff9b9bSNicholas Bellinger unsigned int nlbas = cmd->t_task_nolb; 47968ff9b9bSNicholas Bellinger unsigned int block_size = dev->dev_attrib.block_size; 48068ff9b9bSNicholas Bellinger unsigned int compare_len = (nlbas * block_size); 48168ff9b9bSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 48268ff9b9bSNicholas Bellinger int rc, i; 48368ff9b9bSNicholas Bellinger 484cf6d1f09SNicholas Bellinger /* 485cf6d1f09SNicholas Bellinger * Handle early failure in transport_generic_request_failure(), 486c8e63985SNicholas Bellinger * which will not have taken ->caw_sem yet.. 487cf6d1f09SNicholas Bellinger */ 488c8e63985SNicholas Bellinger if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg)) 489cf6d1f09SNicholas Bellinger return TCM_NO_SENSE; 490db60df88SNicholas Bellinger /* 491c8e63985SNicholas Bellinger * Handle special case for zero-length COMPARE_AND_WRITE 492c8e63985SNicholas Bellinger */ 493c8e63985SNicholas Bellinger if (!cmd->data_length) 494c8e63985SNicholas Bellinger goto out; 495c8e63985SNicholas Bellinger /* 496db60df88SNicholas Bellinger * Immediately exit + release dev->caw_sem if command has already 497db60df88SNicholas Bellinger * been failed with a non-zero SCSI status. 498db60df88SNicholas Bellinger */ 499db60df88SNicholas Bellinger if (cmd->scsi_status) { 500db60df88SNicholas Bellinger pr_err("compare_and_write_callback: non zero scsi_status:" 501db60df88SNicholas Bellinger " 0x%02x\n", cmd->scsi_status); 502db60df88SNicholas Bellinger goto out; 503db60df88SNicholas Bellinger } 504cf6d1f09SNicholas Bellinger 50568ff9b9bSNicholas Bellinger buf = kzalloc(cmd->data_length, GFP_KERNEL); 50668ff9b9bSNicholas Bellinger if (!buf) { 50768ff9b9bSNicholas Bellinger pr_err("Unable to allocate compare_and_write buf\n"); 508a2890087SNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 509a2890087SNicholas Bellinger goto out; 51068ff9b9bSNicholas Bellinger } 51168ff9b9bSNicholas Bellinger 512a1e1774cSMartin Svec write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents, 51368ff9b9bSNicholas Bellinger GFP_KERNEL); 51468ff9b9bSNicholas Bellinger if (!write_sg) { 51568ff9b9bSNicholas Bellinger pr_err("Unable to allocate compare_and_write sg\n"); 51668ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 51768ff9b9bSNicholas Bellinger goto out; 51868ff9b9bSNicholas Bellinger } 519a1e1774cSMartin Svec sg_init_table(write_sg, cmd->t_data_nents); 52068ff9b9bSNicholas Bellinger /* 52168ff9b9bSNicholas Bellinger * Setup verify and write data payloads from total NumberLBAs. 52268ff9b9bSNicholas Bellinger */ 52368ff9b9bSNicholas Bellinger rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf, 52468ff9b9bSNicholas Bellinger cmd->data_length); 52568ff9b9bSNicholas Bellinger if (!rc) { 52668ff9b9bSNicholas Bellinger pr_err("sg_copy_to_buffer() failed for compare_and_write\n"); 52768ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 52868ff9b9bSNicholas Bellinger goto out; 52968ff9b9bSNicholas Bellinger } 53068ff9b9bSNicholas Bellinger /* 53168ff9b9bSNicholas Bellinger * Compare against SCSI READ payload against verify payload 53268ff9b9bSNicholas Bellinger */ 53368ff9b9bSNicholas Bellinger for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) { 53468ff9b9bSNicholas Bellinger addr = (unsigned char *)kmap_atomic(sg_page(sg)); 53568ff9b9bSNicholas Bellinger if (!addr) { 53668ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 53768ff9b9bSNicholas Bellinger goto out; 53868ff9b9bSNicholas Bellinger } 53968ff9b9bSNicholas Bellinger 54068ff9b9bSNicholas Bellinger len = min(sg->length, compare_len); 54168ff9b9bSNicholas Bellinger 54268ff9b9bSNicholas Bellinger if (memcmp(addr, buf + offset, len)) { 54368ff9b9bSNicholas Bellinger pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n", 54468ff9b9bSNicholas Bellinger addr, buf + offset); 54568ff9b9bSNicholas Bellinger kunmap_atomic(addr); 54668ff9b9bSNicholas Bellinger goto miscompare; 54768ff9b9bSNicholas Bellinger } 54868ff9b9bSNicholas Bellinger kunmap_atomic(addr); 54968ff9b9bSNicholas Bellinger 55068ff9b9bSNicholas Bellinger offset += len; 55168ff9b9bSNicholas Bellinger compare_len -= len; 55268ff9b9bSNicholas Bellinger if (!compare_len) 55368ff9b9bSNicholas Bellinger break; 55468ff9b9bSNicholas Bellinger } 55568ff9b9bSNicholas Bellinger 55668ff9b9bSNicholas Bellinger i = 0; 55768ff9b9bSNicholas Bellinger len = cmd->t_task_nolb * block_size; 55868ff9b9bSNicholas Bellinger sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG); 55968ff9b9bSNicholas Bellinger /* 56068ff9b9bSNicholas Bellinger * Currently assumes NoLB=1 and SGLs are PAGE_SIZE.. 56168ff9b9bSNicholas Bellinger */ 56268ff9b9bSNicholas Bellinger while (len) { 56368ff9b9bSNicholas Bellinger sg_miter_next(&m); 56468ff9b9bSNicholas Bellinger 56568ff9b9bSNicholas Bellinger if (block_size < PAGE_SIZE) { 56668ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 56768ff9b9bSNicholas Bellinger block_size); 56868ff9b9bSNicholas Bellinger } else { 56968ff9b9bSNicholas Bellinger sg_miter_next(&m); 57068ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 57168ff9b9bSNicholas Bellinger 0); 57268ff9b9bSNicholas Bellinger } 57368ff9b9bSNicholas Bellinger len -= block_size; 57468ff9b9bSNicholas Bellinger i++; 57568ff9b9bSNicholas Bellinger } 57668ff9b9bSNicholas Bellinger sg_miter_stop(&m); 57768ff9b9bSNicholas Bellinger /* 57868ff9b9bSNicholas Bellinger * Save the original SGL + nents values before updating to new 57968ff9b9bSNicholas Bellinger * assignments, to be released in transport_free_pages() -> 58068ff9b9bSNicholas Bellinger * transport_reset_sgl_orig() 58168ff9b9bSNicholas Bellinger */ 58268ff9b9bSNicholas Bellinger cmd->t_data_sg_orig = cmd->t_data_sg; 58368ff9b9bSNicholas Bellinger cmd->t_data_sg = write_sg; 58468ff9b9bSNicholas Bellinger cmd->t_data_nents_orig = cmd->t_data_nents; 58568ff9b9bSNicholas Bellinger cmd->t_data_nents = 1; 58668ff9b9bSNicholas Bellinger 58768d81f40SChristoph Hellwig cmd->sam_task_attr = TCM_HEAD_TAG; 58868ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_post; 58968ff9b9bSNicholas Bellinger /* 59068ff9b9bSNicholas Bellinger * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler 59168ff9b9bSNicholas Bellinger * for submitting the adjusted SGL to write instance user-data. 59268ff9b9bSNicholas Bellinger */ 59368ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 59468ff9b9bSNicholas Bellinger 59568ff9b9bSNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 59668ff9b9bSNicholas Bellinger cmd->t_state = TRANSPORT_PROCESSING; 59768ff9b9bSNicholas Bellinger cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT; 59868ff9b9bSNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 59968ff9b9bSNicholas Bellinger 60068ff9b9bSNicholas Bellinger __target_execute_cmd(cmd); 60168ff9b9bSNicholas Bellinger 60268ff9b9bSNicholas Bellinger kfree(buf); 60368ff9b9bSNicholas Bellinger return ret; 60468ff9b9bSNicholas Bellinger 60568ff9b9bSNicholas Bellinger miscompare: 60668ff9b9bSNicholas Bellinger pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n", 60768ff9b9bSNicholas Bellinger dev->transport->name); 60868ff9b9bSNicholas Bellinger ret = TCM_MISCOMPARE_VERIFY; 60968ff9b9bSNicholas Bellinger out: 61068ff9b9bSNicholas Bellinger /* 61168ff9b9bSNicholas Bellinger * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in 61268ff9b9bSNicholas Bellinger * sbc_compare_and_write() before the original READ I/O submission. 61368ff9b9bSNicholas Bellinger */ 61468ff9b9bSNicholas Bellinger up(&dev->caw_sem); 61568ff9b9bSNicholas Bellinger kfree(write_sg); 61668ff9b9bSNicholas Bellinger kfree(buf); 61768ff9b9bSNicholas Bellinger return ret; 61868ff9b9bSNicholas Bellinger } 61968ff9b9bSNicholas Bellinger 62068ff9b9bSNicholas Bellinger static sense_reason_t 62168ff9b9bSNicholas Bellinger sbc_compare_and_write(struct se_cmd *cmd) 62268ff9b9bSNicholas Bellinger { 6237a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 62468ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 62568ff9b9bSNicholas Bellinger sense_reason_t ret; 62668ff9b9bSNicholas Bellinger int rc; 62768ff9b9bSNicholas Bellinger /* 62868ff9b9bSNicholas Bellinger * Submit the READ first for COMPARE_AND_WRITE to perform the 62968ff9b9bSNicholas Bellinger * comparision using SGLs at cmd->t_bidi_data_sg.. 63068ff9b9bSNicholas Bellinger */ 63168ff9b9bSNicholas Bellinger rc = down_interruptible(&dev->caw_sem); 632ee7619f2SNicholas Bellinger if (rc != 0) { 63368ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 63468ff9b9bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 63568ff9b9bSNicholas Bellinger } 636b7191253SNicholas Bellinger /* 637b7191253SNicholas Bellinger * Reset cmd->data_length to individual block_size in order to not 638b7191253SNicholas Bellinger * confuse backend drivers that depend on this value matching the 639b7191253SNicholas Bellinger * size of the I/O being submitted. 640b7191253SNicholas Bellinger */ 641b7191253SNicholas Bellinger cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size; 64268ff9b9bSNicholas Bellinger 6437a971b1bSChristoph Hellwig ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents, 64468ff9b9bSNicholas Bellinger DMA_FROM_DEVICE); 64568ff9b9bSNicholas Bellinger if (ret) { 64668ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 64768ff9b9bSNicholas Bellinger up(&dev->caw_sem); 64868ff9b9bSNicholas Bellinger return ret; 64968ff9b9bSNicholas Bellinger } 65068ff9b9bSNicholas Bellinger /* 65168ff9b9bSNicholas Bellinger * Unlock of dev->caw_sem to occur in compare_and_write_callback() 65268ff9b9bSNicholas Bellinger * upon MISCOMPARE, or in compare_and_write_done() upon completion 65368ff9b9bSNicholas Bellinger * of WRITE instance user-data. 65468ff9b9bSNicholas Bellinger */ 65568ff9b9bSNicholas Bellinger return TCM_NO_SENSE; 65668ff9b9bSNicholas Bellinger } 65768ff9b9bSNicholas Bellinger 65819f9361aSSagi Grimberg static int 65938b57f82SNicholas Bellinger sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type, 66019f9361aSSagi Grimberg bool is_write, struct se_cmd *cmd) 66119f9361aSSagi Grimberg { 66219f9361aSSagi Grimberg if (is_write) { 66338b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP : 66438b57f82SNicholas Bellinger protect ? TARGET_PROT_DOUT_PASS : 66519f9361aSSagi Grimberg TARGET_PROT_DOUT_INSERT; 66619f9361aSSagi Grimberg switch (protect) { 66719f9361aSSagi Grimberg case 0x0: 66819f9361aSSagi Grimberg case 0x3: 66919f9361aSSagi Grimberg cmd->prot_checks = 0; 67019f9361aSSagi Grimberg break; 67119f9361aSSagi Grimberg case 0x1: 67219f9361aSSagi Grimberg case 0x5: 67319f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 67419f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 67519f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 67619f9361aSSagi Grimberg break; 67719f9361aSSagi Grimberg case 0x2: 67819f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 67919f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 68019f9361aSSagi Grimberg break; 68119f9361aSSagi Grimberg case 0x4: 68219f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 68319f9361aSSagi Grimberg break; 68419f9361aSSagi Grimberg default: 68519f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 68619f9361aSSagi Grimberg return -EINVAL; 68719f9361aSSagi Grimberg } 68819f9361aSSagi Grimberg } else { 68938b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT : 69038b57f82SNicholas Bellinger protect ? TARGET_PROT_DIN_PASS : 69119f9361aSSagi Grimberg TARGET_PROT_DIN_STRIP; 69219f9361aSSagi Grimberg switch (protect) { 69319f9361aSSagi Grimberg case 0x0: 69419f9361aSSagi Grimberg case 0x1: 69519f9361aSSagi Grimberg case 0x5: 69619f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 69719f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 69819f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 69919f9361aSSagi Grimberg break; 70019f9361aSSagi Grimberg case 0x2: 70119f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 70219f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 70319f9361aSSagi Grimberg break; 70419f9361aSSagi Grimberg case 0x3: 70519f9361aSSagi Grimberg cmd->prot_checks = 0; 70619f9361aSSagi Grimberg break; 70719f9361aSSagi Grimberg case 0x4: 70819f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 70919f9361aSSagi Grimberg break; 71019f9361aSSagi Grimberg default: 71119f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 71219f9361aSSagi Grimberg return -EINVAL; 71319f9361aSSagi Grimberg } 71419f9361aSSagi Grimberg } 71519f9361aSSagi Grimberg 71619f9361aSSagi Grimberg return 0; 71719f9361aSSagi Grimberg } 71819f9361aSSagi Grimberg 719f7b7c06fSNicholas Bellinger static sense_reason_t 720499bf77bSNicholas Bellinger sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb, 72119f9361aSSagi Grimberg u32 sectors, bool is_write) 722499bf77bSNicholas Bellinger { 72319f9361aSSagi Grimberg u8 protect = cdb[1] >> 5; 72438b57f82SNicholas Bellinger int sp_ops = cmd->se_sess->sup_prot_ops; 72538b57f82SNicholas Bellinger int pi_prot_type = dev->dev_attrib.pi_prot_type; 72638b57f82SNicholas Bellinger bool fabric_prot = false; 72719f9361aSSagi Grimberg 728f7b7c06fSNicholas Bellinger if (!cmd->t_prot_sg || !cmd->t_prot_nents) { 72938b57f82SNicholas Bellinger if (unlikely(protect && 73038b57f82SNicholas Bellinger !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) { 73138b57f82SNicholas Bellinger pr_err("CDB contains protect bit, but device + fabric does" 73238b57f82SNicholas Bellinger " not advertise PROTECT=1 feature bit\n"); 733f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 734f7b7c06fSNicholas Bellinger } 735f7b7c06fSNicholas Bellinger if (cmd->prot_pto) 736f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 737f7b7c06fSNicholas Bellinger } 738499bf77bSNicholas Bellinger 739499bf77bSNicholas Bellinger switch (dev->dev_attrib.pi_prot_type) { 740499bf77bSNicholas Bellinger case TARGET_DIF_TYPE3_PROT: 741499bf77bSNicholas Bellinger cmd->reftag_seed = 0xffffffff; 742499bf77bSNicholas Bellinger break; 743499bf77bSNicholas Bellinger case TARGET_DIF_TYPE2_PROT: 74419f9361aSSagi Grimberg if (protect) 745f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 746499bf77bSNicholas Bellinger 747499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 748499bf77bSNicholas Bellinger break; 749499bf77bSNicholas Bellinger case TARGET_DIF_TYPE1_PROT: 750499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 751499bf77bSNicholas Bellinger break; 752499bf77bSNicholas Bellinger case TARGET_DIF_TYPE0_PROT: 75338b57f82SNicholas Bellinger /* 75438b57f82SNicholas Bellinger * See if the fabric supports T10-PI, and the session has been 75538b57f82SNicholas Bellinger * configured to allow export PROTECT=1 feature bit with backend 75638b57f82SNicholas Bellinger * devices that don't support T10-PI. 75738b57f82SNicholas Bellinger */ 75838b57f82SNicholas Bellinger fabric_prot = is_write ? 75938b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) : 76038b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT)); 76138b57f82SNicholas Bellinger 76238b57f82SNicholas Bellinger if (fabric_prot && cmd->se_sess->sess_prot_type) { 76338b57f82SNicholas Bellinger pi_prot_type = cmd->se_sess->sess_prot_type; 76438b57f82SNicholas Bellinger break; 76538b57f82SNicholas Bellinger } 766cceca4a6SNicholas Bellinger if (!protect) 767f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 76838b57f82SNicholas Bellinger /* Fallthrough */ 769499bf77bSNicholas Bellinger default: 770cceca4a6SNicholas Bellinger pr_err("Unable to determine pi_prot_type for CDB: 0x%02x " 771cceca4a6SNicholas Bellinger "PROTECT: 0x%02x\n", cdb[0], protect); 772cceca4a6SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 773499bf77bSNicholas Bellinger } 774499bf77bSNicholas Bellinger 77538b57f82SNicholas Bellinger if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd)) 776f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 77719f9361aSSagi Grimberg 77838b57f82SNicholas Bellinger cmd->prot_type = pi_prot_type; 779499bf77bSNicholas Bellinger cmd->prot_length = dev->prot_length * sectors; 780e2a4f55cSSagi Grimberg 781e2a4f55cSSagi Grimberg /** 782e2a4f55cSSagi Grimberg * In case protection information exists over the wire 783e2a4f55cSSagi Grimberg * we modify command data length to describe pure data. 784e2a4f55cSSagi Grimberg * The actual transfer length is data length + protection 785e2a4f55cSSagi Grimberg * length 786e2a4f55cSSagi Grimberg **/ 787e2a4f55cSSagi Grimberg if (protect) 788e2a4f55cSSagi Grimberg cmd->data_length = sectors * dev->dev_attrib.block_size; 789e2a4f55cSSagi Grimberg 790e2a4f55cSSagi Grimberg pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d " 791e2a4f55cSSagi Grimberg "prot_op=%d prot_checks=%d\n", 792e2a4f55cSSagi Grimberg __func__, cmd->prot_type, cmd->data_length, cmd->prot_length, 79303abad9eSSagi Grimberg cmd->prot_op, cmd->prot_checks); 794499bf77bSNicholas Bellinger 795f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 796499bf77bSNicholas Bellinger } 797499bf77bSNicholas Bellinger 798fde9f50fSNicholas Bellinger static int 799fde9f50fSNicholas Bellinger sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb) 800fde9f50fSNicholas Bellinger { 801fde9f50fSNicholas Bellinger if (cdb[1] & 0x10) { 802814e5b45SChristoph Hellwig /* see explanation in spc_emulate_modesense */ 803814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 804fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with DPO bit set, but device" 805fde9f50fSNicholas Bellinger " does not advertise support for DPO\n", cdb[0]); 806fde9f50fSNicholas Bellinger return -EINVAL; 807fde9f50fSNicholas Bellinger } 808fde9f50fSNicholas Bellinger } 809fde9f50fSNicholas Bellinger if (cdb[1] & 0x8) { 810814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 811fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with FUA bit set, but device" 812fde9f50fSNicholas Bellinger " does not advertise support for FUA write\n", 813fde9f50fSNicholas Bellinger cdb[0]); 814fde9f50fSNicholas Bellinger return -EINVAL; 815fde9f50fSNicholas Bellinger } 816fde9f50fSNicholas Bellinger cmd->se_cmd_flags |= SCF_FUA; 817fde9f50fSNicholas Bellinger } 818fde9f50fSNicholas Bellinger return 0; 819d6e0175cSChristoph Hellwig } 820d6e0175cSChristoph Hellwig 821de103c93SChristoph Hellwig sense_reason_t 822de103c93SChristoph Hellwig sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) 823d6e0175cSChristoph Hellwig { 824d6e0175cSChristoph Hellwig struct se_device *dev = cmd->se_dev; 825d6e0175cSChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 8261fd032eeSChristoph Hellwig unsigned int size; 827d6e0175cSChristoph Hellwig u32 sectors = 0; 828de103c93SChristoph Hellwig sense_reason_t ret; 829d6e0175cSChristoph Hellwig 8307a971b1bSChristoph Hellwig cmd->protocol_data = ops; 8317a971b1bSChristoph Hellwig 832d6e0175cSChristoph Hellwig switch (cdb[0]) { 833d6e0175cSChristoph Hellwig case READ_6: 834d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 835d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 836d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 837a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 838d6e0175cSChristoph Hellwig break; 839d6e0175cSChristoph Hellwig case READ_10: 840d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 841d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 842499bf77bSNicholas Bellinger 843fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 844fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 845fde9f50fSNicholas Bellinger 846f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 847f7b7c06fSNicholas Bellinger if (ret) 848f7b7c06fSNicholas Bellinger return ret; 849499bf77bSNicholas Bellinger 850d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 851a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 852d6e0175cSChristoph Hellwig break; 853d6e0175cSChristoph Hellwig case READ_12: 854d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 855d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 856499bf77bSNicholas Bellinger 857fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 858fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 859fde9f50fSNicholas Bellinger 860f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 861f7b7c06fSNicholas Bellinger if (ret) 862f7b7c06fSNicholas Bellinger return ret; 863499bf77bSNicholas Bellinger 864d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 865a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 866d6e0175cSChristoph Hellwig break; 867d6e0175cSChristoph Hellwig case READ_16: 868d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 869d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 870499bf77bSNicholas Bellinger 871fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 872fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 873fde9f50fSNicholas Bellinger 874f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 875f7b7c06fSNicholas Bellinger if (ret) 876f7b7c06fSNicholas Bellinger return ret; 877499bf77bSNicholas Bellinger 878d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 879a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 880d6e0175cSChristoph Hellwig break; 881d6e0175cSChristoph Hellwig case WRITE_6: 882d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 883d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 884d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 885a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 886d6e0175cSChristoph Hellwig break; 887d6e0175cSChristoph Hellwig case WRITE_10: 888d6e0175cSChristoph Hellwig case WRITE_VERIFY: 889d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 890d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 891499bf77bSNicholas Bellinger 892fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 893fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 894499bf77bSNicholas Bellinger 895f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 896f7b7c06fSNicholas Bellinger if (ret) 897f7b7c06fSNicholas Bellinger return ret; 898d6e0175cSChristoph Hellwig 899d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 900a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 901d6e0175cSChristoph Hellwig break; 902d6e0175cSChristoph Hellwig case WRITE_12: 903d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 904d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 905499bf77bSNicholas Bellinger 906fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 907fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 908499bf77bSNicholas Bellinger 909f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 910f7b7c06fSNicholas Bellinger if (ret) 911f7b7c06fSNicholas Bellinger return ret; 912d6e0175cSChristoph Hellwig 913d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 914a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 915d6e0175cSChristoph Hellwig break; 916d6e0175cSChristoph Hellwig case WRITE_16: 917d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 918d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 919499bf77bSNicholas Bellinger 920fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 921fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 922499bf77bSNicholas Bellinger 923f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 924f7b7c06fSNicholas Bellinger if (ret) 925f7b7c06fSNicholas Bellinger return ret; 926cd063befSNicholas Bellinger 927d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 928d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 929d6e0175cSChristoph Hellwig break; 930d6e0175cSChristoph Hellwig case XDWRITEREAD_10: 931d6e0175cSChristoph Hellwig if (cmd->data_direction != DMA_TO_DEVICE || 932d6e0175cSChristoph Hellwig !(cmd->se_cmd_flags & SCF_BIDI)) 933d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 934d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 935d6e0175cSChristoph Hellwig 936fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 937fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 938fde9f50fSNicholas Bellinger 939d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 940d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 941d6e0175cSChristoph Hellwig 942d6e0175cSChristoph Hellwig /* 943d6e0175cSChristoph Hellwig * Setup BIDI XOR callback to be run after I/O completion. 944d6e0175cSChristoph Hellwig */ 945d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 946d6e0175cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 9470c2ad7d1SChristoph Hellwig break; 948d6e0175cSChristoph Hellwig case VARIABLE_LENGTH_CMD: 949d6e0175cSChristoph Hellwig { 950d6e0175cSChristoph Hellwig u16 service_action = get_unaligned_be16(&cdb[8]); 951d6e0175cSChristoph Hellwig switch (service_action) { 952d6e0175cSChristoph Hellwig case XDWRITEREAD_32: 9536f974e8cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 954de103c93SChristoph Hellwig 955fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 956fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 9576f974e8cSChristoph Hellwig /* 958d6e0175cSChristoph Hellwig * Use WRITE_32 and READ_32 opcodes for the emulated 959d6e0175cSChristoph Hellwig * XDWRITE_READ_32 logic. 960d6e0175cSChristoph Hellwig */ 961d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64_ext(cdb); 962de103c93SChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 963d6e0175cSChristoph Hellwig 964d6e0175cSChristoph Hellwig /* 9651fd032eeSChristoph Hellwig * Setup BIDI XOR callback to be run during after I/O 966d6e0175cSChristoph Hellwig * completion. 967d6e0175cSChristoph Hellwig */ 968de103c93SChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 9696f974e8cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 970d6e0175cSChristoph Hellwig break; 971d6e0175cSChristoph Hellwig case WRITE_SAME_32: 972d6e0175cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 973d6e0175cSChristoph Hellwig if (!sectors) { 974d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" 975d6e0175cSChristoph Hellwig " supported\n"); 976d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 977d6e0175cSChristoph Hellwig } 978d6e0175cSChristoph Hellwig 979d6e0175cSChristoph Hellwig size = sbc_get_size(cmd, 1); 980d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[12]); 981d6e0175cSChristoph Hellwig 982d6e0175cSChristoph Hellwig ret = sbc_setup_write_same(cmd, &cdb[10], ops); 983d6e0175cSChristoph Hellwig if (ret) 9841fd032eeSChristoph Hellwig return ret; 985d6e0175cSChristoph Hellwig break; 986d6e0175cSChristoph Hellwig default: 987d6e0175cSChristoph Hellwig pr_err("VARIABLE_LENGTH_CMD service action" 988d6e0175cSChristoph Hellwig " 0x%04x not supported\n", service_action); 989de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 990d6e0175cSChristoph Hellwig } 991d6e0175cSChristoph Hellwig break; 992d6e0175cSChristoph Hellwig } 99368ff9b9bSNicholas Bellinger case COMPARE_AND_WRITE: 99468ff9b9bSNicholas Bellinger sectors = cdb[13]; 99568ff9b9bSNicholas Bellinger /* 99668ff9b9bSNicholas Bellinger * Currently enforce COMPARE_AND_WRITE for a single sector 99768ff9b9bSNicholas Bellinger */ 99868ff9b9bSNicholas Bellinger if (sectors > 1) { 99968ff9b9bSNicholas Bellinger pr_err("COMPARE_AND_WRITE contains NoLB: %u greater" 100068ff9b9bSNicholas Bellinger " than 1\n", sectors); 100168ff9b9bSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 100268ff9b9bSNicholas Bellinger } 1003ab81a5e0SDavid Disseldorp if (sbc_check_dpofua(dev, cmd, cdb)) 1004ab81a5e0SDavid Disseldorp return TCM_INVALID_CDB_FIELD; 1005ab81a5e0SDavid Disseldorp 100668ff9b9bSNicholas Bellinger /* 100768ff9b9bSNicholas Bellinger * Double size because we have two buffers, note that 100868ff9b9bSNicholas Bellinger * zero is not an error.. 100968ff9b9bSNicholas Bellinger */ 101068ff9b9bSNicholas Bellinger size = 2 * sbc_get_size(cmd, sectors); 101168ff9b9bSNicholas Bellinger cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 101268ff9b9bSNicholas Bellinger cmd->t_task_nolb = sectors; 101368ff9b9bSNicholas Bellinger cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE; 101468ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_compare_and_write; 101568ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_callback; 101668ff9b9bSNicholas Bellinger break; 1017d6e0175cSChristoph Hellwig case READ_CAPACITY: 10181fd032eeSChristoph Hellwig size = READ_CAP_LEN; 10191fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity; 1020d6e0175cSChristoph Hellwig break; 1021eb846d9fSHannes Reinecke case SERVICE_ACTION_IN_16: 1022d6e0175cSChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 1023d6e0175cSChristoph Hellwig case SAI_READ_CAPACITY_16: 10241fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity_16; 1025d6e0175cSChristoph Hellwig break; 1026c66094bfSHannes Reinecke case SAI_REPORT_REFERRALS: 1027c66094bfSHannes Reinecke cmd->execute_cmd = target_emulate_report_referrals; 1028c66094bfSHannes Reinecke break; 1029d6e0175cSChristoph Hellwig default: 1030d6e0175cSChristoph Hellwig pr_err("Unsupported SA: 0x%02x\n", 1031d6e0175cSChristoph Hellwig cmd->t_task_cdb[1] & 0x1f); 1032de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1033d6e0175cSChristoph Hellwig } 10341fd032eeSChristoph Hellwig size = (cdb[10] << 24) | (cdb[11] << 16) | 1035d6e0175cSChristoph Hellwig (cdb[12] << 8) | cdb[13]; 1036d6e0175cSChristoph Hellwig break; 1037d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE: 1038d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE_16: 1039d6e0175cSChristoph Hellwig if (cdb[0] == SYNCHRONIZE_CACHE) { 1040d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1041d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 1042d6e0175cSChristoph Hellwig } else { 1043d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1044d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 1045d6e0175cSChristoph Hellwig } 10466ef31dc7SChristophe Vu-Brugier if (ops->execute_sync_cache) { 1047ad67f0d9SChristoph Hellwig cmd->execute_cmd = ops->execute_sync_cache; 10486ef31dc7SChristophe Vu-Brugier goto check_lba; 10496ef31dc7SChristophe Vu-Brugier } 10506ef31dc7SChristophe Vu-Brugier size = 0; 10516ef31dc7SChristophe Vu-Brugier cmd->execute_cmd = sbc_emulate_noop; 1052d6e0175cSChristoph Hellwig break; 1053d6e0175cSChristoph Hellwig case UNMAP: 105414150a6bSChristoph Hellwig if (!ops->execute_unmap) 1055de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 105614150a6bSChristoph Hellwig 105761fdb4acSNicholas Bellinger if (!dev->dev_attrib.emulate_tpu) { 105861fdb4acSNicholas Bellinger pr_err("Got UNMAP, but backend device has" 105961fdb4acSNicholas Bellinger " emulate_tpu disabled\n"); 106061fdb4acSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 106161fdb4acSNicholas Bellinger } 10621fd032eeSChristoph Hellwig size = get_unaligned_be16(&cdb[7]); 106362e46942SChristoph Hellwig cmd->execute_cmd = sbc_execute_unmap; 1064d6e0175cSChristoph Hellwig break; 1065d6e0175cSChristoph Hellwig case WRITE_SAME_16: 1066d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1067d6e0175cSChristoph Hellwig if (!sectors) { 1068d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1069de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1070d6e0175cSChristoph Hellwig } 1071d6e0175cSChristoph Hellwig 10721fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1073d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 1074d6e0175cSChristoph Hellwig 1075cd063befSNicholas Bellinger ret = sbc_setup_write_same(cmd, &cdb[1], ops); 10766b64e1feSDan Carpenter if (ret) 1077cd063befSNicholas Bellinger return ret; 1078d6e0175cSChristoph Hellwig break; 1079d6e0175cSChristoph Hellwig case WRITE_SAME: 1080d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1081d6e0175cSChristoph Hellwig if (!sectors) { 1082d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1083de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1084d6e0175cSChristoph Hellwig } 1085d6e0175cSChristoph Hellwig 10861fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1087d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be32(&cdb[2]); 1088d6e0175cSChristoph Hellwig 1089d6e0175cSChristoph Hellwig /* 1090d6e0175cSChristoph Hellwig * Follow sbcr26 with WRITE_SAME (10) and check for the existence 1091d6e0175cSChristoph Hellwig * of byte 1 bit 3 UNMAP instead of original reserved field 1092d6e0175cSChristoph Hellwig */ 1093cd063befSNicholas Bellinger ret = sbc_setup_write_same(cmd, &cdb[1], ops); 10946b64e1feSDan Carpenter if (ret) 1095cd063befSNicholas Bellinger return ret; 1096d6e0175cSChristoph Hellwig break; 1097d6e0175cSChristoph Hellwig case VERIFY: 10981fd032eeSChristoph Hellwig size = 0; 1099c52716deSChristophe Vu-Brugier sectors = transport_get_sectors_10(cdb); 1100c52716deSChristophe Vu-Brugier cmd->t_task_lba = transport_lba_32(cdb); 11011920ed61SNicholas Bellinger cmd->execute_cmd = sbc_emulate_noop; 1102c52716deSChristophe Vu-Brugier goto check_lba; 11031a1ff38cSBernhard Kohl case REZERO_UNIT: 11041a1ff38cSBernhard Kohl case SEEK_6: 11051a1ff38cSBernhard Kohl case SEEK_10: 11061a1ff38cSBernhard Kohl /* 11071a1ff38cSBernhard Kohl * There are still clients out there which use these old SCSI-2 11081a1ff38cSBernhard Kohl * commands. This mainly happens when running VMs with legacy 11091a1ff38cSBernhard Kohl * guest systems, connected via SCSI command pass-through to 11101a1ff38cSBernhard Kohl * iSCSI targets. Make them happy and return status GOOD. 11111a1ff38cSBernhard Kohl */ 11121a1ff38cSBernhard Kohl size = 0; 11131a1ff38cSBernhard Kohl cmd->execute_cmd = sbc_emulate_noop; 11141a1ff38cSBernhard Kohl break; 111545182ed5SBrian Bunker case START_STOP: 111645182ed5SBrian Bunker size = 0; 111745182ed5SBrian Bunker cmd->execute_cmd = sbc_emulate_startstop; 111845182ed5SBrian Bunker break; 1119d6e0175cSChristoph Hellwig default: 11201fd032eeSChristoph Hellwig ret = spc_parse_cdb(cmd, &size); 1121d6e0175cSChristoph Hellwig if (ret) 1122d6e0175cSChristoph Hellwig return ret; 1123d6e0175cSChristoph Hellwig } 1124d6e0175cSChristoph Hellwig 1125d6e0175cSChristoph Hellwig /* reject any command that we don't have a handler for */ 112620959c4bSAndy Grover if (!cmd->execute_cmd) 1127de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 1128d6e0175cSChristoph Hellwig 1129d6e0175cSChristoph Hellwig if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { 11301fd032eeSChristoph Hellwig unsigned long long end_lba; 11316ef31dc7SChristophe Vu-Brugier check_lba: 11321fd032eeSChristoph Hellwig end_lba = dev->transport->get_blocks(dev) + 1; 1133aa179935SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 1134aa179935SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 11351fd032eeSChristoph Hellwig pr_err("cmd exceeds last lba %llu " 11361fd032eeSChristoph Hellwig "(lba %llu, sectors %u)\n", 11371fd032eeSChristoph Hellwig end_lba, cmd->t_task_lba, sectors); 113809ceadc7SRoland Dreier return TCM_ADDRESS_OUT_OF_RANGE; 1139d6e0175cSChristoph Hellwig } 1140d6e0175cSChristoph Hellwig 114168ff9b9bSNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) 11421fd032eeSChristoph Hellwig size = sbc_get_size(cmd, sectors); 11431fd032eeSChristoph Hellwig } 11441fd032eeSChristoph Hellwig 1145de103c93SChristoph Hellwig return target_cmd_size_check(cmd, size); 1146d6e0175cSChristoph Hellwig } 1147d6e0175cSChristoph Hellwig EXPORT_SYMBOL(sbc_parse_cdb); 11486f23ac8aSChristoph Hellwig 11496f23ac8aSChristoph Hellwig u32 sbc_get_device_type(struct se_device *dev) 11506f23ac8aSChristoph Hellwig { 11516f23ac8aSChristoph Hellwig return TYPE_DISK; 11526f23ac8aSChristoph Hellwig } 11536f23ac8aSChristoph Hellwig EXPORT_SYMBOL(sbc_get_device_type); 115486d71829SAsias He 115562e46942SChristoph Hellwig static sense_reason_t 115662e46942SChristoph Hellwig sbc_execute_unmap(struct se_cmd *cmd) 115786d71829SAsias He { 115862e46942SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 115986d71829SAsias He struct se_device *dev = cmd->se_dev; 116086d71829SAsias He unsigned char *buf, *ptr = NULL; 116186d71829SAsias He sector_t lba; 116286d71829SAsias He int size; 116386d71829SAsias He u32 range; 116486d71829SAsias He sense_reason_t ret = 0; 116586d71829SAsias He int dl, bd_dl; 116686d71829SAsias He 116786d71829SAsias He /* We never set ANC_SUP */ 116886d71829SAsias He if (cmd->t_task_cdb[1]) 116986d71829SAsias He return TCM_INVALID_CDB_FIELD; 117086d71829SAsias He 117186d71829SAsias He if (cmd->data_length == 0) { 117286d71829SAsias He target_complete_cmd(cmd, SAM_STAT_GOOD); 117386d71829SAsias He return 0; 117486d71829SAsias He } 117586d71829SAsias He 117686d71829SAsias He if (cmd->data_length < 8) { 117786d71829SAsias He pr_warn("UNMAP parameter list length %u too small\n", 117886d71829SAsias He cmd->data_length); 117986d71829SAsias He return TCM_PARAMETER_LIST_LENGTH_ERROR; 118086d71829SAsias He } 118186d71829SAsias He 118286d71829SAsias He buf = transport_kmap_data_sg(cmd); 118386d71829SAsias He if (!buf) 118486d71829SAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 118586d71829SAsias He 118686d71829SAsias He dl = get_unaligned_be16(&buf[0]); 118786d71829SAsias He bd_dl = get_unaligned_be16(&buf[2]); 118886d71829SAsias He 118986d71829SAsias He size = cmd->data_length - 8; 119086d71829SAsias He if (bd_dl > size) 119186d71829SAsias He pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n", 119286d71829SAsias He cmd->data_length, bd_dl); 119386d71829SAsias He else 119486d71829SAsias He size = bd_dl; 119586d71829SAsias He 119686d71829SAsias He if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) { 119786d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 119886d71829SAsias He goto err; 119986d71829SAsias He } 120086d71829SAsias He 120186d71829SAsias He /* First UNMAP block descriptor starts at 8 byte offset */ 120286d71829SAsias He ptr = &buf[8]; 120386d71829SAsias He pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u" 120486d71829SAsias He " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); 120586d71829SAsias He 120686d71829SAsias He while (size >= 16) { 120786d71829SAsias He lba = get_unaligned_be64(&ptr[0]); 120886d71829SAsias He range = get_unaligned_be32(&ptr[8]); 120986d71829SAsias He pr_debug("UNMAP: Using lba: %llu and range: %u\n", 121086d71829SAsias He (unsigned long long)lba, range); 121186d71829SAsias He 121286d71829SAsias He if (range > dev->dev_attrib.max_unmap_lba_count) { 121386d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 121486d71829SAsias He goto err; 121586d71829SAsias He } 121686d71829SAsias He 121786d71829SAsias He if (lba + range > dev->transport->get_blocks(dev) + 1) { 121886d71829SAsias He ret = TCM_ADDRESS_OUT_OF_RANGE; 121986d71829SAsias He goto err; 122086d71829SAsias He } 122186d71829SAsias He 122262e46942SChristoph Hellwig ret = ops->execute_unmap(cmd, lba, range); 122386d71829SAsias He if (ret) 122486d71829SAsias He goto err; 122586d71829SAsias He 122686d71829SAsias He ptr += 16; 122786d71829SAsias He size -= 16; 122886d71829SAsias He } 122986d71829SAsias He 123086d71829SAsias He err: 123186d71829SAsias He transport_kunmap_data_sg(cmd); 123286d71829SAsias He if (!ret) 123386d71829SAsias He target_complete_cmd(cmd, GOOD); 123486d71829SAsias He return ret; 123586d71829SAsias He } 123641861fa8SNicholas Bellinger 123766a3d5bcSNicholas Bellinger void 123866a3d5bcSNicholas Bellinger sbc_dif_generate(struct se_cmd *cmd) 123966a3d5bcSNicholas Bellinger { 124066a3d5bcSNicholas Bellinger struct se_device *dev = cmd->se_dev; 1241fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 124218213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg, *psg; 124366a3d5bcSNicholas Bellinger sector_t sector = cmd->t_task_lba; 124466a3d5bcSNicholas Bellinger void *daddr, *paddr; 124566a3d5bcSNicholas Bellinger int i, j, offset = 0; 124618213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 124766a3d5bcSNicholas Bellinger 124818213afbSAkinobu Mita for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 124918213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 125066a3d5bcSNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 125166a3d5bcSNicholas Bellinger 125218213afbSAkinobu Mita for (j = 0; j < psg->length; 1253fe052a18SSagi Grimberg j += sizeof(*sdt)) { 125418213afbSAkinobu Mita __u16 crc; 125518213afbSAkinobu Mita unsigned int avail; 125666a3d5bcSNicholas Bellinger 125718213afbSAkinobu Mita if (offset >= dsg->length) { 125818213afbSAkinobu Mita offset -= dsg->length; 125918213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 126018213afbSAkinobu Mita dsg = sg_next(dsg); 126118213afbSAkinobu Mita if (!dsg) { 126218213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 126318213afbSAkinobu Mita return; 126418213afbSAkinobu Mita } 126518213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 126666a3d5bcSNicholas Bellinger } 126766a3d5bcSNicholas Bellinger 126818213afbSAkinobu Mita sdt = paddr + j; 126918213afbSAkinobu Mita avail = min(block_size, dsg->length - offset); 127018213afbSAkinobu Mita crc = crc_t10dif(daddr + offset, avail); 127118213afbSAkinobu Mita if (avail < block_size) { 127218213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 127318213afbSAkinobu Mita dsg = sg_next(dsg); 127418213afbSAkinobu Mita if (!dsg) { 127518213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 127618213afbSAkinobu Mita return; 127718213afbSAkinobu Mita } 127818213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 127918213afbSAkinobu Mita offset = block_size - avail; 128018213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, offset); 128118213afbSAkinobu Mita } else { 128218213afbSAkinobu Mita offset += block_size; 128318213afbSAkinobu Mita } 128418213afbSAkinobu Mita 128518213afbSAkinobu Mita sdt->guard_tag = cpu_to_be16(crc); 1286823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT) 128766a3d5bcSNicholas Bellinger sdt->ref_tag = cpu_to_be32(sector & 0xffffffff); 128866a3d5bcSNicholas Bellinger sdt->app_tag = 0; 128966a3d5bcSNicholas Bellinger 12906ae50408SNicholas Bellinger pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x" 129166a3d5bcSNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 12926ae50408SNicholas Bellinger (cmd->data_direction == DMA_TO_DEVICE) ? 12936ae50408SNicholas Bellinger "WRITE" : "READ", (unsigned long long)sector, 12946ae50408SNicholas Bellinger sdt->guard_tag, sdt->app_tag, 12956ae50408SNicholas Bellinger be32_to_cpu(sdt->ref_tag)); 129666a3d5bcSNicholas Bellinger 129766a3d5bcSNicholas Bellinger sector++; 129866a3d5bcSNicholas Bellinger } 129966a3d5bcSNicholas Bellinger 130018213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 130118213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 130266a3d5bcSNicholas Bellinger } 130366a3d5bcSNicholas Bellinger } 130466a3d5bcSNicholas Bellinger 130541861fa8SNicholas Bellinger static sense_reason_t 1306fe052a18SSagi Grimberg sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt, 130718213afbSAkinobu Mita __u16 crc, sector_t sector, unsigned int ei_lba) 130841861fa8SNicholas Bellinger { 130941861fa8SNicholas Bellinger __be16 csum; 131041861fa8SNicholas Bellinger 1311d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD)) 1312d7a463b0SNicholas Bellinger goto check_ref; 1313d7a463b0SNicholas Bellinger 131418213afbSAkinobu Mita csum = cpu_to_be16(crc); 131541861fa8SNicholas Bellinger 131641861fa8SNicholas Bellinger if (sdt->guard_tag != csum) { 131741861fa8SNicholas Bellinger pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x" 131841861fa8SNicholas Bellinger " csum 0x%04x\n", (unsigned long long)sector, 131941861fa8SNicholas Bellinger be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); 132041861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 132141861fa8SNicholas Bellinger } 132241861fa8SNicholas Bellinger 1323d7a463b0SNicholas Bellinger check_ref: 1324d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)) 1325d7a463b0SNicholas Bellinger return 0; 1326d7a463b0SNicholas Bellinger 1327823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT && 132841861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { 132941861fa8SNicholas Bellinger pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x" 133041861fa8SNicholas Bellinger " sector MSB: 0x%08x\n", (unsigned long long)sector, 133141861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff)); 133241861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 133341861fa8SNicholas Bellinger } 133441861fa8SNicholas Bellinger 1335823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE2_PROT && 133641861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != ei_lba) { 133741861fa8SNicholas Bellinger pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x" 133841861fa8SNicholas Bellinger " ei_lba: 0x%08x\n", (unsigned long long)sector, 133941861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), ei_lba); 134041861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 134141861fa8SNicholas Bellinger } 134241861fa8SNicholas Bellinger 134341861fa8SNicholas Bellinger return 0; 134441861fa8SNicholas Bellinger } 134541861fa8SNicholas Bellinger 1346f75b6faeSSagi Grimberg void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read, 134741861fa8SNicholas Bellinger struct scatterlist *sg, int sg_off) 134841861fa8SNicholas Bellinger { 134941861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 135041861fa8SNicholas Bellinger struct scatterlist *psg; 135141861fa8SNicholas Bellinger void *paddr, *addr; 135241861fa8SNicholas Bellinger unsigned int i, len, left; 135310762e80SNicholas Bellinger unsigned int offset = sg_off; 135441861fa8SNicholas Bellinger 135538b57f82SNicholas Bellinger if (!sg) 135638b57f82SNicholas Bellinger return; 135738b57f82SNicholas Bellinger 135841861fa8SNicholas Bellinger left = sectors * dev->prot_length; 135941861fa8SNicholas Bellinger 136041861fa8SNicholas Bellinger for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 136116c0ae02SSagi Grimberg unsigned int psg_len, copied = 0; 136241861fa8SNicholas Bellinger 136316c0ae02SSagi Grimberg paddr = kmap_atomic(sg_page(psg)) + psg->offset; 136416c0ae02SSagi Grimberg psg_len = min(left, psg->length); 136516c0ae02SSagi Grimberg while (psg_len) { 136616c0ae02SSagi Grimberg len = min(psg_len, sg->length - offset); 136716c0ae02SSagi Grimberg addr = kmap_atomic(sg_page(sg)) + sg->offset + offset; 136816c0ae02SSagi Grimberg 136916c0ae02SSagi Grimberg if (read) 137016c0ae02SSagi Grimberg memcpy(paddr + copied, addr, len); 137116c0ae02SSagi Grimberg else 137216c0ae02SSagi Grimberg memcpy(addr, paddr + copied, len); 137316c0ae02SSagi Grimberg 137416c0ae02SSagi Grimberg left -= len; 137516c0ae02SSagi Grimberg offset += len; 137616c0ae02SSagi Grimberg copied += len; 137716c0ae02SSagi Grimberg psg_len -= len; 137816c0ae02SSagi Grimberg 137957636388SAkinobu Mita kunmap_atomic(addr - sg->offset - offset); 138057636388SAkinobu Mita 1381d6a65fdcSSagi Grimberg if (offset >= sg->length) { 1382d6a65fdcSSagi Grimberg sg = sg_next(sg); 1383d6a65fdcSSagi Grimberg offset = 0; 1384d6a65fdcSSagi Grimberg } 138541861fa8SNicholas Bellinger } 138657636388SAkinobu Mita kunmap_atomic(paddr - psg->offset); 138716c0ae02SSagi Grimberg } 138841861fa8SNicholas Bellinger } 1389f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_copy_prot); 139041861fa8SNicholas Bellinger 139141861fa8SNicholas Bellinger sense_reason_t 1392f75b6faeSSagi Grimberg sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors, 1393414e4627SSagi Grimberg unsigned int ei_lba, struct scatterlist *psg, int psg_off) 139441861fa8SNicholas Bellinger { 139541861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 1396fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 139718213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg; 139841861fa8SNicholas Bellinger sector_t sector = start; 139941861fa8SNicholas Bellinger void *daddr, *paddr; 140018213afbSAkinobu Mita int i; 140141861fa8SNicholas Bellinger sense_reason_t rc; 140218213afbSAkinobu Mita int dsg_off = 0; 140318213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 140441861fa8SNicholas Bellinger 140518213afbSAkinobu Mita for (; psg && sector < start + sectors; psg = sg_next(psg)) { 140618213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 140741861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 140841861fa8SNicholas Bellinger 140918213afbSAkinobu Mita for (i = psg_off; i < psg->length && 141018213afbSAkinobu Mita sector < start + sectors; 1411fe052a18SSagi Grimberg i += sizeof(*sdt)) { 141218213afbSAkinobu Mita __u16 crc; 141318213afbSAkinobu Mita unsigned int avail; 141441861fa8SNicholas Bellinger 141518213afbSAkinobu Mita if (dsg_off >= dsg->length) { 141618213afbSAkinobu Mita dsg_off -= dsg->length; 141718213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 141818213afbSAkinobu Mita dsg = sg_next(dsg); 141918213afbSAkinobu Mita if (!dsg) { 1420414e4627SSagi Grimberg kunmap_atomic(paddr - psg->offset); 142141861fa8SNicholas Bellinger return 0; 142241861fa8SNicholas Bellinger } 142341861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 142441861fa8SNicholas Bellinger } 142541861fa8SNicholas Bellinger 142618213afbSAkinobu Mita sdt = paddr + i; 142741861fa8SNicholas Bellinger 142841861fa8SNicholas Bellinger pr_debug("DIF READ sector: %llu guard_tag: 0x%04x" 142941861fa8SNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 143041861fa8SNicholas Bellinger (unsigned long long)sector, sdt->guard_tag, 143141861fa8SNicholas Bellinger sdt->app_tag, be32_to_cpu(sdt->ref_tag)); 143241861fa8SNicholas Bellinger 143341861fa8SNicholas Bellinger if (sdt->app_tag == cpu_to_be16(0xffff)) { 143418213afbSAkinobu Mita dsg_off += block_size; 143518213afbSAkinobu Mita goto next; 143641861fa8SNicholas Bellinger } 143741861fa8SNicholas Bellinger 143818213afbSAkinobu Mita avail = min(block_size, dsg->length - dsg_off); 143918213afbSAkinobu Mita crc = crc_t10dif(daddr + dsg_off, avail); 144018213afbSAkinobu Mita if (avail < block_size) { 1441414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 144218213afbSAkinobu Mita dsg = sg_next(dsg); 144318213afbSAkinobu Mita if (!dsg) { 144418213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 144518213afbSAkinobu Mita return 0; 144618213afbSAkinobu Mita } 144718213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 144818213afbSAkinobu Mita dsg_off = block_size - avail; 144918213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, dsg_off); 145018213afbSAkinobu Mita } else { 145118213afbSAkinobu Mita dsg_off += block_size; 145218213afbSAkinobu Mita } 145318213afbSAkinobu Mita 145418213afbSAkinobu Mita rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba); 145541861fa8SNicholas Bellinger if (rc) { 145618213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 145718213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 145876736db3SSagi Grimberg cmd->bad_sector = sector; 145941861fa8SNicholas Bellinger return rc; 146041861fa8SNicholas Bellinger } 146118213afbSAkinobu Mita next: 146241861fa8SNicholas Bellinger sector++; 146341861fa8SNicholas Bellinger ei_lba++; 146441861fa8SNicholas Bellinger } 146541861fa8SNicholas Bellinger 146618213afbSAkinobu Mita psg_off = 0; 1467414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 146818213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 146941861fa8SNicholas Bellinger } 147041861fa8SNicholas Bellinger 147141861fa8SNicholas Bellinger return 0; 147241861fa8SNicholas Bellinger } 1473f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_verify); 1474