vfio: add multiprocess support
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <fcntl.h>
36 #include <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40
41 #include <rte_log.h>
42 #include <rte_pci.h>
43 #include <rte_tailq.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_malloc.h>
46
47 #include "eal_filesystem.h"
48 #include "eal_pci_init.h"
49 #include "eal_vfio.h"
50
51 /**
52  * @file
53  * PCI probing under linux (VFIO version)
54  *
55  * This code tries to determine if the PCI device is bound to VFIO driver,
56  * and initialize it (map BARs, set up interrupts) if that's the case.
57  *
58  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
59  */
60
61 #ifdef VFIO_PRESENT
62
63 #define VFIO_DIR "/dev/vfio"
64 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
65 #define VFIO_GROUP_FMT "/dev/vfio/%u"
66 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
67
68 /* per-process VFIO config */
69 static struct vfio_config vfio_cfg;
70
71 /* get PCI BAR number where MSI-X interrupts are */
72 static int
73 pci_vfio_get_msix_bar(int fd, int *msix_bar)
74 {
75         int ret;
76         uint32_t reg;
77         uint8_t cap_id, cap_offset;
78
79         /* read PCI capability pointer from config space */
80         ret = pread64(fd, &reg, sizeof(reg),
81                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
82                         PCI_CAPABILITY_LIST);
83         if (ret != sizeof(reg)) {
84                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
85                                 "config space!\n");
86                 return -1;
87         }
88
89         /* we need first byte */
90         cap_offset = reg & 0xFF;
91
92         while (cap_offset) {
93
94                 /* read PCI capability ID */
95                 ret = pread64(fd, &reg, sizeof(reg),
96                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
97                                 cap_offset);
98                 if (ret != sizeof(reg)) {
99                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
100                                         "config space!\n");
101                         return -1;
102                 }
103
104                 /* we need first byte */
105                 cap_id = reg & 0xFF;
106
107                 /* if we haven't reached MSI-X, check next capability */
108                 if (cap_id != PCI_CAP_ID_MSIX) {
109                         ret = pread64(fd, &reg, sizeof(reg),
110                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
111                                         cap_offset);
112                         if (ret != sizeof(reg)) {
113                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
114                                                 "config space!\n");
115                                 return -1;
116                         }
117
118                         /* we need second byte */
119                         cap_offset = (reg & 0xFF00) >> 8;
120
121                         continue;
122                 }
123                 /* else, read table offset */
124                 else {
125                         /* table offset resides in the next 4 bytes */
126                         ret = pread64(fd, &reg, sizeof(reg),
127                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
128                                         cap_offset + 4);
129                         if (ret != sizeof(reg)) {
130                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
131                                                 "space!\n");
132                                 return -1;
133                         }
134
135                         *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
136
137                         return 0;
138                 }
139         }
140         return 0;
141 }
142
143 /* set PCI bus mastering */
144 static int
145 pci_vfio_set_bus_master(int dev_fd)
146 {
147         uint16_t reg;
148         int ret;
149
150         ret = pread64(dev_fd, &reg, sizeof(reg),
151                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
152                         PCI_COMMAND);
153         if (ret != sizeof(reg)) {
154                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
155                 return -1;
156         }
157
158         /* set the master bit */
159         reg |= PCI_COMMAND_MASTER;
160
161         ret = pwrite64(dev_fd, &reg, sizeof(reg),
162                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
163                         PCI_COMMAND);
164
165         if (ret != sizeof(reg)) {
166                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
167                 return -1;
168         }
169
170         return 0;
171 }
172
173 /* set up DMA mappings */
174 static int
175 pci_vfio_setup_dma_maps(int vfio_container_fd)
176 {
177         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
178         int i, ret;
179
180         ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
181                         VFIO_TYPE1_IOMMU);
182         if (ret) {
183                 RTE_LOG(ERR, EAL, "  cannot set IOMMU type!\n");
184                 return -1;
185         }
186
187         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
188         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
189                 struct vfio_iommu_type1_dma_map dma_map;
190
191                 if (ms[i].addr == NULL)
192                         break;
193
194                 memset(&dma_map, 0, sizeof(dma_map));
195                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
196                 dma_map.vaddr = ms[i].addr_64;
197                 dma_map.size = ms[i].len;
198                 dma_map.iova = ms[i].phys_addr;
199                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
200
201                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
202
203                 if (ret) {
204                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping!\n");
205                         return -1;
206                 }
207         }
208
209         return 0;
210 }
211
212 /* set up interrupt support (but not enable interrupts) */
213 static int
214 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
215 {
216         int i, ret, intr_idx;
217
218         /* default to invalid index */
219         intr_idx = VFIO_PCI_NUM_IRQS;
220
221         /* get interrupt type from internal config (MSI-X by default, can be
222          * overriden from the command line
223          */
224         switch (internal_config.vfio_intr_mode) {
225         case RTE_INTR_MODE_MSIX:
226                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
227                 break;
228         case RTE_INTR_MODE_MSI:
229                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
230                 break;
231         case RTE_INTR_MODE_LEGACY:
232                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
233                 break;
234         /* don't do anything if we want to automatically determine interrupt type */
235         case RTE_INTR_MODE_NONE:
236                 break;
237         default:
238                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
239                 return -1;
240         }
241
242         /* start from MSI-X interrupt type */
243         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
244                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
245                 int fd = -1;
246
247                 /* skip interrupt modes we don't want */
248                 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
249                                 i != intr_idx)
250                         continue;
251
252                 irq.index = i;
253
254                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
255                 if (ret < 0) {
256                         RTE_LOG(ERR, EAL, "  cannot get IRQ info!\n");
257                         return -1;
258                 }
259
260                 /* if this vector cannot be used with eventfd, fail if we explicitly
261                  * specified interrupt type, otherwise continue */
262                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
263                         if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
264                                 RTE_LOG(ERR, EAL,
265                                                 "  interrupt vector does not support eventfd!\n");
266                                 return -1;
267                         } else
268                                 continue;
269                 }
270
271                 /* set up an eventfd for interrupts */
272                 fd = eventfd(0, 0);
273                 if (fd < 0) {
274                         RTE_LOG(ERR, EAL, "  cannot set up eventfd!\n");
275                         return -1;
276                 }
277
278                 dev->intr_handle.fd = fd;
279                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
280
281                 switch (i) {
282                 case VFIO_PCI_MSIX_IRQ_INDEX:
283                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
284                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
285                         break;
286                 case VFIO_PCI_MSI_IRQ_INDEX:
287                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
288                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
289                         break;
290                 case VFIO_PCI_INTX_IRQ_INDEX:
291                         internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
292                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
293                         break;
294                 default:
295                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
296                         return -1;
297                 }
298
299                 return 0;
300         }
301
302         /* if we're here, we haven't found a suitable interrupt vector */
303         return -1;
304 }
305
306 /* open container fd or get an existing one */
307 int
308 pci_vfio_get_container_fd(void)
309 {
310         int ret, vfio_container_fd;
311
312         /* if we're in a primary process, try to open the container */
313         if (internal_config.process_type == RTE_PROC_PRIMARY) {
314                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
315                 if (vfio_container_fd < 0) {
316                         RTE_LOG(ERR, EAL, "  cannot open VFIO container!\n");
317                         return -1;
318                 }
319
320                 /* check VFIO API version */
321                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
322                 if (ret != VFIO_API_VERSION) {
323                         RTE_LOG(ERR, EAL, "  unknown VFIO API version!\n");
324                         close(vfio_container_fd);
325                         return -1;
326                 }
327
328                 /* check if we support IOMMU type 1 */
329                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
330                 if (!ret) {
331                         RTE_LOG(ERR, EAL, "  unknown IOMMU driver!\n");
332                         close(vfio_container_fd);
333                         return -1;
334                 }
335
336                 return vfio_container_fd;
337         } else {
338                 /*
339                  * if we're in a secondary process, request container fd from the
340                  * primary process via our socket
341                  */
342                 int socket_fd;
343
344                 socket_fd = vfio_mp_sync_connect_to_primary();
345                 if (socket_fd < 0) {
346                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
347                         return -1;
348                 }
349                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
350                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
351                         close(socket_fd);
352                         return -1;
353                 }
354                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
355                 if (vfio_container_fd < 0) {
356                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
357                         close(socket_fd);
358                         return -1;
359                 }
360                 close(socket_fd);
361                 return vfio_container_fd;
362         }
363
364         return -1;
365 }
366
367 /* open group fd or get an existing one */
368 int
369 pci_vfio_get_group_fd(int iommu_group_no)
370 {
371         int i;
372         int vfio_group_fd;
373         char filename[PATH_MAX];
374
375         /* check if we already have the group descriptor open */
376         for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
377                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
378                         return vfio_cfg.vfio_groups[i].fd;
379
380         /* if primary, try to open the group */
381         if (internal_config.process_type == RTE_PROC_PRIMARY) {
382                 rte_snprintf(filename, sizeof(filename),
383                                  VFIO_GROUP_FMT, iommu_group_no);
384                 vfio_group_fd = open(filename, O_RDWR);
385                 if (vfio_group_fd < 0) {
386                         /* if file not found, it's not an error */
387                         if (errno != ENOENT) {
388                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
389                                                 strerror(errno));
390                                 return -1;
391                         }
392                         return 0;
393                 }
394
395                 /* if the fd is valid, create a new group for it */
396                 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
397                         RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
398                         return -1;
399                 }
400                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
401                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
402                 return vfio_group_fd;
403         }
404         /* if we're in a secondary process, request group fd from the primary
405          * process via our socket
406          */
407         else {
408                 int socket_fd, ret;
409
410                 socket_fd = vfio_mp_sync_connect_to_primary();
411
412                 if (socket_fd < 0) {
413                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
414                         return -1;
415                 }
416                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
417                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
418                         close(socket_fd);
419                         return -1;
420                 }
421                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
422                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
423                         close(socket_fd);
424                         return -1;
425                 }
426                 ret = vfio_mp_sync_receive_request(socket_fd);
427                 switch (ret) {
428                 case SOCKET_NO_FD:
429                         close(socket_fd);
430                         return 0;
431                 case SOCKET_OK:
432                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
433                         /* if we got the fd, return it */
434                         if (vfio_group_fd > 0) {
435                                 close(socket_fd);
436                                 return vfio_group_fd;
437                         }
438                         /* fall-through on error */
439                 default:
440                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
441                         close(socket_fd);
442                         return -1;
443                 }
444         }
445         return -1;
446 }
447
448 /* parse IOMMU group number for a PCI device
449  * returns -1 for errors, 0 for non-existent group */
450 static int
451 pci_vfio_get_group_no(const char *pci_addr)
452 {
453         char linkname[PATH_MAX];
454         char filename[PATH_MAX];
455         char *tok[16], *group_tok, *end;
456         int ret, iommu_group_no;
457
458         memset(linkname, 0, sizeof(linkname));
459         memset(filename, 0, sizeof(filename));
460
461         /* try to find out IOMMU group for this device */
462         rte_snprintf(linkname, sizeof(linkname),
463                          SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
464
465         ret = readlink(linkname, filename, sizeof(filename));
466
467         /* if the link doesn't exist, no VFIO for us */
468         if (ret < 0)
469                 return 0;
470
471         ret = rte_strsplit(filename, sizeof(filename),
472                         tok, RTE_DIM(tok), '/');
473
474         if (ret <= 0) {
475                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", pci_addr);
476                 return -1;
477         }
478
479         /* IOMMU group is always the last token */
480         errno = 0;
481         group_tok = tok[ret - 1];
482         end = group_tok;
483         iommu_group_no = strtol(group_tok, &end, 10);
484         if ((end != group_tok && *end != '\0') || errno != 0) {
485                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", pci_addr);
486                 return -1;
487         }
488
489         return iommu_group_no;
490 }
491
492 static void
493 clear_current_group(void)
494 {
495         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
496         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
497 }
498
499
500 /*
501  * map the PCI resources of a PCI device in virtual memory (VFIO version).
502  * primary and secondary processes follow almost exactly the same path
503  */
504 int
505 pci_vfio_map_resource(struct rte_pci_device *dev)
506 {
507         struct vfio_group_status group_status = {
508                         .argsz = sizeof(group_status)
509         };
510         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
511         int vfio_group_fd, vfio_dev_fd;
512         int iommu_group_no;
513         char pci_addr[PATH_MAX] = {0};
514         struct rte_pci_addr *loc = &dev->addr;
515         int i, ret, msix_bar;
516         struct mapped_pci_resource *vfio_res = NULL;
517         struct pci_map *maps;
518
519         dev->intr_handle.fd = -1;
520         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
521
522         /* store PCI address string */
523         rte_snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
524                         loc->domain, loc->bus, loc->devid, loc->function);
525
526         /* get container fd (needs to be done only once per initialization) */
527         if (vfio_cfg.vfio_container_fd == -1) {
528                 int vfio_container_fd = pci_vfio_get_container_fd();
529                 if (vfio_container_fd < 0) {
530                         RTE_LOG(ERR, EAL, "  %s cannot open VFIO container!\n", pci_addr);
531                         return -1;
532                 }
533
534                 vfio_cfg.vfio_container_fd = vfio_container_fd;
535         }
536
537         /* get group number */
538         iommu_group_no = pci_vfio_get_group_no(pci_addr);
539
540         /* if 0, group doesn't exist */
541         if (iommu_group_no == 0) {
542                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
543                                 pci_addr);
544                 return 1;
545         }
546         /* if negative, something failed */
547         else if (iommu_group_no < 0)
548                 return -1;
549
550         /* get the actual group fd */
551         vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
552         if (vfio_group_fd < 0)
553                 return -1;
554
555         /* store group fd */
556         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
557         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
558
559         /* if group_fd == 0, that means the device isn't managed by VFIO */
560         if (vfio_group_fd == 0) {
561                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
562                                 pci_addr);
563                 /* we store 0 as group fd to distinguish between existing but
564                  * unbound VFIO groups, and groups that don't exist at all.
565                  */
566                 vfio_cfg.vfio_group_idx++;
567                 return 1;
568         }
569
570         /*
571          * at this point, we know at least one port on this device is bound to VFIO,
572          * so we can proceed to try and set this particular port up
573          */
574
575         /* check if the group is viable */
576         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
577         if (ret) {
578                 RTE_LOG(ERR, EAL, "  %s cannot get group status!\n", pci_addr);
579                 close(vfio_group_fd);
580                 clear_current_group();
581                 return -1;
582         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
583                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", pci_addr);
584                 close(vfio_group_fd);
585                 clear_current_group();
586                 return -1;
587         }
588
589         /*
590          * at this point, we know that this group is viable (meaning, all devices
591          * are either bound to VFIO or not bound to anything)
592          */
593
594         /* check if group does not have a container yet */
595         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
596
597                 /* add group to a container */
598                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
599                                 &vfio_cfg.vfio_container_fd);
600                 if (ret) {
601                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container!\n",
602                                         pci_addr);
603                         close(vfio_group_fd);
604                         clear_current_group();
605                         return -1;
606                 }
607                 /*
608                  * at this point we know that this group has been successfully
609                  * initialized, so we increment vfio_group_idx to indicate that we can
610                  * add new groups.
611                  */
612                 vfio_cfg.vfio_group_idx++;
613         }
614
615         /*
616          * set up DMA mappings for container
617          *
618          * needs to be done only once, only when at least one group is assigned to
619          * a container and only in primary process
620          */
621         if (internal_config.process_type == RTE_PROC_PRIMARY &&
622                         vfio_cfg.vfio_container_has_dma == 0) {
623                 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
624                 if (ret) {
625                         RTE_LOG(ERR, EAL, "  %s DMA remapping failed!\n", pci_addr);
626                         return -1;
627                 }
628                 vfio_cfg.vfio_container_has_dma = 1;
629         }
630
631         /* get a file descriptor for the device */
632         vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
633         if (vfio_dev_fd < 0) {
634                 /* if we cannot get a device fd, this simply means that this
635                  * particular port is not bound to VFIO
636                  */
637                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
638                                 pci_addr);
639                 return 1;
640         }
641
642         /* test and setup the device */
643         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
644         if (ret) {
645                 RTE_LOG(ERR, EAL, "  %s cannot get device info!\n", pci_addr);
646                 close(vfio_dev_fd);
647                 return -1;
648         }
649
650         /* get MSI-X BAR, if any (we have to know where it is because we can't
651          * mmap it when using VFIO) */
652         msix_bar = -1;
653         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar);
654         if (ret < 0) {
655                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n", pci_addr);
656                 close(vfio_dev_fd);
657                 return -1;
658         }
659
660         /* if we're in a primary process, allocate vfio_res and get region info */
661         if (internal_config.process_type == RTE_PROC_PRIMARY) {
662                 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
663                 if (vfio_res == NULL) {
664                         RTE_LOG(ERR, EAL,
665                                 "%s(): cannot store uio mmap details\n", __func__);
666                         close(vfio_dev_fd);
667                         return -1;
668                 }
669                 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
670
671                 /* get number of registers (up to BAR5) */
672                 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
673                                 VFIO_PCI_BAR5_REGION_INDEX + 1);
674         } else {
675                 /* if we're in a secondary process, just find our tailq entry */
676                 TAILQ_FOREACH(vfio_res, pci_res_list, next) {
677                         if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
678                                 continue;
679                         break;
680                 }
681                 /* if we haven't found our tailq entry, something's wrong */
682                 if (vfio_res == NULL) {
683                         RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
684                                         pci_addr);
685                         close(vfio_dev_fd);
686                         return -1;
687                 }
688         }
689
690         /* map BARs */
691         maps = vfio_res->maps;
692
693         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
694                 struct vfio_region_info reg = { .argsz = sizeof(reg) };
695                 void *bar_addr;
696
697                 reg.index = i;
698
699                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
700
701                 if (ret) {
702                         RTE_LOG(ERR, EAL, "  %s cannot get device region info!\n",
703                                         pci_addr);
704                         close(vfio_dev_fd);
705                         if (internal_config.process_type == RTE_PROC_PRIMARY)
706                                 rte_free(vfio_res);
707                         return -1;
708                 }
709
710                 /* skip non-mmapable BARs */
711                 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
712                         continue;
713
714                 /* skip MSI-X BAR */
715                 if (i == msix_bar)
716                         continue;
717
718                 bar_addr = pci_map_resource(maps[i].addr, vfio_dev_fd, reg.offset,
719                                 reg.size);
720
721                 if (bar_addr == NULL) {
722                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n", pci_addr, i,
723                                         strerror(errno));
724                         close(vfio_dev_fd);
725                         if (internal_config.process_type == RTE_PROC_PRIMARY)
726                                 rte_free(vfio_res);
727                         return -1;
728                 }
729
730                 maps[i].addr = bar_addr;
731                 maps[i].offset = reg.offset;
732                 maps[i].size = reg.size;
733                 dev->mem_resource[i].addr = bar_addr;
734         }
735
736         /* if secondary process, do not set up interrupts */
737         if (internal_config.process_type == RTE_PROC_PRIMARY) {
738                 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
739                         RTE_LOG(ERR, EAL, "  %s error setting up interrupts!\n", pci_addr);
740                         close(vfio_dev_fd);
741                         rte_free(vfio_res);
742                         return -1;
743                 }
744
745                 /* set bus mastering for the device */
746                 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
747                         RTE_LOG(ERR, EAL, "  %s cannot set up bus mastering!\n", pci_addr);
748                         close(vfio_dev_fd);
749                         rte_free(vfio_res);
750                         return -1;
751                 }
752
753                 /* Reset the device */
754                 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
755         }
756
757         if (internal_config.process_type == RTE_PROC_PRIMARY)
758                 TAILQ_INSERT_TAIL(pci_res_list, vfio_res, next);
759
760         return 0;
761 }
762
763 int
764 pci_vfio_enable(void)
765 {
766         /* initialize group list */
767         int i;
768
769         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
770                 vfio_cfg.vfio_groups[i].fd = -1;
771                 vfio_cfg.vfio_groups[i].group_no = -1;
772         }
773         vfio_cfg.vfio_container_fd = -1;
774
775         /* check if we have VFIO driver enabled */
776         if (access(VFIO_DIR, F_OK) == 0)
777                 vfio_cfg.vfio_enabled = 1;
778         else
779                 RTE_LOG(INFO, EAL, "VFIO driver not loaded or wrong permissions\n");
780
781         return 0;
782 }
783
784 int
785 pci_vfio_is_enabled(void)
786 {
787         return vfio_cfg.vfio_enabled;
788 }
789 #endif