scsi_dh.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * SCSI device handler infrastruture.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2007
  19. * Authors:
  20. * Chandra Seetharaman <sekharan@us.ibm.com>
  21. * Mike Anderson <andmike@linux.vnet.ibm.com>
  22. */
  23. #include <scsi/scsi_dh.h>
  24. #include "../scsi_priv.h"
  25. static DEFINE_SPINLOCK(list_lock);
  26. static LIST_HEAD(scsi_dh_list);
  27. static struct scsi_device_handler *get_device_handler(const char *name)
  28. {
  29. struct scsi_device_handler *tmp, *found = NULL;
  30. spin_lock(&list_lock);
  31. list_for_each_entry(tmp, &scsi_dh_list, list) {
  32. if (!strcmp(tmp->name, name)) {
  33. found = tmp;
  34. break;
  35. }
  36. }
  37. spin_unlock(&list_lock);
  38. return found;
  39. }
  40. static int scsi_dh_notifier_add(struct device *dev, void *data)
  41. {
  42. struct scsi_device_handler *scsi_dh = data;
  43. scsi_dh->nb.notifier_call(&scsi_dh->nb, BUS_NOTIFY_ADD_DEVICE, dev);
  44. return 0;
  45. }
  46. /*
  47. * scsi_register_device_handler - register a device handler personality
  48. * module.
  49. * @scsi_dh - device handler to be registered.
  50. *
  51. * Returns 0 on success, -EBUSY if handler already registered.
  52. */
  53. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  54. {
  55. int ret = -EBUSY;
  56. struct scsi_device_handler *tmp;
  57. tmp = get_device_handler(scsi_dh->name);
  58. if (tmp)
  59. goto done;
  60. ret = bus_register_notifier(&scsi_bus_type, &scsi_dh->nb);
  61. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
  62. spin_lock(&list_lock);
  63. list_add(&scsi_dh->list, &scsi_dh_list);
  64. spin_unlock(&list_lock);
  65. done:
  66. return ret;
  67. }
  68. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  69. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  70. {
  71. struct scsi_device_handler *scsi_dh = data;
  72. scsi_dh->nb.notifier_call(&scsi_dh->nb, BUS_NOTIFY_DEL_DEVICE, dev);
  73. return 0;
  74. }
  75. /*
  76. * scsi_unregister_device_handler - register a device handler personality
  77. * module.
  78. * @scsi_dh - device handler to be unregistered.
  79. *
  80. * Returns 0 on success, -ENODEV if handler not registered.
  81. */
  82. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  83. {
  84. int ret = -ENODEV;
  85. struct scsi_device_handler *tmp;
  86. tmp = get_device_handler(scsi_dh->name);
  87. if (!tmp)
  88. goto done;
  89. ret = bus_unregister_notifier(&scsi_bus_type, &scsi_dh->nb);
  90. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
  91. scsi_dh_notifier_remove);
  92. spin_lock(&list_lock);
  93. list_del(&scsi_dh->list);
  94. spin_unlock(&list_lock);
  95. done:
  96. return ret;
  97. }
  98. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  99. /*
  100. * scsi_dh_activate - activate the path associated with the scsi_device
  101. * corresponding to the given request queue.
  102. * @q - Request queue that is associated with the scsi_device to be
  103. * activated.
  104. */
  105. int scsi_dh_activate(struct request_queue *q)
  106. {
  107. int err = 0;
  108. unsigned long flags;
  109. struct scsi_device *sdev;
  110. struct scsi_device_handler *scsi_dh = NULL;
  111. spin_lock_irqsave(q->queue_lock, flags);
  112. sdev = q->queuedata;
  113. if (sdev && sdev->scsi_dh_data)
  114. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  115. if (!scsi_dh || !get_device(&sdev->sdev_gendev))
  116. err = SCSI_DH_NOSYS;
  117. spin_unlock_irqrestore(q->queue_lock, flags);
  118. if (err)
  119. return err;
  120. if (scsi_dh->activate)
  121. err = scsi_dh->activate(sdev);
  122. put_device(&sdev->sdev_gendev);
  123. return err;
  124. }
  125. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  126. /*
  127. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  128. * the given name. FALSE(0) otherwise.
  129. * @name - name of the device handler.
  130. */
  131. int scsi_dh_handler_exist(const char *name)
  132. {
  133. return (get_device_handler(name) != NULL);
  134. }
  135. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  136. MODULE_DESCRIPTION("SCSI device handler");
  137. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  138. MODULE_LICENSE("GPL");