eal: hide shared memory config
[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;
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                 free(mp_reply.msgs);
324         }
325
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         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
416                 if (vfio_cfgs[i].vfio_container_fd == container_fd)
417                         return &vfio_cfgs[i];
418         }
419
420         return NULL;
421 }
422
423 int
424 rte_vfio_get_group_fd(int iommu_group_num)
425 {
426         struct vfio_config *vfio_cfg;
427
428         /* get the vfio_config it belongs to */
429         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
430         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
431
432         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
433 }
434
435 static int
436 get_vfio_group_idx(int vfio_group_fd)
437 {
438         struct vfio_config *vfio_cfg;
439         int i, j;
440
441         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
442                 vfio_cfg = &vfio_cfgs[i];
443                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
444                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
445                                 return j;
446         }
447
448         return -1;
449 }
450
451 static void
452 vfio_group_device_get(int vfio_group_fd)
453 {
454         struct vfio_config *vfio_cfg;
455         int i;
456
457         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
458         if (vfio_cfg == NULL) {
459                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
460                 return;
461         }
462
463         i = get_vfio_group_idx(vfio_group_fd);
464         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
465                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
466         else
467                 vfio_cfg->vfio_groups[i].devices++;
468 }
469
470 static void
471 vfio_group_device_put(int vfio_group_fd)
472 {
473         struct vfio_config *vfio_cfg;
474         int i;
475
476         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
477         if (vfio_cfg == NULL) {
478                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
479                 return;
480         }
481
482         i = get_vfio_group_idx(vfio_group_fd);
483         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
484                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
485         else
486                 vfio_cfg->vfio_groups[i].devices--;
487 }
488
489 static int
490 vfio_group_device_count(int vfio_group_fd)
491 {
492         struct vfio_config *vfio_cfg;
493         int i;
494
495         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
496         if (vfio_cfg == NULL) {
497                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
498                 return -1;
499         }
500
501         i = get_vfio_group_idx(vfio_group_fd);
502         if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
503                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
504                 return -1;
505         }
506
507         return vfio_cfg->vfio_groups[i].devices;
508 }
509
510 static void
511 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
512                 void *arg __rte_unused)
513 {
514         struct rte_memseg_list *msl;
515         struct rte_memseg *ms;
516         size_t cur_len = 0;
517
518         msl = rte_mem_virt2memseg_list(addr);
519
520         /* for IOVA as VA mode, no need to care for IOVA addresses */
521         if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
522                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
523                 if (type == RTE_MEM_EVENT_ALLOC)
524                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
525                                         len, 1);
526                 else
527                         vfio_dma_mem_map(default_vfio_cfg, vfio_va, vfio_va,
528                                         len, 0);
529                 return;
530         }
531
532         /* memsegs are contiguous in memory */
533         ms = rte_mem_virt2memseg(addr, msl);
534         while (cur_len < len) {
535                 /* some memory segments may have invalid IOVA */
536                 if (ms->iova == RTE_BAD_IOVA) {
537                         RTE_LOG(DEBUG, EAL, "Memory segment at %p has bad IOVA, skipping\n",
538                                         ms->addr);
539                         goto next;
540                 }
541                 if (type == RTE_MEM_EVENT_ALLOC)
542                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
543                                         ms->iova, ms->len, 1);
544                 else
545                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
546                                         ms->iova, ms->len, 0);
547 next:
548                 cur_len += ms->len;
549                 ++ms;
550         }
551 }
552
553 static int
554 vfio_sync_default_container(void)
555 {
556         struct rte_mp_msg mp_req, *mp_rep;
557         struct rte_mp_reply mp_reply;
558         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
559         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
560         int iommu_type_id;
561         unsigned int i;
562
563         /* cannot be called from primary */
564         if (rte_eal_process_type() != RTE_PROC_SECONDARY)
565                 return -1;
566
567         /* default container fd should have been opened in rte_vfio_enable() */
568         if (!default_vfio_cfg->vfio_enabled ||
569                         default_vfio_cfg->vfio_container_fd < 0) {
570                 RTE_LOG(ERR, EAL, "VFIO support is not initialized\n");
571                 return -1;
572         }
573
574         /* find default container's IOMMU type */
575         p->req = SOCKET_REQ_IOMMU_TYPE;
576         strcpy(mp_req.name, EAL_VFIO_MP);
577         mp_req.len_param = sizeof(*p);
578         mp_req.num_fds = 0;
579
580         iommu_type_id = -1;
581         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
582                         mp_reply.nb_received == 1) {
583                 mp_rep = &mp_reply.msgs[0];
584                 p = (struct vfio_mp_param *)mp_rep->param;
585                 if (p->result == SOCKET_OK)
586                         iommu_type_id = p->iommu_type_id;
587                 free(mp_reply.msgs);
588         }
589         if (iommu_type_id < 0) {
590                 RTE_LOG(ERR, EAL, "Could not get IOMMU type for default container\n");
591                 return -1;
592         }
593
594         /* we now have an fd for default container, as well as its IOMMU type.
595          * now, set up default VFIO container config to match.
596          */
597         for (i = 0; i < RTE_DIM(iommu_types); i++) {
598                 const struct vfio_iommu_type *t = &iommu_types[i];
599                 if (t->type_id != iommu_type_id)
600                         continue;
601
602                 /* we found our IOMMU type */
603                 default_vfio_cfg->vfio_iommu_type = t;
604
605                 return 0;
606         }
607         RTE_LOG(ERR, EAL, "Could not find IOMMU type id (%i)\n",
608                         iommu_type_id);
609         return -1;
610 }
611
612 int
613 rte_vfio_clear_group(int vfio_group_fd)
614 {
615         int i;
616         struct vfio_config *vfio_cfg;
617
618         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
619         if (vfio_cfg == NULL) {
620                 RTE_LOG(ERR, EAL, "  invalid group fd!\n");
621                 return -1;
622         }
623
624         i = get_vfio_group_idx(vfio_group_fd);
625         if (i < 0)
626                 return -1;
627         vfio_cfg->vfio_groups[i].group_num = -1;
628         vfio_cfg->vfio_groups[i].fd = -1;
629         vfio_cfg->vfio_groups[i].devices = 0;
630         vfio_cfg->vfio_active_groups--;
631
632         return 0;
633 }
634
635 int
636 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
637                 int *vfio_dev_fd, struct vfio_device_info *device_info)
638 {
639         struct vfio_group_status group_status = {
640                         .argsz = sizeof(group_status)
641         };
642         struct vfio_config *vfio_cfg;
643         struct user_mem_maps *user_mem_maps;
644         int vfio_container_fd;
645         int vfio_group_fd;
646         int iommu_group_num;
647         int i, ret;
648
649         /* get group number */
650         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
651         if (ret == 0) {
652                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
653                         dev_addr);
654                 return 1;
655         }
656
657         /* if negative, something failed */
658         if (ret < 0)
659                 return -1;
660
661         /* get the actual group fd */
662         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
663         if (vfio_group_fd < 0)
664                 return -1;
665
666         /* if group_fd == 0, that means the device isn't managed by VFIO */
667         if (vfio_group_fd == 0) {
668                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
669                                 dev_addr);
670                 return 1;
671         }
672
673         /*
674          * at this point, we know that this group is viable (meaning, all devices
675          * are either bound to VFIO or not bound to anything)
676          */
677
678         /* check if the group is viable */
679         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
680         if (ret) {
681                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
682                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
683                 close(vfio_group_fd);
684                 rte_vfio_clear_group(vfio_group_fd);
685                 return -1;
686         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
687                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable! "
688                                 "Not all devices in IOMMU group bound to VFIO or unbound\n",
689                                 dev_addr);
690                 close(vfio_group_fd);
691                 rte_vfio_clear_group(vfio_group_fd);
692                 return -1;
693         }
694
695         /* get the vfio_config it belongs to */
696         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
697         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
698         vfio_container_fd = vfio_cfg->vfio_container_fd;
699         user_mem_maps = &vfio_cfg->mem_maps;
700
701         /* check if group does not have a container yet */
702         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
703
704                 /* add group to a container */
705                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
706                                 &vfio_container_fd);
707                 if (ret) {
708                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
709                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
710                         close(vfio_group_fd);
711                         rte_vfio_clear_group(vfio_group_fd);
712                         return -1;
713                 }
714
715                 /*
716                  * pick an IOMMU type and set up DMA mappings for container
717                  *
718                  * needs to be done only once, only when first group is
719                  * assigned to a container and only in primary process.
720                  * Note this can happen several times with the hotplug
721                  * functionality.
722                  */
723                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
724                                 vfio_cfg->vfio_active_groups == 1 &&
725                                 vfio_group_device_count(vfio_group_fd) == 0) {
726                         const struct vfio_iommu_type *t;
727
728                         /* select an IOMMU type which we will be using */
729                         t = vfio_set_iommu_type(vfio_container_fd);
730                         if (!t) {
731                                 RTE_LOG(ERR, EAL,
732                                         "  %s failed to select IOMMU type\n",
733                                         dev_addr);
734                                 close(vfio_group_fd);
735                                 rte_vfio_clear_group(vfio_group_fd);
736                                 return -1;
737                         }
738                         /* lock memory hotplug before mapping and release it
739                          * after registering callback, to prevent races
740                          */
741                         rte_mcfg_mem_read_lock();
742                         if (vfio_cfg == default_vfio_cfg)
743                                 ret = t->dma_map_func(vfio_container_fd);
744                         else
745                                 ret = 0;
746                         if (ret) {
747                                 RTE_LOG(ERR, EAL,
748                                         "  %s DMA remapping failed, error %i (%s)\n",
749                                         dev_addr, errno, strerror(errno));
750                                 close(vfio_group_fd);
751                                 rte_vfio_clear_group(vfio_group_fd);
752                                 rte_mcfg_mem_read_unlock();
753                                 return -1;
754                         }
755
756                         vfio_cfg->vfio_iommu_type = t;
757
758                         /* re-map all user-mapped segments */
759                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
760
761                         /* this IOMMU type may not support DMA mapping, but
762                          * if we have mappings in the list - that means we have
763                          * previously mapped something successfully, so we can
764                          * be sure that DMA mapping is supported.
765                          */
766                         for (i = 0; i < user_mem_maps->n_maps; i++) {
767                                 struct user_mem_map *map;
768                                 map = &user_mem_maps->maps[i];
769
770                                 ret = t->dma_user_map_func(
771                                                 vfio_container_fd,
772                                                 map->addr, map->iova, map->len,
773                                                 1);
774                                 if (ret) {
775                                         RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
776                                                         "va: 0x%" PRIx64 " "
777                                                         "iova: 0x%" PRIx64 " "
778                                                         "len: 0x%" PRIu64 "\n",
779                                                         map->addr, map->iova,
780                                                         map->len);
781                                         rte_spinlock_recursive_unlock(
782                                                         &user_mem_maps->lock);
783                                         rte_mcfg_mem_read_unlock();
784                                         return -1;
785                                 }
786                         }
787                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
788
789                         /* register callback for mem events */
790                         if (vfio_cfg == default_vfio_cfg)
791                                 ret = rte_mem_event_callback_register(
792                                         VFIO_MEM_EVENT_CLB_NAME,
793                                         vfio_mem_event_callback, NULL);
794                         else
795                                 ret = 0;
796                         /* unlock memory hotplug */
797                         rte_mcfg_mem_read_unlock();
798
799                         if (ret && rte_errno != ENOTSUP) {
800                                 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
801                                 return -1;
802                         }
803                         if (ret)
804                                 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
805                         else
806                                 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
807                 }
808         } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
809                         vfio_cfg == default_vfio_cfg &&
810                         vfio_cfg->vfio_iommu_type == NULL) {
811                 /* if we're not a primary process, we do not set up the VFIO
812                  * container because it's already been set up by the primary
813                  * process. instead, we simply ask the primary about VFIO type
814                  * we are using, and set the VFIO config up appropriately.
815                  */
816                 ret = vfio_sync_default_container();
817                 if (ret < 0) {
818                         RTE_LOG(ERR, EAL, "Could not sync default VFIO container\n");
819                         close(vfio_group_fd);
820                         rte_vfio_clear_group(vfio_group_fd);
821                         return -1;
822                 }
823                 /* we have successfully initialized VFIO, notify user */
824                 const struct vfio_iommu_type *t =
825                                 default_vfio_cfg->vfio_iommu_type;
826                 RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
827                                 t->type_id, t->name);
828         }
829
830         /* get a file descriptor for the device */
831         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
832         if (*vfio_dev_fd < 0) {
833                 /* if we cannot get a device fd, this implies a problem with
834                  * the VFIO group or the container not having IOMMU configured.
835                  */
836
837                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
838                                 dev_addr);
839                 close(vfio_group_fd);
840                 rte_vfio_clear_group(vfio_group_fd);
841                 return -1;
842         }
843
844         /* test and setup the device */
845         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
846         if (ret) {
847                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
848                                 "error %i (%s)\n", dev_addr, errno,
849                                 strerror(errno));
850                 close(*vfio_dev_fd);
851                 close(vfio_group_fd);
852                 rte_vfio_clear_group(vfio_group_fd);
853                 return -1;
854         }
855         vfio_group_device_get(vfio_group_fd);
856
857         return 0;
858 }
859
860 int
861 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
862                     int vfio_dev_fd)
863 {
864         struct vfio_group_status group_status = {
865                         .argsz = sizeof(group_status)
866         };
867         struct vfio_config *vfio_cfg;
868         int vfio_group_fd;
869         int iommu_group_num;
870         int ret;
871
872         /* we don't want any DMA mapping messages to come while we're detaching
873          * VFIO device, because this might be the last device and we might need
874          * to unregister the callback.
875          */
876         rte_mcfg_mem_read_lock();
877
878         /* get group number */
879         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
880         if (ret <= 0) {
881                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
882                         dev_addr);
883                 /* This is an error at this point. */
884                 ret = -1;
885                 goto out;
886         }
887
888         /* get the actual group fd */
889         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
890         if (vfio_group_fd <= 0) {
891                 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
892                                    dev_addr);
893                 ret = -1;
894                 goto out;
895         }
896
897         /* get the vfio_config it belongs to */
898         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
899         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
900
901         /* At this point we got an active group. Closing it will make the
902          * container detachment. If this is the last active group, VFIO kernel
903          * code will unset the container and the IOMMU mappings.
904          */
905
906         /* Closing a device */
907         if (close(vfio_dev_fd) < 0) {
908                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
909                                    dev_addr);
910                 ret = -1;
911                 goto out;
912         }
913
914         /* An VFIO group can have several devices attached. Just when there is
915          * no devices remaining should the group be closed.
916          */
917         vfio_group_device_put(vfio_group_fd);
918         if (!vfio_group_device_count(vfio_group_fd)) {
919
920                 if (close(vfio_group_fd) < 0) {
921                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
922                                 dev_addr);
923                         ret = -1;
924                         goto out;
925                 }
926
927                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
928                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
929                                            dev_addr);
930                         ret = -1;
931                         goto out;
932                 }
933         }
934
935         /* if there are no active device groups, unregister the callback to
936          * avoid spurious attempts to map/unmap memory from VFIO.
937          */
938         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
939                         rte_eal_process_type() != RTE_PROC_SECONDARY)
940                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
941                                 NULL);
942
943         /* success */
944         ret = 0;
945
946 out:
947         rte_mcfg_mem_read_unlock();
948         return ret;
949 }
950
951 int
952 rte_vfio_enable(const char *modname)
953 {
954         /* initialize group list */
955         int i, j;
956         int vfio_available;
957
958         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
959
960         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
961                 vfio_cfgs[i].vfio_container_fd = -1;
962                 vfio_cfgs[i].vfio_active_groups = 0;
963                 vfio_cfgs[i].vfio_iommu_type = NULL;
964                 vfio_cfgs[i].mem_maps.lock = lock;
965
966                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
967                         vfio_cfgs[i].vfio_groups[j].fd = -1;
968                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
969                         vfio_cfgs[i].vfio_groups[j].devices = 0;
970                 }
971         }
972
973         /* inform the user that we are probing for VFIO */
974         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
975
976         /* check if vfio module is loaded */
977         vfio_available = rte_eal_check_module(modname);
978
979         /* return error directly */
980         if (vfio_available == -1) {
981                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
982                 return -1;
983         }
984
985         /* return 0 if VFIO modules not loaded */
986         if (vfio_available == 0) {
987                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
988                         "skipping VFIO support...\n");
989                 return 0;
990         }
991
992         if (internal_config.process_type == RTE_PROC_PRIMARY) {
993                 /* open a new container */
994                 default_vfio_cfg->vfio_container_fd =
995                                 rte_vfio_get_container_fd();
996         } else {
997                 /* get the default container from the primary process */
998                 default_vfio_cfg->vfio_container_fd =
999                                 vfio_get_default_container_fd();
1000         }
1001
1002         /* check if we have VFIO driver enabled */
1003         if (default_vfio_cfg->vfio_container_fd != -1) {
1004                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
1005                 default_vfio_cfg->vfio_enabled = 1;
1006         } else {
1007                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
1008         }
1009
1010         return 0;
1011 }
1012
1013 int
1014 rte_vfio_is_enabled(const char *modname)
1015 {
1016         const int mod_available = rte_eal_check_module(modname) > 0;
1017         return default_vfio_cfg->vfio_enabled && mod_available;
1018 }
1019
1020 int
1021 vfio_get_default_container_fd(void)
1022 {
1023         struct rte_mp_msg mp_req, *mp_rep;
1024         struct rte_mp_reply mp_reply;
1025         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1026         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1027
1028         if (default_vfio_cfg->vfio_enabled)
1029                 return default_vfio_cfg->vfio_container_fd;
1030
1031         if (internal_config.process_type == RTE_PROC_PRIMARY) {
1032                 /* if we were secondary process we would try requesting
1033                  * container fd from the primary, but we're the primary
1034                  * process so just exit here
1035                  */
1036                 return -1;
1037         }
1038
1039         p->req = SOCKET_REQ_DEFAULT_CONTAINER;
1040         strcpy(mp_req.name, EAL_VFIO_MP);
1041         mp_req.len_param = sizeof(*p);
1042         mp_req.num_fds = 0;
1043
1044         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1045             mp_reply.nb_received == 1) {
1046                 mp_rep = &mp_reply.msgs[0];
1047                 p = (struct vfio_mp_param *)mp_rep->param;
1048                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1049                         free(mp_reply.msgs);
1050                         return mp_rep->fds[0];
1051                 }
1052                 free(mp_reply.msgs);
1053         }
1054
1055         RTE_LOG(ERR, EAL, "  cannot request default container fd\n");
1056         return -1;
1057 }
1058
1059 int
1060 vfio_get_iommu_type(void)
1061 {
1062         if (default_vfio_cfg->vfio_iommu_type == NULL)
1063                 return -1;
1064
1065         return default_vfio_cfg->vfio_iommu_type->type_id;
1066 }
1067
1068 const struct vfio_iommu_type *
1069 vfio_set_iommu_type(int vfio_container_fd)
1070 {
1071         unsigned idx;
1072         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1073                 const struct vfio_iommu_type *t = &iommu_types[idx];
1074
1075                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
1076                                 t->type_id);
1077                 if (!ret) {
1078                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
1079                                         t->type_id, t->name);
1080                         return t;
1081                 }
1082                 /* not an error, there may be more supported IOMMU types */
1083                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
1084                                 "error %i (%s)\n", t->type_id, t->name, errno,
1085                                 strerror(errno));
1086         }
1087         /* if we didn't find a suitable IOMMU type, fail */
1088         return NULL;
1089 }
1090
1091 int
1092 vfio_has_supported_extensions(int vfio_container_fd)
1093 {
1094         int ret;
1095         unsigned idx, n_extensions = 0;
1096         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1097                 const struct vfio_iommu_type *t = &iommu_types[idx];
1098
1099                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1100                                 t->type_id);
1101                 if (ret < 0) {
1102                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
1103                                 "error %i (%s)\n", errno,
1104                                 strerror(errno));
1105                         close(vfio_container_fd);
1106                         return -1;
1107                 } else if (ret == 1) {
1108                         /* we found a supported extension */
1109                         n_extensions++;
1110                 }
1111                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
1112                                 t->type_id, t->name,
1113                                 ret ? "supported" : "not supported");
1114         }
1115
1116         /* if we didn't find any supported IOMMU types, fail */
1117         if (!n_extensions) {
1118                 close(vfio_container_fd);
1119                 return -1;
1120         }
1121
1122         return 0;
1123 }
1124
1125 int
1126 rte_vfio_get_container_fd(void)
1127 {
1128         int ret, vfio_container_fd;
1129         struct rte_mp_msg mp_req, *mp_rep;
1130         struct rte_mp_reply mp_reply;
1131         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1132         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1133
1134
1135         /* if we're in a primary process, try to open the container */
1136         if (internal_config.process_type == RTE_PROC_PRIMARY) {
1137                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1138                 if (vfio_container_fd < 0) {
1139                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
1140                                         "error %i (%s)\n", errno, strerror(errno));
1141                         return -1;
1142                 }
1143
1144                 /* check VFIO API version */
1145                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1146                 if (ret != VFIO_API_VERSION) {
1147                         if (ret < 0)
1148                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
1149                                                 "error %i (%s)\n", errno, strerror(errno));
1150                         else
1151                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
1152                         close(vfio_container_fd);
1153                         return -1;
1154                 }
1155
1156                 ret = vfio_has_supported_extensions(vfio_container_fd);
1157                 if (ret) {
1158                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
1159                                         "extensions found!\n");
1160                         return -1;
1161                 }
1162
1163                 return vfio_container_fd;
1164         }
1165         /*
1166          * if we're in a secondary process, request container fd from the
1167          * primary process via mp channel
1168          */
1169         p->req = SOCKET_REQ_CONTAINER;
1170         strcpy(mp_req.name, EAL_VFIO_MP);
1171         mp_req.len_param = sizeof(*p);
1172         mp_req.num_fds = 0;
1173
1174         vfio_container_fd = -1;
1175         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1176             mp_reply.nb_received == 1) {
1177                 mp_rep = &mp_reply.msgs[0];
1178                 p = (struct vfio_mp_param *)mp_rep->param;
1179                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1180                         vfio_container_fd = mp_rep->fds[0];
1181                         free(mp_reply.msgs);
1182                         return vfio_container_fd;
1183                 }
1184                 free(mp_reply.msgs);
1185         }
1186
1187         RTE_LOG(ERR, EAL, "  cannot request container fd\n");
1188         return -1;
1189 }
1190
1191 int
1192 rte_vfio_get_group_num(const char *sysfs_base,
1193                 const char *dev_addr, int *iommu_group_num)
1194 {
1195         char linkname[PATH_MAX];
1196         char filename[PATH_MAX];
1197         char *tok[16], *group_tok, *end;
1198         int ret;
1199
1200         memset(linkname, 0, sizeof(linkname));
1201         memset(filename, 0, sizeof(filename));
1202
1203         /* try to find out IOMMU group for this device */
1204         snprintf(linkname, sizeof(linkname),
1205                          "%s/%s/iommu_group", sysfs_base, dev_addr);
1206
1207         ret = readlink(linkname, filename, sizeof(filename));
1208
1209         /* if the link doesn't exist, no VFIO for us */
1210         if (ret < 0)
1211                 return 0;
1212
1213         ret = rte_strsplit(filename, sizeof(filename),
1214                         tok, RTE_DIM(tok), '/');
1215
1216         if (ret <= 0) {
1217                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
1218                 return -1;
1219         }
1220
1221         /* IOMMU group is always the last token */
1222         errno = 0;
1223         group_tok = tok[ret - 1];
1224         end = group_tok;
1225         *iommu_group_num = strtol(group_tok, &end, 10);
1226         if ((end != group_tok && *end != '\0') || errno != 0) {
1227                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
1228                 return -1;
1229         }
1230
1231         return 1;
1232 }
1233
1234 static int
1235 type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1236                 void *arg)
1237 {
1238         int *vfio_container_fd = arg;
1239
1240         if (msl->external)
1241                 return 0;
1242
1243         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1244                         ms->len, 1);
1245 }
1246
1247 static int
1248 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1249                 uint64_t len, int do_map)
1250 {
1251         struct vfio_iommu_type1_dma_map dma_map;
1252         struct vfio_iommu_type1_dma_unmap dma_unmap;
1253         int ret;
1254
1255         if (do_map != 0) {
1256                 memset(&dma_map, 0, sizeof(dma_map));
1257                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1258                 dma_map.vaddr = vaddr;
1259                 dma_map.size = len;
1260                 dma_map.iova = iova;
1261                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1262                                 VFIO_DMA_MAP_FLAG_WRITE;
1263
1264                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1265                 if (ret) {
1266                         /**
1267                          * In case the mapping was already done EEXIST will be
1268                          * returned from kernel.
1269                          */
1270                         if (errno == EEXIST) {
1271                                 RTE_LOG(DEBUG, EAL,
1272                                         " Memory segment is allready mapped,"
1273                                         " skipping");
1274                         } else {
1275                                 RTE_LOG(ERR, EAL,
1276                                         "  cannot set up DMA remapping,"
1277                                         " error %i (%s)\n",
1278                                         errno, strerror(errno));
1279                                 return -1;
1280                         }
1281                 }
1282         } else {
1283                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1284                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1285                 dma_unmap.size = len;
1286                 dma_unmap.iova = iova;
1287
1288                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1289                                 &dma_unmap);
1290                 if (ret) {
1291                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1292                                         errno, strerror(errno));
1293                         return -1;
1294                 }
1295         }
1296
1297         return 0;
1298 }
1299
1300 static int
1301 vfio_type1_dma_map(int vfio_container_fd)
1302 {
1303         return rte_memseg_walk(type1_map, &vfio_container_fd);
1304 }
1305
1306 static int
1307 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1308                 uint64_t len, int do_map)
1309 {
1310         struct vfio_iommu_type1_dma_map dma_map;
1311         struct vfio_iommu_type1_dma_unmap dma_unmap;
1312         int ret;
1313         struct vfio_iommu_spapr_register_memory reg = {
1314                 .argsz = sizeof(reg),
1315                 .flags = 0
1316         };
1317         reg.vaddr = (uintptr_t) vaddr;
1318         reg.size = len;
1319
1320         if (do_map != 0) {
1321                 ret = ioctl(vfio_container_fd,
1322                                 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
1323                 if (ret) {
1324                         RTE_LOG(ERR, EAL, "  cannot register vaddr for IOMMU, "
1325                                 "error %i (%s)\n", errno, strerror(errno));
1326                         return -1;
1327                 }
1328
1329                 memset(&dma_map, 0, sizeof(dma_map));
1330                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1331                 dma_map.vaddr = vaddr;
1332                 dma_map.size = len;
1333                 dma_map.iova = iova;
1334                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1335                                 VFIO_DMA_MAP_FLAG_WRITE;
1336
1337                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1338                 if (ret) {
1339                         /**
1340                          * In case the mapping was already done EBUSY will be
1341                          * returned from kernel.
1342                          */
1343                         if (errno == EBUSY) {
1344                                 RTE_LOG(DEBUG, EAL,
1345                                         " Memory segment is allready mapped,"
1346                                         " skipping");
1347                         } else {
1348                                 RTE_LOG(ERR, EAL,
1349                                         "  cannot set up DMA remapping,"
1350                                         " error %i (%s)\n", errno,
1351                                         strerror(errno));
1352                                 return -1;
1353                         }
1354                 }
1355
1356         } else {
1357                 ret = ioctl(vfio_container_fd,
1358                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1359                 if (ret) {
1360                         RTE_LOG(ERR, EAL, "  cannot unregister vaddr for IOMMU, error %i (%s)\n",
1361                                         errno, strerror(errno));
1362                         return -1;
1363                 }
1364
1365                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1366                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1367                 dma_unmap.size = len;
1368                 dma_unmap.iova = iova;
1369
1370                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1371                                 &dma_unmap);
1372                 if (ret) {
1373                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1374                                         errno, strerror(errno));
1375                         return -1;
1376                 }
1377         }
1378
1379         return 0;
1380 }
1381
1382 static int
1383 vfio_spapr_map_walk(const struct rte_memseg_list *msl,
1384                 const struct rte_memseg *ms, void *arg)
1385 {
1386         int *vfio_container_fd = arg;
1387
1388         if (msl->external)
1389                 return 0;
1390
1391         return vfio_spapr_dma_do_map(*vfio_container_fd, ms->addr_64, ms->iova,
1392                         ms->len, 1);
1393 }
1394
1395 struct spapr_walk_param {
1396         uint64_t window_size;
1397         uint64_t hugepage_sz;
1398 };
1399 static int
1400 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl,
1401                 const struct rte_memseg *ms, void *arg)
1402 {
1403         struct spapr_walk_param *param = arg;
1404         uint64_t max = ms->iova + ms->len;
1405
1406         if (msl->external)
1407                 return 0;
1408
1409         if (max > param->window_size) {
1410                 param->hugepage_sz = ms->hugepage_sz;
1411                 param->window_size = max;
1412         }
1413
1414         return 0;
1415 }
1416
1417 static int
1418 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1419                 struct vfio_iommu_spapr_tce_create *create) {
1420         struct vfio_iommu_spapr_tce_remove remove = {
1421                 .argsz = sizeof(remove),
1422         };
1423         struct vfio_iommu_spapr_tce_info info = {
1424                 .argsz = sizeof(info),
1425         };
1426         int ret;
1427
1428         /* query spapr iommu info */
1429         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1430         if (ret) {
1431                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
1432                                 "error %i (%s)\n", errno, strerror(errno));
1433                 return -1;
1434         }
1435
1436         /* remove default DMA of 32 bit window */
1437         remove.start_addr = info.dma32_window_start;
1438         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1439         if (ret) {
1440                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
1441                                 "error %i (%s)\n", errno, strerror(errno));
1442                 return -1;
1443         }
1444
1445         /* create new DMA window */
1446         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1447         if (ret) {
1448                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
1449                                 "error %i (%s)\n", errno, strerror(errno));
1450                 return -1;
1451         }
1452
1453         if (create->start_addr != 0) {
1454                 RTE_LOG(ERR, EAL, "  DMA window start address != 0\n");
1455                 return -1;
1456         }
1457
1458         return 0;
1459 }
1460
1461 static int
1462 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1463                 uint64_t len, int do_map)
1464 {
1465         struct spapr_walk_param param;
1466         struct vfio_iommu_spapr_tce_create create = {
1467                 .argsz = sizeof(create),
1468         };
1469         struct vfio_config *vfio_cfg;
1470         struct user_mem_maps *user_mem_maps;
1471         int i, ret = 0;
1472
1473         vfio_cfg = get_vfio_cfg_by_container_fd(vfio_container_fd);
1474         if (vfio_cfg == NULL) {
1475                 RTE_LOG(ERR, EAL, "  invalid container fd!\n");
1476                 return -1;
1477         }
1478
1479         user_mem_maps = &vfio_cfg->mem_maps;
1480         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1481
1482         /* check if window size needs to be adjusted */
1483         memset(&param, 0, sizeof(param));
1484
1485         /* we're inside a callback so use thread-unsafe version */
1486         if (rte_memseg_walk_thread_unsafe(vfio_spapr_window_size_walk,
1487                                 &param) < 0) {
1488                 RTE_LOG(ERR, EAL, "Could not get window size\n");
1489                 ret = -1;
1490                 goto out;
1491         }
1492
1493         /* also check user maps */
1494         for (i = 0; i < user_mem_maps->n_maps; i++) {
1495                 uint64_t max = user_mem_maps->maps[i].iova +
1496                                 user_mem_maps->maps[i].len;
1497                 create.window_size = RTE_MAX(create.window_size, max);
1498         }
1499
1500         /* sPAPR requires window size to be a power of 2 */
1501         create.window_size = rte_align64pow2(param.window_size);
1502         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1503         create.levels = 1;
1504
1505         if (do_map) {
1506                 void *addr;
1507                 /* re-create window and remap the entire memory */
1508                 if (iova > create.window_size) {
1509                         if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1510                                         &create) < 0) {
1511                                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1512                                 ret = -1;
1513                                 goto out;
1514                         }
1515                         /* we're inside a callback, so use thread-unsafe version
1516                          */
1517                         if (rte_memseg_walk_thread_unsafe(vfio_spapr_map_walk,
1518                                         &vfio_container_fd) < 0) {
1519                                 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1520                                 ret = -1;
1521                                 goto out;
1522                         }
1523                         /* remap all user maps */
1524                         for (i = 0; i < user_mem_maps->n_maps; i++) {
1525                                 struct user_mem_map *map =
1526                                                 &user_mem_maps->maps[i];
1527                                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1528                                                 map->addr, map->iova, map->len,
1529                                                 1)) {
1530                                         RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1531                                         ret = -1;
1532                                         goto out;
1533                                 }
1534                         }
1535                 }
1536
1537                 /* now that we've remapped all of the memory that was present
1538                  * before, map the segment that we were requested to map.
1539                  *
1540                  * however, if we were called by the callback, the memory we
1541                  * were called with was already in the memseg list, so previous
1542                  * mapping should've mapped that segment already.
1543                  *
1544                  * virt2memseg_list is a relatively cheap check, so use that. if
1545                  * memory is within any memseg list, it's a memseg, so it's
1546                  * already mapped.
1547                  */
1548                 addr = (void *)(uintptr_t)vaddr;
1549                 if (rte_mem_virt2memseg_list(addr) == NULL &&
1550                                 vfio_spapr_dma_do_map(vfio_container_fd,
1551                                         vaddr, iova, len, 1) < 0) {
1552                         RTE_LOG(ERR, EAL, "Could not map segment\n");
1553                         ret = -1;
1554                         goto out;
1555                 }
1556         } else {
1557                 /* for unmap, check if iova within DMA window */
1558                 if (iova > create.window_size) {
1559                         RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1560                         ret = -1;
1561                         goto out;
1562                 }
1563
1564                 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1565         }
1566 out:
1567         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1568         return ret;
1569 }
1570
1571 static int
1572 vfio_spapr_dma_map(int vfio_container_fd)
1573 {
1574         struct vfio_iommu_spapr_tce_create create = {
1575                 .argsz = sizeof(create),
1576         };
1577         struct spapr_walk_param param;
1578
1579         memset(&param, 0, sizeof(param));
1580
1581         /* create DMA window from 0 to max(phys_addr + len) */
1582         rte_memseg_walk(vfio_spapr_window_size_walk, &param);
1583
1584         /* sPAPR requires window size to be a power of 2 */
1585         create.window_size = rte_align64pow2(param.window_size);
1586         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1587         create.levels = 1;
1588
1589         if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1590                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1591                 return -1;
1592         }
1593
1594         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1595         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1596                 return -1;
1597
1598         return 0;
1599 }
1600
1601 static int
1602 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1603 {
1604         /* No-IOMMU mode does not need DMA mapping */
1605         return 0;
1606 }
1607
1608 static int
1609 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1610                          uint64_t __rte_unused vaddr,
1611                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1612                          int __rte_unused do_map)
1613 {
1614         /* No-IOMMU mode does not need DMA mapping */
1615         return 0;
1616 }
1617
1618 static int
1619 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1620                 uint64_t len, int do_map)
1621 {
1622         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1623
1624         if (!t) {
1625                 RTE_LOG(ERR, EAL, "  VFIO support not initialized\n");
1626                 rte_errno = ENODEV;
1627                 return -1;
1628         }
1629
1630         if (!t->dma_user_map_func) {
1631                 RTE_LOG(ERR, EAL,
1632                         "  VFIO custom DMA region maping not supported by IOMMU %s\n",
1633                         t->name);
1634                 rte_errno = ENOTSUP;
1635                 return -1;
1636         }
1637
1638         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1639                         len, do_map);
1640 }
1641
1642 static int
1643 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1644                 uint64_t len)
1645 {
1646         struct user_mem_map *new_map;
1647         struct user_mem_maps *user_mem_maps;
1648         int ret = 0;
1649
1650         user_mem_maps = &vfio_cfg->mem_maps;
1651         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1652         if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1653                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1654                 rte_errno = ENOMEM;
1655                 ret = -1;
1656                 goto out;
1657         }
1658         /* map the entry */
1659         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1660                 /* technically, this will fail if there are currently no devices
1661                  * plugged in, even if a device were added later, this mapping
1662                  * might have succeeded. however, since we cannot verify if this
1663                  * is a valid mapping without having a device attached, consider
1664                  * this to be unsupported, because we can't just store any old
1665                  * mapping and pollute list of active mappings willy-nilly.
1666                  */
1667                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1668                 ret = -1;
1669                 goto out;
1670         }
1671         /* create new user mem map entry */
1672         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1673         new_map->addr = vaddr;
1674         new_map->iova = iova;
1675         new_map->len = len;
1676
1677         compact_user_maps(user_mem_maps);
1678 out:
1679         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1680         return ret;
1681 }
1682
1683 static int
1684 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1685                 uint64_t len)
1686 {
1687         struct user_mem_map *map, *new_map = NULL;
1688         struct user_mem_maps *user_mem_maps;
1689         int ret = 0;
1690
1691         user_mem_maps = &vfio_cfg->mem_maps;
1692         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1693
1694         /* find our mapping */
1695         map = find_user_mem_map(user_mem_maps, vaddr, iova, len);
1696         if (!map) {
1697                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1698                 rte_errno = EINVAL;
1699                 ret = -1;
1700                 goto out;
1701         }
1702         if (map->addr != vaddr || map->iova != iova || map->len != len) {
1703                 /* we're partially unmapping a previously mapped region, so we
1704                  * need to split entry into two.
1705                  */
1706                 if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1707                         RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1708                         rte_errno = ENOMEM;
1709                         ret = -1;
1710                         goto out;
1711                 }
1712                 new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1713         }
1714
1715         /* unmap the entry */
1716         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
1717                 /* there may not be any devices plugged in, so unmapping will
1718                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1719                  * stop us from removing the mapping, as the assumption is we
1720                  * won't be needing this memory any more and thus will want to
1721                  * prevent it from being remapped again on hotplug. so, only
1722                  * fail if we indeed failed to unmap (e.g. if the mapping was
1723                  * within our mapped range but had invalid alignment).
1724                  */
1725                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1726                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1727                         ret = -1;
1728                         goto out;
1729                 } else {
1730                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1731                 }
1732         }
1733         /* remove map from the list of active mappings */
1734         if (new_map != NULL) {
1735                 adjust_map(map, new_map, vaddr, len);
1736
1737                 /* if we've created a new map by splitting, sort everything */
1738                 if (!is_null_map(new_map)) {
1739                         compact_user_maps(user_mem_maps);
1740                 } else {
1741                         /* we've created a new mapping, but it was unused */
1742                         user_mem_maps->n_maps--;
1743                 }
1744         } else {
1745                 memset(map, 0, sizeof(*map));
1746                 compact_user_maps(user_mem_maps);
1747                 user_mem_maps->n_maps--;
1748         }
1749
1750 out:
1751         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1752         return ret;
1753 }
1754
1755 int
1756 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1757 {
1758         if (len == 0) {
1759                 rte_errno = EINVAL;
1760                 return -1;
1761         }
1762
1763         return container_dma_map(default_vfio_cfg, vaddr, iova, len);
1764 }
1765
1766 int
1767 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1768 {
1769         if (len == 0) {
1770                 rte_errno = EINVAL;
1771                 return -1;
1772         }
1773
1774         return container_dma_unmap(default_vfio_cfg, vaddr, iova, len);
1775 }
1776
1777 int
1778 rte_vfio_noiommu_is_enabled(void)
1779 {
1780         int fd;
1781         ssize_t cnt;
1782         char c;
1783
1784         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1785         if (fd < 0) {
1786                 if (errno != ENOENT) {
1787                         RTE_LOG(ERR, EAL, "  cannot open vfio noiommu file %i (%s)\n",
1788                                         errno, strerror(errno));
1789                         return -1;
1790                 }
1791                 /*
1792                  * else the file does not exists
1793                  * i.e. noiommu is not enabled
1794                  */
1795                 return 0;
1796         }
1797
1798         cnt = read(fd, &c, 1);
1799         close(fd);
1800         if (cnt != 1) {
1801                 RTE_LOG(ERR, EAL, "  unable to read from vfio noiommu "
1802                                 "file %i (%s)\n", errno, strerror(errno));
1803                 return -1;
1804         }
1805
1806         return c == 'Y';
1807 }
1808
1809 int
1810 rte_vfio_container_create(void)
1811 {
1812         int i;
1813
1814         /* Find an empty slot to store new vfio config */
1815         for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
1816                 if (vfio_cfgs[i].vfio_container_fd == -1)
1817                         break;
1818         }
1819
1820         if (i == VFIO_MAX_CONTAINERS) {
1821                 RTE_LOG(ERR, EAL, "exceed max vfio container limit\n");
1822                 return -1;
1823         }
1824
1825         vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
1826         if (vfio_cfgs[i].vfio_container_fd < 0) {
1827                 RTE_LOG(NOTICE, EAL, "fail to create a new container\n");
1828                 return -1;
1829         }
1830
1831         return vfio_cfgs[i].vfio_container_fd;
1832 }
1833
1834 int
1835 rte_vfio_container_destroy(int container_fd)
1836 {
1837         struct vfio_config *vfio_cfg;
1838         int i;
1839
1840         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1841         if (vfio_cfg == NULL) {
1842                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1843                 return -1;
1844         }
1845
1846         for (i = 0; i < VFIO_MAX_GROUPS; i++)
1847                 if (vfio_cfg->vfio_groups[i].group_num != -1)
1848                         rte_vfio_container_group_unbind(container_fd,
1849                                 vfio_cfg->vfio_groups[i].group_num);
1850
1851         close(container_fd);
1852         vfio_cfg->vfio_container_fd = -1;
1853         vfio_cfg->vfio_active_groups = 0;
1854         vfio_cfg->vfio_iommu_type = NULL;
1855
1856         return 0;
1857 }
1858
1859 int
1860 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
1861 {
1862         struct vfio_config *vfio_cfg;
1863
1864         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1865         if (vfio_cfg == NULL) {
1866                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1867                 return -1;
1868         }
1869
1870         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
1871 }
1872
1873 int
1874 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
1875 {
1876         struct vfio_config *vfio_cfg;
1877         struct vfio_group *cur_grp = NULL;
1878         int i;
1879
1880         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1881         if (vfio_cfg == NULL) {
1882                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1883                 return -1;
1884         }
1885
1886         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1887                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
1888                         cur_grp = &vfio_cfg->vfio_groups[i];
1889                         break;
1890                 }
1891         }
1892
1893         /* This should not happen */
1894         if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
1895                 RTE_LOG(ERR, EAL, "Specified group number not found\n");
1896                 return -1;
1897         }
1898
1899         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
1900                 RTE_LOG(ERR, EAL, "Error when closing vfio_group_fd for"
1901                         " iommu_group_num %d\n", iommu_group_num);
1902                 return -1;
1903         }
1904         cur_grp->group_num = -1;
1905         cur_grp->fd = -1;
1906         cur_grp->devices = 0;
1907         vfio_cfg->vfio_active_groups--;
1908
1909         return 0;
1910 }
1911
1912 int
1913 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
1914                 uint64_t len)
1915 {
1916         struct vfio_config *vfio_cfg;
1917
1918         if (len == 0) {
1919                 rte_errno = EINVAL;
1920                 return -1;
1921         }
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         return container_dma_map(vfio_cfg, vaddr, iova, len);
1930 }
1931
1932 int
1933 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
1934                 uint64_t len)
1935 {
1936         struct vfio_config *vfio_cfg;
1937
1938         if (len == 0) {
1939                 rte_errno = EINVAL;
1940                 return -1;
1941         }
1942
1943         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
1944         if (vfio_cfg == NULL) {
1945                 RTE_LOG(ERR, EAL, "Invalid container fd\n");
1946                 return -1;
1947         }
1948
1949         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
1950 }
1951
1952 #else
1953
1954 int
1955 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1956                   __rte_unused uint64_t len)
1957 {
1958         return -1;
1959 }
1960
1961 int
1962 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1963                     __rte_unused uint64_t len)
1964 {
1965         return -1;
1966 }
1967
1968 int
1969 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
1970                 __rte_unused const char *dev_addr,
1971                 __rte_unused int *vfio_dev_fd,
1972                 __rte_unused struct vfio_device_info *device_info)
1973 {
1974         return -1;
1975 }
1976
1977 int
1978 rte_vfio_release_device(__rte_unused const char *sysfs_base,
1979                 __rte_unused const char *dev_addr, __rte_unused int fd)
1980 {
1981         return -1;
1982 }
1983
1984 int
1985 rte_vfio_enable(__rte_unused const char *modname)
1986 {
1987         return -1;
1988 }
1989
1990 int
1991 rte_vfio_is_enabled(__rte_unused const char *modname)
1992 {
1993         return -1;
1994 }
1995
1996 int
1997 rte_vfio_noiommu_is_enabled(void)
1998 {
1999         return -1;
2000 }
2001
2002 int
2003 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
2004 {
2005         return -1;
2006 }
2007
2008 int
2009 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
2010                 __rte_unused const char *dev_addr,
2011                 __rte_unused int *iommu_group_num)
2012 {
2013         return -1;
2014 }
2015
2016 int
2017 rte_vfio_get_container_fd(void)
2018 {
2019         return -1;
2020 }
2021
2022 int
2023 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
2024 {
2025         return -1;
2026 }
2027
2028 int
2029 rte_vfio_container_create(void)
2030 {
2031         return -1;
2032 }
2033
2034 int
2035 rte_vfio_container_destroy(__rte_unused int container_fd)
2036 {
2037         return -1;
2038 }
2039
2040 int
2041 rte_vfio_container_group_bind(__rte_unused int container_fd,
2042                 __rte_unused int iommu_group_num)
2043 {
2044         return -1;
2045 }
2046
2047 int
2048 rte_vfio_container_group_unbind(__rte_unused int container_fd,
2049                 __rte_unused int iommu_group_num)
2050 {
2051         return -1;
2052 }
2053
2054 int
2055 rte_vfio_container_dma_map(__rte_unused int container_fd,
2056                 __rte_unused uint64_t vaddr,
2057                 __rte_unused uint64_t iova,
2058                 __rte_unused uint64_t len)
2059 {
2060         return -1;
2061 }
2062
2063 int
2064 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
2065                 __rte_unused uint64_t vaddr,
2066                 __rte_unused uint64_t iova,
2067                 __rte_unused uint64_t len)
2068 {
2069         return -1;
2070 }
2071
2072 #endif /* VFIO_PRESENT */