LCOV - code coverage report
Current view: top level - drivers/bus/pci - pci_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 88 252 34.9 %
Date: 2026-11-01 19:11:22 Functions: 13 29 44.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 43 171 25.1 %

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

Generated by: LCOV version 1.14