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 void 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 match_dev_id(struct device *dev, const void *data)
626 struct ccw_device *cdev = to_ccwdev(dev);
627 struct ccw_dev_id *dev_id = (void *)data;
629 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
633 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
634 * @dev_id: id of the device to be searched
636 * This function searches all devices attached to the ccw bus for a device
639 * If a device is found its reference count is increased and returned;
640 * else %NULL is returned.
642 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
646 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
648 return dev ? to_ccwdev(dev) : NULL;
650 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
652 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
656 if (device_is_registered(&cdev->dev)) {
657 device_release_driver(&cdev->dev);
658 ret = device_attach(&cdev->dev);
659 WARN_ON(ret == -ENODEV);
664 ccw_device_release(struct device *dev)
666 struct ccw_device *cdev;
668 cdev = to_ccwdev(dev);
669 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
670 sizeof(*cdev->private->dma_area));
671 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
672 /* Release reference of parent subchannel. */
673 put_device(cdev->dev.parent);
674 kfree(cdev->private);
678 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
680 struct ccw_device *cdev;
681 struct gen_pool *dma_pool;
684 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
689 cdev->private = kzalloc(sizeof(struct ccw_device_private),
690 GFP_KERNEL | GFP_DMA);
691 if (!cdev->private) {
696 cdev->dev.dma_mask = sch->dev.dma_mask;
697 ret = dma_set_coherent_mask(&cdev->dev, sch->dev.coherent_dma_mask);
699 goto err_coherent_mask;
701 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
706 cdev->private->dma_pool = dma_pool;
707 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
708 sizeof(*cdev->private->dma_area));
709 if (!cdev->private->dma_area) {
715 cio_gp_dma_destroy(dma_pool, &cdev->dev);
718 kfree(cdev->private);
725 static void ccw_device_todo(struct work_struct *work);
727 static int io_subchannel_initialize_dev(struct subchannel *sch,
728 struct ccw_device *cdev)
730 struct ccw_device_private *priv = cdev->private;
734 priv->int_class = IRQIO_CIO;
735 priv->state = DEV_STATE_NOT_OPER;
736 priv->dev_id.devno = sch->schib.pmcw.dev;
737 priv->dev_id.ssid = sch->schid.ssid;
739 INIT_WORK(&priv->todo_work, ccw_device_todo);
740 INIT_LIST_HEAD(&priv->cmb_list);
741 init_waitqueue_head(&priv->wait_q);
742 timer_setup(&priv->timer, ccw_device_timeout, 0);
744 atomic_set(&priv->onoff, 0);
745 cdev->ccwlock = sch->lock;
746 cdev->dev.parent = &sch->dev;
747 cdev->dev.release = ccw_device_release;
748 cdev->dev.bus = &ccw_bus_type;
749 cdev->dev.groups = ccwdev_attr_groups;
750 /* Do first half of device_register. */
751 device_initialize(&cdev->dev);
752 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
753 cdev->private->dev_id.devno);
756 if (!get_device(&sch->dev)) {
760 priv->flags.initialized = 1;
761 spin_lock_irq(sch->lock);
762 sch_set_cdev(sch, cdev);
763 spin_unlock_irq(sch->lock);
767 /* Release reference from device_initialize(). */
768 put_device(&cdev->dev);
772 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
774 struct ccw_device *cdev;
777 cdev = io_subchannel_allocate_dev(sch);
779 ret = io_subchannel_initialize_dev(sch, cdev);
786 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
788 static void sch_create_and_recog_new_device(struct subchannel *sch)
790 struct ccw_device *cdev;
792 /* Need to allocate a new ccw device. */
793 cdev = io_subchannel_create_ccwdev(sch);
795 /* OK, we did everything we could... */
796 css_sch_device_unregister(sch);
799 /* Start recognition for the new ccw device. */
800 io_subchannel_recog(cdev, sch);
804 * Register recognized device.
806 static void io_subchannel_register(struct ccw_device *cdev)
808 struct subchannel *sch;
809 int ret, adjust_init_count = 1;
812 sch = to_subchannel(cdev->dev.parent);
814 * Check if subchannel is still registered. It may have become
815 * unregistered if a machine check hit us after finishing
816 * device recognition but before the register work could be
819 if (!device_is_registered(&sch->dev))
821 css_update_ssd_info(sch);
823 * io_subchannel_register() will also be called after device
824 * recognition has been done for a boxed device (which will already
825 * be registered). We need to reprobe since we may now have sense id
828 if (device_is_registered(&cdev->dev)) {
830 ret = device_reprobe(&cdev->dev);
832 /* We can't do much here. */
833 CIO_MSG_EVENT(0, "device_reprobe() returned"
834 " %d for 0.%x.%04x\n", ret,
835 cdev->private->dev_id.ssid,
836 cdev->private->dev_id.devno);
838 adjust_init_count = 0;
842 * Now we know this subchannel will stay, we can throw
843 * our delayed uevent.
845 if (dev_get_uevent_suppress(&sch->dev)) {
846 dev_set_uevent_suppress(&sch->dev, 0);
847 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
849 /* make it known to the system */
850 ret = device_add(&cdev->dev);
852 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
853 cdev->private->dev_id.ssid,
854 cdev->private->dev_id.devno, ret);
855 spin_lock_irqsave(sch->lock, flags);
856 sch_set_cdev(sch, NULL);
857 spin_unlock_irqrestore(sch->lock, flags);
858 /* Release initial device reference. */
859 put_device(&cdev->dev);
863 cdev->private->flags.recog_done = 1;
864 wake_up(&cdev->private->wait_q);
866 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
867 wake_up(&ccw_device_init_wq);
870 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
872 struct subchannel *sch;
874 /* Get subchannel reference for local processing. */
875 if (!get_device(cdev->dev.parent))
877 sch = to_subchannel(cdev->dev.parent);
878 css_sch_device_unregister(sch);
879 /* Release subchannel reference for local processing. */
880 put_device(&sch->dev);
884 * subchannel recognition done. Called from the state machine.
887 io_subchannel_recog_done(struct ccw_device *cdev)
889 if (css_init_done == 0) {
890 cdev->private->flags.recog_done = 1;
893 switch (cdev->private->state) {
894 case DEV_STATE_BOXED:
895 /* Device did not respond in time. */
896 case DEV_STATE_NOT_OPER:
897 cdev->private->flags.recog_done = 1;
898 /* Remove device found not operational. */
899 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
900 if (atomic_dec_and_test(&ccw_device_init_count))
901 wake_up(&ccw_device_init_wq);
903 case DEV_STATE_OFFLINE:
905 * We can't register the device in interrupt context so
906 * we schedule a work item.
908 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
913 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
915 /* Increase counter of devices currently in recognition. */
916 atomic_inc(&ccw_device_init_count);
918 /* Start async. device sensing. */
919 spin_lock_irq(sch->lock);
920 ccw_device_recognition(cdev);
921 spin_unlock_irq(sch->lock);
924 static int ccw_device_move_to_sch(struct ccw_device *cdev,
925 struct subchannel *sch)
927 struct subchannel *old_sch;
928 int rc, old_enabled = 0;
930 old_sch = to_subchannel(cdev->dev.parent);
931 /* Obtain child reference for new parent. */
932 if (!get_device(&sch->dev))
935 if (!sch_is_pseudo_sch(old_sch)) {
936 spin_lock_irq(old_sch->lock);
937 old_enabled = old_sch->schib.pmcw.ena;
940 rc = cio_disable_subchannel(old_sch);
941 spin_unlock_irq(old_sch->lock);
943 /* Release child reference for new parent. */
944 put_device(&sch->dev);
949 mutex_lock(&sch->reg_mutex);
950 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
951 mutex_unlock(&sch->reg_mutex);
953 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
954 cdev->private->dev_id.ssid,
955 cdev->private->dev_id.devno, sch->schid.ssid,
956 sch->schib.pmcw.dev, rc);
958 /* Try to reenable the old subchannel. */
959 spin_lock_irq(old_sch->lock);
960 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
961 spin_unlock_irq(old_sch->lock);
963 /* Release child reference for new parent. */
964 put_device(&sch->dev);
967 /* Clean up old subchannel. */
968 if (!sch_is_pseudo_sch(old_sch)) {
969 spin_lock_irq(old_sch->lock);
970 sch_set_cdev(old_sch, NULL);
971 spin_unlock_irq(old_sch->lock);
972 css_schedule_eval(old_sch->schid);
974 /* Release child reference for old parent. */
975 put_device(&old_sch->dev);
976 /* Initialize new subchannel. */
977 spin_lock_irq(sch->lock);
978 cdev->ccwlock = sch->lock;
979 if (!sch_is_pseudo_sch(sch))
980 sch_set_cdev(sch, cdev);
981 spin_unlock_irq(sch->lock);
982 if (!sch_is_pseudo_sch(sch))
983 css_update_ssd_info(sch);
987 static int ccw_device_move_to_orph(struct ccw_device *cdev)
989 struct subchannel *sch = to_subchannel(cdev->dev.parent);
990 struct channel_subsystem *css = to_css(sch->dev.parent);
992 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
995 static void io_subchannel_irq(struct subchannel *sch)
997 struct ccw_device *cdev;
999 cdev = sch_get_cdev(sch);
1001 CIO_TRACE_EVENT(6, "IRQ");
1002 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1004 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1006 inc_irq_stat(IRQIO_CIO);
1009 void io_subchannel_init_config(struct subchannel *sch)
1011 memset(&sch->config, 0, sizeof(sch->config));
1012 sch->config.csense = 1;
1015 static void io_subchannel_init_fields(struct subchannel *sch)
1017 if (cio_is_console(sch->schid))
1020 sch->opm = chp_get_sch_opm(sch);
1021 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1022 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1024 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1025 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1026 sch->schib.pmcw.dev, sch->schid.ssid,
1027 sch->schid.sch_no, sch->schib.pmcw.pim,
1028 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1030 io_subchannel_init_config(sch);
1034 * Note: We always return 0 so that we bind to the device even on error.
1035 * This is needed so that our remove function is called on unregister.
1037 static int io_subchannel_probe(struct subchannel *sch)
1039 struct io_subchannel_private *io_priv;
1040 struct ccw_device *cdev;
1043 if (cio_is_console(sch->schid)) {
1044 rc = sysfs_create_group(&sch->dev.kobj,
1045 &io_subchannel_attr_group);
1047 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1048 "attributes for subchannel "
1049 "0.%x.%04x (rc=%d)\n",
1050 sch->schid.ssid, sch->schid.sch_no, rc);
1052 * The console subchannel already has an associated ccw_device.
1053 * Throw the delayed uevent for the subchannel, register
1054 * the ccw_device and exit.
1056 if (dev_get_uevent_suppress(&sch->dev)) {
1057 /* should always be the case for the console */
1058 dev_set_uevent_suppress(&sch->dev, 0);
1059 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1061 cdev = sch_get_cdev(sch);
1062 rc = device_add(&cdev->dev);
1064 /* Release online reference. */
1065 put_device(&cdev->dev);
1068 if (atomic_dec_and_test(&ccw_device_init_count))
1069 wake_up(&ccw_device_init_wq);
1072 io_subchannel_init_fields(sch);
1073 rc = cio_commit_config(sch);
1076 rc = sysfs_create_group(&sch->dev.kobj,
1077 &io_subchannel_attr_group);
1080 /* Allocate I/O subchannel private data. */
1081 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1085 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1086 sizeof(*io_priv->dma_area),
1087 &io_priv->dma_area_dma, GFP_KERNEL);
1088 if (!io_priv->dma_area) {
1093 set_io_private(sch, io_priv);
1094 css_schedule_eval(sch->schid);
1098 spin_lock_irq(sch->lock);
1099 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1100 spin_unlock_irq(sch->lock);
1104 static void io_subchannel_remove(struct subchannel *sch)
1106 struct io_subchannel_private *io_priv = to_io_private(sch);
1107 struct ccw_device *cdev;
1109 cdev = sch_get_cdev(sch);
1113 ccw_device_unregister(cdev);
1114 spin_lock_irq(sch->lock);
1115 sch_set_cdev(sch, NULL);
1116 set_io_private(sch, NULL);
1117 spin_unlock_irq(sch->lock);
1119 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1120 io_priv->dma_area, io_priv->dma_area_dma);
1122 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1125 static void io_subchannel_verify(struct subchannel *sch)
1127 struct ccw_device *cdev;
1129 cdev = sch_get_cdev(sch);
1131 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1134 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1136 struct ccw_device *cdev;
1138 cdev = sch_get_cdev(sch);
1141 if (cio_update_schib(sch))
1143 /* Check for I/O on path. */
1144 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1146 if (cdev->private->state == DEV_STATE_ONLINE) {
1147 ccw_device_kill_io(cdev);
1153 /* Trigger path verification. */
1154 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1158 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1161 static int io_subchannel_chp_event(struct subchannel *sch,
1162 struct chp_link *link, int event)
1164 struct ccw_device *cdev = sch_get_cdev(sch);
1165 int mask, chpid, valid_bit;
1168 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1176 cdev->private->path_gone_mask |= mask;
1177 io_subchannel_terminate_path(sch, mask);
1183 cdev->private->path_new_mask |= mask;
1184 io_subchannel_verify(sch);
1187 if (cio_update_schib(sch))
1190 cdev->private->path_gone_mask |= mask;
1191 io_subchannel_terminate_path(sch, mask);
1194 if (cio_update_schib(sch))
1196 sch->lpm |= mask & sch->opm;
1198 cdev->private->path_new_mask |= mask;
1199 io_subchannel_verify(sch);
1201 case CHP_FCES_EVENT:
1202 /* Forward Endpoint Security event */
1203 for (chpid = 0, valid_bit = 0x80; chpid < 8; chpid++,
1205 if (mask & valid_bit)
1206 path_event[chpid] = PE_PATH_FCES_EVENT;
1208 path_event[chpid] = PE_NONE;
1211 cdev->drv->path_event(cdev, path_event);
1217 static void io_subchannel_quiesce(struct subchannel *sch)
1219 struct ccw_device *cdev;
1222 spin_lock_irq(sch->lock);
1223 cdev = sch_get_cdev(sch);
1224 if (cio_is_console(sch->schid))
1226 if (!sch->schib.pmcw.ena)
1228 ret = cio_disable_subchannel(sch);
1232 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1233 while (ret == -EBUSY) {
1234 cdev->private->state = DEV_STATE_QUIESCE;
1235 cdev->private->iretry = 255;
1236 ret = ccw_device_cancel_halt_clear(cdev);
1237 if (ret == -EBUSY) {
1238 ccw_device_set_timeout(cdev, HZ/10);
1239 spin_unlock_irq(sch->lock);
1240 wait_event(cdev->private->wait_q,
1241 cdev->private->state != DEV_STATE_QUIESCE);
1242 spin_lock_irq(sch->lock);
1244 ret = cio_disable_subchannel(sch);
1247 spin_unlock_irq(sch->lock);
1250 static void io_subchannel_shutdown(struct subchannel *sch)
1252 io_subchannel_quiesce(sch);
1255 static int device_is_disconnected(struct ccw_device *cdev)
1259 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1260 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1263 static int recovery_check(struct device *dev, void *data)
1265 struct ccw_device *cdev = to_ccwdev(dev);
1266 struct subchannel *sch;
1269 spin_lock_irq(cdev->ccwlock);
1270 switch (cdev->private->state) {
1271 case DEV_STATE_ONLINE:
1272 sch = to_subchannel(cdev->dev.parent);
1273 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1276 case DEV_STATE_DISCONNECTED:
1277 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1278 cdev->private->dev_id.ssid,
1279 cdev->private->dev_id.devno);
1280 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1283 case DEV_STATE_DISCONNECTED_SENSE_ID:
1287 spin_unlock_irq(cdev->ccwlock);
1292 static void recovery_work_func(struct work_struct *unused)
1296 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1298 spin_lock_irq(&recovery_lock);
1299 if (!timer_pending(&recovery_timer)) {
1300 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1302 mod_timer(&recovery_timer, jiffies +
1303 recovery_delay[recovery_phase] * HZ);
1305 spin_unlock_irq(&recovery_lock);
1307 CIO_MSG_EVENT(3, "recovery: end\n");
1310 static DECLARE_WORK(recovery_work, recovery_work_func);
1312 static void recovery_func(struct timer_list *unused)
1315 * We can't do our recovery in softirq context and it's not
1316 * performance critical, so we schedule it.
1318 schedule_work(&recovery_work);
1321 void ccw_device_schedule_recovery(void)
1323 unsigned long flags;
1325 CIO_MSG_EVENT(3, "recovery: schedule\n");
1326 spin_lock_irqsave(&recovery_lock, flags);
1327 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1329 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1331 spin_unlock_irqrestore(&recovery_lock, flags);
1334 static int purge_fn(struct device *dev, void *data)
1336 struct ccw_device *cdev = to_ccwdev(dev);
1337 struct ccw_dev_id *id = &cdev->private->dev_id;
1339 spin_lock_irq(cdev->ccwlock);
1340 if (is_blacklisted(id->ssid, id->devno) &&
1341 (cdev->private->state == DEV_STATE_OFFLINE) &&
1342 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1343 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1345 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1346 atomic_set(&cdev->private->onoff, 0);
1348 spin_unlock_irq(cdev->ccwlock);
1349 /* Abort loop in case of pending signal. */
1350 if (signal_pending(current))
1357 * ccw_purge_blacklisted - purge unused, blacklisted devices
1359 * Unregister all ccw devices that are offline and on the blacklist.
1361 int ccw_purge_blacklisted(void)
1363 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1364 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1368 void ccw_device_set_disconnected(struct ccw_device *cdev)
1372 ccw_device_set_timeout(cdev, 0);
1373 cdev->private->flags.fake_irb = 0;
1374 cdev->private->state = DEV_STATE_DISCONNECTED;
1376 ccw_device_schedule_recovery();
1379 void ccw_device_set_notoper(struct ccw_device *cdev)
1381 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1383 CIO_TRACE_EVENT(2, "notoper");
1384 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1385 ccw_device_set_timeout(cdev, 0);
1386 cio_disable_subchannel(sch);
1387 cdev->private->state = DEV_STATE_NOT_OPER;
1390 enum io_sch_action {
1394 IO_SCH_UNREG_ATTACH,
1402 static enum io_sch_action sch_get_action(struct subchannel *sch)
1404 struct ccw_device *cdev;
1406 cdev = sch_get_cdev(sch);
1407 if (cio_update_schib(sch)) {
1408 /* Not operational. */
1410 return IO_SCH_UNREG;
1411 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1412 return IO_SCH_UNREG;
1413 return IO_SCH_ORPH_UNREG;
1417 return IO_SCH_ATTACH;
1418 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1419 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1420 return IO_SCH_UNREG_ATTACH;
1421 return IO_SCH_ORPH_ATTACH;
1423 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1424 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1425 return IO_SCH_UNREG;
1428 if (device_is_disconnected(cdev))
1429 return IO_SCH_REPROBE;
1431 return IO_SCH_VERIFY;
1432 if (cdev->private->state == DEV_STATE_NOT_OPER)
1433 return IO_SCH_UNREG_ATTACH;
1438 * io_subchannel_sch_event - process subchannel event
1440 * @process: non-zero if function is called in process context
1442 * An unspecified event occurred for this subchannel. Adjust data according
1443 * to the current operational state of the subchannel and device. Return
1444 * zero when the event has been handled sufficiently or -EAGAIN when this
1445 * function should be called again in process context.
1447 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1449 unsigned long flags;
1450 struct ccw_device *cdev;
1451 struct ccw_dev_id dev_id;
1452 enum io_sch_action action;
1455 spin_lock_irqsave(sch->lock, flags);
1456 if (!device_is_registered(&sch->dev))
1458 if (work_pending(&sch->todo_work))
1460 cdev = sch_get_cdev(sch);
1461 if (cdev && work_pending(&cdev->private->todo_work))
1463 action = sch_get_action(sch);
1464 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1465 sch->schid.ssid, sch->schid.sch_no, process,
1467 /* Perform immediate actions while holding the lock. */
1469 case IO_SCH_REPROBE:
1470 /* Trigger device recognition. */
1471 ccw_device_trigger_reprobe(cdev);
1475 /* Trigger path verification. */
1476 io_subchannel_verify(sch);
1480 ccw_device_set_disconnected(cdev);
1483 case IO_SCH_ORPH_UNREG:
1484 case IO_SCH_ORPH_ATTACH:
1485 ccw_device_set_disconnected(cdev);
1487 case IO_SCH_UNREG_ATTACH:
1491 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1493 * Note: delayed work triggered by this event
1494 * and repeated calls to sch_event are synchronized
1495 * by the above check for work_pending(cdev).
1497 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1499 ccw_device_set_notoper(cdev);
1507 spin_unlock_irqrestore(sch->lock, flags);
1508 /* All other actions require process context. */
1511 /* Handle attached ccw device. */
1513 case IO_SCH_ORPH_UNREG:
1514 case IO_SCH_ORPH_ATTACH:
1515 /* Move ccw device to orphanage. */
1516 rc = ccw_device_move_to_orph(cdev);
1520 case IO_SCH_UNREG_ATTACH:
1521 spin_lock_irqsave(sch->lock, flags);
1522 sch_set_cdev(sch, NULL);
1523 spin_unlock_irqrestore(sch->lock, flags);
1524 /* Unregister ccw device. */
1525 ccw_device_unregister(cdev);
1530 /* Handle subchannel. */
1532 case IO_SCH_ORPH_UNREG:
1534 css_sch_device_unregister(sch);
1536 case IO_SCH_ORPH_ATTACH:
1537 case IO_SCH_UNREG_ATTACH:
1539 dev_id.ssid = sch->schid.ssid;
1540 dev_id.devno = sch->schib.pmcw.dev;
1541 cdev = get_ccwdev_by_dev_id(&dev_id);
1543 sch_create_and_recog_new_device(sch);
1546 rc = ccw_device_move_to_sch(cdev, sch);
1548 /* Release reference from get_ccwdev_by_dev_id() */
1549 put_device(&cdev->dev);
1552 spin_lock_irqsave(sch->lock, flags);
1553 ccw_device_trigger_reprobe(cdev);
1554 spin_unlock_irqrestore(sch->lock, flags);
1555 /* Release reference from get_ccwdev_by_dev_id() */
1556 put_device(&cdev->dev);
1564 spin_unlock_irqrestore(sch->lock, flags);
1569 static void ccw_device_set_int_class(struct ccw_device *cdev)
1571 struct ccw_driver *cdrv = cdev->drv;
1573 /* Note: we interpret class 0 in this context as an uninitialized
1574 * field since it translates to a non-I/O interrupt class. */
1575 if (cdrv->int_class != 0)
1576 cdev->private->int_class = cdrv->int_class;
1578 cdev->private->int_class = IRQIO_CIO;
1581 #ifdef CONFIG_CCW_CONSOLE
1582 int __init ccw_device_enable_console(struct ccw_device *cdev)
1584 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1587 if (!cdev->drv || !cdev->handler)
1590 io_subchannel_init_fields(sch);
1591 rc = cio_commit_config(sch);
1594 sch->driver = &io_subchannel_driver;
1595 io_subchannel_recog(cdev, sch);
1596 /* Now wait for the async. recognition to come to an end. */
1597 spin_lock_irq(cdev->ccwlock);
1598 while (!dev_fsm_final_state(cdev))
1599 ccw_device_wait_idle(cdev);
1601 /* Hold on to an extra reference while device is online. */
1602 get_device(&cdev->dev);
1603 rc = ccw_device_online(cdev);
1607 while (!dev_fsm_final_state(cdev))
1608 ccw_device_wait_idle(cdev);
1610 if (cdev->private->state == DEV_STATE_ONLINE)
1615 spin_unlock_irq(cdev->ccwlock);
1616 if (rc) /* Give up online reference since onlining failed. */
1617 put_device(&cdev->dev);
1621 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1623 struct io_subchannel_private *io_priv;
1624 struct ccw_device *cdev;
1625 struct subchannel *sch;
1627 sch = cio_probe_console();
1629 return ERR_CAST(sch);
1631 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1634 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1635 sizeof(*io_priv->dma_area),
1636 &io_priv->dma_area_dma, GFP_KERNEL);
1637 if (!io_priv->dma_area)
1639 set_io_private(sch, io_priv);
1640 cdev = io_subchannel_create_ccwdev(sch);
1642 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1643 io_priv->dma_area, io_priv->dma_area_dma);
1644 set_io_private(sch, NULL);
1645 put_device(&sch->dev);
1650 ccw_device_set_int_class(cdev);
1656 put_device(&sch->dev);
1657 return ERR_PTR(-ENOMEM);
1660 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1662 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1663 struct io_subchannel_private *io_priv = to_io_private(sch);
1665 set_io_private(sch, NULL);
1666 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1667 io_priv->dma_area, io_priv->dma_area_dma);
1668 put_device(&sch->dev);
1669 put_device(&cdev->dev);
1674 * ccw_device_wait_idle() - busy wait for device to become idle
1677 * Poll until activity control is zero, that is, no function or data
1678 * transfer is pending/active.
1679 * Called with device lock being held.
1681 void ccw_device_wait_idle(struct ccw_device *cdev)
1683 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1687 if (sch->schib.scsw.cmd.actl == 0)
1695 * get_ccwdev_by_busid() - obtain device from a bus id
1696 * @cdrv: driver the device is owned by
1697 * @bus_id: bus id of the device to be searched
1699 * This function searches all devices owned by @cdrv for a device with a bus
1700 * id matching @bus_id.
1702 * If a match is found, its reference count of the found device is increased
1703 * and it is returned; else %NULL is returned.
1705 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1710 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
1712 return dev ? to_ccwdev(dev) : NULL;
1715 /************************** device driver handling ************************/
1717 /* This is the implementation of the ccw_driver class. The probe, remove
1718 * and release methods are initially very similar to the device_driver
1719 * implementations, with the difference that they have ccw_device
1722 * A ccw driver also contains the information that is needed for
1726 ccw_device_probe (struct device *dev)
1728 struct ccw_device *cdev = to_ccwdev(dev);
1729 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1732 cdev->drv = cdrv; /* to let the driver call _set_online */
1733 ccw_device_set_int_class(cdev);
1734 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1737 cdev->private->int_class = IRQIO_CIO;
1744 static void ccw_device_remove(struct device *dev)
1746 struct ccw_device *cdev = to_ccwdev(dev);
1747 struct ccw_driver *cdrv = cdev->drv;
1748 struct subchannel *sch;
1754 spin_lock_irq(cdev->ccwlock);
1757 ret = ccw_device_offline(cdev);
1758 spin_unlock_irq(cdev->ccwlock);
1760 wait_event(cdev->private->wait_q,
1761 dev_fsm_final_state(cdev));
1763 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1764 "device 0.%x.%04x\n",
1765 ret, cdev->private->dev_id.ssid,
1766 cdev->private->dev_id.devno);
1767 /* Give up reference obtained in ccw_device_set_online(). */
1768 put_device(&cdev->dev);
1769 spin_lock_irq(cdev->ccwlock);
1771 ccw_device_set_timeout(cdev, 0);
1773 cdev->private->int_class = IRQIO_CIO;
1774 sch = to_subchannel(cdev->dev.parent);
1775 spin_unlock_irq(cdev->ccwlock);
1776 io_subchannel_quiesce(sch);
1777 __disable_cmf(cdev);
1780 static void ccw_device_shutdown(struct device *dev)
1782 struct ccw_device *cdev;
1784 cdev = to_ccwdev(dev);
1785 if (cdev->drv && cdev->drv->shutdown)
1786 cdev->drv->shutdown(cdev);
1787 __disable_cmf(cdev);
1790 static struct bus_type ccw_bus_type = {
1792 .match = ccw_bus_match,
1793 .uevent = ccw_uevent,
1794 .probe = ccw_device_probe,
1795 .remove = ccw_device_remove,
1796 .shutdown = ccw_device_shutdown,
1800 * ccw_driver_register() - register a ccw driver
1801 * @cdriver: driver to be registered
1803 * This function is mainly a wrapper around driver_register().
1805 * %0 on success and a negative error value on failure.
1807 int ccw_driver_register(struct ccw_driver *cdriver)
1809 struct device_driver *drv = &cdriver->driver;
1811 drv->bus = &ccw_bus_type;
1813 return driver_register(drv);
1817 * ccw_driver_unregister() - deregister a ccw driver
1818 * @cdriver: driver to be deregistered
1820 * This function is mainly a wrapper around driver_unregister().
1822 void ccw_driver_unregister(struct ccw_driver *cdriver)
1824 driver_unregister(&cdriver->driver);
1827 static void ccw_device_todo(struct work_struct *work)
1829 struct ccw_device_private *priv;
1830 struct ccw_device *cdev;
1831 struct subchannel *sch;
1832 enum cdev_todo todo;
1834 priv = container_of(work, struct ccw_device_private, todo_work);
1836 sch = to_subchannel(cdev->dev.parent);
1837 /* Find out todo. */
1838 spin_lock_irq(cdev->ccwlock);
1840 priv->todo = CDEV_TODO_NOTHING;
1841 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1842 priv->dev_id.ssid, priv->dev_id.devno, todo);
1843 spin_unlock_irq(cdev->ccwlock);
1846 case CDEV_TODO_ENABLE_CMF:
1849 case CDEV_TODO_REBIND:
1850 ccw_device_do_unbind_bind(cdev);
1852 case CDEV_TODO_REGISTER:
1853 io_subchannel_register(cdev);
1855 case CDEV_TODO_UNREG_EVAL:
1856 if (!sch_is_pseudo_sch(sch))
1857 css_schedule_eval(sch->schid);
1859 case CDEV_TODO_UNREG:
1860 if (sch_is_pseudo_sch(sch))
1861 ccw_device_unregister(cdev);
1863 ccw_device_call_sch_unregister(cdev);
1868 /* Release workqueue ref. */
1869 put_device(&cdev->dev);
1873 * ccw_device_sched_todo - schedule ccw device operation
1877 * Schedule the operation identified by @todo to be performed on the slow path
1878 * workqueue. Do nothing if another operation with higher priority is already
1879 * scheduled. Needs to be called with ccwdev lock held.
1881 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1883 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1884 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1886 if (cdev->private->todo >= todo)
1888 cdev->private->todo = todo;
1889 /* Get workqueue ref. */
1890 if (!get_device(&cdev->dev))
1892 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
1893 /* Already queued, release workqueue ref. */
1894 put_device(&cdev->dev);
1899 * ccw_device_siosl() - initiate logging
1902 * This function is used to invoke model-dependent logging within the channel
1905 int ccw_device_siosl(struct ccw_device *cdev)
1907 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1909 return chsc_siosl(sch->schid);
1911 EXPORT_SYMBOL_GPL(ccw_device_siosl);
1913 EXPORT_SYMBOL(ccw_device_set_online);
1914 EXPORT_SYMBOL(ccw_device_set_offline);
1915 EXPORT_SYMBOL(ccw_driver_register);
1916 EXPORT_SYMBOL(ccw_driver_unregister);
1917 EXPORT_SYMBOL(get_ccwdev_by_busid);