target_core_cdb.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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 spc4r23, 7.4.7 Control mode page
  682. *
  683. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  684. * restrictions on the algorithm used for reordering commands
  685. * having the SIMPLE task attribute (see SAM-4).
  686. *
  687. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  688. * Code Description
  689. * 0h Restricted reordering
  690. * 1h Unrestricted reordering allowed
  691. * 2h to 7h Reserved
  692. * 8h to Fh Vendor specific
  693. *
  694. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  695. * the device server shall order the processing sequence of commands
  696. * having the SIMPLE task attribute such that data integrity is maintained
  697. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  698. * requests is halted at any time, the final value of all data observable
  699. * on the medium shall be the same as if all the commands had been processed
  700. * with the ORDERED task attribute).
  701. *
  702. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  703. * device server may reorder the processing sequence of commands having the
  704. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  705. * command sequence order shall be explicitly handled by the application client
  706. * through the selection of appropriate ommands and task attributes.
  707. */
  708. p[3] = (dev->se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  709. /*
  710. * From spc4r17, section 7.4.6 Control mode Page
  711. *
  712. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  713. *
  714. * 00b: The logical unit shall clear any unit attention condition
  715. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  716. * status and shall not establish a unit attention condition when a com-
  717. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  718. * status.
  719. *
  720. * 10b: The logical unit shall not clear any unit attention condition
  721. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  722. * status and shall not establish a unit attention condition when
  723. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  724. * CONFLICT status.
  725. *
  726. * 11b a The logical unit shall not clear any unit attention condition
  727. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  728. * status and shall establish a unit attention condition for the
  729. * initiator port associated with the I_T nexus on which the BUSY,
  730. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  731. * Depending on the status, the additional sense code shall be set to
  732. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  733. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  734. * command, a unit attention condition shall be established only once
  735. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  736. * to the number of commands completed with one of those status codes.
  737. */
  738. p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  739. (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  740. /*
  741. * From spc4r17, section 7.4.6 Control mode Page
  742. *
  743. * Task Aborted Status (TAS) bit set to zero.
  744. *
  745. * A task aborted status (TAS) bit set to zero specifies that aborted
  746. * tasks shall be terminated by the device server without any response
  747. * to the application client. A TAS bit set to one specifies that tasks
  748. * aborted by the actions of an I_T nexus other than the I_T nexus on
  749. * which the command was received shall be completed with TASK ABORTED
  750. * status (see SAM-4).
  751. */
  752. p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
  753. p[8] = 0xff;
  754. p[9] = 0xff;
  755. p[11] = 30;
  756. return 12;
  757. }
  758. static int
  759. target_modesense_caching(struct se_device *dev, unsigned char *p)
  760. {
  761. p[0] = 0x08;
  762. p[1] = 0x12;
  763. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
  764. p[2] = 0x04; /* Write Cache Enable */
  765. p[12] = 0x20; /* Disabled Read Ahead */
  766. return 20;
  767. }
  768. static void
  769. target_modesense_write_protect(unsigned char *buf, int type)
  770. {
  771. /*
  772. * I believe that the WP bit (bit 7) in the mode header is the same for
  773. * all device types..
  774. */
  775. switch (type) {
  776. case TYPE_DISK:
  777. case TYPE_TAPE:
  778. default:
  779. buf[0] |= 0x80; /* WP bit */
  780. break;
  781. }
  782. }
  783. static void
  784. target_modesense_dpofua(unsigned char *buf, int type)
  785. {
  786. switch (type) {
  787. case TYPE_DISK:
  788. buf[0] |= 0x10; /* DPOFUA bit */
  789. break;
  790. default:
  791. break;
  792. }
  793. }
  794. static int
  795. target_emulate_modesense(struct se_cmd *cmd, int ten)
  796. {
  797. struct se_device *dev = cmd->se_dev;
  798. char *cdb = cmd->t_task_cdb;
  799. unsigned char *rbuf;
  800. int type = dev->transport->get_device_type(dev);
  801. int offset = (ten) ? 8 : 4;
  802. int length = 0;
  803. unsigned char buf[SE_MODE_PAGE_BUF];
  804. memset(buf, 0, SE_MODE_PAGE_BUF);
  805. switch (cdb[2] & 0x3f) {
  806. case 0x01:
  807. length = target_modesense_rwrecovery(&buf[offset]);
  808. break;
  809. case 0x08:
  810. length = target_modesense_caching(dev, &buf[offset]);
  811. break;
  812. case 0x0a:
  813. length = target_modesense_control(dev, &buf[offset]);
  814. break;
  815. case 0x3f:
  816. length = target_modesense_rwrecovery(&buf[offset]);
  817. length += target_modesense_caching(dev, &buf[offset+length]);
  818. length += target_modesense_control(dev, &buf[offset+length]);
  819. break;
  820. default:
  821. pr_err("Got Unknown Mode Page: 0x%02x\n",
  822. cdb[2] & 0x3f);
  823. return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
  824. }
  825. offset += length;
  826. if (ten) {
  827. offset -= 2;
  828. buf[0] = (offset >> 8) & 0xff;
  829. buf[1] = offset & 0xff;
  830. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  831. (cmd->se_deve &&
  832. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  833. target_modesense_write_protect(&buf[3], type);
  834. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  835. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  836. target_modesense_dpofua(&buf[3], type);
  837. if ((offset + 2) > cmd->data_length)
  838. offset = cmd->data_length;
  839. } else {
  840. offset -= 1;
  841. buf[0] = offset & 0xff;
  842. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  843. (cmd->se_deve &&
  844. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  845. target_modesense_write_protect(&buf[2], type);
  846. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  847. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  848. target_modesense_dpofua(&buf[2], type);
  849. if ((offset + 1) > cmd->data_length)
  850. offset = cmd->data_length;
  851. }
  852. rbuf = transport_kmap_first_data_page(cmd);
  853. memcpy(rbuf, buf, offset);
  854. transport_kunmap_first_data_page(cmd);
  855. return 0;
  856. }
  857. static int
  858. target_emulate_request_sense(struct se_cmd *cmd)
  859. {
  860. unsigned char *cdb = cmd->t_task_cdb;
  861. unsigned char *buf;
  862. u8 ua_asc = 0, ua_ascq = 0;
  863. int err = 0;
  864. if (cdb[1] & 0x01) {
  865. pr_err("REQUEST_SENSE description emulation not"
  866. " supported\n");
  867. return PYX_TRANSPORT_INVALID_CDB_FIELD;
  868. }
  869. buf = transport_kmap_first_data_page(cmd);
  870. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  871. /*
  872. * CURRENT ERROR, UNIT ATTENTION
  873. */
  874. buf[0] = 0x70;
  875. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  876. /*
  877. * Make sure request data length is enough for additional
  878. * sense data.
  879. */
  880. if (cmd->data_length <= 18) {
  881. buf[7] = 0x00;
  882. err = -EINVAL;
  883. goto end;
  884. }
  885. /*
  886. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  887. */
  888. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  889. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  890. buf[7] = 0x0A;
  891. } else {
  892. /*
  893. * CURRENT ERROR, NO SENSE
  894. */
  895. buf[0] = 0x70;
  896. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  897. /*
  898. * Make sure request data length is enough for additional
  899. * sense data.
  900. */
  901. if (cmd->data_length <= 18) {
  902. buf[7] = 0x00;
  903. err = -EINVAL;
  904. goto end;
  905. }
  906. /*
  907. * NO ADDITIONAL SENSE INFORMATION
  908. */
  909. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  910. buf[7] = 0x0A;
  911. }
  912. end:
  913. transport_kunmap_first_data_page(cmd);
  914. return 0;
  915. }
  916. /*
  917. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  918. * Note this is not used for TCM/pSCSI passthrough
  919. */
  920. static int
  921. target_emulate_unmap(struct se_task *task)
  922. {
  923. struct se_cmd *cmd = task->task_se_cmd;
  924. struct se_device *dev = cmd->se_dev;
  925. unsigned char *buf, *ptr = NULL;
  926. unsigned char *cdb = &cmd->t_task_cdb[0];
  927. sector_t lba;
  928. unsigned int size = cmd->data_length, range;
  929. int ret = 0, offset;
  930. unsigned short dl, bd_dl;
  931. /* First UNMAP block descriptor starts at 8 byte offset */
  932. offset = 8;
  933. size -= 8;
  934. dl = get_unaligned_be16(&cdb[0]);
  935. bd_dl = get_unaligned_be16(&cdb[2]);
  936. buf = transport_kmap_first_data_page(cmd);
  937. ptr = &buf[offset];
  938. pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  939. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  940. while (size) {
  941. lba = get_unaligned_be64(&ptr[0]);
  942. range = get_unaligned_be32(&ptr[8]);
  943. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  944. (unsigned long long)lba, range);
  945. ret = dev->transport->do_discard(dev, lba, range);
  946. if (ret < 0) {
  947. pr_err("blkdev_issue_discard() failed: %d\n",
  948. ret);
  949. goto err;
  950. }
  951. ptr += 16;
  952. size -= 16;
  953. }
  954. task->task_scsi_status = GOOD;
  955. transport_complete_task(task, 1);
  956. err:
  957. transport_kunmap_first_data_page(cmd);
  958. return ret;
  959. }
  960. /*
  961. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  962. * Note this is not used for TCM/pSCSI passthrough
  963. */
  964. static int
  965. target_emulate_write_same(struct se_task *task, int write_same32)
  966. {
  967. struct se_cmd *cmd = task->task_se_cmd;
  968. struct se_device *dev = cmd->se_dev;
  969. sector_t range;
  970. sector_t lba = cmd->t_task_lba;
  971. unsigned int num_blocks;
  972. int ret;
  973. /*
  974. * Extract num_blocks from the WRITE_SAME_* CDB. Then use the explict
  975. * range when non zero is supplied, otherwise calculate the remaining
  976. * range based on ->get_blocks() - starting LBA.
  977. */
  978. if (write_same32)
  979. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
  980. else
  981. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
  982. if (num_blocks != 0)
  983. range = num_blocks;
  984. else
  985. range = (dev->transport->get_blocks(dev) - lba);
  986. pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
  987. (unsigned long long)lba, (unsigned long long)range);
  988. ret = dev->transport->do_discard(dev, lba, range);
  989. if (ret < 0) {
  990. pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
  991. return ret;
  992. }
  993. task->task_scsi_status = GOOD;
  994. transport_complete_task(task, 1);
  995. return 0;
  996. }
  997. int
  998. transport_emulate_control_cdb(struct se_task *task)
  999. {
  1000. struct se_cmd *cmd = task->task_se_cmd;
  1001. struct se_device *dev = cmd->se_dev;
  1002. unsigned short service_action;
  1003. int ret = 0;
  1004. switch (cmd->t_task_cdb[0]) {
  1005. case INQUIRY:
  1006. ret = target_emulate_inquiry(cmd);
  1007. break;
  1008. case READ_CAPACITY:
  1009. ret = target_emulate_readcapacity(cmd);
  1010. break;
  1011. case MODE_SENSE:
  1012. ret = target_emulate_modesense(cmd, 0);
  1013. break;
  1014. case MODE_SENSE_10:
  1015. ret = target_emulate_modesense(cmd, 1);
  1016. break;
  1017. case SERVICE_ACTION_IN:
  1018. switch (cmd->t_task_cdb[1] & 0x1f) {
  1019. case SAI_READ_CAPACITY_16:
  1020. ret = target_emulate_readcapacity_16(cmd);
  1021. break;
  1022. default:
  1023. pr_err("Unsupported SA: 0x%02x\n",
  1024. cmd->t_task_cdb[1] & 0x1f);
  1025. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1026. }
  1027. break;
  1028. case REQUEST_SENSE:
  1029. ret = target_emulate_request_sense(cmd);
  1030. break;
  1031. case UNMAP:
  1032. if (!dev->transport->do_discard) {
  1033. pr_err("UNMAP emulation not supported for: %s\n",
  1034. dev->transport->name);
  1035. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1036. }
  1037. ret = target_emulate_unmap(task);
  1038. break;
  1039. case WRITE_SAME_16:
  1040. if (!dev->transport->do_discard) {
  1041. pr_err("WRITE_SAME_16 emulation not supported"
  1042. " for: %s\n", dev->transport->name);
  1043. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1044. }
  1045. ret = target_emulate_write_same(task, 0);
  1046. break;
  1047. case VARIABLE_LENGTH_CMD:
  1048. service_action =
  1049. get_unaligned_be16(&cmd->t_task_cdb[8]);
  1050. switch (service_action) {
  1051. case WRITE_SAME_32:
  1052. if (!dev->transport->do_discard) {
  1053. pr_err("WRITE_SAME_32 SA emulation not"
  1054. " supported for: %s\n",
  1055. dev->transport->name);
  1056. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1057. }
  1058. ret = target_emulate_write_same(task, 1);
  1059. break;
  1060. default:
  1061. pr_err("Unsupported VARIABLE_LENGTH_CMD SA:"
  1062. " 0x%02x\n", service_action);
  1063. break;
  1064. }
  1065. break;
  1066. case SYNCHRONIZE_CACHE:
  1067. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  1068. if (!dev->transport->do_sync_cache) {
  1069. pr_err("SYNCHRONIZE_CACHE emulation not supported"
  1070. " for: %s\n", dev->transport->name);
  1071. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1072. }
  1073. dev->transport->do_sync_cache(task);
  1074. break;
  1075. case ALLOW_MEDIUM_REMOVAL:
  1076. case ERASE:
  1077. case REZERO_UNIT:
  1078. case SEEK_10:
  1079. case SPACE:
  1080. case START_STOP:
  1081. case TEST_UNIT_READY:
  1082. case VERIFY:
  1083. case WRITE_FILEMARKS:
  1084. break;
  1085. default:
  1086. pr_err("Unsupported SCSI Opcode: 0x%02x for %s\n",
  1087. cmd->t_task_cdb[0], dev->transport->name);
  1088. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1089. }
  1090. if (ret < 0)
  1091. return ret;
  1092. task->task_scsi_status = GOOD;
  1093. transport_complete_task(task, 1);
  1094. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  1095. }