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