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