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