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