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 int io_subchannel_prepare(struct subchannel
*sch
)
143 struct ccw_device
*cdev
;
145 * Don't allow suspend while a ccw device registration
146 * is still outstanding.
148 cdev
= sch_get_cdev(sch
);
149 if (cdev
&& !device_is_registered(&cdev
->dev
))
154 static struct css_driver io_subchannel_driver
= {
155 .owner
= THIS_MODULE
,
156 .subchannel_type
= io_subchannel_ids
,
157 .name
= "io_subchannel",
158 .irq
= io_subchannel_irq
,
159 .sch_event
= io_subchannel_sch_event
,
160 .chp_event
= io_subchannel_chp_event
,
161 .probe
= io_subchannel_probe
,
162 .remove
= io_subchannel_remove
,
163 .shutdown
= io_subchannel_shutdown
,
164 .prepare
= io_subchannel_prepare
,
167 struct workqueue_struct
*ccw_device_work
;
168 wait_queue_head_t ccw_device_init_wq
;
169 atomic_t ccw_device_init_count
;
171 static void recovery_func(unsigned long data
);
174 init_ccw_bus_type (void)
178 init_waitqueue_head(&ccw_device_init_wq
);
179 atomic_set(&ccw_device_init_count
, 0);
180 setup_timer(&recovery_timer
, recovery_func
, 0);
182 ccw_device_work
= create_singlethread_workqueue("cio");
183 if (!ccw_device_work
)
184 return -ENOMEM
; /* FIXME: better errno ? */
185 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
187 ret
= -ENOMEM
; /* FIXME: better errno ? */
190 if ((ret
= bus_register (&ccw_bus_type
)))
193 ret
= css_driver_register(&io_subchannel_driver
);
197 wait_event(ccw_device_init_wq
,
198 atomic_read(&ccw_device_init_count
) == 0);
199 flush_workqueue(ccw_device_work
);
203 destroy_workqueue(ccw_device_work
);
205 destroy_workqueue(slow_path_wq
);
210 cleanup_ccw_bus_type (void)
212 css_driver_unregister(&io_subchannel_driver
);
213 bus_unregister(&ccw_bus_type
);
214 destroy_workqueue(ccw_device_work
);
217 subsys_initcall(init_ccw_bus_type
);
218 module_exit(cleanup_ccw_bus_type
);
220 /************************ device handling **************************/
223 * A ccw_device has some interfaces in sysfs in addition to the
225 * The following entries are designed to export the information which
226 * resided in 2.4 in /proc/subchannels. Subchannel and device number
227 * are obvious, so they don't have an entry :)
228 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
231 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
233 struct subchannel
*sch
= to_subchannel(dev
);
234 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
239 for (chp
= 0; chp
< 8; chp
++) {
241 if (ssd
->path_mask
& mask
)
242 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
244 ret
+= sprintf(buf
+ ret
, "00 ");
246 ret
+= sprintf (buf
+ret
, "\n");
247 return min((ssize_t
)PAGE_SIZE
, ret
);
251 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
253 struct subchannel
*sch
= to_subchannel(dev
);
254 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
256 return sprintf (buf
, "%02x %02x %02x\n",
257 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
261 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
263 struct ccw_device
*cdev
= to_ccwdev(dev
);
264 struct ccw_device_id
*id
= &(cdev
->id
);
266 if (id
->dev_type
!= 0)
267 return sprintf(buf
, "%04x/%02x\n",
268 id
->dev_type
, id
->dev_model
);
270 return sprintf(buf
, "n/a\n");
274 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
276 struct ccw_device
*cdev
= to_ccwdev(dev
);
277 struct ccw_device_id
*id
= &(cdev
->id
);
279 return sprintf(buf
, "%04x/%02x\n",
280 id
->cu_type
, id
->cu_model
);
284 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
286 struct ccw_device
*cdev
= to_ccwdev(dev
);
287 struct ccw_device_id
*id
= &(cdev
->id
);
290 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
292 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
296 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
298 struct ccw_device
*cdev
= to_ccwdev(dev
);
300 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
303 int ccw_device_is_orphan(struct ccw_device
*cdev
)
305 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
308 static void ccw_device_unregister(struct ccw_device
*cdev
)
310 if (test_and_clear_bit(1, &cdev
->private->registered
))
311 device_del(&cdev
->dev
);
314 static void ccw_device_remove_orphan_cb(struct work_struct
*work
)
316 struct ccw_device_private
*priv
;
317 struct ccw_device
*cdev
;
319 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
321 ccw_device_unregister(cdev
);
322 put_device(&cdev
->dev
);
323 /* Release cdev reference for workqueue processing. */
324 put_device(&cdev
->dev
);
328 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
333 * Forced offline in disconnected state means
334 * 'throw away device'.
336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev
->dev
))
339 if (ccw_device_is_orphan(cdev
)) {
341 * Deregister ccw device.
342 * Unfortunately, we cannot do this directly from the
345 spin_lock_irqsave(cdev
->ccwlock
, flags
);
346 cdev
->private->state
= DEV_STATE_NOT_OPER
;
347 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
348 PREPARE_WORK(&cdev
->private->kick_work
,
349 ccw_device_remove_orphan_cb
);
350 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
352 /* Deregister subchannel, which will kill the ccw device. */
353 ccw_device_schedule_sch_unregister(cdev
);
357 * ccw_device_set_offline() - disable a ccw device for I/O
358 * @cdev: target ccw device
360 * This function calls the driver's set_offline() function for @cdev, if
361 * given, and then disables @cdev.
363 * %0 on success and a negative error value on failure.
365 * enabled, ccw device lock not held
367 int ccw_device_set_offline(struct ccw_device
*cdev
)
373 if (!cdev
->online
|| !cdev
->drv
)
376 if (cdev
->drv
->set_offline
) {
377 ret
= cdev
->drv
->set_offline(cdev
);
382 spin_lock_irq(cdev
->ccwlock
);
383 ret
= ccw_device_offline(cdev
);
384 if (ret
== -ENODEV
) {
385 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
386 cdev
->private->state
= DEV_STATE_OFFLINE
;
387 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
389 spin_unlock_irq(cdev
->ccwlock
);
390 /* Give up reference from ccw_device_set_online(). */
391 put_device(&cdev
->dev
);
394 spin_unlock_irq(cdev
->ccwlock
);
396 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
397 /* Give up reference from ccw_device_set_online(). */
398 put_device(&cdev
->dev
);
400 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
401 "device 0.%x.%04x\n",
402 ret
, cdev
->private->dev_id
.ssid
,
403 cdev
->private->dev_id
.devno
);
410 * ccw_device_set_online() - enable a ccw device for I/O
411 * @cdev: target ccw device
413 * This function first enables @cdev and then calls the driver's set_online()
414 * function for @cdev, if given. If set_online() returns an error, @cdev is
417 * %0 on success and a negative error value on failure.
419 * enabled, ccw device lock not held
421 int ccw_device_set_online(struct ccw_device
*cdev
)
427 if (cdev
->online
|| !cdev
->drv
)
429 /* Hold on to an extra reference while device is online. */
430 if (!get_device(&cdev
->dev
))
433 spin_lock_irq(cdev
->ccwlock
);
434 ret
= ccw_device_online(cdev
);
435 spin_unlock_irq(cdev
->ccwlock
);
437 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
439 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
440 "device 0.%x.%04x\n",
441 ret
, cdev
->private->dev_id
.ssid
,
442 cdev
->private->dev_id
.devno
);
443 /* Give up online reference since onlining failed. */
444 put_device(&cdev
->dev
);
447 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
448 /* Give up online reference since onlining failed. */
449 put_device(&cdev
->dev
);
452 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
456 spin_lock_irq(cdev
->ccwlock
);
457 ret
= ccw_device_offline(cdev
);
458 spin_unlock_irq(cdev
->ccwlock
);
460 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
462 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
463 "device 0.%x.%04x\n",
464 ret
, cdev
->private->dev_id
.ssid
,
465 cdev
->private->dev_id
.devno
);
466 /* Give up online reference since onlining failed. */
467 put_device(&cdev
->dev
);
468 return (ret
== 0) ? -ENODEV
: ret
;
471 static int online_store_handle_offline(struct ccw_device
*cdev
)
473 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
474 ccw_device_remove_disconnected(cdev
);
475 else if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->set_offline
)
476 return ccw_device_set_offline(cdev
);
480 static int online_store_recog_and_online(struct ccw_device
*cdev
)
484 /* Do device recognition, if needed. */
485 if (cdev
->private->state
== DEV_STATE_BOXED
) {
486 ret
= ccw_device_recognition(cdev
);
488 CIO_MSG_EVENT(0, "Couldn't start recognition "
489 "for device 0.%x.%04x (ret=%d)\n",
490 cdev
->private->dev_id
.ssid
,
491 cdev
->private->dev_id
.devno
, ret
);
494 wait_event(cdev
->private->wait_q
,
495 cdev
->private->flags
.recog_done
);
496 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
497 /* recognition failed */
500 if (cdev
->drv
&& cdev
->drv
->set_online
)
501 ccw_device_set_online(cdev
);
505 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
509 ret
= online_store_recog_and_online(cdev
);
512 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
513 ret
= ccw_device_stlck(cdev
);
516 if (cdev
->id
.cu_type
== 0)
517 cdev
->private->state
= DEV_STATE_NOT_OPER
;
518 ret
= online_store_recog_and_online(cdev
);
525 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
526 const char *buf
, size_t count
)
528 struct ccw_device
*cdev
= to_ccwdev(dev
);
532 if ((cdev
->private->state
!= DEV_STATE_OFFLINE
&&
533 cdev
->private->state
!= DEV_STATE_ONLINE
&&
534 cdev
->private->state
!= DEV_STATE_BOXED
&&
535 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) ||
536 atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
539 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
540 atomic_set(&cdev
->private->onoff
, 0);
543 if (!strncmp(buf
, "force\n", count
)) {
549 ret
= strict_strtoul(buf
, 16, &i
);
555 ret
= online_store_handle_offline(cdev
);
558 ret
= online_store_handle_online(cdev
, force
);
565 module_put(cdev
->drv
->owner
);
566 atomic_set(&cdev
->private->onoff
, 0);
567 return (ret
< 0) ? ret
: count
;
571 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
573 struct ccw_device
*cdev
= to_ccwdev(dev
);
574 struct subchannel
*sch
;
576 if (ccw_device_is_orphan(cdev
))
577 return sprintf(buf
, "no device\n");
578 switch (cdev
->private->state
) {
579 case DEV_STATE_BOXED
:
580 return sprintf(buf
, "boxed\n");
581 case DEV_STATE_DISCONNECTED
:
582 case DEV_STATE_DISCONNECTED_SENSE_ID
:
583 case DEV_STATE_NOT_OPER
:
584 sch
= to_subchannel(dev
->parent
);
586 return sprintf(buf
, "no path\n");
588 return sprintf(buf
, "no device\n");
590 /* All other states considered fine. */
591 return sprintf(buf
, "good\n");
595 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
596 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
597 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
598 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
599 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
600 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
601 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
603 static struct attribute
*io_subchannel_attrs
[] = {
604 &dev_attr_chpids
.attr
,
605 &dev_attr_pimpampom
.attr
,
609 static struct attribute_group io_subchannel_attr_group
= {
610 .attrs
= io_subchannel_attrs
,
613 static struct attribute
* ccwdev_attrs
[] = {
614 &dev_attr_devtype
.attr
,
615 &dev_attr_cutype
.attr
,
616 &dev_attr_modalias
.attr
,
617 &dev_attr_online
.attr
,
618 &dev_attr_cmb_enable
.attr
,
619 &dev_attr_availability
.attr
,
623 static struct attribute_group ccwdev_attr_group
= {
624 .attrs
= ccwdev_attrs
,
627 static struct attribute_group
*ccwdev_attr_groups
[] = {
632 /* this is a simple abstraction for device_register that sets the
633 * correct bus type and adds the bus specific files */
634 static int ccw_device_register(struct ccw_device
*cdev
)
636 struct device
*dev
= &cdev
->dev
;
639 dev
->bus
= &ccw_bus_type
;
641 if ((ret
= device_add(dev
)))
644 set_bit(1, &cdev
->private->registered
);
649 struct ccw_dev_id dev_id
;
650 struct ccw_device
* sibling
;
654 match_devno(struct device
* dev
, void * data
)
656 struct match_data
* d
= data
;
657 struct ccw_device
* cdev
;
659 cdev
= to_ccwdev(dev
);
660 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
661 !ccw_device_is_orphan(cdev
) &&
662 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
663 (cdev
!= d
->sibling
))
668 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
669 struct ccw_device
*sibling
)
672 struct match_data data
;
674 data
.dev_id
= *dev_id
;
675 data
.sibling
= sibling
;
676 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
678 return dev
? to_ccwdev(dev
) : NULL
;
681 static int match_orphan(struct device
*dev
, void *data
)
683 struct ccw_dev_id
*dev_id
;
684 struct ccw_device
*cdev
;
687 cdev
= to_ccwdev(dev
);
688 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
691 static struct ccw_device
*
692 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
693 struct ccw_dev_id
*dev_id
)
697 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
700 return dev
? to_ccwdev(dev
) : NULL
;
703 void ccw_device_do_unbind_bind(struct work_struct
*work
)
705 struct ccw_device_private
*priv
;
706 struct ccw_device
*cdev
;
707 struct subchannel
*sch
;
710 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
712 sch
= to_subchannel(cdev
->dev
.parent
);
714 if (test_bit(1, &cdev
->private->registered
)) {
715 device_release_driver(&cdev
->dev
);
716 ret
= device_attach(&cdev
->dev
);
717 WARN_ON(ret
== -ENODEV
);
722 ccw_device_release(struct device
*dev
)
724 struct ccw_device
*cdev
;
726 cdev
= to_ccwdev(dev
);
727 /* Release reference of parent subchannel. */
728 put_device(cdev
->dev
.parent
);
729 kfree(cdev
->private);
733 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
735 struct ccw_device
*cdev
;
737 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
739 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
740 GFP_KERNEL
| GFP_DMA
);
745 return ERR_PTR(-ENOMEM
);
748 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
749 struct ccw_device
*cdev
)
751 cdev
->private->cdev
= cdev
;
752 atomic_set(&cdev
->private->onoff
, 0);
753 cdev
->dev
.parent
= &sch
->dev
;
754 cdev
->dev
.release
= ccw_device_release
;
755 INIT_WORK(&cdev
->private->kick_work
, NULL
);
756 cdev
->dev
.groups
= ccwdev_attr_groups
;
757 /* Do first half of device_register. */
758 device_initialize(&cdev
->dev
);
759 if (!get_device(&sch
->dev
)) {
760 /* Release reference from device_initialize(). */
761 put_device(&cdev
->dev
);
767 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
769 struct ccw_device
*cdev
;
772 cdev
= io_subchannel_allocate_dev(sch
);
774 ret
= io_subchannel_initialize_dev(sch
, cdev
);
783 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
785 static void sch_attach_device(struct subchannel
*sch
,
786 struct ccw_device
*cdev
)
788 css_update_ssd_info(sch
);
789 spin_lock_irq(sch
->lock
);
790 sch_set_cdev(sch
, cdev
);
791 cdev
->private->schid
= sch
->schid
;
792 cdev
->ccwlock
= sch
->lock
;
793 ccw_device_trigger_reprobe(cdev
);
794 spin_unlock_irq(sch
->lock
);
797 static void sch_attach_disconnected_device(struct subchannel
*sch
,
798 struct ccw_device
*cdev
)
800 struct subchannel
*other_sch
;
803 /* Get reference for new parent. */
804 if (!get_device(&sch
->dev
))
806 other_sch
= to_subchannel(cdev
->dev
.parent
);
807 /* Note: device_move() changes cdev->dev.parent */
808 ret
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
810 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
811 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
812 cdev
->private->dev_id
.devno
, ret
);
813 /* Put reference for new parent. */
814 put_device(&sch
->dev
);
817 sch_set_cdev(other_sch
, NULL
);
818 /* No need to keep a subchannel without ccw device around. */
819 css_sch_device_unregister(other_sch
);
820 sch_attach_device(sch
, cdev
);
821 /* Put reference for old parent. */
822 put_device(&other_sch
->dev
);
825 static void sch_attach_orphaned_device(struct subchannel
*sch
,
826 struct ccw_device
*cdev
)
829 struct subchannel
*pseudo_sch
;
831 /* Get reference for new parent. */
832 if (!get_device(&sch
->dev
))
834 pseudo_sch
= to_subchannel(cdev
->dev
.parent
);
836 * Try to move the ccw device to its new subchannel.
837 * Note: device_move() changes cdev->dev.parent
839 ret
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
841 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
842 "failed (ret=%d)!\n",
843 cdev
->private->dev_id
.ssid
,
844 cdev
->private->dev_id
.devno
, ret
);
845 /* Put reference for new parent. */
846 put_device(&sch
->dev
);
849 sch_attach_device(sch
, cdev
);
850 /* Put reference on pseudo subchannel. */
851 put_device(&pseudo_sch
->dev
);
854 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
856 struct ccw_device
*cdev
;
858 /* Need to allocate a new ccw device. */
859 cdev
= io_subchannel_create_ccwdev(sch
);
861 /* OK, we did everything we could... */
862 css_sch_device_unregister(sch
);
865 spin_lock_irq(sch
->lock
);
866 sch_set_cdev(sch
, cdev
);
867 spin_unlock_irq(sch
->lock
);
868 /* Start recognition for the new ccw device. */
869 if (io_subchannel_recog(cdev
, sch
)) {
870 spin_lock_irq(sch
->lock
);
871 sch_set_cdev(sch
, NULL
);
872 spin_unlock_irq(sch
->lock
);
873 css_sch_device_unregister(sch
);
874 /* Put reference from io_subchannel_create_ccwdev(). */
875 put_device(&sch
->dev
);
876 /* Give up initial reference. */
877 put_device(&cdev
->dev
);
882 void ccw_device_move_to_orphanage(struct work_struct
*work
)
884 struct ccw_device_private
*priv
;
885 struct ccw_device
*cdev
;
886 struct ccw_device
*replacing_cdev
;
887 struct subchannel
*sch
;
889 struct channel_subsystem
*css
;
890 struct ccw_dev_id dev_id
;
892 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
894 sch
= to_subchannel(cdev
->dev
.parent
);
895 css
= to_css(sch
->dev
.parent
);
896 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
897 dev_id
.ssid
= sch
->schid
.ssid
;
899 /* Increase refcount for pseudo subchannel. */
900 get_device(&css
->pseudo_subchannel
->dev
);
902 * Move the orphaned ccw device to the orphanage so the replacing
903 * ccw device can take its place on the subchannel.
904 * Note: device_move() changes cdev->dev.parent
906 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
,
909 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
910 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
911 cdev
->private->dev_id
.devno
, ret
);
912 /* Decrease refcount for pseudo subchannel again. */
913 put_device(&css
->pseudo_subchannel
->dev
);
916 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
918 * Search for the replacing ccw device
919 * - among the disconnected devices
922 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
923 if (replacing_cdev
) {
924 sch_attach_disconnected_device(sch
, replacing_cdev
);
925 /* Release reference from get_disc_ccwdev_by_dev_id() */
926 put_device(&replacing_cdev
->dev
);
927 /* Release reference of subchannel from old cdev. */
928 put_device(&sch
->dev
);
931 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
932 if (replacing_cdev
) {
933 sch_attach_orphaned_device(sch
, replacing_cdev
);
934 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
935 put_device(&replacing_cdev
->dev
);
936 /* Release reference of subchannel from old cdev. */
937 put_device(&sch
->dev
);
940 sch_create_and_recog_new_device(sch
);
941 /* Release reference of subchannel from old cdev. */
942 put_device(&sch
->dev
);
946 * Register recognized device.
949 io_subchannel_register(struct work_struct
*work
)
951 struct ccw_device_private
*priv
;
952 struct ccw_device
*cdev
;
953 struct subchannel
*sch
;
957 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
959 sch
= to_subchannel(cdev
->dev
.parent
);
961 * Check if subchannel is still registered. It may have become
962 * unregistered if a machine check hit us after finishing
963 * device recognition but before the register work could be
966 if (!device_is_registered(&sch
->dev
))
968 css_update_ssd_info(sch
);
970 * io_subchannel_register() will also be called after device
971 * recognition has been done for a boxed device (which will already
972 * be registered). We need to reprobe since we may now have sense id
975 if (device_is_registered(&cdev
->dev
)) {
977 ret
= device_reprobe(&cdev
->dev
);
979 /* We can't do much here. */
980 CIO_MSG_EVENT(0, "device_reprobe() returned"
981 " %d for 0.%x.%04x\n", ret
,
982 cdev
->private->dev_id
.ssid
,
983 cdev
->private->dev_id
.devno
);
988 * Now we know this subchannel will stay, we can throw
989 * our delayed uevent.
991 dev_set_uevent_suppress(&sch
->dev
, 0);
992 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
993 /* make it known to the system */
994 ret
= ccw_device_register(cdev
);
996 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
997 cdev
->private->dev_id
.ssid
,
998 cdev
->private->dev_id
.devno
, ret
);
999 spin_lock_irqsave(sch
->lock
, flags
);
1000 sch_set_cdev(sch
, NULL
);
1001 spin_unlock_irqrestore(sch
->lock
, flags
);
1002 /* Release initial device reference. */
1003 put_device(&cdev
->dev
);
1007 cdev
->private->flags
.recog_done
= 1;
1008 wake_up(&cdev
->private->wait_q
);
1010 /* Release reference for workqueue processing. */
1011 put_device(&cdev
->dev
);
1012 if (atomic_dec_and_test(&ccw_device_init_count
))
1013 wake_up(&ccw_device_init_wq
);
1016 static void ccw_device_call_sch_unregister(struct work_struct
*work
)
1018 struct ccw_device_private
*priv
;
1019 struct ccw_device
*cdev
;
1020 struct subchannel
*sch
;
1022 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1024 /* Get subchannel reference for local processing. */
1025 if (!get_device(cdev
->dev
.parent
))
1027 sch
= to_subchannel(cdev
->dev
.parent
);
1028 css_sch_device_unregister(sch
);
1029 /* Reset intparm to zeroes. */
1030 sch
->config
.intparm
= 0;
1031 cio_commit_config(sch
);
1032 /* Release cdev reference for workqueue processing.*/
1033 put_device(&cdev
->dev
);
1034 /* Release subchannel reference for local processing. */
1035 put_device(&sch
->dev
);
1038 void ccw_device_schedule_sch_unregister(struct ccw_device
*cdev
)
1040 PREPARE_WORK(&cdev
->private->kick_work
,
1041 ccw_device_call_sch_unregister
);
1042 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1046 * subchannel recognition done. Called from the state machine.
1049 io_subchannel_recog_done(struct ccw_device
*cdev
)
1051 if (css_init_done
== 0) {
1052 cdev
->private->flags
.recog_done
= 1;
1055 switch (cdev
->private->state
) {
1056 case DEV_STATE_BOXED
:
1057 /* Device did not respond in time. */
1058 case DEV_STATE_NOT_OPER
:
1059 cdev
->private->flags
.recog_done
= 1;
1060 /* Remove device found not operational. */
1061 if (!get_device(&cdev
->dev
))
1063 ccw_device_schedule_sch_unregister(cdev
);
1064 if (atomic_dec_and_test(&ccw_device_init_count
))
1065 wake_up(&ccw_device_init_wq
);
1067 case DEV_STATE_OFFLINE
:
1069 * We can't register the device in interrupt context so
1070 * we schedule a work item.
1072 if (!get_device(&cdev
->dev
))
1074 PREPARE_WORK(&cdev
->private->kick_work
,
1075 io_subchannel_register
);
1076 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1082 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1085 struct ccw_device_private
*priv
;
1087 sch_set_cdev(sch
, cdev
);
1088 cdev
->ccwlock
= sch
->lock
;
1090 /* Init private data. */
1091 priv
= cdev
->private;
1092 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1093 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1094 priv
->schid
= sch
->schid
;
1095 priv
->state
= DEV_STATE_NOT_OPER
;
1096 INIT_LIST_HEAD(&priv
->cmb_list
);
1097 init_waitqueue_head(&priv
->wait_q
);
1098 init_timer(&priv
->timer
);
1100 /* Set an initial name for the device. */
1101 if (cio_is_console(sch
->schid
))
1102 cdev
->dev
.init_name
= cio_get_console_cdev_name(sch
);
1104 dev_set_name(&cdev
->dev
, "0.%x.%04x",
1105 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1107 /* Increase counter of devices currently in recognition. */
1108 atomic_inc(&ccw_device_init_count
);
1110 /* Start async. device sensing. */
1111 spin_lock_irq(sch
->lock
);
1112 rc
= ccw_device_recognition(cdev
);
1113 spin_unlock_irq(sch
->lock
);
1115 if (atomic_dec_and_test(&ccw_device_init_count
))
1116 wake_up(&ccw_device_init_wq
);
1121 static void ccw_device_move_to_sch(struct work_struct
*work
)
1123 struct ccw_device_private
*priv
;
1125 struct subchannel
*sch
;
1126 struct ccw_device
*cdev
;
1127 struct subchannel
*former_parent
;
1129 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1132 former_parent
= to_subchannel(cdev
->dev
.parent
);
1133 /* Get reference for new parent. */
1134 if (!get_device(&sch
->dev
))
1136 mutex_lock(&sch
->reg_mutex
);
1138 * Try to move the ccw device to its new subchannel.
1139 * Note: device_move() changes cdev->dev.parent
1141 rc
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
1142 mutex_unlock(&sch
->reg_mutex
);
1144 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
1145 "0.%x.%04x failed (ret=%d)!\n",
1146 cdev
->private->dev_id
.ssid
,
1147 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1148 sch
->schid
.sch_no
, rc
);
1149 css_sch_device_unregister(sch
);
1150 /* Put reference for new parent again. */
1151 put_device(&sch
->dev
);
1154 if (!sch_is_pseudo_sch(former_parent
)) {
1155 spin_lock_irq(former_parent
->lock
);
1156 sch_set_cdev(former_parent
, NULL
);
1157 spin_unlock_irq(former_parent
->lock
);
1158 css_sch_device_unregister(former_parent
);
1159 /* Reset intparm to zeroes. */
1160 former_parent
->config
.intparm
= 0;
1161 cio_commit_config(former_parent
);
1163 sch_attach_device(sch
, cdev
);
1165 /* Put reference for old parent. */
1166 put_device(&former_parent
->dev
);
1167 put_device(&cdev
->dev
);
1170 static void io_subchannel_irq(struct subchannel
*sch
)
1172 struct ccw_device
*cdev
;
1174 cdev
= sch_get_cdev(sch
);
1176 CIO_TRACE_EVENT(3, "IRQ");
1177 CIO_TRACE_EVENT(3, dev_name(&sch
->dev
));
1179 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1182 void io_subchannel_init_config(struct subchannel
*sch
)
1184 memset(&sch
->config
, 0, sizeof(sch
->config
));
1185 sch
->config
.csense
= 1;
1186 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1187 if ((sch
->schib
.pmcw
.pim
& (sch
->schib
.pmcw
.pim
- 1)) != 0)
1191 static void io_subchannel_init_fields(struct subchannel
*sch
)
1193 if (cio_is_console(sch
->schid
))
1196 sch
->opm
= chp_get_sch_opm(sch
);
1197 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1198 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1200 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1201 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1202 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1203 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1204 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1206 io_subchannel_init_config(sch
);
1209 static void io_subchannel_do_unreg(struct work_struct
*work
)
1211 struct subchannel
*sch
;
1213 sch
= container_of(work
, struct subchannel
, work
);
1214 css_sch_device_unregister(sch
);
1215 /* Reset intparm to zeroes. */
1216 sch
->config
.intparm
= 0;
1217 cio_commit_config(sch
);
1218 put_device(&sch
->dev
);
1221 /* Schedule unregister if we have no cdev. */
1222 static void io_subchannel_schedule_removal(struct subchannel
*sch
)
1224 get_device(&sch
->dev
);
1225 INIT_WORK(&sch
->work
, io_subchannel_do_unreg
);
1226 queue_work(slow_path_wq
, &sch
->work
);
1230 * Note: We always return 0 so that we bind to the device even on error.
1231 * This is needed so that our remove function is called on unregister.
1233 static int io_subchannel_probe(struct subchannel
*sch
)
1235 struct ccw_device
*cdev
;
1237 unsigned long flags
;
1238 struct ccw_dev_id dev_id
;
1240 cdev
= sch_get_cdev(sch
);
1242 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1243 &io_subchannel_attr_group
);
1245 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1246 "attributes for subchannel "
1247 "0.%x.%04x (rc=%d)\n",
1248 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1250 * This subchannel already has an associated ccw_device.
1251 * Throw the delayed uevent for the subchannel, register
1252 * the ccw_device and exit. This happens for all early
1253 * devices, e.g. the console.
1255 dev_set_uevent_suppress(&sch
->dev
, 0);
1256 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1257 cdev
->dev
.groups
= ccwdev_attr_groups
;
1258 device_initialize(&cdev
->dev
);
1259 ccw_device_register(cdev
);
1261 * Check if the device is already online. If it is
1262 * the reference count needs to be corrected since we
1263 * didn't obtain a reference in ccw_device_set_online.
1265 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1266 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1267 cdev
->private->state
!= DEV_STATE_BOXED
)
1268 get_device(&cdev
->dev
);
1271 io_subchannel_init_fields(sch
);
1272 rc
= cio_commit_config(sch
);
1275 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1276 &io_subchannel_attr_group
);
1279 /* Allocate I/O subchannel private data. */
1280 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1281 GFP_KERNEL
| GFP_DMA
);
1285 * First check if a fitting device may be found amongst the
1286 * disconnected devices or in the orphanage.
1288 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1289 dev_id
.ssid
= sch
->schid
.ssid
;
1290 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1292 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1296 * Schedule moving the device until when we have a registered
1297 * subchannel to move to and succeed the probe. We can
1298 * unregister later again, when the probe is through.
1300 cdev
->private->sch
= sch
;
1301 PREPARE_WORK(&cdev
->private->kick_work
,
1302 ccw_device_move_to_sch
);
1303 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1306 cdev
= io_subchannel_create_ccwdev(sch
);
1309 rc
= io_subchannel_recog(cdev
, sch
);
1311 spin_lock_irqsave(sch
->lock
, flags
);
1312 io_subchannel_recog_done(cdev
);
1313 spin_unlock_irqrestore(sch
->lock
, flags
);
1317 kfree(sch
->private);
1318 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1320 io_subchannel_schedule_removal(sch
);
1325 io_subchannel_remove (struct subchannel
*sch
)
1327 struct ccw_device
*cdev
;
1328 unsigned long flags
;
1330 cdev
= sch_get_cdev(sch
);
1333 /* Set ccw device to not operational and drop reference. */
1334 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1335 sch_set_cdev(sch
, NULL
);
1336 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1337 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1338 ccw_device_unregister(cdev
);
1339 put_device(&cdev
->dev
);
1340 kfree(sch
->private);
1341 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1345 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1347 struct ccw_device
*cdev
;
1349 cdev
= sch_get_cdev(sch
);
1352 return ccw_device_notify(cdev
, event
);
1355 static void io_subchannel_verify(struct subchannel
*sch
)
1357 struct ccw_device
*cdev
;
1359 cdev
= sch_get_cdev(sch
);
1361 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1364 static int check_for_io_on_path(struct subchannel
*sch
, int mask
)
1366 if (cio_update_schib(sch
))
1368 if (scsw_actl(&sch
->schib
.scsw
) && sch
->schib
.pmcw
.lpum
== mask
)
1373 static void terminate_internal_io(struct subchannel
*sch
,
1374 struct ccw_device
*cdev
)
1376 if (cio_clear(sch
)) {
1377 /* Recheck device in case clear failed. */
1380 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1382 css_schedule_eval(sch
->schid
);
1385 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1386 /* Request retry of internal operation. */
1387 cdev
->private->flags
.intretry
= 1;
1390 cdev
->handler(cdev
, cdev
->private->intparm
,
1394 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1396 struct ccw_device
*cdev
;
1398 cdev
= sch_get_cdev(sch
);
1401 if (check_for_io_on_path(sch
, mask
)) {
1402 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1403 ccw_device_kill_io(cdev
);
1405 terminate_internal_io(sch
, cdev
);
1406 /* Re-start path verification. */
1407 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1410 /* trigger path verification. */
1411 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1415 static int io_subchannel_chp_event(struct subchannel
*sch
,
1416 struct chp_link
*link
, int event
)
1420 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1427 io_subchannel_terminate_path(sch
, mask
);
1432 io_subchannel_verify(sch
);
1435 if (cio_update_schib(sch
))
1437 io_subchannel_terminate_path(sch
, mask
);
1440 if (cio_update_schib(sch
))
1442 sch
->lpm
|= mask
& sch
->opm
;
1443 io_subchannel_verify(sch
);
1450 io_subchannel_shutdown(struct subchannel
*sch
)
1452 struct ccw_device
*cdev
;
1455 cdev
= sch_get_cdev(sch
);
1457 if (cio_is_console(sch
->schid
))
1459 if (!sch
->schib
.pmcw
.ena
)
1460 /* Nothing to do. */
1462 ret
= cio_disable_subchannel(sch
);
1464 /* Subchannel is disabled, we're done. */
1466 cdev
->private->state
= DEV_STATE_QUIESCE
;
1468 cdev
->handler(cdev
, cdev
->private->intparm
,
1470 ret
= ccw_device_cancel_halt_clear(cdev
);
1471 if (ret
== -EBUSY
) {
1472 ccw_device_set_timeout(cdev
, HZ
/10);
1473 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1475 cio_disable_subchannel(sch
);
1478 static int io_subchannel_get_status(struct subchannel
*sch
)
1482 if (stsch(sch
->schid
, &schib
) || !schib
.pmcw
.dnv
)
1484 if (sch
->schib
.pmcw
.dnv
&& (schib
.pmcw
.dev
!= sch
->schib
.pmcw
.dev
))
1485 return CIO_REVALIDATE
;
1491 static int device_is_disconnected(struct ccw_device
*cdev
)
1495 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1496 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1499 static int recovery_check(struct device
*dev
, void *data
)
1501 struct ccw_device
*cdev
= to_ccwdev(dev
);
1504 spin_lock_irq(cdev
->ccwlock
);
1505 switch (cdev
->private->state
) {
1506 case DEV_STATE_DISCONNECTED
:
1507 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1508 cdev
->private->dev_id
.ssid
,
1509 cdev
->private->dev_id
.devno
);
1510 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1513 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1517 spin_unlock_irq(cdev
->ccwlock
);
1522 static void recovery_work_func(struct work_struct
*unused
)
1526 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1528 spin_lock_irq(&recovery_lock
);
1529 if (!timer_pending(&recovery_timer
)) {
1530 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1532 mod_timer(&recovery_timer
, jiffies
+
1533 recovery_delay
[recovery_phase
] * HZ
);
1535 spin_unlock_irq(&recovery_lock
);
1537 CIO_MSG_EVENT(4, "recovery: end\n");
1540 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1542 static void recovery_func(unsigned long data
)
1545 * We can't do our recovery in softirq context and it's not
1546 * performance critical, so we schedule it.
1548 schedule_work(&recovery_work
);
1551 static void ccw_device_schedule_recovery(void)
1553 unsigned long flags
;
1555 CIO_MSG_EVENT(4, "recovery: schedule\n");
1556 spin_lock_irqsave(&recovery_lock
, flags
);
1557 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1559 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1561 spin_unlock_irqrestore(&recovery_lock
, flags
);
1564 static int purge_fn(struct device
*dev
, void *data
)
1566 struct ccw_device
*cdev
= to_ccwdev(dev
);
1567 struct ccw_device_private
*priv
= cdev
->private;
1570 spin_lock_irq(cdev
->ccwlock
);
1571 unreg
= is_blacklisted(priv
->dev_id
.ssid
, priv
->dev_id
.devno
) &&
1572 (priv
->state
== DEV_STATE_OFFLINE
);
1573 spin_unlock_irq(cdev
->ccwlock
);
1576 if (!get_device(&cdev
->dev
))
1578 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv
->dev_id
.ssid
,
1579 priv
->dev_id
.devno
);
1580 ccw_device_schedule_sch_unregister(cdev
);
1583 /* Abort loop in case of pending signal. */
1584 if (signal_pending(current
))
1591 * ccw_purge_blacklisted - purge unused, blacklisted devices
1593 * Unregister all ccw devices that are offline and on the blacklist.
1595 int ccw_purge_blacklisted(void)
1597 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1598 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1602 static void device_set_disconnected(struct ccw_device
*cdev
)
1606 ccw_device_set_timeout(cdev
, 0);
1607 cdev
->private->flags
.fake_irb
= 0;
1608 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1610 ccw_device_schedule_recovery();
1613 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1615 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1617 CIO_TRACE_EVENT(2, "notoper");
1618 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1619 ccw_device_set_timeout(cdev
, 0);
1620 cio_disable_subchannel(sch
);
1621 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1624 static int io_subchannel_sch_event(struct subchannel
*sch
, int slow
)
1626 int event
, ret
, disc
;
1627 unsigned long flags
;
1628 enum { NONE
, UNREGISTER
, UNREGISTER_PROBE
, REPROBE
, DISC
} action
;
1629 struct ccw_device
*cdev
;
1631 spin_lock_irqsave(sch
->lock
, flags
);
1632 cdev
= sch_get_cdev(sch
);
1633 disc
= device_is_disconnected(cdev
);
1635 /* Disconnected devices are evaluated directly only.*/
1636 spin_unlock_irqrestore(sch
->lock
, flags
);
1639 /* No interrupt after machine check - kill pending timers. */
1641 ccw_device_set_timeout(cdev
, 0);
1642 if (!disc
&& !slow
) {
1643 /* Non-disconnected devices are evaluated on the slow path. */
1644 spin_unlock_irqrestore(sch
->lock
, flags
);
1647 event
= io_subchannel_get_status(sch
);
1648 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1649 sch
->schid
.ssid
, sch
->schid
.sch_no
, event
,
1650 disc
? "disconnected" : "normal",
1651 slow
? "slow" : "fast");
1652 /* Analyze subchannel status. */
1657 /* Check if paths have become available. */
1663 /* Ask driver what to do with device. */
1664 if (io_subchannel_notify(sch
, event
))
1667 action
= UNREGISTER
;
1669 case CIO_REVALIDATE
:
1670 /* Device will be removed, so no notify necessary. */
1672 /* Reprobe because immediate unregister might block. */
1675 action
= UNREGISTER_PROBE
;
1679 /* Get device operational again. */
1683 /* Perform action. */
1687 case UNREGISTER_PROBE
:
1688 ccw_device_set_notoper(cdev
);
1689 /* Unregister device (will use subchannel lock). */
1690 spin_unlock_irqrestore(sch
->lock
, flags
);
1691 css_sch_device_unregister(sch
);
1692 spin_lock_irqsave(sch
->lock
, flags
);
1694 /* Reset intparm to zeroes. */
1695 sch
->config
.intparm
= 0;
1696 cio_commit_config(sch
);
1699 ccw_device_trigger_reprobe(cdev
);
1702 device_set_disconnected(cdev
);
1707 spin_unlock_irqrestore(sch
->lock
, flags
);
1708 /* Probe if necessary. */
1709 if (action
== UNREGISTER_PROBE
)
1710 ret
= css_probe_device(sch
->schid
);
1715 #ifdef CONFIG_CCW_CONSOLE
1716 static struct ccw_device console_cdev
;
1717 static char console_cdev_name
[10] = "0.x.xxxx";
1718 static struct ccw_device_private console_private
;
1719 static int console_cdev_in_use
;
1721 static DEFINE_SPINLOCK(ccw_console_lock
);
1723 spinlock_t
* cio_get_console_lock(void)
1725 return &ccw_console_lock
;
1728 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1729 struct subchannel
*sch
)
1733 /* Attach subchannel private data. */
1734 sch
->private = cio_get_console_priv();
1735 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1736 io_subchannel_init_fields(sch
);
1737 rc
= cio_commit_config(sch
);
1740 sch
->driver
= &io_subchannel_driver
;
1741 /* Initialize the ccw_device structure. */
1742 cdev
->dev
.parent
= &sch
->dev
;
1743 rc
= io_subchannel_recog(cdev
, sch
);
1747 /* Now wait for the async. recognition to come to an end. */
1748 spin_lock_irq(cdev
->ccwlock
);
1749 while (!dev_fsm_final_state(cdev
))
1752 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1754 ccw_device_online(cdev
);
1755 while (!dev_fsm_final_state(cdev
))
1757 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1761 spin_unlock_irq(cdev
->ccwlock
);
1766 ccw_device_probe_console(void)
1768 struct subchannel
*sch
;
1771 if (xchg(&console_cdev_in_use
, 1) != 0)
1772 return ERR_PTR(-EBUSY
);
1773 sch
= cio_probe_console();
1775 console_cdev_in_use
= 0;
1776 return (void *) sch
;
1778 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1779 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1780 console_cdev
.private = &console_private
;
1781 console_private
.cdev
= &console_cdev
;
1782 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1784 cio_release_console();
1785 console_cdev_in_use
= 0;
1786 return ERR_PTR(ret
);
1788 console_cdev
.online
= 1;
1789 return &console_cdev
;
1792 static int ccw_device_pm_restore(struct device
*dev
);
1794 int ccw_device_force_console(void)
1796 if (!console_cdev_in_use
)
1798 return ccw_device_pm_restore(&console_cdev
.dev
);
1800 EXPORT_SYMBOL_GPL(ccw_device_force_console
);
1802 const char *cio_get_console_cdev_name(struct subchannel
*sch
)
1804 snprintf(console_cdev_name
, 10, "0.%x.%04x",
1805 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1806 return (const char *)console_cdev_name
;
1811 * get ccw_device matching the busid, but only if owned by cdrv
1814 __ccwdev_check_busid(struct device
*dev
, void *id
)
1820 return (strcmp(bus_id
, dev_name(dev
)) == 0);
1825 * get_ccwdev_by_busid() - obtain device from a bus id
1826 * @cdrv: driver the device is owned by
1827 * @bus_id: bus id of the device to be searched
1829 * This function searches all devices owned by @cdrv for a device with a bus
1830 * id matching @bus_id.
1832 * If a match is found, its reference count of the found device is increased
1833 * and it is returned; else %NULL is returned.
1835 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1839 struct device_driver
*drv
;
1841 drv
= get_driver(&cdrv
->driver
);
1845 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1846 __ccwdev_check_busid
);
1849 return dev
? to_ccwdev(dev
) : NULL
;
1852 /************************** device driver handling ************************/
1854 /* This is the implementation of the ccw_driver class. The probe, remove
1855 * and release methods are initially very similar to the device_driver
1856 * implementations, with the difference that they have ccw_device
1859 * A ccw driver also contains the information that is needed for
1863 ccw_device_probe (struct device
*dev
)
1865 struct ccw_device
*cdev
= to_ccwdev(dev
);
1866 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1869 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1871 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1882 ccw_device_remove (struct device
*dev
)
1884 struct ccw_device
*cdev
= to_ccwdev(dev
);
1885 struct ccw_driver
*cdrv
= cdev
->drv
;
1892 spin_lock_irq(cdev
->ccwlock
);
1893 ret
= ccw_device_offline(cdev
);
1894 spin_unlock_irq(cdev
->ccwlock
);
1896 wait_event(cdev
->private->wait_q
,
1897 dev_fsm_final_state(cdev
));
1899 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1900 "device 0.%x.%04x\n",
1901 ret
, cdev
->private->dev_id
.ssid
,
1902 cdev
->private->dev_id
.devno
);
1903 /* Give up reference obtained in ccw_device_set_online(). */
1904 put_device(&cdev
->dev
);
1906 ccw_device_set_timeout(cdev
, 0);
1911 static void ccw_device_shutdown(struct device
*dev
)
1913 struct ccw_device
*cdev
;
1915 cdev
= to_ccwdev(dev
);
1916 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1917 cdev
->drv
->shutdown(cdev
);
1921 static int ccw_device_pm_prepare(struct device
*dev
)
1923 struct ccw_device
*cdev
= to_ccwdev(dev
);
1925 if (work_pending(&cdev
->private->kick_work
))
1927 /* Fail while device is being set online/offline. */
1928 if (atomic_read(&cdev
->private->onoff
))
1931 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->prepare
)
1932 return cdev
->drv
->prepare(cdev
);
1937 static void ccw_device_pm_complete(struct device
*dev
)
1939 struct ccw_device
*cdev
= to_ccwdev(dev
);
1941 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->complete
)
1942 cdev
->drv
->complete(cdev
);
1945 static int ccw_device_pm_freeze(struct device
*dev
)
1947 struct ccw_device
*cdev
= to_ccwdev(dev
);
1948 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1949 int ret
, cm_enabled
;
1951 /* Fail suspend while device is in transistional state. */
1952 if (!dev_fsm_final_state(cdev
))
1956 if (cdev
->drv
&& cdev
->drv
->freeze
) {
1957 ret
= cdev
->drv
->freeze(cdev
);
1962 spin_lock_irq(sch
->lock
);
1963 cm_enabled
= cdev
->private->cmb
!= NULL
;
1964 spin_unlock_irq(sch
->lock
);
1966 /* Don't have the css write on memory. */
1967 ret
= ccw_set_cmf(cdev
, 0);
1971 /* From here on, disallow device driver I/O. */
1972 spin_lock_irq(sch
->lock
);
1973 ret
= cio_disable_subchannel(sch
);
1974 spin_unlock_irq(sch
->lock
);
1979 static int ccw_device_pm_thaw(struct device
*dev
)
1981 struct ccw_device
*cdev
= to_ccwdev(dev
);
1982 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1983 int ret
, cm_enabled
;
1988 spin_lock_irq(sch
->lock
);
1989 /* Allow device driver I/O again. */
1990 ret
= cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1991 cm_enabled
= cdev
->private->cmb
!= NULL
;
1992 spin_unlock_irq(sch
->lock
);
1997 ret
= ccw_set_cmf(cdev
, 1);
2002 if (cdev
->drv
&& cdev
->drv
->thaw
)
2003 ret
= cdev
->drv
->thaw(cdev
);
2008 static void __ccw_device_pm_restore(struct ccw_device
*cdev
)
2010 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
2013 if (cio_is_console(sch
->schid
))
2016 * While we were sleeping, devices may have gone or become
2017 * available again. Kick re-detection.
2019 spin_lock_irq(sch
->lock
);
2020 cdev
->private->flags
.resuming
= 1;
2021 ret
= ccw_device_recognition(cdev
);
2022 spin_unlock_irq(sch
->lock
);
2024 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
2025 "%s (ret=%d)\n", dev_name(&cdev
->dev
), ret
);
2026 spin_lock_irq(sch
->lock
);
2027 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
2028 spin_unlock_irq(sch
->lock
);
2029 /* notify driver after the resume cb */
2032 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
) ||
2033 cdev
->private->state
== DEV_STATE_DISCONNECTED
);
2036 cdev
->private->flags
.resuming
= 0;
2039 static int resume_handle_boxed(struct ccw_device
*cdev
)
2041 cdev
->private->state
= DEV_STATE_BOXED
;
2042 if (ccw_device_notify(cdev
, CIO_BOXED
))
2044 ccw_device_schedule_sch_unregister(cdev
);
2048 static int resume_handle_disc(struct ccw_device
*cdev
)
2050 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
2051 if (ccw_device_notify(cdev
, CIO_GONE
))
2053 ccw_device_schedule_sch_unregister(cdev
);
2057 static int ccw_device_pm_restore(struct device
*dev
)
2059 struct ccw_device
*cdev
= to_ccwdev(dev
);
2060 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
2061 int ret
= 0, cm_enabled
;
2063 __ccw_device_pm_restore(cdev
);
2064 spin_lock_irq(sch
->lock
);
2065 if (cio_is_console(sch
->schid
)) {
2066 cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
2067 spin_unlock_irq(sch
->lock
);
2070 cdev
->private->flags
.donotify
= 0;
2071 /* check recognition results */
2072 switch (cdev
->private->state
) {
2073 case DEV_STATE_OFFLINE
:
2075 case DEV_STATE_BOXED
:
2076 ret
= resume_handle_boxed(cdev
);
2077 spin_unlock_irq(sch
->lock
);
2081 case DEV_STATE_DISCONNECTED
:
2082 goto out_disc_unlock
;
2084 goto out_unreg_unlock
;
2086 /* check if the device id has changed */
2087 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
2088 CIO_MSG_EVENT(0, "resume: sch %s: failed (devno changed from "
2089 "%04x to %04x)\n", dev_name(&sch
->dev
),
2090 cdev
->private->dev_id
.devno
,
2091 sch
->schib
.pmcw
.dev
);
2092 goto out_unreg_unlock
;
2094 /* check if the device type has changed */
2095 if (!ccw_device_test_sense_data(cdev
)) {
2096 ccw_device_update_sense_data(cdev
);
2097 PREPARE_WORK(&cdev
->private->kick_work
,
2098 ccw_device_do_unbind_bind
);
2099 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
2103 if (!cdev
->online
) {
2107 ret
= ccw_device_online(cdev
);
2109 goto out_disc_unlock
;
2111 cm_enabled
= cdev
->private->cmb
!= NULL
;
2112 spin_unlock_irq(sch
->lock
);
2114 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
2115 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
2116 spin_lock_irq(sch
->lock
);
2117 goto out_disc_unlock
;
2120 ret
= ccw_set_cmf(cdev
, 1);
2122 CIO_MSG_EVENT(2, "resume: cdev %s: cmf failed "
2123 "(rc=%d)\n", dev_name(&cdev
->dev
), ret
);
2129 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->restore
)
2130 ret
= cdev
->drv
->restore(cdev
);
2135 ret
= resume_handle_disc(cdev
);
2136 spin_unlock_irq(sch
->lock
);
2142 ccw_device_schedule_sch_unregister(cdev
);
2145 spin_unlock_irq(sch
->lock
);
2149 static struct dev_pm_ops ccw_pm_ops
= {
2150 .prepare
= ccw_device_pm_prepare
,
2151 .complete
= ccw_device_pm_complete
,
2152 .freeze
= ccw_device_pm_freeze
,
2153 .thaw
= ccw_device_pm_thaw
,
2154 .restore
= ccw_device_pm_restore
,
2157 struct bus_type ccw_bus_type
= {
2159 .match
= ccw_bus_match
,
2160 .uevent
= ccw_uevent
,
2161 .probe
= ccw_device_probe
,
2162 .remove
= ccw_device_remove
,
2163 .shutdown
= ccw_device_shutdown
,
2168 * ccw_driver_register() - register a ccw driver
2169 * @cdriver: driver to be registered
2171 * This function is mainly a wrapper around driver_register().
2173 * %0 on success and a negative error value on failure.
2175 int ccw_driver_register(struct ccw_driver
*cdriver
)
2177 struct device_driver
*drv
= &cdriver
->driver
;
2179 drv
->bus
= &ccw_bus_type
;
2180 drv
->name
= cdriver
->name
;
2181 drv
->owner
= cdriver
->owner
;
2183 return driver_register(drv
);
2187 * ccw_driver_unregister() - deregister a ccw driver
2188 * @cdriver: driver to be deregistered
2190 * This function is mainly a wrapper around driver_unregister().
2192 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
2194 driver_unregister(&cdriver
->driver
);
2197 /* Helper func for qdio. */
2198 struct subchannel_id
2199 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
2201 struct subchannel
*sch
;
2203 sch
= to_subchannel(cdev
->dev
.parent
);
2207 MODULE_LICENSE("GPL");
2208 EXPORT_SYMBOL(ccw_device_set_online
);
2209 EXPORT_SYMBOL(ccw_device_set_offline
);
2210 EXPORT_SYMBOL(ccw_driver_register
);
2211 EXPORT_SYMBOL(ccw_driver_unregister
);
2212 EXPORT_SYMBOL(get_ccwdev_by_busid
);
2213 EXPORT_SYMBOL(ccw_bus_type
);
2214 EXPORT_SYMBOL(ccw_device_work
);
2215 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);