Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_hba.c | |
3 | * | |
e3d6f909 | 4 | * This file contains the TCM HBA Transport related functions. |
c66ac9db NB |
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> | |
c66ac9db | 34 | #include <linux/in.h> |
827509e3 | 35 | #include <linux/module.h> |
c66ac9db NB |
36 | #include <net/sock.h> |
37 | #include <net/tcp.h> | |
38 | ||
39 | #include <target/target_core_base.h> | |
40 | #include <target/target_core_device.h> | |
c66ac9db NB |
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 | ||
e3d6f909 AG |
49 | static u32 hba_id_counter; |
50 | ||
51 | static DEFINE_SPINLOCK(hba_lock); | |
52 | static LIST_HEAD(hba_list); | |
53 | ||
c66ac9db NB |
54 | int transport_subsystem_register(struct se_subsystem_api *sub_api) |
55 | { | |
56 | struct se_subsystem_api *s; | |
57 | ||
58 | INIT_LIST_HEAD(&sub_api->sub_api_list); | |
59 | ||
60 | mutex_lock(&subsystem_mutex); | |
61 | list_for_each_entry(s, &subsystem_list, sub_api_list) { | |
6708bb27 AG |
62 | if (!strcmp(s->name, sub_api->name)) { |
63 | pr_err("%p is already registered with" | |
c66ac9db NB |
64 | " duplicate name %s, unable to process" |
65 | " request\n", s, s->name); | |
66 | mutex_unlock(&subsystem_mutex); | |
67 | return -EEXIST; | |
68 | } | |
69 | } | |
70 | list_add_tail(&sub_api->sub_api_list, &subsystem_list); | |
71 | mutex_unlock(&subsystem_mutex); | |
72 | ||
6708bb27 | 73 | pr_debug("TCM: Registered subsystem plugin: %s struct module:" |
c66ac9db NB |
74 | " %p\n", sub_api->name, sub_api->owner); |
75 | return 0; | |
76 | } | |
77 | EXPORT_SYMBOL(transport_subsystem_register); | |
78 | ||
79 | void transport_subsystem_release(struct se_subsystem_api *sub_api) | |
80 | { | |
81 | mutex_lock(&subsystem_mutex); | |
82 | list_del(&sub_api->sub_api_list); | |
83 | mutex_unlock(&subsystem_mutex); | |
84 | } | |
85 | EXPORT_SYMBOL(transport_subsystem_release); | |
86 | ||
87 | static struct se_subsystem_api *core_get_backend(const char *sub_name) | |
88 | { | |
89 | struct se_subsystem_api *s; | |
90 | ||
91 | mutex_lock(&subsystem_mutex); | |
92 | list_for_each_entry(s, &subsystem_list, sub_api_list) { | |
93 | if (!strcmp(s->name, sub_name)) | |
94 | goto found; | |
95 | } | |
96 | mutex_unlock(&subsystem_mutex); | |
97 | return NULL; | |
98 | found: | |
99 | if (s->owner && !try_module_get(s->owner)) | |
100 | s = NULL; | |
101 | mutex_unlock(&subsystem_mutex); | |
102 | return s; | |
103 | } | |
104 | ||
105 | struct se_hba * | |
106 | core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags) | |
107 | { | |
108 | struct se_hba *hba; | |
109 | int ret = 0; | |
110 | ||
111 | hba = kzalloc(sizeof(*hba), GFP_KERNEL); | |
112 | if (!hba) { | |
6708bb27 | 113 | pr_err("Unable to allocate struct se_hba\n"); |
c66ac9db NB |
114 | return ERR_PTR(-ENOMEM); |
115 | } | |
116 | ||
117 | INIT_LIST_HEAD(&hba->hba_dev_list); | |
118 | spin_lock_init(&hba->device_lock); | |
c66ac9db NB |
119 | mutex_init(&hba->hba_access_mutex); |
120 | ||
121 | hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX); | |
122 | hba->hba_flags |= hba_flags; | |
123 | ||
c66ac9db NB |
124 | hba->transport = core_get_backend(plugin_name); |
125 | if (!hba->transport) { | |
126 | ret = -EINVAL; | |
127 | goto out_free_hba; | |
128 | } | |
129 | ||
130 | ret = hba->transport->attach_hba(hba, plugin_dep_id); | |
131 | if (ret < 0) | |
132 | goto out_module_put; | |
133 | ||
e3d6f909 AG |
134 | spin_lock(&hba_lock); |
135 | hba->hba_id = hba_id_counter++; | |
136 | list_add_tail(&hba->hba_node, &hba_list); | |
137 | spin_unlock(&hba_lock); | |
c66ac9db | 138 | |
6708bb27 | 139 | pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target" |
c66ac9db NB |
140 | " Core\n", hba->hba_id); |
141 | ||
142 | return hba; | |
143 | ||
144 | out_module_put: | |
145 | if (hba->transport->owner) | |
146 | module_put(hba->transport->owner); | |
147 | hba->transport = NULL; | |
148 | out_free_hba: | |
149 | kfree(hba); | |
150 | return ERR_PTR(ret); | |
151 | } | |
152 | ||
153 | int | |
154 | core_delete_hba(struct se_hba *hba) | |
155 | { | |
05aea6e7 FC |
156 | if (!list_empty(&hba->hba_dev_list)) |
157 | dump_stack(); | |
c66ac9db NB |
158 | |
159 | hba->transport->detach_hba(hba); | |
160 | ||
e3d6f909 AG |
161 | spin_lock(&hba_lock); |
162 | list_del(&hba->hba_node); | |
163 | spin_unlock(&hba_lock); | |
c66ac9db | 164 | |
6708bb27 | 165 | pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target" |
c66ac9db NB |
166 | " Core\n", hba->hba_id); |
167 | ||
168 | if (hba->transport->owner) | |
169 | module_put(hba->transport->owner); | |
170 | ||
171 | hba->transport = NULL; | |
172 | kfree(hba); | |
173 | return 0; | |
174 | } |