1 /*******************************************************************************
4 * This file contains the configfs implementation for TCM_fc fabric node.
5 * Based on tcm_loop_configfs.c
7 * Copyright (c) 2010 Cisco Systems, Inc.
8 * Copyright (c) 2009,2010 Rising Tide, Inc.
9 * Copyright (c) 2009,2010 Linux-iSCSI.org
11 * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
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>
43 #include <target/target_core_base.h>
44 #include <target/target_core_fabric.h>
45 #include <target/target_core_fabric_configfs.h>
46 #include <target/target_core_configfs.h>
47 #include <target/configfs_macros.h>
51 struct target_fabric_configfs *ft_configfs;
53 static LIST_HEAD(ft_lport_list);
54 DEFINE_MUTEX(ft_lport_lock);
56 unsigned int ft_debug_logging;
57 module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
58 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
62 * If strict, we require lower-case hex and colon separators to be sure
63 * the name is the same as what would be generated by ft_format_wwn()
64 * so the name and wwn are mapped one-to-one.
66 static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
76 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
78 if (c == '\n' && cp[1] == '\0')
80 if (strict && pos++ == 2 && byte++ < 7) {
89 if (strict && byte != 8)
95 if (val < 0 || (strict && isupper(c)))
97 *wwn = (*wwn << 4) | val;
101 pr_debug("err %u len %zu pos %u byte %u\n",
102 err, cp - name, pos, byte);
106 ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
110 put_unaligned_be64(wwn, b);
111 return snprintf(buf, len,
112 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
113 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
116 static ssize_t ft_wwn_show(void *arg, char *buf)
121 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
126 static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
131 ret = ft_parse_wwn(buf, &wwn, 0);
141 static ssize_t ft_nacl_show_port_name(
142 struct se_node_acl *se_nacl,
145 struct ft_node_acl *acl = container_of(se_nacl,
146 struct ft_node_acl, se_node_acl);
148 return ft_wwn_show(&acl->node_auth.port_name, page);
151 static ssize_t ft_nacl_store_port_name(
152 struct se_node_acl *se_nacl,
156 struct ft_node_acl *acl = container_of(se_nacl,
157 struct ft_node_acl, se_node_acl);
159 return ft_wwn_store(&acl->node_auth.port_name, page, count);
162 TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR);
164 static ssize_t ft_nacl_show_node_name(
165 struct se_node_acl *se_nacl,
168 struct ft_node_acl *acl = container_of(se_nacl,
169 struct ft_node_acl, se_node_acl);
171 return ft_wwn_show(&acl->node_auth.node_name, page);
174 static ssize_t ft_nacl_store_node_name(
175 struct se_node_acl *se_nacl,
179 struct ft_node_acl *acl = container_of(se_nacl,
180 struct ft_node_acl, se_node_acl);
182 return ft_wwn_store(&acl->node_auth.node_name, page, count);
185 TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR);
187 static struct configfs_attribute *ft_nacl_base_attrs[] = {
188 &ft_nacl_port_name.attr,
189 &ft_nacl_node_name.attr,
198 * Add ACL for an initiator. The ACL is named arbitrarily.
199 * The port_name and/or node_name are attributes.
201 static struct se_node_acl *ft_add_acl(
202 struct se_portal_group *se_tpg,
203 struct config_group *group,
206 struct ft_node_acl *acl;
211 pr_debug("add acl %s\n", name);
212 tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
214 if (ft_parse_wwn(name, &wwpn, 1) < 0)
215 return ERR_PTR(-EINVAL);
217 acl = kzalloc(sizeof(struct ft_node_acl), GFP_KERNEL);
219 return ERR_PTR(-ENOMEM);
220 acl->node_auth.port_name = wwpn;
222 q_depth = 32; /* XXX bogus default - get from tpg? */
223 return core_tpg_add_initiator_node_acl(&tpg->se_tpg,
224 &acl->se_node_acl, name, q_depth);
227 static void ft_del_acl(struct se_node_acl *se_acl)
229 struct se_portal_group *se_tpg = se_acl->se_tpg;
231 struct ft_node_acl *acl = container_of(se_acl,
232 struct ft_node_acl, se_node_acl);
234 pr_debug("del acl %s\n",
235 config_item_name(&se_acl->acl_group.cg_item));
237 tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
238 pr_debug("del acl %p se_acl %p tpg %p se_tpg %p\n",
239 acl, se_acl, tpg, &tpg->se_tpg);
241 core_tpg_del_initiator_node_acl(&tpg->se_tpg, se_acl, 1);
245 struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
247 struct ft_node_acl *found = NULL;
248 struct ft_node_acl *acl;
249 struct se_portal_group *se_tpg = &tpg->se_tpg;
250 struct se_node_acl *se_acl;
252 spin_lock_irq(&se_tpg->acl_node_lock);
253 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) {
254 acl = container_of(se_acl, struct ft_node_acl, se_node_acl);
255 pr_debug("acl %p port_name %llx\n",
256 acl, (unsigned long long)acl->node_auth.port_name);
257 if (acl->node_auth.port_name == rdata->ids.port_name ||
258 acl->node_auth.node_name == rdata->ids.node_name) {
259 pr_debug("acl %p port_name %llx matched\n", acl,
260 (unsigned long long)rdata->ids.port_name);
262 /* XXX need to hold onto ACL */
266 spin_unlock_irq(&se_tpg->acl_node_lock);
270 static struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg)
272 struct ft_node_acl *acl;
274 acl = kzalloc(sizeof(*acl), GFP_KERNEL);
276 pr_err("Unable to allocate struct ft_node_acl\n");
279 pr_debug("acl %p\n", acl);
280 return &acl->se_node_acl;
283 static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg,
284 struct se_node_acl *se_acl)
286 struct ft_node_acl *acl = container_of(se_acl,
287 struct ft_node_acl, se_node_acl);
289 pr_debug("acl %p\n", acl);
294 * local_port port_group (tpg) ops.
296 static struct se_portal_group *ft_add_tpg(
298 struct config_group *group,
301 struct ft_lport_acl *lacl;
303 struct workqueue_struct *wq;
307 pr_debug("tcm_fc: add tpg %s\n", name);
310 * Name must be "tpgt_" followed by the index.
312 if (strstr(name, "tpgt_") != name)
315 ret = kstrtoul(name + 5, 10, &index);
318 if (index > UINT_MAX)
322 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
323 return ERR_PTR(-ENOSYS);
326 lacl = container_of(wwn, struct ft_lport_acl, fc_lport_wwn);
327 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
331 tpg->lport_acl = lacl;
332 INIT_LIST_HEAD(&tpg->lun_list);
334 wq = alloc_workqueue("tcm_fc", 0, 1);
340 ret = core_tpg_register(&ft_configfs->tf_ops, wwn, &tpg->se_tpg,
341 tpg, TRANSPORT_TPG_TYPE_NORMAL);
343 destroy_workqueue(wq);
349 mutex_lock(&ft_lport_lock);
351 mutex_unlock(&ft_lport_lock);
356 static void ft_del_tpg(struct se_portal_group *se_tpg)
358 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
359 struct ft_lport_acl *lacl = tpg->lport_acl;
361 pr_debug("del tpg %s\n",
362 config_item_name(&tpg->se_tpg.tpg_group.cg_item));
364 destroy_workqueue(tpg->workqueue);
366 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
369 mutex_lock(&ft_lport_lock);
373 tpg->tport->tpg = NULL;
376 mutex_unlock(&ft_lport_lock);
378 core_tpg_deregister(se_tpg);
383 * Verify that an lport is configured to use the tcm_fc module, and return
384 * the target port group that should be used.
386 * The caller holds ft_lport_lock.
388 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
390 struct ft_lport_acl *lacl;
392 list_for_each_entry(lacl, &ft_lport_list, list) {
393 if (lacl->wwpn == lport->wwpn)
400 * target config instance ops.
404 * Add lport to allowed config.
405 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
407 static struct se_wwn *ft_add_lport(
408 struct target_fabric_configfs *tf,
409 struct config_group *group,
412 struct ft_lport_acl *lacl;
413 struct ft_lport_acl *old_lacl;
416 pr_debug("add lport %s\n", name);
417 if (ft_parse_wwn(name, &wwpn, 1) < 0)
419 lacl = kzalloc(sizeof(*lacl), GFP_KERNEL);
424 mutex_lock(&ft_lport_lock);
425 list_for_each_entry(old_lacl, &ft_lport_list, list) {
426 if (old_lacl->wwpn == wwpn) {
427 mutex_unlock(&ft_lport_lock);
432 list_add_tail(&lacl->list, &ft_lport_list);
433 ft_format_wwn(lacl->name, sizeof(lacl->name), wwpn);
434 mutex_unlock(&ft_lport_lock);
436 return &lacl->fc_lport_wwn;
439 static void ft_del_lport(struct se_wwn *wwn)
441 struct ft_lport_acl *lacl = container_of(wwn,
442 struct ft_lport_acl, fc_lport_wwn);
444 pr_debug("del lport %s\n", lacl->name);
445 mutex_lock(&ft_lport_lock);
446 list_del(&lacl->list);
447 mutex_unlock(&ft_lport_lock);
452 static ssize_t ft_wwn_show_attr_version(
453 struct target_fabric_configfs *tf,
456 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
457 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
460 TF_WWN_ATTR_RO(ft, version);
462 static struct configfs_attribute *ft_wwn_attrs[] = {
463 &ft_wwn_version.attr,
467 static char *ft_get_fabric_name(void)
472 static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
474 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
476 return tpg->lport_acl->name;
479 static u16 ft_get_tag(struct se_portal_group *se_tpg)
481 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
484 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
485 * to represent the SCSI Target Port.
490 static u32 ft_get_default_depth(struct se_portal_group *se_tpg)
495 static int ft_check_false(struct se_portal_group *se_tpg)
500 static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
504 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
506 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
511 static struct target_core_fabric_ops ft_fabric_ops = {
512 .get_fabric_name = ft_get_fabric_name,
513 .get_fabric_proto_ident = fc_get_fabric_proto_ident,
514 .tpg_get_wwn = ft_get_fabric_wwn,
515 .tpg_get_tag = ft_get_tag,
516 .tpg_get_default_depth = ft_get_default_depth,
517 .tpg_get_pr_transport_id = fc_get_pr_transport_id,
518 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len,
519 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id,
520 .tpg_check_demo_mode = ft_check_false,
521 .tpg_check_demo_mode_cache = ft_check_false,
522 .tpg_check_demo_mode_write_protect = ft_check_false,
523 .tpg_check_prod_mode_write_protect = ft_check_false,
524 .tpg_alloc_fabric_acl = ft_tpg_alloc_fabric_acl,
525 .tpg_release_fabric_acl = ft_tpg_release_fabric_acl,
526 .tpg_get_inst_index = ft_tpg_get_inst_index,
527 .check_stop_free = ft_check_stop_free,
528 .release_cmd = ft_release_cmd,
529 .shutdown_session = ft_sess_shutdown,
530 .close_session = ft_sess_close,
531 .sess_get_index = ft_sess_get_index,
532 .sess_get_initiator_sid = NULL,
533 .write_pending = ft_write_pending,
534 .write_pending_status = ft_write_pending_status,
535 .set_default_node_attributes = ft_set_default_node_attr,
536 .get_task_tag = ft_get_task_tag,
537 .get_cmd_state = ft_get_cmd_state,
538 .queue_data_in = ft_queue_data_in,
539 .queue_status = ft_queue_status,
540 .queue_tm_rsp = ft_queue_tm_resp,
541 .aborted_task = ft_aborted_task,
543 * Setup function pointers for generic logic in
544 * target_core_fabric_configfs.c
546 .fabric_make_wwn = &ft_add_lport,
547 .fabric_drop_wwn = &ft_del_lport,
548 .fabric_make_tpg = &ft_add_tpg,
549 .fabric_drop_tpg = &ft_del_tpg,
550 .fabric_post_link = NULL,
551 .fabric_pre_unlink = NULL,
552 .fabric_make_np = NULL,
553 .fabric_drop_np = NULL,
554 .fabric_make_nodeacl = &ft_add_acl,
555 .fabric_drop_nodeacl = &ft_del_acl,
558 static int ft_register_configfs(void)
560 struct target_fabric_configfs *fabric;
564 * Register the top level struct config_item_type with TCM core
566 fabric = target_fabric_configfs_init(THIS_MODULE, "fc");
567 if (IS_ERR(fabric)) {
568 pr_err("%s: target_fabric_configfs_init() failed!\n",
570 return PTR_ERR(fabric);
572 fabric->tf_ops = ft_fabric_ops;
575 * Setup default attribute lists for various fabric->tf_cit_tmpl
577 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = ft_wwn_attrs;
578 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = NULL;
579 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
580 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
581 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
582 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs =
584 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
585 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
586 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
588 * register the fabric for use within TCM
590 ret = target_fabric_configfs_register(fabric);
592 pr_debug("target_fabric_configfs_register() for"
593 " FC Target failed!\n");
594 target_fabric_configfs_free(fabric);
599 * Setup our local pointer to *fabric.
601 ft_configfs = fabric;
605 static void ft_deregister_configfs(void)
609 target_fabric_configfs_deregister(ft_configfs);
613 static struct notifier_block ft_notifier = {
614 .notifier_call = ft_lport_notify
617 static int __init ft_init(void)
619 if (ft_register_configfs())
621 if (fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov)) {
622 ft_deregister_configfs();
625 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
626 fc_lport_iterate(ft_lport_add, NULL);
630 static void __exit ft_exit(void)
632 blocking_notifier_chain_unregister(&fc_lport_notifier_head,
634 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
635 fc_lport_iterate(ft_lport_del, NULL);
636 ft_deregister_configfs();
640 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
641 MODULE_LICENSE("GPL");
642 module_init(ft_init);
643 module_exit(ft_exit);