target_core_cdb.c 34 KB

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