target_core_spc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. u32 max_sectors;
  455. int have_tp = 0;
  456. int opt, min;
  457. /*
  458. * Following spc3r22 section 6.5.3 Block Limits VPD page, when
  459. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  460. * different page length for Thin Provisioning.
  461. */
  462. if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
  463. have_tp = 1;
  464. buf[0] = dev->transport->get_device_type(dev);
  465. buf[3] = have_tp ? 0x3c : 0x10;
  466. /* Set WSNZ to 1 */
  467. buf[4] = 0x01;
  468. /*
  469. * Set MAXIMUM COMPARE AND WRITE LENGTH
  470. */
  471. if (dev->dev_attrib.emulate_caw)
  472. buf[5] = 0x01;
  473. /*
  474. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  475. */
  476. if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev)))
  477. put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]);
  478. else
  479. put_unaligned_be16(1, &buf[6]);
  480. /*
  481. * Set MAXIMUM TRANSFER LENGTH
  482. */
  483. max_sectors = min(dev->dev_attrib.fabric_max_sectors,
  484. dev->dev_attrib.hw_max_sectors);
  485. put_unaligned_be32(max_sectors, &buf[8]);
  486. /*
  487. * Set OPTIMAL TRANSFER LENGTH
  488. */
  489. if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev)))
  490. put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]);
  491. else
  492. put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]);
  493. /*
  494. * Exit now if we don't support TP.
  495. */
  496. if (!have_tp)
  497. goto max_write_same;
  498. /*
  499. * Set MAXIMUM UNMAP LBA COUNT
  500. */
  501. put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]);
  502. /*
  503. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  504. */
  505. put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count,
  506. &buf[24]);
  507. /*
  508. * Set OPTIMAL UNMAP GRANULARITY
  509. */
  510. put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]);
  511. /*
  512. * UNMAP GRANULARITY ALIGNMENT
  513. */
  514. put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment,
  515. &buf[32]);
  516. if (dev->dev_attrib.unmap_granularity_alignment != 0)
  517. buf[32] |= 0x80; /* Set the UGAVALID bit */
  518. /*
  519. * MAXIMUM WRITE SAME LENGTH
  520. */
  521. max_write_same:
  522. put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]);
  523. return 0;
  524. }
  525. /* Block Device Characteristics VPD page */
  526. static sense_reason_t
  527. spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  528. {
  529. struct se_device *dev = cmd->se_dev;
  530. buf[0] = dev->transport->get_device_type(dev);
  531. buf[3] = 0x3c;
  532. buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0;
  533. return 0;
  534. }
  535. /* Thin Provisioning VPD */
  536. static sense_reason_t
  537. spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  538. {
  539. struct se_device *dev = cmd->se_dev;
  540. /*
  541. * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
  542. *
  543. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  544. * zero, then the page length shall be set to 0004h. If the DP bit
  545. * is set to one, then the page length shall be set to the value
  546. * defined in table 162.
  547. */
  548. buf[0] = dev->transport->get_device_type(dev);
  549. /*
  550. * Set Hardcoded length mentioned above for DP=0
  551. */
  552. put_unaligned_be16(0x0004, &buf[2]);
  553. /*
  554. * The THRESHOLD EXPONENT field indicates the threshold set size in
  555. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  556. * 2(threshold exponent)).
  557. *
  558. * Note that this is currently set to 0x00 as mkp says it will be
  559. * changing again. We can enable this once it has settled in T10
  560. * and is actually used by Linux/SCSI ML code.
  561. */
  562. buf[4] = 0x00;
  563. /*
  564. * A TPU bit set to one indicates that the device server supports
  565. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  566. * that the device server does not support the UNMAP command.
  567. */
  568. if (dev->dev_attrib.emulate_tpu != 0)
  569. buf[5] = 0x80;
  570. /*
  571. * A TPWS bit set to one indicates that the device server supports
  572. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  573. * A TPWS bit set to zero indicates that the device server does not
  574. * support the use of the WRITE SAME (16) command to unmap LBAs.
  575. */
  576. if (dev->dev_attrib.emulate_tpws != 0)
  577. buf[5] |= 0x40;
  578. return 0;
  579. }
  580. /* Referrals VPD page */
  581. static sense_reason_t
  582. spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf)
  583. {
  584. struct se_device *dev = cmd->se_dev;
  585. buf[0] = dev->transport->get_device_type(dev);
  586. buf[3] = 0x0c;
  587. put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]);
  588. put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[12]);
  589. return 0;
  590. }
  591. static sense_reason_t
  592. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  593. static struct {
  594. uint8_t page;
  595. sense_reason_t (*emulate)(struct se_cmd *, unsigned char *);
  596. } evpd_handlers[] = {
  597. { .page = 0x00, .emulate = spc_emulate_evpd_00 },
  598. { .page = 0x80, .emulate = spc_emulate_evpd_80 },
  599. { .page = 0x83, .emulate = spc_emulate_evpd_83 },
  600. { .page = 0x86, .emulate = spc_emulate_evpd_86 },
  601. { .page = 0xb0, .emulate = spc_emulate_evpd_b0 },
  602. { .page = 0xb1, .emulate = spc_emulate_evpd_b1 },
  603. { .page = 0xb2, .emulate = spc_emulate_evpd_b2 },
  604. { .page = 0xb3, .emulate = spc_emulate_evpd_b3 },
  605. };
  606. /* supported vital product data pages */
  607. static sense_reason_t
  608. spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  609. {
  610. int p;
  611. /*
  612. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  613. * Registered Extended LUN WWN has been set via ConfigFS
  614. * during device creation/restart.
  615. */
  616. if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) {
  617. buf[3] = ARRAY_SIZE(evpd_handlers);
  618. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  619. buf[p + 4] = evpd_handlers[p].page;
  620. }
  621. return 0;
  622. }
  623. static sense_reason_t
  624. spc_emulate_inquiry(struct se_cmd *cmd)
  625. {
  626. struct se_device *dev = cmd->se_dev;
  627. struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg;
  628. unsigned char *rbuf;
  629. unsigned char *cdb = cmd->t_task_cdb;
  630. unsigned char *buf;
  631. sense_reason_t ret;
  632. int p;
  633. int len = 0;
  634. buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL);
  635. if (!buf) {
  636. pr_err("Unable to allocate response buffer for INQUIRY\n");
  637. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  638. }
  639. if (dev == tpg->tpg_virt_lun0.lun_se_dev)
  640. buf[0] = 0x3f; /* Not connected */
  641. else
  642. buf[0] = dev->transport->get_device_type(dev);
  643. if (!(cdb[1] & 0x1)) {
  644. if (cdb[2]) {
  645. pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
  646. cdb[2]);
  647. ret = TCM_INVALID_CDB_FIELD;
  648. goto out;
  649. }
  650. ret = spc_emulate_inquiry_std(cmd, buf);
  651. len = buf[4] + 5;
  652. goto out;
  653. }
  654. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
  655. if (cdb[2] == evpd_handlers[p].page) {
  656. buf[1] = cdb[2];
  657. ret = evpd_handlers[p].emulate(cmd, buf);
  658. len = get_unaligned_be16(&buf[2]) + 4;
  659. goto out;
  660. }
  661. }
  662. pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
  663. ret = TCM_INVALID_CDB_FIELD;
  664. out:
  665. rbuf = transport_kmap_data_sg(cmd);
  666. if (rbuf) {
  667. memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length));
  668. transport_kunmap_data_sg(cmd);
  669. }
  670. kfree(buf);
  671. if (!ret)
  672. target_complete_cmd_with_length(cmd, GOOD, len);
  673. return ret;
  674. }
  675. static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p)
  676. {
  677. p[0] = 0x01;
  678. p[1] = 0x0a;
  679. /* No changeable values for now */
  680. if (pc == 1)
  681. goto out;
  682. out:
  683. return 12;
  684. }
  685. static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p)
  686. {
  687. struct se_device *dev = cmd->se_dev;
  688. struct se_session *sess = cmd->se_sess;
  689. p[0] = 0x0a;
  690. p[1] = 0x0a;
  691. /* No changeable values for now */
  692. if (pc == 1)
  693. goto out;
  694. p[2] = 2;
  695. /*
  696. * From spc4r23, 7.4.7 Control mode page
  697. *
  698. * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
  699. * restrictions on the algorithm used for reordering commands
  700. * having the SIMPLE task attribute (see SAM-4).
  701. *
  702. * Table 368 -- QUEUE ALGORITHM MODIFIER field
  703. * Code Description
  704. * 0h Restricted reordering
  705. * 1h Unrestricted reordering allowed
  706. * 2h to 7h Reserved
  707. * 8h to Fh Vendor specific
  708. *
  709. * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
  710. * the device server shall order the processing sequence of commands
  711. * having the SIMPLE task attribute such that data integrity is maintained
  712. * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
  713. * requests is halted at any time, the final value of all data observable
  714. * on the medium shall be the same as if all the commands had been processed
  715. * with the ORDERED task attribute).
  716. *
  717. * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
  718. * device server may reorder the processing sequence of commands having the
  719. * SIMPLE task attribute in any manner. Any data integrity exposures related to
  720. * command sequence order shall be explicitly handled by the application client
  721. * through the selection of appropriate ommands and task attributes.
  722. */
  723. p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
  724. /*
  725. * From spc4r17, section 7.4.6 Control mode Page
  726. *
  727. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  728. *
  729. * 00b: The logical unit shall clear any unit attention condition
  730. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  731. * status and shall not establish a unit attention condition when a com-
  732. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  733. * status.
  734. *
  735. * 10b: The logical unit shall not clear any unit attention condition
  736. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  737. * status and shall not establish a unit attention condition when
  738. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  739. * CONFLICT status.
  740. *
  741. * 11b a The logical unit shall not clear any unit attention condition
  742. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  743. * status and shall establish a unit attention condition for the
  744. * initiator port associated with the I_T nexus on which the BUSY,
  745. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  746. * Depending on the status, the additional sense code shall be set to
  747. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  748. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  749. * command, a unit attention condition shall be established only once
  750. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  751. * to the number of commands completed with one of those status codes.
  752. */
  753. p[4] = (dev->dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  754. (dev->dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  755. /*
  756. * From spc4r17, section 7.4.6 Control mode Page
  757. *
  758. * Task Aborted Status (TAS) bit set to zero.
  759. *
  760. * A task aborted status (TAS) bit set to zero specifies that aborted
  761. * tasks shall be terminated by the device server without any response
  762. * to the application client. A TAS bit set to one specifies that tasks
  763. * aborted by the actions of an I_T nexus other than the I_T nexus on
  764. * which the command was received shall be completed with TASK ABORTED
  765. * status (see SAM-4).
  766. */
  767. p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00;
  768. /*
  769. * From spc4r30, section 7.5.7 Control mode page
  770. *
  771. * Application Tag Owner (ATO) bit set to one.
  772. *
  773. * If the ATO bit is set to one the device server shall not modify the
  774. * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
  775. * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
  776. * TAG field.
  777. */
  778. if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) {
  779. if (dev->dev_attrib.pi_prot_type)
  780. p[5] |= 0x80;
  781. }
  782. p[8] = 0xff;
  783. p[9] = 0xff;
  784. p[11] = 30;
  785. out:
  786. return 12;
  787. }
  788. static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p)
  789. {
  790. struct se_device *dev = cmd->se_dev;
  791. p[0] = 0x08;
  792. p[1] = 0x12;
  793. /* No changeable values for now */
  794. if (pc == 1)
  795. goto out;
  796. if (spc_check_dev_wce(dev))
  797. p[2] = 0x04; /* Write Cache Enable */
  798. p[12] = 0x20; /* Disabled Read Ahead */
  799. out:
  800. return 20;
  801. }
  802. static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p)
  803. {
  804. p[0] = 0x1c;
  805. p[1] = 0x0a;
  806. /* No changeable values for now */
  807. if (pc == 1)
  808. goto out;
  809. out:
  810. return 12;
  811. }
  812. static struct {
  813. uint8_t page;
  814. uint8_t subpage;
  815. int (*emulate)(struct se_cmd *, u8, unsigned char *);
  816. } modesense_handlers[] = {
  817. { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery },
  818. { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching },
  819. { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control },
  820. { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions },
  821. };
  822. static void spc_modesense_write_protect(unsigned char *buf, int type)
  823. {
  824. /*
  825. * I believe that the WP bit (bit 7) in the mode header is the same for
  826. * all device types..
  827. */
  828. switch (type) {
  829. case TYPE_DISK:
  830. case TYPE_TAPE:
  831. default:
  832. buf[0] |= 0x80; /* WP bit */
  833. break;
  834. }
  835. }
  836. static void spc_modesense_dpofua(unsigned char *buf, int type)
  837. {
  838. switch (type) {
  839. case TYPE_DISK:
  840. buf[0] |= 0x10; /* DPOFUA bit */
  841. break;
  842. default:
  843. break;
  844. }
  845. }
  846. static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  847. {
  848. *buf++ = 8;
  849. put_unaligned_be32(min(blocks, 0xffffffffull), buf);
  850. buf += 4;
  851. put_unaligned_be32(block_size, buf);
  852. return 9;
  853. }
  854. static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size)
  855. {
  856. if (blocks <= 0xffffffff)
  857. return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3;
  858. *buf++ = 1; /* LONGLBA */
  859. buf += 2;
  860. *buf++ = 16;
  861. put_unaligned_be64(blocks, buf);
  862. buf += 12;
  863. put_unaligned_be32(block_size, buf);
  864. return 17;
  865. }
  866. static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
  867. {
  868. struct se_device *dev = cmd->se_dev;
  869. char *cdb = cmd->t_task_cdb;
  870. unsigned char buf[SE_MODE_PAGE_BUF], *rbuf;
  871. int type = dev->transport->get_device_type(dev);
  872. int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
  873. bool dbd = !!(cdb[1] & 0x08);
  874. bool llba = ten ? !!(cdb[1] & 0x10) : false;
  875. u8 pc = cdb[2] >> 6;
  876. u8 page = cdb[2] & 0x3f;
  877. u8 subpage = cdb[3];
  878. int length = 0;
  879. int ret;
  880. int i;
  881. memset(buf, 0, SE_MODE_PAGE_BUF);
  882. /*
  883. * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
  884. * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
  885. */
  886. length = ten ? 3 : 2;
  887. /* DEVICE-SPECIFIC PARAMETER */
  888. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  889. (cmd->se_deve &&
  890. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  891. spc_modesense_write_protect(&buf[length], type);
  892. if ((spc_check_dev_wce(dev)) &&
  893. (dev->dev_attrib.emulate_fua_write > 0))
  894. spc_modesense_dpofua(&buf[length], type);
  895. ++length;
  896. /* BLOCK DESCRIPTOR */
  897. /*
  898. * For now we only include a block descriptor for disk (SBC)
  899. * devices; other command sets use a slightly different format.
  900. */
  901. if (!dbd && type == TYPE_DISK) {
  902. u64 blocks = dev->transport->get_blocks(dev);
  903. u32 block_size = dev->dev_attrib.block_size;
  904. if (ten) {
  905. if (llba) {
  906. length += spc_modesense_long_blockdesc(&buf[length],
  907. blocks, block_size);
  908. } else {
  909. length += 3;
  910. length += spc_modesense_blockdesc(&buf[length],
  911. blocks, block_size);
  912. }
  913. } else {
  914. length += spc_modesense_blockdesc(&buf[length], blocks,
  915. block_size);
  916. }
  917. } else {
  918. if (ten)
  919. length += 4;
  920. else
  921. length += 1;
  922. }
  923. if (page == 0x3f) {
  924. if (subpage != 0x00 && subpage != 0xff) {
  925. pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage);
  926. return TCM_INVALID_CDB_FIELD;
  927. }
  928. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) {
  929. /*
  930. * Tricky way to say all subpage 00h for
  931. * subpage==0, all subpages for subpage==0xff
  932. * (and we just checked above that those are
  933. * the only two possibilities).
  934. */
  935. if ((modesense_handlers[i].subpage & ~subpage) == 0) {
  936. ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  937. if (!ten && length + ret >= 255)
  938. break;
  939. length += ret;
  940. }
  941. }
  942. goto set_length;
  943. }
  944. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  945. if (modesense_handlers[i].page == page &&
  946. modesense_handlers[i].subpage == subpage) {
  947. length += modesense_handlers[i].emulate(cmd, pc, &buf[length]);
  948. goto set_length;
  949. }
  950. /*
  951. * We don't intend to implement:
  952. * - obsolete page 03h "format parameters" (checked by Solaris)
  953. */
  954. if (page != 0x03)
  955. pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
  956. page, subpage);
  957. return TCM_UNKNOWN_MODE_PAGE;
  958. set_length:
  959. if (ten)
  960. put_unaligned_be16(length - 2, buf);
  961. else
  962. buf[0] = length - 1;
  963. rbuf = transport_kmap_data_sg(cmd);
  964. if (rbuf) {
  965. memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length));
  966. transport_kunmap_data_sg(cmd);
  967. }
  968. target_complete_cmd_with_length(cmd, GOOD, length);
  969. return 0;
  970. }
  971. static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd)
  972. {
  973. char *cdb = cmd->t_task_cdb;
  974. bool ten = cdb[0] == MODE_SELECT_10;
  975. int off = ten ? 8 : 4;
  976. bool pf = !!(cdb[1] & 0x10);
  977. u8 page, subpage;
  978. unsigned char *buf;
  979. unsigned char tbuf[SE_MODE_PAGE_BUF];
  980. int length;
  981. int ret = 0;
  982. int i;
  983. if (!cmd->data_length) {
  984. target_complete_cmd(cmd, GOOD);
  985. return 0;
  986. }
  987. if (cmd->data_length < off + 2)
  988. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  989. buf = transport_kmap_data_sg(cmd);
  990. if (!buf)
  991. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  992. if (!pf) {
  993. ret = TCM_INVALID_CDB_FIELD;
  994. goto out;
  995. }
  996. page = buf[off] & 0x3f;
  997. subpage = buf[off] & 0x40 ? buf[off + 1] : 0;
  998. for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i)
  999. if (modesense_handlers[i].page == page &&
  1000. modesense_handlers[i].subpage == subpage) {
  1001. memset(tbuf, 0, SE_MODE_PAGE_BUF);
  1002. length = modesense_handlers[i].emulate(cmd, 0, tbuf);
  1003. goto check_contents;
  1004. }
  1005. ret = TCM_UNKNOWN_MODE_PAGE;
  1006. goto out;
  1007. check_contents:
  1008. if (cmd->data_length < off + length) {
  1009. ret = TCM_PARAMETER_LIST_LENGTH_ERROR;
  1010. goto out;
  1011. }
  1012. if (memcmp(buf + off, tbuf, length))
  1013. ret = TCM_INVALID_PARAMETER_LIST;
  1014. out:
  1015. transport_kunmap_data_sg(cmd);
  1016. if (!ret)
  1017. target_complete_cmd(cmd, GOOD);
  1018. return ret;
  1019. }
  1020. static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd)
  1021. {
  1022. unsigned char *cdb = cmd->t_task_cdb;
  1023. unsigned char *rbuf;
  1024. u8 ua_asc = 0, ua_ascq = 0;
  1025. unsigned char buf[SE_SENSE_BUF];
  1026. memset(buf, 0, SE_SENSE_BUF);
  1027. if (cdb[1] & 0x01) {
  1028. pr_err("REQUEST_SENSE description emulation not"
  1029. " supported\n");
  1030. return TCM_INVALID_CDB_FIELD;
  1031. }
  1032. rbuf = transport_kmap_data_sg(cmd);
  1033. if (!rbuf)
  1034. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1035. if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
  1036. /*
  1037. * CURRENT ERROR, UNIT ATTENTION
  1038. */
  1039. buf[0] = 0x70;
  1040. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  1041. /*
  1042. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  1043. */
  1044. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  1045. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  1046. buf[7] = 0x0A;
  1047. } else {
  1048. /*
  1049. * CURRENT ERROR, NO SENSE
  1050. */
  1051. buf[0] = 0x70;
  1052. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  1053. /*
  1054. * NO ADDITIONAL SENSE INFORMATION
  1055. */
  1056. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  1057. buf[7] = 0x0A;
  1058. }
  1059. memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
  1060. transport_kunmap_data_sg(cmd);
  1061. target_complete_cmd(cmd, GOOD);
  1062. return 0;
  1063. }
  1064. sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd)
  1065. {
  1066. struct se_dev_entry *deve;
  1067. struct se_session *sess = cmd->se_sess;
  1068. unsigned char *buf;
  1069. u32 lun_count = 0, offset = 8, i;
  1070. if (cmd->data_length < 16) {
  1071. pr_warn("REPORT LUNS allocation length %u too small\n",
  1072. cmd->data_length);
  1073. return TCM_INVALID_CDB_FIELD;
  1074. }
  1075. buf = transport_kmap_data_sg(cmd);
  1076. if (!buf)
  1077. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1078. /*
  1079. * If no struct se_session pointer is present, this struct se_cmd is
  1080. * coming via a target_core_mod PASSTHROUGH op, and not through
  1081. * a $FABRIC_MOD. In that case, report LUN=0 only.
  1082. */
  1083. if (!sess) {
  1084. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  1085. lun_count = 1;
  1086. goto done;
  1087. }
  1088. spin_lock_irq(&sess->se_node_acl->device_list_lock);
  1089. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1090. deve = sess->se_node_acl->device_list[i];
  1091. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  1092. continue;
  1093. /*
  1094. * We determine the correct LUN LIST LENGTH even once we
  1095. * have reached the initial allocation length.
  1096. * See SPC2-R20 7.19.
  1097. */
  1098. lun_count++;
  1099. if ((offset + 8) > cmd->data_length)
  1100. continue;
  1101. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  1102. offset += 8;
  1103. }
  1104. spin_unlock_irq(&sess->se_node_acl->device_list_lock);
  1105. /*
  1106. * See SPC3 r07, page 159.
  1107. */
  1108. done:
  1109. lun_count *= 8;
  1110. buf[0] = ((lun_count >> 24) & 0xff);
  1111. buf[1] = ((lun_count >> 16) & 0xff);
  1112. buf[2] = ((lun_count >> 8) & 0xff);
  1113. buf[3] = (lun_count & 0xff);
  1114. transport_kunmap_data_sg(cmd);
  1115. target_complete_cmd_with_length(cmd, GOOD, 8 + lun_count * 8);
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL(spc_emulate_report_luns);
  1119. static sense_reason_t
  1120. spc_emulate_testunitready(struct se_cmd *cmd)
  1121. {
  1122. target_complete_cmd(cmd, GOOD);
  1123. return 0;
  1124. }
  1125. sense_reason_t
  1126. spc_parse_cdb(struct se_cmd *cmd, unsigned int *size)
  1127. {
  1128. struct se_device *dev = cmd->se_dev;
  1129. unsigned char *cdb = cmd->t_task_cdb;
  1130. switch (cdb[0]) {
  1131. case MODE_SELECT:
  1132. *size = cdb[4];
  1133. cmd->execute_cmd = spc_emulate_modeselect;
  1134. break;
  1135. case MODE_SELECT_10:
  1136. *size = (cdb[7] << 8) + cdb[8];
  1137. cmd->execute_cmd = spc_emulate_modeselect;
  1138. break;
  1139. case MODE_SENSE:
  1140. *size = cdb[4];
  1141. cmd->execute_cmd = spc_emulate_modesense;
  1142. break;
  1143. case MODE_SENSE_10:
  1144. *size = (cdb[7] << 8) + cdb[8];
  1145. cmd->execute_cmd = spc_emulate_modesense;
  1146. break;
  1147. case LOG_SELECT:
  1148. case LOG_SENSE:
  1149. *size = (cdb[7] << 8) + cdb[8];
  1150. break;
  1151. case PERSISTENT_RESERVE_IN:
  1152. *size = (cdb[7] << 8) + cdb[8];
  1153. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1154. break;
  1155. case PERSISTENT_RESERVE_OUT:
  1156. *size = (cdb[7] << 8) + cdb[8];
  1157. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1158. break;
  1159. case RELEASE:
  1160. case RELEASE_10:
  1161. if (cdb[0] == RELEASE_10)
  1162. *size = (cdb[7] << 8) | cdb[8];
  1163. else
  1164. *size = cmd->data_length;
  1165. cmd->execute_cmd = target_scsi2_reservation_release;
  1166. break;
  1167. case RESERVE:
  1168. case RESERVE_10:
  1169. /*
  1170. * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
  1171. * Assume the passthrough or $FABRIC_MOD will tell us about it.
  1172. */
  1173. if (cdb[0] == RESERVE_10)
  1174. *size = (cdb[7] << 8) | cdb[8];
  1175. else
  1176. *size = cmd->data_length;
  1177. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1178. break;
  1179. case REQUEST_SENSE:
  1180. *size = cdb[4];
  1181. cmd->execute_cmd = spc_emulate_request_sense;
  1182. break;
  1183. case INQUIRY:
  1184. *size = (cdb[3] << 8) + cdb[4];
  1185. /*
  1186. * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
  1187. * See spc4r17 section 5.3
  1188. */
  1189. cmd->sam_task_attr = MSG_HEAD_TAG;
  1190. cmd->execute_cmd = spc_emulate_inquiry;
  1191. break;
  1192. case SECURITY_PROTOCOL_IN:
  1193. case SECURITY_PROTOCOL_OUT:
  1194. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1195. break;
  1196. case EXTENDED_COPY:
  1197. *size = get_unaligned_be32(&cdb[10]);
  1198. cmd->execute_cmd = target_do_xcopy;
  1199. break;
  1200. case RECEIVE_COPY_RESULTS:
  1201. *size = get_unaligned_be32(&cdb[10]);
  1202. cmd->execute_cmd = target_do_receive_copy_results;
  1203. break;
  1204. case READ_ATTRIBUTE:
  1205. case WRITE_ATTRIBUTE:
  1206. *size = (cdb[10] << 24) | (cdb[11] << 16) |
  1207. (cdb[12] << 8) | cdb[13];
  1208. break;
  1209. case RECEIVE_DIAGNOSTIC:
  1210. case SEND_DIAGNOSTIC:
  1211. *size = (cdb[3] << 8) | cdb[4];
  1212. break;
  1213. case WRITE_BUFFER:
  1214. *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
  1215. break;
  1216. case REPORT_LUNS:
  1217. cmd->execute_cmd = spc_emulate_report_luns;
  1218. *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
  1219. /*
  1220. * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
  1221. * See spc4r17 section 5.3
  1222. */
  1223. cmd->sam_task_attr = MSG_HEAD_TAG;
  1224. break;
  1225. case TEST_UNIT_READY:
  1226. cmd->execute_cmd = spc_emulate_testunitready;
  1227. *size = 0;
  1228. break;
  1229. case MAINTENANCE_IN:
  1230. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1231. /*
  1232. * MAINTENANCE_IN from SCC-2
  1233. * Check for emulated MI_REPORT_TARGET_PGS
  1234. */
  1235. if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) {
  1236. cmd->execute_cmd =
  1237. target_emulate_report_target_port_groups;
  1238. }
  1239. *size = get_unaligned_be32(&cdb[6]);
  1240. } else {
  1241. /*
  1242. * GPCMD_SEND_KEY from multi media commands
  1243. */
  1244. *size = get_unaligned_be16(&cdb[8]);
  1245. }
  1246. break;
  1247. case MAINTENANCE_OUT:
  1248. if (dev->transport->get_device_type(dev) != TYPE_ROM) {
  1249. /*
  1250. * MAINTENANCE_OUT from SCC-2
  1251. * Check for emulated MO_SET_TARGET_PGS.
  1252. */
  1253. if (cdb[1] == MO_SET_TARGET_PGS) {
  1254. cmd->execute_cmd =
  1255. target_emulate_set_target_port_groups;
  1256. }
  1257. *size = get_unaligned_be32(&cdb[6]);
  1258. } else {
  1259. /*
  1260. * GPCMD_SEND_KEY from multi media commands
  1261. */
  1262. *size = get_unaligned_be16(&cdb[8]);
  1263. }
  1264. break;
  1265. default:
  1266. pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
  1267. " 0x%02x, sending CHECK_CONDITION.\n",
  1268. cmd->se_tfo->get_fabric_name(), cdb[0]);
  1269. return TCM_UNSUPPORTED_SCSI_OPCODE;
  1270. }
  1271. return 0;
  1272. }
  1273. EXPORT_SYMBOL(spc_parse_cdb);