1 /******************************************************************************* 2 * Filename: tcm_fc.c 3 * 4 * This file contains the configfs implementation for TCM_fc fabric node. 5 * Based on tcm_loop_configfs.c 6 * 7 * Copyright (c) 2010 Cisco Systems, Inc. 8 * Copyright (c) 2009,2010 Rising Tide, Inc. 9 * Copyright (c) 2009,2010 Linux-iSCSI.org 10 * 11 * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org> 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 ****************************************************************************/ 23 24 #include <linux/module.h> 25 #include <linux/moduleparam.h> 26 #include <generated/utsrelease.h> 27 #include <linux/utsname.h> 28 #include <linux/init.h> 29 #include <linux/slab.h> 30 #include <linux/kthread.h> 31 #include <linux/types.h> 32 #include <linux/string.h> 33 #include <linux/configfs.h> 34 #include <linux/kernel.h> 35 #include <linux/ctype.h> 36 #include <asm/unaligned.h> 37 #include <scsi/libfc.h> 38 39 #include <target/target_core_base.h> 40 #include <target/target_core_fabric.h> 41 #include <target/target_core_fabric_configfs.h> 42 #include <target/target_core_configfs.h> 43 #include <target/configfs_macros.h> 44 45 #include "tcm_fc.h" 46 47 static const struct target_core_fabric_ops ft_fabric_ops; 48 49 static LIST_HEAD(ft_wwn_list); 50 DEFINE_MUTEX(ft_lport_lock); 51 52 unsigned int ft_debug_logging; 53 module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR); 54 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); 55 56 /* 57 * Parse WWN. 58 * If strict, we require lower-case hex and colon separators to be sure 59 * the name is the same as what would be generated by ft_format_wwn() 60 * so the name and wwn are mapped one-to-one. 61 */ 62 static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict) 63 { 64 const char *cp; 65 char c; 66 u32 byte = 0; 67 u32 pos = 0; 68 u32 err; 69 int val; 70 71 *wwn = 0; 72 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) { 73 c = *cp; 74 if (c == '\n' && cp[1] == '\0') 75 continue; 76 if (strict && pos++ == 2 && byte++ < 7) { 77 pos = 0; 78 if (c == ':') 79 continue; 80 err = 1; 81 goto fail; 82 } 83 if (c == '\0') { 84 err = 2; 85 if (strict && byte != 8) 86 goto fail; 87 return cp - name; 88 } 89 err = 3; 90 val = hex_to_bin(c); 91 if (val < 0 || (strict && isupper(c))) 92 goto fail; 93 *wwn = (*wwn << 4) | val; 94 } 95 err = 4; 96 fail: 97 pr_debug("err %u len %zu pos %u byte %u\n", 98 err, cp - name, pos, byte); 99 return -1; 100 } 101 102 ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn) 103 { 104 u8 b[8]; 105 106 put_unaligned_be64(wwn, b); 107 return snprintf(buf, len, 108 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", 109 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); 110 } 111 112 static ssize_t ft_wwn_show(void *arg, char *buf) 113 { 114 u64 *wwn = arg; 115 ssize_t len; 116 117 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn); 118 buf[len++] = '\n'; 119 return len; 120 } 121 122 static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len) 123 { 124 ssize_t ret; 125 u64 wwn; 126 127 ret = ft_parse_wwn(buf, &wwn, 0); 128 if (ret > 0) 129 *(u64 *)arg = wwn; 130 return ret; 131 } 132 133 /* 134 * ACL auth ops. 135 */ 136 137 static ssize_t ft_nacl_show_port_name( 138 struct se_node_acl *se_nacl, 139 char *page) 140 { 141 struct ft_node_acl *acl = container_of(se_nacl, 142 struct ft_node_acl, se_node_acl); 143 144 return ft_wwn_show(&acl->node_auth.port_name, page); 145 } 146 147 static ssize_t ft_nacl_store_port_name( 148 struct se_node_acl *se_nacl, 149 const char *page, 150 size_t count) 151 { 152 struct ft_node_acl *acl = container_of(se_nacl, 153 struct ft_node_acl, se_node_acl); 154 155 return ft_wwn_store(&acl->node_auth.port_name, page, count); 156 } 157 158 TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR); 159 160 static ssize_t ft_nacl_show_node_name( 161 struct se_node_acl *se_nacl, 162 char *page) 163 { 164 struct ft_node_acl *acl = container_of(se_nacl, 165 struct ft_node_acl, se_node_acl); 166 167 return ft_wwn_show(&acl->node_auth.node_name, page); 168 } 169 170 static ssize_t ft_nacl_store_node_name( 171 struct se_node_acl *se_nacl, 172 const char *page, 173 size_t count) 174 { 175 struct ft_node_acl *acl = container_of(se_nacl, 176 struct ft_node_acl, se_node_acl); 177 178 return ft_wwn_store(&acl->node_auth.node_name, page, count); 179 } 180 181 TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR); 182 183 static struct configfs_attribute *ft_nacl_base_attrs[] = { 184 &ft_nacl_port_name.attr, 185 &ft_nacl_node_name.attr, 186 NULL, 187 }; 188 189 /* 190 * ACL ops. 191 */ 192 193 /* 194 * Add ACL for an initiator. The ACL is named arbitrarily. 195 * The port_name and/or node_name are attributes. 196 */ 197 static struct se_node_acl *ft_add_acl( 198 struct se_portal_group *se_tpg, 199 struct config_group *group, 200 const char *name) 201 { 202 struct ft_node_acl *acl; 203 struct ft_tpg *tpg; 204 u64 wwpn; 205 u32 q_depth; 206 207 pr_debug("add acl %s\n", name); 208 tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 209 210 if (ft_parse_wwn(name, &wwpn, 1) < 0) 211 return ERR_PTR(-EINVAL); 212 213 acl = kzalloc(sizeof(struct ft_node_acl), GFP_KERNEL); 214 if (!acl) 215 return ERR_PTR(-ENOMEM); 216 acl->node_auth.port_name = wwpn; 217 218 q_depth = 32; /* XXX bogus default - get from tpg? */ 219 return core_tpg_add_initiator_node_acl(&tpg->se_tpg, 220 &acl->se_node_acl, name, q_depth); 221 } 222 223 static void ft_del_acl(struct se_node_acl *se_acl) 224 { 225 struct se_portal_group *se_tpg = se_acl->se_tpg; 226 struct ft_tpg *tpg; 227 struct ft_node_acl *acl = container_of(se_acl, 228 struct ft_node_acl, se_node_acl); 229 230 pr_debug("del acl %s\n", 231 config_item_name(&se_acl->acl_group.cg_item)); 232 233 tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 234 pr_debug("del acl %p se_acl %p tpg %p se_tpg %p\n", 235 acl, se_acl, tpg, &tpg->se_tpg); 236 237 core_tpg_del_initiator_node_acl(&tpg->se_tpg, se_acl, 1); 238 kfree(acl); 239 } 240 241 struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata) 242 { 243 struct ft_node_acl *found = NULL; 244 struct ft_node_acl *acl; 245 struct se_portal_group *se_tpg = &tpg->se_tpg; 246 struct se_node_acl *se_acl; 247 248 spin_lock_irq(&se_tpg->acl_node_lock); 249 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) { 250 acl = container_of(se_acl, struct ft_node_acl, se_node_acl); 251 pr_debug("acl %p port_name %llx\n", 252 acl, (unsigned long long)acl->node_auth.port_name); 253 if (acl->node_auth.port_name == rdata->ids.port_name || 254 acl->node_auth.node_name == rdata->ids.node_name) { 255 pr_debug("acl %p port_name %llx matched\n", acl, 256 (unsigned long long)rdata->ids.port_name); 257 found = acl; 258 /* XXX need to hold onto ACL */ 259 break; 260 } 261 } 262 spin_unlock_irq(&se_tpg->acl_node_lock); 263 return found; 264 } 265 266 static struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg) 267 { 268 struct ft_node_acl *acl; 269 270 acl = kzalloc(sizeof(*acl), GFP_KERNEL); 271 if (!acl) { 272 pr_err("Unable to allocate struct ft_node_acl\n"); 273 return NULL; 274 } 275 pr_debug("acl %p\n", acl); 276 return &acl->se_node_acl; 277 } 278 279 static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg, 280 struct se_node_acl *se_acl) 281 { 282 struct ft_node_acl *acl = container_of(se_acl, 283 struct ft_node_acl, se_node_acl); 284 285 pr_debug("acl %p\n", acl); 286 kfree(acl); 287 } 288 289 /* 290 * local_port port_group (tpg) ops. 291 */ 292 static struct se_portal_group *ft_add_tpg( 293 struct se_wwn *wwn, 294 struct config_group *group, 295 const char *name) 296 { 297 struct ft_lport_wwn *ft_wwn; 298 struct ft_tpg *tpg; 299 struct workqueue_struct *wq; 300 unsigned long index; 301 int ret; 302 303 pr_debug("tcm_fc: add tpg %s\n", name); 304 305 /* 306 * Name must be "tpgt_" followed by the index. 307 */ 308 if (strstr(name, "tpgt_") != name) 309 return NULL; 310 311 ret = kstrtoul(name + 5, 10, &index); 312 if (ret) 313 return NULL; 314 if (index > UINT_MAX) 315 return NULL; 316 317 if ((index != 1)) { 318 pr_err("Error, a single TPG=1 is used for HW port mappings\n"); 319 return ERR_PTR(-ENOSYS); 320 } 321 322 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn); 323 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 324 if (!tpg) 325 return NULL; 326 tpg->index = index; 327 tpg->lport_wwn = ft_wwn; 328 INIT_LIST_HEAD(&tpg->lun_list); 329 330 wq = alloc_workqueue("tcm_fc", 0, 1); 331 if (!wq) { 332 kfree(tpg); 333 return NULL; 334 } 335 336 ret = core_tpg_register(&ft_fabric_ops, wwn, &tpg->se_tpg, 337 tpg, TRANSPORT_TPG_TYPE_NORMAL); 338 if (ret < 0) { 339 destroy_workqueue(wq); 340 kfree(tpg); 341 return NULL; 342 } 343 tpg->workqueue = wq; 344 345 mutex_lock(&ft_lport_lock); 346 ft_wwn->tpg = tpg; 347 mutex_unlock(&ft_lport_lock); 348 349 return &tpg->se_tpg; 350 } 351 352 static void ft_del_tpg(struct se_portal_group *se_tpg) 353 { 354 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 355 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn; 356 357 pr_debug("del tpg %s\n", 358 config_item_name(&tpg->se_tpg.tpg_group.cg_item)); 359 360 destroy_workqueue(tpg->workqueue); 361 362 /* Wait for sessions to be freed thru RCU, for BUG_ON below */ 363 synchronize_rcu(); 364 365 mutex_lock(&ft_lport_lock); 366 ft_wwn->tpg = NULL; 367 if (tpg->tport) { 368 tpg->tport->tpg = NULL; 369 tpg->tport = NULL; 370 } 371 mutex_unlock(&ft_lport_lock); 372 373 core_tpg_deregister(se_tpg); 374 kfree(tpg); 375 } 376 377 /* 378 * Verify that an lport is configured to use the tcm_fc module, and return 379 * the target port group that should be used. 380 * 381 * The caller holds ft_lport_lock. 382 */ 383 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport) 384 { 385 struct ft_lport_wwn *ft_wwn; 386 387 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) { 388 if (ft_wwn->wwpn == lport->wwpn) 389 return ft_wwn->tpg; 390 } 391 return NULL; 392 } 393 394 /* 395 * target config instance ops. 396 */ 397 398 /* 399 * Add lport to allowed config. 400 * The name is the WWPN in lower-case ASCII, colon-separated bytes. 401 */ 402 static struct se_wwn *ft_add_wwn( 403 struct target_fabric_configfs *tf, 404 struct config_group *group, 405 const char *name) 406 { 407 struct ft_lport_wwn *ft_wwn; 408 struct ft_lport_wwn *old_ft_wwn; 409 u64 wwpn; 410 411 pr_debug("add wwn %s\n", name); 412 if (ft_parse_wwn(name, &wwpn, 1) < 0) 413 return NULL; 414 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL); 415 if (!ft_wwn) 416 return NULL; 417 ft_wwn->wwpn = wwpn; 418 419 mutex_lock(&ft_lport_lock); 420 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) { 421 if (old_ft_wwn->wwpn == wwpn) { 422 mutex_unlock(&ft_lport_lock); 423 kfree(ft_wwn); 424 return NULL; 425 } 426 } 427 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list); 428 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn); 429 mutex_unlock(&ft_lport_lock); 430 431 return &ft_wwn->se_wwn; 432 } 433 434 static void ft_del_wwn(struct se_wwn *wwn) 435 { 436 struct ft_lport_wwn *ft_wwn = container_of(wwn, 437 struct ft_lport_wwn, se_wwn); 438 439 pr_debug("del wwn %s\n", ft_wwn->name); 440 mutex_lock(&ft_lport_lock); 441 list_del(&ft_wwn->ft_wwn_node); 442 mutex_unlock(&ft_lport_lock); 443 444 kfree(ft_wwn); 445 } 446 447 static ssize_t ft_wwn_show_attr_version( 448 struct target_fabric_configfs *tf, 449 char *page) 450 { 451 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on " 452 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine); 453 } 454 455 TF_WWN_ATTR_RO(ft, version); 456 457 static struct configfs_attribute *ft_wwn_attrs[] = { 458 &ft_wwn_version.attr, 459 NULL, 460 }; 461 462 static char *ft_get_fabric_name(void) 463 { 464 return "fc"; 465 } 466 467 static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg) 468 { 469 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr; 470 471 return tpg->lport_wwn->name; 472 } 473 474 static u16 ft_get_tag(struct se_portal_group *se_tpg) 475 { 476 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr; 477 478 /* 479 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83 480 * to represent the SCSI Target Port. 481 */ 482 return tpg->index; 483 } 484 485 static u32 ft_get_default_depth(struct se_portal_group *se_tpg) 486 { 487 return 1; 488 } 489 490 static int ft_check_false(struct se_portal_group *se_tpg) 491 { 492 return 0; 493 } 494 495 static void ft_set_default_node_attr(struct se_node_acl *se_nacl) 496 { 497 } 498 499 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg) 500 { 501 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr; 502 503 return tpg->index; 504 } 505 506 static const struct target_core_fabric_ops ft_fabric_ops = { 507 .module = THIS_MODULE, 508 .name = "fc", 509 .get_fabric_name = ft_get_fabric_name, 510 .get_fabric_proto_ident = fc_get_fabric_proto_ident, 511 .tpg_get_wwn = ft_get_fabric_wwn, 512 .tpg_get_tag = ft_get_tag, 513 .tpg_get_default_depth = ft_get_default_depth, 514 .tpg_get_pr_transport_id = fc_get_pr_transport_id, 515 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len, 516 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id, 517 .tpg_check_demo_mode = ft_check_false, 518 .tpg_check_demo_mode_cache = ft_check_false, 519 .tpg_check_demo_mode_write_protect = ft_check_false, 520 .tpg_check_prod_mode_write_protect = ft_check_false, 521 .tpg_alloc_fabric_acl = ft_tpg_alloc_fabric_acl, 522 .tpg_release_fabric_acl = ft_tpg_release_fabric_acl, 523 .tpg_get_inst_index = ft_tpg_get_inst_index, 524 .check_stop_free = ft_check_stop_free, 525 .release_cmd = ft_release_cmd, 526 .shutdown_session = ft_sess_shutdown, 527 .close_session = ft_sess_close, 528 .sess_get_index = ft_sess_get_index, 529 .sess_get_initiator_sid = NULL, 530 .write_pending = ft_write_pending, 531 .write_pending_status = ft_write_pending_status, 532 .set_default_node_attributes = ft_set_default_node_attr, 533 .get_task_tag = ft_get_task_tag, 534 .get_cmd_state = ft_get_cmd_state, 535 .queue_data_in = ft_queue_data_in, 536 .queue_status = ft_queue_status, 537 .queue_tm_rsp = ft_queue_tm_resp, 538 .aborted_task = ft_aborted_task, 539 /* 540 * Setup function pointers for generic logic in 541 * target_core_fabric_configfs.c 542 */ 543 .fabric_make_wwn = &ft_add_wwn, 544 .fabric_drop_wwn = &ft_del_wwn, 545 .fabric_make_tpg = &ft_add_tpg, 546 .fabric_drop_tpg = &ft_del_tpg, 547 .fabric_post_link = NULL, 548 .fabric_pre_unlink = NULL, 549 .fabric_make_np = NULL, 550 .fabric_drop_np = NULL, 551 .fabric_make_nodeacl = &ft_add_acl, 552 .fabric_drop_nodeacl = &ft_del_acl, 553 554 .tfc_wwn_attrs = ft_wwn_attrs, 555 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs, 556 }; 557 558 static struct notifier_block ft_notifier = { 559 .notifier_call = ft_lport_notify 560 }; 561 562 static int __init ft_init(void) 563 { 564 int ret; 565 566 ret = target_register_template(&ft_fabric_ops); 567 if (ret) 568 goto out; 569 570 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov); 571 if (ret) 572 goto out_unregister_template; 573 574 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier); 575 fc_lport_iterate(ft_lport_add, NULL); 576 return 0; 577 578 out_unregister_template: 579 target_unregister_template(&ft_fabric_ops); 580 out: 581 return ret; 582 } 583 584 static void __exit ft_exit(void) 585 { 586 blocking_notifier_chain_unregister(&fc_lport_notifier_head, 587 &ft_notifier); 588 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov); 589 fc_lport_iterate(ft_lport_del, NULL); 590 target_unregister_template(&ft_fabric_ops); 591 synchronize_rcu(); 592 } 593 594 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION); 595 MODULE_LICENSE("GPL"); 596 module_init(ft_init); 597 module_exit(ft_exit); 598