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 (!strncmp(tmp->name, name, strlen(tmp->name))) {
41 spin_unlock(&list_lock);
45 static int device_handler_match(struct scsi_device_handler *tmp,
46 struct scsi_device *sdev)
50 for(i = 0; tmp->devlist[i].vendor; i++) {
51 if (!strncmp(sdev->vendor, tmp->devlist[i].vendor,
52 strlen(tmp->devlist[i].vendor)) &&
53 !strncmp(sdev->model, tmp->devlist[i].model,
54 strlen(tmp->devlist[i].model))) {
63 * scsi_dh_handler_attach - Attach a device handler to a device
64 * @sdev - SCSI device the device handler should attach to
65 * @scsi_dh - The device handler to attach
67 static int scsi_dh_handler_attach(struct scsi_device *sdev,
68 struct scsi_device_handler *scsi_dh)
72 if (sdev->scsi_dh_data) {
73 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
75 } else if (scsi_dh->attach)
76 err = scsi_dh->attach(sdev);
82 * scsi_dh_handler_detach - Detach a device handler from a device
83 * @sdev - SCSI device the device handler should be detached from
84 * @scsi_dh - Device handler to be detached
86 * Detach from a device handler. If a device handler is specified,
87 * only detach if the currently attached handler is equal to it.
89 static void scsi_dh_handler_detach(struct scsi_device *sdev,
90 struct scsi_device_handler *scsi_dh)
92 if (!sdev->scsi_dh_data)
95 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
99 scsi_dh = sdev->scsi_dh_data->scsi_dh;
101 if (scsi_dh && scsi_dh->detach)
102 scsi_dh->detach(sdev);
106 * scsi_dh_notifier - notifier chain callback
108 static int scsi_dh_notifier(struct notifier_block *nb,
109 unsigned long action, void *data)
111 struct device *dev = data;
112 struct scsi_device *sdev;
114 struct scsi_device_handler *tmp, *devinfo = NULL;
116 if (!scsi_is_sdev_device(dev))
119 sdev = to_scsi_device(dev);
121 spin_lock(&list_lock);
122 list_for_each_entry(tmp, &scsi_dh_list, list) {
123 if (device_handler_match(tmp, sdev)) {
128 spin_unlock(&list_lock);
133 if (action == BUS_NOTIFY_ADD_DEVICE) {
134 err = scsi_dh_handler_attach(sdev, devinfo);
135 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
136 scsi_dh_handler_detach(sdev, NULL);
143 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
145 static int scsi_dh_notifier_add(struct device *dev, void *data)
147 struct scsi_device_handler *scsi_dh = data;
148 struct scsi_device *sdev;
150 if (!scsi_is_sdev_device(dev))
153 if (!get_device(dev))
156 sdev = to_scsi_device(dev);
158 if (device_handler_match(scsi_dh, sdev))
159 scsi_dh_handler_attach(sdev, scsi_dh);
167 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
169 static int scsi_dh_notifier_remove(struct device *dev, void *data)
171 struct scsi_device_handler *scsi_dh = data;
172 struct scsi_device *sdev;
174 if (!scsi_is_sdev_device(dev))
177 if (!get_device(dev))
180 sdev = to_scsi_device(dev);
182 scsi_dh_handler_detach(sdev, scsi_dh);
190 * scsi_register_device_handler - register a device handler personality
192 * @scsi_dh - device handler to be registered.
194 * Returns 0 on success, -EBUSY if handler already registered.
196 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
198 if (get_device_handler(scsi_dh->name))
201 spin_lock(&list_lock);
202 list_add(&scsi_dh->list, &scsi_dh_list);
203 spin_unlock(&list_lock);
204 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
205 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
209 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
212 * scsi_unregister_device_handler - register a device handler personality
214 * @scsi_dh - device handler to be unregistered.
216 * Returns 0 on success, -ENODEV if handler not registered.
218 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
220 if (!get_device_handler(scsi_dh->name))
223 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
224 scsi_dh_notifier_remove);
226 spin_lock(&list_lock);
227 list_del(&scsi_dh->list);
228 spin_unlock(&list_lock);
229 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
233 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
236 * scsi_dh_activate - activate the path associated with the scsi_device
237 * corresponding to the given request queue.
238 * @q - Request queue that is associated with the scsi_device to be
241 int scsi_dh_activate(struct request_queue *q)
245 struct scsi_device *sdev;
246 struct scsi_device_handler *scsi_dh = NULL;
248 spin_lock_irqsave(q->queue_lock, flags);
250 if (sdev && sdev->scsi_dh_data)
251 scsi_dh = sdev->scsi_dh_data->scsi_dh;
252 if (!scsi_dh || !get_device(&sdev->sdev_gendev))
254 spin_unlock_irqrestore(q->queue_lock, flags);
259 if (scsi_dh->activate)
260 err = scsi_dh->activate(sdev);
261 put_device(&sdev->sdev_gendev);
264 EXPORT_SYMBOL_GPL(scsi_dh_activate);
267 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
268 * the given name. FALSE(0) otherwise.
269 * @name - name of the device handler.
271 int scsi_dh_handler_exist(const char *name)
273 return (get_device_handler(name) != NULL);
275 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
277 static struct notifier_block scsi_dh_nb = {
278 .notifier_call = scsi_dh_notifier
281 static int __init scsi_dh_init(void)
285 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
290 static void __exit scsi_dh_exit(void)
292 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
295 module_init(scsi_dh_init);
296 module_exit(scsi_dh_exit);
298 MODULE_DESCRIPTION("SCSI device handler");
299 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
300 MODULE_LICENSE("GPL");