6dda054a7e22d54a28e79c5c549d14671c75729e
[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_private.h"
19 #include "eal_filesystem.h"
20
21 #include "private.h"
22 #include "pci_init.h"
23
24 /**
25  * @file
26  * PCI probing under linux
27  *
28  * This code is used to simulate a PCI probe by parsing information in sysfs.
29  * When a registered device matches a driver, it is then initialized with
30  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
31  */
32
33 extern struct rte_pci_bus rte_pci_bus;
34
35 static int
36 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
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                 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
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 *ms, void *arg)
121 {
122         void *end_va = RTE_PTR_ADD(ms->addr, ms->len);
123         void **max_va = arg;
124
125         if (*max_va < end_va)
126                 *max_va = end_va;
127         return 0;
128 }
129
130 void *
131 pci_find_max_end_va(void)
132 {
133         void *va = NULL;
134
135         rte_memseg_walk(find_max_end_va, &va);
136         return va;
137 }
138
139 /* parse one line of the "resource" sysfs file (note that the 'line'
140  * string is modified)
141  */
142 int
143 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
144         uint64_t *end_addr, uint64_t *flags)
145 {
146         union pci_resource_info {
147                 struct {
148                         char *phys_addr;
149                         char *end_addr;
150                         char *flags;
151                 };
152                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
153         } res_info;
154
155         if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
156                 RTE_LOG(ERR, EAL,
157                         "%s(): bad resource format\n", __func__);
158                 return -1;
159         }
160         errno = 0;
161         *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
162         *end_addr = strtoull(res_info.end_addr, NULL, 16);
163         *flags = strtoull(res_info.flags, NULL, 16);
164         if (errno != 0) {
165                 RTE_LOG(ERR, EAL,
166                         "%s(): bad resource format\n", __func__);
167                 return -1;
168         }
169
170         return 0;
171 }
172
173 /* parse the "resource" sysfs file */
174 static int
175 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
176 {
177         FILE *f;
178         char buf[BUFSIZ];
179         int i;
180         uint64_t phys_addr, end_addr, flags;
181
182         f = fopen(filename, "r");
183         if (f == NULL) {
184                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
185                 return -1;
186         }
187
188         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
189
190                 if (fgets(buf, sizeof(buf), f) == NULL) {
191                         RTE_LOG(ERR, EAL,
192                                 "%s(): cannot read resource\n", __func__);
193                         goto error;
194                 }
195                 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
196                                 &end_addr, &flags) < 0)
197                         goto error;
198
199                 if (flags & IORESOURCE_MEM) {
200                         dev->mem_resource[i].phys_addr = phys_addr;
201                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
202                         /* not mapped for now */
203                         dev->mem_resource[i].addr = NULL;
204                 }
205         }
206         fclose(f);
207         return 0;
208
209 error:
210         fclose(f);
211         return -1;
212 }
213
214 /* Scan one pci sysfs entry, and fill the devices list from it. */
215 static int
216 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
217 {
218         char filename[PATH_MAX];
219         unsigned long tmp;
220         struct rte_pci_device *dev;
221         char driver[PATH_MAX];
222         int ret;
223
224         dev = malloc(sizeof(*dev));
225         if (dev == NULL)
226                 return -1;
227
228         memset(dev, 0, sizeof(*dev));
229         dev->addr = *addr;
230
231         /* get vendor id */
232         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
233         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
234                 free(dev);
235                 return -1;
236         }
237         dev->id.vendor_id = (uint16_t)tmp;
238
239         /* get device id */
240         snprintf(filename, sizeof(filename), "%s/device", dirname);
241         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
242                 free(dev);
243                 return -1;
244         }
245         dev->id.device_id = (uint16_t)tmp;
246
247         /* get subsystem_vendor id */
248         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
249                  dirname);
250         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
251                 free(dev);
252                 return -1;
253         }
254         dev->id.subsystem_vendor_id = (uint16_t)tmp;
255
256         /* get subsystem_device id */
257         snprintf(filename, sizeof(filename), "%s/subsystem_device",
258                  dirname);
259         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
260                 free(dev);
261                 return -1;
262         }
263         dev->id.subsystem_device_id = (uint16_t)tmp;
264
265         /* get class_id */
266         snprintf(filename, sizeof(filename), "%s/class",
267                  dirname);
268         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
269                 free(dev);
270                 return -1;
271         }
272         /* the least 24 bits are valid: class, subclass, program interface */
273         dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
274
275         /* get max_vfs */
276         dev->max_vfs = 0;
277         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
278         if (!access(filename, F_OK) &&
279             eal_parse_sysfs_value(filename, &tmp) == 0)
280                 dev->max_vfs = (uint16_t)tmp;
281         else {
282                 /* for non igb_uio driver, need kernel version >= 3.8 */
283                 snprintf(filename, sizeof(filename),
284                          "%s/sriov_numvfs", dirname);
285                 if (!access(filename, F_OK) &&
286                     eal_parse_sysfs_value(filename, &tmp) == 0)
287                         dev->max_vfs = (uint16_t)tmp;
288         }
289
290         /* get numa node, default to 0 if not present */
291         snprintf(filename, sizeof(filename), "%s/numa_node",
292                  dirname);
293
294         if (access(filename, F_OK) != -1) {
295                 if (eal_parse_sysfs_value(filename, &tmp) == 0)
296                         dev->device.numa_node = tmp;
297                 else
298                         dev->device.numa_node = -1;
299         } else {
300                 dev->device.numa_node = 0;
301         }
302
303         pci_name_set(dev);
304
305         /* parse resources */
306         snprintf(filename, sizeof(filename), "%s/resource", dirname);
307         if (pci_parse_sysfs_resource(filename, dev) < 0) {
308                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
309                 free(dev);
310                 return -1;
311         }
312
313         /* parse driver */
314         snprintf(filename, sizeof(filename), "%s/driver", dirname);
315         ret = pci_get_kernel_driver_by_path(filename, driver);
316         if (ret < 0) {
317                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
318                 free(dev);
319                 return -1;
320         }
321
322         if (!ret) {
323                 if (!strcmp(driver, "vfio-pci"))
324                         dev->kdrv = RTE_KDRV_VFIO;
325                 else if (!strcmp(driver, "igb_uio"))
326                         dev->kdrv = RTE_KDRV_IGB_UIO;
327                 else if (!strcmp(driver, "uio_pci_generic"))
328                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
329                 else
330                         dev->kdrv = RTE_KDRV_UNKNOWN;
331         } else
332                 dev->kdrv = RTE_KDRV_NONE;
333
334         /* device is valid, add in list (sorted) */
335         if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
336                 rte_pci_add_device(dev);
337         } else {
338                 struct rte_pci_device *dev2;
339                 int ret;
340
341                 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
342                         ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
343                         if (ret > 0)
344                                 continue;
345
346                         if (ret < 0) {
347                                 rte_pci_insert_device(dev2, dev);
348                         } else { /* already registered */
349                                 dev2->kdrv = dev->kdrv;
350                                 dev2->max_vfs = dev->max_vfs;
351                                 pci_name_set(dev2);
352                                 memmove(dev2->mem_resource, dev->mem_resource,
353                                         sizeof(dev->mem_resource));
354                                 free(dev);
355                         }
356                         return 0;
357                 }
358
359                 rte_pci_add_device(dev);
360         }
361
362         return 0;
363 }
364
365 int
366 pci_update_device(const struct rte_pci_addr *addr)
367 {
368         char filename[PATH_MAX];
369
370         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
371                  rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
372                  addr->function);
373
374         return pci_scan_one(filename, addr);
375 }
376
377 /*
378  * split up a pci address into its constituent parts.
379  */
380 static int
381 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
382 {
383         /* first split on ':' */
384         union splitaddr {
385                 struct {
386                         char *domain;
387                         char *bus;
388                         char *devid;
389                         char *function;
390                 };
391                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
392         } splitaddr;
393
394         char *buf_copy = strndup(buf, bufsize);
395         if (buf_copy == NULL)
396                 return -1;
397
398         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
399                         != PCI_FMT_NVAL - 1)
400                 goto error;
401         /* final split is on '.' between devid and function */
402         splitaddr.function = strchr(splitaddr.devid,'.');
403         if (splitaddr.function == NULL)
404                 goto error;
405         *splitaddr.function++ = '\0';
406
407         /* now convert to int values */
408         errno = 0;
409         addr->domain = strtoul(splitaddr.domain, NULL, 16);
410         addr->bus = strtoul(splitaddr.bus, NULL, 16);
411         addr->devid = strtoul(splitaddr.devid, NULL, 16);
412         addr->function = strtoul(splitaddr.function, NULL, 10);
413         if (errno != 0)
414                 goto error;
415
416         free(buf_copy); /* free the copy made with strdup */
417         return 0;
418 error:
419         free(buf_copy);
420         return -1;
421 }
422
423 /*
424  * Scan the content of the PCI bus, and the devices in the devices
425  * list
426  */
427 int
428 rte_pci_scan(void)
429 {
430         struct dirent *e;
431         DIR *dir;
432         char dirname[PATH_MAX];
433         struct rte_pci_addr addr;
434
435         /* for debug purposes, PCI can be disabled */
436         if (!rte_eal_has_pci())
437                 return 0;
438
439 #ifdef VFIO_PRESENT
440         if (!pci_vfio_is_enabled())
441                 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
442 #endif
443
444         dir = opendir(rte_pci_get_sysfs_path());
445         if (dir == NULL) {
446                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
447                         __func__, strerror(errno));
448                 return -1;
449         }
450
451         while ((e = readdir(dir)) != NULL) {
452                 if (e->d_name[0] == '.')
453                         continue;
454
455                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
456                         continue;
457
458                 snprintf(dirname, sizeof(dirname), "%s/%s",
459                                 rte_pci_get_sysfs_path(), e->d_name);
460
461                 if (pci_scan_one(dirname, &addr) < 0)
462                         goto error;
463         }
464         closedir(dir);
465         return 0;
466
467 error:
468         closedir(dir);
469         return -1;
470 }
471
472 /*
473  * Is pci device bound to any kdrv
474  */
475 static inline int
476 pci_one_device_is_bound(void)
477 {
478         struct rte_pci_device *dev = NULL;
479         int ret = 0;
480
481         FOREACH_DEVICE_ON_PCIBUS(dev) {
482                 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
483                     dev->kdrv == RTE_KDRV_NONE) {
484                         continue;
485                 } else {
486                         ret = 1;
487                         break;
488                 }
489         }
490         return ret;
491 }
492
493 /*
494  * Any one of the device bound to uio
495  */
496 static inline int
497 pci_one_device_bound_uio(void)
498 {
499         struct rte_pci_device *dev = NULL;
500         struct rte_devargs *devargs;
501         int need_check;
502
503         FOREACH_DEVICE_ON_PCIBUS(dev) {
504                 devargs = dev->device.devargs;
505
506                 need_check = 0;
507                 switch (rte_pci_bus.bus.conf.scan_mode) {
508                 case RTE_BUS_SCAN_WHITELIST:
509                         if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
510                                 need_check = 1;
511                         break;
512                 case RTE_BUS_SCAN_UNDEFINED:
513                 case RTE_BUS_SCAN_BLACKLIST:
514                         if (devargs == NULL ||
515                             devargs->policy != RTE_DEV_BLACKLISTED)
516                                 need_check = 1;
517                         break;
518                 }
519
520                 if (!need_check)
521                         continue;
522
523                 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
524                    dev->kdrv == RTE_KDRV_UIO_GENERIC) {
525                         return 1;
526                 }
527         }
528         return 0;
529 }
530
531 /*
532  * Any one of the device has iova as va
533  */
534 static inline int
535 pci_one_device_has_iova_va(void)
536 {
537         struct rte_pci_device *dev = NULL;
538         struct rte_pci_driver *drv = NULL;
539
540         FOREACH_DRIVER_ON_PCIBUS(drv) {
541                 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
542                         FOREACH_DEVICE_ON_PCIBUS(dev) {
543                                 if (dev->kdrv == RTE_KDRV_VFIO &&
544                                     rte_pci_match(drv, dev))
545                                         return 1;
546                         }
547                 }
548         }
549         return 0;
550 }
551
552 #if defined(RTE_ARCH_X86)
553 static bool
554 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
555 {
556 #define VTD_CAP_MGAW_SHIFT      16
557 #define VTD_CAP_MGAW_MASK       (0x3fULL << VTD_CAP_MGAW_SHIFT)
558 #define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
559         struct rte_pci_addr *addr = &dev->addr;
560         char filename[PATH_MAX];
561         FILE *fp;
562         uint64_t mgaw, vtd_cap_reg = 0;
563
564         snprintf(filename, sizeof(filename),
565                  "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
566                  rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
567                  addr->function);
568         if (access(filename, F_OK) == -1) {
569                 /* We don't have an Intel IOMMU, assume VA supported*/
570                 return true;
571         }
572
573         /* We have an intel IOMMU */
574         fp = fopen(filename, "r");
575         if (fp == NULL) {
576                 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
577                 return false;
578         }
579
580         if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
581                 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
582                 fclose(fp);
583                 return false;
584         }
585
586         fclose(fp);
587
588         mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
589         if (mgaw < X86_VA_WIDTH)
590                 return false;
591
592         return true;
593 }
594 #elif defined(RTE_ARCH_PPC_64)
595 static bool
596 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
597 {
598         return false;
599 }
600 #else
601 static bool
602 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
603 {
604         return true;
605 }
606 #endif
607
608 /*
609  * All devices IOMMUs support VA as IOVA
610  */
611 static bool
612 pci_devices_iommu_support_va(void)
613 {
614         struct rte_pci_device *dev = NULL;
615         struct rte_pci_driver *drv = NULL;
616
617         FOREACH_DRIVER_ON_PCIBUS(drv) {
618                 FOREACH_DEVICE_ON_PCIBUS(dev) {
619                         if (!rte_pci_match(drv, dev))
620                                 continue;
621                         if (!pci_one_device_iommu_support_va(dev))
622                                 return false;
623                 }
624         }
625         return true;
626 }
627
628 /*
629  * Get iommu class of PCI devices on the bus.
630  */
631 enum rte_iova_mode
632 rte_pci_get_iommu_class(void)
633 {
634         bool is_bound;
635         bool is_vfio_noiommu_enabled = true;
636         bool has_iova_va;
637         bool is_bound_uio;
638         bool iommu_no_va;
639
640         is_bound = pci_one_device_is_bound();
641         if (!is_bound)
642                 return RTE_IOVA_DC;
643
644         has_iova_va = pci_one_device_has_iova_va();
645         is_bound_uio = pci_one_device_bound_uio();
646         iommu_no_va = !pci_devices_iommu_support_va();
647 #ifdef VFIO_PRESENT
648         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
649                                         true : false;
650 #endif
651
652         if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
653                         !iommu_no_va)
654                 return RTE_IOVA_VA;
655
656         if (has_iova_va) {
657                 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
658                 if (is_vfio_noiommu_enabled)
659                         RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
660                 if (is_bound_uio)
661                         RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
662                 if (iommu_no_va)
663                         RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
664         }
665
666         return RTE_IOVA_PA;
667 }
668
669 /* Read PCI config space. */
670 int rte_pci_read_config(const struct rte_pci_device *device,
671                 void *buf, size_t len, off_t offset)
672 {
673         const struct rte_intr_handle *intr_handle = &device->intr_handle;
674
675         switch (intr_handle->type) {
676         case RTE_INTR_HANDLE_UIO:
677         case RTE_INTR_HANDLE_UIO_INTX:
678                 return pci_uio_read_config(intr_handle, buf, len, offset);
679
680 #ifdef VFIO_PRESENT
681         case RTE_INTR_HANDLE_VFIO_MSIX:
682         case RTE_INTR_HANDLE_VFIO_MSI:
683         case RTE_INTR_HANDLE_VFIO_LEGACY:
684                 return pci_vfio_read_config(intr_handle, buf, len, offset);
685 #endif
686         default:
687                 RTE_LOG(ERR, EAL,
688                         "Unknown handle type of fd %d\n",
689                                         intr_handle->fd);
690                 return -1;
691         }
692 }
693
694 /* Write PCI config space. */
695 int rte_pci_write_config(const struct rte_pci_device *device,
696                 const void *buf, size_t len, off_t offset)
697 {
698         const struct rte_intr_handle *intr_handle = &device->intr_handle;
699
700         switch (intr_handle->type) {
701         case RTE_INTR_HANDLE_UIO:
702         case RTE_INTR_HANDLE_UIO_INTX:
703                 return pci_uio_write_config(intr_handle, buf, len, offset);
704
705 #ifdef VFIO_PRESENT
706         case RTE_INTR_HANDLE_VFIO_MSIX:
707         case RTE_INTR_HANDLE_VFIO_MSI:
708         case RTE_INTR_HANDLE_VFIO_LEGACY:
709                 return pci_vfio_write_config(intr_handle, buf, len, offset);
710 #endif
711         default:
712                 RTE_LOG(ERR, EAL,
713                         "Unknown handle type of fd %d\n",
714                                         intr_handle->fd);
715                 return -1;
716         }
717 }
718
719 #if defined(RTE_ARCH_X86)
720 static int
721 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
722                 struct rte_pci_ioport *p)
723 {
724         uint16_t start, end;
725         FILE *fp;
726         char *line = NULL;
727         char pci_id[16];
728         int found = 0;
729         size_t linesz;
730
731         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
732                  dev->addr.domain, dev->addr.bus,
733                  dev->addr.devid, dev->addr.function);
734
735         fp = fopen("/proc/ioports", "r");
736         if (fp == NULL) {
737                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
738                 return -1;
739         }
740
741         while (getdelim(&line, &linesz, '\n', fp) > 0) {
742                 char *ptr = line;
743                 char *left;
744                 int n;
745
746                 n = strcspn(ptr, ":");
747                 ptr[n] = 0;
748                 left = &ptr[n + 1];
749
750                 while (*left && isspace(*left))
751                         left++;
752
753                 if (!strncmp(left, pci_id, strlen(pci_id))) {
754                         found = 1;
755
756                         while (*ptr && isspace(*ptr))
757                                 ptr++;
758
759                         sscanf(ptr, "%04hx-%04hx", &start, &end);
760
761                         break;
762                 }
763         }
764
765         free(line);
766         fclose(fp);
767
768         if (!found)
769                 return -1;
770
771         p->base = start;
772         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
773
774         return 0;
775 }
776 #endif
777
778 int
779 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
780                 struct rte_pci_ioport *p)
781 {
782         int ret = -1;
783
784         switch (dev->kdrv) {
785 #ifdef VFIO_PRESENT
786         case RTE_KDRV_VFIO:
787                 if (pci_vfio_is_enabled())
788                         ret = pci_vfio_ioport_map(dev, bar, p);
789                 break;
790 #endif
791         case RTE_KDRV_IGB_UIO:
792                 ret = pci_uio_ioport_map(dev, bar, p);
793                 break;
794         case RTE_KDRV_UIO_GENERIC:
795 #if defined(RTE_ARCH_X86)
796                 ret = pci_ioport_map(dev, bar, p);
797 #else
798                 ret = pci_uio_ioport_map(dev, bar, p);
799 #endif
800                 break;
801         case RTE_KDRV_NONE:
802 #if defined(RTE_ARCH_X86)
803                 ret = pci_ioport_map(dev, bar, p);
804 #endif
805                 break;
806         default:
807                 break;
808         }
809
810         if (!ret)
811                 p->dev = dev;
812
813         return ret;
814 }
815
816 void
817 rte_pci_ioport_read(struct rte_pci_ioport *p,
818                 void *data, size_t len, off_t offset)
819 {
820         switch (p->dev->kdrv) {
821 #ifdef VFIO_PRESENT
822         case RTE_KDRV_VFIO:
823                 pci_vfio_ioport_read(p, data, len, offset);
824                 break;
825 #endif
826         case RTE_KDRV_IGB_UIO:
827                 pci_uio_ioport_read(p, data, len, offset);
828                 break;
829         case RTE_KDRV_UIO_GENERIC:
830                 pci_uio_ioport_read(p, data, len, offset);
831                 break;
832         case RTE_KDRV_NONE:
833 #if defined(RTE_ARCH_X86)
834                 pci_uio_ioport_read(p, data, len, offset);
835 #endif
836                 break;
837         default:
838                 break;
839         }
840 }
841
842 void
843 rte_pci_ioport_write(struct rte_pci_ioport *p,
844                 const void *data, size_t len, off_t offset)
845 {
846         switch (p->dev->kdrv) {
847 #ifdef VFIO_PRESENT
848         case RTE_KDRV_VFIO:
849                 pci_vfio_ioport_write(p, data, len, offset);
850                 break;
851 #endif
852         case RTE_KDRV_IGB_UIO:
853                 pci_uio_ioport_write(p, data, len, offset);
854                 break;
855         case RTE_KDRV_UIO_GENERIC:
856                 pci_uio_ioport_write(p, data, len, offset);
857                 break;
858         case RTE_KDRV_NONE:
859 #if defined(RTE_ARCH_X86)
860                 pci_uio_ioport_write(p, data, len, offset);
861 #endif
862                 break;
863         default:
864                 break;
865         }
866 }
867
868 int
869 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
870 {
871         int ret = -1;
872
873         switch (p->dev->kdrv) {
874 #ifdef VFIO_PRESENT
875         case RTE_KDRV_VFIO:
876                 if (pci_vfio_is_enabled())
877                         ret = pci_vfio_ioport_unmap(p);
878                 break;
879 #endif
880         case RTE_KDRV_IGB_UIO:
881                 ret = pci_uio_ioport_unmap(p);
882                 break;
883         case RTE_KDRV_UIO_GENERIC:
884 #if defined(RTE_ARCH_X86)
885                 ret = 0;
886 #else
887                 ret = pci_uio_ioport_unmap(p);
888 #endif
889                 break;
890         case RTE_KDRV_NONE:
891 #if defined(RTE_ARCH_X86)
892                 ret = 0;
893 #endif
894                 break;
895         default:
896                 break;
897         }
898
899         return ret;
900 }