target_core_cdb.c 32 KB

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