2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20 #include <linux/timer.h>
22 #include <asm/ccwdev.h>
24 #include <asm/param.h> /* HZ */
29 #include "cio_debug.h"
35 static struct timer_list recovery_timer;
36 static DEFINE_SPINLOCK(recovery_lock);
37 static int recovery_phase;
38 static const unsigned long recovery_delay[] = { 3, 30, 300 };
40 /******************* bus type handling ***********************/
42 /* The Linux driver model distinguishes between a bus type and
43 * the bus itself. Of course we only have one channel
44 * subsystem driver and one channel system per machine, but
45 * we still use the abstraction. T.R. says it's a good idea. */
47 ccw_bus_match (struct device * dev, struct device_driver * drv)
49 struct ccw_device *cdev = to_ccwdev(dev);
50 struct ccw_driver *cdrv = to_ccwdrv(drv);
51 const struct ccw_device_id *ids = cdrv->ids, *found;
56 found = ccw_device_id_match(ids, &cdev->id);
60 cdev->id.driver_info = found->driver_info;
65 /* Store modalias string delimited by prefix/suffix string into buffer with
66 * specified size. Return length of resulting string (excluding trailing '\0')
67 * even if string doesn't fit buffer (snprintf semantics). */
68 static int snprint_alias(char *buf, size_t size,
69 struct ccw_device_id *id, const char *suffix)
73 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
79 if (id->dev_type != 0)
80 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
81 id->dev_model, suffix);
83 len += snprintf(buf, size, "dtdm%s", suffix);
88 /* Set up environment variables for ccw device uevent. Return 0 on success,
89 * non-zero otherwise. */
90 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
92 struct ccw_device *cdev = to_ccwdev(dev);
93 struct ccw_device_id *id = &(cdev->id);
95 char modalias_buf[30];
98 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
103 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
107 /* The next two can be zero, that's ok for us */
109 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
114 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
119 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
120 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
124 struct bus_type ccw_bus_type;
126 static void io_subchannel_irq(struct subchannel *);
127 static int io_subchannel_probe(struct subchannel *);
128 static int io_subchannel_remove(struct subchannel *);
129 static int io_subchannel_notify(struct subchannel *, int);
130 static void io_subchannel_verify(struct subchannel *);
131 static void io_subchannel_ioterm(struct subchannel *);
132 static void io_subchannel_shutdown(struct subchannel *);
134 static struct css_driver io_subchannel_driver = {
135 .owner = THIS_MODULE,
136 .subchannel_type = SUBCHANNEL_TYPE_IO,
137 .name = "io_subchannel",
138 .irq = io_subchannel_irq,
139 .notify = io_subchannel_notify,
140 .verify = io_subchannel_verify,
141 .termination = io_subchannel_ioterm,
142 .probe = io_subchannel_probe,
143 .remove = io_subchannel_remove,
144 .shutdown = io_subchannel_shutdown,
147 struct workqueue_struct *ccw_device_work;
148 struct workqueue_struct *ccw_device_notify_work;
149 wait_queue_head_t ccw_device_init_wq;
150 atomic_t ccw_device_init_count;
152 static void recovery_func(unsigned long data);
155 init_ccw_bus_type (void)
159 init_waitqueue_head(&ccw_device_init_wq);
160 atomic_set(&ccw_device_init_count, 0);
161 setup_timer(&recovery_timer, recovery_func, 0);
163 ccw_device_work = create_singlethread_workqueue("cio");
164 if (!ccw_device_work)
165 return -ENOMEM; /* FIXME: better errno ? */
166 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
167 if (!ccw_device_notify_work) {
168 ret = -ENOMEM; /* FIXME: better errno ? */
171 slow_path_wq = create_singlethread_workqueue("kslowcrw");
173 ret = -ENOMEM; /* FIXME: better errno ? */
176 if ((ret = bus_register (&ccw_bus_type)))
179 ret = css_driver_register(&io_subchannel_driver);
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
189 destroy_workqueue(ccw_device_work);
190 if (ccw_device_notify_work)
191 destroy_workqueue(ccw_device_notify_work);
193 destroy_workqueue(slow_path_wq);
198 cleanup_ccw_bus_type (void)
200 css_driver_unregister(&io_subchannel_driver);
201 bus_unregister(&ccw_bus_type);
202 destroy_workqueue(ccw_device_notify_work);
203 destroy_workqueue(ccw_device_work);
206 subsys_initcall(init_ccw_bus_type);
207 module_exit(cleanup_ccw_bus_type);
209 /************************ device handling **************************/
212 * A ccw_device has some interfaces in sysfs in addition to the
214 * The following entries are designed to export the information which
215 * resided in 2.4 in /proc/subchannels. Subchannel and device number
216 * are obvious, so they don't have an entry :)
217 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
220 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
222 struct subchannel *sch = to_subchannel(dev);
223 struct chsc_ssd_info *ssd = &sch->ssd_info;
228 for (chp = 0; chp < 8; chp++) {
230 if (ssd->path_mask & mask)
231 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
233 ret += sprintf(buf + ret, "00 ");
235 ret += sprintf (buf+ret, "\n");
236 return min((ssize_t)PAGE_SIZE, ret);
240 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
242 struct subchannel *sch = to_subchannel(dev);
243 struct pmcw *pmcw = &sch->schib.pmcw;
245 return sprintf (buf, "%02x %02x %02x\n",
246 pmcw->pim, pmcw->pam, pmcw->pom);
250 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
252 struct ccw_device *cdev = to_ccwdev(dev);
253 struct ccw_device_id *id = &(cdev->id);
255 if (id->dev_type != 0)
256 return sprintf(buf, "%04x/%02x\n",
257 id->dev_type, id->dev_model);
259 return sprintf(buf, "n/a\n");
263 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
268 return sprintf(buf, "%04x/%02x\n",
269 id->cu_type, id->cu_model);
273 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
275 struct ccw_device *cdev = to_ccwdev(dev);
276 struct ccw_device_id *id = &(cdev->id);
279 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
281 return len > PAGE_SIZE ? PAGE_SIZE : len;
285 online_show (struct device *dev, struct device_attribute *attr, char *buf)
287 struct ccw_device *cdev = to_ccwdev(dev);
289 return sprintf(buf, cdev->online ? "1\n" : "0\n");
292 int ccw_device_is_orphan(struct ccw_device *cdev)
294 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
297 static void ccw_device_unregister(struct ccw_device *cdev)
299 if (test_and_clear_bit(1, &cdev->private->registered))
300 device_del(&cdev->dev);
303 static void ccw_device_remove_orphan_cb(struct device *dev)
305 struct ccw_device *cdev = to_ccwdev(dev);
307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
311 static void ccw_device_remove_sch_cb(struct device *dev)
313 struct subchannel *sch;
315 sch = to_subchannel(dev);
316 css_sch_device_unregister(sch);
317 /* Reset intparm to zeroes. */
318 sch->schib.pmcw.intparm = 0;
320 put_device(&sch->dev);
324 ccw_device_remove_disconnected(struct ccw_device *cdev)
330 * Forced offline in disconnected state means
331 * 'throw away device'.
333 if (ccw_device_is_orphan(cdev)) {
335 * Deregister ccw device.
336 * Unfortunately, we cannot do this directly from the
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
342 rc = device_schedule_callback(&cdev->dev,
343 ccw_device_remove_orphan_cb);
345 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
347 cdev->private->dev_id.ssid,
348 cdev->private->dev_id.devno);
351 /* Deregister subchannel, which will kill the ccw device. */
352 rc = device_schedule_callback(cdev->dev.parent,
353 ccw_device_remove_sch_cb);
355 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
357 cdev->private->dev_id.ssid,
358 cdev->private->dev_id.devno);
362 * ccw_device_set_offline() - disable a ccw device for I/O
363 * @cdev: target ccw device
365 * This function calls the driver's set_offline() function for @cdev, if
366 * given, and then disables @cdev.
368 * %0 on success and a negative error value on failure.
370 * enabled, ccw device lock not held
372 int ccw_device_set_offline(struct ccw_device *cdev)
378 if (!cdev->online || !cdev->drv)
381 if (cdev->drv->set_offline) {
382 ret = cdev->drv->set_offline(cdev);
387 spin_lock_irq(cdev->ccwlock);
388 ret = ccw_device_offline(cdev);
389 if (ret == -ENODEV) {
390 if (cdev->private->state != DEV_STATE_NOT_OPER) {
391 cdev->private->state = DEV_STATE_OFFLINE;
392 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
394 spin_unlock_irq(cdev->ccwlock);
397 spin_unlock_irq(cdev->ccwlock);
399 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
401 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
402 "device 0.%x.%04x\n",
403 ret, cdev->private->dev_id.ssid,
404 cdev->private->dev_id.devno);
411 * ccw_device_set_online() - enable a ccw device for I/O
412 * @cdev: target ccw device
414 * This function first enables @cdev and then calls the driver's set_online()
415 * function for @cdev, if given. If set_online() returns an error, @cdev is
418 * %0 on success and a negative error value on failure.
420 * enabled, ccw device lock not held
422 int ccw_device_set_online(struct ccw_device *cdev)
428 if (cdev->online || !cdev->drv)
431 spin_lock_irq(cdev->ccwlock);
432 ret = ccw_device_online(cdev);
433 spin_unlock_irq(cdev->ccwlock);
435 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
437 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
438 "device 0.%x.%04x\n",
439 ret, cdev->private->dev_id.ssid,
440 cdev->private->dev_id.devno);
443 if (cdev->private->state != DEV_STATE_ONLINE)
445 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
449 spin_lock_irq(cdev->ccwlock);
450 ret = ccw_device_offline(cdev);
451 spin_unlock_irq(cdev->ccwlock);
453 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
455 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
456 "device 0.%x.%04x\n",
457 ret, cdev->private->dev_id.ssid,
458 cdev->private->dev_id.devno);
459 return (ret == 0) ? -ENODEV : ret;
462 static void online_store_handle_offline(struct ccw_device *cdev)
464 if (cdev->private->state == DEV_STATE_DISCONNECTED)
465 ccw_device_remove_disconnected(cdev);
466 else if (cdev->drv && cdev->drv->set_offline)
467 ccw_device_set_offline(cdev);
470 static int online_store_recog_and_online(struct ccw_device *cdev)
474 /* Do device recognition, if needed. */
475 if (cdev->id.cu_type == 0) {
476 ret = ccw_device_recognition(cdev);
478 CIO_MSG_EVENT(0, "Couldn't start recognition "
479 "for device 0.%x.%04x (ret=%d)\n",
480 cdev->private->dev_id.ssid,
481 cdev->private->dev_id.devno, ret);
484 wait_event(cdev->private->wait_q,
485 cdev->private->flags.recog_done);
487 if (cdev->drv && cdev->drv->set_online)
488 ccw_device_set_online(cdev);
491 static void online_store_handle_online(struct ccw_device *cdev, int force)
495 ret = online_store_recog_and_online(cdev);
498 if (force && cdev->private->state == DEV_STATE_BOXED) {
499 ret = ccw_device_stlck(cdev);
502 "ccw_device_stlck returned %d!\n", ret);
505 if (cdev->id.cu_type == 0)
506 cdev->private->state = DEV_STATE_NOT_OPER;
507 online_store_recog_and_online(cdev);
512 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
513 const char *buf, size_t count)
515 struct ccw_device *cdev = to_ccwdev(dev);
519 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
522 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
523 atomic_set(&cdev->private->onoff, 0);
526 if (!strncmp(buf, "force\n", count)) {
532 ret = strict_strtoul(buf, 16, &i);
538 online_store_handle_offline(cdev);
542 online_store_handle_online(cdev, force);
550 module_put(cdev->drv->owner);
551 atomic_set(&cdev->private->onoff, 0);
556 available_show (struct device *dev, struct device_attribute *attr, char *buf)
558 struct ccw_device *cdev = to_ccwdev(dev);
559 struct subchannel *sch;
561 if (ccw_device_is_orphan(cdev))
562 return sprintf(buf, "no device\n");
563 switch (cdev->private->state) {
564 case DEV_STATE_BOXED:
565 return sprintf(buf, "boxed\n");
566 case DEV_STATE_DISCONNECTED:
567 case DEV_STATE_DISCONNECTED_SENSE_ID:
568 case DEV_STATE_NOT_OPER:
569 sch = to_subchannel(dev->parent);
571 return sprintf(buf, "no path\n");
573 return sprintf(buf, "no device\n");
575 /* All other states considered fine. */
576 return sprintf(buf, "good\n");
580 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
581 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
582 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
583 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
584 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
585 static DEVICE_ATTR(online, 0644, online_show, online_store);
586 static DEVICE_ATTR(availability, 0444, available_show, NULL);
588 static struct attribute *io_subchannel_attrs[] = {
589 &dev_attr_chpids.attr,
590 &dev_attr_pimpampom.attr,
594 static struct attribute_group io_subchannel_attr_group = {
595 .attrs = io_subchannel_attrs,
598 static struct attribute * ccwdev_attrs[] = {
599 &dev_attr_devtype.attr,
600 &dev_attr_cutype.attr,
601 &dev_attr_modalias.attr,
602 &dev_attr_online.attr,
603 &dev_attr_cmb_enable.attr,
604 &dev_attr_availability.attr,
608 static struct attribute_group ccwdev_attr_group = {
609 .attrs = ccwdev_attrs,
612 static struct attribute_group *ccwdev_attr_groups[] = {
617 /* this is a simple abstraction for device_register that sets the
618 * correct bus type and adds the bus specific files */
619 static int ccw_device_register(struct ccw_device *cdev)
621 struct device *dev = &cdev->dev;
624 dev->bus = &ccw_bus_type;
626 if ((ret = device_add(dev)))
629 set_bit(1, &cdev->private->registered);
634 struct ccw_dev_id dev_id;
635 struct ccw_device * sibling;
639 match_devno(struct device * dev, void * data)
641 struct match_data * d = data;
642 struct ccw_device * cdev;
644 cdev = to_ccwdev(dev);
645 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
646 !ccw_device_is_orphan(cdev) &&
647 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
648 (cdev != d->sibling))
653 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
654 struct ccw_device *sibling)
657 struct match_data data;
659 data.dev_id = *dev_id;
660 data.sibling = sibling;
661 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
663 return dev ? to_ccwdev(dev) : NULL;
666 static int match_orphan(struct device *dev, void *data)
668 struct ccw_dev_id *dev_id;
669 struct ccw_device *cdev;
672 cdev = to_ccwdev(dev);
673 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
676 static struct ccw_device *
677 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
678 struct ccw_dev_id *dev_id)
682 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
685 return dev ? to_ccwdev(dev) : NULL;
689 ccw_device_add_changed(struct work_struct *work)
691 struct ccw_device_private *priv;
692 struct ccw_device *cdev;
694 priv = container_of(work, struct ccw_device_private, kick_work);
696 if (device_add(&cdev->dev)) {
697 put_device(&cdev->dev);
700 set_bit(1, &cdev->private->registered);
703 void ccw_device_do_unreg_rereg(struct work_struct *work)
705 struct ccw_device_private *priv;
706 struct ccw_device *cdev;
707 struct subchannel *sch;
709 priv = container_of(work, struct ccw_device_private, kick_work);
711 sch = to_subchannel(cdev->dev.parent);
713 ccw_device_unregister(cdev);
714 PREPARE_WORK(&cdev->private->kick_work,
715 ccw_device_add_changed);
716 queue_work(ccw_device_work, &cdev->private->kick_work);
720 ccw_device_release(struct device *dev)
722 struct ccw_device *cdev;
724 cdev = to_ccwdev(dev);
725 kfree(cdev->private);
729 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
731 struct ccw_device *cdev;
733 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
735 cdev->private = kzalloc(sizeof(struct ccw_device_private),
736 GFP_KERNEL | GFP_DMA);
741 return ERR_PTR(-ENOMEM);
744 static int io_subchannel_initialize_dev(struct subchannel *sch,
745 struct ccw_device *cdev)
747 cdev->private->cdev = cdev;
748 atomic_set(&cdev->private->onoff, 0);
749 cdev->dev.parent = &sch->dev;
750 cdev->dev.release = ccw_device_release;
751 INIT_WORK(&cdev->private->kick_work, NULL);
752 cdev->dev.groups = ccwdev_attr_groups;
753 /* Do first half of device_register. */
754 device_initialize(&cdev->dev);
755 if (!get_device(&sch->dev)) {
756 if (cdev->dev.release)
757 cdev->dev.release(&cdev->dev);
763 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
765 struct ccw_device *cdev;
768 cdev = io_subchannel_allocate_dev(sch);
770 ret = io_subchannel_initialize_dev(sch, cdev);
779 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
781 static void sch_attach_device(struct subchannel *sch,
782 struct ccw_device *cdev)
784 css_update_ssd_info(sch);
785 spin_lock_irq(sch->lock);
786 sch_set_cdev(sch, cdev);
787 cdev->private->schid = sch->schid;
788 cdev->ccwlock = sch->lock;
789 device_trigger_reprobe(sch);
790 spin_unlock_irq(sch->lock);
793 static void sch_attach_disconnected_device(struct subchannel *sch,
794 struct ccw_device *cdev)
796 struct subchannel *other_sch;
799 other_sch = to_subchannel(get_device(cdev->dev.parent));
800 ret = device_move(&cdev->dev, &sch->dev);
802 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
803 "(ret=%d)!\n", cdev->private->dev_id.ssid,
804 cdev->private->dev_id.devno, ret);
805 put_device(&other_sch->dev);
808 sch_set_cdev(other_sch, NULL);
809 /* No need to keep a subchannel without ccw device around. */
810 css_sch_device_unregister(other_sch);
811 put_device(&other_sch->dev);
812 sch_attach_device(sch, cdev);
815 static void sch_attach_orphaned_device(struct subchannel *sch,
816 struct ccw_device *cdev)
820 /* Try to move the ccw device to its new subchannel. */
821 ret = device_move(&cdev->dev, &sch->dev);
823 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
824 "failed (ret=%d)!\n",
825 cdev->private->dev_id.ssid,
826 cdev->private->dev_id.devno, ret);
829 sch_attach_device(sch, cdev);
832 static void sch_create_and_recog_new_device(struct subchannel *sch)
834 struct ccw_device *cdev;
836 /* Need to allocate a new ccw device. */
837 cdev = io_subchannel_create_ccwdev(sch);
839 /* OK, we did everything we could... */
840 css_sch_device_unregister(sch);
843 spin_lock_irq(sch->lock);
844 sch_set_cdev(sch, cdev);
845 spin_unlock_irq(sch->lock);
846 /* Start recognition for the new ccw device. */
847 if (io_subchannel_recog(cdev, sch)) {
848 spin_lock_irq(sch->lock);
849 sch_set_cdev(sch, NULL);
850 spin_unlock_irq(sch->lock);
851 if (cdev->dev.release)
852 cdev->dev.release(&cdev->dev);
853 css_sch_device_unregister(sch);
858 void ccw_device_move_to_orphanage(struct work_struct *work)
860 struct ccw_device_private *priv;
861 struct ccw_device *cdev;
862 struct ccw_device *replacing_cdev;
863 struct subchannel *sch;
865 struct channel_subsystem *css;
866 struct ccw_dev_id dev_id;
868 priv = container_of(work, struct ccw_device_private, kick_work);
870 sch = to_subchannel(cdev->dev.parent);
871 css = to_css(sch->dev.parent);
872 dev_id.devno = sch->schib.pmcw.dev;
873 dev_id.ssid = sch->schid.ssid;
876 * Move the orphaned ccw device to the orphanage so the replacing
877 * ccw device can take its place on the subchannel.
879 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
881 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
882 "(ret=%d)!\n", cdev->private->dev_id.ssid,
883 cdev->private->dev_id.devno, ret);
886 cdev->ccwlock = css->pseudo_subchannel->lock;
888 * Search for the replacing ccw device
889 * - among the disconnected devices
892 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
893 if (replacing_cdev) {
894 sch_attach_disconnected_device(sch, replacing_cdev);
897 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
898 if (replacing_cdev) {
899 sch_attach_orphaned_device(sch, replacing_cdev);
902 sch_create_and_recog_new_device(sch);
906 * Register recognized device.
909 io_subchannel_register(struct work_struct *work)
911 struct ccw_device_private *priv;
912 struct ccw_device *cdev;
913 struct subchannel *sch;
917 priv = container_of(work, struct ccw_device_private, kick_work);
919 sch = to_subchannel(cdev->dev.parent);
920 css_update_ssd_info(sch);
922 * io_subchannel_register() will also be called after device
923 * recognition has been done for a boxed device (which will already
924 * be registered). We need to reprobe since we may now have sense id
927 if (klist_node_attached(&cdev->dev.knode_parent)) {
929 ret = device_reprobe(&cdev->dev);
931 /* We can't do much here. */
932 CIO_MSG_EVENT(0, "device_reprobe() returned"
933 " %d for 0.%x.%04x\n", ret,
934 cdev->private->dev_id.ssid,
935 cdev->private->dev_id.devno);
940 * Now we know this subchannel will stay, we can throw
941 * our delayed uevent.
943 sch->dev.uevent_suppress = 0;
944 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
945 /* make it known to the system */
946 ret = ccw_device_register(cdev);
948 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
949 cdev->private->dev_id.ssid,
950 cdev->private->dev_id.devno, ret);
951 put_device(&cdev->dev);
952 spin_lock_irqsave(sch->lock, flags);
953 sch_set_cdev(sch, NULL);
954 spin_unlock_irqrestore(sch->lock, flags);
955 kfree (cdev->private);
957 put_device(&sch->dev);
958 if (atomic_dec_and_test(&ccw_device_init_count))
959 wake_up(&ccw_device_init_wq);
962 put_device(&cdev->dev);
964 cdev->private->flags.recog_done = 1;
965 put_device(&sch->dev);
966 wake_up(&cdev->private->wait_q);
967 if (atomic_dec_and_test(&ccw_device_init_count))
968 wake_up(&ccw_device_init_wq);
971 static void ccw_device_call_sch_unregister(struct work_struct *work)
973 struct ccw_device_private *priv;
974 struct ccw_device *cdev;
975 struct subchannel *sch;
977 priv = container_of(work, struct ccw_device_private, kick_work);
979 sch = to_subchannel(cdev->dev.parent);
980 css_sch_device_unregister(sch);
981 /* Reset intparm to zeroes. */
982 sch->schib.pmcw.intparm = 0;
984 put_device(&cdev->dev);
985 put_device(&sch->dev);
989 * subchannel recognition done. Called from the state machine.
992 io_subchannel_recog_done(struct ccw_device *cdev)
994 struct subchannel *sch;
996 if (css_init_done == 0) {
997 cdev->private->flags.recog_done = 1;
1000 switch (cdev->private->state) {
1001 case DEV_STATE_NOT_OPER:
1002 cdev->private->flags.recog_done = 1;
1003 /* Remove device found not operational. */
1004 if (!get_device(&cdev->dev))
1006 sch = to_subchannel(cdev->dev.parent);
1007 PREPARE_WORK(&cdev->private->kick_work,
1008 ccw_device_call_sch_unregister);
1009 queue_work(slow_path_wq, &cdev->private->kick_work);
1010 if (atomic_dec_and_test(&ccw_device_init_count))
1011 wake_up(&ccw_device_init_wq);
1013 case DEV_STATE_BOXED:
1014 /* Device did not respond in time. */
1015 case DEV_STATE_OFFLINE:
1017 * We can't register the device in interrupt context so
1018 * we schedule a work item.
1020 if (!get_device(&cdev->dev))
1022 PREPARE_WORK(&cdev->private->kick_work,
1023 io_subchannel_register);
1024 queue_work(slow_path_wq, &cdev->private->kick_work);
1030 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1033 struct ccw_device_private *priv;
1035 sch_set_cdev(sch, cdev);
1036 cdev->ccwlock = sch->lock;
1038 /* Init private data. */
1039 priv = cdev->private;
1040 priv->dev_id.devno = sch->schib.pmcw.dev;
1041 priv->dev_id.ssid = sch->schid.ssid;
1042 priv->schid = sch->schid;
1043 priv->state = DEV_STATE_NOT_OPER;
1044 INIT_LIST_HEAD(&priv->cmb_list);
1045 init_waitqueue_head(&priv->wait_q);
1046 init_timer(&priv->timer);
1048 /* Set an initial name for the device. */
1049 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1050 sch->schid.ssid, sch->schib.pmcw.dev);
1052 /* Increase counter of devices currently in recognition. */
1053 atomic_inc(&ccw_device_init_count);
1055 /* Start async. device sensing. */
1056 spin_lock_irq(sch->lock);
1057 rc = ccw_device_recognition(cdev);
1058 spin_unlock_irq(sch->lock);
1060 if (atomic_dec_and_test(&ccw_device_init_count))
1061 wake_up(&ccw_device_init_wq);
1066 static void ccw_device_move_to_sch(struct work_struct *work)
1068 struct ccw_device_private *priv;
1070 struct subchannel *sch;
1071 struct ccw_device *cdev;
1072 struct subchannel *former_parent;
1074 priv = container_of(work, struct ccw_device_private, kick_work);
1077 former_parent = ccw_device_is_orphan(cdev) ?
1078 NULL : to_subchannel(get_device(cdev->dev.parent));
1079 mutex_lock(&sch->reg_mutex);
1080 /* Try to move the ccw device to its new subchannel. */
1081 rc = device_move(&cdev->dev, &sch->dev);
1082 mutex_unlock(&sch->reg_mutex);
1084 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1085 "0.%x.%04x failed (ret=%d)!\n",
1086 cdev->private->dev_id.ssid,
1087 cdev->private->dev_id.devno, sch->schid.ssid,
1088 sch->schid.sch_no, rc);
1089 css_sch_device_unregister(sch);
1092 if (former_parent) {
1093 spin_lock_irq(former_parent->lock);
1094 sch_set_cdev(former_parent, NULL);
1095 spin_unlock_irq(former_parent->lock);
1096 css_sch_device_unregister(former_parent);
1097 /* Reset intparm to zeroes. */
1098 former_parent->schib.pmcw.intparm = 0;
1099 cio_modify(former_parent);
1101 sch_attach_device(sch, cdev);
1104 put_device(&former_parent->dev);
1105 put_device(&cdev->dev);
1108 static void io_subchannel_irq(struct subchannel *sch)
1110 struct ccw_device *cdev;
1112 cdev = sch_get_cdev(sch);
1114 CIO_TRACE_EVENT(3, "IRQ");
1115 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1117 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1120 static void io_subchannel_init_fields(struct subchannel *sch)
1122 if (cio_is_console(sch->schid))
1125 sch->opm = chp_get_sch_opm(sch);
1126 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1127 sch->isc = cio_is_console(sch->schid) ? 1 : 3;
1129 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1130 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1131 sch->schib.pmcw.dev, sch->schid.ssid,
1132 sch->schid.sch_no, sch->schib.pmcw.pim,
1133 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1134 /* Initially set up some fields in the pmcw. */
1135 sch->schib.pmcw.ena = 0;
1136 sch->schib.pmcw.csense = 1; /* concurrent sense */
1137 if ((sch->lpm & (sch->lpm - 1)) != 0)
1138 sch->schib.pmcw.mp = 1; /* multipath mode */
1139 /* clean up possible residual cmf stuff */
1140 sch->schib.pmcw.mme = 0;
1141 sch->schib.pmcw.mbfc = 0;
1142 sch->schib.pmcw.mbi = 0;
1146 static int io_subchannel_probe(struct subchannel *sch)
1148 struct ccw_device *cdev;
1150 unsigned long flags;
1151 struct ccw_dev_id dev_id;
1153 cdev = sch_get_cdev(sch);
1155 rc = sysfs_create_group(&sch->dev.kobj,
1156 &io_subchannel_attr_group);
1158 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1159 "attributes for subchannel "
1160 "0.%x.%04x (rc=%d)\n",
1161 sch->schid.ssid, sch->schid.sch_no, rc);
1163 * This subchannel already has an associated ccw_device.
1164 * Throw the delayed uevent for the subchannel, register
1165 * the ccw_device and exit. This happens for all early
1166 * devices, e.g. the console.
1168 sch->dev.uevent_suppress = 0;
1169 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1170 cdev->dev.groups = ccwdev_attr_groups;
1171 device_initialize(&cdev->dev);
1172 ccw_device_register(cdev);
1174 * Check if the device is already online. If it is
1175 * the reference count needs to be corrected
1176 * (see ccw_device_online and css_init_done for the
1179 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1180 cdev->private->state != DEV_STATE_OFFLINE &&
1181 cdev->private->state != DEV_STATE_BOXED)
1182 get_device(&cdev->dev);
1185 io_subchannel_init_fields(sch);
1187 * First check if a fitting device may be found amongst the
1188 * disconnected devices or in the orphanage.
1190 dev_id.devno = sch->schib.pmcw.dev;
1191 dev_id.ssid = sch->schid.ssid;
1192 rc = sysfs_create_group(&sch->dev.kobj,
1193 &io_subchannel_attr_group);
1196 /* Allocate I/O subchannel private data. */
1197 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1198 GFP_KERNEL | GFP_DMA);
1199 if (!sch->private) {
1203 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1205 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1209 * Schedule moving the device until when we have a registered
1210 * subchannel to move to and succeed the probe. We can
1211 * unregister later again, when the probe is through.
1213 cdev->private->sch = sch;
1214 PREPARE_WORK(&cdev->private->kick_work,
1215 ccw_device_move_to_sch);
1216 queue_work(slow_path_wq, &cdev->private->kick_work);
1219 cdev = io_subchannel_create_ccwdev(sch);
1224 rc = io_subchannel_recog(cdev, sch);
1226 spin_lock_irqsave(sch->lock, flags);
1227 sch_set_cdev(sch, NULL);
1228 spin_unlock_irqrestore(sch->lock, flags);
1229 if (cdev->dev.release)
1230 cdev->dev.release(&cdev->dev);
1235 kfree(sch->private);
1236 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1241 io_subchannel_remove (struct subchannel *sch)
1243 struct ccw_device *cdev;
1244 unsigned long flags;
1246 cdev = sch_get_cdev(sch);
1249 /* Set ccw device to not operational and drop reference. */
1250 spin_lock_irqsave(cdev->ccwlock, flags);
1251 sch_set_cdev(sch, NULL);
1252 cdev->private->state = DEV_STATE_NOT_OPER;
1253 spin_unlock_irqrestore(cdev->ccwlock, flags);
1254 ccw_device_unregister(cdev);
1255 put_device(&cdev->dev);
1256 kfree(sch->private);
1257 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1261 static int io_subchannel_notify(struct subchannel *sch, int event)
1263 struct ccw_device *cdev;
1265 cdev = sch_get_cdev(sch);
1272 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1275 static void io_subchannel_verify(struct subchannel *sch)
1277 struct ccw_device *cdev;
1279 cdev = sch_get_cdev(sch);
1281 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1284 static void io_subchannel_ioterm(struct subchannel *sch)
1286 struct ccw_device *cdev;
1288 cdev = sch_get_cdev(sch);
1291 /* Internal I/O will be retried by the interrupt handler. */
1292 if (cdev->private->flags.intretry)
1294 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1296 cdev->handler(cdev, cdev->private->intparm,
1301 io_subchannel_shutdown(struct subchannel *sch)
1303 struct ccw_device *cdev;
1306 cdev = sch_get_cdev(sch);
1308 if (cio_is_console(sch->schid))
1310 if (!sch->schib.pmcw.ena)
1311 /* Nothing to do. */
1313 ret = cio_disable_subchannel(sch);
1315 /* Subchannel is disabled, we're done. */
1317 cdev->private->state = DEV_STATE_QUIESCE;
1319 cdev->handler(cdev, cdev->private->intparm,
1321 ret = ccw_device_cancel_halt_clear(cdev);
1322 if (ret == -EBUSY) {
1323 ccw_device_set_timeout(cdev, HZ/10);
1324 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1326 cio_disable_subchannel(sch);
1329 #ifdef CONFIG_CCW_CONSOLE
1330 static struct ccw_device console_cdev;
1331 static struct ccw_device_private console_private;
1332 static int console_cdev_in_use;
1334 static DEFINE_SPINLOCK(ccw_console_lock);
1336 spinlock_t * cio_get_console_lock(void)
1338 return &ccw_console_lock;
1341 static int ccw_device_console_enable(struct ccw_device *cdev,
1342 struct subchannel *sch)
1346 /* Attach subchannel private data. */
1347 sch->private = cio_get_console_priv();
1348 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1349 io_subchannel_init_fields(sch);
1350 sch->driver = &io_subchannel_driver;
1351 /* Initialize the ccw_device structure. */
1352 cdev->dev.parent= &sch->dev;
1353 rc = io_subchannel_recog(cdev, sch);
1357 /* Now wait for the async. recognition to come to an end. */
1358 spin_lock_irq(cdev->ccwlock);
1359 while (!dev_fsm_final_state(cdev))
1362 if (cdev->private->state != DEV_STATE_OFFLINE)
1364 ccw_device_online(cdev);
1365 while (!dev_fsm_final_state(cdev))
1367 if (cdev->private->state != DEV_STATE_ONLINE)
1371 spin_unlock_irq(cdev->ccwlock);
1376 ccw_device_probe_console(void)
1378 struct subchannel *sch;
1381 if (xchg(&console_cdev_in_use, 1) != 0)
1382 return ERR_PTR(-EBUSY);
1383 sch = cio_probe_console();
1385 console_cdev_in_use = 0;
1386 return (void *) sch;
1388 memset(&console_cdev, 0, sizeof(struct ccw_device));
1389 memset(&console_private, 0, sizeof(struct ccw_device_private));
1390 console_cdev.private = &console_private;
1391 console_private.cdev = &console_cdev;
1392 ret = ccw_device_console_enable(&console_cdev, sch);
1394 cio_release_console();
1395 console_cdev_in_use = 0;
1396 return ERR_PTR(ret);
1398 console_cdev.online = 1;
1399 return &console_cdev;
1404 * get ccw_device matching the busid, but only if owned by cdrv
1407 __ccwdev_check_busid(struct device *dev, void *id)
1413 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1418 * get_ccwdev_by_busid() - obtain device from a bus id
1419 * @cdrv: driver the device is owned by
1420 * @bus_id: bus id of the device to be searched
1422 * This function searches all devices owned by @cdrv for a device with a bus
1423 * id matching @bus_id.
1425 * If a match is found, its reference count of the found device is increased
1426 * and it is returned; else %NULL is returned.
1428 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1432 struct device_driver *drv;
1434 drv = get_driver(&cdrv->driver);
1438 dev = driver_find_device(drv, NULL, (void *)bus_id,
1439 __ccwdev_check_busid);
1442 return dev ? to_ccwdev(dev) : NULL;
1445 /************************** device driver handling ************************/
1447 /* This is the implementation of the ccw_driver class. The probe, remove
1448 * and release methods are initially very similar to the device_driver
1449 * implementations, with the difference that they have ccw_device
1452 * A ccw driver also contains the information that is needed for
1456 ccw_device_probe (struct device *dev)
1458 struct ccw_device *cdev = to_ccwdev(dev);
1459 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1462 cdev->drv = cdrv; /* to let the driver call _set_online */
1464 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1475 ccw_device_remove (struct device *dev)
1477 struct ccw_device *cdev = to_ccwdev(dev);
1478 struct ccw_driver *cdrv = cdev->drv;
1485 spin_lock_irq(cdev->ccwlock);
1486 ret = ccw_device_offline(cdev);
1487 spin_unlock_irq(cdev->ccwlock);
1489 wait_event(cdev->private->wait_q,
1490 dev_fsm_final_state(cdev));
1492 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1493 "device 0.%x.%04x\n",
1494 ret, cdev->private->dev_id.ssid,
1495 cdev->private->dev_id.devno);
1497 ccw_device_set_timeout(cdev, 0);
1502 static void ccw_device_shutdown(struct device *dev)
1504 struct ccw_device *cdev;
1506 cdev = to_ccwdev(dev);
1507 if (cdev->drv && cdev->drv->shutdown)
1508 cdev->drv->shutdown(cdev);
1512 struct bus_type ccw_bus_type = {
1514 .match = ccw_bus_match,
1515 .uevent = ccw_uevent,
1516 .probe = ccw_device_probe,
1517 .remove = ccw_device_remove,
1518 .shutdown = ccw_device_shutdown,
1522 * ccw_driver_register() - register a ccw driver
1523 * @cdriver: driver to be registered
1525 * This function is mainly a wrapper around driver_register().
1527 * %0 on success and a negative error value on failure.
1529 int ccw_driver_register(struct ccw_driver *cdriver)
1531 struct device_driver *drv = &cdriver->driver;
1533 drv->bus = &ccw_bus_type;
1534 drv->name = cdriver->name;
1535 drv->owner = cdriver->owner;
1537 return driver_register(drv);
1541 * ccw_driver_unregister() - deregister a ccw driver
1542 * @cdriver: driver to be deregistered
1544 * This function is mainly a wrapper around driver_unregister().
1546 void ccw_driver_unregister(struct ccw_driver *cdriver)
1548 driver_unregister(&cdriver->driver);
1551 /* Helper func for qdio. */
1552 struct subchannel_id
1553 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1555 struct subchannel *sch;
1557 sch = to_subchannel(cdev->dev.parent);
1561 static int recovery_check(struct device *dev, void *data)
1563 struct ccw_device *cdev = to_ccwdev(dev);
1566 spin_lock_irq(cdev->ccwlock);
1567 switch (cdev->private->state) {
1568 case DEV_STATE_DISCONNECTED:
1569 CIO_MSG_EVENT(4, "recovery: trigger 0.%x.%04x\n",
1570 cdev->private->dev_id.ssid,
1571 cdev->private->dev_id.devno);
1572 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1575 case DEV_STATE_DISCONNECTED_SENSE_ID:
1579 spin_unlock_irq(cdev->ccwlock);
1584 static void recovery_work_func(struct work_struct *unused)
1588 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1590 spin_lock_irq(&recovery_lock);
1591 if (!timer_pending(&recovery_timer)) {
1592 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1594 mod_timer(&recovery_timer, jiffies +
1595 recovery_delay[recovery_phase] * HZ);
1597 spin_unlock_irq(&recovery_lock);
1599 CIO_MSG_EVENT(4, "recovery: end\n");
1602 static DECLARE_WORK(recovery_work, recovery_work_func);
1604 static void recovery_func(unsigned long data)
1607 * We can't do our recovery in softirq context and it's not
1608 * performance critical, so we schedule it.
1610 schedule_work(&recovery_work);
1613 void ccw_device_schedule_recovery(void)
1615 unsigned long flags;
1617 CIO_MSG_EVENT(4, "recovery: schedule\n");
1618 spin_lock_irqsave(&recovery_lock, flags);
1619 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1621 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1623 spin_unlock_irqrestore(&recovery_lock, flags);
1626 MODULE_LICENSE("GPL");
1627 EXPORT_SYMBOL(ccw_device_set_online);
1628 EXPORT_SYMBOL(ccw_device_set_offline);
1629 EXPORT_SYMBOL(ccw_driver_register);
1630 EXPORT_SYMBOL(ccw_driver_unregister);
1631 EXPORT_SYMBOL(get_ccwdev_by_busid);
1632 EXPORT_SYMBOL(ccw_bus_type);
1633 EXPORT_SYMBOL(ccw_device_work);
1634 EXPORT_SYMBOL(ccw_device_notify_work);
1635 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);