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