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>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
26 #include "cio_debug.h"
31 /******************* bus type handling ***********************/
33 /* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
38 ccw_bus_match (struct device * dev, struct device_driver * drv)
40 struct ccw_device *cdev = to_ccwdev(dev);
41 struct ccw_driver *cdrv = to_ccwdrv(drv);
42 const struct ccw_device_id *ids = cdrv->ids, *found;
47 found = ccw_device_id_match(ids, &cdev->id);
51 cdev->id.driver_info = found->driver_info;
56 /* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
59 static int snprint_alias(char *buf, size_t size,
60 struct ccw_device_id *id, const char *suffix)
64 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
70 if (id->dev_type != 0)
71 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
72 id->dev_model, suffix);
74 len += snprintf(buf, size, "dtdm%s", suffix);
79 /* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
83 struct ccw_device *cdev = to_ccwdev(dev);
84 struct ccw_device_id *id = &(cdev->id);
86 char modalias_buf[30];
89 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
94 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
98 /* The next two can be zero, that's ok for us */
100 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
105 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
110 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
111 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
115 struct bus_type ccw_bus_type;
117 static int io_subchannel_probe (struct subchannel *);
118 static int io_subchannel_remove (struct subchannel *);
119 static int io_subchannel_notify(struct device *, int);
120 static void io_subchannel_verify(struct device *);
121 static void io_subchannel_ioterm(struct device *);
122 static void io_subchannel_shutdown(struct subchannel *);
124 static struct css_driver io_subchannel_driver = {
125 .subchannel_type = SUBCHANNEL_TYPE_IO,
127 .name = "io_subchannel",
128 .bus = &css_bus_type,
130 .irq = io_subchannel_irq,
131 .notify = io_subchannel_notify,
132 .verify = io_subchannel_verify,
133 .termination = io_subchannel_ioterm,
134 .probe = io_subchannel_probe,
135 .remove = io_subchannel_remove,
136 .shutdown = io_subchannel_shutdown,
139 struct workqueue_struct *ccw_device_work;
140 struct workqueue_struct *ccw_device_notify_work;
141 wait_queue_head_t ccw_device_init_wq;
142 atomic_t ccw_device_init_count;
145 init_ccw_bus_type (void)
149 init_waitqueue_head(&ccw_device_init_wq);
150 atomic_set(&ccw_device_init_count, 0);
152 ccw_device_work = create_singlethread_workqueue("cio");
153 if (!ccw_device_work)
154 return -ENOMEM; /* FIXME: better errno ? */
155 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
156 if (!ccw_device_notify_work) {
157 ret = -ENOMEM; /* FIXME: better errno ? */
160 slow_path_wq = create_singlethread_workqueue("kslowcrw");
162 ret = -ENOMEM; /* FIXME: better errno ? */
165 if ((ret = bus_register (&ccw_bus_type)))
168 if ((ret = driver_register(&io_subchannel_driver.drv)))
171 wait_event(ccw_device_init_wq,
172 atomic_read(&ccw_device_init_count) == 0);
173 flush_workqueue(ccw_device_work);
177 destroy_workqueue(ccw_device_work);
178 if (ccw_device_notify_work)
179 destroy_workqueue(ccw_device_notify_work);
181 destroy_workqueue(slow_path_wq);
186 cleanup_ccw_bus_type (void)
188 driver_unregister(&io_subchannel_driver.drv);
189 bus_unregister(&ccw_bus_type);
190 destroy_workqueue(ccw_device_notify_work);
191 destroy_workqueue(ccw_device_work);
194 subsys_initcall(init_ccw_bus_type);
195 module_exit(cleanup_ccw_bus_type);
197 /************************ device handling **************************/
200 * A ccw_device has some interfaces in sysfs in addition to the
202 * The following entries are designed to export the information which
203 * resided in 2.4 in /proc/subchannels. Subchannel and device number
204 * are obvious, so they don't have an entry :)
205 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
208 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
210 struct subchannel *sch = to_subchannel(dev);
211 struct chsc_ssd_info *ssd = &sch->ssd_info;
216 for (chp = 0; chp < 8; chp++) {
218 if (ssd->path_mask & mask)
219 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
221 ret += sprintf(buf + ret, "00 ");
223 ret += sprintf (buf+ret, "\n");
224 return min((ssize_t)PAGE_SIZE, ret);
228 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
230 struct subchannel *sch = to_subchannel(dev);
231 struct pmcw *pmcw = &sch->schib.pmcw;
233 return sprintf (buf, "%02x %02x %02x\n",
234 pmcw->pim, pmcw->pam, pmcw->pom);
238 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
240 struct ccw_device *cdev = to_ccwdev(dev);
241 struct ccw_device_id *id = &(cdev->id);
243 if (id->dev_type != 0)
244 return sprintf(buf, "%04x/%02x\n",
245 id->dev_type, id->dev_model);
247 return sprintf(buf, "n/a\n");
251 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
253 struct ccw_device *cdev = to_ccwdev(dev);
254 struct ccw_device_id *id = &(cdev->id);
256 return sprintf(buf, "%04x/%02x\n",
257 id->cu_type, id->cu_model);
261 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
267 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
269 return len > PAGE_SIZE ? PAGE_SIZE : len;
273 online_show (struct device *dev, struct device_attribute *attr, char *buf)
275 struct ccw_device *cdev = to_ccwdev(dev);
277 return sprintf(buf, cdev->online ? "1\n" : "0\n");
280 int ccw_device_is_orphan(struct ccw_device *cdev)
282 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
285 static void ccw_device_unregister(struct ccw_device *cdev)
287 if (test_and_clear_bit(1, &cdev->private->registered))
288 device_del(&cdev->dev);
291 static void ccw_device_remove_orphan_cb(struct device *dev)
293 struct ccw_device *cdev = to_ccwdev(dev);
295 ccw_device_unregister(cdev);
296 put_device(&cdev->dev);
299 static void ccw_device_remove_sch_cb(struct device *dev)
301 struct subchannel *sch;
303 sch = to_subchannel(dev);
304 css_sch_device_unregister(sch);
305 /* Reset intparm to zeroes. */
306 sch->schib.pmcw.intparm = 0;
308 put_device(&sch->dev);
312 ccw_device_remove_disconnected(struct ccw_device *cdev)
318 * Forced offline in disconnected state means
319 * 'throw away device'.
321 if (ccw_device_is_orphan(cdev)) {
323 * Deregister ccw device.
324 * Unfortunately, we cannot do this directly from the
327 spin_lock_irqsave(cdev->ccwlock, flags);
328 cdev->private->state = DEV_STATE_NOT_OPER;
329 spin_unlock_irqrestore(cdev->ccwlock, flags);
330 rc = device_schedule_callback(&cdev->dev,
331 ccw_device_remove_orphan_cb);
333 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
335 cdev->private->dev_id.ssid,
336 cdev->private->dev_id.devno);
339 /* Deregister subchannel, which will kill the ccw device. */
340 rc = device_schedule_callback(cdev->dev.parent,
341 ccw_device_remove_sch_cb);
343 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
345 cdev->private->dev_id.ssid,
346 cdev->private->dev_id.devno);
350 ccw_device_set_offline(struct ccw_device *cdev)
356 if (!cdev->online || !cdev->drv)
359 if (cdev->drv->set_offline) {
360 ret = cdev->drv->set_offline(cdev);
365 spin_lock_irq(cdev->ccwlock);
366 ret = ccw_device_offline(cdev);
367 if (ret == -ENODEV) {
368 if (cdev->private->state != DEV_STATE_NOT_OPER) {
369 cdev->private->state = DEV_STATE_OFFLINE;
370 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
372 spin_unlock_irq(cdev->ccwlock);
375 spin_unlock_irq(cdev->ccwlock);
377 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
379 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
380 "device 0.%x.%04x\n",
381 ret, cdev->private->dev_id.ssid,
382 cdev->private->dev_id.devno);
389 ccw_device_set_online(struct ccw_device *cdev)
395 if (cdev->online || !cdev->drv)
398 spin_lock_irq(cdev->ccwlock);
399 ret = ccw_device_online(cdev);
400 spin_unlock_irq(cdev->ccwlock);
402 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
404 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
405 "device 0.%x.%04x\n",
406 ret, cdev->private->dev_id.ssid,
407 cdev->private->dev_id.devno);
410 if (cdev->private->state != DEV_STATE_ONLINE)
412 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
416 spin_lock_irq(cdev->ccwlock);
417 ret = ccw_device_offline(cdev);
418 spin_unlock_irq(cdev->ccwlock);
420 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
422 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
423 "device 0.%x.%04x\n",
424 ret, cdev->private->dev_id.ssid,
425 cdev->private->dev_id.devno);
426 return (ret == 0) ? -ENODEV : ret;
429 static void online_store_handle_offline(struct ccw_device *cdev)
431 if (cdev->private->state == DEV_STATE_DISCONNECTED)
432 ccw_device_remove_disconnected(cdev);
433 else if (cdev->drv && cdev->drv->set_offline)
434 ccw_device_set_offline(cdev);
437 static int online_store_recog_and_online(struct ccw_device *cdev)
441 /* Do device recognition, if needed. */
442 if (cdev->id.cu_type == 0) {
443 ret = ccw_device_recognition(cdev);
445 CIO_MSG_EVENT(0, "Couldn't start recognition "
446 "for device 0.%x.%04x (ret=%d)\n",
447 cdev->private->dev_id.ssid,
448 cdev->private->dev_id.devno, ret);
451 wait_event(cdev->private->wait_q,
452 cdev->private->flags.recog_done);
454 if (cdev->drv && cdev->drv->set_online)
455 ccw_device_set_online(cdev);
458 static void online_store_handle_online(struct ccw_device *cdev, int force)
462 ret = online_store_recog_and_online(cdev);
465 if (force && cdev->private->state == DEV_STATE_BOXED) {
466 ret = ccw_device_stlck(cdev);
469 "ccw_device_stlck returned %d!\n", ret);
472 if (cdev->id.cu_type == 0)
473 cdev->private->state = DEV_STATE_NOT_OPER;
474 online_store_recog_and_online(cdev);
479 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
480 const char *buf, size_t count)
482 struct ccw_device *cdev = to_ccwdev(dev);
486 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
489 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
490 atomic_set(&cdev->private->onoff, 0);
493 if (!strncmp(buf, "force\n", count)) {
498 i = simple_strtoul(buf, &tmp, 16);
503 online_store_handle_offline(cdev);
506 online_store_handle_online(cdev, force);
512 module_put(cdev->drv->owner);
513 atomic_set(&cdev->private->onoff, 0);
518 available_show (struct device *dev, struct device_attribute *attr, char *buf)
520 struct ccw_device *cdev = to_ccwdev(dev);
521 struct subchannel *sch;
523 if (ccw_device_is_orphan(cdev))
524 return sprintf(buf, "no device\n");
525 switch (cdev->private->state) {
526 case DEV_STATE_BOXED:
527 return sprintf(buf, "boxed\n");
528 case DEV_STATE_DISCONNECTED:
529 case DEV_STATE_DISCONNECTED_SENSE_ID:
530 case DEV_STATE_NOT_OPER:
531 sch = to_subchannel(dev->parent);
533 return sprintf(buf, "no path\n");
535 return sprintf(buf, "no device\n");
537 /* All other states considered fine. */
538 return sprintf(buf, "good\n");
542 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
543 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
544 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
545 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
546 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
547 static DEVICE_ATTR(online, 0644, online_show, online_store);
548 extern struct device_attribute dev_attr_cmb_enable;
549 static DEVICE_ATTR(availability, 0444, available_show, NULL);
551 static struct attribute * subch_attrs[] = {
552 &dev_attr_chpids.attr,
553 &dev_attr_pimpampom.attr,
557 static struct attribute_group subch_attr_group = {
558 .attrs = subch_attrs,
561 struct attribute_group *subch_attr_groups[] = {
566 static struct attribute * ccwdev_attrs[] = {
567 &dev_attr_devtype.attr,
568 &dev_attr_cutype.attr,
569 &dev_attr_modalias.attr,
570 &dev_attr_online.attr,
571 &dev_attr_cmb_enable.attr,
572 &dev_attr_availability.attr,
576 static struct attribute_group ccwdev_attr_group = {
577 .attrs = ccwdev_attrs,
580 static struct attribute_group *ccwdev_attr_groups[] = {
585 /* this is a simple abstraction for device_register that sets the
586 * correct bus type and adds the bus specific files */
587 static int ccw_device_register(struct ccw_device *cdev)
589 struct device *dev = &cdev->dev;
592 dev->bus = &ccw_bus_type;
594 if ((ret = device_add(dev)))
597 set_bit(1, &cdev->private->registered);
602 struct ccw_dev_id dev_id;
603 struct ccw_device * sibling;
607 match_devno(struct device * dev, void * data)
609 struct match_data * d = data;
610 struct ccw_device * cdev;
612 cdev = to_ccwdev(dev);
613 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
614 !ccw_device_is_orphan(cdev) &&
615 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
616 (cdev != d->sibling))
621 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
622 struct ccw_device *sibling)
625 struct match_data data;
627 data.dev_id = *dev_id;
628 data.sibling = sibling;
629 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
631 return dev ? to_ccwdev(dev) : NULL;
634 static int match_orphan(struct device *dev, void *data)
636 struct ccw_dev_id *dev_id;
637 struct ccw_device *cdev;
640 cdev = to_ccwdev(dev);
641 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
644 static struct ccw_device *
645 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
646 struct ccw_dev_id *dev_id)
650 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
653 return dev ? to_ccwdev(dev) : NULL;
657 ccw_device_add_changed(struct work_struct *work)
659 struct ccw_device_private *priv;
660 struct ccw_device *cdev;
662 priv = container_of(work, struct ccw_device_private, kick_work);
664 if (device_add(&cdev->dev)) {
665 put_device(&cdev->dev);
668 set_bit(1, &cdev->private->registered);
671 void ccw_device_do_unreg_rereg(struct work_struct *work)
673 struct ccw_device_private *priv;
674 struct ccw_device *cdev;
675 struct subchannel *sch;
677 priv = container_of(work, struct ccw_device_private, kick_work);
679 sch = to_subchannel(cdev->dev.parent);
681 ccw_device_unregister(cdev);
682 PREPARE_WORK(&cdev->private->kick_work,
683 ccw_device_add_changed);
684 queue_work(ccw_device_work, &cdev->private->kick_work);
688 ccw_device_release(struct device *dev)
690 struct ccw_device *cdev;
692 cdev = to_ccwdev(dev);
693 kfree(cdev->private);
697 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
699 struct ccw_device *cdev;
701 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
703 cdev->private = kzalloc(sizeof(struct ccw_device_private),
704 GFP_KERNEL | GFP_DMA);
709 return ERR_PTR(-ENOMEM);
712 static int io_subchannel_initialize_dev(struct subchannel *sch,
713 struct ccw_device *cdev)
715 cdev->private->cdev = cdev;
716 atomic_set(&cdev->private->onoff, 0);
717 cdev->dev.parent = &sch->dev;
718 cdev->dev.release = ccw_device_release;
719 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
720 cdev->dev.groups = ccwdev_attr_groups;
721 /* Do first half of device_register. */
722 device_initialize(&cdev->dev);
723 if (!get_device(&sch->dev)) {
724 if (cdev->dev.release)
725 cdev->dev.release(&cdev->dev);
731 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
733 struct ccw_device *cdev;
736 cdev = io_subchannel_allocate_dev(sch);
738 ret = io_subchannel_initialize_dev(sch, cdev);
747 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
749 static void sch_attach_device(struct subchannel *sch,
750 struct ccw_device *cdev)
752 css_update_ssd_info(sch);
753 spin_lock_irq(sch->lock);
754 sch->dev.driver_data = cdev;
755 cdev->private->schid = sch->schid;
756 cdev->ccwlock = sch->lock;
757 device_trigger_reprobe(sch);
758 spin_unlock_irq(sch->lock);
761 static void sch_attach_disconnected_device(struct subchannel *sch,
762 struct ccw_device *cdev)
764 struct subchannel *other_sch;
767 other_sch = to_subchannel(get_device(cdev->dev.parent));
768 ret = device_move(&cdev->dev, &sch->dev);
770 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
771 "(ret=%d)!\n", cdev->private->dev_id.ssid,
772 cdev->private->dev_id.devno, ret);
773 put_device(&other_sch->dev);
776 other_sch->dev.driver_data = NULL;
777 /* No need to keep a subchannel without ccw device around. */
778 css_sch_device_unregister(other_sch);
779 put_device(&other_sch->dev);
780 sch_attach_device(sch, cdev);
783 static void sch_attach_orphaned_device(struct subchannel *sch,
784 struct ccw_device *cdev)
788 /* Try to move the ccw device to its new subchannel. */
789 ret = device_move(&cdev->dev, &sch->dev);
791 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
792 "failed (ret=%d)!\n",
793 cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno, ret);
797 sch_attach_device(sch, cdev);
800 static void sch_create_and_recog_new_device(struct subchannel *sch)
802 struct ccw_device *cdev;
804 /* Need to allocate a new ccw device. */
805 cdev = io_subchannel_create_ccwdev(sch);
807 /* OK, we did everything we could... */
808 css_sch_device_unregister(sch);
811 spin_lock_irq(sch->lock);
812 sch->dev.driver_data = cdev;
813 spin_unlock_irq(sch->lock);
814 /* Start recognition for the new ccw device. */
815 if (io_subchannel_recog(cdev, sch)) {
816 spin_lock_irq(sch->lock);
817 sch->dev.driver_data = NULL;
818 spin_unlock_irq(sch->lock);
819 if (cdev->dev.release)
820 cdev->dev.release(&cdev->dev);
821 css_sch_device_unregister(sch);
826 void ccw_device_move_to_orphanage(struct work_struct *work)
828 struct ccw_device_private *priv;
829 struct ccw_device *cdev;
830 struct ccw_device *replacing_cdev;
831 struct subchannel *sch;
833 struct channel_subsystem *css;
834 struct ccw_dev_id dev_id;
836 priv = container_of(work, struct ccw_device_private, kick_work);
838 sch = to_subchannel(cdev->dev.parent);
839 css = to_css(sch->dev.parent);
840 dev_id.devno = sch->schib.pmcw.dev;
841 dev_id.ssid = sch->schid.ssid;
844 * Move the orphaned ccw device to the orphanage so the replacing
845 * ccw device can take its place on the subchannel.
847 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
849 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
850 "(ret=%d)!\n", cdev->private->dev_id.ssid,
851 cdev->private->dev_id.devno, ret);
854 cdev->ccwlock = css->pseudo_subchannel->lock;
856 * Search for the replacing ccw device
857 * - among the disconnected devices
860 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
861 if (replacing_cdev) {
862 sch_attach_disconnected_device(sch, replacing_cdev);
865 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
866 if (replacing_cdev) {
867 sch_attach_orphaned_device(sch, replacing_cdev);
870 sch_create_and_recog_new_device(sch);
874 * Register recognized device.
877 io_subchannel_register(struct work_struct *work)
879 struct ccw_device_private *priv;
880 struct ccw_device *cdev;
881 struct subchannel *sch;
885 priv = container_of(work, struct ccw_device_private, kick_work);
887 sch = to_subchannel(cdev->dev.parent);
888 css_update_ssd_info(sch);
890 * io_subchannel_register() will also be called after device
891 * recognition has been done for a boxed device (which will already
892 * be registered). We need to reprobe since we may now have sense id
895 if (klist_node_attached(&cdev->dev.knode_parent)) {
897 ret = device_reprobe(&cdev->dev);
899 /* We can't do much here. */
900 CIO_MSG_EVENT(2, "device_reprobe() returned"
901 " %d for 0.%x.%04x\n", ret,
902 cdev->private->dev_id.ssid,
903 cdev->private->dev_id.devno);
908 * Now we know this subchannel will stay, we can throw
909 * our delayed uevent.
911 sch->dev.uevent_suppress = 0;
912 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
913 /* make it known to the system */
914 ret = ccw_device_register(cdev);
916 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
917 cdev->private->dev_id.ssid,
918 cdev->private->dev_id.devno, ret);
919 put_device(&cdev->dev);
920 spin_lock_irqsave(sch->lock, flags);
921 sch->dev.driver_data = NULL;
922 spin_unlock_irqrestore(sch->lock, flags);
923 kfree (cdev->private);
925 put_device(&sch->dev);
926 if (atomic_dec_and_test(&ccw_device_init_count))
927 wake_up(&ccw_device_init_wq);
930 put_device(&cdev->dev);
932 cdev->private->flags.recog_done = 1;
933 put_device(&sch->dev);
934 wake_up(&cdev->private->wait_q);
935 if (atomic_dec_and_test(&ccw_device_init_count))
936 wake_up(&ccw_device_init_wq);
940 ccw_device_call_sch_unregister(struct work_struct *work)
942 struct ccw_device_private *priv;
943 struct ccw_device *cdev;
944 struct subchannel *sch;
946 priv = container_of(work, struct ccw_device_private, kick_work);
948 sch = to_subchannel(cdev->dev.parent);
949 css_sch_device_unregister(sch);
950 /* Reset intparm to zeroes. */
951 sch->schib.pmcw.intparm = 0;
953 put_device(&cdev->dev);
954 put_device(&sch->dev);
958 * subchannel recognition done. Called from the state machine.
961 io_subchannel_recog_done(struct ccw_device *cdev)
963 struct subchannel *sch;
965 if (css_init_done == 0) {
966 cdev->private->flags.recog_done = 1;
969 switch (cdev->private->state) {
970 case DEV_STATE_NOT_OPER:
971 cdev->private->flags.recog_done = 1;
972 /* Remove device found not operational. */
973 if (!get_device(&cdev->dev))
975 sch = to_subchannel(cdev->dev.parent);
976 PREPARE_WORK(&cdev->private->kick_work,
977 ccw_device_call_sch_unregister);
978 queue_work(slow_path_wq, &cdev->private->kick_work);
979 if (atomic_dec_and_test(&ccw_device_init_count))
980 wake_up(&ccw_device_init_wq);
982 case DEV_STATE_BOXED:
983 /* Device did not respond in time. */
984 case DEV_STATE_OFFLINE:
986 * We can't register the device in interrupt context so
987 * we schedule a work item.
989 if (!get_device(&cdev->dev))
991 PREPARE_WORK(&cdev->private->kick_work,
992 io_subchannel_register);
993 queue_work(slow_path_wq, &cdev->private->kick_work);
999 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1002 struct ccw_device_private *priv;
1004 sch->dev.driver_data = cdev;
1005 sch->driver = &io_subchannel_driver;
1006 cdev->ccwlock = sch->lock;
1008 /* Init private data. */
1009 priv = cdev->private;
1010 priv->dev_id.devno = sch->schib.pmcw.dev;
1011 priv->dev_id.ssid = sch->schid.ssid;
1012 priv->schid = sch->schid;
1013 priv->state = DEV_STATE_NOT_OPER;
1014 INIT_LIST_HEAD(&priv->cmb_list);
1015 init_waitqueue_head(&priv->wait_q);
1016 init_timer(&priv->timer);
1018 /* Set an initial name for the device. */
1019 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1020 sch->schid.ssid, sch->schib.pmcw.dev);
1022 /* Increase counter of devices currently in recognition. */
1023 atomic_inc(&ccw_device_init_count);
1025 /* Start async. device sensing. */
1026 spin_lock_irq(sch->lock);
1027 rc = ccw_device_recognition(cdev);
1028 spin_unlock_irq(sch->lock);
1030 if (atomic_dec_and_test(&ccw_device_init_count))
1031 wake_up(&ccw_device_init_wq);
1036 static void ccw_device_move_to_sch(struct work_struct *work)
1038 struct ccw_device_private *priv;
1040 struct subchannel *sch;
1041 struct ccw_device *cdev;
1042 struct subchannel *former_parent;
1044 priv = container_of(work, struct ccw_device_private, kick_work);
1047 former_parent = ccw_device_is_orphan(cdev) ?
1048 NULL : to_subchannel(get_device(cdev->dev.parent));
1049 mutex_lock(&sch->reg_mutex);
1050 /* Try to move the ccw device to its new subchannel. */
1051 rc = device_move(&cdev->dev, &sch->dev);
1052 mutex_unlock(&sch->reg_mutex);
1054 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1055 "0.%x.%04x failed (ret=%d)!\n",
1056 cdev->private->dev_id.ssid,
1057 cdev->private->dev_id.devno, sch->schid.ssid,
1058 sch->schid.sch_no, rc);
1059 css_sch_device_unregister(sch);
1062 if (former_parent) {
1063 spin_lock_irq(former_parent->lock);
1064 former_parent->dev.driver_data = NULL;
1065 spin_unlock_irq(former_parent->lock);
1066 css_sch_device_unregister(former_parent);
1067 /* Reset intparm to zeroes. */
1068 former_parent->schib.pmcw.intparm = 0;
1069 cio_modify(former_parent);
1071 sch_attach_device(sch, cdev);
1074 put_device(&former_parent->dev);
1075 put_device(&cdev->dev);
1079 io_subchannel_probe (struct subchannel *sch)
1081 struct ccw_device *cdev;
1083 unsigned long flags;
1084 struct ccw_dev_id dev_id;
1086 if (sch->dev.driver_data) {
1088 * This subchannel already has an associated ccw_device.
1089 * Register it and exit. This happens for all early
1090 * device, e.g. the console.
1092 cdev = sch->dev.driver_data;
1093 device_initialize(&cdev->dev);
1094 ccw_device_register(cdev);
1096 * Check if the device is already online. If it is
1097 * the reference count needs to be corrected
1098 * (see ccw_device_online and css_init_done for the
1101 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1102 cdev->private->state != DEV_STATE_OFFLINE &&
1103 cdev->private->state != DEV_STATE_BOXED)
1104 get_device(&cdev->dev);
1108 * First check if a fitting device may be found amongst the
1109 * disconnected devices or in the orphanage.
1111 dev_id.devno = sch->schib.pmcw.dev;
1112 dev_id.ssid = sch->schid.ssid;
1113 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1115 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1119 * Schedule moving the device until when we have a registered
1120 * subchannel to move to and succeed the probe. We can
1121 * unregister later again, when the probe is through.
1123 cdev->private->sch = sch;
1124 PREPARE_WORK(&cdev->private->kick_work,
1125 ccw_device_move_to_sch);
1126 queue_work(slow_path_wq, &cdev->private->kick_work);
1129 cdev = io_subchannel_create_ccwdev(sch);
1131 return PTR_ERR(cdev);
1133 rc = io_subchannel_recog(cdev, sch);
1135 spin_lock_irqsave(sch->lock, flags);
1136 sch->dev.driver_data = NULL;
1137 spin_unlock_irqrestore(sch->lock, flags);
1138 if (cdev->dev.release)
1139 cdev->dev.release(&cdev->dev);
1146 io_subchannel_remove (struct subchannel *sch)
1148 struct ccw_device *cdev;
1149 unsigned long flags;
1151 if (!sch->dev.driver_data)
1153 cdev = sch->dev.driver_data;
1154 /* Set ccw device to not operational and drop reference. */
1155 spin_lock_irqsave(cdev->ccwlock, flags);
1156 sch->dev.driver_data = NULL;
1157 cdev->private->state = DEV_STATE_NOT_OPER;
1158 spin_unlock_irqrestore(cdev->ccwlock, flags);
1159 ccw_device_unregister(cdev);
1160 put_device(&cdev->dev);
1165 io_subchannel_notify(struct device *dev, int event)
1167 struct ccw_device *cdev;
1169 cdev = dev->driver_data;
1176 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1180 io_subchannel_verify(struct device *dev)
1182 struct ccw_device *cdev;
1184 cdev = dev->driver_data;
1186 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1190 io_subchannel_ioterm(struct device *dev)
1192 struct ccw_device *cdev;
1194 cdev = dev->driver_data;
1197 /* Internal I/O will be retried by the interrupt handler. */
1198 if (cdev->private->flags.intretry)
1200 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1202 cdev->handler(cdev, cdev->private->intparm,
1207 io_subchannel_shutdown(struct subchannel *sch)
1209 struct ccw_device *cdev;
1212 cdev = sch->dev.driver_data;
1214 if (cio_is_console(sch->schid))
1216 if (!sch->schib.pmcw.ena)
1217 /* Nothing to do. */
1219 ret = cio_disable_subchannel(sch);
1221 /* Subchannel is disabled, we're done. */
1223 cdev->private->state = DEV_STATE_QUIESCE;
1225 cdev->handler(cdev, cdev->private->intparm,
1227 ret = ccw_device_cancel_halt_clear(cdev);
1228 if (ret == -EBUSY) {
1229 ccw_device_set_timeout(cdev, HZ/10);
1230 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1232 cio_disable_subchannel(sch);
1235 #ifdef CONFIG_CCW_CONSOLE
1236 static struct ccw_device console_cdev;
1237 static struct ccw_device_private console_private;
1238 static int console_cdev_in_use;
1240 static DEFINE_SPINLOCK(ccw_console_lock);
1242 spinlock_t * cio_get_console_lock(void)
1244 return &ccw_console_lock;
1248 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1252 /* Initialize the ccw_device structure. */
1253 cdev->dev.parent= &sch->dev;
1254 rc = io_subchannel_recog(cdev, sch);
1258 /* Now wait for the async. recognition to come to an end. */
1259 spin_lock_irq(cdev->ccwlock);
1260 while (!dev_fsm_final_state(cdev))
1263 if (cdev->private->state != DEV_STATE_OFFLINE)
1265 ccw_device_online(cdev);
1266 while (!dev_fsm_final_state(cdev))
1268 if (cdev->private->state != DEV_STATE_ONLINE)
1272 spin_unlock_irq(cdev->ccwlock);
1277 ccw_device_probe_console(void)
1279 struct subchannel *sch;
1282 if (xchg(&console_cdev_in_use, 1) != 0)
1283 return ERR_PTR(-EBUSY);
1284 sch = cio_probe_console();
1286 console_cdev_in_use = 0;
1287 return (void *) sch;
1289 memset(&console_cdev, 0, sizeof(struct ccw_device));
1290 memset(&console_private, 0, sizeof(struct ccw_device_private));
1291 console_cdev.private = &console_private;
1292 console_private.cdev = &console_cdev;
1293 ret = ccw_device_console_enable(&console_cdev, sch);
1295 cio_release_console();
1296 console_cdev_in_use = 0;
1297 return ERR_PTR(ret);
1299 console_cdev.online = 1;
1300 return &console_cdev;
1305 * get ccw_device matching the busid, but only if owned by cdrv
1308 __ccwdev_check_busid(struct device *dev, void *id)
1314 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1319 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1322 struct device_driver *drv;
1324 drv = get_driver(&cdrv->driver);
1328 dev = driver_find_device(drv, NULL, (void *)bus_id,
1329 __ccwdev_check_busid);
1332 return dev ? to_ccwdev(dev) : NULL;
1335 /************************** device driver handling ************************/
1337 /* This is the implementation of the ccw_driver class. The probe, remove
1338 * and release methods are initially very similar to the device_driver
1339 * implementations, with the difference that they have ccw_device
1342 * A ccw driver also contains the information that is needed for
1346 ccw_device_probe (struct device *dev)
1348 struct ccw_device *cdev = to_ccwdev(dev);
1349 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1352 cdev->drv = cdrv; /* to let the driver call _set_online */
1354 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1365 ccw_device_remove (struct device *dev)
1367 struct ccw_device *cdev = to_ccwdev(dev);
1368 struct ccw_driver *cdrv = cdev->drv;
1375 spin_lock_irq(cdev->ccwlock);
1376 ret = ccw_device_offline(cdev);
1377 spin_unlock_irq(cdev->ccwlock);
1379 wait_event(cdev->private->wait_q,
1380 dev_fsm_final_state(cdev));
1382 //FIXME: we can't fail!
1383 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1384 "device 0.%x.%04x\n",
1385 ret, cdev->private->dev_id.ssid,
1386 cdev->private->dev_id.devno);
1388 ccw_device_set_timeout(cdev, 0);
1393 struct bus_type ccw_bus_type = {
1395 .match = ccw_bus_match,
1396 .uevent = ccw_uevent,
1397 .probe = ccw_device_probe,
1398 .remove = ccw_device_remove,
1402 ccw_driver_register (struct ccw_driver *cdriver)
1404 struct device_driver *drv = &cdriver->driver;
1406 drv->bus = &ccw_bus_type;
1407 drv->name = cdriver->name;
1409 return driver_register(drv);
1413 ccw_driver_unregister (struct ccw_driver *cdriver)
1415 driver_unregister(&cdriver->driver);
1418 /* Helper func for qdio. */
1419 struct subchannel_id
1420 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1422 struct subchannel *sch;
1424 sch = to_subchannel(cdev->dev.parent);
1428 MODULE_LICENSE("GPL");
1429 EXPORT_SYMBOL(ccw_device_set_online);
1430 EXPORT_SYMBOL(ccw_device_set_offline);
1431 EXPORT_SYMBOL(ccw_driver_register);
1432 EXPORT_SYMBOL(ccw_driver_unregister);
1433 EXPORT_SYMBOL(get_ccwdev_by_busid);
1434 EXPORT_SYMBOL(ccw_bus_type);
1435 EXPORT_SYMBOL(ccw_device_work);
1436 EXPORT_SYMBOL(ccw_device_notify_work);
1437 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);