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)
12 #define KMSG_COMPONENT "cio"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/device.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/kernel_stat.h>
28 #include <asm/ccwdev.h>
30 #include <asm/param.h> /* HZ */
36 #include "cio_debug.h"
41 #include "blacklist.h"
44 static struct timer_list recovery_timer
;
45 static DEFINE_SPINLOCK(recovery_lock
);
46 static int recovery_phase
;
47 static const unsigned long recovery_delay
[] = { 3, 30, 300 };
49 static atomic_t ccw_device_init_count
= ATOMIC_INIT(0);
50 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq
);
51 static struct bus_type ccw_bus_type
;
53 /******************* bus type handling ***********************/
55 /* The Linux driver model distinguishes between a bus type and
56 * the bus itself. Of course we only have one channel
57 * subsystem driver and one channel system per machine, but
58 * we still use the abstraction. T.R. says it's a good idea. */
60 ccw_bus_match (struct device
* dev
, struct device_driver
* drv
)
62 struct ccw_device
*cdev
= to_ccwdev(dev
);
63 struct ccw_driver
*cdrv
= to_ccwdrv(drv
);
64 const struct ccw_device_id
*ids
= cdrv
->ids
, *found
;
69 found
= ccw_device_id_match(ids
, &cdev
->id
);
73 cdev
->id
.driver_info
= found
->driver_info
;
78 /* Store modalias string delimited by prefix/suffix string into buffer with
79 * specified size. Return length of resulting string (excluding trailing '\0')
80 * even if string doesn't fit buffer (snprintf semantics). */
81 static int snprint_alias(char *buf
, size_t size
,
82 struct ccw_device_id
*id
, const char *suffix
)
86 len
= snprintf(buf
, size
, "ccw:t%04Xm%02X", id
->cu_type
, id
->cu_model
);
92 if (id
->dev_type
!= 0)
93 len
+= snprintf(buf
, size
, "dt%04Xdm%02X%s", id
->dev_type
,
94 id
->dev_model
, suffix
);
96 len
+= snprintf(buf
, size
, "dtdm%s", suffix
);
101 /* Set up environment variables for ccw device uevent. Return 0 on success,
102 * non-zero otherwise. */
103 static int ccw_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
105 struct ccw_device
*cdev
= to_ccwdev(dev
);
106 struct ccw_device_id
*id
= &(cdev
->id
);
108 char modalias_buf
[30];
111 ret
= add_uevent_var(env
, "CU_TYPE=%04X", id
->cu_type
);
116 ret
= add_uevent_var(env
, "CU_MODEL=%02X", id
->cu_model
);
120 /* The next two can be zero, that's ok for us */
122 ret
= add_uevent_var(env
, "DEV_TYPE=%04X", id
->dev_type
);
127 ret
= add_uevent_var(env
, "DEV_MODEL=%02X", id
->dev_model
);
132 snprint_alias(modalias_buf
, sizeof(modalias_buf
), id
, "");
133 ret
= add_uevent_var(env
, "MODALIAS=%s", modalias_buf
);
137 static void io_subchannel_irq(struct subchannel
*);
138 static int io_subchannel_probe(struct subchannel
*);
139 static int io_subchannel_remove(struct subchannel
*);
140 static void io_subchannel_shutdown(struct subchannel
*);
141 static int io_subchannel_sch_event(struct subchannel
*, int);
142 static int io_subchannel_chp_event(struct subchannel
*, struct chp_link
*,
144 static void recovery_func(unsigned long data
);
146 static struct css_device_id io_subchannel_ids
[] = {
147 { .match_flags
= 0x1, .type
= SUBCHANNEL_TYPE_IO
, },
148 { /* end of list */ },
151 static int io_subchannel_prepare(struct subchannel
*sch
)
153 struct ccw_device
*cdev
;
155 * Don't allow suspend while a ccw device registration
156 * is still outstanding.
158 cdev
= sch_get_cdev(sch
);
159 if (cdev
&& !device_is_registered(&cdev
->dev
))
164 static int io_subchannel_settle(void)
168 ret
= wait_event_interruptible(ccw_device_init_wq
,
169 atomic_read(&ccw_device_init_count
) == 0);
172 flush_workqueue(cio_work_q
);
176 static struct css_driver io_subchannel_driver
= {
178 .owner
= THIS_MODULE
,
179 .name
= "io_subchannel",
181 .subchannel_type
= io_subchannel_ids
,
182 .irq
= io_subchannel_irq
,
183 .sch_event
= io_subchannel_sch_event
,
184 .chp_event
= io_subchannel_chp_event
,
185 .probe
= io_subchannel_probe
,
186 .remove
= io_subchannel_remove
,
187 .shutdown
= io_subchannel_shutdown
,
188 .prepare
= io_subchannel_prepare
,
189 .settle
= io_subchannel_settle
,
192 int __init
io_subchannel_init(void)
196 setup_timer(&recovery_timer
, recovery_func
, 0);
197 ret
= bus_register(&ccw_bus_type
);
200 ret
= css_driver_register(&io_subchannel_driver
);
202 bus_unregister(&ccw_bus_type
);
208 /************************ device handling **************************/
211 * A ccw_device has some interfaces in sysfs in addition to the
213 * The following entries are designed to export the information which
214 * resided in 2.4 in /proc/subchannels. Subchannel and device number
215 * are obvious, so they don't have an entry :)
216 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
219 chpids_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
221 struct subchannel
*sch
= to_subchannel(dev
);
222 struct chsc_ssd_info
*ssd
= &sch
->ssd_info
;
227 for (chp
= 0; chp
< 8; chp
++) {
229 if (ssd
->path_mask
& mask
)
230 ret
+= sprintf(buf
+ ret
, "%02x ", ssd
->chpid
[chp
].id
);
232 ret
+= sprintf(buf
+ ret
, "00 ");
234 ret
+= sprintf (buf
+ret
, "\n");
235 return min((ssize_t
)PAGE_SIZE
, ret
);
239 pimpampom_show (struct device
* dev
, struct device_attribute
*attr
, char * buf
)
241 struct subchannel
*sch
= to_subchannel(dev
);
242 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
244 return sprintf (buf
, "%02x %02x %02x\n",
245 pmcw
->pim
, pmcw
->pam
, pmcw
->pom
);
249 devtype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
251 struct ccw_device
*cdev
= to_ccwdev(dev
);
252 struct ccw_device_id
*id
= &(cdev
->id
);
254 if (id
->dev_type
!= 0)
255 return sprintf(buf
, "%04x/%02x\n",
256 id
->dev_type
, id
->dev_model
);
258 return sprintf(buf
, "n/a\n");
262 cutype_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
264 struct ccw_device
*cdev
= to_ccwdev(dev
);
265 struct ccw_device_id
*id
= &(cdev
->id
);
267 return sprintf(buf
, "%04x/%02x\n",
268 id
->cu_type
, id
->cu_model
);
272 modalias_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
274 struct ccw_device
*cdev
= to_ccwdev(dev
);
275 struct ccw_device_id
*id
= &(cdev
->id
);
278 len
= snprint_alias(buf
, PAGE_SIZE
, id
, "\n");
280 return len
> PAGE_SIZE
? PAGE_SIZE
: len
;
284 online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
286 struct ccw_device
*cdev
= to_ccwdev(dev
);
288 return sprintf(buf
, cdev
->online
? "1\n" : "0\n");
291 int ccw_device_is_orphan(struct ccw_device
*cdev
)
293 return sch_is_pseudo_sch(to_subchannel(cdev
->dev
.parent
));
296 static void ccw_device_unregister(struct ccw_device
*cdev
)
298 if (device_is_registered(&cdev
->dev
)) {
299 /* Undo device_add(). */
300 device_del(&cdev
->dev
);
302 if (cdev
->private->flags
.initialized
) {
303 cdev
->private->flags
.initialized
= 0;
304 /* Release reference from device_initialize(). */
305 put_device(&cdev
->dev
);
309 static void io_subchannel_quiesce(struct subchannel
*);
312 * ccw_device_set_offline() - disable a ccw device for I/O
313 * @cdev: target ccw device
315 * This function calls the driver's set_offline() function for @cdev, if
316 * given, and then disables @cdev.
318 * %0 on success and a negative error value on failure.
320 * enabled, ccw device lock not held
322 int ccw_device_set_offline(struct ccw_device
*cdev
)
324 struct subchannel
*sch
;
329 if (!cdev
->online
|| !cdev
->drv
)
332 if (cdev
->drv
->set_offline
) {
333 ret
= cdev
->drv
->set_offline(cdev
);
337 spin_lock_irq(cdev
->ccwlock
);
338 sch
= to_subchannel(cdev
->dev
.parent
);
340 /* Wait until a final state or DISCONNECTED is reached */
341 while (!dev_fsm_final_state(cdev
) &&
342 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
343 spin_unlock_irq(cdev
->ccwlock
);
344 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
345 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
346 spin_lock_irq(cdev
->ccwlock
);
349 ret
= ccw_device_offline(cdev
);
352 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
353 "0.%x.%04x\n", ret
, cdev
->private->dev_id
.ssid
,
354 cdev
->private->dev_id
.devno
);
357 state
= cdev
->private->state
;
358 spin_unlock_irq(cdev
->ccwlock
);
359 io_subchannel_quiesce(sch
);
360 spin_lock_irq(cdev
->ccwlock
);
361 cdev
->private->state
= state
;
362 } while (ret
== -EBUSY
);
363 spin_unlock_irq(cdev
->ccwlock
);
364 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
365 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
366 /* Inform the user if set offline failed. */
367 if (cdev
->private->state
== DEV_STATE_BOXED
) {
368 pr_warn("%s: The device entered boxed state while being set offline\n",
369 dev_name(&cdev
->dev
));
370 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
371 pr_warn("%s: The device stopped operating while being set offline\n",
372 dev_name(&cdev
->dev
));
374 /* Give up reference from ccw_device_set_online(). */
375 put_device(&cdev
->dev
);
379 cdev
->private->state
= DEV_STATE_OFFLINE
;
380 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
381 spin_unlock_irq(cdev
->ccwlock
);
382 /* Give up reference from ccw_device_set_online(). */
383 put_device(&cdev
->dev
);
388 * ccw_device_set_online() - enable a ccw device for I/O
389 * @cdev: target ccw device
391 * This function first enables @cdev and then calls the driver's set_online()
392 * function for @cdev, if given. If set_online() returns an error, @cdev is
395 * %0 on success and a negative error value on failure.
397 * enabled, ccw device lock not held
399 int ccw_device_set_online(struct ccw_device
*cdev
)
406 if (cdev
->online
|| !cdev
->drv
)
408 /* Hold on to an extra reference while device is online. */
409 if (!get_device(&cdev
->dev
))
412 spin_lock_irq(cdev
->ccwlock
);
413 ret
= ccw_device_online(cdev
);
414 spin_unlock_irq(cdev
->ccwlock
);
416 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
418 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
419 "device 0.%x.%04x\n",
420 ret
, cdev
->private->dev_id
.ssid
,
421 cdev
->private->dev_id
.devno
);
422 /* Give up online reference since onlining failed. */
423 put_device(&cdev
->dev
);
426 spin_lock_irq(cdev
->ccwlock
);
427 /* Check if online processing was successful */
428 if ((cdev
->private->state
!= DEV_STATE_ONLINE
) &&
429 (cdev
->private->state
!= DEV_STATE_W4SENSE
)) {
430 spin_unlock_irq(cdev
->ccwlock
);
431 /* Inform the user that set online failed. */
432 if (cdev
->private->state
== DEV_STATE_BOXED
) {
433 pr_warn("%s: Setting the device online failed because it is boxed\n",
434 dev_name(&cdev
->dev
));
435 } else if (cdev
->private->state
== DEV_STATE_NOT_OPER
) {
436 pr_warn("%s: Setting the device online failed because it is not operational\n",
437 dev_name(&cdev
->dev
));
439 /* Give up online reference since onlining failed. */
440 put_device(&cdev
->dev
);
443 spin_unlock_irq(cdev
->ccwlock
);
444 if (cdev
->drv
->set_online
)
445 ret
= cdev
->drv
->set_online(cdev
);
449 spin_lock_irq(cdev
->ccwlock
);
451 spin_unlock_irq(cdev
->ccwlock
);
455 spin_lock_irq(cdev
->ccwlock
);
456 /* Wait until a final state or DISCONNECTED is reached */
457 while (!dev_fsm_final_state(cdev
) &&
458 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
459 spin_unlock_irq(cdev
->ccwlock
);
460 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
461 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
462 spin_lock_irq(cdev
->ccwlock
);
464 ret2
= ccw_device_offline(cdev
);
467 spin_unlock_irq(cdev
->ccwlock
);
468 wait_event(cdev
->private->wait_q
, (dev_fsm_final_state(cdev
) ||
469 cdev
->private->state
== DEV_STATE_DISCONNECTED
));
470 /* Give up online reference since onlining failed. */
471 put_device(&cdev
->dev
);
475 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
476 "device 0.%x.%04x\n",
477 ret2
, cdev
->private->dev_id
.ssid
,
478 cdev
->private->dev_id
.devno
);
479 cdev
->private->state
= DEV_STATE_OFFLINE
;
480 spin_unlock_irq(cdev
->ccwlock
);
481 /* Give up online reference since onlining failed. */
482 put_device(&cdev
->dev
);
486 static int online_store_handle_offline(struct ccw_device
*cdev
)
488 if (cdev
->private->state
== DEV_STATE_DISCONNECTED
) {
489 spin_lock_irq(cdev
->ccwlock
);
490 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG_EVAL
);
491 spin_unlock_irq(cdev
->ccwlock
);
494 if (cdev
->drv
&& cdev
->drv
->set_offline
)
495 return ccw_device_set_offline(cdev
);
499 static int online_store_recog_and_online(struct ccw_device
*cdev
)
501 /* Do device recognition, if needed. */
502 if (cdev
->private->state
== DEV_STATE_BOXED
) {
503 spin_lock_irq(cdev
->ccwlock
);
504 ccw_device_recognition(cdev
);
505 spin_unlock_irq(cdev
->ccwlock
);
506 wait_event(cdev
->private->wait_q
,
507 cdev
->private->flags
.recog_done
);
508 if (cdev
->private->state
!= DEV_STATE_OFFLINE
)
509 /* recognition failed */
512 if (cdev
->drv
&& cdev
->drv
->set_online
)
513 return ccw_device_set_online(cdev
);
517 static int online_store_handle_online(struct ccw_device
*cdev
, int force
)
521 ret
= online_store_recog_and_online(cdev
);
524 if (force
&& cdev
->private->state
== DEV_STATE_BOXED
) {
525 ret
= ccw_device_stlck(cdev
);
528 if (cdev
->id
.cu_type
== 0)
529 cdev
->private->state
= DEV_STATE_NOT_OPER
;
530 ret
= online_store_recog_and_online(cdev
);
537 static ssize_t
online_store (struct device
*dev
, struct device_attribute
*attr
,
538 const char *buf
, size_t count
)
540 struct ccw_device
*cdev
= to_ccwdev(dev
);
544 /* Prevent conflict between multiple on-/offline processing requests. */
545 if (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) != 0)
547 /* Prevent conflict between internal I/Os and on-/offline processing. */
548 if (!dev_fsm_final_state(cdev
) &&
549 cdev
->private->state
!= DEV_STATE_DISCONNECTED
) {
553 /* Prevent conflict between pending work and on-/offline processing.*/
554 if (work_pending(&cdev
->private->todo_work
)) {
558 if (!strncmp(buf
, "force\n", count
)) {
564 ret
= kstrtoul(buf
, 16, &i
);
572 ret
= online_store_handle_offline(cdev
);
575 ret
= online_store_handle_online(cdev
, force
);
583 atomic_set(&cdev
->private->onoff
, 0);
584 return (ret
< 0) ? ret
: count
;
588 available_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
590 struct ccw_device
*cdev
= to_ccwdev(dev
);
591 struct subchannel
*sch
;
593 if (ccw_device_is_orphan(cdev
))
594 return sprintf(buf
, "no device\n");
595 switch (cdev
->private->state
) {
596 case DEV_STATE_BOXED
:
597 return sprintf(buf
, "boxed\n");
598 case DEV_STATE_DISCONNECTED
:
599 case DEV_STATE_DISCONNECTED_SENSE_ID
:
600 case DEV_STATE_NOT_OPER
:
601 sch
= to_subchannel(dev
->parent
);
603 return sprintf(buf
, "no path\n");
605 return sprintf(buf
, "no device\n");
607 /* All other states considered fine. */
608 return sprintf(buf
, "good\n");
613 initiate_logging(struct device
*dev
, struct device_attribute
*attr
,
614 const char *buf
, size_t count
)
616 struct subchannel
*sch
= to_subchannel(dev
);
619 rc
= chsc_siosl(sch
->schid
);
621 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
622 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
625 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
626 sch
->schid
.ssid
, sch
->schid
.sch_no
);
630 static ssize_t
vpm_show(struct device
*dev
, struct device_attribute
*attr
,
633 struct subchannel
*sch
= to_subchannel(dev
);
635 return sprintf(buf
, "%02x\n", sch
->vpm
);
638 static DEVICE_ATTR(chpids
, 0444, chpids_show
, NULL
);
639 static DEVICE_ATTR(pimpampom
, 0444, pimpampom_show
, NULL
);
640 static DEVICE_ATTR(devtype
, 0444, devtype_show
, NULL
);
641 static DEVICE_ATTR(cutype
, 0444, cutype_show
, NULL
);
642 static DEVICE_ATTR(modalias
, 0444, modalias_show
, NULL
);
643 static DEVICE_ATTR(online
, 0644, online_show
, online_store
);
644 static DEVICE_ATTR(availability
, 0444, available_show
, NULL
);
645 static DEVICE_ATTR(logging
, 0200, NULL
, initiate_logging
);
646 static DEVICE_ATTR(vpm
, 0444, vpm_show
, NULL
);
648 static struct attribute
*io_subchannel_attrs
[] = {
649 &dev_attr_chpids
.attr
,
650 &dev_attr_pimpampom
.attr
,
651 &dev_attr_logging
.attr
,
656 static struct attribute_group io_subchannel_attr_group
= {
657 .attrs
= io_subchannel_attrs
,
660 static struct attribute
* ccwdev_attrs
[] = {
661 &dev_attr_devtype
.attr
,
662 &dev_attr_cutype
.attr
,
663 &dev_attr_modalias
.attr
,
664 &dev_attr_online
.attr
,
665 &dev_attr_cmb_enable
.attr
,
666 &dev_attr_availability
.attr
,
670 static struct attribute_group ccwdev_attr_group
= {
671 .attrs
= ccwdev_attrs
,
674 static const struct attribute_group
*ccwdev_attr_groups
[] = {
679 static int ccw_device_add(struct ccw_device
*cdev
)
681 struct device
*dev
= &cdev
->dev
;
683 dev
->bus
= &ccw_bus_type
;
684 return device_add(dev
);
687 static int match_dev_id(struct device
*dev
, void *data
)
689 struct ccw_device
*cdev
= to_ccwdev(dev
);
690 struct ccw_dev_id
*dev_id
= data
;
692 return ccw_dev_id_is_equal(&cdev
->private->dev_id
, dev_id
);
696 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
697 * @dev_id: id of the device to be searched
699 * This function searches all devices attached to the ccw bus for a device
702 * If a device is found its reference count is increased and returned;
703 * else %NULL is returned.
705 struct ccw_device
*get_ccwdev_by_dev_id(struct ccw_dev_id
*dev_id
)
709 dev
= bus_find_device(&ccw_bus_type
, NULL
, dev_id
, match_dev_id
);
711 return dev
? to_ccwdev(dev
) : NULL
;
713 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id
);
715 static void ccw_device_do_unbind_bind(struct ccw_device
*cdev
)
719 if (device_is_registered(&cdev
->dev
)) {
720 device_release_driver(&cdev
->dev
);
721 ret
= device_attach(&cdev
->dev
);
722 WARN_ON(ret
== -ENODEV
);
727 ccw_device_release(struct device
*dev
)
729 struct ccw_device
*cdev
;
731 cdev
= to_ccwdev(dev
);
732 /* Release reference of parent subchannel. */
733 put_device(cdev
->dev
.parent
);
734 kfree(cdev
->private);
738 static struct ccw_device
* io_subchannel_allocate_dev(struct subchannel
*sch
)
740 struct ccw_device
*cdev
;
742 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
744 cdev
->private = kzalloc(sizeof(struct ccw_device_private
),
745 GFP_KERNEL
| GFP_DMA
);
750 return ERR_PTR(-ENOMEM
);
753 static void ccw_device_todo(struct work_struct
*work
);
755 static int io_subchannel_initialize_dev(struct subchannel
*sch
,
756 struct ccw_device
*cdev
)
758 struct ccw_device_private
*priv
= cdev
->private;
762 priv
->int_class
= IRQIO_CIO
;
763 priv
->state
= DEV_STATE_NOT_OPER
;
764 priv
->dev_id
.devno
= sch
->schib
.pmcw
.dev
;
765 priv
->dev_id
.ssid
= sch
->schid
.ssid
;
767 INIT_WORK(&priv
->todo_work
, ccw_device_todo
);
768 INIT_LIST_HEAD(&priv
->cmb_list
);
769 init_waitqueue_head(&priv
->wait_q
);
770 init_timer(&priv
->timer
);
772 atomic_set(&priv
->onoff
, 0);
773 cdev
->ccwlock
= sch
->lock
;
774 cdev
->dev
.parent
= &sch
->dev
;
775 cdev
->dev
.release
= ccw_device_release
;
776 cdev
->dev
.groups
= ccwdev_attr_groups
;
777 /* Do first half of device_register. */
778 device_initialize(&cdev
->dev
);
779 ret
= dev_set_name(&cdev
->dev
, "0.%x.%04x", cdev
->private->dev_id
.ssid
,
780 cdev
->private->dev_id
.devno
);
783 if (!get_device(&sch
->dev
)) {
787 priv
->flags
.initialized
= 1;
788 spin_lock_irq(sch
->lock
);
789 sch_set_cdev(sch
, cdev
);
790 spin_unlock_irq(sch
->lock
);
794 /* Release reference from device_initialize(). */
795 put_device(&cdev
->dev
);
799 static struct ccw_device
* io_subchannel_create_ccwdev(struct subchannel
*sch
)
801 struct ccw_device
*cdev
;
804 cdev
= io_subchannel_allocate_dev(sch
);
806 ret
= io_subchannel_initialize_dev(sch
, cdev
);
813 static void io_subchannel_recog(struct ccw_device
*, struct subchannel
*);
815 static void sch_create_and_recog_new_device(struct subchannel
*sch
)
817 struct ccw_device
*cdev
;
819 /* Need to allocate a new ccw device. */
820 cdev
= io_subchannel_create_ccwdev(sch
);
822 /* OK, we did everything we could... */
823 css_sch_device_unregister(sch
);
826 /* Start recognition for the new ccw device. */
827 io_subchannel_recog(cdev
, sch
);
831 * Register recognized device.
833 static void io_subchannel_register(struct ccw_device
*cdev
)
835 struct subchannel
*sch
;
836 int ret
, adjust_init_count
= 1;
839 sch
= to_subchannel(cdev
->dev
.parent
);
841 * Check if subchannel is still registered. It may have become
842 * unregistered if a machine check hit us after finishing
843 * device recognition but before the register work could be
846 if (!device_is_registered(&sch
->dev
))
848 css_update_ssd_info(sch
);
850 * io_subchannel_register() will also be called after device
851 * recognition has been done for a boxed device (which will already
852 * be registered). We need to reprobe since we may now have sense id
855 if (device_is_registered(&cdev
->dev
)) {
857 ret
= device_reprobe(&cdev
->dev
);
859 /* We can't do much here. */
860 CIO_MSG_EVENT(0, "device_reprobe() returned"
861 " %d for 0.%x.%04x\n", ret
,
862 cdev
->private->dev_id
.ssid
,
863 cdev
->private->dev_id
.devno
);
865 adjust_init_count
= 0;
869 * Now we know this subchannel will stay, we can throw
870 * our delayed uevent.
872 dev_set_uevent_suppress(&sch
->dev
, 0);
873 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
874 /* make it known to the system */
875 ret
= ccw_device_add(cdev
);
877 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
878 cdev
->private->dev_id
.ssid
,
879 cdev
->private->dev_id
.devno
, ret
);
880 spin_lock_irqsave(sch
->lock
, flags
);
881 sch_set_cdev(sch
, NULL
);
882 spin_unlock_irqrestore(sch
->lock
, flags
);
883 /* Release initial device reference. */
884 put_device(&cdev
->dev
);
888 cdev
->private->flags
.recog_done
= 1;
889 wake_up(&cdev
->private->wait_q
);
891 if (adjust_init_count
&& atomic_dec_and_test(&ccw_device_init_count
))
892 wake_up(&ccw_device_init_wq
);
895 static void ccw_device_call_sch_unregister(struct ccw_device
*cdev
)
897 struct subchannel
*sch
;
899 /* Get subchannel reference for local processing. */
900 if (!get_device(cdev
->dev
.parent
))
902 sch
= to_subchannel(cdev
->dev
.parent
);
903 css_sch_device_unregister(sch
);
904 /* Release subchannel reference for local processing. */
905 put_device(&sch
->dev
);
909 * subchannel recognition done. Called from the state machine.
912 io_subchannel_recog_done(struct ccw_device
*cdev
)
914 if (css_init_done
== 0) {
915 cdev
->private->flags
.recog_done
= 1;
918 switch (cdev
->private->state
) {
919 case DEV_STATE_BOXED
:
920 /* Device did not respond in time. */
921 case DEV_STATE_NOT_OPER
:
922 cdev
->private->flags
.recog_done
= 1;
923 /* Remove device found not operational. */
924 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
925 if (atomic_dec_and_test(&ccw_device_init_count
))
926 wake_up(&ccw_device_init_wq
);
928 case DEV_STATE_OFFLINE
:
930 * We can't register the device in interrupt context so
931 * we schedule a work item.
933 ccw_device_sched_todo(cdev
, CDEV_TODO_REGISTER
);
938 static void io_subchannel_recog(struct ccw_device
*cdev
, struct subchannel
*sch
)
940 /* Increase counter of devices currently in recognition. */
941 atomic_inc(&ccw_device_init_count
);
943 /* Start async. device sensing. */
944 spin_lock_irq(sch
->lock
);
945 ccw_device_recognition(cdev
);
946 spin_unlock_irq(sch
->lock
);
949 static int ccw_device_move_to_sch(struct ccw_device
*cdev
,
950 struct subchannel
*sch
)
952 struct subchannel
*old_sch
;
953 int rc
, old_enabled
= 0;
955 old_sch
= to_subchannel(cdev
->dev
.parent
);
956 /* Obtain child reference for new parent. */
957 if (!get_device(&sch
->dev
))
960 if (!sch_is_pseudo_sch(old_sch
)) {
961 spin_lock_irq(old_sch
->lock
);
962 old_enabled
= old_sch
->schib
.pmcw
.ena
;
965 rc
= cio_disable_subchannel(old_sch
);
966 spin_unlock_irq(old_sch
->lock
);
968 /* Release child reference for new parent. */
969 put_device(&sch
->dev
);
974 mutex_lock(&sch
->reg_mutex
);
975 rc
= device_move(&cdev
->dev
, &sch
->dev
, DPM_ORDER_PARENT_BEFORE_DEV
);
976 mutex_unlock(&sch
->reg_mutex
);
978 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
979 cdev
->private->dev_id
.ssid
,
980 cdev
->private->dev_id
.devno
, sch
->schid
.ssid
,
981 sch
->schib
.pmcw
.dev
, rc
);
983 /* Try to reenable the old subchannel. */
984 spin_lock_irq(old_sch
->lock
);
985 cio_enable_subchannel(old_sch
, (u32
)(addr_t
)old_sch
);
986 spin_unlock_irq(old_sch
->lock
);
988 /* Release child reference for new parent. */
989 put_device(&sch
->dev
);
992 /* Clean up old subchannel. */
993 if (!sch_is_pseudo_sch(old_sch
)) {
994 spin_lock_irq(old_sch
->lock
);
995 sch_set_cdev(old_sch
, NULL
);
996 spin_unlock_irq(old_sch
->lock
);
997 css_schedule_eval(old_sch
->schid
);
999 /* Release child reference for old parent. */
1000 put_device(&old_sch
->dev
);
1001 /* Initialize new subchannel. */
1002 spin_lock_irq(sch
->lock
);
1003 cdev
->ccwlock
= sch
->lock
;
1004 if (!sch_is_pseudo_sch(sch
))
1005 sch_set_cdev(sch
, cdev
);
1006 spin_unlock_irq(sch
->lock
);
1007 if (!sch_is_pseudo_sch(sch
))
1008 css_update_ssd_info(sch
);
1012 static int ccw_device_move_to_orph(struct ccw_device
*cdev
)
1014 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1015 struct channel_subsystem
*css
= to_css(sch
->dev
.parent
);
1017 return ccw_device_move_to_sch(cdev
, css
->pseudo_subchannel
);
1020 static void io_subchannel_irq(struct subchannel
*sch
)
1022 struct ccw_device
*cdev
;
1024 cdev
= sch_get_cdev(sch
);
1026 CIO_TRACE_EVENT(6, "IRQ");
1027 CIO_TRACE_EVENT(6, dev_name(&sch
->dev
));
1029 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1031 inc_irq_stat(IRQIO_CIO
);
1034 void io_subchannel_init_config(struct subchannel
*sch
)
1036 memset(&sch
->config
, 0, sizeof(sch
->config
));
1037 sch
->config
.csense
= 1;
1040 static void io_subchannel_init_fields(struct subchannel
*sch
)
1042 if (cio_is_console(sch
->schid
))
1045 sch
->opm
= chp_get_sch_opm(sch
);
1046 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1047 sch
->isc
= cio_is_console(sch
->schid
) ? CONSOLE_ISC
: IO_SCH_ISC
;
1049 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1050 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1051 sch
->schib
.pmcw
.dev
, sch
->schid
.ssid
,
1052 sch
->schid
.sch_no
, sch
->schib
.pmcw
.pim
,
1053 sch
->schib
.pmcw
.pam
, sch
->schib
.pmcw
.pom
);
1055 io_subchannel_init_config(sch
);
1059 * Note: We always return 0 so that we bind to the device even on error.
1060 * This is needed so that our remove function is called on unregister.
1062 static int io_subchannel_probe(struct subchannel
*sch
)
1064 struct io_subchannel_private
*io_priv
;
1065 struct ccw_device
*cdev
;
1068 if (cio_is_console(sch
->schid
)) {
1069 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1070 &io_subchannel_attr_group
);
1072 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1073 "attributes for subchannel "
1074 "0.%x.%04x (rc=%d)\n",
1075 sch
->schid
.ssid
, sch
->schid
.sch_no
, rc
);
1077 * The console subchannel already has an associated ccw_device.
1078 * Throw the delayed uevent for the subchannel, register
1079 * the ccw_device and exit.
1081 dev_set_uevent_suppress(&sch
->dev
, 0);
1082 kobject_uevent(&sch
->dev
.kobj
, KOBJ_ADD
);
1083 cdev
= sch_get_cdev(sch
);
1084 rc
= ccw_device_add(cdev
);
1086 /* Release online reference. */
1087 put_device(&cdev
->dev
);
1090 if (atomic_dec_and_test(&ccw_device_init_count
))
1091 wake_up(&ccw_device_init_wq
);
1094 io_subchannel_init_fields(sch
);
1095 rc
= cio_commit_config(sch
);
1098 rc
= sysfs_create_group(&sch
->dev
.kobj
,
1099 &io_subchannel_attr_group
);
1102 /* Allocate I/O subchannel private data. */
1103 io_priv
= kzalloc(sizeof(*io_priv
), GFP_KERNEL
| GFP_DMA
);
1107 set_io_private(sch
, io_priv
);
1108 css_schedule_eval(sch
->schid
);
1112 spin_lock_irq(sch
->lock
);
1113 css_sched_sch_todo(sch
, SCH_TODO_UNREG
);
1114 spin_unlock_irq(sch
->lock
);
1119 io_subchannel_remove (struct subchannel
*sch
)
1121 struct io_subchannel_private
*io_priv
= to_io_private(sch
);
1122 struct ccw_device
*cdev
;
1124 cdev
= sch_get_cdev(sch
);
1127 io_subchannel_quiesce(sch
);
1128 /* Set ccw device to not operational and drop reference. */
1129 spin_lock_irq(cdev
->ccwlock
);
1130 sch_set_cdev(sch
, NULL
);
1131 set_io_private(sch
, NULL
);
1132 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1133 spin_unlock_irq(cdev
->ccwlock
);
1134 ccw_device_unregister(cdev
);
1137 sysfs_remove_group(&sch
->dev
.kobj
, &io_subchannel_attr_group
);
1141 static void io_subchannel_verify(struct subchannel
*sch
)
1143 struct ccw_device
*cdev
;
1145 cdev
= sch_get_cdev(sch
);
1147 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1150 static void io_subchannel_terminate_path(struct subchannel
*sch
, u8 mask
)
1152 struct ccw_device
*cdev
;
1154 cdev
= sch_get_cdev(sch
);
1157 if (cio_update_schib(sch
))
1159 /* Check for I/O on path. */
1160 if (scsw_actl(&sch
->schib
.scsw
) == 0 || sch
->schib
.pmcw
.lpum
!= mask
)
1162 if (cdev
->private->state
== DEV_STATE_ONLINE
) {
1163 ccw_device_kill_io(cdev
);
1169 /* Trigger path verification. */
1170 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1174 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1177 static int io_subchannel_chp_event(struct subchannel
*sch
,
1178 struct chp_link
*link
, int event
)
1180 struct ccw_device
*cdev
= sch_get_cdev(sch
);
1183 mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
1191 cdev
->private->path_gone_mask
|= mask
;
1192 io_subchannel_terminate_path(sch
, mask
);
1198 cdev
->private->path_new_mask
|= mask
;
1199 io_subchannel_verify(sch
);
1202 if (cio_update_schib(sch
))
1205 cdev
->private->path_gone_mask
|= mask
;
1206 io_subchannel_terminate_path(sch
, mask
);
1209 if (cio_update_schib(sch
))
1211 sch
->lpm
|= mask
& sch
->opm
;
1213 cdev
->private->path_new_mask
|= mask
;
1214 io_subchannel_verify(sch
);
1220 static void io_subchannel_quiesce(struct subchannel
*sch
)
1222 struct ccw_device
*cdev
;
1225 spin_lock_irq(sch
->lock
);
1226 cdev
= sch_get_cdev(sch
);
1227 if (cio_is_console(sch
->schid
))
1229 if (!sch
->schib
.pmcw
.ena
)
1231 ret
= cio_disable_subchannel(sch
);
1235 cdev
->handler(cdev
, cdev
->private->intparm
, ERR_PTR(-EIO
));
1236 while (ret
== -EBUSY
) {
1237 cdev
->private->state
= DEV_STATE_QUIESCE
;
1238 cdev
->private->iretry
= 255;
1239 ret
= ccw_device_cancel_halt_clear(cdev
);
1240 if (ret
== -EBUSY
) {
1241 ccw_device_set_timeout(cdev
, HZ
/10);
1242 spin_unlock_irq(sch
->lock
);
1243 wait_event(cdev
->private->wait_q
,
1244 cdev
->private->state
!= DEV_STATE_QUIESCE
);
1245 spin_lock_irq(sch
->lock
);
1247 ret
= cio_disable_subchannel(sch
);
1250 spin_unlock_irq(sch
->lock
);
1253 static void io_subchannel_shutdown(struct subchannel
*sch
)
1255 io_subchannel_quiesce(sch
);
1258 static int device_is_disconnected(struct ccw_device
*cdev
)
1262 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
1263 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
1266 static int recovery_check(struct device
*dev
, void *data
)
1268 struct ccw_device
*cdev
= to_ccwdev(dev
);
1271 spin_lock_irq(cdev
->ccwlock
);
1272 switch (cdev
->private->state
) {
1273 case DEV_STATE_DISCONNECTED
:
1274 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1275 cdev
->private->dev_id
.ssid
,
1276 cdev
->private->dev_id
.devno
);
1277 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
1280 case DEV_STATE_DISCONNECTED_SENSE_ID
:
1284 spin_unlock_irq(cdev
->ccwlock
);
1289 static void recovery_work_func(struct work_struct
*unused
)
1293 bus_for_each_dev(&ccw_bus_type
, NULL
, &redo
, recovery_check
);
1295 spin_lock_irq(&recovery_lock
);
1296 if (!timer_pending(&recovery_timer
)) {
1297 if (recovery_phase
< ARRAY_SIZE(recovery_delay
) - 1)
1299 mod_timer(&recovery_timer
, jiffies
+
1300 recovery_delay
[recovery_phase
] * HZ
);
1302 spin_unlock_irq(&recovery_lock
);
1304 CIO_MSG_EVENT(4, "recovery: end\n");
1307 static DECLARE_WORK(recovery_work
, recovery_work_func
);
1309 static void recovery_func(unsigned long data
)
1312 * We can't do our recovery in softirq context and it's not
1313 * performance critical, so we schedule it.
1315 schedule_work(&recovery_work
);
1318 static void ccw_device_schedule_recovery(void)
1320 unsigned long flags
;
1322 CIO_MSG_EVENT(4, "recovery: schedule\n");
1323 spin_lock_irqsave(&recovery_lock
, flags
);
1324 if (!timer_pending(&recovery_timer
) || (recovery_phase
!= 0)) {
1326 mod_timer(&recovery_timer
, jiffies
+ recovery_delay
[0] * HZ
);
1328 spin_unlock_irqrestore(&recovery_lock
, flags
);
1331 static int purge_fn(struct device
*dev
, void *data
)
1333 struct ccw_device
*cdev
= to_ccwdev(dev
);
1334 struct ccw_dev_id
*id
= &cdev
->private->dev_id
;
1336 spin_lock_irq(cdev
->ccwlock
);
1337 if (is_blacklisted(id
->ssid
, id
->devno
) &&
1338 (cdev
->private->state
== DEV_STATE_OFFLINE
) &&
1339 (atomic_cmpxchg(&cdev
->private->onoff
, 0, 1) == 0)) {
1340 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id
->ssid
,
1342 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1343 atomic_set(&cdev
->private->onoff
, 0);
1345 spin_unlock_irq(cdev
->ccwlock
);
1346 /* Abort loop in case of pending signal. */
1347 if (signal_pending(current
))
1354 * ccw_purge_blacklisted - purge unused, blacklisted devices
1356 * Unregister all ccw devices that are offline and on the blacklist.
1358 int ccw_purge_blacklisted(void)
1360 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1361 bus_for_each_dev(&ccw_bus_type
, NULL
, NULL
, purge_fn
);
1365 void ccw_device_set_disconnected(struct ccw_device
*cdev
)
1369 ccw_device_set_timeout(cdev
, 0);
1370 cdev
->private->flags
.fake_irb
= 0;
1371 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1373 ccw_device_schedule_recovery();
1376 void ccw_device_set_notoper(struct ccw_device
*cdev
)
1378 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1380 CIO_TRACE_EVENT(2, "notoper");
1381 CIO_TRACE_EVENT(2, dev_name(&sch
->dev
));
1382 ccw_device_set_timeout(cdev
, 0);
1383 cio_disable_subchannel(sch
);
1384 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1387 enum io_sch_action
{
1391 IO_SCH_UNREG_ATTACH
,
1399 static enum io_sch_action
sch_get_action(struct subchannel
*sch
)
1401 struct ccw_device
*cdev
;
1403 cdev
= sch_get_cdev(sch
);
1404 if (cio_update_schib(sch
)) {
1405 /* Not operational. */
1407 return IO_SCH_UNREG
;
1408 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1409 return IO_SCH_UNREG
;
1410 return IO_SCH_ORPH_UNREG
;
1414 return IO_SCH_ATTACH
;
1415 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
1416 if (ccw_device_notify(cdev
, CIO_GONE
) != NOTIFY_OK
)
1417 return IO_SCH_UNREG_ATTACH
;
1418 return IO_SCH_ORPH_ATTACH
;
1420 if ((sch
->schib
.pmcw
.pam
& sch
->opm
) == 0) {
1421 if (ccw_device_notify(cdev
, CIO_NO_PATH
) != NOTIFY_OK
)
1422 return IO_SCH_UNREG
;
1425 if (device_is_disconnected(cdev
))
1426 return IO_SCH_REPROBE
;
1427 if (cdev
->online
&& !cdev
->private->flags
.resuming
)
1428 return IO_SCH_VERIFY
;
1429 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
1430 return IO_SCH_UNREG_ATTACH
;
1435 * io_subchannel_sch_event - process subchannel event
1437 * @process: non-zero if function is called in process context
1439 * An unspecified event occurred for this subchannel. Adjust data according
1440 * to the current operational state of the subchannel and device. Return
1441 * zero when the event has been handled sufficiently or -EAGAIN when this
1442 * function should be called again in process context.
1444 static int io_subchannel_sch_event(struct subchannel
*sch
, int process
)
1446 unsigned long flags
;
1447 struct ccw_device
*cdev
;
1448 struct ccw_dev_id dev_id
;
1449 enum io_sch_action action
;
1452 spin_lock_irqsave(sch
->lock
, flags
);
1453 if (!device_is_registered(&sch
->dev
))
1455 if (work_pending(&sch
->todo_work
))
1457 cdev
= sch_get_cdev(sch
);
1458 if (cdev
&& work_pending(&cdev
->private->todo_work
))
1460 action
= sch_get_action(sch
);
1461 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1462 sch
->schid
.ssid
, sch
->schid
.sch_no
, process
,
1464 /* Perform immediate actions while holding the lock. */
1466 case IO_SCH_REPROBE
:
1467 /* Trigger device recognition. */
1468 ccw_device_trigger_reprobe(cdev
);
1472 /* Trigger path verification. */
1473 io_subchannel_verify(sch
);
1477 ccw_device_set_disconnected(cdev
);
1480 case IO_SCH_ORPH_UNREG
:
1481 case IO_SCH_ORPH_ATTACH
:
1482 ccw_device_set_disconnected(cdev
);
1484 case IO_SCH_UNREG_ATTACH
:
1488 if (cdev
->private->state
== DEV_STATE_SENSE_ID
) {
1490 * Note: delayed work triggered by this event
1491 * and repeated calls to sch_event are synchronized
1492 * by the above check for work_pending(cdev).
1494 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1496 ccw_device_set_notoper(cdev
);
1504 spin_unlock_irqrestore(sch
->lock
, flags
);
1505 /* All other actions require process context. */
1508 /* Handle attached ccw device. */
1510 case IO_SCH_ORPH_UNREG
:
1511 case IO_SCH_ORPH_ATTACH
:
1512 /* Move ccw device to orphanage. */
1513 rc
= ccw_device_move_to_orph(cdev
);
1517 case IO_SCH_UNREG_ATTACH
:
1518 spin_lock_irqsave(sch
->lock
, flags
);
1519 if (cdev
->private->flags
.resuming
) {
1520 /* Device will be handled later. */
1524 sch_set_cdev(sch
, NULL
);
1525 spin_unlock_irqrestore(sch
->lock
, flags
);
1526 /* Unregister ccw device. */
1527 ccw_device_unregister(cdev
);
1532 /* Handle subchannel. */
1534 case IO_SCH_ORPH_UNREG
:
1536 if (!cdev
|| !cdev
->private->flags
.resuming
)
1537 css_sch_device_unregister(sch
);
1539 case IO_SCH_ORPH_ATTACH
:
1540 case IO_SCH_UNREG_ATTACH
:
1542 dev_id
.ssid
= sch
->schid
.ssid
;
1543 dev_id
.devno
= sch
->schib
.pmcw
.dev
;
1544 cdev
= get_ccwdev_by_dev_id(&dev_id
);
1546 sch_create_and_recog_new_device(sch
);
1549 rc
= ccw_device_move_to_sch(cdev
, sch
);
1551 /* Release reference from get_ccwdev_by_dev_id() */
1552 put_device(&cdev
->dev
);
1555 spin_lock_irqsave(sch
->lock
, flags
);
1556 ccw_device_trigger_reprobe(cdev
);
1557 spin_unlock_irqrestore(sch
->lock
, flags
);
1558 /* Release reference from get_ccwdev_by_dev_id() */
1559 put_device(&cdev
->dev
);
1567 spin_unlock_irqrestore(sch
->lock
, flags
);
1572 static void ccw_device_set_int_class(struct ccw_device
*cdev
)
1574 struct ccw_driver
*cdrv
= cdev
->drv
;
1576 /* Note: we interpret class 0 in this context as an uninitialized
1577 * field since it translates to a non-I/O interrupt class. */
1578 if (cdrv
->int_class
!= 0)
1579 cdev
->private->int_class
= cdrv
->int_class
;
1581 cdev
->private->int_class
= IRQIO_CIO
;
1584 #ifdef CONFIG_CCW_CONSOLE
1585 int __init
ccw_device_enable_console(struct ccw_device
*cdev
)
1587 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1590 if (!cdev
->drv
|| !cdev
->handler
)
1593 io_subchannel_init_fields(sch
);
1594 rc
= cio_commit_config(sch
);
1597 sch
->driver
= &io_subchannel_driver
;
1598 io_subchannel_recog(cdev
, sch
);
1599 /* Now wait for the async. recognition to come to an end. */
1600 spin_lock_irq(cdev
->ccwlock
);
1601 while (!dev_fsm_final_state(cdev
))
1602 ccw_device_wait_idle(cdev
);
1604 /* Hold on to an extra reference while device is online. */
1605 get_device(&cdev
->dev
);
1606 rc
= ccw_device_online(cdev
);
1610 while (!dev_fsm_final_state(cdev
))
1611 ccw_device_wait_idle(cdev
);
1613 if (cdev
->private->state
== DEV_STATE_ONLINE
)
1618 spin_unlock_irq(cdev
->ccwlock
);
1619 if (rc
) /* Give up online reference since onlining failed. */
1620 put_device(&cdev
->dev
);
1624 struct ccw_device
* __init
ccw_device_create_console(struct ccw_driver
*drv
)
1626 struct io_subchannel_private
*io_priv
;
1627 struct ccw_device
*cdev
;
1628 struct subchannel
*sch
;
1630 sch
= cio_probe_console();
1632 return ERR_CAST(sch
);
1634 io_priv
= kzalloc(sizeof(*io_priv
), GFP_KERNEL
| GFP_DMA
);
1636 put_device(&sch
->dev
);
1637 return ERR_PTR(-ENOMEM
);
1639 set_io_private(sch
, io_priv
);
1640 cdev
= io_subchannel_create_ccwdev(sch
);
1642 put_device(&sch
->dev
);
1647 ccw_device_set_int_class(cdev
);
1651 void __init
ccw_device_destroy_console(struct ccw_device
*cdev
)
1653 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1654 struct io_subchannel_private
*io_priv
= to_io_private(sch
);
1656 set_io_private(sch
, NULL
);
1657 put_device(&sch
->dev
);
1658 put_device(&cdev
->dev
);
1663 * ccw_device_wait_idle() - busy wait for device to become idle
1666 * Poll until activity control is zero, that is, no function or data
1667 * transfer is pending/active.
1668 * Called with device lock being held.
1670 void ccw_device_wait_idle(struct ccw_device
*cdev
)
1672 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1676 if (sch
->schib
.scsw
.cmd
.actl
== 0)
1682 static int ccw_device_pm_restore(struct device
*dev
);
1684 int ccw_device_force_console(struct ccw_device
*cdev
)
1686 return ccw_device_pm_restore(&cdev
->dev
);
1688 EXPORT_SYMBOL_GPL(ccw_device_force_console
);
1692 * get ccw_device matching the busid, but only if owned by cdrv
1695 __ccwdev_check_busid(struct device
*dev
, void *id
)
1701 return (strcmp(bus_id
, dev_name(dev
)) == 0);
1706 * get_ccwdev_by_busid() - obtain device from a bus id
1707 * @cdrv: driver the device is owned by
1708 * @bus_id: bus id of the device to be searched
1710 * This function searches all devices owned by @cdrv for a device with a bus
1711 * id matching @bus_id.
1713 * If a match is found, its reference count of the found device is increased
1714 * and it is returned; else %NULL is returned.
1716 struct ccw_device
*get_ccwdev_by_busid(struct ccw_driver
*cdrv
,
1721 dev
= driver_find_device(&cdrv
->driver
, NULL
, (void *)bus_id
,
1722 __ccwdev_check_busid
);
1724 return dev
? to_ccwdev(dev
) : NULL
;
1727 /************************** device driver handling ************************/
1729 /* This is the implementation of the ccw_driver class. The probe, remove
1730 * and release methods are initially very similar to the device_driver
1731 * implementations, with the difference that they have ccw_device
1734 * A ccw driver also contains the information that is needed for
1738 ccw_device_probe (struct device
*dev
)
1740 struct ccw_device
*cdev
= to_ccwdev(dev
);
1741 struct ccw_driver
*cdrv
= to_ccwdrv(dev
->driver
);
1744 cdev
->drv
= cdrv
; /* to let the driver call _set_online */
1745 ccw_device_set_int_class(cdev
);
1746 ret
= cdrv
->probe
? cdrv
->probe(cdev
) : -ENODEV
;
1749 cdev
->private->int_class
= IRQIO_CIO
;
1756 static int ccw_device_remove(struct device
*dev
)
1758 struct ccw_device
*cdev
= to_ccwdev(dev
);
1759 struct ccw_driver
*cdrv
= cdev
->drv
;
1765 spin_lock_irq(cdev
->ccwlock
);
1768 ret
= ccw_device_offline(cdev
);
1769 spin_unlock_irq(cdev
->ccwlock
);
1771 wait_event(cdev
->private->wait_q
,
1772 dev_fsm_final_state(cdev
));
1774 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1775 "device 0.%x.%04x\n",
1776 ret
, cdev
->private->dev_id
.ssid
,
1777 cdev
->private->dev_id
.devno
);
1778 /* Give up reference obtained in ccw_device_set_online(). */
1779 put_device(&cdev
->dev
);
1780 spin_lock_irq(cdev
->ccwlock
);
1782 ccw_device_set_timeout(cdev
, 0);
1784 cdev
->private->int_class
= IRQIO_CIO
;
1785 spin_unlock_irq(cdev
->ccwlock
);
1786 __disable_cmf(cdev
);
1791 static void ccw_device_shutdown(struct device
*dev
)
1793 struct ccw_device
*cdev
;
1795 cdev
= to_ccwdev(dev
);
1796 if (cdev
->drv
&& cdev
->drv
->shutdown
)
1797 cdev
->drv
->shutdown(cdev
);
1798 __disable_cmf(cdev
);
1801 static int ccw_device_pm_prepare(struct device
*dev
)
1803 struct ccw_device
*cdev
= to_ccwdev(dev
);
1805 if (work_pending(&cdev
->private->todo_work
))
1807 /* Fail while device is being set online/offline. */
1808 if (atomic_read(&cdev
->private->onoff
))
1811 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->prepare
)
1812 return cdev
->drv
->prepare(cdev
);
1817 static void ccw_device_pm_complete(struct device
*dev
)
1819 struct ccw_device
*cdev
= to_ccwdev(dev
);
1821 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->complete
)
1822 cdev
->drv
->complete(cdev
);
1825 static int ccw_device_pm_freeze(struct device
*dev
)
1827 struct ccw_device
*cdev
= to_ccwdev(dev
);
1828 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1829 int ret
, cm_enabled
;
1831 /* Fail suspend while device is in transistional state. */
1832 if (!dev_fsm_final_state(cdev
))
1836 if (cdev
->drv
&& cdev
->drv
->freeze
) {
1837 ret
= cdev
->drv
->freeze(cdev
);
1842 spin_lock_irq(sch
->lock
);
1843 cm_enabled
= cdev
->private->cmb
!= NULL
;
1844 spin_unlock_irq(sch
->lock
);
1846 /* Don't have the css write on memory. */
1847 ret
= ccw_set_cmf(cdev
, 0);
1851 /* From here on, disallow device driver I/O. */
1852 spin_lock_irq(sch
->lock
);
1853 ret
= cio_disable_subchannel(sch
);
1854 spin_unlock_irq(sch
->lock
);
1859 static int ccw_device_pm_thaw(struct device
*dev
)
1861 struct ccw_device
*cdev
= to_ccwdev(dev
);
1862 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1863 int ret
, cm_enabled
;
1868 spin_lock_irq(sch
->lock
);
1869 /* Allow device driver I/O again. */
1870 ret
= cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1871 cm_enabled
= cdev
->private->cmb
!= NULL
;
1872 spin_unlock_irq(sch
->lock
);
1877 ret
= ccw_set_cmf(cdev
, 1);
1882 if (cdev
->drv
&& cdev
->drv
->thaw
)
1883 ret
= cdev
->drv
->thaw(cdev
);
1888 static void __ccw_device_pm_restore(struct ccw_device
*cdev
)
1890 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
1892 spin_lock_irq(sch
->lock
);
1893 if (cio_is_console(sch
->schid
)) {
1894 cio_enable_subchannel(sch
, (u32
)(addr_t
)sch
);
1898 * While we were sleeping, devices may have gone or become
1899 * available again. Kick re-detection.
1901 cdev
->private->flags
.resuming
= 1;
1902 cdev
->private->path_new_mask
= LPM_ANYPATH
;
1903 css_sched_sch_todo(sch
, SCH_TODO_EVAL
);
1904 spin_unlock_irq(sch
->lock
);
1905 css_wait_for_slow_path();
1907 /* cdev may have been moved to a different subchannel. */
1908 sch
= to_subchannel(cdev
->dev
.parent
);
1909 spin_lock_irq(sch
->lock
);
1910 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
1911 cdev
->private->state
!= DEV_STATE_OFFLINE
)
1914 ccw_device_recognition(cdev
);
1915 spin_unlock_irq(sch
->lock
);
1916 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
) ||
1917 cdev
->private->state
== DEV_STATE_DISCONNECTED
);
1918 spin_lock_irq(sch
->lock
);
1921 cdev
->private->flags
.resuming
= 0;
1922 spin_unlock_irq(sch
->lock
);
1925 static int resume_handle_boxed(struct ccw_device
*cdev
)
1927 cdev
->private->state
= DEV_STATE_BOXED
;
1928 if (ccw_device_notify(cdev
, CIO_BOXED
) == NOTIFY_OK
)
1930 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1934 static int resume_handle_disc(struct ccw_device
*cdev
)
1936 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
1937 if (ccw_device_notify(cdev
, CIO_GONE
) == NOTIFY_OK
)
1939 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1943 static int ccw_device_pm_restore(struct device
*dev
)
1945 struct ccw_device
*cdev
= to_ccwdev(dev
);
1946 struct subchannel
*sch
;
1949 __ccw_device_pm_restore(cdev
);
1950 sch
= to_subchannel(cdev
->dev
.parent
);
1951 spin_lock_irq(sch
->lock
);
1952 if (cio_is_console(sch
->schid
))
1955 /* check recognition results */
1956 switch (cdev
->private->state
) {
1957 case DEV_STATE_OFFLINE
:
1958 case DEV_STATE_ONLINE
:
1959 cdev
->private->flags
.donotify
= 0;
1961 case DEV_STATE_BOXED
:
1962 ret
= resume_handle_boxed(cdev
);
1967 ret
= resume_handle_disc(cdev
);
1972 /* check if the device type has changed */
1973 if (!ccw_device_test_sense_data(cdev
)) {
1974 ccw_device_update_sense_data(cdev
);
1975 ccw_device_sched_todo(cdev
, CDEV_TODO_REBIND
);
1982 if (ccw_device_online(cdev
)) {
1983 ret
= resume_handle_disc(cdev
);
1988 spin_unlock_irq(sch
->lock
);
1989 wait_event(cdev
->private->wait_q
, dev_fsm_final_state(cdev
));
1990 spin_lock_irq(sch
->lock
);
1992 if (ccw_device_notify(cdev
, CIO_OPER
) == NOTIFY_BAD
) {
1993 ccw_device_sched_todo(cdev
, CDEV_TODO_UNREG
);
1998 /* reenable cmf, if needed */
1999 if (cdev
->private->cmb
) {
2000 spin_unlock_irq(sch
->lock
);
2001 ret
= ccw_set_cmf(cdev
, 1);
2002 spin_lock_irq(sch
->lock
);
2004 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2005 "(rc=%d)\n", cdev
->private->dev_id
.ssid
,
2006 cdev
->private->dev_id
.devno
, ret
);
2012 spin_unlock_irq(sch
->lock
);
2013 if (cdev
->online
&& cdev
->drv
&& cdev
->drv
->restore
)
2014 ret
= cdev
->drv
->restore(cdev
);
2018 spin_unlock_irq(sch
->lock
);
2022 static const struct dev_pm_ops ccw_pm_ops
= {
2023 .prepare
= ccw_device_pm_prepare
,
2024 .complete
= ccw_device_pm_complete
,
2025 .freeze
= ccw_device_pm_freeze
,
2026 .thaw
= ccw_device_pm_thaw
,
2027 .restore
= ccw_device_pm_restore
,
2030 static struct bus_type ccw_bus_type
= {
2032 .match
= ccw_bus_match
,
2033 .uevent
= ccw_uevent
,
2034 .probe
= ccw_device_probe
,
2035 .remove
= ccw_device_remove
,
2036 .shutdown
= ccw_device_shutdown
,
2041 * ccw_driver_register() - register a ccw driver
2042 * @cdriver: driver to be registered
2044 * This function is mainly a wrapper around driver_register().
2046 * %0 on success and a negative error value on failure.
2048 int ccw_driver_register(struct ccw_driver
*cdriver
)
2050 struct device_driver
*drv
= &cdriver
->driver
;
2052 drv
->bus
= &ccw_bus_type
;
2054 return driver_register(drv
);
2058 * ccw_driver_unregister() - deregister a ccw driver
2059 * @cdriver: driver to be deregistered
2061 * This function is mainly a wrapper around driver_unregister().
2063 void ccw_driver_unregister(struct ccw_driver
*cdriver
)
2065 driver_unregister(&cdriver
->driver
);
2068 static void ccw_device_todo(struct work_struct
*work
)
2070 struct ccw_device_private
*priv
;
2071 struct ccw_device
*cdev
;
2072 struct subchannel
*sch
;
2073 enum cdev_todo todo
;
2075 priv
= container_of(work
, struct ccw_device_private
, todo_work
);
2077 sch
= to_subchannel(cdev
->dev
.parent
);
2078 /* Find out todo. */
2079 spin_lock_irq(cdev
->ccwlock
);
2081 priv
->todo
= CDEV_TODO_NOTHING
;
2082 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2083 priv
->dev_id
.ssid
, priv
->dev_id
.devno
, todo
);
2084 spin_unlock_irq(cdev
->ccwlock
);
2087 case CDEV_TODO_ENABLE_CMF
:
2090 case CDEV_TODO_REBIND
:
2091 ccw_device_do_unbind_bind(cdev
);
2093 case CDEV_TODO_REGISTER
:
2094 io_subchannel_register(cdev
);
2096 case CDEV_TODO_UNREG_EVAL
:
2097 if (!sch_is_pseudo_sch(sch
))
2098 css_schedule_eval(sch
->schid
);
2100 case CDEV_TODO_UNREG
:
2101 if (sch_is_pseudo_sch(sch
))
2102 ccw_device_unregister(cdev
);
2104 ccw_device_call_sch_unregister(cdev
);
2109 /* Release workqueue ref. */
2110 put_device(&cdev
->dev
);
2114 * ccw_device_sched_todo - schedule ccw device operation
2118 * Schedule the operation identified by @todo to be performed on the slow path
2119 * workqueue. Do nothing if another operation with higher priority is already
2120 * scheduled. Needs to be called with ccwdev lock held.
2122 void ccw_device_sched_todo(struct ccw_device
*cdev
, enum cdev_todo todo
)
2124 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2125 cdev
->private->dev_id
.ssid
, cdev
->private->dev_id
.devno
,
2127 if (cdev
->private->todo
>= todo
)
2129 cdev
->private->todo
= todo
;
2130 /* Get workqueue ref. */
2131 if (!get_device(&cdev
->dev
))
2133 if (!queue_work(cio_work_q
, &cdev
->private->todo_work
)) {
2134 /* Already queued, release workqueue ref. */
2135 put_device(&cdev
->dev
);
2140 * ccw_device_siosl() - initiate logging
2143 * This function is used to invoke model-dependent logging within the channel
2146 int ccw_device_siosl(struct ccw_device
*cdev
)
2148 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
2150 return chsc_siosl(sch
->schid
);
2152 EXPORT_SYMBOL_GPL(ccw_device_siosl
);
2154 EXPORT_SYMBOL(ccw_device_set_online
);
2155 EXPORT_SYMBOL(ccw_device_set_offline
);
2156 EXPORT_SYMBOL(ccw_driver_register
);
2157 EXPORT_SYMBOL(ccw_driver_unregister
);
2158 EXPORT_SYMBOL(get_ccwdev_by_busid
);