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 *,
135 static struct css_device_id io_subchannel_ids[] = {
136 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137 { /* end of list */ },
139 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
141 static int io_subchannel_prepare(struct subchannel *sch)
143 struct ccw_device *cdev;
145 * Don't allow suspend while a ccw device registration
146 * is still outstanding.
148 cdev = sch_get_cdev(sch);
149 if (cdev && !device_is_registered(&cdev->dev))
154 static struct css_driver io_subchannel_driver = {
155 .owner = THIS_MODULE,
156 .subchannel_type = io_subchannel_ids,
157 .name = "io_subchannel",
158 .irq = io_subchannel_irq,
159 .sch_event = io_subchannel_sch_event,
160 .chp_event = io_subchannel_chp_event,
161 .probe = io_subchannel_probe,
162 .remove = io_subchannel_remove,
163 .shutdown = io_subchannel_shutdown,
164 .prepare = io_subchannel_prepare,
167 struct workqueue_struct *ccw_device_work;
168 wait_queue_head_t ccw_device_init_wq;
169 atomic_t ccw_device_init_count;
171 static void recovery_func(unsigned long data);
173 int __init io_subchannel_init(void)
177 init_waitqueue_head(&ccw_device_init_wq);
178 atomic_set(&ccw_device_init_count, 0);
179 setup_timer(&recovery_timer, recovery_func, 0);
181 ccw_device_work = create_singlethread_workqueue("cio");
182 if (!ccw_device_work)
184 slow_path_wq = create_singlethread_workqueue("kslowcrw");
189 if ((ret = bus_register (&ccw_bus_type)))
192 ret = css_driver_register(&io_subchannel_driver);
199 destroy_workqueue(ccw_device_work);
201 destroy_workqueue(slow_path_wq);
206 /************************ device handling **************************/
209 * A ccw_device has some interfaces in sysfs in addition to the
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
217 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
219 struct subchannel *sch = to_subchannel(dev);
220 struct chsc_ssd_info *ssd = &sch->ssd_info;
225 for (chp = 0; chp < 8; chp++) {
227 if (ssd->path_mask & mask)
228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 ret += sprintf(buf + ret, "00 ");
232 ret += sprintf (buf+ret, "\n");
233 return min((ssize_t)PAGE_SIZE, ret);
237 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
239 struct subchannel *sch = to_subchannel(dev);
240 struct pmcw *pmcw = &sch->schib.pmcw;
242 return sprintf (buf, "%02x %02x %02x\n",
243 pmcw->pim, pmcw->pam, pmcw->pom);
247 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
249 struct ccw_device *cdev = to_ccwdev(dev);
250 struct ccw_device_id *id = &(cdev->id);
252 if (id->dev_type != 0)
253 return sprintf(buf, "%04x/%02x\n",
254 id->dev_type, id->dev_model);
256 return sprintf(buf, "n/a\n");
260 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
262 struct ccw_device *cdev = to_ccwdev(dev);
263 struct ccw_device_id *id = &(cdev->id);
265 return sprintf(buf, "%04x/%02x\n",
266 id->cu_type, id->cu_model);
270 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
276 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
278 return len > PAGE_SIZE ? PAGE_SIZE : len;
282 online_show (struct device *dev, struct device_attribute *attr, char *buf)
284 struct ccw_device *cdev = to_ccwdev(dev);
286 return sprintf(buf, cdev->online ? "1\n" : "0\n");
289 int ccw_device_is_orphan(struct ccw_device *cdev)
291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
294 static void ccw_device_unregister(struct ccw_device *cdev)
296 if (test_and_clear_bit(1, &cdev->private->registered)) {
297 device_del(&cdev->dev);
298 /* Release reference from device_initialize(). */
299 put_device(&cdev->dev);
303 static void ccw_device_remove_orphan_cb(struct work_struct *work)
305 struct ccw_device_private *priv;
306 struct ccw_device *cdev;
308 priv = container_of(work, struct ccw_device_private, kick_work);
310 ccw_device_unregister(cdev);
311 /* Release cdev reference for workqueue processing. */
312 put_device(&cdev->dev);
316 ccw_device_remove_disconnected(struct ccw_device *cdev)
321 * Forced offline in disconnected state means
322 * 'throw away device'.
324 if (ccw_device_is_orphan(cdev)) {
326 * Deregister ccw device.
327 * Unfortunately, we cannot do this directly from the
330 /* Get cdev reference for workqueue processing. */
331 if (!get_device(&cdev->dev))
333 spin_lock_irqsave(cdev->ccwlock, flags);
334 cdev->private->state = DEV_STATE_NOT_OPER;
335 spin_unlock_irqrestore(cdev->ccwlock, flags);
336 PREPARE_WORK(&cdev->private->kick_work,
337 ccw_device_remove_orphan_cb);
338 queue_work(slow_path_wq, &cdev->private->kick_work);
340 /* Deregister subchannel, which will kill the ccw device. */
341 ccw_device_schedule_sch_unregister(cdev);
345 * ccw_device_set_offline() - disable a ccw device for I/O
346 * @cdev: target ccw device
348 * This function calls the driver's set_offline() function for @cdev, if
349 * given, and then disables @cdev.
351 * %0 on success and a negative error value on failure.
353 * enabled, ccw device lock not held
355 int ccw_device_set_offline(struct ccw_device *cdev)
361 if (!cdev->online || !cdev->drv)
364 if (cdev->drv->set_offline) {
365 ret = cdev->drv->set_offline(cdev);
370 spin_lock_irq(cdev->ccwlock);
371 /* Wait until a final state or DISCONNECTED is reached */
372 while (!dev_fsm_final_state(cdev) &&
373 cdev->private->state != DEV_STATE_DISCONNECTED) {
374 spin_unlock_irq(cdev->ccwlock);
375 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
376 cdev->private->state == DEV_STATE_DISCONNECTED));
377 spin_lock_irq(cdev->ccwlock);
379 ret = ccw_device_offline(cdev);
382 spin_unlock_irq(cdev->ccwlock);
383 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
384 cdev->private->state == DEV_STATE_DISCONNECTED));
385 /* Give up reference from ccw_device_set_online(). */
386 put_device(&cdev->dev);
390 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
391 ret, cdev->private->dev_id.ssid,
392 cdev->private->dev_id.devno);
393 cdev->private->state = DEV_STATE_OFFLINE;
394 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
395 spin_unlock_irq(cdev->ccwlock);
396 /* Give up reference from ccw_device_set_online(). */
397 put_device(&cdev->dev);
402 * ccw_device_set_online() - enable a ccw device for I/O
403 * @cdev: target ccw device
405 * This function first enables @cdev and then calls the driver's set_online()
406 * function for @cdev, if given. If set_online() returns an error, @cdev is
409 * %0 on success and a negative error value on failure.
411 * enabled, ccw device lock not held
413 int ccw_device_set_online(struct ccw_device *cdev)
420 if (cdev->online || !cdev->drv)
422 /* Hold on to an extra reference while device is online. */
423 if (!get_device(&cdev->dev))
426 spin_lock_irq(cdev->ccwlock);
427 ret = ccw_device_online(cdev);
428 spin_unlock_irq(cdev->ccwlock);
430 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
432 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
433 "device 0.%x.%04x\n",
434 ret, cdev->private->dev_id.ssid,
435 cdev->private->dev_id.devno);
436 /* Give up online reference since onlining failed. */
437 put_device(&cdev->dev);
440 spin_lock_irq(cdev->ccwlock);
441 /* Check if online processing was successful */
442 if ((cdev->private->state != DEV_STATE_ONLINE) &&
443 (cdev->private->state != DEV_STATE_W4SENSE)) {
444 spin_unlock_irq(cdev->ccwlock);
445 /* Give up online reference since onlining failed. */
446 put_device(&cdev->dev);
449 spin_unlock_irq(cdev->ccwlock);
450 if (cdev->drv->set_online)
451 ret = cdev->drv->set_online(cdev);
458 spin_lock_irq(cdev->ccwlock);
459 /* Wait until a final state or DISCONNECTED is reached */
460 while (!dev_fsm_final_state(cdev) &&
461 cdev->private->state != DEV_STATE_DISCONNECTED) {
462 spin_unlock_irq(cdev->ccwlock);
463 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
464 cdev->private->state == DEV_STATE_DISCONNECTED));
465 spin_lock_irq(cdev->ccwlock);
467 ret2 = ccw_device_offline(cdev);
470 spin_unlock_irq(cdev->ccwlock);
471 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
472 cdev->private->state == DEV_STATE_DISCONNECTED));
473 /* Give up online reference since onlining failed. */
474 put_device(&cdev->dev);
478 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
479 "device 0.%x.%04x\n",
480 ret2, cdev->private->dev_id.ssid,
481 cdev->private->dev_id.devno);
482 cdev->private->state = DEV_STATE_OFFLINE;
483 spin_unlock_irq(cdev->ccwlock);
484 /* Give up online reference since onlining failed. */
485 put_device(&cdev->dev);
489 static int online_store_handle_offline(struct ccw_device *cdev)
491 if (cdev->private->state == DEV_STATE_DISCONNECTED)
492 ccw_device_remove_disconnected(cdev);
493 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
494 return ccw_device_set_offline(cdev);
498 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 ret = ccw_device_recognition(cdev);
506 CIO_MSG_EVENT(0, "Couldn't start recognition "
507 "for device 0.%x.%04x (ret=%d)\n",
508 cdev->private->dev_id.ssid,
509 cdev->private->dev_id.devno, ret);
512 wait_event(cdev->private->wait_q,
513 cdev->private->flags.recog_done);
514 if (cdev->private->state != DEV_STATE_OFFLINE)
515 /* recognition failed */
518 if (cdev->drv && cdev->drv->set_online)
519 ccw_device_set_online(cdev);
523 static int online_store_handle_online(struct ccw_device *cdev, int force)
527 ret = online_store_recog_and_online(cdev);
530 if (force && cdev->private->state == DEV_STATE_BOXED) {
531 ret = ccw_device_stlck(cdev);
534 if (cdev->id.cu_type == 0)
535 cdev->private->state = DEV_STATE_NOT_OPER;
536 ret = online_store_recog_and_online(cdev);
543 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
546 struct ccw_device *cdev = to_ccwdev(dev);
550 if ((cdev->private->state != DEV_STATE_OFFLINE &&
551 cdev->private->state != DEV_STATE_ONLINE &&
552 cdev->private->state != DEV_STATE_BOXED &&
553 cdev->private->state != DEV_STATE_DISCONNECTED) ||
554 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
557 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
558 atomic_set(&cdev->private->onoff, 0);
561 if (!strncmp(buf, "force\n", count)) {
567 ret = strict_strtoul(buf, 16, &i);
573 ret = online_store_handle_offline(cdev);
576 ret = online_store_handle_online(cdev, force);
583 module_put(cdev->drv->owner);
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");
613 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
614 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
615 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
616 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
617 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
618 static DEVICE_ATTR(online, 0644, online_show, online_store);
619 static DEVICE_ATTR(availability, 0444, available_show, NULL);
621 static struct attribute *io_subchannel_attrs[] = {
622 &dev_attr_chpids.attr,
623 &dev_attr_pimpampom.attr,
627 static struct attribute_group io_subchannel_attr_group = {
628 .attrs = io_subchannel_attrs,
631 static struct attribute * ccwdev_attrs[] = {
632 &dev_attr_devtype.attr,
633 &dev_attr_cutype.attr,
634 &dev_attr_modalias.attr,
635 &dev_attr_online.attr,
636 &dev_attr_cmb_enable.attr,
637 &dev_attr_availability.attr,
641 static struct attribute_group ccwdev_attr_group = {
642 .attrs = ccwdev_attrs,
645 static const struct attribute_group *ccwdev_attr_groups[] = {
650 /* this is a simple abstraction for device_register that sets the
651 * correct bus type and adds the bus specific files */
652 static int ccw_device_register(struct ccw_device *cdev)
654 struct device *dev = &cdev->dev;
657 dev->bus = &ccw_bus_type;
658 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
659 cdev->private->dev_id.devno);
662 ret = device_add(dev);
666 set_bit(1, &cdev->private->registered);
671 struct ccw_dev_id dev_id;
672 struct ccw_device * sibling;
676 match_devno(struct device * dev, void * data)
678 struct match_data * d = data;
679 struct ccw_device * cdev;
681 cdev = to_ccwdev(dev);
682 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
683 !ccw_device_is_orphan(cdev) &&
684 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
685 (cdev != d->sibling))
690 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
691 struct ccw_device *sibling)
694 struct match_data data;
696 data.dev_id = *dev_id;
697 data.sibling = sibling;
698 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
700 return dev ? to_ccwdev(dev) : NULL;
703 static int match_orphan(struct device *dev, void *data)
705 struct ccw_dev_id *dev_id;
706 struct ccw_device *cdev;
709 cdev = to_ccwdev(dev);
710 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
713 static struct ccw_device *
714 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
715 struct ccw_dev_id *dev_id)
719 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
722 return dev ? to_ccwdev(dev) : NULL;
725 void ccw_device_do_unbind_bind(struct work_struct *work)
727 struct ccw_device_private *priv;
728 struct ccw_device *cdev;
729 struct subchannel *sch;
732 priv = container_of(work, struct ccw_device_private, kick_work);
734 sch = to_subchannel(cdev->dev.parent);
736 if (test_bit(1, &cdev->private->registered)) {
737 device_release_driver(&cdev->dev);
738 ret = device_attach(&cdev->dev);
739 WARN_ON(ret == -ENODEV);
744 ccw_device_release(struct device *dev)
746 struct ccw_device *cdev;
748 cdev = to_ccwdev(dev);
749 /* Release reference of parent subchannel. */
750 put_device(cdev->dev.parent);
751 kfree(cdev->private);
755 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
757 struct ccw_device *cdev;
759 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
761 cdev->private = kzalloc(sizeof(struct ccw_device_private),
762 GFP_KERNEL | GFP_DMA);
767 return ERR_PTR(-ENOMEM);
770 static int io_subchannel_initialize_dev(struct subchannel *sch,
771 struct ccw_device *cdev)
773 cdev->private->cdev = cdev;
774 atomic_set(&cdev->private->onoff, 0);
775 cdev->dev.parent = &sch->dev;
776 cdev->dev.release = ccw_device_release;
777 INIT_WORK(&cdev->private->kick_work, NULL);
778 cdev->dev.groups = ccwdev_attr_groups;
779 /* Do first half of device_register. */
780 device_initialize(&cdev->dev);
781 if (!get_device(&sch->dev)) {
782 /* Release reference from device_initialize(). */
783 put_device(&cdev->dev);
789 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
791 struct ccw_device *cdev;
794 cdev = io_subchannel_allocate_dev(sch);
796 ret = io_subchannel_initialize_dev(sch, cdev);
803 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
805 static void sch_attach_device(struct subchannel *sch,
806 struct ccw_device *cdev)
808 css_update_ssd_info(sch);
809 spin_lock_irq(sch->lock);
810 sch_set_cdev(sch, cdev);
811 cdev->private->schid = sch->schid;
812 cdev->ccwlock = sch->lock;
813 ccw_device_trigger_reprobe(cdev);
814 spin_unlock_irq(sch->lock);
817 static void sch_attach_disconnected_device(struct subchannel *sch,
818 struct ccw_device *cdev)
820 struct subchannel *other_sch;
823 /* Get reference for new parent. */
824 if (!get_device(&sch->dev))
826 other_sch = to_subchannel(cdev->dev.parent);
827 /* Note: device_move() changes cdev->dev.parent */
828 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
830 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
831 "(ret=%d)!\n", cdev->private->dev_id.ssid,
832 cdev->private->dev_id.devno, ret);
833 /* Put reference for new parent. */
834 put_device(&sch->dev);
837 sch_set_cdev(other_sch, NULL);
838 /* No need to keep a subchannel without ccw device around. */
839 css_sch_device_unregister(other_sch);
840 sch_attach_device(sch, cdev);
841 /* Put reference for old parent. */
842 put_device(&other_sch->dev);
845 static void sch_attach_orphaned_device(struct subchannel *sch,
846 struct ccw_device *cdev)
849 struct subchannel *pseudo_sch;
851 /* Get reference for new parent. */
852 if (!get_device(&sch->dev))
854 pseudo_sch = to_subchannel(cdev->dev.parent);
856 * Try to move the ccw device to its new subchannel.
857 * Note: device_move() changes cdev->dev.parent
859 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
861 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
862 "failed (ret=%d)!\n",
863 cdev->private->dev_id.ssid,
864 cdev->private->dev_id.devno, ret);
865 /* Put reference for new parent. */
866 put_device(&sch->dev);
869 sch_attach_device(sch, cdev);
870 /* Put reference on pseudo subchannel. */
871 put_device(&pseudo_sch->dev);
874 static void sch_create_and_recog_new_device(struct subchannel *sch)
876 struct ccw_device *cdev;
878 /* Need to allocate a new ccw device. */
879 cdev = io_subchannel_create_ccwdev(sch);
881 /* OK, we did everything we could... */
882 css_sch_device_unregister(sch);
885 spin_lock_irq(sch->lock);
886 sch_set_cdev(sch, cdev);
887 spin_unlock_irq(sch->lock);
888 /* Start recognition for the new ccw device. */
889 if (io_subchannel_recog(cdev, sch)) {
890 spin_lock_irq(sch->lock);
891 sch_set_cdev(sch, NULL);
892 spin_unlock_irq(sch->lock);
893 css_sch_device_unregister(sch);
894 /* Put reference from io_subchannel_create_ccwdev(). */
895 put_device(&sch->dev);
896 /* Give up initial reference. */
897 put_device(&cdev->dev);
902 void ccw_device_move_to_orphanage(struct work_struct *work)
904 struct ccw_device_private *priv;
905 struct ccw_device *cdev;
906 struct ccw_device *replacing_cdev;
907 struct subchannel *sch;
909 struct channel_subsystem *css;
910 struct ccw_dev_id dev_id;
912 priv = container_of(work, struct ccw_device_private, kick_work);
914 sch = to_subchannel(cdev->dev.parent);
915 css = to_css(sch->dev.parent);
916 dev_id.devno = sch->schib.pmcw.dev;
917 dev_id.ssid = sch->schid.ssid;
919 /* Increase refcount for pseudo subchannel. */
920 get_device(&css->pseudo_subchannel->dev);
922 * Move the orphaned ccw device to the orphanage so the replacing
923 * ccw device can take its place on the subchannel.
924 * Note: device_move() changes cdev->dev.parent
926 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
929 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
930 "(ret=%d)!\n", cdev->private->dev_id.ssid,
931 cdev->private->dev_id.devno, ret);
932 /* Decrease refcount for pseudo subchannel again. */
933 put_device(&css->pseudo_subchannel->dev);
936 cdev->ccwlock = css->pseudo_subchannel->lock;
938 * Search for the replacing ccw device
939 * - among the disconnected devices
942 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
943 if (replacing_cdev) {
944 sch_attach_disconnected_device(sch, replacing_cdev);
945 /* Release reference from get_disc_ccwdev_by_dev_id() */
946 put_device(&replacing_cdev->dev);
947 /* Release reference of subchannel from old cdev. */
948 put_device(&sch->dev);
951 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
952 if (replacing_cdev) {
953 sch_attach_orphaned_device(sch, replacing_cdev);
954 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
955 put_device(&replacing_cdev->dev);
956 /* Release reference of subchannel from old cdev. */
957 put_device(&sch->dev);
960 sch_create_and_recog_new_device(sch);
961 /* Release reference of subchannel from old cdev. */
962 put_device(&sch->dev);
966 * Register recognized device.
969 io_subchannel_register(struct work_struct *work)
971 struct ccw_device_private *priv;
972 struct ccw_device *cdev;
973 struct subchannel *sch;
977 priv = container_of(work, struct ccw_device_private, kick_work);
979 sch = to_subchannel(cdev->dev.parent);
981 * Check if subchannel is still registered. It may have become
982 * unregistered if a machine check hit us after finishing
983 * device recognition but before the register work could be
986 if (!device_is_registered(&sch->dev))
988 css_update_ssd_info(sch);
990 * io_subchannel_register() will also be called after device
991 * recognition has been done for a boxed device (which will already
992 * be registered). We need to reprobe since we may now have sense id
995 if (device_is_registered(&cdev->dev)) {
997 ret = device_reprobe(&cdev->dev);
999 /* We can't do much here. */
1000 CIO_MSG_EVENT(0, "device_reprobe() returned"
1001 " %d for 0.%x.%04x\n", ret,
1002 cdev->private->dev_id.ssid,
1003 cdev->private->dev_id.devno);
1008 * Now we know this subchannel will stay, we can throw
1009 * our delayed uevent.
1011 dev_set_uevent_suppress(&sch->dev, 0);
1012 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1013 /* make it known to the system */
1014 ret = ccw_device_register(cdev);
1016 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
1017 cdev->private->dev_id.ssid,
1018 cdev->private->dev_id.devno, ret);
1019 spin_lock_irqsave(sch->lock, flags);
1020 sch_set_cdev(sch, NULL);
1021 spin_unlock_irqrestore(sch->lock, flags);
1022 /* Release initial device reference. */
1023 put_device(&cdev->dev);
1027 cdev->private->flags.recog_done = 1;
1028 wake_up(&cdev->private->wait_q);
1030 /* Release reference for workqueue processing. */
1031 put_device(&cdev->dev);
1032 if (atomic_dec_and_test(&ccw_device_init_count))
1033 wake_up(&ccw_device_init_wq);
1036 static void ccw_device_call_sch_unregister(struct work_struct *work)
1038 struct ccw_device_private *priv;
1039 struct ccw_device *cdev;
1040 struct subchannel *sch;
1042 priv = container_of(work, struct ccw_device_private, kick_work);
1044 /* Get subchannel reference for local processing. */
1045 if (!get_device(cdev->dev.parent))
1047 sch = to_subchannel(cdev->dev.parent);
1048 css_sch_device_unregister(sch);
1049 /* Release cdev reference for workqueue processing.*/
1050 put_device(&cdev->dev);
1051 /* Release subchannel reference for local processing. */
1052 put_device(&sch->dev);
1055 void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1057 /* Get cdev reference for workqueue processing. */
1058 if (!get_device(&cdev->dev))
1060 PREPARE_WORK(&cdev->private->kick_work,
1061 ccw_device_call_sch_unregister);
1062 queue_work(slow_path_wq, &cdev->private->kick_work);
1066 * subchannel recognition done. Called from the state machine.
1069 io_subchannel_recog_done(struct ccw_device *cdev)
1071 if (css_init_done == 0) {
1072 cdev->private->flags.recog_done = 1;
1075 switch (cdev->private->state) {
1076 case DEV_STATE_BOXED:
1077 /* Device did not respond in time. */
1078 case DEV_STATE_NOT_OPER:
1079 cdev->private->flags.recog_done = 1;
1080 ccw_device_schedule_sch_unregister(cdev);
1081 if (atomic_dec_and_test(&ccw_device_init_count))
1082 wake_up(&ccw_device_init_wq);
1084 case DEV_STATE_OFFLINE:
1086 * We can't register the device in interrupt context so
1087 * we schedule a work item.
1089 if (!get_device(&cdev->dev))
1091 PREPARE_WORK(&cdev->private->kick_work,
1092 io_subchannel_register);
1093 queue_work(slow_path_wq, &cdev->private->kick_work);
1099 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1102 struct ccw_device_private *priv;
1104 sch_set_cdev(sch, cdev);
1105 cdev->ccwlock = sch->lock;
1107 /* Init private data. */
1108 priv = cdev->private;
1109 priv->dev_id.devno = sch->schib.pmcw.dev;
1110 priv->dev_id.ssid = sch->schid.ssid;
1111 priv->schid = sch->schid;
1112 priv->state = DEV_STATE_NOT_OPER;
1113 INIT_LIST_HEAD(&priv->cmb_list);
1114 init_waitqueue_head(&priv->wait_q);
1115 init_timer(&priv->timer);
1117 /* Increase counter of devices currently in recognition. */
1118 atomic_inc(&ccw_device_init_count);
1120 /* Start async. device sensing. */
1121 spin_lock_irq(sch->lock);
1122 rc = ccw_device_recognition(cdev);
1123 spin_unlock_irq(sch->lock);
1125 if (atomic_dec_and_test(&ccw_device_init_count))
1126 wake_up(&ccw_device_init_wq);
1131 static void ccw_device_move_to_sch(struct work_struct *work)
1133 struct ccw_device_private *priv;
1135 struct subchannel *sch;
1136 struct ccw_device *cdev;
1137 struct subchannel *former_parent;
1139 priv = container_of(work, struct ccw_device_private, kick_work);
1142 former_parent = to_subchannel(cdev->dev.parent);
1143 /* Get reference for new parent. */
1144 if (!get_device(&sch->dev))
1146 mutex_lock(&sch->reg_mutex);
1148 * Try to move the ccw device to its new subchannel.
1149 * Note: device_move() changes cdev->dev.parent
1151 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
1152 mutex_unlock(&sch->reg_mutex);
1154 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1155 "0.%x.%04x failed (ret=%d)!\n",
1156 cdev->private->dev_id.ssid,
1157 cdev->private->dev_id.devno, sch->schid.ssid,
1158 sch->schid.sch_no, rc);
1159 css_sch_device_unregister(sch);
1160 /* Put reference for new parent again. */
1161 put_device(&sch->dev);
1164 if (!sch_is_pseudo_sch(former_parent)) {
1165 spin_lock_irq(former_parent->lock);
1166 sch_set_cdev(former_parent, NULL);
1167 spin_unlock_irq(former_parent->lock);
1168 css_sch_device_unregister(former_parent);
1169 /* Reset intparm to zeroes. */
1170 former_parent->config.intparm = 0;
1171 cio_commit_config(former_parent);
1173 sch_attach_device(sch, cdev);
1175 /* Put reference for old parent. */
1176 put_device(&former_parent->dev);
1177 put_device(&cdev->dev);
1180 static void io_subchannel_irq(struct subchannel *sch)
1182 struct ccw_device *cdev;
1184 cdev = sch_get_cdev(sch);
1186 CIO_TRACE_EVENT(6, "IRQ");
1187 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1189 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1192 void io_subchannel_init_config(struct subchannel *sch)
1194 memset(&sch->config, 0, sizeof(sch->config));
1195 sch->config.csense = 1;
1196 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1197 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1201 static void io_subchannel_init_fields(struct subchannel *sch)
1203 if (cio_is_console(sch->schid))
1206 sch->opm = chp_get_sch_opm(sch);
1207 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1208 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1210 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1211 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1212 sch->schib.pmcw.dev, sch->schid.ssid,
1213 sch->schid.sch_no, sch->schib.pmcw.pim,
1214 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1216 io_subchannel_init_config(sch);
1219 static void io_subchannel_do_unreg(struct work_struct *work)
1221 struct subchannel *sch;
1223 sch = container_of(work, struct subchannel, work);
1224 css_sch_device_unregister(sch);
1225 put_device(&sch->dev);
1228 /* Schedule unregister if we have no cdev. */
1229 static void io_subchannel_schedule_removal(struct subchannel *sch)
1231 get_device(&sch->dev);
1232 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1233 queue_work(slow_path_wq, &sch->work);
1237 * Note: We always return 0 so that we bind to the device even on error.
1238 * This is needed so that our remove function is called on unregister.
1240 static int io_subchannel_probe(struct subchannel *sch)
1242 struct ccw_device *cdev;
1244 unsigned long flags;
1245 struct ccw_dev_id dev_id;
1247 cdev = sch_get_cdev(sch);
1249 rc = sysfs_create_group(&sch->dev.kobj,
1250 &io_subchannel_attr_group);
1252 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1253 "attributes for subchannel "
1254 "0.%x.%04x (rc=%d)\n",
1255 sch->schid.ssid, sch->schid.sch_no, rc);
1257 * This subchannel already has an associated ccw_device.
1258 * Throw the delayed uevent for the subchannel, register
1259 * the ccw_device and exit. This happens for all early
1260 * devices, e.g. the console.
1262 dev_set_uevent_suppress(&sch->dev, 0);
1263 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1264 cdev->dev.groups = ccwdev_attr_groups;
1265 device_initialize(&cdev->dev);
1266 ccw_device_register(cdev);
1268 * Check if the device is already online. If it is
1269 * the reference count needs to be corrected since we
1270 * didn't obtain a reference in ccw_device_set_online.
1272 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1273 cdev->private->state != DEV_STATE_OFFLINE &&
1274 cdev->private->state != DEV_STATE_BOXED)
1275 get_device(&cdev->dev);
1278 io_subchannel_init_fields(sch);
1279 rc = cio_commit_config(sch);
1282 rc = sysfs_create_group(&sch->dev.kobj,
1283 &io_subchannel_attr_group);
1286 /* Allocate I/O subchannel private data. */
1287 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1288 GFP_KERNEL | GFP_DMA);
1292 * First check if a fitting device may be found amongst the
1293 * disconnected devices or in the orphanage.
1295 dev_id.devno = sch->schib.pmcw.dev;
1296 dev_id.ssid = sch->schid.ssid;
1297 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1299 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1303 * Schedule moving the device until when we have a registered
1304 * subchannel to move to and succeed the probe. We can
1305 * unregister later again, when the probe is through.
1307 cdev->private->sch = sch;
1308 PREPARE_WORK(&cdev->private->kick_work,
1309 ccw_device_move_to_sch);
1310 queue_work(slow_path_wq, &cdev->private->kick_work);
1313 cdev = io_subchannel_create_ccwdev(sch);
1316 rc = io_subchannel_recog(cdev, sch);
1318 spin_lock_irqsave(sch->lock, flags);
1319 io_subchannel_recog_done(cdev);
1320 spin_unlock_irqrestore(sch->lock, flags);
1324 kfree(sch->private);
1325 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1327 io_subchannel_schedule_removal(sch);
1332 io_subchannel_remove (struct subchannel *sch)
1334 struct ccw_device *cdev;
1335 unsigned long flags;
1337 cdev = sch_get_cdev(sch);
1340 /* Set ccw device to not operational and drop reference. */
1341 spin_lock_irqsave(cdev->ccwlock, flags);
1342 sch_set_cdev(sch, NULL);
1343 cdev->private->state = DEV_STATE_NOT_OPER;
1344 spin_unlock_irqrestore(cdev->ccwlock, flags);
1345 ccw_device_unregister(cdev);
1346 kfree(sch->private);
1347 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1351 static int io_subchannel_notify(struct subchannel *sch, int event)
1353 struct ccw_device *cdev;
1355 cdev = sch_get_cdev(sch);
1358 return ccw_device_notify(cdev, event);
1361 static void io_subchannel_verify(struct subchannel *sch)
1363 struct ccw_device *cdev;
1365 cdev = sch_get_cdev(sch);
1367 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1370 static int check_for_io_on_path(struct subchannel *sch, int mask)
1372 if (cio_update_schib(sch))
1374 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1379 static void terminate_internal_io(struct subchannel *sch,
1380 struct ccw_device *cdev)
1382 if (cio_clear(sch)) {
1383 /* Recheck device in case clear failed. */
1386 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1388 css_schedule_eval(sch->schid);
1391 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1392 /* Request retry of internal operation. */
1393 cdev->private->flags.intretry = 1;
1396 cdev->handler(cdev, cdev->private->intparm,
1400 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1402 struct ccw_device *cdev;
1404 cdev = sch_get_cdev(sch);
1407 if (check_for_io_on_path(sch, mask)) {
1408 if (cdev->private->state == DEV_STATE_ONLINE)
1409 ccw_device_kill_io(cdev);
1411 terminate_internal_io(sch, cdev);
1412 /* Re-start path verification. */
1413 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1416 /* trigger path verification. */
1417 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1421 static int io_subchannel_chp_event(struct subchannel *sch,
1422 struct chp_link *link, int event)
1426 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1433 io_subchannel_terminate_path(sch, mask);
1438 io_subchannel_verify(sch);
1441 if (cio_update_schib(sch))
1443 io_subchannel_terminate_path(sch, mask);
1446 if (cio_update_schib(sch))
1448 sch->lpm |= mask & sch->opm;
1449 io_subchannel_verify(sch);
1456 io_subchannel_shutdown(struct subchannel *sch)
1458 struct ccw_device *cdev;
1461 cdev = sch_get_cdev(sch);
1463 if (cio_is_console(sch->schid))
1465 if (!sch->schib.pmcw.ena)
1466 /* Nothing to do. */
1468 ret = cio_disable_subchannel(sch);
1470 /* Subchannel is disabled, we're done. */
1472 cdev->private->state = DEV_STATE_QUIESCE;
1474 cdev->handler(cdev, cdev->private->intparm,
1476 ret = ccw_device_cancel_halt_clear(cdev);
1477 if (ret == -EBUSY) {
1478 ccw_device_set_timeout(cdev, HZ/10);
1479 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1481 cio_disable_subchannel(sch);
1484 static int io_subchannel_get_status(struct subchannel *sch)
1488 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1490 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1491 return CIO_REVALIDATE;
1497 static int device_is_disconnected(struct ccw_device *cdev)
1501 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1502 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1505 static int recovery_check(struct device *dev, void *data)
1507 struct ccw_device *cdev = to_ccwdev(dev);
1510 spin_lock_irq(cdev->ccwlock);
1511 switch (cdev->private->state) {
1512 case DEV_STATE_DISCONNECTED:
1513 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1514 cdev->private->dev_id.ssid,
1515 cdev->private->dev_id.devno);
1516 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1519 case DEV_STATE_DISCONNECTED_SENSE_ID:
1523 spin_unlock_irq(cdev->ccwlock);
1528 static void recovery_work_func(struct work_struct *unused)
1532 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1534 spin_lock_irq(&recovery_lock);
1535 if (!timer_pending(&recovery_timer)) {
1536 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1538 mod_timer(&recovery_timer, jiffies +
1539 recovery_delay[recovery_phase] * HZ);
1541 spin_unlock_irq(&recovery_lock);
1543 CIO_MSG_EVENT(4, "recovery: end\n");
1546 static DECLARE_WORK(recovery_work, recovery_work_func);
1548 static void recovery_func(unsigned long data)
1551 * We can't do our recovery in softirq context and it's not
1552 * performance critical, so we schedule it.
1554 schedule_work(&recovery_work);
1557 static void ccw_device_schedule_recovery(void)
1559 unsigned long flags;
1561 CIO_MSG_EVENT(4, "recovery: schedule\n");
1562 spin_lock_irqsave(&recovery_lock, flags);
1563 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1565 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1567 spin_unlock_irqrestore(&recovery_lock, flags);
1570 static int purge_fn(struct device *dev, void *data)
1572 struct ccw_device *cdev = to_ccwdev(dev);
1573 struct ccw_device_private *priv = cdev->private;
1576 spin_lock_irq(cdev->ccwlock);
1577 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1578 (priv->state == DEV_STATE_OFFLINE);
1579 spin_unlock_irq(cdev->ccwlock);
1582 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1583 priv->dev_id.devno);
1584 ccw_device_schedule_sch_unregister(cdev);
1587 /* Abort loop in case of pending signal. */
1588 if (signal_pending(current))
1595 * ccw_purge_blacklisted - purge unused, blacklisted devices
1597 * Unregister all ccw devices that are offline and on the blacklist.
1599 int ccw_purge_blacklisted(void)
1601 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1602 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1606 static void device_set_disconnected(struct ccw_device *cdev)
1610 ccw_device_set_timeout(cdev, 0);
1611 cdev->private->flags.fake_irb = 0;
1612 cdev->private->state = DEV_STATE_DISCONNECTED;
1614 ccw_device_schedule_recovery();
1617 void ccw_device_set_notoper(struct ccw_device *cdev)
1619 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1621 CIO_TRACE_EVENT(2, "notoper");
1622 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1623 ccw_device_set_timeout(cdev, 0);
1624 cio_disable_subchannel(sch);
1625 cdev->private->state = DEV_STATE_NOT_OPER;
1628 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1630 int event, ret, disc;
1631 unsigned long flags;
1632 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1633 struct ccw_device *cdev;
1635 spin_lock_irqsave(sch->lock, flags);
1636 cdev = sch_get_cdev(sch);
1637 disc = device_is_disconnected(cdev);
1639 /* Disconnected devices are evaluated directly only.*/
1640 spin_unlock_irqrestore(sch->lock, flags);
1643 /* No interrupt after machine check - kill pending timers. */
1645 ccw_device_set_timeout(cdev, 0);
1646 if (!disc && !slow) {
1647 /* Non-disconnected devices are evaluated on the slow path. */
1648 spin_unlock_irqrestore(sch->lock, flags);
1651 event = io_subchannel_get_status(sch);
1652 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1653 sch->schid.ssid, sch->schid.sch_no, event,
1654 disc ? "disconnected" : "normal",
1655 slow ? "slow" : "fast");
1656 /* Analyze subchannel status. */
1661 /* Check if paths have become available. */
1667 /* Ask driver what to do with device. */
1668 if (io_subchannel_notify(sch, event))
1671 action = UNREGISTER;
1673 case CIO_REVALIDATE:
1674 /* Device will be removed, so no notify necessary. */
1676 /* Reprobe because immediate unregister might block. */
1679 action = UNREGISTER_PROBE;
1683 /* Get device operational again. */
1687 /* Perform action. */
1691 case UNREGISTER_PROBE:
1692 ccw_device_set_notoper(cdev);
1693 /* Unregister device (will use subchannel lock). */
1694 spin_unlock_irqrestore(sch->lock, flags);
1695 css_sch_device_unregister(sch);
1696 spin_lock_irqsave(sch->lock, flags);
1699 ccw_device_trigger_reprobe(cdev);
1702 device_set_disconnected(cdev);
1707 spin_unlock_irqrestore(sch->lock, flags);
1708 /* Probe if necessary. */
1709 if (action == UNREGISTER_PROBE)
1710 ret = css_probe_device(sch->schid);
1715 #ifdef CONFIG_CCW_CONSOLE
1716 static struct ccw_device console_cdev;
1717 static struct ccw_device_private console_private;
1718 static int console_cdev_in_use;
1720 static DEFINE_SPINLOCK(ccw_console_lock);
1722 spinlock_t * cio_get_console_lock(void)
1724 return &ccw_console_lock;
1727 static int ccw_device_console_enable(struct ccw_device *cdev,
1728 struct subchannel *sch)
1732 /* Attach subchannel private data. */
1733 sch->private = cio_get_console_priv();
1734 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1735 io_subchannel_init_fields(sch);
1736 rc = cio_commit_config(sch);
1739 sch->driver = &io_subchannel_driver;
1740 /* Initialize the ccw_device structure. */
1741 cdev->dev.parent= &sch->dev;
1742 rc = io_subchannel_recog(cdev, sch);
1746 /* Now wait for the async. recognition to come to an end. */
1747 spin_lock_irq(cdev->ccwlock);
1748 while (!dev_fsm_final_state(cdev))
1751 if (cdev->private->state != DEV_STATE_OFFLINE)
1753 ccw_device_online(cdev);
1754 while (!dev_fsm_final_state(cdev))
1756 if (cdev->private->state != DEV_STATE_ONLINE)
1760 spin_unlock_irq(cdev->ccwlock);
1765 ccw_device_probe_console(void)
1767 struct subchannel *sch;
1770 if (xchg(&console_cdev_in_use, 1) != 0)
1771 return ERR_PTR(-EBUSY);
1772 sch = cio_probe_console();
1774 console_cdev_in_use = 0;
1775 return (void *) sch;
1777 memset(&console_cdev, 0, sizeof(struct ccw_device));
1778 memset(&console_private, 0, sizeof(struct ccw_device_private));
1779 console_cdev.private = &console_private;
1780 console_private.cdev = &console_cdev;
1781 ret = ccw_device_console_enable(&console_cdev, sch);
1783 cio_release_console();
1784 console_cdev_in_use = 0;
1785 return ERR_PTR(ret);
1787 console_cdev.online = 1;
1788 return &console_cdev;
1791 static int ccw_device_pm_restore(struct device *dev);
1793 int ccw_device_force_console(void)
1795 if (!console_cdev_in_use)
1797 return ccw_device_pm_restore(&console_cdev.dev);
1799 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1803 * get ccw_device matching the busid, but only if owned by cdrv
1806 __ccwdev_check_busid(struct device *dev, void *id)
1812 return (strcmp(bus_id, dev_name(dev)) == 0);
1817 * get_ccwdev_by_busid() - obtain device from a bus id
1818 * @cdrv: driver the device is owned by
1819 * @bus_id: bus id of the device to be searched
1821 * This function searches all devices owned by @cdrv for a device with a bus
1822 * id matching @bus_id.
1824 * If a match is found, its reference count of the found device is increased
1825 * and it is returned; else %NULL is returned.
1827 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1831 struct device_driver *drv;
1833 drv = get_driver(&cdrv->driver);
1837 dev = driver_find_device(drv, NULL, (void *)bus_id,
1838 __ccwdev_check_busid);
1841 return dev ? to_ccwdev(dev) : NULL;
1844 /************************** device driver handling ************************/
1846 /* This is the implementation of the ccw_driver class. The probe, remove
1847 * and release methods are initially very similar to the device_driver
1848 * implementations, with the difference that they have ccw_device
1851 * A ccw driver also contains the information that is needed for
1855 ccw_device_probe (struct device *dev)
1857 struct ccw_device *cdev = to_ccwdev(dev);
1858 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1861 cdev->drv = cdrv; /* to let the driver call _set_online */
1863 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1874 ccw_device_remove (struct device *dev)
1876 struct ccw_device *cdev = to_ccwdev(dev);
1877 struct ccw_driver *cdrv = cdev->drv;
1884 spin_lock_irq(cdev->ccwlock);
1885 ret = ccw_device_offline(cdev);
1886 spin_unlock_irq(cdev->ccwlock);
1888 wait_event(cdev->private->wait_q,
1889 dev_fsm_final_state(cdev));
1891 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1892 "device 0.%x.%04x\n",
1893 ret, cdev->private->dev_id.ssid,
1894 cdev->private->dev_id.devno);
1895 /* Give up reference obtained in ccw_device_set_online(). */
1896 put_device(&cdev->dev);
1898 ccw_device_set_timeout(cdev, 0);
1903 static void ccw_device_shutdown(struct device *dev)
1905 struct ccw_device *cdev;
1907 cdev = to_ccwdev(dev);
1908 if (cdev->drv && cdev->drv->shutdown)
1909 cdev->drv->shutdown(cdev);
1913 static int ccw_device_pm_prepare(struct device *dev)
1915 struct ccw_device *cdev = to_ccwdev(dev);
1917 if (work_pending(&cdev->private->kick_work))
1919 /* Fail while device is being set online/offline. */
1920 if (atomic_read(&cdev->private->onoff))
1923 if (cdev->online && cdev->drv && cdev->drv->prepare)
1924 return cdev->drv->prepare(cdev);
1929 static void ccw_device_pm_complete(struct device *dev)
1931 struct ccw_device *cdev = to_ccwdev(dev);
1933 if (cdev->online && cdev->drv && cdev->drv->complete)
1934 cdev->drv->complete(cdev);
1937 static int ccw_device_pm_freeze(struct device *dev)
1939 struct ccw_device *cdev = to_ccwdev(dev);
1940 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1941 int ret, cm_enabled;
1943 /* Fail suspend while device is in transistional state. */
1944 if (!dev_fsm_final_state(cdev))
1948 if (cdev->drv && cdev->drv->freeze) {
1949 ret = cdev->drv->freeze(cdev);
1954 spin_lock_irq(sch->lock);
1955 cm_enabled = cdev->private->cmb != NULL;
1956 spin_unlock_irq(sch->lock);
1958 /* Don't have the css write on memory. */
1959 ret = ccw_set_cmf(cdev, 0);
1963 /* From here on, disallow device driver I/O. */
1964 spin_lock_irq(sch->lock);
1965 ret = cio_disable_subchannel(sch);
1966 spin_unlock_irq(sch->lock);
1971 static int ccw_device_pm_thaw(struct device *dev)
1973 struct ccw_device *cdev = to_ccwdev(dev);
1974 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1975 int ret, cm_enabled;
1980 spin_lock_irq(sch->lock);
1981 /* Allow device driver I/O again. */
1982 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1983 cm_enabled = cdev->private->cmb != NULL;
1984 spin_unlock_irq(sch->lock);
1989 ret = ccw_set_cmf(cdev, 1);
1994 if (cdev->drv && cdev->drv->thaw)
1995 ret = cdev->drv->thaw(cdev);
2000 static void __ccw_device_pm_restore(struct ccw_device *cdev)
2002 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2005 if (cio_is_console(sch->schid))
2008 * While we were sleeping, devices may have gone or become
2009 * available again. Kick re-detection.
2011 spin_lock_irq(sch->lock);
2012 cdev->private->flags.resuming = 1;
2013 ret = ccw_device_recognition(cdev);
2014 spin_unlock_irq(sch->lock);
2016 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
2017 "0.%x.%04x (ret=%d)\n",
2018 cdev->private->dev_id.ssid,
2019 cdev->private->dev_id.devno, ret);
2020 spin_lock_irq(sch->lock);
2021 cdev->private->state = DEV_STATE_DISCONNECTED;
2022 spin_unlock_irq(sch->lock);
2023 /* notify driver after the resume cb */
2026 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2027 cdev->private->state == DEV_STATE_DISCONNECTED);
2030 cdev->private->flags.resuming = 0;
2033 static int resume_handle_boxed(struct ccw_device *cdev)
2035 cdev->private->state = DEV_STATE_BOXED;
2036 if (ccw_device_notify(cdev, CIO_BOXED))
2038 ccw_device_schedule_sch_unregister(cdev);
2042 static int resume_handle_disc(struct ccw_device *cdev)
2044 cdev->private->state = DEV_STATE_DISCONNECTED;
2045 if (ccw_device_notify(cdev, CIO_GONE))
2047 ccw_device_schedule_sch_unregister(cdev);
2051 static int ccw_device_pm_restore(struct device *dev)
2053 struct ccw_device *cdev = to_ccwdev(dev);
2054 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2055 int ret = 0, cm_enabled;
2057 __ccw_device_pm_restore(cdev);
2058 spin_lock_irq(sch->lock);
2059 if (cio_is_console(sch->schid)) {
2060 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2061 spin_unlock_irq(sch->lock);
2064 cdev->private->flags.donotify = 0;
2065 /* check recognition results */
2066 switch (cdev->private->state) {
2067 case DEV_STATE_OFFLINE:
2069 case DEV_STATE_BOXED:
2070 ret = resume_handle_boxed(cdev);
2071 spin_unlock_irq(sch->lock);
2075 case DEV_STATE_DISCONNECTED:
2076 goto out_disc_unlock;
2078 goto out_unreg_unlock;
2080 /* check if the device id has changed */
2081 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
2082 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
2083 "changed from %04x to %04x)\n",
2084 sch->schid.ssid, sch->schid.sch_no,
2085 cdev->private->dev_id.devno,
2086 sch->schib.pmcw.dev);
2087 goto out_unreg_unlock;
2089 /* check if the device type has changed */
2090 if (!ccw_device_test_sense_data(cdev)) {
2091 ccw_device_update_sense_data(cdev);
2092 PREPARE_WORK(&cdev->private->kick_work,
2093 ccw_device_do_unbind_bind);
2094 queue_work(ccw_device_work, &cdev->private->kick_work);
2098 if (!cdev->online) {
2102 ret = ccw_device_online(cdev);
2104 goto out_disc_unlock;
2106 cm_enabled = cdev->private->cmb != NULL;
2107 spin_unlock_irq(sch->lock);
2109 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2110 if (cdev->private->state != DEV_STATE_ONLINE) {
2111 spin_lock_irq(sch->lock);
2112 goto out_disc_unlock;
2115 ret = ccw_set_cmf(cdev, 1);
2117 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2118 "(rc=%d)\n", cdev->private->dev_id.ssid,
2119 cdev->private->dev_id.devno, ret);
2125 if (cdev->online && cdev->drv && cdev->drv->restore)
2126 ret = cdev->drv->restore(cdev);
2131 ret = resume_handle_disc(cdev);
2132 spin_unlock_irq(sch->lock);
2138 ccw_device_schedule_sch_unregister(cdev);
2141 spin_unlock_irq(sch->lock);
2145 static struct dev_pm_ops ccw_pm_ops = {
2146 .prepare = ccw_device_pm_prepare,
2147 .complete = ccw_device_pm_complete,
2148 .freeze = ccw_device_pm_freeze,
2149 .thaw = ccw_device_pm_thaw,
2150 .restore = ccw_device_pm_restore,
2153 struct bus_type ccw_bus_type = {
2155 .match = ccw_bus_match,
2156 .uevent = ccw_uevent,
2157 .probe = ccw_device_probe,
2158 .remove = ccw_device_remove,
2159 .shutdown = ccw_device_shutdown,
2164 * ccw_driver_register() - register a ccw driver
2165 * @cdriver: driver to be registered
2167 * This function is mainly a wrapper around driver_register().
2169 * %0 on success and a negative error value on failure.
2171 int ccw_driver_register(struct ccw_driver *cdriver)
2173 struct device_driver *drv = &cdriver->driver;
2175 drv->bus = &ccw_bus_type;
2176 drv->name = cdriver->name;
2177 drv->owner = cdriver->owner;
2179 return driver_register(drv);
2183 * ccw_driver_unregister() - deregister a ccw driver
2184 * @cdriver: driver to be deregistered
2186 * This function is mainly a wrapper around driver_unregister().
2188 void ccw_driver_unregister(struct ccw_driver *cdriver)
2190 driver_unregister(&cdriver->driver);
2193 /* Helper func for qdio. */
2194 struct subchannel_id
2195 ccw_device_get_subchannel_id(struct ccw_device *cdev)
2197 struct subchannel *sch;
2199 sch = to_subchannel(cdev->dev.parent);
2203 MODULE_LICENSE("GPL");
2204 EXPORT_SYMBOL(ccw_device_set_online);
2205 EXPORT_SYMBOL(ccw_device_set_offline);
2206 EXPORT_SYMBOL(ccw_driver_register);
2207 EXPORT_SYMBOL(ccw_driver_unregister);
2208 EXPORT_SYMBOL(get_ccwdev_by_busid);
2209 EXPORT_SYMBOL(ccw_bus_type);
2210 EXPORT_SYMBOL(ccw_device_work);
2211 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);