target_core_spc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
  260. buf[off++] = (lun->lun_rtpi & 0xff);
  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. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  288. buf[off++] = (tg_pt_gp_id & 0xff);
  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. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  313. buf[off++] = (lu_gp_id & 0xff);
  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. buf[2] = ((len >> 8) & 0xff);
  397. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(spc_emulate_evpd_83);
  401. /* Extended INQUIRY Data VPD Page */
  402. static sense_reason_t
  403. spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  404. {
  405. struct se_device *dev = cmd->se_dev;
  406. struct se_session *sess = cmd->se_sess;
  407. buf[3] = 0x3c;
  408. /*
  409. * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK
  410. * only for TYPE3 protection.
  411. */
  412. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  413. if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT ||
  414. cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE1_PROT)
  415. buf[4] = 0x5;
  416. else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT ||
  417. cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE3_PROT)
  418. buf[4] = 0x4;
  419. }
  420. /* logical unit supports type 1 and type 3 protection */
  421. if ((dev->transport->get_device_type(dev) == TYPE_DISK) &&
  422. (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) &&
  423. (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type)) {
  424. buf[4] |= (0x3 << 3);
  425. }
  426. /* Set HEADSUP, ORDSUP, SIMPSUP */
  427. buf[5] = 0x07;
  428. /* If WriteCache emulation is enabled, set V_SUP */
  429. if (target_check_wce(dev))
  430. buf[6] = 0x01;
  431. /* If an LBA map is present set R_SUP */
  432. spin_lock(&cmd->se_dev->t10_alua.lba_map_lock);
  433. if (!list_empty(&dev->t10_alua.lba_map_list))
  434. buf[8] = 0x10;
  435. spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock);
  436. return 0;
  437. }
  438. /* Block Limits VPD page */
  439. static sense_reason_t
  440. spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  441. {
  442. struct se_device *dev = cmd->se_dev;
  443. u32 mtl = 0;
  444. int have_tp = 0, opt, min;
  445. /*
  446. * Following spc3r22 section 6.5.3 Block Limits VPD page, when
  447. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  448. * different page length for Thin Provisioning.
  449. */
  450. if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
  451. have_tp = 1;
  452. buf[0] = dev->transport->get_device_type(dev);
  453. buf[3] = have_tp ? 0x3c : 0x10;
  454. /* Set WSNZ to 1 */
  455. buf[4] = 0x01;
  456. /*
  457. * Set MAXIMUM COMPARE AND WRITE LENGTH
  458. */
  459. if (dev->dev_attrib.emulate_caw)
  460. buf[5] = 0x01;
  461. /*
  462. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  463. */
  464. if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev)))
  465. put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]);
  466. else
  467. put_unaligned_be16(1, &buf[6]);
  468. /*
  469. * Set MAXIMUM TRANSFER LENGTH
  470. *
  471. * XXX: Currently assumes single PAGE_SIZE per scatterlist for fabrics
  472. * enforcing maximum HW scatter-gather-list entry limit
  473. */
  474. if (cmd->se_tfo->max_data_sg_nents) {
  475. mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE) /
  476. dev->dev_attrib.block_size;
  477. }
  478. put_unaligned_be32(min_not_zero(mtl, dev->dev_attrib.hw_max_sectors), &buf[8]);
  479. /*
  480. * Set OPTIMAL TRANSFER LENGTH
  481. */
  482. if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev)))
  483. put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]);
  484. else
  485. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  486. /*
  487. * Exit now if we don't support TP.
  488. */
  489. if (!have_tp)
  490. goto max_write_same;
  491. /*
  492. * Set MAXIMUM UNMAP LBA COUNT
  493. */
  494. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  495. /*
  496. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  497. */
  498. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  499. &buf[24]);
  500. /*
  501. * Set OPTIMAL UNMAP GRANULARITY
  502. */
  503. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  504. /*
  505. * UNMAP GRANULARITY ALIGNMENT
  506. */
  507. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  508. &buf[32]);
  509. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  510. buf[32] |= 0x80; /* Set the UGAVALID bit */
  511. /*
  512. * MAXIMUM WRITE SAME LENGTH
  513. */
  514. max_write_same:
  515. put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
  516. return 0;
  517. }
  518. /* Block Device Characteristics VPD page */
  519. static sense_reason_t
  520. spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  521. {
  522. struct se_device *dev = cmd->se_dev;
  523. buf[0] = dev->transport->get_device_type(dev);
  524. buf[3] = 0x3c;
  525. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  526. return 0;
  527. }
  528. /* Thin Provisioning VPD */
  529. static sense_reason_t
  530. spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  531. {
  532. struct se_device *dev = cmd->se_dev;
  533. /*
  534. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  535. *
  536. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  537. * zero, then the page length shall be set to 0004h. If the DP bit
  538. * is set to one, then the page length shall be set to the value
  539. * defined in table 162.
  540. */
  541. buf[0] = dev->transport->get_device_type(dev);
  542. /*
  543. * Set Hardcoded length mentioned above for DP=0
  544. */
  545. put_unaligned_be16(0x0004, &buf[2]);
  546. /*
  547. * The THRESHOLD EXPONENT field indicates the threshold set size in
  548. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  549. * 2(threshold exponent)).
  550. *
  551. * Note that this is currently set to 0x00 as mkp says it will be
  552. * changing again. We can enable this once it has settled in T10
  553. * and is actually used by Linux/SCSI ML code.
  554. */
  555. buf[4] = 0x00;
  556. /*
  557. * A TPU bit set to one indicates that the device server supports
  558. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  559. * that the device server does not support the UNMAP command.
  560. */
  561. if (dev->dev_attrib.emulate_tpu != 0)
  562. buf[5] = 0x80;
  563. /*
  564. * A TPWS bit set to one indicates that the device server supports
  565. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  566. * A TPWS bit set to zero indicates that the device server does not
  567. * support the use of the WRITE SAME (16) command to unmap LBAs.
  568. */
  569. if (dev->dev_attrib.emulate_tpws != 0)
  570. buf[5] |= 0x40 | 0x20;
  571. return 0;
  572. }
  573. /* Referrals VPD page */
  574. static sense_reason_t
  575. spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf)
  576. {
  577. struct se_device *dev = cmd->se_dev;
  578. buf[0] = dev->transport->get_device_type(dev);
  579. buf[3] = 0x0c;
  580. put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]);
  581. put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]);
  582. return 0;
  583. }
  584. static sense_reason_t
  585. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  586. static struct {
  587. uint8_t page;
  588. sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
  589. } evpd_handlers[] = {
  590. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  591. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  592. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  593. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  594. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  595. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  596. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  597. { .page = 0xb3, .emulate = spc_emulate_evpd_b3 },
  598. };
  599. /* supported vital product data pages */
  600. static sense_reason_t
  601. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  602. {
  603. int p;
  604. /*
  605. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  606. * Registered Extended LUN WWN has been set via ConfigFS
  607. * during device creation/restart.
  608. */
  609. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  610. buf[3] = ARRAY_SIZE(evpd_handlers);
  611. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  612. buf[p + 4] = evpd_handlers[p].page;
  613. }
  614. return 0;
  615. }
  616. static sense_reason_t
  617. spc_emulate_inquiry(struct se_cmd *cmd)
  618. {
  619. struct se_device *dev = cmd->se_dev;
  620. struct se_portal_group *tpg = cmd->se_lun->lun_tpg;
  621. unsigned char *rbuf;
  622. unsigned char *cdb = cmd->t_task_cdb;
  623. unsigned char *buf;
  624. sense_reason_t ret;
  625. int p;
  626. int len = 0;
  627. buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
  628. if (!buf) {
  629. pr_err("Unable to allocate response buffer for INQUIRY\n");
  630. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  631. }
  632. if (dev == rcu_access_pointer(tpg->tpg_virt_lun0->lun_se_dev))
  633. buf[0] = 0x3f; /* Not connected */
  634. else
  635. buf[0] = dev->transport->get_device_type(dev);
  636. if (!(cdb[1] & 0x1)) {
  637. if (cdb[2]) {
  638. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  639. cdb[2]);
  640. ret = TCM_INVALID_CDB_FIELD;
  641. goto out;
  642. }
  643. ret = spc_emulate_inquiry_std(cmd, buf);
  644. len = buf[4] + 5;
  645. goto out;
  646. }
  647. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  648. if (cdb[2] == evpd_handlers[p].page) {
  649. buf[1] = cdb[2];
  650. ret = evpd_handlers[p].emulate(cmd, buf);
  651. len = get_unaligned_be16(&buf[2]) + 4;
  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_with_length(cmd, GOOD, len);
  666. return ret;
  667. }
  668. static int spc_modesense_rwrecovery(struct se_cmd *cmd, 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_cmd *cmd, u8 pc, u8 *p)
  679. {
  680. struct se_device *dev = cmd->se_dev;
  681. struct se_session *sess = cmd->se_sess;
  682. p[0] = 0x0a;
  683. p[1] = 0x0a;
  684. /* No changeable values for now */
  685. if (pc == 1)
  686. goto out;
  687. /* GLTSD: No implicit save of log parameters */
  688. p[2] = (1 << 1);
  689. if (target_sense_desc_format(dev))
  690. /* D_SENSE: Descriptor format sense data for 64bit sectors */
  691. p[2] |= (1 << 2);
  692. /*
  693. * From spc4r23, 7.4.7 Control mode page
  694. *
  695. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  696. * restrictions on the algorithm used for reordering commands
  697. * having the SIMPLE task attribute (see SAM-4).
  698. *
  699. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  700. * Code Description
  701. * 0h Restricted reordering
  702. * 1h Unrestricted reordering allowed
  703. * 2h to 7h Reserved
  704. * 8h to Fh Vendor specific
  705. *
  706. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  707. * the device server shall order the processing sequence of commands
  708. * having the SIMPLE task attribute such that data integrity is maintained
  709. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  710. * requests is halted at any time, the final value of all data observable
  711. * on the medium shall be the same as if all the commands had been processed
  712. * with the ORDERED task attribute).
  713. *
  714. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  715. * device server may reorder the processing sequence of commands having the
  716. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  717. * command sequence order shall be explicitly handled by the application client
  718. * through the selection of appropriate ommands and task attributes.
  719. */
  720. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  721. /*
  722. * From spc4r17, section 7.4.6 Control mode Page
  723. *
  724. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  725. *
  726. * 00b: The logical unit shall 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 a com-
  729. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  730. * status.
  731. *
  732. * 10b: 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 not establish a unit attention condition when
  735. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  736. * CONFLICT status.
  737. *
  738. * 11b a The logical unit shall not clear any unit attention condition
  739. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  740. * status and shall establish a unit attention condition for the
  741. * initiator port associated with the I_T nexus on which the BUSY,
  742. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  743. * Depending on the status, the additional sense code shall be set to
  744. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  745. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  746. * command, a unit attention condition shall be established only once
  747. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  748. * to the number of commands completed with one of those status codes.
  749. */
  750. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  751. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  752. /*
  753. * From spc4r17, section 7.4.6 Control mode Page
  754. *
  755. * Task Aborted Status (TAS) bit set to zero.
  756. *
  757. * A task aborted status (TAS) bit set to zero specifies that aborted
  758. * tasks shall be terminated by the device server without any response
  759. * to the application client. A TAS bit set to one specifies that tasks
  760. * aborted by the actions of an I_T nexus other than the I_T nexus on
  761. * which the command was received shall be completed with TASK ABORTED
  762. * status (see SAM-4).
  763. */
  764. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  765. /*
  766. * From spc4r30, section 7.5.7 Control mode page
  767. *
  768. * Application Tag Owner (ATO) bit set to one.
  769. *
  770. * If the ATO bit is set to one the device server shall not modify the
  771. * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
  772. * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
  773. * TAG field.
  774. */
  775. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  776. if (dev->dev_attrib.pi_prot_type || sess->sess_prot_type)
  777. p[5] |= 0x80;
  778. }
  779. p[8] = 0xff;
  780. p[9] = 0xff;
  781. p[11] = 30;
  782. out:
  783. return 12;
  784. }
  785. static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p)
  786. {
  787. struct se_device *dev = cmd->se_dev;
  788. p[0] = 0x08;
  789. p[1] = 0x12;
  790. /* No changeable values for now */
  791. if (pc == 1)
  792. goto out;
  793. if (target_check_wce(dev))
  794. p[2] = 0x04; /* Write Cache Enable */
  795. p[12] = 0x20; /* Disabled Read Ahead */
  796. out:
  797. return 20;
  798. }
  799. static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p)
  800. {
  801. p[0] = 0x1c;
  802. p[1] = 0x0a;
  803. /* No changeable values for now */
  804. if (pc == 1)
  805. goto out;
  806. out:
  807. return 12;
  808. }
  809. static struct {
  810. uint8_t page;
  811. uint8_t subpage;
  812. int (*emulate)(struct se_cmd *, u8, unsigned char *);
  813. } modesense_handlers[] = {
  814. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  815. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  816. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  817. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  818. };
  819. static void spc_modesense_write_protect(unsigned char *buf, int type)
  820. {
  821. /*
  822. * I believe that the WP bit (bit 7) in the mode header is the same for
  823. * all device types..
  824. */
  825. switch (type) {
  826. case TYPE_DISK:
  827. case TYPE_TAPE:
  828. default:
  829. buf[0] |= 0x80; /* WP bit */
  830. break;
  831. }
  832. }
  833. static void spc_modesense_dpofua(unsigned char *buf, int type)
  834. {
  835. switch (type) {
  836. case TYPE_DISK:
  837. buf[0] |= 0x10; /* DPOFUA bit */
  838. break;
  839. default:
  840. break;
  841. }
  842. }
  843. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  844. {
  845. *buf++ = 8;
  846. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  847. buf += 4;
  848. put_unaligned_be32(block_size, buf);
  849. return 9;
  850. }
  851. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  852. {
  853. if (blocks <= 0xffffffff)
  854. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  855. *buf++ = 1; /* LONGLBA */
  856. buf += 2;
  857. *buf++ = 16;
  858. put_unaligned_be64(blocks, buf);
  859. buf += 12;
  860. put_unaligned_be32(block_size, buf);
  861. return 17;
  862. }
  863. static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
  864. {
  865. struct se_device *dev = cmd->se_dev;
  866. char *cdb = cmd->t_task_cdb;
  867. unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
  868. int type = dev->transport->get_device_type(dev);
  869. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  870. bool dbd = !!(cdb[1] & 0x08);
  871. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  872. u8 pc = cdb[2] >> 6;
  873. u8 page = cdb[2] & 0x3f;
  874. u8 subpage = cdb[3];
  875. int length = 0;
  876. int ret;
  877. int i;
  878. bool read_only = target_lun_is_rdonly(cmd);;
  879. memset(buf, 0, SE_MODE_PAGE_BUF);
  880. /*
  881. * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
  882. * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
  883. */
  884. length = ten ? 3 : 2;
  885. /* DEVICE-SPECIFIC PARAMETER */
  886. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) || read_only)
  887. spc_modesense_write_protect(&buf[length], type);
  888. /*
  889. * SBC only allows us to enable FUA and DPO together. Fortunately
  890. * DPO is explicitly specified as a hint, so a noop is a perfectly
  891. * valid implementation.
  892. */
  893. if (target_check_fua(dev))
  894. spc_modesense_dpofua(&buf[length], type);
  895. ++length;
  896. /* BLOCK DESCRIPTOR */
  897. /*
  898. * For now we only include a block descriptor for disk (SBC)
  899. * devices; other command sets use a slightly different format.
  900. */
  901. if (!dbd && type == TYPE_DISK) {
  902. u64 blocks = dev->transport->get_blocks(dev);
  903. u32 block_size = dev->dev_attrib.block_size;
  904. if (ten) {
  905. if (llba) {
  906. length += spc_modesense_long_blockdesc(&buf[length],
  907. blocks, block_size);
  908. } else {
  909. length += 3;
  910. length += spc_modesense_blockdesc(&buf[length],
  911. blocks, block_size);
  912. }
  913. } else {
  914. length += spc_modesense_blockdesc(&buf[length], blocks,
  915. block_size);
  916. }
  917. } else {
  918. if (ten)
  919. length += 4;
  920. else
  921. length += 1;
  922. }
  923. if (page == 0x3f) {
  924. if (subpage != 0x00 && subpage != 0xff) {
  925. pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
  926. return TCM_INVALID_CDB_FIELD;
  927. }
  928. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  929. /*
  930. * Tricky way to say all subpage 00h for
  931. * subpage==0, all subpages for subpage==0xff
  932. * (and we just checked above that those are
  933. * the only two possibilities).
  934. */
  935. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  936. ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  937. if (!ten && length + ret >= 255)
  938. break;
  939. length += ret;
  940. }
  941. }
  942. goto set_length;
  943. }
  944. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  945. if (modesense_handlers[i].page == page &&
  946. modesense_handlers[i].subpage == subpage) {
  947. length += modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  948. goto set_length;
  949. }
  950. /*
  951. * We don't intend to implement:
  952. * - obsolete page 03h "format parameters" (checked by Solaris)
  953. */
  954. if (page != 0x03)
  955. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  956. page, subpage);
  957. return TCM_UNKNOWN_MODE_PAGE;
  958. set_length:
  959. if (ten)
  960. put_unaligned_be16(length - 2, buf);
  961. else
  962. buf[0] = length - 1;
  963. rbuf = transport_kmap_data_sg(cmd);
  964. if (rbuf) {
  965. memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
  966. transport_kunmap_data_sg(cmd);
  967. }
  968. target_complete_cmd_with_length(cmd, GOOD, length);
  969. return 0;
  970. }
  971. static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
  972. {
  973. char *cdb = cmd->t_task_cdb;
  974. bool ten = cdb[0] == MODE_SELECT_10;
  975. int off = ten ? 8 : 4;
  976. bool pf = !!(cdb[1] & 0x10);
  977. u8 page, subpage;
  978. unsigned char *buf;
  979. unsigned char tbuf[SE_MODE_PAGE_BUF];
  980. int length;
  981. sense_reason_t ret = 0;
  982. int i;
  983. if (!cmd->data_length) {
  984. target_complete_cmd(cmd, GOOD);
  985. return 0;
  986. }
  987. if (cmd->data_length < off + 2)
  988. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  989. buf = transport_kmap_data_sg(cmd);
  990. if (!buf)
  991. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  992. if (!pf) {
  993. ret = TCM_INVALID_CDB_FIELD;
  994. goto out;
  995. }
  996. page = buf[off] & 0x3f;
  997. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  998. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  999. if (modesense_handlers[i].page == page &&
  1000. modesense_handlers[i].subpage == subpage) {
  1001. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  1002. length = modesense_handlers[i].emulate(cmd, 0, tbuf);
  1003. goto check_contents;
  1004. }
  1005. ret = TCM_UNKNOWN_MODE_PAGE;
  1006. goto out;
  1007. check_contents:
  1008. if (cmd->data_length < off + length) {
  1009. ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
  1010. goto out;
  1011. }
  1012. if (memcmp(buf + off, tbuf, length))
  1013. ret = TCM_INVALID_PARAMETER_LIST;
  1014. out:
  1015. transport_kunmap_data_sg(cmd);
  1016. if (!ret)
  1017. target_complete_cmd(cmd, GOOD);
  1018. return ret;
  1019. }
  1020. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  1021. {
  1022. unsigned char *cdb = cmd->t_task_cdb;
  1023. unsigned char *rbuf;
  1024. u8 ua_asc = 0, ua_ascq = 0;
  1025. unsigned char buf[SE_SENSE_BUF];
  1026. bool desc_format = target_sense_desc_format(cmd->se_dev);
  1027. memset(buf, 0, SE_SENSE_BUF);
  1028. if (cdb[1] & 0x01) {
  1029. pr_err("REQUEST_SENSE description emulation not"
  1030. " supported\n");
  1031. return TCM_INVALID_CDB_FIELD;
  1032. }
  1033. rbuf = transport_kmap_data_sg(cmd);
  1034. if (!rbuf)
  1035. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1036. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))
  1037. scsi_build_sense_buffer(desc_format, buf, UNIT_ATTENTION,
  1038. ua_asc, ua_ascq);
  1039. else
  1040. scsi_build_sense_buffer(desc_format, buf, NO_SENSE, 0x0, 0x0);
  1041. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  1042. transport_kunmap_data_sg(cmd);
  1043. target_complete_cmd(cmd, GOOD);
  1044. return 0;
  1045. }
  1046. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  1047. {
  1048. struct se_dev_entry *deve;
  1049. struct se_session *sess = cmd->se_sess;
  1050. struct se_node_acl *nacl;
  1051. struct scsi_lun slun;
  1052. unsigned char *buf;
  1053. u32 lun_count = 0, offset = 8;
  1054. __be32 len;
  1055. buf = transport_kmap_data_sg(cmd);
  1056. if (cmd->data_length && !buf)
  1057. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1058. /*
  1059. * If no struct se_session pointer is present, this struct se_cmd is
  1060. * coming via a target_core_mod PASSTHROUGH op, and not through
  1061. * a $FABRIC_MOD. In that case, report LUN=0 only.
  1062. */
  1063. if (!sess)
  1064. goto done;
  1065. nacl = sess->se_node_acl;
  1066. rcu_read_lock();
  1067. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  1068. /*
  1069. * We determine the correct LUN LIST LENGTH even once we
  1070. * have reached the initial allocation length.
  1071. * See SPC2-R20 7.19.
  1072. */
  1073. lun_count++;
  1074. if (offset >= cmd->data_length)
  1075. continue;
  1076. int_to_scsilun(deve->mapped_lun, &slun);
  1077. memcpy(buf + offset, &slun,
  1078. min(8u, cmd->data_length - offset));
  1079. offset += 8;
  1080. }
  1081. rcu_read_unlock();
  1082. /*
  1083. * See SPC3 r07, page 159.
  1084. */
  1085. done:
  1086. /*
  1087. * If no LUNs are accessible, report virtual LUN 0.
  1088. */
  1089. if (lun_count == 0) {
  1090. int_to_scsilun(0, &slun);
  1091. if (cmd->data_length > 8)
  1092. memcpy(buf + offset, &slun,
  1093. min(8u, cmd->data_length - offset));
  1094. lun_count = 1;
  1095. }
  1096. if (buf) {
  1097. len = cpu_to_be32(lun_count * 8);
  1098. memcpy(buf, &len, min_t(int, sizeof len, cmd->data_length));
  1099. transport_kunmap_data_sg(cmd);
  1100. }
  1101. target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
  1102. return 0;
  1103. }
  1104. EXPORT_SYMBOL(spc_emulate_report_luns);
  1105. static sense_reason_t
  1106. spc_emulate_testunitready(struct se_cmd *cmd)
  1107. {
  1108. target_complete_cmd(cmd, GOOD);
  1109. return 0;
  1110. }
  1111. sense_reason_t
  1112. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  1113. {
  1114. struct se_device *dev = cmd->se_dev;
  1115. unsigned char *cdb = cmd->t_task_cdb;
  1116. switch (cdb[0]) {
  1117. case MODE_SELECT:
  1118. *size = cdb[4];
  1119. cmd->execute_cmd = spc_emulate_modeselect;
  1120. break;
  1121. case MODE_SELECT_10:
  1122. *size = (cdb[7] << 8) + cdb[8];
  1123. cmd->execute_cmd = spc_emulate_modeselect;
  1124. break;
  1125. case MODE_SENSE:
  1126. *size = cdb[4];
  1127. cmd->execute_cmd = spc_emulate_modesense;
  1128. break;
  1129. case MODE_SENSE_10:
  1130. *size = (cdb[7] << 8) + cdb[8];
  1131. cmd->execute_cmd = spc_emulate_modesense;
  1132. break;
  1133. case LOG_SELECT:
  1134. case LOG_SENSE:
  1135. *size = (cdb[7] << 8) + cdb[8];
  1136. break;
  1137. case PERSISTENT_RESERVE_IN:
  1138. *size = (cdb[7] << 8) + cdb[8];
  1139. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1140. break;
  1141. case PERSISTENT_RESERVE_OUT:
  1142. *size = (cdb[7] << 8) + cdb[8];
  1143. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1144. break;
  1145. case RELEASE:
  1146. case RELEASE_10:
  1147. if (cdb[0] == RELEASE_10)
  1148. *size = (cdb[7] << 8) | cdb[8];
  1149. else
  1150. *size = cmd->data_length;
  1151. cmd->execute_cmd = target_scsi2_reservation_release;
  1152. break;
  1153. case RESERVE:
  1154. case RESERVE_10:
  1155. /*
  1156. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1157. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1158. */
  1159. if (cdb[0] == RESERVE_10)
  1160. *size = (cdb[7] << 8) | cdb[8];
  1161. else
  1162. *size = cmd->data_length;
  1163. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1164. break;
  1165. case REQUEST_SENSE:
  1166. *size = cdb[4];
  1167. cmd->execute_cmd = spc_emulate_request_sense;
  1168. break;
  1169. case INQUIRY:
  1170. *size = (cdb[3] << 8) + cdb[4];
  1171. /*
  1172. * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
  1173. * See spc4r17 section 5.3
  1174. */
  1175. cmd->sam_task_attr = TCM_HEAD_TAG;
  1176. cmd->execute_cmd = spc_emulate_inquiry;
  1177. break;
  1178. case SECURITY_PROTOCOL_IN:
  1179. case SECURITY_PROTOCOL_OUT:
  1180. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1181. break;
  1182. case EXTENDED_COPY:
  1183. *size = get_unaligned_be32(&cdb[10]);
  1184. cmd->execute_cmd = target_do_xcopy;
  1185. break;
  1186. case RECEIVE_COPY_RESULTS:
  1187. *size = get_unaligned_be32(&cdb[10]);
  1188. cmd->execute_cmd = target_do_receive_copy_results;
  1189. break;
  1190. case READ_ATTRIBUTE:
  1191. case WRITE_ATTRIBUTE:
  1192. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1193. (cdb[12] << 8) | cdb[13];
  1194. break;
  1195. case RECEIVE_DIAGNOSTIC:
  1196. case SEND_DIAGNOSTIC:
  1197. *size = (cdb[3] << 8) | cdb[4];
  1198. break;
  1199. case WRITE_BUFFER:
  1200. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1201. break;
  1202. case REPORT_LUNS:
  1203. cmd->execute_cmd = spc_emulate_report_luns;
  1204. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1205. /*
  1206. * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
  1207. * See spc4r17 section 5.3
  1208. */
  1209. cmd->sam_task_attr = TCM_HEAD_TAG;
  1210. break;
  1211. case TEST_UNIT_READY:
  1212. cmd->execute_cmd = spc_emulate_testunitready;
  1213. *size = 0;
  1214. break;
  1215. case MAINTENANCE_IN:
  1216. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1217. /*
  1218. * MAINTENANCE_IN from SCC-2
  1219. * Check for emulated MI_REPORT_TARGET_PGS
  1220. */
  1221. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1222. cmd->execute_cmd =
  1223. target_emulate_report_target_port_groups;
  1224. }
  1225. *size = get_unaligned_be32(&cdb[6]);
  1226. } else {
  1227. /*
  1228. * GPCMD_SEND_KEY from multi media commands
  1229. */
  1230. *size = get_unaligned_be16(&cdb[8]);
  1231. }
  1232. break;
  1233. case MAINTENANCE_OUT:
  1234. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1235. /*
  1236. * MAINTENANCE_OUT from SCC-2
  1237. * Check for emulated MO_SET_TARGET_PGS.
  1238. */
  1239. if (cdb[1] == MO_SET_TARGET_PGS) {
  1240. cmd->execute_cmd =
  1241. target_emulate_set_target_port_groups;
  1242. }
  1243. *size = get_unaligned_be32(&cdb[6]);
  1244. } else {
  1245. /*
  1246. * GPCMD_SEND_KEY from multi media commands
  1247. */
  1248. *size = get_unaligned_be16(&cdb[8]);
  1249. }
  1250. break;
  1251. default:
  1252. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1253. }
  1254. return 0;
  1255. }
  1256. EXPORT_SYMBOL(spc_parse_cdb);