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)
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/device.h>
22 #include <linux/workqueue.h>
23 #include <linux/timer.h>
25 #include <asm/ccwdev.h>
27 #include <asm/param.h> /* HZ */
33 #include "cio_debug.h"
38 #include "blacklist.h"
41 static struct timer_list recovery_timer
;
42 static DEFINE_SPINLOCK(recovery_lock
);
43 static int recovery_phase
;
44 static const unsigned long recovery_delay
[] = { 3, 30, 300 };
46 /******************* bus type handling ***********************/
48 /* The Linux driver model distinguishes between a bus type and
49 * the bus itself. Of course we only have one channel
50 * subsystem driver and one channel system per machine, but
51 * we still use the abstraction. T.R. says it's a good idea. */
53 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
55 struct ccw_device
*cdev
= to_ccwdev(dev
);
56 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
57 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
62 found
= ccw_device_id_match(ids
, &cdev
->id
);
66 cdev
->id
.driver_info
= found
->driver_info
;
71 /* Store modalias string delimited by prefix/suffix string into buffer with
72 * specified size. Return length of resulting string (excluding trailing '\0')
73 * even if string doesn't fit buffer (snprintf semantics). */
74 static int snprint_alias(char *buf
, size_t size
,
75 struct ccw_device_id
*id
, const char *suffix
)
79 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
85 if (id
->dev_type
!= 0)
86 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
87 id
->dev_model
, suffix
);
89 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
94 /* Set up environment variables for ccw device uevent. Return 0 on success,
95 * non-zero otherwise. */
96 static int ccw_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
98 struct ccw_device
*cdev
= to_ccwdev(dev
);
99 struct ccw_device_id
*id
= &(cdev
->id
);
101 char modalias_buf
[30];
104 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
109 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
113 /* The next two can be zero, that's ok for us */
115 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
120 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
125 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
126 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
130 struct bus_type ccw_bus_type
;
132 static void io_subchannel_irq(struct subchannel
*);
133 static int io_subchannel_probe(struct subchannel
*);
134 static int io_subchannel_remove(struct subchannel
*);
135 static void io_subchannel_shutdown(struct subchannel
*);
136 static int io_subchannel_sch_event(struct subchannel
*, int);
137 static int io_subchannel_chp_event(struct subchannel
*, struct chp_link
*,
139 static void recovery_func(unsigned long data
);
140 wait_queue_head_t ccw_device_init_wq
;
141 atomic_t ccw_device_init_count
;
143 static struct css_device_id io_subchannel_ids
[] = {
144 { .match_flags
= 0x1, .type
= SUBCHANNEL_TYPE_IO
, },
145 { /* end of list */ },
147 MODULE_DEVICE_TABLE(css
, io_subchannel_ids
);
149 static int io_subchannel_prepare(struct subchannel
*sch
)
151 struct ccw_device
*cdev
;
153 * Don't allow suspend while a ccw device registration
154 * is still outstanding.
156 cdev
= sch_get_cdev(sch
);
157 if (cdev
&& !device_is_registered(&cdev
->dev
))
162 static int io_subchannel_settle(void)
166 ret
= wait_event_interruptible(ccw_device_init_wq
,
167 atomic_read(&ccw_device_init_count
) == 0);
170 flush_workqueue(cio_work_q
);
174 static struct css_driver io_subchannel_driver
= {
175 .owner
= THIS_MODULE
,
176 .subchannel_type
= io_subchannel_ids
,
177 .name
= "io_subchannel",
178 .irq
= io_subchannel_irq
,
179 .sch_event
= io_subchannel_sch_event
,
180 .chp_event
= io_subchannel_chp_event
,
181 .probe
= io_subchannel_probe
,
182 .remove
= io_subchannel_remove
,
183 .shutdown
= io_subchannel_shutdown
,
184 .prepare
= io_subchannel_prepare
,
185 .settle
= io_subchannel_settle
,
188 int __init
io_subchannel_init(void)
192 init_waitqueue_head(&ccw_device_init_wq
);
193 atomic_set(&ccw_device_init_count
, 0);
194 setup_timer(&recovery_timer
, recovery_func
, 0);
196 ret
= bus_register(&ccw_bus_type
);
199 ret
= css_driver_register(&io_subchannel_driver
);
201 bus_unregister(&ccw_bus_type
);
207 /************************ device handling **************************/
210 * A ccw_device has some interfaces in sysfs in addition to the
212 * The following entries are designed to export the information which
213 * resided in 2.4 in /proc/subchannels. Subchannel and device number
214 * are obvious, so they don't have an entry :)
215 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
218 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
220 struct subchannel
*sch
= to_subchannel(dev
);
221 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
226 for (chp
= 0; chp
< 8; chp
++) {
228 if (ssd
->path_mask
& mask
)
229 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
231 ret
+= sprintf(buf
+ ret
, "00 ");
233 ret
+= sprintf (buf
+ret
, "\n");
234 return min((ssize_t
)PAGE_SIZE
, ret
);
238 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
240 struct subchannel
*sch
= to_subchannel(dev
);
241 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
243 return sprintf (buf
, "%02x %02x %02x\n",
244 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
248 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
250 struct ccw_device
*cdev
= to_ccwdev(dev
);
251 struct ccw_device_id
*id
= &(cdev
->id
);
253 if (id
->dev_type
!= 0)
254 return sprintf(buf
, "%04x/%02x\n",
255 id
->dev_type
, id
->dev_model
);
257 return sprintf(buf
, "n/a\n");
261 cutype_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 return sprintf(buf
, "%04x/%02x\n",
267 id
->cu_type
, id
->cu_model
);
271 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
273 struct ccw_device
*cdev
= to_ccwdev(dev
);
274 struct ccw_device_id
*id
= &(cdev
->id
);
277 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
279 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
283 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
285 struct ccw_device
*cdev
= to_ccwdev(dev
);
287 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
290 int ccw_device_is_orphan(struct ccw_device
*cdev
)
292 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
295 static void ccw_device_unregister(struct ccw_device
*cdev
)
297 if (device_is_registered(&cdev
->dev
)) {
298 /* Undo device_add(). */
299 device_del(&cdev
->dev
);
301 if (cdev
->private->flags
.initialized
) {
302 cdev
->private->flags
.initialized
= 0;
303 /* Release reference from device_initialize(). */
304 put_device(&cdev
->dev
);
308 static void io_subchannel_quiesce(struct subchannel
*);
311 * ccw_device_set_offline() - disable a ccw device for I/O
312 * @cdev: target ccw device
314 * This function calls the driver's set_offline() function for @cdev, if
315 * given, and then disables @cdev.
317 * %0 on success and a negative error value on failure.
319 * enabled, ccw device lock not held
321 int ccw_device_set_offline(struct ccw_device
*cdev
)
323 struct subchannel
*sch
;
328 if (!cdev
->online
|| !cdev
->drv
)
331 if (cdev
->drv
->set_offline
) {
332 ret
= cdev
->drv
->set_offline(cdev
);
337 spin_lock_irq(cdev
->ccwlock
);
338 sch
= to_subchannel(cdev
->dev
.parent
);
339 /* Wait until a final state or DISCONNECTED is reached */
340 while (!dev_fsm_final_state(cdev
) &&
341 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
342 spin_unlock_irq(cdev
->ccwlock
);
343 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
344 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
345 spin_lock_irq(cdev
->ccwlock
);
348 ret
= ccw_device_offline(cdev
);
351 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
352 "0.%x.%04x\n", ret
, cdev
->private->dev_id
.ssid
,
353 cdev
->private->dev_id
.devno
);
356 state
= cdev
->private->state
;
357 spin_unlock_irq(cdev
->ccwlock
);
358 io_subchannel_quiesce(sch
);
359 spin_lock_irq(cdev
->ccwlock
);
360 cdev
->private->state
= state
;
361 } while (ret
== -EBUSY
);
362 spin_unlock_irq(cdev
->ccwlock
);
363 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
364 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
365 /* Inform the user if set offline failed. */
366 if (cdev
->private->state
== DEV_STATE_BOXED
) {
367 pr_warning("%s: The device entered boxed state while "
368 "being set offline\n", dev_name(&cdev
->dev
));
369 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
370 pr_warning("%s: The device stopped operating while "
371 "being set offline\n", dev_name(&cdev
->dev
));
373 /* Give up reference from ccw_device_set_online(). */
374 put_device(&cdev
->dev
);
378 cdev
->private->state
= DEV_STATE_OFFLINE
;
379 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
380 spin_unlock_irq(cdev
->ccwlock
);
381 /* Give up reference from ccw_device_set_online(). */
382 put_device(&cdev
->dev
);
387 * ccw_device_set_online() - enable a ccw device for I/O
388 * @cdev: target ccw device
390 * This function first enables @cdev and then calls the driver's set_online()
391 * function for @cdev, if given. If set_online() returns an error, @cdev is
394 * %0 on success and a negative error value on failure.
396 * enabled, ccw device lock not held
398 int ccw_device_set_online(struct ccw_device
*cdev
)
405 if (cdev
->online
|| !cdev
->drv
)
407 /* Hold on to an extra reference while device is online. */
408 if (!get_device(&cdev
->dev
))
411 spin_lock_irq(cdev
->ccwlock
);
412 ret
= ccw_device_online(cdev
);
413 spin_unlock_irq(cdev
->ccwlock
);
415 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
417 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
418 "device 0.%x.%04x\n",
419 ret
, cdev
->private->dev_id
.ssid
,
420 cdev
->private->dev_id
.devno
);
421 /* Give up online reference since onlining failed. */
422 put_device(&cdev
->dev
);
425 spin_lock_irq(cdev
->ccwlock
);
426 /* Check if online processing was successful */
427 if ((cdev
->private->state
!= DEV_STATE_ONLINE
) &&
428 (cdev
->private->state
!= DEV_STATE_W4SENSE
)) {
429 spin_unlock_irq(cdev
->ccwlock
);
430 /* Inform the user that set online failed. */
431 if (cdev
->private->state
== DEV_STATE_BOXED
) {
432 pr_warning("%s: Setting the device online failed "
433 "because it is boxed\n",
434 dev_name(&cdev
->dev
));
435 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
436 pr_warning("%s: Setting the device online failed "
437 "because it is not operational\n",
438 dev_name(&cdev
->dev
));
440 /* Give up online reference since onlining failed. */
441 put_device(&cdev
->dev
);
444 spin_unlock_irq(cdev
->ccwlock
);
445 if (cdev
->drv
->set_online
)
446 ret
= cdev
->drv
->set_online(cdev
);
453 spin_lock_irq(cdev
->ccwlock
);
454 /* Wait until a final state or DISCONNECTED is reached */
455 while (!dev_fsm_final_state(cdev
) &&
456 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
457 spin_unlock_irq(cdev
->ccwlock
);
458 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
459 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
460 spin_lock_irq(cdev
->ccwlock
);
462 ret2
= ccw_device_offline(cdev
);
465 spin_unlock_irq(cdev
->ccwlock
);
466 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
467 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
468 /* Give up online reference since onlining failed. */
469 put_device(&cdev
->dev
);
473 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
474 "device 0.%x.%04x\n",
475 ret2
, cdev
->private->dev_id
.ssid
,
476 cdev
->private->dev_id
.devno
);
477 cdev
->private->state
= DEV_STATE_OFFLINE
;
478 spin_unlock_irq(cdev
->ccwlock
);
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev
->dev
);
484 static int online_store_handle_offline(struct ccw_device
*cdev
)
486 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
) {
487 spin_lock_irq(cdev
->ccwlock
);
488 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG_EVAL
);
489 spin_unlock_irq(cdev
->ccwlock
);
492 if (cdev
->drv
&& cdev
->drv
->set_offline
)
493 return ccw_device_set_offline(cdev
);
497 static int online_store_recog_and_online(struct ccw_device
*cdev
)
499 /* Do device recognition, if needed. */
500 if (cdev
->private->state
== DEV_STATE_BOXED
) {
501 spin_lock_irq(cdev
->ccwlock
);
502 ccw_device_recognition(cdev
);
503 spin_unlock_irq(cdev
->ccwlock
);
504 wait_event(cdev
->private->wait_q
,
505 cdev
->private->flags
.recog_done
);
506 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
507 /* recognition failed */
510 if (cdev
->drv
&& cdev
->drv
->set_online
)
511 return ccw_device_set_online(cdev
);
515 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
519 ret
= online_store_recog_and_online(cdev
);
522 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
523 ret
= ccw_device_stlck(cdev
);
526 if (cdev
->id
.cu_type
== 0)
527 cdev
->private->state
= DEV_STATE_NOT_OPER
;
528 ret
= online_store_recog_and_online(cdev
);
535 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
536 const char *buf
, size_t count
)
538 struct ccw_device
*cdev
= to_ccwdev(dev
);
542 if (!dev_fsm_final_state(cdev
) &&
543 cdev
->private->state
!= DEV_STATE_DISCONNECTED
)
545 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
548 if (cdev
->drv
&& !try_module_get(cdev
->drv
->owner
)) {
549 atomic_set(&cdev
->private->onoff
, 0);
552 if (!strncmp(buf
, "force\n", count
)) {
558 ret
= strict_strtoul(buf
, 16, &i
);
564 ret
= online_store_handle_offline(cdev
);
567 ret
= online_store_handle_online(cdev
, force
);
574 module_put(cdev
->drv
->owner
);
575 atomic_set(&cdev
->private->onoff
, 0);
576 return (ret
< 0) ? ret
: count
;
580 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
582 struct ccw_device
*cdev
= to_ccwdev(dev
);
583 struct subchannel
*sch
;
585 if (ccw_device_is_orphan(cdev
))
586 return sprintf(buf
, "no device\n");
587 switch (cdev
->private->state
) {
588 case DEV_STATE_BOXED
:
589 return sprintf(buf
, "boxed\n");
590 case DEV_STATE_DISCONNECTED
:
591 case DEV_STATE_DISCONNECTED_SENSE_ID
:
592 case DEV_STATE_NOT_OPER
:
593 sch
= to_subchannel(dev
->parent
);
595 return sprintf(buf
, "no path\n");
597 return sprintf(buf
, "no device\n");
599 /* All other states considered fine. */
600 return sprintf(buf
, "good\n");
605 initiate_logging(struct device
*dev
, struct device_attribute
*attr
,
606 const char *buf
, size_t count
)
608 struct subchannel
*sch
= to_subchannel(dev
);
611 rc
= chsc_siosl(sch
->schid
);
613 pr_warning("Logging for subchannel 0.%x.%04x failed with "
615 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
618 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
619 sch
->schid
.ssid
, sch
->schid
.sch_no
);
623 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
624 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
625 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
626 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
627 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
628 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
629 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
630 static DEVICE_ATTR(logging
, 0200, NULL
, initiate_logging
);
632 static struct attribute
*io_subchannel_attrs
[] = {
633 &dev_attr_chpids
.attr
,
634 &dev_attr_pimpampom
.attr
,
635 &dev_attr_logging
.attr
,
639 static struct attribute_group io_subchannel_attr_group
= {
640 .attrs
= io_subchannel_attrs
,
643 static struct attribute
* ccwdev_attrs
[] = {
644 &dev_attr_devtype
.attr
,
645 &dev_attr_cutype
.attr
,
646 &dev_attr_modalias
.attr
,
647 &dev_attr_online
.attr
,
648 &dev_attr_cmb_enable
.attr
,
649 &dev_attr_availability
.attr
,
653 static struct attribute_group ccwdev_attr_group
= {
654 .attrs
= ccwdev_attrs
,
657 static const struct attribute_group
*ccwdev_attr_groups
[] = {
662 /* this is a simple abstraction for device_register that sets the
663 * correct bus type and adds the bus specific files */
664 static int ccw_device_register(struct ccw_device
*cdev
)
666 struct device
*dev
= &cdev
->dev
;
669 dev
->bus
= &ccw_bus_type
;
670 ret
= dev_set_name(&cdev
->dev
, "0.%x.%04x", cdev
->private->dev_id
.ssid
,
671 cdev
->private->dev_id
.devno
);
674 return device_add(dev
);
677 static int match_dev_id(struct device
*dev
, void *data
)
679 struct ccw_device
*cdev
= to_ccwdev(dev
);
680 struct ccw_dev_id
*dev_id
= data
;
682 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
685 static struct ccw_device
*get_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
)
689 dev
= bus_find_device(&ccw_bus_type
, NULL
, dev_id
, match_dev_id
);
691 return dev
? to_ccwdev(dev
) : NULL
;
694 static void ccw_device_do_unbind_bind(struct ccw_device
*cdev
)
698 if (device_is_registered(&cdev
->dev
)) {
699 device_release_driver(&cdev
->dev
);
700 ret
= device_attach(&cdev
->dev
);
701 WARN_ON(ret
== -ENODEV
);
706 ccw_device_release(struct device
*dev
)
708 struct ccw_device
*cdev
;
710 cdev
= to_ccwdev(dev
);
711 /* Release reference of parent subchannel. */
712 put_device(cdev
->dev
.parent
);
713 kfree(cdev
->private);
717 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
719 struct ccw_device
*cdev
;
721 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
723 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
724 GFP_KERNEL
| GFP_DMA
);
729 return ERR_PTR(-ENOMEM
);
732 static void ccw_device_todo(struct work_struct
*work
);
734 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
735 struct ccw_device
*cdev
)
737 cdev
->private->cdev
= cdev
;
738 atomic_set(&cdev
->private->onoff
, 0);
739 cdev
->dev
.parent
= &sch
->dev
;
740 cdev
->dev
.release
= ccw_device_release
;
741 INIT_WORK(&cdev
->private->todo_work
, ccw_device_todo
);
742 cdev
->dev
.groups
= ccwdev_attr_groups
;
743 /* Do first half of device_register. */
744 device_initialize(&cdev
->dev
);
745 if (!get_device(&sch
->dev
)) {
746 /* Release reference from device_initialize(). */
747 put_device(&cdev
->dev
);
750 cdev
->private->flags
.initialized
= 1;
754 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
756 struct ccw_device
*cdev
;
759 cdev
= io_subchannel_allocate_dev(sch
);
761 ret
= io_subchannel_initialize_dev(sch
, cdev
);
768 static void io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
770 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
772 struct ccw_device
*cdev
;
774 /* Need to allocate a new ccw device. */
775 cdev
= io_subchannel_create_ccwdev(sch
);
777 /* OK, we did everything we could... */
778 css_sch_device_unregister(sch
);
781 /* Start recognition for the new ccw device. */
782 io_subchannel_recog(cdev
, sch
);
786 * Register recognized device.
788 static void io_subchannel_register(struct ccw_device
*cdev
)
790 struct subchannel
*sch
;
791 int ret
, adjust_init_count
= 1;
794 sch
= to_subchannel(cdev
->dev
.parent
);
796 * Check if subchannel is still registered. It may have become
797 * unregistered if a machine check hit us after finishing
798 * device recognition but before the register work could be
801 if (!device_is_registered(&sch
->dev
))
803 css_update_ssd_info(sch
);
805 * io_subchannel_register() will also be called after device
806 * recognition has been done for a boxed device (which will already
807 * be registered). We need to reprobe since we may now have sense id
810 if (device_is_registered(&cdev
->dev
)) {
812 ret
= device_reprobe(&cdev
->dev
);
814 /* We can't do much here. */
815 CIO_MSG_EVENT(0, "device_reprobe() returned"
816 " %d for 0.%x.%04x\n", ret
,
817 cdev
->private->dev_id
.ssid
,
818 cdev
->private->dev_id
.devno
);
820 adjust_init_count
= 0;
824 * Now we know this subchannel will stay, we can throw
825 * our delayed uevent.
827 dev_set_uevent_suppress(&sch
->dev
, 0);
828 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
829 /* make it known to the system */
830 ret
= ccw_device_register(cdev
);
832 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
833 cdev
->private->dev_id
.ssid
,
834 cdev
->private->dev_id
.devno
, ret
);
835 spin_lock_irqsave(sch
->lock
, flags
);
836 sch_set_cdev(sch
, NULL
);
837 spin_unlock_irqrestore(sch
->lock
, flags
);
838 /* Release initial device reference. */
839 put_device(&cdev
->dev
);
843 cdev
->private->flags
.recog_done
= 1;
844 wake_up(&cdev
->private->wait_q
);
846 if (adjust_init_count
&& atomic_dec_and_test(&ccw_device_init_count
))
847 wake_up(&ccw_device_init_wq
);
850 static void ccw_device_call_sch_unregister(struct ccw_device
*cdev
)
852 struct subchannel
*sch
;
854 /* Get subchannel reference for local processing. */
855 if (!get_device(cdev
->dev
.parent
))
857 sch
= to_subchannel(cdev
->dev
.parent
);
858 css_sch_device_unregister(sch
);
859 /* Release subchannel reference for local processing. */
860 put_device(&sch
->dev
);
864 * subchannel recognition done. Called from the state machine.
867 io_subchannel_recog_done(struct ccw_device
*cdev
)
869 if (css_init_done
== 0) {
870 cdev
->private->flags
.recog_done
= 1;
873 switch (cdev
->private->state
) {
874 case DEV_STATE_BOXED
:
875 /* Device did not respond in time. */
876 case DEV_STATE_NOT_OPER
:
877 cdev
->private->flags
.recog_done
= 1;
878 /* Remove device found not operational. */
879 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
880 if (atomic_dec_and_test(&ccw_device_init_count
))
881 wake_up(&ccw_device_init_wq
);
883 case DEV_STATE_OFFLINE
:
885 * We can't register the device in interrupt context so
886 * we schedule a work item.
888 ccw_device_sched_todo(cdev
, CDEV_TODO_REGISTER
);
893 static void io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
895 struct ccw_device_private
*priv
;
897 cdev
->ccwlock
= sch
->lock
;
899 /* Init private data. */
900 priv
= cdev
->private;
901 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
902 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
903 priv
->schid
= sch
->schid
;
904 priv
->state
= DEV_STATE_NOT_OPER
;
905 INIT_LIST_HEAD(&priv
->cmb_list
);
906 init_waitqueue_head(&priv
->wait_q
);
907 init_timer(&priv
->timer
);
909 /* Increase counter of devices currently in recognition. */
910 atomic_inc(&ccw_device_init_count
);
912 /* Start async. device sensing. */
913 spin_lock_irq(sch
->lock
);
914 sch_set_cdev(sch
, cdev
);
915 ccw_device_recognition(cdev
);
916 spin_unlock_irq(sch
->lock
);
919 static int ccw_device_move_to_sch(struct ccw_device
*cdev
,
920 struct subchannel
*sch
)
922 struct subchannel
*old_sch
;
923 int rc
, old_enabled
= 0;
925 old_sch
= to_subchannel(cdev
->dev
.parent
);
926 /* Obtain child reference for new parent. */
927 if (!get_device(&sch
->dev
))
930 if (!sch_is_pseudo_sch(old_sch
)) {
931 spin_lock_irq(old_sch
->lock
);
932 old_enabled
= old_sch
->schib
.pmcw
.ena
;
935 rc
= cio_disable_subchannel(old_sch
);
936 spin_unlock_irq(old_sch
->lock
);
938 /* Release child reference for new parent. */
939 put_device(&sch
->dev
);
944 mutex_lock(&sch
->reg_mutex
);
945 rc
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
946 mutex_unlock(&sch
->reg_mutex
);
948 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
949 cdev
->private->dev_id
.ssid
,
950 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
951 sch
->schib
.pmcw
.dev
, rc
);
953 /* Try to reenable the old subchannel. */
954 spin_lock_irq(old_sch
->lock
);
955 cio_enable_subchannel(old_sch
, (u32
)(addr_t
)old_sch
);
956 spin_unlock_irq(old_sch
->lock
);
958 /* Release child reference for new parent. */
959 put_device(&sch
->dev
);
962 /* Clean up old subchannel. */
963 if (!sch_is_pseudo_sch(old_sch
)) {
964 spin_lock_irq(old_sch
->lock
);
965 sch_set_cdev(old_sch
, NULL
);
966 spin_unlock_irq(old_sch
->lock
);
967 css_schedule_eval(old_sch
->schid
);
969 /* Release child reference for old parent. */
970 put_device(&old_sch
->dev
);
971 /* Initialize new subchannel. */
972 spin_lock_irq(sch
->lock
);
973 cdev
->private->schid
= sch
->schid
;
974 cdev
->ccwlock
= sch
->lock
;
975 if (!sch_is_pseudo_sch(sch
))
976 sch_set_cdev(sch
, cdev
);
977 spin_unlock_irq(sch
->lock
);
978 if (!sch_is_pseudo_sch(sch
))
979 css_update_ssd_info(sch
);
983 static int ccw_device_move_to_orph(struct ccw_device
*cdev
)
985 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
986 struct channel_subsystem
*css
= to_css(sch
->dev
.parent
);
988 return ccw_device_move_to_sch(cdev
, css
->pseudo_subchannel
);
991 static void io_subchannel_irq(struct subchannel
*sch
)
993 struct ccw_device
*cdev
;
995 cdev
= sch_get_cdev(sch
);
997 CIO_TRACE_EVENT(6, "IRQ");
998 CIO_TRACE_EVENT(6, dev_name(&sch
->dev
));
1000 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1003 void io_subchannel_init_config(struct subchannel
*sch
)
1005 memset(&sch
->config
, 0, sizeof(sch
->config
));
1006 sch
->config
.csense
= 1;
1009 static void io_subchannel_init_fields(struct subchannel
*sch
)
1011 if (cio_is_console(sch
->schid
))
1014 sch
->opm
= chp_get_sch_opm(sch
);
1015 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1016 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1018 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1019 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1020 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1021 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1022 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1024 io_subchannel_init_config(sch
);
1028 * Note: We always return 0 so that we bind to the device even on error.
1029 * This is needed so that our remove function is called on unregister.
1031 static int io_subchannel_probe(struct subchannel
*sch
)
1033 struct ccw_device
*cdev
;
1036 if (cio_is_console(sch
->schid
)) {
1037 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1038 &io_subchannel_attr_group
);
1040 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1041 "attributes for subchannel "
1042 "0.%x.%04x (rc=%d)\n",
1043 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1045 * The console subchannel already has an associated ccw_device.
1046 * Throw the delayed uevent for the subchannel, register
1047 * the ccw_device and exit.
1049 dev_set_uevent_suppress(&sch
->dev
, 0);
1050 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1051 cdev
= sch_get_cdev(sch
);
1052 cdev
->dev
.groups
= ccwdev_attr_groups
;
1053 device_initialize(&cdev
->dev
);
1054 cdev
->private->flags
.initialized
= 1;
1055 ccw_device_register(cdev
);
1057 * Check if the device is already online. If it is
1058 * the reference count needs to be corrected since we
1059 * didn't obtain a reference in ccw_device_set_online.
1061 if (cdev
->private->state
!= DEV_STATE_NOT_OPER
&&
1062 cdev
->private->state
!= DEV_STATE_OFFLINE
&&
1063 cdev
->private->state
!= DEV_STATE_BOXED
)
1064 get_device(&cdev
->dev
);
1067 io_subchannel_init_fields(sch
);
1068 rc
= cio_commit_config(sch
);
1071 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1072 &io_subchannel_attr_group
);
1075 /* Allocate I/O subchannel private data. */
1076 sch
->private = kzalloc(sizeof(struct io_subchannel_private
),
1077 GFP_KERNEL
| GFP_DMA
);
1080 css_schedule_eval(sch
->schid
);
1084 spin_lock_irq(sch
->lock
);
1085 css_sched_sch_todo(sch
, SCH_TODO_UNREG
);
1086 spin_unlock_irq(sch
->lock
);
1091 io_subchannel_remove (struct subchannel
*sch
)
1093 struct ccw_device
*cdev
;
1095 cdev
= sch_get_cdev(sch
);
1098 io_subchannel_quiesce(sch
);
1099 /* Set ccw device to not operational and drop reference. */
1100 spin_lock_irq(cdev
->ccwlock
);
1101 sch_set_cdev(sch
, NULL
);
1102 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1103 spin_unlock_irq(cdev
->ccwlock
);
1104 ccw_device_unregister(cdev
);
1106 kfree(sch
->private);
1107 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1111 static void io_subchannel_verify(struct subchannel
*sch
)
1113 struct ccw_device
*cdev
;
1115 cdev
= sch_get_cdev(sch
);
1117 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1120 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1122 struct ccw_device
*cdev
;
1124 cdev
= sch_get_cdev(sch
);
1127 if (cio_update_schib(sch
))
1129 /* Check for I/O on path. */
1130 if (scsw_actl(&sch
->schib
.scsw
) == 0 || sch
->schib
.pmcw
.lpum
!= mask
)
1132 if (cdev
->private->state
== DEV_STATE_ONLINE
) {
1133 ccw_device_kill_io(cdev
);
1139 /* Trigger path verification. */
1140 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1144 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1147 static int io_subchannel_chp_event(struct subchannel
*sch
,
1148 struct chp_link
*link
, int event
)
1152 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1159 io_subchannel_terminate_path(sch
, mask
);
1164 io_subchannel_verify(sch
);
1167 if (cio_update_schib(sch
))
1169 io_subchannel_terminate_path(sch
, mask
);
1172 if (cio_update_schib(sch
))
1174 sch
->lpm
|= mask
& sch
->opm
;
1175 io_subchannel_verify(sch
);
1181 static void io_subchannel_quiesce(struct subchannel
*sch
)
1183 struct ccw_device
*cdev
;
1186 spin_lock_irq(sch
->lock
);
1187 cdev
= sch_get_cdev(sch
);
1188 if (cio_is_console(sch
->schid
))
1190 if (!sch
->schib
.pmcw
.ena
)
1192 ret
= cio_disable_subchannel(sch
);
1196 cdev
->handler(cdev
, cdev
->private->intparm
, ERR_PTR(-EIO
));
1197 while (ret
== -EBUSY
) {
1198 cdev
->private->state
= DEV_STATE_QUIESCE
;
1199 ret
= ccw_device_cancel_halt_clear(cdev
);
1200 if (ret
== -EBUSY
) {
1201 ccw_device_set_timeout(cdev
, HZ
/10);
1202 spin_unlock_irq(sch
->lock
);
1203 wait_event(cdev
->private->wait_q
,
1204 cdev
->private->state
!= DEV_STATE_QUIESCE
);
1205 spin_lock_irq(sch
->lock
);
1207 ret
= cio_disable_subchannel(sch
);
1210 spin_unlock_irq(sch
->lock
);
1213 static void io_subchannel_shutdown(struct subchannel
*sch
)
1215 io_subchannel_quiesce(sch
);
1218 static int device_is_disconnected(struct ccw_device
*cdev
)
1222 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1223 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1226 static int recovery_check(struct device
*dev
, void *data
)
1228 struct ccw_device
*cdev
= to_ccwdev(dev
);
1231 spin_lock_irq(cdev
->ccwlock
);
1232 switch (cdev
->private->state
) {
1233 case DEV_STATE_DISCONNECTED
:
1234 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1235 cdev
->private->dev_id
.ssid
,
1236 cdev
->private->dev_id
.devno
);
1237 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1240 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1244 spin_unlock_irq(cdev
->ccwlock
);
1249 static void recovery_work_func(struct work_struct
*unused
)
1253 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1255 spin_lock_irq(&recovery_lock
);
1256 if (!timer_pending(&recovery_timer
)) {
1257 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1259 mod_timer(&recovery_timer
, jiffies
+
1260 recovery_delay
[recovery_phase
] * HZ
);
1262 spin_unlock_irq(&recovery_lock
);
1264 CIO_MSG_EVENT(4, "recovery: end\n");
1267 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1269 static void recovery_func(unsigned long data
)
1272 * We can't do our recovery in softirq context and it's not
1273 * performance critical, so we schedule it.
1275 schedule_work(&recovery_work
);
1278 static void ccw_device_schedule_recovery(void)
1280 unsigned long flags
;
1282 CIO_MSG_EVENT(4, "recovery: schedule\n");
1283 spin_lock_irqsave(&recovery_lock
, flags
);
1284 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1286 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1288 spin_unlock_irqrestore(&recovery_lock
, flags
);
1291 static int purge_fn(struct device
*dev
, void *data
)
1293 struct ccw_device
*cdev
= to_ccwdev(dev
);
1294 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
1296 spin_lock_irq(cdev
->ccwlock
);
1297 if (is_blacklisted(id
->ssid
, id
->devno
) &&
1298 (cdev
->private->state
== DEV_STATE_OFFLINE
)) {
1299 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id
->ssid
,
1301 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1303 spin_unlock_irq(cdev
->ccwlock
);
1304 /* Abort loop in case of pending signal. */
1305 if (signal_pending(current
))
1312 * ccw_purge_blacklisted - purge unused, blacklisted devices
1314 * Unregister all ccw devices that are offline and on the blacklist.
1316 int ccw_purge_blacklisted(void)
1318 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1319 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1323 void ccw_device_set_disconnected(struct ccw_device
*cdev
)
1327 ccw_device_set_timeout(cdev
, 0);
1328 cdev
->private->flags
.fake_irb
= 0;
1329 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1331 ccw_device_schedule_recovery();
1334 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1336 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1338 CIO_TRACE_EVENT(2, "notoper");
1339 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1340 ccw_device_set_timeout(cdev
, 0);
1341 cio_disable_subchannel(sch
);
1342 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1345 enum io_sch_action
{
1349 IO_SCH_UNREG_ATTACH
,
1357 static enum io_sch_action
sch_get_action(struct subchannel
*sch
)
1359 struct ccw_device
*cdev
;
1361 cdev
= sch_get_cdev(sch
);
1362 if (cio_update_schib(sch
)) {
1363 /* Not operational. */
1365 return IO_SCH_UNREG
;
1366 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1367 return IO_SCH_UNREG
;
1368 return IO_SCH_ORPH_UNREG
;
1372 return IO_SCH_ATTACH
;
1373 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
1374 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1375 return IO_SCH_UNREG_ATTACH
;
1376 return IO_SCH_ORPH_ATTACH
;
1378 if ((sch
->schib
.pmcw
.pam
& sch
->opm
) == 0) {
1379 if (ccw_device_notify(cdev
, CIO_NO_PATH
) != NOTIFY_OK
)
1380 return IO_SCH_UNREG
;
1383 if (device_is_disconnected(cdev
))
1384 return IO_SCH_REPROBE
;
1386 return IO_SCH_VERIFY
;
1391 * io_subchannel_sch_event - process subchannel event
1393 * @process: non-zero if function is called in process context
1395 * An unspecified event occurred for this subchannel. Adjust data according
1396 * to the current operational state of the subchannel and device. Return
1397 * zero when the event has been handled sufficiently or -EAGAIN when this
1398 * function should be called again in process context.
1400 static int io_subchannel_sch_event(struct subchannel
*sch
, int process
)
1402 unsigned long flags
;
1403 struct ccw_device
*cdev
;
1404 struct ccw_dev_id dev_id
;
1405 enum io_sch_action action
;
1408 spin_lock_irqsave(sch
->lock
, flags
);
1409 if (!device_is_registered(&sch
->dev
))
1411 if (work_pending(&sch
->todo_work
))
1413 cdev
= sch_get_cdev(sch
);
1414 if (cdev
&& work_pending(&cdev
->private->todo_work
))
1416 action
= sch_get_action(sch
);
1417 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1418 sch
->schid
.ssid
, sch
->schid
.sch_no
, process
,
1420 /* Perform immediate actions while holding the lock. */
1422 case IO_SCH_REPROBE
:
1423 /* Trigger device recognition. */
1424 ccw_device_trigger_reprobe(cdev
);
1428 if (cdev
->private->flags
.resuming
== 1) {
1429 if (cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
)) {
1430 ccw_device_set_notoper(cdev
);
1434 /* Trigger path verification. */
1435 io_subchannel_verify(sch
);
1439 ccw_device_set_disconnected(cdev
);
1442 case IO_SCH_ORPH_UNREG
:
1443 case IO_SCH_ORPH_ATTACH
:
1444 ccw_device_set_disconnected(cdev
);
1446 case IO_SCH_UNREG_ATTACH
:
1449 ccw_device_set_notoper(cdev
);
1457 spin_unlock_irqrestore(sch
->lock
, flags
);
1458 /* All other actions require process context. */
1461 /* Handle attached ccw device. */
1463 case IO_SCH_ORPH_UNREG
:
1464 case IO_SCH_ORPH_ATTACH
:
1465 /* Move ccw device to orphanage. */
1466 rc
= ccw_device_move_to_orph(cdev
);
1470 case IO_SCH_UNREG_ATTACH
:
1471 /* Unregister ccw device. */
1472 if (!cdev
->private->flags
.resuming
)
1473 ccw_device_unregister(cdev
);
1478 /* Handle subchannel. */
1480 case IO_SCH_ORPH_UNREG
:
1482 if (!cdev
|| !cdev
->private->flags
.resuming
)
1483 css_sch_device_unregister(sch
);
1485 case IO_SCH_ORPH_ATTACH
:
1486 case IO_SCH_UNREG_ATTACH
:
1488 dev_id
.ssid
= sch
->schid
.ssid
;
1489 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1490 cdev
= get_ccwdev_by_dev_id(&dev_id
);
1492 sch_create_and_recog_new_device(sch
);
1495 rc
= ccw_device_move_to_sch(cdev
, sch
);
1497 /* Release reference from get_ccwdev_by_dev_id() */
1498 put_device(&cdev
->dev
);
1501 spin_lock_irqsave(sch
->lock
, flags
);
1502 ccw_device_trigger_reprobe(cdev
);
1503 spin_unlock_irqrestore(sch
->lock
, flags
);
1504 /* Release reference from get_ccwdev_by_dev_id() */
1505 put_device(&cdev
->dev
);
1513 spin_unlock_irqrestore(sch
->lock
, flags
);
1518 #ifdef CONFIG_CCW_CONSOLE
1519 static struct ccw_device console_cdev
;
1520 static struct ccw_device_private console_private
;
1521 static int console_cdev_in_use
;
1523 static DEFINE_SPINLOCK(ccw_console_lock
);
1525 spinlock_t
* cio_get_console_lock(void)
1527 return &ccw_console_lock
;
1530 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1531 struct subchannel
*sch
)
1535 /* Attach subchannel private data. */
1536 sch
->private = cio_get_console_priv();
1537 memset(sch
->private, 0, sizeof(struct io_subchannel_private
));
1538 io_subchannel_init_fields(sch
);
1539 rc
= cio_commit_config(sch
);
1542 sch
->driver
= &io_subchannel_driver
;
1543 /* Initialize the ccw_device structure. */
1544 cdev
->dev
.parent
= &sch
->dev
;
1545 sch_set_cdev(sch
, cdev
);
1546 io_subchannel_recog(cdev
, sch
);
1547 /* Now wait for the async. recognition to come to an end. */
1548 spin_lock_irq(cdev
->ccwlock
);
1549 while (!dev_fsm_final_state(cdev
))
1552 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
1554 ccw_device_online(cdev
);
1555 while (!dev_fsm_final_state(cdev
))
1557 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
1561 spin_unlock_irq(cdev
->ccwlock
);
1566 ccw_device_probe_console(void)
1568 struct subchannel
*sch
;
1571 if (xchg(&console_cdev_in_use
, 1) != 0)
1572 return ERR_PTR(-EBUSY
);
1573 sch
= cio_probe_console();
1575 console_cdev_in_use
= 0;
1576 return (void *) sch
;
1578 memset(&console_cdev
, 0, sizeof(struct ccw_device
));
1579 memset(&console_private
, 0, sizeof(struct ccw_device_private
));
1580 console_cdev
.private = &console_private
;
1581 console_private
.cdev
= &console_cdev
;
1582 ret
= ccw_device_console_enable(&console_cdev
, sch
);
1584 cio_release_console();
1585 console_cdev_in_use
= 0;
1586 return ERR_PTR(ret
);
1588 console_cdev
.online
= 1;
1589 return &console_cdev
;
1592 static int ccw_device_pm_restore(struct device
*dev
);
1594 int ccw_device_force_console(void)
1596 if (!console_cdev_in_use
)
1598 return ccw_device_pm_restore(&console_cdev
.dev
);
1600 EXPORT_SYMBOL_GPL(ccw_device_force_console
);
1604 * get ccw_device matching the busid, but only if owned by cdrv
1607 __ccwdev_check_busid(struct device
*dev
, void *id
)
1613 return (strcmp(bus_id
, dev_name(dev
)) == 0);
1618 * get_ccwdev_by_busid() - obtain device from a bus id
1619 * @cdrv: driver the device is owned by
1620 * @bus_id: bus id of the device to be searched
1622 * This function searches all devices owned by @cdrv for a device with a bus
1623 * id matching @bus_id.
1625 * If a match is found, its reference count of the found device is increased
1626 * and it is returned; else %NULL is returned.
1628 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1632 struct device_driver
*drv
;
1634 drv
= get_driver(&cdrv
->driver
);
1638 dev
= driver_find_device(drv
, NULL
, (void *)bus_id
,
1639 __ccwdev_check_busid
);
1642 return dev
? to_ccwdev(dev
) : NULL
;
1645 /************************** device driver handling ************************/
1647 /* This is the implementation of the ccw_driver class. The probe, remove
1648 * and release methods are initially very similar to the device_driver
1649 * implementations, with the difference that they have ccw_device
1652 * A ccw driver also contains the information that is needed for
1656 ccw_device_probe (struct device
*dev
)
1658 struct ccw_device
*cdev
= to_ccwdev(dev
);
1659 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1662 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1664 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1675 ccw_device_remove (struct device
*dev
)
1677 struct ccw_device
*cdev
= to_ccwdev(dev
);
1678 struct ccw_driver
*cdrv
= cdev
->drv
;
1685 spin_lock_irq(cdev
->ccwlock
);
1686 ret
= ccw_device_offline(cdev
);
1687 spin_unlock_irq(cdev
->ccwlock
);
1689 wait_event(cdev
->private->wait_q
,
1690 dev_fsm_final_state(cdev
));
1692 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1693 "device 0.%x.%04x\n",
1694 ret
, cdev
->private->dev_id
.ssid
,
1695 cdev
->private->dev_id
.devno
);
1696 /* Give up reference obtained in ccw_device_set_online(). */
1697 put_device(&cdev
->dev
);
1699 ccw_device_set_timeout(cdev
, 0);
1704 static void ccw_device_shutdown(struct device
*dev
)
1706 struct ccw_device
*cdev
;
1708 cdev
= to_ccwdev(dev
);
1709 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1710 cdev
->drv
->shutdown(cdev
);
1714 static int ccw_device_pm_prepare(struct device
*dev
)
1716 struct ccw_device
*cdev
= to_ccwdev(dev
);
1718 if (work_pending(&cdev
->private->todo_work
))
1720 /* Fail while device is being set online/offline. */
1721 if (atomic_read(&cdev
->private->onoff
))
1724 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->prepare
)
1725 return cdev
->drv
->prepare(cdev
);
1730 static void ccw_device_pm_complete(struct device
*dev
)
1732 struct ccw_device
*cdev
= to_ccwdev(dev
);
1734 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->complete
)
1735 cdev
->drv
->complete(cdev
);
1738 static int ccw_device_pm_freeze(struct device
*dev
)
1740 struct ccw_device
*cdev
= to_ccwdev(dev
);
1741 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1742 int ret
, cm_enabled
;
1744 /* Fail suspend while device is in transistional state. */
1745 if (!dev_fsm_final_state(cdev
))
1749 if (cdev
->drv
&& cdev
->drv
->freeze
) {
1750 ret
= cdev
->drv
->freeze(cdev
);
1755 spin_lock_irq(sch
->lock
);
1756 cm_enabled
= cdev
->private->cmb
!= NULL
;
1757 spin_unlock_irq(sch
->lock
);
1759 /* Don't have the css write on memory. */
1760 ret
= ccw_set_cmf(cdev
, 0);
1764 /* From here on, disallow device driver I/O. */
1765 spin_lock_irq(sch
->lock
);
1766 ret
= cio_disable_subchannel(sch
);
1767 spin_unlock_irq(sch
->lock
);
1772 static int ccw_device_pm_thaw(struct device
*dev
)
1774 struct ccw_device
*cdev
= to_ccwdev(dev
);
1775 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1776 int ret
, cm_enabled
;
1781 spin_lock_irq(sch
->lock
);
1782 /* Allow device driver I/O again. */
1783 ret
= cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1784 cm_enabled
= cdev
->private->cmb
!= NULL
;
1785 spin_unlock_irq(sch
->lock
);
1790 ret
= ccw_set_cmf(cdev
, 1);
1795 if (cdev
->drv
&& cdev
->drv
->thaw
)
1796 ret
= cdev
->drv
->thaw(cdev
);
1801 static void __ccw_device_pm_restore(struct ccw_device
*cdev
)
1803 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1805 spin_lock_irq(sch
->lock
);
1806 if (cio_is_console(sch
->schid
)) {
1807 cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1811 * While we were sleeping, devices may have gone or become
1812 * available again. Kick re-detection.
1814 cdev
->private->flags
.resuming
= 1;
1815 css_schedule_eval(sch
->schid
);
1816 spin_unlock_irq(sch
->lock
);
1817 css_complete_work();
1819 /* cdev may have been moved to a different subchannel. */
1820 sch
= to_subchannel(cdev
->dev
.parent
);
1821 spin_lock_irq(sch
->lock
);
1822 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
1823 cdev
->private->state
!= DEV_STATE_OFFLINE
)
1826 ccw_device_recognition(cdev
);
1827 spin_unlock_irq(sch
->lock
);
1828 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
) ||
1829 cdev
->private->state
== DEV_STATE_DISCONNECTED
);
1830 spin_lock_irq(sch
->lock
);
1833 cdev
->private->flags
.resuming
= 0;
1834 spin_unlock_irq(sch
->lock
);
1837 static int resume_handle_boxed(struct ccw_device
*cdev
)
1839 cdev
->private->state
= DEV_STATE_BOXED
;
1840 if (ccw_device_notify(cdev
, CIO_BOXED
) == NOTIFY_OK
)
1842 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1846 static int resume_handle_disc(struct ccw_device
*cdev
)
1848 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1849 if (ccw_device_notify(cdev
, CIO_GONE
) == NOTIFY_OK
)
1851 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1855 static int ccw_device_pm_restore(struct device
*dev
)
1857 struct ccw_device
*cdev
= to_ccwdev(dev
);
1858 struct subchannel
*sch
;
1861 __ccw_device_pm_restore(cdev
);
1862 sch
= to_subchannel(cdev
->dev
.parent
);
1863 spin_lock_irq(sch
->lock
);
1864 if (cio_is_console(sch
->schid
))
1867 /* check recognition results */
1868 switch (cdev
->private->state
) {
1869 case DEV_STATE_OFFLINE
:
1870 case DEV_STATE_ONLINE
:
1871 cdev
->private->flags
.donotify
= 0;
1873 case DEV_STATE_BOXED
:
1874 ret
= resume_handle_boxed(cdev
);
1879 ret
= resume_handle_disc(cdev
);
1884 /* check if the device type has changed */
1885 if (!ccw_device_test_sense_data(cdev
)) {
1886 ccw_device_update_sense_data(cdev
);
1887 ccw_device_sched_todo(cdev
, CDEV_TODO_REBIND
);
1894 if (ccw_device_online(cdev
)) {
1895 ret
= resume_handle_disc(cdev
);
1900 spin_unlock_irq(sch
->lock
);
1901 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1902 spin_lock_irq(sch
->lock
);
1904 if (ccw_device_notify(cdev
, CIO_OPER
) == NOTIFY_BAD
) {
1905 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1910 /* reenable cmf, if needed */
1911 if (cdev
->private->cmb
) {
1912 spin_unlock_irq(sch
->lock
);
1913 ret
= ccw_set_cmf(cdev
, 1);
1914 spin_lock_irq(sch
->lock
);
1916 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1917 "(rc=%d)\n", cdev
->private->dev_id
.ssid
,
1918 cdev
->private->dev_id
.devno
, ret
);
1924 spin_unlock_irq(sch
->lock
);
1925 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->restore
)
1926 ret
= cdev
->drv
->restore(cdev
);
1930 spin_unlock_irq(sch
->lock
);
1934 static const struct dev_pm_ops ccw_pm_ops
= {
1935 .prepare
= ccw_device_pm_prepare
,
1936 .complete
= ccw_device_pm_complete
,
1937 .freeze
= ccw_device_pm_freeze
,
1938 .thaw
= ccw_device_pm_thaw
,
1939 .restore
= ccw_device_pm_restore
,
1942 struct bus_type ccw_bus_type
= {
1944 .match
= ccw_bus_match
,
1945 .uevent
= ccw_uevent
,
1946 .probe
= ccw_device_probe
,
1947 .remove
= ccw_device_remove
,
1948 .shutdown
= ccw_device_shutdown
,
1953 * ccw_driver_register() - register a ccw driver
1954 * @cdriver: driver to be registered
1956 * This function is mainly a wrapper around driver_register().
1958 * %0 on success and a negative error value on failure.
1960 int ccw_driver_register(struct ccw_driver
*cdriver
)
1962 struct device_driver
*drv
= &cdriver
->driver
;
1964 drv
->bus
= &ccw_bus_type
;
1965 drv
->name
= cdriver
->name
;
1966 drv
->owner
= cdriver
->owner
;
1968 return driver_register(drv
);
1972 * ccw_driver_unregister() - deregister a ccw driver
1973 * @cdriver: driver to be deregistered
1975 * This function is mainly a wrapper around driver_unregister().
1977 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
1979 driver_unregister(&cdriver
->driver
);
1982 /* Helper func for qdio. */
1983 struct subchannel_id
1984 ccw_device_get_subchannel_id(struct ccw_device
*cdev
)
1986 struct subchannel
*sch
;
1988 sch
= to_subchannel(cdev
->dev
.parent
);
1992 static void ccw_device_todo(struct work_struct
*work
)
1994 struct ccw_device_private
*priv
;
1995 struct ccw_device
*cdev
;
1996 struct subchannel
*sch
;
1997 enum cdev_todo todo
;
1999 priv
= container_of(work
, struct ccw_device_private
, todo_work
);
2001 sch
= to_subchannel(cdev
->dev
.parent
);
2002 /* Find out todo. */
2003 spin_lock_irq(cdev
->ccwlock
);
2005 priv
->todo
= CDEV_TODO_NOTHING
;
2006 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2007 priv
->dev_id
.ssid
, priv
->dev_id
.devno
, todo
);
2008 spin_unlock_irq(cdev
->ccwlock
);
2011 case CDEV_TODO_ENABLE_CMF
:
2014 case CDEV_TODO_REBIND
:
2015 ccw_device_do_unbind_bind(cdev
);
2017 case CDEV_TODO_REGISTER
:
2018 io_subchannel_register(cdev
);
2020 case CDEV_TODO_UNREG_EVAL
:
2021 if (!sch_is_pseudo_sch(sch
))
2022 css_schedule_eval(sch
->schid
);
2024 case CDEV_TODO_UNREG
:
2025 if (sch_is_pseudo_sch(sch
))
2026 ccw_device_unregister(cdev
);
2028 ccw_device_call_sch_unregister(cdev
);
2033 /* Release workqueue ref. */
2034 put_device(&cdev
->dev
);
2038 * ccw_device_sched_todo - schedule ccw device operation
2042 * Schedule the operation identified by @todo to be performed on the slow path
2043 * workqueue. Do nothing if another operation with higher priority is already
2044 * scheduled. Needs to be called with ccwdev lock held.
2046 void ccw_device_sched_todo(struct ccw_device
*cdev
, enum cdev_todo todo
)
2048 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2049 cdev
->private->dev_id
.ssid
, cdev
->private->dev_id
.devno
,
2051 if (cdev
->private->todo
>= todo
)
2053 cdev
->private->todo
= todo
;
2054 /* Get workqueue ref. */
2055 if (!get_device(&cdev
->dev
))
2057 if (!queue_work(cio_work_q
, &cdev
->private->todo_work
)) {
2058 /* Already queued, release workqueue ref. */
2059 put_device(&cdev
->dev
);
2064 * ccw_device_siosl() - initiate logging
2067 * This function is used to invoke model-dependent logging within the channel
2070 int ccw_device_siosl(struct ccw_device
*cdev
)
2072 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
2074 return chsc_siosl(sch
->schid
);
2076 EXPORT_SYMBOL_GPL(ccw_device_siosl
);
2078 MODULE_LICENSE("GPL");
2079 EXPORT_SYMBOL(ccw_device_set_online
);
2080 EXPORT_SYMBOL(ccw_device_set_offline
);
2081 EXPORT_SYMBOL(ccw_driver_register
);
2082 EXPORT_SYMBOL(ccw_driver_unregister
);
2083 EXPORT_SYMBOL(get_ccwdev_by_busid
);
2084 EXPORT_SYMBOL(ccw_bus_type
);
2085 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id
);