target_core_sbc.c 38 KB

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