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