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