[S390] qdio_get_micros return value.
[linux-2.6] / drivers / s390 / cio / device.c
1 /*
2  *  drivers/s390/cio/device.c
3  *  bus driver for ccw devices
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *                       IBM Corporation
7  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *               Cornelia Huck (cornelia.huck@de.ibm.com)
9  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
10  */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20
21 #include <asm/ccwdev.h>
22 #include <asm/cio.h>
23 #include <asm/param.h>          /* HZ */
24
25 #include "cio.h"
26 #include "css.h"
27 #include "device.h"
28 #include "ioasm.h"
29
30 /******************* bus type handling ***********************/
31
32 /* The Linux driver model distinguishes between a bus type and
33  * the bus itself. Of course we only have one channel
34  * subsystem driver and one channel system per machine, but
35  * we still use the abstraction. T.R. says it's a good idea. */
36 static int
37 ccw_bus_match (struct device * dev, struct device_driver * drv)
38 {
39         struct ccw_device *cdev = to_ccwdev(dev);
40         struct ccw_driver *cdrv = to_ccwdrv(drv);
41         const struct ccw_device_id *ids = cdrv->ids, *found;
42
43         if (!ids)
44                 return 0;
45
46         found = ccw_device_id_match(ids, &cdev->id);
47         if (!found)
48                 return 0;
49
50         cdev->id.driver_info = found->driver_info;
51
52         return 1;
53 }
54
55 /*
56  * Hotplugging interface for ccw devices.
57  * Heavily modeled on pci and usb hotplug.
58  */
59 static int
60 ccw_uevent (struct device *dev, char **envp, int num_envp,
61              char *buffer, int buffer_size)
62 {
63         struct ccw_device *cdev = to_ccwdev(dev);
64         int i = 0;
65         int length = 0;
66
67         if (!cdev)
68                 return -ENODEV;
69
70         /* what we want to pass to /sbin/hotplug */
71
72         envp[i++] = buffer;
73         length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X",
74                            cdev->id.cu_type);
75         if ((buffer_size - length <= 0) || (i >= num_envp))
76                 return -ENOMEM;
77         ++length;
78         buffer += length;
79
80         envp[i++] = buffer;
81         length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X",
82                            cdev->id.cu_model);
83         if ((buffer_size - length <= 0) || (i >= num_envp))
84                 return -ENOMEM;
85         ++length;
86         buffer += length;
87
88         /* The next two can be zero, that's ok for us */
89         envp[i++] = buffer;
90         length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X",
91                            cdev->id.dev_type);
92         if ((buffer_size - length <= 0) || (i >= num_envp))
93                 return -ENOMEM;
94         ++length;
95         buffer += length;
96
97         envp[i++] = buffer;
98         length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X",
99                            cdev->id.dev_model);
100         if ((buffer_size - length <= 0) || (i >= num_envp))
101                 return -ENOMEM;
102
103         envp[i] = NULL;
104
105         return 0;
106 }
107
108 struct bus_type ccw_bus_type;
109
110 static int io_subchannel_probe (struct subchannel *);
111 static int io_subchannel_remove (struct subchannel *);
112 void io_subchannel_irq (struct device *);
113 static int io_subchannel_notify(struct device *, int);
114 static void io_subchannel_verify(struct device *);
115 static void io_subchannel_ioterm(struct device *);
116 static void io_subchannel_shutdown(struct subchannel *);
117
118 struct css_driver io_subchannel_driver = {
119         .subchannel_type = SUBCHANNEL_TYPE_IO,
120         .drv = {
121                 .name = "io_subchannel",
122                 .bus  = &css_bus_type,
123         },
124         .irq = io_subchannel_irq,
125         .notify = io_subchannel_notify,
126         .verify = io_subchannel_verify,
127         .termination = io_subchannel_ioterm,
128         .probe = io_subchannel_probe,
129         .remove = io_subchannel_remove,
130         .shutdown = io_subchannel_shutdown,
131 };
132
133 struct workqueue_struct *ccw_device_work;
134 struct workqueue_struct *ccw_device_notify_work;
135 wait_queue_head_t ccw_device_init_wq;
136 atomic_t ccw_device_init_count;
137
138 static int __init
139 init_ccw_bus_type (void)
140 {
141         int ret;
142
143         init_waitqueue_head(&ccw_device_init_wq);
144         atomic_set(&ccw_device_init_count, 0);
145
146         ccw_device_work = create_singlethread_workqueue("cio");
147         if (!ccw_device_work)
148                 return -ENOMEM; /* FIXME: better errno ? */
149         ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
150         if (!ccw_device_notify_work) {
151                 ret = -ENOMEM; /* FIXME: better errno ? */
152                 goto out_err;
153         }
154         slow_path_wq = create_singlethread_workqueue("kslowcrw");
155         if (!slow_path_wq) {
156                 ret = -ENOMEM; /* FIXME: better errno ? */
157                 goto out_err;
158         }
159         if ((ret = bus_register (&ccw_bus_type)))
160                 goto out_err;
161
162         if ((ret = driver_register(&io_subchannel_driver.drv)))
163                 goto out_err;
164
165         wait_event(ccw_device_init_wq,
166                    atomic_read(&ccw_device_init_count) == 0);
167         flush_workqueue(ccw_device_work);
168         return 0;
169 out_err:
170         if (ccw_device_work)
171                 destroy_workqueue(ccw_device_work);
172         if (ccw_device_notify_work)
173                 destroy_workqueue(ccw_device_notify_work);
174         if (slow_path_wq)
175                 destroy_workqueue(slow_path_wq);
176         return ret;
177 }
178
179 static void __exit
180 cleanup_ccw_bus_type (void)
181 {
182         driver_unregister(&io_subchannel_driver.drv);
183         bus_unregister(&ccw_bus_type);
184         destroy_workqueue(ccw_device_notify_work);
185         destroy_workqueue(ccw_device_work);
186 }
187
188 subsys_initcall(init_ccw_bus_type);
189 module_exit(cleanup_ccw_bus_type);
190
191 /************************ device handling **************************/
192
193 /*
194  * A ccw_device has some interfaces in sysfs in addition to the
195  * standard ones.
196  * The following entries are designed to export the information which
197  * resided in 2.4 in /proc/subchannels. Subchannel and device number
198  * are obvious, so they don't have an entry :)
199  * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
200  */
201 static ssize_t
202 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
203 {
204         struct subchannel *sch = to_subchannel(dev);
205         struct ssd_info *ssd = &sch->ssd_info;
206         ssize_t ret = 0;
207         int chp;
208
209         for (chp = 0; chp < 8; chp++)
210                 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
211
212         ret += sprintf (buf+ret, "\n");
213         return min((ssize_t)PAGE_SIZE, ret);
214 }
215
216 static ssize_t
217 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
218 {
219         struct subchannel *sch = to_subchannel(dev);
220         struct pmcw *pmcw = &sch->schib.pmcw;
221
222         return sprintf (buf, "%02x %02x %02x\n",
223                         pmcw->pim, pmcw->pam, pmcw->pom);
224 }
225
226 static ssize_t
227 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
228 {
229         struct ccw_device *cdev = to_ccwdev(dev);
230         struct ccw_device_id *id = &(cdev->id);
231
232         if (id->dev_type != 0)
233                 return sprintf(buf, "%04x/%02x\n",
234                                 id->dev_type, id->dev_model);
235         else
236                 return sprintf(buf, "n/a\n");
237 }
238
239 static ssize_t
240 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
241 {
242         struct ccw_device *cdev = to_ccwdev(dev);
243         struct ccw_device_id *id = &(cdev->id);
244
245         return sprintf(buf, "%04x/%02x\n",
246                        id->cu_type, id->cu_model);
247 }
248
249 static ssize_t
250 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
251 {
252         struct ccw_device *cdev = to_ccwdev(dev);
253         struct ccw_device_id *id = &(cdev->id);
254         int ret;
255
256         ret = sprintf(buf, "ccw:t%04Xm%02X",
257                         id->cu_type, id->cu_model);
258         if (id->dev_type != 0)
259                 ret += sprintf(buf + ret, "dt%04Xdm%02X\n",
260                                 id->dev_type, id->dev_model);
261         else
262                 ret += sprintf(buf + ret, "dtdm\n");
263         return ret;
264 }
265
266 static ssize_t
267 online_show (struct device *dev, struct device_attribute *attr, char *buf)
268 {
269         struct ccw_device *cdev = to_ccwdev(dev);
270
271         return sprintf(buf, cdev->online ? "1\n" : "0\n");
272 }
273
274 static void
275 ccw_device_remove_disconnected(struct ccw_device *cdev)
276 {
277         struct subchannel *sch;
278         /*
279          * Forced offline in disconnected state means
280          * 'throw away device'.
281          */
282         sch = to_subchannel(cdev->dev.parent);
283         css_sch_device_unregister(sch);
284         /* Reset intparm to zeroes. */
285         sch->schib.pmcw.intparm = 0;
286         cio_modify(sch);
287         put_device(&sch->dev);
288 }
289
290 int
291 ccw_device_set_offline(struct ccw_device *cdev)
292 {
293         int ret;
294
295         if (!cdev)
296                 return -ENODEV;
297         if (!cdev->online || !cdev->drv)
298                 return -EINVAL;
299
300         if (cdev->drv->set_offline) {
301                 ret = cdev->drv->set_offline(cdev);
302                 if (ret != 0)
303                         return ret;
304         }
305         cdev->online = 0;
306         spin_lock_irq(cdev->ccwlock);
307         ret = ccw_device_offline(cdev);
308         if (ret == -ENODEV) {
309                 if (cdev->private->state != DEV_STATE_NOT_OPER) {
310                         cdev->private->state = DEV_STATE_OFFLINE;
311                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
312                 }
313                 spin_unlock_irq(cdev->ccwlock);
314                 return ret;
315         }
316         spin_unlock_irq(cdev->ccwlock);
317         if (ret == 0)
318                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
319         else {
320                 pr_debug("ccw_device_offline returned %d, device %s\n",
321                          ret, cdev->dev.bus_id);
322                 cdev->online = 1;
323         }
324         return ret;
325 }
326
327 int
328 ccw_device_set_online(struct ccw_device *cdev)
329 {
330         int ret;
331
332         if (!cdev)
333                 return -ENODEV;
334         if (cdev->online || !cdev->drv)
335                 return -EINVAL;
336
337         spin_lock_irq(cdev->ccwlock);
338         ret = ccw_device_online(cdev);
339         spin_unlock_irq(cdev->ccwlock);
340         if (ret == 0)
341                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
342         else {
343                 pr_debug("ccw_device_online returned %d, device %s\n",
344                          ret, cdev->dev.bus_id);
345                 return ret;
346         }
347         if (cdev->private->state != DEV_STATE_ONLINE)
348                 return -ENODEV;
349         if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
350                 cdev->online = 1;
351                 return 0;
352         }
353         spin_lock_irq(cdev->ccwlock);
354         ret = ccw_device_offline(cdev);
355         spin_unlock_irq(cdev->ccwlock);
356         if (ret == 0)
357                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
358         else 
359                 pr_debug("ccw_device_offline returned %d, device %s\n",
360                          ret, cdev->dev.bus_id);
361         return (ret == 0) ? -ENODEV : ret;
362 }
363
364 static ssize_t
365 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
366 {
367         struct ccw_device *cdev = to_ccwdev(dev);
368         int i, force, ret;
369         char *tmp;
370
371         if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
372                 return -EAGAIN;
373
374         if (cdev->drv && !try_module_get(cdev->drv->owner)) {
375                 atomic_set(&cdev->private->onoff, 0);
376                 return -EINVAL;
377         }
378         if (!strncmp(buf, "force\n", count)) {
379                 force = 1;
380                 i = 1;
381         } else {
382                 force = 0;
383                 i = simple_strtoul(buf, &tmp, 16);
384         }
385         if (i == 1) {
386                 /* Do device recognition, if needed. */
387                 if (cdev->id.cu_type == 0) {
388                         ret = ccw_device_recognition(cdev);
389                         if (ret) {
390                                 printk(KERN_WARNING"Couldn't start recognition "
391                                        "for device %s (ret=%d)\n",
392                                        cdev->dev.bus_id, ret);
393                                 goto out;
394                         }
395                         wait_event(cdev->private->wait_q,
396                                    cdev->private->flags.recog_done);
397                 }
398                 if (cdev->drv && cdev->drv->set_online)
399                         ccw_device_set_online(cdev);
400         } else if (i == 0) {
401                 if (cdev->private->state == DEV_STATE_DISCONNECTED)
402                         ccw_device_remove_disconnected(cdev);
403                 else if (cdev->drv && cdev->drv->set_offline)
404                         ccw_device_set_offline(cdev);
405         }
406         if (force && cdev->private->state == DEV_STATE_BOXED) {
407                 ret = ccw_device_stlck(cdev);
408                 if (ret) {
409                         printk(KERN_WARNING"ccw_device_stlck for device %s "
410                                "returned %d!\n", cdev->dev.bus_id, ret);
411                         goto out;
412                 }
413                 /* Do device recognition, if needed. */
414                 if (cdev->id.cu_type == 0) {
415                         cdev->private->state = DEV_STATE_NOT_OPER;
416                         ret = ccw_device_recognition(cdev);
417                         if (ret) {
418                                 printk(KERN_WARNING"Couldn't start recognition "
419                                        "for device %s (ret=%d)\n",
420                                        cdev->dev.bus_id, ret);
421                                 goto out;
422                         }
423                         wait_event(cdev->private->wait_q,
424                                    cdev->private->flags.recog_done);
425                 }
426                 if (cdev->drv && cdev->drv->set_online)
427                         ccw_device_set_online(cdev);
428         }
429         out:
430         if (cdev->drv)
431                 module_put(cdev->drv->owner);
432         atomic_set(&cdev->private->onoff, 0);
433         return count;
434 }
435
436 static ssize_t
437 available_show (struct device *dev, struct device_attribute *attr, char *buf)
438 {
439         struct ccw_device *cdev = to_ccwdev(dev);
440         struct subchannel *sch;
441
442         switch (cdev->private->state) {
443         case DEV_STATE_BOXED:
444                 return sprintf(buf, "boxed\n");
445         case DEV_STATE_DISCONNECTED:
446         case DEV_STATE_DISCONNECTED_SENSE_ID:
447         case DEV_STATE_NOT_OPER:
448                 sch = to_subchannel(dev->parent);
449                 if (!sch->lpm)
450                         return sprintf(buf, "no path\n");
451                 else
452                         return sprintf(buf, "no device\n");
453         default:
454                 /* All other states considered fine. */
455                 return sprintf(buf, "good\n");
456         }
457 }
458
459 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
460 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
461 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
462 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
463 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
464 static DEVICE_ATTR(online, 0644, online_show, online_store);
465 extern struct device_attribute dev_attr_cmb_enable;
466 static DEVICE_ATTR(availability, 0444, available_show, NULL);
467
468 static struct attribute * subch_attrs[] = {
469         &dev_attr_chpids.attr,
470         &dev_attr_pimpampom.attr,
471         NULL,
472 };
473
474 static struct attribute_group subch_attr_group = {
475         .attrs = subch_attrs,
476 };
477
478 static inline int
479 subchannel_add_files (struct device *dev)
480 {
481         return sysfs_create_group(&dev->kobj, &subch_attr_group);
482 }
483
484 static struct attribute * ccwdev_attrs[] = {
485         &dev_attr_devtype.attr,
486         &dev_attr_cutype.attr,
487         &dev_attr_modalias.attr,
488         &dev_attr_online.attr,
489         &dev_attr_cmb_enable.attr,
490         &dev_attr_availability.attr,
491         NULL,
492 };
493
494 static struct attribute_group ccwdev_attr_group = {
495         .attrs = ccwdev_attrs,
496 };
497
498 static inline int
499 device_add_files (struct device *dev)
500 {
501         return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
502 }
503
504 static inline void
505 device_remove_files(struct device *dev)
506 {
507         sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
508 }
509
510 /* this is a simple abstraction for device_register that sets the
511  * correct bus type and adds the bus specific files */
512 int
513 ccw_device_register(struct ccw_device *cdev)
514 {
515         struct device *dev = &cdev->dev;
516         int ret;
517
518         dev->bus = &ccw_bus_type;
519
520         if ((ret = device_add(dev)))
521                 return ret;
522
523         set_bit(1, &cdev->private->registered);
524         if ((ret = device_add_files(dev))) {
525                 if (test_and_clear_bit(1, &cdev->private->registered))
526                         device_del(dev);
527         }
528         return ret;
529 }
530
531 struct match_data {
532         unsigned int devno;
533         unsigned int ssid;
534         struct ccw_device * sibling;
535 };
536
537 static int
538 match_devno(struct device * dev, void * data)
539 {
540         struct match_data * d = (struct match_data *)data;
541         struct ccw_device * cdev;
542
543         cdev = to_ccwdev(dev);
544         if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
545             (cdev->private->devno == d->devno) &&
546             (cdev->private->ssid == d->ssid) &&
547             (cdev != d->sibling)) {
548                 cdev->private->state = DEV_STATE_NOT_OPER;
549                 return 1;
550         }
551         return 0;
552 }
553
554 static struct ccw_device *
555 get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid,
556                          struct ccw_device *sibling)
557 {
558         struct device *dev;
559         struct match_data data;
560
561         data.devno = devno;
562         data.ssid = ssid;
563         data.sibling = sibling;
564         dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
565
566         return dev ? to_ccwdev(dev) : NULL;
567 }
568
569 static void
570 ccw_device_add_changed(void *data)
571 {
572
573         struct ccw_device *cdev;
574
575         cdev = (struct ccw_device *)data;
576         if (device_add(&cdev->dev)) {
577                 put_device(&cdev->dev);
578                 return;
579         }
580         set_bit(1, &cdev->private->registered);
581         if (device_add_files(&cdev->dev)) {
582                 if (test_and_clear_bit(1, &cdev->private->registered))
583                         device_unregister(&cdev->dev);
584         }
585 }
586
587 extern int css_get_ssd_info(struct subchannel *sch);
588
589 void
590 ccw_device_do_unreg_rereg(void *data)
591 {
592         struct ccw_device *cdev;
593         struct subchannel *sch;
594         int need_rename;
595
596         cdev = (struct ccw_device *)data;
597         sch = to_subchannel(cdev->dev.parent);
598         if (cdev->private->devno != sch->schib.pmcw.dev) {
599                 /*
600                  * The device number has changed. This is usually only when
601                  * a device has been detached under VM and then re-appeared
602                  * on another subchannel because of a different attachment
603                  * order than before. Ideally, we should should just switch
604                  * subchannels, but unfortunately, this is not possible with
605                  * the current implementation.
606                  * Instead, we search for the old subchannel for this device
607                  * number and deregister so there are no collisions with the
608                  * newly registered ccw_device.
609                  * FIXME: Find another solution so the block layer doesn't
610                  *        get possibly sick...
611                  */
612                 struct ccw_device *other_cdev;
613
614                 need_rename = 1;
615                 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
616                                                       sch->schid.ssid, cdev);
617                 if (other_cdev) {
618                         struct subchannel *other_sch;
619
620                         other_sch = to_subchannel(other_cdev->dev.parent);
621                         if (get_device(&other_sch->dev)) {
622                                 stsch(other_sch->schid, &other_sch->schib);
623                                 if (other_sch->schib.pmcw.dnv) {
624                                         other_sch->schib.pmcw.intparm = 0;
625                                         cio_modify(other_sch);
626                                 }
627                                 css_sch_device_unregister(other_sch);
628                         }
629                 }
630                 /* Update ssd info here. */
631                 css_get_ssd_info(sch);
632                 cdev->private->devno = sch->schib.pmcw.dev;
633         } else
634                 need_rename = 0;
635         device_remove_files(&cdev->dev);
636         if (test_and_clear_bit(1, &cdev->private->registered))
637                 device_del(&cdev->dev);
638         if (need_rename)
639                 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
640                           sch->schid.ssid, sch->schib.pmcw.dev);
641         PREPARE_WORK(&cdev->private->kick_work,
642                      ccw_device_add_changed, (void *)cdev);
643         queue_work(ccw_device_work, &cdev->private->kick_work);
644 }
645
646 static void
647 ccw_device_release(struct device *dev)
648 {
649         struct ccw_device *cdev;
650
651         cdev = to_ccwdev(dev);
652         kfree(cdev->private);
653         kfree(cdev);
654 }
655
656 /*
657  * Register recognized device.
658  */
659 static void
660 io_subchannel_register(void *data)
661 {
662         struct ccw_device *cdev;
663         struct subchannel *sch;
664         int ret;
665         unsigned long flags;
666
667         cdev = (struct ccw_device *) data;
668         sch = to_subchannel(cdev->dev.parent);
669
670         if (klist_node_attached(&cdev->dev.knode_parent)) {
671                 bus_rescan_devices(&ccw_bus_type);
672                 goto out;
673         }
674         /* make it known to the system */
675         ret = ccw_device_register(cdev);
676         if (ret) {
677                 printk (KERN_WARNING "%s: could not register %s\n",
678                         __func__, cdev->dev.bus_id);
679                 put_device(&cdev->dev);
680                 spin_lock_irqsave(&sch->lock, flags);
681                 sch->dev.driver_data = NULL;
682                 spin_unlock_irqrestore(&sch->lock, flags);
683                 kfree (cdev->private);
684                 kfree (cdev);
685                 put_device(&sch->dev);
686                 if (atomic_dec_and_test(&ccw_device_init_count))
687                         wake_up(&ccw_device_init_wq);
688                 return;
689         }
690
691         ret = subchannel_add_files(cdev->dev.parent);
692         if (ret)
693                 printk(KERN_WARNING "%s: could not add attributes to %s\n",
694                        __func__, sch->dev.bus_id);
695         put_device(&cdev->dev);
696 out:
697         cdev->private->flags.recog_done = 1;
698         put_device(&sch->dev);
699         wake_up(&cdev->private->wait_q);
700         if (atomic_dec_and_test(&ccw_device_init_count))
701                 wake_up(&ccw_device_init_wq);
702 }
703
704 void
705 ccw_device_call_sch_unregister(void *data)
706 {
707         struct ccw_device *cdev = data;
708         struct subchannel *sch;
709
710         sch = to_subchannel(cdev->dev.parent);
711         css_sch_device_unregister(sch);
712         /* Reset intparm to zeroes. */
713         sch->schib.pmcw.intparm = 0;
714         cio_modify(sch);
715         put_device(&cdev->dev);
716         put_device(&sch->dev);
717 }
718
719 /*
720  * subchannel recognition done. Called from the state machine.
721  */
722 void
723 io_subchannel_recog_done(struct ccw_device *cdev)
724 {
725         struct subchannel *sch;
726
727         if (css_init_done == 0) {
728                 cdev->private->flags.recog_done = 1;
729                 return;
730         }
731         switch (cdev->private->state) {
732         case DEV_STATE_NOT_OPER:
733                 cdev->private->flags.recog_done = 1;
734                 /* Remove device found not operational. */
735                 if (!get_device(&cdev->dev))
736                         break;
737                 sch = to_subchannel(cdev->dev.parent);
738                 PREPARE_WORK(&cdev->private->kick_work,
739                              ccw_device_call_sch_unregister, (void *) cdev);
740                 queue_work(slow_path_wq, &cdev->private->kick_work);
741                 if (atomic_dec_and_test(&ccw_device_init_count))
742                         wake_up(&ccw_device_init_wq);
743                 break;
744         case DEV_STATE_BOXED:
745                 /* Device did not respond in time. */
746         case DEV_STATE_OFFLINE:
747                 /* 
748                  * We can't register the device in interrupt context so
749                  * we schedule a work item.
750                  */
751                 if (!get_device(&cdev->dev))
752                         break;
753                 PREPARE_WORK(&cdev->private->kick_work,
754                              io_subchannel_register, (void *) cdev);
755                 queue_work(slow_path_wq, &cdev->private->kick_work);
756                 break;
757         }
758 }
759
760 static int
761 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
762 {
763         int rc;
764         struct ccw_device_private *priv;
765
766         sch->dev.driver_data = cdev;
767         sch->driver = &io_subchannel_driver;
768         cdev->ccwlock = &sch->lock;
769
770         /* Init private data. */
771         priv = cdev->private;
772         priv->devno = sch->schib.pmcw.dev;
773         priv->ssid = sch->schid.ssid;
774         priv->sch_no = sch->schid.sch_no;
775         priv->state = DEV_STATE_NOT_OPER;
776         INIT_LIST_HEAD(&priv->cmb_list);
777         init_waitqueue_head(&priv->wait_q);
778         init_timer(&priv->timer);
779
780         /* Set an initial name for the device. */
781         snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
782                   sch->schid.ssid, sch->schib.pmcw.dev);
783
784         /* Increase counter of devices currently in recognition. */
785         atomic_inc(&ccw_device_init_count);
786
787         /* Start async. device sensing. */
788         spin_lock_irq(&sch->lock);
789         rc = ccw_device_recognition(cdev);
790         spin_unlock_irq(&sch->lock);
791         if (rc) {
792                 if (atomic_dec_and_test(&ccw_device_init_count))
793                         wake_up(&ccw_device_init_wq);
794         }
795         return rc;
796 }
797
798 static int
799 io_subchannel_probe (struct subchannel *sch)
800 {
801         struct ccw_device *cdev;
802         int rc;
803         unsigned long flags;
804
805         if (sch->dev.driver_data) {
806                 /*
807                  * This subchannel already has an associated ccw_device.
808                  * Register it and exit. This happens for all early
809                  * device, e.g. the console.
810                  */
811                 cdev = sch->dev.driver_data;
812                 device_initialize(&cdev->dev);
813                 ccw_device_register(cdev);
814                 subchannel_add_files(&sch->dev);
815                 /*
816                  * Check if the device is already online. If it is
817                  * the reference count needs to be corrected
818                  * (see ccw_device_online and css_init_done for the
819                  * ugly details).
820                  */
821                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
822                     cdev->private->state != DEV_STATE_OFFLINE &&
823                     cdev->private->state != DEV_STATE_BOXED)
824                         get_device(&cdev->dev);
825                 return 0;
826         }
827         cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
828         if (!cdev)
829                 return -ENOMEM;
830         cdev->private = kzalloc(sizeof(struct ccw_device_private),
831                                 GFP_KERNEL | GFP_DMA);
832         if (!cdev->private) {
833                 kfree(cdev);
834                 return -ENOMEM;
835         }
836         atomic_set(&cdev->private->onoff, 0);
837         cdev->dev.parent = &sch->dev;
838         cdev->dev.release = ccw_device_release;
839         INIT_LIST_HEAD(&cdev->private->kick_work.entry);
840         /* Do first half of device_register. */
841         device_initialize(&cdev->dev);
842
843         if (!get_device(&sch->dev)) {
844                 if (cdev->dev.release)
845                         cdev->dev.release(&cdev->dev);
846                 return -ENODEV;
847         }
848
849         rc = io_subchannel_recog(cdev, sch);
850         if (rc) {
851                 spin_lock_irqsave(&sch->lock, flags);
852                 sch->dev.driver_data = NULL;
853                 spin_unlock_irqrestore(&sch->lock, flags);
854                 if (cdev->dev.release)
855                         cdev->dev.release(&cdev->dev);
856         }
857
858         return rc;
859 }
860
861 static void
862 ccw_device_unregister(void *data)
863 {
864         struct ccw_device *cdev;
865
866         cdev = (struct ccw_device *)data;
867         if (test_and_clear_bit(1, &cdev->private->registered))
868                 device_unregister(&cdev->dev);
869         put_device(&cdev->dev);
870 }
871
872 static int
873 io_subchannel_remove (struct subchannel *sch)
874 {
875         struct ccw_device *cdev;
876         unsigned long flags;
877
878         if (!sch->dev.driver_data)
879                 return 0;
880         cdev = sch->dev.driver_data;
881         /* Set ccw device to not operational and drop reference. */
882         spin_lock_irqsave(cdev->ccwlock, flags);
883         sch->dev.driver_data = NULL;
884         cdev->private->state = DEV_STATE_NOT_OPER;
885         spin_unlock_irqrestore(cdev->ccwlock, flags);
886         /*
887          * Put unregistration on workqueue to avoid livelocks on the css bus
888          * semaphore.
889          */
890         if (get_device(&cdev->dev)) {
891                 PREPARE_WORK(&cdev->private->kick_work,
892                              ccw_device_unregister, (void *) cdev);
893                 queue_work(ccw_device_work, &cdev->private->kick_work);
894         }
895         return 0;
896 }
897
898 static int
899 io_subchannel_notify(struct device *dev, int event)
900 {
901         struct ccw_device *cdev;
902
903         cdev = dev->driver_data;
904         if (!cdev)
905                 return 0;
906         if (!cdev->drv)
907                 return 0;
908         if (!cdev->online)
909                 return 0;
910         return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
911 }
912
913 static void
914 io_subchannel_verify(struct device *dev)
915 {
916         struct ccw_device *cdev;
917
918         cdev = dev->driver_data;
919         if (cdev)
920                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
921 }
922
923 static void
924 io_subchannel_ioterm(struct device *dev)
925 {
926         struct ccw_device *cdev;
927
928         cdev = dev->driver_data;
929         if (!cdev)
930                 return;
931         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
932         if (cdev->handler)
933                 cdev->handler(cdev, cdev->private->intparm,
934                               ERR_PTR(-EIO));
935 }
936
937 static void
938 io_subchannel_shutdown(struct subchannel *sch)
939 {
940         struct ccw_device *cdev;
941         int ret;
942
943         cdev = sch->dev.driver_data;
944
945         if (cio_is_console(sch->schid))
946                 return;
947         if (!sch->schib.pmcw.ena)
948                 /* Nothing to do. */
949                 return;
950         ret = cio_disable_subchannel(sch);
951         if (ret != -EBUSY)
952                 /* Subchannel is disabled, we're done. */
953                 return;
954         cdev->private->state = DEV_STATE_QUIESCE;
955         if (cdev->handler)
956                 cdev->handler(cdev, cdev->private->intparm,
957                               ERR_PTR(-EIO));
958         ret = ccw_device_cancel_halt_clear(cdev);
959         if (ret == -EBUSY) {
960                 ccw_device_set_timeout(cdev, HZ/10);
961                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
962         }
963         cio_disable_subchannel(sch);
964 }
965
966 #ifdef CONFIG_CCW_CONSOLE
967 static struct ccw_device console_cdev;
968 static struct ccw_device_private console_private;
969 static int console_cdev_in_use;
970
971 static int
972 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
973 {
974         int rc;
975
976         /* Initialize the ccw_device structure. */
977         cdev->dev.parent= &sch->dev;
978         rc = io_subchannel_recog(cdev, sch);
979         if (rc)
980                 return rc;
981
982         /* Now wait for the async. recognition to come to an end. */
983         spin_lock_irq(cdev->ccwlock);
984         while (!dev_fsm_final_state(cdev))
985                 wait_cons_dev();
986         rc = -EIO;
987         if (cdev->private->state != DEV_STATE_OFFLINE)
988                 goto out_unlock;
989         ccw_device_online(cdev);
990         while (!dev_fsm_final_state(cdev))
991                 wait_cons_dev();
992         if (cdev->private->state != DEV_STATE_ONLINE)
993                 goto out_unlock;
994         rc = 0;
995 out_unlock:
996         spin_unlock_irq(cdev->ccwlock);
997         return 0;
998 }
999
1000 struct ccw_device *
1001 ccw_device_probe_console(void)
1002 {
1003         struct subchannel *sch;
1004         int ret;
1005
1006         if (xchg(&console_cdev_in_use, 1) != 0)
1007                 return ERR_PTR(-EBUSY);
1008         sch = cio_probe_console();
1009         if (IS_ERR(sch)) {
1010                 console_cdev_in_use = 0;
1011                 return (void *) sch;
1012         }
1013         memset(&console_cdev, 0, sizeof(struct ccw_device));
1014         memset(&console_private, 0, sizeof(struct ccw_device_private));
1015         console_cdev.private = &console_private;
1016         ret = ccw_device_console_enable(&console_cdev, sch);
1017         if (ret) {
1018                 cio_release_console();
1019                 console_cdev_in_use = 0;
1020                 return ERR_PTR(ret);
1021         }
1022         console_cdev.online = 1;
1023         return &console_cdev;
1024 }
1025 #endif
1026
1027 /*
1028  * get ccw_device matching the busid, but only if owned by cdrv
1029  */
1030 static int
1031 __ccwdev_check_busid(struct device *dev, void *id)
1032 {
1033         char *bus_id;
1034
1035         bus_id = (char *)id;
1036
1037         return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1038 }
1039
1040
1041 struct ccw_device *
1042 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1043 {
1044         struct device *dev;
1045         struct device_driver *drv;
1046
1047         drv = get_driver(&cdrv->driver);
1048         if (!drv)
1049                 return NULL;
1050
1051         dev = driver_find_device(drv, NULL, (void *)bus_id,
1052                                  __ccwdev_check_busid);
1053         put_driver(drv);
1054
1055         return dev ? to_ccwdev(dev) : NULL;
1056 }
1057
1058 /************************** device driver handling ************************/
1059
1060 /* This is the implementation of the ccw_driver class. The probe, remove
1061  * and release methods are initially very similar to the device_driver
1062  * implementations, with the difference that they have ccw_device
1063  * arguments.
1064  *
1065  * A ccw driver also contains the information that is needed for
1066  * device matching.
1067  */
1068 static int
1069 ccw_device_probe (struct device *dev)
1070 {
1071         struct ccw_device *cdev = to_ccwdev(dev);
1072         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1073         int ret;
1074
1075         cdev->drv = cdrv; /* to let the driver call _set_online */
1076
1077         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1078
1079         if (ret) {
1080                 cdev->drv = NULL;
1081                 return ret;
1082         }
1083
1084         return 0;
1085 }
1086
1087 static int
1088 ccw_device_remove (struct device *dev)
1089 {
1090         struct ccw_device *cdev = to_ccwdev(dev);
1091         struct ccw_driver *cdrv = cdev->drv;
1092         int ret;
1093
1094         pr_debug("removing device %s\n", cdev->dev.bus_id);
1095         if (cdrv->remove)
1096                 cdrv->remove(cdev);
1097         if (cdev->online) {
1098                 cdev->online = 0;
1099                 spin_lock_irq(cdev->ccwlock);
1100                 ret = ccw_device_offline(cdev);
1101                 spin_unlock_irq(cdev->ccwlock);
1102                 if (ret == 0)
1103                         wait_event(cdev->private->wait_q,
1104                                    dev_fsm_final_state(cdev));
1105                 else
1106                         //FIXME: we can't fail!
1107                         pr_debug("ccw_device_offline returned %d, device %s\n",
1108                                  ret, cdev->dev.bus_id);
1109         }
1110         ccw_device_set_timeout(cdev, 0);
1111         cdev->drv = NULL;
1112         return 0;
1113 }
1114
1115 struct bus_type ccw_bus_type = {
1116         .name   = "ccw",
1117         .match  = ccw_bus_match,
1118         .uevent = ccw_uevent,
1119         .probe  = ccw_device_probe,
1120         .remove = ccw_device_remove,
1121 };
1122
1123 int
1124 ccw_driver_register (struct ccw_driver *cdriver)
1125 {
1126         struct device_driver *drv = &cdriver->driver;
1127
1128         drv->bus = &ccw_bus_type;
1129         drv->name = cdriver->name;
1130
1131         return driver_register(drv);
1132 }
1133
1134 void
1135 ccw_driver_unregister (struct ccw_driver *cdriver)
1136 {
1137         driver_unregister(&cdriver->driver);
1138 }
1139
1140 /* Helper func for qdio. */
1141 struct subchannel_id
1142 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1143 {
1144         struct subchannel *sch;
1145
1146         sch = to_subchannel(cdev->dev.parent);
1147         return sch->schid;
1148 }
1149
1150 MODULE_LICENSE("GPL");
1151 EXPORT_SYMBOL(ccw_device_set_online);
1152 EXPORT_SYMBOL(ccw_device_set_offline);
1153 EXPORT_SYMBOL(ccw_driver_register);
1154 EXPORT_SYMBOL(ccw_driver_unregister);
1155 EXPORT_SYMBOL(get_ccwdev_by_busid);
1156 EXPORT_SYMBOL(ccw_bus_type);
1157 EXPORT_SYMBOL(ccw_device_work);
1158 EXPORT_SYMBOL(ccw_device_notify_work);
1159 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);