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