2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
26 #include "cio_debug.h"
31 /******************* bus type handling ***********************/
33 /* The Linux driver model distinguishes between a bus type and
34 * the bus itself. Of course we only have one channel
35 * subsystem driver and one channel system per machine, but
36 * we still use the abstraction. T.R. says it's a good idea. */
38 ccw_bus_match (struct device * dev, struct device_driver * drv)
40 struct ccw_device *cdev = to_ccwdev(dev);
41 struct ccw_driver *cdrv = to_ccwdrv(drv);
42 const struct ccw_device_id *ids = cdrv->ids, *found;
47 found = ccw_device_id_match(ids, &cdev->id);
51 cdev->id.driver_info = found->driver_info;
56 /* Store modalias string delimited by prefix/suffix string into buffer with
57 * specified size. Return length of resulting string (excluding trailing '\0')
58 * even if string doesn't fit buffer (snprintf semantics). */
59 static int snprint_alias(char *buf, size_t size,
60 struct ccw_device_id *id, const char *suffix)
64 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
70 if (id->dev_type != 0)
71 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
72 id->dev_model, suffix);
74 len += snprintf(buf, size, "dtdm%s", suffix);
79 /* Set up environment variables for ccw device uevent. Return 0 on success,
80 * non-zero otherwise. */
81 static int ccw_uevent(struct device *dev, char **envp, int num_envp,
82 char *buffer, int buffer_size)
84 struct ccw_device *cdev = to_ccwdev(dev);
85 struct ccw_device_id *id = &(cdev->id);
89 char modalias_buf[30];
92 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "CU_TYPE=%04X", id->cu_type);
98 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
99 "CU_MODEL=%02X", id->cu_model);
103 /* The next two can be zero, that's ok for us */
105 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
106 "DEV_TYPE=%04X", id->dev_type);
111 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
112 "DEV_MODEL=%02X", id->dev_model);
117 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
118 ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
119 "MODALIAS=%s", modalias_buf);
123 struct bus_type ccw_bus_type;
125 static int io_subchannel_probe (struct subchannel *);
126 static int io_subchannel_remove (struct subchannel *);
127 static int io_subchannel_notify(struct device *, int);
128 static void io_subchannel_verify(struct device *);
129 static void io_subchannel_ioterm(struct device *);
130 static void io_subchannel_shutdown(struct subchannel *);
132 static struct css_driver io_subchannel_driver = {
133 .subchannel_type = SUBCHANNEL_TYPE_IO,
135 .name = "io_subchannel",
136 .bus = &css_bus_type,
138 .irq = io_subchannel_irq,
139 .notify = io_subchannel_notify,
140 .verify = io_subchannel_verify,
141 .termination = io_subchannel_ioterm,
142 .probe = io_subchannel_probe,
143 .remove = io_subchannel_remove,
144 .shutdown = io_subchannel_shutdown,
147 struct workqueue_struct *ccw_device_work;
148 struct workqueue_struct *ccw_device_notify_work;
149 wait_queue_head_t ccw_device_init_wq;
150 atomic_t ccw_device_init_count;
153 init_ccw_bus_type (void)
157 init_waitqueue_head(&ccw_device_init_wq);
158 atomic_set(&ccw_device_init_count, 0);
160 ccw_device_work = create_singlethread_workqueue("cio");
161 if (!ccw_device_work)
162 return -ENOMEM; /* FIXME: better errno ? */
163 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
164 if (!ccw_device_notify_work) {
165 ret = -ENOMEM; /* FIXME: better errno ? */
168 slow_path_wq = create_singlethread_workqueue("kslowcrw");
170 ret = -ENOMEM; /* FIXME: better errno ? */
173 if ((ret = bus_register (&ccw_bus_type)))
176 if ((ret = driver_register(&io_subchannel_driver.drv)))
179 wait_event(ccw_device_init_wq,
180 atomic_read(&ccw_device_init_count) == 0);
181 flush_workqueue(ccw_device_work);
185 destroy_workqueue(ccw_device_work);
186 if (ccw_device_notify_work)
187 destroy_workqueue(ccw_device_notify_work);
189 destroy_workqueue(slow_path_wq);
194 cleanup_ccw_bus_type (void)
196 driver_unregister(&io_subchannel_driver.drv);
197 bus_unregister(&ccw_bus_type);
198 destroy_workqueue(ccw_device_notify_work);
199 destroy_workqueue(ccw_device_work);
202 subsys_initcall(init_ccw_bus_type);
203 module_exit(cleanup_ccw_bus_type);
205 /************************ device handling **************************/
208 * A ccw_device has some interfaces in sysfs in addition to the
210 * The following entries are designed to export the information which
211 * resided in 2.4 in /proc/subchannels. Subchannel and device number
212 * are obvious, so they don't have an entry :)
213 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
216 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
218 struct subchannel *sch = to_subchannel(dev);
219 struct chsc_ssd_info *ssd = &sch->ssd_info;
224 for (chp = 0; chp < 8; chp++) {
226 if (ssd->path_mask & mask)
227 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229 ret += sprintf(buf + ret, "00 ");
231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
236 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
238 struct subchannel *sch = to_subchannel(dev);
239 struct pmcw *pmcw = &sch->schib.pmcw;
241 return sprintf (buf, "%02x %02x %02x\n",
242 pmcw->pim, pmcw->pam, pmcw->pom);
246 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
248 struct ccw_device *cdev = to_ccwdev(dev);
249 struct ccw_device_id *id = &(cdev->id);
251 if (id->dev_type != 0)
252 return sprintf(buf, "%04x/%02x\n",
253 id->dev_type, id->dev_model);
255 return sprintf(buf, "n/a\n");
259 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
261 struct ccw_device *cdev = to_ccwdev(dev);
262 struct ccw_device_id *id = &(cdev->id);
264 return sprintf(buf, "%04x/%02x\n",
265 id->cu_type, id->cu_model);
269 modalias_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);
275 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
281 online_show (struct device *dev, struct device_attribute *attr, char *buf)
283 struct ccw_device *cdev = to_ccwdev(dev);
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
288 int ccw_device_is_orphan(struct ccw_device *cdev)
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293 static void ccw_device_unregister(struct ccw_device *cdev)
295 if (test_and_clear_bit(1, &cdev->private->registered))
296 device_del(&cdev->dev);
299 static void ccw_device_remove_orphan_cb(struct device *dev)
301 struct ccw_device *cdev = to_ccwdev(dev);
303 ccw_device_unregister(cdev);
304 put_device(&cdev->dev);
307 static void ccw_device_remove_sch_cb(struct device *dev)
309 struct subchannel *sch;
311 sch = to_subchannel(dev);
312 css_sch_device_unregister(sch);
313 /* Reset intparm to zeroes. */
314 sch->schib.pmcw.intparm = 0;
316 put_device(&sch->dev);
320 ccw_device_remove_disconnected(struct ccw_device *cdev)
326 * Forced offline in disconnected state means
327 * 'throw away device'.
329 if (ccw_device_is_orphan(cdev)) {
331 * Deregister ccw device.
332 * Unfortunately, we cannot do this directly from the
335 spin_lock_irqsave(cdev->ccwlock, flags);
336 cdev->private->state = DEV_STATE_NOT_OPER;
337 spin_unlock_irqrestore(cdev->ccwlock, flags);
338 rc = device_schedule_callback(&cdev->dev,
339 ccw_device_remove_orphan_cb);
341 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
343 cdev->private->dev_id.ssid,
344 cdev->private->dev_id.devno);
347 /* Deregister subchannel, which will kill the ccw device. */
348 rc = device_schedule_callback(cdev->dev.parent,
349 ccw_device_remove_sch_cb);
351 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
353 cdev->private->dev_id.ssid,
354 cdev->private->dev_id.devno);
358 ccw_device_set_offline(struct ccw_device *cdev)
364 if (!cdev->online || !cdev->drv)
367 if (cdev->drv->set_offline) {
368 ret = cdev->drv->set_offline(cdev);
373 spin_lock_irq(cdev->ccwlock);
374 ret = ccw_device_offline(cdev);
375 if (ret == -ENODEV) {
376 if (cdev->private->state != DEV_STATE_NOT_OPER) {
377 cdev->private->state = DEV_STATE_OFFLINE;
378 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
380 spin_unlock_irq(cdev->ccwlock);
383 spin_unlock_irq(cdev->ccwlock);
385 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
387 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
388 "device 0.%x.%04x\n",
389 ret, cdev->private->dev_id.ssid,
390 cdev->private->dev_id.devno);
397 ccw_device_set_online(struct ccw_device *cdev)
403 if (cdev->online || !cdev->drv)
406 spin_lock_irq(cdev->ccwlock);
407 ret = ccw_device_online(cdev);
408 spin_unlock_irq(cdev->ccwlock);
410 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
412 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
413 "device 0.%x.%04x\n",
414 ret, cdev->private->dev_id.ssid,
415 cdev->private->dev_id.devno);
418 if (cdev->private->state != DEV_STATE_ONLINE)
420 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
424 spin_lock_irq(cdev->ccwlock);
425 ret = ccw_device_offline(cdev);
426 spin_unlock_irq(cdev->ccwlock);
428 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
430 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
431 "device 0.%x.%04x\n",
432 ret, cdev->private->dev_id.ssid,
433 cdev->private->dev_id.devno);
434 return (ret == 0) ? -ENODEV : ret;
437 static void online_store_handle_offline(struct ccw_device *cdev)
439 if (cdev->private->state == DEV_STATE_DISCONNECTED)
440 ccw_device_remove_disconnected(cdev);
441 else if (cdev->drv && cdev->drv->set_offline)
442 ccw_device_set_offline(cdev);
445 static int online_store_recog_and_online(struct ccw_device *cdev)
449 /* Do device recognition, if needed. */
450 if (cdev->id.cu_type == 0) {
451 ret = ccw_device_recognition(cdev);
453 CIO_MSG_EVENT(0, "Couldn't start recognition "
454 "for device 0.%x.%04x (ret=%d)\n",
455 cdev->private->dev_id.ssid,
456 cdev->private->dev_id.devno, ret);
459 wait_event(cdev->private->wait_q,
460 cdev->private->flags.recog_done);
462 if (cdev->drv && cdev->drv->set_online)
463 ccw_device_set_online(cdev);
466 static void online_store_handle_online(struct ccw_device *cdev, int force)
470 ret = online_store_recog_and_online(cdev);
473 if (force && cdev->private->state == DEV_STATE_BOXED) {
474 ret = ccw_device_stlck(cdev);
477 "ccw_device_stlck returned %d!\n", ret);
480 if (cdev->id.cu_type == 0)
481 cdev->private->state = DEV_STATE_NOT_OPER;
482 online_store_recog_and_online(cdev);
487 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
488 const char *buf, size_t count)
490 struct ccw_device *cdev = to_ccwdev(dev);
494 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
497 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
498 atomic_set(&cdev->private->onoff, 0);
501 if (!strncmp(buf, "force\n", count)) {
506 i = simple_strtoul(buf, &tmp, 16);
511 online_store_handle_offline(cdev);
514 online_store_handle_online(cdev, force);
520 module_put(cdev->drv->owner);
521 atomic_set(&cdev->private->onoff, 0);
526 available_show (struct device *dev, struct device_attribute *attr, char *buf)
528 struct ccw_device *cdev = to_ccwdev(dev);
529 struct subchannel *sch;
531 if (ccw_device_is_orphan(cdev))
532 return sprintf(buf, "no device\n");
533 switch (cdev->private->state) {
534 case DEV_STATE_BOXED:
535 return sprintf(buf, "boxed\n");
536 case DEV_STATE_DISCONNECTED:
537 case DEV_STATE_DISCONNECTED_SENSE_ID:
538 case DEV_STATE_NOT_OPER:
539 sch = to_subchannel(dev->parent);
541 return sprintf(buf, "no path\n");
543 return sprintf(buf, "no device\n");
545 /* All other states considered fine. */
546 return sprintf(buf, "good\n");
550 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
551 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
552 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
553 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
554 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
555 static DEVICE_ATTR(online, 0644, online_show, online_store);
556 extern struct device_attribute dev_attr_cmb_enable;
557 static DEVICE_ATTR(availability, 0444, available_show, NULL);
559 static struct attribute * subch_attrs[] = {
560 &dev_attr_chpids.attr,
561 &dev_attr_pimpampom.attr,
565 static struct attribute_group subch_attr_group = {
566 .attrs = subch_attrs,
569 struct attribute_group *subch_attr_groups[] = {
574 static struct attribute * ccwdev_attrs[] = {
575 &dev_attr_devtype.attr,
576 &dev_attr_cutype.attr,
577 &dev_attr_modalias.attr,
578 &dev_attr_online.attr,
579 &dev_attr_cmb_enable.attr,
580 &dev_attr_availability.attr,
584 static struct attribute_group ccwdev_attr_group = {
585 .attrs = ccwdev_attrs,
588 static struct attribute_group *ccwdev_attr_groups[] = {
593 /* this is a simple abstraction for device_register that sets the
594 * correct bus type and adds the bus specific files */
595 static int ccw_device_register(struct ccw_device *cdev)
597 struct device *dev = &cdev->dev;
600 dev->bus = &ccw_bus_type;
602 if ((ret = device_add(dev)))
605 set_bit(1, &cdev->private->registered);
610 struct ccw_dev_id dev_id;
611 struct ccw_device * sibling;
615 match_devno(struct device * dev, void * data)
617 struct match_data * d = data;
618 struct ccw_device * cdev;
620 cdev = to_ccwdev(dev);
621 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
622 !ccw_device_is_orphan(cdev) &&
623 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
624 (cdev != d->sibling))
629 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
630 struct ccw_device *sibling)
633 struct match_data data;
635 data.dev_id = *dev_id;
636 data.sibling = sibling;
637 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
639 return dev ? to_ccwdev(dev) : NULL;
642 static int match_orphan(struct device *dev, void *data)
644 struct ccw_dev_id *dev_id;
645 struct ccw_device *cdev;
648 cdev = to_ccwdev(dev);
649 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
652 static struct ccw_device *
653 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
654 struct ccw_dev_id *dev_id)
658 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
661 return dev ? to_ccwdev(dev) : NULL;
665 ccw_device_add_changed(struct work_struct *work)
667 struct ccw_device_private *priv;
668 struct ccw_device *cdev;
670 priv = container_of(work, struct ccw_device_private, kick_work);
672 if (device_add(&cdev->dev)) {
673 put_device(&cdev->dev);
676 set_bit(1, &cdev->private->registered);
679 void ccw_device_do_unreg_rereg(struct work_struct *work)
681 struct ccw_device_private *priv;
682 struct ccw_device *cdev;
683 struct subchannel *sch;
685 priv = container_of(work, struct ccw_device_private, kick_work);
687 sch = to_subchannel(cdev->dev.parent);
689 ccw_device_unregister(cdev);
690 PREPARE_WORK(&cdev->private->kick_work,
691 ccw_device_add_changed);
692 queue_work(ccw_device_work, &cdev->private->kick_work);
696 ccw_device_release(struct device *dev)
698 struct ccw_device *cdev;
700 cdev = to_ccwdev(dev);
701 kfree(cdev->private);
705 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
707 struct ccw_device *cdev;
709 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
711 cdev->private = kzalloc(sizeof(struct ccw_device_private),
712 GFP_KERNEL | GFP_DMA);
717 return ERR_PTR(-ENOMEM);
720 static int io_subchannel_initialize_dev(struct subchannel *sch,
721 struct ccw_device *cdev)
723 cdev->private->cdev = cdev;
724 atomic_set(&cdev->private->onoff, 0);
725 cdev->dev.parent = &sch->dev;
726 cdev->dev.release = ccw_device_release;
727 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
728 cdev->dev.groups = ccwdev_attr_groups;
729 /* Do first half of device_register. */
730 device_initialize(&cdev->dev);
731 if (!get_device(&sch->dev)) {
732 if (cdev->dev.release)
733 cdev->dev.release(&cdev->dev);
739 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
741 struct ccw_device *cdev;
744 cdev = io_subchannel_allocate_dev(sch);
746 ret = io_subchannel_initialize_dev(sch, cdev);
755 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
757 static void sch_attach_device(struct subchannel *sch,
758 struct ccw_device *cdev)
760 css_update_ssd_info(sch);
761 spin_lock_irq(sch->lock);
762 sch->dev.driver_data = cdev;
763 cdev->private->schid = sch->schid;
764 cdev->ccwlock = sch->lock;
765 device_trigger_reprobe(sch);
766 spin_unlock_irq(sch->lock);
769 static void sch_attach_disconnected_device(struct subchannel *sch,
770 struct ccw_device *cdev)
772 struct subchannel *other_sch;
775 other_sch = to_subchannel(get_device(cdev->dev.parent));
776 ret = device_move(&cdev->dev, &sch->dev);
778 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
779 "(ret=%d)!\n", cdev->private->dev_id.ssid,
780 cdev->private->dev_id.devno, ret);
781 put_device(&other_sch->dev);
784 other_sch->dev.driver_data = NULL;
785 /* No need to keep a subchannel without ccw device around. */
786 css_sch_device_unregister(other_sch);
787 put_device(&other_sch->dev);
788 sch_attach_device(sch, cdev);
791 static void sch_attach_orphaned_device(struct subchannel *sch,
792 struct ccw_device *cdev)
796 /* Try to move the ccw device to its new subchannel. */
797 ret = device_move(&cdev->dev, &sch->dev);
799 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
800 "failed (ret=%d)!\n",
801 cdev->private->dev_id.ssid,
802 cdev->private->dev_id.devno, ret);
805 sch_attach_device(sch, cdev);
808 static void sch_create_and_recog_new_device(struct subchannel *sch)
810 struct ccw_device *cdev;
812 /* Need to allocate a new ccw device. */
813 cdev = io_subchannel_create_ccwdev(sch);
815 /* OK, we did everything we could... */
816 css_sch_device_unregister(sch);
819 spin_lock_irq(sch->lock);
820 sch->dev.driver_data = cdev;
821 spin_unlock_irq(sch->lock);
822 /* Start recognition for the new ccw device. */
823 if (io_subchannel_recog(cdev, sch)) {
824 spin_lock_irq(sch->lock);
825 sch->dev.driver_data = NULL;
826 spin_unlock_irq(sch->lock);
827 if (cdev->dev.release)
828 cdev->dev.release(&cdev->dev);
829 css_sch_device_unregister(sch);
834 void ccw_device_move_to_orphanage(struct work_struct *work)
836 struct ccw_device_private *priv;
837 struct ccw_device *cdev;
838 struct ccw_device *replacing_cdev;
839 struct subchannel *sch;
841 struct channel_subsystem *css;
842 struct ccw_dev_id dev_id;
844 priv = container_of(work, struct ccw_device_private, kick_work);
846 sch = to_subchannel(cdev->dev.parent);
847 css = to_css(sch->dev.parent);
848 dev_id.devno = sch->schib.pmcw.dev;
849 dev_id.ssid = sch->schid.ssid;
852 * Move the orphaned ccw device to the orphanage so the replacing
853 * ccw device can take its place on the subchannel.
855 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
857 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
858 "(ret=%d)!\n", cdev->private->dev_id.ssid,
859 cdev->private->dev_id.devno, ret);
862 cdev->ccwlock = css->pseudo_subchannel->lock;
864 * Search for the replacing ccw device
865 * - among the disconnected devices
868 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
869 if (replacing_cdev) {
870 sch_attach_disconnected_device(sch, replacing_cdev);
873 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
874 if (replacing_cdev) {
875 sch_attach_orphaned_device(sch, replacing_cdev);
878 sch_create_and_recog_new_device(sch);
882 * Register recognized device.
885 io_subchannel_register(struct work_struct *work)
887 struct ccw_device_private *priv;
888 struct ccw_device *cdev;
889 struct subchannel *sch;
893 priv = container_of(work, struct ccw_device_private, kick_work);
895 sch = to_subchannel(cdev->dev.parent);
896 css_update_ssd_info(sch);
898 * io_subchannel_register() will also be called after device
899 * recognition has been done for a boxed device (which will already
900 * be registered). We need to reprobe since we may now have sense id
903 if (klist_node_attached(&cdev->dev.knode_parent)) {
905 ret = device_reprobe(&cdev->dev);
907 /* We can't do much here. */
908 CIO_MSG_EVENT(2, "device_reprobe() returned"
909 " %d for 0.%x.%04x\n", ret,
910 cdev->private->dev_id.ssid,
911 cdev->private->dev_id.devno);
916 * Now we know this subchannel will stay, we can throw
917 * our delayed uevent.
919 sch->dev.uevent_suppress = 0;
920 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
921 /* make it known to the system */
922 ret = ccw_device_register(cdev);
924 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
925 cdev->private->dev_id.ssid,
926 cdev->private->dev_id.devno, ret);
927 put_device(&cdev->dev);
928 spin_lock_irqsave(sch->lock, flags);
929 sch->dev.driver_data = NULL;
930 spin_unlock_irqrestore(sch->lock, flags);
931 kfree (cdev->private);
933 put_device(&sch->dev);
934 if (atomic_dec_and_test(&ccw_device_init_count))
935 wake_up(&ccw_device_init_wq);
938 put_device(&cdev->dev);
940 cdev->private->flags.recog_done = 1;
941 put_device(&sch->dev);
942 wake_up(&cdev->private->wait_q);
943 if (atomic_dec_and_test(&ccw_device_init_count))
944 wake_up(&ccw_device_init_wq);
948 ccw_device_call_sch_unregister(struct work_struct *work)
950 struct ccw_device_private *priv;
951 struct ccw_device *cdev;
952 struct subchannel *sch;
954 priv = container_of(work, struct ccw_device_private, kick_work);
956 sch = to_subchannel(cdev->dev.parent);
957 css_sch_device_unregister(sch);
958 /* Reset intparm to zeroes. */
959 sch->schib.pmcw.intparm = 0;
961 put_device(&cdev->dev);
962 put_device(&sch->dev);
966 * subchannel recognition done. Called from the state machine.
969 io_subchannel_recog_done(struct ccw_device *cdev)
971 struct subchannel *sch;
973 if (css_init_done == 0) {
974 cdev->private->flags.recog_done = 1;
977 switch (cdev->private->state) {
978 case DEV_STATE_NOT_OPER:
979 cdev->private->flags.recog_done = 1;
980 /* Remove device found not operational. */
981 if (!get_device(&cdev->dev))
983 sch = to_subchannel(cdev->dev.parent);
984 PREPARE_WORK(&cdev->private->kick_work,
985 ccw_device_call_sch_unregister);
986 queue_work(slow_path_wq, &cdev->private->kick_work);
987 if (atomic_dec_and_test(&ccw_device_init_count))
988 wake_up(&ccw_device_init_wq);
990 case DEV_STATE_BOXED:
991 /* Device did not respond in time. */
992 case DEV_STATE_OFFLINE:
994 * We can't register the device in interrupt context so
995 * we schedule a work item.
997 if (!get_device(&cdev->dev))
999 PREPARE_WORK(&cdev->private->kick_work,
1000 io_subchannel_register);
1001 queue_work(slow_path_wq, &cdev->private->kick_work);
1007 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1010 struct ccw_device_private *priv;
1012 sch->dev.driver_data = cdev;
1013 sch->driver = &io_subchannel_driver;
1014 cdev->ccwlock = sch->lock;
1016 /* Init private data. */
1017 priv = cdev->private;
1018 priv->dev_id.devno = sch->schib.pmcw.dev;
1019 priv->dev_id.ssid = sch->schid.ssid;
1020 priv->schid = sch->schid;
1021 priv->state = DEV_STATE_NOT_OPER;
1022 INIT_LIST_HEAD(&priv->cmb_list);
1023 init_waitqueue_head(&priv->wait_q);
1024 init_timer(&priv->timer);
1026 /* Set an initial name for the device. */
1027 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1028 sch->schid.ssid, sch->schib.pmcw.dev);
1030 /* Increase counter of devices currently in recognition. */
1031 atomic_inc(&ccw_device_init_count);
1033 /* Start async. device sensing. */
1034 spin_lock_irq(sch->lock);
1035 rc = ccw_device_recognition(cdev);
1036 spin_unlock_irq(sch->lock);
1038 if (atomic_dec_and_test(&ccw_device_init_count))
1039 wake_up(&ccw_device_init_wq);
1044 static void ccw_device_move_to_sch(struct work_struct *work)
1046 struct ccw_device_private *priv;
1048 struct subchannel *sch;
1049 struct ccw_device *cdev;
1050 struct subchannel *former_parent;
1052 priv = container_of(work, struct ccw_device_private, kick_work);
1055 former_parent = ccw_device_is_orphan(cdev) ?
1056 NULL : to_subchannel(get_device(cdev->dev.parent));
1057 mutex_lock(&sch->reg_mutex);
1058 /* Try to move the ccw device to its new subchannel. */
1059 rc = device_move(&cdev->dev, &sch->dev);
1060 mutex_unlock(&sch->reg_mutex);
1062 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1063 "0.%x.%04x failed (ret=%d)!\n",
1064 cdev->private->dev_id.ssid,
1065 cdev->private->dev_id.devno, sch->schid.ssid,
1066 sch->schid.sch_no, rc);
1067 css_sch_device_unregister(sch);
1070 if (former_parent) {
1071 spin_lock_irq(former_parent->lock);
1072 former_parent->dev.driver_data = NULL;
1073 spin_unlock_irq(former_parent->lock);
1074 css_sch_device_unregister(former_parent);
1075 /* Reset intparm to zeroes. */
1076 former_parent->schib.pmcw.intparm = 0;
1077 cio_modify(former_parent);
1079 sch_attach_device(sch, cdev);
1082 put_device(&former_parent->dev);
1083 put_device(&cdev->dev);
1087 io_subchannel_probe (struct subchannel *sch)
1089 struct ccw_device *cdev;
1091 unsigned long flags;
1092 struct ccw_dev_id dev_id;
1094 if (sch->dev.driver_data) {
1096 * This subchannel already has an associated ccw_device.
1097 * Register it and exit. This happens for all early
1098 * device, e.g. the console.
1100 cdev = sch->dev.driver_data;
1101 device_initialize(&cdev->dev);
1102 ccw_device_register(cdev);
1104 * Check if the device is already online. If it is
1105 * the reference count needs to be corrected
1106 * (see ccw_device_online and css_init_done for the
1109 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1110 cdev->private->state != DEV_STATE_OFFLINE &&
1111 cdev->private->state != DEV_STATE_BOXED)
1112 get_device(&cdev->dev);
1116 * First check if a fitting device may be found amongst the
1117 * disconnected devices or in the orphanage.
1119 dev_id.devno = sch->schib.pmcw.dev;
1120 dev_id.ssid = sch->schid.ssid;
1121 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1123 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1127 * Schedule moving the device until when we have a registered
1128 * subchannel to move to and succeed the probe. We can
1129 * unregister later again, when the probe is through.
1131 cdev->private->sch = sch;
1132 PREPARE_WORK(&cdev->private->kick_work,
1133 ccw_device_move_to_sch);
1134 queue_work(slow_path_wq, &cdev->private->kick_work);
1137 cdev = io_subchannel_create_ccwdev(sch);
1139 return PTR_ERR(cdev);
1141 rc = io_subchannel_recog(cdev, sch);
1143 spin_lock_irqsave(sch->lock, flags);
1144 sch->dev.driver_data = NULL;
1145 spin_unlock_irqrestore(sch->lock, flags);
1146 if (cdev->dev.release)
1147 cdev->dev.release(&cdev->dev);
1154 io_subchannel_remove (struct subchannel *sch)
1156 struct ccw_device *cdev;
1157 unsigned long flags;
1159 if (!sch->dev.driver_data)
1161 cdev = sch->dev.driver_data;
1162 /* Set ccw device to not operational and drop reference. */
1163 spin_lock_irqsave(cdev->ccwlock, flags);
1164 sch->dev.driver_data = NULL;
1165 cdev->private->state = DEV_STATE_NOT_OPER;
1166 spin_unlock_irqrestore(cdev->ccwlock, flags);
1167 ccw_device_unregister(cdev);
1168 put_device(&cdev->dev);
1173 io_subchannel_notify(struct device *dev, int event)
1175 struct ccw_device *cdev;
1177 cdev = dev->driver_data;
1184 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1188 io_subchannel_verify(struct device *dev)
1190 struct ccw_device *cdev;
1192 cdev = dev->driver_data;
1194 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1198 io_subchannel_ioterm(struct device *dev)
1200 struct ccw_device *cdev;
1202 cdev = dev->driver_data;
1205 /* Internal I/O will be retried by the interrupt handler. */
1206 if (cdev->private->flags.intretry)
1208 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1210 cdev->handler(cdev, cdev->private->intparm,
1215 io_subchannel_shutdown(struct subchannel *sch)
1217 struct ccw_device *cdev;
1220 cdev = sch->dev.driver_data;
1222 if (cio_is_console(sch->schid))
1224 if (!sch->schib.pmcw.ena)
1225 /* Nothing to do. */
1227 ret = cio_disable_subchannel(sch);
1229 /* Subchannel is disabled, we're done. */
1231 cdev->private->state = DEV_STATE_QUIESCE;
1233 cdev->handler(cdev, cdev->private->intparm,
1235 ret = ccw_device_cancel_halt_clear(cdev);
1236 if (ret == -EBUSY) {
1237 ccw_device_set_timeout(cdev, HZ/10);
1238 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1240 cio_disable_subchannel(sch);
1243 #ifdef CONFIG_CCW_CONSOLE
1244 static struct ccw_device console_cdev;
1245 static struct ccw_device_private console_private;
1246 static int console_cdev_in_use;
1248 static DEFINE_SPINLOCK(ccw_console_lock);
1250 spinlock_t * cio_get_console_lock(void)
1252 return &ccw_console_lock;
1256 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1260 /* Initialize the ccw_device structure. */
1261 cdev->dev.parent= &sch->dev;
1262 rc = io_subchannel_recog(cdev, sch);
1266 /* Now wait for the async. recognition to come to an end. */
1267 spin_lock_irq(cdev->ccwlock);
1268 while (!dev_fsm_final_state(cdev))
1271 if (cdev->private->state != DEV_STATE_OFFLINE)
1273 ccw_device_online(cdev);
1274 while (!dev_fsm_final_state(cdev))
1276 if (cdev->private->state != DEV_STATE_ONLINE)
1280 spin_unlock_irq(cdev->ccwlock);
1285 ccw_device_probe_console(void)
1287 struct subchannel *sch;
1290 if (xchg(&console_cdev_in_use, 1) != 0)
1291 return ERR_PTR(-EBUSY);
1292 sch = cio_probe_console();
1294 console_cdev_in_use = 0;
1295 return (void *) sch;
1297 memset(&console_cdev, 0, sizeof(struct ccw_device));
1298 memset(&console_private, 0, sizeof(struct ccw_device_private));
1299 console_cdev.private = &console_private;
1300 console_private.cdev = &console_cdev;
1301 ret = ccw_device_console_enable(&console_cdev, sch);
1303 cio_release_console();
1304 console_cdev_in_use = 0;
1305 return ERR_PTR(ret);
1307 console_cdev.online = 1;
1308 return &console_cdev;
1313 * get ccw_device matching the busid, but only if owned by cdrv
1316 __ccwdev_check_busid(struct device *dev, void *id)
1322 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1327 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1330 struct device_driver *drv;
1332 drv = get_driver(&cdrv->driver);
1336 dev = driver_find_device(drv, NULL, (void *)bus_id,
1337 __ccwdev_check_busid);
1340 return dev ? to_ccwdev(dev) : NULL;
1343 /************************** device driver handling ************************/
1345 /* This is the implementation of the ccw_driver class. The probe, remove
1346 * and release methods are initially very similar to the device_driver
1347 * implementations, with the difference that they have ccw_device
1350 * A ccw driver also contains the information that is needed for
1354 ccw_device_probe (struct device *dev)
1356 struct ccw_device *cdev = to_ccwdev(dev);
1357 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1360 cdev->drv = cdrv; /* to let the driver call _set_online */
1362 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1373 ccw_device_remove (struct device *dev)
1375 struct ccw_device *cdev = to_ccwdev(dev);
1376 struct ccw_driver *cdrv = cdev->drv;
1383 spin_lock_irq(cdev->ccwlock);
1384 ret = ccw_device_offline(cdev);
1385 spin_unlock_irq(cdev->ccwlock);
1387 wait_event(cdev->private->wait_q,
1388 dev_fsm_final_state(cdev));
1390 //FIXME: we can't fail!
1391 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1392 "device 0.%x.%04x\n",
1393 ret, cdev->private->dev_id.ssid,
1394 cdev->private->dev_id.devno);
1396 ccw_device_set_timeout(cdev, 0);
1401 struct bus_type ccw_bus_type = {
1403 .match = ccw_bus_match,
1404 .uevent = ccw_uevent,
1405 .probe = ccw_device_probe,
1406 .remove = ccw_device_remove,
1410 ccw_driver_register (struct ccw_driver *cdriver)
1412 struct device_driver *drv = &cdriver->driver;
1414 drv->bus = &ccw_bus_type;
1415 drv->name = cdriver->name;
1417 return driver_register(drv);
1421 ccw_driver_unregister (struct ccw_driver *cdriver)
1423 driver_unregister(&cdriver->driver);
1426 /* Helper func for qdio. */
1427 struct subchannel_id
1428 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1430 struct subchannel *sch;
1432 sch = to_subchannel(cdev->dev.parent);
1436 MODULE_LICENSE("GPL");
1437 EXPORT_SYMBOL(ccw_device_set_online);
1438 EXPORT_SYMBOL(ccw_device_set_offline);
1439 EXPORT_SYMBOL(ccw_driver_register);
1440 EXPORT_SYMBOL(ccw_driver_unregister);
1441 EXPORT_SYMBOL(get_ccwdev_by_busid);
1442 EXPORT_SYMBOL(ccw_bus_type);
1443 EXPORT_SYMBOL(ccw_device_work);
1444 EXPORT_SYMBOL(ccw_device_notify_work);
1445 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);