target_core_spc.c 34 KB

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