1 /*******************************************************************************
2 * Filename: target_core_fabric_lib.c
4 * This file contains generic high level protocol identifier and PR
5 * handlers for TCM fabric modules
7 * Copyright (c) 2010 Rising Tide Systems, Inc.
8 * Copyright (c) 2010 Linux-iSCSI.org
10 * Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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.
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.
26 ******************************************************************************/
28 #include <linux/string.h>
29 #include <linux/ctype.h>
30 #include <linux/spinlock.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_device.h>
36 #include <target/target_core_transport.h>
37 #include <target/target_core_fabric_lib.h>
38 #include <target/target_core_fabric_ops.h>
39 #include <target/target_core_configfs.h>
41 #include "target_core_hba.h"
42 #include "target_core_pr.h"
45 * Handlers for Serial Attached SCSI (SAS)
47 u8
sas_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
50 * Return a SAS Serial SCSI Protocol identifier for loopback operations
51 * This is defined in section 7.5.1 Table 362 in spc4r17
55 EXPORT_SYMBOL(sas_get_fabric_proto_ident
);
57 u32
sas_get_pr_transport_id(
58 struct se_portal_group
*se_tpg
,
59 struct se_node_acl
*se_nacl
,
60 struct t10_pr_registration
*pr_reg
,
64 unsigned char binary
, *ptr
;
68 * Set PROTOCOL IDENTIFIER to 6h for SAS
72 * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
73 * over SAS Serial SCSI Protocol
75 ptr
= &se_nacl
->initiatorname
[4]; /* Skip over 'naa. prefix */
77 for (i
= 0; i
< 16; i
+= 2) {
78 binary
= transport_asciihex_to_binaryhex(&ptr
[i
]);
82 * The SAS Transport ID is a hardcoded 24-byte length
86 EXPORT_SYMBOL(sas_get_pr_transport_id
);
88 u32
sas_get_pr_transport_id_len(
89 struct se_portal_group
*se_tpg
,
90 struct se_node_acl
*se_nacl
,
91 struct t10_pr_registration
*pr_reg
,
96 * From spc4r17, 7.5.4.7 TransportID for initiator ports using SCSI
97 * over SAS Serial SCSI Protocol
99 * The SAS Transport ID is a hardcoded 24-byte length
103 EXPORT_SYMBOL(sas_get_pr_transport_id_len
);
106 * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
107 * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
109 char *sas_parse_pr_out_transport_id(
110 struct se_portal_group
*se_tpg
,
113 char **port_nexus_ptr
)
116 * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.7 TransportID
117 * for initiator ports using SCSI over SAS Serial SCSI Protocol
119 * The TransportID for a SAS Initiator Port is of fixed size of
120 * 24 bytes, and SAS does not contain a I_T nexus identifier,
121 * so we return the **port_nexus_ptr set to NULL.
123 *port_nexus_ptr
= NULL
;
126 return (char *)&buf
[4];
128 EXPORT_SYMBOL(sas_parse_pr_out_transport_id
);
131 * Handlers for Fibre Channel Protocol (FCP)
133 u8
fc_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
135 return 0x0; /* 0 = fcp-2 per SPC4 section 7.5.1 */
137 EXPORT_SYMBOL(fc_get_fabric_proto_ident
);
139 u32
fc_get_pr_transport_id_len(
140 struct se_portal_group
*se_tpg
,
141 struct se_node_acl
*se_nacl
,
142 struct t10_pr_registration
*pr_reg
,
147 * The FC Transport ID is a hardcoded 24-byte length
151 EXPORT_SYMBOL(fc_get_pr_transport_id_len
);
153 u32
fc_get_pr_transport_id(
154 struct se_portal_group
*se_tpg
,
155 struct se_node_acl
*se_nacl
,
156 struct t10_pr_registration
*pr_reg
,
160 unsigned char binary
, *ptr
;
164 * PROTOCOL IDENTIFIER is 0h for FCP-2
166 * From spc4r17, 7.5.4.2 TransportID for initiator ports using
167 * SCSI over Fibre Channel
169 * We convert the ASCII formatted N Port name into a binary
170 * encoded TransportID.
172 ptr
= &se_nacl
->initiatorname
[0];
174 for (i
= 0; i
< 24; ) {
175 if (!(strncmp(&ptr
[i
], ":", 1))) {
179 binary
= transport_asciihex_to_binaryhex(&ptr
[i
]);
184 * The FC Transport ID is a hardcoded 24-byte length
188 EXPORT_SYMBOL(fc_get_pr_transport_id
);
190 char *fc_parse_pr_out_transport_id(
191 struct se_portal_group
*se_tpg
,
194 char **port_nexus_ptr
)
197 * The TransportID for a FC N Port is of fixed size of
198 * 24 bytes, and FC does not contain a I_T nexus identifier,
199 * so we return the **port_nexus_ptr set to NULL.
201 *port_nexus_ptr
= NULL
;
204 return (char *)&buf
[8];
206 EXPORT_SYMBOL(fc_parse_pr_out_transport_id
);
209 * Handlers for Internet Small Computer Systems Interface (iSCSI)
212 u8
iscsi_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
215 * This value is defined for "Internet SCSI (iSCSI)"
216 * in spc4r17 section 7.5.1 Table 362
220 EXPORT_SYMBOL(iscsi_get_fabric_proto_ident
);
222 u32
iscsi_get_pr_transport_id(
223 struct se_portal_group
*se_tpg
,
224 struct se_node_acl
*se_nacl
,
225 struct t10_pr_registration
*pr_reg
,
229 u32 off
= 4, padding
= 0;
232 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
234 * Set PROTOCOL IDENTIFIER to 5h for iSCSI
238 * From spc4r17 Section 7.5.4.6: TransportID for initiator
239 * ports using SCSI over iSCSI.
241 * The null-terminated, null-padded (see 4.4.2) ISCSI NAME field
242 * shall contain the iSCSI name of an iSCSI initiator node (see
243 * RFC 3720). The first ISCSI NAME field byte containing an ASCII
244 * null character terminates the ISCSI NAME field without regard for
245 * the specified length of the iSCSI TransportID or the contents of
246 * the ADDITIONAL LENGTH field.
248 len
= sprintf(&buf
[off
], "%s", se_nacl
->initiatorname
);
250 * Add Extra byte for NULL terminator
254 * If there is ISID present with the registration and *format code == 1
255 * 1, use iSCSI Initiator port TransportID format.
257 * Otherwise use iSCSI Initiator device TransportID format that
258 * does not contain the ASCII encoded iSCSI Initiator iSID value
259 * provied by the iSCSi Initiator during the iSCSI login process.
261 if ((*format_code
== 1) && (pr_reg
->isid_present_at_reg
)) {
263 * Set FORMAT CODE 01b for iSCSI Initiator port TransportID
268 * From spc4r17 Section 7.5.4.6: TransportID for initiator
269 * ports using SCSI over iSCSI. Table 390
271 * The SEPARATOR field shall contain the five ASCII
272 * characters ",i,0x".
274 * The null-terminated, null-padded ISCSI INITIATOR SESSION ID
275 * field shall contain the iSCSI initiator session identifier
276 * (see RFC 3720) in the form of ASCII characters that are the
277 * hexadecimal digits converted from the binary iSCSI initiator
278 * session identifier value. The first ISCSI INITIATOR SESSION
279 * ID field byte containing an ASCII null character
281 buf
[off
+len
] = 0x2c; off
++; /* ASCII Character: "," */
282 buf
[off
+len
] = 0x69; off
++; /* ASCII Character: "i" */
283 buf
[off
+len
] = 0x2c; off
++; /* ASCII Character: "," */
284 buf
[off
+len
] = 0x30; off
++; /* ASCII Character: "0" */
285 buf
[off
+len
] = 0x78; off
++; /* ASCII Character: "x" */
287 buf
[off
+len
] = pr_reg
->pr_reg_isid
[0]; off
++;
288 buf
[off
+len
] = pr_reg
->pr_reg_isid
[1]; off
++;
289 buf
[off
+len
] = pr_reg
->pr_reg_isid
[2]; off
++;
290 buf
[off
+len
] = pr_reg
->pr_reg_isid
[3]; off
++;
291 buf
[off
+len
] = pr_reg
->pr_reg_isid
[4]; off
++;
292 buf
[off
+len
] = pr_reg
->pr_reg_isid
[5]; off
++;
293 buf
[off
+len
] = '\0'; off
++;
296 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
298 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
299 * in the TransportID. The additional length shall be at least 20 and
300 * shall be a multiple of four.
302 padding
= ((-len
) & 3);
306 buf
[2] = ((len
>> 8) & 0xff);
307 buf
[3] = (len
& 0xff);
309 * Increment value for total payload + header length for
310 * full status descriptor
316 EXPORT_SYMBOL(iscsi_get_pr_transport_id
);
318 u32
iscsi_get_pr_transport_id_len(
319 struct se_portal_group
*se_tpg
,
320 struct se_node_acl
*se_nacl
,
321 struct t10_pr_registration
*pr_reg
,
324 u32 len
= 0, padding
= 0;
326 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
327 len
= strlen(se_nacl
->initiatorname
);
329 * Add extra byte for NULL terminator
333 * If there is ISID present with the registration, use format code:
334 * 01b: iSCSI Initiator port TransportID format
336 * If there is not an active iSCSI session, use format code:
337 * 00b: iSCSI Initiator device TransportID format
339 if (pr_reg
->isid_present_at_reg
) {
340 len
+= 5; /* For ",i,0x" ASCII seperator */
341 len
+= 7; /* For iSCSI Initiator Session ID + Null terminator */
345 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
347 * The ADDITIONAL LENGTH field specifies the number of bytes that follow
348 * in the TransportID. The additional length shall be at least 20 and
349 * shall be a multiple of four.
351 padding
= ((-len
) & 3);
355 * Increment value for total payload + header length for
356 * full status descriptor
362 EXPORT_SYMBOL(iscsi_get_pr_transport_id_len
);
364 char *iscsi_parse_pr_out_transport_id(
365 struct se_portal_group
*se_tpg
,
368 char **port_nexus_ptr
)
371 u32 tid_len
, padding
;
374 u8 format_code
= (buf
[0] & 0xc0);
376 * Check for FORMAT CODE 00b or 01b from spc4r17, section 7.5.4.6:
378 * TransportID for initiator ports using SCSI over iSCSI,
379 * from Table 388 -- iSCSI TransportID formats.
381 * 00b Initiator port is identified using the world wide unique
382 * SCSI device name of the iSCSI initiator
383 * device containing the initiator port (see table 389).
384 * 01b Initiator port is identified using the world wide unique
385 * initiator port identifier (see table 390).10b to 11b
388 if ((format_code
!= 0x00) && (format_code
!= 0x40)) {
389 printk(KERN_ERR
"Illegal format code: 0x%02x for iSCSI"
390 " Initiator Transport ID\n", format_code
);
394 * If the caller wants the TransportID Length, we set that value for the
395 * entire iSCSI Tarnsport ID now.
397 if (out_tid_len
!= NULL
) {
398 add_len
= ((buf
[2] >> 8) & 0xff);
399 add_len
|= (buf
[3] & 0xff);
401 tid_len
= strlen((char *)&buf
[4]);
402 tid_len
+= 4; /* Add four bytes for iSCSI Transport ID header */
403 tid_len
+= 1; /* Add one byte for NULL terminator */
404 padding
= ((-tid_len
) & 3);
408 if ((add_len
+ 4) != tid_len
) {
409 printk(KERN_INFO
"LIO-Target Extracted add_len: %hu "
410 "does not match calculated tid_len: %u,"
411 " using tid_len instead\n", add_len
+4, tid_len
);
412 *out_tid_len
= tid_len
;
414 *out_tid_len
= (add_len
+ 4);
417 * Check for ',i,0x' seperator between iSCSI Name and iSCSI Initiator
418 * Session ID as defined in Table 390 - iSCSI initiator port TransportID
421 if (format_code
== 0x40) {
422 p
= strstr((char *)&buf
[4], ",i,0x");
424 printk(KERN_ERR
"Unable to locate \",i,0x\" seperator"
425 " for Initiator port identifier: %s\n",
429 *p
= '\0'; /* Terminate iSCSI Name */
430 p
+= 5; /* Skip over ",i,0x" seperator */
434 * Go ahead and do the lower case conversion of the received
435 * 12 ASCII characters representing the ISID in the TransportID
436 * for comparision against the running iSCSI session's ISID from
437 * iscsi_target.c:lio_sess_get_initiator_sid()
439 for (i
= 0; i
< 12; i
++) {
449 return (char *)&buf
[4];
451 EXPORT_SYMBOL(iscsi_parse_pr_out_transport_id
);