1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 <linux/module.h>
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>
41#include <target/target_core_tpg.h>
42#include <target/target_core_transport.h>
43
44#include "target_core_hba.h"
45
46static LIST_HEAD(subsystem_list);
47static DEFINE_MUTEX(subsystem_mutex);
48
49static u32 hba_id_counter;
50
51static DEFINE_SPINLOCK(hba_lock);
52static LIST_HEAD(hba_list);
53
54int 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) {
62 if (!strcmp(s->name, sub_api->name)) {
63 pr_err("%p is already registered with"
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
73 pr_debug("TCM: Registered subsystem plugin: %s struct module:"
74 " %p\n", sub_api->name, sub_api->owner);
75 return 0;
76}
77EXPORT_SYMBOL(transport_subsystem_register);
78
79void 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}
85EXPORT_SYMBOL(transport_subsystem_release);
86
87static 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;
98found:
99 if (s->owner && !try_module_get(s->owner))
100 s = NULL;
101 mutex_unlock(&subsystem_mutex);
102 return s;
103}
104
105struct se_hba *
106core_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) {
113 pr_err("Unable to allocate struct se_hba\n");
114 return ERR_PTR(-ENOMEM);
115 }
116
117 INIT_LIST_HEAD(&hba->hba_dev_list);
118 spin_lock_init(&hba->device_lock);
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
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
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);
138
139 pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
140 " Core\n", hba->hba_id);
141
142 return hba;
143
144out_module_put:
145 if (hba->transport->owner)
146 module_put(hba->transport->owner);
147 hba->transport = NULL;
148out_free_hba:
149 kfree(hba);
150 return ERR_PTR(ret);
151}
152
153int
154core_delete_hba(struct se_hba *hba)
155{
156 if (!list_empty(&hba->hba_dev_list))
157 dump_stack();
158
159 hba->transport->detach_hba(hba);
160
161 spin_lock(&hba_lock);
162 list_del(&hba->hba_node);
163 spin_unlock(&hba_lock);
164
165 pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
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}
175