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 42#include "tcm_fc.h" 43 44static LIST_HEAD(ft_wwn_list); 45DEFINE_MUTEX(ft_lport_lock); 46 47unsigned int ft_debug_logging; 48module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR); 49MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); 50 51/* 52 * Parse WWN. 53 * If strict, we require lower-case hex and colon separators to be sure 54 * the name is the same as what would be generated by ft_format_wwn() 55 * so the name and wwn are mapped one-to-one. 56 */ 57static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict) 58{ 59 const char *cp; 60 char c; 61 u32 byte = 0; 62 u32 pos = 0; 63 u32 err; 64 int val; 65 66 *wwn = 0; 67 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) { 68 c = *cp; 69 if (c == '\n' && cp[1] == '\0') 70 continue; 71 if (strict && pos++ == 2 && byte++ < 7) { 72 pos = 0; 73 if (c == ':') 74 continue; 75 err = 1; 76 goto fail; 77 } 78 if (c == '\0') { 79 err = 2; 80 if (strict && byte != 8) 81 goto fail; 82 return cp - name; 83 } 84 err = 3; 85 val = hex_to_bin(c); 86 if (val < 0 || (strict && isupper(c))) 87 goto fail; 88 *wwn = (*wwn << 4) | val; 89 } 90 err = 4; 91fail: 92 pr_debug("err %u len %zu pos %u byte %u\n", 93 err, cp - name, pos, byte); 94 return -1; 95} 96 97ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn) 98{ 99 u8 b[8]; 100 101 put_unaligned_be64(wwn, b); 102 return snprintf(buf, len, 103 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", 104 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); 105} 106 107static ssize_t ft_wwn_show(void *arg, char *buf) 108{ 109 u64 *wwn = arg; 110 ssize_t len; 111 112 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn); 113 buf[len++] = '\n'; 114 return len; 115} 116 117static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len) 118{ 119 ssize_t ret; 120 u64 wwn; 121 122 ret = ft_parse_wwn(buf, &wwn, 0); 123 if (ret > 0) 124 *(u64 *)arg = wwn; 125 return ret; 126} 127 128/* 129 * ACL auth ops. 130 */ 131 132static ssize_t ft_nacl_port_name_show(struct config_item *item, char *page) 133{ 134 struct se_node_acl *se_nacl = acl_to_nacl(item); 135 struct ft_node_acl *acl = container_of(se_nacl, 136 struct ft_node_acl, se_node_acl); 137 138 return ft_wwn_show(&acl->node_auth.port_name, page); 139} 140 141static ssize_t ft_nacl_port_name_store(struct config_item *item, 142 const char *page, size_t count) 143{ 144 struct se_node_acl *se_nacl = acl_to_nacl(item); 145 struct ft_node_acl *acl = container_of(se_nacl, 146 struct ft_node_acl, se_node_acl); 147 148 return ft_wwn_store(&acl->node_auth.port_name, page, count); 149} 150 151static ssize_t ft_nacl_node_name_show(struct config_item *item, 152 char *page) 153{ 154 struct se_node_acl *se_nacl = acl_to_nacl(item); 155 struct ft_node_acl *acl = container_of(se_nacl, 156 struct ft_node_acl, se_node_acl); 157 158 return ft_wwn_show(&acl->node_auth.node_name, page); 159} 160 161static ssize_t ft_nacl_node_name_store(struct config_item *item, 162 const char *page, size_t count) 163{ 164 struct se_node_acl *se_nacl = acl_to_nacl(item); 165 struct ft_node_acl *acl = container_of(se_nacl, 166 struct ft_node_acl, se_node_acl); 167 168 return ft_wwn_store(&acl->node_auth.node_name, page, count); 169} 170 171CONFIGFS_ATTR(ft_nacl_, node_name); 172CONFIGFS_ATTR(ft_nacl_, port_name); 173 174static struct configfs_attribute *ft_nacl_base_attrs[] = { 175 &ft_nacl_attr_port_name, 176 &ft_nacl_attr_node_name, 177 NULL, 178}; 179 180/* 181 * ACL ops. 182 */ 183 184/* 185 * Add ACL for an initiator. The ACL is named arbitrarily. 186 * The port_name and/or node_name are attributes. 187 */ 188static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name) 189{ 190 struct ft_node_acl *acl = 191 container_of(nacl, struct ft_node_acl, se_node_acl); 192 u64 wwpn; 193 194 if (ft_parse_wwn(name, &wwpn, 1) < 0) 195 return -EINVAL; 196 197 acl->node_auth.port_name = wwpn; 198 return 0; 199} 200 201struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata) 202{ 203 struct ft_node_acl *found = NULL; 204 struct ft_node_acl *acl; 205 struct se_portal_group *se_tpg = &tpg->se_tpg; 206 struct se_node_acl *se_acl; 207 208 mutex_lock(&se_tpg->acl_node_mutex); 209 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) { 210 acl = container_of(se_acl, struct ft_node_acl, se_node_acl); 211 pr_debug("acl %p port_name %llx\n", 212 acl, (unsigned long long)acl->node_auth.port_name); 213 if (acl->node_auth.port_name == rdata->ids.port_name || 214 acl->node_auth.node_name == rdata->ids.node_name) { 215 pr_debug("acl %p port_name %llx matched\n", acl, 216 (unsigned long long)rdata->ids.port_name); 217 found = acl; 218 /* XXX need to hold onto ACL */ 219 break; 220 } 221 } 222 mutex_unlock(&se_tpg->acl_node_mutex); 223 return found; 224} 225 226/* 227 * local_port port_group (tpg) ops. 228 */ 229static struct se_portal_group *ft_add_tpg( 230 struct se_wwn *wwn, 231 struct config_group *group, 232 const char *name) 233{ 234 struct ft_lport_wwn *ft_wwn; 235 struct ft_tpg *tpg; 236 struct workqueue_struct *wq; 237 unsigned long index; 238 int ret; 239 240 pr_debug("tcm_fc: add tpg %s\n", name); 241 242 /* 243 * Name must be "tpgt_" followed by the index. 244 */ 245 if (strstr(name, "tpgt_") != name) 246 return NULL; 247 248 ret = kstrtoul(name + 5, 10, &index); 249 if (ret) 250 return NULL; 251 if (index > UINT_MAX) 252 return NULL; 253 254 if ((index != 1)) { 255 pr_err("Error, a single TPG=1 is used for HW port mappings\n"); 256 return ERR_PTR(-ENOSYS); 257 } 258 259 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn); 260 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 261 if (!tpg) 262 return NULL; 263 tpg->index = index; 264 tpg->lport_wwn = ft_wwn; 265 INIT_LIST_HEAD(&tpg->lun_list); 266 267 wq = alloc_workqueue("tcm_fc", 0, 1); 268 if (!wq) { 269 kfree(tpg); 270 return NULL; 271 } 272 273 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP); 274 if (ret < 0) { 275 destroy_workqueue(wq); 276 kfree(tpg); 277 return NULL; 278 } 279 tpg->workqueue = wq; 280 281 mutex_lock(&ft_lport_lock); 282 ft_wwn->tpg = tpg; 283 mutex_unlock(&ft_lport_lock); 284 285 return &tpg->se_tpg; 286} 287 288static void ft_del_tpg(struct se_portal_group *se_tpg) 289{ 290 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg); 291 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn; 292 293 pr_debug("del tpg %s\n", 294 config_item_name(&tpg->se_tpg.tpg_group.cg_item)); 295 296 destroy_workqueue(tpg->workqueue); 297 298 /* Wait for sessions to be freed thru RCU, for BUG_ON below */ 299 synchronize_rcu(); 300 301 mutex_lock(&ft_lport_lock); 302 ft_wwn->tpg = NULL; 303 if (tpg->tport) { 304 tpg->tport->tpg = NULL; 305 tpg->tport = NULL; 306 } 307 mutex_unlock(&ft_lport_lock); 308 309 core_tpg_deregister(se_tpg); 310 kfree(tpg); 311} 312 313/* 314 * Verify that an lport is configured to use the tcm_fc module, and return 315 * the target port group that should be used. 316 * 317 * The caller holds ft_lport_lock. 318 */ 319struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport) 320{ 321 struct ft_lport_wwn *ft_wwn; 322 323 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) { 324 if (ft_wwn->wwpn == lport->wwpn) 325 return ft_wwn->tpg; 326 } 327 return NULL; 328} 329 330/* 331 * target config instance ops. 332 */ 333 334/* 335 * Add lport to allowed config. 336 * The name is the WWPN in lower-case ASCII, colon-separated bytes. 337 */ 338static struct se_wwn *ft_add_wwn( 339 struct target_fabric_configfs *tf, 340 struct config_group *group, 341 const char *name) 342{ 343 struct ft_lport_wwn *ft_wwn; 344 struct ft_lport_wwn *old_ft_wwn; 345 u64 wwpn; 346 347 pr_debug("add wwn %s\n", name); 348 if (ft_parse_wwn(name, &wwpn, 1) < 0) 349 return NULL; 350 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL); 351 if (!ft_wwn) 352 return NULL; 353 ft_wwn->wwpn = wwpn; 354 355 mutex_lock(&ft_lport_lock); 356 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) { 357 if (old_ft_wwn->wwpn == wwpn) { 358 mutex_unlock(&ft_lport_lock); 359 kfree(ft_wwn); 360 return NULL; 361 } 362 } 363 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list); 364 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn); 365 mutex_unlock(&ft_lport_lock); 366 367 return &ft_wwn->se_wwn; 368} 369 370static void ft_del_wwn(struct se_wwn *wwn) 371{ 372 struct ft_lport_wwn *ft_wwn = container_of(wwn, 373 struct ft_lport_wwn, se_wwn); 374 375 pr_debug("del wwn %s\n", ft_wwn->name); 376 mutex_lock(&ft_lport_lock); 377 list_del(&ft_wwn->ft_wwn_node); 378 mutex_unlock(&ft_lport_lock); 379 380 kfree(ft_wwn); 381} 382 383static ssize_t ft_wwn_version_show(struct config_item *item, char *page) 384{ 385 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on " 386 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine); 387} 388 389CONFIGFS_ATTR_RO(ft_wwn_, version); 390 391static struct configfs_attribute *ft_wwn_attrs[] = { 392 &ft_wwn_attr_version, 393 NULL, 394}; 395 396static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg) 397{ 398 return container_of(se_tpg, struct ft_tpg, se_tpg); 399} 400 401static char *ft_get_fabric_name(void) 402{ 403 return "fc"; 404} 405 406static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg) 407{ 408 return ft_tpg(se_tpg)->lport_wwn->name; 409} 410 411static u16 ft_get_tag(struct se_portal_group *se_tpg) 412{ 413 /* 414 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83 415 * to represent the SCSI Target Port. 416 */ 417 return ft_tpg(se_tpg)->index; 418} 419 420static int ft_check_false(struct se_portal_group *se_tpg) 421{ 422 return 0; 423} 424 425static void ft_set_default_node_attr(struct se_node_acl *se_nacl) 426{ 427} 428 429static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg) 430{ 431 return ft_tpg(se_tpg)->index; 432} 433 434static const struct target_core_fabric_ops ft_fabric_ops = { 435 .module = THIS_MODULE, 436 .name = "fc", 437 .node_acl_size = sizeof(struct ft_node_acl), 438 .get_fabric_name = ft_get_fabric_name, 439 .tpg_get_wwn = ft_get_fabric_wwn, 440 .tpg_get_tag = ft_get_tag, 441 .tpg_check_demo_mode = ft_check_false, 442 .tpg_check_demo_mode_cache = ft_check_false, 443 .tpg_check_demo_mode_write_protect = ft_check_false, 444 .tpg_check_prod_mode_write_protect = ft_check_false, 445 .tpg_get_inst_index = ft_tpg_get_inst_index, 446 .check_stop_free = ft_check_stop_free, 447 .release_cmd = ft_release_cmd, 448 .shutdown_session = ft_sess_shutdown, 449 .close_session = ft_sess_close, 450 .sess_get_index = ft_sess_get_index, 451 .sess_get_initiator_sid = NULL, 452 .write_pending = ft_write_pending, 453 .write_pending_status = ft_write_pending_status, 454 .set_default_node_attributes = ft_set_default_node_attr, 455 .get_cmd_state = ft_get_cmd_state, 456 .queue_data_in = ft_queue_data_in, 457 .queue_status = ft_queue_status, 458 .queue_tm_rsp = ft_queue_tm_resp, 459 .aborted_task = ft_aborted_task, 460 /* 461 * Setup function pointers for generic logic in 462 * target_core_fabric_configfs.c 463 */ 464 .fabric_make_wwn = &ft_add_wwn, 465 .fabric_drop_wwn = &ft_del_wwn, 466 .fabric_make_tpg = &ft_add_tpg, 467 .fabric_drop_tpg = &ft_del_tpg, 468 .fabric_init_nodeacl = &ft_init_nodeacl, 469 470 .tfc_wwn_attrs = ft_wwn_attrs, 471 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs, 472}; 473 474static struct notifier_block ft_notifier = { 475 .notifier_call = ft_lport_notify 476}; 477 478static int __init ft_init(void) 479{ 480 int ret; 481 482 ret = target_register_template(&ft_fabric_ops); 483 if (ret) 484 goto out; 485 486 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov); 487 if (ret) 488 goto out_unregister_template; 489 490 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier); 491 fc_lport_iterate(ft_lport_add, NULL); 492 return 0; 493 494out_unregister_template: 495 target_unregister_template(&ft_fabric_ops); 496out: 497 return ret; 498} 499 500static void __exit ft_exit(void) 501{ 502 blocking_notifier_chain_unregister(&fc_lport_notifier_head, 503 &ft_notifier); 504 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov); 505 fc_lport_iterate(ft_lport_del, NULL); 506 target_unregister_template(&ft_fabric_ops); 507 synchronize_rcu(); 508} 509 510MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION); 511MODULE_LICENSE("GPL"); 512module_init(ft_init); 513module_exit(ft_exit); 514