target_core_spc.c 38 KB

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