Linux Audio

Check our new training course

Loading...
v3.1
 
  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 <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>
 42
 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/configfs_macros.h>
 52
 53#include "tcm_fc.h"
 54
 55struct target_fabric_configfs *ft_configfs;
 56
 57LIST_HEAD(ft_lport_list);
 58DEFINE_MUTEX(ft_lport_lock);
 59
 60unsigned int ft_debug_logging;
 61module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
 62MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
 63
 64/*
 65 * Parse WWN.
 66 * If strict, we require lower-case hex and colon separators to be sure
 67 * the name is the same as what would be generated by ft_format_wwn()
 68 * so the name and wwn are mapped one-to-one.
 69 */
 70static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
 71{
 72	const char *cp;
 73	char c;
 74	u32 nibble;
 75	u32 byte = 0;
 76	u32 pos = 0;
 77	u32 err;
 
 78
 79	*wwn = 0;
 80	for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
 81		c = *cp;
 82		if (c == '\n' && cp[1] == '\0')
 83			continue;
 84		if (strict && pos++ == 2 && byte++ < 7) {
 85			pos = 0;
 86			if (c == ':')
 87				continue;
 88			err = 1;
 89			goto fail;
 90		}
 91		if (c == '\0') {
 92			err = 2;
 93			if (strict && byte != 8)
 94				goto fail;
 95			return cp - name;
 96		}
 97		err = 3;
 98		if (isdigit(c))
 99			nibble = c - '0';
100		else if (isxdigit(c) && (islower(c) || !strict))
101			nibble = tolower(c) - 'a' + 10;
102		else
103			goto fail;
104		*wwn = (*wwn << 4) | nibble;
105	}
106	err = 4;
107fail:
108	pr_debug("err %u len %zu pos %u byte %u\n",
109		    err, cp - name, pos, byte);
110	return -1;
111}
112
113ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
114{
115	u8 b[8];
116
117	put_unaligned_be64(wwn, b);
118	return snprintf(buf, len,
119		 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
120		 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
121}
122
123static ssize_t ft_wwn_show(void *arg, char *buf)
124{
125	u64 *wwn = arg;
126	ssize_t len;
127
128	len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
129	buf[len++] = '\n';
130	return len;
131}
132
133static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
134{
135	ssize_t ret;
136	u64 wwn;
137
138	ret = ft_parse_wwn(buf, &wwn, 0);
139	if (ret > 0)
140		*(u64 *)arg = wwn;
141	return ret;
142}
143
144/*
145 * ACL auth ops.
146 */
147
148static ssize_t ft_nacl_show_port_name(
149	struct se_node_acl *se_nacl,
150	char *page)
151{
 
152	struct ft_node_acl *acl = container_of(se_nacl,
153			struct ft_node_acl, se_node_acl);
154
155	return ft_wwn_show(&acl->node_auth.port_name, page);
156}
157
158static ssize_t ft_nacl_store_port_name(
159	struct se_node_acl *se_nacl,
160	const char *page,
161	size_t count)
162{
 
163	struct ft_node_acl *acl = container_of(se_nacl,
164			struct ft_node_acl, se_node_acl);
165
166	return ft_wwn_store(&acl->node_auth.port_name, page, count);
167}
168
169TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR);
170
171static ssize_t ft_nacl_show_node_name(
172	struct se_node_acl *se_nacl,
173	char *page)
174{
 
175	struct ft_node_acl *acl = container_of(se_nacl,
176			struct ft_node_acl, se_node_acl);
177
178	return ft_wwn_show(&acl->node_auth.node_name, page);
179}
180
181static ssize_t ft_nacl_store_node_name(
182	struct se_node_acl *se_nacl,
183	const char *page,
184	size_t count)
185{
 
186	struct ft_node_acl *acl = container_of(se_nacl,
187			struct ft_node_acl, se_node_acl);
188
189	return ft_wwn_store(&acl->node_auth.node_name, page, count);
190}
191
192TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
194static struct configfs_attribute *ft_nacl_base_attrs[] = {
195	&ft_nacl_port_name.attr,
196	&ft_nacl_node_name.attr,
 
197	NULL,
198};
199
200/*
201 * ACL ops.
202 */
203
204/*
205 * Add ACL for an initiator.  The ACL is named arbitrarily.
206 * The port_name and/or node_name are attributes.
207 */
208static struct se_node_acl *ft_add_acl(
209	struct se_portal_group *se_tpg,
210	struct config_group *group,
211	const char *name)
212{
213	struct ft_node_acl *acl;
214	struct ft_tpg *tpg;
215	u64 wwpn;
216	u32 q_depth;
217
218	pr_debug("add acl %s\n", name);
219	tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
220
221	if (ft_parse_wwn(name, &wwpn, 1) < 0)
222		return ERR_PTR(-EINVAL);
223
224	acl = kzalloc(sizeof(struct ft_node_acl), GFP_KERNEL);
225	if (!acl)
226		return ERR_PTR(-ENOMEM);
227	acl->node_auth.port_name = wwpn;
228
229	q_depth = 32;		/* XXX bogus default - get from tpg? */
230	return core_tpg_add_initiator_node_acl(&tpg->se_tpg,
231				&acl->se_node_acl, name, q_depth);
232}
233
234static void ft_del_acl(struct se_node_acl *se_acl)
235{
236	struct se_portal_group *se_tpg = se_acl->se_tpg;
237	struct ft_tpg *tpg;
238	struct ft_node_acl *acl = container_of(se_acl,
239				struct ft_node_acl, se_node_acl);
240
241	pr_debug("del acl %s\n",
242		config_item_name(&se_acl->acl_group.cg_item));
243
244	tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
245	pr_debug("del acl %p se_acl %p tpg %p se_tpg %p\n",
246		    acl, se_acl, tpg, &tpg->se_tpg);
247
248	core_tpg_del_initiator_node_acl(&tpg->se_tpg, se_acl, 1);
249	kfree(acl);
250}
251
252struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
253{
254	struct ft_node_acl *found = NULL;
255	struct ft_node_acl *acl;
256	struct se_portal_group *se_tpg = &tpg->se_tpg;
257	struct se_node_acl *se_acl;
258
259	spin_lock_irq(&se_tpg->acl_node_lock);
260	list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) {
261		acl = container_of(se_acl, struct ft_node_acl, se_node_acl);
262		pr_debug("acl %p port_name %llx\n",
263			acl, (unsigned long long)acl->node_auth.port_name);
264		if (acl->node_auth.port_name == rdata->ids.port_name ||
265		    acl->node_auth.node_name == rdata->ids.node_name) {
266			pr_debug("acl %p port_name %llx matched\n", acl,
267				    (unsigned long long)rdata->ids.port_name);
268			found = acl;
269			/* XXX need to hold onto ACL */
270			break;
271		}
272	}
273	spin_unlock_irq(&se_tpg->acl_node_lock);
274	return found;
275}
276
277struct se_node_acl *ft_tpg_alloc_fabric_acl(struct se_portal_group *se_tpg)
278{
279	struct ft_node_acl *acl;
280
281	acl = kzalloc(sizeof(*acl), GFP_KERNEL);
282	if (!acl) {
283		pr_err("Unable to allocate struct ft_node_acl\n");
284		return NULL;
285	}
286	pr_debug("acl %p\n", acl);
287	return &acl->se_node_acl;
288}
289
290static void ft_tpg_release_fabric_acl(struct se_portal_group *se_tpg,
291				      struct se_node_acl *se_acl)
292{
293	struct ft_node_acl *acl = container_of(se_acl,
294				struct ft_node_acl, se_node_acl);
295
296	pr_debug("acl %p\n", acl);
297	kfree(acl);
298}
299
300/*
301 * local_port port_group (tpg) ops.
302 */
303static struct se_portal_group *ft_add_tpg(
304	struct se_wwn *wwn,
305	struct config_group *group,
306	const char *name)
307{
308	struct ft_lport_acl *lacl;
309	struct ft_tpg *tpg;
 
310	unsigned long index;
311	int ret;
312
313	pr_debug("tcm_fc: add tpg %s\n", name);
314
315	/*
316	 * Name must be "tpgt_" followed by the index.
317	 */
318	if (strstr(name, "tpgt_") != name)
319		return NULL;
320	if (strict_strtoul(name + 5, 10, &index) || index > UINT_MAX)
 
 
 
 
321		return NULL;
322
323	lacl = container_of(wwn, struct ft_lport_acl, fc_lport_wwn);
 
 
 
 
 
324	tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
325	if (!tpg)
326		return NULL;
327	tpg->index = index;
328	tpg->lport_acl = lacl;
329	INIT_LIST_HEAD(&tpg->lun_list);
330
331	ret = core_tpg_register(&ft_configfs->tf_ops, wwn, &tpg->se_tpg,
332				tpg, TRANSPORT_TPG_TYPE_NORMAL);
333	if (ret < 0) {
334		kfree(tpg);
335		return NULL;
336	}
337
338	tpg->workqueue = alloc_workqueue("tcm_fc", 0, 1);
339	if (!tpg->workqueue) {
 
340		kfree(tpg);
341		return NULL;
342	}
 
343
344	mutex_lock(&ft_lport_lock);
345	list_add_tail(&tpg->list, &lacl->tpg_list);
346	mutex_unlock(&ft_lport_lock);
347
348	return &tpg->se_tpg;
349}
350
351static void ft_del_tpg(struct se_portal_group *se_tpg)
352{
353	struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
 
354
355	pr_debug("del tpg %s\n",
356		    config_item_name(&tpg->se_tpg.tpg_group.cg_item));
357
358	destroy_workqueue(tpg->workqueue);
359
360	/* Wait for sessions to be freed thru RCU, for BUG_ON below */
361	synchronize_rcu();
362
363	mutex_lock(&ft_lport_lock);
364	list_del(&tpg->list);
365	if (tpg->tport) {
366		tpg->tport->tpg = NULL;
367		tpg->tport = NULL;
368	}
369	mutex_unlock(&ft_lport_lock);
370
371	core_tpg_deregister(se_tpg);
372	kfree(tpg);
373}
374
375/*
376 * Verify that an lport is configured to use the tcm_fc module, and return
377 * the target port group that should be used.
378 *
379 * The caller holds ft_lport_lock.
380 */
381struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
382{
383	struct ft_lport_acl *lacl;
384	struct ft_tpg *tpg;
385
386	list_for_each_entry(lacl, &ft_lport_list, list) {
387		if (lacl->wwpn == lport->wwpn) {
388			list_for_each_entry(tpg, &lacl->tpg_list, list)
389				return tpg; /* XXX for now return first entry */
390			return NULL;
391		}
392	}
393	return NULL;
394}
395
396/*
397 * target config instance ops.
398 */
399
400/*
401 * Add lport to allowed config.
402 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
403 */
404static struct se_wwn *ft_add_lport(
405	struct target_fabric_configfs *tf,
406	struct config_group *group,
407	const char *name)
408{
409	struct ft_lport_acl *lacl;
410	struct ft_lport_acl *old_lacl;
411	u64 wwpn;
412
413	pr_debug("add lport %s\n", name);
414	if (ft_parse_wwn(name, &wwpn, 1) < 0)
415		return NULL;
416	lacl = kzalloc(sizeof(*lacl), GFP_KERNEL);
417	if (!lacl)
418		return NULL;
419	lacl->wwpn = wwpn;
420	INIT_LIST_HEAD(&lacl->tpg_list);
421
422	mutex_lock(&ft_lport_lock);
423	list_for_each_entry(old_lacl, &ft_lport_list, list) {
424		if (old_lacl->wwpn == wwpn) {
425			mutex_unlock(&ft_lport_lock);
426			kfree(lacl);
427			return NULL;
428		}
429	}
430	list_add_tail(&lacl->list, &ft_lport_list);
431	ft_format_wwn(lacl->name, sizeof(lacl->name), wwpn);
432	mutex_unlock(&ft_lport_lock);
433
434	return &lacl->fc_lport_wwn;
435}
436
437static void ft_del_lport(struct se_wwn *wwn)
438{
439	struct ft_lport_acl *lacl = container_of(wwn,
440				struct ft_lport_acl, fc_lport_wwn);
441
442	pr_debug("del lport %s\n",
443			config_item_name(&wwn->wwn_group.cg_item));
444	mutex_lock(&ft_lport_lock);
445	list_del(&lacl->list);
446	mutex_unlock(&ft_lport_lock);
447
448	kfree(lacl);
449}
450
451static ssize_t ft_wwn_show_attr_version(
452	struct target_fabric_configfs *tf,
453	char *page)
454{
455	return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
456		""UTS_RELEASE"\n",  utsname()->sysname, utsname()->machine);
457}
458
459TF_WWN_ATTR_RO(ft, version);
460
461static struct configfs_attribute *ft_wwn_attrs[] = {
462	&ft_wwn_version.attr,
463	NULL,
464};
465
466static char *ft_get_fabric_name(void)
467{
468	return "fc";
469}
470
471static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
472{
473	struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
474
475	return tpg->lport_acl->name;
476}
477
478static u16 ft_get_tag(struct se_portal_group *se_tpg)
479{
480	struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
481
482	/*
483	 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
484	 * to represent the SCSI Target Port.
485	 */
486	return tpg->index;
487}
488
489static u32 ft_get_default_depth(struct se_portal_group *se_tpg)
490{
491	return 1;
492}
493
494static int ft_check_false(struct se_portal_group *se_tpg)
495{
496	return 0;
497}
498
499static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
500{
501}
502
503static u16 ft_get_fabric_sense_len(void)
504{
505	return 0;
506}
507
508static u16 ft_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_len)
509{
510	return 0;
511}
512
513static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
514{
515	struct ft_tpg *tpg = se_tpg->se_tpg_fabric_ptr;
516
517	return tpg->index;
518}
519
520static struct target_core_fabric_ops ft_fabric_ops = {
521	.get_fabric_name =		ft_get_fabric_name,
522	.get_fabric_proto_ident =	fc_get_fabric_proto_ident,
 
523	.tpg_get_wwn =			ft_get_fabric_wwn,
524	.tpg_get_tag =			ft_get_tag,
525	.tpg_get_default_depth =	ft_get_default_depth,
526	.tpg_get_pr_transport_id =	fc_get_pr_transport_id,
527	.tpg_get_pr_transport_id_len =	fc_get_pr_transport_id_len,
528	.tpg_parse_pr_out_transport_id = fc_parse_pr_out_transport_id,
529	.tpg_check_demo_mode =		ft_check_false,
530	.tpg_check_demo_mode_cache =	ft_check_false,
531	.tpg_check_demo_mode_write_protect = ft_check_false,
532	.tpg_check_prod_mode_write_protect = ft_check_false,
533	.tpg_alloc_fabric_acl =		ft_tpg_alloc_fabric_acl,
534	.tpg_release_fabric_acl =	ft_tpg_release_fabric_acl,
535	.tpg_get_inst_index =		ft_tpg_get_inst_index,
536	.check_stop_free =		ft_check_stop_free,
537	.release_cmd =			ft_release_cmd,
538	.shutdown_session =		ft_sess_shutdown,
539	.close_session =		ft_sess_close,
540	.stop_session =			ft_sess_stop,
541	.fall_back_to_erl0 =		ft_sess_set_erl0,
542	.sess_logged_in =		ft_sess_logged_in,
543	.sess_get_index =		ft_sess_get_index,
544	.sess_get_initiator_sid =	NULL,
545	.write_pending =		ft_write_pending,
546	.write_pending_status =		ft_write_pending_status,
547	.set_default_node_attributes =	ft_set_default_node_attr,
548	.get_task_tag =			ft_get_task_tag,
549	.get_cmd_state =		ft_get_cmd_state,
550	.queue_data_in =		ft_queue_data_in,
551	.queue_status =			ft_queue_status,
552	.queue_tm_rsp =			ft_queue_tm_resp,
553	.get_fabric_sense_len =		ft_get_fabric_sense_len,
554	.set_fabric_sense_len =		ft_set_fabric_sense_len,
555	.is_state_remove =		ft_is_state_remove,
556	/*
557	 * Setup function pointers for generic logic in
558	 * target_core_fabric_configfs.c
559	 */
560	.fabric_make_wwn =		&ft_add_lport,
561	.fabric_drop_wwn =		&ft_del_lport,
562	.fabric_make_tpg =		&ft_add_tpg,
563	.fabric_drop_tpg =		&ft_del_tpg,
564	.fabric_post_link =		NULL,
565	.fabric_pre_unlink =		NULL,
566	.fabric_make_np =		NULL,
567	.fabric_drop_np =		NULL,
568	.fabric_make_nodeacl =		&ft_add_acl,
569	.fabric_drop_nodeacl =		&ft_del_acl,
570};
571
572int ft_register_configfs(void)
573{
574	struct target_fabric_configfs *fabric;
575	int ret;
576
577	/*
578	 * Register the top level struct config_item_type with TCM core
579	 */
580	fabric = target_fabric_configfs_init(THIS_MODULE, "fc");
581	if (IS_ERR(fabric)) {
582		pr_err("%s: target_fabric_configfs_init() failed!\n",
583		       __func__);
584		return PTR_ERR(fabric);
585	}
586	fabric->tf_ops = ft_fabric_ops;
587
588	/* Allowing support for task_sg_chaining */
589	fabric->tf_ops.task_sg_chaining = 1;
590
591	/*
592	 * Setup default attribute lists for various fabric->tf_cit_tmpl
593	 */
594	TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = ft_wwn_attrs;
595	TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
596	TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
597	TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
598	TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
599	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs =
600						    ft_nacl_base_attrs;
601	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
602	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
603	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
604	/*
605	 * register the fabric for use within TCM
606	 */
607	ret = target_fabric_configfs_register(fabric);
608	if (ret < 0) {
609		pr_debug("target_fabric_configfs_register() for"
610			    " FC Target failed!\n");
611		target_fabric_configfs_free(fabric);
612		return -1;
613	}
614
615	/*
616	 * Setup our local pointer to *fabric.
617	 */
618	ft_configfs = fabric;
619	return 0;
620}
621
622void ft_deregister_configfs(void)
623{
624	if (!ft_configfs)
625		return;
626	target_fabric_configfs_deregister(ft_configfs);
627	ft_configfs = NULL;
628}
629
630static struct notifier_block ft_notifier = {
631	.notifier_call = ft_lport_notify
632};
633
634static int __init ft_init(void)
635{
636	if (ft_register_configfs())
637		return -1;
638	if (fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov)) {
639		ft_deregister_configfs();
640		return -1;
641	}
 
 
 
 
642	blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
643	fc_lport_iterate(ft_lport_add, NULL);
644	return 0;
 
 
 
 
 
645}
646
647static void __exit ft_exit(void)
648{
649	blocking_notifier_chain_unregister(&fc_lport_notifier_head,
650					   &ft_notifier);
651	fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
652	fc_lport_iterate(ft_lport_del, NULL);
653	ft_deregister_configfs();
654	synchronize_rcu();
655}
656
657MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
658MODULE_LICENSE("GPL");
659module_init(ft_init);
660module_exit(ft_exit);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*******************************************************************************
  3 * Filename:  tcm_fc.c
  4 *
  5 * This file contains the configfs implementation for TCM_fc fabric node.
  6 * Based on tcm_loop_configfs.c
  7 *
  8 * Copyright (c) 2010 Cisco Systems, Inc.
  9 * Copyright (c) 2009,2010 Rising Tide, Inc.
 10 * Copyright (c) 2009,2010 Linux-iSCSI.org
 11 *
 12 * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
 13 *
 
 
 
 
 
 
 
 
 
 14 ****************************************************************************/
 15
 16#include <linux/module.h>
 17#include <linux/moduleparam.h>
 
 18#include <generated/utsrelease.h>
 19#include <linux/utsname.h>
 20#include <linux/init.h>
 21#include <linux/slab.h>
 22#include <linux/kthread.h>
 23#include <linux/types.h>
 24#include <linux/string.h>
 25#include <linux/configfs.h>
 26#include <linux/kernel.h>
 27#include <linux/ctype.h>
 28#include <asm/unaligned.h>
 
 
 
 
 29#include <scsi/libfc.h>
 30
 31#include <target/target_core_base.h>
 32#include <target/target_core_fabric.h>
 
 
 
 
 
 
 
 33
 34#include "tcm_fc.h"
 35
 36static LIST_HEAD(ft_wwn_list);
 
 
 37DEFINE_MUTEX(ft_lport_lock);
 38
 39unsigned int ft_debug_logging;
 40module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
 41MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
 42
 43/*
 44 * Parse WWN.
 45 * If strict, we require lower-case hex and colon separators to be sure
 46 * the name is the same as what would be generated by ft_format_wwn()
 47 * so the name and wwn are mapped one-to-one.
 48 */
 49static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
 50{
 51	const char *cp;
 52	char c;
 
 53	u32 byte = 0;
 54	u32 pos = 0;
 55	u32 err;
 56	int val;
 57
 58	*wwn = 0;
 59	for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
 60		c = *cp;
 61		if (c == '\n' && cp[1] == '\0')
 62			continue;
 63		if (strict && pos++ == 2 && byte++ < 7) {
 64			pos = 0;
 65			if (c == ':')
 66				continue;
 67			err = 1;
 68			goto fail;
 69		}
 70		if (c == '\0') {
 71			err = 2;
 72			if (strict && byte != 8)
 73				goto fail;
 74			return cp - name;
 75		}
 76		err = 3;
 77		val = hex_to_bin(c);
 78		if (val < 0 || (strict && isupper(c)))
 
 
 
 79			goto fail;
 80		*wwn = (*wwn << 4) | val;
 81	}
 82	err = 4;
 83fail:
 84	pr_debug("err %u len %zu pos %u byte %u\n",
 85		    err, cp - name, pos, byte);
 86	return -1;
 87}
 88
 89ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
 90{
 91	u8 b[8];
 92
 93	put_unaligned_be64(wwn, b);
 94	return snprintf(buf, len,
 95		 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
 96		 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
 97}
 98
 99static ssize_t ft_wwn_show(void *arg, char *buf)
100{
101	u64 *wwn = arg;
102	ssize_t len;
103
104	len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
105	buf[len++] = '\n';
106	return len;
107}
108
109static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
110{
111	ssize_t ret;
112	u64 wwn;
113
114	ret = ft_parse_wwn(buf, &wwn, 0);
115	if (ret > 0)
116		*(u64 *)arg = wwn;
117	return ret;
118}
119
120/*
121 * ACL auth ops.
122 */
123
124static ssize_t ft_nacl_port_name_show(struct config_item *item, char *page)
 
 
125{
126	struct se_node_acl *se_nacl = acl_to_nacl(item);
127	struct ft_node_acl *acl = container_of(se_nacl,
128			struct ft_node_acl, se_node_acl);
129
130	return ft_wwn_show(&acl->node_auth.port_name, page);
131}
132
133static ssize_t ft_nacl_port_name_store(struct config_item *item,
134		const char *page, size_t count)
 
 
135{
136	struct se_node_acl *se_nacl = acl_to_nacl(item);
137	struct ft_node_acl *acl = container_of(se_nacl,
138			struct ft_node_acl, se_node_acl);
139
140	return ft_wwn_store(&acl->node_auth.port_name, page, count);
141}
142
143static ssize_t ft_nacl_node_name_show(struct config_item *item,
144		char *page)
 
 
 
145{
146	struct se_node_acl *se_nacl = acl_to_nacl(item);
147	struct ft_node_acl *acl = container_of(se_nacl,
148			struct ft_node_acl, se_node_acl);
149
150	return ft_wwn_show(&acl->node_auth.node_name, page);
151}
152
153static ssize_t ft_nacl_node_name_store(struct config_item *item,
154		const char *page, size_t count)
 
 
155{
156	struct se_node_acl *se_nacl = acl_to_nacl(item);
157	struct ft_node_acl *acl = container_of(se_nacl,
158			struct ft_node_acl, se_node_acl);
159
160	return ft_wwn_store(&acl->node_auth.node_name, page, count);
161}
162
163CONFIGFS_ATTR(ft_nacl_, node_name);
164CONFIGFS_ATTR(ft_nacl_, port_name);
165
166static ssize_t ft_nacl_tag_show(struct config_item *item,
167		char *page)
168{
169	return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
170}
171
172static ssize_t ft_nacl_tag_store(struct config_item *item,
173		const char *page, size_t count)
174{
175	struct se_node_acl *se_nacl = acl_to_nacl(item);
176	int ret;
177
178	ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
179
180	if (ret < 0)
181		return ret;
182	return count;
183}
184
185CONFIGFS_ATTR(ft_nacl_, tag);
186
187static struct configfs_attribute *ft_nacl_base_attrs[] = {
188	&ft_nacl_attr_port_name,
189	&ft_nacl_attr_node_name,
190	&ft_nacl_attr_tag,
191	NULL,
192};
193
194/*
195 * ACL ops.
196 */
197
198/*
199 * Add ACL for an initiator.  The ACL is named arbitrarily.
200 * The port_name and/or node_name are attributes.
201 */
202static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
 
 
 
203{
204	struct ft_node_acl *acl =
205		container_of(nacl, struct ft_node_acl, se_node_acl);
206	u64 wwpn;
 
 
 
 
207
208	if (ft_parse_wwn(name, &wwpn, 1) < 0)
209		return -EINVAL;
210
 
 
 
211	acl->node_auth.port_name = wwpn;
212	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213}
214
215/*
216 * local_port port_group (tpg) ops.
217 */
218static struct se_portal_group *ft_add_tpg(struct se_wwn *wwn, const char *name)
 
 
 
219{
220	struct ft_lport_wwn *ft_wwn;
221	struct ft_tpg *tpg;
222	struct workqueue_struct *wq;
223	unsigned long index;
224	int ret;
225
226	pr_debug("tcm_fc: add tpg %s\n", name);
227
228	/*
229	 * Name must be "tpgt_" followed by the index.
230	 */
231	if (strstr(name, "tpgt_") != name)
232		return NULL;
233
234	ret = kstrtoul(name + 5, 10, &index);
235	if (ret)
236		return NULL;
237	if (index > UINT_MAX)
238		return NULL;
239
240	if ((index != 1)) {
241		pr_err("Error, a single TPG=1 is used for HW port mappings\n");
242		return ERR_PTR(-ENOSYS);
243	}
244
245	ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
246	tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
247	if (!tpg)
248		return NULL;
249	tpg->index = index;
250	tpg->lport_wwn = ft_wwn;
251	INIT_LIST_HEAD(&tpg->lun_list);
252
253	wq = alloc_workqueue("tcm_fc", 0, 1);
254	if (!wq) {
 
255		kfree(tpg);
256		return NULL;
257	}
258
259	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
260	if (ret < 0) {
261		destroy_workqueue(wq);
262		kfree(tpg);
263		return NULL;
264	}
265	tpg->workqueue = wq;
266
267	mutex_lock(&ft_lport_lock);
268	ft_wwn->tpg = tpg;
269	mutex_unlock(&ft_lport_lock);
270
271	return &tpg->se_tpg;
272}
273
274static void ft_del_tpg(struct se_portal_group *se_tpg)
275{
276	struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
277	struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
278
279	pr_debug("del tpg %s\n",
280		    config_item_name(&tpg->se_tpg.tpg_group.cg_item));
281
282	destroy_workqueue(tpg->workqueue);
283
284	/* Wait for sessions to be freed thru RCU, for BUG_ON below */
285	synchronize_rcu();
286
287	mutex_lock(&ft_lport_lock);
288	ft_wwn->tpg = NULL;
289	if (tpg->tport) {
290		tpg->tport->tpg = NULL;
291		tpg->tport = NULL;
292	}
293	mutex_unlock(&ft_lport_lock);
294
295	core_tpg_deregister(se_tpg);
296	kfree(tpg);
297}
298
299/*
300 * Verify that an lport is configured to use the tcm_fc module, and return
301 * the target port group that should be used.
302 *
303 * The caller holds ft_lport_lock.
304 */
305struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
306{
307	struct ft_lport_wwn *ft_wwn;
 
308
309	list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
310		if (ft_wwn->wwpn == lport->wwpn)
311			return ft_wwn->tpg;
 
 
 
312	}
313	return NULL;
314}
315
316/*
317 * target config instance ops.
318 */
319
320/*
321 * Add lport to allowed config.
322 * The name is the WWPN in lower-case ASCII, colon-separated bytes.
323 */
324static struct se_wwn *ft_add_wwn(
325	struct target_fabric_configfs *tf,
326	struct config_group *group,
327	const char *name)
328{
329	struct ft_lport_wwn *ft_wwn;
330	struct ft_lport_wwn *old_ft_wwn;
331	u64 wwpn;
332
333	pr_debug("add wwn %s\n", name);
334	if (ft_parse_wwn(name, &wwpn, 1) < 0)
335		return NULL;
336	ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
337	if (!ft_wwn)
338		return NULL;
339	ft_wwn->wwpn = wwpn;
 
340
341	mutex_lock(&ft_lport_lock);
342	list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
343		if (old_ft_wwn->wwpn == wwpn) {
344			mutex_unlock(&ft_lport_lock);
345			kfree(ft_wwn);
346			return NULL;
347		}
348	}
349	list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
350	ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
351	mutex_unlock(&ft_lport_lock);
352
353	return &ft_wwn->se_wwn;
354}
355
356static void ft_del_wwn(struct se_wwn *wwn)
357{
358	struct ft_lport_wwn *ft_wwn = container_of(wwn,
359				struct ft_lport_wwn, se_wwn);
360
361	pr_debug("del wwn %s\n", ft_wwn->name);
 
362	mutex_lock(&ft_lport_lock);
363	list_del(&ft_wwn->ft_wwn_node);
364	mutex_unlock(&ft_lport_lock);
365
366	kfree(ft_wwn);
367}
368
369static ssize_t ft_wwn_version_show(struct config_item *item, char *page)
 
 
370{
371	return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
372		""UTS_RELEASE"\n",  utsname()->sysname, utsname()->machine);
373}
374
375CONFIGFS_ATTR_RO(ft_wwn_, version);
376
377static struct configfs_attribute *ft_wwn_attrs[] = {
378	&ft_wwn_attr_version,
379	NULL,
380};
381
382static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg)
383{
384	return container_of(se_tpg, struct ft_tpg, se_tpg);
385}
386
387static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
388{
389	return ft_tpg(se_tpg)->lport_wwn->name;
 
 
390}
391
392static u16 ft_get_tag(struct se_portal_group *se_tpg)
393{
 
 
394	/*
395	 * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
396	 * to represent the SCSI Target Port.
397	 */
398	return ft_tpg(se_tpg)->index;
 
 
 
 
 
399}
400
401static int ft_check_false(struct se_portal_group *se_tpg)
402{
403	return 0;
404}
405
406static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
407{
408}
409
 
 
 
 
 
 
 
 
 
 
410static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
411{
412	return ft_tpg(se_tpg)->index;
 
 
413}
414
415static const struct target_core_fabric_ops ft_fabric_ops = {
416	.module =			THIS_MODULE,
417	.fabric_name =			"fc",
418	.node_acl_size =		sizeof(struct ft_node_acl),
419	.tpg_get_wwn =			ft_get_fabric_wwn,
420	.tpg_get_tag =			ft_get_tag,
 
 
 
 
421	.tpg_check_demo_mode =		ft_check_false,
422	.tpg_check_demo_mode_cache =	ft_check_false,
423	.tpg_check_demo_mode_write_protect = ft_check_false,
424	.tpg_check_prod_mode_write_protect = ft_check_false,
 
 
425	.tpg_get_inst_index =		ft_tpg_get_inst_index,
426	.check_stop_free =		ft_check_stop_free,
427	.release_cmd =			ft_release_cmd,
 
428	.close_session =		ft_sess_close,
 
 
 
429	.sess_get_index =		ft_sess_get_index,
430	.sess_get_initiator_sid =	NULL,
431	.write_pending =		ft_write_pending,
 
432	.set_default_node_attributes =	ft_set_default_node_attr,
 
433	.get_cmd_state =		ft_get_cmd_state,
434	.queue_data_in =		ft_queue_data_in,
435	.queue_status =			ft_queue_status,
436	.queue_tm_rsp =			ft_queue_tm_resp,
437	.aborted_task =			ft_aborted_task,
 
 
438	/*
439	 * Setup function pointers for generic logic in
440	 * target_core_fabric_configfs.c
441	 */
442	.fabric_make_wwn =		&ft_add_wwn,
443	.fabric_drop_wwn =		&ft_del_wwn,
444	.fabric_make_tpg =		&ft_add_tpg,
445	.fabric_drop_tpg =		&ft_del_tpg,
446	.fabric_init_nodeacl =		&ft_init_nodeacl,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447
448	.tfc_wwn_attrs			= ft_wwn_attrs,
449	.tfc_tpg_nacl_base_attrs	= ft_nacl_base_attrs,
450};
 
 
 
 
 
 
 
 
 
 
 
451
452static struct notifier_block ft_notifier = {
453	.notifier_call = ft_lport_notify
454};
455
456static int __init ft_init(void)
457{
458	int ret;
459
460	ret = target_register_template(&ft_fabric_ops);
461	if (ret)
462		goto out;
463
464	ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
465	if (ret)
466		goto out_unregister_template;
467
468	blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
469	fc_lport_iterate(ft_lport_add, NULL);
470	return 0;
471
472out_unregister_template:
473	target_unregister_template(&ft_fabric_ops);
474out:
475	return ret;
476}
477
478static void __exit ft_exit(void)
479{
480	blocking_notifier_chain_unregister(&fc_lport_notifier_head,
481					   &ft_notifier);
482	fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
483	fc_lport_iterate(ft_lport_del, NULL);
484	target_unregister_template(&ft_fabric_ops);
485	synchronize_rcu();
486}
487
488MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
489MODULE_LICENSE("GPL");
490module_init(ft_init);
491module_exit(ft_exit);