[SCSI] scsi_dh: Implement match callback function
[linux-2.6.git] / drivers / scsi / device_handler / scsi_dh.c
blob3ac71cf6b7f0e9427cacbf16a8fec4872fa80016
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_function - Match a device handler to a device
64 * @sdev - SCSI device to be tested
66 * Tests @sdev against the match function of all registered device_handler.
67 * Returns the found device handler or NULL if not found.
69 static struct scsi_device_handler *
70 device_handler_match_function(struct scsi_device *sdev)
72 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
74 spin_lock(&list_lock);
75 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
76 if (tmp_dh->match && tmp_dh->match(sdev)) {
77 found_dh = tmp_dh;
78 break;
81 spin_unlock(&list_lock);
82 return found_dh;
86 * device_handler_match_devlist - Match a device handler to a device
87 * @sdev - SCSI device to be tested
89 * Tests @sdev against all device_handler registered in the devlist.
90 * Returns the found device handler or NULL if not found.
92 static struct scsi_device_handler *
93 device_handler_match_devlist(struct scsi_device *sdev)
95 int idx;
97 idx = scsi_get_device_flags_keyed(sdev, sdev->vendor, sdev->model,
98 SCSI_DEVINFO_DH);
99 return get_device_handler_by_idx(idx);
103 * device_handler_match - Attach a device handler to a device
104 * @scsi_dh - The device handler to match against or NULL
105 * @sdev - SCSI device to be tested against @scsi_dh
107 * Tests @sdev against the device handler @scsi_dh or against
108 * all registered device_handler if @scsi_dh == NULL.
109 * Returns the found device handler or NULL if not found.
111 static struct scsi_device_handler *
112 device_handler_match(struct scsi_device_handler *scsi_dh,
113 struct scsi_device *sdev)
115 struct scsi_device_handler *found_dh;
117 found_dh = device_handler_match_function(sdev);
118 if (!found_dh)
119 found_dh = device_handler_match_devlist(sdev);
121 if (scsi_dh && found_dh != scsi_dh)
122 found_dh = NULL;
124 return found_dh;
128 * scsi_dh_handler_attach - Attach a device handler to a device
129 * @sdev - SCSI device the device handler should attach to
130 * @scsi_dh - The device handler to attach
132 static int scsi_dh_handler_attach(struct scsi_device *sdev,
133 struct scsi_device_handler *scsi_dh)
135 int err = 0;
137 if (sdev->scsi_dh_data) {
138 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
139 err = -EBUSY;
140 else
141 kref_get(&sdev->scsi_dh_data->kref);
142 } else if (scsi_dh->attach) {
143 err = scsi_dh->attach(sdev);
144 if (!err) {
145 kref_init(&sdev->scsi_dh_data->kref);
146 sdev->scsi_dh_data->sdev = sdev;
149 return err;
152 static void __detach_handler (struct kref *kref)
154 struct scsi_dh_data *scsi_dh_data = container_of(kref, struct scsi_dh_data, kref);
155 scsi_dh_data->scsi_dh->detach(scsi_dh_data->sdev);
159 * scsi_dh_handler_detach - Detach a device handler from a device
160 * @sdev - SCSI device the device handler should be detached from
161 * @scsi_dh - Device handler to be detached
163 * Detach from a device handler. If a device handler is specified,
164 * only detach if the currently attached handler matches @scsi_dh.
166 static void scsi_dh_handler_detach(struct scsi_device *sdev,
167 struct scsi_device_handler *scsi_dh)
169 if (!sdev->scsi_dh_data)
170 return;
172 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
173 return;
175 if (!scsi_dh)
176 scsi_dh = sdev->scsi_dh_data->scsi_dh;
178 if (scsi_dh && scsi_dh->detach)
179 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
183 * Functions for sysfs attribute 'dh_state'
185 static ssize_t
186 store_dh_state(struct device *dev, struct device_attribute *attr,
187 const char *buf, size_t count)
189 struct scsi_device *sdev = to_scsi_device(dev);
190 struct scsi_device_handler *scsi_dh;
191 int err = -EINVAL;
193 if (!sdev->scsi_dh_data) {
195 * Attach to a device handler
197 if (!(scsi_dh = get_device_handler(buf)))
198 return err;
199 err = scsi_dh_handler_attach(sdev, scsi_dh);
200 } else {
201 scsi_dh = sdev->scsi_dh_data->scsi_dh;
202 if (!strncmp(buf, "detach", 6)) {
204 * Detach from a device handler
206 scsi_dh_handler_detach(sdev, scsi_dh);
207 err = 0;
208 } else if (!strncmp(buf, "activate", 8)) {
210 * Activate a device handler
212 if (scsi_dh->activate)
213 err = scsi_dh->activate(sdev, NULL, NULL);
214 else
215 err = 0;
219 return err<0?err:count;
222 static ssize_t
223 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
225 struct scsi_device *sdev = to_scsi_device(dev);
227 if (!sdev->scsi_dh_data)
228 return snprintf(buf, 20, "detached\n");
230 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
233 static struct device_attribute scsi_dh_state_attr =
234 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
235 store_dh_state);
238 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
240 static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
242 struct scsi_device *sdev;
243 int err;
245 if (!scsi_is_sdev_device(dev))
246 return 0;
248 sdev = to_scsi_device(dev);
250 err = device_create_file(&sdev->sdev_gendev,
251 &scsi_dh_state_attr);
253 return 0;
257 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
259 static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
261 struct scsi_device *sdev;
263 if (!scsi_is_sdev_device(dev))
264 return 0;
266 sdev = to_scsi_device(dev);
268 device_remove_file(&sdev->sdev_gendev,
269 &scsi_dh_state_attr);
271 return 0;
275 * scsi_dh_notifier - notifier chain callback
277 static int scsi_dh_notifier(struct notifier_block *nb,
278 unsigned long action, void *data)
280 struct device *dev = data;
281 struct scsi_device *sdev;
282 int err = 0;
283 struct scsi_device_handler *devinfo = NULL;
285 if (!scsi_is_sdev_device(dev))
286 return 0;
288 sdev = to_scsi_device(dev);
290 if (action == BUS_NOTIFY_ADD_DEVICE) {
291 err = device_create_file(dev, &scsi_dh_state_attr);
292 /* don't care about err */
293 devinfo = device_handler_match(NULL, sdev);
294 if (devinfo)
295 err = scsi_dh_handler_attach(sdev, devinfo);
296 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
297 device_remove_file(dev, &scsi_dh_state_attr);
298 scsi_dh_handler_detach(sdev, NULL);
300 return err;
304 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
306 static int scsi_dh_notifier_add(struct device *dev, void *data)
308 struct scsi_device_handler *scsi_dh = data;
309 struct scsi_device *sdev;
311 if (!scsi_is_sdev_device(dev))
312 return 0;
314 if (!get_device(dev))
315 return 0;
317 sdev = to_scsi_device(dev);
319 if (device_handler_match(scsi_dh, sdev))
320 scsi_dh_handler_attach(sdev, scsi_dh);
322 put_device(dev);
324 return 0;
328 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
330 static int scsi_dh_notifier_remove(struct device *dev, void *data)
332 struct scsi_device_handler *scsi_dh = data;
333 struct scsi_device *sdev;
335 if (!scsi_is_sdev_device(dev))
336 return 0;
338 if (!get_device(dev))
339 return 0;
341 sdev = to_scsi_device(dev);
343 scsi_dh_handler_detach(sdev, scsi_dh);
345 put_device(dev);
347 return 0;
351 * scsi_register_device_handler - register a device handler personality
352 * module.
353 * @scsi_dh - device handler to be registered.
355 * Returns 0 on success, -EBUSY if handler already registered.
357 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
359 int i;
361 if (get_device_handler(scsi_dh->name))
362 return -EBUSY;
364 spin_lock(&list_lock);
365 scsi_dh->idx = scsi_dh_list_idx++;
366 list_add(&scsi_dh->list, &scsi_dh_list);
367 spin_unlock(&list_lock);
369 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
370 scsi_dev_info_list_add_keyed(0,
371 scsi_dh->devlist[i].vendor,
372 scsi_dh->devlist[i].model,
373 NULL,
374 scsi_dh->idx,
375 SCSI_DEVINFO_DH);
378 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
379 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
381 return SCSI_DH_OK;
383 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
386 * scsi_unregister_device_handler - register a device handler personality
387 * module.
388 * @scsi_dh - device handler to be unregistered.
390 * Returns 0 on success, -ENODEV if handler not registered.
392 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
394 int i;
396 if (!get_device_handler(scsi_dh->name))
397 return -ENODEV;
399 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
400 scsi_dh_notifier_remove);
402 for (i = 0; scsi_dh->devlist && scsi_dh->devlist[i].vendor; i++) {
403 scsi_dev_info_list_del_keyed(scsi_dh->devlist[i].vendor,
404 scsi_dh->devlist[i].model,
405 SCSI_DEVINFO_DH);
408 spin_lock(&list_lock);
409 list_del(&scsi_dh->list);
410 spin_unlock(&list_lock);
411 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
413 return SCSI_DH_OK;
415 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
418 * scsi_dh_activate - activate the path associated with the scsi_device
419 * corresponding to the given request queue.
420 * Returns immediately without waiting for activation to be completed.
421 * @q - Request queue that is associated with the scsi_device to be
422 * activated.
423 * @fn - Function to be called upon completion of the activation.
424 * Function fn is called with data (below) and the error code.
425 * Function fn may be called from the same calling context. So,
426 * do not hold the lock in the caller which may be needed in fn.
427 * @data - data passed to the function fn upon completion.
430 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
432 int err = 0;
433 unsigned long flags;
434 struct scsi_device *sdev;
435 struct scsi_device_handler *scsi_dh = NULL;
436 struct device *dev = NULL;
438 spin_lock_irqsave(q->queue_lock, flags);
439 sdev = q->queuedata;
440 if (sdev && sdev->scsi_dh_data)
441 scsi_dh = sdev->scsi_dh_data->scsi_dh;
442 dev = get_device(&sdev->sdev_gendev);
443 if (!scsi_dh || !dev ||
444 sdev->sdev_state == SDEV_CANCEL ||
445 sdev->sdev_state == SDEV_DEL)
446 err = SCSI_DH_NOSYS;
447 if (sdev->sdev_state == SDEV_OFFLINE)
448 err = SCSI_DH_DEV_OFFLINED;
449 spin_unlock_irqrestore(q->queue_lock, flags);
451 if (err) {
452 if (fn)
453 fn(data, err);
454 goto out;
457 if (scsi_dh->activate)
458 err = scsi_dh->activate(sdev, fn, data);
459 out:
460 put_device(dev);
461 return err;
463 EXPORT_SYMBOL_GPL(scsi_dh_activate);
466 * scsi_dh_set_params - set the parameters for the device as per the
467 * string specified in params.
468 * @q - Request queue that is associated with the scsi_device for
469 * which the parameters to be set.
470 * @params - parameters in the following format
471 * "no_of_params\0param1\0param2\0param3\0...\0"
472 * for example, string for 2 parameters with value 10 and 21
473 * is specified as "2\010\021\0".
475 int scsi_dh_set_params(struct request_queue *q, const char *params)
477 int err = -SCSI_DH_NOSYS;
478 unsigned long flags;
479 struct scsi_device *sdev;
480 struct scsi_device_handler *scsi_dh = NULL;
482 spin_lock_irqsave(q->queue_lock, flags);
483 sdev = q->queuedata;
484 if (sdev && sdev->scsi_dh_data)
485 scsi_dh = sdev->scsi_dh_data->scsi_dh;
486 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
487 err = 0;
488 spin_unlock_irqrestore(q->queue_lock, flags);
490 if (err)
491 return err;
492 err = scsi_dh->set_params(sdev, params);
493 put_device(&sdev->sdev_gendev);
494 return err;
496 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
499 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
500 * the given name. FALSE(0) otherwise.
501 * @name - name of the device handler.
503 int scsi_dh_handler_exist(const char *name)
505 return (get_device_handler(name) != NULL);
507 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
510 * scsi_dh_attach - Attach device handler
511 * @sdev - sdev the handler should be attached to
512 * @name - name of the handler to attach
514 int scsi_dh_attach(struct request_queue *q, const char *name)
516 unsigned long flags;
517 struct scsi_device *sdev;
518 struct scsi_device_handler *scsi_dh;
519 int err = 0;
521 scsi_dh = get_device_handler(name);
522 if (!scsi_dh)
523 return -EINVAL;
525 spin_lock_irqsave(q->queue_lock, flags);
526 sdev = q->queuedata;
527 if (!sdev || !get_device(&sdev->sdev_gendev))
528 err = -ENODEV;
529 spin_unlock_irqrestore(q->queue_lock, flags);
531 if (!err) {
532 err = scsi_dh_handler_attach(sdev, scsi_dh);
533 put_device(&sdev->sdev_gendev);
535 return err;
537 EXPORT_SYMBOL_GPL(scsi_dh_attach);
540 * scsi_dh_detach - Detach device handler
541 * @sdev - sdev the handler should be detached from
543 * This function will detach the device handler only
544 * if the sdev is not part of the internal list, ie
545 * if it has been attached manually.
547 void scsi_dh_detach(struct request_queue *q)
549 unsigned long flags;
550 struct scsi_device *sdev;
551 struct scsi_device_handler *scsi_dh = NULL;
553 spin_lock_irqsave(q->queue_lock, flags);
554 sdev = q->queuedata;
555 if (!sdev || !get_device(&sdev->sdev_gendev))
556 sdev = NULL;
557 spin_unlock_irqrestore(q->queue_lock, flags);
559 if (!sdev)
560 return;
562 if (sdev->scsi_dh_data) {
563 scsi_dh = sdev->scsi_dh_data->scsi_dh;
564 scsi_dh_handler_detach(sdev, scsi_dh);
566 put_device(&sdev->sdev_gendev);
568 EXPORT_SYMBOL_GPL(scsi_dh_detach);
570 static struct notifier_block scsi_dh_nb = {
571 .notifier_call = scsi_dh_notifier
574 static int __init scsi_dh_init(void)
576 int r;
578 r = scsi_dev_info_add_list(SCSI_DEVINFO_DH, "SCSI Device Handler");
579 if (r)
580 return r;
582 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
584 if (!r)
585 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
586 scsi_dh_sysfs_attr_add);
588 return r;
591 static void __exit scsi_dh_exit(void)
593 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
594 scsi_dh_sysfs_attr_remove);
595 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
596 scsi_dev_info_remove_list(SCSI_DEVINFO_DH);
599 module_init(scsi_dh_init);
600 module_exit(scsi_dh_exit);
602 MODULE_DESCRIPTION("SCSI device handler");
603 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
604 MODULE_LICENSE("GPL");