target_core_spc.c 38 KB

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