target_core_hba.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <linux/net.h>
  26. #include <linux/string.h>
  27. #include <linux/timer.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/in.h>
  31. #include <linux/module.h>
  32. #include <net/sock.h>
  33. #include <net/tcp.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_backend.h>
  36. #include <target/target_core_fabric.h>
  37. #include "target_core_internal.h"
  38. static LIST_HEAD(backend_list);
  39. static DEFINE_MUTEX(backend_mutex);
  40. static u32 hba_id_counter;
  41. static DEFINE_SPINLOCK(hba_lock);
  42. static LIST_HEAD(hba_list);
  43. int transport_backend_register(const struct target_backend_ops *ops)
  44. {
  45. struct target_backend *tb, *old;
  46. tb = kzalloc(sizeof(*tb), GFP_KERNEL);
  47. if (!tb)
  48. return -ENOMEM;
  49. tb->ops = ops;
  50. mutex_lock(&backend_mutex);
  51. list_for_each_entry(old, &backend_list, list) {
  52. if (!strcmp(old->ops->name, ops->name)) {
  53. pr_err("backend %s already registered.\n", ops->name);
  54. mutex_unlock(&backend_mutex);
  55. kfree(tb);
  56. return -EEXIST;
  57. }
  58. }
  59. target_setup_backend_cits(tb);
  60. list_add_tail(&tb->list, &backend_list);
  61. mutex_unlock(&backend_mutex);
  62. pr_debug("TCM: Registered subsystem plugin: %s struct module: %p\n",
  63. ops->name, ops->owner);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(transport_backend_register);
  67. void target_backend_unregister(const struct target_backend_ops *ops)
  68. {
  69. struct target_backend *tb;
  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. }
  88. EXPORT_SYMBOL(target_backend_unregister);
  89. static struct target_backend *core_get_backend(const char *name)
  90. {
  91. struct target_backend *tb;
  92. mutex_lock(&backend_mutex);
  93. list_for_each_entry(tb, &backend_list, list) {
  94. if (!strcmp(tb->ops->name, name))
  95. goto found;
  96. }
  97. mutex_unlock(&backend_mutex);
  98. return NULL;
  99. found:
  100. if (tb->ops->owner && !try_module_get(tb->ops->owner))
  101. tb = NULL;
  102. mutex_unlock(&backend_mutex);
  103. return tb;
  104. }
  105. struct se_hba *
  106. core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
  107. {
  108. struct se_hba *hba;
  109. int ret = 0;
  110. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  111. if (!hba) {
  112. pr_err("Unable to allocate struct se_hba\n");
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. spin_lock_init(&hba->device_lock);
  116. mutex_init(&hba->hba_access_mutex);
  117. hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
  118. hba->hba_flags |= hba_flags;
  119. hba->backend = core_get_backend(plugin_name);
  120. if (!hba->backend) {
  121. ret = -EINVAL;
  122. goto out_free_hba;
  123. }
  124. ret = hba->backend->ops->attach_hba(hba, plugin_dep_id);
  125. if (ret < 0)
  126. goto out_module_put;
  127. spin_lock(&hba_lock);
  128. hba->hba_id = hba_id_counter++;
  129. list_add_tail(&hba->hba_node, &hba_list);
  130. spin_unlock(&hba_lock);
  131. pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
  132. " Core\n", hba->hba_id);
  133. return hba;
  134. out_module_put:
  135. module_put(hba->backend->ops->owner);
  136. hba->backend = NULL;
  137. out_free_hba:
  138. kfree(hba);
  139. return ERR_PTR(ret);
  140. }
  141. int
  142. core_delete_hba(struct se_hba *hba)
  143. {
  144. WARN_ON(hba->dev_count);
  145. hba->backend->ops->detach_hba(hba);
  146. spin_lock(&hba_lock);
  147. list_del(&hba->hba_node);
  148. spin_unlock(&hba_lock);
  149. pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
  150. " Core\n", hba->hba_id);
  151. module_put(hba->backend->ops->owner);
  152. hba->backend = NULL;
  153. kfree(hba);
  154. return 0;
  155. }
  156. bool target_sense_desc_format(struct se_device *dev)
  157. {
  158. return (dev) ? dev->transport->get_blocks(dev) > U32_MAX : false;
  159. }