target_core_cdb.c 35 KB

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