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 */
27 #include "cio_debug.h"
33 /******************* bus type handling ***********************/
35 /* The Linux driver model distinguishes between a bus type and
36 * the bus itself. Of course we only have one channel
37 * subsystem driver and one channel system per machine, but
38 * we still use the abstraction. T.R. says it's a good idea. */
40 ccw_bus_match (struct device * dev, struct device_driver * drv)
42 struct ccw_device *cdev = to_ccwdev(dev);
43 struct ccw_driver *cdrv = to_ccwdrv(drv);
44 const struct ccw_device_id *ids = cdrv->ids, *found;
49 found = ccw_device_id_match(ids, &cdev->id);
53 cdev->id.driver_info = found->driver_info;
58 /* Store modalias string delimited by prefix/suffix string into buffer with
59 * specified size. Return length of resulting string (excluding trailing '\0')
60 * even if string doesn't fit buffer (snprintf semantics). */
61 static int snprint_alias(char *buf, size_t size,
62 struct ccw_device_id *id, const char *suffix)
66 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
72 if (id->dev_type != 0)
73 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
74 id->dev_model, suffix);
76 len += snprintf(buf, size, "dtdm%s", suffix);
81 /* Set up environment variables for ccw device uevent. Return 0 on success,
82 * non-zero otherwise. */
83 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
85 struct ccw_device *cdev = to_ccwdev(dev);
86 struct ccw_device_id *id = &(cdev->id);
88 char modalias_buf[30];
91 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
96 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
100 /* The next two can be zero, that's ok for us */
102 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
107 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
112 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
113 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
117 struct bus_type ccw_bus_type;
119 static void io_subchannel_irq(struct subchannel *);
120 static int io_subchannel_probe(struct subchannel *);
121 static int io_subchannel_remove(struct subchannel *);
122 static int io_subchannel_notify(struct subchannel *, int);
123 static void io_subchannel_verify(struct subchannel *);
124 static void io_subchannel_ioterm(struct subchannel *);
125 static void io_subchannel_shutdown(struct subchannel *);
127 static struct css_driver io_subchannel_driver = {
128 .owner = THIS_MODULE,
129 .subchannel_type = SUBCHANNEL_TYPE_IO,
130 .name = "io_subchannel",
131 .irq = io_subchannel_irq,
132 .notify = io_subchannel_notify,
133 .verify = io_subchannel_verify,
134 .termination = io_subchannel_ioterm,
135 .probe = io_subchannel_probe,
136 .remove = io_subchannel_remove,
137 .shutdown = io_subchannel_shutdown,
140 struct workqueue_struct *ccw_device_work;
141 struct workqueue_struct *ccw_device_notify_work;
142 wait_queue_head_t ccw_device_init_wq;
143 atomic_t ccw_device_init_count;
146 init_ccw_bus_type (void)
150 init_waitqueue_head(&ccw_device_init_wq);
151 atomic_set(&ccw_device_init_count, 0);
153 ccw_device_work = create_singlethread_workqueue("cio");
154 if (!ccw_device_work)
155 return -ENOMEM; /* FIXME: better errno ? */
156 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
157 if (!ccw_device_notify_work) {
158 ret = -ENOMEM; /* FIXME: better errno ? */
161 slow_path_wq = create_singlethread_workqueue("kslowcrw");
163 ret = -ENOMEM; /* FIXME: better errno ? */
166 if ((ret = bus_register (&ccw_bus_type)))
169 ret = css_driver_register(&io_subchannel_driver);
173 wait_event(ccw_device_init_wq,
174 atomic_read(&ccw_device_init_count) == 0);
175 flush_workqueue(ccw_device_work);
179 destroy_workqueue(ccw_device_work);
180 if (ccw_device_notify_work)
181 destroy_workqueue(ccw_device_notify_work);
183 destroy_workqueue(slow_path_wq);
188 cleanup_ccw_bus_type (void)
190 css_driver_unregister(&io_subchannel_driver);
191 bus_unregister(&ccw_bus_type);
192 destroy_workqueue(ccw_device_notify_work);
193 destroy_workqueue(ccw_device_work);
196 subsys_initcall(init_ccw_bus_type);
197 module_exit(cleanup_ccw_bus_type);
199 /************************ device handling **************************/
202 * A ccw_device has some interfaces in sysfs in addition to the
204 * The following entries are designed to export the information which
205 * resided in 2.4 in /proc/subchannels. Subchannel and device number
206 * are obvious, so they don't have an entry :)
207 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
210 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
212 struct subchannel *sch = to_subchannel(dev);
213 struct chsc_ssd_info *ssd = &sch->ssd_info;
218 for (chp = 0; chp < 8; chp++) {
220 if (ssd->path_mask & mask)
221 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
223 ret += sprintf(buf + ret, "00 ");
225 ret += sprintf (buf+ret, "\n");
226 return min((ssize_t)PAGE_SIZE, ret);
230 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
232 struct subchannel *sch = to_subchannel(dev);
233 struct pmcw *pmcw = &sch->schib.pmcw;
235 return sprintf (buf, "%02x %02x %02x\n",
236 pmcw->pim, pmcw->pam, pmcw->pom);
240 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
242 struct ccw_device *cdev = to_ccwdev(dev);
243 struct ccw_device_id *id = &(cdev->id);
245 if (id->dev_type != 0)
246 return sprintf(buf, "%04x/%02x\n",
247 id->dev_type, id->dev_model);
249 return sprintf(buf, "n/a\n");
253 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
255 struct ccw_device *cdev = to_ccwdev(dev);
256 struct ccw_device_id *id = &(cdev->id);
258 return sprintf(buf, "%04x/%02x\n",
259 id->cu_type, id->cu_model);
263 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
265 struct ccw_device *cdev = to_ccwdev(dev);
266 struct ccw_device_id *id = &(cdev->id);
269 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
271 return len > PAGE_SIZE ? PAGE_SIZE : len;
275 online_show (struct device *dev, struct device_attribute *attr, char *buf)
277 struct ccw_device *cdev = to_ccwdev(dev);
279 return sprintf(buf, cdev->online ? "1\n" : "0\n");
282 int ccw_device_is_orphan(struct ccw_device *cdev)
284 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
287 static void ccw_device_unregister(struct ccw_device *cdev)
289 if (test_and_clear_bit(1, &cdev->private->registered))
290 device_del(&cdev->dev);
293 static void ccw_device_remove_orphan_cb(struct device *dev)
295 struct ccw_device *cdev = to_ccwdev(dev);
297 ccw_device_unregister(cdev);
298 put_device(&cdev->dev);
301 static void ccw_device_remove_sch_cb(struct device *dev)
303 struct subchannel *sch;
305 sch = to_subchannel(dev);
306 css_sch_device_unregister(sch);
307 /* Reset intparm to zeroes. */
308 sch->schib.pmcw.intparm = 0;
310 put_device(&sch->dev);
314 ccw_device_remove_disconnected(struct ccw_device *cdev)
320 * Forced offline in disconnected state means
321 * 'throw away device'.
323 if (ccw_device_is_orphan(cdev)) {
325 * Deregister ccw device.
326 * Unfortunately, we cannot do this directly from the
329 spin_lock_irqsave(cdev->ccwlock, flags);
330 cdev->private->state = DEV_STATE_NOT_OPER;
331 spin_unlock_irqrestore(cdev->ccwlock, flags);
332 rc = device_schedule_callback(&cdev->dev,
333 ccw_device_remove_orphan_cb);
335 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
337 cdev->private->dev_id.ssid,
338 cdev->private->dev_id.devno);
341 /* Deregister subchannel, which will kill the ccw device. */
342 rc = device_schedule_callback(cdev->dev.parent,
343 ccw_device_remove_sch_cb);
345 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
347 cdev->private->dev_id.ssid,
348 cdev->private->dev_id.devno);
352 * ccw_device_set_offline() - disable a ccw device for I/O
353 * @cdev: target ccw device
355 * This function calls the driver's set_offline() function for @cdev, if
356 * given, and then disables @cdev.
358 * %0 on success and a negative error value on failure.
360 * enabled, ccw device lock not held
362 int ccw_device_set_offline(struct ccw_device *cdev)
368 if (!cdev->online || !cdev->drv)
371 if (cdev->drv->set_offline) {
372 ret = cdev->drv->set_offline(cdev);
377 spin_lock_irq(cdev->ccwlock);
378 ret = ccw_device_offline(cdev);
379 if (ret == -ENODEV) {
380 if (cdev->private->state != DEV_STATE_NOT_OPER) {
381 cdev->private->state = DEV_STATE_OFFLINE;
382 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
384 spin_unlock_irq(cdev->ccwlock);
387 spin_unlock_irq(cdev->ccwlock);
389 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
391 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
392 "device 0.%x.%04x\n",
393 ret, cdev->private->dev_id.ssid,
394 cdev->private->dev_id.devno);
401 * ccw_device_set_online() - enable a ccw device for I/O
402 * @cdev: target ccw device
404 * This function first enables @cdev and then calls the driver's set_online()
405 * function for @cdev, if given. If set_online() returns an error, @cdev is
408 * %0 on success and a negative error value on failure.
410 * enabled, ccw device lock not held
412 int ccw_device_set_online(struct ccw_device *cdev)
418 if (cdev->online || !cdev->drv)
421 spin_lock_irq(cdev->ccwlock);
422 ret = ccw_device_online(cdev);
423 spin_unlock_irq(cdev->ccwlock);
425 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
427 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
428 "device 0.%x.%04x\n",
429 ret, cdev->private->dev_id.ssid,
430 cdev->private->dev_id.devno);
433 if (cdev->private->state != DEV_STATE_ONLINE)
435 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
439 spin_lock_irq(cdev->ccwlock);
440 ret = ccw_device_offline(cdev);
441 spin_unlock_irq(cdev->ccwlock);
443 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
445 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
446 "device 0.%x.%04x\n",
447 ret, cdev->private->dev_id.ssid,
448 cdev->private->dev_id.devno);
449 return (ret == 0) ? -ENODEV : ret;
452 static void online_store_handle_offline(struct ccw_device *cdev)
454 if (cdev->private->state == DEV_STATE_DISCONNECTED)
455 ccw_device_remove_disconnected(cdev);
456 else if (cdev->drv && cdev->drv->set_offline)
457 ccw_device_set_offline(cdev);
460 static int online_store_recog_and_online(struct ccw_device *cdev)
464 /* Do device recognition, if needed. */
465 if (cdev->id.cu_type == 0) {
466 ret = ccw_device_recognition(cdev);
468 CIO_MSG_EVENT(0, "Couldn't start recognition "
469 "for device 0.%x.%04x (ret=%d)\n",
470 cdev->private->dev_id.ssid,
471 cdev->private->dev_id.devno, ret);
474 wait_event(cdev->private->wait_q,
475 cdev->private->flags.recog_done);
477 if (cdev->drv && cdev->drv->set_online)
478 ccw_device_set_online(cdev);
481 static void online_store_handle_online(struct ccw_device *cdev, int force)
485 ret = online_store_recog_and_online(cdev);
488 if (force && cdev->private->state == DEV_STATE_BOXED) {
489 ret = ccw_device_stlck(cdev);
492 "ccw_device_stlck returned %d!\n", ret);
495 if (cdev->id.cu_type == 0)
496 cdev->private->state = DEV_STATE_NOT_OPER;
497 online_store_recog_and_online(cdev);
502 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
505 struct ccw_device *cdev = to_ccwdev(dev);
509 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
512 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
513 atomic_set(&cdev->private->onoff, 0);
516 if (!strncmp(buf, "force\n", count)) {
521 i = simple_strtoul(buf, &tmp, 16);
526 online_store_handle_offline(cdev);
529 online_store_handle_online(cdev, force);
535 module_put(cdev->drv->owner);
536 atomic_set(&cdev->private->onoff, 0);
541 available_show (struct device *dev, struct device_attribute *attr, char *buf)
543 struct ccw_device *cdev = to_ccwdev(dev);
544 struct subchannel *sch;
546 if (ccw_device_is_orphan(cdev))
547 return sprintf(buf, "no device\n");
548 switch (cdev->private->state) {
549 case DEV_STATE_BOXED:
550 return sprintf(buf, "boxed\n");
551 case DEV_STATE_DISCONNECTED:
552 case DEV_STATE_DISCONNECTED_SENSE_ID:
553 case DEV_STATE_NOT_OPER:
554 sch = to_subchannel(dev->parent);
556 return sprintf(buf, "no path\n");
558 return sprintf(buf, "no device\n");
560 /* All other states considered fine. */
561 return sprintf(buf, "good\n");
565 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
566 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
567 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
568 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
569 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
570 static DEVICE_ATTR(online, 0644, online_show, online_store);
571 extern struct device_attribute dev_attr_cmb_enable;
572 static DEVICE_ATTR(availability, 0444, available_show, NULL);
574 static struct attribute * subch_attrs[] = {
575 &dev_attr_chpids.attr,
576 &dev_attr_pimpampom.attr,
580 static struct attribute_group subch_attr_group = {
581 .attrs = subch_attrs,
584 struct attribute_group *subch_attr_groups[] = {
589 static struct attribute * ccwdev_attrs[] = {
590 &dev_attr_devtype.attr,
591 &dev_attr_cutype.attr,
592 &dev_attr_modalias.attr,
593 &dev_attr_online.attr,
594 &dev_attr_cmb_enable.attr,
595 &dev_attr_availability.attr,
599 static struct attribute_group ccwdev_attr_group = {
600 .attrs = ccwdev_attrs,
603 static struct attribute_group *ccwdev_attr_groups[] = {
608 /* this is a simple abstraction for device_register that sets the
609 * correct bus type and adds the bus specific files */
610 static int ccw_device_register(struct ccw_device *cdev)
612 struct device *dev = &cdev->dev;
615 dev->bus = &ccw_bus_type;
617 if ((ret = device_add(dev)))
620 set_bit(1, &cdev->private->registered);
625 struct ccw_dev_id dev_id;
626 struct ccw_device * sibling;
630 match_devno(struct device * dev, void * data)
632 struct match_data * d = data;
633 struct ccw_device * cdev;
635 cdev = to_ccwdev(dev);
636 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
637 !ccw_device_is_orphan(cdev) &&
638 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
639 (cdev != d->sibling))
644 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
645 struct ccw_device *sibling)
648 struct match_data data;
650 data.dev_id = *dev_id;
651 data.sibling = sibling;
652 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
654 return dev ? to_ccwdev(dev) : NULL;
657 static int match_orphan(struct device *dev, void *data)
659 struct ccw_dev_id *dev_id;
660 struct ccw_device *cdev;
663 cdev = to_ccwdev(dev);
664 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
667 static struct ccw_device *
668 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
669 struct ccw_dev_id *dev_id)
673 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
676 return dev ? to_ccwdev(dev) : NULL;
680 ccw_device_add_changed(struct work_struct *work)
682 struct ccw_device_private *priv;
683 struct ccw_device *cdev;
685 priv = container_of(work, struct ccw_device_private, kick_work);
687 if (device_add(&cdev->dev)) {
688 put_device(&cdev->dev);
691 set_bit(1, &cdev->private->registered);
694 void ccw_device_do_unreg_rereg(struct work_struct *work)
696 struct ccw_device_private *priv;
697 struct ccw_device *cdev;
698 struct subchannel *sch;
700 priv = container_of(work, struct ccw_device_private, kick_work);
702 sch = to_subchannel(cdev->dev.parent);
704 ccw_device_unregister(cdev);
705 PREPARE_WORK(&cdev->private->kick_work,
706 ccw_device_add_changed);
707 queue_work(ccw_device_work, &cdev->private->kick_work);
711 ccw_device_release(struct device *dev)
713 struct ccw_device *cdev;
715 cdev = to_ccwdev(dev);
716 kfree(cdev->private);
720 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
722 struct ccw_device *cdev;
724 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
726 cdev->private = kzalloc(sizeof(struct ccw_device_private),
727 GFP_KERNEL | GFP_DMA);
732 return ERR_PTR(-ENOMEM);
735 static int io_subchannel_initialize_dev(struct subchannel *sch,
736 struct ccw_device *cdev)
738 cdev->private->cdev = cdev;
739 atomic_set(&cdev->private->onoff, 0);
740 cdev->dev.parent = &sch->dev;
741 cdev->dev.release = ccw_device_release;
742 INIT_WORK(&cdev->private->kick_work, NULL);
743 cdev->dev.groups = ccwdev_attr_groups;
744 /* Do first half of device_register. */
745 device_initialize(&cdev->dev);
746 if (!get_device(&sch->dev)) {
747 if (cdev->dev.release)
748 cdev->dev.release(&cdev->dev);
754 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
756 struct ccw_device *cdev;
759 cdev = io_subchannel_allocate_dev(sch);
761 ret = io_subchannel_initialize_dev(sch, cdev);
770 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
772 static void sch_attach_device(struct subchannel *sch,
773 struct ccw_device *cdev)
775 css_update_ssd_info(sch);
776 spin_lock_irq(sch->lock);
777 sch_set_cdev(sch, cdev);
778 cdev->private->schid = sch->schid;
779 cdev->ccwlock = sch->lock;
780 device_trigger_reprobe(sch);
781 spin_unlock_irq(sch->lock);
784 static void sch_attach_disconnected_device(struct subchannel *sch,
785 struct ccw_device *cdev)
787 struct subchannel *other_sch;
790 other_sch = to_subchannel(get_device(cdev->dev.parent));
791 ret = device_move(&cdev->dev, &sch->dev);
793 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
794 "(ret=%d)!\n", cdev->private->dev_id.ssid,
795 cdev->private->dev_id.devno, ret);
796 put_device(&other_sch->dev);
799 sch_set_cdev(other_sch, NULL);
800 /* No need to keep a subchannel without ccw device around. */
801 css_sch_device_unregister(other_sch);
802 put_device(&other_sch->dev);
803 sch_attach_device(sch, cdev);
806 static void sch_attach_orphaned_device(struct subchannel *sch,
807 struct ccw_device *cdev)
811 /* Try to move the ccw device to its new subchannel. */
812 ret = device_move(&cdev->dev, &sch->dev);
814 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
815 "failed (ret=%d)!\n",
816 cdev->private->dev_id.ssid,
817 cdev->private->dev_id.devno, ret);
820 sch_attach_device(sch, cdev);
823 static void sch_create_and_recog_new_device(struct subchannel *sch)
825 struct ccw_device *cdev;
827 /* Need to allocate a new ccw device. */
828 cdev = io_subchannel_create_ccwdev(sch);
830 /* OK, we did everything we could... */
831 css_sch_device_unregister(sch);
834 spin_lock_irq(sch->lock);
835 sch_set_cdev(sch, cdev);
836 spin_unlock_irq(sch->lock);
837 /* Start recognition for the new ccw device. */
838 if (io_subchannel_recog(cdev, sch)) {
839 spin_lock_irq(sch->lock);
840 sch_set_cdev(sch, NULL);
841 spin_unlock_irq(sch->lock);
842 if (cdev->dev.release)
843 cdev->dev.release(&cdev->dev);
844 css_sch_device_unregister(sch);
849 void ccw_device_move_to_orphanage(struct work_struct *work)
851 struct ccw_device_private *priv;
852 struct ccw_device *cdev;
853 struct ccw_device *replacing_cdev;
854 struct subchannel *sch;
856 struct channel_subsystem *css;
857 struct ccw_dev_id dev_id;
859 priv = container_of(work, struct ccw_device_private, kick_work);
861 sch = to_subchannel(cdev->dev.parent);
862 css = to_css(sch->dev.parent);
863 dev_id.devno = sch->schib.pmcw.dev;
864 dev_id.ssid = sch->schid.ssid;
867 * Move the orphaned ccw device to the orphanage so the replacing
868 * ccw device can take its place on the subchannel.
870 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
872 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
873 "(ret=%d)!\n", cdev->private->dev_id.ssid,
874 cdev->private->dev_id.devno, ret);
877 cdev->ccwlock = css->pseudo_subchannel->lock;
879 * Search for the replacing ccw device
880 * - among the disconnected devices
883 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
884 if (replacing_cdev) {
885 sch_attach_disconnected_device(sch, replacing_cdev);
888 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
889 if (replacing_cdev) {
890 sch_attach_orphaned_device(sch, replacing_cdev);
893 sch_create_and_recog_new_device(sch);
897 * Register recognized device.
900 io_subchannel_register(struct work_struct *work)
902 struct ccw_device_private *priv;
903 struct ccw_device *cdev;
904 struct subchannel *sch;
908 priv = container_of(work, struct ccw_device_private, kick_work);
910 sch = to_subchannel(cdev->dev.parent);
911 css_update_ssd_info(sch);
913 * io_subchannel_register() will also be called after device
914 * recognition has been done for a boxed device (which will already
915 * be registered). We need to reprobe since we may now have sense id
918 if (klist_node_attached(&cdev->dev.knode_parent)) {
920 ret = device_reprobe(&cdev->dev);
922 /* We can't do much here. */
923 CIO_MSG_EVENT(2, "device_reprobe() returned"
924 " %d for 0.%x.%04x\n", ret,
925 cdev->private->dev_id.ssid,
926 cdev->private->dev_id.devno);
931 * Now we know this subchannel will stay, we can throw
932 * our delayed uevent.
934 sch->dev.uevent_suppress = 0;
935 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
936 /* make it known to the system */
937 ret = ccw_device_register(cdev);
939 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
940 cdev->private->dev_id.ssid,
941 cdev->private->dev_id.devno, ret);
942 put_device(&cdev->dev);
943 spin_lock_irqsave(sch->lock, flags);
944 sch_set_cdev(sch, NULL);
945 spin_unlock_irqrestore(sch->lock, flags);
946 kfree (cdev->private);
948 put_device(&sch->dev);
949 if (atomic_dec_and_test(&ccw_device_init_count))
950 wake_up(&ccw_device_init_wq);
953 put_device(&cdev->dev);
955 cdev->private->flags.recog_done = 1;
956 put_device(&sch->dev);
957 wake_up(&cdev->private->wait_q);
958 if (atomic_dec_and_test(&ccw_device_init_count))
959 wake_up(&ccw_device_init_wq);
962 static void ccw_device_call_sch_unregister(struct work_struct *work)
964 struct ccw_device_private *priv;
965 struct ccw_device *cdev;
966 struct subchannel *sch;
968 priv = container_of(work, struct ccw_device_private, kick_work);
970 sch = to_subchannel(cdev->dev.parent);
971 css_sch_device_unregister(sch);
972 /* Reset intparm to zeroes. */
973 sch->schib.pmcw.intparm = 0;
975 put_device(&cdev->dev);
976 put_device(&sch->dev);
980 * subchannel recognition done. Called from the state machine.
983 io_subchannel_recog_done(struct ccw_device *cdev)
985 struct subchannel *sch;
987 if (css_init_done == 0) {
988 cdev->private->flags.recog_done = 1;
991 switch (cdev->private->state) {
992 case DEV_STATE_NOT_OPER:
993 cdev->private->flags.recog_done = 1;
994 /* Remove device found not operational. */
995 if (!get_device(&cdev->dev))
997 sch = to_subchannel(cdev->dev.parent);
998 PREPARE_WORK(&cdev->private->kick_work,
999 ccw_device_call_sch_unregister);
1000 queue_work(slow_path_wq, &cdev->private->kick_work);
1001 if (atomic_dec_and_test(&ccw_device_init_count))
1002 wake_up(&ccw_device_init_wq);
1004 case DEV_STATE_BOXED:
1005 /* Device did not respond in time. */
1006 case DEV_STATE_OFFLINE:
1008 * We can't register the device in interrupt context so
1009 * we schedule a work item.
1011 if (!get_device(&cdev->dev))
1013 PREPARE_WORK(&cdev->private->kick_work,
1014 io_subchannel_register);
1015 queue_work(slow_path_wq, &cdev->private->kick_work);
1021 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1024 struct ccw_device_private *priv;
1026 sch_set_cdev(sch, cdev);
1027 sch->driver = &io_subchannel_driver;
1028 cdev->ccwlock = sch->lock;
1030 /* Init private data. */
1031 priv = cdev->private;
1032 priv->dev_id.devno = sch->schib.pmcw.dev;
1033 priv->dev_id.ssid = sch->schid.ssid;
1034 priv->schid = sch->schid;
1035 priv->state = DEV_STATE_NOT_OPER;
1036 INIT_LIST_HEAD(&priv->cmb_list);
1037 init_waitqueue_head(&priv->wait_q);
1038 init_timer(&priv->timer);
1040 /* Set an initial name for the device. */
1041 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1042 sch->schid.ssid, sch->schib.pmcw.dev);
1044 /* Increase counter of devices currently in recognition. */
1045 atomic_inc(&ccw_device_init_count);
1047 /* Start async. device sensing. */
1048 spin_lock_irq(sch->lock);
1049 rc = ccw_device_recognition(cdev);
1050 spin_unlock_irq(sch->lock);
1052 if (atomic_dec_and_test(&ccw_device_init_count))
1053 wake_up(&ccw_device_init_wq);
1058 static void ccw_device_move_to_sch(struct work_struct *work)
1060 struct ccw_device_private *priv;
1062 struct subchannel *sch;
1063 struct ccw_device *cdev;
1064 struct subchannel *former_parent;
1066 priv = container_of(work, struct ccw_device_private, kick_work);
1069 former_parent = ccw_device_is_orphan(cdev) ?
1070 NULL : to_subchannel(get_device(cdev->dev.parent));
1071 mutex_lock(&sch->reg_mutex);
1072 /* Try to move the ccw device to its new subchannel. */
1073 rc = device_move(&cdev->dev, &sch->dev);
1074 mutex_unlock(&sch->reg_mutex);
1076 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1077 "0.%x.%04x failed (ret=%d)!\n",
1078 cdev->private->dev_id.ssid,
1079 cdev->private->dev_id.devno, sch->schid.ssid,
1080 sch->schid.sch_no, rc);
1081 css_sch_device_unregister(sch);
1084 if (former_parent) {
1085 spin_lock_irq(former_parent->lock);
1086 sch_set_cdev(former_parent, NULL);
1087 spin_unlock_irq(former_parent->lock);
1088 css_sch_device_unregister(former_parent);
1089 /* Reset intparm to zeroes. */
1090 former_parent->schib.pmcw.intparm = 0;
1091 cio_modify(former_parent);
1093 sch_attach_device(sch, cdev);
1096 put_device(&former_parent->dev);
1097 put_device(&cdev->dev);
1100 static void io_subchannel_irq(struct subchannel *sch)
1102 struct ccw_device *cdev;
1104 cdev = sch_get_cdev(sch);
1106 CIO_TRACE_EVENT(3, "IRQ");
1107 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1109 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1113 io_subchannel_probe (struct subchannel *sch)
1115 struct ccw_device *cdev;
1117 unsigned long flags;
1118 struct ccw_dev_id dev_id;
1120 cdev = sch_get_cdev(sch);
1123 * This subchannel already has an associated ccw_device.
1124 * Register it and exit. This happens for all early
1125 * device, e.g. the console.
1127 cdev->dev.groups = ccwdev_attr_groups;
1128 device_initialize(&cdev->dev);
1129 ccw_device_register(cdev);
1131 * Check if the device is already online. If it is
1132 * the reference count needs to be corrected
1133 * (see ccw_device_online and css_init_done for the
1136 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1137 cdev->private->state != DEV_STATE_OFFLINE &&
1138 cdev->private->state != DEV_STATE_BOXED)
1139 get_device(&cdev->dev);
1143 * First check if a fitting device may be found amongst the
1144 * disconnected devices or in the orphanage.
1146 dev_id.devno = sch->schib.pmcw.dev;
1147 dev_id.ssid = sch->schid.ssid;
1148 /* Allocate I/O subchannel private data. */
1149 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1150 GFP_KERNEL | GFP_DMA);
1153 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1155 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1159 * Schedule moving the device until when we have a registered
1160 * subchannel to move to and succeed the probe. We can
1161 * unregister later again, when the probe is through.
1163 cdev->private->sch = sch;
1164 PREPARE_WORK(&cdev->private->kick_work,
1165 ccw_device_move_to_sch);
1166 queue_work(slow_path_wq, &cdev->private->kick_work);
1169 cdev = io_subchannel_create_ccwdev(sch);
1171 kfree(sch->private);
1172 return PTR_ERR(cdev);
1174 rc = io_subchannel_recog(cdev, sch);
1176 spin_lock_irqsave(sch->lock, flags);
1177 sch_set_cdev(sch, NULL);
1178 spin_unlock_irqrestore(sch->lock, flags);
1179 if (cdev->dev.release)
1180 cdev->dev.release(&cdev->dev);
1181 kfree(sch->private);
1188 io_subchannel_remove (struct subchannel *sch)
1190 struct ccw_device *cdev;
1191 unsigned long flags;
1193 cdev = sch_get_cdev(sch);
1196 /* Set ccw device to not operational and drop reference. */
1197 spin_lock_irqsave(cdev->ccwlock, flags);
1198 sch_set_cdev(sch, NULL);
1199 cdev->private->state = DEV_STATE_NOT_OPER;
1200 spin_unlock_irqrestore(cdev->ccwlock, flags);
1201 ccw_device_unregister(cdev);
1202 put_device(&cdev->dev);
1203 kfree(sch->private);
1207 static int io_subchannel_notify(struct subchannel *sch, int event)
1209 struct ccw_device *cdev;
1211 cdev = sch_get_cdev(sch);
1218 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1221 static void io_subchannel_verify(struct subchannel *sch)
1223 struct ccw_device *cdev;
1225 cdev = sch_get_cdev(sch);
1227 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1230 static void io_subchannel_ioterm(struct subchannel *sch)
1232 struct ccw_device *cdev;
1234 cdev = sch_get_cdev(sch);
1237 /* Internal I/O will be retried by the interrupt handler. */
1238 if (cdev->private->flags.intretry)
1240 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1242 cdev->handler(cdev, cdev->private->intparm,
1247 io_subchannel_shutdown(struct subchannel *sch)
1249 struct ccw_device *cdev;
1252 cdev = sch_get_cdev(sch);
1254 if (cio_is_console(sch->schid))
1256 if (!sch->schib.pmcw.ena)
1257 /* Nothing to do. */
1259 ret = cio_disable_subchannel(sch);
1261 /* Subchannel is disabled, we're done. */
1263 cdev->private->state = DEV_STATE_QUIESCE;
1265 cdev->handler(cdev, cdev->private->intparm,
1267 ret = ccw_device_cancel_halt_clear(cdev);
1268 if (ret == -EBUSY) {
1269 ccw_device_set_timeout(cdev, HZ/10);
1270 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1272 cio_disable_subchannel(sch);
1275 #ifdef CONFIG_CCW_CONSOLE
1276 static struct ccw_device console_cdev;
1277 static struct ccw_device_private console_private;
1278 static int console_cdev_in_use;
1280 static DEFINE_SPINLOCK(ccw_console_lock);
1282 spinlock_t * cio_get_console_lock(void)
1284 return &ccw_console_lock;
1288 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1292 /* Attach subchannel private data. */
1293 sch->private = cio_get_console_priv();
1294 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1295 /* Initialize the ccw_device structure. */
1296 cdev->dev.parent= &sch->dev;
1297 rc = io_subchannel_recog(cdev, sch);
1301 /* Now wait for the async. recognition to come to an end. */
1302 spin_lock_irq(cdev->ccwlock);
1303 while (!dev_fsm_final_state(cdev))
1306 if (cdev->private->state != DEV_STATE_OFFLINE)
1308 ccw_device_online(cdev);
1309 while (!dev_fsm_final_state(cdev))
1311 if (cdev->private->state != DEV_STATE_ONLINE)
1315 spin_unlock_irq(cdev->ccwlock);
1320 ccw_device_probe_console(void)
1322 struct subchannel *sch;
1325 if (xchg(&console_cdev_in_use, 1) != 0)
1326 return ERR_PTR(-EBUSY);
1327 sch = cio_probe_console();
1329 console_cdev_in_use = 0;
1330 return (void *) sch;
1332 memset(&console_cdev, 0, sizeof(struct ccw_device));
1333 memset(&console_private, 0, sizeof(struct ccw_device_private));
1334 console_cdev.private = &console_private;
1335 console_private.cdev = &console_cdev;
1336 ret = ccw_device_console_enable(&console_cdev, sch);
1338 cio_release_console();
1339 console_cdev_in_use = 0;
1340 return ERR_PTR(ret);
1342 console_cdev.online = 1;
1343 return &console_cdev;
1348 * get ccw_device matching the busid, but only if owned by cdrv
1351 __ccwdev_check_busid(struct device *dev, void *id)
1357 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1362 * get_ccwdev_by_busid() - obtain device from a bus id
1363 * @cdrv: driver the device is owned by
1364 * @bus_id: bus id of the device to be searched
1366 * This function searches all devices owned by @cdrv for a device with a bus
1367 * id matching @bus_id.
1369 * If a match is found, its reference count of the found device is increased
1370 * and it is returned; else %NULL is returned.
1372 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1376 struct device_driver *drv;
1378 drv = get_driver(&cdrv->driver);
1382 dev = driver_find_device(drv, NULL, (void *)bus_id,
1383 __ccwdev_check_busid);
1386 return dev ? to_ccwdev(dev) : NULL;
1389 /************************** device driver handling ************************/
1391 /* This is the implementation of the ccw_driver class. The probe, remove
1392 * and release methods are initially very similar to the device_driver
1393 * implementations, with the difference that they have ccw_device
1396 * A ccw driver also contains the information that is needed for
1400 ccw_device_probe (struct device *dev)
1402 struct ccw_device *cdev = to_ccwdev(dev);
1403 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1406 cdev->drv = cdrv; /* to let the driver call _set_online */
1408 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1419 ccw_device_remove (struct device *dev)
1421 struct ccw_device *cdev = to_ccwdev(dev);
1422 struct ccw_driver *cdrv = cdev->drv;
1429 spin_lock_irq(cdev->ccwlock);
1430 ret = ccw_device_offline(cdev);
1431 spin_unlock_irq(cdev->ccwlock);
1433 wait_event(cdev->private->wait_q,
1434 dev_fsm_final_state(cdev));
1436 //FIXME: we can't fail!
1437 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1438 "device 0.%x.%04x\n",
1439 ret, cdev->private->dev_id.ssid,
1440 cdev->private->dev_id.devno);
1442 ccw_device_set_timeout(cdev, 0);
1447 static void ccw_device_shutdown(struct device *dev)
1449 struct ccw_device *cdev;
1451 cdev = to_ccwdev(dev);
1452 if (cdev->drv && cdev->drv->shutdown)
1453 cdev->drv->shutdown(cdev);
1457 struct bus_type ccw_bus_type = {
1459 .match = ccw_bus_match,
1460 .uevent = ccw_uevent,
1461 .probe = ccw_device_probe,
1462 .remove = ccw_device_remove,
1463 .shutdown = ccw_device_shutdown,
1467 * ccw_driver_register() - register a ccw driver
1468 * @cdriver: driver to be registered
1470 * This function is mainly a wrapper around driver_register().
1472 * %0 on success and a negative error value on failure.
1474 int ccw_driver_register(struct ccw_driver *cdriver)
1476 struct device_driver *drv = &cdriver->driver;
1478 drv->bus = &ccw_bus_type;
1479 drv->name = cdriver->name;
1480 drv->owner = cdriver->owner;
1482 return driver_register(drv);
1486 * ccw_driver_unregister() - deregister a ccw driver
1487 * @cdriver: driver to be deregistered
1489 * This function is mainly a wrapper around driver_unregister().
1491 void ccw_driver_unregister(struct ccw_driver *cdriver)
1493 driver_unregister(&cdriver->driver);
1496 /* Helper func for qdio. */
1497 struct subchannel_id
1498 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1500 struct subchannel *sch;
1502 sch = to_subchannel(cdev->dev.parent);
1506 MODULE_LICENSE("GPL");
1507 EXPORT_SYMBOL(ccw_device_set_online);
1508 EXPORT_SYMBOL(ccw_device_set_offline);
1509 EXPORT_SYMBOL(ccw_driver_register);
1510 EXPORT_SYMBOL(ccw_driver_unregister);
1511 EXPORT_SYMBOL(get_ccwdev_by_busid);
1512 EXPORT_SYMBOL(ccw_bus_type);
1513 EXPORT_SYMBOL(ccw_device_work);
1514 EXPORT_SYMBOL(ccw_device_notify_work);
1515 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);