target_core_cdb.c 34 KB

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