target_core_fabric_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*******************************************************************************
  2. * Filename: target_core_fabric_lib.c
  3. *
  4. * This file contains generic high level protocol identifier and PR
  5. * handlers for TCM fabric modules
  6. *
  7. * (c) Copyright 2010-2012 RisingTide Systems LLC.
  8. *
  9. * Nicholas A. Bellinger <nab@linux-iscsi.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. ******************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/export.h>
  31. #include <scsi/scsi.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_fabric.h>
  35. #include <target/target_core_configfs.h>
  36. #include "target_core_internal.h"
  37. #include "target_core_pr.h"
  38. /*
  39. * Handlers for Serial Attached SCSI (SAS)
  40. */
  41. u8 sas_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  42. {
  43. /*
  44. * Return a SAS Serial SCSI Protocol identifier for loopback operations
  45. * This is defined in section 7.5.1 Table 362 in spc4r17
  46. */
  47. return 0x6;
  48. }
  49. EXPORT_SYMBOL(sas_get_fabric_proto_ident);
  50. u32 sas_get_pr_transport_id(
  51. struct se_portal_group *se_tpg,
  52. struct se_node_acl *se_nacl,
  53. struct t10_pr_registration *pr_reg,
  54. int *format_code,
  55. unsigned char *buf)
  56. {
  57. unsigned char *ptr;
  58. int ret;
  59. /*
  60. * Set PROTOCOL IDENTIFIER to 6h for SAS
  61. */
  62. buf[0] = 0x06;
  63. /*
  64. * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
  65. * over SAS Serial SCSI Protocol
  66. */
  67. ptr = &se_nacl->initiatorname[4]; /* Skip over 'naa. prefix */
  68. ret = hex2bin(&buf[4], ptr, 8);
  69. if (ret < 0)
  70. pr_debug("sas transport_id: invalid hex string\n");
  71. /*
  72. * The SAS Transport ID is a hardcoded 24-byte length
  73. */
  74. return 24;
  75. }
  76. EXPORT_SYMBOL(sas_get_pr_transport_id);
  77. u32 sas_get_pr_transport_id_len(
  78. struct se_portal_group *se_tpg,
  79. struct se_node_acl *se_nacl,
  80. struct t10_pr_registration *pr_reg,
  81. int *format_code)
  82. {
  83. *format_code = 0;
  84. /*
  85. * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
  86. * over SAS Serial SCSI Protocol
  87. *
  88. * The SAS Transport ID is a hardcoded 24-byte length
  89. */
  90. return 24;
  91. }
  92. EXPORT_SYMBOL(sas_get_pr_transport_id_len);
  93. /*
  94. * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
  95. * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
  96. */
  97. char *sas_parse_pr_out_transport_id(
  98. struct se_portal_group *se_tpg,
  99. const char *buf,
  100. u32 *out_tid_len,
  101. char **port_nexus_ptr)
  102. {
  103. /*
  104. * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
  105. * for initiator ports using SCSI over SAS Serial SCSI Protocol
  106. *
  107. * The TransportID for a SAS Initiator Port is of fixed size of
  108. * 24 bytes, and SAS does not contain a I_T nexus identifier,
  109. * so we return the **port_nexus_ptr set to NULL.
  110. */
  111. *port_nexus_ptr = NULL;
  112. *out_tid_len = 24;
  113. return (char *)&buf[4];
  114. }
  115. EXPORT_SYMBOL(sas_parse_pr_out_transport_id);
  116. /*
  117. * Handlers for Fibre Channel Protocol (FCP)
  118. */
  119. u8 fc_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  120. {
  121. return 0x0; /* 0 = fcp-2 per SPC4 section 7.5.1 */
  122. }
  123. EXPORT_SYMBOL(fc_get_fabric_proto_ident);
  124. u32 fc_get_pr_transport_id_len(
  125. struct se_portal_group *se_tpg,
  126. struct se_node_acl *se_nacl,
  127. struct t10_pr_registration *pr_reg,
  128. int *format_code)
  129. {
  130. *format_code = 0;
  131. /*
  132. * The FC Transport ID is a hardcoded 24-byte length
  133. */
  134. return 24;
  135. }
  136. EXPORT_SYMBOL(fc_get_pr_transport_id_len);
  137. u32 fc_get_pr_transport_id(
  138. struct se_portal_group *se_tpg,
  139. struct se_node_acl *se_nacl,
  140. struct t10_pr_registration *pr_reg,
  141. int *format_code,
  142. unsigned char *buf)
  143. {
  144. unsigned char *ptr;
  145. int i, ret;
  146. u32 off = 8;
  147. /*
  148. * PROTOCOL IDENTIFIER is 0h for FCP-2
  149. *
  150. * From spc4r17, 7.5.4.2 TransportID for initiator ports using
  151. * SCSI over Fibre Channel
  152. *
  153. * We convert the ASCII formatted N Port name into a binary
  154. * encoded TransportID.
  155. */
  156. ptr = &se_nacl->initiatorname[0];
  157. for (i = 0; i < 24; ) {
  158. if (!strncmp(&ptr[i], ":", 1)) {
  159. i++;
  160. continue;
  161. }
  162. ret = hex2bin(&buf[off++], &ptr[i], 1);
  163. if (ret < 0)
  164. pr_debug("fc transport_id: invalid hex string\n");
  165. i += 2;
  166. }
  167. /*
  168. * The FC Transport ID is a hardcoded 24-byte length
  169. */
  170. return 24;
  171. }
  172. EXPORT_SYMBOL(fc_get_pr_transport_id);
  173. char *fc_parse_pr_out_transport_id(
  174. struct se_portal_group *se_tpg,
  175. const char *buf,
  176. u32 *out_tid_len,
  177. char **port_nexus_ptr)
  178. {
  179. /*
  180. * The TransportID for a FC N Port is of fixed size of
  181. * 24 bytes, and FC does not contain a I_T nexus identifier,
  182. * so we return the **port_nexus_ptr set to NULL.
  183. */
  184. *port_nexus_ptr = NULL;
  185. *out_tid_len = 24;
  186. return (char *)&buf[8];
  187. }
  188. EXPORT_SYMBOL(fc_parse_pr_out_transport_id);
  189. /*
  190. * Handlers for Internet Small Computer Systems Interface (iSCSI)
  191. */
  192. u8 iscsi_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  193. {
  194. /*
  195. * This value is defined for "Internet SCSI (iSCSI)"
  196. * in spc4r17 section 7.5.1 Table 362
  197. */
  198. return 0x5;
  199. }
  200. EXPORT_SYMBOL(iscsi_get_fabric_proto_ident);
  201. u32 iscsi_get_pr_transport_id(
  202. struct se_portal_group *se_tpg,
  203. struct se_node_acl *se_nacl,
  204. struct t10_pr_registration *pr_reg,
  205. int *format_code,
  206. unsigned char *buf)
  207. {
  208. u32 off = 4, padding = 0;
  209. u16 len = 0;
  210. spin_lock_irq(&se_nacl->nacl_sess_lock);
  211. /*
  212. * Set PROTOCOL IDENTIFIER to 5h for iSCSI
  213. */
  214. buf[0] = 0x05;
  215. /*
  216. * From spc4r17 Section 7.5.4.6: TransportID for initiator
  217. * ports using SCSI over iSCSI.
  218. *
  219. * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
  220. * shall contain the iSCSI name of an iSCSI initiator node (see
  221. * RFC 3720). The first ISCSI NAME field byte containing an ASCII
  222. * null character terminates the ISCSI NAME field without regard for
  223. * the specified length of the iSCSI TransportID or the contents of
  224. * the ADDITIONAL LENGTH field.
  225. */
  226. len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
  227. /*
  228. * Add Extra byte for NULL terminator
  229. */
  230. len++;
  231. /*
  232. * If there is ISID present with the registration and *format code == 1
  233. * 1, use iSCSI Initiator port TransportID format.
  234. *
  235. * Otherwise use iSCSI Initiator device TransportID format that
  236. * does not contain the ASCII encoded iSCSI Initiator iSID value
  237. * provied by the iSCSi Initiator during the iSCSI login process.
  238. */
  239. if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
  240. /*
  241. * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
  242. * format.
  243. */
  244. buf[0] |= 0x40;
  245. /*
  246. * From spc4r17 Section 7.5.4.6: TransportID for initiator
  247. * ports using SCSI over iSCSI. Table 390
  248. *
  249. * The SEPARATOR field shall contain the five ASCII
  250. * characters ",i,0x".
  251. *
  252. * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
  253. * field shall contain the iSCSI initiator session identifier
  254. * (see RFC 3720) in the form of ASCII characters that are the
  255. * hexadecimal digits converted from the binary iSCSI initiator
  256. * session identifier value. The first ISCSI INITIATOR SESSION
  257. * ID field byte containing an ASCII null character
  258. */
  259. buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
  260. buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
  261. buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
  262. buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
  263. buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
  264. len += 5;
  265. buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
  266. buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
  267. buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
  268. buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
  269. buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
  270. buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
  271. buf[off+len] = '\0'; off++;
  272. len += 7;
  273. }
  274. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  275. /*
  276. * The ADDITIONAL LENGTH field specifies the number of bytes that follow
  277. * in the TransportID. The additional length shall be at least 20 and
  278. * shall be a multiple of four.
  279. */
  280. padding = ((-len) & 3);
  281. if (padding != 0)
  282. len += padding;
  283. buf[2] = ((len >> 8) & 0xff);
  284. buf[3] = (len & 0xff);
  285. /*
  286. * Increment value for total payload + header length for
  287. * full status descriptor
  288. */
  289. len += 4;
  290. return len;
  291. }
  292. EXPORT_SYMBOL(iscsi_get_pr_transport_id);
  293. u32 iscsi_get_pr_transport_id_len(
  294. struct se_portal_group *se_tpg,
  295. struct se_node_acl *se_nacl,
  296. struct t10_pr_registration *pr_reg,
  297. int *format_code)
  298. {
  299. u32 len = 0, padding = 0;
  300. spin_lock_irq(&se_nacl->nacl_sess_lock);
  301. len = strlen(se_nacl->initiatorname);
  302. /*
  303. * Add extra byte for NULL terminator
  304. */
  305. len++;
  306. /*
  307. * If there is ISID present with the registration, use format code:
  308. * 01b: iSCSI Initiator port TransportID format
  309. *
  310. * If there is not an active iSCSI session, use format code:
  311. * 00b: iSCSI Initiator device TransportID format
  312. */
  313. if (pr_reg->isid_present_at_reg) {
  314. len += 5; /* For ",i,0x" ASCII separator */
  315. len += 7; /* For iSCSI Initiator Session ID + Null terminator */
  316. *format_code = 1;
  317. } else
  318. *format_code = 0;
  319. spin_unlock_irq(&se_nacl->nacl_sess_lock);
  320. /*
  321. * The ADDITIONAL LENGTH field specifies the number of bytes that follow
  322. * in the TransportID. The additional length shall be at least 20 and
  323. * shall be a multiple of four.
  324. */
  325. padding = ((-len) & 3);
  326. if (padding != 0)
  327. len += padding;
  328. /*
  329. * Increment value for total payload + header length for
  330. * full status descriptor
  331. */
  332. len += 4;
  333. return len;
  334. }
  335. EXPORT_SYMBOL(iscsi_get_pr_transport_id_len);
  336. char *iscsi_parse_pr_out_transport_id(
  337. struct se_portal_group *se_tpg,
  338. const char *buf,
  339. u32 *out_tid_len,
  340. char **port_nexus_ptr)
  341. {
  342. char *p;
  343. u32 tid_len, padding;
  344. int i;
  345. u16 add_len;
  346. u8 format_code = (buf[0] & 0xc0);
  347. /*
  348. * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
  349. *
  350. * TransportID for initiator ports using SCSI over iSCSI,
  351. * from Table 388 -- iSCSI TransportID formats.
  352. *
  353. * 00b Initiator port is identified using the world wide unique
  354. * SCSI device name of the iSCSI initiator
  355. * device containing the initiator port (see table 389).
  356. * 01b Initiator port is identified using the world wide unique
  357. * initiator port identifier (see table 390).10b to 11b
  358. * Reserved
  359. */
  360. if ((format_code != 0x00) && (format_code != 0x40)) {
  361. pr_err("Illegal format code: 0x%02x for iSCSI"
  362. " Initiator Transport ID\n", format_code);
  363. return NULL;
  364. }
  365. /*
  366. * If the caller wants the TransportID Length, we set that value for the
  367. * entire iSCSI Tarnsport ID now.
  368. */
  369. if (out_tid_len != NULL) {
  370. add_len = ((buf[2] >> 8) & 0xff);
  371. add_len |= (buf[3] & 0xff);
  372. tid_len = strlen(&buf[4]);
  373. tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
  374. tid_len += 1; /* Add one byte for NULL terminator */
  375. padding = ((-tid_len) & 3);
  376. if (padding != 0)
  377. tid_len += padding;
  378. if ((add_len + 4) != tid_len) {
  379. pr_debug("LIO-Target Extracted add_len: %hu "
  380. "does not match calculated tid_len: %u,"
  381. " using tid_len instead\n", add_len+4, tid_len);
  382. *out_tid_len = tid_len;
  383. } else
  384. *out_tid_len = (add_len + 4);
  385. }
  386. /*
  387. * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
  388. * Session ID as defined in Table 390 - iSCSI initiator port TransportID
  389. * format.
  390. */
  391. if (format_code == 0x40) {
  392. p = strstr(&buf[4], ",i,0x");
  393. if (!p) {
  394. pr_err("Unable to locate \",i,0x\" separator"
  395. " for Initiator port identifier: %s\n",
  396. &buf[4]);
  397. return NULL;
  398. }
  399. *p = '\0'; /* Terminate iSCSI Name */
  400. p += 5; /* Skip over ",i,0x" separator */
  401. *port_nexus_ptr = p;
  402. /*
  403. * Go ahead and do the lower case conversion of the received
  404. * 12 ASCII characters representing the ISID in the TransportID
  405. * for comparison against the running iSCSI session's ISID from
  406. * iscsi_target.c:lio_sess_get_initiator_sid()
  407. */
  408. for (i = 0; i < 12; i++) {
  409. if (isdigit(*p)) {
  410. p++;
  411. continue;
  412. }
  413. *p = tolower(*p);
  414. p++;
  415. }
  416. }
  417. return (char *)&buf[4];
  418. }
  419. EXPORT_SYMBOL(iscsi_parse_pr_out_transport_id);