target_core_hba.c 4.4 KB

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