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