bfad_intr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
  3. * All rights reserved
  4. * www.brocade.com
  5. *
  6. * Linux driver for Brocade Fibre Channel Host Bus Adapter.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License (GPL) Version 2 as
  10. * published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include "bfad_drv.h"
  18. #include "bfad_trcmod.h"
  19. BFA_TRC_FILE(LDRV, INTR);
  20. /**
  21. * bfa_isr BFA driver interrupt functions
  22. */
  23. static int msix_disable;
  24. module_param(msix_disable, int, S_IRUGO | S_IWUSR);
  25. /**
  26. * Line based interrupt handler.
  27. */
  28. static irqreturn_t
  29. bfad_intx(int irq, void *dev_id)
  30. {
  31. struct bfad_s *bfad = dev_id;
  32. struct list_head doneq;
  33. unsigned long flags;
  34. bfa_boolean_t rc;
  35. spin_lock_irqsave(&bfad->bfad_lock, flags);
  36. rc = bfa_intx(&bfad->bfa);
  37. if (!rc) {
  38. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  39. return IRQ_NONE;
  40. }
  41. bfa_comp_deq(&bfad->bfa, &doneq);
  42. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  43. if (!list_empty(&doneq)) {
  44. bfa_comp_process(&bfad->bfa, &doneq);
  45. spin_lock_irqsave(&bfad->bfad_lock, flags);
  46. bfa_comp_free(&bfad->bfa, &doneq);
  47. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  48. bfa_trc_fp(bfad, irq);
  49. }
  50. return IRQ_HANDLED;
  51. }
  52. static irqreturn_t
  53. bfad_msix(int irq, void *dev_id)
  54. {
  55. struct bfad_msix_s *vec = dev_id;
  56. struct bfad_s *bfad = vec->bfad;
  57. struct list_head doneq;
  58. unsigned long flags;
  59. spin_lock_irqsave(&bfad->bfad_lock, flags);
  60. bfa_msix(&bfad->bfa, vec->msix.entry);
  61. bfa_comp_deq(&bfad->bfa, &doneq);
  62. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  63. if (!list_empty(&doneq)) {
  64. bfa_comp_process(&bfad->bfa, &doneq);
  65. spin_lock_irqsave(&bfad->bfad_lock, flags);
  66. bfa_comp_free(&bfad->bfa, &doneq);
  67. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  68. }
  69. return IRQ_HANDLED;
  70. }
  71. /**
  72. * Initialize the MSIX entry table.
  73. */
  74. static void
  75. bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
  76. int mask, int max_bit)
  77. {
  78. int i;
  79. int match = 0x00000001;
  80. for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
  81. if (mask & match) {
  82. bfad->msix_tab[bfad->nvec].msix.entry = i;
  83. bfad->msix_tab[bfad->nvec].bfad = bfad;
  84. msix_entries[bfad->nvec].entry = i;
  85. bfad->nvec++;
  86. }
  87. match <<= 1;
  88. }
  89. }
  90. int
  91. bfad_install_msix_handler(struct bfad_s *bfad)
  92. {
  93. int i, error = 0;
  94. for (i = 0; i < bfad->nvec; i++) {
  95. error = request_irq(bfad->msix_tab[i].msix.vector,
  96. (irq_handler_t) bfad_msix, 0,
  97. BFAD_DRIVER_NAME, &bfad->msix_tab[i]);
  98. bfa_trc(bfad, i);
  99. bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
  100. if (error) {
  101. int j;
  102. for (j = 0; j < i; j++)
  103. free_irq(bfad->msix_tab[j].msix.vector,
  104. &bfad->msix_tab[j]);
  105. return 1;
  106. }
  107. }
  108. return 0;
  109. }
  110. /**
  111. * Setup MSIX based interrupt.
  112. */
  113. int
  114. bfad_setup_intr(struct bfad_s *bfad)
  115. {
  116. int error = 0;
  117. u32 mask = 0, i, num_bit = 0, max_bit = 0;
  118. struct msix_entry msix_entries[MAX_MSIX_ENTRY];
  119. /* Call BFA to get the msix map for this PCI function. */
  120. bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
  121. /* Set up the msix entry table */
  122. bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
  123. if (!msix_disable) {
  124. error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
  125. if (error) {
  126. /*
  127. * Only error number of vector is available.
  128. * We don't have a mechanism to map multiple
  129. * interrupts into one vector, so even if we
  130. * can try to request less vectors, we don't
  131. * know how to associate interrupt events to
  132. * vectors. Linux doesn't dupicate vectors
  133. * in the MSIX table for this case.
  134. */
  135. printk(KERN_WARNING "bfad%d: "
  136. "pci_enable_msix failed (%d),"
  137. " use line based.\n", bfad->inst_no, error);
  138. goto line_based;
  139. }
  140. /* Save the vectors */
  141. for (i = 0; i < bfad->nvec; i++) {
  142. bfa_trc(bfad, msix_entries[i].vector);
  143. bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
  144. }
  145. bfa_msix_init(&bfad->bfa, bfad->nvec);
  146. bfad->bfad_flags |= BFAD_MSIX_ON;
  147. return error;
  148. }
  149. line_based:
  150. error = 0;
  151. if (request_irq
  152. (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
  153. BFAD_DRIVER_NAME, bfad) != 0) {
  154. /* Enable interrupt handler failed */
  155. return 1;
  156. }
  157. return error;
  158. }
  159. void
  160. bfad_remove_intr(struct bfad_s *bfad)
  161. {
  162. int i;
  163. if (bfad->bfad_flags & BFAD_MSIX_ON) {
  164. for (i = 0; i < bfad->nvec; i++)
  165. free_irq(bfad->msix_tab[i].msix.vector,
  166. &bfad->msix_tab[i]);
  167. pci_disable_msix(bfad->pcidev);
  168. bfad->bfad_flags &= ~BFAD_MSIX_ON;
  169. } else {
  170. free_irq(bfad->pcidev->irq, bfad);
  171. }
  172. }