1 // SPDX-License-Identifier: GPL-1.0+
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/export.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/delay.h>
24 #include <linux/timer.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/sched/signal.h>
27 #include <linux/dma-mapping.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(struct timer_list *unused);
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 timer_setup(&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_RO(devtype);
602 static DEVICE_ATTR_RO(cutype);
603 static DEVICE_ATTR_RO(modalias);
604 static DEVICE_ATTR_RW(online);
605 static DEVICE_ATTR(availability, 0444, available_show, NULL);
606 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
607 static DEVICE_ATTR_RO(vpm);
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, const void *data)
648 struct ccw_device *cdev = to_ccwdev(dev);
649 struct ccw_dev_id *dev_id = (void *)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 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
692 sizeof(*cdev->private->dma_area));
693 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
694 /* Release reference of parent subchannel. */
695 put_device(cdev->dev.parent);
696 kfree(cdev->private);
700 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
702 struct ccw_device *cdev;
703 struct gen_pool *dma_pool;
705 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
708 cdev->private = kzalloc(sizeof(struct ccw_device_private),
709 GFP_KERNEL | GFP_DMA);
712 cdev->dev.coherent_dma_mask = sch->dev.coherent_dma_mask;
713 cdev->dev.dma_mask = sch->dev.dma_mask;
714 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
717 cdev->private->dma_pool = dma_pool;
718 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
719 sizeof(*cdev->private->dma_area));
720 if (!cdev->private->dma_area)
724 cio_gp_dma_destroy(dma_pool, &cdev->dev);
726 kfree(cdev->private);
730 return ERR_PTR(-ENOMEM);
733 static void ccw_device_todo(struct work_struct *work);
735 static int io_subchannel_initialize_dev(struct subchannel *sch,
736 struct ccw_device *cdev)
738 struct ccw_device_private *priv = cdev->private;
742 priv->int_class = IRQIO_CIO;
743 priv->state = DEV_STATE_NOT_OPER;
744 priv->dev_id.devno = sch->schib.pmcw.dev;
745 priv->dev_id.ssid = sch->schid.ssid;
747 INIT_WORK(&priv->todo_work, ccw_device_todo);
748 INIT_LIST_HEAD(&priv->cmb_list);
749 init_waitqueue_head(&priv->wait_q);
750 timer_setup(&priv->timer, ccw_device_timeout, 0);
752 atomic_set(&priv->onoff, 0);
753 cdev->ccwlock = sch->lock;
754 cdev->dev.parent = &sch->dev;
755 cdev->dev.release = ccw_device_release;
756 cdev->dev.groups = ccwdev_attr_groups;
757 /* Do first half of device_register. */
758 device_initialize(&cdev->dev);
759 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
760 cdev->private->dev_id.devno);
763 if (!get_device(&sch->dev)) {
767 priv->flags.initialized = 1;
768 spin_lock_irq(sch->lock);
769 sch_set_cdev(sch, cdev);
770 spin_unlock_irq(sch->lock);
774 /* Release reference from device_initialize(). */
775 put_device(&cdev->dev);
779 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
781 struct ccw_device *cdev;
784 cdev = io_subchannel_allocate_dev(sch);
786 ret = io_subchannel_initialize_dev(sch, cdev);
793 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
795 static void sch_create_and_recog_new_device(struct subchannel *sch)
797 struct ccw_device *cdev;
799 /* Need to allocate a new ccw device. */
800 cdev = io_subchannel_create_ccwdev(sch);
802 /* OK, we did everything we could... */
803 css_sch_device_unregister(sch);
806 /* Start recognition for the new ccw device. */
807 io_subchannel_recog(cdev, sch);
811 * Register recognized device.
813 static void io_subchannel_register(struct ccw_device *cdev)
815 struct subchannel *sch;
816 int ret, adjust_init_count = 1;
819 sch = to_subchannel(cdev->dev.parent);
821 * Check if subchannel is still registered. It may have become
822 * unregistered if a machine check hit us after finishing
823 * device recognition but before the register work could be
826 if (!device_is_registered(&sch->dev))
828 css_update_ssd_info(sch);
830 * io_subchannel_register() will also be called after device
831 * recognition has been done for a boxed device (which will already
832 * be registered). We need to reprobe since we may now have sense id
835 if (device_is_registered(&cdev->dev)) {
837 ret = device_reprobe(&cdev->dev);
839 /* We can't do much here. */
840 CIO_MSG_EVENT(0, "device_reprobe() returned"
841 " %d for 0.%x.%04x\n", ret,
842 cdev->private->dev_id.ssid,
843 cdev->private->dev_id.devno);
845 adjust_init_count = 0;
849 * Now we know this subchannel will stay, we can throw
850 * our delayed uevent.
852 dev_set_uevent_suppress(&sch->dev, 0);
853 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
854 /* make it known to the system */
855 ret = ccw_device_add(cdev);
857 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
858 cdev->private->dev_id.ssid,
859 cdev->private->dev_id.devno, ret);
860 spin_lock_irqsave(sch->lock, flags);
861 sch_set_cdev(sch, NULL);
862 spin_unlock_irqrestore(sch->lock, flags);
863 /* Release initial device reference. */
864 put_device(&cdev->dev);
868 cdev->private->flags.recog_done = 1;
869 wake_up(&cdev->private->wait_q);
871 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
872 wake_up(&ccw_device_init_wq);
875 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
877 struct subchannel *sch;
879 /* Get subchannel reference for local processing. */
880 if (!get_device(cdev->dev.parent))
882 sch = to_subchannel(cdev->dev.parent);
883 css_sch_device_unregister(sch);
884 /* Release subchannel reference for local processing. */
885 put_device(&sch->dev);
889 * subchannel recognition done. Called from the state machine.
892 io_subchannel_recog_done(struct ccw_device *cdev)
894 if (css_init_done == 0) {
895 cdev->private->flags.recog_done = 1;
898 switch (cdev->private->state) {
899 case DEV_STATE_BOXED:
900 /* Device did not respond in time. */
901 case DEV_STATE_NOT_OPER:
902 cdev->private->flags.recog_done = 1;
903 /* Remove device found not operational. */
904 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
905 if (atomic_dec_and_test(&ccw_device_init_count))
906 wake_up(&ccw_device_init_wq);
908 case DEV_STATE_OFFLINE:
910 * We can't register the device in interrupt context so
911 * we schedule a work item.
913 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
918 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
920 /* Increase counter of devices currently in recognition. */
921 atomic_inc(&ccw_device_init_count);
923 /* Start async. device sensing. */
924 spin_lock_irq(sch->lock);
925 ccw_device_recognition(cdev);
926 spin_unlock_irq(sch->lock);
929 static int ccw_device_move_to_sch(struct ccw_device *cdev,
930 struct subchannel *sch)
932 struct subchannel *old_sch;
933 int rc, old_enabled = 0;
935 old_sch = to_subchannel(cdev->dev.parent);
936 /* Obtain child reference for new parent. */
937 if (!get_device(&sch->dev))
940 if (!sch_is_pseudo_sch(old_sch)) {
941 spin_lock_irq(old_sch->lock);
942 old_enabled = old_sch->schib.pmcw.ena;
945 rc = cio_disable_subchannel(old_sch);
946 spin_unlock_irq(old_sch->lock);
948 /* Release child reference for new parent. */
949 put_device(&sch->dev);
954 mutex_lock(&sch->reg_mutex);
955 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
956 mutex_unlock(&sch->reg_mutex);
958 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
959 cdev->private->dev_id.ssid,
960 cdev->private->dev_id.devno, sch->schid.ssid,
961 sch->schib.pmcw.dev, rc);
963 /* Try to reenable the old subchannel. */
964 spin_lock_irq(old_sch->lock);
965 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
966 spin_unlock_irq(old_sch->lock);
968 /* Release child reference for new parent. */
969 put_device(&sch->dev);
972 /* Clean up old subchannel. */
973 if (!sch_is_pseudo_sch(old_sch)) {
974 spin_lock_irq(old_sch->lock);
975 sch_set_cdev(old_sch, NULL);
976 spin_unlock_irq(old_sch->lock);
977 css_schedule_eval(old_sch->schid);
979 /* Release child reference for old parent. */
980 put_device(&old_sch->dev);
981 /* Initialize new subchannel. */
982 spin_lock_irq(sch->lock);
983 cdev->ccwlock = sch->lock;
984 if (!sch_is_pseudo_sch(sch))
985 sch_set_cdev(sch, cdev);
986 spin_unlock_irq(sch->lock);
987 if (!sch_is_pseudo_sch(sch))
988 css_update_ssd_info(sch);
992 static int ccw_device_move_to_orph(struct ccw_device *cdev)
994 struct subchannel *sch = to_subchannel(cdev->dev.parent);
995 struct channel_subsystem *css = to_css(sch->dev.parent);
997 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1000 static void io_subchannel_irq(struct subchannel *sch)
1002 struct ccw_device *cdev;
1004 cdev = sch_get_cdev(sch);
1006 CIO_TRACE_EVENT(6, "IRQ");
1007 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1009 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1011 inc_irq_stat(IRQIO_CIO);
1014 void io_subchannel_init_config(struct subchannel *sch)
1016 memset(&sch->config, 0, sizeof(sch->config));
1017 sch->config.csense = 1;
1020 static void io_subchannel_init_fields(struct subchannel *sch)
1022 if (cio_is_console(sch->schid))
1025 sch->opm = chp_get_sch_opm(sch);
1026 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1027 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1029 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1030 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1031 sch->schib.pmcw.dev, sch->schid.ssid,
1032 sch->schid.sch_no, sch->schib.pmcw.pim,
1033 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1035 io_subchannel_init_config(sch);
1039 * Note: We always return 0 so that we bind to the device even on error.
1040 * This is needed so that our remove function is called on unregister.
1042 static int io_subchannel_probe(struct subchannel *sch)
1044 struct io_subchannel_private *io_priv;
1045 struct ccw_device *cdev;
1048 if (cio_is_console(sch->schid)) {
1049 rc = sysfs_create_group(&sch->dev.kobj,
1050 &io_subchannel_attr_group);
1052 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1053 "attributes for subchannel "
1054 "0.%x.%04x (rc=%d)\n",
1055 sch->schid.ssid, sch->schid.sch_no, rc);
1057 * The console subchannel already has an associated ccw_device.
1058 * Throw the delayed uevent for the subchannel, register
1059 * the ccw_device and exit.
1061 dev_set_uevent_suppress(&sch->dev, 0);
1062 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1063 cdev = sch_get_cdev(sch);
1064 rc = ccw_device_add(cdev);
1066 /* Release online reference. */
1067 put_device(&cdev->dev);
1070 if (atomic_dec_and_test(&ccw_device_init_count))
1071 wake_up(&ccw_device_init_wq);
1074 io_subchannel_init_fields(sch);
1075 rc = cio_commit_config(sch);
1078 rc = sysfs_create_group(&sch->dev.kobj,
1079 &io_subchannel_attr_group);
1082 /* Allocate I/O subchannel private data. */
1083 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1087 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1088 sizeof(*io_priv->dma_area),
1089 &io_priv->dma_area_dma, GFP_KERNEL);
1090 if (!io_priv->dma_area) {
1095 set_io_private(sch, io_priv);
1096 css_schedule_eval(sch->schid);
1100 spin_lock_irq(sch->lock);
1101 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1102 spin_unlock_irq(sch->lock);
1106 static int io_subchannel_remove(struct subchannel *sch)
1108 struct io_subchannel_private *io_priv = to_io_private(sch);
1109 struct ccw_device *cdev;
1111 cdev = sch_get_cdev(sch);
1115 ccw_device_unregister(cdev);
1116 spin_lock_irq(sch->lock);
1117 sch_set_cdev(sch, NULL);
1118 set_io_private(sch, NULL);
1119 spin_unlock_irq(sch->lock);
1121 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1122 io_priv->dma_area, io_priv->dma_area_dma);
1124 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1128 static void io_subchannel_verify(struct subchannel *sch)
1130 struct ccw_device *cdev;
1132 cdev = sch_get_cdev(sch);
1134 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1137 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1139 struct ccw_device *cdev;
1141 cdev = sch_get_cdev(sch);
1144 if (cio_update_schib(sch))
1146 /* Check for I/O on path. */
1147 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1149 if (cdev->private->state == DEV_STATE_ONLINE) {
1150 ccw_device_kill_io(cdev);
1156 /* Trigger path verification. */
1157 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1161 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1164 static int io_subchannel_chp_event(struct subchannel *sch,
1165 struct chp_link *link, int event)
1167 struct ccw_device *cdev = sch_get_cdev(sch);
1170 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1178 cdev->private->path_gone_mask |= mask;
1179 io_subchannel_terminate_path(sch, mask);
1185 cdev->private->path_new_mask |= mask;
1186 io_subchannel_verify(sch);
1189 if (cio_update_schib(sch))
1192 cdev->private->path_gone_mask |= mask;
1193 io_subchannel_terminate_path(sch, mask);
1196 if (cio_update_schib(sch))
1198 sch->lpm |= mask & sch->opm;
1200 cdev->private->path_new_mask |= mask;
1201 io_subchannel_verify(sch);
1207 static void io_subchannel_quiesce(struct subchannel *sch)
1209 struct ccw_device *cdev;
1212 spin_lock_irq(sch->lock);
1213 cdev = sch_get_cdev(sch);
1214 if (cio_is_console(sch->schid))
1216 if (!sch->schib.pmcw.ena)
1218 ret = cio_disable_subchannel(sch);
1222 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1223 while (ret == -EBUSY) {
1224 cdev->private->state = DEV_STATE_QUIESCE;
1225 cdev->private->iretry = 255;
1226 ret = ccw_device_cancel_halt_clear(cdev);
1227 if (ret == -EBUSY) {
1228 ccw_device_set_timeout(cdev, HZ/10);
1229 spin_unlock_irq(sch->lock);
1230 wait_event(cdev->private->wait_q,
1231 cdev->private->state != DEV_STATE_QUIESCE);
1232 spin_lock_irq(sch->lock);
1234 ret = cio_disable_subchannel(sch);
1237 spin_unlock_irq(sch->lock);
1240 static void io_subchannel_shutdown(struct subchannel *sch)
1242 io_subchannel_quiesce(sch);
1245 static int device_is_disconnected(struct ccw_device *cdev)
1249 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1250 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1253 static int recovery_check(struct device *dev, void *data)
1255 struct ccw_device *cdev = to_ccwdev(dev);
1256 struct subchannel *sch;
1259 spin_lock_irq(cdev->ccwlock);
1260 switch (cdev->private->state) {
1261 case DEV_STATE_ONLINE:
1262 sch = to_subchannel(cdev->dev.parent);
1263 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1266 case DEV_STATE_DISCONNECTED:
1267 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1268 cdev->private->dev_id.ssid,
1269 cdev->private->dev_id.devno);
1270 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1273 case DEV_STATE_DISCONNECTED_SENSE_ID:
1277 spin_unlock_irq(cdev->ccwlock);
1282 static void recovery_work_func(struct work_struct *unused)
1286 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1288 spin_lock_irq(&recovery_lock);
1289 if (!timer_pending(&recovery_timer)) {
1290 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1292 mod_timer(&recovery_timer, jiffies +
1293 recovery_delay[recovery_phase] * HZ);
1295 spin_unlock_irq(&recovery_lock);
1297 CIO_MSG_EVENT(3, "recovery: end\n");
1300 static DECLARE_WORK(recovery_work, recovery_work_func);
1302 static void recovery_func(struct timer_list *unused)
1305 * We can't do our recovery in softirq context and it's not
1306 * performance critical, so we schedule it.
1308 schedule_work(&recovery_work);
1311 void ccw_device_schedule_recovery(void)
1313 unsigned long flags;
1315 CIO_MSG_EVENT(3, "recovery: schedule\n");
1316 spin_lock_irqsave(&recovery_lock, flags);
1317 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1319 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1321 spin_unlock_irqrestore(&recovery_lock, flags);
1324 static int purge_fn(struct device *dev, void *data)
1326 struct ccw_device *cdev = to_ccwdev(dev);
1327 struct ccw_dev_id *id = &cdev->private->dev_id;
1329 spin_lock_irq(cdev->ccwlock);
1330 if (is_blacklisted(id->ssid, id->devno) &&
1331 (cdev->private->state == DEV_STATE_OFFLINE) &&
1332 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1333 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1335 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1336 atomic_set(&cdev->private->onoff, 0);
1338 spin_unlock_irq(cdev->ccwlock);
1339 /* Abort loop in case of pending signal. */
1340 if (signal_pending(current))
1347 * ccw_purge_blacklisted - purge unused, blacklisted devices
1349 * Unregister all ccw devices that are offline and on the blacklist.
1351 int ccw_purge_blacklisted(void)
1353 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1354 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1358 void ccw_device_set_disconnected(struct ccw_device *cdev)
1362 ccw_device_set_timeout(cdev, 0);
1363 cdev->private->flags.fake_irb = 0;
1364 cdev->private->state = DEV_STATE_DISCONNECTED;
1366 ccw_device_schedule_recovery();
1369 void ccw_device_set_notoper(struct ccw_device *cdev)
1371 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1373 CIO_TRACE_EVENT(2, "notoper");
1374 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1375 ccw_device_set_timeout(cdev, 0);
1376 cio_disable_subchannel(sch);
1377 cdev->private->state = DEV_STATE_NOT_OPER;
1380 enum io_sch_action {
1384 IO_SCH_UNREG_ATTACH,
1392 static enum io_sch_action sch_get_action(struct subchannel *sch)
1394 struct ccw_device *cdev;
1396 cdev = sch_get_cdev(sch);
1397 if (cio_update_schib(sch)) {
1398 /* Not operational. */
1400 return IO_SCH_UNREG;
1401 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1402 return IO_SCH_UNREG;
1403 return IO_SCH_ORPH_UNREG;
1407 return IO_SCH_ATTACH;
1408 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1409 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1410 return IO_SCH_UNREG_ATTACH;
1411 return IO_SCH_ORPH_ATTACH;
1413 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1414 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1415 return IO_SCH_UNREG;
1418 if (device_is_disconnected(cdev))
1419 return IO_SCH_REPROBE;
1420 if (cdev->online && !cdev->private->flags.resuming)
1421 return IO_SCH_VERIFY;
1422 if (cdev->private->state == DEV_STATE_NOT_OPER)
1423 return IO_SCH_UNREG_ATTACH;
1428 * io_subchannel_sch_event - process subchannel event
1430 * @process: non-zero if function is called in process context
1432 * An unspecified event occurred for this subchannel. Adjust data according
1433 * to the current operational state of the subchannel and device. Return
1434 * zero when the event has been handled sufficiently or -EAGAIN when this
1435 * function should be called again in process context.
1437 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1439 unsigned long flags;
1440 struct ccw_device *cdev;
1441 struct ccw_dev_id dev_id;
1442 enum io_sch_action action;
1445 spin_lock_irqsave(sch->lock, flags);
1446 if (!device_is_registered(&sch->dev))
1448 if (work_pending(&sch->todo_work))
1450 cdev = sch_get_cdev(sch);
1451 if (cdev && work_pending(&cdev->private->todo_work))
1453 action = sch_get_action(sch);
1454 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1455 sch->schid.ssid, sch->schid.sch_no, process,
1457 /* Perform immediate actions while holding the lock. */
1459 case IO_SCH_REPROBE:
1460 /* Trigger device recognition. */
1461 ccw_device_trigger_reprobe(cdev);
1465 /* Trigger path verification. */
1466 io_subchannel_verify(sch);
1470 ccw_device_set_disconnected(cdev);
1473 case IO_SCH_ORPH_UNREG:
1474 case IO_SCH_ORPH_ATTACH:
1475 ccw_device_set_disconnected(cdev);
1477 case IO_SCH_UNREG_ATTACH:
1481 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1483 * Note: delayed work triggered by this event
1484 * and repeated calls to sch_event are synchronized
1485 * by the above check for work_pending(cdev).
1487 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1489 ccw_device_set_notoper(cdev);
1497 spin_unlock_irqrestore(sch->lock, flags);
1498 /* All other actions require process context. */
1501 /* Handle attached ccw device. */
1503 case IO_SCH_ORPH_UNREG:
1504 case IO_SCH_ORPH_ATTACH:
1505 /* Move ccw device to orphanage. */
1506 rc = ccw_device_move_to_orph(cdev);
1510 case IO_SCH_UNREG_ATTACH:
1511 spin_lock_irqsave(sch->lock, flags);
1512 if (cdev->private->flags.resuming) {
1513 /* Device will be handled later. */
1517 sch_set_cdev(sch, NULL);
1518 spin_unlock_irqrestore(sch->lock, flags);
1519 /* Unregister ccw device. */
1520 ccw_device_unregister(cdev);
1525 /* Handle subchannel. */
1527 case IO_SCH_ORPH_UNREG:
1529 if (!cdev || !cdev->private->flags.resuming)
1530 css_sch_device_unregister(sch);
1532 case IO_SCH_ORPH_ATTACH:
1533 case IO_SCH_UNREG_ATTACH:
1535 dev_id.ssid = sch->schid.ssid;
1536 dev_id.devno = sch->schib.pmcw.dev;
1537 cdev = get_ccwdev_by_dev_id(&dev_id);
1539 sch_create_and_recog_new_device(sch);
1542 rc = ccw_device_move_to_sch(cdev, sch);
1544 /* Release reference from get_ccwdev_by_dev_id() */
1545 put_device(&cdev->dev);
1548 spin_lock_irqsave(sch->lock, flags);
1549 ccw_device_trigger_reprobe(cdev);
1550 spin_unlock_irqrestore(sch->lock, flags);
1551 /* Release reference from get_ccwdev_by_dev_id() */
1552 put_device(&cdev->dev);
1560 spin_unlock_irqrestore(sch->lock, flags);
1565 static void ccw_device_set_int_class(struct ccw_device *cdev)
1567 struct ccw_driver *cdrv = cdev->drv;
1569 /* Note: we interpret class 0 in this context as an uninitialized
1570 * field since it translates to a non-I/O interrupt class. */
1571 if (cdrv->int_class != 0)
1572 cdev->private->int_class = cdrv->int_class;
1574 cdev->private->int_class = IRQIO_CIO;
1577 #ifdef CONFIG_CCW_CONSOLE
1578 int __init ccw_device_enable_console(struct ccw_device *cdev)
1580 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1583 if (!cdev->drv || !cdev->handler)
1586 io_subchannel_init_fields(sch);
1587 rc = cio_commit_config(sch);
1590 sch->driver = &io_subchannel_driver;
1591 io_subchannel_recog(cdev, sch);
1592 /* Now wait for the async. recognition to come to an end. */
1593 spin_lock_irq(cdev->ccwlock);
1594 while (!dev_fsm_final_state(cdev))
1595 ccw_device_wait_idle(cdev);
1597 /* Hold on to an extra reference while device is online. */
1598 get_device(&cdev->dev);
1599 rc = ccw_device_online(cdev);
1603 while (!dev_fsm_final_state(cdev))
1604 ccw_device_wait_idle(cdev);
1606 if (cdev->private->state == DEV_STATE_ONLINE)
1611 spin_unlock_irq(cdev->ccwlock);
1612 if (rc) /* Give up online reference since onlining failed. */
1613 put_device(&cdev->dev);
1617 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1619 struct io_subchannel_private *io_priv;
1620 struct ccw_device *cdev;
1621 struct subchannel *sch;
1623 sch = cio_probe_console();
1625 return ERR_CAST(sch);
1627 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1630 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1631 sizeof(*io_priv->dma_area),
1632 &io_priv->dma_area_dma, GFP_KERNEL);
1633 if (!io_priv->dma_area)
1635 set_io_private(sch, io_priv);
1636 cdev = io_subchannel_create_ccwdev(sch);
1638 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1639 io_priv->dma_area, io_priv->dma_area_dma);
1640 set_io_private(sch, NULL);
1641 put_device(&sch->dev);
1646 ccw_device_set_int_class(cdev);
1652 put_device(&sch->dev);
1653 return ERR_PTR(-ENOMEM);
1656 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1658 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1659 struct io_subchannel_private *io_priv = to_io_private(sch);
1661 set_io_private(sch, NULL);
1662 put_device(&sch->dev);
1663 put_device(&cdev->dev);
1664 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1665 io_priv->dma_area, io_priv->dma_area_dma);
1670 * ccw_device_wait_idle() - busy wait for device to become idle
1673 * Poll until activity control is zero, that is, no function or data
1674 * transfer is pending/active.
1675 * Called with device lock being held.
1677 void ccw_device_wait_idle(struct ccw_device *cdev)
1679 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1683 if (sch->schib.scsw.cmd.actl == 0)
1689 static int ccw_device_pm_restore(struct device *dev);
1691 int ccw_device_force_console(struct ccw_device *cdev)
1693 return ccw_device_pm_restore(&cdev->dev);
1695 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1699 * get_ccwdev_by_busid() - obtain device from a bus id
1700 * @cdrv: driver the device is owned by
1701 * @bus_id: bus id of the device to be searched
1703 * This function searches all devices owned by @cdrv for a device with a bus
1704 * id matching @bus_id.
1706 * If a match is found, its reference count of the found device is increased
1707 * and it is returned; else %NULL is returned.
1709 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1714 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
1716 return dev ? to_ccwdev(dev) : NULL;
1719 /************************** device driver handling ************************/
1721 /* This is the implementation of the ccw_driver class. The probe, remove
1722 * and release methods are initially very similar to the device_driver
1723 * implementations, with the difference that they have ccw_device
1726 * A ccw driver also contains the information that is needed for
1730 ccw_device_probe (struct device *dev)
1732 struct ccw_device *cdev = to_ccwdev(dev);
1733 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1736 cdev->drv = cdrv; /* to let the driver call _set_online */
1737 ccw_device_set_int_class(cdev);
1738 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1741 cdev->private->int_class = IRQIO_CIO;
1748 static int ccw_device_remove(struct device *dev)
1750 struct ccw_device *cdev = to_ccwdev(dev);
1751 struct ccw_driver *cdrv = cdev->drv;
1752 struct subchannel *sch;
1758 spin_lock_irq(cdev->ccwlock);
1761 ret = ccw_device_offline(cdev);
1762 spin_unlock_irq(cdev->ccwlock);
1764 wait_event(cdev->private->wait_q,
1765 dev_fsm_final_state(cdev));
1767 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1768 "device 0.%x.%04x\n",
1769 ret, cdev->private->dev_id.ssid,
1770 cdev->private->dev_id.devno);
1771 /* Give up reference obtained in ccw_device_set_online(). */
1772 put_device(&cdev->dev);
1773 spin_lock_irq(cdev->ccwlock);
1775 ccw_device_set_timeout(cdev, 0);
1777 cdev->private->int_class = IRQIO_CIO;
1778 sch = to_subchannel(cdev->dev.parent);
1779 spin_unlock_irq(cdev->ccwlock);
1780 io_subchannel_quiesce(sch);
1781 __disable_cmf(cdev);
1786 static void ccw_device_shutdown(struct device *dev)
1788 struct ccw_device *cdev;
1790 cdev = to_ccwdev(dev);
1791 if (cdev->drv && cdev->drv->shutdown)
1792 cdev->drv->shutdown(cdev);
1793 __disable_cmf(cdev);
1796 static int ccw_device_pm_prepare(struct device *dev)
1798 struct ccw_device *cdev = to_ccwdev(dev);
1800 if (work_pending(&cdev->private->todo_work))
1802 /* Fail while device is being set online/offline. */
1803 if (atomic_read(&cdev->private->onoff))
1806 if (cdev->online && cdev->drv && cdev->drv->prepare)
1807 return cdev->drv->prepare(cdev);
1812 static void ccw_device_pm_complete(struct device *dev)
1814 struct ccw_device *cdev = to_ccwdev(dev);
1816 if (cdev->online && cdev->drv && cdev->drv->complete)
1817 cdev->drv->complete(cdev);
1820 static int ccw_device_pm_freeze(struct device *dev)
1822 struct ccw_device *cdev = to_ccwdev(dev);
1823 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1824 int ret, cm_enabled;
1826 /* Fail suspend while device is in transistional state. */
1827 if (!dev_fsm_final_state(cdev))
1831 if (cdev->drv && cdev->drv->freeze) {
1832 ret = cdev->drv->freeze(cdev);
1837 spin_lock_irq(sch->lock);
1838 cm_enabled = cdev->private->cmb != NULL;
1839 spin_unlock_irq(sch->lock);
1841 /* Don't have the css write on memory. */
1842 ret = ccw_set_cmf(cdev, 0);
1846 /* From here on, disallow device driver I/O. */
1847 spin_lock_irq(sch->lock);
1848 ret = cio_disable_subchannel(sch);
1849 spin_unlock_irq(sch->lock);
1854 static int ccw_device_pm_thaw(struct device *dev)
1856 struct ccw_device *cdev = to_ccwdev(dev);
1857 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1858 int ret, cm_enabled;
1863 spin_lock_irq(sch->lock);
1864 /* Allow device driver I/O again. */
1865 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1866 cm_enabled = cdev->private->cmb != NULL;
1867 spin_unlock_irq(sch->lock);
1872 ret = ccw_set_cmf(cdev, 1);
1877 if (cdev->drv && cdev->drv->thaw)
1878 ret = cdev->drv->thaw(cdev);
1883 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1885 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1887 spin_lock_irq(sch->lock);
1888 if (cio_is_console(sch->schid)) {
1889 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1893 * While we were sleeping, devices may have gone or become
1894 * available again. Kick re-detection.
1896 cdev->private->flags.resuming = 1;
1897 cdev->private->path_new_mask = LPM_ANYPATH;
1898 css_sched_sch_todo(sch, SCH_TODO_EVAL);
1899 spin_unlock_irq(sch->lock);
1900 css_wait_for_slow_path();
1902 /* cdev may have been moved to a different subchannel. */
1903 sch = to_subchannel(cdev->dev.parent);
1904 spin_lock_irq(sch->lock);
1905 if (cdev->private->state != DEV_STATE_ONLINE &&
1906 cdev->private->state != DEV_STATE_OFFLINE)
1909 ccw_device_recognition(cdev);
1910 spin_unlock_irq(sch->lock);
1911 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1912 cdev->private->state == DEV_STATE_DISCONNECTED);
1913 spin_lock_irq(sch->lock);
1916 cdev->private->flags.resuming = 0;
1917 spin_unlock_irq(sch->lock);
1920 static int resume_handle_boxed(struct ccw_device *cdev)
1922 cdev->private->state = DEV_STATE_BOXED;
1923 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1925 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1929 static int resume_handle_disc(struct ccw_device *cdev)
1931 cdev->private->state = DEV_STATE_DISCONNECTED;
1932 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1934 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1938 static int ccw_device_pm_restore(struct device *dev)
1940 struct ccw_device *cdev = to_ccwdev(dev);
1941 struct subchannel *sch;
1944 __ccw_device_pm_restore(cdev);
1945 sch = to_subchannel(cdev->dev.parent);
1946 spin_lock_irq(sch->lock);
1947 if (cio_is_console(sch->schid))
1950 /* check recognition results */
1951 switch (cdev->private->state) {
1952 case DEV_STATE_OFFLINE:
1953 case DEV_STATE_ONLINE:
1954 cdev->private->flags.donotify = 0;
1956 case DEV_STATE_BOXED:
1957 ret = resume_handle_boxed(cdev);
1962 ret = resume_handle_disc(cdev);
1967 /* check if the device type has changed */
1968 if (!ccw_device_test_sense_data(cdev)) {
1969 ccw_device_update_sense_data(cdev);
1970 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1977 if (ccw_device_online(cdev)) {
1978 ret = resume_handle_disc(cdev);
1983 spin_unlock_irq(sch->lock);
1984 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1985 spin_lock_irq(sch->lock);
1987 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1988 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1993 /* reenable cmf, if needed */
1994 if (cdev->private->cmb) {
1995 spin_unlock_irq(sch->lock);
1996 ret = ccw_set_cmf(cdev, 1);
1997 spin_lock_irq(sch->lock);
1999 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2000 "(rc=%d)\n", cdev->private->dev_id.ssid,
2001 cdev->private->dev_id.devno, ret);
2007 spin_unlock_irq(sch->lock);
2008 if (cdev->online && cdev->drv && cdev->drv->restore)
2009 ret = cdev->drv->restore(cdev);
2013 spin_unlock_irq(sch->lock);
2017 static const struct dev_pm_ops ccw_pm_ops = {
2018 .prepare = ccw_device_pm_prepare,
2019 .complete = ccw_device_pm_complete,
2020 .freeze = ccw_device_pm_freeze,
2021 .thaw = ccw_device_pm_thaw,
2022 .restore = ccw_device_pm_restore,
2025 static struct bus_type ccw_bus_type = {
2027 .match = ccw_bus_match,
2028 .uevent = ccw_uevent,
2029 .probe = ccw_device_probe,
2030 .remove = ccw_device_remove,
2031 .shutdown = ccw_device_shutdown,
2036 * ccw_driver_register() - register a ccw driver
2037 * @cdriver: driver to be registered
2039 * This function is mainly a wrapper around driver_register().
2041 * %0 on success and a negative error value on failure.
2043 int ccw_driver_register(struct ccw_driver *cdriver)
2045 struct device_driver *drv = &cdriver->driver;
2047 drv->bus = &ccw_bus_type;
2049 return driver_register(drv);
2053 * ccw_driver_unregister() - deregister a ccw driver
2054 * @cdriver: driver to be deregistered
2056 * This function is mainly a wrapper around driver_unregister().
2058 void ccw_driver_unregister(struct ccw_driver *cdriver)
2060 driver_unregister(&cdriver->driver);
2063 static void ccw_device_todo(struct work_struct *work)
2065 struct ccw_device_private *priv;
2066 struct ccw_device *cdev;
2067 struct subchannel *sch;
2068 enum cdev_todo todo;
2070 priv = container_of(work, struct ccw_device_private, todo_work);
2072 sch = to_subchannel(cdev->dev.parent);
2073 /* Find out todo. */
2074 spin_lock_irq(cdev->ccwlock);
2076 priv->todo = CDEV_TODO_NOTHING;
2077 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2078 priv->dev_id.ssid, priv->dev_id.devno, todo);
2079 spin_unlock_irq(cdev->ccwlock);
2082 case CDEV_TODO_ENABLE_CMF:
2085 case CDEV_TODO_REBIND:
2086 ccw_device_do_unbind_bind(cdev);
2088 case CDEV_TODO_REGISTER:
2089 io_subchannel_register(cdev);
2091 case CDEV_TODO_UNREG_EVAL:
2092 if (!sch_is_pseudo_sch(sch))
2093 css_schedule_eval(sch->schid);
2095 case CDEV_TODO_UNREG:
2096 if (sch_is_pseudo_sch(sch))
2097 ccw_device_unregister(cdev);
2099 ccw_device_call_sch_unregister(cdev);
2104 /* Release workqueue ref. */
2105 put_device(&cdev->dev);
2109 * ccw_device_sched_todo - schedule ccw device operation
2113 * Schedule the operation identified by @todo to be performed on the slow path
2114 * workqueue. Do nothing if another operation with higher priority is already
2115 * scheduled. Needs to be called with ccwdev lock held.
2117 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2119 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2120 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2122 if (cdev->private->todo >= todo)
2124 cdev->private->todo = todo;
2125 /* Get workqueue ref. */
2126 if (!get_device(&cdev->dev))
2128 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2129 /* Already queued, release workqueue ref. */
2130 put_device(&cdev->dev);
2135 * ccw_device_siosl() - initiate logging
2138 * This function is used to invoke model-dependent logging within the channel
2141 int ccw_device_siosl(struct ccw_device *cdev)
2143 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2145 return chsc_siosl(sch->schid);
2147 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2149 EXPORT_SYMBOL(ccw_device_set_online);
2150 EXPORT_SYMBOL(ccw_device_set_offline);
2151 EXPORT_SYMBOL(ccw_driver_register);
2152 EXPORT_SYMBOL(ccw_driver_unregister);
2153 EXPORT_SYMBOL(get_ccwdev_by_busid);