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