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