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 *get_device_handler(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);
48 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
54 static struct scsi_device_handler *
55 device_handler_match_function(struct scsi_device *sdev)
57 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
59 spin_lock(&list_lock);
60 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
61 if (tmp_dh->match && tmp_dh->match(sdev)) {
66 spin_unlock(&list_lock);
71 * device_handler_match - Attach a device handler to a device
72 * @scsi_dh - The device handler to match against or NULL
73 * @sdev - SCSI device to be tested against @scsi_dh
75 * Tests @sdev against the device handler @scsi_dh or against
76 * all registered device_handler if @scsi_dh == NULL.
77 * Returns the found device handler or NULL if not found.
79 static struct scsi_device_handler *
80 device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
83 struct scsi_device_handler *found_dh;
85 found_dh = device_handler_match_function(sdev);
87 if (scsi_dh && found_dh != scsi_dh)
94 * scsi_dh_handler_attach - Attach a device handler to a device
95 * @sdev - SCSI device the device handler should attach to
96 * @scsi_dh - The device handler to attach
98 static int scsi_dh_handler_attach(struct scsi_device *sdev,
99 struct scsi_device_handler *scsi_dh)
101 struct scsi_dh_data *d;
103 if (sdev->scsi_dh_data) {
104 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
107 kref_get(&sdev->scsi_dh_data->kref);
111 if (!try_module_get(scsi_dh->module))
114 d = scsi_dh->attach(sdev);
116 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
117 scsi_dh->name, PTR_ERR(d));
118 module_put(scsi_dh->module);
122 d->scsi_dh = scsi_dh;
126 spin_lock_irq(sdev->request_queue->queue_lock);
127 sdev->scsi_dh_data = d;
128 spin_unlock_irq(sdev->request_queue->queue_lock);
132 static void __detach_handler (struct kref *kref)
134 struct scsi_dh_data *scsi_dh_data =
135 container_of(kref, struct scsi_dh_data, kref);
136 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
137 struct scsi_device *sdev = scsi_dh_data->sdev;
139 spin_lock_irq(sdev->request_queue->queue_lock);
140 sdev->scsi_dh_data = NULL;
141 spin_unlock_irq(sdev->request_queue->queue_lock);
143 scsi_dh->detach(sdev);
144 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
145 module_put(scsi_dh->module);
149 * scsi_dh_handler_detach - Detach a device handler from a device
150 * @sdev - SCSI device the device handler should be detached from
151 * @scsi_dh - Device handler to be detached
153 * Detach from a device handler. If a device handler is specified,
154 * only detach if the currently attached handler matches @scsi_dh.
156 static void scsi_dh_handler_detach(struct scsi_device *sdev,
157 struct scsi_device_handler *scsi_dh)
159 if (!sdev->scsi_dh_data)
162 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
166 scsi_dh = sdev->scsi_dh_data->scsi_dh;
169 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
173 * Functions for sysfs attribute 'dh_state'
176 store_dh_state(struct device *dev, struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct scsi_device *sdev = to_scsi_device(dev);
180 struct scsi_device_handler *scsi_dh;
183 if (sdev->sdev_state == SDEV_CANCEL ||
184 sdev->sdev_state == SDEV_DEL)
187 if (!sdev->scsi_dh_data) {
189 * Attach to a device handler
191 if (!(scsi_dh = get_device_handler(buf)))
193 err = scsi_dh_handler_attach(sdev, scsi_dh);
195 scsi_dh = sdev->scsi_dh_data->scsi_dh;
196 if (!strncmp(buf, "detach", 6)) {
198 * Detach from a device handler
200 scsi_dh_handler_detach(sdev, scsi_dh);
202 } else if (!strncmp(buf, "activate", 8)) {
204 * Activate a device handler
206 if (scsi_dh->activate)
207 err = scsi_dh->activate(sdev, NULL, NULL);
213 return err<0?err:count;
217 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
219 struct scsi_device *sdev = to_scsi_device(dev);
221 if (!sdev->scsi_dh_data)
222 return snprintf(buf, 20, "detached\n");
224 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
227 static struct device_attribute scsi_dh_state_attr =
228 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
232 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
234 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
236 struct scsi_device *sdev;
239 if (!scsi_is_sdev_device(dev))
242 sdev = to_scsi_device(dev);
244 err = device_create_file(&sdev->sdev_gendev,
245 &scsi_dh_state_attr);
251 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
253 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
255 struct scsi_device *sdev;
257 if (!scsi_is_sdev_device(dev))
260 sdev = to_scsi_device(dev);
262 device_remove_file(&sdev->sdev_gendev,
263 &scsi_dh_state_attr);
269 * scsi_dh_notifier - notifier chain callback
271 static int scsi_dh_notifier(struct notifier_block *nb,
272 unsigned long action, void *data)
274 struct device *dev = data;
275 struct scsi_device *sdev;
277 struct scsi_device_handler *devinfo = NULL;
279 if (!scsi_is_sdev_device(dev))
282 sdev = to_scsi_device(dev);
284 if (action == BUS_NOTIFY_ADD_DEVICE) {
285 err = device_create_file(dev, &scsi_dh_state_attr);
286 /* don't care about err */
287 devinfo = device_handler_match(NULL, sdev);
289 err = scsi_dh_handler_attach(sdev, devinfo);
290 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
291 device_remove_file(dev, &scsi_dh_state_attr);
292 scsi_dh_handler_detach(sdev, NULL);
298 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
300 static int scsi_dh_notifier_add(struct device *dev, void *data)
302 struct scsi_device_handler *scsi_dh = data;
303 struct scsi_device *sdev;
305 if (!scsi_is_sdev_device(dev))
308 if (!get_device(dev))
311 sdev = to_scsi_device(dev);
313 if (device_handler_match(scsi_dh, sdev))
314 scsi_dh_handler_attach(sdev, scsi_dh);
322 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
324 static int scsi_dh_notifier_remove(struct device *dev, void *data)
326 struct scsi_device_handler *scsi_dh = data;
327 struct scsi_device *sdev;
329 if (!scsi_is_sdev_device(dev))
332 if (!get_device(dev))
335 sdev = to_scsi_device(dev);
337 scsi_dh_handler_detach(sdev, scsi_dh);
345 * scsi_register_device_handler - register a device handler personality
347 * @scsi_dh - device handler to be registered.
349 * Returns 0 on success, -EBUSY if handler already registered.
351 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
354 if (get_device_handler(scsi_dh->name))
357 if (!scsi_dh->attach || !scsi_dh->detach)
360 spin_lock(&list_lock);
361 list_add(&scsi_dh->list, &scsi_dh_list);
362 spin_unlock(&list_lock);
364 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
365 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
369 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
372 * scsi_unregister_device_handler - register a device handler personality
374 * @scsi_dh - device handler to be unregistered.
376 * Returns 0 on success, -ENODEV if handler not registered.
378 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
381 if (!get_device_handler(scsi_dh->name))
384 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
385 scsi_dh_notifier_remove);
387 spin_lock(&list_lock);
388 list_del(&scsi_dh->list);
389 spin_unlock(&list_lock);
390 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
394 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
397 * scsi_dh_activate - activate the path associated with the scsi_device
398 * corresponding to the given request queue.
399 * Returns immediately without waiting for activation to be completed.
400 * @q - Request queue that is associated with the scsi_device to be
402 * @fn - Function to be called upon completion of the activation.
403 * Function fn is called with data (below) and the error code.
404 * Function fn may be called from the same calling context. So,
405 * do not hold the lock in the caller which may be needed in fn.
406 * @data - data passed to the function fn upon completion.
409 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
413 struct scsi_device *sdev;
414 struct scsi_device_handler *scsi_dh = NULL;
415 struct device *dev = NULL;
417 spin_lock_irqsave(q->queue_lock, flags);
420 spin_unlock_irqrestore(q->queue_lock, flags);
427 if (sdev->scsi_dh_data)
428 scsi_dh = sdev->scsi_dh_data->scsi_dh;
429 dev = get_device(&sdev->sdev_gendev);
430 if (!scsi_dh || !dev ||
431 sdev->sdev_state == SDEV_CANCEL ||
432 sdev->sdev_state == SDEV_DEL)
434 if (sdev->sdev_state == SDEV_OFFLINE)
435 err = SCSI_DH_DEV_OFFLINED;
436 spin_unlock_irqrestore(q->queue_lock, flags);
444 if (scsi_dh->activate)
445 err = scsi_dh->activate(sdev, fn, data);
450 EXPORT_SYMBOL_GPL(scsi_dh_activate);
453 * scsi_dh_set_params - set the parameters for the device as per the
454 * string specified in params.
455 * @q - Request queue that is associated with the scsi_device for
456 * which the parameters to be set.
457 * @params - parameters in the following format
458 * "no_of_params\0param1\0param2\0param3\0...\0"
459 * for example, string for 2 parameters with value 10 and 21
460 * is specified as "2\010\021\0".
462 int scsi_dh_set_params(struct request_queue *q, const char *params)
464 int err = -SCSI_DH_NOSYS;
466 struct scsi_device *sdev;
467 struct scsi_device_handler *scsi_dh = NULL;
469 spin_lock_irqsave(q->queue_lock, flags);
471 if (sdev && sdev->scsi_dh_data)
472 scsi_dh = sdev->scsi_dh_data->scsi_dh;
473 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
475 spin_unlock_irqrestore(q->queue_lock, flags);
479 err = scsi_dh->set_params(sdev, params);
480 put_device(&sdev->sdev_gendev);
483 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
486 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
487 * the given name. FALSE(0) otherwise.
488 * @name - name of the device handler.
490 int scsi_dh_handler_exist(const char *name)
492 return (get_device_handler(name) != NULL);
494 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
497 * scsi_dh_attach - Attach device handler
498 * @q - Request queue that is associated with the scsi_device
499 * the handler should be attached to
500 * @name - name of the handler to attach
502 int scsi_dh_attach(struct request_queue *q, const char *name)
505 struct scsi_device *sdev;
506 struct scsi_device_handler *scsi_dh;
509 scsi_dh = get_device_handler(name);
513 spin_lock_irqsave(q->queue_lock, flags);
515 if (!sdev || !get_device(&sdev->sdev_gendev))
517 spin_unlock_irqrestore(q->queue_lock, flags);
520 err = scsi_dh_handler_attach(sdev, scsi_dh);
521 put_device(&sdev->sdev_gendev);
525 EXPORT_SYMBOL_GPL(scsi_dh_attach);
528 * scsi_dh_detach - Detach device handler
529 * @q - Request queue that is associated with the scsi_device
530 * the handler should be detached from
532 * This function will detach the device handler only
533 * if the sdev is not part of the internal list, ie
534 * if it has been attached manually.
536 void scsi_dh_detach(struct request_queue *q)
539 struct scsi_device *sdev;
540 struct scsi_device_handler *scsi_dh = NULL;
542 spin_lock_irqsave(q->queue_lock, flags);
544 if (!sdev || !get_device(&sdev->sdev_gendev))
546 spin_unlock_irqrestore(q->queue_lock, flags);
551 if (sdev->scsi_dh_data) {
552 scsi_dh = sdev->scsi_dh_data->scsi_dh;
553 scsi_dh_handler_detach(sdev, scsi_dh);
555 put_device(&sdev->sdev_gendev);
557 EXPORT_SYMBOL_GPL(scsi_dh_detach);
560 * scsi_dh_attached_handler_name - Get attached device handler's name
561 * @q - Request queue that is associated with the scsi_device
562 * that may have a device handler attached
563 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
565 * Returns name of attached handler, NULL if no handler is attached.
566 * Caller must take care to free the returned string.
568 const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
571 struct scsi_device *sdev;
572 const char *handler_name = NULL;
574 spin_lock_irqsave(q->queue_lock, flags);
576 if (!sdev || !get_device(&sdev->sdev_gendev))
578 spin_unlock_irqrestore(q->queue_lock, flags);
583 if (sdev->scsi_dh_data)
584 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
586 put_device(&sdev->sdev_gendev);
589 EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
591 static struct notifier_block scsi_dh_nb = {
592 .notifier_call = scsi_dh_notifier
595 static int __init scsi_dh_init(void)
599 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
602 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
603 scsi_dh_sysfs_attr_add);
608 static void __exit scsi_dh_exit(void)
610 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
611 scsi_dh_sysfs_attr_remove);
612 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
615 module_init(scsi_dh_init);
616 module_exit(scsi_dh_exit);
618 MODULE_DESCRIPTION("SCSI device handler");
619 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
620 MODULE_LICENSE("GPL");