1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Channel report handling code 4 * 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 */ 10 11 #include <linux/mutex.h> 12 #include <linux/kthread.h> 13 #include <linux/init.h> 14 #include <linux/wait.h> 15 #include <asm/ctlreg.h> 16 #include <asm/crw.h> 17 #include "ioasm.h" 18 19 static DEFINE_MUTEX(crw_handler_mutex); 20 static crw_handler_t crw_handlers[NR_RSCS]; 21 static atomic_t crw_nr_req = ATOMIC_INIT(0); 22 static DECLARE_WAIT_QUEUE_HEAD(crw_handler_wait_q); 23 24 /** 25 * crw_register_handler() - register a channel report word handler 26 * @rsc: reporting source code to handle 27 * @handler: handler to be registered 28 * 29 * Returns %0 on success and a negative error value otherwise. 30 */ 31 int crw_register_handler(int rsc, crw_handler_t handler) 32 { 33 int rc = 0; 34 35 if ((rsc < 0) || (rsc >= NR_RSCS)) 36 return -EINVAL; 37 mutex_lock(&crw_handler_mutex); 38 if (crw_handlers[rsc]) 39 rc = -EBUSY; 40 else 41 crw_handlers[rsc] = handler; 42 mutex_unlock(&crw_handler_mutex); 43 return rc; 44 } 45 46 /** 47 * crw_unregister_handler() - unregister a channel report word handler 48 * @rsc: reporting source code to handle 49 */ 50 void crw_unregister_handler(int rsc) 51 { 52 if ((rsc < 0) || (rsc >= NR_RSCS)) 53 return; 54 mutex_lock(&crw_handler_mutex); 55 crw_handlers[rsc] = NULL; 56 mutex_unlock(&crw_handler_mutex); 57 } 58 59 /* 60 * Retrieve CRWs and call function to handle event. 61 */ 62 static int crw_collect_info(void *unused) 63 { 64 struct crw crw[2]; 65 int ccode, signal; 66 unsigned int chain; 67 68 repeat: 69 signal = wait_event_interruptible(crw_handler_wait_q, 70 atomic_read(&crw_nr_req) > 0); 71 if (unlikely(signal)) 72 atomic_inc(&crw_nr_req); 73 chain = 0; 74 while (1) { 75 crw_handler_t handler; 76 77 if (unlikely(chain > 1)) { 78 struct crw tmp_crw; 79 80 printk(KERN_WARNING "%s: Code does not support more than two chained crws\n", 81 __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__); 108 mutex_lock(&crw_handler_mutex); 109 for (i = 0; i < NR_RSCS; i++) { 110 if (crw_handlers[i]) 111 crw_handlers[i](NULL, NULL, 1); 112 } 113 mutex_unlock(&crw_handler_mutex); 114 chain = 0; 115 continue; 116 } 117 if (crw[0].chn && !chain) { 118 chain++; 119 continue; 120 } 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); 126 /* chain is always 0 or 1 here. */ 127 chain = crw[chain].chn ? chain + 1 : 0; 128 } 129 if (atomic_dec_and_test(&crw_nr_req)) 130 wake_up(&crw_handler_wait_q); 131 goto repeat; 132 return 0; 133 } 134 135 void crw_handle_channel_report(void) 136 { 137 atomic_inc(&crw_nr_req); 138 wake_up(&crw_handler_wait_q); 139 } 140 141 void crw_wait_for_channel_report(void) 142 { 143 crw_handle_channel_report(); 144 wait_event(crw_handler_wait_q, atomic_read(&crw_nr_req) == 0); 145 } 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 system_ctl_set_bit(14, CR14_CHANNEL_REPORT_SUBMASK_BIT); 159 return 0; 160 } 161 device_initcall(crw_machine_check_init); 162