blob: fc285ca4114144ca860a017781b44775f9e05ff6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Heiko Carstensf5daba12009-03-26 15:24:01 +01002/*
3 * Channel report handling code
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2000, 2009
Heiko Carstensf5daba12009-03-26 15:24:01 +01006 * Author(s): Ingo Adlung <adlung@de.ibm.com>,
7 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
8 * Cornelia Huck <cornelia.huck@de.ibm.com>,
9 * Heiko Carstens <heiko.carstens@de.ibm.com>,
10 */
11
Heiko Carstens98c1c682009-03-26 15:24:09 +010012#include <linux/mutex.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010013#include <linux/kthread.h>
14#include <linux/init.h>
Sebastian Ottb4563e82010-02-26 22:37:26 +010015#include <linux/wait.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010016#include <asm/crw.h>
David Howellsa0616cd2012-03-28 18:30:02 +010017#include <asm/ctl_reg.h>
Peter Oberparleiter2ab59de2015-12-18 12:59:32 +010018#include "ioasm.h"
Heiko Carstensf5daba12009-03-26 15:24:01 +010019
Heiko Carstens98c1c682009-03-26 15:24:09 +010020static DEFINE_MUTEX(crw_handler_mutex);
Heiko Carstensf5daba12009-03-26 15:24:01 +010021static crw_handler_t crw_handlers[NR_RSCS];
Sebastian Ottb4563e82010-02-26 22:37:26 +010022static atomic_t crw_nr_req = ATOMIC_INIT(0);
23static DECLARE_WAIT_QUEUE_HEAD(crw_handler_wait_q);
Heiko Carstensf5daba12009-03-26 15:24:01 +010024
25/**
26 * crw_register_handler() - register a channel report word handler
27 * @rsc: reporting source code to handle
28 * @handler: handler to be registered
29 *
30 * Returns %0 on success and a negative error value otherwise.
31 */
32int crw_register_handler(int rsc, crw_handler_t handler)
33{
Heiko Carstens98c1c682009-03-26 15:24:09 +010034 int rc = 0;
35
Heiko Carstensf5daba12009-03-26 15:24:01 +010036 if ((rsc < 0) || (rsc >= NR_RSCS))
37 return -EINVAL;
Heiko Carstens98c1c682009-03-26 15:24:09 +010038 mutex_lock(&crw_handler_mutex);
39 if (crw_handlers[rsc])
40 rc = -EBUSY;
41 else
42 crw_handlers[rsc] = handler;
43 mutex_unlock(&crw_handler_mutex);
44 return rc;
Heiko Carstensf5daba12009-03-26 15:24:01 +010045}
46
47/**
48 * crw_unregister_handler() - unregister a channel report word handler
49 * @rsc: reporting source code to handle
50 */
51void crw_unregister_handler(int rsc)
52{
53 if ((rsc < 0) || (rsc >= NR_RSCS))
54 return;
Heiko Carstens98c1c682009-03-26 15:24:09 +010055 mutex_lock(&crw_handler_mutex);
56 crw_handlers[rsc] = NULL;
57 mutex_unlock(&crw_handler_mutex);
Heiko Carstensf5daba12009-03-26 15:24:01 +010058}
59
60/*
61 * Retrieve CRWs and call function to handle event.
62 */
63static int crw_collect_info(void *unused)
64{
65 struct crw crw[2];
Sebastian Ottb4563e82010-02-26 22:37:26 +010066 int ccode, signal;
Heiko Carstensf5daba12009-03-26 15:24:01 +010067 unsigned int chain;
Heiko Carstensf5daba12009-03-26 15:24:01 +010068
69repeat:
Sebastian Ottb4563e82010-02-26 22:37:26 +010070 signal = wait_event_interruptible(crw_handler_wait_q,
71 atomic_read(&crw_nr_req) > 0);
72 if (unlikely(signal))
73 atomic_inc(&crw_nr_req);
Heiko Carstensf5daba12009-03-26 15:24:01 +010074 chain = 0;
75 while (1) {
Heiko Carstens98c1c682009-03-26 15:24:09 +010076 crw_handler_t handler;
77
Heiko Carstensf5daba12009-03-26 15:24:01 +010078 if (unlikely(chain > 1)) {
79 struct crw tmp_crw;
80
81 printk(KERN_WARNING"%s: Code does not support more "
82 "than two chained crws; please report to "
83 "linux390@de.ibm.com!\n", __func__);
84 ccode = stcrw(&tmp_crw);
85 printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, "
86 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
87 __func__, tmp_crw.slct, tmp_crw.oflw,
88 tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc,
89 tmp_crw.erc, tmp_crw.rsid);
90 printk(KERN_WARNING"%s: This was crw number %x in the "
91 "chain\n", __func__, chain);
92 if (ccode != 0)
93 break;
94 chain = tmp_crw.chn ? chain + 1 : 0;
95 continue;
96 }
97 ccode = stcrw(&crw[chain]);
98 if (ccode != 0)
99 break;
100 printk(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, "
101 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
102 crw[chain].slct, crw[chain].oflw, crw[chain].chn,
103 crw[chain].rsc, crw[chain].anc, crw[chain].erc,
104 crw[chain].rsid);
105 /* Check for overflows. */
106 if (crw[chain].oflw) {
107 int i;
108
109 pr_debug("%s: crw overflow detected!\n", __func__);
Heiko Carstens98c1c682009-03-26 15:24:09 +0100110 mutex_lock(&crw_handler_mutex);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100111 for (i = 0; i < NR_RSCS; i++) {
112 if (crw_handlers[i])
113 crw_handlers[i](NULL, NULL, 1);
114 }
Heiko Carstens98c1c682009-03-26 15:24:09 +0100115 mutex_unlock(&crw_handler_mutex);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100116 chain = 0;
117 continue;
118 }
119 if (crw[0].chn && !chain) {
120 chain++;
121 continue;
122 }
Heiko Carstens98c1c682009-03-26 15:24:09 +0100123 mutex_lock(&crw_handler_mutex);
124 handler = crw_handlers[crw[chain].rsc];
125 if (handler)
126 handler(&crw[0], chain ? &crw[1] : NULL, 0);
127 mutex_unlock(&crw_handler_mutex);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100128 /* chain is always 0 or 1 here. */
129 chain = crw[chain].chn ? chain + 1 : 0;
130 }
Sebastian Ottb4563e82010-02-26 22:37:26 +0100131 if (atomic_dec_and_test(&crw_nr_req))
132 wake_up(&crw_handler_wait_q);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100133 goto repeat;
134 return 0;
135}
136
137void crw_handle_channel_report(void)
138{
Sebastian Ottb4563e82010-02-26 22:37:26 +0100139 atomic_inc(&crw_nr_req);
140 wake_up(&crw_handler_wait_q);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100141}
142
Sebastian Ottb4563e82010-02-26 22:37:26 +0100143void crw_wait_for_channel_report(void)
Heiko Carstensf5daba12009-03-26 15:24:01 +0100144{
Sebastian Ottb4563e82010-02-26 22:37:26 +0100145 crw_handle_channel_report();
146 wait_event(crw_handler_wait_q, atomic_read(&crw_nr_req) == 0);
Heiko Carstensf5daba12009-03-26 15:24:01 +0100147}
Heiko Carstensf5daba12009-03-26 15:24:01 +0100148
149/*
150 * Machine checks for the channel subsystem must be enabled
151 * after the channel subsystem is initialized
152 */
153static int __init crw_machine_check_init(void)
154{
155 struct task_struct *task;
156
157 task = kthread_run(crw_collect_info, NULL, "kmcheck");
158 if (IS_ERR(task))
159 return PTR_ERR(task);
160 ctl_set_bit(14, 28); /* enable channel report MCH */
161 return 0;
162}
163device_initcall(crw_machine_check_init);