Merge git://git.kernel.org/pub/scm/linux/kernel/git/jk/spufs
[pandora-kernel.git] / drivers / s390 / cio / device.c
1 /*
2  *  drivers/s390/cio/device.c
3  *  bus driver for ccw devices
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *                       IBM Corporation
7  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *               Cornelia Huck (cornelia.huck@de.ibm.com)
9  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
10  */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20 #include <linux/timer.h>
21
22 #include <asm/ccwdev.h>
23 #include <asm/cio.h>
24 #include <asm/param.h>          /* HZ */
25 #include <asm/cmb.h>
26
27 #include "cio.h"
28 #include "cio_debug.h"
29 #include "css.h"
30 #include "device.h"
31 #include "ioasm.h"
32 #include "io_sch.h"
33
34 static struct timer_list recovery_timer;
35 static DEFINE_SPINLOCK(recovery_lock);
36 static int recovery_phase;
37 static const unsigned long recovery_delay[] = { 3, 30, 300 };
38
39 /******************* bus type handling ***********************/
40
41 /* The Linux driver model distinguishes between a bus type and
42  * the bus itself. Of course we only have one channel
43  * subsystem driver and one channel system per machine, but
44  * we still use the abstraction. T.R. says it's a good idea. */
45 static int
46 ccw_bus_match (struct device * dev, struct device_driver * drv)
47 {
48         struct ccw_device *cdev = to_ccwdev(dev);
49         struct ccw_driver *cdrv = to_ccwdrv(drv);
50         const struct ccw_device_id *ids = cdrv->ids, *found;
51
52         if (!ids)
53                 return 0;
54
55         found = ccw_device_id_match(ids, &cdev->id);
56         if (!found)
57                 return 0;
58
59         cdev->id.driver_info = found->driver_info;
60
61         return 1;
62 }
63
64 /* Store modalias string delimited by prefix/suffix string into buffer with
65  * specified size. Return length of resulting string (excluding trailing '\0')
66  * even if string doesn't fit buffer (snprintf semantics). */
67 static int snprint_alias(char *buf, size_t size,
68                          struct ccw_device_id *id, const char *suffix)
69 {
70         int len;
71
72         len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
73         if (len > size)
74                 return len;
75         buf += len;
76         size -= len;
77
78         if (id->dev_type != 0)
79                 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
80                                 id->dev_model, suffix);
81         else
82                 len += snprintf(buf, size, "dtdm%s", suffix);
83
84         return len;
85 }
86
87 /* Set up environment variables for ccw device uevent. Return 0 on success,
88  * non-zero otherwise. */
89 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
90 {
91         struct ccw_device *cdev = to_ccwdev(dev);
92         struct ccw_device_id *id = &(cdev->id);
93         int ret;
94         char modalias_buf[30];
95
96         /* CU_TYPE= */
97         ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
98         if (ret)
99                 return ret;
100
101         /* CU_MODEL= */
102         ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
103         if (ret)
104                 return ret;
105
106         /* The next two can be zero, that's ok for us */
107         /* DEV_TYPE= */
108         ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
109         if (ret)
110                 return ret;
111
112         /* DEV_MODEL= */
113         ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
114         if (ret)
115                 return ret;
116
117         /* MODALIAS=  */
118         snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
119         ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
120         return ret;
121 }
122
123 struct bus_type ccw_bus_type;
124
125 static void io_subchannel_irq(struct subchannel *);
126 static int io_subchannel_probe(struct subchannel *);
127 static int io_subchannel_remove(struct subchannel *);
128 static int io_subchannel_notify(struct subchannel *, int);
129 static void io_subchannel_verify(struct subchannel *);
130 static void io_subchannel_ioterm(struct subchannel *);
131 static void io_subchannel_shutdown(struct subchannel *);
132
133 static struct css_driver io_subchannel_driver = {
134         .owner = THIS_MODULE,
135         .subchannel_type = SUBCHANNEL_TYPE_IO,
136         .name = "io_subchannel",
137         .irq = io_subchannel_irq,
138         .notify = io_subchannel_notify,
139         .verify = io_subchannel_verify,
140         .termination = io_subchannel_ioterm,
141         .probe = io_subchannel_probe,
142         .remove = io_subchannel_remove,
143         .shutdown = io_subchannel_shutdown,
144 };
145
146 struct workqueue_struct *ccw_device_work;
147 struct workqueue_struct *ccw_device_notify_work;
148 wait_queue_head_t ccw_device_init_wq;
149 atomic_t ccw_device_init_count;
150
151 static void recovery_func(unsigned long data);
152
153 static int __init
154 init_ccw_bus_type (void)
155 {
156         int ret;
157
158         init_waitqueue_head(&ccw_device_init_wq);
159         atomic_set(&ccw_device_init_count, 0);
160         setup_timer(&recovery_timer, recovery_func, 0);
161
162         ccw_device_work = create_singlethread_workqueue("cio");
163         if (!ccw_device_work)
164                 return -ENOMEM; /* FIXME: better errno ? */
165         ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
166         if (!ccw_device_notify_work) {
167                 ret = -ENOMEM; /* FIXME: better errno ? */
168                 goto out_err;
169         }
170         slow_path_wq = create_singlethread_workqueue("kslowcrw");
171         if (!slow_path_wq) {
172                 ret = -ENOMEM; /* FIXME: better errno ? */
173                 goto out_err;
174         }
175         if ((ret = bus_register (&ccw_bus_type)))
176                 goto out_err;
177
178         ret = css_driver_register(&io_subchannel_driver);
179         if (ret)
180                 goto out_err;
181
182         wait_event(ccw_device_init_wq,
183                    atomic_read(&ccw_device_init_count) == 0);
184         flush_workqueue(ccw_device_work);
185         return 0;
186 out_err:
187         if (ccw_device_work)
188                 destroy_workqueue(ccw_device_work);
189         if (ccw_device_notify_work)
190                 destroy_workqueue(ccw_device_notify_work);
191         if (slow_path_wq)
192                 destroy_workqueue(slow_path_wq);
193         return ret;
194 }
195
196 static void __exit
197 cleanup_ccw_bus_type (void)
198 {
199         css_driver_unregister(&io_subchannel_driver);
200         bus_unregister(&ccw_bus_type);
201         destroy_workqueue(ccw_device_notify_work);
202         destroy_workqueue(ccw_device_work);
203 }
204
205 subsys_initcall(init_ccw_bus_type);
206 module_exit(cleanup_ccw_bus_type);
207
208 /************************ device handling **************************/
209
210 /*
211  * A ccw_device has some interfaces in sysfs in addition to the
212  * standard ones.
213  * The following entries are designed to export the information which
214  * resided in 2.4 in /proc/subchannels. Subchannel and device number
215  * are obvious, so they don't have an entry :)
216  * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
217  */
218 static ssize_t
219 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
220 {
221         struct subchannel *sch = to_subchannel(dev);
222         struct chsc_ssd_info *ssd = &sch->ssd_info;
223         ssize_t ret = 0;
224         int chp;
225         int mask;
226
227         for (chp = 0; chp < 8; chp++) {
228                 mask = 0x80 >> chp;
229                 if (ssd->path_mask & mask)
230                         ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
231                 else
232                         ret += sprintf(buf + ret, "00 ");
233         }
234         ret += sprintf (buf+ret, "\n");
235         return min((ssize_t)PAGE_SIZE, ret);
236 }
237
238 static ssize_t
239 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
240 {
241         struct subchannel *sch = to_subchannel(dev);
242         struct pmcw *pmcw = &sch->schib.pmcw;
243
244         return sprintf (buf, "%02x %02x %02x\n",
245                         pmcw->pim, pmcw->pam, pmcw->pom);
246 }
247
248 static ssize_t
249 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
250 {
251         struct ccw_device *cdev = to_ccwdev(dev);
252         struct ccw_device_id *id = &(cdev->id);
253
254         if (id->dev_type != 0)
255                 return sprintf(buf, "%04x/%02x\n",
256                                 id->dev_type, id->dev_model);
257         else
258                 return sprintf(buf, "n/a\n");
259 }
260
261 static ssize_t
262 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
263 {
264         struct ccw_device *cdev = to_ccwdev(dev);
265         struct ccw_device_id *id = &(cdev->id);
266
267         return sprintf(buf, "%04x/%02x\n",
268                        id->cu_type, id->cu_model);
269 }
270
271 static ssize_t
272 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
273 {
274         struct ccw_device *cdev = to_ccwdev(dev);
275         struct ccw_device_id *id = &(cdev->id);
276         int len;
277
278         len = snprint_alias(buf, PAGE_SIZE, id, "\n");
279
280         return len > PAGE_SIZE ? PAGE_SIZE : len;
281 }
282
283 static ssize_t
284 online_show (struct device *dev, struct device_attribute *attr, char *buf)
285 {
286         struct ccw_device *cdev = to_ccwdev(dev);
287
288         return sprintf(buf, cdev->online ? "1\n" : "0\n");
289 }
290
291 int ccw_device_is_orphan(struct ccw_device *cdev)
292 {
293         return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
294 }
295
296 static void ccw_device_unregister(struct ccw_device *cdev)
297 {
298         if (test_and_clear_bit(1, &cdev->private->registered))
299                 device_del(&cdev->dev);
300 }
301
302 static void ccw_device_remove_orphan_cb(struct device *dev)
303 {
304         struct ccw_device *cdev = to_ccwdev(dev);
305
306         ccw_device_unregister(cdev);
307         put_device(&cdev->dev);
308 }
309
310 static void ccw_device_remove_sch_cb(struct device *dev)
311 {
312         struct subchannel *sch;
313
314         sch = to_subchannel(dev);
315         css_sch_device_unregister(sch);
316         /* Reset intparm to zeroes. */
317         sch->schib.pmcw.intparm = 0;
318         cio_modify(sch);
319         put_device(&sch->dev);
320 }
321
322 static void
323 ccw_device_remove_disconnected(struct ccw_device *cdev)
324 {
325         unsigned long flags;
326         int rc;
327
328         /*
329          * Forced offline in disconnected state means
330          * 'throw away device'.
331          */
332         if (ccw_device_is_orphan(cdev)) {
333                 /*
334                  * Deregister ccw device.
335                  * Unfortunately, we cannot do this directly from the
336                  * attribute method.
337                  */
338                 spin_lock_irqsave(cdev->ccwlock, flags);
339                 cdev->private->state = DEV_STATE_NOT_OPER;
340                 spin_unlock_irqrestore(cdev->ccwlock, flags);
341                 rc = device_schedule_callback(&cdev->dev,
342                                               ccw_device_remove_orphan_cb);
343                 if (rc)
344                         CIO_MSG_EVENT(2, "Couldn't unregister orphan "
345                                       "0.%x.%04x\n",
346                                       cdev->private->dev_id.ssid,
347                                       cdev->private->dev_id.devno);
348                 return;
349         }
350         /* Deregister subchannel, which will kill the ccw device. */
351         rc = device_schedule_callback(cdev->dev.parent,
352                                       ccw_device_remove_sch_cb);
353         if (rc)
354                 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
355                               "0.%x.%04x\n",
356                               cdev->private->dev_id.ssid,
357                               cdev->private->dev_id.devno);
358 }
359
360 /**
361  * ccw_device_set_offline() - disable a ccw device for I/O
362  * @cdev: target ccw device
363  *
364  * This function calls the driver's set_offline() function for @cdev, if
365  * given, and then disables @cdev.
366  * Returns:
367  *   %0 on success and a negative error value on failure.
368  * Context:
369  *  enabled, ccw device lock not held
370  */
371 int ccw_device_set_offline(struct ccw_device *cdev)
372 {
373         int ret;
374
375         if (!cdev)
376                 return -ENODEV;
377         if (!cdev->online || !cdev->drv)
378                 return -EINVAL;
379
380         if (cdev->drv->set_offline) {
381                 ret = cdev->drv->set_offline(cdev);
382                 if (ret != 0)
383                         return ret;
384         }
385         cdev->online = 0;
386         spin_lock_irq(cdev->ccwlock);
387         ret = ccw_device_offline(cdev);
388         if (ret == -ENODEV) {
389                 if (cdev->private->state != DEV_STATE_NOT_OPER) {
390                         cdev->private->state = DEV_STATE_OFFLINE;
391                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
392                 }
393                 spin_unlock_irq(cdev->ccwlock);
394                 return ret;
395         }
396         spin_unlock_irq(cdev->ccwlock);
397         if (ret == 0)
398                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
399         else {
400                 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
401                               "device 0.%x.%04x\n",
402                               ret, cdev->private->dev_id.ssid,
403                               cdev->private->dev_id.devno);
404                 cdev->online = 1;
405         }
406         return ret;
407 }
408
409 /**
410  * ccw_device_set_online() - enable a ccw device for I/O
411  * @cdev: target ccw device
412  *
413  * This function first enables @cdev and then calls the driver's set_online()
414  * function for @cdev, if given. If set_online() returns an error, @cdev is
415  * disabled again.
416  * Returns:
417  *   %0 on success and a negative error value on failure.
418  * Context:
419  *  enabled, ccw device lock not held
420  */
421 int ccw_device_set_online(struct ccw_device *cdev)
422 {
423         int ret;
424
425         if (!cdev)
426                 return -ENODEV;
427         if (cdev->online || !cdev->drv)
428                 return -EINVAL;
429
430         spin_lock_irq(cdev->ccwlock);
431         ret = ccw_device_online(cdev);
432         spin_unlock_irq(cdev->ccwlock);
433         if (ret == 0)
434                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
435         else {
436                 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
437                               "device 0.%x.%04x\n",
438                               ret, cdev->private->dev_id.ssid,
439                               cdev->private->dev_id.devno);
440                 return ret;
441         }
442         if (cdev->private->state != DEV_STATE_ONLINE)
443                 return -ENODEV;
444         if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
445                 cdev->online = 1;
446                 return 0;
447         }
448         spin_lock_irq(cdev->ccwlock);
449         ret = ccw_device_offline(cdev);
450         spin_unlock_irq(cdev->ccwlock);
451         if (ret == 0)
452                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
453         else
454                 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
455                               "device 0.%x.%04x\n",
456                               ret, cdev->private->dev_id.ssid,
457                               cdev->private->dev_id.devno);
458         return (ret == 0) ? -ENODEV : ret;
459 }
460
461 static void online_store_handle_offline(struct ccw_device *cdev)
462 {
463         if (cdev->private->state == DEV_STATE_DISCONNECTED)
464                 ccw_device_remove_disconnected(cdev);
465         else if (cdev->drv && cdev->drv->set_offline)
466                 ccw_device_set_offline(cdev);
467 }
468
469 static int online_store_recog_and_online(struct ccw_device *cdev)
470 {
471         int ret;
472
473         /* Do device recognition, if needed. */
474         if (cdev->id.cu_type == 0) {
475                 ret = ccw_device_recognition(cdev);
476                 if (ret) {
477                         CIO_MSG_EVENT(0, "Couldn't start recognition "
478                                       "for device 0.%x.%04x (ret=%d)\n",
479                                       cdev->private->dev_id.ssid,
480                                       cdev->private->dev_id.devno, ret);
481                         return ret;
482                 }
483                 wait_event(cdev->private->wait_q,
484                            cdev->private->flags.recog_done);
485         }
486         if (cdev->drv && cdev->drv->set_online)
487                 ccw_device_set_online(cdev);
488         return 0;
489 }
490 static void online_store_handle_online(struct ccw_device *cdev, int force)
491 {
492         int ret;
493
494         ret = online_store_recog_and_online(cdev);
495         if (ret)
496                 return;
497         if (force && cdev->private->state == DEV_STATE_BOXED) {
498                 ret = ccw_device_stlck(cdev);
499                 if (ret) {
500                         dev_warn(&cdev->dev,
501                                  "ccw_device_stlck returned %d!\n", ret);
502                         return;
503                 }
504                 if (cdev->id.cu_type == 0)
505                         cdev->private->state = DEV_STATE_NOT_OPER;
506                 online_store_recog_and_online(cdev);
507         }
508
509 }
510
511 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
512                              const char *buf, size_t count)
513 {
514         struct ccw_device *cdev = to_ccwdev(dev);
515         int i, force;
516         char *tmp;
517
518         if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
519                 return -EAGAIN;
520
521         if (cdev->drv && !try_module_get(cdev->drv->owner)) {
522                 atomic_set(&cdev->private->onoff, 0);
523                 return -EINVAL;
524         }
525         if (!strncmp(buf, "force\n", count)) {
526                 force = 1;
527                 i = 1;
528         } else {
529                 force = 0;
530                 i = simple_strtoul(buf, &tmp, 16);
531         }
532
533         switch (i) {
534         case 0:
535                 online_store_handle_offline(cdev);
536                 break;
537         case 1:
538                 online_store_handle_online(cdev, force);
539                 break;
540         default:
541                 count = -EINVAL;
542         }
543         if (cdev->drv)
544                 module_put(cdev->drv->owner);
545         atomic_set(&cdev->private->onoff, 0);
546         return count;
547 }
548
549 static ssize_t
550 available_show (struct device *dev, struct device_attribute *attr, char *buf)
551 {
552         struct ccw_device *cdev = to_ccwdev(dev);
553         struct subchannel *sch;
554
555         if (ccw_device_is_orphan(cdev))
556                 return sprintf(buf, "no device\n");
557         switch (cdev->private->state) {
558         case DEV_STATE_BOXED:
559                 return sprintf(buf, "boxed\n");
560         case DEV_STATE_DISCONNECTED:
561         case DEV_STATE_DISCONNECTED_SENSE_ID:
562         case DEV_STATE_NOT_OPER:
563                 sch = to_subchannel(dev->parent);
564                 if (!sch->lpm)
565                         return sprintf(buf, "no path\n");
566                 else
567                         return sprintf(buf, "no device\n");
568         default:
569                 /* All other states considered fine. */
570                 return sprintf(buf, "good\n");
571         }
572 }
573
574 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
575 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
576 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
577 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
578 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
579 static DEVICE_ATTR(online, 0644, online_show, online_store);
580 static DEVICE_ATTR(availability, 0444, available_show, NULL);
581
582 static struct attribute * subch_attrs[] = {
583         &dev_attr_chpids.attr,
584         &dev_attr_pimpampom.attr,
585         NULL,
586 };
587
588 static struct attribute_group subch_attr_group = {
589         .attrs = subch_attrs,
590 };
591
592 struct attribute_group *subch_attr_groups[] = {
593         &subch_attr_group,
594         NULL,
595 };
596
597 static struct attribute * ccwdev_attrs[] = {
598         &dev_attr_devtype.attr,
599         &dev_attr_cutype.attr,
600         &dev_attr_modalias.attr,
601         &dev_attr_online.attr,
602         &dev_attr_cmb_enable.attr,
603         &dev_attr_availability.attr,
604         NULL,
605 };
606
607 static struct attribute_group ccwdev_attr_group = {
608         .attrs = ccwdev_attrs,
609 };
610
611 static struct attribute_group *ccwdev_attr_groups[] = {
612         &ccwdev_attr_group,
613         NULL,
614 };
615
616 /* this is a simple abstraction for device_register that sets the
617  * correct bus type and adds the bus specific files */
618 static int ccw_device_register(struct ccw_device *cdev)
619 {
620         struct device *dev = &cdev->dev;
621         int ret;
622
623         dev->bus = &ccw_bus_type;
624
625         if ((ret = device_add(dev)))
626                 return ret;
627
628         set_bit(1, &cdev->private->registered);
629         return ret;
630 }
631
632 struct match_data {
633         struct ccw_dev_id dev_id;
634         struct ccw_device * sibling;
635 };
636
637 static int
638 match_devno(struct device * dev, void * data)
639 {
640         struct match_data * d = data;
641         struct ccw_device * cdev;
642
643         cdev = to_ccwdev(dev);
644         if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
645             !ccw_device_is_orphan(cdev) &&
646             ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
647             (cdev != d->sibling))
648                 return 1;
649         return 0;
650 }
651
652 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
653                                                      struct ccw_device *sibling)
654 {
655         struct device *dev;
656         struct match_data data;
657
658         data.dev_id = *dev_id;
659         data.sibling = sibling;
660         dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
661
662         return dev ? to_ccwdev(dev) : NULL;
663 }
664
665 static int match_orphan(struct device *dev, void *data)
666 {
667         struct ccw_dev_id *dev_id;
668         struct ccw_device *cdev;
669
670         dev_id = data;
671         cdev = to_ccwdev(dev);
672         return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
673 }
674
675 static struct ccw_device *
676 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
677                               struct ccw_dev_id *dev_id)
678 {
679         struct device *dev;
680
681         dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
682                                 match_orphan);
683
684         return dev ? to_ccwdev(dev) : NULL;
685 }
686
687 static void
688 ccw_device_add_changed(struct work_struct *work)
689 {
690         struct ccw_device_private *priv;
691         struct ccw_device *cdev;
692
693         priv = container_of(work, struct ccw_device_private, kick_work);
694         cdev = priv->cdev;
695         if (device_add(&cdev->dev)) {
696                 put_device(&cdev->dev);
697                 return;
698         }
699         set_bit(1, &cdev->private->registered);
700 }
701
702 void ccw_device_do_unreg_rereg(struct work_struct *work)
703 {
704         struct ccw_device_private *priv;
705         struct ccw_device *cdev;
706         struct subchannel *sch;
707
708         priv = container_of(work, struct ccw_device_private, kick_work);
709         cdev = priv->cdev;
710         sch = to_subchannel(cdev->dev.parent);
711
712         ccw_device_unregister(cdev);
713         PREPARE_WORK(&cdev->private->kick_work,
714                      ccw_device_add_changed);
715         queue_work(ccw_device_work, &cdev->private->kick_work);
716 }
717
718 static void
719 ccw_device_release(struct device *dev)
720 {
721         struct ccw_device *cdev;
722
723         cdev = to_ccwdev(dev);
724         kfree(cdev->private);
725         kfree(cdev);
726 }
727
728 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
729 {
730         struct ccw_device *cdev;
731
732         cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
733         if (cdev) {
734                 cdev->private = kzalloc(sizeof(struct ccw_device_private),
735                                         GFP_KERNEL | GFP_DMA);
736                 if (cdev->private)
737                         return cdev;
738         }
739         kfree(cdev);
740         return ERR_PTR(-ENOMEM);
741 }
742
743 static int io_subchannel_initialize_dev(struct subchannel *sch,
744                                         struct ccw_device *cdev)
745 {
746         cdev->private->cdev = cdev;
747         atomic_set(&cdev->private->onoff, 0);
748         cdev->dev.parent = &sch->dev;
749         cdev->dev.release = ccw_device_release;
750         INIT_WORK(&cdev->private->kick_work, NULL);
751         cdev->dev.groups = ccwdev_attr_groups;
752         /* Do first half of device_register. */
753         device_initialize(&cdev->dev);
754         if (!get_device(&sch->dev)) {
755                 if (cdev->dev.release)
756                         cdev->dev.release(&cdev->dev);
757                 return -ENODEV;
758         }
759         return 0;
760 }
761
762 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
763 {
764         struct ccw_device *cdev;
765         int ret;
766
767         cdev = io_subchannel_allocate_dev(sch);
768         if (!IS_ERR(cdev)) {
769                 ret = io_subchannel_initialize_dev(sch, cdev);
770                 if (ret) {
771                         kfree(cdev);
772                         cdev = ERR_PTR(ret);
773                 }
774         }
775         return cdev;
776 }
777
778 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
779
780 static void sch_attach_device(struct subchannel *sch,
781                               struct ccw_device *cdev)
782 {
783         css_update_ssd_info(sch);
784         spin_lock_irq(sch->lock);
785         sch_set_cdev(sch, cdev);
786         cdev->private->schid = sch->schid;
787         cdev->ccwlock = sch->lock;
788         device_trigger_reprobe(sch);
789         spin_unlock_irq(sch->lock);
790 }
791
792 static void sch_attach_disconnected_device(struct subchannel *sch,
793                                            struct ccw_device *cdev)
794 {
795         struct subchannel *other_sch;
796         int ret;
797
798         other_sch = to_subchannel(get_device(cdev->dev.parent));
799         ret = device_move(&cdev->dev, &sch->dev);
800         if (ret) {
801                 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
802                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
803                               cdev->private->dev_id.devno, ret);
804                 put_device(&other_sch->dev);
805                 return;
806         }
807         sch_set_cdev(other_sch, NULL);
808         /* No need to keep a subchannel without ccw device around. */
809         css_sch_device_unregister(other_sch);
810         put_device(&other_sch->dev);
811         sch_attach_device(sch, cdev);
812 }
813
814 static void sch_attach_orphaned_device(struct subchannel *sch,
815                                        struct ccw_device *cdev)
816 {
817         int ret;
818
819         /* Try to move the ccw device to its new subchannel. */
820         ret = device_move(&cdev->dev, &sch->dev);
821         if (ret) {
822                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
823                               "failed (ret=%d)!\n",
824                               cdev->private->dev_id.ssid,
825                               cdev->private->dev_id.devno, ret);
826                 return;
827         }
828         sch_attach_device(sch, cdev);
829 }
830
831 static void sch_create_and_recog_new_device(struct subchannel *sch)
832 {
833         struct ccw_device *cdev;
834
835         /* Need to allocate a new ccw device. */
836         cdev = io_subchannel_create_ccwdev(sch);
837         if (IS_ERR(cdev)) {
838                 /* OK, we did everything we could... */
839                 css_sch_device_unregister(sch);
840                 return;
841         }
842         spin_lock_irq(sch->lock);
843         sch_set_cdev(sch, cdev);
844         spin_unlock_irq(sch->lock);
845         /* Start recognition for the new ccw device. */
846         if (io_subchannel_recog(cdev, sch)) {
847                 spin_lock_irq(sch->lock);
848                 sch_set_cdev(sch, NULL);
849                 spin_unlock_irq(sch->lock);
850                 if (cdev->dev.release)
851                         cdev->dev.release(&cdev->dev);
852                 css_sch_device_unregister(sch);
853         }
854 }
855
856
857 void ccw_device_move_to_orphanage(struct work_struct *work)
858 {
859         struct ccw_device_private *priv;
860         struct ccw_device *cdev;
861         struct ccw_device *replacing_cdev;
862         struct subchannel *sch;
863         int ret;
864         struct channel_subsystem *css;
865         struct ccw_dev_id dev_id;
866
867         priv = container_of(work, struct ccw_device_private, kick_work);
868         cdev = priv->cdev;
869         sch = to_subchannel(cdev->dev.parent);
870         css = to_css(sch->dev.parent);
871         dev_id.devno = sch->schib.pmcw.dev;
872         dev_id.ssid = sch->schid.ssid;
873
874         /*
875          * Move the orphaned ccw device to the orphanage so the replacing
876          * ccw device can take its place on the subchannel.
877          */
878         ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
879         if (ret) {
880                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
881                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
882                               cdev->private->dev_id.devno, ret);
883                 return;
884         }
885         cdev->ccwlock = css->pseudo_subchannel->lock;
886         /*
887          * Search for the replacing ccw device
888          * - among the disconnected devices
889          * - in the orphanage
890          */
891         replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
892         if (replacing_cdev) {
893                 sch_attach_disconnected_device(sch, replacing_cdev);
894                 return;
895         }
896         replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
897         if (replacing_cdev) {
898                 sch_attach_orphaned_device(sch, replacing_cdev);
899                 return;
900         }
901         sch_create_and_recog_new_device(sch);
902 }
903
904 /*
905  * Register recognized device.
906  */
907 static void
908 io_subchannel_register(struct work_struct *work)
909 {
910         struct ccw_device_private *priv;
911         struct ccw_device *cdev;
912         struct subchannel *sch;
913         int ret;
914         unsigned long flags;
915
916         priv = container_of(work, struct ccw_device_private, kick_work);
917         cdev = priv->cdev;
918         sch = to_subchannel(cdev->dev.parent);
919         css_update_ssd_info(sch);
920         /*
921          * io_subchannel_register() will also be called after device
922          * recognition has been done for a boxed device (which will already
923          * be registered). We need to reprobe since we may now have sense id
924          * information.
925          */
926         if (klist_node_attached(&cdev->dev.knode_parent)) {
927                 if (!cdev->drv) {
928                         ret = device_reprobe(&cdev->dev);
929                         if (ret)
930                                 /* We can't do much here. */
931                                 CIO_MSG_EVENT(2, "device_reprobe() returned"
932                                               " %d for 0.%x.%04x\n", ret,
933                                               cdev->private->dev_id.ssid,
934                                               cdev->private->dev_id.devno);
935                 }
936                 goto out;
937         }
938         /*
939          * Now we know this subchannel will stay, we can throw
940          * our delayed uevent.
941          */
942         sch->dev.uevent_suppress = 0;
943         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
944         /* make it known to the system */
945         ret = ccw_device_register(cdev);
946         if (ret) {
947                 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
948                               cdev->private->dev_id.ssid,
949                               cdev->private->dev_id.devno, ret);
950                 put_device(&cdev->dev);
951                 spin_lock_irqsave(sch->lock, flags);
952                 sch_set_cdev(sch, NULL);
953                 spin_unlock_irqrestore(sch->lock, flags);
954                 kfree (cdev->private);
955                 kfree (cdev);
956                 put_device(&sch->dev);
957                 if (atomic_dec_and_test(&ccw_device_init_count))
958                         wake_up(&ccw_device_init_wq);
959                 return;
960         }
961         put_device(&cdev->dev);
962 out:
963         cdev->private->flags.recog_done = 1;
964         put_device(&sch->dev);
965         wake_up(&cdev->private->wait_q);
966         if (atomic_dec_and_test(&ccw_device_init_count))
967                 wake_up(&ccw_device_init_wq);
968 }
969
970 static void ccw_device_call_sch_unregister(struct work_struct *work)
971 {
972         struct ccw_device_private *priv;
973         struct ccw_device *cdev;
974         struct subchannel *sch;
975
976         priv = container_of(work, struct ccw_device_private, kick_work);
977         cdev = priv->cdev;
978         sch = to_subchannel(cdev->dev.parent);
979         css_sch_device_unregister(sch);
980         /* Reset intparm to zeroes. */
981         sch->schib.pmcw.intparm = 0;
982         cio_modify(sch);
983         put_device(&cdev->dev);
984         put_device(&sch->dev);
985 }
986
987 /*
988  * subchannel recognition done. Called from the state machine.
989  */
990 void
991 io_subchannel_recog_done(struct ccw_device *cdev)
992 {
993         struct subchannel *sch;
994
995         if (css_init_done == 0) {
996                 cdev->private->flags.recog_done = 1;
997                 return;
998         }
999         switch (cdev->private->state) {
1000         case DEV_STATE_NOT_OPER:
1001                 cdev->private->flags.recog_done = 1;
1002                 /* Remove device found not operational. */
1003                 if (!get_device(&cdev->dev))
1004                         break;
1005                 sch = to_subchannel(cdev->dev.parent);
1006                 PREPARE_WORK(&cdev->private->kick_work,
1007                              ccw_device_call_sch_unregister);
1008                 queue_work(slow_path_wq, &cdev->private->kick_work);
1009                 if (atomic_dec_and_test(&ccw_device_init_count))
1010                         wake_up(&ccw_device_init_wq);
1011                 break;
1012         case DEV_STATE_BOXED:
1013                 /* Device did not respond in time. */
1014         case DEV_STATE_OFFLINE:
1015                 /* 
1016                  * We can't register the device in interrupt context so
1017                  * we schedule a work item.
1018                  */
1019                 if (!get_device(&cdev->dev))
1020                         break;
1021                 PREPARE_WORK(&cdev->private->kick_work,
1022                              io_subchannel_register);
1023                 queue_work(slow_path_wq, &cdev->private->kick_work);
1024                 break;
1025         }
1026 }
1027
1028 static int
1029 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1030 {
1031         int rc;
1032         struct ccw_device_private *priv;
1033
1034         sch_set_cdev(sch, cdev);
1035         sch->driver = &io_subchannel_driver;
1036         cdev->ccwlock = sch->lock;
1037
1038         /* Init private data. */
1039         priv = cdev->private;
1040         priv->dev_id.devno = sch->schib.pmcw.dev;
1041         priv->dev_id.ssid = sch->schid.ssid;
1042         priv->schid = sch->schid;
1043         priv->state = DEV_STATE_NOT_OPER;
1044         INIT_LIST_HEAD(&priv->cmb_list);
1045         init_waitqueue_head(&priv->wait_q);
1046         init_timer(&priv->timer);
1047
1048         /* Set an initial name for the device. */
1049         snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1050                   sch->schid.ssid, sch->schib.pmcw.dev);
1051
1052         /* Increase counter of devices currently in recognition. */
1053         atomic_inc(&ccw_device_init_count);
1054
1055         /* Start async. device sensing. */
1056         spin_lock_irq(sch->lock);
1057         rc = ccw_device_recognition(cdev);
1058         spin_unlock_irq(sch->lock);
1059         if (rc) {
1060                 if (atomic_dec_and_test(&ccw_device_init_count))
1061                         wake_up(&ccw_device_init_wq);
1062         }
1063         return rc;
1064 }
1065
1066 static void ccw_device_move_to_sch(struct work_struct *work)
1067 {
1068         struct ccw_device_private *priv;
1069         int rc;
1070         struct subchannel *sch;
1071         struct ccw_device *cdev;
1072         struct subchannel *former_parent;
1073
1074         priv = container_of(work, struct ccw_device_private, kick_work);
1075         sch = priv->sch;
1076         cdev = priv->cdev;
1077         former_parent = ccw_device_is_orphan(cdev) ?
1078                 NULL : to_subchannel(get_device(cdev->dev.parent));
1079         mutex_lock(&sch->reg_mutex);
1080         /* Try to move the ccw device to its new subchannel. */
1081         rc = device_move(&cdev->dev, &sch->dev);
1082         mutex_unlock(&sch->reg_mutex);
1083         if (rc) {
1084                 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1085                               "0.%x.%04x failed (ret=%d)!\n",
1086                               cdev->private->dev_id.ssid,
1087                               cdev->private->dev_id.devno, sch->schid.ssid,
1088                               sch->schid.sch_no, rc);
1089                 css_sch_device_unregister(sch);
1090                 goto out;
1091         }
1092         if (former_parent) {
1093                 spin_lock_irq(former_parent->lock);
1094                 sch_set_cdev(former_parent, NULL);
1095                 spin_unlock_irq(former_parent->lock);
1096                 css_sch_device_unregister(former_parent);
1097                 /* Reset intparm to zeroes. */
1098                 former_parent->schib.pmcw.intparm = 0;
1099                 cio_modify(former_parent);
1100         }
1101         sch_attach_device(sch, cdev);
1102 out:
1103         if (former_parent)
1104                 put_device(&former_parent->dev);
1105         put_device(&cdev->dev);
1106 }
1107
1108 static void io_subchannel_irq(struct subchannel *sch)
1109 {
1110         struct ccw_device *cdev;
1111
1112         cdev = sch_get_cdev(sch);
1113
1114         CIO_TRACE_EVENT(3, "IRQ");
1115         CIO_TRACE_EVENT(3, sch->dev.bus_id);
1116         if (cdev)
1117                 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1118 }
1119
1120 static int
1121 io_subchannel_probe (struct subchannel *sch)
1122 {
1123         struct ccw_device *cdev;
1124         int rc;
1125         unsigned long flags;
1126         struct ccw_dev_id dev_id;
1127
1128         cdev = sch_get_cdev(sch);
1129         if (cdev) {
1130                 /*
1131                  * This subchannel already has an associated ccw_device.
1132                  * Register it and exit. This happens for all early
1133                  * device, e.g. the console.
1134                  */
1135                 cdev->dev.groups = ccwdev_attr_groups;
1136                 device_initialize(&cdev->dev);
1137                 ccw_device_register(cdev);
1138                 /*
1139                  * Check if the device is already online. If it is
1140                  * the reference count needs to be corrected
1141                  * (see ccw_device_online and css_init_done for the
1142                  * ugly details).
1143                  */
1144                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1145                     cdev->private->state != DEV_STATE_OFFLINE &&
1146                     cdev->private->state != DEV_STATE_BOXED)
1147                         get_device(&cdev->dev);
1148                 return 0;
1149         }
1150         /*
1151          * First check if a fitting device may be found amongst the
1152          * disconnected devices or in the orphanage.
1153          */
1154         dev_id.devno = sch->schib.pmcw.dev;
1155         dev_id.ssid = sch->schid.ssid;
1156         /* Allocate I/O subchannel private data. */
1157         sch->private = kzalloc(sizeof(struct io_subchannel_private),
1158                                GFP_KERNEL | GFP_DMA);
1159         if (!sch->private)
1160                 return -ENOMEM;
1161         cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1162         if (!cdev)
1163                 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1164                                                      &dev_id);
1165         if (cdev) {
1166                 /*
1167                  * Schedule moving the device until when we have a registered
1168                  * subchannel to move to and succeed the probe. We can
1169                  * unregister later again, when the probe is through.
1170                  */
1171                 cdev->private->sch = sch;
1172                 PREPARE_WORK(&cdev->private->kick_work,
1173                              ccw_device_move_to_sch);
1174                 queue_work(slow_path_wq, &cdev->private->kick_work);
1175                 return 0;
1176         }
1177         cdev = io_subchannel_create_ccwdev(sch);
1178         if (IS_ERR(cdev)) {
1179                 kfree(sch->private);
1180                 return PTR_ERR(cdev);
1181         }
1182         rc = io_subchannel_recog(cdev, sch);
1183         if (rc) {
1184                 spin_lock_irqsave(sch->lock, flags);
1185                 sch_set_cdev(sch, NULL);
1186                 spin_unlock_irqrestore(sch->lock, flags);
1187                 if (cdev->dev.release)
1188                         cdev->dev.release(&cdev->dev);
1189                 kfree(sch->private);
1190         }
1191
1192         return rc;
1193 }
1194
1195 static int
1196 io_subchannel_remove (struct subchannel *sch)
1197 {
1198         struct ccw_device *cdev;
1199         unsigned long flags;
1200
1201         cdev = sch_get_cdev(sch);
1202         if (!cdev)
1203                 return 0;
1204         /* Set ccw device to not operational and drop reference. */
1205         spin_lock_irqsave(cdev->ccwlock, flags);
1206         sch_set_cdev(sch, NULL);
1207         cdev->private->state = DEV_STATE_NOT_OPER;
1208         spin_unlock_irqrestore(cdev->ccwlock, flags);
1209         ccw_device_unregister(cdev);
1210         put_device(&cdev->dev);
1211         kfree(sch->private);
1212         return 0;
1213 }
1214
1215 static int io_subchannel_notify(struct subchannel *sch, int event)
1216 {
1217         struct ccw_device *cdev;
1218
1219         cdev = sch_get_cdev(sch);
1220         if (!cdev)
1221                 return 0;
1222         if (!cdev->drv)
1223                 return 0;
1224         if (!cdev->online)
1225                 return 0;
1226         return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1227 }
1228
1229 static void io_subchannel_verify(struct subchannel *sch)
1230 {
1231         struct ccw_device *cdev;
1232
1233         cdev = sch_get_cdev(sch);
1234         if (cdev)
1235                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1236 }
1237
1238 static void io_subchannel_ioterm(struct subchannel *sch)
1239 {
1240         struct ccw_device *cdev;
1241
1242         cdev = sch_get_cdev(sch);
1243         if (!cdev)
1244                 return;
1245         /* Internal I/O will be retried by the interrupt handler. */
1246         if (cdev->private->flags.intretry)
1247                 return;
1248         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1249         if (cdev->handler)
1250                 cdev->handler(cdev, cdev->private->intparm,
1251                               ERR_PTR(-EIO));
1252 }
1253
1254 static void
1255 io_subchannel_shutdown(struct subchannel *sch)
1256 {
1257         struct ccw_device *cdev;
1258         int ret;
1259
1260         cdev = sch_get_cdev(sch);
1261
1262         if (cio_is_console(sch->schid))
1263                 return;
1264         if (!sch->schib.pmcw.ena)
1265                 /* Nothing to do. */
1266                 return;
1267         ret = cio_disable_subchannel(sch);
1268         if (ret != -EBUSY)
1269                 /* Subchannel is disabled, we're done. */
1270                 return;
1271         cdev->private->state = DEV_STATE_QUIESCE;
1272         if (cdev->handler)
1273                 cdev->handler(cdev, cdev->private->intparm,
1274                               ERR_PTR(-EIO));
1275         ret = ccw_device_cancel_halt_clear(cdev);
1276         if (ret == -EBUSY) {
1277                 ccw_device_set_timeout(cdev, HZ/10);
1278                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1279         }
1280         cio_disable_subchannel(sch);
1281 }
1282
1283 #ifdef CONFIG_CCW_CONSOLE
1284 static struct ccw_device console_cdev;
1285 static struct ccw_device_private console_private;
1286 static int console_cdev_in_use;
1287
1288 static DEFINE_SPINLOCK(ccw_console_lock);
1289
1290 spinlock_t * cio_get_console_lock(void)
1291 {
1292         return &ccw_console_lock;
1293 }
1294
1295 static int
1296 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1297 {
1298         int rc;
1299
1300         /* Attach subchannel private data. */
1301         sch->private = cio_get_console_priv();
1302         memset(sch->private, 0, sizeof(struct io_subchannel_private));
1303         /* Initialize the ccw_device structure. */
1304         cdev->dev.parent= &sch->dev;
1305         rc = io_subchannel_recog(cdev, sch);
1306         if (rc)
1307                 return rc;
1308
1309         /* Now wait for the async. recognition to come to an end. */
1310         spin_lock_irq(cdev->ccwlock);
1311         while (!dev_fsm_final_state(cdev))
1312                 wait_cons_dev();
1313         rc = -EIO;
1314         if (cdev->private->state != DEV_STATE_OFFLINE)
1315                 goto out_unlock;
1316         ccw_device_online(cdev);
1317         while (!dev_fsm_final_state(cdev))
1318                 wait_cons_dev();
1319         if (cdev->private->state != DEV_STATE_ONLINE)
1320                 goto out_unlock;
1321         rc = 0;
1322 out_unlock:
1323         spin_unlock_irq(cdev->ccwlock);
1324         return 0;
1325 }
1326
1327 struct ccw_device *
1328 ccw_device_probe_console(void)
1329 {
1330         struct subchannel *sch;
1331         int ret;
1332
1333         if (xchg(&console_cdev_in_use, 1) != 0)
1334                 return ERR_PTR(-EBUSY);
1335         sch = cio_probe_console();
1336         if (IS_ERR(sch)) {
1337                 console_cdev_in_use = 0;
1338                 return (void *) sch;
1339         }
1340         memset(&console_cdev, 0, sizeof(struct ccw_device));
1341         memset(&console_private, 0, sizeof(struct ccw_device_private));
1342         console_cdev.private = &console_private;
1343         console_private.cdev = &console_cdev;
1344         ret = ccw_device_console_enable(&console_cdev, sch);
1345         if (ret) {
1346                 cio_release_console();
1347                 console_cdev_in_use = 0;
1348                 return ERR_PTR(ret);
1349         }
1350         console_cdev.online = 1;
1351         return &console_cdev;
1352 }
1353 #endif
1354
1355 /*
1356  * get ccw_device matching the busid, but only if owned by cdrv
1357  */
1358 static int
1359 __ccwdev_check_busid(struct device *dev, void *id)
1360 {
1361         char *bus_id;
1362
1363         bus_id = id;
1364
1365         return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1366 }
1367
1368
1369 /**
1370  * get_ccwdev_by_busid() - obtain device from a bus id
1371  * @cdrv: driver the device is owned by
1372  * @bus_id: bus id of the device to be searched
1373  *
1374  * This function searches all devices owned by @cdrv for a device with a bus
1375  * id matching @bus_id.
1376  * Returns:
1377  *  If a match is found, its reference count of the found device is increased
1378  *  and it is returned; else %NULL is returned.
1379  */
1380 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1381                                        const char *bus_id)
1382 {
1383         struct device *dev;
1384         struct device_driver *drv;
1385
1386         drv = get_driver(&cdrv->driver);
1387         if (!drv)
1388                 return NULL;
1389
1390         dev = driver_find_device(drv, NULL, (void *)bus_id,
1391                                  __ccwdev_check_busid);
1392         put_driver(drv);
1393
1394         return dev ? to_ccwdev(dev) : NULL;
1395 }
1396
1397 /************************** device driver handling ************************/
1398
1399 /* This is the implementation of the ccw_driver class. The probe, remove
1400  * and release methods are initially very similar to the device_driver
1401  * implementations, with the difference that they have ccw_device
1402  * arguments.
1403  *
1404  * A ccw driver also contains the information that is needed for
1405  * device matching.
1406  */
1407 static int
1408 ccw_device_probe (struct device *dev)
1409 {
1410         struct ccw_device *cdev = to_ccwdev(dev);
1411         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1412         int ret;
1413
1414         cdev->drv = cdrv; /* to let the driver call _set_online */
1415
1416         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1417
1418         if (ret) {
1419                 cdev->drv = NULL;
1420                 return ret;
1421         }
1422
1423         return 0;
1424 }
1425
1426 static int
1427 ccw_device_remove (struct device *dev)
1428 {
1429         struct ccw_device *cdev = to_ccwdev(dev);
1430         struct ccw_driver *cdrv = cdev->drv;
1431         int ret;
1432
1433         if (cdrv->remove)
1434                 cdrv->remove(cdev);
1435         if (cdev->online) {
1436                 cdev->online = 0;
1437                 spin_lock_irq(cdev->ccwlock);
1438                 ret = ccw_device_offline(cdev);
1439                 spin_unlock_irq(cdev->ccwlock);
1440                 if (ret == 0)
1441                         wait_event(cdev->private->wait_q,
1442                                    dev_fsm_final_state(cdev));
1443                 else
1444                         //FIXME: we can't fail!
1445                         CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1446                                       "device 0.%x.%04x\n",
1447                                       ret, cdev->private->dev_id.ssid,
1448                                       cdev->private->dev_id.devno);
1449         }
1450         ccw_device_set_timeout(cdev, 0);
1451         cdev->drv = NULL;
1452         return 0;
1453 }
1454
1455 static void ccw_device_shutdown(struct device *dev)
1456 {
1457         struct ccw_device *cdev;
1458
1459         cdev = to_ccwdev(dev);
1460         if (cdev->drv && cdev->drv->shutdown)
1461                 cdev->drv->shutdown(cdev);
1462         disable_cmf(cdev);
1463 }
1464
1465 struct bus_type ccw_bus_type = {
1466         .name   = "ccw",
1467         .match  = ccw_bus_match,
1468         .uevent = ccw_uevent,
1469         .probe  = ccw_device_probe,
1470         .remove = ccw_device_remove,
1471         .shutdown = ccw_device_shutdown,
1472 };
1473
1474 /**
1475  * ccw_driver_register() - register a ccw driver
1476  * @cdriver: driver to be registered
1477  *
1478  * This function is mainly a wrapper around driver_register().
1479  * Returns:
1480  *   %0 on success and a negative error value on failure.
1481  */
1482 int ccw_driver_register(struct ccw_driver *cdriver)
1483 {
1484         struct device_driver *drv = &cdriver->driver;
1485
1486         drv->bus = &ccw_bus_type;
1487         drv->name = cdriver->name;
1488         drv->owner = cdriver->owner;
1489
1490         return driver_register(drv);
1491 }
1492
1493 /**
1494  * ccw_driver_unregister() - deregister a ccw driver
1495  * @cdriver: driver to be deregistered
1496  *
1497  * This function is mainly a wrapper around driver_unregister().
1498  */
1499 void ccw_driver_unregister(struct ccw_driver *cdriver)
1500 {
1501         driver_unregister(&cdriver->driver);
1502 }
1503
1504 /* Helper func for qdio. */
1505 struct subchannel_id
1506 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1507 {
1508         struct subchannel *sch;
1509
1510         sch = to_subchannel(cdev->dev.parent);
1511         return sch->schid;
1512 }
1513
1514 static int recovery_check(struct device *dev, void *data)
1515 {
1516         struct ccw_device *cdev = to_ccwdev(dev);
1517         int *redo = data;
1518
1519         spin_lock_irq(cdev->ccwlock);
1520         switch (cdev->private->state) {
1521         case DEV_STATE_DISCONNECTED:
1522                 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1523                               cdev->private->dev_id.ssid,
1524                               cdev->private->dev_id.devno);
1525                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1526                 *redo = 1;
1527                 break;
1528         case DEV_STATE_DISCONNECTED_SENSE_ID:
1529                 *redo = 1;
1530                 break;
1531         }
1532         spin_unlock_irq(cdev->ccwlock);
1533
1534         return 0;
1535 }
1536
1537 static void recovery_work_func(struct work_struct *unused)
1538 {
1539         int redo = 0;
1540
1541         bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1542         if (redo) {
1543                 spin_lock_irq(&recovery_lock);
1544                 if (!timer_pending(&recovery_timer)) {
1545                         if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1546                                 recovery_phase++;
1547                         mod_timer(&recovery_timer, jiffies +
1548                                   recovery_delay[recovery_phase] * HZ);
1549                 }
1550                 spin_unlock_irq(&recovery_lock);
1551         } else
1552                 CIO_MSG_EVENT(2, "recovery: end\n");
1553 }
1554
1555 static DECLARE_WORK(recovery_work, recovery_work_func);
1556
1557 static void recovery_func(unsigned long data)
1558 {
1559         /*
1560          * We can't do our recovery in softirq context and it's not
1561          * performance critical, so we schedule it.
1562          */
1563         schedule_work(&recovery_work);
1564 }
1565
1566 void ccw_device_schedule_recovery(void)
1567 {
1568         unsigned long flags;
1569
1570         CIO_MSG_EVENT(2, "recovery: schedule\n");
1571         spin_lock_irqsave(&recovery_lock, flags);
1572         if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1573                 recovery_phase = 0;
1574                 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1575         }
1576         spin_unlock_irqrestore(&recovery_lock, flags);
1577 }
1578
1579 MODULE_LICENSE("GPL");
1580 EXPORT_SYMBOL(ccw_device_set_online);
1581 EXPORT_SYMBOL(ccw_device_set_offline);
1582 EXPORT_SYMBOL(ccw_driver_register);
1583 EXPORT_SYMBOL(ccw_driver_unregister);
1584 EXPORT_SYMBOL(get_ccwdev_by_busid);
1585 EXPORT_SYMBOL(ccw_bus_type);
1586 EXPORT_SYMBOL(ccw_device_work);
1587 EXPORT_SYMBOL(ccw_device_notify_work);
1588 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);