target_core_spc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /*
  2. * SCSI Primary Commands (SPC) parsing and emulation.
  3. *
  4. * (c) Copyright 2002-2013 Datera, Inc.
  5. *
  6. * Nicholas A. Bellinger <nab@kernel.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <asm/unaligned.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_tcq.h>
  27. #include <target/target_core_base.h>
  28. #include <target/target_core_backend.h>
  29. #include <target/target_core_fabric.h>
  30. #include "target_core_internal.h"
  31. #include "target_core_alua.h"
  32. #include "target_core_pr.h"
  33. #include "target_core_ua.h"
  34. #include "target_core_xcopy.h"
  35. static void spc_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 explicit and/or implicit 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. sense_reason_t
  61. spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
  62. {
  63. struct se_lun *lun = cmd->se_lun;
  64. struct se_device *dev = cmd->se_dev;
  65. struct se_session *sess = cmd->se_sess;
  66. /* Set RMB (removable media) for tape devices */
  67. if (dev->transport->get_device_type(dev) == TYPE_TAPE)
  68. buf[1] = 0x80;
  69. buf[2] = 0x05; /* SPC-3 */
  70. /*
  71. * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
  72. *
  73. * SPC4 says:
  74. * A RESPONSE DATA FORMAT field set to 2h indicates that the
  75. * standard INQUIRY data is in the format defined in this
  76. * standard. Response data format values less than 2h are
  77. * obsolete. Response data format values greater than 2h are
  78. * reserved.
  79. */
  80. buf[3] = 2;
  81. /*
  82. * Enable SCCS and TPGS fields for Emulated ALUA
  83. */
  84. spc_fill_alua_data(lun->lun_sep, buf);
  85. /*
  86. * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY
  87. */
  88. if (dev->dev_attrib.emulate_3pc)
  89. buf[5] |= 0x8;
  90. /*
  91. * Set Protection (PROTECT) bit when DIF has been enabled on the
  92. * device, and the transport supports VERIFY + PASS.
  93. */
  94. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  95. if (dev->dev_attrib.pi_prot_type)
  96. buf[5] |= 0x1;
  97. }
  98. buf[7] = 0x2; /* CmdQue=1 */
  99. memcpy(&buf[8], "LIO-ORG ", 8);
  100. memset(&buf[16], 0x20, 16);
  101. memcpy(&buf[16], dev->t10_wwn.model,
  102. min_t(size_t, strlen(dev->t10_wwn.model), 16));
  103. memcpy(&buf[32], dev->t10_wwn.revision,
  104. min_t(size_t, strlen(dev->t10_wwn.revision), 4));
  105. buf[4] = 31; /* Set additional length to 31 */
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(spc_emulate_inquiry_std);
  109. /* unit serial number */
  110. static sense_reason_t
  111. spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  112. {
  113. struct se_device *dev = cmd->se_dev;
  114. u16 len;
  115. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  116. len = sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial);
  117. len++; /* Extra Byte for NULL Terminator */
  118. buf[3] = len;
  119. }
  120. return 0;
  121. }
  122. void spc_parse_naa_6h_vendor_specific(struct se_device *dev,
  123. unsigned char *buf)
  124. {
  125. unsigned char *p = &dev->t10_wwn.unit_serial[0];
  126. int cnt;
  127. bool next = true;
  128. /*
  129. * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
  130. * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
  131. * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
  132. * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
  133. * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
  134. * per device uniqeness.
  135. */
  136. for (cnt = 0; *p && cnt < 13; p++) {
  137. int val = hex_to_bin(*p);
  138. if (val < 0)
  139. continue;
  140. if (next) {
  141. next = false;
  142. buf[cnt++] |= val;
  143. } else {
  144. next = true;
  145. buf[cnt] = val << 4;
  146. }
  147. }
  148. }
  149. /*
  150. * Device identification VPD, for a complete list of
  151. * DESIGNATOR TYPEs see spc4r17 Table 459.
  152. */
  153. sense_reason_t
  154. spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  155. {
  156. struct se_device *dev = cmd->se_dev;
  157. struct se_lun *lun = cmd->se_lun;
  158. struct se_port *port = NULL;
  159. struct se_portal_group *tpg = NULL;
  160. struct t10_alua_lu_gp_member *lu_gp_mem;
  161. struct t10_alua_tg_pt_gp *tg_pt_gp;
  162. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  163. unsigned char *prod = &dev->t10_wwn.model[0];
  164. u32 prod_len;
  165. u32 unit_serial_len, off = 0;
  166. u16 len = 0, id_len;
  167. off = 4;
  168. /*
  169. * NAA IEEE Registered Extended Assigned designator format, see
  170. * spc4r17 section 7.7.3.6.5
  171. *
  172. * We depend upon a target_core_mod/ConfigFS provided
  173. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  174. * value in order to return the NAA id.
  175. */
  176. if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL))
  177. goto check_t10_vend_desc;
  178. /* CODE SET == Binary */
  179. buf[off++] = 0x1;
  180. /* Set ASSOCIATION == addressed logical unit: 0)b */
  181. buf[off] = 0x00;
  182. /* Identifier/Designator type == NAA identifier */
  183. buf[off++] |= 0x3;
  184. off++;
  185. /* Identifier/Designator length */
  186. buf[off++] = 0x10;
  187. /*
  188. * Start NAA IEEE Registered Extended Identifier/Designator
  189. */
  190. buf[off++] = (0x6 << 4);
  191. /*
  192. * Use OpenFabrics IEEE Company ID: 00 14 05
  193. */
  194. buf[off++] = 0x01;
  195. buf[off++] = 0x40;
  196. buf[off] = (0x5 << 4);
  197. /*
  198. * Return ConfigFS Unit Serial Number information for
  199. * VENDOR_SPECIFIC_IDENTIFIER and
  200. * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
  201. */
  202. spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
  203. len = 20;
  204. off = (len + 4);
  205. check_t10_vend_desc:
  206. /*
  207. * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
  208. */
  209. id_len = 8; /* For Vendor field */
  210. prod_len = 4; /* For VPD Header */
  211. prod_len += 8; /* For Vendor field */
  212. prod_len += strlen(prod);
  213. prod_len++; /* For : */
  214. if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  215. unit_serial_len = strlen(&dev->t10_wwn.unit_serial[0]);
  216. unit_serial_len++; /* For NULL Terminator */
  217. id_len += sprintf(&buf[off+12], "%s:%s", prod,
  218. &dev->t10_wwn.unit_serial[0]);
  219. }
  220. buf[off] = 0x2; /* ASCII */
  221. buf[off+1] = 0x1; /* T10 Vendor ID */
  222. buf[off+2] = 0x0;
  223. memcpy(&buf[off+4], "LIO-ORG", 8);
  224. /* Extra Byte for NULL Terminator */
  225. id_len++;
  226. /* Identifier Length */
  227. buf[off+3] = id_len;
  228. /* Header size for Designation descriptor */
  229. len += (id_len + 4);
  230. off += (id_len + 4);
  231. /*
  232. * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
  233. */
  234. port = lun->lun_sep;
  235. if (port) {
  236. struct t10_alua_lu_gp *lu_gp;
  237. u32 padding, scsi_name_len, scsi_target_len;
  238. u16 lu_gp_id = 0;
  239. u16 tg_pt_gp_id = 0;
  240. u16 tpgt;
  241. tpg = port->sep_tpg;
  242. /*
  243. * Relative target port identifer, see spc4r17
  244. * section 7.7.3.7
  245. *
  246. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  247. * section 7.5.1 Table 362
  248. */
  249. buf[off] =
  250. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  251. buf[off++] |= 0x1; /* CODE SET == Binary */
  252. buf[off] = 0x80; /* Set PIV=1 */
  253. /* Set ASSOCIATION == target port: 01b */
  254. buf[off] |= 0x10;
  255. /* DESIGNATOR TYPE == Relative target port identifer */
  256. buf[off++] |= 0x4;
  257. off++; /* Skip over Reserved */
  258. buf[off++] = 4; /* DESIGNATOR LENGTH */
  259. /* Skip over Obsolete field in RTPI payload
  260. * in Table 472 */
  261. off += 2;
  262. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  263. buf[off++] = (port->sep_rtpi & 0xff);
  264. len += 8; /* Header size + Designation descriptor */
  265. /*
  266. * Target port group identifier, see spc4r17
  267. * section 7.7.3.8
  268. *
  269. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  270. * section 7.5.1 Table 362
  271. */
  272. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  273. if (!tg_pt_gp_mem)
  274. goto check_lu_gp;
  275. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  276. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  277. if (!tg_pt_gp) {
  278. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  279. goto check_lu_gp;
  280. }
  281. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  282. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  283. buf[off] =
  284. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  285. buf[off++] |= 0x1; /* CODE SET == Binary */
  286. buf[off] = 0x80; /* Set PIV=1 */
  287. /* Set ASSOCIATION == target port: 01b */
  288. buf[off] |= 0x10;
  289. /* DESIGNATOR TYPE == Target port group identifier */
  290. buf[off++] |= 0x5;
  291. off++; /* Skip over Reserved */
  292. buf[off++] = 4; /* DESIGNATOR LENGTH */
  293. off += 2; /* Skip over Reserved Field */
  294. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  295. buf[off++] = (tg_pt_gp_id & 0xff);
  296. len += 8; /* Header size + Designation descriptor */
  297. /*
  298. * Logical Unit Group identifier, see spc4r17
  299. * section 7.7.3.8
  300. */
  301. check_lu_gp:
  302. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  303. if (!lu_gp_mem)
  304. goto check_scsi_name;
  305. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  306. lu_gp = lu_gp_mem->lu_gp;
  307. if (!lu_gp) {
  308. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  309. goto check_scsi_name;
  310. }
  311. lu_gp_id = lu_gp->lu_gp_id;
  312. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  313. buf[off++] |= 0x1; /* CODE SET == Binary */
  314. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  315. buf[off++] |= 0x6;
  316. off++; /* Skip over Reserved */
  317. buf[off++] = 4; /* DESIGNATOR LENGTH */
  318. off += 2; /* Skip over Reserved Field */
  319. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  320. buf[off++] = (lu_gp_id & 0xff);
  321. len += 8; /* Header size + Designation descriptor */
  322. /*
  323. * SCSI name string designator, see spc4r17
  324. * section 7.7.3.11
  325. *
  326. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  327. * section 7.5.1 Table 362
  328. */
  329. check_scsi_name:
  330. buf[off] =
  331. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  332. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  333. buf[off] = 0x80; /* Set PIV=1 */
  334. /* Set ASSOCIATION == target port: 01b */
  335. buf[off] |= 0x10;
  336. /* DESIGNATOR TYPE == SCSI name string */
  337. buf[off++] |= 0x8;
  338. off += 2; /* Skip over Reserved and length */
  339. /*
  340. * SCSI name string identifer containing, $FABRIC_MOD
  341. * dependent information. For LIO-Target and iSCSI
  342. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  343. * UTF-8 encoding.
  344. */
  345. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  346. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  347. tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
  348. scsi_name_len += 1 /* Include NULL terminator */;
  349. /*
  350. * The null-terminated, null-padded (see 4.4.2) SCSI
  351. * NAME STRING field contains a UTF-8 format string.
  352. * The number of bytes in the SCSI NAME STRING field
  353. * (i.e., the value in the DESIGNATOR LENGTH field)
  354. * shall be no larger than 256 and shall be a multiple
  355. * of four.
  356. */
  357. padding = ((-scsi_name_len) & 3);
  358. if (padding)
  359. scsi_name_len += padding;
  360. if (scsi_name_len > 256)
  361. scsi_name_len = 256;
  362. buf[off-1] = scsi_name_len;
  363. off += scsi_name_len;
  364. /* Header size + Designation descriptor */
  365. len += (scsi_name_len + 4);
  366. /*
  367. * Target device designator
  368. */
  369. buf[off] =
  370. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  371. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  372. buf[off] = 0x80; /* Set PIV=1 */
  373. /* Set ASSOCIATION == target device: 10b */
  374. buf[off] |= 0x20;
  375. /* DESIGNATOR TYPE == SCSI name string */
  376. buf[off++] |= 0x8;
  377. off += 2; /* Skip over Reserved and length */
  378. /*
  379. * SCSI name string identifer containing, $FABRIC_MOD
  380. * dependent information. For LIO-Target and iSCSI
  381. * Target Port, this means "<iSCSI name>" in
  382. * UTF-8 encoding.
  383. */
  384. scsi_target_len = sprintf(&buf[off], "%s",
  385. tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  386. scsi_target_len += 1 /* Include NULL terminator */;
  387. /*
  388. * The null-terminated, null-padded (see 4.4.2) SCSI
  389. * NAME STRING field contains a UTF-8 format string.
  390. * The number of bytes in the SCSI NAME STRING field
  391. * (i.e., the value in the DESIGNATOR LENGTH field)
  392. * shall be no larger than 256 and shall be a multiple
  393. * of four.
  394. */
  395. padding = ((-scsi_target_len) & 3);
  396. if (padding)
  397. scsi_target_len += padding;
  398. if (scsi_target_len > 256)
  399. scsi_target_len = 256;
  400. buf[off-1] = scsi_target_len;
  401. off += scsi_target_len;
  402. /* Header size + Designation descriptor */
  403. len += (scsi_target_len + 4);
  404. }
  405. buf[2] = ((len >> 8) & 0xff);
  406. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(spc_emulate_evpd_83);
  410. static bool
  411. spc_check_dev_wce(struct se_device *dev)
  412. {
  413. bool wce = false;
  414. if (dev->transport->get_write_cache)
  415. wce = dev->transport->get_write_cache(dev);
  416. else if (dev->dev_attrib.emulate_write_cache > 0)
  417. wce = true;
  418. return wce;
  419. }
  420. /* Extended INQUIRY Data VPD Page */
  421. static sense_reason_t
  422. spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  423. {
  424. struct se_device *dev = cmd->se_dev;
  425. struct se_session *sess = cmd->se_sess;
  426. buf[3] = 0x3c;
  427. /*
  428. * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK
  429. * only for TYPE3 protection.
  430. */
  431. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  432. if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT)
  433. buf[4] = 0x5;
  434. else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT)
  435. buf[4] = 0x4;
  436. }
  437. /* Set HEADSUP, ORDSUP, SIMPSUP */
  438. buf[5] = 0x07;
  439. /* If WriteCache emulation is enabled, set V_SUP */
  440. if (spc_check_dev_wce(dev))
  441. buf[6] = 0x01;
  442. /* If an LBA map is present set R_SUP */
  443. spin_lock(&cmd->se_dev->t10_alua.lba_map_lock);
  444. if (!list_empty(&dev->t10_alua.lba_map_list))
  445. buf[8] = 0x10;
  446. spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock);
  447. return 0;
  448. }
  449. /* Block Limits VPD page */
  450. static sense_reason_t
  451. spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  452. {
  453. struct se_device *dev = cmd->se_dev;
  454. int have_tp = 0;
  455. int opt, min;
  456. /*
  457. * Following spc3r22 section 6.5.3 Block Limits VPD page, when
  458. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  459. * different page length for Thin Provisioning.
  460. */
  461. if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
  462. have_tp = 1;
  463. buf[0] = dev->transport->get_device_type(dev);
  464. buf[3] = have_tp ? 0x3c : 0x10;
  465. /* Set WSNZ to 1 */
  466. buf[4] = 0x01;
  467. /*
  468. * Set MAXIMUM COMPARE AND WRITE LENGTH
  469. */
  470. if (dev->dev_attrib.emulate_caw)
  471. buf[5] = 0x01;
  472. /*
  473. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  474. */
  475. if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev)))
  476. put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]);
  477. else
  478. put_unaligned_be16(1, &buf[6]);
  479. /*
  480. * Set MAXIMUM TRANSFER LENGTH
  481. */
  482. put_unaligned_be32(dev->dev_attrib.hw_max_sectors, &buf[8]);
  483. /*
  484. * Set OPTIMAL TRANSFER LENGTH
  485. */
  486. if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev)))
  487. put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]);
  488. else
  489. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  490. /*
  491. * Exit now if we don't support TP.
  492. */
  493. if (!have_tp)
  494. goto max_write_same;
  495. /*
  496. * Set MAXIMUM UNMAP LBA COUNT
  497. */
  498. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  499. /*
  500. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  501. */
  502. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  503. &buf[24]);
  504. /*
  505. * Set OPTIMAL UNMAP GRANULARITY
  506. */
  507. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  508. /*
  509. * UNMAP GRANULARITY ALIGNMENT
  510. */
  511. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  512. &buf[32]);
  513. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  514. buf[32] |= 0x80; /* Set the UGAVALID bit */
  515. /*
  516. * MAXIMUM WRITE SAME LENGTH
  517. */
  518. max_write_same:
  519. put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
  520. return 0;
  521. }
  522. /* Block Device Characteristics VPD page */
  523. static sense_reason_t
  524. spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  525. {
  526. struct se_device *dev = cmd->se_dev;
  527. buf[0] = dev->transport->get_device_type(dev);
  528. buf[3] = 0x3c;
  529. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  530. return 0;
  531. }
  532. /* Thin Provisioning VPD */
  533. static sense_reason_t
  534. spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  535. {
  536. struct se_device *dev = cmd->se_dev;
  537. /*
  538. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  539. *
  540. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  541. * zero, then the page length shall be set to 0004h. If the DP bit
  542. * is set to one, then the page length shall be set to the value
  543. * defined in table 162.
  544. */
  545. buf[0] = dev->transport->get_device_type(dev);
  546. /*
  547. * Set Hardcoded length mentioned above for DP=0
  548. */
  549. put_unaligned_be16(0x0004, &buf[2]);
  550. /*
  551. * The THRESHOLD EXPONENT field indicates the threshold set size in
  552. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  553. * 2(threshold exponent)).
  554. *
  555. * Note that this is currently set to 0x00 as mkp says it will be
  556. * changing again. We can enable this once it has settled in T10
  557. * and is actually used by Linux/SCSI ML code.
  558. */
  559. buf[4] = 0x00;
  560. /*
  561. * A TPU bit set to one indicates that the device server supports
  562. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  563. * that the device server does not support the UNMAP command.
  564. */
  565. if (dev->dev_attrib.emulate_tpu != 0)
  566. buf[5] = 0x80;
  567. /*
  568. * A TPWS bit set to one indicates that the device server supports
  569. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  570. * A TPWS bit set to zero indicates that the device server does not
  571. * support the use of the WRITE SAME (16) command to unmap LBAs.
  572. */
  573. if (dev->dev_attrib.emulate_tpws != 0)
  574. buf[5] |= 0x40;
  575. return 0;
  576. }
  577. /* Referrals VPD page */
  578. static sense_reason_t
  579. spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf)
  580. {
  581. struct se_device *dev = cmd->se_dev;
  582. buf[0] = dev->transport->get_device_type(dev);
  583. buf[3] = 0x0c;
  584. put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]);
  585. put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]);
  586. return 0;
  587. }
  588. static sense_reason_t
  589. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  590. static struct {
  591. uint8_t page;
  592. sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
  593. } evpd_handlers[] = {
  594. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  595. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  596. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  597. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  598. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  599. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  600. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  601. { .page = 0xb3, .emulate = spc_emulate_evpd_b3 },
  602. };
  603. /* supported vital product data pages */
  604. static sense_reason_t
  605. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  606. {
  607. int p;
  608. /*
  609. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  610. * Registered Extended LUN WWN has been set via ConfigFS
  611. * during device creation/restart.
  612. */
  613. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  614. buf[3] = ARRAY_SIZE(evpd_handlers);
  615. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  616. buf[p + 4] = evpd_handlers[p].page;
  617. }
  618. return 0;
  619. }
  620. static sense_reason_t
  621. spc_emulate_inquiry(struct se_cmd *cmd)
  622. {
  623. struct se_device *dev = cmd->se_dev;
  624. struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
  625. unsigned char *rbuf;
  626. unsigned char *cdb = cmd->t_task_cdb;
  627. unsigned char *buf;
  628. sense_reason_t ret;
  629. int p;
  630. int len = 0;
  631. buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
  632. if (!buf) {
  633. pr_err("Unable to allocate response buffer for INQUIRY\n");
  634. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  635. }
  636. if (dev == tpg->tpg_virt_lun0.lun_se_dev)
  637. buf[0] = 0x3f; /* Not connected */
  638. else
  639. buf[0] = dev->transport->get_device_type(dev);
  640. if (!(cdb[1] & 0x1)) {
  641. if (cdb[2]) {
  642. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  643. cdb[2]);
  644. ret = TCM_INVALID_CDB_FIELD;
  645. goto out;
  646. }
  647. ret = spc_emulate_inquiry_std(cmd, buf);
  648. len = buf[4] + 5;
  649. goto out;
  650. }
  651. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  652. if (cdb[2] == evpd_handlers[p].page) {
  653. buf[1] = cdb[2];
  654. ret = evpd_handlers[p].emulate(cmd, buf);
  655. len = get_unaligned_be16(&buf[2]) + 4;
  656. goto out;
  657. }
  658. }
  659. pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
  660. ret = TCM_INVALID_CDB_FIELD;
  661. out:
  662. rbuf = transport_kmap_data_sg(cmd);
  663. if (rbuf) {
  664. memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length));
  665. transport_kunmap_data_sg(cmd);
  666. }
  667. kfree(buf);
  668. if (!ret)
  669. target_complete_cmd_with_length(cmd, GOOD, len);
  670. return ret;
  671. }
  672. static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p)
  673. {
  674. p[0] = 0x01;
  675. p[1] = 0x0a;
  676. /* No changeable values for now */
  677. if (pc == 1)
  678. goto out;
  679. out:
  680. return 12;
  681. }
  682. static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p)
  683. {
  684. struct se_device *dev = cmd->se_dev;
  685. struct se_session *sess = cmd->se_sess;
  686. p[0] = 0x0a;
  687. p[1] = 0x0a;
  688. /* No changeable values for now */
  689. if (pc == 1)
  690. goto out;
  691. p[2] = 2;
  692. /*
  693. * From spc4r23, 7.4.7 Control mode page
  694. *
  695. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  696. * restrictions on the algorithm used for reordering commands
  697. * having the SIMPLE task attribute (see SAM-4).
  698. *
  699. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  700. * Code Description
  701. * 0h Restricted reordering
  702. * 1h Unrestricted reordering allowed
  703. * 2h to 7h Reserved
  704. * 8h to Fh Vendor specific
  705. *
  706. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  707. * the device server shall order the processing sequence of commands
  708. * having the SIMPLE task attribute such that data integrity is maintained
  709. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  710. * requests is halted at any time, the final value of all data observable
  711. * on the medium shall be the same as if all the commands had been processed
  712. * with the ORDERED task attribute).
  713. *
  714. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  715. * device server may reorder the processing sequence of commands having the
  716. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  717. * command sequence order shall be explicitly handled by the application client
  718. * through the selection of appropriate ommands and task attributes.
  719. */
  720. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  721. /*
  722. * From spc4r17, section 7.4.6 Control mode Page
  723. *
  724. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  725. *
  726. * 00b: The logical unit shall clear any unit attention condition
  727. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  728. * status and shall not establish a unit attention condition when a com-
  729. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  730. * status.
  731. *
  732. * 10b: The logical unit shall not clear any unit attention condition
  733. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  734. * status and shall not establish a unit attention condition when
  735. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  736. * CONFLICT status.
  737. *
  738. * 11b a The logical unit shall not clear any unit attention condition
  739. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  740. * status and shall establish a unit attention condition for the
  741. * initiator port associated with the I_T nexus on which the BUSY,
  742. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  743. * Depending on the status, the additional sense code shall be set to
  744. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  745. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  746. * command, a unit attention condition shall be established only once
  747. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  748. * to the number of commands completed with one of those status codes.
  749. */
  750. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  751. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  752. /*
  753. * From spc4r17, section 7.4.6 Control mode Page
  754. *
  755. * Task Aborted Status (TAS) bit set to zero.
  756. *
  757. * A task aborted status (TAS) bit set to zero specifies that aborted
  758. * tasks shall be terminated by the device server without any response
  759. * to the application client. A TAS bit set to one specifies that tasks
  760. * aborted by the actions of an I_T nexus other than the I_T nexus on
  761. * which the command was received shall be completed with TASK ABORTED
  762. * status (see SAM-4).
  763. */
  764. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  765. /*
  766. * From spc4r30, section 7.5.7 Control mode page
  767. *
  768. * Application Tag Owner (ATO) bit set to one.
  769. *
  770. * If the ATO bit is set to one the device server shall not modify the
  771. * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
  772. * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
  773. * TAG field.
  774. */
  775. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  776. if (dev->dev_attrib.pi_prot_type)
  777. p[5] |= 0x80;
  778. }
  779. p[8] = 0xff;
  780. p[9] = 0xff;
  781. p[11] = 30;
  782. out:
  783. return 12;
  784. }
  785. static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p)
  786. {
  787. struct se_device *dev = cmd->se_dev;
  788. p[0] = 0x08;
  789. p[1] = 0x12;
  790. /* No changeable values for now */
  791. if (pc == 1)
  792. goto out;
  793. if (spc_check_dev_wce(dev))
  794. p[2] = 0x04; /* Write Cache Enable */
  795. p[12] = 0x20; /* Disabled Read Ahead */
  796. out:
  797. return 20;
  798. }
  799. static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p)
  800. {
  801. p[0] = 0x1c;
  802. p[1] = 0x0a;
  803. /* No changeable values for now */
  804. if (pc == 1)
  805. goto out;
  806. out:
  807. return 12;
  808. }
  809. static struct {
  810. uint8_t page;
  811. uint8_t subpage;
  812. int (*emulate)(struct se_cmd *, u8, unsigned char *);
  813. } modesense_handlers[] = {
  814. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  815. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  816. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  817. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  818. };
  819. static void spc_modesense_write_protect(unsigned char *buf, int type)
  820. {
  821. /*
  822. * I believe that the WP bit (bit 7) in the mode header is the same for
  823. * all device types..
  824. */
  825. switch (type) {
  826. case TYPE_DISK:
  827. case TYPE_TAPE:
  828. default:
  829. buf[0] |= 0x80; /* WP bit */
  830. break;
  831. }
  832. }
  833. static void spc_modesense_dpofua(unsigned char *buf, int type)
  834. {
  835. switch (type) {
  836. case TYPE_DISK:
  837. buf[0] |= 0x10; /* DPOFUA bit */
  838. break;
  839. default:
  840. break;
  841. }
  842. }
  843. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  844. {
  845. *buf++ = 8;
  846. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  847. buf += 4;
  848. put_unaligned_be32(block_size, buf);
  849. return 9;
  850. }
  851. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  852. {
  853. if (blocks <= 0xffffffff)
  854. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  855. *buf++ = 1; /* LONGLBA */
  856. buf += 2;
  857. *buf++ = 16;
  858. put_unaligned_be64(blocks, buf);
  859. buf += 12;
  860. put_unaligned_be32(block_size, buf);
  861. return 17;
  862. }
  863. static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
  864. {
  865. struct se_device *dev = cmd->se_dev;
  866. char *cdb = cmd->t_task_cdb;
  867. unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
  868. int type = dev->transport->get_device_type(dev);
  869. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  870. bool dbd = !!(cdb[1] & 0x08);
  871. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  872. u8 pc = cdb[2] >> 6;
  873. u8 page = cdb[2] & 0x3f;
  874. u8 subpage = cdb[3];
  875. int length = 0;
  876. int ret;
  877. int i;
  878. memset(buf, 0, SE_MODE_PAGE_BUF);
  879. /*
  880. * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
  881. * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
  882. */
  883. length = ten ? 3 : 2;
  884. /* DEVICE-SPECIFIC PARAMETER */
  885. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  886. (cmd->se_deve &&
  887. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  888. spc_modesense_write_protect(&buf[length], type);
  889. if ((spc_check_dev_wce(dev)) &&
  890. (dev->dev_attrib.emulate_fua_write > 0))
  891. spc_modesense_dpofua(&buf[length], type);
  892. ++length;
  893. /* BLOCK DESCRIPTOR */
  894. /*
  895. * For now we only include a block descriptor for disk (SBC)
  896. * devices; other command sets use a slightly different format.
  897. */
  898. if (!dbd && type == TYPE_DISK) {
  899. u64 blocks = dev->transport->get_blocks(dev);
  900. u32 block_size = dev->dev_attrib.block_size;
  901. if (ten) {
  902. if (llba) {
  903. length += spc_modesense_long_blockdesc(&buf[length],
  904. blocks, block_size);
  905. } else {
  906. length += 3;
  907. length += spc_modesense_blockdesc(&buf[length],
  908. blocks, block_size);
  909. }
  910. } else {
  911. length += spc_modesense_blockdesc(&buf[length], blocks,
  912. block_size);
  913. }
  914. } else {
  915. if (ten)
  916. length += 4;
  917. else
  918. length += 1;
  919. }
  920. if (page == 0x3f) {
  921. if (subpage != 0x00 && subpage != 0xff) {
  922. pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
  923. return TCM_INVALID_CDB_FIELD;
  924. }
  925. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  926. /*
  927. * Tricky way to say all subpage 00h for
  928. * subpage==0, all subpages for subpage==0xff
  929. * (and we just checked above that those are
  930. * the only two possibilities).
  931. */
  932. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  933. ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  934. if (!ten && length + ret >= 255)
  935. break;
  936. length += ret;
  937. }
  938. }
  939. goto set_length;
  940. }
  941. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  942. if (modesense_handlers[i].page == page &&
  943. modesense_handlers[i].subpage == subpage) {
  944. length += modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  945. goto set_length;
  946. }
  947. /*
  948. * We don't intend to implement:
  949. * - obsolete page 03h "format parameters" (checked by Solaris)
  950. */
  951. if (page != 0x03)
  952. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  953. page, subpage);
  954. return TCM_UNKNOWN_MODE_PAGE;
  955. set_length:
  956. if (ten)
  957. put_unaligned_be16(length - 2, buf);
  958. else
  959. buf[0] = length - 1;
  960. rbuf = transport_kmap_data_sg(cmd);
  961. if (rbuf) {
  962. memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
  963. transport_kunmap_data_sg(cmd);
  964. }
  965. target_complete_cmd_with_length(cmd, GOOD, length);
  966. return 0;
  967. }
  968. static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
  969. {
  970. char *cdb = cmd->t_task_cdb;
  971. bool ten = cdb[0] == MODE_SELECT_10;
  972. int off = ten ? 8 : 4;
  973. bool pf = !!(cdb[1] & 0x10);
  974. u8 page, subpage;
  975. unsigned char *buf;
  976. unsigned char tbuf[SE_MODE_PAGE_BUF];
  977. int length;
  978. int ret = 0;
  979. int i;
  980. if (!cmd->data_length) {
  981. target_complete_cmd(cmd, GOOD);
  982. return 0;
  983. }
  984. if (cmd->data_length < off + 2)
  985. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  986. buf = transport_kmap_data_sg(cmd);
  987. if (!buf)
  988. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  989. if (!pf) {
  990. ret = TCM_INVALID_CDB_FIELD;
  991. goto out;
  992. }
  993. page = buf[off] & 0x3f;
  994. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  995. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  996. if (modesense_handlers[i].page == page &&
  997. modesense_handlers[i].subpage == subpage) {
  998. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  999. length = modesense_handlers[i].emulate(cmd, 0, tbuf);
  1000. goto check_contents;
  1001. }
  1002. ret = TCM_UNKNOWN_MODE_PAGE;
  1003. goto out;
  1004. check_contents:
  1005. if (cmd->data_length < off + length) {
  1006. ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
  1007. goto out;
  1008. }
  1009. if (memcmp(buf + off, tbuf, length))
  1010. ret = TCM_INVALID_PARAMETER_LIST;
  1011. out:
  1012. transport_kunmap_data_sg(cmd);
  1013. if (!ret)
  1014. target_complete_cmd(cmd, GOOD);
  1015. return ret;
  1016. }
  1017. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  1018. {
  1019. unsigned char *cdb = cmd->t_task_cdb;
  1020. unsigned char *rbuf;
  1021. u8 ua_asc = 0, ua_ascq = 0;
  1022. unsigned char buf[SE_SENSE_BUF];
  1023. memset(buf, 0, SE_SENSE_BUF);
  1024. if (cdb[1] & 0x01) {
  1025. pr_err("REQUEST_SENSE description emulation not"
  1026. " supported\n");
  1027. return TCM_INVALID_CDB_FIELD;
  1028. }
  1029. rbuf = transport_kmap_data_sg(cmd);
  1030. if (!rbuf)
  1031. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1032. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  1033. /*
  1034. * CURRENT ERROR, UNIT ATTENTION
  1035. */
  1036. buf[0] = 0x70;
  1037. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  1038. /*
  1039. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  1040. */
  1041. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  1042. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  1043. buf[7] = 0x0A;
  1044. } else {
  1045. /*
  1046. * CURRENT ERROR, NO SENSE
  1047. */
  1048. buf[0] = 0x70;
  1049. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  1050. /*
  1051. * NO ADDITIONAL SENSE INFORMATION
  1052. */
  1053. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  1054. buf[7] = 0x0A;
  1055. }
  1056. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  1057. transport_kunmap_data_sg(cmd);
  1058. target_complete_cmd(cmd, GOOD);
  1059. return 0;
  1060. }
  1061. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  1062. {
  1063. struct se_dev_entry *deve;
  1064. struct se_session *sess = cmd->se_sess;
  1065. unsigned char *buf;
  1066. u32 lun_count = 0, offset = 8, i;
  1067. if (cmd->data_length < 16) {
  1068. pr_warn("REPORT LUNS allocation length %u too small\n",
  1069. cmd->data_length);
  1070. return TCM_INVALID_CDB_FIELD;
  1071. }
  1072. buf = transport_kmap_data_sg(cmd);
  1073. if (!buf)
  1074. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1075. /*
  1076. * If no struct se_session pointer is present, this struct se_cmd is
  1077. * coming via a target_core_mod PASSTHROUGH op, and not through
  1078. * a $FABRIC_MOD. In that case, report LUN=0 only.
  1079. */
  1080. if (!sess) {
  1081. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  1082. lun_count = 1;
  1083. goto done;
  1084. }
  1085. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  1086. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1087. deve = sess->se_node_acl->device_list[i];
  1088. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  1089. continue;
  1090. /*
  1091. * We determine the correct LUN LIST LENGTH even once we
  1092. * have reached the initial allocation length.
  1093. * See SPC2-R20 7.19.
  1094. */
  1095. lun_count++;
  1096. if ((offset + 8) > cmd->data_length)
  1097. continue;
  1098. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  1099. offset += 8;
  1100. }
  1101. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  1102. /*
  1103. * See SPC3 r07, page 159.
  1104. */
  1105. done:
  1106. lun_count *= 8;
  1107. buf[0] = ((lun_count >> 24) & 0xff);
  1108. buf[1] = ((lun_count >> 16) & 0xff);
  1109. buf[2] = ((lun_count >> 8) & 0xff);
  1110. buf[3] = (lun_count & 0xff);
  1111. transport_kunmap_data_sg(cmd);
  1112. target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
  1113. return 0;
  1114. }
  1115. EXPORT_SYMBOL(spc_emulate_report_luns);
  1116. static sense_reason_t
  1117. spc_emulate_testunitready(struct se_cmd *cmd)
  1118. {
  1119. target_complete_cmd(cmd, GOOD);
  1120. return 0;
  1121. }
  1122. sense_reason_t
  1123. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  1124. {
  1125. struct se_device *dev = cmd->se_dev;
  1126. unsigned char *cdb = cmd->t_task_cdb;
  1127. switch (cdb[0]) {
  1128. case MODE_SELECT:
  1129. *size = cdb[4];
  1130. cmd->execute_cmd = spc_emulate_modeselect;
  1131. break;
  1132. case MODE_SELECT_10:
  1133. *size = (cdb[7] << 8) + cdb[8];
  1134. cmd->execute_cmd = spc_emulate_modeselect;
  1135. break;
  1136. case MODE_SENSE:
  1137. *size = cdb[4];
  1138. cmd->execute_cmd = spc_emulate_modesense;
  1139. break;
  1140. case MODE_SENSE_10:
  1141. *size = (cdb[7] << 8) + cdb[8];
  1142. cmd->execute_cmd = spc_emulate_modesense;
  1143. break;
  1144. case LOG_SELECT:
  1145. case LOG_SENSE:
  1146. *size = (cdb[7] << 8) + cdb[8];
  1147. break;
  1148. case PERSISTENT_RESERVE_IN:
  1149. *size = (cdb[7] << 8) + cdb[8];
  1150. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1151. break;
  1152. case PERSISTENT_RESERVE_OUT:
  1153. *size = (cdb[7] << 8) + cdb[8];
  1154. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1155. break;
  1156. case RELEASE:
  1157. case RELEASE_10:
  1158. if (cdb[0] == RELEASE_10)
  1159. *size = (cdb[7] << 8) | cdb[8];
  1160. else
  1161. *size = cmd->data_length;
  1162. cmd->execute_cmd = target_scsi2_reservation_release;
  1163. break;
  1164. case RESERVE:
  1165. case RESERVE_10:
  1166. /*
  1167. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1168. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1169. */
  1170. if (cdb[0] == RESERVE_10)
  1171. *size = (cdb[7] << 8) | cdb[8];
  1172. else
  1173. *size = cmd->data_length;
  1174. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1175. break;
  1176. case REQUEST_SENSE:
  1177. *size = cdb[4];
  1178. cmd->execute_cmd = spc_emulate_request_sense;
  1179. break;
  1180. case INQUIRY:
  1181. *size = (cdb[3] << 8) + cdb[4];
  1182. /*
  1183. * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
  1184. * See spc4r17 section 5.3
  1185. */
  1186. cmd->sam_task_attr = TCM_HEAD_TAG;
  1187. cmd->execute_cmd = spc_emulate_inquiry;
  1188. break;
  1189. case SECURITY_PROTOCOL_IN:
  1190. case SECURITY_PROTOCOL_OUT:
  1191. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1192. break;
  1193. case EXTENDED_COPY:
  1194. *size = get_unaligned_be32(&cdb[10]);
  1195. cmd->execute_cmd = target_do_xcopy;
  1196. break;
  1197. case RECEIVE_COPY_RESULTS:
  1198. *size = get_unaligned_be32(&cdb[10]);
  1199. cmd->execute_cmd = target_do_receive_copy_results;
  1200. break;
  1201. case READ_ATTRIBUTE:
  1202. case WRITE_ATTRIBUTE:
  1203. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1204. (cdb[12] << 8) | cdb[13];
  1205. break;
  1206. case RECEIVE_DIAGNOSTIC:
  1207. case SEND_DIAGNOSTIC:
  1208. *size = (cdb[3] << 8) | cdb[4];
  1209. break;
  1210. case WRITE_BUFFER:
  1211. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1212. break;
  1213. case REPORT_LUNS:
  1214. cmd->execute_cmd = spc_emulate_report_luns;
  1215. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1216. /*
  1217. * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
  1218. * See spc4r17 section 5.3
  1219. */
  1220. cmd->sam_task_attr = TCM_HEAD_TAG;
  1221. break;
  1222. case TEST_UNIT_READY:
  1223. cmd->execute_cmd = spc_emulate_testunitready;
  1224. *size = 0;
  1225. break;
  1226. case MAINTENANCE_IN:
  1227. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1228. /*
  1229. * MAINTENANCE_IN from SCC-2
  1230. * Check for emulated MI_REPORT_TARGET_PGS
  1231. */
  1232. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1233. cmd->execute_cmd =
  1234. target_emulate_report_target_port_groups;
  1235. }
  1236. *size = get_unaligned_be32(&cdb[6]);
  1237. } else {
  1238. /*
  1239. * GPCMD_SEND_KEY from multi media commands
  1240. */
  1241. *size = get_unaligned_be16(&cdb[8]);
  1242. }
  1243. break;
  1244. case MAINTENANCE_OUT:
  1245. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1246. /*
  1247. * MAINTENANCE_OUT from SCC-2
  1248. * Check for emulated MO_SET_TARGET_PGS.
  1249. */
  1250. if (cdb[1] == MO_SET_TARGET_PGS) {
  1251. cmd->execute_cmd =
  1252. target_emulate_set_target_port_groups;
  1253. }
  1254. *size = get_unaligned_be32(&cdb[6]);
  1255. } else {
  1256. /*
  1257. * GPCMD_SEND_KEY from multi media commands
  1258. */
  1259. *size = get_unaligned_be16(&cdb[8]);
  1260. }
  1261. break;
  1262. default:
  1263. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1264. " 0x%02x, sending CHECK_CONDITION.\n",
  1265. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1266. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1267. }
  1268. return 0;
  1269. }
  1270. EXPORT_SYMBOL(spc_parse_cdb);