target_core_sbc.c 38 KB

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