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/libfc.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
41 #include <target/target_core_fabric_configfs.h>
42 #include <target/target_core_configfs.h>
43 #include <target/configfs_macros.h>
47 static const struct target_core_fabric_ops ft_fabric_ops;
49 static LIST_HEAD(ft_wwn_list);
50 DEFINE_MUTEX(ft_lport_lock);
52 unsigned int ft_debug_logging;
53 module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
54 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
58 * If strict, we require lower-case hex and colon separators to be sure
59 * the name is the same as what would be generated by ft_format_wwn()
60 * so the name and wwn are mapped one-to-one.
62 static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
72 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
74 if (c == '\n' && cp[1] == '\0')
76 if (strict && pos++ == 2 && byte++ < 7) {
85 if (strict && byte != 8)
91 if (val < 0 || (strict && isupper(c)))
93 *wwn = (*wwn << 4) | val;
97 pr_debug("err %u len %zu pos %u byte %u\n",
98 err, cp - name, pos, byte);
102 ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
106 put_unaligned_be64(wwn, b);
107 return snprintf(buf, len,
108 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
109 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
112 static ssize_t ft_wwn_show(void *arg, char *buf)
117 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
122 static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
127 ret = ft_parse_wwn(buf, &wwn, 0);
137 static ssize_t ft_nacl_show_port_name(
138 struct se_node_acl *se_nacl,
141 struct ft_node_acl *acl = container_of(se_nacl,
142 struct ft_node_acl, se_node_acl);
144 return ft_wwn_show(&acl->node_auth.port_name, page);
147 static ssize_t ft_nacl_store_port_name(
148 struct se_node_acl *se_nacl,
152 struct ft_node_acl *acl = container_of(se_nacl,
153 struct ft_node_acl, se_node_acl);
155 return ft_wwn_store(&acl->node_auth.port_name, page, count);
158 TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR);
160 static ssize_t ft_nacl_show_node_name(
161 struct se_node_acl *se_nacl,
164 struct ft_node_acl *acl = container_of(se_nacl,
165 struct ft_node_acl, se_node_acl);
167 return ft_wwn_show(&acl->node_auth.node_name, page);
170 static ssize_t ft_nacl_store_node_name(
171 struct se_node_acl *se_nacl,
175 struct ft_node_acl *acl = container_of(se_nacl,
176 struct ft_node_acl, se_node_acl);
178 return ft_wwn_store(&acl->node_auth.node_name, page, count);
181 TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR);
183 static struct configfs_attribute *ft_nacl_base_attrs[] = {
184 &ft_nacl_port_name.attr,
185 &ft_nacl_node_name.attr,
194 * Add ACL for an initiator. The ACL is named arbitrarily.
195 * The port_name and/or node_name are attributes.
197 static struct se_node_acl *ft_add_acl(
198 struct se_portal_group *se_tpg,
199 struct config_group *group,
202 struct ft_node_acl *acl;
207 pr_debug("add acl %s\n", name);
208 tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
210 if (ft_parse_wwn(name, &wwpn, 1) < 0)
211 return ERR_PTR(-EINVAL);
213 acl = kzalloc(sizeof(struct ft_node_acl), GFP_KERNEL);
215 return ERR_PTR(-ENOMEM);
216 acl->node_auth.port_name = wwpn;
218 q_depth = 32; /* XXX bogus default - get from tpg? */
219 return core_tpg_add_initiator_node_acl(&tpg->se_tpg,
220 &acl->se_node_acl, name, q_depth);
223 static void ft_del_acl(struct se_node_acl *se_acl)
225 struct se_portal_group *se_tpg = se_acl->se_tpg;
227 struct ft_node_acl *acl = container_of(se_acl,
228 struct ft_node_acl, se_node_acl);
230 pr_debug("del acl %s\n",
231 config_item_name(&se_acl->acl_group.cg_item));
233 tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
234 pr_debug("del acl %p se_acl %p tpg %p se_tpg %p\n",
235 acl, se_acl, tpg, &tpg->se_tpg);
237 core_tpg_del_initiator_node_acl(&tpg->se_tpg, se_acl, 1);
241 struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
243 struct ft_node_acl *found = NULL;
244 struct ft_node_acl *acl;
245 struct se_portal_group *se_tpg = &tpg->se_tpg;
246 struct se_node_acl *se_acl;
248 spin_lock_irq(&se_tpg->acl_node_lock);
249 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) {
250 acl = container_of(se_acl, struct ft_node_acl, se_node_acl);
251 pr_debug("acl %p port_name %llx\n",
252 acl, (unsigned long long)acl->node_auth.port_name);
253 if (acl->node_auth.port_name == rdata->ids.port_name ||
254 acl->node_auth.node_name == rdata->ids.node_name) {
255 pr_debug("acl %p port_name %llx matched\n", acl,
256 (unsigned long long)rdata->ids.port_name);
258 /* XXX need to hold onto ACL */
262 spin_unlock_irq(&se_tpg->acl_node_lock);
266 static struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg)
268 struct ft_node_acl *acl;
270 acl = kzalloc(sizeof(*acl), GFP_KERNEL);
272 pr_err("Unable to allocate struct ft_node_acl\n");
275 pr_debug("acl %p\n", acl);
276 return &acl->se_node_acl;
279 static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg,
280 struct se_node_acl *se_acl)
282 struct ft_node_acl *acl = container_of(se_acl,
283 struct ft_node_acl, se_node_acl);
285 pr_debug("acl %p\n", acl);
290 * local_port port_group (tpg) ops.
292 static struct se_portal_group *ft_add_tpg(
294 struct config_group *group,
297 struct ft_lport_wwn *ft_wwn;
299 struct workqueue_struct *wq;
303 pr_debug("tcm_fc: add tpg %s\n", name);
306 * Name must be "tpgt_" followed by the index.
308 if (strstr(name, "tpgt_") != name)
311 ret = kstrtoul(name + 5, 10, &index);
314 if (index > UINT_MAX)
318 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
319 return ERR_PTR(-ENOSYS);
322 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
323 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
327 tpg->lport_wwn = ft_wwn;
328 INIT_LIST_HEAD(&tpg->lun_list);
330 wq = alloc_workqueue("tcm_fc", 0, 1);
336 ret = core_tpg_register(&ft_fabric_ops, wwn, &tpg->se_tpg,
337 tpg, TRANSPORT_TPG_TYPE_NORMAL);
339 destroy_workqueue(wq);
345 mutex_lock(&ft_lport_lock);
347 mutex_unlock(&ft_lport_lock);
352 static void ft_del_tpg(struct se_portal_group *se_tpg)
354 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
355 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
357 pr_debug("del tpg %s\n",
358 config_item_name(&tpg->se_tpg.tpg_group.cg_item));
360 destroy_workqueue(tpg->workqueue);
362 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
365 mutex_lock(&ft_lport_lock);
368 tpg->tport->tpg = NULL;
371 mutex_unlock(&ft_lport_lock);
373 core_tpg_deregister(se_tpg);
378 * Verify that an lport is configured to use the tcm_fc module, and return
379 * the target port group that should be used.
381 * The caller holds ft_lport_lock.
383 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
385 struct ft_lport_wwn *ft_wwn;
387 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
388 if (ft_wwn->wwpn == lport->wwpn)
395 * target config instance ops.
399 * Add lport to allowed config.
400 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
402 static struct se_wwn *ft_add_wwn(
403 struct target_fabric_configfs *tf,
404 struct config_group *group,
407 struct ft_lport_wwn *ft_wwn;
408 struct ft_lport_wwn *old_ft_wwn;
411 pr_debug("add wwn %s\n", name);
412 if (ft_parse_wwn(name, &wwpn, 1) < 0)
414 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
419 mutex_lock(&ft_lport_lock);
420 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
421 if (old_ft_wwn->wwpn == wwpn) {
422 mutex_unlock(&ft_lport_lock);
427 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
428 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
429 mutex_unlock(&ft_lport_lock);
431 return &ft_wwn->se_wwn;
434 static void ft_del_wwn(struct se_wwn *wwn)
436 struct ft_lport_wwn *ft_wwn = container_of(wwn,
437 struct ft_lport_wwn, se_wwn);
439 pr_debug("del wwn %s\n", ft_wwn->name);
440 mutex_lock(&ft_lport_lock);
441 list_del(&ft_wwn->ft_wwn_node);
442 mutex_unlock(&ft_lport_lock);
447 static ssize_t ft_wwn_show_attr_version(
448 struct target_fabric_configfs *tf,
451 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
452 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
455 TF_WWN_ATTR_RO(ft, version);
457 static struct configfs_attribute *ft_wwn_attrs[] = {
458 &ft_wwn_version.attr,
462 static char *ft_get_fabric_name(void)
467 static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
469 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
471 return tpg->lport_wwn->name;
474 static u16 ft_get_tag(struct se_portal_group *se_tpg)
476 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
479 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
480 * to represent the SCSI Target Port.
485 static u32 ft_get_default_depth(struct se_portal_group *se_tpg)
490 static int ft_check_false(struct se_portal_group *se_tpg)
495 static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
499 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
501 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
506 static const struct target_core_fabric_ops ft_fabric_ops = {
507 .module = THIS_MODULE,
509 .get_fabric_name = ft_get_fabric_name,
510 .get_fabric_proto_ident = fc_get_fabric_proto_ident,
511 .tpg_get_wwn = ft_get_fabric_wwn,
512 .tpg_get_tag = ft_get_tag,
513 .tpg_get_default_depth = ft_get_default_depth,
514 .tpg_get_pr_transport_id = fc_get_pr_transport_id,
515 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len,
516 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id,
517 .tpg_check_demo_mode = ft_check_false,
518 .tpg_check_demo_mode_cache = ft_check_false,
519 .tpg_check_demo_mode_write_protect = ft_check_false,
520 .tpg_check_prod_mode_write_protect = ft_check_false,
521 .tpg_alloc_fabric_acl = ft_tpg_alloc_fabric_acl,
522 .tpg_release_fabric_acl = ft_tpg_release_fabric_acl,
523 .tpg_get_inst_index = ft_tpg_get_inst_index,
524 .check_stop_free = ft_check_stop_free,
525 .release_cmd = ft_release_cmd,
526 .shutdown_session = ft_sess_shutdown,
527 .close_session = ft_sess_close,
528 .sess_get_index = ft_sess_get_index,
529 .sess_get_initiator_sid = NULL,
530 .write_pending = ft_write_pending,
531 .write_pending_status = ft_write_pending_status,
532 .set_default_node_attributes = ft_set_default_node_attr,
533 .get_task_tag = ft_get_task_tag,
534 .get_cmd_state = ft_get_cmd_state,
535 .queue_data_in = ft_queue_data_in,
536 .queue_status = ft_queue_status,
537 .queue_tm_rsp = ft_queue_tm_resp,
538 .aborted_task = ft_aborted_task,
540 * Setup function pointers for generic logic in
541 * target_core_fabric_configfs.c
543 .fabric_make_wwn = &ft_add_wwn,
544 .fabric_drop_wwn = &ft_del_wwn,
545 .fabric_make_tpg = &ft_add_tpg,
546 .fabric_drop_tpg = &ft_del_tpg,
547 .fabric_post_link = NULL,
548 .fabric_pre_unlink = NULL,
549 .fabric_make_np = NULL,
550 .fabric_drop_np = NULL,
551 .fabric_make_nodeacl = &ft_add_acl,
552 .fabric_drop_nodeacl = &ft_del_acl,
554 .tfc_wwn_attrs = ft_wwn_attrs,
555 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
558 static struct notifier_block ft_notifier = {
559 .notifier_call = ft_lport_notify
562 static int __init ft_init(void)
566 ret = target_register_template(&ft_fabric_ops);
570 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
572 goto out_unregister_template;
574 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
575 fc_lport_iterate(ft_lport_add, NULL);
578 out_unregister_template:
579 target_unregister_template(&ft_fabric_ops);
584 static void __exit ft_exit(void)
586 blocking_notifier_chain_unregister(&fc_lport_notifier_head,
588 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
589 fc_lport_iterate(ft_lport_del, NULL);
590 target_unregister_template(&ft_fabric_ops);
594 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
595 MODULE_LICENSE("GPL");
596 module_init(ft_init);
597 module_exit(ft_exit);