xHCI: fix bug in xhci_clear_command_ring()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / target_core_hba.c
blobc68019d6c406292335522d536760fb8bb5904629
1 /*******************************************************************************
2 * Filename: target_core_hba.c
4 * This file contains the TCM HBA Transport related functions.
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
11 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
27 ******************************************************************************/
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>
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>
44 #include "target_core_hba.h"
46 static LIST_HEAD(subsystem_list);
47 static DEFINE_MUTEX(subsystem_mutex);
49 static u32 hba_id_counter;
51 static DEFINE_SPINLOCK(hba_lock);
52 static LIST_HEAD(hba_list);
54 int transport_subsystem_register(struct se_subsystem_api *sub_api)
56 struct se_subsystem_api *s;
58 INIT_LIST_HEAD(&sub_api->sub_api_list);
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;
70 list_add_tail(&sub_api->sub_api_list, &subsystem_list);
71 mutex_unlock(&subsystem_mutex);
73 pr_debug("TCM: Registered subsystem plugin: %s struct module:"
74 " %p\n", sub_api->name, sub_api->owner);
75 return 0;
77 EXPORT_SYMBOL(transport_subsystem_register);
79 void transport_subsystem_release(struct se_subsystem_api *sub_api)
81 mutex_lock(&subsystem_mutex);
82 list_del(&sub_api->sub_api_list);
83 mutex_unlock(&subsystem_mutex);
85 EXPORT_SYMBOL(transport_subsystem_release);
87 static struct se_subsystem_api *core_get_backend(const char *sub_name)
89 struct se_subsystem_api *s;
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;
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;
105 struct se_hba *
106 core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
108 struct se_hba *hba;
109 int ret = 0;
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);
117 INIT_LIST_HEAD(&hba->hba_dev_list);
118 spin_lock_init(&hba->device_lock);
119 mutex_init(&hba->hba_access_mutex);
121 hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
122 hba->hba_flags |= hba_flags;
124 hba->transport = core_get_backend(plugin_name);
125 if (!hba->transport) {
126 ret = -EINVAL;
127 goto out_free_hba;
130 ret = hba->transport->attach_hba(hba, plugin_dep_id);
131 if (ret < 0)
132 goto out_module_put;
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);
139 pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
140 " Core\n", hba->hba_id);
142 return hba;
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);
154 core_delete_hba(struct se_hba *hba)
156 if (!list_empty(&hba->hba_dev_list))
157 dump_stack();
159 hba->transport->detach_hba(hba);
161 spin_lock(&hba_lock);
162 list_del(&hba->hba_node);
163 spin_unlock(&hba_lock);
165 pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
166 " Core\n", hba->hba_id);
168 if (hba->transport->owner)
169 module_put(hba->transport->owner);
171 hba->transport = NULL;
172 kfree(hba);
173 return 0;