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