include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / scsi / device_handler / scsi_dh.c
blobf4c5d065df3387b152bf4d301bb5781741f5d8b7
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 <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);
31 static int scsi_dh_list_idx = 1;
33 static struct scsi_device_handler *get_device_handler(const char *name)
35 struct scsi_device_handler *tmp, *found = NULL;
37 spin_lock(&list_lock);
38 list_for_each_entry(tmp, &scsi_dh_list, list) {
39 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
40 found = tmp;
41 break;
44 spin_unlock(&list_lock);
45 return found;
48 static struct scsi_device_handler *get_device_handler_by_idx(int idx)
50 struct scsi_device_handler *tmp, *found = NULL;
52 spin_lock(&list_lock);
53 list_for_each_entry(tmp, &scsi_dh_list, list) {
54 if (tmp->idx == idx) {
55 found = tmp;
56 break;
59 spin_unlock(&list_lock);
60 return found;
64 * device_handler_match - Attach a device handler to a device
65 * @scsi_dh - The device handler to match against or NULL
66 * @sdev - SCSI device to be tested against @scsi_dh
68 * Tests @sdev against the device handler @scsi_dh or against
69 * all registered device_handler if @scsi_dh == NULL.
70 * Returns the found device handler or NULL if not found.
72 static struct scsi_device_handler *
73 device_handler_match(struct scsi_device_handler *scsi_dh,
74 struct scsi_device *sdev)
76 struct scsi_device_handler *found_dh = NULL;
77 int idx;
79 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
80 SCSI_DEVINFO_DH);
81 found_dh = get_device_handler_by_idx(idx);
83 if (scsi_dh && found_dh != scsi_dh)
84 found_dh = NULL;
86 return found_dh;
90 * scsi_dh_handler_attach - Attach a device handler to a device
91 * @sdev - SCSI device the device handler should attach to
92 * @scsi_dh - The device handler to attach
94 static int scsi_dh_handler_attach(struct scsi_device *sdev,
95 struct scsi_device_handler *scsi_dh)
97 int err = 0;
99 if (sdev->scsi_dh_data) {
100 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
101 err = -EBUSY;
102 else
103 kref_get(&sdev->scsi_dh_data->kref);
104 } else if (scsi_dh->attach) {
105 err = scsi_dh->attach(sdev);
106 if (!err) {
107 kref_init(&sdev->scsi_dh_data->kref);
108 sdev->scsi_dh_data->sdev = sdev;
111 return err;
114 static void __detach_handler (struct kref *kref)
116 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
117 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
121 * scsi_dh_handler_detach - Detach a device handler from a device
122 * @sdev - SCSI device the device handler should be detached from
123 * @scsi_dh - Device handler to be detached
125 * Detach from a device handler. If a device handler is specified,
126 * only detach if the currently attached handler matches @scsi_dh.
128 static void scsi_dh_handler_detach(struct scsi_device *sdev,
129 struct scsi_device_handler *scsi_dh)
131 if (!sdev->scsi_dh_data)
132 return;
134 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
135 return;
137 if (!scsi_dh)
138 scsi_dh = sdev->scsi_dh_data->scsi_dh;
140 if (scsi_dh && scsi_dh->detach)
141 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
145 * Functions for sysfs attribute 'dh_state'
147 static ssize_t
148 store_dh_state(struct device *dev, struct device_attribute *attr,
149 const char *buf, size_t count)
151 struct scsi_device *sdev = to_scsi_device(dev);
152 struct scsi_device_handler *scsi_dh;
153 int err = -EINVAL;
155 if (!sdev->scsi_dh_data) {
157 * Attach to a device handler
159 if (!(scsi_dh = get_device_handler(buf)))
160 return err;
161 err = scsi_dh_handler_attach(sdev, scsi_dh);
162 } else {
163 scsi_dh = sdev->scsi_dh_data->scsi_dh;
164 if (!strncmp(buf, "detach", 6)) {
166 * Detach from a device handler
168 scsi_dh_handler_detach(sdev, scsi_dh);
169 err = 0;
170 } else if (!strncmp(buf, "activate", 8)) {
172 * Activate a device handler
174 if (scsi_dh->activate)
175 err = scsi_dh->activate(sdev, NULL, NULL);
176 else
177 err = 0;
181 return err<0?err:count;
184 static ssize_t
185 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
187 struct scsi_device *sdev = to_scsi_device(dev);
189 if (!sdev->scsi_dh_data)
190 return snprintf(buf, 20, "detached\n");
192 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
195 static struct device_attribute scsi_dh_state_attr =
196 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
197 store_dh_state);
200 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
202 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
204 struct scsi_device *sdev;
205 int err;
207 if (!scsi_is_sdev_device(dev))
208 return 0;
210 sdev = to_scsi_device(dev);
212 err = device_create_file(&sdev->sdev_gendev,
213 &scsi_dh_state_attr);
215 return 0;
219 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
221 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
223 struct scsi_device *sdev;
225 if (!scsi_is_sdev_device(dev))
226 return 0;
228 sdev = to_scsi_device(dev);
230 device_remove_file(&sdev->sdev_gendev,
231 &scsi_dh_state_attr);
233 return 0;
237 * scsi_dh_notifier - notifier chain callback
239 static int scsi_dh_notifier(struct notifier_block *nb,
240 unsigned long action, void *data)
242 struct device *dev = data;
243 struct scsi_device *sdev;
244 int err = 0;
245 struct scsi_device_handler *devinfo = NULL;
247 if (!scsi_is_sdev_device(dev))
248 return 0;
250 sdev = to_scsi_device(dev);
252 if (action == BUS_NOTIFY_ADD_DEVICE) {
253 err = device_create_file(dev, &scsi_dh_state_attr);
254 /* don't care about err */
255 devinfo = device_handler_match(NULL, sdev);
256 if (devinfo)
257 err = scsi_dh_handler_attach(sdev, devinfo);
258 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
259 device_remove_file(dev, &scsi_dh_state_attr);
260 scsi_dh_handler_detach(sdev, NULL);
262 return err;
266 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
268 static int scsi_dh_notifier_add(struct device *dev, void *data)
270 struct scsi_device_handler *scsi_dh = data;
271 struct scsi_device *sdev;
273 if (!scsi_is_sdev_device(dev))
274 return 0;
276 if (!get_device(dev))
277 return 0;
279 sdev = to_scsi_device(dev);
281 if (device_handler_match(scsi_dh, sdev))
282 scsi_dh_handler_attach(sdev, scsi_dh);
284 put_device(dev);
286 return 0;
290 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
292 static int scsi_dh_notifier_remove(struct device *dev, void *data)
294 struct scsi_device_handler *scsi_dh = data;
295 struct scsi_device *sdev;
297 if (!scsi_is_sdev_device(dev))
298 return 0;
300 if (!get_device(dev))
301 return 0;
303 sdev = to_scsi_device(dev);
305 scsi_dh_handler_detach(sdev, scsi_dh);
307 put_device(dev);
309 return 0;
313 * scsi_register_device_handler - register a device handler personality
314 * module.
315 * @scsi_dh - device handler to be registered.
317 * Returns 0 on success, -EBUSY if handler already registered.
319 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
321 int i;
323 if (get_device_handler(scsi_dh->name))
324 return -EBUSY;
326 spin_lock(&list_lock);
327 scsi_dh->idx = scsi_dh_list_idx++;
328 list_add(&scsi_dh->list, &scsi_dh_list);
329 spin_unlock(&list_lock);
331 for (i = 0; scsi_dh->devlist[i].vendor; i++) {
332 scsi_dev_info_list_add_keyed(0,
333 scsi_dh->devlist[i].vendor,
334 scsi_dh->devlist[i].model,
335 NULL,
336 scsi_dh->idx,
337 SCSI_DEVINFO_DH);
340 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
341 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
343 return SCSI_DH_OK;
345 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
348 * scsi_unregister_device_handler - register a device handler personality
349 * module.
350 * @scsi_dh - device handler to be unregistered.
352 * Returns 0 on success, -ENODEV if handler not registered.
354 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
356 int i;
358 if (!get_device_handler(scsi_dh->name))
359 return -ENODEV;
361 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
362 scsi_dh_notifier_remove);
364 for (i = 0; scsi_dh->devlist[i].vendor; i++) {
365 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
366 scsi_dh->devlist[i].model,
367 SCSI_DEVINFO_DH);
370 spin_lock(&list_lock);
371 list_del(&scsi_dh->list);
372 spin_unlock(&list_lock);
373 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
375 return SCSI_DH_OK;
377 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
380 * scsi_dh_activate - activate the path associated with the scsi_device
381 * corresponding to the given request queue.
382 * Returns immediately without waiting for activation to be completed.
383 * @q - Request queue that is associated with the scsi_device to be
384 * activated.
385 * @fn - Function to be called upon completion of the activation.
386 * Function fn is called with data (below) and the error code.
387 * Function fn may be called from the same calling context. So,
388 * do not hold the lock in the caller which may be needed in fn.
389 * @data - data passed to the function fn upon completion.
392 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
394 int err = 0;
395 unsigned long flags;
396 struct scsi_device *sdev;
397 struct scsi_device_handler *scsi_dh = NULL;
398 struct device *dev = NULL;
400 spin_lock_irqsave(q->queue_lock, flags);
401 sdev = q->queuedata;
402 if (sdev && sdev->scsi_dh_data)
403 scsi_dh = sdev->scsi_dh_data->scsi_dh;
404 dev = get_device(&sdev->sdev_gendev);
405 if (!scsi_dh || !dev ||
406 sdev->sdev_state == SDEV_CANCEL ||
407 sdev->sdev_state == SDEV_DEL)
408 err = SCSI_DH_NOSYS;
409 if (sdev->sdev_state == SDEV_OFFLINE)
410 err = SCSI_DH_DEV_OFFLINED;
411 spin_unlock_irqrestore(q->queue_lock, flags);
413 if (err) {
414 if (fn)
415 fn(data, err);
416 goto out;
419 if (scsi_dh->activate)
420 err = scsi_dh->activate(sdev, fn, data);
421 out:
422 put_device(dev);
423 return err;
425 EXPORT_SYMBOL_GPL(scsi_dh_activate);
428 * scsi_dh_set_params - set the parameters for the device as per the
429 * string specified in params.
430 * @q - Request queue that is associated with the scsi_device for
431 * which the parameters to be set.
432 * @params - parameters in the following format
433 * "no_of_params\0param1\0param2\0param3\0...\0"
434 * for example, string for 2 parameters with value 10 and 21
435 * is specified as "2\010\021\0".
437 int scsi_dh_set_params(struct request_queue *q, const char *params)
439 int err = -SCSI_DH_NOSYS;
440 unsigned long flags;
441 struct scsi_device *sdev;
442 struct scsi_device_handler *scsi_dh = NULL;
444 spin_lock_irqsave(q->queue_lock, flags);
445 sdev = q->queuedata;
446 if (sdev && sdev->scsi_dh_data)
447 scsi_dh = sdev->scsi_dh_data->scsi_dh;
448 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
449 err = 0;
450 spin_unlock_irqrestore(q->queue_lock, flags);
452 if (err)
453 return err;
454 err = scsi_dh->set_params(sdev, params);
455 put_device(&sdev->sdev_gendev);
456 return err;
458 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
461 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
462 * the given name. FALSE(0) otherwise.
463 * @name - name of the device handler.
465 int scsi_dh_handler_exist(const char *name)
467 return (get_device_handler(name) != NULL);
469 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
472 * scsi_dh_handler_attach - Attach device handler
473 * @sdev - sdev 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)
478 unsigned long flags;
479 struct scsi_device *sdev;
480 struct scsi_device_handler *scsi_dh;
481 int err = 0;
483 scsi_dh = get_device_handler(name);
484 if (!scsi_dh)
485 return -EINVAL;
487 spin_lock_irqsave(q->queue_lock, flags);
488 sdev = q->queuedata;
489 if (!sdev || !get_device(&sdev->sdev_gendev))
490 err = -ENODEV;
491 spin_unlock_irqrestore(q->queue_lock, flags);
493 if (!err) {
494 err = scsi_dh_handler_attach(sdev, scsi_dh);
495 put_device(&sdev->sdev_gendev);
497 return err;
499 EXPORT_SYMBOL_GPL(scsi_dh_attach);
502 * scsi_dh_handler_detach - Detach device handler
503 * @sdev - sdev the handler should be detached from
505 * This function will detach the device handler only
506 * if the sdev is not part of the internal list, ie
507 * if it has been attached manually.
509 void scsi_dh_detach(struct request_queue *q)
511 unsigned long flags;
512 struct scsi_device *sdev;
513 struct scsi_device_handler *scsi_dh = NULL;
515 spin_lock_irqsave(q->queue_lock, flags);
516 sdev = q->queuedata;
517 if (!sdev || !get_device(&sdev->sdev_gendev))
518 sdev = NULL;
519 spin_unlock_irqrestore(q->queue_lock, flags);
521 if (!sdev)
522 return;
524 if (sdev->scsi_dh_data) {
525 scsi_dh = sdev->scsi_dh_data->scsi_dh;
526 scsi_dh_handler_detach(sdev, scsi_dh);
528 put_device(&sdev->sdev_gendev);
530 EXPORT_SYMBOL_GPL(scsi_dh_detach);
532 static struct notifier_block scsi_dh_nb = {
533 .notifier_call = scsi_dh_notifier
536 static int __init scsi_dh_init(void)
538 int r;
540 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
541 if (r)
542 return r;
544 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
546 if (!r)
547 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
548 scsi_dh_sysfs_attr_add);
550 return r;
553 static void __exit scsi_dh_exit(void)
555 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
556 scsi_dh_sysfs_attr_remove);
557 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
558 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
561 module_init(scsi_dh_init);
562 module_exit(scsi_dh_exit);
564 MODULE_DESCRIPTION("SCSI device handler");
565 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
566 MODULE_LICENSE("GPL");