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