1 /*******************************************************************************
2  * Filename:  target_core_hba.c
3  *
4  * This file copntains the iSCSI HBA Transport related functions.
5  *
6  * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
7  * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8  * Copyright (c) 2007-2010 Rising Tide Systems
9  * Copyright (c) 2008-2010 Linux-iSCSI.org
10  *
11  * Nicholas A. Bellinger <nab@kernel.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  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  *
27  ******************************************************************************/
28 
29 #include <linux/net.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/in.h>
35 #include <net/sock.h>
36 #include <net/tcp.h>
37 
38 #include <target/target_core_base.h>
39 #include <target/target_core_device.h>
40 #include <target/target_core_device.h>
41 #include <target/target_core_tpg.h>
42 #include <target/target_core_transport.h>
43 
44 #include "target_core_hba.h"
45 
46 static LIST_HEAD(subsystem_list);
47 static DEFINE_MUTEX(subsystem_mutex);
48 
49 int transport_subsystem_register(struct se_subsystem_api *sub_api)
50 {
51 	struct se_subsystem_api *s;
52 
53 	INIT_LIST_HEAD(&sub_api->sub_api_list);
54 
55 	mutex_lock(&subsystem_mutex);
56 	list_for_each_entry(s, &subsystem_list, sub_api_list) {
57 		if (!(strcmp(s->name, sub_api->name))) {
58 			printk(KERN_ERR "%p is already registered with"
59 				" duplicate name %s, unable to process"
60 				" request\n", s, s->name);
61 			mutex_unlock(&subsystem_mutex);
62 			return -EEXIST;
63 		}
64 	}
65 	list_add_tail(&sub_api->sub_api_list, &subsystem_list);
66 	mutex_unlock(&subsystem_mutex);
67 
68 	printk(KERN_INFO "TCM: Registered subsystem plugin: %s struct module:"
69 			" %p\n", sub_api->name, sub_api->owner);
70 	return 0;
71 }
72 EXPORT_SYMBOL(transport_subsystem_register);
73 
74 void transport_subsystem_release(struct se_subsystem_api *sub_api)
75 {
76 	mutex_lock(&subsystem_mutex);
77 	list_del(&sub_api->sub_api_list);
78 	mutex_unlock(&subsystem_mutex);
79 }
80 EXPORT_SYMBOL(transport_subsystem_release);
81 
82 static struct se_subsystem_api *core_get_backend(const char *sub_name)
83 {
84 	struct se_subsystem_api *s;
85 
86 	mutex_lock(&subsystem_mutex);
87 	list_for_each_entry(s, &subsystem_list, sub_api_list) {
88 		if (!strcmp(s->name, sub_name))
89 			goto found;
90 	}
91 	mutex_unlock(&subsystem_mutex);
92 	return NULL;
93 found:
94 	if (s->owner && !try_module_get(s->owner))
95 		s = NULL;
96 	mutex_unlock(&subsystem_mutex);
97 	return s;
98 }
99 
100 struct se_hba *
101 core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
102 {
103 	struct se_hba *hba;
104 	int ret = 0;
105 
106 	hba = kzalloc(sizeof(*hba), GFP_KERNEL);
107 	if (!hba) {
108 		printk(KERN_ERR "Unable to allocate struct se_hba\n");
109 		return ERR_PTR(-ENOMEM);
110 	}
111 
112 	INIT_LIST_HEAD(&hba->hba_dev_list);
113 	spin_lock_init(&hba->device_lock);
114 	spin_lock_init(&hba->hba_queue_lock);
115 	mutex_init(&hba->hba_access_mutex);
116 
117 	hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
118 	hba->hba_flags |= hba_flags;
119 
120 	atomic_set(&hba->max_queue_depth, 0);
121 	atomic_set(&hba->left_queue_depth, 0);
122 
123 	hba->transport = core_get_backend(plugin_name);
124 	if (!hba->transport) {
125 		ret = -EINVAL;
126 		goto out_free_hba;
127 	}
128 
129 	ret = hba->transport->attach_hba(hba, plugin_dep_id);
130 	if (ret < 0)
131 		goto out_module_put;
132 
133 	spin_lock(&se_global->hba_lock);
134 	hba->hba_id = se_global->g_hba_id_counter++;
135 	list_add_tail(&hba->hba_list, &se_global->g_hba_list);
136 	spin_unlock(&se_global->hba_lock);
137 
138 	printk(KERN_INFO "CORE_HBA[%d] - Attached HBA to Generic Target"
139 			" Core\n", hba->hba_id);
140 
141 	return hba;
142 
143 out_module_put:
144 	if (hba->transport->owner)
145 		module_put(hba->transport->owner);
146 	hba->transport = NULL;
147 out_free_hba:
148 	kfree(hba);
149 	return ERR_PTR(ret);
150 }
151 
152 int
153 core_delete_hba(struct se_hba *hba)
154 {
155 	struct se_device *dev, *dev_tmp;
156 
157 	spin_lock(&hba->device_lock);
158 	list_for_each_entry_safe(dev, dev_tmp, &hba->hba_dev_list, dev_list) {
159 
160 		se_clear_dev_ports(dev);
161 		spin_unlock(&hba->device_lock);
162 
163 		se_release_device_for_hba(dev);
164 
165 		spin_lock(&hba->device_lock);
166 	}
167 	spin_unlock(&hba->device_lock);
168 
169 	hba->transport->detach_hba(hba);
170 
171 	spin_lock(&se_global->hba_lock);
172 	list_del(&hba->hba_list);
173 	spin_unlock(&se_global->hba_lock);
174 
175 	printk(KERN_INFO "CORE_HBA[%d] - Detached HBA from Generic Target"
176 			" Core\n", hba->hba_id);
177 
178 	if (hba->transport->owner)
179 		module_put(hba->transport->owner);
180 
181 	hba->transport = NULL;
182 	kfree(hba);
183 	return 0;
184 }
185