LCOV - code coverage report
Current view: top level - drivers/net/mlx4 - mlx4_intr.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 136 0.0 %
Date: 2025-02-01 18:54:23 Functions: 0 12 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 87 0.0 %

           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 :                 &ETH_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 = &ETH_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 :                 &ETH_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         [ #  # ]:          0 :                 if (rte_intr_fd_set(priv->intr_handle, -1))
     269                 :          0 :                         return -rte_errno;
     270                 :            :         }
     271                 :          0 :         rte_eal_alarm_cancel((void (*)(void *))mlx4_link_status_alarm, priv);
     272                 :          0 :         priv->intr_alarm = 0;
     273                 :          0 :         mlx4_rxq_intr_disable(priv);
     274                 :          0 :         rte_errno = err;
     275                 :          0 :         return 0;
     276                 :            : }
     277                 :            : 
     278                 :            : /**
     279                 :            :  * Install interrupt handler.
     280                 :            :  *
     281                 :            :  * @param priv
     282                 :            :  *   Pointer to private structure.
     283                 :            :  *
     284                 :            :  * @return
     285                 :            :  *   0 on success, negative errno value otherwise and rte_errno is set.
     286                 :            :  */
     287                 :            : int
     288                 :          0 : mlx4_intr_install(struct mlx4_priv *priv)
     289                 :            : {
     290                 :            :         const struct rte_eth_intr_conf *const intr_conf =
     291                 :          0 :                 &ETH_DEV(priv)->data->dev_conf.intr_conf;
     292                 :            :         int rc;
     293                 :            : 
     294                 :          0 :         mlx4_intr_uninstall(priv);
     295         [ #  # ]:          0 :         if (intr_conf->lsc | intr_conf->rmv) {
     296         [ #  # ]:          0 :                 if (rte_intr_fd_set(priv->intr_handle, priv->ctx->async_fd))
     297                 :          0 :                         return -rte_errno;
     298                 :            : 
     299                 :          0 :                 rc = rte_intr_callback_register(priv->intr_handle,
     300                 :            :                                                 (void (*)(void *))
     301                 :            :                                                 mlx4_interrupt_handler,
     302                 :            :                                                 priv);
     303         [ #  # ]:          0 :                 if (rc < 0) {
     304                 :          0 :                         rte_errno = -rc;
     305                 :          0 :                         goto error;
     306                 :            :                 }
     307                 :            :         }
     308                 :            :         return 0;
     309                 :            : error:
     310                 :          0 :         mlx4_intr_uninstall(priv);
     311                 :          0 :         return -rte_errno;
     312                 :            : }
     313                 :            : 
     314                 :            : /**
     315                 :            :  * DPDK callback for Rx queue interrupt disable.
     316                 :            :  *
     317                 :            :  * @param dev
     318                 :            :  *   Pointer to Ethernet device structure.
     319                 :            :  * @param idx
     320                 :            :  *   Rx queue index.
     321                 :            :  *
     322                 :            :  * @return
     323                 :            :  *   0 on success, negative errno value otherwise and rte_errno is set.
     324                 :            :  */
     325                 :            : int
     326                 :          0 : mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
     327                 :            : {
     328                 :          0 :         struct rxq *rxq = dev->data->rx_queues[idx];
     329                 :            :         struct ibv_cq *ev_cq;
     330                 :            :         void *ev_ctx;
     331                 :            :         int ret;
     332                 :            : 
     333   [ #  #  #  # ]:          0 :         if (!rxq || !rxq->channel) {
     334                 :            :                 ret = EINVAL;
     335                 :            :         } else {
     336                 :          0 :                 ret = mlx4_glue->get_cq_event(rxq->cq->channel, &ev_cq,
     337                 :            :                                               &ev_ctx);
     338                 :            :                 /** For non-zero ret save the errno (may be EAGAIN
     339                 :            :                  * which means the get_cq_event function was called before
     340                 :            :                  * receiving one).
     341                 :            :                  */
     342         [ #  # ]:          0 :                 if (ret)
     343                 :          0 :                         ret = errno;
     344         [ #  # ]:          0 :                 else if (ev_cq != rxq->cq)
     345                 :            :                         ret = EINVAL;
     346                 :            :         }
     347         [ #  # ]:          0 :         if (ret) {
     348                 :          0 :                 rte_errno = ret;
     349         [ #  # ]:          0 :                 if (ret != EAGAIN)
     350                 :          0 :                         WARN("unable to disable interrupt on rx queue %d",
     351                 :            :                              idx);
     352                 :            :         } else {
     353                 :          0 :                 rxq->mcq.arm_sn++;
     354                 :          0 :                 mlx4_glue->ack_cq_events(rxq->cq, 1);
     355                 :            :         }
     356                 :          0 :         return -ret;
     357                 :            : }
     358                 :            : 
     359                 :            : /**
     360                 :            :  * DPDK callback for Rx queue interrupt enable.
     361                 :            :  *
     362                 :            :  * @param dev
     363                 :            :  *   Pointer to Ethernet device structure.
     364                 :            :  * @param idx
     365                 :            :  *   Rx queue index.
     366                 :            :  *
     367                 :            :  * @return
     368                 :            :  *   0 on success, negative errno value otherwise and rte_errno is set.
     369                 :            :  */
     370                 :            : int
     371                 :          0 : mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
     372                 :            : {
     373                 :          0 :         struct rxq *rxq = dev->data->rx_queues[idx];
     374                 :            :         int ret = 0;
     375                 :            : 
     376   [ #  #  #  # ]:          0 :         if (!rxq || !rxq->channel) {
     377                 :            :                 ret = EINVAL;
     378                 :          0 :                 rte_errno = ret;
     379                 :          0 :                 WARN("unable to arm interrupt on rx queue %d", idx);
     380                 :            :         } else {
     381                 :          0 :                 mlx4_arm_cq(rxq, 0);
     382                 :            :         }
     383                 :          0 :         return -ret;
     384                 :            : }
     385                 :            : 
     386                 :            : /**
     387                 :            :  * Enable datapath interrupts.
     388                 :            :  *
     389                 :            :  * @param priv
     390                 :            :  *   Pointer to private structure.
     391                 :            :  *
     392                 :            :  * @return
     393                 :            :  *   0 on success, negative errno value otherwise and rte_errno is set.
     394                 :            :  */
     395                 :            : int
     396                 :          0 : mlx4_rxq_intr_enable(struct mlx4_priv *priv)
     397                 :            : {
     398                 :            :         const struct rte_eth_intr_conf *const intr_conf =
     399                 :          0 :                 &ETH_DEV(priv)->data->dev_conf.intr_conf;
     400                 :            : 
     401   [ #  #  #  # ]:          0 :         if (intr_conf->rxq && mlx4_rx_intr_vec_enable(priv) < 0)
     402                 :          0 :                 goto error;
     403                 :            :         return 0;
     404                 :            : error:
     405                 :          0 :         return -rte_errno;
     406                 :            : }
     407                 :            : 
     408                 :            : /**
     409                 :            :  * Disable datapath interrupts, keeping other interrupts intact.
     410                 :            :  *
     411                 :            :  * @param priv
     412                 :            :  *   Pointer to private structure.
     413                 :            :  */
     414                 :            : void
     415                 :          0 : mlx4_rxq_intr_disable(struct mlx4_priv *priv)
     416                 :            : {
     417                 :          0 :         int err = rte_errno; /* Make sure rte_errno remains unchanged. */
     418                 :            : 
     419                 :          0 :         mlx4_rx_intr_vec_disable(priv);
     420                 :          0 :         rte_errno = err;
     421                 :          0 : }

Generated by: LCOV version 1.14