target_core_spc.c 34 KB

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