b12f10af5cff31b98b0eda347fea293a86cf434e
[dpdk.git] / drivers / bus / pci / linux / pci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <dirent.h>
7
8 #include <rte_log.h>
9 #include <rte_bus.h>
10 #include <rte_pci.h>
11 #include <rte_bus_pci.h>
12 #include <rte_eal_memconfig.h>
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
16 #include <rte_vfio.h>
17
18 #include "eal_filesystem.h"
19
20 #include "private.h"
21 #include "pci_init.h"
22
23 /**
24  * @file
25  * PCI probing under linux
26  *
27  * This code is used to simulate a PCI probe by parsing information in sysfs.
28  * When a registered device matches a driver, it is then initialized with
29  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
30  */
31
32 extern struct rte_pci_bus rte_pci_bus;
33
34 static int
35 pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
36                               size_t len)
37 {
38         int count;
39         char path[PATH_MAX];
40         char *name;
41
42         if (!filename || !dri_name)
43                 return -1;
44
45         count = readlink(filename, path, PATH_MAX);
46         if (count >= PATH_MAX)
47                 return -1;
48
49         /* For device does not have a driver */
50         if (count < 0)
51                 return 1;
52
53         path[count] = '\0';
54
55         name = strrchr(path, '/');
56         if (name) {
57                 strlcpy(dri_name, name + 1, len);
58                 return 0;
59         }
60
61         return -1;
62 }
63
64 /* Map pci device */
65 int
66 rte_pci_map_device(struct rte_pci_device *dev)
67 {
68         int ret = -1;
69
70         /* try mapping the NIC resources using VFIO if it exists */
71         switch (dev->kdrv) {
72         case RTE_KDRV_VFIO:
73 #ifdef VFIO_PRESENT
74                 if (pci_vfio_is_enabled())
75                         ret = pci_vfio_map_resource(dev);
76 #endif
77                 break;
78         case RTE_KDRV_IGB_UIO:
79         case RTE_KDRV_UIO_GENERIC:
80                 if (rte_eal_using_phys_addrs()) {
81                         /* map resources for devices that use uio */
82                         ret = pci_uio_map_resource(dev);
83                 }
84                 break;
85         default:
86                 RTE_LOG(DEBUG, EAL,
87                         "  Not managed by a supported kernel driver, skipped\n");
88                 ret = 1;
89                 break;
90         }
91
92         return ret;
93 }
94
95 /* Unmap pci device */
96 void
97 rte_pci_unmap_device(struct rte_pci_device *dev)
98 {
99         /* try unmapping the NIC resources using VFIO if it exists */
100         switch (dev->kdrv) {
101         case RTE_KDRV_VFIO:
102 #ifdef VFIO_PRESENT
103                 if (pci_vfio_is_enabled())
104                         pci_vfio_unmap_resource(dev);
105 #endif
106                 break;
107         case RTE_KDRV_IGB_UIO:
108         case RTE_KDRV_UIO_GENERIC:
109                 /* unmap resources for devices that use uio */
110                 pci_uio_unmap_resource(dev);
111                 break;
112         default:
113                 RTE_LOG(DEBUG, EAL,
114                         "  Not managed by a supported kernel driver, skipped\n");
115                 break;
116         }
117 }
118
119 static int
120 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
121 {
122         size_t sz = msl->len;
123         void *end_va = RTE_PTR_ADD(msl->base_va, sz);
124         void **max_va = arg;
125
126         if (*max_va < end_va)
127                 *max_va = end_va;
128         return 0;
129 }
130
131 void *
132 pci_find_max_end_va(void)
133 {
134         void *va = NULL;
135
136         rte_memseg_list_walk(find_max_end_va, &va);
137         return va;
138 }
139
140
141 /* parse one line of the "resource" sysfs file (note that the 'line'
142  * string is modified)
143  */
144 int
145 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
146         uint64_t *end_addr, uint64_t *flags)
147 {
148         union pci_resource_info {
149                 struct {
150                         char *phys_addr;
151                         char *end_addr;
152                         char *flags;
153                 };
154                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
155         } res_info;
156
157         if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
158                 RTE_LOG(ERR, EAL,
159                         "%s(): bad resource format\n", __func__);
160                 return -1;
161         }
162         errno = 0;
163         *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
164         *end_addr = strtoull(res_info.end_addr, NULL, 16);
165         *flags = strtoull(res_info.flags, NULL, 16);
166         if (errno != 0) {
167                 RTE_LOG(ERR, EAL,
168                         "%s(): bad resource format\n", __func__);
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 /* parse the "resource" sysfs file */
176 static int
177 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
178 {
179         FILE *f;
180         char buf[BUFSIZ];
181         int i;
182         uint64_t phys_addr, end_addr, flags;
183
184         f = fopen(filename, "r");
185         if (f == NULL) {
186                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
187                 return -1;
188         }
189
190         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
191
192                 if (fgets(buf, sizeof(buf), f) == NULL) {
193                         RTE_LOG(ERR, EAL,
194                                 "%s(): cannot read resource\n", __func__);
195                         goto error;
196                 }
197                 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
198                                 &end_addr, &flags) < 0)
199                         goto error;
200
201                 if (flags & IORESOURCE_MEM) {
202                         dev->mem_resource[i].phys_addr = phys_addr;
203                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
204                         /* not mapped for now */
205                         dev->mem_resource[i].addr = NULL;
206                 }
207         }
208         fclose(f);
209         return 0;
210
211 error:
212         fclose(f);
213         return -1;
214 }
215
216 /* Scan one pci sysfs entry, and fill the devices list from it. */
217 static int
218 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
219 {
220         char filename[PATH_MAX];
221         unsigned long tmp;
222         struct rte_pci_device *dev;
223         char driver[PATH_MAX];
224         int ret;
225
226         dev = malloc(sizeof(*dev));
227         if (dev == NULL)
228                 return -1;
229
230         memset(dev, 0, sizeof(*dev));
231         dev->device.bus = &rte_pci_bus.bus;
232         dev->addr = *addr;
233
234         /* get vendor id */
235         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
236         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
237                 free(dev);
238                 return -1;
239         }
240         dev->id.vendor_id = (uint16_t)tmp;
241
242         /* get device id */
243         snprintf(filename, sizeof(filename), "%s/device", dirname);
244         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
245                 free(dev);
246                 return -1;
247         }
248         dev->id.device_id = (uint16_t)tmp;
249
250         /* get subsystem_vendor id */
251         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
252                  dirname);
253         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
254                 free(dev);
255                 return -1;
256         }
257         dev->id.subsystem_vendor_id = (uint16_t)tmp;
258
259         /* get subsystem_device id */
260         snprintf(filename, sizeof(filename), "%s/subsystem_device",
261                  dirname);
262         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
263                 free(dev);
264                 return -1;
265         }
266         dev->id.subsystem_device_id = (uint16_t)tmp;
267
268         /* get class_id */
269         snprintf(filename, sizeof(filename), "%s/class",
270                  dirname);
271         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
272                 free(dev);
273                 return -1;
274         }
275         /* the least 24 bits are valid: class, subclass, program interface */
276         dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
277
278         /* get max_vfs */
279         dev->max_vfs = 0;
280         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
281         if (!access(filename, F_OK) &&
282             eal_parse_sysfs_value(filename, &tmp) == 0)
283                 dev->max_vfs = (uint16_t)tmp;
284         else {
285                 /* for non igb_uio driver, need kernel version >= 3.8 */
286                 snprintf(filename, sizeof(filename),
287                          "%s/sriov_numvfs", dirname);
288                 if (!access(filename, F_OK) &&
289                     eal_parse_sysfs_value(filename, &tmp) == 0)
290                         dev->max_vfs = (uint16_t)tmp;
291         }
292
293         /* get numa node, default to 0 if not present */
294         snprintf(filename, sizeof(filename), "%s/numa_node",
295                  dirname);
296
297         if (access(filename, F_OK) != -1) {
298                 if (eal_parse_sysfs_value(filename, &tmp) == 0)
299                         dev->device.numa_node = tmp;
300                 else
301                         dev->device.numa_node = -1;
302         } else {
303                 dev->device.numa_node = 0;
304         }
305
306         pci_name_set(dev);
307
308         /* parse resources */
309         snprintf(filename, sizeof(filename), "%s/resource", dirname);
310         if (pci_parse_sysfs_resource(filename, dev) < 0) {
311                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
312                 free(dev);
313                 return -1;
314         }
315
316         /* parse driver */
317         snprintf(filename, sizeof(filename), "%s/driver", dirname);
318         ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
319         if (ret < 0) {
320                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
321                 free(dev);
322                 return -1;
323         }
324
325         if (!ret) {
326                 if (!strcmp(driver, "vfio-pci"))
327                         dev->kdrv = RTE_KDRV_VFIO;
328                 else if (!strcmp(driver, "igb_uio"))
329                         dev->kdrv = RTE_KDRV_IGB_UIO;
330                 else if (!strcmp(driver, "uio_pci_generic"))
331                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
332                 else
333                         dev->kdrv = RTE_KDRV_UNKNOWN;
334         } else
335                 dev->kdrv = RTE_KDRV_NONE;
336
337         /* device is valid, add in list (sorted) */
338         if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
339                 rte_pci_add_device(dev);
340         } else {
341                 struct rte_pci_device *dev2;
342                 int ret;
343
344                 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
345                         ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
346                         if (ret > 0)
347                                 continue;
348
349                         if (ret < 0) {
350                                 rte_pci_insert_device(dev2, dev);
351                         } else { /* already registered */
352                                 if (!rte_dev_is_probed(&dev2->device)) {
353                                         dev2->kdrv = dev->kdrv;
354                                         dev2->max_vfs = dev->max_vfs;
355                                         pci_name_set(dev2);
356                                         memmove(dev2->mem_resource,
357                                                 dev->mem_resource,
358                                                 sizeof(dev->mem_resource));
359                                 } else {
360                                         /**
361                                          * If device is plugged and driver is
362                                          * probed already, (This happens when
363                                          * we call rte_dev_probe which will
364                                          * scan all device on the bus) we don't
365                                          * need to do anything here unless...
366                                          **/
367                                         if (dev2->kdrv != dev->kdrv ||
368                                                 dev2->max_vfs != dev->max_vfs)
369                                                 /*
370                                                  * This should not happens.
371                                                  * But it is still possible if
372                                                  * we unbind a device from
373                                                  * vfio or uio before hotplug
374                                                  * remove and rebind it with
375                                                  * a different configure.
376                                                  * So we just print out the
377                                                  * error as an alarm.
378                                                  */
379                                                 RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
380                                                         filename);
381                                 }
382                                 free(dev);
383                         }
384                         return 0;
385                 }
386
387                 rte_pci_add_device(dev);
388         }
389
390         return 0;
391 }
392
393 int
394 pci_update_device(const struct rte_pci_addr *addr)
395 {
396         char filename[PATH_MAX];
397
398         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
399                  rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
400                  addr->function);
401
402         return pci_scan_one(filename, addr);
403 }
404
405 /*
406  * split up a pci address into its constituent parts.
407  */
408 static int
409 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
410 {
411         /* first split on ':' */
412         union splitaddr {
413                 struct {
414                         char *domain;
415                         char *bus;
416                         char *devid;
417                         char *function;
418                 };
419                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
420         } splitaddr;
421
422         char *buf_copy = strndup(buf, bufsize);
423         if (buf_copy == NULL)
424                 return -1;
425
426         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
427                         != PCI_FMT_NVAL - 1)
428                 goto error;
429         /* final split is on '.' between devid and function */
430         splitaddr.function = strchr(splitaddr.devid,'.');
431         if (splitaddr.function == NULL)
432                 goto error;
433         *splitaddr.function++ = '\0';
434
435         /* now convert to int values */
436         errno = 0;
437         addr->domain = strtoul(splitaddr.domain, NULL, 16);
438         addr->bus = strtoul(splitaddr.bus, NULL, 16);
439         addr->devid = strtoul(splitaddr.devid, NULL, 16);
440         addr->function = strtoul(splitaddr.function, NULL, 10);
441         if (errno != 0)
442                 goto error;
443
444         free(buf_copy); /* free the copy made with strdup */
445         return 0;
446 error:
447         free(buf_copy);
448         return -1;
449 }
450
451 /*
452  * Scan the content of the PCI bus, and the devices in the devices
453  * list
454  */
455 int
456 rte_pci_scan(void)
457 {
458         struct dirent *e;
459         DIR *dir;
460         char dirname[PATH_MAX];
461         struct rte_pci_addr addr;
462
463         /* for debug purposes, PCI can be disabled */
464         if (!rte_eal_has_pci())
465                 return 0;
466
467 #ifdef VFIO_PRESENT
468         if (!pci_vfio_is_enabled())
469                 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
470 #endif
471
472         dir = opendir(rte_pci_get_sysfs_path());
473         if (dir == NULL) {
474                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
475                         __func__, strerror(errno));
476                 return -1;
477         }
478
479         while ((e = readdir(dir)) != NULL) {
480                 if (e->d_name[0] == '.')
481                         continue;
482
483                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
484                         continue;
485
486                 snprintf(dirname, sizeof(dirname), "%s/%s",
487                                 rte_pci_get_sysfs_path(), e->d_name);
488
489                 if (pci_scan_one(dirname, &addr) < 0)
490                         goto error;
491         }
492         closedir(dir);
493         return 0;
494
495 error:
496         closedir(dir);
497         return -1;
498 }
499
500 #if defined(RTE_ARCH_X86)
501 static bool
502 pci_one_device_iommu_support_va(const struct rte_pci_device *dev)
503 {
504 #define VTD_CAP_MGAW_SHIFT      16
505 #define VTD_CAP_MGAW_MASK       (0x3fULL << VTD_CAP_MGAW_SHIFT)
506 #define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
507         const struct rte_pci_addr *addr = &dev->addr;
508         char filename[PATH_MAX];
509         FILE *fp;
510         uint64_t mgaw, vtd_cap_reg = 0;
511
512         snprintf(filename, sizeof(filename),
513                  "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
514                  rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
515                  addr->function);
516         if (access(filename, F_OK) == -1) {
517                 /* We don't have an Intel IOMMU, assume VA supported*/
518                 return true;
519         }
520
521         /* We have an intel IOMMU */
522         fp = fopen(filename, "r");
523         if (fp == NULL) {
524                 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
525                 return false;
526         }
527
528         if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
529                 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
530                 fclose(fp);
531                 return false;
532         }
533
534         fclose(fp);
535
536         mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
537
538         /*
539          * Assuming there is no limitation by now. We can not know at this point
540          * because the memory has not been initialized yet. Setting the dma mask
541          * will force a check once memory initialization is done. We can not do
542          * a fallback to IOVA PA now, but if the dma check fails, the error
543          * message should advice for using '--iova-mode pa' if IOVA VA is the
544          * current mode.
545          */
546         rte_mem_set_dma_mask(mgaw);
547         return true;
548 }
549 #elif defined(RTE_ARCH_PPC_64)
550 static bool
551 pci_one_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
552 {
553         return false;
554 }
555 #else
556 static bool
557 pci_one_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
558 {
559         return true;
560 }
561 #endif
562
563 enum rte_iova_mode
564 pci_device_iova_mode(const struct rte_pci_driver *pdrv,
565                      const struct rte_pci_device *pdev)
566 {
567         enum rte_iova_mode iova_mode = RTE_IOVA_DC;
568         static int iommu_no_va = -1;
569
570         switch (pdev->kdrv) {
571         case RTE_KDRV_VFIO: {
572 #ifdef VFIO_PRESENT
573                 static int is_vfio_noiommu_enabled = -1;
574
575                 if (is_vfio_noiommu_enabled == -1) {
576                         if (rte_vfio_noiommu_is_enabled() == 1)
577                                 is_vfio_noiommu_enabled = 1;
578                         else
579                                 is_vfio_noiommu_enabled = 0;
580                 }
581                 if ((pdrv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) == 0) {
582                         iova_mode = RTE_IOVA_PA;
583                 } else if (is_vfio_noiommu_enabled != 0) {
584                         RTE_LOG(DEBUG, EAL, "Forcing to 'PA', vfio-noiommu mode configured\n");
585                         iova_mode = RTE_IOVA_PA;
586                 }
587 #endif
588                 break;
589         }
590
591         case RTE_KDRV_IGB_UIO:
592         case RTE_KDRV_UIO_GENERIC:
593                 iova_mode = RTE_IOVA_PA;
594                 break;
595
596         default:
597                 RTE_LOG(DEBUG, EAL, "Unsupported kernel driver? Defaulting to IOVA as 'PA'\n");
598                 iova_mode = RTE_IOVA_PA;
599                 break;
600         }
601
602         if (iova_mode != RTE_IOVA_PA) {
603                 /*
604                  * We can check this only once, because the IOMMU hardware is
605                  * the same for all of them.
606                  */
607                 if (iommu_no_va == -1)
608                         iommu_no_va = pci_one_device_iommu_support_va(pdev)
609                                         ? 0 : 1;
610                 if (iommu_no_va != 0) {
611                         RTE_LOG(DEBUG, EAL, "Forcing to 'PA', IOMMU does not support IOVA as 'VA'\n");
612                         iova_mode = RTE_IOVA_PA;
613                 }
614         }
615         return iova_mode;
616 }
617
618 /* Read PCI config space. */
619 int rte_pci_read_config(const struct rte_pci_device *device,
620                 void *buf, size_t len, off_t offset)
621 {
622         char devname[RTE_DEV_NAME_MAX_LEN] = "";
623         const struct rte_intr_handle *intr_handle = &device->intr_handle;
624
625         switch (device->kdrv) {
626         case RTE_KDRV_IGB_UIO:
627         case RTE_KDRV_UIO_GENERIC:
628                 return pci_uio_read_config(intr_handle, buf, len, offset);
629 #ifdef VFIO_PRESENT
630         case RTE_KDRV_VFIO:
631                 return pci_vfio_read_config(intr_handle, buf, len, offset);
632 #endif
633         default:
634                 rte_pci_device_name(&device->addr, devname,
635                                     RTE_DEV_NAME_MAX_LEN);
636                 RTE_LOG(ERR, EAL,
637                         "Unknown driver type for %s\n", devname);
638                 return -1;
639         }
640 }
641
642 /* Write PCI config space. */
643 int rte_pci_write_config(const struct rte_pci_device *device,
644                 const void *buf, size_t len, off_t offset)
645 {
646         char devname[RTE_DEV_NAME_MAX_LEN] = "";
647         const struct rte_intr_handle *intr_handle = &device->intr_handle;
648
649         switch (device->kdrv) {
650         case RTE_KDRV_IGB_UIO:
651         case RTE_KDRV_UIO_GENERIC:
652                 return pci_uio_write_config(intr_handle, buf, len, offset);
653 #ifdef VFIO_PRESENT
654         case RTE_KDRV_VFIO:
655                 return pci_vfio_write_config(intr_handle, buf, len, offset);
656 #endif
657         default:
658                 rte_pci_device_name(&device->addr, devname,
659                                     RTE_DEV_NAME_MAX_LEN);
660                 RTE_LOG(ERR, EAL,
661                         "Unknown driver type for %s\n", devname);
662                 return -1;
663         }
664 }
665
666 #if defined(RTE_ARCH_X86)
667 static int
668 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
669                 struct rte_pci_ioport *p)
670 {
671         uint16_t start, end;
672         FILE *fp;
673         char *line = NULL;
674         char pci_id[16];
675         int found = 0;
676         size_t linesz;
677
678         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
679                  dev->addr.domain, dev->addr.bus,
680                  dev->addr.devid, dev->addr.function);
681
682         fp = fopen("/proc/ioports", "r");
683         if (fp == NULL) {
684                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
685                 return -1;
686         }
687
688         while (getdelim(&line, &linesz, '\n', fp) > 0) {
689                 char *ptr = line;
690                 char *left;
691                 int n;
692
693                 n = strcspn(ptr, ":");
694                 ptr[n] = 0;
695                 left = &ptr[n + 1];
696
697                 while (*left && isspace(*left))
698                         left++;
699
700                 if (!strncmp(left, pci_id, strlen(pci_id))) {
701                         found = 1;
702
703                         while (*ptr && isspace(*ptr))
704                                 ptr++;
705
706                         sscanf(ptr, "%04hx-%04hx", &start, &end);
707
708                         break;
709                 }
710         }
711
712         free(line);
713         fclose(fp);
714
715         if (!found)
716                 return -1;
717
718         p->base = start;
719         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
720
721         return 0;
722 }
723 #endif
724
725 int
726 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
727                 struct rte_pci_ioport *p)
728 {
729         int ret = -1;
730
731         switch (dev->kdrv) {
732 #ifdef VFIO_PRESENT
733         case RTE_KDRV_VFIO:
734                 if (pci_vfio_is_enabled())
735                         ret = pci_vfio_ioport_map(dev, bar, p);
736                 break;
737 #endif
738         case RTE_KDRV_IGB_UIO:
739                 ret = pci_uio_ioport_map(dev, bar, p);
740                 break;
741         case RTE_KDRV_UIO_GENERIC:
742 #if defined(RTE_ARCH_X86)
743                 ret = pci_ioport_map(dev, bar, p);
744 #else
745                 ret = pci_uio_ioport_map(dev, bar, p);
746 #endif
747                 break;
748         case RTE_KDRV_NONE:
749 #if defined(RTE_ARCH_X86)
750                 ret = pci_ioport_map(dev, bar, p);
751 #endif
752                 break;
753         default:
754                 break;
755         }
756
757         if (!ret)
758                 p->dev = dev;
759
760         return ret;
761 }
762
763 void
764 rte_pci_ioport_read(struct rte_pci_ioport *p,
765                 void *data, size_t len, off_t offset)
766 {
767         switch (p->dev->kdrv) {
768 #ifdef VFIO_PRESENT
769         case RTE_KDRV_VFIO:
770                 pci_vfio_ioport_read(p, data, len, offset);
771                 break;
772 #endif
773         case RTE_KDRV_IGB_UIO:
774                 pci_uio_ioport_read(p, data, len, offset);
775                 break;
776         case RTE_KDRV_UIO_GENERIC:
777                 pci_uio_ioport_read(p, data, len, offset);
778                 break;
779         case RTE_KDRV_NONE:
780 #if defined(RTE_ARCH_X86)
781                 pci_uio_ioport_read(p, data, len, offset);
782 #endif
783                 break;
784         default:
785                 break;
786         }
787 }
788
789 void
790 rte_pci_ioport_write(struct rte_pci_ioport *p,
791                 const void *data, size_t len, off_t offset)
792 {
793         switch (p->dev->kdrv) {
794 #ifdef VFIO_PRESENT
795         case RTE_KDRV_VFIO:
796                 pci_vfio_ioport_write(p, data, len, offset);
797                 break;
798 #endif
799         case RTE_KDRV_IGB_UIO:
800                 pci_uio_ioport_write(p, data, len, offset);
801                 break;
802         case RTE_KDRV_UIO_GENERIC:
803                 pci_uio_ioport_write(p, data, len, offset);
804                 break;
805         case RTE_KDRV_NONE:
806 #if defined(RTE_ARCH_X86)
807                 pci_uio_ioport_write(p, data, len, offset);
808 #endif
809                 break;
810         default:
811                 break;
812         }
813 }
814
815 int
816 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
817 {
818         int ret = -1;
819
820         switch (p->dev->kdrv) {
821 #ifdef VFIO_PRESENT
822         case RTE_KDRV_VFIO:
823                 if (pci_vfio_is_enabled())
824                         ret = pci_vfio_ioport_unmap(p);
825                 break;
826 #endif
827         case RTE_KDRV_IGB_UIO:
828                 ret = pci_uio_ioport_unmap(p);
829                 break;
830         case RTE_KDRV_UIO_GENERIC:
831 #if defined(RTE_ARCH_X86)
832                 ret = 0;
833 #else
834                 ret = pci_uio_ioport_unmap(p);
835 #endif
836                 break;
837         case RTE_KDRV_NONE:
838 #if defined(RTE_ARCH_X86)
839                 ret = 0;
840 #endif
841                 break;
842         default:
843                 break;
844         }
845
846         return ret;
847 }