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