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/scsi.h> 38 #include <scsi/scsi_host.h> 39 #include <scsi/scsi_device.h> 40 #include <scsi/scsi_cmnd.h> 41 #include <scsi/libfc.h> 42 43 #include <target/target_core_base.h> 44 #include <target/target_core_transport.h> 45 #include <target/target_core_fabric_ops.h> 46 #include <target/target_core_fabric_configfs.h> 47 #include <target/target_core_fabric_lib.h> 48 #include <target/target_core_device.h> 49 #include <target/target_core_tpg.h> 50 #include <target/target_core_configfs.h> 51 #include <target/configfs_macros.h> 52 53 #include "tcm_fc.h" 54 55 struct target_fabric_configfs *ft_configfs; 56 57 LIST_HEAD(ft_lport_list); 58 DEFINE_MUTEX(ft_lport_lock); 59 60 unsigned int ft_debug_logging; 61 module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR); 62 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); 63 64 /* 65 * Parse WWN. 66 * If strict, we require lower-case hex and colon separators to be sure 67 * the name is the same as what would be generated by ft_format_wwn() 68 * so the name and wwn are mapped one-to-one. 69 */ 70 static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict) 71 { 72 const char *cp; 73 char c; 74 u32 byte = 0; 75 u32 pos = 0; 76 u32 err; 77 int val; 78 79 *wwn = 0; 80 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) { 81 c = *cp; 82 if (c == '\n' && cp[1] == '\0') 83 continue; 84 if (strict && pos++ == 2 && byte++ < 7) { 85 pos = 0; 86 if (c == ':') 87 continue; 88 err = 1; 89 goto fail; 90 } 91 if (c == '\0') { 92 err = 2; 93 if (strict && byte != 8) 94 goto fail; 95 return cp - name; 96 } 97 err = 3; 98 val = hex_to_bin(c); 99 if (val < 0 || (strict && isupper(c))) 100 goto fail; 101 *wwn = (*wwn << 4) | val; 102 } 103 err = 4; 104 fail: 105 pr_debug("err %u len %zu pos %u byte %u\n", 106 err, cp - name, pos, byte); 107 return -1; 108 } 109 110 ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn) 111 { 112 u8 b[8]; 113 114 put_unaligned_be64(wwn, b); 115 return snprintf(buf, len, 116 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", 117 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); 118 } 119 120 static ssize_t ft_wwn_show(void *arg, char *buf) 121 { 122 u64 *wwn = arg; 123 ssize_t len; 124 125 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn); 126 buf[len++] = '\n'; 127 return len; 128 } 129 130 static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len) 131 { 132 ssize_t ret; 133 u64 wwn; 134 135 ret = ft_parse_wwn(buf, &wwn, 0); 136 if (ret > 0) 137 *(u64 *)arg = wwn; 138 return ret; 139 } 140 141 /* 142 * ACL auth ops. 143 */ 144 145 static ssize_t ft_nacl_show_port_name( 146 struct se_node_acl *se_nacl, 147 char *page) 148 { 149 struct ft_node_acl *acl = container_of(se_nacl, 150 struct ft_node_acl, se_node_acl); 151 152 return ft_wwn_show(&acl->node_auth.port_name, page); 153 } 154 155 static ssize_t ft_nacl_store_port_name( 156 struct se_node_acl *se_nacl, 157 const char *page, 158 size_t count) 159 { 160 struct ft_node_acl *acl = container_of(se_nacl, 161 struct ft_node_acl, se_node_acl); 162 163 return ft_wwn_store(&acl->node_auth.port_name, page, count); 164 } 165 166 TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR); 167 168 static ssize_t ft_nacl_show_node_name( 169 struct se_node_acl *se_nacl, 170 char *page) 171 { 172 struct ft_node_acl *acl = container_of(se_nacl, 173 struct ft_node_acl, se_node_acl); 174 175 return ft_wwn_show(&acl->node_auth.node_name, page); 176 } 177 178 static ssize_t ft_nacl_store_node_name( 179 struct se_node_acl *se_nacl, 180 const char *page, 181 size_t count) 182 { 183 struct ft_node_acl *acl = container_of(se_nacl, 184 struct ft_node_acl, se_node_acl); 185 186 return ft_wwn_store(&acl->node_auth.node_name, page, count); 187 } 188 189 TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR); 190 191 static struct configfs_attribute *ft_nacl_base_attrs[] = { 192 &ft_nacl_port_name.attr, 193 &ft_nacl_node_name.attr, 194 NULL, 195 }; 196 197 /* 198 * ACL ops. 199 */ 200 201 /* 202 * Add ACL for an initiator. The ACL is named arbitrarily. 203 * The port_name and/or node_name are attributes. 204 */ 205 static struct se_node_acl *ft_add_acl( 206 struct se_portal_group *se_tpg, 207 struct config_group *group, 208 const char *name) 209 { 210 struct ft_node_acl *acl; 211 struct ft_tpg *tpg; 212 u64 wwpn; 213 u32 q_depth; 214 215 pr_debug("add acl %s\n", name); 216 tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 217 218 if (ft_parse_wwn(name, &wwpn, 1) < 0) 219 return ERR_PTR(-EINVAL); 220 221 acl = kzalloc(sizeof(struct ft_node_acl), GFP_KERNEL); 222 if (!acl) 223 return ERR_PTR(-ENOMEM); 224 acl->node_auth.port_name = wwpn; 225 226 q_depth = 32; /* XXX bogus default - get from tpg? */ 227 return core_tpg_add_initiator_node_acl(&tpg->se_tpg, 228 &acl->se_node_acl, name, q_depth); 229 } 230 231 static void ft_del_acl(struct se_node_acl *se_acl) 232 { 233 struct se_portal_group *se_tpg = se_acl->se_tpg; 234 struct ft_tpg *tpg; 235 struct ft_node_acl *acl = container_of(se_acl, 236 struct ft_node_acl, se_node_acl); 237 238 pr_debug("del acl %s\n", 239 config_item_name(&se_acl->acl_group.cg_item)); 240 241 tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 242 pr_debug("del acl %p se_acl %p tpg %p se_tpg %p\n", 243 acl, se_acl, tpg, &tpg->se_tpg); 244 245 core_tpg_del_initiator_node_acl(&tpg->se_tpg, se_acl, 1); 246 kfree(acl); 247 } 248 249 struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata) 250 { 251 struct ft_node_acl *found = NULL; 252 struct ft_node_acl *acl; 253 struct se_portal_group *se_tpg = &tpg->se_tpg; 254 struct se_node_acl *se_acl; 255 256 spin_lock_irq(&se_tpg->acl_node_lock); 257 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) { 258 acl = container_of(se_acl, struct ft_node_acl, se_node_acl); 259 pr_debug("acl %p port_name %llx\n", 260 acl, (unsigned long long)acl->node_auth.port_name); 261 if (acl->node_auth.port_name == rdata->ids.port_name || 262 acl->node_auth.node_name == rdata->ids.node_name) { 263 pr_debug("acl %p port_name %llx matched\n", acl, 264 (unsigned long long)rdata->ids.port_name); 265 found = acl; 266 /* XXX need to hold onto ACL */ 267 break; 268 } 269 } 270 spin_unlock_irq(&se_tpg->acl_node_lock); 271 return found; 272 } 273 274 struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg) 275 { 276 struct ft_node_acl *acl; 277 278 acl = kzalloc(sizeof(*acl), GFP_KERNEL); 279 if (!acl) { 280 pr_err("Unable to allocate struct ft_node_acl\n"); 281 return NULL; 282 } 283 pr_debug("acl %p\n", acl); 284 return &acl->se_node_acl; 285 } 286 287 static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg, 288 struct se_node_acl *se_acl) 289 { 290 struct ft_node_acl *acl = container_of(se_acl, 291 struct ft_node_acl, se_node_acl); 292 293 pr_debug("acl %p\n", acl); 294 kfree(acl); 295 } 296 297 /* 298 * local_port port_group (tpg) ops. 299 */ 300 static struct se_portal_group *ft_add_tpg( 301 struct se_wwn *wwn, 302 struct config_group *group, 303 const char *name) 304 { 305 struct ft_lport_acl *lacl; 306 struct ft_tpg *tpg; 307 unsigned long index; 308 int ret; 309 310 pr_debug("tcm_fc: add tpg %s\n", name); 311 312 /* 313 * Name must be "tpgt_" followed by the index. 314 */ 315 if (strstr(name, "tpgt_") != name) 316 return NULL; 317 if (strict_strtoul(name + 5, 10, &index) || index > UINT_MAX) 318 return NULL; 319 320 lacl = container_of(wwn, struct ft_lport_acl, fc_lport_wwn); 321 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 322 if (!tpg) 323 return NULL; 324 tpg->index = index; 325 tpg->lport_acl = lacl; 326 INIT_LIST_HEAD(&tpg->lun_list); 327 328 ret = core_tpg_register(&ft_configfs->tf_ops, wwn, &tpg->se_tpg, 329 tpg, TRANSPORT_TPG_TYPE_NORMAL); 330 if (ret < 0) { 331 kfree(tpg); 332 return NULL; 333 } 334 335 tpg->workqueue = alloc_workqueue("tcm_fc", 0, 1); 336 if (!tpg->workqueue) { 337 kfree(tpg); 338 return NULL; 339 } 340 341 mutex_lock(&ft_lport_lock); 342 list_add_tail(&tpg->list, &lacl->tpg_list); 343 mutex_unlock(&ft_lport_lock); 344 345 return &tpg->se_tpg; 346 } 347 348 static void ft_del_tpg(struct se_portal_group *se_tpg) 349 { 350 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 351 352 pr_debug("del tpg %s\n", 353 config_item_name(&tpg->se_tpg.tpg_group.cg_item)); 354 355 destroy_workqueue(tpg->workqueue); 356 357 /* Wait for sessions to be freed thru RCU, for BUG_ON below */ 358 synchronize_rcu(); 359 360 mutex_lock(&ft_lport_lock); 361 list_del(&tpg->list); 362 if (tpg->tport) { 363 tpg->tport->tpg = NULL; 364 tpg->tport = NULL; 365 } 366 mutex_unlock(&ft_lport_lock); 367 368 core_tpg_deregister(se_tpg); 369 kfree(tpg); 370 } 371 372 /* 373 * Verify that an lport is configured to use the tcm_fc module, and return 374 * the target port group that should be used. 375 * 376 * The caller holds ft_lport_lock. 377 */ 378 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport) 379 { 380 struct ft_lport_acl *lacl; 381 struct ft_tpg *tpg; 382 383 list_for_each_entry(lacl, &ft_lport_list, list) { 384 if (lacl->wwpn == lport->wwpn) { 385 list_for_each_entry(tpg, &lacl->tpg_list, list) 386 return tpg; /* XXX for now return first entry */ 387 return NULL; 388 } 389 } 390 return NULL; 391 } 392 393 /* 394 * target config instance ops. 395 */ 396 397 /* 398 * Add lport to allowed config. 399 * The name is the WWPN in lower-case ASCII, colon-separated bytes. 400 */ 401 static struct se_wwn *ft_add_lport( 402 struct target_fabric_configfs *tf, 403 struct config_group *group, 404 const char *name) 405 { 406 struct ft_lport_acl *lacl; 407 struct ft_lport_acl *old_lacl; 408 u64 wwpn; 409 410 pr_debug("add lport %s\n", name); 411 if (ft_parse_wwn(name, &wwpn, 1) < 0) 412 return NULL; 413 lacl = kzalloc(sizeof(*lacl), GFP_KERNEL); 414 if (!lacl) 415 return NULL; 416 lacl->wwpn = wwpn; 417 INIT_LIST_HEAD(&lacl->tpg_list); 418 419 mutex_lock(&ft_lport_lock); 420 list_for_each_entry(old_lacl, &ft_lport_list, list) { 421 if (old_lacl->wwpn == wwpn) { 422 mutex_unlock(&ft_lport_lock); 423 kfree(lacl); 424 return NULL; 425 } 426 } 427 list_add_tail(&lacl->list, &ft_lport_list); 428 ft_format_wwn(lacl->name, sizeof(lacl->name), wwpn); 429 mutex_unlock(&ft_lport_lock); 430 431 return &lacl->fc_lport_wwn; 432 } 433 434 static void ft_del_lport(struct se_wwn *wwn) 435 { 436 struct ft_lport_acl *lacl = container_of(wwn, 437 struct ft_lport_acl, fc_lport_wwn); 438 439 pr_debug("del lport %s\n", lacl->name); 440 mutex_lock(&ft_lport_lock); 441 list_del(&lacl->list); 442 mutex_unlock(&ft_lport_lock); 443 444 kfree(lacl); 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_acl->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 u16 ft_get_fabric_sense_len(void) 500 { 501 return 0; 502 } 503 504 static u16 ft_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_len) 505 { 506 return 0; 507 } 508 509 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg) 510 { 511 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr; 512 513 return tpg->index; 514 } 515 516 static struct target_core_fabric_ops ft_fabric_ops = { 517 .get_fabric_name = ft_get_fabric_name, 518 .get_fabric_proto_ident = fc_get_fabric_proto_ident, 519 .tpg_get_wwn = ft_get_fabric_wwn, 520 .tpg_get_tag = ft_get_tag, 521 .tpg_get_default_depth = ft_get_default_depth, 522 .tpg_get_pr_transport_id = fc_get_pr_transport_id, 523 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len, 524 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id, 525 .tpg_check_demo_mode = ft_check_false, 526 .tpg_check_demo_mode_cache = ft_check_false, 527 .tpg_check_demo_mode_write_protect = ft_check_false, 528 .tpg_check_prod_mode_write_protect = ft_check_false, 529 .tpg_alloc_fabric_acl = ft_tpg_alloc_fabric_acl, 530 .tpg_release_fabric_acl = ft_tpg_release_fabric_acl, 531 .tpg_get_inst_index = ft_tpg_get_inst_index, 532 .check_stop_free = ft_check_stop_free, 533 .release_cmd = ft_release_cmd, 534 .shutdown_session = ft_sess_shutdown, 535 .close_session = ft_sess_close, 536 .stop_session = ft_sess_stop, 537 .fall_back_to_erl0 = ft_sess_set_erl0, 538 .sess_logged_in = ft_sess_logged_in, 539 .sess_get_index = ft_sess_get_index, 540 .sess_get_initiator_sid = NULL, 541 .write_pending = ft_write_pending, 542 .write_pending_status = ft_write_pending_status, 543 .set_default_node_attributes = ft_set_default_node_attr, 544 .get_task_tag = ft_get_task_tag, 545 .get_cmd_state = ft_get_cmd_state, 546 .queue_data_in = ft_queue_data_in, 547 .queue_status = ft_queue_status, 548 .queue_tm_rsp = ft_queue_tm_resp, 549 .get_fabric_sense_len = ft_get_fabric_sense_len, 550 .set_fabric_sense_len = ft_set_fabric_sense_len, 551 .is_state_remove = ft_is_state_remove, 552 /* 553 * Setup function pointers for generic logic in 554 * target_core_fabric_configfs.c 555 */ 556 .fabric_make_wwn = &ft_add_lport, 557 .fabric_drop_wwn = &ft_del_lport, 558 .fabric_make_tpg = &ft_add_tpg, 559 .fabric_drop_tpg = &ft_del_tpg, 560 .fabric_post_link = NULL, 561 .fabric_pre_unlink = NULL, 562 .fabric_make_np = NULL, 563 .fabric_drop_np = NULL, 564 .fabric_make_nodeacl = &ft_add_acl, 565 .fabric_drop_nodeacl = &ft_del_acl, 566 }; 567 568 int ft_register_configfs(void) 569 { 570 struct target_fabric_configfs *fabric; 571 int ret; 572 573 /* 574 * Register the top level struct config_item_type with TCM core 575 */ 576 fabric = target_fabric_configfs_init(THIS_MODULE, "fc"); 577 if (IS_ERR(fabric)) { 578 pr_err("%s: target_fabric_configfs_init() failed!\n", 579 __func__); 580 return PTR_ERR(fabric); 581 } 582 fabric->tf_ops = ft_fabric_ops; 583 584 /* Allowing support for task_sg_chaining */ 585 fabric->tf_ops.task_sg_chaining = 1; 586 587 /* 588 * Setup default attribute lists for various fabric->tf_cit_tmpl 589 */ 590 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = ft_wwn_attrs; 591 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = NULL; 592 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; 593 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; 594 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; 595 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = 596 ft_nacl_base_attrs; 597 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; 598 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL; 599 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL; 600 /* 601 * register the fabric for use within TCM 602 */ 603 ret = target_fabric_configfs_register(fabric); 604 if (ret < 0) { 605 pr_debug("target_fabric_configfs_register() for" 606 " FC Target failed!\n"); 607 target_fabric_configfs_free(fabric); 608 return -1; 609 } 610 611 /* 612 * Setup our local pointer to *fabric. 613 */ 614 ft_configfs = fabric; 615 return 0; 616 } 617 618 void ft_deregister_configfs(void) 619 { 620 if (!ft_configfs) 621 return; 622 target_fabric_configfs_deregister(ft_configfs); 623 ft_configfs = NULL; 624 } 625 626 static struct notifier_block ft_notifier = { 627 .notifier_call = ft_lport_notify 628 }; 629 630 static int __init ft_init(void) 631 { 632 if (ft_register_configfs()) 633 return -1; 634 if (fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov)) { 635 ft_deregister_configfs(); 636 return -1; 637 } 638 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier); 639 fc_lport_iterate(ft_lport_add, NULL); 640 return 0; 641 } 642 643 static void __exit ft_exit(void) 644 { 645 blocking_notifier_chain_unregister(&fc_lport_notifier_head, 646 &ft_notifier); 647 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov); 648 fc_lport_iterate(ft_lport_del, NULL); 649 ft_deregister_configfs(); 650 synchronize_rcu(); 651 } 652 653 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION); 654 MODULE_LICENSE("GPL"); 655 module_init(ft_init); 656 module_exit(ft_exit); 657