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