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
);
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
))) {
44 spin_unlock(&list_lock
);
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
) {
59 spin_unlock(&list_lock
);
64 * device_handler_match_function - Match a device handler to a device
65 * @sdev - SCSI device to be tested
67 * Tests @sdev against the match function of all registered device_handler.
68 * Returns the found device handler or NULL if not found.
70 static struct scsi_device_handler
*
71 device_handler_match_function(struct scsi_device
*sdev
)
73 struct scsi_device_handler
*tmp_dh
, *found_dh
= NULL
;
75 spin_lock(&list_lock
);
76 list_for_each_entry(tmp_dh
, &scsi_dh_list
, list
) {
77 if (tmp_dh
->match
&& tmp_dh
->match(sdev
)) {
82 spin_unlock(&list_lock
);
87 * device_handler_match_devlist - Match a device handler to a device
88 * @sdev - SCSI device to be tested
90 * Tests @sdev against all device_handler registered in the devlist.
91 * Returns the found device handler or NULL if not found.
93 static struct scsi_device_handler
*
94 device_handler_match_devlist(struct scsi_device
*sdev
)
98 idx
= scsi_get_device_flags_keyed(sdev
, sdev
->vendor
, sdev
->model
,
100 return get_device_handler_by_idx(idx
);
104 * device_handler_match - Attach a device handler to a device
105 * @scsi_dh - The device handler to match against or NULL
106 * @sdev - SCSI device to be tested against @scsi_dh
108 * Tests @sdev against the device handler @scsi_dh or against
109 * all registered device_handler if @scsi_dh == NULL.
110 * Returns the found device handler or NULL if not found.
112 static struct scsi_device_handler
*
113 device_handler_match(struct scsi_device_handler
*scsi_dh
,
114 struct scsi_device
*sdev
)
116 struct scsi_device_handler
*found_dh
;
118 found_dh
= device_handler_match_function(sdev
);
120 found_dh
= device_handler_match_devlist(sdev
);
122 if (scsi_dh
&& found_dh
!= scsi_dh
)
129 * scsi_dh_handler_attach - Attach a device handler to a device
130 * @sdev - SCSI device the device handler should attach to
131 * @scsi_dh - The device handler to attach
133 static int scsi_dh_handler_attach(struct scsi_device
*sdev
,
134 struct scsi_device_handler
*scsi_dh
)
138 if (sdev
->scsi_dh_data
) {
139 if (sdev
->scsi_dh_data
->scsi_dh
!= scsi_dh
)
142 kref_get(&sdev
->scsi_dh_data
->kref
);
143 } else if (scsi_dh
->attach
) {
144 err
= scsi_dh
->attach(sdev
);
146 kref_init(&sdev
->scsi_dh_data
->kref
);
147 sdev
->scsi_dh_data
->sdev
= sdev
;
153 static void __detach_handler (struct kref
*kref
)
155 struct scsi_dh_data
*scsi_dh_data
= container_of(kref
, struct scsi_dh_data
, kref
);
156 scsi_dh_data
->scsi_dh
->detach(scsi_dh_data
->sdev
);
160 * scsi_dh_handler_detach - Detach a device handler from a device
161 * @sdev - SCSI device the device handler should be detached from
162 * @scsi_dh - Device handler to be detached
164 * Detach from a device handler. If a device handler is specified,
165 * only detach if the currently attached handler matches @scsi_dh.
167 static void scsi_dh_handler_detach(struct scsi_device
*sdev
,
168 struct scsi_device_handler
*scsi_dh
)
170 if (!sdev
->scsi_dh_data
)
173 if (scsi_dh
&& scsi_dh
!= sdev
->scsi_dh_data
->scsi_dh
)
177 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
179 if (scsi_dh
&& scsi_dh
->detach
)
180 kref_put(&sdev
->scsi_dh_data
->kref
, __detach_handler
);
184 * Functions for sysfs attribute 'dh_state'
187 store_dh_state(struct device
*dev
, struct device_attribute
*attr
,
188 const char *buf
, size_t count
)
190 struct scsi_device
*sdev
= to_scsi_device(dev
);
191 struct scsi_device_handler
*scsi_dh
;
194 if (sdev
->sdev_state
== SDEV_CANCEL
||
195 sdev
->sdev_state
== SDEV_DEL
)
198 if (!sdev
->scsi_dh_data
) {
200 * Attach to a device handler
202 if (!(scsi_dh
= get_device_handler(buf
)))
204 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
206 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
207 if (!strncmp(buf
, "detach", 6)) {
209 * Detach from a device handler
211 scsi_dh_handler_detach(sdev
, scsi_dh
);
213 } else if (!strncmp(buf
, "activate", 8)) {
215 * Activate a device handler
217 if (scsi_dh
->activate
)
218 err
= scsi_dh
->activate(sdev
, NULL
, NULL
);
224 return err
<0?err
:count
;
228 show_dh_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
230 struct scsi_device
*sdev
= to_scsi_device(dev
);
232 if (!sdev
->scsi_dh_data
)
233 return snprintf(buf
, 20, "detached\n");
235 return snprintf(buf
, 20, "%s\n", sdev
->scsi_dh_data
->scsi_dh
->name
);
238 static struct device_attribute scsi_dh_state_attr
=
239 __ATTR(dh_state
, S_IRUGO
| S_IWUSR
, show_dh_state
,
243 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
245 static int scsi_dh_sysfs_attr_add(struct device
*dev
, void *data
)
247 struct scsi_device
*sdev
;
250 if (!scsi_is_sdev_device(dev
))
253 sdev
= to_scsi_device(dev
);
255 err
= device_create_file(&sdev
->sdev_gendev
,
256 &scsi_dh_state_attr
);
262 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
264 static int scsi_dh_sysfs_attr_remove(struct device
*dev
, void *data
)
266 struct scsi_device
*sdev
;
268 if (!scsi_is_sdev_device(dev
))
271 sdev
= to_scsi_device(dev
);
273 device_remove_file(&sdev
->sdev_gendev
,
274 &scsi_dh_state_attr
);
280 * scsi_dh_notifier - notifier chain callback
282 static int scsi_dh_notifier(struct notifier_block
*nb
,
283 unsigned long action
, void *data
)
285 struct device
*dev
= data
;
286 struct scsi_device
*sdev
;
288 struct scsi_device_handler
*devinfo
= NULL
;
290 if (!scsi_is_sdev_device(dev
))
293 sdev
= to_scsi_device(dev
);
295 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
296 err
= device_create_file(dev
, &scsi_dh_state_attr
);
297 /* don't care about err */
298 devinfo
= device_handler_match(NULL
, sdev
);
300 err
= scsi_dh_handler_attach(sdev
, devinfo
);
301 } else if (action
== BUS_NOTIFY_DEL_DEVICE
) {
302 device_remove_file(dev
, &scsi_dh_state_attr
);
303 scsi_dh_handler_detach(sdev
, NULL
);
309 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
311 static int scsi_dh_notifier_add(struct device
*dev
, void *data
)
313 struct scsi_device_handler
*scsi_dh
= data
;
314 struct scsi_device
*sdev
;
316 if (!scsi_is_sdev_device(dev
))
319 if (!get_device(dev
))
322 sdev
= to_scsi_device(dev
);
324 if (device_handler_match(scsi_dh
, sdev
))
325 scsi_dh_handler_attach(sdev
, scsi_dh
);
333 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
335 static int scsi_dh_notifier_remove(struct device
*dev
, void *data
)
337 struct scsi_device_handler
*scsi_dh
= data
;
338 struct scsi_device
*sdev
;
340 if (!scsi_is_sdev_device(dev
))
343 if (!get_device(dev
))
346 sdev
= to_scsi_device(dev
);
348 scsi_dh_handler_detach(sdev
, scsi_dh
);
356 * scsi_register_device_handler - register a device handler personality
358 * @scsi_dh - device handler to be registered.
360 * Returns 0 on success, -EBUSY if handler already registered.
362 int scsi_register_device_handler(struct scsi_device_handler
*scsi_dh
)
366 if (get_device_handler(scsi_dh
->name
))
369 spin_lock(&list_lock
);
370 scsi_dh
->idx
= scsi_dh_list_idx
++;
371 list_add(&scsi_dh
->list
, &scsi_dh_list
);
372 spin_unlock(&list_lock
);
374 for (i
= 0; scsi_dh
->devlist
&& scsi_dh
->devlist
[i
].vendor
; i
++) {
375 scsi_dev_info_list_add_keyed(0,
376 scsi_dh
->devlist
[i
].vendor
,
377 scsi_dh
->devlist
[i
].model
,
383 bus_for_each_dev(&scsi_bus_type
, NULL
, scsi_dh
, scsi_dh_notifier_add
);
384 printk(KERN_INFO
"%s: device handler registered\n", scsi_dh
->name
);
388 EXPORT_SYMBOL_GPL(scsi_register_device_handler
);
391 * scsi_unregister_device_handler - register a device handler personality
393 * @scsi_dh - device handler to be unregistered.
395 * Returns 0 on success, -ENODEV if handler not registered.
397 int scsi_unregister_device_handler(struct scsi_device_handler
*scsi_dh
)
401 if (!get_device_handler(scsi_dh
->name
))
404 bus_for_each_dev(&scsi_bus_type
, NULL
, scsi_dh
,
405 scsi_dh_notifier_remove
);
407 for (i
= 0; scsi_dh
->devlist
&& scsi_dh
->devlist
[i
].vendor
; i
++) {
408 scsi_dev_info_list_del_keyed(scsi_dh
->devlist
[i
].vendor
,
409 scsi_dh
->devlist
[i
].model
,
413 spin_lock(&list_lock
);
414 list_del(&scsi_dh
->list
);
415 spin_unlock(&list_lock
);
416 printk(KERN_INFO
"%s: device handler unregistered\n", scsi_dh
->name
);
420 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler
);
423 * scsi_dh_activate - activate the path associated with the scsi_device
424 * corresponding to the given request queue.
425 * Returns immediately without waiting for activation to be completed.
426 * @q - Request queue that is associated with the scsi_device to be
428 * @fn - Function to be called upon completion of the activation.
429 * Function fn is called with data (below) and the error code.
430 * Function fn may be called from the same calling context. So,
431 * do not hold the lock in the caller which may be needed in fn.
432 * @data - data passed to the function fn upon completion.
435 int scsi_dh_activate(struct request_queue
*q
, activate_complete fn
, void *data
)
439 struct scsi_device
*sdev
;
440 struct scsi_device_handler
*scsi_dh
= NULL
;
441 struct device
*dev
= NULL
;
443 spin_lock_irqsave(q
->queue_lock
, flags
);
445 if (sdev
&& sdev
->scsi_dh_data
)
446 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
447 dev
= get_device(&sdev
->sdev_gendev
);
448 if (!scsi_dh
|| !dev
||
449 sdev
->sdev_state
== SDEV_CANCEL
||
450 sdev
->sdev_state
== SDEV_DEL
)
452 if (sdev
->sdev_state
== SDEV_OFFLINE
)
453 err
= SCSI_DH_DEV_OFFLINED
;
454 spin_unlock_irqrestore(q
->queue_lock
, flags
);
462 if (scsi_dh
->activate
)
463 err
= scsi_dh
->activate(sdev
, fn
, data
);
468 EXPORT_SYMBOL_GPL(scsi_dh_activate
);
471 * scsi_dh_set_params - set the parameters for the device as per the
472 * string specified in params.
473 * @q - Request queue that is associated with the scsi_device for
474 * which the parameters to be set.
475 * @params - parameters in the following format
476 * "no_of_params\0param1\0param2\0param3\0...\0"
477 * for example, string for 2 parameters with value 10 and 21
478 * is specified as "2\010\021\0".
480 int scsi_dh_set_params(struct request_queue
*q
, const char *params
)
482 int err
= -SCSI_DH_NOSYS
;
484 struct scsi_device
*sdev
;
485 struct scsi_device_handler
*scsi_dh
= NULL
;
487 spin_lock_irqsave(q
->queue_lock
, flags
);
489 if (sdev
&& sdev
->scsi_dh_data
)
490 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
491 if (scsi_dh
&& scsi_dh
->set_params
&& get_device(&sdev
->sdev_gendev
))
493 spin_unlock_irqrestore(q
->queue_lock
, flags
);
497 err
= scsi_dh
->set_params(sdev
, params
);
498 put_device(&sdev
->sdev_gendev
);
501 EXPORT_SYMBOL_GPL(scsi_dh_set_params
);
504 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
505 * the given name. FALSE(0) otherwise.
506 * @name - name of the device handler.
508 int scsi_dh_handler_exist(const char *name
)
510 return (get_device_handler(name
) != NULL
);
512 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist
);
515 * scsi_dh_attach - Attach device handler
516 * @sdev - sdev the handler should be attached to
517 * @name - name of the handler to attach
519 int scsi_dh_attach(struct request_queue
*q
, const char *name
)
522 struct scsi_device
*sdev
;
523 struct scsi_device_handler
*scsi_dh
;
526 scsi_dh
= get_device_handler(name
);
530 spin_lock_irqsave(q
->queue_lock
, flags
);
532 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
534 spin_unlock_irqrestore(q
->queue_lock
, flags
);
537 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
538 put_device(&sdev
->sdev_gendev
);
542 EXPORT_SYMBOL_GPL(scsi_dh_attach
);
545 * scsi_dh_detach - Detach device handler
546 * @sdev - sdev the handler should be detached from
548 * This function will detach the device handler only
549 * if the sdev is not part of the internal list, ie
550 * if it has been attached manually.
552 void scsi_dh_detach(struct request_queue
*q
)
555 struct scsi_device
*sdev
;
556 struct scsi_device_handler
*scsi_dh
= NULL
;
558 spin_lock_irqsave(q
->queue_lock
, flags
);
560 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
562 spin_unlock_irqrestore(q
->queue_lock
, flags
);
567 if (sdev
->scsi_dh_data
) {
568 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
569 scsi_dh_handler_detach(sdev
, scsi_dh
);
571 put_device(&sdev
->sdev_gendev
);
573 EXPORT_SYMBOL_GPL(scsi_dh_detach
);
575 static struct notifier_block scsi_dh_nb
= {
576 .notifier_call
= scsi_dh_notifier
579 static int __init
scsi_dh_init(void)
583 r
= scsi_dev_info_add_list(SCSI_DEVINFO_DH
, "SCSI Device Handler");
587 r
= bus_register_notifier(&scsi_bus_type
, &scsi_dh_nb
);
590 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
591 scsi_dh_sysfs_attr_add
);
596 static void __exit
scsi_dh_exit(void)
598 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
599 scsi_dh_sysfs_attr_remove
);
600 bus_unregister_notifier(&scsi_bus_type
, &scsi_dh_nb
);
601 scsi_dev_info_remove_list(SCSI_DEVINFO_DH
);
604 module_init(scsi_dh_init
);
605 module_exit(scsi_dh_exit
);
607 MODULE_DESCRIPTION("SCSI device handler");
608 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
609 MODULE_LICENSE("GPL");