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