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