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