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