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