2 * bus driver for ccw devices
4 * Copyright IBM Corp. 2002, 2008
5 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
6 * Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #define KMSG_COMPONENT "cio"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/device.h>
21 #include <linux/workqueue.h>
22 #include <linux/delay.h>
23 #include <linux/timer.h>
24 #include <linux/kernel_stat.h>
26 #include <asm/ccwdev.h>
28 #include <asm/param.h> /* HZ */
34 #include "cio_debug.h"
39 #include "blacklist.h"
42 static struct timer_list recovery_timer
;
43 static DEFINE_SPINLOCK(recovery_lock
);
44 static int recovery_phase
;
45 static const unsigned long recovery_delay
[] = { 3, 30, 300 };
47 /******************* bus type handling ***********************/
49 /* The Linux driver model distinguishes between a bus type and
50 * the bus itself. Of course we only have one channel
51 * subsystem driver and one channel system per machine, but
52 * we still use the abstraction. T.R. says it's a good idea. */
54 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
56 struct ccw_device
*cdev
= to_ccwdev(dev
);
57 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
58 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
63 found
= ccw_device_id_match(ids
, &cdev
->id
);
67 cdev
->id
.driver_info
= found
->driver_info
;
72 /* Store modalias string delimited by prefix/suffix string into buffer with
73 * specified size. Return length of resulting string (excluding trailing '\0')
74 * even if string doesn't fit buffer (snprintf semantics). */
75 static int snprint_alias(char *buf
, size_t size
,
76 struct ccw_device_id
*id
, const char *suffix
)
80 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
86 if (id
->dev_type
!= 0)
87 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
88 id
->dev_model
, suffix
);
90 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
95 /* Set up environment variables for ccw device uevent. Return 0 on success,
96 * non-zero otherwise. */
97 static int ccw_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
99 struct ccw_device
*cdev
= to_ccwdev(dev
);
100 struct ccw_device_id
*id
= &(cdev
->id
);
102 char modalias_buf
[30];
105 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
110 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
114 /* The next two can be zero, that's ok for us */
116 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
121 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
126 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
127 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
131 static struct bus_type ccw_bus_type
;
133 static void io_subchannel_irq(struct subchannel
*);
134 static int io_subchannel_probe(struct subchannel
*);
135 static int io_subchannel_remove(struct subchannel
*);
136 static void io_subchannel_shutdown(struct subchannel
*);
137 static int io_subchannel_sch_event(struct subchannel
*, int);
138 static int io_subchannel_chp_event(struct subchannel
*, struct chp_link
*,
140 static void recovery_func(unsigned long data
);
141 wait_queue_head_t ccw_device_init_wq
;
142 atomic_t ccw_device_init_count
;
144 static struct css_device_id io_subchannel_ids
[] = {
145 { .match_flags
= 0x1, .type
= SUBCHANNEL_TYPE_IO
, },
146 { /* end of list */ },
148 MODULE_DEVICE_TABLE(css
, io_subchannel_ids
);
150 static int io_subchannel_prepare(struct subchannel
*sch
)
152 struct ccw_device
*cdev
;
154 * Don't allow suspend while a ccw device registration
155 * is still outstanding.
157 cdev
= sch_get_cdev(sch
);
158 if (cdev
&& !device_is_registered(&cdev
->dev
))
163 static int io_subchannel_settle(void)
167 ret
= wait_event_interruptible(ccw_device_init_wq
,
168 atomic_read(&ccw_device_init_count
) == 0);
171 flush_workqueue(cio_work_q
);
175 static struct css_driver io_subchannel_driver
= {
177 .owner
= THIS_MODULE
,
178 .name
= "io_subchannel",
180 .subchannel_type
= io_subchannel_ids
,
181 .irq
= io_subchannel_irq
,
182 .sch_event
= io_subchannel_sch_event
,
183 .chp_event
= io_subchannel_chp_event
,
184 .probe
= io_subchannel_probe
,
185 .remove
= io_subchannel_remove
,
186 .shutdown
= io_subchannel_shutdown
,
187 .prepare
= io_subchannel_prepare
,
188 .settle
= io_subchannel_settle
,
191 int __init
io_subchannel_init(void)
195 init_waitqueue_head(&ccw_device_init_wq
);
196 atomic_set(&ccw_device_init_count
, 0);
197 setup_timer(&recovery_timer
, recovery_func
, 0);
199 ret
= bus_register(&ccw_bus_type
);
202 ret
= css_driver_register(&io_subchannel_driver
);
204 bus_unregister(&ccw_bus_type
);
210 /************************ device handling **************************/
213 * A ccw_device has some interfaces in sysfs in addition to the
215 * The following entries are designed to export the information which
216 * resided in 2.4 in /proc/subchannels. Subchannel and device number
217 * are obvious, so they don't have an entry :)
218 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
221 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
223 struct subchannel
*sch
= to_subchannel(dev
);
224 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
229 for (chp
= 0; chp
< 8; chp
++) {
231 if (ssd
->path_mask
& mask
)
232 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
234 ret
+= sprintf(buf
+ ret
, "00 ");
236 ret
+= sprintf (buf
+ret
, "\n");
237 return min((ssize_t
)PAGE_SIZE
, ret
);
241 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
243 struct subchannel
*sch
= to_subchannel(dev
);
244 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
246 return sprintf (buf
, "%02x %02x %02x\n",
247 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
251 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
253 struct ccw_device
*cdev
= to_ccwdev(dev
);
254 struct ccw_device_id
*id
= &(cdev
->id
);
256 if (id
->dev_type
!= 0)
257 return sprintf(buf
, "%04x/%02x\n",
258 id
->dev_type
, id
->dev_model
);
260 return sprintf(buf
, "n/a\n");
264 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
266 struct ccw_device
*cdev
= to_ccwdev(dev
);
267 struct ccw_device_id
*id
= &(cdev
->id
);
269 return sprintf(buf
, "%04x/%02x\n",
270 id
->cu_type
, id
->cu_model
);
274 modalias_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
);
280 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
282 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
286 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
288 struct ccw_device
*cdev
= to_ccwdev(dev
);
290 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
293 int ccw_device_is_orphan(struct ccw_device
*cdev
)
295 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
298 static void ccw_device_unregister(struct ccw_device
*cdev
)
300 if (device_is_registered(&cdev
->dev
)) {
301 /* Undo device_add(). */
302 device_del(&cdev
->dev
);
304 if (cdev
->private->flags
.initialized
) {
305 cdev
->private->flags
.initialized
= 0;
306 /* Release reference from device_initialize(). */
307 put_device(&cdev
->dev
);
311 static void io_subchannel_quiesce(struct subchannel
*);
314 * ccw_device_set_offline() - disable a ccw device for I/O
315 * @cdev: target ccw device
317 * This function calls the driver's set_offline() function for @cdev, if
318 * given, and then disables @cdev.
320 * %0 on success and a negative error value on failure.
322 * enabled, ccw device lock not held
324 int ccw_device_set_offline(struct ccw_device
*cdev
)
326 struct subchannel
*sch
;
331 if (!cdev
->online
|| !cdev
->drv
)
334 if (cdev
->drv
->set_offline
) {
335 ret
= cdev
->drv
->set_offline(cdev
);
340 spin_lock_irq(cdev
->ccwlock
);
341 sch
= to_subchannel(cdev
->dev
.parent
);
342 /* Wait until a final state or DISCONNECTED is reached */
343 while (!dev_fsm_final_state(cdev
) &&
344 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
345 spin_unlock_irq(cdev
->ccwlock
);
346 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
347 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
348 spin_lock_irq(cdev
->ccwlock
);
351 ret
= ccw_device_offline(cdev
);
354 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
355 "0.%x.%04x\n", ret
, cdev
->private->dev_id
.ssid
,
356 cdev
->private->dev_id
.devno
);
359 state
= cdev
->private->state
;
360 spin_unlock_irq(cdev
->ccwlock
);
361 io_subchannel_quiesce(sch
);
362 spin_lock_irq(cdev
->ccwlock
);
363 cdev
->private->state
= state
;
364 } while (ret
== -EBUSY
);
365 spin_unlock_irq(cdev
->ccwlock
);
366 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
367 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
368 /* Inform the user if set offline failed. */
369 if (cdev
->private->state
== DEV_STATE_BOXED
) {
370 pr_warning("%s: The device entered boxed state while "
371 "being set offline\n", dev_name(&cdev
->dev
));
372 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
373 pr_warning("%s: The device stopped operating while "
374 "being set offline\n", dev_name(&cdev
->dev
));
376 /* Give up reference from ccw_device_set_online(). */
377 put_device(&cdev
->dev
);
381 cdev
->private->state
= DEV_STATE_OFFLINE
;
382 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
383 spin_unlock_irq(cdev
->ccwlock
);
384 /* Give up reference from ccw_device_set_online(). */
385 put_device(&cdev
->dev
);
390 * ccw_device_set_online() - enable a ccw device for I/O
391 * @cdev: target ccw device
393 * This function first enables @cdev and then calls the driver's set_online()
394 * function for @cdev, if given. If set_online() returns an error, @cdev is
397 * %0 on success and a negative error value on failure.
399 * enabled, ccw device lock not held
401 int ccw_device_set_online(struct ccw_device
*cdev
)
408 if (cdev
->online
|| !cdev
->drv
)
410 /* Hold on to an extra reference while device is online. */
411 if (!get_device(&cdev
->dev
))
414 spin_lock_irq(cdev
->ccwlock
);
415 ret
= ccw_device_online(cdev
);
416 spin_unlock_irq(cdev
->ccwlock
);
418 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
420 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
421 "device 0.%x.%04x\n",
422 ret
, cdev
->private->dev_id
.ssid
,
423 cdev
->private->dev_id
.devno
);
424 /* Give up online reference since onlining failed. */
425 put_device(&cdev
->dev
);
428 spin_lock_irq(cdev
->ccwlock
);
429 /* Check if online processing was successful */
430 if ((cdev
->private->state
!= DEV_STATE_ONLINE
) &&
431 (cdev
->private->state
!= DEV_STATE_W4SENSE
)) {
432 spin_unlock_irq(cdev
->ccwlock
);
433 /* Inform the user that set online failed. */
434 if (cdev
->private->state
== DEV_STATE_BOXED
) {
435 pr_warning("%s: Setting the device online failed "
436 "because it is boxed\n",
437 dev_name(&cdev
->dev
));
438 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
439 pr_warning("%s: Setting the device online failed "
440 "because it is not operational\n",
441 dev_name(&cdev
->dev
));
443 /* Give up online reference since onlining failed. */
444 put_device(&cdev
->dev
);
447 spin_unlock_irq(cdev
->ccwlock
);
448 if (cdev
->drv
->set_online
)
449 ret
= cdev
->drv
->set_online(cdev
);
456 spin_lock_irq(cdev
->ccwlock
);
457 /* Wait until a final state or DISCONNECTED is reached */
458 while (!dev_fsm_final_state(cdev
) &&
459 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
460 spin_unlock_irq(cdev
->ccwlock
);
461 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
462 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
463 spin_lock_irq(cdev
->ccwlock
);
465 ret2
= ccw_device_offline(cdev
);
468 spin_unlock_irq(cdev
->ccwlock
);
469 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
470 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
471 /* Give up online reference since onlining failed. */
472 put_device(&cdev
->dev
);
476 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
477 "device 0.%x.%04x\n",
478 ret2
, cdev
->private->dev_id
.ssid
,
479 cdev
->private->dev_id
.devno
);
480 cdev
->private->state
= DEV_STATE_OFFLINE
;
481 spin_unlock_irq(cdev
->ccwlock
);
482 /* Give up online reference since onlining failed. */
483 put_device(&cdev
->dev
);
487 static int online_store_handle_offline(struct ccw_device
*cdev
)
489 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
) {
490 spin_lock_irq(cdev
->ccwlock
);
491 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG_EVAL
);
492 spin_unlock_irq(cdev
->ccwlock
);
495 if (cdev
->drv
&& cdev
->drv
->set_offline
)
496 return ccw_device_set_offline(cdev
);
500 static int online_store_recog_and_online(struct ccw_device
*cdev
)
502 /* Do device recognition, if needed. */
503 if (cdev
->private->state
== DEV_STATE_BOXED
) {
504 spin_lock_irq(cdev
->ccwlock
);
505 ccw_device_recognition(cdev
);
506 spin_unlock_irq(cdev
->ccwlock
);
507 wait_event(cdev
->private->wait_q
,
508 cdev
->private->flags
.recog_done
);
509 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
510 /* recognition failed */
513 if (cdev
->drv
&& cdev
->drv
->set_online
)
514 return ccw_device_set_online(cdev
);
518 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
522 ret
= online_store_recog_and_online(cdev
);
525 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
526 ret
= ccw_device_stlck(cdev
);
529 if (cdev
->id
.cu_type
== 0)
530 cdev
->private->state
= DEV_STATE_NOT_OPER
;
531 ret
= online_store_recog_and_online(cdev
);
538 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
539 const char *buf
, size_t count
)
541 struct ccw_device
*cdev
= to_ccwdev(dev
);
545 /* Prevent conflict between multiple on-/offline processing requests. */
546 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
548 /* Prevent conflict between internal I/Os and on-/offline processing. */
549 if (!dev_fsm_final_state(cdev
) &&
550 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
554 /* Prevent conflict between pending work and on-/offline processing.*/
555 if (work_pending(&cdev
->private->todo_work
)) {
560 if (cdev
->drv
&& !try_module_get(cdev
->drv
->driver
.owner
)) {
564 if (!strncmp(buf
, "force\n", count
)) {
570 ret
= strict_strtoul(buf
, 16, &i
);
576 ret
= online_store_handle_offline(cdev
);
579 ret
= online_store_handle_online(cdev
, force
);
586 module_put(cdev
->drv
->driver
.owner
);
588 atomic_set(&cdev
->private->onoff
, 0);
589 return (ret
< 0) ? ret
: count
;
593 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
595 struct ccw_device
*cdev
= to_ccwdev(dev
);
596 struct subchannel
*sch
;
598 if (ccw_device_is_orphan(cdev
))
599 return sprintf(buf
, "no device\n");
600 switch (cdev
->private->state
) {
601 case DEV_STATE_BOXED
:
602 return sprintf(buf
, "boxed\n");
603 case DEV_STATE_DISCONNECTED
:
604 case DEV_STATE_DISCONNECTED_SENSE_ID
:
605 case DEV_STATE_NOT_OPER
:
606 sch
= to_subchannel(dev
->parent
);
608 return sprintf(buf
, "no path\n");
610 return sprintf(buf
, "no device\n");
612 /* All other states considered fine. */
613 return sprintf(buf
, "good\n");
618 initiate_logging(struct device
*dev
, struct device_attribute
*attr
,
619 const char *buf
, size_t count
)
621 struct subchannel
*sch
= to_subchannel(dev
);
624 rc
= chsc_siosl(sch
->schid
);
626 pr_warning("Logging for subchannel 0.%x.%04x failed with "
628 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
631 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
632 sch
->schid
.ssid
, sch
->schid
.sch_no
);
636 static ssize_t
vpm_show(struct device
*dev
, struct device_attribute
*attr
,
639 struct subchannel
*sch
= to_subchannel(dev
);
641 return sprintf(buf
, "%02x\n", sch
->vpm
);
644 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
645 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
646 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
647 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
648 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
649 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
650 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
651 static DEVICE_ATTR(logging
, 0200, NULL
, initiate_logging
);
652 static DEVICE_ATTR(vpm
, 0444, vpm_show
, NULL
);
654 static struct attribute
*io_subchannel_attrs
[] = {
655 &dev_attr_chpids
.attr
,
656 &dev_attr_pimpampom
.attr
,
657 &dev_attr_logging
.attr
,
662 static struct attribute_group io_subchannel_attr_group
= {
663 .attrs
= io_subchannel_attrs
,
666 static struct attribute
* ccwdev_attrs
[] = {
667 &dev_attr_devtype
.attr
,
668 &dev_attr_cutype
.attr
,
669 &dev_attr_modalias
.attr
,
670 &dev_attr_online
.attr
,
671 &dev_attr_cmb_enable
.attr
,
672 &dev_attr_availability
.attr
,
676 static struct attribute_group ccwdev_attr_group
= {
677 .attrs
= ccwdev_attrs
,
680 static const struct attribute_group
*ccwdev_attr_groups
[] = {
685 /* this is a simple abstraction for device_register that sets the
686 * correct bus type and adds the bus specific files */
687 static int ccw_device_register(struct ccw_device
*cdev
)
689 struct device
*dev
= &cdev
->dev
;
692 dev
->bus
= &ccw_bus_type
;
693 ret
= dev_set_name(&cdev
->dev
, "0.%x.%04x", cdev
->private->dev_id
.ssid
,
694 cdev
->private->dev_id
.devno
);
697 return device_add(dev
);
700 static int match_dev_id(struct device
*dev
, void *data
)
702 struct ccw_device
*cdev
= to_ccwdev(dev
);
703 struct ccw_dev_id
*dev_id
= data
;
705 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
709 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
710 * @dev_id: id of the device to be searched
712 * This function searches all devices attached to the ccw bus for a device
715 * If a device is found its reference count is increased and returned;
716 * else %NULL is returned.
718 struct ccw_device
*get_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
)
722 dev
= bus_find_device(&ccw_bus_type
, NULL
, dev_id
, match_dev_id
);
724 return dev
? to_ccwdev(dev
) : NULL
;
726 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id
);
728 static void ccw_device_do_unbind_bind(struct ccw_device
*cdev
)
732 if (device_is_registered(&cdev
->dev
)) {
733 device_release_driver(&cdev
->dev
);
734 ret
= device_attach(&cdev
->dev
);
735 WARN_ON(ret
== -ENODEV
);
740 ccw_device_release(struct device
*dev
)
742 struct ccw_device
*cdev
;
744 cdev
= to_ccwdev(dev
);
745 /* Release reference of parent subchannel. */
746 put_device(cdev
->dev
.parent
);
747 kfree(cdev
->private);
751 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
753 struct ccw_device
*cdev
;
755 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
757 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
758 GFP_KERNEL
| GFP_DMA
);
763 return ERR_PTR(-ENOMEM
);
766 static void ccw_device_todo(struct work_struct
*work
);
768 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
769 struct ccw_device
*cdev
)
771 cdev
->private->cdev
= cdev
;
772 cdev
->private->int_class
= IRQIO_CIO
;
773 atomic_set(&cdev
->private->onoff
, 0);
774 cdev
->dev
.parent
= &sch
->dev
;
775 cdev
->dev
.release
= ccw_device_release
;
776 INIT_WORK(&cdev
->private->todo_work
, ccw_device_todo
);
777 cdev
->dev
.groups
= ccwdev_attr_groups
;
778 /* Do first half of device_register. */
779 device_initialize(&cdev
->dev
);
780 if (!get_device(&sch
->dev
)) {
781 /* Release reference from device_initialize(). */
782 put_device(&cdev
->dev
);
785 cdev
->private->flags
.initialized
= 1;
789 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
791 struct ccw_device
*cdev
;
794 cdev
= io_subchannel_allocate_dev(sch
);
796 ret
= io_subchannel_initialize_dev(sch
, cdev
);
803 static void io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
805 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
807 struct ccw_device
*cdev
;
809 /* Need to allocate a new ccw device. */
810 cdev
= io_subchannel_create_ccwdev(sch
);
812 /* OK, we did everything we could... */
813 css_sch_device_unregister(sch
);
816 /* Start recognition for the new ccw device. */
817 io_subchannel_recog(cdev
, sch
);
821 * Register recognized device.
823 static void io_subchannel_register(struct ccw_device
*cdev
)
825 struct subchannel
*sch
;
826 int ret
, adjust_init_count
= 1;
829 sch
= to_subchannel(cdev
->dev
.parent
);
831 * Check if subchannel is still registered. It may have become
832 * unregistered if a machine check hit us after finishing
833 * device recognition but before the register work could be
836 if (!device_is_registered(&sch
->dev
))
838 css_update_ssd_info(sch
);
840 * io_subchannel_register() will also be called after device
841 * recognition has been done for a boxed device (which will already
842 * be registered). We need to reprobe since we may now have sense id
845 if (device_is_registered(&cdev
->dev
)) {
847 ret
= device_reprobe(&cdev
->dev
);
849 /* We can't do much here. */
850 CIO_MSG_EVENT(0, "device_reprobe() returned"
851 " %d for 0.%x.%04x\n", ret
,
852 cdev
->private->dev_id
.ssid
,
853 cdev
->private->dev_id
.devno
);
855 adjust_init_count
= 0;
859 * Now we know this subchannel will stay, we can throw
860 * our delayed uevent.
862 dev_set_uevent_suppress(&sch
->dev
, 0);
863 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
864 /* make it known to the system */
865 ret
= ccw_device_register(cdev
);
867 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
868 cdev
->private->dev_id
.ssid
,
869 cdev
->private->dev_id
.devno
, ret
);
870 spin_lock_irqsave(sch
->lock
, flags
);
871 sch_set_cdev(sch
, NULL
);
872 spin_unlock_irqrestore(sch
->lock
, flags
);
873 /* Release initial device reference. */
874 put_device(&cdev
->dev
);
878 cdev
->private->flags
.recog_done
= 1;
879 wake_up(&cdev
->private->wait_q
);
881 if (adjust_init_count
&& atomic_dec_and_test(&ccw_device_init_count
))
882 wake_up(&ccw_device_init_wq
);
885 static void ccw_device_call_sch_unregister(struct ccw_device
*cdev
)
887 struct subchannel
*sch
;
889 /* Get subchannel reference for local processing. */
890 if (!get_device(cdev
->dev
.parent
))
892 sch
= to_subchannel(cdev
->dev
.parent
);
893 css_sch_device_unregister(sch
);
894 /* Release subchannel reference for local processing. */
895 put_device(&sch
->dev
);
899 * subchannel recognition done. Called from the state machine.
902 io_subchannel_recog_done(struct ccw_device
*cdev
)
904 if (css_init_done
== 0) {
905 cdev
->private->flags
.recog_done
= 1;
908 switch (cdev
->private->state
) {
909 case DEV_STATE_BOXED
:
910 /* Device did not respond in time. */
911 case DEV_STATE_NOT_OPER
:
912 cdev
->private->flags
.recog_done
= 1;
913 /* Remove device found not operational. */
914 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
915 if (atomic_dec_and_test(&ccw_device_init_count
))
916 wake_up(&ccw_device_init_wq
);
918 case DEV_STATE_OFFLINE
:
920 * We can't register the device in interrupt context so
921 * we schedule a work item.
923 ccw_device_sched_todo(cdev
, CDEV_TODO_REGISTER
);
928 static void io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
930 struct ccw_device_private
*priv
;
932 cdev
->ccwlock
= sch
->lock
;
934 /* Init private data. */
935 priv
= cdev
->private;
936 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
937 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
938 priv
->schid
= sch
->schid
;
939 priv
->state
= DEV_STATE_NOT_OPER
;
940 INIT_LIST_HEAD(&priv
->cmb_list
);
941 init_waitqueue_head(&priv
->wait_q
);
942 init_timer(&priv
->timer
);
944 /* Increase counter of devices currently in recognition. */
945 atomic_inc(&ccw_device_init_count
);
947 /* Start async. device sensing. */
948 spin_lock_irq(sch
->lock
);
949 sch_set_cdev(sch
, cdev
);
950 ccw_device_recognition(cdev
);
951 spin_unlock_irq(sch
->lock
);
954 static int ccw_device_move_to_sch(struct ccw_device
*cdev
,
955 struct subchannel
*sch
)
957 struct subchannel
*old_sch
;
958 int rc
, old_enabled
= 0;
960 old_sch
= to_subchannel(cdev
->dev
.parent
);
961 /* Obtain child reference for new parent. */
962 if (!get_device(&sch
->dev
))
965 if (!sch_is_pseudo_sch(old_sch
)) {
966 spin_lock_irq(old_sch
->lock
);
967 old_enabled
= old_sch
->schib
.pmcw
.ena
;
970 rc
= cio_disable_subchannel(old_sch
);
971 spin_unlock_irq(old_sch
->lock
);
973 /* Release child reference for new parent. */
974 put_device(&sch
->dev
);
979 mutex_lock(&sch
->reg_mutex
);
980 rc
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
981 mutex_unlock(&sch
->reg_mutex
);
983 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
984 cdev
->private->dev_id
.ssid
,
985 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
986 sch
->schib
.pmcw
.dev
, rc
);
988 /* Try to reenable the old subchannel. */
989 spin_lock_irq(old_sch
->lock
);
990 cio_enable_subchannel(old_sch
, (u32
)(addr_t
)old_sch
);
991 spin_unlock_irq(old_sch
->lock
);
993 /* Release child reference for new parent. */
994 put_device(&sch
->dev
);
997 /* Clean up old subchannel. */
998 if (!sch_is_pseudo_sch(old_sch
)) {
999 spin_lock_irq(old_sch
->lock
);
1000 sch_set_cdev(old_sch
, NULL
);
1001 spin_unlock_irq(old_sch
->lock
);
1002 css_schedule_eval(old_sch
->schid
);
1004 /* Release child reference for old parent. */
1005 put_device(&old_sch
->dev
);
1006 /* Initialize new subchannel. */
1007 spin_lock_irq(sch
->lock
);
1008 cdev
->private->schid
= sch
->schid
;
1009 cdev
->ccwlock
= sch
->lock
;
1010 if (!sch_is_pseudo_sch(sch
))
1011 sch_set_cdev(sch
, cdev
);
1012 spin_unlock_irq(sch
->lock
);
1013 if (!sch_is_pseudo_sch(sch
))
1014 css_update_ssd_info(sch
);
1018 static int ccw_device_move_to_orph(struct ccw_device
*cdev
)
1020 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1021 struct channel_subsystem
*css
= to_css(sch
->dev
.parent
);
1023 return ccw_device_move_to_sch(cdev
, css
->pseudo_subchannel
);
1026 static void io_subchannel_irq(struct subchannel
*sch
)
1028 struct ccw_device
*cdev
;
1030 cdev
= sch_get_cdev(sch
);
1032 CIO_TRACE_EVENT(6, "IRQ");
1033 CIO_TRACE_EVENT(6, dev_name(&sch
->dev
));
1035 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1037 inc_irq_stat(IRQIO_CIO
);
1040 void io_subchannel_init_config(struct subchannel
*sch
)
1042 memset(&sch
->config
, 0, sizeof(sch
->config
));
1043 sch
->config
.csense
= 1;
1046 static void io_subchannel_init_fields(struct subchannel
*sch
)
1048 if (cio_is_console(sch
->schid
))
1051 sch
->opm
= chp_get_sch_opm(sch
);
1052 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1053 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1055 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1056 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1057 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1058 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1059 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1061 io_subchannel_init_config(sch
);
1065 * Note: We always return 0 so that we bind to the device even on error.
1066 * This is needed so that our remove function is called on unregister.
1068 static int io_subchannel_probe(struct subchannel
*sch
)
1070 struct io_subchannel_private
*io_priv
;
1071 struct ccw_device
*cdev
;
1074 if (cio_is_console(sch
->schid
)) {
1075 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1076 &io_subchannel_attr_group
);
1078 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1079 "attributes for subchannel "
1080 "0.%x.%04x (rc=%d)\n",
1081 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1083 * The console subchannel already has an associated ccw_device.
1084 * Throw the delayed uevent for the subchannel, register
1085 * the ccw_device and exit.
1087 dev_set_uevent_suppress(&sch
->dev
, 0);
1088 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1089 cdev
= sch_get_cdev(sch
);
1090 rc
= ccw_device_register(cdev
);
1092 /* Release online reference. */
1093 put_device(&cdev
->dev
);
1098 io_subchannel_init_fields(sch
);
1099 rc
= cio_commit_config(sch
);
1102 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1103 &io_subchannel_attr_group
);
1106 /* Allocate I/O subchannel private data. */
1107 io_priv
= kzalloc(sizeof(*io_priv
), GFP_KERNEL
| GFP_DMA
);
1111 set_io_private(sch
, io_priv
);
1112 css_schedule_eval(sch
->schid
);
1116 spin_lock_irq(sch
->lock
);
1117 css_sched_sch_todo(sch
, SCH_TODO_UNREG
);
1118 spin_unlock_irq(sch
->lock
);
1123 io_subchannel_remove (struct subchannel
*sch
)
1125 struct io_subchannel_private
*io_priv
= to_io_private(sch
);
1126 struct ccw_device
*cdev
;
1128 cdev
= sch_get_cdev(sch
);
1131 io_subchannel_quiesce(sch
);
1132 /* Set ccw device to not operational and drop reference. */
1133 spin_lock_irq(cdev
->ccwlock
);
1134 sch_set_cdev(sch
, NULL
);
1135 set_io_private(sch
, NULL
);
1136 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1137 spin_unlock_irq(cdev
->ccwlock
);
1138 ccw_device_unregister(cdev
);
1141 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1145 static void io_subchannel_verify(struct subchannel
*sch
)
1147 struct ccw_device
*cdev
;
1149 cdev
= sch_get_cdev(sch
);
1151 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1154 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1156 struct ccw_device
*cdev
;
1158 cdev
= sch_get_cdev(sch
);
1161 if (cio_update_schib(sch
))
1163 /* Check for I/O on path. */
1164 if (scsw_actl(&sch
->schib
.scsw
) == 0 || sch
->schib
.pmcw
.lpum
!= mask
)
1166 if (cdev
->private->state
== DEV_STATE_ONLINE
) {
1167 ccw_device_kill_io(cdev
);
1173 /* Trigger path verification. */
1174 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1178 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1181 static int io_subchannel_chp_event(struct subchannel
*sch
,
1182 struct chp_link
*link
, int event
)
1184 struct ccw_device
*cdev
= sch_get_cdev(sch
);
1187 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1195 cdev
->private->path_gone_mask
|= mask
;
1196 io_subchannel_terminate_path(sch
, mask
);
1202 cdev
->private->path_new_mask
|= mask
;
1203 io_subchannel_verify(sch
);
1206 if (cio_update_schib(sch
))
1209 cdev
->private->path_gone_mask
|= mask
;
1210 io_subchannel_terminate_path(sch
, mask
);
1213 if (cio_update_schib(sch
))
1215 sch
->lpm
|= mask
& sch
->opm
;
1217 cdev
->private->path_new_mask
|= mask
;
1218 io_subchannel_verify(sch
);
1224 static void io_subchannel_quiesce(struct subchannel
*sch
)
1226 struct ccw_device
*cdev
;
1229 spin_lock_irq(sch
->lock
);
1230 cdev
= sch_get_cdev(sch
);
1231 if (cio_is_console(sch
->schid
))
1233 if (!sch
->schib
.pmcw
.ena
)
1235 ret
= cio_disable_subchannel(sch
);
1239 cdev
->handler(cdev
, cdev
->private->intparm
, ERR_PTR(-EIO
));
1240 while (ret
== -EBUSY
) {
1241 cdev
->private->state
= DEV_STATE_QUIESCE
;
1242 cdev
->private->iretry
= 255;
1243 ret
= ccw_device_cancel_halt_clear(cdev
);
1244 if (ret
== -EBUSY
) {
1245 ccw_device_set_timeout(cdev
, HZ
/10);
1246 spin_unlock_irq(sch
->lock
);
1247 wait_event(cdev
->private->wait_q
,
1248 cdev
->private->state
!= DEV_STATE_QUIESCE
);
1249 spin_lock_irq(sch
->lock
);
1251 ret
= cio_disable_subchannel(sch
);
1254 spin_unlock_irq(sch
->lock
);
1257 static void io_subchannel_shutdown(struct subchannel
*sch
)
1259 io_subchannel_quiesce(sch
);
1262 static int device_is_disconnected(struct ccw_device
*cdev
)
1266 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1267 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1270 static int recovery_check(struct device
*dev
, void *data
)
1272 struct ccw_device
*cdev
= to_ccwdev(dev
);
1275 spin_lock_irq(cdev
->ccwlock
);
1276 switch (cdev
->private->state
) {
1277 case DEV_STATE_DISCONNECTED
:
1278 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1279 cdev
->private->dev_id
.ssid
,
1280 cdev
->private->dev_id
.devno
);
1281 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1284 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1288 spin_unlock_irq(cdev
->ccwlock
);
1293 static void recovery_work_func(struct work_struct
*unused
)
1297 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1299 spin_lock_irq(&recovery_lock
);
1300 if (!timer_pending(&recovery_timer
)) {
1301 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1303 mod_timer(&recovery_timer
, jiffies
+
1304 recovery_delay
[recovery_phase
] * HZ
);
1306 spin_unlock_irq(&recovery_lock
);
1308 CIO_MSG_EVENT(4, "recovery: end\n");
1311 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1313 static void recovery_func(unsigned long data
)
1316 * We can't do our recovery in softirq context and it's not
1317 * performance critical, so we schedule it.
1319 schedule_work(&recovery_work
);
1322 static void ccw_device_schedule_recovery(void)
1324 unsigned long flags
;
1326 CIO_MSG_EVENT(4, "recovery: schedule\n");
1327 spin_lock_irqsave(&recovery_lock
, flags
);
1328 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1330 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1332 spin_unlock_irqrestore(&recovery_lock
, flags
);
1335 static int purge_fn(struct device
*dev
, void *data
)
1337 struct ccw_device
*cdev
= to_ccwdev(dev
);
1338 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
1340 spin_lock_irq(cdev
->ccwlock
);
1341 if (is_blacklisted(id
->ssid
, id
->devno
) &&
1342 (cdev
->private->state
== DEV_STATE_OFFLINE
) &&
1343 (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) == 0)) {
1344 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id
->ssid
,
1346 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1347 atomic_set(&cdev
->private->onoff
, 0);
1349 spin_unlock_irq(cdev
->ccwlock
);
1350 /* Abort loop in case of pending signal. */
1351 if (signal_pending(current
))
1358 * ccw_purge_blacklisted - purge unused, blacklisted devices
1360 * Unregister all ccw devices that are offline and on the blacklist.
1362 int ccw_purge_blacklisted(void)
1364 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1365 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1369 void ccw_device_set_disconnected(struct ccw_device
*cdev
)
1373 ccw_device_set_timeout(cdev
, 0);
1374 cdev
->private->flags
.fake_irb
= 0;
1375 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1377 ccw_device_schedule_recovery();
1380 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1382 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1384 CIO_TRACE_EVENT(2, "notoper");
1385 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1386 ccw_device_set_timeout(cdev
, 0);
1387 cio_disable_subchannel(sch
);
1388 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1391 enum io_sch_action
{
1395 IO_SCH_UNREG_ATTACH
,
1403 static enum io_sch_action
sch_get_action(struct subchannel
*sch
)
1405 struct ccw_device
*cdev
;
1407 cdev
= sch_get_cdev(sch
);
1408 if (cio_update_schib(sch
)) {
1409 /* Not operational. */
1411 return IO_SCH_UNREG
;
1412 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1413 return IO_SCH_UNREG
;
1414 return IO_SCH_ORPH_UNREG
;
1418 return IO_SCH_ATTACH
;
1419 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
1420 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1421 return IO_SCH_UNREG_ATTACH
;
1422 return IO_SCH_ORPH_ATTACH
;
1424 if ((sch
->schib
.pmcw
.pam
& sch
->opm
) == 0) {
1425 if (ccw_device_notify(cdev
, CIO_NO_PATH
) != NOTIFY_OK
)
1426 return IO_SCH_UNREG
;
1429 if (device_is_disconnected(cdev
))
1430 return IO_SCH_REPROBE
;
1431 if (cdev
->online
&& !cdev
->private->flags
.resuming
)
1432 return IO_SCH_VERIFY
;
1433 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
1434 return IO_SCH_UNREG_ATTACH
;
1439 * io_subchannel_sch_event - process subchannel event
1441 * @process: non-zero if function is called in process context
1443 * An unspecified event occurred for this subchannel. Adjust data according
1444 * to the current operational state of the subchannel and device. Return
1445 * zero when the event has been handled sufficiently or -EAGAIN when this
1446 * function should be called again in process context.
1448 static int io_subchannel_sch_event(struct subchannel
*sch
, int process
)
1450 unsigned long flags
;
1451 struct ccw_device
*cdev
;
1452 struct ccw_dev_id dev_id
;
1453 enum io_sch_action action
;
1456 spin_lock_irqsave(sch
->lock
, flags
);
1457 if (!device_is_registered(&sch
->dev
))
1459 if (work_pending(&sch
->todo_work
))
1461 cdev
= sch_get_cdev(sch
);
1462 if (cdev
&& work_pending(&cdev
->private->todo_work
))
1464 action
= sch_get_action(sch
);
1465 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1466 sch
->schid
.ssid
, sch
->schid
.sch_no
, process
,
1468 /* Perform immediate actions while holding the lock. */
1470 case IO_SCH_REPROBE
:
1471 /* Trigger device recognition. */
1472 ccw_device_trigger_reprobe(cdev
);
1476 /* Trigger path verification. */
1477 io_subchannel_verify(sch
);
1481 ccw_device_set_disconnected(cdev
);
1484 case IO_SCH_ORPH_UNREG
:
1485 case IO_SCH_ORPH_ATTACH
:
1486 ccw_device_set_disconnected(cdev
);
1488 case IO_SCH_UNREG_ATTACH
:
1492 if (cdev
->private->state
== DEV_STATE_SENSE_ID
) {
1494 * Note: delayed work triggered by this event
1495 * and repeated calls to sch_event are synchronized
1496 * by the above check for work_pending(cdev).
1498 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1500 ccw_device_set_notoper(cdev
);
1508 spin_unlock_irqrestore(sch
->lock
, flags
);
1509 /* All other actions require process context. */
1512 /* Handle attached ccw device. */
1514 case IO_SCH_ORPH_UNREG
:
1515 case IO_SCH_ORPH_ATTACH
:
1516 /* Move ccw device to orphanage. */
1517 rc
= ccw_device_move_to_orph(cdev
);
1521 case IO_SCH_UNREG_ATTACH
:
1522 spin_lock_irqsave(sch
->lock
, flags
);
1523 if (cdev
->private->flags
.resuming
) {
1524 /* Device will be handled later. */
1528 sch_set_cdev(sch
, NULL
);
1529 spin_unlock_irqrestore(sch
->lock
, flags
);
1530 /* Unregister ccw device. */
1531 ccw_device_unregister(cdev
);
1536 /* Handle subchannel. */
1538 case IO_SCH_ORPH_UNREG
:
1540 if (!cdev
|| !cdev
->private->flags
.resuming
)
1541 css_sch_device_unregister(sch
);
1543 case IO_SCH_ORPH_ATTACH
:
1544 case IO_SCH_UNREG_ATTACH
:
1546 dev_id
.ssid
= sch
->schid
.ssid
;
1547 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1548 cdev
= get_ccwdev_by_dev_id(&dev_id
);
1550 sch_create_and_recog_new_device(sch
);
1553 rc
= ccw_device_move_to_sch(cdev
, sch
);
1555 /* Release reference from get_ccwdev_by_dev_id() */
1556 put_device(&cdev
->dev
);
1559 spin_lock_irqsave(sch
->lock
, flags
);
1560 ccw_device_trigger_reprobe(cdev
);
1561 spin_unlock_irqrestore(sch
->lock
, flags
);
1562 /* Release reference from get_ccwdev_by_dev_id() */
1563 put_device(&cdev
->dev
);
1571 spin_unlock_irqrestore(sch
->lock
, flags
);
1576 #ifdef CONFIG_CCW_CONSOLE
1577 static int ccw_device_console_enable(struct ccw_device
*cdev
,
1578 struct subchannel
*sch
)
1582 io_subchannel_init_fields(sch
);
1583 rc
= cio_commit_config(sch
);
1586 sch
->driver
= &io_subchannel_driver
;
1587 sch_set_cdev(sch
, cdev
);
1588 io_subchannel_recog(cdev
, sch
);
1589 /* Now wait for the async. recognition to come to an end. */
1590 spin_lock_irq(cdev
->ccwlock
);
1591 while (!dev_fsm_final_state(cdev
))
1592 ccw_device_wait_idle(cdev
);
1594 /* Hold on to an extra reference while device is online. */
1595 get_device(&cdev
->dev
);
1596 rc
= ccw_device_online(cdev
);
1600 while (!dev_fsm_final_state(cdev
))
1601 ccw_device_wait_idle(cdev
);
1603 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1608 spin_unlock_irq(cdev
->ccwlock
);
1609 if (rc
) /* Give up online reference since onlining failed. */
1610 put_device(&cdev
->dev
);
1614 struct ccw_device
*ccw_device_probe_console(void)
1616 struct io_subchannel_private
*io_priv
;
1617 struct ccw_device
*cdev
;
1618 struct subchannel
*sch
;
1621 sch
= cio_probe_console();
1623 return ERR_CAST(sch
);
1625 io_priv
= kzalloc(sizeof(*io_priv
), GFP_KERNEL
| GFP_DMA
);
1627 put_device(&sch
->dev
);
1628 return ERR_PTR(-ENOMEM
);
1630 cdev
= io_subchannel_create_ccwdev(sch
);
1632 put_device(&sch
->dev
);
1636 set_io_private(sch
, io_priv
);
1637 ret
= ccw_device_console_enable(cdev
, sch
);
1639 set_io_private(sch
, NULL
);
1640 put_device(&sch
->dev
);
1641 put_device(&cdev
->dev
);
1643 return ERR_PTR(ret
);
1649 * ccw_device_wait_idle() - busy wait for device to become idle
1652 * Poll until activity control is zero, that is, no function or data
1653 * transfer is pending/active.
1654 * Called with device lock being held.
1656 void ccw_device_wait_idle(struct ccw_device
*cdev
)
1658 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1662 if (sch
->schib
.scsw
.cmd
.actl
== 0)
1668 static int ccw_device_pm_restore(struct device
*dev
);
1670 int ccw_device_force_console(struct ccw_device
*cdev
)
1672 return ccw_device_pm_restore(&cdev
->dev
);
1674 EXPORT_SYMBOL_GPL(ccw_device_force_console
);
1678 * get ccw_device matching the busid, but only if owned by cdrv
1681 __ccwdev_check_busid(struct device
*dev
, void *id
)
1687 return (strcmp(bus_id
, dev_name(dev
)) == 0);
1692 * get_ccwdev_by_busid() - obtain device from a bus id
1693 * @cdrv: driver the device is owned by
1694 * @bus_id: bus id of the device to be searched
1696 * This function searches all devices owned by @cdrv for a device with a bus
1697 * id matching @bus_id.
1699 * If a match is found, its reference count of the found device is increased
1700 * and it is returned; else %NULL is returned.
1702 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1707 dev
= driver_find_device(&cdrv
->driver
, NULL
, (void *)bus_id
,
1708 __ccwdev_check_busid
);
1710 return dev
? to_ccwdev(dev
) : NULL
;
1713 /************************** device driver handling ************************/
1715 /* This is the implementation of the ccw_driver class. The probe, remove
1716 * and release methods are initially very similar to the device_driver
1717 * implementations, with the difference that they have ccw_device
1720 * A ccw driver also contains the information that is needed for
1724 ccw_device_probe (struct device
*dev
)
1726 struct ccw_device
*cdev
= to_ccwdev(dev
);
1727 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1730 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1731 /* Note: we interpret class 0 in this context as an uninitialized
1732 * field since it translates to a non-I/O interrupt class. */
1733 if (cdrv
->int_class
!= 0)
1734 cdev
->private->int_class
= cdrv
->int_class
;
1736 cdev
->private->int_class
= IRQIO_CIO
;
1738 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1742 cdev
->private->int_class
= IRQIO_CIO
;
1750 ccw_device_remove (struct device
*dev
)
1752 struct ccw_device
*cdev
= to_ccwdev(dev
);
1753 struct ccw_driver
*cdrv
= cdev
->drv
;
1760 spin_lock_irq(cdev
->ccwlock
);
1761 ret
= ccw_device_offline(cdev
);
1762 spin_unlock_irq(cdev
->ccwlock
);
1764 wait_event(cdev
->private->wait_q
,
1765 dev_fsm_final_state(cdev
));
1767 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1768 "device 0.%x.%04x\n",
1769 ret
, cdev
->private->dev_id
.ssid
,
1770 cdev
->private->dev_id
.devno
);
1771 /* Give up reference obtained in ccw_device_set_online(). */
1772 put_device(&cdev
->dev
);
1774 ccw_device_set_timeout(cdev
, 0);
1776 cdev
->private->int_class
= IRQIO_CIO
;
1780 static void ccw_device_shutdown(struct device
*dev
)
1782 struct ccw_device
*cdev
;
1784 cdev
= to_ccwdev(dev
);
1785 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1786 cdev
->drv
->shutdown(cdev
);
1790 static int ccw_device_pm_prepare(struct device
*dev
)
1792 struct ccw_device
*cdev
= to_ccwdev(dev
);
1794 if (work_pending(&cdev
->private->todo_work
))
1796 /* Fail while device is being set online/offline. */
1797 if (atomic_read(&cdev
->private->onoff
))
1800 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->prepare
)
1801 return cdev
->drv
->prepare(cdev
);
1806 static void ccw_device_pm_complete(struct device
*dev
)
1808 struct ccw_device
*cdev
= to_ccwdev(dev
);
1810 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->complete
)
1811 cdev
->drv
->complete(cdev
);
1814 static int ccw_device_pm_freeze(struct device
*dev
)
1816 struct ccw_device
*cdev
= to_ccwdev(dev
);
1817 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1818 int ret
, cm_enabled
;
1820 /* Fail suspend while device is in transistional state. */
1821 if (!dev_fsm_final_state(cdev
))
1825 if (cdev
->drv
&& cdev
->drv
->freeze
) {
1826 ret
= cdev
->drv
->freeze(cdev
);
1831 spin_lock_irq(sch
->lock
);
1832 cm_enabled
= cdev
->private->cmb
!= NULL
;
1833 spin_unlock_irq(sch
->lock
);
1835 /* Don't have the css write on memory. */
1836 ret
= ccw_set_cmf(cdev
, 0);
1840 /* From here on, disallow device driver I/O. */
1841 spin_lock_irq(sch
->lock
);
1842 ret
= cio_disable_subchannel(sch
);
1843 spin_unlock_irq(sch
->lock
);
1848 static int ccw_device_pm_thaw(struct device
*dev
)
1850 struct ccw_device
*cdev
= to_ccwdev(dev
);
1851 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1852 int ret
, cm_enabled
;
1857 spin_lock_irq(sch
->lock
);
1858 /* Allow device driver I/O again. */
1859 ret
= cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1860 cm_enabled
= cdev
->private->cmb
!= NULL
;
1861 spin_unlock_irq(sch
->lock
);
1866 ret
= ccw_set_cmf(cdev
, 1);
1871 if (cdev
->drv
&& cdev
->drv
->thaw
)
1872 ret
= cdev
->drv
->thaw(cdev
);
1877 static void __ccw_device_pm_restore(struct ccw_device
*cdev
)
1879 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1881 spin_lock_irq(sch
->lock
);
1882 if (cio_is_console(sch
->schid
)) {
1883 cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1887 * While we were sleeping, devices may have gone or become
1888 * available again. Kick re-detection.
1890 cdev
->private->flags
.resuming
= 1;
1891 cdev
->private->path_new_mask
= LPM_ANYPATH
;
1892 css_sched_sch_todo(sch
, SCH_TODO_EVAL
);
1893 spin_unlock_irq(sch
->lock
);
1894 css_wait_for_slow_path();
1896 /* cdev may have been moved to a different subchannel. */
1897 sch
= to_subchannel(cdev
->dev
.parent
);
1898 spin_lock_irq(sch
->lock
);
1899 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
1900 cdev
->private->state
!= DEV_STATE_OFFLINE
)
1903 ccw_device_recognition(cdev
);
1904 spin_unlock_irq(sch
->lock
);
1905 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
) ||
1906 cdev
->private->state
== DEV_STATE_DISCONNECTED
);
1907 spin_lock_irq(sch
->lock
);
1910 cdev
->private->flags
.resuming
= 0;
1911 spin_unlock_irq(sch
->lock
);
1914 static int resume_handle_boxed(struct ccw_device
*cdev
)
1916 cdev
->private->state
= DEV_STATE_BOXED
;
1917 if (ccw_device_notify(cdev
, CIO_BOXED
) == NOTIFY_OK
)
1919 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1923 static int resume_handle_disc(struct ccw_device
*cdev
)
1925 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1926 if (ccw_device_notify(cdev
, CIO_GONE
) == NOTIFY_OK
)
1928 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1932 static int ccw_device_pm_restore(struct device
*dev
)
1934 struct ccw_device
*cdev
= to_ccwdev(dev
);
1935 struct subchannel
*sch
;
1938 __ccw_device_pm_restore(cdev
);
1939 sch
= to_subchannel(cdev
->dev
.parent
);
1940 spin_lock_irq(sch
->lock
);
1941 if (cio_is_console(sch
->schid
))
1944 /* check recognition results */
1945 switch (cdev
->private->state
) {
1946 case DEV_STATE_OFFLINE
:
1947 case DEV_STATE_ONLINE
:
1948 cdev
->private->flags
.donotify
= 0;
1950 case DEV_STATE_BOXED
:
1951 ret
= resume_handle_boxed(cdev
);
1956 ret
= resume_handle_disc(cdev
);
1961 /* check if the device type has changed */
1962 if (!ccw_device_test_sense_data(cdev
)) {
1963 ccw_device_update_sense_data(cdev
);
1964 ccw_device_sched_todo(cdev
, CDEV_TODO_REBIND
);
1971 if (ccw_device_online(cdev
)) {
1972 ret
= resume_handle_disc(cdev
);
1977 spin_unlock_irq(sch
->lock
);
1978 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1979 spin_lock_irq(sch
->lock
);
1981 if (ccw_device_notify(cdev
, CIO_OPER
) == NOTIFY_BAD
) {
1982 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1987 /* reenable cmf, if needed */
1988 if (cdev
->private->cmb
) {
1989 spin_unlock_irq(sch
->lock
);
1990 ret
= ccw_set_cmf(cdev
, 1);
1991 spin_lock_irq(sch
->lock
);
1993 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1994 "(rc=%d)\n", cdev
->private->dev_id
.ssid
,
1995 cdev
->private->dev_id
.devno
, ret
);
2001 spin_unlock_irq(sch
->lock
);
2002 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->restore
)
2003 ret
= cdev
->drv
->restore(cdev
);
2007 spin_unlock_irq(sch
->lock
);
2011 static const struct dev_pm_ops ccw_pm_ops
= {
2012 .prepare
= ccw_device_pm_prepare
,
2013 .complete
= ccw_device_pm_complete
,
2014 .freeze
= ccw_device_pm_freeze
,
2015 .thaw
= ccw_device_pm_thaw
,
2016 .restore
= ccw_device_pm_restore
,
2019 static struct bus_type ccw_bus_type
= {
2021 .match
= ccw_bus_match
,
2022 .uevent
= ccw_uevent
,
2023 .probe
= ccw_device_probe
,
2024 .remove
= ccw_device_remove
,
2025 .shutdown
= ccw_device_shutdown
,
2030 * ccw_driver_register() - register a ccw driver
2031 * @cdriver: driver to be registered
2033 * This function is mainly a wrapper around driver_register().
2035 * %0 on success and a negative error value on failure.
2037 int ccw_driver_register(struct ccw_driver
*cdriver
)
2039 struct device_driver
*drv
= &cdriver
->driver
;
2041 drv
->bus
= &ccw_bus_type
;
2043 return driver_register(drv
);
2047 * ccw_driver_unregister() - deregister a ccw driver
2048 * @cdriver: driver to be deregistered
2050 * This function is mainly a wrapper around driver_unregister().
2052 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
2054 driver_unregister(&cdriver
->driver
);
2057 static void ccw_device_todo(struct work_struct
*work
)
2059 struct ccw_device_private
*priv
;
2060 struct ccw_device
*cdev
;
2061 struct subchannel
*sch
;
2062 enum cdev_todo todo
;
2064 priv
= container_of(work
, struct ccw_device_private
, todo_work
);
2066 sch
= to_subchannel(cdev
->dev
.parent
);
2067 /* Find out todo. */
2068 spin_lock_irq(cdev
->ccwlock
);
2070 priv
->todo
= CDEV_TODO_NOTHING
;
2071 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2072 priv
->dev_id
.ssid
, priv
->dev_id
.devno
, todo
);
2073 spin_unlock_irq(cdev
->ccwlock
);
2076 case CDEV_TODO_ENABLE_CMF
:
2079 case CDEV_TODO_REBIND
:
2080 ccw_device_do_unbind_bind(cdev
);
2082 case CDEV_TODO_REGISTER
:
2083 io_subchannel_register(cdev
);
2085 case CDEV_TODO_UNREG_EVAL
:
2086 if (!sch_is_pseudo_sch(sch
))
2087 css_schedule_eval(sch
->schid
);
2089 case CDEV_TODO_UNREG
:
2090 if (sch_is_pseudo_sch(sch
))
2091 ccw_device_unregister(cdev
);
2093 ccw_device_call_sch_unregister(cdev
);
2098 /* Release workqueue ref. */
2099 put_device(&cdev
->dev
);
2103 * ccw_device_sched_todo - schedule ccw device operation
2107 * Schedule the operation identified by @todo to be performed on the slow path
2108 * workqueue. Do nothing if another operation with higher priority is already
2109 * scheduled. Needs to be called with ccwdev lock held.
2111 void ccw_device_sched_todo(struct ccw_device
*cdev
, enum cdev_todo todo
)
2113 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2114 cdev
->private->dev_id
.ssid
, cdev
->private->dev_id
.devno
,
2116 if (cdev
->private->todo
>= todo
)
2118 cdev
->private->todo
= todo
;
2119 /* Get workqueue ref. */
2120 if (!get_device(&cdev
->dev
))
2122 if (!queue_work(cio_work_q
, &cdev
->private->todo_work
)) {
2123 /* Already queued, release workqueue ref. */
2124 put_device(&cdev
->dev
);
2129 * ccw_device_siosl() - initiate logging
2132 * This function is used to invoke model-dependent logging within the channel
2135 int ccw_device_siosl(struct ccw_device
*cdev
)
2137 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
2139 return chsc_siosl(sch
->schid
);
2141 EXPORT_SYMBOL_GPL(ccw_device_siosl
);
2143 MODULE_LICENSE("GPL");
2144 EXPORT_SYMBOL(ccw_device_set_online
);
2145 EXPORT_SYMBOL(ccw_device_set_offline
);
2146 EXPORT_SYMBOL(ccw_driver_register
);
2147 EXPORT_SYMBOL(ccw_driver_unregister
);
2148 EXPORT_SYMBOL(get_ccwdev_by_busid
);