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 <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
))) {
43 spin_unlock(&list_lock
);
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
) {
58 spin_unlock(&list_lock
);
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
)) {
81 spin_unlock(&list_lock
);
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
)
97 idx
= scsi_get_device_flags_keyed(sdev
, sdev
->vendor
, sdev
->model
,
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
);
119 found_dh
= device_handler_match_devlist(sdev
);
121 if (scsi_dh
&& found_dh
!= scsi_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
)
137 if (sdev
->scsi_dh_data
) {
138 if (sdev
->scsi_dh_data
->scsi_dh
!= scsi_dh
)
141 kref_get(&sdev
->scsi_dh_data
->kref
);
142 } else if (scsi_dh
->attach
) {
143 err
= scsi_dh
->attach(sdev
);
145 kref_init(&sdev
->scsi_dh_data
->kref
);
146 sdev
->scsi_dh_data
->sdev
= sdev
;
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
)
172 if (scsi_dh
&& scsi_dh
!= sdev
->scsi_dh_data
->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'
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
;
193 if (!sdev
->scsi_dh_data
) {
195 * Attach to a device handler
197 if (!(scsi_dh
= get_device_handler(buf
)))
199 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
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
);
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
);
219 return err
<0?err
:count
;
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
,
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
;
245 if (!scsi_is_sdev_device(dev
))
248 sdev
= to_scsi_device(dev
);
250 err
= device_create_file(&sdev
->sdev_gendev
,
251 &scsi_dh_state_attr
);
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
))
266 sdev
= to_scsi_device(dev
);
268 device_remove_file(&sdev
->sdev_gendev
,
269 &scsi_dh_state_attr
);
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
;
283 struct scsi_device_handler
*devinfo
= NULL
;
285 if (!scsi_is_sdev_device(dev
))
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
);
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
);
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
))
314 if (!get_device(dev
))
317 sdev
= to_scsi_device(dev
);
319 if (device_handler_match(scsi_dh
, sdev
))
320 scsi_dh_handler_attach(sdev
, scsi_dh
);
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
))
338 if (!get_device(dev
))
341 sdev
= to_scsi_device(dev
);
343 scsi_dh_handler_detach(sdev
, scsi_dh
);
351 * scsi_register_device_handler - register a device handler personality
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
)
361 if (get_device_handler(scsi_dh
->name
))
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
,
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
);
383 EXPORT_SYMBOL_GPL(scsi_register_device_handler
);
386 * scsi_unregister_device_handler - register a device handler personality
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
)
396 if (!get_device_handler(scsi_dh
->name
))
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
,
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
);
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
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
)
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
);
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
)
447 if (sdev
->sdev_state
== SDEV_OFFLINE
)
448 err
= SCSI_DH_DEV_OFFLINED
;
449 spin_unlock_irqrestore(q
->queue_lock
, flags
);
457 if (scsi_dh
->activate
)
458 err
= scsi_dh
->activate(sdev
, fn
, data
);
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
;
479 struct scsi_device
*sdev
;
480 struct scsi_device_handler
*scsi_dh
= NULL
;
482 spin_lock_irqsave(q
->queue_lock
, flags
);
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
))
488 spin_unlock_irqrestore(q
->queue_lock
, flags
);
492 err
= scsi_dh
->set_params(sdev
, params
);
493 put_device(&sdev
->sdev_gendev
);
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
)
517 struct scsi_device
*sdev
;
518 struct scsi_device_handler
*scsi_dh
;
521 scsi_dh
= get_device_handler(name
);
525 spin_lock_irqsave(q
->queue_lock
, flags
);
527 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
529 spin_unlock_irqrestore(q
->queue_lock
, flags
);
532 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
533 put_device(&sdev
->sdev_gendev
);
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
)
550 struct scsi_device
*sdev
;
551 struct scsi_device_handler
*scsi_dh
= NULL
;
553 spin_lock_irqsave(q
->queue_lock
, flags
);
555 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
557 spin_unlock_irqrestore(q
->queue_lock
, flags
);
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)
578 r
= scsi_dev_info_add_list(SCSI_DEVINFO_DH
, "SCSI Device Handler");
582 r
= bus_register_notifier(&scsi_bus_type
, &scsi_dh_nb
);
585 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
586 scsi_dh_sysfs_attr_add
);
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");