target_core_spc.c 38 KB

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