target_core_fabric_lib.c 12 KB

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