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