1 // SPDX-License-Identifier: GPL-1.0+
3 * bus driver for ccw devices
5 * Copyright IBM Corp. 2002, 2008
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_settle(void)
156 ret = wait_event_interruptible(ccw_device_init_wq,
157 atomic_read(&ccw_device_init_count) == 0);
160 flush_workqueue(cio_work_q);
164 static struct css_driver io_subchannel_driver = {
166 .owner = THIS_MODULE,
167 .name = "io_subchannel",
169 .subchannel_type = io_subchannel_ids,
170 .irq = io_subchannel_irq,
171 .sch_event = io_subchannel_sch_event,
172 .chp_event = io_subchannel_chp_event,
173 .probe = io_subchannel_probe,
174 .remove = io_subchannel_remove,
175 .shutdown = io_subchannel_shutdown,
176 .settle = io_subchannel_settle,
179 int __init io_subchannel_init(void)
183 timer_setup(&recovery_timer, recovery_func, 0);
184 ret = bus_register(&ccw_bus_type);
187 ret = css_driver_register(&io_subchannel_driver);
189 bus_unregister(&ccw_bus_type);
195 /************************ device handling **************************/
198 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
200 struct ccw_device *cdev = to_ccwdev(dev);
201 struct ccw_device_id *id = &(cdev->id);
203 if (id->dev_type != 0)
204 return sprintf(buf, "%04x/%02x\n",
205 id->dev_type, id->dev_model);
207 return sprintf(buf, "n/a\n");
211 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
213 struct ccw_device *cdev = to_ccwdev(dev);
214 struct ccw_device_id *id = &(cdev->id);
216 return sprintf(buf, "%04x/%02x\n",
217 id->cu_type, id->cu_model);
221 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
223 struct ccw_device *cdev = to_ccwdev(dev);
224 struct ccw_device_id *id = &(cdev->id);
227 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
229 return len > PAGE_SIZE ? PAGE_SIZE : len;
233 online_show (struct device *dev, struct device_attribute *attr, char *buf)
235 struct ccw_device *cdev = to_ccwdev(dev);
237 return sprintf(buf, cdev->online ? "1\n" : "0\n");
240 int ccw_device_is_orphan(struct ccw_device *cdev)
242 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
245 static void ccw_device_unregister(struct ccw_device *cdev)
247 if (device_is_registered(&cdev->dev)) {
248 /* Undo device_add(). */
249 device_del(&cdev->dev);
251 if (cdev->private->flags.initialized) {
252 cdev->private->flags.initialized = 0;
253 /* Release reference from device_initialize(). */
254 put_device(&cdev->dev);
258 static void io_subchannel_quiesce(struct subchannel *);
261 * ccw_device_set_offline() - disable a ccw device for I/O
262 * @cdev: target ccw device
264 * This function calls the driver's set_offline() function for @cdev, if
265 * given, and then disables @cdev.
267 * %0 on success and a negative error value on failure.
269 * enabled, ccw device lock not held
271 int ccw_device_set_offline(struct ccw_device *cdev)
273 struct subchannel *sch;
278 if (!cdev->online || !cdev->drv)
281 if (cdev->drv->set_offline) {
282 ret = cdev->drv->set_offline(cdev);
286 spin_lock_irq(cdev->ccwlock);
287 sch = to_subchannel(cdev->dev.parent);
289 /* Wait until a final state or DISCONNECTED is reached */
290 while (!dev_fsm_final_state(cdev) &&
291 cdev->private->state != DEV_STATE_DISCONNECTED) {
292 spin_unlock_irq(cdev->ccwlock);
293 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
294 cdev->private->state == DEV_STATE_DISCONNECTED));
295 spin_lock_irq(cdev->ccwlock);
298 ret = ccw_device_offline(cdev);
301 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
302 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
303 cdev->private->dev_id.devno);
306 state = cdev->private->state;
307 spin_unlock_irq(cdev->ccwlock);
308 io_subchannel_quiesce(sch);
309 spin_lock_irq(cdev->ccwlock);
310 cdev->private->state = state;
311 } while (ret == -EBUSY);
312 spin_unlock_irq(cdev->ccwlock);
313 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
314 cdev->private->state == DEV_STATE_DISCONNECTED));
315 /* Inform the user if set offline failed. */
316 if (cdev->private->state == DEV_STATE_BOXED) {
317 pr_warn("%s: The device entered boxed state while being set offline\n",
318 dev_name(&cdev->dev));
319 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
320 pr_warn("%s: The device stopped operating while being set offline\n",
321 dev_name(&cdev->dev));
323 /* Give up reference from ccw_device_set_online(). */
324 put_device(&cdev->dev);
328 cdev->private->state = DEV_STATE_OFFLINE;
329 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
330 spin_unlock_irq(cdev->ccwlock);
331 /* Give up reference from ccw_device_set_online(). */
332 put_device(&cdev->dev);
337 * ccw_device_set_online() - enable a ccw device for I/O
338 * @cdev: target ccw device
340 * This function first enables @cdev and then calls the driver's set_online()
341 * function for @cdev, if given. If set_online() returns an error, @cdev is
344 * %0 on success and a negative error value on failure.
346 * enabled, ccw device lock not held
348 int ccw_device_set_online(struct ccw_device *cdev)
355 if (cdev->online || !cdev->drv)
357 /* Hold on to an extra reference while device is online. */
358 if (!get_device(&cdev->dev))
361 spin_lock_irq(cdev->ccwlock);
362 ret = ccw_device_online(cdev);
363 spin_unlock_irq(cdev->ccwlock);
365 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
367 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
368 "device 0.%x.%04x\n",
369 ret, cdev->private->dev_id.ssid,
370 cdev->private->dev_id.devno);
371 /* Give up online reference since onlining failed. */
372 put_device(&cdev->dev);
375 spin_lock_irq(cdev->ccwlock);
376 /* Check if online processing was successful */
377 if ((cdev->private->state != DEV_STATE_ONLINE) &&
378 (cdev->private->state != DEV_STATE_W4SENSE)) {
379 spin_unlock_irq(cdev->ccwlock);
380 /* Inform the user that set online failed. */
381 if (cdev->private->state == DEV_STATE_BOXED) {
382 pr_warn("%s: Setting the device online failed because it is boxed\n",
383 dev_name(&cdev->dev));
384 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
385 pr_warn("%s: Setting the device online failed because it is not operational\n",
386 dev_name(&cdev->dev));
388 /* Give up online reference since onlining failed. */
389 put_device(&cdev->dev);
392 spin_unlock_irq(cdev->ccwlock);
393 if (cdev->drv->set_online)
394 ret = cdev->drv->set_online(cdev);
398 spin_lock_irq(cdev->ccwlock);
400 spin_unlock_irq(cdev->ccwlock);
404 spin_lock_irq(cdev->ccwlock);
405 /* Wait until a final state or DISCONNECTED is reached */
406 while (!dev_fsm_final_state(cdev) &&
407 cdev->private->state != DEV_STATE_DISCONNECTED) {
408 spin_unlock_irq(cdev->ccwlock);
409 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
410 cdev->private->state == DEV_STATE_DISCONNECTED));
411 spin_lock_irq(cdev->ccwlock);
413 ret2 = ccw_device_offline(cdev);
416 spin_unlock_irq(cdev->ccwlock);
417 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
418 cdev->private->state == DEV_STATE_DISCONNECTED));
419 /* Give up online reference since onlining failed. */
420 put_device(&cdev->dev);
424 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
425 "device 0.%x.%04x\n",
426 ret2, cdev->private->dev_id.ssid,
427 cdev->private->dev_id.devno);
428 cdev->private->state = DEV_STATE_OFFLINE;
429 spin_unlock_irq(cdev->ccwlock);
430 /* Give up online reference since onlining failed. */
431 put_device(&cdev->dev);
435 static int online_store_handle_offline(struct ccw_device *cdev)
437 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
438 spin_lock_irq(cdev->ccwlock);
439 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
440 spin_unlock_irq(cdev->ccwlock);
443 if (cdev->drv && cdev->drv->set_offline)
444 return ccw_device_set_offline(cdev);
448 static int online_store_recog_and_online(struct ccw_device *cdev)
450 /* Do device recognition, if needed. */
451 if (cdev->private->state == DEV_STATE_BOXED) {
452 spin_lock_irq(cdev->ccwlock);
453 ccw_device_recognition(cdev);
454 spin_unlock_irq(cdev->ccwlock);
455 wait_event(cdev->private->wait_q,
456 cdev->private->flags.recog_done);
457 if (cdev->private->state != DEV_STATE_OFFLINE)
458 /* recognition failed */
461 if (cdev->drv && cdev->drv->set_online)
462 return ccw_device_set_online(cdev);
466 static int online_store_handle_online(struct ccw_device *cdev, int force)
470 ret = online_store_recog_and_online(cdev);
473 if (force && cdev->private->state == DEV_STATE_BOXED) {
474 ret = ccw_device_stlck(cdev);
477 if (cdev->id.cu_type == 0)
478 cdev->private->state = DEV_STATE_NOT_OPER;
479 ret = online_store_recog_and_online(cdev);
486 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
489 struct ccw_device *cdev = to_ccwdev(dev);
493 /* Prevent conflict between multiple on-/offline processing requests. */
494 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
496 /* Prevent conflict between internal I/Os and on-/offline processing. */
497 if (!dev_fsm_final_state(cdev) &&
498 cdev->private->state != DEV_STATE_DISCONNECTED) {
502 /* Prevent conflict between pending work and on-/offline processing.*/
503 if (work_pending(&cdev->private->todo_work)) {
507 if (!strncmp(buf, "force\n", count)) {
513 ret = kstrtoul(buf, 16, &i);
521 ret = online_store_handle_offline(cdev);
524 ret = online_store_handle_online(cdev, force);
532 atomic_set(&cdev->private->onoff, 0);
533 return (ret < 0) ? ret : count;
537 available_show (struct device *dev, struct device_attribute *attr, char *buf)
539 struct ccw_device *cdev = to_ccwdev(dev);
540 struct subchannel *sch;
542 if (ccw_device_is_orphan(cdev))
543 return sprintf(buf, "no device\n");
544 switch (cdev->private->state) {
545 case DEV_STATE_BOXED:
546 return sprintf(buf, "boxed\n");
547 case DEV_STATE_DISCONNECTED:
548 case DEV_STATE_DISCONNECTED_SENSE_ID:
549 case DEV_STATE_NOT_OPER:
550 sch = to_subchannel(dev->parent);
552 return sprintf(buf, "no path\n");
554 return sprintf(buf, "no device\n");
556 /* All other states considered fine. */
557 return sprintf(buf, "good\n");
562 initiate_logging(struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
565 struct subchannel *sch = to_subchannel(dev);
568 rc = chsc_siosl(sch->schid);
570 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
571 sch->schid.ssid, sch->schid.sch_no, rc);
574 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
575 sch->schid.ssid, sch->schid.sch_no);
579 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
582 struct subchannel *sch = to_subchannel(dev);
584 return sprintf(buf, "%02x\n", sch->vpm);
587 static DEVICE_ATTR_RO(devtype);
588 static DEVICE_ATTR_RO(cutype);
589 static DEVICE_ATTR_RO(modalias);
590 static DEVICE_ATTR_RW(online);
591 static DEVICE_ATTR(availability, 0444, available_show, NULL);
592 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
593 static DEVICE_ATTR_RO(vpm);
595 static struct attribute *io_subchannel_attrs[] = {
596 &dev_attr_logging.attr,
601 static const struct attribute_group io_subchannel_attr_group = {
602 .attrs = io_subchannel_attrs,
605 static struct attribute * ccwdev_attrs[] = {
606 &dev_attr_devtype.attr,
607 &dev_attr_cutype.attr,
608 &dev_attr_modalias.attr,
609 &dev_attr_online.attr,
610 &dev_attr_cmb_enable.attr,
611 &dev_attr_availability.attr,
615 static const struct attribute_group ccwdev_attr_group = {
616 .attrs = ccwdev_attrs,
619 static const struct attribute_group *ccwdev_attr_groups[] = {
624 static int ccw_device_add(struct ccw_device *cdev)
626 struct device *dev = &cdev->dev;
628 dev->bus = &ccw_bus_type;
629 return device_add(dev);
632 static int match_dev_id(struct device *dev, const void *data)
634 struct ccw_device *cdev = to_ccwdev(dev);
635 struct ccw_dev_id *dev_id = (void *)data;
637 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
641 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
642 * @dev_id: id of the device to be searched
644 * This function searches all devices attached to the ccw bus for a device
647 * If a device is found its reference count is increased and returned;
648 * else %NULL is returned.
650 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
654 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
656 return dev ? to_ccwdev(dev) : NULL;
658 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
660 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
664 if (device_is_registered(&cdev->dev)) {
665 device_release_driver(&cdev->dev);
666 ret = device_attach(&cdev->dev);
667 WARN_ON(ret == -ENODEV);
672 ccw_device_release(struct device *dev)
674 struct ccw_device *cdev;
676 cdev = to_ccwdev(dev);
677 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
678 sizeof(*cdev->private->dma_area));
679 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
680 /* Release reference of parent subchannel. */
681 put_device(cdev->dev.parent);
682 kfree(cdev->private);
686 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
688 struct ccw_device *cdev;
689 struct gen_pool *dma_pool;
691 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
694 cdev->private = kzalloc(sizeof(struct ccw_device_private),
695 GFP_KERNEL | GFP_DMA);
698 cdev->dev.coherent_dma_mask = sch->dev.coherent_dma_mask;
699 cdev->dev.dma_mask = sch->dev.dma_mask;
700 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
703 cdev->private->dma_pool = dma_pool;
704 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
705 sizeof(*cdev->private->dma_area));
706 if (!cdev->private->dma_area)
710 cio_gp_dma_destroy(dma_pool, &cdev->dev);
712 kfree(cdev->private);
716 return ERR_PTR(-ENOMEM);
719 static void ccw_device_todo(struct work_struct *work);
721 static int io_subchannel_initialize_dev(struct subchannel *sch,
722 struct ccw_device *cdev)
724 struct ccw_device_private *priv = cdev->private;
728 priv->int_class = IRQIO_CIO;
729 priv->state = DEV_STATE_NOT_OPER;
730 priv->dev_id.devno = sch->schib.pmcw.dev;
731 priv->dev_id.ssid = sch->schid.ssid;
733 INIT_WORK(&priv->todo_work, ccw_device_todo);
734 INIT_LIST_HEAD(&priv->cmb_list);
735 init_waitqueue_head(&priv->wait_q);
736 timer_setup(&priv->timer, ccw_device_timeout, 0);
738 atomic_set(&priv->onoff, 0);
739 cdev->ccwlock = sch->lock;
740 cdev->dev.parent = &sch->dev;
741 cdev->dev.release = ccw_device_release;
742 cdev->dev.groups = ccwdev_attr_groups;
743 /* Do first half of device_register. */
744 device_initialize(&cdev->dev);
745 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
746 cdev->private->dev_id.devno);
749 if (!get_device(&sch->dev)) {
753 priv->flags.initialized = 1;
754 spin_lock_irq(sch->lock);
755 sch_set_cdev(sch, cdev);
756 spin_unlock_irq(sch->lock);
760 /* Release reference from device_initialize(). */
761 put_device(&cdev->dev);
765 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
767 struct ccw_device *cdev;
770 cdev = io_subchannel_allocate_dev(sch);
772 ret = io_subchannel_initialize_dev(sch, cdev);
779 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
781 static void sch_create_and_recog_new_device(struct subchannel *sch)
783 struct ccw_device *cdev;
785 /* Need to allocate a new ccw device. */
786 cdev = io_subchannel_create_ccwdev(sch);
788 /* OK, we did everything we could... */
789 css_sch_device_unregister(sch);
792 /* Start recognition for the new ccw device. */
793 io_subchannel_recog(cdev, sch);
797 * Register recognized device.
799 static void io_subchannel_register(struct ccw_device *cdev)
801 struct subchannel *sch;
802 int ret, adjust_init_count = 1;
805 sch = to_subchannel(cdev->dev.parent);
807 * Check if subchannel is still registered. It may have become
808 * unregistered if a machine check hit us after finishing
809 * device recognition but before the register work could be
812 if (!device_is_registered(&sch->dev))
814 css_update_ssd_info(sch);
816 * io_subchannel_register() will also be called after device
817 * recognition has been done for a boxed device (which will already
818 * be registered). We need to reprobe since we may now have sense id
821 if (device_is_registered(&cdev->dev)) {
823 ret = device_reprobe(&cdev->dev);
825 /* We can't do much here. */
826 CIO_MSG_EVENT(0, "device_reprobe() returned"
827 " %d for 0.%x.%04x\n", ret,
828 cdev->private->dev_id.ssid,
829 cdev->private->dev_id.devno);
831 adjust_init_count = 0;
835 * Now we know this subchannel will stay, we can throw
836 * our delayed uevent.
838 if (dev_get_uevent_suppress(&sch->dev)) {
839 dev_set_uevent_suppress(&sch->dev, 0);
840 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
842 /* make it known to the system */
843 ret = ccw_device_add(cdev);
845 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
846 cdev->private->dev_id.ssid,
847 cdev->private->dev_id.devno, ret);
848 spin_lock_irqsave(sch->lock, flags);
849 sch_set_cdev(sch, NULL);
850 spin_unlock_irqrestore(sch->lock, flags);
851 /* Release initial device reference. */
852 put_device(&cdev->dev);
856 cdev->private->flags.recog_done = 1;
857 wake_up(&cdev->private->wait_q);
859 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
860 wake_up(&ccw_device_init_wq);
863 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
865 struct subchannel *sch;
867 /* Get subchannel reference for local processing. */
868 if (!get_device(cdev->dev.parent))
870 sch = to_subchannel(cdev->dev.parent);
871 css_sch_device_unregister(sch);
872 /* Release subchannel reference for local processing. */
873 put_device(&sch->dev);
877 * subchannel recognition done. Called from the state machine.
880 io_subchannel_recog_done(struct ccw_device *cdev)
882 if (css_init_done == 0) {
883 cdev->private->flags.recog_done = 1;
886 switch (cdev->private->state) {
887 case DEV_STATE_BOXED:
888 /* Device did not respond in time. */
889 case DEV_STATE_NOT_OPER:
890 cdev->private->flags.recog_done = 1;
891 /* Remove device found not operational. */
892 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
893 if (atomic_dec_and_test(&ccw_device_init_count))
894 wake_up(&ccw_device_init_wq);
896 case DEV_STATE_OFFLINE:
898 * We can't register the device in interrupt context so
899 * we schedule a work item.
901 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
906 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
908 /* Increase counter of devices currently in recognition. */
909 atomic_inc(&ccw_device_init_count);
911 /* Start async. device sensing. */
912 spin_lock_irq(sch->lock);
913 ccw_device_recognition(cdev);
914 spin_unlock_irq(sch->lock);
917 static int ccw_device_move_to_sch(struct ccw_device *cdev,
918 struct subchannel *sch)
920 struct subchannel *old_sch;
921 int rc, old_enabled = 0;
923 old_sch = to_subchannel(cdev->dev.parent);
924 /* Obtain child reference for new parent. */
925 if (!get_device(&sch->dev))
928 if (!sch_is_pseudo_sch(old_sch)) {
929 spin_lock_irq(old_sch->lock);
930 old_enabled = old_sch->schib.pmcw.ena;
933 rc = cio_disable_subchannel(old_sch);
934 spin_unlock_irq(old_sch->lock);
936 /* Release child reference for new parent. */
937 put_device(&sch->dev);
942 mutex_lock(&sch->reg_mutex);
943 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
944 mutex_unlock(&sch->reg_mutex);
946 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
947 cdev->private->dev_id.ssid,
948 cdev->private->dev_id.devno, sch->schid.ssid,
949 sch->schib.pmcw.dev, rc);
951 /* Try to reenable the old subchannel. */
952 spin_lock_irq(old_sch->lock);
953 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
954 spin_unlock_irq(old_sch->lock);
956 /* Release child reference for new parent. */
957 put_device(&sch->dev);
960 /* Clean up old subchannel. */
961 if (!sch_is_pseudo_sch(old_sch)) {
962 spin_lock_irq(old_sch->lock);
963 sch_set_cdev(old_sch, NULL);
964 spin_unlock_irq(old_sch->lock);
965 css_schedule_eval(old_sch->schid);
967 /* Release child reference for old parent. */
968 put_device(&old_sch->dev);
969 /* Initialize new subchannel. */
970 spin_lock_irq(sch->lock);
971 cdev->ccwlock = sch->lock;
972 if (!sch_is_pseudo_sch(sch))
973 sch_set_cdev(sch, cdev);
974 spin_unlock_irq(sch->lock);
975 if (!sch_is_pseudo_sch(sch))
976 css_update_ssd_info(sch);
980 static int ccw_device_move_to_orph(struct ccw_device *cdev)
982 struct subchannel *sch = to_subchannel(cdev->dev.parent);
983 struct channel_subsystem *css = to_css(sch->dev.parent);
985 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
988 static void io_subchannel_irq(struct subchannel *sch)
990 struct ccw_device *cdev;
992 cdev = sch_get_cdev(sch);
994 CIO_TRACE_EVENT(6, "IRQ");
995 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
997 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
999 inc_irq_stat(IRQIO_CIO);
1002 void io_subchannel_init_config(struct subchannel *sch)
1004 memset(&sch->config, 0, sizeof(sch->config));
1005 sch->config.csense = 1;
1008 static void io_subchannel_init_fields(struct subchannel *sch)
1010 if (cio_is_console(sch->schid))
1013 sch->opm = chp_get_sch_opm(sch);
1014 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1015 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1017 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1018 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1019 sch->schib.pmcw.dev, sch->schid.ssid,
1020 sch->schid.sch_no, sch->schib.pmcw.pim,
1021 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1023 io_subchannel_init_config(sch);
1027 * Note: We always return 0 so that we bind to the device even on error.
1028 * This is needed so that our remove function is called on unregister.
1030 static int io_subchannel_probe(struct subchannel *sch)
1032 struct io_subchannel_private *io_priv;
1033 struct ccw_device *cdev;
1036 if (cio_is_console(sch->schid)) {
1037 rc = sysfs_create_group(&sch->dev.kobj,
1038 &io_subchannel_attr_group);
1040 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1041 "attributes for subchannel "
1042 "0.%x.%04x (rc=%d)\n",
1043 sch->schid.ssid, sch->schid.sch_no, rc);
1045 * The console subchannel already has an associated ccw_device.
1046 * Throw the delayed uevent for the subchannel, register
1047 * the ccw_device and exit.
1049 if (dev_get_uevent_suppress(&sch->dev)) {
1050 /* should always be the case for the console */
1051 dev_set_uevent_suppress(&sch->dev, 0);
1052 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1054 cdev = sch_get_cdev(sch);
1055 rc = ccw_device_add(cdev);
1057 /* Release online reference. */
1058 put_device(&cdev->dev);
1061 if (atomic_dec_and_test(&ccw_device_init_count))
1062 wake_up(&ccw_device_init_wq);
1065 io_subchannel_init_fields(sch);
1066 rc = cio_commit_config(sch);
1069 rc = sysfs_create_group(&sch->dev.kobj,
1070 &io_subchannel_attr_group);
1073 /* Allocate I/O subchannel private data. */
1074 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1078 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1079 sizeof(*io_priv->dma_area),
1080 &io_priv->dma_area_dma, GFP_KERNEL);
1081 if (!io_priv->dma_area) {
1086 set_io_private(sch, io_priv);
1087 css_schedule_eval(sch->schid);
1091 spin_lock_irq(sch->lock);
1092 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1093 spin_unlock_irq(sch->lock);
1097 static int io_subchannel_remove(struct subchannel *sch)
1099 struct io_subchannel_private *io_priv = to_io_private(sch);
1100 struct ccw_device *cdev;
1102 cdev = sch_get_cdev(sch);
1106 ccw_device_unregister(cdev);
1107 spin_lock_irq(sch->lock);
1108 sch_set_cdev(sch, NULL);
1109 set_io_private(sch, NULL);
1110 spin_unlock_irq(sch->lock);
1112 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1113 io_priv->dma_area, io_priv->dma_area_dma);
1115 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1119 static void io_subchannel_verify(struct subchannel *sch)
1121 struct ccw_device *cdev;
1123 cdev = sch_get_cdev(sch);
1125 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1128 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1130 struct ccw_device *cdev;
1132 cdev = sch_get_cdev(sch);
1135 if (cio_update_schib(sch))
1137 /* Check for I/O on path. */
1138 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1140 if (cdev->private->state == DEV_STATE_ONLINE) {
1141 ccw_device_kill_io(cdev);
1147 /* Trigger path verification. */
1148 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1152 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1155 static int io_subchannel_chp_event(struct subchannel *sch,
1156 struct chp_link *link, int event)
1158 struct ccw_device *cdev = sch_get_cdev(sch);
1159 int mask, chpid, valid_bit;
1162 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1170 cdev->private->path_gone_mask |= mask;
1171 io_subchannel_terminate_path(sch, mask);
1177 cdev->private->path_new_mask |= mask;
1178 io_subchannel_verify(sch);
1181 if (cio_update_schib(sch))
1184 cdev->private->path_gone_mask |= mask;
1185 io_subchannel_terminate_path(sch, mask);
1188 if (cio_update_schib(sch))
1190 sch->lpm |= mask & sch->opm;
1192 cdev->private->path_new_mask |= mask;
1193 io_subchannel_verify(sch);
1195 case CHP_FCES_EVENT:
1196 /* Forward Endpoint Security event */
1197 for (chpid = 0, valid_bit = 0x80; chpid < 8; chpid++,
1199 if (mask & valid_bit)
1200 path_event[chpid] = PE_PATH_FCES_EVENT;
1202 path_event[chpid] = PE_NONE;
1205 cdev->drv->path_event(cdev, path_event);
1211 static void io_subchannel_quiesce(struct subchannel *sch)
1213 struct ccw_device *cdev;
1216 spin_lock_irq(sch->lock);
1217 cdev = sch_get_cdev(sch);
1218 if (cio_is_console(sch->schid))
1220 if (!sch->schib.pmcw.ena)
1222 ret = cio_disable_subchannel(sch);
1226 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1227 while (ret == -EBUSY) {
1228 cdev->private->state = DEV_STATE_QUIESCE;
1229 cdev->private->iretry = 255;
1230 ret = ccw_device_cancel_halt_clear(cdev);
1231 if (ret == -EBUSY) {
1232 ccw_device_set_timeout(cdev, HZ/10);
1233 spin_unlock_irq(sch->lock);
1234 wait_event(cdev->private->wait_q,
1235 cdev->private->state != DEV_STATE_QUIESCE);
1236 spin_lock_irq(sch->lock);
1238 ret = cio_disable_subchannel(sch);
1241 spin_unlock_irq(sch->lock);
1244 static void io_subchannel_shutdown(struct subchannel *sch)
1246 io_subchannel_quiesce(sch);
1249 static int device_is_disconnected(struct ccw_device *cdev)
1253 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1254 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1257 static int recovery_check(struct device *dev, void *data)
1259 struct ccw_device *cdev = to_ccwdev(dev);
1260 struct subchannel *sch;
1263 spin_lock_irq(cdev->ccwlock);
1264 switch (cdev->private->state) {
1265 case DEV_STATE_ONLINE:
1266 sch = to_subchannel(cdev->dev.parent);
1267 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1270 case DEV_STATE_DISCONNECTED:
1271 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1272 cdev->private->dev_id.ssid,
1273 cdev->private->dev_id.devno);
1274 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1277 case DEV_STATE_DISCONNECTED_SENSE_ID:
1281 spin_unlock_irq(cdev->ccwlock);
1286 static void recovery_work_func(struct work_struct *unused)
1290 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1292 spin_lock_irq(&recovery_lock);
1293 if (!timer_pending(&recovery_timer)) {
1294 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1296 mod_timer(&recovery_timer, jiffies +
1297 recovery_delay[recovery_phase] * HZ);
1299 spin_unlock_irq(&recovery_lock);
1301 CIO_MSG_EVENT(3, "recovery: end\n");
1304 static DECLARE_WORK(recovery_work, recovery_work_func);
1306 static void recovery_func(struct timer_list *unused)
1309 * We can't do our recovery in softirq context and it's not
1310 * performance critical, so we schedule it.
1312 schedule_work(&recovery_work);
1315 void ccw_device_schedule_recovery(void)
1317 unsigned long flags;
1319 CIO_MSG_EVENT(3, "recovery: schedule\n");
1320 spin_lock_irqsave(&recovery_lock, flags);
1321 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1323 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1325 spin_unlock_irqrestore(&recovery_lock, flags);
1328 static int purge_fn(struct device *dev, void *data)
1330 struct ccw_device *cdev = to_ccwdev(dev);
1331 struct ccw_dev_id *id = &cdev->private->dev_id;
1333 spin_lock_irq(cdev->ccwlock);
1334 if (is_blacklisted(id->ssid, id->devno) &&
1335 (cdev->private->state == DEV_STATE_OFFLINE) &&
1336 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1337 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1339 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1340 atomic_set(&cdev->private->onoff, 0);
1342 spin_unlock_irq(cdev->ccwlock);
1343 /* Abort loop in case of pending signal. */
1344 if (signal_pending(current))
1351 * ccw_purge_blacklisted - purge unused, blacklisted devices
1353 * Unregister all ccw devices that are offline and on the blacklist.
1355 int ccw_purge_blacklisted(void)
1357 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1358 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1362 void ccw_device_set_disconnected(struct ccw_device *cdev)
1366 ccw_device_set_timeout(cdev, 0);
1367 cdev->private->flags.fake_irb = 0;
1368 cdev->private->state = DEV_STATE_DISCONNECTED;
1370 ccw_device_schedule_recovery();
1373 void ccw_device_set_notoper(struct ccw_device *cdev)
1375 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1377 CIO_TRACE_EVENT(2, "notoper");
1378 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1379 ccw_device_set_timeout(cdev, 0);
1380 cio_disable_subchannel(sch);
1381 cdev->private->state = DEV_STATE_NOT_OPER;
1384 enum io_sch_action {
1388 IO_SCH_UNREG_ATTACH,
1396 static enum io_sch_action sch_get_action(struct subchannel *sch)
1398 struct ccw_device *cdev;
1400 cdev = sch_get_cdev(sch);
1401 if (cio_update_schib(sch)) {
1402 /* Not operational. */
1404 return IO_SCH_UNREG;
1405 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1406 return IO_SCH_UNREG;
1407 return IO_SCH_ORPH_UNREG;
1411 return IO_SCH_ATTACH;
1412 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1413 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1414 return IO_SCH_UNREG_ATTACH;
1415 return IO_SCH_ORPH_ATTACH;
1417 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1418 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1419 return IO_SCH_UNREG;
1422 if (device_is_disconnected(cdev))
1423 return IO_SCH_REPROBE;
1425 return IO_SCH_VERIFY;
1426 if (cdev->private->state == DEV_STATE_NOT_OPER)
1427 return IO_SCH_UNREG_ATTACH;
1432 * io_subchannel_sch_event - process subchannel event
1434 * @process: non-zero if function is called in process context
1436 * An unspecified event occurred for this subchannel. Adjust data according
1437 * to the current operational state of the subchannel and device. Return
1438 * zero when the event has been handled sufficiently or -EAGAIN when this
1439 * function should be called again in process context.
1441 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1443 unsigned long flags;
1444 struct ccw_device *cdev;
1445 struct ccw_dev_id dev_id;
1446 enum io_sch_action action;
1449 spin_lock_irqsave(sch->lock, flags);
1450 if (!device_is_registered(&sch->dev))
1452 if (work_pending(&sch->todo_work))
1454 cdev = sch_get_cdev(sch);
1455 if (cdev && work_pending(&cdev->private->todo_work))
1457 action = sch_get_action(sch);
1458 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1459 sch->schid.ssid, sch->schid.sch_no, process,
1461 /* Perform immediate actions while holding the lock. */
1463 case IO_SCH_REPROBE:
1464 /* Trigger device recognition. */
1465 ccw_device_trigger_reprobe(cdev);
1469 /* Trigger path verification. */
1470 io_subchannel_verify(sch);
1474 ccw_device_set_disconnected(cdev);
1477 case IO_SCH_ORPH_UNREG:
1478 case IO_SCH_ORPH_ATTACH:
1479 ccw_device_set_disconnected(cdev);
1481 case IO_SCH_UNREG_ATTACH:
1485 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1487 * Note: delayed work triggered by this event
1488 * and repeated calls to sch_event are synchronized
1489 * by the above check for work_pending(cdev).
1491 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1493 ccw_device_set_notoper(cdev);
1501 spin_unlock_irqrestore(sch->lock, flags);
1502 /* All other actions require process context. */
1505 /* Handle attached ccw device. */
1507 case IO_SCH_ORPH_UNREG:
1508 case IO_SCH_ORPH_ATTACH:
1509 /* Move ccw device to orphanage. */
1510 rc = ccw_device_move_to_orph(cdev);
1514 case IO_SCH_UNREG_ATTACH:
1515 spin_lock_irqsave(sch->lock, flags);
1516 sch_set_cdev(sch, NULL);
1517 spin_unlock_irqrestore(sch->lock, flags);
1518 /* Unregister ccw device. */
1519 ccw_device_unregister(cdev);
1524 /* Handle subchannel. */
1526 case IO_SCH_ORPH_UNREG:
1529 css_sch_device_unregister(sch);
1531 case IO_SCH_ORPH_ATTACH:
1532 case IO_SCH_UNREG_ATTACH:
1534 dev_id.ssid = sch->schid.ssid;
1535 dev_id.devno = sch->schib.pmcw.dev;
1536 cdev = get_ccwdev_by_dev_id(&dev_id);
1538 sch_create_and_recog_new_device(sch);
1541 rc = ccw_device_move_to_sch(cdev, sch);
1543 /* Release reference from get_ccwdev_by_dev_id() */
1544 put_device(&cdev->dev);
1547 spin_lock_irqsave(sch->lock, flags);
1548 ccw_device_trigger_reprobe(cdev);
1549 spin_unlock_irqrestore(sch->lock, flags);
1550 /* Release reference from get_ccwdev_by_dev_id() */
1551 put_device(&cdev->dev);
1559 spin_unlock_irqrestore(sch->lock, flags);
1564 static void ccw_device_set_int_class(struct ccw_device *cdev)
1566 struct ccw_driver *cdrv = cdev->drv;
1568 /* Note: we interpret class 0 in this context as an uninitialized
1569 * field since it translates to a non-I/O interrupt class. */
1570 if (cdrv->int_class != 0)
1571 cdev->private->int_class = cdrv->int_class;
1573 cdev->private->int_class = IRQIO_CIO;
1576 #ifdef CONFIG_CCW_CONSOLE
1577 int __init ccw_device_enable_console(struct ccw_device *cdev)
1579 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1582 if (!cdev->drv || !cdev->handler)
1585 io_subchannel_init_fields(sch);
1586 rc = cio_commit_config(sch);
1589 sch->driver = &io_subchannel_driver;
1590 io_subchannel_recog(cdev, sch);
1591 /* Now wait for the async. recognition to come to an end. */
1592 spin_lock_irq(cdev->ccwlock);
1593 while (!dev_fsm_final_state(cdev))
1594 ccw_device_wait_idle(cdev);
1596 /* Hold on to an extra reference while device is online. */
1597 get_device(&cdev->dev);
1598 rc = ccw_device_online(cdev);
1602 while (!dev_fsm_final_state(cdev))
1603 ccw_device_wait_idle(cdev);
1605 if (cdev->private->state == DEV_STATE_ONLINE)
1610 spin_unlock_irq(cdev->ccwlock);
1611 if (rc) /* Give up online reference since onlining failed. */
1612 put_device(&cdev->dev);
1616 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1618 struct io_subchannel_private *io_priv;
1619 struct ccw_device *cdev;
1620 struct subchannel *sch;
1622 sch = cio_probe_console();
1624 return ERR_CAST(sch);
1626 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1629 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1630 sizeof(*io_priv->dma_area),
1631 &io_priv->dma_area_dma, GFP_KERNEL);
1632 if (!io_priv->dma_area)
1634 set_io_private(sch, io_priv);
1635 cdev = io_subchannel_create_ccwdev(sch);
1637 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1638 io_priv->dma_area, io_priv->dma_area_dma);
1639 set_io_private(sch, NULL);
1640 put_device(&sch->dev);
1645 ccw_device_set_int_class(cdev);
1651 put_device(&sch->dev);
1652 return ERR_PTR(-ENOMEM);
1655 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1657 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1658 struct io_subchannel_private *io_priv = to_io_private(sch);
1660 set_io_private(sch, NULL);
1661 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1662 io_priv->dma_area, io_priv->dma_area_dma);
1663 put_device(&sch->dev);
1664 put_device(&cdev->dev);
1669 * ccw_device_wait_idle() - busy wait for device to become idle
1672 * Poll until activity control is zero, that is, no function or data
1673 * transfer is pending/active.
1674 * Called with device lock being held.
1676 void ccw_device_wait_idle(struct ccw_device *cdev)
1678 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1682 if (sch->schib.scsw.cmd.actl == 0)
1690 * get_ccwdev_by_busid() - obtain device from a bus id
1691 * @cdrv: driver the device is owned by
1692 * @bus_id: bus id of the device to be searched
1694 * This function searches all devices owned by @cdrv for a device with a bus
1695 * id matching @bus_id.
1697 * If a match is found, its reference count of the found device is increased
1698 * and it is returned; else %NULL is returned.
1700 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1705 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
1707 return dev ? to_ccwdev(dev) : NULL;
1710 /************************** device driver handling ************************/
1712 /* This is the implementation of the ccw_driver class. The probe, remove
1713 * and release methods are initially very similar to the device_driver
1714 * implementations, with the difference that they have ccw_device
1717 * A ccw driver also contains the information that is needed for
1721 ccw_device_probe (struct device *dev)
1723 struct ccw_device *cdev = to_ccwdev(dev);
1724 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1727 cdev->drv = cdrv; /* to let the driver call _set_online */
1728 ccw_device_set_int_class(cdev);
1729 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1732 cdev->private->int_class = IRQIO_CIO;
1739 static int ccw_device_remove(struct device *dev)
1741 struct ccw_device *cdev = to_ccwdev(dev);
1742 struct ccw_driver *cdrv = cdev->drv;
1743 struct subchannel *sch;
1749 spin_lock_irq(cdev->ccwlock);
1752 ret = ccw_device_offline(cdev);
1753 spin_unlock_irq(cdev->ccwlock);
1755 wait_event(cdev->private->wait_q,
1756 dev_fsm_final_state(cdev));
1758 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1759 "device 0.%x.%04x\n",
1760 ret, cdev->private->dev_id.ssid,
1761 cdev->private->dev_id.devno);
1762 /* Give up reference obtained in ccw_device_set_online(). */
1763 put_device(&cdev->dev);
1764 spin_lock_irq(cdev->ccwlock);
1766 ccw_device_set_timeout(cdev, 0);
1768 cdev->private->int_class = IRQIO_CIO;
1769 sch = to_subchannel(cdev->dev.parent);
1770 spin_unlock_irq(cdev->ccwlock);
1771 io_subchannel_quiesce(sch);
1772 __disable_cmf(cdev);
1777 static void ccw_device_shutdown(struct device *dev)
1779 struct ccw_device *cdev;
1781 cdev = to_ccwdev(dev);
1782 if (cdev->drv && cdev->drv->shutdown)
1783 cdev->drv->shutdown(cdev);
1784 __disable_cmf(cdev);
1787 static struct bus_type ccw_bus_type = {
1789 .match = ccw_bus_match,
1790 .uevent = ccw_uevent,
1791 .probe = ccw_device_probe,
1792 .remove = ccw_device_remove,
1793 .shutdown = ccw_device_shutdown,
1797 * ccw_driver_register() - register a ccw driver
1798 * @cdriver: driver to be registered
1800 * This function is mainly a wrapper around driver_register().
1802 * %0 on success and a negative error value on failure.
1804 int ccw_driver_register(struct ccw_driver *cdriver)
1806 struct device_driver *drv = &cdriver->driver;
1808 drv->bus = &ccw_bus_type;
1810 return driver_register(drv);
1814 * ccw_driver_unregister() - deregister a ccw driver
1815 * @cdriver: driver to be deregistered
1817 * This function is mainly a wrapper around driver_unregister().
1819 void ccw_driver_unregister(struct ccw_driver *cdriver)
1821 driver_unregister(&cdriver->driver);
1824 static void ccw_device_todo(struct work_struct *work)
1826 struct ccw_device_private *priv;
1827 struct ccw_device *cdev;
1828 struct subchannel *sch;
1829 enum cdev_todo todo;
1831 priv = container_of(work, struct ccw_device_private, todo_work);
1833 sch = to_subchannel(cdev->dev.parent);
1834 /* Find out todo. */
1835 spin_lock_irq(cdev->ccwlock);
1837 priv->todo = CDEV_TODO_NOTHING;
1838 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1839 priv->dev_id.ssid, priv->dev_id.devno, todo);
1840 spin_unlock_irq(cdev->ccwlock);
1843 case CDEV_TODO_ENABLE_CMF:
1846 case CDEV_TODO_REBIND:
1847 ccw_device_do_unbind_bind(cdev);
1849 case CDEV_TODO_REGISTER:
1850 io_subchannel_register(cdev);
1852 case CDEV_TODO_UNREG_EVAL:
1853 if (!sch_is_pseudo_sch(sch))
1854 css_schedule_eval(sch->schid);
1856 case CDEV_TODO_UNREG:
1857 if (sch_is_pseudo_sch(sch))
1858 ccw_device_unregister(cdev);
1860 ccw_device_call_sch_unregister(cdev);
1865 /* Release workqueue ref. */
1866 put_device(&cdev->dev);
1870 * ccw_device_sched_todo - schedule ccw device operation
1874 * Schedule the operation identified by @todo to be performed on the slow path
1875 * workqueue. Do nothing if another operation with higher priority is already
1876 * scheduled. Needs to be called with ccwdev lock held.
1878 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1880 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1881 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1883 if (cdev->private->todo >= todo)
1885 cdev->private->todo = todo;
1886 /* Get workqueue ref. */
1887 if (!get_device(&cdev->dev))
1889 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
1890 /* Already queued, release workqueue ref. */
1891 put_device(&cdev->dev);
1896 * ccw_device_siosl() - initiate logging
1899 * This function is used to invoke model-dependent logging within the channel
1902 int ccw_device_siosl(struct ccw_device *cdev)
1904 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1906 return chsc_siosl(sch->schid);
1908 EXPORT_SYMBOL_GPL(ccw_device_siosl);
1910 EXPORT_SYMBOL(ccw_device_set_online);
1911 EXPORT_SYMBOL(ccw_device_set_offline);
1912 EXPORT_SYMBOL(ccw_driver_register);
1913 EXPORT_SYMBOL(ccw_driver_unregister);
1914 EXPORT_SYMBOL(get_ccwdev_by_busid);