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