crw.c 4.1 KB

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