2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20 #include <linux/timer.h>
22 #include <asm/ccwdev.h>
24 #include <asm/param.h> /* HZ */
28 #include "cio_debug.h"
34 static struct timer_list recovery_timer
;
35 static spinlock_t recovery_lock
;
36 static int recovery_phase
;
37 static const unsigned long recovery_delay
[] = { 3, 30, 300 };
39 /******************* bus type handling ***********************/
41 /* The Linux driver model distinguishes between a bus type and
42 * the bus itself. Of course we only have one channel
43 * subsystem driver and one channel system per machine, but
44 * we still use the abstraction. T.R. says it's a good idea. */
46 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
48 struct ccw_device
*cdev
= to_ccwdev(dev
);
49 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
50 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
55 found
= ccw_device_id_match(ids
, &cdev
->id
);
59 cdev
->id
.driver_info
= found
->driver_info
;
64 /* Store modalias string delimited by prefix/suffix string into buffer with
65 * specified size. Return length of resulting string (excluding trailing '\0')
66 * even if string doesn't fit buffer (snprintf semantics). */
67 static int snprint_alias(char *buf
, size_t size
,
68 struct ccw_device_id
*id
, const char *suffix
)
72 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
78 if (id
->dev_type
!= 0)
79 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
80 id
->dev_model
, suffix
);
82 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
87 /* Set up environment variables for ccw device uevent. Return 0 on success,
88 * non-zero otherwise. */
89 static int ccw_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
91 struct ccw_device
*cdev
= to_ccwdev(dev
);
92 struct ccw_device_id
*id
= &(cdev
->id
);
94 char modalias_buf
[30];
97 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
102 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
106 /* The next two can be zero, that's ok for us */
108 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
113 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
118 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
119 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
123 struct bus_type ccw_bus_type
;
125 static void io_subchannel_irq(struct subchannel
*);
126 static int io_subchannel_probe(struct subchannel
*);
127 static int io_subchannel_remove(struct subchannel
*);
128 static int io_subchannel_notify(struct subchannel
*, int);
129 static void io_subchannel_verify(struct subchannel
*);
130 static void io_subchannel_ioterm(struct subchannel
*);
131 static void io_subchannel_shutdown(struct subchannel
*);
133 static struct css_driver io_subchannel_driver
= {
134 .owner
= THIS_MODULE
,
135 .subchannel_type
= SUBCHANNEL_TYPE_IO
,
136 .name
= "io_subchannel",
137 .irq
= io_subchannel_irq
,
138 .notify
= io_subchannel_notify
,
139 .verify
= io_subchannel_verify
,
140 .termination
= io_subchannel_ioterm
,
141 .probe
= io_subchannel_probe
,
142 .remove
= io_subchannel_remove
,
143 .shutdown
= io_subchannel_shutdown
,
146 struct workqueue_struct
*ccw_device_work
;
147 struct workqueue_struct
*ccw_device_notify_work
;
148 wait_queue_head_t ccw_device_init_wq
;
149 atomic_t ccw_device_init_count
;
151 static void recovery_func(unsigned long data
);
154 init_ccw_bus_type (void)
158 init_waitqueue_head(&ccw_device_init_wq
);
159 atomic_set(&ccw_device_init_count
, 0);
160 setup_timer(&recovery_timer
, recovery_func
, 0);
162 ccw_device_work
= create_singlethread_workqueue("cio");
163 if (!ccw_device_work
)
164 return -ENOMEM
; /* FIXME: better errno ? */
165 ccw_device_notify_work
= create_singlethread_workqueue("cio_notify");
166 if (!ccw_device_notify_work
) {
167 ret
= -ENOMEM
; /* FIXME: better errno ? */
170 slow_path_wq
= create_singlethread_workqueue("kslowcrw");
172 ret
= -ENOMEM
; /* FIXME: better errno ? */
175 if ((ret
= bus_register (&ccw_bus_type
)))
178 ret
= css_driver_register(&io_subchannel_driver
);
182 wait_event(ccw_device_init_wq
,
183 atomic_read(&ccw_device_init_count
) == 0);
184 flush_workqueue(ccw_device_work
);
188 destroy_workqueue(ccw_device_work
);
189 if (ccw_device_notify_work
)
190 destroy_workqueue(ccw_device_notify_work
);
192 destroy_workqueue(slow_path_wq
);
197 cleanup_ccw_bus_type (void)
199 css_driver_unregister(&io_subchannel_driver
);
200 bus_unregister(&ccw_bus_type
);
201 destroy_workqueue(ccw_device_notify_work
);
202 destroy_workqueue(ccw_device_work
);
205 subsys_initcall(init_ccw_bus_type
);
206 module_exit(cleanup_ccw_bus_type
);
208 /************************ device handling **************************/
211 * A ccw_device has some interfaces in sysfs in addition to the
213 * The following entries are designed to export the information which
214 * resided in 2.4 in /proc/subchannels. Subchannel and device number
215 * are obvious, so they don't have an entry :)
216 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
219 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
221 struct subchannel
*sch
= to_subchannel(dev
);
222 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
227 for (chp
= 0; chp
< 8; chp
++) {
229 if (ssd
->path_mask
& mask
)
230 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
232 ret
+= sprintf(buf
+ ret
, "00 ");
234 ret
+= sprintf (buf
+ret
, "\n");
235 return min((ssize_t
)PAGE_SIZE
, ret
);
239 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
241 struct subchannel
*sch
= to_subchannel(dev
);
242 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
244 return sprintf (buf
, "%02x %02x %02x\n",
245 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
249 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
251 struct ccw_device
*cdev
= to_ccwdev(dev
);
252 struct ccw_device_id
*id
= &(cdev
->id
);
254 if (id
->dev_type
!= 0)
255 return sprintf(buf
, "%04x/%02x\n",
256 id
->dev_type
, id
->dev_model
);
258 return sprintf(buf
, "n/a\n");
262 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
264 struct ccw_device
*cdev
= to_ccwdev(dev
);
265 struct ccw_device_id
*id
= &(cdev
->id
);
267 return sprintf(buf
, "%04x/%02x\n",
268 id
->cu_type
, id
->cu_model
);
272 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
274 struct ccw_device
*cdev
= to_ccwdev(dev
);
275 struct ccw_device_id
*id
= &(cdev
->id
);
278 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
280 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
284 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
286 struct ccw_device
*cdev
= to_ccwdev(dev
);
288 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
291 int ccw_device_is_orphan(struct ccw_device
*cdev
)
293 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
296 static void ccw_device_unregister(struct ccw_device
*cdev
)
298 if (test_and_clear_bit(1, &cdev
->private->registered
))
299 device_del(&cdev
->dev
);
302 static void ccw_device_remove_orphan_cb(struct device
*dev
)
304 struct ccw_device
*cdev
= to_ccwdev(dev
);
306 ccw_device_unregister(cdev
);
307 put_device(&cdev
->dev
);
310 static void ccw_device_remove_sch_cb(struct device
*dev
)
312 struct subchannel
*sch
;
314 sch
= to_subchannel(dev
);
315 css_sch_device_unregister(sch
);
316 /* Reset intparm to zeroes. */
317 sch
->schib
.pmcw
.intparm
= 0;
319 put_device(&sch
->dev
);
323 ccw_device_remove_disconnected(struct ccw_device
*cdev
)
329 * Forced offline in disconnected state means
330 * 'throw away device'.
332 if (ccw_device_is_orphan(cdev
)) {
334 * Deregister ccw device.
335 * Unfortunately, we cannot do this directly from the
338 spin_lock_irqsave(cdev
->ccwlock
, flags
);
339 cdev
->private->state
= DEV_STATE_NOT_OPER
;
340 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
341 rc
= device_schedule_callback(&cdev
->dev
,
342 ccw_device_remove_orphan_cb
);
344 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
346 cdev
->private->dev_id
.ssid
,
347 cdev
->private->dev_id
.devno
);
350 /* Deregister subchannel, which will kill the ccw device. */
351 rc
= device_schedule_callback(cdev
->dev
.parent
,
352 ccw_device_remove_sch_cb
);
354 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
356 cdev
->private->dev_id
.ssid
,
357 cdev
->private->dev_id
.devno
);
361 * ccw_device_set_offline() - disable a ccw device for I/O
362 * @cdev: target ccw device
364 * This function calls the driver's set_offline() function for @cdev, if
365 * given, and then disables @cdev.
367 * %0 on success and a negative error value on failure.
369 * enabled, ccw device lock not held
371 int ccw_device_set_offline(struct ccw_device
*cdev
)
377 if (!cdev
->online
|| !cdev
->drv
)
380 if (cdev
->drv
->set_offline
) {
381 ret
= cdev
->drv
->set_offline(cdev
);
386 spin_lock_irq(cdev
->ccwlock
);
387 ret
= ccw_device_offline(cdev
);
388 if (ret
== -ENODEV
) {
389 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
) {
390 cdev
->private->state
= DEV_STATE_OFFLINE
;
391 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
393 spin_unlock_irq(cdev
->ccwlock
);
396 spin_unlock_irq(cdev
->ccwlock
);
398 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
400 CIO_MSG_EVENT(2, "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
)
430 spin_lock_irq(cdev
->ccwlock
);
431 ret
= ccw_device_online(cdev
);
432 spin_unlock_irq(cdev
->ccwlock
);
434 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
436 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
437 "device 0.%x.%04x\n",
438 ret
, cdev
->private->dev_id
.ssid
,
439 cdev
->private->dev_id
.devno
);
442 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
444 if (!cdev
->drv
->set_online
|| cdev
->drv
->set_online(cdev
) == 0) {
448 spin_lock_irq(cdev
->ccwlock
);
449 ret
= ccw_device_offline(cdev
);
450 spin_unlock_irq(cdev
->ccwlock
);
452 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
454 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
455 "device 0.%x.%04x\n",
456 ret
, cdev
->private->dev_id
.ssid
,
457 cdev
->private->dev_id
.devno
);
458 return (ret
== 0) ? -ENODEV
: ret
;
461 static void online_store_handle_offline(struct ccw_device
*cdev
)
463 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
)
464 ccw_device_remove_disconnected(cdev
);
465 else if (cdev
->drv
&& cdev
->drv
->set_offline
)
466 ccw_device_set_offline(cdev
);
469 static int online_store_recog_and_online(struct ccw_device
*cdev
)
473 /* Do device recognition, if needed. */
474 if (cdev
->id
.cu_type
== 0) {
475 ret
= ccw_device_recognition(cdev
);
477 CIO_MSG_EVENT(0, "Couldn't start recognition "
478 "for device 0.%x.%04x (ret=%d)\n",
479 cdev
->private->dev_id
.ssid
,
480 cdev
->private->dev_id
.devno
, ret
);
483 wait_event(cdev
->private->wait_q
,
484 cdev
->private->flags
.recog_done
);
486 if (cdev
->drv
&& cdev
->drv
->set_online
)
487 ccw_device_set_online(cdev
);
490 static void online_store_handle_online(struct ccw_device
*cdev
, int force
)
494 ret
= online_store_recog_and_online(cdev
);
497 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
498 ret
= ccw_device_stlck(cdev
);
501 "ccw_device_stlck returned %d!\n", ret
);
504 if (cdev
->id
.cu_type
== 0)
505 cdev
->private->state
= DEV_STATE_NOT_OPER
;
506 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
)) {
530 i
= simple_strtoul(buf
, &tmp
, 16);
535 online_store_handle_offline(cdev
);
538 online_store_handle_online(cdev
, force
);
544 module_put(cdev
->drv
->owner
);
545 atomic_set(&cdev
->private->onoff
, 0);
550 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
552 struct ccw_device
*cdev
= to_ccwdev(dev
);
553 struct subchannel
*sch
;
555 if (ccw_device_is_orphan(cdev
))
556 return sprintf(buf
, "no device\n");
557 switch (cdev
->private->state
) {
558 case DEV_STATE_BOXED
:
559 return sprintf(buf
, "boxed\n");
560 case DEV_STATE_DISCONNECTED
:
561 case DEV_STATE_DISCONNECTED_SENSE_ID
:
562 case DEV_STATE_NOT_OPER
:
563 sch
= to_subchannel(dev
->parent
);
565 return sprintf(buf
, "no path\n");
567 return sprintf(buf
, "no device\n");
569 /* All other states considered fine. */
570 return sprintf(buf
, "good\n");
574 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
575 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
576 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
577 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
578 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
579 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
580 extern struct device_attribute dev_attr_cmb_enable
;
581 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
583 static struct attribute
* subch_attrs
[] = {
584 &dev_attr_chpids
.attr
,
585 &dev_attr_pimpampom
.attr
,
589 static struct attribute_group subch_attr_group
= {
590 .attrs
= subch_attrs
,
593 struct attribute_group
*subch_attr_groups
[] = {
598 static struct attribute
* ccwdev_attrs
[] = {
599 &dev_attr_devtype
.attr
,
600 &dev_attr_cutype
.attr
,
601 &dev_attr_modalias
.attr
,
602 &dev_attr_online
.attr
,
603 &dev_attr_cmb_enable
.attr
,
604 &dev_attr_availability
.attr
,
608 static struct attribute_group ccwdev_attr_group
= {
609 .attrs
= ccwdev_attrs
,
612 static struct attribute_group
*ccwdev_attr_groups
[] = {
617 /* this is a simple abstraction for device_register that sets the
618 * correct bus type and adds the bus specific files */
619 static int ccw_device_register(struct ccw_device
*cdev
)
621 struct device
*dev
= &cdev
->dev
;
624 dev
->bus
= &ccw_bus_type
;
626 if ((ret
= device_add(dev
)))
629 set_bit(1, &cdev
->private->registered
);
634 struct ccw_dev_id dev_id
;
635 struct ccw_device
* sibling
;
639 match_devno(struct device
* dev
, void * data
)
641 struct match_data
* d
= data
;
642 struct ccw_device
* cdev
;
644 cdev
= to_ccwdev(dev
);
645 if ((cdev
->private->state
== DEV_STATE_DISCONNECTED
) &&
646 !ccw_device_is_orphan(cdev
) &&
647 ccw_dev_id_is_equal(&cdev
->private->dev_id
, &d
->dev_id
) &&
648 (cdev
!= d
->sibling
))
653 static struct ccw_device
* get_disc_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
,
654 struct ccw_device
*sibling
)
657 struct match_data data
;
659 data
.dev_id
= *dev_id
;
660 data
.sibling
= sibling
;
661 dev
= bus_find_device(&ccw_bus_type
, NULL
, &data
, match_devno
);
663 return dev
? to_ccwdev(dev
) : NULL
;
666 static int match_orphan(struct device
*dev
, void *data
)
668 struct ccw_dev_id
*dev_id
;
669 struct ccw_device
*cdev
;
672 cdev
= to_ccwdev(dev
);
673 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
676 static struct ccw_device
*
677 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem
*css
,
678 struct ccw_dev_id
*dev_id
)
682 dev
= device_find_child(&css
->pseudo_subchannel
->dev
, dev_id
,
685 return dev
? to_ccwdev(dev
) : NULL
;
689 ccw_device_add_changed(struct work_struct
*work
)
691 struct ccw_device_private
*priv
;
692 struct ccw_device
*cdev
;
694 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
696 if (device_add(&cdev
->dev
)) {
697 put_device(&cdev
->dev
);
700 set_bit(1, &cdev
->private->registered
);
703 void ccw_device_do_unreg_rereg(struct work_struct
*work
)
705 struct ccw_device_private
*priv
;
706 struct ccw_device
*cdev
;
707 struct subchannel
*sch
;
709 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
711 sch
= to_subchannel(cdev
->dev
.parent
);
713 ccw_device_unregister(cdev
);
714 PREPARE_WORK(&cdev
->private->kick_work
,
715 ccw_device_add_changed
);
716 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
720 ccw_device_release(struct device
*dev
)
722 struct ccw_device
*cdev
;
724 cdev
= to_ccwdev(dev
);
725 kfree(cdev
->private);
729 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
731 struct ccw_device
*cdev
;
733 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
735 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
736 GFP_KERNEL
| GFP_DMA
);
741 return ERR_PTR(-ENOMEM
);
744 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
745 struct ccw_device
*cdev
)
747 cdev
->private->cdev
= cdev
;
748 atomic_set(&cdev
->private->onoff
, 0);
749 cdev
->dev
.parent
= &sch
->dev
;
750 cdev
->dev
.release
= ccw_device_release
;
751 INIT_WORK(&cdev
->private->kick_work
, NULL
);
752 cdev
->dev
.groups
= ccwdev_attr_groups
;
753 /* Do first half of device_register. */
754 device_initialize(&cdev
->dev
);
755 if (!get_device(&sch
->dev
)) {
756 if (cdev
->dev
.release
)
757 cdev
->dev
.release(&cdev
->dev
);
763 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
765 struct ccw_device
*cdev
;
768 cdev
= io_subchannel_allocate_dev(sch
);
770 ret
= io_subchannel_initialize_dev(sch
, cdev
);
779 static int io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
781 static void sch_attach_device(struct subchannel
*sch
,
782 struct ccw_device
*cdev
)
784 css_update_ssd_info(sch
);
785 spin_lock_irq(sch
->lock
);
786 sch_set_cdev(sch
, cdev
);
787 cdev
->private->schid
= sch
->schid
;
788 cdev
->ccwlock
= sch
->lock
;
789 device_trigger_reprobe(sch
);
790 spin_unlock_irq(sch
->lock
);
793 static void sch_attach_disconnected_device(struct subchannel
*sch
,
794 struct ccw_device
*cdev
)
796 struct subchannel
*other_sch
;
799 other_sch
= to_subchannel(get_device(cdev
->dev
.parent
));
800 ret
= device_move(&cdev
->dev
, &sch
->dev
);
802 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
803 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
804 cdev
->private->dev_id
.devno
, ret
);
805 put_device(&other_sch
->dev
);
808 sch_set_cdev(other_sch
, NULL
);
809 /* No need to keep a subchannel without ccw device around. */
810 css_sch_device_unregister(other_sch
);
811 put_device(&other_sch
->dev
);
812 sch_attach_device(sch
, cdev
);
815 static void sch_attach_orphaned_device(struct subchannel
*sch
,
816 struct ccw_device
*cdev
)
820 /* Try to move the ccw device to its new subchannel. */
821 ret
= device_move(&cdev
->dev
, &sch
->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
);
829 sch_attach_device(sch
, cdev
);
832 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
834 struct ccw_device
*cdev
;
836 /* Need to allocate a new ccw device. */
837 cdev
= io_subchannel_create_ccwdev(sch
);
839 /* OK, we did everything we could... */
840 css_sch_device_unregister(sch
);
843 spin_lock_irq(sch
->lock
);
844 sch_set_cdev(sch
, cdev
);
845 spin_unlock_irq(sch
->lock
);
846 /* Start recognition for the new ccw device. */
847 if (io_subchannel_recog(cdev
, sch
)) {
848 spin_lock_irq(sch
->lock
);
849 sch_set_cdev(sch
, NULL
);
850 spin_unlock_irq(sch
->lock
);
851 if (cdev
->dev
.release
)
852 cdev
->dev
.release(&cdev
->dev
);
853 css_sch_device_unregister(sch
);
858 void ccw_device_move_to_orphanage(struct work_struct
*work
)
860 struct ccw_device_private
*priv
;
861 struct ccw_device
*cdev
;
862 struct ccw_device
*replacing_cdev
;
863 struct subchannel
*sch
;
865 struct channel_subsystem
*css
;
866 struct ccw_dev_id dev_id
;
868 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
870 sch
= to_subchannel(cdev
->dev
.parent
);
871 css
= to_css(sch
->dev
.parent
);
872 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
873 dev_id
.ssid
= sch
->schid
.ssid
;
876 * Move the orphaned ccw device to the orphanage so the replacing
877 * ccw device can take its place on the subchannel.
879 ret
= device_move(&cdev
->dev
, &css
->pseudo_subchannel
->dev
);
881 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
882 "(ret=%d)!\n", cdev
->private->dev_id
.ssid
,
883 cdev
->private->dev_id
.devno
, ret
);
886 cdev
->ccwlock
= css
->pseudo_subchannel
->lock
;
888 * Search for the replacing ccw device
889 * - among the disconnected devices
892 replacing_cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, cdev
);
893 if (replacing_cdev
) {
894 sch_attach_disconnected_device(sch
, replacing_cdev
);
897 replacing_cdev
= get_orphaned_ccwdev_by_dev_id(css
, &dev_id
);
898 if (replacing_cdev
) {
899 sch_attach_orphaned_device(sch
, replacing_cdev
);
902 sch_create_and_recog_new_device(sch
);
906 * Register recognized device.
909 io_subchannel_register(struct work_struct
*work
)
911 struct ccw_device_private
*priv
;
912 struct ccw_device
*cdev
;
913 struct subchannel
*sch
;
917 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
919 sch
= to_subchannel(cdev
->dev
.parent
);
920 css_update_ssd_info(sch
);
922 * io_subchannel_register() will also be called after device
923 * recognition has been done for a boxed device (which will already
924 * be registered). We need to reprobe since we may now have sense id
927 if (klist_node_attached(&cdev
->dev
.knode_parent
)) {
929 ret
= device_reprobe(&cdev
->dev
);
931 /* We can't do much here. */
932 CIO_MSG_EVENT(2, "device_reprobe() returned"
933 " %d for 0.%x.%04x\n", ret
,
934 cdev
->private->dev_id
.ssid
,
935 cdev
->private->dev_id
.devno
);
940 * Now we know this subchannel will stay, we can throw
941 * our delayed uevent.
943 sch
->dev
.uevent_suppress
= 0;
944 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
945 /* make it known to the system */
946 ret
= ccw_device_register(cdev
);
948 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
949 cdev
->private->dev_id
.ssid
,
950 cdev
->private->dev_id
.devno
, ret
);
951 put_device(&cdev
->dev
);
952 spin_lock_irqsave(sch
->lock
, flags
);
953 sch_set_cdev(sch
, NULL
);
954 spin_unlock_irqrestore(sch
->lock
, flags
);
955 kfree (cdev
->private);
957 put_device(&sch
->dev
);
958 if (atomic_dec_and_test(&ccw_device_init_count
))
959 wake_up(&ccw_device_init_wq
);
962 put_device(&cdev
->dev
);
964 cdev
->private->flags
.recog_done
= 1;
965 put_device(&sch
->dev
);
966 wake_up(&cdev
->private->wait_q
);
967 if (atomic_dec_and_test(&ccw_device_init_count
))
968 wake_up(&ccw_device_init_wq
);
971 static void ccw_device_call_sch_unregister(struct work_struct
*work
)
973 struct ccw_device_private
*priv
;
974 struct ccw_device
*cdev
;
975 struct subchannel
*sch
;
977 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
979 sch
= to_subchannel(cdev
->dev
.parent
);
980 css_sch_device_unregister(sch
);
981 /* Reset intparm to zeroes. */
982 sch
->schib
.pmcw
.intparm
= 0;
984 put_device(&cdev
->dev
);
985 put_device(&sch
->dev
);
989 * subchannel recognition done. Called from the state machine.
992 io_subchannel_recog_done(struct ccw_device
*cdev
)
994 struct subchannel
*sch
;
996 if (css_init_done
== 0) {
997 cdev
->private->flags
.recog_done
= 1;
1000 switch (cdev
->private->state
) {
1001 case DEV_STATE_NOT_OPER
:
1002 cdev
->private->flags
.recog_done
= 1;
1003 /* Remove device found not operational. */
1004 if (!get_device(&cdev
->dev
))
1006 sch
= to_subchannel(cdev
->dev
.parent
);
1007 PREPARE_WORK(&cdev
->private->kick_work
,
1008 ccw_device_call_sch_unregister
);
1009 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1010 if (atomic_dec_and_test(&ccw_device_init_count
))
1011 wake_up(&ccw_device_init_wq
);
1013 case DEV_STATE_BOXED
:
1014 /* Device did not respond in time. */
1015 case DEV_STATE_OFFLINE
:
1017 * We can't register the device in interrupt context so
1018 * we schedule a work item.
1020 if (!get_device(&cdev
->dev
))
1022 PREPARE_WORK(&cdev
->private->kick_work
,
1023 io_subchannel_register
);
1024 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1030 io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
1033 struct ccw_device_private
*priv
;
1035 sch_set_cdev(sch
, cdev
);
1036 sch
->driver
= &io_subchannel_driver
;
1037 cdev
->ccwlock
= sch
->lock
;
1039 /* Init private data. */
1040 priv
= cdev
->private;
1041 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1042 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
1043 priv
->schid
= sch
->schid
;
1044 priv
->state
= DEV_STATE_NOT_OPER
;
1045 INIT_LIST_HEAD(&priv
->cmb_list
);
1046 init_waitqueue_head(&priv
->wait_q
);
1047 init_timer(&priv
->timer
);
1049 /* Set an initial name for the device. */
1050 snprintf (cdev
->dev
.bus_id
, BUS_ID_SIZE
, "0.%x.%04x",
1051 sch
->schid
.ssid
, sch
->schib
.pmcw
.dev
);
1053 /* Increase counter of devices currently in recognition. */
1054 atomic_inc(&ccw_device_init_count
);
1056 /* Start async. device sensing. */
1057 spin_lock_irq(sch
->lock
);
1058 rc
= ccw_device_recognition(cdev
);
1059 spin_unlock_irq(sch
->lock
);
1061 if (atomic_dec_and_test(&ccw_device_init_count
))
1062 wake_up(&ccw_device_init_wq
);
1067 static void ccw_device_move_to_sch(struct work_struct
*work
)
1069 struct ccw_device_private
*priv
;
1071 struct subchannel
*sch
;
1072 struct ccw_device
*cdev
;
1073 struct subchannel
*former_parent
;
1075 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
1078 former_parent
= ccw_device_is_orphan(cdev
) ?
1079 NULL
: to_subchannel(get_device(cdev
->dev
.parent
));
1080 mutex_lock(&sch
->reg_mutex
);
1081 /* Try to move the ccw device to its new subchannel. */
1082 rc
= device_move(&cdev
->dev
, &sch
->dev
);
1083 mutex_unlock(&sch
->reg_mutex
);
1085 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1086 "0.%x.%04x failed (ret=%d)!\n",
1087 cdev
->private->dev_id
.ssid
,
1088 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
1089 sch
->schid
.sch_no
, rc
);
1090 css_sch_device_unregister(sch
);
1093 if (former_parent
) {
1094 spin_lock_irq(former_parent
->lock
);
1095 sch_set_cdev(former_parent
, NULL
);
1096 spin_unlock_irq(former_parent
->lock
);
1097 css_sch_device_unregister(former_parent
);
1098 /* Reset intparm to zeroes. */
1099 former_parent
->schib
.pmcw
.intparm
= 0;
1100 cio_modify(former_parent
);
1102 sch_attach_device(sch
, cdev
);
1105 put_device(&former_parent
->dev
);
1106 put_device(&cdev
->dev
);
1109 static void io_subchannel_irq(struct subchannel
*sch
)
1111 struct ccw_device
*cdev
;
1113 cdev
= sch_get_cdev(sch
);
1115 CIO_TRACE_EVENT(3, "IRQ");
1116 CIO_TRACE_EVENT(3, sch
->dev
.bus_id
);
1118 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1122 io_subchannel_probe (struct subchannel
*sch
)
1124 struct ccw_device
*cdev
;
1126 unsigned long flags
;
1127 struct ccw_dev_id dev_id
;
1129 cdev
= sch_get_cdev(sch
);
1132 * This subchannel already has an associated ccw_device.
1133 * Register it and exit. This happens for all early
1134 * device, e.g. the console.
1136 cdev
->dev
.groups
= ccwdev_attr_groups
;
1137 device_initialize(&cdev
->dev
);
1138 ccw_device_register(cdev
);
1140 * Check if the device is already online. If it is
1141 * the reference count needs to be corrected
1142 * (see ccw_device_online and css_init_done for the
1145 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1146 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1147 cdev
->private->state
!= DEV_STATE_BOXED
)
1148 get_device(&cdev
->dev
);
1152 * First check if a fitting device may be found amongst the
1153 * disconnected devices or in the orphanage.
1155 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1156 dev_id
.ssid
= sch
->schid
.ssid
;
1157 /* Allocate I/O subchannel private data. */
1158 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1159 GFP_KERNEL
| GFP_DMA
);
1162 cdev
= get_disc_ccwdev_by_dev_id(&dev_id
, NULL
);
1164 cdev
= get_orphaned_ccwdev_by_dev_id(to_css(sch
->dev
.parent
),
1168 * Schedule moving the device until when we have a registered
1169 * subchannel to move to and succeed the probe. We can
1170 * unregister later again, when the probe is through.
1172 cdev
->private->sch
= sch
;
1173 PREPARE_WORK(&cdev
->private->kick_work
,
1174 ccw_device_move_to_sch
);
1175 queue_work(slow_path_wq
, &cdev
->private->kick_work
);
1178 cdev
= io_subchannel_create_ccwdev(sch
);
1180 kfree(sch
->private);
1181 return PTR_ERR(cdev
);
1183 rc
= io_subchannel_recog(cdev
, sch
);
1185 spin_lock_irqsave(sch
->lock
, flags
);
1186 sch_set_cdev(sch
, NULL
);
1187 spin_unlock_irqrestore(sch
->lock
, flags
);
1188 if (cdev
->dev
.release
)
1189 cdev
->dev
.release(&cdev
->dev
);
1190 kfree(sch
->private);
1197 io_subchannel_remove (struct subchannel
*sch
)
1199 struct ccw_device
*cdev
;
1200 unsigned long flags
;
1202 cdev
= sch_get_cdev(sch
);
1205 /* Set ccw device to not operational and drop reference. */
1206 spin_lock_irqsave(cdev
->ccwlock
, flags
);
1207 sch_set_cdev(sch
, NULL
);
1208 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1209 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
1210 ccw_device_unregister(cdev
);
1211 put_device(&cdev
->dev
);
1212 kfree(sch
->private);
1216 static int io_subchannel_notify(struct subchannel
*sch
, int event
)
1218 struct ccw_device
*cdev
;
1220 cdev
= sch_get_cdev(sch
);
1227 return cdev
->drv
->notify
? cdev
->drv
->notify(cdev
, event
) : 0;
1230 static void io_subchannel_verify(struct subchannel
*sch
)
1232 struct ccw_device
*cdev
;
1234 cdev
= sch_get_cdev(sch
);
1236 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1239 static void io_subchannel_ioterm(struct subchannel
*sch
)
1241 struct ccw_device
*cdev
;
1243 cdev
= sch_get_cdev(sch
);
1246 /* Internal I/O will be retried by the interrupt handler. */
1247 if (cdev
->private->flags
.intretry
)
1249 cdev
->private->state
= DEV_STATE_CLEAR_VERIFY
;
1251 cdev
->handler(cdev
, cdev
->private->intparm
,
1256 io_subchannel_shutdown(struct subchannel
*sch
)
1258 struct ccw_device
*cdev
;
1261 cdev
= sch_get_cdev(sch
);
1263 if (cio_is_console(sch
->schid
))
1265 if (!sch
->schib
.pmcw
.ena
)
1266 /* Nothing to do. */
1268 ret
= cio_disable_subchannel(sch
);
1270 /* Subchannel is disabled, we're done. */
1272 cdev
->private->state
= DEV_STATE_QUIESCE
;
1274 cdev
->handler(cdev
, cdev
->private->intparm
,
1276 ret
= ccw_device_cancel_halt_clear(cdev
);
1277 if (ret
== -EBUSY
) {
1278 ccw_device_set_timeout(cdev
, HZ
/10);
1279 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1281 cio_disable_subchannel(sch
);
1284 #ifdef CONFIG_CCW_CONSOLE
1285 static struct ccw_device console_cdev
;
1286 static struct ccw_device_private console_private
;
1287 static int console_cdev_in_use
;
1289 static DEFINE_SPINLOCK(ccw_console_lock
);
1291 spinlock_t
* cio_get_console_lock(void)
1293 return &ccw_console_lock
;
1297 ccw_device_console_enable (struct ccw_device
*cdev
, struct subchannel
*sch
)
1301 /* Attach subchannel private data. */
1302 sch
->private = cio_get_console_priv();
1303 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1304 /* Initialize the ccw_device structure. */
1305 cdev
->dev
.parent
= &sch
->dev
;
1306 rc
= io_subchannel_recog(cdev
, sch
);
1310 /* Now wait for the async. recognition to come to an end. */
1311 spin_lock_irq(cdev
->ccwlock
);
1312 while (!dev_fsm_final_state(cdev
))
1315 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1317 ccw_device_online(cdev
);
1318 while (!dev_fsm_final_state(cdev
))
1320 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1324 spin_unlock_irq(cdev
->ccwlock
);
1329 ccw_device_probe_console(void)
1331 struct subchannel
*sch
;
1334 if (xchg(&console_cdev_in_use
, 1) != 0)
1335 return ERR_PTR(-EBUSY
);
1336 sch
= cio_probe_console();
1338 console_cdev_in_use
= 0;
1339 return (void *) sch
;
1341 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1342 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1343 console_cdev
.private = &console_private
;
1344 console_private
.cdev
= &console_cdev
;
1345 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1347 cio_release_console();
1348 console_cdev_in_use
= 0;
1349 return ERR_PTR(ret
);
1351 console_cdev
.online
= 1;
1352 return &console_cdev
;
1357 * get ccw_device matching the busid, but only if owned by cdrv
1360 __ccwdev_check_busid(struct device
*dev
, void *id
)
1366 return (strncmp(bus_id
, dev
->bus_id
, BUS_ID_SIZE
) == 0);
1371 * get_ccwdev_by_busid() - obtain device from a bus id
1372 * @cdrv: driver the device is owned by
1373 * @bus_id: bus id of the device to be searched
1375 * This function searches all devices owned by @cdrv for a device with a bus
1376 * id matching @bus_id.
1378 * If a match is found, its reference count of the found device is increased
1379 * and it is returned; else %NULL is returned.
1381 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1385 struct device_driver
*drv
;
1387 drv
= get_driver(&cdrv
->driver
);
1391 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1392 __ccwdev_check_busid
);
1395 return dev
? to_ccwdev(dev
) : NULL
;
1398 /************************** device driver handling ************************/
1400 /* This is the implementation of the ccw_driver class. The probe, remove
1401 * and release methods are initially very similar to the device_driver
1402 * implementations, with the difference that they have ccw_device
1405 * A ccw driver also contains the information that is needed for
1409 ccw_device_probe (struct device
*dev
)
1411 struct ccw_device
*cdev
= to_ccwdev(dev
);
1412 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1415 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1417 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1428 ccw_device_remove (struct device
*dev
)
1430 struct ccw_device
*cdev
= to_ccwdev(dev
);
1431 struct ccw_driver
*cdrv
= cdev
->drv
;
1438 spin_lock_irq(cdev
->ccwlock
);
1439 ret
= ccw_device_offline(cdev
);
1440 spin_unlock_irq(cdev
->ccwlock
);
1442 wait_event(cdev
->private->wait_q
,
1443 dev_fsm_final_state(cdev
));
1445 //FIXME: we can't fail!
1446 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1447 "device 0.%x.%04x\n",
1448 ret
, cdev
->private->dev_id
.ssid
,
1449 cdev
->private->dev_id
.devno
);
1451 ccw_device_set_timeout(cdev
, 0);
1456 static void ccw_device_shutdown(struct device
*dev
)
1458 struct ccw_device
*cdev
;
1460 cdev
= to_ccwdev(dev
);
1461 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1462 cdev
->drv
->shutdown(cdev
);
1466 struct bus_type ccw_bus_type
= {
1468 .match
= ccw_bus_match
,
1469 .uevent
= ccw_uevent
,
1470 .probe
= ccw_device_probe
,
1471 .remove
= ccw_device_remove
,
1472 .shutdown
= ccw_device_shutdown
,
1476 * ccw_driver_register() - register a ccw driver
1477 * @cdriver: driver to be registered
1479 * This function is mainly a wrapper around driver_register().
1481 * %0 on success and a negative error value on failure.
1483 int ccw_driver_register(struct ccw_driver
*cdriver
)
1485 struct device_driver
*drv
= &cdriver
->driver
;
1487 drv
->bus
= &ccw_bus_type
;
1488 drv
->name
= cdriver
->name
;
1489 drv
->owner
= cdriver
->owner
;
1491 return driver_register(drv
);
1495 * ccw_driver_unregister() - deregister a ccw driver
1496 * @cdriver: driver to be deregistered
1498 * This function is mainly a wrapper around driver_unregister().
1500 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1502 driver_unregister(&cdriver
->driver
);
1505 /* Helper func for qdio. */
1506 struct subchannel_id
1507 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1509 struct subchannel
*sch
;
1511 sch
= to_subchannel(cdev
->dev
.parent
);
1515 static int recovery_check(struct device
*dev
, void *data
)
1517 struct ccw_device
*cdev
= to_ccwdev(dev
);
1520 spin_lock_irq(cdev
->ccwlock
);
1521 switch (cdev
->private->state
) {
1522 case DEV_STATE_DISCONNECTED
:
1523 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1524 cdev
->private->dev_id
.ssid
,
1525 cdev
->private->dev_id
.devno
);
1526 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1529 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1533 spin_unlock_irq(cdev
->ccwlock
);
1538 static void recovery_func(unsigned long data
)
1542 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1544 spin_lock_irq(&recovery_lock
);
1545 if (!timer_pending(&recovery_timer
)) {
1546 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1548 mod_timer(&recovery_timer
, jiffies
+
1549 recovery_delay
[recovery_phase
] * HZ
);
1551 spin_unlock_irq(&recovery_lock
);
1553 CIO_MSG_EVENT(2, "recovery: end\n");
1556 void ccw_device_schedule_recovery(void)
1558 unsigned long flags
;
1560 CIO_MSG_EVENT(2, "recovery: schedule\n");
1561 spin_lock_irqsave(&recovery_lock
, flags
);
1562 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1564 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1566 spin_unlock_irqrestore(&recovery_lock
, flags
);
1569 MODULE_LICENSE("GPL");
1570 EXPORT_SYMBOL(ccw_device_set_online
);
1571 EXPORT_SYMBOL(ccw_device_set_offline
);
1572 EXPORT_SYMBOL(ccw_driver_register
);
1573 EXPORT_SYMBOL(ccw_driver_unregister
);
1574 EXPORT_SYMBOL(get_ccwdev_by_busid
);
1575 EXPORT_SYMBOL(ccw_bus_type
);
1576 EXPORT_SYMBOL(ccw_device_work
);
1577 EXPORT_SYMBOL(ccw_device_notify_work
);
1578 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);