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 - 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
;
79 idx
= scsi_get_device_flags_keyed(sdev
, sdev
->vendor
, sdev
->model
,
81 found_dh
= get_device_handler_by_idx(idx
);
83 if (scsi_dh
&& found_dh
!= scsi_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
)
99 if (sdev
->scsi_dh_data
) {
100 if (sdev
->scsi_dh_data
->scsi_dh
!= scsi_dh
)
103 kref_get(&sdev
->scsi_dh_data
->kref
);
104 } else if (scsi_dh
->attach
) {
105 err
= scsi_dh
->attach(sdev
);
107 kref_init(&sdev
->scsi_dh_data
->kref
);
108 sdev
->scsi_dh_data
->sdev
= sdev
;
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
)
134 if (scsi_dh
&& scsi_dh
!= sdev
->scsi_dh_data
->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'
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
;
155 if (!sdev
->scsi_dh_data
) {
157 * Attach to a device handler
159 if (!(scsi_dh
= get_device_handler(buf
)))
161 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
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
);
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
);
181 return err
<0?err
:count
;
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
,
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
;
207 if (!scsi_is_sdev_device(dev
))
210 sdev
= to_scsi_device(dev
);
212 err
= device_create_file(&sdev
->sdev_gendev
,
213 &scsi_dh_state_attr
);
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
))
228 sdev
= to_scsi_device(dev
);
230 device_remove_file(&sdev
->sdev_gendev
,
231 &scsi_dh_state_attr
);
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
;
245 struct scsi_device_handler
*devinfo
= NULL
;
247 if (!scsi_is_sdev_device(dev
))
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
);
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
);
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
))
276 if (!get_device(dev
))
279 sdev
= to_scsi_device(dev
);
281 if (device_handler_match(scsi_dh
, sdev
))
282 scsi_dh_handler_attach(sdev
, scsi_dh
);
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
))
300 if (!get_device(dev
))
303 sdev
= to_scsi_device(dev
);
305 scsi_dh_handler_detach(sdev
, scsi_dh
);
313 * scsi_register_device_handler - register a device handler personality
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
)
323 if (get_device_handler(scsi_dh
->name
))
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
,
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
);
345 EXPORT_SYMBOL_GPL(scsi_register_device_handler
);
348 * scsi_unregister_device_handler - register a device handler personality
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
)
358 if (!get_device_handler(scsi_dh
->name
))
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
,
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
);
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
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
)
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
);
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
)
409 if (sdev
->sdev_state
== SDEV_OFFLINE
)
410 err
= SCSI_DH_DEV_OFFLINED
;
411 spin_unlock_irqrestore(q
->queue_lock
, flags
);
419 if (scsi_dh
->activate
)
420 err
= scsi_dh
->activate(sdev
, fn
, data
);
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
;
441 struct scsi_device
*sdev
;
442 struct scsi_device_handler
*scsi_dh
= NULL
;
444 spin_lock_irqsave(q
->queue_lock
, flags
);
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
))
450 spin_unlock_irqrestore(q
->queue_lock
, flags
);
454 err
= scsi_dh
->set_params(sdev
, params
);
455 put_device(&sdev
->sdev_gendev
);
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
)
479 struct scsi_device
*sdev
;
480 struct scsi_device_handler
*scsi_dh
;
483 scsi_dh
= get_device_handler(name
);
487 spin_lock_irqsave(q
->queue_lock
, flags
);
489 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
491 spin_unlock_irqrestore(q
->queue_lock
, flags
);
494 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
495 put_device(&sdev
->sdev_gendev
);
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
)
512 struct scsi_device
*sdev
;
513 struct scsi_device_handler
*scsi_dh
= NULL
;
515 spin_lock_irqsave(q
->queue_lock
, flags
);
517 if (!sdev
|| !get_device(&sdev
->sdev_gendev
))
519 spin_unlock_irqrestore(q
->queue_lock
, flags
);
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)
540 r
= scsi_dev_info_add_list(SCSI_DEVINFO_DH
, "SCSI Device Handler");
544 r
= bus_register_notifier(&scsi_bus_type
, &scsi_dh_nb
);
547 bus_for_each_dev(&scsi_bus_type
, NULL
, NULL
,
548 scsi_dh_sysfs_attr_add
);
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");