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