crw.c 4.0 KB

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