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;
238 for (chp = 0; chp < 8; chp++)
239 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
241 ret += sprintf (buf+ret, "\n");
242 return min((ssize_t)PAGE_SIZE, ret);
246 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
248 struct subchannel *sch = to_subchannel(dev);
249 struct pmcw *pmcw = &sch->schib.pmcw;
251 return sprintf (buf, "%02x %02x %02x\n",
252 pmcw->pim, pmcw->pam, pmcw->pom);
256 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
258 struct ccw_device *cdev = to_ccwdev(dev);
259 struct ccw_device_id *id = &(cdev->id);
261 if (id->dev_type != 0)
262 return sprintf(buf, "%04x/%02x\n",
263 id->dev_type, id->dev_model);
265 return sprintf(buf, "n/a\n");
269 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
271 struct ccw_device *cdev = to_ccwdev(dev);
272 struct ccw_device_id *id = &(cdev->id);
274 return sprintf(buf, "%04x/%02x\n",
275 id->cu_type, id->cu_model);
279 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
281 struct ccw_device *cdev = to_ccwdev(dev);
282 struct ccw_device_id *id = &(cdev->id);
285 len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
287 return len > PAGE_SIZE ? PAGE_SIZE : len;
291 online_show (struct device *dev, struct device_attribute *attr, char *buf)
293 struct ccw_device *cdev = to_ccwdev(dev);
295 return sprintf(buf, cdev->online ? "1\n" : "0\n");
298 int ccw_device_is_orphan(struct ccw_device *cdev)
300 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
303 static void ccw_device_unregister(struct work_struct *work)
305 struct ccw_device_private *priv;
306 struct ccw_device *cdev;
308 priv = container_of(work, struct ccw_device_private, kick_work);
310 if (test_and_clear_bit(1, &cdev->private->registered))
311 device_unregister(&cdev->dev);
312 put_device(&cdev->dev);
316 ccw_device_remove_disconnected(struct ccw_device *cdev)
318 struct subchannel *sch;
321 * Forced offline in disconnected state means
322 * 'throw away device'.
324 if (ccw_device_is_orphan(cdev)) {
325 /* Deregister ccw device. */
326 spin_lock_irqsave(cdev->ccwlock, flags);
327 cdev->private->state = DEV_STATE_NOT_OPER;
328 spin_unlock_irqrestore(cdev->ccwlock, flags);
329 if (get_device(&cdev->dev)) {
330 PREPARE_WORK(&cdev->private->kick_work,
331 ccw_device_unregister);
332 queue_work(ccw_device_work, &cdev->private->kick_work);
336 sch = to_subchannel(cdev->dev.parent);
337 css_sch_device_unregister(sch);
338 /* Reset intparm to zeroes. */
339 sch->schib.pmcw.intparm = 0;
341 put_device(&sch->dev);
345 ccw_device_set_offline(struct ccw_device *cdev)
351 if (!cdev->online || !cdev->drv)
354 if (cdev->drv->set_offline) {
355 ret = cdev->drv->set_offline(cdev);
360 spin_lock_irq(cdev->ccwlock);
361 ret = ccw_device_offline(cdev);
362 if (ret == -ENODEV) {
363 if (cdev->private->state != DEV_STATE_NOT_OPER) {
364 cdev->private->state = DEV_STATE_OFFLINE;
365 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
367 spin_unlock_irq(cdev->ccwlock);
370 spin_unlock_irq(cdev->ccwlock);
372 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
374 pr_debug("ccw_device_offline returned %d, device %s\n",
375 ret, cdev->dev.bus_id);
382 ccw_device_set_online(struct ccw_device *cdev)
388 if (cdev->online || !cdev->drv)
391 spin_lock_irq(cdev->ccwlock);
392 ret = ccw_device_online(cdev);
393 spin_unlock_irq(cdev->ccwlock);
395 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
397 pr_debug("ccw_device_online returned %d, device %s\n",
398 ret, cdev->dev.bus_id);
401 if (cdev->private->state != DEV_STATE_ONLINE)
403 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
407 spin_lock_irq(cdev->ccwlock);
408 ret = ccw_device_offline(cdev);
409 spin_unlock_irq(cdev->ccwlock);
411 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
413 pr_debug("ccw_device_offline returned %d, device %s\n",
414 ret, cdev->dev.bus_id);
415 return (ret == 0) ? -ENODEV : ret;
419 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
421 struct ccw_device *cdev = to_ccwdev(dev);
425 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
428 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
429 atomic_set(&cdev->private->onoff, 0);
432 if (!strncmp(buf, "force\n", count)) {
437 i = simple_strtoul(buf, &tmp, 16);
440 /* Do device recognition, if needed. */
441 if (cdev->id.cu_type == 0) {
442 ret = ccw_device_recognition(cdev);
444 printk(KERN_WARNING"Couldn't start recognition "
445 "for device %s (ret=%d)\n",
446 cdev->dev.bus_id, ret);
449 wait_event(cdev->private->wait_q,
450 cdev->private->flags.recog_done);
452 if (cdev->drv && cdev->drv->set_online)
453 ccw_device_set_online(cdev);
455 if (cdev->private->state == DEV_STATE_DISCONNECTED)
456 ccw_device_remove_disconnected(cdev);
457 else if (cdev->drv && cdev->drv->set_offline)
458 ccw_device_set_offline(cdev);
460 if (force && cdev->private->state == DEV_STATE_BOXED) {
461 ret = ccw_device_stlck(cdev);
463 printk(KERN_WARNING"ccw_device_stlck for device %s "
464 "returned %d!\n", cdev->dev.bus_id, ret);
467 /* Do device recognition, if needed. */
468 if (cdev->id.cu_type == 0) {
469 cdev->private->state = DEV_STATE_NOT_OPER;
470 ret = ccw_device_recognition(cdev);
472 printk(KERN_WARNING"Couldn't start recognition "
473 "for device %s (ret=%d)\n",
474 cdev->dev.bus_id, ret);
477 wait_event(cdev->private->wait_q,
478 cdev->private->flags.recog_done);
480 if (cdev->drv && cdev->drv->set_online)
481 ccw_device_set_online(cdev);
485 module_put(cdev->drv->owner);
486 atomic_set(&cdev->private->onoff, 0);
491 available_show (struct device *dev, struct device_attribute *attr, char *buf)
493 struct ccw_device *cdev = to_ccwdev(dev);
494 struct subchannel *sch;
496 if (ccw_device_is_orphan(cdev))
497 return sprintf(buf, "no device\n");
498 switch (cdev->private->state) {
499 case DEV_STATE_BOXED:
500 return sprintf(buf, "boxed\n");
501 case DEV_STATE_DISCONNECTED:
502 case DEV_STATE_DISCONNECTED_SENSE_ID:
503 case DEV_STATE_NOT_OPER:
504 sch = to_subchannel(dev->parent);
506 return sprintf(buf, "no path\n");
508 return sprintf(buf, "no device\n");
510 /* All other states considered fine. */
511 return sprintf(buf, "good\n");
515 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
516 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
517 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
518 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
519 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
520 static DEVICE_ATTR(online, 0644, online_show, online_store);
521 extern struct device_attribute dev_attr_cmb_enable;
522 static DEVICE_ATTR(availability, 0444, available_show, NULL);
524 static struct attribute * subch_attrs[] = {
525 &dev_attr_chpids.attr,
526 &dev_attr_pimpampom.attr,
530 static struct attribute_group subch_attr_group = {
531 .attrs = subch_attrs,
534 int subchannel_add_files (struct device *dev)
536 return sysfs_create_group(&dev->kobj, &subch_attr_group);
539 static struct attribute * ccwdev_attrs[] = {
540 &dev_attr_devtype.attr,
541 &dev_attr_cutype.attr,
542 &dev_attr_modalias.attr,
543 &dev_attr_online.attr,
544 &dev_attr_cmb_enable.attr,
545 &dev_attr_availability.attr,
549 static struct attribute_group ccwdev_attr_group = {
550 .attrs = ccwdev_attrs,
554 device_add_files (struct device *dev)
556 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
560 device_remove_files(struct device *dev)
562 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
565 /* this is a simple abstraction for device_register that sets the
566 * correct bus type and adds the bus specific files */
567 static int ccw_device_register(struct ccw_device *cdev)
569 struct device *dev = &cdev->dev;
572 dev->bus = &ccw_bus_type;
574 if ((ret = device_add(dev)))
577 set_bit(1, &cdev->private->registered);
578 if ((ret = device_add_files(dev))) {
579 if (test_and_clear_bit(1, &cdev->private->registered))
586 struct ccw_dev_id dev_id;
587 struct ccw_device * sibling;
591 match_devno(struct device * dev, void * data)
593 struct match_data * d = data;
594 struct ccw_device * cdev;
596 cdev = to_ccwdev(dev);
597 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
598 !ccw_device_is_orphan(cdev) &&
599 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
600 (cdev != d->sibling))
605 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
606 struct ccw_device *sibling)
609 struct match_data data;
611 data.dev_id = *dev_id;
612 data.sibling = sibling;
613 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
615 return dev ? to_ccwdev(dev) : NULL;
618 static int match_orphan(struct device *dev, void *data)
620 struct ccw_dev_id *dev_id;
621 struct ccw_device *cdev;
624 cdev = to_ccwdev(dev);
625 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
628 static struct ccw_device *
629 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
630 struct ccw_dev_id *dev_id)
634 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
637 return dev ? to_ccwdev(dev) : NULL;
641 ccw_device_add_changed(struct work_struct *work)
643 struct ccw_device_private *priv;
644 struct ccw_device *cdev;
646 priv = container_of(work, struct ccw_device_private, kick_work);
648 if (device_add(&cdev->dev)) {
649 put_device(&cdev->dev);
652 set_bit(1, &cdev->private->registered);
653 if (device_add_files(&cdev->dev)) {
654 if (test_and_clear_bit(1, &cdev->private->registered))
655 device_unregister(&cdev->dev);
659 void ccw_device_do_unreg_rereg(struct work_struct *work)
661 struct ccw_device_private *priv;
662 struct ccw_device *cdev;
663 struct subchannel *sch;
665 priv = container_of(work, struct ccw_device_private, kick_work);
667 sch = to_subchannel(cdev->dev.parent);
669 device_remove_files(&cdev->dev);
670 if (test_and_clear_bit(1, &cdev->private->registered))
671 device_del(&cdev->dev);
672 PREPARE_WORK(&cdev->private->kick_work,
673 ccw_device_add_changed);
674 queue_work(ccw_device_work, &cdev->private->kick_work);
678 ccw_device_release(struct device *dev)
680 struct ccw_device *cdev;
682 cdev = to_ccwdev(dev);
683 kfree(cdev->private);
687 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
689 struct ccw_device *cdev;
691 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
693 cdev->private = kzalloc(sizeof(struct ccw_device_private),
694 GFP_KERNEL | GFP_DMA);
699 return ERR_PTR(-ENOMEM);
702 static int io_subchannel_initialize_dev(struct subchannel *sch,
703 struct ccw_device *cdev)
705 cdev->private->cdev = cdev;
706 atomic_set(&cdev->private->onoff, 0);
707 cdev->dev.parent = &sch->dev;
708 cdev->dev.release = ccw_device_release;
709 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
710 /* Do first half of device_register. */
711 device_initialize(&cdev->dev);
712 if (!get_device(&sch->dev)) {
713 if (cdev->dev.release)
714 cdev->dev.release(&cdev->dev);
720 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
722 struct ccw_device *cdev;
725 cdev = io_subchannel_allocate_dev(sch);
727 ret = io_subchannel_initialize_dev(sch, cdev);
736 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
738 static void sch_attach_device(struct subchannel *sch,
739 struct ccw_device *cdev)
741 spin_lock_irq(sch->lock);
742 sch->dev.driver_data = cdev;
743 cdev->private->schid = sch->schid;
744 cdev->ccwlock = sch->lock;
745 device_trigger_reprobe(sch);
746 spin_unlock_irq(sch->lock);
749 static void sch_attach_disconnected_device(struct subchannel *sch,
750 struct ccw_device *cdev)
752 struct subchannel *other_sch;
755 other_sch = to_subchannel(get_device(cdev->dev.parent));
756 ret = device_move(&cdev->dev, &sch->dev);
758 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
759 "(ret=%d)!\n", cdev->private->dev_id.ssid,
760 cdev->private->dev_id.devno, ret);
761 put_device(&other_sch->dev);
764 other_sch->dev.driver_data = NULL;
765 /* No need to keep a subchannel without ccw device around. */
766 css_sch_device_unregister(other_sch);
767 put_device(&other_sch->dev);
768 sch_attach_device(sch, cdev);
771 static void sch_attach_orphaned_device(struct subchannel *sch,
772 struct ccw_device *cdev)
776 /* Try to move the ccw device to its new subchannel. */
777 ret = device_move(&cdev->dev, &sch->dev);
779 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
780 "failed (ret=%d)!\n",
781 cdev->private->dev_id.ssid,
782 cdev->private->dev_id.devno, ret);
785 sch_attach_device(sch, cdev);
788 static void sch_create_and_recog_new_device(struct subchannel *sch)
790 struct ccw_device *cdev;
792 /* Need to allocate a new ccw device. */
793 cdev = io_subchannel_create_ccwdev(sch);
795 /* OK, we did everything we could... */
796 css_sch_device_unregister(sch);
799 spin_lock_irq(sch->lock);
800 sch->dev.driver_data = cdev;
801 spin_unlock_irq(sch->lock);
802 /* Start recognition for the new ccw device. */
803 if (io_subchannel_recog(cdev, sch)) {
804 spin_lock_irq(sch->lock);
805 sch->dev.driver_data = NULL;
806 spin_unlock_irq(sch->lock);
807 if (cdev->dev.release)
808 cdev->dev.release(&cdev->dev);
809 css_sch_device_unregister(sch);
814 void ccw_device_move_to_orphanage(struct work_struct *work)
816 struct ccw_device_private *priv;
817 struct ccw_device *cdev;
818 struct ccw_device *replacing_cdev;
819 struct subchannel *sch;
821 struct channel_subsystem *css;
822 struct ccw_dev_id dev_id;
824 priv = container_of(work, struct ccw_device_private, kick_work);
826 sch = to_subchannel(cdev->dev.parent);
827 css = to_css(sch->dev.parent);
828 dev_id.devno = sch->schib.pmcw.dev;
829 dev_id.ssid = sch->schid.ssid;
832 * Move the orphaned ccw device to the orphanage so the replacing
833 * ccw device can take its place on the subchannel.
835 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
837 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
838 "(ret=%d)!\n", cdev->private->dev_id.ssid,
839 cdev->private->dev_id.devno, ret);
842 cdev->ccwlock = css->pseudo_subchannel->lock;
844 * Search for the replacing ccw device
845 * - among the disconnected devices
848 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
849 if (replacing_cdev) {
850 sch_attach_disconnected_device(sch, replacing_cdev);
853 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
854 if (replacing_cdev) {
855 sch_attach_orphaned_device(sch, replacing_cdev);
858 sch_create_and_recog_new_device(sch);
862 * Register recognized device.
865 io_subchannel_register(struct work_struct *work)
867 struct ccw_device_private *priv;
868 struct ccw_device *cdev;
869 struct subchannel *sch;
873 priv = container_of(work, struct ccw_device_private, kick_work);
875 sch = to_subchannel(cdev->dev.parent);
878 * io_subchannel_register() will also be called after device
879 * recognition has been done for a boxed device (which will already
880 * be registered). We need to reprobe since we may now have sense id
883 if (klist_node_attached(&cdev->dev.knode_parent)) {
885 ret = device_reprobe(&cdev->dev);
887 /* We can't do much here. */
888 dev_info(&cdev->dev, "device_reprobe() returned"
893 /* make it known to the system */
894 ret = ccw_device_register(cdev);
896 printk (KERN_WARNING "%s: could not register %s\n",
897 __func__, cdev->dev.bus_id);
898 put_device(&cdev->dev);
899 spin_lock_irqsave(sch->lock, flags);
900 sch->dev.driver_data = NULL;
901 spin_unlock_irqrestore(sch->lock, flags);
902 kfree (cdev->private);
904 put_device(&sch->dev);
905 if (atomic_dec_and_test(&ccw_device_init_count))
906 wake_up(&ccw_device_init_wq);
909 put_device(&cdev->dev);
911 cdev->private->flags.recog_done = 1;
912 put_device(&sch->dev);
913 wake_up(&cdev->private->wait_q);
914 if (atomic_dec_and_test(&ccw_device_init_count))
915 wake_up(&ccw_device_init_wq);
919 ccw_device_call_sch_unregister(struct work_struct *work)
921 struct ccw_device_private *priv;
922 struct ccw_device *cdev;
923 struct subchannel *sch;
925 priv = container_of(work, struct ccw_device_private, kick_work);
927 sch = to_subchannel(cdev->dev.parent);
928 css_sch_device_unregister(sch);
929 /* Reset intparm to zeroes. */
930 sch->schib.pmcw.intparm = 0;
932 put_device(&cdev->dev);
933 put_device(&sch->dev);
937 * subchannel recognition done. Called from the state machine.
940 io_subchannel_recog_done(struct ccw_device *cdev)
942 struct subchannel *sch;
944 if (css_init_done == 0) {
945 cdev->private->flags.recog_done = 1;
948 switch (cdev->private->state) {
949 case DEV_STATE_NOT_OPER:
950 cdev->private->flags.recog_done = 1;
951 /* Remove device found not operational. */
952 if (!get_device(&cdev->dev))
954 sch = to_subchannel(cdev->dev.parent);
955 PREPARE_WORK(&cdev->private->kick_work,
956 ccw_device_call_sch_unregister);
957 queue_work(slow_path_wq, &cdev->private->kick_work);
958 if (atomic_dec_and_test(&ccw_device_init_count))
959 wake_up(&ccw_device_init_wq);
961 case DEV_STATE_BOXED:
962 /* Device did not respond in time. */
963 case DEV_STATE_OFFLINE:
965 * We can't register the device in interrupt context so
966 * we schedule a work item.
968 if (!get_device(&cdev->dev))
970 PREPARE_WORK(&cdev->private->kick_work,
971 io_subchannel_register);
972 queue_work(slow_path_wq, &cdev->private->kick_work);
978 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
981 struct ccw_device_private *priv;
983 sch->dev.driver_data = cdev;
984 sch->driver = &io_subchannel_driver;
985 cdev->ccwlock = sch->lock;
987 /* Init private data. */
988 priv = cdev->private;
989 priv->dev_id.devno = sch->schib.pmcw.dev;
990 priv->dev_id.ssid = sch->schid.ssid;
991 priv->schid = sch->schid;
992 priv->state = DEV_STATE_NOT_OPER;
993 INIT_LIST_HEAD(&priv->cmb_list);
994 init_waitqueue_head(&priv->wait_q);
995 init_timer(&priv->timer);
997 /* Set an initial name for the device. */
998 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
999 sch->schid.ssid, sch->schib.pmcw.dev);
1001 /* Increase counter of devices currently in recognition. */
1002 atomic_inc(&ccw_device_init_count);
1004 /* Start async. device sensing. */
1005 spin_lock_irq(sch->lock);
1006 rc = ccw_device_recognition(cdev);
1007 spin_unlock_irq(sch->lock);
1009 if (atomic_dec_and_test(&ccw_device_init_count))
1010 wake_up(&ccw_device_init_wq);
1015 static void ccw_device_move_to_sch(struct work_struct *work)
1017 struct ccw_device_private *priv;
1019 struct subchannel *sch;
1020 struct ccw_device *cdev;
1021 struct subchannel *former_parent;
1023 priv = container_of(work, struct ccw_device_private, kick_work);
1026 former_parent = ccw_device_is_orphan(cdev) ?
1027 NULL : to_subchannel(get_device(cdev->dev.parent));
1028 mutex_lock(&sch->reg_mutex);
1029 /* Try to move the ccw device to its new subchannel. */
1030 rc = device_move(&cdev->dev, &sch->dev);
1031 mutex_unlock(&sch->reg_mutex);
1033 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1034 "0.%x.%04x failed (ret=%d)!\n",
1035 cdev->private->dev_id.ssid,
1036 cdev->private->dev_id.devno, sch->schid.ssid,
1037 sch->schid.sch_no, rc);
1038 css_sch_device_unregister(sch);
1041 if (former_parent) {
1042 spin_lock_irq(former_parent->lock);
1043 former_parent->dev.driver_data = NULL;
1044 spin_unlock_irq(former_parent->lock);
1045 css_sch_device_unregister(former_parent);
1046 /* Reset intparm to zeroes. */
1047 former_parent->schib.pmcw.intparm = 0;
1048 cio_modify(former_parent);
1050 sch_attach_device(sch, cdev);
1053 put_device(&former_parent->dev);
1054 put_device(&cdev->dev);
1058 io_subchannel_probe (struct subchannel *sch)
1060 struct ccw_device *cdev;
1062 unsigned long flags;
1063 struct ccw_dev_id dev_id;
1065 if (sch->dev.driver_data) {
1067 * This subchannel already has an associated ccw_device.
1068 * Register it and exit. This happens for all early
1069 * device, e.g. the console.
1071 cdev = sch->dev.driver_data;
1072 device_initialize(&cdev->dev);
1073 ccw_device_register(cdev);
1075 * Check if the device is already online. If it is
1076 * the reference count needs to be corrected
1077 * (see ccw_device_online and css_init_done for the
1080 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1081 cdev->private->state != DEV_STATE_OFFLINE &&
1082 cdev->private->state != DEV_STATE_BOXED)
1083 get_device(&cdev->dev);
1087 * First check if a fitting device may be found amongst the
1088 * disconnected devices or in the orphanage.
1090 dev_id.devno = sch->schib.pmcw.dev;
1091 dev_id.ssid = sch->schid.ssid;
1092 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1094 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1098 * Schedule moving the device until when we have a registered
1099 * subchannel to move to and succeed the probe. We can
1100 * unregister later again, when the probe is through.
1102 cdev->private->sch = sch;
1103 PREPARE_WORK(&cdev->private->kick_work,
1104 ccw_device_move_to_sch);
1105 queue_work(slow_path_wq, &cdev->private->kick_work);
1108 cdev = io_subchannel_create_ccwdev(sch);
1110 return PTR_ERR(cdev);
1112 rc = io_subchannel_recog(cdev, sch);
1114 spin_lock_irqsave(sch->lock, flags);
1115 sch->dev.driver_data = NULL;
1116 spin_unlock_irqrestore(sch->lock, flags);
1117 if (cdev->dev.release)
1118 cdev->dev.release(&cdev->dev);
1125 io_subchannel_remove (struct subchannel *sch)
1127 struct ccw_device *cdev;
1128 unsigned long flags;
1130 if (!sch->dev.driver_data)
1132 cdev = sch->dev.driver_data;
1133 /* Set ccw device to not operational and drop reference. */
1134 spin_lock_irqsave(cdev->ccwlock, flags);
1135 sch->dev.driver_data = NULL;
1136 cdev->private->state = DEV_STATE_NOT_OPER;
1137 spin_unlock_irqrestore(cdev->ccwlock, flags);
1139 * Put unregistration on workqueue to avoid livelocks on the css bus
1142 if (get_device(&cdev->dev)) {
1143 PREPARE_WORK(&cdev->private->kick_work,
1144 ccw_device_unregister);
1145 queue_work(ccw_device_work, &cdev->private->kick_work);
1151 io_subchannel_notify(struct device *dev, int event)
1153 struct ccw_device *cdev;
1155 cdev = dev->driver_data;
1162 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1166 io_subchannel_verify(struct device *dev)
1168 struct ccw_device *cdev;
1170 cdev = dev->driver_data;
1172 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1176 io_subchannel_ioterm(struct device *dev)
1178 struct ccw_device *cdev;
1180 cdev = dev->driver_data;
1183 /* Internal I/O will be retried by the interrupt handler. */
1184 if (cdev->private->flags.intretry)
1186 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1188 cdev->handler(cdev, cdev->private->intparm,
1193 io_subchannel_shutdown(struct subchannel *sch)
1195 struct ccw_device *cdev;
1198 cdev = sch->dev.driver_data;
1200 if (cio_is_console(sch->schid))
1202 if (!sch->schib.pmcw.ena)
1203 /* Nothing to do. */
1205 ret = cio_disable_subchannel(sch);
1207 /* Subchannel is disabled, we're done. */
1209 cdev->private->state = DEV_STATE_QUIESCE;
1211 cdev->handler(cdev, cdev->private->intparm,
1213 ret = ccw_device_cancel_halt_clear(cdev);
1214 if (ret == -EBUSY) {
1215 ccw_device_set_timeout(cdev, HZ/10);
1216 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1218 cio_disable_subchannel(sch);
1221 #ifdef CONFIG_CCW_CONSOLE
1222 static struct ccw_device console_cdev;
1223 static struct ccw_device_private console_private;
1224 static int console_cdev_in_use;
1226 static DEFINE_SPINLOCK(ccw_console_lock);
1228 spinlock_t * cio_get_console_lock(void)
1230 return &ccw_console_lock;
1234 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1238 /* Initialize the ccw_device structure. */
1239 cdev->dev.parent= &sch->dev;
1240 rc = io_subchannel_recog(cdev, sch);
1244 /* Now wait for the async. recognition to come to an end. */
1245 spin_lock_irq(cdev->ccwlock);
1246 while (!dev_fsm_final_state(cdev))
1249 if (cdev->private->state != DEV_STATE_OFFLINE)
1251 ccw_device_online(cdev);
1252 while (!dev_fsm_final_state(cdev))
1254 if (cdev->private->state != DEV_STATE_ONLINE)
1258 spin_unlock_irq(cdev->ccwlock);
1263 ccw_device_probe_console(void)
1265 struct subchannel *sch;
1268 if (xchg(&console_cdev_in_use, 1) != 0)
1269 return ERR_PTR(-EBUSY);
1270 sch = cio_probe_console();
1272 console_cdev_in_use = 0;
1273 return (void *) sch;
1275 memset(&console_cdev, 0, sizeof(struct ccw_device));
1276 memset(&console_private, 0, sizeof(struct ccw_device_private));
1277 console_cdev.private = &console_private;
1278 console_private.cdev = &console_cdev;
1279 ret = ccw_device_console_enable(&console_cdev, sch);
1281 cio_release_console();
1282 console_cdev_in_use = 0;
1283 return ERR_PTR(ret);
1285 console_cdev.online = 1;
1286 return &console_cdev;
1291 * get ccw_device matching the busid, but only if owned by cdrv
1294 __ccwdev_check_busid(struct device *dev, void *id)
1300 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1305 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1308 struct device_driver *drv;
1310 drv = get_driver(&cdrv->driver);
1314 dev = driver_find_device(drv, NULL, (void *)bus_id,
1315 __ccwdev_check_busid);
1318 return dev ? to_ccwdev(dev) : NULL;
1321 /************************** device driver handling ************************/
1323 /* This is the implementation of the ccw_driver class. The probe, remove
1324 * and release methods are initially very similar to the device_driver
1325 * implementations, with the difference that they have ccw_device
1328 * A ccw driver also contains the information that is needed for
1332 ccw_device_probe (struct device *dev)
1334 struct ccw_device *cdev = to_ccwdev(dev);
1335 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1338 cdev->drv = cdrv; /* to let the driver call _set_online */
1340 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1351 ccw_device_remove (struct device *dev)
1353 struct ccw_device *cdev = to_ccwdev(dev);
1354 struct ccw_driver *cdrv = cdev->drv;
1357 pr_debug("removing device %s\n", cdev->dev.bus_id);
1362 spin_lock_irq(cdev->ccwlock);
1363 ret = ccw_device_offline(cdev);
1364 spin_unlock_irq(cdev->ccwlock);
1366 wait_event(cdev->private->wait_q,
1367 dev_fsm_final_state(cdev));
1369 //FIXME: we can't fail!
1370 pr_debug("ccw_device_offline returned %d, device %s\n",
1371 ret, cdev->dev.bus_id);
1373 ccw_device_set_timeout(cdev, 0);
1378 struct bus_type ccw_bus_type = {
1380 .match = ccw_bus_match,
1381 .uevent = ccw_uevent,
1382 .probe = ccw_device_probe,
1383 .remove = ccw_device_remove,
1387 ccw_driver_register (struct ccw_driver *cdriver)
1389 struct device_driver *drv = &cdriver->driver;
1391 drv->bus = &ccw_bus_type;
1392 drv->name = cdriver->name;
1394 return driver_register(drv);
1398 ccw_driver_unregister (struct ccw_driver *cdriver)
1400 driver_unregister(&cdriver->driver);
1403 /* Helper func for qdio. */
1404 struct subchannel_id
1405 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1407 struct subchannel *sch;
1409 sch = to_subchannel(cdev->dev.parent);
1413 MODULE_LICENSE("GPL");
1414 EXPORT_SYMBOL(ccw_device_set_online);
1415 EXPORT_SYMBOL(ccw_device_set_offline);
1416 EXPORT_SYMBOL(ccw_driver_register);
1417 EXPORT_SYMBOL(ccw_driver_unregister);
1418 EXPORT_SYMBOL(get_ccwdev_by_busid);
1419 EXPORT_SYMBOL(ccw_bus_type);
1420 EXPORT_SYMBOL(ccw_device_work);
1421 EXPORT_SYMBOL(ccw_device_notify_work);
1422 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);