2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
30 /******************* bus type handling ***********************/
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. */
37 ccw_bus_match (struct device * dev, struct device_driver * drv)
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;
46 found = ccw_device_id_match(ids, &cdev->id);
50 cdev->id.driver_info = found->driver_info;
56 * Hotplugging interface for ccw devices.
57 * Heavily modeled on pci and usb hotplug.
60 ccw_uevent (struct device *dev, char **envp, int num_envp,
61 char *buffer, int buffer_size)
63 struct ccw_device *cdev = to_ccwdev(dev);
70 /* what we want to pass to /sbin/hotplug */
73 length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X",
75 if ((buffer_size - length <= 0) || (i >= num_envp))
81 length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X",
83 if ((buffer_size - length <= 0) || (i >= num_envp))
88 /* The next two can be zero, that's ok for us */
90 length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X",
92 if ((buffer_size - length <= 0) || (i >= num_envp))
98 length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X",
100 if ((buffer_size - length <= 0) || (i >= num_envp))
108 struct bus_type ccw_bus_type;
110 static int io_subchannel_probe (struct subchannel *);
111 static int io_subchannel_remove (struct subchannel *);
112 void io_subchannel_irq (struct device *);
113 static int io_subchannel_notify(struct device *, int);
114 static void io_subchannel_verify(struct device *);
115 static void io_subchannel_ioterm(struct device *);
116 static void io_subchannel_shutdown(struct subchannel *);
118 struct css_driver io_subchannel_driver = {
119 .subchannel_type = SUBCHANNEL_TYPE_IO,
121 .name = "io_subchannel",
122 .bus = &css_bus_type,
124 .irq = io_subchannel_irq,
125 .notify = io_subchannel_notify,
126 .verify = io_subchannel_verify,
127 .termination = io_subchannel_ioterm,
128 .probe = io_subchannel_probe,
129 .remove = io_subchannel_remove,
130 .shutdown = io_subchannel_shutdown,
133 struct workqueue_struct *ccw_device_work;
134 struct workqueue_struct *ccw_device_notify_work;
135 wait_queue_head_t ccw_device_init_wq;
136 atomic_t ccw_device_init_count;
139 init_ccw_bus_type (void)
143 init_waitqueue_head(&ccw_device_init_wq);
144 atomic_set(&ccw_device_init_count, 0);
146 ccw_device_work = create_singlethread_workqueue("cio");
147 if (!ccw_device_work)
148 return -ENOMEM; /* FIXME: better errno ? */
149 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
150 if (!ccw_device_notify_work) {
151 ret = -ENOMEM; /* FIXME: better errno ? */
154 slow_path_wq = create_singlethread_workqueue("kslowcrw");
156 ret = -ENOMEM; /* FIXME: better errno ? */
159 if ((ret = bus_register (&ccw_bus_type)))
162 if ((ret = driver_register(&io_subchannel_driver.drv)))
165 wait_event(ccw_device_init_wq,
166 atomic_read(&ccw_device_init_count) == 0);
167 flush_workqueue(ccw_device_work);
171 destroy_workqueue(ccw_device_work);
172 if (ccw_device_notify_work)
173 destroy_workqueue(ccw_device_notify_work);
175 destroy_workqueue(slow_path_wq);
180 cleanup_ccw_bus_type (void)
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);
188 subsys_initcall(init_ccw_bus_type);
189 module_exit(cleanup_ccw_bus_type);
191 /************************ device handling **************************/
194 * A ccw_device has some interfaces in sysfs in addition to the
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?
202 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
204 struct subchannel *sch = to_subchannel(dev);
205 struct ssd_info *ssd = &sch->ssd_info;
209 for (chp = 0; chp < 8; chp++)
210 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
212 ret += sprintf (buf+ret, "\n");
213 return min((ssize_t)PAGE_SIZE, ret);
217 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
219 struct subchannel *sch = to_subchannel(dev);
220 struct pmcw *pmcw = &sch->schib.pmcw;
222 return sprintf (buf, "%02x %02x %02x\n",
223 pmcw->pim, pmcw->pam, pmcw->pom);
227 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
229 struct ccw_device *cdev = to_ccwdev(dev);
230 struct ccw_device_id *id = &(cdev->id);
232 if (id->dev_type != 0)
233 return sprintf(buf, "%04x/%02x\n",
234 id->dev_type, id->dev_model);
236 return sprintf(buf, "n/a\n");
240 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
242 struct ccw_device *cdev = to_ccwdev(dev);
243 struct ccw_device_id *id = &(cdev->id);
245 return sprintf(buf, "%04x/%02x\n",
246 id->cu_type, id->cu_model);
250 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
256 ret = sprintf(buf, "ccw:t%04Xm%02X",
257 id->cu_type, id->cu_model);
258 if (id->dev_type != 0)
259 ret += sprintf(buf + ret, "dt%04Xdm%02X\n",
260 id->dev_type, id->dev_model);
262 ret += sprintf(buf + ret, "dtdm\n");
267 online_show (struct device *dev, struct device_attribute *attr, char *buf)
269 struct ccw_device *cdev = to_ccwdev(dev);
271 return sprintf(buf, cdev->online ? "1\n" : "0\n");
275 ccw_device_remove_disconnected(struct ccw_device *cdev)
277 struct subchannel *sch;
279 * Forced offline in disconnected state means
280 * 'throw away device'.
282 sch = to_subchannel(cdev->dev.parent);
283 device_unregister(&sch->dev);
284 /* Reset intparm to zeroes. */
285 sch->schib.pmcw.intparm = 0;
287 put_device(&sch->dev);
291 ccw_device_set_offline(struct ccw_device *cdev)
297 if (!cdev->online || !cdev->drv)
300 if (cdev->drv->set_offline) {
301 ret = cdev->drv->set_offline(cdev);
306 spin_lock_irq(cdev->ccwlock);
307 ret = ccw_device_offline(cdev);
308 if (ret == -ENODEV) {
309 if (cdev->private->state != DEV_STATE_NOT_OPER) {
310 cdev->private->state = DEV_STATE_OFFLINE;
311 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
313 spin_unlock_irq(cdev->ccwlock);
316 spin_unlock_irq(cdev->ccwlock);
318 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
320 pr_debug("ccw_device_offline returned %d, device %s\n",
321 ret, cdev->dev.bus_id);
328 ccw_device_set_online(struct ccw_device *cdev)
334 if (cdev->online || !cdev->drv)
337 spin_lock_irq(cdev->ccwlock);
338 ret = ccw_device_online(cdev);
339 spin_unlock_irq(cdev->ccwlock);
341 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
343 pr_debug("ccw_device_online returned %d, device %s\n",
344 ret, cdev->dev.bus_id);
347 if (cdev->private->state != DEV_STATE_ONLINE)
349 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
353 spin_lock_irq(cdev->ccwlock);
354 ret = ccw_device_offline(cdev);
355 spin_unlock_irq(cdev->ccwlock);
357 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
359 pr_debug("ccw_device_offline returned %d, device %s\n",
360 ret, cdev->dev.bus_id);
361 return (ret == 0) ? -ENODEV : ret;
365 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
367 struct ccw_device *cdev = to_ccwdev(dev);
371 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
374 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
375 atomic_set(&cdev->private->onoff, 0);
378 if (!strncmp(buf, "force\n", count)) {
383 i = simple_strtoul(buf, &tmp, 16);
386 /* Do device recognition, if needed. */
387 if (cdev->id.cu_type == 0) {
388 ret = ccw_device_recognition(cdev);
390 printk(KERN_WARNING"Couldn't start recognition "
391 "for device %s (ret=%d)\n",
392 cdev->dev.bus_id, ret);
395 wait_event(cdev->private->wait_q,
396 cdev->private->flags.recog_done);
398 if (cdev->drv && cdev->drv->set_online)
399 ccw_device_set_online(cdev);
401 if (cdev->private->state == DEV_STATE_DISCONNECTED)
402 ccw_device_remove_disconnected(cdev);
403 else if (cdev->drv && cdev->drv->set_offline)
404 ccw_device_set_offline(cdev);
406 if (force && cdev->private->state == DEV_STATE_BOXED) {
407 ret = ccw_device_stlck(cdev);
409 printk(KERN_WARNING"ccw_device_stlck for device %s "
410 "returned %d!\n", cdev->dev.bus_id, ret);
413 /* Do device recognition, if needed. */
414 if (cdev->id.cu_type == 0) {
415 cdev->private->state = DEV_STATE_NOT_OPER;
416 ret = ccw_device_recognition(cdev);
418 printk(KERN_WARNING"Couldn't start recognition "
419 "for device %s (ret=%d)\n",
420 cdev->dev.bus_id, ret);
423 wait_event(cdev->private->wait_q,
424 cdev->private->flags.recog_done);
426 if (cdev->drv && cdev->drv->set_online)
427 ccw_device_set_online(cdev);
431 module_put(cdev->drv->owner);
432 atomic_set(&cdev->private->onoff, 0);
437 available_show (struct device *dev, struct device_attribute *attr, char *buf)
439 struct ccw_device *cdev = to_ccwdev(dev);
440 struct subchannel *sch;
442 switch (cdev->private->state) {
443 case DEV_STATE_BOXED:
444 return sprintf(buf, "boxed\n");
445 case DEV_STATE_DISCONNECTED:
446 case DEV_STATE_DISCONNECTED_SENSE_ID:
447 case DEV_STATE_NOT_OPER:
448 sch = to_subchannel(dev->parent);
450 return sprintf(buf, "no path\n");
452 return sprintf(buf, "no device\n");
454 /* All other states considered fine. */
455 return sprintf(buf, "good\n");
459 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
460 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
461 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
462 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
463 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
464 static DEVICE_ATTR(online, 0644, online_show, online_store);
465 extern struct device_attribute dev_attr_cmb_enable;
466 static DEVICE_ATTR(availability, 0444, available_show, NULL);
468 static struct attribute * subch_attrs[] = {
469 &dev_attr_chpids.attr,
470 &dev_attr_pimpampom.attr,
474 static struct attribute_group subch_attr_group = {
475 .attrs = subch_attrs,
479 subchannel_add_files (struct device *dev)
481 return sysfs_create_group(&dev->kobj, &subch_attr_group);
484 static struct attribute * ccwdev_attrs[] = {
485 &dev_attr_devtype.attr,
486 &dev_attr_cutype.attr,
487 &dev_attr_modalias.attr,
488 &dev_attr_online.attr,
489 &dev_attr_cmb_enable.attr,
490 &dev_attr_availability.attr,
494 static struct attribute_group ccwdev_attr_group = {
495 .attrs = ccwdev_attrs,
499 device_add_files (struct device *dev)
501 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
505 device_remove_files(struct device *dev)
507 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
510 /* this is a simple abstraction for device_register that sets the
511 * correct bus type and adds the bus specific files */
513 ccw_device_register(struct ccw_device *cdev)
515 struct device *dev = &cdev->dev;
518 dev->bus = &ccw_bus_type;
520 if ((ret = device_add(dev)))
523 set_bit(1, &cdev->private->registered);
524 if ((ret = device_add_files(dev))) {
525 if (test_and_clear_bit(1, &cdev->private->registered))
534 struct ccw_device * sibling;
538 match_devno(struct device * dev, void * data)
540 struct match_data * d = (struct match_data *)data;
541 struct ccw_device * cdev;
543 cdev = to_ccwdev(dev);
544 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
545 (cdev->private->devno == d->devno) &&
546 (cdev->private->ssid == d->ssid) &&
547 (cdev != d->sibling)) {
548 cdev->private->state = DEV_STATE_NOT_OPER;
554 static struct ccw_device *
555 get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid,
556 struct ccw_device *sibling)
559 struct match_data data = {
565 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
567 return dev ? to_ccwdev(dev) : NULL;
571 ccw_device_add_changed(void *data)
574 struct ccw_device *cdev;
576 cdev = (struct ccw_device *)data;
577 if (device_add(&cdev->dev)) {
578 put_device(&cdev->dev);
581 set_bit(1, &cdev->private->registered);
582 if (device_add_files(&cdev->dev)) {
583 if (test_and_clear_bit(1, &cdev->private->registered))
584 device_unregister(&cdev->dev);
588 extern int css_get_ssd_info(struct subchannel *sch);
591 ccw_device_do_unreg_rereg(void *data)
593 struct ccw_device *cdev;
594 struct subchannel *sch;
597 cdev = (struct ccw_device *)data;
598 sch = to_subchannel(cdev->dev.parent);
599 if (cdev->private->devno != sch->schib.pmcw.dev) {
601 * The device number has changed. This is usually only when
602 * a device has been detached under VM and then re-appeared
603 * on another subchannel because of a different attachment
604 * order than before. Ideally, we should should just switch
605 * subchannels, but unfortunately, this is not possible with
606 * the current implementation.
607 * Instead, we search for the old subchannel for this device
608 * number and deregister so there are no collisions with the
609 * newly registered ccw_device.
610 * FIXME: Find another solution so the block layer doesn't
611 * get possibly sick...
613 struct ccw_device *other_cdev;
616 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
617 sch->schid.ssid, cdev);
619 struct subchannel *other_sch;
621 other_sch = to_subchannel(other_cdev->dev.parent);
622 if (get_device(&other_sch->dev)) {
623 stsch(other_sch->schid, &other_sch->schib);
624 if (other_sch->schib.pmcw.dnv) {
625 other_sch->schib.pmcw.intparm = 0;
626 cio_modify(other_sch);
628 device_unregister(&other_sch->dev);
631 /* Update ssd info here. */
632 css_get_ssd_info(sch);
633 cdev->private->devno = sch->schib.pmcw.dev;
636 device_remove_files(&cdev->dev);
637 if (test_and_clear_bit(1, &cdev->private->registered))
638 device_del(&cdev->dev);
640 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
641 sch->schid.ssid, sch->schib.pmcw.dev);
642 PREPARE_WORK(&cdev->private->kick_work,
643 ccw_device_add_changed, (void *)cdev);
644 queue_work(ccw_device_work, &cdev->private->kick_work);
648 ccw_device_release(struct device *dev)
650 struct ccw_device *cdev;
652 cdev = to_ccwdev(dev);
653 kfree(cdev->private);
658 * Register recognized device.
661 io_subchannel_register(void *data)
663 struct ccw_device *cdev;
664 struct subchannel *sch;
668 cdev = (struct ccw_device *) data;
669 sch = to_subchannel(cdev->dev.parent);
671 if (klist_node_attached(&cdev->dev.knode_parent)) {
672 bus_rescan_devices(&ccw_bus_type);
675 /* make it known to the system */
676 ret = ccw_device_register(cdev);
678 printk (KERN_WARNING "%s: could not register %s\n",
679 __func__, cdev->dev.bus_id);
680 put_device(&cdev->dev);
681 spin_lock_irqsave(&sch->lock, flags);
682 sch->dev.driver_data = NULL;
683 spin_unlock_irqrestore(&sch->lock, flags);
684 kfree (cdev->private);
686 put_device(&sch->dev);
687 if (atomic_dec_and_test(&ccw_device_init_count))
688 wake_up(&ccw_device_init_wq);
692 ret = subchannel_add_files(cdev->dev.parent);
694 printk(KERN_WARNING "%s: could not add attributes to %s\n",
695 __func__, sch->dev.bus_id);
696 put_device(&cdev->dev);
698 cdev->private->flags.recog_done = 1;
699 put_device(&sch->dev);
700 wake_up(&cdev->private->wait_q);
701 if (atomic_dec_and_test(&ccw_device_init_count))
702 wake_up(&ccw_device_init_wq);
706 ccw_device_call_sch_unregister(void *data)
708 struct ccw_device *cdev = data;
709 struct subchannel *sch;
711 sch = to_subchannel(cdev->dev.parent);
712 device_unregister(&sch->dev);
713 /* Reset intparm to zeroes. */
714 sch->schib.pmcw.intparm = 0;
716 put_device(&cdev->dev);
717 put_device(&sch->dev);
721 * subchannel recognition done. Called from the state machine.
724 io_subchannel_recog_done(struct ccw_device *cdev)
726 struct subchannel *sch;
728 if (css_init_done == 0) {
729 cdev->private->flags.recog_done = 1;
732 switch (cdev->private->state) {
733 case DEV_STATE_NOT_OPER:
734 cdev->private->flags.recog_done = 1;
735 /* Remove device found not operational. */
736 if (!get_device(&cdev->dev))
738 sch = to_subchannel(cdev->dev.parent);
739 PREPARE_WORK(&cdev->private->kick_work,
740 ccw_device_call_sch_unregister, (void *) cdev);
741 queue_work(slow_path_wq, &cdev->private->kick_work);
742 if (atomic_dec_and_test(&ccw_device_init_count))
743 wake_up(&ccw_device_init_wq);
745 case DEV_STATE_BOXED:
746 /* Device did not respond in time. */
747 case DEV_STATE_OFFLINE:
749 * We can't register the device in interrupt context so
750 * we schedule a work item.
752 if (!get_device(&cdev->dev))
754 PREPARE_WORK(&cdev->private->kick_work,
755 io_subchannel_register, (void *) cdev);
756 queue_work(slow_path_wq, &cdev->private->kick_work);
762 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
765 struct ccw_device_private *priv;
767 sch->dev.driver_data = cdev;
768 sch->driver = &io_subchannel_driver;
769 cdev->ccwlock = &sch->lock;
771 /* Init private data. */
772 priv = cdev->private;
773 priv->devno = sch->schib.pmcw.dev;
774 priv->ssid = sch->schid.ssid;
775 priv->sch_no = sch->schid.sch_no;
776 priv->state = DEV_STATE_NOT_OPER;
777 INIT_LIST_HEAD(&priv->cmb_list);
778 init_waitqueue_head(&priv->wait_q);
779 init_timer(&priv->timer);
781 /* Set an initial name for the device. */
782 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
783 sch->schid.ssid, sch->schib.pmcw.dev);
785 /* Increase counter of devices currently in recognition. */
786 atomic_inc(&ccw_device_init_count);
788 /* Start async. device sensing. */
789 spin_lock_irq(&sch->lock);
790 rc = ccw_device_recognition(cdev);
791 spin_unlock_irq(&sch->lock);
793 if (atomic_dec_and_test(&ccw_device_init_count))
794 wake_up(&ccw_device_init_wq);
800 io_subchannel_probe (struct subchannel *sch)
802 struct ccw_device *cdev;
806 if (sch->dev.driver_data) {
808 * This subchannel already has an associated ccw_device.
809 * Register it and exit. This happens for all early
810 * device, e.g. the console.
812 cdev = sch->dev.driver_data;
813 device_initialize(&cdev->dev);
814 ccw_device_register(cdev);
815 subchannel_add_files(&sch->dev);
817 * Check if the device is already online. If it is
818 * the reference count needs to be corrected
819 * (see ccw_device_online and css_init_done for the
822 if (cdev->private->state != DEV_STATE_NOT_OPER &&
823 cdev->private->state != DEV_STATE_OFFLINE &&
824 cdev->private->state != DEV_STATE_BOXED)
825 get_device(&cdev->dev);
828 cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
831 cdev->private = kzalloc(sizeof(struct ccw_device_private),
832 GFP_KERNEL | GFP_DMA);
833 if (!cdev->private) {
837 atomic_set(&cdev->private->onoff, 0);
838 cdev->dev = (struct device) {
840 .release = ccw_device_release,
842 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
843 /* Do first half of device_register. */
844 device_initialize(&cdev->dev);
846 if (!get_device(&sch->dev)) {
847 if (cdev->dev.release)
848 cdev->dev.release(&cdev->dev);
852 rc = io_subchannel_recog(cdev, sch);
854 spin_lock_irqsave(&sch->lock, flags);
855 sch->dev.driver_data = NULL;
856 spin_unlock_irqrestore(&sch->lock, flags);
857 if (cdev->dev.release)
858 cdev->dev.release(&cdev->dev);
865 ccw_device_unregister(void *data)
867 struct ccw_device *cdev;
869 cdev = (struct ccw_device *)data;
870 if (test_and_clear_bit(1, &cdev->private->registered))
871 device_unregister(&cdev->dev);
872 put_device(&cdev->dev);
876 io_subchannel_remove (struct subchannel *sch)
878 struct ccw_device *cdev;
881 if (!sch->dev.driver_data)
883 cdev = sch->dev.driver_data;
884 /* Set ccw device to not operational and drop reference. */
885 spin_lock_irqsave(cdev->ccwlock, flags);
886 sch->dev.driver_data = NULL;
887 cdev->private->state = DEV_STATE_NOT_OPER;
888 spin_unlock_irqrestore(cdev->ccwlock, flags);
890 * Put unregistration on workqueue to avoid livelocks on the css bus
893 if (get_device(&cdev->dev)) {
894 PREPARE_WORK(&cdev->private->kick_work,
895 ccw_device_unregister, (void *) cdev);
896 queue_work(ccw_device_work, &cdev->private->kick_work);
902 io_subchannel_notify(struct device *dev, int event)
904 struct ccw_device *cdev;
906 cdev = dev->driver_data;
913 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
917 io_subchannel_verify(struct device *dev)
919 struct ccw_device *cdev;
921 cdev = dev->driver_data;
923 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
927 io_subchannel_ioterm(struct device *dev)
929 struct ccw_device *cdev;
931 cdev = dev->driver_data;
934 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
936 cdev->handler(cdev, cdev->private->intparm,
941 io_subchannel_shutdown(struct subchannel *sch)
943 struct ccw_device *cdev;
946 cdev = sch->dev.driver_data;
948 if (cio_is_console(sch->schid))
950 if (!sch->schib.pmcw.ena)
953 ret = cio_disable_subchannel(sch);
955 /* Subchannel is disabled, we're done. */
957 cdev->private->state = DEV_STATE_QUIESCE;
959 cdev->handler(cdev, cdev->private->intparm,
961 ret = ccw_device_cancel_halt_clear(cdev);
963 ccw_device_set_timeout(cdev, HZ/10);
964 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
966 cio_disable_subchannel(sch);
969 #ifdef CONFIG_CCW_CONSOLE
970 static struct ccw_device console_cdev;
971 static struct ccw_device_private console_private;
972 static int console_cdev_in_use;
975 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
979 /* Initialize the ccw_device structure. */
980 cdev->dev = (struct device) {
983 rc = io_subchannel_recog(cdev, sch);
987 /* Now wait for the async. recognition to come to an end. */
988 spin_lock_irq(cdev->ccwlock);
989 while (!dev_fsm_final_state(cdev))
992 if (cdev->private->state != DEV_STATE_OFFLINE)
994 ccw_device_online(cdev);
995 while (!dev_fsm_final_state(cdev))
997 if (cdev->private->state != DEV_STATE_ONLINE)
1001 spin_unlock_irq(cdev->ccwlock);
1006 ccw_device_probe_console(void)
1008 struct subchannel *sch;
1011 if (xchg(&console_cdev_in_use, 1) != 0)
1012 return ERR_PTR(-EBUSY);
1013 sch = cio_probe_console();
1015 console_cdev_in_use = 0;
1016 return (void *) sch;
1018 memset(&console_cdev, 0, sizeof(struct ccw_device));
1019 memset(&console_private, 0, sizeof(struct ccw_device_private));
1020 console_cdev.private = &console_private;
1021 ret = ccw_device_console_enable(&console_cdev, sch);
1023 cio_release_console();
1024 console_cdev_in_use = 0;
1025 return ERR_PTR(ret);
1027 console_cdev.online = 1;
1028 return &console_cdev;
1033 * get ccw_device matching the busid, but only if owned by cdrv
1036 __ccwdev_check_busid(struct device *dev, void *id)
1040 bus_id = (char *)id;
1042 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1047 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1050 struct device_driver *drv;
1052 drv = get_driver(&cdrv->driver);
1056 dev = driver_find_device(drv, NULL, (void *)bus_id,
1057 __ccwdev_check_busid);
1060 return dev ? to_ccwdev(dev) : 0;
1063 /************************** device driver handling ************************/
1065 /* This is the implementation of the ccw_driver class. The probe, remove
1066 * and release methods are initially very similar to the device_driver
1067 * implementations, with the difference that they have ccw_device
1070 * A ccw driver also contains the information that is needed for
1074 ccw_device_probe (struct device *dev)
1076 struct ccw_device *cdev = to_ccwdev(dev);
1077 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1080 cdev->drv = cdrv; /* to let the driver call _set_online */
1082 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1093 ccw_device_remove (struct device *dev)
1095 struct ccw_device *cdev = to_ccwdev(dev);
1096 struct ccw_driver *cdrv = cdev->drv;
1099 pr_debug("removing device %s\n", cdev->dev.bus_id);
1104 spin_lock_irq(cdev->ccwlock);
1105 ret = ccw_device_offline(cdev);
1106 spin_unlock_irq(cdev->ccwlock);
1108 wait_event(cdev->private->wait_q,
1109 dev_fsm_final_state(cdev));
1111 //FIXME: we can't fail!
1112 pr_debug("ccw_device_offline returned %d, device %s\n",
1113 ret, cdev->dev.bus_id);
1115 ccw_device_set_timeout(cdev, 0);
1120 struct bus_type ccw_bus_type = {
1122 .match = ccw_bus_match,
1123 .uevent = ccw_uevent,
1124 .probe = ccw_device_probe,
1125 .remove = ccw_device_remove,
1129 ccw_driver_register (struct ccw_driver *cdriver)
1131 struct device_driver *drv = &cdriver->driver;
1133 drv->bus = &ccw_bus_type;
1134 drv->name = cdriver->name;
1136 return driver_register(drv);
1140 ccw_driver_unregister (struct ccw_driver *cdriver)
1142 driver_unregister(&cdriver->driver);
1145 /* Helper func for qdio. */
1146 struct subchannel_id
1147 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1149 struct subchannel *sch;
1151 sch = to_subchannel(cdev->dev.parent);
1155 MODULE_LICENSE("GPL");
1156 EXPORT_SYMBOL(ccw_device_set_online);
1157 EXPORT_SYMBOL(ccw_device_set_offline);
1158 EXPORT_SYMBOL(ccw_driver_register);
1159 EXPORT_SYMBOL(ccw_driver_unregister);
1160 EXPORT_SYMBOL(get_ccwdev_by_busid);
1161 EXPORT_SYMBOL(ccw_bus_type);
1162 EXPORT_SYMBOL(ccw_device_work);
1163 EXPORT_SYMBOL(ccw_device_notify_work);
1164 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);