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