target_core_hba.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*******************************************************************************
  2. * Filename: target_core_hba.c
  3. *
  4. * This file contains the TCM HBA Transport related functions.
  5. *
  6. * (c) Copyright 2003-2012 RisingTide Systems LLC.
  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(subsystem_list);
  39. static DEFINE_MUTEX(subsystem_mutex);
  40. static u32 hba_id_counter;
  41. static DEFINE_SPINLOCK(hba_lock);
  42. static LIST_HEAD(hba_list);
  43. int transport_subsystem_register(struct se_subsystem_api *sub_api)
  44. {
  45. struct se_subsystem_api *s;
  46. INIT_LIST_HEAD(&sub_api->sub_api_list);
  47. mutex_lock(&subsystem_mutex);
  48. list_for_each_entry(s, &subsystem_list, sub_api_list) {
  49. if (!strcmp(s->name, sub_api->name)) {
  50. pr_err("%p is already registered with"
  51. " duplicate name %s, unable to process"
  52. " request\n", s, s->name);
  53. mutex_unlock(&subsystem_mutex);
  54. return -EEXIST;
  55. }
  56. }
  57. list_add_tail(&sub_api->sub_api_list, &subsystem_list);
  58. mutex_unlock(&subsystem_mutex);
  59. pr_debug("TCM: Registered subsystem plugin: %s struct module:"
  60. " %p\n", sub_api->name, sub_api->owner);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(transport_subsystem_register);
  64. void transport_subsystem_release(struct se_subsystem_api *sub_api)
  65. {
  66. mutex_lock(&subsystem_mutex);
  67. list_del(&sub_api->sub_api_list);
  68. mutex_unlock(&subsystem_mutex);
  69. }
  70. EXPORT_SYMBOL(transport_subsystem_release);
  71. static struct se_subsystem_api *core_get_backend(const char *sub_name)
  72. {
  73. struct se_subsystem_api *s;
  74. mutex_lock(&subsystem_mutex);
  75. list_for_each_entry(s, &subsystem_list, sub_api_list) {
  76. if (!strcmp(s->name, sub_name))
  77. goto found;
  78. }
  79. mutex_unlock(&subsystem_mutex);
  80. return NULL;
  81. found:
  82. if (s->owner && !try_module_get(s->owner))
  83. s = NULL;
  84. mutex_unlock(&subsystem_mutex);
  85. return s;
  86. }
  87. struct se_hba *
  88. core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
  89. {
  90. struct se_hba *hba;
  91. int ret = 0;
  92. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  93. if (!hba) {
  94. pr_err("Unable to allocate struct se_hba\n");
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. spin_lock_init(&hba->device_lock);
  98. mutex_init(&hba->hba_access_mutex);
  99. hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
  100. hba->hba_flags |= hba_flags;
  101. hba->transport = core_get_backend(plugin_name);
  102. if (!hba->transport) {
  103. ret = -EINVAL;
  104. goto out_free_hba;
  105. }
  106. ret = hba->transport->attach_hba(hba, plugin_dep_id);
  107. if (ret < 0)
  108. goto out_module_put;
  109. spin_lock(&hba_lock);
  110. hba->hba_id = hba_id_counter++;
  111. list_add_tail(&hba->hba_node, &hba_list);
  112. spin_unlock(&hba_lock);
  113. pr_debug("CORE_HBA[%d] - Attached HBA to Generic Target"
  114. " Core\n", hba->hba_id);
  115. return hba;
  116. out_module_put:
  117. if (hba->transport->owner)
  118. module_put(hba->transport->owner);
  119. hba->transport = NULL;
  120. out_free_hba:
  121. kfree(hba);
  122. return ERR_PTR(ret);
  123. }
  124. int
  125. core_delete_hba(struct se_hba *hba)
  126. {
  127. WARN_ON(hba->dev_count);
  128. hba->transport->detach_hba(hba);
  129. spin_lock(&hba_lock);
  130. list_del(&hba->hba_node);
  131. spin_unlock(&hba_lock);
  132. pr_debug("CORE_HBA[%d] - Detached HBA from Generic Target"
  133. " Core\n", hba->hba_id);
  134. if (hba->transport->owner)
  135. module_put(hba->transport->owner);
  136. hba->transport = NULL;
  137. kfree(hba);
  138. return 0;
  139. }