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)
10 #define KMSG_COMPONENT "cio"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/device.h>
21 #include <linux/workqueue.h>
22 #include <linux/delay.h>
23 #include <linux/timer.h>
24 #include <linux/kernel_stat.h>
26 #include <asm/ccwdev.h>
28 #include <asm/param.h> /* HZ */
34 #include "cio_debug.h"
39 #include "blacklist.h"
42 static struct timer_list recovery_timer;
43 static DEFINE_SPINLOCK(recovery_lock);
44 static int recovery_phase;
45 static const unsigned long recovery_delay[] = { 3, 30, 300 };
47 static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
48 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
49 static struct bus_type ccw_bus_type;
51 /******************* bus type handling ***********************/
53 /* The Linux driver model distinguishes between a bus type and
54 * the bus itself. Of course we only have one channel
55 * subsystem driver and one channel system per machine, but
56 * we still use the abstraction. T.R. says it's a good idea. */
58 ccw_bus_match (struct device * dev, struct device_driver * drv)
60 struct ccw_device *cdev = to_ccwdev(dev);
61 struct ccw_driver *cdrv = to_ccwdrv(drv);
62 const struct ccw_device_id *ids = cdrv->ids, *found;
67 found = ccw_device_id_match(ids, &cdev->id);
71 cdev->id.driver_info = found->driver_info;
76 /* Store modalias string delimited by prefix/suffix string into buffer with
77 * specified size. Return length of resulting string (excluding trailing '\0')
78 * even if string doesn't fit buffer (snprintf semantics). */
79 static int snprint_alias(char *buf, size_t size,
80 struct ccw_device_id *id, const char *suffix)
84 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
90 if (id->dev_type != 0)
91 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
92 id->dev_model, suffix);
94 len += snprintf(buf, size, "dtdm%s", suffix);
99 /* Set up environment variables for ccw device uevent. Return 0 on success,
100 * non-zero otherwise. */
101 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
103 struct ccw_device *cdev = to_ccwdev(dev);
104 struct ccw_device_id *id = &(cdev->id);
106 char modalias_buf[30];
109 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
114 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
118 /* The next two can be zero, that's ok for us */
120 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
125 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
130 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
131 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
135 static void io_subchannel_irq(struct subchannel *);
136 static int io_subchannel_probe(struct subchannel *);
137 static int io_subchannel_remove(struct subchannel *);
138 static void io_subchannel_shutdown(struct subchannel *);
139 static int io_subchannel_sch_event(struct subchannel *, int);
140 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
142 static void recovery_func(unsigned long data);
144 static struct css_device_id io_subchannel_ids[] = {
145 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
146 { /* end of list */ },
148 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
150 static int io_subchannel_prepare(struct subchannel *sch)
152 struct ccw_device *cdev;
154 * Don't allow suspend while a ccw device registration
155 * is still outstanding.
157 cdev = sch_get_cdev(sch);
158 if (cdev && !device_is_registered(&cdev->dev))
163 static int io_subchannel_settle(void)
167 ret = wait_event_interruptible(ccw_device_init_wq,
168 atomic_read(&ccw_device_init_count) == 0);
171 flush_workqueue(cio_work_q);
175 static struct css_driver io_subchannel_driver = {
177 .owner = THIS_MODULE,
178 .name = "io_subchannel",
180 .subchannel_type = io_subchannel_ids,
181 .irq = io_subchannel_irq,
182 .sch_event = io_subchannel_sch_event,
183 .chp_event = io_subchannel_chp_event,
184 .probe = io_subchannel_probe,
185 .remove = io_subchannel_remove,
186 .shutdown = io_subchannel_shutdown,
187 .prepare = io_subchannel_prepare,
188 .settle = io_subchannel_settle,
191 int __init io_subchannel_init(void)
195 setup_timer(&recovery_timer, recovery_func, 0);
196 ret = bus_register(&ccw_bus_type);
199 ret = css_driver_register(&io_subchannel_driver);
201 bus_unregister(&ccw_bus_type);
207 /************************ device handling **************************/
210 * A ccw_device has some interfaces in sysfs in addition to the
212 * The following entries are designed to export the information which
213 * resided in 2.4 in /proc/subchannels. Subchannel and device number
214 * are obvious, so they don't have an entry :)
215 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
218 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
220 struct subchannel *sch = to_subchannel(dev);
221 struct chsc_ssd_info *ssd = &sch->ssd_info;
226 for (chp = 0; chp < 8; chp++) {
228 if (ssd->path_mask & mask)
229 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
231 ret += sprintf(buf + ret, "00 ");
233 ret += sprintf (buf+ret, "\n");
234 return min((ssize_t)PAGE_SIZE, ret);
238 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
240 struct subchannel *sch = to_subchannel(dev);
241 struct pmcw *pmcw = &sch->schib.pmcw;
243 return sprintf (buf, "%02x %02x %02x\n",
244 pmcw->pim, pmcw->pam, pmcw->pom);
248 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
250 struct ccw_device *cdev = to_ccwdev(dev);
251 struct ccw_device_id *id = &(cdev->id);
253 if (id->dev_type != 0)
254 return sprintf(buf, "%04x/%02x\n",
255 id->dev_type, id->dev_model);
257 return sprintf(buf, "n/a\n");
261 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
266 return sprintf(buf, "%04x/%02x\n",
267 id->cu_type, id->cu_model);
271 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
277 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
279 return len > PAGE_SIZE ? PAGE_SIZE : len;
283 online_show (struct device *dev, struct device_attribute *attr, char *buf)
285 struct ccw_device *cdev = to_ccwdev(dev);
287 return sprintf(buf, cdev->online ? "1\n" : "0\n");
290 int ccw_device_is_orphan(struct ccw_device *cdev)
292 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
295 static void ccw_device_unregister(struct ccw_device *cdev)
297 if (device_is_registered(&cdev->dev)) {
298 /* Undo device_add(). */
299 device_del(&cdev->dev);
301 if (cdev->private->flags.initialized) {
302 cdev->private->flags.initialized = 0;
303 /* Release reference from device_initialize(). */
304 put_device(&cdev->dev);
308 static void io_subchannel_quiesce(struct subchannel *);
311 * ccw_device_set_offline() - disable a ccw device for I/O
312 * @cdev: target ccw device
314 * This function calls the driver's set_offline() function for @cdev, if
315 * given, and then disables @cdev.
317 * %0 on success and a negative error value on failure.
319 * enabled, ccw device lock not held
321 int ccw_device_set_offline(struct ccw_device *cdev)
323 struct subchannel *sch;
328 if (!cdev->online || !cdev->drv)
331 if (cdev->drv->set_offline) {
332 ret = cdev->drv->set_offline(cdev);
336 spin_lock_irq(cdev->ccwlock);
337 sch = to_subchannel(cdev->dev.parent);
339 /* Wait until a final state or DISCONNECTED is reached */
340 while (!dev_fsm_final_state(cdev) &&
341 cdev->private->state != DEV_STATE_DISCONNECTED) {
342 spin_unlock_irq(cdev->ccwlock);
343 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
344 cdev->private->state == DEV_STATE_DISCONNECTED));
345 spin_lock_irq(cdev->ccwlock);
348 ret = ccw_device_offline(cdev);
351 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
352 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
353 cdev->private->dev_id.devno);
356 state = cdev->private->state;
357 spin_unlock_irq(cdev->ccwlock);
358 io_subchannel_quiesce(sch);
359 spin_lock_irq(cdev->ccwlock);
360 cdev->private->state = state;
361 } while (ret == -EBUSY);
362 spin_unlock_irq(cdev->ccwlock);
363 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
364 cdev->private->state == DEV_STATE_DISCONNECTED));
365 /* Inform the user if set offline failed. */
366 if (cdev->private->state == DEV_STATE_BOXED) {
367 pr_warning("%s: The device entered boxed state while "
368 "being set offline\n", dev_name(&cdev->dev));
369 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
370 pr_warning("%s: The device stopped operating while "
371 "being set offline\n", dev_name(&cdev->dev));
373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev->dev);
378 cdev->private->state = DEV_STATE_OFFLINE;
379 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
380 spin_unlock_irq(cdev->ccwlock);
381 /* Give up reference from ccw_device_set_online(). */
382 put_device(&cdev->dev);
387 * ccw_device_set_online() - enable a ccw device for I/O
388 * @cdev: target ccw device
390 * This function first enables @cdev and then calls the driver's set_online()
391 * function for @cdev, if given. If set_online() returns an error, @cdev is
394 * %0 on success and a negative error value on failure.
396 * enabled, ccw device lock not held
398 int ccw_device_set_online(struct ccw_device *cdev)
405 if (cdev->online || !cdev->drv)
407 /* Hold on to an extra reference while device is online. */
408 if (!get_device(&cdev->dev))
411 spin_lock_irq(cdev->ccwlock);
412 ret = ccw_device_online(cdev);
413 spin_unlock_irq(cdev->ccwlock);
415 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
417 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
418 "device 0.%x.%04x\n",
419 ret, cdev->private->dev_id.ssid,
420 cdev->private->dev_id.devno);
421 /* Give up online reference since onlining failed. */
422 put_device(&cdev->dev);
425 spin_lock_irq(cdev->ccwlock);
426 /* Check if online processing was successful */
427 if ((cdev->private->state != DEV_STATE_ONLINE) &&
428 (cdev->private->state != DEV_STATE_W4SENSE)) {
429 spin_unlock_irq(cdev->ccwlock);
430 /* Inform the user that set online failed. */
431 if (cdev->private->state == DEV_STATE_BOXED) {
432 pr_warning("%s: Setting the device online failed "
433 "because it is boxed\n",
434 dev_name(&cdev->dev));
435 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
436 pr_warning("%s: Setting the device online failed "
437 "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_warning("Logging for subchannel 0.%x.%04x failed with "
624 sch->schid.ssid, sch->schid.sch_no, rc);
627 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
628 sch->schid.ssid, sch->schid.sch_no);
632 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
635 struct subchannel *sch = to_subchannel(dev);
637 return sprintf(buf, "%02x\n", sch->vpm);
640 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
641 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
642 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
643 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
644 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
645 static DEVICE_ATTR(online, 0644, online_show, online_store);
646 static DEVICE_ATTR(availability, 0444, available_show, NULL);
647 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
648 static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
650 static struct attribute *io_subchannel_attrs[] = {
651 &dev_attr_chpids.attr,
652 &dev_attr_pimpampom.attr,
653 &dev_attr_logging.attr,
658 static struct attribute_group io_subchannel_attr_group = {
659 .attrs = io_subchannel_attrs,
662 static struct attribute * ccwdev_attrs[] = {
663 &dev_attr_devtype.attr,
664 &dev_attr_cutype.attr,
665 &dev_attr_modalias.attr,
666 &dev_attr_online.attr,
667 &dev_attr_cmb_enable.attr,
668 &dev_attr_availability.attr,
672 static struct attribute_group ccwdev_attr_group = {
673 .attrs = ccwdev_attrs,
676 static const struct attribute_group *ccwdev_attr_groups[] = {
681 /* this is a simple abstraction for device_register that sets the
682 * correct bus type and adds the bus specific files */
683 static int ccw_device_register(struct ccw_device *cdev)
685 struct device *dev = &cdev->dev;
688 dev->bus = &ccw_bus_type;
689 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
690 cdev->private->dev_id.devno);
693 return device_add(dev);
696 static int match_dev_id(struct device *dev, void *data)
698 struct ccw_device *cdev = to_ccwdev(dev);
699 struct ccw_dev_id *dev_id = data;
701 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
705 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
706 * @dev_id: id of the device to be searched
708 * This function searches all devices attached to the ccw bus for a device
711 * If a device is found its reference count is increased and returned;
712 * else %NULL is returned.
714 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
718 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
720 return dev ? to_ccwdev(dev) : NULL;
722 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
724 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
728 if (device_is_registered(&cdev->dev)) {
729 device_release_driver(&cdev->dev);
730 ret = device_attach(&cdev->dev);
731 WARN_ON(ret == -ENODEV);
736 ccw_device_release(struct device *dev)
738 struct ccw_device *cdev;
740 cdev = to_ccwdev(dev);
741 /* Release reference of parent subchannel. */
742 put_device(cdev->dev.parent);
743 kfree(cdev->private);
747 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
749 struct ccw_device *cdev;
751 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
753 cdev->private = kzalloc(sizeof(struct ccw_device_private),
754 GFP_KERNEL | GFP_DMA);
759 return ERR_PTR(-ENOMEM);
762 static void ccw_device_todo(struct work_struct *work);
764 static int io_subchannel_initialize_dev(struct subchannel *sch,
765 struct ccw_device *cdev)
767 cdev->private->cdev = cdev;
768 cdev->private->int_class = IRQIO_CIO;
769 atomic_set(&cdev->private->onoff, 0);
770 cdev->dev.parent = &sch->dev;
771 cdev->dev.release = ccw_device_release;
772 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
773 cdev->dev.groups = ccwdev_attr_groups;
774 /* Do first half of device_register. */
775 device_initialize(&cdev->dev);
776 if (!get_device(&sch->dev)) {
777 /* Release reference from device_initialize(). */
778 put_device(&cdev->dev);
781 cdev->private->flags.initialized = 1;
785 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
787 struct ccw_device *cdev;
790 cdev = io_subchannel_allocate_dev(sch);
792 ret = io_subchannel_initialize_dev(sch, cdev);
799 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
801 static void sch_create_and_recog_new_device(struct subchannel *sch)
803 struct ccw_device *cdev;
805 /* Need to allocate a new ccw device. */
806 cdev = io_subchannel_create_ccwdev(sch);
808 /* OK, we did everything we could... */
809 css_sch_device_unregister(sch);
812 /* Start recognition for the new ccw device. */
813 io_subchannel_recog(cdev, sch);
817 * Register recognized device.
819 static void io_subchannel_register(struct ccw_device *cdev)
821 struct subchannel *sch;
822 int ret, adjust_init_count = 1;
825 sch = to_subchannel(cdev->dev.parent);
827 * Check if subchannel is still registered. It may have become
828 * unregistered if a machine check hit us after finishing
829 * device recognition but before the register work could be
832 if (!device_is_registered(&sch->dev))
834 css_update_ssd_info(sch);
836 * io_subchannel_register() will also be called after device
837 * recognition has been done for a boxed device (which will already
838 * be registered). We need to reprobe since we may now have sense id
841 if (device_is_registered(&cdev->dev)) {
843 ret = device_reprobe(&cdev->dev);
845 /* We can't do much here. */
846 CIO_MSG_EVENT(0, "device_reprobe() returned"
847 " %d for 0.%x.%04x\n", ret,
848 cdev->private->dev_id.ssid,
849 cdev->private->dev_id.devno);
851 adjust_init_count = 0;
855 * Now we know this subchannel will stay, we can throw
856 * our delayed uevent.
858 dev_set_uevent_suppress(&sch->dev, 0);
859 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
860 /* make it known to the system */
861 ret = ccw_device_register(cdev);
863 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
864 cdev->private->dev_id.ssid,
865 cdev->private->dev_id.devno, ret);
866 spin_lock_irqsave(sch->lock, flags);
867 sch_set_cdev(sch, NULL);
868 spin_unlock_irqrestore(sch->lock, flags);
869 /* Release initial device reference. */
870 put_device(&cdev->dev);
874 cdev->private->flags.recog_done = 1;
875 wake_up(&cdev->private->wait_q);
877 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
878 wake_up(&ccw_device_init_wq);
881 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
883 struct subchannel *sch;
885 /* Get subchannel reference for local processing. */
886 if (!get_device(cdev->dev.parent))
888 sch = to_subchannel(cdev->dev.parent);
889 css_sch_device_unregister(sch);
890 /* Release subchannel reference for local processing. */
891 put_device(&sch->dev);
895 * subchannel recognition done. Called from the state machine.
898 io_subchannel_recog_done(struct ccw_device *cdev)
900 if (css_init_done == 0) {
901 cdev->private->flags.recog_done = 1;
904 switch (cdev->private->state) {
905 case DEV_STATE_BOXED:
906 /* Device did not respond in time. */
907 case DEV_STATE_NOT_OPER:
908 cdev->private->flags.recog_done = 1;
909 /* Remove device found not operational. */
910 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
911 if (atomic_dec_and_test(&ccw_device_init_count))
912 wake_up(&ccw_device_init_wq);
914 case DEV_STATE_OFFLINE:
916 * We can't register the device in interrupt context so
917 * we schedule a work item.
919 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
924 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
926 struct ccw_device_private *priv;
928 cdev->ccwlock = sch->lock;
930 /* Init private data. */
931 priv = cdev->private;
932 priv->dev_id.devno = sch->schib.pmcw.dev;
933 priv->dev_id.ssid = sch->schid.ssid;
934 priv->schid = sch->schid;
935 priv->state = DEV_STATE_NOT_OPER;
936 INIT_LIST_HEAD(&priv->cmb_list);
937 init_waitqueue_head(&priv->wait_q);
938 init_timer(&priv->timer);
940 /* Increase counter of devices currently in recognition. */
941 atomic_inc(&ccw_device_init_count);
943 /* Start async. device sensing. */
944 spin_lock_irq(sch->lock);
945 sch_set_cdev(sch, cdev);
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->private->schid = sch->schid;
1005 cdev->ccwlock = sch->lock;
1006 if (!sch_is_pseudo_sch(sch))
1007 sch_set_cdev(sch, cdev);
1008 spin_unlock_irq(sch->lock);
1009 if (!sch_is_pseudo_sch(sch))
1010 css_update_ssd_info(sch);
1014 static int ccw_device_move_to_orph(struct ccw_device *cdev)
1016 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1017 struct channel_subsystem *css = to_css(sch->dev.parent);
1019 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1022 static void io_subchannel_irq(struct subchannel *sch)
1024 struct ccw_device *cdev;
1026 cdev = sch_get_cdev(sch);
1028 CIO_TRACE_EVENT(6, "IRQ");
1029 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1031 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1033 inc_irq_stat(IRQIO_CIO);
1036 void io_subchannel_init_config(struct subchannel *sch)
1038 memset(&sch->config, 0, sizeof(sch->config));
1039 sch->config.csense = 1;
1042 static void io_subchannel_init_fields(struct subchannel *sch)
1044 if (cio_is_console(sch->schid))
1047 sch->opm = chp_get_sch_opm(sch);
1048 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1049 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1051 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1052 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1053 sch->schib.pmcw.dev, sch->schid.ssid,
1054 sch->schid.sch_no, sch->schib.pmcw.pim,
1055 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1057 io_subchannel_init_config(sch);
1061 * Note: We always return 0 so that we bind to the device even on error.
1062 * This is needed so that our remove function is called on unregister.
1064 static int io_subchannel_probe(struct subchannel *sch)
1066 struct io_subchannel_private *io_priv;
1067 struct ccw_device *cdev;
1070 if (cio_is_console(sch->schid)) {
1071 rc = sysfs_create_group(&sch->dev.kobj,
1072 &io_subchannel_attr_group);
1074 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1075 "attributes for subchannel "
1076 "0.%x.%04x (rc=%d)\n",
1077 sch->schid.ssid, sch->schid.sch_no, rc);
1079 * The console subchannel already has an associated ccw_device.
1080 * Throw the delayed uevent for the subchannel, register
1081 * the ccw_device and exit.
1083 dev_set_uevent_suppress(&sch->dev, 0);
1084 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1085 cdev = sch_get_cdev(sch);
1086 rc = ccw_device_register(cdev);
1088 /* Release online reference. */
1089 put_device(&cdev->dev);
1092 if (atomic_dec_and_test(&ccw_device_init_count))
1093 wake_up(&ccw_device_init_wq);
1096 io_subchannel_init_fields(sch);
1097 rc = cio_commit_config(sch);
1100 rc = sysfs_create_group(&sch->dev.kobj,
1101 &io_subchannel_attr_group);
1104 /* Allocate I/O subchannel private data. */
1105 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1109 set_io_private(sch, io_priv);
1110 css_schedule_eval(sch->schid);
1114 spin_lock_irq(sch->lock);
1115 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1116 spin_unlock_irq(sch->lock);
1121 io_subchannel_remove (struct subchannel *sch)
1123 struct io_subchannel_private *io_priv = to_io_private(sch);
1124 struct ccw_device *cdev;
1126 cdev = sch_get_cdev(sch);
1129 io_subchannel_quiesce(sch);
1130 /* Set ccw device to not operational and drop reference. */
1131 spin_lock_irq(cdev->ccwlock);
1132 sch_set_cdev(sch, NULL);
1133 set_io_private(sch, NULL);
1134 cdev->private->state = DEV_STATE_NOT_OPER;
1135 spin_unlock_irq(cdev->ccwlock);
1136 ccw_device_unregister(cdev);
1139 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1143 static void io_subchannel_verify(struct subchannel *sch)
1145 struct ccw_device *cdev;
1147 cdev = sch_get_cdev(sch);
1149 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1152 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1154 struct ccw_device *cdev;
1156 cdev = sch_get_cdev(sch);
1159 if (cio_update_schib(sch))
1161 /* Check for I/O on path. */
1162 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1164 if (cdev->private->state == DEV_STATE_ONLINE) {
1165 ccw_device_kill_io(cdev);
1171 /* Trigger path verification. */
1172 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1176 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1179 static int io_subchannel_chp_event(struct subchannel *sch,
1180 struct chp_link *link, int event)
1182 struct ccw_device *cdev = sch_get_cdev(sch);
1185 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1193 cdev->private->path_gone_mask |= mask;
1194 io_subchannel_terminate_path(sch, mask);
1200 cdev->private->path_new_mask |= mask;
1201 io_subchannel_verify(sch);
1204 if (cio_update_schib(sch))
1207 cdev->private->path_gone_mask |= mask;
1208 io_subchannel_terminate_path(sch, mask);
1211 if (cio_update_schib(sch))
1213 sch->lpm |= mask & sch->opm;
1215 cdev->private->path_new_mask |= mask;
1216 io_subchannel_verify(sch);
1222 static void io_subchannel_quiesce(struct subchannel *sch)
1224 struct ccw_device *cdev;
1227 spin_lock_irq(sch->lock);
1228 cdev = sch_get_cdev(sch);
1229 if (cio_is_console(sch->schid))
1231 if (!sch->schib.pmcw.ena)
1233 ret = cio_disable_subchannel(sch);
1237 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1238 while (ret == -EBUSY) {
1239 cdev->private->state = DEV_STATE_QUIESCE;
1240 cdev->private->iretry = 255;
1241 ret = ccw_device_cancel_halt_clear(cdev);
1242 if (ret == -EBUSY) {
1243 ccw_device_set_timeout(cdev, HZ/10);
1244 spin_unlock_irq(sch->lock);
1245 wait_event(cdev->private->wait_q,
1246 cdev->private->state != DEV_STATE_QUIESCE);
1247 spin_lock_irq(sch->lock);
1249 ret = cio_disable_subchannel(sch);
1252 spin_unlock_irq(sch->lock);
1255 static void io_subchannel_shutdown(struct subchannel *sch)
1257 io_subchannel_quiesce(sch);
1260 static int device_is_disconnected(struct ccw_device *cdev)
1264 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1265 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1268 static int recovery_check(struct device *dev, void *data)
1270 struct ccw_device *cdev = to_ccwdev(dev);
1273 spin_lock_irq(cdev->ccwlock);
1274 switch (cdev->private->state) {
1275 case DEV_STATE_DISCONNECTED:
1276 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1277 cdev->private->dev_id.ssid,
1278 cdev->private->dev_id.devno);
1279 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1282 case DEV_STATE_DISCONNECTED_SENSE_ID:
1286 spin_unlock_irq(cdev->ccwlock);
1291 static void recovery_work_func(struct work_struct *unused)
1295 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1297 spin_lock_irq(&recovery_lock);
1298 if (!timer_pending(&recovery_timer)) {
1299 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1301 mod_timer(&recovery_timer, jiffies +
1302 recovery_delay[recovery_phase] * HZ);
1304 spin_unlock_irq(&recovery_lock);
1306 CIO_MSG_EVENT(4, "recovery: end\n");
1309 static DECLARE_WORK(recovery_work, recovery_work_func);
1311 static void recovery_func(unsigned long data)
1314 * We can't do our recovery in softirq context and it's not
1315 * performance critical, so we schedule it.
1317 schedule_work(&recovery_work);
1320 static void ccw_device_schedule_recovery(void)
1322 unsigned long flags;
1324 CIO_MSG_EVENT(4, "recovery: schedule\n");
1325 spin_lock_irqsave(&recovery_lock, flags);
1326 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1328 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1330 spin_unlock_irqrestore(&recovery_lock, flags);
1333 static int purge_fn(struct device *dev, void *data)
1335 struct ccw_device *cdev = to_ccwdev(dev);
1336 struct ccw_dev_id *id = &cdev->private->dev_id;
1338 spin_lock_irq(cdev->ccwlock);
1339 if (is_blacklisted(id->ssid, id->devno) &&
1340 (cdev->private->state == DEV_STATE_OFFLINE) &&
1341 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1342 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1344 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1345 atomic_set(&cdev->private->onoff, 0);
1347 spin_unlock_irq(cdev->ccwlock);
1348 /* Abort loop in case of pending signal. */
1349 if (signal_pending(current))
1356 * ccw_purge_blacklisted - purge unused, blacklisted devices
1358 * Unregister all ccw devices that are offline and on the blacklist.
1360 int ccw_purge_blacklisted(void)
1362 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1363 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1367 void ccw_device_set_disconnected(struct ccw_device *cdev)
1371 ccw_device_set_timeout(cdev, 0);
1372 cdev->private->flags.fake_irb = 0;
1373 cdev->private->state = DEV_STATE_DISCONNECTED;
1375 ccw_device_schedule_recovery();
1378 void ccw_device_set_notoper(struct ccw_device *cdev)
1380 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1382 CIO_TRACE_EVENT(2, "notoper");
1383 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1384 ccw_device_set_timeout(cdev, 0);
1385 cio_disable_subchannel(sch);
1386 cdev->private->state = DEV_STATE_NOT_OPER;
1389 enum io_sch_action {
1393 IO_SCH_UNREG_ATTACH,
1401 static enum io_sch_action sch_get_action(struct subchannel *sch)
1403 struct ccw_device *cdev;
1405 cdev = sch_get_cdev(sch);
1406 if (cio_update_schib(sch)) {
1407 /* Not operational. */
1409 return IO_SCH_UNREG;
1410 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1411 return IO_SCH_UNREG;
1412 return IO_SCH_ORPH_UNREG;
1416 return IO_SCH_ATTACH;
1417 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1418 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1419 return IO_SCH_UNREG_ATTACH;
1420 return IO_SCH_ORPH_ATTACH;
1422 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1423 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1424 return IO_SCH_UNREG;
1427 if (device_is_disconnected(cdev))
1428 return IO_SCH_REPROBE;
1429 if (cdev->online && !cdev->private->flags.resuming)
1430 return IO_SCH_VERIFY;
1431 if (cdev->private->state == DEV_STATE_NOT_OPER)
1432 return IO_SCH_UNREG_ATTACH;
1437 * io_subchannel_sch_event - process subchannel event
1439 * @process: non-zero if function is called in process context
1441 * An unspecified event occurred for this subchannel. Adjust data according
1442 * to the current operational state of the subchannel and device. Return
1443 * zero when the event has been handled sufficiently or -EAGAIN when this
1444 * function should be called again in process context.
1446 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1448 unsigned long flags;
1449 struct ccw_device *cdev;
1450 struct ccw_dev_id dev_id;
1451 enum io_sch_action action;
1454 spin_lock_irqsave(sch->lock, flags);
1455 if (!device_is_registered(&sch->dev))
1457 if (work_pending(&sch->todo_work))
1459 cdev = sch_get_cdev(sch);
1460 if (cdev && work_pending(&cdev->private->todo_work))
1462 action = sch_get_action(sch);
1463 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1464 sch->schid.ssid, sch->schid.sch_no, process,
1466 /* Perform immediate actions while holding the lock. */
1468 case IO_SCH_REPROBE:
1469 /* Trigger device recognition. */
1470 ccw_device_trigger_reprobe(cdev);
1474 /* Trigger path verification. */
1475 io_subchannel_verify(sch);
1479 ccw_device_set_disconnected(cdev);
1482 case IO_SCH_ORPH_UNREG:
1483 case IO_SCH_ORPH_ATTACH:
1484 ccw_device_set_disconnected(cdev);
1486 case IO_SCH_UNREG_ATTACH:
1490 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1492 * Note: delayed work triggered by this event
1493 * and repeated calls to sch_event are synchronized
1494 * by the above check for work_pending(cdev).
1496 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1498 ccw_device_set_notoper(cdev);
1506 spin_unlock_irqrestore(sch->lock, flags);
1507 /* All other actions require process context. */
1510 /* Handle attached ccw device. */
1512 case IO_SCH_ORPH_UNREG:
1513 case IO_SCH_ORPH_ATTACH:
1514 /* Move ccw device to orphanage. */
1515 rc = ccw_device_move_to_orph(cdev);
1519 case IO_SCH_UNREG_ATTACH:
1520 spin_lock_irqsave(sch->lock, flags);
1521 if (cdev->private->flags.resuming) {
1522 /* Device will be handled later. */
1526 sch_set_cdev(sch, NULL);
1527 spin_unlock_irqrestore(sch->lock, flags);
1528 /* Unregister ccw device. */
1529 ccw_device_unregister(cdev);
1534 /* Handle subchannel. */
1536 case IO_SCH_ORPH_UNREG:
1538 if (!cdev || !cdev->private->flags.resuming)
1539 css_sch_device_unregister(sch);
1541 case IO_SCH_ORPH_ATTACH:
1542 case IO_SCH_UNREG_ATTACH:
1544 dev_id.ssid = sch->schid.ssid;
1545 dev_id.devno = sch->schib.pmcw.dev;
1546 cdev = get_ccwdev_by_dev_id(&dev_id);
1548 sch_create_and_recog_new_device(sch);
1551 rc = ccw_device_move_to_sch(cdev, sch);
1553 /* Release reference from get_ccwdev_by_dev_id() */
1554 put_device(&cdev->dev);
1557 spin_lock_irqsave(sch->lock, flags);
1558 ccw_device_trigger_reprobe(cdev);
1559 spin_unlock_irqrestore(sch->lock, flags);
1560 /* Release reference from get_ccwdev_by_dev_id() */
1561 put_device(&cdev->dev);
1569 spin_unlock_irqrestore(sch->lock, flags);
1574 #ifdef CONFIG_CCW_CONSOLE
1575 static int ccw_device_console_enable(struct ccw_device *cdev,
1576 struct subchannel *sch)
1580 io_subchannel_init_fields(sch);
1581 rc = cio_commit_config(sch);
1584 sch->driver = &io_subchannel_driver;
1585 sch_set_cdev(sch, cdev);
1586 io_subchannel_recog(cdev, sch);
1587 /* Now wait for the async. recognition to come to an end. */
1588 spin_lock_irq(cdev->ccwlock);
1589 while (!dev_fsm_final_state(cdev))
1590 ccw_device_wait_idle(cdev);
1592 /* Hold on to an extra reference while device is online. */
1593 get_device(&cdev->dev);
1594 rc = ccw_device_online(cdev);
1598 while (!dev_fsm_final_state(cdev))
1599 ccw_device_wait_idle(cdev);
1601 if (cdev->private->state == DEV_STATE_ONLINE)
1606 spin_unlock_irq(cdev->ccwlock);
1607 if (rc) /* Give up online reference since onlining failed. */
1608 put_device(&cdev->dev);
1612 struct ccw_device *ccw_device_probe_console(struct ccw_driver *drv)
1614 struct io_subchannel_private *io_priv;
1615 struct ccw_device *cdev;
1616 struct subchannel *sch;
1619 sch = cio_probe_console();
1621 return ERR_CAST(sch);
1623 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1625 put_device(&sch->dev);
1626 return ERR_PTR(-ENOMEM);
1628 cdev = io_subchannel_create_ccwdev(sch);
1630 put_device(&sch->dev);
1635 set_io_private(sch, io_priv);
1636 ret = ccw_device_console_enable(cdev, sch);
1638 set_io_private(sch, NULL);
1639 put_device(&sch->dev);
1640 put_device(&cdev->dev);
1642 return ERR_PTR(ret);
1648 * ccw_device_wait_idle() - busy wait for device to become idle
1651 * Poll until activity control is zero, that is, no function or data
1652 * transfer is pending/active.
1653 * Called with device lock being held.
1655 void ccw_device_wait_idle(struct ccw_device *cdev)
1657 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1661 if (sch->schib.scsw.cmd.actl == 0)
1667 static int ccw_device_pm_restore(struct device *dev);
1669 int ccw_device_force_console(struct ccw_device *cdev)
1671 return ccw_device_pm_restore(&cdev->dev);
1673 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1677 * get ccw_device matching the busid, but only if owned by cdrv
1680 __ccwdev_check_busid(struct device *dev, void *id)
1686 return (strcmp(bus_id, dev_name(dev)) == 0);
1691 * get_ccwdev_by_busid() - obtain device from a bus id
1692 * @cdrv: driver the device is owned by
1693 * @bus_id: bus id of the device to be searched
1695 * This function searches all devices owned by @cdrv for a device with a bus
1696 * id matching @bus_id.
1698 * If a match is found, its reference count of the found device is increased
1699 * and it is returned; else %NULL is returned.
1701 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1706 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1707 __ccwdev_check_busid);
1709 return dev ? to_ccwdev(dev) : NULL;
1712 /************************** device driver handling ************************/
1714 /* This is the implementation of the ccw_driver class. The probe, remove
1715 * and release methods are initially very similar to the device_driver
1716 * implementations, with the difference that they have ccw_device
1719 * A ccw driver also contains the information that is needed for
1723 ccw_device_probe (struct device *dev)
1725 struct ccw_device *cdev = to_ccwdev(dev);
1726 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1729 cdev->drv = cdrv; /* to let the driver call _set_online */
1730 /* Note: we interpret class 0 in this context as an uninitialized
1731 * field since it translates to a non-I/O interrupt class. */
1732 if (cdrv->int_class != 0)
1733 cdev->private->int_class = cdrv->int_class;
1735 cdev->private->int_class = IRQIO_CIO;
1737 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1741 cdev->private->int_class = IRQIO_CIO;
1748 static int ccw_device_remove(struct device *dev)
1750 struct ccw_device *cdev = to_ccwdev(dev);
1751 struct ccw_driver *cdrv = cdev->drv;
1757 spin_lock_irq(cdev->ccwlock);
1760 ret = ccw_device_offline(cdev);
1761 spin_unlock_irq(cdev->ccwlock);
1763 wait_event(cdev->private->wait_q,
1764 dev_fsm_final_state(cdev));
1766 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1767 "device 0.%x.%04x\n",
1768 ret, cdev->private->dev_id.ssid,
1769 cdev->private->dev_id.devno);
1770 /* Give up reference obtained in ccw_device_set_online(). */
1771 put_device(&cdev->dev);
1772 spin_lock_irq(cdev->ccwlock);
1774 ccw_device_set_timeout(cdev, 0);
1776 cdev->private->int_class = IRQIO_CIO;
1777 spin_unlock_irq(cdev->ccwlock);
1781 static void ccw_device_shutdown(struct device *dev)
1783 struct ccw_device *cdev;
1785 cdev = to_ccwdev(dev);
1786 if (cdev->drv && cdev->drv->shutdown)
1787 cdev->drv->shutdown(cdev);
1791 static int ccw_device_pm_prepare(struct device *dev)
1793 struct ccw_device *cdev = to_ccwdev(dev);
1795 if (work_pending(&cdev->private->todo_work))
1797 /* Fail while device is being set online/offline. */
1798 if (atomic_read(&cdev->private->onoff))
1801 if (cdev->online && cdev->drv && cdev->drv->prepare)
1802 return cdev->drv->prepare(cdev);
1807 static void ccw_device_pm_complete(struct device *dev)
1809 struct ccw_device *cdev = to_ccwdev(dev);
1811 if (cdev->online && cdev->drv && cdev->drv->complete)
1812 cdev->drv->complete(cdev);
1815 static int ccw_device_pm_freeze(struct device *dev)
1817 struct ccw_device *cdev = to_ccwdev(dev);
1818 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1819 int ret, cm_enabled;
1821 /* Fail suspend while device is in transistional state. */
1822 if (!dev_fsm_final_state(cdev))
1826 if (cdev->drv && cdev->drv->freeze) {
1827 ret = cdev->drv->freeze(cdev);
1832 spin_lock_irq(sch->lock);
1833 cm_enabled = cdev->private->cmb != NULL;
1834 spin_unlock_irq(sch->lock);
1836 /* Don't have the css write on memory. */
1837 ret = ccw_set_cmf(cdev, 0);
1841 /* From here on, disallow device driver I/O. */
1842 spin_lock_irq(sch->lock);
1843 ret = cio_disable_subchannel(sch);
1844 spin_unlock_irq(sch->lock);
1849 static int ccw_device_pm_thaw(struct device *dev)
1851 struct ccw_device *cdev = to_ccwdev(dev);
1852 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1853 int ret, cm_enabled;
1858 spin_lock_irq(sch->lock);
1859 /* Allow device driver I/O again. */
1860 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1861 cm_enabled = cdev->private->cmb != NULL;
1862 spin_unlock_irq(sch->lock);
1867 ret = ccw_set_cmf(cdev, 1);
1872 if (cdev->drv && cdev->drv->thaw)
1873 ret = cdev->drv->thaw(cdev);
1878 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1880 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1882 spin_lock_irq(sch->lock);
1883 if (cio_is_console(sch->schid)) {
1884 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1888 * While we were sleeping, devices may have gone or become
1889 * available again. Kick re-detection.
1891 cdev->private->flags.resuming = 1;
1892 cdev->private->path_new_mask = LPM_ANYPATH;
1893 css_sched_sch_todo(sch, SCH_TODO_EVAL);
1894 spin_unlock_irq(sch->lock);
1895 css_wait_for_slow_path();
1897 /* cdev may have been moved to a different subchannel. */
1898 sch = to_subchannel(cdev->dev.parent);
1899 spin_lock_irq(sch->lock);
1900 if (cdev->private->state != DEV_STATE_ONLINE &&
1901 cdev->private->state != DEV_STATE_OFFLINE)
1904 ccw_device_recognition(cdev);
1905 spin_unlock_irq(sch->lock);
1906 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1907 cdev->private->state == DEV_STATE_DISCONNECTED);
1908 spin_lock_irq(sch->lock);
1911 cdev->private->flags.resuming = 0;
1912 spin_unlock_irq(sch->lock);
1915 static int resume_handle_boxed(struct ccw_device *cdev)
1917 cdev->private->state = DEV_STATE_BOXED;
1918 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1920 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1924 static int resume_handle_disc(struct ccw_device *cdev)
1926 cdev->private->state = DEV_STATE_DISCONNECTED;
1927 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1929 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1933 static int ccw_device_pm_restore(struct device *dev)
1935 struct ccw_device *cdev = to_ccwdev(dev);
1936 struct subchannel *sch;
1939 __ccw_device_pm_restore(cdev);
1940 sch = to_subchannel(cdev->dev.parent);
1941 spin_lock_irq(sch->lock);
1942 if (cio_is_console(sch->schid))
1945 /* check recognition results */
1946 switch (cdev->private->state) {
1947 case DEV_STATE_OFFLINE:
1948 case DEV_STATE_ONLINE:
1949 cdev->private->flags.donotify = 0;
1951 case DEV_STATE_BOXED:
1952 ret = resume_handle_boxed(cdev);
1957 ret = resume_handle_disc(cdev);
1962 /* check if the device type has changed */
1963 if (!ccw_device_test_sense_data(cdev)) {
1964 ccw_device_update_sense_data(cdev);
1965 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1972 if (ccw_device_online(cdev)) {
1973 ret = resume_handle_disc(cdev);
1978 spin_unlock_irq(sch->lock);
1979 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1980 spin_lock_irq(sch->lock);
1982 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1983 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1988 /* reenable cmf, if needed */
1989 if (cdev->private->cmb) {
1990 spin_unlock_irq(sch->lock);
1991 ret = ccw_set_cmf(cdev, 1);
1992 spin_lock_irq(sch->lock);
1994 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1995 "(rc=%d)\n", cdev->private->dev_id.ssid,
1996 cdev->private->dev_id.devno, ret);
2002 spin_unlock_irq(sch->lock);
2003 if (cdev->online && cdev->drv && cdev->drv->restore)
2004 ret = cdev->drv->restore(cdev);
2008 spin_unlock_irq(sch->lock);
2012 static const struct dev_pm_ops ccw_pm_ops = {
2013 .prepare = ccw_device_pm_prepare,
2014 .complete = ccw_device_pm_complete,
2015 .freeze = ccw_device_pm_freeze,
2016 .thaw = ccw_device_pm_thaw,
2017 .restore = ccw_device_pm_restore,
2020 static struct bus_type ccw_bus_type = {
2022 .match = ccw_bus_match,
2023 .uevent = ccw_uevent,
2024 .probe = ccw_device_probe,
2025 .remove = ccw_device_remove,
2026 .shutdown = ccw_device_shutdown,
2031 * ccw_driver_register() - register a ccw driver
2032 * @cdriver: driver to be registered
2034 * This function is mainly a wrapper around driver_register().
2036 * %0 on success and a negative error value on failure.
2038 int ccw_driver_register(struct ccw_driver *cdriver)
2040 struct device_driver *drv = &cdriver->driver;
2042 drv->bus = &ccw_bus_type;
2044 return driver_register(drv);
2048 * ccw_driver_unregister() - deregister a ccw driver
2049 * @cdriver: driver to be deregistered
2051 * This function is mainly a wrapper around driver_unregister().
2053 void ccw_driver_unregister(struct ccw_driver *cdriver)
2055 driver_unregister(&cdriver->driver);
2058 static void ccw_device_todo(struct work_struct *work)
2060 struct ccw_device_private *priv;
2061 struct ccw_device *cdev;
2062 struct subchannel *sch;
2063 enum cdev_todo todo;
2065 priv = container_of(work, struct ccw_device_private, todo_work);
2067 sch = to_subchannel(cdev->dev.parent);
2068 /* Find out todo. */
2069 spin_lock_irq(cdev->ccwlock);
2071 priv->todo = CDEV_TODO_NOTHING;
2072 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2073 priv->dev_id.ssid, priv->dev_id.devno, todo);
2074 spin_unlock_irq(cdev->ccwlock);
2077 case CDEV_TODO_ENABLE_CMF:
2080 case CDEV_TODO_REBIND:
2081 ccw_device_do_unbind_bind(cdev);
2083 case CDEV_TODO_REGISTER:
2084 io_subchannel_register(cdev);
2086 case CDEV_TODO_UNREG_EVAL:
2087 if (!sch_is_pseudo_sch(sch))
2088 css_schedule_eval(sch->schid);
2090 case CDEV_TODO_UNREG:
2091 if (sch_is_pseudo_sch(sch))
2092 ccw_device_unregister(cdev);
2094 ccw_device_call_sch_unregister(cdev);
2099 /* Release workqueue ref. */
2100 put_device(&cdev->dev);
2104 * ccw_device_sched_todo - schedule ccw device operation
2108 * Schedule the operation identified by @todo to be performed on the slow path
2109 * workqueue. Do nothing if another operation with higher priority is already
2110 * scheduled. Needs to be called with ccwdev lock held.
2112 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2114 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2115 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2117 if (cdev->private->todo >= todo)
2119 cdev->private->todo = todo;
2120 /* Get workqueue ref. */
2121 if (!get_device(&cdev->dev))
2123 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2124 /* Already queued, release workqueue ref. */
2125 put_device(&cdev->dev);
2130 * ccw_device_siosl() - initiate logging
2133 * This function is used to invoke model-dependent logging within the channel
2136 int ccw_device_siosl(struct ccw_device *cdev)
2138 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2140 return chsc_siosl(sch->schid);
2142 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2144 MODULE_LICENSE("GPL");
2145 EXPORT_SYMBOL(ccw_device_set_online);
2146 EXPORT_SYMBOL(ccw_device_set_offline);
2147 EXPORT_SYMBOL(ccw_driver_register);
2148 EXPORT_SYMBOL(ccw_driver_unregister);
2149 EXPORT_SYMBOL(get_ccwdev_by_busid);