target_core_spc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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] = 0x05; /* SPC-3 */
  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(struct se_device *dev, u8 pc, u8 *p)
  557. {
  558. p[0] = 0x01;
  559. p[1] = 0x0a;
  560. /* No changeable values for now */
  561. if (pc == 1)
  562. goto out;
  563. out:
  564. return 12;
  565. }
  566. static int spc_modesense_control(struct se_device *dev, u8 pc, u8 *p)
  567. {
  568. p[0] = 0x0a;
  569. p[1] = 0x0a;
  570. /* No changeable values for now */
  571. if (pc == 1)
  572. goto out;
  573. p[2] = 2;
  574. /*
  575. * From spc4r23, 7.4.7 Control mode page
  576. *
  577. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  578. * restrictions on the algorithm used for reordering commands
  579. * having the SIMPLE task attribute (see SAM-4).
  580. *
  581. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  582. * Code Description
  583. * 0h Restricted reordering
  584. * 1h Unrestricted reordering allowed
  585. * 2h to 7h Reserved
  586. * 8h to Fh Vendor specific
  587. *
  588. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  589. * the device server shall order the processing sequence of commands
  590. * having the SIMPLE task attribute such that data integrity is maintained
  591. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  592. * requests is halted at any time, the final value of all data observable
  593. * on the medium shall be the same as if all the commands had been processed
  594. * with the ORDERED task attribute).
  595. *
  596. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  597. * device server may reorder the processing sequence of commands having the
  598. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  599. * command sequence order shall be explicitly handled by the application client
  600. * through the selection of appropriate ommands and task attributes.
  601. */
  602. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  603. /*
  604. * From spc4r17, section 7.4.6 Control mode Page
  605. *
  606. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  607. *
  608. * 00b: The logical unit shall clear any unit attention condition
  609. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  610. * status and shall not establish a unit attention condition when a com-
  611. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  612. * status.
  613. *
  614. * 10b: The logical unit shall not clear any unit attention condition
  615. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  616. * status and shall not establish a unit attention condition when
  617. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  618. * CONFLICT status.
  619. *
  620. * 11b a The logical unit shall not clear any unit attention condition
  621. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  622. * status and shall establish a unit attention condition for the
  623. * initiator port associated with the I_T nexus on which the BUSY,
  624. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  625. * Depending on the status, the additional sense code shall be set to
  626. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  627. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  628. * command, a unit attention condition shall be established only once
  629. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  630. * to the number of commands completed with one of those status codes.
  631. */
  632. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  633. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  634. /*
  635. * From spc4r17, section 7.4.6 Control mode Page
  636. *
  637. * Task Aborted Status (TAS) bit set to zero.
  638. *
  639. * A task aborted status (TAS) bit set to zero specifies that aborted
  640. * tasks shall be terminated by the device server without any response
  641. * to the application client. A TAS bit set to one specifies that tasks
  642. * aborted by the actions of an I_T nexus other than the I_T nexus on
  643. * which the command was received shall be completed with TASK ABORTED
  644. * status (see SAM-4).
  645. */
  646. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  647. p[8] = 0xff;
  648. p[9] = 0xff;
  649. p[11] = 30;
  650. out:
  651. return 12;
  652. }
  653. static int spc_modesense_caching(struct se_device *dev, u8 pc, u8 *p)
  654. {
  655. p[0] = 0x08;
  656. p[1] = 0x12;
  657. /* No changeable values for now */
  658. if (pc == 1)
  659. goto out;
  660. if (dev->dev_attrib.emulate_write_cache > 0)
  661. p[2] = 0x04; /* Write Cache Enable */
  662. p[12] = 0x20; /* Disabled Read Ahead */
  663. out:
  664. return 20;
  665. }
  666. static struct {
  667. uint8_t page;
  668. uint8_t subpage;
  669. int (*emulate)(struct se_device *, u8, unsigned char *);
  670. } modesense_handlers[] = {
  671. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  672. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  673. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  674. };
  675. static void spc_modesense_write_protect(unsigned char *buf, int type)
  676. {
  677. /*
  678. * I believe that the WP bit (bit 7) in the mode header is the same for
  679. * all device types..
  680. */
  681. switch (type) {
  682. case TYPE_DISK:
  683. case TYPE_TAPE:
  684. default:
  685. buf[0] |= 0x80; /* WP bit */
  686. break;
  687. }
  688. }
  689. static void spc_modesense_dpofua(unsigned char *buf, int type)
  690. {
  691. switch (type) {
  692. case TYPE_DISK:
  693. buf[0] |= 0x10; /* DPOFUA bit */
  694. break;
  695. default:
  696. break;
  697. }
  698. }
  699. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  700. {
  701. *buf++ = 8;
  702. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  703. buf += 4;
  704. put_unaligned_be32(block_size, buf);
  705. return 9;
  706. }
  707. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  708. {
  709. if (blocks <= 0xffffffff)
  710. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  711. *buf++ = 1; /* LONGLBA */
  712. buf += 2;
  713. *buf++ = 16;
  714. put_unaligned_be64(blocks, buf);
  715. buf += 12;
  716. put_unaligned_be32(block_size, buf);
  717. return 17;
  718. }
  719. static int spc_emulate_modesense(struct se_cmd *cmd)
  720. {
  721. struct se_device *dev = cmd->se_dev;
  722. char *cdb = cmd->t_task_cdb;
  723. unsigned char *buf, *map_buf;
  724. int type = dev->transport->get_device_type(dev);
  725. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  726. bool dbd = !!(cdb[1] & 0x08);
  727. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  728. u8 pc = cdb[2] >> 6;
  729. u8 page = cdb[2] & 0x3f;
  730. u8 subpage = cdb[3];
  731. int length = 0;
  732. int ret;
  733. int i;
  734. map_buf = transport_kmap_data_sg(cmd);
  735. /*
  736. * If SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is not set, then we
  737. * know we actually allocated a full page. Otherwise, if the
  738. * data buffer is too small, allocate a temporary buffer so we
  739. * don't have to worry about overruns in all our INQUIRY
  740. * emulation handling.
  741. */
  742. if (cmd->data_length < SE_MODE_PAGE_BUF &&
  743. (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  744. buf = kzalloc(SE_MODE_PAGE_BUF, GFP_KERNEL);
  745. if (!buf) {
  746. transport_kunmap_data_sg(cmd);
  747. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  748. return -ENOMEM;
  749. }
  750. } else {
  751. buf = map_buf;
  752. }
  753. length = ten ? 2 : 1;
  754. /* DEVICE-SPECIFIC PARAMETER */
  755. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  756. (cmd->se_deve &&
  757. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  758. spc_modesense_write_protect(&buf[length], type);
  759. if ((dev->dev_attrib.emulate_write_cache > 0) &&
  760. (dev->dev_attrib.emulate_fua_write > 0))
  761. spc_modesense_dpofua(&buf[length], type);
  762. ++length;
  763. /* BLOCK DESCRIPTOR */
  764. /*
  765. * For now we only include a block descriptor for disk (SBC)
  766. * devices; other command sets use a slightly different format.
  767. */
  768. if (!dbd && type == TYPE_DISK) {
  769. u64 blocks = dev->transport->get_blocks(dev);
  770. u32 block_size = dev->dev_attrib.block_size;
  771. if (ten) {
  772. if (llba) {
  773. length += spc_modesense_long_blockdesc(&buf[length],
  774. blocks, block_size);
  775. } else {
  776. length += 3;
  777. length += spc_modesense_blockdesc(&buf[length],
  778. blocks, block_size);
  779. }
  780. } else {
  781. length += spc_modesense_blockdesc(&buf[length], blocks,
  782. block_size);
  783. }
  784. } else {
  785. if (ten)
  786. length += 4;
  787. else
  788. length += 1;
  789. }
  790. if (page == 0x3f) {
  791. if (subpage != 0x00 && subpage != 0xff) {
  792. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  793. length = -EINVAL;
  794. goto out;
  795. }
  796. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  797. /*
  798. * Tricky way to say all subpage 00h for
  799. * subpage==0, all subpages for subpage==0xff
  800. * (and we just checked above that those are
  801. * the only two possibilities).
  802. */
  803. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  804. ret = modesense_handlers[i].emulate(dev, pc, &buf[length]);
  805. if (!ten && length + ret >= 255)
  806. break;
  807. length += ret;
  808. }
  809. }
  810. goto set_length;
  811. }
  812. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  813. if (modesense_handlers[i].page == page &&
  814. modesense_handlers[i].subpage == subpage) {
  815. length += modesense_handlers[i].emulate(dev, pc, &buf[length]);
  816. goto set_length;
  817. }
  818. /*
  819. * We don't intend to implement:
  820. * - obsolete page 03h "format parameters" (checked by Solaris)
  821. */
  822. if (page != 0x03)
  823. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  824. page, subpage);
  825. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  826. return -EINVAL;
  827. set_length:
  828. if (ten)
  829. put_unaligned_be16(length - 2, buf);
  830. else
  831. buf[0] = length - 1;
  832. out:
  833. if (buf != map_buf) {
  834. memcpy(map_buf, buf, cmd->data_length);
  835. kfree(buf);
  836. }
  837. transport_kunmap_data_sg(cmd);
  838. target_complete_cmd(cmd, GOOD);
  839. return 0;
  840. }
  841. static int spc_emulate_request_sense(struct se_cmd *cmd)
  842. {
  843. unsigned char *cdb = cmd->t_task_cdb;
  844. unsigned char *rbuf;
  845. u8 ua_asc = 0, ua_ascq = 0;
  846. unsigned char buf[SE_SENSE_BUF];
  847. memset(buf, 0, SE_SENSE_BUF);
  848. if (cdb[1] & 0x01) {
  849. pr_err("REQUEST_SENSE description emulation not"
  850. " supported\n");
  851. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  852. return -ENOSYS;
  853. }
  854. rbuf = transport_kmap_data_sg(cmd);
  855. if (cmd->scsi_sense_reason != 0) {
  856. /*
  857. * Out of memory. We will fail with CHECK CONDITION, so
  858. * we must not clear the unit attention condition.
  859. */
  860. target_complete_cmd(cmd, CHECK_CONDITION);
  861. return 0;
  862. } else if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  863. /*
  864. * CURRENT ERROR, UNIT ATTENTION
  865. */
  866. buf[0] = 0x70;
  867. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  868. /*
  869. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  870. */
  871. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  872. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  873. buf[7] = 0x0A;
  874. } else {
  875. /*
  876. * CURRENT ERROR, NO SENSE
  877. */
  878. buf[0] = 0x70;
  879. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  880. /*
  881. * NO ADDITIONAL SENSE INFORMATION
  882. */
  883. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  884. buf[7] = 0x0A;
  885. }
  886. if (rbuf) {
  887. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  888. transport_kunmap_data_sg(cmd);
  889. }
  890. target_complete_cmd(cmd, GOOD);
  891. return 0;
  892. }
  893. int spc_emulate_report_luns(struct se_cmd *cmd)
  894. {
  895. struct se_dev_entry *deve;
  896. struct se_session *sess = cmd->se_sess;
  897. unsigned char *buf;
  898. u32 lun_count = 0, offset = 8, i;
  899. if (cmd->data_length < 16) {
  900. pr_warn("REPORT LUNS allocation length %u too small\n",
  901. cmd->data_length);
  902. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  903. return -EINVAL;
  904. }
  905. buf = transport_kmap_data_sg(cmd);
  906. if (!buf)
  907. return -ENOMEM;
  908. /*
  909. * If no struct se_session pointer is present, this struct se_cmd is
  910. * coming via a target_core_mod PASSTHROUGH op, and not through
  911. * a $FABRIC_MOD. In that case, report LUN=0 only.
  912. */
  913. if (!sess) {
  914. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  915. lun_count = 1;
  916. goto done;
  917. }
  918. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  919. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  920. deve = sess->se_node_acl->device_list[i];
  921. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  922. continue;
  923. /*
  924. * We determine the correct LUN LIST LENGTH even once we
  925. * have reached the initial allocation length.
  926. * See SPC2-R20 7.19.
  927. */
  928. lun_count++;
  929. if ((offset + 8) > cmd->data_length)
  930. continue;
  931. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  932. offset += 8;
  933. }
  934. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  935. /*
  936. * See SPC3 r07, page 159.
  937. */
  938. done:
  939. lun_count *= 8;
  940. buf[0] = ((lun_count >> 24) & 0xff);
  941. buf[1] = ((lun_count >> 16) & 0xff);
  942. buf[2] = ((lun_count >> 8) & 0xff);
  943. buf[3] = (lun_count & 0xff);
  944. transport_kunmap_data_sg(cmd);
  945. target_complete_cmd(cmd, GOOD);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL(spc_emulate_report_luns);
  949. static int spc_emulate_testunitready(struct se_cmd *cmd)
  950. {
  951. target_complete_cmd(cmd, GOOD);
  952. return 0;
  953. }
  954. int spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  955. {
  956. struct se_device *dev = cmd->se_dev;
  957. unsigned char *cdb = cmd->t_task_cdb;
  958. switch (cdb[0]) {
  959. case MODE_SELECT:
  960. *size = cdb[4];
  961. break;
  962. case MODE_SELECT_10:
  963. *size = (cdb[7] << 8) + cdb[8];
  964. break;
  965. case MODE_SENSE:
  966. *size = cdb[4];
  967. cmd->execute_cmd = spc_emulate_modesense;
  968. break;
  969. case MODE_SENSE_10:
  970. *size = (cdb[7] << 8) + cdb[8];
  971. cmd->execute_cmd = spc_emulate_modesense;
  972. break;
  973. case LOG_SELECT:
  974. case LOG_SENSE:
  975. *size = (cdb[7] << 8) + cdb[8];
  976. break;
  977. case PERSISTENT_RESERVE_IN:
  978. *size = (cdb[7] << 8) + cdb[8];
  979. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  980. break;
  981. case PERSISTENT_RESERVE_OUT:
  982. *size = (cdb[7] << 8) + cdb[8];
  983. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  984. break;
  985. case RELEASE:
  986. case RELEASE_10:
  987. if (cdb[0] == RELEASE_10)
  988. *size = (cdb[7] << 8) | cdb[8];
  989. else
  990. *size = cmd->data_length;
  991. cmd->execute_cmd = target_scsi2_reservation_release;
  992. break;
  993. case RESERVE:
  994. case RESERVE_10:
  995. /*
  996. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  997. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  998. */
  999. if (cdb[0] == RESERVE_10)
  1000. *size = (cdb[7] << 8) | cdb[8];
  1001. else
  1002. *size = cmd->data_length;
  1003. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1004. break;
  1005. case REQUEST_SENSE:
  1006. *size = cdb[4];
  1007. cmd->execute_cmd = spc_emulate_request_sense;
  1008. break;
  1009. case INQUIRY:
  1010. *size = (cdb[3] << 8) + cdb[4];
  1011. /*
  1012. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  1013. * See spc4r17 section 5.3
  1014. */
  1015. cmd->sam_task_attr = MSG_HEAD_TAG;
  1016. cmd->execute_cmd = spc_emulate_inquiry;
  1017. break;
  1018. case SECURITY_PROTOCOL_IN:
  1019. case SECURITY_PROTOCOL_OUT:
  1020. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1021. break;
  1022. case EXTENDED_COPY:
  1023. case READ_ATTRIBUTE:
  1024. case RECEIVE_COPY_RESULTS:
  1025. case WRITE_ATTRIBUTE:
  1026. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1027. (cdb[12] << 8) | cdb[13];
  1028. break;
  1029. case RECEIVE_DIAGNOSTIC:
  1030. case SEND_DIAGNOSTIC:
  1031. *size = (cdb[3] << 8) | cdb[4];
  1032. break;
  1033. case WRITE_BUFFER:
  1034. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1035. break;
  1036. case REPORT_LUNS:
  1037. cmd->execute_cmd = spc_emulate_report_luns;
  1038. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1039. /*
  1040. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  1041. * See spc4r17 section 5.3
  1042. */
  1043. cmd->sam_task_attr = MSG_HEAD_TAG;
  1044. break;
  1045. case TEST_UNIT_READY:
  1046. cmd->execute_cmd = spc_emulate_testunitready;
  1047. *size = 0;
  1048. break;
  1049. case MAINTENANCE_IN:
  1050. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1051. /*
  1052. * MAINTENANCE_IN from SCC-2
  1053. * Check for emulated MI_REPORT_TARGET_PGS
  1054. */
  1055. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1056. cmd->execute_cmd =
  1057. target_emulate_report_target_port_groups;
  1058. }
  1059. *size = get_unaligned_be32(&cdb[6]);
  1060. } else {
  1061. /*
  1062. * GPCMD_SEND_KEY from multi media commands
  1063. */
  1064. *size = get_unaligned_be16(&cdb[8]);
  1065. }
  1066. break;
  1067. case MAINTENANCE_OUT:
  1068. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1069. /*
  1070. * MAINTENANCE_OUT from SCC-2
  1071. * Check for emulated MO_SET_TARGET_PGS.
  1072. */
  1073. if (cdb[1] == MO_SET_TARGET_PGS) {
  1074. cmd->execute_cmd =
  1075. target_emulate_set_target_port_groups;
  1076. }
  1077. *size = get_unaligned_be32(&cdb[6]);
  1078. } else {
  1079. /*
  1080. * GPCMD_SEND_KEY from multi media commands
  1081. */
  1082. *size = get_unaligned_be16(&cdb[8]);
  1083. }
  1084. break;
  1085. default:
  1086. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1087. " 0x%02x, sending CHECK_CONDITION.\n",
  1088. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1089. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1090. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1091. return -EINVAL;
  1092. }
  1093. return 0;
  1094. }
  1095. EXPORT_SYMBOL(spc_parse_cdb);