bus/pci: fix selection of default device NUMA node
[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         unsigned socket_id;
170         bool already_probed;
171         struct rte_pci_addr *loc;
172
173         if ((dr == NULL) || (dev == NULL))
174                 return -EINVAL;
175
176         loc = &dev->addr;
177
178         /* The device is not blocked; Check if driver supports it */
179         if (!rte_pci_match(dr, dev))
180                 /* Match of device and driver failed */
181                 return 1;
182
183         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
184                         loc->domain, loc->bus, loc->devid, loc->function,
185                         dev->device.numa_node);
186
187         /* no initialization when marked as blocked, return without error */
188         if (dev->device.devargs != NULL &&
189                 dev->device.devargs->policy == RTE_DEV_BLOCKED) {
190                 RTE_LOG(INFO, EAL, "  Device is blocked, not initializing\n");
191                 return 1;
192         }
193
194         if (dev->device.numa_node < 0) {
195                 if (rte_socket_count() > 1)
196                         RTE_LOG(INFO, EAL, "Device %s is not NUMA-aware, defaulting socket to 0\n",
197                                         dev->name);
198                 socket_id = rte_lcore_to_socket_id(rte_get_next_lcore(-1, 0, 0));
199                 dev->device.numa_node = socket_id;
200         }
201
202         already_probed = rte_dev_is_probed(&dev->device);
203         if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
204                 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
205                                 dev->device.name);
206                 return -EEXIST;
207         }
208
209         RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
210                 dev->id.device_id, dr->driver.name);
211
212         /*
213          * reference driver structure
214          * This needs to be before rte_pci_map_device(), as it enables to use
215          * driver flags for adjusting configuration.
216          */
217         if (!already_probed) {
218                 enum rte_iova_mode dev_iova_mode;
219                 enum rte_iova_mode iova_mode;
220
221                 dev_iova_mode = pci_device_iova_mode(dr, dev);
222                 iova_mode = rte_eal_iova_mode();
223                 if (dev_iova_mode != RTE_IOVA_DC &&
224                     dev_iova_mode != iova_mode) {
225                         RTE_LOG(ERR, EAL, "  Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
226                                 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
227                                 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
228                         return -EINVAL;
229                 }
230
231                 dev->driver = dr;
232         }
233
234         if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
235                 /* map resources for devices that use igb_uio */
236                 ret = rte_pci_map_device(dev);
237                 if (ret != 0) {
238                         dev->driver = NULL;
239                         return ret;
240                 }
241         }
242
243         RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
244                         dr->driver.name, dev->id.vendor_id, dev->id.device_id,
245                         loc->domain, loc->bus, loc->devid, loc->function,
246                         dev->device.numa_node);
247         /* call the driver probe() function */
248         ret = dr->probe(dr, dev);
249         if (already_probed)
250                 return ret; /* no rollback if already succeeded earlier */
251         if (ret) {
252                 dev->driver = NULL;
253                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
254                         /* Don't unmap if device is unsupported and
255                          * driver needs mapped resources.
256                          */
257                         !(ret > 0 &&
258                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
259                         rte_pci_unmap_device(dev);
260         } else {
261                 dev->device.driver = &dr->driver;
262         }
263
264         return ret;
265 }
266
267 /*
268  * If vendor/device ID match, call the remove() function of the
269  * driver.
270  */
271 static int
272 rte_pci_detach_dev(struct rte_pci_device *dev)
273 {
274         struct rte_pci_addr *loc;
275         struct rte_pci_driver *dr;
276         int ret = 0;
277
278         if (dev == NULL)
279                 return -EINVAL;
280
281         dr = dev->driver;
282         loc = &dev->addr;
283
284         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
285                         loc->domain, loc->bus, loc->devid,
286                         loc->function, dev->device.numa_node);
287
288         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
289                         dev->id.device_id, dr->driver.name);
290
291         if (dr->remove) {
292                 ret = dr->remove(dev);
293                 if (ret < 0)
294                         return ret;
295         }
296
297         /* clear driver structure */
298         dev->driver = NULL;
299         dev->device.driver = NULL;
300
301         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
302                 /* unmap resources for devices that use igb_uio */
303                 rte_pci_unmap_device(dev);
304
305         return 0;
306 }
307
308 /*
309  * If vendor/device ID match, call the probe() function of all
310  * registered driver for the given device. Return < 0 if initialization
311  * failed, return 1 if no driver is found for this device.
312  */
313 static int
314 pci_probe_all_drivers(struct rte_pci_device *dev)
315 {
316         struct rte_pci_driver *dr = NULL;
317         int rc = 0;
318
319         if (dev == NULL)
320                 return -EINVAL;
321
322         FOREACH_DRIVER_ON_PCIBUS(dr) {
323                 rc = rte_pci_probe_one_driver(dr, dev);
324                 if (rc < 0)
325                         /* negative value is an error */
326                         return rc;
327                 if (rc > 0)
328                         /* positive value means driver doesn't support it */
329                         continue;
330                 return 0;
331         }
332         return 1;
333 }
334
335 /*
336  * Scan the content of the PCI bus, and call the probe() function for
337  * all registered drivers that have a matching entry in its id_table
338  * for discovered devices.
339  */
340 static int
341 pci_probe(void)
342 {
343         struct rte_pci_device *dev = NULL;
344         size_t probed = 0, failed = 0;
345         int ret = 0;
346
347         FOREACH_DEVICE_ON_PCIBUS(dev) {
348                 probed++;
349
350                 ret = pci_probe_all_drivers(dev);
351                 if (ret < 0) {
352                         if (ret != -EEXIST) {
353                                 RTE_LOG(ERR, EAL, "Requested device "
354                                         PCI_PRI_FMT " cannot be used\n",
355                                         dev->addr.domain, dev->addr.bus,
356                                         dev->addr.devid, dev->addr.function);
357                                 rte_errno = errno;
358                                 failed++;
359                         }
360                         ret = 0;
361                 }
362         }
363
364         return (probed && probed == failed) ? -1 : 0;
365 }
366
367 /* dump one device */
368 static int
369 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
370 {
371         int i;
372
373         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
374                dev->addr.devid, dev->addr.function);
375         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
376                dev->id.device_id);
377
378         for (i = 0; i != sizeof(dev->mem_resource) /
379                 sizeof(dev->mem_resource[0]); i++) {
380                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
381                         dev->mem_resource[i].phys_addr,
382                         dev->mem_resource[i].len);
383         }
384         return 0;
385 }
386
387 /* dump devices on the bus */
388 void
389 rte_pci_dump(FILE *f)
390 {
391         struct rte_pci_device *dev = NULL;
392
393         FOREACH_DEVICE_ON_PCIBUS(dev) {
394                 pci_dump_one_device(f, dev);
395         }
396 }
397
398 static int
399 pci_parse(const char *name, void *addr)
400 {
401         struct rte_pci_addr *out = addr;
402         struct rte_pci_addr pci_addr;
403         bool parse;
404
405         parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
406         if (parse && addr != NULL)
407                 *out = pci_addr;
408         return parse == false;
409 }
410
411 /* register a driver */
412 void
413 rte_pci_register(struct rte_pci_driver *driver)
414 {
415         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
416         driver->bus = &rte_pci_bus;
417 }
418
419 /* unregister a driver */
420 void
421 rte_pci_unregister(struct rte_pci_driver *driver)
422 {
423         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
424         driver->bus = NULL;
425 }
426
427 /* Add a device to PCI bus */
428 void
429 rte_pci_add_device(struct rte_pci_device *pci_dev)
430 {
431         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
432 }
433
434 /* Insert a device into a predefined position in PCI bus */
435 void
436 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
437                       struct rte_pci_device *new_pci_dev)
438 {
439         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
440 }
441
442 /* Remove a device from PCI bus */
443 static void
444 rte_pci_remove_device(struct rte_pci_device *pci_dev)
445 {
446         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
447 }
448
449 static struct rte_device *
450 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
451                 const void *data)
452 {
453         const struct rte_pci_device *pstart;
454         struct rte_pci_device *pdev;
455
456         if (start != NULL) {
457                 pstart = RTE_DEV_TO_PCI_CONST(start);
458                 pdev = TAILQ_NEXT(pstart, next);
459         } else {
460                 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
461         }
462         while (pdev != NULL) {
463                 if (cmp(&pdev->device, data) == 0)
464                         return &pdev->device;
465                 pdev = TAILQ_NEXT(pdev, next);
466         }
467         return NULL;
468 }
469
470 /*
471  * find the device which encounter the failure, by iterate over all device on
472  * PCI bus to check if the memory failure address is located in the range
473  * of the BARs of the device.
474  */
475 static struct rte_pci_device *
476 pci_find_device_by_addr(const void *failure_addr)
477 {
478         struct rte_pci_device *pdev = NULL;
479         uint64_t check_point, start, end, len;
480         int i;
481
482         check_point = (uint64_t)(uintptr_t)failure_addr;
483
484         FOREACH_DEVICE_ON_PCIBUS(pdev) {
485                 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
486                         start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
487                         len = pdev->mem_resource[i].len;
488                         end = start + len;
489                         if (check_point >= start && check_point < end) {
490                                 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
491                                         PRIx64" belongs to device %s!\n",
492                                         check_point, pdev->device.name);
493                                 return pdev;
494                         }
495                 }
496         }
497         return NULL;
498 }
499
500 static int
501 pci_hot_unplug_handler(struct rte_device *dev)
502 {
503         struct rte_pci_device *pdev = NULL;
504         int ret = 0;
505
506         pdev = RTE_DEV_TO_PCI(dev);
507         if (!pdev)
508                 return -1;
509
510         switch (pdev->kdrv) {
511 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
512         case RTE_PCI_KDRV_VFIO:
513                 /*
514                  * vfio kernel module guaranty the pci device would not be
515                  * deleted until the user space release the resource, so no
516                  * need to remap BARs resource here, just directly notify
517                  * the req event to the user space to handle it.
518                  */
519                 rte_dev_event_callback_process(dev->name,
520                                                RTE_DEV_EVENT_REMOVE);
521                 break;
522 #endif
523         case RTE_PCI_KDRV_IGB_UIO:
524         case RTE_PCI_KDRV_UIO_GENERIC:
525         case RTE_PCI_KDRV_NIC_UIO:
526                 /* BARs resource is invalid, remap it to be safe. */
527                 ret = pci_uio_remap_resource(pdev);
528                 break;
529         default:
530                 RTE_LOG(DEBUG, EAL,
531                         "Not managed by a supported kernel driver, skipped\n");
532                 ret = -1;
533                 break;
534         }
535
536         return ret;
537 }
538
539 static int
540 pci_sigbus_handler(const void *failure_addr)
541 {
542         struct rte_pci_device *pdev = NULL;
543         int ret = 0;
544
545         pdev = pci_find_device_by_addr(failure_addr);
546         if (!pdev) {
547                 /* It is a generic sigbus error, no bus would handle it. */
548                 ret = 1;
549         } else {
550                 /* The sigbus error is caused of hot-unplug. */
551                 ret = pci_hot_unplug_handler(&pdev->device);
552                 if (ret) {
553                         RTE_LOG(ERR, EAL,
554                                 "Failed to handle hot-unplug for device %s",
555                                 pdev->name);
556                         ret = -1;
557                 }
558         }
559         return ret;
560 }
561
562 static int
563 pci_plug(struct rte_device *dev)
564 {
565         return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
566 }
567
568 static int
569 pci_unplug(struct rte_device *dev)
570 {
571         struct rte_pci_device *pdev;
572         int ret;
573
574         pdev = RTE_DEV_TO_PCI(dev);
575         ret = rte_pci_detach_dev(pdev);
576         if (ret == 0) {
577                 rte_pci_remove_device(pdev);
578                 rte_devargs_remove(dev->devargs);
579                 free(pdev);
580         }
581         return ret;
582 }
583
584 static int
585 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
586 {
587         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
588
589         if (!pdev || !pdev->driver) {
590                 rte_errno = EINVAL;
591                 return -1;
592         }
593         if (pdev->driver->dma_map)
594                 return pdev->driver->dma_map(pdev, addr, iova, len);
595         /**
596          *  In case driver don't provides any specific mapping
597          *  try fallback to VFIO.
598          */
599         if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
600                 return rte_vfio_container_dma_map
601                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
602                                  iova, len);
603         rte_errno = ENOTSUP;
604         return -1;
605 }
606
607 static int
608 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
609 {
610         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
611
612         if (!pdev || !pdev->driver) {
613                 rte_errno = EINVAL;
614                 return -1;
615         }
616         if (pdev->driver->dma_unmap)
617                 return pdev->driver->dma_unmap(pdev, addr, iova, len);
618         /**
619          *  In case driver don't provides any specific mapping
620          *  try fallback to VFIO.
621          */
622         if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
623                 return rte_vfio_container_dma_unmap
624                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
625                                  iova, len);
626         rte_errno = ENOTSUP;
627         return -1;
628 }
629
630 bool
631 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
632 {
633         struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
634
635         switch (rte_pci_bus.bus.conf.scan_mode) {
636         case RTE_BUS_SCAN_ALLOWLIST:
637                 if (devargs && devargs->policy == RTE_DEV_ALLOWED)
638                         return false;
639                 break;
640         case RTE_BUS_SCAN_UNDEFINED:
641         case RTE_BUS_SCAN_BLOCKLIST:
642                 if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
643                         return false;
644                 break;
645         }
646         return true;
647 }
648
649 enum rte_iova_mode
650 rte_pci_get_iommu_class(void)
651 {
652         enum rte_iova_mode iova_mode = RTE_IOVA_DC;
653         const struct rte_pci_device *dev;
654         const struct rte_pci_driver *drv;
655         bool devices_want_va = false;
656         bool devices_want_pa = false;
657         int iommu_no_va = -1;
658
659         FOREACH_DEVICE_ON_PCIBUS(dev) {
660                 /*
661                  * We can check this only once, because the IOMMU hardware is
662                  * the same for all of them.
663                  */
664                 if (iommu_no_va == -1)
665                         iommu_no_va = pci_device_iommu_support_va(dev)
666                                         ? 0 : 1;
667
668                 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
669                     dev->kdrv == RTE_PCI_KDRV_NONE)
670                         continue;
671                 FOREACH_DRIVER_ON_PCIBUS(drv) {
672                         enum rte_iova_mode dev_iova_mode;
673
674                         if (!rte_pci_match(drv, dev))
675                                 continue;
676
677                         dev_iova_mode = pci_device_iova_mode(drv, dev);
678                         RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
679                                 PCI_PRI_FMT " wants IOVA as '%s'\n",
680                                 drv->driver.name,
681                                 dev->addr.domain, dev->addr.bus,
682                                 dev->addr.devid, dev->addr.function,
683                                 dev_iova_mode == RTE_IOVA_DC ? "DC" :
684                                 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
685                         if (dev_iova_mode == RTE_IOVA_PA)
686                                 devices_want_pa = true;
687                         else if (dev_iova_mode == RTE_IOVA_VA)
688                                 devices_want_va = true;
689                 }
690         }
691         if (iommu_no_va == 1) {
692                 iova_mode = RTE_IOVA_PA;
693                 if (devices_want_va) {
694                         RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
695                         RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
696                 }
697         } else if (devices_want_va && !devices_want_pa) {
698                 iova_mode = RTE_IOVA_VA;
699         } else if (devices_want_pa && !devices_want_va) {
700                 iova_mode = RTE_IOVA_PA;
701         } else {
702                 iova_mode = RTE_IOVA_DC;
703                 if (devices_want_va) {
704                         RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
705                         RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
706                 }
707         }
708         return iova_mode;
709 }
710
711 off_t
712 rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap)
713 {
714         off_t offset = RTE_PCI_CFG_SPACE_SIZE;
715         uint32_t header;
716         int ttl;
717
718         /* minimum 8 bytes per capability */
719         ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
720
721         if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
722                 RTE_LOG(ERR, EAL, "error in reading extended capabilities\n");
723                 return -1;
724         }
725
726         /*
727          * If we have no capabilities, this is indicated by cap ID,
728          * cap version and next pointer all being 0.
729          */
730         if (header == 0)
731                 return 0;
732
733         while (ttl != 0) {
734                 if (RTE_PCI_EXT_CAP_ID(header) == cap)
735                         return offset;
736
737                 offset = RTE_PCI_EXT_CAP_NEXT(header);
738
739                 if (offset < RTE_PCI_CFG_SPACE_SIZE)
740                         break;
741
742                 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
743                         RTE_LOG(ERR, EAL,
744                                 "error in reading extended capabilities\n");
745                         return -1;
746                 }
747
748                 ttl--;
749         }
750
751         return 0;
752 }
753
754 int
755 rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable)
756 {
757         uint16_t old_cmd, cmd;
758
759         if (rte_pci_read_config(dev, &old_cmd, sizeof(old_cmd),
760                                 RTE_PCI_COMMAND) < 0) {
761                 RTE_LOG(ERR, EAL, "error in reading PCI command register\n");
762                 return -1;
763         }
764
765         if (enable)
766                 cmd = old_cmd | RTE_PCI_COMMAND_MASTER;
767         else
768                 cmd = old_cmd & ~RTE_PCI_COMMAND_MASTER;
769
770         if (cmd == old_cmd)
771                 return 0;
772
773         if (rte_pci_write_config(dev, &cmd, sizeof(cmd),
774                                  RTE_PCI_COMMAND) < 0) {
775                 RTE_LOG(ERR, EAL, "error in writing PCI command register\n");
776                 return -1;
777         }
778
779         return 0;
780 }
781
782 struct rte_pci_bus rte_pci_bus = {
783         .bus = {
784                 .scan = rte_pci_scan,
785                 .probe = pci_probe,
786                 .find_device = pci_find_device,
787                 .plug = pci_plug,
788                 .unplug = pci_unplug,
789                 .parse = pci_parse,
790                 .devargs_parse = rte_pci_devargs_parse,
791                 .dma_map = pci_dma_map,
792                 .dma_unmap = pci_dma_unmap,
793                 .get_iommu_class = rte_pci_get_iommu_class,
794                 .dev_iterate = rte_pci_dev_iterate,
795                 .hot_unplug_handler = pci_hot_unplug_handler,
796                 .sigbus_handler = pci_sigbus_handler,
797         },
798         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
799         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
800 };
801
802 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);