2 * bus driver for ccw devices
4 * Copyright IBM Corp. 2002, 2008
12 #define KMSG_COMPONENT "cio"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/device.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/sched/signal.h>
29 #include <asm/ccwdev.h>
31 #include <asm/param.h> /* HZ */
37 #include "cio_debug.h"
42 #include "blacklist.h"
45 static struct timer_list recovery_timer;
46 static DEFINE_SPINLOCK(recovery_lock);
47 static int recovery_phase;
48 static const unsigned long recovery_delay[] = { 3, 30, 300 };
50 static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
51 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
52 static struct bus_type ccw_bus_type;
54 /******************* bus type handling ***********************/
56 /* The Linux driver model distinguishes between a bus type and
57 * the bus itself. Of course we only have one channel
58 * subsystem driver and one channel system per machine, but
59 * we still use the abstraction. T.R. says it's a good idea. */
61 ccw_bus_match (struct device * dev, struct device_driver * drv)
63 struct ccw_device *cdev = to_ccwdev(dev);
64 struct ccw_driver *cdrv = to_ccwdrv(drv);
65 const struct ccw_device_id *ids = cdrv->ids, *found;
70 found = ccw_device_id_match(ids, &cdev->id);
74 cdev->id.driver_info = found->driver_info;
79 /* Store modalias string delimited by prefix/suffix string into buffer with
80 * specified size. Return length of resulting string (excluding trailing '\0')
81 * even if string doesn't fit buffer (snprintf semantics). */
82 static int snprint_alias(char *buf, size_t size,
83 struct ccw_device_id *id, const char *suffix)
87 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
93 if (id->dev_type != 0)
94 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
95 id->dev_model, suffix);
97 len += snprintf(buf, size, "dtdm%s", suffix);
102 /* Set up environment variables for ccw device uevent. Return 0 on success,
103 * non-zero otherwise. */
104 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
106 struct ccw_device *cdev = to_ccwdev(dev);
107 struct ccw_device_id *id = &(cdev->id);
109 char modalias_buf[30];
112 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
117 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
121 /* The next two can be zero, that's ok for us */
123 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
128 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
133 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
134 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
138 static void io_subchannel_irq(struct subchannel *);
139 static int io_subchannel_probe(struct subchannel *);
140 static int io_subchannel_remove(struct subchannel *);
141 static void io_subchannel_shutdown(struct subchannel *);
142 static int io_subchannel_sch_event(struct subchannel *, int);
143 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
145 static void recovery_func(unsigned long data);
147 static struct css_device_id io_subchannel_ids[] = {
148 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
149 { /* end of list */ },
152 static int io_subchannel_prepare(struct subchannel *sch)
154 struct ccw_device *cdev;
156 * Don't allow suspend while a ccw device registration
157 * is still outstanding.
159 cdev = sch_get_cdev(sch);
160 if (cdev && !device_is_registered(&cdev->dev))
165 static int io_subchannel_settle(void)
169 ret = wait_event_interruptible(ccw_device_init_wq,
170 atomic_read(&ccw_device_init_count) == 0);
173 flush_workqueue(cio_work_q);
177 static struct css_driver io_subchannel_driver = {
179 .owner = THIS_MODULE,
180 .name = "io_subchannel",
182 .subchannel_type = io_subchannel_ids,
183 .irq = io_subchannel_irq,
184 .sch_event = io_subchannel_sch_event,
185 .chp_event = io_subchannel_chp_event,
186 .probe = io_subchannel_probe,
187 .remove = io_subchannel_remove,
188 .shutdown = io_subchannel_shutdown,
189 .prepare = io_subchannel_prepare,
190 .settle = io_subchannel_settle,
193 int __init io_subchannel_init(void)
197 setup_timer(&recovery_timer, recovery_func, 0);
198 ret = bus_register(&ccw_bus_type);
201 ret = css_driver_register(&io_subchannel_driver);
203 bus_unregister(&ccw_bus_type);
209 /************************ device handling **************************/
212 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
214 struct ccw_device *cdev = to_ccwdev(dev);
215 struct ccw_device_id *id = &(cdev->id);
217 if (id->dev_type != 0)
218 return sprintf(buf, "%04x/%02x\n",
219 id->dev_type, id->dev_model);
221 return sprintf(buf, "n/a\n");
225 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
227 struct ccw_device *cdev = to_ccwdev(dev);
228 struct ccw_device_id *id = &(cdev->id);
230 return sprintf(buf, "%04x/%02x\n",
231 id->cu_type, id->cu_model);
235 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
237 struct ccw_device *cdev = to_ccwdev(dev);
238 struct ccw_device_id *id = &(cdev->id);
241 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
243 return len > PAGE_SIZE ? PAGE_SIZE : len;
247 online_show (struct device *dev, struct device_attribute *attr, char *buf)
249 struct ccw_device *cdev = to_ccwdev(dev);
251 return sprintf(buf, cdev->online ? "1\n" : "0\n");
254 int ccw_device_is_orphan(struct ccw_device *cdev)
256 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
259 static void ccw_device_unregister(struct ccw_device *cdev)
261 if (device_is_registered(&cdev->dev)) {
262 /* Undo device_add(). */
263 device_del(&cdev->dev);
265 if (cdev->private->flags.initialized) {
266 cdev->private->flags.initialized = 0;
267 /* Release reference from device_initialize(). */
268 put_device(&cdev->dev);
272 static void io_subchannel_quiesce(struct subchannel *);
275 * ccw_device_set_offline() - disable a ccw device for I/O
276 * @cdev: target ccw device
278 * This function calls the driver's set_offline() function for @cdev, if
279 * given, and then disables @cdev.
281 * %0 on success and a negative error value on failure.
283 * enabled, ccw device lock not held
285 int ccw_device_set_offline(struct ccw_device *cdev)
287 struct subchannel *sch;
292 if (!cdev->online || !cdev->drv)
295 if (cdev->drv->set_offline) {
296 ret = cdev->drv->set_offline(cdev);
300 spin_lock_irq(cdev->ccwlock);
301 sch = to_subchannel(cdev->dev.parent);
303 /* Wait until a final state or DISCONNECTED is reached */
304 while (!dev_fsm_final_state(cdev) &&
305 cdev->private->state != DEV_STATE_DISCONNECTED) {
306 spin_unlock_irq(cdev->ccwlock);
307 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
308 cdev->private->state == DEV_STATE_DISCONNECTED));
309 spin_lock_irq(cdev->ccwlock);
312 ret = ccw_device_offline(cdev);
315 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
316 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
317 cdev->private->dev_id.devno);
320 state = cdev->private->state;
321 spin_unlock_irq(cdev->ccwlock);
322 io_subchannel_quiesce(sch);
323 spin_lock_irq(cdev->ccwlock);
324 cdev->private->state = state;
325 } while (ret == -EBUSY);
326 spin_unlock_irq(cdev->ccwlock);
327 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
328 cdev->private->state == DEV_STATE_DISCONNECTED));
329 /* Inform the user if set offline failed. */
330 if (cdev->private->state == DEV_STATE_BOXED) {
331 pr_warn("%s: The device entered boxed state while being set offline\n",
332 dev_name(&cdev->dev));
333 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
334 pr_warn("%s: The device stopped operating while being set offline\n",
335 dev_name(&cdev->dev));
337 /* Give up reference from ccw_device_set_online(). */
338 put_device(&cdev->dev);
342 cdev->private->state = DEV_STATE_OFFLINE;
343 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
344 spin_unlock_irq(cdev->ccwlock);
345 /* Give up reference from ccw_device_set_online(). */
346 put_device(&cdev->dev);
351 * ccw_device_set_online() - enable a ccw device for I/O
352 * @cdev: target ccw device
354 * This function first enables @cdev and then calls the driver's set_online()
355 * function for @cdev, if given. If set_online() returns an error, @cdev is
358 * %0 on success and a negative error value on failure.
360 * enabled, ccw device lock not held
362 int ccw_device_set_online(struct ccw_device *cdev)
369 if (cdev->online || !cdev->drv)
371 /* Hold on to an extra reference while device is online. */
372 if (!get_device(&cdev->dev))
375 spin_lock_irq(cdev->ccwlock);
376 ret = ccw_device_online(cdev);
377 spin_unlock_irq(cdev->ccwlock);
379 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
381 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
382 "device 0.%x.%04x\n",
383 ret, cdev->private->dev_id.ssid,
384 cdev->private->dev_id.devno);
385 /* Give up online reference since onlining failed. */
386 put_device(&cdev->dev);
389 spin_lock_irq(cdev->ccwlock);
390 /* Check if online processing was successful */
391 if ((cdev->private->state != DEV_STATE_ONLINE) &&
392 (cdev->private->state != DEV_STATE_W4SENSE)) {
393 spin_unlock_irq(cdev->ccwlock);
394 /* Inform the user that set online failed. */
395 if (cdev->private->state == DEV_STATE_BOXED) {
396 pr_warn("%s: Setting the device online failed because it is boxed\n",
397 dev_name(&cdev->dev));
398 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
399 pr_warn("%s: Setting the device online failed because it is not operational\n",
400 dev_name(&cdev->dev));
402 /* Give up online reference since onlining failed. */
403 put_device(&cdev->dev);
406 spin_unlock_irq(cdev->ccwlock);
407 if (cdev->drv->set_online)
408 ret = cdev->drv->set_online(cdev);
412 spin_lock_irq(cdev->ccwlock);
414 spin_unlock_irq(cdev->ccwlock);
418 spin_lock_irq(cdev->ccwlock);
419 /* Wait until a final state or DISCONNECTED is reached */
420 while (!dev_fsm_final_state(cdev) &&
421 cdev->private->state != DEV_STATE_DISCONNECTED) {
422 spin_unlock_irq(cdev->ccwlock);
423 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
424 cdev->private->state == DEV_STATE_DISCONNECTED));
425 spin_lock_irq(cdev->ccwlock);
427 ret2 = ccw_device_offline(cdev);
430 spin_unlock_irq(cdev->ccwlock);
431 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
432 cdev->private->state == DEV_STATE_DISCONNECTED));
433 /* Give up online reference since onlining failed. */
434 put_device(&cdev->dev);
438 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
439 "device 0.%x.%04x\n",
440 ret2, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
442 cdev->private->state = DEV_STATE_OFFLINE;
443 spin_unlock_irq(cdev->ccwlock);
444 /* Give up online reference since onlining failed. */
445 put_device(&cdev->dev);
449 static int online_store_handle_offline(struct ccw_device *cdev)
451 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
452 spin_lock_irq(cdev->ccwlock);
453 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
454 spin_unlock_irq(cdev->ccwlock);
457 if (cdev->drv && cdev->drv->set_offline)
458 return ccw_device_set_offline(cdev);
462 static int online_store_recog_and_online(struct ccw_device *cdev)
464 /* Do device recognition, if needed. */
465 if (cdev->private->state == DEV_STATE_BOXED) {
466 spin_lock_irq(cdev->ccwlock);
467 ccw_device_recognition(cdev);
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q,
470 cdev->private->flags.recog_done);
471 if (cdev->private->state != DEV_STATE_OFFLINE)
472 /* recognition failed */
475 if (cdev->drv && cdev->drv->set_online)
476 return ccw_device_set_online(cdev);
480 static int online_store_handle_online(struct ccw_device *cdev, int force)
484 ret = online_store_recog_and_online(cdev);
487 if (force && cdev->private->state == DEV_STATE_BOXED) {
488 ret = ccw_device_stlck(cdev);
491 if (cdev->id.cu_type == 0)
492 cdev->private->state = DEV_STATE_NOT_OPER;
493 ret = online_store_recog_and_online(cdev);
500 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
503 struct ccw_device *cdev = to_ccwdev(dev);
507 /* Prevent conflict between multiple on-/offline processing requests. */
508 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
510 /* Prevent conflict between internal I/Os and on-/offline processing. */
511 if (!dev_fsm_final_state(cdev) &&
512 cdev->private->state != DEV_STATE_DISCONNECTED) {
516 /* Prevent conflict between pending work and on-/offline processing.*/
517 if (work_pending(&cdev->private->todo_work)) {
521 if (!strncmp(buf, "force\n", count)) {
527 ret = kstrtoul(buf, 16, &i);
535 ret = online_store_handle_offline(cdev);
538 ret = online_store_handle_online(cdev, force);
546 atomic_set(&cdev->private->onoff, 0);
547 return (ret < 0) ? ret : count;
551 available_show (struct device *dev, struct device_attribute *attr, char *buf)
553 struct ccw_device *cdev = to_ccwdev(dev);
554 struct subchannel *sch;
556 if (ccw_device_is_orphan(cdev))
557 return sprintf(buf, "no device\n");
558 switch (cdev->private->state) {
559 case DEV_STATE_BOXED:
560 return sprintf(buf, "boxed\n");
561 case DEV_STATE_DISCONNECTED:
562 case DEV_STATE_DISCONNECTED_SENSE_ID:
563 case DEV_STATE_NOT_OPER:
564 sch = to_subchannel(dev->parent);
566 return sprintf(buf, "no path\n");
568 return sprintf(buf, "no device\n");
570 /* All other states considered fine. */
571 return sprintf(buf, "good\n");
576 initiate_logging(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
579 struct subchannel *sch = to_subchannel(dev);
582 rc = chsc_siosl(sch->schid);
584 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
585 sch->schid.ssid, sch->schid.sch_no, rc);
588 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
589 sch->schid.ssid, sch->schid.sch_no);
593 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
596 struct subchannel *sch = to_subchannel(dev);
598 return sprintf(buf, "%02x\n", sch->vpm);
601 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
602 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
603 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
604 static DEVICE_ATTR(online, 0644, online_show, online_store);
605 static DEVICE_ATTR(availability, 0444, available_show, NULL);
606 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
607 static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
609 static struct attribute *io_subchannel_attrs[] = {
610 &dev_attr_logging.attr,
615 static const struct attribute_group io_subchannel_attr_group = {
616 .attrs = io_subchannel_attrs,
619 static struct attribute * ccwdev_attrs[] = {
620 &dev_attr_devtype.attr,
621 &dev_attr_cutype.attr,
622 &dev_attr_modalias.attr,
623 &dev_attr_online.attr,
624 &dev_attr_cmb_enable.attr,
625 &dev_attr_availability.attr,
629 static const struct attribute_group ccwdev_attr_group = {
630 .attrs = ccwdev_attrs,
633 static const struct attribute_group *ccwdev_attr_groups[] = {
638 static int ccw_device_add(struct ccw_device *cdev)
640 struct device *dev = &cdev->dev;
642 dev->bus = &ccw_bus_type;
643 return device_add(dev);
646 static int match_dev_id(struct device *dev, void *data)
648 struct ccw_device *cdev = to_ccwdev(dev);
649 struct ccw_dev_id *dev_id = data;
651 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
655 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
656 * @dev_id: id of the device to be searched
658 * This function searches all devices attached to the ccw bus for a device
661 * If a device is found its reference count is increased and returned;
662 * else %NULL is returned.
664 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
668 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
670 return dev ? to_ccwdev(dev) : NULL;
672 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
674 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
678 if (device_is_registered(&cdev->dev)) {
679 device_release_driver(&cdev->dev);
680 ret = device_attach(&cdev->dev);
681 WARN_ON(ret == -ENODEV);
686 ccw_device_release(struct device *dev)
688 struct ccw_device *cdev;
690 cdev = to_ccwdev(dev);
691 /* Release reference of parent subchannel. */
692 put_device(cdev->dev.parent);
693 kfree(cdev->private);
697 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
699 struct ccw_device *cdev;
701 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
703 cdev->private = kzalloc(sizeof(struct ccw_device_private),
704 GFP_KERNEL | GFP_DMA);
709 return ERR_PTR(-ENOMEM);
712 static void ccw_device_todo(struct work_struct *work);
714 static int io_subchannel_initialize_dev(struct subchannel *sch,
715 struct ccw_device *cdev)
717 struct ccw_device_private *priv = cdev->private;
721 priv->int_class = IRQIO_CIO;
722 priv->state = DEV_STATE_NOT_OPER;
723 priv->dev_id.devno = sch->schib.pmcw.dev;
724 priv->dev_id.ssid = sch->schid.ssid;
726 INIT_WORK(&priv->todo_work, ccw_device_todo);
727 INIT_LIST_HEAD(&priv->cmb_list);
728 init_waitqueue_head(&priv->wait_q);
729 init_timer(&priv->timer);
731 atomic_set(&priv->onoff, 0);
732 cdev->ccwlock = sch->lock;
733 cdev->dev.parent = &sch->dev;
734 cdev->dev.release = ccw_device_release;
735 cdev->dev.groups = ccwdev_attr_groups;
736 /* Do first half of device_register. */
737 device_initialize(&cdev->dev);
738 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
739 cdev->private->dev_id.devno);
742 if (!get_device(&sch->dev)) {
746 priv->flags.initialized = 1;
747 spin_lock_irq(sch->lock);
748 sch_set_cdev(sch, cdev);
749 spin_unlock_irq(sch->lock);
753 /* Release reference from device_initialize(). */
754 put_device(&cdev->dev);
758 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
760 struct ccw_device *cdev;
763 cdev = io_subchannel_allocate_dev(sch);
765 ret = io_subchannel_initialize_dev(sch, cdev);
772 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
774 static void sch_create_and_recog_new_device(struct subchannel *sch)
776 struct ccw_device *cdev;
778 /* Need to allocate a new ccw device. */
779 cdev = io_subchannel_create_ccwdev(sch);
781 /* OK, we did everything we could... */
782 css_sch_device_unregister(sch);
785 /* Start recognition for the new ccw device. */
786 io_subchannel_recog(cdev, sch);
790 * Register recognized device.
792 static void io_subchannel_register(struct ccw_device *cdev)
794 struct subchannel *sch;
795 int ret, adjust_init_count = 1;
798 sch = to_subchannel(cdev->dev.parent);
800 * Check if subchannel is still registered. It may have become
801 * unregistered if a machine check hit us after finishing
802 * device recognition but before the register work could be
805 if (!device_is_registered(&sch->dev))
807 css_update_ssd_info(sch);
809 * io_subchannel_register() will also be called after device
810 * recognition has been done for a boxed device (which will already
811 * be registered). We need to reprobe since we may now have sense id
814 if (device_is_registered(&cdev->dev)) {
816 ret = device_reprobe(&cdev->dev);
818 /* We can't do much here. */
819 CIO_MSG_EVENT(0, "device_reprobe() returned"
820 " %d for 0.%x.%04x\n", ret,
821 cdev->private->dev_id.ssid,
822 cdev->private->dev_id.devno);
824 adjust_init_count = 0;
828 * Now we know this subchannel will stay, we can throw
829 * our delayed uevent.
831 dev_set_uevent_suppress(&sch->dev, 0);
832 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
833 /* make it known to the system */
834 ret = ccw_device_add(cdev);
836 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
837 cdev->private->dev_id.ssid,
838 cdev->private->dev_id.devno, ret);
839 spin_lock_irqsave(sch->lock, flags);
840 sch_set_cdev(sch, NULL);
841 spin_unlock_irqrestore(sch->lock, flags);
842 /* Release initial device reference. */
843 put_device(&cdev->dev);
847 cdev->private->flags.recog_done = 1;
848 wake_up(&cdev->private->wait_q);
850 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
851 wake_up(&ccw_device_init_wq);
854 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
856 struct subchannel *sch;
858 /* Get subchannel reference for local processing. */
859 if (!get_device(cdev->dev.parent))
861 sch = to_subchannel(cdev->dev.parent);
862 css_sch_device_unregister(sch);
863 /* Release subchannel reference for local processing. */
864 put_device(&sch->dev);
868 * subchannel recognition done. Called from the state machine.
871 io_subchannel_recog_done(struct ccw_device *cdev)
873 if (css_init_done == 0) {
874 cdev->private->flags.recog_done = 1;
877 switch (cdev->private->state) {
878 case DEV_STATE_BOXED:
879 /* Device did not respond in time. */
880 case DEV_STATE_NOT_OPER:
881 cdev->private->flags.recog_done = 1;
882 /* Remove device found not operational. */
883 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
884 if (atomic_dec_and_test(&ccw_device_init_count))
885 wake_up(&ccw_device_init_wq);
887 case DEV_STATE_OFFLINE:
889 * We can't register the device in interrupt context so
890 * we schedule a work item.
892 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
897 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
899 /* Increase counter of devices currently in recognition. */
900 atomic_inc(&ccw_device_init_count);
902 /* Start async. device sensing. */
903 spin_lock_irq(sch->lock);
904 ccw_device_recognition(cdev);
905 spin_unlock_irq(sch->lock);
908 static int ccw_device_move_to_sch(struct ccw_device *cdev,
909 struct subchannel *sch)
911 struct subchannel *old_sch;
912 int rc, old_enabled = 0;
914 old_sch = to_subchannel(cdev->dev.parent);
915 /* Obtain child reference for new parent. */
916 if (!get_device(&sch->dev))
919 if (!sch_is_pseudo_sch(old_sch)) {
920 spin_lock_irq(old_sch->lock);
921 old_enabled = old_sch->schib.pmcw.ena;
924 rc = cio_disable_subchannel(old_sch);
925 spin_unlock_irq(old_sch->lock);
927 /* Release child reference for new parent. */
928 put_device(&sch->dev);
933 mutex_lock(&sch->reg_mutex);
934 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
935 mutex_unlock(&sch->reg_mutex);
937 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
938 cdev->private->dev_id.ssid,
939 cdev->private->dev_id.devno, sch->schid.ssid,
940 sch->schib.pmcw.dev, rc);
942 /* Try to reenable the old subchannel. */
943 spin_lock_irq(old_sch->lock);
944 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
945 spin_unlock_irq(old_sch->lock);
947 /* Release child reference for new parent. */
948 put_device(&sch->dev);
951 /* Clean up old subchannel. */
952 if (!sch_is_pseudo_sch(old_sch)) {
953 spin_lock_irq(old_sch->lock);
954 sch_set_cdev(old_sch, NULL);
955 spin_unlock_irq(old_sch->lock);
956 css_schedule_eval(old_sch->schid);
958 /* Release child reference for old parent. */
959 put_device(&old_sch->dev);
960 /* Initialize new subchannel. */
961 spin_lock_irq(sch->lock);
962 cdev->ccwlock = sch->lock;
963 if (!sch_is_pseudo_sch(sch))
964 sch_set_cdev(sch, cdev);
965 spin_unlock_irq(sch->lock);
966 if (!sch_is_pseudo_sch(sch))
967 css_update_ssd_info(sch);
971 static int ccw_device_move_to_orph(struct ccw_device *cdev)
973 struct subchannel *sch = to_subchannel(cdev->dev.parent);
974 struct channel_subsystem *css = to_css(sch->dev.parent);
976 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
979 static void io_subchannel_irq(struct subchannel *sch)
981 struct ccw_device *cdev;
983 cdev = sch_get_cdev(sch);
985 CIO_TRACE_EVENT(6, "IRQ");
986 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
988 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
990 inc_irq_stat(IRQIO_CIO);
993 void io_subchannel_init_config(struct subchannel *sch)
995 memset(&sch->config, 0, sizeof(sch->config));
996 sch->config.csense = 1;
999 static void io_subchannel_init_fields(struct subchannel *sch)
1001 if (cio_is_console(sch->schid))
1004 sch->opm = chp_get_sch_opm(sch);
1005 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1006 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1008 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1009 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1010 sch->schib.pmcw.dev, sch->schid.ssid,
1011 sch->schid.sch_no, sch->schib.pmcw.pim,
1012 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1014 io_subchannel_init_config(sch);
1018 * Note: We always return 0 so that we bind to the device even on error.
1019 * This is needed so that our remove function is called on unregister.
1021 static int io_subchannel_probe(struct subchannel *sch)
1023 struct io_subchannel_private *io_priv;
1024 struct ccw_device *cdev;
1027 if (cio_is_console(sch->schid)) {
1028 rc = sysfs_create_group(&sch->dev.kobj,
1029 &io_subchannel_attr_group);
1031 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1032 "attributes for subchannel "
1033 "0.%x.%04x (rc=%d)\n",
1034 sch->schid.ssid, sch->schid.sch_no, rc);
1036 * The console subchannel already has an associated ccw_device.
1037 * Throw the delayed uevent for the subchannel, register
1038 * the ccw_device and exit.
1040 dev_set_uevent_suppress(&sch->dev, 0);
1041 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1042 cdev = sch_get_cdev(sch);
1043 rc = ccw_device_add(cdev);
1045 /* Release online reference. */
1046 put_device(&cdev->dev);
1049 if (atomic_dec_and_test(&ccw_device_init_count))
1050 wake_up(&ccw_device_init_wq);
1053 io_subchannel_init_fields(sch);
1054 rc = cio_commit_config(sch);
1057 rc = sysfs_create_group(&sch->dev.kobj,
1058 &io_subchannel_attr_group);
1061 /* Allocate I/O subchannel private data. */
1062 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1066 set_io_private(sch, io_priv);
1067 css_schedule_eval(sch->schid);
1071 spin_lock_irq(sch->lock);
1072 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1073 spin_unlock_irq(sch->lock);
1078 io_subchannel_remove (struct subchannel *sch)
1080 struct io_subchannel_private *io_priv = to_io_private(sch);
1081 struct ccw_device *cdev;
1083 cdev = sch_get_cdev(sch);
1086 io_subchannel_quiesce(sch);
1087 /* Set ccw device to not operational and drop reference. */
1088 spin_lock_irq(cdev->ccwlock);
1089 sch_set_cdev(sch, NULL);
1090 set_io_private(sch, NULL);
1091 cdev->private->state = DEV_STATE_NOT_OPER;
1092 spin_unlock_irq(cdev->ccwlock);
1093 ccw_device_unregister(cdev);
1096 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1100 static void io_subchannel_verify(struct subchannel *sch)
1102 struct ccw_device *cdev;
1104 cdev = sch_get_cdev(sch);
1106 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1109 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1111 struct ccw_device *cdev;
1113 cdev = sch_get_cdev(sch);
1116 if (cio_update_schib(sch))
1118 /* Check for I/O on path. */
1119 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1121 if (cdev->private->state == DEV_STATE_ONLINE) {
1122 ccw_device_kill_io(cdev);
1128 /* Trigger path verification. */
1129 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1133 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1136 static int io_subchannel_chp_event(struct subchannel *sch,
1137 struct chp_link *link, int event)
1139 struct ccw_device *cdev = sch_get_cdev(sch);
1142 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1150 cdev->private->path_gone_mask |= mask;
1151 io_subchannel_terminate_path(sch, mask);
1157 cdev->private->path_new_mask |= mask;
1158 io_subchannel_verify(sch);
1161 if (cio_update_schib(sch))
1164 cdev->private->path_gone_mask |= mask;
1165 io_subchannel_terminate_path(sch, mask);
1168 if (cio_update_schib(sch))
1170 sch->lpm |= mask & sch->opm;
1172 cdev->private->path_new_mask |= mask;
1173 io_subchannel_verify(sch);
1179 static void io_subchannel_quiesce(struct subchannel *sch)
1181 struct ccw_device *cdev;
1184 spin_lock_irq(sch->lock);
1185 cdev = sch_get_cdev(sch);
1186 if (cio_is_console(sch->schid))
1188 if (!sch->schib.pmcw.ena)
1190 ret = cio_disable_subchannel(sch);
1194 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1195 while (ret == -EBUSY) {
1196 cdev->private->state = DEV_STATE_QUIESCE;
1197 cdev->private->iretry = 255;
1198 ret = ccw_device_cancel_halt_clear(cdev);
1199 if (ret == -EBUSY) {
1200 ccw_device_set_timeout(cdev, HZ/10);
1201 spin_unlock_irq(sch->lock);
1202 wait_event(cdev->private->wait_q,
1203 cdev->private->state != DEV_STATE_QUIESCE);
1204 spin_lock_irq(sch->lock);
1206 ret = cio_disable_subchannel(sch);
1209 spin_unlock_irq(sch->lock);
1212 static void io_subchannel_shutdown(struct subchannel *sch)
1214 io_subchannel_quiesce(sch);
1217 static int device_is_disconnected(struct ccw_device *cdev)
1221 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1222 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1225 static int recovery_check(struct device *dev, void *data)
1227 struct ccw_device *cdev = to_ccwdev(dev);
1228 struct subchannel *sch;
1231 spin_lock_irq(cdev->ccwlock);
1232 switch (cdev->private->state) {
1233 case DEV_STATE_ONLINE:
1234 sch = to_subchannel(cdev->dev.parent);
1235 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1238 case DEV_STATE_DISCONNECTED:
1239 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1240 cdev->private->dev_id.ssid,
1241 cdev->private->dev_id.devno);
1242 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1245 case DEV_STATE_DISCONNECTED_SENSE_ID:
1249 spin_unlock_irq(cdev->ccwlock);
1254 static void recovery_work_func(struct work_struct *unused)
1258 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1260 spin_lock_irq(&recovery_lock);
1261 if (!timer_pending(&recovery_timer)) {
1262 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1264 mod_timer(&recovery_timer, jiffies +
1265 recovery_delay[recovery_phase] * HZ);
1267 spin_unlock_irq(&recovery_lock);
1269 CIO_MSG_EVENT(3, "recovery: end\n");
1272 static DECLARE_WORK(recovery_work, recovery_work_func);
1274 static void recovery_func(unsigned long data)
1277 * We can't do our recovery in softirq context and it's not
1278 * performance critical, so we schedule it.
1280 schedule_work(&recovery_work);
1283 void ccw_device_schedule_recovery(void)
1285 unsigned long flags;
1287 CIO_MSG_EVENT(3, "recovery: schedule\n");
1288 spin_lock_irqsave(&recovery_lock, flags);
1289 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1291 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1293 spin_unlock_irqrestore(&recovery_lock, flags);
1296 static int purge_fn(struct device *dev, void *data)
1298 struct ccw_device *cdev = to_ccwdev(dev);
1299 struct ccw_dev_id *id = &cdev->private->dev_id;
1301 spin_lock_irq(cdev->ccwlock);
1302 if (is_blacklisted(id->ssid, id->devno) &&
1303 (cdev->private->state == DEV_STATE_OFFLINE) &&
1304 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1305 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1307 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1308 atomic_set(&cdev->private->onoff, 0);
1310 spin_unlock_irq(cdev->ccwlock);
1311 /* Abort loop in case of pending signal. */
1312 if (signal_pending(current))
1319 * ccw_purge_blacklisted - purge unused, blacklisted devices
1321 * Unregister all ccw devices that are offline and on the blacklist.
1323 int ccw_purge_blacklisted(void)
1325 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1326 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1330 void ccw_device_set_disconnected(struct ccw_device *cdev)
1334 ccw_device_set_timeout(cdev, 0);
1335 cdev->private->flags.fake_irb = 0;
1336 cdev->private->state = DEV_STATE_DISCONNECTED;
1338 ccw_device_schedule_recovery();
1341 void ccw_device_set_notoper(struct ccw_device *cdev)
1343 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1345 CIO_TRACE_EVENT(2, "notoper");
1346 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1347 ccw_device_set_timeout(cdev, 0);
1348 cio_disable_subchannel(sch);
1349 cdev->private->state = DEV_STATE_NOT_OPER;
1352 enum io_sch_action {
1356 IO_SCH_UNREG_ATTACH,
1364 static enum io_sch_action sch_get_action(struct subchannel *sch)
1366 struct ccw_device *cdev;
1368 cdev = sch_get_cdev(sch);
1369 if (cio_update_schib(sch)) {
1370 /* Not operational. */
1372 return IO_SCH_UNREG;
1373 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1374 return IO_SCH_UNREG;
1375 return IO_SCH_ORPH_UNREG;
1379 return IO_SCH_ATTACH;
1380 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1381 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1382 return IO_SCH_UNREG_ATTACH;
1383 return IO_SCH_ORPH_ATTACH;
1385 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1386 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1387 return IO_SCH_UNREG;
1390 if (device_is_disconnected(cdev))
1391 return IO_SCH_REPROBE;
1392 if (cdev->online && !cdev->private->flags.resuming)
1393 return IO_SCH_VERIFY;
1394 if (cdev->private->state == DEV_STATE_NOT_OPER)
1395 return IO_SCH_UNREG_ATTACH;
1400 * io_subchannel_sch_event - process subchannel event
1402 * @process: non-zero if function is called in process context
1404 * An unspecified event occurred for this subchannel. Adjust data according
1405 * to the current operational state of the subchannel and device. Return
1406 * zero when the event has been handled sufficiently or -EAGAIN when this
1407 * function should be called again in process context.
1409 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1411 unsigned long flags;
1412 struct ccw_device *cdev;
1413 struct ccw_dev_id dev_id;
1414 enum io_sch_action action;
1417 spin_lock_irqsave(sch->lock, flags);
1418 if (!device_is_registered(&sch->dev))
1420 if (work_pending(&sch->todo_work))
1422 cdev = sch_get_cdev(sch);
1423 if (cdev && work_pending(&cdev->private->todo_work))
1425 action = sch_get_action(sch);
1426 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1427 sch->schid.ssid, sch->schid.sch_no, process,
1429 /* Perform immediate actions while holding the lock. */
1431 case IO_SCH_REPROBE:
1432 /* Trigger device recognition. */
1433 ccw_device_trigger_reprobe(cdev);
1437 /* Trigger path verification. */
1438 io_subchannel_verify(sch);
1442 ccw_device_set_disconnected(cdev);
1445 case IO_SCH_ORPH_UNREG:
1446 case IO_SCH_ORPH_ATTACH:
1447 ccw_device_set_disconnected(cdev);
1449 case IO_SCH_UNREG_ATTACH:
1453 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1455 * Note: delayed work triggered by this event
1456 * and repeated calls to sch_event are synchronized
1457 * by the above check for work_pending(cdev).
1459 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1461 ccw_device_set_notoper(cdev);
1469 spin_unlock_irqrestore(sch->lock, flags);
1470 /* All other actions require process context. */
1473 /* Handle attached ccw device. */
1475 case IO_SCH_ORPH_UNREG:
1476 case IO_SCH_ORPH_ATTACH:
1477 /* Move ccw device to orphanage. */
1478 rc = ccw_device_move_to_orph(cdev);
1482 case IO_SCH_UNREG_ATTACH:
1483 spin_lock_irqsave(sch->lock, flags);
1484 if (cdev->private->flags.resuming) {
1485 /* Device will be handled later. */
1489 sch_set_cdev(sch, NULL);
1490 spin_unlock_irqrestore(sch->lock, flags);
1491 /* Unregister ccw device. */
1492 ccw_device_unregister(cdev);
1497 /* Handle subchannel. */
1499 case IO_SCH_ORPH_UNREG:
1501 if (!cdev || !cdev->private->flags.resuming)
1502 css_sch_device_unregister(sch);
1504 case IO_SCH_ORPH_ATTACH:
1505 case IO_SCH_UNREG_ATTACH:
1507 dev_id.ssid = sch->schid.ssid;
1508 dev_id.devno = sch->schib.pmcw.dev;
1509 cdev = get_ccwdev_by_dev_id(&dev_id);
1511 sch_create_and_recog_new_device(sch);
1514 rc = ccw_device_move_to_sch(cdev, sch);
1516 /* Release reference from get_ccwdev_by_dev_id() */
1517 put_device(&cdev->dev);
1520 spin_lock_irqsave(sch->lock, flags);
1521 ccw_device_trigger_reprobe(cdev);
1522 spin_unlock_irqrestore(sch->lock, flags);
1523 /* Release reference from get_ccwdev_by_dev_id() */
1524 put_device(&cdev->dev);
1532 spin_unlock_irqrestore(sch->lock, flags);
1537 static void ccw_device_set_int_class(struct ccw_device *cdev)
1539 struct ccw_driver *cdrv = cdev->drv;
1541 /* Note: we interpret class 0 in this context as an uninitialized
1542 * field since it translates to a non-I/O interrupt class. */
1543 if (cdrv->int_class != 0)
1544 cdev->private->int_class = cdrv->int_class;
1546 cdev->private->int_class = IRQIO_CIO;
1549 #ifdef CONFIG_CCW_CONSOLE
1550 int __init ccw_device_enable_console(struct ccw_device *cdev)
1552 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1555 if (!cdev->drv || !cdev->handler)
1558 io_subchannel_init_fields(sch);
1559 rc = cio_commit_config(sch);
1562 sch->driver = &io_subchannel_driver;
1563 io_subchannel_recog(cdev, sch);
1564 /* Now wait for the async. recognition to come to an end. */
1565 spin_lock_irq(cdev->ccwlock);
1566 while (!dev_fsm_final_state(cdev))
1567 ccw_device_wait_idle(cdev);
1569 /* Hold on to an extra reference while device is online. */
1570 get_device(&cdev->dev);
1571 rc = ccw_device_online(cdev);
1575 while (!dev_fsm_final_state(cdev))
1576 ccw_device_wait_idle(cdev);
1578 if (cdev->private->state == DEV_STATE_ONLINE)
1583 spin_unlock_irq(cdev->ccwlock);
1584 if (rc) /* Give up online reference since onlining failed. */
1585 put_device(&cdev->dev);
1589 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1591 struct io_subchannel_private *io_priv;
1592 struct ccw_device *cdev;
1593 struct subchannel *sch;
1595 sch = cio_probe_console();
1597 return ERR_CAST(sch);
1599 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1601 put_device(&sch->dev);
1602 return ERR_PTR(-ENOMEM);
1604 set_io_private(sch, io_priv);
1605 cdev = io_subchannel_create_ccwdev(sch);
1607 put_device(&sch->dev);
1612 ccw_device_set_int_class(cdev);
1616 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1618 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1619 struct io_subchannel_private *io_priv = to_io_private(sch);
1621 set_io_private(sch, NULL);
1622 put_device(&sch->dev);
1623 put_device(&cdev->dev);
1628 * ccw_device_wait_idle() - busy wait for device to become idle
1631 * Poll until activity control is zero, that is, no function or data
1632 * transfer is pending/active.
1633 * Called with device lock being held.
1635 void ccw_device_wait_idle(struct ccw_device *cdev)
1637 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1641 if (sch->schib.scsw.cmd.actl == 0)
1647 static int ccw_device_pm_restore(struct device *dev);
1649 int ccw_device_force_console(struct ccw_device *cdev)
1651 return ccw_device_pm_restore(&cdev->dev);
1653 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1657 * get ccw_device matching the busid, but only if owned by cdrv
1660 __ccwdev_check_busid(struct device *dev, void *id)
1666 return (strcmp(bus_id, dev_name(dev)) == 0);
1671 * get_ccwdev_by_busid() - obtain device from a bus id
1672 * @cdrv: driver the device is owned by
1673 * @bus_id: bus id of the device to be searched
1675 * This function searches all devices owned by @cdrv for a device with a bus
1676 * id matching @bus_id.
1678 * If a match is found, its reference count of the found device is increased
1679 * and it is returned; else %NULL is returned.
1681 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1686 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1687 __ccwdev_check_busid);
1689 return dev ? to_ccwdev(dev) : NULL;
1692 /************************** device driver handling ************************/
1694 /* This is the implementation of the ccw_driver class. The probe, remove
1695 * and release methods are initially very similar to the device_driver
1696 * implementations, with the difference that they have ccw_device
1699 * A ccw driver also contains the information that is needed for
1703 ccw_device_probe (struct device *dev)
1705 struct ccw_device *cdev = to_ccwdev(dev);
1706 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1709 cdev->drv = cdrv; /* to let the driver call _set_online */
1710 ccw_device_set_int_class(cdev);
1711 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1714 cdev->private->int_class = IRQIO_CIO;
1721 static int ccw_device_remove(struct device *dev)
1723 struct ccw_device *cdev = to_ccwdev(dev);
1724 struct ccw_driver *cdrv = cdev->drv;
1730 spin_lock_irq(cdev->ccwlock);
1733 ret = ccw_device_offline(cdev);
1734 spin_unlock_irq(cdev->ccwlock);
1736 wait_event(cdev->private->wait_q,
1737 dev_fsm_final_state(cdev));
1739 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1740 "device 0.%x.%04x\n",
1741 ret, cdev->private->dev_id.ssid,
1742 cdev->private->dev_id.devno);
1743 /* Give up reference obtained in ccw_device_set_online(). */
1744 put_device(&cdev->dev);
1745 spin_lock_irq(cdev->ccwlock);
1747 ccw_device_set_timeout(cdev, 0);
1749 cdev->private->int_class = IRQIO_CIO;
1750 spin_unlock_irq(cdev->ccwlock);
1751 __disable_cmf(cdev);
1756 static void ccw_device_shutdown(struct device *dev)
1758 struct ccw_device *cdev;
1760 cdev = to_ccwdev(dev);
1761 if (cdev->drv && cdev->drv->shutdown)
1762 cdev->drv->shutdown(cdev);
1763 __disable_cmf(cdev);
1766 static int ccw_device_pm_prepare(struct device *dev)
1768 struct ccw_device *cdev = to_ccwdev(dev);
1770 if (work_pending(&cdev->private->todo_work))
1772 /* Fail while device is being set online/offline. */
1773 if (atomic_read(&cdev->private->onoff))
1776 if (cdev->online && cdev->drv && cdev->drv->prepare)
1777 return cdev->drv->prepare(cdev);
1782 static void ccw_device_pm_complete(struct device *dev)
1784 struct ccw_device *cdev = to_ccwdev(dev);
1786 if (cdev->online && cdev->drv && cdev->drv->complete)
1787 cdev->drv->complete(cdev);
1790 static int ccw_device_pm_freeze(struct device *dev)
1792 struct ccw_device *cdev = to_ccwdev(dev);
1793 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1794 int ret, cm_enabled;
1796 /* Fail suspend while device is in transistional state. */
1797 if (!dev_fsm_final_state(cdev))
1801 if (cdev->drv && cdev->drv->freeze) {
1802 ret = cdev->drv->freeze(cdev);
1807 spin_lock_irq(sch->lock);
1808 cm_enabled = cdev->private->cmb != NULL;
1809 spin_unlock_irq(sch->lock);
1811 /* Don't have the css write on memory. */
1812 ret = ccw_set_cmf(cdev, 0);
1816 /* From here on, disallow device driver I/O. */
1817 spin_lock_irq(sch->lock);
1818 ret = cio_disable_subchannel(sch);
1819 spin_unlock_irq(sch->lock);
1824 static int ccw_device_pm_thaw(struct device *dev)
1826 struct ccw_device *cdev = to_ccwdev(dev);
1827 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1828 int ret, cm_enabled;
1833 spin_lock_irq(sch->lock);
1834 /* Allow device driver I/O again. */
1835 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1836 cm_enabled = cdev->private->cmb != NULL;
1837 spin_unlock_irq(sch->lock);
1842 ret = ccw_set_cmf(cdev, 1);
1847 if (cdev->drv && cdev->drv->thaw)
1848 ret = cdev->drv->thaw(cdev);
1853 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1855 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1857 spin_lock_irq(sch->lock);
1858 if (cio_is_console(sch->schid)) {
1859 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1863 * While we were sleeping, devices may have gone or become
1864 * available again. Kick re-detection.
1866 cdev->private->flags.resuming = 1;
1867 cdev->private->path_new_mask = LPM_ANYPATH;
1868 css_sched_sch_todo(sch, SCH_TODO_EVAL);
1869 spin_unlock_irq(sch->lock);
1870 css_wait_for_slow_path();
1872 /* cdev may have been moved to a different subchannel. */
1873 sch = to_subchannel(cdev->dev.parent);
1874 spin_lock_irq(sch->lock);
1875 if (cdev->private->state != DEV_STATE_ONLINE &&
1876 cdev->private->state != DEV_STATE_OFFLINE)
1879 ccw_device_recognition(cdev);
1880 spin_unlock_irq(sch->lock);
1881 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1882 cdev->private->state == DEV_STATE_DISCONNECTED);
1883 spin_lock_irq(sch->lock);
1886 cdev->private->flags.resuming = 0;
1887 spin_unlock_irq(sch->lock);
1890 static int resume_handle_boxed(struct ccw_device *cdev)
1892 cdev->private->state = DEV_STATE_BOXED;
1893 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1895 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1899 static int resume_handle_disc(struct ccw_device *cdev)
1901 cdev->private->state = DEV_STATE_DISCONNECTED;
1902 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1904 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1908 static int ccw_device_pm_restore(struct device *dev)
1910 struct ccw_device *cdev = to_ccwdev(dev);
1911 struct subchannel *sch;
1914 __ccw_device_pm_restore(cdev);
1915 sch = to_subchannel(cdev->dev.parent);
1916 spin_lock_irq(sch->lock);
1917 if (cio_is_console(sch->schid))
1920 /* check recognition results */
1921 switch (cdev->private->state) {
1922 case DEV_STATE_OFFLINE:
1923 case DEV_STATE_ONLINE:
1924 cdev->private->flags.donotify = 0;
1926 case DEV_STATE_BOXED:
1927 ret = resume_handle_boxed(cdev);
1932 ret = resume_handle_disc(cdev);
1937 /* check if the device type has changed */
1938 if (!ccw_device_test_sense_data(cdev)) {
1939 ccw_device_update_sense_data(cdev);
1940 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1947 if (ccw_device_online(cdev)) {
1948 ret = resume_handle_disc(cdev);
1953 spin_unlock_irq(sch->lock);
1954 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1955 spin_lock_irq(sch->lock);
1957 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1958 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1963 /* reenable cmf, if needed */
1964 if (cdev->private->cmb) {
1965 spin_unlock_irq(sch->lock);
1966 ret = ccw_set_cmf(cdev, 1);
1967 spin_lock_irq(sch->lock);
1969 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1970 "(rc=%d)\n", cdev->private->dev_id.ssid,
1971 cdev->private->dev_id.devno, ret);
1977 spin_unlock_irq(sch->lock);
1978 if (cdev->online && cdev->drv && cdev->drv->restore)
1979 ret = cdev->drv->restore(cdev);
1983 spin_unlock_irq(sch->lock);
1987 static const struct dev_pm_ops ccw_pm_ops = {
1988 .prepare = ccw_device_pm_prepare,
1989 .complete = ccw_device_pm_complete,
1990 .freeze = ccw_device_pm_freeze,
1991 .thaw = ccw_device_pm_thaw,
1992 .restore = ccw_device_pm_restore,
1995 static struct bus_type ccw_bus_type = {
1997 .match = ccw_bus_match,
1998 .uevent = ccw_uevent,
1999 .probe = ccw_device_probe,
2000 .remove = ccw_device_remove,
2001 .shutdown = ccw_device_shutdown,
2006 * ccw_driver_register() - register a ccw driver
2007 * @cdriver: driver to be registered
2009 * This function is mainly a wrapper around driver_register().
2011 * %0 on success and a negative error value on failure.
2013 int ccw_driver_register(struct ccw_driver *cdriver)
2015 struct device_driver *drv = &cdriver->driver;
2017 drv->bus = &ccw_bus_type;
2019 return driver_register(drv);
2023 * ccw_driver_unregister() - deregister a ccw driver
2024 * @cdriver: driver to be deregistered
2026 * This function is mainly a wrapper around driver_unregister().
2028 void ccw_driver_unregister(struct ccw_driver *cdriver)
2030 driver_unregister(&cdriver->driver);
2033 static void ccw_device_todo(struct work_struct *work)
2035 struct ccw_device_private *priv;
2036 struct ccw_device *cdev;
2037 struct subchannel *sch;
2038 enum cdev_todo todo;
2040 priv = container_of(work, struct ccw_device_private, todo_work);
2042 sch = to_subchannel(cdev->dev.parent);
2043 /* Find out todo. */
2044 spin_lock_irq(cdev->ccwlock);
2046 priv->todo = CDEV_TODO_NOTHING;
2047 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2048 priv->dev_id.ssid, priv->dev_id.devno, todo);
2049 spin_unlock_irq(cdev->ccwlock);
2052 case CDEV_TODO_ENABLE_CMF:
2055 case CDEV_TODO_REBIND:
2056 ccw_device_do_unbind_bind(cdev);
2058 case CDEV_TODO_REGISTER:
2059 io_subchannel_register(cdev);
2061 case CDEV_TODO_UNREG_EVAL:
2062 if (!sch_is_pseudo_sch(sch))
2063 css_schedule_eval(sch->schid);
2065 case CDEV_TODO_UNREG:
2066 if (sch_is_pseudo_sch(sch))
2067 ccw_device_unregister(cdev);
2069 ccw_device_call_sch_unregister(cdev);
2074 /* Release workqueue ref. */
2075 put_device(&cdev->dev);
2079 * ccw_device_sched_todo - schedule ccw device operation
2083 * Schedule the operation identified by @todo to be performed on the slow path
2084 * workqueue. Do nothing if another operation with higher priority is already
2085 * scheduled. Needs to be called with ccwdev lock held.
2087 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2089 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2090 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2092 if (cdev->private->todo >= todo)
2094 cdev->private->todo = todo;
2095 /* Get workqueue ref. */
2096 if (!get_device(&cdev->dev))
2098 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2099 /* Already queued, release workqueue ref. */
2100 put_device(&cdev->dev);
2105 * ccw_device_siosl() - initiate logging
2108 * This function is used to invoke model-dependent logging within the channel
2111 int ccw_device_siosl(struct ccw_device *cdev)
2113 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2115 return chsc_siosl(sch->schid);
2117 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2119 EXPORT_SYMBOL(ccw_device_set_online);
2120 EXPORT_SYMBOL(ccw_device_set_offline);
2121 EXPORT_SYMBOL(ccw_driver_register);
2122 EXPORT_SYMBOL(ccw_driver_unregister);
2123 EXPORT_SYMBOL(get_ccwdev_by_busid);