LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_interrupts.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 143 579 24.7 %
Date: 2026-11-01 19:11:22 Functions: 12 43 27.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 108 405 26.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <uapi/linux/vfio.h>
       6                 :            : 
       7                 :            : #include <stdio.h>
       8                 :            : #include <stdint.h>
       9                 :            : #include <stdlib.h>
      10                 :            : #include <sys/queue.h>
      11                 :            : #include <unistd.h>
      12                 :            : #include <string.h>
      13                 :            : #include <errno.h>
      14                 :            : #include <sys/epoll.h>
      15                 :            : #include <sys/ioctl.h>
      16                 :            : #include <sys/eventfd.h>
      17                 :            : #include <assert.h>
      18                 :            : #include <stdbool.h>
      19                 :            : 
      20                 :            : #include <eal_export.h>
      21                 :            : #include <eal_trace_internal.h>
      22                 :            : #include <rte_common.h>
      23                 :            : #include <rte_interrupts.h>
      24                 :            : #include <rte_thread.h>
      25                 :            : #include <rte_per_lcore.h>
      26                 :            : #include <rte_lcore.h>
      27                 :            : #include <rte_branch_prediction.h>
      28                 :            : #include <rte_debug.h>
      29                 :            : #include <rte_log.h>
      30                 :            : #include <rte_errno.h>
      31                 :            : #include <rte_spinlock.h>
      32                 :            : #include <rte_pause.h>
      33                 :            : 
      34                 :            : #include "eal_private.h"
      35                 :            : 
      36                 :            : #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
      37                 :            : #define NB_OTHER_INTR               1
      38                 :            : 
      39                 :            : #define MAX_ITER_EVNUM  RTE_EVENT_ETH_INTR_RING_SIZE
      40                 :            : 
      41                 :            : static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
      42                 :            : 
      43                 :            : static uint32_t active_events; /**< events for active interrupt */
      44                 :            : 
      45                 :            : /**
      46                 :            :  * union for pipe fds.
      47                 :            :  */
      48                 :            : union intr_pipefds{
      49                 :            :         struct {
      50                 :            :                 int pipefd[2];
      51                 :            :         };
      52                 :            :         struct {
      53                 :            :                 int readfd;
      54                 :            :                 int writefd;
      55                 :            :         };
      56                 :            : };
      57                 :            : 
      58                 :            : /**
      59                 :            :  * union buffer for reading on different devices
      60                 :            :  */
      61                 :            : union rte_intr_read_buffer {
      62                 :            :         int uio_intr_count;              /* for uio device */
      63                 :            :         uint64_t vfio_intr_count;        /* for vfio device */
      64                 :            :         uint64_t timerfd_num;            /* for timerfd */
      65                 :            :         char charbuf[16];                /* for others */
      66                 :            : };
      67                 :            : 
      68                 :            : TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
      69                 :            : TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
      70                 :            : 
      71                 :            : struct rte_intr_callback {
      72                 :            :         TAILQ_ENTRY(rte_intr_callback) next;
      73                 :            :         rte_intr_callback_fn cb_fn;  /**< callback address */
      74                 :            :         void *cb_arg;                /**< parameter for callback */
      75                 :            :         uint8_t pending_delete;      /**< delete after callback is called */
      76                 :            :         rte_intr_unregister_callback_fn ucb_fn; /**< fn to call before cb is deleted */
      77                 :            : };
      78                 :            : 
      79                 :            : struct rte_intr_source {
      80                 :            :         TAILQ_ENTRY(rte_intr_source) next;
      81                 :            :         const struct rte_intr_handle *intr_handle; /**< interrupt handle */
      82                 :            :         struct rte_intr_cb_list callbacks;  /**< user callbacks */
      83                 :            :         uint32_t active;
      84                 :            : };
      85                 :            : 
      86                 :            : /* global spinlock for interrupt data operation */
      87                 :            : static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
      88                 :            : 
      89                 :            : /* union buffer for pipe read/write */
      90                 :            : static union intr_pipefds intr_pipe;
      91                 :            : 
      92                 :            : /* interrupt sources list */
      93                 :            : static struct rte_intr_source_list intr_sources;
      94                 :            : 
      95                 :            : /* interrupt handling thread */
      96                 :            : static rte_thread_t intr_thread;
      97                 :            : 
      98                 :            : /* VFIO interrupts */
      99                 :            : #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
     100                 :            : /* irq set buffer length for queue interrupts and LSC interrupt */
     101                 :            : #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
     102                 :            :                               sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
     103                 :            : 
     104                 :            : /* enable legacy (INTx) interrupts */
     105                 :            : static int
     106                 :          0 : vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
     107                 :            :         struct vfio_irq_set *irq_set;
     108                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     109                 :            :         int len, ret, vfio_dev_fd;
     110                 :            :         int *fd_ptr;
     111                 :            : 
     112                 :            :         len = sizeof(irq_set_buf);
     113                 :            : 
     114                 :            :         /* enable INTx */
     115                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     116                 :          0 :         irq_set->argsz = len;
     117                 :          0 :         irq_set->count = 1;
     118                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
     119                 :          0 :         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
     120                 :          0 :         irq_set->start = 0;
     121                 :            :         fd_ptr = (int *) &irq_set->data;
     122                 :          0 :         *fd_ptr = rte_intr_fd_get(intr_handle);
     123                 :            : 
     124                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     125                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     126                 :            : 
     127         [ #  # ]:          0 :         if (ret) {
     128                 :          0 :                 EAL_LOG(ERR, "Error enabling INTx interrupts for fd %d",
     129                 :            :                         rte_intr_fd_get(intr_handle));
     130                 :          0 :                 return -1;
     131                 :            :         }
     132                 :            : 
     133                 :            :         /* unmask INTx after enabling */
     134                 :            :         memset(irq_set, 0, len);
     135                 :            :         len = sizeof(struct vfio_irq_set);
     136                 :          0 :         irq_set->argsz = len;
     137                 :          0 :         irq_set->count = 1;
     138                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
     139                 :            :         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
     140                 :            :         irq_set->start = 0;
     141                 :            : 
     142                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     143                 :            : 
     144         [ #  # ]:          0 :         if (ret) {
     145                 :          0 :                 EAL_LOG(ERR, "Error unmasking INTx interrupts for fd %d",
     146                 :            :                         rte_intr_fd_get(intr_handle));
     147                 :          0 :                 return -1;
     148                 :            :         }
     149                 :            :         return 0;
     150                 :            : }
     151                 :            : 
     152                 :            : /* disable legacy (INTx) interrupts */
     153                 :            : static int
     154                 :          0 : vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
     155                 :            :         struct vfio_irq_set *irq_set;
     156                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     157                 :            :         int len, ret, vfio_dev_fd;
     158                 :            : 
     159                 :            :         len = sizeof(struct vfio_irq_set);
     160                 :            : 
     161                 :            :         /* mask interrupts before disabling */
     162                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     163                 :          0 :         irq_set->argsz = len;
     164                 :          0 :         irq_set->count = 1;
     165                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
     166                 :          0 :         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
     167                 :          0 :         irq_set->start = 0;
     168                 :            : 
     169                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     170                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     171                 :            : 
     172         [ #  # ]:          0 :         if (ret) {
     173                 :          0 :                 EAL_LOG(ERR, "Error masking INTx interrupts for fd %d",
     174                 :            :                         rte_intr_fd_get(intr_handle));
     175                 :          0 :                 return -1;
     176                 :            :         }
     177                 :            : 
     178                 :            :         /* disable INTx*/
     179                 :            :         memset(irq_set, 0, len);
     180                 :          0 :         irq_set->argsz = len;
     181                 :            :         irq_set->count = 0;
     182                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
     183                 :            :         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
     184                 :            :         irq_set->start = 0;
     185                 :            : 
     186                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     187                 :            : 
     188         [ #  # ]:          0 :         if (ret) {
     189                 :          0 :                 EAL_LOG(ERR, "Error disabling INTx interrupts for fd %d",
     190                 :            :                         rte_intr_fd_get(intr_handle));
     191                 :          0 :                 return -1;
     192                 :            :         }
     193                 :            :         return 0;
     194                 :            : }
     195                 :            : 
     196                 :            : /* unmask/ack legacy (INTx) interrupts */
     197                 :            : static int
     198                 :          0 : vfio_ack_intx(const struct rte_intr_handle *intr_handle)
     199                 :            : {
     200                 :            :         struct vfio_irq_set irq_set;
     201                 :            :         int vfio_dev_fd;
     202                 :            : 
     203                 :            :         /* unmask INTx */
     204                 :            :         memset(&irq_set, 0, sizeof(irq_set));
     205                 :          0 :         irq_set.argsz = sizeof(irq_set);
     206                 :          0 :         irq_set.count = 1;
     207                 :          0 :         irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
     208                 :            :         irq_set.index = VFIO_PCI_INTX_IRQ_INDEX;
     209                 :            :         irq_set.start = 0;
     210                 :            : 
     211                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     212         [ #  # ]:          0 :         if (ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set)) {
     213                 :          0 :                 EAL_LOG(ERR, "Error unmasking INTx interrupts for fd %d",
     214                 :            :                         rte_intr_fd_get(intr_handle));
     215                 :          0 :                 return -1;
     216                 :            :         }
     217                 :            :         return 0;
     218                 :            : }
     219                 :            : 
     220                 :            : /* enable MSI interrupts */
     221                 :            : static int
     222                 :          0 : vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
     223                 :            :         int len, ret;
     224                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     225                 :            :         struct vfio_irq_set *irq_set;
     226                 :            :         int *fd_ptr, vfio_dev_fd;
     227                 :            : 
     228                 :            :         len = sizeof(irq_set_buf);
     229                 :            : 
     230                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     231                 :          0 :         irq_set->argsz = len;
     232                 :          0 :         irq_set->count = 1;
     233                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
     234                 :          0 :         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
     235                 :          0 :         irq_set->start = 0;
     236                 :            :         fd_ptr = (int *) &irq_set->data;
     237                 :          0 :         *fd_ptr = rte_intr_fd_get(intr_handle);
     238                 :            : 
     239                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     240                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     241                 :            : 
     242         [ #  # ]:          0 :         if (ret) {
     243                 :          0 :                 EAL_LOG(ERR, "Error enabling MSI interrupts for fd %d",
     244                 :            :                         rte_intr_fd_get(intr_handle));
     245                 :          0 :                 return -1;
     246                 :            :         }
     247                 :            :         return 0;
     248                 :            : }
     249                 :            : 
     250                 :            : /* disable MSI interrupts */
     251                 :            : static int
     252                 :          0 : vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
     253                 :            :         struct vfio_irq_set *irq_set;
     254                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     255                 :            :         int len, ret, vfio_dev_fd;
     256                 :            : 
     257                 :            :         len = sizeof(struct vfio_irq_set);
     258                 :            : 
     259                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     260                 :          0 :         irq_set->argsz = len;
     261                 :          0 :         irq_set->count = 0;
     262                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
     263                 :          0 :         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
     264                 :          0 :         irq_set->start = 0;
     265                 :            : 
     266                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     267                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     268         [ #  # ]:          0 :         if (ret)
     269                 :          0 :                 EAL_LOG(ERR, "Error disabling MSI interrupts for fd %d",
     270                 :            :                         rte_intr_fd_get(intr_handle));
     271                 :            : 
     272                 :          0 :         return ret;
     273                 :            : }
     274                 :            : 
     275                 :            : /* enable MSI-X interrupts */
     276                 :            : static int
     277                 :          0 : vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
     278                 :            :         int len, ret;
     279                 :            :         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
     280                 :            :         struct vfio_irq_set *irq_set;
     281                 :            :         int *fd_ptr, vfio_dev_fd, i;
     282                 :            : 
     283                 :            :         len = sizeof(irq_set_buf);
     284                 :            : 
     285                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     286                 :          0 :         irq_set->argsz = len;
     287                 :            :         /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
     288                 :          0 :         irq_set->count = rte_intr_max_intr_get(intr_handle) ?
     289                 :          0 :                 (rte_intr_max_intr_get(intr_handle) >
     290                 :            :                  RTE_MAX_RXTX_INTR_VEC_ID + 1 ? RTE_MAX_RXTX_INTR_VEC_ID + 1 :
     291   [ #  #  #  # ]:          0 :                  rte_intr_max_intr_get(intr_handle)) : 1;
     292                 :            : 
     293                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
     294                 :          0 :         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
     295                 :          0 :         irq_set->start = 0;
     296                 :            :         fd_ptr = (int *) &irq_set->data;
     297                 :            :         /* INTR vector offset 0 reserve for non-efds mapping */
     298                 :          0 :         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = rte_intr_fd_get(intr_handle);
     299         [ #  # ]:          0 :         for (i = 0; i < rte_intr_nb_efd_get(intr_handle); i++) {
     300                 :          0 :                 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] =
     301                 :          0 :                         rte_intr_efds_index_get(intr_handle, i);
     302                 :            :         }
     303                 :            : 
     304                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     305                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     306                 :            : 
     307         [ #  # ]:          0 :         if (ret) {
     308                 :          0 :                 EAL_LOG(ERR, "Error enabling MSI-X interrupts for fd %d",
     309                 :            :                         rte_intr_fd_get(intr_handle));
     310                 :          0 :                 return -1;
     311                 :            :         }
     312                 :            : 
     313                 :            :         return 0;
     314                 :            : }
     315                 :            : 
     316                 :            : /* disable MSI-X interrupts */
     317                 :            : static int
     318                 :          0 : vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
     319                 :            :         struct vfio_irq_set *irq_set;
     320                 :            :         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
     321                 :            :         int len, ret, vfio_dev_fd;
     322                 :            : 
     323                 :            :         len = sizeof(struct vfio_irq_set);
     324                 :            : 
     325                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     326                 :          0 :         irq_set->argsz = len;
     327                 :          0 :         irq_set->count = 0;
     328                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
     329                 :          0 :         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
     330                 :          0 :         irq_set->start = 0;
     331                 :            : 
     332                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     333                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     334                 :            : 
     335         [ #  # ]:          0 :         if (ret)
     336                 :          0 :                 EAL_LOG(ERR, "Error disabling MSI-X interrupts for fd %d",
     337                 :            :                         rte_intr_fd_get(intr_handle));
     338                 :            : 
     339                 :          0 :         return ret;
     340                 :            : }
     341                 :            : 
     342                 :            : /* enable req notifier */
     343                 :            : static int
     344                 :          0 : vfio_enable_req(const struct rte_intr_handle *intr_handle)
     345                 :            : {
     346                 :            :         int len, ret;
     347                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     348                 :            :         struct vfio_irq_set *irq_set;
     349                 :            :         int *fd_ptr, vfio_dev_fd;
     350                 :            : 
     351                 :            :         len = sizeof(irq_set_buf);
     352                 :            : 
     353                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     354                 :          0 :         irq_set->argsz = len;
     355                 :          0 :         irq_set->count = 1;
     356                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
     357                 :            :                          VFIO_IRQ_SET_ACTION_TRIGGER;
     358                 :          0 :         irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
     359                 :          0 :         irq_set->start = 0;
     360                 :            :         fd_ptr = (int *) &irq_set->data;
     361                 :          0 :         *fd_ptr = rte_intr_fd_get(intr_handle);
     362                 :            : 
     363                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     364                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     365                 :            : 
     366         [ #  # ]:          0 :         if (ret) {
     367                 :          0 :                 EAL_LOG(ERR, "Error enabling req interrupts for fd %d",
     368                 :            :                         rte_intr_fd_get(intr_handle));
     369                 :          0 :                 return -1;
     370                 :            :         }
     371                 :            : 
     372                 :            :         return 0;
     373                 :            : }
     374                 :            : 
     375                 :            : /* disable req notifier */
     376                 :            : static int
     377                 :          0 : vfio_disable_req(const struct rte_intr_handle *intr_handle)
     378                 :            : {
     379                 :            :         struct vfio_irq_set *irq_set;
     380                 :            :         char irq_set_buf[IRQ_SET_BUF_LEN];
     381                 :            :         int len, ret, vfio_dev_fd;
     382                 :            : 
     383                 :            :         len = sizeof(struct vfio_irq_set);
     384                 :            : 
     385                 :            :         irq_set = (struct vfio_irq_set *) irq_set_buf;
     386                 :          0 :         irq_set->argsz = len;
     387                 :          0 :         irq_set->count = 0;
     388                 :          0 :         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
     389                 :          0 :         irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
     390                 :          0 :         irq_set->start = 0;
     391                 :            : 
     392                 :          0 :         vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
     393                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
     394                 :            : 
     395         [ #  # ]:          0 :         if (ret)
     396                 :          0 :                 EAL_LOG(ERR, "Error disabling req interrupts for fd %d",
     397                 :            :                         rte_intr_fd_get(intr_handle));
     398                 :            : 
     399                 :          0 :         return ret;
     400                 :            : }
     401                 :            : 
     402                 :            : static int
     403                 :          0 : uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
     404                 :            : {
     405                 :            :         unsigned char command_high;
     406                 :            :         int uio_cfg_fd;
     407                 :            : 
     408                 :            :         /* use UIO config file descriptor for uio_pci_generic */
     409                 :          0 :         uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
     410   [ #  #  #  # ]:          0 :         if (uio_cfg_fd < 0 || pread(uio_cfg_fd, &command_high, 1, 5) != 1) {
     411                 :          0 :                 EAL_LOG(ERR,
     412                 :            :                         "Error reading interrupts status for fd %d",
     413                 :            :                         uio_cfg_fd);
     414                 :          0 :                 return -1;
     415                 :            :         }
     416                 :            :         /* disable interrupts */
     417                 :          0 :         command_high |= 0x4;
     418         [ #  # ]:          0 :         if (pwrite(uio_cfg_fd, &command_high, 1, 5) != 1) {
     419                 :          0 :                 EAL_LOG(ERR,
     420                 :            :                         "Error disabling interrupts for fd %d",
     421                 :            :                         uio_cfg_fd);
     422                 :          0 :                 return -1;
     423                 :            :         }
     424                 :            : 
     425                 :            :         return 0;
     426                 :            : }
     427                 :            : 
     428                 :            : static int
     429                 :          0 : uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
     430                 :            : {
     431                 :            :         unsigned char command_high;
     432                 :            :         int uio_cfg_fd;
     433                 :            : 
     434                 :            :         /* use UIO config file descriptor for uio_pci_generic */
     435                 :          0 :         uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
     436   [ #  #  #  # ]:          0 :         if (uio_cfg_fd < 0 || pread(uio_cfg_fd, &command_high, 1, 5) != 1) {
     437                 :          0 :                 EAL_LOG(ERR,
     438                 :            :                         "Error reading interrupts status for fd %d",
     439                 :            :                         uio_cfg_fd);
     440                 :          0 :                 return -1;
     441                 :            :         }
     442                 :            :         /* enable interrupts */
     443                 :          0 :         command_high &= ~0x4;
     444         [ #  # ]:          0 :         if (pwrite(uio_cfg_fd, &command_high, 1, 5) != 1) {
     445                 :          0 :                 EAL_LOG(ERR,
     446                 :            :                         "Error enabling interrupts for fd %d",
     447                 :            :                         uio_cfg_fd);
     448                 :          0 :                 return -1;
     449                 :            :         }
     450                 :            : 
     451                 :            :         return 0;
     452                 :            : }
     453                 :            : 
     454                 :            : static int
     455                 :          2 : uio_intr_disable(const struct rte_intr_handle *intr_handle)
     456                 :            : {
     457                 :          2 :         const int value = 0;
     458                 :            : 
     459   [ +  -  +  + ]:          4 :         if (rte_intr_fd_get(intr_handle) < 0 ||
     460                 :          2 :             write(rte_intr_fd_get(intr_handle), &value, sizeof(value)) < 0) {
     461                 :          1 :                 EAL_LOG(ERR, "Error disabling interrupts for fd %d (%s)",
     462                 :            :                         rte_intr_fd_get(intr_handle), strerror(errno));
     463                 :          1 :                 return -1;
     464                 :            :         }
     465                 :            :         return 0;
     466                 :            : }
     467                 :            : 
     468                 :            : static int
     469                 :          2 : uio_intr_enable(const struct rte_intr_handle *intr_handle)
     470                 :            : {
     471                 :          2 :         const int value = 1;
     472                 :            : 
     473   [ +  -  +  + ]:          4 :         if (rte_intr_fd_get(intr_handle) < 0 ||
     474                 :          2 :             write(rte_intr_fd_get(intr_handle), &value, sizeof(value)) < 0) {
     475                 :          1 :                 EAL_LOG(ERR, "Error enabling interrupts for fd %d (%s)",
     476                 :            :                         rte_intr_fd_get(intr_handle), strerror(errno));
     477                 :          1 :                 return -1;
     478                 :            :         }
     479                 :            :         return 0;
     480                 :            : }
     481                 :            : 
     482                 :            : RTE_EXPORT_SYMBOL(rte_intr_callback_register)
     483                 :            : int
     484                 :         11 : rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
     485                 :            :                         rte_intr_callback_fn cb, void *cb_arg)
     486                 :            : {
     487                 :            :         int ret, wake_thread;
     488                 :            :         struct rte_intr_source *src;
     489                 :            :         struct rte_intr_callback *callback;
     490                 :            : 
     491                 :            :         wake_thread = 0;
     492                 :            : 
     493                 :            :         /* first do parameter checking */
     494   [ +  +  +  + ]:         11 :         if (rte_intr_fd_get(intr_handle) < 0 || cb == NULL) {
     495                 :          3 :                 EAL_LOG(ERR, "Registering with invalid input parameter");
     496                 :          3 :                 return -EINVAL;
     497                 :            :         }
     498                 :            : 
     499                 :            :         /* allocate a new interrupt callback entity */
     500                 :          8 :         callback = calloc(1, sizeof(*callback));
     501         [ -  + ]:          8 :         if (callback == NULL) {
     502                 :          0 :                 EAL_LOG(ERR, "Can not allocate memory");
     503                 :          0 :                 return -ENOMEM;
     504                 :            :         }
     505                 :          8 :         callback->cb_fn = cb;
     506                 :          8 :         callback->cb_arg = cb_arg;
     507                 :          8 :         callback->pending_delete = 0;
     508                 :          8 :         callback->ucb_fn = NULL;
     509                 :            : 
     510                 :            :         rte_spinlock_lock(&intr_lock);
     511                 :            : 
     512                 :            :         /* check if there is at least one callback registered for the fd */
     513         [ +  + ]:          8 :         TAILQ_FOREACH(src, &intr_sources, next) {
     514         [ +  - ]:          1 :                 if (src->intr_handle == intr_handle) {
     515                 :            :                         /* we had no interrupts for this */
     516         [ -  + ]:          1 :                         if (TAILQ_EMPTY(&src->callbacks))
     517                 :            :                                 wake_thread = 1;
     518                 :            : 
     519                 :          1 :                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
     520                 :            :                         ret = 0;
     521                 :          1 :                         break;
     522                 :            :                 }
     523                 :            :         }
     524                 :            : 
     525                 :            :         /* no existing callbacks for this - add new source */
     526         [ +  + ]:          8 :         if (src == NULL) {
     527                 :          7 :                 src = calloc(1, sizeof(*src));
     528         [ -  + ]:          7 :                 if (src == NULL) {
     529                 :          0 :                         EAL_LOG(ERR, "Can not allocate memory");
     530                 :            :                         ret = -ENOMEM;
     531                 :          0 :                         free(callback);
     532                 :            :                         callback = NULL;
     533                 :            :                 } else {
     534                 :          7 :                         src->intr_handle = intr_handle;
     535                 :          7 :                         TAILQ_INIT(&src->callbacks);
     536                 :          7 :                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
     537                 :          7 :                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
     538                 :            :                         wake_thread = 1;
     539                 :            :                         ret = 0;
     540                 :            :                 }
     541                 :            :         }
     542                 :            : 
     543                 :            :         rte_spinlock_unlock(&intr_lock);
     544                 :            : 
     545                 :            :         /**
     546                 :            :          * check if need to notify the pipe fd waited by epoll_wait to
     547                 :            :          * rebuild the wait list.
     548                 :            :          */
     549         [ +  + ]:          8 :         if (wake_thread)
     550         [ -  + ]:          7 :                 if (write(intr_pipe.writefd, "1", 1) < 0)
     551                 :            :                         ret = -EPIPE;
     552                 :            : 
     553                 :          8 :         rte_eal_trace_intr_callback_register(intr_handle, cb, cb_arg, ret);
     554                 :          8 :         return ret;
     555                 :            : }
     556                 :            : 
     557                 :            : RTE_EXPORT_SYMBOL(rte_intr_callback_unregister_pending)
     558                 :            : int
     559                 :          0 : rte_intr_callback_unregister_pending(const struct rte_intr_handle *intr_handle,
     560                 :            :                                 rte_intr_callback_fn cb_fn, void *cb_arg,
     561                 :            :                                 rte_intr_unregister_callback_fn ucb_fn)
     562                 :            : {
     563                 :            :         int ret;
     564                 :            :         struct rte_intr_source *src;
     565                 :            :         struct rte_intr_callback *cb, *next;
     566                 :            : 
     567                 :            :         /* do parameter checking first */
     568         [ #  # ]:          0 :         if (rte_intr_fd_get(intr_handle) < 0) {
     569                 :          0 :                 EAL_LOG(ERR, "Unregistering with invalid input parameter");
     570                 :          0 :                 return -EINVAL;
     571                 :            :         }
     572                 :            : 
     573                 :            :         rte_spinlock_lock(&intr_lock);
     574                 :            : 
     575                 :            :         /* check if the interrupt source for the fd is existent */
     576         [ #  # ]:          0 :         TAILQ_FOREACH(src, &intr_sources, next) {
     577         [ #  # ]:          0 :                 if (src->intr_handle == intr_handle)
     578                 :            :                         break;
     579                 :            :         }
     580                 :            : 
     581                 :            :         /* No interrupt source registered for the fd */
     582         [ #  # ]:          0 :         if (src == NULL) {
     583                 :            :                 ret = -ENOENT;
     584                 :            : 
     585                 :            :         /* only usable if the source is active */
     586         [ #  # ]:          0 :         } else if (src->active == 0) {
     587                 :            :                 ret = -EAGAIN;
     588                 :            : 
     589                 :            :         } else {
     590                 :            :                 ret = 0;
     591                 :            : 
     592                 :            :                 /* walk through the callbacks and mark all that match. */
     593         [ #  # ]:          0 :                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
     594                 :          0 :                         next = TAILQ_NEXT(cb, next);
     595   [ #  #  #  # ]:          0 :                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
     596         [ #  # ]:          0 :                                         cb->cb_arg == cb_arg)) {
     597                 :          0 :                                 cb->pending_delete = 1;
     598                 :          0 :                                 cb->ucb_fn = ucb_fn;
     599                 :          0 :                                 ret++;
     600                 :            :                         }
     601                 :            :                 }
     602                 :            :         }
     603                 :            : 
     604                 :            :         rte_spinlock_unlock(&intr_lock);
     605                 :            : 
     606                 :          0 :         return ret;
     607                 :            : }
     608                 :            : 
     609                 :            : RTE_EXPORT_SYMBOL(rte_intr_callback_unregister)
     610                 :            : int
     611                 :     260344 : rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
     612                 :            :                         rte_intr_callback_fn cb_fn, void *cb_arg)
     613                 :            : {
     614                 :            :         int ret;
     615                 :            :         struct rte_intr_source *src;
     616                 :            :         struct rte_intr_callback *cb, *next;
     617                 :            : 
     618                 :            :         /* do parameter checking first */
     619         [ +  + ]:     260344 :         if (rte_intr_fd_get(intr_handle) < 0) {
     620                 :         71 :                 EAL_LOG(ERR, "Unregistering with invalid input parameter");
     621                 :         71 :                 return -EINVAL;
     622                 :            :         }
     623                 :            : 
     624                 :            :         rte_spinlock_lock(&intr_lock);
     625                 :            : 
     626                 :            :         /* check if the interrupt source for the fd is existent */
     627         [ +  + ]:     260273 :         TAILQ_FOREACH(src, &intr_sources, next)
     628         [ -  + ]:     260038 :                 if (src->intr_handle == intr_handle)
     629                 :            :                         break;
     630                 :            : 
     631                 :            :         /* No interrupt source registered for the fd */
     632         [ +  + ]:     260273 :         if (src == NULL) {
     633                 :            :                 ret = -ENOENT;
     634                 :            : 
     635                 :            :         /* interrupt source has some active callbacks right now. */
     636         [ +  + ]:     260038 :         } else if (src->active != 0) {
     637                 :            :                 ret = -EAGAIN;
     638                 :            : 
     639                 :            :         /* ok to remove. */
     640                 :            :         } else {
     641                 :            :                 ret = 0;
     642                 :            : 
     643                 :            :                 /*walk through the callbacks and remove all that match. */
     644         [ +  + ]:         20 :                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
     645                 :            : 
     646                 :         11 :                         next = TAILQ_NEXT(cb, next);
     647                 :            : 
     648   [ +  +  +  + ]:         11 :                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
     649         [ +  + ]:          6 :                                         cb->cb_arg == cb_arg)) {
     650         [ +  + ]:          8 :                                 TAILQ_REMOVE(&src->callbacks, cb, next);
     651                 :          8 :                                 free(cb);
     652                 :          8 :                                 ret++;
     653                 :            :                         }
     654                 :            :                 }
     655                 :            : 
     656                 :            :                 /* all callbacks for that source are removed. */
     657         [ +  + ]:          9 :                 if (TAILQ_EMPTY(&src->callbacks)) {
     658         [ -  + ]:          7 :                         TAILQ_REMOVE(&intr_sources, src, next);
     659                 :          7 :                         free(src);
     660                 :            :                 }
     661                 :            :         }
     662                 :            : 
     663                 :            :         rte_spinlock_unlock(&intr_lock);
     664                 :            : 
     665                 :            :         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
     666   [ +  +  -  + ]:     260273 :         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
     667                 :            :                 ret = -EPIPE;
     668                 :            :         }
     669                 :            : 
     670                 :     260273 :         rte_eal_trace_intr_callback_unregister(intr_handle, cb_fn, cb_arg,
     671                 :            :                 ret);
     672                 :     260273 :         return ret;
     673                 :            : }
     674                 :            : 
     675                 :            : RTE_EXPORT_SYMBOL(rte_intr_callback_unregister_sync)
     676                 :            : int
     677                 :        298 : rte_intr_callback_unregister_sync(const struct rte_intr_handle *intr_handle,
     678                 :            :                         rte_intr_callback_fn cb_fn, void *cb_arg)
     679                 :            : {
     680                 :            :         int ret = 0;
     681                 :            : 
     682         [ -  + ]:        298 :         while ((ret = rte_intr_callback_unregister(intr_handle, cb_fn, cb_arg)) == -EAGAIN)
     683                 :            :                 rte_pause();
     684                 :            : 
     685                 :        298 :         return ret;
     686                 :            : }
     687                 :            : 
     688                 :            : RTE_EXPORT_SYMBOL(rte_intr_enable)
     689                 :            : int
     690                 :          7 : rte_intr_enable(const struct rte_intr_handle *intr_handle)
     691                 :            : {
     692                 :            :         int rc = 0, uio_cfg_fd;
     693                 :            : 
     694         [ +  + ]:          7 :         if (intr_handle == NULL)
     695                 :            :                 return -1;
     696                 :            : 
     697         [ -  + ]:          6 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
     698                 :            :                 rc = 0;
     699                 :          0 :                 goto out;
     700                 :            :         }
     701                 :            : 
     702                 :          6 :         uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
     703   [ +  +  +  + ]:          6 :         if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0) {
     704                 :            :                 rc = -1;
     705                 :          4 :                 goto out;
     706                 :            :         }
     707                 :            : 
     708   [ +  -  -  -  :          2 :         switch (rte_intr_type_get(intr_handle)) {
             -  -  -  - ]
     709                 :            :         /* write to the uio fd to enable the interrupt */
     710                 :          2 :         case RTE_INTR_HANDLE_UIO:
     711         [ +  + ]:          2 :                 if (uio_intr_enable(intr_handle))
     712                 :            :                         rc = -1;
     713                 :            :                 break;
     714                 :          0 :         case RTE_INTR_HANDLE_UIO_INTX:
     715         [ #  # ]:          0 :                 if (uio_intx_intr_enable(intr_handle))
     716                 :            :                         rc = -1;
     717                 :            :                 break;
     718                 :            :         /* not used at this moment */
     719                 :            :         case RTE_INTR_HANDLE_ALARM:
     720                 :            :                 rc = -1;
     721                 :            :                 break;
     722                 :          0 :         case RTE_INTR_HANDLE_VFIO_MSIX:
     723         [ #  # ]:          0 :                 if (vfio_enable_msix(intr_handle))
     724                 :            :                         rc = -1;
     725                 :            :                 break;
     726                 :          0 :         case RTE_INTR_HANDLE_VFIO_MSI:
     727         [ #  # ]:          0 :                 if (vfio_enable_msi(intr_handle))
     728                 :            :                         rc = -1;
     729                 :            :                 break;
     730                 :          0 :         case RTE_INTR_HANDLE_VFIO_LEGACY:
     731         [ #  # ]:          0 :                 if (vfio_enable_intx(intr_handle))
     732                 :            :                         rc = -1;
     733                 :            :                 break;
     734                 :          0 :         case RTE_INTR_HANDLE_VFIO_REQ:
     735         [ #  # ]:          0 :                 if (vfio_enable_req(intr_handle))
     736                 :            :                         rc = -1;
     737                 :            :                 break;
     738                 :            :         /* not used at this moment */
     739                 :            :         case RTE_INTR_HANDLE_DEV_EVENT:
     740                 :            :                 rc = -1;
     741                 :            :                 break;
     742                 :            :         /* unknown handle type */
     743                 :          0 :         default:
     744                 :          0 :                 EAL_LOG(ERR, "Unknown handle type of fd %d",
     745                 :            :                         rte_intr_fd_get(intr_handle));
     746                 :            :                 rc = -1;
     747                 :          0 :                 break;
     748                 :            :         }
     749         [ -  + ]:          6 : out:
     750                 :          6 :         rte_eal_trace_intr_enable(intr_handle, rc);
     751                 :          6 :         return rc;
     752                 :            : }
     753                 :            : 
     754                 :            : /**
     755                 :            :  * PMD generally calls this function at the end of its IRQ callback.
     756                 :            :  * Internally, it unmasks the interrupt if possible.
     757                 :            :  *
     758                 :            :  * For INTx, unmasking is required as the interrupt is auto-masked prior to
     759                 :            :  * invoking callback.
     760                 :            :  *
     761                 :            :  * For MSI/MSI-X, unmasking is typically not needed as the interrupt is not
     762                 :            :  * auto-masked. In fact, for interrupt handle types VFIO_MSIX and VFIO_MSI,
     763                 :            :  * this function is no-op.
     764                 :            :  */
     765                 :            : RTE_EXPORT_SYMBOL(rte_intr_ack)
     766                 :            : int
     767                 :          0 : rte_intr_ack(const struct rte_intr_handle *intr_handle)
     768                 :            : {
     769                 :            :         int uio_cfg_fd;
     770                 :            : 
     771         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV)
     772                 :            :                 return 0;
     773                 :            : 
     774                 :          0 :         uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
     775   [ #  #  #  # ]:          0 :         if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0)
     776                 :            :                 return -1;
     777                 :            : 
     778   [ #  #  #  #  :          0 :         switch (rte_intr_type_get(intr_handle)) {
                   #  # ]
     779                 :            :         /* Both acking and enabling are same for UIO */
     780                 :          0 :         case RTE_INTR_HANDLE_UIO:
     781         [ #  # ]:          0 :                 if (uio_intr_enable(intr_handle))
     782                 :          0 :                         return -1;
     783                 :            :                 break;
     784                 :          0 :         case RTE_INTR_HANDLE_UIO_INTX:
     785         [ #  # ]:          0 :                 if (uio_intx_intr_enable(intr_handle))
     786                 :          0 :                         return -1;
     787                 :            :                 break;
     788                 :            :         /* not used at this moment */
     789                 :            :         case RTE_INTR_HANDLE_ALARM:
     790                 :            :                 return -1;
     791                 :            :         /* VFIO MSI* is implicitly acked unlike INTx, nothing to do */
     792                 :          0 :         case RTE_INTR_HANDLE_VFIO_MSIX:
     793                 :            :         case RTE_INTR_HANDLE_VFIO_MSI:
     794                 :          0 :                 return 0;
     795                 :          0 :         case RTE_INTR_HANDLE_VFIO_LEGACY:
     796         [ #  # ]:          0 :                 if (vfio_ack_intx(intr_handle))
     797                 :          0 :                         return -1;
     798                 :            :                 break;
     799                 :            :         case RTE_INTR_HANDLE_VFIO_REQ:
     800                 :            :                 return -1;
     801                 :            :         /* not used at this moment */
     802                 :            :         case RTE_INTR_HANDLE_DEV_EVENT:
     803                 :            :                 return -1;
     804                 :            :         /* unknown handle type */
     805                 :          0 :         default:
     806                 :          0 :                 EAL_LOG(ERR, "Unknown handle type of fd %d",
     807                 :            :                         rte_intr_fd_get(intr_handle));
     808                 :          0 :                 return -1;
     809                 :            :         }
     810                 :            : 
     811                 :            :         return 0;
     812                 :            : }
     813                 :            : 
     814                 :            : RTE_EXPORT_SYMBOL(rte_intr_disable)
     815                 :            : int
     816                 :          7 : rte_intr_disable(const struct rte_intr_handle *intr_handle)
     817                 :            : {
     818                 :            :         int rc = 0, uio_cfg_fd;
     819                 :            : 
     820         [ +  + ]:          7 :         if (intr_handle == NULL)
     821                 :            :                 return -1;
     822                 :            : 
     823         [ -  + ]:          6 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
     824                 :            :                 rc = 0;
     825                 :          0 :                 goto out;
     826                 :            :         }
     827                 :            : 
     828                 :          6 :         uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
     829   [ +  +  +  + ]:          6 :         if (rte_intr_fd_get(intr_handle) < 0 || uio_cfg_fd < 0) {
     830                 :            :                 rc = -1;
     831                 :          4 :                 goto out;
     832                 :            :         }
     833                 :            : 
     834   [ +  -  -  -  :          2 :         switch (rte_intr_type_get(intr_handle)) {
             -  -  -  - ]
     835                 :            :         /* write to the uio fd to disable the interrupt */
     836                 :          2 :         case RTE_INTR_HANDLE_UIO:
     837         [ +  + ]:          2 :                 if (uio_intr_disable(intr_handle))
     838                 :            :                         rc = -1;
     839                 :            :                 break;
     840                 :          0 :         case RTE_INTR_HANDLE_UIO_INTX:
     841         [ #  # ]:          0 :                 if (uio_intx_intr_disable(intr_handle))
     842                 :            :                         rc = -1;
     843                 :            :                 break;
     844                 :            :         /* not used at this moment */
     845                 :            :         case RTE_INTR_HANDLE_ALARM:
     846                 :            :                 rc = -1;
     847                 :            :                 break;
     848                 :          0 :         case RTE_INTR_HANDLE_VFIO_MSIX:
     849         [ #  # ]:          0 :                 if (vfio_disable_msix(intr_handle))
     850                 :            :                         rc = -1;
     851                 :            :                 break;
     852                 :          0 :         case RTE_INTR_HANDLE_VFIO_MSI:
     853         [ #  # ]:          0 :                 if (vfio_disable_msi(intr_handle))
     854                 :            :                         rc = -1;
     855                 :            :                 break;
     856                 :          0 :         case RTE_INTR_HANDLE_VFIO_LEGACY:
     857         [ #  # ]:          0 :                 if (vfio_disable_intx(intr_handle))
     858                 :            :                         rc = -1;
     859                 :            :                 break;
     860                 :          0 :         case RTE_INTR_HANDLE_VFIO_REQ:
     861         [ #  # ]:          0 :                 if (vfio_disable_req(intr_handle))
     862                 :            :                         rc = -1;
     863                 :            :                 break;
     864                 :            :         /* not used at this moment */
     865                 :            :         case RTE_INTR_HANDLE_DEV_EVENT:
     866                 :            :                 rc = -1;
     867                 :            :                 break;
     868                 :            :         /* unknown handle type */
     869                 :          0 :         default:
     870                 :          0 :                 EAL_LOG(ERR, "Unknown handle type of fd %d",
     871                 :            :                         rte_intr_fd_get(intr_handle));
     872                 :            :                 rc = -1;
     873                 :          0 :                 break;
     874                 :            :         }
     875         [ -  + ]:          6 : out:
     876                 :          6 :         rte_eal_trace_intr_disable(intr_handle, rc);
     877                 :          6 :         return rc;
     878                 :            : }
     879                 :            : 
     880                 :            : static uint32_t
     881                 :            : epoll_to_intr_events(uint32_t epoll_events)
     882                 :            : {
     883                 :            :         uint32_t ev = 0;
     884                 :            : 
     885                 :     260032 :         if (epoll_events & EPOLLIN)
     886                 :            :                 ev |= RTE_INTR_EVENT_IN;
     887         [ -  + ]:     260032 :         if (epoll_events & EPOLLERR)
     888                 :          0 :                 ev |= RTE_INTR_EVENT_ERR;
     889         [ -  + ]:     260032 :         if (epoll_events & EPOLLHUP)
     890                 :          0 :                 ev |= RTE_INTR_EVENT_HUP;
     891         [ -  + ]:     260032 :         if (epoll_events & EPOLLRDHUP)
     892                 :          0 :                 ev |= RTE_INTR_EVENT_RDHUP;
     893                 :            :         return ev;
     894                 :            : }
     895                 :            : 
     896                 :            : static void
     897                 :          0 : eal_intr_source_remove_and_free(struct rte_intr_source *src)
     898                 :            : {
     899                 :            :         struct rte_intr_callback *cb, *next;
     900                 :            : 
     901                 :            :         /* Remove the interrupt source */
     902                 :            :         rte_spinlock_lock(&intr_lock);
     903         [ #  # ]:          0 :         TAILQ_REMOVE(&intr_sources, src, next);
     904                 :            :         rte_spinlock_unlock(&intr_lock);
     905                 :            : 
     906                 :            :         /* Free callbacks */
     907         [ #  # ]:          0 :         for (cb = TAILQ_FIRST(&src->callbacks); cb; cb = next) {
     908                 :          0 :                 next = TAILQ_NEXT(cb, next);
     909         [ #  # ]:          0 :                 TAILQ_REMOVE(&src->callbacks, cb, next);
     910                 :          0 :                 free(cb);
     911                 :            :         }
     912                 :            : 
     913                 :            :         /* Free the interrupt source */
     914                 :          0 :         free(src);
     915                 :          0 : }
     916                 :            : 
     917                 :            : static int
     918                 :     260047 : eal_intr_process_interrupts(struct epoll_event *events, int nfds)
     919                 :            : {
     920                 :            :         bool call = false;
     921                 :            :         int n, bytes_read, rv;
     922                 :            :         struct rte_intr_source *src;
     923                 :            :         struct rte_intr_callback *cb, *next;
     924                 :            :         union rte_intr_read_buffer buf;
     925                 :            :         struct rte_intr_callback active_cb;
     926                 :            : 
     927         [ +  + ]:     520079 :         for (n = 0; n < nfds; n++) {
     928                 :            : 
     929                 :            :                 /**
     930                 :            :                  * if the pipe fd is ready to read, return out to
     931                 :            :                  * rebuild the wait list.
     932                 :            :                  */
     933         [ +  + ]:     260047 :                 if (events[n].data.fd == intr_pipe.readfd){
     934                 :            :                         int r = read(intr_pipe.readfd, buf.charbuf,
     935                 :            :                                         sizeof(buf.charbuf));
     936                 :            :                         RTE_SET_USED(r);
     937                 :         15 :                         return -1;
     938                 :            :                 }
     939                 :            :                 rte_spinlock_lock(&intr_lock);
     940         [ +  - ]:     260032 :                 TAILQ_FOREACH(src, &intr_sources, next)
     941         [ -  + ]:     260032 :                         if (rte_intr_fd_get(src->intr_handle) == events[n].data.fd)
     942                 :            :                                 break;
     943         [ -  + ]:     260032 :                 if (src == NULL){
     944                 :            :                         rte_spinlock_unlock(&intr_lock);
     945                 :          0 :                         continue;
     946                 :            :                 }
     947                 :            : 
     948                 :            :                 /* mark this interrupt source as active and release the lock. */
     949                 :     260032 :                 src->active = 1;
     950                 :            :                 rte_spinlock_unlock(&intr_lock);
     951                 :            : 
     952                 :            :                 /* set the length to be read dor different handle type */
     953   [ +  -  +  +  :     260032 :                 switch (rte_intr_type_get(src->intr_handle)) {
                      + ]
     954                 :            :                 case RTE_INTR_HANDLE_UIO:
     955                 :            :                 case RTE_INTR_HANDLE_UIO_INTX:
     956                 :            :                         bytes_read = sizeof(buf.uio_intr_count);
     957                 :            :                         break;
     958                 :            :                 case RTE_INTR_HANDLE_ALARM:
     959                 :            :                         bytes_read = sizeof(buf.timerfd_num);
     960                 :            :                         break;
     961                 :            :                 case RTE_INTR_HANDLE_VFIO_REQ:
     962                 :            :                 case RTE_INTR_HANDLE_VFIO_MSIX:
     963                 :            :                 case RTE_INTR_HANDLE_VFIO_MSI:
     964                 :            :                 case RTE_INTR_HANDLE_VFIO_LEGACY:
     965                 :            :                         bytes_read = sizeof(buf.vfio_intr_count);
     966                 :            :                         break;
     967                 :            :                 case RTE_INTR_HANDLE_VDEV:
     968                 :            :                 case RTE_INTR_HANDLE_EXT:
     969                 :            :                         bytes_read = 0;
     970                 :            :                         call = true;
     971                 :            :                         break;
     972                 :            :                 case RTE_INTR_HANDLE_DEV_EVENT:
     973                 :            :                         bytes_read = 0;
     974                 :            :                         call = true;
     975                 :            :                         break;
     976                 :            :                 default:
     977                 :            :                         bytes_read = 1;
     978                 :            :                         break;
     979                 :            :                 }
     980                 :            : 
     981                 :            :                 if (bytes_read > 0) {
     982                 :            :                         /*
     983                 :            :                          * Check for epoll error or disconnect events for
     984                 :            :                          * interrupts that are read directly in eal.
     985                 :            :                          */
     986         [ -  + ]:          6 :                         if (events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
     987                 :          0 :                                 EAL_LOG(ERR, "Disconnect condition on fd %d "
     988                 :            :                                         "(events=0x%x), removing from epoll",
     989                 :            :                                         events[n].data.fd, events[n].events);
     990                 :          0 :                                 eal_intr_source_remove_and_free(src);
     991                 :          0 :                                 return -1;
     992                 :            :                         }
     993                 :            : 
     994                 :            :                         /*
     995                 :            :                          * read out to clear the ready-to-be-read flag
     996                 :            :                          * for epoll_wait.
     997                 :            :                          */
     998                 :          6 :                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
     999         [ -  + ]:          6 :                         if (bytes_read > 0) {
    1000                 :            :                                 call = true;
    1001         [ #  # ]:          0 :                         } else if (bytes_read < 0) {
    1002         [ #  # ]:          0 :                                 if (errno == EINTR || errno == EWOULDBLOCK)
    1003                 :          0 :                                         continue;
    1004                 :            : 
    1005                 :          0 :                                 EAL_LOG(ERR, "Error reading from file descriptor %d: %s",
    1006                 :            :                                         events[n].data.fd,
    1007                 :            :                                         strerror(errno));
    1008                 :            :                         } else {
    1009                 :          0 :                                 EAL_LOG(ERR, "Read nothing from file descriptor %d",
    1010                 :            :                                         events[n].data.fd);
    1011                 :            :                         }
    1012         [ -  + ]:          6 :                         if (bytes_read <= 0) {
    1013                 :          0 :                                 eal_intr_source_remove_and_free(src);
    1014                 :          0 :                                 return -1;
    1015                 :            :                         }
    1016                 :            :                 }
    1017                 :            : 
    1018                 :            :                 /* grab a lock, again to call callbacks and update status. */
    1019                 :            :                 rte_spinlock_lock(&intr_lock);
    1020                 :            : 
    1021         [ +  - ]:     260032 :                 if (call) {
    1022         [ +  - ]:     260032 :                         active_events = epoll_to_intr_events(events[n].events);
    1023                 :            :                         /* Finally, call all callbacks. */
    1024         [ +  + ]:     520064 :                         TAILQ_FOREACH(cb, &src->callbacks, next) {
    1025                 :            : 
    1026                 :            :                                 /* make a copy and unlock. */
    1027                 :     260032 :                                 active_cb = *cb;
    1028                 :            :                                 rte_spinlock_unlock(&intr_lock);
    1029                 :            : 
    1030                 :            :                                 /* call the actual callback */
    1031                 :     260032 :                                 active_cb.cb_fn(active_cb.cb_arg);
    1032                 :            : 
    1033                 :            :                                 /*get the lock back. */
    1034                 :            :                                 rte_spinlock_lock(&intr_lock);
    1035                 :            :                         }
    1036                 :     260032 :                         active_events = 0;
    1037                 :            :                 }
    1038                 :            :                 /* we done with that interrupt source, release it. */
    1039                 :     260032 :                 src->active = 0;
    1040                 :            : 
    1041                 :            :                 rv = 0;
    1042                 :            : 
    1043                 :            :                 /* check if any callback are supposed to be removed */
    1044         [ +  + ]:     520064 :                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
    1045                 :     260032 :                         next = TAILQ_NEXT(cb, next);
    1046         [ -  + ]:     260032 :                         if (cb->pending_delete) {
    1047         [ #  # ]:          0 :                                 TAILQ_REMOVE(&src->callbacks, cb, next);
    1048         [ #  # ]:          0 :                                 if (cb->ucb_fn)
    1049                 :          0 :                                         cb->ucb_fn(src->intr_handle, cb->cb_arg);
    1050                 :          0 :                                 free(cb);
    1051                 :          0 :                                 rv++;
    1052                 :            :                         }
    1053                 :            :                 }
    1054                 :            : 
    1055                 :            :                 /* all callbacks for that source are removed. */
    1056         [ -  + ]:     260032 :                 if (TAILQ_EMPTY(&src->callbacks)) {
    1057         [ #  # ]:          0 :                         TAILQ_REMOVE(&intr_sources, src, next);
    1058                 :          0 :                         free(src);
    1059                 :            :                 }
    1060                 :            : 
    1061                 :            :                 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
    1062   [ -  +  -  - ]:     260032 :                 if (rv > 0 && write(intr_pipe.writefd, "1", 1) < 0) {
    1063                 :            :                         rte_spinlock_unlock(&intr_lock);
    1064                 :          0 :                         return -EPIPE;
    1065                 :            :                 }
    1066                 :            : 
    1067                 :            :                 rte_spinlock_unlock(&intr_lock);
    1068                 :            :         }
    1069                 :            : 
    1070                 :            :         return 0;
    1071                 :            : }
    1072                 :            : 
    1073                 :            : /**
    1074                 :            :  * It handles all the interrupts.
    1075                 :            :  *
    1076                 :            :  * @param pfd
    1077                 :            :  *  epoll file descriptor.
    1078                 :            :  * @param totalfds
    1079                 :            :  *  The number of file descriptors added in epoll.
    1080                 :            :  *
    1081                 :            :  * @return
    1082                 :            :  *  void
    1083                 :            :  */
    1084                 :            : static void
    1085                 :        244 : eal_intr_handle_interrupts(int pfd, unsigned totalfds)
    1086                 :            : {
    1087                 :        244 :         struct epoll_event *events = alloca(sizeof(struct epoll_event) * totalfds);
    1088                 :            :         int nfds = 0;
    1089                 :            : 
    1090                 :            :         for(;;) {
    1091                 :     260276 :                 nfds = epoll_wait(pfd, events, totalfds,
    1092                 :            :                         EAL_INTR_EPOLL_WAIT_FOREVER);
    1093                 :            :                 /* epoll_wait fail */
    1094         [ -  + ]:     260047 :                 if (nfds < 0) {
    1095         [ #  # ]:          0 :                         if (errno == EINTR)
    1096                 :          0 :                                 continue;
    1097                 :          0 :                         EAL_LOG(ERR,
    1098                 :            :                                 "epoll_wait returns with fail");
    1099                 :          0 :                         return;
    1100                 :            :                 }
    1101                 :            :                 /* epoll_wait timeout, will never happens here */
    1102         [ -  + ]:     260047 :                 else if (nfds == 0)
    1103                 :          0 :                         continue;
    1104                 :            :                 /* epoll_wait has at least one fd ready to read */
    1105         [ +  + ]:     260047 :                 if (eal_intr_process_interrupts(events, nfds) < 0)
    1106                 :            :                         return;
    1107                 :            :         }
    1108                 :            : }
    1109                 :            : 
    1110                 :            : /**
    1111                 :            :  * It builds/rebuilds up the epoll file descriptor with all the
    1112                 :            :  * file descriptors being waited on. Then handles the interrupts.
    1113                 :            :  *
    1114                 :            :  * @param arg
    1115                 :            :  *  pointer. (unused)
    1116                 :            :  *
    1117                 :            :  * @return
    1118                 :            :  *  never return;
    1119                 :            :  */
    1120                 :            : static __rte_noreturn uint32_t
    1121                 :        229 : eal_intr_thread_main(__rte_unused void *arg)
    1122                 :            : {
    1123                 :            :         /* host thread, never break out */
    1124                 :         15 :         for (;;) {
    1125                 :            :                 /* build up the epoll fd with all descriptors we are to
    1126                 :            :                  * wait on then pass it to the handle_interrupts function
    1127                 :            :                  */
    1128                 :            :                 static struct epoll_event pipe_event = {
    1129                 :            :                         .events = EPOLLIN | EPOLLPRI,
    1130                 :            :                 };
    1131                 :            :                 struct rte_intr_source *src;
    1132                 :            :                 unsigned numfds = 0;
    1133                 :            : 
    1134                 :            :                 /* create epoll fd */
    1135                 :        244 :                 int pfd = epoll_create(1);
    1136         [ -  + ]:        244 :                 if (pfd < 0)
    1137                 :          0 :                         rte_panic("Cannot create epoll instance\n");
    1138                 :            : 
    1139                 :        244 :                 pipe_event.data.fd = intr_pipe.readfd;
    1140                 :            :                 /**
    1141                 :            :                  * add pipe fd into wait list, this pipe is used to
    1142                 :            :                  * rebuild the wait list.
    1143                 :            :                  */
    1144         [ -  + ]:        244 :                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
    1145                 :            :                                                 &pipe_event) < 0) {
    1146                 :          0 :                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
    1147                 :            :                                         intr_pipe.readfd, strerror(errno));
    1148                 :            :                 }
    1149                 :            :                 numfds++;
    1150                 :            : 
    1151                 :            :                 rte_spinlock_lock(&intr_lock);
    1152                 :            : 
    1153         [ +  + ]:        253 :                 TAILQ_FOREACH(src, &intr_sources, next) {
    1154                 :            :                         struct epoll_event ev;
    1155                 :            : 
    1156         [ -  + ]:          9 :                         if (src->callbacks.tqh_first == NULL)
    1157                 :          0 :                                 continue; /* skip those with no callbacks */
    1158                 :            :                         memset(&ev, 0, sizeof(ev));
    1159                 :          9 :                         ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
    1160                 :          9 :                         ev.data.fd = rte_intr_fd_get(src->intr_handle);
    1161                 :            : 
    1162                 :            :                         /**
    1163                 :            :                          * add all the uio device file descriptor
    1164                 :            :                          * into wait list.
    1165                 :            :                          */
    1166         [ -  + ]:          9 :                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
    1167                 :            :                                         rte_intr_fd_get(src->intr_handle), &ev) < 0) {
    1168                 :          0 :                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
    1169                 :            :                                         rte_intr_fd_get(src->intr_handle),
    1170                 :            :                                         strerror(errno));
    1171                 :            :                         }
    1172                 :            :                         else
    1173                 :          9 :                                 numfds++;
    1174                 :            :                 }
    1175                 :            :                 rte_spinlock_unlock(&intr_lock);
    1176                 :            :                 /* serve the interrupt */
    1177                 :        244 :                 eal_intr_handle_interrupts(pfd, numfds);
    1178                 :            : 
    1179                 :            :                 /**
    1180                 :            :                  * when we return, we need to rebuild the
    1181                 :            :                  * list of fds to monitor.
    1182                 :            :                  */
    1183                 :         15 :                 close(pfd);
    1184                 :            :         }
    1185                 :            : }
    1186                 :            : 
    1187                 :            : int
    1188                 :        229 : rte_eal_intr_init(void)
    1189                 :            : {
    1190                 :            :         int ret = 0;
    1191                 :            : 
    1192                 :            :         /* init the global interrupt source head */
    1193                 :        229 :         TAILQ_INIT(&intr_sources);
    1194                 :            : 
    1195                 :            :         /**
    1196                 :            :          * create a pipe which will be waited by epoll and notified to
    1197                 :            :          * rebuild the wait list of epoll.
    1198                 :            :          */
    1199         [ -  + ]:        229 :         if (pipe(intr_pipe.pipefd) < 0) {
    1200                 :          0 :                 rte_errno = errno;
    1201                 :          0 :                 return -1;
    1202                 :            :         }
    1203                 :            : 
    1204                 :            :         /* create the host thread to wait/handle the interrupt */
    1205                 :        229 :         ret = rte_thread_create_internal_control(&intr_thread, "intr",
    1206                 :            :                         eal_intr_thread_main, NULL);
    1207         [ -  + ]:        229 :         if (ret != 0) {
    1208                 :          0 :                 rte_errno = -ret;
    1209                 :          0 :                 EAL_LOG(ERR,
    1210                 :            :                         "Failed to create thread for interrupt handling");
    1211                 :            :         }
    1212                 :            : 
    1213                 :            :         return ret;
    1214                 :            : }
    1215                 :            : 
    1216                 :            : static void
    1217                 :          0 : eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
    1218                 :            : {
    1219                 :            :         union rte_intr_read_buffer buf;
    1220                 :            :         int bytes_read = 0;
    1221                 :            :         int nbytes;
    1222                 :            : 
    1223   [ #  #  #  #  :          0 :         switch (rte_intr_type_get(intr_handle)) {
                      # ]
    1224                 :            :         case RTE_INTR_HANDLE_UIO:
    1225                 :            :         case RTE_INTR_HANDLE_UIO_INTX:
    1226                 :            :                 bytes_read = sizeof(buf.uio_intr_count);
    1227                 :            :                 break;
    1228                 :            :         case RTE_INTR_HANDLE_VFIO_MSIX:
    1229                 :            :         case RTE_INTR_HANDLE_VFIO_MSI:
    1230                 :            :         case RTE_INTR_HANDLE_VFIO_LEGACY:
    1231                 :            :                 bytes_read = sizeof(buf.vfio_intr_count);
    1232                 :            :                 break;
    1233                 :          0 :         case RTE_INTR_HANDLE_VDEV:
    1234                 :          0 :                 bytes_read = rte_intr_efd_counter_size_get(intr_handle);
    1235                 :            :                 /* For vdev, number of bytes to read is set by driver */
    1236                 :            :                 break;
    1237                 :            :         case RTE_INTR_HANDLE_EXT:
    1238                 :            :                 return;
    1239                 :          0 :         default:
    1240                 :            :                 bytes_read = 1;
    1241                 :          0 :                 EAL_LOG(INFO, "unexpected intr type");
    1242                 :            :                 break;
    1243                 :            :         }
    1244                 :            : 
    1245                 :            :         /**
    1246                 :            :          * read out to clear the ready-to-be-read flag
    1247                 :            :          * for epoll_wait.
    1248                 :            :          */
    1249         [ #  # ]:          0 :         if (bytes_read == 0)
    1250                 :            :                 return;
    1251                 :            :         do {
    1252         [ #  # ]:          0 :                 nbytes = read(fd, &buf, bytes_read);
    1253         [ #  # ]:          0 :                 if (nbytes < 0) {
    1254         [ #  # ]:          0 :                         if (errno == EINTR || errno == EWOULDBLOCK ||
    1255                 :            :                             errno == EAGAIN)
    1256                 :            :                                 continue;
    1257                 :          0 :                         EAL_LOG(ERR,
    1258                 :            :                                 "Error reading from fd %d: %s",
    1259                 :            :                                 fd, strerror(errno));
    1260         [ #  # ]:          0 :                 } else if (nbytes == 0)
    1261                 :          0 :                         EAL_LOG(ERR, "Read nothing from fd %d", fd);
    1262                 :            :                 return;
    1263                 :            :         } while (1);
    1264                 :            : }
    1265                 :            : 
    1266                 :            : static int
    1267                 :          0 : eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
    1268                 :            :                         struct rte_epoll_event *events)
    1269                 :            : {
    1270                 :            :         unsigned int i, count = 0;
    1271                 :            :         struct rte_epoll_event *rev;
    1272                 :            :         uint32_t valid_status;
    1273                 :            : 
    1274         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
    1275                 :          0 :                 rev = evs[i].data.ptr;
    1276                 :            :                 valid_status =  RTE_EPOLL_VALID;
    1277                 :            :                 /* ACQUIRE memory ordering here pairs with RELEASE
    1278                 :            :                  * ordering below acting as a lock to synchronize
    1279                 :            :                  * the event data updating.
    1280                 :            :                  */
    1281   [ #  #  #  # ]:          0 :                 if (!rev || !rte_atomic_compare_exchange_strong_explicit(&rev->status,
    1282                 :            :                                     &valid_status, RTE_EPOLL_EXEC,
    1283                 :            :                                     rte_memory_order_acquire, rte_memory_order_relaxed))
    1284                 :          0 :                         continue;
    1285                 :            : 
    1286                 :          0 :                 events[count].status        = RTE_EPOLL_VALID;
    1287                 :          0 :                 events[count].fd            = rev->fd;
    1288                 :          0 :                 events[count].epfd          = rev->epfd;
    1289                 :          0 :                 events[count].epdata.event  = evs[i].events;
    1290                 :          0 :                 events[count].epdata.data   = rev->epdata.data;
    1291         [ #  # ]:          0 :                 if (rev->epdata.cb_fun)
    1292                 :          0 :                         rev->epdata.cb_fun(rev->fd,
    1293                 :            :                                            rev->epdata.cb_arg);
    1294                 :            : 
    1295                 :            :                 /* the status update should be observed after
    1296                 :            :                  * the other fields change.
    1297                 :            :                  */
    1298                 :          0 :                 rte_atomic_store_explicit(&rev->status, RTE_EPOLL_VALID,
    1299                 :            :                                 rte_memory_order_release);
    1300                 :          0 :                 count++;
    1301                 :            :         }
    1302                 :          0 :         return count;
    1303                 :            : }
    1304                 :            : 
    1305                 :            : static inline int
    1306                 :          0 : eal_init_tls_epfd(void)
    1307                 :            : {
    1308                 :          0 :         int pfd = epoll_create(255);
    1309                 :            : 
    1310         [ #  # ]:          0 :         if (pfd < 0) {
    1311                 :          0 :                 EAL_LOG(ERR,
    1312                 :            :                         "Cannot create epoll instance");
    1313                 :          0 :                 return -1;
    1314                 :            :         }
    1315                 :            :         return pfd;
    1316                 :            : }
    1317                 :            : 
    1318                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_tls_epfd)
    1319                 :            : int
    1320                 :          0 : rte_intr_tls_epfd(void)
    1321                 :            : {
    1322         [ #  # ]:          0 :         if (RTE_PER_LCORE(_epfd) == -1)
    1323                 :          0 :                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
    1324                 :            : 
    1325                 :          0 :         return RTE_PER_LCORE(_epfd);
    1326                 :            : }
    1327                 :            : 
    1328                 :            : static int
    1329                 :          0 : eal_epoll_wait(int epfd, struct rte_epoll_event *events,
    1330                 :            :                int maxevents, int timeout, bool interruptible)
    1331                 :            : {
    1332                 :            :         int rc;
    1333                 :            :         uint32_t i, k, n, num;
    1334                 :            :         struct epoll_event evs[MAX_ITER_EVNUM];
    1335                 :            : 
    1336         [ #  # ]:          0 :         if (!events) {
    1337                 :          0 :                 EAL_LOG(ERR, "rte_epoll_event can't be NULL");
    1338                 :          0 :                 return -1;
    1339                 :            :         }
    1340                 :            : 
    1341                 :            :         /* using per thread epoll fd */
    1342         [ #  # ]:          0 :         if (epfd == RTE_EPOLL_PER_THREAD)
    1343                 :          0 :                 epfd = rte_intr_tls_epfd();
    1344                 :            : 
    1345                 :          0 :         num = maxevents;
    1346                 :          0 :         n = RTE_MIN(RTE_DIM(evs), num);
    1347                 :            : 
    1348                 :            :         /* Process events in chunks of MAX_ITER_EVNUM */
    1349                 :            : 
    1350                 :            :         while (1) {
    1351                 :          0 :                 rc = epoll_wait(epfd, evs, n, timeout);
    1352         [ #  # ]:          0 :                 if (likely(rc > 0)) {
    1353                 :            : 
    1354                 :            :                         /* epoll_wait has at least one fd ready to read */
    1355         [ #  # ]:          0 :                         for (i = 0, k = 0; rc > 0;) {
    1356                 :          0 :                                 k += rc;
    1357                 :          0 :                                 rc = eal_epoll_process_event(evs, rc,
    1358                 :          0 :                                                 events + i);
    1359                 :          0 :                                 i += rc;
    1360                 :            : 
    1361                 :            :                                 /*
    1362                 :            :                                  * try to read more events that are already
    1363                 :            :                                  * available (up to maxevents in total).
    1364                 :            :                                  */
    1365                 :          0 :                                 n = RTE_MIN(RTE_DIM(evs), num - k);
    1366         [ #  # ]:          0 :                                 rc = (n == 0) ? 0 : epoll_wait(epfd, evs, n, 0);
    1367                 :            :                         }
    1368                 :          0 :                         return i;
    1369                 :            : 
    1370         [ #  # ]:          0 :                 } else if (rc < 0) {
    1371         [ #  # ]:          0 :                         if (errno == EINTR) {
    1372         [ #  # ]:          0 :                                 if (interruptible)
    1373                 :            :                                         return -1;
    1374                 :            :                                 else
    1375                 :            :                                         continue;
    1376                 :            :                         }
    1377                 :            :                         /* epoll_wait fail */
    1378                 :          0 :                         EAL_LOG(ERR, "epoll_wait returns with fail %s",
    1379                 :            :                                 strerror(errno));
    1380                 :            :                         rc = -1;
    1381                 :          0 :                         break;
    1382                 :            :                 } else {
    1383                 :            :                         /* rc == 0, epoll_wait timed out */
    1384                 :            :                         break;
    1385                 :            :                 }
    1386                 :            :         }
    1387                 :            : 
    1388                 :            :         return rc;
    1389                 :            : }
    1390                 :            : 
    1391                 :            : RTE_EXPORT_SYMBOL(rte_epoll_wait)
    1392                 :            : int
    1393                 :          0 : rte_epoll_wait(int epfd, struct rte_epoll_event *events,
    1394                 :            :                int maxevents, int timeout)
    1395                 :            : {
    1396                 :          0 :         return eal_epoll_wait(epfd, events, maxevents, timeout, false);
    1397                 :            : }
    1398                 :            : 
    1399                 :            : RTE_EXPORT_SYMBOL(rte_epoll_wait_interruptible)
    1400                 :            : int
    1401                 :          0 : rte_epoll_wait_interruptible(int epfd, struct rte_epoll_event *events,
    1402                 :            :                              int maxevents, int timeout)
    1403                 :            : {
    1404                 :          0 :         return eal_epoll_wait(epfd, events, maxevents, timeout, true);
    1405                 :            : }
    1406                 :            : 
    1407                 :            : static inline void
    1408                 :          0 : eal_epoll_data_safe_free(struct rte_epoll_event *ev)
    1409                 :            : {
    1410                 :            :         uint32_t valid_status = RTE_EPOLL_VALID;
    1411                 :            : 
    1412         [ #  # ]:          0 :         while (!rte_atomic_compare_exchange_strong_explicit(&ev->status, &valid_status,
    1413                 :            :                     RTE_EPOLL_INVALID, rte_memory_order_acquire, rte_memory_order_relaxed)) {
    1414                 :          0 :                 while (rte_atomic_load_explicit(&ev->status,
    1415         [ #  # ]:          0 :                                 rte_memory_order_relaxed) != RTE_EPOLL_VALID)
    1416                 :            :                         rte_pause();
    1417                 :            :                 valid_status = RTE_EPOLL_VALID;
    1418                 :            :         }
    1419                 :          0 :         memset(&ev->epdata, 0, sizeof(ev->epdata));
    1420                 :          0 :         ev->fd = -1;
    1421                 :          0 :         ev->epfd = -1;
    1422                 :          0 : }
    1423                 :            : 
    1424                 :            : RTE_EXPORT_SYMBOL(rte_epoll_ctl)
    1425                 :            : int
    1426                 :          0 : rte_epoll_ctl(int epfd, int op, int fd,
    1427                 :            :               struct rte_epoll_event *event)
    1428                 :            : {
    1429                 :            :         struct epoll_event ev;
    1430                 :            : 
    1431         [ #  # ]:          0 :         if (!event) {
    1432                 :          0 :                 EAL_LOG(ERR, "rte_epoll_event can't be NULL");
    1433                 :          0 :                 return -EINVAL;
    1434                 :            :         }
    1435                 :            : 
    1436                 :            :         /* using per thread epoll fd */
    1437         [ #  # ]:          0 :         if (epfd == RTE_EPOLL_PER_THREAD)
    1438                 :          0 :                 epfd = rte_intr_tls_epfd();
    1439                 :            : 
    1440         [ #  # ]:          0 :         if (op == EPOLL_CTL_ADD) {
    1441                 :          0 :                 rte_atomic_store_explicit(&event->status, RTE_EPOLL_VALID,
    1442                 :            :                                 rte_memory_order_relaxed);
    1443                 :          0 :                 event->fd = fd;  /* ignore fd in event */
    1444                 :          0 :                 event->epfd = epfd;
    1445                 :          0 :                 ev.data.ptr = (void *)event;
    1446                 :            :         }
    1447                 :            : 
    1448                 :          0 :         ev.events = event->epdata.event;
    1449         [ #  # ]:          0 :         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
    1450                 :          0 :                 int err = errno;
    1451                 :            : 
    1452                 :            :                 /* the fd is already in the set (e.g. shared across vectors):
    1453                 :            :                  * keep the event valid and report -EEXIST, not a hard error.
    1454                 :            :                  */
    1455         [ #  # ]:          0 :                 if (op == EPOLL_CTL_ADD && err == EEXIST)
    1456                 :            :                         return -EEXIST;
    1457                 :            : 
    1458                 :          0 :                 EAL_LOG(ERR, "Error op %d fd %d epoll_ctl, %s",
    1459                 :            :                         op, fd, strerror(err));
    1460         [ #  # ]:          0 :                 if (op == EPOLL_CTL_ADD)
    1461                 :            :                         /* rollback status when CTL_ADD fail */
    1462                 :          0 :                         rte_atomic_store_explicit(&event->status, RTE_EPOLL_INVALID,
    1463                 :            :                                         rte_memory_order_relaxed);
    1464                 :          0 :                 return -err;
    1465                 :            :         }
    1466                 :            : 
    1467   [ #  #  #  # ]:          0 :         if (op == EPOLL_CTL_DEL && rte_atomic_load_explicit(&event->status,
    1468                 :            :                         rte_memory_order_relaxed) != RTE_EPOLL_INVALID)
    1469                 :          0 :                 eal_epoll_data_safe_free(event);
    1470                 :            : 
    1471                 :            :         return 0;
    1472                 :            : }
    1473                 :            : 
    1474                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_rx_ctl)
    1475                 :            : int
    1476                 :          0 : rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
    1477                 :            :                 int op, unsigned int vec, void *data)
    1478                 :            : {
    1479                 :            :         struct rte_epoll_event *rev;
    1480                 :            :         struct rte_epoll_data *epdata;
    1481                 :            :         int epfd_op;
    1482                 :            :         unsigned int efd_idx;
    1483                 :            :         int rc = 0;
    1484                 :            : 
    1485                 :            :         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
    1486         [ #  # ]:          0 :                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
    1487                 :            : 
    1488   [ #  #  #  # ]:          0 :         if (intr_handle == NULL || rte_intr_nb_efd_get(intr_handle) == 0 ||
    1489         [ #  # ]:          0 :                         efd_idx >= (unsigned int)rte_intr_nb_efd_get(intr_handle)) {
    1490                 :          0 :                 EAL_LOG(ERR, "Wrong intr vector number.");
    1491                 :          0 :                 return -EPERM;
    1492                 :            :         }
    1493                 :            : 
    1494      [ #  #  # ]:          0 :         switch (op) {
    1495                 :          0 :         case RTE_INTR_EVENT_ADD:
    1496                 :            :                 epfd_op = EPOLL_CTL_ADD;
    1497                 :          0 :                 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
    1498         [ #  # ]:          0 :                 if (rte_atomic_load_explicit(&rev->status,
    1499                 :            :                                 rte_memory_order_relaxed) != RTE_EPOLL_INVALID) {
    1500                 :          0 :                         EAL_LOG(INFO, "Event already been added.");
    1501                 :          0 :                         return -EEXIST;
    1502                 :            :                 }
    1503                 :            : 
    1504                 :            :                 /* attach to intr vector fd */
    1505                 :            :                 epdata = &rev->epdata;
    1506                 :          0 :                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
    1507                 :          0 :                 epdata->data   = data;
    1508                 :          0 :                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
    1509                 :          0 :                 epdata->cb_arg = (void *)intr_handle;
    1510                 :          0 :                 rc = rte_epoll_ctl(epfd, epfd_op,
    1511                 :            :                         rte_intr_efds_index_get(intr_handle, efd_idx), rev);
    1512         [ #  # ]:          0 :                 if (!rc)
    1513                 :          0 :                         EAL_LOG(DEBUG,
    1514                 :            :                                 "efd %d associated with vec %d added on epfd %d",
    1515                 :            :                                 rev->fd, vec, epfd);
    1516                 :            :                 break;
    1517                 :          0 :         case RTE_INTR_EVENT_DEL:
    1518                 :            :                 epfd_op = EPOLL_CTL_DEL;
    1519                 :          0 :                 rev = rte_intr_elist_index_get(intr_handle, efd_idx);
    1520         [ #  # ]:          0 :                 if (rte_atomic_load_explicit(&rev->status,
    1521                 :            :                                 rte_memory_order_relaxed) == RTE_EPOLL_INVALID) {
    1522                 :          0 :                         EAL_LOG(INFO, "Event does not exist.");
    1523                 :          0 :                         return -EPERM;
    1524                 :            :                 }
    1525                 :            : 
    1526                 :          0 :                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
    1527                 :          0 :                 break;
    1528                 :          0 :         default:
    1529                 :          0 :                 EAL_LOG(ERR, "event op type mismatch");
    1530                 :            :                 rc = -EPERM;
    1531                 :            :         }
    1532                 :            : 
    1533                 :            :         return rc;
    1534                 :            : }
    1535                 :            : 
    1536                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_free_epoll_fd)
    1537                 :            : void
    1538                 :          8 : rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
    1539                 :            : {
    1540                 :            :         uint32_t i;
    1541                 :            :         struct rte_epoll_event *rev;
    1542                 :            : 
    1543         [ -  + ]:          8 :         for (i = 0; i < (uint32_t)rte_intr_nb_efd_get(intr_handle); i++) {
    1544                 :          0 :                 rev = rte_intr_elist_index_get(intr_handle, i);
    1545         [ #  # ]:          0 :                 if (rte_atomic_load_explicit(&rev->status,
    1546                 :            :                                 rte_memory_order_relaxed) == RTE_EPOLL_INVALID)
    1547                 :          0 :                         continue;
    1548         [ #  # ]:          0 :                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
    1549                 :            :                         /* force free if the entry valid */
    1550                 :          0 :                         eal_epoll_data_safe_free(rev);
    1551                 :            :                 }
    1552                 :            :         }
    1553                 :          8 : }
    1554                 :            : 
    1555                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_efd_enable)
    1556                 :            : int
    1557                 :          0 : rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
    1558                 :            : {
    1559                 :          0 :         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
    1560                 :            :         int ret;
    1561                 :            : 
    1562         [ #  # ]:          0 :         assert(nb_efd != 0);
    1563                 :            : 
    1564         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX) {
    1565         [ #  # ]:          0 :                 if (rte_intr_nb_efd_get(intr_handle) != 0) {
    1566                 :          0 :                         EAL_LOG(ERR, "Interrupt eventfds are already enabled");
    1567                 :          0 :                         return -EBUSY;
    1568                 :            :                 }
    1569                 :            : 
    1570         [ #  # ]:          0 :                 for (uint32_t i = 0; i < n; i++) {
    1571                 :          0 :                         int fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
    1572                 :            : 
    1573         [ #  # ]:          0 :                         if (fd < 0) {
    1574                 :          0 :                                 ret = -errno;
    1575                 :          0 :                                 EAL_LOG(ERR,
    1576                 :            :                                         "can't setup eventfd, error %i (%s)",
    1577                 :            :                                         errno, strerror(errno));
    1578                 :          0 :                                 goto error;
    1579                 :            :                         }
    1580                 :            : 
    1581                 :            :                         /* Record each eventfd as it is created so that the
    1582                 :            :                          * error path can close exactly those that exist.
    1583                 :            :                          */
    1584   [ #  #  #  # ]:          0 :                         if (rte_intr_efds_index_set(intr_handle, i, fd) ||
    1585                 :          0 :                                         rte_intr_nb_efd_set(intr_handle, i + 1)) {
    1586                 :          0 :                                 ret = -rte_errno;
    1587                 :          0 :                                 close(fd);
    1588                 :          0 :                                 rte_intr_efds_index_set(intr_handle, i, -1);
    1589                 :          0 :                                 goto error;
    1590                 :            :                         }
    1591                 :            :                 }
    1592                 :            : 
    1593         [ #  # ]:          0 :                 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR + n)) {
    1594                 :          0 :                         ret = -rte_errno;
    1595                 :          0 :                         goto error;
    1596                 :            :                 }
    1597         [ #  # ]:          0 :         } else if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV) {
    1598                 :            :                 /* only check, initialization would be done in vdev driver.*/
    1599         [ #  # ]:          0 :                 if ((uint64_t)rte_intr_efd_counter_size_get(intr_handle) >
    1600                 :            :                     sizeof(union rte_intr_read_buffer)) {
    1601                 :          0 :                         EAL_LOG(ERR, "the efd_counter_size is oversized");
    1602                 :          0 :                         return -EINVAL;
    1603                 :            :                 }
    1604                 :            :         } else {
    1605         [ #  # ]:          0 :                 if (rte_intr_efds_index_set(intr_handle, 0, rte_intr_fd_get(intr_handle)))
    1606                 :          0 :                         return -rte_errno;
    1607         [ #  # ]:          0 :                 if (rte_intr_nb_efd_set(intr_handle, RTE_MIN(nb_efd, 1U)))
    1608                 :          0 :                         return -rte_errno;
    1609         [ #  # ]:          0 :                 if (rte_intr_max_intr_set(intr_handle, NB_OTHER_INTR))
    1610                 :          0 :                         return -rte_errno;
    1611                 :            :         }
    1612                 :            : 
    1613                 :            :         return 0;
    1614                 :            : 
    1615                 :          0 : error:
    1616         [ #  # ]:          0 :         for (int i = 0; i < rte_intr_nb_efd_get(intr_handle); i++) {
    1617                 :          0 :                 close(rte_intr_efds_index_get(intr_handle, i));
    1618                 :          0 :                 rte_intr_efds_index_set(intr_handle, i, -1);
    1619                 :            :         }
    1620                 :          0 :         rte_intr_nb_efd_set(intr_handle, 0);
    1621                 :            : 
    1622                 :          0 :         return ret;
    1623                 :            : }
    1624                 :            : 
    1625                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_efd_disable)
    1626                 :            : void
    1627                 :          0 : rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
    1628                 :            : {
    1629                 :          0 :         rte_intr_free_epoll_fd(intr_handle);
    1630                 :            :         /* Only eventfds created by rte_intr_efd_enable() for MSI-X are owned
    1631                 :            :          * by the handle. In other modes the entries reference fds owned by
    1632                 :            :          * the device or the driver and must not be closed here.
    1633                 :            :          */
    1634         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX) {
    1635         [ #  # ]:          0 :                 for (int i = 0; i < rte_intr_nb_efd_get(intr_handle); i++)
    1636                 :          0 :                         close(rte_intr_efds_index_get(intr_handle, i));
    1637                 :            :         }
    1638                 :            :         /* VDEV is responsible for efds, see rte_intr_efd_enable(). */
    1639         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) != RTE_INTR_HANDLE_VDEV) {
    1640         [ #  # ]:          0 :                 for (int i = 0; i < rte_intr_nb_efd_get(intr_handle); i++)
    1641                 :          0 :                         rte_intr_efds_index_set(intr_handle, i, -1);
    1642                 :            :         }
    1643                 :          0 :         rte_intr_nb_efd_set(intr_handle, 0);
    1644                 :          0 :         rte_intr_max_intr_set(intr_handle, 0);
    1645                 :          0 : }
    1646                 :            : 
    1647                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_dp_is_en)
    1648                 :            : int
    1649                 :          0 : rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
    1650                 :            : {
    1651                 :          0 :         return !(!rte_intr_nb_efd_get(intr_handle));
    1652                 :            : }
    1653                 :            : 
    1654                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_allow_others)
    1655                 :            : int
    1656                 :          0 : rte_intr_allow_others(struct rte_intr_handle *intr_handle)
    1657                 :            : {
    1658         [ #  # ]:          0 :         if (!rte_intr_dp_is_en(intr_handle))
    1659                 :            :                 return 1;
    1660                 :            :         else
    1661                 :          0 :                 return !!(rte_intr_max_intr_get(intr_handle) -
    1662                 :          0 :                                 rte_intr_nb_efd_get(intr_handle));
    1663                 :            : }
    1664                 :            : 
    1665                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_cap_multiple)
    1666                 :            : int
    1667                 :          0 : rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
    1668                 :            : {
    1669         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VFIO_MSIX)
    1670                 :            :                 return 1;
    1671                 :            : 
    1672         [ #  # ]:          0 :         if (rte_intr_type_get(intr_handle) == RTE_INTR_HANDLE_VDEV)
    1673                 :          0 :                 return 1;
    1674                 :            : 
    1675                 :            :         return 0;
    1676                 :            : }
    1677                 :            : 
    1678                 :            : RTE_EXPORT_SYMBOL(rte_thread_is_intr)
    1679                 :          0 : int rte_thread_is_intr(void)
    1680                 :            : {
    1681                 :          0 :         return rte_thread_equal(intr_thread, rte_thread_self());
    1682                 :            : }
    1683                 :            : 
    1684                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_intr_active_events_flags)
    1685                 :            : uint32_t
    1686                 :          0 : rte_intr_active_events_flags(void)
    1687                 :            : {
    1688         [ #  # ]:          0 :         if (rte_thread_is_intr())
    1689                 :          0 :                 return active_events;
    1690                 :            : 
    1691                 :            :         return 0;
    1692                 :            : }

Generated by: LCOV version 1.14