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 */ 1440fd97ccfSChristoph Hellwig if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) 1457f7caf6aSAndy Grover buf[14] |= 0x80; 1461fd032eeSChristoph Hellwig 147a50da144SPaolo Bonzini rbuf = transport_kmap_data_sg(cmd); 1488b4b0dcbSNicholas Bellinger if (rbuf) { 149a50da144SPaolo Bonzini memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 1501fd032eeSChristoph Hellwig transport_kunmap_data_sg(cmd); 1518b4b0dcbSNicholas Bellinger } 1521fd032eeSChristoph Hellwig 1532426bd45SRoland Dreier target_complete_cmd_with_length(cmd, GOOD, 32); 1541fd032eeSChristoph Hellwig return 0; 1551fd032eeSChristoph Hellwig } 1561fd032eeSChristoph Hellwig 15745182ed5SBrian Bunker static sense_reason_t 15845182ed5SBrian Bunker sbc_emulate_startstop(struct se_cmd *cmd) 15945182ed5SBrian Bunker { 16045182ed5SBrian Bunker unsigned char *cdb = cmd->t_task_cdb; 16145182ed5SBrian Bunker 16245182ed5SBrian Bunker /* 16345182ed5SBrian Bunker * See sbc3r36 section 5.25 16445182ed5SBrian Bunker * Immediate bit should be set since there is nothing to complete 16545182ed5SBrian Bunker * POWER CONDITION MODIFIER 0h 16645182ed5SBrian Bunker */ 16745182ed5SBrian Bunker if (!(cdb[1] & 1) || cdb[2] || cdb[3]) 16845182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 16945182ed5SBrian Bunker 17045182ed5SBrian Bunker /* 17145182ed5SBrian Bunker * See sbc3r36 section 5.25 17245182ed5SBrian Bunker * POWER CONDITION 0h START_VALID - process START and LOEJ 17345182ed5SBrian Bunker */ 17445182ed5SBrian Bunker if (cdb[4] >> 4 & 0xf) 17545182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 17645182ed5SBrian Bunker 17745182ed5SBrian Bunker /* 17845182ed5SBrian Bunker * See sbc3r36 section 5.25 17945182ed5SBrian Bunker * LOEJ 0h - nothing to load or unload 18045182ed5SBrian Bunker * START 1h - we are ready 18145182ed5SBrian Bunker */ 18245182ed5SBrian Bunker if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4)) 18345182ed5SBrian Bunker return TCM_INVALID_CDB_FIELD; 18445182ed5SBrian Bunker 18545182ed5SBrian Bunker target_complete_cmd(cmd, SAM_STAT_GOOD); 18645182ed5SBrian Bunker return 0; 18745182ed5SBrian Bunker } 18845182ed5SBrian Bunker 189972b29c8SRoland Dreier sector_t sbc_get_write_same_sectors(struct se_cmd *cmd) 1901fd032eeSChristoph Hellwig { 1911fd032eeSChristoph Hellwig u32 num_blocks; 1921fd032eeSChristoph Hellwig 1931fd032eeSChristoph Hellwig if (cmd->t_task_cdb[0] == WRITE_SAME) 1941fd032eeSChristoph Hellwig num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]); 1951fd032eeSChristoph Hellwig else if (cmd->t_task_cdb[0] == WRITE_SAME_16) 1961fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]); 1971fd032eeSChristoph Hellwig else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */ 1981fd032eeSChristoph Hellwig num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]); 1991fd032eeSChristoph Hellwig 2001fd032eeSChristoph Hellwig /* 2011fd032eeSChristoph Hellwig * Use the explicit range when non zero is supplied, otherwise calculate 2021fd032eeSChristoph Hellwig * the remaining range based on ->get_blocks() - starting LBA. 2031fd032eeSChristoph Hellwig */ 2046f974e8cSChristoph Hellwig if (num_blocks) 2056f974e8cSChristoph Hellwig return num_blocks; 2061fd032eeSChristoph Hellwig 2076f974e8cSChristoph Hellwig return cmd->se_dev->transport->get_blocks(cmd->se_dev) - 2086f974e8cSChristoph Hellwig cmd->t_task_lba + 1; 2091fd032eeSChristoph Hellwig } 210972b29c8SRoland Dreier EXPORT_SYMBOL(sbc_get_write_same_sectors); 2111fd032eeSChristoph Hellwig 212de103c93SChristoph Hellwig static sense_reason_t 213b753d643SChristoph Hellwig sbc_execute_write_same_unmap(struct se_cmd *cmd) 214b753d643SChristoph Hellwig { 215b753d643SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 216b753d643SChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 217b753d643SChristoph Hellwig sense_reason_t ret; 218b753d643SChristoph Hellwig 219b753d643SChristoph Hellwig if (nolb) { 220b753d643SChristoph Hellwig ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb); 221b753d643SChristoph Hellwig if (ret) 222b753d643SChristoph Hellwig return ret; 223b753d643SChristoph Hellwig } 224b753d643SChristoph Hellwig 225b753d643SChristoph Hellwig target_complete_cmd(cmd, GOOD); 226b753d643SChristoph Hellwig return 0; 227b753d643SChristoph Hellwig } 228b753d643SChristoph Hellwig 229b753d643SChristoph Hellwig static sense_reason_t 2301920ed61SNicholas Bellinger sbc_emulate_noop(struct se_cmd *cmd) 2311a1ff38cSBernhard Kohl { 2321a1ff38cSBernhard Kohl target_complete_cmd(cmd, GOOD); 2331a1ff38cSBernhard Kohl return 0; 2341a1ff38cSBernhard Kohl } 2351a1ff38cSBernhard Kohl 236d6e0175cSChristoph Hellwig static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors) 237d6e0175cSChristoph Hellwig { 2380fd97ccfSChristoph Hellwig return cmd->se_dev->dev_attrib.block_size * sectors; 239d6e0175cSChristoph Hellwig } 240d6e0175cSChristoph Hellwig 241d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_6(unsigned char *cdb) 242d6e0175cSChristoph Hellwig { 243d6e0175cSChristoph Hellwig /* 244d6e0175cSChristoph Hellwig * Use 8-bit sector value. SBC-3 says: 245d6e0175cSChristoph Hellwig * 246d6e0175cSChristoph Hellwig * A TRANSFER LENGTH field set to zero specifies that 256 247d6e0175cSChristoph Hellwig * logical blocks shall be written. Any other value 248d6e0175cSChristoph Hellwig * specifies the number of logical blocks that shall be 249d6e0175cSChristoph Hellwig * written. 250d6e0175cSChristoph Hellwig */ 251d6e0175cSChristoph Hellwig return cdb[4] ? : 256; 252d6e0175cSChristoph Hellwig } 253d6e0175cSChristoph Hellwig 254d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_10(unsigned char *cdb) 255d6e0175cSChristoph Hellwig { 256d6e0175cSChristoph Hellwig return (u32)(cdb[7] << 8) + cdb[8]; 257d6e0175cSChristoph Hellwig } 258d6e0175cSChristoph Hellwig 259d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_12(unsigned char *cdb) 260d6e0175cSChristoph Hellwig { 261d6e0175cSChristoph Hellwig return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; 262d6e0175cSChristoph Hellwig } 263d6e0175cSChristoph Hellwig 264d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_16(unsigned char *cdb) 265d6e0175cSChristoph Hellwig { 266d6e0175cSChristoph Hellwig return (u32)(cdb[10] << 24) + (cdb[11] << 16) + 267d6e0175cSChristoph Hellwig (cdb[12] << 8) + cdb[13]; 268d6e0175cSChristoph Hellwig } 269d6e0175cSChristoph Hellwig 270d6e0175cSChristoph Hellwig /* 271d6e0175cSChristoph Hellwig * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants 272d6e0175cSChristoph Hellwig */ 273d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_32(unsigned char *cdb) 274d6e0175cSChristoph Hellwig { 275d6e0175cSChristoph Hellwig return (u32)(cdb[28] << 24) + (cdb[29] << 16) + 276d6e0175cSChristoph Hellwig (cdb[30] << 8) + cdb[31]; 277d6e0175cSChristoph Hellwig 278d6e0175cSChristoph Hellwig } 279d6e0175cSChristoph Hellwig 280d6e0175cSChristoph Hellwig static inline u32 transport_lba_21(unsigned char *cdb) 281d6e0175cSChristoph Hellwig { 282d6e0175cSChristoph Hellwig return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; 283d6e0175cSChristoph Hellwig } 284d6e0175cSChristoph Hellwig 285d6e0175cSChristoph Hellwig static inline u32 transport_lba_32(unsigned char *cdb) 286d6e0175cSChristoph Hellwig { 287d6e0175cSChristoph Hellwig return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; 288d6e0175cSChristoph Hellwig } 289d6e0175cSChristoph Hellwig 290d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64(unsigned char *cdb) 291d6e0175cSChristoph Hellwig { 292d6e0175cSChristoph Hellwig unsigned int __v1, __v2; 293d6e0175cSChristoph Hellwig 294d6e0175cSChristoph Hellwig __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; 295d6e0175cSChristoph Hellwig __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; 296d6e0175cSChristoph Hellwig 297d6e0175cSChristoph Hellwig return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 298d6e0175cSChristoph Hellwig } 299d6e0175cSChristoph Hellwig 300d6e0175cSChristoph Hellwig /* 301d6e0175cSChristoph Hellwig * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs 302d6e0175cSChristoph Hellwig */ 303d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) 304d6e0175cSChristoph Hellwig { 305d6e0175cSChristoph Hellwig unsigned int __v1, __v2; 306d6e0175cSChristoph Hellwig 307d6e0175cSChristoph Hellwig __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; 308d6e0175cSChristoph Hellwig __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; 309d6e0175cSChristoph Hellwig 310d6e0175cSChristoph Hellwig return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; 311d6e0175cSChristoph Hellwig } 312d6e0175cSChristoph Hellwig 313cd063befSNicholas Bellinger static sense_reason_t 314cd063befSNicholas Bellinger sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops) 315d6e0175cSChristoph Hellwig { 3168e575c50SNicholas Bellinger struct se_device *dev = cmd->se_dev; 3178e575c50SNicholas Bellinger sector_t end_lba = dev->transport->get_blocks(dev) + 1; 318972b29c8SRoland Dreier unsigned int sectors = sbc_get_write_same_sectors(cmd); 319afd73f1bSNicholas Bellinger sense_reason_t ret; 320773cbaf7SNicholas Bellinger 321d6e0175cSChristoph Hellwig if ((flags[0] & 0x04) || (flags[0] & 0x02)) { 322d6e0175cSChristoph Hellwig pr_err("WRITE_SAME PBDATA and LBDATA" 323d6e0175cSChristoph Hellwig " bits not supported for Block Discard" 324d6e0175cSChristoph Hellwig " Emulation\n"); 325cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 326d6e0175cSChristoph Hellwig } 327773cbaf7SNicholas Bellinger if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) { 328773cbaf7SNicholas Bellinger pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n", 329773cbaf7SNicholas Bellinger sectors, cmd->se_dev->dev_attrib.max_write_same_len); 330773cbaf7SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 331773cbaf7SNicholas Bellinger } 3328e575c50SNicholas Bellinger /* 3338e575c50SNicholas Bellinger * Sanity check for LBA wrap and request past end of device. 3348e575c50SNicholas Bellinger */ 3358e575c50SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 3368e575c50SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 3378e575c50SNicholas Bellinger pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n", 3388e575c50SNicholas Bellinger (unsigned long long)end_lba, cmd->t_task_lba, sectors); 3398e575c50SNicholas Bellinger return TCM_ADDRESS_OUT_OF_RANGE; 3408e575c50SNicholas Bellinger } 3418e575c50SNicholas Bellinger 3425cb770bfSRoland Dreier /* We always have ANC_SUP == 0 so setting ANCHOR is always an error */ 3435cb770bfSRoland Dreier if (flags[0] & 0x10) { 3445cb770bfSRoland Dreier pr_warn("WRITE SAME with ANCHOR not supported\n"); 3455cb770bfSRoland Dreier return TCM_INVALID_CDB_FIELD; 3465cb770bfSRoland Dreier } 347d6e0175cSChristoph Hellwig /* 348cd063befSNicholas Bellinger * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting 349cd063befSNicholas Bellinger * translated into block discard requests within backend code. 350d6e0175cSChristoph Hellwig */ 351cd063befSNicholas Bellinger if (flags[0] & 0x08) { 352b753d643SChristoph Hellwig if (!ops->execute_unmap) 353cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 354d6e0175cSChristoph Hellwig 355d0a91295SNicholas Bellinger if (!dev->dev_attrib.emulate_tpws) { 356d0a91295SNicholas Bellinger pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device" 357d0a91295SNicholas Bellinger " has emulate_tpws disabled\n"); 358d0a91295SNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 359d0a91295SNicholas Bellinger } 360b753d643SChristoph Hellwig cmd->execute_cmd = sbc_execute_write_same_unmap; 361cd063befSNicholas Bellinger return 0; 362cd063befSNicholas Bellinger } 363cd063befSNicholas Bellinger if (!ops->execute_write_same) 364cd063befSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 365cd063befSNicholas Bellinger 366afd73f1bSNicholas Bellinger ret = sbc_check_prot(dev, cmd, &cmd->t_task_cdb[0], sectors, true); 367afd73f1bSNicholas Bellinger if (ret) 368afd73f1bSNicholas Bellinger return ret; 369afd73f1bSNicholas Bellinger 370cd063befSNicholas Bellinger cmd->execute_cmd = ops->execute_write_same; 371d6e0175cSChristoph Hellwig return 0; 372d6e0175cSChristoph Hellwig } 373d6e0175cSChristoph Hellwig 374057085e5SNicholas Bellinger static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success, 375057085e5SNicholas Bellinger int *post_ret) 376d6e0175cSChristoph Hellwig { 377d6e0175cSChristoph Hellwig unsigned char *buf, *addr; 378d6e0175cSChristoph Hellwig struct scatterlist *sg; 379d6e0175cSChristoph Hellwig unsigned int offset; 380a6b0133cSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 381a6b0133cSNicholas Bellinger int i, count; 382d6e0175cSChristoph Hellwig /* 383d6e0175cSChristoph Hellwig * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command 384d6e0175cSChristoph Hellwig * 385d6e0175cSChristoph Hellwig * 1) read the specified logical block(s); 386d6e0175cSChristoph Hellwig * 2) transfer logical blocks from the data-out buffer; 387d6e0175cSChristoph Hellwig * 3) XOR the logical blocks transferred from the data-out buffer with 388d6e0175cSChristoph Hellwig * the logical blocks read, storing the resulting XOR data in a buffer; 389d6e0175cSChristoph Hellwig * 4) if the DISABLE WRITE bit is set to zero, then write the logical 390d6e0175cSChristoph Hellwig * blocks transferred from the data-out buffer; and 391d6e0175cSChristoph Hellwig * 5) transfer the resulting XOR data to the data-in buffer. 392d6e0175cSChristoph Hellwig */ 393d6e0175cSChristoph Hellwig buf = kmalloc(cmd->data_length, GFP_KERNEL); 394d6e0175cSChristoph Hellwig if (!buf) { 395d6e0175cSChristoph Hellwig pr_err("Unable to allocate xor_callback buf\n"); 396a6b0133cSNicholas Bellinger return TCM_OUT_OF_RESOURCES; 397d6e0175cSChristoph Hellwig } 398d6e0175cSChristoph Hellwig /* 399d6e0175cSChristoph Hellwig * Copy the scatterlist WRITE buffer located at cmd->t_data_sg 400d6e0175cSChristoph Hellwig * into the locally allocated *buf 401d6e0175cSChristoph Hellwig */ 402d6e0175cSChristoph Hellwig sg_copy_to_buffer(cmd->t_data_sg, 403d6e0175cSChristoph Hellwig cmd->t_data_nents, 404d6e0175cSChristoph Hellwig buf, 405d6e0175cSChristoph Hellwig cmd->data_length); 406d6e0175cSChristoph Hellwig 407d6e0175cSChristoph Hellwig /* 408d6e0175cSChristoph Hellwig * Now perform the XOR against the BIDI read memory located at 409d6e0175cSChristoph Hellwig * cmd->t_mem_bidi_list 410d6e0175cSChristoph Hellwig */ 411d6e0175cSChristoph Hellwig 412d6e0175cSChristoph Hellwig offset = 0; 413d6e0175cSChristoph Hellwig for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { 414d6e0175cSChristoph Hellwig addr = kmap_atomic(sg_page(sg)); 415a6b0133cSNicholas Bellinger if (!addr) { 416a6b0133cSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 417d6e0175cSChristoph Hellwig goto out; 418a6b0133cSNicholas Bellinger } 419d6e0175cSChristoph Hellwig 420d6e0175cSChristoph Hellwig for (i = 0; i < sg->length; i++) 421d6e0175cSChristoph Hellwig *(addr + sg->offset + i) ^= *(buf + offset + i); 422d6e0175cSChristoph Hellwig 423d6e0175cSChristoph Hellwig offset += sg->length; 424d6e0175cSChristoph Hellwig kunmap_atomic(addr); 425d6e0175cSChristoph Hellwig } 426d6e0175cSChristoph Hellwig 427d6e0175cSChristoph Hellwig out: 428d6e0175cSChristoph Hellwig kfree(buf); 429a6b0133cSNicholas Bellinger return ret; 430d6e0175cSChristoph Hellwig } 431d6e0175cSChristoph Hellwig 432a82a9538SNicholas Bellinger static sense_reason_t 433a82a9538SNicholas Bellinger sbc_execute_rw(struct se_cmd *cmd) 434a82a9538SNicholas Bellinger { 4357a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 4367a971b1bSChristoph Hellwig 4377a971b1bSChristoph Hellwig return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents, 438a82a9538SNicholas Bellinger cmd->data_direction); 439a82a9538SNicholas Bellinger } 440a82a9538SNicholas Bellinger 441057085e5SNicholas Bellinger static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success, 442057085e5SNicholas Bellinger int *post_ret) 44368ff9b9bSNicholas Bellinger { 44468ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 44568ff9b9bSNicholas Bellinger 446d8855c15SNicholas Bellinger /* 447d8855c15SNicholas Bellinger * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through 448d8855c15SNicholas Bellinger * within target_complete_ok_work() if the command was successfully 449d8855c15SNicholas Bellinger * sent to the backend driver. 450d8855c15SNicholas Bellinger */ 451d8855c15SNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 452057085e5SNicholas Bellinger if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) { 45368ff9b9bSNicholas Bellinger cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST; 454057085e5SNicholas Bellinger *post_ret = 1; 455057085e5SNicholas Bellinger } 456d8855c15SNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 457d8855c15SNicholas Bellinger 45868ff9b9bSNicholas Bellinger /* 45968ff9b9bSNicholas Bellinger * Unlock ->caw_sem originally obtained during sbc_compare_and_write() 46068ff9b9bSNicholas Bellinger * before the original READ I/O submission. 46168ff9b9bSNicholas Bellinger */ 46268ff9b9bSNicholas Bellinger up(&dev->caw_sem); 46368ff9b9bSNicholas Bellinger 46468ff9b9bSNicholas Bellinger return TCM_NO_SENSE; 46568ff9b9bSNicholas Bellinger } 46668ff9b9bSNicholas Bellinger 467057085e5SNicholas Bellinger static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success, 468057085e5SNicholas Bellinger int *post_ret) 46968ff9b9bSNicholas Bellinger { 47068ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 47168ff9b9bSNicholas Bellinger struct scatterlist *write_sg = NULL, *sg; 472db60df88SNicholas Bellinger unsigned char *buf = NULL, *addr; 47368ff9b9bSNicholas Bellinger struct sg_mapping_iter m; 47468ff9b9bSNicholas Bellinger unsigned int offset = 0, len; 47568ff9b9bSNicholas Bellinger unsigned int nlbas = cmd->t_task_nolb; 47668ff9b9bSNicholas Bellinger unsigned int block_size = dev->dev_attrib.block_size; 47768ff9b9bSNicholas Bellinger unsigned int compare_len = (nlbas * block_size); 47868ff9b9bSNicholas Bellinger sense_reason_t ret = TCM_NO_SENSE; 47968ff9b9bSNicholas Bellinger int rc, i; 48068ff9b9bSNicholas Bellinger 481cf6d1f09SNicholas Bellinger /* 482cf6d1f09SNicholas Bellinger * Handle early failure in transport_generic_request_failure(), 483c8e63985SNicholas Bellinger * which will not have taken ->caw_sem yet.. 484cf6d1f09SNicholas Bellinger */ 485c8e63985SNicholas Bellinger if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg)) 486cf6d1f09SNicholas Bellinger return TCM_NO_SENSE; 487db60df88SNicholas Bellinger /* 488c8e63985SNicholas Bellinger * Handle special case for zero-length COMPARE_AND_WRITE 489c8e63985SNicholas Bellinger */ 490c8e63985SNicholas Bellinger if (!cmd->data_length) 491c8e63985SNicholas Bellinger goto out; 492c8e63985SNicholas Bellinger /* 493db60df88SNicholas Bellinger * Immediately exit + release dev->caw_sem if command has already 494db60df88SNicholas Bellinger * been failed with a non-zero SCSI status. 495db60df88SNicholas Bellinger */ 496db60df88SNicholas Bellinger if (cmd->scsi_status) { 497db60df88SNicholas Bellinger pr_err("compare_and_write_callback: non zero scsi_status:" 498db60df88SNicholas Bellinger " 0x%02x\n", cmd->scsi_status); 499db60df88SNicholas Bellinger goto out; 500db60df88SNicholas Bellinger } 501cf6d1f09SNicholas Bellinger 50268ff9b9bSNicholas Bellinger buf = kzalloc(cmd->data_length, GFP_KERNEL); 50368ff9b9bSNicholas Bellinger if (!buf) { 50468ff9b9bSNicholas Bellinger pr_err("Unable to allocate compare_and_write buf\n"); 505a2890087SNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 506a2890087SNicholas Bellinger goto out; 50768ff9b9bSNicholas Bellinger } 50868ff9b9bSNicholas Bellinger 509a1e1774cSMartin Svec write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents, 51068ff9b9bSNicholas Bellinger GFP_KERNEL); 51168ff9b9bSNicholas Bellinger if (!write_sg) { 51268ff9b9bSNicholas Bellinger pr_err("Unable to allocate compare_and_write sg\n"); 51368ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 51468ff9b9bSNicholas Bellinger goto out; 51568ff9b9bSNicholas Bellinger } 516a1e1774cSMartin Svec sg_init_table(write_sg, cmd->t_data_nents); 51768ff9b9bSNicholas Bellinger /* 51868ff9b9bSNicholas Bellinger * Setup verify and write data payloads from total NumberLBAs. 51968ff9b9bSNicholas Bellinger */ 52068ff9b9bSNicholas Bellinger rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf, 52168ff9b9bSNicholas Bellinger cmd->data_length); 52268ff9b9bSNicholas Bellinger if (!rc) { 52368ff9b9bSNicholas Bellinger pr_err("sg_copy_to_buffer() failed for compare_and_write\n"); 52468ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 52568ff9b9bSNicholas Bellinger goto out; 52668ff9b9bSNicholas Bellinger } 52768ff9b9bSNicholas Bellinger /* 52868ff9b9bSNicholas Bellinger * Compare against SCSI READ payload against verify payload 52968ff9b9bSNicholas Bellinger */ 53068ff9b9bSNicholas Bellinger for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) { 53168ff9b9bSNicholas Bellinger addr = (unsigned char *)kmap_atomic(sg_page(sg)); 53268ff9b9bSNicholas Bellinger if (!addr) { 53368ff9b9bSNicholas Bellinger ret = TCM_OUT_OF_RESOURCES; 53468ff9b9bSNicholas Bellinger goto out; 53568ff9b9bSNicholas Bellinger } 53668ff9b9bSNicholas Bellinger 53768ff9b9bSNicholas Bellinger len = min(sg->length, compare_len); 53868ff9b9bSNicholas Bellinger 53968ff9b9bSNicholas Bellinger if (memcmp(addr, buf + offset, len)) { 54068ff9b9bSNicholas Bellinger pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n", 54168ff9b9bSNicholas Bellinger addr, buf + offset); 54268ff9b9bSNicholas Bellinger kunmap_atomic(addr); 54368ff9b9bSNicholas Bellinger goto miscompare; 54468ff9b9bSNicholas Bellinger } 54568ff9b9bSNicholas Bellinger kunmap_atomic(addr); 54668ff9b9bSNicholas Bellinger 54768ff9b9bSNicholas Bellinger offset += len; 54868ff9b9bSNicholas Bellinger compare_len -= len; 54968ff9b9bSNicholas Bellinger if (!compare_len) 55068ff9b9bSNicholas Bellinger break; 55168ff9b9bSNicholas Bellinger } 55268ff9b9bSNicholas Bellinger 55368ff9b9bSNicholas Bellinger i = 0; 55468ff9b9bSNicholas Bellinger len = cmd->t_task_nolb * block_size; 55568ff9b9bSNicholas Bellinger sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG); 55668ff9b9bSNicholas Bellinger /* 55768ff9b9bSNicholas Bellinger * Currently assumes NoLB=1 and SGLs are PAGE_SIZE.. 55868ff9b9bSNicholas Bellinger */ 55968ff9b9bSNicholas Bellinger while (len) { 56068ff9b9bSNicholas Bellinger sg_miter_next(&m); 56168ff9b9bSNicholas Bellinger 56268ff9b9bSNicholas Bellinger if (block_size < PAGE_SIZE) { 56368ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 564d94e5a61SJan Engelhardt m.piter.sg->offset + block_size); 56568ff9b9bSNicholas Bellinger } else { 56668ff9b9bSNicholas Bellinger sg_miter_next(&m); 56768ff9b9bSNicholas Bellinger sg_set_page(&write_sg[i], m.page, block_size, 568d94e5a61SJan Engelhardt m.piter.sg->offset); 56968ff9b9bSNicholas Bellinger } 57068ff9b9bSNicholas Bellinger len -= block_size; 57168ff9b9bSNicholas Bellinger i++; 57268ff9b9bSNicholas Bellinger } 57368ff9b9bSNicholas Bellinger sg_miter_stop(&m); 57468ff9b9bSNicholas Bellinger /* 57568ff9b9bSNicholas Bellinger * Save the original SGL + nents values before updating to new 57668ff9b9bSNicholas Bellinger * assignments, to be released in transport_free_pages() -> 57768ff9b9bSNicholas Bellinger * transport_reset_sgl_orig() 57868ff9b9bSNicholas Bellinger */ 57968ff9b9bSNicholas Bellinger cmd->t_data_sg_orig = cmd->t_data_sg; 58068ff9b9bSNicholas Bellinger cmd->t_data_sg = write_sg; 58168ff9b9bSNicholas Bellinger cmd->t_data_nents_orig = cmd->t_data_nents; 58268ff9b9bSNicholas Bellinger cmd->t_data_nents = 1; 58368ff9b9bSNicholas Bellinger 58468d81f40SChristoph Hellwig cmd->sam_task_attr = TCM_HEAD_TAG; 58568ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_post; 58668ff9b9bSNicholas Bellinger /* 58768ff9b9bSNicholas Bellinger * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler 58868ff9b9bSNicholas Bellinger * for submitting the adjusted SGL to write instance user-data. 58968ff9b9bSNicholas Bellinger */ 59068ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 59168ff9b9bSNicholas Bellinger 59268ff9b9bSNicholas Bellinger spin_lock_irq(&cmd->t_state_lock); 59368ff9b9bSNicholas Bellinger cmd->t_state = TRANSPORT_PROCESSING; 59468ff9b9bSNicholas Bellinger cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT; 59568ff9b9bSNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock); 59668ff9b9bSNicholas Bellinger 59768ff9b9bSNicholas Bellinger __target_execute_cmd(cmd); 59868ff9b9bSNicholas Bellinger 59968ff9b9bSNicholas Bellinger kfree(buf); 60068ff9b9bSNicholas Bellinger return ret; 60168ff9b9bSNicholas Bellinger 60268ff9b9bSNicholas Bellinger miscompare: 60368ff9b9bSNicholas Bellinger pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n", 60468ff9b9bSNicholas Bellinger dev->transport->name); 60568ff9b9bSNicholas Bellinger ret = TCM_MISCOMPARE_VERIFY; 60668ff9b9bSNicholas Bellinger out: 60768ff9b9bSNicholas Bellinger /* 60868ff9b9bSNicholas Bellinger * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in 60968ff9b9bSNicholas Bellinger * sbc_compare_and_write() before the original READ I/O submission. 61068ff9b9bSNicholas Bellinger */ 61168ff9b9bSNicholas Bellinger up(&dev->caw_sem); 61268ff9b9bSNicholas Bellinger kfree(write_sg); 61368ff9b9bSNicholas Bellinger kfree(buf); 61468ff9b9bSNicholas Bellinger return ret; 61568ff9b9bSNicholas Bellinger } 61668ff9b9bSNicholas Bellinger 61768ff9b9bSNicholas Bellinger static sense_reason_t 61868ff9b9bSNicholas Bellinger sbc_compare_and_write(struct se_cmd *cmd) 61968ff9b9bSNicholas Bellinger { 6207a971b1bSChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 62168ff9b9bSNicholas Bellinger struct se_device *dev = cmd->se_dev; 62268ff9b9bSNicholas Bellinger sense_reason_t ret; 62368ff9b9bSNicholas Bellinger int rc; 62468ff9b9bSNicholas Bellinger /* 62568ff9b9bSNicholas Bellinger * Submit the READ first for COMPARE_AND_WRITE to perform the 62668ff9b9bSNicholas Bellinger * comparision using SGLs at cmd->t_bidi_data_sg.. 62768ff9b9bSNicholas Bellinger */ 62868ff9b9bSNicholas Bellinger rc = down_interruptible(&dev->caw_sem); 629ee7619f2SNicholas Bellinger if (rc != 0) { 63068ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 63168ff9b9bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 63268ff9b9bSNicholas Bellinger } 633b7191253SNicholas Bellinger /* 634b7191253SNicholas Bellinger * Reset cmd->data_length to individual block_size in order to not 635b7191253SNicholas Bellinger * confuse backend drivers that depend on this value matching the 636b7191253SNicholas Bellinger * size of the I/O being submitted. 637b7191253SNicholas Bellinger */ 638b7191253SNicholas Bellinger cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size; 63968ff9b9bSNicholas Bellinger 6407a971b1bSChristoph Hellwig ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents, 64168ff9b9bSNicholas Bellinger DMA_FROM_DEVICE); 64268ff9b9bSNicholas Bellinger if (ret) { 64368ff9b9bSNicholas Bellinger cmd->transport_complete_callback = NULL; 64468ff9b9bSNicholas Bellinger up(&dev->caw_sem); 64568ff9b9bSNicholas Bellinger return ret; 64668ff9b9bSNicholas Bellinger } 64768ff9b9bSNicholas Bellinger /* 64868ff9b9bSNicholas Bellinger * Unlock of dev->caw_sem to occur in compare_and_write_callback() 64968ff9b9bSNicholas Bellinger * upon MISCOMPARE, or in compare_and_write_done() upon completion 65068ff9b9bSNicholas Bellinger * of WRITE instance user-data. 65168ff9b9bSNicholas Bellinger */ 65268ff9b9bSNicholas Bellinger return TCM_NO_SENSE; 65368ff9b9bSNicholas Bellinger } 65468ff9b9bSNicholas Bellinger 65519f9361aSSagi Grimberg static int 65638b57f82SNicholas Bellinger sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type, 65719f9361aSSagi Grimberg bool is_write, struct se_cmd *cmd) 65819f9361aSSagi Grimberg { 65919f9361aSSagi Grimberg if (is_write) { 66038b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP : 66138b57f82SNicholas Bellinger protect ? TARGET_PROT_DOUT_PASS : 66219f9361aSSagi Grimberg TARGET_PROT_DOUT_INSERT; 66319f9361aSSagi Grimberg switch (protect) { 66419f9361aSSagi Grimberg case 0x0: 66519f9361aSSagi Grimberg case 0x3: 66619f9361aSSagi Grimberg cmd->prot_checks = 0; 66719f9361aSSagi Grimberg break; 66819f9361aSSagi Grimberg case 0x1: 66919f9361aSSagi Grimberg case 0x5: 67019f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 67119f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 67219f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 67319f9361aSSagi Grimberg break; 67419f9361aSSagi Grimberg case 0x2: 67519f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 67619f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 67719f9361aSSagi Grimberg break; 67819f9361aSSagi Grimberg case 0x4: 67919f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 68019f9361aSSagi Grimberg break; 68119f9361aSSagi Grimberg default: 68219f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 68319f9361aSSagi Grimberg return -EINVAL; 68419f9361aSSagi Grimberg } 68519f9361aSSagi Grimberg } else { 68638b57f82SNicholas Bellinger cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT : 68738b57f82SNicholas Bellinger protect ? TARGET_PROT_DIN_PASS : 68819f9361aSSagi Grimberg TARGET_PROT_DIN_STRIP; 68919f9361aSSagi Grimberg switch (protect) { 69019f9361aSSagi Grimberg case 0x0: 69119f9361aSSagi Grimberg case 0x1: 69219f9361aSSagi Grimberg case 0x5: 69319f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 69419f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 69519f9361aSSagi Grimberg cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG; 69619f9361aSSagi Grimberg break; 69719f9361aSSagi Grimberg case 0x2: 69819f9361aSSagi Grimberg if (prot_type == TARGET_DIF_TYPE1_PROT) 69919f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_REFTAG; 70019f9361aSSagi Grimberg break; 70119f9361aSSagi Grimberg case 0x3: 70219f9361aSSagi Grimberg cmd->prot_checks = 0; 70319f9361aSSagi Grimberg break; 70419f9361aSSagi Grimberg case 0x4: 70519f9361aSSagi Grimberg cmd->prot_checks = TARGET_DIF_CHECK_GUARD; 70619f9361aSSagi Grimberg break; 70719f9361aSSagi Grimberg default: 70819f9361aSSagi Grimberg pr_err("Unsupported protect field %d\n", protect); 70919f9361aSSagi Grimberg return -EINVAL; 71019f9361aSSagi Grimberg } 71119f9361aSSagi Grimberg } 71219f9361aSSagi Grimberg 71319f9361aSSagi Grimberg return 0; 71419f9361aSSagi Grimberg } 71519f9361aSSagi Grimberg 716f7b7c06fSNicholas Bellinger static sense_reason_t 717499bf77bSNicholas Bellinger sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb, 71819f9361aSSagi Grimberg u32 sectors, bool is_write) 719499bf77bSNicholas Bellinger { 72019f9361aSSagi Grimberg u8 protect = cdb[1] >> 5; 72138b57f82SNicholas Bellinger int sp_ops = cmd->se_sess->sup_prot_ops; 72238b57f82SNicholas Bellinger int pi_prot_type = dev->dev_attrib.pi_prot_type; 72338b57f82SNicholas Bellinger bool fabric_prot = false; 72419f9361aSSagi Grimberg 725f7b7c06fSNicholas Bellinger if (!cmd->t_prot_sg || !cmd->t_prot_nents) { 72638b57f82SNicholas Bellinger if (unlikely(protect && 72738b57f82SNicholas Bellinger !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) { 72838b57f82SNicholas Bellinger pr_err("CDB contains protect bit, but device + fabric does" 72938b57f82SNicholas Bellinger " not advertise PROTECT=1 feature bit\n"); 730f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 731f7b7c06fSNicholas Bellinger } 732f7b7c06fSNicholas Bellinger if (cmd->prot_pto) 733f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 734f7b7c06fSNicholas Bellinger } 735499bf77bSNicholas Bellinger 736499bf77bSNicholas Bellinger switch (dev->dev_attrib.pi_prot_type) { 737499bf77bSNicholas Bellinger case TARGET_DIF_TYPE3_PROT: 738499bf77bSNicholas Bellinger cmd->reftag_seed = 0xffffffff; 739499bf77bSNicholas Bellinger break; 740499bf77bSNicholas Bellinger case TARGET_DIF_TYPE2_PROT: 74119f9361aSSagi Grimberg if (protect) 742f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 743499bf77bSNicholas Bellinger 744499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 745499bf77bSNicholas Bellinger break; 746499bf77bSNicholas Bellinger case TARGET_DIF_TYPE1_PROT: 747499bf77bSNicholas Bellinger cmd->reftag_seed = cmd->t_task_lba; 748499bf77bSNicholas Bellinger break; 749499bf77bSNicholas Bellinger case TARGET_DIF_TYPE0_PROT: 75038b57f82SNicholas Bellinger /* 75138b57f82SNicholas Bellinger * See if the fabric supports T10-PI, and the session has been 75238b57f82SNicholas Bellinger * configured to allow export PROTECT=1 feature bit with backend 75338b57f82SNicholas Bellinger * devices that don't support T10-PI. 75438b57f82SNicholas Bellinger */ 75538b57f82SNicholas Bellinger fabric_prot = is_write ? 75638b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) : 75738b57f82SNicholas Bellinger !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT)); 75838b57f82SNicholas Bellinger 75938b57f82SNicholas Bellinger if (fabric_prot && cmd->se_sess->sess_prot_type) { 76038b57f82SNicholas Bellinger pi_prot_type = cmd->se_sess->sess_prot_type; 76138b57f82SNicholas Bellinger break; 76238b57f82SNicholas Bellinger } 763cceca4a6SNicholas Bellinger if (!protect) 764f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 76538b57f82SNicholas Bellinger /* Fallthrough */ 766499bf77bSNicholas Bellinger default: 767cceca4a6SNicholas Bellinger pr_err("Unable to determine pi_prot_type for CDB: 0x%02x " 768cceca4a6SNicholas Bellinger "PROTECT: 0x%02x\n", cdb[0], protect); 769cceca4a6SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 770499bf77bSNicholas Bellinger } 771499bf77bSNicholas Bellinger 77238b57f82SNicholas Bellinger if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd)) 773f7b7c06fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 77419f9361aSSagi Grimberg 77538b57f82SNicholas Bellinger cmd->prot_type = pi_prot_type; 776499bf77bSNicholas Bellinger cmd->prot_length = dev->prot_length * sectors; 777e2a4f55cSSagi Grimberg 778e2a4f55cSSagi Grimberg /** 779e2a4f55cSSagi Grimberg * In case protection information exists over the wire 780e2a4f55cSSagi Grimberg * we modify command data length to describe pure data. 781e2a4f55cSSagi Grimberg * The actual transfer length is data length + protection 782e2a4f55cSSagi Grimberg * length 783e2a4f55cSSagi Grimberg **/ 784e2a4f55cSSagi Grimberg if (protect) 785e2a4f55cSSagi Grimberg cmd->data_length = sectors * dev->dev_attrib.block_size; 786e2a4f55cSSagi Grimberg 787e2a4f55cSSagi Grimberg pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d " 788e2a4f55cSSagi Grimberg "prot_op=%d prot_checks=%d\n", 789e2a4f55cSSagi Grimberg __func__, cmd->prot_type, cmd->data_length, cmd->prot_length, 79003abad9eSSagi Grimberg cmd->prot_op, cmd->prot_checks); 791499bf77bSNicholas Bellinger 792f7b7c06fSNicholas Bellinger return TCM_NO_SENSE; 793499bf77bSNicholas Bellinger } 794499bf77bSNicholas Bellinger 795fde9f50fSNicholas Bellinger static int 796fde9f50fSNicholas Bellinger sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb) 797fde9f50fSNicholas Bellinger { 798fde9f50fSNicholas Bellinger if (cdb[1] & 0x10) { 799814e5b45SChristoph Hellwig /* see explanation in spc_emulate_modesense */ 800814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 801fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with DPO bit set, but device" 802fde9f50fSNicholas Bellinger " does not advertise support for DPO\n", cdb[0]); 803fde9f50fSNicholas Bellinger return -EINVAL; 804fde9f50fSNicholas Bellinger } 805fde9f50fSNicholas Bellinger } 806fde9f50fSNicholas Bellinger if (cdb[1] & 0x8) { 807814e5b45SChristoph Hellwig if (!target_check_fua(dev)) { 808fde9f50fSNicholas Bellinger pr_err("Got CDB: 0x%02x with FUA bit set, but device" 809fde9f50fSNicholas Bellinger " does not advertise support for FUA write\n", 810fde9f50fSNicholas Bellinger cdb[0]); 811fde9f50fSNicholas Bellinger return -EINVAL; 812fde9f50fSNicholas Bellinger } 813fde9f50fSNicholas Bellinger cmd->se_cmd_flags |= SCF_FUA; 814fde9f50fSNicholas Bellinger } 815fde9f50fSNicholas Bellinger return 0; 816d6e0175cSChristoph Hellwig } 817d6e0175cSChristoph Hellwig 818de103c93SChristoph Hellwig sense_reason_t 819de103c93SChristoph Hellwig sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) 820d6e0175cSChristoph Hellwig { 821d6e0175cSChristoph Hellwig struct se_device *dev = cmd->se_dev; 822d6e0175cSChristoph Hellwig unsigned char *cdb = cmd->t_task_cdb; 8231fd032eeSChristoph Hellwig unsigned int size; 824d6e0175cSChristoph Hellwig u32 sectors = 0; 825de103c93SChristoph Hellwig sense_reason_t ret; 826d6e0175cSChristoph Hellwig 8277a971b1bSChristoph Hellwig cmd->protocol_data = ops; 8287a971b1bSChristoph Hellwig 829d6e0175cSChristoph Hellwig switch (cdb[0]) { 830d6e0175cSChristoph Hellwig case READ_6: 831d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 832d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 833d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 834a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 835d6e0175cSChristoph Hellwig break; 836d6e0175cSChristoph Hellwig case READ_10: 837d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 838d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 839499bf77bSNicholas Bellinger 840fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 841fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 842fde9f50fSNicholas Bellinger 843f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 844f7b7c06fSNicholas Bellinger if (ret) 845f7b7c06fSNicholas Bellinger return ret; 846499bf77bSNicholas Bellinger 847d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 848a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 849d6e0175cSChristoph Hellwig break; 850d6e0175cSChristoph Hellwig case READ_12: 851d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 852d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 853499bf77bSNicholas Bellinger 854fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 855fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 856fde9f50fSNicholas Bellinger 857f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 858f7b7c06fSNicholas Bellinger if (ret) 859f7b7c06fSNicholas Bellinger return ret; 860499bf77bSNicholas Bellinger 861d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 862a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 863d6e0175cSChristoph Hellwig break; 864d6e0175cSChristoph Hellwig case READ_16: 865d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 866d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 867499bf77bSNicholas Bellinger 868fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 869fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 870fde9f50fSNicholas Bellinger 871f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, false); 872f7b7c06fSNicholas Bellinger if (ret) 873f7b7c06fSNicholas Bellinger return ret; 874499bf77bSNicholas Bellinger 875d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 876a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 877d6e0175cSChristoph Hellwig break; 878d6e0175cSChristoph Hellwig case WRITE_6: 879d6e0175cSChristoph Hellwig sectors = transport_get_sectors_6(cdb); 880d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_21(cdb); 881d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 882a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 883d6e0175cSChristoph Hellwig break; 884d6e0175cSChristoph Hellwig case WRITE_10: 885d6e0175cSChristoph Hellwig case WRITE_VERIFY: 886d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 887d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 888499bf77bSNicholas Bellinger 889fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 890fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 891499bf77bSNicholas Bellinger 892f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 893f7b7c06fSNicholas Bellinger if (ret) 894f7b7c06fSNicholas Bellinger return ret; 895d6e0175cSChristoph Hellwig 896d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 897a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 898d6e0175cSChristoph Hellwig break; 899d6e0175cSChristoph Hellwig case WRITE_12: 900d6e0175cSChristoph Hellwig sectors = transport_get_sectors_12(cdb); 901d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 902499bf77bSNicholas Bellinger 903fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 904fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 905499bf77bSNicholas Bellinger 906f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 907f7b7c06fSNicholas Bellinger if (ret) 908f7b7c06fSNicholas Bellinger return ret; 909d6e0175cSChristoph Hellwig 910d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 911a82a9538SNicholas Bellinger cmd->execute_cmd = sbc_execute_rw; 912d6e0175cSChristoph Hellwig break; 913d6e0175cSChristoph Hellwig case WRITE_16: 914d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 915d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 916499bf77bSNicholas Bellinger 917fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 918fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 919499bf77bSNicholas Bellinger 920f7b7c06fSNicholas Bellinger ret = sbc_check_prot(dev, cmd, cdb, sectors, true); 921f7b7c06fSNicholas Bellinger if (ret) 922f7b7c06fSNicholas Bellinger return ret; 923cd063befSNicholas Bellinger 924d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 925d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 926d6e0175cSChristoph Hellwig break; 927d6e0175cSChristoph Hellwig case XDWRITEREAD_10: 928d6e0175cSChristoph Hellwig if (cmd->data_direction != DMA_TO_DEVICE || 929d6e0175cSChristoph Hellwig !(cmd->se_cmd_flags & SCF_BIDI)) 930d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 931d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 932d6e0175cSChristoph Hellwig 933fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 934fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 935fde9f50fSNicholas Bellinger 936d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 937d6e0175cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 938d6e0175cSChristoph Hellwig 939d6e0175cSChristoph Hellwig /* 940d6e0175cSChristoph Hellwig * Setup BIDI XOR callback to be run after I/O completion. 941d6e0175cSChristoph Hellwig */ 942d6e0175cSChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 943d6e0175cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 9440c2ad7d1SChristoph Hellwig break; 945d6e0175cSChristoph Hellwig case VARIABLE_LENGTH_CMD: 946d6e0175cSChristoph Hellwig { 947d6e0175cSChristoph Hellwig u16 service_action = get_unaligned_be16(&cdb[8]); 948d6e0175cSChristoph Hellwig switch (service_action) { 949d6e0175cSChristoph Hellwig case XDWRITEREAD_32: 9506f974e8cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 951de103c93SChristoph Hellwig 952fde9f50fSNicholas Bellinger if (sbc_check_dpofua(dev, cmd, cdb)) 953fde9f50fSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 9546f974e8cSChristoph Hellwig /* 955d6e0175cSChristoph Hellwig * Use WRITE_32 and READ_32 opcodes for the emulated 956d6e0175cSChristoph Hellwig * XDWRITE_READ_32 logic. 957d6e0175cSChristoph Hellwig */ 958d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64_ext(cdb); 959de103c93SChristoph Hellwig cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; 960d6e0175cSChristoph Hellwig 961d6e0175cSChristoph Hellwig /* 9621fd032eeSChristoph Hellwig * Setup BIDI XOR callback to be run during after I/O 963d6e0175cSChristoph Hellwig * completion. 964d6e0175cSChristoph Hellwig */ 965de103c93SChristoph Hellwig cmd->execute_cmd = sbc_execute_rw; 9666f974e8cSChristoph Hellwig cmd->transport_complete_callback = &xdreadwrite_callback; 967d6e0175cSChristoph Hellwig break; 968d6e0175cSChristoph Hellwig case WRITE_SAME_32: 969d6e0175cSChristoph Hellwig sectors = transport_get_sectors_32(cdb); 970d6e0175cSChristoph Hellwig if (!sectors) { 971d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" 972d6e0175cSChristoph Hellwig " supported\n"); 973d6e0175cSChristoph Hellwig return TCM_INVALID_CDB_FIELD; 974d6e0175cSChristoph Hellwig } 975d6e0175cSChristoph Hellwig 976d6e0175cSChristoph Hellwig size = sbc_get_size(cmd, 1); 977d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[12]); 978d6e0175cSChristoph Hellwig 979d6e0175cSChristoph Hellwig ret = sbc_setup_write_same(cmd, &cdb[10], ops); 980d6e0175cSChristoph Hellwig if (ret) 9811fd032eeSChristoph Hellwig return ret; 982d6e0175cSChristoph Hellwig break; 983d6e0175cSChristoph Hellwig default: 984d6e0175cSChristoph Hellwig pr_err("VARIABLE_LENGTH_CMD service action" 985d6e0175cSChristoph Hellwig " 0x%04x not supported\n", service_action); 986de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 987d6e0175cSChristoph Hellwig } 988d6e0175cSChristoph Hellwig break; 989d6e0175cSChristoph Hellwig } 99068ff9b9bSNicholas Bellinger case COMPARE_AND_WRITE: 99168ff9b9bSNicholas Bellinger sectors = cdb[13]; 99268ff9b9bSNicholas Bellinger /* 99368ff9b9bSNicholas Bellinger * Currently enforce COMPARE_AND_WRITE for a single sector 99468ff9b9bSNicholas Bellinger */ 99568ff9b9bSNicholas Bellinger if (sectors > 1) { 99668ff9b9bSNicholas Bellinger pr_err("COMPARE_AND_WRITE contains NoLB: %u greater" 99768ff9b9bSNicholas Bellinger " than 1\n", sectors); 99868ff9b9bSNicholas Bellinger return TCM_INVALID_CDB_FIELD; 99968ff9b9bSNicholas Bellinger } 1000ab81a5e0SDavid Disseldorp if (sbc_check_dpofua(dev, cmd, cdb)) 1001ab81a5e0SDavid Disseldorp return TCM_INVALID_CDB_FIELD; 1002ab81a5e0SDavid Disseldorp 100368ff9b9bSNicholas Bellinger /* 100468ff9b9bSNicholas Bellinger * Double size because we have two buffers, note that 100568ff9b9bSNicholas Bellinger * zero is not an error.. 100668ff9b9bSNicholas Bellinger */ 100768ff9b9bSNicholas Bellinger size = 2 * sbc_get_size(cmd, sectors); 100868ff9b9bSNicholas Bellinger cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 100968ff9b9bSNicholas Bellinger cmd->t_task_nolb = sectors; 101068ff9b9bSNicholas Bellinger cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE; 101168ff9b9bSNicholas Bellinger cmd->execute_cmd = sbc_compare_and_write; 101268ff9b9bSNicholas Bellinger cmd->transport_complete_callback = compare_and_write_callback; 101368ff9b9bSNicholas Bellinger break; 1014d6e0175cSChristoph Hellwig case READ_CAPACITY: 10151fd032eeSChristoph Hellwig size = READ_CAP_LEN; 10161fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity; 1017d6e0175cSChristoph Hellwig break; 1018eb846d9fSHannes Reinecke case SERVICE_ACTION_IN_16: 1019d6e0175cSChristoph Hellwig switch (cmd->t_task_cdb[1] & 0x1f) { 1020d6e0175cSChristoph Hellwig case SAI_READ_CAPACITY_16: 10211fd032eeSChristoph Hellwig cmd->execute_cmd = sbc_emulate_readcapacity_16; 1022d6e0175cSChristoph Hellwig break; 1023c66094bfSHannes Reinecke case SAI_REPORT_REFERRALS: 1024c66094bfSHannes Reinecke cmd->execute_cmd = target_emulate_report_referrals; 1025c66094bfSHannes Reinecke break; 1026d6e0175cSChristoph Hellwig default: 1027d6e0175cSChristoph Hellwig pr_err("Unsupported SA: 0x%02x\n", 1028d6e0175cSChristoph Hellwig cmd->t_task_cdb[1] & 0x1f); 1029de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1030d6e0175cSChristoph Hellwig } 10311fd032eeSChristoph Hellwig size = (cdb[10] << 24) | (cdb[11] << 16) | 1032d6e0175cSChristoph Hellwig (cdb[12] << 8) | cdb[13]; 1033d6e0175cSChristoph Hellwig break; 1034d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE: 1035d6e0175cSChristoph Hellwig case SYNCHRONIZE_CACHE_16: 1036d6e0175cSChristoph Hellwig if (cdb[0] == SYNCHRONIZE_CACHE) { 1037d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1038d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_32(cdb); 1039d6e0175cSChristoph Hellwig } else { 1040d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1041d6e0175cSChristoph Hellwig cmd->t_task_lba = transport_lba_64(cdb); 1042d6e0175cSChristoph Hellwig } 10436ef31dc7SChristophe Vu-Brugier if (ops->execute_sync_cache) { 1044ad67f0d9SChristoph Hellwig cmd->execute_cmd = ops->execute_sync_cache; 10456ef31dc7SChristophe Vu-Brugier goto check_lba; 10466ef31dc7SChristophe Vu-Brugier } 10476ef31dc7SChristophe Vu-Brugier size = 0; 10486ef31dc7SChristophe Vu-Brugier cmd->execute_cmd = sbc_emulate_noop; 1049d6e0175cSChristoph Hellwig break; 1050d6e0175cSChristoph Hellwig case UNMAP: 105114150a6bSChristoph Hellwig if (!ops->execute_unmap) 1052de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 105314150a6bSChristoph Hellwig 105461fdb4acSNicholas Bellinger if (!dev->dev_attrib.emulate_tpu) { 105561fdb4acSNicholas Bellinger pr_err("Got UNMAP, but backend device has" 105661fdb4acSNicholas Bellinger " emulate_tpu disabled\n"); 105761fdb4acSNicholas Bellinger return TCM_UNSUPPORTED_SCSI_OPCODE; 105861fdb4acSNicholas Bellinger } 10591fd032eeSChristoph Hellwig size = get_unaligned_be16(&cdb[7]); 106062e46942SChristoph Hellwig cmd->execute_cmd = sbc_execute_unmap; 1061d6e0175cSChristoph Hellwig break; 1062d6e0175cSChristoph Hellwig case WRITE_SAME_16: 1063d6e0175cSChristoph Hellwig sectors = transport_get_sectors_16(cdb); 1064d6e0175cSChristoph Hellwig if (!sectors) { 1065d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1066de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1067d6e0175cSChristoph Hellwig } 1068d6e0175cSChristoph Hellwig 10691fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1070d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be64(&cdb[2]); 1071d6e0175cSChristoph Hellwig 1072cd063befSNicholas Bellinger ret = sbc_setup_write_same(cmd, &cdb[1], ops); 10736b64e1feSDan Carpenter if (ret) 1074cd063befSNicholas Bellinger return ret; 1075d6e0175cSChristoph Hellwig break; 1076d6e0175cSChristoph Hellwig case WRITE_SAME: 1077d6e0175cSChristoph Hellwig sectors = transport_get_sectors_10(cdb); 1078d6e0175cSChristoph Hellwig if (!sectors) { 1079d6e0175cSChristoph Hellwig pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); 1080de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD; 1081d6e0175cSChristoph Hellwig } 1082d6e0175cSChristoph Hellwig 10831fd032eeSChristoph Hellwig size = sbc_get_size(cmd, 1); 1084d6e0175cSChristoph Hellwig cmd->t_task_lba = get_unaligned_be32(&cdb[2]); 1085d6e0175cSChristoph Hellwig 1086d6e0175cSChristoph Hellwig /* 1087d6e0175cSChristoph Hellwig * Follow sbcr26 with WRITE_SAME (10) and check for the existence 1088d6e0175cSChristoph Hellwig * of byte 1 bit 3 UNMAP instead of original reserved field 1089d6e0175cSChristoph Hellwig */ 1090cd063befSNicholas Bellinger ret = sbc_setup_write_same(cmd, &cdb[1], ops); 10916b64e1feSDan Carpenter if (ret) 1092cd063befSNicholas Bellinger return ret; 1093d6e0175cSChristoph Hellwig break; 1094d6e0175cSChristoph Hellwig case VERIFY: 10951fd032eeSChristoph Hellwig size = 0; 1096c52716deSChristophe Vu-Brugier sectors = transport_get_sectors_10(cdb); 1097c52716deSChristophe Vu-Brugier cmd->t_task_lba = transport_lba_32(cdb); 10981920ed61SNicholas Bellinger cmd->execute_cmd = sbc_emulate_noop; 1099c52716deSChristophe Vu-Brugier goto check_lba; 11001a1ff38cSBernhard Kohl case REZERO_UNIT: 11011a1ff38cSBernhard Kohl case SEEK_6: 11021a1ff38cSBernhard Kohl case SEEK_10: 11031a1ff38cSBernhard Kohl /* 11041a1ff38cSBernhard Kohl * There are still clients out there which use these old SCSI-2 11051a1ff38cSBernhard Kohl * commands. This mainly happens when running VMs with legacy 11061a1ff38cSBernhard Kohl * guest systems, connected via SCSI command pass-through to 11071a1ff38cSBernhard Kohl * iSCSI targets. Make them happy and return status GOOD. 11081a1ff38cSBernhard Kohl */ 11091a1ff38cSBernhard Kohl size = 0; 11101a1ff38cSBernhard Kohl cmd->execute_cmd = sbc_emulate_noop; 11111a1ff38cSBernhard Kohl break; 111245182ed5SBrian Bunker case START_STOP: 111345182ed5SBrian Bunker size = 0; 111445182ed5SBrian Bunker cmd->execute_cmd = sbc_emulate_startstop; 111545182ed5SBrian Bunker break; 1116d6e0175cSChristoph Hellwig default: 11171fd032eeSChristoph Hellwig ret = spc_parse_cdb(cmd, &size); 1118d6e0175cSChristoph Hellwig if (ret) 1119d6e0175cSChristoph Hellwig return ret; 1120d6e0175cSChristoph Hellwig } 1121d6e0175cSChristoph Hellwig 1122d6e0175cSChristoph Hellwig /* reject any command that we don't have a handler for */ 112320959c4bSAndy Grover if (!cmd->execute_cmd) 1124de103c93SChristoph Hellwig return TCM_UNSUPPORTED_SCSI_OPCODE; 1125d6e0175cSChristoph Hellwig 1126d6e0175cSChristoph Hellwig if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { 11271fd032eeSChristoph Hellwig unsigned long long end_lba; 11286ef31dc7SChristophe Vu-Brugier check_lba: 11291fd032eeSChristoph Hellwig end_lba = dev->transport->get_blocks(dev) + 1; 1130aa179935SNicholas Bellinger if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) || 1131aa179935SNicholas Bellinger ((cmd->t_task_lba + sectors) > end_lba)) { 11321fd032eeSChristoph Hellwig pr_err("cmd exceeds last lba %llu " 11331fd032eeSChristoph Hellwig "(lba %llu, sectors %u)\n", 11341fd032eeSChristoph Hellwig end_lba, cmd->t_task_lba, sectors); 113509ceadc7SRoland Dreier return TCM_ADDRESS_OUT_OF_RANGE; 1136d6e0175cSChristoph Hellwig } 1137d6e0175cSChristoph Hellwig 113868ff9b9bSNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) 11391fd032eeSChristoph Hellwig size = sbc_get_size(cmd, sectors); 11401fd032eeSChristoph Hellwig } 11411fd032eeSChristoph Hellwig 1142de103c93SChristoph Hellwig return target_cmd_size_check(cmd, size); 1143d6e0175cSChristoph Hellwig } 1144d6e0175cSChristoph Hellwig EXPORT_SYMBOL(sbc_parse_cdb); 11456f23ac8aSChristoph Hellwig 11466f23ac8aSChristoph Hellwig u32 sbc_get_device_type(struct se_device *dev) 11476f23ac8aSChristoph Hellwig { 11486f23ac8aSChristoph Hellwig return TYPE_DISK; 11496f23ac8aSChristoph Hellwig } 11506f23ac8aSChristoph Hellwig EXPORT_SYMBOL(sbc_get_device_type); 115186d71829SAsias He 115262e46942SChristoph Hellwig static sense_reason_t 115362e46942SChristoph Hellwig sbc_execute_unmap(struct se_cmd *cmd) 115486d71829SAsias He { 115562e46942SChristoph Hellwig struct sbc_ops *ops = cmd->protocol_data; 115686d71829SAsias He struct se_device *dev = cmd->se_dev; 115786d71829SAsias He unsigned char *buf, *ptr = NULL; 115886d71829SAsias He sector_t lba; 115986d71829SAsias He int size; 116086d71829SAsias He u32 range; 116186d71829SAsias He sense_reason_t ret = 0; 116286d71829SAsias He int dl, bd_dl; 116386d71829SAsias He 116486d71829SAsias He /* We never set ANC_SUP */ 116586d71829SAsias He if (cmd->t_task_cdb[1]) 116686d71829SAsias He return TCM_INVALID_CDB_FIELD; 116786d71829SAsias He 116886d71829SAsias He if (cmd->data_length == 0) { 116986d71829SAsias He target_complete_cmd(cmd, SAM_STAT_GOOD); 117086d71829SAsias He return 0; 117186d71829SAsias He } 117286d71829SAsias He 117386d71829SAsias He if (cmd->data_length < 8) { 117486d71829SAsias He pr_warn("UNMAP parameter list length %u too small\n", 117586d71829SAsias He cmd->data_length); 117686d71829SAsias He return TCM_PARAMETER_LIST_LENGTH_ERROR; 117786d71829SAsias He } 117886d71829SAsias He 117986d71829SAsias He buf = transport_kmap_data_sg(cmd); 118086d71829SAsias He if (!buf) 118186d71829SAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 118286d71829SAsias He 118386d71829SAsias He dl = get_unaligned_be16(&buf[0]); 118486d71829SAsias He bd_dl = get_unaligned_be16(&buf[2]); 118586d71829SAsias He 118686d71829SAsias He size = cmd->data_length - 8; 118786d71829SAsias He if (bd_dl > size) 118886d71829SAsias He pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n", 118986d71829SAsias He cmd->data_length, bd_dl); 119086d71829SAsias He else 119186d71829SAsias He size = bd_dl; 119286d71829SAsias He 119386d71829SAsias He if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) { 119486d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 119586d71829SAsias He goto err; 119686d71829SAsias He } 119786d71829SAsias He 119886d71829SAsias He /* First UNMAP block descriptor starts at 8 byte offset */ 119986d71829SAsias He ptr = &buf[8]; 120086d71829SAsias He pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u" 120186d71829SAsias He " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr); 120286d71829SAsias He 120386d71829SAsias He while (size >= 16) { 120486d71829SAsias He lba = get_unaligned_be64(&ptr[0]); 120586d71829SAsias He range = get_unaligned_be32(&ptr[8]); 120686d71829SAsias He pr_debug("UNMAP: Using lba: %llu and range: %u\n", 120786d71829SAsias He (unsigned long long)lba, range); 120886d71829SAsias He 120986d71829SAsias He if (range > dev->dev_attrib.max_unmap_lba_count) { 121086d71829SAsias He ret = TCM_INVALID_PARAMETER_LIST; 121186d71829SAsias He goto err; 121286d71829SAsias He } 121386d71829SAsias He 121486d71829SAsias He if (lba + range > dev->transport->get_blocks(dev) + 1) { 121586d71829SAsias He ret = TCM_ADDRESS_OUT_OF_RANGE; 121686d71829SAsias He goto err; 121786d71829SAsias He } 121886d71829SAsias He 121962e46942SChristoph Hellwig ret = ops->execute_unmap(cmd, lba, range); 122086d71829SAsias He if (ret) 122186d71829SAsias He goto err; 122286d71829SAsias He 122386d71829SAsias He ptr += 16; 122486d71829SAsias He size -= 16; 122586d71829SAsias He } 122686d71829SAsias He 122786d71829SAsias He err: 122886d71829SAsias He transport_kunmap_data_sg(cmd); 122986d71829SAsias He if (!ret) 123086d71829SAsias He target_complete_cmd(cmd, GOOD); 123186d71829SAsias He return ret; 123286d71829SAsias He } 123341861fa8SNicholas Bellinger 123466a3d5bcSNicholas Bellinger void 123566a3d5bcSNicholas Bellinger sbc_dif_generate(struct se_cmd *cmd) 123666a3d5bcSNicholas Bellinger { 123766a3d5bcSNicholas Bellinger struct se_device *dev = cmd->se_dev; 1238fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 123918213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg, *psg; 124066a3d5bcSNicholas Bellinger sector_t sector = cmd->t_task_lba; 124166a3d5bcSNicholas Bellinger void *daddr, *paddr; 124266a3d5bcSNicholas Bellinger int i, j, offset = 0; 124318213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 124466a3d5bcSNicholas Bellinger 124518213afbSAkinobu Mita for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 124618213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 124766a3d5bcSNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 124866a3d5bcSNicholas Bellinger 124918213afbSAkinobu Mita for (j = 0; j < psg->length; 1250fe052a18SSagi Grimberg j += sizeof(*sdt)) { 125118213afbSAkinobu Mita __u16 crc; 125218213afbSAkinobu Mita unsigned int avail; 125366a3d5bcSNicholas Bellinger 125418213afbSAkinobu Mita if (offset >= dsg->length) { 125518213afbSAkinobu Mita offset -= dsg->length; 125618213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 125718213afbSAkinobu Mita dsg = sg_next(dsg); 125818213afbSAkinobu Mita if (!dsg) { 125918213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 126018213afbSAkinobu Mita return; 126118213afbSAkinobu Mita } 126218213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 126366a3d5bcSNicholas Bellinger } 126466a3d5bcSNicholas Bellinger 126518213afbSAkinobu Mita sdt = paddr + j; 126618213afbSAkinobu Mita avail = min(block_size, dsg->length - offset); 126718213afbSAkinobu Mita crc = crc_t10dif(daddr + offset, avail); 126818213afbSAkinobu Mita if (avail < block_size) { 126918213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 127018213afbSAkinobu Mita dsg = sg_next(dsg); 127118213afbSAkinobu Mita if (!dsg) { 127218213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 127318213afbSAkinobu Mita return; 127418213afbSAkinobu Mita } 127518213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 127618213afbSAkinobu Mita offset = block_size - avail; 127718213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, offset); 127818213afbSAkinobu Mita } else { 127918213afbSAkinobu Mita offset += block_size; 128018213afbSAkinobu Mita } 128118213afbSAkinobu Mita 128218213afbSAkinobu Mita sdt->guard_tag = cpu_to_be16(crc); 1283823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT) 128466a3d5bcSNicholas Bellinger sdt->ref_tag = cpu_to_be32(sector & 0xffffffff); 128566a3d5bcSNicholas Bellinger sdt->app_tag = 0; 128666a3d5bcSNicholas Bellinger 12876ae50408SNicholas Bellinger pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x" 128866a3d5bcSNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 12896ae50408SNicholas Bellinger (cmd->data_direction == DMA_TO_DEVICE) ? 12906ae50408SNicholas Bellinger "WRITE" : "READ", (unsigned long long)sector, 12916ae50408SNicholas Bellinger sdt->guard_tag, sdt->app_tag, 12926ae50408SNicholas Bellinger be32_to_cpu(sdt->ref_tag)); 129366a3d5bcSNicholas Bellinger 129466a3d5bcSNicholas Bellinger sector++; 129566a3d5bcSNicholas Bellinger } 129666a3d5bcSNicholas Bellinger 129718213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 129818213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 129966a3d5bcSNicholas Bellinger } 130066a3d5bcSNicholas Bellinger } 130166a3d5bcSNicholas Bellinger 130241861fa8SNicholas Bellinger static sense_reason_t 1303fe052a18SSagi Grimberg sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt, 130418213afbSAkinobu Mita __u16 crc, sector_t sector, unsigned int ei_lba) 130541861fa8SNicholas Bellinger { 130641861fa8SNicholas Bellinger __be16 csum; 130741861fa8SNicholas Bellinger 1308d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD)) 1309d7a463b0SNicholas Bellinger goto check_ref; 1310d7a463b0SNicholas Bellinger 131118213afbSAkinobu Mita csum = cpu_to_be16(crc); 131241861fa8SNicholas Bellinger 131341861fa8SNicholas Bellinger if (sdt->guard_tag != csum) { 131441861fa8SNicholas Bellinger pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x" 131541861fa8SNicholas Bellinger " csum 0x%04x\n", (unsigned long long)sector, 131641861fa8SNicholas Bellinger be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum)); 131741861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; 131841861fa8SNicholas Bellinger } 131941861fa8SNicholas Bellinger 1320d7a463b0SNicholas Bellinger check_ref: 1321d7a463b0SNicholas Bellinger if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG)) 1322d7a463b0SNicholas Bellinger return 0; 1323d7a463b0SNicholas Bellinger 1324823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE1_PROT && 132541861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) { 132641861fa8SNicholas Bellinger pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x" 132741861fa8SNicholas Bellinger " sector MSB: 0x%08x\n", (unsigned long long)sector, 132841861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff)); 132941861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 133041861fa8SNicholas Bellinger } 133141861fa8SNicholas Bellinger 1332823ddd87SNicholas Bellinger if (cmd->prot_type == TARGET_DIF_TYPE2_PROT && 133341861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag) != ei_lba) { 133441861fa8SNicholas Bellinger pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x" 133541861fa8SNicholas Bellinger " ei_lba: 0x%08x\n", (unsigned long long)sector, 133641861fa8SNicholas Bellinger be32_to_cpu(sdt->ref_tag), ei_lba); 133741861fa8SNicholas Bellinger return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; 133841861fa8SNicholas Bellinger } 133941861fa8SNicholas Bellinger 134041861fa8SNicholas Bellinger return 0; 134141861fa8SNicholas Bellinger } 134241861fa8SNicholas Bellinger 1343f75b6faeSSagi Grimberg void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read, 134441861fa8SNicholas Bellinger struct scatterlist *sg, int sg_off) 134541861fa8SNicholas Bellinger { 134641861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 134741861fa8SNicholas Bellinger struct scatterlist *psg; 134841861fa8SNicholas Bellinger void *paddr, *addr; 134941861fa8SNicholas Bellinger unsigned int i, len, left; 135010762e80SNicholas Bellinger unsigned int offset = sg_off; 135141861fa8SNicholas Bellinger 135238b57f82SNicholas Bellinger if (!sg) 135338b57f82SNicholas Bellinger return; 135438b57f82SNicholas Bellinger 135541861fa8SNicholas Bellinger left = sectors * dev->prot_length; 135641861fa8SNicholas Bellinger 135741861fa8SNicholas Bellinger for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) { 135816c0ae02SSagi Grimberg unsigned int psg_len, copied = 0; 135941861fa8SNicholas Bellinger 136016c0ae02SSagi Grimberg paddr = kmap_atomic(sg_page(psg)) + psg->offset; 136116c0ae02SSagi Grimberg psg_len = min(left, psg->length); 136216c0ae02SSagi Grimberg while (psg_len) { 136316c0ae02SSagi Grimberg len = min(psg_len, sg->length - offset); 136416c0ae02SSagi Grimberg addr = kmap_atomic(sg_page(sg)) + sg->offset + offset; 136516c0ae02SSagi Grimberg 136616c0ae02SSagi Grimberg if (read) 136716c0ae02SSagi Grimberg memcpy(paddr + copied, addr, len); 136816c0ae02SSagi Grimberg else 136916c0ae02SSagi Grimberg memcpy(addr, paddr + copied, len); 137016c0ae02SSagi Grimberg 137116c0ae02SSagi Grimberg left -= len; 137216c0ae02SSagi Grimberg offset += len; 137316c0ae02SSagi Grimberg copied += len; 137416c0ae02SSagi Grimberg psg_len -= len; 137516c0ae02SSagi Grimberg 137657636388SAkinobu Mita kunmap_atomic(addr - sg->offset - offset); 137757636388SAkinobu Mita 1378d6a65fdcSSagi Grimberg if (offset >= sg->length) { 1379d6a65fdcSSagi Grimberg sg = sg_next(sg); 1380d6a65fdcSSagi Grimberg offset = 0; 1381d6a65fdcSSagi Grimberg } 138241861fa8SNicholas Bellinger } 138357636388SAkinobu Mita kunmap_atomic(paddr - psg->offset); 138416c0ae02SSagi Grimberg } 138541861fa8SNicholas Bellinger } 1386f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_copy_prot); 138741861fa8SNicholas Bellinger 138841861fa8SNicholas Bellinger sense_reason_t 1389f75b6faeSSagi Grimberg sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors, 1390414e4627SSagi Grimberg unsigned int ei_lba, struct scatterlist *psg, int psg_off) 139141861fa8SNicholas Bellinger { 139241861fa8SNicholas Bellinger struct se_device *dev = cmd->se_dev; 1393fe052a18SSagi Grimberg struct t10_pi_tuple *sdt; 139418213afbSAkinobu Mita struct scatterlist *dsg = cmd->t_data_sg; 139541861fa8SNicholas Bellinger sector_t sector = start; 139641861fa8SNicholas Bellinger void *daddr, *paddr; 139718213afbSAkinobu Mita int i; 139841861fa8SNicholas Bellinger sense_reason_t rc; 139918213afbSAkinobu Mita int dsg_off = 0; 140018213afbSAkinobu Mita unsigned int block_size = dev->dev_attrib.block_size; 140141861fa8SNicholas Bellinger 140218213afbSAkinobu Mita for (; psg && sector < start + sectors; psg = sg_next(psg)) { 140318213afbSAkinobu Mita paddr = kmap_atomic(sg_page(psg)) + psg->offset; 140441861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 140541861fa8SNicholas Bellinger 140618213afbSAkinobu Mita for (i = psg_off; i < psg->length && 140718213afbSAkinobu Mita sector < start + sectors; 1408fe052a18SSagi Grimberg i += sizeof(*sdt)) { 140918213afbSAkinobu Mita __u16 crc; 141018213afbSAkinobu Mita unsigned int avail; 141141861fa8SNicholas Bellinger 141218213afbSAkinobu Mita if (dsg_off >= dsg->length) { 141318213afbSAkinobu Mita dsg_off -= dsg->length; 141418213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 141518213afbSAkinobu Mita dsg = sg_next(dsg); 141618213afbSAkinobu Mita if (!dsg) { 1417414e4627SSagi Grimberg kunmap_atomic(paddr - psg->offset); 141841861fa8SNicholas Bellinger return 0; 141941861fa8SNicholas Bellinger } 142041861fa8SNicholas Bellinger daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 142141861fa8SNicholas Bellinger } 142241861fa8SNicholas Bellinger 142318213afbSAkinobu Mita sdt = paddr + i; 142441861fa8SNicholas Bellinger 142541861fa8SNicholas Bellinger pr_debug("DIF READ sector: %llu guard_tag: 0x%04x" 142641861fa8SNicholas Bellinger " app_tag: 0x%04x ref_tag: %u\n", 142741861fa8SNicholas Bellinger (unsigned long long)sector, sdt->guard_tag, 142841861fa8SNicholas Bellinger sdt->app_tag, be32_to_cpu(sdt->ref_tag)); 142941861fa8SNicholas Bellinger 143041861fa8SNicholas Bellinger if (sdt->app_tag == cpu_to_be16(0xffff)) { 143118213afbSAkinobu Mita dsg_off += block_size; 143218213afbSAkinobu Mita goto next; 143341861fa8SNicholas Bellinger } 143441861fa8SNicholas Bellinger 143518213afbSAkinobu Mita avail = min(block_size, dsg->length - dsg_off); 143618213afbSAkinobu Mita crc = crc_t10dif(daddr + dsg_off, avail); 143718213afbSAkinobu Mita if (avail < block_size) { 1438414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 143918213afbSAkinobu Mita dsg = sg_next(dsg); 144018213afbSAkinobu Mita if (!dsg) { 144118213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 144218213afbSAkinobu Mita return 0; 144318213afbSAkinobu Mita } 144418213afbSAkinobu Mita daddr = kmap_atomic(sg_page(dsg)) + dsg->offset; 144518213afbSAkinobu Mita dsg_off = block_size - avail; 144618213afbSAkinobu Mita crc = crc_t10dif_update(crc, daddr, dsg_off); 144718213afbSAkinobu Mita } else { 144818213afbSAkinobu Mita dsg_off += block_size; 144918213afbSAkinobu Mita } 145018213afbSAkinobu Mita 145118213afbSAkinobu Mita rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba); 145241861fa8SNicholas Bellinger if (rc) { 145318213afbSAkinobu Mita kunmap_atomic(daddr - dsg->offset); 145418213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 145576736db3SSagi Grimberg cmd->bad_sector = sector; 145641861fa8SNicholas Bellinger return rc; 145741861fa8SNicholas Bellinger } 145818213afbSAkinobu Mita next: 145941861fa8SNicholas Bellinger sector++; 146041861fa8SNicholas Bellinger ei_lba++; 146141861fa8SNicholas Bellinger } 146241861fa8SNicholas Bellinger 146318213afbSAkinobu Mita psg_off = 0; 1464414e4627SSagi Grimberg kunmap_atomic(daddr - dsg->offset); 146518213afbSAkinobu Mita kunmap_atomic(paddr - psg->offset); 146641861fa8SNicholas Bellinger } 146741861fa8SNicholas Bellinger 146841861fa8SNicholas Bellinger return 0; 146941861fa8SNicholas Bellinger } 1470f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_verify); 1471