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 : 8196 : const char *rte_pci_get_sysfs_path(void)
38 : : {
39 : : const char *path = NULL;
40 : :
41 : : #ifdef RTE_EXEC_ENV_LINUX
42 : 8196 : path = getenv("SYSFS_PCI_DEVICES");
43 [ + - ]: 8196 : if (path == NULL)
44 : 8196 : 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 : 15825 : pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
84 : : {
85 : : struct rte_devargs *devargs;
86 : : struct rte_pci_addr addr;
87 : :
88 [ + + ]: 16168 : 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 : 7827 : 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 : 7827 : rte_pci_device_name(&dev->addr,
103 : 7827 : dev->name, sizeof(dev->name));
104 : 7827 : dev->device.name = dev->name;
105 : :
106 : 7827 : devargs = pci_devargs_lookup(&dev->addr);
107 : 7827 : dev->device.devargs = devargs;
108 : :
109 [ + - + - ]: 15654 : if (dev->bus_info != NULL ||
110 : 7827 : asprintf(&dev->bus_info, "vendor_id=%"PRIx16", device_id=%"PRIx16,
111 : 7827 : dev->id.vendor_id, dev->id.device_id) != -1)
112 : 7827 : dev->device.bus_info = dev->bus_info;
113 : 7827 : }
114 : :
115 : : void
116 : 7827 : pci_free(struct rte_pci_device_internal *pdev)
117 : : {
118 [ + - ]: 7827 : if (pdev == NULL)
119 : : return;
120 : 7827 : free(pdev->device.bus_info);
121 : 7827 : 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 : 690400 : 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 [ + + ]: 6959055 : for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
171 : 6268655 : id_table++) {
172 : : /* check if device's identifiers match the driver's ones */
173 [ + + + - ]: 6268832 : if (id_table->vendor_id != pci_dev->id.vendor_id &&
174 : : id_table->vendor_id != RTE_PCI_ANY_ID)
175 : 6147440 : continue;
176 [ + + + - ]: 121392 : if (id_table->device_id != pci_dev->id.device_id &&
177 : : id_table->device_id != RTE_PCI_ANY_ID)
178 : 121215 : continue;
179 : 177 : if (id_table->subsystem_vendor_id !=
180 [ + - - + ]: 177 : pci_dev->id.subsystem_vendor_id &&
181 : : id_table->subsystem_vendor_id != RTE_PCI_ANY_ID)
182 : 0 : continue;
183 : 177 : if (id_table->subsystem_device_id !=
184 [ + - - + ]: 177 : pci_dev->id.subsystem_device_id &&
185 : : id_table->subsystem_device_id != RTE_PCI_ANY_ID)
186 : 0 : continue;
187 [ + - - + ]: 177 : 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 : 690400 : 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 [ + - ]: 690400 : 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 [ + + ]: 690400 : if (!rte_pci_match(dr, dev))
216 : : /* Match of device and driver failed */
217 : : return 1;
218 : :
219 : 177 : 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 [ - + ]: 177 : 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 [ + - + - ]: 177 : if (dev->device.numa_node < 0 && rte_socket_count() > 1)
231 : 177 : PCI_LOG(INFO, "Device %s is not NUMA-aware", dev->name);
232 : :
233 : 177 : already_probed = rte_dev_is_probed(&dev->device);
234 [ - + - - ]: 177 : 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 : 177 : PCI_LOG(DEBUG, " probe driver: %x:%x %s", dev->id.vendor_id,
240 : : dev->id.device_id, dr->driver.name);
241 : :
242 [ + - ]: 177 : if (!already_probed) {
243 : : enum rte_iova_mode dev_iova_mode;
244 : : enum rte_iova_mode iova_mode;
245 : :
246 : 177 : dev_iova_mode = pci_device_iova_mode(dr, dev);
247 : 177 : iova_mode = rte_eal_iova_mode();
248 : 177 : if (dev_iova_mode != RTE_IOVA_DC &&
249 [ - + ]: 177 : 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 : 177 : dev->intr_handle =
258 : 177 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
259 [ - + ]: 177 : 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 : 177 : dev->vfio_req_intr_handle =
266 : 177 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
267 [ - + ]: 177 : 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 : 177 : dev->driver = dr;
281 [ + - ]: 177 : if (dev->driver->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
282 : 177 : ret = rte_pci_map_device(dev);
283 [ + - ]: 177 : if (ret != 0) {
284 : 177 : dev->driver = NULL;
285 : 177 : rte_intr_instance_free(dev->vfio_req_intr_handle);
286 : 177 : dev->vfio_req_intr_handle = NULL;
287 : 177 : rte_intr_instance_free(dev->intr_handle);
288 : 177 : dev->intr_handle = NULL;
289 : 177 : 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 : 6904 : pci_probe_all_drivers(struct rte_pci_device *dev)
375 : : {
376 : : struct rte_pci_driver *dr = NULL;
377 : : int rc = 0;
378 : :
379 [ + - ]: 6904 : if (dev == NULL)
380 : : return -EINVAL;
381 : :
382 [ + + ]: 697304 : FOREACH_DRIVER_ON_PCIBUS(dr) {
383 : 690400 : rc = rte_pci_probe_one_driver(dr, dev);
384 [ - + ]: 690400 : if (rc < 0)
385 : : /* negative value is an error */
386 : 0 : return rc;
387 [ + - ]: 690400 : 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 : 182 : pci_probe(void)
402 : : {
403 : : struct rte_pci_device *dev = NULL;
404 : : size_t probed = 0, failed = 0;
405 : : int ret = 0;
406 : :
407 [ + + ]: 7086 : FOREACH_DEVICE_ON_PCIBUS(dev) {
408 : 6904 : probed++;
409 : :
410 : 6904 : ret = pci_probe_all_drivers(dev);
411 [ - + ]: 6904 : 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 [ + - ]: 182 : return (probed && probed == failed) ? -1 : 0;
424 : : }
425 : :
426 : : static int
427 : 254 : pci_cleanup(void)
428 : : {
429 : : struct rte_pci_device *dev, *tmp_dev;
430 : : int error = 0;
431 : :
432 [ + + ]: 7353 : RTE_TAILQ_FOREACH_SAFE(dev, &rte_pci_bus.device_list, next, tmp_dev) {
433 : 7099 : struct rte_pci_driver *drv = dev->driver;
434 : : int ret = 0;
435 : :
436 [ - - - + ]: 7099 : if (drv == NULL || drv->remove == NULL)
437 : 7099 : 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 : 7099 : free:
448 : : /* free interrupt handles */
449 : 7099 : rte_intr_instance_free(dev->intr_handle);
450 : 7099 : dev->intr_handle = NULL;
451 : 7099 : rte_intr_instance_free(dev->vfio_req_intr_handle);
452 : 7099 : dev->vfio_req_intr_handle = NULL;
453 : :
454 [ + + ]: 7099 : TAILQ_REMOVE(&rte_pci_bus.device_list, dev, next);
455 : 7099 : pci_free(RTE_PCI_DEVICE_INTERNAL(dev));
456 : : }
457 : :
458 : 254 : return error;
459 : : }
460 : :
461 : : /* dump one device */
462 : : static int
463 : 0 : pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
464 : : {
465 : : int i;
466 : :
467 : 0 : fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
468 : 0 : dev->addr.devid, dev->addr.function);
469 : 0 : fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
470 : 0 : dev->id.device_id);
471 : :
472 [ # # ]: 0 : for (i = 0; i != sizeof(dev->mem_resource) /
473 : 0 : sizeof(dev->mem_resource[0]); i++) {
474 : 0 : fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
475 : : dev->mem_resource[i].phys_addr,
476 : : dev->mem_resource[i].len);
477 : : }
478 : 0 : return 0;
479 : : }
480 : :
481 : : /* dump devices on the bus */
482 : : RTE_EXPORT_SYMBOL(rte_pci_dump)
483 : : void
484 : 0 : rte_pci_dump(FILE *f)
485 : : {
486 : : struct rte_pci_device *dev = NULL;
487 : :
488 [ # # ]: 0 : FOREACH_DEVICE_ON_PCIBUS(dev) {
489 : 0 : pci_dump_one_device(f, dev);
490 : : }
491 : 0 : }
492 : :
493 : : static int
494 : 382 : pci_parse(const char *name, void *addr)
495 : : {
496 : : struct rte_pci_addr *out = addr;
497 : : struct rte_pci_addr pci_addr;
498 : : bool parse;
499 : :
500 : 382 : parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
501 [ + + ]: 382 : if (parse && addr != NULL)
502 : 345 : *out = pci_addr;
503 : 382 : return parse == false;
504 : : }
505 : :
506 : : /* register a driver */
507 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_register)
508 : : void
509 : 25400 : rte_pci_register(struct rte_pci_driver *driver)
510 : : {
511 : 25400 : TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
512 : 25400 : }
513 : :
514 : : /* unregister a driver */
515 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_unregister)
516 : : void
517 : 254 : rte_pci_unregister(struct rte_pci_driver *driver)
518 : : {
519 [ + - ]: 254 : TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
520 : 254 : }
521 : :
522 : : /* Add a device to PCI bus */
523 : : void
524 : 183 : rte_pci_add_device(struct rte_pci_device *pci_dev)
525 : : {
526 : 183 : TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
527 : 183 : }
528 : :
529 : : /* Insert a device into a predefined position in PCI bus */
530 : : void
531 : 6916 : rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
532 : : struct rte_pci_device *new_pci_dev)
533 : : {
534 : 6916 : TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
535 : 6916 : }
536 : :
537 : : /* Remove a device from PCI bus */
538 : : static void
539 : : rte_pci_remove_device(struct rte_pci_device *pci_dev)
540 : : {
541 [ # # ]: 0 : TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
542 : : }
543 : :
544 : : static struct rte_device *
545 : 0 : pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
546 : : const void *data)
547 : : {
548 : : const struct rte_pci_device *pstart;
549 : : struct rte_pci_device *pdev;
550 : :
551 [ # # ]: 0 : if (start != NULL) {
552 : 0 : pstart = RTE_DEV_TO_PCI_CONST(start);
553 : 0 : pdev = TAILQ_NEXT(pstart, next);
554 : : } else {
555 : 0 : pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
556 : : }
557 [ # # ]: 0 : while (pdev != NULL) {
558 [ # # ]: 0 : if (cmp(&pdev->device, data) == 0)
559 : 0 : return &pdev->device;
560 : 0 : pdev = TAILQ_NEXT(pdev, next);
561 : : }
562 : : return NULL;
563 : : }
564 : :
565 : : /*
566 : : * find the device which encounter the failure, by iterate over all device on
567 : : * PCI bus to check if the memory failure address is located in the range
568 : : * of the BARs of the device.
569 : : */
570 : : static struct rte_pci_device *
571 : 0 : pci_find_device_by_addr(const void *failure_addr)
572 : : {
573 : : struct rte_pci_device *pdev = NULL;
574 : : uint64_t check_point, start, end, len;
575 : : int i;
576 : :
577 : 0 : check_point = (uint64_t)(uintptr_t)failure_addr;
578 : :
579 [ # # ]: 0 : FOREACH_DEVICE_ON_PCIBUS(pdev) {
580 [ # # ]: 0 : for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
581 : 0 : start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
582 : 0 : len = pdev->mem_resource[i].len;
583 : 0 : end = start + len;
584 [ # # ]: 0 : if (check_point >= start && check_point < end) {
585 : 0 : PCI_LOG(DEBUG, "Failure address %16.16"
586 : : PRIx64" belongs to device %s!",
587 : : check_point, pdev->device.name);
588 : 0 : return pdev;
589 : : }
590 : : }
591 : : }
592 : : return NULL;
593 : : }
594 : :
595 : : static int
596 : 0 : pci_hot_unplug_handler(struct rte_device *dev)
597 : : {
598 : : struct rte_pci_device *pdev = NULL;
599 : : int ret = 0;
600 : :
601 : 0 : pdev = RTE_DEV_TO_PCI(dev);
602 [ # # ]: 0 : if (!pdev)
603 : : return -1;
604 : :
605 [ # # # ]: 0 : switch (pdev->kdrv) {
606 : : #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
607 : 0 : case RTE_PCI_KDRV_VFIO:
608 : : /*
609 : : * vfio kernel module guaranty the pci device would not be
610 : : * deleted until the user space release the resource, so no
611 : : * need to remap BARs resource here, just directly notify
612 : : * the req event to the user space to handle it.
613 : : */
614 : 0 : rte_dev_event_callback_process(dev->name,
615 : : RTE_DEV_EVENT_REMOVE);
616 : 0 : break;
617 : : #endif
618 : 0 : case RTE_PCI_KDRV_IGB_UIO:
619 : : case RTE_PCI_KDRV_UIO_GENERIC:
620 : : case RTE_PCI_KDRV_NIC_UIO:
621 : : /* BARs resource is invalid, remap it to be safe. */
622 : 0 : ret = pci_uio_remap_resource(pdev);
623 : 0 : break;
624 : 0 : default:
625 : 0 : PCI_LOG(DEBUG, "Not managed by a supported kernel driver, skipped");
626 : : ret = -1;
627 : 0 : break;
628 : : }
629 : :
630 : : return ret;
631 : : }
632 : :
633 : : static int
634 : 0 : pci_sigbus_handler(const void *failure_addr)
635 : : {
636 : : struct rte_pci_device *pdev = NULL;
637 : : int ret = 0;
638 : :
639 : 0 : pdev = pci_find_device_by_addr(failure_addr);
640 [ # # ]: 0 : if (!pdev) {
641 : : /* It is a generic sigbus error, no bus would handle it. */
642 : : ret = 1;
643 : : } else {
644 : : /* The sigbus error is caused of hot-unplug. */
645 : 0 : ret = pci_hot_unplug_handler(&pdev->device);
646 [ # # ]: 0 : if (ret) {
647 : 0 : PCI_LOG(ERR, "Failed to handle hot-unplug for device %s",
648 : : pdev->name);
649 : : ret = -1;
650 : : }
651 : : }
652 : 0 : return ret;
653 : : }
654 : :
655 : : static int
656 : 0 : pci_plug(struct rte_device *dev)
657 : : {
658 : 0 : return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
659 : : }
660 : :
661 : : static int
662 : 0 : pci_unplug(struct rte_device *dev)
663 : : {
664 : : struct rte_pci_device *pdev;
665 : : int ret;
666 : :
667 : 0 : pdev = RTE_DEV_TO_PCI(dev);
668 : 0 : ret = rte_pci_detach_dev(pdev);
669 [ # # ]: 0 : if (ret == 0) {
670 : : rte_pci_remove_device(pdev);
671 : 0 : rte_devargs_remove(dev->devargs);
672 : 0 : pci_free(RTE_PCI_DEVICE_INTERNAL(pdev));
673 : : }
674 : 0 : return ret;
675 : : }
676 : :
677 : : static int
678 : 0 : pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
679 : : {
680 : 0 : struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
681 : :
682 [ # # # # ]: 0 : if (!pdev || !pdev->driver) {
683 : 0 : rte_errno = EINVAL;
684 : 0 : return -1;
685 : : }
686 [ # # ]: 0 : if (pdev->driver->dma_map)
687 : 0 : return pdev->driver->dma_map(pdev, addr, iova, len);
688 : : /**
689 : : * In case driver don't provides any specific mapping
690 : : * try fallback to VFIO.
691 : : */
692 [ # # ]: 0 : if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
693 : 0 : return rte_vfio_container_dma_map
694 : : (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
695 : : iova, len);
696 : 0 : rte_errno = ENOTSUP;
697 : 0 : return -1;
698 : : }
699 : :
700 : : static int
701 : 0 : pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
702 : : {
703 : 0 : struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
704 : :
705 [ # # # # ]: 0 : if (!pdev || !pdev->driver) {
706 : 0 : rte_errno = EINVAL;
707 : 0 : return -1;
708 : : }
709 [ # # ]: 0 : if (pdev->driver->dma_unmap)
710 : 0 : return pdev->driver->dma_unmap(pdev, addr, iova, len);
711 : : /**
712 : : * In case driver don't provides any specific mapping
713 : : * try fallback to VFIO.
714 : : */
715 [ # # ]: 0 : if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
716 : 0 : return rte_vfio_container_dma_unmap
717 : : (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
718 : : iova, len);
719 : 0 : rte_errno = ENOTSUP;
720 : 0 : return -1;
721 : : }
722 : :
723 : : bool
724 : 7998 : rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
725 : : {
726 : 7998 : struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
727 : :
728 [ + + - ]: 7998 : switch (rte_pci_bus.bus.conf.scan_mode) {
729 : 172 : case RTE_BUS_SCAN_ALLOWLIST:
730 [ + + + - ]: 172 : if (devargs && devargs->policy == RTE_DEV_ALLOWED)
731 : 1 : return false;
732 : : break;
733 : 7826 : case RTE_BUS_SCAN_UNDEFINED:
734 : : case RTE_BUS_SCAN_BLOCKLIST:
735 [ - + - - ]: 7826 : if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
736 : 7826 : return false;
737 : : break;
738 : : }
739 : : return true;
740 : : }
741 : :
742 : : enum rte_iova_mode
743 : 187 : rte_pci_get_iommu_class(void)
744 : : {
745 : : enum rte_iova_mode iova_mode = RTE_IOVA_DC;
746 : : const struct rte_pci_device *dev;
747 : : const struct rte_pci_driver *drv;
748 : : bool devices_want_va = false;
749 : : bool devices_want_pa = false;
750 : : int iommu_no_va = -1;
751 : :
752 [ + + ]: 7286 : FOREACH_DEVICE_ON_PCIBUS(dev) {
753 : : /*
754 : : * We can check this only once, because the IOMMU hardware is
755 : : * the same for all of them.
756 : : */
757 [ + + ]: 7099 : if (iommu_no_va == -1)
758 : 183 : iommu_no_va = pci_device_iommu_support_va(dev)
759 : 183 : ? 0 : 1;
760 : :
761 [ + - ]: 7099 : if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
762 : : dev->kdrv == RTE_PCI_KDRV_NONE)
763 : 7099 : continue;
764 [ # # ]: 0 : FOREACH_DRIVER_ON_PCIBUS(drv) {
765 : : enum rte_iova_mode dev_iova_mode;
766 : :
767 [ # # ]: 0 : if (!rte_pci_match(drv, dev))
768 : 0 : continue;
769 : :
770 : 0 : dev_iova_mode = pci_device_iova_mode(drv, dev);
771 [ # # # # ]: 0 : PCI_LOG(DEBUG, "PCI driver %s for device "PCI_PRI_FMT" wants IOVA as '%s'",
772 : : drv->driver.name,
773 : : dev->addr.domain, dev->addr.bus,
774 : : dev->addr.devid, dev->addr.function,
775 : : dev_iova_mode == RTE_IOVA_DC ? "DC" :
776 : : (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
777 [ # # ]: 0 : if (dev_iova_mode == RTE_IOVA_PA)
778 : : devices_want_pa = true;
779 [ # # ]: 0 : else if (dev_iova_mode == RTE_IOVA_VA)
780 : : devices_want_va = true;
781 : : }
782 : : }
783 [ - + ]: 187 : if (iommu_no_va == 1) {
784 : : iova_mode = RTE_IOVA_PA;
785 [ # # ]: 0 : if (devices_want_va) {
786 : 0 : PCI_LOG(WARNING, "Some devices want 'VA' but IOMMU does not support 'VA'.");
787 : 0 : PCI_LOG(WARNING, "The devices that want 'VA' won't initialize.");
788 : : }
789 [ + - ]: 187 : } else if (devices_want_va && !devices_want_pa) {
790 : : iova_mode = RTE_IOVA_VA;
791 [ + - ]: 187 : } else if (devices_want_pa && !devices_want_va) {
792 : : iova_mode = RTE_IOVA_PA;
793 : : } else {
794 : : iova_mode = RTE_IOVA_DC;
795 [ - + ]: 187 : if (devices_want_va) {
796 : 0 : PCI_LOG(WARNING, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.");
797 : 0 : PCI_LOG(WARNING, "Depending on the final decision by the EAL, not all devices may be able to initialize.");
798 : : }
799 : : }
800 : 187 : return iova_mode;
801 : : }
802 : :
803 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_has_capability_list, 23.11)
804 : : bool
805 : 0 : rte_pci_has_capability_list(const struct rte_pci_device *dev)
806 : : {
807 : : uint16_t status;
808 : :
809 [ # # ]: 0 : if (rte_pci_read_config(dev, &status, sizeof(status), RTE_PCI_STATUS) != sizeof(status))
810 : : return false;
811 : :
812 : 0 : return (status & RTE_PCI_STATUS_CAP_LIST) != 0;
813 : : }
814 : :
815 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_capability, 23.11)
816 : : off_t
817 : 0 : rte_pci_find_capability(const struct rte_pci_device *dev, uint8_t cap)
818 : : {
819 : 0 : return rte_pci_find_next_capability(dev, cap, 0);
820 : : }
821 : :
822 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_next_capability, 23.11)
823 : : off_t
824 : 0 : rte_pci_find_next_capability(const struct rte_pci_device *dev, uint8_t cap,
825 : : off_t offset)
826 : : {
827 : : uint8_t pos;
828 : : int ttl;
829 : :
830 [ # # ]: 0 : if (offset == 0)
831 : : offset = RTE_PCI_CAPABILITY_LIST;
832 : : else
833 : 0 : offset += RTE_PCI_CAP_NEXT;
834 : : ttl = (RTE_PCI_CFG_SPACE_SIZE - RTE_PCI_STD_HEADER_SIZEOF) / RTE_PCI_CAP_SIZEOF;
835 : :
836 [ # # ]: 0 : if (rte_pci_read_config(dev, &pos, sizeof(pos), offset) < 0)
837 : : return -1;
838 : :
839 [ # # # # ]: 0 : while (pos && ttl--) {
840 : : uint16_t ent;
841 : : uint8_t id;
842 : :
843 : 0 : offset = pos;
844 [ # # ]: 0 : if (rte_pci_read_config(dev, &ent, sizeof(ent), offset) < 0)
845 : 0 : return -1;
846 : :
847 : 0 : id = ent & 0xff;
848 [ # # ]: 0 : if (id == 0xff)
849 : : break;
850 : :
851 [ # # ]: 0 : if (id == cap)
852 : 0 : return offset;
853 : :
854 : 0 : pos = (ent >> 8);
855 : : }
856 : :
857 : : return 0;
858 : : }
859 : :
860 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_ext_capability, 20.11)
861 : : off_t
862 : 0 : rte_pci_find_ext_capability(const struct rte_pci_device *dev, uint32_t cap)
863 : : {
864 : : off_t offset = RTE_PCI_CFG_SPACE_SIZE;
865 : : uint32_t header;
866 : : int ttl;
867 : :
868 : : /* minimum 8 bytes per capability */
869 : : ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
870 : :
871 [ # # ]: 0 : if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
872 : 0 : PCI_LOG(ERR, "error in reading extended capabilities");
873 : 0 : return -1;
874 : : }
875 : :
876 : : /*
877 : : * If we have no capabilities, this is indicated by cap ID,
878 : : * cap version and next pointer all being 0.
879 : : */
880 [ # # ]: 0 : if (header == 0)
881 : : return 0;
882 : :
883 [ # # ]: 0 : while (ttl != 0) {
884 [ # # ]: 0 : if (RTE_PCI_EXT_CAP_ID(header) == cap)
885 : 0 : return offset;
886 : :
887 : 0 : offset = RTE_PCI_EXT_CAP_NEXT(header);
888 : :
889 [ # # ]: 0 : if (offset < RTE_PCI_CFG_SPACE_SIZE)
890 : : break;
891 : :
892 [ # # ]: 0 : if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
893 : 0 : PCI_LOG(ERR, "error in reading extended capabilities");
894 : 0 : return -1;
895 : : }
896 : :
897 : 0 : ttl--;
898 : : }
899 : :
900 : : return 0;
901 : : }
902 : :
903 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_set_bus_master, 21.08)
904 : : int
905 : 0 : rte_pci_set_bus_master(const struct rte_pci_device *dev, bool enable)
906 : : {
907 : : uint16_t old_cmd, cmd;
908 : :
909 [ # # ]: 0 : if (rte_pci_read_config(dev, &old_cmd, sizeof(old_cmd),
910 : : RTE_PCI_COMMAND) < 0) {
911 : 0 : PCI_LOG(ERR, "error in reading PCI command register");
912 : 0 : return -1;
913 : : }
914 : :
915 [ # # ]: 0 : if (enable)
916 : 0 : cmd = old_cmd | RTE_PCI_COMMAND_MASTER;
917 : : else
918 : 0 : cmd = old_cmd & ~RTE_PCI_COMMAND_MASTER;
919 : :
920 [ # # ]: 0 : if (cmd == old_cmd)
921 : : return 0;
922 : :
923 [ # # ]: 0 : if (rte_pci_write_config(dev, &cmd, sizeof(cmd),
924 : : RTE_PCI_COMMAND) < 0) {
925 : 0 : PCI_LOG(ERR, "error in writing PCI command register");
926 : 0 : return -1;
927 : : }
928 : :
929 : : return 0;
930 : : }
931 : :
932 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_pasid_set_state)
933 : : int
934 : 0 : rte_pci_pasid_set_state(const struct rte_pci_device *dev,
935 : : off_t offset, bool enable)
936 : : {
937 : 0 : uint16_t pasid = enable;
938 : 0 : return rte_pci_write_config(dev, &pasid, sizeof(pasid),
939 [ # # ]: 0 : offset + RTE_PCI_PASID_CTRL) != sizeof(pasid) ? -1 : 0;
940 : : }
941 : :
942 : : struct rte_pci_bus rte_pci_bus = {
943 : : .bus = {
944 : : .scan = rte_pci_scan,
945 : : .probe = pci_probe,
946 : : .cleanup = pci_cleanup,
947 : : .find_device = pci_find_device,
948 : : .plug = pci_plug,
949 : : .unplug = pci_unplug,
950 : : .parse = pci_parse,
951 : : .devargs_parse = rte_pci_devargs_parse,
952 : : .dma_map = pci_dma_map,
953 : : .dma_unmap = pci_dma_unmap,
954 : : .get_iommu_class = rte_pci_get_iommu_class,
955 : : .dev_iterate = rte_pci_dev_iterate,
956 : : .hot_unplug_handler = pci_hot_unplug_handler,
957 : : .sigbus_handler = pci_sigbus_handler,
958 : : },
959 : : .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
960 : : .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
961 : : };
962 : :
963 : 254 : RTE_REGISTER_BUS(pci, rte_pci_bus.bus);
964 [ - + ]: 254 : RTE_LOG_REGISTER_DEFAULT(pci_bus_logtype, NOTICE);
|