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