target_core_cdb.c 35 KB

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