net/mlx4: use SPDX tags on 6WIND copyrighted files
[dpdk.git] / drivers / net / mlx4 / mlx4_intr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox
4  */
5
6 /**
7  * @file
8  * Interrupts handling for mlx4 driver.
9  */
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15
16 /* Verbs headers do not support -pedantic. */
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic ignored "-Wpedantic"
19 #endif
20 #include <infiniband/verbs.h>
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic error "-Wpedantic"
23 #endif
24
25 #include <rte_alarm.h>
26 #include <rte_errno.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_io.h>
29 #include <rte_interrupts.h>
30
31 #include "mlx4.h"
32 #include "mlx4_rxtx.h"
33 #include "mlx4_utils.h"
34
35 static int mlx4_link_status_check(struct priv *priv);
36
37 /**
38  * Clean up Rx interrupts handler.
39  *
40  * @param priv
41  *   Pointer to private structure.
42  */
43 static void
44 mlx4_rx_intr_vec_disable(struct priv *priv)
45 {
46         struct rte_intr_handle *intr_handle = &priv->intr_handle;
47
48         rte_intr_free_epoll_fd(intr_handle);
49         free(intr_handle->intr_vec);
50         intr_handle->nb_efd = 0;
51         intr_handle->intr_vec = NULL;
52 }
53
54 /**
55  * Allocate queue vector and fill epoll fd list for Rx interrupts.
56  *
57  * @param priv
58  *   Pointer to private structure.
59  *
60  * @return
61  *   0 on success, negative errno value otherwise and rte_errno is set.
62  */
63 static int
64 mlx4_rx_intr_vec_enable(struct priv *priv)
65 {
66         unsigned int i;
67         unsigned int rxqs_n = priv->dev->data->nb_rx_queues;
68         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
69         unsigned int count = 0;
70         struct rte_intr_handle *intr_handle = &priv->intr_handle;
71
72         mlx4_rx_intr_vec_disable(priv);
73         intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
74         if (intr_handle->intr_vec == NULL) {
75                 rte_errno = ENOMEM;
76                 ERROR("failed to allocate memory for interrupt vector,"
77                       " Rx interrupts will not be supported");
78                 return -rte_errno;
79         }
80         for (i = 0; i != n; ++i) {
81                 struct rxq *rxq = priv->dev->data->rx_queues[i];
82
83                 /* Skip queues that cannot request interrupts. */
84                 if (!rxq || !rxq->channel) {
85                         /* Use invalid intr_vec[] index to disable entry. */
86                         intr_handle->intr_vec[i] =
87                                 RTE_INTR_VEC_RXTX_OFFSET +
88                                 RTE_MAX_RXTX_INTR_VEC_ID;
89                         continue;
90                 }
91                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
92                         rte_errno = E2BIG;
93                         ERROR("too many Rx queues for interrupt vector size"
94                               " (%d), Rx interrupts cannot be enabled",
95                               RTE_MAX_RXTX_INTR_VEC_ID);
96                         mlx4_rx_intr_vec_disable(priv);
97                         return -rte_errno;
98                 }
99                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
100                 intr_handle->efds[count] = rxq->channel->fd;
101                 count++;
102         }
103         if (!count)
104                 mlx4_rx_intr_vec_disable(priv);
105         else
106                 intr_handle->nb_efd = count;
107         return 0;
108 }
109
110 /**
111  * Process scheduled link status check.
112  *
113  * If LSC interrupts are requested, process related callback.
114  *
115  * @param priv
116  *   Pointer to private structure.
117  */
118 static void
119 mlx4_link_status_alarm(struct priv *priv)
120 {
121         const struct rte_intr_conf *const intr_conf =
122                 &priv->dev->data->dev_conf.intr_conf;
123
124         assert(priv->intr_alarm == 1);
125         priv->intr_alarm = 0;
126         if (intr_conf->lsc && !mlx4_link_status_check(priv))
127                 _rte_eth_dev_callback_process(priv->dev,
128                                               RTE_ETH_EVENT_INTR_LSC,
129                                               NULL);
130 }
131
132 /**
133  * Check link status.
134  *
135  * In case of inconsistency, another check is scheduled.
136  *
137  * @param priv
138  *   Pointer to private structure.
139  *
140  * @return
141  *   0 on success (link status is consistent), negative errno value
142  *   otherwise and rte_errno is set.
143  */
144 static int
145 mlx4_link_status_check(struct priv *priv)
146 {
147         struct rte_eth_link *link = &priv->dev->data->dev_link;
148         int ret = mlx4_link_update(priv->dev, 0);
149
150         if (ret)
151                 return ret;
152         if ((!link->link_speed && link->link_status) ||
153             (link->link_speed && !link->link_status)) {
154                 if (!priv->intr_alarm) {
155                         /* Inconsistent status, check again later. */
156                         ret = rte_eal_alarm_set(MLX4_INTR_ALARM_TIMEOUT,
157                                                 (void (*)(void *))
158                                                 mlx4_link_status_alarm,
159                                                 priv);
160                         if (ret)
161                                 return ret;
162                         priv->intr_alarm = 1;
163                 }
164                 rte_errno = EINPROGRESS;
165                 return -rte_errno;
166         }
167         return 0;
168 }
169
170 /**
171  * Handle interrupts from the NIC.
172  *
173  * @param priv
174  *   Pointer to private structure.
175  */
176 static void
177 mlx4_interrupt_handler(struct priv *priv)
178 {
179         enum { LSC, RMV, };
180         static const enum rte_eth_event_type type[] = {
181                 [LSC] = RTE_ETH_EVENT_INTR_LSC,
182                 [RMV] = RTE_ETH_EVENT_INTR_RMV,
183         };
184         uint32_t caught[RTE_DIM(type)] = { 0 };
185         struct ibv_async_event event;
186         const struct rte_intr_conf *const intr_conf =
187                 &priv->dev->data->dev_conf.intr_conf;
188         unsigned int i;
189
190         /* Read all message and acknowledge them. */
191         while (!ibv_get_async_event(priv->ctx, &event)) {
192                 switch (event.event_type) {
193                 case IBV_EVENT_PORT_ACTIVE:
194                 case IBV_EVENT_PORT_ERR:
195                         if (intr_conf->lsc && !mlx4_link_status_check(priv))
196                                 ++caught[LSC];
197                         break;
198                 case IBV_EVENT_DEVICE_FATAL:
199                         if (intr_conf->rmv)
200                                 ++caught[RMV];
201                         break;
202                 default:
203                         DEBUG("event type %d on physical port %d not handled",
204                               event.event_type, event.element.port_num);
205                 }
206                 ibv_ack_async_event(&event);
207         }
208         for (i = 0; i != RTE_DIM(caught); ++i)
209                 if (caught[i])
210                         _rte_eth_dev_callback_process(priv->dev, type[i],
211                                                       NULL);
212 }
213
214 /**
215  * MLX4 CQ notification .
216  *
217  * @param rxq
218  *   Pointer to receive queue structure.
219  * @param solicited
220  *   Is request solicited or not.
221  */
222 static void
223 mlx4_arm_cq(struct rxq *rxq, int solicited)
224 {
225         struct mlx4_cq *cq = &rxq->mcq;
226         uint64_t doorbell;
227         uint32_t sn = cq->arm_sn & MLX4_CQ_DB_GEQ_N_MASK;
228         uint32_t ci = cq->cons_index & MLX4_CQ_DB_CI_MASK;
229         uint32_t cmd = solicited ? MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT;
230
231         *cq->arm_db = rte_cpu_to_be_32(sn << 28 | cmd | ci);
232         /*
233          * Make sure that the doorbell record in host memory is
234          * written before ringing the doorbell via PCI MMIO.
235          */
236         rte_wmb();
237         doorbell = sn << 28 | cmd | cq->cqn;
238         doorbell <<= 32;
239         doorbell |= ci;
240         rte_write64(rte_cpu_to_be_64(doorbell), cq->cq_db_reg);
241 }
242
243 /**
244  * Uninstall interrupt handler.
245  *
246  * @param priv
247  *   Pointer to private structure.
248  *
249  * @return
250  *   0 on success, negative errno value otherwise and rte_errno is set.
251  */
252 int
253 mlx4_intr_uninstall(struct priv *priv)
254 {
255         int err = rte_errno; /* Make sure rte_errno remains unchanged. */
256
257         if (priv->intr_handle.fd != -1) {
258                 rte_intr_callback_unregister(&priv->intr_handle,
259                                              (void (*)(void *))
260                                              mlx4_interrupt_handler,
261                                              priv);
262                 priv->intr_handle.fd = -1;
263         }
264         rte_eal_alarm_cancel((void (*)(void *))mlx4_link_status_alarm, priv);
265         priv->intr_alarm = 0;
266         mlx4_rx_intr_vec_disable(priv);
267         rte_errno = err;
268         return 0;
269 }
270
271 /**
272  * Install interrupt handler.
273  *
274  * @param priv
275  *   Pointer to private structure.
276  *
277  * @return
278  *   0 on success, negative errno value otherwise and rte_errno is set.
279  */
280 int
281 mlx4_intr_install(struct priv *priv)
282 {
283         const struct rte_intr_conf *const intr_conf =
284                 &priv->dev->data->dev_conf.intr_conf;
285         int rc;
286
287         mlx4_intr_uninstall(priv);
288         if (intr_conf->rxq && mlx4_rx_intr_vec_enable(priv) < 0)
289                 goto error;
290         if (intr_conf->lsc | intr_conf->rmv) {
291                 priv->intr_handle.fd = priv->ctx->async_fd;
292                 rc = rte_intr_callback_register(&priv->intr_handle,
293                                                 (void (*)(void *))
294                                                 mlx4_interrupt_handler,
295                                                 priv);
296                 if (rc < 0) {
297                         rte_errno = -rc;
298                         goto error;
299                 }
300         }
301         return 0;
302 error:
303         mlx4_intr_uninstall(priv);
304         return -rte_errno;
305 }
306
307 /**
308  * DPDK callback for Rx queue interrupt disable.
309  *
310  * @param dev
311  *   Pointer to Ethernet device structure.
312  * @param idx
313  *   Rx queue index.
314  *
315  * @return
316  *   0 on success, negative errno value otherwise and rte_errno is set.
317  */
318 int
319 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
320 {
321         struct rxq *rxq = dev->data->rx_queues[idx];
322         struct ibv_cq *ev_cq;
323         void *ev_ctx;
324         int ret;
325
326         if (!rxq || !rxq->channel) {
327                 ret = EINVAL;
328         } else {
329                 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
330                 if (ret || ev_cq != rxq->cq)
331                         ret = EINVAL;
332         }
333         if (ret) {
334                 rte_errno = ret;
335                 WARN("unable to disable interrupt on rx queue %d",
336                      idx);
337         } else {
338                 rxq->mcq.arm_sn++;
339                 ibv_ack_cq_events(rxq->cq, 1);
340         }
341         return -ret;
342 }
343
344 /**
345  * DPDK callback for Rx queue interrupt enable.
346  *
347  * @param dev
348  *   Pointer to Ethernet device structure.
349  * @param idx
350  *   Rx queue index.
351  *
352  * @return
353  *   0 on success, negative errno value otherwise and rte_errno is set.
354  */
355 int
356 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
357 {
358         struct rxq *rxq = dev->data->rx_queues[idx];
359         int ret = 0;
360
361         if (!rxq || !rxq->channel) {
362                 ret = EINVAL;
363                 rte_errno = ret;
364                 WARN("unable to arm interrupt on rx queue %d", idx);
365         } else {
366                 mlx4_arm_cq(rxq, 0);
367         }
368         return -ret;
369 }