target_core_fabric_lib.c 12 KB

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