1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_fabric_lib.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains generic high level protocol identifier and PR
5c66ac9dbSNicholas Bellinger  * handlers for TCM fabric modules
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2010-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@linux-iscsi.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
12c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
13c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
14c66ac9dbSNicholas Bellinger  * (at your option) any later version.
15c66ac9dbSNicholas Bellinger  *
16c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
17c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
20c66ac9dbSNicholas Bellinger  *
21c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
22c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
23c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24c66ac9dbSNicholas Bellinger  *
25c66ac9dbSNicholas Bellinger  ******************************************************************************/
26c66ac9dbSNicholas Bellinger 
272650d71eSChristoph Hellwig /*
282650d71eSChristoph Hellwig  * See SPC4, section 7.5 "Protocol specific parameters" for details
292650d71eSChristoph Hellwig  * on the formats implemented in this file.
302650d71eSChristoph Hellwig  */
312650d71eSChristoph Hellwig 
3211650b85SAndy Shevchenko #include <linux/kernel.h>
33c66ac9dbSNicholas Bellinger #include <linux/string.h>
34c66ac9dbSNicholas Bellinger #include <linux/ctype.h>
35c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
36c53181afSPaul Gortmaker #include <linux/export.h>
37a85d667eSBart Van Assche #include <asm/unaligned.h>
38c66ac9dbSNicholas Bellinger 
396546a02aSStephen Rothwell #include <scsi/scsi_proto.h>
406546a02aSStephen Rothwell 
41c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
42c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
43c66ac9dbSNicholas Bellinger 
44e26d99aeSChristoph Hellwig #include "target_core_internal.h"
45c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
46c66ac9dbSNicholas Bellinger 
47c66ac9dbSNicholas Bellinger 
482650d71eSChristoph Hellwig static int sas_get_pr_transport_id(
492650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
50c66ac9dbSNicholas Bellinger 	int *format_code,
51c66ac9dbSNicholas Bellinger 	unsigned char *buf)
52c66ac9dbSNicholas Bellinger {
538c35ad20SMimi Zohar 	int ret;
5411650b85SAndy Shevchenko 
552650d71eSChristoph Hellwig 	/* Skip over 'naa. prefix */
562650d71eSChristoph Hellwig 	ret = hex2bin(&buf[4], &nacl->initiatorname[4], 8);
572650d71eSChristoph Hellwig 	if (ret) {
582650d71eSChristoph Hellwig 		pr_debug("%s: invalid hex string\n", __func__);
592650d71eSChristoph Hellwig 		return ret;
602650d71eSChristoph Hellwig 	}
61c66ac9dbSNicholas Bellinger 
62c66ac9dbSNicholas Bellinger 	return 24;
63c66ac9dbSNicholas Bellinger }
64c66ac9dbSNicholas Bellinger 
652650d71eSChristoph Hellwig static int fc_get_pr_transport_id(
66c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
67c66ac9dbSNicholas Bellinger 	int *format_code,
68c66ac9dbSNicholas Bellinger 	unsigned char *buf)
69c66ac9dbSNicholas Bellinger {
7011650b85SAndy Shevchenko 	unsigned char *ptr;
718c35ad20SMimi Zohar 	int i, ret;
72c66ac9dbSNicholas Bellinger 	u32 off = 8;
738c35ad20SMimi Zohar 
74c66ac9dbSNicholas Bellinger 	/*
75c66ac9dbSNicholas Bellinger 	 * We convert the ASCII formatted N Port name into a binary
76c66ac9dbSNicholas Bellinger 	 * encoded TransportID.
77c66ac9dbSNicholas Bellinger 	 */
78c66ac9dbSNicholas Bellinger 	ptr = &se_nacl->initiatorname[0];
79c66ac9dbSNicholas Bellinger 	for (i = 0; i < 24; ) {
806708bb27SAndy Grover 		if (!strncmp(&ptr[i], ":", 1)) {
81c66ac9dbSNicholas Bellinger 			i++;
82c66ac9dbSNicholas Bellinger 			continue;
83c66ac9dbSNicholas Bellinger 		}
848c35ad20SMimi Zohar 		ret = hex2bin(&buf[off++], &ptr[i], 1);
852650d71eSChristoph Hellwig 		if (ret < 0) {
862650d71eSChristoph Hellwig 			pr_debug("%s: invalid hex string\n", __func__);
872650d71eSChristoph Hellwig 			return ret;
882650d71eSChristoph Hellwig 		}
89c66ac9dbSNicholas Bellinger 		i += 2;
90c66ac9dbSNicholas Bellinger 	}
91c66ac9dbSNicholas Bellinger 	/*
92c66ac9dbSNicholas Bellinger 	 * The FC Transport ID is a hardcoded 24-byte length
93c66ac9dbSNicholas Bellinger 	 */
94c66ac9dbSNicholas Bellinger 	return 24;
95c66ac9dbSNicholas Bellinger }
96c66ac9dbSNicholas Bellinger 
972650d71eSChristoph Hellwig static int sbp_get_pr_transport_id(
982650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
992650d71eSChristoph Hellwig 	int *format_code,
1002650d71eSChristoph Hellwig 	unsigned char *buf)
101c66ac9dbSNicholas Bellinger {
1022650d71eSChristoph Hellwig 	int ret;
103c66ac9dbSNicholas Bellinger 
1042650d71eSChristoph Hellwig 	ret = hex2bin(&buf[8], nacl->initiatorname, 8);
1052650d71eSChristoph Hellwig 	if (ret) {
1062650d71eSChristoph Hellwig 		pr_debug("%s: invalid hex string\n", __func__);
1072650d71eSChristoph Hellwig 		return ret;
108c66ac9dbSNicholas Bellinger 	}
109c66ac9dbSNicholas Bellinger 
1102650d71eSChristoph Hellwig 	return 24;
1112650d71eSChristoph Hellwig }
112c66ac9dbSNicholas Bellinger 
1132650d71eSChristoph Hellwig static int srp_get_pr_transport_id(
1142650d71eSChristoph Hellwig 	struct se_node_acl *nacl,
1152650d71eSChristoph Hellwig 	int *format_code,
1162650d71eSChristoph Hellwig 	unsigned char *buf)
117c66ac9dbSNicholas Bellinger {
1182650d71eSChristoph Hellwig 	const char *p;
1192650d71eSChristoph Hellwig 	unsigned len, count, leading_zero_bytes;
1202650d71eSChristoph Hellwig 	int rc;
121c66ac9dbSNicholas Bellinger 
1222650d71eSChristoph Hellwig 	p = nacl->initiatorname;
1232650d71eSChristoph Hellwig 	if (strncasecmp(p, "0x", 2) == 0)
1242650d71eSChristoph Hellwig 		p += 2;
1252650d71eSChristoph Hellwig 	len = strlen(p);
1262650d71eSChristoph Hellwig 	if (len % 2)
1272650d71eSChristoph Hellwig 		return -EINVAL;
1282650d71eSChristoph Hellwig 
1292650d71eSChristoph Hellwig 	count = min(len / 2, 16U);
1302650d71eSChristoph Hellwig 	leading_zero_bytes = 16 - count;
1312650d71eSChristoph Hellwig 	memset(buf + 8, 0, leading_zero_bytes);
1322650d71eSChristoph Hellwig 	rc = hex2bin(buf + 8 + leading_zero_bytes, p, count);
1332650d71eSChristoph Hellwig 	if (rc < 0) {
1342650d71eSChristoph Hellwig 		pr_debug("hex2bin failed for %s: %d\n", __func__, rc);
1352650d71eSChristoph Hellwig 		return rc;
1362650d71eSChristoph Hellwig 	}
1372650d71eSChristoph Hellwig 
1382650d71eSChristoph Hellwig 	return 24;
1392650d71eSChristoph Hellwig }
1402650d71eSChristoph Hellwig 
1412650d71eSChristoph Hellwig static int iscsi_get_pr_transport_id(
142c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
143c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
144c66ac9dbSNicholas Bellinger 	int *format_code,
145c66ac9dbSNicholas Bellinger 	unsigned char *buf)
146c66ac9dbSNicholas Bellinger {
147c66ac9dbSNicholas Bellinger 	u32 off = 4, padding = 0;
148c66ac9dbSNicholas Bellinger 	u16 len = 0;
149c66ac9dbSNicholas Bellinger 
150c66ac9dbSNicholas Bellinger 	spin_lock_irq(&se_nacl->nacl_sess_lock);
151c66ac9dbSNicholas Bellinger 	/*
152c66ac9dbSNicholas Bellinger 	 * From spc4r17 Section 7.5.4.6: TransportID for initiator
153c66ac9dbSNicholas Bellinger 	 * ports using SCSI over iSCSI.
154c66ac9dbSNicholas Bellinger 	 *
155c66ac9dbSNicholas Bellinger 	 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
156c66ac9dbSNicholas Bellinger 	 * shall contain the iSCSI name of an iSCSI initiator node (see
157c66ac9dbSNicholas Bellinger 	 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
158c66ac9dbSNicholas Bellinger 	 * null character terminates the ISCSI NAME field without regard for
159c66ac9dbSNicholas Bellinger 	 * the specified length of the iSCSI TransportID or the contents of
160c66ac9dbSNicholas Bellinger 	 * the ADDITIONAL LENGTH field.
161c66ac9dbSNicholas Bellinger 	 */
162c66ac9dbSNicholas Bellinger 	len = sprintf(&buf[off], "%s", se_nacl->initiatorname);
163c66ac9dbSNicholas Bellinger 	/*
164c66ac9dbSNicholas Bellinger 	 * Add Extra byte for NULL terminator
165c66ac9dbSNicholas Bellinger 	 */
166c66ac9dbSNicholas Bellinger 	len++;
167c66ac9dbSNicholas Bellinger 	/*
168c66ac9dbSNicholas Bellinger 	 * If there is ISID present with the registration and *format code == 1
169c66ac9dbSNicholas Bellinger 	 * 1, use iSCSI Initiator port TransportID format.
170c66ac9dbSNicholas Bellinger 	 *
171c66ac9dbSNicholas Bellinger 	 * Otherwise use iSCSI Initiator device TransportID format that
172c66ac9dbSNicholas Bellinger 	 * does not contain the ASCII encoded iSCSI Initiator iSID value
173c66ac9dbSNicholas Bellinger 	 * provied by the iSCSi Initiator during the iSCSI login process.
174c66ac9dbSNicholas Bellinger 	 */
175c66ac9dbSNicholas Bellinger 	if ((*format_code == 1) && (pr_reg->isid_present_at_reg)) {
176c66ac9dbSNicholas Bellinger 		/*
177c66ac9dbSNicholas Bellinger 		 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
178c66ac9dbSNicholas Bellinger 		 * format.
179c66ac9dbSNicholas Bellinger 		 */
180c66ac9dbSNicholas Bellinger 		buf[0] |= 0x40;
181c66ac9dbSNicholas Bellinger 		/*
182c66ac9dbSNicholas Bellinger 		 * From spc4r17 Section 7.5.4.6: TransportID for initiator
183c66ac9dbSNicholas Bellinger 		 * ports using SCSI over iSCSI.  Table 390
184c66ac9dbSNicholas Bellinger 		 *
185c66ac9dbSNicholas Bellinger 		 * The SEPARATOR field shall contain the five ASCII
186c66ac9dbSNicholas Bellinger 		 * characters ",i,0x".
187c66ac9dbSNicholas Bellinger 		 *
188c66ac9dbSNicholas Bellinger 		 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
189c66ac9dbSNicholas Bellinger 		 * field shall contain the iSCSI initiator session identifier
190c66ac9dbSNicholas Bellinger 		 * (see RFC 3720) in the form of ASCII characters that are the
191c66ac9dbSNicholas Bellinger 		 * hexadecimal digits converted from the binary iSCSI initiator
192c66ac9dbSNicholas Bellinger 		 * session identifier value. The first ISCSI INITIATOR SESSION
193c66ac9dbSNicholas Bellinger 		 * ID field byte containing an ASCII null character
194c66ac9dbSNicholas Bellinger 		 */
195c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
196c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x69; off++; /* ASCII Character: "i" */
197c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x2c; off++; /* ASCII Character: "," */
198c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x30; off++; /* ASCII Character: "0" */
199c66ac9dbSNicholas Bellinger 		buf[off+len] = 0x78; off++; /* ASCII Character: "x" */
200c66ac9dbSNicholas Bellinger 		len += 5;
201c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[0]; off++;
202c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[1]; off++;
203c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[2]; off++;
204c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[3]; off++;
205c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[4]; off++;
206c66ac9dbSNicholas Bellinger 		buf[off+len] = pr_reg->pr_reg_isid[5]; off++;
207c66ac9dbSNicholas Bellinger 		buf[off+len] = '\0'; off++;
208c66ac9dbSNicholas Bellinger 		len += 7;
209c66ac9dbSNicholas Bellinger 	}
210c66ac9dbSNicholas Bellinger 	spin_unlock_irq(&se_nacl->nacl_sess_lock);
211c66ac9dbSNicholas Bellinger 	/*
212c66ac9dbSNicholas Bellinger 	 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
213c66ac9dbSNicholas Bellinger 	 * in the TransportID. The additional length shall be at least 20 and
214c66ac9dbSNicholas Bellinger 	 * shall be a multiple of four.
215c66ac9dbSNicholas Bellinger 	*/
216c66ac9dbSNicholas Bellinger 	padding = ((-len) & 3);
217c66ac9dbSNicholas Bellinger 	if (padding != 0)
218c66ac9dbSNicholas Bellinger 		len += padding;
219c66ac9dbSNicholas Bellinger 
220a85d667eSBart Van Assche 	put_unaligned_be16(len, &buf[2]);
221c66ac9dbSNicholas Bellinger 	/*
222c66ac9dbSNicholas Bellinger 	 * Increment value for total payload + header length for
223c66ac9dbSNicholas Bellinger 	 * full status descriptor
224c66ac9dbSNicholas Bellinger 	 */
225c66ac9dbSNicholas Bellinger 	len += 4;
226c66ac9dbSNicholas Bellinger 
227c66ac9dbSNicholas Bellinger 	return len;
228c66ac9dbSNicholas Bellinger }
229c66ac9dbSNicholas Bellinger 
2302650d71eSChristoph Hellwig static int iscsi_get_pr_transport_id_len(
231c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl,
232c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg,
233c66ac9dbSNicholas Bellinger 	int *format_code)
234c66ac9dbSNicholas Bellinger {
235c66ac9dbSNicholas Bellinger 	u32 len = 0, padding = 0;
236c66ac9dbSNicholas Bellinger 
237c66ac9dbSNicholas Bellinger 	spin_lock_irq(&se_nacl->nacl_sess_lock);
238c66ac9dbSNicholas Bellinger 	len = strlen(se_nacl->initiatorname);
239c66ac9dbSNicholas Bellinger 	/*
240c66ac9dbSNicholas Bellinger 	 * Add extra byte for NULL terminator
241c66ac9dbSNicholas Bellinger 	 */
242c66ac9dbSNicholas Bellinger 	len++;
243c66ac9dbSNicholas Bellinger 	/*
244c66ac9dbSNicholas Bellinger 	 * If there is ISID present with the registration, use format code:
245c66ac9dbSNicholas Bellinger 	 * 01b: iSCSI Initiator port TransportID format
246c66ac9dbSNicholas Bellinger 	 *
247c66ac9dbSNicholas Bellinger 	 * If there is not an active iSCSI session, use format code:
248c66ac9dbSNicholas Bellinger 	 * 00b: iSCSI Initiator device TransportID format
249c66ac9dbSNicholas Bellinger 	 */
250c66ac9dbSNicholas Bellinger 	if (pr_reg->isid_present_at_reg) {
25135d1efe8SMasanari Iida 		len += 5; /* For ",i,0x" ASCII separator */
252c66ac9dbSNicholas Bellinger 		len += 7; /* For iSCSI Initiator Session ID + Null terminator */
253c66ac9dbSNicholas Bellinger 		*format_code = 1;
254c66ac9dbSNicholas Bellinger 	} else
255c66ac9dbSNicholas Bellinger 		*format_code = 0;
256c66ac9dbSNicholas Bellinger 	spin_unlock_irq(&se_nacl->nacl_sess_lock);
257c66ac9dbSNicholas Bellinger 	/*
258c66ac9dbSNicholas Bellinger 	 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
259c66ac9dbSNicholas Bellinger 	 * in the TransportID. The additional length shall be at least 20 and
260c66ac9dbSNicholas Bellinger 	 * shall be a multiple of four.
261c66ac9dbSNicholas Bellinger 	 */
262c66ac9dbSNicholas Bellinger 	padding = ((-len) & 3);
263c66ac9dbSNicholas Bellinger 	if (padding != 0)
264c66ac9dbSNicholas Bellinger 		len += padding;
265c66ac9dbSNicholas Bellinger 	/*
266c66ac9dbSNicholas Bellinger 	 * Increment value for total payload + header length for
267c66ac9dbSNicholas Bellinger 	 * full status descriptor
268c66ac9dbSNicholas Bellinger 	 */
269c66ac9dbSNicholas Bellinger 	len += 4;
270c66ac9dbSNicholas Bellinger 
271c66ac9dbSNicholas Bellinger 	return len;
272c66ac9dbSNicholas Bellinger }
273c66ac9dbSNicholas Bellinger 
2742650d71eSChristoph Hellwig static char *iscsi_parse_pr_out_transport_id(
275c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg,
276094bb5d7SRasmus Villemoes 	char *buf,
277c66ac9dbSNicholas Bellinger 	u32 *out_tid_len,
278c66ac9dbSNicholas Bellinger 	char **port_nexus_ptr)
279c66ac9dbSNicholas Bellinger {
280c66ac9dbSNicholas Bellinger 	char *p;
281c66ac9dbSNicholas Bellinger 	u32 tid_len, padding;
282c66ac9dbSNicholas Bellinger 	int i;
283c66ac9dbSNicholas Bellinger 	u16 add_len;
284c66ac9dbSNicholas Bellinger 	u8 format_code = (buf[0] & 0xc0);
285c66ac9dbSNicholas Bellinger 	/*
286c66ac9dbSNicholas Bellinger 	 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
287c66ac9dbSNicholas Bellinger 	 *
288c66ac9dbSNicholas Bellinger 	 *       TransportID for initiator ports using SCSI over iSCSI,
289c66ac9dbSNicholas Bellinger 	 *       from Table 388 -- iSCSI TransportID formats.
290c66ac9dbSNicholas Bellinger 	 *
291c66ac9dbSNicholas Bellinger 	 *    00b     Initiator port is identified using the world wide unique
292c66ac9dbSNicholas Bellinger 	 *            SCSI device name of the iSCSI initiator
293c66ac9dbSNicholas Bellinger 	 *            device containing the initiator port (see table 389).
294c66ac9dbSNicholas Bellinger 	 *    01b     Initiator port is identified using the world wide unique
295c66ac9dbSNicholas Bellinger 	 *            initiator port identifier (see table 390).10b to 11b
296c66ac9dbSNicholas Bellinger 	 *            Reserved
297c66ac9dbSNicholas Bellinger 	 */
298c66ac9dbSNicholas Bellinger 	if ((format_code != 0x00) && (format_code != 0x40)) {
2996708bb27SAndy Grover 		pr_err("Illegal format code: 0x%02x for iSCSI"
300c66ac9dbSNicholas Bellinger 			" Initiator Transport ID\n", format_code);
301c66ac9dbSNicholas Bellinger 		return NULL;
302c66ac9dbSNicholas Bellinger 	}
303c66ac9dbSNicholas Bellinger 	/*
304c66ac9dbSNicholas Bellinger 	 * If the caller wants the TransportID Length, we set that value for the
305c66ac9dbSNicholas Bellinger 	 * entire iSCSI Tarnsport ID now.
306c66ac9dbSNicholas Bellinger 	 */
30768edbce4SJoern Engel 	if (out_tid_len) {
30868edbce4SJoern Engel 		/* The shift works thanks to integer promotion rules */
309a85d667eSBart Van Assche 		add_len = get_unaligned_be16(&buf[2]);
310c66ac9dbSNicholas Bellinger 
3118359cf43SJörn Engel 		tid_len = strlen(&buf[4]);
312c66ac9dbSNicholas Bellinger 		tid_len += 4; /* Add four bytes for iSCSI Transport ID header */
313c66ac9dbSNicholas Bellinger 		tid_len += 1; /* Add one byte for NULL terminator */
314c66ac9dbSNicholas Bellinger 		padding = ((-tid_len) & 3);
315c66ac9dbSNicholas Bellinger 		if (padding != 0)
316c66ac9dbSNicholas Bellinger 			tid_len += padding;
317c66ac9dbSNicholas Bellinger 
318c66ac9dbSNicholas Bellinger 		if ((add_len + 4) != tid_len) {
3196708bb27SAndy Grover 			pr_debug("LIO-Target Extracted add_len: %hu "
320c66ac9dbSNicholas Bellinger 				"does not match calculated tid_len: %u,"
321c66ac9dbSNicholas Bellinger 				" using tid_len instead\n", add_len+4, tid_len);
322c66ac9dbSNicholas Bellinger 			*out_tid_len = tid_len;
323c66ac9dbSNicholas Bellinger 		} else
324c66ac9dbSNicholas Bellinger 			*out_tid_len = (add_len + 4);
325c66ac9dbSNicholas Bellinger 	}
326c66ac9dbSNicholas Bellinger 	/*
32735d1efe8SMasanari Iida 	 * Check for ',i,0x' separator between iSCSI Name and iSCSI Initiator
328c66ac9dbSNicholas Bellinger 	 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
329c66ac9dbSNicholas Bellinger 	 * format.
330c66ac9dbSNicholas Bellinger 	 */
331c66ac9dbSNicholas Bellinger 	if (format_code == 0x40) {
3328359cf43SJörn Engel 		p = strstr(&buf[4], ",i,0x");
3336708bb27SAndy Grover 		if (!p) {
33435d1efe8SMasanari Iida 			pr_err("Unable to locate \",i,0x\" separator"
335c66ac9dbSNicholas Bellinger 				" for Initiator port identifier: %s\n",
3368359cf43SJörn Engel 				&buf[4]);
337c66ac9dbSNicholas Bellinger 			return NULL;
338c66ac9dbSNicholas Bellinger 		}
339c66ac9dbSNicholas Bellinger 		*p = '\0'; /* Terminate iSCSI Name */
34035d1efe8SMasanari Iida 		p += 5; /* Skip over ",i,0x" separator */
341c66ac9dbSNicholas Bellinger 
342c66ac9dbSNicholas Bellinger 		*port_nexus_ptr = p;
343c66ac9dbSNicholas Bellinger 		/*
344c66ac9dbSNicholas Bellinger 		 * Go ahead and do the lower case conversion of the received
345c66ac9dbSNicholas Bellinger 		 * 12 ASCII characters representing the ISID in the TransportID
34625985edcSLucas De Marchi 		 * for comparison against the running iSCSI session's ISID from
347c66ac9dbSNicholas Bellinger 		 * iscsi_target.c:lio_sess_get_initiator_sid()
348c66ac9dbSNicholas Bellinger 		 */
349c66ac9dbSNicholas Bellinger 		for (i = 0; i < 12; i++) {
350c66ac9dbSNicholas Bellinger 			if (isdigit(*p)) {
351c66ac9dbSNicholas Bellinger 				p++;
352c66ac9dbSNicholas Bellinger 				continue;
353c66ac9dbSNicholas Bellinger 			}
354c66ac9dbSNicholas Bellinger 			*p = tolower(*p);
355c66ac9dbSNicholas Bellinger 			p++;
356c66ac9dbSNicholas Bellinger 		}
357c66ac9dbSNicholas Bellinger 	}
358c66ac9dbSNicholas Bellinger 
359094bb5d7SRasmus Villemoes 	return &buf[4];
360c66ac9dbSNicholas Bellinger }
3612650d71eSChristoph Hellwig 
3622650d71eSChristoph Hellwig int target_get_pr_transport_id_len(struct se_node_acl *nacl,
3632650d71eSChristoph Hellwig 		struct t10_pr_registration *pr_reg, int *format_code)
3642650d71eSChristoph Hellwig {
3652650d71eSChristoph Hellwig 	switch (nacl->se_tpg->proto_id) {
3662650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
3672650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
3682650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
3692650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
3702650d71eSChristoph Hellwig 		break;
3712650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
3722650d71eSChristoph Hellwig 		return iscsi_get_pr_transport_id_len(nacl, pr_reg, format_code);
3732650d71eSChristoph Hellwig 	default:
3742650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
3752650d71eSChristoph Hellwig 		return -EINVAL;
3762650d71eSChristoph Hellwig 	}
3772650d71eSChristoph Hellwig 
3782650d71eSChristoph Hellwig 	/*
3792650d71eSChristoph Hellwig 	 * Most transports use a fixed length 24 byte identifier.
3802650d71eSChristoph Hellwig 	 */
3812650d71eSChristoph Hellwig 	*format_code = 0;
3822650d71eSChristoph Hellwig 	return 24;
3832650d71eSChristoph Hellwig }
3842650d71eSChristoph Hellwig 
3852650d71eSChristoph Hellwig int target_get_pr_transport_id(struct se_node_acl *nacl,
3862650d71eSChristoph Hellwig 		struct t10_pr_registration *pr_reg, int *format_code,
3872650d71eSChristoph Hellwig 		unsigned char *buf)
3882650d71eSChristoph Hellwig {
3892650d71eSChristoph Hellwig 	switch (nacl->se_tpg->proto_id) {
3902650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
3912650d71eSChristoph Hellwig 		return sas_get_pr_transport_id(nacl, format_code, buf);
3922650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
3932650d71eSChristoph Hellwig 		return sbp_get_pr_transport_id(nacl, format_code, buf);
3942650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
3952650d71eSChristoph Hellwig 		return srp_get_pr_transport_id(nacl, format_code, buf);
3962650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
3972650d71eSChristoph Hellwig 		return fc_get_pr_transport_id(nacl, format_code, buf);
3982650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
3992650d71eSChristoph Hellwig 		return iscsi_get_pr_transport_id(nacl, pr_reg, format_code,
4002650d71eSChristoph Hellwig 				buf);
4012650d71eSChristoph Hellwig 	default:
4022650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", nacl->se_tpg->proto_id);
4032650d71eSChristoph Hellwig 		return -EINVAL;
4042650d71eSChristoph Hellwig 	}
4052650d71eSChristoph Hellwig }
4062650d71eSChristoph Hellwig 
4072650d71eSChristoph Hellwig const char *target_parse_pr_out_transport_id(struct se_portal_group *tpg,
408094bb5d7SRasmus Villemoes 		char *buf, u32 *out_tid_len, char **port_nexus_ptr)
4092650d71eSChristoph Hellwig {
4102650d71eSChristoph Hellwig 	u32 offset;
4112650d71eSChristoph Hellwig 
4122650d71eSChristoph Hellwig 	switch (tpg->proto_id) {
4132650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SAS:
4142650d71eSChristoph Hellwig 		/*
4152650d71eSChristoph Hellwig 		 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
4162650d71eSChristoph Hellwig 		 * for initiator ports using SCSI over SAS Serial SCSI Protocol.
4172650d71eSChristoph Hellwig 		 */
4182650d71eSChristoph Hellwig 		offset = 4;
4192650d71eSChristoph Hellwig 		break;
4202650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SBP:
4212650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_SRP:
4222650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_FCP:
4232650d71eSChristoph Hellwig 		offset = 8;
4242650d71eSChristoph Hellwig 		break;
4252650d71eSChristoph Hellwig 	case SCSI_PROTOCOL_ISCSI:
4262650d71eSChristoph Hellwig 		return iscsi_parse_pr_out_transport_id(tpg, buf, out_tid_len,
4272650d71eSChristoph Hellwig 					port_nexus_ptr);
4282650d71eSChristoph Hellwig 	default:
4292650d71eSChristoph Hellwig 		pr_err("Unknown proto_id: 0x%02x\n", tpg->proto_id);
4302650d71eSChristoph Hellwig 		return NULL;
4312650d71eSChristoph Hellwig 	}
4322650d71eSChristoph Hellwig 
4332650d71eSChristoph Hellwig 	*port_nexus_ptr = NULL;
4342650d71eSChristoph Hellwig 	*out_tid_len = 24;
4352650d71eSChristoph Hellwig 	return buf + offset;
4362650d71eSChristoph Hellwig }
437