11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d6e0175cSChristoph Hellwig /*
3d6e0175cSChristoph Hellwig  * SCSI Block Commands (SBC) parsing and emulation.
4d6e0175cSChristoph Hellwig  *
54c76251eSNicholas Bellinger  * (c) Copyright 2002-2013 Datera, Inc.
6d6e0175cSChristoph Hellwig  *
7d6e0175cSChristoph Hellwig  * Nicholas A. Bellinger <nab@kernel.org>
8d6e0175cSChristoph Hellwig  */
9d6e0175cSChristoph Hellwig 
10d6e0175cSChristoph Hellwig #include <linux/kernel.h>
11d6e0175cSChristoph Hellwig #include <linux/module.h>
12d6e0175cSChristoph Hellwig #include <linux/ratelimit.h>
1341861fa8SNicholas Bellinger #include <linux/crc-t10dif.h>
148dcf07beSBart Van Assche #include <linux/t10-pi.h>
15d6e0175cSChristoph Hellwig #include <asm/unaligned.h>
16ba929992SBart Van Assche #include <scsi/scsi_proto.h>
1768ff9b9bSNicholas Bellinger #include <scsi/scsi_tcq.h>
18d6e0175cSChristoph Hellwig 
19d6e0175cSChristoph Hellwig #include <target/target_core_base.h>
20d6e0175cSChristoph Hellwig #include <target/target_core_backend.h>
21d6e0175cSChristoph Hellwig #include <target/target_core_fabric.h>
22d6e0175cSChristoph Hellwig 
23d6e0175cSChristoph Hellwig #include "target_core_internal.h"
24d6e0175cSChristoph Hellwig #include "target_core_ua.h"
25c66094bfSHannes Reinecke #include "target_core_alua.h"
26d6e0175cSChristoph Hellwig 
27de103c93SChristoph Hellwig static sense_reason_t
286d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char, u32, bool);
2962e46942SChristoph Hellwig static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd);
30afd73f1bSNicholas Bellinger 
31afd73f1bSNicholas Bellinger static sense_reason_t
sbc_emulate_readcapacity(struct se_cmd * cmd)32de103c93SChristoph Hellwig sbc_emulate_readcapacity(struct se_cmd *cmd)
331fd032eeSChristoph Hellwig {
341fd032eeSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
358dc8632aSRoland Dreier 	unsigned char *cdb = cmd->t_task_cdb;
361fd032eeSChristoph Hellwig 	unsigned long long blocks_long = dev->transport->get_blocks(dev);
37a50da144SPaolo Bonzini 	unsigned char *rbuf;
38a50da144SPaolo Bonzini 	unsigned char buf[8];
391fd032eeSChristoph Hellwig 	u32 blocks;
401fd032eeSChristoph Hellwig 
418dc8632aSRoland Dreier 	/*
428dc8632aSRoland Dreier 	 * SBC-2 says:
438dc8632aSRoland Dreier 	 *   If the PMI bit is set to zero and the LOGICAL BLOCK
448dc8632aSRoland Dreier 	 *   ADDRESS field is not set to zero, the device server shall
458dc8632aSRoland Dreier 	 *   terminate the command with CHECK CONDITION status with
468dc8632aSRoland Dreier 	 *   the sense key set to ILLEGAL REQUEST and the additional
478dc8632aSRoland Dreier 	 *   sense code set to INVALID FIELD IN CDB.
488dc8632aSRoland Dreier 	 *
498dc8632aSRoland Dreier 	 * In SBC-3, these fields are obsolete, but some SCSI
508dc8632aSRoland Dreier 	 * compliance tests actually check this, so we might as well
518dc8632aSRoland Dreier 	 * follow SBC-2.
528dc8632aSRoland Dreier 	 */
538dc8632aSRoland Dreier 	if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5]))
548dc8632aSRoland Dreier 		return TCM_INVALID_CDB_FIELD;
558dc8632aSRoland Dreier 
561fd032eeSChristoph Hellwig 	if (blocks_long >= 0x00000000ffffffff)
571fd032eeSChristoph Hellwig 		blocks = 0xffffffff;
581fd032eeSChristoph Hellwig 	else
591fd032eeSChristoph Hellwig 		blocks = (u32)blocks_long;
601fd032eeSChristoph Hellwig 
61a85d667eSBart Van Assche 	put_unaligned_be32(blocks, &buf[0]);
62a85d667eSBart Van Assche 	put_unaligned_be32(dev->dev_attrib.block_size, &buf[4]);
631fd032eeSChristoph Hellwig 
64a50da144SPaolo Bonzini 	rbuf = transport_kmap_data_sg(cmd);
658b4b0dcbSNicholas Bellinger 	if (rbuf) {
66a50da144SPaolo Bonzini 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
671fd032eeSChristoph Hellwig 		transport_kunmap_data_sg(cmd);
688b4b0dcbSNicholas Bellinger 	}
691fd032eeSChristoph Hellwig 
7014b40c1eSHannes Reinecke 	target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 8);
711fd032eeSChristoph Hellwig 	return 0;
721fd032eeSChristoph Hellwig }
731fd032eeSChristoph Hellwig 
74de103c93SChristoph Hellwig static sense_reason_t
sbc_emulate_readcapacity_16(struct se_cmd * cmd)75de103c93SChristoph Hellwig sbc_emulate_readcapacity_16(struct se_cmd *cmd)
761fd032eeSChristoph Hellwig {
771fd032eeSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
782d335983SNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
799ef5466eSNicholas Bellinger 	int pi_prot_type = dev->dev_attrib.pi_prot_type;
809ef5466eSNicholas Bellinger 
81a50da144SPaolo Bonzini 	unsigned char *rbuf;
82a50da144SPaolo Bonzini 	unsigned char buf[32];
831fd032eeSChristoph Hellwig 	unsigned long long blocks = dev->transport->get_blocks(dev);
841fd032eeSChristoph Hellwig 
85a50da144SPaolo Bonzini 	memset(buf, 0, sizeof(buf));
86a85d667eSBart Van Assche 	put_unaligned_be64(blocks, &buf[0]);
87a85d667eSBart Van Assche 	put_unaligned_be32(dev->dev_attrib.block_size, &buf[8]);
8856dac14cSNicholas Bellinger 	/*
8956dac14cSNicholas Bellinger 	 * Set P_TYPE and PROT_EN bits for DIF support
9056dac14cSNicholas Bellinger 	 */
912d335983SNicholas Bellinger 	if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
929ef5466eSNicholas Bellinger 		/*
939ef5466eSNicholas Bellinger 		 * Only override a device's pi_prot_type if no T10-PI is
949ef5466eSNicholas Bellinger 		 * available, and sess_prot_type has been explicitly enabled.
959ef5466eSNicholas Bellinger 		 */
969ef5466eSNicholas Bellinger 		if (!pi_prot_type)
979ef5466eSNicholas Bellinger 			pi_prot_type = sess->sess_prot_type;
989ef5466eSNicholas Bellinger 
999ef5466eSNicholas Bellinger 		if (pi_prot_type)
1009ef5466eSNicholas Bellinger 			buf[12] = (pi_prot_type - 1) << 1 | 0x1;
1012d335983SNicholas Bellinger 	}
1027f7caf6aSAndy Grover 
1037f7caf6aSAndy Grover 	if (dev->transport->get_lbppbe)
1047f7caf6aSAndy Grover 		buf[13] = dev->transport->get_lbppbe(dev) & 0x0f;
1057f7caf6aSAndy Grover 
1067f7caf6aSAndy Grover 	if (dev->transport->get_alignment_offset_lbas) {
1077f7caf6aSAndy Grover 		u16 lalba = dev->transport->get_alignment_offset_lbas(dev);
108a85d667eSBart Van Assche 
109a85d667eSBart Van Assche 		put_unaligned_be16(lalba, &buf[14]);
1107f7caf6aSAndy Grover 	}
1117f7caf6aSAndy Grover 
1121fd032eeSChristoph Hellwig 	/*
1131fd032eeSChristoph Hellwig 	 * Set Thin Provisioning Enable bit following sbc3r22 in section
1141fd032eeSChristoph Hellwig 	 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
1151fd032eeSChristoph Hellwig 	 */
116e6f41633SJamie Pocas 	if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) {
1177f7caf6aSAndy Grover 		buf[14] |= 0x80;
1181fd032eeSChristoph Hellwig 
119e6f41633SJamie Pocas 		/*
120e6f41633SJamie Pocas 		 * LBPRZ signifies that zeroes will be read back from an LBA after
121e6f41633SJamie Pocas 		 * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2)
122e6f41633SJamie Pocas 		 */
123e6f41633SJamie Pocas 		if (dev->dev_attrib.unmap_zeroes_data)
124e6f41633SJamie Pocas 			buf[14] |= 0x40;
125e6f41633SJamie Pocas 	}
126e6f41633SJamie Pocas 
127a50da144SPaolo Bonzini 	rbuf = transport_kmap_data_sg(cmd);
1288b4b0dcbSNicholas Bellinger 	if (rbuf) {
129a50da144SPaolo Bonzini 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
1301fd032eeSChristoph Hellwig 		transport_kunmap_data_sg(cmd);
1318b4b0dcbSNicholas Bellinger 	}
1321fd032eeSChristoph Hellwig 
13314b40c1eSHannes Reinecke 	target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 32);
1341fd032eeSChristoph Hellwig 	return 0;
1351fd032eeSChristoph Hellwig }
1361fd032eeSChristoph Hellwig 
13745182ed5SBrian Bunker static sense_reason_t
sbc_emulate_startstop(struct se_cmd * cmd)13845182ed5SBrian Bunker sbc_emulate_startstop(struct se_cmd *cmd)
13945182ed5SBrian Bunker {
14045182ed5SBrian Bunker 	unsigned char *cdb = cmd->t_task_cdb;
14145182ed5SBrian Bunker 
14245182ed5SBrian Bunker 	/*
14345182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
14445182ed5SBrian Bunker 	 * Immediate bit should be set since there is nothing to complete
14545182ed5SBrian Bunker 	 * POWER CONDITION MODIFIER 0h
14645182ed5SBrian Bunker 	 */
14745182ed5SBrian Bunker 	if (!(cdb[1] & 1) || cdb[2] || cdb[3])
14845182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
14945182ed5SBrian Bunker 
15045182ed5SBrian Bunker 	/*
15145182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
15245182ed5SBrian Bunker 	 * POWER CONDITION 0h START_VALID - process START and LOEJ
15345182ed5SBrian Bunker 	 */
15445182ed5SBrian Bunker 	if (cdb[4] >> 4 & 0xf)
15545182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
15645182ed5SBrian Bunker 
15745182ed5SBrian Bunker 	/*
15845182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
15945182ed5SBrian Bunker 	 * LOEJ 0h - nothing to load or unload
16045182ed5SBrian Bunker 	 * START 1h - we are ready
16145182ed5SBrian Bunker 	 */
16245182ed5SBrian Bunker 	if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4))
16345182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
16445182ed5SBrian Bunker 
16545182ed5SBrian Bunker 	target_complete_cmd(cmd, SAM_STAT_GOOD);
16645182ed5SBrian Bunker 	return 0;
16745182ed5SBrian Bunker }
16845182ed5SBrian Bunker 
sbc_get_write_same_sectors(struct se_cmd * cmd)169972b29c8SRoland Dreier sector_t sbc_get_write_same_sectors(struct se_cmd *cmd)
1701fd032eeSChristoph Hellwig {
1711fd032eeSChristoph Hellwig 	u32 num_blocks;
1721fd032eeSChristoph Hellwig 
1731fd032eeSChristoph Hellwig 	if (cmd->t_task_cdb[0] == WRITE_SAME)
1741fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
1751fd032eeSChristoph Hellwig 	else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
1761fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
1771fd032eeSChristoph Hellwig 	else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
1781fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
1791fd032eeSChristoph Hellwig 
1801fd032eeSChristoph Hellwig 	/*
1811fd032eeSChristoph Hellwig 	 * Use the explicit range when non zero is supplied, otherwise calculate
1821fd032eeSChristoph Hellwig 	 * the remaining range based on ->get_blocks() - starting LBA.
1831fd032eeSChristoph Hellwig 	 */
1846f974e8cSChristoph Hellwig 	if (num_blocks)
1856f974e8cSChristoph Hellwig 		return num_blocks;
1861fd032eeSChristoph Hellwig 
1876f974e8cSChristoph Hellwig 	return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
1886f974e8cSChristoph Hellwig 		cmd->t_task_lba + 1;
1891fd032eeSChristoph Hellwig }
190972b29c8SRoland Dreier EXPORT_SYMBOL(sbc_get_write_same_sectors);
1911fd032eeSChristoph Hellwig 
192de103c93SChristoph Hellwig static sense_reason_t
sbc_execute_write_same_unmap(struct se_cmd * cmd)193b753d643SChristoph Hellwig sbc_execute_write_same_unmap(struct se_cmd *cmd)
194b753d643SChristoph Hellwig {
195*0217da08SMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
196b753d643SChristoph Hellwig 	sector_t nolb = sbc_get_write_same_sectors(cmd);
197b753d643SChristoph Hellwig 	sense_reason_t ret;
198b753d643SChristoph Hellwig 
199b753d643SChristoph Hellwig 	if (nolb) {
200b753d643SChristoph Hellwig 		ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb);
201b753d643SChristoph Hellwig 		if (ret)
202b753d643SChristoph Hellwig 			return ret;
203b753d643SChristoph Hellwig 	}
204b753d643SChristoph Hellwig 
20514b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
206b753d643SChristoph Hellwig 	return 0;
207b753d643SChristoph Hellwig }
208b753d643SChristoph Hellwig 
209b753d643SChristoph Hellwig static sense_reason_t
sbc_emulate_noop(struct se_cmd * cmd)2101920ed61SNicholas Bellinger sbc_emulate_noop(struct se_cmd *cmd)
2111a1ff38cSBernhard Kohl {
21214b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
2131a1ff38cSBernhard Kohl 	return 0;
2141a1ff38cSBernhard Kohl }
2151a1ff38cSBernhard Kohl 
sbc_get_size(struct se_cmd * cmd,u32 sectors)216d6e0175cSChristoph Hellwig static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
217d6e0175cSChristoph Hellwig {
2180fd97ccfSChristoph Hellwig 	return cmd->se_dev->dev_attrib.block_size * sectors;
219d6e0175cSChristoph Hellwig }
220d6e0175cSChristoph Hellwig 
transport_get_sectors_6(unsigned char * cdb)221d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_6(unsigned char *cdb)
222d6e0175cSChristoph Hellwig {
223d6e0175cSChristoph Hellwig 	/*
224d6e0175cSChristoph Hellwig 	 * Use 8-bit sector value.  SBC-3 says:
225d6e0175cSChristoph Hellwig 	 *
226d6e0175cSChristoph Hellwig 	 *   A TRANSFER LENGTH field set to zero specifies that 256
227d6e0175cSChristoph Hellwig 	 *   logical blocks shall be written.  Any other value
228d6e0175cSChristoph Hellwig 	 *   specifies the number of logical blocks that shall be
229d6e0175cSChristoph Hellwig 	 *   written.
230d6e0175cSChristoph Hellwig 	 */
231d6e0175cSChristoph Hellwig 	return cdb[4] ? : 256;
232d6e0175cSChristoph Hellwig }
233d6e0175cSChristoph Hellwig 
transport_get_sectors_10(unsigned char * cdb)234d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_10(unsigned char *cdb)
235d6e0175cSChristoph Hellwig {
236a85d667eSBart Van Assche 	return get_unaligned_be16(&cdb[7]);
237d6e0175cSChristoph Hellwig }
238d6e0175cSChristoph Hellwig 
transport_get_sectors_12(unsigned char * cdb)239d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_12(unsigned char *cdb)
240d6e0175cSChristoph Hellwig {
241a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[6]);
242d6e0175cSChristoph Hellwig }
243d6e0175cSChristoph Hellwig 
transport_get_sectors_16(unsigned char * cdb)244d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_16(unsigned char *cdb)
245d6e0175cSChristoph Hellwig {
246a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[10]);
247d6e0175cSChristoph Hellwig }
248d6e0175cSChristoph Hellwig 
249d6e0175cSChristoph Hellwig /*
250d6e0175cSChristoph Hellwig  * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
251d6e0175cSChristoph Hellwig  */
transport_get_sectors_32(unsigned char * cdb)252d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_32(unsigned char *cdb)
253d6e0175cSChristoph Hellwig {
254a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[28]);
255d6e0175cSChristoph Hellwig 
256d6e0175cSChristoph Hellwig }
257d6e0175cSChristoph Hellwig 
transport_lba_21(unsigned char * cdb)258d6e0175cSChristoph Hellwig static inline u32 transport_lba_21(unsigned char *cdb)
259d6e0175cSChristoph Hellwig {
260a85d667eSBart Van Assche 	return get_unaligned_be24(&cdb[1]) & 0x1fffff;
261d6e0175cSChristoph Hellwig }
262d6e0175cSChristoph Hellwig 
transport_lba_32(unsigned char * cdb)263d6e0175cSChristoph Hellwig static inline u32 transport_lba_32(unsigned char *cdb)
264d6e0175cSChristoph Hellwig {
265a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[2]);
266d6e0175cSChristoph Hellwig }
267d6e0175cSChristoph Hellwig 
transport_lba_64(unsigned char * cdb)268d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64(unsigned char *cdb)
269d6e0175cSChristoph Hellwig {
270a85d667eSBart Van Assche 	return get_unaligned_be64(&cdb[2]);
271d6e0175cSChristoph Hellwig }
272d6e0175cSChristoph Hellwig 
273cd063befSNicholas Bellinger static sense_reason_t
sbc_setup_write_same(struct se_cmd * cmd,unsigned char flags,struct exec_cmd_ops * ops)274*0217da08SMike Christie sbc_setup_write_same(struct se_cmd *cmd, unsigned char flags,
275*0217da08SMike Christie 		     struct exec_cmd_ops *ops)
276d6e0175cSChristoph Hellwig {
2778e575c50SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2788e575c50SNicholas Bellinger 	sector_t end_lba = dev->transport->get_blocks(dev) + 1;
279972b29c8SRoland Dreier 	unsigned int sectors = sbc_get_write_same_sectors(cmd);
280afd73f1bSNicholas Bellinger 	sense_reason_t ret;
281773cbaf7SNicholas Bellinger 
2826d8e7e7cSDmitry Bogdanov 	if ((flags & 0x04) || (flags & 0x02)) {
283d6e0175cSChristoph Hellwig 		pr_err("WRITE_SAME PBDATA and LBDATA"
284d6e0175cSChristoph Hellwig 			" bits not supported for Block Discard"
285d6e0175cSChristoph Hellwig 			" Emulation\n");
286cd063befSNicholas Bellinger 		return TCM_UNSUPPORTED_SCSI_OPCODE;
287d6e0175cSChristoph Hellwig 	}
288773cbaf7SNicholas Bellinger 	if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) {
289773cbaf7SNicholas Bellinger 		pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n",
290773cbaf7SNicholas Bellinger 			sectors, cmd->se_dev->dev_attrib.max_write_same_len);
291773cbaf7SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
292773cbaf7SNicholas Bellinger 	}
2938e575c50SNicholas Bellinger 	/*
2948e575c50SNicholas Bellinger 	 * Sanity check for LBA wrap and request past end of device.
2958e575c50SNicholas Bellinger 	 */
2968e575c50SNicholas Bellinger 	if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
2978e575c50SNicholas Bellinger 	    ((cmd->t_task_lba + sectors) > end_lba)) {
2988e575c50SNicholas Bellinger 		pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n",
2998e575c50SNicholas Bellinger 		       (unsigned long long)end_lba, cmd->t_task_lba, sectors);
3008e575c50SNicholas Bellinger 		return TCM_ADDRESS_OUT_OF_RANGE;
3018e575c50SNicholas Bellinger 	}
3028e575c50SNicholas Bellinger 
3035cb770bfSRoland Dreier 	/* We always have ANC_SUP == 0 so setting ANCHOR is always an error */
3046d8e7e7cSDmitry Bogdanov 	if (flags & 0x10) {
3055cb770bfSRoland Dreier 		pr_warn("WRITE SAME with ANCHOR not supported\n");
3065cb770bfSRoland Dreier 		return TCM_INVALID_CDB_FIELD;
3075cb770bfSRoland Dreier 	}
308ccd3f449SMike Christie 
309ccd3f449SMike Christie 	if (flags & 0x01) {
310ccd3f449SMike Christie 		pr_warn("WRITE SAME with NDOB not supported\n");
311ccd3f449SMike Christie 		return TCM_INVALID_CDB_FIELD;
312ccd3f449SMike Christie 	}
313ccd3f449SMike Christie 
314d6e0175cSChristoph Hellwig 	/*
315cd063befSNicholas Bellinger 	 * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting
316cd063befSNicholas Bellinger 	 * translated into block discard requests within backend code.
317d6e0175cSChristoph Hellwig 	 */
3186d8e7e7cSDmitry Bogdanov 	if (flags & 0x08) {
319b753d643SChristoph Hellwig 		if (!ops->execute_unmap)
320cd063befSNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
321d6e0175cSChristoph Hellwig 
322d0a91295SNicholas Bellinger 		if (!dev->dev_attrib.emulate_tpws) {
323d0a91295SNicholas Bellinger 			pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device"
324d0a91295SNicholas Bellinger 			       " has emulate_tpws disabled\n");
325d0a91295SNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
326d0a91295SNicholas Bellinger 		}
327b753d643SChristoph Hellwig 		cmd->execute_cmd = sbc_execute_write_same_unmap;
328cd063befSNicholas Bellinger 		return 0;
329cd063befSNicholas Bellinger 	}
330cd063befSNicholas Bellinger 	if (!ops->execute_write_same)
331cd063befSNicholas Bellinger 		return TCM_UNSUPPORTED_SCSI_OPCODE;
332cd063befSNicholas Bellinger 
3336d8e7e7cSDmitry Bogdanov 	ret = sbc_check_prot(dev, cmd, flags >> 5, sectors, true);
334afd73f1bSNicholas Bellinger 	if (ret)
335afd73f1bSNicholas Bellinger 		return ret;
336afd73f1bSNicholas Bellinger 
337cd063befSNicholas Bellinger 	cmd->execute_cmd = ops->execute_write_same;
338d6e0175cSChristoph Hellwig 	return 0;
339d6e0175cSChristoph Hellwig }
340d6e0175cSChristoph Hellwig 
341a82a9538SNicholas Bellinger static sense_reason_t
sbc_execute_rw(struct se_cmd * cmd)342a82a9538SNicholas Bellinger sbc_execute_rw(struct se_cmd *cmd)
343a82a9538SNicholas Bellinger {
344*0217da08SMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
3457a971b1bSChristoph Hellwig 
3467a971b1bSChristoph Hellwig 	return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents,
347a82a9538SNicholas Bellinger 			       cmd->data_direction);
348a82a9538SNicholas Bellinger }
349a82a9538SNicholas Bellinger 
compare_and_write_post(struct se_cmd * cmd,bool success,int * post_ret)350057085e5SNicholas Bellinger static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success,
351057085e5SNicholas Bellinger 					     int *post_ret)
35268ff9b9bSNicholas Bellinger {
35368ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3549b2792c3SNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
35568ff9b9bSNicholas Bellinger 
356d8855c15SNicholas Bellinger 	spin_lock_irq(&cmd->t_state_lock);
357aa73237dSBart Van Assche 	if (success) {
358057085e5SNicholas Bellinger 		*post_ret = 1;
3599b2792c3SNicholas Bellinger 
3609b2792c3SNicholas Bellinger 		if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
3619b2792c3SNicholas Bellinger 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
362057085e5SNicholas Bellinger 	}
363d8855c15SNicholas Bellinger 	spin_unlock_irq(&cmd->t_state_lock);
364d8855c15SNicholas Bellinger 
36568ff9b9bSNicholas Bellinger 	/*
36668ff9b9bSNicholas Bellinger 	 * Unlock ->caw_sem originally obtained during sbc_compare_and_write()
36768ff9b9bSNicholas Bellinger 	 * before the original READ I/O submission.
36868ff9b9bSNicholas Bellinger 	 */
36968ff9b9bSNicholas Bellinger 	up(&dev->caw_sem);
37068ff9b9bSNicholas Bellinger 
3719b2792c3SNicholas Bellinger 	return ret;
37268ff9b9bSNicholas Bellinger }
37368ff9b9bSNicholas Bellinger 
374ab628b9fSDavid Disseldorp /*
375749c226cSDavid Disseldorp  * compare @cmp_len bytes of @read_sgl with @cmp_sgl. On miscompare, fill
376749c226cSDavid Disseldorp  * @miscmp_off and return TCM_MISCOMPARE_VERIFY.
377ab628b9fSDavid Disseldorp  */
378ab628b9fSDavid Disseldorp static sense_reason_t
compare_and_write_do_cmp(struct scatterlist * read_sgl,unsigned int read_nents,struct scatterlist * cmp_sgl,unsigned int cmp_nents,unsigned int cmp_len,unsigned int * miscmp_off)379ab628b9fSDavid Disseldorp compare_and_write_do_cmp(struct scatterlist *read_sgl, unsigned int read_nents,
380ab628b9fSDavid Disseldorp 			 struct scatterlist *cmp_sgl, unsigned int cmp_nents,
381749c226cSDavid Disseldorp 			 unsigned int cmp_len, unsigned int *miscmp_off)
382ab628b9fSDavid Disseldorp {
383ab628b9fSDavid Disseldorp 	unsigned char *buf = NULL;
384ab628b9fSDavid Disseldorp 	struct scatterlist *sg;
385ab628b9fSDavid Disseldorp 	sense_reason_t ret;
386ab628b9fSDavid Disseldorp 	unsigned int offset;
387ab628b9fSDavid Disseldorp 	size_t rc;
3885cfb5b02SChaitanya Kulkarni 	int sg_cnt;
389ab628b9fSDavid Disseldorp 
390ab628b9fSDavid Disseldorp 	buf = kzalloc(cmp_len, GFP_KERNEL);
391ab628b9fSDavid Disseldorp 	if (!buf) {
392ab628b9fSDavid Disseldorp 		ret = TCM_OUT_OF_RESOURCES;
393ab628b9fSDavid Disseldorp 		goto out;
394ab628b9fSDavid Disseldorp 	}
395ab628b9fSDavid Disseldorp 
396ab628b9fSDavid Disseldorp 	rc = sg_copy_to_buffer(cmp_sgl, cmp_nents, buf, cmp_len);
397ab628b9fSDavid Disseldorp 	if (!rc) {
398ab628b9fSDavid Disseldorp 		pr_err("sg_copy_to_buffer() failed for compare_and_write\n");
399ab628b9fSDavid Disseldorp 		ret = TCM_OUT_OF_RESOURCES;
400ab628b9fSDavid Disseldorp 		goto out;
401ab628b9fSDavid Disseldorp 	}
402ab628b9fSDavid Disseldorp 	/*
403ab628b9fSDavid Disseldorp 	 * Compare SCSI READ payload against verify payload
404ab628b9fSDavid Disseldorp 	 */
405ab628b9fSDavid Disseldorp 	offset = 0;
406749c226cSDavid Disseldorp 	ret = TCM_NO_SENSE;
4075cfb5b02SChaitanya Kulkarni 	for_each_sg(read_sgl, sg, read_nents, sg_cnt) {
408ab628b9fSDavid Disseldorp 		unsigned int len = min(sg->length, cmp_len);
409ab628b9fSDavid Disseldorp 		unsigned char *addr = kmap_atomic(sg_page(sg));
410ab628b9fSDavid Disseldorp 
411ab628b9fSDavid Disseldorp 		if (memcmp(addr, buf + offset, len)) {
412749c226cSDavid Disseldorp 			unsigned int i;
413749c226cSDavid Disseldorp 
414749c226cSDavid Disseldorp 			for (i = 0; i < len && addr[i] == buf[offset + i]; i++)
415749c226cSDavid Disseldorp 				;
416749c226cSDavid Disseldorp 			*miscmp_off = offset + i;
417749c226cSDavid Disseldorp 			pr_warn("Detected MISCOMPARE at offset %u\n",
418749c226cSDavid Disseldorp 				*miscmp_off);
419ab628b9fSDavid Disseldorp 			ret = TCM_MISCOMPARE_VERIFY;
420ab628b9fSDavid Disseldorp 		}
421ab628b9fSDavid Disseldorp 		kunmap_atomic(addr);
422749c226cSDavid Disseldorp 		if (ret != TCM_NO_SENSE)
423749c226cSDavid Disseldorp 			goto out;
424ab628b9fSDavid Disseldorp 
425ab628b9fSDavid Disseldorp 		offset += len;
426ab628b9fSDavid Disseldorp 		cmp_len -= len;
427ab628b9fSDavid Disseldorp 		if (!cmp_len)
428ab628b9fSDavid Disseldorp 			break;
429ab628b9fSDavid Disseldorp 	}
430ab628b9fSDavid Disseldorp 	pr_debug("COMPARE AND WRITE read data matches compare data\n");
431ab628b9fSDavid Disseldorp out:
432ab628b9fSDavid Disseldorp 	kfree(buf);
433ab628b9fSDavid Disseldorp 	return ret;
434ab628b9fSDavid Disseldorp }
435ab628b9fSDavid Disseldorp 
compare_and_write_callback(struct se_cmd * cmd,bool success,int * post_ret)436057085e5SNicholas Bellinger static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success,
437057085e5SNicholas Bellinger 						 int *post_ret)
43868ff9b9bSNicholas Bellinger {
43968ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
44081b6ca6dSBart Van Assche 	struct sg_table write_tbl = { };
441ab628b9fSDavid Disseldorp 	struct scatterlist *write_sg;
44268ff9b9bSNicholas Bellinger 	struct sg_mapping_iter m;
443ab628b9fSDavid Disseldorp 	unsigned int len;
44468ff9b9bSNicholas Bellinger 	unsigned int block_size = dev->dev_attrib.block_size;
445ab628b9fSDavid Disseldorp 	unsigned int compare_len = (cmd->t_task_nolb * block_size);
446749c226cSDavid Disseldorp 	unsigned int miscmp_off = 0;
44768ff9b9bSNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
448ab628b9fSDavid Disseldorp 	int i;
44968ff9b9bSNicholas Bellinger 
450a72629b5SMaurizio Lombardi 	if (!success) {
451cf6d1f09SNicholas Bellinger 		/*
452cf6d1f09SNicholas Bellinger 		 * Handle early failure in transport_generic_request_failure(),
453c8e63985SNicholas Bellinger 		 * which will not have taken ->caw_sem yet..
454cf6d1f09SNicholas Bellinger 		 */
455a72629b5SMaurizio Lombardi 		if (!cmd->t_data_sg || !cmd->t_bidi_data_sg)
456cf6d1f09SNicholas Bellinger 			return TCM_NO_SENSE;
457a72629b5SMaurizio Lombardi 
458a72629b5SMaurizio Lombardi 		/*
459a72629b5SMaurizio Lombardi 		 * The command has been stopped or aborted so
460a72629b5SMaurizio Lombardi 		 * we don't have to perform the write operation.
461a72629b5SMaurizio Lombardi 		 */
462a72629b5SMaurizio Lombardi 		WARN_ON(!(cmd->transport_state &
463a72629b5SMaurizio Lombardi 			(CMD_T_ABORTED | CMD_T_STOP)));
464a72629b5SMaurizio Lombardi 		goto out;
465a72629b5SMaurizio Lombardi 	}
466db60df88SNicholas Bellinger 	/*
467c8e63985SNicholas Bellinger 	 * Handle special case for zero-length COMPARE_AND_WRITE
468c8e63985SNicholas Bellinger 	 */
469c8e63985SNicholas Bellinger 	if (!cmd->data_length)
470c8e63985SNicholas Bellinger 		goto out;
471c8e63985SNicholas Bellinger 	/*
472db60df88SNicholas Bellinger 	 * Immediately exit + release dev->caw_sem if command has already
473db60df88SNicholas Bellinger 	 * been failed with a non-zero SCSI status.
474db60df88SNicholas Bellinger 	 */
475db60df88SNicholas Bellinger 	if (cmd->scsi_status) {
476a71a5dc7SNicholas Bellinger 		pr_debug("compare_and_write_callback: non zero scsi_status:"
477db60df88SNicholas Bellinger 			" 0x%02x\n", cmd->scsi_status);
478a71a5dc7SNicholas Bellinger 		*post_ret = 1;
479a71a5dc7SNicholas Bellinger 		if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
480a71a5dc7SNicholas Bellinger 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
481db60df88SNicholas Bellinger 		goto out;
482db60df88SNicholas Bellinger 	}
483cf6d1f09SNicholas Bellinger 
484ab628b9fSDavid Disseldorp 	ret = compare_and_write_do_cmp(cmd->t_bidi_data_sg,
485ab628b9fSDavid Disseldorp 				       cmd->t_bidi_data_nents,
486ab628b9fSDavid Disseldorp 				       cmd->t_data_sg,
487ab628b9fSDavid Disseldorp 				       cmd->t_data_nents,
488749c226cSDavid Disseldorp 				       compare_len,
489749c226cSDavid Disseldorp 				       &miscmp_off);
490749c226cSDavid Disseldorp 	if (ret == TCM_MISCOMPARE_VERIFY) {
491749c226cSDavid Disseldorp 		/*
492749c226cSDavid Disseldorp 		 * SBC-4 r15: 5.3 COMPARE AND WRITE command
493749c226cSDavid Disseldorp 		 * In the sense data (see 4.18 and SPC-5) the offset from the
494749c226cSDavid Disseldorp 		 * start of the Data-Out Buffer to the first byte of data that
495749c226cSDavid Disseldorp 		 * was not equal shall be reported in the INFORMATION field.
496749c226cSDavid Disseldorp 		 */
497749c226cSDavid Disseldorp 		cmd->sense_info = miscmp_off;
498749c226cSDavid Disseldorp 		goto out;
499749c226cSDavid Disseldorp 	} else if (ret)
500a2890087SNicholas Bellinger 		goto out;
50168ff9b9bSNicholas Bellinger 
50281b6ca6dSBart Van Assche 	if (sg_alloc_table(&write_tbl, cmd->t_data_nents, GFP_KERNEL) < 0) {
50368ff9b9bSNicholas Bellinger 		pr_err("Unable to allocate compare_and_write sg\n");
50468ff9b9bSNicholas Bellinger 		ret = TCM_OUT_OF_RESOURCES;
50568ff9b9bSNicholas Bellinger 		goto out;
50668ff9b9bSNicholas Bellinger 	}
50781b6ca6dSBart Van Assche 	write_sg = write_tbl.sgl;
50868ff9b9bSNicholas Bellinger 
50968ff9b9bSNicholas Bellinger 	i = 0;
510ab628b9fSDavid Disseldorp 	len = compare_len;
51168ff9b9bSNicholas Bellinger 	sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG);
51268ff9b9bSNicholas Bellinger 	/*
51368ff9b9bSNicholas Bellinger 	 * Currently assumes NoLB=1 and SGLs are PAGE_SIZE..
51468ff9b9bSNicholas Bellinger 	 */
51568ff9b9bSNicholas Bellinger 	while (len) {
51668ff9b9bSNicholas Bellinger 		sg_miter_next(&m);
51768ff9b9bSNicholas Bellinger 
51868ff9b9bSNicholas Bellinger 		if (block_size < PAGE_SIZE) {
51968ff9b9bSNicholas Bellinger 			sg_set_page(&write_sg[i], m.page, block_size,
520d94e5a61SJan Engelhardt 				    m.piter.sg->offset + block_size);
52168ff9b9bSNicholas Bellinger 		} else {
52268ff9b9bSNicholas Bellinger 			sg_miter_next(&m);
52368ff9b9bSNicholas Bellinger 			sg_set_page(&write_sg[i], m.page, block_size,
524d94e5a61SJan Engelhardt 				    m.piter.sg->offset);
52568ff9b9bSNicholas Bellinger 		}
52668ff9b9bSNicholas Bellinger 		len -= block_size;
52768ff9b9bSNicholas Bellinger 		i++;
52868ff9b9bSNicholas Bellinger 	}
52968ff9b9bSNicholas Bellinger 	sg_miter_stop(&m);
53068ff9b9bSNicholas Bellinger 	/*
53168ff9b9bSNicholas Bellinger 	 * Save the original SGL + nents values before updating to new
53268ff9b9bSNicholas Bellinger 	 * assignments, to be released in transport_free_pages() ->
53368ff9b9bSNicholas Bellinger 	 * transport_reset_sgl_orig()
53468ff9b9bSNicholas Bellinger 	 */
53568ff9b9bSNicholas Bellinger 	cmd->t_data_sg_orig = cmd->t_data_sg;
53668ff9b9bSNicholas Bellinger 	cmd->t_data_sg = write_sg;
53768ff9b9bSNicholas Bellinger 	cmd->t_data_nents_orig = cmd->t_data_nents;
53868ff9b9bSNicholas Bellinger 	cmd->t_data_nents = 1;
53968ff9b9bSNicholas Bellinger 
54068d81f40SChristoph Hellwig 	cmd->sam_task_attr = TCM_HEAD_TAG;
54168ff9b9bSNicholas Bellinger 	cmd->transport_complete_callback = compare_and_write_post;
54268ff9b9bSNicholas Bellinger 	/*
54368ff9b9bSNicholas Bellinger 	 * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler
54468ff9b9bSNicholas Bellinger 	 * for submitting the adjusted SGL to write instance user-data.
54568ff9b9bSNicholas Bellinger 	 */
54668ff9b9bSNicholas Bellinger 	cmd->execute_cmd = sbc_execute_rw;
54768ff9b9bSNicholas Bellinger 
54868ff9b9bSNicholas Bellinger 	spin_lock_irq(&cmd->t_state_lock);
54968ff9b9bSNicholas Bellinger 	cmd->t_state = TRANSPORT_PROCESSING;
550fd5e64deSBart Van Assche 	cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
55168ff9b9bSNicholas Bellinger 	spin_unlock_irq(&cmd->t_state_lock);
55268ff9b9bSNicholas Bellinger 
553dff0ca9eSNicholas Bellinger 	__target_execute_cmd(cmd, false);
55468ff9b9bSNicholas Bellinger 
55568ff9b9bSNicholas Bellinger 	return ret;
55668ff9b9bSNicholas Bellinger 
55768ff9b9bSNicholas Bellinger out:
55868ff9b9bSNicholas Bellinger 	/*
55968ff9b9bSNicholas Bellinger 	 * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in
56068ff9b9bSNicholas Bellinger 	 * sbc_compare_and_write() before the original READ I/O submission.
56168ff9b9bSNicholas Bellinger 	 */
56268ff9b9bSNicholas Bellinger 	up(&dev->caw_sem);
56381b6ca6dSBart Van Assche 	sg_free_table(&write_tbl);
56468ff9b9bSNicholas Bellinger 	return ret;
56568ff9b9bSNicholas Bellinger }
56668ff9b9bSNicholas Bellinger 
56768ff9b9bSNicholas Bellinger static sense_reason_t
sbc_compare_and_write(struct se_cmd * cmd)56868ff9b9bSNicholas Bellinger sbc_compare_and_write(struct se_cmd *cmd)
56968ff9b9bSNicholas Bellinger {
570*0217da08SMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
57168ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
57268ff9b9bSNicholas Bellinger 	sense_reason_t ret;
57368ff9b9bSNicholas Bellinger 	int rc;
57468ff9b9bSNicholas Bellinger 	/*
57568ff9b9bSNicholas Bellinger 	 * Submit the READ first for COMPARE_AND_WRITE to perform the
57668ff9b9bSNicholas Bellinger 	 * comparision using SGLs at cmd->t_bidi_data_sg..
57768ff9b9bSNicholas Bellinger 	 */
57868ff9b9bSNicholas Bellinger 	rc = down_interruptible(&dev->caw_sem);
579ee7619f2SNicholas Bellinger 	if (rc != 0) {
58068ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = NULL;
58168ff9b9bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
58268ff9b9bSNicholas Bellinger 	}
583b7191253SNicholas Bellinger 	/*
584b7191253SNicholas Bellinger 	 * Reset cmd->data_length to individual block_size in order to not
585b7191253SNicholas Bellinger 	 * confuse backend drivers that depend on this value matching the
586b7191253SNicholas Bellinger 	 * size of the I/O being submitted.
587b7191253SNicholas Bellinger 	 */
588b7191253SNicholas Bellinger 	cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size;
58968ff9b9bSNicholas Bellinger 
5907a971b1bSChristoph Hellwig 	ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents,
59168ff9b9bSNicholas Bellinger 			      DMA_FROM_DEVICE);
59268ff9b9bSNicholas Bellinger 	if (ret) {
59368ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = NULL;
59468ff9b9bSNicholas Bellinger 		up(&dev->caw_sem);
59568ff9b9bSNicholas Bellinger 		return ret;
59668ff9b9bSNicholas Bellinger 	}
59768ff9b9bSNicholas Bellinger 	/*
59868ff9b9bSNicholas Bellinger 	 * Unlock of dev->caw_sem to occur in compare_and_write_callback()
59968ff9b9bSNicholas Bellinger 	 * upon MISCOMPARE, or in compare_and_write_done() upon completion
60068ff9b9bSNicholas Bellinger 	 * of WRITE instance user-data.
60168ff9b9bSNicholas Bellinger 	 */
60268ff9b9bSNicholas Bellinger 	return TCM_NO_SENSE;
60368ff9b9bSNicholas Bellinger }
60468ff9b9bSNicholas Bellinger 
60519f9361aSSagi Grimberg static int
sbc_set_prot_op_checks(u8 protect,bool fabric_prot,enum target_prot_type prot_type,bool is_write,struct se_cmd * cmd)60638b57f82SNicholas Bellinger sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type,
60719f9361aSSagi Grimberg 		       bool is_write, struct se_cmd *cmd)
60819f9361aSSagi Grimberg {
60919f9361aSSagi Grimberg 	if (is_write) {
61038b57f82SNicholas Bellinger 		cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP :
61138b57f82SNicholas Bellinger 			       protect ? TARGET_PROT_DOUT_PASS :
61219f9361aSSagi Grimberg 			       TARGET_PROT_DOUT_INSERT;
61319f9361aSSagi Grimberg 		switch (protect) {
61419f9361aSSagi Grimberg 		case 0x0:
61519f9361aSSagi Grimberg 		case 0x3:
61619f9361aSSagi Grimberg 			cmd->prot_checks = 0;
61719f9361aSSagi Grimberg 			break;
61819f9361aSSagi Grimberg 		case 0x1:
61919f9361aSSagi Grimberg 		case 0x5:
62019f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
62119f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
62219f9361aSSagi Grimberg 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
62319f9361aSSagi Grimberg 			break;
62419f9361aSSagi Grimberg 		case 0x2:
62519f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
62619f9361aSSagi Grimberg 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
62719f9361aSSagi Grimberg 			break;
62819f9361aSSagi Grimberg 		case 0x4:
62919f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
63019f9361aSSagi Grimberg 			break;
63119f9361aSSagi Grimberg 		default:
63219f9361aSSagi Grimberg 			pr_err("Unsupported protect field %d\n", protect);
63319f9361aSSagi Grimberg 			return -EINVAL;
63419f9361aSSagi Grimberg 		}
63519f9361aSSagi Grimberg 	} else {
63638b57f82SNicholas Bellinger 		cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT :
63738b57f82SNicholas Bellinger 			       protect ? TARGET_PROT_DIN_PASS :
63819f9361aSSagi Grimberg 			       TARGET_PROT_DIN_STRIP;
63919f9361aSSagi Grimberg 		switch (protect) {
64019f9361aSSagi Grimberg 		case 0x0:
64119f9361aSSagi Grimberg 		case 0x1:
64219f9361aSSagi Grimberg 		case 0x5:
64319f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
64419f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
64519f9361aSSagi Grimberg 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
64619f9361aSSagi Grimberg 			break;
64719f9361aSSagi Grimberg 		case 0x2:
64819f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
64919f9361aSSagi Grimberg 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
65019f9361aSSagi Grimberg 			break;
65119f9361aSSagi Grimberg 		case 0x3:
65219f9361aSSagi Grimberg 			cmd->prot_checks = 0;
65319f9361aSSagi Grimberg 			break;
65419f9361aSSagi Grimberg 		case 0x4:
65519f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
65619f9361aSSagi Grimberg 			break;
65719f9361aSSagi Grimberg 		default:
65819f9361aSSagi Grimberg 			pr_err("Unsupported protect field %d\n", protect);
65919f9361aSSagi Grimberg 			return -EINVAL;
66019f9361aSSagi Grimberg 		}
66119f9361aSSagi Grimberg 	}
66219f9361aSSagi Grimberg 
66319f9361aSSagi Grimberg 	return 0;
66419f9361aSSagi Grimberg }
66519f9361aSSagi Grimberg 
666f7b7c06fSNicholas Bellinger static sense_reason_t
sbc_check_prot(struct se_device * dev,struct se_cmd * cmd,unsigned char protect,u32 sectors,bool is_write)6676d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char protect,
66819f9361aSSagi Grimberg 	       u32 sectors, bool is_write)
669499bf77bSNicholas Bellinger {
67038b57f82SNicholas Bellinger 	int sp_ops = cmd->se_sess->sup_prot_ops;
67138b57f82SNicholas Bellinger 	int pi_prot_type = dev->dev_attrib.pi_prot_type;
67238b57f82SNicholas Bellinger 	bool fabric_prot = false;
67319f9361aSSagi Grimberg 
674f7b7c06fSNicholas Bellinger 	if (!cmd->t_prot_sg || !cmd->t_prot_nents) {
67538b57f82SNicholas Bellinger 		if (unlikely(protect &&
67638b57f82SNicholas Bellinger 		    !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) {
67738b57f82SNicholas Bellinger 			pr_err("CDB contains protect bit, but device + fabric does"
67838b57f82SNicholas Bellinger 			       " not advertise PROTECT=1 feature bit\n");
679f7b7c06fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
680f7b7c06fSNicholas Bellinger 		}
681f7b7c06fSNicholas Bellinger 		if (cmd->prot_pto)
682f7b7c06fSNicholas Bellinger 			return TCM_NO_SENSE;
683f7b7c06fSNicholas Bellinger 	}
684499bf77bSNicholas Bellinger 
685499bf77bSNicholas Bellinger 	switch (dev->dev_attrib.pi_prot_type) {
686499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE3_PROT:
687499bf77bSNicholas Bellinger 		cmd->reftag_seed = 0xffffffff;
688499bf77bSNicholas Bellinger 		break;
689499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE2_PROT:
69019f9361aSSagi Grimberg 		if (protect)
691f7b7c06fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
692499bf77bSNicholas Bellinger 
693499bf77bSNicholas Bellinger 		cmd->reftag_seed = cmd->t_task_lba;
694499bf77bSNicholas Bellinger 		break;
695499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE1_PROT:
696499bf77bSNicholas Bellinger 		cmd->reftag_seed = cmd->t_task_lba;
697499bf77bSNicholas Bellinger 		break;
698499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE0_PROT:
69938b57f82SNicholas Bellinger 		/*
70038b57f82SNicholas Bellinger 		 * See if the fabric supports T10-PI, and the session has been
70138b57f82SNicholas Bellinger 		 * configured to allow export PROTECT=1 feature bit with backend
70238b57f82SNicholas Bellinger 		 * devices that don't support T10-PI.
70338b57f82SNicholas Bellinger 		 */
70438b57f82SNicholas Bellinger 		fabric_prot = is_write ?
70538b57f82SNicholas Bellinger 			      !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) :
70638b57f82SNicholas Bellinger 			      !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT));
70738b57f82SNicholas Bellinger 
70838b57f82SNicholas Bellinger 		if (fabric_prot && cmd->se_sess->sess_prot_type) {
70938b57f82SNicholas Bellinger 			pi_prot_type = cmd->se_sess->sess_prot_type;
71038b57f82SNicholas Bellinger 			break;
71138b57f82SNicholas Bellinger 		}
712cceca4a6SNicholas Bellinger 		if (!protect)
713f7b7c06fSNicholas Bellinger 			return TCM_NO_SENSE;
714df561f66SGustavo A. R. Silva 		fallthrough;
715499bf77bSNicholas Bellinger 	default:
716cceca4a6SNicholas Bellinger 		pr_err("Unable to determine pi_prot_type for CDB: 0x%02x "
7176d8e7e7cSDmitry Bogdanov 		       "PROTECT: 0x%02x\n", cmd->t_task_cdb[0], protect);
718cceca4a6SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
719499bf77bSNicholas Bellinger 	}
720499bf77bSNicholas Bellinger 
72138b57f82SNicholas Bellinger 	if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd))
722f7b7c06fSNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
72319f9361aSSagi Grimberg 
72438b57f82SNicholas Bellinger 	cmd->prot_type = pi_prot_type;
725499bf77bSNicholas Bellinger 	cmd->prot_length = dev->prot_length * sectors;
726e2a4f55cSSagi Grimberg 
727e2a4f55cSSagi Grimberg 	/**
728e2a4f55cSSagi Grimberg 	 * In case protection information exists over the wire
729e2a4f55cSSagi Grimberg 	 * we modify command data length to describe pure data.
730e2a4f55cSSagi Grimberg 	 * The actual transfer length is data length + protection
731e2a4f55cSSagi Grimberg 	 * length
732e2a4f55cSSagi Grimberg 	 **/
733e2a4f55cSSagi Grimberg 	if (protect)
734e2a4f55cSSagi Grimberg 		cmd->data_length = sectors * dev->dev_attrib.block_size;
735e2a4f55cSSagi Grimberg 
736e2a4f55cSSagi Grimberg 	pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d "
737e2a4f55cSSagi Grimberg 		 "prot_op=%d prot_checks=%d\n",
738e2a4f55cSSagi Grimberg 		 __func__, cmd->prot_type, cmd->data_length, cmd->prot_length,
73903abad9eSSagi Grimberg 		 cmd->prot_op, cmd->prot_checks);
740499bf77bSNicholas Bellinger 
741f7b7c06fSNicholas Bellinger 	return TCM_NO_SENSE;
742499bf77bSNicholas Bellinger }
743499bf77bSNicholas Bellinger 
744fde9f50fSNicholas Bellinger static int
sbc_check_dpofua(struct se_device * dev,struct se_cmd * cmd,unsigned char * cdb)745fde9f50fSNicholas Bellinger sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb)
746fde9f50fSNicholas Bellinger {
747fde9f50fSNicholas Bellinger 	if (cdb[1] & 0x10) {
748814e5b45SChristoph Hellwig 		/* see explanation in spc_emulate_modesense */
749814e5b45SChristoph Hellwig 		if (!target_check_fua(dev)) {
750fde9f50fSNicholas Bellinger 			pr_err("Got CDB: 0x%02x with DPO bit set, but device"
751fde9f50fSNicholas Bellinger 			       " does not advertise support for DPO\n", cdb[0]);
752fde9f50fSNicholas Bellinger 			return -EINVAL;
753fde9f50fSNicholas Bellinger 		}
754fde9f50fSNicholas Bellinger 	}
755fde9f50fSNicholas Bellinger 	if (cdb[1] & 0x8) {
756814e5b45SChristoph Hellwig 		if (!target_check_fua(dev)) {
757fde9f50fSNicholas Bellinger 			pr_err("Got CDB: 0x%02x with FUA bit set, but device"
758fde9f50fSNicholas Bellinger 			       " does not advertise support for FUA write\n",
759fde9f50fSNicholas Bellinger 			       cdb[0]);
760fde9f50fSNicholas Bellinger 			return -EINVAL;
761fde9f50fSNicholas Bellinger 		}
762fde9f50fSNicholas Bellinger 		cmd->se_cmd_flags |= SCF_FUA;
763fde9f50fSNicholas Bellinger 	}
764fde9f50fSNicholas Bellinger 	return 0;
765d6e0175cSChristoph Hellwig }
766d6e0175cSChristoph Hellwig 
767de103c93SChristoph Hellwig sense_reason_t
sbc_parse_cdb(struct se_cmd * cmd,struct exec_cmd_ops * ops)768*0217da08SMike Christie sbc_parse_cdb(struct se_cmd *cmd, struct exec_cmd_ops *ops)
769d6e0175cSChristoph Hellwig {
770d6e0175cSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
771d6e0175cSChristoph Hellwig 	unsigned char *cdb = cmd->t_task_cdb;
7721fd032eeSChristoph Hellwig 	unsigned int size;
773d6e0175cSChristoph Hellwig 	u32 sectors = 0;
774de103c93SChristoph Hellwig 	sense_reason_t ret;
775d6e0175cSChristoph Hellwig 
7767a971b1bSChristoph Hellwig 	cmd->protocol_data = ops;
7777a971b1bSChristoph Hellwig 
778d6e0175cSChristoph Hellwig 	switch (cdb[0]) {
779d6e0175cSChristoph Hellwig 	case READ_6:
780d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_6(cdb);
781d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_21(cdb);
782d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
783a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
784d6e0175cSChristoph Hellwig 		break;
785d6e0175cSChristoph Hellwig 	case READ_10:
786d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
787d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
788499bf77bSNicholas Bellinger 
789fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
790fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
791fde9f50fSNicholas Bellinger 
7926d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
793f7b7c06fSNicholas Bellinger 		if (ret)
794f7b7c06fSNicholas Bellinger 			return ret;
795499bf77bSNicholas Bellinger 
796d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
797a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
798d6e0175cSChristoph Hellwig 		break;
799d6e0175cSChristoph Hellwig 	case READ_12:
800d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_12(cdb);
801d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
802499bf77bSNicholas Bellinger 
803fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
804fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
805fde9f50fSNicholas Bellinger 
8066d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
807f7b7c06fSNicholas Bellinger 		if (ret)
808f7b7c06fSNicholas Bellinger 			return ret;
809499bf77bSNicholas Bellinger 
810d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
811a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
812d6e0175cSChristoph Hellwig 		break;
813d6e0175cSChristoph Hellwig 	case READ_16:
814d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
815d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_64(cdb);
816499bf77bSNicholas Bellinger 
817fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
818fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
819fde9f50fSNicholas Bellinger 
8206d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
821f7b7c06fSNicholas Bellinger 		if (ret)
822f7b7c06fSNicholas Bellinger 			return ret;
823499bf77bSNicholas Bellinger 
824d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
825a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
826d6e0175cSChristoph Hellwig 		break;
827d6e0175cSChristoph Hellwig 	case WRITE_6:
828d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_6(cdb);
829d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_21(cdb);
830d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
831a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
832d6e0175cSChristoph Hellwig 		break;
833d6e0175cSChristoph Hellwig 	case WRITE_10:
834984a9d4cSNicholas Bellinger 	case WRITE_VERIFY:
835d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
836d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
837499bf77bSNicholas Bellinger 
838fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
839fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
840499bf77bSNicholas Bellinger 
8416d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
842f7b7c06fSNicholas Bellinger 		if (ret)
843f7b7c06fSNicholas Bellinger 			return ret;
844d6e0175cSChristoph Hellwig 
845d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
846a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
847d6e0175cSChristoph Hellwig 		break;
848d6e0175cSChristoph Hellwig 	case WRITE_12:
849d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_12(cdb);
850d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
851499bf77bSNicholas Bellinger 
852fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
853fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
854499bf77bSNicholas Bellinger 
8556d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
856f7b7c06fSNicholas Bellinger 		if (ret)
857f7b7c06fSNicholas Bellinger 			return ret;
858d6e0175cSChristoph Hellwig 
859d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
860a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
861d6e0175cSChristoph Hellwig 		break;
862d6e0175cSChristoph Hellwig 	case WRITE_16:
863984a9d4cSNicholas Bellinger 	case WRITE_VERIFY_16:
864d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
865d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_64(cdb);
866499bf77bSNicholas Bellinger 
867fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
868fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
869499bf77bSNicholas Bellinger 
8706d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
871f7b7c06fSNicholas Bellinger 		if (ret)
872f7b7c06fSNicholas Bellinger 			return ret;
873cd063befSNicholas Bellinger 
874d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
875d6e0175cSChristoph Hellwig 		cmd->execute_cmd = sbc_execute_rw;
876d6e0175cSChristoph Hellwig 		break;
877d6e0175cSChristoph Hellwig 	case VARIABLE_LENGTH_CMD:
878d6e0175cSChristoph Hellwig 	{
879d6e0175cSChristoph Hellwig 		u16 service_action = get_unaligned_be16(&cdb[8]);
880d6e0175cSChristoph Hellwig 		switch (service_action) {
8816f974e8cSChristoph Hellwig 		case WRITE_SAME_32:
882d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_32(cdb);
883d6e0175cSChristoph Hellwig 			if (!sectors) {
884d6e0175cSChristoph Hellwig 				pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
885d6e0175cSChristoph Hellwig 				       " supported\n");
886de103c93SChristoph Hellwig 				return TCM_INVALID_CDB_FIELD;
887d6e0175cSChristoph Hellwig 			}
888d6e0175cSChristoph Hellwig 
8891fd032eeSChristoph Hellwig 			size = sbc_get_size(cmd, 1);
890d6e0175cSChristoph Hellwig 			cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
891d6e0175cSChristoph Hellwig 
8926d8e7e7cSDmitry Bogdanov 			ret = sbc_setup_write_same(cmd, cdb[10], ops);
893de103c93SChristoph Hellwig 			if (ret)
8946f974e8cSChristoph Hellwig 				return ret;
895d6e0175cSChristoph Hellwig 			break;
896d6e0175cSChristoph Hellwig 		default:
897d6e0175cSChristoph Hellwig 			pr_err("VARIABLE_LENGTH_CMD service action"
898d6e0175cSChristoph Hellwig 				" 0x%04x not supported\n", service_action);
899de103c93SChristoph Hellwig 			return TCM_UNSUPPORTED_SCSI_OPCODE;
900d6e0175cSChristoph Hellwig 		}
901d6e0175cSChristoph Hellwig 		break;
902d6e0175cSChristoph Hellwig 	}
90368ff9b9bSNicholas Bellinger 	case COMPARE_AND_WRITE:
90412f66e4aSJiang Yi 		if (!dev->dev_attrib.emulate_caw) {
9056f3bf5a2SBart Van Assche 			pr_err_ratelimited("se_device %s/%s (vpd_unit_serial %s) reject COMPARE_AND_WRITE\n",
9066f3bf5a2SBart Van Assche 					   dev->se_hba->backend->ops->name,
9076f3bf5a2SBart Van Assche 					   config_item_name(&dev->dev_group.cg_item),
9086f3bf5a2SBart Van Assche 					   dev->t10_wwn.unit_serial);
90912f66e4aSJiang Yi 			return TCM_UNSUPPORTED_SCSI_OPCODE;
91012f66e4aSJiang Yi 		}
91168ff9b9bSNicholas Bellinger 		sectors = cdb[13];
91268ff9b9bSNicholas Bellinger 		/*
91368ff9b9bSNicholas Bellinger 		 * Currently enforce COMPARE_AND_WRITE for a single sector
91468ff9b9bSNicholas Bellinger 		 */
91568ff9b9bSNicholas Bellinger 		if (sectors > 1) {
91668ff9b9bSNicholas Bellinger 			pr_err("COMPARE_AND_WRITE contains NoLB: %u greater"
91768ff9b9bSNicholas Bellinger 			       " than 1\n", sectors);
91868ff9b9bSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
91968ff9b9bSNicholas Bellinger 		}
920ab81a5e0SDavid Disseldorp 		if (sbc_check_dpofua(dev, cmd, cdb))
921ab81a5e0SDavid Disseldorp 			return TCM_INVALID_CDB_FIELD;
922ab81a5e0SDavid Disseldorp 
92368ff9b9bSNicholas Bellinger 		/*
92468ff9b9bSNicholas Bellinger 		 * Double size because we have two buffers, note that
92568ff9b9bSNicholas Bellinger 		 * zero is not an error..
92668ff9b9bSNicholas Bellinger 		 */
92768ff9b9bSNicholas Bellinger 		size = 2 * sbc_get_size(cmd, sectors);
92868ff9b9bSNicholas Bellinger 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
92968ff9b9bSNicholas Bellinger 		cmd->t_task_nolb = sectors;
93068ff9b9bSNicholas Bellinger 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE;
93168ff9b9bSNicholas Bellinger 		cmd->execute_cmd = sbc_compare_and_write;
93268ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = compare_and_write_callback;
93368ff9b9bSNicholas Bellinger 		break;
934d6e0175cSChristoph Hellwig 	case READ_CAPACITY:
9351fd032eeSChristoph Hellwig 		size = READ_CAP_LEN;
9361fd032eeSChristoph Hellwig 		cmd->execute_cmd = sbc_emulate_readcapacity;
937d6e0175cSChristoph Hellwig 		break;
938eb846d9fSHannes Reinecke 	case SERVICE_ACTION_IN_16:
939d6e0175cSChristoph Hellwig 		switch (cmd->t_task_cdb[1] & 0x1f) {
940d6e0175cSChristoph Hellwig 		case SAI_READ_CAPACITY_16:
9411fd032eeSChristoph Hellwig 			cmd->execute_cmd = sbc_emulate_readcapacity_16;
942d6e0175cSChristoph Hellwig 			break;
943c66094bfSHannes Reinecke 		case SAI_REPORT_REFERRALS:
944c66094bfSHannes Reinecke 			cmd->execute_cmd = target_emulate_report_referrals;
945c66094bfSHannes Reinecke 			break;
946d6e0175cSChristoph Hellwig 		default:
947d6e0175cSChristoph Hellwig 			pr_err("Unsupported SA: 0x%02x\n",
948d6e0175cSChristoph Hellwig 				cmd->t_task_cdb[1] & 0x1f);
949de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
950d6e0175cSChristoph Hellwig 		}
951a85d667eSBart Van Assche 		size = get_unaligned_be32(&cdb[10]);
952d6e0175cSChristoph Hellwig 		break;
953d6e0175cSChristoph Hellwig 	case SYNCHRONIZE_CACHE:
954d6e0175cSChristoph Hellwig 	case SYNCHRONIZE_CACHE_16:
955d6e0175cSChristoph Hellwig 		if (cdb[0] == SYNCHRONIZE_CACHE) {
956d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_10(cdb);
957d6e0175cSChristoph Hellwig 			cmd->t_task_lba = transport_lba_32(cdb);
958d6e0175cSChristoph Hellwig 		} else {
959d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_16(cdb);
960d6e0175cSChristoph Hellwig 			cmd->t_task_lba = transport_lba_64(cdb);
961d6e0175cSChristoph Hellwig 		}
9626ef31dc7SChristophe Vu-Brugier 		if (ops->execute_sync_cache) {
963ad67f0d9SChristoph Hellwig 			cmd->execute_cmd = ops->execute_sync_cache;
9646ef31dc7SChristophe Vu-Brugier 			goto check_lba;
9656ef31dc7SChristophe Vu-Brugier 		}
9666ef31dc7SChristophe Vu-Brugier 		size = 0;
9676ef31dc7SChristophe Vu-Brugier 		cmd->execute_cmd = sbc_emulate_noop;
968d6e0175cSChristoph Hellwig 		break;
969d6e0175cSChristoph Hellwig 	case UNMAP:
97014150a6bSChristoph Hellwig 		if (!ops->execute_unmap)
971de103c93SChristoph Hellwig 			return TCM_UNSUPPORTED_SCSI_OPCODE;
97214150a6bSChristoph Hellwig 
97361fdb4acSNicholas Bellinger 		if (!dev->dev_attrib.emulate_tpu) {
97461fdb4acSNicholas Bellinger 			pr_err("Got UNMAP, but backend device has"
97561fdb4acSNicholas Bellinger 			       " emulate_tpu disabled\n");
97661fdb4acSNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
97761fdb4acSNicholas Bellinger 		}
9781fd032eeSChristoph Hellwig 		size = get_unaligned_be16(&cdb[7]);
97962e46942SChristoph Hellwig 		cmd->execute_cmd = sbc_execute_unmap;
980d6e0175cSChristoph Hellwig 		break;
981d6e0175cSChristoph Hellwig 	case WRITE_SAME_16:
982d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
983d6e0175cSChristoph Hellwig 		if (!sectors) {
984d6e0175cSChristoph Hellwig 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
985de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
986d6e0175cSChristoph Hellwig 		}
987d6e0175cSChristoph Hellwig 
9881fd032eeSChristoph Hellwig 		size = sbc_get_size(cmd, 1);
989d6e0175cSChristoph Hellwig 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
990d6e0175cSChristoph Hellwig 
9916d8e7e7cSDmitry Bogdanov 		ret = sbc_setup_write_same(cmd, cdb[1], ops);
9926b64e1feSDan Carpenter 		if (ret)
993cd063befSNicholas Bellinger 			return ret;
994d6e0175cSChristoph Hellwig 		break;
995d6e0175cSChristoph Hellwig 	case WRITE_SAME:
996d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
997d6e0175cSChristoph Hellwig 		if (!sectors) {
998d6e0175cSChristoph Hellwig 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
999de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
1000d6e0175cSChristoph Hellwig 		}
1001d6e0175cSChristoph Hellwig 
10021fd032eeSChristoph Hellwig 		size = sbc_get_size(cmd, 1);
1003d6e0175cSChristoph Hellwig 		cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
1004d6e0175cSChristoph Hellwig 
1005d6e0175cSChristoph Hellwig 		/*
1006d6e0175cSChristoph Hellwig 		 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
1007d6e0175cSChristoph Hellwig 		 * of byte 1 bit 3 UNMAP instead of original reserved field
1008d6e0175cSChristoph Hellwig 		 */
10096d8e7e7cSDmitry Bogdanov 		ret = sbc_setup_write_same(cmd, cdb[1], ops);
10106b64e1feSDan Carpenter 		if (ret)
1011cd063befSNicholas Bellinger 			return ret;
1012d6e0175cSChristoph Hellwig 		break;
1013d6e0175cSChristoph Hellwig 	case VERIFY:
101413603685SMax Lohrmann 	case VERIFY_16:
1015984a9d4cSNicholas Bellinger 		size = 0;
1016984a9d4cSNicholas Bellinger 		if (cdb[0] == VERIFY) {
1017984a9d4cSNicholas Bellinger 			sectors = transport_get_sectors_10(cdb);
1018984a9d4cSNicholas Bellinger 			cmd->t_task_lba = transport_lba_32(cdb);
1019984a9d4cSNicholas Bellinger 		} else {
1020984a9d4cSNicholas Bellinger 			sectors = transport_get_sectors_16(cdb);
1021984a9d4cSNicholas Bellinger 			cmd->t_task_lba = transport_lba_64(cdb);
1022984a9d4cSNicholas Bellinger 		}
10231920ed61SNicholas Bellinger 		cmd->execute_cmd = sbc_emulate_noop;
1024c52716deSChristophe Vu-Brugier 		goto check_lba;
10251a1ff38cSBernhard Kohl 	case REZERO_UNIT:
10261a1ff38cSBernhard Kohl 	case SEEK_6:
10271a1ff38cSBernhard Kohl 	case SEEK_10:
10281a1ff38cSBernhard Kohl 		/*
10291a1ff38cSBernhard Kohl 		 * There are still clients out there which use these old SCSI-2
10301a1ff38cSBernhard Kohl 		 * commands. This mainly happens when running VMs with legacy
10311a1ff38cSBernhard Kohl 		 * guest systems, connected via SCSI command pass-through to
10321a1ff38cSBernhard Kohl 		 * iSCSI targets. Make them happy and return status GOOD.
10331a1ff38cSBernhard Kohl 		 */
10341a1ff38cSBernhard Kohl 		size = 0;
10351a1ff38cSBernhard Kohl 		cmd->execute_cmd = sbc_emulate_noop;
10361a1ff38cSBernhard Kohl 		break;
103745182ed5SBrian Bunker 	case START_STOP:
103845182ed5SBrian Bunker 		size = 0;
103945182ed5SBrian Bunker 		cmd->execute_cmd = sbc_emulate_startstop;
104045182ed5SBrian Bunker 		break;
1041d6e0175cSChristoph Hellwig 	default:
10421fd032eeSChristoph Hellwig 		ret = spc_parse_cdb(cmd, &size);
1043d6e0175cSChristoph Hellwig 		if (ret)
1044d6e0175cSChristoph Hellwig 			return ret;
1045d6e0175cSChristoph Hellwig 	}
1046d6e0175cSChristoph Hellwig 
1047d6e0175cSChristoph Hellwig 	/* reject any command that we don't have a handler for */
104820959c4bSAndy Grover 	if (!cmd->execute_cmd)
1049de103c93SChristoph Hellwig 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1050d6e0175cSChristoph Hellwig 
1051d6e0175cSChristoph Hellwig 	if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
10521fd032eeSChristoph Hellwig 		unsigned long long end_lba;
10536ef31dc7SChristophe Vu-Brugier check_lba:
10541fd032eeSChristoph Hellwig 		end_lba = dev->transport->get_blocks(dev) + 1;
1055aa179935SNicholas Bellinger 		if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
1056aa179935SNicholas Bellinger 		    ((cmd->t_task_lba + sectors) > end_lba)) {
10571fd032eeSChristoph Hellwig 			pr_err("cmd exceeds last lba %llu "
10581fd032eeSChristoph Hellwig 				"(lba %llu, sectors %u)\n",
10591fd032eeSChristoph Hellwig 				end_lba, cmd->t_task_lba, sectors);
106009ceadc7SRoland Dreier 			return TCM_ADDRESS_OUT_OF_RANGE;
1061d6e0175cSChristoph Hellwig 		}
1062d6e0175cSChristoph Hellwig 
106368ff9b9bSNicholas Bellinger 		if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE))
10641fd032eeSChristoph Hellwig 			size = sbc_get_size(cmd, sectors);
10651fd032eeSChristoph Hellwig 	}
10661fd032eeSChristoph Hellwig 
1067de103c93SChristoph Hellwig 	return target_cmd_size_check(cmd, size);
1068d6e0175cSChristoph Hellwig }
1069d6e0175cSChristoph Hellwig EXPORT_SYMBOL(sbc_parse_cdb);
10706f23ac8aSChristoph Hellwig 
sbc_get_device_type(struct se_device * dev)10716f23ac8aSChristoph Hellwig u32 sbc_get_device_type(struct se_device *dev)
10726f23ac8aSChristoph Hellwig {
10736f23ac8aSChristoph Hellwig 	return TYPE_DISK;
10746f23ac8aSChristoph Hellwig }
10756f23ac8aSChristoph Hellwig EXPORT_SYMBOL(sbc_get_device_type);
107686d71829SAsias He 
107762e46942SChristoph Hellwig static sense_reason_t
sbc_execute_unmap(struct se_cmd * cmd)107862e46942SChristoph Hellwig sbc_execute_unmap(struct se_cmd *cmd)
107986d71829SAsias He {
1080*0217da08SMike Christie 	struct exec_cmd_ops *ops = cmd->protocol_data;
108186d71829SAsias He 	struct se_device *dev = cmd->se_dev;
108286d71829SAsias He 	unsigned char *buf, *ptr = NULL;
108386d71829SAsias He 	sector_t lba;
108486d71829SAsias He 	int size;
108586d71829SAsias He 	u32 range;
108686d71829SAsias He 	sense_reason_t ret = 0;
108786d71829SAsias He 	int dl, bd_dl;
108886d71829SAsias He 
108986d71829SAsias He 	/* We never set ANC_SUP */
109086d71829SAsias He 	if (cmd->t_task_cdb[1])
109186d71829SAsias He 		return TCM_INVALID_CDB_FIELD;
109286d71829SAsias He 
109386d71829SAsias He 	if (cmd->data_length == 0) {
109486d71829SAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
109586d71829SAsias He 		return 0;
109686d71829SAsias He 	}
109786d71829SAsias He 
109886d71829SAsias He 	if (cmd->data_length < 8) {
109986d71829SAsias He 		pr_warn("UNMAP parameter list length %u too small\n",
110086d71829SAsias He 			cmd->data_length);
110186d71829SAsias He 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
110286d71829SAsias He 	}
110386d71829SAsias He 
110486d71829SAsias He 	buf = transport_kmap_data_sg(cmd);
110586d71829SAsias He 	if (!buf)
110686d71829SAsias He 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
110786d71829SAsias He 
110886d71829SAsias He 	dl = get_unaligned_be16(&buf[0]);
110986d71829SAsias He 	bd_dl = get_unaligned_be16(&buf[2]);
111086d71829SAsias He 
111186d71829SAsias He 	size = cmd->data_length - 8;
111286d71829SAsias He 	if (bd_dl > size)
111386d71829SAsias He 		pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
111486d71829SAsias He 			cmd->data_length, bd_dl);
111586d71829SAsias He 	else
111686d71829SAsias He 		size = bd_dl;
111786d71829SAsias He 
111886d71829SAsias He 	if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
111986d71829SAsias He 		ret = TCM_INVALID_PARAMETER_LIST;
112086d71829SAsias He 		goto err;
112186d71829SAsias He 	}
112286d71829SAsias He 
112386d71829SAsias He 	/* First UNMAP block descriptor starts at 8 byte offset */
112486d71829SAsias He 	ptr = &buf[8];
112586d71829SAsias He 	pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
112686d71829SAsias He 		" ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
112786d71829SAsias He 
112886d71829SAsias He 	while (size >= 16) {
112986d71829SAsias He 		lba = get_unaligned_be64(&ptr[0]);
113086d71829SAsias He 		range = get_unaligned_be32(&ptr[8]);
113186d71829SAsias He 		pr_debug("UNMAP: Using lba: %llu and range: %u\n",
113286d71829SAsias He 				 (unsigned long long)lba, range);
113386d71829SAsias He 
113486d71829SAsias He 		if (range > dev->dev_attrib.max_unmap_lba_count) {
113586d71829SAsias He 			ret = TCM_INVALID_PARAMETER_LIST;
113686d71829SAsias He 			goto err;
113786d71829SAsias He 		}
113886d71829SAsias He 
113986d71829SAsias He 		if (lba + range > dev->transport->get_blocks(dev) + 1) {
114086d71829SAsias He 			ret = TCM_ADDRESS_OUT_OF_RANGE;
114186d71829SAsias He 			goto err;
114286d71829SAsias He 		}
114386d71829SAsias He 
11449960f851SAndrei Vagin 		if (range) {
114562e46942SChristoph Hellwig 			ret = ops->execute_unmap(cmd, lba, range);
114686d71829SAsias He 			if (ret)
114786d71829SAsias He 				goto err;
11489960f851SAndrei Vagin 		}
114986d71829SAsias He 
115086d71829SAsias He 		ptr += 16;
115186d71829SAsias He 		size -= 16;
115286d71829SAsias He 	}
115386d71829SAsias He 
115486d71829SAsias He err:
115586d71829SAsias He 	transport_kunmap_data_sg(cmd);
115686d71829SAsias He 	if (!ret)
115714b40c1eSHannes Reinecke 		target_complete_cmd(cmd, SAM_STAT_GOOD);
115886d71829SAsias He 	return ret;
115986d71829SAsias He }
116041861fa8SNicholas Bellinger 
116166a3d5bcSNicholas Bellinger void
sbc_dif_generate(struct se_cmd * cmd)116266a3d5bcSNicholas Bellinger sbc_dif_generate(struct se_cmd *cmd)
116366a3d5bcSNicholas Bellinger {
116466a3d5bcSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
1165fe052a18SSagi Grimberg 	struct t10_pi_tuple *sdt;
116618213afbSAkinobu Mita 	struct scatterlist *dsg = cmd->t_data_sg, *psg;
116766a3d5bcSNicholas Bellinger 	sector_t sector = cmd->t_task_lba;
116866a3d5bcSNicholas Bellinger 	void *daddr, *paddr;
116966a3d5bcSNicholas Bellinger 	int i, j, offset = 0;
117018213afbSAkinobu Mita 	unsigned int block_size = dev->dev_attrib.block_size;
117166a3d5bcSNicholas Bellinger 
117218213afbSAkinobu Mita 	for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
117318213afbSAkinobu Mita 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
117466a3d5bcSNicholas Bellinger 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
117566a3d5bcSNicholas Bellinger 
117618213afbSAkinobu Mita 		for (j = 0; j < psg->length;
1177fe052a18SSagi Grimberg 				j += sizeof(*sdt)) {
117818213afbSAkinobu Mita 			__u16 crc;
117918213afbSAkinobu Mita 			unsigned int avail;
118066a3d5bcSNicholas Bellinger 
118118213afbSAkinobu Mita 			if (offset >= dsg->length) {
118218213afbSAkinobu Mita 				offset -= dsg->length;
118318213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
118418213afbSAkinobu Mita 				dsg = sg_next(dsg);
118518213afbSAkinobu Mita 				if (!dsg) {
118618213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
118718213afbSAkinobu Mita 					return;
118818213afbSAkinobu Mita 				}
118918213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
119066a3d5bcSNicholas Bellinger 			}
119166a3d5bcSNicholas Bellinger 
119218213afbSAkinobu Mita 			sdt = paddr + j;
119318213afbSAkinobu Mita 			avail = min(block_size, dsg->length - offset);
119418213afbSAkinobu Mita 			crc = crc_t10dif(daddr + offset, avail);
119518213afbSAkinobu Mita 			if (avail < block_size) {
119618213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
119718213afbSAkinobu Mita 				dsg = sg_next(dsg);
119818213afbSAkinobu Mita 				if (!dsg) {
119918213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
120018213afbSAkinobu Mita 					return;
120118213afbSAkinobu Mita 				}
120218213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
120318213afbSAkinobu Mita 				offset = block_size - avail;
120418213afbSAkinobu Mita 				crc = crc_t10dif_update(crc, daddr, offset);
120518213afbSAkinobu Mita 			} else {
120618213afbSAkinobu Mita 				offset += block_size;
120718213afbSAkinobu Mita 			}
120818213afbSAkinobu Mita 
120918213afbSAkinobu Mita 			sdt->guard_tag = cpu_to_be16(crc);
1210823ddd87SNicholas Bellinger 			if (cmd->prot_type == TARGET_DIF_TYPE1_PROT)
121166a3d5bcSNicholas Bellinger 				sdt->ref_tag = cpu_to_be32(sector & 0xffffffff);
121266a3d5bcSNicholas Bellinger 			sdt->app_tag = 0;
121366a3d5bcSNicholas Bellinger 
12146ae50408SNicholas Bellinger 			pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x"
121566a3d5bcSNicholas Bellinger 				 " app_tag: 0x%04x ref_tag: %u\n",
12166ae50408SNicholas Bellinger 				 (cmd->data_direction == DMA_TO_DEVICE) ?
12176ae50408SNicholas Bellinger 				 "WRITE" : "READ", (unsigned long long)sector,
12186ae50408SNicholas Bellinger 				 sdt->guard_tag, sdt->app_tag,
12196ae50408SNicholas Bellinger 				 be32_to_cpu(sdt->ref_tag));
122066a3d5bcSNicholas Bellinger 
122166a3d5bcSNicholas Bellinger 			sector++;
122266a3d5bcSNicholas Bellinger 		}
122366a3d5bcSNicholas Bellinger 
122418213afbSAkinobu Mita 		kunmap_atomic(daddr - dsg->offset);
122518213afbSAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
122666a3d5bcSNicholas Bellinger 	}
122766a3d5bcSNicholas Bellinger }
122866a3d5bcSNicholas Bellinger 
122941861fa8SNicholas Bellinger static sense_reason_t
sbc_dif_v1_verify(struct se_cmd * cmd,struct t10_pi_tuple * sdt,__u16 crc,sector_t sector,unsigned int ei_lba)1230fe052a18SSagi Grimberg sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt,
123118213afbSAkinobu Mita 		  __u16 crc, sector_t sector, unsigned int ei_lba)
123241861fa8SNicholas Bellinger {
123341861fa8SNicholas Bellinger 	__be16 csum;
123441861fa8SNicholas Bellinger 
1235d7a463b0SNicholas Bellinger 	if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
1236d7a463b0SNicholas Bellinger 		goto check_ref;
1237d7a463b0SNicholas Bellinger 
123818213afbSAkinobu Mita 	csum = cpu_to_be16(crc);
123941861fa8SNicholas Bellinger 
124041861fa8SNicholas Bellinger 	if (sdt->guard_tag != csum) {
124141861fa8SNicholas Bellinger 		pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x"
124241861fa8SNicholas Bellinger 			" csum 0x%04x\n", (unsigned long long)sector,
124341861fa8SNicholas Bellinger 			be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
124441861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
124541861fa8SNicholas Bellinger 	}
124641861fa8SNicholas Bellinger 
1247d7a463b0SNicholas Bellinger check_ref:
1248d7a463b0SNicholas Bellinger 	if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG))
1249d7a463b0SNicholas Bellinger 		return 0;
1250d7a463b0SNicholas Bellinger 
1251823ddd87SNicholas Bellinger 	if (cmd->prot_type == TARGET_DIF_TYPE1_PROT &&
125241861fa8SNicholas Bellinger 	    be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
125341861fa8SNicholas Bellinger 		pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x"
125441861fa8SNicholas Bellinger 		       " sector MSB: 0x%08x\n", (unsigned long long)sector,
125541861fa8SNicholas Bellinger 		       be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff));
125641861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
125741861fa8SNicholas Bellinger 	}
125841861fa8SNicholas Bellinger 
1259823ddd87SNicholas Bellinger 	if (cmd->prot_type == TARGET_DIF_TYPE2_PROT &&
126041861fa8SNicholas Bellinger 	    be32_to_cpu(sdt->ref_tag) != ei_lba) {
126141861fa8SNicholas Bellinger 		pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x"
126241861fa8SNicholas Bellinger 		       " ei_lba: 0x%08x\n", (unsigned long long)sector,
126341861fa8SNicholas Bellinger 			be32_to_cpu(sdt->ref_tag), ei_lba);
126441861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
126541861fa8SNicholas Bellinger 	}
126641861fa8SNicholas Bellinger 
126741861fa8SNicholas Bellinger 	return 0;
126841861fa8SNicholas Bellinger }
126941861fa8SNicholas Bellinger 
sbc_dif_copy_prot(struct se_cmd * cmd,unsigned int sectors,bool read,struct scatterlist * sg,int sg_off)1270f75b6faeSSagi Grimberg void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read,
127141861fa8SNicholas Bellinger 		       struct scatterlist *sg, int sg_off)
127241861fa8SNicholas Bellinger {
127341861fa8SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
127441861fa8SNicholas Bellinger 	struct scatterlist *psg;
127541861fa8SNicholas Bellinger 	void *paddr, *addr;
127641861fa8SNicholas Bellinger 	unsigned int i, len, left;
127710762e80SNicholas Bellinger 	unsigned int offset = sg_off;
127841861fa8SNicholas Bellinger 
127938b57f82SNicholas Bellinger 	if (!sg)
128038b57f82SNicholas Bellinger 		return;
128138b57f82SNicholas Bellinger 
128241861fa8SNicholas Bellinger 	left = sectors * dev->prot_length;
128341861fa8SNicholas Bellinger 
128441861fa8SNicholas Bellinger 	for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
128516c0ae02SSagi Grimberg 		unsigned int psg_len, copied = 0;
128641861fa8SNicholas Bellinger 
128716c0ae02SSagi Grimberg 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
128816c0ae02SSagi Grimberg 		psg_len = min(left, psg->length);
128916c0ae02SSagi Grimberg 		while (psg_len) {
129016c0ae02SSagi Grimberg 			len = min(psg_len, sg->length - offset);
129116c0ae02SSagi Grimberg 			addr = kmap_atomic(sg_page(sg)) + sg->offset + offset;
129216c0ae02SSagi Grimberg 
129316c0ae02SSagi Grimberg 			if (read)
129416c0ae02SSagi Grimberg 				memcpy(paddr + copied, addr, len);
129516c0ae02SSagi Grimberg 			else
129616c0ae02SSagi Grimberg 				memcpy(addr, paddr + copied, len);
129716c0ae02SSagi Grimberg 
129816c0ae02SSagi Grimberg 			left -= len;
129916c0ae02SSagi Grimberg 			offset += len;
130016c0ae02SSagi Grimberg 			copied += len;
130116c0ae02SSagi Grimberg 			psg_len -= len;
130216c0ae02SSagi Grimberg 
130357636388SAkinobu Mita 			kunmap_atomic(addr - sg->offset - offset);
130457636388SAkinobu Mita 
1305d6a65fdcSSagi Grimberg 			if (offset >= sg->length) {
1306d6a65fdcSSagi Grimberg 				sg = sg_next(sg);
1307d6a65fdcSSagi Grimberg 				offset = 0;
1308d6a65fdcSSagi Grimberg 			}
130941861fa8SNicholas Bellinger 		}
131057636388SAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
131116c0ae02SSagi Grimberg 	}
131241861fa8SNicholas Bellinger }
1313f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_copy_prot);
131441861fa8SNicholas Bellinger 
131541861fa8SNicholas Bellinger sense_reason_t
sbc_dif_verify(struct se_cmd * cmd,sector_t start,unsigned int sectors,unsigned int ei_lba,struct scatterlist * psg,int psg_off)1316f75b6faeSSagi Grimberg sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors,
1317414e4627SSagi Grimberg 	       unsigned int ei_lba, struct scatterlist *psg, int psg_off)
131841861fa8SNicholas Bellinger {
131941861fa8SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
1320fe052a18SSagi Grimberg 	struct t10_pi_tuple *sdt;
132118213afbSAkinobu Mita 	struct scatterlist *dsg = cmd->t_data_sg;
132241861fa8SNicholas Bellinger 	sector_t sector = start;
132341861fa8SNicholas Bellinger 	void *daddr, *paddr;
132418213afbSAkinobu Mita 	int i;
132541861fa8SNicholas Bellinger 	sense_reason_t rc;
132618213afbSAkinobu Mita 	int dsg_off = 0;
132718213afbSAkinobu Mita 	unsigned int block_size = dev->dev_attrib.block_size;
132841861fa8SNicholas Bellinger 
132918213afbSAkinobu Mita 	for (; psg && sector < start + sectors; psg = sg_next(psg)) {
133018213afbSAkinobu Mita 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
133141861fa8SNicholas Bellinger 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
133241861fa8SNicholas Bellinger 
133318213afbSAkinobu Mita 		for (i = psg_off; i < psg->length &&
133418213afbSAkinobu Mita 				sector < start + sectors;
1335fe052a18SSagi Grimberg 				i += sizeof(*sdt)) {
133618213afbSAkinobu Mita 			__u16 crc;
133718213afbSAkinobu Mita 			unsigned int avail;
133841861fa8SNicholas Bellinger 
133918213afbSAkinobu Mita 			if (dsg_off >= dsg->length) {
134018213afbSAkinobu Mita 				dsg_off -= dsg->length;
134118213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
134218213afbSAkinobu Mita 				dsg = sg_next(dsg);
134318213afbSAkinobu Mita 				if (!dsg) {
1344414e4627SSagi Grimberg 					kunmap_atomic(paddr - psg->offset);
134541861fa8SNicholas Bellinger 					return 0;
134641861fa8SNicholas Bellinger 				}
134741861fa8SNicholas Bellinger 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
134841861fa8SNicholas Bellinger 			}
134941861fa8SNicholas Bellinger 
135018213afbSAkinobu Mita 			sdt = paddr + i;
135141861fa8SNicholas Bellinger 
135241861fa8SNicholas Bellinger 			pr_debug("DIF READ sector: %llu guard_tag: 0x%04x"
135341861fa8SNicholas Bellinger 				 " app_tag: 0x%04x ref_tag: %u\n",
135441861fa8SNicholas Bellinger 				 (unsigned long long)sector, sdt->guard_tag,
135541861fa8SNicholas Bellinger 				 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
135641861fa8SNicholas Bellinger 
1357128b6f9fSDmitry Monakhov 			if (sdt->app_tag == T10_PI_APP_ESCAPE) {
135818213afbSAkinobu Mita 				dsg_off += block_size;
135918213afbSAkinobu Mita 				goto next;
136041861fa8SNicholas Bellinger 			}
136141861fa8SNicholas Bellinger 
136218213afbSAkinobu Mita 			avail = min(block_size, dsg->length - dsg_off);
136318213afbSAkinobu Mita 			crc = crc_t10dif(daddr + dsg_off, avail);
136418213afbSAkinobu Mita 			if (avail < block_size) {
1365414e4627SSagi Grimberg 				kunmap_atomic(daddr - dsg->offset);
136618213afbSAkinobu Mita 				dsg = sg_next(dsg);
136718213afbSAkinobu Mita 				if (!dsg) {
136818213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
136918213afbSAkinobu Mita 					return 0;
137018213afbSAkinobu Mita 				}
137118213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
137218213afbSAkinobu Mita 				dsg_off = block_size - avail;
137318213afbSAkinobu Mita 				crc = crc_t10dif_update(crc, daddr, dsg_off);
137418213afbSAkinobu Mita 			} else {
137518213afbSAkinobu Mita 				dsg_off += block_size;
137618213afbSAkinobu Mita 			}
137718213afbSAkinobu Mita 
137818213afbSAkinobu Mita 			rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba);
137941861fa8SNicholas Bellinger 			if (rc) {
138018213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
138118213afbSAkinobu Mita 				kunmap_atomic(paddr - psg->offset);
13828dd992fbSDavid Disseldorp 				cmd->sense_info = sector;
138341861fa8SNicholas Bellinger 				return rc;
138441861fa8SNicholas Bellinger 			}
138518213afbSAkinobu Mita next:
138641861fa8SNicholas Bellinger 			sector++;
138741861fa8SNicholas Bellinger 			ei_lba++;
138841861fa8SNicholas Bellinger 		}
138941861fa8SNicholas Bellinger 
139018213afbSAkinobu Mita 		psg_off = 0;
1391414e4627SSagi Grimberg 		kunmap_atomic(daddr - dsg->offset);
139218213afbSAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
139341861fa8SNicholas Bellinger 	}
139441861fa8SNicholas Bellinger 
139541861fa8SNicholas Bellinger 	return 0;
139641861fa8SNicholas Bellinger }
1397f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_verify);
1398