target_core_spc.c 35 KB

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