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