6c5bcfb62392d409d0bd79cf9eea1e87756af176
[dpdk.git] / drivers / net / bnxt / bnxt_irq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_cycles.h>
9 #include <rte_malloc.h>
10
11 #include "bnxt.h"
12 #include "bnxt_irq.h"
13 #include "bnxt_ring.h"
14 #include "hsi_struct_def_dpdk.h"
15
16 /*
17  * Interrupts
18  */
19
20 void bnxt_int_handler(void *param)
21 {
22         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
23         struct bnxt *bp = eth_dev->data->dev_private;
24         struct bnxt_cp_ring_info *cpr = bp->async_cp_ring;
25         struct cmpl_base *cmp;
26         uint32_t raw_cons;
27         uint32_t cons;
28
29         if (cpr == NULL)
30                 return;
31
32         raw_cons = cpr->cp_raw_cons;
33         while (1) {
34                 if (!cpr || !cpr->cp_ring_struct || !cpr->cp_db.doorbell)
35                         return;
36
37                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
38                 cmp = &cpr->cp_desc_ring[cons];
39
40                 if (!CMP_VALID(cmp, raw_cons, cpr->cp_ring_struct))
41                         break;
42
43                 bnxt_event_hwrm_resp_handler(bp, cmp);
44                 raw_cons = NEXT_RAW_CMP(raw_cons);
45         }
46
47         cpr->cp_raw_cons = raw_cons;
48         if (BNXT_HAS_NQ(bp))
49                 bnxt_db_nq_arm(cpr);
50         else
51                 B_CP_DB_REARM(cpr, cpr->cp_raw_cons);
52 }
53
54 int bnxt_free_int(struct bnxt *bp)
55 {
56         struct rte_intr_handle *intr_handle = &bp->pdev->intr_handle;
57         struct bnxt_irq *irq = bp->irq_tbl;
58         int rc = 0;
59
60         if (!irq)
61                 return 0;
62
63         if (irq->requested) {
64                 int count = 0;
65
66                 /*
67                  * Callback deregistration will fail with rc -EAGAIN if the
68                  * callback is currently active. Retry every 50 ms until
69                  * successful or 500 ms has elapsed.
70                  */
71                 do {
72                         rc = rte_intr_callback_unregister(intr_handle,
73                                                           irq->handler,
74                                                           bp->eth_dev);
75                         if (rc >= 0) {
76                                 irq->requested = 0;
77                                 break;
78                         }
79                         rte_delay_ms(50);
80                 } while (count++ < 10);
81
82                 if (rc < 0) {
83                         PMD_DRV_LOG(ERR, "irq cb unregister failed rc: %d\n",
84                                     rc);
85                         return rc;
86                 }
87         }
88
89         rte_free(bp->irq_tbl);
90         bp->irq_tbl = NULL;
91
92         return 0;
93 }
94
95 void bnxt_disable_int(struct bnxt *bp)
96 {
97         struct bnxt_cp_ring_info *cpr = bp->async_cp_ring;
98
99         if (BNXT_NUM_ASYNC_CPR(bp) == 0)
100                 return;
101
102         if (!cpr || !cpr->cp_db.doorbell)
103                 return;
104
105         /* Only the default completion ring */
106         if (BNXT_HAS_NQ(bp))
107                 bnxt_db_nq(cpr);
108         else
109                 B_CP_DB_DISARM(cpr);
110 }
111
112 void bnxt_enable_int(struct bnxt *bp)
113 {
114         struct bnxt_cp_ring_info *cpr = bp->async_cp_ring;
115
116         if (BNXT_NUM_ASYNC_CPR(bp) == 0)
117                 return;
118
119         if (!cpr || !cpr->cp_db.doorbell)
120                 return;
121
122         /* Only the default completion ring */
123         if (BNXT_HAS_NQ(bp))
124                 bnxt_db_nq_arm(cpr);
125         else
126                 B_CP_DB_ARM(cpr);
127 }
128
129 int bnxt_setup_int(struct bnxt *bp)
130 {
131         uint16_t total_vecs;
132         const int len = sizeof(bp->irq_tbl[0].name);
133         int i;
134
135         /* DPDK host only supports 1 MSI-X vector */
136         total_vecs = 1;
137         bp->irq_tbl = rte_calloc("bnxt_irq_tbl", total_vecs,
138                                  sizeof(struct bnxt_irq), 0);
139         if (bp->irq_tbl) {
140                 for (i = 0; i < total_vecs; i++) {
141                         bp->irq_tbl[i].vector = i;
142                         snprintf(bp->irq_tbl[i].name, len,
143                                  "%s-%d", bp->eth_dev->device->name, i);
144                         bp->irq_tbl[i].handler = bnxt_int_handler;
145                 }
146         } else {
147                 PMD_DRV_LOG(ERR, "bnxt_irq_tbl setup failed\n");
148                 return -ENOMEM;
149         }
150
151         return 0;
152 }
153
154 int bnxt_request_int(struct bnxt *bp)
155 {
156         struct rte_intr_handle *intr_handle = &bp->pdev->intr_handle;
157         struct bnxt_irq *irq = bp->irq_tbl;
158         int rc = 0;
159
160         if (!irq)
161                 return 0;
162
163         if (!irq->requested) {
164                 rc = rte_intr_callback_register(intr_handle,
165                                                 irq->handler,
166                                                 bp->eth_dev);
167                 if (!rc)
168                         irq->requested = 1;
169         }
170
171         return rc;
172 }