target_core_cdb.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * CDB emulation for non-READ/WRITE commands.
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
  5. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  6. * Copyright (c) 2007-2010 Rising Tide Systems
  7. * Copyright (c) 2008-2010 Linux-iSCSI.org
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #include <asm/unaligned.h>
  26. #include <scsi/scsi.h>
  27. #include <target/target_core_base.h>
  28. #include <target/target_core_transport.h>
  29. #include <target/target_core_fabric_ops.h>
  30. #include "target_core_ua.h"
  31. static void
  32. target_fill_alua_data(struct se_port *port, unsigned char *buf)
  33. {
  34. struct t10_alua_tg_pt_gp *tg_pt_gp;
  35. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  36. /*
  37. * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
  38. */
  39. buf[5] = 0x80;
  40. /*
  41. * Set TPGS field for explict and/or implict ALUA access type
  42. * and opteration.
  43. *
  44. * See spc4r17 section 6.4.2 Table 135
  45. */
  46. if (!port)
  47. return;
  48. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  49. if (!tg_pt_gp_mem)
  50. return;
  51. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  52. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  53. if (tg_pt_gp)
  54. buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
  55. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  56. }
  57. static int
  58. target_emulate_inquiry_std(struct se_cmd *cmd)
  59. {
  60. struct se_lun *lun = SE_LUN(cmd);
  61. struct se_device *dev = SE_DEV(cmd);
  62. unsigned char *buf = cmd->t_task->t_task_buf;
  63. /*
  64. * Make sure we at least have 6 bytes of INQUIRY response
  65. * payload going back for EVPD=0
  66. */
  67. if (cmd->data_length < 6) {
  68. printk(KERN_ERR "SCSI Inquiry payload length: %u"
  69. " too small for EVPD=0\n", cmd->data_length);
  70. return -1;
  71. }
  72. buf[0] = dev->transport->get_device_type(dev);
  73. if (buf[0] == TYPE_TAPE)
  74. buf[1] = 0x80;
  75. buf[2] = dev->transport->get_device_rev(dev);
  76. /*
  77. * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
  78. *
  79. * SPC4 says:
  80. * A RESPONSE DATA FORMAT field set to 2h indicates that the
  81. * standard INQUIRY data is in the format defined in this
  82. * standard. Response data format values less than 2h are
  83. * obsolete. Response data format values greater than 2h are
  84. * reserved.
  85. */
  86. buf[3] = 2;
  87. /*
  88. * Enable SCCS and TPGS fields for Emulated ALUA
  89. */
  90. if (T10_ALUA(dev->se_sub_dev)->alua_type == SPC3_ALUA_EMULATED)
  91. target_fill_alua_data(lun->lun_sep, buf);
  92. if (cmd->data_length < 8) {
  93. buf[4] = 1; /* Set additional length to 1 */
  94. return 0;
  95. }
  96. buf[7] = 0x2; /* CmdQue=1 */
  97. /*
  98. * Do not include vendor, product, reversion info in INQUIRY
  99. * response payload for cdbs with a small allocation length.
  100. */
  101. if (cmd->data_length < 36) {
  102. buf[4] = 3; /* Set additional length to 3 */
  103. return 0;
  104. }
  105. snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
  106. snprintf((unsigned char *)&buf[16], 16, "%s",
  107. &DEV_T10_WWN(dev)->model[0]);
  108. snprintf((unsigned char *)&buf[32], 4, "%s",
  109. &DEV_T10_WWN(dev)->revision[0]);
  110. buf[4] = 31; /* Set additional length to 31 */
  111. return 0;
  112. }
  113. /* supported vital product data pages */
  114. static int
  115. target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  116. {
  117. buf[1] = 0x00;
  118. if (cmd->data_length < 8)
  119. return 0;
  120. buf[4] = 0x0;
  121. /*
  122. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  123. * Registered Extended LUN WWN has been set via ConfigFS
  124. * during device creation/restart.
  125. */
  126. if (SE_DEV(cmd)->se_sub_dev->su_dev_flags &
  127. SDF_EMULATED_VPD_UNIT_SERIAL) {
  128. buf[3] = 3;
  129. buf[5] = 0x80;
  130. buf[6] = 0x83;
  131. buf[7] = 0x86;
  132. }
  133. return 0;
  134. }
  135. /* unit serial number */
  136. static int
  137. target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  138. {
  139. struct se_device *dev = SE_DEV(cmd);
  140. u16 len = 0;
  141. buf[1] = 0x80;
  142. if (dev->se_sub_dev->su_dev_flags &
  143. SDF_EMULATED_VPD_UNIT_SERIAL) {
  144. u32 unit_serial_len;
  145. unit_serial_len =
  146. strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
  147. unit_serial_len++; /* For NULL Terminator */
  148. if (((len + 4) + unit_serial_len) > cmd->data_length) {
  149. len += unit_serial_len;
  150. buf[2] = ((len >> 8) & 0xff);
  151. buf[3] = (len & 0xff);
  152. return 0;
  153. }
  154. len += sprintf((unsigned char *)&buf[4], "%s",
  155. &DEV_T10_WWN(dev)->unit_serial[0]);
  156. len++; /* Extra Byte for NULL Terminator */
  157. buf[3] = len;
  158. }
  159. return 0;
  160. }
  161. /*
  162. * Device identification VPD, for a complete list of
  163. * DESIGNATOR TYPEs see spc4r17 Table 459.
  164. */
  165. static int
  166. target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  167. {
  168. struct se_device *dev = SE_DEV(cmd);
  169. struct se_lun *lun = SE_LUN(cmd);
  170. struct se_port *port = NULL;
  171. struct se_portal_group *tpg = NULL;
  172. struct t10_alua_lu_gp_member *lu_gp_mem;
  173. struct t10_alua_tg_pt_gp *tg_pt_gp;
  174. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  175. unsigned char binary, binary_new;
  176. unsigned char *prod = &DEV_T10_WWN(dev)->model[0];
  177. u32 prod_len;
  178. u32 unit_serial_len, off = 0;
  179. int i;
  180. u16 len = 0, id_len;
  181. buf[1] = 0x83;
  182. off = 4;
  183. /*
  184. * NAA IEEE Registered Extended Assigned designator format, see
  185. * spc4r17 section 7.7.3.6.5
  186. *
  187. * We depend upon a target_core_mod/ConfigFS provided
  188. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  189. * value in order to return the NAA id.
  190. */
  191. if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
  192. goto check_t10_vend_desc;
  193. if (off + 20 > cmd->data_length)
  194. goto check_t10_vend_desc;
  195. /* CODE SET == Binary */
  196. buf[off++] = 0x1;
  197. /* Set ASSOICATION == addressed logical unit: 0)b */
  198. buf[off] = 0x00;
  199. /* Identifier/Designator type == NAA identifier */
  200. buf[off++] = 0x3;
  201. off++;
  202. /* Identifier/Designator length */
  203. buf[off++] = 0x10;
  204. /*
  205. * Start NAA IEEE Registered Extended Identifier/Designator
  206. */
  207. buf[off++] = (0x6 << 4);
  208. /*
  209. * Use OpenFabrics IEEE Company ID: 00 14 05
  210. */
  211. buf[off++] = 0x01;
  212. buf[off++] = 0x40;
  213. buf[off] = (0x5 << 4);
  214. /*
  215. * Return ConfigFS Unit Serial Number information for
  216. * VENDOR_SPECIFIC_IDENTIFIER and
  217. * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
  218. */
  219. binary = transport_asciihex_to_binaryhex(
  220. &DEV_T10_WWN(dev)->unit_serial[0]);
  221. buf[off++] |= (binary & 0xf0) >> 4;
  222. for (i = 0; i < 24; i += 2) {
  223. binary_new = transport_asciihex_to_binaryhex(
  224. &DEV_T10_WWN(dev)->unit_serial[i+2]);
  225. buf[off] = (binary & 0x0f) << 4;
  226. buf[off++] |= (binary_new & 0xf0) >> 4;
  227. binary = binary_new;
  228. }
  229. len = 20;
  230. off = (len + 4);
  231. check_t10_vend_desc:
  232. /*
  233. * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
  234. */
  235. id_len = 8; /* For Vendor field */
  236. prod_len = 4; /* For VPD Header */
  237. prod_len += 8; /* For Vendor field */
  238. prod_len += strlen(prod);
  239. prod_len++; /* For : */
  240. if (dev->se_sub_dev->su_dev_flags &
  241. SDF_EMULATED_VPD_UNIT_SERIAL) {
  242. unit_serial_len =
  243. strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
  244. unit_serial_len++; /* For NULL Terminator */
  245. if ((len + (id_len + 4) +
  246. (prod_len + unit_serial_len)) >
  247. cmd->data_length) {
  248. len += (prod_len + unit_serial_len);
  249. goto check_port;
  250. }
  251. id_len += sprintf((unsigned char *)&buf[off+12],
  252. "%s:%s", prod,
  253. &DEV_T10_WWN(dev)->unit_serial[0]);
  254. }
  255. buf[off] = 0x2; /* ASCII */
  256. buf[off+1] = 0x1; /* T10 Vendor ID */
  257. buf[off+2] = 0x0;
  258. memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
  259. /* Extra Byte for NULL Terminator */
  260. id_len++;
  261. /* Identifier Length */
  262. buf[off+3] = id_len;
  263. /* Header size for Designation descriptor */
  264. len += (id_len + 4);
  265. off += (id_len + 4);
  266. /*
  267. * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
  268. */
  269. check_port:
  270. port = lun->lun_sep;
  271. if (port) {
  272. struct t10_alua_lu_gp *lu_gp;
  273. u32 padding, scsi_name_len;
  274. u16 lu_gp_id = 0;
  275. u16 tg_pt_gp_id = 0;
  276. u16 tpgt;
  277. tpg = port->sep_tpg;
  278. /*
  279. * Relative target port identifer, see spc4r17
  280. * section 7.7.3.7
  281. *
  282. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  283. * section 7.5.1 Table 362
  284. */
  285. if (((len + 4) + 8) > cmd->data_length) {
  286. len += 8;
  287. goto check_tpgi;
  288. }
  289. buf[off] =
  290. (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
  291. buf[off++] |= 0x1; /* CODE SET == Binary */
  292. buf[off] = 0x80; /* Set PIV=1 */
  293. /* Set ASSOICATION == target port: 01b */
  294. buf[off] |= 0x10;
  295. /* DESIGNATOR TYPE == Relative target port identifer */
  296. buf[off++] |= 0x4;
  297. off++; /* Skip over Reserved */
  298. buf[off++] = 4; /* DESIGNATOR LENGTH */
  299. /* Skip over Obsolete field in RTPI payload
  300. * in Table 472 */
  301. off += 2;
  302. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  303. buf[off++] = (port->sep_rtpi & 0xff);
  304. len += 8; /* Header size + Designation descriptor */
  305. /*
  306. * Target port group identifier, see spc4r17
  307. * section 7.7.3.8
  308. *
  309. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  310. * section 7.5.1 Table 362
  311. */
  312. check_tpgi:
  313. if (T10_ALUA(dev->se_sub_dev)->alua_type !=
  314. SPC3_ALUA_EMULATED)
  315. goto check_scsi_name;
  316. if (((len + 4) + 8) > cmd->data_length) {
  317. len += 8;
  318. goto check_lu_gp;
  319. }
  320. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  321. if (!tg_pt_gp_mem)
  322. goto check_lu_gp;
  323. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  324. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  325. if (!(tg_pt_gp)) {
  326. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  327. goto check_lu_gp;
  328. }
  329. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  330. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  331. buf[off] =
  332. (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
  333. buf[off++] |= 0x1; /* CODE SET == Binary */
  334. buf[off] = 0x80; /* Set PIV=1 */
  335. /* Set ASSOICATION == target port: 01b */
  336. buf[off] |= 0x10;
  337. /* DESIGNATOR TYPE == Target port group identifier */
  338. buf[off++] |= 0x5;
  339. off++; /* Skip over Reserved */
  340. buf[off++] = 4; /* DESIGNATOR LENGTH */
  341. off += 2; /* Skip over Reserved Field */
  342. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  343. buf[off++] = (tg_pt_gp_id & 0xff);
  344. len += 8; /* Header size + Designation descriptor */
  345. /*
  346. * Logical Unit Group identifier, see spc4r17
  347. * section 7.7.3.8
  348. */
  349. check_lu_gp:
  350. if (((len + 4) + 8) > cmd->data_length) {
  351. len += 8;
  352. goto check_scsi_name;
  353. }
  354. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  355. if (!(lu_gp_mem))
  356. goto check_scsi_name;
  357. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  358. lu_gp = lu_gp_mem->lu_gp;
  359. if (!(lu_gp)) {
  360. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  361. goto check_scsi_name;
  362. }
  363. lu_gp_id = lu_gp->lu_gp_id;
  364. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  365. buf[off++] |= 0x1; /* CODE SET == Binary */
  366. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  367. buf[off++] |= 0x6;
  368. off++; /* Skip over Reserved */
  369. buf[off++] = 4; /* DESIGNATOR LENGTH */
  370. off += 2; /* Skip over Reserved Field */
  371. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  372. buf[off++] = (lu_gp_id & 0xff);
  373. len += 8; /* Header size + Designation descriptor */
  374. /*
  375. * SCSI name string designator, see spc4r17
  376. * section 7.7.3.11
  377. *
  378. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  379. * section 7.5.1 Table 362
  380. */
  381. check_scsi_name:
  382. scsi_name_len = strlen(TPG_TFO(tpg)->tpg_get_wwn(tpg));
  383. /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
  384. scsi_name_len += 10;
  385. /* Check for 4-byte padding */
  386. padding = ((-scsi_name_len) & 3);
  387. if (padding != 0)
  388. scsi_name_len += padding;
  389. /* Header size + Designation descriptor */
  390. scsi_name_len += 4;
  391. if (((len + 4) + scsi_name_len) > cmd->data_length) {
  392. len += scsi_name_len;
  393. goto set_len;
  394. }
  395. buf[off] =
  396. (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
  397. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  398. buf[off] = 0x80; /* Set PIV=1 */
  399. /* Set ASSOICATION == target port: 01b */
  400. buf[off] |= 0x10;
  401. /* DESIGNATOR TYPE == SCSI name string */
  402. buf[off++] |= 0x8;
  403. off += 2; /* Skip over Reserved and length */
  404. /*
  405. * SCSI name string identifer containing, $FABRIC_MOD
  406. * dependent information. For LIO-Target and iSCSI
  407. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  408. * UTF-8 encoding.
  409. */
  410. tpgt = TPG_TFO(tpg)->tpg_get_tag(tpg);
  411. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  412. TPG_TFO(tpg)->tpg_get_wwn(tpg), tpgt);
  413. scsi_name_len += 1 /* Include NULL terminator */;
  414. /*
  415. * The null-terminated, null-padded (see 4.4.2) SCSI
  416. * NAME STRING field contains a UTF-8 format string.
  417. * The number of bytes in the SCSI NAME STRING field
  418. * (i.e., the value in the DESIGNATOR LENGTH field)
  419. * shall be no larger than 256 and shall be a multiple
  420. * of four.
  421. */
  422. if (padding)
  423. scsi_name_len += padding;
  424. buf[off-1] = scsi_name_len;
  425. off += scsi_name_len;
  426. /* Header size + Designation descriptor */
  427. len += (scsi_name_len + 4);
  428. }
  429. set_len:
  430. buf[2] = ((len >> 8) & 0xff);
  431. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  432. return 0;
  433. }
  434. /* Extended INQUIRY Data VPD Page */
  435. static int
  436. target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  437. {
  438. if (cmd->data_length < 60)
  439. return 0;
  440. buf[1] = 0x86;
  441. buf[2] = 0x3c;
  442. /* Set HEADSUP, ORDSUP, SIMPSUP */
  443. buf[5] = 0x07;
  444. /* If WriteCache emulation is enabled, set V_SUP */
  445. if (DEV_ATTRIB(SE_DEV(cmd))->emulate_write_cache > 0)
  446. buf[6] = 0x01;
  447. return 0;
  448. }
  449. /* Block Limits VPD page */
  450. static int
  451. target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  452. {
  453. struct se_device *dev = SE_DEV(cmd);
  454. int have_tp = 0;
  455. /*
  456. * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
  457. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  458. * different page length for Thin Provisioning.
  459. */
  460. if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
  461. have_tp = 1;
  462. if (cmd->data_length < (0x10 + 4)) {
  463. printk(KERN_INFO "Received data_length: %u"
  464. " too small for EVPD 0xb0\n",
  465. cmd->data_length);
  466. return -1;
  467. }
  468. if (have_tp && cmd->data_length < (0x3c + 4)) {
  469. printk(KERN_INFO "Received data_length: %u"
  470. " too small for TPE=1 EVPD 0xb0\n",
  471. cmd->data_length);
  472. have_tp = 0;
  473. }
  474. buf[0] = dev->transport->get_device_type(dev);
  475. buf[1] = 0xb0;
  476. buf[3] = have_tp ? 0x3c : 0x10;
  477. /*
  478. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  479. */
  480. put_unaligned_be16(1, &buf[6]);
  481. /*
  482. * Set MAXIMUM TRANSFER LENGTH
  483. */
  484. put_unaligned_be32(DEV_ATTRIB(dev)->max_sectors, &buf[8]);
  485. /*
  486. * Set OPTIMAL TRANSFER LENGTH
  487. */
  488. put_unaligned_be32(DEV_ATTRIB(dev)->optimal_sectors, &buf[12]);
  489. /*
  490. * Exit now if we don't support TP or the initiator sent a too
  491. * short buffer.
  492. */
  493. if (!have_tp || cmd->data_length < (0x3c + 4))
  494. return 0;
  495. /*
  496. * Set MAXIMUM UNMAP LBA COUNT
  497. */
  498. put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_lba_count, &buf[20]);
  499. /*
  500. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  501. */
  502. put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_block_desc_count,
  503. &buf[24]);
  504. /*
  505. * Set OPTIMAL UNMAP GRANULARITY
  506. */
  507. put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity, &buf[28]);
  508. /*
  509. * UNMAP GRANULARITY ALIGNMENT
  510. */
  511. put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity_alignment,
  512. &buf[32]);
  513. if (DEV_ATTRIB(dev)->unmap_granularity_alignment != 0)
  514. buf[32] |= 0x80; /* Set the UGAVALID bit */
  515. return 0;
  516. }
  517. /* Thin Provisioning VPD */
  518. static int
  519. target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  520. {
  521. struct se_device *dev = SE_DEV(cmd);
  522. /*
  523. * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
  524. *
  525. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  526. * zero, then the page length shall be set to 0004h. If the DP bit
  527. * is set to one, then the page length shall be set to the value
  528. * defined in table 162.
  529. */
  530. buf[0] = dev->transport->get_device_type(dev);
  531. buf[1] = 0xb2;
  532. /*
  533. * Set Hardcoded length mentioned above for DP=0
  534. */
  535. put_unaligned_be16(0x0004, &buf[2]);
  536. /*
  537. * The THRESHOLD EXPONENT field indicates the threshold set size in
  538. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  539. * 2(threshold exponent)).
  540. *
  541. * Note that this is currently set to 0x00 as mkp says it will be
  542. * changing again. We can enable this once it has settled in T10
  543. * and is actually used by Linux/SCSI ML code.
  544. */
  545. buf[4] = 0x00;
  546. /*
  547. * A TPU bit set to one indicates that the device server supports
  548. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  549. * that the device server does not support the UNMAP command.
  550. */
  551. if (DEV_ATTRIB(dev)->emulate_tpu != 0)
  552. buf[5] = 0x80;
  553. /*
  554. * A TPWS bit set to one indicates that the device server supports
  555. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  556. * A TPWS bit set to zero indicates that the device server does not
  557. * support the use of the WRITE SAME (16) command to unmap LBAs.
  558. */
  559. if (DEV_ATTRIB(dev)->emulate_tpws != 0)
  560. buf[5] |= 0x40;
  561. return 0;
  562. }
  563. static int
  564. target_emulate_inquiry(struct se_cmd *cmd)
  565. {
  566. struct se_device *dev = SE_DEV(cmd);
  567. unsigned char *buf = cmd->t_task->t_task_buf;
  568. unsigned char *cdb = cmd->t_task->t_task_cdb;
  569. if (!(cdb[1] & 0x1))
  570. return target_emulate_inquiry_std(cmd);
  571. /*
  572. * Make sure we at least have 4 bytes of INQUIRY response
  573. * payload for 0x00 going back for EVPD=1. Note that 0x80
  574. * and 0x83 will check for enough payload data length and
  575. * jump to set_len: label when there is not enough inquiry EVPD
  576. * payload length left for the next outgoing EVPD metadata
  577. */
  578. if (cmd->data_length < 4) {
  579. printk(KERN_ERR "SCSI Inquiry payload length: %u"
  580. " too small for EVPD=1\n", cmd->data_length);
  581. return -1;
  582. }
  583. buf[0] = dev->transport->get_device_type(dev);
  584. switch (cdb[2]) {
  585. case 0x00:
  586. return target_emulate_evpd_00(cmd, buf);
  587. case 0x80:
  588. return target_emulate_evpd_80(cmd, buf);
  589. case 0x83:
  590. return target_emulate_evpd_83(cmd, buf);
  591. case 0x86:
  592. return target_emulate_evpd_86(cmd, buf);
  593. case 0xb0:
  594. return target_emulate_evpd_b0(cmd, buf);
  595. case 0xb2:
  596. return target_emulate_evpd_b2(cmd, buf);
  597. default:
  598. printk(KERN_ERR "Unknown VPD Code: 0x%02x\n", cdb[2]);
  599. return -1;
  600. }
  601. return 0;
  602. }
  603. static int
  604. target_emulate_readcapacity(struct se_cmd *cmd)
  605. {
  606. struct se_device *dev = SE_DEV(cmd);
  607. unsigned char *buf = cmd->t_task->t_task_buf;
  608. unsigned long long blocks_long = dev->transport->get_blocks(dev);
  609. u32 blocks;
  610. if (blocks_long >= 0x00000000ffffffff)
  611. blocks = 0xffffffff;
  612. else
  613. blocks = (u32)blocks_long;
  614. buf[0] = (blocks >> 24) & 0xff;
  615. buf[1] = (blocks >> 16) & 0xff;
  616. buf[2] = (blocks >> 8) & 0xff;
  617. buf[3] = blocks & 0xff;
  618. buf[4] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
  619. buf[5] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
  620. buf[6] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
  621. buf[7] = DEV_ATTRIB(dev)->block_size & 0xff;
  622. /*
  623. * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
  624. */
  625. if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
  626. put_unaligned_be32(0xFFFFFFFF, &buf[0]);
  627. return 0;
  628. }
  629. static int
  630. target_emulate_readcapacity_16(struct se_cmd *cmd)
  631. {
  632. struct se_device *dev = SE_DEV(cmd);
  633. unsigned char *buf = cmd->t_task->t_task_buf;
  634. unsigned long long blocks = dev->transport->get_blocks(dev);
  635. buf[0] = (blocks >> 56) & 0xff;
  636. buf[1] = (blocks >> 48) & 0xff;
  637. buf[2] = (blocks >> 40) & 0xff;
  638. buf[3] = (blocks >> 32) & 0xff;
  639. buf[4] = (blocks >> 24) & 0xff;
  640. buf[5] = (blocks >> 16) & 0xff;
  641. buf[6] = (blocks >> 8) & 0xff;
  642. buf[7] = blocks & 0xff;
  643. buf[8] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
  644. buf[9] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
  645. buf[10] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
  646. buf[11] = DEV_ATTRIB(dev)->block_size & 0xff;
  647. /*
  648. * Set Thin Provisioning Enable bit following sbc3r22 in section
  649. * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
  650. */
  651. if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
  652. buf[14] = 0x80;
  653. return 0;
  654. }
  655. static int
  656. target_modesense_rwrecovery(unsigned char *p)
  657. {
  658. p[0] = 0x01;
  659. p[1] = 0x0a;
  660. return 12;
  661. }
  662. static int
  663. target_modesense_control(struct se_device *dev, unsigned char *p)
  664. {
  665. p[0] = 0x0a;
  666. p[1] = 0x0a;
  667. p[2] = 2;
  668. /*
  669. * From spc4r17, section 7.4.6 Control mode Page
  670. *
  671. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  672. *
  673. * 00b: The logical unit shall clear any unit attention condition
  674. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  675. * status and shall not establish a unit attention condition when a com-
  676. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  677. * status.
  678. *
  679. * 10b: The logical unit shall not clear any unit attention condition
  680. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  681. * status and shall not establish a unit attention condition when
  682. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  683. * CONFLICT status.
  684. *
  685. * 11b a The logical unit shall not clear any unit attention condition
  686. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  687. * status and shall establish a unit attention condition for the
  688. * initiator port associated with the I_T nexus on which the BUSY,
  689. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  690. * Depending on the status, the additional sense code shall be set to
  691. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  692. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  693. * command, a unit attention condition shall be established only once
  694. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  695. * to the number of commands completed with one of those status codes.
  696. */
  697. p[4] = (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 2) ? 0x30 :
  698. (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  699. /*
  700. * From spc4r17, section 7.4.6 Control mode Page
  701. *
  702. * Task Aborted Status (TAS) bit set to zero.
  703. *
  704. * A task aborted status (TAS) bit set to zero specifies that aborted
  705. * tasks shall be terminated by the device server without any response
  706. * to the application client. A TAS bit set to one specifies that tasks
  707. * aborted by the actions of an I_T nexus other than the I_T nexus on
  708. * which the command was received shall be completed with TASK ABORTED
  709. * status (see SAM-4).
  710. */
  711. p[5] = (DEV_ATTRIB(dev)->emulate_tas) ? 0x40 : 0x00;
  712. p[8] = 0xff;
  713. p[9] = 0xff;
  714. p[11] = 30;
  715. return 12;
  716. }
  717. static int
  718. target_modesense_caching(struct se_device *dev, unsigned char *p)
  719. {
  720. p[0] = 0x08;
  721. p[1] = 0x12;
  722. if (DEV_ATTRIB(dev)->emulate_write_cache > 0)
  723. p[2] = 0x04; /* Write Cache Enable */
  724. p[12] = 0x20; /* Disabled Read Ahead */
  725. return 20;
  726. }
  727. static void
  728. target_modesense_write_protect(unsigned char *buf, int type)
  729. {
  730. /*
  731. * I believe that the WP bit (bit 7) in the mode header is the same for
  732. * all device types..
  733. */
  734. switch (type) {
  735. case TYPE_DISK:
  736. case TYPE_TAPE:
  737. default:
  738. buf[0] |= 0x80; /* WP bit */
  739. break;
  740. }
  741. }
  742. static void
  743. target_modesense_dpofua(unsigned char *buf, int type)
  744. {
  745. switch (type) {
  746. case TYPE_DISK:
  747. buf[0] |= 0x10; /* DPOFUA bit */
  748. break;
  749. default:
  750. break;
  751. }
  752. }
  753. static int
  754. target_emulate_modesense(struct se_cmd *cmd, int ten)
  755. {
  756. struct se_device *dev = SE_DEV(cmd);
  757. char *cdb = cmd->t_task->t_task_cdb;
  758. unsigned char *rbuf = cmd->t_task->t_task_buf;
  759. int type = dev->transport->get_device_type(dev);
  760. int offset = (ten) ? 8 : 4;
  761. int length = 0;
  762. unsigned char buf[SE_MODE_PAGE_BUF];
  763. memset(buf, 0, SE_MODE_PAGE_BUF);
  764. switch (cdb[2] & 0x3f) {
  765. case 0x01:
  766. length = target_modesense_rwrecovery(&buf[offset]);
  767. break;
  768. case 0x08:
  769. length = target_modesense_caching(dev, &buf[offset]);
  770. break;
  771. case 0x0a:
  772. length = target_modesense_control(dev, &buf[offset]);
  773. break;
  774. case 0x3f:
  775. length = target_modesense_rwrecovery(&buf[offset]);
  776. length += target_modesense_caching(dev, &buf[offset+length]);
  777. length += target_modesense_control(dev, &buf[offset+length]);
  778. break;
  779. default:
  780. printk(KERN_ERR "Got Unknown Mode Page: 0x%02x\n",
  781. cdb[2] & 0x3f);
  782. return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
  783. }
  784. offset += length;
  785. if (ten) {
  786. offset -= 2;
  787. buf[0] = (offset >> 8) & 0xff;
  788. buf[1] = offset & 0xff;
  789. if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  790. (cmd->se_deve &&
  791. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  792. target_modesense_write_protect(&buf[3], type);
  793. if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
  794. (DEV_ATTRIB(dev)->emulate_fua_write > 0))
  795. target_modesense_dpofua(&buf[3], type);
  796. if ((offset + 2) > cmd->data_length)
  797. offset = cmd->data_length;
  798. } else {
  799. offset -= 1;
  800. buf[0] = offset & 0xff;
  801. if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  802. (cmd->se_deve &&
  803. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  804. target_modesense_write_protect(&buf[2], type);
  805. if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
  806. (DEV_ATTRIB(dev)->emulate_fua_write > 0))
  807. target_modesense_dpofua(&buf[2], type);
  808. if ((offset + 1) > cmd->data_length)
  809. offset = cmd->data_length;
  810. }
  811. memcpy(rbuf, buf, offset);
  812. return 0;
  813. }
  814. static int
  815. target_emulate_request_sense(struct se_cmd *cmd)
  816. {
  817. unsigned char *cdb = cmd->t_task->t_task_cdb;
  818. unsigned char *buf = cmd->t_task->t_task_buf;
  819. u8 ua_asc = 0, ua_ascq = 0;
  820. if (cdb[1] & 0x01) {
  821. printk(KERN_ERR "REQUEST_SENSE description emulation not"
  822. " supported\n");
  823. return PYX_TRANSPORT_INVALID_CDB_FIELD;
  824. }
  825. if (!(core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))) {
  826. /*
  827. * CURRENT ERROR, UNIT ATTENTION
  828. */
  829. buf[0] = 0x70;
  830. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  831. /*
  832. * Make sure request data length is enough for additional
  833. * sense data.
  834. */
  835. if (cmd->data_length <= 18) {
  836. buf[7] = 0x00;
  837. return 0;
  838. }
  839. /*
  840. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  841. */
  842. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  843. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  844. buf[7] = 0x0A;
  845. } else {
  846. /*
  847. * CURRENT ERROR, NO SENSE
  848. */
  849. buf[0] = 0x70;
  850. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  851. /*
  852. * Make sure request data length is enough for additional
  853. * sense data.
  854. */
  855. if (cmd->data_length <= 18) {
  856. buf[7] = 0x00;
  857. return 0;
  858. }
  859. /*
  860. * NO ADDITIONAL SENSE INFORMATION
  861. */
  862. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  863. buf[7] = 0x0A;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  869. * Note this is not used for TCM/pSCSI passthrough
  870. */
  871. static int
  872. target_emulate_unmap(struct se_task *task)
  873. {
  874. struct se_cmd *cmd = TASK_CMD(task);
  875. struct se_device *dev = SE_DEV(cmd);
  876. unsigned char *buf = cmd->t_task->t_task_buf, *ptr = NULL;
  877. unsigned char *cdb = &cmd->t_task->t_task_cdb[0];
  878. sector_t lba;
  879. unsigned int size = cmd->data_length, range;
  880. int ret, offset;
  881. unsigned short dl, bd_dl;
  882. /* First UNMAP block descriptor starts at 8 byte offset */
  883. offset = 8;
  884. size -= 8;
  885. dl = get_unaligned_be16(&cdb[0]);
  886. bd_dl = get_unaligned_be16(&cdb[2]);
  887. ptr = &buf[offset];
  888. printk(KERN_INFO "UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  889. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  890. while (size) {
  891. lba = get_unaligned_be64(&ptr[0]);
  892. range = get_unaligned_be32(&ptr[8]);
  893. printk(KERN_INFO "UNMAP: Using lba: %llu and range: %u\n",
  894. (unsigned long long)lba, range);
  895. ret = dev->transport->do_discard(dev, lba, range);
  896. if (ret < 0) {
  897. printk(KERN_ERR "blkdev_issue_discard() failed: %d\n",
  898. ret);
  899. return -1;
  900. }
  901. ptr += 16;
  902. size -= 16;
  903. }
  904. task->task_scsi_status = GOOD;
  905. transport_complete_task(task, 1);
  906. return 0;
  907. }
  908. /*
  909. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  910. * Note this is not used for TCM/pSCSI passthrough
  911. */
  912. static int
  913. target_emulate_write_same(struct se_task *task)
  914. {
  915. struct se_cmd *cmd = TASK_CMD(task);
  916. struct se_device *dev = SE_DEV(cmd);
  917. sector_t lba = cmd->t_task->t_task_lba;
  918. unsigned int range;
  919. int ret;
  920. range = (cmd->data_length / DEV_ATTRIB(dev)->block_size);
  921. printk(KERN_INFO "WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
  922. (unsigned long long)lba, range);
  923. ret = dev->transport->do_discard(dev, lba, range);
  924. if (ret < 0) {
  925. printk(KERN_INFO "blkdev_issue_discard() failed for WRITE_SAME\n");
  926. return -1;
  927. }
  928. task->task_scsi_status = GOOD;
  929. transport_complete_task(task, 1);
  930. return 0;
  931. }
  932. int
  933. transport_emulate_control_cdb(struct se_task *task)
  934. {
  935. struct se_cmd *cmd = TASK_CMD(task);
  936. struct se_device *dev = SE_DEV(cmd);
  937. unsigned short service_action;
  938. int ret = 0;
  939. switch (cmd->t_task->t_task_cdb[0]) {
  940. case INQUIRY:
  941. ret = target_emulate_inquiry(cmd);
  942. break;
  943. case READ_CAPACITY:
  944. ret = target_emulate_readcapacity(cmd);
  945. break;
  946. case MODE_SENSE:
  947. ret = target_emulate_modesense(cmd, 0);
  948. break;
  949. case MODE_SENSE_10:
  950. ret = target_emulate_modesense(cmd, 1);
  951. break;
  952. case SERVICE_ACTION_IN:
  953. switch (cmd->t_task->t_task_cdb[1] & 0x1f) {
  954. case SAI_READ_CAPACITY_16:
  955. ret = target_emulate_readcapacity_16(cmd);
  956. break;
  957. default:
  958. printk(KERN_ERR "Unsupported SA: 0x%02x\n",
  959. cmd->t_task->t_task_cdb[1] & 0x1f);
  960. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  961. }
  962. break;
  963. case REQUEST_SENSE:
  964. ret = target_emulate_request_sense(cmd);
  965. break;
  966. case UNMAP:
  967. if (!dev->transport->do_discard) {
  968. printk(KERN_ERR "UNMAP emulation not supported for: %s\n",
  969. dev->transport->name);
  970. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  971. }
  972. ret = target_emulate_unmap(task);
  973. break;
  974. case WRITE_SAME_16:
  975. if (!dev->transport->do_discard) {
  976. printk(KERN_ERR "WRITE_SAME_16 emulation not supported"
  977. " for: %s\n", dev->transport->name);
  978. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  979. }
  980. ret = target_emulate_write_same(task);
  981. break;
  982. case VARIABLE_LENGTH_CMD:
  983. service_action =
  984. get_unaligned_be16(&cmd->t_task->t_task_cdb[8]);
  985. switch (service_action) {
  986. case WRITE_SAME_32:
  987. if (!dev->transport->do_discard) {
  988. printk(KERN_ERR "WRITE_SAME_32 SA emulation not"
  989. " supported for: %s\n",
  990. dev->transport->name);
  991. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  992. }
  993. ret = target_emulate_write_same(task);
  994. break;
  995. default:
  996. printk(KERN_ERR "Unsupported VARIABLE_LENGTH_CMD SA:"
  997. " 0x%02x\n", service_action);
  998. break;
  999. }
  1000. break;
  1001. case SYNCHRONIZE_CACHE:
  1002. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  1003. if (!dev->transport->do_sync_cache) {
  1004. printk(KERN_ERR
  1005. "SYNCHRONIZE_CACHE emulation not supported"
  1006. " for: %s\n", dev->transport->name);
  1007. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1008. }
  1009. dev->transport->do_sync_cache(task);
  1010. break;
  1011. case ALLOW_MEDIUM_REMOVAL:
  1012. case ERASE:
  1013. case REZERO_UNIT:
  1014. case SEEK_10:
  1015. case SPACE:
  1016. case START_STOP:
  1017. case TEST_UNIT_READY:
  1018. case VERIFY:
  1019. case WRITE_FILEMARKS:
  1020. break;
  1021. default:
  1022. printk(KERN_ERR "Unsupported SCSI Opcode: 0x%02x for %s\n",
  1023. cmd->t_task->t_task_cdb[0], dev->transport->name);
  1024. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1025. }
  1026. if (ret < 0)
  1027. return ret;
  1028. task->task_scsi_status = GOOD;
  1029. transport_complete_task(task, 1);
  1030. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  1031. }