target_core_spc.c 37 KB

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