[S390] cio: merge init calls
[safe/jmp/linux-2.6] / drivers / s390 / cio / device.c
1 /*
2  *  drivers/s390/cio/device.c
3  *  bus driver for ccw devices
4  *
5  *    Copyright IBM Corp. 2002,2008
6  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/workqueue.h>
19 #include <linux/timer.h>
20
21 #include <asm/ccwdev.h>
22 #include <asm/cio.h>
23 #include <asm/param.h>          /* HZ */
24 #include <asm/cmb.h>
25 #include <asm/isc.h>
26
27 #include "chp.h"
28 #include "cio.h"
29 #include "cio_debug.h"
30 #include "css.h"
31 #include "device.h"
32 #include "ioasm.h"
33 #include "io_sch.h"
34 #include "blacklist.h"
35
36 static struct timer_list recovery_timer;
37 static DEFINE_SPINLOCK(recovery_lock);
38 static int recovery_phase;
39 static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
41 /******************* bus type handling ***********************/
42
43 /* The Linux driver model distinguishes between a bus type and
44  * the bus itself. Of course we only have one channel
45  * subsystem driver and one channel system per machine, but
46  * we still use the abstraction. T.R. says it's a good idea. */
47 static int
48 ccw_bus_match (struct device * dev, struct device_driver * drv)
49 {
50         struct ccw_device *cdev = to_ccwdev(dev);
51         struct ccw_driver *cdrv = to_ccwdrv(drv);
52         const struct ccw_device_id *ids = cdrv->ids, *found;
53
54         if (!ids)
55                 return 0;
56
57         found = ccw_device_id_match(ids, &cdev->id);
58         if (!found)
59                 return 0;
60
61         cdev->id.driver_info = found->driver_info;
62
63         return 1;
64 }
65
66 /* Store modalias string delimited by prefix/suffix string into buffer with
67  * specified size. Return length of resulting string (excluding trailing '\0')
68  * even if string doesn't fit buffer (snprintf semantics). */
69 static int snprint_alias(char *buf, size_t size,
70                          struct ccw_device_id *id, const char *suffix)
71 {
72         int len;
73
74         len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
75         if (len > size)
76                 return len;
77         buf += len;
78         size -= len;
79
80         if (id->dev_type != 0)
81                 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82                                 id->dev_model, suffix);
83         else
84                 len += snprintf(buf, size, "dtdm%s", suffix);
85
86         return len;
87 }
88
89 /* Set up environment variables for ccw device uevent. Return 0 on success,
90  * non-zero otherwise. */
91 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
92 {
93         struct ccw_device *cdev = to_ccwdev(dev);
94         struct ccw_device_id *id = &(cdev->id);
95         int ret;
96         char modalias_buf[30];
97
98         /* CU_TYPE= */
99         ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
100         if (ret)
101                 return ret;
102
103         /* CU_MODEL= */
104         ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
105         if (ret)
106                 return ret;
107
108         /* The next two can be zero, that's ok for us */
109         /* DEV_TYPE= */
110         ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
111         if (ret)
112                 return ret;
113
114         /* DEV_MODEL= */
115         ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
116         if (ret)
117                 return ret;
118
119         /* MODALIAS=  */
120         snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
121         ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122         return ret;
123 }
124
125 struct bus_type ccw_bus_type;
126
127 static void io_subchannel_irq(struct subchannel *);
128 static int io_subchannel_probe(struct subchannel *);
129 static int io_subchannel_remove(struct subchannel *);
130 static void io_subchannel_shutdown(struct subchannel *);
131 static int io_subchannel_sch_event(struct subchannel *, int);
132 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133                                    int);
134
135 static struct css_device_id io_subchannel_ids[] = {
136         { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137         { /* end of list */ },
138 };
139 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
140
141 static int io_subchannel_prepare(struct subchannel *sch)
142 {
143         struct ccw_device *cdev;
144         /*
145          * Don't allow suspend while a ccw device registration
146          * is still outstanding.
147          */
148         cdev = sch_get_cdev(sch);
149         if (cdev && !device_is_registered(&cdev->dev))
150                 return -EAGAIN;
151         return 0;
152 }
153
154 static struct css_driver io_subchannel_driver = {
155         .owner = THIS_MODULE,
156         .subchannel_type = io_subchannel_ids,
157         .name = "io_subchannel",
158         .irq = io_subchannel_irq,
159         .sch_event = io_subchannel_sch_event,
160         .chp_event = io_subchannel_chp_event,
161         .probe = io_subchannel_probe,
162         .remove = io_subchannel_remove,
163         .shutdown = io_subchannel_shutdown,
164         .prepare = io_subchannel_prepare,
165 };
166
167 struct workqueue_struct *ccw_device_work;
168 wait_queue_head_t ccw_device_init_wq;
169 atomic_t ccw_device_init_count;
170
171 static void recovery_func(unsigned long data);
172
173 int __init io_subchannel_init(void)
174 {
175         int ret;
176
177         init_waitqueue_head(&ccw_device_init_wq);
178         atomic_set(&ccw_device_init_count, 0);
179         setup_timer(&recovery_timer, recovery_func, 0);
180
181         ccw_device_work = create_singlethread_workqueue("cio");
182         if (!ccw_device_work)
183                 return -ENOMEM;
184         slow_path_wq = create_singlethread_workqueue("kslowcrw");
185         if (!slow_path_wq) {
186                 ret = -ENOMEM;
187                 goto out_err;
188         }
189         if ((ret = bus_register (&ccw_bus_type)))
190                 goto out_err;
191
192         ret = css_driver_register(&io_subchannel_driver);
193         if (ret)
194                 goto out_err;
195
196         return 0;
197 out_err:
198         if (ccw_device_work)
199                 destroy_workqueue(ccw_device_work);
200         if (slow_path_wq)
201                 destroy_workqueue(slow_path_wq);
202         return ret;
203 }
204
205
206 /************************ device handling **************************/
207
208 /*
209  * A ccw_device has some interfaces in sysfs in addition to the
210  * standard ones.
211  * The following entries are designed to export the information which
212  * resided in 2.4 in /proc/subchannels. Subchannel and device number
213  * are obvious, so they don't have an entry :)
214  * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215  */
216 static ssize_t
217 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
218 {
219         struct subchannel *sch = to_subchannel(dev);
220         struct chsc_ssd_info *ssd = &sch->ssd_info;
221         ssize_t ret = 0;
222         int chp;
223         int mask;
224
225         for (chp = 0; chp < 8; chp++) {
226                 mask = 0x80 >> chp;
227                 if (ssd->path_mask & mask)
228                         ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229                 else
230                         ret += sprintf(buf + ret, "00 ");
231         }
232         ret += sprintf (buf+ret, "\n");
233         return min((ssize_t)PAGE_SIZE, ret);
234 }
235
236 static ssize_t
237 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
238 {
239         struct subchannel *sch = to_subchannel(dev);
240         struct pmcw *pmcw = &sch->schib.pmcw;
241
242         return sprintf (buf, "%02x %02x %02x\n",
243                         pmcw->pim, pmcw->pam, pmcw->pom);
244 }
245
246 static ssize_t
247 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
248 {
249         struct ccw_device *cdev = to_ccwdev(dev);
250         struct ccw_device_id *id = &(cdev->id);
251
252         if (id->dev_type != 0)
253                 return sprintf(buf, "%04x/%02x\n",
254                                 id->dev_type, id->dev_model);
255         else
256                 return sprintf(buf, "n/a\n");
257 }
258
259 static ssize_t
260 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
261 {
262         struct ccw_device *cdev = to_ccwdev(dev);
263         struct ccw_device_id *id = &(cdev->id);
264
265         return sprintf(buf, "%04x/%02x\n",
266                        id->cu_type, id->cu_model);
267 }
268
269 static ssize_t
270 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271 {
272         struct ccw_device *cdev = to_ccwdev(dev);
273         struct ccw_device_id *id = &(cdev->id);
274         int len;
275
276         len = snprint_alias(buf, PAGE_SIZE, id, "\n");
277
278         return len > PAGE_SIZE ? PAGE_SIZE : len;
279 }
280
281 static ssize_t
282 online_show (struct device *dev, struct device_attribute *attr, char *buf)
283 {
284         struct ccw_device *cdev = to_ccwdev(dev);
285
286         return sprintf(buf, cdev->online ? "1\n" : "0\n");
287 }
288
289 int ccw_device_is_orphan(struct ccw_device *cdev)
290 {
291         return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
292 }
293
294 static void ccw_device_unregister(struct ccw_device *cdev)
295 {
296         if (test_and_clear_bit(1, &cdev->private->registered)) {
297                 device_del(&cdev->dev);
298                 /* Release reference from device_initialize(). */
299                 put_device(&cdev->dev);
300         }
301 }
302
303 static void ccw_device_remove_orphan_cb(struct work_struct *work)
304 {
305         struct ccw_device_private *priv;
306         struct ccw_device *cdev;
307
308         priv = container_of(work, struct ccw_device_private, kick_work);
309         cdev = priv->cdev;
310         ccw_device_unregister(cdev);
311         /* Release cdev reference for workqueue processing. */
312         put_device(&cdev->dev);
313 }
314
315 static void
316 ccw_device_remove_disconnected(struct ccw_device *cdev)
317 {
318         unsigned long flags;
319
320         /*
321          * Forced offline in disconnected state means
322          * 'throw away device'.
323          */
324         if (ccw_device_is_orphan(cdev)) {
325                 /*
326                  * Deregister ccw device.
327                  * Unfortunately, we cannot do this directly from the
328                  * attribute method.
329                  */
330                 /* Get cdev reference for workqueue processing. */
331                 if (!get_device(&cdev->dev))
332                         return;
333                 spin_lock_irqsave(cdev->ccwlock, flags);
334                 cdev->private->state = DEV_STATE_NOT_OPER;
335                 spin_unlock_irqrestore(cdev->ccwlock, flags);
336                 PREPARE_WORK(&cdev->private->kick_work,
337                                 ccw_device_remove_orphan_cb);
338                 queue_work(slow_path_wq, &cdev->private->kick_work);
339         } else
340                 /* Deregister subchannel, which will kill the ccw device. */
341                 ccw_device_schedule_sch_unregister(cdev);
342 }
343
344 /**
345  * ccw_device_set_offline() - disable a ccw device for I/O
346  * @cdev: target ccw device
347  *
348  * This function calls the driver's set_offline() function for @cdev, if
349  * given, and then disables @cdev.
350  * Returns:
351  *   %0 on success and a negative error value on failure.
352  * Context:
353  *  enabled, ccw device lock not held
354  */
355 int ccw_device_set_offline(struct ccw_device *cdev)
356 {
357         int ret;
358
359         if (!cdev)
360                 return -ENODEV;
361         if (!cdev->online || !cdev->drv)
362                 return -EINVAL;
363
364         if (cdev->drv->set_offline) {
365                 ret = cdev->drv->set_offline(cdev);
366                 if (ret != 0)
367                         return ret;
368         }
369         cdev->online = 0;
370         spin_lock_irq(cdev->ccwlock);
371         /* Wait until a final state or DISCONNECTED is reached */
372         while (!dev_fsm_final_state(cdev) &&
373                cdev->private->state != DEV_STATE_DISCONNECTED) {
374                 spin_unlock_irq(cdev->ccwlock);
375                 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
376                            cdev->private->state == DEV_STATE_DISCONNECTED));
377                 spin_lock_irq(cdev->ccwlock);
378         }
379         ret = ccw_device_offline(cdev);
380         if (ret)
381                 goto error;
382         spin_unlock_irq(cdev->ccwlock);
383         wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
384                    cdev->private->state == DEV_STATE_DISCONNECTED));
385         /* Give up reference from ccw_device_set_online(). */
386         put_device(&cdev->dev);
387         return 0;
388
389 error:
390         CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
391                       ret, cdev->private->dev_id.ssid,
392                       cdev->private->dev_id.devno);
393         cdev->private->state = DEV_STATE_OFFLINE;
394         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
395         spin_unlock_irq(cdev->ccwlock);
396         /* Give up reference from ccw_device_set_online(). */
397         put_device(&cdev->dev);
398         return -ENODEV;
399 }
400
401 /**
402  * ccw_device_set_online() - enable a ccw device for I/O
403  * @cdev: target ccw device
404  *
405  * This function first enables @cdev and then calls the driver's set_online()
406  * function for @cdev, if given. If set_online() returns an error, @cdev is
407  * disabled again.
408  * Returns:
409  *   %0 on success and a negative error value on failure.
410  * Context:
411  *  enabled, ccw device lock not held
412  */
413 int ccw_device_set_online(struct ccw_device *cdev)
414 {
415         int ret;
416         int ret2;
417
418         if (!cdev)
419                 return -ENODEV;
420         if (cdev->online || !cdev->drv)
421                 return -EINVAL;
422         /* Hold on to an extra reference while device is online. */
423         if (!get_device(&cdev->dev))
424                 return -ENODEV;
425
426         spin_lock_irq(cdev->ccwlock);
427         ret = ccw_device_online(cdev);
428         spin_unlock_irq(cdev->ccwlock);
429         if (ret == 0)
430                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
431         else {
432                 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
433                               "device 0.%x.%04x\n",
434                               ret, cdev->private->dev_id.ssid,
435                               cdev->private->dev_id.devno);
436                 /* Give up online reference since onlining failed. */
437                 put_device(&cdev->dev);
438                 return ret;
439         }
440         spin_lock_irq(cdev->ccwlock);
441         /* Check if online processing was successful */
442         if ((cdev->private->state != DEV_STATE_ONLINE) &&
443             (cdev->private->state != DEV_STATE_W4SENSE)) {
444                 spin_unlock_irq(cdev->ccwlock);
445                 /* Give up online reference since onlining failed. */
446                 put_device(&cdev->dev);
447                 return -ENODEV;
448         }
449         spin_unlock_irq(cdev->ccwlock);
450         if (cdev->drv->set_online)
451                 ret = cdev->drv->set_online(cdev);
452         if (ret)
453                 goto rollback;
454         cdev->online = 1;
455         return 0;
456
457 rollback:
458         spin_lock_irq(cdev->ccwlock);
459         /* Wait until a final state or DISCONNECTED is reached */
460         while (!dev_fsm_final_state(cdev) &&
461                cdev->private->state != DEV_STATE_DISCONNECTED) {
462                 spin_unlock_irq(cdev->ccwlock);
463                 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
464                            cdev->private->state == DEV_STATE_DISCONNECTED));
465                 spin_lock_irq(cdev->ccwlock);
466         }
467         ret2 = ccw_device_offline(cdev);
468         if (ret2)
469                 goto error;
470         spin_unlock_irq(cdev->ccwlock);
471         wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
472                    cdev->private->state == DEV_STATE_DISCONNECTED));
473         /* Give up online reference since onlining failed. */
474         put_device(&cdev->dev);
475         return ret;
476
477 error:
478         CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
479                       "device 0.%x.%04x\n",
480                       ret2, cdev->private->dev_id.ssid,
481                       cdev->private->dev_id.devno);
482         cdev->private->state = DEV_STATE_OFFLINE;
483         spin_unlock_irq(cdev->ccwlock);
484         /* Give up online reference since onlining failed. */
485         put_device(&cdev->dev);
486         return ret;
487 }
488
489 static int online_store_handle_offline(struct ccw_device *cdev)
490 {
491         if (cdev->private->state == DEV_STATE_DISCONNECTED)
492                 ccw_device_remove_disconnected(cdev);
493         else if (cdev->online && cdev->drv && cdev->drv->set_offline)
494                 return ccw_device_set_offline(cdev);
495         return 0;
496 }
497
498 static int online_store_recog_and_online(struct ccw_device *cdev)
499 {
500         int ret;
501
502         /* Do device recognition, if needed. */
503         if (cdev->private->state == DEV_STATE_BOXED) {
504                 ret = ccw_device_recognition(cdev);
505                 if (ret) {
506                         CIO_MSG_EVENT(0, "Couldn't start recognition "
507                                       "for device 0.%x.%04x (ret=%d)\n",
508                                       cdev->private->dev_id.ssid,
509                                       cdev->private->dev_id.devno, ret);
510                         return ret;
511                 }
512                 wait_event(cdev->private->wait_q,
513                            cdev->private->flags.recog_done);
514                 if (cdev->private->state != DEV_STATE_OFFLINE)
515                         /* recognition failed */
516                         return -EAGAIN;
517         }
518         if (cdev->drv && cdev->drv->set_online)
519                 ccw_device_set_online(cdev);
520         return 0;
521 }
522
523 static int online_store_handle_online(struct ccw_device *cdev, int force)
524 {
525         int ret;
526
527         ret = online_store_recog_and_online(cdev);
528         if (ret && !force)
529                 return ret;
530         if (force && cdev->private->state == DEV_STATE_BOXED) {
531                 ret = ccw_device_stlck(cdev);
532                 if (ret)
533                         return ret;
534                 if (cdev->id.cu_type == 0)
535                         cdev->private->state = DEV_STATE_NOT_OPER;
536                 ret = online_store_recog_and_online(cdev);
537                 if (ret)
538                         return ret;
539         }
540         return 0;
541 }
542
543 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
544                              const char *buf, size_t count)
545 {
546         struct ccw_device *cdev = to_ccwdev(dev);
547         int force, ret;
548         unsigned long i;
549
550         if ((cdev->private->state != DEV_STATE_OFFLINE &&
551              cdev->private->state != DEV_STATE_ONLINE &&
552              cdev->private->state != DEV_STATE_BOXED &&
553              cdev->private->state != DEV_STATE_DISCONNECTED) ||
554             atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
555                 return -EAGAIN;
556
557         if (cdev->drv && !try_module_get(cdev->drv->owner)) {
558                 atomic_set(&cdev->private->onoff, 0);
559                 return -EINVAL;
560         }
561         if (!strncmp(buf, "force\n", count)) {
562                 force = 1;
563                 i = 1;
564                 ret = 0;
565         } else {
566                 force = 0;
567                 ret = strict_strtoul(buf, 16, &i);
568         }
569         if (ret)
570                 goto out;
571         switch (i) {
572         case 0:
573                 ret = online_store_handle_offline(cdev);
574                 break;
575         case 1:
576                 ret = online_store_handle_online(cdev, force);
577                 break;
578         default:
579                 ret = -EINVAL;
580         }
581 out:
582         if (cdev->drv)
583                 module_put(cdev->drv->owner);
584         atomic_set(&cdev->private->onoff, 0);
585         return (ret < 0) ? ret : count;
586 }
587
588 static ssize_t
589 available_show (struct device *dev, struct device_attribute *attr, char *buf)
590 {
591         struct ccw_device *cdev = to_ccwdev(dev);
592         struct subchannel *sch;
593
594         if (ccw_device_is_orphan(cdev))
595                 return sprintf(buf, "no device\n");
596         switch (cdev->private->state) {
597         case DEV_STATE_BOXED:
598                 return sprintf(buf, "boxed\n");
599         case DEV_STATE_DISCONNECTED:
600         case DEV_STATE_DISCONNECTED_SENSE_ID:
601         case DEV_STATE_NOT_OPER:
602                 sch = to_subchannel(dev->parent);
603                 if (!sch->lpm)
604                         return sprintf(buf, "no path\n");
605                 else
606                         return sprintf(buf, "no device\n");
607         default:
608                 /* All other states considered fine. */
609                 return sprintf(buf, "good\n");
610         }
611 }
612
613 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
614 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
615 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
616 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
617 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
618 static DEVICE_ATTR(online, 0644, online_show, online_store);
619 static DEVICE_ATTR(availability, 0444, available_show, NULL);
620
621 static struct attribute *io_subchannel_attrs[] = {
622         &dev_attr_chpids.attr,
623         &dev_attr_pimpampom.attr,
624         NULL,
625 };
626
627 static struct attribute_group io_subchannel_attr_group = {
628         .attrs = io_subchannel_attrs,
629 };
630
631 static struct attribute * ccwdev_attrs[] = {
632         &dev_attr_devtype.attr,
633         &dev_attr_cutype.attr,
634         &dev_attr_modalias.attr,
635         &dev_attr_online.attr,
636         &dev_attr_cmb_enable.attr,
637         &dev_attr_availability.attr,
638         NULL,
639 };
640
641 static struct attribute_group ccwdev_attr_group = {
642         .attrs = ccwdev_attrs,
643 };
644
645 static const struct attribute_group *ccwdev_attr_groups[] = {
646         &ccwdev_attr_group,
647         NULL,
648 };
649
650 /* this is a simple abstraction for device_register that sets the
651  * correct bus type and adds the bus specific files */
652 static int ccw_device_register(struct ccw_device *cdev)
653 {
654         struct device *dev = &cdev->dev;
655         int ret;
656
657         dev->bus = &ccw_bus_type;
658         ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
659                            cdev->private->dev_id.devno);
660         if (ret)
661                 return ret;
662         ret = device_add(dev);
663         if (ret)
664                 return ret;
665
666         set_bit(1, &cdev->private->registered);
667         return ret;
668 }
669
670 struct match_data {
671         struct ccw_dev_id dev_id;
672         struct ccw_device * sibling;
673 };
674
675 static int
676 match_devno(struct device * dev, void * data)
677 {
678         struct match_data * d = data;
679         struct ccw_device * cdev;
680
681         cdev = to_ccwdev(dev);
682         if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
683             !ccw_device_is_orphan(cdev) &&
684             ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
685             (cdev != d->sibling))
686                 return 1;
687         return 0;
688 }
689
690 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
691                                                      struct ccw_device *sibling)
692 {
693         struct device *dev;
694         struct match_data data;
695
696         data.dev_id = *dev_id;
697         data.sibling = sibling;
698         dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
699
700         return dev ? to_ccwdev(dev) : NULL;
701 }
702
703 static int match_orphan(struct device *dev, void *data)
704 {
705         struct ccw_dev_id *dev_id;
706         struct ccw_device *cdev;
707
708         dev_id = data;
709         cdev = to_ccwdev(dev);
710         return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
711 }
712
713 static struct ccw_device *
714 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
715                               struct ccw_dev_id *dev_id)
716 {
717         struct device *dev;
718
719         dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
720                                 match_orphan);
721
722         return dev ? to_ccwdev(dev) : NULL;
723 }
724
725 void ccw_device_do_unbind_bind(struct work_struct *work)
726 {
727         struct ccw_device_private *priv;
728         struct ccw_device *cdev;
729         struct subchannel *sch;
730         int ret;
731
732         priv = container_of(work, struct ccw_device_private, kick_work);
733         cdev = priv->cdev;
734         sch = to_subchannel(cdev->dev.parent);
735
736         if (test_bit(1, &cdev->private->registered)) {
737                 device_release_driver(&cdev->dev);
738                 ret = device_attach(&cdev->dev);
739                 WARN_ON(ret == -ENODEV);
740         }
741 }
742
743 static void
744 ccw_device_release(struct device *dev)
745 {
746         struct ccw_device *cdev;
747
748         cdev = to_ccwdev(dev);
749         /* Release reference of parent subchannel. */
750         put_device(cdev->dev.parent);
751         kfree(cdev->private);
752         kfree(cdev);
753 }
754
755 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
756 {
757         struct ccw_device *cdev;
758
759         cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
760         if (cdev) {
761                 cdev->private = kzalloc(sizeof(struct ccw_device_private),
762                                         GFP_KERNEL | GFP_DMA);
763                 if (cdev->private)
764                         return cdev;
765         }
766         kfree(cdev);
767         return ERR_PTR(-ENOMEM);
768 }
769
770 static int io_subchannel_initialize_dev(struct subchannel *sch,
771                                         struct ccw_device *cdev)
772 {
773         cdev->private->cdev = cdev;
774         atomic_set(&cdev->private->onoff, 0);
775         cdev->dev.parent = &sch->dev;
776         cdev->dev.release = ccw_device_release;
777         INIT_WORK(&cdev->private->kick_work, NULL);
778         cdev->dev.groups = ccwdev_attr_groups;
779         /* Do first half of device_register. */
780         device_initialize(&cdev->dev);
781         if (!get_device(&sch->dev)) {
782                 /* Release reference from device_initialize(). */
783                 put_device(&cdev->dev);
784                 return -ENODEV;
785         }
786         return 0;
787 }
788
789 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
790 {
791         struct ccw_device *cdev;
792         int ret;
793
794         cdev = io_subchannel_allocate_dev(sch);
795         if (!IS_ERR(cdev)) {
796                 ret = io_subchannel_initialize_dev(sch, cdev);
797                 if (ret)
798                         cdev = ERR_PTR(ret);
799         }
800         return cdev;
801 }
802
803 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
804
805 static void sch_attach_device(struct subchannel *sch,
806                               struct ccw_device *cdev)
807 {
808         css_update_ssd_info(sch);
809         spin_lock_irq(sch->lock);
810         sch_set_cdev(sch, cdev);
811         cdev->private->schid = sch->schid;
812         cdev->ccwlock = sch->lock;
813         ccw_device_trigger_reprobe(cdev);
814         spin_unlock_irq(sch->lock);
815 }
816
817 static void sch_attach_disconnected_device(struct subchannel *sch,
818                                            struct ccw_device *cdev)
819 {
820         struct subchannel *other_sch;
821         int ret;
822
823         /* Get reference for new parent. */
824         if (!get_device(&sch->dev))
825                 return;
826         other_sch = to_subchannel(cdev->dev.parent);
827         /* Note: device_move() changes cdev->dev.parent */
828         ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
829         if (ret) {
830                 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
831                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
832                               cdev->private->dev_id.devno, ret);
833                 /* Put reference for new parent. */
834                 put_device(&sch->dev);
835                 return;
836         }
837         sch_set_cdev(other_sch, NULL);
838         /* No need to keep a subchannel without ccw device around. */
839         css_sch_device_unregister(other_sch);
840         sch_attach_device(sch, cdev);
841         /* Put reference for old parent. */
842         put_device(&other_sch->dev);
843 }
844
845 static void sch_attach_orphaned_device(struct subchannel *sch,
846                                        struct ccw_device *cdev)
847 {
848         int ret;
849         struct subchannel *pseudo_sch;
850
851         /* Get reference for new parent. */
852         if (!get_device(&sch->dev))
853                 return;
854         pseudo_sch = to_subchannel(cdev->dev.parent);
855         /*
856          * Try to move the ccw device to its new subchannel.
857          * Note: device_move() changes cdev->dev.parent
858          */
859         ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
860         if (ret) {
861                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
862                               "failed (ret=%d)!\n",
863                               cdev->private->dev_id.ssid,
864                               cdev->private->dev_id.devno, ret);
865                 /* Put reference for new parent. */
866                 put_device(&sch->dev);
867                 return;
868         }
869         sch_attach_device(sch, cdev);
870         /* Put reference on pseudo subchannel. */
871         put_device(&pseudo_sch->dev);
872 }
873
874 static void sch_create_and_recog_new_device(struct subchannel *sch)
875 {
876         struct ccw_device *cdev;
877
878         /* Need to allocate a new ccw device. */
879         cdev = io_subchannel_create_ccwdev(sch);
880         if (IS_ERR(cdev)) {
881                 /* OK, we did everything we could... */
882                 css_sch_device_unregister(sch);
883                 return;
884         }
885         spin_lock_irq(sch->lock);
886         sch_set_cdev(sch, cdev);
887         spin_unlock_irq(sch->lock);
888         /* Start recognition for the new ccw device. */
889         if (io_subchannel_recog(cdev, sch)) {
890                 spin_lock_irq(sch->lock);
891                 sch_set_cdev(sch, NULL);
892                 spin_unlock_irq(sch->lock);
893                 css_sch_device_unregister(sch);
894                 /* Put reference from io_subchannel_create_ccwdev(). */
895                 put_device(&sch->dev);
896                 /* Give up initial reference. */
897                 put_device(&cdev->dev);
898         }
899 }
900
901
902 void ccw_device_move_to_orphanage(struct work_struct *work)
903 {
904         struct ccw_device_private *priv;
905         struct ccw_device *cdev;
906         struct ccw_device *replacing_cdev;
907         struct subchannel *sch;
908         int ret;
909         struct channel_subsystem *css;
910         struct ccw_dev_id dev_id;
911
912         priv = container_of(work, struct ccw_device_private, kick_work);
913         cdev = priv->cdev;
914         sch = to_subchannel(cdev->dev.parent);
915         css = to_css(sch->dev.parent);
916         dev_id.devno = sch->schib.pmcw.dev;
917         dev_id.ssid = sch->schid.ssid;
918
919         /* Increase refcount for pseudo subchannel. */
920         get_device(&css->pseudo_subchannel->dev);
921         /*
922          * Move the orphaned ccw device to the orphanage so the replacing
923          * ccw device can take its place on the subchannel.
924          * Note: device_move() changes cdev->dev.parent
925          */
926         ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
927                 DPM_ORDER_NONE);
928         if (ret) {
929                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
930                               "(ret=%d)!\n", cdev->private->dev_id.ssid,
931                               cdev->private->dev_id.devno, ret);
932                 /* Decrease refcount for pseudo subchannel again. */
933                 put_device(&css->pseudo_subchannel->dev);
934                 return;
935         }
936         cdev->ccwlock = css->pseudo_subchannel->lock;
937         /*
938          * Search for the replacing ccw device
939          * - among the disconnected devices
940          * - in the orphanage
941          */
942         replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
943         if (replacing_cdev) {
944                 sch_attach_disconnected_device(sch, replacing_cdev);
945                 /* Release reference from get_disc_ccwdev_by_dev_id() */
946                 put_device(&replacing_cdev->dev);
947                 /* Release reference of subchannel from old cdev. */
948                 put_device(&sch->dev);
949                 return;
950         }
951         replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
952         if (replacing_cdev) {
953                 sch_attach_orphaned_device(sch, replacing_cdev);
954                 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
955                 put_device(&replacing_cdev->dev);
956                 /* Release reference of subchannel from old cdev. */
957                 put_device(&sch->dev);
958                 return;
959         }
960         sch_create_and_recog_new_device(sch);
961         /* Release reference of subchannel from old cdev. */
962         put_device(&sch->dev);
963 }
964
965 /*
966  * Register recognized device.
967  */
968 static void
969 io_subchannel_register(struct work_struct *work)
970 {
971         struct ccw_device_private *priv;
972         struct ccw_device *cdev;
973         struct subchannel *sch;
974         int ret;
975         unsigned long flags;
976
977         priv = container_of(work, struct ccw_device_private, kick_work);
978         cdev = priv->cdev;
979         sch = to_subchannel(cdev->dev.parent);
980         /*
981          * Check if subchannel is still registered. It may have become
982          * unregistered if a machine check hit us after finishing
983          * device recognition but before the register work could be
984          * queued.
985          */
986         if (!device_is_registered(&sch->dev))
987                 goto out_err;
988         css_update_ssd_info(sch);
989         /*
990          * io_subchannel_register() will also be called after device
991          * recognition has been done for a boxed device (which will already
992          * be registered). We need to reprobe since we may now have sense id
993          * information.
994          */
995         if (device_is_registered(&cdev->dev)) {
996                 if (!cdev->drv) {
997                         ret = device_reprobe(&cdev->dev);
998                         if (ret)
999                                 /* We can't do much here. */
1000                                 CIO_MSG_EVENT(0, "device_reprobe() returned"
1001                                               " %d for 0.%x.%04x\n", ret,
1002                                               cdev->private->dev_id.ssid,
1003                                               cdev->private->dev_id.devno);
1004                 }
1005                 goto out;
1006         }
1007         /*
1008          * Now we know this subchannel will stay, we can throw
1009          * our delayed uevent.
1010          */
1011         dev_set_uevent_suppress(&sch->dev, 0);
1012         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1013         /* make it known to the system */
1014         ret = ccw_device_register(cdev);
1015         if (ret) {
1016                 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
1017                               cdev->private->dev_id.ssid,
1018                               cdev->private->dev_id.devno, ret);
1019                 spin_lock_irqsave(sch->lock, flags);
1020                 sch_set_cdev(sch, NULL);
1021                 spin_unlock_irqrestore(sch->lock, flags);
1022                 /* Release initial device reference. */
1023                 put_device(&cdev->dev);
1024                 goto out_err;
1025         }
1026 out:
1027         cdev->private->flags.recog_done = 1;
1028         wake_up(&cdev->private->wait_q);
1029 out_err:
1030         /* Release reference for workqueue processing. */
1031         put_device(&cdev->dev);
1032         if (atomic_dec_and_test(&ccw_device_init_count))
1033                 wake_up(&ccw_device_init_wq);
1034 }
1035
1036 static void ccw_device_call_sch_unregister(struct work_struct *work)
1037 {
1038         struct ccw_device_private *priv;
1039         struct ccw_device *cdev;
1040         struct subchannel *sch;
1041
1042         priv = container_of(work, struct ccw_device_private, kick_work);
1043         cdev = priv->cdev;
1044         /* Get subchannel reference for local processing. */
1045         if (!get_device(cdev->dev.parent))
1046                 return;
1047         sch = to_subchannel(cdev->dev.parent);
1048         css_sch_device_unregister(sch);
1049         /* Release cdev reference for workqueue processing.*/
1050         put_device(&cdev->dev);
1051         /* Release subchannel reference for local processing. */
1052         put_device(&sch->dev);
1053 }
1054
1055 void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1056 {
1057         /* Get cdev reference for workqueue processing. */
1058         if (!get_device(&cdev->dev))
1059                 return;
1060         PREPARE_WORK(&cdev->private->kick_work,
1061                      ccw_device_call_sch_unregister);
1062         queue_work(slow_path_wq, &cdev->private->kick_work);
1063 }
1064
1065 /*
1066  * subchannel recognition done. Called from the state machine.
1067  */
1068 void
1069 io_subchannel_recog_done(struct ccw_device *cdev)
1070 {
1071         if (css_init_done == 0) {
1072                 cdev->private->flags.recog_done = 1;
1073                 return;
1074         }
1075         switch (cdev->private->state) {
1076         case DEV_STATE_BOXED:
1077                 /* Device did not respond in time. */
1078         case DEV_STATE_NOT_OPER:
1079                 cdev->private->flags.recog_done = 1;
1080                 ccw_device_schedule_sch_unregister(cdev);
1081                 if (atomic_dec_and_test(&ccw_device_init_count))
1082                         wake_up(&ccw_device_init_wq);
1083                 break;
1084         case DEV_STATE_OFFLINE:
1085                 /* 
1086                  * We can't register the device in interrupt context so
1087                  * we schedule a work item.
1088                  */
1089                 if (!get_device(&cdev->dev))
1090                         break;
1091                 PREPARE_WORK(&cdev->private->kick_work,
1092                              io_subchannel_register);
1093                 queue_work(slow_path_wq, &cdev->private->kick_work);
1094                 break;
1095         }
1096 }
1097
1098 static int
1099 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1100 {
1101         int rc;
1102         struct ccw_device_private *priv;
1103
1104         sch_set_cdev(sch, cdev);
1105         cdev->ccwlock = sch->lock;
1106
1107         /* Init private data. */
1108         priv = cdev->private;
1109         priv->dev_id.devno = sch->schib.pmcw.dev;
1110         priv->dev_id.ssid = sch->schid.ssid;
1111         priv->schid = sch->schid;
1112         priv->state = DEV_STATE_NOT_OPER;
1113         INIT_LIST_HEAD(&priv->cmb_list);
1114         init_waitqueue_head(&priv->wait_q);
1115         init_timer(&priv->timer);
1116
1117         /* Increase counter of devices currently in recognition. */
1118         atomic_inc(&ccw_device_init_count);
1119
1120         /* Start async. device sensing. */
1121         spin_lock_irq(sch->lock);
1122         rc = ccw_device_recognition(cdev);
1123         spin_unlock_irq(sch->lock);
1124         if (rc) {
1125                 if (atomic_dec_and_test(&ccw_device_init_count))
1126                         wake_up(&ccw_device_init_wq);
1127         }
1128         return rc;
1129 }
1130
1131 static void ccw_device_move_to_sch(struct work_struct *work)
1132 {
1133         struct ccw_device_private *priv;
1134         int rc;
1135         struct subchannel *sch;
1136         struct ccw_device *cdev;
1137         struct subchannel *former_parent;
1138
1139         priv = container_of(work, struct ccw_device_private, kick_work);
1140         sch = priv->sch;
1141         cdev = priv->cdev;
1142         former_parent = to_subchannel(cdev->dev.parent);
1143         /* Get reference for new parent. */
1144         if (!get_device(&sch->dev))
1145                 return;
1146         mutex_lock(&sch->reg_mutex);
1147         /*
1148          * Try to move the ccw device to its new subchannel.
1149          * Note: device_move() changes cdev->dev.parent
1150          */
1151         rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
1152         mutex_unlock(&sch->reg_mutex);
1153         if (rc) {
1154                 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1155                               "0.%x.%04x failed (ret=%d)!\n",
1156                               cdev->private->dev_id.ssid,
1157                               cdev->private->dev_id.devno, sch->schid.ssid,
1158                               sch->schid.sch_no, rc);
1159                 css_sch_device_unregister(sch);
1160                 /* Put reference for new parent again. */
1161                 put_device(&sch->dev);
1162                 goto out;
1163         }
1164         if (!sch_is_pseudo_sch(former_parent)) {
1165                 spin_lock_irq(former_parent->lock);
1166                 sch_set_cdev(former_parent, NULL);
1167                 spin_unlock_irq(former_parent->lock);
1168                 css_sch_device_unregister(former_parent);
1169                 /* Reset intparm to zeroes. */
1170                 former_parent->config.intparm = 0;
1171                 cio_commit_config(former_parent);
1172         }
1173         sch_attach_device(sch, cdev);
1174 out:
1175         /* Put reference for old parent. */
1176         put_device(&former_parent->dev);
1177         put_device(&cdev->dev);
1178 }
1179
1180 static void io_subchannel_irq(struct subchannel *sch)
1181 {
1182         struct ccw_device *cdev;
1183
1184         cdev = sch_get_cdev(sch);
1185
1186         CIO_TRACE_EVENT(6, "IRQ");
1187         CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1188         if (cdev)
1189                 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1190 }
1191
1192 void io_subchannel_init_config(struct subchannel *sch)
1193 {
1194         memset(&sch->config, 0, sizeof(sch->config));
1195         sch->config.csense = 1;
1196         /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1197         if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1198                 sch->config.mp = 1;
1199 }
1200
1201 static void io_subchannel_init_fields(struct subchannel *sch)
1202 {
1203         if (cio_is_console(sch->schid))
1204                 sch->opm = 0xff;
1205         else
1206                 sch->opm = chp_get_sch_opm(sch);
1207         sch->lpm = sch->schib.pmcw.pam & sch->opm;
1208         sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1209
1210         CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1211                       " - PIM = %02X, PAM = %02X, POM = %02X\n",
1212                       sch->schib.pmcw.dev, sch->schid.ssid,
1213                       sch->schid.sch_no, sch->schib.pmcw.pim,
1214                       sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1215
1216         io_subchannel_init_config(sch);
1217 }
1218
1219 static void io_subchannel_do_unreg(struct work_struct *work)
1220 {
1221         struct subchannel *sch;
1222
1223         sch = container_of(work, struct subchannel, work);
1224         css_sch_device_unregister(sch);
1225         put_device(&sch->dev);
1226 }
1227
1228 /* Schedule unregister if we have no cdev. */
1229 static void io_subchannel_schedule_removal(struct subchannel *sch)
1230 {
1231         get_device(&sch->dev);
1232         INIT_WORK(&sch->work, io_subchannel_do_unreg);
1233         queue_work(slow_path_wq, &sch->work);
1234 }
1235
1236 /*
1237  * Note: We always return 0 so that we bind to the device even on error.
1238  * This is needed so that our remove function is called on unregister.
1239  */
1240 static int io_subchannel_probe(struct subchannel *sch)
1241 {
1242         struct ccw_device *cdev;
1243         int rc;
1244         unsigned long flags;
1245         struct ccw_dev_id dev_id;
1246
1247         cdev = sch_get_cdev(sch);
1248         if (cdev) {
1249                 rc = sysfs_create_group(&sch->dev.kobj,
1250                                         &io_subchannel_attr_group);
1251                 if (rc)
1252                         CIO_MSG_EVENT(0, "Failed to create io subchannel "
1253                                       "attributes for subchannel "
1254                                       "0.%x.%04x (rc=%d)\n",
1255                                       sch->schid.ssid, sch->schid.sch_no, rc);
1256                 /*
1257                  * This subchannel already has an associated ccw_device.
1258                  * Throw the delayed uevent for the subchannel, register
1259                  * the ccw_device and exit. This happens for all early
1260                  * devices, e.g. the console.
1261                  */
1262                 dev_set_uevent_suppress(&sch->dev, 0);
1263                 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1264                 cdev->dev.groups = ccwdev_attr_groups;
1265                 device_initialize(&cdev->dev);
1266                 ccw_device_register(cdev);
1267                 /*
1268                  * Check if the device is already online. If it is
1269                  * the reference count needs to be corrected since we
1270                  * didn't obtain a reference in ccw_device_set_online.
1271                  */
1272                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1273                     cdev->private->state != DEV_STATE_OFFLINE &&
1274                     cdev->private->state != DEV_STATE_BOXED)
1275                         get_device(&cdev->dev);
1276                 return 0;
1277         }
1278         io_subchannel_init_fields(sch);
1279         rc = cio_commit_config(sch);
1280         if (rc)
1281                 goto out_schedule;
1282         rc = sysfs_create_group(&sch->dev.kobj,
1283                                 &io_subchannel_attr_group);
1284         if (rc)
1285                 goto out_schedule;
1286         /* Allocate I/O subchannel private data. */
1287         sch->private = kzalloc(sizeof(struct io_subchannel_private),
1288                                GFP_KERNEL | GFP_DMA);
1289         if (!sch->private)
1290                 goto out_err;
1291         /*
1292          * First check if a fitting device may be found amongst the
1293          * disconnected devices or in the orphanage.
1294          */
1295         dev_id.devno = sch->schib.pmcw.dev;
1296         dev_id.ssid = sch->schid.ssid;
1297         cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1298         if (!cdev)
1299                 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1300                                                      &dev_id);
1301         if (cdev) {
1302                 /*
1303                  * Schedule moving the device until when we have a registered
1304                  * subchannel to move to and succeed the probe. We can
1305                  * unregister later again, when the probe is through.
1306                  */
1307                 cdev->private->sch = sch;
1308                 PREPARE_WORK(&cdev->private->kick_work,
1309                              ccw_device_move_to_sch);
1310                 queue_work(slow_path_wq, &cdev->private->kick_work);
1311                 return 0;
1312         }
1313         cdev = io_subchannel_create_ccwdev(sch);
1314         if (IS_ERR(cdev))
1315                 goto out_err;
1316         rc = io_subchannel_recog(cdev, sch);
1317         if (rc) {
1318                 spin_lock_irqsave(sch->lock, flags);
1319                 io_subchannel_recog_done(cdev);
1320                 spin_unlock_irqrestore(sch->lock, flags);
1321         }
1322         return 0;
1323 out_err:
1324         kfree(sch->private);
1325         sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1326 out_schedule:
1327         io_subchannel_schedule_removal(sch);
1328         return 0;
1329 }
1330
1331 static int
1332 io_subchannel_remove (struct subchannel *sch)
1333 {
1334         struct ccw_device *cdev;
1335         unsigned long flags;
1336
1337         cdev = sch_get_cdev(sch);
1338         if (!cdev)
1339                 return 0;
1340         /* Set ccw device to not operational and drop reference. */
1341         spin_lock_irqsave(cdev->ccwlock, flags);
1342         sch_set_cdev(sch, NULL);
1343         cdev->private->state = DEV_STATE_NOT_OPER;
1344         spin_unlock_irqrestore(cdev->ccwlock, flags);
1345         ccw_device_unregister(cdev);
1346         kfree(sch->private);
1347         sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1348         return 0;
1349 }
1350
1351 static int io_subchannel_notify(struct subchannel *sch, int event)
1352 {
1353         struct ccw_device *cdev;
1354
1355         cdev = sch_get_cdev(sch);
1356         if (!cdev)
1357                 return 0;
1358         return ccw_device_notify(cdev, event);
1359 }
1360
1361 static void io_subchannel_verify(struct subchannel *sch)
1362 {
1363         struct ccw_device *cdev;
1364
1365         cdev = sch_get_cdev(sch);
1366         if (cdev)
1367                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1368 }
1369
1370 static int check_for_io_on_path(struct subchannel *sch, int mask)
1371 {
1372         if (cio_update_schib(sch))
1373                 return 0;
1374         if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1375                 return 1;
1376         return 0;
1377 }
1378
1379 static void terminate_internal_io(struct subchannel *sch,
1380                                   struct ccw_device *cdev)
1381 {
1382         if (cio_clear(sch)) {
1383                 /* Recheck device in case clear failed. */
1384                 sch->lpm = 0;
1385                 if (cdev->online)
1386                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1387                 else
1388                         css_schedule_eval(sch->schid);
1389                 return;
1390         }
1391         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1392         /* Request retry of internal operation. */
1393         cdev->private->flags.intretry = 1;
1394         /* Call handler. */
1395         if (cdev->handler)
1396                 cdev->handler(cdev, cdev->private->intparm,
1397                               ERR_PTR(-EIO));
1398 }
1399
1400 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1401 {
1402         struct ccw_device *cdev;
1403
1404         cdev = sch_get_cdev(sch);
1405         if (!cdev)
1406                 return;
1407         if (check_for_io_on_path(sch, mask)) {
1408                 if (cdev->private->state == DEV_STATE_ONLINE)
1409                         ccw_device_kill_io(cdev);
1410                 else {
1411                         terminate_internal_io(sch, cdev);
1412                         /* Re-start path verification. */
1413                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1414                 }
1415         } else
1416                 /* trigger path verification. */
1417                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1418
1419 }
1420
1421 static int io_subchannel_chp_event(struct subchannel *sch,
1422                                    struct chp_link *link, int event)
1423 {
1424         int mask;
1425
1426         mask = chp_ssd_get_mask(&sch->ssd_info, link);
1427         if (!mask)
1428                 return 0;
1429         switch (event) {
1430         case CHP_VARY_OFF:
1431                 sch->opm &= ~mask;
1432                 sch->lpm &= ~mask;
1433                 io_subchannel_terminate_path(sch, mask);
1434                 break;
1435         case CHP_VARY_ON:
1436                 sch->opm |= mask;
1437                 sch->lpm |= mask;
1438                 io_subchannel_verify(sch);
1439                 break;
1440         case CHP_OFFLINE:
1441                 if (cio_update_schib(sch))
1442                         return -ENODEV;
1443                 io_subchannel_terminate_path(sch, mask);
1444                 break;
1445         case CHP_ONLINE:
1446                 if (cio_update_schib(sch))
1447                         return -ENODEV;
1448                 sch->lpm |= mask & sch->opm;
1449                 io_subchannel_verify(sch);
1450                 break;
1451         }
1452         return 0;
1453 }
1454
1455 static void
1456 io_subchannel_shutdown(struct subchannel *sch)
1457 {
1458         struct ccw_device *cdev;
1459         int ret;
1460
1461         cdev = sch_get_cdev(sch);
1462
1463         if (cio_is_console(sch->schid))
1464                 return;
1465         if (!sch->schib.pmcw.ena)
1466                 /* Nothing to do. */
1467                 return;
1468         ret = cio_disable_subchannel(sch);
1469         if (ret != -EBUSY)
1470                 /* Subchannel is disabled, we're done. */
1471                 return;
1472         cdev->private->state = DEV_STATE_QUIESCE;
1473         if (cdev->handler)
1474                 cdev->handler(cdev, cdev->private->intparm,
1475                               ERR_PTR(-EIO));
1476         ret = ccw_device_cancel_halt_clear(cdev);
1477         if (ret == -EBUSY) {
1478                 ccw_device_set_timeout(cdev, HZ/10);
1479                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1480         }
1481         cio_disable_subchannel(sch);
1482 }
1483
1484 static int io_subchannel_get_status(struct subchannel *sch)
1485 {
1486         struct schib schib;
1487
1488         if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1489                 return CIO_GONE;
1490         if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1491                 return CIO_REVALIDATE;
1492         if (!sch->lpm)
1493                 return CIO_NO_PATH;
1494         return CIO_OPER;
1495 }
1496
1497 static int device_is_disconnected(struct ccw_device *cdev)
1498 {
1499         if (!cdev)
1500                 return 0;
1501         return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1502                 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1503 }
1504
1505 static int recovery_check(struct device *dev, void *data)
1506 {
1507         struct ccw_device *cdev = to_ccwdev(dev);
1508         int *redo = data;
1509
1510         spin_lock_irq(cdev->ccwlock);
1511         switch (cdev->private->state) {
1512         case DEV_STATE_DISCONNECTED:
1513                 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1514                               cdev->private->dev_id.ssid,
1515                               cdev->private->dev_id.devno);
1516                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1517                 *redo = 1;
1518                 break;
1519         case DEV_STATE_DISCONNECTED_SENSE_ID:
1520                 *redo = 1;
1521                 break;
1522         }
1523         spin_unlock_irq(cdev->ccwlock);
1524
1525         return 0;
1526 }
1527
1528 static void recovery_work_func(struct work_struct *unused)
1529 {
1530         int redo = 0;
1531
1532         bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1533         if (redo) {
1534                 spin_lock_irq(&recovery_lock);
1535                 if (!timer_pending(&recovery_timer)) {
1536                         if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1537                                 recovery_phase++;
1538                         mod_timer(&recovery_timer, jiffies +
1539                                   recovery_delay[recovery_phase] * HZ);
1540                 }
1541                 spin_unlock_irq(&recovery_lock);
1542         } else
1543                 CIO_MSG_EVENT(4, "recovery: end\n");
1544 }
1545
1546 static DECLARE_WORK(recovery_work, recovery_work_func);
1547
1548 static void recovery_func(unsigned long data)
1549 {
1550         /*
1551          * We can't do our recovery in softirq context and it's not
1552          * performance critical, so we schedule it.
1553          */
1554         schedule_work(&recovery_work);
1555 }
1556
1557 static void ccw_device_schedule_recovery(void)
1558 {
1559         unsigned long flags;
1560
1561         CIO_MSG_EVENT(4, "recovery: schedule\n");
1562         spin_lock_irqsave(&recovery_lock, flags);
1563         if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1564                 recovery_phase = 0;
1565                 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1566         }
1567         spin_unlock_irqrestore(&recovery_lock, flags);
1568 }
1569
1570 static int purge_fn(struct device *dev, void *data)
1571 {
1572         struct ccw_device *cdev = to_ccwdev(dev);
1573         struct ccw_device_private *priv = cdev->private;
1574         int unreg;
1575
1576         spin_lock_irq(cdev->ccwlock);
1577         unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1578                 (priv->state == DEV_STATE_OFFLINE);
1579         spin_unlock_irq(cdev->ccwlock);
1580         if (!unreg)
1581                 goto out;
1582         CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1583                       priv->dev_id.devno);
1584         ccw_device_schedule_sch_unregister(cdev);
1585
1586 out:
1587         /* Abort loop in case of pending signal. */
1588         if (signal_pending(current))
1589                 return -EINTR;
1590
1591         return 0;
1592 }
1593
1594 /**
1595  * ccw_purge_blacklisted - purge unused, blacklisted devices
1596  *
1597  * Unregister all ccw devices that are offline and on the blacklist.
1598  */
1599 int ccw_purge_blacklisted(void)
1600 {
1601         CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1602         bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1603         return 0;
1604 }
1605
1606 static void device_set_disconnected(struct ccw_device *cdev)
1607 {
1608         if (!cdev)
1609                 return;
1610         ccw_device_set_timeout(cdev, 0);
1611         cdev->private->flags.fake_irb = 0;
1612         cdev->private->state = DEV_STATE_DISCONNECTED;
1613         if (cdev->online)
1614                 ccw_device_schedule_recovery();
1615 }
1616
1617 void ccw_device_set_notoper(struct ccw_device *cdev)
1618 {
1619         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1620
1621         CIO_TRACE_EVENT(2, "notoper");
1622         CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1623         ccw_device_set_timeout(cdev, 0);
1624         cio_disable_subchannel(sch);
1625         cdev->private->state = DEV_STATE_NOT_OPER;
1626 }
1627
1628 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1629 {
1630         int event, ret, disc;
1631         unsigned long flags;
1632         enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1633         struct ccw_device *cdev;
1634
1635         spin_lock_irqsave(sch->lock, flags);
1636         cdev = sch_get_cdev(sch);
1637         disc = device_is_disconnected(cdev);
1638         if (disc && slow) {
1639                 /* Disconnected devices are evaluated directly only.*/
1640                 spin_unlock_irqrestore(sch->lock, flags);
1641                 return 0;
1642         }
1643         /* No interrupt after machine check - kill pending timers. */
1644         if (cdev)
1645                 ccw_device_set_timeout(cdev, 0);
1646         if (!disc && !slow) {
1647                 /* Non-disconnected devices are evaluated on the slow path. */
1648                 spin_unlock_irqrestore(sch->lock, flags);
1649                 return -EAGAIN;
1650         }
1651         event = io_subchannel_get_status(sch);
1652         CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1653                       sch->schid.ssid, sch->schid.sch_no, event,
1654                       disc ? "disconnected" : "normal",
1655                       slow ? "slow" : "fast");
1656         /* Analyze subchannel status. */
1657         action = NONE;
1658         switch (event) {
1659         case CIO_NO_PATH:
1660                 if (disc) {
1661                         /* Check if paths have become available. */
1662                         action = REPROBE;
1663                         break;
1664                 }
1665                 /* fall through */
1666         case CIO_GONE:
1667                 /* Ask driver what to do with device. */
1668                 if (io_subchannel_notify(sch, event))
1669                         action = DISC;
1670                 else
1671                         action = UNREGISTER;
1672                 break;
1673         case CIO_REVALIDATE:
1674                 /* Device will be removed, so no notify necessary. */
1675                 if (disc)
1676                         /* Reprobe because immediate unregister might block. */
1677                         action = REPROBE;
1678                 else
1679                         action = UNREGISTER_PROBE;
1680                 break;
1681         case CIO_OPER:
1682                 if (disc)
1683                         /* Get device operational again. */
1684                         action = REPROBE;
1685                 break;
1686         }
1687         /* Perform action. */
1688         ret = 0;
1689         switch (action) {
1690         case UNREGISTER:
1691         case UNREGISTER_PROBE:
1692                 ccw_device_set_notoper(cdev);
1693                 /* Unregister device (will use subchannel lock). */
1694                 spin_unlock_irqrestore(sch->lock, flags);
1695                 css_sch_device_unregister(sch);
1696                 spin_lock_irqsave(sch->lock, flags);
1697                 break;
1698         case REPROBE:
1699                 ccw_device_trigger_reprobe(cdev);
1700                 break;
1701         case DISC:
1702                 device_set_disconnected(cdev);
1703                 break;
1704         default:
1705                 break;
1706         }
1707         spin_unlock_irqrestore(sch->lock, flags);
1708         /* Probe if necessary. */
1709         if (action == UNREGISTER_PROBE)
1710                 ret = css_probe_device(sch->schid);
1711
1712         return ret;
1713 }
1714
1715 #ifdef CONFIG_CCW_CONSOLE
1716 static struct ccw_device console_cdev;
1717 static struct ccw_device_private console_private;
1718 static int console_cdev_in_use;
1719
1720 static DEFINE_SPINLOCK(ccw_console_lock);
1721
1722 spinlock_t * cio_get_console_lock(void)
1723 {
1724         return &ccw_console_lock;
1725 }
1726
1727 static int ccw_device_console_enable(struct ccw_device *cdev,
1728                                      struct subchannel *sch)
1729 {
1730         int rc;
1731
1732         /* Attach subchannel private data. */
1733         sch->private = cio_get_console_priv();
1734         memset(sch->private, 0, sizeof(struct io_subchannel_private));
1735         io_subchannel_init_fields(sch);
1736         rc = cio_commit_config(sch);
1737         if (rc)
1738                 return rc;
1739         sch->driver = &io_subchannel_driver;
1740         /* Initialize the ccw_device structure. */
1741         cdev->dev.parent= &sch->dev;
1742         rc = io_subchannel_recog(cdev, sch);
1743         if (rc)
1744                 return rc;
1745
1746         /* Now wait for the async. recognition to come to an end. */
1747         spin_lock_irq(cdev->ccwlock);
1748         while (!dev_fsm_final_state(cdev))
1749                 wait_cons_dev();
1750         rc = -EIO;
1751         if (cdev->private->state != DEV_STATE_OFFLINE)
1752                 goto out_unlock;
1753         ccw_device_online(cdev);
1754         while (!dev_fsm_final_state(cdev))
1755                 wait_cons_dev();
1756         if (cdev->private->state != DEV_STATE_ONLINE)
1757                 goto out_unlock;
1758         rc = 0;
1759 out_unlock:
1760         spin_unlock_irq(cdev->ccwlock);
1761         return 0;
1762 }
1763
1764 struct ccw_device *
1765 ccw_device_probe_console(void)
1766 {
1767         struct subchannel *sch;
1768         int ret;
1769
1770         if (xchg(&console_cdev_in_use, 1) != 0)
1771                 return ERR_PTR(-EBUSY);
1772         sch = cio_probe_console();
1773         if (IS_ERR(sch)) {
1774                 console_cdev_in_use = 0;
1775                 return (void *) sch;
1776         }
1777         memset(&console_cdev, 0, sizeof(struct ccw_device));
1778         memset(&console_private, 0, sizeof(struct ccw_device_private));
1779         console_cdev.private = &console_private;
1780         console_private.cdev = &console_cdev;
1781         ret = ccw_device_console_enable(&console_cdev, sch);
1782         if (ret) {
1783                 cio_release_console();
1784                 console_cdev_in_use = 0;
1785                 return ERR_PTR(ret);
1786         }
1787         console_cdev.online = 1;
1788         return &console_cdev;
1789 }
1790
1791 static int ccw_device_pm_restore(struct device *dev);
1792
1793 int ccw_device_force_console(void)
1794 {
1795         if (!console_cdev_in_use)
1796                 return -ENODEV;
1797         return ccw_device_pm_restore(&console_cdev.dev);
1798 }
1799 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1800 #endif
1801
1802 /*
1803  * get ccw_device matching the busid, but only if owned by cdrv
1804  */
1805 static int
1806 __ccwdev_check_busid(struct device *dev, void *id)
1807 {
1808         char *bus_id;
1809
1810         bus_id = id;
1811
1812         return (strcmp(bus_id, dev_name(dev)) == 0);
1813 }
1814
1815
1816 /**
1817  * get_ccwdev_by_busid() - obtain device from a bus id
1818  * @cdrv: driver the device is owned by
1819  * @bus_id: bus id of the device to be searched
1820  *
1821  * This function searches all devices owned by @cdrv for a device with a bus
1822  * id matching @bus_id.
1823  * Returns:
1824  *  If a match is found, its reference count of the found device is increased
1825  *  and it is returned; else %NULL is returned.
1826  */
1827 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1828                                        const char *bus_id)
1829 {
1830         struct device *dev;
1831         struct device_driver *drv;
1832
1833         drv = get_driver(&cdrv->driver);
1834         if (!drv)
1835                 return NULL;
1836
1837         dev = driver_find_device(drv, NULL, (void *)bus_id,
1838                                  __ccwdev_check_busid);
1839         put_driver(drv);
1840
1841         return dev ? to_ccwdev(dev) : NULL;
1842 }
1843
1844 /************************** device driver handling ************************/
1845
1846 /* This is the implementation of the ccw_driver class. The probe, remove
1847  * and release methods are initially very similar to the device_driver
1848  * implementations, with the difference that they have ccw_device
1849  * arguments.
1850  *
1851  * A ccw driver also contains the information that is needed for
1852  * device matching.
1853  */
1854 static int
1855 ccw_device_probe (struct device *dev)
1856 {
1857         struct ccw_device *cdev = to_ccwdev(dev);
1858         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1859         int ret;
1860
1861         cdev->drv = cdrv; /* to let the driver call _set_online */
1862
1863         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1864
1865         if (ret) {
1866                 cdev->drv = NULL;
1867                 return ret;
1868         }
1869
1870         return 0;
1871 }
1872
1873 static int
1874 ccw_device_remove (struct device *dev)
1875 {
1876         struct ccw_device *cdev = to_ccwdev(dev);
1877         struct ccw_driver *cdrv = cdev->drv;
1878         int ret;
1879
1880         if (cdrv->remove)
1881                 cdrv->remove(cdev);
1882         if (cdev->online) {
1883                 cdev->online = 0;
1884                 spin_lock_irq(cdev->ccwlock);
1885                 ret = ccw_device_offline(cdev);
1886                 spin_unlock_irq(cdev->ccwlock);
1887                 if (ret == 0)
1888                         wait_event(cdev->private->wait_q,
1889                                    dev_fsm_final_state(cdev));
1890                 else
1891                         CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1892                                       "device 0.%x.%04x\n",
1893                                       ret, cdev->private->dev_id.ssid,
1894                                       cdev->private->dev_id.devno);
1895                 /* Give up reference obtained in ccw_device_set_online(). */
1896                 put_device(&cdev->dev);
1897         }
1898         ccw_device_set_timeout(cdev, 0);
1899         cdev->drv = NULL;
1900         return 0;
1901 }
1902
1903 static void ccw_device_shutdown(struct device *dev)
1904 {
1905         struct ccw_device *cdev;
1906
1907         cdev = to_ccwdev(dev);
1908         if (cdev->drv && cdev->drv->shutdown)
1909                 cdev->drv->shutdown(cdev);
1910         disable_cmf(cdev);
1911 }
1912
1913 static int ccw_device_pm_prepare(struct device *dev)
1914 {
1915         struct ccw_device *cdev = to_ccwdev(dev);
1916
1917         if (work_pending(&cdev->private->kick_work))
1918                 return -EAGAIN;
1919         /* Fail while device is being set online/offline. */
1920         if (atomic_read(&cdev->private->onoff))
1921                 return -EAGAIN;
1922
1923         if (cdev->online && cdev->drv && cdev->drv->prepare)
1924                 return cdev->drv->prepare(cdev);
1925
1926         return 0;
1927 }
1928
1929 static void ccw_device_pm_complete(struct device *dev)
1930 {
1931         struct ccw_device *cdev = to_ccwdev(dev);
1932
1933         if (cdev->online && cdev->drv && cdev->drv->complete)
1934                 cdev->drv->complete(cdev);
1935 }
1936
1937 static int ccw_device_pm_freeze(struct device *dev)
1938 {
1939         struct ccw_device *cdev = to_ccwdev(dev);
1940         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1941         int ret, cm_enabled;
1942
1943         /* Fail suspend while device is in transistional state. */
1944         if (!dev_fsm_final_state(cdev))
1945                 return -EAGAIN;
1946         if (!cdev->online)
1947                 return 0;
1948         if (cdev->drv && cdev->drv->freeze) {
1949                 ret = cdev->drv->freeze(cdev);
1950                 if (ret)
1951                         return ret;
1952         }
1953
1954         spin_lock_irq(sch->lock);
1955         cm_enabled = cdev->private->cmb != NULL;
1956         spin_unlock_irq(sch->lock);
1957         if (cm_enabled) {
1958                 /* Don't have the css write on memory. */
1959                 ret = ccw_set_cmf(cdev, 0);
1960                 if (ret)
1961                         return ret;
1962         }
1963         /* From here on, disallow device driver I/O. */
1964         spin_lock_irq(sch->lock);
1965         ret = cio_disable_subchannel(sch);
1966         spin_unlock_irq(sch->lock);
1967
1968         return ret;
1969 }
1970
1971 static int ccw_device_pm_thaw(struct device *dev)
1972 {
1973         struct ccw_device *cdev = to_ccwdev(dev);
1974         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1975         int ret, cm_enabled;
1976
1977         if (!cdev->online)
1978                 return 0;
1979
1980         spin_lock_irq(sch->lock);
1981         /* Allow device driver I/O again. */
1982         ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1983         cm_enabled = cdev->private->cmb != NULL;
1984         spin_unlock_irq(sch->lock);
1985         if (ret)
1986                 return ret;
1987
1988         if (cm_enabled) {
1989                 ret = ccw_set_cmf(cdev, 1);
1990                 if (ret)
1991                         return ret;
1992         }
1993
1994         if (cdev->drv && cdev->drv->thaw)
1995                 ret = cdev->drv->thaw(cdev);
1996
1997         return ret;
1998 }
1999
2000 static void __ccw_device_pm_restore(struct ccw_device *cdev)
2001 {
2002         struct subchannel *sch = to_subchannel(cdev->dev.parent);
2003         int ret;
2004
2005         if (cio_is_console(sch->schid))
2006                 goto out;
2007         /*
2008          * While we were sleeping, devices may have gone or become
2009          * available again. Kick re-detection.
2010          */
2011         spin_lock_irq(sch->lock);
2012         cdev->private->flags.resuming = 1;
2013         ret = ccw_device_recognition(cdev);
2014         spin_unlock_irq(sch->lock);
2015         if (ret) {
2016                 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
2017                               "0.%x.%04x (ret=%d)\n",
2018                               cdev->private->dev_id.ssid,
2019                               cdev->private->dev_id.devno, ret);
2020                 spin_lock_irq(sch->lock);
2021                 cdev->private->state = DEV_STATE_DISCONNECTED;
2022                 spin_unlock_irq(sch->lock);
2023                 /* notify driver after the resume cb */
2024                 goto out;
2025         }
2026         wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2027                    cdev->private->state == DEV_STATE_DISCONNECTED);
2028
2029 out:
2030         cdev->private->flags.resuming = 0;
2031 }
2032
2033 static int resume_handle_boxed(struct ccw_device *cdev)
2034 {
2035         cdev->private->state = DEV_STATE_BOXED;
2036         if (ccw_device_notify(cdev, CIO_BOXED))
2037                 return 0;
2038         ccw_device_schedule_sch_unregister(cdev);
2039         return -ENODEV;
2040 }
2041
2042 static int resume_handle_disc(struct ccw_device *cdev)
2043 {
2044         cdev->private->state = DEV_STATE_DISCONNECTED;
2045         if (ccw_device_notify(cdev, CIO_GONE))
2046                 return 0;
2047         ccw_device_schedule_sch_unregister(cdev);
2048         return -ENODEV;
2049 }
2050
2051 static int ccw_device_pm_restore(struct device *dev)
2052 {
2053         struct ccw_device *cdev = to_ccwdev(dev);
2054         struct subchannel *sch = to_subchannel(cdev->dev.parent);
2055         int ret = 0, cm_enabled;
2056
2057         __ccw_device_pm_restore(cdev);
2058         spin_lock_irq(sch->lock);
2059         if (cio_is_console(sch->schid)) {
2060                 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2061                 spin_unlock_irq(sch->lock);
2062                 goto out_restore;
2063         }
2064         cdev->private->flags.donotify = 0;
2065         /* check recognition results */
2066         switch (cdev->private->state) {
2067         case DEV_STATE_OFFLINE:
2068                 break;
2069         case DEV_STATE_BOXED:
2070                 ret = resume_handle_boxed(cdev);
2071                 spin_unlock_irq(sch->lock);
2072                 if (ret)
2073                         goto out;
2074                 goto out_restore;
2075         case DEV_STATE_DISCONNECTED:
2076                 goto out_disc_unlock;
2077         default:
2078                 goto out_unreg_unlock;
2079         }
2080         /* check if the device id has changed */
2081         if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
2082                 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
2083                               "changed from %04x to %04x)\n",
2084                               sch->schid.ssid, sch->schid.sch_no,
2085                               cdev->private->dev_id.devno,
2086                               sch->schib.pmcw.dev);
2087                 goto out_unreg_unlock;
2088         }
2089         /* check if the device type has changed */
2090         if (!ccw_device_test_sense_data(cdev)) {
2091                 ccw_device_update_sense_data(cdev);
2092                 PREPARE_WORK(&cdev->private->kick_work,
2093                              ccw_device_do_unbind_bind);
2094                 queue_work(ccw_device_work, &cdev->private->kick_work);
2095                 ret = -ENODEV;
2096                 goto out_unlock;
2097         }
2098         if (!cdev->online) {
2099                 ret = 0;
2100                 goto out_unlock;
2101         }
2102         ret = ccw_device_online(cdev);
2103         if (ret)
2104                 goto out_disc_unlock;
2105
2106         cm_enabled = cdev->private->cmb != NULL;
2107         spin_unlock_irq(sch->lock);
2108
2109         wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2110         if (cdev->private->state != DEV_STATE_ONLINE) {
2111                 spin_lock_irq(sch->lock);
2112                 goto out_disc_unlock;
2113         }
2114         if (cm_enabled) {
2115                 ret = ccw_set_cmf(cdev, 1);
2116                 if (ret) {
2117                         CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2118                                       "(rc=%d)\n", cdev->private->dev_id.ssid,
2119                                       cdev->private->dev_id.devno, ret);
2120                         ret = 0;
2121                 }
2122         }
2123
2124 out_restore:
2125         if (cdev->online && cdev->drv && cdev->drv->restore)
2126                 ret = cdev->drv->restore(cdev);
2127 out:
2128         return ret;
2129
2130 out_disc_unlock:
2131         ret = resume_handle_disc(cdev);
2132         spin_unlock_irq(sch->lock);
2133         if (ret)
2134                 return ret;
2135         goto out_restore;
2136
2137 out_unreg_unlock:
2138         ccw_device_schedule_sch_unregister(cdev);
2139         ret = -ENODEV;
2140 out_unlock:
2141         spin_unlock_irq(sch->lock);
2142         return ret;
2143 }
2144
2145 static struct dev_pm_ops ccw_pm_ops = {
2146         .prepare = ccw_device_pm_prepare,
2147         .complete = ccw_device_pm_complete,
2148         .freeze = ccw_device_pm_freeze,
2149         .thaw = ccw_device_pm_thaw,
2150         .restore = ccw_device_pm_restore,
2151 };
2152
2153 struct bus_type ccw_bus_type = {
2154         .name   = "ccw",
2155         .match  = ccw_bus_match,
2156         .uevent = ccw_uevent,
2157         .probe  = ccw_device_probe,
2158         .remove = ccw_device_remove,
2159         .shutdown = ccw_device_shutdown,
2160         .pm = &ccw_pm_ops,
2161 };
2162
2163 /**
2164  * ccw_driver_register() - register a ccw driver
2165  * @cdriver: driver to be registered
2166  *
2167  * This function is mainly a wrapper around driver_register().
2168  * Returns:
2169  *   %0 on success and a negative error value on failure.
2170  */
2171 int ccw_driver_register(struct ccw_driver *cdriver)
2172 {
2173         struct device_driver *drv = &cdriver->driver;
2174
2175         drv->bus = &ccw_bus_type;
2176         drv->name = cdriver->name;
2177         drv->owner = cdriver->owner;
2178
2179         return driver_register(drv);
2180 }
2181
2182 /**
2183  * ccw_driver_unregister() - deregister a ccw driver
2184  * @cdriver: driver to be deregistered
2185  *
2186  * This function is mainly a wrapper around driver_unregister().
2187  */
2188 void ccw_driver_unregister(struct ccw_driver *cdriver)
2189 {
2190         driver_unregister(&cdriver->driver);
2191 }
2192
2193 /* Helper func for qdio. */
2194 struct subchannel_id
2195 ccw_device_get_subchannel_id(struct ccw_device *cdev)
2196 {
2197         struct subchannel *sch;
2198
2199         sch = to_subchannel(cdev->dev.parent);
2200         return sch->schid;
2201 }
2202
2203 MODULE_LICENSE("GPL");
2204 EXPORT_SYMBOL(ccw_device_set_online);
2205 EXPORT_SYMBOL(ccw_device_set_offline);
2206 EXPORT_SYMBOL(ccw_driver_register);
2207 EXPORT_SYMBOL(ccw_driver_unregister);
2208 EXPORT_SYMBOL(get_ccwdev_by_busid);
2209 EXPORT_SYMBOL(ccw_bus_type);
2210 EXPORT_SYMBOL(ccw_device_work);
2211 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);