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