target_core_sbc.c 39 KB

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