LCOV - code coverage report
Current view: top level - drivers/net/ntnic - ntnic_vfio.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 68 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 5 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 26 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2023 Napatech A/S
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <sys/ioctl.h>
       6                 :            : 
       7                 :            : #include <rte_malloc.h>
       8                 :            : #include <rte_memory.h>
       9                 :            : #include <dev_vfio.h>
      10                 :            : #include <rte_dev.h>
      11                 :            : #include <rte_bus_pci.h>
      12                 :            : #include <rte_spinlock.h>
      13                 :            : 
      14                 :            : #include <ntlog.h>
      15                 :            : #include <nt_util.h>
      16                 :            : #include "ntnic_vfio.h"
      17                 :            : 
      18                 :            : #define ONE_G_SIZE 0x40000000
      19                 :            : #define ONE_G_MASK (ONE_G_SIZE - 1)
      20                 :            : #define START_VF_IOVA 0x220000000000
      21                 :            : 
      22                 :            : static int
      23                 :            : nt_vfio_vf_num(const struct rte_pci_device *pdev)
      24                 :            : {
      25                 :          0 :         return ((pdev->addr.devid & 0x1f) << 3) + ((pdev->addr.function) & 0x7);
      26                 :            : }
      27                 :            : 
      28                 :            : /* Internal API */
      29                 :            : struct vfio_dev {
      30                 :            :         int container_fd;
      31                 :            :         int dev_fd;
      32                 :            :         uint64_t iova_addr;
      33                 :            : };
      34                 :            : 
      35                 :            : static struct vfio_dev vfio_list[256];
      36                 :            : 
      37                 :            : static struct vfio_dev *
      38                 :            : vfio_get(int vf_num)
      39                 :            : {
      40   [ #  #  #  #  :          0 :         if (vf_num < 0 || vf_num > 255)
             #  #  #  # ]
      41                 :            :                 return NULL;
      42                 :            : 
      43                 :          0 :         return &vfio_list[vf_num];
      44                 :            : }
      45                 :            : 
      46                 :            : /* External API */
      47                 :            : int
      48                 :          0 : nthw_vfio_setup(struct rte_pci_device *dev)
      49                 :            : {
      50                 :            :         int ret;
      51                 :          0 :         char devname[RTE_DEV_NAME_MAX_LEN] = { 0 };
      52                 :            :         int vf_num;
      53                 :            :         struct vfio_dev *vfio;
      54                 :            : 
      55                 :          0 :         NT_LOG(INF, NTNIC, "NT VFIO device setup %s", dev->name);
      56                 :            : 
      57                 :            :         vf_num = nt_vfio_vf_num(dev);
      58                 :            : 
      59                 :            :         vfio = vfio_get(vf_num);
      60                 :            : 
      61                 :            :         if (vfio == NULL) {
      62                 :          0 :                 NT_LOG(ERR, NTNIC, "VFIO device setup failed. Illegal device id");
      63                 :          0 :                 return -1;
      64                 :            :         }
      65                 :            : 
      66                 :          0 :         vfio->dev_fd = -1;
      67                 :          0 :         vfio->iova_addr = START_VF_IOVA;
      68                 :            : 
      69                 :          0 :         rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
      70         [ #  # ]:          0 :         if (vf_num == 0) {
      71                 :            :                 /* use default container for pf0 */
      72                 :          0 :                 vfio->container_fd = DEV_VFIO_DEFAULT_CONTAINER_FD;
      73                 :            : 
      74                 :            :         } else {
      75                 :          0 :                 vfio->container_fd = dev_vfio_container_create();
      76                 :            : 
      77         [ #  # ]:          0 :                 if (vfio->container_fd < 0) {
      78                 :          0 :                         NT_LOG(ERR, NTNIC,
      79                 :            :                                 "VFIO device setup failed. VFIO container creation failed.");
      80                 :          0 :                         return -1;
      81                 :            :                 }
      82                 :          0 :                 ret = dev_vfio_container_assign_device(vfio->container_fd,
      83                 :            :                                 rte_pci_get_sysfs_path(), devname);
      84         [ #  # ]:          0 :                 if (ret < 0) {
      85                 :          0 :                         NT_LOG(ERR, NTNIC,
      86                 :            :                                 "VFIO device setup failed. Assign device to container failed.");
      87                 :          0 :                         goto err;
      88                 :            :                 }
      89                 :            : 
      90         [ #  # ]:          0 :                 if (rte_pci_map_device(dev)) {
      91                 :          0 :                         NT_LOG(ERR, NTNIC,
      92                 :            :                                 "Map VFIO device failed. is the vfio-pci driver loaded?");
      93                 :          0 :                         goto err;
      94                 :            :                 }
      95                 :            :         }
      96                 :            : 
      97                 :          0 :         vfio->dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
      98                 :            : 
      99                 :          0 :         NT_LOG(DBG, NTNIC, "%s: VFIO id=%d, dev_fd=%d, container_fd=%d",
     100                 :            :                 dev->name, vf_num, vfio->dev_fd, vfio->container_fd);
     101                 :            : 
     102                 :          0 :         return vf_num;
     103                 :            : 
     104                 :          0 : err:
     105                 :            : 
     106         [ #  # ]:          0 :         if (vfio->container_fd != DEV_VFIO_DEFAULT_CONTAINER_FD)
     107                 :          0 :                 dev_vfio_container_destroy(vfio->container_fd);
     108                 :            : 
     109                 :            :         return -1;
     110                 :            : }
     111                 :            : 
     112                 :            : int
     113                 :          0 : nthw_vfio_remove(int vf_num)
     114                 :            : {
     115                 :            :         struct vfio_dev *vfio;
     116                 :            : 
     117                 :          0 :         NT_LOG(DBG, NTNIC, "NT VFIO device remove VF=%d", vf_num);
     118                 :            : 
     119                 :            :         vfio = vfio_get(vf_num);
     120                 :            : 
     121                 :            :         if (!vfio) {
     122                 :          0 :                 NT_LOG(ERR, NTNIC, "VFIO device remove failed. Illegal device id");
     123                 :          0 :                 return -1;
     124                 :            :         }
     125                 :            : 
     126                 :          0 :         dev_vfio_container_destroy(vfio->container_fd);
     127                 :          0 :         return 0;
     128                 :            : }
     129                 :            : 
     130                 :            : int
     131                 :          0 : nthw_vfio_dma_map(int vf_num, void *virt_addr, uint64_t *iova_addr, uint64_t size)
     132                 :            : {
     133                 :            :         uint64_t gp_virt_base;
     134                 :            :         uint64_t gp_offset;
     135                 :            : 
     136         [ #  # ]:          0 :         if (size == ONE_G_SIZE) {
     137                 :          0 :                 gp_virt_base = (uint64_t)virt_addr & ~ONE_G_MASK;
     138                 :          0 :                 gp_offset = (uint64_t)virt_addr & ONE_G_MASK;
     139                 :            : 
     140                 :            :         } else {
     141                 :          0 :                 gp_virt_base = (uint64_t)virt_addr;
     142                 :            :                 gp_offset = 0;
     143                 :            :         }
     144                 :            : 
     145                 :            :         struct vfio_dev *vfio;
     146                 :            : 
     147                 :            :         vfio = vfio_get(vf_num);
     148                 :            : 
     149                 :            :         if (vfio == NULL) {
     150                 :          0 :                 NT_LOG(ERR, NTNIC, "VFIO MAP: VF number %d invalid", vf_num);
     151                 :          0 :                 return -1;
     152                 :            :         }
     153                 :            : 
     154                 :          0 :         NT_LOG(DBG, NTNIC,
     155                 :            :                 "VFIO MMAP VF=%d VirtAddr=%p HPA=%" PRIX64 " VirtBase=%" PRIX64
     156                 :            :                 " IOVA Addr=%" PRIX64 " size=%" PRIX64,
     157                 :            :                 vf_num, virt_addr, rte_malloc_virt2iova(virt_addr), gp_virt_base, vfio->iova_addr,
     158                 :            :                 size);
     159                 :            : 
     160                 :          0 :         int res = dev_vfio_container_dma_map(vfio->container_fd, gp_virt_base, vfio->iova_addr,
     161                 :            :                         size);
     162                 :            : 
     163                 :          0 :         NT_LOG(DBG, NTNIC, "VFIO MMAP res %i, container_fd %i, vf_num %i", res,
     164                 :            :                 vfio->container_fd, vf_num);
     165                 :            : 
     166         [ #  # ]:          0 :         if (res) {
     167                 :          0 :                 NT_LOG(ERR, NTNIC, "dev_vfio_container_dma_map failed: res %d", res);
     168                 :          0 :                 return -1;
     169                 :            :         }
     170                 :            : 
     171                 :          0 :         *iova_addr = vfio->iova_addr + gp_offset;
     172                 :            : 
     173                 :          0 :         vfio->iova_addr += ONE_G_SIZE;
     174                 :            : 
     175                 :          0 :         return 0;
     176                 :            : }
     177                 :            : 
     178                 :            : int
     179                 :          0 : nthw_vfio_dma_unmap(int vf_num, void *virt_addr, uint64_t iova_addr, uint64_t size)
     180                 :            : {
     181                 :            :         uint64_t gp_virt_base;
     182                 :            :         struct vfio_dev *vfio;
     183                 :            : 
     184         [ #  # ]:          0 :         if (size == ONE_G_SIZE) {
     185                 :            :                 uint64_t gp_offset;
     186                 :          0 :                 gp_virt_base = (uint64_t)virt_addr & ~ONE_G_MASK;
     187                 :          0 :                 gp_offset = (uint64_t)virt_addr & ONE_G_MASK;
     188                 :          0 :                 iova_addr -= gp_offset;
     189                 :            : 
     190                 :            :         } else {
     191                 :          0 :                 gp_virt_base = (uint64_t)virt_addr;
     192                 :            :         }
     193                 :            : 
     194                 :            :         vfio = vfio_get(vf_num);
     195                 :            : 
     196                 :            :         if (vfio == NULL) {
     197                 :          0 :                 NT_LOG(ERR, NTNIC, "VFIO UNMAP: VF number %d invalid", vf_num);
     198                 :          0 :                 return -1;
     199                 :            :         }
     200                 :            : 
     201                 :          0 :         int res = dev_vfio_container_dma_unmap(vfio->container_fd, gp_virt_base, iova_addr, size);
     202                 :            : 
     203         [ #  # ]:          0 :         if (res != 0) {
     204                 :          0 :                 NT_LOG(ERR, NTNIC,
     205                 :            :                         "VFIO UNMMAP FAILED! res %i, container_fd %i, vf_num %i, virt_base=%" PRIX64
     206                 :            :                         ", IOVA=%" PRIX64 ", size=%" PRIX64,
     207                 :            :                         res, vfio->container_fd, vf_num, gp_virt_base, iova_addr, size);
     208                 :          0 :                 return -1;
     209                 :            :         }
     210                 :            : 
     211                 :            :         return 0;
     212                 :            : }
     213                 :            : 
     214                 :            : void
     215                 :          0 : nthw_vfio_init(void)
     216                 :            : {
     217                 :          0 :         struct nt_util_vfio_impl s = { .vfio_dma_map = nthw_vfio_dma_map,
     218                 :            :                        .vfio_dma_unmap = nthw_vfio_dma_unmap
     219                 :            :         };
     220                 :          0 :         nthw_util_vfio_init(&s);
     221                 :          0 : }

Generated by: LCOV version 1.14