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