2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/device.h>
22 #include <linux/workqueue.h>
23 #include <linux/timer.h>
25 #include <asm/ccwdev.h>
27 #include <asm/param.h> /* HZ */
33 #include "cio_debug.h"
38 #include "blacklist.h"
40 static struct timer_list recovery_timer;
41 static DEFINE_SPINLOCK(recovery_lock);
42 static int recovery_phase;
43 static const unsigned long recovery_delay[] = { 3, 30, 300 };
45 /******************* bus type handling ***********************/
47 /* The Linux driver model distinguishes between a bus type and
48 * the bus itself. Of course we only have one channel
49 * subsystem driver and one channel system per machine, but
50 * we still use the abstraction. T.R. says it's a good idea. */
52 ccw_bus_match (struct device * dev, struct device_driver * drv)
54 struct ccw_device *cdev = to_ccwdev(dev);
55 struct ccw_driver *cdrv = to_ccwdrv(drv);
56 const struct ccw_device_id *ids = cdrv->ids, *found;
61 found = ccw_device_id_match(ids, &cdev->id);
65 cdev->id.driver_info = found->driver_info;
70 /* Store modalias string delimited by prefix/suffix string into buffer with
71 * specified size. Return length of resulting string (excluding trailing '\0')
72 * even if string doesn't fit buffer (snprintf semantics). */
73 static int snprint_alias(char *buf, size_t size,
74 struct ccw_device_id *id, const char *suffix)
78 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
84 if (id->dev_type != 0)
85 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
86 id->dev_model, suffix);
88 len += snprintf(buf, size, "dtdm%s", suffix);
93 /* Set up environment variables for ccw device uevent. Return 0 on success,
94 * non-zero otherwise. */
95 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
97 struct ccw_device *cdev = to_ccwdev(dev);
98 struct ccw_device_id *id = &(cdev->id);
100 char modalias_buf[30];
103 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
108 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
112 /* The next two can be zero, that's ok for us */
114 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
119 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
124 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
125 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
129 struct bus_type ccw_bus_type;
131 static void io_subchannel_irq(struct subchannel *);
132 static int io_subchannel_probe(struct subchannel *);
133 static int io_subchannel_remove(struct subchannel *);
134 static void io_subchannel_shutdown(struct subchannel *);
135 static int io_subchannel_sch_event(struct subchannel *, int);
136 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
138 static void recovery_func(unsigned long data);
139 struct workqueue_struct *ccw_device_work;
140 wait_queue_head_t ccw_device_init_wq;
141 atomic_t ccw_device_init_count;
143 static struct css_device_id io_subchannel_ids[] = {
144 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
145 { /* end of list */ },
147 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
149 static int io_subchannel_prepare(struct subchannel *sch)
151 struct ccw_device *cdev;
153 * Don't allow suspend while a ccw device registration
154 * is still outstanding.
156 cdev = sch_get_cdev(sch);
157 if (cdev && !device_is_registered(&cdev->dev))
162 static void io_subchannel_settle(void)
164 wait_event(ccw_device_init_wq,
165 atomic_read(&ccw_device_init_count) == 0);
166 flush_workqueue(ccw_device_work);
169 static struct css_driver io_subchannel_driver = {
170 .owner = THIS_MODULE,
171 .subchannel_type = io_subchannel_ids,
172 .name = "io_subchannel",
173 .irq = io_subchannel_irq,
174 .sch_event = io_subchannel_sch_event,
175 .chp_event = io_subchannel_chp_event,
176 .probe = io_subchannel_probe,
177 .remove = io_subchannel_remove,
178 .shutdown = io_subchannel_shutdown,
179 .prepare = io_subchannel_prepare,
180 .settle = io_subchannel_settle,
183 int __init io_subchannel_init(void)
187 init_waitqueue_head(&ccw_device_init_wq);
188 atomic_set(&ccw_device_init_count, 0);
189 setup_timer(&recovery_timer, recovery_func, 0);
191 ccw_device_work = create_singlethread_workqueue("cio");
192 if (!ccw_device_work)
194 slow_path_wq = create_singlethread_workqueue("kslowcrw");
199 if ((ret = bus_register (&ccw_bus_type)))
202 ret = css_driver_register(&io_subchannel_driver);
209 destroy_workqueue(ccw_device_work);
211 destroy_workqueue(slow_path_wq);
216 /************************ device handling **************************/
219 * A ccw_device has some interfaces in sysfs in addition to the
221 * The following entries are designed to export the information which
222 * resided in 2.4 in /proc/subchannels. Subchannel and device number
223 * are obvious, so they don't have an entry :)
224 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
227 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
229 struct subchannel *sch = to_subchannel(dev);
230 struct chsc_ssd_info *ssd = &sch->ssd_info;
235 for (chp = 0; chp < 8; chp++) {
237 if (ssd->path_mask & mask)
238 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
240 ret += sprintf(buf + ret, "00 ");
242 ret += sprintf (buf+ret, "\n");
243 return min((ssize_t)PAGE_SIZE, ret);
247 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
249 struct subchannel *sch = to_subchannel(dev);
250 struct pmcw *pmcw = &sch->schib.pmcw;
252 return sprintf (buf, "%02x %02x %02x\n",
253 pmcw->pim, pmcw->pam, pmcw->pom);
257 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
259 struct ccw_device *cdev = to_ccwdev(dev);
260 struct ccw_device_id *id = &(cdev->id);
262 if (id->dev_type != 0)
263 return sprintf(buf, "%04x/%02x\n",
264 id->dev_type, id->dev_model);
266 return sprintf(buf, "n/a\n");
270 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
275 return sprintf(buf, "%04x/%02x\n",
276 id->cu_type, id->cu_model);
280 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
282 struct ccw_device *cdev = to_ccwdev(dev);
283 struct ccw_device_id *id = &(cdev->id);
286 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
288 return len > PAGE_SIZE ? PAGE_SIZE : len;
292 online_show (struct device *dev, struct device_attribute *attr, char *buf)
294 struct ccw_device *cdev = to_ccwdev(dev);
296 return sprintf(buf, cdev->online ? "1\n" : "0\n");
299 int ccw_device_is_orphan(struct ccw_device *cdev)
301 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
304 static void ccw_device_unregister(struct ccw_device *cdev)
306 if (test_and_clear_bit(1, &cdev->private->registered)) {
307 device_del(&cdev->dev);
308 /* Release reference from device_initialize(). */
309 put_device(&cdev->dev);
314 * ccw_device_set_offline() - disable a ccw device for I/O
315 * @cdev: target ccw device
317 * This function calls the driver's set_offline() function for @cdev, if
318 * given, and then disables @cdev.
320 * %0 on success and a negative error value on failure.
322 * enabled, ccw device lock not held
324 int ccw_device_set_offline(struct ccw_device *cdev)
330 if (!cdev->online || !cdev->drv)
333 if (cdev->drv->set_offline) {
334 ret = cdev->drv->set_offline(cdev);
339 spin_lock_irq(cdev->ccwlock);
340 /* Wait until a final state or DISCONNECTED is reached */
341 while (!dev_fsm_final_state(cdev) &&
342 cdev->private->state != DEV_STATE_DISCONNECTED) {
343 spin_unlock_irq(cdev->ccwlock);
344 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
345 cdev->private->state == DEV_STATE_DISCONNECTED));
346 spin_lock_irq(cdev->ccwlock);
348 ret = ccw_device_offline(cdev);
351 spin_unlock_irq(cdev->ccwlock);
352 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
353 cdev->private->state == DEV_STATE_DISCONNECTED));
354 /* Inform the user if set offline failed. */
355 if (cdev->private->state == DEV_STATE_BOXED) {
356 pr_warning("%s: The device entered boxed state while "
357 "being set offline\n", dev_name(&cdev->dev));
358 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
359 pr_warning("%s: The device stopped operating while "
360 "being set offline\n", dev_name(&cdev->dev));
362 /* Give up reference from ccw_device_set_online(). */
363 put_device(&cdev->dev);
367 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
368 ret, cdev->private->dev_id.ssid,
369 cdev->private->dev_id.devno);
370 cdev->private->state = DEV_STATE_OFFLINE;
371 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
372 spin_unlock_irq(cdev->ccwlock);
373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev->dev);
379 * ccw_device_set_online() - enable a ccw device for I/O
380 * @cdev: target ccw device
382 * This function first enables @cdev and then calls the driver's set_online()
383 * function for @cdev, if given. If set_online() returns an error, @cdev is
386 * %0 on success and a negative error value on failure.
388 * enabled, ccw device lock not held
390 int ccw_device_set_online(struct ccw_device *cdev)
397 if (cdev->online || !cdev->drv)
399 /* Hold on to an extra reference while device is online. */
400 if (!get_device(&cdev->dev))
403 spin_lock_irq(cdev->ccwlock);
404 ret = ccw_device_online(cdev);
405 spin_unlock_irq(cdev->ccwlock);
407 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
409 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
410 "device 0.%x.%04x\n",
411 ret, cdev->private->dev_id.ssid,
412 cdev->private->dev_id.devno);
413 /* Give up online reference since onlining failed. */
414 put_device(&cdev->dev);
417 spin_lock_irq(cdev->ccwlock);
418 /* Check if online processing was successful */
419 if ((cdev->private->state != DEV_STATE_ONLINE) &&
420 (cdev->private->state != DEV_STATE_W4SENSE)) {
421 spin_unlock_irq(cdev->ccwlock);
422 /* Inform the user that set online failed. */
423 if (cdev->private->state == DEV_STATE_BOXED) {
424 pr_warning("%s: Setting the device online failed "
425 "because it is boxed\n",
426 dev_name(&cdev->dev));
427 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
428 pr_warning("%s: Setting the device online failed "
429 "because it is not operational\n",
430 dev_name(&cdev->dev));
432 /* Give up online reference since onlining failed. */
433 put_device(&cdev->dev);
436 spin_unlock_irq(cdev->ccwlock);
437 if (cdev->drv->set_online)
438 ret = cdev->drv->set_online(cdev);
445 spin_lock_irq(cdev->ccwlock);
446 /* Wait until a final state or DISCONNECTED is reached */
447 while (!dev_fsm_final_state(cdev) &&
448 cdev->private->state != DEV_STATE_DISCONNECTED) {
449 spin_unlock_irq(cdev->ccwlock);
450 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
451 cdev->private->state == DEV_STATE_DISCONNECTED));
452 spin_lock_irq(cdev->ccwlock);
454 ret2 = ccw_device_offline(cdev);
457 spin_unlock_irq(cdev->ccwlock);
458 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
459 cdev->private->state == DEV_STATE_DISCONNECTED));
460 /* Give up online reference since onlining failed. */
461 put_device(&cdev->dev);
465 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
466 "device 0.%x.%04x\n",
467 ret2, cdev->private->dev_id.ssid,
468 cdev->private->dev_id.devno);
469 cdev->private->state = DEV_STATE_OFFLINE;
470 spin_unlock_irq(cdev->ccwlock);
471 /* Give up online reference since onlining failed. */
472 put_device(&cdev->dev);
476 static int online_store_handle_offline(struct ccw_device *cdev)
478 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
479 spin_lock_irq(cdev->ccwlock);
480 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
481 spin_unlock_irq(cdev->ccwlock);
482 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
483 return ccw_device_set_offline(cdev);
487 static int online_store_recog_and_online(struct ccw_device *cdev)
489 /* Do device recognition, if needed. */
490 if (cdev->private->state == DEV_STATE_BOXED) {
491 spin_lock_irq(cdev->ccwlock);
492 ccw_device_recognition(cdev);
493 spin_unlock_irq(cdev->ccwlock);
494 wait_event(cdev->private->wait_q,
495 cdev->private->flags.recog_done);
496 if (cdev->private->state != DEV_STATE_OFFLINE)
497 /* recognition failed */
500 if (cdev->drv && cdev->drv->set_online)
501 ccw_device_set_online(cdev);
505 static int online_store_handle_online(struct ccw_device *cdev, int force)
509 ret = online_store_recog_and_online(cdev);
512 if (force && cdev->private->state == DEV_STATE_BOXED) {
513 ret = ccw_device_stlck(cdev);
516 if (cdev->id.cu_type == 0)
517 cdev->private->state = DEV_STATE_NOT_OPER;
518 ret = online_store_recog_and_online(cdev);
525 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
528 struct ccw_device *cdev = to_ccwdev(dev);
532 if (!dev_fsm_final_state(cdev) &&
533 cdev->private->state != DEV_STATE_DISCONNECTED)
535 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
538 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
539 atomic_set(&cdev->private->onoff, 0);
542 if (!strncmp(buf, "force\n", count)) {
548 ret = strict_strtoul(buf, 16, &i);
554 ret = online_store_handle_offline(cdev);
557 ret = online_store_handle_online(cdev, force);
564 module_put(cdev->drv->owner);
565 atomic_set(&cdev->private->onoff, 0);
566 return (ret < 0) ? ret : count;
570 available_show (struct device *dev, struct device_attribute *attr, char *buf)
572 struct ccw_device *cdev = to_ccwdev(dev);
573 struct subchannel *sch;
575 if (ccw_device_is_orphan(cdev))
576 return sprintf(buf, "no device\n");
577 switch (cdev->private->state) {
578 case DEV_STATE_BOXED:
579 return sprintf(buf, "boxed\n");
580 case DEV_STATE_DISCONNECTED:
581 case DEV_STATE_DISCONNECTED_SENSE_ID:
582 case DEV_STATE_NOT_OPER:
583 sch = to_subchannel(dev->parent);
585 return sprintf(buf, "no path\n");
587 return sprintf(buf, "no device\n");
589 /* All other states considered fine. */
590 return sprintf(buf, "good\n");
594 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
595 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
596 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
597 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
598 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
599 static DEVICE_ATTR(online, 0644, online_show, online_store);
600 static DEVICE_ATTR(availability, 0444, available_show, NULL);
602 static struct attribute *io_subchannel_attrs[] = {
603 &dev_attr_chpids.attr,
604 &dev_attr_pimpampom.attr,
608 static struct attribute_group io_subchannel_attr_group = {
609 .attrs = io_subchannel_attrs,
612 static struct attribute * ccwdev_attrs[] = {
613 &dev_attr_devtype.attr,
614 &dev_attr_cutype.attr,
615 &dev_attr_modalias.attr,
616 &dev_attr_online.attr,
617 &dev_attr_cmb_enable.attr,
618 &dev_attr_availability.attr,
622 static struct attribute_group ccwdev_attr_group = {
623 .attrs = ccwdev_attrs,
626 static const struct attribute_group *ccwdev_attr_groups[] = {
631 /* this is a simple abstraction for device_register that sets the
632 * correct bus type and adds the bus specific files */
633 static int ccw_device_register(struct ccw_device *cdev)
635 struct device *dev = &cdev->dev;
638 dev->bus = &ccw_bus_type;
639 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
640 cdev->private->dev_id.devno);
643 ret = device_add(dev);
647 set_bit(1, &cdev->private->registered);
651 static int match_dev_id(struct device *dev, void *data)
653 struct ccw_device *cdev = to_ccwdev(dev);
654 struct ccw_dev_id *dev_id = data;
656 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
659 static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
663 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
665 return dev ? to_ccwdev(dev) : NULL;
668 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
672 if (test_bit(1, &cdev->private->registered)) {
673 device_release_driver(&cdev->dev);
674 ret = device_attach(&cdev->dev);
675 WARN_ON(ret == -ENODEV);
680 ccw_device_release(struct device *dev)
682 struct ccw_device *cdev;
684 cdev = to_ccwdev(dev);
685 /* Release reference of parent subchannel. */
686 put_device(cdev->dev.parent);
687 kfree(cdev->private);
691 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
693 struct ccw_device *cdev;
695 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
697 cdev->private = kzalloc(sizeof(struct ccw_device_private),
698 GFP_KERNEL | GFP_DMA);
703 return ERR_PTR(-ENOMEM);
706 static void ccw_device_todo(struct work_struct *work);
708 static int io_subchannel_initialize_dev(struct subchannel *sch,
709 struct ccw_device *cdev)
711 cdev->private->cdev = cdev;
712 atomic_set(&cdev->private->onoff, 0);
713 cdev->dev.parent = &sch->dev;
714 cdev->dev.release = ccw_device_release;
715 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
716 cdev->dev.groups = ccwdev_attr_groups;
717 /* Do first half of device_register. */
718 device_initialize(&cdev->dev);
719 if (!get_device(&sch->dev)) {
720 /* Release reference from device_initialize(). */
721 put_device(&cdev->dev);
727 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
729 struct ccw_device *cdev;
732 cdev = io_subchannel_allocate_dev(sch);
734 ret = io_subchannel_initialize_dev(sch, cdev);
741 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
743 static void sch_create_and_recog_new_device(struct subchannel *sch)
745 struct ccw_device *cdev;
747 /* Need to allocate a new ccw device. */
748 cdev = io_subchannel_create_ccwdev(sch);
750 /* OK, we did everything we could... */
751 css_sch_device_unregister(sch);
754 /* Start recognition for the new ccw device. */
755 io_subchannel_recog(cdev, sch);
759 * Register recognized device.
761 static void io_subchannel_register(struct ccw_device *cdev)
763 struct subchannel *sch;
767 sch = to_subchannel(cdev->dev.parent);
769 * Check if subchannel is still registered. It may have become
770 * unregistered if a machine check hit us after finishing
771 * device recognition but before the register work could be
774 if (!device_is_registered(&sch->dev))
776 css_update_ssd_info(sch);
778 * io_subchannel_register() will also be called after device
779 * recognition has been done for a boxed device (which will already
780 * be registered). We need to reprobe since we may now have sense id
783 if (device_is_registered(&cdev->dev)) {
785 ret = device_reprobe(&cdev->dev);
787 /* We can't do much here. */
788 CIO_MSG_EVENT(0, "device_reprobe() returned"
789 " %d for 0.%x.%04x\n", ret,
790 cdev->private->dev_id.ssid,
791 cdev->private->dev_id.devno);
796 * Now we know this subchannel will stay, we can throw
797 * our delayed uevent.
799 dev_set_uevent_suppress(&sch->dev, 0);
800 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
801 /* make it known to the system */
802 ret = ccw_device_register(cdev);
804 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
805 cdev->private->dev_id.ssid,
806 cdev->private->dev_id.devno, ret);
807 spin_lock_irqsave(sch->lock, flags);
808 sch_set_cdev(sch, NULL);
809 spin_unlock_irqrestore(sch->lock, flags);
810 /* Release initial device reference. */
811 put_device(&cdev->dev);
815 cdev->private->flags.recog_done = 1;
816 wake_up(&cdev->private->wait_q);
818 if (atomic_dec_and_test(&ccw_device_init_count))
819 wake_up(&ccw_device_init_wq);
822 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
824 struct subchannel *sch;
826 /* Get subchannel reference for local processing. */
827 if (!get_device(cdev->dev.parent))
829 sch = to_subchannel(cdev->dev.parent);
830 css_sch_device_unregister(sch);
831 /* Release subchannel reference for local processing. */
832 put_device(&sch->dev);
836 * subchannel recognition done. Called from the state machine.
839 io_subchannel_recog_done(struct ccw_device *cdev)
841 if (css_init_done == 0) {
842 cdev->private->flags.recog_done = 1;
845 switch (cdev->private->state) {
846 case DEV_STATE_BOXED:
847 /* Device did not respond in time. */
848 case DEV_STATE_NOT_OPER:
849 cdev->private->flags.recog_done = 1;
850 /* Remove device found not operational. */
851 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
852 if (atomic_dec_and_test(&ccw_device_init_count))
853 wake_up(&ccw_device_init_wq);
855 case DEV_STATE_OFFLINE:
857 * We can't register the device in interrupt context so
858 * we schedule a work item.
860 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
865 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
867 struct ccw_device_private *priv;
869 cdev->ccwlock = sch->lock;
871 /* Init private data. */
872 priv = cdev->private;
873 priv->dev_id.devno = sch->schib.pmcw.dev;
874 priv->dev_id.ssid = sch->schid.ssid;
875 priv->schid = sch->schid;
876 priv->state = DEV_STATE_NOT_OPER;
877 INIT_LIST_HEAD(&priv->cmb_list);
878 init_waitqueue_head(&priv->wait_q);
879 init_timer(&priv->timer);
881 /* Increase counter of devices currently in recognition. */
882 atomic_inc(&ccw_device_init_count);
884 /* Start async. device sensing. */
885 spin_lock_irq(sch->lock);
886 sch_set_cdev(sch, cdev);
887 ccw_device_recognition(cdev);
888 spin_unlock_irq(sch->lock);
891 static int ccw_device_move_to_sch(struct ccw_device *cdev,
892 struct subchannel *sch)
894 struct subchannel *old_sch;
897 old_sch = to_subchannel(cdev->dev.parent);
898 /* Obtain child reference for new parent. */
899 if (!get_device(&sch->dev))
901 mutex_lock(&sch->reg_mutex);
902 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
903 mutex_unlock(&sch->reg_mutex);
905 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
906 cdev->private->dev_id.ssid,
907 cdev->private->dev_id.devno, sch->schid.ssid,
908 sch->schib.pmcw.dev, rc);
909 /* Release child reference for new parent. */
910 put_device(&sch->dev);
913 /* Clean up old subchannel. */
914 if (!sch_is_pseudo_sch(old_sch)) {
915 spin_lock_irq(old_sch->lock);
916 sch_set_cdev(old_sch, NULL);
917 cio_disable_subchannel(old_sch);
918 spin_unlock_irq(old_sch->lock);
919 css_schedule_eval(old_sch->schid);
921 /* Release child reference for old parent. */
922 put_device(&old_sch->dev);
923 /* Initialize new subchannel. */
924 spin_lock_irq(sch->lock);
925 cdev->private->schid = sch->schid;
926 cdev->ccwlock = sch->lock;
927 if (!sch_is_pseudo_sch(sch))
928 sch_set_cdev(sch, cdev);
929 spin_unlock_irq(sch->lock);
930 if (!sch_is_pseudo_sch(sch))
931 css_update_ssd_info(sch);
935 static int ccw_device_move_to_orph(struct ccw_device *cdev)
937 struct subchannel *sch = to_subchannel(cdev->dev.parent);
938 struct channel_subsystem *css = to_css(sch->dev.parent);
940 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
943 static void io_subchannel_irq(struct subchannel *sch)
945 struct ccw_device *cdev;
947 cdev = sch_get_cdev(sch);
949 CIO_TRACE_EVENT(6, "IRQ");
950 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
952 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
955 void io_subchannel_init_config(struct subchannel *sch)
957 memset(&sch->config, 0, sizeof(sch->config));
958 sch->config.csense = 1;
961 static void io_subchannel_init_fields(struct subchannel *sch)
963 if (cio_is_console(sch->schid))
966 sch->opm = chp_get_sch_opm(sch);
967 sch->lpm = sch->schib.pmcw.pam & sch->opm;
968 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
970 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
971 " - PIM = %02X, PAM = %02X, POM = %02X\n",
972 sch->schib.pmcw.dev, sch->schid.ssid,
973 sch->schid.sch_no, sch->schib.pmcw.pim,
974 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
976 io_subchannel_init_config(sch);
980 * Note: We always return 0 so that we bind to the device even on error.
981 * This is needed so that our remove function is called on unregister.
983 static int io_subchannel_probe(struct subchannel *sch)
985 struct ccw_device *cdev;
988 if (cio_is_console(sch->schid)) {
989 rc = sysfs_create_group(&sch->dev.kobj,
990 &io_subchannel_attr_group);
992 CIO_MSG_EVENT(0, "Failed to create io subchannel "
993 "attributes for subchannel "
994 "0.%x.%04x (rc=%d)\n",
995 sch->schid.ssid, sch->schid.sch_no, rc);
997 * The console subchannel already has an associated ccw_device.
998 * Throw the delayed uevent for the subchannel, register
999 * the ccw_device and exit.
1001 dev_set_uevent_suppress(&sch->dev, 0);
1002 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1003 cdev = sch_get_cdev(sch);
1004 cdev->dev.groups = ccwdev_attr_groups;
1005 device_initialize(&cdev->dev);
1006 ccw_device_register(cdev);
1008 * Check if the device is already online. If it is
1009 * the reference count needs to be corrected since we
1010 * didn't obtain a reference in ccw_device_set_online.
1012 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1013 cdev->private->state != DEV_STATE_OFFLINE &&
1014 cdev->private->state != DEV_STATE_BOXED)
1015 get_device(&cdev->dev);
1018 io_subchannel_init_fields(sch);
1019 rc = cio_commit_config(sch);
1022 rc = sysfs_create_group(&sch->dev.kobj,
1023 &io_subchannel_attr_group);
1026 /* Allocate I/O subchannel private data. */
1027 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1028 GFP_KERNEL | GFP_DMA);
1031 css_schedule_eval(sch->schid);
1035 spin_lock_irq(sch->lock);
1036 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1037 spin_unlock_irq(sch->lock);
1042 io_subchannel_remove (struct subchannel *sch)
1044 struct ccw_device *cdev;
1045 unsigned long flags;
1047 cdev = sch_get_cdev(sch);
1050 /* Set ccw device to not operational and drop reference. */
1051 spin_lock_irqsave(cdev->ccwlock, flags);
1052 sch_set_cdev(sch, NULL);
1053 cdev->private->state = DEV_STATE_NOT_OPER;
1054 spin_unlock_irqrestore(cdev->ccwlock, flags);
1055 ccw_device_unregister(cdev);
1057 kfree(sch->private);
1058 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1062 static void io_subchannel_verify(struct subchannel *sch)
1064 struct ccw_device *cdev;
1066 cdev = sch_get_cdev(sch);
1068 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1071 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1073 struct ccw_device *cdev;
1075 cdev = sch_get_cdev(sch);
1078 if (cio_update_schib(sch))
1080 /* Check for I/O on path. */
1081 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1083 if (cdev->private->state == DEV_STATE_ONLINE) {
1084 ccw_device_kill_io(cdev);
1090 /* Trigger path verification. */
1091 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1095 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1098 static int io_subchannel_chp_event(struct subchannel *sch,
1099 struct chp_link *link, int event)
1103 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1110 io_subchannel_terminate_path(sch, mask);
1115 io_subchannel_verify(sch);
1118 if (cio_update_schib(sch))
1120 io_subchannel_terminate_path(sch, mask);
1123 if (cio_update_schib(sch))
1125 sch->lpm |= mask & sch->opm;
1126 io_subchannel_verify(sch);
1133 io_subchannel_shutdown(struct subchannel *sch)
1135 struct ccw_device *cdev;
1138 cdev = sch_get_cdev(sch);
1140 if (cio_is_console(sch->schid))
1142 if (!sch->schib.pmcw.ena)
1143 /* Nothing to do. */
1145 ret = cio_disable_subchannel(sch);
1147 /* Subchannel is disabled, we're done. */
1149 cdev->private->state = DEV_STATE_QUIESCE;
1151 cdev->handler(cdev, cdev->private->intparm,
1153 ret = ccw_device_cancel_halt_clear(cdev);
1154 if (ret == -EBUSY) {
1155 ccw_device_set_timeout(cdev, HZ/10);
1156 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1158 cio_disable_subchannel(sch);
1161 static int device_is_disconnected(struct ccw_device *cdev)
1165 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1166 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1169 static int recovery_check(struct device *dev, void *data)
1171 struct ccw_device *cdev = to_ccwdev(dev);
1174 spin_lock_irq(cdev->ccwlock);
1175 switch (cdev->private->state) {
1176 case DEV_STATE_DISCONNECTED:
1177 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1178 cdev->private->dev_id.ssid,
1179 cdev->private->dev_id.devno);
1180 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1183 case DEV_STATE_DISCONNECTED_SENSE_ID:
1187 spin_unlock_irq(cdev->ccwlock);
1192 static void recovery_work_func(struct work_struct *unused)
1196 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1198 spin_lock_irq(&recovery_lock);
1199 if (!timer_pending(&recovery_timer)) {
1200 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1202 mod_timer(&recovery_timer, jiffies +
1203 recovery_delay[recovery_phase] * HZ);
1205 spin_unlock_irq(&recovery_lock);
1207 CIO_MSG_EVENT(4, "recovery: end\n");
1210 static DECLARE_WORK(recovery_work, recovery_work_func);
1212 static void recovery_func(unsigned long data)
1215 * We can't do our recovery in softirq context and it's not
1216 * performance critical, so we schedule it.
1218 schedule_work(&recovery_work);
1221 static void ccw_device_schedule_recovery(void)
1223 unsigned long flags;
1225 CIO_MSG_EVENT(4, "recovery: schedule\n");
1226 spin_lock_irqsave(&recovery_lock, flags);
1227 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1229 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1231 spin_unlock_irqrestore(&recovery_lock, flags);
1234 static int purge_fn(struct device *dev, void *data)
1236 struct ccw_device *cdev = to_ccwdev(dev);
1237 struct ccw_dev_id *id = &cdev->private->dev_id;
1239 spin_lock_irq(cdev->ccwlock);
1240 if (is_blacklisted(id->ssid, id->devno) &&
1241 (cdev->private->state == DEV_STATE_OFFLINE)) {
1242 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1244 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1246 spin_unlock_irq(cdev->ccwlock);
1247 /* Abort loop in case of pending signal. */
1248 if (signal_pending(current))
1255 * ccw_purge_blacklisted - purge unused, blacklisted devices
1257 * Unregister all ccw devices that are offline and on the blacklist.
1259 int ccw_purge_blacklisted(void)
1261 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1262 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1266 void ccw_device_set_disconnected(struct ccw_device *cdev)
1270 ccw_device_set_timeout(cdev, 0);
1271 cdev->private->flags.fake_irb = 0;
1272 cdev->private->state = DEV_STATE_DISCONNECTED;
1274 ccw_device_schedule_recovery();
1277 void ccw_device_set_notoper(struct ccw_device *cdev)
1279 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1281 CIO_TRACE_EVENT(2, "notoper");
1282 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1283 ccw_device_set_timeout(cdev, 0);
1284 cio_disable_subchannel(sch);
1285 cdev->private->state = DEV_STATE_NOT_OPER;
1288 enum io_sch_action {
1292 IO_SCH_UNREG_ATTACH,
1300 static enum io_sch_action sch_get_action(struct subchannel *sch)
1302 struct ccw_device *cdev;
1304 cdev = sch_get_cdev(sch);
1305 if (cio_update_schib(sch)) {
1306 /* Not operational. */
1308 return IO_SCH_UNREG;
1309 if (!ccw_device_notify(cdev, CIO_GONE))
1310 return IO_SCH_UNREG;
1311 return IO_SCH_ORPH_UNREG;
1315 return IO_SCH_ATTACH;
1316 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1317 if (!ccw_device_notify(cdev, CIO_GONE))
1318 return IO_SCH_UNREG_ATTACH;
1319 return IO_SCH_ORPH_ATTACH;
1321 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1322 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1323 return IO_SCH_UNREG;
1326 if (device_is_disconnected(cdev))
1327 return IO_SCH_REPROBE;
1329 return IO_SCH_VERIFY;
1334 * io_subchannel_sch_event - process subchannel event
1336 * @process: non-zero if function is called in process context
1338 * An unspecified event occurred for this subchannel. Adjust data according
1339 * to the current operational state of the subchannel and device. Return
1340 * zero when the event has been handled sufficiently or -EAGAIN when this
1341 * function should be called again in process context.
1343 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1345 unsigned long flags;
1346 struct ccw_device *cdev;
1347 struct ccw_dev_id dev_id;
1348 enum io_sch_action action;
1351 spin_lock_irqsave(sch->lock, flags);
1352 if (!device_is_registered(&sch->dev))
1354 if (work_pending(&sch->todo_work))
1356 cdev = sch_get_cdev(sch);
1357 if (cdev && work_pending(&cdev->private->todo_work))
1359 action = sch_get_action(sch);
1360 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1361 sch->schid.ssid, sch->schid.sch_no, process,
1363 /* Perform immediate actions while holding the lock. */
1365 case IO_SCH_REPROBE:
1366 /* Trigger device recognition. */
1367 ccw_device_trigger_reprobe(cdev);
1371 /* Trigger path verification. */
1372 io_subchannel_verify(sch);
1376 ccw_device_set_disconnected(cdev);
1379 case IO_SCH_ORPH_UNREG:
1380 case IO_SCH_ORPH_ATTACH:
1381 ccw_device_set_disconnected(cdev);
1383 case IO_SCH_UNREG_ATTACH:
1386 ccw_device_set_notoper(cdev);
1394 spin_unlock_irqrestore(sch->lock, flags);
1395 /* All other actions require process context. */
1398 /* Handle attached ccw device. */
1400 case IO_SCH_ORPH_UNREG:
1401 case IO_SCH_ORPH_ATTACH:
1402 /* Move ccw device to orphanage. */
1403 rc = ccw_device_move_to_orph(cdev);
1407 case IO_SCH_UNREG_ATTACH:
1408 /* Unregister ccw device. */
1409 ccw_device_unregister(cdev);
1414 /* Handle subchannel. */
1416 case IO_SCH_ORPH_UNREG:
1418 css_sch_device_unregister(sch);
1420 case IO_SCH_ORPH_ATTACH:
1421 case IO_SCH_UNREG_ATTACH:
1423 dev_id.ssid = sch->schid.ssid;
1424 dev_id.devno = sch->schib.pmcw.dev;
1425 cdev = get_ccwdev_by_dev_id(&dev_id);
1427 sch_create_and_recog_new_device(sch);
1430 rc = ccw_device_move_to_sch(cdev, sch);
1432 /* Release reference from get_ccwdev_by_dev_id() */
1433 put_device(&cdev->dev);
1436 spin_lock_irqsave(sch->lock, flags);
1437 ccw_device_trigger_reprobe(cdev);
1438 spin_unlock_irqrestore(sch->lock, flags);
1439 /* Release reference from get_ccwdev_by_dev_id() */
1440 put_device(&cdev->dev);
1448 spin_unlock_irqrestore(sch->lock, flags);
1453 #ifdef CONFIG_CCW_CONSOLE
1454 static struct ccw_device console_cdev;
1455 static struct ccw_device_private console_private;
1456 static int console_cdev_in_use;
1458 static DEFINE_SPINLOCK(ccw_console_lock);
1460 spinlock_t * cio_get_console_lock(void)
1462 return &ccw_console_lock;
1465 static int ccw_device_console_enable(struct ccw_device *cdev,
1466 struct subchannel *sch)
1470 /* Attach subchannel private data. */
1471 sch->private = cio_get_console_priv();
1472 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1473 io_subchannel_init_fields(sch);
1474 rc = cio_commit_config(sch);
1477 sch->driver = &io_subchannel_driver;
1478 /* Initialize the ccw_device structure. */
1479 cdev->dev.parent= &sch->dev;
1480 io_subchannel_recog(cdev, sch);
1481 /* Now wait for the async. recognition to come to an end. */
1482 spin_lock_irq(cdev->ccwlock);
1483 while (!dev_fsm_final_state(cdev))
1486 if (cdev->private->state != DEV_STATE_OFFLINE)
1488 ccw_device_online(cdev);
1489 while (!dev_fsm_final_state(cdev))
1491 if (cdev->private->state != DEV_STATE_ONLINE)
1495 spin_unlock_irq(cdev->ccwlock);
1500 ccw_device_probe_console(void)
1502 struct subchannel *sch;
1505 if (xchg(&console_cdev_in_use, 1) != 0)
1506 return ERR_PTR(-EBUSY);
1507 sch = cio_probe_console();
1509 console_cdev_in_use = 0;
1510 return (void *) sch;
1512 memset(&console_cdev, 0, sizeof(struct ccw_device));
1513 memset(&console_private, 0, sizeof(struct ccw_device_private));
1514 console_cdev.private = &console_private;
1515 console_private.cdev = &console_cdev;
1516 ret = ccw_device_console_enable(&console_cdev, sch);
1518 cio_release_console();
1519 console_cdev_in_use = 0;
1520 return ERR_PTR(ret);
1522 console_cdev.online = 1;
1523 return &console_cdev;
1526 static int ccw_device_pm_restore(struct device *dev);
1528 int ccw_device_force_console(void)
1530 if (!console_cdev_in_use)
1532 return ccw_device_pm_restore(&console_cdev.dev);
1534 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1538 * get ccw_device matching the busid, but only if owned by cdrv
1541 __ccwdev_check_busid(struct device *dev, void *id)
1547 return (strcmp(bus_id, dev_name(dev)) == 0);
1552 * get_ccwdev_by_busid() - obtain device from a bus id
1553 * @cdrv: driver the device is owned by
1554 * @bus_id: bus id of the device to be searched
1556 * This function searches all devices owned by @cdrv for a device with a bus
1557 * id matching @bus_id.
1559 * If a match is found, its reference count of the found device is increased
1560 * and it is returned; else %NULL is returned.
1562 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1566 struct device_driver *drv;
1568 drv = get_driver(&cdrv->driver);
1572 dev = driver_find_device(drv, NULL, (void *)bus_id,
1573 __ccwdev_check_busid);
1576 return dev ? to_ccwdev(dev) : NULL;
1579 /************************** device driver handling ************************/
1581 /* This is the implementation of the ccw_driver class. The probe, remove
1582 * and release methods are initially very similar to the device_driver
1583 * implementations, with the difference that they have ccw_device
1586 * A ccw driver also contains the information that is needed for
1590 ccw_device_probe (struct device *dev)
1592 struct ccw_device *cdev = to_ccwdev(dev);
1593 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1596 cdev->drv = cdrv; /* to let the driver call _set_online */
1598 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1609 ccw_device_remove (struct device *dev)
1611 struct ccw_device *cdev = to_ccwdev(dev);
1612 struct ccw_driver *cdrv = cdev->drv;
1619 spin_lock_irq(cdev->ccwlock);
1620 ret = ccw_device_offline(cdev);
1621 spin_unlock_irq(cdev->ccwlock);
1623 wait_event(cdev->private->wait_q,
1624 dev_fsm_final_state(cdev));
1626 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1627 "device 0.%x.%04x\n",
1628 ret, cdev->private->dev_id.ssid,
1629 cdev->private->dev_id.devno);
1630 /* Give up reference obtained in ccw_device_set_online(). */
1631 put_device(&cdev->dev);
1633 ccw_device_set_timeout(cdev, 0);
1638 static void ccw_device_shutdown(struct device *dev)
1640 struct ccw_device *cdev;
1642 cdev = to_ccwdev(dev);
1643 if (cdev->drv && cdev->drv->shutdown)
1644 cdev->drv->shutdown(cdev);
1648 static int ccw_device_pm_prepare(struct device *dev)
1650 struct ccw_device *cdev = to_ccwdev(dev);
1652 if (work_pending(&cdev->private->todo_work))
1654 /* Fail while device is being set online/offline. */
1655 if (atomic_read(&cdev->private->onoff))
1658 if (cdev->online && cdev->drv && cdev->drv->prepare)
1659 return cdev->drv->prepare(cdev);
1664 static void ccw_device_pm_complete(struct device *dev)
1666 struct ccw_device *cdev = to_ccwdev(dev);
1668 if (cdev->online && cdev->drv && cdev->drv->complete)
1669 cdev->drv->complete(cdev);
1672 static int ccw_device_pm_freeze(struct device *dev)
1674 struct ccw_device *cdev = to_ccwdev(dev);
1675 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1676 int ret, cm_enabled;
1678 /* Fail suspend while device is in transistional state. */
1679 if (!dev_fsm_final_state(cdev))
1683 if (cdev->drv && cdev->drv->freeze) {
1684 ret = cdev->drv->freeze(cdev);
1689 spin_lock_irq(sch->lock);
1690 cm_enabled = cdev->private->cmb != NULL;
1691 spin_unlock_irq(sch->lock);
1693 /* Don't have the css write on memory. */
1694 ret = ccw_set_cmf(cdev, 0);
1698 /* From here on, disallow device driver I/O. */
1699 spin_lock_irq(sch->lock);
1700 ret = cio_disable_subchannel(sch);
1701 spin_unlock_irq(sch->lock);
1706 static int ccw_device_pm_thaw(struct device *dev)
1708 struct ccw_device *cdev = to_ccwdev(dev);
1709 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1710 int ret, cm_enabled;
1715 spin_lock_irq(sch->lock);
1716 /* Allow device driver I/O again. */
1717 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1718 cm_enabled = cdev->private->cmb != NULL;
1719 spin_unlock_irq(sch->lock);
1724 ret = ccw_set_cmf(cdev, 1);
1729 if (cdev->drv && cdev->drv->thaw)
1730 ret = cdev->drv->thaw(cdev);
1735 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1737 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1739 if (cio_is_console(sch->schid))
1742 * While we were sleeping, devices may have gone or become
1743 * available again. Kick re-detection.
1745 spin_lock_irq(sch->lock);
1746 cdev->private->flags.resuming = 1;
1747 ccw_device_recognition(cdev);
1748 spin_unlock_irq(sch->lock);
1749 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1750 cdev->private->state == DEV_STATE_DISCONNECTED);
1752 cdev->private->flags.resuming = 0;
1755 static int resume_handle_boxed(struct ccw_device *cdev)
1757 cdev->private->state = DEV_STATE_BOXED;
1758 if (ccw_device_notify(cdev, CIO_BOXED))
1760 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1764 static int resume_handle_disc(struct ccw_device *cdev)
1766 cdev->private->state = DEV_STATE_DISCONNECTED;
1767 if (ccw_device_notify(cdev, CIO_GONE))
1769 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1773 static int ccw_device_pm_restore(struct device *dev)
1775 struct ccw_device *cdev = to_ccwdev(dev);
1776 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1777 int ret = 0, cm_enabled;
1779 __ccw_device_pm_restore(cdev);
1780 spin_lock_irq(sch->lock);
1781 if (cio_is_console(sch->schid)) {
1782 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1783 spin_unlock_irq(sch->lock);
1786 cdev->private->flags.donotify = 0;
1787 /* check recognition results */
1788 switch (cdev->private->state) {
1789 case DEV_STATE_OFFLINE:
1791 case DEV_STATE_BOXED:
1792 ret = resume_handle_boxed(cdev);
1793 spin_unlock_irq(sch->lock);
1797 case DEV_STATE_DISCONNECTED:
1798 goto out_disc_unlock;
1800 goto out_unreg_unlock;
1802 /* check if the device id has changed */
1803 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1804 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1805 "changed from %04x to %04x)\n",
1806 sch->schid.ssid, sch->schid.sch_no,
1807 cdev->private->dev_id.devno,
1808 sch->schib.pmcw.dev);
1809 goto out_unreg_unlock;
1811 /* check if the device type has changed */
1812 if (!ccw_device_test_sense_data(cdev)) {
1813 ccw_device_update_sense_data(cdev);
1814 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1818 if (!cdev->online) {
1822 ret = ccw_device_online(cdev);
1824 goto out_disc_unlock;
1826 cm_enabled = cdev->private->cmb != NULL;
1827 spin_unlock_irq(sch->lock);
1829 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1830 if (cdev->private->state != DEV_STATE_ONLINE) {
1831 spin_lock_irq(sch->lock);
1832 goto out_disc_unlock;
1835 ret = ccw_set_cmf(cdev, 1);
1837 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1838 "(rc=%d)\n", cdev->private->dev_id.ssid,
1839 cdev->private->dev_id.devno, ret);
1845 if (cdev->online && cdev->drv && cdev->drv->restore)
1846 ret = cdev->drv->restore(cdev);
1851 ret = resume_handle_disc(cdev);
1852 spin_unlock_irq(sch->lock);
1858 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
1861 spin_unlock_irq(sch->lock);
1865 static struct dev_pm_ops ccw_pm_ops = {
1866 .prepare = ccw_device_pm_prepare,
1867 .complete = ccw_device_pm_complete,
1868 .freeze = ccw_device_pm_freeze,
1869 .thaw = ccw_device_pm_thaw,
1870 .restore = ccw_device_pm_restore,
1873 struct bus_type ccw_bus_type = {
1875 .match = ccw_bus_match,
1876 .uevent = ccw_uevent,
1877 .probe = ccw_device_probe,
1878 .remove = ccw_device_remove,
1879 .shutdown = ccw_device_shutdown,
1884 * ccw_driver_register() - register a ccw driver
1885 * @cdriver: driver to be registered
1887 * This function is mainly a wrapper around driver_register().
1889 * %0 on success and a negative error value on failure.
1891 int ccw_driver_register(struct ccw_driver *cdriver)
1893 struct device_driver *drv = &cdriver->driver;
1895 drv->bus = &ccw_bus_type;
1896 drv->name = cdriver->name;
1897 drv->owner = cdriver->owner;
1899 return driver_register(drv);
1903 * ccw_driver_unregister() - deregister a ccw driver
1904 * @cdriver: driver to be deregistered
1906 * This function is mainly a wrapper around driver_unregister().
1908 void ccw_driver_unregister(struct ccw_driver *cdriver)
1910 driver_unregister(&cdriver->driver);
1913 /* Helper func for qdio. */
1914 struct subchannel_id
1915 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1917 struct subchannel *sch;
1919 sch = to_subchannel(cdev->dev.parent);
1923 static void ccw_device_todo(struct work_struct *work)
1925 struct ccw_device_private *priv;
1926 struct ccw_device *cdev;
1927 struct subchannel *sch;
1928 enum cdev_todo todo;
1930 priv = container_of(work, struct ccw_device_private, todo_work);
1932 sch = to_subchannel(cdev->dev.parent);
1933 /* Find out todo. */
1934 spin_lock_irq(cdev->ccwlock);
1936 priv->todo = CDEV_TODO_NOTHING;
1937 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1938 priv->dev_id.ssid, priv->dev_id.devno, todo);
1939 spin_unlock_irq(cdev->ccwlock);
1942 case CDEV_TODO_ENABLE_CMF:
1945 case CDEV_TODO_REBIND:
1946 ccw_device_do_unbind_bind(cdev);
1948 case CDEV_TODO_REGISTER:
1949 io_subchannel_register(cdev);
1951 case CDEV_TODO_UNREG_EVAL:
1952 if (!sch_is_pseudo_sch(sch))
1953 css_schedule_eval(sch->schid);
1955 case CDEV_TODO_UNREG:
1956 if (sch_is_pseudo_sch(sch))
1957 ccw_device_unregister(cdev);
1959 ccw_device_call_sch_unregister(cdev);
1964 /* Release workqueue ref. */
1965 put_device(&cdev->dev);
1969 * ccw_device_sched_todo - schedule ccw device operation
1973 * Schedule the operation identified by @todo to be performed on the slow path
1974 * workqueue. Do nothing if another operation with higher priority is already
1975 * scheduled. Needs to be called with ccwdev lock held.
1977 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1979 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1980 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1982 if (cdev->private->todo >= todo)
1984 cdev->private->todo = todo;
1985 /* Get workqueue ref. */
1986 if (!get_device(&cdev->dev))
1988 if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
1989 /* Already queued, release workqueue ref. */
1990 put_device(&cdev->dev);
1994 MODULE_LICENSE("GPL");
1995 EXPORT_SYMBOL(ccw_device_set_online);
1996 EXPORT_SYMBOL(ccw_device_set_offline);
1997 EXPORT_SYMBOL(ccw_driver_register);
1998 EXPORT_SYMBOL(ccw_driver_unregister);
1999 EXPORT_SYMBOL(get_ccwdev_by_busid);
2000 EXPORT_SYMBOL(ccw_bus_type);
2001 EXPORT_SYMBOL(ccw_device_work);
2002 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);