LCOV - code coverage report
Current view: top level - drivers/bus/pci - pci_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 88 256 34.4 %
Date: 2026-08-01 17:54:00 Functions: 13 29 44.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 43 175 24.6 %

           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                 :       8424 : const char *rte_pci_get_sysfs_path(void)
      38                 :            : {
      39                 :            :         const char *path = NULL;
      40                 :            : 
      41                 :            : #ifdef RTE_EXEC_ENV_LINUX
      42                 :       8424 :         path = getenv("SYSFS_PCI_DEVICES");
      43         [ +  - ]:       8424 :         if (path == NULL)
      44                 :       8424 :                 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                 :       8041 : pci_common_set(struct rte_pci_device *dev)
      84                 :            : {
      85                 :            :         /* Each device has its internal, canonical name set. */
      86                 :       8041 :         rte_pci_device_name(&dev->addr,
      87                 :       8041 :                         dev->name, sizeof(dev->name));
      88                 :       8041 :         dev->device.name = dev->name;
      89                 :            : 
      90                 :       8041 :         dev->device.devargs = rte_bus_find_devargs(&rte_pci_bus, dev->name);
      91                 :            : 
      92   [ +  -  +  - ]:      16082 :         if (dev->bus_info != NULL ||
      93                 :       8041 :                         asprintf(&dev->bus_info, "vendor_id=%"PRIx16", device_id=%"PRIx16,
      94                 :       8041 :                                 dev->id.vendor_id, dev->id.device_id) != -1)
      95                 :       8041 :                 dev->device.bus_info = dev->bus_info;
      96                 :       8041 : }
      97                 :            : 
      98                 :            : void
      99                 :       8041 : pci_free(struct rte_pci_device_internal *pdev)
     100                 :            : {
     101         [ +  - ]:       8041 :         if (pdev == NULL)
     102                 :            :                 return;
     103                 :       8041 :         free(pdev->device.bus_info);
     104                 :       8041 :         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                 :            : static bool
     146                 :     764790 : pci_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
     147                 :            : {
     148                 :            :         const struct rte_pci_driver *pci_drv = RTE_BUS_DRIVER(drv, *pci_drv);
     149                 :            :         const struct rte_pci_device *pci_dev = RTE_BUS_DEVICE(dev, *pci_dev);
     150                 :            :         const struct rte_pci_id *id_table;
     151                 :            : 
     152         [ +  + ]:    8318710 :         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
     153                 :    7553920 :              id_table++) {
     154                 :            :                 /* check if device's identifiers match the driver's ones */
     155   [ +  +  +  + ]:    7554105 :                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
     156                 :            :                                 id_table->vendor_id != RTE_PCI_ANY_ID)
     157                 :    7390380 :                         continue;
     158   [ +  +  +  + ]:     163725 :                 if (id_table->device_id != pci_dev->id.device_id &&
     159                 :            :                                 id_table->device_id != RTE_PCI_ANY_ID)
     160                 :     156325 :                         continue;
     161                 :       7400 :                 if (id_table->subsystem_vendor_id !=
     162   [ +  -  -  + ]:       7400 :                     pci_dev->id.subsystem_vendor_id &&
     163                 :            :                     id_table->subsystem_vendor_id != RTE_PCI_ANY_ID)
     164                 :          0 :                         continue;
     165                 :       7400 :                 if (id_table->subsystem_device_id !=
     166   [ +  -  -  + ]:       7400 :                     pci_dev->id.subsystem_device_id &&
     167                 :            :                     id_table->subsystem_device_id != RTE_PCI_ANY_ID)
     168                 :          0 :                         continue;
     169   [ +  -  +  + ]:       7400 :                 if (id_table->class_id != pci_dev->id.class_id &&
     170                 :            :                                 id_table->class_id != RTE_CLASS_ANY_ID)
     171                 :       7215 :                         continue;
     172                 :            : 
     173                 :            :                 return true;
     174                 :            :         }
     175                 :            : 
     176                 :            :         return false;
     177                 :            : }
     178                 :            : 
     179                 :            : /*
     180                 :            :  * If vendor/device ID match, call the probe() function of the
     181                 :            :  * driver.
     182                 :            :  */
     183                 :            : static int
     184                 :        185 : pci_probe_device(struct rte_driver *drv, struct rte_device *dev)
     185                 :            : {
     186                 :            :         struct rte_pci_device *pci_dev = RTE_BUS_DEVICE(dev, *pci_dev);
     187                 :            :         struct rte_pci_driver *pci_drv = RTE_BUS_DRIVER(drv, *pci_drv);
     188                 :            :         struct rte_pci_addr *loc = &pci_dev->addr;
     189                 :            :         bool already_probed;
     190                 :            :         int ret;
     191                 :            : 
     192                 :        185 :         PCI_LOG(DEBUG, "PCI device "PCI_PRI_FMT" on NUMA socket %i",
     193                 :            :                 loc->domain, loc->bus, loc->devid, loc->function,
     194                 :            :                 pci_dev->device.numa_node);
     195                 :            : 
     196   [ +  -  +  - ]:        185 :         if (pci_dev->device.numa_node < 0 && rte_socket_count() > 1)
     197                 :        185 :                 PCI_LOG(INFO, "Device %s is not NUMA-aware", pci_dev->name);
     198                 :            : 
     199                 :        185 :         already_probed = (pci_dev->intr_handle != NULL);
     200   [ -  +  -  - ]:        185 :         if (already_probed && !(pci_drv->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
     201                 :          0 :                 PCI_LOG(DEBUG, "Device %s is already probed", pci_dev->device.name);
     202                 :          0 :                 return -EEXIST;
     203                 :            :         }
     204                 :            : 
     205                 :        185 :         PCI_LOG(DEBUG, "  probe driver: %x:%x %s", pci_dev->id.vendor_id,
     206                 :            :                 pci_dev->id.device_id, pci_drv->driver.name);
     207                 :            : 
     208         [ +  - ]:        185 :         if (!already_probed) {
     209                 :            :                 enum rte_iova_mode dev_iova_mode;
     210                 :            :                 enum rte_iova_mode iova_mode;
     211                 :            : 
     212                 :        185 :                 dev_iova_mode = pci_device_iova_mode(pci_drv, pci_dev);
     213                 :        185 :                 iova_mode = rte_eal_iova_mode();
     214                 :        185 :                 if (dev_iova_mode != RTE_IOVA_DC &&
     215         [ -  + ]:        185 :                     dev_iova_mode != iova_mode) {
     216   [ #  #  #  # ]:          0 :                         PCI_LOG(ERR, "  Expecting '%s' IOVA mode but current mode is '%s', not initializing",
     217                 :            :                                 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
     218                 :            :                                 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
     219                 :          0 :                         return -EINVAL;
     220                 :            :                 }
     221                 :            : 
     222                 :            :                 /* Allocate interrupt instance for pci device */
     223                 :        185 :                 pci_dev->intr_handle =
     224                 :        185 :                         rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     225         [ -  + ]:        185 :                 if (pci_dev->intr_handle == NULL) {
     226                 :          0 :                         PCI_LOG(ERR, "Failed to create interrupt instance for %s",
     227                 :            :                                 pci_dev->device.name);
     228                 :          0 :                         return -ENOMEM;
     229                 :            :                 }
     230                 :            : 
     231                 :        185 :                 pci_dev->vfio_req_intr_handle =
     232                 :        185 :                         rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     233         [ -  + ]:        185 :                 if (pci_dev->vfio_req_intr_handle == NULL) {
     234                 :          0 :                         rte_intr_instance_free(pci_dev->intr_handle);
     235                 :          0 :                         pci_dev->intr_handle = NULL;
     236                 :          0 :                         PCI_LOG(ERR, "Failed to create vfio req interrupt instance for %s",
     237                 :            :                                 pci_dev->device.name);
     238                 :          0 :                         return -ENOMEM;
     239                 :            :                 }
     240                 :            : 
     241         [ +  - ]:        185 :                 if (pci_drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
     242                 :        185 :                         ret = rte_pci_map_device(pci_dev);
     243         [ +  - ]:        185 :                         if (ret != 0) {
     244                 :        185 :                                 rte_intr_instance_free(pci_dev->vfio_req_intr_handle);
     245                 :        185 :                                 pci_dev->vfio_req_intr_handle = NULL;
     246                 :        185 :                                 rte_intr_instance_free(pci_dev->intr_handle);
     247                 :        185 :                                 pci_dev->intr_handle = NULL;
     248                 :        185 :                                 return ret;
     249                 :            :                         }
     250                 :            :                 }
     251                 :            :         }
     252                 :            : 
     253                 :          0 :         PCI_LOG(INFO, "Probe PCI driver: %s (%x:%04x) device: "PCI_PRI_FMT" (socket %i)",
     254                 :            :                 pci_drv->driver.name, pci_dev->id.vendor_id, pci_dev->id.device_id,
     255                 :            :                 loc->domain, loc->bus, loc->devid, loc->function,
     256                 :            :                 pci_dev->device.numa_node);
     257                 :            :         /* call the driver probe() function */
     258                 :          0 :         ret = pci_drv->probe(pci_drv, pci_dev);
     259         [ #  # ]:          0 :         if (already_probed)
     260                 :            :                 return ret; /* no rollback if already succeeded earlier */
     261         [ #  # ]:          0 :         if (ret) {
     262   [ #  #  #  # ]:          0 :                 if ((pci_drv->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
     263                 :            :                         /* Don't unmap if device is unsupported and
     264                 :            :                          * driver needs mapped resources.
     265                 :            :                          */
     266                 :          0 :                         !(ret > 0 &&
     267         [ #  # ]:          0 :                                 (pci_drv->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
     268                 :          0 :                         rte_pci_unmap_device(pci_dev);
     269                 :          0 :                 rte_intr_instance_free(pci_dev->vfio_req_intr_handle);
     270                 :          0 :                 pci_dev->vfio_req_intr_handle = NULL;
     271                 :          0 :                 rte_intr_instance_free(pci_dev->intr_handle);
     272                 :          0 :                 pci_dev->intr_handle = NULL;
     273                 :            :         }
     274                 :            : 
     275                 :            :         return ret;
     276                 :            : }
     277                 :            : 
     278                 :            : static int
     279                 :          0 : pci_unplug_device(struct rte_device *rte_dev)
     280                 :            : {
     281                 :            :         struct rte_pci_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
     282                 :            :         struct rte_pci_addr *loc;
     283                 :          0 :         const struct rte_pci_driver *dr = RTE_BUS_DRIVER(dev->device.driver, *dr);
     284                 :            :         int ret = 0;
     285                 :            : 
     286                 :            :         loc = &dev->addr;
     287                 :            : 
     288                 :          0 :         PCI_LOG(DEBUG, "PCI device "PCI_PRI_FMT" on NUMA socket %i",
     289                 :            :                 loc->domain, loc->bus, loc->devid,
     290                 :            :                 loc->function, dev->device.numa_node);
     291                 :            : 
     292                 :          0 :         PCI_LOG(DEBUG, "  remove driver: %x:%x %s", dev->id.vendor_id,
     293                 :            :                 dev->id.device_id, dr->driver.name);
     294                 :            : 
     295         [ #  # ]:          0 :         if (dr->remove) {
     296                 :          0 :                 ret = dr->remove(dev);
     297         [ #  # ]:          0 :                 if (ret < 0)
     298                 :            :                         return ret;
     299                 :            :         }
     300                 :            : 
     301         [ #  # ]:          0 :         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
     302                 :            :                 /* unmap resources for devices that use igb_uio */
     303                 :          0 :                 rte_pci_unmap_device(dev);
     304                 :            : 
     305                 :          0 :         rte_intr_instance_free(dev->intr_handle);
     306                 :          0 :         dev->intr_handle = NULL;
     307                 :          0 :         rte_intr_instance_free(dev->vfio_req_intr_handle);
     308                 :          0 :         dev->vfio_req_intr_handle = NULL;
     309                 :            : 
     310                 :          0 :         return 0;
     311                 :            : }
     312                 :            : 
     313                 :            : static void
     314                 :       7293 : pci_free_device(struct rte_device *dev)
     315                 :            : {
     316                 :            :         struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
     317                 :       7293 :         pci_free(RTE_PCI_DEVICE_INTERNAL(pdev));
     318                 :       7293 : }
     319                 :            : 
     320                 :            : /* dump one device */
     321                 :            : static int
     322                 :          0 : pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
     323                 :            : {
     324                 :            :         int i;
     325                 :            : 
     326                 :          0 :         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
     327                 :          0 :                dev->addr.devid, dev->addr.function);
     328                 :          0 :         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
     329                 :          0 :                dev->id.device_id);
     330                 :            : 
     331         [ #  # ]:          0 :         for (i = 0; i != sizeof(dev->mem_resource) /
     332                 :          0 :                 sizeof(dev->mem_resource[0]); i++) {
     333                 :          0 :                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
     334                 :            :                         dev->mem_resource[i].phys_addr,
     335                 :            :                         dev->mem_resource[i].len);
     336                 :            :         }
     337                 :          0 :         return 0;
     338                 :            : }
     339                 :            : 
     340                 :            : /* dump devices on the bus */
     341                 :            : RTE_EXPORT_SYMBOL(rte_pci_dump)
     342                 :            : void
     343                 :          0 : rte_pci_dump(FILE *f)
     344                 :            : {
     345                 :            :         struct rte_pci_device *dev = NULL;
     346                 :            : 
     347         [ #  # ]:          0 :         RTE_BUS_FOREACH_DEV(dev, &rte_pci_bus) {
     348                 :          0 :                 pci_dump_one_device(f, dev);
     349                 :            :         }
     350                 :          0 : }
     351                 :            : 
     352                 :            : static int
     353                 :         42 : pci_parse(const char *name, void *addr)
     354                 :            : {
     355                 :            :         struct rte_pci_addr *out = addr;
     356                 :            :         struct rte_pci_addr pci_addr;
     357                 :            :         bool parse;
     358                 :            : 
     359                 :         42 :         parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
     360         [ -  + ]:         42 :         if (parse && addr != NULL)
     361                 :          0 :                 *out = pci_addr;
     362                 :         42 :         return parse == false;
     363                 :            : }
     364                 :            : 
     365                 :            : static int
     366                 :        594 : pci_dev_compare(const char *name1, const char *name2)
     367                 :            : {
     368                 :            :         struct rte_pci_addr addr1, addr2;
     369                 :            : 
     370   [ +  -  -  + ]:       1188 :         if (rte_pci_addr_parse(name1, &addr1) != 0 ||
     371                 :        594 :                         rte_pci_addr_parse(name2, &addr2) != 0)
     372                 :          0 :                 return 1;
     373                 :            : 
     374                 :        594 :         return rte_pci_addr_cmp(&addr1, &addr2);
     375                 :            : }
     376                 :            : 
     377                 :            : /* register a driver */
     378                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_register)
     379                 :            : void
     380                 :      32118 : rte_pci_register(struct rte_pci_driver *driver)
     381                 :            : {
     382                 :      32118 :         rte_bus_add_driver(&rte_pci_bus, &driver->driver);
     383                 :      32118 : }
     384                 :            : 
     385                 :            : /* unregister a driver */
     386                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_unregister)
     387                 :            : void
     388                 :        606 : rte_pci_unregister(struct rte_pci_driver *driver)
     389                 :            : {
     390                 :        606 :         rte_bus_remove_driver(&rte_pci_bus, &driver->driver);
     391                 :        606 : }
     392                 :            : 
     393                 :            : /*
     394                 :            :  * find the device which encounter the failure, by iterate over all device on
     395                 :            :  * PCI bus to check if the memory failure address is located in the range
     396                 :            :  * of the BARs of the device.
     397                 :            :  */
     398                 :            : static struct rte_pci_device *
     399                 :          0 : pci_find_device_by_addr(const void *failure_addr)
     400                 :            : {
     401                 :            :         struct rte_pci_device *pdev = NULL;
     402                 :            :         uint64_t check_point, start, end, len;
     403                 :            :         int i;
     404                 :            : 
     405                 :          0 :         check_point = (uint64_t)(uintptr_t)failure_addr;
     406                 :            : 
     407         [ #  # ]:          0 :         RTE_BUS_FOREACH_DEV(pdev, &rte_pci_bus) {
     408         [ #  # ]:          0 :                 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
     409                 :          0 :                         start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
     410                 :          0 :                         len = pdev->mem_resource[i].len;
     411                 :          0 :                         end = start + len;
     412         [ #  # ]:          0 :                         if (check_point >= start && check_point < end) {
     413                 :          0 :                                 PCI_LOG(DEBUG, "Failure address %16.16"
     414                 :            :                                         PRIx64" belongs to device %s!",
     415                 :            :                                         check_point, pdev->device.name);
     416                 :          0 :                                 return pdev;
     417                 :            :                         }
     418                 :            :                 }
     419                 :            :         }
     420                 :            :         return NULL;
     421                 :            : }
     422                 :            : 
     423                 :            : static int
     424                 :          0 : pci_hot_unplug_handler(struct rte_device *dev)
     425                 :            : {
     426                 :            :         struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
     427                 :            :         int ret = 0;
     428                 :            : 
     429      [ #  #  # ]:          0 :         switch (pdev->kdrv) {
     430                 :          0 :         case RTE_PCI_KDRV_VFIO:
     431                 :            :                 /*
     432                 :            :                  * vfio kernel module guaranty the pci device would not be
     433                 :            :                  * deleted until the user space release the resource, so no
     434                 :            :                  * need to remap BARs resource here, just directly notify
     435                 :            :                  * the req event to the user space to handle it.
     436                 :            :                  */
     437                 :          0 :                 rte_dev_event_callback_process(dev->name,
     438                 :            :                                                RTE_DEV_EVENT_REMOVE);
     439                 :          0 :                 break;
     440                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     441                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     442                 :            :         case RTE_PCI_KDRV_NIC_UIO:
     443                 :            :                 /* BARs resource is invalid, remap it to be safe. */
     444                 :          0 :                 ret = pci_uio_remap_resource(pdev);
     445                 :          0 :                 break;
     446                 :          0 :         default:
     447                 :          0 :                 PCI_LOG(DEBUG, "Not managed by a supported kernel driver, skipped");
     448                 :            :                 ret = -1;
     449                 :          0 :                 break;
     450                 :            :         }
     451                 :            : 
     452                 :          0 :         return ret;
     453                 :            : }
     454                 :            : 
     455                 :            : static int
     456                 :          0 : pci_sigbus_handler(const void *failure_addr)
     457                 :            : {
     458                 :            :         struct rte_pci_device *pdev = NULL;
     459                 :            :         int ret = 0;
     460                 :            : 
     461                 :          0 :         pdev = pci_find_device_by_addr(failure_addr);
     462         [ #  # ]:          0 :         if (!pdev) {
     463                 :            :                 /* It is a generic sigbus error, no bus would handle it. */
     464                 :            :                 ret = 1;
     465                 :            :         } else {
     466                 :            :                 /* The sigbus error is caused of hot-unplug. */
     467                 :          0 :                 ret = pci_hot_unplug_handler(&pdev->device);
     468         [ #  # ]:          0 :                 if (ret) {
     469                 :          0 :                         PCI_LOG(ERR, "Failed to handle hot-unplug for device %s",
     470                 :            :                                 pdev->name);
     471                 :            :                         ret = -1;
     472                 :            :                 }
     473                 :            :         }
     474                 :          0 :         return ret;
     475                 :            : }
     476                 :            : 
     477                 :            : static int
     478                 :          0 : pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
     479                 :            : {
     480                 :            :         struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
     481                 :          0 :         const struct rte_pci_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
     482                 :            : 
     483         [ #  # ]:          0 :         if (pdrv->dma_map != NULL)
     484                 :          0 :                 return pdrv->dma_map(pdev, addr, iova, len);
     485                 :            :         /**
     486                 :            :          *  In case driver don't provides any specific mapping
     487                 :            :          *  try fallback to VFIO.
     488                 :            :          */
     489         [ #  # ]:          0 :         if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
     490                 :          0 :                 return rte_vfio_container_dma_map
     491                 :            :                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
     492                 :            :                                  iova, len);
     493                 :          0 :         rte_errno = ENOTSUP;
     494                 :          0 :         return -1;
     495                 :            : }
     496                 :            : 
     497                 :            : static int
     498                 :          0 : pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
     499                 :            : {
     500                 :            :         struct rte_pci_device *pdev = RTE_BUS_DEVICE(dev, *pdev);
     501                 :          0 :         const struct rte_pci_driver *pdrv = RTE_BUS_DRIVER(dev->driver, *pdrv);
     502                 :            : 
     503         [ #  # ]:          0 :         if (pdrv->dma_unmap != NULL)
     504                 :          0 :                 return pdrv->dma_unmap(pdev, addr, iova, len);
     505                 :            :         /**
     506                 :            :          *  In case driver don't provides any specific mapping
     507                 :            :          *  try fallback to VFIO.
     508                 :            :          */
     509         [ #  # ]:          0 :         if (pdev->kdrv == RTE_PCI_KDRV_VFIO)
     510                 :          0 :                 return rte_vfio_container_dma_unmap
     511                 :            :                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
     512                 :            :                                  iova, len);
     513                 :          0 :         rte_errno = ENOTSUP;
     514                 :          0 :         return -1;
     515                 :            : }
     516                 :            : 
     517                 :            : enum rte_iova_mode
     518                 :        227 : rte_pci_get_iommu_class(void)
     519                 :            : {
     520                 :            :         enum rte_iova_mode iova_mode = RTE_IOVA_DC;
     521                 :            :         const struct rte_pci_device *dev;
     522                 :            :         const struct rte_pci_driver *drv;
     523                 :            :         bool devices_want_va = false;
     524                 :            :         bool devices_want_pa = false;
     525                 :            :         int iommu_no_va = -1;
     526                 :            : 
     527         [ +  + ]:       7520 :         RTE_BUS_FOREACH_DEV(dev, &rte_pci_bus) {
     528                 :            :                 /*
     529                 :            :                  * We can check this only once, because the IOMMU hardware is
     530                 :            :                  * the same for all of them.
     531                 :            :                  */
     532         [ +  + ]:       7293 :                 if (iommu_no_va == -1)
     533                 :        188 :                         iommu_no_va = pci_device_iommu_support_va(dev)
     534                 :        188 :                                         ? 0 : 1;
     535                 :            : 
     536         [ +  - ]:       7293 :                 if (dev->kdrv == RTE_PCI_KDRV_UNKNOWN ||
     537                 :            :                     dev->kdrv == RTE_PCI_KDRV_NONE)
     538                 :       7293 :                         continue;
     539         [ #  # ]:          0 :                 RTE_BUS_FOREACH_DRV(drv, &rte_pci_bus) {
     540                 :            :                         enum rte_iova_mode dev_iova_mode;
     541                 :            : 
     542         [ #  # ]:          0 :                         if (!pci_bus_match(&drv->driver, &dev->device))
     543                 :          0 :                                 continue;
     544                 :            : 
     545                 :          0 :                         dev_iova_mode = pci_device_iova_mode(drv, dev);
     546   [ #  #  #  # ]:          0 :                         PCI_LOG(DEBUG, "PCI driver %s for device "PCI_PRI_FMT" wants IOVA as '%s'",
     547                 :            :                                 drv->driver.name,
     548                 :            :                                 dev->addr.domain, dev->addr.bus,
     549                 :            :                                 dev->addr.devid, dev->addr.function,
     550                 :            :                                 dev_iova_mode == RTE_IOVA_DC ? "DC" :
     551                 :            :                                 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
     552         [ #  # ]:          0 :                         if (dev_iova_mode == RTE_IOVA_PA)
     553                 :            :                                 devices_want_pa = true;
     554         [ #  # ]:          0 :                         else if (dev_iova_mode == RTE_IOVA_VA)
     555                 :            :                                 devices_want_va = true;
     556                 :            :                 }
     557                 :            :         }
     558         [ -  + ]:        227 :         if (iommu_no_va == 1) {
     559                 :            :                 iova_mode = RTE_IOVA_PA;
     560         [ #  # ]:          0 :                 if (devices_want_va) {
     561                 :          0 :                         PCI_LOG(WARNING, "Some devices want 'VA' but IOMMU does not support 'VA'.");
     562                 :          0 :                         PCI_LOG(WARNING, "The devices that want 'VA' won't initialize.");
     563                 :            :                 }
     564         [ +  - ]:        227 :         } else if (devices_want_va && !devices_want_pa) {
     565                 :            :                 iova_mode = RTE_IOVA_VA;
     566         [ +  - ]:        227 :         } else if (devices_want_pa && !devices_want_va) {
     567                 :            :                 iova_mode = RTE_IOVA_PA;
     568                 :            :         } else {
     569                 :            :                 iova_mode = RTE_IOVA_DC;
     570         [ -  + ]:        227 :                 if (devices_want_va) {
     571                 :          0 :                         PCI_LOG(WARNING, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.");
     572                 :          0 :                         PCI_LOG(WARNING, "Depending on the final decision by the EAL, not all devices may be able to initialize.");
     573                 :            :                 }
     574                 :            :         }
     575                 :        227 :         return iova_mode;
     576                 :            : }
     577                 :            : 
     578                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_has_capability_list, 23.11)
     579                 :            : bool
     580                 :          0 : rte_pci_has_capability_list(const struct rte_pci_device *dev)
     581                 :            : {
     582                 :            :         uint16_t status;
     583                 :            : 
     584         [ #  # ]:          0 :         if (rte_pci_read_config(dev, &status, sizeof(status), RTE_PCI_STATUS) != sizeof(status))
     585                 :            :                 return false;
     586                 :            : 
     587                 :          0 :         return (status & RTE_PCI_STATUS_CAP_LIST) != 0;
     588                 :            : }
     589                 :            : 
     590                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_capability, 23.11)
     591                 :            : off_t
     592                 :          0 : rte_pci_find_capability(const struct rte_pci_device *dev, uint8_t cap)
     593                 :            : {
     594                 :          0 :         return rte_pci_find_next_capability(dev, cap, 0);
     595                 :            : }
     596                 :            : 
     597                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_next_capability, 23.11)
     598                 :            : off_t
     599                 :          0 : rte_pci_find_next_capability(const struct rte_pci_device *dev, uint8_t cap,
     600                 :            :         off_t offset)
     601                 :            : {
     602                 :            :         uint8_t pos;
     603                 :            :         int ttl;
     604                 :            : 
     605         [ #  # ]:          0 :         if (offset == 0)
     606                 :            :                 offset = RTE_PCI_CAPABILITY_LIST;
     607                 :            :         else
     608                 :          0 :                 offset += RTE_PCI_CAP_NEXT;
     609                 :            :         ttl = (RTE_PCI_CFG_SPACE_SIZE - RTE_PCI_STD_HEADER_SIZEOF) / RTE_PCI_CAP_SIZEOF;
     610                 :            : 
     611         [ #  # ]:          0 :         if (rte_pci_read_config(dev, &pos, sizeof(pos), offset) < 0)
     612                 :            :                 return -1;
     613                 :            : 
     614   [ #  #  #  # ]:          0 :         while (pos && ttl--) {
     615                 :            :                 uint16_t ent;
     616                 :            :                 uint8_t id;
     617                 :            : 
     618                 :          0 :                 offset = pos;
     619         [ #  # ]:          0 :                 if (rte_pci_read_config(dev, &ent, sizeof(ent), offset) < 0)
     620                 :          0 :                         return -1;
     621                 :            : 
     622                 :          0 :                 id = ent & 0xff;
     623         [ #  # ]:          0 :                 if (id == 0xff)
     624                 :            :                         break;
     625                 :            : 
     626         [ #  # ]:          0 :                 if (id == cap)
     627                 :          0 :                         return offset;
     628                 :            : 
     629                 :          0 :                 pos = (ent >> 8);
     630                 :            :         }
     631                 :            : 
     632                 :            :         return 0;
     633                 :            : }
     634                 :            : 
     635                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_find_ext_capability, 20.11)
     636                 :            : off_t
     637                 :          0 : rte_pci_find_ext_capability(const struct rte_pci_device *dev, uint32_t cap)
     638                 :            : {
     639                 :            :         off_t offset = RTE_PCI_CFG_SPACE_SIZE;
     640                 :            :         uint32_t header;
     641                 :            :         int ttl;
     642                 :            : 
     643                 :            :         /* minimum 8 bytes per capability */
     644                 :            :         ttl = (RTE_PCI_CFG_SPACE_EXP_SIZE - RTE_PCI_CFG_SPACE_SIZE) / 8;
     645                 :            : 
     646         [ #  # ]:          0 :         if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
     647                 :          0 :                 PCI_LOG(ERR, "error in reading extended capabilities");
     648                 :          0 :                 return -1;
     649                 :            :         }
     650                 :            : 
     651                 :            :         /*
     652                 :            :          * If we have no capabilities, this is indicated by cap ID,
     653                 :            :          * cap version and next pointer all being 0.
     654                 :            :          */
     655         [ #  # ]:          0 :         if (header == 0)
     656                 :            :                 return 0;
     657                 :            : 
     658         [ #  # ]:          0 :         while (ttl != 0) {
     659         [ #  # ]:          0 :                 if (RTE_PCI_EXT_CAP_ID(header) == cap)
     660                 :          0 :                         return offset;
     661                 :            : 
     662                 :          0 :                 offset = RTE_PCI_EXT_CAP_NEXT(header);
     663                 :            : 
     664         [ #  # ]:          0 :                 if (offset < RTE_PCI_CFG_SPACE_SIZE)
     665                 :            :                         break;
     666                 :            : 
     667         [ #  # ]:          0 :                 if (rte_pci_read_config(dev, &header, 4, offset) < 0) {
     668                 :          0 :                         PCI_LOG(ERR, "error in reading extended capabilities");
     669                 :          0 :                         return -1;
     670                 :            :                 }
     671                 :            : 
     672                 :          0 :                 ttl--;
     673                 :            :         }
     674                 :            : 
     675                 :            :         return 0;
     676                 :            : }
     677                 :            : 
     678                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_set_bus_master, 21.08)
     679                 :            : int
     680                 :          0 : rte_pci_set_bus_master(const struct rte_pci_device *dev, bool enable)
     681                 :            : {
     682                 :            :         uint16_t old_cmd, cmd;
     683                 :            : 
     684         [ #  # ]:          0 :         if (rte_pci_read_config(dev, &old_cmd, sizeof(old_cmd),
     685                 :            :                                 RTE_PCI_COMMAND) < 0) {
     686                 :          0 :                 PCI_LOG(ERR, "error in reading PCI command register");
     687                 :          0 :                 return -1;
     688                 :            :         }
     689                 :            : 
     690         [ #  # ]:          0 :         if (enable)
     691                 :          0 :                 cmd = old_cmd | RTE_PCI_COMMAND_MASTER;
     692                 :            :         else
     693                 :          0 :                 cmd = old_cmd & ~RTE_PCI_COMMAND_MASTER;
     694                 :            : 
     695         [ #  # ]:          0 :         if (cmd == old_cmd)
     696                 :            :                 return 0;
     697                 :            : 
     698         [ #  # ]:          0 :         if (rte_pci_write_config(dev, &cmd, sizeof(cmd),
     699                 :            :                                  RTE_PCI_COMMAND) < 0) {
     700                 :          0 :                 PCI_LOG(ERR, "error in writing PCI command register");
     701                 :          0 :                 return -1;
     702                 :            :         }
     703                 :            : 
     704                 :            :         return 0;
     705                 :            : }
     706                 :            : 
     707                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_pci_pasid_set_state)
     708                 :            : int
     709                 :          0 : rte_pci_pasid_set_state(const struct rte_pci_device *dev,
     710                 :            :                 off_t offset, bool enable)
     711                 :            : {
     712                 :          0 :         uint16_t pasid = enable;
     713                 :          0 :         return rte_pci_write_config(dev, &pasid, sizeof(pasid),
     714         [ #  # ]:          0 :                         offset + RTE_PCI_PASID_CTRL) != sizeof(pasid) ? -1 : 0;
     715                 :            : }
     716                 :            : 
     717                 :            : struct rte_bus rte_pci_bus = {
     718                 :            :         .allow_multi_probe = true,
     719                 :            :         .scan = rte_pci_scan,
     720                 :            :         .probe = rte_bus_generic_probe,
     721                 :            :         .free_device = pci_free_device,
     722                 :            :         .cleanup = rte_bus_generic_cleanup,
     723                 :            :         .find_device = rte_bus_generic_find_device,
     724                 :            :         .match = pci_bus_match,
     725                 :            :         .probe_device = pci_probe_device,
     726                 :            :         .unplug_device = pci_unplug_device,
     727                 :            :         .parse = pci_parse,
     728                 :            :         .dev_compare = pci_dev_compare,
     729                 :            :         .devargs_parse = rte_pci_devargs_parse,
     730                 :            :         .dma_map = pci_dma_map,
     731                 :            :         .dma_unmap = pci_dma_unmap,
     732                 :            :         .get_iommu_class = rte_pci_get_iommu_class,
     733                 :            :         .dev_iterate = rte_pci_dev_iterate,
     734                 :            :         .hot_unplug_handler = pci_hot_unplug_handler,
     735                 :            :         .sigbus_handler = pci_sigbus_handler,
     736                 :            : };
     737                 :            : 
     738                 :        303 : RTE_REGISTER_BUS(pci, rte_pci_bus);
     739         [ -  + ]:        303 : RTE_LOG_REGISTER_DEFAULT(pci_bus_logtype, NOTICE);

Generated by: LCOV version 1.14