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