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