target_core_spc.c 34 KB

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