target_core_spc.c 35 KB

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