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