Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * Filename: target_core_hba.c
4 *
5 * This file contains the TCM HBA Transport related functions.
6 *
7 * (c) Copyright 2003-2013 Datera, Inc.
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 ******************************************************************************/
12
13#include <linux/net.h>
14#include <linux/string.h>
15#include <linux/timer.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/in.h>
19#include <linux/module.h>
20#include <net/sock.h>
21#include <net/tcp.h>
22
23#include <target/target_core_base.h>
24#include <target/target_core_backend.h>
25#include <target/target_core_fabric.h>
26
27#include "target_core_internal.h"
28
29static LIST_HEAD(backend_list);
30static DEFINE_MUTEX(backend_mutex);
31
32static u32 hba_id_counter;
33
34static DEFINE_SPINLOCK(hba_lock);
35static LIST_HEAD(hba_list);
36
37
38int transport_backend_register(const struct target_backend_ops *ops)
39{
40 struct target_backend *tb, *old;
41
42 tb = kzalloc(sizeof(*tb), GFP_KERNEL);
43 if (!tb)
44 return -ENOMEM;
45 tb->ops = ops;
46
47 mutex_lock(&backend_mutex);
48 list_for_each_entry(old, &backend_list, list) {
49 if (!strcmp(old->ops->name, ops->name)) {
50 pr_err("backend %s already registered.\n", ops->name);
51 mutex_unlock(&backend_mutex);
52 kfree(tb);
53 return -EEXIST;
54 }
55 }
56 target_setup_backend_cits(tb);
57 list_add_tail(&tb->list, &backend_list);
58 mutex_unlock(&backend_mutex);
59
60 pr_debug("TCM: Registered subsystem plugin: %s struct module: %p\n",
61 ops->name, ops->owner);
62 return 0;
63}
64EXPORT_SYMBOL(transport_backend_register);
65
66void target_backend_unregister(const struct target_backend_ops *ops)
67{
68 struct target_backend *tb;
69
70 mutex_lock(&backend_mutex);
71 list_for_each_entry(tb, &backend_list, list) {
72 if (tb->ops == ops) {
73 list_del(&tb->list);
74 mutex_unlock(&backend_mutex);
75 /*
76 * Wait for any outstanding backend driver ->rcu_head
77 * callbacks to complete post TBO->free_device() ->
78 * call_rcu(), before allowing backend driver module
79 * unload of target_backend_ops->owner to proceed.
80 */
81 rcu_barrier();
82 kfree(tb);
83 return;
84 }
85 }
86 mutex_unlock(&backend_mutex);
87}
88EXPORT_SYMBOL(target_backend_unregister);
89
90static struct target_backend *core_get_backend(const char *name)
91{
92 struct target_backend *tb;
93
94 mutex_lock(&backend_mutex);
95 list_for_each_entry(tb, &backend_list, list) {
96 if (!strcmp(tb->ops->name, name))
97 goto found;
98 }
99 mutex_unlock(&backend_mutex);
100 return NULL;
101found:
102 if (tb->ops->owner && !try_module_get(tb->ops->owner))
103 tb = NULL;
104 mutex_unlock(&backend_mutex);
105 return tb;
106}
107
108struct se_hba *
109core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
110{
111 struct se_hba *hba;
112 int ret = 0;
113
114 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
115 if (!hba) {
116 pr_err("Unable to allocate struct se_hba\n");
117 return ERR_PTR(-ENOMEM);
118 }
119
120 spin_lock_init(&hba->device_lock);
121 mutex_init(&hba->hba_access_mutex);
122
123 hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
124 hba->hba_flags |= hba_flags;
125
126 hba->backend = core_get_backend(plugin_name);
127 if (!hba->backend) {
128 ret = -EINVAL;
129 goto out_free_hba;
130 }
131
132 ret = hba->backend->ops->attach_hba(hba, plugin_dep_id);
133 if (ret < 0)
134 goto out_module_put;
135
136 spin_lock(&hba_lock);
137 hba->hba_id = hba_id_counter++;
138 list_add_tail(&hba->hba_node, &hba_list);
139 spin_unlock(&hba_lock);
140
141 pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
142 " Core\n", hba->hba_id);
143
144 return hba;
145
146out_module_put:
147 module_put(hba->backend->ops->owner);
148 hba->backend = NULL;
149out_free_hba:
150 kfree(hba);
151 return ERR_PTR(ret);
152}
153
154int
155core_delete_hba(struct se_hba *hba)
156{
157 WARN_ON(hba->dev_count);
158
159 hba->backend->ops->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 module_put(hba->backend->ops->owner);
169
170 hba->backend = NULL;
171 kfree(hba);
172 return 0;
173}
174
175bool target_sense_desc_format(struct se_device *dev)
176{
177 return (dev) ? dev->transport->get_blocks(dev) > U32_MAX : false;
178}
1/*******************************************************************************
2 * Filename: target_core_hba.c
3 *
4 * This file contains the TCM HBA Transport related functions.
5 *
6 * (c) Copyright 2003-2013 Datera, Inc.
7 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 ******************************************************************************/
25
26#include <linux/net.h>
27#include <linux/string.h>
28#include <linux/timer.h>
29#include <linux/slab.h>
30#include <linux/spinlock.h>
31#include <linux/in.h>
32#include <linux/module.h>
33#include <net/sock.h>
34#include <net/tcp.h>
35
36#include <target/target_core_base.h>
37#include <target/target_core_backend.h>
38#include <target/target_core_fabric.h>
39
40#include "target_core_internal.h"
41
42static LIST_HEAD(subsystem_list);
43static DEFINE_MUTEX(subsystem_mutex);
44
45static u32 hba_id_counter;
46
47static DEFINE_SPINLOCK(hba_lock);
48static LIST_HEAD(hba_list);
49
50int transport_subsystem_register(struct se_subsystem_api *sub_api)
51{
52 struct se_subsystem_api *s;
53
54 INIT_LIST_HEAD(&sub_api->sub_api_list);
55
56 mutex_lock(&subsystem_mutex);
57 list_for_each_entry(s, &subsystem_list, sub_api_list) {
58 if (!strcmp(s->name, sub_api->name)) {
59 pr_err("%p is already registered with"
60 " duplicate name %s, unable to process"
61 " request\n", s, s->name);
62 mutex_unlock(&subsystem_mutex);
63 return -EEXIST;
64 }
65 }
66 list_add_tail(&sub_api->sub_api_list, &subsystem_list);
67 mutex_unlock(&subsystem_mutex);
68
69 pr_debug("TCM: Registered subsystem plugin: %s struct module:"
70 " %p\n", sub_api->name, sub_api->owner);
71 return 0;
72}
73EXPORT_SYMBOL(transport_subsystem_register);
74
75void transport_subsystem_release(struct se_subsystem_api *sub_api)
76{
77 mutex_lock(&subsystem_mutex);
78 list_del(&sub_api->sub_api_list);
79 mutex_unlock(&subsystem_mutex);
80}
81EXPORT_SYMBOL(transport_subsystem_release);
82
83static struct se_subsystem_api *core_get_backend(const char *sub_name)
84{
85 struct se_subsystem_api *s;
86
87 mutex_lock(&subsystem_mutex);
88 list_for_each_entry(s, &subsystem_list, sub_api_list) {
89 if (!strcmp(s->name, sub_name))
90 goto found;
91 }
92 mutex_unlock(&subsystem_mutex);
93 return NULL;
94found:
95 if (s->owner && !try_module_get(s->owner))
96 s = NULL;
97 mutex_unlock(&subsystem_mutex);
98 return s;
99}
100
101struct se_hba *
102core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
103{
104 struct se_hba *hba;
105 int ret = 0;
106
107 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
108 if (!hba) {
109 pr_err("Unable to allocate struct se_hba\n");
110 return ERR_PTR(-ENOMEM);
111 }
112
113 spin_lock_init(&hba->device_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 hba->transport = core_get_backend(plugin_name);
120 if (!hba->transport) {
121 ret = -EINVAL;
122 goto out_free_hba;
123 }
124
125 ret = hba->transport->attach_hba(hba, plugin_dep_id);
126 if (ret < 0)
127 goto out_module_put;
128
129 spin_lock(&hba_lock);
130 hba->hba_id = hba_id_counter++;
131 list_add_tail(&hba->hba_node, &hba_list);
132 spin_unlock(&hba_lock);
133
134 pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
135 " Core\n", hba->hba_id);
136
137 return hba;
138
139out_module_put:
140 if (hba->transport->owner)
141 module_put(hba->transport->owner);
142 hba->transport = NULL;
143out_free_hba:
144 kfree(hba);
145 return ERR_PTR(ret);
146}
147
148int
149core_delete_hba(struct se_hba *hba)
150{
151 WARN_ON(hba->dev_count);
152
153 hba->transport->detach_hba(hba);
154
155 spin_lock(&hba_lock);
156 list_del(&hba->hba_node);
157 spin_unlock(&hba_lock);
158
159 pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
160 " Core\n", hba->hba_id);
161
162 if (hba->transport->owner)
163 module_put(hba->transport->owner);
164
165 hba->transport = NULL;
166 kfree(hba);
167 return 0;
168}