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