Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Channel report handling code |
| 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2000, 2009 |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 5 | * Author(s): Ingo Adlung <adlung@de.ibm.com>, |
| 6 | * Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 7 | * Cornelia Huck <cornelia.huck@de.ibm.com>, |
| 8 | * Heiko Carstens <heiko.carstens@de.ibm.com>, |
| 9 | */ |
| 10 | |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 12 | #include <linux/kthread.h> |
| 13 | #include <linux/init.h> |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 14 | #include <linux/wait.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 15 | #include <asm/crw.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 16 | #include <asm/ctl_reg.h> |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 17 | |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 18 | static DEFINE_MUTEX(crw_handler_mutex); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 19 | static crw_handler_t crw_handlers[NR_RSCS]; |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 20 | static atomic_t crw_nr_req = ATOMIC_INIT(0); |
| 21 | static DECLARE_WAIT_QUEUE_HEAD(crw_handler_wait_q); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * crw_register_handler() - register a channel report word handler |
| 25 | * @rsc: reporting source code to handle |
| 26 | * @handler: handler to be registered |
| 27 | * |
| 28 | * Returns %0 on success and a negative error value otherwise. |
| 29 | */ |
| 30 | int crw_register_handler(int rsc, crw_handler_t handler) |
| 31 | { |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 32 | int rc = 0; |
| 33 | |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 34 | if ((rsc < 0) || (rsc >= NR_RSCS)) |
| 35 | return -EINVAL; |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 36 | mutex_lock(&crw_handler_mutex); |
| 37 | if (crw_handlers[rsc]) |
| 38 | rc = -EBUSY; |
| 39 | else |
| 40 | crw_handlers[rsc] = handler; |
| 41 | mutex_unlock(&crw_handler_mutex); |
| 42 | return rc; |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * crw_unregister_handler() - unregister a channel report word handler |
| 47 | * @rsc: reporting source code to handle |
| 48 | */ |
| 49 | void crw_unregister_handler(int rsc) |
| 50 | { |
| 51 | if ((rsc < 0) || (rsc >= NR_RSCS)) |
| 52 | return; |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 53 | mutex_lock(&crw_handler_mutex); |
| 54 | crw_handlers[rsc] = NULL; |
| 55 | mutex_unlock(&crw_handler_mutex); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Retrieve CRWs and call function to handle event. |
| 60 | */ |
| 61 | static int crw_collect_info(void *unused) |
| 62 | { |
| 63 | struct crw crw[2]; |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 64 | int ccode, signal; |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 65 | unsigned int chain; |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 66 | |
| 67 | repeat: |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 68 | signal = wait_event_interruptible(crw_handler_wait_q, |
| 69 | atomic_read(&crw_nr_req) > 0); |
| 70 | if (unlikely(signal)) |
| 71 | atomic_inc(&crw_nr_req); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 72 | chain = 0; |
| 73 | while (1) { |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 74 | crw_handler_t handler; |
| 75 | |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 76 | if (unlikely(chain > 1)) { |
| 77 | struct crw tmp_crw; |
| 78 | |
| 79 | printk(KERN_WARNING"%s: Code does not support more " |
| 80 | "than two chained crws; please report to " |
| 81 | "linux390@de.ibm.com!\n", __func__); |
| 82 | ccode = stcrw(&tmp_crw); |
| 83 | printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, " |
| 84 | "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", |
| 85 | __func__, tmp_crw.slct, tmp_crw.oflw, |
| 86 | tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc, |
| 87 | tmp_crw.erc, tmp_crw.rsid); |
| 88 | printk(KERN_WARNING"%s: This was crw number %x in the " |
| 89 | "chain\n", __func__, chain); |
| 90 | if (ccode != 0) |
| 91 | break; |
| 92 | chain = tmp_crw.chn ? chain + 1 : 0; |
| 93 | continue; |
| 94 | } |
| 95 | ccode = stcrw(&crw[chain]); |
| 96 | if (ccode != 0) |
| 97 | break; |
| 98 | printk(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, " |
| 99 | "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", |
| 100 | crw[chain].slct, crw[chain].oflw, crw[chain].chn, |
| 101 | crw[chain].rsc, crw[chain].anc, crw[chain].erc, |
| 102 | crw[chain].rsid); |
| 103 | /* Check for overflows. */ |
| 104 | if (crw[chain].oflw) { |
| 105 | int i; |
| 106 | |
| 107 | pr_debug("%s: crw overflow detected!\n", __func__); |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 108 | mutex_lock(&crw_handler_mutex); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 109 | for (i = 0; i < NR_RSCS; i++) { |
| 110 | if (crw_handlers[i]) |
| 111 | crw_handlers[i](NULL, NULL, 1); |
| 112 | } |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 113 | mutex_unlock(&crw_handler_mutex); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 114 | chain = 0; |
| 115 | continue; |
| 116 | } |
| 117 | if (crw[0].chn && !chain) { |
| 118 | chain++; |
| 119 | continue; |
| 120 | } |
Heiko Carstens | 98c1c68 | 2009-03-26 15:24:09 +0100 | [diff] [blame] | 121 | mutex_lock(&crw_handler_mutex); |
| 122 | handler = crw_handlers[crw[chain].rsc]; |
| 123 | if (handler) |
| 124 | handler(&crw[0], chain ? &crw[1] : NULL, 0); |
| 125 | mutex_unlock(&crw_handler_mutex); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 126 | /* chain is always 0 or 1 here. */ |
| 127 | chain = crw[chain].chn ? chain + 1 : 0; |
| 128 | } |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 129 | if (atomic_dec_and_test(&crw_nr_req)) |
| 130 | wake_up(&crw_handler_wait_q); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 131 | goto repeat; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | void crw_handle_channel_report(void) |
| 136 | { |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 137 | atomic_inc(&crw_nr_req); |
| 138 | wake_up(&crw_handler_wait_q); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 141 | void crw_wait_for_channel_report(void) |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 142 | { |
Sebastian Ott | b4563e8 | 2010-02-26 22:37:26 +0100 | [diff] [blame] | 143 | crw_handle_channel_report(); |
| 144 | wait_event(crw_handler_wait_q, atomic_read(&crw_nr_req) == 0); |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 145 | } |
Heiko Carstens | f5daba1 | 2009-03-26 15:24:01 +0100 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * Machine checks for the channel subsystem must be enabled |
| 149 | * after the channel subsystem is initialized |
| 150 | */ |
| 151 | static int __init crw_machine_check_init(void) |
| 152 | { |
| 153 | struct task_struct *task; |
| 154 | |
| 155 | task = kthread_run(crw_collect_info, NULL, "kmcheck"); |
| 156 | if (IS_ERR(task)) |
| 157 | return PTR_ERR(task); |
| 158 | ctl_set_bit(14, 28); /* enable channel report MCH */ |
| 159 | return 0; |
| 160 | } |
| 161 | device_initcall(crw_machine_check_init); |