target_core_cdb.c 33 KB

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