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