airq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Support for adapter interruptions
  3. *
  4. * Copyright IBM Corp. 1999, 2007
  5. * Author(s): Ingo Adlung <adlung@de.ibm.com>
  6. * Cornelia Huck <cornelia.huck@de.ibm.com>
  7. * Arnd Bergmann <arndb@de.ibm.com>
  8. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/rcupdate.h>
  14. #include <asm/airq.h>
  15. #include <asm/isc.h>
  16. #include "cio.h"
  17. #include "cio_debug.h"
  18. #define NR_AIRQS 32
  19. #define NR_AIRQS_PER_WORD sizeof(unsigned long)
  20. #define NR_AIRQ_WORDS (NR_AIRQS / NR_AIRQS_PER_WORD)
  21. union indicator_t {
  22. unsigned long word[NR_AIRQ_WORDS];
  23. unsigned char byte[NR_AIRQS];
  24. } __attribute__((packed));
  25. struct airq_t {
  26. adapter_int_handler_t handler;
  27. void *drv_data;
  28. };
  29. static union indicator_t indicators[MAX_ISC+1];
  30. static struct airq_t *airqs[MAX_ISC+1][NR_AIRQS];
  31. static int register_airq(struct airq_t *airq, u8 isc)
  32. {
  33. int i;
  34. for (i = 0; i < NR_AIRQS; i++)
  35. if (!cmpxchg(&airqs[isc][i], NULL, airq))
  36. return i;
  37. return -ENOMEM;
  38. }
  39. /**
  40. * s390_register_adapter_interrupt() - register adapter interrupt handler
  41. * @handler: adapter handler to be registered
  42. * @drv_data: driver data passed with each call to the handler
  43. * @isc: isc for which the handler should be called
  44. *
  45. * Returns:
  46. * Pointer to the indicator to be used on success
  47. * ERR_PTR() if registration failed
  48. */
  49. void *s390_register_adapter_interrupt(adapter_int_handler_t handler,
  50. void *drv_data, u8 isc)
  51. {
  52. struct airq_t *airq;
  53. char dbf_txt[16];
  54. int ret;
  55. if (isc > MAX_ISC)
  56. return ERR_PTR(-EINVAL);
  57. airq = kmalloc(sizeof(struct airq_t), GFP_KERNEL);
  58. if (!airq) {
  59. ret = -ENOMEM;
  60. goto out;
  61. }
  62. airq->handler = handler;
  63. airq->drv_data = drv_data;
  64. ret = register_airq(airq, isc);
  65. out:
  66. snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%d", ret);
  67. CIO_TRACE_EVENT(4, dbf_txt);
  68. if (ret < 0) {
  69. kfree(airq);
  70. return ERR_PTR(ret);
  71. } else
  72. return &indicators[isc].byte[ret];
  73. }
  74. EXPORT_SYMBOL(s390_register_adapter_interrupt);
  75. /**
  76. * s390_unregister_adapter_interrupt - unregister adapter interrupt handler
  77. * @ind: indicator for which the handler is to be unregistered
  78. * @isc: interruption subclass
  79. */
  80. void s390_unregister_adapter_interrupt(void *ind, u8 isc)
  81. {
  82. struct airq_t *airq;
  83. char dbf_txt[16];
  84. int i;
  85. i = (int) ((addr_t) ind) - ((addr_t) &indicators[isc].byte[0]);
  86. snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%d", i);
  87. CIO_TRACE_EVENT(4, dbf_txt);
  88. indicators[isc].byte[i] = 0;
  89. airq = xchg(&airqs[isc][i], NULL);
  90. /*
  91. * Allow interrupts to complete. This will ensure that the airq handle
  92. * is no longer referenced by any interrupt handler.
  93. */
  94. synchronize_sched();
  95. kfree(airq);
  96. }
  97. EXPORT_SYMBOL(s390_unregister_adapter_interrupt);
  98. #define INDICATOR_MASK (0xffUL << ((NR_AIRQS_PER_WORD - 1) * 8))
  99. void do_adapter_IO(u8 isc)
  100. {
  101. int w;
  102. int i;
  103. unsigned long word;
  104. struct airq_t *airq;
  105. /*
  106. * Access indicator array in word-sized chunks to minimize storage
  107. * fetch operations.
  108. */
  109. for (w = 0; w < NR_AIRQ_WORDS; w++) {
  110. word = indicators[isc].word[w];
  111. i = w * NR_AIRQS_PER_WORD;
  112. /*
  113. * Check bytes within word for active indicators.
  114. */
  115. while (word) {
  116. if (word & INDICATOR_MASK) {
  117. airq = airqs[isc][i];
  118. /* Make sure gcc reads from airqs only once. */
  119. barrier();
  120. if (likely(airq))
  121. airq->handler(&indicators[isc].byte[i],
  122. airq->drv_data);
  123. else
  124. /*
  125. * Reset ill-behaved indicator.
  126. */
  127. indicators[isc].byte[i] = 0;
  128. }
  129. word <<= 8;
  130. i++;
  131. }
  132. }
  133. }