xref: /dpdk/drivers/bus/pci/pci_common.c (revision 665b49c51639a10c553433bc2bcd85c7331c631e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5 
6 #include <string.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <sys/queue.h>
13 #include <rte_errno.h>
14 #include <rte_interrupts.h>
15 #include <rte_log.h>
16 #include <bus_driver.h>
17 #include <rte_pci.h>
18 #include <rte_bus_pci.h>
19 #include <rte_lcore.h>
20 #include <rte_per_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_eal.h>
23 #include <rte_eal_paging.h>
24 #include <rte_string_fns.h>
25 #include <rte_common.h>
26 #include <rte_devargs.h>
27 #include <rte_vfio.h>
28 #include <rte_tailq.h>
29 
30 #include "private.h"
31 
32 
33 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
34 
35 const char *rte_pci_get_sysfs_path(void)
36 {
37 	const char *path = NULL;
38 
39 #ifdef RTE_EXEC_ENV_LINUX
40 	path = getenv("SYSFS_PCI_DEVICES");
41 	if (path == NULL)
42 		return SYSFS_PCI_DEVICES;
43 #endif
44 
45 	return path;
46 }
47 
48 #ifdef RTE_EXEC_ENV_WINDOWS
49 #define asprintf pci_asprintf
50 
51 static int
52 __rte_format_printf(2, 3)
53 pci_asprintf(char **buffer, const char *format, ...)
54 {
55 	int size, ret;
56 	va_list arg;
57 
58 	va_start(arg, format);
59 	size = vsnprintf(NULL, 0, format, arg);
60 	va_end(arg);
61 	if (size < 0)
62 		return -1;
63 	size++;
64 
65 	*buffer = malloc(size);
66 	if (*buffer == NULL)
67 		return -1;
68 
69 	va_start(arg, format);
70 	ret = vsnprintf(*buffer, size, format, arg);
71 	va_end(arg);
72 	if (ret != size - 1) {
73 		free(*buffer);
74 		return -1;
75 	}
76 	return ret;
77 }
78 #endif /* RTE_EXEC_ENV_WINDOWS */
79 
80 static struct rte_devargs *
81 pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
82 {
83 	struct rte_devargs *devargs;
84 	struct rte_pci_addr addr;
85 
86 	RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
87 		devargs->bus->parse(devargs->name, &addr);
88 		if (!rte_pci_addr_cmp(pci_addr, &addr))
89 			return devargs;
90 	}
91 	return NULL;
92 }
93 
94 void
95 pci_common_set(struct rte_pci_device *dev)
96 {
97 	struct rte_devargs *devargs;
98 
99 	/* Each device has its internal, canonical name set. */
100 	rte_pci_device_name(&dev->addr,
101 			dev->name, sizeof(dev->name));
102 	devargs = pci_devargs_lookup(&dev->addr);
103 	dev->device.devargs = devargs;
104 
105 	/* When using a blocklist, only blocked devices will have
106 	 * an rte_devargs. Allowed devices won't have one.
107 	 */
108 	if (devargs != NULL)
109 		/* If an rte_devargs exists, the generic rte_device uses the
110 		 * given name as its name.
111 		 */
112 		dev->device.name = dev->device.devargs->name;
113 	else
114 		/* Otherwise, it uses the internal, canonical form. */
115 		dev->device.name = dev->name;
116 
117 	if (dev->bus_info != NULL ||
118 			asprintf(&dev->bus_info, "vendor_id=%"PRIx16", device_id=%"PRIx16,
119 				dev->id.vendor_id, dev->id.device_id) != -1)
120 		dev->device.bus_info = dev->bus_info;
121 }
122 
123 void
124 pci_free(struct rte_pci_device *dev)
125 {
126 	if (dev == NULL)
127 		return;
128 	free(dev->bus_info);
129 	free(dev);
130 }
131 
132 /* map a particular resource from a file */
133 void *
134 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
135 		 int additional_flags)
136 {
137 	void *mapaddr;
138 
139 	/* Map the PCI memory resource of device */
140 	mapaddr = rte_mem_map(requested_addr, size,
141 		RTE_PROT_READ | RTE_PROT_WRITE,
142 		RTE_MAP_SHARED | additional_flags, fd, offset);
143 	if (mapaddr == NULL) {
144 		RTE_LOG(ERR, EAL,
145 			"%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
146 			__func__, fd, requested_addr, size,
147 			(unsigned long long)offset,
148 			rte_strerror(rte_errno), mapaddr);
149 	} else
150 		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
151 
152 	return mapaddr;
153 }
154 
155 /* unmap a particular resource */
156 void
157 pci_unmap_resource(void *requested_addr, size_t size)
158 {
159 	if (requested_addr == NULL)
160 		return;
161 
162 	/* Unmap the PCI memory resource of device */
163 	if (rte_mem_unmap(requested_addr, size)) {
164 		RTE_LOG(ERR, EAL, "%s(): cannot mem unmap(%p, %#zx): %s\n",
165 			__func__, requested_addr, size,
166 			rte_strerror(rte_errno));
167 	} else
168 		RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
169 				requested_addr);
170 }
171 /*
172  * Match the PCI Driver and Device using the ID Table
173  */
174 int
175 rte_pci_match(const struct rte_pci_driver *pci_drv,
176 	      const struct rte_pci_device *pci_dev)
177 {
178 	const struct rte_pci_id *id_table;
179 
180 	for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
181 	     id_table++) {
182 		/* check if device's identifiers match the driver's ones */
183 		if (id_table->vendor_id != pci_dev->id.vendor_id &&
184 				id_table->vendor_id != RTE_PCI_ANY_ID)
185 			continue;
186 		if (id_table->device_id != pci_dev->id.device_id &&
187 				id_table->device_id != RTE_PCI_ANY_ID)
188 			continue;
189 		if (id_table->subsystem_vendor_id !=
190 		    pci_dev->id.subsystem_vendor_id &&
191 		    id_table->subsystem_vendor_id != RTE_PCI_ANY_ID)
192 			continue;
193 		if (id_table->subsystem_device_id !=
194 		    pci_dev->id.subsystem_device_id &&
195 		    id_table->subsystem_device_id != RTE_PCI_ANY_ID)
196 			continue;
197 		if (id_table->class_id != pci_dev->id.class_id &&
198 				id_table->class_id != RTE_CLASS_ANY_ID)
199 			continue;
200 
201 		return 1;
202 	}
203 
204 	return 0;
205 }
206 
207 /*
208  * If vendor/device ID match, call the probe() function of the
209  * driver.
210  */
211 static int
212 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
213 			 struct rte_pci_device *dev)
214 {
215 	int ret;
216 	bool already_probed;
217 	struct rte_pci_addr *loc;
218 
219 	if ((dr == NULL) || (dev == NULL))
220 		return -EINVAL;
221 
222 	loc = &dev->addr;
223 
224 	/* The device is not blocked; Check if driver supports it */
225 	if (!rte_pci_match(dr, dev))
226 		/* Match of device and driver failed */
227 		return 1;
228 
229 	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
230 			loc->domain, loc->bus, loc->devid, loc->function,
231 			dev->device.numa_node);
232 
233 	/* no initialization when marked as blocked, return without error */
234 	if (dev->device.devargs != NULL &&
235 		dev->device.devargs->policy == RTE_DEV_BLOCKED) {
236 		RTE_LOG(INFO, EAL, "  Device is blocked, not initializing\n");
237 		return 1;
238 	}
239 
240 	if (dev->device.numa_node < 0 && rte_socket_count() > 1)
241 		RTE_LOG(INFO, EAL, "Device %s is not NUMA-aware\n", dev->name);
242 
243 	already_probed = rte_dev_is_probed(&dev->device);
244 	if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
245 		RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
246 				dev->device.name);
247 		return -EEXIST;
248 	}
249 
250 	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
251 		dev->id.device_id, dr->driver.name);
252 
253 	if (!already_probed) {
254 		enum rte_iova_mode dev_iova_mode;
255 		enum rte_iova_mode iova_mode;
256 
257 		dev_iova_mode = pci_device_iova_mode(dr, dev);
258 		iova_mode = rte_eal_iova_mode();
259 		if (dev_iova_mode != RTE_IOVA_DC &&
260 		    dev_iova_mode != iova_mode) {
261 			RTE_LOG(ERR, EAL, "  Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
262 				dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
263 				iova_mode == RTE_IOVA_PA ? "PA" : "VA");
264 			return -EINVAL;
265 		}
266 
267 		/* Allocate interrupt instance for pci device */
268 		dev->intr_handle =
269 			rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
270 		if (dev->intr_handle == NULL) {
271 			RTE_LOG(ERR, EAL,
272 				"Failed to create interrupt instance for %s\n",
273 				dev->device.name);
274 			return -ENOMEM;
275 		}
276 
277 		dev->vfio_req_intr_handle =
278 			rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
279 		if (dev->vfio_req_intr_handle == NULL) {
280 			rte_intr_instance_free(dev->intr_handle);
281 			dev->intr_handle = NULL;
282 			RTE_LOG(ERR, EAL,
283 				"Failed to create vfio req interrupt instance for %s\n",
284 				dev->device.name);
285 			return -ENOMEM;
286 		}
287 
288 		/*
289 		 * Reference driver structure.
290 		 * This needs to be before rte_pci_map_device(), as it enables
291 		 * to use driver flags for adjusting configuration.
292 		 */
293 		dev->driver = dr;
294 		if (dev->driver->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
295 			ret = rte_pci_map_device(dev);
296 			if (ret != 0) {
297 				dev->driver = NULL;
298 				rte_intr_instance_free(dev->vfio_req_intr_handle);
299 				dev->vfio_req_intr_handle = NULL;
300 				rte_intr_instance_free(dev->intr_handle);
301 				dev->intr_handle = NULL;
302 				return ret;
303 			}
304 		}
305 	}
306 
307 	RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
308 			dr->driver.name, dev->id.vendor_id, dev->id.device_id,
309 			loc->domain, loc->bus, loc->devid, loc->function,
310 			dev->device.numa_node);
311 	/* call the driver probe() function */
312 	ret = dr->probe(dr, dev);
313 	if (already_probed)
314 		return ret; /* no rollback if already succeeded earlier */
315 	if (ret) {
316 		dev->driver = NULL;
317 		if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
318 			/* Don't unmap if device is unsupported and
319 			 * driver needs mapped resources.
320 			 */
321 			!(ret > 0 &&
322 				(dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
323 			rte_pci_unmap_device(dev);
324 		rte_intr_instance_free(dev->vfio_req_intr_handle);
325 		dev->vfio_req_intr_handle = NULL;
326 		rte_intr_instance_free(dev->intr_handle);
327 		dev->intr_handle = NULL;
328 	} else {
329 		dev->device.driver = &dr->driver;
330 	}
331 
332 	return ret;
333 }
334 
335 /*
336  * If vendor/device ID match, call the remove() function of the
337  * driver.
338  */
339 static int
340 rte_pci_detach_dev(struct rte_pci_device *dev)
341 {
342 	struct rte_pci_addr *loc;
343 	struct rte_pci_driver *dr;
344 	int ret = 0;
345 
346 	if (dev == NULL)
347 		return -EINVAL;
348 
349 	dr = dev->driver;
350 	loc = &dev->addr;
351 
352 	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
353 			loc->domain, loc->bus, loc->devid,
354 			loc->function, dev->device.numa_node);
355 
356 	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
357 			dev->id.device_id, dr->driver.name);
358 
359 	if (dr->remove) {
360 		ret = dr->remove(dev);
361 		if (ret < 0)
362 			return ret;
363 	}
364 
365 	/* clear driver structure */
366 	dev->driver = NULL;
367 	dev->device.driver = NULL;
368 
369 	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
370 		/* unmap resources for devices that use igb_uio */
371 		rte_pci_unmap_device(dev);
372 
373 	rte_intr_instance_free(dev->intr_handle);
374 	dev->intr_handle = NULL;
375 	rte_intr_instance_free(dev->vfio_req_intr_handle);
376 	dev->vfio_req_intr_handle = NULL;
377 
378 	return 0;
379 }
380 
381 /*
382  * If vendor/device ID match, call the probe() function of all
383  * registered driver for the given device. Return < 0 if initialization
384  * failed, return 1 if no driver is found for this device.
385  */
386 static int
387 pci_probe_all_drivers(struct rte_pci_device *dev)
388 {
389 	struct rte_pci_driver *dr = NULL;
390 	int rc = 0;
391 
392 	if (dev == NULL)
393 		return -EINVAL;
394 
395 	FOREACH_DRIVER_ON_PCIBUS(dr) {
396 		rc = rte_pci_probe_one_driver(dr, dev);
397 		if (rc < 0)
398 			/* negative value is an error */
399 			return rc;
400 		if (rc > 0)
401 			/* positive value means driver doesn't support it */
402 			continue;
403 		return 0;
404 	}
405 	return 1;
406 }
407 
408 /*
409  * Scan the content of the PCI bus, and call the probe() function for
410  * all registered drivers that have a matching entry in its id_table
411  * for discovered devices.
412  */
413 static int
414 pci_probe(void)
415 {
416 	struct rte_pci_device *dev = NULL;
417 	size_t probed = 0, failed = 0;
418 	int ret = 0;
419 
420 	FOREACH_DEVICE_ON_PCIBUS(dev) {
421 		probed++;
422 
423 		ret = pci_probe_all_drivers(dev);
424 		if (ret < 0) {
425 			if (ret != -EEXIST) {
426 				RTE_LOG(ERR, EAL, "Requested device "
427 					PCI_PRI_FMT " cannot be used\n",
428 					dev->addr.domain, dev->addr.bus,
429 					dev->addr.devid, dev->addr.function);
430 				rte_errno = errno;
431 				failed++;
432 			}
433 			ret = 0;
434 		}
435 	}
436 
437 	return (probed && probed == failed) ? -1 : 0;
438 }
439 
440 static int
441 pci_cleanup(void)
442 {
443 	struct rte_pci_device *dev, *tmp_dev;
444 	int error = 0;
445 
446 	RTE_TAILQ_FOREACH_SAFE(dev, &rte_pci_bus.device_list, next, tmp_dev) {
447 		struct rte_pci_driver *drv = dev->driver;
448 		int ret = 0;
449 
450 		if (drv == NULL || drv->remove == NULL)
451 			goto free;
452 
453 		ret = drv->remove(dev);
454 		if (ret < 0) {
455 			rte_errno = errno;
456 			error = -1;
457 		}
458 		dev->driver = NULL;
459 		dev->device.driver = NULL;
460 
461 free:
462 		/* free interrupt handles */
463 		rte_intr_instance_free(dev->intr_handle);
464 		dev->intr_handle = NULL;
465 		rte_intr_instance_free(dev->vfio_req_intr_handle);
466 		dev->vfio_req_intr_handle = NULL;
467 
468 		pci_free(dev);
469 	}
470 
471 	return error;
472 }
473 
474 /* dump one device */
475 static int
476 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
477 {
478 	int i;
479 
480 	fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
481 	       dev->addr.devid, dev->addr.function);
482 	fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
483 	       dev->id.device_id);
484 
485 	for (i = 0; i != sizeof(dev->mem_resource) /
486 		sizeof(dev->mem_resource[0]); i++) {
487 		fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
488 			dev->mem_resource[i].phys_addr,
489 			dev->mem_resource[i].len);
490 	}
491 	return 0;
492 }
493 
494 /* dump devices on the bus */
495 void
496 rte_pci_dump(FILE *f)
497 {
498 	struct rte_pci_device *dev = NULL;
499 
500 	FOREACH_DEVICE_ON_PCIBUS(dev) {
501 		pci_dump_one_device(f, dev);
502 	}
503 }
504 
505 static int
506 pci_parse(const char *name, void *addr)
507 {
508 	struct rte_pci_addr *out = addr;
509 	struct rte_pci_addr pci_addr;
510 	bool parse;
511 
512 	parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
513 	if (parse && addr != NULL)
514 		*out = pci_addr;
515 	return parse == false;
516 }
517 
518 /* register a driver */
519 void
520 rte_pci_register(struct rte_pci_driver *driver)
521 {
522 	TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
523 }
524 
525 /* unregister a driver */
526 void
527 rte_pci_unregister(struct rte_pci_driver *driver)
528 {
529 	TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
530 }
531 
532 /* Add a device to PCI bus */
533 void
534 rte_pci_add_device(struct rte_pci_device *pci_dev)
535 {
536 	TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
537 }
538 
539 /* Insert a device into a predefined position in PCI bus */
540 void
541 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
542 		      struct rte_pci_device *new_pci_dev)
543 {
544 	TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
545 }
546 
547 /* Remove a device from PCI bus */
548 static void
549 rte_pci_remove_device(struct rte_pci_device *pci_dev)
550 {
551 	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
552 }
553 
554 static struct rte_device *
555 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
556 		const void *data)
557 {
558 	const struct rte_pci_device *pstart;
559 	struct rte_pci_device *pdev;
560 
561 	if (start != NULL) {
562 		pstart = RTE_DEV_TO_PCI_CONST(start);
563 		pdev = TAILQ_NEXT(pstart, next);
564 	} else {
565 		pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
566 	}
567 	while (pdev != NULL) {
568 		if (cmp(&pdev->device, data) == 0)
569 			return &pdev->device;
570 		pdev = TAILQ_NEXT(pdev, next);
571 	}
572 	return NULL;
573 }
574 
575 /*
576  * find the device which encounter the failure, by iterate over all device on
577  * PCI bus to check if the memory failure address is located in the range
578  * of the BARs of the device.
579  */
580 static struct rte_pci_device *
581 pci_find_device_by_addr(const void *failure_addr)
582 {
583 	struct rte_pci_device *pdev = NULL;
584 	uint64_t check_point, start, end, len;
585 	int i;
586 
587 	check_point = (uint64_t)(uintptr_t)failure_addr;
588 
589 	FOREACH_DEVICE_ON_PCIBUS(pdev) {
590 		for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
591 			start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
592 			len = pdev->mem_resource[i].len;
593 			end = start + len;
594 			if (check_point >= start && check_point < end) {
595 				RTE_LOG(DEBUG, EAL, "Failure address %16.16"
596 					PRIx64" belongs to device %s!\n",
597 					check_point, pdev->device.name);
598 				return pdev;
599 			}
600 		}
601 	}
602 	return NULL;
603 }
604 
605 static int
606 pci_hot_unplug_handler(struct rte_device *dev)
607 {
608 	struct rte_pci_device *pdev = NULL;
609 	int ret = 0;
610 
611 	pdev = RTE_DEV_TO_PCI(dev);
612 	if (!pdev)
613 		return -1;
614 
615 	switch (pdev->kdrv) {
616 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
617 	case RTE_PCI_KDRV_VFIO:
618 		/*
619 		 * vfio kernel module guaranty the pci device would not be
620 		 * deleted until the user space release the resource, so no
621 		 * need to remap BARs resource here, just directly notify
622 		 * the req event to the user space to handle it.
623 		 */
624 		rte_dev_event_callback_process(dev->name,
625 					       RTE_DEV_EVENT_REMOVE);
626 		break;
627 #endif
628 	case RTE_PCI_KDRV_IGB_UIO:
629 	case RTE_PCI_KDRV_UIO_GENERIC:
630 	case RTE_PCI_KDRV_NIC_UIO:
631 		/* BARs resource is invalid, remap it to be safe. */
632 		ret = pci_uio_remap_resource(pdev);
633 		break;
634 	default:
635 		RTE_LOG(DEBUG, EAL,
636 			"Not managed by a supported kernel driver, skipped\n");
637 		ret = -1;
638 		break;
639 	}
640 
641 	return ret;
642 }
643 
644 static int
645 pci_sigbus_handler(const void *failure_addr)
646 {
647 	struct rte_pci_device *pdev = NULL;
648 	int ret = 0;
649 
650 	pdev = pci_find_device_by_addr(failure_addr);
651 	if (!pdev) {
652 		/* It is a generic sigbus error, no bus would handle it. */
653 		ret = 1;
654 	} else {
655 		/* The sigbus error is caused of hot-unplug. */
656 		ret = pci_hot_unplug_handler(&pdev->device);
657 		if (ret) {
658 			RTE_LOG(ERR, EAL,
659 				"Failed to handle hot-unplug for device %s",
660 				pdev->name);
661 			ret = -1;
662 		}
663 	}
664 	return ret;
665 }
666 
667 static int
668 pci_plug(struct rte_device *dev)
669 {
670 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
671 }
672 
673 static int
674 pci_unplug(struct rte_device *dev)
675 {
676 	struct rte_pci_device *pdev;
677 	int ret;
678 
679 	pdev = RTE_DEV_TO_PCI(dev);
680 	ret = rte_pci_detach_dev(pdev);
681 	if (ret == 0) {
682 		rte_pci_remove_device(pdev);
683 		rte_devargs_remove(dev->devargs);
684 		pci_free(pdev);
685 	}
686 	return ret;
687 }
688 
689 static int
690 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
691 {
692 	struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
693 
694 	if (!pdev || !pdev->driver) {
695 		rte_errno = EINVAL;
696 		return -1;
697 	}
698 	if (pdev->driver->dma_map)
699 		return pdev->driver->dma_map(pdev, addr, iova, len);
700 	/**
701 	 *  In case driver don't provides any specific mapping
702 	 *  try fallback to VFIO.
703 	 */
704 	if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
705 		return rte_vfio_container_dma_map
706 				(RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
707 				 iova, len);
708 	rte_errno = ENOTSUP;
709 	return -1;
710 }
711 
712 static int
713 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
714 {
715 	struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
716 
717 	if (!pdev || !pdev->driver) {
718 		rte_errno = EINVAL;
719 		return -1;
720 	}
721 	if (pdev->driver->dma_unmap)
722 		return pdev->driver->dma_unmap(pdev, addr, iova, len);
723 	/**
724 	 *  In case driver don't provides any specific mapping
725 	 *  try fallback to VFIO.
726 	 */
727 	if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
728 		return rte_vfio_container_dma_unmap
729 				(RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
730 				 iova, len);
731 	rte_errno = ENOTSUP;
732 	return -1;
733 }
734 
735 bool
736 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
737 {
738 	struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
739 
740 	switch (rte_pci_bus.bus.conf.scan_mode) {
741 	case RTE_BUS_SCAN_ALLOWLIST:
742 		if (devargs && devargs->policy == RTE_DEV_ALLOWED)
743 			return false;
744 		break;
745 	case RTE_BUS_SCAN_UNDEFINED:
746 	case RTE_BUS_SCAN_BLOCKLIST:
747 		if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
748 			return false;
749 		break;
750 	}
751 	return true;
752 }
753 
754 enum rte_iova_mode
755 rte_pci_get_iommu_class(void)
756 {
757 	enum rte_iova_mode iova_mode = RTE_IOVA_DC;
758 	const struct rte_pci_device *dev;
759 	const struct rte_pci_driver *drv;
760 	bool devices_want_va = false;
761 	bool devices_want_pa = false;
762 	int iommu_no_va = -1;
763 
764 	FOREACH_DEVICE_ON_PCIBUS(dev) {
765 		/*
766 		 * We can check this only once, because the IOMMU hardware is
767 		 * the same for all of them.
768 		 */
769 		if (iommu_no_va == -1)
770 			iommu_no_va = pci_device_iommu_support_va(dev)
771 					? 0 : 1;
772 
773 		if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
774 		    dev->kdrv == RTE_PCI_KDRV_NONE)
775 			continue;
776 		FOREACH_DRIVER_ON_PCIBUS(drv) {
777 			enum rte_iova_mode dev_iova_mode;
778 
779 			if (!rte_pci_match(drv, dev))
780 				continue;
781 
782 			dev_iova_mode = pci_device_iova_mode(drv, dev);
783 			RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
784 				PCI_PRI_FMT " wants IOVA as '%s'\n",
785 				drv->driver.name,
786 				dev->addr.domain, dev->addr.bus,
787 				dev->addr.devid, dev->addr.function,
788 				dev_iova_mode == RTE_IOVA_DC ? "DC" :
789 				(dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
790 			if (dev_iova_mode == RTE_IOVA_PA)
791 				devices_want_pa = true;
792 			else if (dev_iova_mode == RTE_IOVA_VA)
793 				devices_want_va = true;
794 		}
795 	}
796 	if (iommu_no_va == 1) {
797 		iova_mode = RTE_IOVA_PA;
798 		if (devices_want_va) {
799 			RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
800 			RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
801 		}
802 	} else if (devices_want_va && !devices_want_pa) {
803 		iova_mode = RTE_IOVA_VA;
804 	} else if (devices_want_pa && !devices_want_va) {
805 		iova_mode = RTE_IOVA_PA;
806 	} else {
807 		iova_mode = RTE_IOVA_DC;
808 		if (devices_want_va) {
809 			RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
810 			RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
811 		}
812 	}
813 	return iova_mode;
814 }
815 
816 off_t
817 rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap)
818 {
819 	off_t offset = RTE_PCI_CFG_SPACE_SIZE;
820 	uint32_t header;
821 	int ttl;
822 
823 	/* minimum 8 bytes per capability */
824 	ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
825 
826 	if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
827 		RTE_LOG(ERR, EAL, "error in reading extended capabilities\n");
828 		return -1;
829 	}
830 
831 	/*
832 	 * If we have no capabilities, this is indicated by cap ID,
833 	 * cap version and next pointer all being 0.
834 	 */
835 	if (header == 0)
836 		return 0;
837 
838 	while (ttl != 0) {
839 		if (RTE_PCI_EXT_CAP_ID(header) == cap)
840 			return offset;
841 
842 		offset = RTE_PCI_EXT_CAP_NEXT(header);
843 
844 		if (offset < RTE_PCI_CFG_SPACE_SIZE)
845 			break;
846 
847 		if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
848 			RTE_LOG(ERR, EAL,
849 				"error in reading extended capabilities\n");
850 			return -1;
851 		}
852 
853 		ttl--;
854 	}
855 
856 	return 0;
857 }
858 
859 int
860 rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable)
861 {
862 	uint16_t old_cmd, cmd;
863 
864 	if (rte_pci_read_config(dev, &old_cmd, sizeof(old_cmd),
865 				RTE_PCI_COMMAND) < 0) {
866 		RTE_LOG(ERR, EAL, "error in reading PCI command register\n");
867 		return -1;
868 	}
869 
870 	if (enable)
871 		cmd = old_cmd | RTE_PCI_COMMAND_MASTER;
872 	else
873 		cmd = old_cmd & ~RTE_PCI_COMMAND_MASTER;
874 
875 	if (cmd == old_cmd)
876 		return 0;
877 
878 	if (rte_pci_write_config(dev, &cmd, sizeof(cmd),
879 				 RTE_PCI_COMMAND) < 0) {
880 		RTE_LOG(ERR, EAL, "error in writing PCI command register\n");
881 		return -1;
882 	}
883 
884 	return 0;
885 }
886 
887 struct rte_pci_bus rte_pci_bus = {
888 	.bus = {
889 		.scan = rte_pci_scan,
890 		.probe = pci_probe,
891 		.cleanup = pci_cleanup,
892 		.find_device = pci_find_device,
893 		.plug = pci_plug,
894 		.unplug = pci_unplug,
895 		.parse = pci_parse,
896 		.devargs_parse = rte_pci_devargs_parse,
897 		.dma_map = pci_dma_map,
898 		.dma_unmap = pci_dma_unmap,
899 		.get_iommu_class = rte_pci_get_iommu_class,
900 		.dev_iterate = rte_pci_dev_iterate,
901 		.hot_unplug_handler = pci_hot_unplug_handler,
902 		.sigbus_handler = pci_sigbus_handler,
903 	},
904 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
905 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
906 };
907 
908 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);
909