]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/target/tcm_fc/tfc_conf.c
target: refactor init/drop_nodeacl methods
[mirror_ubuntu-bionic-kernel.git] / drivers / target / tcm_fc / tfc_conf.c
CommitLineData
3699d92a
KP
1/*******************************************************************************
2 * Filename: tcm_fc.c
3 *
4 * This file contains the configfs implementation for TCM_fc fabric node.
5 * Based on tcm_loop_configfs.c
6 *
7 * Copyright (c) 2010 Cisco Systems, Inc.
8 * Copyright (c) 2009,2010 Rising Tide, Inc.
9 * Copyright (c) 2009,2010 Linux-iSCSI.org
10 *
11 * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
12 *
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.
17 *
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 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
3699d92a
KP
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>
635a2b3f 34#include <linux/kernel.h>
3699d92a
KP
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>
42
43#include <target/target_core_base.h>
c4795fb2 44#include <target/target_core_fabric.h>
3699d92a 45#include <target/target_core_fabric_configfs.h>
3699d92a 46#include <target/target_core_configfs.h>
3699d92a
KP
47#include <target/configfs_macros.h>
48
49#include "tcm_fc.h"
50
9ac8928e 51static const struct target_core_fabric_ops ft_fabric_ops;
3699d92a 52
705665da 53static LIST_HEAD(ft_wwn_list);
3699d92a
KP
54DEFINE_MUTEX(ft_lport_lock);
55
56unsigned int ft_debug_logging;
57module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
58MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
59
60/*
61 * Parse WWN.
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.
65 */
66static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
67{
68 const char *cp;
69 char c;
3699d92a
KP
70 u32 byte = 0;
71 u32 pos = 0;
72 u32 err;
635a2b3f 73 int val;
3699d92a
KP
74
75 *wwn = 0;
76 for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
77 c = *cp;
78 if (c == '\n' && cp[1] == '\0')
79 continue;
80 if (strict && pos++ == 2 && byte++ < 7) {
81 pos = 0;
82 if (c == ':')
83 continue;
84 err = 1;
85 goto fail;
86 }
87 if (c == '\0') {
88 err = 2;
89 if (strict && byte != 8)
90 goto fail;
91 return cp - name;
92 }
93 err = 3;
635a2b3f
AS
94 val = hex_to_bin(c);
95 if (val < 0 || (strict && isupper(c)))
3699d92a 96 goto fail;
635a2b3f 97 *wwn = (*wwn << 4) | val;
3699d92a
KP
98 }
99 err = 4;
100fail:
6708bb27 101 pr_debug("err %u len %zu pos %u byte %u\n",
3699d92a
KP
102 err, cp - name, pos, byte);
103 return -1;
104}
105
106ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
107{
108 u8 b[8];
109
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]);
114}
115
116static ssize_t ft_wwn_show(void *arg, char *buf)
117{
118 u64 *wwn = arg;
119 ssize_t len;
120
121 len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
122 buf[len++] = '\n';
123 return len;
124}
125
126static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
127{
128 ssize_t ret;
129 u64 wwn;
130
131 ret = ft_parse_wwn(buf, &wwn, 0);
132 if (ret > 0)
133 *(u64 *)arg = wwn;
134 return ret;
135}
136
137/*
138 * ACL auth ops.
139 */
140
141static ssize_t ft_nacl_show_port_name(
142 struct se_node_acl *se_nacl,
143 char *page)
144{
145 struct ft_node_acl *acl = container_of(se_nacl,
146 struct ft_node_acl, se_node_acl);
147
148 return ft_wwn_show(&acl->node_auth.port_name, page);
149}
150
151static ssize_t ft_nacl_store_port_name(
152 struct se_node_acl *se_nacl,
153 const char *page,
154 size_t count)
155{
156 struct ft_node_acl *acl = container_of(se_nacl,
157 struct ft_node_acl, se_node_acl);
158
159 return ft_wwn_store(&acl->node_auth.port_name, page, count);
160}
161
162TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR);
163
164static ssize_t ft_nacl_show_node_name(
165 struct se_node_acl *se_nacl,
166 char *page)
167{
168 struct ft_node_acl *acl = container_of(se_nacl,
169 struct ft_node_acl, se_node_acl);
170
171 return ft_wwn_show(&acl->node_auth.node_name, page);
172}
173
174static ssize_t ft_nacl_store_node_name(
175 struct se_node_acl *se_nacl,
176 const char *page,
177 size_t count)
178{
179 struct ft_node_acl *acl = container_of(se_nacl,
180 struct ft_node_acl, se_node_acl);
181
182 return ft_wwn_store(&acl->node_auth.node_name, page, count);
183}
184
185TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR);
186
187static struct configfs_attribute *ft_nacl_base_attrs[] = {
188 &ft_nacl_port_name.attr,
189 &ft_nacl_node_name.attr,
190 NULL,
191};
192
193/*
194 * ACL ops.
195 */
196
197/*
198 * Add ACL for an initiator. The ACL is named arbitrarily.
199 * The port_name and/or node_name are attributes.
200 */
c7d6a803 201static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
3699d92a 202{
c7d6a803
CH
203 struct ft_node_acl *acl =
204 container_of(nacl, struct ft_node_acl, se_node_acl);
3699d92a 205 u64 wwpn;
3699d92a
KP
206
207 if (ft_parse_wwn(name, &wwpn, 1) < 0)
c7d6a803 208 return -EINVAL;
3699d92a 209
3699d92a 210 acl->node_auth.port_name = wwpn;
c7d6a803 211 return 0;
3699d92a
KP
212}
213
214struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
215{
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;
220
28638887 221 spin_lock_irq(&se_tpg->acl_node_lock);
3699d92a
KP
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);
6708bb27 224 pr_debug("acl %p port_name %llx\n",
3699d92a
KP
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) {
6708bb27 228 pr_debug("acl %p port_name %llx matched\n", acl,
3699d92a
KP
229 (unsigned long long)rdata->ids.port_name);
230 found = acl;
231 /* XXX need to hold onto ACL */
232 break;
233 }
234 }
28638887 235 spin_unlock_irq(&se_tpg->acl_node_lock);
3699d92a
KP
236 return found;
237}
238
594c42e9 239static struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg)
3699d92a
KP
240{
241 struct ft_node_acl *acl;
242
243 acl = kzalloc(sizeof(*acl), GFP_KERNEL);
2be18149 244 if (!acl) {
6708bb27 245 pr_err("Unable to allocate struct ft_node_acl\n");
3699d92a
KP
246 return NULL;
247 }
6708bb27 248 pr_debug("acl %p\n", acl);
3699d92a
KP
249 return &acl->se_node_acl;
250}
251
252static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg,
253 struct se_node_acl *se_acl)
254{
255 struct ft_node_acl *acl = container_of(se_acl,
256 struct ft_node_acl, se_node_acl);
257
6708bb27 258 pr_debug("acl %p\n", acl);
3699d92a
KP
259 kfree(acl);
260}
261
262/*
263 * local_port port_group (tpg) ops.
264 */
265static struct se_portal_group *ft_add_tpg(
266 struct se_wwn *wwn,
267 struct config_group *group,
268 const char *name)
269{
705665da 270 struct ft_lport_wwn *ft_wwn;
3699d92a 271 struct ft_tpg *tpg;
06383f10 272 struct workqueue_struct *wq;
3699d92a
KP
273 unsigned long index;
274 int ret;
275
6708bb27 276 pr_debug("tcm_fc: add tpg %s\n", name);
3699d92a
KP
277
278 /*
279 * Name must be "tpgt_" followed by the index.
280 */
281 if (strstr(name, "tpgt_") != name)
282 return NULL;
57103d7f
JH
283
284 ret = kstrtoul(name + 5, 10, &index);
285 if (ret)
286 return NULL;
287 if (index > UINT_MAX)
3699d92a
KP
288 return NULL;
289
d242c1d7
AG
290 if ((index != 1)) {
291 pr_err("Error, a single TPG=1 is used for HW port mappings\n");
292 return ERR_PTR(-ENOSYS);
293 }
294
705665da 295 ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
3699d92a
KP
296 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
297 if (!tpg)
298 return NULL;
299 tpg->index = index;
705665da 300 tpg->lport_wwn = ft_wwn;
3699d92a 301 INIT_LIST_HEAD(&tpg->lun_list);
3699d92a 302
06383f10
MR
303 wq = alloc_workqueue("tcm_fc", 0, 1);
304 if (!wq) {
3699d92a
KP
305 kfree(tpg);
306 return NULL;
307 }
308
9ac8928e 309 ret = core_tpg_register(&ft_fabric_ops, wwn, &tpg->se_tpg,
06383f10
MR
310 tpg, TRANSPORT_TPG_TYPE_NORMAL);
311 if (ret < 0) {
312 destroy_workqueue(wq);
3699d92a
KP
313 kfree(tpg);
314 return NULL;
315 }
06383f10 316 tpg->workqueue = wq;
3699d92a
KP
317
318 mutex_lock(&ft_lport_lock);
705665da 319 ft_wwn->tpg = tpg;
3699d92a
KP
320 mutex_unlock(&ft_lport_lock);
321
322 return &tpg->se_tpg;
323}
324
325static void ft_del_tpg(struct se_portal_group *se_tpg)
326{
327 struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
705665da 328 struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
3699d92a 329
6708bb27 330 pr_debug("del tpg %s\n",
3699d92a
KP
331 config_item_name(&tpg->se_tpg.tpg_group.cg_item));
332
58fc73d1 333 destroy_workqueue(tpg->workqueue);
3699d92a
KP
334
335 /* Wait for sessions to be freed thru RCU, for BUG_ON below */
336 synchronize_rcu();
337
338 mutex_lock(&ft_lport_lock);
705665da 339 ft_wwn->tpg = NULL;
3699d92a
KP
340 if (tpg->tport) {
341 tpg->tport->tpg = NULL;
342 tpg->tport = NULL;
343 }
344 mutex_unlock(&ft_lport_lock);
345
346 core_tpg_deregister(se_tpg);
347 kfree(tpg);
348}
349
350/*
351 * Verify that an lport is configured to use the tcm_fc module, and return
352 * the target port group that should be used.
353 *
354 * The caller holds ft_lport_lock.
355 */
356struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
357{
705665da 358 struct ft_lport_wwn *ft_wwn;
3699d92a 359
705665da
AG
360 list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
361 if (ft_wwn->wwpn == lport->wwpn)
362 return ft_wwn->tpg;
3699d92a
KP
363 }
364 return NULL;
365}
366
367/*
368 * target config instance ops.
369 */
370
371/*
372 * Add lport to allowed config.
373 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
374 */
0d7cb932 375static struct se_wwn *ft_add_wwn(
3699d92a
KP
376 struct target_fabric_configfs *tf,
377 struct config_group *group,
378 const char *name)
379{
705665da
AG
380 struct ft_lport_wwn *ft_wwn;
381 struct ft_lport_wwn *old_ft_wwn;
3699d92a
KP
382 u64 wwpn;
383
0d7cb932 384 pr_debug("add wwn %s\n", name);
3699d92a
KP
385 if (ft_parse_wwn(name, &wwpn, 1) < 0)
386 return NULL;
705665da
AG
387 ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
388 if (!ft_wwn)
3699d92a 389 return NULL;
705665da 390 ft_wwn->wwpn = wwpn;
3699d92a
KP
391
392 mutex_lock(&ft_lport_lock);
705665da
AG
393 list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
394 if (old_ft_wwn->wwpn == wwpn) {
3699d92a 395 mutex_unlock(&ft_lport_lock);
705665da 396 kfree(ft_wwn);
3699d92a
KP
397 return NULL;
398 }
399 }
705665da
AG
400 list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
401 ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
3699d92a
KP
402 mutex_unlock(&ft_lport_lock);
403
705665da 404 return &ft_wwn->se_wwn;
3699d92a
KP
405}
406
0d7cb932 407static void ft_del_wwn(struct se_wwn *wwn)
3699d92a 408{
705665da
AG
409 struct ft_lport_wwn *ft_wwn = container_of(wwn,
410 struct ft_lport_wwn, se_wwn);
3699d92a 411
0d7cb932 412 pr_debug("del wwn %s\n", ft_wwn->name);
3699d92a 413 mutex_lock(&ft_lport_lock);
705665da 414 list_del(&ft_wwn->ft_wwn_node);
3699d92a
KP
415 mutex_unlock(&ft_lport_lock);
416
705665da 417 kfree(ft_wwn);
3699d92a
KP
418}
419
420static ssize_t ft_wwn_show_attr_version(
421 struct target_fabric_configfs *tf,
422 char *page)
423{
424 return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
425 ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
426}
427
428TF_WWN_ATTR_RO(ft, version);
429
430static struct configfs_attribute *ft_wwn_attrs[] = {
431 &ft_wwn_version.attr,
432 NULL,
433};
434
435static char *ft_get_fabric_name(void)
436{
437 return "fc";
438}
439
440static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
441{
442 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
443
705665da 444 return tpg->lport_wwn->name;
3699d92a
KP
445}
446
447static u16 ft_get_tag(struct se_portal_group *se_tpg)
448{
449 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
450
451 /*
452 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
453 * to represent the SCSI Target Port.
454 */
455 return tpg->index;
456}
457
3699d92a
KP
458static int ft_check_false(struct se_portal_group *se_tpg)
459{
460 return 0;
461}
462
463static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
464{
465}
466
3699d92a
KP
467static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
468{
469 struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
470
471 return tpg->index;
472}
473
9ac8928e
CH
474static const struct target_core_fabric_ops ft_fabric_ops = {
475 .module = THIS_MODULE,
476 .name = "fc",
3699d92a
KP
477 .get_fabric_name = ft_get_fabric_name,
478 .get_fabric_proto_ident = fc_get_fabric_proto_ident,
479 .tpg_get_wwn = ft_get_fabric_wwn,
480 .tpg_get_tag = ft_get_tag,
3699d92a
KP
481 .tpg_get_pr_transport_id = fc_get_pr_transport_id,
482 .tpg_get_pr_transport_id_len = fc_get_pr_transport_id_len,
483 .tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id,
484 .tpg_check_demo_mode = ft_check_false,
485 .tpg_check_demo_mode_cache = ft_check_false,
486 .tpg_check_demo_mode_write_protect = ft_check_false,
487 .tpg_check_prod_mode_write_protect = ft_check_false,
488 .tpg_alloc_fabric_acl = ft_tpg_alloc_fabric_acl,
489 .tpg_release_fabric_acl = ft_tpg_release_fabric_acl,
490 .tpg_get_inst_index = ft_tpg_get_inst_index,
491 .check_stop_free = ft_check_stop_free,
35462975 492 .release_cmd = ft_release_cmd,
3699d92a
KP
493 .shutdown_session = ft_sess_shutdown,
494 .close_session = ft_sess_close,
3699d92a
KP
495 .sess_get_index = ft_sess_get_index,
496 .sess_get_initiator_sid = NULL,
497 .write_pending = ft_write_pending,
498 .write_pending_status = ft_write_pending_status,
499 .set_default_node_attributes = ft_set_default_node_attr,
500 .get_task_tag = ft_get_task_tag,
501 .get_cmd_state = ft_get_cmd_state,
3699d92a
KP
502 .queue_data_in = ft_queue_data_in,
503 .queue_status = ft_queue_status,
504 .queue_tm_rsp = ft_queue_tm_resp,
131e6abc 505 .aborted_task = ft_aborted_task,
3699d92a
KP
506 /*
507 * Setup function pointers for generic logic in
508 * target_core_fabric_configfs.c
509 */
0d7cb932
AG
510 .fabric_make_wwn = &ft_add_wwn,
511 .fabric_drop_wwn = &ft_del_wwn,
3699d92a
KP
512 .fabric_make_tpg = &ft_add_tpg,
513 .fabric_drop_tpg = &ft_del_tpg,
c7d6a803 514 .fabric_init_nodeacl = &ft_init_nodeacl,
3699d92a 515
9ac8928e
CH
516 .tfc_wwn_attrs = ft_wwn_attrs,
517 .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
518};
3699d92a
KP
519
520static struct notifier_block ft_notifier = {
521 .notifier_call = ft_lport_notify
522};
523
524static int __init ft_init(void)
525{
9ac8928e
CH
526 int ret;
527
528 ret = target_register_template(&ft_fabric_ops);
529 if (ret)
530 goto out;
531
532 ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
533 if (ret)
534 goto out_unregister_template;
535
3699d92a
KP
536 blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
537 fc_lport_iterate(ft_lport_add, NULL);
538 return 0;
9ac8928e
CH
539
540out_unregister_template:
541 target_unregister_template(&ft_fabric_ops);
542out:
543 return ret;
3699d92a
KP
544}
545
546static void __exit ft_exit(void)
547{
548 blocking_notifier_chain_unregister(&fc_lport_notifier_head,
549 &ft_notifier);
550 fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
551 fc_lport_iterate(ft_lport_del, NULL);
9ac8928e 552 target_unregister_template(&ft_fabric_ops);
3699d92a
KP
553 synchronize_rcu();
554}
555
3699d92a
KP
556MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
557MODULE_LICENSE("GPL");
558module_init(ft_init);
559module_exit(ft_exit);