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.
16 /* Verbs headers do not support -pedantic. */
18 #pragma GCC diagnostic ignored "-Wpedantic"
20 #include <infiniband/verbs.h>
22 #pragma GCC diagnostic error "-Wpedantic"
25 #include <rte_alarm.h>
26 #include <rte_errno.h>
27 #include <rte_ethdev_driver.h>
29 #include <rte_interrupts.h>
32 #include "mlx4_glue.h"
33 #include "mlx4_rxtx.h"
34 #include "mlx4_utils.h"
36 static int mlx4_link_status_check(struct mlx4_priv *priv);
39 * Clean up Rx interrupts handler.
42 * Pointer to private structure.
45 mlx4_rx_intr_vec_disable(struct mlx4_priv *priv)
47 struct rte_intr_handle *intr_handle = &priv->intr_handle;
49 rte_intr_free_epoll_fd(intr_handle);
50 free(intr_handle->intr_vec);
51 intr_handle->nb_efd = 0;
52 intr_handle->intr_vec = NULL;
56 * Allocate queue vector and fill epoll fd list for Rx interrupts.
59 * Pointer to private structure.
62 * 0 on success, negative errno value otherwise and rte_errno is set.
65 mlx4_rx_intr_vec_enable(struct mlx4_priv *priv)
68 unsigned int rxqs_n = ETH_DEV(priv)->data->nb_rx_queues;
69 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
70 unsigned int count = 0;
71 struct rte_intr_handle *intr_handle = &priv->intr_handle;
73 mlx4_rx_intr_vec_disable(priv);
74 intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
75 if (intr_handle->intr_vec == NULL) {
77 ERROR("failed to allocate memory for interrupt vector,"
78 " Rx interrupts will not be supported");
81 for (i = 0; i != n; ++i) {
82 struct rxq *rxq = ETH_DEV(priv)->data->rx_queues[i];
84 /* Skip queues that cannot request interrupts. */
85 if (!rxq || !rxq->channel) {
86 /* Use invalid intr_vec[] index to disable entry. */
87 intr_handle->intr_vec[i] =
88 RTE_INTR_VEC_RXTX_OFFSET +
89 RTE_MAX_RXTX_INTR_VEC_ID;
92 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
94 ERROR("too many Rx queues for interrupt vector size"
95 " (%d), Rx interrupts cannot be enabled",
96 RTE_MAX_RXTX_INTR_VEC_ID);
97 mlx4_rx_intr_vec_disable(priv);
100 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
101 intr_handle->efds[count] = rxq->channel->fd;
105 mlx4_rx_intr_vec_disable(priv);
107 intr_handle->nb_efd = count;
112 * Process scheduled link status check.
114 * If LSC interrupts are requested, process related callback.
117 * Pointer to private structure.
120 mlx4_link_status_alarm(struct mlx4_priv *priv)
122 const struct rte_intr_conf *const intr_conf =
123 Ð_DEV(priv)->data->dev_conf.intr_conf;
125 assert(priv->intr_alarm == 1);
126 priv->intr_alarm = 0;
127 if (intr_conf->lsc && !mlx4_link_status_check(priv))
128 _rte_eth_dev_callback_process(ETH_DEV(priv),
129 RTE_ETH_EVENT_INTR_LSC,
136 * In case of inconsistency, another check is scheduled.
139 * Pointer to private structure.
142 * 0 on success (link status is consistent), negative errno value
143 * otherwise and rte_errno is set.
146 mlx4_link_status_check(struct mlx4_priv *priv)
148 struct rte_eth_link *link = Ð_DEV(priv)->data->dev_link;
149 int ret = mlx4_link_update(ETH_DEV(priv), 0);
153 if ((!link->link_speed && link->link_status) ||
154 (link->link_speed && !link->link_status)) {
155 if (!priv->intr_alarm) {
156 /* Inconsistent status, check again later. */
157 ret = rte_eal_alarm_set(MLX4_INTR_ALARM_TIMEOUT,
159 mlx4_link_status_alarm,
163 priv->intr_alarm = 1;
165 rte_errno = EINPROGRESS;
172 * Handle interrupts from the NIC.
175 * Pointer to private structure.
178 mlx4_interrupt_handler(struct mlx4_priv *priv)
181 static const enum rte_eth_event_type type[] = {
182 [LSC] = RTE_ETH_EVENT_INTR_LSC,
183 [RMV] = RTE_ETH_EVENT_INTR_RMV,
185 uint32_t caught[RTE_DIM(type)] = { 0 };
186 struct ibv_async_event event;
187 const struct rte_intr_conf *const intr_conf =
188 Ð_DEV(priv)->data->dev_conf.intr_conf;
191 /* Read all message and acknowledge them. */
192 while (!mlx4_glue->get_async_event(priv->ctx, &event)) {
193 switch (event.event_type) {
194 case IBV_EVENT_PORT_ACTIVE:
195 case IBV_EVENT_PORT_ERR:
196 if (intr_conf->lsc && !mlx4_link_status_check(priv))
199 case IBV_EVENT_DEVICE_FATAL:
204 DEBUG("event type %d on physical port %d not handled",
205 event.event_type, event.element.port_num);
207 mlx4_glue->ack_async_event(&event);
209 for (i = 0; i != RTE_DIM(caught); ++i)
211 _rte_eth_dev_callback_process(ETH_DEV(priv), type[i],
216 * MLX4 CQ notification .
219 * Pointer to receive queue structure.
221 * Is request solicited or not.
224 mlx4_arm_cq(struct rxq *rxq, int solicited)
226 struct mlx4_cq *cq = &rxq->mcq;
228 uint32_t sn = cq->arm_sn & MLX4_CQ_DB_GEQ_N_MASK;
229 uint32_t ci = cq->cons_index & MLX4_CQ_DB_CI_MASK;
230 uint32_t cmd = solicited ? MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT;
232 *cq->arm_db = rte_cpu_to_be_32(sn << 28 | cmd | ci);
234 * Make sure that the doorbell record in host memory is
235 * written before ringing the doorbell via PCI MMIO.
238 doorbell = sn << 28 | cmd | cq->cqn;
241 rte_write64(rte_cpu_to_be_64(doorbell), cq->cq_db_reg);
245 * Uninstall interrupt handler.
248 * Pointer to private structure.
251 * 0 on success, negative errno value otherwise and rte_errno is set.
254 mlx4_intr_uninstall(struct mlx4_priv *priv)
256 int err = rte_errno; /* Make sure rte_errno remains unchanged. */
258 if (priv->intr_handle.fd != -1) {
259 rte_intr_callback_unregister(&priv->intr_handle,
261 mlx4_interrupt_handler,
263 priv->intr_handle.fd = -1;
265 rte_eal_alarm_cancel((void (*)(void *))mlx4_link_status_alarm, priv);
266 priv->intr_alarm = 0;
267 mlx4_rxq_intr_disable(priv);
273 * Install interrupt handler.
276 * Pointer to private structure.
279 * 0 on success, negative errno value otherwise and rte_errno is set.
282 mlx4_intr_install(struct mlx4_priv *priv)
284 const struct rte_intr_conf *const intr_conf =
285 Ð_DEV(priv)->data->dev_conf.intr_conf;
288 mlx4_intr_uninstall(priv);
289 if (intr_conf->lsc | intr_conf->rmv) {
290 priv->intr_handle.fd = priv->ctx->async_fd;
291 rc = rte_intr_callback_register(&priv->intr_handle,
293 mlx4_interrupt_handler,
302 mlx4_intr_uninstall(priv);
307 * DPDK callback for Rx queue interrupt disable.
310 * Pointer to Ethernet device structure.
315 * 0 on success, negative errno value otherwise and rte_errno is set.
318 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
320 struct rxq *rxq = dev->data->rx_queues[idx];
321 struct ibv_cq *ev_cq;
325 if (!rxq || !rxq->channel) {
328 ret = mlx4_glue->get_cq_event(rxq->cq->channel, &ev_cq,
330 if (ret || ev_cq != rxq->cq)
335 WARN("unable to disable interrupt on rx queue %d",
339 mlx4_glue->ack_cq_events(rxq->cq, 1);
345 * DPDK callback for Rx queue interrupt enable.
348 * Pointer to Ethernet device structure.
353 * 0 on success, negative errno value otherwise and rte_errno is set.
356 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
358 struct rxq *rxq = dev->data->rx_queues[idx];
361 if (!rxq || !rxq->channel) {
364 WARN("unable to arm interrupt on rx queue %d", idx);
372 * Enable datapath interrupts.
375 * Pointer to private structure.
378 * 0 on success, negative errno value otherwise and rte_errno is set.
381 mlx4_rxq_intr_enable(struct mlx4_priv *priv)
383 const struct rte_intr_conf *const intr_conf =
384 Ð_DEV(priv)->data->dev_conf.intr_conf;
386 if (intr_conf->rxq && mlx4_rx_intr_vec_enable(priv) < 0)
394 * Disable datapath interrupts, keeping other interrupts intact.
397 * Pointer to private structure.
400 mlx4_rxq_intr_disable(struct mlx4_priv *priv)
402 int err = rte_errno; /* Make sure rte_errno remains unchanged. */
404 mlx4_rx_intr_vec_disable(priv);