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);
314 ccw_device_remove_disconnected(struct ccw_device *cdev)
319 * Forced offline in disconnected state means
320 * 'throw away device'.
322 /* Get cdev reference for workqueue processing. */
323 if (!get_device(&cdev->dev))
325 if (ccw_device_is_orphan(cdev)) {
327 * Deregister ccw device.
328 * Unfortunately, we cannot do this directly from the
331 spin_lock_irqsave(cdev->ccwlock, flags);
332 cdev->private->state = DEV_STATE_NOT_OPER;
333 spin_unlock_irqrestore(cdev->ccwlock, flags);
334 PREPARE_WORK(&cdev->private->kick_work,
335 ccw_device_remove_orphan_cb);
336 queue_work(slow_path_wq, &cdev->private->kick_work);
338 /* Deregister subchannel, which will kill the ccw device. */
339 ccw_device_schedule_sch_unregister(cdev);
343 * ccw_device_set_offline() - disable a ccw device for I/O
344 * @cdev: target ccw device
346 * This function calls the driver's set_offline() function for @cdev, if
347 * given, and then disables @cdev.
349 * %0 on success and a negative error value on failure.
351 * enabled, ccw device lock not held
353 int ccw_device_set_offline(struct ccw_device *cdev)
359 if (!cdev->online || !cdev->drv)
362 if (cdev->drv->set_offline) {
363 ret = cdev->drv->set_offline(cdev);
368 spin_lock_irq(cdev->ccwlock);
369 ret = ccw_device_offline(cdev);
370 if (ret == -ENODEV) {
371 if (cdev->private->state != DEV_STATE_NOT_OPER) {
372 cdev->private->state = DEV_STATE_OFFLINE;
373 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
375 spin_unlock_irq(cdev->ccwlock);
376 /* Give up reference from ccw_device_set_online(). */
377 put_device(&cdev->dev);
380 spin_unlock_irq(cdev->ccwlock);
382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
383 /* Give up reference from ccw_device_set_online(). */
384 put_device(&cdev->dev);
386 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
387 "device 0.%x.%04x\n",
388 ret, cdev->private->dev_id.ssid,
389 cdev->private->dev_id.devno);
396 * ccw_device_set_online() - enable a ccw device for I/O
397 * @cdev: target ccw device
399 * This function first enables @cdev and then calls the driver's set_online()
400 * function for @cdev, if given. If set_online() returns an error, @cdev is
403 * %0 on success and a negative error value on failure.
405 * enabled, ccw device lock not held
407 int ccw_device_set_online(struct ccw_device *cdev)
413 if (cdev->online || !cdev->drv)
415 /* Hold on to an extra reference while device is online. */
416 if (!get_device(&cdev->dev))
419 spin_lock_irq(cdev->ccwlock);
420 ret = ccw_device_online(cdev);
421 spin_unlock_irq(cdev->ccwlock);
423 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
425 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
426 "device 0.%x.%04x\n",
427 ret, cdev->private->dev_id.ssid,
428 cdev->private->dev_id.devno);
429 /* Give up online reference since onlining failed. */
430 put_device(&cdev->dev);
433 if (cdev->private->state != DEV_STATE_ONLINE) {
434 /* Give up online reference since onlining failed. */
435 put_device(&cdev->dev);
438 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
442 spin_lock_irq(cdev->ccwlock);
443 ret = ccw_device_offline(cdev);
444 spin_unlock_irq(cdev->ccwlock);
446 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
448 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
449 "device 0.%x.%04x\n",
450 ret, cdev->private->dev_id.ssid,
451 cdev->private->dev_id.devno);
452 /* Give up online reference since onlining failed. */
453 put_device(&cdev->dev);
454 return (ret == 0) ? -ENODEV : ret;
457 static int online_store_handle_offline(struct ccw_device *cdev)
459 if (cdev->private->state == DEV_STATE_DISCONNECTED)
460 ccw_device_remove_disconnected(cdev);
461 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
462 return ccw_device_set_offline(cdev);
466 static int online_store_recog_and_online(struct ccw_device *cdev)
470 /* Do device recognition, if needed. */
471 if (cdev->id.cu_type == 0) {
472 ret = ccw_device_recognition(cdev);
474 CIO_MSG_EVENT(0, "Couldn't start recognition "
475 "for device 0.%x.%04x (ret=%d)\n",
476 cdev->private->dev_id.ssid,
477 cdev->private->dev_id.devno, ret);
480 wait_event(cdev->private->wait_q,
481 cdev->private->flags.recog_done);
482 if (cdev->private->state != DEV_STATE_OFFLINE)
483 /* recognition failed */
486 if (cdev->drv && cdev->drv->set_online)
487 ccw_device_set_online(cdev);
491 static int online_store_handle_online(struct ccw_device *cdev, int force)
495 ret = online_store_recog_and_online(cdev);
498 if (force && cdev->private->state == DEV_STATE_BOXED) {
499 ret = ccw_device_stlck(cdev);
502 if (cdev->id.cu_type == 0)
503 cdev->private->state = DEV_STATE_NOT_OPER;
504 ret = online_store_recog_and_online(cdev);
511 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count)
514 struct ccw_device *cdev = to_ccwdev(dev);
518 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
521 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
522 atomic_set(&cdev->private->onoff, 0);
525 if (!strncmp(buf, "force\n", count)) {
531 ret = strict_strtoul(buf, 16, &i);
537 ret = online_store_handle_offline(cdev);
540 ret = online_store_handle_online(cdev, force);
547 module_put(cdev->drv->owner);
548 atomic_set(&cdev->private->onoff, 0);
549 return (ret < 0) ? ret : count;
553 available_show (struct device *dev, struct device_attribute *attr, char *buf)
555 struct ccw_device *cdev = to_ccwdev(dev);
556 struct subchannel *sch;
558 if (ccw_device_is_orphan(cdev))
559 return sprintf(buf, "no device\n");
560 switch (cdev->private->state) {
561 case DEV_STATE_BOXED:
562 return sprintf(buf, "boxed\n");
563 case DEV_STATE_DISCONNECTED:
564 case DEV_STATE_DISCONNECTED_SENSE_ID:
565 case DEV_STATE_NOT_OPER:
566 sch = to_subchannel(dev->parent);
568 return sprintf(buf, "no path\n");
570 return sprintf(buf, "no device\n");
572 /* All other states considered fine. */
573 return sprintf(buf, "good\n");
577 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
578 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
579 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
580 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
581 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
582 static DEVICE_ATTR(online, 0644, online_show, online_store);
583 static DEVICE_ATTR(availability, 0444, available_show, NULL);
585 static struct attribute *io_subchannel_attrs[] = {
586 &dev_attr_chpids.attr,
587 &dev_attr_pimpampom.attr,
591 static struct attribute_group io_subchannel_attr_group = {
592 .attrs = io_subchannel_attrs,
595 static struct attribute * ccwdev_attrs[] = {
596 &dev_attr_devtype.attr,
597 &dev_attr_cutype.attr,
598 &dev_attr_modalias.attr,
599 &dev_attr_online.attr,
600 &dev_attr_cmb_enable.attr,
601 &dev_attr_availability.attr,
605 static struct attribute_group ccwdev_attr_group = {
606 .attrs = ccwdev_attrs,
609 static struct attribute_group *ccwdev_attr_groups[] = {
614 /* this is a simple abstraction for device_register that sets the
615 * correct bus type and adds the bus specific files */
616 static int ccw_device_register(struct ccw_device *cdev)
618 struct device *dev = &cdev->dev;
621 dev->bus = &ccw_bus_type;
623 if ((ret = device_add(dev)))
626 set_bit(1, &cdev->private->registered);
631 struct ccw_dev_id dev_id;
632 struct ccw_device * sibling;
636 match_devno(struct device * dev, void * data)
638 struct match_data * d = data;
639 struct ccw_device * cdev;
641 cdev = to_ccwdev(dev);
642 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
643 !ccw_device_is_orphan(cdev) &&
644 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
645 (cdev != d->sibling))
650 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
651 struct ccw_device *sibling)
654 struct match_data data;
656 data.dev_id = *dev_id;
657 data.sibling = sibling;
658 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
660 return dev ? to_ccwdev(dev) : NULL;
663 static int match_orphan(struct device *dev, void *data)
665 struct ccw_dev_id *dev_id;
666 struct ccw_device *cdev;
669 cdev = to_ccwdev(dev);
670 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
673 static struct ccw_device *
674 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
675 struct ccw_dev_id *dev_id)
679 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
682 return dev ? to_ccwdev(dev) : NULL;
685 void ccw_device_do_unbind_bind(struct work_struct *work)
687 struct ccw_device_private *priv;
688 struct ccw_device *cdev;
689 struct subchannel *sch;
692 priv = container_of(work, struct ccw_device_private, kick_work);
694 sch = to_subchannel(cdev->dev.parent);
696 if (test_bit(1, &cdev->private->registered)) {
697 device_release_driver(&cdev->dev);
698 ret = device_attach(&cdev->dev);
699 WARN_ON(ret == -ENODEV);
704 ccw_device_release(struct device *dev)
706 struct ccw_device *cdev;
708 cdev = to_ccwdev(dev);
709 /* Release reference of parent subchannel. */
710 put_device(cdev->dev.parent);
711 kfree(cdev->private);
715 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
717 struct ccw_device *cdev;
719 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
721 cdev->private = kzalloc(sizeof(struct ccw_device_private),
722 GFP_KERNEL | GFP_DMA);
727 return ERR_PTR(-ENOMEM);
730 static int io_subchannel_initialize_dev(struct subchannel *sch,
731 struct ccw_device *cdev)
733 cdev->private->cdev = cdev;
734 atomic_set(&cdev->private->onoff, 0);
735 cdev->dev.parent = &sch->dev;
736 cdev->dev.release = ccw_device_release;
737 INIT_WORK(&cdev->private->kick_work, NULL);
738 cdev->dev.groups = ccwdev_attr_groups;
739 /* Do first half of device_register. */
740 device_initialize(&cdev->dev);
741 if (!get_device(&sch->dev)) {
742 /* Release reference from device_initialize(). */
743 put_device(&cdev->dev);
749 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
751 struct ccw_device *cdev;
754 cdev = io_subchannel_allocate_dev(sch);
756 ret = io_subchannel_initialize_dev(sch, cdev);
765 static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
767 static void sch_attach_device(struct subchannel *sch,
768 struct ccw_device *cdev)
770 css_update_ssd_info(sch);
771 spin_lock_irq(sch->lock);
772 sch_set_cdev(sch, cdev);
773 cdev->private->schid = sch->schid;
774 cdev->ccwlock = sch->lock;
775 ccw_device_trigger_reprobe(cdev);
776 spin_unlock_irq(sch->lock);
779 static void sch_attach_disconnected_device(struct subchannel *sch,
780 struct ccw_device *cdev)
782 struct subchannel *other_sch;
785 /* Get reference for new parent. */
786 if (!get_device(&sch->dev))
788 other_sch = to_subchannel(cdev->dev.parent);
789 /* Note: device_move() changes cdev->dev.parent */
790 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
792 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
793 "(ret=%d)!\n", cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno, ret);
795 /* Put reference for new parent. */
796 put_device(&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 sch_attach_device(sch, cdev);
803 /* Put reference for old parent. */
804 put_device(&other_sch->dev);
807 static void sch_attach_orphaned_device(struct subchannel *sch,
808 struct ccw_device *cdev)
811 struct subchannel *pseudo_sch;
813 /* Get reference for new parent. */
814 if (!get_device(&sch->dev))
816 pseudo_sch = to_subchannel(cdev->dev.parent);
818 * Try to move the ccw device to its new subchannel.
819 * Note: device_move() changes cdev->dev.parent
821 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
823 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
824 "failed (ret=%d)!\n",
825 cdev->private->dev_id.ssid,
826 cdev->private->dev_id.devno, ret);
827 /* Put reference for new parent. */
828 put_device(&sch->dev);
831 sch_attach_device(sch, cdev);
832 /* Put reference on pseudo subchannel. */
833 put_device(&pseudo_sch->dev);
836 static void sch_create_and_recog_new_device(struct subchannel *sch)
838 struct ccw_device *cdev;
840 /* Need to allocate a new ccw device. */
841 cdev = io_subchannel_create_ccwdev(sch);
843 /* OK, we did everything we could... */
844 css_sch_device_unregister(sch);
847 spin_lock_irq(sch->lock);
848 sch_set_cdev(sch, cdev);
849 spin_unlock_irq(sch->lock);
850 /* Start recognition for the new ccw device. */
851 if (io_subchannel_recog(cdev, sch)) {
852 spin_lock_irq(sch->lock);
853 sch_set_cdev(sch, NULL);
854 spin_unlock_irq(sch->lock);
855 css_sch_device_unregister(sch);
856 /* Put reference from io_subchannel_create_ccwdev(). */
857 put_device(&sch->dev);
858 /* Give up initial reference. */
859 put_device(&cdev->dev);
864 void ccw_device_move_to_orphanage(struct work_struct *work)
866 struct ccw_device_private *priv;
867 struct ccw_device *cdev;
868 struct ccw_device *replacing_cdev;
869 struct subchannel *sch;
871 struct channel_subsystem *css;
872 struct ccw_dev_id dev_id;
874 priv = container_of(work, struct ccw_device_private, kick_work);
876 sch = to_subchannel(cdev->dev.parent);
877 css = to_css(sch->dev.parent);
878 dev_id.devno = sch->schib.pmcw.dev;
879 dev_id.ssid = sch->schid.ssid;
881 /* Increase refcount for pseudo subchannel. */
882 get_device(&css->pseudo_subchannel->dev);
884 * Move the orphaned ccw device to the orphanage so the replacing
885 * ccw device can take its place on the subchannel.
886 * Note: device_move() changes cdev->dev.parent
888 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
891 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
892 "(ret=%d)!\n", cdev->private->dev_id.ssid,
893 cdev->private->dev_id.devno, ret);
894 /* Decrease refcount for pseudo subchannel again. */
895 put_device(&css->pseudo_subchannel->dev);
898 cdev->ccwlock = css->pseudo_subchannel->lock;
900 * Search for the replacing ccw device
901 * - among the disconnected devices
904 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
905 if (replacing_cdev) {
906 sch_attach_disconnected_device(sch, replacing_cdev);
907 /* Release reference from get_disc_ccwdev_by_dev_id() */
908 put_device(&replacing_cdev->dev);
909 /* Release reference of subchannel from old cdev. */
910 put_device(&sch->dev);
913 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
914 if (replacing_cdev) {
915 sch_attach_orphaned_device(sch, replacing_cdev);
916 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
917 put_device(&replacing_cdev->dev);
918 /* Release reference of subchannel from old cdev. */
919 put_device(&sch->dev);
922 sch_create_and_recog_new_device(sch);
923 /* Release reference of subchannel from old cdev. */
924 put_device(&sch->dev);
928 * Register recognized device.
931 io_subchannel_register(struct work_struct *work)
933 struct ccw_device_private *priv;
934 struct ccw_device *cdev;
935 struct subchannel *sch;
939 priv = container_of(work, struct ccw_device_private, kick_work);
941 sch = to_subchannel(cdev->dev.parent);
943 * Check if subchannel is still registered. It may have become
944 * unregistered if a machine check hit us after finishing
945 * device recognition but before the register work could be
948 if (!device_is_registered(&sch->dev))
950 css_update_ssd_info(sch);
952 * io_subchannel_register() will also be called after device
953 * recognition has been done for a boxed device (which will already
954 * be registered). We need to reprobe since we may now have sense id
957 if (device_is_registered(&cdev->dev)) {
959 ret = device_reprobe(&cdev->dev);
961 /* We can't do much here. */
962 CIO_MSG_EVENT(0, "device_reprobe() returned"
963 " %d for 0.%x.%04x\n", ret,
964 cdev->private->dev_id.ssid,
965 cdev->private->dev_id.devno);
970 * Now we know this subchannel will stay, we can throw
971 * our delayed uevent.
973 dev_set_uevent_suppress(&sch->dev, 0);
974 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
975 /* make it known to the system */
976 ret = ccw_device_register(cdev);
978 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
979 cdev->private->dev_id.ssid,
980 cdev->private->dev_id.devno, ret);
981 spin_lock_irqsave(sch->lock, flags);
982 sch_set_cdev(sch, NULL);
983 spin_unlock_irqrestore(sch->lock, flags);
984 /* Release initial device reference. */
985 put_device(&cdev->dev);
989 cdev->private->flags.recog_done = 1;
990 wake_up(&cdev->private->wait_q);
992 /* Release reference for workqueue processing. */
993 put_device(&cdev->dev);
994 if (atomic_dec_and_test(&ccw_device_init_count))
995 wake_up(&ccw_device_init_wq);
998 static void ccw_device_call_sch_unregister(struct work_struct *work)
1000 struct ccw_device_private *priv;
1001 struct ccw_device *cdev;
1002 struct subchannel *sch;
1004 priv = container_of(work, struct ccw_device_private, kick_work);
1006 /* Get subchannel reference for local processing. */
1007 if (!get_device(cdev->dev.parent))
1009 sch = to_subchannel(cdev->dev.parent);
1010 css_sch_device_unregister(sch);
1011 /* Reset intparm to zeroes. */
1012 sch->config.intparm = 0;
1013 cio_commit_config(sch);
1014 /* Release cdev reference for workqueue processing.*/
1015 put_device(&cdev->dev);
1016 /* Release subchannel reference for local processing. */
1017 put_device(&sch->dev);
1020 void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1022 PREPARE_WORK(&cdev->private->kick_work,
1023 ccw_device_call_sch_unregister);
1024 queue_work(slow_path_wq, &cdev->private->kick_work);
1028 * subchannel recognition done. Called from the state machine.
1031 io_subchannel_recog_done(struct ccw_device *cdev)
1033 if (css_init_done == 0) {
1034 cdev->private->flags.recog_done = 1;
1037 switch (cdev->private->state) {
1038 case DEV_STATE_BOXED:
1039 /* Device did not respond in time. */
1040 case DEV_STATE_NOT_OPER:
1041 cdev->private->flags.recog_done = 1;
1042 /* Remove device found not operational. */
1043 if (!get_device(&cdev->dev))
1045 ccw_device_schedule_sch_unregister(cdev);
1046 if (atomic_dec_and_test(&ccw_device_init_count))
1047 wake_up(&ccw_device_init_wq);
1049 case DEV_STATE_OFFLINE:
1051 * We can't register the device in interrupt context so
1052 * we schedule a work item.
1054 if (!get_device(&cdev->dev))
1056 PREPARE_WORK(&cdev->private->kick_work,
1057 io_subchannel_register);
1058 queue_work(slow_path_wq, &cdev->private->kick_work);
1064 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1067 struct ccw_device_private *priv;
1069 sch_set_cdev(sch, cdev);
1070 cdev->ccwlock = sch->lock;
1072 /* Init private data. */
1073 priv = cdev->private;
1074 priv->dev_id.devno = sch->schib.pmcw.dev;
1075 priv->dev_id.ssid = sch->schid.ssid;
1076 priv->schid = sch->schid;
1077 priv->state = DEV_STATE_NOT_OPER;
1078 INIT_LIST_HEAD(&priv->cmb_list);
1079 init_waitqueue_head(&priv->wait_q);
1080 init_timer(&priv->timer);
1082 /* Set an initial name for the device. */
1083 if (cio_is_console(sch->schid))
1084 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1086 dev_set_name(&cdev->dev, "0.%x.%04x",
1087 sch->schid.ssid, sch->schib.pmcw.dev);
1089 /* Increase counter of devices currently in recognition. */
1090 atomic_inc(&ccw_device_init_count);
1092 /* Start async. device sensing. */
1093 spin_lock_irq(sch->lock);
1094 rc = ccw_device_recognition(cdev);
1095 spin_unlock_irq(sch->lock);
1097 if (atomic_dec_and_test(&ccw_device_init_count))
1098 wake_up(&ccw_device_init_wq);
1103 static void ccw_device_move_to_sch(struct work_struct *work)
1105 struct ccw_device_private *priv;
1107 struct subchannel *sch;
1108 struct ccw_device *cdev;
1109 struct subchannel *former_parent;
1111 priv = container_of(work, struct ccw_device_private, kick_work);
1114 former_parent = to_subchannel(cdev->dev.parent);
1115 /* Get reference for new parent. */
1116 if (!get_device(&sch->dev))
1118 mutex_lock(&sch->reg_mutex);
1120 * Try to move the ccw device to its new subchannel.
1121 * Note: device_move() changes cdev->dev.parent
1123 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
1124 mutex_unlock(&sch->reg_mutex);
1126 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1127 "0.%x.%04x failed (ret=%d)!\n",
1128 cdev->private->dev_id.ssid,
1129 cdev->private->dev_id.devno, sch->schid.ssid,
1130 sch->schid.sch_no, rc);
1131 css_sch_device_unregister(sch);
1132 /* Put reference for new parent again. */
1133 put_device(&sch->dev);
1136 if (!sch_is_pseudo_sch(former_parent)) {
1137 spin_lock_irq(former_parent->lock);
1138 sch_set_cdev(former_parent, NULL);
1139 spin_unlock_irq(former_parent->lock);
1140 css_sch_device_unregister(former_parent);
1141 /* Reset intparm to zeroes. */
1142 former_parent->config.intparm = 0;
1143 cio_commit_config(former_parent);
1145 sch_attach_device(sch, cdev);
1147 /* Put reference for old parent. */
1148 put_device(&former_parent->dev);
1149 put_device(&cdev->dev);
1152 static void io_subchannel_irq(struct subchannel *sch)
1154 struct ccw_device *cdev;
1156 cdev = sch_get_cdev(sch);
1158 CIO_TRACE_EVENT(3, "IRQ");
1159 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
1161 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1164 void io_subchannel_init_config(struct subchannel *sch)
1166 memset(&sch->config, 0, sizeof(sch->config));
1167 sch->config.csense = 1;
1168 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1169 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
1173 static void io_subchannel_init_fields(struct subchannel *sch)
1175 if (cio_is_console(sch->schid))
1178 sch->opm = chp_get_sch_opm(sch);
1179 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1180 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1182 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1183 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1184 sch->schib.pmcw.dev, sch->schid.ssid,
1185 sch->schid.sch_no, sch->schib.pmcw.pim,
1186 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1188 io_subchannel_init_config(sch);
1191 static void io_subchannel_do_unreg(struct work_struct *work)
1193 struct subchannel *sch;
1195 sch = container_of(work, struct subchannel, work);
1196 css_sch_device_unregister(sch);
1197 /* Reset intparm to zeroes. */
1198 sch->config.intparm = 0;
1199 cio_commit_config(sch);
1200 put_device(&sch->dev);
1203 /* Schedule unregister if we have no cdev. */
1204 static void io_subchannel_schedule_removal(struct subchannel *sch)
1206 get_device(&sch->dev);
1207 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1208 queue_work(slow_path_wq, &sch->work);
1212 * Note: We always return 0 so that we bind to the device even on error.
1213 * This is needed so that our remove function is called on unregister.
1215 static int io_subchannel_probe(struct subchannel *sch)
1217 struct ccw_device *cdev;
1219 unsigned long flags;
1220 struct ccw_dev_id dev_id;
1222 cdev = sch_get_cdev(sch);
1224 rc = sysfs_create_group(&sch->dev.kobj,
1225 &io_subchannel_attr_group);
1227 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1228 "attributes for subchannel "
1229 "0.%x.%04x (rc=%d)\n",
1230 sch->schid.ssid, sch->schid.sch_no, rc);
1232 * This subchannel already has an associated ccw_device.
1233 * Throw the delayed uevent for the subchannel, register
1234 * the ccw_device and exit. This happens for all early
1235 * devices, e.g. the console.
1237 dev_set_uevent_suppress(&sch->dev, 0);
1238 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1239 cdev->dev.groups = ccwdev_attr_groups;
1240 device_initialize(&cdev->dev);
1241 ccw_device_register(cdev);
1243 * Check if the device is already online. If it is
1244 * the reference count needs to be corrected since we
1245 * didn't obtain a reference in ccw_device_set_online.
1247 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1248 cdev->private->state != DEV_STATE_OFFLINE &&
1249 cdev->private->state != DEV_STATE_BOXED)
1250 get_device(&cdev->dev);
1253 io_subchannel_init_fields(sch);
1254 rc = cio_commit_config(sch);
1257 rc = sysfs_create_group(&sch->dev.kobj,
1258 &io_subchannel_attr_group);
1261 /* Allocate I/O subchannel private data. */
1262 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1263 GFP_KERNEL | GFP_DMA);
1267 * First check if a fitting device may be found amongst the
1268 * disconnected devices or in the orphanage.
1270 dev_id.devno = sch->schib.pmcw.dev;
1271 dev_id.ssid = sch->schid.ssid;
1272 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1274 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1278 * Schedule moving the device until when we have a registered
1279 * subchannel to move to and succeed the probe. We can
1280 * unregister later again, when the probe is through.
1282 cdev->private->sch = sch;
1283 PREPARE_WORK(&cdev->private->kick_work,
1284 ccw_device_move_to_sch);
1285 queue_work(slow_path_wq, &cdev->private->kick_work);
1288 cdev = io_subchannel_create_ccwdev(sch);
1291 rc = io_subchannel_recog(cdev, sch);
1293 spin_lock_irqsave(sch->lock, flags);
1294 io_subchannel_recog_done(cdev);
1295 spin_unlock_irqrestore(sch->lock, flags);
1299 kfree(sch->private);
1300 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1302 io_subchannel_schedule_removal(sch);
1307 io_subchannel_remove (struct subchannel *sch)
1309 struct ccw_device *cdev;
1310 unsigned long flags;
1312 cdev = sch_get_cdev(sch);
1315 /* Set ccw device to not operational and drop reference. */
1316 spin_lock_irqsave(cdev->ccwlock, flags);
1317 sch_set_cdev(sch, NULL);
1318 cdev->private->state = DEV_STATE_NOT_OPER;
1319 spin_unlock_irqrestore(cdev->ccwlock, flags);
1320 ccw_device_unregister(cdev);
1321 put_device(&cdev->dev);
1322 kfree(sch->private);
1323 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1327 static int io_subchannel_notify(struct subchannel *sch, int event)
1329 struct ccw_device *cdev;
1331 cdev = sch_get_cdev(sch);
1334 return ccw_device_notify(cdev, event);
1337 static void io_subchannel_verify(struct subchannel *sch)
1339 struct ccw_device *cdev;
1341 cdev = sch_get_cdev(sch);
1343 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1346 static int check_for_io_on_path(struct subchannel *sch, int mask)
1348 if (cio_update_schib(sch))
1350 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
1355 static void terminate_internal_io(struct subchannel *sch,
1356 struct ccw_device *cdev)
1358 if (cio_clear(sch)) {
1359 /* Recheck device in case clear failed. */
1362 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1364 css_schedule_eval(sch->schid);
1367 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1368 /* Request retry of internal operation. */
1369 cdev->private->flags.intretry = 1;
1372 cdev->handler(cdev, cdev->private->intparm,
1376 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1378 struct ccw_device *cdev;
1380 cdev = sch_get_cdev(sch);
1383 if (check_for_io_on_path(sch, mask)) {
1384 if (cdev->private->state == DEV_STATE_ONLINE)
1385 ccw_device_kill_io(cdev);
1387 terminate_internal_io(sch, cdev);
1388 /* Re-start path verification. */
1389 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1392 /* trigger path verification. */
1393 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1397 static int io_subchannel_chp_event(struct subchannel *sch,
1398 struct chp_link *link, int event)
1402 mask = chp_ssd_get_mask(&sch->ssd_info, link);
1409 io_subchannel_terminate_path(sch, mask);
1414 io_subchannel_verify(sch);
1417 if (cio_update_schib(sch))
1419 io_subchannel_terminate_path(sch, mask);
1422 if (cio_update_schib(sch))
1424 sch->lpm |= mask & sch->opm;
1425 io_subchannel_verify(sch);
1432 io_subchannel_shutdown(struct subchannel *sch)
1434 struct ccw_device *cdev;
1437 cdev = sch_get_cdev(sch);
1439 if (cio_is_console(sch->schid))
1441 if (!sch->schib.pmcw.ena)
1442 /* Nothing to do. */
1444 ret = cio_disable_subchannel(sch);
1446 /* Subchannel is disabled, we're done. */
1448 cdev->private->state = DEV_STATE_QUIESCE;
1450 cdev->handler(cdev, cdev->private->intparm,
1452 ret = ccw_device_cancel_halt_clear(cdev);
1453 if (ret == -EBUSY) {
1454 ccw_device_set_timeout(cdev, HZ/10);
1455 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1457 cio_disable_subchannel(sch);
1460 static int io_subchannel_get_status(struct subchannel *sch)
1464 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1466 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1467 return CIO_REVALIDATE;
1473 static int device_is_disconnected(struct ccw_device *cdev)
1477 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1478 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1481 static int recovery_check(struct device *dev, void *data)
1483 struct ccw_device *cdev = to_ccwdev(dev);
1486 spin_lock_irq(cdev->ccwlock);
1487 switch (cdev->private->state) {
1488 case DEV_STATE_DISCONNECTED:
1489 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1490 cdev->private->dev_id.ssid,
1491 cdev->private->dev_id.devno);
1492 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1495 case DEV_STATE_DISCONNECTED_SENSE_ID:
1499 spin_unlock_irq(cdev->ccwlock);
1504 static void recovery_work_func(struct work_struct *unused)
1508 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1510 spin_lock_irq(&recovery_lock);
1511 if (!timer_pending(&recovery_timer)) {
1512 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1514 mod_timer(&recovery_timer, jiffies +
1515 recovery_delay[recovery_phase] * HZ);
1517 spin_unlock_irq(&recovery_lock);
1519 CIO_MSG_EVENT(4, "recovery: end\n");
1522 static DECLARE_WORK(recovery_work, recovery_work_func);
1524 static void recovery_func(unsigned long data)
1527 * We can't do our recovery in softirq context and it's not
1528 * performance critical, so we schedule it.
1530 schedule_work(&recovery_work);
1533 static void ccw_device_schedule_recovery(void)
1535 unsigned long flags;
1537 CIO_MSG_EVENT(4, "recovery: schedule\n");
1538 spin_lock_irqsave(&recovery_lock, flags);
1539 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1541 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1543 spin_unlock_irqrestore(&recovery_lock, flags);
1546 static int purge_fn(struct device *dev, void *data)
1548 struct ccw_device *cdev = to_ccwdev(dev);
1549 struct ccw_device_private *priv = cdev->private;
1552 spin_lock_irq(cdev->ccwlock);
1553 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1554 (priv->state == DEV_STATE_OFFLINE);
1555 spin_unlock_irq(cdev->ccwlock);
1558 if (!get_device(&cdev->dev))
1560 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1561 priv->dev_id.devno);
1562 ccw_device_schedule_sch_unregister(cdev);
1565 /* Abort loop in case of pending signal. */
1566 if (signal_pending(current))
1573 * ccw_purge_blacklisted - purge unused, blacklisted devices
1575 * Unregister all ccw devices that are offline and on the blacklist.
1577 int ccw_purge_blacklisted(void)
1579 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1580 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1584 static void device_set_disconnected(struct ccw_device *cdev)
1588 ccw_device_set_timeout(cdev, 0);
1589 cdev->private->flags.fake_irb = 0;
1590 cdev->private->state = DEV_STATE_DISCONNECTED;
1592 ccw_device_schedule_recovery();
1595 void ccw_device_set_notoper(struct ccw_device *cdev)
1597 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1599 CIO_TRACE_EVENT(2, "notoper");
1600 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1601 ccw_device_set_timeout(cdev, 0);
1602 cio_disable_subchannel(sch);
1603 cdev->private->state = DEV_STATE_NOT_OPER;
1606 static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1608 int event, ret, disc;
1609 unsigned long flags;
1610 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
1611 struct ccw_device *cdev;
1613 spin_lock_irqsave(sch->lock, flags);
1614 cdev = sch_get_cdev(sch);
1615 disc = device_is_disconnected(cdev);
1617 /* Disconnected devices are evaluated directly only.*/
1618 spin_unlock_irqrestore(sch->lock, flags);
1621 /* No interrupt after machine check - kill pending timers. */
1623 ccw_device_set_timeout(cdev, 0);
1624 if (!disc && !slow) {
1625 /* Non-disconnected devices are evaluated on the slow path. */
1626 spin_unlock_irqrestore(sch->lock, flags);
1629 event = io_subchannel_get_status(sch);
1630 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1631 sch->schid.ssid, sch->schid.sch_no, event,
1632 disc ? "disconnected" : "normal",
1633 slow ? "slow" : "fast");
1634 /* Analyze subchannel status. */
1639 /* Check if paths have become available. */
1645 /* Ask driver what to do with device. */
1646 if (io_subchannel_notify(sch, event))
1649 action = UNREGISTER;
1651 case CIO_REVALIDATE:
1652 /* Device will be removed, so no notify necessary. */
1654 /* Reprobe because immediate unregister might block. */
1657 action = UNREGISTER_PROBE;
1661 /* Get device operational again. */
1665 /* Perform action. */
1669 case UNREGISTER_PROBE:
1670 ccw_device_set_notoper(cdev);
1671 /* Unregister device (will use subchannel lock). */
1672 spin_unlock_irqrestore(sch->lock, flags);
1673 css_sch_device_unregister(sch);
1674 spin_lock_irqsave(sch->lock, flags);
1676 /* Reset intparm to zeroes. */
1677 sch->config.intparm = 0;
1678 cio_commit_config(sch);
1681 ccw_device_trigger_reprobe(cdev);
1684 device_set_disconnected(cdev);
1689 spin_unlock_irqrestore(sch->lock, flags);
1690 /* Probe if necessary. */
1691 if (action == UNREGISTER_PROBE)
1692 ret = css_probe_device(sch->schid);
1697 #ifdef CONFIG_CCW_CONSOLE
1698 static struct ccw_device console_cdev;
1699 static char console_cdev_name[10] = "0.x.xxxx";
1700 static struct ccw_device_private console_private;
1701 static int console_cdev_in_use;
1703 static DEFINE_SPINLOCK(ccw_console_lock);
1705 spinlock_t * cio_get_console_lock(void)
1707 return &ccw_console_lock;
1710 static int ccw_device_console_enable(struct ccw_device *cdev,
1711 struct subchannel *sch)
1715 /* Attach subchannel private data. */
1716 sch->private = cio_get_console_priv();
1717 memset(sch->private, 0, sizeof(struct io_subchannel_private));
1718 io_subchannel_init_fields(sch);
1719 rc = cio_commit_config(sch);
1722 sch->driver = &io_subchannel_driver;
1723 /* Initialize the ccw_device structure. */
1724 cdev->dev.parent= &sch->dev;
1725 rc = io_subchannel_recog(cdev, sch);
1729 /* Now wait for the async. recognition to come to an end. */
1730 spin_lock_irq(cdev->ccwlock);
1731 while (!dev_fsm_final_state(cdev))
1734 if (cdev->private->state != DEV_STATE_OFFLINE)
1736 ccw_device_online(cdev);
1737 while (!dev_fsm_final_state(cdev))
1739 if (cdev->private->state != DEV_STATE_ONLINE)
1743 spin_unlock_irq(cdev->ccwlock);
1748 ccw_device_probe_console(void)
1750 struct subchannel *sch;
1753 if (xchg(&console_cdev_in_use, 1) != 0)
1754 return ERR_PTR(-EBUSY);
1755 sch = cio_probe_console();
1757 console_cdev_in_use = 0;
1758 return (void *) sch;
1760 memset(&console_cdev, 0, sizeof(struct ccw_device));
1761 memset(&console_private, 0, sizeof(struct ccw_device_private));
1762 console_cdev.private = &console_private;
1763 console_private.cdev = &console_cdev;
1764 ret = ccw_device_console_enable(&console_cdev, sch);
1766 cio_release_console();
1767 console_cdev_in_use = 0;
1768 return ERR_PTR(ret);
1770 console_cdev.online = 1;
1771 return &console_cdev;
1775 const char *cio_get_console_cdev_name(struct subchannel *sch)
1777 snprintf(console_cdev_name, 10, "0.%x.%04x",
1778 sch->schid.ssid, sch->schib.pmcw.dev);
1779 return (const char *)console_cdev_name;
1784 * get ccw_device matching the busid, but only if owned by cdrv
1787 __ccwdev_check_busid(struct device *dev, void *id)
1793 return (strcmp(bus_id, dev_name(dev)) == 0);
1798 * get_ccwdev_by_busid() - obtain device from a bus id
1799 * @cdrv: driver the device is owned by
1800 * @bus_id: bus id of the device to be searched
1802 * This function searches all devices owned by @cdrv for a device with a bus
1803 * id matching @bus_id.
1805 * If a match is found, its reference count of the found device is increased
1806 * and it is returned; else %NULL is returned.
1808 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1812 struct device_driver *drv;
1814 drv = get_driver(&cdrv->driver);
1818 dev = driver_find_device(drv, NULL, (void *)bus_id,
1819 __ccwdev_check_busid);
1822 return dev ? to_ccwdev(dev) : NULL;
1825 /************************** device driver handling ************************/
1827 /* This is the implementation of the ccw_driver class. The probe, remove
1828 * and release methods are initially very similar to the device_driver
1829 * implementations, with the difference that they have ccw_device
1832 * A ccw driver also contains the information that is needed for
1836 ccw_device_probe (struct device *dev)
1838 struct ccw_device *cdev = to_ccwdev(dev);
1839 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1842 cdev->drv = cdrv; /* to let the driver call _set_online */
1844 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1855 ccw_device_remove (struct device *dev)
1857 struct ccw_device *cdev = to_ccwdev(dev);
1858 struct ccw_driver *cdrv = cdev->drv;
1865 spin_lock_irq(cdev->ccwlock);
1866 ret = ccw_device_offline(cdev);
1867 spin_unlock_irq(cdev->ccwlock);
1869 wait_event(cdev->private->wait_q,
1870 dev_fsm_final_state(cdev));
1872 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1873 "device 0.%x.%04x\n",
1874 ret, cdev->private->dev_id.ssid,
1875 cdev->private->dev_id.devno);
1876 /* Give up reference obtained in ccw_device_set_online(). */
1877 put_device(&cdev->dev);
1879 ccw_device_set_timeout(cdev, 0);
1884 static void ccw_device_shutdown(struct device *dev)
1886 struct ccw_device *cdev;
1888 cdev = to_ccwdev(dev);
1889 if (cdev->drv && cdev->drv->shutdown)
1890 cdev->drv->shutdown(cdev);
1894 struct bus_type ccw_bus_type = {
1896 .match = ccw_bus_match,
1897 .uevent = ccw_uevent,
1898 .probe = ccw_device_probe,
1899 .remove = ccw_device_remove,
1900 .shutdown = ccw_device_shutdown,
1904 * ccw_driver_register() - register a ccw driver
1905 * @cdriver: driver to be registered
1907 * This function is mainly a wrapper around driver_register().
1909 * %0 on success and a negative error value on failure.
1911 int ccw_driver_register(struct ccw_driver *cdriver)
1913 struct device_driver *drv = &cdriver->driver;
1915 drv->bus = &ccw_bus_type;
1916 drv->name = cdriver->name;
1917 drv->owner = cdriver->owner;
1919 return driver_register(drv);
1923 * ccw_driver_unregister() - deregister a ccw driver
1924 * @cdriver: driver to be deregistered
1926 * This function is mainly a wrapper around driver_unregister().
1928 void ccw_driver_unregister(struct ccw_driver *cdriver)
1930 driver_unregister(&cdriver->driver);
1933 /* Helper func for qdio. */
1934 struct subchannel_id
1935 ccw_device_get_subchannel_id(struct ccw_device *cdev)
1937 struct subchannel *sch;
1939 sch = to_subchannel(cdev->dev.parent);
1943 MODULE_LICENSE("GPL");
1944 EXPORT_SYMBOL(ccw_device_set_online);
1945 EXPORT_SYMBOL(ccw_device_set_offline);
1946 EXPORT_SYMBOL(ccw_driver_register);
1947 EXPORT_SYMBOL(ccw_driver_unregister);
1948 EXPORT_SYMBOL(get_ccwdev_by_busid);
1949 EXPORT_SYMBOL(ccw_bus_type);
1950 EXPORT_SYMBOL(ccw_device_work);
1951 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);