target_core_sbc.c 39 KB

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