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