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