72d77bfc68b91f8517314291452d843de55fb1d8
[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 static void
283 pci_vfio_req_handler(void *param)
284 {
285         struct rte_bus *bus;
286         int ret;
287         struct rte_device *device = (struct rte_device *)param;
288
289         bus = rte_bus_find_by_device(device);
290         if (bus == NULL) {
291                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
292                         device->name);
293                 return;
294         }
295
296         /*
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.
300          */
301         ret = bus->hot_unplug_handler(device);
302         if (ret)
303                 RTE_LOG(ERR, EAL,
304                         "Can not handle hot-unplug for device (%s)\n",
305                         device->name);
306 }
307
308 /* enable notifier (only enable req now) */
309 static int
310 pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
311 {
312         int ret;
313         int fd = -1;
314
315         /* set up an eventfd for req notifier */
316         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
317         if (fd < 0) {
318                 RTE_LOG(ERR, EAL, "Cannot set up eventfd, error %i (%s)\n",
319                         errno, strerror(errno));
320                 return -1;
321         }
322
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;
326
327         ret = rte_intr_callback_register(&dev->vfio_req_intr_handle,
328                                          pci_vfio_req_handler,
329                                          (void *)&dev->device);
330         if (ret) {
331                 RTE_LOG(ERR, EAL, "Fail to register req notifier handler.\n");
332                 goto error;
333         }
334
335         ret = rte_intr_enable(&dev->vfio_req_intr_handle);
336         if (ret) {
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);
341                 if (ret)
342                         RTE_LOG(ERR, EAL,
343                                 "Fail to unregister req notifier handler.\n");
344                 goto error;
345         }
346
347         return 0;
348 error:
349         close(fd);
350
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;
354
355         return -1;
356 }
357
358 /* disable notifier (only disable req now) */
359 static int
360 pci_vfio_disable_notifier(struct rte_pci_device *dev)
361 {
362         int ret;
363
364         ret = rte_intr_disable(&dev->vfio_req_intr_handle);
365         if (ret) {
366                 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
367                 return -1;
368         }
369
370         ret = rte_intr_callback_unregister(&dev->vfio_req_intr_handle,
371                                            pci_vfio_req_handler,
372                                            (void *)&dev->device);
373         if (ret) {
374                 RTE_LOG(ERR, EAL,
375                          "fail to unregister req notifier handler.\n");
376                 return -1;
377         }
378
379         close(dev->vfio_req_intr_handle.fd);
380
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;
384
385         return 0;
386 }
387
388 static int
389 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
390 {
391         uint32_t ioport_bar;
392         int ret;
393
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);
400                 return -1;
401         }
402
403         return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
404 }
405
406 static int
407 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
408 {
409         if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
410                 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
411                 return -1;
412         }
413
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");
417                 return -1;
418         }
419
420         /*
421          * Reset the device. If the device is not capable of resetting,
422          * then it updates errno as EINVAL.
423          */
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));
427                 return -1;
428         }
429
430         return 0;
431 }
432
433 static int
434 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
435                 int bar_index, int additional_flags)
436 {
437         struct memreg {
438                 unsigned long offset, size;
439         } memreg[2] = {};
440         void *bar_addr;
441         struct pci_msix_table *msix_table = &vfio_res->msix_table;
442         struct pci_map *bar = &vfio_res->maps[bar_index];
443
444         if (bar->size == 0)
445                 /* Skip this BAR */
446                 return 0;
447
448         if (msix_table->bar_index == bar_index) {
449                 /*
450                  * VFIO will not let us map the MSI-X table,
451                  * but we can map around it.
452                  */
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;
457
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);
461                         bar->size = 0;
462                         bar->addr = 0;
463                         return 0;
464                 }
465
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;
470
471                 RTE_LOG(DEBUG, EAL,
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);
477         } else {
478                 memreg[0].offset = bar->offset;
479                 memreg[0].size = bar->size;
480         }
481
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,
490                                                         memreg[0].offset,
491                                                         memreg[0].size,
492                                                         MAP_FIXED);
493                 }
494
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,
499                                                         memreg[1].offset -
500                                                         (uintptr_t)bar->offset);
501                         map_addr = pci_map_resource(second_addr,
502                                                         vfio_dev_fd,
503                                                         memreg[1].offset,
504                                                         memreg[1].size,
505                                                         MAP_FIXED);
506                 }
507
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",
512                                         bar_index);
513                         return -1;
514                 }
515         } else {
516                 RTE_LOG(ERR, EAL,
517                                 "Failed to create inaccessible mapping for BAR%d\n",
518                                 bar_index);
519                 return -1;
520         }
521
522         bar->addr = bar_addr;
523         return 0;
524 }
525
526 /*
527  * region info may contain capability headers, so we need to keep reallocating
528  * the memory until we match allocated memory size with argsz.
529  */
530 static int
531 pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
532                 int region)
533 {
534         struct vfio_region_info *ri;
535         size_t argsz = sizeof(*ri);
536         int ret;
537
538         ri = malloc(sizeof(*ri));
539         if (ri == NULL) {
540                 RTE_LOG(ERR, EAL, "Cannot allocate memory for region info\n");
541                 return -1;
542         }
543 again:
544         memset(ri, 0, argsz);
545         ri->argsz = argsz;
546         ri->index = region;
547
548         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
549         if (ret < 0) {
550                 free(ri);
551                 return ret;
552         }
553         if (ri->argsz != argsz) {
554                 struct vfio_region_info *tmp;
555
556                 argsz = ri->argsz;
557                 tmp = realloc(ri, argsz);
558
559                 if (tmp == NULL) {
560                         /* realloc failed but the ri is still there */
561                         free(ri);
562                         RTE_LOG(ERR, EAL, "Cannot reallocate memory for region info\n");
563                         return -1;
564                 }
565                 ri = tmp;
566                 goto again;
567         }
568         *info = ri;
569
570         return 0;
571 }
572
573 static struct vfio_info_cap_header *
574 pci_vfio_info_cap(struct vfio_region_info *info, int cap)
575 {
576         struct vfio_info_cap_header *h;
577         size_t offset;
578
579         if ((info->flags & RTE_VFIO_INFO_FLAG_CAPS) == 0) {
580                 /* VFIO info does not advertise capabilities */
581                 return NULL;
582         }
583
584         offset = VFIO_CAP_OFFSET(info);
585         while (offset != 0) {
586                 h = RTE_PTR_ADD(info, offset);
587                 if (h->id == cap)
588                         return h;
589                 offset = h->next;
590         }
591         return NULL;
592 }
593
594 static int
595 pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
596 {
597         struct vfio_region_info *info;
598         int ret;
599
600         ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
601         if (ret < 0)
602                 return -1;
603
604         ret = pci_vfio_info_cap(info, RTE_VFIO_CAP_MSIX_MAPPABLE) != NULL;
605
606         /* cleanup */
607         free(info);
608
609         return ret;
610 }
611
612
613 static int
614 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
615 {
616         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
617         char pci_addr[PATH_MAX] = {0};
618         int vfio_dev_fd;
619         struct rte_pci_addr *loc = &dev->addr;
620         int i, ret;
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);
624
625         struct pci_map *maps;
626
627         dev->intr_handle.fd = -1;
628         dev->vfio_req_intr_handle.fd = -1;
629
630         /* store PCI address string */
631         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
632                         loc->domain, loc->bus, loc->devid, loc->function);
633
634         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
635                                         &vfio_dev_fd, &device_info);
636         if (ret)
637                 return ret;
638
639         /* allocate vfio_res and get region info */
640         vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
641         if (vfio_res == NULL) {
642                 RTE_LOG(ERR, EAL,
643                         "%s(): cannot store uio mmap details\n", __func__);
644                 goto err_vfio_dev_fd;
645         }
646         memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
647
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);
651
652         /* map BARs */
653         maps = vfio_res->maps;
654
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)
658          */
659         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
660         if (ret < 0) {
661                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n",
662                                 pci_addr);
663                 goto err_vfio_res;
664         }
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);
669                 if (ret < 0) {
670                         RTE_LOG(ERR, EAL, "Couldn't check if MSI-X BAR is mappable\n");
671                         goto err_vfio_res;
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;
676                 }
677         }
678
679         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
680                 struct vfio_region_info *reg = NULL;
681                 void *bar_addr;
682
683                 ret = pci_vfio_get_region_info(vfio_dev_fd, &reg, i);
684                 if (ret < 0) {
685                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
686                                 "error %i (%s)\n", pci_addr, errno,
687                                 strerror(errno));
688                         goto err_vfio_res;
689                 }
690
691                 /* chk for io port region */
692                 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
693                 if (ret < 0) {
694                         free(reg);
695                         goto err_vfio_res;
696                 } else if (ret) {
697                         RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
698                                         i);
699                         free(reg);
700                         continue;
701                 }
702
703                 /* skip non-mmapable BARs */
704                 if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
705                         free(reg);
706                         continue;
707                 }
708
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();
712
713                 bar_addr = pci_map_addr;
714                 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
715
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 */
720
721                 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
722                 if (ret < 0) {
723                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
724                                         pci_addr, i, strerror(errno));
725                         free(reg);
726                         goto err_vfio_res;
727                 }
728
729                 dev->mem_resource[i].addr = maps[i].addr;
730
731                 free(reg);
732         }
733
734         if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
735                 RTE_LOG(ERR, EAL, "  %s setup device failed\n", pci_addr);
736                 goto err_vfio_res;
737         }
738
739         if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
740                 RTE_LOG(ERR, EAL, "Error setting up notifier!\n");
741                 goto err_vfio_res;
742         }
743
744         TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
745
746         return 0;
747 err_vfio_res:
748         rte_free(vfio_res);
749 err_vfio_dev_fd:
750         close(vfio_dev_fd);
751         return -1;
752 }
753
754 static int
755 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
756 {
757         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
758         char pci_addr[PATH_MAX] = {0};
759         int vfio_dev_fd;
760         struct rte_pci_addr *loc = &dev->addr;
761         int i, ret;
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);
765
766         struct pci_map *maps;
767
768         dev->intr_handle.fd = -1;
769         dev->vfio_req_intr_handle.fd = -1;
770
771         /* store PCI address string */
772         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
773                         loc->domain, loc->bus, loc->devid, loc->function);
774
775         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
776                                         &vfio_dev_fd, &device_info);
777         if (ret)
778                 return ret;
779
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,
783                                                  &dev->addr))
784                         continue;
785                 break;
786         }
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",
790                                 pci_addr);
791                 goto err_vfio_dev_fd;
792         }
793
794         /* map BARs */
795         maps = vfio_res->maps;
796
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);
799                 if (ret < 0) {
800                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
801                                         pci_addr, i, strerror(errno));
802                         goto err_vfio_dev_fd;
803                 }
804
805                 dev->mem_resource[i].addr = maps[i].addr;
806         }
807
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;
811
812         return 0;
813 err_vfio_dev_fd:
814         close(vfio_dev_fd);
815         return -1;
816 }
817
818 /*
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
821  */
822 int
823 pci_vfio_map_resource(struct rte_pci_device *dev)
824 {
825         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
826                 return pci_vfio_map_resource_primary(dev);
827         else
828                 return pci_vfio_map_resource_secondary(dev);
829 }
830
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)
835 {
836         struct mapped_pci_resource *vfio_res = NULL;
837         struct pci_map *maps;
838         int i;
839
840         /* Get vfio_res */
841         TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
842                 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
843                         continue;
844                 break;
845         }
846
847         if  (vfio_res == NULL)
848                 return vfio_res;
849
850         RTE_LOG(INFO, EAL, "Releasing pci mapped resource for %s\n",
851                 pci_addr);
852
853         maps = vfio_res->maps;
854         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
855
856                 /*
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
859                  */
860                 if (maps[i].addr) {
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);
864                 }
865         }
866
867         return vfio_res;
868 }
869
870 static int
871 pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
872 {
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;
877         int ret;
878
879         /* store PCI address string */
880         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
881                         loc->domain, loc->bus, loc->devid, loc->function);
882
883         ret = pci_vfio_disable_notifier(dev);
884         if (ret) {
885                 RTE_LOG(ERR, EAL, "fail to disable req notifier.\n");
886                 return -1;
887         }
888
889         if (close(dev->intr_handle.fd) < 0) {
890                 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
891                         pci_addr);
892                 return -1;
893         }
894
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",
897                                 pci_addr);
898                 return -1;
899         }
900
901         ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
902                                   dev->intr_handle.vfio_dev_fd);
903         if (ret < 0) {
904                 RTE_LOG(ERR, EAL,
905                         "%s(): cannot release device\n", __func__);
906                 return ret;
907         }
908
909         vfio_res_list =
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);
912
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",
916                                 pci_addr);
917                 return -1;
918         }
919
920         TAILQ_REMOVE(vfio_res_list, vfio_res, next);
921
922         return 0;
923 }
924
925 static int
926 pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
927 {
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;
932         int ret;
933
934         /* store PCI address string */
935         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
936                         loc->domain, loc->bus, loc->devid, loc->function);
937
938         ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
939                                   dev->intr_handle.vfio_dev_fd);
940         if (ret < 0) {
941                 RTE_LOG(ERR, EAL,
942                         "%s(): cannot release device\n", __func__);
943                 return ret;
944         }
945
946         vfio_res_list =
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);
949
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",
953                                 pci_addr);
954                 return -1;
955         }
956
957         return 0;
958 }
959
960 int
961 pci_vfio_unmap_resource(struct rte_pci_device *dev)
962 {
963         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
964                 return pci_vfio_unmap_resource_primary(dev);
965         else
966                 return pci_vfio_unmap_resource_secondary(dev);
967 }
968
969 int
970 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
971                     struct rte_pci_ioport *p)
972 {
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);
976                 return -1;
977         }
978
979         p->dev = dev;
980         p->base = VFIO_GET_REGION_ADDR(bar);
981         return 0;
982 }
983
984 void
985 pci_vfio_ioport_read(struct rte_pci_ioport *p,
986                      void *data, size_t len, off_t offset)
987 {
988         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
989
990         if (pread64(intr_handle->vfio_dev_fd, data,
991                     len, p->base + offset) <= 0)
992                 RTE_LOG(ERR, EAL,
993                         "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
994                         VFIO_GET_REGION_IDX(p->base), (int)offset);
995 }
996
997 void
998 pci_vfio_ioport_write(struct rte_pci_ioport *p,
999                       const void *data, size_t len, off_t offset)
1000 {
1001         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1002
1003         if (pwrite64(intr_handle->vfio_dev_fd, data,
1004                      len, p->base + offset) <= 0)
1005                 RTE_LOG(ERR, EAL,
1006                         "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1007                         VFIO_GET_REGION_IDX(p->base), (int)offset);
1008 }
1009
1010 int
1011 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1012 {
1013         RTE_SET_USED(p);
1014         return -1;
1015 }
1016
1017 int
1018 pci_vfio_is_enabled(void)
1019 {
1020         return rte_vfio_is_enabled("vfio_pci");
1021 }
1022 #endif