security: fix enum start value
[dpdk.git] / drivers / bus / fslmc / fslmc_vfio.c
index 22b01e1..9f28fa0 100644 (file)
@@ -1,34 +1,8 @@
-/*-
- *   BSD LICENSE
+/* SPDX-License-Identifier: BSD-3-Clause
  *
  *   Copyright (c) 2015-2016 Freescale Semiconductor, Inc. All rights reserved.
- *   Copyright 2016 NXP.
+ *   Copyright 2016 NXP
  *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Freescale Semiconductor, Inc nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <unistd.h>
@@ -46,6 +20,7 @@
 #include <dirent.h>
 #include <sys/eventfd.h>
 
+#include <eal_filesystem.h>
 #include <rte_mbuf.h>
 #include <rte_ethdev.h>
 #include <rte_malloc.h>
@@ -248,7 +223,10 @@ int rte_fslmc_vfio_dmamap(void)
                dma_map.size = memseg[i].len;
                dma_map.vaddr = memseg[i].addr_64;
 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
-               dma_map.iova = memseg[i].phys_addr;
+               if (rte_eal_iova_mode() == RTE_IOVA_VA)
+                       dma_map.iova = dma_map.vaddr;
+               else
+                       dma_map.iova = memseg[i].iova;
 #else
                dma_map.iova = dma_map.vaddr;
 #endif
@@ -335,36 +313,107 @@ MC_FAILURE:
 
 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
 
-int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
-                         uint32_t index)
+int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
 {
-       struct vfio_irq_set *irq_set;
+       int len, ret;
        char irq_set_buf[IRQ_SET_BUF_LEN];
-       int *fd_ptr, fd, ret;
+       struct vfio_irq_set *irq_set;
+       int *fd_ptr;
+
+       len = sizeof(irq_set_buf);
 
-       /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
-       /* Give the eventfd to VFIO */
-       fd = eventfd(0, 0);
        irq_set = (struct vfio_irq_set *)irq_set_buf;
-       irq_set->argsz = sizeof(irq_set_buf);
+       irq_set->argsz = len;
        irq_set->count = 1;
-       irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
-                        VFIO_IRQ_SET_ACTION_TRIGGER;
+       irq_set->flags =
+               VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
        irq_set->index = index;
        irq_set->start = 0;
        fd_ptr = (int *)&irq_set->data;
-       *fd_ptr = fd;
+       *fd_ptr = intr_handle->fd;
 
        ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
-       if (ret < 0) {
-               FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
-                              ret);
-               return -1;
+       if (ret) {
+               RTE_LOG(ERR, EAL, "Error:dpaa2 SET IRQs fd=%d, err = %d(%s)\n",
+                       intr_handle->fd, errno, strerror(errno));
+               return ret;
        }
 
-       /* Set the FD and update the flags */
-       intr_handle->fd = fd;
-       return 0;
+       return ret;
+}
+
+int rte_dpaa2_intr_disable(struct rte_intr_handle *intr_handle, int index)
+{
+       struct vfio_irq_set *irq_set;
+       char irq_set_buf[IRQ_SET_BUF_LEN];
+       int len, ret;
+
+       len = sizeof(struct vfio_irq_set);
+
+       irq_set = (struct vfio_irq_set *)irq_set_buf;
+       irq_set->argsz = len;
+       irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
+       irq_set->index = index;
+       irq_set->start = 0;
+       irq_set->count = 0;
+
+       ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
+       if (ret)
+               RTE_LOG(ERR, EAL,
+                       "Error disabling dpaa2 interrupts for fd %d\n",
+                       intr_handle->fd);
+
+       return ret;
+}
+
+/* set up interrupt support (but not enable interrupts) */
+int
+rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
+                         int vfio_dev_fd,
+                         int num_irqs)
+{
+       int i, ret;
+
+       /* start from MSI-X interrupt type */
+       for (i = 0; i < num_irqs; i++) {
+               struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
+               int fd = -1;
+
+               irq_info.index = i;
+
+               ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+               if (ret < 0) {
+                       FSLMC_VFIO_LOG(ERR,
+                                      "cannot get IRQ(%d) info, error %i (%s)",
+                                      i, errno, strerror(errno));
+                       return -1;
+               }
+
+               /* if this vector cannot be used with eventfd,
+                * fail if we explicitly
+                * specified interrupt type, otherwise continue
+                */
+               if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) == 0)
+                       continue;
+
+               /* set up an eventfd for interrupts */
+               fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+               if (fd < 0) {
+                       FSLMC_VFIO_LOG(ERR,
+                                      "cannot set up eventfd, error %i (%s)\n",
+                                      errno, strerror(errno));
+                       return -1;
+               }
+
+               intr_handle->fd = fd;
+               intr_handle->type = RTE_INTR_HANDLE_VFIO_MSI;
+               intr_handle->vfio_dev_fd = vfio_dev_fd;
+
+               return 0;
+       }
+
+       /* if we're here, we haven't found a suitable interrupt vector */
+       return -1;
 }
 
 /*
@@ -392,6 +441,10 @@ fslmc_process_iodevices(struct rte_dpaa2_device *dev)
        }
 
        switch (dev->dev_type) {
+       case DPAA2_ETH:
+               rte_dpaa2_vfio_setup_intr(&dev->intr_handle, dev_fd,
+                                         device_info.num_irqs);
+               break;
        case DPAA2_CON:
        case DPAA2_IO:
        case DPAA2_CI:
@@ -584,12 +637,14 @@ fslmc_vfio_setup_group(void)
        if (ret) {
                FSLMC_VFIO_LOG(ERR, "VFIO error getting group status");
                close(vfio_group.fd);
+               rte_vfio_clear_group(vfio_group.fd);
                return ret;
        }
 
        if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
                FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
                close(vfio_group.fd);
+               rte_vfio_clear_group(vfio_group.fd);
                return -EPERM;
        }
        /* Since Group is VIABLE, Store the groupid */
@@ -604,6 +659,7 @@ fslmc_vfio_setup_group(void)
                                "Error connecting container with groupid %d",
                                groupid);
                        close(vfio_group.fd);
+                       rte_vfio_clear_group(vfio_group.fd);
                        return ret;
                }
        }
@@ -614,6 +670,7 @@ fslmc_vfio_setup_group(void)
                FSLMC_VFIO_LOG(ERR, "Error getting device %s fd from group %d",
                               g_container, vfio_group.groupid);
                close(vfio_group.fd);
+               rte_vfio_clear_group(vfio_group.fd);
                return ret;
        }
        container_device_fd = ret;