xref: /dpdk/drivers/bus/pci/linux/pci.c (revision c39d1e082a4b426e915074ce30eb6f410ee2654a)
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_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_KDRV_IGB_UIO:
78 	case RTE_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_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_KDRV_IGB_UIO:
107 	case RTE_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_KDRV_VFIO;
327 		else if (!strcmp(driver, "igb_uio"))
328 			dev->kdrv = RTE_KDRV_IGB_UIO;
329 		else if (!strcmp(driver, "uio_pci_generic"))
330 			dev->kdrv = RTE_KDRV_UIO_GENERIC;
331 		else
332 			dev->kdrv = RTE_KDRV_UNKNOWN;
333 	} else
334 		dev->kdrv = RTE_KDRV_NONE;
335 
336 	/* device is valid, add in list (sorted) */
337 	if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
338 		rte_pci_add_device(dev);
339 	} else {
340 		struct rte_pci_device *dev2;
341 		int ret;
342 
343 		TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
344 			ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
345 			if (ret > 0)
346 				continue;
347 
348 			if (ret < 0) {
349 				rte_pci_insert_device(dev2, dev);
350 			} else { /* already registered */
351 				if (!rte_dev_is_probed(&dev2->device)) {
352 					dev2->kdrv = dev->kdrv;
353 					dev2->max_vfs = dev->max_vfs;
354 					pci_name_set(dev2);
355 					memmove(dev2->mem_resource,
356 						dev->mem_resource,
357 						sizeof(dev->mem_resource));
358 				} else {
359 					/**
360 					 * If device is plugged and driver is
361 					 * probed already, (This happens when
362 					 * we call rte_dev_probe which will
363 					 * scan all device on the bus) we don't
364 					 * need to do anything here unless...
365 					 **/
366 					if (dev2->kdrv != dev->kdrv ||
367 						dev2->max_vfs != dev->max_vfs)
368 						/*
369 						 * This should not happens.
370 						 * But it is still possible if
371 						 * we unbind a device from
372 						 * vfio or uio before hotplug
373 						 * remove and rebind it with
374 						 * a different configure.
375 						 * So we just print out the
376 						 * error as an alarm.
377 						 */
378 						RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
379 							filename);
380 				}
381 				free(dev);
382 			}
383 			return 0;
384 		}
385 
386 		rte_pci_add_device(dev);
387 	}
388 
389 	return 0;
390 }
391 
392 int
393 pci_update_device(const struct rte_pci_addr *addr)
394 {
395 	char filename[PATH_MAX];
396 
397 	snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
398 		 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
399 		 addr->function);
400 
401 	return pci_scan_one(filename, addr);
402 }
403 
404 /*
405  * split up a pci address into its constituent parts.
406  */
407 static int
408 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
409 {
410 	/* first split on ':' */
411 	union splitaddr {
412 		struct {
413 			char *domain;
414 			char *bus;
415 			char *devid;
416 			char *function;
417 		};
418 		char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
419 	} splitaddr;
420 
421 	char *buf_copy = strndup(buf, bufsize);
422 	if (buf_copy == NULL)
423 		return -1;
424 
425 	if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
426 			!= PCI_FMT_NVAL - 1)
427 		goto error;
428 	/* final split is on '.' between devid and function */
429 	splitaddr.function = strchr(splitaddr.devid,'.');
430 	if (splitaddr.function == NULL)
431 		goto error;
432 	*splitaddr.function++ = '\0';
433 
434 	/* now convert to int values */
435 	errno = 0;
436 	addr->domain = strtoul(splitaddr.domain, NULL, 16);
437 	addr->bus = strtoul(splitaddr.bus, NULL, 16);
438 	addr->devid = strtoul(splitaddr.devid, NULL, 16);
439 	addr->function = strtoul(splitaddr.function, NULL, 10);
440 	if (errno != 0)
441 		goto error;
442 
443 	free(buf_copy); /* free the copy made with strdup */
444 	return 0;
445 error:
446 	free(buf_copy);
447 	return -1;
448 }
449 
450 /*
451  * Scan the content of the PCI bus, and the devices in the devices
452  * list
453  */
454 int
455 rte_pci_scan(void)
456 {
457 	struct dirent *e;
458 	DIR *dir;
459 	char dirname[PATH_MAX];
460 	struct rte_pci_addr addr;
461 
462 	/* for debug purposes, PCI can be disabled */
463 	if (!rte_eal_has_pci())
464 		return 0;
465 
466 #ifdef VFIO_PRESENT
467 	if (!pci_vfio_is_enabled())
468 		RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
469 #endif
470 
471 	dir = opendir(rte_pci_get_sysfs_path());
472 	if (dir == NULL) {
473 		RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
474 			__func__, strerror(errno));
475 		return -1;
476 	}
477 
478 	while ((e = readdir(dir)) != NULL) {
479 		if (e->d_name[0] == '.')
480 			continue;
481 
482 		if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
483 			continue;
484 
485 		snprintf(dirname, sizeof(dirname), "%s/%s",
486 				rte_pci_get_sysfs_path(), e->d_name);
487 
488 		if (pci_scan_one(dirname, &addr) < 0)
489 			goto error;
490 	}
491 	closedir(dir);
492 	return 0;
493 
494 error:
495 	closedir(dir);
496 	return -1;
497 }
498 
499 #if defined(RTE_ARCH_X86)
500 bool
501 pci_device_iommu_support_va(const struct rte_pci_device *dev)
502 {
503 #define VTD_CAP_MGAW_SHIFT	16
504 #define VTD_CAP_MGAW_MASK	(0x3fULL << VTD_CAP_MGAW_SHIFT)
505 	const struct rte_pci_addr *addr = &dev->addr;
506 	char filename[PATH_MAX];
507 	FILE *fp;
508 	uint64_t mgaw, vtd_cap_reg = 0;
509 
510 	snprintf(filename, sizeof(filename),
511 		 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
512 		 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
513 		 addr->function);
514 
515 	fp = fopen(filename, "r");
516 	if (fp == NULL) {
517 		/* We don't have an Intel IOMMU, assume VA supported */
518 		if (errno == ENOENT)
519 			return true;
520 
521 		RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n",
522 			__func__, filename, strerror(errno));
523 		return false;
524 	}
525 
526 	/* We have an Intel IOMMU */
527 	if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
528 		RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
529 		fclose(fp);
530 		return false;
531 	}
532 
533 	fclose(fp);
534 
535 	mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
536 
537 	/*
538 	 * Assuming there is no limitation by now. We can not know at this point
539 	 * because the memory has not been initialized yet. Setting the dma mask
540 	 * will force a check once memory initialization is done. We can not do
541 	 * a fallback to IOVA PA now, but if the dma check fails, the error
542 	 * message should advice for using '--iova-mode pa' if IOVA VA is the
543 	 * current mode.
544 	 */
545 	rte_mem_set_dma_mask(mgaw);
546 	return true;
547 }
548 #elif defined(RTE_ARCH_PPC_64)
549 bool
550 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
551 {
552 	return false;
553 }
554 #else
555 bool
556 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
557 {
558 	return true;
559 }
560 #endif
561 
562 enum rte_iova_mode
563 pci_device_iova_mode(const struct rte_pci_driver *pdrv,
564 		     const struct rte_pci_device *pdev)
565 {
566 	enum rte_iova_mode iova_mode = RTE_IOVA_DC;
567 
568 	switch (pdev->kdrv) {
569 	case RTE_KDRV_VFIO: {
570 #ifdef VFIO_PRESENT
571 		static int is_vfio_noiommu_enabled = -1;
572 
573 		if (is_vfio_noiommu_enabled == -1) {
574 			if (rte_vfio_noiommu_is_enabled() == 1)
575 				is_vfio_noiommu_enabled = 1;
576 			else
577 				is_vfio_noiommu_enabled = 0;
578 		}
579 		if (is_vfio_noiommu_enabled != 0)
580 			iova_mode = RTE_IOVA_PA;
581 		else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
582 			iova_mode = RTE_IOVA_VA;
583 #endif
584 		break;
585 	}
586 
587 	case RTE_KDRV_IGB_UIO:
588 	case RTE_KDRV_UIO_GENERIC:
589 		iova_mode = RTE_IOVA_PA;
590 		break;
591 
592 	default:
593 		if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
594 			iova_mode = RTE_IOVA_VA;
595 		break;
596 	}
597 	return iova_mode;
598 }
599 
600 /* Read PCI config space. */
601 int rte_pci_read_config(const struct rte_pci_device *device,
602 		void *buf, size_t len, off_t offset)
603 {
604 	char devname[RTE_DEV_NAME_MAX_LEN] = "";
605 	const struct rte_intr_handle *intr_handle = &device->intr_handle;
606 
607 	switch (device->kdrv) {
608 	case RTE_KDRV_IGB_UIO:
609 	case RTE_KDRV_UIO_GENERIC:
610 		return pci_uio_read_config(intr_handle, buf, len, offset);
611 #ifdef VFIO_PRESENT
612 	case RTE_KDRV_VFIO:
613 		return pci_vfio_read_config(intr_handle, buf, len, offset);
614 #endif
615 	default:
616 		rte_pci_device_name(&device->addr, devname,
617 				    RTE_DEV_NAME_MAX_LEN);
618 		RTE_LOG(ERR, EAL,
619 			"Unknown driver type for %s\n", devname);
620 		return -1;
621 	}
622 }
623 
624 /* Write PCI config space. */
625 int rte_pci_write_config(const struct rte_pci_device *device,
626 		const void *buf, size_t len, off_t offset)
627 {
628 	char devname[RTE_DEV_NAME_MAX_LEN] = "";
629 	const struct rte_intr_handle *intr_handle = &device->intr_handle;
630 
631 	switch (device->kdrv) {
632 	case RTE_KDRV_IGB_UIO:
633 	case RTE_KDRV_UIO_GENERIC:
634 		return pci_uio_write_config(intr_handle, buf, len, offset);
635 #ifdef VFIO_PRESENT
636 	case RTE_KDRV_VFIO:
637 		return pci_vfio_write_config(intr_handle, buf, len, offset);
638 #endif
639 	default:
640 		rte_pci_device_name(&device->addr, devname,
641 				    RTE_DEV_NAME_MAX_LEN);
642 		RTE_LOG(ERR, EAL,
643 			"Unknown driver type for %s\n", devname);
644 		return -1;
645 	}
646 }
647 
648 #if defined(RTE_ARCH_X86)
649 static int
650 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
651 		struct rte_pci_ioport *p)
652 {
653 	uint16_t start, end;
654 	FILE *fp;
655 	char *line = NULL;
656 	char pci_id[16];
657 	int found = 0;
658 	size_t linesz;
659 
660 	snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
661 		 dev->addr.domain, dev->addr.bus,
662 		 dev->addr.devid, dev->addr.function);
663 
664 	fp = fopen("/proc/ioports", "r");
665 	if (fp == NULL) {
666 		RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
667 		return -1;
668 	}
669 
670 	while (getdelim(&line, &linesz, '\n', fp) > 0) {
671 		char *ptr = line;
672 		char *left;
673 		int n;
674 
675 		n = strcspn(ptr, ":");
676 		ptr[n] = 0;
677 		left = &ptr[n + 1];
678 
679 		while (*left && isspace(*left))
680 			left++;
681 
682 		if (!strncmp(left, pci_id, strlen(pci_id))) {
683 			found = 1;
684 
685 			while (*ptr && isspace(*ptr))
686 				ptr++;
687 
688 			sscanf(ptr, "%04hx-%04hx", &start, &end);
689 
690 			break;
691 		}
692 	}
693 
694 	free(line);
695 	fclose(fp);
696 
697 	if (!found)
698 		return -1;
699 
700 	p->base = start;
701 	RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
702 
703 	return 0;
704 }
705 #endif
706 
707 int
708 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
709 		struct rte_pci_ioport *p)
710 {
711 	int ret = -1;
712 
713 	switch (dev->kdrv) {
714 #ifdef VFIO_PRESENT
715 	case RTE_KDRV_VFIO:
716 		if (pci_vfio_is_enabled())
717 			ret = pci_vfio_ioport_map(dev, bar, p);
718 		break;
719 #endif
720 	case RTE_KDRV_IGB_UIO:
721 		ret = pci_uio_ioport_map(dev, bar, p);
722 		break;
723 	case RTE_KDRV_UIO_GENERIC:
724 #if defined(RTE_ARCH_X86)
725 		ret = pci_ioport_map(dev, bar, p);
726 #else
727 		ret = pci_uio_ioport_map(dev, bar, p);
728 #endif
729 		break;
730 	case RTE_KDRV_NONE:
731 #if defined(RTE_ARCH_X86)
732 		ret = pci_ioport_map(dev, bar, p);
733 #endif
734 		break;
735 	default:
736 		break;
737 	}
738 
739 	if (!ret)
740 		p->dev = dev;
741 
742 	return ret;
743 }
744 
745 void
746 rte_pci_ioport_read(struct rte_pci_ioport *p,
747 		void *data, size_t len, off_t offset)
748 {
749 	switch (p->dev->kdrv) {
750 #ifdef VFIO_PRESENT
751 	case RTE_KDRV_VFIO:
752 		pci_vfio_ioport_read(p, data, len, offset);
753 		break;
754 #endif
755 	case RTE_KDRV_IGB_UIO:
756 		pci_uio_ioport_read(p, data, len, offset);
757 		break;
758 	case RTE_KDRV_UIO_GENERIC:
759 		pci_uio_ioport_read(p, data, len, offset);
760 		break;
761 	case RTE_KDRV_NONE:
762 #if defined(RTE_ARCH_X86)
763 		pci_uio_ioport_read(p, data, len, offset);
764 #endif
765 		break;
766 	default:
767 		break;
768 	}
769 }
770 
771 void
772 rte_pci_ioport_write(struct rte_pci_ioport *p,
773 		const void *data, size_t len, off_t offset)
774 {
775 	switch (p->dev->kdrv) {
776 #ifdef VFIO_PRESENT
777 	case RTE_KDRV_VFIO:
778 		pci_vfio_ioport_write(p, data, len, offset);
779 		break;
780 #endif
781 	case RTE_KDRV_IGB_UIO:
782 		pci_uio_ioport_write(p, data, len, offset);
783 		break;
784 	case RTE_KDRV_UIO_GENERIC:
785 		pci_uio_ioport_write(p, data, len, offset);
786 		break;
787 	case RTE_KDRV_NONE:
788 #if defined(RTE_ARCH_X86)
789 		pci_uio_ioport_write(p, data, len, offset);
790 #endif
791 		break;
792 	default:
793 		break;
794 	}
795 }
796 
797 int
798 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
799 {
800 	int ret = -1;
801 
802 	switch (p->dev->kdrv) {
803 #ifdef VFIO_PRESENT
804 	case RTE_KDRV_VFIO:
805 		if (pci_vfio_is_enabled())
806 			ret = pci_vfio_ioport_unmap(p);
807 		break;
808 #endif
809 	case RTE_KDRV_IGB_UIO:
810 		ret = pci_uio_ioport_unmap(p);
811 		break;
812 	case RTE_KDRV_UIO_GENERIC:
813 #if defined(RTE_ARCH_X86)
814 		ret = 0;
815 #else
816 		ret = pci_uio_ioport_unmap(p);
817 #endif
818 		break;
819 	case RTE_KDRV_NONE:
820 #if defined(RTE_ARCH_X86)
821 		ret = 0;
822 #endif
823 		break;
824 	default:
825 		break;
826 	}
827 
828 	return ret;
829 }
830