[S390] cio: handle error during device recognition consistently
[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                 ccw_device_recognition(cdev);
492                 wait_event(cdev->private->wait_q,
493                            cdev->private->flags.recog_done);
494                 if (cdev->private->state != DEV_STATE_OFFLINE)
495                         /* recognition failed */
496                         return -EAGAIN;
497         }
498         if (cdev->drv && cdev->drv->set_online)
499                 ccw_device_set_online(cdev);
500         return 0;
501 }
502
503 static int online_store_handle_online(struct ccw_device *cdev, int force)
504 {
505         int ret;
506
507         ret = online_store_recog_and_online(cdev);
508         if (ret && !force)
509                 return ret;
510         if (force && cdev->private->state == DEV_STATE_BOXED) {
511                 ret = ccw_device_stlck(cdev);
512                 if (ret)
513                         return ret;
514                 if (cdev->id.cu_type == 0)
515                         cdev->private->state = DEV_STATE_NOT_OPER;
516                 ret = online_store_recog_and_online(cdev);
517                 if (ret)
518                         return ret;
519         }
520         return 0;
521 }
522
523 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
524                              const char *buf, size_t count)
525 {
526         struct ccw_device *cdev = to_ccwdev(dev);
527         int force, ret;
528         unsigned long i;
529
530         if ((cdev->private->state != DEV_STATE_OFFLINE &&
531              cdev->private->state != DEV_STATE_ONLINE &&
532              cdev->private->state != DEV_STATE_BOXED &&
533              cdev->private->state != DEV_STATE_DISCONNECTED) ||
534             atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
535                 return -EAGAIN;
536
537         if (cdev->drv && !try_module_get(cdev->drv->owner)) {
538                 atomic_set(&cdev->private->onoff, 0);
539                 return -EINVAL;
540         }
541         if (!strncmp(buf, "force\n", count)) {
542                 force = 1;
543                 i = 1;
544                 ret = 0;
545         } else {
546                 force = 0;
547                 ret = strict_strtoul(buf, 16, &i);
548         }
549         if (ret)
550                 goto out;
551         switch (i) {
552         case 0:
553                 ret = online_store_handle_offline(cdev);
554                 break;
555         case 1:
556                 ret = online_store_handle_online(cdev, force);
557                 break;
558         default:
559                 ret = -EINVAL;
560         }
561 out:
562         if (cdev->drv)
563                 module_put(cdev->drv->owner);
564         atomic_set(&cdev->private->onoff, 0);
565         return (ret < 0) ? ret : count;
566 }
567
568 static ssize_t
569 available_show (struct device *dev, struct device_attribute *attr, char *buf)
570 {
571         struct ccw_device *cdev = to_ccwdev(dev);
572         struct subchannel *sch;
573
574         if (ccw_device_is_orphan(cdev))
575                 return sprintf(buf, "no device\n");
576         switch (cdev->private->state) {
577         case DEV_STATE_BOXED:
578                 return sprintf(buf, "boxed\n");
579         case DEV_STATE_DISCONNECTED:
580         case DEV_STATE_DISCONNECTED_SENSE_ID:
581         case DEV_STATE_NOT_OPER:
582                 sch = to_subchannel(dev->parent);
583                 if (!sch->lpm)
584                         return sprintf(buf, "no path\n");
585                 else
586                         return sprintf(buf, "no device\n");
587         default:
588                 /* All other states considered fine. */
589                 return sprintf(buf, "good\n");
590         }
591 }
592
593 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
594 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
595 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
596 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
597 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
598 static DEVICE_ATTR(online, 0644, online_show, online_store);
599 static DEVICE_ATTR(availability, 0444, available_show, NULL);
600
601 static struct attribute *io_subchannel_attrs[] = {
602         &dev_attr_chpids.attr,
603         &dev_attr_pimpampom.attr,
604         NULL,
605 };
606
607 static struct attribute_group io_subchannel_attr_group = {
608         .attrs = io_subchannel_attrs,
609 };
610
611 static struct attribute * ccwdev_attrs[] = {
612         &dev_attr_devtype.attr,
613         &dev_attr_cutype.attr,
614         &dev_attr_modalias.attr,
615         &dev_attr_online.attr,
616         &dev_attr_cmb_enable.attr,
617         &dev_attr_availability.attr,
618         NULL,
619 };
620
621 static struct attribute_group ccwdev_attr_group = {
622         .attrs = ccwdev_attrs,
623 };
624
625 static const struct attribute_group *ccwdev_attr_groups[] = {
626         &ccwdev_attr_group,
627         NULL,
628 };
629
630 /* this is a simple abstraction for device_register that sets the
631  * correct bus type and adds the bus specific files */
632 static int ccw_device_register(struct ccw_device *cdev)
633 {
634         struct device *dev = &cdev->dev;
635         int ret;
636
637         dev->bus = &ccw_bus_type;
638         ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
639                            cdev->private->dev_id.devno);
640         if (ret)
641                 return ret;
642         ret = device_add(dev);
643         if (ret)
644                 return ret;
645
646         set_bit(1, &cdev->private->registered);
647         return ret;
648 }
649
650 static int match_dev_id(struct device *dev, void *data)
651 {
652         struct ccw_device *cdev = to_ccwdev(dev);
653         struct ccw_dev_id *dev_id = data;
654
655         return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
656 }
657
658 static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
659 {
660         struct device *dev;
661
662         dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
663
664         return dev ? to_ccwdev(dev) : NULL;
665 }
666
667 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
668 {
669         int ret;
670
671         if (test_bit(1, &cdev->private->registered)) {
672                 device_release_driver(&cdev->dev);
673                 ret = device_attach(&cdev->dev);
674                 WARN_ON(ret == -ENODEV);
675         }
676 }
677
678 static void
679 ccw_device_release(struct device *dev)
680 {
681         struct ccw_device *cdev;
682
683         cdev = to_ccwdev(dev);
684         /* Release reference of parent subchannel. */
685         put_device(cdev->dev.parent);
686         kfree(cdev->private);
687         kfree(cdev);
688 }
689
690 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
691 {
692         struct ccw_device *cdev;
693
694         cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
695         if (cdev) {
696                 cdev->private = kzalloc(sizeof(struct ccw_device_private),
697                                         GFP_KERNEL | GFP_DMA);
698                 if (cdev->private)
699                         return cdev;
700         }
701         kfree(cdev);
702         return ERR_PTR(-ENOMEM);
703 }
704
705 static void ccw_device_todo(struct work_struct *work);
706
707 static int io_subchannel_initialize_dev(struct subchannel *sch,
708                                         struct ccw_device *cdev)
709 {
710         cdev->private->cdev = cdev;
711         atomic_set(&cdev->private->onoff, 0);
712         cdev->dev.parent = &sch->dev;
713         cdev->dev.release = ccw_device_release;
714         INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
715         cdev->dev.groups = ccwdev_attr_groups;
716         /* Do first half of device_register. */
717         device_initialize(&cdev->dev);
718         if (!get_device(&sch->dev)) {
719                 /* Release reference from device_initialize(). */
720                 put_device(&cdev->dev);
721                 return -ENODEV;
722         }
723         return 0;
724 }
725
726 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
727 {
728         struct ccw_device *cdev;
729         int ret;
730
731         cdev = io_subchannel_allocate_dev(sch);
732         if (!IS_ERR(cdev)) {
733                 ret = io_subchannel_initialize_dev(sch, cdev);
734                 if (ret)
735                         cdev = ERR_PTR(ret);
736         }
737         return cdev;
738 }
739
740 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
741
742 static void sch_create_and_recog_new_device(struct subchannel *sch)
743 {
744         struct ccw_device *cdev;
745
746         /* Need to allocate a new ccw device. */
747         cdev = io_subchannel_create_ccwdev(sch);
748         if (IS_ERR(cdev)) {
749                 /* OK, we did everything we could... */
750                 css_sch_device_unregister(sch);
751                 return;
752         }
753         /* Start recognition for the new ccw device. */
754         io_subchannel_recog(cdev, sch);
755 }
756
757 /*
758  * Register recognized device.
759  */
760 static void io_subchannel_register(struct ccw_device *cdev)
761 {
762         struct subchannel *sch;
763         int ret;
764         unsigned long flags;
765
766         sch = to_subchannel(cdev->dev.parent);
767         /*
768          * Check if subchannel is still registered. It may have become
769          * unregistered if a machine check hit us after finishing
770          * device recognition but before the register work could be
771          * queued.
772          */
773         if (!device_is_registered(&sch->dev))
774                 goto out_err;
775         css_update_ssd_info(sch);
776         /*
777          * io_subchannel_register() will also be called after device
778          * recognition has been done for a boxed device (which will already
779          * be registered). We need to reprobe since we may now have sense id
780          * information.
781          */
782         if (device_is_registered(&cdev->dev)) {
783                 if (!cdev->drv) {
784                         ret = device_reprobe(&cdev->dev);
785                         if (ret)
786                                 /* We can't do much here. */
787                                 CIO_MSG_EVENT(0, "device_reprobe() returned"
788                                               " %d for 0.%x.%04x\n", ret,
789                                               cdev->private->dev_id.ssid,
790                                               cdev->private->dev_id.devno);
791                 }
792                 goto out;
793         }
794         /*
795          * Now we know this subchannel will stay, we can throw
796          * our delayed uevent.
797          */
798         dev_set_uevent_suppress(&sch->dev, 0);
799         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
800         /* make it known to the system */
801         ret = ccw_device_register(cdev);
802         if (ret) {
803                 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
804                               cdev->private->dev_id.ssid,
805                               cdev->private->dev_id.devno, ret);
806                 spin_lock_irqsave(sch->lock, flags);
807                 sch_set_cdev(sch, NULL);
808                 spin_unlock_irqrestore(sch->lock, flags);
809                 /* Release initial device reference. */
810                 put_device(&cdev->dev);
811                 goto out_err;
812         }
813 out:
814         cdev->private->flags.recog_done = 1;
815         wake_up(&cdev->private->wait_q);
816 out_err:
817         if (atomic_dec_and_test(&ccw_device_init_count))
818                 wake_up(&ccw_device_init_wq);
819 }
820
821 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
822 {
823         struct subchannel *sch;
824
825         /* Get subchannel reference for local processing. */
826         if (!get_device(cdev->dev.parent))
827                 return;
828         sch = to_subchannel(cdev->dev.parent);
829         css_sch_device_unregister(sch);
830         /* Release subchannel reference for local processing. */
831         put_device(&sch->dev);
832 }
833
834 /*
835  * subchannel recognition done. Called from the state machine.
836  */
837 void
838 io_subchannel_recog_done(struct ccw_device *cdev)
839 {
840         if (css_init_done == 0) {
841                 cdev->private->flags.recog_done = 1;
842                 return;
843         }
844         switch (cdev->private->state) {
845         case DEV_STATE_BOXED:
846                 /* Device did not respond in time. */
847         case DEV_STATE_NOT_OPER:
848                 cdev->private->flags.recog_done = 1;
849                 /* Remove device found not operational. */
850                 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
851                 if (atomic_dec_and_test(&ccw_device_init_count))
852                         wake_up(&ccw_device_init_wq);
853                 break;
854         case DEV_STATE_OFFLINE:
855                 /* 
856                  * We can't register the device in interrupt context so
857                  * we schedule a work item.
858                  */
859                 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
860                 break;
861         }
862 }
863
864 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
865 {
866         struct ccw_device_private *priv;
867
868         cdev->ccwlock = sch->lock;
869
870         /* Init private data. */
871         priv = cdev->private;
872         priv->dev_id.devno = sch->schib.pmcw.dev;
873         priv->dev_id.ssid = sch->schid.ssid;
874         priv->schid = sch->schid;
875         priv->state = DEV_STATE_NOT_OPER;
876         INIT_LIST_HEAD(&priv->cmb_list);
877         init_waitqueue_head(&priv->wait_q);
878         init_timer(&priv->timer);
879
880         /* Increase counter of devices currently in recognition. */
881         atomic_inc(&ccw_device_init_count);
882
883         /* Start async. device sensing. */
884         spin_lock_irq(sch->lock);
885         sch_set_cdev(sch, cdev);
886         ccw_device_recognition(cdev);
887         spin_unlock_irq(sch->lock);
888 }
889
890 static int ccw_device_move_to_sch(struct ccw_device *cdev,
891                                   struct subchannel *sch)
892 {
893         struct subchannel *old_sch;
894         int rc;
895
896         old_sch = to_subchannel(cdev->dev.parent);
897         /* Obtain child reference for new parent. */
898         if (!get_device(&sch->dev))
899                 return -ENODEV;
900         mutex_lock(&sch->reg_mutex);
901         rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
902         mutex_unlock(&sch->reg_mutex);
903         if (rc) {
904                 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
905                               cdev->private->dev_id.ssid,
906                               cdev->private->dev_id.devno, sch->schid.ssid,
907                               sch->schib.pmcw.dev, rc);
908                 /* Release child reference for new parent. */
909                 put_device(&sch->dev);
910                 return rc;
911         }
912         /* Clean up old subchannel. */
913         if (!sch_is_pseudo_sch(old_sch)) {
914                 spin_lock_irq(old_sch->lock);
915                 sch_set_cdev(old_sch, NULL);
916                 cio_disable_subchannel(old_sch);
917                 spin_unlock_irq(old_sch->lock);
918                 css_schedule_eval(old_sch->schid);
919         }
920         /* Release child reference for old parent. */
921         put_device(&old_sch->dev);
922         /* Initialize new subchannel. */
923         spin_lock_irq(sch->lock);
924         cdev->private->schid = sch->schid;
925         cdev->ccwlock = sch->lock;
926         if (!sch_is_pseudo_sch(sch))
927                 sch_set_cdev(sch, cdev);
928         spin_unlock_irq(sch->lock);
929         if (!sch_is_pseudo_sch(sch))
930                 css_update_ssd_info(sch);
931         return 0;
932 }
933
934 static int ccw_device_move_to_orph(struct ccw_device *cdev)
935 {
936         struct subchannel *sch = to_subchannel(cdev->dev.parent);
937         struct channel_subsystem *css = to_css(sch->dev.parent);
938
939         return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
940 }
941
942 static void io_subchannel_irq(struct subchannel *sch)
943 {
944         struct ccw_device *cdev;
945
946         cdev = sch_get_cdev(sch);
947
948         CIO_TRACE_EVENT(6, "IRQ");
949         CIO_TRACE_EVENT(6, dev_name(&sch->dev));
950         if (cdev)
951                 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
952 }
953
954 void io_subchannel_init_config(struct subchannel *sch)
955 {
956         memset(&sch->config, 0, sizeof(sch->config));
957         sch->config.csense = 1;
958         /* Use subchannel mp mode when there is more than 1 installed CHPID. */
959         if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
960                 sch->config.mp = 1;
961 }
962
963 static void io_subchannel_init_fields(struct subchannel *sch)
964 {
965         if (cio_is_console(sch->schid))
966                 sch->opm = 0xff;
967         else
968                 sch->opm = chp_get_sch_opm(sch);
969         sch->lpm = sch->schib.pmcw.pam & sch->opm;
970         sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
971
972         CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
973                       " - PIM = %02X, PAM = %02X, POM = %02X\n",
974                       sch->schib.pmcw.dev, sch->schid.ssid,
975                       sch->schid.sch_no, sch->schib.pmcw.pim,
976                       sch->schib.pmcw.pam, sch->schib.pmcw.pom);
977
978         io_subchannel_init_config(sch);
979 }
980
981 /*
982  * Note: We always return 0 so that we bind to the device even on error.
983  * This is needed so that our remove function is called on unregister.
984  */
985 static int io_subchannel_probe(struct subchannel *sch)
986 {
987         struct ccw_device *cdev;
988         int rc;
989
990         if (cio_is_console(sch->schid)) {
991                 rc = sysfs_create_group(&sch->dev.kobj,
992                                         &io_subchannel_attr_group);
993                 if (rc)
994                         CIO_MSG_EVENT(0, "Failed to create io subchannel "
995                                       "attributes for subchannel "
996                                       "0.%x.%04x (rc=%d)\n",
997                                       sch->schid.ssid, sch->schid.sch_no, rc);
998                 /*
999                  * The console subchannel already has an associated ccw_device.
1000                  * Throw the delayed uevent for the subchannel, register
1001                  * the ccw_device and exit.
1002                  */
1003                 dev_set_uevent_suppress(&sch->dev, 0);
1004                 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1005                 cdev = sch_get_cdev(sch);
1006                 cdev->dev.groups = ccwdev_attr_groups;
1007                 device_initialize(&cdev->dev);
1008                 ccw_device_register(cdev);
1009                 /*
1010                  * Check if the device is already online. If it is
1011                  * the reference count needs to be corrected since we
1012                  * didn't obtain a reference in ccw_device_set_online.
1013                  */
1014                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1015                     cdev->private->state != DEV_STATE_OFFLINE &&
1016                     cdev->private->state != DEV_STATE_BOXED)
1017                         get_device(&cdev->dev);
1018                 return 0;
1019         }
1020         io_subchannel_init_fields(sch);
1021         rc = cio_commit_config(sch);
1022         if (rc)
1023                 goto out_schedule;
1024         rc = sysfs_create_group(&sch->dev.kobj,
1025                                 &io_subchannel_attr_group);
1026         if (rc)
1027                 goto out_schedule;
1028         /* Allocate I/O subchannel private data. */
1029         sch->private = kzalloc(sizeof(struct io_subchannel_private),
1030                                GFP_KERNEL | GFP_DMA);
1031         if (!sch->private)
1032                 goto out_schedule;
1033         css_schedule_eval(sch->schid);
1034         return 0;
1035
1036 out_schedule:
1037         spin_lock_irq(sch->lock);
1038         css_sched_sch_todo(sch, SCH_TODO_UNREG);
1039         spin_unlock_irq(sch->lock);
1040         return 0;
1041 }
1042
1043 static int
1044 io_subchannel_remove (struct subchannel *sch)
1045 {
1046         struct ccw_device *cdev;
1047         unsigned long flags;
1048
1049         cdev = sch_get_cdev(sch);
1050         if (!cdev)
1051                 goto out_free;
1052         /* Set ccw device to not operational and drop reference. */
1053         spin_lock_irqsave(cdev->ccwlock, flags);
1054         sch_set_cdev(sch, NULL);
1055         cdev->private->state = DEV_STATE_NOT_OPER;
1056         spin_unlock_irqrestore(cdev->ccwlock, flags);
1057         ccw_device_unregister(cdev);
1058 out_free:
1059         kfree(sch->private);
1060         sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1061         return 0;
1062 }
1063
1064 static void io_subchannel_verify(struct subchannel *sch)
1065 {
1066         struct ccw_device *cdev;
1067
1068         cdev = sch_get_cdev(sch);
1069         if (cdev)
1070                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1071 }
1072
1073 static int check_for_io_on_path(struct subchannel *sch, int mask)
1074 {
1075         if (cio_update_schib(sch))
1076                 return 0;
1077         if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1078                 return 1;
1079         return 0;
1080 }
1081
1082 static void terminate_internal_io(struct subchannel *sch,
1083                                   struct ccw_device *cdev)
1084 {
1085         if (cio_clear(sch)) {
1086                 /* Recheck device in case clear failed. */
1087                 sch->lpm = 0;
1088                 if (cdev->online)
1089                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1090                 else
1091                         css_schedule_eval(sch->schid);
1092                 return;
1093         }
1094         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1095         /* Request retry of internal operation. */
1096         cdev->private->flags.intretry = 1;
1097         /* Call handler. */
1098         if (cdev->handler)
1099                 cdev->handler(cdev, cdev->private->intparm,
1100                               ERR_PTR(-EIO));
1101 }
1102
1103 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1104 {
1105         struct ccw_device *cdev;
1106
1107         cdev = sch_get_cdev(sch);
1108         if (!cdev)
1109                 return;
1110         if (check_for_io_on_path(sch, mask)) {
1111                 if (cdev->private->state == DEV_STATE_ONLINE)
1112                         ccw_device_kill_io(cdev);
1113                 else {
1114                         terminate_internal_io(sch, cdev);
1115                         /* Re-start path verification. */
1116                         dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1117                 }
1118         } else
1119                 /* trigger path verification. */
1120                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1121
1122 }
1123
1124 static int io_subchannel_chp_event(struct subchannel *sch,
1125                                    struct chp_link *link, int event)
1126 {
1127         int mask;
1128
1129         mask = chp_ssd_get_mask(&sch->ssd_info, link);
1130         if (!mask)
1131                 return 0;
1132         switch (event) {
1133         case CHP_VARY_OFF:
1134                 sch->opm &= ~mask;
1135                 sch->lpm &= ~mask;
1136                 io_subchannel_terminate_path(sch, mask);
1137                 break;
1138         case CHP_VARY_ON:
1139                 sch->opm |= mask;
1140                 sch->lpm |= mask;
1141                 io_subchannel_verify(sch);
1142                 break;
1143         case CHP_OFFLINE:
1144                 if (cio_update_schib(sch))
1145                         return -ENODEV;
1146                 io_subchannel_terminate_path(sch, mask);
1147                 break;
1148         case CHP_ONLINE:
1149                 if (cio_update_schib(sch))
1150                         return -ENODEV;
1151                 sch->lpm |= mask & sch->opm;
1152                 io_subchannel_verify(sch);
1153                 break;
1154         }
1155         return 0;
1156 }
1157
1158 static void
1159 io_subchannel_shutdown(struct subchannel *sch)
1160 {
1161         struct ccw_device *cdev;
1162         int ret;
1163
1164         cdev = sch_get_cdev(sch);
1165
1166         if (cio_is_console(sch->schid))
1167                 return;
1168         if (!sch->schib.pmcw.ena)
1169                 /* Nothing to do. */
1170                 return;
1171         ret = cio_disable_subchannel(sch);
1172         if (ret != -EBUSY)
1173                 /* Subchannel is disabled, we're done. */
1174                 return;
1175         cdev->private->state = DEV_STATE_QUIESCE;
1176         if (cdev->handler)
1177                 cdev->handler(cdev, cdev->private->intparm,
1178                               ERR_PTR(-EIO));
1179         ret = ccw_device_cancel_halt_clear(cdev);
1180         if (ret == -EBUSY) {
1181                 ccw_device_set_timeout(cdev, HZ/10);
1182                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1183         }
1184         cio_disable_subchannel(sch);
1185 }
1186
1187 static int device_is_disconnected(struct ccw_device *cdev)
1188 {
1189         if (!cdev)
1190                 return 0;
1191         return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1192                 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1193 }
1194
1195 static int recovery_check(struct device *dev, void *data)
1196 {
1197         struct ccw_device *cdev = to_ccwdev(dev);
1198         int *redo = data;
1199
1200         spin_lock_irq(cdev->ccwlock);
1201         switch (cdev->private->state) {
1202         case DEV_STATE_DISCONNECTED:
1203                 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1204                               cdev->private->dev_id.ssid,
1205                               cdev->private->dev_id.devno);
1206                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1207                 *redo = 1;
1208                 break;
1209         case DEV_STATE_DISCONNECTED_SENSE_ID:
1210                 *redo = 1;
1211                 break;
1212         }
1213         spin_unlock_irq(cdev->ccwlock);
1214
1215         return 0;
1216 }
1217
1218 static void recovery_work_func(struct work_struct *unused)
1219 {
1220         int redo = 0;
1221
1222         bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1223         if (redo) {
1224                 spin_lock_irq(&recovery_lock);
1225                 if (!timer_pending(&recovery_timer)) {
1226                         if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1227                                 recovery_phase++;
1228                         mod_timer(&recovery_timer, jiffies +
1229                                   recovery_delay[recovery_phase] * HZ);
1230                 }
1231                 spin_unlock_irq(&recovery_lock);
1232         } else
1233                 CIO_MSG_EVENT(4, "recovery: end\n");
1234 }
1235
1236 static DECLARE_WORK(recovery_work, recovery_work_func);
1237
1238 static void recovery_func(unsigned long data)
1239 {
1240         /*
1241          * We can't do our recovery in softirq context and it's not
1242          * performance critical, so we schedule it.
1243          */
1244         schedule_work(&recovery_work);
1245 }
1246
1247 static void ccw_device_schedule_recovery(void)
1248 {
1249         unsigned long flags;
1250
1251         CIO_MSG_EVENT(4, "recovery: schedule\n");
1252         spin_lock_irqsave(&recovery_lock, flags);
1253         if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1254                 recovery_phase = 0;
1255                 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1256         }
1257         spin_unlock_irqrestore(&recovery_lock, flags);
1258 }
1259
1260 static int purge_fn(struct device *dev, void *data)
1261 {
1262         struct ccw_device *cdev = to_ccwdev(dev);
1263         struct ccw_dev_id *id = &cdev->private->dev_id;
1264
1265         spin_lock_irq(cdev->ccwlock);
1266         if (is_blacklisted(id->ssid, id->devno) &&
1267             (cdev->private->state == DEV_STATE_OFFLINE)) {
1268                 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1269                               id->devno);
1270                 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1271         }
1272         spin_unlock_irq(cdev->ccwlock);
1273         /* Abort loop in case of pending signal. */
1274         if (signal_pending(current))
1275                 return -EINTR;
1276
1277         return 0;
1278 }
1279
1280 /**
1281  * ccw_purge_blacklisted - purge unused, blacklisted devices
1282  *
1283  * Unregister all ccw devices that are offline and on the blacklist.
1284  */
1285 int ccw_purge_blacklisted(void)
1286 {
1287         CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1288         bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1289         return 0;
1290 }
1291
1292 void ccw_device_set_disconnected(struct ccw_device *cdev)
1293 {
1294         if (!cdev)
1295                 return;
1296         ccw_device_set_timeout(cdev, 0);
1297         cdev->private->flags.fake_irb = 0;
1298         cdev->private->state = DEV_STATE_DISCONNECTED;
1299         if (cdev->online)
1300                 ccw_device_schedule_recovery();
1301 }
1302
1303 void ccw_device_set_notoper(struct ccw_device *cdev)
1304 {
1305         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1306
1307         CIO_TRACE_EVENT(2, "notoper");
1308         CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1309         ccw_device_set_timeout(cdev, 0);
1310         cio_disable_subchannel(sch);
1311         cdev->private->state = DEV_STATE_NOT_OPER;
1312 }
1313
1314 enum io_sch_action {
1315         IO_SCH_UNREG,
1316         IO_SCH_ORPH_UNREG,
1317         IO_SCH_ATTACH,
1318         IO_SCH_UNREG_ATTACH,
1319         IO_SCH_ORPH_ATTACH,
1320         IO_SCH_REPROBE,
1321         IO_SCH_VERIFY,
1322         IO_SCH_DISC,
1323         IO_SCH_NOP,
1324 };
1325
1326 static enum io_sch_action sch_get_action(struct subchannel *sch)
1327 {
1328         struct ccw_device *cdev;
1329
1330         cdev = sch_get_cdev(sch);
1331         if (cio_update_schib(sch)) {
1332                 /* Not operational. */
1333                 if (!cdev)
1334                         return IO_SCH_UNREG;
1335                 if (!ccw_device_notify(cdev, CIO_GONE))
1336                         return IO_SCH_UNREG;
1337                 return IO_SCH_ORPH_UNREG;
1338         }
1339         /* Operational. */
1340         if (!cdev)
1341                 return IO_SCH_ATTACH;
1342         if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1343                 if (!ccw_device_notify(cdev, CIO_GONE))
1344                         return IO_SCH_UNREG_ATTACH;
1345                 return IO_SCH_ORPH_ATTACH;
1346         }
1347         if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1348                 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1349                         return IO_SCH_UNREG;
1350                 return IO_SCH_DISC;
1351         }
1352         if (device_is_disconnected(cdev))
1353                 return IO_SCH_REPROBE;
1354         if (cdev->online)
1355                 return IO_SCH_VERIFY;
1356         return IO_SCH_NOP;
1357 }
1358
1359 /**
1360  * io_subchannel_sch_event - process subchannel event
1361  * @sch: subchannel
1362  * @process: non-zero if function is called in process context
1363  *
1364  * An unspecified event occurred for this subchannel. Adjust data according
1365  * to the current operational state of the subchannel and device. Return
1366  * zero when the event has been handled sufficiently or -EAGAIN when this
1367  * function should be called again in process context.
1368  */
1369 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1370 {
1371         unsigned long flags;
1372         struct ccw_device *cdev;
1373         struct ccw_dev_id dev_id;
1374         enum io_sch_action action;
1375         int rc = -EAGAIN;
1376
1377         spin_lock_irqsave(sch->lock, flags);
1378         if (!device_is_registered(&sch->dev))
1379                 goto out_unlock;
1380         if (work_pending(&sch->todo_work))
1381                 goto out_unlock;
1382         cdev = sch_get_cdev(sch);
1383         if (cdev && work_pending(&cdev->private->todo_work))
1384                 goto out_unlock;
1385         action = sch_get_action(sch);
1386         CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1387                       sch->schid.ssid, sch->schid.sch_no, process,
1388                       action);
1389         /* Perform immediate actions while holding the lock. */
1390         switch (action) {
1391         case IO_SCH_REPROBE:
1392                 /* Trigger device recognition. */
1393                 ccw_device_trigger_reprobe(cdev);
1394                 rc = 0;
1395                 goto out_unlock;
1396         case IO_SCH_VERIFY:
1397                 /* Trigger path verification. */
1398                 io_subchannel_verify(sch);
1399                 rc = 0;
1400                 goto out_unlock;
1401         case IO_SCH_DISC:
1402                 ccw_device_set_disconnected(cdev);
1403                 rc = 0;
1404                 goto out_unlock;
1405         case IO_SCH_ORPH_UNREG:
1406         case IO_SCH_ORPH_ATTACH:
1407                 ccw_device_set_disconnected(cdev);
1408                 break;
1409         case IO_SCH_UNREG_ATTACH:
1410         case IO_SCH_UNREG:
1411                 if (cdev)
1412                         ccw_device_set_notoper(cdev);
1413                 break;
1414         case IO_SCH_NOP:
1415                 rc = 0;
1416                 goto out_unlock;
1417         default:
1418                 break;
1419         }
1420         spin_unlock_irqrestore(sch->lock, flags);
1421         /* All other actions require process context. */
1422         if (!process)
1423                 goto out;
1424         /* Handle attached ccw device. */
1425         switch (action) {
1426         case IO_SCH_ORPH_UNREG:
1427         case IO_SCH_ORPH_ATTACH:
1428                 /* Move ccw device to orphanage. */
1429                 rc = ccw_device_move_to_orph(cdev);
1430                 if (rc)
1431                         goto out;
1432                 break;
1433         case IO_SCH_UNREG_ATTACH:
1434                 /* Unregister ccw device. */
1435                 ccw_device_unregister(cdev);
1436                 break;
1437         default:
1438                 break;
1439         }
1440         /* Handle subchannel. */
1441         switch (action) {
1442         case IO_SCH_ORPH_UNREG:
1443         case IO_SCH_UNREG:
1444                 css_sch_device_unregister(sch);
1445                 break;
1446         case IO_SCH_ORPH_ATTACH:
1447         case IO_SCH_UNREG_ATTACH:
1448         case IO_SCH_ATTACH:
1449                 dev_id.ssid = sch->schid.ssid;
1450                 dev_id.devno = sch->schib.pmcw.dev;
1451                 cdev = get_ccwdev_by_dev_id(&dev_id);
1452                 if (!cdev) {
1453                         sch_create_and_recog_new_device(sch);
1454                         break;
1455                 }
1456                 rc = ccw_device_move_to_sch(cdev, sch);
1457                 if (rc) {
1458                         /* Release reference from get_ccwdev_by_dev_id() */
1459                         put_device(&cdev->dev);
1460                         goto out;
1461                 }
1462                 spin_lock_irqsave(sch->lock, flags);
1463                 ccw_device_trigger_reprobe(cdev);
1464                 spin_unlock_irqrestore(sch->lock, flags);
1465                 /* Release reference from get_ccwdev_by_dev_id() */
1466                 put_device(&cdev->dev);
1467                 break;
1468         default:
1469                 break;
1470         }
1471         return 0;
1472
1473 out_unlock:
1474         spin_unlock_irqrestore(sch->lock, flags);
1475 out:
1476         return rc;
1477 }
1478
1479 #ifdef CONFIG_CCW_CONSOLE
1480 static struct ccw_device console_cdev;
1481 static struct ccw_device_private console_private;
1482 static int console_cdev_in_use;
1483
1484 static DEFINE_SPINLOCK(ccw_console_lock);
1485
1486 spinlock_t * cio_get_console_lock(void)
1487 {
1488         return &ccw_console_lock;
1489 }
1490
1491 static int ccw_device_console_enable(struct ccw_device *cdev,
1492                                      struct subchannel *sch)
1493 {
1494         int rc;
1495
1496         /* Attach subchannel private data. */
1497         sch->private = cio_get_console_priv();
1498         memset(sch->private, 0, sizeof(struct io_subchannel_private));
1499         io_subchannel_init_fields(sch);
1500         rc = cio_commit_config(sch);
1501         if (rc)
1502                 return rc;
1503         sch->driver = &io_subchannel_driver;
1504         /* Initialize the ccw_device structure. */
1505         cdev->dev.parent= &sch->dev;
1506         io_subchannel_recog(cdev, sch);
1507         /* Now wait for the async. recognition to come to an end. */
1508         spin_lock_irq(cdev->ccwlock);
1509         while (!dev_fsm_final_state(cdev))
1510                 wait_cons_dev();
1511         rc = -EIO;
1512         if (cdev->private->state != DEV_STATE_OFFLINE)
1513                 goto out_unlock;
1514         ccw_device_online(cdev);
1515         while (!dev_fsm_final_state(cdev))
1516                 wait_cons_dev();
1517         if (cdev->private->state != DEV_STATE_ONLINE)
1518                 goto out_unlock;
1519         rc = 0;
1520 out_unlock:
1521         spin_unlock_irq(cdev->ccwlock);
1522         return rc;
1523 }
1524
1525 struct ccw_device *
1526 ccw_device_probe_console(void)
1527 {
1528         struct subchannel *sch;
1529         int ret;
1530
1531         if (xchg(&console_cdev_in_use, 1) != 0)
1532                 return ERR_PTR(-EBUSY);
1533         sch = cio_probe_console();
1534         if (IS_ERR(sch)) {
1535                 console_cdev_in_use = 0;
1536                 return (void *) sch;
1537         }
1538         memset(&console_cdev, 0, sizeof(struct ccw_device));
1539         memset(&console_private, 0, sizeof(struct ccw_device_private));
1540         console_cdev.private = &console_private;
1541         console_private.cdev = &console_cdev;
1542         ret = ccw_device_console_enable(&console_cdev, sch);
1543         if (ret) {
1544                 cio_release_console();
1545                 console_cdev_in_use = 0;
1546                 return ERR_PTR(ret);
1547         }
1548         console_cdev.online = 1;
1549         return &console_cdev;
1550 }
1551
1552 static int ccw_device_pm_restore(struct device *dev);
1553
1554 int ccw_device_force_console(void)
1555 {
1556         if (!console_cdev_in_use)
1557                 return -ENODEV;
1558         return ccw_device_pm_restore(&console_cdev.dev);
1559 }
1560 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1561 #endif
1562
1563 /*
1564  * get ccw_device matching the busid, but only if owned by cdrv
1565  */
1566 static int
1567 __ccwdev_check_busid(struct device *dev, void *id)
1568 {
1569         char *bus_id;
1570
1571         bus_id = id;
1572
1573         return (strcmp(bus_id, dev_name(dev)) == 0);
1574 }
1575
1576
1577 /**
1578  * get_ccwdev_by_busid() - obtain device from a bus id
1579  * @cdrv: driver the device is owned by
1580  * @bus_id: bus id of the device to be searched
1581  *
1582  * This function searches all devices owned by @cdrv for a device with a bus
1583  * id matching @bus_id.
1584  * Returns:
1585  *  If a match is found, its reference count of the found device is increased
1586  *  and it is returned; else %NULL is returned.
1587  */
1588 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1589                                        const char *bus_id)
1590 {
1591         struct device *dev;
1592         struct device_driver *drv;
1593
1594         drv = get_driver(&cdrv->driver);
1595         if (!drv)
1596                 return NULL;
1597
1598         dev = driver_find_device(drv, NULL, (void *)bus_id,
1599                                  __ccwdev_check_busid);
1600         put_driver(drv);
1601
1602         return dev ? to_ccwdev(dev) : NULL;
1603 }
1604
1605 /************************** device driver handling ************************/
1606
1607 /* This is the implementation of the ccw_driver class. The probe, remove
1608  * and release methods are initially very similar to the device_driver
1609  * implementations, with the difference that they have ccw_device
1610  * arguments.
1611  *
1612  * A ccw driver also contains the information that is needed for
1613  * device matching.
1614  */
1615 static int
1616 ccw_device_probe (struct device *dev)
1617 {
1618         struct ccw_device *cdev = to_ccwdev(dev);
1619         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1620         int ret;
1621
1622         cdev->drv = cdrv; /* to let the driver call _set_online */
1623
1624         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1625
1626         if (ret) {
1627                 cdev->drv = NULL;
1628                 return ret;
1629         }
1630
1631         return 0;
1632 }
1633
1634 static int
1635 ccw_device_remove (struct device *dev)
1636 {
1637         struct ccw_device *cdev = to_ccwdev(dev);
1638         struct ccw_driver *cdrv = cdev->drv;
1639         int ret;
1640
1641         if (cdrv->remove)
1642                 cdrv->remove(cdev);
1643         if (cdev->online) {
1644                 cdev->online = 0;
1645                 spin_lock_irq(cdev->ccwlock);
1646                 ret = ccw_device_offline(cdev);
1647                 spin_unlock_irq(cdev->ccwlock);
1648                 if (ret == 0)
1649                         wait_event(cdev->private->wait_q,
1650                                    dev_fsm_final_state(cdev));
1651                 else
1652                         CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1653                                       "device 0.%x.%04x\n",
1654                                       ret, cdev->private->dev_id.ssid,
1655                                       cdev->private->dev_id.devno);
1656                 /* Give up reference obtained in ccw_device_set_online(). */
1657                 put_device(&cdev->dev);
1658         }
1659         ccw_device_set_timeout(cdev, 0);
1660         cdev->drv = NULL;
1661         return 0;
1662 }
1663
1664 static void ccw_device_shutdown(struct device *dev)
1665 {
1666         struct ccw_device *cdev;
1667
1668         cdev = to_ccwdev(dev);
1669         if (cdev->drv && cdev->drv->shutdown)
1670                 cdev->drv->shutdown(cdev);
1671         disable_cmf(cdev);
1672 }
1673
1674 static int ccw_device_pm_prepare(struct device *dev)
1675 {
1676         struct ccw_device *cdev = to_ccwdev(dev);
1677
1678         if (work_pending(&cdev->private->todo_work))
1679                 return -EAGAIN;
1680         /* Fail while device is being set online/offline. */
1681         if (atomic_read(&cdev->private->onoff))
1682                 return -EAGAIN;
1683
1684         if (cdev->online && cdev->drv && cdev->drv->prepare)
1685                 return cdev->drv->prepare(cdev);
1686
1687         return 0;
1688 }
1689
1690 static void ccw_device_pm_complete(struct device *dev)
1691 {
1692         struct ccw_device *cdev = to_ccwdev(dev);
1693
1694         if (cdev->online && cdev->drv && cdev->drv->complete)
1695                 cdev->drv->complete(cdev);
1696 }
1697
1698 static int ccw_device_pm_freeze(struct device *dev)
1699 {
1700         struct ccw_device *cdev = to_ccwdev(dev);
1701         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1702         int ret, cm_enabled;
1703
1704         /* Fail suspend while device is in transistional state. */
1705         if (!dev_fsm_final_state(cdev))
1706                 return -EAGAIN;
1707         if (!cdev->online)
1708                 return 0;
1709         if (cdev->drv && cdev->drv->freeze) {
1710                 ret = cdev->drv->freeze(cdev);
1711                 if (ret)
1712                         return ret;
1713         }
1714
1715         spin_lock_irq(sch->lock);
1716         cm_enabled = cdev->private->cmb != NULL;
1717         spin_unlock_irq(sch->lock);
1718         if (cm_enabled) {
1719                 /* Don't have the css write on memory. */
1720                 ret = ccw_set_cmf(cdev, 0);
1721                 if (ret)
1722                         return ret;
1723         }
1724         /* From here on, disallow device driver I/O. */
1725         spin_lock_irq(sch->lock);
1726         ret = cio_disable_subchannel(sch);
1727         spin_unlock_irq(sch->lock);
1728
1729         return ret;
1730 }
1731
1732 static int ccw_device_pm_thaw(struct device *dev)
1733 {
1734         struct ccw_device *cdev = to_ccwdev(dev);
1735         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1736         int ret, cm_enabled;
1737
1738         if (!cdev->online)
1739                 return 0;
1740
1741         spin_lock_irq(sch->lock);
1742         /* Allow device driver I/O again. */
1743         ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1744         cm_enabled = cdev->private->cmb != NULL;
1745         spin_unlock_irq(sch->lock);
1746         if (ret)
1747                 return ret;
1748
1749         if (cm_enabled) {
1750                 ret = ccw_set_cmf(cdev, 1);
1751                 if (ret)
1752                         return ret;
1753         }
1754
1755         if (cdev->drv && cdev->drv->thaw)
1756                 ret = cdev->drv->thaw(cdev);
1757
1758         return ret;
1759 }
1760
1761 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1762 {
1763         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1764
1765         if (cio_is_console(sch->schid))
1766                 goto out;
1767         /*
1768          * While we were sleeping, devices may have gone or become
1769          * available again. Kick re-detection.
1770          */
1771         spin_lock_irq(sch->lock);
1772         cdev->private->flags.resuming = 1;
1773         ccw_device_recognition(cdev);
1774         spin_unlock_irq(sch->lock);
1775         wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1776                    cdev->private->state == DEV_STATE_DISCONNECTED);
1777 out:
1778         cdev->private->flags.resuming = 0;
1779 }
1780
1781 static int resume_handle_boxed(struct ccw_device *cdev)
1782 {
1783         cdev->private->state = DEV_STATE_BOXED;
1784         if (ccw_device_notify(cdev, CIO_BOXED))
1785                 return 0;
1786         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1787         return -ENODEV;
1788 }
1789
1790 static int resume_handle_disc(struct ccw_device *cdev)
1791 {
1792         cdev->private->state = DEV_STATE_DISCONNECTED;
1793         if (ccw_device_notify(cdev, CIO_GONE))
1794                 return 0;
1795         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1796         return -ENODEV;
1797 }
1798
1799 static int ccw_device_pm_restore(struct device *dev)
1800 {
1801         struct ccw_device *cdev = to_ccwdev(dev);
1802         struct subchannel *sch = to_subchannel(cdev->dev.parent);
1803         int ret = 0, cm_enabled;
1804
1805         __ccw_device_pm_restore(cdev);
1806         spin_lock_irq(sch->lock);
1807         if (cio_is_console(sch->schid)) {
1808                 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1809                 spin_unlock_irq(sch->lock);
1810                 goto out_restore;
1811         }
1812         cdev->private->flags.donotify = 0;
1813         /* check recognition results */
1814         switch (cdev->private->state) {
1815         case DEV_STATE_OFFLINE:
1816                 break;
1817         case DEV_STATE_BOXED:
1818                 ret = resume_handle_boxed(cdev);
1819                 spin_unlock_irq(sch->lock);
1820                 if (ret)
1821                         goto out;
1822                 goto out_restore;
1823         case DEV_STATE_DISCONNECTED:
1824                 goto out_disc_unlock;
1825         default:
1826                 goto out_unreg_unlock;
1827         }
1828         /* check if the device id has changed */
1829         if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1830                 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1831                               "changed from %04x to %04x)\n",
1832                               sch->schid.ssid, sch->schid.sch_no,
1833                               cdev->private->dev_id.devno,
1834                               sch->schib.pmcw.dev);
1835                 goto out_unreg_unlock;
1836         }
1837         /* check if the device type has changed */
1838         if (!ccw_device_test_sense_data(cdev)) {
1839                 ccw_device_update_sense_data(cdev);
1840                 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1841                 ret = -ENODEV;
1842                 goto out_unlock;
1843         }
1844         if (!cdev->online) {
1845                 ret = 0;
1846                 goto out_unlock;
1847         }
1848         ret = ccw_device_online(cdev);
1849         if (ret)
1850                 goto out_disc_unlock;
1851
1852         cm_enabled = cdev->private->cmb != NULL;
1853         spin_unlock_irq(sch->lock);
1854
1855         wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1856         if (cdev->private->state != DEV_STATE_ONLINE) {
1857                 spin_lock_irq(sch->lock);
1858                 goto out_disc_unlock;
1859         }
1860         if (cm_enabled) {
1861                 ret = ccw_set_cmf(cdev, 1);
1862                 if (ret) {
1863                         CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1864                                       "(rc=%d)\n", cdev->private->dev_id.ssid,
1865                                       cdev->private->dev_id.devno, ret);
1866                         ret = 0;
1867                 }
1868         }
1869
1870 out_restore:
1871         if (cdev->online && cdev->drv && cdev->drv->restore)
1872                 ret = cdev->drv->restore(cdev);
1873 out:
1874         return ret;
1875
1876 out_disc_unlock:
1877         ret = resume_handle_disc(cdev);
1878         spin_unlock_irq(sch->lock);
1879         if (ret)
1880                 return ret;
1881         goto out_restore;
1882
1883 out_unreg_unlock:
1884         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
1885         ret = -ENODEV;
1886 out_unlock:
1887         spin_unlock_irq(sch->lock);
1888         return ret;
1889 }
1890
1891 static struct dev_pm_ops ccw_pm_ops = {
1892         .prepare = ccw_device_pm_prepare,
1893         .complete = ccw_device_pm_complete,
1894         .freeze = ccw_device_pm_freeze,
1895         .thaw = ccw_device_pm_thaw,
1896         .restore = ccw_device_pm_restore,
1897 };
1898
1899 struct bus_type ccw_bus_type = {
1900         .name   = "ccw",
1901         .match  = ccw_bus_match,
1902         .uevent = ccw_uevent,
1903         .probe  = ccw_device_probe,
1904         .remove = ccw_device_remove,
1905         .shutdown = ccw_device_shutdown,
1906         .pm = &ccw_pm_ops,
1907 };
1908
1909 /**
1910  * ccw_driver_register() - register a ccw driver
1911  * @cdriver: driver to be registered
1912  *
1913  * This function is mainly a wrapper around driver_register().
1914  * Returns:
1915  *   %0 on success and a negative error value on failure.
1916  */
1917 int ccw_driver_register(struct ccw_driver *cdriver)
1918 {
1919         struct device_driver *drv = &cdriver->driver;
1920
1921         drv->bus = &ccw_bus_type;
1922         drv->name = cdriver->name;
1923         drv->owner = cdriver->owner;
1924
1925         return driver_register(drv);
1926 }
1927
1928 /**
1929  * ccw_driver_unregister() - deregister a ccw driver
1930  * @cdriver: driver to be deregistered
1931  *
1932  * This function is mainly a wrapper around driver_unregister().
1933  */
1934 void ccw_driver_unregister(struct ccw_driver *cdriver)
1935 {
1936         driver_unregister(&cdriver->driver);
1937 }
1938
1939 /* Helper func for qdio. */
1940 struct subchannel_id
1941 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1942 {
1943         struct subchannel *sch;
1944
1945         sch = to_subchannel(cdev->dev.parent);
1946         return sch->schid;
1947 }
1948
1949 static void ccw_device_todo(struct work_struct *work)
1950 {
1951         struct ccw_device_private *priv;
1952         struct ccw_device *cdev;
1953         struct subchannel *sch;
1954         enum cdev_todo todo;
1955
1956         priv = container_of(work, struct ccw_device_private, todo_work);
1957         cdev = priv->cdev;
1958         sch = to_subchannel(cdev->dev.parent);
1959         /* Find out todo. */
1960         spin_lock_irq(cdev->ccwlock);
1961         todo = priv->todo;
1962         priv->todo = CDEV_TODO_NOTHING;
1963         CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1964                       priv->dev_id.ssid, priv->dev_id.devno, todo);
1965         spin_unlock_irq(cdev->ccwlock);
1966         /* Perform todo. */
1967         switch (todo) {
1968         case CDEV_TODO_ENABLE_CMF:
1969                 cmf_reenable(cdev);
1970                 break;
1971         case CDEV_TODO_REBIND:
1972                 ccw_device_do_unbind_bind(cdev);
1973                 break;
1974         case CDEV_TODO_REGISTER:
1975                 io_subchannel_register(cdev);
1976                 break;
1977         case CDEV_TODO_UNREG_EVAL:
1978                 if (!sch_is_pseudo_sch(sch))
1979                         css_schedule_eval(sch->schid);
1980                 /* fall-through */
1981         case CDEV_TODO_UNREG:
1982                 if (sch_is_pseudo_sch(sch))
1983                         ccw_device_unregister(cdev);
1984                 else
1985                         ccw_device_call_sch_unregister(cdev);
1986                 break;
1987         default:
1988                 break;
1989         }
1990         /* Release workqueue ref. */
1991         put_device(&cdev->dev);
1992 }
1993
1994 /**
1995  * ccw_device_sched_todo - schedule ccw device operation
1996  * @cdev: ccw device
1997  * @todo: todo
1998  *
1999  * Schedule the operation identified by @todo to be performed on the slow path
2000  * workqueue. Do nothing if another operation with higher priority is already
2001  * scheduled. Needs to be called with ccwdev lock held.
2002  */
2003 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2004 {
2005         CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2006                       cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2007                       todo);
2008         if (cdev->private->todo >= todo)
2009                 return;
2010         cdev->private->todo = todo;
2011         /* Get workqueue ref. */
2012         if (!get_device(&cdev->dev))
2013                 return;
2014         if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
2015                 /* Already queued, release workqueue ref. */
2016                 put_device(&cdev->dev);
2017         }
2018 }
2019
2020 MODULE_LICENSE("GPL");
2021 EXPORT_SYMBOL(ccw_device_set_online);
2022 EXPORT_SYMBOL(ccw_device_set_offline);
2023 EXPORT_SYMBOL(ccw_driver_register);
2024 EXPORT_SYMBOL(ccw_driver_unregister);
2025 EXPORT_SYMBOL(get_ccwdev_by_busid);
2026 EXPORT_SYMBOL(ccw_bus_type);
2027 EXPORT_SYMBOL(ccw_device_work);
2028 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);