target_core_spc.c 37 KB

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