Linux 5.1.15
[linux/fpc-iii.git] / drivers / s390 / cio / crw.c
blobfc285ca4114144ca860a017781b44775f9e05ff6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Channel report handling code
5 * Copyright IBM Corp. 2000, 2009
6 * 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>,
12 #include <linux/mutex.h>
13 #include <linux/kthread.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <asm/crw.h>
17 #include <asm/ctl_reg.h>
18 #include "ioasm.h"
20 static DEFINE_MUTEX(crw_handler_mutex);
21 static crw_handler_t crw_handlers[NR_RSCS];
22 static atomic_t crw_nr_req = ATOMIC_INIT(0);
23 static DECLARE_WAIT_QUEUE_HEAD(crw_handler_wait_q);
25 /**
26 * crw_register_handler() - register a channel report word handler
27 * @rsc: reporting source code to handle
28 * @handler: handler to be registered
30 * Returns %0 on success and a negative error value otherwise.
32 int crw_register_handler(int rsc, crw_handler_t handler)
34 int rc = 0;
36 if ((rsc < 0) || (rsc >= NR_RSCS))
37 return -EINVAL;
38 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;
47 /**
48 * crw_unregister_handler() - unregister a channel report word handler
49 * @rsc: reporting source code to handle
51 void crw_unregister_handler(int rsc)
53 if ((rsc < 0) || (rsc >= NR_RSCS))
54 return;
55 mutex_lock(&crw_handler_mutex);
56 crw_handlers[rsc] = NULL;
57 mutex_unlock(&crw_handler_mutex);
61 * Retrieve CRWs and call function to handle event.
63 static int crw_collect_info(void *unused)
65 struct crw crw[2];
66 int ccode, signal;
67 unsigned int chain;
69 repeat:
70 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);
74 chain = 0;
75 while (1) {
76 crw_handler_t handler;
78 if (unlikely(chain > 1)) {
79 struct crw tmp_crw;
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;
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;
109 pr_debug("%s: crw overflow detected!\n", __func__);
110 mutex_lock(&crw_handler_mutex);
111 for (i = 0; i < NR_RSCS; i++) {
112 if (crw_handlers[i])
113 crw_handlers[i](NULL, NULL, 1);
115 mutex_unlock(&crw_handler_mutex);
116 chain = 0;
117 continue;
119 if (crw[0].chn && !chain) {
120 chain++;
121 continue;
123 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);
128 /* chain is always 0 or 1 here. */
129 chain = crw[chain].chn ? chain + 1 : 0;
131 if (atomic_dec_and_test(&crw_nr_req))
132 wake_up(&crw_handler_wait_q);
133 goto repeat;
134 return 0;
137 void crw_handle_channel_report(void)
139 atomic_inc(&crw_nr_req);
140 wake_up(&crw_handler_wait_q);
143 void crw_wait_for_channel_report(void)
145 crw_handle_channel_report();
146 wait_event(crw_handler_wait_q, atomic_read(&crw_nr_req) == 0);
150 * Machine checks for the channel subsystem must be enabled
151 * after the channel subsystem is initialized
153 static int __init crw_machine_check_init(void)
155 struct task_struct *task;
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;
163 device_initcall(crw_machine_check_init);