target_core_cdb.c 35 KB

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