1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/slab.h>
25#include <linux/module.h>
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) {
38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
39 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
47
48
49
50
51
52
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
70
71
72
73
74
75
76
77
78
79static struct scsi_device_handler *
80device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
82{
83 struct scsi_device_handler *found_dh;
84
85 found_dh = device_handler_match_function(sdev);
86
87 if (scsi_dh && found_dh != scsi_dh)
88 found_dh = NULL;
89
90 return found_dh;
91}
92
93
94
95
96
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)
105 err = -EBUSY;
106 else
107 kref_get(&sdev->scsi_dh_data->kref);
108 } else if (scsi_dh->attach) {
109 err = scsi_dh->attach(sdev);
110 if (!err) {
111 kref_init(&sdev->scsi_dh_data->kref);
112 sdev->scsi_dh_data->sdev = sdev;
113 }
114 }
115 return err;
116}
117
118static void __detach_handler (struct kref *kref)
119{
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);
122}
123
124
125
126
127
128
129
130
131
132static void scsi_dh_handler_detach(struct scsi_device *sdev,
133 struct scsi_device_handler *scsi_dh)
134{
135 if (!sdev->scsi_dh_data)
136 return;
137
138 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
139 return;
140
141 if (!scsi_dh)
142 scsi_dh = sdev->scsi_dh_data->scsi_dh;
143
144 if (scsi_dh && scsi_dh->detach)
145 kref_put(&sdev->scsi_dh_data->kref, __detach_handler);
146}
147
148
149
150
151static ssize_t
152store_dh_state(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t count)
154{
155 struct scsi_device *sdev = to_scsi_device(dev);
156 struct scsi_device_handler *scsi_dh;
157 int err = -EINVAL;
158
159 if (sdev->sdev_state == SDEV_CANCEL ||
160 sdev->sdev_state == SDEV_DEL)
161 return -ENODEV;
162
163 if (!sdev->scsi_dh_data) {
164
165
166
167 if (!(scsi_dh = get_device_handler(buf)))
168 return err;
169 err = scsi_dh_handler_attach(sdev, scsi_dh);
170 } else {
171 scsi_dh = sdev->scsi_dh_data->scsi_dh;
172 if (!strncmp(buf, "detach", 6)) {
173
174
175
176 scsi_dh_handler_detach(sdev, scsi_dh);
177 err = 0;
178 } else if (!strncmp(buf, "activate", 8)) {
179
180
181
182 if (scsi_dh->activate)
183 err = scsi_dh->activate(sdev, NULL, NULL);
184 else
185 err = 0;
186 }
187 }
188
189 return err<0?err:count;
190}
191
192static ssize_t
193show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
194{
195 struct scsi_device *sdev = to_scsi_device(dev);
196
197 if (!sdev->scsi_dh_data)
198 return snprintf(buf, 20, "detached\n");
199
200 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
201}
202
203static struct device_attribute scsi_dh_state_attr =
204 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
205 store_dh_state);
206
207
208
209
210static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
211{
212 struct scsi_device *sdev;
213 int err;
214
215 if (!scsi_is_sdev_device(dev))
216 return 0;
217
218 sdev = to_scsi_device(dev);
219
220 err = device_create_file(&sdev->sdev_gendev,
221 &scsi_dh_state_attr);
222
223 return 0;
224}
225
226
227
228
229static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
230{
231 struct scsi_device *sdev;
232
233 if (!scsi_is_sdev_device(dev))
234 return 0;
235
236 sdev = to_scsi_device(dev);
237
238 device_remove_file(&sdev->sdev_gendev,
239 &scsi_dh_state_attr);
240
241 return 0;
242}
243
244
245
246
247static int scsi_dh_notifier(struct notifier_block *nb,
248 unsigned long action, void *data)
249{
250 struct device *dev = data;
251 struct scsi_device *sdev;
252 int err = 0;
253 struct scsi_device_handler *devinfo = NULL;
254
255 if (!scsi_is_sdev_device(dev))
256 return 0;
257
258 sdev = to_scsi_device(dev);
259
260 if (action == BUS_NOTIFY_ADD_DEVICE) {
261 err = device_create_file(dev, &scsi_dh_state_attr);
262
263 devinfo = device_handler_match(NULL, sdev);
264 if (devinfo)
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);
269 }
270 return err;
271}
272
273
274
275
276static int scsi_dh_notifier_add(struct device *dev, void *data)
277{
278 struct scsi_device_handler *scsi_dh = data;
279 struct scsi_device *sdev;
280
281 if (!scsi_is_sdev_device(dev))
282 return 0;
283
284 if (!get_device(dev))
285 return 0;
286
287 sdev = to_scsi_device(dev);
288
289 if (device_handler_match(scsi_dh, sdev))
290 scsi_dh_handler_attach(sdev, scsi_dh);
291
292 put_device(dev);
293
294 return 0;
295}
296
297
298
299
300static int scsi_dh_notifier_remove(struct device *dev, void *data)
301{
302 struct scsi_device_handler *scsi_dh = data;
303 struct scsi_device *sdev;
304
305 if (!scsi_is_sdev_device(dev))
306 return 0;
307
308 if (!get_device(dev))
309 return 0;
310
311 sdev = to_scsi_device(dev);
312
313 scsi_dh_handler_detach(sdev, scsi_dh);
314
315 put_device(dev);
316
317 return 0;
318}
319
320
321
322
323
324
325
326
327int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
328{
329
330 if (get_device_handler(scsi_dh->name))
331 return -EBUSY;
332
333 spin_lock(&list_lock);
334 list_add(&scsi_dh->list, &scsi_dh_list);
335 spin_unlock(&list_lock);
336
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);
339
340 return SCSI_DH_OK;
341}
342EXPORT_SYMBOL_GPL(scsi_register_device_handler);
343
344
345
346
347
348
349
350
351int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
352{
353
354 if (!get_device_handler(scsi_dh->name))
355 return -ENODEV;
356
357 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
358 scsi_dh_notifier_remove);
359
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);
364
365 return SCSI_DH_OK;
366}
367EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
383{
384 int err = 0;
385 unsigned long flags;
386 struct scsi_device *sdev;
387 struct scsi_device_handler *scsi_dh = NULL;
388 struct device *dev = NULL;
389
390 spin_lock_irqsave(q->queue_lock, flags);
391 sdev = q->queuedata;
392 if (!sdev) {
393 spin_unlock_irqrestore(q->queue_lock, flags);
394 err = SCSI_DH_NOSYS;
395 if (fn)
396 fn(data, err);
397 return err;
398 }
399
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)
406 err = SCSI_DH_NOSYS;
407 if (sdev->sdev_state == SDEV_OFFLINE)
408 err = SCSI_DH_DEV_OFFLINED;
409 spin_unlock_irqrestore(q->queue_lock, flags);
410
411 if (err) {
412 if (fn)
413 fn(data, err);
414 goto out;
415 }
416
417 if (scsi_dh->activate)
418 err = scsi_dh->activate(sdev, fn, data);
419out:
420 put_device(dev);
421 return err;
422}
423EXPORT_SYMBOL_GPL(scsi_dh_activate);
424
425
426
427
428
429
430
431
432
433
434
435int scsi_dh_set_params(struct request_queue *q, const char *params)
436{
437 int err = -SCSI_DH_NOSYS;
438 unsigned long flags;
439 struct scsi_device *sdev;
440 struct scsi_device_handler *scsi_dh = NULL;
441
442 spin_lock_irqsave(q->queue_lock, flags);
443 sdev = q->queuedata;
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))
447 err = 0;
448 spin_unlock_irqrestore(q->queue_lock, flags);
449
450 if (err)
451 return err;
452 err = scsi_dh->set_params(sdev, params);
453 put_device(&sdev->sdev_gendev);
454 return err;
455}
456EXPORT_SYMBOL_GPL(scsi_dh_set_params);
457
458
459
460
461
462
463int scsi_dh_handler_exist(const char *name)
464{
465 return (get_device_handler(name) != NULL);
466}
467EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
468
469
470
471
472
473
474
475int scsi_dh_attach(struct request_queue *q, const char *name)
476{
477 unsigned long flags;
478 struct scsi_device *sdev;
479 struct scsi_device_handler *scsi_dh;
480 int err = 0;
481
482 scsi_dh = get_device_handler(name);
483 if (!scsi_dh)
484 return -EINVAL;
485
486 spin_lock_irqsave(q->queue_lock, flags);
487 sdev = q->queuedata;
488 if (!sdev || !get_device(&sdev->sdev_gendev))
489 err = -ENODEV;
490 spin_unlock_irqrestore(q->queue_lock, flags);
491
492 if (!err) {
493 err = scsi_dh_handler_attach(sdev, scsi_dh);
494 put_device(&sdev->sdev_gendev);
495 }
496 return err;
497}
498EXPORT_SYMBOL_GPL(scsi_dh_attach);
499
500
501
502
503
504
505
506
507
508
509void scsi_dh_detach(struct request_queue *q)
510{
511 unsigned long flags;
512 struct scsi_device *sdev;
513 struct scsi_device_handler *scsi_dh = NULL;
514
515 spin_lock_irqsave(q->queue_lock, flags);
516 sdev = q->queuedata;
517 if (!sdev || !get_device(&sdev->sdev_gendev))
518 sdev = NULL;
519 spin_unlock_irqrestore(q->queue_lock, flags);
520
521 if (!sdev)
522 return;
523
524 if (sdev->scsi_dh_data) {
525 scsi_dh = sdev->scsi_dh_data->scsi_dh;
526 scsi_dh_handler_detach(sdev, scsi_dh);
527 }
528 put_device(&sdev->sdev_gendev);
529}
530EXPORT_SYMBOL_GPL(scsi_dh_detach);
531
532
533
534
535
536
537
538
539
540
541const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
542{
543 unsigned long flags;
544 struct scsi_device *sdev;
545 const char *handler_name = NULL;
546
547 spin_lock_irqsave(q->queue_lock, flags);
548 sdev = q->queuedata;
549 if (!sdev || !get_device(&sdev->sdev_gendev))
550 sdev = NULL;
551 spin_unlock_irqrestore(q->queue_lock, flags);
552
553 if (!sdev)
554 return NULL;
555
556 if (sdev->scsi_dh_data)
557 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
558
559 put_device(&sdev->sdev_gendev);
560 return handler_name;
561}
562EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
563
564static struct notifier_block scsi_dh_nb = {
565 .notifier_call = scsi_dh_notifier
566};
567
568static int __init scsi_dh_init(void)
569{
570 int r;
571
572 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
573
574 if (!r)
575 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
576 scsi_dh_sysfs_attr_add);
577
578 return r;
579}
580
581static void __exit scsi_dh_exit(void)
582{
583 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
584 scsi_dh_sysfs_attr_remove);
585 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
586}
587
588module_init(scsi_dh_init);
589module_exit(scsi_dh_exit);
590
591MODULE_DESCRIPTION("SCSI device handler");
592MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
593MODULE_LICENSE("GPL");
594