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