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