Branch data Line data Source code
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 <eal_export.h>
14 : : #include <rte_errno.h>
15 : : #include <rte_interrupts.h>
16 : : #include <rte_log.h>
17 : : #include <bus_driver.h>
18 : : #include <rte_pci.h>
19 : : #include <rte_bus_pci.h>
20 : : #include <rte_lcore.h>
21 : : #include <rte_per_lcore.h>
22 : : #include <rte_memory.h>
23 : : #include <rte_eal.h>
24 : : #include <rte_eal_paging.h>
25 : : #include <rte_string_fns.h>
26 : : #include <rte_common.h>
27 : : #include <rte_devargs.h>
28 : : #include <rte_vfio.h>
29 : : #include <rte_tailq.h>
30 : :
31 : : #include "private.h"
32 : :
33 : :
34 : : #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
35 : :
36 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_get_sysfs_path)
37 : 8334 : const char *rte_pci_get_sysfs_path(void)
38 : : {
39 : : const char *path = NULL;
40 : :
41 : : #ifdef RTE_EXEC_ENV_LINUX
42 : 8334 : path = getenv("SYSFS_PCI_DEVICES");
43 [ + - ]: 8334 : if (path == NULL)
44 : 8334 : return SYSFS_PCI_DEVICES;
45 : : #endif
46 : :
47 : : return path;
48 : : }
49 : :
50 : : #ifdef RTE_EXEC_ENV_WINDOWS
51 : : #define asprintf pci_asprintf
52 : :
53 : : static int
54 : : __rte_format_printf(2, 3)
55 : : pci_asprintf(char **buffer, const char *format, ...)
56 : : {
57 : : int size, ret;
58 : : va_list arg;
59 : :
60 : : va_start(arg, format);
61 : : size = vsnprintf(NULL, 0, format, arg);
62 : : va_end(arg);
63 : : if (size < 0)
64 : : return -1;
65 : : size++;
66 : :
67 : : *buffer = malloc(size);
68 : : if (*buffer == NULL)
69 : : return -1;
70 : :
71 : : va_start(arg, format);
72 : : ret = vsnprintf(*buffer, size, format, arg);
73 : : va_end(arg);
74 : : if (ret != size - 1) {
75 : : free(*buffer);
76 : : return -1;
77 : : }
78 : : return ret;
79 : : }
80 : : #endif /* RTE_EXEC_ENV_WINDOWS */
81 : :
82 : : void
83 : 7955 : pci_common_set(struct rte_pci_device *dev)
84 : : {
85 : : /* Each device has its internal, canonical name set. */
86 : 7955 : rte_pci_device_name(&dev->addr,
87 : 7955 : dev->name, sizeof(dev->name));
88 : 7955 : dev->device.name = dev->name;
89 : :
90 : 7955 : dev->device.devargs = rte_bus_find_devargs(&rte_pci_bus, dev->name);
91 : :
92 [ + - + - ]: 15910 : if (dev->bus_info != NULL ||
93 : 7955 : asprintf(&dev->bus_info, "vendor_id=%"PRIx16", device_id=%"PRIx16,
94 : 7955 : dev->id.vendor_id, dev->id.device_id) != -1)
95 : 7955 : dev->device.bus_info = dev->bus_info;
96 : 7955 : }
97 : :
98 : : void
99 : 7955 : pci_free(struct rte_pci_device_internal *pdev)
100 : : {
101 [ + - ]: 7955 : if (pdev == NULL)
102 : : return;
103 : 7955 : free(pdev->device.bus_info);
104 : 7955 : free(pdev);
105 : : }
106 : :
107 : : /* map a particular resource from a file */
108 : : void *
109 : 0 : pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
110 : : int additional_flags)
111 : : {
112 : : void *mapaddr;
113 : :
114 : : /* Map the PCI memory resource of device */
115 : 0 : mapaddr = rte_mem_map(requested_addr, size,
116 : : RTE_PROT_READ | RTE_PROT_WRITE,
117 : : RTE_MAP_SHARED | additional_flags, fd, offset);
118 [ # # ]: 0 : if (mapaddr == NULL) {
119 : 0 : PCI_LOG(ERR, "%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)",
120 : : __func__, fd, requested_addr, size,
121 : : (unsigned long long)offset,
122 : : rte_strerror(rte_errno), mapaddr);
123 : : } else
124 : 0 : PCI_LOG(DEBUG, " PCI memory mapped at %p", mapaddr);
125 : :
126 : 0 : return mapaddr;
127 : : }
128 : :
129 : : /* unmap a particular resource */
130 : : void
131 : 0 : pci_unmap_resource(void *requested_addr, size_t size)
132 : : {
133 [ # # ]: 0 : if (requested_addr == NULL)
134 : : return;
135 : :
136 : : /* Unmap the PCI memory resource of device */
137 [ # # ]: 0 : if (rte_mem_unmap(requested_addr, size)) {
138 : 0 : PCI_LOG(ERR, "%s(): cannot mem unmap(%p, %#zx): %s",
139 : : __func__, requested_addr, size,
140 : : rte_strerror(rte_errno));
141 : : } else
142 : 0 : PCI_LOG(DEBUG, " PCI memory unmapped at %p", requested_addr);
143 : : }
144 : :
145 : : static bool
146 : 756522 : pci_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
147 : : {
148 : : const struct rte_pci_driver *pci_drv = RTE_BUS_DRIVER(drv, *pci_drv);
149 : : const struct rte_pci_device *pci_dev = RTE_BUS_DEVICE(dev, *pci_dev);
150 : : const struct rte_pci_id *id_table;
151 : :
152 [ + + ]: 7707777 : for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
153 : 6951255 : id_table++) {
154 : : /* check if device's identifiers match the driver's ones */
155 [ + + + + ]: 6951438 : if (id_table->vendor_id != pci_dev->id.vendor_id &&
156 : : id_table->vendor_id != RTE_PCI_ANY_ID)
157 : 6815103 : continue;
158 [ + + + + ]: 136335 : if (id_table->device_id != pci_dev->id.device_id &&
159 : : id_table->device_id != RTE_PCI_ANY_ID)
160 : 129015 : continue;
161 : 7320 : if (id_table->subsystem_vendor_id !=
162 [ + - - + ]: 7320 : pci_dev->id.subsystem_vendor_id &&
163 : : id_table->subsystem_vendor_id != RTE_PCI_ANY_ID)
164 : 0 : continue;
165 : 7320 : if (id_table->subsystem_device_id !=
166 [ + - - + ]: 7320 : pci_dev->id.subsystem_device_id &&
167 : : id_table->subsystem_device_id != RTE_PCI_ANY_ID)
168 : 0 : continue;
169 [ + - + + ]: 7320 : if (id_table->class_id != pci_dev->id.class_id &&
170 : : id_table->class_id != RTE_CLASS_ANY_ID)
171 : 7137 : continue;
172 : :
173 : : return true;
174 : : }
175 : :
176 : : return false;
177 : : }
178 : :
179 : : /*
180 : : * If vendor/device ID match, call the probe() function of the
181 : : * driver.
182 : : */
183 : : static int
184 : 183 : pci_probe_device(struct rte_driver *drv, struct rte_device *dev)
185 : : {
186 : : struct rte_pci_device *pci_dev = RTE_BUS_DEVICE(dev, *pci_dev);
187 : : struct rte_pci_driver *pci_drv = RTE_BUS_DRIVER(drv, *pci_drv);
188 : : struct rte_pci_addr *loc = &pci_dev->addr;
189 : : bool already_probed;
190 : : int ret;
191 : :
192 : 183 : PCI_LOG(DEBUG, "PCI device "PCI_PRI_FMT" on NUMA socket %i",
193 : : loc->domain, loc->bus, loc->devid, loc->function,
194 : : pci_dev->device.numa_node);
195 : :
196 : : /* no initialization when marked as blocked, return without error */
197 [ - + ]: 183 : if (pci_dev->device.devargs != NULL &&
198 [ # # ]: 0 : pci_dev->device.devargs->policy == RTE_DEV_BLOCKED) {
199 : 0 : PCI_LOG(INFO, " Device is blocked, not initializing");
200 : 0 : return 1;
201 : : }
202 : :
203 [ + - + - ]: 183 : if (pci_dev->device.numa_node < 0 && rte_socket_count() > 1)
204 : 183 : PCI_LOG(INFO, "Device %s is not NUMA-aware", pci_dev->name);
205 : :
206 : 183 : already_probed = (pci_dev->intr_handle != NULL);
207 [ - + - - ]: 183 : if (already_probed && !(pci_drv->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
208 : 0 : PCI_LOG(DEBUG, "Device %s is already probed", pci_dev->device.name);
209 : 0 : return -EEXIST;
210 : : }
211 : :
212 : 183 : PCI_LOG(DEBUG, " probe driver: %x:%x %s", pci_dev->id.vendor_id,
213 : : pci_dev->id.device_id, pci_drv->driver.name);
214 : :
215 [ + - ]: 183 : if (!already_probed) {
216 : : enum rte_iova_mode dev_iova_mode;
217 : : enum rte_iova_mode iova_mode;
218 : :
219 : 183 : dev_iova_mode = pci_device_iova_mode(pci_drv, pci_dev);
220 : 183 : iova_mode = rte_eal_iova_mode();
221 : 183 : if (dev_iova_mode != RTE_IOVA_DC &&
222 [ - + ]: 183 : dev_iova_mode != iova_mode) {
223 [ # # # # ]: 0 : PCI_LOG(ERR, " Expecting '%s' IOVA mode but current mode is '%s', not initializing",
224 : : dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
225 : : iova_mode == RTE_IOVA_PA ? "PA" : "VA");
226 : 0 : return -EINVAL;
227 : : }
228 : :
229 : : /* Allocate interrupt instance for pci device */
230 : 183 : pci_dev->intr_handle =
231 : 183 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
232 [ - + ]: 183 : if (pci_dev->intr_handle == NULL) {
233 : 0 : PCI_LOG(ERR, "Failed to create interrupt instance for %s",
234 : : pci_dev->device.name);
235 : 0 : return -ENOMEM;
236 : : }
237 : :
238 : 183 : pci_dev->vfio_req_intr_handle =
239 : 183 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
240 [ - + ]: 183 : if (pci_dev->vfio_req_intr_handle == NULL) {
241 : 0 : rte_intr_instance_free(pci_dev->intr_handle);
242 : 0 : pci_dev->intr_handle = NULL;
243 : 0 : PCI_LOG(ERR, "Failed to create vfio req interrupt instance for %s",
244 : : pci_dev->device.name);
245 : 0 : return -ENOMEM;
246 : : }
247 : :
248 [ + - ]: 183 : if (pci_drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
249 : 183 : ret = rte_pci_map_device(pci_dev);
250 [ + - ]: 183 : if (ret != 0) {
251 : 183 : rte_intr_instance_free(pci_dev->vfio_req_intr_handle);
252 : 183 : pci_dev->vfio_req_intr_handle = NULL;
253 : 183 : rte_intr_instance_free(pci_dev->intr_handle);
254 : 183 : pci_dev->intr_handle = NULL;
255 : 183 : return ret;
256 : : }
257 : : }
258 : : }
259 : :
260 : 0 : PCI_LOG(INFO, "Probe PCI driver: %s (%x:%04x) device: "PCI_PRI_FMT" (socket %i)",
261 : : pci_drv->driver.name, pci_dev->id.vendor_id, pci_dev->id.device_id,
262 : : loc->domain, loc->bus, loc->devid, loc->function,
263 : : pci_dev->device.numa_node);
264 : : /* call the driver probe() function */
265 : 0 : ret = pci_drv->probe(pci_drv, pci_dev);
266 [ # # ]: 0 : if (already_probed)
267 : : return ret; /* no rollback if already succeeded earlier */
268 [ # # ]: 0 : if (ret) {
269 [ # # # # ]: 0 : if ((pci_drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
270 : : /* Don't unmap if device is unsupported and
271 : : * driver needs mapped resources.
272 : : */
273 : 0 : !(ret > 0 &&
274 [ # # ]: 0 : (pci_drv->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
275 : 0 : rte_pci_unmap_device(pci_dev);
276 : 0 : rte_intr_instance_free(pci_dev->vfio_req_intr_handle);
277 : 0 : pci_dev->vfio_req_intr_handle = NULL;
278 : 0 : rte_intr_instance_free(pci_dev->intr_handle);
279 : 0 : pci_dev->intr_handle = NULL;
280 : : }
281 : :
282 : : return ret;
283 : : }
284 : :
285 : : static int
286 : 0 : pci_unplug_device(struct rte_device *rte_dev)
287 : : {
288 : : struct rte_pci_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
289 : : struct rte_pci_addr *loc;
290 : 0 : const struct rte_pci_driver *dr = RTE_BUS_DRIVER(dev->device.driver, *dr);
291 : : int ret = 0;
292 : :
293 : : loc = &dev->addr;
294 : :
295 : 0 : PCI_LOG(DEBUG, "PCI device "PCI_PRI_FMT" on NUMA socket %i",
296 : : loc->domain, loc->bus, loc->devid,
297 : : loc->function, dev->device.numa_node);
298 : :
299 : 0 : PCI_LOG(DEBUG, " remove driver: %x:%x %s", dev->id.vendor_id,
300 : : dev->id.device_id, dr->driver.name);
301 : :
302 [ # # ]: 0 : if (dr->remove) {
303 : 0 : ret = dr->remove(dev);
304 [ # # ]: 0 : if (ret < 0)
305 : : return ret;
306 : : }
307 : :
308 [ # # ]: 0 : if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
309 : : /* unmap resources for devices that use igb_uio */
310 : 0 : rte_pci_unmap_device(dev);
311 : :
312 : 0 : rte_intr_instance_free(dev->intr_handle);
313 : 0 : dev->intr_handle = NULL;
314 : 0 : rte_intr_instance_free(dev->vfio_req_intr_handle);
315 : 0 : dev->vfio_req_intr_handle = NULL;
316 : :
317 : 0 : return 0;
318 : : }
319 : :
320 : : static void
321 : 7215 : pci_free_device(struct rte_device *dev)
322 : : {
323 : : struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
324 : 7215 : pci_free(RTE_PCI_DEVICE_INTERNAL(pdev));
325 : 7215 : }
326 : :
327 : : /* dump one device */
328 : : static int
329 : 0 : pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
330 : : {
331 : : int i;
332 : :
333 : 0 : fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
334 : 0 : dev->addr.devid, dev->addr.function);
335 : 0 : fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
336 : 0 : dev->id.device_id);
337 : :
338 [ # # ]: 0 : for (i = 0; i != sizeof(dev->mem_resource) /
339 : 0 : sizeof(dev->mem_resource[0]); i++) {
340 : 0 : fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
341 : : dev->mem_resource[i].phys_addr,
342 : : dev->mem_resource[i].len);
343 : : }
344 : 0 : return 0;
345 : : }
346 : :
347 : : /* dump devices on the bus */
348 : : RTE_EXPORT_SYMBOL(rte_pci_dump)
349 : : void
350 : 0 : rte_pci_dump(FILE *f)
351 : : {
352 : : struct rte_pci_device *dev = NULL;
353 : :
354 [ # # ]: 0 : RTE_BUS_FOREACH_DEV(dev, &rte_pci_bus) {
355 : 0 : pci_dump_one_device(f, dev);
356 : : }
357 : 0 : }
358 : :
359 : : static int
360 : 42 : pci_parse(const char *name, void *addr)
361 : : {
362 : : struct rte_pci_addr *out = addr;
363 : : struct rte_pci_addr pci_addr;
364 : : bool parse;
365 : :
366 : 42 : parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
367 [ - + ]: 42 : if (parse && addr != NULL)
368 : 0 : *out = pci_addr;
369 : 42 : return parse == false;
370 : : }
371 : :
372 : : static int
373 : 594 : pci_dev_compare(const char *name1, const char *name2)
374 : : {
375 : : struct rte_pci_addr addr1, addr2;
376 : :
377 [ + - - + ]: 1188 : if (rte_pci_addr_parse(name1, &addr1) != 0 ||
378 : 594 : rte_pci_addr_parse(name2, &addr2) != 0)
379 : 0 : return 1;
380 : :
381 : 594 : return rte_pci_addr_cmp(&addr1, &addr2);
382 : : }
383 : :
384 : : /* register a driver */
385 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_register)
386 : : void
387 : 31906 : rte_pci_register(struct rte_pci_driver *driver)
388 : : {
389 : 31906 : rte_bus_add_driver(&rte_pci_bus, &driver->driver);
390 : 31906 : }
391 : :
392 : : /* unregister a driver */
393 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_unregister)
394 : : void
395 : 602 : rte_pci_unregister(struct rte_pci_driver *driver)
396 : : {
397 : 602 : rte_bus_remove_driver(&rte_pci_bus, &driver->driver);
398 : 602 : }
399 : :
400 : : /*
401 : : * find the device which encounter the failure, by iterate over all device on
402 : : * PCI bus to check if the memory failure address is located in the range
403 : : * of the BARs of the device.
404 : : */
405 : : static struct rte_pci_device *
406 : 0 : pci_find_device_by_addr(const void *failure_addr)
407 : : {
408 : : struct rte_pci_device *pdev = NULL;
409 : : uint64_t check_point, start, end, len;
410 : : int i;
411 : :
412 : 0 : check_point = (uint64_t)(uintptr_t)failure_addr;
413 : :
414 [ # # ]: 0 : RTE_BUS_FOREACH_DEV(pdev, &rte_pci_bus) {
415 [ # # ]: 0 : for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
416 : 0 : start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
417 : 0 : len = pdev->mem_resource[i].len;
418 : 0 : end = start + len;
419 [ # # ]: 0 : if (check_point >= start && check_point < end) {
420 : 0 : PCI_LOG(DEBUG, "Failure address %16.16"
421 : : PRIx64" belongs to device %s!",
422 : : check_point, pdev->device.name);
423 : 0 : return pdev;
424 : : }
425 : : }
426 : : }
427 : : return NULL;
428 : : }
429 : :
430 : : static int
431 : 0 : pci_hot_unplug_handler(struct rte_device *dev)
432 : : {
433 : : struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
434 : : int ret = 0;
435 : :
436 [ # # # ]: 0 : switch (pdev->kdrv) {
437 : 0 : case RTE_PCI_KDRV_VFIO:
438 : : /*
439 : : * vfio kernel module guaranty the pci device would not be
440 : : * deleted until the user space release the resource, so no
441 : : * need to remap BARs resource here, just directly notify
442 : : * the req event to the user space to handle it.
443 : : */
444 : 0 : rte_dev_event_callback_process(dev->name,
445 : : RTE_DEV_EVENT_REMOVE);
446 : 0 : break;
447 : 0 : case RTE_PCI_KDRV_IGB_UIO:
448 : : case RTE_PCI_KDRV_UIO_GENERIC:
449 : : case RTE_PCI_KDRV_NIC_UIO:
450 : : /* BARs resource is invalid, remap it to be safe. */
451 : 0 : ret = pci_uio_remap_resource(pdev);
452 : 0 : break;
453 : 0 : default:
454 : 0 : PCI_LOG(DEBUG, "Not managed by a supported kernel driver, skipped");
455 : : ret = -1;
456 : 0 : break;
457 : : }
458 : :
459 : 0 : return ret;
460 : : }
461 : :
462 : : static int
463 : 0 : pci_sigbus_handler(const void *failure_addr)
464 : : {
465 : : struct rte_pci_device *pdev = NULL;
466 : : int ret = 0;
467 : :
468 : 0 : pdev = pci_find_device_by_addr(failure_addr);
469 [ # # ]: 0 : if (!pdev) {
470 : : /* It is a generic sigbus error, no bus would handle it. */
471 : : ret = 1;
472 : : } else {
473 : : /* The sigbus error is caused of hot-unplug. */
474 : 0 : ret = pci_hot_unplug_handler(&pdev->device);
475 [ # # ]: 0 : if (ret) {
476 : 0 : PCI_LOG(ERR, "Failed to handle hot-unplug for device %s",
477 : : pdev->name);
478 : : ret = -1;
479 : : }
480 : : }
481 : 0 : return ret;
482 : : }
483 : :
484 : : static int
485 : 0 : pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
486 : : {
487 : : struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
488 : 0 : const struct rte_pci_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
489 : :
490 [ # # ]: 0 : if (pdrv->dma_map != NULL)
491 : 0 : return pdrv->dma_map(pdev, addr, iova, len);
492 : : /**
493 : : * In case driver don't provides any specific mapping
494 : : * try fallback to VFIO.
495 : : */
496 [ # # ]: 0 : if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
497 : 0 : return rte_vfio_container_dma_map
498 : : (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
499 : : iova, len);
500 : 0 : rte_errno = ENOTSUP;
501 : 0 : return -1;
502 : : }
503 : :
504 : : static int
505 : 0 : pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
506 : : {
507 : : struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
508 : 0 : const struct rte_pci_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
509 : :
510 [ # # ]: 0 : if (pdrv->dma_unmap != NULL)
511 : 0 : return pdrv->dma_unmap(pdev, addr, iova, len);
512 : : /**
513 : : * In case driver don't provides any specific mapping
514 : : * try fallback to VFIO.
515 : : */
516 [ # # ]: 0 : if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
517 : 0 : return rte_vfio_container_dma_unmap
518 : : (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
519 : : iova, len);
520 : 0 : rte_errno = ENOTSUP;
521 : 0 : return -1;
522 : : }
523 : :
524 : : enum rte_iova_mode
525 : 225 : rte_pci_get_iommu_class(void)
526 : : {
527 : : enum rte_iova_mode iova_mode = RTE_IOVA_DC;
528 : : const struct rte_pci_device *dev;
529 : : const struct rte_pci_driver *drv;
530 : : bool devices_want_va = false;
531 : : bool devices_want_pa = false;
532 : : int iommu_no_va = -1;
533 : :
534 [ + + ]: 7440 : RTE_BUS_FOREACH_DEV(dev, &rte_pci_bus) {
535 : : /*
536 : : * We can check this only once, because the IOMMU hardware is
537 : : * the same for all of them.
538 : : */
539 [ + + ]: 7215 : if (iommu_no_va == -1)
540 : 186 : iommu_no_va = pci_device_iommu_support_va(dev)
541 : 186 : ? 0 : 1;
542 : :
543 [ + - ]: 7215 : if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
544 : : dev->kdrv == RTE_PCI_KDRV_NONE)
545 : 7215 : continue;
546 [ # # ]: 0 : RTE_BUS_FOREACH_DRV(drv, &rte_pci_bus) {
547 : : enum rte_iova_mode dev_iova_mode;
548 : :
549 [ # # ]: 0 : if (!pci_bus_match(&drv->driver, &dev->device))
550 : 0 : continue;
551 : :
552 : 0 : dev_iova_mode = pci_device_iova_mode(drv, dev);
553 [ # # # # ]: 0 : PCI_LOG(DEBUG, "PCI driver %s for device "PCI_PRI_FMT" wants IOVA as '%s'",
554 : : drv->driver.name,
555 : : dev->addr.domain, dev->addr.bus,
556 : : dev->addr.devid, dev->addr.function,
557 : : dev_iova_mode == RTE_IOVA_DC ? "DC" :
558 : : (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
559 [ # # ]: 0 : if (dev_iova_mode == RTE_IOVA_PA)
560 : : devices_want_pa = true;
561 [ # # ]: 0 : else if (dev_iova_mode == RTE_IOVA_VA)
562 : : devices_want_va = true;
563 : : }
564 : : }
565 [ - + ]: 225 : if (iommu_no_va == 1) {
566 : : iova_mode = RTE_IOVA_PA;
567 [ # # ]: 0 : if (devices_want_va) {
568 : 0 : PCI_LOG(WARNING, "Some devices want 'VA' but IOMMU does not support 'VA'.");
569 : 0 : PCI_LOG(WARNING, "The devices that want 'VA' won't initialize.");
570 : : }
571 [ + - ]: 225 : } else if (devices_want_va && !devices_want_pa) {
572 : : iova_mode = RTE_IOVA_VA;
573 [ + - ]: 225 : } else if (devices_want_pa && !devices_want_va) {
574 : : iova_mode = RTE_IOVA_PA;
575 : : } else {
576 : : iova_mode = RTE_IOVA_DC;
577 [ - + ]: 225 : if (devices_want_va) {
578 : 0 : PCI_LOG(WARNING, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.");
579 : 0 : PCI_LOG(WARNING, "Depending on the final decision by the EAL, not all devices may be able to initialize.");
580 : : }
581 : : }
582 : 225 : return iova_mode;
583 : : }
584 : :
585 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_has_capability_list, 23.11)
586 : : bool
587 : 0 : rte_pci_has_capability_list(const struct rte_pci_device *dev)
588 : : {
589 : : uint16_t status;
590 : :
591 [ # # ]: 0 : if (rte_pci_read_config(dev, &status, sizeof(status), RTE_PCI_STATUS) != sizeof(status))
592 : : return false;
593 : :
594 : 0 : return (status & RTE_PCI_STATUS_CAP_LIST) != 0;
595 : : }
596 : :
597 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_capability, 23.11)
598 : : off_t
599 : 0 : rte_pci_find_capability(const struct rte_pci_device *dev, uint8_t cap)
600 : : {
601 : 0 : return rte_pci_find_next_capability(dev, cap, 0);
602 : : }
603 : :
604 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_next_capability, 23.11)
605 : : off_t
606 : 0 : rte_pci_find_next_capability(const struct rte_pci_device *dev, uint8_t cap,
607 : : off_t offset)
608 : : {
609 : : uint8_t pos;
610 : : int ttl;
611 : :
612 [ # # ]: 0 : if (offset == 0)
613 : : offset = RTE_PCI_CAPABILITY_LIST;
614 : : else
615 : 0 : offset += RTE_PCI_CAP_NEXT;
616 : : ttl = (RTE_PCI_CFG_SPACE_SIZE - RTE_PCI_STD_HEADER_SIZEOF) / RTE_PCI_CAP_SIZEOF;
617 : :
618 [ # # ]: 0 : if (rte_pci_read_config(dev, &pos, sizeof(pos), offset) < 0)
619 : : return -1;
620 : :
621 [ # # # # ]: 0 : while (pos && ttl--) {
622 : : uint16_t ent;
623 : : uint8_t id;
624 : :
625 : 0 : offset = pos;
626 [ # # ]: 0 : if (rte_pci_read_config(dev, &ent, sizeof(ent), offset) < 0)
627 : 0 : return -1;
628 : :
629 : 0 : id = ent & 0xff;
630 [ # # ]: 0 : if (id == 0xff)
631 : : break;
632 : :
633 [ # # ]: 0 : if (id == cap)
634 : 0 : return offset;
635 : :
636 : 0 : pos = (ent >> 8);
637 : : }
638 : :
639 : : return 0;
640 : : }
641 : :
642 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_ext_capability, 20.11)
643 : : off_t
644 : 0 : rte_pci_find_ext_capability(const struct rte_pci_device *dev, uint32_t cap)
645 : : {
646 : : off_t offset = RTE_PCI_CFG_SPACE_SIZE;
647 : : uint32_t header;
648 : : int ttl;
649 : :
650 : : /* minimum 8 bytes per capability */
651 : : ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
652 : :
653 [ # # ]: 0 : if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
654 : 0 : PCI_LOG(ERR, "error in reading extended capabilities");
655 : 0 : return -1;
656 : : }
657 : :
658 : : /*
659 : : * If we have no capabilities, this is indicated by cap ID,
660 : : * cap version and next pointer all being 0.
661 : : */
662 [ # # ]: 0 : if (header == 0)
663 : : return 0;
664 : :
665 [ # # ]: 0 : while (ttl != 0) {
666 [ # # ]: 0 : if (RTE_PCI_EXT_CAP_ID(header) == cap)
667 : 0 : return offset;
668 : :
669 : 0 : offset = RTE_PCI_EXT_CAP_NEXT(header);
670 : :
671 [ # # ]: 0 : if (offset < RTE_PCI_CFG_SPACE_SIZE)
672 : : break;
673 : :
674 [ # # ]: 0 : if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
675 : 0 : PCI_LOG(ERR, "error in reading extended capabilities");
676 : 0 : return -1;
677 : : }
678 : :
679 : 0 : ttl--;
680 : : }
681 : :
682 : : return 0;
683 : : }
684 : :
685 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_set_bus_master, 21.08)
686 : : int
687 : 0 : rte_pci_set_bus_master(const struct rte_pci_device *dev, bool enable)
688 : : {
689 : : uint16_t old_cmd, cmd;
690 : :
691 [ # # ]: 0 : if (rte_pci_read_config(dev, &old_cmd, sizeof(old_cmd),
692 : : RTE_PCI_COMMAND) < 0) {
693 : 0 : PCI_LOG(ERR, "error in reading PCI command register");
694 : 0 : return -1;
695 : : }
696 : :
697 [ # # ]: 0 : if (enable)
698 : 0 : cmd = old_cmd | RTE_PCI_COMMAND_MASTER;
699 : : else
700 : 0 : cmd = old_cmd & ~RTE_PCI_COMMAND_MASTER;
701 : :
702 [ # # ]: 0 : if (cmd == old_cmd)
703 : : return 0;
704 : :
705 [ # # ]: 0 : if (rte_pci_write_config(dev, &cmd, sizeof(cmd),
706 : : RTE_PCI_COMMAND) < 0) {
707 : 0 : PCI_LOG(ERR, "error in writing PCI command register");
708 : 0 : return -1;
709 : : }
710 : :
711 : : return 0;
712 : : }
713 : :
714 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_pasid_set_state)
715 : : int
716 : 0 : rte_pci_pasid_set_state(const struct rte_pci_device *dev,
717 : : off_t offset, bool enable)
718 : : {
719 : 0 : uint16_t pasid = enable;
720 : 0 : return rte_pci_write_config(dev, &pasid, sizeof(pasid),
721 [ # # ]: 0 : offset + RTE_PCI_PASID_CTRL) != sizeof(pasid) ? -1 : 0;
722 : : }
723 : :
724 : : struct rte_bus rte_pci_bus = {
725 : : .allow_multi_probe = true,
726 : : .scan = rte_pci_scan,
727 : : .probe = rte_bus_generic_probe,
728 : : .free_device = pci_free_device,
729 : : .cleanup = rte_bus_generic_cleanup,
730 : : .find_device = rte_bus_generic_find_device,
731 : : .match = pci_bus_match,
732 : : .probe_device = pci_probe_device,
733 : : .unplug_device = pci_unplug_device,
734 : : .parse = pci_parse,
735 : : .dev_compare = pci_dev_compare,
736 : : .devargs_parse = rte_pci_devargs_parse,
737 : : .dma_map = pci_dma_map,
738 : : .dma_unmap = pci_dma_unmap,
739 : : .get_iommu_class = rte_pci_get_iommu_class,
740 : : .dev_iterate = rte_pci_dev_iterate,
741 : : .hot_unplug_handler = pci_hot_unplug_handler,
742 : : .sigbus_handler = pci_sigbus_handler,
743 : : };
744 : :
745 : 301 : RTE_REGISTER_BUS(pci, rte_pci_bus);
746 [ - + ]: 301 : RTE_LOG_REGISTER_DEFAULT(pci_bus_logtype, NOTICE);
|