target_core_cdb.c 34 KB

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