PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/target/target_core_fabric_lib.c

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