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