vfio: fix build with Linux < 4.0
[dpdk.git] / drivers / bus / pci / linux / pci_vfio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <fcntl.h>
7 #include <linux/pci_regs.h>
8 #include <sys/eventfd.h>
9 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/mman.h>
12 #include <stdbool.h>
13
14 #include <rte_log.h>
15 #include <rte_pci.h>
16 #include <rte_bus_pci.h>
17 #include <rte_eal_memconfig.h>
18 #include <rte_malloc.h>
19 #include <rte_vfio.h>
20 #include <rte_eal.h>
21 #include <rte_bus.h>
22
23 #include "eal_filesystem.h"
24
25 #include "pci_init.h"
26 #include "private.h"
27
28 /**
29  * @file
30  * PCI probing under linux (VFIO version)
31  *
32  * This code tries to determine if the PCI device is bound to VFIO driver,
33  * and initialize it (map BARs, set up interrupts) if that's the case.
34  *
35  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
36  */
37
38 #ifdef VFIO_PRESENT
39
40 #define PAGE_SIZE   (sysconf(_SC_PAGESIZE))
41 #define PAGE_MASK   (~(PAGE_SIZE - 1))
42
43 static struct rte_tailq_elem rte_vfio_tailq = {
44         .name = "VFIO_RESOURCE_LIST",
45 };
46 EAL_REGISTER_TAILQ(rte_vfio_tailq)
47
48 int
49 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
50                     void *buf, size_t len, off_t offs)
51 {
52         return pread64(intr_handle->vfio_dev_fd, buf, len,
53                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
54 }
55
56 int
57 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
58                     const void *buf, size_t len, off_t offs)
59 {
60         return pwrite64(intr_handle->vfio_dev_fd, buf, len,
61                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
62 }
63
64 /* get PCI BAR number where MSI-X interrupts are */
65 static int
66 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
67 {
68         int ret;
69         uint32_t reg;
70         uint16_t flags;
71         uint8_t cap_id, cap_offset;
72
73         /* read PCI capability pointer from config space */
74         ret = pread64(fd, &reg, sizeof(reg),
75                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
76                         PCI_CAPABILITY_LIST);
77         if (ret != sizeof(reg)) {
78                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
79                                 "config space!\n");
80                 return -1;
81         }
82
83         /* we need first byte */
84         cap_offset = reg & 0xFF;
85
86         while (cap_offset) {
87
88                 /* read PCI capability ID */
89                 ret = pread64(fd, &reg, sizeof(reg),
90                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
91                                 cap_offset);
92                 if (ret != sizeof(reg)) {
93                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
94                                         "config space!\n");
95                         return -1;
96                 }
97
98                 /* we need first byte */
99                 cap_id = reg & 0xFF;
100
101                 /* if we haven't reached MSI-X, check next capability */
102                 if (cap_id != PCI_CAP_ID_MSIX) {
103                         ret = pread64(fd, &reg, sizeof(reg),
104                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
105                                         cap_offset);
106                         if (ret != sizeof(reg)) {
107                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
108                                                 "config space!\n");
109                                 return -1;
110                         }
111
112                         /* we need second byte */
113                         cap_offset = (reg & 0xFF00) >> 8;
114
115                         continue;
116                 }
117                 /* else, read table offset */
118                 else {
119                         /* table offset resides in the next 4 bytes */
120                         ret = pread64(fd, &reg, sizeof(reg),
121                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
122                                         cap_offset + 4);
123                         if (ret != sizeof(reg)) {
124                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
125                                                 "space!\n");
126                                 return -1;
127                         }
128
129                         ret = pread64(fd, &flags, sizeof(flags),
130                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
131                                         cap_offset + 2);
132                         if (ret != sizeof(flags)) {
133                                 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
134                                                 "space!\n");
135                                 return -1;
136                         }
137
138                         msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
139                         msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
140                         msix_table->size =
141                                 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
142
143                         return 0;
144                 }
145         }
146         return 0;
147 }
148
149 /* set PCI bus mastering */
150 static int
151 pci_vfio_set_bus_master(int dev_fd, bool op)
152 {
153         uint16_t reg;
154         int ret;
155
156         ret = pread64(dev_fd, &reg, sizeof(reg),
157                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
158                         PCI_COMMAND);
159         if (ret != sizeof(reg)) {
160                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
161                 return -1;
162         }
163
164         if (op)
165                 /* set the master bit */
166                 reg |= PCI_COMMAND_MASTER;
167         else
168                 reg &= ~(PCI_COMMAND_MASTER);
169
170         ret = pwrite64(dev_fd, &reg, sizeof(reg),
171                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
172                         PCI_COMMAND);
173
174         if (ret != sizeof(reg)) {
175                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 /* set up interrupt support (but not enable interrupts) */
183 static int
184 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
185 {
186         int i, ret, intr_idx;
187         enum rte_intr_mode intr_mode;
188
189         /* default to invalid index */
190         intr_idx = VFIO_PCI_NUM_IRQS;
191
192         /* Get default / configured intr_mode */
193         intr_mode = rte_eal_vfio_intr_mode();
194
195         /* get interrupt type from internal config (MSI-X by default, can be
196          * overridden from the command line
197          */
198         switch (intr_mode) {
199         case RTE_INTR_MODE_MSIX:
200                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
201                 break;
202         case RTE_INTR_MODE_MSI:
203                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
204                 break;
205         case RTE_INTR_MODE_LEGACY:
206                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
207                 break;
208         /* don't do anything if we want to automatically determine interrupt type */
209         case RTE_INTR_MODE_NONE:
210                 break;
211         default:
212                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
213                 return -1;
214         }
215
216         /* start from MSI-X interrupt type */
217         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
218                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
219                 int fd = -1;
220
221                 /* skip interrupt modes we don't want */
222                 if (intr_mode != RTE_INTR_MODE_NONE &&
223                                 i != intr_idx)
224                         continue;
225
226                 irq.index = i;
227
228                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
229                 if (ret < 0) {
230                         RTE_LOG(ERR, EAL, "  cannot get IRQ info, "
231                                         "error %i (%s)\n", errno, strerror(errno));
232                         return -1;
233                 }
234
235                 /* if this vector cannot be used with eventfd, fail if we explicitly
236                  * specified interrupt type, otherwise continue */
237                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
238                         if (intr_mode != RTE_INTR_MODE_NONE) {
239                                 RTE_LOG(ERR, EAL,
240                                                 "  interrupt vector does not support eventfd!\n");
241                                 return -1;
242                         } else
243                                 continue;
244                 }
245
246                 /* set up an eventfd for interrupts */
247                 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
248                 if (fd < 0) {
249                         RTE_LOG(ERR, EAL, "  cannot set up eventfd, "
250                                         "error %i (%s)\n", errno, strerror(errno));
251                         return -1;
252                 }
253
254                 dev->intr_handle.fd = fd;
255                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
256
257                 switch (i) {
258                 case VFIO_PCI_MSIX_IRQ_INDEX:
259                         intr_mode = RTE_INTR_MODE_MSIX;
260                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
261                         break;
262                 case VFIO_PCI_MSI_IRQ_INDEX:
263                         intr_mode = RTE_INTR_MODE_MSI;
264                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
265                         break;
266                 case VFIO_PCI_INTX_IRQ_INDEX:
267                         intr_mode = RTE_INTR_MODE_LEGACY;
268                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
269                         break;
270                 default:
271                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
272                         return -1;
273                 }
274
275                 return 0;
276         }
277
278         /* if we're here, we haven't found a suitable interrupt vector */
279         return -1;
280 }
281
282 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
283 static void
284 pci_vfio_req_handler(void *param)
285 {
286         struct rte_bus *bus;
287         int ret;
288         struct rte_device *device = (struct rte_device *)param;
289
290         bus = rte_bus_find_by_device(device);
291         if (bus == NULL) {
292                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
293                         device->name);
294                 return;
295         }
296
297         /*
298          * vfio kernel module request user space to release allocated
299          * resources before device be deleted in kernel, so it can directly
300          * call the vfio bus hot-unplug handler to process it.
301          */
302         ret = bus->hot_unplug_handler(device);
303         if (ret)
304                 RTE_LOG(ERR, EAL,
305                         "Can not handle hot-unplug for device (%s)\n",
306                         device->name);
307 }
308
309 /* enable notifier (only enable req now) */
310 static int
311 pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
312 {
313         int ret;
314         int fd = -1;
315
316         /* set up an eventfd for req notifier */
317         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
318         if (fd < 0) {
319                 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error %i (%s)\n",
320                         errno, strerror(errno));
321                 return -1;
322         }
323
324         dev->vfio_req_intr_handle.fd = fd;
325         dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_VFIO_REQ;
326         dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
327
328         ret = rte_intr_callback_register(&dev->vfio_req_intr_handle,
329                                          pci_vfio_req_handler,
330                                          (void *)&dev->device);
331         if (ret) {
332                 RTE_LOG(ERR, EAL, "Fail to register req notifier handler.\n");
333                 goto error;
334         }
335
336         ret = rte_intr_enable(&dev->vfio_req_intr_handle);
337         if (ret) {
338                 RTE_LOG(ERR, EAL, "Fail to enable req notifier.\n");
339                 ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
340                                                  pci_vfio_req_handler,
341                                                  (void *)&dev->device);
342                 if (ret)
343                         RTE_LOG(ERR, EAL,
344                                 "Fail to unregister req notifier handler.\n");
345                 goto error;
346         }
347
348         return 0;
349 error:
350         close(fd);
351
352         dev->vfio_req_intr_handle.fd = -1;
353         dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
354         dev->vfio_req_intr_handle.vfio_dev_fd = -1;
355
356         return -1;
357 }
358
359 /* disable notifier (only disable req now) */
360 static int
361 pci_vfio_disable_notifier(struct rte_pci_device *dev)
362 {
363         int ret;
364
365         ret = rte_intr_disable(&dev->vfio_req_intr_handle);
366         if (ret) {
367                 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
368                 return -1;
369         }
370
371         ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
372                                            pci_vfio_req_handler,
373                                            (void *)&dev->device);
374         if (ret) {
375                 RTE_LOG(ERR, EAL,
376                          "fail to unregister req notifier handler.\n");
377                 return -1;
378         }
379
380         close(dev->vfio_req_intr_handle.fd);
381
382         dev->vfio_req_intr_handle.fd = -1;
383         dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
384         dev->vfio_req_intr_handle.vfio_dev_fd = -1;
385
386         return 0;
387 }
388 #endif
389
390 static int
391 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
392 {
393         uint32_t ioport_bar;
394         int ret;
395
396         ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
397                           VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
398                           + PCI_BASE_ADDRESS_0 + bar_index*4);
399         if (ret != sizeof(ioport_bar)) {
400                 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
401                         PCI_BASE_ADDRESS_0 + bar_index*4);
402                 return -1;
403         }
404
405         return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
406 }
407
408 static int
409 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
410 {
411         if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
412                 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
413                 return -1;
414         }
415
416         /* set bus mastering for the device */
417         if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
418                 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
419                 return -1;
420         }
421
422         /*
423          * Reset the device. If the device is not capable of resetting,
424          * then it updates errno as EINVAL.
425          */
426         if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
427                 RTE_LOG(ERR, EAL, "Unable to reset device! Error: %d (%s)\n",
428                                 errno, strerror(errno));
429                 return -1;
430         }
431
432         return 0;
433 }
434
435 static int
436 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
437                 int bar_index, int additional_flags)
438 {
439         struct memreg {
440                 unsigned long offset, size;
441         } memreg[2] = {};
442         void *bar_addr;
443         struct pci_msix_table *msix_table = &vfio_res->msix_table;
444         struct pci_map *bar = &vfio_res->maps[bar_index];
445
446         if (bar->size == 0)
447                 /* Skip this BAR */
448                 return 0;
449
450         if (msix_table->bar_index == bar_index) {
451                 /*
452                  * VFIO will not let us map the MSI-X table,
453                  * but we can map around it.
454                  */
455                 uint32_t table_start = msix_table->offset;
456                 uint32_t table_end = table_start + msix_table->size;
457                 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
458                 table_start &= PAGE_MASK;
459
460                 if (table_start == 0 && table_end >= bar->size) {
461                         /* Cannot map this BAR */
462                         RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
463                         bar->size = 0;
464                         bar->addr = 0;
465                         return 0;
466                 }
467
468                 memreg[0].offset = bar->offset;
469                 memreg[0].size = table_start;
470                 memreg[1].offset = bar->offset + table_end;
471                 memreg[1].size = bar->size - table_end;
472
473                 RTE_LOG(DEBUG, EAL,
474                         "Trying to map BAR%d that contains the MSI-X "
475                         "table. Trying offsets: "
476                         "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", bar_index,
477                         memreg[0].offset, memreg[0].size,
478                         memreg[1].offset, memreg[1].size);
479         } else {
480                 memreg[0].offset = bar->offset;
481                 memreg[0].size = bar->size;
482         }
483
484         /* reserve the address using an inaccessible mapping */
485         bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
486                         MAP_ANONYMOUS | additional_flags, -1, 0);
487         if (bar_addr != MAP_FAILED) {
488                 void *map_addr = NULL;
489                 if (memreg[0].size) {
490                         /* actual map of first part */
491                         map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
492                                                         memreg[0].offset,
493                                                         memreg[0].size,
494                                                         MAP_FIXED);
495                 }
496
497                 /* if there's a second part, try to map it */
498                 if (map_addr != MAP_FAILED
499                         && memreg[1].offset && memreg[1].size) {
500                         void *second_addr = RTE_PTR_ADD(bar_addr,
501                                                         memreg[1].offset -
502                                                         (uintptr_t)bar->offset);
503                         map_addr = pci_map_resource(second_addr,
504                                                         vfio_dev_fd,
505                                                         memreg[1].offset,
506                                                         memreg[1].size,
507                                                         MAP_FIXED);
508                 }
509
510                 if (map_addr == MAP_FAILED || !map_addr) {
511                         munmap(bar_addr, bar->size);
512                         bar_addr = MAP_FAILED;
513                         RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
514                                         bar_index);
515                         return -1;
516                 }
517         } else {
518                 RTE_LOG(ERR, EAL,
519                                 "Failed to create inaccessible mapping for BAR%d\n",
520                                 bar_index);
521                 return -1;
522         }
523
524         bar->addr = bar_addr;
525         return 0;
526 }
527
528 /*
529  * region info may contain capability headers, so we need to keep reallocating
530  * the memory until we match allocated memory size with argsz.
531  */
532 static int
533 pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
534                 int region)
535 {
536         struct vfio_region_info *ri;
537         size_t argsz = sizeof(*ri);
538         int ret;
539
540         ri = malloc(sizeof(*ri));
541         if (ri == NULL) {
542                 RTE_LOG(ERR, EAL, "Cannot allocate memory for region info\n");
543                 return -1;
544         }
545 again:
546         memset(ri, 0, argsz);
547         ri->argsz = argsz;
548         ri->index = region;
549
550         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
551         if (ret < 0) {
552                 free(ri);
553                 return ret;
554         }
555         if (ri->argsz != argsz) {
556                 struct vfio_region_info *tmp;
557
558                 argsz = ri->argsz;
559                 tmp = realloc(ri, argsz);
560
561                 if (tmp == NULL) {
562                         /* realloc failed but the ri is still there */
563                         free(ri);
564                         RTE_LOG(ERR, EAL, "Cannot reallocate memory for region info\n");
565                         return -1;
566                 }
567                 ri = tmp;
568                 goto again;
569         }
570         *info = ri;
571
572         return 0;
573 }
574
575 static struct vfio_info_cap_header *
576 pci_vfio_info_cap(struct vfio_region_info *info, int cap)
577 {
578         struct vfio_info_cap_header *h;
579         size_t offset;
580
581         if ((info->flags & RTE_VFIO_INFO_FLAG_CAPS) == 0) {
582                 /* VFIO info does not advertise capabilities */
583                 return NULL;
584         }
585
586         offset = VFIO_CAP_OFFSET(info);
587         while (offset != 0) {
588                 h = RTE_PTR_ADD(info, offset);
589                 if (h->id == cap)
590                         return h;
591                 offset = h->next;
592         }
593         return NULL;
594 }
595
596 static int
597 pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
598 {
599         struct vfio_region_info *info;
600         int ret;
601
602         ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
603         if (ret < 0)
604                 return -1;
605
606         ret = pci_vfio_info_cap(info, RTE_VFIO_CAP_MSIX_MAPPABLE) != NULL;
607
608         /* cleanup */
609         free(info);
610
611         return ret;
612 }
613
614
615 static int
616 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
617 {
618         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
619         char pci_addr[PATH_MAX] = {0};
620         int vfio_dev_fd;
621         struct rte_pci_addr *loc = &dev->addr;
622         int i, ret;
623         struct mapped_pci_resource *vfio_res = NULL;
624         struct mapped_pci_res_list *vfio_res_list =
625                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
626
627         struct pci_map *maps;
628
629         dev->intr_handle.fd = -1;
630 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
631         dev->vfio_req_intr_handle.fd = -1;
632 #endif
633
634         /* store PCI address string */
635         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
636                         loc->domain, loc->bus, loc->devid, loc->function);
637
638         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
639                                         &vfio_dev_fd, &device_info);
640         if (ret)
641                 return ret;
642
643         /* allocate vfio_res and get region info */
644         vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
645         if (vfio_res == NULL) {
646                 RTE_LOG(ERR, EAL,
647                         "%s(): cannot store uio mmap details\n", __func__);
648                 goto err_vfio_dev_fd;
649         }
650         memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
651
652         /* get number of registers (up to BAR5) */
653         vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
654                         VFIO_PCI_BAR5_REGION_INDEX + 1);
655
656         /* map BARs */
657         maps = vfio_res->maps;
658
659         vfio_res->msix_table.bar_index = -1;
660         /* get MSI-X BAR, if any (we have to know where it is because we can't
661          * easily mmap it when using VFIO)
662          */
663         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
664         if (ret < 0) {
665                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n",
666                                 pci_addr);
667                 goto err_vfio_res;
668         }
669         /* if we found our MSI-X BAR region, check if we can mmap it */
670         if (vfio_res->msix_table.bar_index != -1) {
671                 int ret = pci_vfio_msix_is_mappable(vfio_dev_fd,
672                                 vfio_res->msix_table.bar_index);
673                 if (ret < 0) {
674                         RTE_LOG(ERR, EAL, "Couldn't check if MSI-X BAR is mappable\n");
675                         goto err_vfio_res;
676                 } else if (ret != 0) {
677                         /* we can map it, so we don't care where it is */
678                         RTE_LOG(DEBUG, EAL, "VFIO reports MSI-X BAR as mappable\n");
679                         vfio_res->msix_table.bar_index = -1;
680                 }
681         }
682
683         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
684                 struct vfio_region_info *reg = NULL;
685                 void *bar_addr;
686
687                 ret = pci_vfio_get_region_info(vfio_dev_fd, &reg, i);
688                 if (ret < 0) {
689                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
690                                 "error %i (%s)\n", pci_addr, errno,
691                                 strerror(errno));
692                         goto err_vfio_res;
693                 }
694
695                 /* chk for io port region */
696                 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
697                 if (ret < 0) {
698                         free(reg);
699                         goto err_vfio_res;
700                 } else if (ret) {
701                         RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
702                                         i);
703                         free(reg);
704                         continue;
705                 }
706
707                 /* skip non-mmapable BARs */
708                 if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
709                         free(reg);
710                         continue;
711                 }
712
713                 /* try mapping somewhere close to the end of hugepages */
714                 if (pci_map_addr == NULL)
715                         pci_map_addr = pci_find_max_end_va();
716
717                 bar_addr = pci_map_addr;
718                 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
719
720                 maps[i].addr = bar_addr;
721                 maps[i].offset = reg->offset;
722                 maps[i].size = reg->size;
723                 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
724
725                 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
726                 if (ret < 0) {
727                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
728                                         pci_addr, i, strerror(errno));
729                         free(reg);
730                         goto err_vfio_res;
731                 }
732
733                 dev->mem_resource[i].addr = maps[i].addr;
734
735                 free(reg);
736         }
737
738         if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
739                 RTE_LOG(ERR, EAL, "  %s setup device failed\n", pci_addr);
740                 goto err_vfio_res;
741         }
742
743 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
744         if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
745                 RTE_LOG(ERR, EAL, "Error setting up notifier!\n");
746                 goto err_vfio_res;
747         }
748
749 #endif
750         TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
751
752         return 0;
753 err_vfio_res:
754         rte_free(vfio_res);
755 err_vfio_dev_fd:
756         close(vfio_dev_fd);
757         return -1;
758 }
759
760 static int
761 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
762 {
763         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
764         char pci_addr[PATH_MAX] = {0};
765         int vfio_dev_fd;
766         struct rte_pci_addr *loc = &dev->addr;
767         int i, ret;
768         struct mapped_pci_resource *vfio_res = NULL;
769         struct mapped_pci_res_list *vfio_res_list =
770                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
771
772         struct pci_map *maps;
773
774         dev->intr_handle.fd = -1;
775 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
776         dev->vfio_req_intr_handle.fd = -1;
777 #endif
778
779         /* store PCI address string */
780         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
781                         loc->domain, loc->bus, loc->devid, loc->function);
782
783         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
784                                         &vfio_dev_fd, &device_info);
785         if (ret)
786                 return ret;
787
788         /* if we're in a secondary process, just find our tailq entry */
789         TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
790                 if (rte_pci_addr_cmp(&vfio_res->pci_addr,
791                                                  &dev->addr))
792                         continue;
793                 break;
794         }
795         /* if we haven't found our tailq entry, something's wrong */
796         if (vfio_res == NULL) {
797                 RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
798                                 pci_addr);
799                 goto err_vfio_dev_fd;
800         }
801
802         /* map BARs */
803         maps = vfio_res->maps;
804
805         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
806                 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
807                 if (ret < 0) {
808                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
809                                         pci_addr, i, strerror(errno));
810                         goto err_vfio_dev_fd;
811                 }
812
813                 dev->mem_resource[i].addr = maps[i].addr;
814         }
815
816         /* we need save vfio_dev_fd, so it can be used during release */
817         dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
818 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
819         dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
820 #endif
821
822         return 0;
823 err_vfio_dev_fd:
824         close(vfio_dev_fd);
825         return -1;
826 }
827
828 /*
829  * map the PCI resources of a PCI device in virtual memory (VFIO version).
830  * primary and secondary processes follow almost exactly the same path
831  */
832 int
833 pci_vfio_map_resource(struct rte_pci_device *dev)
834 {
835         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
836                 return pci_vfio_map_resource_primary(dev);
837         else
838                 return pci_vfio_map_resource_secondary(dev);
839 }
840
841 static struct mapped_pci_resource *
842 find_and_unmap_vfio_resource(struct mapped_pci_res_list *vfio_res_list,
843                         struct rte_pci_device *dev,
844                         const char *pci_addr)
845 {
846         struct mapped_pci_resource *vfio_res = NULL;
847         struct pci_map *maps;
848         int i;
849
850         /* Get vfio_res */
851         TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
852                 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
853                         continue;
854                 break;
855         }
856
857         if  (vfio_res == NULL)
858                 return vfio_res;
859
860         RTE_LOG(INFO, EAL, "Releasing pci mapped resource for %s\n",
861                 pci_addr);
862
863         maps = vfio_res->maps;
864         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
865
866                 /*
867                  * We do not need to be aware of MSI-X table BAR mappings as
868                  * when mapping. Just using current maps array is enough
869                  */
870                 if (maps[i].addr) {
871                         RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
872                                 pci_addr, maps[i].addr);
873                         pci_unmap_resource(maps[i].addr, maps[i].size);
874                 }
875         }
876
877         return vfio_res;
878 }
879
880 static int
881 pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
882 {
883         char pci_addr[PATH_MAX] = {0};
884         struct rte_pci_addr *loc = &dev->addr;
885         struct mapped_pci_resource *vfio_res = NULL;
886         struct mapped_pci_res_list *vfio_res_list;
887         int ret;
888
889         /* store PCI address string */
890         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
891                         loc->domain, loc->bus, loc->devid, loc->function);
892
893 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
894         ret = pci_vfio_disable_notifier(dev);
895         if (ret) {
896                 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
897                 return -1;
898         }
899
900 #endif
901         if (close(dev->intr_handle.fd) < 0) {
902                 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
903                         pci_addr);
904                 return -1;
905         }
906
907         if (pci_vfio_set_bus_master(dev->intr_handle.vfio_dev_fd, false)) {
908                 RTE_LOG(ERR, EAL, "  %s cannot unset bus mastering for PCI device!\n",
909                                 pci_addr);
910                 return -1;
911         }
912
913         ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
914                                   dev->intr_handle.vfio_dev_fd);
915         if (ret < 0) {
916                 RTE_LOG(ERR, EAL,
917                         "%s(): cannot release device\n", __func__);
918                 return ret;
919         }
920
921         vfio_res_list =
922                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
923         vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
924
925         /* if we haven't found our tailq entry, something's wrong */
926         if (vfio_res == NULL) {
927                 RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
928                                 pci_addr);
929                 return -1;
930         }
931
932         TAILQ_REMOVE(vfio_res_list, vfio_res, next);
933
934         return 0;
935 }
936
937 static int
938 pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
939 {
940         char pci_addr[PATH_MAX] = {0};
941         struct rte_pci_addr *loc = &dev->addr;
942         struct mapped_pci_resource *vfio_res = NULL;
943         struct mapped_pci_res_list *vfio_res_list;
944         int ret;
945
946         /* store PCI address string */
947         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
948                         loc->domain, loc->bus, loc->devid, loc->function);
949
950         ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
951                                   dev->intr_handle.vfio_dev_fd);
952         if (ret < 0) {
953                 RTE_LOG(ERR, EAL,
954                         "%s(): cannot release device\n", __func__);
955                 return ret;
956         }
957
958         vfio_res_list =
959                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
960         vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
961
962         /* if we haven't found our tailq entry, something's wrong */
963         if (vfio_res == NULL) {
964                 RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
965                                 pci_addr);
966                 return -1;
967         }
968
969         return 0;
970 }
971
972 int
973 pci_vfio_unmap_resource(struct rte_pci_device *dev)
974 {
975         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
976                 return pci_vfio_unmap_resource_primary(dev);
977         else
978                 return pci_vfio_unmap_resource_secondary(dev);
979 }
980
981 int
982 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
983                     struct rte_pci_ioport *p)
984 {
985         if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
986             bar > VFIO_PCI_BAR5_REGION_INDEX) {
987                 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
988                 return -1;
989         }
990
991         p->dev = dev;
992         p->base = VFIO_GET_REGION_ADDR(bar);
993         return 0;
994 }
995
996 void
997 pci_vfio_ioport_read(struct rte_pci_ioport *p,
998                      void *data, size_t len, off_t offset)
999 {
1000         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1001
1002         if (pread64(intr_handle->vfio_dev_fd, data,
1003                     len, p->base + offset) <= 0)
1004                 RTE_LOG(ERR, EAL,
1005                         "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
1006                         VFIO_GET_REGION_IDX(p->base), (int)offset);
1007 }
1008
1009 void
1010 pci_vfio_ioport_write(struct rte_pci_ioport *p,
1011                       const void *data, size_t len, off_t offset)
1012 {
1013         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1014
1015         if (pwrite64(intr_handle->vfio_dev_fd, data,
1016                      len, p->base + offset) <= 0)
1017                 RTE_LOG(ERR, EAL,
1018                         "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1019                         VFIO_GET_REGION_IDX(p->base), (int)offset);
1020 }
1021
1022 int
1023 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1024 {
1025         RTE_SET_USED(p);
1026         return -1;
1027 }
1028
1029 int
1030 pci_vfio_is_enabled(void)
1031 {
1032         return rte_vfio_is_enabled("vfio_pci");
1033 }
1034 #endif