2 * bus driver for ccw devices
4 * Copyright IBM Corp. 2002, 2008
5 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
6 * Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
12 #define KMSG_COMPONENT "cio"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/device.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/sched/signal.h>
29 #include <asm/ccwdev.h>
31 #include <asm/param.h> /* HZ */
37 #include "cio_debug.h"
42 #include "blacklist.h"
45 static struct timer_list recovery_timer;
46 static DEFINE_SPINLOCK(recovery_lock);
47 static int recovery_phase;
48 static const unsigned long recovery_delay[] = { 3, 30, 300 };
50 static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
51 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
52 static struct bus_type ccw_bus_type;
54 /******************* bus type handling ***********************/
56 /* The Linux driver model distinguishes between a bus type and
57 * the bus itself. Of course we only have one channel
58 * subsystem driver and one channel system per machine, but
59 * we still use the abstraction. T.R. says it's a good idea. */
61 ccw_bus_match (struct device * dev, struct device_driver * drv)
63 struct ccw_device *cdev = to_ccwdev(dev);
64 struct ccw_driver *cdrv = to_ccwdrv(drv);
65 const struct ccw_device_id *ids = cdrv->ids, *found;
70 found = ccw_device_id_match(ids, &cdev->id);
74 cdev->id.driver_info = found->driver_info;
79 /* Store modalias string delimited by prefix/suffix string into buffer with
80 * specified size. Return length of resulting string (excluding trailing '\0')
81 * even if string doesn't fit buffer (snprintf semantics). */
82 static int snprint_alias(char *buf, size_t size,
83 struct ccw_device_id *id, const char *suffix)
87 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
93 if (id->dev_type != 0)
94 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
95 id->dev_model, suffix);
97 len += snprintf(buf, size, "dtdm%s", suffix);
102 /* Set up environment variables for ccw device uevent. Return 0 on success,
103 * non-zero otherwise. */
104 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
106 struct ccw_device *cdev = to_ccwdev(dev);
107 struct ccw_device_id *id = &(cdev->id);
109 char modalias_buf[30];
112 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
117 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
121 /* The next two can be zero, that's ok for us */
123 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
128 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
133 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
134 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
138 static void io_subchannel_irq(struct subchannel *);
139 static int io_subchannel_probe(struct subchannel *);
140 static int io_subchannel_remove(struct subchannel *);
141 static void io_subchannel_shutdown(struct subchannel *);
142 static int io_subchannel_sch_event(struct subchannel *, int);
143 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
145 static void recovery_func(unsigned long data);
147 static struct css_device_id io_subchannel_ids[] = {
148 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
149 { /* end of list */ },
152 static int io_subchannel_prepare(struct subchannel *sch)
154 struct ccw_device *cdev;
156 * Don't allow suspend while a ccw device registration
157 * is still outstanding.
159 cdev = sch_get_cdev(sch);
160 if (cdev && !device_is_registered(&cdev->dev))
165 static int io_subchannel_settle(void)
169 ret = wait_event_interruptible(ccw_device_init_wq,
170 atomic_read(&ccw_device_init_count) == 0);
173 flush_workqueue(cio_work_q);
177 static struct css_driver io_subchannel_driver = {
179 .owner = THIS_MODULE,
180 .name = "io_subchannel",
182 .subchannel_type = io_subchannel_ids,
183 .irq = io_subchannel_irq,
184 .sch_event = io_subchannel_sch_event,
185 .chp_event = io_subchannel_chp_event,
186 .probe = io_subchannel_probe,
187 .remove = io_subchannel_remove,
188 .shutdown = io_subchannel_shutdown,
189 .prepare = io_subchannel_prepare,
190 .settle = io_subchannel_settle,
193 int __init io_subchannel_init(void)
197 setup_timer(&recovery_timer, recovery_func, 0);
198 ret = bus_register(&ccw_bus_type);
201 ret = css_driver_register(&io_subchannel_driver);
203 bus_unregister(&ccw_bus_type);
209 /************************ device handling **************************/
212 * A ccw_device has some interfaces in sysfs in addition to the
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
220 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
222 struct subchannel *sch = to_subchannel(dev);
223 struct chsc_ssd_info *ssd = &sch->ssd_info;
228 for (chp = 0; chp < 8; chp++) {
230 if (ssd->path_mask & mask)
231 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
233 ret += sprintf(buf + ret, "00 ");
235 ret += sprintf (buf+ret, "\n");
236 return min((ssize_t)PAGE_SIZE, ret);
240 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
242 struct subchannel *sch = to_subchannel(dev);
243 struct pmcw *pmcw = &sch->schib.pmcw;
245 return sprintf (buf, "%02x %02x %02x\n",
246 pmcw->pim, pmcw->pam, pmcw->pom);
250 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
255 if (id->dev_type != 0)
256 return sprintf(buf, "%04x/%02x\n",
257 id->dev_type, id->dev_model);
259 return sprintf(buf, "n/a\n");
263 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
268 return sprintf(buf, "%04x/%02x\n",
269 id->cu_type, id->cu_model);
273 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
275 struct ccw_device *cdev = to_ccwdev(dev);
276 struct ccw_device_id *id = &(cdev->id);
279 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
281 return len > PAGE_SIZE ? PAGE_SIZE : len;
285 online_show (struct device *dev, struct device_attribute *attr, char *buf)
287 struct ccw_device *cdev = to_ccwdev(dev);
289 return sprintf(buf, cdev->online ? "1\n" : "0\n");
292 int ccw_device_is_orphan(struct ccw_device *cdev)
294 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
297 static void ccw_device_unregister(struct ccw_device *cdev)
299 if (device_is_registered(&cdev->dev)) {
300 /* Undo device_add(). */
301 device_del(&cdev->dev);
303 if (cdev->private->flags.initialized) {
304 cdev->private->flags.initialized = 0;
305 /* Release reference from device_initialize(). */
306 put_device(&cdev->dev);
310 static void io_subchannel_quiesce(struct subchannel *);
313 * ccw_device_set_offline() - disable a ccw device for I/O
314 * @cdev: target ccw device
316 * This function calls the driver's set_offline() function for @cdev, if
317 * given, and then disables @cdev.
319 * %0 on success and a negative error value on failure.
321 * enabled, ccw device lock not held
323 int ccw_device_set_offline(struct ccw_device *cdev)
325 struct subchannel *sch;
330 if (!cdev->online || !cdev->drv)
333 if (cdev->drv->set_offline) {
334 ret = cdev->drv->set_offline(cdev);
338 spin_lock_irq(cdev->ccwlock);
339 sch = to_subchannel(cdev->dev.parent);
341 /* Wait until a final state or DISCONNECTED is reached */
342 while (!dev_fsm_final_state(cdev) &&
343 cdev->private->state != DEV_STATE_DISCONNECTED) {
344 spin_unlock_irq(cdev->ccwlock);
345 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
346 cdev->private->state == DEV_STATE_DISCONNECTED));
347 spin_lock_irq(cdev->ccwlock);
350 ret = ccw_device_offline(cdev);
353 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
354 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
355 cdev->private->dev_id.devno);
358 state = cdev->private->state;
359 spin_unlock_irq(cdev->ccwlock);
360 io_subchannel_quiesce(sch);
361 spin_lock_irq(cdev->ccwlock);
362 cdev->private->state = state;
363 } while (ret == -EBUSY);
364 spin_unlock_irq(cdev->ccwlock);
365 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
366 cdev->private->state == DEV_STATE_DISCONNECTED));
367 /* Inform the user if set offline failed. */
368 if (cdev->private->state == DEV_STATE_BOXED) {
369 pr_warn("%s: The device entered boxed state while being set offline\n",
370 dev_name(&cdev->dev));
371 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
372 pr_warn("%s: The device stopped operating while being set offline\n",
373 dev_name(&cdev->dev));
375 /* Give up reference from ccw_device_set_online(). */
376 put_device(&cdev->dev);
380 cdev->private->state = DEV_STATE_OFFLINE;
381 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
382 spin_unlock_irq(cdev->ccwlock);
383 /* Give up reference from ccw_device_set_online(). */
384 put_device(&cdev->dev);
389 * ccw_device_set_online() - enable a ccw device for I/O
390 * @cdev: target ccw device
392 * This function first enables @cdev and then calls the driver's set_online()
393 * function for @cdev, if given. If set_online() returns an error, @cdev is
396 * %0 on success and a negative error value on failure.
398 * enabled, ccw device lock not held
400 int ccw_device_set_online(struct ccw_device *cdev)
407 if (cdev->online || !cdev->drv)
409 /* Hold on to an extra reference while device is online. */
410 if (!get_device(&cdev->dev))
413 spin_lock_irq(cdev->ccwlock);
414 ret = ccw_device_online(cdev);
415 spin_unlock_irq(cdev->ccwlock);
417 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
419 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
420 "device 0.%x.%04x\n",
421 ret, cdev->private->dev_id.ssid,
422 cdev->private->dev_id.devno);
423 /* Give up online reference since onlining failed. */
424 put_device(&cdev->dev);
427 spin_lock_irq(cdev->ccwlock);
428 /* Check if online processing was successful */
429 if ((cdev->private->state != DEV_STATE_ONLINE) &&
430 (cdev->private->state != DEV_STATE_W4SENSE)) {
431 spin_unlock_irq(cdev->ccwlock);
432 /* Inform the user that set online failed. */
433 if (cdev->private->state == DEV_STATE_BOXED) {
434 pr_warn("%s: Setting the device online failed because it is boxed\n",
435 dev_name(&cdev->dev));
436 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
437 pr_warn("%s: Setting the device online failed because it is not operational\n",
438 dev_name(&cdev->dev));
440 /* Give up online reference since onlining failed. */
441 put_device(&cdev->dev);
444 spin_unlock_irq(cdev->ccwlock);
445 if (cdev->drv->set_online)
446 ret = cdev->drv->set_online(cdev);
450 spin_lock_irq(cdev->ccwlock);
452 spin_unlock_irq(cdev->ccwlock);
456 spin_lock_irq(cdev->ccwlock);
457 /* Wait until a final state or DISCONNECTED is reached */
458 while (!dev_fsm_final_state(cdev) &&
459 cdev->private->state != DEV_STATE_DISCONNECTED) {
460 spin_unlock_irq(cdev->ccwlock);
461 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
462 cdev->private->state == DEV_STATE_DISCONNECTED));
463 spin_lock_irq(cdev->ccwlock);
465 ret2 = ccw_device_offline(cdev);
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 cdev->private->state == DEV_STATE_DISCONNECTED));
471 /* Give up online reference since onlining failed. */
472 put_device(&cdev->dev);
476 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
477 "device 0.%x.%04x\n",
478 ret2, cdev->private->dev_id.ssid,
479 cdev->private->dev_id.devno);
480 cdev->private->state = DEV_STATE_OFFLINE;
481 spin_unlock_irq(cdev->ccwlock);
482 /* Give up online reference since onlining failed. */
483 put_device(&cdev->dev);
487 static int online_store_handle_offline(struct ccw_device *cdev)
489 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
490 spin_lock_irq(cdev->ccwlock);
491 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
492 spin_unlock_irq(cdev->ccwlock);
495 if (cdev->drv && cdev->drv->set_offline)
496 return ccw_device_set_offline(cdev);
500 static int online_store_recog_and_online(struct ccw_device *cdev)
502 /* Do device recognition, if needed. */
503 if (cdev->private->state == DEV_STATE_BOXED) {
504 spin_lock_irq(cdev->ccwlock);
505 ccw_device_recognition(cdev);
506 spin_unlock_irq(cdev->ccwlock);
507 wait_event(cdev->private->wait_q,
508 cdev->private->flags.recog_done);
509 if (cdev->private->state != DEV_STATE_OFFLINE)
510 /* recognition failed */
513 if (cdev->drv && cdev->drv->set_online)
514 return ccw_device_set_online(cdev);
518 static int online_store_handle_online(struct ccw_device *cdev, int force)
522 ret = online_store_recog_and_online(cdev);
525 if (force && cdev->private->state == DEV_STATE_BOXED) {
526 ret = ccw_device_stlck(cdev);
529 if (cdev->id.cu_type == 0)
530 cdev->private->state = DEV_STATE_NOT_OPER;
531 ret = online_store_recog_and_online(cdev);
538 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
541 struct ccw_device *cdev = to_ccwdev(dev);
545 /* Prevent conflict between multiple on-/offline processing requests. */
546 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
548 /* Prevent conflict between internal I/Os and on-/offline processing. */
549 if (!dev_fsm_final_state(cdev) &&
550 cdev->private->state != DEV_STATE_DISCONNECTED) {
554 /* Prevent conflict between pending work and on-/offline processing.*/
555 if (work_pending(&cdev->private->todo_work)) {
559 if (!strncmp(buf, "force\n", count)) {
565 ret = kstrtoul(buf, 16, &i);
573 ret = online_store_handle_offline(cdev);
576 ret = online_store_handle_online(cdev, force);
584 atomic_set(&cdev->private->onoff, 0);
585 return (ret < 0) ? ret : count;
589 available_show (struct device *dev, struct device_attribute *attr, char *buf)
591 struct ccw_device *cdev = to_ccwdev(dev);
592 struct subchannel *sch;
594 if (ccw_device_is_orphan(cdev))
595 return sprintf(buf, "no device\n");
596 switch (cdev->private->state) {
597 case DEV_STATE_BOXED:
598 return sprintf(buf, "boxed\n");
599 case DEV_STATE_DISCONNECTED:
600 case DEV_STATE_DISCONNECTED_SENSE_ID:
601 case DEV_STATE_NOT_OPER:
602 sch = to_subchannel(dev->parent);
604 return sprintf(buf, "no path\n");
606 return sprintf(buf, "no device\n");
608 /* All other states considered fine. */
609 return sprintf(buf, "good\n");
614 initiate_logging(struct device *dev, struct device_attribute *attr,
615 const char *buf, size_t count)
617 struct subchannel *sch = to_subchannel(dev);
620 rc = chsc_siosl(sch->schid);
622 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
623 sch->schid.ssid, sch->schid.sch_no, rc);
626 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
627 sch->schid.ssid, sch->schid.sch_no);
631 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
634 struct subchannel *sch = to_subchannel(dev);
636 return sprintf(buf, "%02x\n", sch->vpm);
639 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
640 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
641 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
642 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
643 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
644 static DEVICE_ATTR(online, 0644, online_show, online_store);
645 static DEVICE_ATTR(availability, 0444, available_show, NULL);
646 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
647 static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
649 static struct attribute *io_subchannel_attrs[] = {
650 &dev_attr_chpids.attr,
651 &dev_attr_pimpampom.attr,
652 &dev_attr_logging.attr,
657 static struct attribute_group io_subchannel_attr_group = {
658 .attrs = io_subchannel_attrs,
661 static struct attribute * ccwdev_attrs[] = {
662 &dev_attr_devtype.attr,
663 &dev_attr_cutype.attr,
664 &dev_attr_modalias.attr,
665 &dev_attr_online.attr,
666 &dev_attr_cmb_enable.attr,
667 &dev_attr_availability.attr,
671 static struct attribute_group ccwdev_attr_group = {
672 .attrs = ccwdev_attrs,
675 static const struct attribute_group *ccwdev_attr_groups[] = {
680 static int ccw_device_add(struct ccw_device *cdev)
682 struct device *dev = &cdev->dev;
684 dev->bus = &ccw_bus_type;
685 return device_add(dev);
688 static int match_dev_id(struct device *dev, void *data)
690 struct ccw_device *cdev = to_ccwdev(dev);
691 struct ccw_dev_id *dev_id = data;
693 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
697 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
698 * @dev_id: id of the device to be searched
700 * This function searches all devices attached to the ccw bus for a device
703 * If a device is found its reference count is increased and returned;
704 * else %NULL is returned.
706 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
710 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
712 return dev ? to_ccwdev(dev) : NULL;
714 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
716 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
720 if (device_is_registered(&cdev->dev)) {
721 device_release_driver(&cdev->dev);
722 ret = device_attach(&cdev->dev);
723 WARN_ON(ret == -ENODEV);
728 ccw_device_release(struct device *dev)
730 struct ccw_device *cdev;
732 cdev = to_ccwdev(dev);
733 /* Release reference of parent subchannel. */
734 put_device(cdev->dev.parent);
735 kfree(cdev->private);
739 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
741 struct ccw_device *cdev;
743 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
745 cdev->private = kzalloc(sizeof(struct ccw_device_private),
746 GFP_KERNEL | GFP_DMA);
751 return ERR_PTR(-ENOMEM);
754 static void ccw_device_todo(struct work_struct *work);
756 static int io_subchannel_initialize_dev(struct subchannel *sch,
757 struct ccw_device *cdev)
759 struct ccw_device_private *priv = cdev->private;
763 priv->int_class = IRQIO_CIO;
764 priv->state = DEV_STATE_NOT_OPER;
765 priv->dev_id.devno = sch->schib.pmcw.dev;
766 priv->dev_id.ssid = sch->schid.ssid;
768 INIT_WORK(&priv->todo_work, ccw_device_todo);
769 INIT_LIST_HEAD(&priv->cmb_list);
770 init_waitqueue_head(&priv->wait_q);
771 init_timer(&priv->timer);
773 atomic_set(&priv->onoff, 0);
774 cdev->ccwlock = sch->lock;
775 cdev->dev.parent = &sch->dev;
776 cdev->dev.release = ccw_device_release;
777 cdev->dev.groups = ccwdev_attr_groups;
778 /* Do first half of device_register. */
779 device_initialize(&cdev->dev);
780 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
781 cdev->private->dev_id.devno);
784 if (!get_device(&sch->dev)) {
788 priv->flags.initialized = 1;
789 spin_lock_irq(sch->lock);
790 sch_set_cdev(sch, cdev);
791 spin_unlock_irq(sch->lock);
795 /* Release reference from device_initialize(). */
796 put_device(&cdev->dev);
800 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
802 struct ccw_device *cdev;
805 cdev = io_subchannel_allocate_dev(sch);
807 ret = io_subchannel_initialize_dev(sch, cdev);
814 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
816 static void sch_create_and_recog_new_device(struct subchannel *sch)
818 struct ccw_device *cdev;
820 /* Need to allocate a new ccw device. */
821 cdev = io_subchannel_create_ccwdev(sch);
823 /* OK, we did everything we could... */
824 css_sch_device_unregister(sch);
827 /* Start recognition for the new ccw device. */
828 io_subchannel_recog(cdev, sch);
832 * Register recognized device.
834 static void io_subchannel_register(struct ccw_device *cdev)
836 struct subchannel *sch;
837 int ret, adjust_init_count = 1;
840 sch = to_subchannel(cdev->dev.parent);
842 * Check if subchannel is still registered. It may have become
843 * unregistered if a machine check hit us after finishing
844 * device recognition but before the register work could be
847 if (!device_is_registered(&sch->dev))
849 css_update_ssd_info(sch);
851 * io_subchannel_register() will also be called after device
852 * recognition has been done for a boxed device (which will already
853 * be registered). We need to reprobe since we may now have sense id
856 if (device_is_registered(&cdev->dev)) {
858 ret = device_reprobe(&cdev->dev);
860 /* We can't do much here. */
861 CIO_MSG_EVENT(0, "device_reprobe() returned"
862 " %d for 0.%x.%04x\n", ret,
863 cdev->private->dev_id.ssid,
864 cdev->private->dev_id.devno);
866 adjust_init_count = 0;
870 * Now we know this subchannel will stay, we can throw
871 * our delayed uevent.
873 dev_set_uevent_suppress(&sch->dev, 0);
874 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
875 /* make it known to the system */
876 ret = ccw_device_add(cdev);
878 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
879 cdev->private->dev_id.ssid,
880 cdev->private->dev_id.devno, ret);
881 spin_lock_irqsave(sch->lock, flags);
882 sch_set_cdev(sch, NULL);
883 spin_unlock_irqrestore(sch->lock, flags);
884 /* Release initial device reference. */
885 put_device(&cdev->dev);
889 cdev->private->flags.recog_done = 1;
890 wake_up(&cdev->private->wait_q);
892 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
893 wake_up(&ccw_device_init_wq);
896 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
898 struct subchannel *sch;
900 /* Get subchannel reference for local processing. */
901 if (!get_device(cdev->dev.parent))
903 sch = to_subchannel(cdev->dev.parent);
904 css_sch_device_unregister(sch);
905 /* Release subchannel reference for local processing. */
906 put_device(&sch->dev);
910 * subchannel recognition done. Called from the state machine.
913 io_subchannel_recog_done(struct ccw_device *cdev)
915 if (css_init_done == 0) {
916 cdev->private->flags.recog_done = 1;
919 switch (cdev->private->state) {
920 case DEV_STATE_BOXED:
921 /* Device did not respond in time. */
922 case DEV_STATE_NOT_OPER:
923 cdev->private->flags.recog_done = 1;
924 /* Remove device found not operational. */
925 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
926 if (atomic_dec_and_test(&ccw_device_init_count))
927 wake_up(&ccw_device_init_wq);
929 case DEV_STATE_OFFLINE:
931 * We can't register the device in interrupt context so
932 * we schedule a work item.
934 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
939 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
941 /* Increase counter of devices currently in recognition. */
942 atomic_inc(&ccw_device_init_count);
944 /* Start async. device sensing. */
945 spin_lock_irq(sch->lock);
946 ccw_device_recognition(cdev);
947 spin_unlock_irq(sch->lock);
950 static int ccw_device_move_to_sch(struct ccw_device *cdev,
951 struct subchannel *sch)
953 struct subchannel *old_sch;
954 int rc, old_enabled = 0;
956 old_sch = to_subchannel(cdev->dev.parent);
957 /* Obtain child reference for new parent. */
958 if (!get_device(&sch->dev))
961 if (!sch_is_pseudo_sch(old_sch)) {
962 spin_lock_irq(old_sch->lock);
963 old_enabled = old_sch->schib.pmcw.ena;
966 rc = cio_disable_subchannel(old_sch);
967 spin_unlock_irq(old_sch->lock);
969 /* Release child reference for new parent. */
970 put_device(&sch->dev);
975 mutex_lock(&sch->reg_mutex);
976 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
977 mutex_unlock(&sch->reg_mutex);
979 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
980 cdev->private->dev_id.ssid,
981 cdev->private->dev_id.devno, sch->schid.ssid,
982 sch->schib.pmcw.dev, rc);
984 /* Try to reenable the old subchannel. */
985 spin_lock_irq(old_sch->lock);
986 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
987 spin_unlock_irq(old_sch->lock);
989 /* Release child reference for new parent. */
990 put_device(&sch->dev);
993 /* Clean up old subchannel. */
994 if (!sch_is_pseudo_sch(old_sch)) {
995 spin_lock_irq(old_sch->lock);
996 sch_set_cdev(old_sch, NULL);
997 spin_unlock_irq(old_sch->lock);
998 css_schedule_eval(old_sch->schid);
1000 /* Release child reference for old parent. */
1001 put_device(&old_sch->dev);
1002 /* Initialize new subchannel. */
1003 spin_lock_irq(sch->lock);
1004 cdev->ccwlock = sch->lock;
1005 if (!sch_is_pseudo_sch(sch))
1006 sch_set_cdev(sch, cdev);
1007 spin_unlock_irq(sch->lock);
1008 if (!sch_is_pseudo_sch(sch))
1009 css_update_ssd_info(sch);
1013 static int ccw_device_move_to_orph(struct ccw_device *cdev)
1015 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1016 struct channel_subsystem *css = to_css(sch->dev.parent);
1018 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1021 static void io_subchannel_irq(struct subchannel *sch)
1023 struct ccw_device *cdev;
1025 cdev = sch_get_cdev(sch);
1027 CIO_TRACE_EVENT(6, "IRQ");
1028 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1030 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1032 inc_irq_stat(IRQIO_CIO);
1035 void io_subchannel_init_config(struct subchannel *sch)
1037 memset(&sch->config, 0, sizeof(sch->config));
1038 sch->config.csense = 1;
1041 static void io_subchannel_init_fields(struct subchannel *sch)
1043 if (cio_is_console(sch->schid))
1046 sch->opm = chp_get_sch_opm(sch);
1047 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1048 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1050 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1051 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1052 sch->schib.pmcw.dev, sch->schid.ssid,
1053 sch->schid.sch_no, sch->schib.pmcw.pim,
1054 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1056 io_subchannel_init_config(sch);
1060 * Note: We always return 0 so that we bind to the device even on error.
1061 * This is needed so that our remove function is called on unregister.
1063 static int io_subchannel_probe(struct subchannel *sch)
1065 struct io_subchannel_private *io_priv;
1066 struct ccw_device *cdev;
1069 if (cio_is_console(sch->schid)) {
1070 rc = sysfs_create_group(&sch->dev.kobj,
1071 &io_subchannel_attr_group);
1073 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1074 "attributes for subchannel "
1075 "0.%x.%04x (rc=%d)\n",
1076 sch->schid.ssid, sch->schid.sch_no, rc);
1078 * The console subchannel already has an associated ccw_device.
1079 * Throw the delayed uevent for the subchannel, register
1080 * the ccw_device and exit.
1082 dev_set_uevent_suppress(&sch->dev, 0);
1083 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1084 cdev = sch_get_cdev(sch);
1085 rc = ccw_device_add(cdev);
1087 /* Release online reference. */
1088 put_device(&cdev->dev);
1091 if (atomic_dec_and_test(&ccw_device_init_count))
1092 wake_up(&ccw_device_init_wq);
1095 io_subchannel_init_fields(sch);
1096 rc = cio_commit_config(sch);
1099 rc = sysfs_create_group(&sch->dev.kobj,
1100 &io_subchannel_attr_group);
1103 /* Allocate I/O subchannel private data. */
1104 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1108 set_io_private(sch, io_priv);
1109 css_schedule_eval(sch->schid);
1113 spin_lock_irq(sch->lock);
1114 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1115 spin_unlock_irq(sch->lock);
1120 io_subchannel_remove (struct subchannel *sch)
1122 struct io_subchannel_private *io_priv = to_io_private(sch);
1123 struct ccw_device *cdev;
1125 cdev = sch_get_cdev(sch);
1128 io_subchannel_quiesce(sch);
1129 /* Set ccw device to not operational and drop reference. */
1130 spin_lock_irq(cdev->ccwlock);
1131 sch_set_cdev(sch, NULL);
1132 set_io_private(sch, NULL);
1133 cdev->private->state = DEV_STATE_NOT_OPER;
1134 spin_unlock_irq(cdev->ccwlock);
1135 ccw_device_unregister(cdev);
1138 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1142 static void io_subchannel_verify(struct subchannel *sch)
1144 struct ccw_device *cdev;
1146 cdev = sch_get_cdev(sch);
1148 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1151 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1153 struct ccw_device *cdev;
1155 cdev = sch_get_cdev(sch);
1158 if (cio_update_schib(sch))
1160 /* Check for I/O on path. */
1161 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1163 if (cdev->private->state == DEV_STATE_ONLINE) {
1164 ccw_device_kill_io(cdev);
1170 /* Trigger path verification. */
1171 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1175 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1178 static int io_subchannel_chp_event(struct subchannel *sch,
1179 struct chp_link *link, int event)
1181 struct ccw_device *cdev = sch_get_cdev(sch);
1184 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1192 cdev->private->path_gone_mask |= mask;
1193 io_subchannel_terminate_path(sch, mask);
1199 cdev->private->path_new_mask |= mask;
1200 io_subchannel_verify(sch);
1203 if (cio_update_schib(sch))
1206 cdev->private->path_gone_mask |= mask;
1207 io_subchannel_terminate_path(sch, mask);
1210 if (cio_update_schib(sch))
1212 sch->lpm |= mask & sch->opm;
1214 cdev->private->path_new_mask |= mask;
1215 io_subchannel_verify(sch);
1221 static void io_subchannel_quiesce(struct subchannel *sch)
1223 struct ccw_device *cdev;
1226 spin_lock_irq(sch->lock);
1227 cdev = sch_get_cdev(sch);
1228 if (cio_is_console(sch->schid))
1230 if (!sch->schib.pmcw.ena)
1232 ret = cio_disable_subchannel(sch);
1236 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1237 while (ret == -EBUSY) {
1238 cdev->private->state = DEV_STATE_QUIESCE;
1239 cdev->private->iretry = 255;
1240 ret = ccw_device_cancel_halt_clear(cdev);
1241 if (ret == -EBUSY) {
1242 ccw_device_set_timeout(cdev, HZ/10);
1243 spin_unlock_irq(sch->lock);
1244 wait_event(cdev->private->wait_q,
1245 cdev->private->state != DEV_STATE_QUIESCE);
1246 spin_lock_irq(sch->lock);
1248 ret = cio_disable_subchannel(sch);
1251 spin_unlock_irq(sch->lock);
1254 static void io_subchannel_shutdown(struct subchannel *sch)
1256 io_subchannel_quiesce(sch);
1259 static int device_is_disconnected(struct ccw_device *cdev)
1263 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1264 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1267 static int recovery_check(struct device *dev, void *data)
1269 struct ccw_device *cdev = to_ccwdev(dev);
1272 spin_lock_irq(cdev->ccwlock);
1273 switch (cdev->private->state) {
1274 case DEV_STATE_DISCONNECTED:
1275 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1276 cdev->private->dev_id.ssid,
1277 cdev->private->dev_id.devno);
1278 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1281 case DEV_STATE_DISCONNECTED_SENSE_ID:
1285 spin_unlock_irq(cdev->ccwlock);
1290 static void recovery_work_func(struct work_struct *unused)
1294 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1296 spin_lock_irq(&recovery_lock);
1297 if (!timer_pending(&recovery_timer)) {
1298 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1300 mod_timer(&recovery_timer, jiffies +
1301 recovery_delay[recovery_phase] * HZ);
1303 spin_unlock_irq(&recovery_lock);
1305 CIO_MSG_EVENT(4, "recovery: end\n");
1308 static DECLARE_WORK(recovery_work, recovery_work_func);
1310 static void recovery_func(unsigned long data)
1313 * We can't do our recovery in softirq context and it's not
1314 * performance critical, so we schedule it.
1316 schedule_work(&recovery_work);
1319 static void ccw_device_schedule_recovery(void)
1321 unsigned long flags;
1323 CIO_MSG_EVENT(4, "recovery: schedule\n");
1324 spin_lock_irqsave(&recovery_lock, flags);
1325 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1327 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1329 spin_unlock_irqrestore(&recovery_lock, flags);
1332 static int purge_fn(struct device *dev, void *data)
1334 struct ccw_device *cdev = to_ccwdev(dev);
1335 struct ccw_dev_id *id = &cdev->private->dev_id;
1337 spin_lock_irq(cdev->ccwlock);
1338 if (is_blacklisted(id->ssid, id->devno) &&
1339 (cdev->private->state == DEV_STATE_OFFLINE) &&
1340 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1341 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1343 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1344 atomic_set(&cdev->private->onoff, 0);
1346 spin_unlock_irq(cdev->ccwlock);
1347 /* Abort loop in case of pending signal. */
1348 if (signal_pending(current))
1355 * ccw_purge_blacklisted - purge unused, blacklisted devices
1357 * Unregister all ccw devices that are offline and on the blacklist.
1359 int ccw_purge_blacklisted(void)
1361 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1362 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1366 void ccw_device_set_disconnected(struct ccw_device *cdev)
1370 ccw_device_set_timeout(cdev, 0);
1371 cdev->private->flags.fake_irb = 0;
1372 cdev->private->state = DEV_STATE_DISCONNECTED;
1374 ccw_device_schedule_recovery();
1377 void ccw_device_set_notoper(struct ccw_device *cdev)
1379 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1381 CIO_TRACE_EVENT(2, "notoper");
1382 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1383 ccw_device_set_timeout(cdev, 0);
1384 cio_disable_subchannel(sch);
1385 cdev->private->state = DEV_STATE_NOT_OPER;
1388 enum io_sch_action {
1392 IO_SCH_UNREG_ATTACH,
1400 static enum io_sch_action sch_get_action(struct subchannel *sch)
1402 struct ccw_device *cdev;
1404 cdev = sch_get_cdev(sch);
1405 if (cio_update_schib(sch)) {
1406 /* Not operational. */
1408 return IO_SCH_UNREG;
1409 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1410 return IO_SCH_UNREG;
1411 return IO_SCH_ORPH_UNREG;
1415 return IO_SCH_ATTACH;
1416 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1417 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1418 return IO_SCH_UNREG_ATTACH;
1419 return IO_SCH_ORPH_ATTACH;
1421 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1422 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1423 return IO_SCH_UNREG;
1426 if (device_is_disconnected(cdev))
1427 return IO_SCH_REPROBE;
1428 if (cdev->online && !cdev->private->flags.resuming)
1429 return IO_SCH_VERIFY;
1430 if (cdev->private->state == DEV_STATE_NOT_OPER)
1431 return IO_SCH_UNREG_ATTACH;
1436 * io_subchannel_sch_event - process subchannel event
1438 * @process: non-zero if function is called in process context
1440 * An unspecified event occurred for this subchannel. Adjust data according
1441 * to the current operational state of the subchannel and device. Return
1442 * zero when the event has been handled sufficiently or -EAGAIN when this
1443 * function should be called again in process context.
1445 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1447 unsigned long flags;
1448 struct ccw_device *cdev;
1449 struct ccw_dev_id dev_id;
1450 enum io_sch_action action;
1453 spin_lock_irqsave(sch->lock, flags);
1454 if (!device_is_registered(&sch->dev))
1456 if (work_pending(&sch->todo_work))
1458 cdev = sch_get_cdev(sch);
1459 if (cdev && work_pending(&cdev->private->todo_work))
1461 action = sch_get_action(sch);
1462 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1463 sch->schid.ssid, sch->schid.sch_no, process,
1465 /* Perform immediate actions while holding the lock. */
1467 case IO_SCH_REPROBE:
1468 /* Trigger device recognition. */
1469 ccw_device_trigger_reprobe(cdev);
1473 /* Trigger path verification. */
1474 io_subchannel_verify(sch);
1478 ccw_device_set_disconnected(cdev);
1481 case IO_SCH_ORPH_UNREG:
1482 case IO_SCH_ORPH_ATTACH:
1483 ccw_device_set_disconnected(cdev);
1485 case IO_SCH_UNREG_ATTACH:
1489 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1491 * Note: delayed work triggered by this event
1492 * and repeated calls to sch_event are synchronized
1493 * by the above check for work_pending(cdev).
1495 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1497 ccw_device_set_notoper(cdev);
1505 spin_unlock_irqrestore(sch->lock, flags);
1506 /* All other actions require process context. */
1509 /* Handle attached ccw device. */
1511 case IO_SCH_ORPH_UNREG:
1512 case IO_SCH_ORPH_ATTACH:
1513 /* Move ccw device to orphanage. */
1514 rc = ccw_device_move_to_orph(cdev);
1518 case IO_SCH_UNREG_ATTACH:
1519 spin_lock_irqsave(sch->lock, flags);
1520 if (cdev->private->flags.resuming) {
1521 /* Device will be handled later. */
1525 sch_set_cdev(sch, NULL);
1526 spin_unlock_irqrestore(sch->lock, flags);
1527 /* Unregister ccw device. */
1528 ccw_device_unregister(cdev);
1533 /* Handle subchannel. */
1535 case IO_SCH_ORPH_UNREG:
1537 if (!cdev || !cdev->private->flags.resuming)
1538 css_sch_device_unregister(sch);
1540 case IO_SCH_ORPH_ATTACH:
1541 case IO_SCH_UNREG_ATTACH:
1543 dev_id.ssid = sch->schid.ssid;
1544 dev_id.devno = sch->schib.pmcw.dev;
1545 cdev = get_ccwdev_by_dev_id(&dev_id);
1547 sch_create_and_recog_new_device(sch);
1550 rc = ccw_device_move_to_sch(cdev, sch);
1552 /* Release reference from get_ccwdev_by_dev_id() */
1553 put_device(&cdev->dev);
1556 spin_lock_irqsave(sch->lock, flags);
1557 ccw_device_trigger_reprobe(cdev);
1558 spin_unlock_irqrestore(sch->lock, flags);
1559 /* Release reference from get_ccwdev_by_dev_id() */
1560 put_device(&cdev->dev);
1568 spin_unlock_irqrestore(sch->lock, flags);
1573 static void ccw_device_set_int_class(struct ccw_device *cdev)
1575 struct ccw_driver *cdrv = cdev->drv;
1577 /* Note: we interpret class 0 in this context as an uninitialized
1578 * field since it translates to a non-I/O interrupt class. */
1579 if (cdrv->int_class != 0)
1580 cdev->private->int_class = cdrv->int_class;
1582 cdev->private->int_class = IRQIO_CIO;
1585 #ifdef CONFIG_CCW_CONSOLE
1586 int __init ccw_device_enable_console(struct ccw_device *cdev)
1588 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1591 if (!cdev->drv || !cdev->handler)
1594 io_subchannel_init_fields(sch);
1595 rc = cio_commit_config(sch);
1598 sch->driver = &io_subchannel_driver;
1599 io_subchannel_recog(cdev, sch);
1600 /* Now wait for the async. recognition to come to an end. */
1601 spin_lock_irq(cdev->ccwlock);
1602 while (!dev_fsm_final_state(cdev))
1603 ccw_device_wait_idle(cdev);
1605 /* Hold on to an extra reference while device is online. */
1606 get_device(&cdev->dev);
1607 rc = ccw_device_online(cdev);
1611 while (!dev_fsm_final_state(cdev))
1612 ccw_device_wait_idle(cdev);
1614 if (cdev->private->state == DEV_STATE_ONLINE)
1619 spin_unlock_irq(cdev->ccwlock);
1620 if (rc) /* Give up online reference since onlining failed. */
1621 put_device(&cdev->dev);
1625 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1627 struct io_subchannel_private *io_priv;
1628 struct ccw_device *cdev;
1629 struct subchannel *sch;
1631 sch = cio_probe_console();
1633 return ERR_CAST(sch);
1635 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1637 put_device(&sch->dev);
1638 return ERR_PTR(-ENOMEM);
1640 set_io_private(sch, io_priv);
1641 cdev = io_subchannel_create_ccwdev(sch);
1643 put_device(&sch->dev);
1648 ccw_device_set_int_class(cdev);
1652 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1654 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1655 struct io_subchannel_private *io_priv = to_io_private(sch);
1657 set_io_private(sch, NULL);
1658 put_device(&sch->dev);
1659 put_device(&cdev->dev);
1664 * ccw_device_wait_idle() - busy wait for device to become idle
1667 * Poll until activity control is zero, that is, no function or data
1668 * transfer is pending/active.
1669 * Called with device lock being held.
1671 void ccw_device_wait_idle(struct ccw_device *cdev)
1673 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1677 if (sch->schib.scsw.cmd.actl == 0)
1683 static int ccw_device_pm_restore(struct device *dev);
1685 int ccw_device_force_console(struct ccw_device *cdev)
1687 return ccw_device_pm_restore(&cdev->dev);
1689 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1693 * get ccw_device matching the busid, but only if owned by cdrv
1696 __ccwdev_check_busid(struct device *dev, void *id)
1702 return (strcmp(bus_id, dev_name(dev)) == 0);
1707 * get_ccwdev_by_busid() - obtain device from a bus id
1708 * @cdrv: driver the device is owned by
1709 * @bus_id: bus id of the device to be searched
1711 * This function searches all devices owned by @cdrv for a device with a bus
1712 * id matching @bus_id.
1714 * If a match is found, its reference count of the found device is increased
1715 * and it is returned; else %NULL is returned.
1717 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1722 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1723 __ccwdev_check_busid);
1725 return dev ? to_ccwdev(dev) : NULL;
1728 /************************** device driver handling ************************/
1730 /* This is the implementation of the ccw_driver class. The probe, remove
1731 * and release methods are initially very similar to the device_driver
1732 * implementations, with the difference that they have ccw_device
1735 * A ccw driver also contains the information that is needed for
1739 ccw_device_probe (struct device *dev)
1741 struct ccw_device *cdev = to_ccwdev(dev);
1742 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1745 cdev->drv = cdrv; /* to let the driver call _set_online */
1746 ccw_device_set_int_class(cdev);
1747 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1750 cdev->private->int_class = IRQIO_CIO;
1757 static int ccw_device_remove(struct device *dev)
1759 struct ccw_device *cdev = to_ccwdev(dev);
1760 struct ccw_driver *cdrv = cdev->drv;
1766 spin_lock_irq(cdev->ccwlock);
1769 ret = ccw_device_offline(cdev);
1770 spin_unlock_irq(cdev->ccwlock);
1772 wait_event(cdev->private->wait_q,
1773 dev_fsm_final_state(cdev));
1775 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1776 "device 0.%x.%04x\n",
1777 ret, cdev->private->dev_id.ssid,
1778 cdev->private->dev_id.devno);
1779 /* Give up reference obtained in ccw_device_set_online(). */
1780 put_device(&cdev->dev);
1781 spin_lock_irq(cdev->ccwlock);
1783 ccw_device_set_timeout(cdev, 0);
1785 cdev->private->int_class = IRQIO_CIO;
1786 spin_unlock_irq(cdev->ccwlock);
1787 __disable_cmf(cdev);
1792 static void ccw_device_shutdown(struct device *dev)
1794 struct ccw_device *cdev;
1796 cdev = to_ccwdev(dev);
1797 if (cdev->drv && cdev->drv->shutdown)
1798 cdev->drv->shutdown(cdev);
1799 __disable_cmf(cdev);
1802 static int ccw_device_pm_prepare(struct device *dev)
1804 struct ccw_device *cdev = to_ccwdev(dev);
1806 if (work_pending(&cdev->private->todo_work))
1808 /* Fail while device is being set online/offline. */
1809 if (atomic_read(&cdev->private->onoff))
1812 if (cdev->online && cdev->drv && cdev->drv->prepare)
1813 return cdev->drv->prepare(cdev);
1818 static void ccw_device_pm_complete(struct device *dev)
1820 struct ccw_device *cdev = to_ccwdev(dev);
1822 if (cdev->online && cdev->drv && cdev->drv->complete)
1823 cdev->drv->complete(cdev);
1826 static int ccw_device_pm_freeze(struct device *dev)
1828 struct ccw_device *cdev = to_ccwdev(dev);
1829 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1830 int ret, cm_enabled;
1832 /* Fail suspend while device is in transistional state. */
1833 if (!dev_fsm_final_state(cdev))
1837 if (cdev->drv && cdev->drv->freeze) {
1838 ret = cdev->drv->freeze(cdev);
1843 spin_lock_irq(sch->lock);
1844 cm_enabled = cdev->private->cmb != NULL;
1845 spin_unlock_irq(sch->lock);
1847 /* Don't have the css write on memory. */
1848 ret = ccw_set_cmf(cdev, 0);
1852 /* From here on, disallow device driver I/O. */
1853 spin_lock_irq(sch->lock);
1854 ret = cio_disable_subchannel(sch);
1855 spin_unlock_irq(sch->lock);
1860 static int ccw_device_pm_thaw(struct device *dev)
1862 struct ccw_device *cdev = to_ccwdev(dev);
1863 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1864 int ret, cm_enabled;
1869 spin_lock_irq(sch->lock);
1870 /* Allow device driver I/O again. */
1871 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1872 cm_enabled = cdev->private->cmb != NULL;
1873 spin_unlock_irq(sch->lock);
1878 ret = ccw_set_cmf(cdev, 1);
1883 if (cdev->drv && cdev->drv->thaw)
1884 ret = cdev->drv->thaw(cdev);
1889 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1891 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1893 spin_lock_irq(sch->lock);
1894 if (cio_is_console(sch->schid)) {
1895 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1899 * While we were sleeping, devices may have gone or become
1900 * available again. Kick re-detection.
1902 cdev->private->flags.resuming = 1;
1903 cdev->private->path_new_mask = LPM_ANYPATH;
1904 css_sched_sch_todo(sch, SCH_TODO_EVAL);
1905 spin_unlock_irq(sch->lock);
1906 css_wait_for_slow_path();
1908 /* cdev may have been moved to a different subchannel. */
1909 sch = to_subchannel(cdev->dev.parent);
1910 spin_lock_irq(sch->lock);
1911 if (cdev->private->state != DEV_STATE_ONLINE &&
1912 cdev->private->state != DEV_STATE_OFFLINE)
1915 ccw_device_recognition(cdev);
1916 spin_unlock_irq(sch->lock);
1917 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1918 cdev->private->state == DEV_STATE_DISCONNECTED);
1919 spin_lock_irq(sch->lock);
1922 cdev->private->flags.resuming = 0;
1923 spin_unlock_irq(sch->lock);
1926 static int resume_handle_boxed(struct ccw_device *cdev)
1928 cdev->private->state = DEV_STATE_BOXED;
1929 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1931 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1935 static int resume_handle_disc(struct ccw_device *cdev)
1937 cdev->private->state = DEV_STATE_DISCONNECTED;
1938 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1940 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1944 static int ccw_device_pm_restore(struct device *dev)
1946 struct ccw_device *cdev = to_ccwdev(dev);
1947 struct subchannel *sch;
1950 __ccw_device_pm_restore(cdev);
1951 sch = to_subchannel(cdev->dev.parent);
1952 spin_lock_irq(sch->lock);
1953 if (cio_is_console(sch->schid))
1956 /* check recognition results */
1957 switch (cdev->private->state) {
1958 case DEV_STATE_OFFLINE:
1959 case DEV_STATE_ONLINE:
1960 cdev->private->flags.donotify = 0;
1962 case DEV_STATE_BOXED:
1963 ret = resume_handle_boxed(cdev);
1968 ret = resume_handle_disc(cdev);
1973 /* check if the device type has changed */
1974 if (!ccw_device_test_sense_data(cdev)) {
1975 ccw_device_update_sense_data(cdev);
1976 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1983 if (ccw_device_online(cdev)) {
1984 ret = resume_handle_disc(cdev);
1989 spin_unlock_irq(sch->lock);
1990 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1991 spin_lock_irq(sch->lock);
1993 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1994 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1999 /* reenable cmf, if needed */
2000 if (cdev->private->cmb) {
2001 spin_unlock_irq(sch->lock);
2002 ret = ccw_set_cmf(cdev, 1);
2003 spin_lock_irq(sch->lock);
2005 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2006 "(rc=%d)\n", cdev->private->dev_id.ssid,
2007 cdev->private->dev_id.devno, ret);
2013 spin_unlock_irq(sch->lock);
2014 if (cdev->online && cdev->drv && cdev->drv->restore)
2015 ret = cdev->drv->restore(cdev);
2019 spin_unlock_irq(sch->lock);
2023 static const struct dev_pm_ops ccw_pm_ops = {
2024 .prepare = ccw_device_pm_prepare,
2025 .complete = ccw_device_pm_complete,
2026 .freeze = ccw_device_pm_freeze,
2027 .thaw = ccw_device_pm_thaw,
2028 .restore = ccw_device_pm_restore,
2031 static struct bus_type ccw_bus_type = {
2033 .match = ccw_bus_match,
2034 .uevent = ccw_uevent,
2035 .probe = ccw_device_probe,
2036 .remove = ccw_device_remove,
2037 .shutdown = ccw_device_shutdown,
2042 * ccw_driver_register() - register a ccw driver
2043 * @cdriver: driver to be registered
2045 * This function is mainly a wrapper around driver_register().
2047 * %0 on success and a negative error value on failure.
2049 int ccw_driver_register(struct ccw_driver *cdriver)
2051 struct device_driver *drv = &cdriver->driver;
2053 drv->bus = &ccw_bus_type;
2055 return driver_register(drv);
2059 * ccw_driver_unregister() - deregister a ccw driver
2060 * @cdriver: driver to be deregistered
2062 * This function is mainly a wrapper around driver_unregister().
2064 void ccw_driver_unregister(struct ccw_driver *cdriver)
2066 driver_unregister(&cdriver->driver);
2069 static void ccw_device_todo(struct work_struct *work)
2071 struct ccw_device_private *priv;
2072 struct ccw_device *cdev;
2073 struct subchannel *sch;
2074 enum cdev_todo todo;
2076 priv = container_of(work, struct ccw_device_private, todo_work);
2078 sch = to_subchannel(cdev->dev.parent);
2079 /* Find out todo. */
2080 spin_lock_irq(cdev->ccwlock);
2082 priv->todo = CDEV_TODO_NOTHING;
2083 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2084 priv->dev_id.ssid, priv->dev_id.devno, todo);
2085 spin_unlock_irq(cdev->ccwlock);
2088 case CDEV_TODO_ENABLE_CMF:
2091 case CDEV_TODO_REBIND:
2092 ccw_device_do_unbind_bind(cdev);
2094 case CDEV_TODO_REGISTER:
2095 io_subchannel_register(cdev);
2097 case CDEV_TODO_UNREG_EVAL:
2098 if (!sch_is_pseudo_sch(sch))
2099 css_schedule_eval(sch->schid);
2101 case CDEV_TODO_UNREG:
2102 if (sch_is_pseudo_sch(sch))
2103 ccw_device_unregister(cdev);
2105 ccw_device_call_sch_unregister(cdev);
2110 /* Release workqueue ref. */
2111 put_device(&cdev->dev);
2115 * ccw_device_sched_todo - schedule ccw device operation
2119 * Schedule the operation identified by @todo to be performed on the slow path
2120 * workqueue. Do nothing if another operation with higher priority is already
2121 * scheduled. Needs to be called with ccwdev lock held.
2123 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2125 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2126 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2128 if (cdev->private->todo >= todo)
2130 cdev->private->todo = todo;
2131 /* Get workqueue ref. */
2132 if (!get_device(&cdev->dev))
2134 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2135 /* Already queued, release workqueue ref. */
2136 put_device(&cdev->dev);
2141 * ccw_device_siosl() - initiate logging
2144 * This function is used to invoke model-dependent logging within the channel
2147 int ccw_device_siosl(struct ccw_device *cdev)
2149 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2151 return chsc_siosl(sch->schid);
2153 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2155 EXPORT_SYMBOL(ccw_device_set_online);
2156 EXPORT_SYMBOL(ccw_device_set_offline);
2157 EXPORT_SYMBOL(ccw_driver_register);
2158 EXPORT_SYMBOL(ccw_driver_unregister);
2159 EXPORT_SYMBOL(get_ccwdev_by_busid);