target_core_hba.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*******************************************************************************
  2. * Filename: target_core_hba.c
  3. *
  4. * This file copntains the iSCSI 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 <net/sock.h>
  35. #include <net/tcp.h>
  36. #include <target/target_core_base.h>
  37. #include <target/target_core_device.h>
  38. #include <target/target_core_device.h>
  39. #include <target/target_core_tpg.h>
  40. #include <target/target_core_transport.h>
  41. #include "target_core_hba.h"
  42. static LIST_HEAD(subsystem_list);
  43. static DEFINE_MUTEX(subsystem_mutex);
  44. int transport_subsystem_register(struct se_subsystem_api *sub_api)
  45. {
  46. struct se_subsystem_api *s;
  47. INIT_LIST_HEAD(&sub_api->sub_api_list);
  48. mutex_lock(&subsystem_mutex);
  49. list_for_each_entry(s, &subsystem_list, sub_api_list) {
  50. if (!(strcmp(s->name, sub_api->name))) {
  51. printk(KERN_ERR "%p is already registered with"
  52. " duplicate name %s, unable to process"
  53. " request\n", s, s->name);
  54. mutex_unlock(&subsystem_mutex);
  55. return -EEXIST;
  56. }
  57. }
  58. list_add_tail(&sub_api->sub_api_list, &subsystem_list);
  59. mutex_unlock(&subsystem_mutex);
  60. printk(KERN_INFO "TCM: Registered subsystem plugin: %s struct module:"
  61. " %p\n", sub_api->name, sub_api->owner);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(transport_subsystem_register);
  65. void transport_subsystem_release(struct se_subsystem_api *sub_api)
  66. {
  67. mutex_lock(&subsystem_mutex);
  68. list_del(&sub_api->sub_api_list);
  69. mutex_unlock(&subsystem_mutex);
  70. }
  71. EXPORT_SYMBOL(transport_subsystem_release);
  72. static struct se_subsystem_api *core_get_backend(const char *sub_name)
  73. {
  74. struct se_subsystem_api *s;
  75. mutex_lock(&subsystem_mutex);
  76. list_for_each_entry(s, &subsystem_list, sub_api_list) {
  77. if (!strcmp(s->name, sub_name))
  78. goto found;
  79. }
  80. mutex_unlock(&subsystem_mutex);
  81. return NULL;
  82. found:
  83. if (s->owner && !try_module_get(s->owner))
  84. s = NULL;
  85. mutex_unlock(&subsystem_mutex);
  86. return s;
  87. }
  88. struct se_hba *
  89. core_alloc_hba(const char *plugin_name, u32 plugin_dep_id, u32 hba_flags)
  90. {
  91. struct se_hba *hba;
  92. int ret = 0;
  93. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  94. if (!hba) {
  95. printk(KERN_ERR "Unable to allocate struct se_hba\n");
  96. return ERR_PTR(-ENOMEM);
  97. }
  98. INIT_LIST_HEAD(&hba->hba_dev_list);
  99. spin_lock_init(&hba->device_lock);
  100. spin_lock_init(&hba->hba_queue_lock);
  101. mutex_init(&hba->hba_access_mutex);
  102. hba->hba_index = scsi_get_new_index(SCSI_INST_INDEX);
  103. hba->hba_flags |= hba_flags;
  104. atomic_set(&hba->max_queue_depth, 0);
  105. atomic_set(&hba->left_queue_depth, 0);
  106. hba->transport = core_get_backend(plugin_name);
  107. if (!hba->transport) {
  108. ret = -EINVAL;
  109. goto out_free_hba;
  110. }
  111. ret = hba->transport->attach_hba(hba, plugin_dep_id);
  112. if (ret < 0)
  113. goto out_module_put;
  114. spin_lock(&se_global->hba_lock);
  115. hba->hba_id = se_global->g_hba_id_counter++;
  116. list_add_tail(&hba->hba_list, &se_global->g_hba_list);
  117. spin_unlock(&se_global->hba_lock);
  118. printk(KERN_INFO "CORE_HBA[%d] - Attached HBA to Generic Target"
  119. " Core\n", hba->hba_id);
  120. return hba;
  121. out_module_put:
  122. if (hba->transport->owner)
  123. module_put(hba->transport->owner);
  124. hba->transport = NULL;
  125. out_free_hba:
  126. kfree(hba);
  127. return ERR_PTR(ret);
  128. }
  129. int
  130. core_delete_hba(struct se_hba *hba)
  131. {
  132. struct se_device *dev, *dev_tmp;
  133. spin_lock(&hba->device_lock);
  134. list_for_each_entry_safe(dev, dev_tmp, &hba->hba_dev_list, dev_list) {
  135. se_clear_dev_ports(dev);
  136. spin_unlock(&hba->device_lock);
  137. se_release_device_for_hba(dev);
  138. spin_lock(&hba->device_lock);
  139. }
  140. spin_unlock(&hba->device_lock);
  141. hba->transport->detach_hba(hba);
  142. spin_lock(&se_global->hba_lock);
  143. list_del(&hba->hba_list);
  144. spin_unlock(&se_global->hba_lock);
  145. printk(KERN_INFO "CORE_HBA[%d] - Detached HBA from Generic Target"
  146. " Core\n", hba->hba_id);
  147. if (hba->transport->owner)
  148. module_put(hba->transport->owner);
  149. hba->transport = NULL;
  150. kfree(hba);
  151. return 0;
  152. }