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