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