vfio: fix disabling INTx
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.c
index 8758239..029a147 100644 (file)
@@ -45,6 +45,7 @@
 #include <sys/signalfd.h>
 #include <sys/ioctl.h>
 #include <sys/eventfd.h>
+#include <assert.h>
 
 #include <rte_common.h>
 #include <rte_interrupts.h>
 #include <rte_lcore.h>
 #include <rte_atomic.h>
 #include <rte_branch_prediction.h>
-#include <rte_ring.h>
 #include <rte_debug.h>
 #include <rte_log.h>
-#include <rte_mempool.h>
 #include <rte_pci.h>
 #include <rte_malloc.h>
 #include <rte_errno.h>
@@ -137,7 +136,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;
@@ -184,7 +183,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;
@@ -195,14 +194,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;
        }
@@ -227,7 +226,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;
@@ -256,7 +255,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;
@@ -279,9 +278,32 @@ vfio_disable_msi(struct rte_intr_handle *intr_handle) {
        return ret;
 }
 
+static int
+get_max_intr(const struct rte_intr_handle *intr_handle)
+{
+       struct rte_intr_source *src;
+
+       TAILQ_FOREACH(src, &intr_sources, next) {
+               if (src->intr_handle.fd != intr_handle->fd)
+                       continue;
+
+               if (src->intr_handle.max_intr < intr_handle->max_intr)
+                       src->intr_handle.max_intr = intr_handle->max_intr;
+               if (!src->intr_handle.max_intr)
+                       src->intr_handle.max_intr = 1;
+               else if (src->intr_handle.max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
+                       src->intr_handle.max_intr
+                               = RTE_MAX_RXTX_INTR_VEC_ID + 1;
+
+               return src->intr_handle.max_intr;
+       }
+
+       return -1;
+}
+
 /* 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;
@@ -291,12 +313,15 @@ 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;
+       ret = get_max_intr(intr_handle);
+       if (ret < 0) {
+               RTE_LOG(ERR, EAL, "Invalid number of MSI-X irqs for fd %d\n",
+                       intr_handle->fd);
+               return -1;
+       }
+
+       irq_set->count = ret;
        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;
@@ -319,7 +344,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;
@@ -344,7 +369,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;
 
@@ -368,7 +393,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;
 
@@ -392,7 +417,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;
 
@@ -406,7 +431,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;
 
@@ -420,7 +445,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;
@@ -492,7 +517,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;
@@ -556,7 +581,7 @@ 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->fd < 0 || intr_handle->uio_cfg_fd < 0)
                return -1;
@@ -600,7 +625,7 @@ 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->fd < 0 || intr_handle->uio_cfg_fd < 0)
                return -1;
@@ -873,22 +898,25 @@ 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 {
                /* Set thread_name for aid in debugging. */
                snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
                        "eal-intr-thread");
-               ret_1 = pthread_setname_np(intr_thread, thread_name);
+               ret_1 = rte_thread_setname(intr_thread, thread_name);
                if (ret_1 != 0)
-                       RTE_LOG(ERR, EAL,
+                       RTE_LOG(DEBUG, EAL,
                        "Failed to set thread name for interrupt handling\n");
        }
 
@@ -900,6 +928,7 @@ eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
 {
        union rte_intr_read_buffer buf;
        int bytes_read = 1;
+       int nbytes;
 
        switch (intr_handle->type) {
        case RTE_INTR_HANDLE_UIO:
@@ -924,15 +953,15 @@ eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
         * for epoll_wait.
         */
        do {
-               bytes_read = read(fd, &buf, bytes_read);
-               if (bytes_read < 0) {
+               nbytes = read(fd, &buf, bytes_read);
+               if (nbytes < 0) {
                        if (errno == EINTR || errno == EWOULDBLOCK ||
                            errno == EAGAIN)
                                continue;
                        RTE_LOG(ERR, EAL,
                                "Error reading from fd %d: %s\n",
                                fd, strerror(errno));
-               } else if (bytes_read == 0)
+               } else if (nbytes == 0)
                        RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
                return;
        } while (1);
@@ -1148,6 +1177,8 @@ rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
        int fd;
        uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
 
+       assert(nb_efd != 0);
+
        if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
                for (i = 0; i < n; i++) {
                        fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
@@ -1155,7 +1186,7 @@ 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;
                }
@@ -1204,5 +1235,17 @@ rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
 int
 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
 {
-       return !!(intr_handle->max_intr - intr_handle->nb_efd);
+       if (!rte_intr_dp_is_en(intr_handle))
+               return 1;
+       else
+               return !!(intr_handle->max_intr - intr_handle->nb_efd);
+}
+
+int
+rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
+{
+       if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
+               return 1;
+
+       return 0;
 }