fnic_isr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. */
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <scsi/libfc.h>
  23. #include <scsi/fc_frame.h>
  24. #include "vnic_dev.h"
  25. #include "vnic_intr.h"
  26. #include "vnic_stats.h"
  27. #include "fnic_io.h"
  28. #include "fnic.h"
  29. static irqreturn_t fnic_isr_legacy(int irq, void *data)
  30. {
  31. struct fnic *fnic = data;
  32. u32 pba;
  33. unsigned long work_done = 0;
  34. pba = vnic_intr_legacy_pba(fnic->legacy_pba);
  35. if (!pba)
  36. return IRQ_NONE;
  37. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  38. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  39. if (pba & (1 << FNIC_INTX_NOTIFY)) {
  40. vnic_intr_return_all_credits(&fnic->intr[FNIC_INTX_NOTIFY]);
  41. fnic_handle_link_event(fnic);
  42. }
  43. if (pba & (1 << FNIC_INTX_ERR)) {
  44. vnic_intr_return_all_credits(&fnic->intr[FNIC_INTX_ERR]);
  45. fnic_log_q_error(fnic);
  46. }
  47. if (pba & (1 << FNIC_INTX_WQ_RQ_COPYWQ)) {
  48. work_done += fnic_wq_copy_cmpl_handler(fnic, -1);
  49. work_done += fnic_wq_cmpl_handler(fnic, -1);
  50. work_done += fnic_rq_cmpl_handler(fnic, -1);
  51. vnic_intr_return_credits(&fnic->intr[FNIC_INTX_WQ_RQ_COPYWQ],
  52. work_done,
  53. 1 /* unmask intr */,
  54. 1 /* reset intr timer */);
  55. }
  56. return IRQ_HANDLED;
  57. }
  58. static irqreturn_t fnic_isr_msi(int irq, void *data)
  59. {
  60. struct fnic *fnic = data;
  61. unsigned long work_done = 0;
  62. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  63. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  64. work_done += fnic_wq_copy_cmpl_handler(fnic, -1);
  65. work_done += fnic_wq_cmpl_handler(fnic, -1);
  66. work_done += fnic_rq_cmpl_handler(fnic, -1);
  67. vnic_intr_return_credits(&fnic->intr[0],
  68. work_done,
  69. 1 /* unmask intr */,
  70. 1 /* reset intr timer */);
  71. return IRQ_HANDLED;
  72. }
  73. static irqreturn_t fnic_isr_msix_rq(int irq, void *data)
  74. {
  75. struct fnic *fnic = data;
  76. unsigned long rq_work_done = 0;
  77. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  78. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  79. rq_work_done = fnic_rq_cmpl_handler(fnic, -1);
  80. vnic_intr_return_credits(&fnic->intr[FNIC_MSIX_RQ],
  81. rq_work_done,
  82. 1 /* unmask intr */,
  83. 1 /* reset intr timer */);
  84. return IRQ_HANDLED;
  85. }
  86. static irqreturn_t fnic_isr_msix_wq(int irq, void *data)
  87. {
  88. struct fnic *fnic = data;
  89. unsigned long wq_work_done = 0;
  90. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  91. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  92. wq_work_done = fnic_wq_cmpl_handler(fnic, -1);
  93. vnic_intr_return_credits(&fnic->intr[FNIC_MSIX_WQ],
  94. wq_work_done,
  95. 1 /* unmask intr */,
  96. 1 /* reset intr timer */);
  97. return IRQ_HANDLED;
  98. }
  99. static irqreturn_t fnic_isr_msix_wq_copy(int irq, void *data)
  100. {
  101. struct fnic *fnic = data;
  102. unsigned long wq_copy_work_done = 0;
  103. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  104. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  105. wq_copy_work_done = fnic_wq_copy_cmpl_handler(fnic, -1);
  106. vnic_intr_return_credits(&fnic->intr[FNIC_MSIX_WQ_COPY],
  107. wq_copy_work_done,
  108. 1 /* unmask intr */,
  109. 1 /* reset intr timer */);
  110. return IRQ_HANDLED;
  111. }
  112. static irqreturn_t fnic_isr_msix_err_notify(int irq, void *data)
  113. {
  114. struct fnic *fnic = data;
  115. fnic->fnic_stats.misc_stats.last_isr_time = jiffies;
  116. atomic64_inc(&fnic->fnic_stats.misc_stats.isr_count);
  117. vnic_intr_return_all_credits(&fnic->intr[FNIC_MSIX_ERR_NOTIFY]);
  118. fnic_log_q_error(fnic);
  119. fnic_handle_link_event(fnic);
  120. return IRQ_HANDLED;
  121. }
  122. void fnic_free_intr(struct fnic *fnic)
  123. {
  124. int i;
  125. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  126. case VNIC_DEV_INTR_MODE_INTX:
  127. case VNIC_DEV_INTR_MODE_MSI:
  128. free_irq(fnic->pdev->irq, fnic);
  129. break;
  130. case VNIC_DEV_INTR_MODE_MSIX:
  131. for (i = 0; i < ARRAY_SIZE(fnic->msix); i++)
  132. if (fnic->msix[i].requested)
  133. free_irq(fnic->msix_entry[i].vector,
  134. fnic->msix[i].devid);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. int fnic_request_intr(struct fnic *fnic)
  141. {
  142. int err = 0;
  143. int i;
  144. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  145. case VNIC_DEV_INTR_MODE_INTX:
  146. err = request_irq(fnic->pdev->irq, &fnic_isr_legacy,
  147. IRQF_SHARED, DRV_NAME, fnic);
  148. break;
  149. case VNIC_DEV_INTR_MODE_MSI:
  150. err = request_irq(fnic->pdev->irq, &fnic_isr_msi,
  151. 0, fnic->name, fnic);
  152. break;
  153. case VNIC_DEV_INTR_MODE_MSIX:
  154. sprintf(fnic->msix[FNIC_MSIX_RQ].devname,
  155. "%.11s-fcs-rq", fnic->name);
  156. fnic->msix[FNIC_MSIX_RQ].isr = fnic_isr_msix_rq;
  157. fnic->msix[FNIC_MSIX_RQ].devid = fnic;
  158. sprintf(fnic->msix[FNIC_MSIX_WQ].devname,
  159. "%.11s-fcs-wq", fnic->name);
  160. fnic->msix[FNIC_MSIX_WQ].isr = fnic_isr_msix_wq;
  161. fnic->msix[FNIC_MSIX_WQ].devid = fnic;
  162. sprintf(fnic->msix[FNIC_MSIX_WQ_COPY].devname,
  163. "%.11s-scsi-wq", fnic->name);
  164. fnic->msix[FNIC_MSIX_WQ_COPY].isr = fnic_isr_msix_wq_copy;
  165. fnic->msix[FNIC_MSIX_WQ_COPY].devid = fnic;
  166. sprintf(fnic->msix[FNIC_MSIX_ERR_NOTIFY].devname,
  167. "%.11s-err-notify", fnic->name);
  168. fnic->msix[FNIC_MSIX_ERR_NOTIFY].isr =
  169. fnic_isr_msix_err_notify;
  170. fnic->msix[FNIC_MSIX_ERR_NOTIFY].devid = fnic;
  171. for (i = 0; i < ARRAY_SIZE(fnic->msix); i++) {
  172. err = request_irq(fnic->msix_entry[i].vector,
  173. fnic->msix[i].isr, 0,
  174. fnic->msix[i].devname,
  175. fnic->msix[i].devid);
  176. if (err) {
  177. shost_printk(KERN_ERR, fnic->lport->host,
  178. "MSIX: request_irq"
  179. " failed %d\n", err);
  180. fnic_free_intr(fnic);
  181. break;
  182. }
  183. fnic->msix[i].requested = 1;
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. return err;
  190. }
  191. int fnic_set_intr_mode(struct fnic *fnic)
  192. {
  193. unsigned int n = ARRAY_SIZE(fnic->rq);
  194. unsigned int m = ARRAY_SIZE(fnic->wq);
  195. unsigned int o = ARRAY_SIZE(fnic->wq_copy);
  196. unsigned int i;
  197. /*
  198. * Set interrupt mode (INTx, MSI, MSI-X) depending
  199. * system capabilities.
  200. *
  201. * Try MSI-X first
  202. *
  203. * We need n RQs, m WQs, o Copy WQs, n+m+o CQs, and n+m+o+1 INTRs
  204. * (last INTR is used for WQ/RQ errors and notification area)
  205. */
  206. BUG_ON(ARRAY_SIZE(fnic->msix_entry) < n + m + o + 1);
  207. for (i = 0; i < n + m + o + 1; i++)
  208. fnic->msix_entry[i].entry = i;
  209. if (fnic->rq_count >= n &&
  210. fnic->raw_wq_count >= m &&
  211. fnic->wq_copy_count >= o &&
  212. fnic->cq_count >= n + m + o) {
  213. if (!pci_enable_msix_exact(fnic->pdev, fnic->msix_entry,
  214. n + m + o + 1)) {
  215. fnic->rq_count = n;
  216. fnic->raw_wq_count = m;
  217. fnic->wq_copy_count = o;
  218. fnic->wq_count = m + o;
  219. fnic->cq_count = n + m + o;
  220. fnic->intr_count = n + m + o + 1;
  221. fnic->err_intr_offset = FNIC_MSIX_ERR_NOTIFY;
  222. FNIC_ISR_DBG(KERN_DEBUG, fnic->lport->host,
  223. "Using MSI-X Interrupts\n");
  224. vnic_dev_set_intr_mode(fnic->vdev,
  225. VNIC_DEV_INTR_MODE_MSIX);
  226. return 0;
  227. }
  228. }
  229. /*
  230. * Next try MSI
  231. * We need 1 RQ, 1 WQ, 1 WQ_COPY, 3 CQs, and 1 INTR
  232. */
  233. if (fnic->rq_count >= 1 &&
  234. fnic->raw_wq_count >= 1 &&
  235. fnic->wq_copy_count >= 1 &&
  236. fnic->cq_count >= 3 &&
  237. fnic->intr_count >= 1 &&
  238. !pci_enable_msi(fnic->pdev)) {
  239. fnic->rq_count = 1;
  240. fnic->raw_wq_count = 1;
  241. fnic->wq_copy_count = 1;
  242. fnic->wq_count = 2;
  243. fnic->cq_count = 3;
  244. fnic->intr_count = 1;
  245. fnic->err_intr_offset = 0;
  246. FNIC_ISR_DBG(KERN_DEBUG, fnic->lport->host,
  247. "Using MSI Interrupts\n");
  248. vnic_dev_set_intr_mode(fnic->vdev, VNIC_DEV_INTR_MODE_MSI);
  249. return 0;
  250. }
  251. /*
  252. * Next try INTx
  253. * We need 1 RQ, 1 WQ, 1 WQ_COPY, 3 CQs, and 3 INTRs
  254. * 1 INTR is used for all 3 queues, 1 INTR for queue errors
  255. * 1 INTR for notification area
  256. */
  257. if (fnic->rq_count >= 1 &&
  258. fnic->raw_wq_count >= 1 &&
  259. fnic->wq_copy_count >= 1 &&
  260. fnic->cq_count >= 3 &&
  261. fnic->intr_count >= 3) {
  262. fnic->rq_count = 1;
  263. fnic->raw_wq_count = 1;
  264. fnic->wq_copy_count = 1;
  265. fnic->cq_count = 3;
  266. fnic->intr_count = 3;
  267. FNIC_ISR_DBG(KERN_DEBUG, fnic->lport->host,
  268. "Using Legacy Interrupts\n");
  269. vnic_dev_set_intr_mode(fnic->vdev, VNIC_DEV_INTR_MODE_INTX);
  270. return 0;
  271. }
  272. vnic_dev_set_intr_mode(fnic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  273. return -EINVAL;
  274. }
  275. void fnic_clear_intr_mode(struct fnic *fnic)
  276. {
  277. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  278. case VNIC_DEV_INTR_MODE_MSIX:
  279. pci_disable_msix(fnic->pdev);
  280. break;
  281. case VNIC_DEV_INTR_MODE_MSI:
  282. pci_disable_msi(fnic->pdev);
  283. break;
  284. default:
  285. break;
  286. }
  287. vnic_dev_set_intr_mode(fnic->vdev, VNIC_DEV_INTR_MODE_INTX);
  288. }