target_core_cdb.c 35 KB

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