target_core_spc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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 MAXIMUM COMPARE AND WRITE LENGTH
  406. */
  407. if (dev->dev_attrib.emulate_caw)
  408. buf[5] = 0x01;
  409. /*
  410. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  411. */
  412. put_unaligned_be16(1, &buf[6]);
  413. /*
  414. * Set MAXIMUM TRANSFER LENGTH
  415. */
  416. max_sectors = min(dev->dev_attrib.fabric_max_sectors,
  417. dev->dev_attrib.hw_max_sectors);
  418. put_unaligned_be32(max_sectors, &buf[8]);
  419. /*
  420. * Set OPTIMAL TRANSFER LENGTH
  421. */
  422. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  423. /*
  424. * Exit now if we don't support TP.
  425. */
  426. if (!have_tp)
  427. goto max_write_same;
  428. /*
  429. * Set MAXIMUM UNMAP LBA COUNT
  430. */
  431. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  432. /*
  433. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  434. */
  435. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  436. &buf[24]);
  437. /*
  438. * Set OPTIMAL UNMAP GRANULARITY
  439. */
  440. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  441. /*
  442. * UNMAP GRANULARITY ALIGNMENT
  443. */
  444. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  445. &buf[32]);
  446. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  447. buf[32] |= 0x80; /* Set the UGAVALID bit */
  448. /*
  449. * MAXIMUM WRITE SAME LENGTH
  450. */
  451. max_write_same:
  452. put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
  453. return 0;
  454. }
  455. /* Block Device Characteristics VPD page */
  456. static sense_reason_t
  457. spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  458. {
  459. struct se_device *dev = cmd->se_dev;
  460. buf[0] = dev->transport->get_device_type(dev);
  461. buf[3] = 0x3c;
  462. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  463. return 0;
  464. }
  465. /* Thin Provisioning VPD */
  466. static sense_reason_t
  467. spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  468. {
  469. struct se_device *dev = cmd->se_dev;
  470. /*
  471. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  472. *
  473. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  474. * zero, then the page length shall be set to 0004h. If the DP bit
  475. * is set to one, then the page length shall be set to the value
  476. * defined in table 162.
  477. */
  478. buf[0] = dev->transport->get_device_type(dev);
  479. /*
  480. * Set Hardcoded length mentioned above for DP=0
  481. */
  482. put_unaligned_be16(0x0004, &buf[2]);
  483. /*
  484. * The THRESHOLD EXPONENT field indicates the threshold set size in
  485. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  486. * 2(threshold exponent)).
  487. *
  488. * Note that this is currently set to 0x00 as mkp says it will be
  489. * changing again. We can enable this once it has settled in T10
  490. * and is actually used by Linux/SCSI ML code.
  491. */
  492. buf[4] = 0x00;
  493. /*
  494. * A TPU bit set to one indicates that the device server supports
  495. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  496. * that the device server does not support the UNMAP command.
  497. */
  498. if (dev->dev_attrib.emulate_tpu != 0)
  499. buf[5] = 0x80;
  500. /*
  501. * A TPWS bit set to one indicates that the device server supports
  502. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  503. * A TPWS bit set to zero indicates that the device server does not
  504. * support the use of the WRITE SAME (16) command to unmap LBAs.
  505. */
  506. if (dev->dev_attrib.emulate_tpws != 0)
  507. buf[5] |= 0x40;
  508. return 0;
  509. }
  510. static sense_reason_t
  511. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  512. static struct {
  513. uint8_t page;
  514. sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
  515. } evpd_handlers[] = {
  516. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  517. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  518. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  519. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  520. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  521. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  522. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  523. };
  524. /* supported vital product data pages */
  525. static sense_reason_t
  526. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  527. {
  528. int p;
  529. /*
  530. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  531. * Registered Extended LUN WWN has been set via ConfigFS
  532. * during device creation/restart.
  533. */
  534. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  535. buf[3] = ARRAY_SIZE(evpd_handlers);
  536. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  537. buf[p + 4] = evpd_handlers[p].page;
  538. }
  539. return 0;
  540. }
  541. static sense_reason_t
  542. spc_emulate_inquiry(struct se_cmd *cmd)
  543. {
  544. struct se_device *dev = cmd->se_dev;
  545. struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
  546. unsigned char *rbuf;
  547. unsigned char *cdb = cmd->t_task_cdb;
  548. unsigned char buf[SE_INQUIRY_BUF];
  549. sense_reason_t ret;
  550. int p;
  551. memset(buf, 0, SE_INQUIRY_BUF);
  552. if (dev == tpg->tpg_virt_lun0.lun_se_dev)
  553. buf[0] = 0x3f; /* Not connected */
  554. else
  555. buf[0] = dev->transport->get_device_type(dev);
  556. if (!(cdb[1] & 0x1)) {
  557. if (cdb[2]) {
  558. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  559. cdb[2]);
  560. ret = TCM_INVALID_CDB_FIELD;
  561. goto out;
  562. }
  563. ret = spc_emulate_inquiry_std(cmd, buf);
  564. goto out;
  565. }
  566. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  567. if (cdb[2] == evpd_handlers[p].page) {
  568. buf[1] = cdb[2];
  569. ret = evpd_handlers[p].emulate(cmd, buf);
  570. goto out;
  571. }
  572. }
  573. pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
  574. ret = TCM_INVALID_CDB_FIELD;
  575. out:
  576. rbuf = transport_kmap_data_sg(cmd);
  577. if (rbuf) {
  578. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  579. transport_kunmap_data_sg(cmd);
  580. }
  581. if (!ret)
  582. target_complete_cmd(cmd, GOOD);
  583. return ret;
  584. }
  585. static int spc_modesense_rwrecovery(struct se_device *dev, u8 pc, u8 *p)
  586. {
  587. p[0] = 0x01;
  588. p[1] = 0x0a;
  589. /* No changeable values for now */
  590. if (pc == 1)
  591. goto out;
  592. out:
  593. return 12;
  594. }
  595. static int spc_modesense_control(struct se_device *dev, u8 pc, u8 *p)
  596. {
  597. p[0] = 0x0a;
  598. p[1] = 0x0a;
  599. /* No changeable values for now */
  600. if (pc == 1)
  601. goto out;
  602. p[2] = 2;
  603. /*
  604. * From spc4r23, 7.4.7 Control mode page
  605. *
  606. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  607. * restrictions on the algorithm used for reordering commands
  608. * having the SIMPLE task attribute (see SAM-4).
  609. *
  610. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  611. * Code Description
  612. * 0h Restricted reordering
  613. * 1h Unrestricted reordering allowed
  614. * 2h to 7h Reserved
  615. * 8h to Fh Vendor specific
  616. *
  617. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  618. * the device server shall order the processing sequence of commands
  619. * having the SIMPLE task attribute such that data integrity is maintained
  620. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  621. * requests is halted at any time, the final value of all data observable
  622. * on the medium shall be the same as if all the commands had been processed
  623. * with the ORDERED task attribute).
  624. *
  625. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  626. * device server may reorder the processing sequence of commands having the
  627. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  628. * command sequence order shall be explicitly handled by the application client
  629. * through the selection of appropriate ommands and task attributes.
  630. */
  631. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  632. /*
  633. * From spc4r17, section 7.4.6 Control mode Page
  634. *
  635. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  636. *
  637. * 00b: The logical unit shall clear any unit attention condition
  638. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  639. * status and shall not establish a unit attention condition when a com-
  640. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  641. * status.
  642. *
  643. * 10b: The logical unit shall not clear any unit attention condition
  644. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  645. * status and shall not establish a unit attention condition when
  646. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  647. * CONFLICT status.
  648. *
  649. * 11b a The logical unit shall not clear any unit attention condition
  650. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  651. * status and shall establish a unit attention condition for the
  652. * initiator port associated with the I_T nexus on which the BUSY,
  653. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  654. * Depending on the status, the additional sense code shall be set to
  655. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  656. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  657. * command, a unit attention condition shall be established only once
  658. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  659. * to the number of commands completed with one of those status codes.
  660. */
  661. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  662. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  663. /*
  664. * From spc4r17, section 7.4.6 Control mode Page
  665. *
  666. * Task Aborted Status (TAS) bit set to zero.
  667. *
  668. * A task aborted status (TAS) bit set to zero specifies that aborted
  669. * tasks shall be terminated by the device server without any response
  670. * to the application client. A TAS bit set to one specifies that tasks
  671. * aborted by the actions of an I_T nexus other than the I_T nexus on
  672. * which the command was received shall be completed with TASK ABORTED
  673. * status (see SAM-4).
  674. */
  675. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  676. p[8] = 0xff;
  677. p[9] = 0xff;
  678. p[11] = 30;
  679. out:
  680. return 12;
  681. }
  682. static int spc_modesense_caching(struct se_device *dev, u8 pc, u8 *p)
  683. {
  684. p[0] = 0x08;
  685. p[1] = 0x12;
  686. /* No changeable values for now */
  687. if (pc == 1)
  688. goto out;
  689. if (spc_check_dev_wce(dev))
  690. p[2] = 0x04; /* Write Cache Enable */
  691. p[12] = 0x20; /* Disabled Read Ahead */
  692. out:
  693. return 20;
  694. }
  695. static int spc_modesense_informational_exceptions(struct se_device *dev, u8 pc, unsigned char *p)
  696. {
  697. p[0] = 0x1c;
  698. p[1] = 0x0a;
  699. /* No changeable values for now */
  700. if (pc == 1)
  701. goto out;
  702. out:
  703. return 12;
  704. }
  705. static struct {
  706. uint8_t page;
  707. uint8_t subpage;
  708. int (*emulate)(struct se_device *, u8, unsigned char *);
  709. } modesense_handlers[] = {
  710. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  711. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  712. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  713. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  714. };
  715. static void spc_modesense_write_protect(unsigned char *buf, int type)
  716. {
  717. /*
  718. * I believe that the WP bit (bit 7) in the mode header is the same for
  719. * all device types..
  720. */
  721. switch (type) {
  722. case TYPE_DISK:
  723. case TYPE_TAPE:
  724. default:
  725. buf[0] |= 0x80; /* WP bit */
  726. break;
  727. }
  728. }
  729. static void spc_modesense_dpofua(unsigned char *buf, int type)
  730. {
  731. switch (type) {
  732. case TYPE_DISK:
  733. buf[0] |= 0x10; /* DPOFUA bit */
  734. break;
  735. default:
  736. break;
  737. }
  738. }
  739. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  740. {
  741. *buf++ = 8;
  742. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  743. buf += 4;
  744. put_unaligned_be32(block_size, buf);
  745. return 9;
  746. }
  747. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  748. {
  749. if (blocks <= 0xffffffff)
  750. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  751. *buf++ = 1; /* LONGLBA */
  752. buf += 2;
  753. *buf++ = 16;
  754. put_unaligned_be64(blocks, buf);
  755. buf += 12;
  756. put_unaligned_be32(block_size, buf);
  757. return 17;
  758. }
  759. static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
  760. {
  761. struct se_device *dev = cmd->se_dev;
  762. char *cdb = cmd->t_task_cdb;
  763. unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
  764. int type = dev->transport->get_device_type(dev);
  765. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  766. bool dbd = !!(cdb[1] & 0x08);
  767. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  768. u8 pc = cdb[2] >> 6;
  769. u8 page = cdb[2] & 0x3f;
  770. u8 subpage = cdb[3];
  771. int length = 0;
  772. int ret;
  773. int i;
  774. memset(buf, 0, SE_MODE_PAGE_BUF);
  775. /*
  776. * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
  777. * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
  778. */
  779. length = ten ? 3 : 2;
  780. /* DEVICE-SPECIFIC PARAMETER */
  781. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  782. (cmd->se_deve &&
  783. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  784. spc_modesense_write_protect(&buf[length], type);
  785. if ((spc_check_dev_wce(dev)) &&
  786. (dev->dev_attrib.emulate_fua_write > 0))
  787. spc_modesense_dpofua(&buf[length], type);
  788. ++length;
  789. /* BLOCK DESCRIPTOR */
  790. /*
  791. * For now we only include a block descriptor for disk (SBC)
  792. * devices; other command sets use a slightly different format.
  793. */
  794. if (!dbd && type == TYPE_DISK) {
  795. u64 blocks = dev->transport->get_blocks(dev);
  796. u32 block_size = dev->dev_attrib.block_size;
  797. if (ten) {
  798. if (llba) {
  799. length += spc_modesense_long_blockdesc(&buf[length],
  800. blocks, block_size);
  801. } else {
  802. length += 3;
  803. length += spc_modesense_blockdesc(&buf[length],
  804. blocks, block_size);
  805. }
  806. } else {
  807. length += spc_modesense_blockdesc(&buf[length], blocks,
  808. block_size);
  809. }
  810. } else {
  811. if (ten)
  812. length += 4;
  813. else
  814. length += 1;
  815. }
  816. if (page == 0x3f) {
  817. if (subpage != 0x00 && subpage != 0xff) {
  818. pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
  819. return TCM_INVALID_CDB_FIELD;
  820. }
  821. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  822. /*
  823. * Tricky way to say all subpage 00h for
  824. * subpage==0, all subpages for subpage==0xff
  825. * (and we just checked above that those are
  826. * the only two possibilities).
  827. */
  828. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  829. ret = modesense_handlers[i].emulate(dev, pc, &buf[length]);
  830. if (!ten && length + ret >= 255)
  831. break;
  832. length += ret;
  833. }
  834. }
  835. goto set_length;
  836. }
  837. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  838. if (modesense_handlers[i].page == page &&
  839. modesense_handlers[i].subpage == subpage) {
  840. length += modesense_handlers[i].emulate(dev, pc, &buf[length]);
  841. goto set_length;
  842. }
  843. /*
  844. * We don't intend to implement:
  845. * - obsolete page 03h "format parameters" (checked by Solaris)
  846. */
  847. if (page != 0x03)
  848. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  849. page, subpage);
  850. return TCM_UNKNOWN_MODE_PAGE;
  851. set_length:
  852. if (ten)
  853. put_unaligned_be16(length - 2, buf);
  854. else
  855. buf[0] = length - 1;
  856. rbuf = transport_kmap_data_sg(cmd);
  857. if (rbuf) {
  858. memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
  859. transport_kunmap_data_sg(cmd);
  860. }
  861. target_complete_cmd(cmd, GOOD);
  862. return 0;
  863. }
  864. static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
  865. {
  866. struct se_device *dev = cmd->se_dev;
  867. char *cdb = cmd->t_task_cdb;
  868. bool ten = cdb[0] == MODE_SELECT_10;
  869. int off = ten ? 8 : 4;
  870. bool pf = !!(cdb[1] & 0x10);
  871. u8 page, subpage;
  872. unsigned char *buf;
  873. unsigned char tbuf[SE_MODE_PAGE_BUF];
  874. int length;
  875. int ret = 0;
  876. int i;
  877. if (!cmd->data_length) {
  878. target_complete_cmd(cmd, GOOD);
  879. return 0;
  880. }
  881. if (cmd->data_length < off + 2)
  882. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  883. buf = transport_kmap_data_sg(cmd);
  884. if (!buf)
  885. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  886. if (!pf) {
  887. ret = TCM_INVALID_CDB_FIELD;
  888. goto out;
  889. }
  890. page = buf[off] & 0x3f;
  891. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  892. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  893. if (modesense_handlers[i].page == page &&
  894. modesense_handlers[i].subpage == subpage) {
  895. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  896. length = modesense_handlers[i].emulate(dev, 0, tbuf);
  897. goto check_contents;
  898. }
  899. ret = TCM_UNKNOWN_MODE_PAGE;
  900. goto out;
  901. check_contents:
  902. if (cmd->data_length < off + length) {
  903. ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
  904. goto out;
  905. }
  906. if (memcmp(buf + off, tbuf, length))
  907. ret = TCM_INVALID_PARAMETER_LIST;
  908. out:
  909. transport_kunmap_data_sg(cmd);
  910. if (!ret)
  911. target_complete_cmd(cmd, GOOD);
  912. return ret;
  913. }
  914. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  915. {
  916. unsigned char *cdb = cmd->t_task_cdb;
  917. unsigned char *rbuf;
  918. u8 ua_asc = 0, ua_ascq = 0;
  919. unsigned char buf[SE_SENSE_BUF];
  920. memset(buf, 0, SE_SENSE_BUF);
  921. if (cdb[1] & 0x01) {
  922. pr_err("REQUEST_SENSE description emulation not"
  923. " supported\n");
  924. return TCM_INVALID_CDB_FIELD;
  925. }
  926. rbuf = transport_kmap_data_sg(cmd);
  927. if (!rbuf)
  928. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  929. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  930. /*
  931. * CURRENT ERROR, UNIT ATTENTION
  932. */
  933. buf[0] = 0x70;
  934. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  935. /*
  936. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  937. */
  938. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  939. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  940. buf[7] = 0x0A;
  941. } else {
  942. /*
  943. * CURRENT ERROR, NO SENSE
  944. */
  945. buf[0] = 0x70;
  946. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  947. /*
  948. * NO ADDITIONAL SENSE INFORMATION
  949. */
  950. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  951. buf[7] = 0x0A;
  952. }
  953. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  954. transport_kunmap_data_sg(cmd);
  955. target_complete_cmd(cmd, GOOD);
  956. return 0;
  957. }
  958. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  959. {
  960. struct se_dev_entry *deve;
  961. struct se_session *sess = cmd->se_sess;
  962. unsigned char *buf;
  963. u32 lun_count = 0, offset = 8, i;
  964. if (cmd->data_length < 16) {
  965. pr_warn("REPORT LUNS allocation length %u too small\n",
  966. cmd->data_length);
  967. return TCM_INVALID_CDB_FIELD;
  968. }
  969. buf = transport_kmap_data_sg(cmd);
  970. if (!buf)
  971. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  972. /*
  973. * If no struct se_session pointer is present, this struct se_cmd is
  974. * coming via a target_core_mod PASSTHROUGH op, and not through
  975. * a $FABRIC_MOD. In that case, report LUN=0 only.
  976. */
  977. if (!sess) {
  978. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  979. lun_count = 1;
  980. goto done;
  981. }
  982. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  983. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  984. deve = sess->se_node_acl->device_list[i];
  985. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  986. continue;
  987. /*
  988. * We determine the correct LUN LIST LENGTH even once we
  989. * have reached the initial allocation length.
  990. * See SPC2-R20 7.19.
  991. */
  992. lun_count++;
  993. if ((offset + 8) > cmd->data_length)
  994. continue;
  995. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  996. offset += 8;
  997. }
  998. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  999. /*
  1000. * See SPC3 r07, page 159.
  1001. */
  1002. done:
  1003. lun_count *= 8;
  1004. buf[0] = ((lun_count >> 24) & 0xff);
  1005. buf[1] = ((lun_count >> 16) & 0xff);
  1006. buf[2] = ((lun_count >> 8) & 0xff);
  1007. buf[3] = (lun_count & 0xff);
  1008. transport_kunmap_data_sg(cmd);
  1009. target_complete_cmd(cmd, GOOD);
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(spc_emulate_report_luns);
  1013. static sense_reason_t
  1014. spc_emulate_testunitready(struct se_cmd *cmd)
  1015. {
  1016. target_complete_cmd(cmd, GOOD);
  1017. return 0;
  1018. }
  1019. sense_reason_t
  1020. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  1021. {
  1022. struct se_device *dev = cmd->se_dev;
  1023. unsigned char *cdb = cmd->t_task_cdb;
  1024. switch (cdb[0]) {
  1025. case MODE_SELECT:
  1026. *size = cdb[4];
  1027. cmd->execute_cmd = spc_emulate_modeselect;
  1028. break;
  1029. case MODE_SELECT_10:
  1030. *size = (cdb[7] << 8) + cdb[8];
  1031. cmd->execute_cmd = spc_emulate_modeselect;
  1032. break;
  1033. case MODE_SENSE:
  1034. *size = cdb[4];
  1035. cmd->execute_cmd = spc_emulate_modesense;
  1036. break;
  1037. case MODE_SENSE_10:
  1038. *size = (cdb[7] << 8) + cdb[8];
  1039. cmd->execute_cmd = spc_emulate_modesense;
  1040. break;
  1041. case LOG_SELECT:
  1042. case LOG_SENSE:
  1043. *size = (cdb[7] << 8) + cdb[8];
  1044. break;
  1045. case PERSISTENT_RESERVE_IN:
  1046. *size = (cdb[7] << 8) + cdb[8];
  1047. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1048. break;
  1049. case PERSISTENT_RESERVE_OUT:
  1050. *size = (cdb[7] << 8) + cdb[8];
  1051. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1052. break;
  1053. case RELEASE:
  1054. case RELEASE_10:
  1055. if (cdb[0] == RELEASE_10)
  1056. *size = (cdb[7] << 8) | cdb[8];
  1057. else
  1058. *size = cmd->data_length;
  1059. cmd->execute_cmd = target_scsi2_reservation_release;
  1060. break;
  1061. case RESERVE:
  1062. case RESERVE_10:
  1063. /*
  1064. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1065. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1066. */
  1067. if (cdb[0] == RESERVE_10)
  1068. *size = (cdb[7] << 8) | cdb[8];
  1069. else
  1070. *size = cmd->data_length;
  1071. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1072. break;
  1073. case REQUEST_SENSE:
  1074. *size = cdb[4];
  1075. cmd->execute_cmd = spc_emulate_request_sense;
  1076. break;
  1077. case INQUIRY:
  1078. *size = (cdb[3] << 8) + cdb[4];
  1079. /*
  1080. * Do implict HEAD_OF_QUEUE processing for INQUIRY.
  1081. * See spc4r17 section 5.3
  1082. */
  1083. cmd->sam_task_attr = MSG_HEAD_TAG;
  1084. cmd->execute_cmd = spc_emulate_inquiry;
  1085. break;
  1086. case SECURITY_PROTOCOL_IN:
  1087. case SECURITY_PROTOCOL_OUT:
  1088. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1089. break;
  1090. case EXTENDED_COPY:
  1091. case READ_ATTRIBUTE:
  1092. case RECEIVE_COPY_RESULTS:
  1093. case WRITE_ATTRIBUTE:
  1094. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1095. (cdb[12] << 8) | cdb[13];
  1096. break;
  1097. case RECEIVE_DIAGNOSTIC:
  1098. case SEND_DIAGNOSTIC:
  1099. *size = (cdb[3] << 8) | cdb[4];
  1100. break;
  1101. case WRITE_BUFFER:
  1102. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1103. break;
  1104. case REPORT_LUNS:
  1105. cmd->execute_cmd = spc_emulate_report_luns;
  1106. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1107. /*
  1108. * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
  1109. * See spc4r17 section 5.3
  1110. */
  1111. cmd->sam_task_attr = MSG_HEAD_TAG;
  1112. break;
  1113. case TEST_UNIT_READY:
  1114. cmd->execute_cmd = spc_emulate_testunitready;
  1115. *size = 0;
  1116. break;
  1117. case MAINTENANCE_IN:
  1118. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1119. /*
  1120. * MAINTENANCE_IN from SCC-2
  1121. * Check for emulated MI_REPORT_TARGET_PGS
  1122. */
  1123. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1124. cmd->execute_cmd =
  1125. target_emulate_report_target_port_groups;
  1126. }
  1127. *size = get_unaligned_be32(&cdb[6]);
  1128. } else {
  1129. /*
  1130. * GPCMD_SEND_KEY from multi media commands
  1131. */
  1132. *size = get_unaligned_be16(&cdb[8]);
  1133. }
  1134. break;
  1135. case MAINTENANCE_OUT:
  1136. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1137. /*
  1138. * MAINTENANCE_OUT from SCC-2
  1139. * Check for emulated MO_SET_TARGET_PGS.
  1140. */
  1141. if (cdb[1] == MO_SET_TARGET_PGS) {
  1142. cmd->execute_cmd =
  1143. target_emulate_set_target_port_groups;
  1144. }
  1145. *size = get_unaligned_be32(&cdb[6]);
  1146. } else {
  1147. /*
  1148. * GPCMD_SEND_KEY from multi media commands
  1149. */
  1150. *size = get_unaligned_be16(&cdb[8]);
  1151. }
  1152. break;
  1153. default:
  1154. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1155. " 0x%02x, sending CHECK_CONDITION.\n",
  1156. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1157. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1158. }
  1159. return 0;
  1160. }
  1161. EXPORT_SYMBOL(spc_parse_cdb);