tailq: remove unneeded inclusions
[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 #include <sys/mman.h>
41
42 #include <rte_log.h>
43 #include <rte_pci.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_malloc.h>
46 #include <eal_private.h>
47
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
50 #include "eal_vfio.h"
51
52 /**
53  * @file
54  * PCI probing under linux (VFIO version)
55  *
56  * This code tries to determine if the PCI device is bound to VFIO driver,
57  * and initialize it (map BARs, set up interrupts) if that's the case.
58  *
59  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
60  */
61
62 #ifdef VFIO_PRESENT
63
64 #define PAGE_SIZE   (sysconf(_SC_PAGESIZE))
65 #define PAGE_MASK   (~(PAGE_SIZE - 1))
66
67 #define VFIO_DIR "/dev/vfio"
68 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
69 #define VFIO_GROUP_FMT "/dev/vfio/%u"
70 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
71
72 /* per-process VFIO config */
73 static struct vfio_config vfio_cfg;
74
75 /* get PCI BAR number where MSI-X interrupts are */
76 static int
77 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
78                       uint32_t *msix_table_size)
79 {
80         int ret;
81         uint32_t reg;
82         uint16_t flags;
83         uint8_t cap_id, cap_offset;
84
85         /* read PCI capability pointer from config space */
86         ret = pread64(fd, &reg, sizeof(reg),
87                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
88                         PCI_CAPABILITY_LIST);
89         if (ret != sizeof(reg)) {
90                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
91                                 "config space!\n");
92                 return -1;
93         }
94
95         /* we need first byte */
96         cap_offset = reg & 0xFF;
97
98         while (cap_offset) {
99
100                 /* read PCI capability ID */
101                 ret = pread64(fd, &reg, sizeof(reg),
102                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
103                                 cap_offset);
104                 if (ret != sizeof(reg)) {
105                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
106                                         "config space!\n");
107                         return -1;
108                 }
109
110                 /* we need first byte */
111                 cap_id = reg & 0xFF;
112
113                 /* if we haven't reached MSI-X, check next capability */
114                 if (cap_id != PCI_CAP_ID_MSIX) {
115                         ret = pread64(fd, &reg, sizeof(reg),
116                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
117                                         cap_offset);
118                         if (ret != sizeof(reg)) {
119                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
120                                                 "config space!\n");
121                                 return -1;
122                         }
123
124                         /* we need second byte */
125                         cap_offset = (reg & 0xFF00) >> 8;
126
127                         continue;
128                 }
129                 /* else, read table offset */
130                 else {
131                         /* table offset resides in the next 4 bytes */
132                         ret = pread64(fd, &reg, sizeof(reg),
133                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
134                                         cap_offset + 4);
135                         if (ret != sizeof(reg)) {
136                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
137                                                 "space!\n");
138                                 return -1;
139                         }
140
141                         ret = pread64(fd, &flags, sizeof(flags),
142                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
143                                         cap_offset + 2);
144                         if (ret != sizeof(flags)) {
145                                 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
146                                                 "space!\n");
147                                 return -1;
148                         }
149
150                         *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
151                         *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
152                         *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
153
154                         return 0;
155                 }
156         }
157         return 0;
158 }
159
160 /* set PCI bus mastering */
161 static int
162 pci_vfio_set_bus_master(int dev_fd)
163 {
164         uint16_t reg;
165         int ret;
166
167         ret = pread64(dev_fd, &reg, sizeof(reg),
168                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
169                         PCI_COMMAND);
170         if (ret != sizeof(reg)) {
171                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
172                 return -1;
173         }
174
175         /* set the master bit */
176         reg |= PCI_COMMAND_MASTER;
177
178         ret = pwrite64(dev_fd, &reg, sizeof(reg),
179                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
180                         PCI_COMMAND);
181
182         if (ret != sizeof(reg)) {
183                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
184                 return -1;
185         }
186
187         return 0;
188 }
189
190 /* set up DMA mappings */
191 static int
192 pci_vfio_setup_dma_maps(int vfio_container_fd)
193 {
194         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
195         int i, ret;
196
197         ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
198                         VFIO_TYPE1_IOMMU);
199         if (ret) {
200                 RTE_LOG(ERR, EAL, "  cannot set IOMMU type, "
201                                 "error %i (%s)\n", errno, strerror(errno));
202                 return -1;
203         }
204
205         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
206         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
207                 struct vfio_iommu_type1_dma_map dma_map;
208
209                 if (ms[i].addr == NULL)
210                         break;
211
212                 memset(&dma_map, 0, sizeof(dma_map));
213                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
214                 dma_map.vaddr = ms[i].addr_64;
215                 dma_map.size = ms[i].len;
216                 dma_map.iova = ms[i].phys_addr;
217                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
218
219                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
220
221                 if (ret) {
222                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
223                                         "error %i (%s)\n", errno, strerror(errno));
224                         return -1;
225                 }
226         }
227
228         return 0;
229 }
230
231 /* set up interrupt support (but not enable interrupts) */
232 static int
233 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
234 {
235         int i, ret, intr_idx;
236
237         /* default to invalid index */
238         intr_idx = VFIO_PCI_NUM_IRQS;
239
240         /* get interrupt type from internal config (MSI-X by default, can be
241          * overriden from the command line
242          */
243         switch (internal_config.vfio_intr_mode) {
244         case RTE_INTR_MODE_MSIX:
245                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
246                 break;
247         case RTE_INTR_MODE_MSI:
248                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
249                 break;
250         case RTE_INTR_MODE_LEGACY:
251                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
252                 break;
253         /* don't do anything if we want to automatically determine interrupt type */
254         case RTE_INTR_MODE_NONE:
255                 break;
256         default:
257                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
258                 return -1;
259         }
260
261         /* start from MSI-X interrupt type */
262         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
263                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
264                 int fd = -1;
265
266                 /* skip interrupt modes we don't want */
267                 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
268                                 i != intr_idx)
269                         continue;
270
271                 irq.index = i;
272
273                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
274                 if (ret < 0) {
275                         RTE_LOG(ERR, EAL, "  cannot get IRQ info, "
276                                         "error %i (%s)\n", errno, strerror(errno));
277                         return -1;
278                 }
279
280                 /* if this vector cannot be used with eventfd, fail if we explicitly
281                  * specified interrupt type, otherwise continue */
282                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
283                         if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
284                                 RTE_LOG(ERR, EAL,
285                                                 "  interrupt vector does not support eventfd!\n");
286                                 return -1;
287                         } else
288                                 continue;
289                 }
290
291                 /* set up an eventfd for interrupts */
292                 fd = eventfd(0, 0);
293                 if (fd < 0) {
294                         RTE_LOG(ERR, EAL, "  cannot set up eventfd, "
295                                         "error %i (%s)\n", errno, strerror(errno));
296                         return -1;
297                 }
298
299                 dev->intr_handle.fd = fd;
300                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
301
302                 switch (i) {
303                 case VFIO_PCI_MSIX_IRQ_INDEX:
304                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
305                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
306                         break;
307                 case VFIO_PCI_MSI_IRQ_INDEX:
308                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
309                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
310                         break;
311                 case VFIO_PCI_INTX_IRQ_INDEX:
312                         internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
313                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
314                         break;
315                 default:
316                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
317                         return -1;
318                 }
319
320                 return 0;
321         }
322
323         /* if we're here, we haven't found a suitable interrupt vector */
324         return -1;
325 }
326
327 /* open container fd or get an existing one */
328 int
329 pci_vfio_get_container_fd(void)
330 {
331         int ret, vfio_container_fd;
332
333         /* if we're in a primary process, try to open the container */
334         if (internal_config.process_type == RTE_PROC_PRIMARY) {
335                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
336                 if (vfio_container_fd < 0) {
337                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
338                                         "error %i (%s)\n", errno, strerror(errno));
339                         return -1;
340                 }
341
342                 /* check VFIO API version */
343                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
344                 if (ret != VFIO_API_VERSION) {
345                         if (ret < 0)
346                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
347                                                 "error %i (%s)\n", errno, strerror(errno));
348                         else
349                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
350                         close(vfio_container_fd);
351                         return -1;
352                 }
353
354                 /* check if we support IOMMU type 1 */
355                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
356                 if (ret != 1) {
357                         if (ret < 0)
358                                 RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
359                                         "error %i (%s)\n", errno,
360                                         strerror(errno));
361                         else
362                                 RTE_LOG(ERR, EAL, "  unsupported IOMMU type "
363                                         "detected in VFIO\n");
364                         close(vfio_container_fd);
365                         return -1;
366                 }
367
368                 return vfio_container_fd;
369         } else {
370                 /*
371                  * if we're in a secondary process, request container fd from the
372                  * primary process via our socket
373                  */
374                 int socket_fd;
375
376                 socket_fd = vfio_mp_sync_connect_to_primary();
377                 if (socket_fd < 0) {
378                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
379                         return -1;
380                 }
381                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
382                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
383                         close(socket_fd);
384                         return -1;
385                 }
386                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
387                 if (vfio_container_fd < 0) {
388                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
389                         close(socket_fd);
390                         return -1;
391                 }
392                 close(socket_fd);
393                 return vfio_container_fd;
394         }
395
396         return -1;
397 }
398
399 /* open group fd or get an existing one */
400 int
401 pci_vfio_get_group_fd(int iommu_group_no)
402 {
403         int i;
404         int vfio_group_fd;
405         char filename[PATH_MAX];
406
407         /* check if we already have the group descriptor open */
408         for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
409                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
410                         return vfio_cfg.vfio_groups[i].fd;
411
412         /* if primary, try to open the group */
413         if (internal_config.process_type == RTE_PROC_PRIMARY) {
414                 snprintf(filename, sizeof(filename),
415                                  VFIO_GROUP_FMT, iommu_group_no);
416                 vfio_group_fd = open(filename, O_RDWR);
417                 if (vfio_group_fd < 0) {
418                         /* if file not found, it's not an error */
419                         if (errno != ENOENT) {
420                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
421                                                 strerror(errno));
422                                 return -1;
423                         }
424                         return 0;
425                 }
426
427                 /* if the fd is valid, create a new group for it */
428                 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
429                         RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
430                         return -1;
431                 }
432                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
433                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
434                 return vfio_group_fd;
435         }
436         /* if we're in a secondary process, request group fd from the primary
437          * process via our socket
438          */
439         else {
440                 int socket_fd, ret;
441
442                 socket_fd = vfio_mp_sync_connect_to_primary();
443
444                 if (socket_fd < 0) {
445                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
446                         return -1;
447                 }
448                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
449                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
450                         close(socket_fd);
451                         return -1;
452                 }
453                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
454                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
455                         close(socket_fd);
456                         return -1;
457                 }
458                 ret = vfio_mp_sync_receive_request(socket_fd);
459                 switch (ret) {
460                 case SOCKET_NO_FD:
461                         close(socket_fd);
462                         return 0;
463                 case SOCKET_OK:
464                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
465                         /* if we got the fd, return it */
466                         if (vfio_group_fd > 0) {
467                                 close(socket_fd);
468                                 return vfio_group_fd;
469                         }
470                         /* fall-through on error */
471                 default:
472                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
473                         close(socket_fd);
474                         return -1;
475                 }
476         }
477         return -1;
478 }
479
480 /* parse IOMMU group number for a PCI device
481  * returns -1 for errors, 0 for non-existent group */
482 static int
483 pci_vfio_get_group_no(const char *pci_addr)
484 {
485         char linkname[PATH_MAX];
486         char filename[PATH_MAX];
487         char *tok[16], *group_tok, *end;
488         int ret, iommu_group_no;
489
490         memset(linkname, 0, sizeof(linkname));
491         memset(filename, 0, sizeof(filename));
492
493         /* try to find out IOMMU group for this device */
494         snprintf(linkname, sizeof(linkname),
495                          SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
496
497         ret = readlink(linkname, filename, sizeof(filename));
498
499         /* if the link doesn't exist, no VFIO for us */
500         if (ret < 0)
501                 return 0;
502
503         ret = rte_strsplit(filename, sizeof(filename),
504                         tok, RTE_DIM(tok), '/');
505
506         if (ret <= 0) {
507                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", pci_addr);
508                 return -1;
509         }
510
511         /* IOMMU group is always the last token */
512         errno = 0;
513         group_tok = tok[ret - 1];
514         end = group_tok;
515         iommu_group_no = strtol(group_tok, &end, 10);
516         if ((end != group_tok && *end != '\0') || errno != 0) {
517                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", pci_addr);
518                 return -1;
519         }
520
521         return iommu_group_no;
522 }
523
524 static void
525 clear_current_group(void)
526 {
527         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
528         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
529 }
530
531
532 /*
533  * map the PCI resources of a PCI device in virtual memory (VFIO version).
534  * primary and secondary processes follow almost exactly the same path
535  */
536 int
537 pci_vfio_map_resource(struct rte_pci_device *dev)
538 {
539         struct vfio_group_status group_status = {
540                         .argsz = sizeof(group_status)
541         };
542         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
543         int vfio_group_fd, vfio_dev_fd;
544         int iommu_group_no;
545         char pci_addr[PATH_MAX] = {0};
546         struct rte_pci_addr *loc = &dev->addr;
547         int i, ret, msix_bar;
548         struct mapped_pci_resource *vfio_res = NULL;
549         struct pci_map *maps;
550         uint32_t msix_table_offset = 0;
551         uint32_t msix_table_size = 0;
552
553         dev->intr_handle.fd = -1;
554         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
555
556         /* store PCI address string */
557         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
558                         loc->domain, loc->bus, loc->devid, loc->function);
559
560         /* get group number */
561         iommu_group_no = pci_vfio_get_group_no(pci_addr);
562
563         /* if 0, group doesn't exist */
564         if (iommu_group_no == 0) {
565                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
566                                 pci_addr);
567                 return 1;
568         }
569         /* if negative, something failed */
570         else if (iommu_group_no < 0)
571                 return -1;
572
573         /* get the actual group fd */
574         vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
575         if (vfio_group_fd < 0)
576                 return -1;
577
578         /* store group fd */
579         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
580         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
581
582         /* if group_fd == 0, that means the device isn't managed by VFIO */
583         if (vfio_group_fd == 0) {
584                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
585                                 pci_addr);
586                 /* we store 0 as group fd to distinguish between existing but
587                  * unbound VFIO groups, and groups that don't exist at all.
588                  */
589                 vfio_cfg.vfio_group_idx++;
590                 return 1;
591         }
592
593         /*
594          * at this point, we know at least one port on this device is bound to VFIO,
595          * so we can proceed to try and set this particular port up
596          */
597
598         /* check if the group is viable */
599         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
600         if (ret) {
601                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
602                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
603                 close(vfio_group_fd);
604                 clear_current_group();
605                 return -1;
606         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
607                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", pci_addr);
608                 close(vfio_group_fd);
609                 clear_current_group();
610                 return -1;
611         }
612
613         /*
614          * at this point, we know that this group is viable (meaning, all devices
615          * are either bound to VFIO or not bound to anything)
616          */
617
618         /* check if group does not have a container yet */
619         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
620
621                 /* add group to a container */
622                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
623                                 &vfio_cfg.vfio_container_fd);
624                 if (ret) {
625                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
626                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
627                         close(vfio_group_fd);
628                         clear_current_group();
629                         return -1;
630                 }
631                 /*
632                  * at this point we know that this group has been successfully
633                  * initialized, so we increment vfio_group_idx to indicate that we can
634                  * add new groups.
635                  */
636                 vfio_cfg.vfio_group_idx++;
637         }
638
639         /*
640          * set up DMA mappings for container
641          *
642          * needs to be done only once, only when at least one group is assigned to
643          * a container and only in primary process
644          */
645         if (internal_config.process_type == RTE_PROC_PRIMARY &&
646                         vfio_cfg.vfio_container_has_dma == 0) {
647                 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
648                 if (ret) {
649                         RTE_LOG(ERR, EAL, "  %s DMA remapping failed, "
650                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
651                         return -1;
652                 }
653                 vfio_cfg.vfio_container_has_dma = 1;
654         }
655
656         /* get a file descriptor for the device */
657         vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
658         if (vfio_dev_fd < 0) {
659                 /* if we cannot get a device fd, this simply means that this
660                  * particular port is not bound to VFIO
661                  */
662                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
663                                 pci_addr);
664                 return 1;
665         }
666
667         /* test and setup the device */
668         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
669         if (ret) {
670                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
671                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
672                 close(vfio_dev_fd);
673                 return -1;
674         }
675
676         /* get MSI-X BAR, if any (we have to know where it is because we can't
677          * easily mmap it when using VFIO) */
678         msix_bar = -1;
679         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
680                                     &msix_table_offset, &msix_table_size);
681         if (ret < 0) {
682                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n", pci_addr);
683                 close(vfio_dev_fd);
684                 return -1;
685         }
686
687         /* if we're in a primary process, allocate vfio_res and get region info */
688         if (internal_config.process_type == RTE_PROC_PRIMARY) {
689                 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
690                 if (vfio_res == NULL) {
691                         RTE_LOG(ERR, EAL,
692                                 "%s(): cannot store uio mmap details\n", __func__);
693                         close(vfio_dev_fd);
694                         return -1;
695                 }
696                 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
697
698                 /* get number of registers (up to BAR5) */
699                 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
700                                 VFIO_PCI_BAR5_REGION_INDEX + 1);
701         } else {
702                 /* if we're in a secondary process, just find our tailq entry */
703                 TAILQ_FOREACH(vfio_res, pci_res_list, next) {
704                         if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
705                                 continue;
706                         break;
707                 }
708                 /* if we haven't found our tailq entry, something's wrong */
709                 if (vfio_res == NULL) {
710                         RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
711                                         pci_addr);
712                         close(vfio_dev_fd);
713                         return -1;
714                 }
715         }
716
717         /* map BARs */
718         maps = vfio_res->maps;
719
720         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
721                 struct vfio_region_info reg = { .argsz = sizeof(reg) };
722                 void *bar_addr;
723                 struct memreg {
724                         uint32_t offset, size;
725                 } memreg[2] = {};
726
727                 reg.index = i;
728
729                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
730
731                 if (ret) {
732                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
733                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
734                         close(vfio_dev_fd);
735                         if (internal_config.process_type == RTE_PROC_PRIMARY)
736                                 rte_free(vfio_res);
737                         return -1;
738                 }
739
740                 /* skip non-mmapable BARs */
741                 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
742                         continue;
743
744                 if (i == msix_bar) {
745                         /*
746                          * VFIO will not let us map the MSI-X table,
747                          * but we can map around it.
748                          */
749                         uint32_t table_start = msix_table_offset;
750                         uint32_t table_end = table_start + msix_table_size;
751                         table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
752                         table_start &= PAGE_MASK;
753
754                         if (table_start == 0 && table_end >= reg.size) {
755                                 /* Cannot map this BAR */
756                                 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
757                                 continue;
758                         } else {
759                                 memreg[0].offset = reg.offset;
760                                 memreg[0].size = table_start;
761                                 memreg[1].offset = table_end;
762                                 memreg[1].size = reg.size - table_end;
763
764                                 RTE_LOG(DEBUG, EAL,
765                                         "Trying to map BAR %d that contains the MSI-X "
766                                         "table. Trying offsets: "
767                                         "%04x:%04x, %04x:%04x\n", i,
768                                         memreg[0].offset, memreg[0].size,
769                                         memreg[1].offset, memreg[1].size);
770                         }
771                 } else {
772                         memreg[0].offset = reg.offset;
773                         memreg[0].size = reg.size;
774                 }
775
776                 /* try to figure out an address */
777                 if (internal_config.process_type == RTE_PROC_PRIMARY) {
778                         /* try mapping somewhere close to the end of hugepages */
779                         if (pci_map_addr == NULL)
780                                 pci_map_addr = pci_find_max_end_va();
781
782                         bar_addr = pci_map_addr;
783                         pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
784                 } else {
785                         bar_addr = maps[i].addr;
786                 }
787
788                 /* reserve the address using an inaccessible mapping */
789                 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
790                                 MAP_ANONYMOUS, -1, 0);
791                 if (bar_addr != MAP_FAILED) {
792                         void *map_addr = NULL;
793                         if (memreg[0].size) {
794                                 /* actual map of first part */
795                                 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
796                                                             memreg[0].offset,
797                                                             memreg[0].size,
798                                                             MAP_FIXED);
799                         }
800
801                         /* if there's a second part, try to map it */
802                         if (map_addr != MAP_FAILED
803                             && memreg[1].offset && memreg[1].size) {
804                                 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
805                                 map_addr = pci_map_resource(second_addr,
806                                                             vfio_dev_fd, memreg[1].offset,
807                                                             memreg[1].size,
808                                                             MAP_FIXED);
809                         }
810
811                         if (map_addr == MAP_FAILED || !map_addr) {
812                                 munmap(bar_addr, reg.size);
813                                 bar_addr = MAP_FAILED;
814                         }
815                 }
816
817                 if (bar_addr == MAP_FAILED ||
818                                 (internal_config.process_type == RTE_PROC_SECONDARY &&
819                                                 bar_addr != maps[i].addr)) {
820                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n", pci_addr, i,
821                                         strerror(errno));
822                         close(vfio_dev_fd);
823                         if (internal_config.process_type == RTE_PROC_PRIMARY)
824                                 rte_free(vfio_res);
825                         return -1;
826                 }
827
828                 maps[i].addr = bar_addr;
829                 maps[i].offset = reg.offset;
830                 maps[i].size = reg.size;
831                 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
832                 dev->mem_resource[i].addr = bar_addr;
833         }
834
835         /* if secondary process, do not set up interrupts */
836         if (internal_config.process_type == RTE_PROC_PRIMARY) {
837                 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
838                         RTE_LOG(ERR, EAL, "  %s error setting up interrupts!\n", pci_addr);
839                         close(vfio_dev_fd);
840                         rte_free(vfio_res);
841                         return -1;
842                 }
843
844                 /* set bus mastering for the device */
845                 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
846                         RTE_LOG(ERR, EAL, "  %s cannot set up bus mastering!\n", pci_addr);
847                         close(vfio_dev_fd);
848                         rte_free(vfio_res);
849                         return -1;
850                 }
851
852                 /* Reset the device */
853                 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
854         }
855
856         if (internal_config.process_type == RTE_PROC_PRIMARY)
857                 TAILQ_INSERT_TAIL(pci_res_list, vfio_res, next);
858
859         return 0;
860 }
861
862 int
863 pci_vfio_enable(void)
864 {
865         /* initialize group list */
866         int i;
867         int module_vfio_type1;
868
869         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
870                 vfio_cfg.vfio_groups[i].fd = -1;
871                 vfio_cfg.vfio_groups[i].group_no = -1;
872         }
873
874         module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
875
876         /* return error directly */
877         if (module_vfio_type1 == -1) {
878                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
879                 return -1;
880         }
881
882         /* return 0 if VFIO modules not loaded */
883         if (module_vfio_type1 == 0) {
884                 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
885                         "skip VFIO support...\n");
886                 return 0;
887         }
888
889         vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
890
891         /* check if we have VFIO driver enabled */
892         if (vfio_cfg.vfio_container_fd != -1)
893                 vfio_cfg.vfio_enabled = 1;
894         else
895                 RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n");
896
897         return 0;
898 }
899
900 int
901 pci_vfio_is_enabled(void)
902 {
903         return vfio_cfg.vfio_enabled;
904 }
905 #endif