blob: ff95f95dcd13d571a59c70b7f6a7b1c6d0a6120d [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_hba.c
3 *
Andy Grovere3d6f902011-07-19 08:55:10 +00004 * This file contains the TCM HBA Transport related functions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08005 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2003-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#include <linux/in.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040032#include <linux/module.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080033#include <net/sock.h>
34#include <net/tcp.h>
35
36#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050037#include <target/target_core_backend.h>
38#include <target/target_core_fabric.h>
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080039#include <target/target_core_configfs.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040
Christoph Hellwige26d99a2011-11-14 12:30:30 -050041#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080042
43static LIST_HEAD(subsystem_list);
44static DEFINE_MUTEX(subsystem_mutex);
45
Andy Grovere3d6f902011-07-19 08:55:10 +000046static u32 hba_id_counter;
47
48static DEFINE_SPINLOCK(hba_lock);
49static LIST_HEAD(hba_list);
50
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051int transport_subsystem_register(struct se_subsystem_api *sub_api)
52{
53 struct se_subsystem_api *s;
54
55 INIT_LIST_HEAD(&sub_api->sub_api_list);
56
57 mutex_lock(&subsystem_mutex);
58 list_for_each_entry(s, &subsystem_list, sub_api_list) {
Andy Grover6708bb22011-06-08 10:36:43 -070059 if (!strcmp(s->name, sub_api->name)) {
60 pr_err("%p is already registered with"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061 " duplicate name %s, unable to process"
62 " request\n", s, s->name);
63 mutex_unlock(&subsystem_mutex);
64 return -EEXIST;
65 }
66 }
67 list_add_tail(&sub_api->sub_api_list, &subsystem_list);
68 mutex_unlock(&subsystem_mutex);
69
Andy Grover6708bb22011-06-08 10:36:43 -070070 pr_debug("TCM: Registered subsystem plugin: %s struct module:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080071 " %p\n", sub_api->name, sub_api->owner);
72 return 0;
73}
74EXPORT_SYMBOL(transport_subsystem_register);
75
76void transport_subsystem_release(struct se_subsystem_api *sub_api)
77{
78 mutex_lock(&subsystem_mutex);
79 list_del(&sub_api->sub_api_list);
80 mutex_unlock(&subsystem_mutex);
81}
82EXPORT_SYMBOL(transport_subsystem_release);
83
84static struct se_subsystem_api *core_get_backend(const char *sub_name)
85{
86 struct se_subsystem_api *s;
87
88 mutex_lock(&subsystem_mutex);
89 list_for_each_entry(s, &subsystem_list, sub_api_list) {
90 if (!strcmp(s->name, sub_name))
91 goto found;
92 }
93 mutex_unlock(&subsystem_mutex);
94 return NULL;
95found:
96 if (s->owner && !try_module_get(s->owner))
97 s = NULL;
98 mutex_unlock(&subsystem_mutex);
99 return s;
100}
101
102struct se_hba *
103core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
104{
105 struct se_hba *hba;
106 int ret = 0;
107
108 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
109 if (!hba) {
Andy Grover6708bb22011-06-08 10:36:43 -0700110 pr_err("Unable to allocate struct se_hba\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 return ERR_PTR(-ENOMEM);
112 }
113
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800114 spin_lock_init(&hba->device_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800115 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 hba->transport = core_get_backend(plugin_name);
121 if (!hba->transport) {
122 ret = -EINVAL;
123 goto out_free_hba;
124 }
125
126 ret = hba->transport->attach_hba(hba, plugin_dep_id);
127 if (ret < 0)
128 goto out_module_put;
129
Andy Grovere3d6f902011-07-19 08:55:10 +0000130 spin_lock(&hba_lock);
131 hba->hba_id = hba_id_counter++;
132 list_add_tail(&hba->hba_node, &hba_list);
133 spin_unlock(&hba_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134
Andy Grover6708bb22011-06-08 10:36:43 -0700135 pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 " Core\n", hba->hba_id);
137
138 return hba;
139
140out_module_put:
Markus Elfring2ed37f62014-11-21 10:25:45 +0100141 module_put(hba->transport->owner);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142 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{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400151 WARN_ON(hba->dev_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800152
153 hba->transport->detach_hba(hba);
154
Andy Grovere3d6f902011-07-19 08:55:10 +0000155 spin_lock(&hba_lock);
156 list_del(&hba->hba_node);
157 spin_unlock(&hba_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800158
Andy Grover6708bb22011-06-08 10:36:43 -0700159 pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800160 " Core\n", hba->hba_id);
161
Markus Elfring2ed37f62014-11-21 10:25:45 +0100162 module_put(hba->transport->owner);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163
164 hba->transport = NULL;
165 kfree(hba);
166 return 0;
167}