1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <linux/pci_regs.h>
8 #include <sys/eventfd.h>
9 #include <sys/socket.h>
10 #include <sys/ioctl.h>
16 #include <rte_bus_pci.h>
17 #include <rte_eal_memconfig.h>
18 #include <rte_malloc.h>
23 #include "eal_filesystem.h"
30 * PCI probing under linux (VFIO version)
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.
35 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
40 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
41 #define PAGE_MASK (~(PAGE_SIZE - 1))
43 static struct rte_tailq_elem rte_vfio_tailq = {
44 .name = "VFIO_RESOURCE_LIST",
46 EAL_REGISTER_TAILQ(rte_vfio_tailq)
49 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
50 void *buf, size_t len, off_t offs)
52 return pread64(intr_handle->vfio_dev_fd, buf, len,
53 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
57 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
58 const void *buf, size_t len, off_t offs)
60 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
61 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
64 /* get PCI BAR number where MSI-X interrupts are */
66 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
71 uint8_t cap_id, cap_offset;
73 /* read PCI capability pointer from config space */
74 ret = pread64(fd, ®, sizeof(reg),
75 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
77 if (ret != sizeof(reg)) {
78 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
83 /* we need first byte */
84 cap_offset = reg & 0xFF;
88 /* read PCI capability ID */
89 ret = pread64(fd, ®, sizeof(reg),
90 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
92 if (ret != sizeof(reg)) {
93 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
98 /* we need first byte */
101 /* if we haven't reached MSI-X, check next capability */
102 if (cap_id != PCI_CAP_ID_MSIX) {
103 ret = pread64(fd, ®, sizeof(reg),
104 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
106 if (ret != sizeof(reg)) {
107 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
112 /* we need second byte */
113 cap_offset = (reg & 0xFF00) >> 8;
117 /* else, read table offset */
119 /* table offset resides in the next 4 bytes */
120 ret = pread64(fd, ®, sizeof(reg),
121 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
123 if (ret != sizeof(reg)) {
124 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
129 ret = pread64(fd, &flags, sizeof(flags),
130 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
132 if (ret != sizeof(flags)) {
133 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
138 msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
139 msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
141 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
149 /* set PCI bus mastering */
151 pci_vfio_set_bus_master(int dev_fd, bool op)
156 ret = pread64(dev_fd, ®, sizeof(reg),
157 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
159 if (ret != sizeof(reg)) {
160 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
165 /* set the master bit */
166 reg |= PCI_COMMAND_MASTER;
168 reg &= ~(PCI_COMMAND_MASTER);
170 ret = pwrite64(dev_fd, ®, sizeof(reg),
171 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
174 if (ret != sizeof(reg)) {
175 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
182 /* set up interrupt support (but not enable interrupts) */
184 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
186 int i, ret, intr_idx;
187 enum rte_intr_mode intr_mode;
189 /* default to invalid index */
190 intr_idx = VFIO_PCI_NUM_IRQS;
192 /* Get default / configured intr_mode */
193 intr_mode = rte_eal_vfio_intr_mode();
195 /* get interrupt type from internal config (MSI-X by default, can be
196 * overridden from the command line
199 case RTE_INTR_MODE_MSIX:
200 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
202 case RTE_INTR_MODE_MSI:
203 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
205 case RTE_INTR_MODE_LEGACY:
206 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
208 /* don't do anything if we want to automatically determine interrupt type */
209 case RTE_INTR_MODE_NONE:
212 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
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) };
221 /* skip interrupt modes we don't want */
222 if (intr_mode != RTE_INTR_MODE_NONE &&
228 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
230 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
231 "error %i (%s)\n", errno, strerror(errno));
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) {
240 " interrupt vector does not support eventfd!\n");
246 /* set up an eventfd for interrupts */
247 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
249 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
250 "error %i (%s)\n", errno, strerror(errno));
254 dev->intr_handle.fd = fd;
255 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
258 case VFIO_PCI_MSIX_IRQ_INDEX:
259 intr_mode = RTE_INTR_MODE_MSIX;
260 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
262 case VFIO_PCI_MSI_IRQ_INDEX:
263 intr_mode = RTE_INTR_MODE_MSI;
264 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
266 case VFIO_PCI_INTX_IRQ_INDEX:
267 intr_mode = RTE_INTR_MODE_LEGACY;
268 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
271 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
278 /* if we're here, we haven't found a suitable interrupt vector */
283 pci_vfio_req_handler(void *param)
287 struct rte_device *device = (struct rte_device *)param;
289 bus = rte_bus_find_by_device(device);
291 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
297 * vfio kernel module request user space to release allocated
298 * resources before device be deleted in kernel, so it can directly
299 * call the vfio bus hot-unplug handler to process it.
301 ret = bus->hot_unplug_handler(device);
304 "Can not handle hot-unplug for device (%s)\n",
308 /* enable notifier (only enable req now) */
310 pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
315 /* set up an eventfd for req notifier */
316 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
318 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error %i (%s)\n",
319 errno, strerror(errno));
323 dev->vfio_req_intr_handle.fd = fd;
324 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_VFIO_REQ;
325 dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
327 ret = rte_intr_callback_register(&dev->vfio_req_intr_handle,
328 pci_vfio_req_handler,
329 (void *)&dev->device);
331 RTE_LOG(ERR, EAL, "Fail to register req notifier handler.\n");
335 ret = rte_intr_enable(&dev->vfio_req_intr_handle);
337 RTE_LOG(ERR, EAL, "Fail to enable req notifier.\n");
338 ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
339 pci_vfio_req_handler,
340 (void *)&dev->device);
343 "Fail to unregister req notifier handler.\n");
351 dev->vfio_req_intr_handle.fd = -1;
352 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
353 dev->vfio_req_intr_handle.vfio_dev_fd = -1;
358 /* disable notifier (only disable req now) */
360 pci_vfio_disable_notifier(struct rte_pci_device *dev)
364 ret = rte_intr_disable(&dev->vfio_req_intr_handle);
366 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
370 ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
371 pci_vfio_req_handler,
372 (void *)&dev->device);
375 "fail to unregister req notifier handler.\n");
379 close(dev->vfio_req_intr_handle.fd);
381 dev->vfio_req_intr_handle.fd = -1;
382 dev->vfio_req_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
383 dev->vfio_req_intr_handle.vfio_dev_fd = -1;
389 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
394 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
395 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
396 + PCI_BASE_ADDRESS_0 + bar_index*4);
397 if (ret != sizeof(ioport_bar)) {
398 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
399 PCI_BASE_ADDRESS_0 + bar_index*4);
403 return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
407 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
409 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
410 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
414 /* set bus mastering for the device */
415 if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
416 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
421 * Reset the device. If the device is not capable of resetting,
422 * then it updates errno as EINVAL.
424 if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
425 RTE_LOG(ERR, EAL, "Unable to reset device! Error: %d (%s)\n",
426 errno, strerror(errno));
434 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
435 int bar_index, int additional_flags)
438 unsigned long offset, size;
441 struct pci_msix_table *msix_table = &vfio_res->msix_table;
442 struct pci_map *bar = &vfio_res->maps[bar_index];
448 if (msix_table->bar_index == bar_index) {
450 * VFIO will not let us map the MSI-X table,
451 * but we can map around it.
453 uint32_t table_start = msix_table->offset;
454 uint32_t table_end = table_start + msix_table->size;
455 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
456 table_start &= PAGE_MASK;
458 if (table_start == 0 && table_end >= bar->size) {
459 /* Cannot map this BAR */
460 RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
466 memreg[0].offset = bar->offset;
467 memreg[0].size = table_start;
468 memreg[1].offset = bar->offset + table_end;
469 memreg[1].size = bar->size - table_end;
472 "Trying to map BAR%d that contains the MSI-X "
473 "table. Trying offsets: "
474 "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", bar_index,
475 memreg[0].offset, memreg[0].size,
476 memreg[1].offset, memreg[1].size);
478 memreg[0].offset = bar->offset;
479 memreg[0].size = bar->size;
482 /* reserve the address using an inaccessible mapping */
483 bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
484 MAP_ANONYMOUS | additional_flags, -1, 0);
485 if (bar_addr != MAP_FAILED) {
486 void *map_addr = NULL;
487 if (memreg[0].size) {
488 /* actual map of first part */
489 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
495 /* if there's a second part, try to map it */
496 if (map_addr != MAP_FAILED
497 && memreg[1].offset && memreg[1].size) {
498 void *second_addr = RTE_PTR_ADD(bar_addr,
500 (uintptr_t)bar->offset);
501 map_addr = pci_map_resource(second_addr,
508 if (map_addr == MAP_FAILED || !map_addr) {
509 munmap(bar_addr, bar->size);
510 bar_addr = MAP_FAILED;
511 RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
517 "Failed to create inaccessible mapping for BAR%d\n",
522 bar->addr = bar_addr;
527 * region info may contain capability headers, so we need to keep reallocating
528 * the memory until we match allocated memory size with argsz.
531 pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
534 struct vfio_region_info *ri;
535 size_t argsz = sizeof(*ri);
538 ri = malloc(sizeof(*ri));
540 RTE_LOG(ERR, EAL, "Cannot allocate memory for region info\n");
544 memset(ri, 0, argsz);
548 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
553 if (ri->argsz != argsz) {
554 struct vfio_region_info *tmp;
557 tmp = realloc(ri, argsz);
560 /* realloc failed but the ri is still there */
562 RTE_LOG(ERR, EAL, "Cannot reallocate memory for region info\n");
573 static struct vfio_info_cap_header *
574 pci_vfio_info_cap(struct vfio_region_info *info, int cap)
576 struct vfio_info_cap_header *h;
579 if ((info->flags & RTE_VFIO_INFO_FLAG_CAPS) == 0) {
580 /* VFIO info does not advertise capabilities */
584 offset = VFIO_CAP_OFFSET(info);
585 while (offset != 0) {
586 h = RTE_PTR_ADD(info, offset);
595 pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
597 struct vfio_region_info *info;
600 ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
604 ret = pci_vfio_info_cap(info, RTE_VFIO_CAP_MSIX_MAPPABLE) != NULL;
614 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
616 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
617 char pci_addr[PATH_MAX] = {0};
619 struct rte_pci_addr *loc = &dev->addr;
621 struct mapped_pci_resource *vfio_res = NULL;
622 struct mapped_pci_res_list *vfio_res_list =
623 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
625 struct pci_map *maps;
627 dev->intr_handle.fd = -1;
628 dev->vfio_req_intr_handle.fd = -1;
630 /* store PCI address string */
631 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
632 loc->domain, loc->bus, loc->devid, loc->function);
634 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
635 &vfio_dev_fd, &device_info);
639 /* allocate vfio_res and get region info */
640 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
641 if (vfio_res == NULL) {
643 "%s(): cannot store uio mmap details\n", __func__);
644 goto err_vfio_dev_fd;
646 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
648 /* get number of registers (up to BAR5) */
649 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
650 VFIO_PCI_BAR5_REGION_INDEX + 1);
653 maps = vfio_res->maps;
655 vfio_res->msix_table.bar_index = -1;
656 /* get MSI-X BAR, if any (we have to know where it is because we can't
657 * easily mmap it when using VFIO)
659 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
661 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n",
665 /* if we found our MSI-X BAR region, check if we can mmap it */
666 if (vfio_res->msix_table.bar_index != -1) {
667 int ret = pci_vfio_msix_is_mappable(vfio_dev_fd,
668 vfio_res->msix_table.bar_index);
670 RTE_LOG(ERR, EAL, "Couldn't check if MSI-X BAR is mappable\n");
672 } else if (ret != 0) {
673 /* we can map it, so we don't care where it is */
674 RTE_LOG(DEBUG, EAL, "VFIO reports MSI-X BAR as mappable\n");
675 vfio_res->msix_table.bar_index = -1;
679 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
680 struct vfio_region_info *reg = NULL;
683 ret = pci_vfio_get_region_info(vfio_dev_fd, ®, i);
685 RTE_LOG(ERR, EAL, " %s cannot get device region info "
686 "error %i (%s)\n", pci_addr, errno,
691 /* chk for io port region */
692 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
697 RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
703 /* skip non-mmapable BARs */
704 if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
709 /* try mapping somewhere close to the end of hugepages */
710 if (pci_map_addr == NULL)
711 pci_map_addr = pci_find_max_end_va();
713 bar_addr = pci_map_addr;
714 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
716 maps[i].addr = bar_addr;
717 maps[i].offset = reg->offset;
718 maps[i].size = reg->size;
719 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
721 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
723 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n",
724 pci_addr, i, strerror(errno));
729 dev->mem_resource[i].addr = maps[i].addr;
734 if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
735 RTE_LOG(ERR, EAL, " %s setup device failed\n", pci_addr);
739 if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
740 RTE_LOG(ERR, EAL, "Error setting up notifier!\n");
744 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
755 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
757 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
758 char pci_addr[PATH_MAX] = {0};
760 struct rte_pci_addr *loc = &dev->addr;
762 struct mapped_pci_resource *vfio_res = NULL;
763 struct mapped_pci_res_list *vfio_res_list =
764 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
766 struct pci_map *maps;
768 dev->intr_handle.fd = -1;
769 dev->vfio_req_intr_handle.fd = -1;
771 /* store PCI address string */
772 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
773 loc->domain, loc->bus, loc->devid, loc->function);
775 ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
776 &vfio_dev_fd, &device_info);
780 /* if we're in a secondary process, just find our tailq entry */
781 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
782 if (rte_pci_addr_cmp(&vfio_res->pci_addr,
787 /* if we haven't found our tailq entry, something's wrong */
788 if (vfio_res == NULL) {
789 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
791 goto err_vfio_dev_fd;
795 maps = vfio_res->maps;
797 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
798 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
800 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n",
801 pci_addr, i, strerror(errno));
802 goto err_vfio_dev_fd;
805 dev->mem_resource[i].addr = maps[i].addr;
808 /* we need save vfio_dev_fd, so it can be used during release */
809 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
810 dev->vfio_req_intr_handle.vfio_dev_fd = vfio_dev_fd;
819 * map the PCI resources of a PCI device in virtual memory (VFIO version).
820 * primary and secondary processes follow almost exactly the same path
823 pci_vfio_map_resource(struct rte_pci_device *dev)
825 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
826 return pci_vfio_map_resource_primary(dev);
828 return pci_vfio_map_resource_secondary(dev);
831 static struct mapped_pci_resource *
832 find_and_unmap_vfio_resource(struct mapped_pci_res_list *vfio_res_list,
833 struct rte_pci_device *dev,
834 const char *pci_addr)
836 struct mapped_pci_resource *vfio_res = NULL;
837 struct pci_map *maps;
841 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
842 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
847 if (vfio_res == NULL)
850 RTE_LOG(INFO, EAL, "Releasing pci mapped resource for %s\n",
853 maps = vfio_res->maps;
854 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
857 * We do not need to be aware of MSI-X table BAR mappings as
858 * when mapping. Just using current maps array is enough
861 RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
862 pci_addr, maps[i].addr);
863 pci_unmap_resource(maps[i].addr, maps[i].size);
871 pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
873 char pci_addr[PATH_MAX] = {0};
874 struct rte_pci_addr *loc = &dev->addr;
875 struct mapped_pci_resource *vfio_res = NULL;
876 struct mapped_pci_res_list *vfio_res_list;
879 /* store PCI address string */
880 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
881 loc->domain, loc->bus, loc->devid, loc->function);
883 ret = pci_vfio_disable_notifier(dev);
885 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
889 if (close(dev->intr_handle.fd) < 0) {
890 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
895 if (pci_vfio_set_bus_master(dev->intr_handle.vfio_dev_fd, false)) {
896 RTE_LOG(ERR, EAL, " %s cannot unset bus mastering for PCI device!\n",
901 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
902 dev->intr_handle.vfio_dev_fd);
905 "%s(): cannot release device\n", __func__);
910 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
911 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
913 /* if we haven't found our tailq entry, something's wrong */
914 if (vfio_res == NULL) {
915 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
920 TAILQ_REMOVE(vfio_res_list, vfio_res, next);
926 pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
928 char pci_addr[PATH_MAX] = {0};
929 struct rte_pci_addr *loc = &dev->addr;
930 struct mapped_pci_resource *vfio_res = NULL;
931 struct mapped_pci_res_list *vfio_res_list;
934 /* store PCI address string */
935 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
936 loc->domain, loc->bus, loc->devid, loc->function);
938 ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
939 dev->intr_handle.vfio_dev_fd);
942 "%s(): cannot release device\n", __func__);
947 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
948 vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
950 /* if we haven't found our tailq entry, something's wrong */
951 if (vfio_res == NULL) {
952 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
961 pci_vfio_unmap_resource(struct rte_pci_device *dev)
963 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
964 return pci_vfio_unmap_resource_primary(dev);
966 return pci_vfio_unmap_resource_secondary(dev);
970 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
971 struct rte_pci_ioport *p)
973 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
974 bar > VFIO_PCI_BAR5_REGION_INDEX) {
975 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
980 p->base = VFIO_GET_REGION_ADDR(bar);
985 pci_vfio_ioport_read(struct rte_pci_ioport *p,
986 void *data, size_t len, off_t offset)
988 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
990 if (pread64(intr_handle->vfio_dev_fd, data,
991 len, p->base + offset) <= 0)
993 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
994 VFIO_GET_REGION_IDX(p->base), (int)offset);
998 pci_vfio_ioport_write(struct rte_pci_ioport *p,
999 const void *data, size_t len, off_t offset)
1001 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1003 if (pwrite64(intr_handle->vfio_dev_fd, data,
1004 len, p->base + offset) <= 0)
1006 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1007 VFIO_GET_REGION_IDX(p->base), (int)offset);
1011 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1018 pci_vfio_is_enabled(void)
1020 return rte_vfio_is_enabled("vfio_pci");