crw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Channel report handling code
  3. *
  4. * Copyright IBM Corp. 2000, 2009
  5. * Author(s): Ingo Adlung <adlung@de.ibm.com>,
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>,
  7. * Cornelia Huck <cornelia.huck@de.ibm.com>,
  8. * Heiko Carstens <heiko.carstens@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/crw.h>
  15. #include <asm/ctl_reg.h>
  16. static DEFINE_MUTEX(crw_handler_mutex);
  17. static crw_handler_t crw_handlers[NR_RSCS];
  18. static atomic_t crw_nr_req = ATOMIC_INIT(0);
  19. static DECLARE_WAIT_QUEUE_HEAD(crw_handler_wait_q);
  20. /**
  21. * crw_register_handler() - register a channel report word handler
  22. * @rsc: reporting source code to handle
  23. * @handler: handler to be registered
  24. *
  25. * Returns %0 on success and a negative error value otherwise.
  26. */
  27. int crw_register_handler(int rsc, crw_handler_t handler)
  28. {
  29. int rc = 0;
  30. if ((rsc < 0) || (rsc >= NR_RSCS))
  31. return -EINVAL;
  32. mutex_lock(&crw_handler_mutex);
  33. if (crw_handlers[rsc])
  34. rc = -EBUSY;
  35. else
  36. crw_handlers[rsc] = handler;
  37. mutex_unlock(&crw_handler_mutex);
  38. return rc;
  39. }
  40. /**
  41. * crw_unregister_handler() - unregister a channel report word handler
  42. * @rsc: reporting source code to handle
  43. */
  44. void crw_unregister_handler(int rsc)
  45. {
  46. if ((rsc < 0) || (rsc >= NR_RSCS))
  47. return;
  48. mutex_lock(&crw_handler_mutex);
  49. crw_handlers[rsc] = NULL;
  50. mutex_unlock(&crw_handler_mutex);
  51. }
  52. /*
  53. * Retrieve CRWs and call function to handle event.
  54. */
  55. static int crw_collect_info(void *unused)
  56. {
  57. struct crw crw[2];
  58. int ccode, signal;
  59. unsigned int chain;
  60. repeat:
  61. signal = wait_event_interruptible(crw_handler_wait_q,
  62. atomic_read(&crw_nr_req) > 0);
  63. if (unlikely(signal))
  64. atomic_inc(&crw_nr_req);
  65. chain = 0;
  66. while (1) {
  67. crw_handler_t handler;
  68. if (unlikely(chain > 1)) {
  69. struct crw tmp_crw;
  70. printk(KERN_WARNING"%s: Code does not support more "
  71. "than two chained crws; please report to "
  72. "linux390@de.ibm.com!\n", __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. ctl_set_bit(14, 28); /* enable channel report MCH */
  145. return 0;
  146. }
  147. device_initcall(crw_machine_check_init);