target_core_sbc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/ratelimit.h>
  28. #include <asm/unaligned.h>
  29. #include <scsi/scsi.h>
  30. #include <target/target_core_base.h>
  31. #include <target/target_core_backend.h>
  32. #include <target/target_core_fabric.h>
  33. #include "target_core_internal.h"
  34. #include "target_core_ua.h"
  35. static int sbc_emulate_readcapacity(struct se_cmd *cmd)
  36. {
  37. struct se_device *dev = cmd->se_dev;
  38. unsigned long long blocks_long = dev->transport->get_blocks(dev);
  39. unsigned char *rbuf;
  40. unsigned char buf[8];
  41. u32 blocks;
  42. if (blocks_long >= 0x00000000ffffffff)
  43. blocks = 0xffffffff;
  44. else
  45. blocks = (u32)blocks_long;
  46. buf[0] = (blocks >> 24) & 0xff;
  47. buf[1] = (blocks >> 16) & 0xff;
  48. buf[2] = (blocks >> 8) & 0xff;
  49. buf[3] = blocks & 0xff;
  50. buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  51. buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  52. buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  53. buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  54. rbuf = transport_kmap_data_sg(cmd);
  55. if (rbuf) {
  56. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  57. transport_kunmap_data_sg(cmd);
  58. }
  59. target_complete_cmd(cmd, GOOD);
  60. return 0;
  61. }
  62. static int sbc_emulate_readcapacity_16(struct se_cmd *cmd)
  63. {
  64. struct se_device *dev = cmd->se_dev;
  65. unsigned char *rbuf;
  66. unsigned char buf[32];
  67. unsigned long long blocks = dev->transport->get_blocks(dev);
  68. memset(buf, 0, sizeof(buf));
  69. buf[0] = (blocks >> 56) & 0xff;
  70. buf[1] = (blocks >> 48) & 0xff;
  71. buf[2] = (blocks >> 40) & 0xff;
  72. buf[3] = (blocks >> 32) & 0xff;
  73. buf[4] = (blocks >> 24) & 0xff;
  74. buf[5] = (blocks >> 16) & 0xff;
  75. buf[6] = (blocks >> 8) & 0xff;
  76. buf[7] = blocks & 0xff;
  77. buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  78. buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  79. buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  80. buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  81. /*
  82. * Set Thin Provisioning Enable bit following sbc3r22 in section
  83. * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
  84. */
  85. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  86. buf[14] = 0x80;
  87. rbuf = transport_kmap_data_sg(cmd);
  88. if (rbuf) {
  89. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  90. transport_kunmap_data_sg(cmd);
  91. }
  92. target_complete_cmd(cmd, GOOD);
  93. return 0;
  94. }
  95. int spc_get_write_same_sectors(struct se_cmd *cmd)
  96. {
  97. u32 num_blocks;
  98. if (cmd->t_task_cdb[0] == WRITE_SAME)
  99. num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
  100. else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
  101. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
  102. else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
  103. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
  104. /*
  105. * Use the explicit range when non zero is supplied, otherwise calculate
  106. * the remaining range based on ->get_blocks() - starting LBA.
  107. */
  108. if (num_blocks)
  109. return num_blocks;
  110. return cmd->se_dev->transport->get_blocks(cmd->se_dev) -
  111. cmd->t_task_lba + 1;
  112. }
  113. EXPORT_SYMBOL(spc_get_write_same_sectors);
  114. static int sbc_emulate_verify(struct se_cmd *cmd)
  115. {
  116. target_complete_cmd(cmd, GOOD);
  117. return 0;
  118. }
  119. static int sbc_emulate_noop(struct se_cmd *cmd)
  120. {
  121. target_complete_cmd(cmd, GOOD);
  122. return 0;
  123. }
  124. static inline u32 sbc_get_size(struct se_cmd *cmd, u32 sectors)
  125. {
  126. return cmd->se_dev->se_sub_dev->se_dev_attrib.block_size * sectors;
  127. }
  128. static int sbc_check_valid_sectors(struct se_cmd *cmd)
  129. {
  130. struct se_device *dev = cmd->se_dev;
  131. unsigned long long end_lba;
  132. u32 sectors;
  133. sectors = cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size;
  134. end_lba = dev->transport->get_blocks(dev) + 1;
  135. if (cmd->t_task_lba + sectors > end_lba) {
  136. pr_err("target: lba %llu, sectors %u exceeds end lba %llu\n",
  137. cmd->t_task_lba, sectors, end_lba);
  138. return -EINVAL;
  139. }
  140. return 0;
  141. }
  142. static inline u32 transport_get_sectors_6(unsigned char *cdb)
  143. {
  144. /*
  145. * Use 8-bit sector value. SBC-3 says:
  146. *
  147. * A TRANSFER LENGTH field set to zero specifies that 256
  148. * logical blocks shall be written. Any other value
  149. * specifies the number of logical blocks that shall be
  150. * written.
  151. */
  152. return cdb[4] ? : 256;
  153. }
  154. static inline u32 transport_get_sectors_10(unsigned char *cdb)
  155. {
  156. return (u32)(cdb[7] << 8) + cdb[8];
  157. }
  158. static inline u32 transport_get_sectors_12(unsigned char *cdb)
  159. {
  160. return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
  161. }
  162. static inline u32 transport_get_sectors_16(unsigned char *cdb)
  163. {
  164. return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
  165. (cdb[12] << 8) + cdb[13];
  166. }
  167. /*
  168. * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
  169. */
  170. static inline u32 transport_get_sectors_32(unsigned char *cdb)
  171. {
  172. return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
  173. (cdb[30] << 8) + cdb[31];
  174. }
  175. static inline u32 transport_lba_21(unsigned char *cdb)
  176. {
  177. return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
  178. }
  179. static inline u32 transport_lba_32(unsigned char *cdb)
  180. {
  181. return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  182. }
  183. static inline unsigned long long transport_lba_64(unsigned char *cdb)
  184. {
  185. unsigned int __v1, __v2;
  186. __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
  187. __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  188. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  189. }
  190. /*
  191. * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
  192. */
  193. static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
  194. {
  195. unsigned int __v1, __v2;
  196. __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
  197. __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
  198. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  199. }
  200. static int sbc_write_same_supported(struct se_device *dev,
  201. unsigned char *flags)
  202. {
  203. if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
  204. pr_err("WRITE_SAME PBDATA and LBDATA"
  205. " bits not supported for Block Discard"
  206. " Emulation\n");
  207. return -ENOSYS;
  208. }
  209. /*
  210. * Currently for the emulated case we only accept
  211. * tpws with the UNMAP=1 bit set.
  212. */
  213. if (!(flags[0] & 0x08)) {
  214. pr_err("WRITE_SAME w/o UNMAP bit not"
  215. " supported for Block Discard Emulation\n");
  216. return -ENOSYS;
  217. }
  218. return 0;
  219. }
  220. static void xdreadwrite_callback(struct se_cmd *cmd)
  221. {
  222. unsigned char *buf, *addr;
  223. struct scatterlist *sg;
  224. unsigned int offset;
  225. int i;
  226. int count;
  227. /*
  228. * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
  229. *
  230. * 1) read the specified logical block(s);
  231. * 2) transfer logical blocks from the data-out buffer;
  232. * 3) XOR the logical blocks transferred from the data-out buffer with
  233. * the logical blocks read, storing the resulting XOR data in a buffer;
  234. * 4) if the DISABLE WRITE bit is set to zero, then write the logical
  235. * blocks transferred from the data-out buffer; and
  236. * 5) transfer the resulting XOR data to the data-in buffer.
  237. */
  238. buf = kmalloc(cmd->data_length, GFP_KERNEL);
  239. if (!buf) {
  240. pr_err("Unable to allocate xor_callback buf\n");
  241. return;
  242. }
  243. /*
  244. * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
  245. * into the locally allocated *buf
  246. */
  247. sg_copy_to_buffer(cmd->t_data_sg,
  248. cmd->t_data_nents,
  249. buf,
  250. cmd->data_length);
  251. /*
  252. * Now perform the XOR against the BIDI read memory located at
  253. * cmd->t_mem_bidi_list
  254. */
  255. offset = 0;
  256. for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
  257. addr = kmap_atomic(sg_page(sg));
  258. if (!addr)
  259. goto out;
  260. for (i = 0; i < sg->length; i++)
  261. *(addr + sg->offset + i) ^= *(buf + offset + i);
  262. offset += sg->length;
  263. kunmap_atomic(addr);
  264. }
  265. out:
  266. kfree(buf);
  267. }
  268. int sbc_parse_cdb(struct se_cmd *cmd, struct spc_ops *ops)
  269. {
  270. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  271. struct se_device *dev = cmd->se_dev;
  272. unsigned char *cdb = cmd->t_task_cdb;
  273. unsigned int size;
  274. u32 sectors = 0;
  275. int ret;
  276. switch (cdb[0]) {
  277. case READ_6:
  278. sectors = transport_get_sectors_6(cdb);
  279. cmd->t_task_lba = transport_lba_21(cdb);
  280. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  281. cmd->execute_cmd = ops->execute_rw;
  282. break;
  283. case READ_10:
  284. sectors = transport_get_sectors_10(cdb);
  285. cmd->t_task_lba = transport_lba_32(cdb);
  286. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  287. cmd->execute_cmd = ops->execute_rw;
  288. break;
  289. case READ_12:
  290. sectors = transport_get_sectors_12(cdb);
  291. cmd->t_task_lba = transport_lba_32(cdb);
  292. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  293. cmd->execute_cmd = ops->execute_rw;
  294. break;
  295. case READ_16:
  296. sectors = transport_get_sectors_16(cdb);
  297. cmd->t_task_lba = transport_lba_64(cdb);
  298. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  299. cmd->execute_cmd = ops->execute_rw;
  300. break;
  301. case WRITE_6:
  302. sectors = transport_get_sectors_6(cdb);
  303. cmd->t_task_lba = transport_lba_21(cdb);
  304. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  305. cmd->execute_cmd = ops->execute_rw;
  306. break;
  307. case WRITE_10:
  308. case WRITE_VERIFY:
  309. sectors = transport_get_sectors_10(cdb);
  310. cmd->t_task_lba = transport_lba_32(cdb);
  311. if (cdb[1] & 0x8)
  312. cmd->se_cmd_flags |= SCF_FUA;
  313. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  314. cmd->execute_cmd = ops->execute_rw;
  315. break;
  316. case WRITE_12:
  317. sectors = transport_get_sectors_12(cdb);
  318. cmd->t_task_lba = transport_lba_32(cdb);
  319. if (cdb[1] & 0x8)
  320. cmd->se_cmd_flags |= SCF_FUA;
  321. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  322. cmd->execute_cmd = ops->execute_rw;
  323. break;
  324. case WRITE_16:
  325. sectors = transport_get_sectors_16(cdb);
  326. cmd->t_task_lba = transport_lba_64(cdb);
  327. if (cdb[1] & 0x8)
  328. cmd->se_cmd_flags |= SCF_FUA;
  329. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  330. cmd->execute_cmd = ops->execute_rw;
  331. break;
  332. case XDWRITEREAD_10:
  333. if ((cmd->data_direction != DMA_TO_DEVICE) ||
  334. !(cmd->se_cmd_flags & SCF_BIDI))
  335. goto out_invalid_cdb_field;
  336. sectors = transport_get_sectors_10(cdb);
  337. cmd->t_task_lba = transport_lba_32(cdb);
  338. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  339. /*
  340. * Setup BIDI XOR callback to be run after I/O completion.
  341. */
  342. cmd->execute_cmd = ops->execute_rw;
  343. cmd->transport_complete_callback = &xdreadwrite_callback;
  344. if (cdb[1] & 0x8)
  345. cmd->se_cmd_flags |= SCF_FUA;
  346. break;
  347. case VARIABLE_LENGTH_CMD:
  348. {
  349. u16 service_action = get_unaligned_be16(&cdb[8]);
  350. switch (service_action) {
  351. case XDWRITEREAD_32:
  352. sectors = transport_get_sectors_32(cdb);
  353. /*
  354. * Use WRITE_32 and READ_32 opcodes for the emulated
  355. * XDWRITE_READ_32 logic.
  356. */
  357. cmd->t_task_lba = transport_lba_64_ext(cdb);
  358. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  359. /*
  360. * Setup BIDI XOR callback to be run during after I/O
  361. * completion.
  362. */
  363. cmd->execute_cmd = ops->execute_rw;
  364. cmd->transport_complete_callback = &xdreadwrite_callback;
  365. if (cdb[1] & 0x8)
  366. cmd->se_cmd_flags |= SCF_FUA;
  367. break;
  368. case WRITE_SAME_32:
  369. if (!ops->execute_write_same)
  370. goto out_unsupported_cdb;
  371. sectors = transport_get_sectors_32(cdb);
  372. if (!sectors) {
  373. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
  374. " supported\n");
  375. goto out_invalid_cdb_field;
  376. }
  377. size = sbc_get_size(cmd, 1);
  378. cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
  379. if (sbc_write_same_supported(dev, &cdb[10]) < 0)
  380. goto out_unsupported_cdb;
  381. cmd->execute_cmd = ops->execute_write_same;
  382. break;
  383. default:
  384. pr_err("VARIABLE_LENGTH_CMD service action"
  385. " 0x%04x not supported\n", service_action);
  386. goto out_unsupported_cdb;
  387. }
  388. break;
  389. }
  390. case READ_CAPACITY:
  391. size = READ_CAP_LEN;
  392. cmd->execute_cmd = sbc_emulate_readcapacity;
  393. break;
  394. case SERVICE_ACTION_IN:
  395. switch (cmd->t_task_cdb[1] & 0x1f) {
  396. case SAI_READ_CAPACITY_16:
  397. cmd->execute_cmd = sbc_emulate_readcapacity_16;
  398. break;
  399. default:
  400. pr_err("Unsupported SA: 0x%02x\n",
  401. cmd->t_task_cdb[1] & 0x1f);
  402. goto out_invalid_cdb_field;
  403. }
  404. size = (cdb[10] << 24) | (cdb[11] << 16) |
  405. (cdb[12] << 8) | cdb[13];
  406. break;
  407. case SYNCHRONIZE_CACHE:
  408. case SYNCHRONIZE_CACHE_16:
  409. if (!ops->execute_sync_cache)
  410. goto out_unsupported_cdb;
  411. /*
  412. * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
  413. */
  414. if (cdb[0] == SYNCHRONIZE_CACHE) {
  415. sectors = transport_get_sectors_10(cdb);
  416. cmd->t_task_lba = transport_lba_32(cdb);
  417. } else {
  418. sectors = transport_get_sectors_16(cdb);
  419. cmd->t_task_lba = transport_lba_64(cdb);
  420. }
  421. size = sbc_get_size(cmd, sectors);
  422. /*
  423. * Check to ensure that LBA + Range does not exceed past end of
  424. * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
  425. */
  426. if (cmd->t_task_lba || sectors) {
  427. if (sbc_check_valid_sectors(cmd) < 0)
  428. goto out_invalid_cdb_field;
  429. }
  430. cmd->execute_cmd = ops->execute_sync_cache;
  431. break;
  432. case UNMAP:
  433. if (!ops->execute_unmap)
  434. goto out_unsupported_cdb;
  435. size = get_unaligned_be16(&cdb[7]);
  436. cmd->execute_cmd = ops->execute_unmap;
  437. break;
  438. case WRITE_SAME_16:
  439. if (!ops->execute_write_same)
  440. goto out_unsupported_cdb;
  441. sectors = transport_get_sectors_16(cdb);
  442. if (!sectors) {
  443. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  444. goto out_invalid_cdb_field;
  445. }
  446. size = sbc_get_size(cmd, 1);
  447. cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
  448. if (sbc_write_same_supported(dev, &cdb[1]) < 0)
  449. goto out_unsupported_cdb;
  450. cmd->execute_cmd = ops->execute_write_same;
  451. break;
  452. case WRITE_SAME:
  453. if (!ops->execute_write_same)
  454. goto out_unsupported_cdb;
  455. sectors = transport_get_sectors_10(cdb);
  456. if (!sectors) {
  457. pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
  458. goto out_invalid_cdb_field;
  459. }
  460. size = sbc_get_size(cmd, 1);
  461. cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
  462. /*
  463. * Follow sbcr26 with WRITE_SAME (10) and check for the existence
  464. * of byte 1 bit 3 UNMAP instead of original reserved field
  465. */
  466. if (sbc_write_same_supported(dev, &cdb[1]) < 0)
  467. goto out_unsupported_cdb;
  468. cmd->execute_cmd = ops->execute_write_same;
  469. break;
  470. case VERIFY:
  471. size = 0;
  472. cmd->execute_cmd = sbc_emulate_verify;
  473. break;
  474. case REZERO_UNIT:
  475. case SEEK_6:
  476. case SEEK_10:
  477. /*
  478. * There are still clients out there which use these old SCSI-2
  479. * commands. This mainly happens when running VMs with legacy
  480. * guest systems, connected via SCSI command pass-through to
  481. * iSCSI targets. Make them happy and return status GOOD.
  482. */
  483. size = 0;
  484. cmd->execute_cmd = sbc_emulate_noop;
  485. break;
  486. default:
  487. ret = spc_parse_cdb(cmd, &size);
  488. if (ret)
  489. return ret;
  490. }
  491. /* reject any command that we don't have a handler for */
  492. if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && !cmd->execute_cmd)
  493. goto out_unsupported_cdb;
  494. if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
  495. unsigned long long end_lba;
  496. if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
  497. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  498. " big sectors %u exceeds fabric_max_sectors:"
  499. " %u\n", cdb[0], sectors,
  500. su_dev->se_dev_attrib.fabric_max_sectors);
  501. goto out_invalid_cdb_field;
  502. }
  503. if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
  504. printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
  505. " big sectors %u exceeds backend hw_max_sectors:"
  506. " %u\n", cdb[0], sectors,
  507. su_dev->se_dev_attrib.hw_max_sectors);
  508. goto out_invalid_cdb_field;
  509. }
  510. end_lba = dev->transport->get_blocks(dev) + 1;
  511. if (cmd->t_task_lba + sectors > end_lba) {
  512. pr_err("cmd exceeds last lba %llu "
  513. "(lba %llu, sectors %u)\n",
  514. end_lba, cmd->t_task_lba, sectors);
  515. goto out_invalid_cdb_field;
  516. }
  517. size = sbc_get_size(cmd, sectors);
  518. }
  519. ret = target_cmd_size_check(cmd, size);
  520. if (ret < 0)
  521. return ret;
  522. return 0;
  523. out_unsupported_cdb:
  524. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  525. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  526. return -EINVAL;
  527. out_invalid_cdb_field:
  528. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  529. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  530. return -EINVAL;
  531. }
  532. EXPORT_SYMBOL(sbc_parse_cdb);