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