11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_fabric_lib.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains generic high level protocol identifier and PR
6c66ac9dbSNicholas Bellinger  * handlers for TCM fabric modules
7c66ac9dbSNicholas Bellinger  *
84c76251eSNicholas Bellinger  * (c) Copyright 2010-2013 Datera, Inc.
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  ******************************************************************************/
13c66ac9dbSNicholas Bellinger 
142650d71eSChristoph Hellwig /*
152650d71eSChristoph Hellwig  * See SPC4, section 7.5 "Protocol specific parameters" for details
162650d71eSChristoph Hellwig  * on the formats implemented in this file.
172650d71eSChristoph Hellwig  */
182650d71eSChristoph Hellwig 
1911650b85SAndy Shevchenko #include <linux/kernel.h>
20c66ac9dbSNicholas Bellinger #include <linux/string.h>
21c66ac9dbSNicholas Bellinger #include <linux/ctype.h>
22c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
23c53181afSPaul Gortmaker #include <linux/export.h>
24a85d667eSBart Van Assche #include <asm/unaligned.h>
25c66ac9dbSNicholas Bellinger 
266546a02aSStephen Rothwell #include <scsi/scsi_proto.h>
276546a02aSStephen Rothwell 
28c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
29c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
30c66ac9dbSNicholas Bellinger 
31e26d99aeSChristoph Hellwig #include "target_core_internal.h"
32c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
33c66ac9dbSNicholas Bellinger 
34c66ac9dbSNicholas Bellinger 
352650d71eSChristoph Hellwig static int sas_get_pr_transport_id(
362650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
37c66ac9dbSNicholas Bellinger 	int *format_code,
38c66ac9dbSNicholas Bellinger 	unsigned char *buf)
39c66ac9dbSNicholas Bellinger {
408c35ad20SMimi Zohar 	int ret;
4111650b85SAndy Shevchenko 
422650d71eSChristoph Hellwig 	/* Skip over 'naa. prefix */
432650d71eSChristoph Hellwig 	ret = hex2bin(&buf[4], &nacl->initiatorname[4], 8);
442650d71eSChristoph Hellwig 	if (ret) {
452650d71eSChristoph Hellwig 		pr_debug("%s: invalid hex string\n", __func__);
462650d71eSChristoph Hellwig 		return ret;
472650d71eSChristoph Hellwig 	}
48c66ac9dbSNicholas Bellinger 
49c66ac9dbSNicholas Bellinger 	return 24;
50c66ac9dbSNicholas Bellinger }
51c66ac9dbSNicholas Bellinger 
522650d71eSChristoph Hellwig static int fc_get_pr_transport_id(
53c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
54c66ac9dbSNicholas Bellinger 	int *format_code,
55c66ac9dbSNicholas Bellinger 	unsigned char *buf)
56c66ac9dbSNicholas Bellinger {
5711650b85SAndy Shevchenko 	unsigned char *ptr;
588c35ad20SMimi Zohar 	int i, ret;
59c66ac9dbSNicholas Bellinger 	u32 off = 8;
608c35ad20SMimi Zohar 
61c66ac9dbSNicholas Bellinger 	/*
62c66ac9dbSNicholas Bellinger 	 * We convert the ASCII formatted N Port name into a binary
63c66ac9dbSNicholas Bellinger 	 * encoded TransportID.
64c66ac9dbSNicholas Bellinger 	 */
65c66ac9dbSNicholas Bellinger 	ptr = &se_nacl->initiatorname[0];
668fed04ebSBodo Stroesser 	for (i = 0; i < 23; ) {
676708bb27SAndy Grover 		if (!strncmp(&ptr[i], ":", 1)) {
68c66ac9dbSNicholas Bellinger 			i++;
69c66ac9dbSNicholas Bellinger 			continue;
70c66ac9dbSNicholas Bellinger 		}
718c35ad20SMimi Zohar 		ret = hex2bin(&buf[off++], &ptr[i], 1);
722650d71eSChristoph Hellwig 		if (ret < 0) {
732650d71eSChristoph Hellwig 			pr_debug("%s: invalid hex string\n", __func__);
742650d71eSChristoph Hellwig 			return ret;
752650d71eSChristoph Hellwig 		}
76c66ac9dbSNicholas Bellinger 		i += 2;
77c66ac9dbSNicholas Bellinger 	}
78c66ac9dbSNicholas Bellinger 	/*
79c66ac9dbSNicholas Bellinger 	 * The FC Transport ID is a hardcoded 24-byte length
80c66ac9dbSNicholas Bellinger 	 */
81c66ac9dbSNicholas Bellinger 	return 24;
82c66ac9dbSNicholas Bellinger }
83c66ac9dbSNicholas Bellinger 
842650d71eSChristoph Hellwig static int sbp_get_pr_transport_id(
852650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
862650d71eSChristoph Hellwig 	int *format_code,
872650d71eSChristoph Hellwig 	unsigned char *buf)
88c66ac9dbSNicholas Bellinger {
892650d71eSChristoph Hellwig 	int ret;
90c66ac9dbSNicholas Bellinger 
912650d71eSChristoph Hellwig 	ret = hex2bin(&buf[8], nacl->initiatorname, 8);
922650d71eSChristoph Hellwig 	if (ret) {
932650d71eSChristoph Hellwig 		pr_debug("%s: invalid hex string\n", __func__);
942650d71eSChristoph Hellwig 		return ret;
95c66ac9dbSNicholas Bellinger 	}
96c66ac9dbSNicholas Bellinger 
972650d71eSChristoph Hellwig 	return 24;
982650d71eSChristoph Hellwig }
99c66ac9dbSNicholas Bellinger 
1002650d71eSChristoph Hellwig static int srp_get_pr_transport_id(
1012650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
1022650d71eSChristoph Hellwig 	int *format_code,
1032650d71eSChristoph Hellwig 	unsigned char *buf)
104c66ac9dbSNicholas Bellinger {
1052650d71eSChristoph Hellwig 	const char *p;
1062650d71eSChristoph Hellwig 	unsigned len, count, leading_zero_bytes;
1072650d71eSChristoph Hellwig 	int rc;
108c66ac9dbSNicholas Bellinger 
1092650d71eSChristoph Hellwig 	p = nacl->initiatorname;
1102650d71eSChristoph Hellwig 	if (strncasecmp(p, "0x", 2) == 0)
1112650d71eSChristoph Hellwig 		p += 2;
1122650d71eSChristoph Hellwig 	len = strlen(p);
1132650d71eSChristoph Hellwig 	if (len % 2)
1142650d71eSChristoph Hellwig 		return -EINVAL;
1152650d71eSChristoph Hellwig 
1162650d71eSChristoph Hellwig 	count = min(len / 2, 16U);
1172650d71eSChristoph Hellwig 	leading_zero_bytes = 16 - count;
1182650d71eSChristoph Hellwig 	memset(buf + 8, 0, leading_zero_bytes);
1192650d71eSChristoph Hellwig 	rc = hex2bin(buf + 8 + leading_zero_bytes, p, count);
1202650d71eSChristoph Hellwig 	if (rc < 0) {
121c941e0d1SBart Van Assche 		pr_debug("hex2bin failed for %s: %d\n", p, rc);
1222650d71eSChristoph Hellwig 		return rc;
1232650d71eSChristoph Hellwig 	}
1242650d71eSChristoph Hellwig 
1252650d71eSChristoph Hellwig 	return 24;
1262650d71eSChristoph Hellwig }
1272650d71eSChristoph Hellwig 
1282650d71eSChristoph Hellwig static int iscsi_get_pr_transport_id(
129c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
130c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
131c66ac9dbSNicholas Bellinger 	int *format_code,
132c66ac9dbSNicholas Bellinger 	unsigned char *buf)
133c66ac9dbSNicholas Bellinger {
134c66ac9dbSNicholas Bellinger 	u32 off = 4, padding = 0;
135c66ac9dbSNicholas Bellinger 	u16 len = 0;
136c66ac9dbSNicholas Bellinger 
137c66ac9dbSNicholas Bellinger 	spin_lock_irq(&se_nacl->nacl_sess_lock);
138c66ac9dbSNicholas Bellinger 	/*
139c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 7.5.4.6: TransportID for initiator
140c66ac9dbSNicholas Bellinger 	 * ports using SCSI over iSCSI.
141c66ac9dbSNicholas Bellinger 	 *
142c66ac9dbSNicholas Bellinger 	 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
143c66ac9dbSNicholas Bellinger 	 * shall contain the iSCSI name of an iSCSI initiator node (see
144c66ac9dbSNicholas Bellinger 	 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
145c66ac9dbSNicholas Bellinger 	 * null character terminates the ISCSI NAME field without regard for
146c66ac9dbSNicholas Bellinger 	 * the specified length of the iSCSI TransportID or the contents of
147c66ac9dbSNicholas Bellinger 	 * the ADDITIONAL LENGTH field.
148c66ac9dbSNicholas Bellinger 	 */
149c66ac9dbSNicholas Bellinger 	len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
150c66ac9dbSNicholas Bellinger 	/*
151c66ac9dbSNicholas Bellinger 	 * Add Extra byte for NULL terminator
152c66ac9dbSNicholas Bellinger 	 */
153c66ac9dbSNicholas Bellinger 	len++;
154c66ac9dbSNicholas Bellinger 	/*
155c66ac9dbSNicholas Bellinger 	 * If there is ISID present with the registration and *format code == 1
156c66ac9dbSNicholas Bellinger 	 * 1, use iSCSI Initiator port TransportID format.
157c66ac9dbSNicholas Bellinger 	 *
158c66ac9dbSNicholas Bellinger 	 * Otherwise use iSCSI Initiator device TransportID format that
159c66ac9dbSNicholas Bellinger 	 * does not contain the ASCII encoded iSCSI Initiator iSID value
160c66ac9dbSNicholas Bellinger 	 * provied by the iSCSi Initiator during the iSCSI login process.
161c66ac9dbSNicholas Bellinger 	 */
162c66ac9dbSNicholas Bellinger 	if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
163c66ac9dbSNicholas Bellinger 		/*
164c66ac9dbSNicholas Bellinger 		 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
165c66ac9dbSNicholas Bellinger 		 * format.
166c66ac9dbSNicholas Bellinger 		 */
167c66ac9dbSNicholas Bellinger 		buf[0] |= 0x40;
168c66ac9dbSNicholas Bellinger 		/*
169c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 7.5.4.6: TransportID for initiator
170c66ac9dbSNicholas Bellinger 		 * ports using SCSI over iSCSI.  Table 390
171c66ac9dbSNicholas Bellinger 		 *
172c66ac9dbSNicholas Bellinger 		 * The SEPARATOR field shall contain the five ASCII
173c66ac9dbSNicholas Bellinger 		 * characters ",i,0x".
174c66ac9dbSNicholas Bellinger 		 *
175c66ac9dbSNicholas Bellinger 		 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
176c66ac9dbSNicholas Bellinger 		 * field shall contain the iSCSI initiator session identifier
177c66ac9dbSNicholas Bellinger 		 * (see RFC 3720) in the form of ASCII characters that are the
178c66ac9dbSNicholas Bellinger 		 * hexadecimal digits converted from the binary iSCSI initiator
179c66ac9dbSNicholas Bellinger 		 * session identifier value. The first ISCSI INITIATOR SESSION
180c66ac9dbSNicholas Bellinger 		 * ID field byte containing an ASCII null character
181c66ac9dbSNicholas Bellinger 		 */
182c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
183c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
184c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
185c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
186c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
187c66ac9dbSNicholas Bellinger 		len += 5;
188c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
189c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
190c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
191c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
192c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
193c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
194c66ac9dbSNicholas Bellinger 		buf[off+len] = '\0'; off++;
195c66ac9dbSNicholas Bellinger 		len += 7;
196c66ac9dbSNicholas Bellinger 	}
197c66ac9dbSNicholas Bellinger 	spin_unlock_irq(&se_nacl->nacl_sess_lock);
198c66ac9dbSNicholas Bellinger 	/*
199c66ac9dbSNicholas Bellinger 	 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
200c66ac9dbSNicholas Bellinger 	 * in the TransportID. The additional length shall be at least 20 and
201c66ac9dbSNicholas Bellinger 	 * shall be a multiple of four.
202c66ac9dbSNicholas Bellinger 	*/
203c66ac9dbSNicholas Bellinger 	padding = ((-len) & 3);
204c66ac9dbSNicholas Bellinger 	if (padding != 0)
205c66ac9dbSNicholas Bellinger 		len += padding;
206c66ac9dbSNicholas Bellinger 
207a85d667eSBart Van Assche 	put_unaligned_be16(len, &buf[2]);
208c66ac9dbSNicholas Bellinger 	/*
209c66ac9dbSNicholas Bellinger 	 * Increment value for total payload + header length for
210c66ac9dbSNicholas Bellinger 	 * full status descriptor
211c66ac9dbSNicholas Bellinger 	 */
212c66ac9dbSNicholas Bellinger 	len += 4;
213c66ac9dbSNicholas Bellinger 
214c66ac9dbSNicholas Bellinger 	return len;
215c66ac9dbSNicholas Bellinger }
216c66ac9dbSNicholas Bellinger 
2172650d71eSChristoph Hellwig static int iscsi_get_pr_transport_id_len(
218c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
219c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
220c66ac9dbSNicholas Bellinger 	int *format_code)
221c66ac9dbSNicholas Bellinger {
222c66ac9dbSNicholas Bellinger 	u32 len = 0, padding = 0;
223c66ac9dbSNicholas Bellinger 
224c66ac9dbSNicholas Bellinger 	spin_lock_irq(&se_nacl->nacl_sess_lock);
225c66ac9dbSNicholas Bellinger 	len = strlen(se_nacl->initiatorname);
226c66ac9dbSNicholas Bellinger 	/*
227c66ac9dbSNicholas Bellinger 	 * Add extra byte for NULL terminator
228c66ac9dbSNicholas Bellinger 	 */
229c66ac9dbSNicholas Bellinger 	len++;
230c66ac9dbSNicholas Bellinger 	/*
231c66ac9dbSNicholas Bellinger 	 * If there is ISID present with the registration, use format code:
232c66ac9dbSNicholas Bellinger 	 * 01b: iSCSI Initiator port TransportID format
233c66ac9dbSNicholas Bellinger 	 *
234c66ac9dbSNicholas Bellinger 	 * If there is not an active iSCSI session, use format code:
235c66ac9dbSNicholas Bellinger 	 * 00b: iSCSI Initiator device TransportID format
236c66ac9dbSNicholas Bellinger 	 */
237c66ac9dbSNicholas Bellinger 	if (pr_reg->isid_present_at_reg) {
23835d1efe8SMasanari Iida 		len += 5; /* For ",i,0x" ASCII separator */
239c66ac9dbSNicholas Bellinger 		len += 7; /* For iSCSI Initiator Session ID + Null terminator */
240c66ac9dbSNicholas Bellinger 		*format_code = 1;
241c66ac9dbSNicholas Bellinger 	} else
242c66ac9dbSNicholas Bellinger 		*format_code = 0;
243c66ac9dbSNicholas Bellinger 	spin_unlock_irq(&se_nacl->nacl_sess_lock);
244c66ac9dbSNicholas Bellinger 	/*
245c66ac9dbSNicholas Bellinger 	 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
246c66ac9dbSNicholas Bellinger 	 * in the TransportID. The additional length shall be at least 20 and
247c66ac9dbSNicholas Bellinger 	 * shall be a multiple of four.
248c66ac9dbSNicholas Bellinger 	 */
249c66ac9dbSNicholas Bellinger 	padding = ((-len) & 3);
250c66ac9dbSNicholas Bellinger 	if (padding != 0)
251c66ac9dbSNicholas Bellinger 		len += padding;
252c66ac9dbSNicholas Bellinger 	/*
253c66ac9dbSNicholas Bellinger 	 * Increment value for total payload + header length for
254c66ac9dbSNicholas Bellinger 	 * full status descriptor
255c66ac9dbSNicholas Bellinger 	 */
256c66ac9dbSNicholas Bellinger 	len += 4;
257c66ac9dbSNicholas Bellinger 
258c66ac9dbSNicholas Bellinger 	return len;
259c66ac9dbSNicholas Bellinger }
260c66ac9dbSNicholas Bellinger 
2612650d71eSChristoph Hellwig static char *iscsi_parse_pr_out_transport_id(
262c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg,
263094bb5d7SRasmus Villemoes 	char *buf,
264c66ac9dbSNicholas Bellinger 	u32 *out_tid_len,
265c66ac9dbSNicholas Bellinger 	char **port_nexus_ptr)
266c66ac9dbSNicholas Bellinger {
267c66ac9dbSNicholas Bellinger 	char *p;
268c66ac9dbSNicholas Bellinger 	u32 tid_len, padding;
269c66ac9dbSNicholas Bellinger 	int i;
270c66ac9dbSNicholas Bellinger 	u16 add_len;
271c66ac9dbSNicholas Bellinger 	u8 format_code = (buf[0] & 0xc0);
272c66ac9dbSNicholas Bellinger 	/*
273c66ac9dbSNicholas Bellinger 	 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
274c66ac9dbSNicholas Bellinger 	 *
275c66ac9dbSNicholas Bellinger 	 *       TransportID for initiator ports using SCSI over iSCSI,
276c66ac9dbSNicholas Bellinger 	 *       from Table 388 -- iSCSI TransportID formats.
277c66ac9dbSNicholas Bellinger 	 *
278c66ac9dbSNicholas Bellinger 	 *    00b     Initiator port is identified using the world wide unique
279c66ac9dbSNicholas Bellinger 	 *            SCSI device name of the iSCSI initiator
280c66ac9dbSNicholas Bellinger 	 *            device containing the initiator port (see table 389).
281c66ac9dbSNicholas Bellinger 	 *    01b     Initiator port is identified using the world wide unique
282c66ac9dbSNicholas Bellinger 	 *            initiator port identifier (see table 390).10b to 11b
283c66ac9dbSNicholas Bellinger 	 *            Reserved
284c66ac9dbSNicholas Bellinger 	 */
285c66ac9dbSNicholas Bellinger 	if ((format_code != 0x00) && (format_code != 0x40)) {
2866708bb27SAndy Grover 		pr_err("Illegal format code: 0x%02x for iSCSI"
287c66ac9dbSNicholas Bellinger 			" Initiator Transport ID\n", format_code);
288c66ac9dbSNicholas Bellinger 		return NULL;
289c66ac9dbSNicholas Bellinger 	}
290c66ac9dbSNicholas Bellinger 	/*
291c66ac9dbSNicholas Bellinger 	 * If the caller wants the TransportID Length, we set that value for the
292c66ac9dbSNicholas Bellinger 	 * entire iSCSI Tarnsport ID now.
293c66ac9dbSNicholas Bellinger 	 */
29468edbce4SJoern Engel 	if (out_tid_len) {
29568edbce4SJoern Engel 		/* The shift works thanks to integer promotion rules */
296a85d667eSBart Van Assche 		add_len = get_unaligned_be16(&buf[2]);
297c66ac9dbSNicholas Bellinger 
2988359cf43SJörn Engel 		tid_len = strlen(&buf[4]);
299c66ac9dbSNicholas Bellinger 		tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
300c66ac9dbSNicholas Bellinger 		tid_len += 1; /* Add one byte for NULL terminator */
301c66ac9dbSNicholas Bellinger 		padding = ((-tid_len) & 3);
302c66ac9dbSNicholas Bellinger 		if (padding != 0)
303c66ac9dbSNicholas Bellinger 			tid_len += padding;
304c66ac9dbSNicholas Bellinger 
305c66ac9dbSNicholas Bellinger 		if ((add_len + 4) != tid_len) {
3066708bb27SAndy Grover 			pr_debug("LIO-Target Extracted add_len: %hu "
307c66ac9dbSNicholas Bellinger 				"does not match calculated tid_len: %u,"
308c66ac9dbSNicholas Bellinger 				" using tid_len instead\n", add_len+4, tid_len);
309c66ac9dbSNicholas Bellinger 			*out_tid_len = tid_len;
310c66ac9dbSNicholas Bellinger 		} else
311c66ac9dbSNicholas Bellinger 			*out_tid_len = (add_len + 4);
312c66ac9dbSNicholas Bellinger 	}
313c66ac9dbSNicholas Bellinger 	/*
31435d1efe8SMasanari Iida 	 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
315c66ac9dbSNicholas Bellinger 	 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
316c66ac9dbSNicholas Bellinger 	 * format.
317c66ac9dbSNicholas Bellinger 	 */
318c66ac9dbSNicholas Bellinger 	if (format_code == 0x40) {
3198359cf43SJörn Engel 		p = strstr(&buf[4], ",i,0x");
3206708bb27SAndy Grover 		if (!p) {
32135d1efe8SMasanari Iida 			pr_err("Unable to locate \",i,0x\" separator"
322c66ac9dbSNicholas Bellinger 				" for Initiator port identifier: %s\n",
3238359cf43SJörn Engel 				&buf[4]);
324c66ac9dbSNicholas Bellinger 			return NULL;
325c66ac9dbSNicholas Bellinger 		}
326c66ac9dbSNicholas Bellinger 		*p = '\0'; /* Terminate iSCSI Name */
32735d1efe8SMasanari Iida 		p += 5; /* Skip over ",i,0x" separator */
328c66ac9dbSNicholas Bellinger 
329c66ac9dbSNicholas Bellinger 		*port_nexus_ptr = p;
330c66ac9dbSNicholas Bellinger 		/*
331c66ac9dbSNicholas Bellinger 		 * Go ahead and do the lower case conversion of the received
332c66ac9dbSNicholas Bellinger 		 * 12 ASCII characters representing the ISID in the TransportID
33325985edcSLucas De Marchi 		 * for comparison against the running iSCSI session's ISID from
334c66ac9dbSNicholas Bellinger 		 * iscsi_target.c:lio_sess_get_initiator_sid()
335c66ac9dbSNicholas Bellinger 		 */
336c66ac9dbSNicholas Bellinger 		for (i = 0; i < 12; i++) {
337c66ac9dbSNicholas Bellinger 			if (isdigit(*p)) {
338c66ac9dbSNicholas Bellinger 				p++;
339c66ac9dbSNicholas Bellinger 				continue;
340c66ac9dbSNicholas Bellinger 			}
341c66ac9dbSNicholas Bellinger 			*p = tolower(*p);
342c66ac9dbSNicholas Bellinger 			p++;
343c66ac9dbSNicholas Bellinger 		}
34413ef143dSBodo Stroesser 	} else
34513ef143dSBodo Stroesser 		*port_nexus_ptr = NULL;
346c66ac9dbSNicholas Bellinger 
347094bb5d7SRasmus Villemoes 	return &buf[4];
348c66ac9dbSNicholas Bellinger }
3492650d71eSChristoph Hellwig 
3502650d71eSChristoph Hellwig int target_get_pr_transport_id_len(struct se_node_acl *nacl,
3512650d71eSChristoph Hellwig 		struct t10_pr_registration *pr_reg, int *format_code)
3522650d71eSChristoph Hellwig {
3532650d71eSChristoph Hellwig 	switch (nacl->se_tpg->proto_id) {
3542650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
3552650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
3562650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
3572650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
3582650d71eSChristoph Hellwig 		break;
3592650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
3602650d71eSChristoph Hellwig 		return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code);
3612650d71eSChristoph Hellwig 	default:
3622650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
3632650d71eSChristoph Hellwig 		return -EINVAL;
3642650d71eSChristoph Hellwig 	}
3652650d71eSChristoph Hellwig 
3662650d71eSChristoph Hellwig 	/*
3672650d71eSChristoph Hellwig 	 * Most transports use a fixed length 24 byte identifier.
3682650d71eSChristoph Hellwig 	 */
3692650d71eSChristoph Hellwig 	*format_code = 0;
3702650d71eSChristoph Hellwig 	return 24;
3712650d71eSChristoph Hellwig }
3722650d71eSChristoph Hellwig 
3732650d71eSChristoph Hellwig int target_get_pr_transport_id(struct se_node_acl *nacl,
3742650d71eSChristoph Hellwig 		struct t10_pr_registration *pr_reg, int *format_code,
3752650d71eSChristoph Hellwig 		unsigned char *buf)
3762650d71eSChristoph Hellwig {
3772650d71eSChristoph Hellwig 	switch (nacl->se_tpg->proto_id) {
3782650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
3792650d71eSChristoph Hellwig 		return sas_get_pr_transport_id(nacl, format_code, buf);
3802650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
3812650d71eSChristoph Hellwig 		return sbp_get_pr_transport_id(nacl, format_code, buf);
3822650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
3832650d71eSChristoph Hellwig 		return srp_get_pr_transport_id(nacl, format_code, buf);
3842650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
3852650d71eSChristoph Hellwig 		return fc_get_pr_transport_id(nacl, format_code, buf);
3862650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
3872650d71eSChristoph Hellwig 		return iscsi_get_pr_transport_id(nacl, pr_reg, format_code,
3882650d71eSChristoph Hellwig 				buf);
3892650d71eSChristoph Hellwig 	default:
3902650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
3912650d71eSChristoph Hellwig 		return -EINVAL;
3922650d71eSChristoph Hellwig 	}
3932650d71eSChristoph Hellwig }
3942650d71eSChristoph Hellwig 
3952650d71eSChristoph Hellwig const char *target_parse_pr_out_transport_id(struct se_portal_group *tpg,
396094bb5d7SRasmus Villemoes 		char *buf, u32 *out_tid_len, char **port_nexus_ptr)
3972650d71eSChristoph Hellwig {
3982650d71eSChristoph Hellwig 	u32 offset;
3992650d71eSChristoph Hellwig 
4002650d71eSChristoph Hellwig 	switch (tpg->proto_id) {
4012650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
4022650d71eSChristoph Hellwig 		/*
4032650d71eSChristoph Hellwig 		 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
4042650d71eSChristoph Hellwig 		 * for initiator ports using SCSI over SAS Serial SCSI Protocol.
4052650d71eSChristoph Hellwig 		 */
4062650d71eSChristoph Hellwig 		offset = 4;
4072650d71eSChristoph Hellwig 		break;
4082650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
4092650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
4102650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
4112650d71eSChristoph Hellwig 		offset = 8;
4122650d71eSChristoph Hellwig 		break;
4132650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
4142650d71eSChristoph Hellwig 		return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len,
4152650d71eSChristoph Hellwig 					port_nexus_ptr);
4162650d71eSChristoph Hellwig 	default:
4172650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id);
4182650d71eSChristoph Hellwig 		return NULL;
4192650d71eSChristoph Hellwig 	}
4202650d71eSChristoph Hellwig 
4212650d71eSChristoph Hellwig 	*port_nexus_ptr = NULL;
4222650d71eSChristoph Hellwig 	*out_tid_len = 24;
4232650d71eSChristoph Hellwig 	return buf + offset;
4242650d71eSChristoph Hellwig }
425