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 <linux/slab.h>
25 #include <linux/module.h>
26 #include <scsi/scsi_dh.h>
27 #include "scsi_priv.h"
29 static DEFINE_SPINLOCK(list_lock);
30 static LIST_HEAD(scsi_dh_list);
32 static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
34 struct scsi_device_handler *tmp, *found = NULL;
36 spin_lock(&list_lock);
37 list_for_each_entry(tmp, &scsi_dh_list, list) {
38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
43 spin_unlock(&list_lock);
47 static struct scsi_device_handler *scsi_dh_lookup(const char *name)
49 struct scsi_device_handler *dh;
51 dh = __scsi_dh_lookup(name);
54 dh = __scsi_dh_lookup(name);
61 * device_handler_match_function - Match a device handler to a device
62 * @sdev - SCSI device to be tested
64 * Tests @sdev against the match function of all registered device_handler.
65 * Returns the found device handler or NULL if not found.
67 static struct scsi_device_handler *
68 device_handler_match_function(struct scsi_device *sdev)
70 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
72 spin_lock(&list_lock);
73 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
74 if (tmp_dh->match && tmp_dh->match(sdev)) {
79 spin_unlock(&list_lock);
84 * device_handler_match - Attach a device handler to a device
85 * @scsi_dh - The device handler to match against or NULL
86 * @sdev - SCSI device to be tested against @scsi_dh
88 * Tests @sdev against the device handler @scsi_dh or against
89 * all registered device_handler if @scsi_dh == NULL.
90 * Returns the found device handler or NULL if not found.
92 static struct scsi_device_handler *
93 device_handler_match(struct scsi_device_handler *scsi_dh,
94 struct scsi_device *sdev)
96 struct scsi_device_handler *found_dh;
98 found_dh = device_handler_match_function(sdev);
100 if (scsi_dh && found_dh != scsi_dh)
107 * scsi_dh_handler_attach - Attach a device handler to a device
108 * @sdev - SCSI device the device handler should attach to
109 * @scsi_dh - The device handler to attach
111 static int scsi_dh_handler_attach(struct scsi_device *sdev,
112 struct scsi_device_handler *scsi_dh)
114 struct scsi_dh_data *d;
116 if (!try_module_get(scsi_dh->module))
119 d = scsi_dh->attach(sdev);
121 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
122 scsi_dh->name, PTR_ERR(d));
123 module_put(scsi_dh->module);
127 d->scsi_dh = scsi_dh;
130 spin_lock_irq(sdev->request_queue->queue_lock);
131 sdev->scsi_dh_data = d;
132 spin_unlock_irq(sdev->request_queue->queue_lock);
137 * scsi_dh_handler_detach - Detach a device handler from a device
138 * @sdev - SCSI device the device handler should be detached from
140 static void scsi_dh_handler_detach(struct scsi_device *sdev)
142 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
143 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
145 scsi_dh->detach(sdev);
147 spin_lock_irq(sdev->request_queue->queue_lock);
148 sdev->scsi_dh_data = NULL;
149 spin_unlock_irq(sdev->request_queue->queue_lock);
151 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
152 module_put(scsi_dh->module);
156 * Functions for sysfs attribute 'dh_state'
159 store_dh_state(struct device *dev, struct device_attribute *attr,
160 const char *buf, size_t count)
162 struct scsi_device *sdev = to_scsi_device(dev);
163 struct scsi_device_handler *scsi_dh;
166 if (sdev->sdev_state == SDEV_CANCEL ||
167 sdev->sdev_state == SDEV_DEL)
170 if (!sdev->scsi_dh_data) {
172 * Attach to a device handler
174 scsi_dh = scsi_dh_lookup(buf);
177 err = scsi_dh_handler_attach(sdev, scsi_dh);
179 scsi_dh = sdev->scsi_dh_data->scsi_dh;
180 if (!strncmp(buf, "detach", 6)) {
182 * Detach from a device handler
184 scsi_dh_handler_detach(sdev);
186 } else if (!strncmp(buf, "activate", 8)) {
188 * Activate a device handler
190 if (scsi_dh->activate)
191 err = scsi_dh->activate(sdev, NULL, NULL);
197 return err<0?err:count;
201 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
203 struct scsi_device *sdev = to_scsi_device(dev);
205 if (!sdev->scsi_dh_data)
206 return snprintf(buf, 20, "detached\n");
208 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
211 static struct device_attribute scsi_dh_state_attr =
212 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
216 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
218 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
220 struct scsi_device *sdev;
223 if (!scsi_is_sdev_device(dev))
226 sdev = to_scsi_device(dev);
228 err = device_create_file(&sdev->sdev_gendev,
229 &scsi_dh_state_attr);
235 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
237 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
239 struct scsi_device *sdev;
241 if (!scsi_is_sdev_device(dev))
244 sdev = to_scsi_device(dev);
246 device_remove_file(&sdev->sdev_gendev,
247 &scsi_dh_state_attr);
253 * scsi_dh_notifier - notifier chain callback
255 static int scsi_dh_notifier(struct notifier_block *nb,
256 unsigned long action, void *data)
258 struct device *dev = data;
259 struct scsi_device *sdev;
261 struct scsi_device_handler *devinfo = NULL;
263 if (!scsi_is_sdev_device(dev))
266 sdev = to_scsi_device(dev);
268 if (action == BUS_NOTIFY_ADD_DEVICE) {
269 err = device_create_file(dev, &scsi_dh_state_attr);
270 /* don't care about err */
271 devinfo = device_handler_match(NULL, sdev);
273 err = scsi_dh_handler_attach(sdev, devinfo);
274 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
275 device_remove_file(dev, &scsi_dh_state_attr);
276 if (sdev->scsi_dh_data)
277 scsi_dh_handler_detach(sdev);
283 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
285 static int scsi_dh_notifier_add(struct device *dev, void *data)
287 struct scsi_device_handler *scsi_dh = data;
288 struct scsi_device *sdev;
290 if (!scsi_is_sdev_device(dev))
293 if (!get_device(dev))
296 sdev = to_scsi_device(dev);
298 if (device_handler_match(scsi_dh, sdev))
299 scsi_dh_handler_attach(sdev, scsi_dh);
307 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
309 static int scsi_dh_notifier_remove(struct device *dev, void *data)
311 struct scsi_device_handler *scsi_dh = data;
312 struct scsi_device *sdev;
314 if (!scsi_is_sdev_device(dev))
317 if (!get_device(dev))
320 sdev = to_scsi_device(dev);
322 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh == scsi_dh)
323 scsi_dh_handler_detach(sdev);
331 * scsi_register_device_handler - register a device handler personality
333 * @scsi_dh - device handler to be registered.
335 * Returns 0 on success, -EBUSY if handler already registered.
337 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
339 if (__scsi_dh_lookup(scsi_dh->name))
342 if (!scsi_dh->attach || !scsi_dh->detach)
345 spin_lock(&list_lock);
346 list_add(&scsi_dh->list, &scsi_dh_list);
347 spin_unlock(&list_lock);
349 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
350 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
354 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
357 * scsi_unregister_device_handler - register a device handler personality
359 * @scsi_dh - device handler to be unregistered.
361 * Returns 0 on success, -ENODEV if handler not registered.
363 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
366 if (!__scsi_dh_lookup(scsi_dh->name))
369 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
370 scsi_dh_notifier_remove);
372 spin_lock(&list_lock);
373 list_del(&scsi_dh->list);
374 spin_unlock(&list_lock);
375 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
379 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
382 * scsi_dh_activate - activate the path associated with the scsi_device
383 * corresponding to the given request queue.
384 * Returns immediately without waiting for activation to be completed.
385 * @q - Request queue that is associated with the scsi_device to be
387 * @fn - Function to be called upon completion of the activation.
388 * Function fn is called with data (below) and the error code.
389 * Function fn may be called from the same calling context. So,
390 * do not hold the lock in the caller which may be needed in fn.
391 * @data - data passed to the function fn upon completion.
394 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
398 struct scsi_device *sdev;
399 struct scsi_device_handler *scsi_dh = NULL;
400 struct device *dev = NULL;
402 spin_lock_irqsave(q->queue_lock, flags);
405 spin_unlock_irqrestore(q->queue_lock, flags);
412 if (sdev->scsi_dh_data)
413 scsi_dh = sdev->scsi_dh_data->scsi_dh;
414 dev = get_device(&sdev->sdev_gendev);
415 if (!scsi_dh || !dev ||
416 sdev->sdev_state == SDEV_CANCEL ||
417 sdev->sdev_state == SDEV_DEL)
419 if (sdev->sdev_state == SDEV_OFFLINE)
420 err = SCSI_DH_DEV_OFFLINED;
421 spin_unlock_irqrestore(q->queue_lock, flags);
429 if (scsi_dh->activate)
430 err = scsi_dh->activate(sdev, fn, data);
435 EXPORT_SYMBOL_GPL(scsi_dh_activate);
438 * scsi_dh_set_params - set the parameters for the device as per the
439 * string specified in params.
440 * @q - Request queue that is associated with the scsi_device for
441 * which the parameters to be set.
442 * @params - parameters in the following format
443 * "no_of_params\0param1\0param2\0param3\0...\0"
444 * for example, string for 2 parameters with value 10 and 21
445 * is specified as "2\010\021\0".
447 int scsi_dh_set_params(struct request_queue *q, const char *params)
449 int err = -SCSI_DH_NOSYS;
451 struct scsi_device *sdev;
452 struct scsi_device_handler *scsi_dh = NULL;
454 spin_lock_irqsave(q->queue_lock, flags);
456 if (sdev && sdev->scsi_dh_data)
457 scsi_dh = sdev->scsi_dh_data->scsi_dh;
458 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
460 spin_unlock_irqrestore(q->queue_lock, flags);
464 err = scsi_dh->set_params(sdev, params);
465 put_device(&sdev->sdev_gendev);
468 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
471 * scsi_dh_attach - Attach device handler
472 * @q - Request queue that is associated with the scsi_device
473 * the handler should be attached to
474 * @name - name of the handler to attach
476 int scsi_dh_attach(struct request_queue *q, const char *name)
479 struct scsi_device *sdev;
480 struct scsi_device_handler *scsi_dh;
483 scsi_dh = scsi_dh_lookup(name);
487 spin_lock_irqsave(q->queue_lock, flags);
489 if (!sdev || !get_device(&sdev->sdev_gendev))
491 spin_unlock_irqrestore(q->queue_lock, flags);
496 if (sdev->scsi_dh_data) {
497 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
502 err = scsi_dh_handler_attach(sdev, scsi_dh);
505 put_device(&sdev->sdev_gendev);
508 EXPORT_SYMBOL_GPL(scsi_dh_attach);
511 * scsi_dh_attached_handler_name - Get attached device handler's name
512 * @q - Request queue that is associated with the scsi_device
513 * that may have a device handler attached
514 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
516 * Returns name of attached handler, NULL if no handler is attached.
517 * Caller must take care to free the returned string.
519 const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
522 struct scsi_device *sdev;
523 const char *handler_name = NULL;
525 spin_lock_irqsave(q->queue_lock, flags);
527 if (!sdev || !get_device(&sdev->sdev_gendev))
529 spin_unlock_irqrestore(q->queue_lock, flags);
534 if (sdev->scsi_dh_data)
535 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
537 put_device(&sdev->sdev_gendev);
540 EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
542 static struct notifier_block scsi_dh_nb = {
543 .notifier_call = scsi_dh_notifier
546 static int __init scsi_dh_init(void)
550 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
553 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
554 scsi_dh_sysfs_attr_add);
559 static void __exit scsi_dh_exit(void)
561 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
562 scsi_dh_sysfs_attr_remove);
563 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
566 module_init(scsi_dh_init);
567 module_exit(scsi_dh_exit);
569 MODULE_DESCRIPTION("SCSI device handler");
570 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
571 MODULE_LICENSE("GPL");