scsi_dh: check queuedata pointer before proceeding further
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / device_handler / scsi_dh.c
blobd973325ded2a047db22cf42b1ef6ba15d7f5c412
1 /*
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
19 * Authors:
20 * Chandra Seetharaman <sekharan@us.ibm.com>
21 * Mike Anderson <andmike@linux.vnet.ibm.com>
24 #include <linux/slab.h>
25 #include <scsi/scsi_dh.h>
26 #include "../scsi_priv.h"
28 static DEFINE_SPINLOCK(list_lock);
29 static LIST_HEAD(scsi_dh_list);
30 static int scsi_dh_list_idx = 1;
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))) {
39 found = tmp;
40 break;
43 spin_unlock(&list_lock);
44 return found;
47 static struct scsi_device_handler *get_device_handler_by_idx(int idx)
49 struct scsi_device_handler *tmp, *found = NULL;
51 spin_lock(&list_lock);
52 list_for_each_entry(tmp, &scsi_dh_list, list) {
53 if (tmp->idx == idx) {
54 found = tmp;
55 break;
58 spin_unlock(&list_lock);
59 return found;
63 * device_handler_match - Attach a device handler to a device
64 * @scsi_dh - The device handler to match against or NULL
65 * @sdev - SCSI device to be tested against @scsi_dh
67 * Tests @sdev against the device handler @scsi_dh or against
68 * all registered device_handler if @scsi_dh == NULL.
69 * Returns the found device handler or NULL if not found.
71 static struct scsi_device_handler *
72 device_handler_match(struct scsi_device_handler *scsi_dh,
73 struct scsi_device *sdev)
75 struct scsi_device_handler *found_dh = NULL;
76 int idx;
78 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
79 SCSI_DEVINFO_DH);
80 found_dh = get_device_handler_by_idx(idx);
82 if (scsi_dh && found_dh != scsi_dh)
83 found_dh = NULL;
85 return found_dh;
89 * scsi_dh_handler_attach - Attach a device handler to a device
90 * @sdev - SCSI device the device handler should attach to
91 * @scsi_dh - The device handler to attach
93 static int scsi_dh_handler_attach(struct scsi_device *sdev,
94 struct scsi_device_handler *scsi_dh)
96 int err = 0;
98 if (sdev->scsi_dh_data) {
99 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
100 err = -EBUSY;
101 else
102 kref_get(&sdev->scsi_dh_data->kref);
103 } else if (scsi_dh->attach) {
104 err = scsi_dh->attach(sdev);
105 if (!err) {
106 kref_init(&sdev->scsi_dh_data->kref);
107 sdev->scsi_dh_data->sdev = sdev;
110 return err;
113 static void __detach_handler (struct kref *kref)
115 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
116 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
120 * scsi_dh_handler_detach - Detach a device handler from a device
121 * @sdev - SCSI device the device handler should be detached from
122 * @scsi_dh - Device handler to be detached
124 * Detach from a device handler. If a device handler is specified,
125 * only detach if the currently attached handler matches @scsi_dh.
127 static void scsi_dh_handler_detach(struct scsi_device *sdev,
128 struct scsi_device_handler *scsi_dh)
130 if (!sdev->scsi_dh_data)
131 return;
133 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
134 return;
136 if (!scsi_dh)
137 scsi_dh = sdev->scsi_dh_data->scsi_dh;
139 if (scsi_dh && scsi_dh->detach)
140 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
144 * Functions for sysfs attribute 'dh_state'
146 static ssize_t
147 store_dh_state(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
150 struct scsi_device *sdev = to_scsi_device(dev);
151 struct scsi_device_handler *scsi_dh;
152 int err = -EINVAL;
154 if (!sdev->scsi_dh_data) {
156 * Attach to a device handler
158 if (!(scsi_dh = get_device_handler(buf)))
159 return err;
160 err = scsi_dh_handler_attach(sdev, scsi_dh);
161 } else {
162 scsi_dh = sdev->scsi_dh_data->scsi_dh;
163 if (!strncmp(buf, "detach", 6)) {
165 * Detach from a device handler
167 scsi_dh_handler_detach(sdev, scsi_dh);
168 err = 0;
169 } else if (!strncmp(buf, "activate", 8)) {
171 * Activate a device handler
173 if (scsi_dh->activate)
174 err = scsi_dh->activate(sdev, NULL, NULL);
175 else
176 err = 0;
180 return err<0?err:count;
183 static ssize_t
184 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
186 struct scsi_device *sdev = to_scsi_device(dev);
188 if (!sdev->scsi_dh_data)
189 return snprintf(buf, 20, "detached\n");
191 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
194 static struct device_attribute scsi_dh_state_attr =
195 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
196 store_dh_state);
199 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
201 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
203 struct scsi_device *sdev;
204 int err;
206 if (!scsi_is_sdev_device(dev))
207 return 0;
209 sdev = to_scsi_device(dev);
211 err = device_create_file(&sdev->sdev_gendev,
212 &scsi_dh_state_attr);
214 return 0;
218 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
220 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
222 struct scsi_device *sdev;
224 if (!scsi_is_sdev_device(dev))
225 return 0;
227 sdev = to_scsi_device(dev);
229 device_remove_file(&sdev->sdev_gendev,
230 &scsi_dh_state_attr);
232 return 0;
236 * scsi_dh_notifier - notifier chain callback
238 static int scsi_dh_notifier(struct notifier_block *nb,
239 unsigned long action, void *data)
241 struct device *dev = data;
242 struct scsi_device *sdev;
243 int err = 0;
244 struct scsi_device_handler *devinfo = NULL;
246 if (!scsi_is_sdev_device(dev))
247 return 0;
249 sdev = to_scsi_device(dev);
251 if (action == BUS_NOTIFY_ADD_DEVICE) {
252 err = device_create_file(dev, &scsi_dh_state_attr);
253 /* don't care about err */
254 devinfo = device_handler_match(NULL, sdev);
255 if (devinfo)
256 err = scsi_dh_handler_attach(sdev, devinfo);
257 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
258 device_remove_file(dev, &scsi_dh_state_attr);
259 scsi_dh_handler_detach(sdev, NULL);
261 return err;
265 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
267 static int scsi_dh_notifier_add(struct device *dev, void *data)
269 struct scsi_device_handler *scsi_dh = data;
270 struct scsi_device *sdev;
272 if (!scsi_is_sdev_device(dev))
273 return 0;
275 if (!get_device(dev))
276 return 0;
278 sdev = to_scsi_device(dev);
280 if (device_handler_match(scsi_dh, sdev))
281 scsi_dh_handler_attach(sdev, scsi_dh);
283 put_device(dev);
285 return 0;
289 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
291 static int scsi_dh_notifier_remove(struct device *dev, void *data)
293 struct scsi_device_handler *scsi_dh = data;
294 struct scsi_device *sdev;
296 if (!scsi_is_sdev_device(dev))
297 return 0;
299 if (!get_device(dev))
300 return 0;
302 sdev = to_scsi_device(dev);
304 scsi_dh_handler_detach(sdev, scsi_dh);
306 put_device(dev);
308 return 0;
312 * scsi_register_device_handler - register a device handler personality
313 * module.
314 * @scsi_dh - device handler to be registered.
316 * Returns 0 on success, -EBUSY if handler already registered.
318 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
320 int i;
322 if (get_device_handler(scsi_dh->name))
323 return -EBUSY;
325 spin_lock(&list_lock);
326 scsi_dh->idx = scsi_dh_list_idx++;
327 list_add(&scsi_dh->list, &scsi_dh_list);
328 spin_unlock(&list_lock);
330 for (i = 0; scsi_dh->devlist[i].vendor; i++) {
331 scsi_dev_info_list_add_keyed(0,
332 scsi_dh->devlist[i].vendor,
333 scsi_dh->devlist[i].model,
334 NULL,
335 scsi_dh->idx,
336 SCSI_DEVINFO_DH);
339 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
340 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
342 return SCSI_DH_OK;
344 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
347 * scsi_unregister_device_handler - register a device handler personality
348 * module.
349 * @scsi_dh - device handler to be unregistered.
351 * Returns 0 on success, -ENODEV if handler not registered.
353 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
355 int i;
357 if (!get_device_handler(scsi_dh->name))
358 return -ENODEV;
360 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
361 scsi_dh_notifier_remove);
363 for (i = 0; scsi_dh->devlist[i].vendor; i++) {
364 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
365 scsi_dh->devlist[i].model,
366 SCSI_DEVINFO_DH);
369 spin_lock(&list_lock);
370 list_del(&scsi_dh->list);
371 spin_unlock(&list_lock);
372 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
374 return SCSI_DH_OK;
376 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
379 * scsi_dh_activate - activate the path associated with the scsi_device
380 * corresponding to the given request queue.
381 * Returns immediately without waiting for activation to be completed.
382 * @q - Request queue that is associated with the scsi_device to be
383 * activated.
384 * @fn - Function to be called upon completion of the activation.
385 * Function fn is called with data (below) and the error code.
386 * Function fn may be called from the same calling context. So,
387 * do not hold the lock in the caller which may be needed in fn.
388 * @data - data passed to the function fn upon completion.
391 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
393 int err = 0;
394 unsigned long flags;
395 struct scsi_device *sdev;
396 struct scsi_device_handler *scsi_dh = NULL;
397 struct device *dev = NULL;
399 spin_lock_irqsave(q->queue_lock, flags);
400 sdev = q->queuedata;
401 if (!sdev) {
402 spin_unlock_irqrestore(q->queue_lock, flags);
403 err = SCSI_DH_NOSYS;
404 if (fn)
405 fn(data, err);
406 return err;
409 if (sdev->scsi_dh_data)
410 scsi_dh = sdev->scsi_dh_data->scsi_dh;
411 dev = get_device(&sdev->sdev_gendev);
412 if (!scsi_dh || !dev ||
413 sdev->sdev_state == SDEV_CANCEL ||
414 sdev->sdev_state == SDEV_DEL)
415 err = SCSI_DH_NOSYS;
416 if (sdev->sdev_state == SDEV_OFFLINE)
417 err = SCSI_DH_DEV_OFFLINED;
418 spin_unlock_irqrestore(q->queue_lock, flags);
420 if (err) {
421 if (fn)
422 fn(data, err);
423 goto out;
426 if (scsi_dh->activate)
427 err = scsi_dh->activate(sdev, fn, data);
428 out:
429 put_device(dev);
430 return err;
432 EXPORT_SYMBOL_GPL(scsi_dh_activate);
435 * scsi_dh_set_params - set the parameters for the device as per the
436 * string specified in params.
437 * @q - Request queue that is associated with the scsi_device for
438 * which the parameters to be set.
439 * @params - parameters in the following format
440 * "no_of_params\0param1\0param2\0param3\0...\0"
441 * for example, string for 2 parameters with value 10 and 21
442 * is specified as "2\010\021\0".
444 int scsi_dh_set_params(struct request_queue *q, const char *params)
446 int err = -SCSI_DH_NOSYS;
447 unsigned long flags;
448 struct scsi_device *sdev;
449 struct scsi_device_handler *scsi_dh = NULL;
451 spin_lock_irqsave(q->queue_lock, flags);
452 sdev = q->queuedata;
453 if (sdev && sdev->scsi_dh_data)
454 scsi_dh = sdev->scsi_dh_data->scsi_dh;
455 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
456 err = 0;
457 spin_unlock_irqrestore(q->queue_lock, flags);
459 if (err)
460 return err;
461 err = scsi_dh->set_params(sdev, params);
462 put_device(&sdev->sdev_gendev);
463 return err;
465 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
468 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
469 * the given name. FALSE(0) otherwise.
470 * @name - name of the device handler.
472 int scsi_dh_handler_exist(const char *name)
474 return (get_device_handler(name) != NULL);
476 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
479 * scsi_dh_handler_attach - Attach device handler
480 * @sdev - sdev the handler should be attached to
481 * @name - name of the handler to attach
483 int scsi_dh_attach(struct request_queue *q, const char *name)
485 unsigned long flags;
486 struct scsi_device *sdev;
487 struct scsi_device_handler *scsi_dh;
488 int err = 0;
490 scsi_dh = get_device_handler(name);
491 if (!scsi_dh)
492 return -EINVAL;
494 spin_lock_irqsave(q->queue_lock, flags);
495 sdev = q->queuedata;
496 if (!sdev || !get_device(&sdev->sdev_gendev))
497 err = -ENODEV;
498 spin_unlock_irqrestore(q->queue_lock, flags);
500 if (!err) {
501 err = scsi_dh_handler_attach(sdev, scsi_dh);
502 put_device(&sdev->sdev_gendev);
504 return err;
506 EXPORT_SYMBOL_GPL(scsi_dh_attach);
509 * scsi_dh_handler_detach - Detach device handler
510 * @sdev - sdev the handler should be detached from
512 * This function will detach the device handler only
513 * if the sdev is not part of the internal list, ie
514 * if it has been attached manually.
516 void scsi_dh_detach(struct request_queue *q)
518 unsigned long flags;
519 struct scsi_device *sdev;
520 struct scsi_device_handler *scsi_dh = NULL;
522 spin_lock_irqsave(q->queue_lock, flags);
523 sdev = q->queuedata;
524 if (!sdev || !get_device(&sdev->sdev_gendev))
525 sdev = NULL;
526 spin_unlock_irqrestore(q->queue_lock, flags);
528 if (!sdev)
529 return;
531 if (sdev->scsi_dh_data) {
532 scsi_dh = sdev->scsi_dh_data->scsi_dh;
533 scsi_dh_handler_detach(sdev, scsi_dh);
535 put_device(&sdev->sdev_gendev);
537 EXPORT_SYMBOL_GPL(scsi_dh_detach);
539 static struct notifier_block scsi_dh_nb = {
540 .notifier_call = scsi_dh_notifier
543 static int __init scsi_dh_init(void)
545 int r;
547 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
548 if (r)
549 return r;
551 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
553 if (!r)
554 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
555 scsi_dh_sysfs_attr_add);
557 return r;
560 static void __exit scsi_dh_exit(void)
562 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
563 scsi_dh_sysfs_attr_remove);
564 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
565 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
568 module_init(scsi_dh_init);
569 module_exit(scsi_dh_exit);
571 MODULE_DESCRIPTION("SCSI device handler");
572 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
573 MODULE_LICENSE("GPL");