Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Filename: target_core_hba.c |
| 3 | * |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4 | * This file contains the TCM HBA Transport related functions. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5 | * |
Nicholas Bellinger | fd9a11d | 2012-11-09 14:51:48 -0800 | [diff] [blame] | 6 | * (c) Copyright 2003-2012 RisingTide Systems LLC. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 31 | #include <linux/in.h> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 32 | #include <linux/module.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 33 | #include <net/sock.h> |
| 34 | #include <net/tcp.h> |
| 35 | |
| 36 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 37 | #include <target/target_core_backend.h> |
| 38 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 39 | |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 40 | #include "target_core_internal.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 41 | |
| 42 | static LIST_HEAD(subsystem_list); |
| 43 | static DEFINE_MUTEX(subsystem_mutex); |
| 44 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 45 | static u32 hba_id_counter; |
| 46 | |
| 47 | static DEFINE_SPINLOCK(hba_lock); |
| 48 | static LIST_HEAD(hba_list); |
| 49 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 50 | int 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) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 58 | if (!strcmp(s->name, sub_api->name)) { |
| 59 | pr_err("%p is already registered with" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 69 | pr_debug("TCM: Registered subsystem plugin: %s struct module:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 70 | " %p\n", sub_api->name, sub_api->owner); |
| 71 | return 0; |
| 72 | } |
| 73 | EXPORT_SYMBOL(transport_subsystem_register); |
| 74 | |
| 75 | void 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 | } |
| 81 | EXPORT_SYMBOL(transport_subsystem_release); |
| 82 | |
| 83 | static 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; |
| 94 | found: |
| 95 | if (s->owner && !try_module_get(s->owner)) |
| 96 | s = NULL; |
| 97 | mutex_unlock(&subsystem_mutex); |
| 98 | return s; |
| 99 | } |
| 100 | |
| 101 | struct se_hba * |
| 102 | core_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) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 109 | pr_err("Unable to allocate struct se_hba\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 110 | return ERR_PTR(-ENOMEM); |
| 111 | } |
| 112 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 113 | spin_lock_init(&hba->device_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 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); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 133 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 134 | pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 135 | " Core\n", hba->hba_id); |
| 136 | |
| 137 | return hba; |
| 138 | |
| 139 | out_module_put: |
| 140 | if (hba->transport->owner) |
| 141 | module_put(hba->transport->owner); |
| 142 | hba->transport = NULL; |
| 143 | out_free_hba: |
| 144 | kfree(hba); |
| 145 | return ERR_PTR(ret); |
| 146 | } |
| 147 | |
| 148 | int |
| 149 | core_delete_hba(struct se_hba *hba) |
| 150 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 151 | WARN_ON(hba->dev_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 152 | |
| 153 | hba->transport->detach_hba(hba); |
| 154 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 155 | spin_lock(&hba_lock); |
| 156 | list_del(&hba->hba_node); |
| 157 | spin_unlock(&hba_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 158 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 159 | pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | } |