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