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
);
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
);
48 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
54 static struct scsi_device_handler
*
55 device_handler_match_function(struct scsi_device
*sdev
)
57 struct scsi_device_handler
*tmp_dh
, *found_dh
= NULL
;
59 spin_lock(&list_lock
);
60 list_for_each_entry(tmp_dh
, &scsi_dh_list
, list
) {
61 if (tmp_dh
->match
&& tmp_dh
->match(sdev
)) {
66 spin_unlock(&list_lock
);
71 * device_handler_match - Attach a device handler to a device
72 * @scsi_dh - The device handler to match against or NULL
73 * @sdev - SCSI device to be tested against @scsi_dh
75 * Tests @sdev against the device handler @scsi_dh or against
76 * all registered device_handler if @scsi_dh == NULL.
77 * Returns the found device handler or NULL if not found.
79 static struct scsi_device_handler
*
80 device_handler_match(struct scsi_device_handler
*scsi_dh
,
81 struct scsi_device
*sdev
)
83 struct scsi_device_handler
*found_dh
;
85 found_dh
= device_handler_match_function(sdev
);
87 if (scsi_dh
&& found_dh
!= scsi_dh
)
94 * scsi_dh_handler_attach - Attach a device handler to a device
95 * @sdev - SCSI device the device handler should attach to
96 * @scsi_dh - The device handler to attach
98 static int scsi_dh_handler_attach(struct scsi_device
*sdev
,
99 struct scsi_device_handler
*scsi_dh
)
103 if (sdev
->scsi_dh_data
) {
104 if (sdev
->scsi_dh_data
->scsi_dh
!= scsi_dh
)
107 kref_get(&sdev
->scsi_dh_data
->kref
);
108 } else if (scsi_dh
->attach
) {
109 err
= scsi_dh
->attach(sdev
);
111 kref_init(&sdev
->scsi_dh_data
->kref
);
112 sdev
->scsi_dh_data
->sdev
= sdev
;
118 static void __detach_handler (struct kref
*kref
)
120 struct scsi_dh_data
*scsi_dh_data
= container_of(kref
, struct scsi_dh_data
, kref
);
121 scsi_dh_data
->scsi_dh
->detach(scsi_dh_data
->sdev
);
125 * scsi_dh_handler_detach - Detach a device handler from a device
126 * @sdev - SCSI device the device handler should be detached from
127 * @scsi_dh - Device handler to be detached
129 * Detach from a device handler. If a device handler is specified,
130 * only detach if the currently attached handler matches @scsi_dh.
132 static void scsi_dh_handler_detach(struct scsi_device
*sdev
,
133 struct scsi_device_handler
*scsi_dh
)
135 if (!sdev
->scsi_dh_data
)
138 if (scsi_dh
&& scsi_dh
!= sdev
->scsi_dh_data
->scsi_dh
)
142 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
144 if (scsi_dh
&& scsi_dh
->detach
)
145 kref_put(&sdev
->scsi_dh_data
->kref
, __detach_handler
);
149 * Functions for sysfs attribute 'dh_state'
152 store_dh_state(struct device
*dev
, struct device_attribute
*attr
,
153 const char *buf
, size_t count
)
155 struct scsi_device
*sdev
= to_scsi_device(dev
);
156 struct scsi_device_handler
*scsi_dh
;
159 if (sdev
->sdev_state
== SDEV_CANCEL
||
160 sdev
->sdev_state
== SDEV_DEL
)
163 if (!sdev
->scsi_dh_data
) {
165 * Attach to a device handler
167 if (!(scsi_dh
= get_device_handler(buf
)))
169 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
171 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
172 if (!strncmp(buf
, "detach", 6)) {
174 * Detach from a device handler
176 scsi_dh_handler_detach(sdev
, scsi_dh
);
178 } else if (!strncmp(buf
, "activate", 8)) {
180 * Activate a device handler
182 if (scsi_dh
->activate
)
183 err
= scsi_dh
->activate(sdev
, NULL
, NULL
);
189 return err
<0?err
:count
;
193 show_dh_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
195 struct scsi_device
*sdev
= to_scsi_device(dev
);
197 if (!sdev
->scsi_dh_data
)
198 return snprintf(buf
, 20, "detached\n");
200 return snprintf(buf
, 20, "%s\n", sdev
->scsi_dh_data
->scsi_dh
->name
);
203 static struct device_attribute scsi_dh_state_attr
=
204 __ATTR(dh_state
, S_IRUGO
| S_IWUSR
, show_dh_state
,
208 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
210 static int scsi_dh_sysfs_attr_add(struct device
*dev
, void *data
)
212 struct scsi_device
*sdev
;
215 if (!scsi_is_sdev_device(dev
))
218 sdev
= to_scsi_device(dev
);
220 err
= device_create_file(&sdev
->sdev_gendev
,
221 &scsi_dh_state_attr
);
227 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
229 static int scsi_dh_sysfs_attr_remove(struct device
*dev
, void *data
)
231 struct scsi_device
*sdev
;
233 if (!scsi_is_sdev_device(dev
))
236 sdev
= to_scsi_device(dev
);
238 device_remove_file(&sdev
->sdev_gendev
,
239 &scsi_dh_state_attr
);
245 * scsi_dh_notifier - notifier chain callback
247 static int scsi_dh_notifier(struct notifier_block
*nb
,
248 unsigned long action
, void *data
)
250 struct device
*dev
= data
;
251 struct scsi_device
*sdev
;
253 struct scsi_device_handler
*devinfo
= NULL
;
255 if (!scsi_is_sdev_device(dev
))
258 sdev
= to_scsi_device(dev
);
260 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
261 err
= device_create_file(dev
, &scsi_dh_state_attr
);
262 /* don't care about err */
263 devinfo
= device_handler_match(NULL
, sdev
);
265 err
= scsi_dh_handler_attach(sdev
, devinfo
);
266 } else if (action
== BUS_NOTIFY_DEL_DEVICE
) {
267 device_remove_file(dev
, &scsi_dh_state_attr
);
268 scsi_dh_handler_detach(sdev
, NULL
);
274 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
276 static int scsi_dh_notifier_add(struct device
*dev
, void *data
)
278 struct scsi_device_handler
*scsi_dh
= data
;
279 struct scsi_device
*sdev
;
281 if (!scsi_is_sdev_device(dev
))
284 if (!get_device(dev
))
287 sdev
= to_scsi_device(dev
);
289 if (device_handler_match(scsi_dh
, sdev
))
290 scsi_dh_handler_attach(sdev
, scsi_dh
);
298 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
300 static int scsi_dh_notifier_remove(struct device
*dev
, void *data
)
302 struct scsi_device_handler
*scsi_dh
= data
;
303 struct scsi_device
*sdev
;
305 if (!scsi_is_sdev_device(dev
))
308 if (!get_device(dev
))
311 sdev
= to_scsi_device(dev
);
313 scsi_dh_handler_detach(sdev
, scsi_dh
);
321 * scsi_register_device_handler - register a device handler personality
323 * @scsi_dh - device handler to be registered.
325 * Returns 0 on success, -EBUSY if handler already registered.
327 int scsi_register_device_handler(struct scsi_device_handler
*scsi_dh
)
330 if (get_device_handler(scsi_dh
->name
))
333 spin_lock(&list_lock
);
334 list_add(&scsi_dh
->list
, &scsi_dh_list
);
335 spin_unlock(&list_lock
);
337 bus_for_each_dev(&scsi_bus_type
, NULL
, scsi_dh
, scsi_dh_notifier_add
);
338 printk(KERN_INFO
"%s: device handler registered\n", scsi_dh
->name
);
342 EXPORT_SYMBOL_GPL(scsi_register_device_handler
);
345 * scsi_unregister_device_handler - register a device handler personality
347 * @scsi_dh - device handler to be unregistered.
349 * Returns 0 on success, -ENODEV if handler not registered.
351 int scsi_unregister_device_handler(struct scsi_device_handler
*scsi_dh
)
354 if (!get_device_handler(scsi_dh
->name
))
357 bus_for_each_dev(&scsi_bus_type
, NULL
, scsi_dh
,
358 scsi_dh_notifier_remove
);
360 spin_lock(&list_lock
);
361 list_del(&scsi_dh
->list
);
362 spin_unlock(&list_lock
);
363 printk(KERN_INFO
"%s: device handler unregistered\n", scsi_dh
->name
);
367 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler
);
370 * scsi_dh_activate - activate the path associated with the scsi_device
371 * corresponding to the given request queue.
372 * Returns immediately without waiting for activation to be completed.
373 * @q - Request queue that is associated with the scsi_device to be
375 * @fn - Function to be called upon completion of the activation.
376 * Function fn is called with data (below) and the error code.
377 * Function fn may be called from the same calling context. So,
378 * do not hold the lock in the caller which may be needed in fn.
379 * @data - data passed to the function fn upon completion.
382 int scsi_dh_activate(struct request_queue
*q
, activate_complete fn
, void *data
)
386 struct scsi_device
*sdev
;
387 struct scsi_device_handler
*scsi_dh
= NULL
;
388 struct device
*dev
= NULL
;
390 spin_lock_irqsave(q
->queue_lock
, flags
);
393 spin_unlock_irqrestore(q
->queue_lock
, flags
);
400 if (sdev
->scsi_dh_data
)
401 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
402 dev
= get_device(&sdev
->sdev_gendev
);
403 if (!scsi_dh
|| !dev
||
404 sdev
->sdev_state
== SDEV_CANCEL
||
405 sdev
->sdev_state
== SDEV_DEL
)
407 if (sdev
->sdev_state
== SDEV_OFFLINE
)
408 err
= SCSI_DH_DEV_OFFLINED
;
409 spin_unlock_irqrestore(q
->queue_lock
, flags
);
417 if (scsi_dh
->activate
)
418 err
= scsi_dh
->activate(sdev
, fn
, data
);
423 EXPORT_SYMBOL_GPL(scsi_dh_activate
);
426 * scsi_dh_set_params - set the parameters for the device as per the
427 * string specified in params.
428 * @q - Request queue that is associated with the scsi_device for
429 * which the parameters to be set.
430 * @params - parameters in the following format
431 * "no_of_params\0param1\0param2\0param3\0...\0"
432 * for example, string for 2 parameters with value 10 and 21
433 * is specified as "2\010\021\0".
435 int scsi_dh_set_params(struct request_queue
*q
, const char *params
)
437 int err
= -SCSI_DH_NOSYS
;
439 struct scsi_device
*sdev
;
440 struct scsi_device_handler
*scsi_dh
= NULL
;
442 spin_lock_irqsave(q
->queue_lock
, flags
);
444 if (sdev
&& sdev
->scsi_dh_data
)
445 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
446 if (scsi_dh
&& scsi_dh
->set_params
&& get_device(&sdev
->sdev_gendev
))
448 spin_unlock_irqrestore(q
->queue_lock
, flags
);
452 err
= scsi_dh
->set_params(sdev
, params
);
453 put_device(&sdev
->sdev_gendev
);
456 EXPORT_SYMBOL_GPL(scsi_dh_set_params
);
459 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
460 * the given name. FALSE(0) otherwise.
461 * @name - name of the device handler.
463 int scsi_dh_handler_exist(const char *name
)
465 return (get_device_handler(name
) != NULL
);
467 EXPORT_SYMBOL_GPL(scsi_dh_handler_exist
);
470 * scsi_dh_attach - Attach device handler
471 * @sdev - sdev the handler should be attached to
472 * @name - name of the handler to attach
474 int scsi_dh_attach(struct request_queue
*q
, const char *name
)
477 struct scsi_device
*sdev
;
478 struct scsi_device_handler
*scsi_dh
;
481 scsi_dh
= get_device_handler(name
);
485 spin_lock_irqsave(q
->queue_lock
, flags
);
487 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
489 spin_unlock_irqrestore(q
->queue_lock
, flags
);
492 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
493 put_device(&sdev
->sdev_gendev
);
497 EXPORT_SYMBOL_GPL(scsi_dh_attach
);
500 * scsi_dh_detach - Detach device handler
501 * @sdev - sdev the handler should be detached from
503 * This function will detach the device handler only
504 * if the sdev is not part of the internal list, ie
505 * if it has been attached manually.
507 void scsi_dh_detach(struct request_queue
*q
)
510 struct scsi_device
*sdev
;
511 struct scsi_device_handler
*scsi_dh
= NULL
;
513 spin_lock_irqsave(q
->queue_lock
, flags
);
515 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
517 spin_unlock_irqrestore(q
->queue_lock
, flags
);
522 if (sdev
->scsi_dh_data
) {
523 scsi_dh
= sdev
->scsi_dh_data
->scsi_dh
;
524 scsi_dh_handler_detach(sdev
, scsi_dh
);
526 put_device(&sdev
->sdev_gendev
);
528 EXPORT_SYMBOL_GPL(scsi_dh_detach
);
530 static struct notifier_block scsi_dh_nb
= {
531 .notifier_call
= scsi_dh_notifier
534 static int __init
scsi_dh_init(void)
538 r
= bus_register_notifier(&scsi_bus_type
, &scsi_dh_nb
);
541 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
542 scsi_dh_sysfs_attr_add
);
547 static void __exit
scsi_dh_exit(void)
549 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
550 scsi_dh_sysfs_attr_remove
);
551 bus_unregister_notifier(&scsi_bus_type
, &scsi_dh_nb
);
554 module_init(scsi_dh_init
);
555 module_exit(scsi_dh_exit
);
557 MODULE_DESCRIPTION("SCSI device handler");
558 MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
559 MODULE_LICENSE("GPL");