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 matches @scsi_dh.
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 * Functions for sysfs attribute 'dh_state'
109 store_dh_state(struct device *dev, struct device_attribute *attr,
110 const char *buf, size_t count)
112 struct scsi_device *sdev = to_scsi_device(dev);
113 struct scsi_device_handler *scsi_dh;
116 if (!sdev->scsi_dh_data) {
118 * Attach to a device handler
120 if (!(scsi_dh = get_device_handler(buf)))
122 err = scsi_dh_handler_attach(sdev, scsi_dh);
124 scsi_dh = sdev->scsi_dh_data->scsi_dh;
125 if (!strncmp(buf, "detach", 6)) {
127 * Detach from a device handler
129 scsi_dh_handler_detach(sdev, scsi_dh);
131 } else if (!strncmp(buf, "activate", 8)) {
133 * Activate a device handler
135 if (scsi_dh->activate)
136 err = scsi_dh->activate(sdev);
142 return err<0?err:count;
146 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
148 struct scsi_device *sdev = to_scsi_device(dev);
150 if (!sdev->scsi_dh_data)
151 return snprintf(buf, 20, "detached\n");
153 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
156 static struct device_attribute scsi_dh_state_attr =
157 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
161 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
163 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
165 struct scsi_device *sdev;
168 if (!scsi_is_sdev_device(dev))
171 sdev = to_scsi_device(dev);
173 err = device_create_file(&sdev->sdev_gendev,
174 &scsi_dh_state_attr);
180 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
182 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
184 struct scsi_device *sdev;
186 if (!scsi_is_sdev_device(dev))
189 sdev = to_scsi_device(dev);
191 device_remove_file(&sdev->sdev_gendev,
192 &scsi_dh_state_attr);
198 * scsi_dh_notifier - notifier chain callback
200 static int scsi_dh_notifier(struct notifier_block *nb,
201 unsigned long action, void *data)
203 struct device *dev = data;
204 struct scsi_device *sdev;
206 struct scsi_device_handler *tmp, *devinfo = NULL;
208 if (!scsi_is_sdev_device(dev))
211 sdev = to_scsi_device(dev);
213 spin_lock(&list_lock);
214 list_for_each_entry(tmp, &scsi_dh_list, list) {
215 if (device_handler_match(tmp, sdev)) {
220 spin_unlock(&list_lock);
225 if (action == BUS_NOTIFY_ADD_DEVICE) {
226 err = scsi_dh_handler_attach(sdev, devinfo);
228 err = device_create_file(dev, &scsi_dh_state_attr);
229 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
230 device_remove_file(dev, &scsi_dh_state_attr);
231 scsi_dh_handler_detach(sdev, NULL);
238 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
240 static int scsi_dh_notifier_add(struct device *dev, void *data)
242 struct scsi_device_handler *scsi_dh = data;
243 struct scsi_device *sdev;
245 if (!scsi_is_sdev_device(dev))
248 if (!get_device(dev))
251 sdev = to_scsi_device(dev);
253 if (device_handler_match(scsi_dh, sdev))
254 scsi_dh_handler_attach(sdev, scsi_dh);
262 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
264 static int scsi_dh_notifier_remove(struct device *dev, void *data)
266 struct scsi_device_handler *scsi_dh = data;
267 struct scsi_device *sdev;
269 if (!scsi_is_sdev_device(dev))
272 if (!get_device(dev))
275 sdev = to_scsi_device(dev);
277 scsi_dh_handler_detach(sdev, scsi_dh);
285 * scsi_register_device_handler - register a device handler personality
287 * @scsi_dh - device handler to be registered.
289 * Returns 0 on success, -EBUSY if handler already registered.
291 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
293 if (get_device_handler(scsi_dh->name))
296 spin_lock(&list_lock);
297 list_add(&scsi_dh->list, &scsi_dh_list);
298 spin_unlock(&list_lock);
299 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
300 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
304 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
307 * scsi_unregister_device_handler - register a device handler personality
309 * @scsi_dh - device handler to be unregistered.
311 * Returns 0 on success, -ENODEV if handler not registered.
313 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
315 if (!get_device_handler(scsi_dh->name))
318 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
319 scsi_dh_notifier_remove);
321 spin_lock(&list_lock);
322 list_del(&scsi_dh->list);
323 spin_unlock(&list_lock);
324 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
328 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
331 * scsi_dh_activate - activate the path associated with the scsi_device
332 * corresponding to the given request queue.
333 * @q - Request queue that is associated with the scsi_device to be
336 int scsi_dh_activate(struct request_queue *q)
340 struct scsi_device *sdev;
341 struct scsi_device_handler *scsi_dh = NULL;
343 spin_lock_irqsave(q->queue_lock, flags);
345 if (sdev && sdev->scsi_dh_data)
346 scsi_dh = sdev->scsi_dh_data->scsi_dh;
347 if (!scsi_dh || !get_device(&sdev->sdev_gendev))
349 spin_unlock_irqrestore(q->queue_lock, flags);
354 if (scsi_dh->activate)
355 err = scsi_dh->activate(sdev);
356 put_device(&sdev->sdev_gendev);
359 EXPORT_SYMBOL_GPL(scsi_dh_activate);
362 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
363 * the given name. FALSE(0) otherwise.
364 * @name - name of the device handler.
366 int scsi_dh_handler_exist(const char *name)
368 return (get_device_handler(name) != NULL);
370 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
373 * scsi_dh_handler_attach - Attach device handler
374 * @sdev - sdev the handler should be attached to
375 * @name - name of the handler to attach
377 int scsi_dh_attach(struct request_queue *q, const char *name)
380 struct scsi_device *sdev;
381 struct scsi_device_handler *scsi_dh;
384 scsi_dh = get_device_handler(name);
388 spin_lock_irqsave(q->queue_lock, flags);
390 if (!sdev || !get_device(&sdev->sdev_gendev))
392 spin_unlock_irqrestore(q->queue_lock, flags);
395 err = scsi_dh_handler_attach(sdev, scsi_dh);
397 put_device(&sdev->sdev_gendev);
401 EXPORT_SYMBOL_GPL(scsi_dh_attach);
404 * scsi_dh_handler_detach - Detach device handler
405 * @sdev - sdev the handler should be detached from
407 * This function will detach the device handler only
408 * if the sdev is not part of the internal list, ie
409 * if it has been attached manually.
411 void scsi_dh_detach(struct request_queue *q)
414 struct scsi_device *sdev;
415 struct scsi_device_handler *scsi_dh = NULL;
417 spin_lock_irqsave(q->queue_lock, flags);
419 if (!sdev || !get_device(&sdev->sdev_gendev))
421 spin_unlock_irqrestore(q->queue_lock, flags);
426 if (sdev->scsi_dh_data) {
427 /* if sdev is not on internal list, detach */
428 scsi_dh = sdev->scsi_dh_data->scsi_dh;
429 if (!device_handler_match(scsi_dh, sdev))
430 scsi_dh_handler_detach(sdev, scsi_dh);
432 put_device(&sdev->sdev_gendev);
434 EXPORT_SYMBOL_GPL(scsi_dh_detach);
436 static struct notifier_block scsi_dh_nb = {
437 .notifier_call = scsi_dh_notifier
440 static int __init scsi_dh_init(void)
444 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
447 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
448 scsi_dh_sysfs_attr_add);
453 static void __exit scsi_dh_exit(void)
455 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
456 scsi_dh_sysfs_attr_remove);
457 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
460 module_init(scsi_dh_init);
461 module_exit(scsi_dh_exit);
463 MODULE_DESCRIPTION("SCSI device handler");
464 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
465 MODULE_LICENSE("GPL");