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 | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 6 | * (c) Copyright 2003-2013 Datera, Inc. |
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 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 42 | static LIST_HEAD(backend_list); |
| 43 | static DEFINE_MUTEX(backend_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 50 | |
| 51 | int transport_backend_register(const struct target_backend_ops *ops) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 52 | { |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 53 | struct target_backend *tb, *old; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 54 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 55 | tb = kzalloc(sizeof(*tb), GFP_KERNEL); |
| 56 | if (!tb) |
| 57 | return -ENOMEM; |
| 58 | tb->ops = ops; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 59 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 60 | mutex_lock(&backend_mutex); |
| 61 | list_for_each_entry(old, &backend_list, list) { |
| 62 | if (!strcmp(old->ops->name, ops->name)) { |
| 63 | pr_err("backend %s already registered.\n", ops->name); |
| 64 | mutex_unlock(&backend_mutex); |
| 65 | kfree(tb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 66 | return -EEXIST; |
| 67 | } |
| 68 | } |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 69 | target_setup_backend_cits(tb); |
| 70 | list_add_tail(&tb->list, &backend_list); |
| 71 | mutex_unlock(&backend_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 72 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 73 | pr_debug("TCM: Registered subsystem plugin: %s struct module: %p\n", |
| 74 | ops->name, ops->owner); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 77 | EXPORT_SYMBOL(transport_backend_register); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 78 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 79 | void target_backend_unregister(const struct target_backend_ops *ops) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 80 | { |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 81 | struct target_backend *tb; |
| 82 | |
| 83 | mutex_lock(&backend_mutex); |
| 84 | list_for_each_entry(tb, &backend_list, list) { |
| 85 | if (tb->ops == ops) { |
| 86 | list_del(&tb->list); |
| 87 | kfree(tb); |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | mutex_unlock(&backend_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 92 | } |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 93 | EXPORT_SYMBOL(target_backend_unregister); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 94 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 95 | static struct target_backend *core_get_backend(const char *name) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 96 | { |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 97 | struct target_backend *tb; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 98 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 99 | mutex_lock(&backend_mutex); |
| 100 | list_for_each_entry(tb, &backend_list, list) { |
| 101 | if (!strcmp(tb->ops->name, name)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 102 | goto found; |
| 103 | } |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 104 | mutex_unlock(&backend_mutex); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 105 | return NULL; |
| 106 | found: |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 107 | if (tb->ops->owner && !try_module_get(tb->ops->owner)) |
| 108 | tb = NULL; |
| 109 | mutex_unlock(&backend_mutex); |
| 110 | return tb; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | struct se_hba * |
| 114 | core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags) |
| 115 | { |
| 116 | struct se_hba *hba; |
| 117 | int ret = 0; |
| 118 | |
| 119 | hba = kzalloc(sizeof(*hba), GFP_KERNEL); |
| 120 | if (!hba) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 121 | pr_err("Unable to allocate struct se_hba\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 122 | return ERR_PTR(-ENOMEM); |
| 123 | } |
| 124 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 125 | spin_lock_init(&hba->device_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 126 | mutex_init(&hba->hba_access_mutex); |
| 127 | |
| 128 | hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX); |
| 129 | hba->hba_flags |= hba_flags; |
| 130 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 131 | hba->backend = core_get_backend(plugin_name); |
| 132 | if (!hba->backend) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 133 | ret = -EINVAL; |
| 134 | goto out_free_hba; |
| 135 | } |
| 136 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 137 | ret = hba->backend->ops->attach_hba(hba, plugin_dep_id); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 138 | if (ret < 0) |
| 139 | goto out_module_put; |
| 140 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 141 | spin_lock(&hba_lock); |
| 142 | hba->hba_id = hba_id_counter++; |
| 143 | list_add_tail(&hba->hba_node, &hba_list); |
| 144 | spin_unlock(&hba_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 145 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 146 | pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 147 | " Core\n", hba->hba_id); |
| 148 | |
| 149 | return hba; |
| 150 | |
| 151 | out_module_put: |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 152 | module_put(hba->backend->ops->owner); |
| 153 | hba->backend = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 154 | out_free_hba: |
| 155 | kfree(hba); |
| 156 | return ERR_PTR(ret); |
| 157 | } |
| 158 | |
| 159 | int |
| 160 | core_delete_hba(struct se_hba *hba) |
| 161 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 162 | WARN_ON(hba->dev_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 163 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 164 | hba->backend->ops->detach_hba(hba); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 165 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 166 | spin_lock(&hba_lock); |
| 167 | list_del(&hba->hba_node); |
| 168 | spin_unlock(&hba_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 169 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 170 | pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 171 | " Core\n", hba->hba_id); |
| 172 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 173 | module_put(hba->backend->ops->owner); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 174 | |
Christoph Hellwig | 0a06d43 | 2015-05-10 18:14:56 +0200 | [diff] [blame] | 175 | hba->backend = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 176 | kfree(hba); |
| 177 | return 0; |
| 178 | } |