2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/workqueue.h>
19 #include <linux/timer.h>
21 #include <asm/ccwdev.h>
23 #include <asm/param.h> /* HZ */
29 #include "cio_debug.h"
34 #include "blacklist.h"
36 static struct timer_list recovery_timer;
37 static DEFINE_SPINLOCK(recovery_lock);
38 static int recovery_phase;
39 static const unsigned long recovery_delay[] = { 3, 30, 300 };
41 /******************* bus type handling ***********************/
43 /* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
48 ccw_bus_match (struct device * dev, struct device_driver * drv)
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
57 found = ccw_device_id_match(ids, &cdev->id);
61 cdev->id.driver_info = found->driver_info;
66 /* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
69 static int snprint_alias(char *buf, size_t size,
70 struct ccw_device_id *id, const char *suffix)
74 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
84 len += snprintf(buf, size, "dtdm%s", suffix);
89 /* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
91 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
93 struct ccw_device *cdev = to_ccwdev(dev);
94 struct ccw_device_id *id = &(cdev->id);
96 char modalias_buf[30];
99 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
108 /* The next two can be zero, that's ok for us */
110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
125 struct bus_type ccw_bus_type;
127 static void io_subchannel_irq(struct subchannel *);
128 static int io_subchannel_probe(struct subchannel *);
129 static int io_subchannel_remove(struct subchannel *);
130 static void io_subchannel_shutdown(struct subchannel *);
131 static int io_subchannel_sch_event(struct subchannel *, int);
132 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
135 static struct css_device_id io_subchannel_ids[] = {
136 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137 { /* end of list */ },
139 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
141 static struct css_driver io_subchannel_driver = {
142 .owner = THIS_MODULE,
143 .subchannel_type = io_subchannel_ids,
144 .name = "io_subchannel",
145 .irq = io_subchannel_irq,
146 .sch_event = io_subchannel_sch_event,
147 .chp_event = io_subchannel_chp_event,
148 .probe = io_subchannel_probe,
149 .remove = io_subchannel_remove,
150 .shutdown = io_subchannel_shutdown,
153 struct workqueue_struct *ccw_device_work;
154 wait_queue_head_t ccw_device_init_wq;
155 atomic_t ccw_device_init_count;
157 static void recovery_func(unsigned long data);
160 init_ccw_bus_type (void)
164 init_waitqueue_head(&ccw_device_init_wq);
165 atomic_set(&ccw_device_init_count, 0);
166 setup_timer(&recovery_timer, recovery_func, 0);
168 ccw_device_work = create_singlethread_workqueue("cio");
169 if (!ccw_device_work)
170 return -ENOMEM; /* FIXME: better errno ? */
171 slow_path_wq = create_singlethread_workqueue("kslowcrw");
173 ret = -ENOMEM; /* FIXME: better errno ? */
176 if ((ret = bus_register (&ccw_bus_type)))
179 ret = css_driver_register(&io_subchannel_driver);
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
189 destroy_workqueue(ccw_device_work);
191 destroy_workqueue(slow_path_wq);
196 cleanup_ccw_bus_type (void)
198 css_driver_unregister(&io_subchannel_driver);
199 bus_unregister(&ccw_bus_type);
200 destroy_workqueue(ccw_device_work);
203 subsys_initcall(init_ccw_bus_type);
204 module_exit(cleanup_ccw_bus_type);
206 /************************ device handling **************************/
209 * A ccw_device has some interfaces in sysfs in addition to the
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
217 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
219 struct subchannel *sch = to_subchannel(dev);
220 struct chsc_ssd_info *ssd = &sch->ssd_info;
225 for (chp = 0; chp < 8; chp++) {
227 if (ssd->path_mask & mask)
228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 ret += sprintf(buf + ret, "00 ");
232 ret += sprintf (buf+ret, "\n");
233 return min((ssize_t)PAGE_SIZE, ret);
237 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
239 struct subchannel *sch = to_subchannel(dev);
240 struct pmcw *pmcw = &sch->schib.pmcw;
242 return sprintf (buf, "%02x %02x %02x\n",
243 pmcw->pim, pmcw->pam, pmcw->pom);
247 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
249 struct ccw_device *cdev = to_ccwdev(dev);
250 struct ccw_device_id *id = &(cdev->id);
252 if (id->dev_type != 0)
253 return sprintf(buf, "%04x/%02x\n",
254 id->dev_type, id->dev_model);
256 return sprintf(buf, "n/a\n");
260 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
262 struct ccw_device *cdev = to_ccwdev(dev);
263 struct ccw_device_id *id = &(cdev->id);
265 return sprintf(buf, "%04x/%02x\n",
266 id->cu_type, id->cu_model);
270 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
276 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
278 return len > PAGE_SIZE ? PAGE_SIZE : len;
282 online_show (struct device *dev, struct device_attribute *attr, char *buf)
284 struct ccw_device *cdev = to_ccwdev(dev);
286 return sprintf(buf, cdev->online ? "1\n" : "0\n");
289 int ccw_device_is_orphan(struct ccw_device *cdev)
291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
294 static void ccw_device_unregister(struct ccw_device *cdev)
296 if (test_and_clear_bit(1, &cdev->private->registered))
297 device_del(&cdev->dev);
300 static void ccw_device_remove_orphan_cb(struct work_struct *work)
302 struct ccw_device_private *priv;
303 struct ccw_device *cdev;
305 priv = container_of(work, struct ccw_device_private, kick_work);
307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
309 /* Release cdev reference for workqueue processing. */
310 put_device(&cdev->dev);
313 static void ccw_device_call_sch_unregister(struct work_struct *work);
316 ccw_device_remove_disconnected(struct ccw_device *cdev)
321 * Forced offline in disconnected state means
322 * 'throw away device'.
324 /* Get cdev reference for workqueue processing. */
325 if (!get_device(&cdev->dev))
327 if (ccw_device_is_orphan(cdev)) {
329 * Deregister ccw device.
330 * Unfortunately, we cannot do this directly from the
333 spin_lock_irqsave(cdev->ccwlock, flags);
334 cdev->private->state = DEV_STATE_NOT_OPER;
335 spin_unlock_irqrestore(cdev->ccwlock, flags);
336 PREPARE_WORK(&cdev->private->kick_work,
337 ccw_device_remove_orphan_cb);
339 /* Deregister subchannel, which will kill the ccw device. */
340 PREPARE_WORK(&cdev->private->kick_work,
341 ccw_device_call_sch_unregister);
342 queue_work(slow_path_wq, &cdev->private->kick_work);
346 * ccw_device_set_offline() - disable a ccw device for I/O
347 * @cdev: target ccw device
349 * This function calls the driver's set_offline() function for @cdev, if
350 * given, and then disables @cdev.
352 * %0 on success and a negative error value on failure.
354 * enabled, ccw device lock not held
356 int ccw_device_set_offline(struct ccw_device *cdev)
362 if (!cdev->online || !cdev->drv)
365 if (cdev->drv->set_offline) {
366 ret = cdev->drv->set_offline(cdev);
371 spin_lock_irq(cdev->ccwlock);
372 ret = ccw_device_offline(cdev);
373 if (ret == -ENODEV) {
374 if (cdev->private->state != DEV_STATE_NOT_OPER) {
375 cdev->private->state = DEV_STATE_OFFLINE;
376 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
378 spin_unlock_irq(cdev->ccwlock);
379 /* Give up reference from ccw_device_set_online(). */
380 put_device(&cdev->dev);
383 spin_unlock_irq(cdev->ccwlock);
385 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
386 /* Give up reference from ccw_device_set_online(). */
387 put_device(&cdev->dev);
389 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
390 "device 0.%x.%04x\n",
391 ret, cdev->private->dev_id.ssid,
392 cdev->private->dev_id.devno);
399 * ccw_device_set_online() - enable a ccw device for I/O
400 * @cdev: target ccw device
402 * This function first enables @cdev and then calls the driver's set_online()
403 * function for @cdev, if given. If set_online() returns an error, @cdev is
406 * %0 on success and a negative error value on failure.
408 * enabled, ccw device lock not held
410 int ccw_device_set_online(struct ccw_device *cdev)
416 if (cdev->online || !cdev->drv)
418 /* Hold on to an extra reference while device is online. */
419 if (!get_device(&cdev->dev))
422 spin_lock_irq(cdev->ccwlock);
423 ret = ccw_device_online(cdev);
424 spin_unlock_irq(cdev->ccwlock);
426 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
428 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
429 "device 0.%x.%04x\n",
430 ret, cdev->private->dev_id.ssid,
431 cdev->private->dev_id.devno);
432 /* Give up online reference since onlining failed. */
433 put_device(&cdev->dev);
436 if (cdev->private->state != DEV_STATE_ONLINE) {
437 /* Give up online reference since onlining failed. */
438 put_device(&cdev->dev);
441 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
445 spin_lock_irq(cdev->ccwlock);
446 ret = ccw_device_offline(cdev);
447 spin_unlock_irq(cdev->ccwlock);
449 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
451 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
452 "device 0.%x.%04x\n",
453 ret, cdev->private->dev_id.ssid,
454 cdev->private->dev_id.devno);
455 /* Give up online reference since onlining failed. */
456 put_device(&cdev->dev);
457 return (ret == 0) ? -ENODEV : ret;
460 static int online_store_handle_offline(struct ccw_device *cdev)
462 if (cdev->private->state == DEV_STATE_DISCONNECTED)
463 ccw_device_remove_disconnected(cdev);
464 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
465 return ccw_device_set_offline(cdev);
469 static int online_store_recog_and_online(struct ccw_device *cdev)
473 /* Do device recognition, if needed. */
474 if (cdev->id.cu_type == 0) {
475 ret = ccw_device_recognition(cdev);
477 CIO_MSG_EVENT(0, "Couldn't start recognition "
478 "for device 0.%x.%04x (ret=%d)\n",
479 cdev->private->dev_id.ssid,
480 cdev->private->dev_id.devno, ret);
483 wait_event(cdev->private->wait_q,
484 cdev->private->flags.recog_done);
486 if (cdev->drv && cdev->drv->set_online)
487 ccw_device_set_online(cdev);
490 static int online_store_handle_online(struct ccw_device *cdev, int force)
494 ret = online_store_recog_and_online(cdev);
497 if (force && cdev->private->state == DEV_STATE_BOXED) {
498 ret = ccw_device_stlck(cdev);
501 if (cdev->id.cu_type == 0)
502 cdev->private->state = DEV_STATE_NOT_OPER;
503 online_store_recog_and_online(cdev);
508 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
511 struct ccw_device *cdev = to_ccwdev(dev);
515 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
518 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
519 atomic_set(&cdev->private->onoff, 0);
522 if (!strncmp(buf, "force\n", count)) {
528 ret = strict_strtoul(buf, 16, &i);
534 ret = online_store_handle_offline(cdev);
537 ret = online_store_handle_online(cdev, force);
544 module_put(cdev->drv->owner);
545 atomic_set(&cdev->private->onoff, 0);
546 return (ret < 0) ? ret : count;
550 available_show (struct device *dev, struct device_attribute *attr, char *buf)
552 struct ccw_device *cdev = to_ccwdev(dev);
553 struct subchannel *sch;
555 if (ccw_device_is_orphan(cdev))
556 return sprintf(buf, "no device\n");
557 switch (cdev->private->state) {
558 case DEV_STATE_BOXED:
559 return sprintf(buf, "boxed\n");
560 case DEV_STATE_DISCONNECTED:
561 case DEV_STATE_DISCONNECTED_SENSE_ID:
562 case DEV_STATE_NOT_OPER:
563 sch = to_subchannel(dev->parent);
565 return sprintf(buf, "no path\n");
567 return sprintf(buf, "no device\n");
569 /* All other states considered fine. */
570 return sprintf(buf, "good\n");
574 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
575 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
576 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
577 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
578 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
579 static DEVICE_ATTR(online, 0644, online_show, online_store);
580 static DEVICE_ATTR(availability, 0444, available_show, NULL);
582 static struct attribute *io_subchannel_attrs[] = {
583 &dev_attr_chpids.attr,
584 &dev_attr_pimpampom.attr,
588 static struct attribute_group io_subchannel_attr_group = {
589 .attrs = io_subchannel_attrs,
592 static struct attribute * ccwdev_attrs[] = {
593 &dev_attr_devtype.attr,
594 &dev_attr_cutype.attr,
595 &dev_attr_modalias.attr,
596 &dev_attr_online.attr,
597 &dev_attr_cmb_enable.attr,
598 &dev_attr_availability.attr,
602 static struct attribute_group ccwdev_attr_group = {
603 .attrs = ccwdev_attrs,
606 static struct attribute_group *ccwdev_attr_groups[] = {
611 /* this is a simple abstraction for device_register that sets the
612 * correct bus type and adds the bus specific files */
613 static int ccw_device_register(struct ccw_device *cdev)
615 struct device *dev = &cdev->dev;
618 dev->bus = &ccw_bus_type;
620 if ((ret = device_add(dev)))
623 set_bit(1, &cdev->private->registered);
628 struct ccw_dev_id dev_id;
629 struct ccw_device * sibling;
633 match_devno(struct device * dev, void * data)
635 struct match_data * d = data;
636 struct ccw_device * cdev;
638 cdev = to_ccwdev(dev);
639 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
640 !ccw_device_is_orphan(cdev) &&
641 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
642 (cdev != d->sibling))
647 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
648 struct ccw_device *sibling)
651 struct match_data data;
653 data.dev_id = *dev_id;
654 data.sibling = sibling;
655 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
657 return dev ? to_ccwdev(dev) : NULL;
660 static int match_orphan(struct device *dev, void *data)
662 struct ccw_dev_id *dev_id;
663 struct ccw_device *cdev;
666 cdev = to_ccwdev(dev);
667 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
670 static struct ccw_device *
671 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
672 struct ccw_dev_id *dev_id)
676 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
679 return dev ? to_ccwdev(dev) : NULL;
682 void ccw_device_do_unbind_bind(struct work_struct *work)
684 struct ccw_device_private *priv;
685 struct ccw_device *cdev;
686 struct subchannel *sch;
689 priv = container_of(work, struct ccw_device_private, kick_work);
691 sch = to_subchannel(cdev->dev.parent);
693 if (test_bit(1, &cdev->private->registered)) {
694 device_release_driver(&cdev->dev);
695 ret = device_attach(&cdev->dev);
696 WARN_ON(ret == -ENODEV);
701 ccw_device_release(struct device *dev)
703 struct ccw_device *cdev;
705 cdev = to_ccwdev(dev);
706 /* Release reference of parent subchannel. */
707 put_device(cdev->dev.parent);
708 kfree(cdev->private);
712 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
714 struct ccw_device *cdev;
716 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
718 cdev->private = kzalloc(sizeof(struct ccw_device_private),
719 GFP_KERNEL | GFP_DMA);
724 return ERR_PTR(-ENOMEM);
727 static int io_subchannel_initialize_dev(struct subchannel *sch,
728 struct ccw_device *cdev)
730 cdev->private->cdev = cdev;
731 atomic_set(&cdev->private->onoff, 0);
732 cdev->dev.parent = &sch->dev;
733 cdev->dev.release = ccw_device_release;
734 INIT_WORK(&cdev->private->kick_work, NULL);
735 cdev->dev.groups = ccwdev_attr_groups;
736 /* Do first half of device_register. */
737 device_initialize(&cdev->dev);
738 if (!get_device(&sch->dev)) {
739 /* Release reference from device_initialize(). */
740 put_device(&cdev->dev);
746 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
748 struct ccw_device *cdev;
751 cdev = io_subchannel_allocate_dev(sch);
753 ret = io_subchannel_initialize_dev(sch, cdev);
762 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
764 static void sch_attach_device(struct subchannel *sch,
765 struct ccw_device *cdev)
767 css_update_ssd_info(sch);
768 spin_lock_irq(sch->lock);
769 sch_set_cdev(sch, cdev);
770 cdev->private->schid = sch->schid;
771 cdev->ccwlock = sch->lock;
772 ccw_device_trigger_reprobe(cdev);
773 spin_unlock_irq(sch->lock);
776 static void sch_attach_disconnected_device(struct subchannel *sch,
777 struct ccw_device *cdev)
779 struct subchannel *other_sch;
782 /* Get reference for new parent. */
783 if (!get_device(&sch->dev))
785 other_sch = to_subchannel(cdev->dev.parent);
786 /* Note: device_move() changes cdev->dev.parent */
787 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
789 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
790 "(ret=%d)!\n", cdev->private->dev_id.ssid,
791 cdev->private->dev_id.devno, ret);
792 /* Put reference for new parent. */
793 put_device(&sch->dev);
796 sch_set_cdev(other_sch, NULL);
797 /* No need to keep a subchannel without ccw device around. */
798 css_sch_device_unregister(other_sch);
799 sch_attach_device(sch, cdev);
800 /* Put reference for old parent. */
801 put_device(&other_sch->dev);
804 static void sch_attach_orphaned_device(struct subchannel *sch,
805 struct ccw_device *cdev)
808 struct subchannel *pseudo_sch;
810 /* Get reference for new parent. */
811 if (!get_device(&sch->dev))
813 pseudo_sch = to_subchannel(cdev->dev.parent);
815 * Try to move the ccw device to its new subchannel.
816 * Note: device_move() changes cdev->dev.parent
818 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
820 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
821 "failed (ret=%d)!\n",
822 cdev->private->dev_id.ssid,
823 cdev->private->dev_id.devno, ret);
824 /* Put reference for new parent. */
825 put_device(&sch->dev);
828 sch_attach_device(sch, cdev);
829 /* Put reference on pseudo subchannel. */
830 put_device(&pseudo_sch->dev);
833 static void sch_create_and_recog_new_device(struct subchannel *sch)
835 struct ccw_device *cdev;
837 /* Need to allocate a new ccw device. */
838 cdev = io_subchannel_create_ccwdev(sch);
840 /* OK, we did everything we could... */
841 css_sch_device_unregister(sch);
844 spin_lock_irq(sch->lock);
845 sch_set_cdev(sch, cdev);
846 spin_unlock_irq(sch->lock);
847 /* Start recognition for the new ccw device. */
848 if (io_subchannel_recog(cdev, sch)) {
849 spin_lock_irq(sch->lock);
850 sch_set_cdev(sch, NULL);
851 spin_unlock_irq(sch->lock);
852 css_sch_device_unregister(sch);
853 /* Put reference from io_subchannel_create_ccwdev(). */
854 put_device(&sch->dev);
855 /* Give up initial reference. */
856 put_device(&cdev->dev);
861 void ccw_device_move_to_orphanage(struct work_struct *work)
863 struct ccw_device_private *priv;
864 struct ccw_device *cdev;
865 struct ccw_device *replacing_cdev;
866 struct subchannel *sch;
868 struct channel_subsystem *css;
869 struct ccw_dev_id dev_id;
871 priv = container_of(work, struct ccw_device_private, kick_work);
873 sch = to_subchannel(cdev->dev.parent);
874 css = to_css(sch->dev.parent);
875 dev_id.devno = sch->schib.pmcw.dev;
876 dev_id.ssid = sch->schid.ssid;
878 /* Increase refcount for pseudo subchannel. */
879 get_device(&css->pseudo_subchannel->dev);
881 * Move the orphaned ccw device to the orphanage so the replacing
882 * ccw device can take its place on the subchannel.
883 * Note: device_move() changes cdev->dev.parent
885 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
888 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
889 "(ret=%d)!\n", cdev->private->dev_id.ssid,
890 cdev->private->dev_id.devno, ret);
891 /* Decrease refcount for pseudo subchannel again. */
892 put_device(&css->pseudo_subchannel->dev);
895 cdev->ccwlock = css->pseudo_subchannel->lock;
897 * Search for the replacing ccw device
898 * - among the disconnected devices
901 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
902 if (replacing_cdev) {
903 sch_attach_disconnected_device(sch, replacing_cdev);
904 /* Release reference from get_disc_ccwdev_by_dev_id() */
905 put_device(&replacing_cdev->dev);
906 /* Release reference of subchannel from old cdev. */
907 put_device(&sch->dev);
910 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
911 if (replacing_cdev) {
912 sch_attach_orphaned_device(sch, replacing_cdev);
913 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
914 put_device(&replacing_cdev->dev);
915 /* Release reference of subchannel from old cdev. */
916 put_device(&sch->dev);
919 sch_create_and_recog_new_device(sch);
920 /* Release reference of subchannel from old cdev. */
921 put_device(&sch->dev);
925 * Register recognized device.
928 io_subchannel_register(struct work_struct *work)
930 struct ccw_device_private *priv;
931 struct ccw_device *cdev;
932 struct subchannel *sch;
936 priv = container_of(work, struct ccw_device_private, kick_work);
938 sch = to_subchannel(cdev->dev.parent);
940 * Check if subchannel is still registered. It may have become
941 * unregistered if a machine check hit us after finishing
942 * device recognition but before the register work could be
945 if (!device_is_registered(&sch->dev))
947 css_update_ssd_info(sch);
949 * io_subchannel_register() will also be called after device
950 * recognition has been done for a boxed device (which will already
951 * be registered). We need to reprobe since we may now have sense id
954 if (device_is_registered(&cdev->dev)) {
956 ret = device_reprobe(&cdev->dev);
958 /* We can't do much here. */
959 CIO_MSG_EVENT(0, "device_reprobe() returned"
960 " %d for 0.%x.%04x\n", ret,
961 cdev->private->dev_id.ssid,
962 cdev->private->dev_id.devno);
967 * Now we know this subchannel will stay, we can throw
968 * our delayed uevent.
970 dev_set_uevent_suppress(&sch->dev, 0);
971 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
972 /* make it known to the system */
973 ret = ccw_device_register(cdev);
975 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
976 cdev->private->dev_id.ssid,
977 cdev->private->dev_id.devno, ret);
978 spin_lock_irqsave(sch->lock, flags);
979 sch_set_cdev(sch, NULL);
980 spin_unlock_irqrestore(sch->lock, flags);
981 /* Release initial device reference. */
982 put_device(&cdev->dev);
986 cdev->private->flags.recog_done = 1;
987 wake_up(&cdev->private->wait_q);
989 /* Release reference for workqueue processing. */
990 put_device(&cdev->dev);
991 if (atomic_dec_and_test(&ccw_device_init_count))
992 wake_up(&ccw_device_init_wq);
995 static void ccw_device_call_sch_unregister(struct work_struct *work)
997 struct ccw_device_private *priv;
998 struct ccw_device *cdev;
999 struct subchannel *sch;
1001 priv = container_of(work, struct ccw_device_private, kick_work);
1003 /* Get subchannel reference for local processing. */
1004 if (!get_device(cdev->dev.parent))
1006 sch = to_subchannel(cdev->dev.parent);
1007 css_sch_device_unregister(sch);
1008 /* Reset intparm to zeroes. */
1009 sch->config.intparm = 0;
1010 cio_commit_config(sch);
1011 /* Release cdev reference for workqueue processing.*/
1012 put_device(&cdev->dev);
1013 /* Release subchannel reference for local processing. */
1014 put_device(&sch->dev);
1018 * subchannel recognition done. Called from the state machine.
1021 io_subchannel_recog_done(struct ccw_device *cdev)
1023 if (css_init_done == 0) {
1024 cdev->private->flags.recog_done = 1;
1027 switch (cdev->private->state) {
1028 case DEV_STATE_NOT_OPER:
1029 cdev->private->flags.recog_done = 1;
1030 /* Remove device found not operational. */
1031 if (!get_device(&cdev->dev))
1033 PREPARE_WORK(&cdev->private->kick_work,
1034 ccw_device_call_sch_unregister);
1035 queue_work(slow_path_wq, &cdev->private->kick_work);
1036 if (atomic_dec_and_test(&ccw_device_init_count))
1037 wake_up(&ccw_device_init_wq);
1039 case DEV_STATE_BOXED:
1040 /* Device did not respond in time. */
1041 case DEV_STATE_OFFLINE:
1043 * We can't register the device in interrupt context so
1044 * we schedule a work item.
1046 if (!get_device(&cdev->dev))
1048 PREPARE_WORK(&cdev->private->kick_work,
1049 io_subchannel_register);
1050 queue_work(slow_path_wq, &cdev->private->kick_work);
1056 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1059 struct ccw_device_private *priv;
1061 sch_set_cdev(sch, cdev);
1062 cdev->ccwlock = sch->lock;
1064 /* Init private data. */
1065 priv = cdev->private;
1066 priv->dev_id.devno = sch->schib.pmcw.dev;
1067 priv->dev_id.ssid = sch->schid.ssid;
1068 priv->schid = sch->schid;
1069 priv->state = DEV_STATE_NOT_OPER;
1070 INIT_LIST_HEAD(&priv->cmb_list);
1071 init_waitqueue_head(&priv->wait_q);
1072 init_timer(&priv->timer);
1074 /* Set an initial name for the device. */
1075 if (cio_is_console(sch->schid))
1076 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1078 dev_set_name(&cdev->dev, "0.%x.%04x",
1079 sch->schid.ssid, sch->schib.pmcw.dev);
1081 /* Increase counter of devices currently in recognition. */
1082 atomic_inc(&ccw_device_init_count);
1084 /* Start async. device sensing. */
1085 spin_lock_irq(sch->lock);
1086 rc = ccw_device_recognition(cdev);
1087 spin_unlock_irq(sch->lock);
1089 if (atomic_dec_and_test(&ccw_device_init_count))
1090 wake_up(&ccw_device_init_wq);
1095 static void ccw_device_move_to_sch(struct work_struct *work)
1097 struct ccw_device_private *priv;
1099 struct subchannel *sch;
1100 struct ccw_device *cdev;
1101 struct subchannel *former_parent;
1103 priv = container_of(work, struct ccw_device_private, kick_work);
1106 former_parent = to_subchannel(cdev->dev.parent);
1107 /* Get reference for new parent. */
1108 if (!get_device(&sch->dev))
1110 mutex_lock(&sch->reg_mutex);
1112 * Try to move the ccw device to its new subchannel.
1113 * Note: device_move() changes cdev->dev.parent
1115 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
1116 mutex_unlock(&sch->reg_mutex);
1118 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1119 "0.%x.%04x failed (ret=%d)!\n",
1120 cdev->private->dev_id.ssid,
1121 cdev->private->dev_id.devno, sch->schid.ssid,
1122 sch->schid.sch_no, rc);
1123 css_sch_device_unregister(sch);
1124 /* Put reference for new parent again. */
1125 put_device(&sch->dev);
1128 if (!sch_is_pseudo_sch(former_parent)) {
1129 spin_lock_irq(former_parent->lock);
1130 sch_set_cdev(former_parent, NULL);
1131 spin_unlock_irq(former_parent->lock);
1132 css_sch_device_unregister(former_parent);
1133 /* Reset intparm to zeroes. */
1134 former_parent->config.intparm = 0;
1135 cio_commit_config(former_parent);
1137 sch_attach_device(sch, cdev);
1139 /* Put reference for old parent. */
1140 put_device(&former_parent->dev);
1141 put_device(&cdev->dev);
1144 static void io_subchannel_irq(struct subchannel *sch)
1146 struct ccw_device *cdev;
1148 cdev = sch_get_cdev(sch);
1150 CIO_TRACE_EVENT(3, "IRQ");
1151 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
1153 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1156 void io_subchannel_init_config(struct subchannel *sch)
1158 memset(&sch->config, 0, sizeof(sch->config));
1159 sch->config.csense = 1;
1160 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1161 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1165 static void io_subchannel_init_fields(struct subchannel *sch)
1167 if (cio_is_console(sch->schid))
1170 sch->opm = chp_get_sch_opm(sch);
1171 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1172 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1174 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1175 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1176 sch->schib.pmcw.dev, sch->schid.ssid,
1177 sch->schid.sch_no, sch->schib.pmcw.pim,
1178 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1180 io_subchannel_init_config(sch);
1183 static void io_subchannel_do_unreg(struct work_struct *work)
1185 struct subchannel *sch;
1187 sch = container_of(work, struct subchannel, work);
1188 css_sch_device_unregister(sch);
1189 /* Reset intparm to zeroes. */
1190 sch->config.intparm = 0;
1191 cio_commit_config(sch);
1192 put_device(&sch->dev);
1195 /* Schedule unregister if we have no cdev. */
1196 static void io_subchannel_schedule_removal(struct subchannel *sch)
1198 get_device(&sch->dev);
1199 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1200 queue_work(slow_path_wq, &sch->work);
1204 * Note: We always return 0 so that we bind to the device even on error.
1205 * This is needed so that our remove function is called on unregister.
1207 static int io_subchannel_probe(struct subchannel *sch)
1209 struct ccw_device *cdev;
1211 unsigned long flags;
1212 struct ccw_dev_id dev_id;
1214 cdev = sch_get_cdev(sch);
1216 rc = sysfs_create_group(&sch->dev.kobj,
1217 &io_subchannel_attr_group);
1219 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1220 "attributes for subchannel "
1221 "0.%x.%04x (rc=%d)\n",
1222 sch->schid.ssid, sch->schid.sch_no, rc);
1224 * This subchannel already has an associated ccw_device.
1225 * Throw the delayed uevent for the subchannel, register
1226 * the ccw_device and exit. This happens for all early
1227 * devices, e.g. the console.
1229 dev_set_uevent_suppress(&sch->dev, 0);
1230 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1231 cdev->dev.groups = ccwdev_attr_groups;
1232 device_initialize(&cdev->dev);
1233 ccw_device_register(cdev);
1235 * Check if the device is already online. If it is
1236 * the reference count needs to be corrected since we
1237 * didn't obtain a reference in ccw_device_set_online.
1239 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1240 cdev->private->state != DEV_STATE_OFFLINE &&
1241 cdev->private->state != DEV_STATE_BOXED)
1242 get_device(&cdev->dev);
1245 io_subchannel_init_fields(sch);
1246 rc = cio_commit_config(sch);
1249 rc = sysfs_create_group(&sch->dev.kobj,
1250 &io_subchannel_attr_group);
1253 /* Allocate I/O subchannel private data. */
1254 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1255 GFP_KERNEL | GFP_DMA);
1259 * First check if a fitting device may be found amongst the
1260 * disconnected devices or in the orphanage.
1262 dev_id.devno = sch->schib.pmcw.dev;
1263 dev_id.ssid = sch->schid.ssid;
1264 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1266 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1270 * Schedule moving the device until when we have a registered
1271 * subchannel to move to and succeed the probe. We can
1272 * unregister later again, when the probe is through.
1274 cdev->private->sch = sch;
1275 PREPARE_WORK(&cdev->private->kick_work,
1276 ccw_device_move_to_sch);
1277 queue_work(slow_path_wq, &cdev->private->kick_work);
1280 cdev = io_subchannel_create_ccwdev(sch);
1283 rc = io_subchannel_recog(cdev, sch);
1285 spin_lock_irqsave(sch->lock, flags);
1286 io_subchannel_recog_done(cdev);
1287 spin_unlock_irqrestore(sch->lock, flags);
1291 kfree(sch->private);
1292 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1294 io_subchannel_schedule_removal(sch);
1299 io_subchannel_remove (struct subchannel *sch)
1301 struct ccw_device *cdev;
1302 unsigned long flags;
1304 cdev = sch_get_cdev(sch);
1307 /* Set ccw device to not operational and drop reference. */
1308 spin_lock_irqsave(cdev->ccwlock, flags);
1309 sch_set_cdev(sch, NULL);
1310 cdev->private->state = DEV_STATE_NOT_OPER;
1311 spin_unlock_irqrestore(cdev->ccwlock, flags);
1312 ccw_device_unregister(cdev);
1313 put_device(&cdev->dev);
1314 kfree(sch->private);
1315 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1319 static int io_subchannel_notify(struct subchannel *sch, int event)
1321 struct ccw_device *cdev;
1323 cdev = sch_get_cdev(sch);
1326 return ccw_device_notify(cdev, event);
1329 static void io_subchannel_verify(struct subchannel *sch)
1331 struct ccw_device *cdev;
1333 cdev = sch_get_cdev(sch);
1335 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1338 static int check_for_io_on_path(struct subchannel *sch, int mask)
1340 if (cio_update_schib(sch))
1342 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1347 static void terminate_internal_io(struct subchannel *sch,
1348 struct ccw_device *cdev)
1350 if (cio_clear(sch)) {
1351 /* Recheck device in case clear failed. */
1354 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1356 css_schedule_eval(sch->schid);
1359 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1360 /* Request retry of internal operation. */
1361 cdev->private->flags.intretry = 1;
1364 cdev->handler(cdev, cdev->private->intparm,
1368 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1370 struct ccw_device *cdev;
1372 cdev = sch_get_cdev(sch);
1375 if (check_for_io_on_path(sch, mask)) {
1376 if (cdev->private->state == DEV_STATE_ONLINE)
1377 ccw_device_kill_io(cdev);
1379 terminate_internal_io(sch, cdev);
1380 /* Re-start path verification. */
1381 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1384 /* trigger path verification. */
1385 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1389 static int io_subchannel_chp_event(struct subchannel *sch,
1390 struct chp_link *link, int event)
1394 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1401 io_subchannel_terminate_path(sch, mask);
1406 io_subchannel_verify(sch);
1409 if (cio_update_schib(sch))
1411 io_subchannel_terminate_path(sch, mask);
1414 if (cio_update_schib(sch))
1416 sch->lpm |= mask & sch->opm;
1417 io_subchannel_verify(sch);
1424 io_subchannel_shutdown(struct subchannel *sch)
1426 struct ccw_device *cdev;
1429 cdev = sch_get_cdev(sch);
1431 if (cio_is_console(sch->schid))
1433 if (!sch->schib.pmcw.ena)
1434 /* Nothing to do. */
1436 ret = cio_disable_subchannel(sch);
1438 /* Subchannel is disabled, we're done. */
1440 cdev->private->state = DEV_STATE_QUIESCE;
1442 cdev->handler(cdev, cdev->private->intparm,
1444 ret = ccw_device_cancel_halt_clear(cdev);
1445 if (ret == -EBUSY) {
1446 ccw_device_set_timeout(cdev, HZ/10);
1447 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1449 cio_disable_subchannel(sch);
1452 static int io_subchannel_get_status(struct subchannel *sch)
1456 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1458 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1459 return CIO_REVALIDATE;
1465 static int device_is_disconnected(struct ccw_device *cdev)
1469 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1470 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1473 static int recovery_check(struct device *dev, void *data)
1475 struct ccw_device *cdev = to_ccwdev(dev);
1478 spin_lock_irq(cdev->ccwlock);
1479 switch (cdev->private->state) {
1480 case DEV_STATE_DISCONNECTED:
1481 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1482 cdev->private->dev_id.ssid,
1483 cdev->private->dev_id.devno);
1484 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1487 case DEV_STATE_DISCONNECTED_SENSE_ID:
1491 spin_unlock_irq(cdev->ccwlock);
1496 static void recovery_work_func(struct work_struct *unused)
1500 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1502 spin_lock_irq(&recovery_lock);
1503 if (!timer_pending(&recovery_timer)) {
1504 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1506 mod_timer(&recovery_timer, jiffies +
1507 recovery_delay[recovery_phase] * HZ);
1509 spin_unlock_irq(&recovery_lock);
1511 CIO_MSG_EVENT(4, "recovery: end\n");
1514 static DECLARE_WORK(recovery_work, recovery_work_func);
1516 static void recovery_func(unsigned long data)
1519 * We can't do our recovery in softirq context and it's not
1520 * performance critical, so we schedule it.
1522 schedule_work(&recovery_work);
1525 static void ccw_device_schedule_recovery(void)
1527 unsigned long flags;
1529 CIO_MSG_EVENT(4, "recovery: schedule\n");
1530 spin_lock_irqsave(&recovery_lock, flags);
1531 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1533 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1535 spin_unlock_irqrestore(&recovery_lock, flags);
1538 static int purge_fn(struct device *dev, void *data)
1540 struct ccw_device *cdev = to_ccwdev(dev);
1541 struct ccw_device_private *priv = cdev->private;
1544 spin_lock_irq(cdev->ccwlock);
1545 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1546 (priv->state == DEV_STATE_OFFLINE);
1547 spin_unlock_irq(cdev->ccwlock);
1550 if (!get_device(&cdev->dev))
1552 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1553 priv->dev_id.devno);
1554 PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
1555 queue_work(slow_path_wq, &cdev->private->kick_work);
1558 /* Abort loop in case of pending signal. */
1559 if (signal_pending(current))
1566 * ccw_purge_blacklisted - purge unused, blacklisted devices
1568 * Unregister all ccw devices that are offline and on the blacklist.
1570 int ccw_purge_blacklisted(void)
1572 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1573 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1577 static void device_set_disconnected(struct ccw_device *cdev)
1581 ccw_device_set_timeout(cdev, 0);
1582 cdev->private->flags.fake_irb = 0;
1583 cdev->private->state = DEV_STATE_DISCONNECTED;
1585 ccw_device_schedule_recovery();
1588 void ccw_device_set_notoper(struct ccw_device *cdev)
1590 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1592 CIO_TRACE_EVENT(2, "notoper");
1593 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1594 ccw_device_set_timeout(cdev, 0);
1595 cio_disable_subchannel(sch);
1596 cdev->private->state = DEV_STATE_NOT_OPER;
1599 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1601 int event, ret, disc;
1602 unsigned long flags;
1603 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1604 struct ccw_device *cdev;
1606 spin_lock_irqsave(sch->lock, flags);
1607 cdev = sch_get_cdev(sch);
1608 disc = device_is_disconnected(cdev);
1610 /* Disconnected devices are evaluated directly only.*/
1611 spin_unlock_irqrestore(sch->lock, flags);
1614 /* No interrupt after machine check - kill pending timers. */
1616 ccw_device_set_timeout(cdev, 0);
1617 if (!disc && !slow) {
1618 /* Non-disconnected devices are evaluated on the slow path. */
1619 spin_unlock_irqrestore(sch->lock, flags);
1622 event = io_subchannel_get_status(sch);
1623 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1624 sch->schid.ssid, sch->schid.sch_no, event,
1625 disc ? "disconnected" : "normal",
1626 slow ? "slow" : "fast");
1627 /* Analyze subchannel status. */
1632 /* Check if paths have become available. */
1638 /* Ask driver what to do with device. */
1639 if (io_subchannel_notify(sch, event))
1642 action = UNREGISTER;
1644 case CIO_REVALIDATE:
1645 /* Device will be removed, so no notify necessary. */
1647 /* Reprobe because immediate unregister might block. */
1650 action = UNREGISTER_PROBE;
1654 /* Get device operational again. */
1658 /* Perform action. */
1662 case UNREGISTER_PROBE:
1663 ccw_device_set_notoper(cdev);
1664 /* Unregister device (will use subchannel lock). */
1665 spin_unlock_irqrestore(sch->lock, flags);
1666 css_sch_device_unregister(sch);
1667 spin_lock_irqsave(sch->lock, flags);
1669 /* Reset intparm to zeroes. */
1670 sch->config.intparm = 0;
1671 cio_commit_config(sch);
1674 ccw_device_trigger_reprobe(cdev);
1677 device_set_disconnected(cdev);
1682 spin_unlock_irqrestore(sch->lock, flags);
1683 /* Probe if necessary. */
1684 if (action == UNREGISTER_PROBE)
1685 ret = css_probe_device(sch->schid);
1690 #ifdef CONFIG_CCW_CONSOLE
1691 static struct ccw_device console_cdev;
1692 static char console_cdev_name[10] = "0.x.xxxx";
1693 static struct ccw_device_private console_private;
1694 static int console_cdev_in_use;
1696 static DEFINE_SPINLOCK(ccw_console_lock);
1698 spinlock_t * cio_get_console_lock(void)
1700 return &ccw_console_lock;
1703 static int ccw_device_console_enable(struct ccw_device *cdev,
1704 struct subchannel *sch)
1708 /* Attach subchannel private data. */
1709 sch->private = cio_get_console_priv();
1710 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1711 io_subchannel_init_fields(sch);
1712 rc = cio_commit_config(sch);
1715 sch->driver = &io_subchannel_driver;
1716 /* Initialize the ccw_device structure. */
1717 cdev->dev.parent= &sch->dev;
1718 rc = io_subchannel_recog(cdev, sch);
1722 /* Now wait for the async. recognition to come to an end. */
1723 spin_lock_irq(cdev->ccwlock);
1724 while (!dev_fsm_final_state(cdev))
1727 if (cdev->private->state != DEV_STATE_OFFLINE)
1729 ccw_device_online(cdev);
1730 while (!dev_fsm_final_state(cdev))
1732 if (cdev->private->state != DEV_STATE_ONLINE)
1736 spin_unlock_irq(cdev->ccwlock);
1741 ccw_device_probe_console(void)
1743 struct subchannel *sch;
1746 if (xchg(&console_cdev_in_use, 1) != 0)
1747 return ERR_PTR(-EBUSY);
1748 sch = cio_probe_console();
1750 console_cdev_in_use = 0;
1751 return (void *) sch;
1753 memset(&console_cdev, 0, sizeof(struct ccw_device));
1754 memset(&console_private, 0, sizeof(struct ccw_device_private));
1755 console_cdev.private = &console_private;
1756 console_private.cdev = &console_cdev;
1757 ret = ccw_device_console_enable(&console_cdev, sch);
1759 cio_release_console();
1760 console_cdev_in_use = 0;
1761 return ERR_PTR(ret);
1763 console_cdev.online = 1;
1764 return &console_cdev;
1768 const char *cio_get_console_cdev_name(struct subchannel *sch)
1770 snprintf(console_cdev_name, 10, "0.%x.%04x",
1771 sch->schid.ssid, sch->schib.pmcw.dev);
1772 return (const char *)console_cdev_name;
1777 * get ccw_device matching the busid, but only if owned by cdrv
1780 __ccwdev_check_busid(struct device *dev, void *id)
1786 return (strcmp(bus_id, dev_name(dev)) == 0);
1791 * get_ccwdev_by_busid() - obtain device from a bus id
1792 * @cdrv: driver the device is owned by
1793 * @bus_id: bus id of the device to be searched
1795 * This function searches all devices owned by @cdrv for a device with a bus
1796 * id matching @bus_id.
1798 * If a match is found, its reference count of the found device is increased
1799 * and it is returned; else %NULL is returned.
1801 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1805 struct device_driver *drv;
1807 drv = get_driver(&cdrv->driver);
1811 dev = driver_find_device(drv, NULL, (void *)bus_id,
1812 __ccwdev_check_busid);
1815 return dev ? to_ccwdev(dev) : NULL;
1818 /************************** device driver handling ************************/
1820 /* This is the implementation of the ccw_driver class. The probe, remove
1821 * and release methods are initially very similar to the device_driver
1822 * implementations, with the difference that they have ccw_device
1825 * A ccw driver also contains the information that is needed for
1829 ccw_device_probe (struct device *dev)
1831 struct ccw_device *cdev = to_ccwdev(dev);
1832 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1835 cdev->drv = cdrv; /* to let the driver call _set_online */
1837 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1848 ccw_device_remove (struct device *dev)
1850 struct ccw_device *cdev = to_ccwdev(dev);
1851 struct ccw_driver *cdrv = cdev->drv;
1858 spin_lock_irq(cdev->ccwlock);
1859 ret = ccw_device_offline(cdev);
1860 spin_unlock_irq(cdev->ccwlock);
1862 wait_event(cdev->private->wait_q,
1863 dev_fsm_final_state(cdev));
1865 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1866 "device 0.%x.%04x\n",
1867 ret, cdev->private->dev_id.ssid,
1868 cdev->private->dev_id.devno);
1869 /* Give up reference obtained in ccw_device_set_online(). */
1870 put_device(&cdev->dev);
1872 ccw_device_set_timeout(cdev, 0);
1877 static void ccw_device_shutdown(struct device *dev)
1879 struct ccw_device *cdev;
1881 cdev = to_ccwdev(dev);
1882 if (cdev->drv && cdev->drv->shutdown)
1883 cdev->drv->shutdown(cdev);
1887 struct bus_type ccw_bus_type = {
1889 .match = ccw_bus_match,
1890 .uevent = ccw_uevent,
1891 .probe = ccw_device_probe,
1892 .remove = ccw_device_remove,
1893 .shutdown = ccw_device_shutdown,
1897 * ccw_driver_register() - register a ccw driver
1898 * @cdriver: driver to be registered
1900 * This function is mainly a wrapper around driver_register().
1902 * %0 on success and a negative error value on failure.
1904 int ccw_driver_register(struct ccw_driver *cdriver)
1906 struct device_driver *drv = &cdriver->driver;
1908 drv->bus = &ccw_bus_type;
1909 drv->name = cdriver->name;
1910 drv->owner = cdriver->owner;
1912 return driver_register(drv);
1916 * ccw_driver_unregister() - deregister a ccw driver
1917 * @cdriver: driver to be deregistered
1919 * This function is mainly a wrapper around driver_unregister().
1921 void ccw_driver_unregister(struct ccw_driver *cdriver)
1923 driver_unregister(&cdriver->driver);
1926 /* Helper func for qdio. */
1927 struct subchannel_id
1928 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1930 struct subchannel *sch;
1932 sch = to_subchannel(cdev->dev.parent);
1936 MODULE_LICENSE("GPL");
1937 EXPORT_SYMBOL(ccw_device_set_online);
1938 EXPORT_SYMBOL(ccw_device_set_offline);
1939 EXPORT_SYMBOL(ccw_driver_register);
1940 EXPORT_SYMBOL(ccw_driver_unregister);
1941 EXPORT_SYMBOL(get_ccwdev_by_busid);
1942 EXPORT_SYMBOL(ccw_bus_type);
1943 EXPORT_SYMBOL(ccw_device_work);
1944 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);