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