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 <linux/version.h>
27 #include <generated/utsrelease.h>
28 #include <linux/utsname.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/kthread.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/configfs.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_transport.h>
45 #include <target/target_core_fabric_ops.h>
46 #include <target/target_core_fabric_configfs.h>
47 #include <target/target_core_fabric_lib.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tpg.h>
50 #include <target/target_core_configfs.h>
51 #include <target/target_core_base.h>
52 #include <target/configfs_macros.h>
56 struct target_fabric_configfs
*ft_configfs
;
58 LIST_HEAD(ft_lport_list
);
59 DEFINE_MUTEX(ft_lport_lock
);
61 unsigned int ft_debug_logging
;
62 module_param_named(debug_logging
, ft_debug_logging
, int, S_IRUGO
|S_IWUSR
);
63 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
67 * If strict, we require lower-case hex and colon separators to be sure
68 * the name is the same as what would be generated by ft_format_wwn()
69 * so the name and wwn are mapped one-to-one.
71 static ssize_t
ft_parse_wwn(const char *name
, u64
*wwn
, int strict
)
81 for (cp
= name
; cp
< &name
[FT_NAMELEN
- 1]; cp
++) {
83 if (c
== '\n' && cp
[1] == '\0')
85 if (strict
&& pos
++ == 2 && byte
++ < 7) {
94 if (strict
&& byte
!= 8)
101 else if (isxdigit(c
) && (islower(c
) || !strict
))
102 nibble
= tolower(c
) - 'a' + 10;
105 *wwn
= (*wwn
<< 4) | nibble
;
109 FT_CONF_DBG("err %u len %zu pos %u byte %u\n",
110 err
, cp
- name
, pos
, byte
);
114 ssize_t
ft_format_wwn(char *buf
, size_t len
, u64 wwn
)
118 put_unaligned_be64(wwn
, b
);
119 return snprintf(buf
, len
,
120 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
121 b
[0], b
[1], b
[2], b
[3], b
[4], b
[5], b
[6], b
[7]);
124 static ssize_t
ft_wwn_show(void *arg
, char *buf
)
129 len
= ft_format_wwn(buf
, PAGE_SIZE
- 2, *wwn
);
134 static ssize_t
ft_wwn_store(void *arg
, const char *buf
, size_t len
)
139 ret
= ft_parse_wwn(buf
, &wwn
, 0);
149 static ssize_t
ft_nacl_show_port_name(
150 struct se_node_acl
*se_nacl
,
153 struct ft_node_acl
*acl
= container_of(se_nacl
,
154 struct ft_node_acl
, se_node_acl
);
156 return ft_wwn_show(&acl
->node_auth
.port_name
, page
);
159 static ssize_t
ft_nacl_store_port_name(
160 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_store(&acl
->node_auth
.port_name
, page
, count
);
170 TF_NACL_BASE_ATTR(ft
, port_name
, S_IRUGO
| S_IWUSR
);
172 static ssize_t
ft_nacl_show_node_name(
173 struct se_node_acl
*se_nacl
,
176 struct ft_node_acl
*acl
= container_of(se_nacl
,
177 struct ft_node_acl
, se_node_acl
);
179 return ft_wwn_show(&acl
->node_auth
.node_name
, page
);
182 static ssize_t
ft_nacl_store_node_name(
183 struct se_node_acl
*se_nacl
,
187 struct ft_node_acl
*acl
= container_of(se_nacl
,
188 struct ft_node_acl
, se_node_acl
);
190 return ft_wwn_store(&acl
->node_auth
.node_name
, page
, count
);
193 TF_NACL_BASE_ATTR(ft
, node_name
, S_IRUGO
| S_IWUSR
);
195 static struct configfs_attribute
*ft_nacl_base_attrs
[] = {
196 &ft_nacl_port_name
.attr
,
197 &ft_nacl_node_name
.attr
,
206 * Add ACL for an initiator. The ACL is named arbitrarily.
207 * The port_name and/or node_name are attributes.
209 static struct se_node_acl
*ft_add_acl(
210 struct se_portal_group
*se_tpg
,
211 struct config_group
*group
,
214 struct ft_node_acl
*acl
;
219 FT_CONF_DBG("add acl %s\n", name
);
220 tpg
= container_of(se_tpg
, struct ft_tpg
, se_tpg
);
222 if (ft_parse_wwn(name
, &wwpn
, 1) < 0)
223 return ERR_PTR(-EINVAL
);
225 acl
= kzalloc(sizeof(struct ft_node_acl
), GFP_KERNEL
);
227 return ERR_PTR(-ENOMEM
);
228 acl
->node_auth
.port_name
= wwpn
;
230 q_depth
= 32; /* XXX bogus default - get from tpg? */
231 return core_tpg_add_initiator_node_acl(&tpg
->se_tpg
,
232 &acl
->se_node_acl
, name
, q_depth
);
235 static void ft_del_acl(struct se_node_acl
*se_acl
)
237 struct se_portal_group
*se_tpg
= se_acl
->se_tpg
;
239 struct ft_node_acl
*acl
= container_of(se_acl
,
240 struct ft_node_acl
, se_node_acl
);
242 FT_CONF_DBG("del acl %s\n",
243 config_item_name(&se_acl
->acl_group
.cg_item
));
245 tpg
= container_of(se_tpg
, struct ft_tpg
, se_tpg
);
246 FT_CONF_DBG("del acl %p se_acl %p tpg %p se_tpg %p\n",
247 acl
, se_acl
, tpg
, &tpg
->se_tpg
);
249 core_tpg_del_initiator_node_acl(&tpg
->se_tpg
, se_acl
, 1);
253 struct ft_node_acl
*ft_acl_get(struct ft_tpg
*tpg
, struct fc_rport_priv
*rdata
)
255 struct ft_node_acl
*found
= NULL
;
256 struct ft_node_acl
*acl
;
257 struct se_portal_group
*se_tpg
= &tpg
->se_tpg
;
258 struct se_node_acl
*se_acl
;
260 spin_lock_bh(&se_tpg
->acl_node_lock
);
261 list_for_each_entry(se_acl
, &se_tpg
->acl_node_list
, acl_list
) {
262 acl
= container_of(se_acl
, struct ft_node_acl
, se_node_acl
);
263 FT_CONF_DBG("acl %p port_name %llx\n",
264 acl
, (unsigned long long)acl
->node_auth
.port_name
);
265 if (acl
->node_auth
.port_name
== rdata
->ids
.port_name
||
266 acl
->node_auth
.node_name
== rdata
->ids
.node_name
) {
267 FT_CONF_DBG("acl %p port_name %llx matched\n", acl
,
268 (unsigned long long)rdata
->ids
.port_name
);
270 /* XXX need to hold onto ACL */
274 spin_unlock_bh(&se_tpg
->acl_node_lock
);
278 struct se_node_acl
*ft_tpg_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
280 struct ft_node_acl
*acl
;
282 acl
= kzalloc(sizeof(*acl
), GFP_KERNEL
);
284 printk(KERN_ERR
"Unable to allocate struct ft_node_acl\n");
287 FT_CONF_DBG("acl %p\n", acl
);
288 return &acl
->se_node_acl
;
291 static void ft_tpg_release_fabric_acl(struct se_portal_group
*se_tpg
,
292 struct se_node_acl
*se_acl
)
294 struct ft_node_acl
*acl
= container_of(se_acl
,
295 struct ft_node_acl
, se_node_acl
);
297 FT_CONF_DBG(KERN_INFO
"acl %p\n", acl
);
302 * local_port port_group (tpg) ops.
304 static struct se_portal_group
*ft_add_tpg(
306 struct config_group
*group
,
309 struct ft_lport_acl
*lacl
;
314 FT_CONF_DBG("tcm_fc: add tpg %s\n", name
);
317 * Name must be "tpgt_" followed by the index.
319 if (strstr(name
, "tpgt_") != name
)
321 if (strict_strtoul(name
+ 5, 10, &index
) || index
> UINT_MAX
)
324 lacl
= container_of(wwn
, struct ft_lport_acl
, fc_lport_wwn
);
325 tpg
= kzalloc(sizeof(*tpg
), GFP_KERNEL
);
329 tpg
->lport_acl
= lacl
;
330 INIT_LIST_HEAD(&tpg
->lun_list
);
331 transport_init_queue_obj(&tpg
->qobj
);
333 ret
= core_tpg_register(&ft_configfs
->tf_ops
, wwn
, &tpg
->se_tpg
,
334 (void *)tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
340 tpg
->thread
= kthread_run(ft_thread
, tpg
, "ft_tpg%lu", index
);
341 if (IS_ERR(tpg
->thread
)) {
346 mutex_lock(&ft_lport_lock
);
347 list_add_tail(&tpg
->list
, &lacl
->tpg_list
);
348 mutex_unlock(&ft_lport_lock
);
353 static void ft_del_tpg(struct se_portal_group
*se_tpg
)
355 struct ft_tpg
*tpg
= container_of(se_tpg
, struct ft_tpg
, se_tpg
);
357 FT_CONF_DBG("del tpg %s\n",
358 config_item_name(&tpg
->se_tpg
.tpg_group
.cg_item
));
360 kthread_stop(tpg
->thread
);
362 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
365 mutex_lock(&ft_lport_lock
);
366 list_del(&tpg
->list
);
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_acl
*lacl
;
388 list_for_each_entry(lacl
, &ft_lport_list
, list
) {
389 if (lacl
->wwpn
== lport
->wwpn
) {
390 list_for_each_entry(tpg
, &lacl
->tpg_list
, list
)
391 return tpg
; /* XXX for now return first entry */
399 * target config instance ops.
403 * Add lport to allowed config.
404 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
406 static struct se_wwn
*ft_add_lport(
407 struct target_fabric_configfs
*tf
,
408 struct config_group
*group
,
411 struct ft_lport_acl
*lacl
;
412 struct ft_lport_acl
*old_lacl
;
415 FT_CONF_DBG("add lport %s\n", name
);
416 if (ft_parse_wwn(name
, &wwpn
, 1) < 0)
418 lacl
= kzalloc(sizeof(*lacl
), GFP_KERNEL
);
422 INIT_LIST_HEAD(&lacl
->tpg_list
);
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 FT_CONF_DBG("del lport %s\n",
445 config_item_name(&wwn
->wwn_group
.cg_item
));
446 mutex_lock(&ft_lport_lock
);
447 list_del(&lacl
->list
);
448 mutex_unlock(&ft_lport_lock
);
453 static ssize_t
ft_wwn_show_attr_version(
454 struct target_fabric_configfs
*tf
,
457 return sprintf(page
, "TCM FC " FT_VERSION
" on %s/%s on "
458 ""UTS_RELEASE
"\n", utsname()->sysname
, utsname()->machine
);
461 TF_WWN_ATTR_RO(ft
, version
);
463 static struct configfs_attribute
*ft_wwn_attrs
[] = {
464 &ft_wwn_version
.attr
,
468 static char *ft_get_fabric_name(void)
473 static char *ft_get_fabric_wwn(struct se_portal_group
*se_tpg
)
475 struct ft_tpg
*tpg
= se_tpg
->se_tpg_fabric_ptr
;
477 return tpg
->lport_acl
->name
;
480 static u16
ft_get_tag(struct se_portal_group
*se_tpg
)
482 struct ft_tpg
*tpg
= se_tpg
->se_tpg_fabric_ptr
;
485 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
486 * to represent the SCSI Target Port.
491 static u32
ft_get_default_depth(struct se_portal_group
*se_tpg
)
496 static int ft_check_false(struct se_portal_group
*se_tpg
)
501 static void ft_set_default_node_attr(struct se_node_acl
*se_nacl
)
505 static u16
ft_get_fabric_sense_len(void)
510 static u16
ft_set_fabric_sense_len(struct se_cmd
*se_cmd
, u32 sense_len
)
515 static u32
ft_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
517 struct ft_tpg
*tpg
= se_tpg
->se_tpg_fabric_ptr
;
522 static u64
ft_pack_lun(unsigned int index
)
524 WARN_ON(index
>= 256);
525 /* Caller wants this byte-swapped */
526 return cpu_to_le64((index
& 0xff) << 8);
529 static struct target_core_fabric_ops ft_fabric_ops
= {
530 .get_fabric_name
= ft_get_fabric_name
,
531 .get_fabric_proto_ident
= fc_get_fabric_proto_ident
,
532 .tpg_get_wwn
= ft_get_fabric_wwn
,
533 .tpg_get_tag
= ft_get_tag
,
534 .tpg_get_default_depth
= ft_get_default_depth
,
535 .tpg_get_pr_transport_id
= fc_get_pr_transport_id
,
536 .tpg_get_pr_transport_id_len
= fc_get_pr_transport_id_len
,
537 .tpg_parse_pr_out_transport_id
= fc_parse_pr_out_transport_id
,
538 .tpg_check_demo_mode
= ft_check_false
,
539 .tpg_check_demo_mode_cache
= ft_check_false
,
540 .tpg_check_demo_mode_write_protect
= ft_check_false
,
541 .tpg_check_prod_mode_write_protect
= ft_check_false
,
542 .tpg_alloc_fabric_acl
= ft_tpg_alloc_fabric_acl
,
543 .tpg_release_fabric_acl
= ft_tpg_release_fabric_acl
,
544 .tpg_get_inst_index
= ft_tpg_get_inst_index
,
545 .check_stop_free
= ft_check_stop_free
,
546 .release_cmd_to_pool
= ft_release_cmd
,
547 .release_cmd_direct
= ft_release_cmd
,
548 .shutdown_session
= ft_sess_shutdown
,
549 .close_session
= ft_sess_close
,
550 .stop_session
= ft_sess_stop
,
551 .fall_back_to_erl0
= ft_sess_set_erl0
,
552 .sess_logged_in
= ft_sess_logged_in
,
553 .sess_get_index
= ft_sess_get_index
,
554 .sess_get_initiator_sid
= NULL
,
555 .write_pending
= ft_write_pending
,
556 .write_pending_status
= ft_write_pending_status
,
557 .set_default_node_attributes
= ft_set_default_node_attr
,
558 .get_task_tag
= ft_get_task_tag
,
559 .get_cmd_state
= ft_get_cmd_state
,
560 .new_cmd_failure
= ft_new_cmd_failure
,
561 .queue_data_in
= ft_queue_data_in
,
562 .queue_status
= ft_queue_status
,
563 .queue_tm_rsp
= ft_queue_tm_resp
,
564 .get_fabric_sense_len
= ft_get_fabric_sense_len
,
565 .set_fabric_sense_len
= ft_set_fabric_sense_len
,
566 .is_state_remove
= ft_is_state_remove
,
567 .pack_lun
= ft_pack_lun
,
569 * Setup function pointers for generic logic in
570 * target_core_fabric_configfs.c
572 .fabric_make_wwn
= &ft_add_lport
,
573 .fabric_drop_wwn
= &ft_del_lport
,
574 .fabric_make_tpg
= &ft_add_tpg
,
575 .fabric_drop_tpg
= &ft_del_tpg
,
576 .fabric_post_link
= NULL
,
577 .fabric_pre_unlink
= NULL
,
578 .fabric_make_np
= NULL
,
579 .fabric_drop_np
= NULL
,
580 .fabric_make_nodeacl
= &ft_add_acl
,
581 .fabric_drop_nodeacl
= &ft_del_acl
,
584 int ft_register_configfs(void)
586 struct target_fabric_configfs
*fabric
;
590 * Register the top level struct config_item_type with TCM core
592 fabric
= target_fabric_configfs_init(THIS_MODULE
, "fc");
594 printk(KERN_INFO
"%s: target_fabric_configfs_init() failed!\n",
598 fabric
->tf_ops
= ft_fabric_ops
;
600 /* Allowing support for task_sg_chaining */
601 fabric
->tf_ops
.task_sg_chaining
= 1;
604 * Setup default attribute lists for various fabric->tf_cit_tmpl
606 TF_CIT_TMPL(fabric
)->tfc_wwn_cit
.ct_attrs
= ft_wwn_attrs
;
607 TF_CIT_TMPL(fabric
)->tfc_tpg_base_cit
.ct_attrs
= NULL
;
608 TF_CIT_TMPL(fabric
)->tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
609 TF_CIT_TMPL(fabric
)->tfc_tpg_param_cit
.ct_attrs
= NULL
;
610 TF_CIT_TMPL(fabric
)->tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
611 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_base_cit
.ct_attrs
=
613 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
614 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
615 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
617 * register the fabric for use within TCM
619 ret
= target_fabric_configfs_register(fabric
);
621 FT_CONF_DBG("target_fabric_configfs_register() for"
622 " FC Target failed!\n");
624 "%s: target_fabric_configfs_register() failed!\n",
626 target_fabric_configfs_free(fabric
);
631 * Setup our local pointer to *fabric.
633 ft_configfs
= fabric
;
637 void ft_deregister_configfs(void)
641 target_fabric_configfs_deregister(ft_configfs
);
645 static struct notifier_block ft_notifier
= {
646 .notifier_call
= ft_lport_notify
649 static int __init
ft_init(void)
651 if (ft_register_configfs())
653 if (fc_fc4_register_provider(FC_TYPE_FCP
, &ft_prov
)) {
654 ft_deregister_configfs();
657 blocking_notifier_chain_register(&fc_lport_notifier_head
, &ft_notifier
);
658 fc_lport_iterate(ft_lport_add
, NULL
);
662 static void __exit
ft_exit(void)
664 blocking_notifier_chain_unregister(&fc_lport_notifier_head
,
666 fc_fc4_deregister_provider(FC_TYPE_FCP
, &ft_prov
);
667 fc_lport_iterate(ft_lport_del
, NULL
);
668 ft_deregister_configfs();
673 MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION
);
674 MODULE_LICENSE("GPL");
675 module_init(ft_init
);
676 module_exit(ft_exit
);