2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/workqueue.h>
19 #include <linux/timer.h>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
29 #include "cio_debug.h"
34 #include "blacklist.h"
36 static struct timer_list recovery_timer;
37 static DEFINE_SPINLOCK(recovery_lock);
38 static int recovery_phase;
39 static const unsigned long recovery_delay[] = { 3, 30, 300 };
41 /******************* bus type handling ***********************/
43 /* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
48 ccw_bus_match (struct device * dev, struct device_driver * drv)
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
57 found = ccw_device_id_match(ids, &cdev->id);
61 cdev->id.driver_info = found->driver_info;
66 /* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
69 static int snprint_alias(char *buf, size_t size,
70 struct ccw_device_id *id, const char *suffix)
74 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
84 len += snprintf(buf, size, "dtdm%s", suffix);
89 /* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
91 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
93 struct ccw_device *cdev = to_ccwdev(dev);
94 struct ccw_device_id *id = &(cdev->id);
96 char modalias_buf[30];
99 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
108 /* The next two can be zero, that's ok for us */
110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
125 struct bus_type ccw_bus_type;
127 static void io_subchannel_irq(struct subchannel *);
128 static int io_subchannel_probe(struct subchannel *);
129 static int io_subchannel_remove(struct subchannel *);
130 static void io_subchannel_shutdown(struct subchannel *);
131 static int io_subchannel_sch_event(struct subchannel *, int);
132 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
134 static void recovery_func(unsigned long data);
135 struct workqueue_struct *ccw_device_work;
136 wait_queue_head_t ccw_device_init_wq;
137 atomic_t ccw_device_init_count;
139 static struct css_device_id io_subchannel_ids[] = {
140 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
141 { /* end of list */ },
143 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
145 static int io_subchannel_prepare(struct subchannel *sch)
147 struct ccw_device *cdev;
149 * Don't allow suspend while a ccw device registration
150 * is still outstanding.
152 cdev = sch_get_cdev(sch);
153 if (cdev && !device_is_registered(&cdev->dev))
158 static void io_subchannel_settle(void)
160 wait_event(ccw_device_init_wq,
161 atomic_read(&ccw_device_init_count) == 0);
162 flush_workqueue(ccw_device_work);
165 static struct css_driver io_subchannel_driver = {
166 .owner = THIS_MODULE,
167 .subchannel_type = io_subchannel_ids,
168 .name = "io_subchannel",
169 .irq = io_subchannel_irq,
170 .sch_event = io_subchannel_sch_event,
171 .chp_event = io_subchannel_chp_event,
172 .probe = io_subchannel_probe,
173 .remove = io_subchannel_remove,
174 .shutdown = io_subchannel_shutdown,
175 .prepare = io_subchannel_prepare,
176 .settle = io_subchannel_settle,
179 int __init io_subchannel_init(void)
183 init_waitqueue_head(&ccw_device_init_wq);
184 atomic_set(&ccw_device_init_count, 0);
185 setup_timer(&recovery_timer, recovery_func, 0);
187 ccw_device_work = create_singlethread_workqueue("cio");
188 if (!ccw_device_work)
190 slow_path_wq = create_singlethread_workqueue("kslowcrw");
195 if ((ret = bus_register (&ccw_bus_type)))
198 ret = css_driver_register(&io_subchannel_driver);
205 destroy_workqueue(ccw_device_work);
207 destroy_workqueue(slow_path_wq);
212 /************************ device handling **************************/
215 * A ccw_device has some interfaces in sysfs in addition to the
217 * The following entries are designed to export the information which
218 * resided in 2.4 in /proc/subchannels. Subchannel and device number
219 * are obvious, so they don't have an entry :)
220 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
223 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
225 struct subchannel *sch = to_subchannel(dev);
226 struct chsc_ssd_info *ssd = &sch->ssd_info;
231 for (chp = 0; chp < 8; chp++) {
233 if (ssd->path_mask & mask)
234 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
236 ret += sprintf(buf + ret, "00 ");
238 ret += sprintf (buf+ret, "\n");
239 return min((ssize_t)PAGE_SIZE, ret);
243 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
245 struct subchannel *sch = to_subchannel(dev);
246 struct pmcw *pmcw = &sch->schib.pmcw;
248 return sprintf (buf, "%02x %02x %02x\n",
249 pmcw->pim, pmcw->pam, pmcw->pom);
253 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
255 struct ccw_device *cdev = to_ccwdev(dev);
256 struct ccw_device_id *id = &(cdev->id);
258 if (id->dev_type != 0)
259 return sprintf(buf, "%04x/%02x\n",
260 id->dev_type, id->dev_model);
262 return sprintf(buf, "n/a\n");
266 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
268 struct ccw_device *cdev = to_ccwdev(dev);
269 struct ccw_device_id *id = &(cdev->id);
271 return sprintf(buf, "%04x/%02x\n",
272 id->cu_type, id->cu_model);
276 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
278 struct ccw_device *cdev = to_ccwdev(dev);
279 struct ccw_device_id *id = &(cdev->id);
282 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
284 return len > PAGE_SIZE ? PAGE_SIZE : len;
288 online_show (struct device *dev, struct device_attribute *attr, char *buf)
290 struct ccw_device *cdev = to_ccwdev(dev);
292 return sprintf(buf, cdev->online ? "1\n" : "0\n");
295 int ccw_device_is_orphan(struct ccw_device *cdev)
297 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
300 static void ccw_device_unregister(struct ccw_device *cdev)
302 if (test_and_clear_bit(1, &cdev->private->registered)) {
303 device_del(&cdev->dev);
304 /* Release reference from device_initialize(). */
305 put_device(&cdev->dev);
309 static void ccw_device_remove_orphan_cb(struct work_struct *work)
311 struct ccw_device_private *priv;
312 struct ccw_device *cdev;
314 priv = container_of(work, struct ccw_device_private, kick_work);
316 ccw_device_unregister(cdev);
317 /* Release cdev reference for workqueue processing. */
318 put_device(&cdev->dev);
322 ccw_device_remove_disconnected(struct ccw_device *cdev)
327 * Forced offline in disconnected state means
328 * 'throw away device'.
330 if (ccw_device_is_orphan(cdev)) {
332 * Deregister ccw device.
333 * Unfortunately, we cannot do this directly from the
336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
342 PREPARE_WORK(&cdev->private->kick_work,
343 ccw_device_remove_orphan_cb);
344 queue_work(slow_path_wq, &cdev->private->kick_work);
346 /* Deregister subchannel, which will kill the ccw device. */
347 ccw_device_schedule_sch_unregister(cdev);
351 * ccw_device_set_offline() - disable a ccw device for I/O
352 * @cdev: target ccw device
354 * This function calls the driver's set_offline() function for @cdev, if
355 * given, and then disables @cdev.
357 * %0 on success and a negative error value on failure.
359 * enabled, ccw device lock not held
361 int ccw_device_set_offline(struct ccw_device *cdev)
367 if (!cdev->online || !cdev->drv)
370 if (cdev->drv->set_offline) {
371 ret = cdev->drv->set_offline(cdev);
376 spin_lock_irq(cdev->ccwlock);
377 /* Wait until a final state or DISCONNECTED is reached */
378 while (!dev_fsm_final_state(cdev) &&
379 cdev->private->state != DEV_STATE_DISCONNECTED) {
380 spin_unlock_irq(cdev->ccwlock);
381 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
382 cdev->private->state == DEV_STATE_DISCONNECTED));
383 spin_lock_irq(cdev->ccwlock);
385 ret = ccw_device_offline(cdev);
388 spin_unlock_irq(cdev->ccwlock);
389 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
390 cdev->private->state == DEV_STATE_DISCONNECTED));
391 /* Give up reference from ccw_device_set_online(). */
392 put_device(&cdev->dev);
396 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
397 ret, cdev->private->dev_id.ssid,
398 cdev->private->dev_id.devno);
399 cdev->private->state = DEV_STATE_OFFLINE;
400 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
401 spin_unlock_irq(cdev->ccwlock);
402 /* Give up reference from ccw_device_set_online(). */
403 put_device(&cdev->dev);
408 * ccw_device_set_online() - enable a ccw device for I/O
409 * @cdev: target ccw device
411 * This function first enables @cdev and then calls the driver's set_online()
412 * function for @cdev, if given. If set_online() returns an error, @cdev is
415 * %0 on success and a negative error value on failure.
417 * enabled, ccw device lock not held
419 int ccw_device_set_online(struct ccw_device *cdev)
426 if (cdev->online || !cdev->drv)
428 /* Hold on to an extra reference while device is online. */
429 if (!get_device(&cdev->dev))
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_online(cdev);
434 spin_unlock_irq(cdev->ccwlock);
436 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
438 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
442 /* Give up online reference since onlining failed. */
443 put_device(&cdev->dev);
446 spin_lock_irq(cdev->ccwlock);
447 /* Check if online processing was successful */
448 if ((cdev->private->state != DEV_STATE_ONLINE) &&
449 (cdev->private->state != DEV_STATE_W4SENSE)) {
450 spin_unlock_irq(cdev->ccwlock);
451 /* Give up online reference since onlining failed. */
452 put_device(&cdev->dev);
455 spin_unlock_irq(cdev->ccwlock);
456 if (cdev->drv->set_online)
457 ret = cdev->drv->set_online(cdev);
464 spin_lock_irq(cdev->ccwlock);
465 /* Wait until a final state or DISCONNECTED is reached */
466 while (!dev_fsm_final_state(cdev) &&
467 cdev->private->state != DEV_STATE_DISCONNECTED) {
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 spin_lock_irq(cdev->ccwlock);
473 ret2 = ccw_device_offline(cdev);
476 spin_unlock_irq(cdev->ccwlock);
477 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
478 cdev->private->state == DEV_STATE_DISCONNECTED));
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
484 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
485 "device 0.%x.%04x\n",
486 ret2, cdev->private->dev_id.ssid,
487 cdev->private->dev_id.devno);
488 cdev->private->state = DEV_STATE_OFFLINE;
489 spin_unlock_irq(cdev->ccwlock);
490 /* Give up online reference since onlining failed. */
491 put_device(&cdev->dev);
495 static int online_store_handle_offline(struct ccw_device *cdev)
497 if (cdev->private->state == DEV_STATE_DISCONNECTED)
498 ccw_device_remove_disconnected(cdev);
499 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
500 return ccw_device_set_offline(cdev);
504 static int online_store_recog_and_online(struct ccw_device *cdev)
508 /* Do device recognition, if needed. */
509 if (cdev->private->state == DEV_STATE_BOXED) {
510 ret = ccw_device_recognition(cdev);
512 CIO_MSG_EVENT(0, "Couldn't start recognition "
513 "for device 0.%x.%04x (ret=%d)\n",
514 cdev->private->dev_id.ssid,
515 cdev->private->dev_id.devno, ret);
518 wait_event(cdev->private->wait_q,
519 cdev->private->flags.recog_done);
520 if (cdev->private->state != DEV_STATE_OFFLINE)
521 /* recognition failed */
524 if (cdev->drv && cdev->drv->set_online)
525 ccw_device_set_online(cdev);
529 static int online_store_handle_online(struct ccw_device *cdev, int force)
533 ret = online_store_recog_and_online(cdev);
536 if (force && cdev->private->state == DEV_STATE_BOXED) {
537 ret = ccw_device_stlck(cdev);
540 if (cdev->id.cu_type == 0)
541 cdev->private->state = DEV_STATE_NOT_OPER;
542 ret = online_store_recog_and_online(cdev);
549 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct ccw_device *cdev = to_ccwdev(dev);
556 if ((cdev->private->state != DEV_STATE_OFFLINE &&
557 cdev->private->state != DEV_STATE_ONLINE &&
558 cdev->private->state != DEV_STATE_BOXED &&
559 cdev->private->state != DEV_STATE_DISCONNECTED) ||
560 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
563 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
564 atomic_set(&cdev->private->onoff, 0);
567 if (!strncmp(buf, "force\n", count)) {
573 ret = strict_strtoul(buf, 16, &i);
579 ret = online_store_handle_offline(cdev);
582 ret = online_store_handle_online(cdev, force);
589 module_put(cdev->drv->owner);
590 atomic_set(&cdev->private->onoff, 0);
591 return (ret < 0) ? ret : count;
595 available_show (struct device *dev, struct device_attribute *attr, char *buf)
597 struct ccw_device *cdev = to_ccwdev(dev);
598 struct subchannel *sch;
600 if (ccw_device_is_orphan(cdev))
601 return sprintf(buf, "no device\n");
602 switch (cdev->private->state) {
603 case DEV_STATE_BOXED:
604 return sprintf(buf, "boxed\n");
605 case DEV_STATE_DISCONNECTED:
606 case DEV_STATE_DISCONNECTED_SENSE_ID:
607 case DEV_STATE_NOT_OPER:
608 sch = to_subchannel(dev->parent);
610 return sprintf(buf, "no path\n");
612 return sprintf(buf, "no device\n");
614 /* All other states considered fine. */
615 return sprintf(buf, "good\n");
619 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
620 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
621 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
622 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
623 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
624 static DEVICE_ATTR(online, 0644, online_show, online_store);
625 static DEVICE_ATTR(availability, 0444, available_show, NULL);
627 static struct attribute *io_subchannel_attrs[] = {
628 &dev_attr_chpids.attr,
629 &dev_attr_pimpampom.attr,
633 static struct attribute_group io_subchannel_attr_group = {
634 .attrs = io_subchannel_attrs,
637 static struct attribute * ccwdev_attrs[] = {
638 &dev_attr_devtype.attr,
639 &dev_attr_cutype.attr,
640 &dev_attr_modalias.attr,
641 &dev_attr_online.attr,
642 &dev_attr_cmb_enable.attr,
643 &dev_attr_availability.attr,
647 static struct attribute_group ccwdev_attr_group = {
648 .attrs = ccwdev_attrs,
651 static const struct attribute_group *ccwdev_attr_groups[] = {
656 /* this is a simple abstraction for device_register that sets the
657 * correct bus type and adds the bus specific files */
658 static int ccw_device_register(struct ccw_device *cdev)
660 struct device *dev = &cdev->dev;
663 dev->bus = &ccw_bus_type;
664 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
665 cdev->private->dev_id.devno);
668 ret = device_add(dev);
672 set_bit(1, &cdev->private->registered);
676 static int match_dev_id(struct device *dev, void *data)
678 struct ccw_device *cdev = to_ccwdev(dev);
679 struct ccw_dev_id *dev_id = data;
681 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
684 static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
688 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
690 return dev ? to_ccwdev(dev) : NULL;
693 void ccw_device_do_unbind_bind(struct work_struct *work)
695 struct ccw_device_private *priv;
696 struct ccw_device *cdev;
697 struct subchannel *sch;
700 priv = container_of(work, struct ccw_device_private, kick_work);
702 sch = to_subchannel(cdev->dev.parent);
704 if (test_bit(1, &cdev->private->registered)) {
705 device_release_driver(&cdev->dev);
706 ret = device_attach(&cdev->dev);
707 WARN_ON(ret == -ENODEV);
712 ccw_device_release(struct device *dev)
714 struct ccw_device *cdev;
716 cdev = to_ccwdev(dev);
717 /* Release reference of parent subchannel. */
718 put_device(cdev->dev.parent);
719 kfree(cdev->private);
723 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
725 struct ccw_device *cdev;
727 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
729 cdev->private = kzalloc(sizeof(struct ccw_device_private),
730 GFP_KERNEL | GFP_DMA);
735 return ERR_PTR(-ENOMEM);
738 static int io_subchannel_initialize_dev(struct subchannel *sch,
739 struct ccw_device *cdev)
741 cdev->private->cdev = cdev;
742 atomic_set(&cdev->private->onoff, 0);
743 cdev->dev.parent = &sch->dev;
744 cdev->dev.release = ccw_device_release;
745 INIT_WORK(&cdev->private->kick_work, NULL);
746 cdev->dev.groups = ccwdev_attr_groups;
747 /* Do first half of device_register. */
748 device_initialize(&cdev->dev);
749 if (!get_device(&sch->dev)) {
750 /* Release reference from device_initialize(). */
751 put_device(&cdev->dev);
757 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
759 struct ccw_device *cdev;
762 cdev = io_subchannel_allocate_dev(sch);
764 ret = io_subchannel_initialize_dev(sch, cdev);
771 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
773 static void sch_create_and_recog_new_device(struct subchannel *sch)
775 struct ccw_device *cdev;
777 /* Need to allocate a new ccw device. */
778 cdev = io_subchannel_create_ccwdev(sch);
780 /* OK, we did everything we could... */
781 css_sch_device_unregister(sch);
784 /* Start recognition for the new ccw device. */
785 if (io_subchannel_recog(cdev, sch)) {
786 spin_lock_irq(sch->lock);
787 sch_set_cdev(sch, NULL);
788 spin_unlock_irq(sch->lock);
789 css_sch_device_unregister(sch);
790 /* Put reference from io_subchannel_create_ccwdev(). */
791 put_device(&sch->dev);
792 /* Give up initial reference. */
793 put_device(&cdev->dev);
798 * Register recognized device.
801 io_subchannel_register(struct work_struct *work)
803 struct ccw_device_private *priv;
804 struct ccw_device *cdev;
805 struct subchannel *sch;
809 priv = container_of(work, struct ccw_device_private, kick_work);
811 sch = to_subchannel(cdev->dev.parent);
813 * Check if subchannel is still registered. It may have become
814 * unregistered if a machine check hit us after finishing
815 * device recognition but before the register work could be
818 if (!device_is_registered(&sch->dev))
820 css_update_ssd_info(sch);
822 * io_subchannel_register() will also be called after device
823 * recognition has been done for a boxed device (which will already
824 * be registered). We need to reprobe since we may now have sense id
827 if (device_is_registered(&cdev->dev)) {
829 ret = device_reprobe(&cdev->dev);
831 /* We can't do much here. */
832 CIO_MSG_EVENT(0, "device_reprobe() returned"
833 " %d for 0.%x.%04x\n", ret,
834 cdev->private->dev_id.ssid,
835 cdev->private->dev_id.devno);
840 * Now we know this subchannel will stay, we can throw
841 * our delayed uevent.
843 dev_set_uevent_suppress(&sch->dev, 0);
844 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
845 /* make it known to the system */
846 ret = ccw_device_register(cdev);
848 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
849 cdev->private->dev_id.ssid,
850 cdev->private->dev_id.devno, ret);
851 spin_lock_irqsave(sch->lock, flags);
852 sch_set_cdev(sch, NULL);
853 spin_unlock_irqrestore(sch->lock, flags);
854 /* Release initial device reference. */
855 put_device(&cdev->dev);
859 cdev->private->flags.recog_done = 1;
860 wake_up(&cdev->private->wait_q);
862 /* Release reference for workqueue processing. */
863 put_device(&cdev->dev);
864 if (atomic_dec_and_test(&ccw_device_init_count))
865 wake_up(&ccw_device_init_wq);
868 static void ccw_device_call_sch_unregister(struct work_struct *work)
870 struct ccw_device_private *priv;
871 struct ccw_device *cdev;
872 struct subchannel *sch;
874 priv = container_of(work, struct ccw_device_private, kick_work);
876 /* Get subchannel reference for local processing. */
877 if (!get_device(cdev->dev.parent))
879 sch = to_subchannel(cdev->dev.parent);
880 css_sch_device_unregister(sch);
881 /* Release cdev reference for workqueue processing.*/
882 put_device(&cdev->dev);
883 /* Release subchannel reference for local processing. */
884 put_device(&sch->dev);
887 void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
889 /* Get cdev reference for workqueue processing. */
890 if (!get_device(&cdev->dev))
892 PREPARE_WORK(&cdev->private->kick_work,
893 ccw_device_call_sch_unregister);
894 queue_work(slow_path_wq, &cdev->private->kick_work);
898 * subchannel recognition done. Called from the state machine.
901 io_subchannel_recog_done(struct ccw_device *cdev)
903 if (css_init_done == 0) {
904 cdev->private->flags.recog_done = 1;
907 switch (cdev->private->state) {
908 case DEV_STATE_BOXED:
909 /* Device did not respond in time. */
910 case DEV_STATE_NOT_OPER:
911 cdev->private->flags.recog_done = 1;
912 ccw_device_schedule_sch_unregister(cdev);
913 if (atomic_dec_and_test(&ccw_device_init_count))
914 wake_up(&ccw_device_init_wq);
916 case DEV_STATE_OFFLINE:
918 * We can't register the device in interrupt context so
919 * we schedule a work item.
921 if (!get_device(&cdev->dev))
923 PREPARE_WORK(&cdev->private->kick_work,
924 io_subchannel_register);
925 queue_work(slow_path_wq, &cdev->private->kick_work);
931 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
934 struct ccw_device_private *priv;
936 cdev->ccwlock = sch->lock;
938 /* Init private data. */
939 priv = cdev->private;
940 priv->dev_id.devno = sch->schib.pmcw.dev;
941 priv->dev_id.ssid = sch->schid.ssid;
942 priv->schid = sch->schid;
943 priv->state = DEV_STATE_NOT_OPER;
944 INIT_LIST_HEAD(&priv->cmb_list);
945 init_waitqueue_head(&priv->wait_q);
946 init_timer(&priv->timer);
948 /* Increase counter of devices currently in recognition. */
949 atomic_inc(&ccw_device_init_count);
951 /* Start async. device sensing. */
952 spin_lock_irq(sch->lock);
953 sch_set_cdev(sch, cdev);
954 rc = ccw_device_recognition(cdev);
955 spin_unlock_irq(sch->lock);
957 if (atomic_dec_and_test(&ccw_device_init_count))
958 wake_up(&ccw_device_init_wq);
963 static int ccw_device_move_to_sch(struct ccw_device *cdev,
964 struct subchannel *sch)
966 struct subchannel *old_sch;
969 old_sch = to_subchannel(cdev->dev.parent);
970 /* Obtain child reference for new parent. */
971 if (!get_device(&sch->dev))
973 mutex_lock(&sch->reg_mutex);
974 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
975 mutex_unlock(&sch->reg_mutex);
977 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
978 cdev->private->dev_id.ssid,
979 cdev->private->dev_id.devno, sch->schid.ssid,
980 sch->schib.pmcw.dev, rc);
981 /* Release child reference for new parent. */
982 put_device(&sch->dev);
985 /* Clean up old subchannel. */
986 if (!sch_is_pseudo_sch(old_sch)) {
987 spin_lock_irq(old_sch->lock);
988 sch_set_cdev(old_sch, NULL);
989 cio_disable_subchannel(old_sch);
990 spin_unlock_irq(old_sch->lock);
991 css_schedule_eval(old_sch->schid);
993 /* Release child reference for old parent. */
994 put_device(&old_sch->dev);
995 /* Initialize new subchannel. */
996 spin_lock_irq(sch->lock);
997 cdev->private->schid = sch->schid;
998 cdev->ccwlock = sch->lock;
999 if (!sch_is_pseudo_sch(sch))
1000 sch_set_cdev(sch, cdev);
1001 spin_unlock_irq(sch->lock);
1002 if (!sch_is_pseudo_sch(sch))
1003 css_update_ssd_info(sch);
1007 static int ccw_device_move_to_orph(struct ccw_device *cdev)
1009 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1010 struct channel_subsystem *css = to_css(sch->dev.parent);
1012 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1015 static void io_subchannel_irq(struct subchannel *sch)
1017 struct ccw_device *cdev;
1019 cdev = sch_get_cdev(sch);
1021 CIO_TRACE_EVENT(6, "IRQ");
1022 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1024 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1027 void io_subchannel_init_config(struct subchannel *sch)
1029 memset(&sch->config, 0, sizeof(sch->config));
1030 sch->config.csense = 1;
1031 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1032 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1036 static void io_subchannel_init_fields(struct subchannel *sch)
1038 if (cio_is_console(sch->schid))
1041 sch->opm = chp_get_sch_opm(sch);
1042 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1043 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1045 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1046 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1047 sch->schib.pmcw.dev, sch->schid.ssid,
1048 sch->schid.sch_no, sch->schib.pmcw.pim,
1049 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1051 io_subchannel_init_config(sch);
1055 * Note: We always return 0 so that we bind to the device even on error.
1056 * This is needed so that our remove function is called on unregister.
1058 static int io_subchannel_probe(struct subchannel *sch)
1060 struct ccw_device *cdev;
1063 if (cio_is_console(sch->schid)) {
1064 rc = sysfs_create_group(&sch->dev.kobj,
1065 &io_subchannel_attr_group);
1067 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1068 "attributes for subchannel "
1069 "0.%x.%04x (rc=%d)\n",
1070 sch->schid.ssid, sch->schid.sch_no, rc);
1072 * The console subchannel already has an associated ccw_device.
1073 * Throw the delayed uevent for the subchannel, register
1074 * the ccw_device and exit.
1076 dev_set_uevent_suppress(&sch->dev, 0);
1077 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1078 cdev = sch_get_cdev(sch);
1079 cdev->dev.groups = ccwdev_attr_groups;
1080 device_initialize(&cdev->dev);
1081 ccw_device_register(cdev);
1083 * Check if the device is already online. If it is
1084 * the reference count needs to be corrected since we
1085 * didn't obtain a reference in ccw_device_set_online.
1087 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1088 cdev->private->state != DEV_STATE_OFFLINE &&
1089 cdev->private->state != DEV_STATE_BOXED)
1090 get_device(&cdev->dev);
1093 io_subchannel_init_fields(sch);
1094 rc = cio_commit_config(sch);
1097 rc = sysfs_create_group(&sch->dev.kobj,
1098 &io_subchannel_attr_group);
1101 /* Allocate I/O subchannel private data. */
1102 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1103 GFP_KERNEL | GFP_DMA);
1106 css_schedule_eval(sch->schid);
1110 spin_lock_irq(sch->lock);
1111 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1112 spin_unlock_irq(sch->lock);
1117 io_subchannel_remove (struct subchannel *sch)
1119 struct ccw_device *cdev;
1120 unsigned long flags;
1122 cdev = sch_get_cdev(sch);
1125 /* Set ccw device to not operational and drop reference. */
1126 spin_lock_irqsave(cdev->ccwlock, flags);
1127 sch_set_cdev(sch, NULL);
1128 cdev->private->state = DEV_STATE_NOT_OPER;
1129 spin_unlock_irqrestore(cdev->ccwlock, flags);
1130 ccw_device_unregister(cdev);
1132 kfree(sch->private);
1133 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1137 static void io_subchannel_verify(struct subchannel *sch)
1139 struct ccw_device *cdev;
1141 cdev = sch_get_cdev(sch);
1143 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1146 static int check_for_io_on_path(struct subchannel *sch, int mask)
1148 if (cio_update_schib(sch))
1150 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1155 static void terminate_internal_io(struct subchannel *sch,
1156 struct ccw_device *cdev)
1158 if (cio_clear(sch)) {
1159 /* Recheck device in case clear failed. */
1162 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1164 css_schedule_eval(sch->schid);
1167 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1168 /* Request retry of internal operation. */
1169 cdev->private->flags.intretry = 1;
1172 cdev->handler(cdev, cdev->private->intparm,
1176 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1178 struct ccw_device *cdev;
1180 cdev = sch_get_cdev(sch);
1183 if (check_for_io_on_path(sch, mask)) {
1184 if (cdev->private->state == DEV_STATE_ONLINE)
1185 ccw_device_kill_io(cdev);
1187 terminate_internal_io(sch, cdev);
1188 /* Re-start path verification. */
1189 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1192 /* trigger path verification. */
1193 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1197 static int io_subchannel_chp_event(struct subchannel *sch,
1198 struct chp_link *link, int event)
1202 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1209 io_subchannel_terminate_path(sch, mask);
1214 io_subchannel_verify(sch);
1217 if (cio_update_schib(sch))
1219 io_subchannel_terminate_path(sch, mask);
1222 if (cio_update_schib(sch))
1224 sch->lpm |= mask & sch->opm;
1225 io_subchannel_verify(sch);
1232 io_subchannel_shutdown(struct subchannel *sch)
1234 struct ccw_device *cdev;
1237 cdev = sch_get_cdev(sch);
1239 if (cio_is_console(sch->schid))
1241 if (!sch->schib.pmcw.ena)
1242 /* Nothing to do. */
1244 ret = cio_disable_subchannel(sch);
1246 /* Subchannel is disabled, we're done. */
1248 cdev->private->state = DEV_STATE_QUIESCE;
1250 cdev->handler(cdev, cdev->private->intparm,
1252 ret = ccw_device_cancel_halt_clear(cdev);
1253 if (ret == -EBUSY) {
1254 ccw_device_set_timeout(cdev, HZ/10);
1255 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1257 cio_disable_subchannel(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_device_private *priv = cdev->private;
1339 spin_lock_irq(cdev->ccwlock);
1340 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1341 (priv->state == DEV_STATE_OFFLINE);
1342 spin_unlock_irq(cdev->ccwlock);
1345 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1346 priv->dev_id.devno);
1347 ccw_device_schedule_sch_unregister(cdev);
1350 /* Abort loop in case of pending signal. */
1351 if (signal_pending(current))
1358 * ccw_purge_blacklisted - purge unused, blacklisted devices
1360 * Unregister all ccw devices that are offline and on the blacklist.
1362 int ccw_purge_blacklisted(void)
1364 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1365 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1369 void ccw_device_set_disconnected(struct ccw_device *cdev)
1373 ccw_device_set_timeout(cdev, 0);
1374 cdev->private->flags.fake_irb = 0;
1375 cdev->private->state = DEV_STATE_DISCONNECTED;
1377 ccw_device_schedule_recovery();
1380 void ccw_device_set_notoper(struct ccw_device *cdev)
1382 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1384 CIO_TRACE_EVENT(2, "notoper");
1385 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1386 ccw_device_set_timeout(cdev, 0);
1387 cio_disable_subchannel(sch);
1388 cdev->private->state = DEV_STATE_NOT_OPER;
1391 enum io_sch_action {
1395 IO_SCH_UNREG_ATTACH,
1403 static enum io_sch_action sch_get_action(struct subchannel *sch)
1405 struct ccw_device *cdev;
1407 cdev = sch_get_cdev(sch);
1408 if (cio_update_schib(sch)) {
1409 /* Not operational. */
1411 return IO_SCH_UNREG;
1412 if (!ccw_device_notify(cdev, CIO_GONE))
1413 return IO_SCH_UNREG;
1414 return IO_SCH_ORPH_UNREG;
1418 return IO_SCH_ATTACH;
1419 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1420 if (!ccw_device_notify(cdev, CIO_GONE))
1421 return IO_SCH_UNREG_ATTACH;
1422 return IO_SCH_ORPH_ATTACH;
1424 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1425 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1426 return IO_SCH_UNREG;
1429 if (device_is_disconnected(cdev))
1430 return IO_SCH_REPROBE;
1432 return IO_SCH_VERIFY;
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 action = sch_get_action(sch);
1460 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1461 sch->schid.ssid, sch->schid.sch_no, process,
1463 /* Perform immediate actions while holding the lock. */
1464 cdev = sch_get_cdev(sch);
1466 case IO_SCH_REPROBE:
1467 /* Trigger device recognition. */
1468 ccw_device_trigger_reprobe(cdev);
1472 /* Trigger path verification. */
1473 io_subchannel_verify(sch);
1477 ccw_device_set_disconnected(cdev);
1480 case IO_SCH_ORPH_UNREG:
1481 case IO_SCH_ORPH_ATTACH:
1482 ccw_device_set_disconnected(cdev);
1484 case IO_SCH_UNREG_ATTACH:
1487 ccw_device_set_notoper(cdev);
1495 spin_unlock_irqrestore(sch->lock, flags);
1496 /* All other actions require process context. */
1499 /* Handle attached ccw device. */
1501 case IO_SCH_ORPH_UNREG:
1502 case IO_SCH_ORPH_ATTACH:
1503 /* Move ccw device to orphanage. */
1504 rc = ccw_device_move_to_orph(cdev);
1508 case IO_SCH_UNREG_ATTACH:
1509 /* Unregister ccw device. */
1510 ccw_device_unregister(cdev);
1515 /* Handle subchannel. */
1517 case IO_SCH_ORPH_UNREG:
1519 css_sch_device_unregister(sch);
1521 case IO_SCH_ORPH_ATTACH:
1522 case IO_SCH_UNREG_ATTACH:
1524 dev_id.ssid = sch->schid.ssid;
1525 dev_id.devno = sch->schib.pmcw.dev;
1526 cdev = get_ccwdev_by_dev_id(&dev_id);
1528 sch_create_and_recog_new_device(sch);
1531 rc = ccw_device_move_to_sch(cdev, sch);
1533 /* Release reference from get_ccwdev_by_dev_id() */
1534 put_device(&cdev->dev);
1537 spin_lock_irqsave(sch->lock, flags);
1538 ccw_device_trigger_reprobe(cdev);
1539 spin_unlock_irqrestore(sch->lock, flags);
1540 /* Release reference from get_ccwdev_by_dev_id() */
1541 put_device(&cdev->dev);
1549 spin_unlock_irqrestore(sch->lock, flags);
1554 #ifdef CONFIG_CCW_CONSOLE
1555 static struct ccw_device console_cdev;
1556 static struct ccw_device_private console_private;
1557 static int console_cdev_in_use;
1559 static DEFINE_SPINLOCK(ccw_console_lock);
1561 spinlock_t * cio_get_console_lock(void)
1563 return &ccw_console_lock;
1566 static int ccw_device_console_enable(struct ccw_device *cdev,
1567 struct subchannel *sch)
1571 /* Attach subchannel private data. */
1572 sch->private = cio_get_console_priv();
1573 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1574 io_subchannel_init_fields(sch);
1575 rc = cio_commit_config(sch);
1578 sch->driver = &io_subchannel_driver;
1579 /* Initialize the ccw_device structure. */
1580 cdev->dev.parent= &sch->dev;
1581 rc = io_subchannel_recog(cdev, sch);
1585 /* Now wait for the async. recognition to come to an end. */
1586 spin_lock_irq(cdev->ccwlock);
1587 while (!dev_fsm_final_state(cdev))
1590 if (cdev->private->state != DEV_STATE_OFFLINE)
1592 ccw_device_online(cdev);
1593 while (!dev_fsm_final_state(cdev))
1595 if (cdev->private->state != DEV_STATE_ONLINE)
1599 spin_unlock_irq(cdev->ccwlock);
1604 ccw_device_probe_console(void)
1606 struct subchannel *sch;
1609 if (xchg(&console_cdev_in_use, 1) != 0)
1610 return ERR_PTR(-EBUSY);
1611 sch = cio_probe_console();
1613 console_cdev_in_use = 0;
1614 return (void *) sch;
1616 memset(&console_cdev, 0, sizeof(struct ccw_device));
1617 memset(&console_private, 0, sizeof(struct ccw_device_private));
1618 console_cdev.private = &console_private;
1619 console_private.cdev = &console_cdev;
1620 ret = ccw_device_console_enable(&console_cdev, sch);
1622 cio_release_console();
1623 console_cdev_in_use = 0;
1624 return ERR_PTR(ret);
1626 console_cdev.online = 1;
1627 return &console_cdev;
1630 static int ccw_device_pm_restore(struct device *dev);
1632 int ccw_device_force_console(void)
1634 if (!console_cdev_in_use)
1636 return ccw_device_pm_restore(&console_cdev.dev);
1638 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1642 * get ccw_device matching the busid, but only if owned by cdrv
1645 __ccwdev_check_busid(struct device *dev, void *id)
1651 return (strcmp(bus_id, dev_name(dev)) == 0);
1656 * get_ccwdev_by_busid() - obtain device from a bus id
1657 * @cdrv: driver the device is owned by
1658 * @bus_id: bus id of the device to be searched
1660 * This function searches all devices owned by @cdrv for a device with a bus
1661 * id matching @bus_id.
1663 * If a match is found, its reference count of the found device is increased
1664 * and it is returned; else %NULL is returned.
1666 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1670 struct device_driver *drv;
1672 drv = get_driver(&cdrv->driver);
1676 dev = driver_find_device(drv, NULL, (void *)bus_id,
1677 __ccwdev_check_busid);
1680 return dev ? to_ccwdev(dev) : NULL;
1683 /************************** device driver handling ************************/
1685 /* This is the implementation of the ccw_driver class. The probe, remove
1686 * and release methods are initially very similar to the device_driver
1687 * implementations, with the difference that they have ccw_device
1690 * A ccw driver also contains the information that is needed for
1694 ccw_device_probe (struct device *dev)
1696 struct ccw_device *cdev = to_ccwdev(dev);
1697 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1700 cdev->drv = cdrv; /* to let the driver call _set_online */
1702 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1713 ccw_device_remove (struct device *dev)
1715 struct ccw_device *cdev = to_ccwdev(dev);
1716 struct ccw_driver *cdrv = cdev->drv;
1723 spin_lock_irq(cdev->ccwlock);
1724 ret = ccw_device_offline(cdev);
1725 spin_unlock_irq(cdev->ccwlock);
1727 wait_event(cdev->private->wait_q,
1728 dev_fsm_final_state(cdev));
1730 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1731 "device 0.%x.%04x\n",
1732 ret, cdev->private->dev_id.ssid,
1733 cdev->private->dev_id.devno);
1734 /* Give up reference obtained in ccw_device_set_online(). */
1735 put_device(&cdev->dev);
1737 ccw_device_set_timeout(cdev, 0);
1742 static void ccw_device_shutdown(struct device *dev)
1744 struct ccw_device *cdev;
1746 cdev = to_ccwdev(dev);
1747 if (cdev->drv && cdev->drv->shutdown)
1748 cdev->drv->shutdown(cdev);
1752 static int ccw_device_pm_prepare(struct device *dev)
1754 struct ccw_device *cdev = to_ccwdev(dev);
1756 if (work_pending(&cdev->private->kick_work))
1758 /* Fail while device is being set online/offline. */
1759 if (atomic_read(&cdev->private->onoff))
1762 if (cdev->online && cdev->drv && cdev->drv->prepare)
1763 return cdev->drv->prepare(cdev);
1768 static void ccw_device_pm_complete(struct device *dev)
1770 struct ccw_device *cdev = to_ccwdev(dev);
1772 if (cdev->online && cdev->drv && cdev->drv->complete)
1773 cdev->drv->complete(cdev);
1776 static int ccw_device_pm_freeze(struct device *dev)
1778 struct ccw_device *cdev = to_ccwdev(dev);
1779 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1780 int ret, cm_enabled;
1782 /* Fail suspend while device is in transistional state. */
1783 if (!dev_fsm_final_state(cdev))
1787 if (cdev->drv && cdev->drv->freeze) {
1788 ret = cdev->drv->freeze(cdev);
1793 spin_lock_irq(sch->lock);
1794 cm_enabled = cdev->private->cmb != NULL;
1795 spin_unlock_irq(sch->lock);
1797 /* Don't have the css write on memory. */
1798 ret = ccw_set_cmf(cdev, 0);
1802 /* From here on, disallow device driver I/O. */
1803 spin_lock_irq(sch->lock);
1804 ret = cio_disable_subchannel(sch);
1805 spin_unlock_irq(sch->lock);
1810 static int ccw_device_pm_thaw(struct device *dev)
1812 struct ccw_device *cdev = to_ccwdev(dev);
1813 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1814 int ret, cm_enabled;
1819 spin_lock_irq(sch->lock);
1820 /* Allow device driver I/O again. */
1821 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1822 cm_enabled = cdev->private->cmb != NULL;
1823 spin_unlock_irq(sch->lock);
1828 ret = ccw_set_cmf(cdev, 1);
1833 if (cdev->drv && cdev->drv->thaw)
1834 ret = cdev->drv->thaw(cdev);
1839 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1841 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1844 if (cio_is_console(sch->schid))
1847 * While we were sleeping, devices may have gone or become
1848 * available again. Kick re-detection.
1850 spin_lock_irq(sch->lock);
1851 cdev->private->flags.resuming = 1;
1852 ret = ccw_device_recognition(cdev);
1853 spin_unlock_irq(sch->lock);
1855 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
1856 "0.%x.%04x (ret=%d)\n",
1857 cdev->private->dev_id.ssid,
1858 cdev->private->dev_id.devno, ret);
1859 spin_lock_irq(sch->lock);
1860 cdev->private->state = DEV_STATE_DISCONNECTED;
1861 spin_unlock_irq(sch->lock);
1862 /* notify driver after the resume cb */
1865 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1866 cdev->private->state == DEV_STATE_DISCONNECTED);
1869 cdev->private->flags.resuming = 0;
1872 static int resume_handle_boxed(struct ccw_device *cdev)
1874 cdev->private->state = DEV_STATE_BOXED;
1875 if (ccw_device_notify(cdev, CIO_BOXED))
1877 ccw_device_schedule_sch_unregister(cdev);
1881 static int resume_handle_disc(struct ccw_device *cdev)
1883 cdev->private->state = DEV_STATE_DISCONNECTED;
1884 if (ccw_device_notify(cdev, CIO_GONE))
1886 ccw_device_schedule_sch_unregister(cdev);
1890 static int ccw_device_pm_restore(struct device *dev)
1892 struct ccw_device *cdev = to_ccwdev(dev);
1893 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1894 int ret = 0, cm_enabled;
1896 __ccw_device_pm_restore(cdev);
1897 spin_lock_irq(sch->lock);
1898 if (cio_is_console(sch->schid)) {
1899 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1900 spin_unlock_irq(sch->lock);
1903 cdev->private->flags.donotify = 0;
1904 /* check recognition results */
1905 switch (cdev->private->state) {
1906 case DEV_STATE_OFFLINE:
1908 case DEV_STATE_BOXED:
1909 ret = resume_handle_boxed(cdev);
1910 spin_unlock_irq(sch->lock);
1914 case DEV_STATE_DISCONNECTED:
1915 goto out_disc_unlock;
1917 goto out_unreg_unlock;
1919 /* check if the device id has changed */
1920 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1921 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1922 "changed from %04x to %04x)\n",
1923 sch->schid.ssid, sch->schid.sch_no,
1924 cdev->private->dev_id.devno,
1925 sch->schib.pmcw.dev);
1926 goto out_unreg_unlock;
1928 /* check if the device type has changed */
1929 if (!ccw_device_test_sense_data(cdev)) {
1930 ccw_device_update_sense_data(cdev);
1931 PREPARE_WORK(&cdev->private->kick_work,
1932 ccw_device_do_unbind_bind);
1933 queue_work(ccw_device_work, &cdev->private->kick_work);
1937 if (!cdev->online) {
1941 ret = ccw_device_online(cdev);
1943 goto out_disc_unlock;
1945 cm_enabled = cdev->private->cmb != NULL;
1946 spin_unlock_irq(sch->lock);
1948 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1949 if (cdev->private->state != DEV_STATE_ONLINE) {
1950 spin_lock_irq(sch->lock);
1951 goto out_disc_unlock;
1954 ret = ccw_set_cmf(cdev, 1);
1956 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1957 "(rc=%d)\n", cdev->private->dev_id.ssid,
1958 cdev->private->dev_id.devno, ret);
1964 if (cdev->online && cdev->drv && cdev->drv->restore)
1965 ret = cdev->drv->restore(cdev);
1970 ret = resume_handle_disc(cdev);
1971 spin_unlock_irq(sch->lock);
1977 ccw_device_schedule_sch_unregister(cdev);
1980 spin_unlock_irq(sch->lock);
1984 static struct dev_pm_ops ccw_pm_ops = {
1985 .prepare = ccw_device_pm_prepare,
1986 .complete = ccw_device_pm_complete,
1987 .freeze = ccw_device_pm_freeze,
1988 .thaw = ccw_device_pm_thaw,
1989 .restore = ccw_device_pm_restore,
1992 struct bus_type ccw_bus_type = {
1994 .match = ccw_bus_match,
1995 .uevent = ccw_uevent,
1996 .probe = ccw_device_probe,
1997 .remove = ccw_device_remove,
1998 .shutdown = ccw_device_shutdown,
2003 * ccw_driver_register() - register a ccw driver
2004 * @cdriver: driver to be registered
2006 * This function is mainly a wrapper around driver_register().
2008 * %0 on success and a negative error value on failure.
2010 int ccw_driver_register(struct ccw_driver *cdriver)
2012 struct device_driver *drv = &cdriver->driver;
2014 drv->bus = &ccw_bus_type;
2015 drv->name = cdriver->name;
2016 drv->owner = cdriver->owner;
2018 return driver_register(drv);
2022 * ccw_driver_unregister() - deregister a ccw driver
2023 * @cdriver: driver to be deregistered
2025 * This function is mainly a wrapper around driver_unregister().
2027 void ccw_driver_unregister(struct ccw_driver *cdriver)
2029 driver_unregister(&cdriver->driver);
2032 /* Helper func for qdio. */
2033 struct subchannel_id
2034 ccw_device_get_subchannel_id(struct ccw_device *cdev)
2036 struct subchannel *sch;
2038 sch = to_subchannel(cdev->dev.parent);
2042 MODULE_LICENSE("GPL");
2043 EXPORT_SYMBOL(ccw_device_set_online);
2044 EXPORT_SYMBOL(ccw_device_set_offline);
2045 EXPORT_SYMBOL(ccw_driver_register);
2046 EXPORT_SYMBOL(ccw_driver_unregister);
2047 EXPORT_SYMBOL(get_ccwdev_by_busid);
2048 EXPORT_SYMBOL(ccw_bus_type);
2049 EXPORT_SYMBOL(ccw_device_work);
2050 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);