1 /*
2  * SCSI Block Commands (SBC) parsing and emulation.
3  *
4  * (c) Copyright 2002-2013 Datera, Inc.
5  *
6  * Nicholas A. Bellinger <nab@kernel.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/ratelimit.h>
26 #include <linux/crc-t10dif.h>
27 #include <asm/unaligned.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_tcq.h>
30 
31 #include <target/target_core_base.h>
32 #include <target/target_core_backend.h>
33 #include <target/target_core_fabric.h>
34 
35 #include "target_core_internal.h"
36 #include "target_core_ua.h"
37 #include "target_core_alua.h"
38 
39 static sense_reason_t
40 sbc_emulate_readcapacity(struct se_cmd *cmd)
41 {
42 	struct se_device *dev = cmd->se_dev;
43 	unsigned char *cdb = cmd->t_task_cdb;
44 	unsigned long long blocks_long = dev->transport->get_blocks(dev);
45 	unsigned char *rbuf;
46 	unsigned char buf[8];
47 	u32 blocks;
48 
49 	/*
50 	 * SBC-2 says:
51 	 *   If the PMI bit is set to zero and the LOGICAL BLOCK
52 	 *   ADDRESS field is not set to zero, the device server shall
53 	 *   terminate the command with CHECK CONDITION status with
54 	 *   the sense key set to ILLEGAL REQUEST and the additional
55 	 *   sense code set to INVALID FIELD IN CDB.
56 	 *
57 	 * In SBC-3, these fields are obsolete, but some SCSI
58 	 * compliance tests actually check this, so we might as well
59 	 * follow SBC-2.
60 	 */
61 	if (!(cdb[8] & 1) && !!(cdb[2] | cdb[3] | cdb[4] | cdb[5]))
62 		return TCM_INVALID_CDB_FIELD;
63 
64 	if (blocks_long >= 0x00000000ffffffff)
65 		blocks = 0xffffffff;
66 	else
67 		blocks = (u32)blocks_long;
68 
69 	buf[0] = (blocks >> 24) & 0xff;
70 	buf[1] = (blocks >> 16) & 0xff;
71 	buf[2] = (blocks >> 8) & 0xff;
72 	buf[3] = blocks & 0xff;
73 	buf[4] = (dev->dev_attrib.block_size >> 24) & 0xff;
74 	buf[5] = (dev->dev_attrib.block_size >> 16) & 0xff;
75 	buf[6] = (dev->dev_attrib.block_size >> 8) & 0xff;
76 	buf[7] = dev->dev_attrib.block_size & 0xff;
77 
78 	rbuf = transport_kmap_data_sg(cmd);
79 	if (rbuf) {
80 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
81 		transport_kunmap_data_sg(cmd);
82 	}
83 
84 	target_complete_cmd(cmd, GOOD);
85 	return 0;
86 }
87 
88 static sense_reason_t
89 sbc_emulate_readcapacity_16(struct se_cmd *cmd)
90 {
91 	struct se_device *dev = cmd->se_dev;
92 	struct se_session *sess = cmd->se_sess;
93 	unsigned char *rbuf;
94 	unsigned char buf[32];
95 	unsigned long long blocks = dev->transport->get_blocks(dev);
96 
97 	memset(buf, 0, sizeof(buf));
98 	buf[0] = (blocks >> 56) & 0xff;
99 	buf[1] = (blocks >> 48) & 0xff;
100 	buf[2] = (blocks >> 40) & 0xff;
101 	buf[3] = (blocks >> 32) & 0xff;
102 	buf[4] = (blocks >> 24) & 0xff;
103 	buf[5] = (blocks >> 16) & 0xff;
104 	buf[6] = (blocks >> 8) & 0xff;
105 	buf[7] = blocks & 0xff;
106 	buf[8] = (dev->dev_attrib.block_size >> 24) & 0xff;
107 	buf[9] = (dev->dev_attrib.block_size >> 16) & 0xff;
108 	buf[10] = (dev->dev_attrib.block_size >> 8) & 0xff;
109 	buf[11] = dev->dev_attrib.block_size & 0xff;
110 	/*
111 	 * Set P_TYPE and PROT_EN bits for DIF support
112 	 */
113 	if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
114 		if (dev->dev_attrib.pi_prot_type)
115 			buf[12] = (dev->dev_attrib.pi_prot_type - 1) << 1 | 0x1;
116 	}
117 
118 	if (dev->transport->get_lbppbe)
119 		buf[13] = dev->transport->get_lbppbe(dev) & 0x0f;
120 
121 	if (dev->transport->get_alignment_offset_lbas) {
122 		u16 lalba = dev->transport->get_alignment_offset_lbas(dev);
123 		buf[14] = (lalba >> 8) & 0x3f;
124 		buf[15] = lalba & 0xff;
125 	}
126 
127 	/*
128 	 * Set Thin Provisioning Enable bit following sbc3r22 in section
129 	 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
130 	 */
131 	if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
132 		buf[14] |= 0x80;
133 
134 	rbuf = transport_kmap_data_sg(cmd);
135 	if (rbuf) {
136 		memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
137 		transport_kunmap_data_sg(cmd);
138 	}
139 
140 	target_complete_cmd(cmd, GOOD);
141 	return 0;
142 }
143 
144 sector_t sbc_get_write_same_sectors(struct se_cmd *cmd)
145 {
146 	u32 num_blocks;
147 
148 	if (cmd->t_task_cdb[0] == WRITE_SAME)
149 		num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
150 	else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
151 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
152 	else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
153 		num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
154 
155 	/*
156 	 * Use the explicit range when non zero is supplied, otherwise calculate
157 	 * the remaining range based on ->get_blocks() - starting LBA.
158 	 */
159 	if (num_blocks)
160 		return num_blocks;
161 
162 	return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
163 		cmd->t_task_lba + 1;
164 }
165 EXPORT_SYMBOL(sbc_get_write_same_sectors);
166 
167 static sense_reason_t
168 sbc_emulate_noop(struct se_cmd *cmd)
169 {
170 	target_complete_cmd(cmd, GOOD);
171 	return 0;
172 }
173 
174 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
175 {
176 	return cmd->se_dev->dev_attrib.block_size * sectors;
177 }
178 
179 static int sbc_check_valid_sectors(struct se_cmd *cmd)
180 {
181 	struct se_device *dev = cmd->se_dev;
182 	unsigned long long end_lba;
183 	u32 sectors;
184 
185 	sectors = cmd->data_length / dev->dev_attrib.block_size;
186 	end_lba = dev->transport->get_blocks(dev) + 1;
187 
188 	if (cmd->t_task_lba + sectors > end_lba) {
189 		pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
190 			cmd->t_task_lba, sectors, end_lba);
191 		return -EINVAL;
192 	}
193 
194 	return 0;
195 }
196 
197 static inline u32 transport_get_sectors_6(unsigned char *cdb)
198 {
199 	/*
200 	 * Use 8-bit sector value.  SBC-3 says:
201 	 *
202 	 *   A TRANSFER LENGTH field set to zero specifies that 256
203 	 *   logical blocks shall be written.  Any other value
204 	 *   specifies the number of logical blocks that shall be
205 	 *   written.
206 	 */
207 	return cdb[4] ? : 256;
208 }
209 
210 static inline u32 transport_get_sectors_10(unsigned char *cdb)
211 {
212 	return (u32)(cdb[7] << 8) + cdb[8];
213 }
214 
215 static inline u32 transport_get_sectors_12(unsigned char *cdb)
216 {
217 	return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
218 }
219 
220 static inline u32 transport_get_sectors_16(unsigned char *cdb)
221 {
222 	return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
223 		    (cdb[12] << 8) + cdb[13];
224 }
225 
226 /*
227  * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
228  */
229 static inline u32 transport_get_sectors_32(unsigned char *cdb)
230 {
231 	return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
232 		    (cdb[30] << 8) + cdb[31];
233 
234 }
235 
236 static inline u32 transport_lba_21(unsigned char *cdb)
237 {
238 	return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
239 }
240 
241 static inline u32 transport_lba_32(unsigned char *cdb)
242 {
243 	return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
244 }
245 
246 static inline unsigned long long transport_lba_64(unsigned char *cdb)
247 {
248 	unsigned int __v1, __v2;
249 
250 	__v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
251 	__v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
252 
253 	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
254 }
255 
256 /*
257  * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
258  */
259 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
260 {
261 	unsigned int __v1, __v2;
262 
263 	__v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
264 	__v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
265 
266 	return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
267 }
268 
269 static sense_reason_t
270 sbc_setup_write_same(struct se_cmd *cmd, unsigned char *flags, struct sbc_ops *ops)
271 {
272 	unsigned int sectors = sbc_get_write_same_sectors(cmd);
273 
274 	if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
275 		pr_err("WRITE_SAME PBDATA and LBDATA"
276 			" bits not supported for Block Discard"
277 			" Emulation\n");
278 		return TCM_UNSUPPORTED_SCSI_OPCODE;
279 	}
280 	if (sectors > cmd->se_dev->dev_attrib.max_write_same_len) {
281 		pr_warn("WRITE_SAME sectors: %u exceeds max_write_same_len: %u\n",
282 			sectors, cmd->se_dev->dev_attrib.max_write_same_len);
283 		return TCM_INVALID_CDB_FIELD;
284 	}
285 	/* We always have ANC_SUP == 0 so setting ANCHOR is always an error */
286 	if (flags[0] & 0x10) {
287 		pr_warn("WRITE SAME with ANCHOR not supported\n");
288 		return TCM_INVALID_CDB_FIELD;
289 	}
290 	/*
291 	 * Special case for WRITE_SAME w/ UNMAP=1 that ends up getting
292 	 * translated into block discard requests within backend code.
293 	 */
294 	if (flags[0] & 0x08) {
295 		if (!ops->execute_write_same_unmap)
296 			return TCM_UNSUPPORTED_SCSI_OPCODE;
297 
298 		cmd->execute_cmd = ops->execute_write_same_unmap;
299 		return 0;
300 	}
301 	if (!ops->execute_write_same)
302 		return TCM_UNSUPPORTED_SCSI_OPCODE;
303 
304 	cmd->execute_cmd = ops->execute_write_same;
305 	return 0;
306 }
307 
308 static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd)
309 {
310 	unsigned char *buf, *addr;
311 	struct scatterlist *sg;
312 	unsigned int offset;
313 	sense_reason_t ret = TCM_NO_SENSE;
314 	int i, count;
315 	/*
316 	 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
317 	 *
318 	 * 1) read the specified logical block(s);
319 	 * 2) transfer logical blocks from the data-out buffer;
320 	 * 3) XOR the logical blocks transferred from the data-out buffer with
321 	 *    the logical blocks read, storing the resulting XOR data in a buffer;
322 	 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
323 	 *    blocks transferred from the data-out buffer; and
324 	 * 5) transfer the resulting XOR data to the data-in buffer.
325 	 */
326 	buf = kmalloc(cmd->data_length, GFP_KERNEL);
327 	if (!buf) {
328 		pr_err("Unable to allocate xor_callback buf\n");
329 		return TCM_OUT_OF_RESOURCES;
330 	}
331 	/*
332 	 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
333 	 * into the locally allocated *buf
334 	 */
335 	sg_copy_to_buffer(cmd->t_data_sg,
336 			  cmd->t_data_nents,
337 			  buf,
338 			  cmd->data_length);
339 
340 	/*
341 	 * Now perform the XOR against the BIDI read memory located at
342 	 * cmd->t_mem_bidi_list
343 	 */
344 
345 	offset = 0;
346 	for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
347 		addr = kmap_atomic(sg_page(sg));
348 		if (!addr) {
349 			ret = TCM_OUT_OF_RESOURCES;
350 			goto out;
351 		}
352 
353 		for (i = 0; i < sg->length; i++)
354 			*(addr + sg->offset + i) ^= *(buf + offset + i);
355 
356 		offset += sg->length;
357 		kunmap_atomic(addr);
358 	}
359 
360 out:
361 	kfree(buf);
362 	return ret;
363 }
364 
365 static sense_reason_t
366 sbc_execute_rw(struct se_cmd *cmd)
367 {
368 	return cmd->execute_rw(cmd, cmd->t_data_sg, cmd->t_data_nents,
369 			       cmd->data_direction);
370 }
371 
372 static sense_reason_t compare_and_write_post(struct se_cmd *cmd)
373 {
374 	struct se_device *dev = cmd->se_dev;
375 
376 	/*
377 	 * Only set SCF_COMPARE_AND_WRITE_POST to force a response fall-through
378 	 * within target_complete_ok_work() if the command was successfully
379 	 * sent to the backend driver.
380 	 */
381 	spin_lock_irq(&cmd->t_state_lock);
382 	if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status)
383 		cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST;
384 	spin_unlock_irq(&cmd->t_state_lock);
385 
386 	/*
387 	 * Unlock ->caw_sem originally obtained during sbc_compare_and_write()
388 	 * before the original READ I/O submission.
389 	 */
390 	up(&dev->caw_sem);
391 
392 	return TCM_NO_SENSE;
393 }
394 
395 static sense_reason_t compare_and_write_callback(struct se_cmd *cmd)
396 {
397 	struct se_device *dev = cmd->se_dev;
398 	struct scatterlist *write_sg = NULL, *sg;
399 	unsigned char *buf = NULL, *addr;
400 	struct sg_mapping_iter m;
401 	unsigned int offset = 0, len;
402 	unsigned int nlbas = cmd->t_task_nolb;
403 	unsigned int block_size = dev->dev_attrib.block_size;
404 	unsigned int compare_len = (nlbas * block_size);
405 	sense_reason_t ret = TCM_NO_SENSE;
406 	int rc, i;
407 
408 	/*
409 	 * Handle early failure in transport_generic_request_failure(),
410 	 * which will not have taken ->caw_mutex yet..
411 	 */
412 	if (!cmd->t_data_sg || !cmd->t_bidi_data_sg)
413 		return TCM_NO_SENSE;
414 	/*
415 	 * Immediately exit + release dev->caw_sem if command has already
416 	 * been failed with a non-zero SCSI status.
417 	 */
418 	if (cmd->scsi_status) {
419 		pr_err("compare_and_write_callback: non zero scsi_status:"
420 			" 0x%02x\n", cmd->scsi_status);
421 		goto out;
422 	}
423 
424 	buf = kzalloc(cmd->data_length, GFP_KERNEL);
425 	if (!buf) {
426 		pr_err("Unable to allocate compare_and_write buf\n");
427 		ret = TCM_OUT_OF_RESOURCES;
428 		goto out;
429 	}
430 
431 	write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
432 			   GFP_KERNEL);
433 	if (!write_sg) {
434 		pr_err("Unable to allocate compare_and_write sg\n");
435 		ret = TCM_OUT_OF_RESOURCES;
436 		goto out;
437 	}
438 	sg_init_table(write_sg, cmd->t_data_nents);
439 	/*
440 	 * Setup verify and write data payloads from total NumberLBAs.
441 	 */
442 	rc = sg_copy_to_buffer(cmd->t_data_sg, cmd->t_data_nents, buf,
443 			       cmd->data_length);
444 	if (!rc) {
445 		pr_err("sg_copy_to_buffer() failed for compare_and_write\n");
446 		ret = TCM_OUT_OF_RESOURCES;
447 		goto out;
448 	}
449 	/*
450 	 * Compare against SCSI READ payload against verify payload
451 	 */
452 	for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, i) {
453 		addr = (unsigned char *)kmap_atomic(sg_page(sg));
454 		if (!addr) {
455 			ret = TCM_OUT_OF_RESOURCES;
456 			goto out;
457 		}
458 
459 		len = min(sg->length, compare_len);
460 
461 		if (memcmp(addr, buf + offset, len)) {
462 			pr_warn("Detected MISCOMPARE for addr: %p buf: %p\n",
463 				addr, buf + offset);
464 			kunmap_atomic(addr);
465 			goto miscompare;
466 		}
467 		kunmap_atomic(addr);
468 
469 		offset += len;
470 		compare_len -= len;
471 		if (!compare_len)
472 			break;
473 	}
474 
475 	i = 0;
476 	len = cmd->t_task_nolb * block_size;
477 	sg_miter_start(&m, cmd->t_data_sg, cmd->t_data_nents, SG_MITER_TO_SG);
478 	/*
479 	 * Currently assumes NoLB=1 and SGLs are PAGE_SIZE..
480 	 */
481 	while (len) {
482 		sg_miter_next(&m);
483 
484 		if (block_size < PAGE_SIZE) {
485 			sg_set_page(&write_sg[i], m.page, block_size,
486 				    block_size);
487 		} else {
488 			sg_miter_next(&m);
489 			sg_set_page(&write_sg[i], m.page, block_size,
490 				    0);
491 		}
492 		len -= block_size;
493 		i++;
494 	}
495 	sg_miter_stop(&m);
496 	/*
497 	 * Save the original SGL + nents values before updating to new
498 	 * assignments, to be released in transport_free_pages() ->
499 	 * transport_reset_sgl_orig()
500 	 */
501 	cmd->t_data_sg_orig = cmd->t_data_sg;
502 	cmd->t_data_sg = write_sg;
503 	cmd->t_data_nents_orig = cmd->t_data_nents;
504 	cmd->t_data_nents = 1;
505 
506 	cmd->sam_task_attr = MSG_HEAD_TAG;
507 	cmd->transport_complete_callback = compare_and_write_post;
508 	/*
509 	 * Now reset ->execute_cmd() to the normal sbc_execute_rw() handler
510 	 * for submitting the adjusted SGL to write instance user-data.
511 	 */
512 	cmd->execute_cmd = sbc_execute_rw;
513 
514 	spin_lock_irq(&cmd->t_state_lock);
515 	cmd->t_state = TRANSPORT_PROCESSING;
516 	cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT;
517 	spin_unlock_irq(&cmd->t_state_lock);
518 
519 	__target_execute_cmd(cmd);
520 
521 	kfree(buf);
522 	return ret;
523 
524 miscompare:
525 	pr_warn("Target/%s: Send MISCOMPARE check condition and sense\n",
526 		dev->transport->name);
527 	ret = TCM_MISCOMPARE_VERIFY;
528 out:
529 	/*
530 	 * In the MISCOMPARE or failure case, unlock ->caw_sem obtained in
531 	 * sbc_compare_and_write() before the original READ I/O submission.
532 	 */
533 	up(&dev->caw_sem);
534 	kfree(write_sg);
535 	kfree(buf);
536 	return ret;
537 }
538 
539 static sense_reason_t
540 sbc_compare_and_write(struct se_cmd *cmd)
541 {
542 	struct se_device *dev = cmd->se_dev;
543 	sense_reason_t ret;
544 	int rc;
545 	/*
546 	 * Submit the READ first for COMPARE_AND_WRITE to perform the
547 	 * comparision using SGLs at cmd->t_bidi_data_sg..
548 	 */
549 	rc = down_interruptible(&dev->caw_sem);
550 	if ((rc != 0) || signal_pending(current)) {
551 		cmd->transport_complete_callback = NULL;
552 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
553 	}
554 	/*
555 	 * Reset cmd->data_length to individual block_size in order to not
556 	 * confuse backend drivers that depend on this value matching the
557 	 * size of the I/O being submitted.
558 	 */
559 	cmd->data_length = cmd->t_task_nolb * dev->dev_attrib.block_size;
560 
561 	ret = cmd->execute_rw(cmd, cmd->t_bidi_data_sg, cmd->t_bidi_data_nents,
562 			      DMA_FROM_DEVICE);
563 	if (ret) {
564 		cmd->transport_complete_callback = NULL;
565 		up(&dev->caw_sem);
566 		return ret;
567 	}
568 	/*
569 	 * Unlock of dev->caw_sem to occur in compare_and_write_callback()
570 	 * upon MISCOMPARE, or in compare_and_write_done() upon completion
571 	 * of WRITE instance user-data.
572 	 */
573 	return TCM_NO_SENSE;
574 }
575 
576 static int
577 sbc_set_prot_op_checks(u8 protect, enum target_prot_type prot_type,
578 		       bool is_write, struct se_cmd *cmd)
579 {
580 	if (is_write) {
581 		cmd->prot_op = protect ? TARGET_PROT_DOUT_PASS :
582 					 TARGET_PROT_DOUT_INSERT;
583 		switch (protect) {
584 		case 0x0:
585 		case 0x3:
586 			cmd->prot_checks = 0;
587 			break;
588 		case 0x1:
589 		case 0x5:
590 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
591 			if (prot_type == TARGET_DIF_TYPE1_PROT)
592 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
593 			break;
594 		case 0x2:
595 			if (prot_type == TARGET_DIF_TYPE1_PROT)
596 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
597 			break;
598 		case 0x4:
599 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
600 			break;
601 		default:
602 			pr_err("Unsupported protect field %d\n", protect);
603 			return -EINVAL;
604 		}
605 	} else {
606 		cmd->prot_op = protect ? TARGET_PROT_DIN_PASS :
607 					 TARGET_PROT_DIN_STRIP;
608 		switch (protect) {
609 		case 0x0:
610 		case 0x1:
611 		case 0x5:
612 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
613 			if (prot_type == TARGET_DIF_TYPE1_PROT)
614 				cmd->prot_checks |= TARGET_DIF_CHECK_REFTAG;
615 			break;
616 		case 0x2:
617 			if (prot_type == TARGET_DIF_TYPE1_PROT)
618 				cmd->prot_checks = TARGET_DIF_CHECK_REFTAG;
619 			break;
620 		case 0x3:
621 			cmd->prot_checks = 0;
622 			break;
623 		case 0x4:
624 			cmd->prot_checks = TARGET_DIF_CHECK_GUARD;
625 			break;
626 		default:
627 			pr_err("Unsupported protect field %d\n", protect);
628 			return -EINVAL;
629 		}
630 	}
631 
632 	return 0;
633 }
634 
635 static bool
636 sbc_check_prot(struct se_device *dev, struct se_cmd *cmd, unsigned char *cdb,
637 	       u32 sectors, bool is_write)
638 {
639 	u8 protect = cdb[1] >> 5;
640 
641 	if ((!cmd->t_prot_sg || !cmd->t_prot_nents) && cmd->prot_pto)
642 		return true;
643 
644 	switch (dev->dev_attrib.pi_prot_type) {
645 	case TARGET_DIF_TYPE3_PROT:
646 		cmd->reftag_seed = 0xffffffff;
647 		break;
648 	case TARGET_DIF_TYPE2_PROT:
649 		if (protect)
650 			return false;
651 
652 		cmd->reftag_seed = cmd->t_task_lba;
653 		break;
654 	case TARGET_DIF_TYPE1_PROT:
655 		cmd->reftag_seed = cmd->t_task_lba;
656 		break;
657 	case TARGET_DIF_TYPE0_PROT:
658 	default:
659 		return true;
660 	}
661 
662 	if (sbc_set_prot_op_checks(protect, dev->dev_attrib.pi_prot_type,
663 				   is_write, cmd))
664 		return false;
665 
666 	cmd->prot_type = dev->dev_attrib.pi_prot_type;
667 	cmd->prot_length = dev->prot_length * sectors;
668 	pr_debug("%s: prot_type=%d, prot_length=%d prot_op=%d prot_checks=%d\n",
669 		 __func__, cmd->prot_type, cmd->prot_length,
670 		 cmd->prot_op, cmd->prot_checks);
671 
672 	return true;
673 }
674 
675 sense_reason_t
676 sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops)
677 {
678 	struct se_device *dev = cmd->se_dev;
679 	unsigned char *cdb = cmd->t_task_cdb;
680 	unsigned int size;
681 	u32 sectors = 0;
682 	sense_reason_t ret;
683 
684 	switch (cdb[0]) {
685 	case READ_6:
686 		sectors = transport_get_sectors_6(cdb);
687 		cmd->t_task_lba = transport_lba_21(cdb);
688 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
689 		cmd->execute_rw = ops->execute_rw;
690 		cmd->execute_cmd = sbc_execute_rw;
691 		break;
692 	case READ_10:
693 		sectors = transport_get_sectors_10(cdb);
694 		cmd->t_task_lba = transport_lba_32(cdb);
695 
696 		if (!sbc_check_prot(dev, cmd, cdb, sectors, false))
697 			return TCM_UNSUPPORTED_SCSI_OPCODE;
698 
699 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
700 		cmd->execute_rw = ops->execute_rw;
701 		cmd->execute_cmd = sbc_execute_rw;
702 		break;
703 	case READ_12:
704 		sectors = transport_get_sectors_12(cdb);
705 		cmd->t_task_lba = transport_lba_32(cdb);
706 
707 		if (!sbc_check_prot(dev, cmd, cdb, sectors, false))
708 			return TCM_UNSUPPORTED_SCSI_OPCODE;
709 
710 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
711 		cmd->execute_rw = ops->execute_rw;
712 		cmd->execute_cmd = sbc_execute_rw;
713 		break;
714 	case READ_16:
715 		sectors = transport_get_sectors_16(cdb);
716 		cmd->t_task_lba = transport_lba_64(cdb);
717 
718 		if (!sbc_check_prot(dev, cmd, cdb, sectors, false))
719 			return TCM_UNSUPPORTED_SCSI_OPCODE;
720 
721 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
722 		cmd->execute_rw = ops->execute_rw;
723 		cmd->execute_cmd = sbc_execute_rw;
724 		break;
725 	case WRITE_6:
726 		sectors = transport_get_sectors_6(cdb);
727 		cmd->t_task_lba = transport_lba_21(cdb);
728 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
729 		cmd->execute_rw = ops->execute_rw;
730 		cmd->execute_cmd = sbc_execute_rw;
731 		break;
732 	case WRITE_10:
733 	case WRITE_VERIFY:
734 		sectors = transport_get_sectors_10(cdb);
735 		cmd->t_task_lba = transport_lba_32(cdb);
736 
737 		if (!sbc_check_prot(dev, cmd, cdb, sectors, true))
738 			return TCM_UNSUPPORTED_SCSI_OPCODE;
739 
740 		if (cdb[1] & 0x8)
741 			cmd->se_cmd_flags |= SCF_FUA;
742 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
743 		cmd->execute_rw = ops->execute_rw;
744 		cmd->execute_cmd = sbc_execute_rw;
745 		break;
746 	case WRITE_12:
747 		sectors = transport_get_sectors_12(cdb);
748 		cmd->t_task_lba = transport_lba_32(cdb);
749 
750 		if (!sbc_check_prot(dev, cmd, cdb, sectors, true))
751 			return TCM_UNSUPPORTED_SCSI_OPCODE;
752 
753 		if (cdb[1] & 0x8)
754 			cmd->se_cmd_flags |= SCF_FUA;
755 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
756 		cmd->execute_rw = ops->execute_rw;
757 		cmd->execute_cmd = sbc_execute_rw;
758 		break;
759 	case WRITE_16:
760 		sectors = transport_get_sectors_16(cdb);
761 		cmd->t_task_lba = transport_lba_64(cdb);
762 
763 		if (!sbc_check_prot(dev, cmd, cdb, sectors, true))
764 			return TCM_UNSUPPORTED_SCSI_OPCODE;
765 
766 		if (cdb[1] & 0x8)
767 			cmd->se_cmd_flags |= SCF_FUA;
768 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
769 		cmd->execute_rw = ops->execute_rw;
770 		cmd->execute_cmd = sbc_execute_rw;
771 		break;
772 	case XDWRITEREAD_10:
773 		if (cmd->data_direction != DMA_TO_DEVICE ||
774 		    !(cmd->se_cmd_flags & SCF_BIDI))
775 			return TCM_INVALID_CDB_FIELD;
776 		sectors = transport_get_sectors_10(cdb);
777 
778 		cmd->t_task_lba = transport_lba_32(cdb);
779 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
780 
781 		/*
782 		 * Setup BIDI XOR callback to be run after I/O completion.
783 		 */
784 		cmd->execute_rw = ops->execute_rw;
785 		cmd->execute_cmd = sbc_execute_rw;
786 		cmd->transport_complete_callback = &xdreadwrite_callback;
787 		if (cdb[1] & 0x8)
788 			cmd->se_cmd_flags |= SCF_FUA;
789 		break;
790 	case VARIABLE_LENGTH_CMD:
791 	{
792 		u16 service_action = get_unaligned_be16(&cdb[8]);
793 		switch (service_action) {
794 		case XDWRITEREAD_32:
795 			sectors = transport_get_sectors_32(cdb);
796 
797 			/*
798 			 * Use WRITE_32 and READ_32 opcodes for the emulated
799 			 * XDWRITE_READ_32 logic.
800 			 */
801 			cmd->t_task_lba = transport_lba_64_ext(cdb);
802 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
803 
804 			/*
805 			 * Setup BIDI XOR callback to be run during after I/O
806 			 * completion.
807 			 */
808 			cmd->execute_rw = ops->execute_rw;
809 			cmd->execute_cmd = sbc_execute_rw;
810 			cmd->transport_complete_callback = &xdreadwrite_callback;
811 			if (cdb[1] & 0x8)
812 				cmd->se_cmd_flags |= SCF_FUA;
813 			break;
814 		case WRITE_SAME_32:
815 			sectors = transport_get_sectors_32(cdb);
816 			if (!sectors) {
817 				pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
818 				       " supported\n");
819 				return TCM_INVALID_CDB_FIELD;
820 			}
821 
822 			size = sbc_get_size(cmd, 1);
823 			cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
824 
825 			ret = sbc_setup_write_same(cmd, &cdb[10], ops);
826 			if (ret)
827 				return ret;
828 			break;
829 		default:
830 			pr_err("VARIABLE_LENGTH_CMD service action"
831 				" 0x%04x not supported\n", service_action);
832 			return TCM_UNSUPPORTED_SCSI_OPCODE;
833 		}
834 		break;
835 	}
836 	case COMPARE_AND_WRITE:
837 		sectors = cdb[13];
838 		/*
839 		 * Currently enforce COMPARE_AND_WRITE for a single sector
840 		 */
841 		if (sectors > 1) {
842 			pr_err("COMPARE_AND_WRITE contains NoLB: %u greater"
843 			       " than 1\n", sectors);
844 			return TCM_INVALID_CDB_FIELD;
845 		}
846 		/*
847 		 * Double size because we have two buffers, note that
848 		 * zero is not an error..
849 		 */
850 		size = 2 * sbc_get_size(cmd, sectors);
851 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
852 		cmd->t_task_nolb = sectors;
853 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB | SCF_COMPARE_AND_WRITE;
854 		cmd->execute_rw = ops->execute_rw;
855 		cmd->execute_cmd = sbc_compare_and_write;
856 		cmd->transport_complete_callback = compare_and_write_callback;
857 		break;
858 	case READ_CAPACITY:
859 		size = READ_CAP_LEN;
860 		cmd->execute_cmd = sbc_emulate_readcapacity;
861 		break;
862 	case SERVICE_ACTION_IN:
863 		switch (cmd->t_task_cdb[1] & 0x1f) {
864 		case SAI_READ_CAPACITY_16:
865 			cmd->execute_cmd = sbc_emulate_readcapacity_16;
866 			break;
867 		case SAI_REPORT_REFERRALS:
868 			cmd->execute_cmd = target_emulate_report_referrals;
869 			break;
870 		default:
871 			pr_err("Unsupported SA: 0x%02x\n",
872 				cmd->t_task_cdb[1] & 0x1f);
873 			return TCM_INVALID_CDB_FIELD;
874 		}
875 		size = (cdb[10] << 24) | (cdb[11] << 16) |
876 		       (cdb[12] << 8) | cdb[13];
877 		break;
878 	case SYNCHRONIZE_CACHE:
879 	case SYNCHRONIZE_CACHE_16:
880 		if (!ops->execute_sync_cache) {
881 			size = 0;
882 			cmd->execute_cmd = sbc_emulate_noop;
883 			break;
884 		}
885 
886 		/*
887 		 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
888 		 */
889 		if (cdb[0] == SYNCHRONIZE_CACHE) {
890 			sectors = transport_get_sectors_10(cdb);
891 			cmd->t_task_lba = transport_lba_32(cdb);
892 		} else {
893 			sectors = transport_get_sectors_16(cdb);
894 			cmd->t_task_lba = transport_lba_64(cdb);
895 		}
896 
897 		size = sbc_get_size(cmd, sectors);
898 
899 		/*
900 		 * Check to ensure that LBA + Range does not exceed past end of
901 		 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
902 		 */
903 		if (cmd->t_task_lba || sectors) {
904 			if (sbc_check_valid_sectors(cmd) < 0)
905 				return TCM_ADDRESS_OUT_OF_RANGE;
906 		}
907 		cmd->execute_cmd = ops->execute_sync_cache;
908 		break;
909 	case UNMAP:
910 		if (!ops->execute_unmap)
911 			return TCM_UNSUPPORTED_SCSI_OPCODE;
912 
913 		size = get_unaligned_be16(&cdb[7]);
914 		cmd->execute_cmd = ops->execute_unmap;
915 		break;
916 	case WRITE_SAME_16:
917 		sectors = transport_get_sectors_16(cdb);
918 		if (!sectors) {
919 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
920 			return TCM_INVALID_CDB_FIELD;
921 		}
922 
923 		size = sbc_get_size(cmd, 1);
924 		cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
925 
926 		ret = sbc_setup_write_same(cmd, &cdb[1], ops);
927 		if (ret)
928 			return ret;
929 		break;
930 	case WRITE_SAME:
931 		sectors = transport_get_sectors_10(cdb);
932 		if (!sectors) {
933 			pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
934 			return TCM_INVALID_CDB_FIELD;
935 		}
936 
937 		size = sbc_get_size(cmd, 1);
938 		cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
939 
940 		/*
941 		 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
942 		 * of byte 1 bit 3 UNMAP instead of original reserved field
943 		 */
944 		ret = sbc_setup_write_same(cmd, &cdb[1], ops);
945 		if (ret)
946 			return ret;
947 		break;
948 	case VERIFY:
949 		size = 0;
950 		cmd->execute_cmd = sbc_emulate_noop;
951 		break;
952 	case REZERO_UNIT:
953 	case SEEK_6:
954 	case SEEK_10:
955 		/*
956 		 * There are still clients out there which use these old SCSI-2
957 		 * commands. This mainly happens when running VMs with legacy
958 		 * guest systems, connected via SCSI command pass-through to
959 		 * iSCSI targets. Make them happy and return status GOOD.
960 		 */
961 		size = 0;
962 		cmd->execute_cmd = sbc_emulate_noop;
963 		break;
964 	default:
965 		ret = spc_parse_cdb(cmd, &size);
966 		if (ret)
967 			return ret;
968 	}
969 
970 	/* reject any command that we don't have a handler for */
971 	if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
972 		return TCM_UNSUPPORTED_SCSI_OPCODE;
973 
974 	if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
975 		unsigned long long end_lba;
976 
977 		if (sectors > dev->dev_attrib.fabric_max_sectors) {
978 			printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
979 				" big sectors %u exceeds fabric_max_sectors:"
980 				" %u\n", cdb[0], sectors,
981 				dev->dev_attrib.fabric_max_sectors);
982 			return TCM_INVALID_CDB_FIELD;
983 		}
984 		if (sectors > dev->dev_attrib.hw_max_sectors) {
985 			printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
986 				" big sectors %u exceeds backend hw_max_sectors:"
987 				" %u\n", cdb[0], sectors,
988 				dev->dev_attrib.hw_max_sectors);
989 			return TCM_INVALID_CDB_FIELD;
990 		}
991 
992 		end_lba = dev->transport->get_blocks(dev) + 1;
993 		if (cmd->t_task_lba + sectors > end_lba) {
994 			pr_err("cmd exceeds last lba %llu "
995 				"(lba %llu, sectors %u)\n",
996 				end_lba, cmd->t_task_lba, sectors);
997 			return TCM_ADDRESS_OUT_OF_RANGE;
998 		}
999 
1000 		if (!(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE))
1001 			size = sbc_get_size(cmd, sectors);
1002 	}
1003 
1004 	return target_cmd_size_check(cmd, size);
1005 }
1006 EXPORT_SYMBOL(sbc_parse_cdb);
1007 
1008 u32 sbc_get_device_type(struct se_device *dev)
1009 {
1010 	return TYPE_DISK;
1011 }
1012 EXPORT_SYMBOL(sbc_get_device_type);
1013 
1014 sense_reason_t
1015 sbc_execute_unmap(struct se_cmd *cmd,
1016 	sense_reason_t (*do_unmap_fn)(struct se_cmd *, void *,
1017 				      sector_t, sector_t),
1018 	void *priv)
1019 {
1020 	struct se_device *dev = cmd->se_dev;
1021 	unsigned char *buf, *ptr = NULL;
1022 	sector_t lba;
1023 	int size;
1024 	u32 range;
1025 	sense_reason_t ret = 0;
1026 	int dl, bd_dl;
1027 
1028 	/* We never set ANC_SUP */
1029 	if (cmd->t_task_cdb[1])
1030 		return TCM_INVALID_CDB_FIELD;
1031 
1032 	if (cmd->data_length == 0) {
1033 		target_complete_cmd(cmd, SAM_STAT_GOOD);
1034 		return 0;
1035 	}
1036 
1037 	if (cmd->data_length < 8) {
1038 		pr_warn("UNMAP parameter list length %u too small\n",
1039 			cmd->data_length);
1040 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
1041 	}
1042 
1043 	buf = transport_kmap_data_sg(cmd);
1044 	if (!buf)
1045 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1046 
1047 	dl = get_unaligned_be16(&buf[0]);
1048 	bd_dl = get_unaligned_be16(&buf[2]);
1049 
1050 	size = cmd->data_length - 8;
1051 	if (bd_dl > size)
1052 		pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
1053 			cmd->data_length, bd_dl);
1054 	else
1055 		size = bd_dl;
1056 
1057 	if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
1058 		ret = TCM_INVALID_PARAMETER_LIST;
1059 		goto err;
1060 	}
1061 
1062 	/* First UNMAP block descriptor starts at 8 byte offset */
1063 	ptr = &buf[8];
1064 	pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
1065 		" ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
1066 
1067 	while (size >= 16) {
1068 		lba = get_unaligned_be64(&ptr[0]);
1069 		range = get_unaligned_be32(&ptr[8]);
1070 		pr_debug("UNMAP: Using lba: %llu and range: %u\n",
1071 				 (unsigned long long)lba, range);
1072 
1073 		if (range > dev->dev_attrib.max_unmap_lba_count) {
1074 			ret = TCM_INVALID_PARAMETER_LIST;
1075 			goto err;
1076 		}
1077 
1078 		if (lba + range > dev->transport->get_blocks(dev) + 1) {
1079 			ret = TCM_ADDRESS_OUT_OF_RANGE;
1080 			goto err;
1081 		}
1082 
1083 		ret = do_unmap_fn(cmd, priv, lba, range);
1084 		if (ret)
1085 			goto err;
1086 
1087 		ptr += 16;
1088 		size -= 16;
1089 	}
1090 
1091 err:
1092 	transport_kunmap_data_sg(cmd);
1093 	if (!ret)
1094 		target_complete_cmd(cmd, GOOD);
1095 	return ret;
1096 }
1097 EXPORT_SYMBOL(sbc_execute_unmap);
1098 
1099 void
1100 sbc_dif_generate(struct se_cmd *cmd)
1101 {
1102 	struct se_device *dev = cmd->se_dev;
1103 	struct se_dif_v1_tuple *sdt;
1104 	struct scatterlist *dsg, *psg = cmd->t_prot_sg;
1105 	sector_t sector = cmd->t_task_lba;
1106 	void *daddr, *paddr;
1107 	int i, j, offset = 0;
1108 
1109 	for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) {
1110 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1111 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1112 
1113 		for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) {
1114 
1115 			if (offset >= psg->length) {
1116 				kunmap_atomic(paddr);
1117 				psg = sg_next(psg);
1118 				paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1119 				offset = 0;
1120 			}
1121 
1122 			sdt = paddr + offset;
1123 			sdt->guard_tag = cpu_to_be16(crc_t10dif(daddr + j,
1124 						dev->dev_attrib.block_size));
1125 			if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT)
1126 				sdt->ref_tag = cpu_to_be32(sector & 0xffffffff);
1127 			sdt->app_tag = 0;
1128 
1129 			pr_debug("DIF WRITE INSERT sector: %llu guard_tag: 0x%04x"
1130 				 " app_tag: 0x%04x ref_tag: %u\n",
1131 				 (unsigned long long)sector, sdt->guard_tag,
1132 				 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
1133 
1134 			sector++;
1135 			offset += sizeof(struct se_dif_v1_tuple);
1136 		}
1137 
1138 		kunmap_atomic(paddr);
1139 		kunmap_atomic(daddr);
1140 	}
1141 }
1142 
1143 static sense_reason_t
1144 sbc_dif_v1_verify(struct se_device *dev, struct se_dif_v1_tuple *sdt,
1145 		  const void *p, sector_t sector, unsigned int ei_lba)
1146 {
1147 	int block_size = dev->dev_attrib.block_size;
1148 	__be16 csum;
1149 
1150 	csum = cpu_to_be16(crc_t10dif(p, block_size));
1151 
1152 	if (sdt->guard_tag != csum) {
1153 		pr_err("DIFv1 checksum failed on sector %llu guard tag 0x%04x"
1154 			" csum 0x%04x\n", (unsigned long long)sector,
1155 			be16_to_cpu(sdt->guard_tag), be16_to_cpu(csum));
1156 		return TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
1157 	}
1158 
1159 	if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT &&
1160 	    be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
1161 		pr_err("DIFv1 Type 1 reference failed on sector: %llu tag: 0x%08x"
1162 		       " sector MSB: 0x%08x\n", (unsigned long long)sector,
1163 		       be32_to_cpu(sdt->ref_tag), (u32)(sector & 0xffffffff));
1164 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1165 	}
1166 
1167 	if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE2_PROT &&
1168 	    be32_to_cpu(sdt->ref_tag) != ei_lba) {
1169 		pr_err("DIFv1 Type 2 reference failed on sector: %llu tag: 0x%08x"
1170 		       " ei_lba: 0x%08x\n", (unsigned long long)sector,
1171 			be32_to_cpu(sdt->ref_tag), ei_lba);
1172 		return TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
1178 static void
1179 sbc_dif_copy_prot(struct se_cmd *cmd, unsigned int sectors, bool read,
1180 		  struct scatterlist *sg, int sg_off)
1181 {
1182 	struct se_device *dev = cmd->se_dev;
1183 	struct scatterlist *psg;
1184 	void *paddr, *addr;
1185 	unsigned int i, len, left;
1186 	unsigned int offset = sg_off;
1187 
1188 	left = sectors * dev->prot_length;
1189 
1190 	for_each_sg(cmd->t_prot_sg, psg, cmd->t_prot_nents, i) {
1191 		unsigned int psg_len, copied = 0;
1192 
1193 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1194 		psg_len = min(left, psg->length);
1195 		while (psg_len) {
1196 			len = min(psg_len, sg->length - offset);
1197 			addr = kmap_atomic(sg_page(sg)) + sg->offset + offset;
1198 
1199 			if (read)
1200 				memcpy(paddr + copied, addr, len);
1201 			else
1202 				memcpy(addr, paddr + copied, len);
1203 
1204 			left -= len;
1205 			offset += len;
1206 			copied += len;
1207 			psg_len -= len;
1208 
1209 			if (offset >= sg->length) {
1210 				sg = sg_next(sg);
1211 				offset = 0;
1212 			}
1213 			kunmap_atomic(addr);
1214 		}
1215 		kunmap_atomic(paddr);
1216 	}
1217 }
1218 
1219 sense_reason_t
1220 sbc_dif_verify_write(struct se_cmd *cmd, sector_t start, unsigned int sectors,
1221 		     unsigned int ei_lba, struct scatterlist *sg, int sg_off)
1222 {
1223 	struct se_device *dev = cmd->se_dev;
1224 	struct se_dif_v1_tuple *sdt;
1225 	struct scatterlist *dsg, *psg = cmd->t_prot_sg;
1226 	sector_t sector = start;
1227 	void *daddr, *paddr;
1228 	int i, j, offset = 0;
1229 	sense_reason_t rc;
1230 
1231 	for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) {
1232 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1233 		paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1234 
1235 		for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) {
1236 
1237 			if (offset >= psg->length) {
1238 				kunmap_atomic(paddr);
1239 				psg = sg_next(psg);
1240 				paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1241 				offset = 0;
1242 			}
1243 
1244 			sdt = paddr + offset;
1245 
1246 			pr_debug("DIF WRITE sector: %llu guard_tag: 0x%04x"
1247 				 " app_tag: 0x%04x ref_tag: %u\n",
1248 				 (unsigned long long)sector, sdt->guard_tag,
1249 				 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
1250 
1251 			rc = sbc_dif_v1_verify(dev, sdt, daddr + j, sector,
1252 					       ei_lba);
1253 			if (rc) {
1254 				kunmap_atomic(paddr);
1255 				kunmap_atomic(daddr);
1256 				cmd->bad_sector = sector;
1257 				return rc;
1258 			}
1259 
1260 			sector++;
1261 			ei_lba++;
1262 			offset += sizeof(struct se_dif_v1_tuple);
1263 		}
1264 
1265 		kunmap_atomic(paddr);
1266 		kunmap_atomic(daddr);
1267 	}
1268 	sbc_dif_copy_prot(cmd, sectors, false, sg, sg_off);
1269 
1270 	return 0;
1271 }
1272 EXPORT_SYMBOL(sbc_dif_verify_write);
1273 
1274 sense_reason_t
1275 sbc_dif_verify_read(struct se_cmd *cmd, sector_t start, unsigned int sectors,
1276 		    unsigned int ei_lba, struct scatterlist *sg, int sg_off)
1277 {
1278 	struct se_device *dev = cmd->se_dev;
1279 	struct se_dif_v1_tuple *sdt;
1280 	struct scatterlist *dsg, *psg = sg;
1281 	sector_t sector = start;
1282 	void *daddr, *paddr;
1283 	int i, j, offset = sg_off;
1284 	sense_reason_t rc;
1285 
1286 	for_each_sg(cmd->t_data_sg, dsg, cmd->t_data_nents, i) {
1287 		daddr = kmap_atomic(sg_page(dsg)) + dsg->offset;
1288 		paddr = kmap_atomic(sg_page(psg)) + sg->offset;
1289 
1290 		for (j = 0; j < dsg->length; j += dev->dev_attrib.block_size) {
1291 
1292 			if (offset >= psg->length) {
1293 				kunmap_atomic(paddr);
1294 				psg = sg_next(psg);
1295 				paddr = kmap_atomic(sg_page(psg)) + psg->offset;
1296 				offset = 0;
1297 			}
1298 
1299 			sdt = paddr + offset;
1300 
1301 			pr_debug("DIF READ sector: %llu guard_tag: 0x%04x"
1302 				 " app_tag: 0x%04x ref_tag: %u\n",
1303 				 (unsigned long long)sector, sdt->guard_tag,
1304 				 sdt->app_tag, be32_to_cpu(sdt->ref_tag));
1305 
1306 			if (sdt->app_tag == cpu_to_be16(0xffff)) {
1307 				sector++;
1308 				offset += sizeof(struct se_dif_v1_tuple);
1309 				continue;
1310 			}
1311 
1312 			rc = sbc_dif_v1_verify(dev, sdt, daddr + j, sector,
1313 					       ei_lba);
1314 			if (rc) {
1315 				kunmap_atomic(paddr);
1316 				kunmap_atomic(daddr);
1317 				cmd->bad_sector = sector;
1318 				return rc;
1319 			}
1320 
1321 			sector++;
1322 			ei_lba++;
1323 			offset += sizeof(struct se_dif_v1_tuple);
1324 		}
1325 
1326 		kunmap_atomic(paddr);
1327 		kunmap_atomic(daddr);
1328 	}
1329 	sbc_dif_copy_prot(cmd, sectors, true, sg, sg_off);
1330 
1331 	return 0;
1332 }
1333 EXPORT_SYMBOL(sbc_dif_verify_read);
1334