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