target_core_spc.c 35 KB

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