LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_vfio_group.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 341 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 28 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 212 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2025 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <uapi/linux/vfio.h>
       6                 :            : 
       7                 :            : #include <dirent.h>
       8                 :            : #include <fcntl.h>
       9                 :            : #include <inttypes.h>
      10                 :            : #include <stdbool.h>
      11                 :            : #include <stdint.h>
      12                 :            : #include <stdlib.h>
      13                 :            : #include <sys/ioctl.h>
      14                 :            : #include <unistd.h>
      15                 :            : 
      16                 :            : #include <rte_log.h>
      17                 :            : #include <rte_errno.h>
      18                 :            : #include <rte_eal_memconfig.h>
      19                 :            : #include <rte_memory.h>
      20                 :            : #include <rte_string_fns.h>
      21                 :            : #include <dev_vfio.h>
      22                 :            : 
      23                 :            : #include "eal_vfio.h"
      24                 :            : #include "eal_private.h"
      25                 :            : #include "eal_internal_cfg.h"
      26                 :            : 
      27                 :            : #define DEV_VFIO_DIR "/dev/vfio"
      28                 :            : #define DEV_VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
      29                 :            : 
      30                 :            : static int vfio_type1_dma_map(struct vfio_container *);
      31                 :            : static int vfio_type1_dma_mem_map(struct vfio_container *, uint64_t, uint64_t, uint64_t, int);
      32                 :            : static int vfio_spapr_dma_map(struct vfio_container *);
      33                 :            : static int vfio_spapr_dma_mem_map(struct vfio_container *, uint64_t, uint64_t, uint64_t, int);
      34                 :            : static int vfio_noiommu_dma_map(struct vfio_container *);
      35                 :            : static int vfio_noiommu_dma_mem_map(struct vfio_container *, uint64_t, uint64_t, uint64_t, int);
      36                 :            : 
      37                 :            : /* IOMMU types we support */
      38                 :            : static const struct vfio_iommu_ops iommu_types[] = {
      39                 :            :         /* x86 IOMMU, otherwise known as type 1 */
      40                 :            :         {
      41                 :            :                 .type_id = VFIO_TYPE1_IOMMU,
      42                 :            :                 .name = "Type 1",
      43                 :            :                 .partial_unmap = false,
      44                 :            :                 .dma_map_func = &vfio_type1_dma_map,
      45                 :            :                 .dma_user_map_func = &vfio_type1_dma_mem_map
      46                 :            :         },
      47                 :            :         /* ppc64 IOMMU, otherwise known as spapr */
      48                 :            :         {
      49                 :            :                 .type_id = VFIO_SPAPR_TCE_v2_IOMMU,
      50                 :            :                 .name = "sPAPR",
      51                 :            :                 .partial_unmap = true,
      52                 :            :                 .dma_map_func = &vfio_spapr_dma_map,
      53                 :            :                 .dma_user_map_func = &vfio_spapr_dma_mem_map
      54                 :            :         },
      55                 :            :         /* IOMMU-less mode */
      56                 :            :         {
      57                 :            :                 .type_id = VFIO_NOIOMMU_IOMMU,
      58                 :            :                 .name = "No-IOMMU",
      59                 :            :                 .partial_unmap = true,
      60                 :            :                 .dma_map_func = &vfio_noiommu_dma_map,
      61                 :            :                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
      62                 :            :         },
      63                 :            : };
      64                 :            : 
      65                 :            : static const struct vfio_iommu_ops *
      66                 :          0 : vfio_group_set_iommu_type(int vfio_container_fd)
      67                 :            : {
      68         [ #  # ]:          0 :         for (unsigned int idx = 0; idx < RTE_DIM(iommu_types); idx++) {
      69                 :          0 :                 const struct vfio_iommu_ops *t = &iommu_types[idx];
      70                 :            : 
      71                 :          0 :                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU, t->type_id);
      72         [ #  # ]:          0 :                 if (ret == 0)
      73                 :          0 :                         return t;
      74                 :            :                 /* not an error, there may be more supported IOMMU types */
      75                 :          0 :                 EAL_LOG(DEBUG, "Set IOMMU type %d (%s) failed, error %i (%s)", t->type_id, t->name,
      76                 :            :                         errno, strerror(errno));
      77                 :            :         }
      78                 :            :         /* if we didn't find a suitable IOMMU type, fail */
      79                 :            :         return NULL;
      80                 :            : }
      81                 :            : 
      82                 :            : static int
      83                 :          0 : type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms, void *arg)
      84                 :            : {
      85                 :            :         struct vfio_container *cfg = arg;
      86                 :            : 
      87                 :            :         /* skip external memory that isn't a heap */
      88   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
      89                 :            :                 return 0;
      90                 :            : 
      91                 :            :         /* skip any segments with invalid IOVA addresses */
      92         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
      93                 :            :                 return 0;
      94                 :            : 
      95                 :          0 :         return vfio_type1_dma_mem_map(cfg, ms->addr_64, ms->iova, ms->len, 1);
      96                 :            : }
      97                 :            : 
      98                 :            : static int
      99                 :          0 : vfio_type1_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len,
     100                 :            :                 int do_map)
     101                 :            : {
     102                 :            :         struct vfio_iommu_type1_dma_map dma_map;
     103                 :            :         struct vfio_iommu_type1_dma_unmap dma_unmap;
     104                 :            :         int ret;
     105                 :            : 
     106         [ #  # ]:          0 :         if (do_map != 0) {
     107                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
     108                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
     109                 :          0 :                 dma_map.vaddr = vaddr;
     110                 :          0 :                 dma_map.size = len;
     111                 :          0 :                 dma_map.iova = iova;
     112                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
     113                 :            : 
     114                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
     115         [ #  # ]:          0 :                 if (ret) {
     116                 :            :                         /**
     117                 :            :                          * In case the mapping was already done EEXIST will be
     118                 :            :                          * returned from kernel.
     119                 :            :                          */
     120         [ #  # ]:          0 :                         if (errno == EEXIST) {
     121                 :          0 :                                 EAL_LOG(DEBUG, "Memory segment is already mapped, skipping");
     122                 :            :                         } else {
     123                 :          0 :                                 EAL_LOG(ERR, "Cannot set up DMA remapping, error %i (%s)", errno,
     124                 :            :                                         strerror(errno));
     125                 :          0 :                                 return -1;
     126                 :            :                         }
     127                 :            :                 }
     128                 :            :         } else {
     129                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
     130                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
     131                 :          0 :                 dma_unmap.size = len;
     132                 :          0 :                 dma_unmap.iova = iova;
     133                 :            : 
     134                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
     135         [ #  # ]:          0 :                 if (ret) {
     136                 :          0 :                         EAL_LOG(ERR, "Cannot clear DMA remapping, error %i (%s)", errno,
     137                 :            :                                 strerror(errno));
     138                 :          0 :                         return -1;
     139         [ #  # ]:          0 :                 } else if (dma_unmap.size != len) {
     140                 :          0 :                         EAL_LOG(ERR, "Unexpected size %"PRIu64
     141                 :            :                                 " of DMA remapping cleared instead of %"PRIu64,
     142                 :            :                                 (uint64_t)dma_unmap.size, len);
     143                 :          0 :                         return -1;
     144                 :            :                 }
     145                 :            :         }
     146                 :            : 
     147                 :            :         return 0;
     148                 :            : }
     149                 :            : 
     150                 :            : static int
     151                 :          0 : vfio_type1_dma_map(struct vfio_container *cfg)
     152                 :            : {
     153                 :          0 :         return rte_memseg_walk(type1_map, cfg);
     154                 :            : }
     155                 :            : 
     156                 :            : /* Track the size of the statically allocated DMA window for SPAPR */
     157                 :            : static uint64_t spapr_dma_win_len;
     158                 :            : static uint64_t spapr_dma_win_page_sz;
     159                 :            : 
     160                 :            : static int
     161                 :          0 : vfio_spapr_dma_do_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len,
     162                 :            :                 int do_map)
     163                 :            : {
     164                 :          0 :         struct vfio_iommu_spapr_register_memory reg = {
     165                 :            :                 .argsz = sizeof(reg),
     166                 :            :                 .vaddr = (uintptr_t) vaddr,
     167                 :            :                 .size = len,
     168                 :            :                 .flags = 0
     169                 :            :         };
     170                 :            :         int ret;
     171                 :            : 
     172         [ #  # ]:          0 :         if (do_map != 0) {
     173                 :            :                 struct vfio_iommu_type1_dma_map dma_map;
     174                 :            : 
     175         [ #  # ]:          0 :                 if (iova + len > spapr_dma_win_len) {
     176                 :          0 :                         EAL_LOG(ERR, "DMA map attempt outside DMA window");
     177                 :          0 :                         return -1;
     178                 :            :                 }
     179                 :            : 
     180                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
     181         [ #  # ]:          0 :                 if (ret) {
     182                 :          0 :                         EAL_LOG(ERR, "Cannot register vaddr for IOMMU, error %i (%s)", errno,
     183                 :            :                                 strerror(errno));
     184                 :          0 :                         return -1;
     185                 :            :                 }
     186                 :            : 
     187                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
     188                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
     189                 :          0 :                 dma_map.vaddr = vaddr;
     190                 :          0 :                 dma_map.size = len;
     191                 :          0 :                 dma_map.iova = iova;
     192                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
     193                 :            : 
     194                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
     195         [ #  # ]:          0 :                 if (ret) {
     196                 :          0 :                         EAL_LOG(ERR, "Cannot map vaddr for IOMMU, error %i (%s)", errno,
     197                 :            :                                 strerror(errno));
     198                 :          0 :                         return -1;
     199                 :            :                 }
     200                 :            : 
     201                 :            :         } else {
     202                 :            :                 struct vfio_iommu_type1_dma_unmap dma_unmap;
     203                 :            : 
     204                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
     205                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
     206                 :          0 :                 dma_unmap.size = len;
     207                 :          0 :                 dma_unmap.iova = iova;
     208                 :            : 
     209                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
     210         [ #  # ]:          0 :                 if (ret) {
     211                 :          0 :                         EAL_LOG(ERR, "Cannot unmap vaddr for IOMMU, error %i (%s)", errno,
     212                 :            :                                 strerror(errno));
     213                 :          0 :                         return -1;
     214                 :            :                 }
     215                 :            : 
     216                 :          0 :                 ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
     217         [ #  # ]:          0 :                 if (ret) {
     218                 :          0 :                         EAL_LOG(ERR, "Cannot unregister vaddr for IOMMU, error %i (%s)", errno,
     219                 :            :                                 strerror(errno));
     220                 :          0 :                         return -1;
     221                 :            :                 }
     222                 :            :         }
     223                 :            : 
     224                 :            :         return ret;
     225                 :            : }
     226                 :            : 
     227                 :            : static int
     228                 :          0 : vfio_spapr_map_walk(const struct rte_memseg_list *msl, const struct rte_memseg *ms, void *arg)
     229                 :            : {
     230                 :            :         struct vfio_container *cfg = arg;
     231                 :            : 
     232                 :            :         /* skip external memory that isn't a heap */
     233   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
     234                 :            :                 return 0;
     235                 :            : 
     236                 :            :         /* skip any segments with invalid IOVA addresses */
     237         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
     238                 :            :                 return 0;
     239                 :            : 
     240                 :          0 :         return vfio_spapr_dma_do_map(cfg, ms->addr_64, ms->iova, ms->len, 1);
     241                 :            : }
     242                 :            : 
     243                 :            : struct spapr_size_walk_param {
     244                 :            :         uint64_t max_va;
     245                 :            :         uint64_t page_sz;
     246                 :            :         bool is_user_managed;
     247                 :            : };
     248                 :            : 
     249                 :            : /*
     250                 :            :  * In order to set the DMA window size required for the SPAPR IOMMU
     251                 :            :  * we need to walk the existing virtual memory allocations as well as
     252                 :            :  * find the hugepage size used.
     253                 :            :  */
     254                 :            : static int
     255                 :          0 : vfio_spapr_size_walk(const struct rte_memseg_list *msl, void *arg)
     256                 :            : {
     257                 :            :         struct spapr_size_walk_param *param = arg;
     258                 :          0 :         uint64_t max = (uint64_t) msl->base_va + (uint64_t) msl->len;
     259                 :            : 
     260   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap) {
     261                 :            :                 /* ignore user managed external memory */
     262                 :          0 :                 param->is_user_managed = true;
     263                 :          0 :                 return 0;
     264                 :            :         }
     265                 :            : 
     266         [ #  # ]:          0 :         if (max > param->max_va) {
     267                 :          0 :                 param->page_sz = msl->page_sz;
     268                 :          0 :                 param->max_va = max;
     269                 :            :         }
     270                 :            : 
     271                 :            :         return 0;
     272                 :            : }
     273                 :            : 
     274                 :            : /*
     275                 :            :  * Find the highest memory address used in physical or virtual address
     276                 :            :  * space and use that as the top of the DMA window.
     277                 :            :  */
     278                 :            : static int
     279                 :          0 : find_highest_mem_addr(struct spapr_size_walk_param *param)
     280                 :            : {
     281                 :            :         /* find the maximum IOVA address for setting the DMA window size */
     282         [ #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_PA) {
     283                 :            :                 static const char proc_iomem[] = "/proc/iomem";
     284                 :            :                 static const char str_sysram[] = "System RAM";
     285                 :            :                 uint64_t start, end, max = 0;
     286                 :          0 :                 char *line = NULL;
     287                 :            :                 char *dash, *space;
     288                 :            :                 size_t line_len;
     289                 :            : 
     290                 :            :                 /*
     291                 :            :                  * Example "System RAM" in /proc/iomem:
     292                 :            :                  * 00000000-1fffffffff : System RAM
     293                 :            :                  * 200000000000-201fffffffff : System RAM
     294                 :            :                  */
     295                 :          0 :                 FILE *fd = fopen(proc_iomem, "r");
     296         [ #  # ]:          0 :                 if (fd == NULL) {
     297                 :          0 :                         EAL_LOG(ERR, "Cannot open %s", proc_iomem);
     298                 :          0 :                         return -1;
     299                 :            :                 }
     300                 :            :                 /* Scan /proc/iomem for the highest PA in the system */
     301         [ #  # ]:          0 :                 while (getline(&line, &line_len, fd) != -1) {
     302         [ #  # ]:          0 :                         if (strstr(line, str_sysram) == NULL)
     303                 :          0 :                                 continue;
     304                 :            : 
     305                 :          0 :                         space = strstr(line, " ");
     306                 :          0 :                         dash = strstr(line, "-");
     307                 :            : 
     308                 :            :                         /* Validate the format of the memory string */
     309   [ #  #  #  # ]:          0 :                         if (space == NULL || dash == NULL || space < dash) {
     310                 :          0 :                                 EAL_LOG(ERR, "Can't parse line \"%s\" in file %s", line,
     311                 :            :                                         proc_iomem);
     312                 :          0 :                                 continue;
     313                 :            :                         }
     314                 :            : 
     315                 :          0 :                         start = strtoull(line, NULL, 16);
     316                 :          0 :                         end   = strtoull(dash + 1, NULL, 16);
     317                 :          0 :                         EAL_LOG(DEBUG, "Found system RAM from 0x%" PRIx64 " to 0x%" PRIx64,
     318                 :            :                                 start, end);
     319                 :            :                         if (end > max)
     320                 :            :                                 max = end;
     321                 :            :                 }
     322                 :          0 :                 free(line);
     323                 :          0 :                 fclose(fd);
     324                 :            : 
     325         [ #  # ]:          0 :                 if (max == 0) {
     326                 :          0 :                         EAL_LOG(ERR, "Failed to find valid \"System RAM\" entry in file %s",
     327                 :            :                                 proc_iomem);
     328                 :          0 :                         return -1;
     329                 :            :                 }
     330                 :            : 
     331                 :          0 :                 spapr_dma_win_len = rte_align64pow2(max + 1);
     332                 :          0 :                 return 0;
     333         [ #  # ]:          0 :         } else if (rte_eal_iova_mode() == RTE_IOVA_VA) {
     334                 :          0 :                 EAL_LOG(DEBUG, "Highest VA address in memseg list is 0x%" PRIx64, param->max_va);
     335                 :          0 :                 spapr_dma_win_len = rte_align64pow2(param->max_va);
     336                 :          0 :                 return 0;
     337                 :            :         }
     338                 :            : 
     339                 :          0 :         spapr_dma_win_len = 0;
     340                 :          0 :         EAL_LOG(ERR, "Unsupported IOVA mode");
     341                 :          0 :         return -1;
     342                 :            : }
     343                 :            : 
     344                 :            : 
     345                 :            : /*
     346                 :            :  * The SPAPRv2 IOMMU supports 2 DMA windows with starting
     347                 :            :  * address at 0 or 1<<59.  By default, a DMA window is set
     348                 :            :  * at address 0, 2GB long, with a 4KB page.  For DPDK we
     349                 :            :  * must remove the default window and setup a new DMA window
     350                 :            :  * based on the hugepage size and memory requirements of
     351                 :            :  * the application before we can map memory for DMA.
     352                 :            :  */
     353                 :            : static int
     354                 :          0 : spapr_dma_win_size(void)
     355                 :            : {
     356                 :            :         struct spapr_size_walk_param param;
     357                 :            : 
     358                 :            :         /* only create DMA window once */
     359         [ #  # ]:          0 :         if (spapr_dma_win_len > 0)
     360                 :            :                 return 0;
     361                 :            : 
     362                 :            :         /* walk the memseg list to find the page size/max VA address */
     363                 :            :         memset(&param, 0, sizeof(param));
     364         [ #  # ]:          0 :         if (rte_memseg_list_walk(vfio_spapr_size_walk, &param) < 0) {
     365                 :          0 :                 EAL_LOG(ERR, "Failed to walk memseg list for DMA window size");
     366                 :          0 :                 return -1;
     367                 :            :         }
     368                 :            : 
     369                 :            :         /* we can't be sure if DMA window covers external memory */
     370         [ #  # ]:          0 :         if (param.is_user_managed)
     371                 :          0 :                 EAL_LOG(WARNING, "Detected user managed external memory which may not be managed by the IOMMU");
     372                 :            : 
     373                 :            :         /* check physical/virtual memory size */
     374         [ #  # ]:          0 :         if (find_highest_mem_addr(&param) < 0)
     375                 :            :                 return -1;
     376                 :          0 :         EAL_LOG(DEBUG, "Setting DMA window size to 0x%" PRIx64, spapr_dma_win_len);
     377                 :          0 :         spapr_dma_win_page_sz = param.page_sz;
     378                 :          0 :         rte_mem_set_dma_mask(rte_ctz64(spapr_dma_win_len));
     379                 :          0 :         return 0;
     380                 :            : }
     381                 :            : 
     382                 :            : static int
     383                 :          0 : vfio_spapr_create_dma_window(struct vfio_container *cfg)
     384                 :            : {
     385                 :          0 :         struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create), };
     386                 :          0 :         struct vfio_iommu_spapr_tce_remove remove = { .argsz = sizeof(remove), };
     387                 :          0 :         struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info), };
     388                 :            :         int ret;
     389                 :            : 
     390                 :          0 :         ret = spapr_dma_win_size();
     391         [ #  # ]:          0 :         if (ret < 0)
     392                 :            :                 return ret;
     393                 :            : 
     394                 :          0 :         ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
     395         [ #  # ]:          0 :         if (ret) {
     396                 :          0 :                 EAL_LOG(ERR, "Cannot get IOMMU info, error %i (%s)", errno, strerror(errno));
     397                 :          0 :                 return -1;
     398                 :            :         }
     399                 :            : 
     400                 :            :         /*
     401                 :            :          * sPAPR v1/v2 IOMMU always has a default 1G DMA window set.  The window
     402                 :            :          * can't be changed for v1 but it can be changed for v2. Since DPDK only
     403                 :            :          * supports v2, remove the default DMA window so it can be resized.
     404                 :            :          */
     405                 :          0 :         remove.start_addr = info.dma32_window_start;
     406                 :          0 :         ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
     407         [ #  # ]:          0 :         if (ret)
     408                 :            :                 return -1;
     409                 :            : 
     410                 :            :         /* create a new DMA window (start address is not selectable) */
     411                 :          0 :         create.window_size = spapr_dma_win_len;
     412                 :          0 :         create.page_shift  = rte_ctz64(spapr_dma_win_page_sz);
     413                 :          0 :         create.levels = 1;
     414                 :          0 :         ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
     415                 :            :         /*
     416                 :            :          * The vfio_iommu_spapr_tce_info structure was modified in
     417                 :            :          * Linux kernel 4.2.0 to add support for the
     418                 :            :          * vfio_iommu_spapr_tce_ddw_info structure needed to try
     419                 :            :          * multiple table levels.  Skip the attempt if running with
     420                 :            :          * an older kernel.
     421                 :            :          */
     422         [ #  # ]:          0 :         if (ret) {
     423                 :            :                 /* if at first we don't succeed, try more levels */
     424                 :            :                 uint32_t levels;
     425                 :            : 
     426                 :          0 :                 for (levels = create.levels + 1;
     427   [ #  #  #  # ]:          0 :                         ret && levels <= info.ddw.levels; levels++) {
     428                 :          0 :                         create.levels = levels;
     429                 :          0 :                         ret = ioctl(cfg->container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
     430                 :            :                 }
     431                 :            :         }
     432         [ #  # ]:          0 :         if (ret) {
     433                 :          0 :                 EAL_LOG(ERR, "Cannot create new DMA window, error %i (%s)", errno,
     434                 :            :                         strerror(errno));
     435                 :          0 :                 EAL_LOG(ERR, "Consider using a larger hugepage size if supported by the system");
     436                 :          0 :                 return -1;
     437                 :            :         }
     438                 :            : 
     439                 :            :         /* verify the start address  */
     440         [ #  # ]:          0 :         if (create.start_addr != 0) {
     441                 :          0 :                 EAL_LOG(ERR, "Received unsupported start address 0x%" PRIx64,
     442                 :            :                         (uint64_t)create.start_addr);
     443                 :          0 :                 return -1;
     444                 :            :         }
     445                 :            :         return ret;
     446                 :            : }
     447                 :            : 
     448                 :            : static int
     449                 :          0 : vfio_spapr_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len,
     450                 :            :                 int do_map)
     451                 :            : {
     452                 :            :         int ret = 0;
     453                 :            : 
     454         [ #  # ]:          0 :         if (do_map) {
     455         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(cfg, vaddr, iova, len, 1)) {
     456                 :          0 :                         EAL_LOG(ERR, "Failed to map DMA");
     457                 :            :                         ret = -1;
     458                 :            :                 }
     459                 :            :         } else {
     460         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(cfg, vaddr, iova, len, 0)) {
     461                 :          0 :                         EAL_LOG(ERR, "Failed to unmap DMA");
     462                 :            :                         ret = -1;
     463                 :            :                 }
     464                 :            :         }
     465                 :            : 
     466                 :          0 :         return ret;
     467                 :            : }
     468                 :            : 
     469                 :            : static int
     470                 :          0 : vfio_spapr_dma_map(struct vfio_container *cfg)
     471                 :            : {
     472         [ #  # ]:          0 :         if (vfio_spapr_create_dma_window(cfg) < 0) {
     473                 :          0 :                 EAL_LOG(ERR, "Could not create new DMA window!");
     474                 :          0 :                 return -1;
     475                 :            :         }
     476                 :            : 
     477                 :            :         /* map all existing DPDK segments for DMA */
     478         [ #  # ]:          0 :         if (rte_memseg_walk(vfio_spapr_map_walk, cfg) < 0)
     479                 :          0 :                 return -1;
     480                 :            : 
     481                 :            :         return 0;
     482                 :            : }
     483                 :            : 
     484                 :            : static int
     485                 :          0 : vfio_noiommu_dma_map(struct vfio_container *cfg __rte_unused)
     486                 :            : {
     487                 :            :         /* No-IOMMU mode does not need DMA mapping */
     488                 :          0 :         return 0;
     489                 :            : }
     490                 :            : 
     491                 :            : static int
     492                 :          0 : vfio_noiommu_dma_mem_map(struct vfio_container *cfg __rte_unused, uint64_t vaddr __rte_unused,
     493                 :            :                 uint64_t iova __rte_unused, uint64_t len __rte_unused, int do_map __rte_unused)
     494                 :            : {
     495                 :            :         /* No-IOMMU mode does not need DMA mapping */
     496                 :          0 :         return 0;
     497                 :            : }
     498                 :            : 
     499                 :            : struct vfio_group *
     500                 :          0 : vfio_group_create(struct vfio_container *cfg, int iommu_group)
     501                 :            : {
     502                 :            :         struct vfio_group *grp;
     503                 :            : 
     504         [ #  # ]:          0 :         if (cfg->group_cfg.n_groups >= RTE_DIM(cfg->group_cfg.groups)) {
     505                 :          0 :                 EAL_LOG(ERR, "Cannot add more VFIO groups to container");
     506                 :          0 :                 return NULL;
     507                 :            :         }
     508         [ #  # ]:          0 :         VFIO_GROUP_FOREACH(cfg, grp) {
     509         [ #  # ]:          0 :                 if (grp->active)
     510                 :            :                         continue;
     511                 :          0 :                 cfg->group_cfg.n_groups++;
     512                 :          0 :                 grp->active = true;
     513                 :          0 :                 grp->group_num = iommu_group;
     514                 :          0 :                 grp->fd = -1;
     515                 :          0 :                 return grp;
     516                 :            :         }
     517                 :            :         /* should not happen */
     518                 :            :         return NULL;
     519                 :            : }
     520                 :            : 
     521                 :            : void
     522                 :          0 : vfio_group_erase(struct vfio_container *cfg, struct vfio_group *grp)
     523                 :            : {
     524                 :            :         struct vfio_group_config *group_cfg = &cfg->group_cfg;
     525                 :            : 
     526   [ #  #  #  # ]:          0 :         if (grp->fd >= 0 && close(grp->fd) < 0)
     527                 :          0 :                 EAL_LOG(ERR, "Error when closing group fd %d", grp->fd);
     528                 :            : 
     529                 :          0 :         *grp = (struct vfio_group){0};
     530                 :          0 :         group_cfg->n_groups--;
     531                 :            : 
     532                 :            :         /* if this was the last group in config, erase IOMMU setup and unregister callback */
     533         [ #  # ]:          0 :         if (group_cfg->n_groups == 0) {
     534                 :          0 :                 cfg->dma_setup_done = false;
     535                 :          0 :                 group_cfg->iommu_type_set = false;
     536                 :            :         }
     537                 :          0 : }
     538                 :            : 
     539                 :            : struct vfio_group *
     540                 :          0 : vfio_group_get_by_num(struct vfio_container *cfg, int iommu_group)
     541                 :            : {
     542                 :            :         struct vfio_group *grp;
     543                 :            : 
     544   [ #  #  #  # ]:          0 :         VFIO_GROUP_FOREACH_ACTIVE(cfg, grp) {
     545         [ #  # ]:          0 :                 if (grp->group_num == iommu_group)
     546                 :          0 :                         return grp;
     547                 :            :         }
     548                 :            :         return NULL;
     549                 :            : }
     550                 :            : 
     551                 :            : static int
     552                 :          0 : vfio_open_group_sysfs(int iommu_group_num)
     553                 :            : {
     554                 :            :         char filename[PATH_MAX];
     555                 :            :         int fd;
     556                 :            : 
     557                 :            :         /*
     558                 :            :          * Global no-IOMMU mode does not mean every group is no-IOMMU: the
     559                 :            :          * kernel only creates no-IOMMU groups for devices without an IOMMU
     560                 :            :          * group, so try the regular group first, then the no-IOMMU one.
     561                 :            :          */
     562                 :            :         snprintf(filename, sizeof(filename), DEV_VFIO_GROUP_FMT, iommu_group_num);
     563                 :            :         fd = open(filename, O_RDWR);
     564   [ #  #  #  # ]:          0 :         if (fd < 0 && errno == ENOENT) {
     565                 :            :                 snprintf(filename, sizeof(filename), DEV_VFIO_NOIOMMU_GROUP_FMT, iommu_group_num);
     566                 :            :                 fd = open(filename, O_RDWR);
     567                 :            :         }
     568                 :            : 
     569                 :            :         /* we have to differentiate between failed open and non-existence */
     570   [ #  #  #  # ]:          0 :         if (fd < 0 && errno == ENOENT)
     571                 :          0 :                 return -ENOENT;
     572                 :            :         return fd;
     573                 :            : }
     574                 :            : 
     575                 :            : static int
     576                 :          0 : vfio_group_request_fd(int iommu_group_num)
     577                 :            : {
     578                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     579                 :          0 :         struct rte_mp_reply mp_reply = {0};
     580                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     581                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     582                 :            :         int vfio_group_fd = -1;
     583                 :            : 
     584                 :          0 :         p->req = VFIO_SOCKET_REQ_GROUP;
     585                 :          0 :         p->group_num = iommu_group_num;
     586                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
     587                 :          0 :         mp_req.len_param = sizeof(*p);
     588                 :          0 :         mp_req.num_fds = 0;
     589                 :            : 
     590   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
     591                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     592                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     593   [ #  #  #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK && mp_rep->num_fds == 1) {
     594                 :          0 :                         vfio_group_fd = mp_rep->fds[0];
     595         [ #  # ]:          0 :                 } else if (p->result == VFIO_SOCKET_NO_FD) {
     596                 :          0 :                         EAL_LOG(ERR, "Bad VFIO group fd");
     597                 :            :                         vfio_group_fd = -ENOENT;
     598                 :            :                 }
     599                 :            :         }
     600                 :            : 
     601                 :          0 :         free(mp_reply.msgs);
     602                 :          0 :         return vfio_group_fd;
     603                 :            : }
     604                 :            : 
     605                 :            : int
     606                 :          0 : vfio_group_open_fd(struct vfio_container *cfg, struct vfio_group *grp)
     607                 :            : {
     608                 :            :         int vfio_group_fd;
     609                 :            : 
     610                 :            :         /* we make multiprocess request only in secondary processes for default config */
     611   [ #  #  #  # ]:          0 :         if ((rte_eal_process_type() != RTE_PROC_PRIMARY) && (vfio_container_is_default(cfg)))
     612                 :          0 :                 vfio_group_fd = vfio_group_request_fd(grp->group_num);
     613                 :            :         else
     614                 :          0 :                 vfio_group_fd = vfio_open_group_sysfs(grp->group_num);
     615                 :            : 
     616                 :            :         /* pass the non-existence up the chain */
     617         [ #  # ]:          0 :         if (vfio_group_fd == -ENOENT)
     618                 :            :                 return vfio_group_fd;
     619         [ #  # ]:          0 :         else if (vfio_group_fd < 0) {
     620                 :          0 :                 EAL_LOG(ERR, "Failed to open VFIO group %d", grp->group_num);
     621                 :          0 :                 return vfio_group_fd;
     622                 :            :         }
     623                 :          0 :         grp->fd = vfio_group_fd;
     624                 :          0 :         return 0;
     625                 :            : }
     626                 :            : 
     627                 :            : static const struct vfio_iommu_ops *
     628                 :          0 : vfio_group_sync_iommu_ops(void)
     629                 :            : {
     630                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     631                 :          0 :         struct rte_mp_reply mp_reply = {0};
     632                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     633                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     634                 :            :         int iommu_type_id;
     635                 :            :         unsigned int i;
     636                 :            : 
     637                 :            :         /* find default container's IOMMU type */
     638                 :          0 :         p->req = VFIO_SOCKET_REQ_IOMMU_TYPE;
     639                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
     640                 :          0 :         mp_req.len_param = sizeof(*p);
     641                 :          0 :         mp_req.num_fds = 0;
     642                 :            : 
     643                 :            :         iommu_type_id = -1;
     644   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
     645                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     646                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     647         [ #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK)
     648                 :          0 :                         iommu_type_id = p->iommu_type_id;
     649                 :            :         }
     650                 :          0 :         free(mp_reply.msgs);
     651         [ #  # ]:          0 :         if (iommu_type_id < 0) {
     652                 :          0 :                 EAL_LOG(ERR, "Could not get IOMMU type from primary process");
     653                 :          0 :                 return NULL;
     654                 :            :         }
     655                 :            : 
     656                 :            :         /* we now have an fd for default container, as well as its IOMMU type.
     657                 :            :          * now, set up default VFIO container config to match.
     658                 :            :          */
     659         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(iommu_types); i++) {
     660                 :          0 :                 const struct vfio_iommu_ops *t = &iommu_types[i];
     661         [ #  # ]:          0 :                 if (t->type_id != iommu_type_id)
     662                 :            :                         continue;
     663                 :            : 
     664                 :            :                 return t;
     665                 :            :         }
     666                 :          0 :         EAL_LOG(ERR, "Could not find IOMMU type id (%i)", iommu_type_id);
     667                 :          0 :         return NULL;
     668                 :            : }
     669                 :            : 
     670                 :            : static int
     671                 :          0 : vfio_has_supported_extensions(int vfio_container_fd)
     672                 :            : {
     673                 :            :         unsigned int n_extensions = 0;
     674                 :            :         int ret;
     675                 :            : 
     676         [ #  # ]:          0 :         for (unsigned int idx = 0; idx < RTE_DIM(iommu_types); idx++) {
     677                 :            :                 const struct vfio_iommu_ops *t = &iommu_types[idx];
     678                 :            : 
     679                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, t->type_id);
     680         [ #  # ]:          0 :                 if (ret < 0) {
     681                 :          0 :                         EAL_LOG(ERR, "Could not get IOMMU type, error %i (%s)", errno,
     682                 :            :                                 strerror(errno));
     683                 :          0 :                         return -1;
     684         [ #  # ]:          0 :                 } else if (ret == 1) {
     685                 :            :                         /* we found a supported extension */
     686                 :          0 :                         n_extensions++;
     687                 :            :                 }
     688         [ #  # ]:          0 :                 EAL_LOG(DEBUG, "IOMMU type %d (%s) is %s", t->type_id, t->name,
     689                 :            :                         ret ? "supported" : "not supported");
     690                 :            :         }
     691                 :            : 
     692                 :            :         /* if we didn't find any supported IOMMU types, fail */
     693         [ #  # ]:          0 :         if (n_extensions == 0)
     694                 :          0 :                 return -1;
     695                 :            : 
     696                 :            :         return 0;
     697                 :            : }
     698                 :            : 
     699                 :            : int
     700                 :          0 : vfio_group_open_container_fd(void)
     701                 :            : {
     702                 :            :         int ret, vfio_container_fd;
     703                 :            : 
     704                 :            :         vfio_container_fd = open(DEV_VFIO_CONTAINER_PATH, O_RDWR);
     705         [ #  # ]:          0 :         if (vfio_container_fd < 0) {
     706                 :          0 :                 EAL_LOG(DEBUG, "Cannot open VFIO container %s, error %i (%s)",
     707                 :            :                         DEV_VFIO_CONTAINER_PATH, errno, strerror(errno));
     708                 :          0 :                 return -1;
     709                 :            :         }
     710                 :            : 
     711                 :            :         /* check VFIO API version */
     712                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
     713         [ #  # ]:          0 :         if (ret != VFIO_API_VERSION) {
     714         [ #  # ]:          0 :                 if (ret < 0)
     715                 :          0 :                         EAL_LOG(DEBUG, "Could not get VFIO API version, error %i (%s)", errno,
     716                 :            :                                 strerror(errno));
     717                 :            :                 else
     718                 :          0 :                         EAL_LOG(DEBUG, "Unsupported VFIO API version!");
     719                 :          0 :                 close(vfio_container_fd);
     720                 :          0 :                 return -1;
     721                 :            :         }
     722                 :            : 
     723                 :          0 :         ret = vfio_has_supported_extensions(vfio_container_fd);
     724         [ #  # ]:          0 :         if (ret) {
     725                 :          0 :                 EAL_LOG(DEBUG, "No supported IOMMU extensions found!");
     726                 :          0 :                 close(vfio_container_fd);
     727                 :          0 :                 return -1;
     728                 :            :         }
     729                 :            : 
     730                 :            :         return vfio_container_fd;
     731                 :            : }
     732                 :            : 
     733                 :            : int
     734                 :          0 : vfio_group_enable(struct vfio_container *cfg)
     735                 :            : {
     736                 :            :         int container_fd;
     737                 :            :         DIR *dir;
     738                 :            : 
     739                 :            :         /* VFIO directory might not exist (e.g., unprivileged containers) */
     740                 :          0 :         dir = opendir(DEV_VFIO_DIR);
     741         [ #  # ]:          0 :         if (dir == NULL) {
     742                 :          0 :                 EAL_LOG(DEBUG, "VFIO directory does not exist, skipping VFIO group support...");
     743                 :          0 :                 return 1;
     744                 :            :         }
     745                 :          0 :         closedir(dir);
     746                 :            : 
     747                 :            :         /* open a default container */
     748                 :          0 :         container_fd = vfio_group_open_container_fd();
     749         [ #  # ]:          0 :         if (container_fd < 0)
     750                 :            :                 return -1;
     751                 :            : 
     752                 :          0 :         cfg->container_fd = container_fd;
     753                 :          0 :         return 0;
     754                 :            : }
     755                 :            : 
     756                 :            : int
     757                 :          0 : vfio_group_prepare(struct vfio_container *cfg, struct vfio_group *grp)
     758                 :            : {
     759                 :          0 :         struct vfio_group_status group_status = {
     760                 :            :                 .argsz = sizeof(group_status)};
     761                 :            :         int ret;
     762                 :            : 
     763                 :            :         /*
     764                 :            :          * We need to assign group to a container and check if it is viable, but there are cases
     765                 :            :          * where we don't need to do that.
     766                 :            :          *
     767                 :            :          * For default container, we need to set up the group only in primary process, as secondary
     768                 :            :          * process would have requested group fd over IPC, which implies it would have already been
     769                 :            :          * set up by the primary.
     770                 :            :          *
     771                 :            :          * For custom containers, every process sets up its own groups.
     772                 :            :          */
     773   [ #  #  #  # ]:          0 :         if (vfio_container_is_default(cfg) && rte_eal_process_type() != RTE_PROC_PRIMARY) {
     774                 :          0 :                 EAL_LOG(DEBUG, "Skipping setup for VFIO group %d", grp->group_num);
     775                 :          0 :                 return 0;
     776                 :            :         }
     777                 :            : 
     778                 :            :         /* check if the group is viable */
     779                 :          0 :         ret = ioctl(grp->fd, VFIO_GROUP_GET_STATUS, &group_status);
     780         [ #  # ]:          0 :         if (ret) {
     781                 :          0 :                 EAL_LOG(ERR, "Cannot get VFIO group status for group %d, error %i (%s)",
     782                 :            :                         grp->group_num, errno, strerror(errno));
     783                 :          0 :                 return -1;
     784                 :            :         }
     785                 :            : 
     786         [ #  # ]:          0 :         if ((group_status.flags & VFIO_GROUP_FLAGS_VIABLE) == 0) {
     787                 :          0 :                 EAL_LOG(ERR, "VFIO group %d is not viable! "
     788                 :            :                         "Not all devices in IOMMU group bound to VFIO or unbound",
     789                 :            :                         grp->group_num);
     790                 :          0 :                 return -1;
     791                 :            :         }
     792                 :            : 
     793                 :            :         /* set container for group if necessary */
     794         [ #  # ]:          0 :         if ((group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET) == 0) {
     795                 :            :                 /* add group to a container */
     796                 :          0 :                 ret = ioctl(grp->fd, VFIO_GROUP_SET_CONTAINER, &cfg->container_fd);
     797         [ #  # ]:          0 :                 if (ret) {
     798                 :          0 :                         EAL_LOG(ERR, "Cannot add VFIO group %d to container, error %i (%s)",
     799                 :            :                                 grp->group_num, errno, strerror(errno));
     800                 :          0 :                         return -1;
     801                 :            :                 }
     802                 :            :         } else {
     803                 :            :                 /* group is already added to a container - this should not happen */
     804                 :          0 :                 EAL_LOG(ERR, "VFIO group %d is already assigned to a container", grp->group_num);
     805                 :          0 :                 return -1;
     806                 :            :         }
     807                 :            :         return 0;
     808                 :            : }
     809                 :            : 
     810                 :            : int
     811                 :          0 : vfio_group_setup_iommu(struct vfio_container *cfg)
     812                 :            : {
     813                 :            :         const struct vfio_iommu_ops *ops;
     814                 :            : 
     815                 :            :         /*
     816                 :            :          * Setting IOMMU type is a per-container operation (via ioctl on container fd), but the ops
     817                 :            :          * structure is global and shared across all containers.
     818                 :            :          *
     819                 :            :          * For secondary processes with default container, we sync ops from primary. For all other
     820                 :            :          * cases (primary, or secondary with custom containers), we set IOMMU type on the container
     821                 :            :          * which also discovers the ops.
     822                 :            :          */
     823   [ #  #  #  # ]:          0 :         if (vfio_container_is_default(cfg) && rte_eal_process_type() != RTE_PROC_PRIMARY) {
     824                 :            :                 /* Secondary process: sync ops from primary for default container */
     825                 :          0 :                 ops = vfio_group_sync_iommu_ops();
     826         [ #  # ]:          0 :                 if (ops == NULL)
     827                 :            :                         return -1;
     828                 :            :         } else {
     829                 :            :                 /* Primary process OR custom container: set IOMMU type on container */
     830                 :          0 :                 ops = vfio_group_set_iommu_type(cfg->container_fd);
     831         [ #  # ]:          0 :                 if (ops == NULL)
     832                 :            :                         return -1;
     833                 :            :         }
     834                 :            : 
     835                 :            :         /* Set or verify global ops */
     836         [ #  # ]:          0 :         if (vfio_global_cfg.ops == NULL) {
     837                 :          0 :                 vfio_global_cfg.ops = ops;
     838                 :          0 :                 EAL_LOG(INFO, "IOMMU type set to %d (%s)", ops->type_id, ops->name);
     839         [ #  # ]:          0 :         } else if (vfio_global_cfg.ops != ops) {
     840                 :            :                 /* This shouldn't happen on the same machine, but log it */
     841                 :          0 :                 EAL_LOG(WARNING, "Container has different IOMMU type (%d - %s) "
     842                 :            :                         "than previously set (%d - %s)",
     843                 :            :                         ops->type_id, ops->name, vfio_global_cfg.ops->type_id,
     844                 :            :                         vfio_global_cfg.ops->name);
     845                 :            :         }
     846                 :            : 
     847                 :            :         return 0;
     848                 :            : }
     849                 :            : 
     850                 :            : int
     851                 :          0 : vfio_group_setup_device_fd(const char *dev_addr, struct vfio_group *grp, struct vfio_device *dev)
     852                 :            : {
     853                 :            :         rte_uuid_t vf_token;
     854                 :            :         int fd;
     855                 :            : 
     856                 :          0 :         rte_eal_vfio_get_vf_token(vf_token);
     857                 :            : 
     858         [ #  # ]:          0 :         if (!rte_uuid_is_null(vf_token)) {
     859                 :            :                 char vf_token_str[RTE_UUID_STRLEN];
     860                 :            :                 char devaddr[PATH_MAX];
     861                 :            : 
     862                 :          0 :                 rte_uuid_unparse(vf_token, vf_token_str, sizeof(vf_token_str));
     863                 :            :                 snprintf(devaddr, sizeof(devaddr), "%s vf_token=%s", dev_addr, vf_token_str);
     864                 :            : 
     865                 :          0 :                 fd = ioctl(grp->fd, VFIO_GROUP_GET_DEVICE_FD, devaddr);
     866         [ #  # ]:          0 :                 if (fd >= 0)
     867                 :          0 :                         goto out;
     868                 :            :         }
     869                 :            :         /* get a file descriptor for the device */
     870                 :          0 :         fd = ioctl(grp->fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
     871         [ #  # ]:          0 :         if (fd < 0) {
     872                 :            :                 /*
     873                 :            :                  * if we cannot get a device fd, this implies a problem with the VFIO group or the
     874                 :            :                  * container not having IOMMU configured.
     875                 :            :                  */
     876                 :          0 :                 EAL_LOG(WARNING, "Getting a vfio_dev_fd for %s failed", dev_addr);
     877                 :          0 :                 return -1;
     878                 :            :         }
     879                 :          0 : out:
     880                 :          0 :         dev->fd = fd;
     881                 :            :         /* store backreference to group */
     882                 :          0 :         dev->group = grp->group_num;
     883                 :            :         /* increment number of devices in group */
     884                 :          0 :         grp->n_devices++;
     885                 :          0 :         return 0;
     886                 :            : }
     887                 :            : 
     888                 :            : int
     889                 :          0 : vfio_group_get_num(const char *sysfs_base, const char *dev_addr, int *iommu_group_num)
     890                 :            : {
     891                 :            :         char linkname[PATH_MAX];
     892                 :            :         char filename[PATH_MAX];
     893                 :            :         char *tok[16], *group_tok, *end;
     894                 :            :         int ret, group_num;
     895                 :            : 
     896                 :            :         memset(linkname, 0, sizeof(linkname));
     897                 :            :         memset(filename, 0, sizeof(filename));
     898                 :            : 
     899                 :            :         /* try to find out IOMMU group for this device */
     900                 :            :         snprintf(linkname, sizeof(linkname), "%s/%s/iommu_group", sysfs_base, dev_addr);
     901                 :            : 
     902                 :          0 :         ret = readlink(linkname, filename, sizeof(filename));
     903                 :            : 
     904                 :            :         /* if the link doesn't exist, no VFIO for us */
     905         [ #  # ]:          0 :         if (ret < 0)
     906                 :            :                 return 0;
     907                 :            : 
     908                 :          0 :         ret = rte_strsplit(filename, sizeof(filename), tok, RTE_DIM(tok), '/');
     909         [ #  # ]:          0 :         if (ret <= 0) {
     910                 :          0 :                 EAL_LOG(ERR, "%s cannot get IOMMU group", dev_addr);
     911                 :          0 :                 return -1;
     912                 :            :         }
     913                 :            : 
     914                 :            :         /* IOMMU group is always the last token */
     915                 :          0 :         errno = 0;
     916                 :          0 :         group_tok = tok[ret - 1];
     917                 :          0 :         end = group_tok;
     918                 :          0 :         group_num = strtol(group_tok, &end, 10);
     919   [ #  #  #  #  :          0 :         if (end == group_tok || *end != '\0' || errno != 0) {
                   #  # ]
     920                 :          0 :                 EAL_LOG(ERR, "%s error parsing IOMMU number!", dev_addr);
     921                 :          0 :                 return -1;
     922                 :            :         }
     923                 :          0 :         *iommu_group_num = group_num;
     924                 :            : 
     925                 :          0 :         return 1;
     926                 :            : }

Generated by: LCOV version 1.14