target_core_spc.c 35 KB

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