target_core_sbc.c 17 KB

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