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