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