target_core_cdb.c 33 KB

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