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