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