scsi_dh.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 (!strncmp(tmp->name, name, strlen(tmp->name))) {
  33. found = tmp;
  34. break;
  35. }
  36. }
  37. spin_unlock(&list_lock);
  38. return found;
  39. }
  40. static int device_handler_match(struct scsi_device_handler *tmp,
  41. struct scsi_device *sdev)
  42. {
  43. int i;
  44. for(i = 0; tmp->devlist[i].vendor; i++) {
  45. if (!strncmp(sdev->vendor, tmp->devlist[i].vendor,
  46. strlen(tmp->devlist[i].vendor)) &&
  47. !strncmp(sdev->model, tmp->devlist[i].model,
  48. strlen(tmp->devlist[i].model))) {
  49. return 1;
  50. }
  51. }
  52. return 0;
  53. }
  54. /*
  55. * scsi_dh_handler_attach - Attach a device handler to a device
  56. * @sdev - SCSI device the device handler should attach to
  57. * @scsi_dh - The device handler to attach
  58. */
  59. static int scsi_dh_handler_attach(struct scsi_device *sdev,
  60. struct scsi_device_handler *scsi_dh)
  61. {
  62. int err = 0;
  63. if (sdev->scsi_dh_data) {
  64. if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
  65. err = -EBUSY;
  66. } else if (scsi_dh->attach)
  67. err = scsi_dh->attach(sdev);
  68. return err;
  69. }
  70. /*
  71. * scsi_dh_handler_detach - Detach a device handler from a device
  72. * @sdev - SCSI device the device handler should be detached from
  73. * @scsi_dh - Device handler to be detached
  74. *
  75. * Detach from a device handler. If a device handler is specified,
  76. * only detach if the currently attached handler is equal to it.
  77. */
  78. static void scsi_dh_handler_detach(struct scsi_device *sdev,
  79. struct scsi_device_handler *scsi_dh)
  80. {
  81. if (!sdev->scsi_dh_data)
  82. return;
  83. if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
  84. return;
  85. if (!scsi_dh)
  86. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  87. if (scsi_dh && scsi_dh->detach)
  88. scsi_dh->detach(sdev);
  89. }
  90. /*
  91. * scsi_dh_notifier - notifier chain callback
  92. */
  93. static int scsi_dh_notifier(struct notifier_block *nb,
  94. unsigned long action, void *data)
  95. {
  96. struct device *dev = data;
  97. struct scsi_device *sdev;
  98. int err = 0;
  99. struct scsi_device_handler *tmp, *devinfo = NULL;
  100. if (!scsi_is_sdev_device(dev))
  101. return 0;
  102. sdev = to_scsi_device(dev);
  103. spin_lock(&list_lock);
  104. list_for_each_entry(tmp, &scsi_dh_list, list) {
  105. if (device_handler_match(tmp, sdev)) {
  106. devinfo = tmp;
  107. break;
  108. }
  109. }
  110. spin_unlock(&list_lock);
  111. if (!devinfo)
  112. goto out;
  113. if (action == BUS_NOTIFY_ADD_DEVICE) {
  114. err = scsi_dh_handler_attach(sdev, devinfo);
  115. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  116. scsi_dh_handler_detach(sdev, NULL);
  117. }
  118. out:
  119. return err;
  120. }
  121. /*
  122. * scsi_dh_notifier_add - Callback for scsi_register_device_handler
  123. */
  124. static int scsi_dh_notifier_add(struct device *dev, void *data)
  125. {
  126. struct scsi_device_handler *scsi_dh = data;
  127. struct scsi_device *sdev;
  128. if (!scsi_is_sdev_device(dev))
  129. return 0;
  130. if (!get_device(dev))
  131. return 0;
  132. sdev = to_scsi_device(dev);
  133. if (device_handler_match(scsi_dh, sdev))
  134. scsi_dh_handler_attach(sdev, scsi_dh);
  135. put_device(dev);
  136. return 0;
  137. }
  138. /*
  139. * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
  140. */
  141. static int scsi_dh_notifier_remove(struct device *dev, void *data)
  142. {
  143. struct scsi_device_handler *scsi_dh = data;
  144. struct scsi_device *sdev;
  145. if (!scsi_is_sdev_device(dev))
  146. return 0;
  147. if (!get_device(dev))
  148. return 0;
  149. sdev = to_scsi_device(dev);
  150. scsi_dh_handler_detach(sdev, scsi_dh);
  151. put_device(dev);
  152. return 0;
  153. }
  154. /*
  155. * scsi_register_device_handler - register a device handler personality
  156. * module.
  157. * @scsi_dh - device handler to be registered.
  158. *
  159. * Returns 0 on success, -EBUSY if handler already registered.
  160. */
  161. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  162. {
  163. if (get_device_handler(scsi_dh->name))
  164. return -EBUSY;
  165. spin_lock(&list_lock);
  166. list_add(&scsi_dh->list, &scsi_dh_list);
  167. spin_unlock(&list_lock);
  168. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
  169. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  170. return SCSI_DH_OK;
  171. }
  172. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  173. /*
  174. * scsi_unregister_device_handler - register a device handler personality
  175. * module.
  176. * @scsi_dh - device handler to be unregistered.
  177. *
  178. * Returns 0 on success, -ENODEV if handler not registered.
  179. */
  180. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  181. {
  182. if (!get_device_handler(scsi_dh->name))
  183. return -ENODEV;
  184. bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
  185. scsi_dh_notifier_remove);
  186. spin_lock(&list_lock);
  187. list_del(&scsi_dh->list);
  188. spin_unlock(&list_lock);
  189. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  190. return SCSI_DH_OK;
  191. }
  192. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  193. /*
  194. * scsi_dh_activate - activate the path associated with the scsi_device
  195. * corresponding to the given request queue.
  196. * @q - Request queue that is associated with the scsi_device to be
  197. * activated.
  198. */
  199. int scsi_dh_activate(struct request_queue *q)
  200. {
  201. int err = 0;
  202. unsigned long flags;
  203. struct scsi_device *sdev;
  204. struct scsi_device_handler *scsi_dh = NULL;
  205. spin_lock_irqsave(q->queue_lock, flags);
  206. sdev = q->queuedata;
  207. if (sdev && sdev->scsi_dh_data)
  208. scsi_dh = sdev->scsi_dh_data->scsi_dh;
  209. if (!scsi_dh || !get_device(&sdev->sdev_gendev))
  210. err = SCSI_DH_NOSYS;
  211. spin_unlock_irqrestore(q->queue_lock, flags);
  212. if (err)
  213. return err;
  214. if (scsi_dh->activate)
  215. err = scsi_dh->activate(sdev);
  216. put_device(&sdev->sdev_gendev);
  217. return err;
  218. }
  219. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  220. /*
  221. * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
  222. * the given name. FALSE(0) otherwise.
  223. * @name - name of the device handler.
  224. */
  225. int scsi_dh_handler_exist(const char *name)
  226. {
  227. return (get_device_handler(name) != NULL);
  228. }
  229. EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
  230. static struct notifier_block scsi_dh_nb = {
  231. .notifier_call = scsi_dh_notifier
  232. };
  233. static int __init scsi_dh_init(void)
  234. {
  235. int r;
  236. r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
  237. return r;
  238. }
  239. static void __exit scsi_dh_exit(void)
  240. {
  241. bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
  242. }
  243. module_init(scsi_dh_init);
  244. module_exit(scsi_dh_exit);
  245. MODULE_DESCRIPTION("SCSI device handler");
  246. MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
  247. MODULE_LICENSE("GPL");