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_tpg.h>
41 #include <target/target_core_transport.h>
42
43 #include "target_core_hba.h"
44
45 static LIST_HEAD(subsystem_list);
46 static DEFINE_MUTEX(subsystem_mutex);
47
transport_subsystem_register(struct se_subsystem_api * sub_api)48 int transport_subsystem_register(struct se_subsystem_api *sub_api)
49 {
50 struct se_subsystem_api *s;
51
52 INIT_LIST_HEAD(&sub_api->sub_api_list);
53
54 mutex_lock(&subsystem_mutex);
55 list_for_each_entry(s, &subsystem_list, sub_api_list) {
56 if (!(strcmp(s->name, sub_api->name))) {
57 printk(KERN_ERR "%p is already registered with"
58 " duplicate name %s, unable to process"
59 " request\n", s, s->name);
60 mutex_unlock(&subsystem_mutex);
61 return -EEXIST;
62 }
63 }
64 list_add_tail(&sub_api->sub_api_list, &subsystem_list);
65 mutex_unlock(&subsystem_mutex);
66
67 printk(KERN_INFO "TCM: Registered subsystem plugin: %s struct module:"
68 " %p\n", sub_api->name, sub_api->owner);
69 return 0;
70 }
71 EXPORT_SYMBOL(transport_subsystem_register);
72
transport_subsystem_release(struct se_subsystem_api * sub_api)73 void transport_subsystem_release(struct se_subsystem_api *sub_api)
74 {
75 mutex_lock(&subsystem_mutex);
76 list_del(&sub_api->sub_api_list);
77 mutex_unlock(&subsystem_mutex);
78 }
79 EXPORT_SYMBOL(transport_subsystem_release);
80
core_get_backend(const char * sub_name)81 static struct se_subsystem_api *core_get_backend(const char *sub_name)
82 {
83 struct se_subsystem_api *s;
84
85 mutex_lock(&subsystem_mutex);
86 list_for_each_entry(s, &subsystem_list, sub_api_list) {
87 if (!strcmp(s->name, sub_name))
88 goto found;
89 }
90 mutex_unlock(&subsystem_mutex);
91 return NULL;
92 found:
93 if (s->owner && !try_module_get(s->owner))
94 s = NULL;
95 mutex_unlock(&subsystem_mutex);
96 return s;
97 }
98
99 struct se_hba *
core_alloc_hba(const char * plugin_name,u32 plugin_dep_id,u32 hba_flags)100 core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
101 {
102 struct se_hba *hba;
103 int ret = 0;
104
105 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
106 if (!hba) {
107 printk(KERN_ERR "Unable to allocate struct se_hba\n");
108 return ERR_PTR(-ENOMEM);
109 }
110
111 INIT_LIST_HEAD(&hba->hba_dev_list);
112 spin_lock_init(&hba->device_lock);
113 spin_lock_init(&hba->hba_queue_lock);
114 mutex_init(&hba->hba_access_mutex);
115
116 hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
117 hba->hba_flags |= hba_flags;
118
119 atomic_set(&hba->max_queue_depth, 0);
120 atomic_set(&hba->left_queue_depth, 0);
121
122 hba->transport = core_get_backend(plugin_name);
123 if (!hba->transport) {
124 ret = -EINVAL;
125 goto out_free_hba;
126 }
127
128 ret = hba->transport->attach_hba(hba, plugin_dep_id);
129 if (ret < 0)
130 goto out_module_put;
131
132 spin_lock(&se_global->hba_lock);
133 hba->hba_id = se_global->g_hba_id_counter++;
134 list_add_tail(&hba->hba_list, &se_global->g_hba_list);
135 spin_unlock(&se_global->hba_lock);
136
137 printk(KERN_INFO "CORE_HBA[%d] - Attached HBA to Generic Target"
138 " Core\n", hba->hba_id);
139
140 return hba;
141
142 out_module_put:
143 if (hba->transport->owner)
144 module_put(hba->transport->owner);
145 hba->transport = NULL;
146 out_free_hba:
147 kfree(hba);
148 return ERR_PTR(ret);
149 }
150
151 int
core_delete_hba(struct se_hba * hba)152 core_delete_hba(struct se_hba *hba)
153 {
154 if (!list_empty(&hba->hba_dev_list))
155 dump_stack();
156
157 hba->transport->detach_hba(hba);
158
159 spin_lock(&se_global->hba_lock);
160 list_del(&hba->hba_list);
161 spin_unlock(&se_global->hba_lock);
162
163 printk(KERN_INFO "CORE_HBA[%d] - Detached HBA from Generic Target"
164 " Core\n", hba->hba_id);
165
166 if (hba->transport->owner)
167 module_put(hba->transport->owner);
168
169 hba->transport = NULL;
170 kfree(hba);
171 return 0;
172 }
173