target_core_fabric_lib.c 12 KB

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