1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Interrupts handling for mlx4 driver.
15 /* Verbs headers do not support -pedantic. */
17 #pragma GCC diagnostic ignored "-Wpedantic"
19 #include <infiniband/verbs.h>
21 #pragma GCC diagnostic error "-Wpedantic"
24 #include <rte_alarm.h>
25 #include <rte_errno.h>
26 #include <rte_ethdev_driver.h>
28 #include <rte_interrupts.h>
31 #include "mlx4_glue.h"
32 #include "mlx4_rxtx.h"
33 #include "mlx4_utils.h"
35 static int mlx4_link_status_check(struct mlx4_priv *priv);
38 * Clean up Rx interrupts handler.
41 * Pointer to private structure.
44 mlx4_rx_intr_vec_disable(struct mlx4_priv *priv)
46 struct rte_intr_handle *intr_handle = &priv->intr_handle;
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;
55 * Allocate queue vector and fill epoll fd list for Rx interrupts.
58 * Pointer to private structure.
61 * 0 on success, negative errno value otherwise and rte_errno is set.
64 mlx4_rx_intr_vec_enable(struct mlx4_priv *priv)
67 unsigned int rxqs_n = ETH_DEV(priv)->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;
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) {
76 ERROR("failed to allocate memory for interrupt vector,"
77 " Rx interrupts will not be supported");
80 for (i = 0; i != n; ++i) {
81 struct rxq *rxq = ETH_DEV(priv)->data->rx_queues[i];
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;
91 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
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);
99 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
100 intr_handle->efds[count] = rxq->channel->fd;
104 mlx4_rx_intr_vec_disable(priv);
106 intr_handle->nb_efd = count;
111 * Process scheduled link status check.
113 * If LSC interrupts are requested, process related callback.
116 * Pointer to private structure.
119 mlx4_link_status_alarm(struct mlx4_priv *priv)
121 const struct rte_intr_conf *const intr_conf =
122 Ð_DEV(priv)->data->dev_conf.intr_conf;
124 MLX4_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(ETH_DEV(priv),
128 RTE_ETH_EVENT_INTR_LSC,
135 * In case of inconsistency, another check is scheduled.
138 * Pointer to private structure.
141 * 0 on success (link status is consistent), negative errno value
142 * otherwise and rte_errno is set.
145 mlx4_link_status_check(struct mlx4_priv *priv)
147 struct rte_eth_link *link = Ð_DEV(priv)->data->dev_link;
148 int ret = mlx4_link_update(ETH_DEV(priv), 0);
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,
158 mlx4_link_status_alarm,
162 priv->intr_alarm = 1;
164 rte_errno = EINPROGRESS;
171 * Handle interrupts from the NIC.
174 * Pointer to private structure.
177 mlx4_interrupt_handler(struct mlx4_priv *priv)
180 static const enum rte_eth_event_type type[] = {
181 [LSC] = RTE_ETH_EVENT_INTR_LSC,
182 [RMV] = RTE_ETH_EVENT_INTR_RMV,
184 uint32_t caught[RTE_DIM(type)] = { 0 };
185 struct ibv_async_event event;
186 const struct rte_intr_conf *const intr_conf =
187 Ð_DEV(priv)->data->dev_conf.intr_conf;
190 /* Read all message and acknowledge them. */
191 while (!mlx4_glue->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))
198 case IBV_EVENT_DEVICE_FATAL:
203 DEBUG("event type %d on physical port %d not handled",
204 event.event_type, event.element.port_num);
206 mlx4_glue->ack_async_event(&event);
208 for (i = 0; i != RTE_DIM(caught); ++i)
210 _rte_eth_dev_callback_process(ETH_DEV(priv), type[i],
215 * MLX4 CQ notification .
218 * Pointer to receive queue structure.
220 * Is request solicited or not.
223 mlx4_arm_cq(struct rxq *rxq, int solicited)
225 struct mlx4_cq *cq = &rxq->mcq;
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;
231 *cq->arm_db = rte_cpu_to_be_32(sn << 28 | cmd | ci);
233 * Make sure that the doorbell record in host memory is
234 * written before ringing the doorbell via PCI MMIO.
237 doorbell = sn << 28 | cmd | cq->cqn;
240 rte_write64(rte_cpu_to_be_64(doorbell), cq->cq_db_reg);
244 * Uninstall interrupt handler.
247 * Pointer to private structure.
250 * 0 on success, negative errno value otherwise and rte_errno is set.
253 mlx4_intr_uninstall(struct mlx4_priv *priv)
255 int err = rte_errno; /* Make sure rte_errno remains unchanged. */
257 if (priv->intr_handle.fd != -1) {
258 rte_intr_callback_unregister(&priv->intr_handle,
260 mlx4_interrupt_handler,
262 priv->intr_handle.fd = -1;
264 rte_eal_alarm_cancel((void (*)(void *))mlx4_link_status_alarm, priv);
265 priv->intr_alarm = 0;
266 mlx4_rxq_intr_disable(priv);
272 * Install interrupt handler.
275 * Pointer to private structure.
278 * 0 on success, negative errno value otherwise and rte_errno is set.
281 mlx4_intr_install(struct mlx4_priv *priv)
283 const struct rte_intr_conf *const intr_conf =
284 Ð_DEV(priv)->data->dev_conf.intr_conf;
287 mlx4_intr_uninstall(priv);
288 if (intr_conf->lsc | intr_conf->rmv) {
289 priv->intr_handle.fd = priv->ctx->async_fd;
290 rc = rte_intr_callback_register(&priv->intr_handle,
292 mlx4_interrupt_handler,
301 mlx4_intr_uninstall(priv);
306 * DPDK callback for Rx queue interrupt disable.
309 * Pointer to Ethernet device structure.
314 * 0 on success, negative errno value otherwise and rte_errno is set.
317 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
319 struct rxq *rxq = dev->data->rx_queues[idx];
320 struct ibv_cq *ev_cq;
324 if (!rxq || !rxq->channel) {
327 ret = mlx4_glue->get_cq_event(rxq->cq->channel, &ev_cq,
329 if (ret || ev_cq != rxq->cq)
334 WARN("unable to disable interrupt on rx queue %d",
338 mlx4_glue->ack_cq_events(rxq->cq, 1);
344 * DPDK callback for Rx queue interrupt enable.
347 * Pointer to Ethernet device structure.
352 * 0 on success, negative errno value otherwise and rte_errno is set.
355 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
357 struct rxq *rxq = dev->data->rx_queues[idx];
360 if (!rxq || !rxq->channel) {
363 WARN("unable to arm interrupt on rx queue %d", idx);
371 * Enable datapath interrupts.
374 * Pointer to private structure.
377 * 0 on success, negative errno value otherwise and rte_errno is set.
380 mlx4_rxq_intr_enable(struct mlx4_priv *priv)
382 const struct rte_intr_conf *const intr_conf =
383 Ð_DEV(priv)->data->dev_conf.intr_conf;
385 if (intr_conf->rxq && mlx4_rx_intr_vec_enable(priv) < 0)
393 * Disable datapath interrupts, keeping other interrupts intact.
396 * Pointer to private structure.
399 mlx4_rxq_intr_disable(struct mlx4_priv *priv)
401 int err = rte_errno; /* Make sure rte_errno remains unchanged. */
403 mlx4_rx_intr_vec_disable(priv);