target_core_cdb.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. u32 blocks = dev->transport->get_blocks(dev);
  598. buf[0] = (blocks >> 24) & 0xff;
  599. buf[1] = (blocks >> 16) & 0xff;
  600. buf[2] = (blocks >> 8) & 0xff;
  601. buf[3] = blocks & 0xff;
  602. buf[4] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
  603. buf[5] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
  604. buf[6] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
  605. buf[7] = DEV_ATTRIB(dev)->block_size & 0xff;
  606. /*
  607. * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
  608. */
  609. if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
  610. put_unaligned_be32(0xFFFFFFFF, &buf[0]);
  611. return 0;
  612. }
  613. static int
  614. target_emulate_readcapacity_16(struct se_cmd *cmd)
  615. {
  616. struct se_device *dev = SE_DEV(cmd);
  617. unsigned char *buf = cmd->t_task->t_task_buf;
  618. unsigned long long blocks = dev->transport->get_blocks(dev);
  619. buf[0] = (blocks >> 56) & 0xff;
  620. buf[1] = (blocks >> 48) & 0xff;
  621. buf[2] = (blocks >> 40) & 0xff;
  622. buf[3] = (blocks >> 32) & 0xff;
  623. buf[4] = (blocks >> 24) & 0xff;
  624. buf[5] = (blocks >> 16) & 0xff;
  625. buf[6] = (blocks >> 8) & 0xff;
  626. buf[7] = blocks & 0xff;
  627. buf[8] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
  628. buf[9] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
  629. buf[10] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
  630. buf[11] = DEV_ATTRIB(dev)->block_size & 0xff;
  631. /*
  632. * Set Thin Provisioning Enable bit following sbc3r22 in section
  633. * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
  634. */
  635. if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
  636. buf[14] = 0x80;
  637. return 0;
  638. }
  639. static int
  640. target_modesense_rwrecovery(unsigned char *p)
  641. {
  642. p[0] = 0x01;
  643. p[1] = 0x0a;
  644. return 12;
  645. }
  646. static int
  647. target_modesense_control(struct se_device *dev, unsigned char *p)
  648. {
  649. p[0] = 0x0a;
  650. p[1] = 0x0a;
  651. p[2] = 2;
  652. /*
  653. * From spc4r17, section 7.4.6 Control mode Page
  654. *
  655. * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
  656. *
  657. * 00b: The logical unit shall clear any unit attention condition
  658. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  659. * status and shall not establish a unit attention condition when a com-
  660. * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
  661. * status.
  662. *
  663. * 10b: The logical unit shall not clear any unit attention condition
  664. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  665. * status and shall not establish a unit attention condition when
  666. * a command is completed with BUSY, TASK SET FULL, or RESERVATION
  667. * CONFLICT status.
  668. *
  669. * 11b a The logical unit shall not clear any unit attention condition
  670. * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
  671. * status and shall establish a unit attention condition for the
  672. * initiator port associated with the I_T nexus on which the BUSY,
  673. * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
  674. * Depending on the status, the additional sense code shall be set to
  675. * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
  676. * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
  677. * command, a unit attention condition shall be established only once
  678. * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
  679. * to the number of commands completed with one of those status codes.
  680. */
  681. p[4] = (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 2) ? 0x30 :
  682. (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
  683. /*
  684. * From spc4r17, section 7.4.6 Control mode Page
  685. *
  686. * Task Aborted Status (TAS) bit set to zero.
  687. *
  688. * A task aborted status (TAS) bit set to zero specifies that aborted
  689. * tasks shall be terminated by the device server without any response
  690. * to the application client. A TAS bit set to one specifies that tasks
  691. * aborted by the actions of an I_T nexus other than the I_T nexus on
  692. * which the command was received shall be completed with TASK ABORTED
  693. * status (see SAM-4).
  694. */
  695. p[5] = (DEV_ATTRIB(dev)->emulate_tas) ? 0x40 : 0x00;
  696. p[8] = 0xff;
  697. p[9] = 0xff;
  698. p[11] = 30;
  699. return 12;
  700. }
  701. static int
  702. target_modesense_caching(struct se_device *dev, unsigned char *p)
  703. {
  704. p[0] = 0x08;
  705. p[1] = 0x12;
  706. if (DEV_ATTRIB(dev)->emulate_write_cache > 0)
  707. p[2] = 0x04; /* Write Cache Enable */
  708. p[12] = 0x20; /* Disabled Read Ahead */
  709. return 20;
  710. }
  711. static void
  712. target_modesense_write_protect(unsigned char *buf, int type)
  713. {
  714. /*
  715. * I believe that the WP bit (bit 7) in the mode header is the same for
  716. * all device types..
  717. */
  718. switch (type) {
  719. case TYPE_DISK:
  720. case TYPE_TAPE:
  721. default:
  722. buf[0] |= 0x80; /* WP bit */
  723. break;
  724. }
  725. }
  726. static void
  727. target_modesense_dpofua(unsigned char *buf, int type)
  728. {
  729. switch (type) {
  730. case TYPE_DISK:
  731. buf[0] |= 0x10; /* DPOFUA bit */
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. static int
  738. target_emulate_modesense(struct se_cmd *cmd, int ten)
  739. {
  740. struct se_device *dev = SE_DEV(cmd);
  741. char *cdb = cmd->t_task->t_task_cdb;
  742. unsigned char *rbuf = cmd->t_task->t_task_buf;
  743. int type = dev->transport->get_device_type(dev);
  744. int offset = (ten) ? 8 : 4;
  745. int length = 0;
  746. unsigned char buf[SE_MODE_PAGE_BUF];
  747. memset(buf, 0, SE_MODE_PAGE_BUF);
  748. switch (cdb[2] & 0x3f) {
  749. case 0x01:
  750. length = target_modesense_rwrecovery(&buf[offset]);
  751. break;
  752. case 0x08:
  753. length = target_modesense_caching(dev, &buf[offset]);
  754. break;
  755. case 0x0a:
  756. length = target_modesense_control(dev, &buf[offset]);
  757. break;
  758. case 0x3f:
  759. length = target_modesense_rwrecovery(&buf[offset]);
  760. length += target_modesense_caching(dev, &buf[offset+length]);
  761. length += target_modesense_control(dev, &buf[offset+length]);
  762. break;
  763. default:
  764. printk(KERN_ERR "Got Unknown Mode Page: 0x%02x\n",
  765. cdb[2] & 0x3f);
  766. return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
  767. }
  768. offset += length;
  769. if (ten) {
  770. offset -= 2;
  771. buf[0] = (offset >> 8) & 0xff;
  772. buf[1] = offset & 0xff;
  773. if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  774. (cmd->se_deve &&
  775. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  776. target_modesense_write_protect(&buf[3], type);
  777. if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
  778. (DEV_ATTRIB(dev)->emulate_fua_write > 0))
  779. target_modesense_dpofua(&buf[3], type);
  780. if ((offset + 2) > cmd->data_length)
  781. offset = cmd->data_length;
  782. } else {
  783. offset -= 1;
  784. buf[0] = offset & 0xff;
  785. if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
  786. (cmd->se_deve &&
  787. (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
  788. target_modesense_write_protect(&buf[2], type);
  789. if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
  790. (DEV_ATTRIB(dev)->emulate_fua_write > 0))
  791. target_modesense_dpofua(&buf[2], type);
  792. if ((offset + 1) > cmd->data_length)
  793. offset = cmd->data_length;
  794. }
  795. memcpy(rbuf, buf, offset);
  796. return 0;
  797. }
  798. static int
  799. target_emulate_request_sense(struct se_cmd *cmd)
  800. {
  801. unsigned char *cdb = cmd->t_task->t_task_cdb;
  802. unsigned char *buf = cmd->t_task->t_task_buf;
  803. u8 ua_asc = 0, ua_ascq = 0;
  804. if (cdb[1] & 0x01) {
  805. printk(KERN_ERR "REQUEST_SENSE description emulation not"
  806. " supported\n");
  807. return PYX_TRANSPORT_INVALID_CDB_FIELD;
  808. }
  809. if (!(core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))) {
  810. /*
  811. * CURRENT ERROR, UNIT ATTENTION
  812. */
  813. buf[0] = 0x70;
  814. buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
  815. /*
  816. * Make sure request data length is enough for additional
  817. * sense data.
  818. */
  819. if (cmd->data_length <= 18) {
  820. buf[7] = 0x00;
  821. return 0;
  822. }
  823. /*
  824. * The Additional Sense Code (ASC) from the UNIT ATTENTION
  825. */
  826. buf[SPC_ASC_KEY_OFFSET] = ua_asc;
  827. buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
  828. buf[7] = 0x0A;
  829. } else {
  830. /*
  831. * CURRENT ERROR, NO SENSE
  832. */
  833. buf[0] = 0x70;
  834. buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
  835. /*
  836. * Make sure request data length is enough for additional
  837. * sense data.
  838. */
  839. if (cmd->data_length <= 18) {
  840. buf[7] = 0x00;
  841. return 0;
  842. }
  843. /*
  844. * NO ADDITIONAL SENSE INFORMATION
  845. */
  846. buf[SPC_ASC_KEY_OFFSET] = 0x00;
  847. buf[7] = 0x0A;
  848. }
  849. return 0;
  850. }
  851. /*
  852. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  853. * Note this is not used for TCM/pSCSI passthrough
  854. */
  855. static int
  856. target_emulate_unmap(struct se_task *task)
  857. {
  858. struct se_cmd *cmd = TASK_CMD(task);
  859. struct se_device *dev = SE_DEV(cmd);
  860. unsigned char *buf = cmd->t_task->t_task_buf, *ptr = NULL;
  861. unsigned char *cdb = &cmd->t_task->t_task_cdb[0];
  862. sector_t lba;
  863. unsigned int size = cmd->data_length, range;
  864. int ret, offset;
  865. unsigned short dl, bd_dl;
  866. /* First UNMAP block descriptor starts at 8 byte offset */
  867. offset = 8;
  868. size -= 8;
  869. dl = get_unaligned_be16(&cdb[0]);
  870. bd_dl = get_unaligned_be16(&cdb[2]);
  871. ptr = &buf[offset];
  872. printk(KERN_INFO "UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  873. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  874. while (size) {
  875. lba = get_unaligned_be64(&ptr[0]);
  876. range = get_unaligned_be32(&ptr[8]);
  877. printk(KERN_INFO "UNMAP: Using lba: %llu and range: %u\n",
  878. (unsigned long long)lba, range);
  879. ret = dev->transport->do_discard(dev, lba, range);
  880. if (ret < 0) {
  881. printk(KERN_ERR "blkdev_issue_discard() failed: %d\n",
  882. ret);
  883. return -1;
  884. }
  885. ptr += 16;
  886. size -= 16;
  887. }
  888. task->task_scsi_status = GOOD;
  889. transport_complete_task(task, 1);
  890. return 0;
  891. }
  892. /*
  893. * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
  894. * Note this is not used for TCM/pSCSI passthrough
  895. */
  896. static int
  897. target_emulate_write_same(struct se_task *task)
  898. {
  899. struct se_cmd *cmd = TASK_CMD(task);
  900. struct se_device *dev = SE_DEV(cmd);
  901. sector_t lba = cmd->t_task->t_task_lba;
  902. unsigned int range;
  903. int ret;
  904. range = (cmd->data_length / DEV_ATTRIB(dev)->block_size);
  905. printk(KERN_INFO "WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
  906. (unsigned long long)lba, range);
  907. ret = dev->transport->do_discard(dev, lba, range);
  908. if (ret < 0) {
  909. printk(KERN_INFO "blkdev_issue_discard() failed for WRITE_SAME\n");
  910. return -1;
  911. }
  912. task->task_scsi_status = GOOD;
  913. transport_complete_task(task, 1);
  914. return 0;
  915. }
  916. int
  917. transport_emulate_control_cdb(struct se_task *task)
  918. {
  919. struct se_cmd *cmd = TASK_CMD(task);
  920. struct se_device *dev = SE_DEV(cmd);
  921. unsigned short service_action;
  922. int ret = 0;
  923. switch (cmd->t_task->t_task_cdb[0]) {
  924. case INQUIRY:
  925. ret = target_emulate_inquiry(cmd);
  926. break;
  927. case READ_CAPACITY:
  928. ret = target_emulate_readcapacity(cmd);
  929. break;
  930. case MODE_SENSE:
  931. ret = target_emulate_modesense(cmd, 0);
  932. break;
  933. case MODE_SENSE_10:
  934. ret = target_emulate_modesense(cmd, 1);
  935. break;
  936. case SERVICE_ACTION_IN:
  937. switch (cmd->t_task->t_task_cdb[1] & 0x1f) {
  938. case SAI_READ_CAPACITY_16:
  939. ret = target_emulate_readcapacity_16(cmd);
  940. break;
  941. default:
  942. printk(KERN_ERR "Unsupported SA: 0x%02x\n",
  943. cmd->t_task->t_task_cdb[1] & 0x1f);
  944. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  945. }
  946. break;
  947. case REQUEST_SENSE:
  948. ret = target_emulate_request_sense(cmd);
  949. break;
  950. case UNMAP:
  951. if (!dev->transport->do_discard) {
  952. printk(KERN_ERR "UNMAP emulation not supported for: %s\n",
  953. dev->transport->name);
  954. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  955. }
  956. ret = target_emulate_unmap(task);
  957. break;
  958. case WRITE_SAME_16:
  959. if (!dev->transport->do_discard) {
  960. printk(KERN_ERR "WRITE_SAME_16 emulation not supported"
  961. " for: %s\n", dev->transport->name);
  962. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  963. }
  964. ret = target_emulate_write_same(task);
  965. break;
  966. case VARIABLE_LENGTH_CMD:
  967. service_action =
  968. get_unaligned_be16(&cmd->t_task->t_task_cdb[8]);
  969. switch (service_action) {
  970. case WRITE_SAME_32:
  971. if (!dev->transport->do_discard) {
  972. printk(KERN_ERR "WRITE_SAME_32 SA emulation not"
  973. " supported for: %s\n",
  974. dev->transport->name);
  975. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  976. }
  977. ret = target_emulate_write_same(task);
  978. break;
  979. default:
  980. printk(KERN_ERR "Unsupported VARIABLE_LENGTH_CMD SA:"
  981. " 0x%02x\n", service_action);
  982. break;
  983. }
  984. break;
  985. case SYNCHRONIZE_CACHE:
  986. case 0x91: /* SYNCHRONIZE_CACHE_16: */
  987. if (!dev->transport->do_sync_cache) {
  988. printk(KERN_ERR
  989. "SYNCHRONIZE_CACHE emulation not supported"
  990. " for: %s\n", dev->transport->name);
  991. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  992. }
  993. dev->transport->do_sync_cache(task);
  994. break;
  995. case ALLOW_MEDIUM_REMOVAL:
  996. case ERASE:
  997. case REZERO_UNIT:
  998. case SEEK_10:
  999. case SPACE:
  1000. case START_STOP:
  1001. case TEST_UNIT_READY:
  1002. case VERIFY:
  1003. case WRITE_FILEMARKS:
  1004. break;
  1005. default:
  1006. printk(KERN_ERR "Unsupported SCSI Opcode: 0x%02x for %s\n",
  1007. cmd->t_task->t_task_cdb[0], dev->transport->name);
  1008. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  1009. }
  1010. if (ret < 0)
  1011. return ret;
  1012. task->task_scsi_status = GOOD;
  1013. transport_complete_task(task, 1);
  1014. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  1015. }