target_core_spc.c 34 KB

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