target_core_fabric_lib.c 12 KB

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