target_core_sbc.c 39 KB

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