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