target_core_sbc.c 39 KB

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