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 static const struct target_core_fabric_ops ft_fabric_ops;
53 static LIST_HEAD(ft_wwn_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 int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
203 struct ft_node_acl *acl =
204 container_of(nacl, struct ft_node_acl, se_node_acl);
207 if (ft_parse_wwn(name, &wwpn, 1) < 0)
210 acl->node_auth.port_name = wwpn;
214 struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
216 struct ft_node_acl *found = NULL;
217 struct ft_node_acl *acl;
218 struct se_portal_group *se_tpg = &tpg->se_tpg;
219 struct se_node_acl *se_acl;
221 spin_lock_irq(&se_tpg->acl_node_lock);
222 list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) {
223 acl = container_of(se_acl, struct ft_node_acl, se_node_acl);
224 pr_debug("acl %p port_name %llx\n",
225 acl, (unsigned long long)acl->node_auth.port_name);
226 if (acl->node_auth.port_name == rdata->ids.port_name ||
227 acl->node_auth.node_name == rdata->ids.node_name) {
228 pr_debug("acl %p port_name %llx matched\n", acl,
229 (unsigned long long)rdata->ids.port_name);
231 /* XXX need to hold onto ACL */
235 spin_unlock_irq(&se_tpg->acl_node_lock);
240 * local_port port_group (tpg) ops.
242 static struct se_portal_group *ft_add_tpg(
244 struct config_group *group,
247 struct ft_lport_wwn *ft_wwn;
249 struct workqueue_struct *wq;
253 pr_debug("tcm_fc: add tpg %s\n", name);
256 * Name must be "tpgt_" followed by the index.
258 if (strstr(name, "tpgt_") != name)
261 ret = kstrtoul(name + 5, 10, &index);
264 if (index > UINT_MAX)
268 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
269 return ERR_PTR(-ENOSYS);
272 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
273 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
277 tpg->lport_wwn = ft_wwn;
278 INIT_LIST_HEAD(&tpg->lun_list);
280 wq = alloc_workqueue("tcm_fc", 0, 1);
286 ret = core_tpg_register(&ft_fabric_ops, wwn, &tpg->se_tpg,
287 tpg, TRANSPORT_TPG_TYPE_NORMAL);
289 destroy_workqueue(wq);
295 mutex_lock(&ft_lport_lock);
297 mutex_unlock(&ft_lport_lock);
302 static void ft_del_tpg(struct se_portal_group *se_tpg)
304 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
305 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
307 pr_debug("del tpg %s\n",
308 config_item_name(&tpg->se_tpg.tpg_group.cg_item));
310 destroy_workqueue(tpg->workqueue);
312 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
315 mutex_lock(&ft_lport_lock);
318 tpg->tport->tpg = NULL;
321 mutex_unlock(&ft_lport_lock);
323 core_tpg_deregister(se_tpg);
328 * Verify that an lport is configured to use the tcm_fc module, and return
329 * the target port group that should be used.
331 * The caller holds ft_lport_lock.
333 struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
335 struct ft_lport_wwn *ft_wwn;
337 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
338 if (ft_wwn->wwpn == lport->wwpn)
345 * target config instance ops.
349 * Add lport to allowed config.
350 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
352 static struct se_wwn *ft_add_wwn(
353 struct target_fabric_configfs *tf,
354 struct config_group *group,
357 struct ft_lport_wwn *ft_wwn;
358 struct ft_lport_wwn *old_ft_wwn;
361 pr_debug("add wwn %s\n", name);
362 if (ft_parse_wwn(name, &wwpn, 1) < 0)
364 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
369 mutex_lock(&ft_lport_lock);
370 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
371 if (old_ft_wwn->wwpn == wwpn) {
372 mutex_unlock(&ft_lport_lock);
377 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
378 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
379 mutex_unlock(&ft_lport_lock);
381 return &ft_wwn->se_wwn;
384 static void ft_del_wwn(struct se_wwn *wwn)
386 struct ft_lport_wwn *ft_wwn = container_of(wwn,
387 struct ft_lport_wwn, se_wwn);
389 pr_debug("del wwn %s\n", ft_wwn->name);
390 mutex_lock(&ft_lport_lock);
391 list_del(&ft_wwn->ft_wwn_node);
392 mutex_unlock(&ft_lport_lock);
397 static ssize_t ft_wwn_show_attr_version(
398 struct target_fabric_configfs *tf,
401 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
402 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
405 TF_WWN_ATTR_RO(ft, version);
407 static struct configfs_attribute *ft_wwn_attrs[] = {
408 &ft_wwn_version.attr,
412 static char *ft_get_fabric_name(void)
417 static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
419 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
421 return tpg->lport_wwn->name;
424 static u16 ft_get_tag(struct se_portal_group *se_tpg)
426 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
429 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
430 * to represent the SCSI Target Port.
435 static int ft_check_false(struct se_portal_group *se_tpg)
440 static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
444 static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
446 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
451 static const struct target_core_fabric_ops ft_fabric_ops = {
452 .module = THIS_MODULE,
454 .node_acl_size = sizeof(struct ft_node_acl),
455 .get_fabric_name = ft_get_fabric_name,
456 .get_fabric_proto_ident = fc_get_fabric_proto_ident,
457 .tpg_get_wwn = ft_get_fabric_wwn,
458 .tpg_get_tag = ft_get_tag,
459 .tpg_get_pr_transport_id = fc_get_pr_transport_id,
460 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len,
461 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id,
462 .tpg_check_demo_mode = ft_check_false,
463 .tpg_check_demo_mode_cache = ft_check_false,
464 .tpg_check_demo_mode_write_protect = ft_check_false,
465 .tpg_check_prod_mode_write_protect = ft_check_false,
466 .tpg_get_inst_index = ft_tpg_get_inst_index,
467 .check_stop_free = ft_check_stop_free,
468 .release_cmd = ft_release_cmd,
469 .shutdown_session = ft_sess_shutdown,
470 .close_session = ft_sess_close,
471 .sess_get_index = ft_sess_get_index,
472 .sess_get_initiator_sid = NULL,
473 .write_pending = ft_write_pending,
474 .write_pending_status = ft_write_pending_status,
475 .set_default_node_attributes = ft_set_default_node_attr,
476 .get_task_tag = ft_get_task_tag,
477 .get_cmd_state = ft_get_cmd_state,
478 .queue_data_in = ft_queue_data_in,
479 .queue_status = ft_queue_status,
480 .queue_tm_rsp = ft_queue_tm_resp,
481 .aborted_task = ft_aborted_task,
483 * Setup function pointers for generic logic in
484 * target_core_fabric_configfs.c
486 .fabric_make_wwn = &ft_add_wwn,
487 .fabric_drop_wwn = &ft_del_wwn,
488 .fabric_make_tpg = &ft_add_tpg,
489 .fabric_drop_tpg = &ft_del_tpg,
490 .fabric_init_nodeacl = &ft_init_nodeacl,
492 .tfc_wwn_attrs = ft_wwn_attrs,
493 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
496 static struct notifier_block ft_notifier = {
497 .notifier_call = ft_lport_notify
500 static int __init ft_init(void)
504 ret = target_register_template(&ft_fabric_ops);
508 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
510 goto out_unregister_template;
512 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
513 fc_lport_iterate(ft_lport_add, NULL);
516 out_unregister_template:
517 target_unregister_template(&ft_fabric_ops);
522 static void __exit ft_exit(void)
524 blocking_notifier_chain_unregister(&fc_lport_notifier_head,
526 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
527 fc_lport_iterate(ft_lport_del, NULL);
528 target_unregister_template(&ft_fabric_ops);
532 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
533 MODULE_LICENSE("GPL");
534 module_init(ft_init);
535 module_exit(ft_exit);