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
28*6d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *, struct se_cmd *, unsigned char, u32, bool);
2962e46942SChristoph Hellwig static sense_reason_t sbc_execute_unmap(struct se_cmd *cmd);
30afd73f1bSNicholas Bellinger 
31afd73f1bSNicholas Bellinger static sense_reason_t
32de103c93SChristoph Hellwig sbc_emulate_readcapacity(struct se_cmd *cmd)
331fd032eeSChristoph Hellwig {
341fd032eeSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
358dc8632aSRoland Dreier 	unsigned char *cdb = cmd->t_task_cdb;
361fd032eeSChristoph Hellwig 	unsigned long long blocks_long = dev->transport->get_blocks(dev);
37a50da144SPaolo Bonzini 	unsigned char *rbuf;
38a50da144SPaolo Bonzini 	unsigned char buf[8];
391fd032eeSChristoph Hellwig 	u32 blocks;
401fd032eeSChristoph Hellwig 
418dc8632aSRoland Dreier 	/*
428dc8632aSRoland Dreier 	 * SBC-2 says:
438dc8632aSRoland Dreier 	 *   If the PMI bit is set to zero and the LOGICAL BLOCK
448dc8632aSRoland Dreier 	 *   ADDRESS field is not set to zero, the device server shall
458dc8632aSRoland Dreier 	 *   terminate the command with CHECK CONDITION status with
468dc8632aSRoland Dreier 	 *   the sense key set to ILLEGAL REQUEST and the additional
478dc8632aSRoland Dreier 	 *   sense code set to INVALID FIELD IN CDB.
488dc8632aSRoland Dreier 	 *
498dc8632aSRoland Dreier 	 * In SBC-3, these fields are obsolete, but some SCSI
508dc8632aSRoland Dreier 	 * compliance tests actually check this, so we might as well
518dc8632aSRoland Dreier 	 * follow SBC-2.
528dc8632aSRoland Dreier 	 */
538dc8632aSRoland Dreier 	if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5]))
548dc8632aSRoland Dreier 		return TCM_INVALID_CDB_FIELD;
558dc8632aSRoland Dreier 
561fd032eeSChristoph Hellwig 	if (blocks_long >= 0x00000000ffffffff)
571fd032eeSChristoph Hellwig 		blocks = 0xffffffff;
581fd032eeSChristoph Hellwig 	else
591fd032eeSChristoph Hellwig 		blocks = (u32)blocks_long;
601fd032eeSChristoph Hellwig 
61a85d667eSBart Van Assche 	put_unaligned_be32(blocks, &buf[0]);
62a85d667eSBart Van Assche 	put_unaligned_be32(dev->dev_attrib.block_size, &buf[4]);
631fd032eeSChristoph Hellwig 
64a50da144SPaolo Bonzini 	rbuf = transport_kmap_data_sg(cmd);
658b4b0dcbSNicholas Bellinger 	if (rbuf) {
66a50da144SPaolo Bonzini 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
671fd032eeSChristoph Hellwig 		transport_kunmap_data_sg(cmd);
688b4b0dcbSNicholas Bellinger 	}
691fd032eeSChristoph Hellwig 
7014b40c1eSHannes Reinecke 	target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 8);
711fd032eeSChristoph Hellwig 	return 0;
721fd032eeSChristoph Hellwig }
731fd032eeSChristoph Hellwig 
74de103c93SChristoph Hellwig static sense_reason_t
75de103c93SChristoph Hellwig sbc_emulate_readcapacity_16(struct se_cmd *cmd)
761fd032eeSChristoph Hellwig {
771fd032eeSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
782d335983SNicholas Bellinger 	struct se_session *sess = cmd->se_sess;
799ef5466eSNicholas Bellinger 	int pi_prot_type = dev->dev_attrib.pi_prot_type;
809ef5466eSNicholas Bellinger 
81a50da144SPaolo Bonzini 	unsigned char *rbuf;
82a50da144SPaolo Bonzini 	unsigned char buf[32];
831fd032eeSChristoph Hellwig 	unsigned long long blocks = dev->transport->get_blocks(dev);
841fd032eeSChristoph Hellwig 
85a50da144SPaolo Bonzini 	memset(buf, 0, sizeof(buf));
86a85d667eSBart Van Assche 	put_unaligned_be64(blocks, &buf[0]);
87a85d667eSBart Van Assche 	put_unaligned_be32(dev->dev_attrib.block_size, &buf[8]);
8856dac14cSNicholas Bellinger 	/*
8956dac14cSNicholas Bellinger 	 * Set P_TYPE and PROT_EN bits for DIF support
9056dac14cSNicholas Bellinger 	 */
912d335983SNicholas Bellinger 	if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
929ef5466eSNicholas Bellinger 		/*
939ef5466eSNicholas Bellinger 		 * Only override a device's pi_prot_type if no T10-PI is
949ef5466eSNicholas Bellinger 		 * available, and sess_prot_type has been explicitly enabled.
959ef5466eSNicholas Bellinger 		 */
969ef5466eSNicholas Bellinger 		if (!pi_prot_type)
979ef5466eSNicholas Bellinger 			pi_prot_type = sess->sess_prot_type;
989ef5466eSNicholas Bellinger 
999ef5466eSNicholas Bellinger 		if (pi_prot_type)
1009ef5466eSNicholas Bellinger 			buf[12] = (pi_prot_type - 1) << 1 | 0x1;
1012d335983SNicholas Bellinger 	}
1027f7caf6aSAndy Grover 
1037f7caf6aSAndy Grover 	if (dev->transport->get_lbppbe)
1047f7caf6aSAndy Grover 		buf[13] = dev->transport->get_lbppbe(dev) & 0x0f;
1057f7caf6aSAndy Grover 
1067f7caf6aSAndy Grover 	if (dev->transport->get_alignment_offset_lbas) {
1077f7caf6aSAndy Grover 		u16 lalba = dev->transport->get_alignment_offset_lbas(dev);
108a85d667eSBart Van Assche 
109a85d667eSBart Van Assche 		put_unaligned_be16(lalba, &buf[14]);
1107f7caf6aSAndy Grover 	}
1117f7caf6aSAndy Grover 
1121fd032eeSChristoph Hellwig 	/*
1131fd032eeSChristoph Hellwig 	 * Set Thin Provisioning Enable bit following sbc3r22 in section
1141fd032eeSChristoph Hellwig 	 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
1151fd032eeSChristoph Hellwig 	 */
116e6f41633SJamie Pocas 	if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) {
1177f7caf6aSAndy Grover 		buf[14] |= 0x80;
1181fd032eeSChristoph Hellwig 
119e6f41633SJamie Pocas 		/*
120e6f41633SJamie Pocas 		 * LBPRZ signifies that zeroes will be read back from an LBA after
121e6f41633SJamie Pocas 		 * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2)
122e6f41633SJamie Pocas 		 */
123e6f41633SJamie Pocas 		if (dev->dev_attrib.unmap_zeroes_data)
124e6f41633SJamie Pocas 			buf[14] |= 0x40;
125e6f41633SJamie Pocas 	}
126e6f41633SJamie Pocas 
127a50da144SPaolo Bonzini 	rbuf = transport_kmap_data_sg(cmd);
1288b4b0dcbSNicholas Bellinger 	if (rbuf) {
129a50da144SPaolo Bonzini 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
1301fd032eeSChristoph Hellwig 		transport_kunmap_data_sg(cmd);
1318b4b0dcbSNicholas Bellinger 	}
1321fd032eeSChristoph Hellwig 
13314b40c1eSHannes Reinecke 	target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 32);
1341fd032eeSChristoph Hellwig 	return 0;
1351fd032eeSChristoph Hellwig }
1361fd032eeSChristoph Hellwig 
13745182ed5SBrian Bunker static sense_reason_t
13845182ed5SBrian Bunker sbc_emulate_startstop(struct se_cmd *cmd)
13945182ed5SBrian Bunker {
14045182ed5SBrian Bunker 	unsigned char *cdb = cmd->t_task_cdb;
14145182ed5SBrian Bunker 
14245182ed5SBrian Bunker 	/*
14345182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
14445182ed5SBrian Bunker 	 * Immediate bit should be set since there is nothing to complete
14545182ed5SBrian Bunker 	 * POWER CONDITION MODIFIER 0h
14645182ed5SBrian Bunker 	 */
14745182ed5SBrian Bunker 	if (!(cdb[1] & 1) || cdb[2] || cdb[3])
14845182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
14945182ed5SBrian Bunker 
15045182ed5SBrian Bunker 	/*
15145182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
15245182ed5SBrian Bunker 	 * POWER CONDITION 0h START_VALID - process START and LOEJ
15345182ed5SBrian Bunker 	 */
15445182ed5SBrian Bunker 	if (cdb[4] >> 4 & 0xf)
15545182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
15645182ed5SBrian Bunker 
15745182ed5SBrian Bunker 	/*
15845182ed5SBrian Bunker 	 * See sbc3r36 section 5.25
15945182ed5SBrian Bunker 	 * LOEJ 0h - nothing to load or unload
16045182ed5SBrian Bunker 	 * START 1h - we are ready
16145182ed5SBrian Bunker 	 */
16245182ed5SBrian Bunker 	if (!(cdb[4] & 1) || (cdb[4] & 2) || (cdb[4] & 4))
16345182ed5SBrian Bunker 		return TCM_INVALID_CDB_FIELD;
16445182ed5SBrian Bunker 
16545182ed5SBrian Bunker 	target_complete_cmd(cmd, SAM_STAT_GOOD);
16645182ed5SBrian Bunker 	return 0;
16745182ed5SBrian Bunker }
16845182ed5SBrian Bunker 
169972b29c8SRoland Dreier sector_t sbc_get_write_same_sectors(struct se_cmd *cmd)
1701fd032eeSChristoph Hellwig {
1711fd032eeSChristoph Hellwig 	u32 num_blocks;
1721fd032eeSChristoph Hellwig 
1731fd032eeSChristoph Hellwig 	if (cmd->t_task_cdb[0] == WRITE_SAME)
1741fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
1751fd032eeSChristoph Hellwig 	else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
1761fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
1771fd032eeSChristoph Hellwig 	else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
1781fd032eeSChristoph Hellwig 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
1791fd032eeSChristoph Hellwig 
1801fd032eeSChristoph Hellwig 	/*
1811fd032eeSChristoph Hellwig 	 * Use the explicit range when non zero is supplied, otherwise calculate
1821fd032eeSChristoph Hellwig 	 * the remaining range based on ->get_blocks() - starting LBA.
1831fd032eeSChristoph Hellwig 	 */
1846f974e8cSChristoph Hellwig 	if (num_blocks)
1856f974e8cSChristoph Hellwig 		return num_blocks;
1861fd032eeSChristoph Hellwig 
1876f974e8cSChristoph Hellwig 	return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
1886f974e8cSChristoph Hellwig 		cmd->t_task_lba + 1;
1891fd032eeSChristoph Hellwig }
190972b29c8SRoland Dreier EXPORT_SYMBOL(sbc_get_write_same_sectors);
1911fd032eeSChristoph Hellwig 
192de103c93SChristoph Hellwig static sense_reason_t
193b753d643SChristoph Hellwig sbc_execute_write_same_unmap(struct se_cmd *cmd)
194b753d643SChristoph Hellwig {
195b753d643SChristoph Hellwig 	struct sbc_ops *ops = cmd->protocol_data;
196b753d643SChristoph Hellwig 	sector_t nolb = sbc_get_write_same_sectors(cmd);
197b753d643SChristoph Hellwig 	sense_reason_t ret;
198b753d643SChristoph Hellwig 
199b753d643SChristoph Hellwig 	if (nolb) {
200b753d643SChristoph Hellwig 		ret = ops->execute_unmap(cmd, cmd->t_task_lba, nolb);
201b753d643SChristoph Hellwig 		if (ret)
202b753d643SChristoph Hellwig 			return ret;
203b753d643SChristoph Hellwig 	}
204b753d643SChristoph Hellwig 
20514b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
206b753d643SChristoph Hellwig 	return 0;
207b753d643SChristoph Hellwig }
208b753d643SChristoph Hellwig 
209b753d643SChristoph Hellwig static sense_reason_t
2101920ed61SNicholas Bellinger sbc_emulate_noop(struct se_cmd *cmd)
2111a1ff38cSBernhard Kohl {
21214b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
2131a1ff38cSBernhard Kohl 	return 0;
2141a1ff38cSBernhard Kohl }
2151a1ff38cSBernhard Kohl 
216d6e0175cSChristoph Hellwig static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
217d6e0175cSChristoph Hellwig {
2180fd97ccfSChristoph Hellwig 	return cmd->se_dev->dev_attrib.block_size * sectors;
219d6e0175cSChristoph Hellwig }
220d6e0175cSChristoph Hellwig 
221d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_6(unsigned char *cdb)
222d6e0175cSChristoph Hellwig {
223d6e0175cSChristoph Hellwig 	/*
224d6e0175cSChristoph Hellwig 	 * Use 8-bit sector value.  SBC-3 says:
225d6e0175cSChristoph Hellwig 	 *
226d6e0175cSChristoph Hellwig 	 *   A TRANSFER LENGTH field set to zero specifies that 256
227d6e0175cSChristoph Hellwig 	 *   logical blocks shall be written.  Any other value
228d6e0175cSChristoph Hellwig 	 *   specifies the number of logical blocks that shall be
229d6e0175cSChristoph Hellwig 	 *   written.
230d6e0175cSChristoph Hellwig 	 */
231d6e0175cSChristoph Hellwig 	return cdb[4] ? : 256;
232d6e0175cSChristoph Hellwig }
233d6e0175cSChristoph Hellwig 
234d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_10(unsigned char *cdb)
235d6e0175cSChristoph Hellwig {
236a85d667eSBart Van Assche 	return get_unaligned_be16(&cdb[7]);
237d6e0175cSChristoph Hellwig }
238d6e0175cSChristoph Hellwig 
239d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_12(unsigned char *cdb)
240d6e0175cSChristoph Hellwig {
241a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[6]);
242d6e0175cSChristoph Hellwig }
243d6e0175cSChristoph Hellwig 
244d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_16(unsigned char *cdb)
245d6e0175cSChristoph Hellwig {
246a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[10]);
247d6e0175cSChristoph Hellwig }
248d6e0175cSChristoph Hellwig 
249d6e0175cSChristoph Hellwig /*
250d6e0175cSChristoph Hellwig  * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
251d6e0175cSChristoph Hellwig  */
252d6e0175cSChristoph Hellwig static inline u32 transport_get_sectors_32(unsigned char *cdb)
253d6e0175cSChristoph Hellwig {
254a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[28]);
255d6e0175cSChristoph Hellwig 
256d6e0175cSChristoph Hellwig }
257d6e0175cSChristoph Hellwig 
258d6e0175cSChristoph Hellwig static inline u32 transport_lba_21(unsigned char *cdb)
259d6e0175cSChristoph Hellwig {
260a85d667eSBart Van Assche 	return get_unaligned_be24(&cdb[1]) & 0x1fffff;
261d6e0175cSChristoph Hellwig }
262d6e0175cSChristoph Hellwig 
263d6e0175cSChristoph Hellwig static inline u32 transport_lba_32(unsigned char *cdb)
264d6e0175cSChristoph Hellwig {
265a85d667eSBart Van Assche 	return get_unaligned_be32(&cdb[2]);
266d6e0175cSChristoph Hellwig }
267d6e0175cSChristoph Hellwig 
268d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64(unsigned char *cdb)
269d6e0175cSChristoph Hellwig {
270a85d667eSBart Van Assche 	return get_unaligned_be64(&cdb[2]);
271d6e0175cSChristoph Hellwig }
272d6e0175cSChristoph Hellwig 
273d6e0175cSChristoph Hellwig /*
274d6e0175cSChristoph Hellwig  * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
275d6e0175cSChristoph Hellwig  */
276d6e0175cSChristoph Hellwig static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
277d6e0175cSChristoph Hellwig {
278a85d667eSBart Van Assche 	return get_unaligned_be64(&cdb[12]);
279d6e0175cSChristoph Hellwig }
280d6e0175cSChristoph Hellwig 
281cd063befSNicholas Bellinger static sense_reason_t
282*6d8e7e7cSDmitry Bogdanov sbc_setup_write_same(struct se_cmd *cmd, unsigned char flags, struct sbc_ops *ops)
283d6e0175cSChristoph Hellwig {
2848e575c50SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
2858e575c50SNicholas Bellinger 	sector_t end_lba = dev->transport->get_blocks(dev) + 1;
286972b29c8SRoland Dreier 	unsigned int sectors = sbc_get_write_same_sectors(cmd);
287afd73f1bSNicholas Bellinger 	sense_reason_t ret;
288773cbaf7SNicholas Bellinger 
289*6d8e7e7cSDmitry Bogdanov 	if ((flags & 0x04) || (flags & 0x02)) {
290d6e0175cSChristoph Hellwig 		pr_err("WRITE_SAME PBDATA and LBDATA"
291d6e0175cSChristoph Hellwig 			" bits not supported for Block Discard"
292d6e0175cSChristoph Hellwig 			" Emulation\n");
293cd063befSNicholas Bellinger 		return TCM_UNSUPPORTED_SCSI_OPCODE;
294d6e0175cSChristoph Hellwig 	}
295773cbaf7SNicholas Bellinger 	if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) {
296773cbaf7SNicholas Bellinger 		pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n",
297773cbaf7SNicholas Bellinger 			sectors, cmd->se_dev->dev_attrib.max_write_same_len);
298773cbaf7SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
299773cbaf7SNicholas Bellinger 	}
3008e575c50SNicholas Bellinger 	/*
3018e575c50SNicholas Bellinger 	 * Sanity check for LBA wrap and request past end of device.
3028e575c50SNicholas Bellinger 	 */
3038e575c50SNicholas Bellinger 	if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
3048e575c50SNicholas Bellinger 	    ((cmd->t_task_lba + sectors) > end_lba)) {
3058e575c50SNicholas Bellinger 		pr_err("WRITE_SAME exceeds last lba %llu (lba %llu, sectors %u)\n",
3068e575c50SNicholas Bellinger 		       (unsigned long long)end_lba, cmd->t_task_lba, sectors);
3078e575c50SNicholas Bellinger 		return TCM_ADDRESS_OUT_OF_RANGE;
3088e575c50SNicholas Bellinger 	}
3098e575c50SNicholas Bellinger 
3105cb770bfSRoland Dreier 	/* We always have ANC_SUP == 0 so setting ANCHOR is always an error */
311*6d8e7e7cSDmitry Bogdanov 	if (flags & 0x10) {
3125cb770bfSRoland Dreier 		pr_warn("WRITE SAME with ANCHOR not supported\n");
3135cb770bfSRoland Dreier 		return TCM_INVALID_CDB_FIELD;
3145cb770bfSRoland Dreier 	}
315d6e0175cSChristoph Hellwig 	/*
316cd063befSNicholas Bellinger 	 * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting
317cd063befSNicholas Bellinger 	 * translated into block discard requests within backend code.
318d6e0175cSChristoph Hellwig 	 */
319*6d8e7e7cSDmitry Bogdanov 	if (flags & 0x08) {
320b753d643SChristoph Hellwig 		if (!ops->execute_unmap)
321cd063befSNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
322d6e0175cSChristoph Hellwig 
323d0a91295SNicholas Bellinger 		if (!dev->dev_attrib.emulate_tpws) {
324d0a91295SNicholas Bellinger 			pr_err("Got WRITE_SAME w/ UNMAP=1, but backend device"
325d0a91295SNicholas Bellinger 			       " has emulate_tpws disabled\n");
326d0a91295SNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
327d0a91295SNicholas Bellinger 		}
328b753d643SChristoph Hellwig 		cmd->execute_cmd = sbc_execute_write_same_unmap;
329cd063befSNicholas Bellinger 		return 0;
330cd063befSNicholas Bellinger 	}
331cd063befSNicholas Bellinger 	if (!ops->execute_write_same)
332cd063befSNicholas Bellinger 		return TCM_UNSUPPORTED_SCSI_OPCODE;
333cd063befSNicholas Bellinger 
334*6d8e7e7cSDmitry Bogdanov 	ret = sbc_check_prot(dev, cmd, flags >> 5, sectors, true);
335afd73f1bSNicholas Bellinger 	if (ret)
336afd73f1bSNicholas Bellinger 		return ret;
337afd73f1bSNicholas Bellinger 
338cd063befSNicholas Bellinger 	cmd->execute_cmd = ops->execute_write_same;
339d6e0175cSChristoph Hellwig 	return 0;
340d6e0175cSChristoph Hellwig }
341d6e0175cSChristoph Hellwig 
342057085e5SNicholas Bellinger static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success,
343057085e5SNicholas Bellinger 					   int *post_ret)
344d6e0175cSChristoph Hellwig {
345d6e0175cSChristoph Hellwig 	unsigned char *buf, *addr;
346d6e0175cSChristoph Hellwig 	struct scatterlist *sg;
347d6e0175cSChristoph Hellwig 	unsigned int offset;
348a6b0133cSNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
349a6b0133cSNicholas Bellinger 	int i, count;
350aa73237dSBart Van Assche 
351aa73237dSBart Van Assche 	if (!success)
352aa73237dSBart Van Assche 		return 0;
353aa73237dSBart Van Assche 
354d6e0175cSChristoph Hellwig 	/*
355d6e0175cSChristoph Hellwig 	 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
356d6e0175cSChristoph Hellwig 	 *
357d6e0175cSChristoph Hellwig 	 * 1) read the specified logical block(s);
358d6e0175cSChristoph Hellwig 	 * 2) transfer logical blocks from the data-out buffer;
359d6e0175cSChristoph Hellwig 	 * 3) XOR the logical blocks transferred from the data-out buffer with
360d6e0175cSChristoph Hellwig 	 *    the logical blocks read, storing the resulting XOR data in a buffer;
361d6e0175cSChristoph Hellwig 	 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
362d6e0175cSChristoph Hellwig 	 *    blocks transferred from the data-out buffer; and
363d6e0175cSChristoph Hellwig 	 * 5) transfer the resulting XOR data to the data-in buffer.
364d6e0175cSChristoph Hellwig 	 */
365d6e0175cSChristoph Hellwig 	buf = kmalloc(cmd->data_length, GFP_KERNEL);
366d6e0175cSChristoph Hellwig 	if (!buf) {
367d6e0175cSChristoph Hellwig 		pr_err("Unable to allocate xor_callback buf\n");
368a6b0133cSNicholas Bellinger 		return TCM_OUT_OF_RESOURCES;
369d6e0175cSChristoph Hellwig 	}
370d6e0175cSChristoph Hellwig 	/*
371d6e0175cSChristoph Hellwig 	 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
372d6e0175cSChristoph Hellwig 	 * into the locally allocated *buf
373d6e0175cSChristoph Hellwig 	 */
374d6e0175cSChristoph Hellwig 	sg_copy_to_buffer(cmd->t_data_sg,
375d6e0175cSChristoph Hellwig 			  cmd->t_data_nents,
376d6e0175cSChristoph Hellwig 			  buf,
377d6e0175cSChristoph Hellwig 			  cmd->data_length);
378d6e0175cSChristoph Hellwig 
379d6e0175cSChristoph Hellwig 	/*
380d6e0175cSChristoph Hellwig 	 * Now perform the XOR against the BIDI read memory located at
381d6e0175cSChristoph Hellwig 	 * cmd->t_mem_bidi_list
382d6e0175cSChristoph Hellwig 	 */
383d6e0175cSChristoph Hellwig 
384d6e0175cSChristoph Hellwig 	offset = 0;
385d6e0175cSChristoph Hellwig 	for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
386d6e0175cSChristoph Hellwig 		addr = kmap_atomic(sg_page(sg));
387a6b0133cSNicholas Bellinger 		if (!addr) {
388a6b0133cSNicholas Bellinger 			ret = TCM_OUT_OF_RESOURCES;
389d6e0175cSChristoph Hellwig 			goto out;
390a6b0133cSNicholas Bellinger 		}
391d6e0175cSChristoph Hellwig 
392d6e0175cSChristoph Hellwig 		for (i = 0; i < sg->length; i++)
393d6e0175cSChristoph Hellwig 			*(addr + sg->offset + i) ^= *(buf + offset + i);
394d6e0175cSChristoph Hellwig 
395d6e0175cSChristoph Hellwig 		offset += sg->length;
396d6e0175cSChristoph Hellwig 		kunmap_atomic(addr);
397d6e0175cSChristoph Hellwig 	}
398d6e0175cSChristoph Hellwig 
399d6e0175cSChristoph Hellwig out:
400d6e0175cSChristoph Hellwig 	kfree(buf);
401a6b0133cSNicholas Bellinger 	return ret;
402d6e0175cSChristoph Hellwig }
403d6e0175cSChristoph Hellwig 
404a82a9538SNicholas Bellinger static sense_reason_t
405a82a9538SNicholas Bellinger sbc_execute_rw(struct se_cmd *cmd)
406a82a9538SNicholas Bellinger {
4077a971b1bSChristoph Hellwig 	struct sbc_ops *ops = cmd->protocol_data;
4087a971b1bSChristoph Hellwig 
4097a971b1bSChristoph Hellwig 	return ops->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents,
410a82a9538SNicholas Bellinger 			       cmd->data_direction);
411a82a9538SNicholas Bellinger }
412a82a9538SNicholas Bellinger 
413057085e5SNicholas Bellinger static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success,
414057085e5SNicholas Bellinger 					     int *post_ret)
41568ff9b9bSNicholas Bellinger {
41668ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
4179b2792c3SNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
41868ff9b9bSNicholas Bellinger 
419d8855c15SNicholas Bellinger 	spin_lock_irq(&cmd->t_state_lock);
420aa73237dSBart Van Assche 	if (success) {
421057085e5SNicholas Bellinger 		*post_ret = 1;
4229b2792c3SNicholas Bellinger 
4239b2792c3SNicholas Bellinger 		if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
4249b2792c3SNicholas Bellinger 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
425057085e5SNicholas Bellinger 	}
426d8855c15SNicholas Bellinger 	spin_unlock_irq(&cmd->t_state_lock);
427d8855c15SNicholas Bellinger 
42868ff9b9bSNicholas Bellinger 	/*
42968ff9b9bSNicholas Bellinger 	 * Unlock ->caw_sem originally obtained during sbc_compare_and_write()
43068ff9b9bSNicholas Bellinger 	 * before the original READ I/O submission.
43168ff9b9bSNicholas Bellinger 	 */
43268ff9b9bSNicholas Bellinger 	up(&dev->caw_sem);
43368ff9b9bSNicholas Bellinger 
4349b2792c3SNicholas Bellinger 	return ret;
43568ff9b9bSNicholas Bellinger }
43668ff9b9bSNicholas Bellinger 
437ab628b9fSDavid Disseldorp /*
438749c226cSDavid Disseldorp  * compare @cmp_len bytes of @read_sgl with @cmp_sgl. On miscompare, fill
439749c226cSDavid Disseldorp  * @miscmp_off and return TCM_MISCOMPARE_VERIFY.
440ab628b9fSDavid Disseldorp  */
441ab628b9fSDavid Disseldorp static sense_reason_t
442ab628b9fSDavid Disseldorp compare_and_write_do_cmp(struct scatterlist *read_sgl, unsigned int read_nents,
443ab628b9fSDavid Disseldorp 			 struct scatterlist *cmp_sgl, unsigned int cmp_nents,
444749c226cSDavid Disseldorp 			 unsigned int cmp_len, unsigned int *miscmp_off)
445ab628b9fSDavid Disseldorp {
446ab628b9fSDavid Disseldorp 	unsigned char *buf = NULL;
447ab628b9fSDavid Disseldorp 	struct scatterlist *sg;
448ab628b9fSDavid Disseldorp 	sense_reason_t ret;
449ab628b9fSDavid Disseldorp 	unsigned int offset;
450ab628b9fSDavid Disseldorp 	size_t rc;
4515cfb5b02SChaitanya Kulkarni 	int sg_cnt;
452ab628b9fSDavid Disseldorp 
453ab628b9fSDavid Disseldorp 	buf = kzalloc(cmp_len, GFP_KERNEL);
454ab628b9fSDavid Disseldorp 	if (!buf) {
455ab628b9fSDavid Disseldorp 		ret = TCM_OUT_OF_RESOURCES;
456ab628b9fSDavid Disseldorp 		goto out;
457ab628b9fSDavid Disseldorp 	}
458ab628b9fSDavid Disseldorp 
459ab628b9fSDavid Disseldorp 	rc = sg_copy_to_buffer(cmp_sgl, cmp_nents, buf, cmp_len);
460ab628b9fSDavid Disseldorp 	if (!rc) {
461ab628b9fSDavid Disseldorp 		pr_err("sg_copy_to_buffer() failed for compare_and_write\n");
462ab628b9fSDavid Disseldorp 		ret = TCM_OUT_OF_RESOURCES;
463ab628b9fSDavid Disseldorp 		goto out;
464ab628b9fSDavid Disseldorp 	}
465ab628b9fSDavid Disseldorp 	/*
466ab628b9fSDavid Disseldorp 	 * Compare SCSI READ payload against verify payload
467ab628b9fSDavid Disseldorp 	 */
468ab628b9fSDavid Disseldorp 	offset = 0;
469749c226cSDavid Disseldorp 	ret = TCM_NO_SENSE;
4705cfb5b02SChaitanya Kulkarni 	for_each_sg(read_sgl, sg, read_nents, sg_cnt) {
471ab628b9fSDavid Disseldorp 		unsigned int len = min(sg->length, cmp_len);
472ab628b9fSDavid Disseldorp 		unsigned char *addr = kmap_atomic(sg_page(sg));
473ab628b9fSDavid Disseldorp 
474ab628b9fSDavid Disseldorp 		if (memcmp(addr, buf + offset, len)) {
475749c226cSDavid Disseldorp 			unsigned int i;
476749c226cSDavid Disseldorp 
477749c226cSDavid Disseldorp 			for (i = 0; i < len && addr[i] == buf[offset + i]; i++)
478749c226cSDavid Disseldorp 				;
479749c226cSDavid Disseldorp 			*miscmp_off = offset + i;
480749c226cSDavid Disseldorp 			pr_warn("Detected MISCOMPARE at offset %u\n",
481749c226cSDavid Disseldorp 				*miscmp_off);
482ab628b9fSDavid Disseldorp 			ret = TCM_MISCOMPARE_VERIFY;
483ab628b9fSDavid Disseldorp 		}
484ab628b9fSDavid Disseldorp 		kunmap_atomic(addr);
485749c226cSDavid Disseldorp 		if (ret != TCM_NO_SENSE)
486749c226cSDavid Disseldorp 			goto out;
487ab628b9fSDavid Disseldorp 
488ab628b9fSDavid Disseldorp 		offset += len;
489ab628b9fSDavid Disseldorp 		cmp_len -= len;
490ab628b9fSDavid Disseldorp 		if (!cmp_len)
491ab628b9fSDavid Disseldorp 			break;
492ab628b9fSDavid Disseldorp 	}
493ab628b9fSDavid Disseldorp 	pr_debug("COMPARE AND WRITE read data matches compare data\n");
494ab628b9fSDavid Disseldorp out:
495ab628b9fSDavid Disseldorp 	kfree(buf);
496ab628b9fSDavid Disseldorp 	return ret;
497ab628b9fSDavid Disseldorp }
498ab628b9fSDavid Disseldorp 
499057085e5SNicholas Bellinger static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success,
500057085e5SNicholas Bellinger 						 int *post_ret)
50168ff9b9bSNicholas Bellinger {
50268ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
50381b6ca6dSBart Van Assche 	struct sg_table write_tbl = { };
504ab628b9fSDavid Disseldorp 	struct scatterlist *write_sg;
50568ff9b9bSNicholas Bellinger 	struct sg_mapping_iter m;
506ab628b9fSDavid Disseldorp 	unsigned int len;
50768ff9b9bSNicholas Bellinger 	unsigned int block_size = dev->dev_attrib.block_size;
508ab628b9fSDavid Disseldorp 	unsigned int compare_len = (cmd->t_task_nolb * block_size);
509749c226cSDavid Disseldorp 	unsigned int miscmp_off = 0;
51068ff9b9bSNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
511ab628b9fSDavid Disseldorp 	int i;
51268ff9b9bSNicholas Bellinger 
513cf6d1f09SNicholas Bellinger 	/*
514cf6d1f09SNicholas Bellinger 	 * Handle early failure in transport_generic_request_failure(),
515c8e63985SNicholas Bellinger 	 * which will not have taken ->caw_sem yet..
516cf6d1f09SNicholas Bellinger 	 */
517c8e63985SNicholas Bellinger 	if (!success && (!cmd->t_data_sg || !cmd->t_bidi_data_sg))
518cf6d1f09SNicholas Bellinger 		return TCM_NO_SENSE;
519db60df88SNicholas Bellinger 	/*
520c8e63985SNicholas Bellinger 	 * Handle special case for zero-length COMPARE_AND_WRITE
521c8e63985SNicholas Bellinger 	 */
522c8e63985SNicholas Bellinger 	if (!cmd->data_length)
523c8e63985SNicholas Bellinger 		goto out;
524c8e63985SNicholas Bellinger 	/*
525db60df88SNicholas Bellinger 	 * Immediately exit + release dev->caw_sem if command has already
526db60df88SNicholas Bellinger 	 * been failed with a non-zero SCSI status.
527db60df88SNicholas Bellinger 	 */
528db60df88SNicholas Bellinger 	if (cmd->scsi_status) {
529a71a5dc7SNicholas Bellinger 		pr_debug("compare_and_write_callback: non zero scsi_status:"
530db60df88SNicholas Bellinger 			" 0x%02x\n", cmd->scsi_status);
531a71a5dc7SNicholas Bellinger 		*post_ret = 1;
532a71a5dc7SNicholas Bellinger 		if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION)
533a71a5dc7SNicholas Bellinger 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
534db60df88SNicholas Bellinger 		goto out;
535db60df88SNicholas Bellinger 	}
536cf6d1f09SNicholas Bellinger 
537ab628b9fSDavid Disseldorp 	ret = compare_and_write_do_cmp(cmd->t_bidi_data_sg,
538ab628b9fSDavid Disseldorp 				       cmd->t_bidi_data_nents,
539ab628b9fSDavid Disseldorp 				       cmd->t_data_sg,
540ab628b9fSDavid Disseldorp 				       cmd->t_data_nents,
541749c226cSDavid Disseldorp 				       compare_len,
542749c226cSDavid Disseldorp 				       &miscmp_off);
543749c226cSDavid Disseldorp 	if (ret == TCM_MISCOMPARE_VERIFY) {
544749c226cSDavid Disseldorp 		/*
545749c226cSDavid Disseldorp 		 * SBC-4 r15: 5.3 COMPARE AND WRITE command
546749c226cSDavid Disseldorp 		 * In the sense data (see 4.18 and SPC-5) the offset from the
547749c226cSDavid Disseldorp 		 * start of the Data-Out Buffer to the first byte of data that
548749c226cSDavid Disseldorp 		 * was not equal shall be reported in the INFORMATION field.
549749c226cSDavid Disseldorp 		 */
550749c226cSDavid Disseldorp 		cmd->sense_info = miscmp_off;
551749c226cSDavid Disseldorp 		goto out;
552749c226cSDavid Disseldorp 	} else if (ret)
553a2890087SNicholas Bellinger 		goto out;
55468ff9b9bSNicholas Bellinger 
55581b6ca6dSBart Van Assche 	if (sg_alloc_table(&write_tbl, cmd->t_data_nents, GFP_KERNEL) < 0) {
55668ff9b9bSNicholas Bellinger 		pr_err("Unable to allocate compare_and_write sg\n");
55768ff9b9bSNicholas Bellinger 		ret = TCM_OUT_OF_RESOURCES;
55868ff9b9bSNicholas Bellinger 		goto out;
55968ff9b9bSNicholas Bellinger 	}
56081b6ca6dSBart Van Assche 	write_sg = write_tbl.sgl;
56168ff9b9bSNicholas Bellinger 
56268ff9b9bSNicholas Bellinger 	i = 0;
563ab628b9fSDavid Disseldorp 	len = compare_len;
56468ff9b9bSNicholas Bellinger 	sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG);
56568ff9b9bSNicholas Bellinger 	/*
56668ff9b9bSNicholas Bellinger 	 * Currently assumes NoLB=1 and SGLs are PAGE_SIZE..
56768ff9b9bSNicholas Bellinger 	 */
56868ff9b9bSNicholas Bellinger 	while (len) {
56968ff9b9bSNicholas Bellinger 		sg_miter_next(&m);
57068ff9b9bSNicholas Bellinger 
57168ff9b9bSNicholas Bellinger 		if (block_size < PAGE_SIZE) {
57268ff9b9bSNicholas Bellinger 			sg_set_page(&write_sg[i], m.page, block_size,
573d94e5a61SJan Engelhardt 				    m.piter.sg->offset + block_size);
57468ff9b9bSNicholas Bellinger 		} else {
57568ff9b9bSNicholas Bellinger 			sg_miter_next(&m);
57668ff9b9bSNicholas Bellinger 			sg_set_page(&write_sg[i], m.page, block_size,
577d94e5a61SJan Engelhardt 				    m.piter.sg->offset);
57868ff9b9bSNicholas Bellinger 		}
57968ff9b9bSNicholas Bellinger 		len -= block_size;
58068ff9b9bSNicholas Bellinger 		i++;
58168ff9b9bSNicholas Bellinger 	}
58268ff9b9bSNicholas Bellinger 	sg_miter_stop(&m);
58368ff9b9bSNicholas Bellinger 	/*
58468ff9b9bSNicholas Bellinger 	 * Save the original SGL + nents values before updating to new
58568ff9b9bSNicholas Bellinger 	 * assignments, to be released in transport_free_pages() ->
58668ff9b9bSNicholas Bellinger 	 * transport_reset_sgl_orig()
58768ff9b9bSNicholas Bellinger 	 */
58868ff9b9bSNicholas Bellinger 	cmd->t_data_sg_orig = cmd->t_data_sg;
58968ff9b9bSNicholas Bellinger 	cmd->t_data_sg = write_sg;
59068ff9b9bSNicholas Bellinger 	cmd->t_data_nents_orig = cmd->t_data_nents;
59168ff9b9bSNicholas Bellinger 	cmd->t_data_nents = 1;
59268ff9b9bSNicholas Bellinger 
59368d81f40SChristoph Hellwig 	cmd->sam_task_attr = TCM_HEAD_TAG;
59468ff9b9bSNicholas Bellinger 	cmd->transport_complete_callback = compare_and_write_post;
59568ff9b9bSNicholas Bellinger 	/*
59668ff9b9bSNicholas Bellinger 	 * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler
59768ff9b9bSNicholas Bellinger 	 * for submitting the adjusted SGL to write instance user-data.
59868ff9b9bSNicholas Bellinger 	 */
59968ff9b9bSNicholas Bellinger 	cmd->execute_cmd = sbc_execute_rw;
60068ff9b9bSNicholas Bellinger 
60168ff9b9bSNicholas Bellinger 	spin_lock_irq(&cmd->t_state_lock);
60268ff9b9bSNicholas Bellinger 	cmd->t_state = TRANSPORT_PROCESSING;
603fd5e64deSBart Van Assche 	cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
60468ff9b9bSNicholas Bellinger 	spin_unlock_irq(&cmd->t_state_lock);
60568ff9b9bSNicholas Bellinger 
606dff0ca9eSNicholas Bellinger 	__target_execute_cmd(cmd, false);
60768ff9b9bSNicholas Bellinger 
60868ff9b9bSNicholas Bellinger 	return ret;
60968ff9b9bSNicholas Bellinger 
61068ff9b9bSNicholas Bellinger out:
61168ff9b9bSNicholas Bellinger 	/*
61268ff9b9bSNicholas Bellinger 	 * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in
61368ff9b9bSNicholas Bellinger 	 * sbc_compare_and_write() before the original READ I/O submission.
61468ff9b9bSNicholas Bellinger 	 */
61568ff9b9bSNicholas Bellinger 	up(&dev->caw_sem);
61681b6ca6dSBart Van Assche 	sg_free_table(&write_tbl);
61768ff9b9bSNicholas Bellinger 	return ret;
61868ff9b9bSNicholas Bellinger }
61968ff9b9bSNicholas Bellinger 
62068ff9b9bSNicholas Bellinger static sense_reason_t
62168ff9b9bSNicholas Bellinger sbc_compare_and_write(struct se_cmd *cmd)
62268ff9b9bSNicholas Bellinger {
6237a971b1bSChristoph Hellwig 	struct sbc_ops *ops = cmd->protocol_data;
62468ff9b9bSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
62568ff9b9bSNicholas Bellinger 	sense_reason_t ret;
62668ff9b9bSNicholas Bellinger 	int rc;
62768ff9b9bSNicholas Bellinger 	/*
62868ff9b9bSNicholas Bellinger 	 * Submit the READ first for COMPARE_AND_WRITE to perform the
62968ff9b9bSNicholas Bellinger 	 * comparision using SGLs at cmd->t_bidi_data_sg..
63068ff9b9bSNicholas Bellinger 	 */
63168ff9b9bSNicholas Bellinger 	rc = down_interruptible(&dev->caw_sem);
632ee7619f2SNicholas Bellinger 	if (rc != 0) {
63368ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = NULL;
63468ff9b9bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
63568ff9b9bSNicholas Bellinger 	}
636b7191253SNicholas Bellinger 	/*
637b7191253SNicholas Bellinger 	 * Reset cmd->data_length to individual block_size in order to not
638b7191253SNicholas Bellinger 	 * confuse backend drivers that depend on this value matching the
639b7191253SNicholas Bellinger 	 * size of the I/O being submitted.
640b7191253SNicholas Bellinger 	 */
641b7191253SNicholas Bellinger 	cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size;
64268ff9b9bSNicholas Bellinger 
6437a971b1bSChristoph Hellwig 	ret = ops->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents,
64468ff9b9bSNicholas Bellinger 			      DMA_FROM_DEVICE);
64568ff9b9bSNicholas Bellinger 	if (ret) {
64668ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = NULL;
64768ff9b9bSNicholas Bellinger 		up(&dev->caw_sem);
64868ff9b9bSNicholas Bellinger 		return ret;
64968ff9b9bSNicholas Bellinger 	}
65068ff9b9bSNicholas Bellinger 	/*
65168ff9b9bSNicholas Bellinger 	 * Unlock of dev->caw_sem to occur in compare_and_write_callback()
65268ff9b9bSNicholas Bellinger 	 * upon MISCOMPARE, or in compare_and_write_done() upon completion
65368ff9b9bSNicholas Bellinger 	 * of WRITE instance user-data.
65468ff9b9bSNicholas Bellinger 	 */
65568ff9b9bSNicholas Bellinger 	return TCM_NO_SENSE;
65668ff9b9bSNicholas Bellinger }
65768ff9b9bSNicholas Bellinger 
65819f9361aSSagi Grimberg static int
65938b57f82SNicholas Bellinger sbc_set_prot_op_checks(u8 protect, bool fabric_prot, enum target_prot_type prot_type,
66019f9361aSSagi Grimberg 		       bool is_write, struct se_cmd *cmd)
66119f9361aSSagi Grimberg {
66219f9361aSSagi Grimberg 	if (is_write) {
66338b57f82SNicholas Bellinger 		cmd->prot_op = fabric_prot ? TARGET_PROT_DOUT_STRIP :
66438b57f82SNicholas Bellinger 			       protect ? TARGET_PROT_DOUT_PASS :
66519f9361aSSagi Grimberg 			       TARGET_PROT_DOUT_INSERT;
66619f9361aSSagi Grimberg 		switch (protect) {
66719f9361aSSagi Grimberg 		case 0x0:
66819f9361aSSagi Grimberg 		case 0x3:
66919f9361aSSagi Grimberg 			cmd->prot_checks = 0;
67019f9361aSSagi Grimberg 			break;
67119f9361aSSagi Grimberg 		case 0x1:
67219f9361aSSagi Grimberg 		case 0x5:
67319f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
67419f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
67519f9361aSSagi Grimberg 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
67619f9361aSSagi Grimberg 			break;
67719f9361aSSagi Grimberg 		case 0x2:
67819f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
67919f9361aSSagi Grimberg 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
68019f9361aSSagi Grimberg 			break;
68119f9361aSSagi Grimberg 		case 0x4:
68219f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
68319f9361aSSagi Grimberg 			break;
68419f9361aSSagi Grimberg 		default:
68519f9361aSSagi Grimberg 			pr_err("Unsupported protect field %d\n", protect);
68619f9361aSSagi Grimberg 			return -EINVAL;
68719f9361aSSagi Grimberg 		}
68819f9361aSSagi Grimberg 	} else {
68938b57f82SNicholas Bellinger 		cmd->prot_op = fabric_prot ? TARGET_PROT_DIN_INSERT :
69038b57f82SNicholas Bellinger 			       protect ? TARGET_PROT_DIN_PASS :
69119f9361aSSagi Grimberg 			       TARGET_PROT_DIN_STRIP;
69219f9361aSSagi Grimberg 		switch (protect) {
69319f9361aSSagi Grimberg 		case 0x0:
69419f9361aSSagi Grimberg 		case 0x1:
69519f9361aSSagi Grimberg 		case 0x5:
69619f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
69719f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
69819f9361aSSagi Grimberg 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
69919f9361aSSagi Grimberg 			break;
70019f9361aSSagi Grimberg 		case 0x2:
70119f9361aSSagi Grimberg 			if (prot_type == TARGET_DIF_TYPE1_PROT)
70219f9361aSSagi Grimberg 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
70319f9361aSSagi Grimberg 			break;
70419f9361aSSagi Grimberg 		case 0x3:
70519f9361aSSagi Grimberg 			cmd->prot_checks = 0;
70619f9361aSSagi Grimberg 			break;
70719f9361aSSagi Grimberg 		case 0x4:
70819f9361aSSagi Grimberg 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
70919f9361aSSagi Grimberg 			break;
71019f9361aSSagi Grimberg 		default:
71119f9361aSSagi Grimberg 			pr_err("Unsupported protect field %d\n", protect);
71219f9361aSSagi Grimberg 			return -EINVAL;
71319f9361aSSagi Grimberg 		}
71419f9361aSSagi Grimberg 	}
71519f9361aSSagi Grimberg 
71619f9361aSSagi Grimberg 	return 0;
71719f9361aSSagi Grimberg }
71819f9361aSSagi Grimberg 
719f7b7c06fSNicholas Bellinger static sense_reason_t
720*6d8e7e7cSDmitry Bogdanov sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char protect,
72119f9361aSSagi Grimberg 	       u32 sectors, bool is_write)
722499bf77bSNicholas Bellinger {
72338b57f82SNicholas Bellinger 	int sp_ops = cmd->se_sess->sup_prot_ops;
72438b57f82SNicholas Bellinger 	int pi_prot_type = dev->dev_attrib.pi_prot_type;
72538b57f82SNicholas Bellinger 	bool fabric_prot = false;
72619f9361aSSagi Grimberg 
727f7b7c06fSNicholas Bellinger 	if (!cmd->t_prot_sg || !cmd->t_prot_nents) {
72838b57f82SNicholas Bellinger 		if (unlikely(protect &&
72938b57f82SNicholas Bellinger 		    !dev->dev_attrib.pi_prot_type && !cmd->se_sess->sess_prot_type)) {
73038b57f82SNicholas Bellinger 			pr_err("CDB contains protect bit, but device + fabric does"
73138b57f82SNicholas Bellinger 			       " not advertise PROTECT=1 feature bit\n");
732f7b7c06fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
733f7b7c06fSNicholas Bellinger 		}
734f7b7c06fSNicholas Bellinger 		if (cmd->prot_pto)
735f7b7c06fSNicholas Bellinger 			return TCM_NO_SENSE;
736f7b7c06fSNicholas Bellinger 	}
737499bf77bSNicholas Bellinger 
738499bf77bSNicholas Bellinger 	switch (dev->dev_attrib.pi_prot_type) {
739499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE3_PROT:
740499bf77bSNicholas Bellinger 		cmd->reftag_seed = 0xffffffff;
741499bf77bSNicholas Bellinger 		break;
742499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE2_PROT:
74319f9361aSSagi Grimberg 		if (protect)
744f7b7c06fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
745499bf77bSNicholas Bellinger 
746499bf77bSNicholas Bellinger 		cmd->reftag_seed = cmd->t_task_lba;
747499bf77bSNicholas Bellinger 		break;
748499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE1_PROT:
749499bf77bSNicholas Bellinger 		cmd->reftag_seed = cmd->t_task_lba;
750499bf77bSNicholas Bellinger 		break;
751499bf77bSNicholas Bellinger 	case TARGET_DIF_TYPE0_PROT:
75238b57f82SNicholas Bellinger 		/*
75338b57f82SNicholas Bellinger 		 * See if the fabric supports T10-PI, and the session has been
75438b57f82SNicholas Bellinger 		 * configured to allow export PROTECT=1 feature bit with backend
75538b57f82SNicholas Bellinger 		 * devices that don't support T10-PI.
75638b57f82SNicholas Bellinger 		 */
75738b57f82SNicholas Bellinger 		fabric_prot = is_write ?
75838b57f82SNicholas Bellinger 			      !!(sp_ops & (TARGET_PROT_DOUT_PASS | TARGET_PROT_DOUT_STRIP)) :
75938b57f82SNicholas Bellinger 			      !!(sp_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DIN_INSERT));
76038b57f82SNicholas Bellinger 
76138b57f82SNicholas Bellinger 		if (fabric_prot && cmd->se_sess->sess_prot_type) {
76238b57f82SNicholas Bellinger 			pi_prot_type = cmd->se_sess->sess_prot_type;
76338b57f82SNicholas Bellinger 			break;
76438b57f82SNicholas Bellinger 		}
765cceca4a6SNicholas Bellinger 		if (!protect)
766f7b7c06fSNicholas Bellinger 			return TCM_NO_SENSE;
767df561f66SGustavo A. R. Silva 		fallthrough;
768499bf77bSNicholas Bellinger 	default:
769cceca4a6SNicholas Bellinger 		pr_err("Unable to determine pi_prot_type for CDB: 0x%02x "
770*6d8e7e7cSDmitry Bogdanov 		       "PROTECT: 0x%02x\n", cmd->t_task_cdb[0], protect);
771cceca4a6SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
772499bf77bSNicholas Bellinger 	}
773499bf77bSNicholas Bellinger 
77438b57f82SNicholas Bellinger 	if (sbc_set_prot_op_checks(protect, fabric_prot, pi_prot_type, is_write, cmd))
775f7b7c06fSNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
77619f9361aSSagi Grimberg 
77738b57f82SNicholas Bellinger 	cmd->prot_type = pi_prot_type;
778499bf77bSNicholas Bellinger 	cmd->prot_length = dev->prot_length * sectors;
779e2a4f55cSSagi Grimberg 
780e2a4f55cSSagi Grimberg 	/**
781e2a4f55cSSagi Grimberg 	 * In case protection information exists over the wire
782e2a4f55cSSagi Grimberg 	 * we modify command data length to describe pure data.
783e2a4f55cSSagi Grimberg 	 * The actual transfer length is data length + protection
784e2a4f55cSSagi Grimberg 	 * length
785e2a4f55cSSagi Grimberg 	 **/
786e2a4f55cSSagi Grimberg 	if (protect)
787e2a4f55cSSagi Grimberg 		cmd->data_length = sectors * dev->dev_attrib.block_size;
788e2a4f55cSSagi Grimberg 
789e2a4f55cSSagi Grimberg 	pr_debug("%s: prot_type=%d, data_length=%d, prot_length=%d "
790e2a4f55cSSagi Grimberg 		 "prot_op=%d prot_checks=%d\n",
791e2a4f55cSSagi Grimberg 		 __func__, cmd->prot_type, cmd->data_length, cmd->prot_length,
79203abad9eSSagi Grimberg 		 cmd->prot_op, cmd->prot_checks);
793499bf77bSNicholas Bellinger 
794f7b7c06fSNicholas Bellinger 	return TCM_NO_SENSE;
795499bf77bSNicholas Bellinger }
796499bf77bSNicholas Bellinger 
797fde9f50fSNicholas Bellinger static int
798fde9f50fSNicholas Bellinger sbc_check_dpofua(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb)
799fde9f50fSNicholas Bellinger {
800fde9f50fSNicholas Bellinger 	if (cdb[1] & 0x10) {
801814e5b45SChristoph Hellwig 		/* see explanation in spc_emulate_modesense */
802814e5b45SChristoph Hellwig 		if (!target_check_fua(dev)) {
803fde9f50fSNicholas Bellinger 			pr_err("Got CDB: 0x%02x with DPO bit set, but device"
804fde9f50fSNicholas Bellinger 			       " does not advertise support for DPO\n", cdb[0]);
805fde9f50fSNicholas Bellinger 			return -EINVAL;
806fde9f50fSNicholas Bellinger 		}
807fde9f50fSNicholas Bellinger 	}
808fde9f50fSNicholas Bellinger 	if (cdb[1] & 0x8) {
809814e5b45SChristoph Hellwig 		if (!target_check_fua(dev)) {
810fde9f50fSNicholas Bellinger 			pr_err("Got CDB: 0x%02x with FUA bit set, but device"
811fde9f50fSNicholas Bellinger 			       " does not advertise support for FUA write\n",
812fde9f50fSNicholas Bellinger 			       cdb[0]);
813fde9f50fSNicholas Bellinger 			return -EINVAL;
814fde9f50fSNicholas Bellinger 		}
815fde9f50fSNicholas Bellinger 		cmd->se_cmd_flags |= SCF_FUA;
816fde9f50fSNicholas Bellinger 	}
817fde9f50fSNicholas Bellinger 	return 0;
818d6e0175cSChristoph Hellwig }
819d6e0175cSChristoph Hellwig 
820de103c93SChristoph Hellwig sense_reason_t
821de103c93SChristoph Hellwig sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
822d6e0175cSChristoph Hellwig {
823d6e0175cSChristoph Hellwig 	struct se_device *dev = cmd->se_dev;
824d6e0175cSChristoph Hellwig 	unsigned char *cdb = cmd->t_task_cdb;
8251fd032eeSChristoph Hellwig 	unsigned int size;
826d6e0175cSChristoph Hellwig 	u32 sectors = 0;
827de103c93SChristoph Hellwig 	sense_reason_t ret;
828d6e0175cSChristoph Hellwig 
8297a971b1bSChristoph Hellwig 	cmd->protocol_data = ops;
8307a971b1bSChristoph Hellwig 
831d6e0175cSChristoph Hellwig 	switch (cdb[0]) {
832d6e0175cSChristoph Hellwig 	case READ_6:
833d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_6(cdb);
834d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_21(cdb);
835d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
836a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
837d6e0175cSChristoph Hellwig 		break;
838d6e0175cSChristoph Hellwig 	case READ_10:
839d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
840d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
841499bf77bSNicholas Bellinger 
842fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
843fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
844fde9f50fSNicholas Bellinger 
845*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
846f7b7c06fSNicholas Bellinger 		if (ret)
847f7b7c06fSNicholas Bellinger 			return ret;
848499bf77bSNicholas Bellinger 
849d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
850a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
851d6e0175cSChristoph Hellwig 		break;
852d6e0175cSChristoph Hellwig 	case READ_12:
853d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_12(cdb);
854d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
855499bf77bSNicholas Bellinger 
856fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
857fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
858fde9f50fSNicholas Bellinger 
859*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
860f7b7c06fSNicholas Bellinger 		if (ret)
861f7b7c06fSNicholas Bellinger 			return ret;
862499bf77bSNicholas Bellinger 
863d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
864a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
865d6e0175cSChristoph Hellwig 		break;
866d6e0175cSChristoph Hellwig 	case READ_16:
867d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
868d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_64(cdb);
869499bf77bSNicholas Bellinger 
870fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
871fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
872fde9f50fSNicholas Bellinger 
873*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, false);
874f7b7c06fSNicholas Bellinger 		if (ret)
875f7b7c06fSNicholas Bellinger 			return ret;
876499bf77bSNicholas Bellinger 
877d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
878a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
879d6e0175cSChristoph Hellwig 		break;
880d6e0175cSChristoph Hellwig 	case WRITE_6:
881d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_6(cdb);
882d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_21(cdb);
883d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
884a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
885d6e0175cSChristoph Hellwig 		break;
886d6e0175cSChristoph Hellwig 	case WRITE_10:
887984a9d4cSNicholas Bellinger 	case WRITE_VERIFY:
888d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
889d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
890499bf77bSNicholas Bellinger 
891fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
892fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
893499bf77bSNicholas Bellinger 
894*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
895f7b7c06fSNicholas Bellinger 		if (ret)
896f7b7c06fSNicholas Bellinger 			return ret;
897d6e0175cSChristoph Hellwig 
898d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
899a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
900d6e0175cSChristoph Hellwig 		break;
901d6e0175cSChristoph Hellwig 	case WRITE_12:
902d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_12(cdb);
903d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
904499bf77bSNicholas Bellinger 
905fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
906fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
907499bf77bSNicholas Bellinger 
908*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
909f7b7c06fSNicholas Bellinger 		if (ret)
910f7b7c06fSNicholas Bellinger 			return ret;
911d6e0175cSChristoph Hellwig 
912d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
913a82a9538SNicholas Bellinger 		cmd->execute_cmd = sbc_execute_rw;
914d6e0175cSChristoph Hellwig 		break;
915d6e0175cSChristoph Hellwig 	case WRITE_16:
916984a9d4cSNicholas Bellinger 	case WRITE_VERIFY_16:
917d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
918d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_64(cdb);
919499bf77bSNicholas Bellinger 
920fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
921fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
922499bf77bSNicholas Bellinger 
923*6d8e7e7cSDmitry Bogdanov 		ret = sbc_check_prot(dev, cmd, cdb[1] >> 5, sectors, true);
924f7b7c06fSNicholas Bellinger 		if (ret)
925f7b7c06fSNicholas Bellinger 			return ret;
926cd063befSNicholas Bellinger 
927d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
928d6e0175cSChristoph Hellwig 		cmd->execute_cmd = sbc_execute_rw;
929d6e0175cSChristoph Hellwig 		break;
930d6e0175cSChristoph Hellwig 	case XDWRITEREAD_10:
931d6e0175cSChristoph Hellwig 		if (cmd->data_direction != DMA_TO_DEVICE ||
932d6e0175cSChristoph Hellwig 		    !(cmd->se_cmd_flags & SCF_BIDI))
933d6e0175cSChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
934d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
935d6e0175cSChristoph Hellwig 
936fde9f50fSNicholas Bellinger 		if (sbc_check_dpofua(dev, cmd, cdb))
937fde9f50fSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
938fde9f50fSNicholas Bellinger 
939d6e0175cSChristoph Hellwig 		cmd->t_task_lba = transport_lba_32(cdb);
940d6e0175cSChristoph Hellwig 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
941d6e0175cSChristoph Hellwig 
942d6e0175cSChristoph Hellwig 		/*
943d6e0175cSChristoph Hellwig 		 * Setup BIDI XOR callback to be run after I/O completion.
944d6e0175cSChristoph Hellwig 		 */
945d6e0175cSChristoph Hellwig 		cmd->execute_cmd = sbc_execute_rw;
946d6e0175cSChristoph Hellwig 		cmd->transport_complete_callback = &xdreadwrite_callback;
9470c2ad7d1SChristoph Hellwig 		break;
948d6e0175cSChristoph Hellwig 	case VARIABLE_LENGTH_CMD:
949d6e0175cSChristoph Hellwig 	{
950d6e0175cSChristoph Hellwig 		u16 service_action = get_unaligned_be16(&cdb[8]);
951d6e0175cSChristoph Hellwig 		switch (service_action) {
952d6e0175cSChristoph Hellwig 		case XDWRITEREAD_32:
9536f974e8cSChristoph Hellwig 			sectors = transport_get_sectors_32(cdb);
954de103c93SChristoph Hellwig 
955fde9f50fSNicholas Bellinger 			if (sbc_check_dpofua(dev, cmd, cdb))
956fde9f50fSNicholas Bellinger 				return TCM_INVALID_CDB_FIELD;
9576f974e8cSChristoph Hellwig 			/*
958d6e0175cSChristoph Hellwig 			 * Use WRITE_32 and READ_32 opcodes for the emulated
959d6e0175cSChristoph Hellwig 			 * XDWRITE_READ_32 logic.
960d6e0175cSChristoph Hellwig 			 */
961d6e0175cSChristoph Hellwig 			cmd->t_task_lba = transport_lba_64_ext(cdb);
962de103c93SChristoph Hellwig 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
963d6e0175cSChristoph Hellwig 
964d6e0175cSChristoph Hellwig 			/*
9651fd032eeSChristoph Hellwig 			 * Setup BIDI XOR callback to be run during after I/O
966d6e0175cSChristoph Hellwig 			 * completion.
967d6e0175cSChristoph Hellwig 			 */
968de103c93SChristoph Hellwig 			cmd->execute_cmd = sbc_execute_rw;
9696f974e8cSChristoph Hellwig 			cmd->transport_complete_callback = &xdreadwrite_callback;
970d6e0175cSChristoph Hellwig 			break;
971d6e0175cSChristoph Hellwig 		case WRITE_SAME_32:
972d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_32(cdb);
973d6e0175cSChristoph Hellwig 			if (!sectors) {
974d6e0175cSChristoph Hellwig 				pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
975d6e0175cSChristoph Hellwig 				       " supported\n");
976d6e0175cSChristoph Hellwig 				return TCM_INVALID_CDB_FIELD;
977d6e0175cSChristoph Hellwig 			}
978d6e0175cSChristoph Hellwig 
979d6e0175cSChristoph Hellwig 			size = sbc_get_size(cmd, 1);
980d6e0175cSChristoph Hellwig 			cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
981d6e0175cSChristoph Hellwig 
982*6d8e7e7cSDmitry Bogdanov 			ret = sbc_setup_write_same(cmd, cdb[10], ops);
983d6e0175cSChristoph Hellwig 			if (ret)
9841fd032eeSChristoph Hellwig 				return ret;
985d6e0175cSChristoph Hellwig 			break;
986d6e0175cSChristoph Hellwig 		default:
987d6e0175cSChristoph Hellwig 			pr_err("VARIABLE_LENGTH_CMD service action"
988d6e0175cSChristoph Hellwig 				" 0x%04x not supported\n", service_action);
989de103c93SChristoph Hellwig 			return TCM_UNSUPPORTED_SCSI_OPCODE;
990d6e0175cSChristoph Hellwig 		}
991d6e0175cSChristoph Hellwig 		break;
992d6e0175cSChristoph Hellwig 	}
99368ff9b9bSNicholas Bellinger 	case COMPARE_AND_WRITE:
99412f66e4aSJiang Yi 		if (!dev->dev_attrib.emulate_caw) {
9956f3bf5a2SBart Van Assche 			pr_err_ratelimited("se_device %s/%s (vpd_unit_serial %s) reject COMPARE_AND_WRITE\n",
9966f3bf5a2SBart Van Assche 					   dev->se_hba->backend->ops->name,
9976f3bf5a2SBart Van Assche 					   config_item_name(&dev->dev_group.cg_item),
9986f3bf5a2SBart Van Assche 					   dev->t10_wwn.unit_serial);
99912f66e4aSJiang Yi 			return TCM_UNSUPPORTED_SCSI_OPCODE;
100012f66e4aSJiang Yi 		}
100168ff9b9bSNicholas Bellinger 		sectors = cdb[13];
100268ff9b9bSNicholas Bellinger 		/*
100368ff9b9bSNicholas Bellinger 		 * Currently enforce COMPARE_AND_WRITE for a single sector
100468ff9b9bSNicholas Bellinger 		 */
100568ff9b9bSNicholas Bellinger 		if (sectors > 1) {
100668ff9b9bSNicholas Bellinger 			pr_err("COMPARE_AND_WRITE contains NoLB: %u greater"
100768ff9b9bSNicholas Bellinger 			       " than 1\n", sectors);
100868ff9b9bSNicholas Bellinger 			return TCM_INVALID_CDB_FIELD;
100968ff9b9bSNicholas Bellinger 		}
1010ab81a5e0SDavid Disseldorp 		if (sbc_check_dpofua(dev, cmd, cdb))
1011ab81a5e0SDavid Disseldorp 			return TCM_INVALID_CDB_FIELD;
1012ab81a5e0SDavid Disseldorp 
101368ff9b9bSNicholas Bellinger 		/*
101468ff9b9bSNicholas Bellinger 		 * Double size because we have two buffers, note that
101568ff9b9bSNicholas Bellinger 		 * zero is not an error..
101668ff9b9bSNicholas Bellinger 		 */
101768ff9b9bSNicholas Bellinger 		size = 2 * sbc_get_size(cmd, sectors);
101868ff9b9bSNicholas Bellinger 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
101968ff9b9bSNicholas Bellinger 		cmd->t_task_nolb = sectors;
102068ff9b9bSNicholas Bellinger 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE;
102168ff9b9bSNicholas Bellinger 		cmd->execute_cmd = sbc_compare_and_write;
102268ff9b9bSNicholas Bellinger 		cmd->transport_complete_callback = compare_and_write_callback;
102368ff9b9bSNicholas Bellinger 		break;
1024d6e0175cSChristoph Hellwig 	case READ_CAPACITY:
10251fd032eeSChristoph Hellwig 		size = READ_CAP_LEN;
10261fd032eeSChristoph Hellwig 		cmd->execute_cmd = sbc_emulate_readcapacity;
1027d6e0175cSChristoph Hellwig 		break;
1028eb846d9fSHannes Reinecke 	case SERVICE_ACTION_IN_16:
1029d6e0175cSChristoph Hellwig 		switch (cmd->t_task_cdb[1] & 0x1f) {
1030d6e0175cSChristoph Hellwig 		case SAI_READ_CAPACITY_16:
10311fd032eeSChristoph Hellwig 			cmd->execute_cmd = sbc_emulate_readcapacity_16;
1032d6e0175cSChristoph Hellwig 			break;
1033c66094bfSHannes Reinecke 		case SAI_REPORT_REFERRALS:
1034c66094bfSHannes Reinecke 			cmd->execute_cmd = target_emulate_report_referrals;
1035c66094bfSHannes Reinecke 			break;
1036d6e0175cSChristoph Hellwig 		default:
1037d6e0175cSChristoph Hellwig 			pr_err("Unsupported SA: 0x%02x\n",
1038d6e0175cSChristoph Hellwig 				cmd->t_task_cdb[1] & 0x1f);
1039de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
1040d6e0175cSChristoph Hellwig 		}
1041a85d667eSBart Van Assche 		size = get_unaligned_be32(&cdb[10]);
1042d6e0175cSChristoph Hellwig 		break;
1043d6e0175cSChristoph Hellwig 	case SYNCHRONIZE_CACHE:
1044d6e0175cSChristoph Hellwig 	case SYNCHRONIZE_CACHE_16:
1045d6e0175cSChristoph Hellwig 		if (cdb[0] == SYNCHRONIZE_CACHE) {
1046d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_10(cdb);
1047d6e0175cSChristoph Hellwig 			cmd->t_task_lba = transport_lba_32(cdb);
1048d6e0175cSChristoph Hellwig 		} else {
1049d6e0175cSChristoph Hellwig 			sectors = transport_get_sectors_16(cdb);
1050d6e0175cSChristoph Hellwig 			cmd->t_task_lba = transport_lba_64(cdb);
1051d6e0175cSChristoph Hellwig 		}
10526ef31dc7SChristophe Vu-Brugier 		if (ops->execute_sync_cache) {
1053ad67f0d9SChristoph Hellwig 			cmd->execute_cmd = ops->execute_sync_cache;
10546ef31dc7SChristophe Vu-Brugier 			goto check_lba;
10556ef31dc7SChristophe Vu-Brugier 		}
10566ef31dc7SChristophe Vu-Brugier 		size = 0;
10576ef31dc7SChristophe Vu-Brugier 		cmd->execute_cmd = sbc_emulate_noop;
1058d6e0175cSChristoph Hellwig 		break;
1059d6e0175cSChristoph Hellwig 	case UNMAP:
106014150a6bSChristoph Hellwig 		if (!ops->execute_unmap)
1061de103c93SChristoph Hellwig 			return TCM_UNSUPPORTED_SCSI_OPCODE;
106214150a6bSChristoph Hellwig 
106361fdb4acSNicholas Bellinger 		if (!dev->dev_attrib.emulate_tpu) {
106461fdb4acSNicholas Bellinger 			pr_err("Got UNMAP, but backend device has"
106561fdb4acSNicholas Bellinger 			       " emulate_tpu disabled\n");
106661fdb4acSNicholas Bellinger 			return TCM_UNSUPPORTED_SCSI_OPCODE;
106761fdb4acSNicholas Bellinger 		}
10681fd032eeSChristoph Hellwig 		size = get_unaligned_be16(&cdb[7]);
106962e46942SChristoph Hellwig 		cmd->execute_cmd = sbc_execute_unmap;
1070d6e0175cSChristoph Hellwig 		break;
1071d6e0175cSChristoph Hellwig 	case WRITE_SAME_16:
1072d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_16(cdb);
1073d6e0175cSChristoph Hellwig 		if (!sectors) {
1074d6e0175cSChristoph Hellwig 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
1075de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
1076d6e0175cSChristoph Hellwig 		}
1077d6e0175cSChristoph Hellwig 
10781fd032eeSChristoph Hellwig 		size = sbc_get_size(cmd, 1);
1079d6e0175cSChristoph Hellwig 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
1080d6e0175cSChristoph Hellwig 
1081*6d8e7e7cSDmitry Bogdanov 		ret = sbc_setup_write_same(cmd, cdb[1], ops);
10826b64e1feSDan Carpenter 		if (ret)
1083cd063befSNicholas Bellinger 			return ret;
1084d6e0175cSChristoph Hellwig 		break;
1085d6e0175cSChristoph Hellwig 	case WRITE_SAME:
1086d6e0175cSChristoph Hellwig 		sectors = transport_get_sectors_10(cdb);
1087d6e0175cSChristoph Hellwig 		if (!sectors) {
1088d6e0175cSChristoph Hellwig 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
1089de103c93SChristoph Hellwig 			return TCM_INVALID_CDB_FIELD;
1090d6e0175cSChristoph Hellwig 		}
1091d6e0175cSChristoph Hellwig 
10921fd032eeSChristoph Hellwig 		size = sbc_get_size(cmd, 1);
1093d6e0175cSChristoph Hellwig 		cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
1094d6e0175cSChristoph Hellwig 
1095d6e0175cSChristoph Hellwig 		/*
1096d6e0175cSChristoph Hellwig 		 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
1097d6e0175cSChristoph Hellwig 		 * of byte 1 bit 3 UNMAP instead of original reserved field
1098d6e0175cSChristoph Hellwig 		 */
1099*6d8e7e7cSDmitry Bogdanov 		ret = sbc_setup_write_same(cmd, cdb[1], ops);
11006b64e1feSDan Carpenter 		if (ret)
1101cd063befSNicholas Bellinger 			return ret;
1102d6e0175cSChristoph Hellwig 		break;
1103d6e0175cSChristoph Hellwig 	case VERIFY:
110413603685SMax Lohrmann 	case VERIFY_16:
1105984a9d4cSNicholas Bellinger 		size = 0;
1106984a9d4cSNicholas Bellinger 		if (cdb[0] == VERIFY) {
1107984a9d4cSNicholas Bellinger 			sectors = transport_get_sectors_10(cdb);
1108984a9d4cSNicholas Bellinger 			cmd->t_task_lba = transport_lba_32(cdb);
1109984a9d4cSNicholas Bellinger 		} else {
1110984a9d4cSNicholas Bellinger 			sectors = transport_get_sectors_16(cdb);
1111984a9d4cSNicholas Bellinger 			cmd->t_task_lba = transport_lba_64(cdb);
1112984a9d4cSNicholas Bellinger 		}
11131920ed61SNicholas Bellinger 		cmd->execute_cmd = sbc_emulate_noop;
1114c52716deSChristophe Vu-Brugier 		goto check_lba;
11151a1ff38cSBernhard Kohl 	case REZERO_UNIT:
11161a1ff38cSBernhard Kohl 	case SEEK_6:
11171a1ff38cSBernhard Kohl 	case SEEK_10:
11181a1ff38cSBernhard Kohl 		/*
11191a1ff38cSBernhard Kohl 		 * There are still clients out there which use these old SCSI-2
11201a1ff38cSBernhard Kohl 		 * commands. This mainly happens when running VMs with legacy
11211a1ff38cSBernhard Kohl 		 * guest systems, connected via SCSI command pass-through to
11221a1ff38cSBernhard Kohl 		 * iSCSI targets. Make them happy and return status GOOD.
11231a1ff38cSBernhard Kohl 		 */
11241a1ff38cSBernhard Kohl 		size = 0;
11251a1ff38cSBernhard Kohl 		cmd->execute_cmd = sbc_emulate_noop;
11261a1ff38cSBernhard Kohl 		break;
112745182ed5SBrian Bunker 	case START_STOP:
112845182ed5SBrian Bunker 		size = 0;
112945182ed5SBrian Bunker 		cmd->execute_cmd = sbc_emulate_startstop;
113045182ed5SBrian Bunker 		break;
1131d6e0175cSChristoph Hellwig 	default:
11321fd032eeSChristoph Hellwig 		ret = spc_parse_cdb(cmd, &size);
1133d6e0175cSChristoph Hellwig 		if (ret)
1134d6e0175cSChristoph Hellwig 			return ret;
1135d6e0175cSChristoph Hellwig 	}
1136d6e0175cSChristoph Hellwig 
1137d6e0175cSChristoph Hellwig 	/* reject any command that we don't have a handler for */
113820959c4bSAndy Grover 	if (!cmd->execute_cmd)
1139de103c93SChristoph Hellwig 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1140d6e0175cSChristoph Hellwig 
1141d6e0175cSChristoph Hellwig 	if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
11421fd032eeSChristoph Hellwig 		unsigned long long end_lba;
11436ef31dc7SChristophe Vu-Brugier check_lba:
11441fd032eeSChristoph Hellwig 		end_lba = dev->transport->get_blocks(dev) + 1;
1145aa179935SNicholas Bellinger 		if (((cmd->t_task_lba + sectors) < cmd->t_task_lba) ||
1146aa179935SNicholas Bellinger 		    ((cmd->t_task_lba + sectors) > end_lba)) {
11471fd032eeSChristoph Hellwig 			pr_err("cmd exceeds last lba %llu "
11481fd032eeSChristoph Hellwig 				"(lba %llu, sectors %u)\n",
11491fd032eeSChristoph Hellwig 				end_lba, cmd->t_task_lba, sectors);
115009ceadc7SRoland Dreier 			return TCM_ADDRESS_OUT_OF_RANGE;
1151d6e0175cSChristoph Hellwig 		}
1152d6e0175cSChristoph Hellwig 
115368ff9b9bSNicholas Bellinger 		if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE))
11541fd032eeSChristoph Hellwig 			size = sbc_get_size(cmd, sectors);
11551fd032eeSChristoph Hellwig 	}
11561fd032eeSChristoph Hellwig 
1157de103c93SChristoph Hellwig 	return target_cmd_size_check(cmd, size);
1158d6e0175cSChristoph Hellwig }
1159d6e0175cSChristoph Hellwig EXPORT_SYMBOL(sbc_parse_cdb);
11606f23ac8aSChristoph Hellwig 
11616f23ac8aSChristoph Hellwig u32 sbc_get_device_type(struct se_device *dev)
11626f23ac8aSChristoph Hellwig {
11636f23ac8aSChristoph Hellwig 	return TYPE_DISK;
11646f23ac8aSChristoph Hellwig }
11656f23ac8aSChristoph Hellwig EXPORT_SYMBOL(sbc_get_device_type);
116686d71829SAsias He 
116762e46942SChristoph Hellwig static sense_reason_t
116862e46942SChristoph Hellwig sbc_execute_unmap(struct se_cmd *cmd)
116986d71829SAsias He {
117062e46942SChristoph Hellwig 	struct sbc_ops *ops = cmd->protocol_data;
117186d71829SAsias He 	struct se_device *dev = cmd->se_dev;
117286d71829SAsias He 	unsigned char *buf, *ptr = NULL;
117386d71829SAsias He 	sector_t lba;
117486d71829SAsias He 	int size;
117586d71829SAsias He 	u32 range;
117686d71829SAsias He 	sense_reason_t ret = 0;
117786d71829SAsias He 	int dl, bd_dl;
117886d71829SAsias He 
117986d71829SAsias He 	/* We never set ANC_SUP */
118086d71829SAsias He 	if (cmd->t_task_cdb[1])
118186d71829SAsias He 		return TCM_INVALID_CDB_FIELD;
118286d71829SAsias He 
118386d71829SAsias He 	if (cmd->data_length == 0) {
118486d71829SAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
118586d71829SAsias He 		return 0;
118686d71829SAsias He 	}
118786d71829SAsias He 
118886d71829SAsias He 	if (cmd->data_length < 8) {
118986d71829SAsias He 		pr_warn("UNMAP parameter list length %u too small\n",
119086d71829SAsias He 			cmd->data_length);
119186d71829SAsias He 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
119286d71829SAsias He 	}
119386d71829SAsias He 
119486d71829SAsias He 	buf = transport_kmap_data_sg(cmd);
119586d71829SAsias He 	if (!buf)
119686d71829SAsias He 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
119786d71829SAsias He 
119886d71829SAsias He 	dl = get_unaligned_be16(&buf[0]);
119986d71829SAsias He 	bd_dl = get_unaligned_be16(&buf[2]);
120086d71829SAsias He 
120186d71829SAsias He 	size = cmd->data_length - 8;
120286d71829SAsias He 	if (bd_dl > size)
120386d71829SAsias He 		pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
120486d71829SAsias He 			cmd->data_length, bd_dl);
120586d71829SAsias He 	else
120686d71829SAsias He 		size = bd_dl;
120786d71829SAsias He 
120886d71829SAsias He 	if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
120986d71829SAsias He 		ret = TCM_INVALID_PARAMETER_LIST;
121086d71829SAsias He 		goto err;
121186d71829SAsias He 	}
121286d71829SAsias He 
121386d71829SAsias He 	/* First UNMAP block descriptor starts at 8 byte offset */
121486d71829SAsias He 	ptr = &buf[8];
121586d71829SAsias He 	pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
121686d71829SAsias He 		" ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
121786d71829SAsias He 
121886d71829SAsias He 	while (size >= 16) {
121986d71829SAsias He 		lba = get_unaligned_be64(&ptr[0]);
122086d71829SAsias He 		range = get_unaligned_be32(&ptr[8]);
122186d71829SAsias He 		pr_debug("UNMAP: Using lba: %llu and range: %u\n",
122286d71829SAsias He 				 (unsigned long long)lba, range);
122386d71829SAsias He 
122486d71829SAsias He 		if (range > dev->dev_attrib.max_unmap_lba_count) {
122586d71829SAsias He 			ret = TCM_INVALID_PARAMETER_LIST;
122686d71829SAsias He 			goto err;
122786d71829SAsias He 		}
122886d71829SAsias He 
122986d71829SAsias He 		if (lba + range > dev->transport->get_blocks(dev) + 1) {
123086d71829SAsias He 			ret = TCM_ADDRESS_OUT_OF_RANGE;
123186d71829SAsias He 			goto err;
123286d71829SAsias He 		}
123386d71829SAsias He 
12349960f851SAndrei Vagin 		if (range) {
123562e46942SChristoph Hellwig 			ret = ops->execute_unmap(cmd, lba, range);
123686d71829SAsias He 			if (ret)
123786d71829SAsias He 				goto err;
12389960f851SAndrei Vagin 		}
123986d71829SAsias He 
124086d71829SAsias He 		ptr += 16;
124186d71829SAsias He 		size -= 16;
124286d71829SAsias He 	}
124386d71829SAsias He 
124486d71829SAsias He err:
124586d71829SAsias He 	transport_kunmap_data_sg(cmd);
124686d71829SAsias He 	if (!ret)
124714b40c1eSHannes Reinecke 		target_complete_cmd(cmd, SAM_STAT_GOOD);
124886d71829SAsias He 	return ret;
124986d71829SAsias He }
125041861fa8SNicholas Bellinger 
125166a3d5bcSNicholas Bellinger void
125266a3d5bcSNicholas Bellinger sbc_dif_generate(struct se_cmd *cmd)
125366a3d5bcSNicholas Bellinger {
125466a3d5bcSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
1255fe052a18SSagi Grimberg 	struct t10_pi_tuple *sdt;
125618213afbSAkinobu Mita 	struct scatterlist *dsg = cmd->t_data_sg, *psg;
125766a3d5bcSNicholas Bellinger 	sector_t sector = cmd->t_task_lba;
125866a3d5bcSNicholas Bellinger 	void *daddr, *paddr;
125966a3d5bcSNicholas Bellinger 	int i, j, offset = 0;
126018213afbSAkinobu Mita 	unsigned int block_size = dev->dev_attrib.block_size;
126166a3d5bcSNicholas Bellinger 
126218213afbSAkinobu Mita 	for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
126318213afbSAkinobu Mita 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
126466a3d5bcSNicholas Bellinger 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
126566a3d5bcSNicholas Bellinger 
126618213afbSAkinobu Mita 		for (j = 0; j < psg->length;
1267fe052a18SSagi Grimberg 				j += sizeof(*sdt)) {
126818213afbSAkinobu Mita 			__u16 crc;
126918213afbSAkinobu Mita 			unsigned int avail;
127066a3d5bcSNicholas Bellinger 
127118213afbSAkinobu Mita 			if (offset >= dsg->length) {
127218213afbSAkinobu Mita 				offset -= dsg->length;
127318213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
127418213afbSAkinobu Mita 				dsg = sg_next(dsg);
127518213afbSAkinobu Mita 				if (!dsg) {
127618213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
127718213afbSAkinobu Mita 					return;
127818213afbSAkinobu Mita 				}
127918213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
128066a3d5bcSNicholas Bellinger 			}
128166a3d5bcSNicholas Bellinger 
128218213afbSAkinobu Mita 			sdt = paddr + j;
128318213afbSAkinobu Mita 			avail = min(block_size, dsg->length - offset);
128418213afbSAkinobu Mita 			crc = crc_t10dif(daddr + offset, avail);
128518213afbSAkinobu Mita 			if (avail < block_size) {
128618213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
128718213afbSAkinobu Mita 				dsg = sg_next(dsg);
128818213afbSAkinobu Mita 				if (!dsg) {
128918213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
129018213afbSAkinobu Mita 					return;
129118213afbSAkinobu Mita 				}
129218213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
129318213afbSAkinobu Mita 				offset = block_size - avail;
129418213afbSAkinobu Mita 				crc = crc_t10dif_update(crc, daddr, offset);
129518213afbSAkinobu Mita 			} else {
129618213afbSAkinobu Mita 				offset += block_size;
129718213afbSAkinobu Mita 			}
129818213afbSAkinobu Mita 
129918213afbSAkinobu Mita 			sdt->guard_tag = cpu_to_be16(crc);
1300823ddd87SNicholas Bellinger 			if (cmd->prot_type == TARGET_DIF_TYPE1_PROT)
130166a3d5bcSNicholas Bellinger 				sdt->ref_tag = cpu_to_be32(sector & 0xffffffff);
130266a3d5bcSNicholas Bellinger 			sdt->app_tag = 0;
130366a3d5bcSNicholas Bellinger 
13046ae50408SNicholas Bellinger 			pr_debug("DIF %s INSERT sector: %llu guard_tag: 0x%04x"
130566a3d5bcSNicholas Bellinger 				 " app_tag: 0x%04x ref_tag: %u\n",
13066ae50408SNicholas Bellinger 				 (cmd->data_direction == DMA_TO_DEVICE) ?
13076ae50408SNicholas Bellinger 				 "WRITE" : "READ", (unsigned long long)sector,
13086ae50408SNicholas Bellinger 				 sdt->guard_tag, sdt->app_tag,
13096ae50408SNicholas Bellinger 				 be32_to_cpu(sdt->ref_tag));
131066a3d5bcSNicholas Bellinger 
131166a3d5bcSNicholas Bellinger 			sector++;
131266a3d5bcSNicholas Bellinger 		}
131366a3d5bcSNicholas Bellinger 
131418213afbSAkinobu Mita 		kunmap_atomic(daddr - dsg->offset);
131518213afbSAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
131666a3d5bcSNicholas Bellinger 	}
131766a3d5bcSNicholas Bellinger }
131866a3d5bcSNicholas Bellinger 
131941861fa8SNicholas Bellinger static sense_reason_t
1320fe052a18SSagi Grimberg sbc_dif_v1_verify(struct se_cmd *cmd, struct t10_pi_tuple *sdt,
132118213afbSAkinobu Mita 		  __u16 crc, sector_t sector, unsigned int ei_lba)
132241861fa8SNicholas Bellinger {
132341861fa8SNicholas Bellinger 	__be16 csum;
132441861fa8SNicholas Bellinger 
1325d7a463b0SNicholas Bellinger 	if (!(cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
1326d7a463b0SNicholas Bellinger 		goto check_ref;
1327d7a463b0SNicholas Bellinger 
132818213afbSAkinobu Mita 	csum = cpu_to_be16(crc);
132941861fa8SNicholas Bellinger 
133041861fa8SNicholas Bellinger 	if (sdt->guard_tag != csum) {
133141861fa8SNicholas Bellinger 		pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x"
133241861fa8SNicholas Bellinger 			" csum 0x%04x\n", (unsigned long long)sector,
133341861fa8SNicholas Bellinger 			be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
133441861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
133541861fa8SNicholas Bellinger 	}
133641861fa8SNicholas Bellinger 
1337d7a463b0SNicholas Bellinger check_ref:
1338d7a463b0SNicholas Bellinger 	if (!(cmd->prot_checks & TARGET_DIF_CHECK_REFTAG))
1339d7a463b0SNicholas Bellinger 		return 0;
1340d7a463b0SNicholas Bellinger 
1341823ddd87SNicholas Bellinger 	if (cmd->prot_type == TARGET_DIF_TYPE1_PROT &&
134241861fa8SNicholas Bellinger 	    be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
134341861fa8SNicholas Bellinger 		pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x"
134441861fa8SNicholas Bellinger 		       " sector MSB: 0x%08x\n", (unsigned long long)sector,
134541861fa8SNicholas Bellinger 		       be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff));
134641861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
134741861fa8SNicholas Bellinger 	}
134841861fa8SNicholas Bellinger 
1349823ddd87SNicholas Bellinger 	if (cmd->prot_type == TARGET_DIF_TYPE2_PROT &&
135041861fa8SNicholas Bellinger 	    be32_to_cpu(sdt->ref_tag) != ei_lba) {
135141861fa8SNicholas Bellinger 		pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x"
135241861fa8SNicholas Bellinger 		       " ei_lba: 0x%08x\n", (unsigned long long)sector,
135341861fa8SNicholas Bellinger 			be32_to_cpu(sdt->ref_tag), ei_lba);
135441861fa8SNicholas Bellinger 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
135541861fa8SNicholas Bellinger 	}
135641861fa8SNicholas Bellinger 
135741861fa8SNicholas Bellinger 	return 0;
135841861fa8SNicholas Bellinger }
135941861fa8SNicholas Bellinger 
1360f75b6faeSSagi Grimberg void sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read,
136141861fa8SNicholas Bellinger 		       struct scatterlist *sg, int sg_off)
136241861fa8SNicholas Bellinger {
136341861fa8SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
136441861fa8SNicholas Bellinger 	struct scatterlist *psg;
136541861fa8SNicholas Bellinger 	void *paddr, *addr;
136641861fa8SNicholas Bellinger 	unsigned int i, len, left;
136710762e80SNicholas Bellinger 	unsigned int offset = sg_off;
136841861fa8SNicholas Bellinger 
136938b57f82SNicholas Bellinger 	if (!sg)
137038b57f82SNicholas Bellinger 		return;
137138b57f82SNicholas Bellinger 
137241861fa8SNicholas Bellinger 	left = sectors * dev->prot_length;
137341861fa8SNicholas Bellinger 
137441861fa8SNicholas Bellinger 	for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
137516c0ae02SSagi Grimberg 		unsigned int psg_len, copied = 0;
137641861fa8SNicholas Bellinger 
137716c0ae02SSagi Grimberg 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
137816c0ae02SSagi Grimberg 		psg_len = min(left, psg->length);
137916c0ae02SSagi Grimberg 		while (psg_len) {
138016c0ae02SSagi Grimberg 			len = min(psg_len, sg->length - offset);
138116c0ae02SSagi Grimberg 			addr = kmap_atomic(sg_page(sg)) + sg->offset + offset;
138216c0ae02SSagi Grimberg 
138316c0ae02SSagi Grimberg 			if (read)
138416c0ae02SSagi Grimberg 				memcpy(paddr + copied, addr, len);
138516c0ae02SSagi Grimberg 			else
138616c0ae02SSagi Grimberg 				memcpy(addr, paddr + copied, len);
138716c0ae02SSagi Grimberg 
138816c0ae02SSagi Grimberg 			left -= len;
138916c0ae02SSagi Grimberg 			offset += len;
139016c0ae02SSagi Grimberg 			copied += len;
139116c0ae02SSagi Grimberg 			psg_len -= len;
139216c0ae02SSagi Grimberg 
139357636388SAkinobu Mita 			kunmap_atomic(addr - sg->offset - offset);
139457636388SAkinobu Mita 
1395d6a65fdcSSagi Grimberg 			if (offset >= sg->length) {
1396d6a65fdcSSagi Grimberg 				sg = sg_next(sg);
1397d6a65fdcSSagi Grimberg 				offset = 0;
1398d6a65fdcSSagi Grimberg 			}
139941861fa8SNicholas Bellinger 		}
140057636388SAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
140116c0ae02SSagi Grimberg 	}
140241861fa8SNicholas Bellinger }
1403f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_copy_prot);
140441861fa8SNicholas Bellinger 
140541861fa8SNicholas Bellinger sense_reason_t
1406f75b6faeSSagi Grimberg sbc_dif_verify(struct se_cmd *cmd, sector_t start, unsigned int sectors,
1407414e4627SSagi Grimberg 	       unsigned int ei_lba, struct scatterlist *psg, int psg_off)
140841861fa8SNicholas Bellinger {
140941861fa8SNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
1410fe052a18SSagi Grimberg 	struct t10_pi_tuple *sdt;
141118213afbSAkinobu Mita 	struct scatterlist *dsg = cmd->t_data_sg;
141241861fa8SNicholas Bellinger 	sector_t sector = start;
141341861fa8SNicholas Bellinger 	void *daddr, *paddr;
141418213afbSAkinobu Mita 	int i;
141541861fa8SNicholas Bellinger 	sense_reason_t rc;
141618213afbSAkinobu Mita 	int dsg_off = 0;
141718213afbSAkinobu Mita 	unsigned int block_size = dev->dev_attrib.block_size;
141841861fa8SNicholas Bellinger 
141918213afbSAkinobu Mita 	for (; psg && sector < start + sectors; psg = sg_next(psg)) {
142018213afbSAkinobu Mita 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
142141861fa8SNicholas Bellinger 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
142241861fa8SNicholas Bellinger 
142318213afbSAkinobu Mita 		for (i = psg_off; i < psg->length &&
142418213afbSAkinobu Mita 				sector < start + sectors;
1425fe052a18SSagi Grimberg 				i += sizeof(*sdt)) {
142618213afbSAkinobu Mita 			__u16 crc;
142718213afbSAkinobu Mita 			unsigned int avail;
142841861fa8SNicholas Bellinger 
142918213afbSAkinobu Mita 			if (dsg_off >= dsg->length) {
143018213afbSAkinobu Mita 				dsg_off -= dsg->length;
143118213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
143218213afbSAkinobu Mita 				dsg = sg_next(dsg);
143318213afbSAkinobu Mita 				if (!dsg) {
1434414e4627SSagi Grimberg 					kunmap_atomic(paddr - psg->offset);
143541861fa8SNicholas Bellinger 					return 0;
143641861fa8SNicholas Bellinger 				}
143741861fa8SNicholas Bellinger 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
143841861fa8SNicholas Bellinger 			}
143941861fa8SNicholas Bellinger 
144018213afbSAkinobu Mita 			sdt = paddr + i;
144141861fa8SNicholas Bellinger 
144241861fa8SNicholas Bellinger 			pr_debug("DIF READ sector: %llu guard_tag: 0x%04x"
144341861fa8SNicholas Bellinger 				 " app_tag: 0x%04x ref_tag: %u\n",
144441861fa8SNicholas Bellinger 				 (unsigned long long)sector, sdt->guard_tag,
144541861fa8SNicholas Bellinger 				 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
144641861fa8SNicholas Bellinger 
1447128b6f9fSDmitry Monakhov 			if (sdt->app_tag == T10_PI_APP_ESCAPE) {
144818213afbSAkinobu Mita 				dsg_off += block_size;
144918213afbSAkinobu Mita 				goto next;
145041861fa8SNicholas Bellinger 			}
145141861fa8SNicholas Bellinger 
145218213afbSAkinobu Mita 			avail = min(block_size, dsg->length - dsg_off);
145318213afbSAkinobu Mita 			crc = crc_t10dif(daddr + dsg_off, avail);
145418213afbSAkinobu Mita 			if (avail < block_size) {
1455414e4627SSagi Grimberg 				kunmap_atomic(daddr - dsg->offset);
145618213afbSAkinobu Mita 				dsg = sg_next(dsg);
145718213afbSAkinobu Mita 				if (!dsg) {
145818213afbSAkinobu Mita 					kunmap_atomic(paddr - psg->offset);
145918213afbSAkinobu Mita 					return 0;
146018213afbSAkinobu Mita 				}
146118213afbSAkinobu Mita 				daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
146218213afbSAkinobu Mita 				dsg_off = block_size - avail;
146318213afbSAkinobu Mita 				crc = crc_t10dif_update(crc, daddr, dsg_off);
146418213afbSAkinobu Mita 			} else {
146518213afbSAkinobu Mita 				dsg_off += block_size;
146618213afbSAkinobu Mita 			}
146718213afbSAkinobu Mita 
146818213afbSAkinobu Mita 			rc = sbc_dif_v1_verify(cmd, sdt, crc, sector, ei_lba);
146941861fa8SNicholas Bellinger 			if (rc) {
147018213afbSAkinobu Mita 				kunmap_atomic(daddr - dsg->offset);
147118213afbSAkinobu Mita 				kunmap_atomic(paddr - psg->offset);
14728dd992fbSDavid Disseldorp 				cmd->sense_info = sector;
147341861fa8SNicholas Bellinger 				return rc;
147441861fa8SNicholas Bellinger 			}
147518213afbSAkinobu Mita next:
147641861fa8SNicholas Bellinger 			sector++;
147741861fa8SNicholas Bellinger 			ei_lba++;
147841861fa8SNicholas Bellinger 		}
147941861fa8SNicholas Bellinger 
148018213afbSAkinobu Mita 		psg_off = 0;
1481414e4627SSagi Grimberg 		kunmap_atomic(daddr - dsg->offset);
148218213afbSAkinobu Mita 		kunmap_atomic(paddr - psg->offset);
148341861fa8SNicholas Bellinger 	}
148441861fa8SNicholas Bellinger 
148541861fa8SNicholas Bellinger 	return 0;
148641861fa8SNicholas Bellinger }
1487f75b6faeSSagi Grimberg EXPORT_SYMBOL(sbc_dif_verify);
1488