target_core_spc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * SCSI Primary Commands (SPC) parsing and emulation.
  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 <linux/module.h>
  27. #include <asm/unaligned.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <target/target_core_base.h>
  31. #include <target/target_core_backend.h>
  32. #include <target/target_core_fabric.h>
  33. #include "target_core_internal.h"
  34. #include "target_core_alua.h"
  35. #include "target_core_pr.h"
  36. #include "target_core_ua.h"
  37. static void spc_fill_alua_data(struct se_port *port, unsigned char *buf)
  38. {
  39. struct t10_alua_tg_pt_gp *tg_pt_gp;
  40. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  41. /*
  42. * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
  43. */
  44. buf[5] = 0x80;
  45. /*
  46. * Set TPGS field for explict and/or implict ALUA access type
  47. * and opteration.
  48. *
  49. * See spc4r17 section 6.4.2 Table 135
  50. */
  51. if (!port)
  52. return;
  53. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  54. if (!tg_pt_gp_mem)
  55. return;
  56. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  57. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  58. if (tg_pt_gp)
  59. buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
  60. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  61. }
  62. static int spc_emulate_inquiry_std(struct se_cmd *cmd, char *buf)
  63. {
  64. struct se_lun *lun = cmd->se_lun;
  65. struct se_device *dev = cmd->se_dev;
  66. /* Set RMB (removable media) for tape devices */
  67. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  68. buf[1] = 0x80;
  69. buf[2] = dev->transport->get_device_rev(dev);
  70. /*
  71. * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
  72. *
  73. * SPC4 says:
  74. * A RESPONSE DATA FORMAT field set to 2h indicates that the
  75. * standard INQUIRY data is in the format defined in this
  76. * standard. Response data format values less than 2h are
  77. * obsolete. Response data format values greater than 2h are
  78. * reserved.
  79. */
  80. buf[3] = 2;
  81. /*
  82. * Enable SCCS and TPGS fields for Emulated ALUA
  83. */
  84. spc_fill_alua_data(lun->lun_sep, buf);
  85. buf[7] = 0x2; /* CmdQue=1 */
  86. snprintf(&buf[8], 8, "LIO-ORG");
  87. snprintf(&buf[16], 16, "%s", dev->t10_wwn.model);
  88. snprintf(&buf[32], 4, "%s", dev->t10_wwn.revision);
  89. buf[4] = 31; /* Set additional length to 31 */
  90. return 0;
  91. }
  92. /* unit serial number */
  93. static int spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  94. {
  95. struct se_device *dev = cmd->se_dev;
  96. u16 len = 0;
  97. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  98. u32 unit_serial_len;
  99. unit_serial_len = strlen(dev->t10_wwn.unit_serial);
  100. unit_serial_len++; /* For NULL Terminator */
  101. len += sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
  102. len++; /* Extra Byte for NULL Terminator */
  103. buf[3] = len;
  104. }
  105. return 0;
  106. }
  107. static void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
  108. unsigned char *buf)
  109. {
  110. unsigned char *p = &dev->t10_wwn.unit_serial[0];
  111. int cnt;
  112. bool next = true;
  113. /*
  114. * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
  115. * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
  116. * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
  117. * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
  118. * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
  119. * per device uniqeness.
  120. */
  121. for (cnt = 0; *p && cnt < 13; p++) {
  122. int val = hex_to_bin(*p);
  123. if (val < 0)
  124. continue;
  125. if (next) {
  126. next = false;
  127. buf[cnt++] |= val;
  128. } else {
  129. next = true;
  130. buf[cnt] = val << 4;
  131. }
  132. }
  133. }
  134. /*
  135. * Device identification VPD, for a complete list of
  136. * DESIGNATOR TYPEs see spc4r17 Table 459.
  137. */
  138. static int spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  139. {
  140. struct se_device *dev = cmd->se_dev;
  141. struct se_lun *lun = cmd->se_lun;
  142. struct se_port *port = NULL;
  143. struct se_portal_group *tpg = NULL;
  144. struct t10_alua_lu_gp_member *lu_gp_mem;
  145. struct t10_alua_tg_pt_gp *tg_pt_gp;
  146. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  147. unsigned char *prod = &dev->t10_wwn.model[0];
  148. u32 prod_len;
  149. u32 unit_serial_len, off = 0;
  150. u16 len = 0, id_len;
  151. off = 4;
  152. /*
  153. * NAA IEEE Registered Extended Assigned designator format, see
  154. * spc4r17 section 7.7.3.6.5
  155. *
  156. * We depend upon a target_core_mod/ConfigFS provided
  157. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  158. * value in order to return the NAA id.
  159. */
  160. if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
  161. goto check_t10_vend_desc;
  162. /* CODE SET == Binary */
  163. buf[off++] = 0x1;
  164. /* Set ASSOCIATION == addressed logical unit: 0)b */
  165. buf[off] = 0x00;
  166. /* Identifier/Designator type == NAA identifier */
  167. buf[off++] |= 0x3;
  168. off++;
  169. /* Identifier/Designator length */
  170. buf[off++] = 0x10;
  171. /*
  172. * Start NAA IEEE Registered Extended Identifier/Designator
  173. */
  174. buf[off++] = (0x6 << 4);
  175. /*
  176. * Use OpenFabrics IEEE Company ID: 00 14 05
  177. */
  178. buf[off++] = 0x01;
  179. buf[off++] = 0x40;
  180. buf[off] = (0x5 << 4);
  181. /*
  182. * Return ConfigFS Unit Serial Number information for
  183. * VENDOR_SPECIFIC_IDENTIFIER and
  184. * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
  185. */
  186. spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
  187. len = 20;
  188. off = (len + 4);
  189. check_t10_vend_desc:
  190. /*
  191. * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
  192. */
  193. id_len = 8; /* For Vendor field */
  194. prod_len = 4; /* For VPD Header */
  195. prod_len += 8; /* For Vendor field */
  196. prod_len += strlen(prod);
  197. prod_len++; /* For : */
  198. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  199. unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]);
  200. unit_serial_len++; /* For NULL Terminator */
  201. id_len += sprintf(&buf[off+12], "%s:%s", prod,
  202. &dev->t10_wwn.unit_serial[0]);
  203. }
  204. buf[off] = 0x2; /* ASCII */
  205. buf[off+1] = 0x1; /* T10 Vendor ID */
  206. buf[off+2] = 0x0;
  207. memcpy(&buf[off+4], "LIO-ORG", 8);
  208. /* Extra Byte for NULL Terminator */
  209. id_len++;
  210. /* Identifier Length */
  211. buf[off+3] = id_len;
  212. /* Header size for Designation descriptor */
  213. len += (id_len + 4);
  214. off += (id_len + 4);
  215. /*
  216. * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
  217. */
  218. port = lun->lun_sep;
  219. if (port) {
  220. struct t10_alua_lu_gp *lu_gp;
  221. u32 padding, scsi_name_len;
  222. u16 lu_gp_id = 0;
  223. u16 tg_pt_gp_id = 0;
  224. u16 tpgt;
  225. tpg = port->sep_tpg;
  226. /*
  227. * Relative target port identifer, see spc4r17
  228. * section 7.7.3.7
  229. *
  230. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  231. * section 7.5.1 Table 362
  232. */
  233. buf[off] =
  234. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  235. buf[off++] |= 0x1; /* CODE SET == Binary */
  236. buf[off] = 0x80; /* Set PIV=1 */
  237. /* Set ASSOCIATION == target port: 01b */
  238. buf[off] |= 0x10;
  239. /* DESIGNATOR TYPE == Relative target port identifer */
  240. buf[off++] |= 0x4;
  241. off++; /* Skip over Reserved */
  242. buf[off++] = 4; /* DESIGNATOR LENGTH */
  243. /* Skip over Obsolete field in RTPI payload
  244. * in Table 472 */
  245. off += 2;
  246. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  247. buf[off++] = (port->sep_rtpi & 0xff);
  248. len += 8; /* Header size + Designation descriptor */
  249. /*
  250. * Target port group identifier, see spc4r17
  251. * section 7.7.3.8
  252. *
  253. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  254. * section 7.5.1 Table 362
  255. */
  256. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  257. if (!tg_pt_gp_mem)
  258. goto check_lu_gp;
  259. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  260. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  261. if (!tg_pt_gp) {
  262. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  263. goto check_lu_gp;
  264. }
  265. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  266. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  267. buf[off] =
  268. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  269. buf[off++] |= 0x1; /* CODE SET == Binary */
  270. buf[off] = 0x80; /* Set PIV=1 */
  271. /* Set ASSOCIATION == target port: 01b */
  272. buf[off] |= 0x10;
  273. /* DESIGNATOR TYPE == Target port group identifier */
  274. buf[off++] |= 0x5;
  275. off++; /* Skip over Reserved */
  276. buf[off++] = 4; /* DESIGNATOR LENGTH */
  277. off += 2; /* Skip over Reserved Field */
  278. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  279. buf[off++] = (tg_pt_gp_id & 0xff);
  280. len += 8; /* Header size + Designation descriptor */
  281. /*
  282. * Logical Unit Group identifier, see spc4r17
  283. * section 7.7.3.8
  284. */
  285. check_lu_gp:
  286. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  287. if (!lu_gp_mem)
  288. goto check_scsi_name;
  289. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  290. lu_gp = lu_gp_mem->lu_gp;
  291. if (!lu_gp) {
  292. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  293. goto check_scsi_name;
  294. }
  295. lu_gp_id = lu_gp->lu_gp_id;
  296. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  297. buf[off++] |= 0x1; /* CODE SET == Binary */
  298. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  299. buf[off++] |= 0x6;
  300. off++; /* Skip over Reserved */
  301. buf[off++] = 4; /* DESIGNATOR LENGTH */
  302. off += 2; /* Skip over Reserved Field */
  303. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  304. buf[off++] = (lu_gp_id & 0xff);
  305. len += 8; /* Header size + Designation descriptor */
  306. /*
  307. * SCSI name string designator, see spc4r17
  308. * section 7.7.3.11
  309. *
  310. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  311. * section 7.5.1 Table 362
  312. */
  313. check_scsi_name:
  314. scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  315. /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
  316. scsi_name_len += 10;
  317. /* Check for 4-byte padding */
  318. padding = ((-scsi_name_len) & 3);
  319. if (padding != 0)
  320. scsi_name_len += padding;
  321. /* Header size + Designation descriptor */
  322. scsi_name_len += 4;
  323. buf[off] =
  324. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  325. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  326. buf[off] = 0x80; /* Set PIV=1 */
  327. /* Set ASSOCIATION == target port: 01b */
  328. buf[off] |= 0x10;
  329. /* DESIGNATOR TYPE == SCSI name string */
  330. buf[off++] |= 0x8;
  331. off += 2; /* Skip over Reserved and length */
  332. /*
  333. * SCSI name string identifer containing, $FABRIC_MOD
  334. * dependent information. For LIO-Target and iSCSI
  335. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  336. * UTF-8 encoding.
  337. */
  338. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  339. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  340. tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
  341. scsi_name_len += 1 /* Include NULL terminator */;
  342. /*
  343. * The null-terminated, null-padded (see 4.4.2) SCSI
  344. * NAME STRING field contains a UTF-8 format string.
  345. * The number of bytes in the SCSI NAME STRING field
  346. * (i.e., the value in the DESIGNATOR LENGTH field)
  347. * shall be no larger than 256 and shall be a multiple
  348. * of four.
  349. */
  350. if (padding)
  351. scsi_name_len += padding;
  352. buf[off-1] = scsi_name_len;
  353. off += scsi_name_len;
  354. /* Header size + Designation descriptor */
  355. len += (scsi_name_len + 4);
  356. }
  357. buf[2] = ((len >> 8) & 0xff);
  358. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  359. return 0;
  360. }
  361. /* Extended INQUIRY Data VPD Page */
  362. static int spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  363. {
  364. buf[3] = 0x3c;
  365. /* Set HEADSUP, ORDSUP, SIMPSUP */
  366. buf[5] = 0x07;
  367. /* If WriteCache emulation is enabled, set V_SUP */
  368. if (cmd->se_dev->dev_attrib.emulate_write_cache > 0)
  369. buf[6] = 0x01;
  370. return 0;
  371. }
  372. /* Block Limits VPD page */
  373. static int spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  374. {
  375. struct se_device *dev = cmd->se_dev;
  376. u32 max_sectors;
  377. int have_tp = 0;
  378. /*
  379. * Following spc3r22 section 6.5.3 Block Limits VPD page, when
  380. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  381. * different page length for Thin Provisioning.
  382. */
  383. if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
  384. have_tp = 1;
  385. buf[0] = dev->transport->get_device_type(dev);
  386. buf[3] = have_tp ? 0x3c : 0x10;
  387. /* Set WSNZ to 1 */
  388. buf[4] = 0x01;
  389. /*
  390. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  391. */
  392. put_unaligned_be16(1, &buf[6]);
  393. /*
  394. * Set MAXIMUM TRANSFER LENGTH
  395. */
  396. max_sectors = min(dev->dev_attrib.fabric_max_sectors,
  397. dev->dev_attrib.hw_max_sectors);
  398. put_unaligned_be32(max_sectors, &buf[8]);
  399. /*
  400. * Set OPTIMAL TRANSFER LENGTH
  401. */
  402. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  403. /*
  404. * Exit now if we don't support TP.
  405. */
  406. if (!have_tp)
  407. return 0;
  408. /*
  409. * Set MAXIMUM UNMAP LBA COUNT
  410. */
  411. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  412. /*
  413. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  414. */
  415. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  416. &buf[24]);
  417. /*
  418. * Set OPTIMAL UNMAP GRANULARITY
  419. */
  420. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  421. /*
  422. * UNMAP GRANULARITY ALIGNMENT
  423. */
  424. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  425. &buf[32]);
  426. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  427. buf[32] |= 0x80; /* Set the UGAVALID bit */
  428. return 0;
  429. }
  430. /* Block Device Characteristics VPD page */
  431. static int spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  432. {
  433. struct se_device *dev = cmd->se_dev;
  434. buf[0] = dev->transport->get_device_type(dev);
  435. buf[3] = 0x3c;
  436. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  437. return 0;
  438. }
  439. /* Thin Provisioning VPD */
  440. static int spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  441. {
  442. struct se_device *dev = cmd->se_dev;
  443. /*
  444. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  445. *
  446. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  447. * zero, then the page length shall be set to 0004h. If the DP bit
  448. * is set to one, then the page length shall be set to the value
  449. * defined in table 162.
  450. */
  451. buf[0] = dev->transport->get_device_type(dev);
  452. /*
  453. * Set Hardcoded length mentioned above for DP=0
  454. */
  455. put_unaligned_be16(0x0004, &buf[2]);
  456. /*
  457. * The THRESHOLD EXPONENT field indicates the threshold set size in
  458. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  459. * 2(threshold exponent)).
  460. *
  461. * Note that this is currently set to 0x00 as mkp says it will be
  462. * changing again. We can enable this once it has settled in T10
  463. * and is actually used by Linux/SCSI ML code.
  464. */
  465. buf[4] = 0x00;
  466. /*
  467. * A TPU bit set to one indicates that the device server supports
  468. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  469. * that the device server does not support the UNMAP command.
  470. */
  471. if (dev->dev_attrib.emulate_tpu != 0)
  472. buf[5] = 0x80;
  473. /*
  474. * A TPWS bit set to one indicates that the device server supports
  475. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  476. * A TPWS bit set to zero indicates that the device server does not
  477. * support the use of the WRITE SAME (16) command to unmap LBAs.
  478. */
  479. if (dev->dev_attrib.emulate_tpws != 0)
  480. buf[5] |= 0x40;
  481. return 0;
  482. }
  483. static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  484. static struct {
  485. uint8_t page;
  486. int (*emulate)(struct se_cmd *, unsigned char *);
  487. } evpd_handlers[] = {
  488. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  489. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  490. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  491. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  492. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  493. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  494. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  495. };
  496. /* supported vital product data pages */
  497. static int spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  498. {
  499. int p;
  500. /*
  501. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  502. * Registered Extended LUN WWN has been set via ConfigFS
  503. * during device creation/restart.
  504. */
  505. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  506. buf[3] = ARRAY_SIZE(evpd_handlers);
  507. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  508. buf[p + 4] = evpd_handlers[p].page;
  509. }
  510. return 0;
  511. }
  512. static int spc_emulate_inquiry(struct se_cmd *cmd)
  513. {
  514. struct se_device *dev = cmd->se_dev;
  515. struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
  516. unsigned char *rbuf;
  517. unsigned char *cdb = cmd->t_task_cdb;
  518. unsigned char buf[SE_INQUIRY_BUF];
  519. int p, ret;
  520. memset(buf, 0, SE_INQUIRY_BUF);
  521. if (dev == tpg->tpg_virt_lun0.lun_se_dev)
  522. buf[0] = 0x3f; /* Not connected */
  523. else
  524. buf[0] = dev->transport->get_device_type(dev);
  525. if (!(cdb[1] & 0x1)) {
  526. if (cdb[2]) {
  527. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  528. cdb[2]);
  529. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  530. ret = -EINVAL;
  531. goto out;
  532. }
  533. ret = spc_emulate_inquiry_std(cmd, buf);
  534. goto out;
  535. }
  536. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  537. if (cdb[2] == evpd_handlers[p].page) {
  538. buf[1] = cdb[2];
  539. ret = evpd_handlers[p].emulate(cmd, buf);
  540. goto out;
  541. }
  542. }
  543. pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
  544. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  545. ret = -EINVAL;
  546. out:
  547. rbuf = transport_kmap_data_sg(cmd);
  548. if (rbuf) {
  549. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  550. transport_kunmap_data_sg(cmd);
  551. }
  552. if (!ret)
  553. target_complete_cmd(cmd, GOOD);
  554. return ret;
  555. }
  556. static int spc_modesense_rwrecovery(unsigned char *p)
  557. {
  558. p[0] = 0x01;
  559. p[1] = 0x0a;
  560. return 12;
  561. }
  562. static int spc_modesense_control(struct se_device *dev, unsigned char *p)
  563. {
  564. p[0] = 0x0a;
  565. p[1] = 0x0a;
  566. p[2] = 2;
  567. /*
  568. * From spc4r23, 7.4.7 Control mode page
  569. *
  570. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  571. * restrictions on the algorithm used for reordering commands
  572. * having the SIMPLE task attribute (see SAM-4).
  573. *
  574. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  575. * Code Description
  576. * 0h Restricted reordering
  577. * 1h Unrestricted reordering allowed
  578. * 2h to 7h Reserved
  579. * 8h to Fh Vendor specific
  580. *
  581. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  582. * the device server shall order the processing sequence of commands
  583. * having the SIMPLE task attribute such that data integrity is maintained
  584. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  585. * requests is halted at any time, the final value of all data observable
  586. * on the medium shall be the same as if all the commands had been processed
  587. * with the ORDERED task attribute).
  588. *
  589. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  590. * device server may reorder the processing sequence of commands having the
  591. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  592. * command sequence order shall be explicitly handled by the application client
  593. * through the selection of appropriate ommands and task attributes.
  594. */
  595. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  596. /*
  597. * From spc4r17, section 7.4.6 Control mode Page
  598. *
  599. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  600. *
  601. * 00b: The logical unit shall clear any unit attention condition
  602. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  603. * status and shall not establish a unit attention condition when a com-
  604. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  605. * status.
  606. *
  607. * 10b: The logical unit shall not clear any unit attention condition
  608. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  609. * status and shall not establish a unit attention condition when
  610. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  611. * CONFLICT status.
  612. *
  613. * 11b a The logical unit shall not clear any unit attention condition
  614. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  615. * status and shall establish a unit attention condition for the
  616. * initiator port associated with the I_T nexus on which the BUSY,
  617. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  618. * Depending on the status, the additional sense code shall be set to
  619. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  620. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  621. * command, a unit attention condition shall be established only once
  622. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  623. * to the number of commands completed with one of those status codes.
  624. */
  625. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  626. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  627. /*
  628. * From spc4r17, section 7.4.6 Control mode Page
  629. *
  630. * Task Aborted Status (TAS) bit set to zero.
  631. *
  632. * A task aborted status (TAS) bit set to zero specifies that aborted
  633. * tasks shall be terminated by the device server without any response
  634. * to the application client. A TAS bit set to one specifies that tasks
  635. * aborted by the actions of an I_T nexus other than the I_T nexus on
  636. * which the command was received shall be completed with TASK ABORTED
  637. * status (see SAM-4).
  638. */
  639. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  640. p[8] = 0xff;
  641. p[9] = 0xff;
  642. p[11] = 30;
  643. return 12;
  644. }
  645. static int spc_modesense_caching(struct se_device *dev, unsigned char *p)
  646. {
  647. p[0] = 0x08;
  648. p[1] = 0x12;
  649. if (dev->dev_attrib.emulate_write_cache > 0)
  650. p[2] = 0x04; /* Write Cache Enable */
  651. p[12] = 0x20; /* Disabled Read Ahead */
  652. return 20;
  653. }
  654. static void spc_modesense_write_protect(unsigned char *buf, int type)
  655. {
  656. /*
  657. * I believe that the WP bit (bit 7) in the mode header is the same for
  658. * all device types..
  659. */
  660. switch (type) {
  661. case TYPE_DISK:
  662. case TYPE_TAPE:
  663. default:
  664. buf[0] |= 0x80; /* WP bit */
  665. break;
  666. }
  667. }
  668. static void spc_modesense_dpofua(unsigned char *buf, int type)
  669. {
  670. switch (type) {
  671. case TYPE_DISK:
  672. buf[0] |= 0x10; /* DPOFUA bit */
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. static int spc_emulate_modesense(struct se_cmd *cmd)
  679. {
  680. struct se_device *dev = cmd->se_dev;
  681. char *cdb = cmd->t_task_cdb;
  682. unsigned char *rbuf;
  683. int type = dev->transport->get_device_type(dev);
  684. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  685. u32 offset = ten ? 8 : 4;
  686. int length = 0;
  687. unsigned char buf[SE_MODE_PAGE_BUF];
  688. memset(buf, 0, SE_MODE_PAGE_BUF);
  689. switch (cdb[2] & 0x3f) {
  690. case 0x01:
  691. length = spc_modesense_rwrecovery(&buf[offset]);
  692. break;
  693. case 0x08:
  694. length = spc_modesense_caching(dev, &buf[offset]);
  695. break;
  696. case 0x0a:
  697. length = spc_modesense_control(dev, &buf[offset]);
  698. break;
  699. case 0x3f:
  700. length = spc_modesense_rwrecovery(&buf[offset]);
  701. length += spc_modesense_caching(dev, &buf[offset+length]);
  702. length += spc_modesense_control(dev, &buf[offset+length]);
  703. break;
  704. default:
  705. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  706. cdb[2] & 0x3f, cdb[3]);
  707. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  708. return -EINVAL;
  709. }
  710. offset += length;
  711. if (ten) {
  712. offset -= 2;
  713. buf[0] = (offset >> 8) & 0xff;
  714. buf[1] = offset & 0xff;
  715. offset += 2;
  716. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  717. (cmd->se_deve &&
  718. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  719. spc_modesense_write_protect(&buf[3], type);
  720. if ((dev->dev_attrib.emulate_write_cache > 0) &&
  721. (dev->dev_attrib.emulate_fua_write > 0))
  722. spc_modesense_dpofua(&buf[3], type);
  723. } else {
  724. offset -= 1;
  725. buf[0] = offset & 0xff;
  726. offset += 1;
  727. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  728. (cmd->se_deve &&
  729. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  730. spc_modesense_write_protect(&buf[2], type);
  731. if ((dev->dev_attrib.emulate_write_cache > 0) &&
  732. (dev->dev_attrib.emulate_fua_write > 0))
  733. spc_modesense_dpofua(&buf[2], type);
  734. }
  735. rbuf = transport_kmap_data_sg(cmd);
  736. if (rbuf) {
  737. memcpy(rbuf, buf, min(offset, cmd->data_length));
  738. transport_kunmap_data_sg(cmd);
  739. }
  740. target_complete_cmd(cmd, GOOD);
  741. return 0;
  742. }
  743. static int spc_emulate_request_sense(struct se_cmd *cmd)
  744. {
  745. unsigned char *cdb = cmd->t_task_cdb;
  746. unsigned char *rbuf;
  747. u8 ua_asc = 0, ua_ascq = 0;
  748. unsigned char buf[SE_SENSE_BUF];
  749. memset(buf, 0, SE_SENSE_BUF);
  750. if (cdb[1] & 0x01) {
  751. pr_err("REQUEST_SENSE description emulation not"
  752. " supported\n");
  753. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  754. return -ENOSYS;
  755. }
  756. rbuf = transport_kmap_data_sg(cmd);
  757. if (cmd->scsi_sense_reason != 0) {
  758. /*
  759. * Out of memory. We will fail with CHECK CONDITION, so
  760. * we must not clear the unit attention condition.
  761. */
  762. target_complete_cmd(cmd, CHECK_CONDITION);
  763. return 0;
  764. } else if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  765. /*
  766. * CURRENT ERROR, UNIT ATTENTION
  767. */
  768. buf[0] = 0x70;
  769. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  770. /*
  771. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  772. */
  773. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  774. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  775. buf[7] = 0x0A;
  776. } else {
  777. /*
  778. * CURRENT ERROR, NO SENSE
  779. */
  780. buf[0] = 0x70;
  781. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  782. /*
  783. * NO ADDITIONAL SENSE INFORMATION
  784. */
  785. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  786. buf[7] = 0x0A;
  787. }
  788. if (rbuf) {
  789. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  790. transport_kunmap_data_sg(cmd);
  791. }
  792. target_complete_cmd(cmd, GOOD);
  793. return 0;
  794. }
  795. int spc_emulate_report_luns(struct se_cmd *cmd)
  796. {
  797. struct se_dev_entry *deve;
  798. struct se_session *sess = cmd->se_sess;
  799. unsigned char *buf;
  800. u32 lun_count = 0, offset = 8, i;
  801. if (cmd->data_length < 16) {
  802. pr_warn("REPORT LUNS allocation length %u too small\n",
  803. cmd->data_length);
  804. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  805. return -EINVAL;
  806. }
  807. buf = transport_kmap_data_sg(cmd);
  808. if (!buf)
  809. return -ENOMEM;
  810. /*
  811. * If no struct se_session pointer is present, this struct se_cmd is
  812. * coming via a target_core_mod PASSTHROUGH op, and not through
  813. * a $FABRIC_MOD. In that case, report LUN=0 only.
  814. */
  815. if (!sess) {
  816. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  817. lun_count = 1;
  818. goto done;
  819. }
  820. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  821. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  822. deve = sess->se_node_acl->device_list[i];
  823. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  824. continue;
  825. /*
  826. * We determine the correct LUN LIST LENGTH even once we
  827. * have reached the initial allocation length.
  828. * See SPC2-R20 7.19.
  829. */
  830. lun_count++;
  831. if ((offset + 8) > cmd->data_length)
  832. continue;
  833. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  834. offset += 8;
  835. }
  836. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  837. /*
  838. * See SPC3 r07, page 159.
  839. */
  840. done:
  841. lun_count *= 8;
  842. buf[0] = ((lun_count >> 24) & 0xff);
  843. buf[1] = ((lun_count >> 16) & 0xff);
  844. buf[2] = ((lun_count >> 8) & 0xff);
  845. buf[3] = (lun_count & 0xff);
  846. transport_kunmap_data_sg(cmd);
  847. target_complete_cmd(cmd, GOOD);
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(spc_emulate_report_luns);
  851. static int spc_emulate_testunitready(struct se_cmd *cmd)
  852. {
  853. target_complete_cmd(cmd, GOOD);
  854. return 0;
  855. }
  856. int spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  857. {
  858. struct se_device *dev = cmd->se_dev;
  859. unsigned char *cdb = cmd->t_task_cdb;
  860. switch (cdb[0]) {
  861. case MODE_SELECT:
  862. *size = cdb[4];
  863. break;
  864. case MODE_SELECT_10:
  865. *size = (cdb[7] << 8) + cdb[8];
  866. break;
  867. case MODE_SENSE:
  868. *size = cdb[4];
  869. cmd->execute_cmd = spc_emulate_modesense;
  870. break;
  871. case MODE_SENSE_10:
  872. *size = (cdb[7] << 8) + cdb[8];
  873. cmd->execute_cmd = spc_emulate_modesense;
  874. break;
  875. case LOG_SELECT:
  876. case LOG_SENSE:
  877. *size = (cdb[7] << 8) + cdb[8];
  878. break;
  879. case PERSISTENT_RESERVE_IN:
  880. *size = (cdb[7] << 8) + cdb[8];
  881. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  882. break;
  883. case PERSISTENT_RESERVE_OUT:
  884. *size = (cdb[7] << 8) + cdb[8];
  885. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  886. break;
  887. case RELEASE:
  888. case RELEASE_10:
  889. if (cdb[0] == RELEASE_10)
  890. *size = (cdb[7] << 8) | cdb[8];
  891. else
  892. *size = cmd->data_length;
  893. cmd->execute_cmd = target_scsi2_reservation_release;
  894. break;
  895. case RESERVE:
  896. case RESERVE_10:
  897. /*
  898. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  899. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  900. */
  901. if (cdb[0] == RESERVE_10)
  902. *size = (cdb[7] << 8) | cdb[8];
  903. else
  904. *size = cmd->data_length;
  905. cmd->execute_cmd = target_scsi2_reservation_reserve;
  906. break;
  907. case REQUEST_SENSE:
  908. *size = cdb[4];
  909. cmd->execute_cmd = spc_emulate_request_sense;
  910. break;
  911. case INQUIRY:
  912. *size = (cdb[3] << 8) + cdb[4];
  913. /*
  914. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  915. * See spc4r17 section 5.3
  916. */
  917. cmd->sam_task_attr = MSG_HEAD_TAG;
  918. cmd->execute_cmd = spc_emulate_inquiry;
  919. break;
  920. case SECURITY_PROTOCOL_IN:
  921. case SECURITY_PROTOCOL_OUT:
  922. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  923. break;
  924. case EXTENDED_COPY:
  925. case READ_ATTRIBUTE:
  926. case RECEIVE_COPY_RESULTS:
  927. case WRITE_ATTRIBUTE:
  928. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  929. (cdb[12] << 8) | cdb[13];
  930. break;
  931. case RECEIVE_DIAGNOSTIC:
  932. case SEND_DIAGNOSTIC:
  933. *size = (cdb[3] << 8) | cdb[4];
  934. break;
  935. case WRITE_BUFFER:
  936. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  937. break;
  938. case REPORT_LUNS:
  939. cmd->execute_cmd = spc_emulate_report_luns;
  940. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  941. /*
  942. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  943. * See spc4r17 section 5.3
  944. */
  945. cmd->sam_task_attr = MSG_HEAD_TAG;
  946. break;
  947. case TEST_UNIT_READY:
  948. cmd->execute_cmd = spc_emulate_testunitready;
  949. *size = 0;
  950. break;
  951. case MAINTENANCE_IN:
  952. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  953. /*
  954. * MAINTENANCE_IN from SCC-2
  955. * Check for emulated MI_REPORT_TARGET_PGS
  956. */
  957. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  958. cmd->execute_cmd =
  959. target_emulate_report_target_port_groups;
  960. }
  961. *size = get_unaligned_be32(&cdb[6]);
  962. } else {
  963. /*
  964. * GPCMD_SEND_KEY from multi media commands
  965. */
  966. *size = get_unaligned_be16(&cdb[8]);
  967. }
  968. break;
  969. case MAINTENANCE_OUT:
  970. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  971. /*
  972. * MAINTENANCE_OUT from SCC-2
  973. * Check for emulated MO_SET_TARGET_PGS.
  974. */
  975. if (cdb[1] == MO_SET_TARGET_PGS) {
  976. cmd->execute_cmd =
  977. target_emulate_set_target_port_groups;
  978. }
  979. *size = get_unaligned_be32(&cdb[6]);
  980. } else {
  981. /*
  982. * GPCMD_SEND_KEY from multi media commands
  983. */
  984. *size = get_unaligned_be16(&cdb[8]);
  985. }
  986. break;
  987. default:
  988. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  989. " 0x%02x, sending CHECK_CONDITION.\n",
  990. cmd->se_tfo->get_fabric_name(), cdb[0]);
  991. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  992. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  993. return -EINVAL;
  994. }
  995. return 0;
  996. }
  997. EXPORT_SYMBOL(spc_parse_cdb);