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, const char *prefix,
60 struct ccw_device_id *id, const char *suffix)
64 len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
71 if (id->dev_type != 0)
72 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
73 id->dev_model, suffix);
75 len += snprintf(buf, size, "dtdm%s", suffix);
80 /* Set up environment variables for ccw device uevent. Return 0 on success,
81 * non-zero otherwise. */
82 static int ccw_uevent(struct device *dev, char **envp, int num_envp,
83 char *buffer, int buffer_size)
85 struct ccw_device *cdev = to_ccwdev(dev);
86 struct ccw_device_id *id = &(cdev->id);
91 len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
92 if (len > buffer_size || i >= num_envp)
99 len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
100 if (len > buffer_size || i >= num_envp)
106 /* The next two can be zero, that's ok for us */
108 len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
109 if (len > buffer_size || i >= num_envp)
116 len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
117 (unsigned char) id->dev_model) + 1;
118 if (len > buffer_size || i >= num_envp)
125 len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
126 if (len > buffer_size || i >= num_envp)
137 struct bus_type ccw_bus_type;
139 static int io_subchannel_probe (struct subchannel *);
140 static int io_subchannel_remove (struct subchannel *);
141 void io_subchannel_irq (struct device *);
142 static int io_subchannel_notify(struct device *, int);
143 static void io_subchannel_verify(struct device *);
144 static void io_subchannel_ioterm(struct device *);
145 static void io_subchannel_shutdown(struct subchannel *);
147 struct css_driver io_subchannel_driver = {
148 .subchannel_type = SUBCHANNEL_TYPE_IO,
150 .name = "io_subchannel",
151 .bus = &css_bus_type,
153 .irq = io_subchannel_irq,
154 .notify = io_subchannel_notify,
155 .verify = io_subchannel_verify,
156 .termination = io_subchannel_ioterm,
157 .probe = io_subchannel_probe,
158 .remove = io_subchannel_remove,
159 .shutdown = io_subchannel_shutdown,
162 struct workqueue_struct *ccw_device_work;
163 struct workqueue_struct *ccw_device_notify_work;
164 wait_queue_head_t ccw_device_init_wq;
165 atomic_t ccw_device_init_count;
168 init_ccw_bus_type (void)
172 init_waitqueue_head(&ccw_device_init_wq);
173 atomic_set(&ccw_device_init_count, 0);
175 ccw_device_work = create_singlethread_workqueue("cio");
176 if (!ccw_device_work)
177 return -ENOMEM; /* FIXME: better errno ? */
178 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
179 if (!ccw_device_notify_work) {
180 ret = -ENOMEM; /* FIXME: better errno ? */
183 slow_path_wq = create_singlethread_workqueue("kslowcrw");
185 ret = -ENOMEM; /* FIXME: better errno ? */
188 if ((ret = bus_register (&ccw_bus_type)))
191 if ((ret = driver_register(&io_subchannel_driver.drv)))
194 wait_event(ccw_device_init_wq,
195 atomic_read(&ccw_device_init_count) == 0);
196 flush_workqueue(ccw_device_work);
200 destroy_workqueue(ccw_device_work);
201 if (ccw_device_notify_work)
202 destroy_workqueue(ccw_device_notify_work);
204 destroy_workqueue(slow_path_wq);
209 cleanup_ccw_bus_type (void)
211 driver_unregister(&io_subchannel_driver.drv);
212 bus_unregister(&ccw_bus_type);
213 destroy_workqueue(ccw_device_notify_work);
214 destroy_workqueue(ccw_device_work);
217 subsys_initcall(init_ccw_bus_type);
218 module_exit(cleanup_ccw_bus_type);
220 /************************ device handling **************************/
223 * A ccw_device has some interfaces in sysfs in addition to the
225 * The following entries are designed to export the information which
226 * resided in 2.4 in /proc/subchannels. Subchannel and device number
227 * are obvious, so they don't have an entry :)
228 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
231 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
233 struct subchannel *sch = to_subchannel(dev);
234 struct ssd_info *ssd = &sch->ssd_info;
239 for (chp = 0; chp < 8; chp++)
240 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
242 ret += sprintf (buf, "n/a");
243 ret += sprintf (buf+ret, "\n");
244 return min((ssize_t)PAGE_SIZE, ret);
248 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
250 struct subchannel *sch = to_subchannel(dev);
251 struct pmcw *pmcw = &sch->schib.pmcw;
253 return sprintf (buf, "%02x %02x %02x\n",
254 pmcw->pim, pmcw->pam, pmcw->pom);
258 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
260 struct ccw_device *cdev = to_ccwdev(dev);
261 struct ccw_device_id *id = &(cdev->id);
263 if (id->dev_type != 0)
264 return sprintf(buf, "%04x/%02x\n",
265 id->dev_type, id->dev_model);
267 return sprintf(buf, "n/a\n");
271 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
276 return sprintf(buf, "%04x/%02x\n",
277 id->cu_type, id->cu_model);
281 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
283 struct ccw_device *cdev = to_ccwdev(dev);
284 struct ccw_device_id *id = &(cdev->id);
287 len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
289 return len > PAGE_SIZE ? PAGE_SIZE : len;
293 online_show (struct device *dev, struct device_attribute *attr, char *buf)
295 struct ccw_device *cdev = to_ccwdev(dev);
297 return sprintf(buf, cdev->online ? "1\n" : "0\n");
300 int ccw_device_is_orphan(struct ccw_device *cdev)
302 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
305 static void ccw_device_unregister(struct work_struct *work)
307 struct ccw_device_private *priv;
308 struct ccw_device *cdev;
310 priv = container_of(work, struct ccw_device_private, kick_work);
312 if (test_and_clear_bit(1, &cdev->private->registered))
313 device_unregister(&cdev->dev);
314 put_device(&cdev->dev);
318 ccw_device_remove_disconnected(struct ccw_device *cdev)
320 struct subchannel *sch;
323 * Forced offline in disconnected state means
324 * 'throw away device'.
326 if (ccw_device_is_orphan(cdev)) {
327 /* Deregister ccw device. */
328 spin_lock_irqsave(cdev->ccwlock, flags);
329 cdev->private->state = DEV_STATE_NOT_OPER;
330 spin_unlock_irqrestore(cdev->ccwlock, flags);
331 if (get_device(&cdev->dev)) {
332 PREPARE_WORK(&cdev->private->kick_work,
333 ccw_device_unregister);
334 queue_work(ccw_device_work, &cdev->private->kick_work);
338 sch = to_subchannel(cdev->dev.parent);
339 css_sch_device_unregister(sch);
340 /* Reset intparm to zeroes. */
341 sch->schib.pmcw.intparm = 0;
343 put_device(&sch->dev);
347 ccw_device_set_offline(struct ccw_device *cdev)
353 if (!cdev->online || !cdev->drv)
356 if (cdev->drv->set_offline) {
357 ret = cdev->drv->set_offline(cdev);
362 spin_lock_irq(cdev->ccwlock);
363 ret = ccw_device_offline(cdev);
364 if (ret == -ENODEV) {
365 if (cdev->private->state != DEV_STATE_NOT_OPER) {
366 cdev->private->state = DEV_STATE_OFFLINE;
367 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
369 spin_unlock_irq(cdev->ccwlock);
372 spin_unlock_irq(cdev->ccwlock);
374 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
376 pr_debug("ccw_device_offline returned %d, device %s\n",
377 ret, cdev->dev.bus_id);
384 ccw_device_set_online(struct ccw_device *cdev)
390 if (cdev->online || !cdev->drv)
393 spin_lock_irq(cdev->ccwlock);
394 ret = ccw_device_online(cdev);
395 spin_unlock_irq(cdev->ccwlock);
397 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
399 pr_debug("ccw_device_online returned %d, device %s\n",
400 ret, cdev->dev.bus_id);
403 if (cdev->private->state != DEV_STATE_ONLINE)
405 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
409 spin_lock_irq(cdev->ccwlock);
410 ret = ccw_device_offline(cdev);
411 spin_unlock_irq(cdev->ccwlock);
413 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
415 pr_debug("ccw_device_offline returned %d, device %s\n",
416 ret, cdev->dev.bus_id);
417 return (ret == 0) ? -ENODEV : ret;
421 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
423 struct ccw_device *cdev = to_ccwdev(dev);
427 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
430 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
431 atomic_set(&cdev->private->onoff, 0);
434 if (!strncmp(buf, "force\n", count)) {
439 i = simple_strtoul(buf, &tmp, 16);
442 /* Do device recognition, if needed. */
443 if (cdev->id.cu_type == 0) {
444 ret = ccw_device_recognition(cdev);
446 printk(KERN_WARNING"Couldn't start recognition "
447 "for device %s (ret=%d)\n",
448 cdev->dev.bus_id, 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);
457 if (cdev->private->state == DEV_STATE_DISCONNECTED)
458 ccw_device_remove_disconnected(cdev);
459 else if (cdev->drv && cdev->drv->set_offline)
460 ccw_device_set_offline(cdev);
462 if (force && cdev->private->state == DEV_STATE_BOXED) {
463 ret = ccw_device_stlck(cdev);
465 printk(KERN_WARNING"ccw_device_stlck for device %s "
466 "returned %d!\n", cdev->dev.bus_id, ret);
469 /* Do device recognition, if needed. */
470 if (cdev->id.cu_type == 0) {
471 cdev->private->state = DEV_STATE_NOT_OPER;
472 ret = ccw_device_recognition(cdev);
474 printk(KERN_WARNING"Couldn't start recognition "
475 "for device %s (ret=%d)\n",
476 cdev->dev.bus_id, ret);
479 wait_event(cdev->private->wait_q,
480 cdev->private->flags.recog_done);
482 if (cdev->drv && cdev->drv->set_online)
483 ccw_device_set_online(cdev);
487 module_put(cdev->drv->owner);
488 atomic_set(&cdev->private->onoff, 0);
493 available_show (struct device *dev, struct device_attribute *attr, char *buf)
495 struct ccw_device *cdev = to_ccwdev(dev);
496 struct subchannel *sch;
498 if (ccw_device_is_orphan(cdev))
499 return sprintf(buf, "no device\n");
500 switch (cdev->private->state) {
501 case DEV_STATE_BOXED:
502 return sprintf(buf, "boxed\n");
503 case DEV_STATE_DISCONNECTED:
504 case DEV_STATE_DISCONNECTED_SENSE_ID:
505 case DEV_STATE_NOT_OPER:
506 sch = to_subchannel(dev->parent);
508 return sprintf(buf, "no path\n");
510 return sprintf(buf, "no device\n");
512 /* All other states considered fine. */
513 return sprintf(buf, "good\n");
517 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
518 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
519 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
520 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
521 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
522 static DEVICE_ATTR(online, 0644, online_show, online_store);
523 extern struct device_attribute dev_attr_cmb_enable;
524 static DEVICE_ATTR(availability, 0444, available_show, NULL);
526 static struct attribute * subch_attrs[] = {
527 &dev_attr_chpids.attr,
528 &dev_attr_pimpampom.attr,
532 static struct attribute_group subch_attr_group = {
533 .attrs = subch_attrs,
536 struct attribute_group *subch_attr_groups[] = {
541 static struct attribute * ccwdev_attrs[] = {
542 &dev_attr_devtype.attr,
543 &dev_attr_cutype.attr,
544 &dev_attr_modalias.attr,
545 &dev_attr_online.attr,
546 &dev_attr_cmb_enable.attr,
547 &dev_attr_availability.attr,
551 static struct attribute_group ccwdev_attr_group = {
552 .attrs = ccwdev_attrs,
556 device_add_files (struct device *dev)
558 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
562 device_remove_files(struct device *dev)
564 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
567 /* this is a simple abstraction for device_register that sets the
568 * correct bus type and adds the bus specific files */
569 static int ccw_device_register(struct ccw_device *cdev)
571 struct device *dev = &cdev->dev;
574 dev->bus = &ccw_bus_type;
576 if ((ret = device_add(dev)))
579 set_bit(1, &cdev->private->registered);
580 if ((ret = device_add_files(dev))) {
581 if (test_and_clear_bit(1, &cdev->private->registered))
588 struct ccw_dev_id dev_id;
589 struct ccw_device * sibling;
593 match_devno(struct device * dev, void * data)
595 struct match_data * d = data;
596 struct ccw_device * cdev;
598 cdev = to_ccwdev(dev);
599 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
600 !ccw_device_is_orphan(cdev) &&
601 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
602 (cdev != d->sibling))
607 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
608 struct ccw_device *sibling)
611 struct match_data data;
613 data.dev_id = *dev_id;
614 data.sibling = sibling;
615 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
617 return dev ? to_ccwdev(dev) : NULL;
620 static int match_orphan(struct device *dev, void *data)
622 struct ccw_dev_id *dev_id;
623 struct ccw_device *cdev;
626 cdev = to_ccwdev(dev);
627 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
630 static struct ccw_device *
631 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
632 struct ccw_dev_id *dev_id)
636 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
639 return dev ? to_ccwdev(dev) : NULL;
643 ccw_device_add_changed(struct work_struct *work)
645 struct ccw_device_private *priv;
646 struct ccw_device *cdev;
648 priv = container_of(work, struct ccw_device_private, kick_work);
650 if (device_add(&cdev->dev)) {
651 put_device(&cdev->dev);
654 set_bit(1, &cdev->private->registered);
655 if (device_add_files(&cdev->dev)) {
656 if (test_and_clear_bit(1, &cdev->private->registered))
657 device_unregister(&cdev->dev);
661 void ccw_device_do_unreg_rereg(struct work_struct *work)
663 struct ccw_device_private *priv;
664 struct ccw_device *cdev;
665 struct subchannel *sch;
667 priv = container_of(work, struct ccw_device_private, kick_work);
669 sch = to_subchannel(cdev->dev.parent);
671 device_remove_files(&cdev->dev);
672 if (test_and_clear_bit(1, &cdev->private->registered))
673 device_del(&cdev->dev);
674 PREPARE_WORK(&cdev->private->kick_work,
675 ccw_device_add_changed);
676 queue_work(ccw_device_work, &cdev->private->kick_work);
680 ccw_device_release(struct device *dev)
682 struct ccw_device *cdev;
684 cdev = to_ccwdev(dev);
685 kfree(cdev->private);
689 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
691 struct ccw_device *cdev;
693 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
695 cdev->private = kzalloc(sizeof(struct ccw_device_private),
696 GFP_KERNEL | GFP_DMA);
701 return ERR_PTR(-ENOMEM);
704 static int io_subchannel_initialize_dev(struct subchannel *sch,
705 struct ccw_device *cdev)
707 cdev->private->cdev = cdev;
708 atomic_set(&cdev->private->onoff, 0);
709 cdev->dev.parent = &sch->dev;
710 cdev->dev.release = ccw_device_release;
711 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
712 /* Do first half of device_register. */
713 device_initialize(&cdev->dev);
714 if (!get_device(&sch->dev)) {
715 if (cdev->dev.release)
716 cdev->dev.release(&cdev->dev);
722 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
724 struct ccw_device *cdev;
727 cdev = io_subchannel_allocate_dev(sch);
729 ret = io_subchannel_initialize_dev(sch, cdev);
738 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
740 static void sch_attach_device(struct subchannel *sch,
741 struct ccw_device *cdev)
743 spin_lock_irq(sch->lock);
744 sch->dev.driver_data = cdev;
745 cdev->private->schid = sch->schid;
746 cdev->ccwlock = sch->lock;
747 device_trigger_reprobe(sch);
748 spin_unlock_irq(sch->lock);
751 static void sch_attach_disconnected_device(struct subchannel *sch,
752 struct ccw_device *cdev)
754 struct subchannel *other_sch;
757 other_sch = to_subchannel(get_device(cdev->dev.parent));
758 ret = device_move(&cdev->dev, &sch->dev);
760 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
761 "(ret=%d)!\n", cdev->private->dev_id.ssid,
762 cdev->private->dev_id.devno, ret);
763 put_device(&other_sch->dev);
766 other_sch->dev.driver_data = NULL;
767 /* No need to keep a subchannel without ccw device around. */
768 css_sch_device_unregister(other_sch);
769 put_device(&other_sch->dev);
770 sch_attach_device(sch, cdev);
773 static void sch_attach_orphaned_device(struct subchannel *sch,
774 struct ccw_device *cdev)
778 /* Try to move the ccw device to its new subchannel. */
779 ret = device_move(&cdev->dev, &sch->dev);
781 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
782 "failed (ret=%d)!\n",
783 cdev->private->dev_id.ssid,
784 cdev->private->dev_id.devno, ret);
787 sch_attach_device(sch, cdev);
790 static void sch_create_and_recog_new_device(struct subchannel *sch)
792 struct ccw_device *cdev;
794 /* Need to allocate a new ccw device. */
795 cdev = io_subchannel_create_ccwdev(sch);
797 /* OK, we did everything we could... */
798 css_sch_device_unregister(sch);
801 spin_lock_irq(sch->lock);
802 sch->dev.driver_data = cdev;
803 spin_unlock_irq(sch->lock);
804 /* Start recognition for the new ccw device. */
805 if (io_subchannel_recog(cdev, sch)) {
806 spin_lock_irq(sch->lock);
807 sch->dev.driver_data = NULL;
808 spin_unlock_irq(sch->lock);
809 if (cdev->dev.release)
810 cdev->dev.release(&cdev->dev);
811 css_sch_device_unregister(sch);
816 void ccw_device_move_to_orphanage(struct work_struct *work)
818 struct ccw_device_private *priv;
819 struct ccw_device *cdev;
820 struct ccw_device *replacing_cdev;
821 struct subchannel *sch;
823 struct channel_subsystem *css;
824 struct ccw_dev_id dev_id;
826 priv = container_of(work, struct ccw_device_private, kick_work);
828 sch = to_subchannel(cdev->dev.parent);
829 css = to_css(sch->dev.parent);
830 dev_id.devno = sch->schib.pmcw.dev;
831 dev_id.ssid = sch->schid.ssid;
834 * Move the orphaned ccw device to the orphanage so the replacing
835 * ccw device can take its place on the subchannel.
837 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
839 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
840 "(ret=%d)!\n", cdev->private->dev_id.ssid,
841 cdev->private->dev_id.devno, ret);
844 cdev->ccwlock = css->pseudo_subchannel->lock;
846 * Search for the replacing ccw device
847 * - among the disconnected devices
850 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
851 if (replacing_cdev) {
852 sch_attach_disconnected_device(sch, replacing_cdev);
855 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
856 if (replacing_cdev) {
857 sch_attach_orphaned_device(sch, replacing_cdev);
860 sch_create_and_recog_new_device(sch);
864 * Register recognized device.
867 io_subchannel_register(struct work_struct *work)
869 struct ccw_device_private *priv;
870 struct ccw_device *cdev;
871 struct subchannel *sch;
875 priv = container_of(work, struct ccw_device_private, kick_work);
877 sch = to_subchannel(cdev->dev.parent);
880 * io_subchannel_register() will also be called after device
881 * recognition has been done for a boxed device (which will already
882 * be registered). We need to reprobe since we may now have sense id
885 if (klist_node_attached(&cdev->dev.knode_parent)) {
887 ret = device_reprobe(&cdev->dev);
889 /* We can't do much here. */
890 dev_info(&cdev->dev, "device_reprobe() returned"
895 /* make it known to the system */
896 ret = ccw_device_register(cdev);
898 printk (KERN_WARNING "%s: could not register %s\n",
899 __func__, cdev->dev.bus_id);
900 put_device(&cdev->dev);
901 spin_lock_irqsave(sch->lock, flags);
902 sch->dev.driver_data = NULL;
903 spin_unlock_irqrestore(sch->lock, flags);
904 kfree (cdev->private);
906 put_device(&sch->dev);
907 if (atomic_dec_and_test(&ccw_device_init_count))
908 wake_up(&ccw_device_init_wq);
911 put_device(&cdev->dev);
913 cdev->private->flags.recog_done = 1;
914 put_device(&sch->dev);
915 wake_up(&cdev->private->wait_q);
916 if (atomic_dec_and_test(&ccw_device_init_count))
917 wake_up(&ccw_device_init_wq);
921 ccw_device_call_sch_unregister(struct work_struct *work)
923 struct ccw_device_private *priv;
924 struct ccw_device *cdev;
925 struct subchannel *sch;
927 priv = container_of(work, struct ccw_device_private, kick_work);
929 sch = to_subchannel(cdev->dev.parent);
930 css_sch_device_unregister(sch);
931 /* Reset intparm to zeroes. */
932 sch->schib.pmcw.intparm = 0;
934 put_device(&cdev->dev);
935 put_device(&sch->dev);
939 * subchannel recognition done. Called from the state machine.
942 io_subchannel_recog_done(struct ccw_device *cdev)
944 struct subchannel *sch;
946 if (css_init_done == 0) {
947 cdev->private->flags.recog_done = 1;
950 switch (cdev->private->state) {
951 case DEV_STATE_NOT_OPER:
952 cdev->private->flags.recog_done = 1;
953 /* Remove device found not operational. */
954 if (!get_device(&cdev->dev))
956 sch = to_subchannel(cdev->dev.parent);
957 PREPARE_WORK(&cdev->private->kick_work,
958 ccw_device_call_sch_unregister);
959 queue_work(slow_path_wq, &cdev->private->kick_work);
960 if (atomic_dec_and_test(&ccw_device_init_count))
961 wake_up(&ccw_device_init_wq);
963 case DEV_STATE_BOXED:
964 /* Device did not respond in time. */
965 case DEV_STATE_OFFLINE:
967 * We can't register the device in interrupt context so
968 * we schedule a work item.
970 if (!get_device(&cdev->dev))
972 PREPARE_WORK(&cdev->private->kick_work,
973 io_subchannel_register);
974 queue_work(slow_path_wq, &cdev->private->kick_work);
980 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
983 struct ccw_device_private *priv;
985 sch->dev.driver_data = cdev;
986 sch->driver = &io_subchannel_driver;
987 cdev->ccwlock = sch->lock;
989 /* Init private data. */
990 priv = cdev->private;
991 priv->dev_id.devno = sch->schib.pmcw.dev;
992 priv->dev_id.ssid = sch->schid.ssid;
993 priv->schid = sch->schid;
994 priv->state = DEV_STATE_NOT_OPER;
995 INIT_LIST_HEAD(&priv->cmb_list);
996 init_waitqueue_head(&priv->wait_q);
997 init_timer(&priv->timer);
999 /* Set an initial name for the device. */
1000 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1001 sch->schid.ssid, sch->schib.pmcw.dev);
1003 /* Increase counter of devices currently in recognition. */
1004 atomic_inc(&ccw_device_init_count);
1006 /* Start async. device sensing. */
1007 spin_lock_irq(sch->lock);
1008 rc = ccw_device_recognition(cdev);
1009 spin_unlock_irq(sch->lock);
1011 if (atomic_dec_and_test(&ccw_device_init_count))
1012 wake_up(&ccw_device_init_wq);
1017 static void ccw_device_move_to_sch(struct work_struct *work)
1019 struct ccw_device_private *priv;
1021 struct subchannel *sch;
1022 struct ccw_device *cdev;
1023 struct subchannel *former_parent;
1025 priv = container_of(work, struct ccw_device_private, kick_work);
1028 former_parent = ccw_device_is_orphan(cdev) ?
1029 NULL : to_subchannel(get_device(cdev->dev.parent));
1030 mutex_lock(&sch->reg_mutex);
1031 /* Try to move the ccw device to its new subchannel. */
1032 rc = device_move(&cdev->dev, &sch->dev);
1033 mutex_unlock(&sch->reg_mutex);
1035 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1036 "0.%x.%04x failed (ret=%d)!\n",
1037 cdev->private->dev_id.ssid,
1038 cdev->private->dev_id.devno, sch->schid.ssid,
1039 sch->schid.sch_no, rc);
1040 css_sch_device_unregister(sch);
1043 if (former_parent) {
1044 spin_lock_irq(former_parent->lock);
1045 former_parent->dev.driver_data = NULL;
1046 spin_unlock_irq(former_parent->lock);
1047 css_sch_device_unregister(former_parent);
1048 /* Reset intparm to zeroes. */
1049 former_parent->schib.pmcw.intparm = 0;
1050 cio_modify(former_parent);
1052 sch_attach_device(sch, cdev);
1055 put_device(&former_parent->dev);
1056 put_device(&cdev->dev);
1060 io_subchannel_probe (struct subchannel *sch)
1062 struct ccw_device *cdev;
1064 unsigned long flags;
1065 struct ccw_dev_id dev_id;
1067 if (sch->dev.driver_data) {
1069 * This subchannel already has an associated ccw_device.
1070 * Register it and exit. This happens for all early
1071 * device, e.g. the console.
1073 cdev = sch->dev.driver_data;
1074 device_initialize(&cdev->dev);
1075 ccw_device_register(cdev);
1077 * Check if the device is already online. If it is
1078 * the reference count needs to be corrected
1079 * (see ccw_device_online and css_init_done for the
1082 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1083 cdev->private->state != DEV_STATE_OFFLINE &&
1084 cdev->private->state != DEV_STATE_BOXED)
1085 get_device(&cdev->dev);
1089 * First check if a fitting device may be found amongst the
1090 * disconnected devices or in the orphanage.
1092 dev_id.devno = sch->schib.pmcw.dev;
1093 dev_id.ssid = sch->schid.ssid;
1094 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1096 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1100 * Schedule moving the device until when we have a registered
1101 * subchannel to move to and succeed the probe. We can
1102 * unregister later again, when the probe is through.
1104 cdev->private->sch = sch;
1105 PREPARE_WORK(&cdev->private->kick_work,
1106 ccw_device_move_to_sch);
1107 queue_work(slow_path_wq, &cdev->private->kick_work);
1110 cdev = io_subchannel_create_ccwdev(sch);
1112 return PTR_ERR(cdev);
1114 rc = io_subchannel_recog(cdev, sch);
1116 spin_lock_irqsave(sch->lock, flags);
1117 sch->dev.driver_data = NULL;
1118 spin_unlock_irqrestore(sch->lock, flags);
1119 if (cdev->dev.release)
1120 cdev->dev.release(&cdev->dev);
1127 io_subchannel_remove (struct subchannel *sch)
1129 struct ccw_device *cdev;
1130 unsigned long flags;
1132 if (!sch->dev.driver_data)
1134 cdev = sch->dev.driver_data;
1135 /* Set ccw device to not operational and drop reference. */
1136 spin_lock_irqsave(cdev->ccwlock, flags);
1137 sch->dev.driver_data = NULL;
1138 cdev->private->state = DEV_STATE_NOT_OPER;
1139 spin_unlock_irqrestore(cdev->ccwlock, flags);
1141 * Put unregistration on workqueue to avoid livelocks on the css bus
1144 if (get_device(&cdev->dev)) {
1145 PREPARE_WORK(&cdev->private->kick_work,
1146 ccw_device_unregister);
1147 queue_work(ccw_device_work, &cdev->private->kick_work);
1153 io_subchannel_notify(struct device *dev, int event)
1155 struct ccw_device *cdev;
1157 cdev = dev->driver_data;
1164 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1168 io_subchannel_verify(struct device *dev)
1170 struct ccw_device *cdev;
1172 cdev = dev->driver_data;
1174 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1178 io_subchannel_ioterm(struct device *dev)
1180 struct ccw_device *cdev;
1182 cdev = dev->driver_data;
1185 /* Internal I/O will be retried by the interrupt handler. */
1186 if (cdev->private->flags.intretry)
1188 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1190 cdev->handler(cdev, cdev->private->intparm,
1195 io_subchannel_shutdown(struct subchannel *sch)
1197 struct ccw_device *cdev;
1200 cdev = sch->dev.driver_data;
1202 if (cio_is_console(sch->schid))
1204 if (!sch->schib.pmcw.ena)
1205 /* Nothing to do. */
1207 ret = cio_disable_subchannel(sch);
1209 /* Subchannel is disabled, we're done. */
1211 cdev->private->state = DEV_STATE_QUIESCE;
1213 cdev->handler(cdev, cdev->private->intparm,
1215 ret = ccw_device_cancel_halt_clear(cdev);
1216 if (ret == -EBUSY) {
1217 ccw_device_set_timeout(cdev, HZ/10);
1218 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1220 cio_disable_subchannel(sch);
1223 #ifdef CONFIG_CCW_CONSOLE
1224 static struct ccw_device console_cdev;
1225 static struct ccw_device_private console_private;
1226 static int console_cdev_in_use;
1228 static DEFINE_SPINLOCK(ccw_console_lock);
1230 spinlock_t * cio_get_console_lock(void)
1232 return &ccw_console_lock;
1236 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1240 /* Initialize the ccw_device structure. */
1241 cdev->dev.parent= &sch->dev;
1242 rc = io_subchannel_recog(cdev, sch);
1246 /* Now wait for the async. recognition to come to an end. */
1247 spin_lock_irq(cdev->ccwlock);
1248 while (!dev_fsm_final_state(cdev))
1251 if (cdev->private->state != DEV_STATE_OFFLINE)
1253 ccw_device_online(cdev);
1254 while (!dev_fsm_final_state(cdev))
1256 if (cdev->private->state != DEV_STATE_ONLINE)
1260 spin_unlock_irq(cdev->ccwlock);
1265 ccw_device_probe_console(void)
1267 struct subchannel *sch;
1270 if (xchg(&console_cdev_in_use, 1) != 0)
1271 return ERR_PTR(-EBUSY);
1272 sch = cio_probe_console();
1274 console_cdev_in_use = 0;
1275 return (void *) sch;
1277 memset(&console_cdev, 0, sizeof(struct ccw_device));
1278 memset(&console_private, 0, sizeof(struct ccw_device_private));
1279 console_cdev.private = &console_private;
1280 console_private.cdev = &console_cdev;
1281 ret = ccw_device_console_enable(&console_cdev, sch);
1283 cio_release_console();
1284 console_cdev_in_use = 0;
1285 return ERR_PTR(ret);
1287 console_cdev.online = 1;
1288 return &console_cdev;
1293 * get ccw_device matching the busid, but only if owned by cdrv
1296 __ccwdev_check_busid(struct device *dev, void *id)
1302 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1307 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1310 struct device_driver *drv;
1312 drv = get_driver(&cdrv->driver);
1316 dev = driver_find_device(drv, NULL, (void *)bus_id,
1317 __ccwdev_check_busid);
1320 return dev ? to_ccwdev(dev) : NULL;
1323 /************************** device driver handling ************************/
1325 /* This is the implementation of the ccw_driver class. The probe, remove
1326 * and release methods are initially very similar to the device_driver
1327 * implementations, with the difference that they have ccw_device
1330 * A ccw driver also contains the information that is needed for
1334 ccw_device_probe (struct device *dev)
1336 struct ccw_device *cdev = to_ccwdev(dev);
1337 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1340 cdev->drv = cdrv; /* to let the driver call _set_online */
1342 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1353 ccw_device_remove (struct device *dev)
1355 struct ccw_device *cdev = to_ccwdev(dev);
1356 struct ccw_driver *cdrv = cdev->drv;
1359 pr_debug("removing device %s\n", cdev->dev.bus_id);
1364 spin_lock_irq(cdev->ccwlock);
1365 ret = ccw_device_offline(cdev);
1366 spin_unlock_irq(cdev->ccwlock);
1368 wait_event(cdev->private->wait_q,
1369 dev_fsm_final_state(cdev));
1371 //FIXME: we can't fail!
1372 pr_debug("ccw_device_offline returned %d, device %s\n",
1373 ret, cdev->dev.bus_id);
1375 ccw_device_set_timeout(cdev, 0);
1380 struct bus_type ccw_bus_type = {
1382 .match = ccw_bus_match,
1383 .uevent = ccw_uevent,
1384 .probe = ccw_device_probe,
1385 .remove = ccw_device_remove,
1389 ccw_driver_register (struct ccw_driver *cdriver)
1391 struct device_driver *drv = &cdriver->driver;
1393 drv->bus = &ccw_bus_type;
1394 drv->name = cdriver->name;
1396 return driver_register(drv);
1400 ccw_driver_unregister (struct ccw_driver *cdriver)
1402 driver_unregister(&cdriver->driver);
1405 /* Helper func for qdio. */
1406 struct subchannel_id
1407 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1409 struct subchannel *sch;
1411 sch = to_subchannel(cdev->dev.parent);
1415 MODULE_LICENSE("GPL");
1416 EXPORT_SYMBOL(ccw_device_set_online);
1417 EXPORT_SYMBOL(ccw_device_set_offline);
1418 EXPORT_SYMBOL(ccw_driver_register);
1419 EXPORT_SYMBOL(ccw_driver_unregister);
1420 EXPORT_SYMBOL(get_ccwdev_by_busid);
1421 EXPORT_SYMBOL(ccw_bus_type);
1422 EXPORT_SYMBOL(ccw_device_work);
1423 EXPORT_SYMBOL(ccw_device_notify_work);
1424 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);