target_core_cdb.c 36 KB

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