target_core_spc.c 38 KB

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