target_core_cdb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*
  2. * CDB emulation for non-READ/WRITE commands.
  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 <asm/unaligned.h>
  26. #include <scsi/scsi.h>
  27. #include <target/target_core_base.h>
  28. #include <target/target_core_transport.h>
  29. #include <target/target_core_fabric_ops.h>
  30. #include "target_core_ua.h"
  31. static void
  32. target_fill_alua_data(struct se_port *port, unsigned char *buf)
  33. {
  34. struct t10_alua_tg_pt_gp *tg_pt_gp;
  35. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  36. /*
  37. * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
  38. */
  39. buf[5] = 0x80;
  40. /*
  41. * Set TPGS field for explict and/or implict ALUA access type
  42. * and opteration.
  43. *
  44. * See spc4r17 section 6.4.2 Table 135
  45. */
  46. if (!port)
  47. return;
  48. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  49. if (!tg_pt_gp_mem)
  50. return;
  51. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  52. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  53. if (tg_pt_gp)
  54. buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
  55. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  56. }
  57. static int
  58. target_emulate_inquiry_std(struct se_cmd *cmd)
  59. {
  60. struct se_lun *lun = cmd->se_lun;
  61. struct se_device *dev = cmd->se_dev;
  62. unsigned char *buf;
  63. /*
  64. * Make sure we at least have 6 bytes of INQUIRY response
  65. * payload going back for EVPD=0
  66. */
  67. if (cmd->data_length < 6) {
  68. printk(KERN_ERR "SCSI Inquiry payload length: %u"
  69. " too small for EVPD=0\n", cmd->data_length);
  70. return -EINVAL;
  71. }
  72. buf = transport_kmap_first_data_page(cmd);
  73. buf[0] = dev->transport->get_device_type(dev);
  74. if (buf[0] == TYPE_TAPE)
  75. buf[1] = 0x80;
  76. buf[2] = dev->transport->get_device_rev(dev);
  77. /*
  78. * Enable SCCS and TPGS fields for Emulated ALUA
  79. */
  80. if (dev->se_sub_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED)
  81. target_fill_alua_data(lun->lun_sep, buf);
  82. if (cmd->data_length < 8) {
  83. buf[4] = 1; /* Set additional length to 1 */
  84. goto out;
  85. }
  86. buf[7] = 0x32; /* Sync=1 and CmdQue=1 */
  87. /*
  88. * Do not include vendor, product, reversion info in INQUIRY
  89. * response payload for cdbs with a small allocation length.
  90. */
  91. if (cmd->data_length < 36) {
  92. buf[4] = 3; /* Set additional length to 3 */
  93. goto out;
  94. }
  95. snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
  96. snprintf((unsigned char *)&buf[16], 16, "%s",
  97. &dev->se_sub_dev->t10_wwn.model[0]);
  98. snprintf((unsigned char *)&buf[32], 4, "%s",
  99. &dev->se_sub_dev->t10_wwn.revision[0]);
  100. buf[4] = 31; /* Set additional length to 31 */
  101. out:
  102. transport_kunmap_first_data_page(cmd);
  103. return 0;
  104. }
  105. /* unit serial number */
  106. static int
  107. target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
  108. {
  109. struct se_device *dev = cmd->se_dev;
  110. u16 len = 0;
  111. if (dev->se_sub_dev->su_dev_flags &
  112. SDF_EMULATED_VPD_UNIT_SERIAL) {
  113. u32 unit_serial_len;
  114. unit_serial_len =
  115. strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
  116. unit_serial_len++; /* For NULL Terminator */
  117. if (((len + 4) + unit_serial_len) > cmd->data_length) {
  118. len += unit_serial_len;
  119. buf[2] = ((len >> 8) & 0xff);
  120. buf[3] = (len & 0xff);
  121. return 0;
  122. }
  123. len += sprintf((unsigned char *)&buf[4], "%s",
  124. &dev->se_sub_dev->t10_wwn.unit_serial[0]);
  125. len++; /* Extra Byte for NULL Terminator */
  126. buf[3] = len;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * Device identification VPD, for a complete list of
  132. * DESIGNATOR TYPEs see spc4r17 Table 459.
  133. */
  134. static int
  135. target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
  136. {
  137. struct se_device *dev = cmd->se_dev;
  138. struct se_lun *lun = cmd->se_lun;
  139. struct se_port *port = NULL;
  140. struct se_portal_group *tpg = NULL;
  141. struct t10_alua_lu_gp_member *lu_gp_mem;
  142. struct t10_alua_tg_pt_gp *tg_pt_gp;
  143. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  144. unsigned char binary, binary_new;
  145. unsigned char *prod = &dev->se_sub_dev->t10_wwn.model[0];
  146. u32 prod_len;
  147. u32 unit_serial_len, off = 0;
  148. int i;
  149. u16 len = 0, id_len;
  150. off = 4;
  151. /*
  152. * NAA IEEE Registered Extended Assigned designator format, see
  153. * spc4r17 section 7.7.3.6.5
  154. *
  155. * We depend upon a target_core_mod/ConfigFS provided
  156. * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
  157. * value in order to return the NAA id.
  158. */
  159. if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
  160. goto check_t10_vend_desc;
  161. if (off + 20 > cmd->data_length)
  162. goto check_t10_vend_desc;
  163. /* CODE SET == Binary */
  164. buf[off++] = 0x1;
  165. /* Set ASSOICATION == addressed logical unit: 0)b */
  166. buf[off] = 0x00;
  167. /* Identifier/Designator type == NAA identifier */
  168. buf[off++] = 0x3;
  169. off++;
  170. /* Identifier/Designator length */
  171. buf[off++] = 0x10;
  172. /*
  173. * Start NAA IEEE Registered Extended Identifier/Designator
  174. */
  175. buf[off++] = (0x6 << 4);
  176. /*
  177. * Use OpenFabrics IEEE Company ID: 00 14 05
  178. */
  179. buf[off++] = 0x01;
  180. buf[off++] = 0x40;
  181. buf[off] = (0x5 << 4);
  182. /*
  183. * Return ConfigFS Unit Serial Number information for
  184. * VENDOR_SPECIFIC_IDENTIFIER and
  185. * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
  186. */
  187. binary = transport_asciihex_to_binaryhex(
  188. &dev->se_sub_dev->t10_wwn.unit_serial[0]);
  189. buf[off++] |= (binary & 0xf0) >> 4;
  190. for (i = 0; i < 24; i += 2) {
  191. binary_new = transport_asciihex_to_binaryhex(
  192. &dev->se_sub_dev->t10_wwn.unit_serial[i+2]);
  193. buf[off] = (binary & 0x0f) << 4;
  194. buf[off++] |= (binary_new & 0xf0) >> 4;
  195. binary = binary_new;
  196. }
  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->se_sub_dev->su_dev_flags &
  209. SDF_EMULATED_VPD_UNIT_SERIAL) {
  210. unit_serial_len =
  211. strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
  212. unit_serial_len++; /* For NULL Terminator */
  213. if ((len + (id_len + 4) +
  214. (prod_len + unit_serial_len)) >
  215. cmd->data_length) {
  216. len += (prod_len + unit_serial_len);
  217. goto check_port;
  218. }
  219. id_len += sprintf((unsigned char *)&buf[off+12],
  220. "%s:%s", prod,
  221. &dev->se_sub_dev->t10_wwn.unit_serial[0]);
  222. }
  223. buf[off] = 0x2; /* ASCII */
  224. buf[off+1] = 0x1; /* T10 Vendor ID */
  225. buf[off+2] = 0x0;
  226. memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
  227. /* Extra Byte for NULL Terminator */
  228. id_len++;
  229. /* Identifier Length */
  230. buf[off+3] = id_len;
  231. /* Header size for Designation descriptor */
  232. len += (id_len + 4);
  233. off += (id_len + 4);
  234. /*
  235. * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
  236. */
  237. check_port:
  238. port = lun->lun_sep;
  239. if (port) {
  240. struct t10_alua_lu_gp *lu_gp;
  241. u32 padding, scsi_name_len;
  242. u16 lu_gp_id = 0;
  243. u16 tg_pt_gp_id = 0;
  244. u16 tpgt;
  245. tpg = port->sep_tpg;
  246. /*
  247. * Relative target port identifer, see spc4r17
  248. * section 7.7.3.7
  249. *
  250. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  251. * section 7.5.1 Table 362
  252. */
  253. if (((len + 4) + 8) > cmd->data_length) {
  254. len += 8;
  255. goto check_tpgi;
  256. }
  257. buf[off] =
  258. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  259. buf[off++] |= 0x1; /* CODE SET == Binary */
  260. buf[off] = 0x80; /* Set PIV=1 */
  261. /* Set ASSOICATION == target port: 01b */
  262. buf[off] |= 0x10;
  263. /* DESIGNATOR TYPE == Relative target port identifer */
  264. buf[off++] |= 0x4;
  265. off++; /* Skip over Reserved */
  266. buf[off++] = 4; /* DESIGNATOR LENGTH */
  267. /* Skip over Obsolete field in RTPI payload
  268. * in Table 472 */
  269. off += 2;
  270. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  271. buf[off++] = (port->sep_rtpi & 0xff);
  272. len += 8; /* Header size + Designation descriptor */
  273. /*
  274. * Target port group identifier, see spc4r17
  275. * section 7.7.3.8
  276. *
  277. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  278. * section 7.5.1 Table 362
  279. */
  280. check_tpgi:
  281. if (dev->se_sub_dev->t10_alua.alua_type !=
  282. SPC3_ALUA_EMULATED)
  283. goto check_scsi_name;
  284. if (((len + 4) + 8) > cmd->data_length) {
  285. len += 8;
  286. goto check_lu_gp;
  287. }
  288. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  289. if (!tg_pt_gp_mem)
  290. goto check_lu_gp;
  291. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  292. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  293. if (!(tg_pt_gp)) {
  294. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  295. goto check_lu_gp;
  296. }
  297. tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
  298. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  299. buf[off] =
  300. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  301. buf[off++] |= 0x1; /* CODE SET == Binary */
  302. buf[off] = 0x80; /* Set PIV=1 */
  303. /* Set ASSOICATION == target port: 01b */
  304. buf[off] |= 0x10;
  305. /* DESIGNATOR TYPE == Target port group identifier */
  306. buf[off++] |= 0x5;
  307. off++; /* Skip over Reserved */
  308. buf[off++] = 4; /* DESIGNATOR LENGTH */
  309. off += 2; /* Skip over Reserved Field */
  310. buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
  311. buf[off++] = (tg_pt_gp_id & 0xff);
  312. len += 8; /* Header size + Designation descriptor */
  313. /*
  314. * Logical Unit Group identifier, see spc4r17
  315. * section 7.7.3.8
  316. */
  317. check_lu_gp:
  318. if (((len + 4) + 8) > cmd->data_length) {
  319. len += 8;
  320. goto check_scsi_name;
  321. }
  322. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  323. if (!(lu_gp_mem))
  324. goto check_scsi_name;
  325. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  326. lu_gp = lu_gp_mem->lu_gp;
  327. if (!(lu_gp)) {
  328. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  329. goto check_scsi_name;
  330. }
  331. lu_gp_id = lu_gp->lu_gp_id;
  332. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  333. buf[off++] |= 0x1; /* CODE SET == Binary */
  334. /* DESIGNATOR TYPE == Logical Unit Group identifier */
  335. buf[off++] |= 0x6;
  336. off++; /* Skip over Reserved */
  337. buf[off++] = 4; /* DESIGNATOR LENGTH */
  338. off += 2; /* Skip over Reserved Field */
  339. buf[off++] = ((lu_gp_id >> 8) & 0xff);
  340. buf[off++] = (lu_gp_id & 0xff);
  341. len += 8; /* Header size + Designation descriptor */
  342. /*
  343. * SCSI name string designator, see spc4r17
  344. * section 7.7.3.11
  345. *
  346. * Get the PROTOCOL IDENTIFIER as defined by spc4r17
  347. * section 7.5.1 Table 362
  348. */
  349. check_scsi_name:
  350. scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  351. /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
  352. scsi_name_len += 10;
  353. /* Check for 4-byte padding */
  354. padding = ((-scsi_name_len) & 3);
  355. if (padding != 0)
  356. scsi_name_len += padding;
  357. /* Header size + Designation descriptor */
  358. scsi_name_len += 4;
  359. if (((len + 4) + scsi_name_len) > cmd->data_length) {
  360. len += scsi_name_len;
  361. goto set_len;
  362. }
  363. buf[off] =
  364. (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
  365. buf[off++] |= 0x3; /* CODE SET == UTF-8 */
  366. buf[off] = 0x80; /* Set PIV=1 */
  367. /* Set ASSOICATION == target port: 01b */
  368. buf[off] |= 0x10;
  369. /* DESIGNATOR TYPE == SCSI name string */
  370. buf[off++] |= 0x8;
  371. off += 2; /* Skip over Reserved and length */
  372. /*
  373. * SCSI name string identifer containing, $FABRIC_MOD
  374. * dependent information. For LIO-Target and iSCSI
  375. * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
  376. * UTF-8 encoding.
  377. */
  378. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  379. scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
  380. tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
  381. scsi_name_len += 1 /* Include NULL terminator */;
  382. /*
  383. * The null-terminated, null-padded (see 4.4.2) SCSI
  384. * NAME STRING field contains a UTF-8 format string.
  385. * The number of bytes in the SCSI NAME STRING field
  386. * (i.e., the value in the DESIGNATOR LENGTH field)
  387. * shall be no larger than 256 and shall be a multiple
  388. * of four.
  389. */
  390. if (padding)
  391. scsi_name_len += padding;
  392. buf[off-1] = scsi_name_len;
  393. off += scsi_name_len;
  394. /* Header size + Designation descriptor */
  395. len += (scsi_name_len + 4);
  396. }
  397. set_len:
  398. buf[2] = ((len >> 8) & 0xff);
  399. buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
  400. return 0;
  401. }
  402. /* Extended INQUIRY Data VPD Page */
  403. static int
  404. target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
  405. {
  406. if (cmd->data_length < 60)
  407. return 0;
  408. buf[2] = 0x3c;
  409. /* Set HEADSUP, ORDSUP, SIMPSUP */
  410. buf[5] = 0x07;
  411. /* If WriteCache emulation is enabled, set V_SUP */
  412. if (cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
  413. buf[6] = 0x01;
  414. return 0;
  415. }
  416. /* Block Limits VPD page */
  417. static int
  418. target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
  419. {
  420. struct se_device *dev = cmd->se_dev;
  421. int have_tp = 0;
  422. /*
  423. * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
  424. * emulate_tpu=1 or emulate_tpws=1 we will be expect a
  425. * different page length for Thin Provisioning.
  426. */
  427. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  428. have_tp = 1;
  429. if (cmd->data_length < (0x10 + 4)) {
  430. printk(KERN_INFO "Received data_length: %u"
  431. " too small for EVPD 0xb0\n",
  432. cmd->data_length);
  433. return -EINVAL;
  434. }
  435. if (have_tp && cmd->data_length < (0x3c + 4)) {
  436. printk(KERN_INFO "Received data_length: %u"
  437. " too small for TPE=1 EVPD 0xb0\n",
  438. cmd->data_length);
  439. have_tp = 0;
  440. }
  441. buf[0] = dev->transport->get_device_type(dev);
  442. buf[3] = have_tp ? 0x3c : 0x10;
  443. /*
  444. * Set OPTIMAL TRANSFER LENGTH GRANULARITY
  445. */
  446. put_unaligned_be16(1, &buf[6]);
  447. /*
  448. * Set MAXIMUM TRANSFER LENGTH
  449. */
  450. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_sectors, &buf[8]);
  451. /*
  452. * Set OPTIMAL TRANSFER LENGTH
  453. */
  454. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.optimal_sectors, &buf[12]);
  455. /*
  456. * Exit now if we don't support TP or the initiator sent a too
  457. * short buffer.
  458. */
  459. if (!have_tp || cmd->data_length < (0x3c + 4))
  460. return 0;
  461. /*
  462. * Set MAXIMUM UNMAP LBA COUNT
  463. */
  464. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count, &buf[20]);
  465. /*
  466. * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
  467. */
  468. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count,
  469. &buf[24]);
  470. /*
  471. * Set OPTIMAL UNMAP GRANULARITY
  472. */
  473. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity, &buf[28]);
  474. /*
  475. * UNMAP GRANULARITY ALIGNMENT
  476. */
  477. put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment,
  478. &buf[32]);
  479. if (dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
  480. buf[32] |= 0x80; /* Set the UGAVALID bit */
  481. return 0;
  482. }
  483. /* Block Device Characteristics VPD page */
  484. static int
  485. target_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
  486. {
  487. struct se_device *dev = cmd->se_dev;
  488. buf[0] = dev->transport->get_device_type(dev);
  489. buf[3] = 0x3c;
  490. if (cmd->data_length >= 5 &&
  491. dev->se_sub_dev->se_dev_attrib.is_nonrot)
  492. buf[5] = 1;
  493. return 0;
  494. }
  495. /* Thin Provisioning VPD */
  496. static int
  497. target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
  498. {
  499. struct se_device *dev = cmd->se_dev;
  500. /*
  501. * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
  502. *
  503. * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
  504. * zero, then the page length shall be set to 0004h. If the DP bit
  505. * is set to one, then the page length shall be set to the value
  506. * defined in table 162.
  507. */
  508. buf[0] = dev->transport->get_device_type(dev);
  509. /*
  510. * Set Hardcoded length mentioned above for DP=0
  511. */
  512. put_unaligned_be16(0x0004, &buf[2]);
  513. /*
  514. * The THRESHOLD EXPONENT field indicates the threshold set size in
  515. * LBAs as a power of 2 (i.e., the threshold set size is equal to
  516. * 2(threshold exponent)).
  517. *
  518. * Note that this is currently set to 0x00 as mkp says it will be
  519. * changing again. We can enable this once it has settled in T10
  520. * and is actually used by Linux/SCSI ML code.
  521. */
  522. buf[4] = 0x00;
  523. /*
  524. * A TPU bit set to one indicates that the device server supports
  525. * the UNMAP command (see 5.25). A TPU bit set to zero indicates
  526. * that the device server does not support the UNMAP command.
  527. */
  528. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu != 0)
  529. buf[5] = 0x80;
  530. /*
  531. * A TPWS bit set to one indicates that the device server supports
  532. * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
  533. * A TPWS bit set to zero indicates that the device server does not
  534. * support the use of the WRITE SAME (16) command to unmap LBAs.
  535. */
  536. if (dev->se_sub_dev->se_dev_attrib.emulate_tpws != 0)
  537. buf[5] |= 0x40;
  538. return 0;
  539. }
  540. static int
  541. target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
  542. static struct {
  543. uint8_t page;
  544. int (*emulate)(struct se_cmd *, unsigned char *);
  545. } evpd_handlers[] = {
  546. { .page = 0x00, .emulate = target_emulate_evpd_00 },
  547. { .page = 0x80, .emulate = target_emulate_evpd_80 },
  548. { .page = 0x83, .emulate = target_emulate_evpd_83 },
  549. { .page = 0x86, .emulate = target_emulate_evpd_86 },
  550. { .page = 0xb0, .emulate = target_emulate_evpd_b0 },
  551. { .page = 0xb1, .emulate = target_emulate_evpd_b1 },
  552. { .page = 0xb2, .emulate = target_emulate_evpd_b2 },
  553. };
  554. /* supported vital product data pages */
  555. static int
  556. target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
  557. {
  558. int p;
  559. if (cmd->data_length < 8)
  560. return 0;
  561. /*
  562. * Only report the INQUIRY EVPD=1 pages after a valid NAA
  563. * Registered Extended LUN WWN has been set via ConfigFS
  564. * during device creation/restart.
  565. */
  566. if (cmd->se_dev->se_sub_dev->su_dev_flags &
  567. SDF_EMULATED_VPD_UNIT_SERIAL) {
  568. buf[3] = ARRAY_SIZE(evpd_handlers);
  569. for (p = 0; p < min_t(int, ARRAY_SIZE(evpd_handlers),
  570. cmd->data_length - 4); ++p)
  571. buf[p + 4] = evpd_handlers[p].page;
  572. }
  573. return 0;
  574. }
  575. static int
  576. target_emulate_inquiry(struct se_cmd *cmd)
  577. {
  578. struct se_device *dev = cmd->se_dev;
  579. unsigned char *buf;
  580. unsigned char *cdb = cmd->t_task_cdb;
  581. int p, ret;
  582. if (!(cdb[1] & 0x1))
  583. return target_emulate_inquiry_std(cmd);
  584. /*
  585. * Make sure we at least have 4 bytes of INQUIRY response
  586. * payload for 0x00 going back for EVPD=1. Note that 0x80
  587. * and 0x83 will check for enough payload data length and
  588. * jump to set_len: label when there is not enough inquiry EVPD
  589. * payload length left for the next outgoing EVPD metadata
  590. */
  591. if (cmd->data_length < 4) {
  592. printk(KERN_ERR "SCSI Inquiry payload length: %u"
  593. " too small for EVPD=1\n", cmd->data_length);
  594. return -EINVAL;
  595. }
  596. buf = transport_kmap_first_data_page(cmd);
  597. buf[0] = dev->transport->get_device_type(dev);
  598. for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
  599. if (cdb[2] == evpd_handlers[p].page) {
  600. buf[1] = cdb[2];
  601. ret = evpd_handlers[p].emulate(cmd, buf);
  602. transport_kunmap_first_data_page(cmd);
  603. return ret;
  604. }
  605. transport_kunmap_first_data_page(cmd);
  606. printk(KERN_ERR "Unknown VPD Code: 0x%02x\n", cdb[2]);
  607. return -EINVAL;
  608. }
  609. static int
  610. target_emulate_readcapacity(struct se_cmd *cmd)
  611. {
  612. struct se_device *dev = cmd->se_dev;
  613. unsigned char *buf;
  614. unsigned long long blocks_long = dev->transport->get_blocks(dev);
  615. u32 blocks;
  616. if (blocks_long >= 0x00000000ffffffff)
  617. blocks = 0xffffffff;
  618. else
  619. blocks = (u32)blocks_long;
  620. buf = transport_kmap_first_data_page(cmd);
  621. buf[0] = (blocks >> 24) & 0xff;
  622. buf[1] = (blocks >> 16) & 0xff;
  623. buf[2] = (blocks >> 8) & 0xff;
  624. buf[3] = blocks & 0xff;
  625. buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  626. buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  627. buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  628. buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  629. /*
  630. * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
  631. */
  632. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  633. put_unaligned_be32(0xFFFFFFFF, &buf[0]);
  634. transport_kunmap_first_data_page(cmd);
  635. return 0;
  636. }
  637. static int
  638. target_emulate_readcapacity_16(struct se_cmd *cmd)
  639. {
  640. struct se_device *dev = cmd->se_dev;
  641. unsigned char *buf;
  642. unsigned long long blocks = dev->transport->get_blocks(dev);
  643. buf = transport_kmap_first_data_page(cmd);
  644. buf[0] = (blocks >> 56) & 0xff;
  645. buf[1] = (blocks >> 48) & 0xff;
  646. buf[2] = (blocks >> 40) & 0xff;
  647. buf[3] = (blocks >> 32) & 0xff;
  648. buf[4] = (blocks >> 24) & 0xff;
  649. buf[5] = (blocks >> 16) & 0xff;
  650. buf[6] = (blocks >> 8) & 0xff;
  651. buf[7] = blocks & 0xff;
  652. buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
  653. buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
  654. buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
  655. buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
  656. /*
  657. * Set Thin Provisioning Enable bit following sbc3r22 in section
  658. * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
  659. */
  660. if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
  661. buf[14] = 0x80;
  662. transport_kunmap_first_data_page(cmd);
  663. return 0;
  664. }
  665. static int
  666. target_modesense_rwrecovery(unsigned char *p)
  667. {
  668. p[0] = 0x01;
  669. p[1] = 0x0a;
  670. return 12;
  671. }
  672. static int
  673. target_modesense_control(struct se_device *dev, unsigned char *p)
  674. {
  675. p[0] = 0x0a;
  676. p[1] = 0x0a;
  677. p[2] = 2;
  678. /*
  679. * From spc4r17, section 7.4.6 Control mode Page
  680. *
  681. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  682. *
  683. * 00b: The logical unit shall clear any unit attention condition
  684. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  685. * status and shall not establish a unit attention condition when a com-
  686. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  687. * status.
  688. *
  689. * 10b: The logical unit shall not clear any unit attention condition
  690. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  691. * status and shall not establish a unit attention condition when
  692. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  693. * CONFLICT status.
  694. *
  695. * 11b a The logical unit shall not clear any unit attention condition
  696. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  697. * status and shall establish a unit attention condition for the
  698. * initiator port associated with the I_T nexus on which the BUSY,
  699. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  700. * Depending on the status, the additional sense code shall be set to
  701. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  702. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  703. * command, a unit attention condition shall be established only once
  704. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  705. * to the number of commands completed with one of those status codes.
  706. */
  707. p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
  708. (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  709. /*
  710. * From spc4r17, section 7.4.6 Control mode Page
  711. *
  712. * Task Aborted Status (TAS) bit set to zero.
  713. *
  714. * A task aborted status (TAS) bit set to zero specifies that aborted
  715. * tasks shall be terminated by the device server without any response
  716. * to the application client. A TAS bit set to one specifies that tasks
  717. * aborted by the actions of an I_T nexus other than the I_T nexus on
  718. * which the command was received shall be completed with TASK ABORTED
  719. * status (see SAM-4).
  720. */
  721. p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
  722. p[8] = 0xff;
  723. p[9] = 0xff;
  724. p[11] = 30;
  725. return 12;
  726. }
  727. static int
  728. target_modesense_caching(struct se_device *dev, unsigned char *p)
  729. {
  730. p[0] = 0x08;
  731. p[1] = 0x12;
  732. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
  733. p[2] = 0x04; /* Write Cache Enable */
  734. p[12] = 0x20; /* Disabled Read Ahead */
  735. return 20;
  736. }
  737. static void
  738. target_modesense_write_protect(unsigned char *buf, int type)
  739. {
  740. /*
  741. * I believe that the WP bit (bit 7) in the mode header is the same for
  742. * all device types..
  743. */
  744. switch (type) {
  745. case TYPE_DISK:
  746. case TYPE_TAPE:
  747. default:
  748. buf[0] |= 0x80; /* WP bit */
  749. break;
  750. }
  751. }
  752. static void
  753. target_modesense_dpofua(unsigned char *buf, int type)
  754. {
  755. switch (type) {
  756. case TYPE_DISK:
  757. buf[0] |= 0x10; /* DPOFUA bit */
  758. break;
  759. default:
  760. break;
  761. }
  762. }
  763. static int
  764. target_emulate_modesense(struct se_cmd *cmd, int ten)
  765. {
  766. struct se_device *dev = cmd->se_dev;
  767. char *cdb = cmd->t_task_cdb;
  768. unsigned char *rbuf;
  769. int type = dev->transport->get_device_type(dev);
  770. int offset = (ten) ? 8 : 4;
  771. int length = 0;
  772. unsigned char buf[SE_MODE_PAGE_BUF];
  773. memset(buf, 0, SE_MODE_PAGE_BUF);
  774. switch (cdb[2] & 0x3f) {
  775. case 0x01:
  776. length = target_modesense_rwrecovery(&buf[offset]);
  777. break;
  778. case 0x08:
  779. length = target_modesense_caching(dev, &buf[offset]);
  780. break;
  781. case 0x0a:
  782. length = target_modesense_control(dev, &buf[offset]);
  783. break;
  784. case 0x3f:
  785. length = target_modesense_rwrecovery(&buf[offset]);
  786. length += target_modesense_caching(dev, &buf[offset+length]);
  787. length += target_modesense_control(dev, &buf[offset+length]);
  788. break;
  789. default:
  790. printk(KERN_ERR "Got Unknown Mode Page: 0x%02x\n",
  791. cdb[2] & 0x3f);
  792. return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
  793. }
  794. offset += length;
  795. if (ten) {
  796. offset -= 2;
  797. buf[0] = (offset >> 8) & 0xff;
  798. buf[1] = offset & 0xff;
  799. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  800. (cmd->se_deve &&
  801. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  802. target_modesense_write_protect(&buf[3], type);
  803. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  804. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  805. target_modesense_dpofua(&buf[3], type);
  806. if ((offset + 2) > cmd->data_length)
  807. offset = cmd->data_length;
  808. } else {
  809. offset -= 1;
  810. buf[0] = offset & 0xff;
  811. if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  812. (cmd->se_deve &&
  813. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  814. target_modesense_write_protect(&buf[2], type);
  815. if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
  816. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
  817. target_modesense_dpofua(&buf[2], type);
  818. if ((offset + 1) > cmd->data_length)
  819. offset = cmd->data_length;
  820. }
  821. rbuf = transport_kmap_first_data_page(cmd);
  822. memcpy(rbuf, buf, offset);
  823. transport_kunmap_first_data_page(cmd);
  824. return 0;
  825. }
  826. static int
  827. target_emulate_request_sense(struct se_cmd *cmd)
  828. {
  829. unsigned char *cdb = cmd->t_task_cdb;
  830. unsigned char *buf;
  831. u8 ua_asc = 0, ua_ascq = 0;
  832. int err = 0;
  833. if (cdb[1] & 0x01) {
  834. printk(KERN_ERR "REQUEST_SENSE description emulation not"
  835. " supported\n");
  836. return PYX_TRANSPORT_INVALID_CDB_FIELD;
  837. }
  838. buf = transport_kmap_first_data_page(cmd);
  839. if (!(core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))) {
  840. /*
  841. * CURRENT ERROR, UNIT ATTENTION
  842. */
  843. buf[0] = 0x70;
  844. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  845. /*
  846. * Make sure request data length is enough for additional
  847. * sense data.
  848. */
  849. if (cmd->data_length <= 18) {
  850. buf[7] = 0x00;
  851. err = -EINVAL;
  852. goto end;
  853. }
  854. /*
  855. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  856. */
  857. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  858. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  859. buf[7] = 0x0A;
  860. } else {
  861. /*
  862. * CURRENT ERROR, NO SENSE
  863. */
  864. buf[0] = 0x70;
  865. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  866. /*
  867. * Make sure request data length is enough for additional
  868. * sense data.
  869. */
  870. if (cmd->data_length <= 18) {
  871. buf[7] = 0x00;
  872. err = -EINVAL;
  873. goto end;
  874. }
  875. /*
  876. * NO ADDITIONAL SENSE INFORMATION
  877. */
  878. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  879. buf[7] = 0x0A;
  880. }
  881. end:
  882. transport_kunmap_first_data_page(cmd);
  883. return 0;
  884. }
  885. /*
  886. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  887. * Note this is not used for TCM/pSCSI passthrough
  888. */
  889. static int
  890. target_emulate_unmap(struct se_task *task)
  891. {
  892. struct se_cmd *cmd = task->task_se_cmd;
  893. struct se_device *dev = cmd->se_dev;
  894. unsigned char *buf, *ptr = NULL;
  895. unsigned char *cdb = &cmd->t_task_cdb[0];
  896. sector_t lba;
  897. unsigned int size = cmd->data_length, range;
  898. int ret = 0, offset;
  899. unsigned short dl, bd_dl;
  900. /* First UNMAP block descriptor starts at 8 byte offset */
  901. offset = 8;
  902. size -= 8;
  903. dl = get_unaligned_be16(&cdb[0]);
  904. bd_dl = get_unaligned_be16(&cdb[2]);
  905. buf = transport_kmap_first_data_page(cmd);
  906. ptr = &buf[offset];
  907. printk(KERN_INFO "UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  908. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  909. while (size) {
  910. lba = get_unaligned_be64(&ptr[0]);
  911. range = get_unaligned_be32(&ptr[8]);
  912. printk(KERN_INFO "UNMAP: Using lba: %llu and range: %u\n",
  913. (unsigned long long)lba, range);
  914. ret = dev->transport->do_discard(dev, lba, range);
  915. if (ret < 0) {
  916. printk(KERN_ERR "blkdev_issue_discard() failed: %d\n",
  917. ret);
  918. goto err;
  919. }
  920. ptr += 16;
  921. size -= 16;
  922. }
  923. task->task_scsi_status = GOOD;
  924. transport_complete_task(task, 1);
  925. err:
  926. transport_kunmap_first_data_page(cmd);
  927. return ret;
  928. }
  929. /*
  930. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  931. * Note this is not used for TCM/pSCSI passthrough
  932. */
  933. static int
  934. target_emulate_write_same(struct se_task *task, int write_same32)
  935. {
  936. struct se_cmd *cmd = task->task_se_cmd;
  937. struct se_device *dev = cmd->se_dev;
  938. sector_t range;
  939. sector_t lba = cmd->t_task_lba;
  940. unsigned int num_blocks;
  941. int ret;
  942. /*
  943. * Extract num_blocks from the WRITE_SAME_* CDB. Then use the explict
  944. * range when non zero is supplied, otherwise calculate the remaining
  945. * range based on ->get_blocks() - starting LBA.
  946. */
  947. if (write_same32)
  948. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
  949. else
  950. num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
  951. if (num_blocks != 0)
  952. range = num_blocks;
  953. else
  954. range = (dev->transport->get_blocks(dev) - lba);
  955. printk(KERN_INFO "WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
  956. (unsigned long long)lba, (unsigned long long)range);
  957. ret = dev->transport->do_discard(dev, lba, range);
  958. if (ret < 0) {
  959. printk(KERN_INFO "blkdev_issue_discard() failed for WRITE_SAME\n");
  960. return ret;
  961. }
  962. task->task_scsi_status = GOOD;
  963. transport_complete_task(task, 1);
  964. return 0;
  965. }
  966. int
  967. transport_emulate_control_cdb(struct se_task *task)
  968. {
  969. struct se_cmd *cmd = task->task_se_cmd;
  970. struct se_device *dev = cmd->se_dev;
  971. unsigned short service_action;
  972. int ret = 0;
  973. switch (cmd->t_task_cdb[0]) {
  974. case INQUIRY:
  975. ret = target_emulate_inquiry(cmd);
  976. break;
  977. case READ_CAPACITY:
  978. ret = target_emulate_readcapacity(cmd);
  979. break;
  980. case MODE_SENSE:
  981. ret = target_emulate_modesense(cmd, 0);
  982. break;
  983. case MODE_SENSE_10:
  984. ret = target_emulate_modesense(cmd, 1);
  985. break;
  986. case SERVICE_ACTION_IN:
  987. switch (cmd->t_task_cdb[1] & 0x1f) {
  988. case SAI_READ_CAPACITY_16:
  989. ret = target_emulate_readcapacity_16(cmd);
  990. break;
  991. default:
  992. printk(KERN_ERR "Unsupported SA: 0x%02x\n",
  993. cmd->t_task_cdb[1] & 0x1f);
  994. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  995. }
  996. break;
  997. case REQUEST_SENSE:
  998. ret = target_emulate_request_sense(cmd);
  999. break;
  1000. case UNMAP:
  1001. if (!dev->transport->do_discard) {
  1002. printk(KERN_ERR "UNMAP emulation not supported for: %s\n",
  1003. dev->transport->name);
  1004. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1005. }
  1006. ret = target_emulate_unmap(task);
  1007. break;
  1008. case WRITE_SAME_16:
  1009. if (!dev->transport->do_discard) {
  1010. printk(KERN_ERR "WRITE_SAME_16 emulation not supported"
  1011. " for: %s\n", dev->transport->name);
  1012. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1013. }
  1014. ret = target_emulate_write_same(task, 0);
  1015. break;
  1016. case VARIABLE_LENGTH_CMD:
  1017. service_action =
  1018. get_unaligned_be16(&cmd->t_task_cdb[8]);
  1019. switch (service_action) {
  1020. case WRITE_SAME_32:
  1021. if (!dev->transport->do_discard) {
  1022. printk(KERN_ERR "WRITE_SAME_32 SA emulation not"
  1023. " supported for: %s\n",
  1024. dev->transport->name);
  1025. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1026. }
  1027. ret = target_emulate_write_same(task, 1);
  1028. break;
  1029. default:
  1030. printk(KERN_ERR "Unsupported VARIABLE_LENGTH_CMD SA:"
  1031. " 0x%02x\n", service_action);
  1032. break;
  1033. }
  1034. break;
  1035. case SYNCHRONIZE_CACHE:
  1036. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  1037. if (!dev->transport->do_sync_cache) {
  1038. printk(KERN_ERR
  1039. "SYNCHRONIZE_CACHE emulation not supported"
  1040. " for: %s\n", dev->transport->name);
  1041. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1042. }
  1043. dev->transport->do_sync_cache(task);
  1044. break;
  1045. case ALLOW_MEDIUM_REMOVAL:
  1046. case ERASE:
  1047. case REZERO_UNIT:
  1048. case SEEK_10:
  1049. case SPACE:
  1050. case START_STOP:
  1051. case TEST_UNIT_READY:
  1052. case VERIFY:
  1053. case WRITE_FILEMARKS:
  1054. break;
  1055. default:
  1056. printk(KERN_ERR "Unsupported SCSI Opcode: 0x%02x for %s\n",
  1057. cmd->t_task_cdb[0], dev->transport->name);
  1058. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1059. }
  1060. if (ret < 0)
  1061. return ret;
  1062. task->task_scsi_status = GOOD;
  1063. transport_complete_task(task, 1);
  1064. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  1065. }