target_core_cdb.c 31 KB

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