scsi: device handlers must have attach and detach methods
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh.c
CommitLineData
a6a8d9f8
CS
1/*
2 * SCSI device handler infrastruture.
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright IBM Corporation, 2007
19 * Authors:
20 * Chandra Seetharaman <sekharan@us.ibm.com>
21 * Mike Anderson <andmike@linux.vnet.ibm.com>
22 */
23
5a0e3ad6 24#include <linux/slab.h>
acf3368f 25#include <linux/module.h>
a6a8d9f8
CS
26#include <scsi/scsi_dh.h>
27#include "../scsi_priv.h"
28
29static DEFINE_SPINLOCK(list_lock);
30static LIST_HEAD(scsi_dh_list);
31
32static struct scsi_device_handler *get_device_handler(const char *name)
33{
34 struct scsi_device_handler *tmp, *found = NULL;
35
36 spin_lock(&list_lock);
37 list_for_each_entry(tmp, &scsi_dh_list, list) {
765cbc6d 38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
a6a8d9f8
CS
39 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
6c3633d0
HR
47/*
48 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
50 *
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
53 */
54static struct scsi_device_handler *
55device_handler_match_function(struct scsi_device *sdev)
56{
57 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
58
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)) {
62 found_dh = tmp_dh;
63 break;
64 }
65 }
66 spin_unlock(&list_lock);
67 return found_dh;
68}
69
7c32c7a2
HR
70/*
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
74 *
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.
78 */
79static struct scsi_device_handler *
80device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
82{
6c3633d0 83 struct scsi_device_handler *found_dh;
7c32c7a2 84
6c3633d0 85 found_dh = device_handler_match_function(sdev);
7c32c7a2 86
940d7faa
PJ
87 if (scsi_dh && found_dh != scsi_dh)
88 found_dh = NULL;
7c32c7a2
HR
89
90 return found_dh;
a6a8d9f8
CS
91}
92
93/*
765cbc6d
HR
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
97 */
98static int scsi_dh_handler_attach(struct scsi_device *sdev,
99 struct scsi_device_handler *scsi_dh)
100{
101 int err = 0;
102
103 if (sdev->scsi_dh_data) {
104 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
27c888f0
CH
105 return -EBUSY;
106
107 kref_get(&sdev->scsi_dh_data->kref);
108 return 0;
109 }
110
1f12ffa5
CH
111 if (!try_module_get(scsi_dh->module))
112 return -EINVAL;
27c888f0 113
1f12ffa5
CH
114 err = scsi_dh->attach(sdev);
115 if (err) {
116 module_put(scsi_dh->module);
117 return err;
6c10db72 118 }
1f12ffa5
CH
119
120 kref_init(&sdev->scsi_dh_data->kref);
121 sdev->scsi_dh_data->sdev = sdev;
765cbc6d
HR
122 return err;
123}
124
6c10db72
CS
125static void __detach_handler (struct kref *kref)
126{
27c888f0
CH
127 struct scsi_dh_data *scsi_dh_data =
128 container_of(kref, struct scsi_dh_data, kref);
129 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
130
131 scsi_dh->detach(scsi_dh_data->sdev);
132 module_put(scsi_dh->module);
6c10db72
CS
133}
134
765cbc6d
HR
135/*
136 * scsi_dh_handler_detach - Detach a device handler from a device
137 * @sdev - SCSI device the device handler should be detached from
138 * @scsi_dh - Device handler to be detached
a6a8d9f8 139 *
765cbc6d 140 * Detach from a device handler. If a device handler is specified,
4c05ae52 141 * only detach if the currently attached handler matches @scsi_dh.
a6a8d9f8 142 */
765cbc6d
HR
143static void scsi_dh_handler_detach(struct scsi_device *sdev,
144 struct scsi_device_handler *scsi_dh)
a6a8d9f8 145{
765cbc6d
HR
146 if (!sdev->scsi_dh_data)
147 return;
a6a8d9f8 148
765cbc6d
HR
149 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
150 return;
a6a8d9f8 151
765cbc6d
HR
152 if (!scsi_dh)
153 scsi_dh = sdev->scsi_dh_data->scsi_dh;
154
1f12ffa5 155 if (scsi_dh)
6c10db72 156 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
765cbc6d
HR
157}
158
4c05ae52
HR
159/*
160 * Functions for sysfs attribute 'dh_state'
161 */
162static ssize_t
163store_dh_state(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count)
165{
166 struct scsi_device *sdev = to_scsi_device(dev);
167 struct scsi_device_handler *scsi_dh;
168 int err = -EINVAL;
169
6bc8d2a0
HR
170 if (sdev->sdev_state == SDEV_CANCEL ||
171 sdev->sdev_state == SDEV_DEL)
172 return -ENODEV;
173
4c05ae52
HR
174 if (!sdev->scsi_dh_data) {
175 /*
176 * Attach to a device handler
177 */
178 if (!(scsi_dh = get_device_handler(buf)))
179 return err;
180 err = scsi_dh_handler_attach(sdev, scsi_dh);
181 } else {
182 scsi_dh = sdev->scsi_dh_data->scsi_dh;
183 if (!strncmp(buf, "detach", 6)) {
184 /*
185 * Detach from a device handler
186 */
187 scsi_dh_handler_detach(sdev, scsi_dh);
188 err = 0;
189 } else if (!strncmp(buf, "activate", 8)) {
190 /*
191 * Activate a device handler
192 */
193 if (scsi_dh->activate)
3ae31f6a 194 err = scsi_dh->activate(sdev, NULL, NULL);
4c05ae52
HR
195 else
196 err = 0;
197 }
198 }
199
200 return err<0?err:count;
201}
202
203static ssize_t
204show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
205{
206 struct scsi_device *sdev = to_scsi_device(dev);
207
208 if (!sdev->scsi_dh_data)
209 return snprintf(buf, 20, "detached\n");
210
211 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
212}
213
214static struct device_attribute scsi_dh_state_attr =
215 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
216 store_dh_state);
217
218/*
219 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
220 */
221static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
222{
223 struct scsi_device *sdev;
224 int err;
225
226 if (!scsi_is_sdev_device(dev))
227 return 0;
228
229 sdev = to_scsi_device(dev);
230
231 err = device_create_file(&sdev->sdev_gendev,
232 &scsi_dh_state_attr);
233
234 return 0;
235}
236
237/*
238 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
239 */
240static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
241{
242 struct scsi_device *sdev;
243
244 if (!scsi_is_sdev_device(dev))
245 return 0;
246
247 sdev = to_scsi_device(dev);
248
249 device_remove_file(&sdev->sdev_gendev,
250 &scsi_dh_state_attr);
251
252 return 0;
253}
254
765cbc6d
HR
255/*
256 * scsi_dh_notifier - notifier chain callback
257 */
258static int scsi_dh_notifier(struct notifier_block *nb,
259 unsigned long action, void *data)
260{
261 struct device *dev = data;
262 struct scsi_device *sdev;
263 int err = 0;
7c32c7a2 264 struct scsi_device_handler *devinfo = NULL;
765cbc6d
HR
265
266 if (!scsi_is_sdev_device(dev))
267 return 0;
268
269 sdev = to_scsi_device(dev);
a6a8d9f8 270
765cbc6d 271 if (action == BUS_NOTIFY_ADD_DEVICE) {
5917290c
CS
272 err = device_create_file(dev, &scsi_dh_state_attr);
273 /* don't care about err */
7c32c7a2 274 devinfo = device_handler_match(NULL, sdev);
5917290c
CS
275 if (devinfo)
276 err = scsi_dh_handler_attach(sdev, devinfo);
765cbc6d 277 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
4c05ae52 278 device_remove_file(dev, &scsi_dh_state_attr);
765cbc6d
HR
279 scsi_dh_handler_detach(sdev, NULL);
280 }
765cbc6d 281 return err;
a6a8d9f8 282}
a6a8d9f8 283
765cbc6d
HR
284/*
285 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
286 */
287static int scsi_dh_notifier_add(struct device *dev, void *data)
288{
289 struct scsi_device_handler *scsi_dh = data;
290 struct scsi_device *sdev;
291
292 if (!scsi_is_sdev_device(dev))
293 return 0;
294
295 if (!get_device(dev))
296 return 0;
297
298 sdev = to_scsi_device(dev);
299
300 if (device_handler_match(scsi_dh, sdev))
301 scsi_dh_handler_attach(sdev, scsi_dh);
302
303 put_device(dev);
304
305 return 0;
306}
307
308/*
309 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
310 */
a6a8d9f8
CS
311static int scsi_dh_notifier_remove(struct device *dev, void *data)
312{
313 struct scsi_device_handler *scsi_dh = data;
765cbc6d
HR
314 struct scsi_device *sdev;
315
316 if (!scsi_is_sdev_device(dev))
317 return 0;
318
319 if (!get_device(dev))
320 return 0;
321
322 sdev = to_scsi_device(dev);
323
324 scsi_dh_handler_detach(sdev, scsi_dh);
325
326 put_device(dev);
a6a8d9f8 327
a6a8d9f8
CS
328 return 0;
329}
330
765cbc6d
HR
331/*
332 * scsi_register_device_handler - register a device handler personality
333 * module.
334 * @scsi_dh - device handler to be registered.
335 *
336 * Returns 0 on success, -EBUSY if handler already registered.
337 */
338int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
339{
940d7faa 340
765cbc6d
HR
341 if (get_device_handler(scsi_dh->name))
342 return -EBUSY;
343
1f12ffa5
CH
344 if (!scsi_dh->attach || !scsi_dh->detach)
345 return -EINVAL;
346
765cbc6d
HR
347 spin_lock(&list_lock);
348 list_add(&scsi_dh->list, &scsi_dh_list);
349 spin_unlock(&list_lock);
940d7faa 350
765cbc6d
HR
351 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
352 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
353
354 return SCSI_DH_OK;
355}
356EXPORT_SYMBOL_GPL(scsi_register_device_handler);
357
a6a8d9f8
CS
358/*
359 * scsi_unregister_device_handler - register a device handler personality
360 * module.
361 * @scsi_dh - device handler to be unregistered.
362 *
363 * Returns 0 on success, -ENODEV if handler not registered.
364 */
365int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
366{
7c32c7a2 367
765cbc6d
HR
368 if (!get_device_handler(scsi_dh->name))
369 return -ENODEV;
a6a8d9f8
CS
370
371 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
765cbc6d
HR
372 scsi_dh_notifier_remove);
373
a6a8d9f8
CS
374 spin_lock(&list_lock);
375 list_del(&scsi_dh->list);
376 spin_unlock(&list_lock);
765cbc6d 377 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
a6a8d9f8 378
765cbc6d 379 return SCSI_DH_OK;
a6a8d9f8
CS
380}
381EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
382
383/*
384 * scsi_dh_activate - activate the path associated with the scsi_device
385 * corresponding to the given request queue.
3ae31f6a
CS
386 * Returns immediately without waiting for activation to be completed.
387 * @q - Request queue that is associated with the scsi_device to be
388 * activated.
389 * @fn - Function to be called upon completion of the activation.
390 * Function fn is called with data (below) and the error code.
391 * Function fn may be called from the same calling context. So,
392 * do not hold the lock in the caller which may be needed in fn.
393 * @data - data passed to the function fn upon completion.
394 *
a6a8d9f8 395 */
3ae31f6a 396int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
a6a8d9f8
CS
397{
398 int err = 0;
399 unsigned long flags;
400 struct scsi_device *sdev;
401 struct scsi_device_handler *scsi_dh = NULL;
0b839357 402 struct device *dev = NULL;
a6a8d9f8
CS
403
404 spin_lock_irqsave(q->queue_lock, flags);
405 sdev = q->queuedata;
a18a920c
MB
406 if (!sdev) {
407 spin_unlock_irqrestore(q->queue_lock, flags);
408 err = SCSI_DH_NOSYS;
409 if (fn)
410 fn(data, err);
411 return err;
412 }
413
414 if (sdev->scsi_dh_data)
a6a8d9f8 415 scsi_dh = sdev->scsi_dh_data->scsi_dh;
0b839357
MS
416 dev = get_device(&sdev->sdev_gendev);
417 if (!scsi_dh || !dev ||
db422318
MH
418 sdev->sdev_state == SDEV_CANCEL ||
419 sdev->sdev_state == SDEV_DEL)
a6a8d9f8 420 err = SCSI_DH_NOSYS;
db422318
MH
421 if (sdev->sdev_state == SDEV_OFFLINE)
422 err = SCSI_DH_DEV_OFFLINED;
a6a8d9f8
CS
423 spin_unlock_irqrestore(q->queue_lock, flags);
424
db422318
MH
425 if (err) {
426 if (fn)
427 fn(data, err);
0b839357 428 goto out;
db422318 429 }
a6a8d9f8
CS
430
431 if (scsi_dh->activate)
3ae31f6a 432 err = scsi_dh->activate(sdev, fn, data);
0b839357
MS
433out:
434 put_device(dev);
a6a8d9f8
CS
435 return err;
436}
437EXPORT_SYMBOL_GPL(scsi_dh_activate);
438
18ee70c9
CS
439/*
440 * scsi_dh_set_params - set the parameters for the device as per the
441 * string specified in params.
442 * @q - Request queue that is associated with the scsi_device for
443 * which the parameters to be set.
444 * @params - parameters in the following format
445 * "no_of_params\0param1\0param2\0param3\0...\0"
446 * for example, string for 2 parameters with value 10 and 21
447 * is specified as "2\010\021\0".
448 */
449int scsi_dh_set_params(struct request_queue *q, const char *params)
450{
451 int err = -SCSI_DH_NOSYS;
452 unsigned long flags;
453 struct scsi_device *sdev;
454 struct scsi_device_handler *scsi_dh = NULL;
455
456 spin_lock_irqsave(q->queue_lock, flags);
457 sdev = q->queuedata;
458 if (sdev && sdev->scsi_dh_data)
459 scsi_dh = sdev->scsi_dh_data->scsi_dh;
460 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
461 err = 0;
462 spin_unlock_irqrestore(q->queue_lock, flags);
463
464 if (err)
465 return err;
466 err = scsi_dh->set_params(sdev, params);
467 put_device(&sdev->sdev_gendev);
468 return err;
469}
470EXPORT_SYMBOL_GPL(scsi_dh_set_params);
471
a6a8d9f8
CS
472/*
473 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
474 * the given name. FALSE(0) otherwise.
475 * @name - name of the device handler.
476 */
477int scsi_dh_handler_exist(const char *name)
478{
479 return (get_device_handler(name) != NULL);
480}
481EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
482
ae11b1b3 483/*
2a9ab40f 484 * scsi_dh_attach - Attach device handler
7e8a74b1
MS
485 * @q - Request queue that is associated with the scsi_device
486 * the handler should be attached to
ae11b1b3
HR
487 * @name - name of the handler to attach
488 */
489int scsi_dh_attach(struct request_queue *q, const char *name)
490{
491 unsigned long flags;
492 struct scsi_device *sdev;
493 struct scsi_device_handler *scsi_dh;
494 int err = 0;
495
496 scsi_dh = get_device_handler(name);
497 if (!scsi_dh)
498 return -EINVAL;
499
500 spin_lock_irqsave(q->queue_lock, flags);
501 sdev = q->queuedata;
502 if (!sdev || !get_device(&sdev->sdev_gendev))
503 err = -ENODEV;
504 spin_unlock_irqrestore(q->queue_lock, flags);
505
506 if (!err) {
507 err = scsi_dh_handler_attach(sdev, scsi_dh);
ae11b1b3
HR
508 put_device(&sdev->sdev_gendev);
509 }
510 return err;
511}
512EXPORT_SYMBOL_GPL(scsi_dh_attach);
513
514/*
2a9ab40f 515 * scsi_dh_detach - Detach device handler
7e8a74b1
MS
516 * @q - Request queue that is associated with the scsi_device
517 * the handler should be detached from
ae11b1b3
HR
518 *
519 * This function will detach the device handler only
520 * if the sdev is not part of the internal list, ie
521 * if it has been attached manually.
522 */
523void scsi_dh_detach(struct request_queue *q)
524{
525 unsigned long flags;
526 struct scsi_device *sdev;
527 struct scsi_device_handler *scsi_dh = NULL;
528
529 spin_lock_irqsave(q->queue_lock, flags);
530 sdev = q->queuedata;
531 if (!sdev || !get_device(&sdev->sdev_gendev))
532 sdev = NULL;
533 spin_unlock_irqrestore(q->queue_lock, flags);
534
535 if (!sdev)
536 return;
537
538 if (sdev->scsi_dh_data) {
ae11b1b3 539 scsi_dh = sdev->scsi_dh_data->scsi_dh;
6c10db72 540 scsi_dh_handler_detach(sdev, scsi_dh);
ae11b1b3
HR
541 }
542 put_device(&sdev->sdev_gendev);
543}
544EXPORT_SYMBOL_GPL(scsi_dh_detach);
545
7e8a74b1
MS
546/*
547 * scsi_dh_attached_handler_name - Get attached device handler's name
548 * @q - Request queue that is associated with the scsi_device
549 * that may have a device handler attached
550 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
551 *
552 * Returns name of attached handler, NULL if no handler is attached.
553 * Caller must take care to free the returned string.
554 */
555const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
556{
557 unsigned long flags;
558 struct scsi_device *sdev;
559 const char *handler_name = NULL;
560
561 spin_lock_irqsave(q->queue_lock, flags);
562 sdev = q->queuedata;
563 if (!sdev || !get_device(&sdev->sdev_gendev))
564 sdev = NULL;
565 spin_unlock_irqrestore(q->queue_lock, flags);
566
567 if (!sdev)
568 return NULL;
569
570 if (sdev->scsi_dh_data)
571 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
572
573 put_device(&sdev->sdev_gendev);
574 return handler_name;
575}
576EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
577
765cbc6d
HR
578static struct notifier_block scsi_dh_nb = {
579 .notifier_call = scsi_dh_notifier
580};
581
582static int __init scsi_dh_init(void)
583{
584 int r;
585
586 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
587
4c05ae52
HR
588 if (!r)
589 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
590 scsi_dh_sysfs_attr_add);
591
765cbc6d
HR
592 return r;
593}
594
595static void __exit scsi_dh_exit(void)
596{
4c05ae52
HR
597 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
598 scsi_dh_sysfs_attr_remove);
765cbc6d
HR
599 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
600}
601
602module_init(scsi_dh_init);
603module_exit(scsi_dh_exit);
604
a6a8d9f8
CS
605MODULE_DESCRIPTION("SCSI device handler");
606MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
607MODULE_LICENSE("GPL");