patch-2_6_7-vs1_9_1_12
[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.120 $
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 void
544 ccw_device_do_unreg_rereg(void *data)
545 {
546         struct ccw_device *cdev;
547         struct subchannel *sch;
548         int need_rename;
549
550         cdev = (struct ccw_device *)data;
551         sch = to_subchannel(cdev->dev.parent);
552         if (cdev->private->devno != sch->schib.pmcw.dev) {
553                 /*
554                  * The device number has changed. This is usually only when
555                  * a device has been detached under VM and then re-appeared
556                  * on another subchannel because of a different attachment
557                  * order than before. Ideally, we should should just switch
558                  * subchannels, but unfortunately, this is not possible with
559                  * the current implementation.
560                  * Instead, we search for the old subchannel for this device
561                  * number and deregister so there are no collisions with the
562                  * newly registered ccw_device.
563                  * FIXME: Find another solution so the block layer doesn't
564                  *        get possibly sick...
565                  */
566                 struct ccw_device *other_cdev;
567
568                 need_rename = 1;
569                 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
570                                                       cdev);
571                 if (other_cdev) {
572                         struct subchannel *other_sch;
573
574                         other_sch = to_subchannel(other_cdev->dev.parent);
575                         if (get_device(&other_sch->dev)) {
576                                 stsch(other_sch->irq, &other_sch->schib);
577                                 if (other_sch->schib.pmcw.dnv) {
578                                         other_sch->schib.pmcw.intparm = 0;
579                                         cio_modify(other_sch);
580                                 }
581                                 device_unregister(&other_sch->dev);
582                         }
583                 }
584                 cdev->private->devno = sch->schib.pmcw.dev;
585         } else
586                 need_rename = 0;
587         device_remove_files(&cdev->dev);
588         device_del(&cdev->dev);
589         if (need_rename)
590                 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.0.%04x",
591                           sch->schib.pmcw.dev);
592         if (device_add(&cdev->dev)) {
593                 put_device(&cdev->dev);
594                 return;
595         }
596         if (device_add_files(&cdev->dev))
597                 device_unregister(&cdev->dev);
598 }
599
600 static void
601 ccw_device_release(struct device *dev)
602 {
603         struct ccw_device *cdev;
604
605         cdev = to_ccwdev(dev);
606         kfree(cdev->private);
607         kfree(cdev);
608 }
609
610 /*
611  * Register recognized device.
612  */
613 static void
614 io_subchannel_register(void *data)
615 {
616         struct ccw_device *cdev;
617         struct subchannel *sch;
618         int ret;
619
620         cdev = (struct ccw_device *) data;
621         sch = to_subchannel(cdev->dev.parent);
622
623         if (!list_empty(&sch->dev.children)) {
624                 bus_rescan_devices(&ccw_bus_type);
625                 goto out;
626         }
627         /* make it known to the system */
628         ret = ccw_device_register(cdev);
629         if (ret) {
630                 printk (KERN_WARNING "%s: could not register %s\n",
631                         __func__, cdev->dev.bus_id);
632                 put_device(&cdev->dev);
633                 sch->dev.driver_data = 0;
634                 kfree (cdev->private);
635                 kfree (cdev);
636                 goto out;
637         }
638
639         ret = subchannel_add_files(cdev->dev.parent);
640         if (ret)
641                 printk(KERN_WARNING "%s: could not add attributes to %s\n",
642                        __func__, sch->dev.bus_id);
643         put_device(&cdev->dev);
644 out:
645         cdev->private->flags.recog_done = 1;
646         put_device(&sch->dev);
647         wake_up(&cdev->private->wait_q);
648 }
649
650 void
651 ccw_device_call_sch_unregister(void *data)
652 {
653         struct ccw_device *cdev = data;
654         struct subchannel *sch;
655
656         sch = to_subchannel(cdev->dev.parent);
657         device_unregister(&sch->dev);
658         /* Reset intparm to zeroes. */
659         sch->schib.pmcw.intparm = 0;
660         cio_modify(sch);
661         put_device(&cdev->dev);
662         put_device(&sch->dev);
663 }
664
665 /*
666  * subchannel recognition done. Called from the state machine.
667  */
668 void
669 io_subchannel_recog_done(struct ccw_device *cdev)
670 {
671         struct subchannel *sch;
672
673         if (css_init_done == 0) {
674                 cdev->private->flags.recog_done = 1;
675                 return;
676         }
677         switch (cdev->private->state) {
678         case DEV_STATE_NOT_OPER:
679                 cdev->private->flags.recog_done = 1;
680                 /* Remove device found not operational. */
681                 if (!get_device(&cdev->dev))
682                         break;
683                 sch = to_subchannel(cdev->dev.parent);
684                 INIT_WORK(&cdev->private->kick_work,
685                           ccw_device_call_sch_unregister, (void *) cdev);
686                 queue_work(slow_path_wq, &cdev->private->kick_work);
687                 break;
688         case DEV_STATE_BOXED:
689                 /* Device did not respond in time. */
690         case DEV_STATE_OFFLINE:
691                 /* 
692                  * We can't register the device in interrupt context so
693                  * we schedule a work item.
694                  */
695                 if (!get_device(&cdev->dev))
696                         break;
697                 INIT_WORK(&cdev->private->kick_work,
698                           io_subchannel_register, (void *) cdev);
699                 queue_work(ccw_device_work, &cdev->private->kick_work);
700                 break;
701         }
702         if (atomic_dec_and_test(&ccw_device_init_count))
703                 wake_up(&ccw_device_init_wq);
704 }
705
706 static int
707 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
708 {
709         int rc;
710
711         sch->dev.driver_data = cdev;
712         sch->driver = &io_subchannel_driver;
713         cdev->ccwlock = &sch->lock;
714         *cdev->private = (struct ccw_device_private) {
715                 .devno  = sch->schib.pmcw.dev,
716                 .irq    = sch->irq,
717                 .state  = DEV_STATE_NOT_OPER,
718                 .cmb_list = LIST_HEAD_INIT(cdev->private->cmb_list),
719         };
720         init_waitqueue_head(&cdev->private->wait_q);
721         init_timer(&cdev->private->timer);
722
723         /* Set an initial name for the device. */
724         snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.0.%04x",
725                   sch->schib.pmcw.dev);
726
727         /* Increase counter of devices currently in recognition. */
728         atomic_inc(&ccw_device_init_count);
729
730         /* Start async. device sensing. */
731         spin_lock_irq(&sch->lock);
732         rc = ccw_device_recognition(cdev);
733         spin_unlock_irq(&sch->lock);
734         if (rc) {
735                 if (atomic_dec_and_test(&ccw_device_init_count))
736                         wake_up(&ccw_device_init_wq);
737         }
738         return rc;
739 }
740
741 static int
742 io_subchannel_probe (struct device *pdev)
743 {
744         struct subchannel *sch;
745         struct ccw_device *cdev;
746         int rc;
747
748         sch = to_subchannel(pdev);
749         if (sch->dev.driver_data) {
750                 /*
751                  * This subchannel already has an associated ccw_device.
752                  * Register it and exit. This happens for all early
753                  * device, e.g. the console.
754                  */
755                 cdev = sch->dev.driver_data;
756                 device_initialize(&cdev->dev);
757                 ccw_device_register(cdev);
758                 subchannel_add_files(&sch->dev);
759                 /*
760                  * Check if the device is already online. If it is
761                  * the reference count needs to be corrected
762                  * (see ccw_device_online and css_init_done for the
763                  * ugly details).
764                  */
765                 if (cdev->private->state != DEV_STATE_NOT_OPER &&
766                     cdev->private->state != DEV_STATE_OFFLINE &&
767                     cdev->private->state != DEV_STATE_BOXED)
768                         get_device(&cdev->dev);
769                 return 0;
770         }
771         cdev  = kmalloc (sizeof(*cdev), GFP_KERNEL);
772         if (!cdev)
773                 return -ENOMEM;
774         memset(cdev, 0, sizeof(struct ccw_device));
775         cdev->private = kmalloc(sizeof(struct ccw_device_private), 
776                                 GFP_KERNEL | GFP_DMA);
777         if (!cdev->private) {
778                 kfree(cdev);
779                 return -ENOMEM;
780         }
781         memset(cdev->private, 0, sizeof(struct ccw_device_private));
782         atomic_set(&cdev->private->onoff, 0);
783         cdev->dev = (struct device) {
784                 .parent = pdev,
785                 .release = ccw_device_release,
786         };
787         /* Do first half of device_register. */
788         device_initialize(&cdev->dev);
789
790         if (!get_device(&sch->dev)) {
791                 if (cdev->dev.release)
792                         cdev->dev.release(&cdev->dev);
793                 return -ENODEV;
794         }
795
796         rc = io_subchannel_recog(cdev, to_subchannel(pdev));
797         if (rc) {
798                 sch->dev.driver_data = 0;
799                 if (cdev->dev.release)
800                         cdev->dev.release(&cdev->dev);
801         }
802
803         return rc;
804 }
805
806 static int
807 io_subchannel_remove (struct device *dev)
808 {
809         struct ccw_device *cdev;
810
811         if (!dev->driver_data)
812                 return 0;
813         cdev = dev->driver_data;
814         /* Set ccw device to not operational and drop reference. */
815         cdev->private->state = DEV_STATE_NOT_OPER;
816         /*
817          * Careful here. Our ccw device might be yet unregistered when
818          * de-registering its subchannel (machine check during device
819          * recognition). Better look if the subchannel has children.
820          */
821         if (!list_empty(&dev->children))
822                 device_unregister(&cdev->dev);
823         dev->driver_data = NULL;
824         return 0;
825 }
826
827 static int
828 io_subchannel_notify(struct device *dev, int event)
829 {
830         struct ccw_device *cdev;
831
832         cdev = dev->driver_data;
833         if (!cdev)
834                 return 0;
835         if (!cdev->drv)
836                 return 0;
837         if (!cdev->online)
838                 return 0;
839         return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
840 }
841
842 static void
843 io_subchannel_verify(struct device *dev)
844 {
845         struct ccw_device *cdev;
846
847         cdev = dev->driver_data;
848         if (cdev)
849                 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
850 }
851
852 static void
853 io_subchannel_ioterm(struct device *dev)
854 {
855         struct ccw_device *cdev;
856
857         cdev = dev->driver_data;
858         if (!cdev)
859                 return;
860         cdev->private->state = DEV_STATE_CLEAR_VERIFY;
861         if (cdev->handler)
862                 cdev->handler(cdev, cdev->private->intparm,
863                               ERR_PTR(-EIO));
864 }
865
866 static void
867 io_subchannel_shutdown(struct device *dev)
868 {
869         struct subchannel *sch;
870         struct ccw_device *cdev;
871         int ret;
872
873         sch = to_subchannel(dev);
874         cdev = dev->driver_data;
875
876         if (cio_is_console(sch->irq))
877                 return;
878         if (!sch->schib.pmcw.ena)
879                 /* Nothing to do. */
880                 return;
881         ret = cio_disable_subchannel(sch);
882         if (ret != -EBUSY)
883                 /* Subchannel is disabled, we're done. */
884                 return;
885         cdev->private->state = DEV_STATE_QUIESCE;
886         if (cdev->handler)
887                 cdev->handler(cdev, cdev->private->intparm,
888                               ERR_PTR(-EIO));
889         ret = ccw_device_cancel_halt_clear(cdev);
890         if (ret == -EBUSY) {
891                 ccw_device_set_timeout(cdev, HZ/10);
892                 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
893         }
894         cio_disable_subchannel(sch);
895 }
896
897 #ifdef CONFIG_CCW_CONSOLE
898 static struct ccw_device console_cdev;
899 static struct ccw_device_private console_private;
900 static int console_cdev_in_use;
901
902 static int
903 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
904 {
905         int rc;
906
907         /* Initialize the ccw_device structure. */
908         cdev->dev = (struct device) {
909                 .parent = &sch->dev,
910         };
911         /* Initialize the subchannel structure */
912         sch->dev.parent = &css_bus_device;
913         sch->dev.bus = &css_bus_type;
914
915         rc = io_subchannel_recog(cdev, sch);
916         if (rc)
917                 return rc;
918
919         /* Now wait for the async. recognition to come to an end. */
920         spin_lock_irq(cdev->ccwlock);
921         while (!dev_fsm_final_state(cdev))
922                 wait_cons_dev();
923         rc = -EIO;
924         if (cdev->private->state != DEV_STATE_OFFLINE)
925                 goto out_unlock;
926         ccw_device_online(cdev);
927         while (!dev_fsm_final_state(cdev))
928                 wait_cons_dev();
929         if (cdev->private->state != DEV_STATE_ONLINE)
930                 goto out_unlock;
931         rc = 0;
932 out_unlock:
933         spin_unlock_irq(cdev->ccwlock);
934         return 0;
935 }
936
937 struct ccw_device *
938 ccw_device_probe_console(void)
939 {
940         struct subchannel *sch;
941         int ret;
942
943         if (xchg(&console_cdev_in_use, 1) != 0)
944                 return NULL;
945         sch = cio_probe_console();
946         if (IS_ERR(sch)) {
947                 console_cdev_in_use = 0;
948                 return (void *) sch;
949         }
950         memset(&console_cdev, 0, sizeof(struct ccw_device));
951         memset(&console_private, 0, sizeof(struct ccw_device_private));
952         console_cdev.private = &console_private;
953         ret = ccw_device_console_enable(&console_cdev, sch);
954         if (ret) {
955                 cio_release_console();
956                 console_cdev_in_use = 0;
957                 return ERR_PTR(ret);
958         }
959         console_cdev.online = 1;
960         return &console_cdev;
961 }
962 #endif
963
964 /*
965  * get ccw_device matching the busid, but only if owned by cdrv
966  */
967 struct ccw_device *
968 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
969 {
970         struct device *d, *dev;
971         struct device_driver *drv;
972
973         drv = get_driver(&cdrv->driver);
974         if (!drv)
975                 return 0;
976
977         down_read(&drv->bus->subsys.rwsem);
978
979         dev = NULL;
980         list_for_each_entry(d, &drv->devices, driver_list) {
981                 dev = get_device(d);
982
983                 if (dev && !strncmp(bus_id, dev->bus_id, BUS_ID_SIZE))
984                         break;
985                 else if (dev) {
986                         put_device(dev);
987                         dev = NULL;
988                 }
989         }
990         up_read(&drv->bus->subsys.rwsem);
991         put_driver(drv);
992
993         return dev ? to_ccwdev(dev) : 0;
994 }
995
996 /************************** device driver handling ************************/
997
998 /* This is the implementation of the ccw_driver class. The probe, remove
999  * and release methods are initially very similar to the device_driver
1000  * implementations, with the difference that they have ccw_device
1001  * arguments.
1002  *
1003  * A ccw driver also contains the information that is needed for
1004  * device matching.
1005  */
1006 static int
1007 ccw_device_probe (struct device *dev)
1008 {
1009         struct ccw_device *cdev = to_ccwdev(dev);
1010         struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1011         int ret;
1012
1013         cdev->drv = cdrv; /* to let the driver call _set_online */
1014
1015         ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1016
1017         if (ret) {
1018                 cdev->drv = 0;
1019                 return ret;
1020         }
1021
1022         return 0;
1023 }
1024
1025 static int
1026 ccw_device_remove (struct device *dev)
1027 {
1028         struct ccw_device *cdev = to_ccwdev(dev);
1029         struct ccw_driver *cdrv = cdev->drv;
1030         int ret;
1031
1032         pr_debug("removing device %s\n", cdev->dev.bus_id);
1033         if (cdrv->remove)
1034                 cdrv->remove(cdev);
1035         if (cdev->online) {
1036                 cdev->online = 0;
1037                 spin_lock_irq(cdev->ccwlock);
1038                 ret = ccw_device_offline(cdev);
1039                 spin_unlock_irq(cdev->ccwlock);
1040                 if (ret == 0)
1041                         wait_event(cdev->private->wait_q,
1042                                    dev_fsm_final_state(cdev));
1043                 else
1044                         //FIXME: we can't fail!
1045                         pr_debug("ccw_device_offline returned %d, device %s\n",
1046                                  ret, cdev->dev.bus_id);
1047         }
1048         ccw_device_set_timeout(cdev, 0);
1049         cdev->drv = 0;
1050         return 0;
1051 }
1052
1053 int
1054 ccw_driver_register (struct ccw_driver *cdriver)
1055 {
1056         struct device_driver *drv = &cdriver->driver;
1057
1058         drv->bus = &ccw_bus_type;
1059         drv->name = cdriver->name;
1060         drv->probe = ccw_device_probe;
1061         drv->remove = ccw_device_remove;
1062
1063         return driver_register(drv);
1064 }
1065
1066 void
1067 ccw_driver_unregister (struct ccw_driver *cdriver)
1068 {
1069         driver_unregister(&cdriver->driver);
1070 }
1071
1072 MODULE_LICENSE("GPL");
1073 EXPORT_SYMBOL(ccw_device_set_online);
1074 EXPORT_SYMBOL(ccw_device_set_offline);
1075 EXPORT_SYMBOL(ccw_driver_register);
1076 EXPORT_SYMBOL(ccw_driver_unregister);
1077 EXPORT_SYMBOL(get_ccwdev_by_busid);
1078 EXPORT_SYMBOL(ccw_bus_type);
1079 EXPORT_SYMBOL(ccw_device_work);
1080 EXPORT_SYMBOL(ccw_device_notify_work);