Imported Upstream version 17.05
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.c
index 47a3b20..2e3bd12 100644 (file)
@@ -46,6 +46,7 @@
 #include <sys/ioctl.h>
 #include <sys/eventfd.h>
 #include <assert.h>
+#include <stdbool.h>
 
 #include <rte_common.h>
 #include <rte_interrupts.h>
@@ -136,7 +137,7 @@ static pthread_t intr_thread;
 
 /* enable legacy (INTx) interrupts */
 static int
-vfio_enable_intx(struct rte_intr_handle *intr_handle) {
+vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
        char irq_set_buf[IRQ_SET_BUF_LEN];
        int len, ret;
@@ -183,7 +184,7 @@ vfio_enable_intx(struct rte_intr_handle *intr_handle) {
 
 /* disable legacy (INTx) interrupts */
 static int
-vfio_disable_intx(struct rte_intr_handle *intr_handle) {
+vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
        char irq_set_buf[IRQ_SET_BUF_LEN];
        int len, ret;
@@ -194,14 +195,14 @@ vfio_disable_intx(struct rte_intr_handle *intr_handle) {
        irq_set = (struct vfio_irq_set *) irq_set_buf;
        irq_set->argsz = len;
        irq_set->count = 1;
-       irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
+       irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
        irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
        irq_set->start = 0;
 
        ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
 
        if (ret) {
-               RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
+               RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
                                                intr_handle->fd);
                return -1;
        }
@@ -226,7 +227,7 @@ vfio_disable_intx(struct rte_intr_handle *intr_handle) {
 
 /* enable MSI interrupts */
 static int
-vfio_enable_msi(struct rte_intr_handle *intr_handle) {
+vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
        int len, ret;
        char irq_set_buf[IRQ_SET_BUF_LEN];
        struct vfio_irq_set *irq_set;
@@ -255,7 +256,7 @@ vfio_enable_msi(struct rte_intr_handle *intr_handle) {
 
 /* disable MSI interrupts */
 static int
-vfio_disable_msi(struct rte_intr_handle *intr_handle) {
+vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
        char irq_set_buf[IRQ_SET_BUF_LEN];
        int len, ret;
@@ -280,7 +281,7 @@ vfio_disable_msi(struct rte_intr_handle *intr_handle) {
 
 /* enable MSI-X interrupts */
 static int
-vfio_enable_msix(struct rte_intr_handle *intr_handle) {
+vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
        int len, ret;
        char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
        struct vfio_irq_set *irq_set;
@@ -290,12 +291,10 @@ vfio_enable_msix(struct rte_intr_handle *intr_handle) {
 
        irq_set = (struct vfio_irq_set *) irq_set_buf;
        irq_set->argsz = len;
-       if (!intr_handle->max_intr)
-               intr_handle->max_intr = 1;
-       else if (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
-               intr_handle->max_intr = RTE_MAX_RXTX_INTR_VEC_ID + 1;
-
-       irq_set->count = intr_handle->max_intr;
+       /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
+       irq_set->count = intr_handle->max_intr ?
+               (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
+               RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
        irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
        irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
        irq_set->start = 0;
@@ -318,7 +317,7 @@ vfio_enable_msix(struct rte_intr_handle *intr_handle) {
 
 /* disable MSI-X interrupts */
 static int
-vfio_disable_msix(struct rte_intr_handle *intr_handle) {
+vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
        char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
        int len, ret;
@@ -343,7 +342,7 @@ vfio_disable_msix(struct rte_intr_handle *intr_handle) {
 #endif
 
 static int
-uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
+uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
 {
        unsigned char command_high;
 
@@ -367,7 +366,7 @@ uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
 }
 
 static int
-uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
+uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
 {
        unsigned char command_high;
 
@@ -391,7 +390,7 @@ uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
 }
 
 static int
-uio_intr_disable(struct rte_intr_handle *intr_handle)
+uio_intr_disable(const struct rte_intr_handle *intr_handle)
 {
        const int value = 0;
 
@@ -405,7 +404,7 @@ uio_intr_disable(struct rte_intr_handle *intr_handle)
 }
 
 static int
-uio_intr_enable(struct rte_intr_handle *intr_handle)
+uio_intr_enable(const struct rte_intr_handle *intr_handle)
 {
        const int value = 1;
 
@@ -419,7 +418,7 @@ uio_intr_enable(struct rte_intr_handle *intr_handle)
 }
 
 int
-rte_intr_callback_register(struct rte_intr_handle *intr_handle,
+rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
                        rte_intr_callback_fn cb, void *cb_arg)
 {
        int ret, wake_thread;
@@ -491,7 +490,7 @@ rte_intr_callback_register(struct rte_intr_handle *intr_handle,
 }
 
 int
-rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
+rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
                        rte_intr_callback_fn cb_fn, void *cb_arg)
 {
        int ret;
@@ -555,8 +554,11 @@ rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
 }
 
 int
-rte_intr_enable(struct rte_intr_handle *intr_handle)
+rte_intr_enable(const struct rte_intr_handle *intr_handle)
 {
+       if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
+               return 0;
+
        if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
                return -1;
 
@@ -599,8 +601,11 @@ rte_intr_enable(struct rte_intr_handle *intr_handle)
 }
 
 int
-rte_intr_disable(struct rte_intr_handle *intr_handle)
+rte_intr_disable(const struct rte_intr_handle *intr_handle)
 {
+       if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
+               return 0;
+
        if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
                return -1;
 
@@ -645,6 +650,7 @@ rte_intr_disable(struct rte_intr_handle *intr_handle)
 static int
 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
 {
+       bool call = false;
        int n, bytes_read;
        struct rte_intr_source *src;
        struct rte_intr_callback *cb;
@@ -693,13 +699,18 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
                        bytes_read = sizeof(buf.vfio_intr_count);
                        break;
 #endif
+               case RTE_INTR_HANDLE_VDEV:
                case RTE_INTR_HANDLE_EXT:
+                       bytes_read = 0;
+                       call = true;
+                       break;
+
                default:
                        bytes_read = 1;
                        break;
                }
 
-               if (src->intr_handle.type != RTE_INTR_HANDLE_EXT) {
+               if (bytes_read > 0) {
                        /**
                         * read out to clear the ready-to-be-read flag
                         * for epoll_wait.
@@ -716,12 +727,14 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
                        } else if (bytes_read == 0)
                                RTE_LOG(ERR, EAL, "Read nothing from file "
                                        "descriptor %d\n", events[n].data.fd);
+                       else
+                               call = true;
                }
 
                /* grab a lock, again to call callbacks and update status. */
                rte_spinlock_lock(&intr_lock);
 
-               if (bytes_read > 0) {
+               if (call) {
 
                        /* Finally, call all callbacks. */
                        TAILQ_FOREACH(cb, &src->callbacks, next) {
@@ -731,8 +744,7 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
                                rte_spinlock_unlock(&intr_lock);
 
                                /* call the actual callback */
-                               active_cb.cb_fn(&src->intr_handle,
-                                       active_cb.cb_arg);
+                               active_cb.cb_fn(active_cb.cb_arg);
 
                                /*get the lock back. */
                                rte_spinlock_lock(&intr_lock);
@@ -832,7 +844,7 @@ eal_intr_thread_main(__rte_unused void *arg)
                TAILQ_FOREACH(src, &intr_sources, next) {
                        if (src->callbacks.tqh_first == NULL)
                                continue; /* skip those with no callbacks */
-                       ev.events = EPOLLIN | EPOLLPRI;
+                       ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
                        ev.data.fd = src->intr_handle.fd;
 
                        /**
@@ -872,13 +884,16 @@ rte_eal_intr_init(void)
         * create a pipe which will be waited by epoll and notified to
         * rebuild the wait list of epoll.
         */
-       if (pipe(intr_pipe.pipefd) < 0)
+       if (pipe(intr_pipe.pipefd) < 0) {
+               rte_errno = errno;
                return -1;
+       }
 
        /* create the host thread to wait/handle the interrupt */
        ret = pthread_create(&intr_thread, NULL,
                        eal_intr_thread_main, NULL);
        if (ret != 0) {
+               rte_errno = ret;
                RTE_LOG(ERR, EAL,
                        "Failed to create thread for interrupt handling\n");
        } else {
@@ -913,6 +928,14 @@ eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
                bytes_read = sizeof(buf.vfio_intr_count);
                break;
 #endif
+       case RTE_INTR_HANDLE_VDEV:
+               /* for vdev, fd points to:
+                * a. eventfd which does not need to read out;
+                * b. datapath fd which needs PMD to read out.
+                */
+               return;
+       case RTE_INTR_HANDLE_EXT:
+               return;
        default:
                bytes_read = 1;
                RTE_LOG(INFO, EAL, "unexpected intr type\n");
@@ -1141,6 +1164,24 @@ rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
        return rc;
 }
 
+void
+rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
+{
+       uint32_t i;
+       struct rte_epoll_event *rev;
+
+       for (i = 0; i < intr_handle->nb_efd; i++) {
+               rev = &intr_handle->elist[i];
+               if (rev->status == RTE_EPOLL_INVALID)
+                       continue;
+               if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
+                       /* force free if the entry valid */
+                       eal_epoll_data_safe_free(rev);
+                       rev->status = RTE_EPOLL_INVALID;
+               }
+       }
+}
+
 int
 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
 {
@@ -1157,12 +1198,14 @@ rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
                                RTE_LOG(ERR, EAL,
                                        "can't setup eventfd, error %i (%s)\n",
                                        errno, strerror(errno));
-                               return -1;
+                               return -errno;
                        }
                        intr_handle->efds[i] = fd;
                }
                intr_handle->nb_efd   = n;
                intr_handle->max_intr = NB_OTHER_INTR + n;
+       } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
+               /* do nothing, and let vdev driver to initialize this struct */
        } else {
                intr_handle->efds[0]  = intr_handle->fd;
                intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
@@ -1176,19 +1219,8 @@ void
 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
 {
        uint32_t i;
-       struct rte_epoll_event *rev;
-
-       for (i = 0; i < intr_handle->nb_efd; i++) {
-               rev = &intr_handle->elist[i];
-               if (rev->status == RTE_EPOLL_INVALID)
-                       continue;
-               if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
-                       /* force free if the entry valid */
-                       eal_epoll_data_safe_free(rev);
-                       rev->status = RTE_EPOLL_INVALID;
-               }
-       }
 
+       rte_intr_free_epoll_fd(intr_handle);
        if (intr_handle->max_intr > intr_handle->nb_efd) {
                for (i = 0; i < intr_handle->nb_efd; i++)
                        close(intr_handle->efds[i]);
@@ -1218,5 +1250,8 @@ rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
        if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
                return 1;
 
+       if (intr_handle->type == RTE_INTR_HANDLE_VDEV)
+               return 1;
+
        return 0;
 }