target_core_cdb.c 34 KB

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