target_core_spc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 int spc_modesense_informational_exceptions(struct se_device *dev, u8 pc, unsigned char *p)
  667. {
  668. p[0] = 0x1c;
  669. p[1] = 0x0a;
  670. /* No changeable values for now */
  671. if (pc == 1)
  672. goto out;
  673. out:
  674. return 12;
  675. }
  676. static struct {
  677. uint8_t page;
  678. uint8_t subpage;
  679. int (*emulate)(struct se_device *, u8, unsigned char *);
  680. } modesense_handlers[] = {
  681. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  682. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  683. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  684. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  685. };
  686. static void spc_modesense_write_protect(unsigned char *buf, int type)
  687. {
  688. /*
  689. * I believe that the WP bit (bit 7) in the mode header is the same for
  690. * all device types..
  691. */
  692. switch (type) {
  693. case TYPE_DISK:
  694. case TYPE_TAPE:
  695. default:
  696. buf[0] |= 0x80; /* WP bit */
  697. break;
  698. }
  699. }
  700. static void spc_modesense_dpofua(unsigned char *buf, int type)
  701. {
  702. switch (type) {
  703. case TYPE_DISK:
  704. buf[0] |= 0x10; /* DPOFUA bit */
  705. break;
  706. default:
  707. break;
  708. }
  709. }
  710. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  711. {
  712. *buf++ = 8;
  713. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  714. buf += 4;
  715. put_unaligned_be32(block_size, buf);
  716. return 9;
  717. }
  718. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  719. {
  720. if (blocks <= 0xffffffff)
  721. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  722. *buf++ = 1; /* LONGLBA */
  723. buf += 2;
  724. *buf++ = 16;
  725. put_unaligned_be64(blocks, buf);
  726. buf += 12;
  727. put_unaligned_be32(block_size, buf);
  728. return 17;
  729. }
  730. static int spc_emulate_modesense(struct se_cmd *cmd)
  731. {
  732. struct se_device *dev = cmd->se_dev;
  733. char *cdb = cmd->t_task_cdb;
  734. unsigned char *buf, *map_buf;
  735. int type = dev->transport->get_device_type(dev);
  736. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  737. bool dbd = !!(cdb[1] & 0x08);
  738. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  739. u8 pc = cdb[2] >> 6;
  740. u8 page = cdb[2] & 0x3f;
  741. u8 subpage = cdb[3];
  742. int length = 0;
  743. int ret;
  744. int i;
  745. map_buf = transport_kmap_data_sg(cmd);
  746. /*
  747. * If SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is not set, then we
  748. * know we actually allocated a full page. Otherwise, if the
  749. * data buffer is too small, allocate a temporary buffer so we
  750. * don't have to worry about overruns in all our INQUIRY
  751. * emulation handling.
  752. */
  753. if (cmd->data_length < SE_MODE_PAGE_BUF &&
  754. (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
  755. buf = kzalloc(SE_MODE_PAGE_BUF, GFP_KERNEL);
  756. if (!buf) {
  757. transport_kunmap_data_sg(cmd);
  758. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  759. return -ENOMEM;
  760. }
  761. } else {
  762. buf = map_buf;
  763. }
  764. length = ten ? 2 : 1;
  765. /* DEVICE-SPECIFIC PARAMETER */
  766. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  767. (cmd->se_deve &&
  768. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  769. spc_modesense_write_protect(&buf[length], type);
  770. if ((dev->dev_attrib.emulate_write_cache > 0) &&
  771. (dev->dev_attrib.emulate_fua_write > 0))
  772. spc_modesense_dpofua(&buf[length], type);
  773. ++length;
  774. /* BLOCK DESCRIPTOR */
  775. /*
  776. * For now we only include a block descriptor for disk (SBC)
  777. * devices; other command sets use a slightly different format.
  778. */
  779. if (!dbd && type == TYPE_DISK) {
  780. u64 blocks = dev->transport->get_blocks(dev);
  781. u32 block_size = dev->dev_attrib.block_size;
  782. if (ten) {
  783. if (llba) {
  784. length += spc_modesense_long_blockdesc(&buf[length],
  785. blocks, block_size);
  786. } else {
  787. length += 3;
  788. length += spc_modesense_blockdesc(&buf[length],
  789. blocks, block_size);
  790. }
  791. } else {
  792. length += spc_modesense_blockdesc(&buf[length], blocks,
  793. block_size);
  794. }
  795. } else {
  796. if (ten)
  797. length += 4;
  798. else
  799. length += 1;
  800. }
  801. if (page == 0x3f) {
  802. if (subpage != 0x00 && subpage != 0xff) {
  803. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  804. length = -EINVAL;
  805. goto out;
  806. }
  807. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  808. /*
  809. * Tricky way to say all subpage 00h for
  810. * subpage==0, all subpages for subpage==0xff
  811. * (and we just checked above that those are
  812. * the only two possibilities).
  813. */
  814. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  815. ret = modesense_handlers[i].emulate(dev, pc, &buf[length]);
  816. if (!ten && length + ret >= 255)
  817. break;
  818. length += ret;
  819. }
  820. }
  821. goto set_length;
  822. }
  823. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  824. if (modesense_handlers[i].page == page &&
  825. modesense_handlers[i].subpage == subpage) {
  826. length += modesense_handlers[i].emulate(dev, pc, &buf[length]);
  827. goto set_length;
  828. }
  829. /*
  830. * We don't intend to implement:
  831. * - obsolete page 03h "format parameters" (checked by Solaris)
  832. */
  833. if (page != 0x03)
  834. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  835. page, subpage);
  836. cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
  837. return -EINVAL;
  838. set_length:
  839. if (ten)
  840. put_unaligned_be16(length - 2, buf);
  841. else
  842. buf[0] = length - 1;
  843. out:
  844. if (buf != map_buf) {
  845. memcpy(map_buf, buf, cmd->data_length);
  846. kfree(buf);
  847. }
  848. transport_kunmap_data_sg(cmd);
  849. target_complete_cmd(cmd, GOOD);
  850. return 0;
  851. }
  852. static int spc_emulate_request_sense(struct se_cmd *cmd)
  853. {
  854. unsigned char *cdb = cmd->t_task_cdb;
  855. unsigned char *rbuf;
  856. u8 ua_asc = 0, ua_ascq = 0;
  857. unsigned char buf[SE_SENSE_BUF];
  858. memset(buf, 0, SE_SENSE_BUF);
  859. if (cdb[1] & 0x01) {
  860. pr_err("REQUEST_SENSE description emulation not"
  861. " supported\n");
  862. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  863. return -ENOSYS;
  864. }
  865. rbuf = transport_kmap_data_sg(cmd);
  866. if (cmd->scsi_sense_reason != 0) {
  867. /*
  868. * Out of memory. We will fail with CHECK CONDITION, so
  869. * we must not clear the unit attention condition.
  870. */
  871. target_complete_cmd(cmd, CHECK_CONDITION);
  872. return 0;
  873. } else if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  874. /*
  875. * CURRENT ERROR, UNIT ATTENTION
  876. */
  877. buf[0] = 0x70;
  878. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  879. /*
  880. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  881. */
  882. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  883. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  884. buf[7] = 0x0A;
  885. } else {
  886. /*
  887. * CURRENT ERROR, NO SENSE
  888. */
  889. buf[0] = 0x70;
  890. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  891. /*
  892. * NO ADDITIONAL SENSE INFORMATION
  893. */
  894. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  895. buf[7] = 0x0A;
  896. }
  897. if (rbuf) {
  898. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  899. transport_kunmap_data_sg(cmd);
  900. }
  901. target_complete_cmd(cmd, GOOD);
  902. return 0;
  903. }
  904. int spc_emulate_report_luns(struct se_cmd *cmd)
  905. {
  906. struct se_dev_entry *deve;
  907. struct se_session *sess = cmd->se_sess;
  908. unsigned char *buf;
  909. u32 lun_count = 0, offset = 8, i;
  910. if (cmd->data_length < 16) {
  911. pr_warn("REPORT LUNS allocation length %u too small\n",
  912. cmd->data_length);
  913. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  914. return -EINVAL;
  915. }
  916. buf = transport_kmap_data_sg(cmd);
  917. if (!buf)
  918. return -ENOMEM;
  919. /*
  920. * If no struct se_session pointer is present, this struct se_cmd is
  921. * coming via a target_core_mod PASSTHROUGH op, and not through
  922. * a $FABRIC_MOD. In that case, report LUN=0 only.
  923. */
  924. if (!sess) {
  925. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  926. lun_count = 1;
  927. goto done;
  928. }
  929. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  930. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  931. deve = sess->se_node_acl->device_list[i];
  932. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  933. continue;
  934. /*
  935. * We determine the correct LUN LIST LENGTH even once we
  936. * have reached the initial allocation length.
  937. * See SPC2-R20 7.19.
  938. */
  939. lun_count++;
  940. if ((offset + 8) > cmd->data_length)
  941. continue;
  942. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  943. offset += 8;
  944. }
  945. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  946. /*
  947. * See SPC3 r07, page 159.
  948. */
  949. done:
  950. lun_count *= 8;
  951. buf[0] = ((lun_count >> 24) & 0xff);
  952. buf[1] = ((lun_count >> 16) & 0xff);
  953. buf[2] = ((lun_count >> 8) & 0xff);
  954. buf[3] = (lun_count & 0xff);
  955. transport_kunmap_data_sg(cmd);
  956. target_complete_cmd(cmd, GOOD);
  957. return 0;
  958. }
  959. EXPORT_SYMBOL(spc_emulate_report_luns);
  960. static int spc_emulate_testunitready(struct se_cmd *cmd)
  961. {
  962. target_complete_cmd(cmd, GOOD);
  963. return 0;
  964. }
  965. int spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  966. {
  967. struct se_device *dev = cmd->se_dev;
  968. unsigned char *cdb = cmd->t_task_cdb;
  969. switch (cdb[0]) {
  970. case MODE_SELECT:
  971. *size = cdb[4];
  972. break;
  973. case MODE_SELECT_10:
  974. *size = (cdb[7] << 8) + cdb[8];
  975. break;
  976. case MODE_SENSE:
  977. *size = cdb[4];
  978. cmd->execute_cmd = spc_emulate_modesense;
  979. break;
  980. case MODE_SENSE_10:
  981. *size = (cdb[7] << 8) + cdb[8];
  982. cmd->execute_cmd = spc_emulate_modesense;
  983. break;
  984. case LOG_SELECT:
  985. case LOG_SENSE:
  986. *size = (cdb[7] << 8) + cdb[8];
  987. break;
  988. case PERSISTENT_RESERVE_IN:
  989. *size = (cdb[7] << 8) + cdb[8];
  990. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  991. break;
  992. case PERSISTENT_RESERVE_OUT:
  993. *size = (cdb[7] << 8) + cdb[8];
  994. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  995. break;
  996. case RELEASE:
  997. case RELEASE_10:
  998. if (cdb[0] == RELEASE_10)
  999. *size = (cdb[7] << 8) | cdb[8];
  1000. else
  1001. *size = cmd->data_length;
  1002. cmd->execute_cmd = target_scsi2_reservation_release;
  1003. break;
  1004. case RESERVE:
  1005. case RESERVE_10:
  1006. /*
  1007. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1008. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1009. */
  1010. if (cdb[0] == RESERVE_10)
  1011. *size = (cdb[7] << 8) | cdb[8];
  1012. else
  1013. *size = cmd->data_length;
  1014. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1015. break;
  1016. case REQUEST_SENSE:
  1017. *size = cdb[4];
  1018. cmd->execute_cmd = spc_emulate_request_sense;
  1019. break;
  1020. case INQUIRY:
  1021. *size = (cdb[3] << 8) + cdb[4];
  1022. /*
  1023. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  1024. * See spc4r17 section 5.3
  1025. */
  1026. cmd->sam_task_attr = MSG_HEAD_TAG;
  1027. cmd->execute_cmd = spc_emulate_inquiry;
  1028. break;
  1029. case SECURITY_PROTOCOL_IN:
  1030. case SECURITY_PROTOCOL_OUT:
  1031. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1032. break;
  1033. case EXTENDED_COPY:
  1034. case READ_ATTRIBUTE:
  1035. case RECEIVE_COPY_RESULTS:
  1036. case WRITE_ATTRIBUTE:
  1037. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1038. (cdb[12] << 8) | cdb[13];
  1039. break;
  1040. case RECEIVE_DIAGNOSTIC:
  1041. case SEND_DIAGNOSTIC:
  1042. *size = (cdb[3] << 8) | cdb[4];
  1043. break;
  1044. case WRITE_BUFFER:
  1045. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1046. break;
  1047. case REPORT_LUNS:
  1048. cmd->execute_cmd = spc_emulate_report_luns;
  1049. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1050. /*
  1051. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  1052. * See spc4r17 section 5.3
  1053. */
  1054. cmd->sam_task_attr = MSG_HEAD_TAG;
  1055. break;
  1056. case TEST_UNIT_READY:
  1057. cmd->execute_cmd = spc_emulate_testunitready;
  1058. *size = 0;
  1059. break;
  1060. case MAINTENANCE_IN:
  1061. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1062. /*
  1063. * MAINTENANCE_IN from SCC-2
  1064. * Check for emulated MI_REPORT_TARGET_PGS
  1065. */
  1066. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1067. cmd->execute_cmd =
  1068. target_emulate_report_target_port_groups;
  1069. }
  1070. *size = get_unaligned_be32(&cdb[6]);
  1071. } else {
  1072. /*
  1073. * GPCMD_SEND_KEY from multi media commands
  1074. */
  1075. *size = get_unaligned_be16(&cdb[8]);
  1076. }
  1077. break;
  1078. case MAINTENANCE_OUT:
  1079. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1080. /*
  1081. * MAINTENANCE_OUT from SCC-2
  1082. * Check for emulated MO_SET_TARGET_PGS.
  1083. */
  1084. if (cdb[1] == MO_SET_TARGET_PGS) {
  1085. cmd->execute_cmd =
  1086. target_emulate_set_target_port_groups;
  1087. }
  1088. *size = get_unaligned_be32(&cdb[6]);
  1089. } else {
  1090. /*
  1091. * GPCMD_SEND_KEY from multi media commands
  1092. */
  1093. *size = get_unaligned_be16(&cdb[8]);
  1094. }
  1095. break;
  1096. default:
  1097. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1098. " 0x%02x, sending CHECK_CONDITION.\n",
  1099. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1100. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1101. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  1102. return -EINVAL;
  1103. }
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL(spc_parse_cdb);