New upstream version 17.11.4
[deb_dpdk.git] / drivers / bus / pci / linux / pci_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <fcntl.h>
36 #include <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41 #include <stdbool.h>
42
43 #include <rte_log.h>
44 #include <rte_pci.h>
45 #include <rte_bus_pci.h>
46 #include <rte_eal_memconfig.h>
47 #include <rte_malloc.h>
48 #include <rte_vfio.h>
49
50 #include "eal_filesystem.h"
51
52 #include "pci_init.h"
53 #include "private.h"
54
55 /**
56  * @file
57  * PCI probing under linux (VFIO version)
58  *
59  * This code tries to determine if the PCI device is bound to VFIO driver,
60  * and initialize it (map BARs, set up interrupts) if that's the case.
61  *
62  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
63  */
64
65 #ifdef VFIO_PRESENT
66
67 #define PAGE_SIZE   (sysconf(_SC_PAGESIZE))
68 #define PAGE_MASK   (~(PAGE_SIZE - 1))
69
70 static struct rte_tailq_elem rte_vfio_tailq = {
71         .name = "VFIO_RESOURCE_LIST",
72 };
73 EAL_REGISTER_TAILQ(rte_vfio_tailq)
74
75 int
76 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
77                     void *buf, size_t len, off_t offs)
78 {
79         return pread64(intr_handle->vfio_dev_fd, buf, len,
80                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
81 }
82
83 int
84 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
85                     const void *buf, size_t len, off_t offs)
86 {
87         return pwrite64(intr_handle->vfio_dev_fd, buf, len,
88                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
89 }
90
91 /* get PCI BAR number where MSI-X interrupts are */
92 static int
93 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
94 {
95         int ret;
96         uint32_t reg;
97         uint16_t flags;
98         uint8_t cap_id, cap_offset;
99
100         /* read PCI capability pointer from config space */
101         ret = pread64(fd, &reg, sizeof(reg),
102                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
103                         PCI_CAPABILITY_LIST);
104         if (ret != sizeof(reg)) {
105                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
106                                 "config space!\n");
107                 return -1;
108         }
109
110         /* we need first byte */
111         cap_offset = reg & 0xFF;
112
113         while (cap_offset) {
114
115                 /* read PCI capability ID */
116                 ret = pread64(fd, &reg, sizeof(reg),
117                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
118                                 cap_offset);
119                 if (ret != sizeof(reg)) {
120                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
121                                         "config space!\n");
122                         return -1;
123                 }
124
125                 /* we need first byte */
126                 cap_id = reg & 0xFF;
127
128                 /* if we haven't reached MSI-X, check next capability */
129                 if (cap_id != PCI_CAP_ID_MSIX) {
130                         ret = pread64(fd, &reg, sizeof(reg),
131                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
132                                         cap_offset);
133                         if (ret != sizeof(reg)) {
134                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
135                                                 "config space!\n");
136                                 return -1;
137                         }
138
139                         /* we need second byte */
140                         cap_offset = (reg & 0xFF00) >> 8;
141
142                         continue;
143                 }
144                 /* else, read table offset */
145                 else {
146                         /* table offset resides in the next 4 bytes */
147                         ret = pread64(fd, &reg, sizeof(reg),
148                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
149                                         cap_offset + 4);
150                         if (ret != sizeof(reg)) {
151                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
152                                                 "space!\n");
153                                 return -1;
154                         }
155
156                         ret = pread64(fd, &flags, sizeof(flags),
157                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
158                                         cap_offset + 2);
159                         if (ret != sizeof(flags)) {
160                                 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
161                                                 "space!\n");
162                                 return -1;
163                         }
164
165                         msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
166                         msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
167                         msix_table->size =
168                                 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
169
170                         return 0;
171                 }
172         }
173         return 0;
174 }
175
176 /* set PCI bus mastering */
177 static int
178 pci_vfio_set_bus_master(int dev_fd, bool op)
179 {
180         uint16_t reg;
181         int ret;
182
183         ret = pread64(dev_fd, &reg, sizeof(reg),
184                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
185                         PCI_COMMAND);
186         if (ret != sizeof(reg)) {
187                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
188                 return -1;
189         }
190
191         if (op)
192                 /* set the master bit */
193                 reg |= PCI_COMMAND_MASTER;
194         else
195                 reg &= ~(PCI_COMMAND_MASTER);
196
197         ret = pwrite64(dev_fd, &reg, sizeof(reg),
198                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
199                         PCI_COMMAND);
200
201         if (ret != sizeof(reg)) {
202                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 /* set up interrupt support (but not enable interrupts) */
210 static int
211 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
212 {
213         int i, ret, intr_idx;
214         enum rte_intr_mode intr_mode;
215
216         /* default to invalid index */
217         intr_idx = VFIO_PCI_NUM_IRQS;
218
219         /* Get default / configured intr_mode */
220         intr_mode = rte_eal_vfio_intr_mode();
221
222         /* get interrupt type from internal config (MSI-X by default, can be
223          * overridden from the command line
224          */
225         switch (intr_mode) {
226         case RTE_INTR_MODE_MSIX:
227                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
228                 break;
229         case RTE_INTR_MODE_MSI:
230                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
231                 break;
232         case RTE_INTR_MODE_LEGACY:
233                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
234                 break;
235         /* don't do anything if we want to automatically determine interrupt type */
236         case RTE_INTR_MODE_NONE:
237                 break;
238         default:
239                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
240                 return -1;
241         }
242
243         /* start from MSI-X interrupt type */
244         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
245                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
246                 int fd = -1;
247
248                 /* skip interrupt modes we don't want */
249                 if (intr_mode != RTE_INTR_MODE_NONE &&
250                                 i != intr_idx)
251                         continue;
252
253                 irq.index = i;
254
255                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
256                 if (ret < 0) {
257                         RTE_LOG(ERR, EAL, "  cannot get IRQ info, "
258                                         "error %i (%s)\n", errno, strerror(errno));
259                         return -1;
260                 }
261
262                 /* if this vector cannot be used with eventfd, fail if we explicitly
263                  * specified interrupt type, otherwise continue */
264                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
265                         if (intr_mode != RTE_INTR_MODE_NONE) {
266                                 RTE_LOG(ERR, EAL,
267                                                 "  interrupt vector does not support eventfd!\n");
268                                 return -1;
269                         } else
270                                 continue;
271                 }
272
273                 /* set up an eventfd for interrupts */
274                 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
275                 if (fd < 0) {
276                         RTE_LOG(ERR, EAL, "  cannot set up eventfd, "
277                                         "error %i (%s)\n", errno, strerror(errno));
278                         return -1;
279                 }
280
281                 dev->intr_handle.fd = fd;
282                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
283
284                 switch (i) {
285                 case VFIO_PCI_MSIX_IRQ_INDEX:
286                         intr_mode = RTE_INTR_MODE_MSIX;
287                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
288                         break;
289                 case VFIO_PCI_MSI_IRQ_INDEX:
290                         intr_mode = RTE_INTR_MODE_MSI;
291                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
292                         break;
293                 case VFIO_PCI_INTX_IRQ_INDEX:
294                         intr_mode = RTE_INTR_MODE_LEGACY;
295                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
296                         break;
297                 default:
298                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
299                         return -1;
300                 }
301
302                 return 0;
303         }
304
305         /* if we're here, we haven't found a suitable interrupt vector */
306         return -1;
307 }
308
309 static int
310 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
311 {
312         uint32_t ioport_bar;
313         int ret;
314
315         ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
316                           VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
317                           + PCI_BASE_ADDRESS_0 + bar_index*4);
318         if (ret != sizeof(ioport_bar)) {
319                 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
320                         PCI_BASE_ADDRESS_0 + bar_index*4);
321                 return -1;
322         }
323
324         return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
325 }
326
327 static int
328 pci_rte_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
329 {
330         if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
331                 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
332                 return -1;
333         }
334
335         /* set bus mastering for the device */
336         if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
337                 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
338                 return -1;
339         }
340
341         /*
342          * Reset the device. If the device is not capable of resetting,
343          * then it updates errno as EINVAL.
344          */
345         if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
346                 RTE_LOG(ERR, EAL, "Unable to reset device! Error: %d (%s)\n",
347                                 errno, strerror(errno));
348                 return -1;
349         }
350
351         return 0;
352 }
353
354 static int
355 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
356                 int bar_index, int additional_flags)
357 {
358         struct memreg {
359                 unsigned long offset, size;
360         } memreg[2] = {};
361         void *bar_addr;
362         struct pci_msix_table *msix_table = &vfio_res->msix_table;
363         struct pci_map *bar = &vfio_res->maps[bar_index];
364
365         if (bar->size == 0)
366                 /* Skip this BAR */
367                 return 0;
368
369         if (msix_table->bar_index == bar_index) {
370                 /*
371                  * VFIO will not let us map the MSI-X table,
372                  * but we can map around it.
373                  */
374                 uint32_t table_start = msix_table->offset;
375                 uint32_t table_end = table_start + msix_table->size;
376                 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
377                 table_start &= PAGE_MASK;
378
379                 if (table_start == 0 && table_end >= bar->size) {
380                         /* Cannot map this BAR */
381                         RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
382                         bar->size = 0;
383                         bar->addr = 0;
384                         return 0;
385                 }
386
387                 memreg[0].offset = bar->offset;
388                 memreg[0].size = table_start;
389                 memreg[1].offset = bar->offset + table_end;
390                 memreg[1].size = bar->size - table_end;
391
392                 RTE_LOG(DEBUG, EAL,
393                         "Trying to map BAR%d that contains the MSI-X "
394                         "table. Trying offsets: "
395                         "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", bar_index,
396                         memreg[0].offset, memreg[0].size,
397                         memreg[1].offset, memreg[1].size);
398         } else {
399                 memreg[0].offset = bar->offset;
400                 memreg[0].size = bar->size;
401         }
402
403         /* reserve the address using an inaccessible mapping */
404         bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
405                         MAP_ANONYMOUS | additional_flags, -1, 0);
406         if (bar_addr != MAP_FAILED) {
407                 void *map_addr = NULL;
408                 if (memreg[0].size) {
409                         /* actual map of first part */
410                         map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
411                                                         memreg[0].offset,
412                                                         memreg[0].size,
413                                                         MAP_FIXED);
414                 }
415
416                 /* if there's a second part, try to map it */
417                 if (map_addr != MAP_FAILED
418                         && memreg[1].offset && memreg[1].size) {
419                         void *second_addr = RTE_PTR_ADD(bar_addr,
420                                                         memreg[1].offset -
421                                                         (uintptr_t)bar->offset);
422                         map_addr = pci_map_resource(second_addr,
423                                                         vfio_dev_fd,
424                                                         memreg[1].offset,
425                                                         memreg[1].size,
426                                                         MAP_FIXED);
427                 }
428
429                 if (map_addr == MAP_FAILED || !map_addr) {
430                         munmap(bar_addr, bar->size);
431                         bar_addr = MAP_FAILED;
432                         RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
433                                         bar_index);
434                         return -1;
435                 }
436         } else {
437                 RTE_LOG(ERR, EAL,
438                                 "Failed to create inaccessible mapping for BAR%d\n",
439                                 bar_index);
440                 return -1;
441         }
442
443         bar->addr = bar_addr;
444         return 0;
445 }
446
447 static int
448 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
449 {
450         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
451         char pci_addr[PATH_MAX] = {0};
452         int vfio_dev_fd;
453         struct rte_pci_addr *loc = &dev->addr;
454         int i, ret;
455         struct mapped_pci_resource *vfio_res = NULL;
456         struct mapped_pci_res_list *vfio_res_list =
457                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
458
459         struct pci_map *maps;
460
461         dev->intr_handle.fd = -1;
462
463         /* store PCI address string */
464         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
465                         loc->domain, loc->bus, loc->devid, loc->function);
466
467         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
468                                         &vfio_dev_fd, &device_info);
469         if (ret)
470                 return ret;
471
472         /* allocate vfio_res and get region info */
473         vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
474         if (vfio_res == NULL) {
475                 RTE_LOG(ERR, EAL,
476                         "%s(): cannot store uio mmap details\n", __func__);
477                 goto err_vfio_dev_fd;
478         }
479         memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
480
481         /* get number of registers (up to BAR5) */
482         vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
483                         VFIO_PCI_BAR5_REGION_INDEX + 1);
484
485         /* map BARs */
486         maps = vfio_res->maps;
487
488         vfio_res->msix_table.bar_index = -1;
489         /* get MSI-X BAR, if any (we have to know where it is because we can't
490          * easily mmap it when using VFIO)
491          */
492         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
493         if (ret < 0) {
494                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n",
495                                 pci_addr);
496                 goto err_vfio_dev_fd;
497         }
498
499         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
500                 struct vfio_region_info reg = { .argsz = sizeof(reg) };
501                 void *bar_addr;
502
503                 reg.index = i;
504
505                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
506                 if (ret) {
507                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
508                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
509                         goto err_vfio_res;
510                 }
511
512                 /* chk for io port region */
513                 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
514                 if (ret < 0)
515                         goto err_vfio_res;
516                 else if (ret) {
517                         RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
518                                         i);
519                         continue;
520                 }
521
522                 /* skip non-mmapable BARs */
523                 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
524                         continue;
525
526                 /* try mapping somewhere close to the end of hugepages */
527                 if (pci_map_addr == NULL)
528                         pci_map_addr = pci_find_max_end_va();
529
530                 bar_addr = pci_map_addr;
531                 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
532
533                 maps[i].addr = bar_addr;
534                 maps[i].offset = reg.offset;
535                 maps[i].size = reg.size;
536                 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
537
538                 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
539                 if (ret < 0) {
540                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
541                                         pci_addr, i, strerror(errno));
542                         goto err_vfio_res;
543                 }
544
545                 dev->mem_resource[i].addr = maps[i].addr;
546         }
547
548         if (pci_rte_vfio_setup_device(dev, vfio_dev_fd) < 0) {
549                 RTE_LOG(ERR, EAL, "  %s setup device failed\n", pci_addr);
550                 goto err_vfio_res;
551         }
552
553         TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
554
555         return 0;
556 err_vfio_res:
557         rte_free(vfio_res);
558 err_vfio_dev_fd:
559         close(vfio_dev_fd);
560         return -1;
561 }
562
563 static int
564 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
565 {
566         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
567         char pci_addr[PATH_MAX] = {0};
568         int vfio_dev_fd;
569         struct rte_pci_addr *loc = &dev->addr;
570         int i, ret;
571         struct mapped_pci_resource *vfio_res = NULL;
572         struct mapped_pci_res_list *vfio_res_list =
573                 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
574
575         struct pci_map *maps;
576
577         dev->intr_handle.fd = -1;
578
579         /* store PCI address string */
580         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
581                         loc->domain, loc->bus, loc->devid, loc->function);
582
583         ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
584                                         &vfio_dev_fd, &device_info);
585         if (ret)
586                 return ret;
587
588         /* if we're in a secondary process, just find our tailq entry */
589         TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
590                 if (rte_pci_addr_cmp(&vfio_res->pci_addr,
591                                                  &dev->addr))
592                         continue;
593                 break;
594         }
595         /* if we haven't found our tailq entry, something's wrong */
596         if (vfio_res == NULL) {
597                 RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
598                                 pci_addr);
599                 goto err_vfio_dev_fd;
600         }
601
602         /* map BARs */
603         maps = vfio_res->maps;
604
605         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
606                 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
607                 if (ret < 0) {
608                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n",
609                                         pci_addr, i, strerror(errno));
610                         goto err_vfio_dev_fd;
611                 }
612
613                 dev->mem_resource[i].addr = maps[i].addr;
614         }
615
616         return 0;
617 err_vfio_dev_fd:
618         close(vfio_dev_fd);
619         return -1;
620 }
621
622 /*
623  * map the PCI resources of a PCI device in virtual memory (VFIO version).
624  * primary and secondary processes follow almost exactly the same path
625  */
626 int
627 pci_vfio_map_resource(struct rte_pci_device *dev)
628 {
629         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
630                 return pci_vfio_map_resource_primary(dev);
631         else
632                 return pci_vfio_map_resource_secondary(dev);
633 }
634
635 int
636 pci_vfio_unmap_resource(struct rte_pci_device *dev)
637 {
638         char pci_addr[PATH_MAX] = {0};
639         struct rte_pci_addr *loc = &dev->addr;
640         int i, ret;
641         struct mapped_pci_resource *vfio_res = NULL;
642         struct mapped_pci_res_list *vfio_res_list;
643
644         struct pci_map *maps;
645
646         /* store PCI address string */
647         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
648                         loc->domain, loc->bus, loc->devid, loc->function);
649
650
651         if (close(dev->intr_handle.fd) < 0) {
652                 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
653                         pci_addr);
654                 return -1;
655         }
656
657         if (pci_vfio_set_bus_master(dev->intr_handle.vfio_dev_fd, false)) {
658                 RTE_LOG(ERR, EAL, "  %s cannot unset bus mastering for PCI device!\n",
659                                 pci_addr);
660                 return -1;
661         }
662
663         ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
664                                   dev->intr_handle.vfio_dev_fd);
665         if (ret < 0) {
666                 RTE_LOG(ERR, EAL,
667                         "%s(): cannot release device\n", __func__);
668                 return ret;
669         }
670
671         vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
672         /* Get vfio_res */
673         TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
674                 if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
675                         continue;
676                 break;
677         }
678         /* if we haven't found our tailq entry, something's wrong */
679         if (vfio_res == NULL) {
680                 RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
681                                 pci_addr);
682                 return -1;
683         }
684
685         /* unmap BARs */
686         maps = vfio_res->maps;
687
688         RTE_LOG(INFO, EAL, "Releasing pci mapped resource for %s\n",
689                 pci_addr);
690         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
691
692                 /*
693                  * We do not need to be aware of MSI-X table BAR mappings as
694                  * when mapping. Just using current maps array is enough
695                  */
696                 if (maps[i].addr) {
697                         RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
698                                 pci_addr, maps[i].addr);
699                         pci_unmap_resource(maps[i].addr, maps[i].size);
700                 }
701         }
702
703         TAILQ_REMOVE(vfio_res_list, vfio_res, next);
704
705         return 0;
706 }
707
708 int
709 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
710                     struct rte_pci_ioport *p)
711 {
712         if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
713             bar > VFIO_PCI_BAR5_REGION_INDEX) {
714                 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
715                 return -1;
716         }
717
718         p->dev = dev;
719         p->base = VFIO_GET_REGION_ADDR(bar);
720         return 0;
721 }
722
723 void
724 pci_vfio_ioport_read(struct rte_pci_ioport *p,
725                      void *data, size_t len, off_t offset)
726 {
727         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
728
729         if (pread64(intr_handle->vfio_dev_fd, data,
730                     len, p->base + offset) <= 0)
731                 RTE_LOG(ERR, EAL,
732                         "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
733                         VFIO_GET_REGION_IDX(p->base), (int)offset);
734 }
735
736 void
737 pci_vfio_ioport_write(struct rte_pci_ioport *p,
738                       const void *data, size_t len, off_t offset)
739 {
740         const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
741
742         if (pwrite64(intr_handle->vfio_dev_fd, data,
743                      len, p->base + offset) <= 0)
744                 RTE_LOG(ERR, EAL,
745                         "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
746                         VFIO_GET_REGION_IDX(p->base), (int)offset);
747 }
748
749 int
750 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
751 {
752         RTE_SET_USED(p);
753         return -1;
754 }
755
756 int
757 pci_vfio_is_enabled(void)
758 {
759         return rte_vfio_is_enabled("vfio_pci");
760 }
761 #endif