2 * SCSI device handler infrastruture.
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.
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.
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.
18 * Copyright IBM Corporation, 2007
20 * Chandra Seetharaman <sekharan@us.ibm.com>
21 * Mike Anderson <andmike@linux.vnet.ibm.com>
24 #include <scsi/scsi_dh.h>
25 #include "../scsi_priv.h"
27 static DEFINE_SPINLOCK(list_lock);
28 static LIST_HEAD(scsi_dh_list);
30 static struct scsi_device_handler *get_device_handler(const char *name)
32 struct scsi_device_handler *tmp, *found = NULL;
34 spin_lock(&list_lock);
35 list_for_each_entry(tmp, &scsi_dh_list, list) {
36 if (!strcmp(tmp->name, name)) {
41 spin_unlock(&list_lock);
45 static int scsi_dh_notifier_add(struct device *dev, void *data)
47 struct scsi_device_handler *scsi_dh = data;
49 scsi_dh->nb.notifier_call(&scsi_dh->nb, BUS_NOTIFY_ADD_DEVICE, dev);
54 * scsi_register_device_handler - register a device handler personality
56 * @scsi_dh - device handler to be registered.
58 * Returns 0 on success, -EBUSY if handler already registered.
60 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
63 struct scsi_device_handler *tmp;
65 tmp = get_device_handler(scsi_dh->name);
69 ret = bus_register_notifier(&scsi_bus_type, &scsi_dh->nb);
71 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
72 spin_lock(&list_lock);
73 list_add(&scsi_dh->list, &scsi_dh_list);
74 spin_unlock(&list_lock);
79 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
81 static int scsi_dh_notifier_remove(struct device *dev, void *data)
83 struct scsi_device_handler *scsi_dh = data;
85 scsi_dh->nb.notifier_call(&scsi_dh->nb, BUS_NOTIFY_DEL_DEVICE, dev);
90 * scsi_unregister_device_handler - register a device handler personality
92 * @scsi_dh - device handler to be unregistered.
94 * Returns 0 on success, -ENODEV if handler not registered.
96 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
99 struct scsi_device_handler *tmp;
101 tmp = get_device_handler(scsi_dh->name);
105 ret = bus_unregister_notifier(&scsi_bus_type, &scsi_dh->nb);
107 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
108 scsi_dh_notifier_remove);
109 spin_lock(&list_lock);
110 list_del(&scsi_dh->list);
111 spin_unlock(&list_lock);
116 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
119 * scsi_dh_activate - activate the path associated with the scsi_device
120 * corresponding to the given request queue.
121 * @q - Request queue that is associated with the scsi_device to be
124 int scsi_dh_activate(struct request_queue *q)
128 struct scsi_device *sdev;
129 struct scsi_device_handler *scsi_dh = NULL;
131 spin_lock_irqsave(q->queue_lock, flags);
133 if (sdev && sdev->scsi_dh_data)
134 scsi_dh = sdev->scsi_dh_data->scsi_dh;
135 if (!scsi_dh || !get_device(&sdev->sdev_gendev))
137 spin_unlock_irqrestore(q->queue_lock, flags);
142 if (scsi_dh->activate)
143 err = scsi_dh->activate(sdev);
144 put_device(&sdev->sdev_gendev);
147 EXPORT_SYMBOL_GPL(scsi_dh_activate);
150 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
151 * the given name. FALSE(0) otherwise.
152 * @name - name of the device handler.
154 int scsi_dh_handler_exist(const char *name)
156 return (get_device_handler(name) != NULL);
158 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
160 MODULE_DESCRIPTION("SCSI device handler");
161 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
162 MODULE_LICENSE("GPL");