target_core_cdb.c 33 KB

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