LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_vfio.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 23 803 2.9 %
Date: 2026-11-01 19:11:22 Functions: 4 46 8.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 571 2.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <uapi/linux/vfio.h>
       6                 :            : 
       7                 :            : #include <errno.h>
       8                 :            : #include <inttypes.h>
       9                 :            : #include <limits.h>
      10                 :            : #include <string.h>
      11                 :            : #include <fcntl.h>
      12                 :            : #include <sys/stat.h>
      13                 :            : #include <unistd.h>
      14                 :            : #include <sys/ioctl.h>
      15                 :            : #include <dirent.h>
      16                 :            : 
      17                 :            : #include <rte_errno.h>
      18                 :            : #include <rte_log.h>
      19                 :            : #include <rte_memory.h>
      20                 :            : #include <rte_eal_memconfig.h>
      21                 :            : #include <dev_vfio.h>
      22                 :            : 
      23                 :            : #include <eal_export.h>
      24                 :            : #include "eal_filesystem.h"
      25                 :            : #include "eal_memcfg.h"
      26                 :            : #include "eal_vfio.h"
      27                 :            : #include "eal_private.h"
      28                 :            : #include "eal_internal_cfg.h"
      29                 :            : 
      30                 :            : #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
      31                 :            : 
      32                 :            : /*
      33                 :            :  * rte_errno convention:
      34                 :            :  *
      35                 :            :  * - EINVAL: invalid parameters
      36                 :            :  * - ENOTSUP: current mode does not support this operation
      37                 :            :  * - ENXIO: VFIO not initialized
      38                 :            :  * - ENODEV: device not managed by VFIO
      39                 :            :  * - ENOSPC: no space in config
      40                 :            :  * - EEXIST: device already assigned
      41                 :            :  * - ENOENT: group or device not found
      42                 :            :  * - ENOMEM: memory allocation failed
      43                 :            :  * - EIO: underlying VFIO operation failed
      44                 :            :  */
      45                 :            : 
      46                 :            : /* functions can fail for multiple reasons, and errno is tedious */
      47                 :            : enum vfio_result {
      48                 :            :         VFIO_SUCCESS,
      49                 :            :         VFIO_ERROR,
      50                 :            :         VFIO_EXISTS,
      51                 :            :         VFIO_NOT_SUPPORTED,
      52                 :            :         VFIO_NOT_MANAGED,
      53                 :            :         VFIO_NOT_FOUND,
      54                 :            :         VFIO_NO_SPACE,
      55                 :            :         VFIO_NO_MEM,
      56                 :            : };
      57                 :            : 
      58                 :            : /* per-process, per-container data */
      59                 :            : static struct vfio_container vfio_containers[RTE_MAX_VFIO_CONTAINERS] = {0};
      60                 :            : 
      61                 :            : #define VFIO_CONTAINER_FOREACH(cfg) \
      62                 :            :         for ((cfg) = &vfio_containers[0]; \
      63                 :            :                 (cfg) < &vfio_containers[RTE_DIM(vfio_containers)]; \
      64                 :            :                 (cfg)++)
      65                 :            : 
      66                 :            : #define VFIO_CONTAINER_FOREACH_ACTIVE(cfg) \
      67                 :            :         VFIO_CONTAINER_FOREACH((cfg)) \
      68                 :            :                 if (((cfg)->active))
      69                 :            : 
      70                 :            : /* for containers, we need to initialize the fd, and mem maps lock */
      71                 :            : #define VFIO_CONTAINER_INITIALIZER \
      72                 :            :         ((struct vfio_container){ \
      73                 :            :                 .mem_maps = { .lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER, }, \
      74                 :            :                 .container_fd = -1, \
      75                 :            :         })
      76                 :            : 
      77                 :            : struct vfio_config vfio_global_cfg = {
      78                 :            :         .mode = DEV_VFIO_MODE_NONE,
      79                 :            :         .iova_mode = DEV_VFIO_IOVA_MODE_UNKNOWN,
      80                 :            :         .default_cfg = &vfio_containers[0]
      81                 :            : };
      82                 :            : 
      83                 :            : static int
      84                 :        229 : vfio_check_module(enum dev_vfio_module module)
      85                 :            : {
      86                 :            :         const char *module_name;
      87                 :            :         char sysfs_mod_name[PATH_MAX];
      88                 :            :         struct stat st;
      89                 :            :         int n;
      90                 :            : 
      91      [ -  +  - ]:        229 :         switch (module) {
      92                 :            :         case DEV_VFIO_MODULE_VFIO:
      93                 :            :                 module_name = "vfio";
      94                 :            :                 break;
      95                 :          0 :         case DEV_VFIO_MODULE_VFIO_PCI:
      96                 :            :                 module_name = "vfio_pci";
      97                 :          0 :                 break;
      98                 :            :         default:
      99                 :            :                 return -1;
     100                 :            :         }
     101                 :            : 
     102                 :            :         /* Check if there is sysfs mounted */
     103         [ -  + ]:        229 :         if (stat("/sys/module", &st) != 0) {
     104                 :          0 :                 EAL_LOG(DEBUG, "sysfs is not mounted! error %i (%s)", errno, strerror(errno));
     105                 :          0 :                 return -1;
     106                 :            :         }
     107                 :            : 
     108                 :            :         /* A module might be built-in, therefore try sysfs */
     109                 :            :         n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
     110         [ -  + ]:        229 :         if (n < 0 || n >= PATH_MAX) {
     111                 :          0 :                 EAL_LOG(DEBUG, "Could not format module path");
     112                 :          0 :                 return -1;
     113                 :            :         }
     114                 :            : 
     115         [ +  - ]:        229 :         if (stat(sysfs_mod_name, &st) != 0) {
     116                 :        229 :                 EAL_LOG(DEBUG, "Module %s not found! error %i (%s)", sysfs_mod_name, errno,
     117                 :            :                         strerror(errno));
     118                 :        229 :                 return 0;
     119                 :            :         }
     120                 :            : 
     121                 :            :         /* Module has been found */
     122                 :            :         return 1;
     123                 :            : }
     124                 :            : 
     125                 :            : static int vfio_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova,
     126                 :            :                 uint64_t len, int do_map);
     127                 :            : 
     128                 :            : static int
     129                 :            : is_null_map(const struct vfio_user_mem_map *map)
     130                 :            : {
     131   [ #  #  #  #  :          0 :         return map->addr == 0 && map->iova == 0 && map->len == 0 && map->chunk == 0;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     132                 :            : }
     133                 :            : 
     134                 :            : /* we may need to merge user mem maps together in case of user mapping/unmapping
     135                 :            :  * chunks of memory, so we'll need a comparator function to sort segments.
     136                 :            :  */
     137                 :            : static int
     138         [ #  # ]:          0 : user_mem_map_cmp(const void *a, const void *b)
     139                 :            : {
     140                 :            :         const struct vfio_user_mem_map *umm_a = a;
     141                 :            :         const struct vfio_user_mem_map *umm_b = b;
     142                 :            : 
     143                 :            :         /* move null entries to end */
     144                 :            :         if (is_null_map(umm_a))
     145                 :            :                 return 1;
     146                 :            :         if (is_null_map(umm_b))
     147                 :            :                 return -1;
     148                 :            : 
     149                 :            :         /* sort by iova first */
     150         [ #  # ]:          0 :         if (umm_a->iova < umm_b->iova)
     151                 :            :                 return -1;
     152         [ #  # ]:          0 :         if (umm_a->iova > umm_b->iova)
     153                 :            :                 return 1;
     154                 :            : 
     155         [ #  # ]:          0 :         if (umm_a->addr < umm_b->addr)
     156                 :            :                 return -1;
     157         [ #  # ]:          0 :         if (umm_a->addr > umm_b->addr)
     158                 :            :                 return 1;
     159                 :            : 
     160         [ #  # ]:          0 :         if (umm_a->len < umm_b->len)
     161                 :            :                 return -1;
     162         [ #  # ]:          0 :         if (umm_a->len > umm_b->len)
     163                 :            :                 return 1;
     164                 :            : 
     165         [ #  # ]:          0 :         if (umm_a->chunk < umm_b->chunk)
     166                 :            :                 return -1;
     167         [ #  # ]:          0 :         if (umm_a->chunk > umm_b->chunk)
     168                 :          0 :                 return 1;
     169                 :            : 
     170                 :            :         return 0;
     171                 :            : }
     172                 :            : 
     173                 :            : /*
     174                 :            :  * Take in an address range and list of current mappings, and produce a list of
     175                 :            :  * mappings that will be kept.
     176                 :            :  */
     177                 :            : static int
     178                 :          0 : process_maps(struct vfio_user_mem_map *src, size_t src_len, struct vfio_user_mem_map newmap[2],
     179                 :            :                 uint64_t vaddr, uint64_t len)
     180                 :            : {
     181                 :            :         struct vfio_user_mem_map *src_first = &src[0];
     182                 :          0 :         struct vfio_user_mem_map *src_last = &src[src_len - 1];
     183                 :            :         struct vfio_user_mem_map *dst_first = &newmap[0];
     184                 :            :         /* we can get at most two new segments */
     185                 :          0 :         struct vfio_user_mem_map *dst_last = &newmap[1];
     186                 :          0 :         uint64_t first_off = vaddr - src_first->addr;
     187                 :          0 :         uint64_t last_off = (src_last->addr + src_last->len) - (vaddr + len);
     188                 :            :         int newmap_len = 0;
     189                 :            : 
     190         [ #  # ]:          0 :         if (first_off != 0) {
     191                 :          0 :                 dst_first->addr = src_first->addr;
     192                 :          0 :                 dst_first->iova = src_first->iova;
     193                 :          0 :                 dst_first->len = first_off;
     194                 :          0 :                 dst_first->chunk = src_first->chunk;
     195                 :            : 
     196                 :            :                 newmap_len++;
     197                 :            :         }
     198         [ #  # ]:          0 :         if (last_off != 0) {
     199                 :            :                 /* if we had start offset, we have two segments */
     200         [ #  # ]:          0 :                 struct vfio_user_mem_map *last = first_off == 0 ? dst_first : dst_last;
     201                 :            : 
     202                 :          0 :                 last->addr = (src_last->addr + src_last->len) - last_off;
     203                 :          0 :                 last->iova = (src_last->iova + src_last->len) - last_off;
     204                 :          0 :                 last->len = last_off;
     205                 :          0 :                 last->chunk = src_last->chunk;
     206                 :            : 
     207                 :          0 :                 newmap_len++;
     208                 :            :         }
     209                 :          0 :         return newmap_len;
     210                 :            : }
     211                 :            : 
     212                 :            : /* erase certain maps from the list */
     213                 :            : static void
     214                 :          0 : delete_maps(struct vfio_user_mem_maps *user_mem_maps, struct vfio_user_mem_map *del_maps,
     215                 :            :                 size_t n_del)
     216                 :            : {
     217                 :            :         unsigned int i;
     218                 :            :         size_t j;
     219                 :            : 
     220         [ #  # ]:          0 :         for (i = 0, j = 0; i < RTE_DIM(user_mem_maps->maps) && j < n_del; i++) {
     221                 :          0 :                 struct vfio_user_mem_map *left = &user_mem_maps->maps[i];
     222                 :          0 :                 struct vfio_user_mem_map *right = &del_maps[j];
     223                 :            : 
     224         [ #  # ]:          0 :                 if (user_mem_map_cmp(left, right) == 0) {
     225                 :            :                         memset(left, 0, sizeof(*left));
     226                 :          0 :                         j++;
     227                 :          0 :                         user_mem_maps->n_maps--;
     228                 :            :                 }
     229                 :            :         }
     230                 :          0 : }
     231                 :            : 
     232                 :            : static void
     233                 :          0 : copy_maps(struct vfio_user_mem_maps *user_mem_maps, struct vfio_user_mem_map *add_maps,
     234                 :            :                 size_t n_add)
     235                 :            : {
     236                 :            :         unsigned int i;
     237                 :            :         size_t j;
     238                 :            : 
     239         [ #  # ]:          0 :         for (i = 0, j = 0; i < RTE_DIM(user_mem_maps->maps) && j < n_add; i++) {
     240                 :          0 :                 struct vfio_user_mem_map *left = &user_mem_maps->maps[i];
     241         [ #  # ]:          0 :                 struct vfio_user_mem_map *right = &add_maps[j];
     242                 :            : 
     243                 :            :                 /* insert into empty space */
     244                 :            :                 if (is_null_map(left)) {
     245                 :            :                         memcpy(left, right, sizeof(*left));
     246                 :          0 :                         j++;
     247                 :          0 :                         user_mem_maps->n_maps++;
     248                 :            :                 }
     249                 :            :         }
     250                 :          0 : }
     251                 :            : 
     252                 :            : /* try merging two maps into one, return 1 if succeeded */
     253                 :            : static int
     254                 :          0 : merge_map(struct vfio_user_mem_map *left, struct vfio_user_mem_map *right)
     255                 :            : {
     256                 :            :         /* merge the same maps into one */
     257         [ #  # ]:          0 :         if (memcmp(left, right, sizeof(struct vfio_user_mem_map)) == 0)
     258                 :          0 :                 goto out;
     259                 :            : 
     260         [ #  # ]:          0 :         if (left->addr + left->len != right->addr)
     261                 :            :                 return 0;
     262         [ #  # ]:          0 :         if (left->iova + left->len != right->iova)
     263                 :            :                 return 0;
     264         [ #  # ]:          0 :         if (left->chunk != right->chunk)
     265                 :            :                 return 0;
     266                 :          0 :         left->len += right->len;
     267                 :            : 
     268                 :          0 : out:
     269                 :            :         memset(right, 0, sizeof(*right));
     270                 :            : 
     271                 :          0 :         return 1;
     272                 :            : }
     273                 :            : 
     274                 :            : static bool
     275                 :          0 : addr_is_chunk_aligned(struct vfio_user_mem_map *maps, size_t n_maps, uint64_t vaddr, uint64_t iova)
     276                 :            : {
     277                 :            :         unsigned int i;
     278                 :            : 
     279         [ #  # ]:          0 :         for (i = 0; i < n_maps; i++) {
     280                 :          0 :                 struct vfio_user_mem_map *map = &maps[i];
     281                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     282                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     283                 :          0 :                 uint64_t map_va_off = vaddr - map->addr;
     284                 :          0 :                 uint64_t map_iova_off = iova - map->iova;
     285                 :            : 
     286                 :            :                 /* we include end of the segment in comparison as well */
     287   [ #  #  #  # ]:          0 :                 bool addr_in_map = (vaddr >= map->addr) && (vaddr <= map_va_end);
     288   [ #  #  #  # ]:          0 :                 bool iova_in_map = (iova >= map->iova) && (iova <= map_iova_end);
     289                 :            :                 /* chunk may not be power of two, so use modulo */
     290                 :          0 :                 bool addr_is_aligned = (map_va_off % map->chunk) == 0;
     291                 :          0 :                 bool iova_is_aligned = (map_iova_off % map->chunk) == 0;
     292                 :            : 
     293   [ #  #  #  # ]:          0 :                 if (addr_in_map && iova_in_map && addr_is_aligned && iova_is_aligned)
     294                 :            :                         return true;
     295                 :            :         }
     296                 :            :         return false;
     297                 :            : }
     298                 :            : 
     299                 :            : static int
     300                 :          0 : find_user_mem_maps(struct vfio_user_mem_maps *user_mem_maps, uint64_t addr, uint64_t iova,
     301                 :            :                 uint64_t len, struct vfio_user_mem_map *dst, size_t dst_len)
     302                 :            : {
     303                 :          0 :         uint64_t va_end = addr + len;
     304                 :          0 :         uint64_t iova_end = iova + len;
     305                 :            :         bool found = false;
     306                 :            :         size_t j;
     307                 :            :         int i, ret;
     308                 :            : 
     309         [ #  # ]:          0 :         for (i = 0, j = 0; i < user_mem_maps->n_maps; i++) {
     310                 :          0 :                 struct vfio_user_mem_map *map = &user_mem_maps->maps[i];
     311                 :          0 :                 uint64_t map_va_end = map->addr + map->len;
     312                 :          0 :                 uint64_t map_iova_end = map->iova + map->len;
     313                 :            : 
     314   [ #  #  #  # ]:          0 :                 bool start_addr_in_map = (addr >= map->addr) && (addr < map_va_end);
     315   [ #  #  #  # ]:          0 :                 bool end_addr_in_map = (va_end > map->addr) && (va_end <= map_va_end);
     316   [ #  #  #  # ]:          0 :                 bool start_iova_in_map = (iova >= map->iova) && (iova < map_iova_end);
     317   [ #  #  #  # ]:          0 :                 bool end_iova_in_map = (iova_end > map->iova) && (iova_end <= map_iova_end);
     318                 :            : 
     319                 :            :                 /* do we have space in temporary map? */
     320         [ #  # ]:          0 :                 if (j == dst_len) {
     321                 :            :                         ret = -ENOSPC;
     322                 :          0 :                         goto err;
     323                 :            :                 }
     324                 :            :                 /* check if current map is start of our segment */
     325   [ #  #  #  # ]:          0 :                 if (!found && start_addr_in_map && start_iova_in_map)
     326                 :            :                         found = true;
     327                 :            :                 /* if we have previously found a segment, add it to the map */
     328         [ #  # ]:          0 :                 if (found) {
     329                 :            :                         /* copy the segment into our temporary map */
     330         [ #  # ]:          0 :                         memcpy(&dst[j++], map, sizeof(*map));
     331                 :            : 
     332                 :            :                         /* if we match end of segment, quit */
     333         [ #  # ]:          0 :                         if (end_addr_in_map && end_iova_in_map)
     334                 :          0 :                                 return j;
     335                 :            :                 }
     336                 :            :         }
     337                 :            :         /* we didn't find anything */
     338                 :            :         ret = -ENOENT;
     339                 :          0 : err:
     340                 :          0 :         memset(dst, 0, sizeof(*dst) * dst_len);
     341                 :          0 :         return ret;
     342                 :            : }
     343                 :            : 
     344                 :            : /* this will sort all user maps, and merge/compact any adjacent maps */
     345                 :            : static void
     346                 :          0 : compact_user_maps(struct vfio_user_mem_maps *user_mem_maps)
     347                 :            : {
     348                 :            :         unsigned int i;
     349                 :            : 
     350                 :          0 :         qsort(user_mem_maps->maps, RTE_DIM(user_mem_maps->maps), sizeof(user_mem_maps->maps[0]),
     351                 :            :                 user_mem_map_cmp);
     352                 :            : 
     353                 :            :         /* we'll go over the list backwards when merging */
     354         [ #  # ]:          0 :         for (i = RTE_DIM(user_mem_maps->maps) - 2; i != 0; i--) {
     355                 :            :                 struct vfio_user_mem_map *l, *r;
     356                 :            : 
     357                 :          0 :                 l = &user_mem_maps->maps[i];
     358         [ #  # ]:          0 :                 r = &user_mem_maps->maps[i + 1];
     359                 :            : 
     360                 :          0 :                 if (is_null_map(l) || is_null_map(r))
     361                 :          0 :                         continue;
     362                 :            : 
     363                 :            :                 /* try and merge the maps */
     364         [ #  # ]:          0 :                 if (merge_map(l, r))
     365                 :          0 :                         user_mem_maps->n_maps--;
     366                 :            :         }
     367                 :            : 
     368                 :            :         /* the entries are still sorted, but now they have holes in them, so
     369                 :            :          * sort the list again.
     370                 :            :          */
     371                 :          0 :         qsort(user_mem_maps->maps, RTE_DIM(user_mem_maps->maps), sizeof(user_mem_maps->maps[0]),
     372                 :            :                 user_mem_map_cmp);
     373                 :          0 : }
     374                 :            : 
     375                 :            : #define VFIO_NOIOMMU_MODE_PATH "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
     376                 :            : 
     377                 :            : static int
     378                 :          0 : vfio_noiommu_is_enabled(void)
     379                 :            : {
     380                 :            :         int fd;
     381                 :            :         ssize_t cnt;
     382                 :            :         char c;
     383                 :            : 
     384                 :            :         fd = open(VFIO_NOIOMMU_MODE_PATH, O_RDONLY);
     385         [ #  # ]:          0 :         if (fd < 0) {
     386         [ #  # ]:          0 :                 if (errno != ENOENT) {
     387                 :          0 :                         EAL_LOG(ERR, "Cannot open VFIO noiommu file %i (%s)", errno,
     388                 :            :                                 strerror(errno));
     389                 :          0 :                         return -1;
     390                 :            :                 }
     391                 :            :                 return 0;
     392                 :            :         }
     393                 :            : 
     394                 :            :         cnt = read(fd, &c, 1);
     395                 :          0 :         close(fd);
     396         [ #  # ]:          0 :         if (cnt != 1) {
     397                 :          0 :                 EAL_LOG(ERR, "Unable to read from VFIO noiommu file %i (%s)", errno,
     398                 :            :                         strerror(errno));
     399                 :          0 :                 return -1;
     400                 :            :         }
     401                 :            : 
     402                 :          0 :         return c == 'Y';
     403                 :            : }
     404                 :            : 
     405                 :            : bool
     406                 :          0 : vfio_container_is_default(struct vfio_container *cfg)
     407                 :            : {
     408                 :          0 :         return cfg == vfio_global_cfg.default_cfg;
     409                 :            : }
     410                 :            : 
     411                 :            : static struct vfio_container *
     412                 :            : vfio_container_get_by_fd(int container_fd)
     413                 :            : {
     414                 :            :         struct vfio_container *cfg;
     415                 :            : 
     416   [ #  #  #  #  :          0 :         if (container_fd == DEV_VFIO_DEFAULT_CONTAINER_FD)
             #  #  #  # ]
     417                 :          0 :                 return vfio_global_cfg.default_cfg;
     418                 :            : 
     419   [ #  #  #  #  :          0 :         VFIO_CONTAINER_FOREACH_ACTIVE(cfg) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     420   [ #  #  #  #  :          0 :                 if (cfg->container_fd == container_fd)
             #  #  #  # ]
     421                 :            :                         return cfg;
     422                 :            :         }
     423                 :            :         return NULL;
     424                 :            : }
     425                 :            : 
     426                 :            : static struct vfio_container *
     427                 :            : vfio_container_get_by_group_num(int group_num)
     428                 :            : {
     429                 :            :         struct vfio_container *cfg;
     430                 :            :         struct vfio_group *grp;
     431                 :            : 
     432   [ #  #  #  # ]:          0 :         VFIO_CONTAINER_FOREACH_ACTIVE(cfg) {
     433   [ #  #  #  # ]:          0 :                 VFIO_GROUP_FOREACH_ACTIVE(cfg, grp)
     434         [ #  # ]:          0 :                         if (grp->group_num == group_num)
     435                 :            :                                 return cfg;
     436                 :            :         }
     437                 :            :         return NULL;
     438                 :            : }
     439                 :            : 
     440                 :            : static struct vfio_container *
     441                 :          0 : vfio_container_get_by_dev_num(int dev_num)
     442                 :            : {
     443                 :            :         struct vfio_container *cfg;
     444                 :            :         struct vfio_device *dev;
     445                 :            : 
     446   [ #  #  #  # ]:          0 :         VFIO_CONTAINER_FOREACH_ACTIVE(cfg) {
     447   [ #  #  #  # ]:          0 :                 VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev) {
     448                 :            :                         /* only cdev mode has dev_num */
     449         [ #  # ]:          0 :                         if (dev->mode != DEV_VFIO_MODE_CDEV)
     450                 :          0 :                                 continue;
     451         [ #  # ]:          0 :                         if (dev->dev_num == dev_num)
     452                 :          0 :                                 return cfg;
     453                 :            :                 }
     454                 :            :         }
     455                 :            :         return NULL;
     456                 :            : }
     457                 :            : 
     458                 :            : static struct vfio_container *
     459                 :            : vfio_container_create(void)
     460                 :            : {
     461                 :            :         struct vfio_container *cfg;
     462                 :            : 
     463                 :            :         /* find an unused container config */
     464   [ #  #  #  # ]:          0 :         VFIO_CONTAINER_FOREACH(cfg) {
     465   [ #  #  #  # ]:          0 :                 if (!cfg->active) {
     466                 :          0 :                         *cfg = VFIO_CONTAINER_INITIALIZER;
     467                 :          0 :                         cfg->active = true;
     468                 :            :                         return cfg;
     469                 :            :                 }
     470                 :            :         }
     471                 :            :         /* no space */
     472                 :            :         return NULL;
     473                 :            : }
     474                 :            : 
     475                 :            : static void
     476                 :          0 : vfio_container_erase(struct vfio_container *cfg)
     477                 :            : {
     478   [ #  #  #  # ]:          0 :         if (cfg->container_fd >= 0 && close(cfg->container_fd))
     479                 :          0 :                 EAL_LOG(ERR, "Error when closing container, %d (%s)", errno, strerror(errno));
     480                 :            : 
     481                 :          0 :         *cfg = VFIO_CONTAINER_INITIALIZER;
     482                 :          0 : }
     483                 :            : 
     484                 :            : static struct vfio_device *
     485                 :          0 : vfio_device_create(struct vfio_container *cfg, enum dev_vfio_mode mode)
     486                 :            : {
     487                 :            :         struct vfio_device *dev;
     488                 :            : 
     489                 :            :         /* is there space? */
     490         [ #  # ]:          0 :         if (cfg->n_devices == RTE_DIM(cfg->devices))
     491                 :            :                 return NULL;
     492                 :            : 
     493         [ #  # ]:          0 :         VFIO_DEVICE_FOREACH(cfg, dev) {
     494         [ #  # ]:          0 :                 if (dev->active)
     495                 :            :                         continue;
     496                 :          0 :                 dev->active = true;
     497                 :          0 :                 dev->mode = mode;
     498                 :            :                 /* set to invalid fd */
     499                 :          0 :                 dev->fd = -1;
     500                 :            : 
     501                 :          0 :                 cfg->n_devices++;
     502                 :          0 :                 return dev;
     503                 :            :         }
     504                 :            :         /* should not happen */
     505                 :          0 :         EAL_LOG(WARNING, "Could not find space in device list for container");
     506                 :          0 :         return NULL;
     507                 :            : }
     508                 :            : 
     509                 :            : static void
     510                 :          0 : vfio_device_erase(struct vfio_container *cfg, struct vfio_device *dev)
     511                 :            : {
     512   [ #  #  #  # ]:          0 :         if (dev->fd >= 0 && close(dev->fd))
     513                 :          0 :                 EAL_LOG(ERR, "Error when closing device, %d (%s)", errno, strerror(errno));
     514                 :            : 
     515         [ #  # ]:          0 :         if (dev->mode == DEV_VFIO_MODE_GROUP) {
     516                 :          0 :                 free(dev->sysfs_base);
     517                 :          0 :                 free(dev->dev_addr);
     518                 :            :         }
     519                 :          0 :         *dev = (struct vfio_device){0};
     520                 :          0 :         cfg->n_devices--;
     521                 :          0 : }
     522                 :            : 
     523                 :            : static struct vfio_device *
     524                 :          0 : vfio_device_get_by_addr(struct vfio_container *cfg, const char *sysfs_base, const char *dev_addr)
     525                 :            : {
     526                 :            :         struct vfio_device *dev;
     527                 :            : 
     528   [ #  #  #  # ]:          0 :         VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev) {
     529                 :            :                 /* only group mode have sysfs_base and dev_addr */
     530         [ #  # ]:          0 :                 if (dev->mode != DEV_VFIO_MODE_GROUP)
     531                 :          0 :                         continue;
     532         [ #  # ]:          0 :                 if (strcmp(dev->sysfs_base, sysfs_base) == 0 &&
     533         [ #  # ]:          0 :                                 strcmp(dev->dev_addr, dev_addr) == 0)
     534                 :          0 :                         return dev;
     535                 :            :         }
     536                 :            :         return NULL;
     537                 :            : }
     538                 :            : 
     539                 :            : static void
     540                 :          0 : vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
     541                 :            :                 void *arg __rte_unused)
     542                 :            : {
     543                 :          0 :         struct vfio_container *cfg = vfio_global_cfg.default_cfg;
     544                 :            :         struct rte_memseg_list *msl;
     545                 :            :         struct rte_memseg *ms;
     546                 :            :         size_t cur_len = 0;
     547                 :            : 
     548                 :          0 :         msl = rte_mem_virt2memseg_list(addr);
     549                 :            : 
     550                 :            :         /* for IOVA as VA mode, no need to care for IOVA addresses */
     551   [ #  #  #  # ]:          0 :         if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
     552                 :          0 :                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
     553                 :          0 :                 uint64_t page_sz = msl->page_sz;
     554                 :            : 
     555                 :            :                 /* Maintain granularity of DMA map/unmap to memseg size */
     556         [ #  # ]:          0 :                 for (; cur_len < len; cur_len += page_sz) {
     557         [ #  # ]:          0 :                         if (type == RTE_MEM_EVENT_ALLOC)
     558                 :          0 :                                 vfio_dma_mem_map(cfg, vfio_va, vfio_va, page_sz, 1);
     559                 :            :                         else
     560                 :          0 :                                 vfio_dma_mem_map(cfg, vfio_va, vfio_va, page_sz, 0);
     561                 :          0 :                         vfio_va += page_sz;
     562                 :            :                 }
     563                 :            : 
     564                 :            :                 return;
     565                 :            :         }
     566                 :            : 
     567                 :            :         /* memsegs are contiguous in memory */
     568                 :          0 :         ms = rte_mem_virt2memseg(addr, msl);
     569         [ #  # ]:          0 :         while (cur_len < len) {
     570                 :            :                 /* some memory segments may have invalid IOVA */
     571         [ #  # ]:          0 :                 if (ms->iova == RTE_BAD_IOVA) {
     572                 :          0 :                         EAL_LOG(DEBUG, "Memory segment at %p has bad IOVA, skipping", ms->addr);
     573                 :          0 :                         goto next;
     574                 :            :                 }
     575         [ #  # ]:          0 :                 if (type == RTE_MEM_EVENT_ALLOC)
     576                 :          0 :                         vfio_dma_mem_map(cfg, ms->addr_64, ms->iova, ms->len, 1);
     577                 :            :                 else
     578                 :          0 :                         vfio_dma_mem_map(cfg, ms->addr_64, ms->iova, ms->len, 0);
     579                 :          0 : next:
     580                 :          0 :                 cur_len += ms->len;
     581                 :          0 :                 ++ms;
     582                 :            :         }
     583                 :            : }
     584                 :            : 
     585                 :            : static int
     586                 :          0 : vfio_register_mem_event_callback(void)
     587                 :            : {
     588                 :            :         int ret;
     589                 :            : 
     590                 :          0 :         ret = rte_mem_event_callback_register(VFIO_MEM_EVENT_CLB_NAME, vfio_mem_event_callback,
     591                 :            :                 NULL);
     592                 :            : 
     593   [ #  #  #  # ]:          0 :         if (ret && rte_errno != ENOTSUP) {
     594                 :          0 :                 EAL_LOG(ERR, "Could not install memory event callback for VFIO");
     595                 :          0 :                 return -1;
     596                 :            :         }
     597         [ #  # ]:          0 :         if (ret)
     598                 :          0 :                 EAL_LOG(DEBUG, "Memory event callbacks not supported");
     599                 :            :         else
     600                 :          0 :                 EAL_LOG(DEBUG, "Installed memory event callback for VFIO");
     601                 :            : 
     602                 :            :         return 0;
     603                 :            : }
     604                 :            : 
     605                 :            : static int
     606                 :          0 : vfio_setup_dma_mem(struct vfio_container *cfg)
     607                 :            : {
     608                 :            :         struct vfio_user_mem_maps *user_mem_maps = &cfg->mem_maps;
     609                 :            :         int i, ret;
     610                 :            : 
     611                 :            :         /* do we need to map DPDK-managed memory? */
     612   [ #  #  #  # ]:          0 :         if (vfio_container_is_default(cfg) && rte_eal_process_type() == RTE_PROC_PRIMARY)
     613                 :          0 :                 ret = vfio_global_cfg.ops->dma_map_func(cfg);
     614                 :            :         else
     615                 :            :                 ret = 0;
     616         [ #  # ]:          0 :         if (ret) {
     617                 :          0 :                 EAL_LOG(ERR, "DMA remapping failed, error %i (%s)", errno, strerror(errno));
     618                 :          0 :                 return -1;
     619                 :            :         }
     620                 :            : 
     621                 :            :         /*
     622                 :            :          * not all IOMMU types support DMA mapping, but if we have mappings in the list - that
     623                 :            :          * means we have previously mapped something successfully, so we can be sure that DMA
     624                 :            :          * mapping is supported.
     625                 :            :          */
     626         [ #  # ]:          0 :         for (i = 0; i < user_mem_maps->n_maps; i++) {
     627                 :            :                 struct vfio_user_mem_map *map;
     628                 :            :                 map = &user_mem_maps->maps[i];
     629                 :            : 
     630                 :          0 :                 ret = vfio_global_cfg.ops->dma_user_map_func(cfg, map->addr, map->iova, map->len,
     631                 :            :                         1);
     632         [ #  # ]:          0 :                 if (ret) {
     633                 :          0 :                         EAL_LOG(ERR, "Couldn't map user memory for DMA: "
     634                 :            :                                 "va: 0x%" PRIx64 " iova: 0x%" PRIx64 " len: 0x%" PRIu64,
     635                 :            :                                 map->addr, map->iova, map->len);
     636                 :          0 :                         return -1;
     637                 :            :                 }
     638                 :            :         }
     639                 :            : 
     640                 :            :         return 0;
     641                 :            : }
     642                 :            : 
     643                 :            : static enum vfio_result
     644                 :          0 : vfio_cdev_assign_device(struct vfio_container *cfg, const char *sysfs_base, const char *dev_addr,
     645                 :            :                 struct vfio_device **out_dev)
     646                 :            : {
     647                 :            :         struct vfio_device *dev, *found_dev;
     648                 :            :         enum vfio_result res;
     649                 :            :         int dev_num, ret;
     650                 :            : 
     651                 :            :         /* get the cdev device number from sysfs */
     652                 :          0 :         ret = vfio_cdev_get_device_num(sysfs_base, dev_addr, &dev_num);
     653         [ #  # ]:          0 :         if (ret < 0) {
     654                 :          0 :                 EAL_LOG(ERR, "Failed to get cdev device number for %s", dev_addr);
     655                 :          0 :                 return VFIO_ERROR;
     656         [ #  # ]:          0 :         } else if (ret == 0) {
     657                 :          0 :                 EAL_LOG(ERR, "Device %s not bound to vfio-pci cdev", dev_addr);
     658                 :          0 :                 return VFIO_NOT_MANAGED;
     659                 :            :         }
     660                 :            : 
     661                 :            :         /* do we already have this device? */
     662                 :          0 :         found_dev = vfio_cdev_get_dev_by_num(cfg, dev_num);
     663         [ #  # ]:          0 :         if (found_dev != NULL) {
     664                 :          0 :                 EAL_LOG(DEBUG, "Device %s already assigned to this container", dev_addr);
     665                 :          0 :                 *out_dev = found_dev;
     666                 :          0 :                 return VFIO_EXISTS;
     667                 :            :         }
     668                 :            :         /* create new device structure */
     669                 :          0 :         dev = vfio_device_create(cfg, DEV_VFIO_MODE_CDEV);
     670         [ #  # ]:          0 :         if (dev == NULL) {
     671                 :          0 :                 EAL_LOG(ERR, "No space to track new VFIO cdev device");
     672                 :          0 :                 return VFIO_NO_SPACE;
     673                 :            :         }
     674                 :            :         /* store device number */
     675                 :          0 :         dev->dev_num = dev_num;
     676                 :            : 
     677                 :            :         /* set up our device now and store it in config */
     678                 :          0 :         ret = vfio_cdev_setup_device(cfg, dev);
     679         [ #  # ]:          0 :         if (ret < 0) {
     680                 :          0 :                 EAL_LOG(ERR, "Cannot setup cdev device %s", dev_addr);
     681                 :            :                 res = VFIO_ERROR;
     682                 :          0 :                 goto err;
     683                 :            :         }
     684                 :          0 :         *out_dev = dev;
     685                 :          0 :         return VFIO_SUCCESS;
     686                 :            : 
     687                 :            : err:
     688                 :          0 :         vfio_device_erase(cfg, dev);
     689                 :          0 :         return res;
     690                 :            : }
     691                 :            : 
     692                 :            : static enum vfio_result
     693                 :          0 : vfio_group_assign_device(struct vfio_container *cfg, const char *sysfs_base, const char *dev_addr,
     694                 :            :                 struct vfio_device **out_dev)
     695                 :            : {
     696                 :            :         struct vfio_group_config *group_cfg = &cfg->group_cfg;
     697                 :            :         struct vfio_group *grp;
     698                 :            :         struct vfio_device *dev;
     699                 :            :         int iommu_group_num;
     700                 :            :         enum vfio_result res;
     701                 :            :         int ret;
     702                 :            : 
     703                 :            :         /* if this device is already assigned, reuse it instead of opening a second fd */
     704                 :          0 :         dev = vfio_device_get_by_addr(cfg, sysfs_base, dev_addr);
     705         [ #  # ]:          0 :         if (dev != NULL) {
     706                 :          0 :                 *out_dev = dev;
     707                 :          0 :                 return VFIO_EXISTS;
     708                 :            :         }
     709                 :            : 
     710                 :            :         /* allocate new device in config */
     711                 :          0 :         dev = vfio_device_create(cfg, vfio_global_cfg.mode);
     712         [ #  # ]:          0 :         if (dev == NULL) {
     713                 :          0 :                 EAL_LOG(ERR, "No space to track new VFIO device");
     714                 :          0 :                 return VFIO_NO_SPACE;
     715                 :            :         }
     716                 :            : 
     717                 :            :         /* allocate strings for sysfs path and device address */
     718                 :          0 :         dev->sysfs_base = strdup(sysfs_base);
     719                 :          0 :         dev->dev_addr = strdup(dev_addr);
     720   [ #  #  #  # ]:          0 :         if (dev->sysfs_base == NULL || dev->dev_addr == NULL) {
     721                 :          0 :                 EAL_LOG(ERR, "Cannot allocate memory for device %s", dev_addr);
     722                 :          0 :                 free(dev->sysfs_base);
     723                 :          0 :                 dev->sysfs_base = NULL;
     724                 :          0 :                 free(dev->dev_addr);
     725                 :          0 :                 dev->dev_addr = NULL;
     726                 :            :                 res = VFIO_NO_MEM;
     727                 :          0 :                 goto device_erase;
     728                 :            :         }
     729                 :            : 
     730                 :            :         /* remember to register mem event callback for default container in primary */
     731   [ #  #  #  # ]:          0 :         bool need_clb = vfio_container_is_default(cfg) &&
     732                 :          0 :                 rte_eal_process_type() == RTE_PROC_PRIMARY;
     733                 :            : 
     734                 :            :         /* get group number for this device */
     735                 :          0 :         ret = vfio_group_get_num(sysfs_base, dev_addr, &iommu_group_num);
     736         [ #  # ]:          0 :         if (ret < 0) {
     737                 :          0 :                 EAL_LOG(ERR, "Cannot get IOMMU group for %s", dev_addr);
     738                 :            :                 res = VFIO_ERROR;
     739                 :          0 :                 goto device_erase;
     740         [ #  # ]:          0 :         } else if (ret == 0) {
     741                 :            :                 res = VFIO_NOT_MANAGED;
     742                 :          0 :                 goto device_erase;
     743                 :            :         }
     744                 :            : 
     745                 :            :         /* group may already exist as multiple devices may share group */
     746                 :          0 :         grp = vfio_group_get_by_num(cfg, iommu_group_num);
     747         [ #  # ]:          0 :         if (grp == NULL) {
     748                 :            :                 /* no device currently uses this group, create it */
     749                 :          0 :                 grp = vfio_group_create(cfg, iommu_group_num);
     750         [ #  # ]:          0 :                 if (grp == NULL) {
     751                 :          0 :                         EAL_LOG(ERR, "Cannot allocate group for device %s", dev_addr);
     752                 :            :                         res = VFIO_NO_SPACE;
     753                 :          0 :                         goto device_erase;
     754                 :            :                 }
     755                 :            : 
     756                 :            :                 /* open group fd */
     757                 :          0 :                 ret = vfio_group_open_fd(cfg, grp);
     758         [ #  # ]:          0 :                 if (ret == -ENOENT) {
     759                 :          0 :                         EAL_LOG(DEBUG, "Device %s (IOMMU group %d) not managed by VFIO",
     760                 :            :                                 dev_addr, iommu_group_num);
     761                 :            :                         res = VFIO_NOT_MANAGED;
     762                 :          0 :                         goto group_erase;
     763         [ #  # ]:          0 :                 } else if (ret < 0) {
     764                 :          0 :                         EAL_LOG(ERR, "Cannot open VFIO group %d for device %s",
     765                 :            :                                 iommu_group_num, dev_addr);
     766                 :            :                         res = VFIO_ERROR;
     767                 :          0 :                         goto group_erase;
     768                 :            :                 }
     769                 :            : 
     770                 :            :                 /* prepare group (viability + container attach) */
     771                 :          0 :                 ret = vfio_group_prepare(cfg, grp);
     772         [ #  # ]:          0 :                 if (ret < 0) {
     773                 :            :                         res = VFIO_ERROR;
     774                 :          0 :                         goto group_erase;
     775                 :            :                 }
     776                 :            : 
     777                 :            :                 /* set up IOMMU type once per container */
     778         [ #  # ]:          0 :                 if (!group_cfg->iommu_type_set) {
     779                 :          0 :                         ret = vfio_group_setup_iommu(cfg);
     780         [ #  # ]:          0 :                         if (ret < 0) {
     781                 :            :                                 res = VFIO_ERROR;
     782                 :          0 :                                 goto group_erase;
     783                 :            :                         }
     784                 :          0 :                         group_cfg->iommu_type_set = true;
     785                 :            :                 }
     786                 :            : 
     787                 :            :                 /* set up DMA memory once per container */
     788         [ #  # ]:          0 :                 if (!cfg->dma_setup_done) {
     789                 :          0 :                         rte_spinlock_recursive_lock(&cfg->mem_maps.lock);
     790                 :          0 :                         ret = vfio_setup_dma_mem(cfg);
     791                 :          0 :                         rte_spinlock_recursive_unlock(&cfg->mem_maps.lock);
     792         [ #  # ]:          0 :                         if (ret < 0) {
     793                 :          0 :                                 EAL_LOG(ERR, "DMA remapping for %s failed", dev_addr);
     794                 :            :                                 res = VFIO_ERROR;
     795                 :          0 :                                 goto group_erase;
     796                 :            :                         }
     797                 :          0 :                         cfg->dma_setup_done = true;
     798                 :            :                 }
     799                 :            : 
     800                 :            :                 /* set up mem event callback if needed */
     801   [ #  #  #  # ]:          0 :                 if (need_clb && !group_cfg->mem_event_clb_set) {
     802                 :          0 :                         ret = vfio_register_mem_event_callback();
     803         [ #  # ]:          0 :                         if (ret < 0) {
     804                 :            :                                 res = VFIO_ERROR;
     805                 :          0 :                                 goto group_erase;
     806                 :            :                         }
     807                 :          0 :                         group_cfg->mem_event_clb_set = true;
     808                 :            :                 }
     809                 :            :         }
     810                 :            : 
     811                 :            :         /* open dev fd */
     812                 :          0 :         ret = vfio_group_setup_device_fd(dev_addr, grp, dev);
     813         [ #  # ]:          0 :         if (ret < 0) {
     814                 :          0 :                 EAL_LOG(ERR, "Cannot open VFIO device %s, error %i (%s)", dev_addr, errno,
     815                 :            :                         strerror(errno));
     816                 :            :                 res = VFIO_ERROR;
     817                 :          0 :                 goto group_erase;
     818                 :            :         }
     819                 :            : 
     820                 :            :         /*
     821                 :            :          * we do not need to look in other configs: if we were to attempt to use
     822                 :            :          * a different container, the kernel wouldn't have allowed us to bind the
     823                 :            :          * group to the container in the first place.
     824                 :            :          */
     825                 :          0 :         *out_dev = dev;
     826                 :          0 :         return VFIO_SUCCESS;
     827                 :          0 : group_erase:
     828                 :            :         /* this may be a pre-existing group so only erase it if it has no devices */
     829         [ #  # ]:          0 :         if (grp->n_devices == 0)
     830                 :          0 :                 vfio_group_erase(cfg, grp);
     831                 :            :         /* if we registered callback, unregister it */
     832   [ #  #  #  # ]:          0 :         if (group_cfg->n_groups == 0 && group_cfg->mem_event_clb_set) {
     833                 :          0 :                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME, NULL);
     834                 :          0 :                 group_cfg->mem_event_clb_set = false;
     835                 :            :         }
     836                 :          0 : device_erase:
     837                 :          0 :         vfio_device_erase(cfg, dev);
     838                 :          0 :         return res;
     839                 :            : }
     840                 :            : 
     841                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_container_assign_device)
     842                 :            : int
     843                 :          0 : dev_vfio_container_assign_device(int container_fd, const char *sysfs_base, const char *dev_addr)
     844                 :            : {
     845                 :            :         struct vfio_container *cfg;
     846                 :            :         enum vfio_result res;
     847                 :            :         struct vfio_device *dev;
     848                 :            : 
     849         [ #  # ]:          0 :         if (sysfs_base == NULL || dev_addr == NULL) {
     850                 :          0 :                 rte_errno = EINVAL;
     851                 :          0 :                 return -1;
     852                 :            :         }
     853                 :            : 
     854                 :            :         /*
     855                 :            :          * The device-to-container assignment is a complex problem to solve,
     856                 :            :          * for the following reasons:
     857                 :            :          *
     858                 :            :          * 1. PCI infrastructure is decoupled from VFIO, so PCI does not know
     859                 :            :          *    anything about VFIO
     860                 :            :          *
     861                 :            :          * This means that while 99% of VFIO usage is PCI-related, we cannot
     862                 :            :          * communicate to PCI that we want to map a particular device using a
     863                 :            :          * particular container. Previously, this was achieved using
     864                 :            :          * back-channel communication between VFIO and PCI bus via IOMMU group
     865                 :            :          * binding, so that whenever PCI map actually happens, VFIO knows which
     866                 :            :          * container to use, so this is roughly the model we are going with.
     867                 :            :          *
     868                 :            :          * 2. VFIO cannot depend on PCI because VFIO is in EAL
     869                 :            :          *
     870                 :            :          * We cannot "assign" a PCI device to container using rte_pci_device
     871                 :            :          * pointer because VFIO cannot depend on PCI definitions, nor can we
     872                 :            :          * even assume that our device is in fact a PCI device, even though in
     873                 :            :          * practice this is true (at the time of this writing, FSLMC is the only
     874                 :            :          * bus doing non-PCI VFIO mappings, but FSLMC manages all VFIO
     875                 :            :          * infrastructure by itself, so in practice even counting FSLMC bus,
     876                 :            :          * we're always dealing with PCI devices).
     877                 :            :          *
     878                 :            :          * 3. The "assignment" means different things for group and cdev mode
     879                 :            :          *
     880                 :            :          * In group mode, to "bind" a device to a specific container, it is
     881                 :            :          * enough to bind its IOMMU group, so that when dev_vfio_setup_device()
     882                 :            :          * is called, we simply retrieve already existing group, and through
     883                 :            :          * that we figure out which container to use.
     884                 :            :          *
     885                 :            :          * For cdev mode, there are no "groups", so "assignment" either means we
     886                 :            :          * store some kind of uniquely identifying token (such as device number,
     887                 :            :          * or an opaque pointer), or we simply open the device straight away,
     888                 :            :          * and when dev_vfio_setup_device() comes we simply return the fd that
     889                 :            :          * was already opened at assign.
     890                 :            :          *
     891                 :            :          * Doing it the latter way (opening the device at assign for both group
     892                 :            :          * and cdev modes) actually solves all of these problems, so that's what
     893                 :            :          * we're going to do - the device setup API call will actually just
     894                 :            :          * assign the device to default container, while release will
     895                 :            :          * automatically cleanup and unassign anything that needs to be
     896                 :            :          * unassigned. There will be no "unassign" call, as it is not necessary.
     897                 :            :          *
     898                 :            :          * The cdev mode can deduplicate on "device number", which is assigned
     899                 :            :          * by the kernel and is unique per device. The group mode keeps its
     900                 :            :          * deduplication mechanism based on a combination of sysfs path and
     901                 :            :          * device address, which is unique per device as well. This way we can
     902                 :            :          * ensure that assigning a device before setting it up works correctly,
     903                 :            :          * and that we do not open the same device fd twice or leak any
     904                 :            :          * resources on device release.
     905                 :            :          */
     906                 :            : 
     907         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
     908                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
     909                 :          0 :                 rte_errno = ENXIO;
     910                 :          0 :                 return -1;
     911                 :            :         }
     912                 :            : 
     913                 :            :         cfg = vfio_container_get_by_fd(container_fd);
     914         [ #  # ]:          0 :         if (cfg == NULL) {
     915                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
     916                 :          0 :                 rte_errno = EINVAL;
     917                 :          0 :                 return -1;
     918                 :            :         }
     919                 :            :         /* protect memory configuration while setting up IOMMU/DMA */
     920                 :          0 :         rte_mcfg_mem_read_lock();
     921                 :            : 
     922      [ #  #  # ]:          0 :         switch (vfio_global_cfg.mode) {
     923                 :          0 :         case DEV_VFIO_MODE_GROUP:
     924                 :          0 :                 res = vfio_group_assign_device(cfg, sysfs_base, dev_addr, &dev);
     925                 :          0 :                 break;
     926                 :          0 :         case DEV_VFIO_MODE_CDEV:
     927                 :          0 :                 res = vfio_cdev_assign_device(cfg, sysfs_base, dev_addr, &dev);
     928                 :          0 :                 break;
     929                 :          0 :         default:
     930                 :          0 :                 EAL_LOG(ERR, "Unsupported VFIO mode");
     931                 :            :                 res = VFIO_NOT_SUPPORTED;
     932                 :            :                 break;
     933                 :            :         }
     934                 :          0 :         rte_mcfg_mem_read_unlock();
     935                 :            : 
     936   [ #  #  #  #  :          0 :         switch (res) {
                   #  # ]
     937                 :            :         case VFIO_SUCCESS:
     938                 :            :                 return 0;
     939                 :          0 :         case VFIO_EXISTS:
     940                 :          0 :                 rte_errno = EEXIST;
     941                 :          0 :                 return -1;
     942                 :          0 :         case VFIO_NOT_MANAGED:
     943                 :          0 :                 EAL_LOG(DEBUG, "Device %s not managed by VFIO", dev_addr);
     944                 :          0 :                 rte_errno = ENODEV;
     945                 :          0 :                 return -1;
     946                 :          0 :         case VFIO_NO_SPACE:
     947                 :          0 :                 EAL_LOG(ERR, "No space in VFIO container to assign device %s", dev_addr);
     948                 :          0 :                 rte_errno = ENOSPC;
     949                 :          0 :                 return -1;
     950                 :          0 :         case VFIO_NO_MEM:
     951                 :          0 :                 EAL_LOG(ERR, "Memory allocation failed for device %s", dev_addr);
     952                 :          0 :                 rte_errno = ENOMEM;
     953                 :          0 :                 return -1;
     954                 :          0 :         default:
     955                 :          0 :                 EAL_LOG(ERR, "Error assigning device %s to container", dev_addr);
     956                 :          0 :                 rte_errno = EIO;
     957                 :          0 :                 return -1;
     958                 :            :         }
     959                 :            : }
     960                 :            : 
     961                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_setup_device)
     962                 :            : int
     963                 :          0 : dev_vfio_setup_device(const char *sysfs_base, const char *dev_addr, int *vfio_dev_fd)
     964                 :            : {
     965                 :            :         struct vfio_container *cfg;
     966                 :            :         struct vfio_device *dev;
     967                 :            :         enum vfio_result res;
     968                 :            :         int ret;
     969                 :            : 
     970   [ #  #  #  # ]:          0 :         if (sysfs_base == NULL || dev_addr == NULL || vfio_dev_fd == NULL) {
     971                 :          0 :                 rte_errno = EINVAL;
     972                 :          0 :                 return -1;
     973                 :            :         }
     974                 :            : 
     975         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
     976                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
     977                 :          0 :                 rte_errno = ENXIO;
     978                 :          0 :                 return -1;
     979                 :            :         }
     980                 :            : 
     981                 :          0 :         rte_mcfg_mem_read_lock();
     982                 :            : 
     983      [ #  #  # ]:          0 :         switch (vfio_global_cfg.mode) {
     984                 :          0 :         case DEV_VFIO_MODE_GROUP:
     985                 :            :         {
     986                 :            :                 int iommu_group_num;
     987                 :            : 
     988                 :            :                 /* find group number */
     989                 :          0 :                 ret = vfio_group_get_num(sysfs_base, dev_addr, &iommu_group_num);
     990         [ #  # ]:          0 :                 if (ret < 0)
     991                 :          0 :                         goto assign_fail;
     992         [ #  # ]:          0 :                 else if (ret == 0)
     993                 :          0 :                         goto not_managed;
     994                 :            : 
     995                 :            :                 /* find config by group */
     996                 :          0 :                 cfg = vfio_container_get_by_group_num(iommu_group_num);
     997         [ #  # ]:          0 :                 if (cfg == NULL)
     998                 :          0 :                         cfg = vfio_global_cfg.default_cfg;
     999                 :            : 
    1000                 :          0 :                 res = vfio_group_assign_device(cfg, sysfs_base, dev_addr, &dev);
    1001                 :          0 :                 break;
    1002                 :            :         }
    1003                 :          0 :         case DEV_VFIO_MODE_CDEV:
    1004                 :            :         {
    1005                 :            :                 int dev_num;
    1006                 :            : 
    1007                 :            :                 /* find device number */
    1008                 :          0 :                 ret = vfio_cdev_get_device_num(sysfs_base, dev_addr, &dev_num);
    1009         [ #  # ]:          0 :                 if (ret < 0)
    1010                 :          0 :                         goto assign_fail;
    1011         [ #  # ]:          0 :                 else if (ret == 0)
    1012                 :          0 :                         goto not_managed;
    1013                 :            : 
    1014                 :          0 :                 cfg = vfio_container_get_by_dev_num(dev_num);
    1015         [ #  # ]:          0 :                 if (cfg == NULL)
    1016                 :          0 :                         cfg = vfio_global_cfg.default_cfg;
    1017                 :            : 
    1018                 :          0 :                 res = vfio_cdev_assign_device(cfg, sysfs_base, dev_addr, &dev);
    1019                 :          0 :                 break;
    1020                 :            :         }
    1021                 :          0 :         default:
    1022                 :          0 :                 EAL_LOG(ERR, "Unsupported VFIO mode");
    1023                 :          0 :                 rte_errno = ENOTSUP;
    1024                 :            :                 ret = -1;
    1025                 :          0 :                 goto unlock;
    1026                 :            :         }
    1027                 :            : 
    1028   [ #  #  #  #  :          0 :         switch (res) {
                      # ]
    1029                 :            :         case VFIO_NOT_MANAGED:
    1030                 :          0 : not_managed:
    1031                 :          0 :                 EAL_LOG(DEBUG, "Device %s not managed by VFIO", dev_addr);
    1032                 :          0 :                 rte_errno = ENODEV;
    1033                 :            :                 ret = -1;
    1034                 :          0 :                 goto unlock;
    1035                 :            :         case VFIO_SUCCESS:
    1036                 :            :         case VFIO_EXISTS:
    1037                 :            :                 break;
    1038                 :          0 :         case VFIO_NO_SPACE:
    1039                 :          0 :                 EAL_LOG(ERR, "No space in VFIO container to assign device %s", dev_addr);
    1040                 :          0 :                 rte_errno = ENOSPC;
    1041                 :            :                 ret = -1;
    1042                 :          0 :                 goto unlock;
    1043                 :          0 :         case VFIO_NO_MEM:
    1044                 :          0 :                 EAL_LOG(ERR, "Memory allocation failed for device %s", dev_addr);
    1045                 :          0 :                 rte_errno = ENOMEM;
    1046                 :            :                 ret = -1;
    1047                 :          0 :                 goto unlock;
    1048                 :            :         default:
    1049                 :          0 : assign_fail:
    1050                 :          0 :                 EAL_LOG(ERR, "Error assigning device %s to container", dev_addr);
    1051                 :          0 :                 rte_errno = EIO;
    1052                 :            :                 ret = -1;
    1053                 :          0 :                 goto unlock;
    1054                 :            :         }
    1055                 :          0 :         *vfio_dev_fd = dev->fd;
    1056                 :            : 
    1057                 :            :         /* success */
    1058                 :            :         ret = 0;
    1059                 :            : 
    1060                 :          0 : unlock:
    1061                 :          0 :         rte_mcfg_mem_read_unlock();
    1062                 :            : 
    1063                 :          0 :         return ret;
    1064                 :            : }
    1065                 :            : 
    1066                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_release_device)
    1067                 :            : int
    1068                 :          0 : dev_vfio_release_device(const char *sysfs_base __rte_unused, const char *dev_addr, int vfio_dev_fd)
    1069                 :            : {
    1070                 :            :         struct vfio_container *cfg = NULL, *icfg;
    1071                 :            :         struct vfio_device *dev = NULL, *idev;
    1072                 :            :         int ret;
    1073                 :            : 
    1074         [ #  # ]:          0 :         if (sysfs_base == NULL || dev_addr == NULL) {
    1075                 :          0 :                 rte_errno = EINVAL;
    1076                 :          0 :                 return -1;
    1077                 :            :         }
    1078                 :            : 
    1079         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1080                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1081                 :          0 :                 rte_errno = ENXIO;
    1082                 :          0 :                 return -1;
    1083                 :            :         }
    1084                 :            : 
    1085                 :          0 :         rte_mcfg_mem_read_lock();
    1086                 :            : 
    1087                 :            :         /* we need to find both config and device */
    1088   [ #  #  #  # ]:          0 :         VFIO_CONTAINER_FOREACH_ACTIVE(icfg) {
    1089   [ #  #  #  # ]:          0 :                 VFIO_DEVICE_FOREACH_ACTIVE(icfg, idev) {
    1090         [ #  # ]:          0 :                         if (idev->fd != vfio_dev_fd)
    1091                 :          0 :                                 continue;
    1092                 :            :                         cfg = icfg;
    1093                 :            :                         dev = idev;
    1094                 :          0 :                         goto found;
    1095                 :            :                 }
    1096                 :            :         }
    1097                 :          0 : found:
    1098         [ #  # ]:          0 :         if (dev == NULL) {
    1099                 :          0 :                 EAL_LOG(ERR, "Device %s not managed by any container", dev_addr);
    1100                 :          0 :                 rte_errno = ENOENT;
    1101                 :            :                 ret = -1;
    1102                 :          0 :                 goto unlock;
    1103                 :            :         }
    1104                 :            : 
    1105      [ #  #  # ]:          0 :         switch (vfio_global_cfg.mode) {
    1106                 :          0 :         case DEV_VFIO_MODE_GROUP:
    1107                 :            :         {
    1108                 :          0 :                 int iommu_group_num = dev->group;
    1109                 :            :                 struct vfio_group_config *group_cfg = &cfg->group_cfg;
    1110                 :            :                 struct vfio_group *grp;
    1111                 :            : 
    1112   [ #  #  #  # ]:          0 :                 bool need_clb = vfio_container_is_default(cfg) &&
    1113                 :          0 :                         rte_eal_process_type() == RTE_PROC_PRIMARY;
    1114                 :            : 
    1115                 :            :                 /* find the group */
    1116                 :          0 :                 grp = vfio_group_get_by_num(cfg, iommu_group_num);
    1117         [ #  # ]:          0 :                 if (grp == NULL) {
    1118                 :            :                         /* shouldn't happen because we already know the device is valid */
    1119                 :          0 :                         EAL_LOG(ERR, "IOMMU group %d not found in container", iommu_group_num);
    1120                 :          0 :                         rte_errno = EIO;
    1121                 :            :                         ret = -1;
    1122                 :          0 :                         goto unlock;
    1123                 :            :                 }
    1124                 :            : 
    1125                 :            :                 /* close device handle */
    1126                 :          0 :                 vfio_device_erase(cfg, dev);
    1127                 :            : 
    1128                 :            :                 /* remove device from group */
    1129                 :          0 :                 grp->n_devices--;
    1130                 :            : 
    1131                 :            :                 /* was this the last device? */
    1132         [ #  # ]:          0 :                 if (grp->n_devices == 0)
    1133                 :          0 :                         vfio_group_erase(cfg, grp);
    1134                 :            : 
    1135                 :            :                 /* if no more groups left, remove callback */
    1136   [ #  #  #  #  :          0 :                 if (need_clb && group_cfg->n_groups == 0 && group_cfg->mem_event_clb_set) {
                   #  # ]
    1137                 :          0 :                         rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME, NULL);
    1138                 :          0 :                         group_cfg->mem_event_clb_set = false;
    1139                 :            :                 }
    1140                 :            :                 break;
    1141                 :            :         }
    1142                 :          0 :         case DEV_VFIO_MODE_CDEV:
    1143                 :            :         {
    1144                 :            :                 /* for cdev, just erase the device and we're done */
    1145                 :          0 :                 vfio_device_erase(cfg, dev);
    1146                 :          0 :                 break;
    1147                 :            :         }
    1148                 :          0 :         default:
    1149                 :          0 :                 EAL_LOG(ERR, "Unsupported VFIO mode");
    1150                 :          0 :                 rte_errno = ENOTSUP;
    1151                 :            :                 ret = -1;
    1152                 :          0 :                 goto unlock;
    1153                 :            :         }
    1154                 :            :         ret = 0;
    1155                 :          0 : unlock:
    1156                 :          0 :         rte_mcfg_mem_read_unlock();
    1157                 :            : 
    1158                 :          0 :         return ret;
    1159                 :            : }
    1160                 :            : 
    1161                 :            : static int
    1162                 :          0 : vfio_sync_mode(struct vfio_container *cfg, enum dev_vfio_mode *mode)
    1163                 :            : {
    1164                 :            :         struct vfio_mp_param *p;
    1165                 :          0 :         struct rte_mp_msg mp_req = {0};
    1166                 :          0 :         struct rte_mp_reply mp_reply = {0};
    1167                 :          0 :         struct timespec ts = {5, 0};
    1168                 :            : 
    1169                 :            :         /* request container from primary via mp_sync */
    1170                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
    1171                 :          0 :         mp_req.len_param = sizeof(*p);
    1172                 :          0 :         mp_req.num_fds = 0;
    1173                 :            :         p = (struct vfio_mp_param *)mp_req.param;
    1174                 :          0 :         p->req = VFIO_SOCKET_REQ_CONTAINER;
    1175                 :            : 
    1176   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
    1177                 :          0 :                 struct rte_mp_msg *mp_rep = &mp_reply.msgs[0];
    1178                 :            : 
    1179                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
    1180   [ #  #  #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK && mp_rep->num_fds == 1) {
    1181                 :          0 :                         cfg->container_fd = mp_rep->fds[0];
    1182                 :          0 :                         *mode = p->mode;
    1183                 :          0 :                         free(mp_reply.msgs);
    1184                 :          0 :                         return 0;
    1185                 :            :                 }
    1186                 :            :         }
    1187                 :            : 
    1188                 :          0 :         free(mp_reply.msgs);
    1189                 :          0 :         EAL_LOG(ERR, "Cannot request container_fd");
    1190                 :          0 :         return -1;
    1191                 :            : }
    1192                 :            : 
    1193                 :            : static int
    1194                 :          0 : vfio_sync_iova_mode(enum dev_vfio_iova_mode *iova_mode)
    1195                 :            : {
    1196                 :            :         struct vfio_mp_param *p;
    1197                 :          0 :         struct rte_mp_msg mp_req = {0};
    1198                 :          0 :         struct rte_mp_reply mp_reply = {0};
    1199                 :          0 :         struct timespec ts = {5, 0};
    1200                 :            : 
    1201                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
    1202                 :          0 :         mp_req.len_param = sizeof(*p);
    1203                 :          0 :         mp_req.num_fds = 0;
    1204                 :            :         p = (struct vfio_mp_param *)mp_req.param;
    1205                 :          0 :         p->req = VFIO_SOCKET_REQ_IOVA_MODE;
    1206                 :            : 
    1207   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
    1208                 :          0 :                 struct rte_mp_msg *mp_rep = &mp_reply.msgs[0];
    1209                 :            : 
    1210                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
    1211         [ #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK) {
    1212                 :          0 :                         *iova_mode = p->iova_mode;
    1213                 :          0 :                         free(mp_reply.msgs);
    1214                 :          0 :                         return 0;
    1215                 :            :                 }
    1216                 :            :         }
    1217                 :            : 
    1218                 :          0 :         free(mp_reply.msgs);
    1219                 :          0 :         EAL_LOG(ERR, "Cannot request VFIO IOMMU mode");
    1220                 :          0 :         return -1;
    1221                 :            : }
    1222                 :            : 
    1223                 :            : static enum dev_vfio_mode
    1224                 :          0 : vfio_select_mode(void)
    1225                 :            : {
    1226                 :            :         struct vfio_container *cfg;
    1227                 :          0 :         enum dev_vfio_mode mode = DEV_VFIO_MODE_NONE;
    1228                 :          0 :         enum dev_vfio_iova_mode iova_mode = DEV_VFIO_IOVA_MODE_UNKNOWN;
    1229                 :            :         int ret;
    1230                 :            : 
    1231                 :            :         cfg = vfio_container_create();
    1232                 :            :         /* cannot happen */
    1233   [ #  #  #  # ]:          0 :         if (cfg == NULL || cfg != vfio_global_cfg.default_cfg) {
    1234                 :          0 :                 EAL_LOG(ERR, "Unexpected VFIO config structure");
    1235                 :          0 :                 return DEV_VFIO_MODE_NONE;
    1236                 :            :         }
    1237                 :            : 
    1238                 :            :         /* for secondary, just ask the primary for the container and mode */
    1239         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
    1240   [ #  #  #  # ]:          0 :                 if (vfio_sync_mode(cfg, &mode) < 0 || vfio_sync_iova_mode(&iova_mode) < 0)
    1241                 :          0 :                         goto err;
    1242                 :          0 :                 vfio_global_cfg.iova_mode = iova_mode;
    1243                 :            : 
    1244         [ #  # ]:          0 :                 if (mode == DEV_VFIO_MODE_CDEV) {
    1245                 :            :                         /* set up ops and sync IOAS */
    1246                 :          0 :                         vfio_cdev_setup_ops();
    1247         [ #  # ]:          0 :                         if (vfio_cdev_sync_ioas(cfg) < 0)
    1248                 :          0 :                                 goto err;
    1249                 :            : 
    1250                 :            :                         /* primary handles DMA */
    1251                 :          0 :                         cfg->dma_setup_done = true;
    1252         [ #  # ]:          0 :                 } else if (mode == DEV_VFIO_MODE_GROUP) {
    1253                 :            :                         /* primary handles DMA */
    1254                 :          0 :                         cfg->dma_setup_done = true;
    1255                 :            :                 } else {
    1256                 :            :                         /* shouldn't happen */
    1257                 :          0 :                         EAL_LOG(ERR, "Unknown VFIO mode %d", mode);
    1258                 :          0 :                         goto err;
    1259                 :            :                 }
    1260                 :            : 
    1261                 :          0 :                 return mode;
    1262                 :            :         }
    1263                 :            :         /* if we failed mp sync setup, we cannot initialize VFIO */
    1264         [ #  # ]:          0 :         if (vfio_mp_sync_setup() < 0)
    1265                 :            :                 return DEV_VFIO_MODE_NONE;
    1266                 :            : 
    1267                 :            :         /* check no-IOMMU mode */
    1268                 :          0 :         ret = vfio_noiommu_is_enabled();
    1269         [ #  # ]:          0 :         if (ret < 0)
    1270                 :          0 :                 goto err_mpsync;
    1271         [ #  # ]:          0 :         iova_mode = ret == 1 ? DEV_VFIO_IOVA_MODE_PA : DEV_VFIO_IOVA_MODE_VA;
    1272                 :            : 
    1273                 :            :         /* try group mode first */
    1274         [ #  # ]:          0 :         if (vfio_group_enable(cfg) == 0) {
    1275                 :          0 :                 vfio_global_cfg.iova_mode = iova_mode;
    1276                 :          0 :                 return DEV_VFIO_MODE_GROUP;
    1277                 :            :         }
    1278                 :          0 :         EAL_LOG(DEBUG, "VFIO group mode not available, trying cdev mode...");
    1279                 :            :         /* cdev ops, IOAS and DMA setup are deferred to dev_vfio_init_mem() */
    1280         [ #  # ]:          0 :         if (vfio_cdev_enable(cfg) == 0) {
    1281                 :          0 :                 vfio_global_cfg.iova_mode = iova_mode;
    1282                 :          0 :                 return DEV_VFIO_MODE_CDEV;
    1283                 :            :         }
    1284                 :          0 :         EAL_LOG(DEBUG, "VFIO cdev mode not available");
    1285                 :          0 : err_mpsync:
    1286                 :          0 :         vfio_mp_sync_cleanup();
    1287                 :          0 : err:
    1288                 :          0 :         vfio_container_erase(cfg);
    1289                 :          0 :         vfio_global_cfg.iova_mode = DEV_VFIO_IOVA_MODE_UNKNOWN;
    1290                 :            :         /* reset ops as well */
    1291                 :          0 :         vfio_global_cfg.ops = NULL;
    1292                 :            : 
    1293                 :          0 :         return DEV_VFIO_MODE_NONE;
    1294                 :            : }
    1295                 :            : 
    1296                 :            : static const char *
    1297                 :            : vfio_mode_to_str(enum dev_vfio_mode mode)
    1298                 :            : {
    1299      [ #  #  # ]:          0 :         switch (mode) {
    1300                 :            :         case DEV_VFIO_MODE_GROUP: return "group";
    1301                 :          0 :         case DEV_VFIO_MODE_CDEV: return "cdev";
    1302                 :          0 :         default: return "not initialized";
    1303                 :            :         }
    1304                 :            : }
    1305                 :            : 
    1306                 :            : static const char *
    1307                 :            : vfio_iova_mode_to_str(enum dev_vfio_iova_mode iova_mode)
    1308                 :            : {
    1309                 :          0 :         switch (iova_mode) {
    1310                 :            :         case DEV_VFIO_IOVA_MODE_VA: return "VA";
    1311                 :          0 :         case DEV_VFIO_IOVA_MODE_PA: return "PA";
    1312                 :          0 :         default: return "unknown";
    1313                 :            :         }
    1314                 :            : }
    1315                 :            : 
    1316                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_enable)
    1317                 :            : int
    1318                 :        229 : dev_vfio_enable(void)
    1319                 :            : {
    1320                 :            :         int vfio_available;
    1321                 :            :         enum dev_vfio_mode mode = DEV_VFIO_MODE_NONE;
    1322                 :            : 
    1323                 :            :         /* if already enabled, nothing to do */
    1324         [ +  - ]:        229 :         if (vfio_global_cfg.mode != DEV_VFIO_MODE_NONE)
    1325                 :            :                 return 0;
    1326                 :            : 
    1327                 :        229 :         EAL_LOG(DEBUG, "Probing VFIO support...");
    1328                 :            : 
    1329                 :            :         /* check if vfio module is loaded */
    1330                 :        229 :         vfio_available = vfio_check_module(DEV_VFIO_MODULE_VFIO);
    1331                 :            : 
    1332                 :            :         /* return error directly */
    1333         [ -  + ]:        229 :         if (vfio_available == -1) {
    1334                 :          0 :                 EAL_LOG(INFO, "Could not get loaded module details!");
    1335                 :          0 :                 return -1;
    1336                 :            :         }
    1337                 :            : 
    1338                 :            :         /* return 0 if VFIO modules not loaded */
    1339         [ +  - ]:        229 :         if (vfio_available == 0) {
    1340                 :        229 :                 EAL_LOG(DEBUG, "VFIO modules not loaded, skipping VFIO support...");
    1341                 :        229 :                 return 0;
    1342                 :            :         }
    1343                 :          0 :         EAL_LOG(DEBUG, "VFIO module 'vfio' loaded, attempting to initialize VFIO...");
    1344                 :          0 :         mode = vfio_select_mode();
    1345                 :            : 
    1346                 :            :         /* have we initialized anything? */
    1347         [ #  # ]:          0 :         if (mode == DEV_VFIO_MODE_NONE)
    1348                 :          0 :                 EAL_LOG(NOTICE, "VFIO support could not be initialized");
    1349                 :            :         else
    1350      [ #  #  # ]:          0 :                 EAL_LOG(NOTICE, "VFIO support initialized: %s mode, IOVA as %s",
    1351                 :            :                         vfio_mode_to_str(mode), vfio_iova_mode_to_str(vfio_global_cfg.iova_mode));
    1352                 :            : 
    1353                 :          0 :         vfio_global_cfg.mode = mode;
    1354                 :            : 
    1355                 :          0 :         return 0;
    1356                 :            : }
    1357                 :            : 
    1358                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_module_is_loaded)
    1359                 :            : int
    1360                 :          0 : dev_vfio_module_is_loaded(enum dev_vfio_module module)
    1361                 :            : {
    1362                 :          0 :         return vfio_check_module(module) > 0;
    1363                 :            : }
    1364                 :            : 
    1365                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_init_mem)
    1366                 :            : int
    1367                 :        224 : dev_vfio_init_mem(void)
    1368                 :            : {
    1369                 :        224 :         struct vfio_container *cfg = vfio_global_cfg.default_cfg;
    1370                 :            : 
    1371                 :            :         /* secondary already synced everything during dev_vfio_enable() */
    1372         [ +  + ]:        224 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1373                 :            :                 return 0;
    1374                 :            : 
    1375         [ -  + ]:        194 :         switch (vfio_global_cfg.mode) {
    1376                 :          0 :         case DEV_VFIO_MODE_CDEV:
    1377                 :            :                 /* iommufd IOAS is populated once DPDK memory is available */
    1378                 :          0 :                 vfio_cdev_setup_ops();
    1379         [ #  # ]:          0 :                 if (vfio_cdev_setup_ioas(cfg) < 0)
    1380                 :            :                         return -1;
    1381         [ #  # ]:          0 :                 if (vfio_setup_dma_mem(cfg) < 0)
    1382                 :            :                         return -1;
    1383         [ #  # ]:          0 :                 if (vfio_register_mem_event_callback() < 0)
    1384                 :            :                         return -1;
    1385                 :          0 :                 cfg->dma_setup_done = true;
    1386                 :          0 :                 break;
    1387                 :            :         default:
    1388                 :            :                 /* group/noiommu map memory at device attach, or no VFIO */
    1389                 :            :                 break;
    1390                 :            :         }
    1391                 :            : 
    1392                 :            :         return 0;
    1393                 :            : }
    1394                 :            : 
    1395                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_is_enabled)
    1396                 :            : int
    1397                 :          0 : dev_vfio_is_enabled(void)
    1398                 :            : {
    1399                 :          0 :         return vfio_global_cfg.default_cfg->active;
    1400                 :            : }
    1401                 :            : 
    1402                 :            : int
    1403                 :          0 : vfio_get_iommu_type(void)
    1404                 :            : {
    1405         [ #  # ]:          0 :         if (vfio_global_cfg.ops == NULL)
    1406                 :            :                 return -1;
    1407                 :            : 
    1408                 :          0 :         return vfio_global_cfg.ops->type_id;
    1409                 :            : }
    1410                 :            : 
    1411                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_device_info)
    1412                 :            : int
    1413                 :          0 : dev_vfio_get_device_info(int vfio_dev_fd, struct vfio_device_info *device_info)
    1414                 :            : {
    1415                 :            :         int ret;
    1416                 :            : 
    1417         [ #  # ]:          0 :         if (device_info == NULL) {
    1418                 :          0 :                 rte_errno = EINVAL;
    1419                 :          0 :                 return -1;
    1420                 :            :         }
    1421                 :            : 
    1422         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1423                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1424                 :          0 :                 rte_errno = ENXIO;
    1425                 :          0 :                 return -1;
    1426                 :            :         }
    1427                 :            : 
    1428                 :          0 :         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
    1429         [ #  # ]:          0 :         if (ret) {
    1430                 :          0 :                 EAL_LOG(ERR, "Cannot get device info, error %i (%s)", errno, strerror(errno));
    1431                 :          0 :                 rte_errno = errno;
    1432                 :          0 :                 return -1;
    1433                 :            :         }
    1434                 :            : 
    1435                 :            :         return 0;
    1436                 :            : }
    1437                 :            : 
    1438                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_container_fd)
    1439                 :            : int
    1440                 :          0 : dev_vfio_get_container_fd(void)
    1441                 :            : {
    1442                 :            :         /* Return the default container fd if VFIO is enabled.
    1443                 :            :          * The default container is set up during dev_vfio_enable().
    1444                 :            :          * This function does not create a new container.
    1445                 :            :          */
    1446         [ #  # ]:          0 :         if (vfio_global_cfg.mode != DEV_VFIO_MODE_NONE)
    1447                 :          0 :                 return vfio_global_cfg.default_cfg->container_fd;
    1448                 :            : 
    1449                 :          0 :         EAL_LOG(ERR, "VFIO support not initialized");
    1450                 :          0 :         rte_errno = ENXIO;
    1451                 :          0 :         return -1;
    1452                 :            : }
    1453                 :            : 
    1454                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_group_num)
    1455                 :            : int
    1456                 :          0 : dev_vfio_get_group_num(const char *sysfs_base, const char *dev_addr, int *iommu_group_num)
    1457                 :            : {
    1458                 :            :         int ret;
    1459                 :            : 
    1460   [ #  #  #  # ]:          0 :         if (sysfs_base == NULL || dev_addr == NULL || iommu_group_num == NULL) {
    1461                 :          0 :                 rte_errno = EINVAL;
    1462                 :          0 :                 return -1;
    1463                 :            :         }
    1464                 :            : 
    1465         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1466                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1467                 :          0 :                 rte_errno = ENXIO;
    1468                 :          0 :                 return -1;
    1469                 :            :         }
    1470         [ #  # ]:          0 :         if (vfio_global_cfg.mode != DEV_VFIO_MODE_GROUP) {
    1471                 :          0 :                 EAL_LOG(ERR, "VFIO not initialized in group mode");
    1472                 :          0 :                 rte_errno = ENOTSUP;
    1473                 :          0 :                 return -1;
    1474                 :            :         }
    1475                 :          0 :         ret = vfio_group_get_num(sysfs_base, dev_addr, iommu_group_num);
    1476         [ #  # ]:          0 :         if (ret < 0) {
    1477                 :          0 :                 rte_errno = EINVAL;
    1478                 :          0 :                 return -1;
    1479         [ #  # ]:          0 :         } else if (ret == 0) {
    1480                 :          0 :                 rte_errno = ENODEV;
    1481                 :          0 :                 return -1;
    1482                 :            :         }
    1483                 :            :         return 0;
    1484                 :            : }
    1485                 :            : 
    1486                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_device_num)
    1487                 :            : int
    1488                 :          0 : dev_vfio_get_device_num(const char *sysfs_base, const char *dev_addr, int *device_num)
    1489                 :            : {
    1490                 :            :         int ret;
    1491                 :            : 
    1492   [ #  #  #  # ]:          0 :         if (sysfs_base == NULL || dev_addr == NULL || device_num == NULL) {
    1493                 :          0 :                 rte_errno = EINVAL;
    1494                 :          0 :                 return -1;
    1495                 :            :         }
    1496                 :            : 
    1497         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1498                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1499                 :          0 :                 rte_errno = ENXIO;
    1500                 :          0 :                 return -1;
    1501                 :            :         }
    1502                 :            : 
    1503         [ #  # ]:          0 :         if (vfio_global_cfg.mode != DEV_VFIO_MODE_CDEV) {
    1504                 :          0 :                 EAL_LOG(ERR, "VFIO not initialized in cdev mode");
    1505                 :          0 :                 rte_errno = ENOTSUP;
    1506                 :          0 :                 return -1;
    1507                 :            :         }
    1508                 :            : 
    1509                 :          0 :         ret = vfio_cdev_get_device_num(sysfs_base, dev_addr, device_num);
    1510         [ #  # ]:          0 :         if (ret < 0) {
    1511                 :          0 :                 rte_errno = EINVAL;
    1512                 :          0 :                 return -1;
    1513         [ #  # ]:          0 :         } else if (ret == 0) {
    1514                 :          0 :                 rte_errno = ENODEV;
    1515                 :          0 :                 return -1;
    1516                 :            :         }
    1517                 :            :         return 0;
    1518                 :            : }
    1519                 :            : 
    1520                 :            : static int
    1521                 :          0 : vfio_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len,
    1522                 :            :                 int do_map)
    1523                 :            : {
    1524                 :          0 :         const struct vfio_iommu_ops *t = vfio_global_cfg.ops;
    1525                 :            : 
    1526         [ #  # ]:          0 :         if (!t) {
    1527                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1528                 :          0 :                 return -1;
    1529                 :            :         }
    1530                 :            : 
    1531         [ #  # ]:          0 :         if (!t->dma_user_map_func) {
    1532                 :          0 :                 EAL_LOG(ERR, "VFIO custom DMA region mapping not supported by IOMMU %s", t->name);
    1533                 :          0 :                 return -1;
    1534                 :            :         }
    1535                 :            : 
    1536                 :          0 :         return t->dma_user_map_func(cfg, vaddr, iova, len, do_map);
    1537                 :            : }
    1538                 :            : 
    1539                 :            : static int
    1540                 :          0 : container_dma_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len)
    1541                 :            : {
    1542                 :            :         struct vfio_user_mem_map *new_map;
    1543                 :            :         struct vfio_user_mem_maps *user_mem_maps;
    1544                 :            :         bool has_partial_unmap;
    1545                 :            :         int ret = 0;
    1546                 :            : 
    1547                 :          0 :         user_mem_maps = &cfg->mem_maps;
    1548                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1549         [ #  # ]:          0 :         if (user_mem_maps->n_maps == RTE_DIM(user_mem_maps->maps)) {
    1550                 :          0 :                 EAL_LOG(ERR, "No more space for user mem maps");
    1551                 :            :                 ret = -1;
    1552                 :          0 :                 goto out;
    1553                 :            :         }
    1554                 :            :         /* map the entry */
    1555         [ #  # ]:          0 :         if (vfio_dma_mem_map(cfg, vaddr, iova, len, 1)) {
    1556                 :            :                 /* technically, this will fail if there are currently no devices
    1557                 :            :                  * plugged in, even if a device were added later, this mapping
    1558                 :            :                  * might have succeeded. however, since we cannot verify if this
    1559                 :            :                  * is a valid mapping without having a device attached, consider
    1560                 :            :                  * this to be unsupported, because we can't just store any old
    1561                 :            :                  * mapping and pollute list of active mappings willy-nilly.
    1562                 :            :                  */
    1563                 :          0 :                 EAL_LOG(ERR, "Couldn't map new region for DMA");
    1564                 :            :                 ret = -1;
    1565                 :          0 :                 goto out;
    1566                 :            :         }
    1567                 :            :         /* do we have partial unmap support? */
    1568                 :          0 :         has_partial_unmap = vfio_global_cfg.ops->partial_unmap;
    1569                 :            : 
    1570                 :            :         /* create new user mem map entry */
    1571                 :          0 :         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
    1572                 :          0 :         new_map->addr = vaddr;
    1573                 :          0 :         new_map->iova = iova;
    1574                 :          0 :         new_map->len = len;
    1575                 :            :         /* for IOMMU types supporting partial unmap, we don't need chunking */
    1576         [ #  # ]:          0 :         new_map->chunk = has_partial_unmap ? 0 : len;
    1577                 :            : 
    1578                 :          0 :         compact_user_maps(user_mem_maps);
    1579                 :          0 : out:
    1580                 :          0 :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    1581                 :          0 :         return ret;
    1582                 :            : }
    1583                 :            : 
    1584                 :            : static int
    1585                 :          0 : container_dma_unmap(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova, uint64_t len)
    1586                 :            : {
    1587                 :            :         struct vfio_user_mem_map orig_maps[RTE_DIM(cfg->mem_maps.maps)];
    1588                 :            :         struct vfio_user_mem_map new_maps[2]; /* can be at most 2 */
    1589                 :            :         struct vfio_user_mem_maps *user_mem_maps;
    1590                 :            :         int n_orig, n_new, ret = 0;
    1591                 :            :         bool has_partial_unmap;
    1592                 :            :         unsigned int newlen;
    1593                 :            : 
    1594                 :          0 :         user_mem_maps = &cfg->mem_maps;
    1595                 :          0 :         rte_spinlock_recursive_lock(&user_mem_maps->lock);
    1596                 :            : 
    1597                 :            :         /*
    1598                 :            :          * Previously, we had adjacent mappings entirely contained within one
    1599                 :            :          * mapping entry. Since we now store original mapping length in some
    1600                 :            :          * cases, this is no longer the case, so unmapping can potentially go
    1601                 :            :          * over multiple segments and split them in any number of ways.
    1602                 :            :          *
    1603                 :            :          * To complicate things further, some IOMMU types support arbitrary
    1604                 :            :          * partial unmapping, while others will only support unmapping along the
    1605                 :            :          * chunk size, so there are a lot of cases we need to handle. To make
    1606                 :            :          * things easier code wise, instead of trying to adjust existing
    1607                 :            :          * mappings, let's just rebuild them using information we have.
    1608                 :            :          */
    1609                 :            : 
    1610                 :            :         /*
    1611                 :            :          * first thing to do is check if there exists a mapping that includes
    1612                 :            :          * the start and the end of our requested unmap. We need to collect all
    1613                 :            :          * maps that include our unmapped region.
    1614                 :            :          */
    1615                 :          0 :         n_orig = find_user_mem_maps(user_mem_maps, vaddr, iova, len, orig_maps,
    1616                 :            :                 RTE_DIM(orig_maps));
    1617                 :            :         /* did we find anything? */
    1618         [ #  # ]:          0 :         if (n_orig < 0) {
    1619                 :          0 :                 EAL_LOG(ERR, "Couldn't find previously mapped region");
    1620                 :            :                 ret = -1;
    1621                 :          0 :                 goto out;
    1622                 :            :         }
    1623                 :            : 
    1624                 :            :         /* do we have partial unmap capability? */
    1625                 :          0 :         has_partial_unmap = vfio_global_cfg.ops->partial_unmap;
    1626                 :            : 
    1627                 :            :         /*
    1628                 :            :          * if we don't support partial unmap, we must check if start and end of
    1629                 :            :          * current unmap region are chunk-aligned.
    1630                 :            :          */
    1631         [ #  # ]:          0 :         if (!has_partial_unmap) {
    1632                 :            :                 bool start_aligned, end_aligned;
    1633                 :            : 
    1634                 :          0 :                 start_aligned = addr_is_chunk_aligned(orig_maps, n_orig, vaddr, iova);
    1635                 :          0 :                 end_aligned = addr_is_chunk_aligned(orig_maps, n_orig, vaddr + len, iova + len);
    1636                 :            : 
    1637         [ #  # ]:          0 :                 if (!start_aligned || !end_aligned) {
    1638                 :          0 :                         EAL_LOG(DEBUG, "DMA partial unmap unsupported");
    1639                 :            :                         ret = -1;
    1640                 :          0 :                         goto out;
    1641                 :            :                 }
    1642                 :            :         }
    1643                 :            : 
    1644                 :            :         /*
    1645                 :            :          * now we know we can potentially unmap the region, but we still have to
    1646                 :            :          * figure out if there is enough space in our list to store remaining
    1647                 :            :          * maps. for this, we will figure out how many segments we are going to
    1648                 :            :          * remove, and how many new segments we are going to create.
    1649                 :            :          */
    1650                 :          0 :         n_new = process_maps(orig_maps, n_orig, new_maps, vaddr, len);
    1651                 :            : 
    1652                 :            :         /* can we store the new maps in our list? */
    1653                 :          0 :         newlen = (user_mem_maps->n_maps - n_orig) + n_new;
    1654         [ #  # ]:          0 :         if (newlen >= RTE_DIM(user_mem_maps->maps)) {
    1655                 :          0 :                 EAL_LOG(ERR, "Not enough space to store partial mapping");
    1656                 :            :                 ret = -1;
    1657                 :          0 :                 goto out;
    1658                 :            :         }
    1659                 :            : 
    1660                 :            :         /* unmap the entry */
    1661         [ #  # ]:          0 :         if (vfio_dma_mem_map(cfg, vaddr, iova, len, 0)) {
    1662                 :            :                 /* there may not be any devices plugged in, so unmapping will
    1663                 :            :                  * fail, but that doesn't stop us from removing the mapping,
    1664                 :            :                  * as the assumption is we won't be needing this memory any
    1665                 :            :                  * more and thus will want to prevent it from being remapped
    1666                 :            :                  * again on hotplug. Ignore the error and proceed with
    1667                 :            :                  * removing the mapping from our records.
    1668                 :            :                  */
    1669                 :          0 :                 EAL_LOG(DEBUG, "DMA unmapping failed, but removing mappings anyway");
    1670                 :            :         }
    1671                 :            : 
    1672                 :            :         /* we have unmapped the region, so now update the maps */
    1673                 :          0 :         delete_maps(user_mem_maps, orig_maps, n_orig);
    1674                 :          0 :         copy_maps(user_mem_maps, new_maps, n_new);
    1675                 :          0 :         compact_user_maps(user_mem_maps);
    1676                 :          0 : out:
    1677                 :          0 :         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
    1678                 :          0 :         return ret;
    1679                 :            : }
    1680                 :            : 
    1681                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_container_create)
    1682                 :            : int
    1683                 :          0 : dev_vfio_container_create(void)
    1684                 :            : {
    1685                 :            :         struct vfio_container *cfg;
    1686                 :            :         int container_fd;
    1687                 :            : 
    1688         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1689                 :          0 :                 EAL_LOG(ERR, "VFIO not initialized");
    1690                 :          0 :                 rte_errno = ENXIO;
    1691                 :          0 :                 return -1;
    1692                 :            :         }
    1693                 :            :         cfg = vfio_container_create();
    1694         [ #  # ]:          0 :         if (cfg == NULL) {
    1695                 :          0 :                 EAL_LOG(ERR, "Reached VFIO container limit");
    1696                 :          0 :                 rte_errno = ENOSPC;
    1697                 :          0 :                 return -1;
    1698                 :            :         }
    1699                 :            : 
    1700      [ #  #  # ]:          0 :         switch (vfio_global_cfg.mode) {
    1701                 :          0 :         case DEV_VFIO_MODE_GROUP:
    1702                 :            :         {
    1703                 :          0 :                 container_fd = vfio_group_open_container_fd();
    1704         [ #  # ]:          0 :                 if (container_fd < 0) {
    1705                 :          0 :                         EAL_LOG(ERR, "Fail to create a new VFIO container");
    1706                 :          0 :                         rte_errno = EIO;
    1707                 :          0 :                         goto err;
    1708                 :            :                 }
    1709                 :          0 :                 cfg->container_fd = container_fd;
    1710                 :          0 :                 break;
    1711                 :            :         }
    1712                 :          0 :         case DEV_VFIO_MODE_CDEV:
    1713                 :            :         {
    1714                 :            :                 /* Open new iommufd for custom container */
    1715                 :          0 :                 container_fd = vfio_cdev_get_iommufd();
    1716         [ #  # ]:          0 :                 if (container_fd < 0) {
    1717                 :          0 :                         EAL_LOG(ERR, "Cannot open iommufd for cdev container");
    1718                 :          0 :                         rte_errno = EIO;
    1719                 :          0 :                         goto err;
    1720                 :            :                 }
    1721                 :          0 :                 cfg->container_fd = container_fd;
    1722                 :            : 
    1723                 :            :                 /* Set up IOAS for this container */
    1724         [ #  # ]:          0 :                 if (vfio_cdev_setup_ioas(cfg) < 0) {
    1725                 :          0 :                         EAL_LOG(ERR, "Cannot setup IOAS for cdev container");
    1726                 :          0 :                         rte_errno = EIO;
    1727                 :          0 :                         goto err;
    1728                 :            :                 }
    1729                 :            :                 /* we only need to set up IOAS for DMA to work */
    1730                 :          0 :                 cfg->dma_setup_done = true;
    1731                 :          0 :                 break;
    1732                 :            :         }
    1733                 :          0 :         default:
    1734                 :          0 :                 EAL_LOG(NOTICE, "Unsupported VFIO mode");
    1735                 :          0 :                 rte_errno = ENOTSUP;
    1736                 :          0 :                 goto err;
    1737                 :            :         }
    1738                 :            :         return container_fd;
    1739                 :          0 : err:
    1740                 :          0 :         vfio_container_erase(cfg);
    1741                 :          0 :         return -1;
    1742                 :            : }
    1743                 :            : 
    1744                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_container_destroy)
    1745                 :            : int
    1746                 :          0 : dev_vfio_container_destroy(int container_fd)
    1747                 :            : {
    1748                 :            :         struct vfio_container *cfg;
    1749                 :            :         struct vfio_device *dev;
    1750                 :            : 
    1751         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1752                 :          0 :                 EAL_LOG(ERR, "VFIO not initialized");
    1753                 :          0 :                 rte_errno = ENXIO;
    1754                 :          0 :                 return -1;
    1755                 :            :         }
    1756                 :            : 
    1757                 :            :         cfg = vfio_container_get_by_fd(container_fd);
    1758         [ #  # ]:          0 :         if (cfg == NULL) {
    1759                 :          0 :                 EAL_LOG(ERR, "VFIO container fd not managed by VFIO");
    1760                 :          0 :                 rte_errno = ENODEV;
    1761                 :          0 :                 return -1;
    1762                 :            :         }
    1763                 :            : 
    1764                 :            :         /* forbid destroying default container */
    1765         [ #  # ]:          0 :         if (vfio_container_is_default(cfg)) {
    1766                 :          0 :                 EAL_LOG(ERR, "Cannot destroy default VFIO container");
    1767                 :          0 :                 rte_errno = EINVAL;
    1768                 :          0 :                 return -1;
    1769                 :            :         }
    1770                 :            : 
    1771      [ #  #  # ]:          0 :         switch (vfio_global_cfg.mode) {
    1772                 :          0 :         case DEV_VFIO_MODE_GROUP: {
    1773                 :            :                 struct vfio_group *grp;
    1774                 :            : 
    1775                 :            :                 /* erase all devices */
    1776   [ #  #  #  # ]:          0 :                 VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev) {
    1777                 :          0 :                         EAL_LOG(DEBUG, "Device in IOMMU group %d still open, closing", dev->group);
    1778                 :            :                         /*
    1779                 :            :                          * technically we could've done back-reference lookup and closed our groups
    1780                 :            :                          * following a device close, but since we're closing and erasing all groups
    1781                 :            :                          * anyway, we can afford to not bother.
    1782                 :            :                          */
    1783                 :          0 :                         vfio_device_erase(cfg, dev);
    1784                 :            :                 }
    1785                 :            : 
    1786                 :            :                 /* erase all groups */
    1787   [ #  #  #  # ]:          0 :                 VFIO_GROUP_FOREACH_ACTIVE(cfg, grp) {
    1788                 :          0 :                         EAL_LOG(DEBUG, "IOMMU group %d still open, closing", grp->group_num);
    1789                 :          0 :                         vfio_group_erase(cfg, grp);
    1790                 :            :                 }
    1791                 :            :                 break;
    1792                 :            :         }
    1793                 :          0 :         case DEV_VFIO_MODE_CDEV:
    1794                 :            :                 /* erase all devices */
    1795   [ #  #  #  # ]:          0 :                 VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev) {
    1796                 :          0 :                         EAL_LOG(DEBUG, "Device vfio%d still open, closing", dev->dev_num);
    1797                 :          0 :                         vfio_device_erase(cfg, dev);
    1798                 :            :                 }
    1799                 :            :                 break;
    1800                 :          0 :         default:
    1801                 :          0 :                 EAL_LOG(ERR, "Unsupported VFIO mode");
    1802                 :          0 :                 rte_errno = ENOTSUP;
    1803                 :          0 :                 return -1;
    1804                 :            :         }
    1805                 :            : 
    1806                 :            :         /* erase entire config */
    1807                 :          0 :         vfio_container_erase(cfg);
    1808                 :            : 
    1809                 :          0 :         return 0;
    1810                 :            : }
    1811                 :            : 
    1812                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_container_dma_map)
    1813                 :            : int
    1814                 :          0 : dev_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova, uint64_t len)
    1815                 :            : {
    1816                 :            :         struct vfio_container *cfg;
    1817                 :            : 
    1818         [ #  # ]:          0 :         if (len == 0) {
    1819                 :          0 :                 rte_errno = EINVAL;
    1820                 :          0 :                 return -1;
    1821                 :            :         }
    1822                 :            : 
    1823         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1824                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1825                 :          0 :                 rte_errno = ENXIO;
    1826                 :          0 :                 return -1;
    1827                 :            :         }
    1828                 :            : 
    1829                 :            :         cfg = vfio_container_get_by_fd(container_fd);
    1830         [ #  # ]:          0 :         if (cfg == NULL) {
    1831                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    1832                 :          0 :                 rte_errno = EINVAL;
    1833                 :          0 :                 return -1;
    1834                 :            :         }
    1835                 :            : 
    1836         [ #  # ]:          0 :         if (container_dma_map(cfg, vaddr, iova, len) < 0) {
    1837                 :          0 :                 rte_errno = EIO;
    1838                 :          0 :                 return -1;
    1839                 :            :         }
    1840                 :            : 
    1841                 :            :         return 0;
    1842                 :            : }
    1843                 :            : 
    1844                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_container_dma_unmap)
    1845                 :            : int
    1846                 :          0 : dev_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova, uint64_t len)
    1847                 :            : {
    1848                 :            :         struct vfio_container *cfg;
    1849                 :            : 
    1850         [ #  # ]:          0 :         if (len == 0) {
    1851                 :          0 :                 rte_errno = EINVAL;
    1852                 :          0 :                 return -1;
    1853                 :            :         }
    1854                 :            : 
    1855         [ #  # ]:          0 :         if (vfio_global_cfg.mode == DEV_VFIO_MODE_NONE) {
    1856                 :          0 :                 EAL_LOG(ERR, "VFIO support not initialized");
    1857                 :          0 :                 rte_errno = ENXIO;
    1858                 :          0 :                 return -1;
    1859                 :            :         }
    1860                 :            : 
    1861                 :            :         cfg = vfio_container_get_by_fd(container_fd);
    1862         [ #  # ]:          0 :         if (cfg == NULL) {
    1863                 :          0 :                 EAL_LOG(ERR, "Invalid VFIO container fd");
    1864                 :          0 :                 rte_errno = EINVAL;
    1865                 :          0 :                 return -1;
    1866                 :            :         }
    1867                 :            : 
    1868         [ #  # ]:          0 :         if (container_dma_unmap(cfg, vaddr, iova, len) < 0) {
    1869                 :          0 :                 rte_errno = EIO;
    1870                 :          0 :                 return -1;
    1871                 :            :         }
    1872                 :            : 
    1873                 :            :         return 0;
    1874                 :            : }
    1875                 :            : 
    1876                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_mode)
    1877                 :            : enum dev_vfio_mode
    1878                 :          0 : dev_vfio_get_mode(void)
    1879                 :            : {
    1880                 :          0 :         return vfio_global_cfg.mode;
    1881                 :            : }
    1882                 :            : 
    1883                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_get_iova_mode)
    1884                 :            : enum dev_vfio_iova_mode
    1885                 :          0 : dev_vfio_get_iova_mode(void)
    1886                 :            : {
    1887                 :          0 :         return vfio_global_cfg.iova_mode;
    1888                 :            : }
    1889                 :            : 
    1890                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dev_vfio_cleanup)
    1891                 :            : void
    1892                 :        298 : dev_vfio_cleanup(void)
    1893                 :            : {
    1894                 :            :         struct vfio_container *cfg;
    1895                 :            : 
    1896                 :        298 :         vfio_mp_sync_cleanup();
    1897                 :            : 
    1898   [ -  +  +  + ]:      19370 :         VFIO_CONTAINER_FOREACH_ACTIVE(cfg) {
    1899                 :            :                 struct vfio_group_config *group_cfg = &cfg->group_cfg;
    1900                 :            :                 struct vfio_device *dev;
    1901                 :            :                 struct vfio_group *grp;
    1902                 :            : 
    1903   [ #  #  #  # ]:          0 :                 VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev)
    1904                 :          0 :                         vfio_device_erase(cfg, dev);
    1905   [ #  #  #  # ]:          0 :                 VFIO_GROUP_FOREACH_ACTIVE(cfg, grp)
    1906                 :          0 :                         vfio_group_erase(cfg, grp);
    1907                 :            : 
    1908                 :            :                 /* callback is only registered for the default container in the primary */
    1909         [ #  # ]:          0 :                 if (group_cfg->mem_event_clb_set) {
    1910                 :          0 :                         rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME, NULL);
    1911                 :          0 :                         group_cfg->mem_event_clb_set = false;
    1912                 :            :                 }
    1913                 :            : 
    1914                 :          0 :                 vfio_container_erase(cfg);
    1915                 :            :         }
    1916                 :        298 : }

Generated by: LCOV version 1.14