target_core_cdb.c 36 KB

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