csio_isr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/cpumask.h>
  38. #include <linux/string.h>
  39. #include "csio_init.h"
  40. #include "csio_hw.h"
  41. static irqreturn_t
  42. csio_nondata_isr(int irq, void *dev_id)
  43. {
  44. struct csio_hw *hw = (struct csio_hw *) dev_id;
  45. int rv;
  46. unsigned long flags;
  47. if (unlikely(!hw))
  48. return IRQ_NONE;
  49. if (unlikely(pci_channel_offline(hw->pdev))) {
  50. CSIO_INC_STATS(hw, n_pcich_offline);
  51. return IRQ_NONE;
  52. }
  53. spin_lock_irqsave(&hw->lock, flags);
  54. csio_hw_slow_intr_handler(hw);
  55. rv = csio_mb_isr_handler(hw);
  56. if (rv == 0 && !(hw->flags & CSIO_HWF_FWEVT_PENDING)) {
  57. hw->flags |= CSIO_HWF_FWEVT_PENDING;
  58. spin_unlock_irqrestore(&hw->lock, flags);
  59. schedule_work(&hw->evtq_work);
  60. return IRQ_HANDLED;
  61. }
  62. spin_unlock_irqrestore(&hw->lock, flags);
  63. return IRQ_HANDLED;
  64. }
  65. /*
  66. * csio_fwevt_handler - Common FW event handler routine.
  67. * @hw: HW module.
  68. *
  69. * This is the ISR for FW events. It is shared b/w MSIX
  70. * and INTx handlers.
  71. */
  72. static void
  73. csio_fwevt_handler(struct csio_hw *hw)
  74. {
  75. int rv;
  76. unsigned long flags;
  77. rv = csio_fwevtq_handler(hw);
  78. spin_lock_irqsave(&hw->lock, flags);
  79. if (rv == 0 && !(hw->flags & CSIO_HWF_FWEVT_PENDING)) {
  80. hw->flags |= CSIO_HWF_FWEVT_PENDING;
  81. spin_unlock_irqrestore(&hw->lock, flags);
  82. schedule_work(&hw->evtq_work);
  83. return;
  84. }
  85. spin_unlock_irqrestore(&hw->lock, flags);
  86. } /* csio_fwevt_handler */
  87. /*
  88. * csio_fwevt_isr() - FW events MSIX ISR
  89. * @irq:
  90. * @dev_id:
  91. *
  92. * Process WRs on the FW event queue.
  93. *
  94. */
  95. static irqreturn_t
  96. csio_fwevt_isr(int irq, void *dev_id)
  97. {
  98. struct csio_hw *hw = (struct csio_hw *) dev_id;
  99. if (unlikely(!hw))
  100. return IRQ_NONE;
  101. if (unlikely(pci_channel_offline(hw->pdev))) {
  102. CSIO_INC_STATS(hw, n_pcich_offline);
  103. return IRQ_NONE;
  104. }
  105. csio_fwevt_handler(hw);
  106. return IRQ_HANDLED;
  107. }
  108. /*
  109. * csio_fwevt_isr() - INTx wrapper for handling FW events.
  110. * @irq:
  111. * @dev_id:
  112. */
  113. void
  114. csio_fwevt_intx_handler(struct csio_hw *hw, void *wr, uint32_t len,
  115. struct csio_fl_dma_buf *flb, void *priv)
  116. {
  117. csio_fwevt_handler(hw);
  118. } /* csio_fwevt_intx_handler */
  119. /*
  120. * csio_process_scsi_cmpl - Process a SCSI WR completion.
  121. * @hw: HW module.
  122. * @wr: The completed WR from the ingress queue.
  123. * @len: Length of the WR.
  124. * @flb: Freelist buffer array.
  125. *
  126. */
  127. static void
  128. csio_process_scsi_cmpl(struct csio_hw *hw, void *wr, uint32_t len,
  129. struct csio_fl_dma_buf *flb, void *cbfn_q)
  130. {
  131. struct csio_ioreq *ioreq;
  132. uint8_t *scsiwr;
  133. uint8_t subop;
  134. void *cmnd;
  135. unsigned long flags;
  136. ioreq = csio_scsi_cmpl_handler(hw, wr, len, flb, NULL, &scsiwr);
  137. if (likely(ioreq)) {
  138. if (unlikely(*scsiwr == FW_SCSI_ABRT_CLS_WR)) {
  139. subop = FW_SCSI_ABRT_CLS_WR_SUB_OPCODE_GET(
  140. ((struct fw_scsi_abrt_cls_wr *)
  141. scsiwr)->sub_opcode_to_chk_all_io);
  142. csio_dbg(hw, "%s cmpl recvd ioreq:%p status:%d\n",
  143. subop ? "Close" : "Abort",
  144. ioreq, ioreq->wr_status);
  145. spin_lock_irqsave(&hw->lock, flags);
  146. if (subop)
  147. csio_scsi_closed(ioreq,
  148. (struct list_head *)cbfn_q);
  149. else
  150. csio_scsi_aborted(ioreq,
  151. (struct list_head *)cbfn_q);
  152. /*
  153. * We call scsi_done for I/Os that driver thinks aborts
  154. * have timed out. If there is a race caused by FW
  155. * completing abort at the exact same time that the
  156. * driver has deteced the abort timeout, the following
  157. * check prevents calling of scsi_done twice for the
  158. * same command: once from the eh_abort_handler, another
  159. * from csio_scsi_isr_handler(). This also avoids the
  160. * need to check if csio_scsi_cmnd(req) is NULL in the
  161. * fast path.
  162. */
  163. cmnd = csio_scsi_cmnd(ioreq);
  164. if (unlikely(cmnd == NULL))
  165. list_del_init(&ioreq->sm.sm_list);
  166. spin_unlock_irqrestore(&hw->lock, flags);
  167. if (unlikely(cmnd == NULL))
  168. csio_put_scsi_ioreq_lock(hw,
  169. csio_hw_to_scsim(hw), ioreq);
  170. } else {
  171. spin_lock_irqsave(&hw->lock, flags);
  172. csio_scsi_completed(ioreq, (struct list_head *)cbfn_q);
  173. spin_unlock_irqrestore(&hw->lock, flags);
  174. }
  175. }
  176. }
  177. /*
  178. * csio_scsi_isr_handler() - Common SCSI ISR handler.
  179. * @iq: Ingress queue pointer.
  180. *
  181. * Processes SCSI completions on the SCSI IQ indicated by scm->iq_idx
  182. * by calling csio_wr_process_iq_idx. If there are completions on the
  183. * isr_cbfn_q, yank them out into a local queue and call their io_cbfns.
  184. * Once done, add these completions onto the freelist.
  185. * This routine is shared b/w MSIX and INTx.
  186. */
  187. static inline irqreturn_t
  188. csio_scsi_isr_handler(struct csio_q *iq)
  189. {
  190. struct csio_hw *hw = (struct csio_hw *)iq->owner;
  191. LIST_HEAD(cbfn_q);
  192. struct list_head *tmp;
  193. struct csio_scsim *scm;
  194. struct csio_ioreq *ioreq;
  195. int isr_completions = 0;
  196. scm = csio_hw_to_scsim(hw);
  197. if (unlikely(csio_wr_process_iq(hw, iq, csio_process_scsi_cmpl,
  198. &cbfn_q) != 0))
  199. return IRQ_NONE;
  200. /* Call back the completion routines */
  201. list_for_each(tmp, &cbfn_q) {
  202. ioreq = (struct csio_ioreq *)tmp;
  203. isr_completions++;
  204. ioreq->io_cbfn(hw, ioreq);
  205. /* Release ddp buffer if used for this req */
  206. if (unlikely(ioreq->dcopy))
  207. csio_put_scsi_ddp_list_lock(hw, scm, &ioreq->gen_list,
  208. ioreq->nsge);
  209. }
  210. if (isr_completions) {
  211. /* Return the ioreqs back to ioreq->freelist */
  212. csio_put_scsi_ioreq_list_lock(hw, scm, &cbfn_q,
  213. isr_completions);
  214. }
  215. return IRQ_HANDLED;
  216. }
  217. /*
  218. * csio_scsi_isr() - SCSI MSIX handler
  219. * @irq:
  220. * @dev_id:
  221. *
  222. * This is the top level SCSI MSIX handler. Calls csio_scsi_isr_handler()
  223. * for handling SCSI completions.
  224. */
  225. static irqreturn_t
  226. csio_scsi_isr(int irq, void *dev_id)
  227. {
  228. struct csio_q *iq = (struct csio_q *) dev_id;
  229. struct csio_hw *hw;
  230. if (unlikely(!iq))
  231. return IRQ_NONE;
  232. hw = (struct csio_hw *)iq->owner;
  233. if (unlikely(pci_channel_offline(hw->pdev))) {
  234. CSIO_INC_STATS(hw, n_pcich_offline);
  235. return IRQ_NONE;
  236. }
  237. csio_scsi_isr_handler(iq);
  238. return IRQ_HANDLED;
  239. }
  240. /*
  241. * csio_scsi_intx_handler() - SCSI INTx handler
  242. * @irq:
  243. * @dev_id:
  244. *
  245. * This is the top level SCSI INTx handler. Calls csio_scsi_isr_handler()
  246. * for handling SCSI completions.
  247. */
  248. void
  249. csio_scsi_intx_handler(struct csio_hw *hw, void *wr, uint32_t len,
  250. struct csio_fl_dma_buf *flb, void *priv)
  251. {
  252. struct csio_q *iq = priv;
  253. csio_scsi_isr_handler(iq);
  254. } /* csio_scsi_intx_handler */
  255. /*
  256. * csio_fcoe_isr() - INTx/MSI interrupt service routine for FCoE.
  257. * @irq:
  258. * @dev_id:
  259. *
  260. *
  261. */
  262. static irqreturn_t
  263. csio_fcoe_isr(int irq, void *dev_id)
  264. {
  265. struct csio_hw *hw = (struct csio_hw *) dev_id;
  266. struct csio_q *intx_q = NULL;
  267. int rv;
  268. irqreturn_t ret = IRQ_NONE;
  269. unsigned long flags;
  270. if (unlikely(!hw))
  271. return IRQ_NONE;
  272. if (unlikely(pci_channel_offline(hw->pdev))) {
  273. CSIO_INC_STATS(hw, n_pcich_offline);
  274. return IRQ_NONE;
  275. }
  276. /* Disable the interrupt for this PCI function. */
  277. if (hw->intr_mode == CSIO_IM_INTX)
  278. csio_wr_reg32(hw, 0, MYPF_REG(PCIE_PF_CLI_A));
  279. /*
  280. * The read in the following function will flush the
  281. * above write.
  282. */
  283. if (csio_hw_slow_intr_handler(hw))
  284. ret = IRQ_HANDLED;
  285. /* Get the INTx Forward interrupt IQ. */
  286. intx_q = csio_get_q(hw, hw->intr_iq_idx);
  287. CSIO_DB_ASSERT(intx_q);
  288. /* IQ handler is not possible for intx_q, hence pass in NULL */
  289. if (likely(csio_wr_process_iq(hw, intx_q, NULL, NULL) == 0))
  290. ret = IRQ_HANDLED;
  291. spin_lock_irqsave(&hw->lock, flags);
  292. rv = csio_mb_isr_handler(hw);
  293. if (rv == 0 && !(hw->flags & CSIO_HWF_FWEVT_PENDING)) {
  294. hw->flags |= CSIO_HWF_FWEVT_PENDING;
  295. spin_unlock_irqrestore(&hw->lock, flags);
  296. schedule_work(&hw->evtq_work);
  297. return IRQ_HANDLED;
  298. }
  299. spin_unlock_irqrestore(&hw->lock, flags);
  300. return ret;
  301. }
  302. static void
  303. csio_add_msix_desc(struct csio_hw *hw)
  304. {
  305. int i;
  306. struct csio_msix_entries *entryp = &hw->msix_entries[0];
  307. int k = CSIO_EXTRA_VECS;
  308. int len = sizeof(entryp->desc) - 1;
  309. int cnt = hw->num_sqsets + k;
  310. /* Non-data vector */
  311. memset(entryp->desc, 0, len + 1);
  312. snprintf(entryp->desc, len, "csio-%02x:%02x:%x-nondata",
  313. CSIO_PCI_BUS(hw), CSIO_PCI_DEV(hw), CSIO_PCI_FUNC(hw));
  314. entryp++;
  315. memset(entryp->desc, 0, len + 1);
  316. snprintf(entryp->desc, len, "csio-%02x:%02x:%x-fwevt",
  317. CSIO_PCI_BUS(hw), CSIO_PCI_DEV(hw), CSIO_PCI_FUNC(hw));
  318. entryp++;
  319. /* Name SCSI vecs */
  320. for (i = k; i < cnt; i++, entryp++) {
  321. memset(entryp->desc, 0, len + 1);
  322. snprintf(entryp->desc, len, "csio-%02x:%02x:%x-scsi%d",
  323. CSIO_PCI_BUS(hw), CSIO_PCI_DEV(hw),
  324. CSIO_PCI_FUNC(hw), i - CSIO_EXTRA_VECS);
  325. }
  326. }
  327. int
  328. csio_request_irqs(struct csio_hw *hw)
  329. {
  330. int rv, i, j, k = 0;
  331. struct csio_msix_entries *entryp = &hw->msix_entries[0];
  332. struct csio_scsi_cpu_info *info;
  333. struct pci_dev *pdev = hw->pdev;
  334. if (hw->intr_mode != CSIO_IM_MSIX) {
  335. rv = request_irq(pci_irq_vector(pdev, 0), csio_fcoe_isr,
  336. hw->intr_mode == CSIO_IM_MSI ? 0 : IRQF_SHARED,
  337. KBUILD_MODNAME, hw);
  338. if (rv) {
  339. csio_err(hw, "Failed to allocate interrupt line.\n");
  340. goto out_free_irqs;
  341. }
  342. goto out;
  343. }
  344. /* Add the MSIX vector descriptions */
  345. csio_add_msix_desc(hw);
  346. rv = request_irq(pci_irq_vector(pdev, k), csio_nondata_isr, 0,
  347. entryp[k].desc, hw);
  348. if (rv) {
  349. csio_err(hw, "IRQ request failed for vec %d err:%d\n",
  350. pci_irq_vector(pdev, k), rv);
  351. goto out_free_irqs;
  352. }
  353. entryp[k++].dev_id = hw;
  354. rv = request_irq(pci_irq_vector(pdev, k), csio_fwevt_isr, 0,
  355. entryp[k].desc, hw);
  356. if (rv) {
  357. csio_err(hw, "IRQ request failed for vec %d err:%d\n",
  358. pci_irq_vector(pdev, k), rv);
  359. goto out_free_irqs;
  360. }
  361. entryp[k++].dev_id = (void *)hw;
  362. /* Allocate IRQs for SCSI */
  363. for (i = 0; i < hw->num_pports; i++) {
  364. info = &hw->scsi_cpu_info[i];
  365. for (j = 0; j < info->max_cpus; j++, k++) {
  366. struct csio_scsi_qset *sqset = &hw->sqset[i][j];
  367. struct csio_q *q = hw->wrm.q_arr[sqset->iq_idx];
  368. rv = request_irq(pci_irq_vector(pdev, k), csio_scsi_isr, 0,
  369. entryp[k].desc, q);
  370. if (rv) {
  371. csio_err(hw,
  372. "IRQ request failed for vec %d err:%d\n",
  373. pci_irq_vector(pdev, k), rv);
  374. goto out_free_irqs;
  375. }
  376. entryp[k].dev_id = q;
  377. } /* for all scsi cpus */
  378. } /* for all ports */
  379. out:
  380. hw->flags |= CSIO_HWF_HOST_INTR_ENABLED;
  381. return 0;
  382. out_free_irqs:
  383. for (i = 0; i < k; i++)
  384. free_irq(pci_irq_vector(pdev, i), hw->msix_entries[i].dev_id);
  385. pci_free_irq_vectors(hw->pdev);
  386. return -EINVAL;
  387. }
  388. /* Reduce per-port max possible CPUs */
  389. static void
  390. csio_reduce_sqsets(struct csio_hw *hw, int cnt)
  391. {
  392. int i;
  393. struct csio_scsi_cpu_info *info;
  394. while (cnt < hw->num_sqsets) {
  395. for (i = 0; i < hw->num_pports; i++) {
  396. info = &hw->scsi_cpu_info[i];
  397. if (info->max_cpus > 1) {
  398. info->max_cpus--;
  399. hw->num_sqsets--;
  400. if (hw->num_sqsets <= cnt)
  401. break;
  402. }
  403. }
  404. }
  405. csio_dbg(hw, "Reduced sqsets to %d\n", hw->num_sqsets);
  406. }
  407. static int
  408. csio_enable_msix(struct csio_hw *hw)
  409. {
  410. int i, j, k, n, min, cnt;
  411. int extra = CSIO_EXTRA_VECS;
  412. struct csio_scsi_cpu_info *info;
  413. struct irq_affinity desc = { .pre_vectors = 2 };
  414. min = hw->num_pports + extra;
  415. cnt = hw->num_sqsets + extra;
  416. /* Max vectors required based on #niqs configured in fw */
  417. if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS || !csio_is_hw_master(hw))
  418. cnt = min_t(uint8_t, hw->cfg_niq, cnt);
  419. csio_dbg(hw, "FW supp #niq:%d, trying %d msix's\n", hw->cfg_niq, cnt);
  420. cnt = pci_alloc_irq_vectors_affinity(hw->pdev, min, cnt,
  421. PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc);
  422. if (cnt < 0)
  423. return cnt;
  424. if (cnt < (hw->num_sqsets + extra)) {
  425. csio_dbg(hw, "Reducing sqsets to %d\n", cnt - extra);
  426. csio_reduce_sqsets(hw, cnt - extra);
  427. }
  428. /* Distribute vectors */
  429. k = 0;
  430. csio_set_nondata_intr_idx(hw, k);
  431. csio_set_mb_intr_idx(csio_hw_to_mbm(hw), k++);
  432. csio_set_fwevt_intr_idx(hw, k++);
  433. for (i = 0; i < hw->num_pports; i++) {
  434. info = &hw->scsi_cpu_info[i];
  435. for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
  436. n = (j % info->max_cpus) + k;
  437. hw->sqset[i][j].intr_idx = n;
  438. }
  439. k += info->max_cpus;
  440. }
  441. return 0;
  442. }
  443. void
  444. csio_intr_enable(struct csio_hw *hw)
  445. {
  446. hw->intr_mode = CSIO_IM_NONE;
  447. hw->flags &= ~CSIO_HWF_HOST_INTR_ENABLED;
  448. /* Try MSIX, then MSI or fall back to INTx */
  449. if ((csio_msi == 2) && !csio_enable_msix(hw))
  450. hw->intr_mode = CSIO_IM_MSIX;
  451. else {
  452. /* Max iqs required based on #niqs configured in fw */
  453. if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS ||
  454. !csio_is_hw_master(hw)) {
  455. int extra = CSIO_EXTRA_MSI_IQS;
  456. if (hw->cfg_niq < (hw->num_sqsets + extra)) {
  457. csio_dbg(hw, "Reducing sqsets to %d\n",
  458. hw->cfg_niq - extra);
  459. csio_reduce_sqsets(hw, hw->cfg_niq - extra);
  460. }
  461. }
  462. if ((csio_msi == 1) && !pci_enable_msi(hw->pdev))
  463. hw->intr_mode = CSIO_IM_MSI;
  464. else
  465. hw->intr_mode = CSIO_IM_INTX;
  466. }
  467. csio_dbg(hw, "Using %s interrupt mode.\n",
  468. (hw->intr_mode == CSIO_IM_MSIX) ? "MSIX" :
  469. ((hw->intr_mode == CSIO_IM_MSI) ? "MSI" : "INTx"));
  470. }
  471. void
  472. csio_intr_disable(struct csio_hw *hw, bool free)
  473. {
  474. csio_hw_intr_disable(hw);
  475. if (free) {
  476. int i;
  477. switch (hw->intr_mode) {
  478. case CSIO_IM_MSIX:
  479. for (i = 0; i < hw->num_sqsets + CSIO_EXTRA_VECS; i++) {
  480. free_irq(pci_irq_vector(hw->pdev, i),
  481. hw->msix_entries[i].dev_id);
  482. }
  483. break;
  484. case CSIO_IM_MSI:
  485. case CSIO_IM_INTX:
  486. free_irq(pci_irq_vector(hw->pdev, 0), hw);
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. pci_free_irq_vectors(hw->pdev);
  493. hw->intr_mode = CSIO_IM_NONE;
  494. hw->flags &= ~CSIO_HWF_HOST_INTR_ENABLED;
  495. }