LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_vfio.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 64 813 7.9 %
Date: 2025-05-01 17:49:45 Functions: 4 51 7.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 626 5.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <inttypes.h>
       6                 :            : #include <string.h>
       7                 :            : #include <fcntl.h>
       8                 :            : #include <unistd.h>
       9                 :            : #include <sys/ioctl.h>
      10                 :            : 
      11                 :            : #include <rte_errno.h>
      12                 :            : #include <rte_log.h>
      13                 :            : #include <rte_memory.h>
      14                 :            : #include <rte_eal_memconfig.h>
      15                 :            : #include <rte_vfio.h>
      16                 :            : 
      17                 :            : #include <eal_export.h>
      18                 :            : #include "eal_filesystem.h"
      19                 :            : #include "eal_memcfg.h"
      20                 :            : #include "eal_vfio.h"
      21                 :            : #include "eal_private.h"
      22                 :            : #include "eal_internal_cfg.h"
      23                 :            : 
      24                 :            : #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
      25                 :            : 
      26                 :            : /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
      27                 :            :  * recreate the mappings for DPDK segments, but we cannot do so for memory that
      28                 :            :  * was registered by the user themselves, so we need to store the user mappings
      29                 :            :  * somewhere, to recreate them later.
      30                 :            :  */
      31                 :            : #define VFIO_MAX_USER_MEM_MAPS 256
      32                 :            : struct user_mem_map {
      33                 :            :         uint64_t addr;  /**< start VA */
      34                 :            :         uint64_t iova;  /**< start IOVA */
      35                 :            :         uint64_t len;   /**< total length of the mapping */
      36                 :            :         uint64_t chunk; /**< this mapping can be split in chunks of this size */
      37                 :            : };
      38                 :            : 
      39                 :            : struct user_mem_maps {
      40                 :            :         rte_spinlock_recursive_t lock;
      41                 :            :         int n_maps;
      42                 :            :         struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
      43                 :            : };
      44                 :            : 
      45                 :            : struct vfio_config {
      46                 :            :         int vfio_enabled;
      47                 :            :         int vfio_container_fd;
      48                 :            :         int vfio_active_groups;
      49                 :            :         const struct vfio_iommu_type *vfio_iommu_type;
      50                 :            :         struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
      51                 :            :         struct user_mem_maps mem_maps;
      52                 :            : };
      53                 :            : 
      54                 :            : /* per-process VFIO config */
      55                 :            : static struct vfio_config vfio_cfgs[VFIO_MAX_CONTAINERS];
      56                 :            : static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
      57                 :            : 
      58                 :            : static int vfio_type1_dma_map(int);
      59                 :            : static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      60                 :            : static int vfio_spapr_dma_map(int);
      61                 :            : static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      62                 :            : static int vfio_noiommu_dma_map(int);
      63                 :            : static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
      64                 :            : static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
      65                 :            :                 uint64_t iova, uint64_t len, int do_map);
      66                 :            : 
      67                 :            : /* IOMMU types we support */
      68                 :            : static const struct vfio_iommu_type iommu_types[] = {
      69                 :            :         /* x86 IOMMU, otherwise known as type 1 */
      70                 :            :         {
      71                 :            :                 .type_id = RTE_VFIO_TYPE1,
      72                 :            :                 .name = "Type 1",
      73                 :            :                 .partial_unmap = false,
      74                 :            :                 .dma_map_func = &vfio_type1_dma_map,
      75                 :            :                 .dma_user_map_func = &vfio_type1_dma_mem_map
      76                 :            :         },
      77                 :            :         /* ppc64 IOMMU, otherwise known as spapr */
      78                 :            :         {
      79                 :            :                 .type_id = RTE_VFIO_SPAPR,
      80                 :            :                 .name = "sPAPR",
      81                 :            :                 .partial_unmap = true,
      82                 :            :                 .dma_map_func = &vfio_spapr_dma_map,
      83                 :            :                 .dma_user_map_func = &vfio_spapr_dma_mem_map
      84                 :            :         },
      85                 :            :         /* IOMMU-less mode */
      86                 :            :         {
      87                 :            :                 .type_id = RTE_VFIO_NOIOMMU,
      88                 :            :                 .name = "No-IOMMU",
      89                 :            :                 .partial_unmap = true,
      90                 :            :                 .dma_map_func = &vfio_noiommu_dma_map,
      91                 :            :                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
      92                 :            :         },
      93                 :            : };
      94                 :            : 
      95                 :            : static int
      96                 :            : is_null_map(const struct user_mem_map *map)
      97                 :            : {
      98   [ #  #  #  #  :          0 :         return map->addr == 0 && map->iova == 0 &&
          #  #  #  #  #  
                      # ]
      99   [ #  #  #  #  :          0 :                         map->len == 0 && map->chunk == 0;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     100                 :            : }
     101                 :            : 
     102                 :            : /* we may need to merge user mem maps together in case of user mapping/unmapping
     103                 :            :  * chunks of memory, so we'll need a comparator function to sort segments.
     104                 :            :  */
     105                 :            : static int
     106         [ #  # ]:          0 : user_mem_map_cmp(const void *a, const void *b)
     107                 :            : {
     108                 :            :         const struct user_mem_map *umm_a = a;
     109                 :            :         const struct user_mem_map *umm_b = b;
     110                 :            : 
     111                 :            :         /* move null entries to end */
     112                 :            :         if (is_null_map(umm_a))
     113                 :            :                 return 1;
     114                 :            :         if (is_null_map(umm_b))
     115                 :            :                 return -1;
     116                 :            : 
     117                 :            :         /* sort by iova first */
     118         [ #  # ]:          0 :         if (umm_a->iova < umm_b->iova)
     119                 :            :                 return -1;
     120         [ #  # ]:          0 :         if (umm_a->iova > umm_b->iova)
     121                 :            :                 return 1;
     122                 :            : 
     123         [ #  # ]:          0 :         if (umm_a->addr < umm_b->addr)
     124                 :            :                 return -1;
     125         [ #  # ]:          0 :         if (umm_a->addr > umm_b->addr)
     126                 :            :                 return 1;
     127                 :            : 
     128         [ #  # ]:          0 :         if (umm_a->len < umm_b->len)
     129                 :            :                 return -1;
     130         [ #  # ]:          0 :         if (umm_a->len > umm_b->len)
     131                 :            :                 return 1;
     132                 :            : 
     133         [ #  # ]:          0 :         if (umm_a->chunk < umm_b->chunk)
     134                 :            :                 return -1;
     135         [ #  # ]:          0 :         if (umm_a->chunk > umm_b->chunk)
     136                 :          0 :                 return 1;
     137                 :            : 
     138                 :            :         return 0;
     139                 :            : }
     140                 :            : 
     141                 :            : /*
     142                 :            :  * Take in an address range and list of current mappings, and produce a list of
     143                 :            :  * mappings that will be kept.
     144                 :            :  */
     145                 :            : static int
     146                 :          0 : process_maps(struct user_mem_map *src, size_t src_len,
     147                 :            :                 struct user_mem_map newmap[2], uint64_t vaddr, uint64_t len)
     148                 :            : {
     149                 :            :         struct user_mem_map *src_first = &src[0];
     150                 :          0 :         struct user_mem_map *src_last = &src[src_len - 1];
     151                 :            :         struct user_mem_map *dst_first = &newmap[0];
     152                 :            :         /* we can get at most two new segments */
     153                 :          0 :         struct user_mem_map *dst_last = &newmap[1];
     154                 :          0 :         uint64_t first_off = vaddr - src_first->addr;
     155                 :          0 :         uint64_t last_off = (src_last->addr + src_last->len) - (vaddr + len);
     156                 :            :         int newmap_len = 0;
     157                 :            : 
     158         [ #  # ]:          0 :         if (first_off != 0) {
     159                 :          0 :                 dst_first->addr = src_first->addr;
     160                 :          0 :                 dst_first->iova = src_first->iova;
     161                 :          0 :                 dst_first->len = first_off;
     162                 :          0 :                 dst_first->chunk = src_first->chunk;
     163                 :            : 
     164                 :            :                 newmap_len++;
     165                 :            :         }
     166         [ #  # ]:          0 :         if (last_off != 0) {
     167                 :            :                 /* if we had start offset, we have two segments */
     168                 :            :                 struct user_mem_map *last =
     169         [ #  # ]:          0 :                                 first_off == 0 ? dst_first : dst_last;
     170                 :          0 :                 last->addr = (src_last->addr + src_last->len) - last_off;
     171                 :          0 :                 last->iova = (src_last->iova + src_last->len) - last_off;
     172                 :          0 :                 last->len = last_off;
     173                 :          0 :                 last->chunk = src_last->chunk;
     174                 :            : 
     175                 :          0 :                 newmap_len++;
     176                 :            :         }
     177                 :          0 :         return newmap_len;
     178                 :            : }
     179                 :            : 
     180                 :            : /* erase certain maps from the list */
     181                 :            : static void
     182                 :          0 : delete_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *del_maps,
     183                 :            :                 size_t n_del)
     184                 :            : {
     185                 :            :         int i;
     186                 :            :         size_t j;
     187                 :            : 
     188         [ #  # ]:          0 :         for (i = 0, j = 0; i < VFIO_MAX_USER_MEM_MAPS && j < n_del; i++) {
     189                 :          0 :                 struct user_mem_map *left = &user_mem_maps->maps[i];
     190                 :          0 :                 struct user_mem_map *right = &del_maps[j];
     191                 :            : 
     192         [ #  # ]:          0 :                 if (user_mem_map_cmp(left, right) == 0) {
     193                 :            :                         memset(left, 0, sizeof(*left));
     194                 :          0 :                         j++;
     195                 :          0 :                         user_mem_maps->n_maps--;
     196                 :            :                 }
     197                 :            :         }
     198                 :          0 : }
     199                 :            : 
     200                 :            : static void
     201                 :          0 : copy_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *add_maps,
     202                 :            :                 size_t n_add)
     203                 :            : {
     204                 :            :         int i;
     205                 :            :         size_t j;
     206                 :            : 
     207         [ #  # ]:          0 :         for (i = 0, j = 0; i < VFIO_MAX_USER_MEM_MAPS && j < n_add; i++) {
     208                 :          0 :                 struct user_mem_map *left = &user_mem_maps->maps[i];
     209         [ #  # ]:          0 :                 struct user_mem_map *right = &add_maps[j];
     210                 :            : 
     211                 :            :                 /* insert into empty space */
     212                 :            :                 if (is_null_map(left)) {
     213                 :            :                         memcpy(left, right, sizeof(*left));
     214                 :          0 :                         j++;
     215                 :          0 :                         user_mem_maps->n_maps++;
     216                 :            :                 }
     217                 :            :         }
     218                 :          0 : }
     219                 :            : 
     220                 :            : /* try merging two maps into one, return 1 if succeeded */
     221                 :            : static int
     222                 :          0 : merge_map(struct user_mem_map *left, struct user_mem_map *right)
     223                 :            : {
     224                 :            :         /* merge the same maps into one */
     225         [ #  # ]:          0 :         if (memcmp(left, right, sizeof(struct user_mem_map)) == 0)
     226                 :          0 :                 goto out;
     227                 :            : 
     228         [ #  # ]:          0 :         if (left->addr + left->len != right->addr)
     229                 :            :                 return 0;
     230         [ #  # ]:          0 :         if (left->iova + left->len != right->iova)
     231                 :            :                 return 0;
     232         [ #  # ]:          0 :         if (left->chunk != right->chunk)
     233                 :            :                 return 0;
     234                 :          0 :         left->len += right->len;
     235                 :            : 
     236                 :          0 : out:
     237                 :            :         memset(right, 0, sizeof(*right));
     238                 :            : 
     239                 :          0 :         return 1;
     240                 :            : }
     241                 :            : 
     242                 :            : static bool
     243                 :          0 : addr_is_chunk_aligned(struct user_mem_map *maps, size_t n_maps,
     244                 :            :                 uint64_t vaddr, uint64_t iova)
     245                 :            : {
     246                 :            :         unsigned int i;
     247                 :            : 
     248         [ #  # ]:          0 :         for (i = 0; i < n_maps; i++) {
     249                 :          0 :                 struct user_mem_map *map = &maps[i];
     250                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     251                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     252                 :          0 :                 uint64_t map_va_off = vaddr - map->addr;
     253                 :          0 :                 uint64_t map_iova_off = iova - map->iova;
     254                 :            : 
     255                 :            :                 /* we include end of the segment in comparison as well */
     256   [ #  #  #  # ]:          0 :                 bool addr_in_map = (vaddr >= map->addr) && (vaddr <= map_va_end);
     257   [ #  #  #  # ]:          0 :                 bool iova_in_map = (iova >= map->iova) && (iova <= map_iova_end);
     258                 :            :                 /* chunk may not be power of two, so use modulo */
     259                 :          0 :                 bool addr_is_aligned = (map_va_off % map->chunk) == 0;
     260                 :          0 :                 bool iova_is_aligned = (map_iova_off % map->chunk) == 0;
     261                 :            : 
     262         [ #  # ]:          0 :                 if (addr_in_map && iova_in_map &&
     263         [ #  # ]:          0 :                                 addr_is_aligned && iova_is_aligned)
     264                 :            :                         return true;
     265                 :            :         }
     266                 :            :         return false;
     267                 :            : }
     268                 :            : 
     269                 :            : static int
     270                 :          0 : find_user_mem_maps(struct user_mem_maps *user_mem_maps, uint64_t addr,
     271                 :            :                 uint64_t iova, uint64_t len, struct user_mem_map *dst,
     272                 :            :                 size_t dst_len)
     273                 :            : {
     274                 :          0 :         uint64_t va_end = addr + len;
     275                 :          0 :         uint64_t iova_end = iova + len;
     276                 :            :         bool found = false;
     277                 :            :         size_t j;
     278                 :            :         int i, ret;
     279                 :            : 
     280         [ #  # ]:          0 :         for (i = 0, j = 0; i < user_mem_maps->n_maps; i++) {
     281                 :          0 :                 struct user_mem_map *map = &user_mem_maps->maps[i];
     282                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     283                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     284                 :            : 
     285   [ #  #  #  # ]:          0 :                 bool start_addr_in_map = (addr >= map->addr) &&
     286                 :            :                                 (addr < map_va_end);
     287   [ #  #  #  # ]:          0 :                 bool end_addr_in_map = (va_end > map->addr) &&
     288                 :            :                                 (va_end <= map_va_end);
     289   [ #  #  #  # ]:          0 :                 bool start_iova_in_map = (iova >= map->iova) &&
     290                 :            :                                 (iova < map_iova_end);
     291   [ #  #  #  # ]:          0 :                 bool end_iova_in_map = (iova_end > map->iova) &&
     292                 :            :                                 (iova_end <= map_iova_end);
     293                 :            : 
     294                 :            :                 /* do we have space in temporary map? */
     295         [ #  # ]:          0 :                 if (j == dst_len) {
     296                 :            :                         ret = -ENOSPC;
     297                 :          0 :                         goto err;
     298                 :            :                 }
     299                 :            :                 /* check if current map is start of our segment */
     300   [ #  #  #  # ]:          0 :                 if (!found && start_addr_in_map && start_iova_in_map)
     301                 :            :                         found = true;
     302                 :            :                 /* if we have previously found a segment, add it to the map */
     303         [ #  # ]:          0 :                 if (found) {
     304                 :            :                         /* copy the segment into our temporary map */
     305         [ #  # ]:          0 :                         memcpy(&dst[j++], map, sizeof(*map));
     306                 :            : 
     307                 :            :                         /* if we match end of segment, quit */
     308         [ #  # ]:          0 :                         if (end_addr_in_map && end_iova_in_map)
     309                 :          0 :                                 return j;
     310                 :            :                 }
     311                 :            :         }
     312                 :            :         /* we didn't find anything */
     313                 :            :         ret = -ENOENT;
     314                 :          0 : err:
     315                 :          0 :         memset(dst, 0, sizeof(*dst) * dst_len);
     316                 :          0 :         return ret;
     317                 :            : }
     318                 :            : 
     319                 :            : /* this will sort all user maps, and merge/compact any adjacent maps */
     320                 :            : static void
     321                 :          0 : compact_user_maps(struct user_mem_maps *user_mem_maps)
     322                 :            : {
     323                 :            :         int i;
     324                 :            : 
     325                 :          0 :         qsort(user_mem_maps->maps, VFIO_MAX_USER_MEM_MAPS,
     326                 :            :                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
     327                 :            : 
     328                 :            :         /* we'll go over the list backwards when merging */
     329         [ #  # ]:          0 :         for (i = VFIO_MAX_USER_MEM_MAPS - 2; i >= 0; i--) {
     330                 :            :                 struct user_mem_map *l, *r;
     331                 :            : 
     332                 :          0 :                 l = &user_mem_maps->maps[i];
     333         [ #  # ]:          0 :                 r = &user_mem_maps->maps[i + 1];
     334                 :            : 
     335                 :          0 :                 if (is_null_map(l) || is_null_map(r))
     336                 :          0 :                         continue;
     337                 :            : 
     338                 :            :                 /* try and merge the maps */
     339         [ #  # ]:          0 :                 if (merge_map(l, r))
     340                 :          0 :                         user_mem_maps->n_maps--;
     341                 :            :         }
     342                 :            : 
     343                 :            :         /* the entries are still sorted, but now they have holes in them, so
     344                 :            :          * sort the list again.
     345                 :            :          */
     346                 :          0 :         qsort(user_mem_maps->maps, VFIO_MAX_USER_MEM_MAPS,
     347                 :            :                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
     348                 :          0 : }
     349                 :            : 
     350                 :            : static int
     351                 :          0 : vfio_open_group_fd(int iommu_group_num)
     352                 :            : {
     353                 :            :         int vfio_group_fd;
     354                 :            :         char filename[PATH_MAX];
     355                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     356                 :          0 :         struct rte_mp_reply mp_reply = {0};
     357                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     358                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     359                 :            :         const struct internal_config *internal_conf =
     360                 :          0 :                 eal_get_internal_configuration();
     361                 :            : 
     362                 :            :         /* if primary, try to open the group */
     363         [ #  # ]:          0 :         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
     364                 :            :                 /* try regular group format */
     365                 :            :                 snprintf(filename, sizeof(filename),
     366                 :            :                                  VFIO_GROUP_FMT, iommu_group_num);
     367                 :            :                 vfio_group_fd = open(filename, O_RDWR);
     368         [ #  # ]:          0 :                 if (vfio_group_fd < 0) {
     369                 :            :                         /* if file not found, it's not an error */
     370         [ #  # ]:          0 :                         if (errno != ENOENT) {
     371                 :          0 :                                 EAL_LOG(ERR, "Cannot open %s: %s",
     372                 :            :                                                 filename, strerror(errno));
     373                 :          0 :                                 return -1;
     374                 :            :                         }
     375                 :            : 
     376                 :            :                         /* special case: try no-IOMMU path as well */
     377                 :            :                         snprintf(filename, sizeof(filename),
     378                 :            :                                         VFIO_NOIOMMU_GROUP_FMT,
     379                 :            :                                         iommu_group_num);
     380                 :            :                         vfio_group_fd = open(filename, O_RDWR);
     381         [ #  # ]:          0 :                         if (vfio_group_fd < 0) {
     382         [ #  # ]:          0 :                                 if (errno != ENOENT) {
     383                 :          0 :                                         EAL_LOG(ERR,
     384                 :            :                                                 "Cannot open %s: %s",
     385                 :            :                                                 filename, strerror(errno));
     386                 :          0 :                                         return -1;
     387                 :            :                                 }
     388                 :            :                                 return -ENOENT;
     389                 :            :                         }
     390                 :            :                         /* noiommu group found */
     391                 :            :                 }
     392                 :            : 
     393                 :          0 :                 return vfio_group_fd;
     394                 :            :         }
     395                 :            :         /* if we're in a secondary process, request group fd from the primary
     396                 :            :          * process via mp channel.
     397                 :            :          */
     398                 :          0 :         p->req = SOCKET_REQ_GROUP;
     399                 :          0 :         p->group_num = iommu_group_num;
     400                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
     401                 :          0 :         mp_req.len_param = sizeof(*p);
     402                 :          0 :         mp_req.num_fds = 0;
     403                 :            : 
     404                 :            :         vfio_group_fd = -1;
     405         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
     406         [ #  # ]:          0 :             mp_reply.nb_received == 1) {
     407                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     408                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     409   [ #  #  #  # ]:          0 :                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
     410                 :          0 :                         vfio_group_fd = mp_rep->fds[0];
     411         [ #  # ]:          0 :                 } else if (p->result == SOCKET_NO_FD) {
     412                 :          0 :                         EAL_LOG(ERR, "Bad VFIO group fd");
     413                 :            :                         vfio_group_fd = -ENOENT;
     414                 :            :                 }
     415                 :            :         }
     416                 :            : 
     417                 :          0 :         free(mp_reply.msgs);
     418         [ #  # ]:          0 :         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
     419                 :          0 :                 EAL_LOG(ERR, "Cannot request VFIO group fd");
     420                 :            :         return vfio_group_fd;
     421                 :            : }
     422                 :            : 
     423                 :            : static struct vfio_config *
     424                 :            : get_vfio_cfg_by_group_num(int iommu_group_num)
     425                 :            : {
     426                 :            :         struct vfio_config *vfio_cfg;
     427                 :            :         int i, j;
     428                 :            : 
     429   [ #  #  #  #  :          0 :         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
                   #  # ]
     430                 :          0 :                 vfio_cfg = &vfio_cfgs[i];
     431   [ #  #  #  #  :          0 :                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
                   #  # ]
     432   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].group_num ==
                   #  # ]
     433                 :            :                                         iommu_group_num)
     434                 :            :                                 return vfio_cfg;
     435                 :            :                 }
     436                 :            :         }
     437                 :            : 
     438                 :            :         return NULL;
     439                 :            : }
     440                 :            : 
     441                 :            : static int
     442                 :          0 : vfio_get_group_fd(struct vfio_config *vfio_cfg,
     443                 :            :                 int iommu_group_num)
     444                 :            : {
     445                 :            :         int i;
     446                 :            :         int vfio_group_fd;
     447                 :            :         struct vfio_group *cur_grp;
     448                 :            : 
     449                 :            :         /* check if we already have the group descriptor open */
     450         [ #  # ]:          0 :         for (i = 0; i < VFIO_MAX_GROUPS; i++)
     451         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
     452                 :          0 :                         return vfio_cfg->vfio_groups[i].fd;
     453                 :            : 
     454                 :            :         /* Lets see first if there is room for a new group */
     455         [ #  # ]:          0 :         if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
     456                 :          0 :                 EAL_LOG(ERR, "Maximum number of VFIO groups reached!");
     457                 :          0 :                 return -1;
     458                 :            :         }
     459                 :            : 
     460                 :            :         /* Now lets get an index for the new group */
     461         [ #  # ]:          0 :         for (i = 0; i < VFIO_MAX_GROUPS; i++)
     462         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
     463                 :          0 :                         cur_grp = &vfio_cfg->vfio_groups[i];
     464                 :          0 :                         break;
     465                 :            :                 }
     466                 :            : 
     467                 :            :         /* This should not happen */
     468         [ #  # ]:          0 :         if (i == VFIO_MAX_GROUPS) {
     469                 :          0 :                 EAL_LOG(ERR, "No VFIO group free slot found");
     470                 :          0 :                 return -1;
     471                 :            :         }
     472                 :            : 
     473                 :          0 :         vfio_group_fd = vfio_open_group_fd(iommu_group_num);
     474         [ #  # ]:          0 :         if (vfio_group_fd < 0) {
     475                 :          0 :                 EAL_LOG(ERR, "Failed to open VFIO group %d",
     476                 :            :                         iommu_group_num);
     477                 :          0 :                 return vfio_group_fd;
     478                 :            :         }
     479                 :            : 
     480                 :          0 :         cur_grp->group_num = iommu_group_num;
     481                 :          0 :         cur_grp->fd = vfio_group_fd;
     482                 :          0 :         vfio_cfg->vfio_active_groups++;
     483                 :            : 
     484                 :          0 :         return vfio_group_fd;
     485                 :            : }
     486                 :            : 
     487                 :            : static struct vfio_config *
     488                 :            : get_vfio_cfg_by_group_fd(int vfio_group_fd)
     489                 :            : {
     490                 :            :         struct vfio_config *vfio_cfg;
     491                 :            :         int i, j;
     492                 :            : 
     493   [ #  #  #  #  :          0 :         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
             #  #  #  # ]
     494                 :          0 :                 vfio_cfg = &vfio_cfgs[i];
     495   [ #  #  #  #  :          0 :                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
             #  #  #  # ]
     496   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
             #  #  #  # ]
     497                 :            :                                 return vfio_cfg;
     498                 :            :         }
     499                 :            : 
     500                 :            :         return NULL;
     501                 :            : }
     502                 :            : 
     503                 :            : static struct vfio_config *
     504                 :            : get_vfio_cfg_by_container_fd(int container_fd)
     505                 :            : {
     506                 :            :         int i;
     507                 :            : 
     508   [ #  #  #  # ]:          0 :         if (container_fd == RTE_VFIO_DEFAULT_CONTAINER_FD)
     509                 :          0 :                 return default_vfio_cfg;
     510                 :            : 
     511   [ #  #  #  #  :          0 :         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
          #  #  #  #  #  
                      # ]
     512   [ #  #  #  #  :          0 :                 if (vfio_cfgs[i].vfio_container_fd == container_fd)
          #  #  #  #  #  
                      # ]
     513                 :          0 :                         return &vfio_cfgs[i];
     514                 :            :         }
     515                 :            : 
     516                 :            :         return NULL;
     517                 :            : }
     518                 :            : 
     519                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_group_fd)
     520                 :            : int
     521                 :          0 : rte_vfio_get_group_fd(int iommu_group_num)
     522                 :            : {
     523                 :            :         struct vfio_config *vfio_cfg;
     524                 :            : 
     525                 :            :         /* get the vfio_config it belongs to */
     526                 :            :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
     527         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
     528                 :            : 
     529                 :          0 :         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
     530                 :            : }
     531                 :            : 
     532                 :            : static int
     533                 :            : get_vfio_group_idx(int vfio_group_fd)
     534                 :            : {
     535                 :            :         struct vfio_config *vfio_cfg;
     536                 :            :         int i, j;
     537                 :            : 
     538   [ #  #  #  #  :          0 :         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
             #  #  #  # ]
     539                 :            :                 vfio_cfg = &vfio_cfgs[i];
     540   [ #  #  #  #  :          0 :                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
             #  #  #  # ]
     541   [ #  #  #  #  :          0 :                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
             #  #  #  # ]
     542                 :            :                                 return j;
     543                 :            :         }
     544                 :            : 
     545                 :            :         return -1;
     546                 :            : }
     547                 :            : 
     548                 :            : static void
     549                 :          0 : vfio_group_device_get(int vfio_group_fd)
     550                 :            : {
     551                 :            :         struct vfio_config *vfio_cfg;
     552                 :            :         int i;
     553                 :            : 
     554                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     555         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     556                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     557                 :          0 :                 return;
     558                 :            :         }
     559                 :            : 
     560                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     561         [ #  # ]:          0 :         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
     562                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     563                 :            :         else
     564                 :          0 :                 vfio_cfg->vfio_groups[i].devices++;
     565                 :            : }
     566                 :            : 
     567                 :            : static void
     568                 :          0 : vfio_group_device_put(int vfio_group_fd)
     569                 :            : {
     570                 :            :         struct vfio_config *vfio_cfg;
     571                 :            :         int i;
     572                 :            : 
     573                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     574         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     575                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     576                 :          0 :                 return;
     577                 :            :         }
     578                 :            : 
     579                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     580         [ #  # ]:          0 :         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
     581                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     582                 :            :         else
     583                 :          0 :                 vfio_cfg->vfio_groups[i].devices--;
     584                 :            : }
     585                 :            : 
     586                 :            : static int
     587                 :          0 : vfio_group_device_count(int vfio_group_fd)
     588                 :            : {
     589                 :            :         struct vfio_config *vfio_cfg;
     590                 :            :         int i;
     591                 :            : 
     592                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     593         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     594                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     595                 :          0 :                 return -1;
     596                 :            :         }
     597                 :            : 
     598                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     599         [ #  # ]:          0 :         if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
     600                 :          0 :                 EAL_LOG(ERR, "Wrong VFIO group index (%d)", i);
     601                 :          0 :                 return -1;
     602                 :            :         }
     603                 :            : 
     604                 :          0 :         return vfio_cfg->vfio_groups[i].devices;
     605                 :            : }
     606                 :            : 
     607                 :            : static void
     608                 :          0 : vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
     609                 :            :                 void *arg __rte_unused)
     610                 :            : {
     611                 :            :         struct rte_memseg_list *msl;
     612                 :            :         struct rte_memseg *ms;
     613                 :            :         size_t cur_len = 0;
     614                 :            : 
     615                 :          0 :         msl = rte_mem_virt2memseg_list(addr);
     616                 :            : 
     617                 :            :         /* for IOVA as VA mode, no need to care for IOVA addresses */
     618   [ #  #  #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
     619                 :          0 :                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
     620                 :          0 :                 uint64_t page_sz = msl->page_sz;
     621                 :            : 
     622                 :            :                 /* Maintain granularity of DMA map/unmap to memseg size */
     623         [ #  # ]:          0 :                 for (; cur_len < len; cur_len += page_sz) {
     624         [ #  # ]:          0 :                         if (type == RTE_MEM_EVENT_ALLOC)
     625                 :          0 :                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
     626                 :            :                                                  vfio_va, page_sz, 1);
     627                 :            :                         else
     628                 :          0 :                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
     629                 :            :                                                  vfio_va, page_sz, 0);
     630                 :          0 :                         vfio_va += page_sz;
     631                 :            :                 }
     632                 :            : 
     633                 :            :                 return;
     634                 :            :         }
     635                 :            : 
     636                 :            :         /* memsegs are contiguous in memory */
     637                 :          0 :         ms = rte_mem_virt2memseg(addr, msl);
     638         [ #  # ]:          0 :         while (cur_len < len) {
     639                 :            :                 /* some memory segments may have invalid IOVA */
     640         [ #  # ]:          0 :                 if (ms->iova == RTE_BAD_IOVA) {
     641                 :          0 :                         EAL_LOG(DEBUG,
     642                 :            :                                 "Memory segment at %p has bad IOVA, skipping",
     643                 :            :                                 ms->addr);
     644                 :          0 :                         goto next;
     645                 :            :                 }
     646         [ #  # ]:          0 :                 if (type == RTE_MEM_EVENT_ALLOC)
     647                 :          0 :                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
     648                 :            :                                         ms->iova, ms->len, 1);
     649                 :            :                 else
     650                 :          0 :                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
     651                 :            :                                         ms->iova, ms->len, 0);
     652                 :          0 : next:
     653                 :          0 :                 cur_len += ms->len;
     654                 :          0 :                 ++ms;
     655                 :            :         }
     656                 :            : }
     657                 :            : 
     658                 :            : static int
     659                 :          0 : vfio_sync_default_container(void)
     660                 :            : {
     661                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     662                 :          0 :         struct rte_mp_reply mp_reply = {0};
     663                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     664                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     665                 :            :         int iommu_type_id;
     666                 :            :         unsigned int i;
     667                 :            : 
     668                 :            :         /* cannot be called from primary */
     669         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_SECONDARY)
     670                 :            :                 return -1;
     671                 :            : 
     672                 :            :         /* default container fd should have been opened in rte_vfio_enable() */
     673         [ #  # ]:          0 :         if (!default_vfio_cfg->vfio_enabled ||
     674         [ #  # ]:          0 :                         default_vfio_cfg->vfio_container_fd < 0) {
     675                 :          0 :                 EAL_LOG(ERR, "VFIO support is not initialized");
     676                 :          0 :                 return -1;
     677                 :            :         }
     678                 :            : 
     679                 :            :         /* find default container's IOMMU type */
     680                 :          0 :         p->req = SOCKET_REQ_IOMMU_TYPE;
     681                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
     682                 :          0 :         mp_req.len_param = sizeof(*p);
     683                 :          0 :         mp_req.num_fds = 0;
     684                 :            : 
     685                 :            :         iommu_type_id = -1;
     686         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
     687         [ #  # ]:          0 :                         mp_reply.nb_received == 1) {
     688                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     689                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     690         [ #  # ]:          0 :                 if (p->result == SOCKET_OK)
     691                 :          0 :                         iommu_type_id = p->iommu_type_id;
     692                 :            :         }
     693                 :          0 :         free(mp_reply.msgs);
     694         [ #  # ]:          0 :         if (iommu_type_id < 0) {
     695                 :          0 :                 EAL_LOG(ERR,
     696                 :            :                         "Could not get IOMMU type for default container");
     697                 :          0 :                 return -1;
     698                 :            :         }
     699                 :            : 
     700                 :            :         /* we now have an fd for default container, as well as its IOMMU type.
     701                 :            :          * now, set up default VFIO container config to match.
     702                 :            :          */
     703         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(iommu_types); i++) {
     704                 :          0 :                 const struct vfio_iommu_type *t = &iommu_types[i];
     705         [ #  # ]:          0 :                 if (t->type_id != iommu_type_id)
     706                 :            :                         continue;
     707                 :            : 
     708                 :            :                 /* we found our IOMMU type */
     709                 :          0 :                 default_vfio_cfg->vfio_iommu_type = t;
     710                 :            : 
     711                 :          0 :                 return 0;
     712                 :            :         }
     713                 :          0 :         EAL_LOG(ERR, "Could not find IOMMU type id (%i)",
     714                 :            :                         iommu_type_id);
     715                 :          0 :         return -1;
     716                 :            : }
     717                 :            : 
     718                 :            : RTE_EXPORT_SYMBOL(rte_vfio_clear_group)
     719                 :            : int
     720                 :          0 : rte_vfio_clear_group(int vfio_group_fd)
     721                 :            : {
     722                 :            :         int i;
     723                 :            :         struct vfio_config *vfio_cfg;
     724                 :            : 
     725                 :            :         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
     726         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
     727                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO group fd!");
     728                 :          0 :                 return -1;
     729                 :            :         }
     730                 :            : 
     731                 :            :         i = get_vfio_group_idx(vfio_group_fd);
     732         [ #  # ]:          0 :         if (i < 0)
     733                 :            :                 return -1;
     734                 :          0 :         vfio_cfg->vfio_groups[i].group_num = -1;
     735                 :          0 :         vfio_cfg->vfio_groups[i].fd = -1;
     736                 :          0 :         vfio_cfg->vfio_groups[i].devices = 0;
     737                 :          0 :         vfio_cfg->vfio_active_groups--;
     738                 :            : 
     739                 :          0 :         return 0;
     740                 :            : }
     741                 :            : 
     742                 :            : RTE_EXPORT_SYMBOL(rte_vfio_setup_device)
     743                 :            : int
     744                 :          0 : rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
     745                 :            :                 int *vfio_dev_fd, struct vfio_device_info *device_info)
     746                 :            : {
     747                 :          0 :         struct vfio_group_status group_status = {
     748                 :            :                         .argsz = sizeof(group_status)
     749                 :            :         };
     750                 :            :         struct vfio_config *vfio_cfg;
     751                 :            :         struct user_mem_maps *user_mem_maps;
     752                 :            :         int vfio_container_fd;
     753                 :            :         int vfio_group_fd;
     754                 :            :         int iommu_group_num;
     755                 :            :         rte_uuid_t vf_token;
     756                 :            :         int i, ret;
     757                 :            :         const struct internal_config *internal_conf =
     758                 :          0 :                 eal_get_internal_configuration();
     759                 :            : 
     760                 :            :         /* get group number */
     761                 :          0 :         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
     762         [ #  # ]:          0 :         if (ret == 0) {
     763                 :          0 :                 EAL_LOG(NOTICE,
     764                 :            :                                 "%s not managed by VFIO driver, skipping",
     765                 :            :                                 dev_addr);
     766                 :          0 :                 return 1;
     767                 :            :         }
     768                 :            : 
     769                 :            :         /* if negative, something failed */
     770         [ #  # ]:          0 :         if (ret < 0)
     771                 :            :                 return -1;
     772                 :            : 
     773                 :            :         /* get the actual group fd */
     774                 :          0 :         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
     775         [ #  # ]:          0 :         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
     776                 :            :                 return -1;
     777                 :            : 
     778                 :            :         /*
     779                 :            :          * if vfio_group_fd == -ENOENT, that means the device
     780                 :            :          * isn't managed by VFIO
     781                 :            :          */
     782         [ #  # ]:          0 :         if (vfio_group_fd == -ENOENT) {
     783                 :          0 :                 EAL_LOG(NOTICE,
     784                 :            :                                 "%s not managed by VFIO driver, skipping",
     785                 :            :                                 dev_addr);
     786                 :          0 :                 return 1;
     787                 :            :         }
     788                 :            : 
     789                 :            :         /*
     790                 :            :          * at this point, we know that this group is viable (meaning, all devices
     791                 :            :          * are either bound to VFIO or not bound to anything)
     792                 :            :          */
     793                 :            : 
     794                 :            :         /* check if the group is viable */
     795                 :          0 :         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
     796         [ #  # ]:          0 :         if (ret) {
     797                 :          0 :                 EAL_LOG(ERR, "%s cannot get VFIO group status, "
     798                 :            :                         "error %i (%s)", dev_addr, errno, strerror(errno));
     799                 :          0 :                 close(vfio_group_fd);
     800                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     801                 :          0 :                 return -1;
     802         [ #  # ]:          0 :         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
     803                 :          0 :                 EAL_LOG(ERR, "%s VFIO group is not viable! "
     804                 :            :                         "Not all devices in IOMMU group bound to VFIO or unbound",
     805                 :            :                         dev_addr);
     806                 :          0 :                 close(vfio_group_fd);
     807                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     808                 :          0 :                 return -1;
     809                 :            :         }
     810                 :            : 
     811                 :            :         /* get the vfio_config it belongs to */
     812                 :          0 :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
     813         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
     814                 :          0 :         vfio_container_fd = vfio_cfg->vfio_container_fd;
     815                 :            :         user_mem_maps = &vfio_cfg->mem_maps;
     816                 :            : 
     817                 :            :         /* check if group does not have a container yet */
     818         [ #  # ]:          0 :         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
     819                 :            : 
     820                 :            :                 /* add group to a container */
     821                 :          0 :                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
     822                 :            :                                 &vfio_container_fd);
     823         [ #  # ]:          0 :                 if (ret) {
     824                 :          0 :                         EAL_LOG(ERR,
     825                 :            :                                 "%s cannot add VFIO group to container, error "
     826                 :            :                                 "%i (%s)", dev_addr, errno, strerror(errno));
     827                 :          0 :                         close(vfio_group_fd);
     828                 :          0 :                         rte_vfio_clear_group(vfio_group_fd);
     829                 :          0 :                         return -1;
     830                 :            :                 }
     831                 :            : 
     832                 :            :                 /*
     833                 :            :                  * pick an IOMMU type and set up DMA mappings for container
     834                 :            :                  *
     835                 :            :                  * needs to be done only once, only when first group is
     836                 :            :                  * assigned to a container and only in primary process.
     837                 :            :                  * Note this can happen several times with the hotplug
     838                 :            :                  * functionality.
     839                 :            :                  */
     840         [ #  # ]:          0 :                 if (internal_conf->process_type == RTE_PROC_PRIMARY &&
     841   [ #  #  #  # ]:          0 :                                 vfio_cfg->vfio_active_groups == 1 &&
     842                 :          0 :                                 vfio_group_device_count(vfio_group_fd) == 0) {
     843                 :            :                         const struct vfio_iommu_type *t;
     844                 :            : 
     845                 :            :                         /* select an IOMMU type which we will be using */
     846                 :          0 :                         t = vfio_set_iommu_type(vfio_container_fd);
     847         [ #  # ]:          0 :                         if (!t) {
     848                 :          0 :                                 EAL_LOG(ERR,
     849                 :            :                                         "%s failed to select IOMMU type",
     850                 :            :                                         dev_addr);
     851                 :          0 :                                 close(vfio_group_fd);
     852                 :          0 :                                 rte_vfio_clear_group(vfio_group_fd);
     853                 :          0 :                                 return -1;
     854                 :            :                         }
     855                 :            :                         /* lock memory hotplug before mapping and release it
     856                 :            :                          * after registering callback, to prevent races
     857                 :            :                          */
     858                 :          0 :                         rte_mcfg_mem_read_lock();
     859         [ #  # ]:          0 :                         if (vfio_cfg == default_vfio_cfg)
     860                 :          0 :                                 ret = t->dma_map_func(vfio_container_fd);
     861                 :            :                         else
     862                 :            :                                 ret = 0;
     863         [ #  # ]:          0 :                         if (ret) {
     864                 :          0 :                                 EAL_LOG(ERR,
     865                 :            :                                         "%s DMA remapping failed, error "
     866                 :            :                                         "%i (%s)",
     867                 :            :                                         dev_addr, errno, strerror(errno));
     868                 :          0 :                                 close(vfio_group_fd);
     869                 :          0 :                                 rte_vfio_clear_group(vfio_group_fd);
     870                 :          0 :                                 rte_mcfg_mem_read_unlock();
     871                 :          0 :                                 return -1;
     872                 :            :                         }
     873                 :            : 
     874                 :          0 :                         vfio_cfg->vfio_iommu_type = t;
     875                 :            : 
     876                 :            :                         /* re-map all user-mapped segments */
     877                 :          0 :                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
     878                 :            : 
     879                 :            :                         /* this IOMMU type may not support DMA mapping, but
     880                 :            :                          * if we have mappings in the list - that means we have
     881                 :            :                          * previously mapped something successfully, so we can
     882                 :            :                          * be sure that DMA mapping is supported.
     883                 :            :                          */
     884         [ #  # ]:          0 :                         for (i = 0; i < user_mem_maps->n_maps; i++) {
     885                 :            :                                 struct user_mem_map *map;
     886                 :            :                                 map = &user_mem_maps->maps[i];
     887                 :            : 
     888                 :          0 :                                 ret = t->dma_user_map_func(
     889                 :            :                                                 vfio_container_fd,
     890                 :            :                                                 map->addr, map->iova, map->len,
     891                 :            :                                                 1);
     892         [ #  # ]:          0 :                                 if (ret) {
     893                 :          0 :                                         EAL_LOG(ERR, "Couldn't map user memory for DMA: "
     894                 :            :                                                         "va: 0x%" PRIx64 " "
     895                 :            :                                                         "iova: 0x%" PRIx64 " "
     896                 :            :                                                         "len: 0x%" PRIu64,
     897                 :            :                                                         map->addr, map->iova,
     898                 :            :                                                         map->len);
     899                 :            :                                         rte_spinlock_recursive_unlock(
     900                 :            :                                                         &user_mem_maps->lock);
     901                 :          0 :                                         rte_mcfg_mem_read_unlock();
     902                 :          0 :                                         return -1;
     903                 :            :                                 }
     904                 :            :                         }
     905                 :            :                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
     906                 :            : 
     907                 :            :                         /* register callback for mem events */
     908         [ #  # ]:          0 :                         if (vfio_cfg == default_vfio_cfg)
     909                 :          0 :                                 ret = rte_mem_event_callback_register(
     910                 :            :                                         VFIO_MEM_EVENT_CLB_NAME,
     911                 :            :                                         vfio_mem_event_callback, NULL);
     912                 :            :                         else
     913                 :            :                                 ret = 0;
     914                 :            :                         /* unlock memory hotplug */
     915                 :          0 :                         rte_mcfg_mem_read_unlock();
     916                 :            : 
     917   [ #  #  #  # ]:          0 :                         if (ret && rte_errno != ENOTSUP) {
     918                 :          0 :                                 EAL_LOG(ERR, "Could not install memory event callback for VFIO");
     919                 :          0 :                                 return -1;
     920                 :            :                         }
     921         [ #  # ]:          0 :                         if (ret)
     922                 :          0 :                                 EAL_LOG(DEBUG, "Memory event callbacks not supported");
     923                 :            :                         else
     924                 :          0 :                                 EAL_LOG(DEBUG, "Installed memory event callback for VFIO");
     925                 :            :                 }
     926         [ #  # ]:          0 :         } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
     927         [ #  # ]:          0 :                         vfio_cfg == default_vfio_cfg &&
     928         [ #  # ]:          0 :                         vfio_cfg->vfio_iommu_type == NULL) {
     929                 :            :                 /* if we're not a primary process, we do not set up the VFIO
     930                 :            :                  * container because it's already been set up by the primary
     931                 :            :                  * process. instead, we simply ask the primary about VFIO type
     932                 :            :                  * we are using, and set the VFIO config up appropriately.
     933                 :            :                  */
     934                 :          0 :                 ret = vfio_sync_default_container();
     935         [ #  # ]:          0 :                 if (ret < 0) {
     936                 :          0 :                         EAL_LOG(ERR, "Could not sync default VFIO container");
     937                 :          0 :                         close(vfio_group_fd);
     938                 :          0 :                         rte_vfio_clear_group(vfio_group_fd);
     939                 :          0 :                         return -1;
     940                 :            :                 }
     941                 :            :                 /* we have successfully initialized VFIO, notify user */
     942                 :          0 :                 const struct vfio_iommu_type *t =
     943                 :          0 :                                 default_vfio_cfg->vfio_iommu_type;
     944                 :          0 :                 EAL_LOG(INFO, "Using IOMMU type %d (%s)",
     945                 :            :                                 t->type_id, t->name);
     946                 :            :         }
     947                 :            : 
     948                 :          0 :         rte_eal_vfio_get_vf_token(vf_token);
     949                 :            : 
     950                 :            :         /* get a file descriptor for the device with VF token firstly */
     951         [ #  # ]:          0 :         if (!rte_uuid_is_null(vf_token)) {
     952                 :            :                 char vf_token_str[RTE_UUID_STRLEN];
     953                 :            :                 char dev[PATH_MAX];
     954                 :            : 
     955                 :          0 :                 rte_uuid_unparse(vf_token, vf_token_str, sizeof(vf_token_str));
     956                 :            :                 snprintf(dev, sizeof(dev),
     957                 :            :                          "%s vf_token=%s", dev_addr, vf_token_str);
     958                 :            : 
     959                 :          0 :                 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD,
     960                 :            :                                      dev);
     961         [ #  # ]:          0 :                 if (*vfio_dev_fd >= 0)
     962                 :          0 :                         goto dev_get_info;
     963                 :            :         }
     964                 :            : 
     965                 :            :         /* get a file descriptor for the device */
     966                 :          0 :         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
     967         [ #  # ]:          0 :         if (*vfio_dev_fd < 0) {
     968                 :            :                 /* if we cannot get a device fd, this implies a problem with
     969                 :            :                  * the VFIO group or the container not having IOMMU configured.
     970                 :            :                  */
     971                 :            : 
     972                 :          0 :                 EAL_LOG(WARNING, "Getting a vfio_dev_fd for %s failed",
     973                 :            :                                 dev_addr);
     974                 :          0 :                 close(vfio_group_fd);
     975                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     976                 :          0 :                 return -1;
     977                 :            :         }
     978                 :            : 
     979                 :            :         /* test and setup the device */
     980                 :          0 : dev_get_info:
     981                 :          0 :         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
     982         [ #  # ]:          0 :         if (ret) {
     983                 :          0 :                 EAL_LOG(ERR, "%s cannot get device info, "
     984                 :            :                                 "error %i (%s)", dev_addr, errno,
     985                 :            :                                 strerror(errno));
     986                 :          0 :                 close(*vfio_dev_fd);
     987                 :          0 :                 close(vfio_group_fd);
     988                 :          0 :                 rte_vfio_clear_group(vfio_group_fd);
     989                 :          0 :                 return -1;
     990                 :            :         }
     991                 :          0 :         vfio_group_device_get(vfio_group_fd);
     992                 :            : 
     993                 :          0 :         return 0;
     994                 :            : }
     995                 :            : 
     996                 :            : RTE_EXPORT_SYMBOL(rte_vfio_release_device)
     997                 :            : int
     998                 :          0 : rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
     999                 :            :                     int vfio_dev_fd)
    1000                 :            : {
    1001                 :            :         struct vfio_config *vfio_cfg;
    1002                 :            :         int vfio_group_fd;
    1003                 :            :         int iommu_group_num;
    1004                 :            :         int ret;
    1005                 :            : 
    1006                 :            :         /* we don't want any DMA mapping messages to come while we're detaching
    1007                 :            :          * VFIO device, because this might be the last device and we might need
    1008                 :            :          * to unregister the callback.
    1009                 :            :          */
    1010                 :          0 :         rte_mcfg_mem_read_lock();
    1011                 :            : 
    1012                 :            :         /* get group number */
    1013                 :          0 :         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
    1014         [ #  # ]:          0 :         if (ret <= 0) {
    1015                 :          0 :                 EAL_LOG(WARNING, "%s not managed by VFIO driver",
    1016                 :            :                         dev_addr);
    1017                 :            :                 /* This is an error at this point. */
    1018                 :            :                 ret = -1;
    1019                 :          0 :                 goto out;
    1020                 :            :         }
    1021                 :            : 
    1022                 :            :         /* get the actual group fd */
    1023                 :          0 :         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
    1024         [ #  # ]:          0 :         if (vfio_group_fd < 0) {
    1025                 :          0 :                 EAL_LOG(INFO, "rte_vfio_get_group_fd failed for %s",
    1026                 :            :                                    dev_addr);
    1027                 :            :                 ret = vfio_group_fd;
    1028                 :          0 :                 goto out;
    1029                 :            :         }
    1030                 :            : 
    1031                 :            :         /* get the vfio_config it belongs to */
    1032                 :          0 :         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
    1033         [ #  # ]:          0 :         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
    1034                 :            : 
    1035                 :            :         /* At this point we got an active group. Closing it will make the
    1036                 :            :          * container detachment. If this is the last active group, VFIO kernel
    1037                 :            :          * code will unset the container and the IOMMU mappings.
    1038                 :            :          */
    1039                 :            : 
    1040                 :            :         /* Closing a device */
    1041         [ #  # ]:          0 :         if (close(vfio_dev_fd) < 0) {
    1042                 :          0 :                 EAL_LOG(INFO, "Error when closing vfio_dev_fd for %s",
    1043                 :            :                                    dev_addr);
    1044                 :            :                 ret = -1;
    1045                 :          0 :                 goto out;
    1046                 :            :         }
    1047                 :            : 
    1048                 :            :         /* An VFIO group can have several devices attached. Just when there is
    1049                 :            :          * no devices remaining should the group be closed.
    1050                 :            :          */
    1051                 :          0 :         vfio_group_device_put(vfio_group_fd);
    1052         [ #  # ]:          0 :         if (!vfio_group_device_count(vfio_group_fd)) {
    1053                 :            : 
    1054         [ #  # ]:          0 :                 if (close(vfio_group_fd) < 0) {
    1055                 :          0 :                         EAL_LOG(INFO, "Error when closing vfio_group_fd for %s",
    1056                 :            :                                 dev_addr);
    1057                 :            :                         ret = -1;
    1058                 :          0 :                         goto out;
    1059                 :            :                 }
    1060                 :            : 
    1061         [ #  # ]:          0 :                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
    1062                 :          0 :                         EAL_LOG(INFO, "Error when clearing group for %s",
    1063                 :            :                                            dev_addr);
    1064                 :            :                         ret = -1;
    1065                 :          0 :                         goto out;
    1066                 :            :                 }
    1067                 :            :         }
    1068                 :            : 
    1069                 :            :         /* if there are no active device groups, unregister the callback to
    1070                 :            :          * avoid spurious attempts to map/unmap memory from VFIO.
    1071                 :            :          */
    1072   [ #  #  #  #  :          0 :         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
                   #  # ]
    1073                 :          0 :                         rte_eal_process_type() != RTE_PROC_SECONDARY)
    1074                 :          0 :                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
    1075                 :            :                                 NULL);
    1076                 :            : 
    1077                 :            :         /* success */
    1078                 :            :         ret = 0;
    1079                 :            : 
    1080                 :          0 : out:
    1081                 :          0 :         rte_mcfg_mem_read_unlock();
    1082                 :          0 :         return ret;
    1083                 :            : }
    1084                 :            : 
    1085                 :            : RTE_EXPORT_SYMBOL(rte_vfio_enable)
    1086                 :            : int
    1087                 :        183 : rte_vfio_enable(const char *modname)
    1088                 :            : {
    1089                 :            :         /* initialize group list */
    1090                 :            :         int i, j;
    1091                 :            :         int vfio_available;
    1092                 :            :         const struct internal_config *internal_conf =
    1093                 :        183 :                 eal_get_internal_configuration();
    1094                 :            : 
    1095                 :        183 :         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
    1096                 :            : 
    1097         [ +  + ]:      11895 :         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
    1098                 :      11712 :                 vfio_cfgs[i].vfio_container_fd = -1;
    1099                 :      11712 :                 vfio_cfgs[i].vfio_active_groups = 0;
    1100                 :      11712 :                 vfio_cfgs[i].vfio_iommu_type = NULL;
    1101                 :      11712 :                 vfio_cfgs[i].mem_maps.lock = lock;
    1102                 :            : 
    1103         [ +  + ]:     761280 :                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
    1104                 :     749568 :                         vfio_cfgs[i].vfio_groups[j].fd = -1;
    1105                 :     749568 :                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
    1106                 :     749568 :                         vfio_cfgs[i].vfio_groups[j].devices = 0;
    1107                 :            :                 }
    1108                 :            :         }
    1109                 :            : 
    1110                 :        183 :         EAL_LOG(DEBUG, "Probing VFIO support...");
    1111                 :            : 
    1112                 :            :         /* check if vfio module is loaded */
    1113                 :        183 :         vfio_available = rte_eal_check_module(modname);
    1114                 :            : 
    1115                 :            :         /* return error directly */
    1116         [ -  + ]:        183 :         if (vfio_available == -1) {
    1117                 :          0 :                 EAL_LOG(INFO, "Could not get loaded module details!");
    1118                 :          0 :                 return -1;
    1119                 :            :         }
    1120                 :            : 
    1121                 :            :         /* return 0 if VFIO modules not loaded */
    1122         [ -  + ]:        183 :         if (vfio_available == 0) {
    1123                 :          0 :                 EAL_LOG(DEBUG,
    1124                 :            :                         "VFIO modules not loaded, skipping VFIO support...");
    1125                 :          0 :                 return 0;
    1126                 :            :         }
    1127                 :            : 
    1128         [ +  + ]:        183 :         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
    1129         [ -  + ]:        156 :                 if (vfio_mp_sync_setup() == -1) {
    1130                 :          0 :                         default_vfio_cfg->vfio_container_fd = -1;
    1131                 :            :                 } else {
    1132                 :            :                         /* open a new container */
    1133                 :        156 :                         default_vfio_cfg->vfio_container_fd = rte_vfio_get_container_fd();
    1134                 :            :                 }
    1135                 :            :         } else {
    1136                 :            :                 /* get the default container from the primary process */
    1137                 :         27 :                 default_vfio_cfg->vfio_container_fd =
    1138                 :         27 :                                 vfio_get_default_container_fd();
    1139                 :            :         }
    1140                 :            : 
    1141                 :            :         /* check if we have VFIO driver enabled */
    1142         [ +  + ]:        183 :         if (default_vfio_cfg->vfio_container_fd != -1) {
    1143                 :        182 :                 EAL_LOG(INFO, "VFIO support initialized");
    1144                 :        182 :                 default_vfio_cfg->vfio_enabled = 1;
    1145                 :            :         } else {
    1146                 :          1 :                 EAL_LOG(NOTICE, "VFIO support could not be initialized");
    1147                 :            :         }
    1148                 :            : 
    1149                 :            :         return 0;
    1150                 :            : }
    1151                 :            : 
    1152                 :            : RTE_EXPORT_SYMBOL(rte_vfio_is_enabled)
    1153                 :            : int
    1154                 :          0 : rte_vfio_is_enabled(const char *modname)
    1155                 :            : {
    1156                 :          0 :         const int mod_available = rte_eal_check_module(modname) > 0;
    1157   [ #  #  #  # ]:          0 :         return default_vfio_cfg->vfio_enabled && mod_available;
    1158                 :            : }
    1159                 :            : 
    1160                 :            : int
    1161                 :         53 : vfio_get_default_container_fd(void)
    1162                 :            : {
    1163                 :            :         struct rte_mp_msg mp_req, *mp_rep;
    1164                 :         53 :         struct rte_mp_reply mp_reply = {0};
    1165                 :         53 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
    1166                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
    1167                 :            :         int container_fd;
    1168                 :            :         const struct internal_config *internal_conf =
    1169                 :         53 :                 eal_get_internal_configuration();
    1170                 :            : 
    1171         [ +  + ]:         53 :         if (default_vfio_cfg->vfio_enabled)
    1172                 :         26 :                 return default_vfio_cfg->vfio_container_fd;
    1173                 :            : 
    1174         [ +  - ]:         27 :         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
    1175                 :            :                 /* if we were secondary process we would try requesting
    1176                 :            :                  * container fd from the primary, but we're the primary
    1177                 :            :                  * process so just exit here
    1178                 :            :                  */
    1179                 :            :                 return -1;
    1180                 :            :         }
    1181                 :            : 
    1182                 :         27 :         p->req = SOCKET_REQ_DEFAULT_CONTAINER;
    1183                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
    1184                 :         27 :         mp_req.len_param = sizeof(*p);
    1185                 :         27 :         mp_req.num_fds = 0;
    1186                 :            : 
    1187         [ +  + ]:         27 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
    1188         [ +  - ]:         26 :             mp_reply.nb_received == 1) {
    1189                 :         26 :                 mp_rep = &mp_reply.msgs[0];
    1190                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
    1191   [ +  -  +  - ]:         26 :                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
    1192                 :         26 :                         container_fd = mp_rep->fds[0];
    1193                 :         26 :                         free(mp_reply.msgs);
    1194                 :         26 :                         return container_fd;
    1195                 :            :                 }
    1196                 :            :         }
    1197                 :            : 
    1198                 :          1 :         free(mp_reply.msgs);
    1199                 :          1 :         EAL_LOG(ERR, "Cannot request default VFIO container fd");
    1200                 :          1 :         return -1;
    1201                 :            : }
    1202                 :            : 
    1203                 :            : int
    1204                 :          0 : vfio_get_iommu_type(void)
    1205                 :            : {
    1206         [ #  # ]:          0 :         if (default_vfio_cfg->vfio_iommu_type == NULL)
    1207                 :            :                 return -1;
    1208                 :            : 
    1209                 :          0 :         return default_vfio_cfg->vfio_iommu_type->type_id;
    1210                 :            : }
    1211                 :            : 
    1212                 :            : const struct vfio_iommu_type *
    1213                 :          0 : vfio_set_iommu_type(int vfio_container_fd)
    1214                 :            : {
    1215                 :            :         unsigned idx;
    1216         [ #  # ]:          0 :         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
    1217                 :          0 :                 const struct vfio_iommu_type *t = &iommu_types[idx];
    1218                 :            : 
    1219                 :          0 :                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
    1220                 :          0 :                                 t->type_id);
    1221         [ #  # ]:          0 :                 if (!ret) {
    1222                 :          0 :                         EAL_LOG(INFO, "Using IOMMU type %d (%s)",
    1223                 :            :                                         t->type_id, t->name);
    1224                 :          0 :                         return t;
    1225                 :            :                 }
    1226                 :            :                 /* not an error, there may be more supported IOMMU types */
    1227                 :          0 :                 EAL_LOG(DEBUG, "Set IOMMU type %d (%s) failed, error "
    1228                 :            :                                 "%i (%s)", t->type_id, t->name, errno,
    1229                 :            :                                 strerror(errno));
    1230                 :            :         }
    1231                 :            :         /* if we didn't find a suitable IOMMU type, fail */
    1232                 :            :         return NULL;
    1233                 :            : }
    1234                 :            : 
    1235                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vfio_get_device_info, 24.03)
    1236                 :            : int
    1237                 :          0 : rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr,
    1238                 :            :                 int *vfio_dev_fd, struct vfio_device_info *device_info)
    1239                 :            : {
    1240                 :            :         int ret;
    1241                 :            : 
    1242   [ #  #  #  # ]:          0 :         if (device_info == NULL || *vfio_dev_fd < 0)
    1243                 :            :                 return -1;
    1244                 :            : 
    1245         [ #  # ]:          0 :         if (*vfio_dev_fd == 0) {
    1246                 :          0 :                 ret = rte_vfio_setup_device(sysfs_base, dev_addr,
    1247                 :            :                                 vfio_dev_fd, device_info);
    1248         [ #  # ]:          0 :                 if (ret)
    1249                 :          0 :                         return -1;
    1250                 :            :         } else {
    1251                 :          0 :                 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
    1252         [ #  # ]:          0 :                 if (ret) {
    1253                 :          0 :                         EAL_LOG(ERR, "%s cannot get device info, error %i (%s)",
    1254                 :            :                                         dev_addr, errno, strerror(errno));
    1255                 :          0 :                         return -1;
    1256                 :            :                 }
    1257                 :            :         }
    1258                 :            : 
    1259                 :            :         return 0;
    1260                 :            : }
    1261                 :            : 
    1262                 :            : int
    1263                 :        156 : vfio_has_supported_extensions(int vfio_container_fd)
    1264                 :            : {
    1265                 :            :         int ret;
    1266                 :            :         unsigned idx, n_extensions = 0;
    1267         [ +  + ]:        624 :         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
    1268                 :            :                 const struct vfio_iommu_type *t = &iommu_types[idx];
    1269                 :            : 
    1270                 :        468 :                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
    1271                 :        468 :                                 t->type_id);
    1272         [ -  + ]:        468 :                 if (ret < 0) {
    1273                 :          0 :                         EAL_LOG(ERR, "Could not get IOMMU type, error "
    1274                 :            :                                         "%i (%s)", errno, strerror(errno));
    1275                 :          0 :                         close(vfio_container_fd);
    1276                 :          0 :                         return -1;
    1277         [ +  + ]:        468 :                 } else if (ret == 1) {
    1278                 :            :                         /* we found a supported extension */
    1279                 :        156 :                         n_extensions++;
    1280                 :            :                 }
    1281         [ +  + ]:        780 :                 EAL_LOG(DEBUG, "IOMMU type %d (%s) is %s",
    1282                 :            :                                 t->type_id, t->name,
    1283                 :            :                                 ret ? "supported" : "not supported");
    1284                 :            :         }
    1285                 :            : 
    1286                 :            :         /* if we didn't find any supported IOMMU types, fail */
    1287         [ -  + ]:        156 :         if (!n_extensions) {
    1288                 :          0 :                 close(vfio_container_fd);
    1289                 :          0 :                 return -1;
    1290                 :            :         }
    1291                 :            : 
    1292                 :            :         return 0;
    1293                 :            : }
    1294                 :            : 
    1295                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_container_fd)
    1296                 :            : int
    1297                 :        156 : rte_vfio_get_container_fd(void)
    1298                 :            : {
    1299                 :            :         int ret, vfio_container_fd;
    1300                 :            :         struct rte_mp_msg mp_req, *mp_rep;
    1301                 :        156 :         struct rte_mp_reply mp_reply = {0};
    1302                 :        156 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
    1303                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
    1304                 :            :         const struct internal_config *internal_conf =
    1305                 :        156 :                 eal_get_internal_configuration();
    1306                 :            : 
    1307                 :            : 
    1308                 :            :         /* if we're in a primary process, try to open the container */
    1309         [ +  - ]:        156 :         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
    1310                 :            :                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
    1311         [ -  + ]:        156 :                 if (vfio_container_fd < 0) {
    1312                 :          0 :                         EAL_LOG(ERR,
    1313                 :            :                                         "Cannot open VFIO container %s, error "
    1314                 :            :                                         "%i (%s)", VFIO_CONTAINER_PATH,
    1315                 :            :                                         errno, strerror(errno));
    1316                 :          0 :                         return -1;
    1317                 :            :                 }
    1318                 :            : 
    1319                 :            :                 /* check VFIO API version */
    1320                 :        156 :                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
    1321         [ -  + ]:        156 :                 if (ret != VFIO_API_VERSION) {
    1322         [ #  # ]:          0 :                         if (ret < 0)
    1323                 :          0 :                                 EAL_LOG(ERR,
    1324                 :            :                                         "Could not get VFIO API version, error "
    1325                 :            :                                         "%i (%s)", errno, strerror(errno));
    1326                 :            :                         else
    1327                 :          0 :                                 EAL_LOG(ERR, "Unsupported VFIO API version!");
    1328                 :          0 :                         close(vfio_container_fd);
    1329                 :          0 :                         return -1;
    1330                 :            :                 }
    1331                 :            : 
    1332                 :        156 :                 ret = vfio_has_supported_extensions(vfio_container_fd);
    1333         [ -  + ]:        156 :                 if (ret) {
    1334                 :          0 :                         EAL_LOG(ERR,
    1335                 :            :                                 "No supported IOMMU extensions found!");
    1336                 :          0 :                         return -1;
    1337                 :            :                 }
    1338                 :            : 
    1339                 :            :                 return vfio_container_fd;
    1340                 :            :         }
    1341                 :            :         /*
    1342                 :            :          * if we're in a secondary process, request container fd from the
    1343                 :            :          * primary process via mp channel
    1344                 :            :          */
    1345                 :          0 :         p->req = SOCKET_REQ_CONTAINER;
    1346                 :            :         strcpy(mp_req.name, EAL_VFIO_MP);
    1347                 :          0 :         mp_req.len_param = sizeof(*p);
    1348                 :          0 :         mp_req.num_fds = 0;
    1349                 :            : 
    1350                 :            :         vfio_container_fd = -1;
    1351         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
    1352         [ #  # ]:          0 :             mp_reply.nb_received == 1) {
    1353                 :          0 :                 mp_rep = &mp_reply.msgs[0];
    1354                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
    1355   [ #  #  #  # ]:          0 :                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
    1356                 :          0 :                         vfio_container_fd = mp_rep->fds[0];
    1357                 :          0 :                         free(mp_reply.msgs);
    1358                 :          0 :                         return vfio_container_fd;
    1359                 :            :                 }
    1360                 :            :         }
    1361                 :            : 
    1362                 :          0 :         free(mp_reply.msgs);
    1363                 :          0 :         EAL_LOG(ERR, "Cannot request VFIO container fd");
    1364                 :          0 :         return -1;
    1365                 :            : }
    1366                 :            : 
    1367                 :            : RTE_EXPORT_SYMBOL(rte_vfio_get_group_num)
    1368                 :            : int
    1369                 :          0 : rte_vfio_get_group_num(const char *sysfs_base,
    1370                 :            :                 const char *dev_addr, int *iommu_group_num)
    1371                 :            : {
    1372                 :            :         char linkname[PATH_MAX];
    1373                 :            :         char filename[PATH_MAX];
    1374                 :            :         char *tok[16], *group_tok, *end;
    1375                 :            :         int ret;
    1376                 :            : 
    1377                 :            :         memset(linkname, 0, sizeof(linkname));
    1378                 :            :         memset(filename, 0, sizeof(filename));
    1379                 :            : 
    1380                 :            :         /* try to find out IOMMU group for this device */
    1381                 :            :         snprintf(linkname, sizeof(linkname),
    1382                 :            :                          "%s/%s/iommu_group", sysfs_base, dev_addr);
    1383                 :            : 
    1384                 :          0 :         ret = readlink(linkname, filename, sizeof(filename));
    1385                 :            : 
    1386                 :            :         /* if the link doesn't exist, no VFIO for us */
    1387         [ #  # ]:          0 :         if (ret < 0)
    1388                 :            :                 return 0;
    1389                 :            : 
    1390                 :          0 :         ret = rte_strsplit(filename, sizeof(filename),
    1391                 :            :                         tok, RTE_DIM(tok), '/');
    1392                 :            : 
    1393         [ #  # ]:          0 :         if (ret <= 0) {
    1394                 :          0 :                 EAL_LOG(ERR, "%s cannot get IOMMU group", dev_addr);
    1395                 :          0 :                 return -1;
    1396                 :            :         }
    1397                 :            : 
    1398                 :            :         /* IOMMU group is always the last token */
    1399                 :          0 :         errno = 0;
    1400                 :          0 :         group_tok = tok[ret - 1];
    1401                 :          0 :         end = group_tok;
    1402                 :          0 :         *iommu_group_num = strtol(group_tok, &end, 10);
    1403   [ #  #  #  #  :          0 :         if ((end != group_tok && *end != '\0') || errno != 0) {
                   #  # ]
    1404                 :          0 :                 EAL_LOG(ERR, "%s error parsing IOMMU number!", dev_addr);
    1405                 :          0 :                 return -1;
    1406                 :            :         }
    1407                 :            : 
    1408                 :            :         return 1;
    1409                 :            : }
    1410                 :            : 
    1411                 :            : static int
    1412                 :          0 : type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
    1413                 :            :                 void *arg)
    1414                 :            : {
    1415                 :            :         int *vfio_container_fd = arg;
    1416                 :            : 
    1417                 :            :         /* skip external memory that isn't a heap */
    1418   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
    1419                 :            :                 return 0;
    1420                 :            : 
    1421                 :            :         /* skip any segments with invalid IOVA addresses */
    1422         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
    1423                 :            :                 return 0;
    1424                 :            : 
    1425                 :          0 :         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
    1426                 :          0 :                         ms->len, 1);
    1427                 :            : }
    1428                 :            : 
    1429                 :            : static int
    1430                 :          0 : vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
    1431                 :            :                 uint64_t len, int do_map)
    1432                 :            : {
    1433                 :            :         struct vfio_iommu_type1_dma_map dma_map;
    1434                 :            :         struct vfio_iommu_type1_dma_unmap dma_unmap;
    1435                 :            :         int ret;
    1436                 :            : 
    1437         [ #  # ]:          0 :         if (do_map != 0) {
    1438                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
    1439                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
    1440                 :          0 :                 dma_map.vaddr = vaddr;
    1441                 :          0 :                 dma_map.size = len;
    1442                 :          0 :                 dma_map.iova = iova;
    1443                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
    1444                 :            :                                 VFIO_DMA_MAP_FLAG_WRITE;
    1445                 :            : 
    1446                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
    1447         [ #  # ]:          0 :                 if (ret) {
    1448                 :            :                         /**
    1449                 :            :                          * In case the mapping was already done EEXIST will be
    1450                 :            :                          * returned from kernel.
    1451                 :            :                          */
    1452         [ #  # ]:          0 :                         if (errno == EEXIST) {
    1453                 :          0 :                                 EAL_LOG(DEBUG,
    1454                 :            :                                         "Memory segment is already mapped, skipping");
    1455                 :            :                         } else {
    1456                 :          0 :                                 EAL_LOG(ERR,
    1457                 :            :                                         "Cannot set up DMA remapping, error "
    1458                 :            :                                         "%i (%s)", errno, strerror(errno));
    1459                 :          0 :                                 return -1;
    1460                 :            :                         }
    1461                 :            :                 }
    1462                 :            :         } else {
    1463                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
    1464                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
    1465                 :          0 :                 dma_unmap.size = len;
    1466                 :          0 :                 dma_unmap.iova = iova;
    1467                 :            : 
    1468                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
    1469                 :            :                                 &dma_unmap);
    1470         [ #  # ]:          0 :                 if (ret) {
    1471                 :          0 :                         EAL_LOG(ERR, "Cannot clear DMA remapping, error "
    1472                 :            :                                         "%i (%s)", errno, strerror(errno));
    1473                 :          0 :                         return -1;
    1474         [ #  # ]:          0 :                 } else if (dma_unmap.size != len) {
    1475                 :          0 :                         EAL_LOG(ERR, "Unexpected size %"PRIu64
    1476                 :            :                                 " of DMA remapping cleared instead of %"PRIu64,
    1477                 :            :                                 (uint64_t)dma_unmap.size, len);
    1478                 :          0 :                         rte_errno = EIO;
    1479                 :          0 :                         return -1;
    1480                 :            :                 }
    1481                 :            :         }
    1482                 :            : 
    1483                 :            :         return 0;
    1484                 :            : }
    1485                 :            : 
    1486                 :            : static int
    1487                 :          0 : vfio_type1_dma_map(int vfio_container_fd)
    1488                 :            : {
    1489                 :          0 :         return rte_memseg_walk(type1_map, &vfio_container_fd);
    1490                 :            : }
    1491                 :            : 
    1492                 :            : /* Track the size of the statically allocated DMA window for SPAPR */
    1493                 :            : uint64_t spapr_dma_win_len;
    1494                 :            : uint64_t spapr_dma_win_page_sz;
    1495                 :            : 
    1496                 :            : static int
    1497                 :          0 : vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
    1498                 :            :                 uint64_t len, int do_map)
    1499                 :            : {
    1500                 :          0 :         struct vfio_iommu_spapr_register_memory reg = {
    1501                 :            :                 .argsz = sizeof(reg),
    1502                 :            :                 .vaddr = (uintptr_t) vaddr,
    1503                 :            :                 .size = len,
    1504                 :            :                 .flags = 0
    1505                 :            :         };
    1506                 :            :         int ret;
    1507                 :            : 
    1508         [ #  # ]:          0 :         if (do_map != 0) {
    1509                 :            :                 struct vfio_iommu_type1_dma_map dma_map;
    1510                 :            : 
    1511         [ #  # ]:          0 :                 if (iova + len > spapr_dma_win_len) {
    1512                 :          0 :                         EAL_LOG(ERR, "DMA map attempt outside DMA window");
    1513                 :          0 :                         return -1;
    1514                 :            :                 }
    1515                 :            : 
    1516                 :          0 :                 ret = ioctl(vfio_container_fd,
    1517                 :            :                                 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
    1518         [ #  # ]:          0 :                 if (ret) {
    1519                 :          0 :                         EAL_LOG(ERR,
    1520                 :            :                                 "Cannot register vaddr for IOMMU, error "
    1521                 :            :                                 "%i (%s)", errno, strerror(errno));
    1522                 :          0 :                         return -1;
    1523                 :            :                 }
    1524                 :            : 
    1525                 :            :                 memset(&dma_map, 0, sizeof(dma_map));
    1526                 :          0 :                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
    1527                 :          0 :                 dma_map.vaddr = vaddr;
    1528                 :          0 :                 dma_map.size = len;
    1529                 :          0 :                 dma_map.iova = iova;
    1530                 :          0 :                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
    1531                 :            :                                 VFIO_DMA_MAP_FLAG_WRITE;
    1532                 :            : 
    1533                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
    1534         [ #  # ]:          0 :                 if (ret) {
    1535                 :          0 :                         EAL_LOG(ERR, "Cannot map vaddr for IOMMU, error "
    1536                 :            :                                         "%i (%s)", errno, strerror(errno));
    1537                 :          0 :                         return -1;
    1538                 :            :                 }
    1539                 :            : 
    1540                 :            :         } else {
    1541                 :            :                 struct vfio_iommu_type1_dma_map dma_unmap;
    1542                 :            : 
    1543                 :            :                 memset(&dma_unmap, 0, sizeof(dma_unmap));
    1544                 :          0 :                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
    1545                 :          0 :                 dma_unmap.size = len;
    1546                 :          0 :                 dma_unmap.iova = iova;
    1547                 :            : 
    1548                 :          0 :                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
    1549                 :            :                                 &dma_unmap);
    1550         [ #  # ]:          0 :                 if (ret) {
    1551                 :          0 :                         EAL_LOG(ERR, "Cannot unmap vaddr for IOMMU, error "
    1552                 :            :                                         "%i (%s)", errno, strerror(errno));
    1553                 :          0 :                         return -1;
    1554                 :            :                 }
    1555                 :            : 
    1556                 :          0 :                 ret = ioctl(vfio_container_fd,
    1557                 :            :                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
    1558         [ #  # ]:          0 :                 if (ret) {
    1559                 :          0 :                         EAL_LOG(ERR,
    1560                 :            :                                 "Cannot unregister vaddr for IOMMU, error "
    1561                 :            :                                 "%i (%s)", errno, strerror(errno));
    1562                 :          0 :                         return -1;
    1563                 :            :                 }
    1564                 :            :         }
    1565                 :            : 
    1566                 :            :         return ret;
    1567                 :            : }
    1568                 :            : 
    1569                 :            : static int
    1570                 :          0 : vfio_spapr_map_walk(const struct rte_memseg_list *msl,
    1571                 :            :                 const struct rte_memseg *ms, void *arg)
    1572                 :            : {
    1573                 :            :         int *vfio_container_fd = arg;
    1574                 :            : 
    1575                 :            :         /* skip external memory that isn't a heap */
    1576   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
    1577                 :            :                 return 0;
    1578                 :            : 
    1579                 :            :         /* skip any segments with invalid IOVA addresses */
    1580         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
    1581                 :            :                 return 0;
    1582                 :            : 
    1583                 :          0 :         return vfio_spapr_dma_do_map(*vfio_container_fd,
    1584                 :          0 :                 ms->addr_64, ms->iova, ms->len, 1);
    1585                 :            : }
    1586                 :            : 
    1587                 :            : struct spapr_size_walk_param {
    1588                 :            :         uint64_t max_va;
    1589                 :            :         uint64_t page_sz;
    1590                 :            :         bool is_user_managed;
    1591                 :            : };
    1592                 :            : 
    1593                 :            : /*
    1594                 :            :  * In order to set the DMA window size required for the SPAPR IOMMU
    1595                 :            :  * we need to walk the existing virtual memory allocations as well as
    1596                 :            :  * find the hugepage size used.
    1597                 :            :  */
    1598                 :            : static int
    1599                 :          0 : vfio_spapr_size_walk(const struct rte_memseg_list *msl, void *arg)
    1600                 :            : {
    1601                 :            :         struct spapr_size_walk_param *param = arg;
    1602                 :          0 :         uint64_t max = (uint64_t) msl->base_va + (uint64_t) msl->len;
    1603                 :            : 
    1604   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap) {
    1605                 :            :                 /* ignore user managed external memory */
    1606                 :          0 :                 param->is_user_managed = true;
    1607                 :          0 :                 return 0;
    1608                 :            :         }
    1609                 :            : 
    1610         [ #  # ]:          0 :         if (max > param->max_va) {
    1611                 :          0 :                 param->page_sz = msl->page_sz;
    1612                 :          0 :                 param->max_va = max;
    1613                 :            :         }
    1614                 :            : 
    1615                 :            :         return 0;
    1616                 :            : }
    1617                 :            : 
    1618                 :            : /*
    1619                 :            :  * Find the highest memory address used in physical or virtual address
    1620                 :            :  * space and use that as the top of the DMA window.
    1621                 :            :  */
    1622                 :            : static int
    1623                 :          0 : find_highest_mem_addr(struct spapr_size_walk_param *param)
    1624                 :            : {
    1625                 :            :         /* find the maximum IOVA address for setting the DMA window size */
    1626         [ #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_PA) {
    1627                 :            :                 static const char proc_iomem[] = "/proc/iomem";
    1628                 :            :                 static const char str_sysram[] = "System RAM";
    1629                 :            :                 uint64_t start, end, max = 0;
    1630                 :          0 :                 char *line = NULL;
    1631                 :            :                 char *dash, *space;
    1632                 :            :                 size_t line_len;
    1633                 :            : 
    1634                 :            :                 /*
    1635                 :            :                  * Example "System RAM" in /proc/iomem:
    1636                 :            :                  * 00000000-1fffffffff : System RAM
    1637                 :            :                  * 200000000000-201fffffffff : System RAM
    1638                 :            :                  */
    1639                 :          0 :                 FILE *fd = fopen(proc_iomem, "r");
    1640         [ #  # ]:          0 :                 if (fd == NULL) {
    1641                 :          0 :                         EAL_LOG(ERR, "Cannot open %s", proc_iomem);
    1642                 :          0 :                         return -1;
    1643                 :            :                 }
    1644                 :            :                 /* Scan /proc/iomem for the highest PA in the system */
    1645         [ #  # ]:          0 :                 while (getline(&line, &line_len, fd) != -1) {
    1646         [ #  # ]:          0 :                         if (strstr(line, str_sysram) == NULL)
    1647                 :          0 :                                 continue;
    1648                 :            : 
    1649                 :          0 :                         space = strstr(line, " ");
    1650                 :          0 :                         dash = strstr(line, "-");
    1651                 :            : 
    1652                 :            :                         /* Validate the format of the memory string */
    1653   [ #  #  #  # ]:          0 :                         if (space == NULL || dash == NULL || space < dash) {
    1654                 :          0 :                                 EAL_LOG(ERR, "Can't parse line \"%s\" in file %s",
    1655                 :            :                                         line, proc_iomem);
    1656                 :          0 :                                 continue;
    1657                 :            :                         }
    1658                 :            : 
    1659                 :          0 :                         start = strtoull(line, NULL, 16);
    1660                 :          0 :                         end   = strtoull(dash + 1, NULL, 16);
    1661                 :          0 :                         EAL_LOG(DEBUG, "Found system RAM from 0x%" PRIx64
    1662                 :            :                                 " to 0x%" PRIx64, start, end);
    1663                 :            :                         if (end > max)
    1664                 :            :                                 max = end;
    1665                 :            :                 }
    1666                 :          0 :                 free(line);
    1667                 :          0 :                 fclose(fd);
    1668                 :            : 
    1669         [ #  # ]:          0 :                 if (max == 0) {
    1670                 :          0 :                         EAL_LOG(ERR, "Failed to find valid \"System RAM\" "
    1671                 :            :                                 "entry in file %s", proc_iomem);
    1672                 :          0 :                         return -1;
    1673                 :            :                 }
    1674                 :            : 
    1675                 :          0 :                 spapr_dma_win_len = rte_align64pow2(max + 1);
    1676                 :          0 :                 return 0;
    1677         [ #  # ]:          0 :         } else if (rte_eal_iova_mode() == RTE_IOVA_VA) {
    1678                 :          0 :                 EAL_LOG(DEBUG, "Highest VA address in memseg list is 0x%"
    1679                 :            :                         PRIx64, param->max_va);
    1680                 :          0 :                 spapr_dma_win_len = rte_align64pow2(param->max_va);
    1681                 :          0 :                 return 0;
    1682                 :            :         }
    1683                 :            : 
    1684                 :          0 :         spapr_dma_win_len = 0;
    1685                 :          0 :         EAL_LOG(ERR, "Unsupported IOVA mode");
    1686                 :          0 :         return -1;
    1687                 :            : }
    1688                 :            : 
    1689                 :            : 
    1690                 :            : /*
    1691                 :            :  * The SPAPRv2 IOMMU supports 2 DMA windows with starting
    1692                 :            :  * address at 0 or 1<<59.  By default, a DMA window is set
    1693                 :            :  * at address 0, 2GB long, with a 4KB page.  For DPDK we
    1694                 :            :  * must remove the default window and setup a new DMA window
    1695                 :            :  * based on the hugepage size and memory requirements of
    1696                 :            :  * the application before we can map memory for DMA.
    1697                 :            :  */
    1698                 :            : static int
    1699                 :          0 : spapr_dma_win_size(void)
    1700                 :            : {
    1701                 :            :         struct spapr_size_walk_param param;
    1702                 :            : 
    1703                 :            :         /* only create DMA window once */
    1704         [ #  # ]:          0 :         if (spapr_dma_win_len > 0)
    1705                 :            :                 return 0;
    1706                 :            : 
    1707                 :            :         /* walk the memseg list to find the page size/max VA address */
    1708                 :            :         memset(&param, 0, sizeof(param));
    1709         [ #  # ]:          0 :         if (rte_memseg_list_walk(vfio_spapr_size_walk, &param) < 0) {
    1710                 :          0 :                 EAL_LOG(ERR, "Failed to walk memseg list for DMA window size");
    1711                 :          0 :                 return -1;
    1712                 :            :         }
    1713                 :            : 
    1714                 :            :         /* we can't be sure if DMA window covers external memory */
    1715         [ #  # ]:          0 :         if (param.is_user_managed)
    1716                 :          0 :                 EAL_LOG(WARNING, "Detected user managed external memory which may not be managed by the IOMMU");
    1717                 :            : 
    1718                 :            :         /* check physical/virtual memory size */
    1719         [ #  # ]:          0 :         if (find_highest_mem_addr(&param) < 0)
    1720                 :            :                 return -1;
    1721                 :          0 :         EAL_LOG(DEBUG, "Setting DMA window size to 0x%" PRIx64,
    1722                 :            :                 spapr_dma_win_len);
    1723                 :          0 :         spapr_dma_win_page_sz = param.page_sz;
    1724                 :          0 :         rte_mem_set_dma_mask(rte_ctz64(spapr_dma_win_len));
    1725                 :          0 :         return 0;
    1726                 :            : }
    1727                 :            : 
    1728                 :            : static int
    1729                 :          0 : vfio_spapr_create_dma_window(int vfio_container_fd)
    1730                 :            : {
    1731                 :          0 :         struct vfio_iommu_spapr_tce_create create = {
    1732                 :            :                 .argsz = sizeof(create), };
    1733                 :          0 :         struct vfio_iommu_spapr_tce_remove remove = {
    1734                 :            :                 .argsz = sizeof(remove), };
    1735                 :          0 :         struct vfio_iommu_spapr_tce_info info = {
    1736                 :            :                 .argsz = sizeof(info), };
    1737                 :            :         int ret;
    1738                 :            : 
    1739                 :          0 :         ret = spapr_dma_win_size();
    1740         [ #  # ]:          0 :         if (ret < 0)
    1741                 :            :                 return ret;
    1742                 :            : 
    1743                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
    1744         [ #  # ]:          0 :         if (ret) {
    1745                 :          0 :                 EAL_LOG(ERR, "Cannot get IOMMU info, error %i (%s)",
    1746                 :            :                         errno, strerror(errno));
    1747                 :          0 :                 return -1;
    1748                 :            :         }
    1749                 :            : 
    1750                 :            :         /*
    1751                 :            :          * sPAPR v1/v2 IOMMU always has a default 1G DMA window set.  The window
    1752                 :            :          * can't be changed for v1 but it can be changed for v2. Since DPDK only
    1753                 :            :          * supports v2, remove the default DMA window so it can be resized.
    1754                 :            :          */
    1755                 :          0 :         remove.start_addr = info.dma32_window_start;
    1756                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
    1757         [ #  # ]:          0 :         if (ret)
    1758                 :            :                 return -1;
    1759                 :            : 
    1760                 :            :         /* create a new DMA window (start address is not selectable) */
    1761                 :          0 :         create.window_size = spapr_dma_win_len;
    1762                 :          0 :         create.page_shift  = rte_ctz64(spapr_dma_win_page_sz);
    1763                 :          0 :         create.levels = 1;
    1764                 :          0 :         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
    1765                 :            : #ifdef VFIO_IOMMU_SPAPR_INFO_DDW
    1766                 :            :         /*
    1767                 :            :          * The vfio_iommu_spapr_tce_info structure was modified in
    1768                 :            :          * Linux kernel 4.2.0 to add support for the
    1769                 :            :          * vfio_iommu_spapr_tce_ddw_info structure needed to try
    1770                 :            :          * multiple table levels.  Skip the attempt if running with
    1771                 :            :          * an older kernel.
    1772                 :            :          */
    1773         [ #  # ]:          0 :         if (ret) {
    1774                 :            :                 /* if at first we don't succeed, try more levels */
    1775                 :            :                 uint32_t levels;
    1776                 :            : 
    1777                 :          0 :                 for (levels = create.levels + 1;
    1778   [ #  #  #  # ]:          0 :                         ret && levels <= info.ddw.levels; levels++) {
    1779                 :          0 :                         create.levels = levels;
    1780                 :          0 :                         ret = ioctl(vfio_container_fd,
    1781                 :            :                                 VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
    1782                 :            :                 }
    1783                 :            :         }
    1784                 :            : #endif /* VFIO_IOMMU_SPAPR_INFO_DDW */
    1785         [ #  # ]:          0 :         if (ret) {
    1786                 :          0 :                 EAL_LOG(ERR, "Cannot create new DMA window, error "
    1787                 :            :                                 "%i (%s)", errno, strerror(errno));
    1788                 :          0 :                 EAL_LOG(ERR,
    1789                 :            :                         "Consider using a larger hugepage size if supported by the system");
    1790                 :          0 :                 return -1;
    1791                 :            :         }
    1792                 :            : 
    1793                 :            :         /* verify the start address  */
    1794         [ #  # ]:          0 :         if (create.start_addr != 0) {
    1795                 :          0 :                 EAL_LOG(ERR, "Received unsupported start address 0x%"
    1796                 :            :                         PRIx64, (uint64_t)create.start_addr);
    1797                 :          0 :                 return -1;
    1798                 :            :         }
    1799                 :            :         return ret;
    1800                 :            : }
    1801                 :            : 
    1802                 :            : static int
    1803                 :          0 : vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr,
    1804                 :            :                 uint64_t iova, uint64_t len, int do_map)
    1805                 :            : {
    1806                 :            :         int ret = 0;
    1807                 :            : 
    1808         [ #  # ]:          0 :         if (do_map) {
    1809         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(vfio_container_fd,
    1810                 :            :                         vaddr, iova, len, 1)) {
    1811                 :          0 :                         EAL_LOG(ERR, "Failed to map DMA");
    1812                 :            :                         ret = -1;
    1813                 :            :                 }
    1814                 :            :         } else {
    1815         [ #  # ]:          0 :                 if (vfio_spapr_dma_do_map(vfio_container_fd,
    1816                 :            :                         vaddr, iova, len, 0)) {
    1817                 :          0 :                         EAL_LOG(ERR, "Failed to unmap DMA");
    1818                 :            :                         ret = -1;
    1819                 :            :                 }
    1820                 :            :         }
    1821                 :            : 
    1822                 :          0 :         return ret;
    1823                 :            : }
    1824                 :            : 
    1825                 :            : static int
    1826                 :          0 : vfio_spapr_dma_map(int vfio_container_fd)
    1827                 :            : {
    1828         [ #  # ]:          0 :         if (vfio_spapr_create_dma_window(vfio_container_fd) < 0) {
    1829                 :          0 :                 EAL_LOG(ERR, "Could not create new DMA window!");
    1830                 :          0 :                 return -1;
    1831                 :            :         }
    1832                 :            : 
    1833                 :            :         /* map all existing DPDK segments for DMA */
    1834         [ #  # ]:          0 :         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
    1835                 :          0 :                 return -1;
    1836                 :            : 
    1837                 :            :         return 0;
    1838                 :            : }
    1839                 :            : 
    1840                 :            : static int
    1841                 :          0 : vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
    1842                 :            : {
    1843                 :            :         /* No-IOMMU mode does not need DMA mapping */
    1844                 :          0 :         return 0;
    1845                 :            : }
    1846                 :            : 
    1847                 :            : static int
    1848                 :          0 : vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
    1849                 :            :                          uint64_t __rte_unused vaddr,
    1850                 :            :                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
    1851                 :            :                          int __rte_unused do_map)
    1852                 :            : {
    1853                 :            :         /* No-IOMMU mode does not need DMA mapping */
    1854                 :          0 :         return 0;
    1855                 :            : }
    1856                 :            : 
    1857                 :            : static int
    1858                 :          0 : vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1859                 :            :                 uint64_t len, int do_map)
    1860                 :            : {
    1861                 :          0 :         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
    1862                 :            : 
    1863         [ #  # ]:          0 :         if (!t) {
    1864                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1865                 :          0 :                 rte_errno = ENODEV;
    1866                 :          0 :                 return -1;
    1867                 :            :         }
    1868                 :            : 
    1869         [ #  # ]:          0 :         if (!t->dma_user_map_func) {
    1870                 :          0 :                 EAL_LOG(ERR,
    1871                 :            :                         "VFIO custom DMA region mapping not supported by IOMMU %s",
    1872                 :            :                         t->name);
    1873                 :          0 :                 rte_errno = ENOTSUP;
    1874                 :          0 :                 return -1;
    1875                 :            :         }
    1876                 :            : 
    1877                 :          0 :         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
    1878                 :            :                         len, do_map);
    1879                 :            : }
    1880                 :            : 
    1881                 :            : static int
    1882                 :          0 : container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1883                 :            :                 uint64_t len)
    1884                 :            : {
    1885                 :            :         struct user_mem_map *new_map;
    1886                 :            :         struct user_mem_maps *user_mem_maps;
    1887                 :            :         bool has_partial_unmap;
    1888                 :            :         int ret = 0;
    1889                 :            : 
    1890                 :          0 :         user_mem_maps = &vfio_cfg->mem_maps;
    1891                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1892         [ #  # ]:          0 :         if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
    1893                 :          0 :                 EAL_LOG(ERR, "No more space for user mem maps");
    1894                 :          0 :                 rte_errno = ENOMEM;
    1895                 :            :                 ret = -1;
    1896                 :          0 :                 goto out;
    1897                 :            :         }
    1898                 :            :         /* map the entry */
    1899         [ #  # ]:          0 :         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
    1900                 :            :                 /* technically, this will fail if there are currently no devices
    1901                 :            :                  * plugged in, even if a device were added later, this mapping
    1902                 :            :                  * might have succeeded. however, since we cannot verify if this
    1903                 :            :                  * is a valid mapping without having a device attached, consider
    1904                 :            :                  * this to be unsupported, because we can't just store any old
    1905                 :            :                  * mapping and pollute list of active mappings willy-nilly.
    1906                 :            :                  */
    1907                 :          0 :                 EAL_LOG(ERR, "Couldn't map new region for DMA");
    1908                 :            :                 ret = -1;
    1909                 :          0 :                 goto out;
    1910                 :            :         }
    1911                 :            :         /* do we have partial unmap support? */
    1912                 :          0 :         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
    1913                 :            : 
    1914                 :            :         /* create new user mem map entry */
    1915                 :          0 :         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
    1916                 :          0 :         new_map->addr = vaddr;
    1917                 :          0 :         new_map->iova = iova;
    1918                 :          0 :         new_map->len = len;
    1919                 :            :         /* for IOMMU types supporting partial unmap, we don't need chunking */
    1920         [ #  # ]:          0 :         new_map->chunk = has_partial_unmap ? 0 : len;
    1921                 :            : 
    1922                 :          0 :         compact_user_maps(user_mem_maps);
    1923         [ #  # ]:          0 : out:
    1924                 :            :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    1925                 :          0 :         return ret;
    1926                 :            : }
    1927                 :            : 
    1928                 :            : static int
    1929                 :          0 : container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
    1930                 :            :                 uint64_t len)
    1931                 :            : {
    1932                 :            :         struct user_mem_map orig_maps[VFIO_MAX_USER_MEM_MAPS];
    1933                 :            :         struct user_mem_map new_maps[2]; /* can be at most 2 */
    1934                 :            :         struct user_mem_maps *user_mem_maps;
    1935                 :            :         int n_orig, n_new, newlen, ret = 0;
    1936                 :            :         bool has_partial_unmap;
    1937                 :            : 
    1938                 :          0 :         user_mem_maps = &vfio_cfg->mem_maps;
    1939                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1940                 :            : 
    1941                 :            :         /*
    1942                 :            :          * Previously, we had adjacent mappings entirely contained within one
    1943                 :            :          * mapping entry. Since we now store original mapping length in some
    1944                 :            :          * cases, this is no longer the case, so unmapping can potentially go
    1945                 :            :          * over multiple segments and split them in any number of ways.
    1946                 :            :          *
    1947                 :            :          * To complicate things further, some IOMMU types support arbitrary
    1948                 :            :          * partial unmapping, while others will only support unmapping along the
    1949                 :            :          * chunk size, so there are a lot of cases we need to handle. To make
    1950                 :            :          * things easier code wise, instead of trying to adjust existing
    1951                 :            :          * mappings, let's just rebuild them using information we have.
    1952                 :            :          */
    1953                 :            : 
    1954                 :            :         /*
    1955                 :            :          * first thing to do is check if there exists a mapping that includes
    1956                 :            :          * the start and the end of our requested unmap. We need to collect all
    1957                 :            :          * maps that include our unmapped region.
    1958                 :            :          */
    1959                 :          0 :         n_orig = find_user_mem_maps(user_mem_maps, vaddr, iova, len,
    1960                 :            :                         orig_maps, RTE_DIM(orig_maps));
    1961                 :            :         /* did we find anything? */
    1962         [ #  # ]:          0 :         if (n_orig < 0) {
    1963                 :          0 :                 EAL_LOG(ERR, "Couldn't find previously mapped region");
    1964                 :          0 :                 rte_errno = EINVAL;
    1965                 :            :                 ret = -1;
    1966                 :          0 :                 goto out;
    1967                 :            :         }
    1968                 :            : 
    1969                 :            :         /* do we have partial unmap capability? */
    1970                 :          0 :         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
    1971                 :            : 
    1972                 :            :         /*
    1973                 :            :          * if we don't support partial unmap, we must check if start and end of
    1974                 :            :          * current unmap region are chunk-aligned.
    1975                 :            :          */
    1976         [ #  # ]:          0 :         if (!has_partial_unmap) {
    1977                 :            :                 bool start_aligned, end_aligned;
    1978                 :            : 
    1979                 :          0 :                 start_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
    1980                 :            :                                 vaddr, iova);
    1981                 :          0 :                 end_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
    1982                 :            :                                 vaddr + len, iova + len);
    1983                 :            : 
    1984         [ #  # ]:          0 :                 if (!start_aligned || !end_aligned) {
    1985                 :          0 :                         EAL_LOG(DEBUG, "DMA partial unmap unsupported");
    1986                 :          0 :                         rte_errno = ENOTSUP;
    1987                 :            :                         ret = -1;
    1988                 :          0 :                         goto out;
    1989                 :            :                 }
    1990                 :            :         }
    1991                 :            : 
    1992                 :            :         /*
    1993                 :            :          * now we know we can potentially unmap the region, but we still have to
    1994                 :            :          * figure out if there is enough space in our list to store remaining
    1995                 :            :          * maps. for this, we will figure out how many segments we are going to
    1996                 :            :          * remove, and how many new segments we are going to create.
    1997                 :            :          */
    1998                 :          0 :         n_new = process_maps(orig_maps, n_orig, new_maps, vaddr, len);
    1999                 :            : 
    2000                 :            :         /* can we store the new maps in our list? */
    2001                 :          0 :         newlen = (user_mem_maps->n_maps - n_orig) + n_new;
    2002         [ #  # ]:          0 :         if (newlen >= VFIO_MAX_USER_MEM_MAPS) {
    2003                 :          0 :                 EAL_LOG(ERR, "Not enough space to store partial mapping");
    2004                 :          0 :                 rte_errno = ENOMEM;
    2005                 :            :                 ret = -1;
    2006                 :          0 :                 goto out;
    2007                 :            :         }
    2008                 :            : 
    2009                 :            :         /* unmap the entry */
    2010         [ #  # ]:          0 :         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
    2011                 :            :                 /* there may not be any devices plugged in, so unmapping will
    2012                 :            :                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
    2013                 :            :                  * stop us from removing the mapping, as the assumption is we
    2014                 :            :                  * won't be needing this memory any more and thus will want to
    2015                 :            :                  * prevent it from being remapped again on hotplug. so, only
    2016                 :            :                  * fail if we indeed failed to unmap (e.g. if the mapping was
    2017                 :            :                  * within our mapped range but had invalid alignment).
    2018                 :            :                  */
    2019         [ #  # ]:          0 :                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
    2020                 :          0 :                         EAL_LOG(ERR, "Couldn't unmap region for DMA");
    2021                 :            :                         ret = -1;
    2022                 :          0 :                         goto out;
    2023                 :            :                 } else {
    2024                 :          0 :                         EAL_LOG(DEBUG, "DMA unmapping failed, but removing mappings anyway");
    2025                 :            :                 }
    2026                 :            :         }
    2027                 :            : 
    2028                 :            :         /* we have unmapped the region, so now update the maps */
    2029                 :          0 :         delete_maps(user_mem_maps, orig_maps, n_orig);
    2030                 :          0 :         copy_maps(user_mem_maps, new_maps, n_new);
    2031                 :          0 :         compact_user_maps(user_mem_maps);
    2032         [ #  # ]:          0 : out:
    2033                 :            :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    2034                 :          0 :         return ret;
    2035                 :            : }
    2036                 :            : 
    2037                 :            : RTE_EXPORT_SYMBOL(rte_vfio_noiommu_is_enabled)
    2038                 :            : int
    2039                 :          0 : rte_vfio_noiommu_is_enabled(void)
    2040                 :            : {
    2041                 :            :         int fd;
    2042                 :            :         ssize_t cnt;
    2043                 :            :         char c;
    2044                 :            : 
    2045                 :            :         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
    2046         [ #  # ]:          0 :         if (fd < 0) {
    2047         [ #  # ]:          0 :                 if (errno != ENOENT) {
    2048                 :          0 :                         EAL_LOG(ERR, "Cannot open VFIO noiommu file "
    2049                 :            :                                         "%i (%s)", errno, strerror(errno));
    2050                 :          0 :                         return -1;
    2051                 :            :                 }
    2052                 :            :                 /*
    2053                 :            :                  * else the file does not exists
    2054                 :            :                  * i.e. noiommu is not enabled
    2055                 :            :                  */
    2056                 :            :                 return 0;
    2057                 :            :         }
    2058                 :            : 
    2059                 :            :         cnt = read(fd, &c, 1);
    2060                 :          0 :         close(fd);
    2061         [ #  # ]:          0 :         if (cnt != 1) {
    2062                 :          0 :                 EAL_LOG(ERR, "Unable to read from VFIO noiommu file "
    2063                 :            :                                 "%i (%s)", errno, strerror(errno));
    2064                 :          0 :                 return -1;
    2065                 :            :         }
    2066                 :            : 
    2067                 :          0 :         return c == 'Y';
    2068                 :            : }
    2069                 :            : 
    2070                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_create)
    2071                 :            : int
    2072                 :          0 : rte_vfio_container_create(void)
    2073                 :            : {
    2074                 :            :         int i;
    2075                 :            : 
    2076                 :            :         /* Find an empty slot to store new vfio config */
    2077         [ #  # ]:          0 :         for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
    2078         [ #  # ]:          0 :                 if (vfio_cfgs[i].vfio_container_fd == -1)
    2079                 :            :                         break;
    2080                 :            :         }
    2081                 :            : 
    2082         [ #  # ]:          0 :         if (i == VFIO_MAX_CONTAINERS) {
    2083                 :          0 :                 EAL_LOG(ERR, "Exceed max VFIO container limit");
    2084                 :          0 :                 return -1;
    2085                 :            :         }
    2086                 :            : 
    2087                 :          0 :         vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
    2088         [ #  # ]:          0 :         if (vfio_cfgs[i].vfio_container_fd < 0) {
    2089                 :          0 :                 EAL_LOG(NOTICE, "Fail to create a new VFIO container");
    2090                 :          0 :                 return -1;
    2091                 :            :         }
    2092                 :            : 
    2093                 :            :         return vfio_cfgs[i].vfio_container_fd;
    2094                 :            : }
    2095                 :            : 
    2096                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_destroy)
    2097                 :            : int
    2098         [ #  # ]:          0 : rte_vfio_container_destroy(int container_fd)
    2099                 :            : {
    2100                 :            :         struct vfio_config *vfio_cfg;
    2101                 :            :         int i;
    2102                 :            : 
    2103                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2104         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2105                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2106                 :          0 :                 return -1;
    2107                 :            :         }
    2108                 :            : 
    2109         [ #  # ]:          0 :         for (i = 0; i < VFIO_MAX_GROUPS; i++)
    2110         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num != -1)
    2111                 :          0 :                         rte_vfio_container_group_unbind(container_fd,
    2112                 :            :                                 vfio_cfg->vfio_groups[i].group_num);
    2113                 :            : 
    2114                 :          0 :         close(container_fd);
    2115                 :          0 :         vfio_cfg->vfio_container_fd = -1;
    2116                 :          0 :         vfio_cfg->vfio_active_groups = 0;
    2117                 :          0 :         vfio_cfg->vfio_iommu_type = NULL;
    2118                 :            : 
    2119                 :          0 :         return 0;
    2120                 :            : }
    2121                 :            : 
    2122                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_group_bind)
    2123                 :            : int
    2124         [ #  # ]:          0 : rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
    2125                 :            : {
    2126                 :            :         struct vfio_config *vfio_cfg;
    2127                 :            : 
    2128                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2129         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2130                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2131                 :          0 :                 return -1;
    2132                 :            :         }
    2133                 :            : 
    2134                 :          0 :         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
    2135                 :            : }
    2136                 :            : 
    2137                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_group_unbind)
    2138                 :            : int
    2139         [ #  # ]:          0 : rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
    2140                 :            : {
    2141                 :            :         struct vfio_config *vfio_cfg;
    2142                 :            :         struct vfio_group *cur_grp = NULL;
    2143                 :            :         int i;
    2144                 :            : 
    2145                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2146         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2147                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2148                 :          0 :                 return -1;
    2149                 :            :         }
    2150                 :            : 
    2151         [ #  # ]:          0 :         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
    2152         [ #  # ]:          0 :                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
    2153                 :          0 :                         cur_grp = &vfio_cfg->vfio_groups[i];
    2154                 :          0 :                         break;
    2155                 :            :                 }
    2156                 :            :         }
    2157                 :            : 
    2158                 :            :         /* This should not happen */
    2159         [ #  # ]:          0 :         if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
    2160                 :          0 :                 EAL_LOG(ERR, "Specified VFIO group number not found");
    2161                 :          0 :                 return -1;
    2162                 :            :         }
    2163                 :            : 
    2164   [ #  #  #  # ]:          0 :         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
    2165                 :          0 :                 EAL_LOG(ERR,
    2166                 :            :                         "Error when closing vfio_group_fd for iommu_group_num "
    2167                 :            :                         "%d", iommu_group_num);
    2168                 :          0 :                 return -1;
    2169                 :            :         }
    2170                 :          0 :         cur_grp->group_num = -1;
    2171                 :          0 :         cur_grp->fd = -1;
    2172                 :          0 :         cur_grp->devices = 0;
    2173                 :          0 :         vfio_cfg->vfio_active_groups--;
    2174                 :            : 
    2175                 :          0 :         return 0;
    2176                 :            : }
    2177                 :            : 
    2178                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_dma_map)
    2179                 :            : int
    2180                 :          0 : rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
    2181                 :            :                 uint64_t len)
    2182                 :            : {
    2183                 :            :         struct vfio_config *vfio_cfg;
    2184                 :            : 
    2185         [ #  # ]:          0 :         if (len == 0) {
    2186                 :          0 :                 rte_errno = EINVAL;
    2187                 :          0 :                 return -1;
    2188                 :            :         }
    2189                 :            : 
    2190                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2191         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2192                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2193                 :          0 :                 return -1;
    2194                 :            :         }
    2195                 :            : 
    2196                 :          0 :         return container_dma_map(vfio_cfg, vaddr, iova, len);
    2197                 :            : }
    2198                 :            : 
    2199                 :            : RTE_EXPORT_SYMBOL(rte_vfio_container_dma_unmap)
    2200                 :            : int
    2201                 :          0 : rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
    2202                 :            :                 uint64_t len)
    2203                 :            : {
    2204                 :            :         struct vfio_config *vfio_cfg;
    2205                 :            : 
    2206         [ #  # ]:          0 :         if (len == 0) {
    2207                 :          0 :                 rte_errno = EINVAL;
    2208                 :          0 :                 return -1;
    2209                 :            :         }
    2210                 :            : 
    2211                 :            :         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
    2212         [ #  # ]:          0 :         if (vfio_cfg == NULL) {
    2213                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    2214                 :          0 :                 return -1;
    2215                 :            :         }
    2216                 :            : 
    2217                 :          0 :         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
    2218                 :            : }

Generated by: LCOV version 1.14