Linux Audio

Check our new training course

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