target_core_cdb.c 30 KB

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