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