target_core_spc.c 38 KB

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