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>
44 static LIST_HEAD(ft_wwn_list);
45 DEFINE_MUTEX(ft_lport_lock);
47 unsigned int ft_debug_logging;
48 module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
49 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
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.
57 static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
67 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
69 if (c == '\n' && cp[1] == '\0')
71 if (strict && pos++ == 2 && byte++ < 7) {
80 if (strict && byte != 8)
86 if (val < 0 || (strict && isupper(c)))
88 *wwn = (*wwn << 4) | val;
92 pr_debug("err %u len %zu pos %u byte %u\n",
93 err, cp - name, pos, byte);
97 ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
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]);
107 static ssize_t ft_wwn_show(void *arg, char *buf)
112 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
117 static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
122 ret = ft_parse_wwn(buf, &wwn, 0);
132 static ssize_t ft_nacl_port_name_show(struct config_item *item, char *page)
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);
138 return ft_wwn_show(&acl->node_auth.port_name, page);
141 static ssize_t ft_nacl_port_name_store(struct config_item *item,
142 const char *page, size_t count)
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);
148 return ft_wwn_store(&acl->node_auth.port_name, page, count);
151 static ssize_t ft_nacl_node_name_show(struct config_item *item,
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);
158 return ft_wwn_show(&acl->node_auth.node_name, page);
161 static ssize_t ft_nacl_node_name_store(struct config_item *item,
162 const char *page, size_t count)
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);
168 return ft_wwn_store(&acl->node_auth.node_name, page, count);
171 CONFIGFS_ATTR(ft_nacl_, node_name);
172 CONFIGFS_ATTR(ft_nacl_, port_name);
174 static ssize_t ft_nacl_tag_show(struct config_item *item,
177 return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
180 static ssize_t ft_nacl_tag_store(struct config_item *item,
181 const char *page, size_t count)
183 struct se_node_acl *se_nacl = acl_to_nacl(item);
186 ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
193 CONFIGFS_ATTR(ft_nacl_, tag);
195 static struct configfs_attribute *ft_nacl_base_attrs[] = {
196 &ft_nacl_attr_port_name,
197 &ft_nacl_attr_node_name,
207 * Add ACL for an initiator. The ACL is named arbitrarily.
208 * The port_name and/or node_name are attributes.
210 static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
212 struct ft_node_acl *acl =
213 container_of(nacl, struct ft_node_acl, se_node_acl);
216 if (ft_parse_wwn(name, &wwpn, 1) < 0)
219 acl->node_auth.port_name = wwpn;
224 * local_port port_group (tpg) ops.
226 static struct se_portal_group *ft_add_tpg(
228 struct config_group *group,
231 struct ft_lport_wwn *ft_wwn;
233 struct workqueue_struct *wq;
237 pr_debug("tcm_fc: add tpg %s\n", name);
240 * Name must be "tpgt_" followed by the index.
242 if (strstr(name, "tpgt_") != name)
245 ret = kstrtoul(name + 5, 10, &index);
248 if (index > UINT_MAX)
252 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
253 return ERR_PTR(-ENOSYS);
256 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
257 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
261 tpg->lport_wwn = ft_wwn;
262 INIT_LIST_HEAD(&tpg->lun_list);
264 wq = alloc_workqueue("tcm_fc", 0, 1);
270 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
272 destroy_workqueue(wq);
278 mutex_lock(&ft_lport_lock);
280 mutex_unlock(&ft_lport_lock);
285 static void ft_del_tpg(struct se_portal_group *se_tpg)
287 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
288 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
290 pr_debug("del tpg %s\n",
291 config_item_name(&tpg->se_tpg.tpg_group.cg_item));
293 destroy_workqueue(tpg->workqueue);
295 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
298 mutex_lock(&ft_lport_lock);
301 tpg->tport->tpg = NULL;
304 mutex_unlock(&ft_lport_lock);
306 core_tpg_deregister(se_tpg);
311 * Verify that an lport is configured to use the tcm_fc module, and return
312 * the target port group that should be used.
314 * The caller holds ft_lport_lock.
316 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
318 struct ft_lport_wwn *ft_wwn;
320 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
321 if (ft_wwn->wwpn == lport->wwpn)
328 * target config instance ops.
332 * Add lport to allowed config.
333 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
335 static struct se_wwn *ft_add_wwn(
336 struct target_fabric_configfs *tf,
337 struct config_group *group,
340 struct ft_lport_wwn *ft_wwn;
341 struct ft_lport_wwn *old_ft_wwn;
344 pr_debug("add wwn %s\n", name);
345 if (ft_parse_wwn(name, &wwpn, 1) < 0)
347 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
352 mutex_lock(&ft_lport_lock);
353 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
354 if (old_ft_wwn->wwpn == wwpn) {
355 mutex_unlock(&ft_lport_lock);
360 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
361 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
362 mutex_unlock(&ft_lport_lock);
364 return &ft_wwn->se_wwn;
367 static void ft_del_wwn(struct se_wwn *wwn)
369 struct ft_lport_wwn *ft_wwn = container_of(wwn,
370 struct ft_lport_wwn, se_wwn);
372 pr_debug("del wwn %s\n", ft_wwn->name);
373 mutex_lock(&ft_lport_lock);
374 list_del(&ft_wwn->ft_wwn_node);
375 mutex_unlock(&ft_lport_lock);
380 static ssize_t ft_wwn_version_show(struct config_item *item, char *page)
382 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
383 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
386 CONFIGFS_ATTR_RO(ft_wwn_, version);
388 static struct configfs_attribute *ft_wwn_attrs[] = {
389 &ft_wwn_attr_version,
393 static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg)
395 return container_of(se_tpg, struct ft_tpg, se_tpg);
398 static char *ft_get_fabric_name(void)
403 static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
405 return ft_tpg(se_tpg)->lport_wwn->name;
408 static u16 ft_get_tag(struct se_portal_group *se_tpg)
411 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
412 * to represent the SCSI Target Port.
414 return ft_tpg(se_tpg)->index;
417 static int ft_check_false(struct se_portal_group *se_tpg)
422 static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
426 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
428 return ft_tpg(se_tpg)->index;
431 static const struct target_core_fabric_ops ft_fabric_ops = {
432 .module = THIS_MODULE,
434 .node_acl_size = sizeof(struct ft_node_acl),
435 .get_fabric_name = ft_get_fabric_name,
436 .tpg_get_wwn = ft_get_fabric_wwn,
437 .tpg_get_tag = ft_get_tag,
438 .tpg_check_demo_mode = ft_check_false,
439 .tpg_check_demo_mode_cache = ft_check_false,
440 .tpg_check_demo_mode_write_protect = ft_check_false,
441 .tpg_check_prod_mode_write_protect = ft_check_false,
442 .tpg_get_inst_index = ft_tpg_get_inst_index,
443 .check_stop_free = ft_check_stop_free,
444 .release_cmd = ft_release_cmd,
445 .close_session = ft_sess_close,
446 .sess_get_index = ft_sess_get_index,
447 .sess_get_initiator_sid = NULL,
448 .write_pending = ft_write_pending,
449 .write_pending_status = ft_write_pending_status,
450 .set_default_node_attributes = ft_set_default_node_attr,
451 .get_cmd_state = ft_get_cmd_state,
452 .queue_data_in = ft_queue_data_in,
453 .queue_status = ft_queue_status,
454 .queue_tm_rsp = ft_queue_tm_resp,
455 .aborted_task = ft_aborted_task,
457 * Setup function pointers for generic logic in
458 * target_core_fabric_configfs.c
460 .fabric_make_wwn = &ft_add_wwn,
461 .fabric_drop_wwn = &ft_del_wwn,
462 .fabric_make_tpg = &ft_add_tpg,
463 .fabric_drop_tpg = &ft_del_tpg,
464 .fabric_init_nodeacl = &ft_init_nodeacl,
466 .tfc_wwn_attrs = ft_wwn_attrs,
467 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
470 static struct notifier_block ft_notifier = {
471 .notifier_call = ft_lport_notify
474 static int __init ft_init(void)
478 ret = target_register_template(&ft_fabric_ops);
482 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
484 goto out_unregister_template;
486 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
487 fc_lport_iterate(ft_lport_add, NULL);
490 out_unregister_template:
491 target_unregister_template(&ft_fabric_ops);
496 static void __exit ft_exit(void)
498 blocking_notifier_chain_unregister(&fc_lport_notifier_head,
500 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
501 fc_lport_iterate(ft_lport_del, NULL);
502 target_unregister_template(&ft_fabric_ops);
506 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
507 MODULE_LICENSE("GPL");
508 module_init(ft_init);
509 module_exit(ft_exit);