target: move code for CDB emulation
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / target / target_core_sbc.c
1 /*
2  * SCSI Block Commands (SBC) parsing and emulation.
3  *
4  * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
5  * Copyright (c) 2005, 2006, 2007 SBE, Inc.
6  * Copyright (c) 2007-2010 Rising Tide Systems
7  * Copyright (c) 2008-2010 Linux-iSCSI.org
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/ratelimit.h>
29 #include <asm/unaligned.h>
30 #include <scsi/scsi.h>
31
32 #include <target/target_core_base.h>
33 #include <target/target_core_backend.h>
34 #include <target/target_core_fabric.h>
35
36 #include "target_core_internal.h"
37 #include "target_core_ua.h"
38
39
40 static int sbc_emulate_readcapacity(struct se_cmd *cmd)
41 {
42         struct se_device *dev = cmd->se_dev;
43         unsigned char *buf;
44         unsigned long long blocks_long = dev->transport->get_blocks(dev);
45         u32 blocks;
46
47         if (blocks_long >= 0x00000000ffffffff)
48                 blocks = 0xffffffff;
49         else
50                 blocks = (u32)blocks_long;
51
52         buf = transport_kmap_data_sg(cmd);
53
54         buf[0] = (blocks >> 24) & 0xff;
55         buf[1] = (blocks >> 16) & 0xff;
56         buf[2] = (blocks >> 8) & 0xff;
57         buf[3] = blocks & 0xff;
58         buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
59         buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
60         buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
61         buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
62
63         transport_kunmap_data_sg(cmd);
64
65         target_complete_cmd(cmd, GOOD);
66         return 0;
67 }
68
69 static int sbc_emulate_readcapacity_16(struct se_cmd *cmd)
70 {
71         struct se_device *dev = cmd->se_dev;
72         unsigned char *buf;
73         unsigned long long blocks = dev->transport->get_blocks(dev);
74
75         buf = transport_kmap_data_sg(cmd);
76
77         buf[0] = (blocks >> 56) & 0xff;
78         buf[1] = (blocks >> 48) & 0xff;
79         buf[2] = (blocks >> 40) & 0xff;
80         buf[3] = (blocks >> 32) & 0xff;
81         buf[4] = (blocks >> 24) & 0xff;
82         buf[5] = (blocks >> 16) & 0xff;
83         buf[6] = (blocks >> 8) & 0xff;
84         buf[7] = blocks & 0xff;
85         buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
86         buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
87         buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
88         buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
89         /*
90          * Set Thin Provisioning Enable bit following sbc3r22 in section
91          * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
92          */
93         if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
94                 buf[14] = 0x80;
95
96         transport_kunmap_data_sg(cmd);
97
98         target_complete_cmd(cmd, GOOD);
99         return 0;
100 }
101
102 /*
103  * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
104  * Note this is not used for TCM/pSCSI passthrough
105  */
106 static int sbc_emulate_unmap(struct se_cmd *cmd)
107 {
108         struct se_device *dev = cmd->se_dev;
109         unsigned char *buf, *ptr = NULL;
110         unsigned char *cdb = &cmd->t_task_cdb[0];
111         sector_t lba;
112         unsigned int size = cmd->data_length, range;
113         int ret = 0, offset;
114         unsigned short dl, bd_dl;
115
116         if (!dev->transport->do_discard) {
117                 pr_err("UNMAP emulation not supported for: %s\n",
118                                 dev->transport->name);
119                 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
120                 return -ENOSYS;
121         }
122
123         /* First UNMAP block descriptor starts at 8 byte offset */
124         offset = 8;
125         size -= 8;
126         dl = get_unaligned_be16(&cdb[0]);
127         bd_dl = get_unaligned_be16(&cdb[2]);
128
129         buf = transport_kmap_data_sg(cmd);
130
131         ptr = &buf[offset];
132         pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
133                 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
134
135         while (size) {
136                 lba = get_unaligned_be64(&ptr[0]);
137                 range = get_unaligned_be32(&ptr[8]);
138                 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
139                                  (unsigned long long)lba, range);
140
141                 ret = dev->transport->do_discard(dev, lba, range);
142                 if (ret < 0) {
143                         pr_err("blkdev_issue_discard() failed: %d\n",
144                                         ret);
145                         goto err;
146                 }
147
148                 ptr += 16;
149                 size -= 16;
150         }
151
152 err:
153         transport_kunmap_data_sg(cmd);
154         if (!ret)
155                 target_complete_cmd(cmd, GOOD);
156         return ret;
157 }
158
159 /*
160  * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
161  * Note this is not used for TCM/pSCSI passthrough
162  */
163 static int sbc_emulate_write_same(struct se_cmd *cmd)
164 {
165         struct se_device *dev = cmd->se_dev;
166         sector_t range;
167         sector_t lba = cmd->t_task_lba;
168         u32 num_blocks;
169         int ret;
170
171         if (!dev->transport->do_discard) {
172                 pr_err("WRITE_SAME emulation not supported"
173                                 " for: %s\n", dev->transport->name);
174                 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
175                 return -ENOSYS;
176         }
177
178         if (cmd->t_task_cdb[0] == WRITE_SAME)
179                 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
180         else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
181                 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
182         else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
183                 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
184
185         /*
186          * Use the explicit range when non zero is supplied, otherwise calculate
187          * the remaining range based on ->get_blocks() - starting LBA.
188          */
189         if (num_blocks != 0)
190                 range = num_blocks;
191         else
192                 range = (dev->transport->get_blocks(dev) - lba) + 1;
193
194         pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
195                  (unsigned long long)lba, (unsigned long long)range);
196
197         ret = dev->transport->do_discard(dev, lba, range);
198         if (ret < 0) {
199                 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
200                 return ret;
201         }
202
203         target_complete_cmd(cmd, GOOD);
204         return 0;
205 }
206
207 static int sbc_emulate_synchronize_cache(struct se_cmd *cmd)
208 {
209         if (!cmd->se_dev->transport->do_sync_cache) {
210                 pr_err("SYNCHRONIZE_CACHE emulation not supported"
211                         " for: %s\n", cmd->se_dev->transport->name);
212                 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
213                 return -ENOSYS;
214         }
215
216         cmd->se_dev->transport->do_sync_cache(cmd);
217         return 0;
218 }
219
220 static int sbc_emulate_verify(struct se_cmd *cmd)
221 {
222         target_complete_cmd(cmd, GOOD);
223         return 0;
224 }
225
226 static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
227 {
228         return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors;
229 }
230
231 static int sbc_check_valid_sectors(struct se_cmd *cmd)
232 {
233         struct se_device *dev = cmd->se_dev;
234         unsigned long long end_lba;
235         u32 sectors;
236
237         sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size;
238         end_lba = dev->transport->get_blocks(dev) + 1;
239
240         if (cmd->t_task_lba + sectors > end_lba) {
241                 pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
242                         cmd->t_task_lba, sectors, end_lba);
243                 return -EINVAL;
244         }
245
246         return 0;
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 int sbc_write_same_supported(struct se_device *dev,
322                 unsigned char *flags)
323 {
324         if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
325                 pr_err("WRITE_SAME PBDATA and LBDATA"
326                         " bits not supported for Block Discard"
327                         " Emulation\n");
328                 return -ENOSYS;
329         }
330
331         /*
332          * Currently for the emulated case we only accept
333          * tpws with the UNMAP=1 bit set.
334          */
335         if (!(flags[0] & 0x08)) {
336                 pr_err("WRITE_SAME w/o UNMAP bit not"
337                         " supported for Block Discard Emulation\n");
338                 return -ENOSYS;
339         }
340
341         return 0;
342 }
343
344 static void xdreadwrite_callback(struct se_cmd *cmd)
345 {
346         unsigned char *buf, *addr;
347         struct scatterlist *sg;
348         unsigned int offset;
349         int i;
350         int count;
351         /*
352          * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
353          *
354          * 1) read the specified logical block(s);
355          * 2) transfer logical blocks from the data-out buffer;
356          * 3) XOR the logical blocks transferred from the data-out buffer with
357          *    the logical blocks read, storing the resulting XOR data in a buffer;
358          * 4) if the DISABLE WRITE bit is set to zero, then write the logical
359          *    blocks transferred from the data-out buffer; and
360          * 5) transfer the resulting XOR data to the data-in buffer.
361          */
362         buf = kmalloc(cmd->data_length, GFP_KERNEL);
363         if (!buf) {
364                 pr_err("Unable to allocate xor_callback buf\n");
365                 return;
366         }
367         /*
368          * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
369          * into the locally allocated *buf
370          */
371         sg_copy_to_buffer(cmd->t_data_sg,
372                           cmd->t_data_nents,
373                           buf,
374                           cmd->data_length);
375
376         /*
377          * Now perform the XOR against the BIDI read memory located at
378          * cmd->t_mem_bidi_list
379          */
380
381         offset = 0;
382         for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
383                 addr = kmap_atomic(sg_page(sg));
384                 if (!addr)
385                         goto out;
386
387                 for (i = 0; i < sg->length; i++)
388                         *(addr + sg->offset + i) ^= *(buf + offset + i);
389
390                 offset += sg->length;
391                 kunmap_atomic(addr);
392         }
393
394 out:
395         kfree(buf);
396 }
397
398 int sbc_parse_cdb(struct se_cmd *cmd)
399 {
400         struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
401         struct se_device *dev = cmd->se_dev;
402         unsigned char *cdb = cmd->t_task_cdb;
403         unsigned int size;
404         u32 sectors = 0;
405         int ret;
406
407         switch (cdb[0]) {
408         case READ_6:
409                 sectors = transport_get_sectors_6(cdb);
410                 cmd->t_task_lba = transport_lba_21(cdb);
411                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
412                 break;
413         case READ_10:
414                 sectors = transport_get_sectors_10(cdb);
415                 cmd->t_task_lba = transport_lba_32(cdb);
416                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
417                 break;
418         case READ_12:
419                 sectors = transport_get_sectors_12(cdb);
420                 cmd->t_task_lba = transport_lba_32(cdb);
421                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
422                 break;
423         case READ_16:
424                 sectors = transport_get_sectors_16(cdb);
425                 cmd->t_task_lba = transport_lba_64(cdb);
426                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
427                 break;
428         case WRITE_6:
429                 sectors = transport_get_sectors_6(cdb);
430                 cmd->t_task_lba = transport_lba_21(cdb);
431                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
432                 break;
433         case WRITE_10:
434         case WRITE_VERIFY:
435                 sectors = transport_get_sectors_10(cdb);
436                 cmd->t_task_lba = transport_lba_32(cdb);
437                 if (cdb[1] & 0x8)
438                         cmd->se_cmd_flags |= SCF_FUA;
439                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
440                 break;
441         case WRITE_12:
442                 sectors = transport_get_sectors_12(cdb);
443                 cmd->t_task_lba = transport_lba_32(cdb);
444                 if (cdb[1] & 0x8)
445                         cmd->se_cmd_flags |= SCF_FUA;
446                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
447                 break;
448         case WRITE_16:
449                 sectors = transport_get_sectors_16(cdb);
450                 cmd->t_task_lba = transport_lba_64(cdb);
451                 if (cdb[1] & 0x8)
452                         cmd->se_cmd_flags |= SCF_FUA;
453                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
454                 break;
455         case XDWRITEREAD_10:
456                 if ((cmd->data_direction != DMA_TO_DEVICE) ||
457                     !(cmd->se_cmd_flags & SCF_BIDI))
458                         goto out_invalid_cdb_field;
459                 sectors = transport_get_sectors_10(cdb);
460
461                 cmd->t_task_lba = transport_lba_32(cdb);
462                 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
463
464                 /*
465                  * Setup BIDI XOR callback to be run after I/O completion.
466                  */
467                 cmd->transport_complete_callback = &xdreadwrite_callback;
468                 if (cdb[1] & 0x8)
469                         cmd->se_cmd_flags |= SCF_FUA;
470                 break;
471         case VARIABLE_LENGTH_CMD:
472         {
473                 u16 service_action = get_unaligned_be16(&cdb[8]);
474                 switch (service_action) {
475                 case XDWRITEREAD_32:
476                         sectors = transport_get_sectors_32(cdb);
477
478                         /*
479                          * Use WRITE_32 and READ_32 opcodes for the emulated
480                          * XDWRITE_READ_32 logic.
481                          */
482                         cmd->t_task_lba = transport_lba_64_ext(cdb);
483                         cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
484
485                         /*
486                          * Setup BIDI XOR callback to be run during after I/O
487                          * completion.
488                          */
489                         cmd->transport_complete_callback = &xdreadwrite_callback;
490                         if (cdb[1] & 0x8)
491                                 cmd->se_cmd_flags |= SCF_FUA;
492                         break;
493                 case WRITE_SAME_32:
494                         sectors = transport_get_sectors_32(cdb);
495                         if (!sectors) {
496                                 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
497                                        " supported\n");
498                                 goto out_invalid_cdb_field;
499                         }
500
501                         size = sbc_get_size(cmd, 1);
502                         cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
503
504                         if (sbc_write_same_supported(dev, &cdb[10]) < 0)
505                                 goto out_unsupported_cdb;
506                         cmd->execute_cmd = sbc_emulate_write_same;
507                         break;
508                 default:
509                         pr_err("VARIABLE_LENGTH_CMD service action"
510                                 " 0x%04x not supported\n", service_action);
511                         goto out_unsupported_cdb;
512                 }
513                 break;
514         }
515         case READ_CAPACITY:
516                 size = READ_CAP_LEN;
517                 cmd->execute_cmd = sbc_emulate_readcapacity;
518                 break;
519         case SERVICE_ACTION_IN:
520                 switch (cmd->t_task_cdb[1] & 0x1f) {
521                 case SAI_READ_CAPACITY_16:
522                         cmd->execute_cmd = sbc_emulate_readcapacity_16;
523                         break;
524                 default:
525                         pr_err("Unsupported SA: 0x%02x\n",
526                                 cmd->t_task_cdb[1] & 0x1f);
527                         goto out_invalid_cdb_field;
528                 }
529                 size = (cdb[10] << 24) | (cdb[11] << 16) |
530                        (cdb[12] << 8) | cdb[13];
531                 break;
532         case SYNCHRONIZE_CACHE:
533         case SYNCHRONIZE_CACHE_16:
534                 /*
535                  * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
536                  */
537                 if (cdb[0] == SYNCHRONIZE_CACHE) {
538                         sectors = transport_get_sectors_10(cdb);
539                         cmd->t_task_lba = transport_lba_32(cdb);
540                 } else {
541                         sectors = transport_get_sectors_16(cdb);
542                         cmd->t_task_lba = transport_lba_64(cdb);
543                 }
544
545                 size = sbc_get_size(cmd, sectors);
546
547                 /*
548                  * Check to ensure that LBA + Range does not exceed past end of
549                  * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
550                  */
551                 if (cmd->t_task_lba || sectors) {
552                         if (sbc_check_valid_sectors(cmd) < 0)
553                                 goto out_invalid_cdb_field;
554                 }
555                 cmd->execute_cmd = sbc_emulate_synchronize_cache;
556                 break;
557         case UNMAP:
558                 size = get_unaligned_be16(&cdb[7]);
559                 cmd->execute_cmd = sbc_emulate_unmap;
560                 break;
561         case WRITE_SAME_16:
562                 sectors = transport_get_sectors_16(cdb);
563                 if (!sectors) {
564                         pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
565                         goto out_invalid_cdb_field;
566                 }
567
568                 size = sbc_get_size(cmd, 1);
569                 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
570
571                 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
572                         goto out_unsupported_cdb;
573                 cmd->execute_cmd = sbc_emulate_write_same;
574                 break;
575         case WRITE_SAME:
576                 sectors = transport_get_sectors_10(cdb);
577                 if (!sectors) {
578                         pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
579                         goto out_invalid_cdb_field;
580                 }
581
582                 size = sbc_get_size(cmd, 1);
583                 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
584
585                 /*
586                  * Follow sbcr26 with WRITE_SAME (10) and check for the existence
587                  * of byte 1 bit 3 UNMAP instead of original reserved field
588                  */
589                 if (sbc_write_same_supported(dev, &cdb[1]) < 0)
590                         goto out_unsupported_cdb;
591                 cmd->execute_cmd = sbc_emulate_write_same;
592                 break;
593         case VERIFY:
594                 size = 0;
595                 cmd->execute_cmd = sbc_emulate_verify;
596                 break;
597         default:
598                 ret = spc_parse_cdb(cmd, &size);
599                 if (ret)
600                         return ret;
601         }
602
603         /* reject any command that we don't have a handler for */
604         if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
605                 goto out_unsupported_cdb;
606
607         if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
608                 unsigned long long end_lba;
609
610                 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
611                         printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
612                                 " big sectors %u exceeds fabric_max_sectors:"
613                                 " %u\n", cdb[0], sectors,
614                                 su_dev->se_dev_attrib.fabric_max_sectors);
615                         goto out_invalid_cdb_field;
616                 }
617                 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
618                         printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
619                                 " big sectors %u exceeds backend hw_max_sectors:"
620                                 " %u\n", cdb[0], sectors,
621                                 su_dev->se_dev_attrib.hw_max_sectors);
622                         goto out_invalid_cdb_field;
623                 }
624
625                 end_lba = dev->transport->get_blocks(dev) + 1;
626                 if (cmd->t_task_lba + sectors > end_lba) {
627                         pr_err("cmd exceeds last lba %llu "
628                                 "(lba %llu, sectors %u)\n",
629                                 end_lba, cmd->t_task_lba, sectors);
630                         goto out_invalid_cdb_field;
631                 }
632
633                 size = sbc_get_size(cmd, sectors);
634         }
635
636         ret = target_cmd_size_check(cmd, size);
637         if (ret < 0)
638                 return ret;
639
640         return 0;
641
642 out_unsupported_cdb:
643         cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
644         cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
645         return -EINVAL;
646 out_invalid_cdb_field:
647         cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
648         cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
649         return -EINVAL;
650 }
651 EXPORT_SYMBOL(sbc_parse_cdb);