target_core_spc.c 38 KB

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