1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
5 * This file contains the configfs implementation for TCM_fc fabric node.
6 * Based on tcm_loop_configfs.c
8 * Copyright (c) 2010 Cisco Systems, Inc.
9 * Copyright (c) 2009,2010 Rising Tide, Inc.
10 * Copyright (c) 2009,2010 Linux-iSCSI.org
12 * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
14 ****************************************************************************/
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <generated/utsrelease.h>
19 #include <linux/utsname.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/kthread.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/configfs.h>
26 #include <linux/kernel.h>
27 #include <linux/ctype.h>
28 #include <asm/unaligned.h>
29 #include <scsi/libfc.h>
31 #include <target/target_core_base.h>
32 #include <target/target_core_fabric.h>
36 static LIST_HEAD(ft_wwn_list
);
37 DEFINE_MUTEX(ft_lport_lock
);
39 unsigned int ft_debug_logging
;
40 module_param_named(debug_logging
, ft_debug_logging
, int, S_IRUGO
|S_IWUSR
);
41 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
45 * If strict, we require lower-case hex and colon separators to be sure
46 * the name is the same as what would be generated by ft_format_wwn()
47 * so the name and wwn are mapped one-to-one.
49 static ssize_t
ft_parse_wwn(const char *name
, u64
*wwn
, int strict
)
59 for (cp
= name
; cp
< &name
[FT_NAMELEN
- 1]; cp
++) {
61 if (c
== '\n' && cp
[1] == '\0')
63 if (strict
&& pos
++ == 2 && byte
++ < 7) {
72 if (strict
&& byte
!= 8)
78 if (val
< 0 || (strict
&& isupper(c
)))
80 *wwn
= (*wwn
<< 4) | val
;
84 pr_debug("err %u len %zu pos %u byte %u\n",
85 err
, cp
- name
, pos
, byte
);
89 ssize_t
ft_format_wwn(char *buf
, size_t len
, u64 wwn
)
93 put_unaligned_be64(wwn
, b
);
94 return snprintf(buf
, len
,
95 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
96 b
[0], b
[1], b
[2], b
[3], b
[4], b
[5], b
[6], b
[7]);
99 static ssize_t
ft_wwn_show(void *arg
, char *buf
)
104 len
= ft_format_wwn(buf
, PAGE_SIZE
- 2, *wwn
);
109 static ssize_t
ft_wwn_store(void *arg
, const char *buf
, size_t len
)
114 ret
= ft_parse_wwn(buf
, &wwn
, 0);
124 static ssize_t
ft_nacl_port_name_show(struct config_item
*item
, char *page
)
126 struct se_node_acl
*se_nacl
= acl_to_nacl(item
);
127 struct ft_node_acl
*acl
= container_of(se_nacl
,
128 struct ft_node_acl
, se_node_acl
);
130 return ft_wwn_show(&acl
->node_auth
.port_name
, page
);
133 static ssize_t
ft_nacl_port_name_store(struct config_item
*item
,
134 const char *page
, size_t count
)
136 struct se_node_acl
*se_nacl
= acl_to_nacl(item
);
137 struct ft_node_acl
*acl
= container_of(se_nacl
,
138 struct ft_node_acl
, se_node_acl
);
140 return ft_wwn_store(&acl
->node_auth
.port_name
, page
, count
);
143 static ssize_t
ft_nacl_node_name_show(struct config_item
*item
,
146 struct se_node_acl
*se_nacl
= acl_to_nacl(item
);
147 struct ft_node_acl
*acl
= container_of(se_nacl
,
148 struct ft_node_acl
, se_node_acl
);
150 return ft_wwn_show(&acl
->node_auth
.node_name
, page
);
153 static ssize_t
ft_nacl_node_name_store(struct config_item
*item
,
154 const char *page
, size_t count
)
156 struct se_node_acl
*se_nacl
= acl_to_nacl(item
);
157 struct ft_node_acl
*acl
= container_of(se_nacl
,
158 struct ft_node_acl
, se_node_acl
);
160 return ft_wwn_store(&acl
->node_auth
.node_name
, page
, count
);
163 CONFIGFS_ATTR(ft_nacl_
, node_name
);
164 CONFIGFS_ATTR(ft_nacl_
, port_name
);
166 static ssize_t
ft_nacl_tag_show(struct config_item
*item
,
169 return snprintf(page
, PAGE_SIZE
, "%s", acl_to_nacl(item
)->acl_tag
);
172 static ssize_t
ft_nacl_tag_store(struct config_item
*item
,
173 const char *page
, size_t count
)
175 struct se_node_acl
*se_nacl
= acl_to_nacl(item
);
178 ret
= core_tpg_set_initiator_node_tag(se_nacl
->se_tpg
, se_nacl
, page
);
185 CONFIGFS_ATTR(ft_nacl_
, tag
);
187 static struct configfs_attribute
*ft_nacl_base_attrs
[] = {
188 &ft_nacl_attr_port_name
,
189 &ft_nacl_attr_node_name
,
199 * Add ACL for an initiator. The ACL is named arbitrarily.
200 * The port_name and/or node_name are attributes.
202 static int ft_init_nodeacl(struct se_node_acl
*nacl
, const char *name
)
204 struct ft_node_acl
*acl
=
205 container_of(nacl
, struct ft_node_acl
, se_node_acl
);
208 if (ft_parse_wwn(name
, &wwpn
, 1) < 0)
211 acl
->node_auth
.port_name
= wwpn
;
216 * local_port port_group (tpg) ops.
218 static struct se_portal_group
*ft_add_tpg(struct se_wwn
*wwn
, const char *name
)
220 struct ft_lport_wwn
*ft_wwn
;
222 struct workqueue_struct
*wq
;
226 pr_debug("tcm_fc: add tpg %s\n", name
);
229 * Name must be "tpgt_" followed by the index.
231 if (strstr(name
, "tpgt_") != name
)
234 ret
= kstrtoul(name
+ 5, 10, &index
);
237 if (index
> UINT_MAX
)
241 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
242 return ERR_PTR(-ENOSYS
);
245 ft_wwn
= container_of(wwn
, struct ft_lport_wwn
, se_wwn
);
246 tpg
= kzalloc(sizeof(*tpg
), GFP_KERNEL
);
250 tpg
->lport_wwn
= ft_wwn
;
251 INIT_LIST_HEAD(&tpg
->lun_list
);
253 wq
= alloc_workqueue("tcm_fc", 0, 1);
259 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, SCSI_PROTOCOL_FCP
);
261 destroy_workqueue(wq
);
267 mutex_lock(&ft_lport_lock
);
269 mutex_unlock(&ft_lport_lock
);
274 static void ft_del_tpg(struct se_portal_group
*se_tpg
)
276 struct ft_tpg
*tpg
= container_of(se_tpg
, struct ft_tpg
, se_tpg
);
277 struct ft_lport_wwn
*ft_wwn
= tpg
->lport_wwn
;
279 pr_debug("del tpg %s\n",
280 config_item_name(&tpg
->se_tpg
.tpg_group
.cg_item
));
282 destroy_workqueue(tpg
->workqueue
);
284 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
287 mutex_lock(&ft_lport_lock
);
290 tpg
->tport
->tpg
= NULL
;
293 mutex_unlock(&ft_lport_lock
);
295 core_tpg_deregister(se_tpg
);
300 * Verify that an lport is configured to use the tcm_fc module, and return
301 * the target port group that should be used.
303 * The caller holds ft_lport_lock.
305 struct ft_tpg
*ft_lport_find_tpg(struct fc_lport
*lport
)
307 struct ft_lport_wwn
*ft_wwn
;
309 list_for_each_entry(ft_wwn
, &ft_wwn_list
, ft_wwn_node
) {
310 if (ft_wwn
->wwpn
== lport
->wwpn
)
317 * target config instance ops.
321 * Add lport to allowed config.
322 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
324 static struct se_wwn
*ft_add_wwn(
325 struct target_fabric_configfs
*tf
,
326 struct config_group
*group
,
329 struct ft_lport_wwn
*ft_wwn
;
330 struct ft_lport_wwn
*old_ft_wwn
;
333 pr_debug("add wwn %s\n", name
);
334 if (ft_parse_wwn(name
, &wwpn
, 1) < 0)
336 ft_wwn
= kzalloc(sizeof(*ft_wwn
), GFP_KERNEL
);
341 mutex_lock(&ft_lport_lock
);
342 list_for_each_entry(old_ft_wwn
, &ft_wwn_list
, ft_wwn_node
) {
343 if (old_ft_wwn
->wwpn
== wwpn
) {
344 mutex_unlock(&ft_lport_lock
);
349 list_add_tail(&ft_wwn
->ft_wwn_node
, &ft_wwn_list
);
350 ft_format_wwn(ft_wwn
->name
, sizeof(ft_wwn
->name
), wwpn
);
351 mutex_unlock(&ft_lport_lock
);
353 return &ft_wwn
->se_wwn
;
356 static void ft_del_wwn(struct se_wwn
*wwn
)
358 struct ft_lport_wwn
*ft_wwn
= container_of(wwn
,
359 struct ft_lport_wwn
, se_wwn
);
361 pr_debug("del wwn %s\n", ft_wwn
->name
);
362 mutex_lock(&ft_lport_lock
);
363 list_del(&ft_wwn
->ft_wwn_node
);
364 mutex_unlock(&ft_lport_lock
);
369 static ssize_t
ft_wwn_version_show(struct config_item
*item
, char *page
)
371 return sprintf(page
, "TCM FC " FT_VERSION
" on %s/%s on "
372 ""UTS_RELEASE
"\n", utsname()->sysname
, utsname()->machine
);
375 CONFIGFS_ATTR_RO(ft_wwn_
, version
);
377 static struct configfs_attribute
*ft_wwn_attrs
[] = {
378 &ft_wwn_attr_version
,
382 static inline struct ft_tpg
*ft_tpg(struct se_portal_group
*se_tpg
)
384 return container_of(se_tpg
, struct ft_tpg
, se_tpg
);
387 static char *ft_get_fabric_wwn(struct se_portal_group
*se_tpg
)
389 return ft_tpg(se_tpg
)->lport_wwn
->name
;
392 static u16
ft_get_tag(struct se_portal_group
*se_tpg
)
395 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
396 * to represent the SCSI Target Port.
398 return ft_tpg(se_tpg
)->index
;
401 static int ft_check_false(struct se_portal_group
*se_tpg
)
406 static void ft_set_default_node_attr(struct se_node_acl
*se_nacl
)
410 static u32
ft_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
412 return ft_tpg(se_tpg
)->index
;
415 static const struct target_core_fabric_ops ft_fabric_ops
= {
416 .module
= THIS_MODULE
,
418 .node_acl_size
= sizeof(struct ft_node_acl
),
419 .tpg_get_wwn
= ft_get_fabric_wwn
,
420 .tpg_get_tag
= ft_get_tag
,
421 .tpg_check_demo_mode
= ft_check_false
,
422 .tpg_check_demo_mode_cache
= ft_check_false
,
423 .tpg_check_demo_mode_write_protect
= ft_check_false
,
424 .tpg_check_prod_mode_write_protect
= ft_check_false
,
425 .tpg_get_inst_index
= ft_tpg_get_inst_index
,
426 .check_stop_free
= ft_check_stop_free
,
427 .release_cmd
= ft_release_cmd
,
428 .close_session
= ft_sess_close
,
429 .sess_get_index
= ft_sess_get_index
,
430 .sess_get_initiator_sid
= NULL
,
431 .write_pending
= ft_write_pending
,
432 .set_default_node_attributes
= ft_set_default_node_attr
,
433 .get_cmd_state
= ft_get_cmd_state
,
434 .queue_data_in
= ft_queue_data_in
,
435 .queue_status
= ft_queue_status
,
436 .queue_tm_rsp
= ft_queue_tm_resp
,
437 .aborted_task
= ft_aborted_task
,
439 * Setup function pointers for generic logic in
440 * target_core_fabric_configfs.c
442 .fabric_make_wwn
= &ft_add_wwn
,
443 .fabric_drop_wwn
= &ft_del_wwn
,
444 .fabric_make_tpg
= &ft_add_tpg
,
445 .fabric_drop_tpg
= &ft_del_tpg
,
446 .fabric_init_nodeacl
= &ft_init_nodeacl
,
448 .tfc_wwn_attrs
= ft_wwn_attrs
,
449 .tfc_tpg_nacl_base_attrs
= ft_nacl_base_attrs
,
452 static struct notifier_block ft_notifier
= {
453 .notifier_call
= ft_lport_notify
456 static int __init
ft_init(void)
460 ret
= target_register_template(&ft_fabric_ops
);
464 ret
= fc_fc4_register_provider(FC_TYPE_FCP
, &ft_prov
);
466 goto out_unregister_template
;
468 blocking_notifier_chain_register(&fc_lport_notifier_head
, &ft_notifier
);
469 fc_lport_iterate(ft_lport_add
, NULL
);
472 out_unregister_template
:
473 target_unregister_template(&ft_fabric_ops
);
478 static void __exit
ft_exit(void)
480 blocking_notifier_chain_unregister(&fc_lport_notifier_head
,
482 fc_fc4_deregister_provider(FC_TYPE_FCP
, &ft_prov
);
483 fc_lport_iterate(ft_lport_del
, NULL
);
484 target_unregister_template(&ft_fabric_ops
);
488 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION
);
489 MODULE_LICENSE("GPL");
490 module_init(ft_init
);
491 module_exit(ft_exit
);