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