target_core_spc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. buf = transport_kmap_data_sg(cmd);
  873. if (!buf)
  874. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  875. if (!pf) {
  876. ret = TCM_INVALID_CDB_FIELD;
  877. goto out;
  878. }
  879. page = buf[off] & 0x3f;
  880. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  881. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  882. if (modesense_handlers[i].page == page &&
  883. modesense_handlers[i].subpage == subpage) {
  884. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  885. length = modesense_handlers[i].emulate(dev, 0, tbuf);
  886. goto check_contents;
  887. }
  888. ret = TCM_UNKNOWN_MODE_PAGE;
  889. goto out;
  890. check_contents:
  891. if (memcmp(buf + off, tbuf, length))
  892. ret = TCM_INVALID_PARAMETER_LIST;
  893. out:
  894. transport_kunmap_data_sg(cmd);
  895. if (!ret)
  896. target_complete_cmd(cmd, GOOD);
  897. return ret;
  898. }
  899. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  900. {
  901. unsigned char *cdb = cmd->t_task_cdb;
  902. unsigned char *rbuf;
  903. u8 ua_asc = 0, ua_ascq = 0;
  904. unsigned char buf[SE_SENSE_BUF];
  905. memset(buf, 0, SE_SENSE_BUF);
  906. if (cdb[1] & 0x01) {
  907. pr_err("REQUEST_SENSE description emulation not"
  908. " supported\n");
  909. return TCM_INVALID_CDB_FIELD;
  910. }
  911. rbuf = transport_kmap_data_sg(cmd);
  912. if (!rbuf)
  913. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  914. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  915. /*
  916. * CURRENT ERROR, UNIT ATTENTION
  917. */
  918. buf[0] = 0x70;
  919. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  920. /*
  921. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  922. */
  923. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  924. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  925. buf[7] = 0x0A;
  926. } else {
  927. /*
  928. * CURRENT ERROR, NO SENSE
  929. */
  930. buf[0] = 0x70;
  931. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  932. /*
  933. * NO ADDITIONAL SENSE INFORMATION
  934. */
  935. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  936. buf[7] = 0x0A;
  937. }
  938. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  939. transport_kunmap_data_sg(cmd);
  940. target_complete_cmd(cmd, GOOD);
  941. return 0;
  942. }
  943. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  944. {
  945. struct se_dev_entry *deve;
  946. struct se_session *sess = cmd->se_sess;
  947. unsigned char *buf;
  948. u32 lun_count = 0, offset = 8, i;
  949. if (cmd->data_length < 16) {
  950. pr_warn("REPORT LUNS allocation length %u too small\n",
  951. cmd->data_length);
  952. return TCM_INVALID_CDB_FIELD;
  953. }
  954. buf = transport_kmap_data_sg(cmd);
  955. if (!buf)
  956. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  957. /*
  958. * If no struct se_session pointer is present, this struct se_cmd is
  959. * coming via a target_core_mod PASSTHROUGH op, and not through
  960. * a $FABRIC_MOD. In that case, report LUN=0 only.
  961. */
  962. if (!sess) {
  963. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  964. lun_count = 1;
  965. goto done;
  966. }
  967. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  968. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  969. deve = sess->se_node_acl->device_list[i];
  970. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  971. continue;
  972. /*
  973. * We determine the correct LUN LIST LENGTH even once we
  974. * have reached the initial allocation length.
  975. * See SPC2-R20 7.19.
  976. */
  977. lun_count++;
  978. if ((offset + 8) > cmd->data_length)
  979. continue;
  980. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  981. offset += 8;
  982. }
  983. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  984. /*
  985. * See SPC3 r07, page 159.
  986. */
  987. done:
  988. lun_count *= 8;
  989. buf[0] = ((lun_count >> 24) & 0xff);
  990. buf[1] = ((lun_count >> 16) & 0xff);
  991. buf[2] = ((lun_count >> 8) & 0xff);
  992. buf[3] = (lun_count & 0xff);
  993. transport_kunmap_data_sg(cmd);
  994. target_complete_cmd(cmd, GOOD);
  995. return 0;
  996. }
  997. EXPORT_SYMBOL(spc_emulate_report_luns);
  998. static sense_reason_t
  999. spc_emulate_testunitready(struct se_cmd *cmd)
  1000. {
  1001. target_complete_cmd(cmd, GOOD);
  1002. return 0;
  1003. }
  1004. sense_reason_t
  1005. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  1006. {
  1007. struct se_device *dev = cmd->se_dev;
  1008. unsigned char *cdb = cmd->t_task_cdb;
  1009. switch (cdb[0]) {
  1010. case MODE_SELECT:
  1011. *size = cdb[4];
  1012. cmd->execute_cmd = spc_emulate_modeselect;
  1013. break;
  1014. case MODE_SELECT_10:
  1015. *size = (cdb[7] << 8) + cdb[8];
  1016. cmd->execute_cmd = spc_emulate_modeselect;
  1017. break;
  1018. case MODE_SENSE:
  1019. *size = cdb[4];
  1020. cmd->execute_cmd = spc_emulate_modesense;
  1021. break;
  1022. case MODE_SENSE_10:
  1023. *size = (cdb[7] << 8) + cdb[8];
  1024. cmd->execute_cmd = spc_emulate_modesense;
  1025. break;
  1026. case LOG_SELECT:
  1027. case LOG_SENSE:
  1028. *size = (cdb[7] << 8) + cdb[8];
  1029. break;
  1030. case PERSISTENT_RESERVE_IN:
  1031. *size = (cdb[7] << 8) + cdb[8];
  1032. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1033. break;
  1034. case PERSISTENT_RESERVE_OUT:
  1035. *size = (cdb[7] << 8) + cdb[8];
  1036. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1037. break;
  1038. case RELEASE:
  1039. case RELEASE_10:
  1040. if (cdb[0] == RELEASE_10)
  1041. *size = (cdb[7] << 8) | cdb[8];
  1042. else
  1043. *size = cmd->data_length;
  1044. cmd->execute_cmd = target_scsi2_reservation_release;
  1045. break;
  1046. case RESERVE:
  1047. case RESERVE_10:
  1048. /*
  1049. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1050. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1051. */
  1052. if (cdb[0] == RESERVE_10)
  1053. *size = (cdb[7] << 8) | cdb[8];
  1054. else
  1055. *size = cmd->data_length;
  1056. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1057. break;
  1058. case REQUEST_SENSE:
  1059. *size = cdb[4];
  1060. cmd->execute_cmd = spc_emulate_request_sense;
  1061. break;
  1062. case INQUIRY:
  1063. *size = (cdb[3] << 8) + cdb[4];
  1064. /*
  1065. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  1066. * See spc4r17 section 5.3
  1067. */
  1068. cmd->sam_task_attr = MSG_HEAD_TAG;
  1069. cmd->execute_cmd = spc_emulate_inquiry;
  1070. break;
  1071. case SECURITY_PROTOCOL_IN:
  1072. case SECURITY_PROTOCOL_OUT:
  1073. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1074. break;
  1075. case EXTENDED_COPY:
  1076. case READ_ATTRIBUTE:
  1077. case RECEIVE_COPY_RESULTS:
  1078. case WRITE_ATTRIBUTE:
  1079. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1080. (cdb[12] << 8) | cdb[13];
  1081. break;
  1082. case RECEIVE_DIAGNOSTIC:
  1083. case SEND_DIAGNOSTIC:
  1084. *size = (cdb[3] << 8) | cdb[4];
  1085. break;
  1086. case WRITE_BUFFER:
  1087. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1088. break;
  1089. case REPORT_LUNS:
  1090. cmd->execute_cmd = spc_emulate_report_luns;
  1091. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1092. /*
  1093. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  1094. * See spc4r17 section 5.3
  1095. */
  1096. cmd->sam_task_attr = MSG_HEAD_TAG;
  1097. break;
  1098. case TEST_UNIT_READY:
  1099. cmd->execute_cmd = spc_emulate_testunitready;
  1100. *size = 0;
  1101. break;
  1102. case MAINTENANCE_IN:
  1103. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1104. /*
  1105. * MAINTENANCE_IN from SCC-2
  1106. * Check for emulated MI_REPORT_TARGET_PGS
  1107. */
  1108. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1109. cmd->execute_cmd =
  1110. target_emulate_report_target_port_groups;
  1111. }
  1112. *size = get_unaligned_be32(&cdb[6]);
  1113. } else {
  1114. /*
  1115. * GPCMD_SEND_KEY from multi media commands
  1116. */
  1117. *size = get_unaligned_be16(&cdb[8]);
  1118. }
  1119. break;
  1120. case MAINTENANCE_OUT:
  1121. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1122. /*
  1123. * MAINTENANCE_OUT from SCC-2
  1124. * Check for emulated MO_SET_TARGET_PGS.
  1125. */
  1126. if (cdb[1] == MO_SET_TARGET_PGS) {
  1127. cmd->execute_cmd =
  1128. target_emulate_set_target_port_groups;
  1129. }
  1130. *size = get_unaligned_be32(&cdb[6]);
  1131. } else {
  1132. /*
  1133. * GPCMD_SEND_KEY from multi media commands
  1134. */
  1135. *size = get_unaligned_be16(&cdb[8]);
  1136. }
  1137. break;
  1138. default:
  1139. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1140. " 0x%02x, sending CHECK_CONDITION.\n",
  1141. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1142. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1143. }
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL(spc_parse_cdb);