target_core_cdb.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * CDB emulation for non-READ/WRITE commands.
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
  5. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  6. * Copyright (c) 2007-2010 Rising Tide Systems
  7. * Copyright (c) 2008-2010 Linux-iSCSI.org
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <asm/unaligned.h>
  28. #include <scsi/scsi.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. static void
  35. target_fill_alua_data(struct se_port *port, unsigned char *buf)
  36. {
  37. struct t10_alua_tg_pt_gp *tg_pt_gp;
  38. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  39. /*
  40. * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
  41. */
  42. buf[5] = 0x80;
  43. /*
  44. * Set TPGS field for explict and/or implict ALUA access type
  45. * and opteration.
  46. *
  47. * See spc4r17 section 6.4.2 Table 135
  48. */
  49. if (!port)
  50. return;
  51. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  52. if (!tg_pt_gp_mem)
  53. return;
  54. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  55. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  56. if (tg_pt_gp)
  57. buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
  58. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  59. }
  60. static int
  61. target_emulate_inquiry_std(struct se_cmd *cmd, char *buf)
  62. {
  63. struct se_lun *lun = cmd->se_lun;
  64. struct se_device *dev = cmd->se_dev;
  65. /* Set RMB (removable media) for tape devices */
  66. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  67. buf[1] = 0x80;
  68. buf[2] = dev->transport->get_device_rev(dev);
  69. /*
  70. * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
  71. *
  72. * SPC4 says:
  73. * A RESPONSE DATA FORMAT field set to 2h indicates that the
  74. * standard INQUIRY data is in the format defined in this
  75. * standard. Response data format values less than 2h are
  76. * obsolete. Response data format values greater than 2h are
  77. * reserved.
  78. */
  79. buf[3] = 2;
  80. /*
  81. * Enable SCCS and TPGS fields for Emulated ALUA
  82. */
  83. if (dev->se_sub_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED)
  84. target_fill_alua_data(lun->lun_sep, buf);
  85. buf[7] = 0x2; /* CmdQue=1 */
  86. snprintf(&buf[8], 8, "LIO-ORG");
  87. snprintf(&buf[16], 16, "%s", dev->se_sub_dev->t10_wwn.model);
  88. snprintf(&buf[32], 4, "%s", dev->se_sub_dev->t10_wwn.revision);
  89. buf[4] = 31; /* Set additional length to 31 */
  90. return 0;
  91. }
  92. /* unit serial number */
  93. static int
  94. target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  95. {
  96. struct se_device *dev = cmd->se_dev;
  97. u16 len = 0;
  98. if (dev->se_sub_dev->su_dev_flags &
  99. SDF_EMULATED_VPD_UNIT_SERIAL) {
  100. u32 unit_serial_len;
  101. unit_serial_len = strlen(dev->se_sub_dev->t10_wwn.unit_serial);
  102. unit_serial_len++; /* For NULL Terminator */
  103. len += sprintf(&buf[4], "%s",
  104. dev->se_sub_dev->t10_wwn.unit_serial);
  105. len++; /* Extra Byte for NULL Terminator */
  106. buf[3] = len;
  107. }
  108. return 0;
  109. }
  110. static void
  111. target_parse_naa_6h_vendor_specific(struct se_device *dev, unsigned char *buf)
  112. {
  113. unsigned char *p = &dev->se_sub_dev->t10_wwn.unit_serial[0];
  114. int cnt;
  115. bool next = true;
  116. /*
  117. * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
  118. * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
  119. * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
  120. * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
  121. * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
  122. * per device uniqeness.
  123. */
  124. for (cnt = 0; *p && cnt < 13; p++) {
  125. int val = hex_to_bin(*p);
  126. if (val < 0)
  127. continue;
  128. if (next) {
  129. next = false;
  130. buf[cnt++] |= val;
  131. } else {
  132. next = true;
  133. buf[cnt] = val << 4;
  134. }
  135. }
  136. }
  137. /*
  138. * Device identification VPD, for a complete list of
  139. * DESIGNATOR TYPEs see spc4r17 Table 459.
  140. */
  141. static int
  142. target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  143. {
  144. struct se_device *dev = cmd->se_dev;
  145. struct se_lun *lun = cmd->se_lun;
  146. struct se_port *port = NULL;
  147. struct se_portal_group *tpg = NULL;
  148. struct t10_alua_lu_gp_member *lu_gp_mem;
  149. struct t10_alua_tg_pt_gp *tg_pt_gp;
  150. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  151. unsigned char *prod = &dev->se_sub_dev->t10_wwn.model[0];
  152. u32 prod_len;
  153. u32 unit_serial_len, off = 0;
  154. u16 len = 0, id_len;
  155. off = 4;
  156. /*
  157. * NAA IEEE Registered Extended Assigned designator format, see
  158. * spc4r17 section 7.7.3.6.5
  159. *
  160. * We depend upon a target_core_mod/ConfigFS provided
  161. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  162. * value in order to return the NAA id.
  163. */
  164. if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
  165. goto check_t10_vend_desc;
  166. /* CODE SET == Binary */
  167. buf[off++] = 0x1;
  168. /* Set ASSOCIATION == addressed logical unit: 0)b */
  169. buf[off] = 0x00;
  170. /* Identifier/Designator type == NAA identifier */
  171. buf[off++] |= 0x3;
  172. off++;
  173. /* Identifier/Designator length */
  174. buf[off++] = 0x10;
  175. /*
  176. * Start NAA IEEE Registered Extended Identifier/Designator
  177. */
  178. buf[off++] = (0x6 << 4);
  179. /*
  180. * Use OpenFabrics IEEE Company ID: 00 14 05
  181. */
  182. buf[off++] = 0x01;
  183. buf[off++] = 0x40;
  184. buf[off] = (0x5 << 4);
  185. /*
  186. * Return ConfigFS Unit Serial Number information for
  187. * VENDOR_SPECIFIC_IDENTIFIER and
  188. * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
  189. */
  190. target_parse_naa_6h_vendor_specific(dev, &buf[off]);
  191. len = 20;
  192. off = (len + 4);
  193. check_t10_vend_desc:
  194. /*
  195. * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
  196. */
  197. id_len = 8; /* For Vendor field */
  198. prod_len = 4; /* For VPD Header */
  199. prod_len += 8; /* For Vendor field */
  200. prod_len += strlen(prod);
  201. prod_len++; /* For : */
  202. if (dev->se_sub_dev->su_dev_flags &
  203. SDF_EMULATED_VPD_UNIT_SERIAL) {
  204. unit_serial_len =
  205. strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
  206. unit_serial_len++; /* For NULL Terminator */
  207. id_len += sprintf(&buf[off+12], "%s:%s", prod,
  208. &dev->se_sub_dev->t10_wwn.unit_serial[0]);
  209. }
  210. buf[off] = 0x2; /* ASCII */
  211. buf[off+1] = 0x1; /* T10 Vendor ID */
  212. buf[off+2] = 0x0;
  213. memcpy(&buf[off+4], "LIO-ORG", 8);
  214. /* Extra Byte for NULL Terminator */
  215. id_len++;
  216. /* Identifier Length */
  217. buf[off+3] = id_len;
  218. /* Header size for Designation descriptor */
  219. len += (id_len + 4);
  220. off += (id_len + 4);
  221. /*
  222. * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
  223. */
  224. port = lun->lun_sep;
  225. if (port) {
  226. struct t10_alua_lu_gp *lu_gp;
  227. u32 padding, scsi_name_len;
  228. u16 lu_gp_id = 0;
  229. u16 tg_pt_gp_id = 0;
  230. u16 tpgt;
  231. tpg = port->sep_tpg;
  232. /*
  233. * Relative target port identifer, see spc4r17
  234. * section 7.7.3.7
  235. *
  236. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  237. * section 7.5.1 Table 362
  238. */
  239. buf[off] =
  240. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  241. buf[off++] |= 0x1; /* CODE SET == Binary */
  242. buf[off] = 0x80; /* Set PIV=1 */
  243. /* Set ASSOCIATION == target port: 01b */
  244. buf[off] |= 0x10;
  245. /* DESIGNATOR TYPE == Relative target port identifer */
  246. buf[off++] |= 0x4;
  247. off++; /* Skip over Reserved */
  248. buf[off++] = 4; /* DESIGNATOR LENGTH */
  249. /* Skip over Obsolete field in RTPI payload
  250. * in Table 472 */
  251. off += 2;
  252. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  253. buf[off++] = (port->sep_rtpi & 0xff);
  254. len += 8; /* Header size + Designation descriptor */
  255. /*
  256. * Target port group identifier, see spc4r17
  257. * section 7.7.3.8
  258. *
  259. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  260. * section 7.5.1 Table 362
  261. */
  262. if (dev->se_sub_dev->t10_alua.alua_type !=
  263. SPC3_ALUA_EMULATED)
  264. goto check_scsi_name;
  265. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  266. if (!tg_pt_gp_mem)
  267. goto check_lu_gp;
  268. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  269. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  270. if (!tg_pt_gp) {
  271. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  272. goto check_lu_gp;
  273. }
  274. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  275. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  276. buf[off] =
  277. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  278. buf[off++] |= 0x1; /* CODE SET == Binary */
  279. buf[off] = 0x80; /* Set PIV=1 */
  280. /* Set ASSOCIATION == target port: 01b */
  281. buf[off] |= 0x10;
  282. /* DESIGNATOR TYPE == Target port group identifier */
  283. buf[off++] |= 0x5;
  284. off++; /* Skip over Reserved */
  285. buf[off++] = 4; /* DESIGNATOR LENGTH */
  286. off += 2; /* Skip over Reserved Field */
  287. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  288. buf[off++] = (tg_pt_gp_id & 0xff);
  289. len += 8; /* Header size + Designation descriptor */
  290. /*
  291. * Logical Unit Group identifier, see spc4r17
  292. * section 7.7.3.8
  293. */
  294. check_lu_gp:
  295. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  296. if (!lu_gp_mem)
  297. goto check_scsi_name;
  298. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  299. lu_gp = lu_gp_mem->lu_gp;
  300. if (!lu_gp) {
  301. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  302. goto check_scsi_name;
  303. }
  304. lu_gp_id = lu_gp->lu_gp_id;
  305. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  306. buf[off++] |= 0x1; /* CODE SET == Binary */
  307. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  308. buf[off++] |= 0x6;
  309. off++; /* Skip over Reserved */
  310. buf[off++] = 4; /* DESIGNATOR LENGTH */
  311. off += 2; /* Skip over Reserved Field */
  312. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  313. buf[off++] = (lu_gp_id & 0xff);
  314. len += 8; /* Header size + Designation descriptor */
  315. /*
  316. * SCSI name string designator, see spc4r17
  317. * section 7.7.3.11
  318. *
  319. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  320. * section 7.5.1 Table 362
  321. */
  322. check_scsi_name:
  323. scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  324. /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
  325. scsi_name_len += 10;
  326. /* Check for 4-byte padding */
  327. padding = ((-scsi_name_len) & 3);
  328. if (padding != 0)
  329. scsi_name_len += padding;
  330. /* Header size + Designation descriptor */
  331. scsi_name_len += 4;
  332. buf[off] =
  333. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  334. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  335. buf[off] = 0x80; /* Set PIV=1 */
  336. /* Set ASSOCIATION == target port: 01b */
  337. buf[off] |= 0x10;
  338. /* DESIGNATOR TYPE == SCSI name string */
  339. buf[off++] |= 0x8;
  340. off += 2; /* Skip over Reserved and length */
  341. /*
  342. * SCSI name string identifer containing, $FABRIC_MOD
  343. * dependent information. For LIO-Target and iSCSI
  344. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  345. * UTF-8 encoding.
  346. */
  347. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  348. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  349. tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
  350. scsi_name_len += 1 /* Include NULL terminator */;
  351. /*
  352. * The null-terminated, null-padded (see 4.4.2) SCSI
  353. * NAME STRING field contains a UTF-8 format string.
  354. * The number of bytes in the SCSI NAME STRING field
  355. * (i.e., the value in the DESIGNATOR LENGTH field)
  356. * shall be no larger than 256 and shall be a multiple
  357. * of four.
  358. */
  359. if (padding)
  360. scsi_name_len += padding;
  361. buf[off-1] = scsi_name_len;
  362. off += scsi_name_len;
  363. /* Header size + Designation descriptor */
  364. len += (scsi_name_len + 4);
  365. }
  366. buf[2] = ((len >> 8) & 0xff);
  367. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  368. return 0;
  369. }
  370. /* Extended INQUIRY Data VPD Page */
  371. static int
  372. target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  373. {
  374. buf[3] = 0x3c;
  375. /* Set HEADSUP, ORDSUP, SIMPSUP */
  376. buf[5] = 0x07;
  377. /* If WriteCache emulation is enabled, set V_SUP */
  378. if (cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
  379. buf[6] = 0x01;
  380. return 0;
  381. }
  382. /* Block Limits VPD page */
  383. static int
  384. target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  385. {
  386. struct se_device *dev = cmd->se_dev;
  387. int have_tp = 0;
  388. /*
  389. * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
  390. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  391. * different page length for Thin Provisioning.
  392. */
  393. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  394. have_tp = 1;
  395. buf[0] = dev->transport->get_device_type(dev);
  396. buf[3] = have_tp ? 0x3c : 0x10;
  397. /* Set WSNZ to 1 */
  398. buf[4] = 0x01;
  399. /*
  400. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  401. */
  402. put_unaligned_be16(1, &buf[6]);
  403. /*
  404. * Set MAXIMUM TRANSFER LENGTH
  405. */
  406. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.fabric_max_sectors, &buf[8]);
  407. /*
  408. * Set OPTIMAL TRANSFER LENGTH
  409. */
  410. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.optimal_sectors, &buf[12]);
  411. /*
  412. * Exit now if we don't support TP.
  413. */
  414. if (!have_tp)
  415. return 0;
  416. /*
  417. * Set MAXIMUM UNMAP LBA COUNT
  418. */
  419. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count, &buf[20]);
  420. /*
  421. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  422. */
  423. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count,
  424. &buf[24]);
  425. /*
  426. * Set OPTIMAL UNMAP GRANULARITY
  427. */
  428. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity, &buf[28]);
  429. /*
  430. * UNMAP GRANULARITY ALIGNMENT
  431. */
  432. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment,
  433. &buf[32]);
  434. if (dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
  435. buf[32] |= 0x80; /* Set the UGAVALID bit */
  436. return 0;
  437. }
  438. /* Block Device Characteristics VPD page */
  439. static int
  440. target_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  441. {
  442. struct se_device *dev = cmd->se_dev;
  443. buf[0] = dev->transport->get_device_type(dev);
  444. buf[3] = 0x3c;
  445. buf[5] = dev->se_sub_dev->se_dev_attrib.is_nonrot ? 1 : 0;
  446. return 0;
  447. }
  448. /* Thin Provisioning VPD */
  449. static int
  450. target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  451. {
  452. struct se_device *dev = cmd->se_dev;
  453. /*
  454. * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
  455. *
  456. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  457. * zero, then the page length shall be set to 0004h. If the DP bit
  458. * is set to one, then the page length shall be set to the value
  459. * defined in table 162.
  460. */
  461. buf[0] = dev->transport->get_device_type(dev);
  462. /*
  463. * Set Hardcoded length mentioned above for DP=0
  464. */
  465. put_unaligned_be16(0x0004, &buf[2]);
  466. /*
  467. * The THRESHOLD EXPONENT field indicates the threshold set size in
  468. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  469. * 2(threshold exponent)).
  470. *
  471. * Note that this is currently set to 0x00 as mkp says it will be
  472. * changing again. We can enable this once it has settled in T10
  473. * and is actually used by Linux/SCSI ML code.
  474. */
  475. buf[4] = 0x00;
  476. /*
  477. * A TPU bit set to one indicates that the device server supports
  478. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  479. * that the device server does not support the UNMAP command.
  480. */
  481. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu != 0)
  482. buf[5] = 0x80;
  483. /*
  484. * A TPWS bit set to one indicates that the device server supports
  485. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  486. * A TPWS bit set to zero indicates that the device server does not
  487. * support the use of the WRITE SAME (16) command to unmap LBAs.
  488. */
  489. if (dev->se_sub_dev->se_dev_attrib.emulate_tpws != 0)
  490. buf[5] |= 0x40;
  491. return 0;
  492. }
  493. static int
  494. target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  495. static struct {
  496. uint8_t page;
  497. int (*emulate)(struct se_cmd *, unsigned char *);
  498. } evpd_handlers[] = {
  499. { .page = 0x00, .emulate = target_emulate_evpd_00 },
  500. { .page = 0x80, .emulate = target_emulate_evpd_80 },
  501. { .page = 0x83, .emulate = target_emulate_evpd_83 },
  502. { .page = 0x86, .emulate = target_emulate_evpd_86 },
  503. { .page = 0xb0, .emulate = target_emulate_evpd_b0 },
  504. { .page = 0xb1, .emulate = target_emulate_evpd_b1 },
  505. { .page = 0xb2, .emulate = target_emulate_evpd_b2 },
  506. };
  507. /* supported vital product data pages */
  508. static int
  509. target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  510. {
  511. int p;
  512. /*
  513. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  514. * Registered Extended LUN WWN has been set via ConfigFS
  515. * during device creation/restart.
  516. */
  517. if (cmd->se_dev->se_sub_dev->su_dev_flags &
  518. SDF_EMULATED_VPD_UNIT_SERIAL) {
  519. buf[3] = ARRAY_SIZE(evpd_handlers);
  520. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  521. buf[p + 4] = evpd_handlers[p].page;
  522. }
  523. return 0;
  524. }
  525. int target_emulate_inquiry(struct se_task *task)
  526. {
  527. struct se_cmd *cmd = task->task_se_cmd;
  528. struct se_device *dev = cmd->se_dev;
  529. struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
  530. unsigned char *buf, *map_buf;
  531. unsigned char *cdb = cmd->t_task_cdb;
  532. int p, ret;
  533. map_buf = transport_kmap_data_sg(cmd);
  534. /*
  535. * If SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is not set, then we
  536. * know we actually allocated a full page. Otherwise, if the
  537. * data buffer is too small, allocate a temporary buffer so we
  538. * don't have to worry about overruns in all our INQUIRY
  539. * emulation handling.
  540. */
  541. if (cmd->data_length < SE_INQUIRY_BUF &&
  542. (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  543. buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
  544. if (!buf) {
  545. transport_kunmap_data_sg(cmd);
  546. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  547. return -ENOMEM;
  548. }
  549. } else {
  550. buf = map_buf;
  551. }
  552. if (dev == tpg->tpg_virt_lun0.lun_se_dev)
  553. buf[0] = 0x3f; /* Not connected */
  554. else
  555. buf[0] = dev->transport->get_device_type(dev);
  556. if (!(cdb[1] & 0x1)) {
  557. if (cdb[2]) {
  558. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  559. cdb[2]);
  560. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  561. ret = -EINVAL;
  562. goto out;
  563. }
  564. ret = target_emulate_inquiry_std(cmd, buf);
  565. goto out;
  566. }
  567. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  568. if (cdb[2] == evpd_handlers[p].page) {
  569. buf[1] = cdb[2];
  570. ret = evpd_handlers[p].emulate(cmd, buf);
  571. goto out;
  572. }
  573. }
  574. pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
  575. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  576. ret = -EINVAL;
  577. out:
  578. if (buf != map_buf) {
  579. memcpy(map_buf, buf, cmd->data_length);
  580. kfree(buf);
  581. }
  582. transport_kunmap_data_sg(cmd);
  583. if (!ret) {
  584. task->task_scsi_status = GOOD;
  585. transport_complete_task(task, 1);
  586. }
  587. return ret;
  588. }
  589. int target_emulate_readcapacity(struct se_task *task)
  590. {
  591. struct se_cmd *cmd = task->task_se_cmd;
  592. struct se_device *dev = cmd->se_dev;
  593. unsigned char *buf;
  594. unsigned long long blocks_long = dev->transport->get_blocks(dev);
  595. u32 blocks;
  596. if (blocks_long >= 0x00000000ffffffff)
  597. blocks = 0xffffffff;
  598. else
  599. blocks = (u32)blocks_long;
  600. buf = transport_kmap_data_sg(cmd);
  601. buf[0] = (blocks >> 24) & 0xff;
  602. buf[1] = (blocks >> 16) & 0xff;
  603. buf[2] = (blocks >> 8) & 0xff;
  604. buf[3] = blocks & 0xff;
  605. buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  606. buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  607. buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  608. buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  609. transport_kunmap_data_sg(cmd);
  610. task->task_scsi_status = GOOD;
  611. transport_complete_task(task, 1);
  612. return 0;
  613. }
  614. int target_emulate_readcapacity_16(struct se_task *task)
  615. {
  616. struct se_cmd *cmd = task->task_se_cmd;
  617. struct se_device *dev = cmd->se_dev;
  618. unsigned char *buf;
  619. unsigned long long blocks = dev->transport->get_blocks(dev);
  620. buf = transport_kmap_data_sg(cmd);
  621. buf[0] = (blocks >> 56) & 0xff;
  622. buf[1] = (blocks >> 48) & 0xff;
  623. buf[2] = (blocks >> 40) & 0xff;
  624. buf[3] = (blocks >> 32) & 0xff;
  625. buf[4] = (blocks >> 24) & 0xff;
  626. buf[5] = (blocks >> 16) & 0xff;
  627. buf[6] = (blocks >> 8) & 0xff;
  628. buf[7] = blocks & 0xff;
  629. buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  630. buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  631. buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  632. buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  633. /*
  634. * Set Thin Provisioning Enable bit following sbc3r22 in section
  635. * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
  636. */
  637. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  638. buf[14] = 0x80;
  639. transport_kunmap_data_sg(cmd);
  640. task->task_scsi_status = GOOD;
  641. transport_complete_task(task, 1);
  642. return 0;
  643. }
  644. static int
  645. target_modesense_rwrecovery(unsigned char *p)
  646. {
  647. p[0] = 0x01;
  648. p[1] = 0x0a;
  649. return 12;
  650. }
  651. static int
  652. target_modesense_control(struct se_device *dev, unsigned char *p)
  653. {
  654. p[0] = 0x0a;
  655. p[1] = 0x0a;
  656. p[2] = 2;
  657. /*
  658. * From spc4r23, 7.4.7 Control mode page
  659. *
  660. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  661. * restrictions on the algorithm used for reordering commands
  662. * having the SIMPLE task attribute (see SAM-4).
  663. *
  664. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  665. * Code Description
  666. * 0h Restricted reordering
  667. * 1h Unrestricted reordering allowed
  668. * 2h to 7h Reserved
  669. * 8h to Fh Vendor specific
  670. *
  671. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  672. * the device server shall order the processing sequence of commands
  673. * having the SIMPLE task attribute such that data integrity is maintained
  674. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  675. * requests is halted at any time, the final value of all data observable
  676. * on the medium shall be the same as if all the commands had been processed
  677. * with the ORDERED task attribute).
  678. *
  679. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  680. * device server may reorder the processing sequence of commands having the
  681. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  682. * command sequence order shall be explicitly handled by the application client
  683. * through the selection of appropriate ommands and task attributes.
  684. */
  685. p[3] = (dev->se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  686. /*
  687. * From spc4r17, section 7.4.6 Control mode Page
  688. *
  689. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  690. *
  691. * 00b: The logical unit shall clear any unit attention condition
  692. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  693. * status and shall not establish a unit attention condition when a com-
  694. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  695. * status.
  696. *
  697. * 10b: The logical unit shall not clear any unit attention condition
  698. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  699. * status and shall not establish a unit attention condition when
  700. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  701. * CONFLICT status.
  702. *
  703. * 11b a The logical unit shall not clear any unit attention condition
  704. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  705. * status and shall establish a unit attention condition for the
  706. * initiator port associated with the I_T nexus on which the BUSY,
  707. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  708. * Depending on the status, the additional sense code shall be set to
  709. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  710. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  711. * command, a unit attention condition shall be established only once
  712. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  713. * to the number of commands completed with one of those status codes.
  714. */
  715. p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  716. (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  717. /*
  718. * From spc4r17, section 7.4.6 Control mode Page
  719. *
  720. * Task Aborted Status (TAS) bit set to zero.
  721. *
  722. * A task aborted status (TAS) bit set to zero specifies that aborted
  723. * tasks shall be terminated by the device server without any response
  724. * to the application client. A TAS bit set to one specifies that tasks
  725. * aborted by the actions of an I_T nexus other than the I_T nexus on
  726. * which the command was received shall be completed with TASK ABORTED
  727. * status (see SAM-4).
  728. */
  729. p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
  730. p[8] = 0xff;
  731. p[9] = 0xff;
  732. p[11] = 30;
  733. return 12;
  734. }
  735. static int
  736. target_modesense_caching(struct se_device *dev, unsigned char *p)
  737. {
  738. p[0] = 0x08;
  739. p[1] = 0x12;
  740. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
  741. p[2] = 0x04; /* Write Cache Enable */
  742. p[12] = 0x20; /* Disabled Read Ahead */
  743. return 20;
  744. }
  745. static void
  746. target_modesense_write_protect(unsigned char *buf, int type)
  747. {
  748. /*
  749. * I believe that the WP bit (bit 7) in the mode header is the same for
  750. * all device types..
  751. */
  752. switch (type) {
  753. case TYPE_DISK:
  754. case TYPE_TAPE:
  755. default:
  756. buf[0] |= 0x80; /* WP bit */
  757. break;
  758. }
  759. }
  760. static void
  761. target_modesense_dpofua(unsigned char *buf, int type)
  762. {
  763. switch (type) {
  764. case TYPE_DISK:
  765. buf[0] |= 0x10; /* DPOFUA bit */
  766. break;
  767. default:
  768. break;
  769. }
  770. }
  771. int target_emulate_modesense(struct se_task *task)
  772. {
  773. struct se_cmd *cmd = task->task_se_cmd;
  774. struct se_device *dev = cmd->se_dev;
  775. char *cdb = cmd->t_task_cdb;
  776. unsigned char *rbuf;
  777. int type = dev->transport->get_device_type(dev);
  778. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  779. int offset = ten ? 8 : 4;
  780. int length = 0;
  781. unsigned char buf[SE_MODE_PAGE_BUF];
  782. memset(buf, 0, SE_MODE_PAGE_BUF);
  783. switch (cdb[2] & 0x3f) {
  784. case 0x01:
  785. length = target_modesense_rwrecovery(&buf[offset]);
  786. break;
  787. case 0x08:
  788. length = target_modesense_caching(dev, &buf[offset]);
  789. break;
  790. case 0x0a:
  791. length = target_modesense_control(dev, &buf[offset]);
  792. break;
  793. case 0x3f:
  794. length = target_modesense_rwrecovery(&buf[offset]);
  795. length += target_modesense_caching(dev, &buf[offset+length]);
  796. length += target_modesense_control(dev, &buf[offset+length]);
  797. break;
  798. default:
  799. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  800. cdb[2] & 0x3f, cdb[3]);
  801. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  802. return -EINVAL;
  803. }
  804. offset += length;
  805. if (ten) {
  806. offset -= 2;
  807. buf[0] = (offset >> 8) & 0xff;
  808. buf[1] = offset & 0xff;
  809. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  810. (cmd->se_deve &&
  811. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  812. target_modesense_write_protect(&buf[3], type);
  813. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  814. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  815. target_modesense_dpofua(&buf[3], type);
  816. if ((offset + 2) > cmd->data_length)
  817. offset = cmd->data_length;
  818. } else {
  819. offset -= 1;
  820. buf[0] = offset & 0xff;
  821. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  822. (cmd->se_deve &&
  823. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  824. target_modesense_write_protect(&buf[2], type);
  825. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  826. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  827. target_modesense_dpofua(&buf[2], type);
  828. if ((offset + 1) > cmd->data_length)
  829. offset = cmd->data_length;
  830. }
  831. rbuf = transport_kmap_data_sg(cmd);
  832. memcpy(rbuf, buf, offset);
  833. transport_kunmap_data_sg(cmd);
  834. task->task_scsi_status = GOOD;
  835. transport_complete_task(task, 1);
  836. return 0;
  837. }
  838. int target_emulate_request_sense(struct se_task *task)
  839. {
  840. struct se_cmd *cmd = task->task_se_cmd;
  841. unsigned char *cdb = cmd->t_task_cdb;
  842. unsigned char *buf;
  843. u8 ua_asc = 0, ua_ascq = 0;
  844. int err = 0;
  845. if (cdb[1] & 0x01) {
  846. pr_err("REQUEST_SENSE description emulation not"
  847. " supported\n");
  848. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  849. return -ENOSYS;
  850. }
  851. buf = transport_kmap_data_sg(cmd);
  852. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  853. /*
  854. * CURRENT ERROR, UNIT ATTENTION
  855. */
  856. buf[0] = 0x70;
  857. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  858. if (cmd->data_length < 18) {
  859. buf[7] = 0x00;
  860. err = -EINVAL;
  861. goto end;
  862. }
  863. /*
  864. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  865. */
  866. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  867. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  868. buf[7] = 0x0A;
  869. } else {
  870. /*
  871. * CURRENT ERROR, NO SENSE
  872. */
  873. buf[0] = 0x70;
  874. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  875. if (cmd->data_length < 18) {
  876. buf[7] = 0x00;
  877. err = -EINVAL;
  878. goto end;
  879. }
  880. /*
  881. * NO ADDITIONAL SENSE INFORMATION
  882. */
  883. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  884. buf[7] = 0x0A;
  885. }
  886. end:
  887. transport_kunmap_data_sg(cmd);
  888. task->task_scsi_status = GOOD;
  889. transport_complete_task(task, 1);
  890. return 0;
  891. }
  892. /*
  893. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  894. * Note this is not used for TCM/pSCSI passthrough
  895. */
  896. int target_emulate_unmap(struct se_task *task)
  897. {
  898. struct se_cmd *cmd = task->task_se_cmd;
  899. struct se_device *dev = cmd->se_dev;
  900. unsigned char *buf, *ptr = NULL;
  901. unsigned char *cdb = &cmd->t_task_cdb[0];
  902. sector_t lba;
  903. unsigned int size = cmd->data_length, range;
  904. int ret = 0, offset;
  905. unsigned short dl, bd_dl;
  906. if (!dev->transport->do_discard) {
  907. pr_err("UNMAP emulation not supported for: %s\n",
  908. dev->transport->name);
  909. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  910. return -ENOSYS;
  911. }
  912. /* First UNMAP block descriptor starts at 8 byte offset */
  913. offset = 8;
  914. size -= 8;
  915. dl = get_unaligned_be16(&cdb[0]);
  916. bd_dl = get_unaligned_be16(&cdb[2]);
  917. buf = transport_kmap_data_sg(cmd);
  918. ptr = &buf[offset];
  919. pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  920. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  921. while (size) {
  922. lba = get_unaligned_be64(&ptr[0]);
  923. range = get_unaligned_be32(&ptr[8]);
  924. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  925. (unsigned long long)lba, range);
  926. ret = dev->transport->do_discard(dev, lba, range);
  927. if (ret < 0) {
  928. pr_err("blkdev_issue_discard() failed: %d\n",
  929. ret);
  930. goto err;
  931. }
  932. ptr += 16;
  933. size -= 16;
  934. }
  935. err:
  936. transport_kunmap_data_sg(cmd);
  937. if (!ret) {
  938. task->task_scsi_status = GOOD;
  939. transport_complete_task(task, 1);
  940. }
  941. return ret;
  942. }
  943. /*
  944. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  945. * Note this is not used for TCM/pSCSI passthrough
  946. */
  947. int target_emulate_write_same(struct se_task *task)
  948. {
  949. struct se_cmd *cmd = task->task_se_cmd;
  950. struct se_device *dev = cmd->se_dev;
  951. sector_t range;
  952. sector_t lba = cmd->t_task_lba;
  953. u32 num_blocks;
  954. int ret;
  955. if (!dev->transport->do_discard) {
  956. pr_err("WRITE_SAME emulation not supported"
  957. " for: %s\n", dev->transport->name);
  958. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  959. return -ENOSYS;
  960. }
  961. if (cmd->t_task_cdb[0] == WRITE_SAME)
  962. num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
  963. else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
  964. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
  965. else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
  966. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
  967. /*
  968. * Use the explicit range when non zero is supplied, otherwise calculate
  969. * the remaining range based on ->get_blocks() - starting LBA.
  970. */
  971. if (num_blocks != 0)
  972. range = num_blocks;
  973. else
  974. range = (dev->transport->get_blocks(dev) - lba);
  975. pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
  976. (unsigned long long)lba, (unsigned long long)range);
  977. ret = dev->transport->do_discard(dev, lba, range);
  978. if (ret < 0) {
  979. pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
  980. return ret;
  981. }
  982. task->task_scsi_status = GOOD;
  983. transport_complete_task(task, 1);
  984. return 0;
  985. }
  986. int target_emulate_synchronize_cache(struct se_task *task)
  987. {
  988. struct se_device *dev = task->task_se_cmd->se_dev;
  989. struct se_cmd *cmd = task->task_se_cmd;
  990. if (!dev->transport->do_sync_cache) {
  991. pr_err("SYNCHRONIZE_CACHE emulation not supported"
  992. " for: %s\n", dev->transport->name);
  993. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  994. return -ENOSYS;
  995. }
  996. dev->transport->do_sync_cache(task);
  997. return 0;
  998. }
  999. int target_emulate_noop(struct se_task *task)
  1000. {
  1001. task->task_scsi_status = GOOD;
  1002. transport_complete_task(task, 1);
  1003. return 0;
  1004. }
  1005. /*
  1006. * Write a CDB into @cdb that is based on the one the intiator sent us,
  1007. * but updated to only cover the sectors that the current task handles.
  1008. */
  1009. void target_get_task_cdb(struct se_task *task, unsigned char *cdb)
  1010. {
  1011. struct se_cmd *cmd = task->task_se_cmd;
  1012. unsigned int cdb_len = scsi_command_size(cmd->t_task_cdb);
  1013. memcpy(cdb, cmd->t_task_cdb, cdb_len);
  1014. if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
  1015. unsigned long long lba = task->task_lba;
  1016. u32 sectors = task->task_sectors;
  1017. switch (cdb_len) {
  1018. case 6:
  1019. /* 21-bit LBA and 8-bit sectors */
  1020. cdb[1] = (lba >> 16) & 0x1f;
  1021. cdb[2] = (lba >> 8) & 0xff;
  1022. cdb[3] = lba & 0xff;
  1023. cdb[4] = sectors & 0xff;
  1024. break;
  1025. case 10:
  1026. /* 32-bit LBA and 16-bit sectors */
  1027. put_unaligned_be32(lba, &cdb[2]);
  1028. put_unaligned_be16(sectors, &cdb[7]);
  1029. break;
  1030. case 12:
  1031. /* 32-bit LBA and 32-bit sectors */
  1032. put_unaligned_be32(lba, &cdb[2]);
  1033. put_unaligned_be32(sectors, &cdb[6]);
  1034. break;
  1035. case 16:
  1036. /* 64-bit LBA and 32-bit sectors */
  1037. put_unaligned_be64(lba, &cdb[2]);
  1038. put_unaligned_be32(sectors, &cdb[10]);
  1039. break;
  1040. case 32:
  1041. /* 64-bit LBA and 32-bit sectors, extended CDB */
  1042. put_unaligned_be64(lba, &cdb[12]);
  1043. put_unaligned_be32(sectors, &cdb[28]);
  1044. break;
  1045. default:
  1046. BUG();
  1047. }
  1048. }
  1049. }
  1050. EXPORT_SYMBOL(target_get_task_cdb);