LCOV - code coverage report
Current view: top level - drivers/bus/pci/linux - pci.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 118 314 37.6 %
Date: 2025-11-01 17:50:34 Functions: 9 20 45.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 57 159 35.8 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <string.h>
       6                 :            : #include <dirent.h>
       7                 :            : 
       8                 :            : #include <rte_log.h>
       9                 :            : #include <rte_pci.h>
      10                 :            : #include <rte_bus_pci.h>
      11                 :            : #include <rte_malloc.h>
      12                 :            : #include <rte_devargs.h>
      13                 :            : #include <rte_memcpy.h>
      14                 :            : #include <rte_vfio.h>
      15                 :            : 
      16                 :            : #include <eal_export.h>
      17                 :            : #include "eal_filesystem.h"
      18                 :            : 
      19                 :            : #include "private.h"
      20                 :            : #include "pci_init.h"
      21                 :            : 
      22                 :            : /**
      23                 :            :  * @file
      24                 :            :  * PCI probing using Linux sysfs.
      25                 :            :  */
      26                 :            : 
      27                 :            : static int
      28                 :       7784 : pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
      29                 :            :                               size_t len)
      30                 :            : {
      31                 :            :         int count;
      32                 :            :         char path[PATH_MAX];
      33                 :            :         char *name;
      34                 :            : 
      35         [ +  - ]:       7784 :         if (!filename || !dri_name)
      36                 :            :                 return -1;
      37                 :            : 
      38                 :       7784 :         count = readlink(filename, path, PATH_MAX);
      39         [ +  - ]:       7784 :         if (count >= PATH_MAX)
      40                 :            :                 return -1;
      41                 :            : 
      42                 :            :         /* For device does not have a driver */
      43         [ +  + ]:       7784 :         if (count < 0)
      44                 :            :                 return 1;
      45                 :            : 
      46                 :       7060 :         path[count] = '\0';
      47                 :            : 
      48                 :       7060 :         name = strrchr(path, '/');
      49         [ +  - ]:       7060 :         if (name) {
      50                 :       7060 :                 strlcpy(dri_name, name + 1, len);
      51                 :       7060 :                 return 0;
      52                 :            :         }
      53                 :            : 
      54                 :            :         return -1;
      55                 :            : }
      56                 :            : 
      57                 :            : /* Map pci device */
      58                 :            : RTE_EXPORT_SYMBOL(rte_pci_map_device)
      59                 :            : int
      60                 :        176 : rte_pci_map_device(struct rte_pci_device *dev)
      61                 :            : {
      62                 :            :         int ret = -1;
      63                 :            : 
      64                 :            :         /* try mapping the NIC resources using VFIO if it exists */
      65      [ -  -  + ]:        176 :         switch (dev->kdrv) {
      66                 :          0 :         case RTE_PCI_KDRV_VFIO:
      67         [ #  # ]:          0 :                 if (pci_vfio_is_enabled())
      68                 :          0 :                         ret = pci_vfio_map_resource(dev);
      69                 :            :                 break;
      70                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
      71                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
      72         [ #  # ]:          0 :                 if (rte_eal_using_phys_addrs()) {
      73                 :            :                         /* map resources for devices that use uio */
      74                 :          0 :                         ret = pci_uio_map_resource(dev);
      75                 :            :                 }
      76                 :            :                 break;
      77                 :        176 :         default:
      78                 :        176 :                 PCI_LOG(DEBUG, "  Not managed by a supported kernel driver, skipped");
      79                 :            :                 ret = 1;
      80                 :        176 :                 break;
      81                 :            :         }
      82                 :            : 
      83                 :        176 :         return ret;
      84                 :            : }
      85                 :            : 
      86                 :            : /* Unmap pci device */
      87                 :            : RTE_EXPORT_SYMBOL(rte_pci_unmap_device)
      88                 :            : void
      89                 :          0 : rte_pci_unmap_device(struct rte_pci_device *dev)
      90                 :            : {
      91                 :            :         /* try unmapping the NIC resources using VFIO if it exists */
      92      [ #  #  # ]:          0 :         switch (dev->kdrv) {
      93                 :          0 :         case RTE_PCI_KDRV_VFIO:
      94         [ #  # ]:          0 :                 if (pci_vfio_is_enabled())
      95                 :          0 :                         pci_vfio_unmap_resource(dev);
      96                 :            :                 break;
      97                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
      98                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
      99                 :            :                 /* unmap resources for devices that use uio */
     100                 :          0 :                 pci_uio_unmap_resource(dev);
     101                 :          0 :                 break;
     102                 :          0 :         default:
     103                 :          0 :                 PCI_LOG(DEBUG, "  Not managed by a supported kernel driver, skipped");
     104                 :          0 :                 break;
     105                 :            :         }
     106                 :          0 : }
     107                 :            : 
     108                 :            : static int
     109                 :          0 : find_max_end_va(const struct rte_memseg_list *msl, void *arg)
     110                 :            : {
     111                 :          0 :         size_t sz = msl->len;
     112                 :          0 :         void *end_va = RTE_PTR_ADD(msl->base_va, sz);
     113                 :            :         void **max_va = arg;
     114                 :            : 
     115         [ #  # ]:          0 :         if (*max_va < end_va)
     116                 :          0 :                 *max_va = end_va;
     117                 :          0 :         return 0;
     118                 :            : }
     119                 :            : 
     120                 :            : void *
     121                 :          0 : pci_find_max_end_va(void)
     122                 :            : {
     123                 :          0 :         void *va = NULL;
     124                 :            : 
     125                 :          0 :         rte_memseg_list_walk(find_max_end_va, &va);
     126                 :          0 :         return va;
     127                 :            : }
     128                 :            : 
     129                 :            : 
     130                 :            : /* parse one line of the "resource" sysfs file (note that the 'line'
     131                 :            :  * string is modified)
     132                 :            :  */
     133                 :            : int
     134                 :      46704 : pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
     135                 :            :         uint64_t *end_addr, uint64_t *flags)
     136                 :            : {
     137                 :            :         union pci_resource_info {
     138                 :            :                 struct {
     139                 :            :                         char *phys_addr;
     140                 :            :                         char *end_addr;
     141                 :            :                         char *flags;
     142                 :            :                 };
     143                 :            :                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
     144                 :            :         } res_info;
     145                 :            : 
     146         [ -  + ]:      46704 :         if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
     147                 :          0 :                 PCI_LOG(ERR, "%s(): bad resource format", __func__);
     148                 :          0 :                 return -1;
     149                 :            :         }
     150                 :      46704 :         errno = 0;
     151                 :      46704 :         *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
     152                 :      46704 :         *end_addr = strtoull(res_info.end_addr, NULL, 16);
     153                 :      46704 :         *flags = strtoull(res_info.flags, NULL, 16);
     154         [ -  + ]:      46704 :         if (errno != 0) {
     155                 :          0 :                 PCI_LOG(ERR, "%s(): bad resource format", __func__);
     156                 :          0 :                 return -1;
     157                 :            :         }
     158                 :            : 
     159                 :            :         return 0;
     160                 :            : }
     161                 :            : 
     162                 :            : /* parse the "resource" sysfs file */
     163                 :            : static int
     164                 :       7784 : pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
     165                 :            : {
     166                 :            :         FILE *f;
     167                 :            :         char buf[BUFSIZ];
     168                 :            :         int i;
     169                 :            :         uint64_t phys_addr, end_addr, flags;
     170                 :            : 
     171                 :       7784 :         f = fopen(filename, "r");
     172         [ -  + ]:       7784 :         if (f == NULL) {
     173                 :          0 :                 PCI_LOG(ERR, "Cannot open sysfs resource");
     174                 :          0 :                 return -1;
     175                 :            :         }
     176                 :            : 
     177         [ +  + ]:      54488 :         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
     178                 :            : 
     179         [ -  + ]:      46704 :                 if (fgets(buf, sizeof(buf), f) == NULL) {
     180                 :          0 :                         PCI_LOG(ERR, "%s(): cannot read resource", __func__);
     181                 :          0 :                         goto error;
     182                 :            :                 }
     183         [ -  + ]:      46704 :                 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
     184                 :            :                                 &end_addr, &flags) < 0)
     185                 :          0 :                         goto error;
     186                 :            : 
     187         [ +  + ]:      46704 :                 if (flags & IORESOURCE_MEM) {
     188                 :       1629 :                         dev->mem_resource[i].phys_addr = phys_addr;
     189                 :       1629 :                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
     190                 :            :                         /* not mapped for now */
     191                 :       1629 :                         dev->mem_resource[i].addr = NULL;
     192                 :            :                 }
     193                 :            :         }
     194                 :       7784 :         fclose(f);
     195                 :       7784 :         return 0;
     196                 :            : 
     197                 :          0 : error:
     198                 :          0 :         fclose(f);
     199                 :          0 :         return -1;
     200                 :            : }
     201                 :            : 
     202                 :            : /* Scan one pci sysfs entry, and fill the devices list from it. */
     203                 :            : static int
     204                 :       7784 : pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
     205                 :            : {
     206                 :            :         char filename[PATH_MAX];
     207                 :            :         unsigned long tmp;
     208                 :            :         struct rte_pci_device_internal *pdev;
     209                 :            :         struct rte_pci_device *dev;
     210                 :            :         char driver[PATH_MAX];
     211                 :            :         int ret;
     212                 :            : 
     213                 :       7784 :         pdev = malloc(sizeof(*pdev));
     214         [ -  + ]:       7784 :         if (pdev == NULL) {
     215                 :          0 :                 PCI_LOG(ERR, "Cannot allocate memory for internal pci device");
     216                 :          0 :                 return -1;
     217                 :            :         }
     218                 :            : 
     219                 :            :         memset(pdev, 0, sizeof(*pdev));
     220                 :       7784 :         dev = &pdev->device;
     221                 :       7784 :         dev->device.bus = &rte_pci_bus.bus;
     222                 :       7784 :         dev->addr = *addr;
     223                 :            : 
     224                 :            :         /* get vendor id */
     225                 :            :         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
     226         [ -  + ]:       7784 :         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
     227                 :          0 :                 pci_free(pdev);
     228                 :          0 :                 return -1;
     229                 :            :         }
     230                 :       7784 :         dev->id.vendor_id = (uint16_t)tmp;
     231                 :            : 
     232                 :            :         /* get device id */
     233                 :            :         snprintf(filename, sizeof(filename), "%s/device", dirname);
     234         [ -  + ]:       7784 :         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
     235                 :          0 :                 pci_free(pdev);
     236                 :          0 :                 return -1;
     237                 :            :         }
     238                 :       7784 :         dev->id.device_id = (uint16_t)tmp;
     239                 :            : 
     240                 :            :         /* get subsystem_vendor id */
     241                 :            :         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
     242                 :            :                  dirname);
     243         [ -  + ]:       7784 :         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
     244                 :          0 :                 pci_free(pdev);
     245                 :          0 :                 return -1;
     246                 :            :         }
     247                 :       7784 :         dev->id.subsystem_vendor_id = (uint16_t)tmp;
     248                 :            : 
     249                 :            :         /* get subsystem_device id */
     250                 :            :         snprintf(filename, sizeof(filename), "%s/subsystem_device",
     251                 :            :                  dirname);
     252         [ -  + ]:       7784 :         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
     253                 :          0 :                 pci_free(pdev);
     254                 :          0 :                 return -1;
     255                 :            :         }
     256                 :       7784 :         dev->id.subsystem_device_id = (uint16_t)tmp;
     257                 :            : 
     258                 :            :         /* get class_id */
     259                 :            :         snprintf(filename, sizeof(filename), "%s/class",
     260                 :            :                  dirname);
     261         [ -  + ]:       7784 :         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
     262                 :          0 :                 pci_free(pdev);
     263                 :          0 :                 return -1;
     264                 :            :         }
     265                 :            :         /* the least 24 bits are valid: class, subclass, program interface */
     266                 :       7784 :         dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
     267                 :            : 
     268                 :            :         /* get max_vfs */
     269                 :       7784 :         dev->max_vfs = 0;
     270                 :            :         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
     271   [ -  +  -  - ]:       7784 :         if (!access(filename, F_OK) &&
     272                 :          0 :             eal_parse_sysfs_value(filename, &tmp) == 0)
     273                 :          0 :                 dev->max_vfs = (uint16_t)tmp;
     274                 :            :         else {
     275                 :            :                 /* for non igb_uio driver, need kernel version >= 3.8 */
     276                 :            :                 snprintf(filename, sizeof(filename),
     277                 :            :                          "%s/sriov_numvfs", dirname);
     278   [ -  +  -  - ]:       7784 :                 if (!access(filename, F_OK) &&
     279                 :          0 :                     eal_parse_sysfs_value(filename, &tmp) == 0)
     280                 :          0 :                         dev->max_vfs = (uint16_t)tmp;
     281                 :            :         }
     282                 :            : 
     283                 :            :         /* get numa node, default to 0 if not present */
     284                 :            :         snprintf(filename, sizeof(filename), "%s/numa_node", dirname);
     285                 :            : 
     286   [ +  -  +  - ]:      15568 :         if (access(filename, F_OK) == 0 &&
     287                 :       7784 :             eal_parse_sysfs_value(filename, &tmp) == 0)
     288                 :       7784 :                 dev->device.numa_node = tmp;
     289                 :            :         else
     290                 :          0 :                 dev->device.numa_node = SOCKET_ID_ANY;
     291                 :            : 
     292                 :       7784 :         pci_common_set(dev);
     293                 :            : 
     294                 :            :         /* parse resources */
     295                 :            :         snprintf(filename, sizeof(filename), "%s/resource", dirname);
     296         [ -  + ]:       7784 :         if (pci_parse_sysfs_resource(filename, dev) < 0) {
     297                 :          0 :                 PCI_LOG(ERR, "%s(): cannot parse resource", __func__);
     298                 :          0 :                 pci_free(pdev);
     299                 :          0 :                 return -1;
     300                 :            :         }
     301                 :            : 
     302                 :            :         /* parse driver */
     303                 :            :         snprintf(filename, sizeof(filename), "%s/driver", dirname);
     304                 :       7784 :         ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
     305         [ -  + ]:       7784 :         if (ret < 0) {
     306                 :          0 :                 PCI_LOG(ERR, "Fail to get kernel driver");
     307                 :          0 :                 pci_free(pdev);
     308                 :          0 :                 return -1;
     309                 :            :         }
     310                 :            : 
     311         [ +  + ]:       7784 :         if (!ret) {
     312         [ -  + ]:       7060 :                 if (!strcmp(driver, "vfio-pci"))
     313                 :          0 :                         dev->kdrv = RTE_PCI_KDRV_VFIO;
     314         [ -  + ]:       7060 :                 else if (!strcmp(driver, "igb_uio"))
     315                 :          0 :                         dev->kdrv = RTE_PCI_KDRV_IGB_UIO;
     316         [ -  + ]:       7060 :                 else if (!strcmp(driver, "uio_pci_generic"))
     317                 :          0 :                         dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC;
     318                 :            :                 else
     319                 :       7060 :                         dev->kdrv = RTE_PCI_KDRV_UNKNOWN;
     320                 :            :         } else {
     321                 :        724 :                 pci_free(pdev);
     322                 :        724 :                 return 0;
     323                 :            :         }
     324                 :            :         /* device is valid, add in list (sorted) */
     325         [ +  + ]:       7060 :         if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
     326                 :        182 :                 rte_pci_add_device(dev);
     327                 :            :         } else {
     328                 :            :                 struct rte_pci_device *dev2;
     329                 :            :                 int ret;
     330                 :            : 
     331         [ +  - ]:      67151 :                 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
     332                 :      67151 :                         ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
     333         [ +  + ]:      67151 :                         if (ret > 0)
     334                 :            :                                 continue;
     335                 :            : 
     336         [ +  - ]:       6878 :                         if (ret < 0) {
     337                 :       6878 :                                 rte_pci_insert_device(dev2, dev);
     338                 :            :                         } else { /* already registered */
     339         [ #  # ]:          0 :                                 if (!rte_dev_is_probed(&dev2->device)) {
     340                 :          0 :                                         dev2->kdrv = dev->kdrv;
     341                 :          0 :                                         dev2->max_vfs = dev->max_vfs;
     342                 :          0 :                                         dev2->id = dev->id;
     343                 :          0 :                                         pci_common_set(dev2);
     344                 :          0 :                                         memmove(dev2->mem_resource,
     345                 :          0 :                                                 dev->mem_resource,
     346                 :            :                                                 sizeof(dev->mem_resource));
     347                 :            :                                 } else {
     348                 :            :                                         /**
     349                 :            :                                          * If device is plugged and driver is
     350                 :            :                                          * probed already, (This happens when
     351                 :            :                                          * we call rte_dev_probe which will
     352                 :            :                                          * scan all device on the bus) we don't
     353                 :            :                                          * need to do anything here unless...
     354                 :            :                                          **/
     355         [ #  # ]:          0 :                                         if (dev2->kdrv != dev->kdrv ||
     356                 :          0 :                                                 dev2->max_vfs != dev->max_vfs ||
     357         [ #  # ]:          0 :                                                 memcmp(&dev2->id, &dev->id, sizeof(dev2->id)))
     358                 :            :                                                 /*
     359                 :            :                                                  * This should not happens.
     360                 :            :                                                  * But it is still possible if
     361                 :            :                                                  * we unbind a device from
     362                 :            :                                                  * vfio or uio before hotplug
     363                 :            :                                                  * remove and rebind it with
     364                 :            :                                                  * a different configure.
     365                 :            :                                                  * So we just print out the
     366                 :            :                                                  * error as an alarm.
     367                 :            :                                                  */
     368                 :          0 :                                                 PCI_LOG(ERR, "Unexpected device scan at %s!",
     369                 :            :                                                         filename);
     370                 :          0 :                                         else if (dev2->device.devargs !=
     371         [ #  # ]:          0 :                                                  dev->device.devargs) {
     372                 :          0 :                                                 rte_devargs_remove(dev2->device.devargs);
     373                 :          0 :                                                 pci_common_set(dev2);
     374                 :            :                                         }
     375                 :            :                                 }
     376                 :          0 :                                 pci_free(pdev);
     377                 :            :                         }
     378                 :       6878 :                         return 0;
     379                 :            :                 }
     380                 :            : 
     381                 :          0 :                 rte_pci_add_device(dev);
     382                 :            :         }
     383                 :            : 
     384                 :            :         return 0;
     385                 :            : }
     386                 :            : 
     387                 :            : /*
     388                 :            :  * split up a pci address into its constituent parts.
     389                 :            :  */
     390                 :            : static int
     391                 :       7955 : parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
     392                 :            : {
     393                 :            :         /* first split on ':' */
     394                 :            :         union splitaddr {
     395                 :            :                 struct {
     396                 :            :                         char *domain;
     397                 :            :                         char *bus;
     398                 :            :                         char *devid;
     399                 :            :                         char *function;
     400                 :            :                 };
     401                 :            :                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
     402                 :            :         } splitaddr;
     403                 :            : 
     404                 :       7955 :         char *buf_copy = strndup(buf, bufsize);
     405         [ +  - ]:       7955 :         if (buf_copy == NULL)
     406                 :            :                 return -1;
     407                 :            : 
     408         [ -  + ]:       7955 :         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
     409                 :            :                         != PCI_FMT_NVAL - 1)
     410                 :          0 :                 goto error;
     411                 :            :         /* final split is on '.' between devid and function */
     412                 :       7955 :         splitaddr.function = strchr(splitaddr.devid,'.');
     413         [ -  + ]:       7955 :         if (splitaddr.function == NULL)
     414                 :          0 :                 goto error;
     415                 :       7955 :         *splitaddr.function++ = '\0';
     416                 :            : 
     417                 :            :         /* now convert to int values */
     418                 :       7955 :         errno = 0;
     419                 :       7955 :         addr->domain = strtoul(splitaddr.domain, NULL, 16);
     420                 :       7955 :         addr->bus = strtoul(splitaddr.bus, NULL, 16);
     421                 :       7955 :         addr->devid = strtoul(splitaddr.devid, NULL, 16);
     422                 :       7955 :         addr->function = strtoul(splitaddr.function, NULL, 10);
     423         [ -  + ]:       7955 :         if (errno != 0)
     424                 :          0 :                 goto error;
     425                 :            : 
     426                 :       7955 :         free(buf_copy); /* free the copy made with strdup */
     427                 :       7955 :         return 0;
     428                 :          0 : error:
     429                 :          0 :         free(buf_copy);
     430                 :          0 :         return -1;
     431                 :            : }
     432                 :            : 
     433                 :            : /*
     434                 :            :  * Scan the content of the PCI bus, and the devices in the devices
     435                 :            :  * list
     436                 :            :  */
     437                 :            : int
     438                 :        186 : rte_pci_scan(void)
     439                 :            : {
     440                 :            :         struct dirent *e;
     441                 :            :         DIR *dir;
     442                 :            :         char dirname[PATH_MAX];
     443                 :            :         struct rte_pci_addr addr;
     444                 :            : 
     445                 :            :         /* for debug purposes, PCI can be disabled */
     446         [ +  + ]:        186 :         if (!rte_eal_has_pci())
     447                 :            :                 return 0;
     448                 :            : 
     449                 :        185 :         dir = opendir(rte_pci_get_sysfs_path());
     450         [ -  + ]:        185 :         if (dir == NULL) {
     451                 :          0 :                 PCI_LOG(ERR, "%s(): opendir failed: %s", __func__, strerror(errno));
     452                 :          0 :                 return -1;
     453                 :            :         }
     454                 :            : 
     455         [ +  + ]:       8510 :         while ((e = readdir(dir)) != NULL) {
     456         [ +  + ]:       8325 :                 if (e->d_name[0] == '.')
     457                 :        370 :                         continue;
     458                 :            : 
     459         [ -  + ]:       7955 :                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
     460                 :          0 :                         continue;
     461                 :            : 
     462         [ +  + ]:       7955 :                 if (rte_pci_ignore_device(&addr))
     463                 :        171 :                         continue;
     464                 :            : 
     465                 :       7784 :                 snprintf(dirname, sizeof(dirname), "%s/%s",
     466                 :            :                                 rte_pci_get_sysfs_path(), e->d_name);
     467                 :            : 
     468         [ -  + ]:       7784 :                 if (pci_scan_one(dirname, &addr) < 0)
     469                 :          0 :                         goto error;
     470                 :            :         }
     471                 :        185 :         closedir(dir);
     472                 :        185 :         return 0;
     473                 :            : 
     474                 :            : error:
     475                 :          0 :         closedir(dir);
     476                 :          0 :         return -1;
     477                 :            : }
     478                 :            : 
     479                 :            : #if defined(RTE_ARCH_X86)
     480                 :            : bool
     481                 :        182 : pci_device_iommu_support_va(const struct rte_pci_device *dev)
     482                 :            : {
     483                 :            : #define VTD_CAP_MGAW_SHIFT      16
     484                 :            : #define VTD_CAP_MGAW_MASK       (0x3fULL << VTD_CAP_MGAW_SHIFT)
     485                 :            :         const struct rte_pci_addr *addr = &dev->addr;
     486                 :            :         char filename[PATH_MAX];
     487                 :            :         FILE *fp;
     488                 :        182 :         uint64_t mgaw, vtd_cap_reg = 0;
     489                 :            : 
     490                 :        182 :         snprintf(filename, sizeof(filename),
     491                 :            :                  "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
     492                 :        182 :                  rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
     493                 :        182 :                  addr->function);
     494                 :            : 
     495                 :        182 :         fp = fopen(filename, "r");
     496         [ +  - ]:        182 :         if (fp == NULL) {
     497                 :            :                 /* We don't have an Intel IOMMU, assume VA supported */
     498         [ -  + ]:        182 :                 if (errno == ENOENT)
     499                 :            :                         return true;
     500                 :            : 
     501                 :          0 :                 PCI_LOG(ERR, "%s(): can't open %s: %s",
     502                 :            :                         __func__, filename, strerror(errno));
     503                 :          0 :                 return false;
     504                 :            :         }
     505                 :            : 
     506                 :            :         /* We have an Intel IOMMU */
     507         [ #  # ]:          0 :         if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
     508                 :          0 :                 PCI_LOG(ERR, "%s(): can't read %s", __func__, filename);
     509                 :          0 :                 fclose(fp);
     510                 :          0 :                 return false;
     511                 :            :         }
     512                 :            : 
     513                 :          0 :         fclose(fp);
     514                 :            : 
     515                 :          0 :         mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
     516                 :            : 
     517                 :            :         /*
     518                 :            :          * Assuming there is no limitation by now. We can not know at this point
     519                 :            :          * because the memory has not been initialized yet. Setting the dma mask
     520                 :            :          * will force a check once memory initialization is done. We can not do
     521                 :            :          * a fallback to IOVA PA now, but if the dma check fails, the error
     522                 :            :          * message should advice for using '--iova-mode pa' if IOVA VA is the
     523                 :            :          * current mode.
     524                 :            :          */
     525                 :          0 :         rte_mem_set_dma_mask(mgaw);
     526                 :          0 :         return true;
     527                 :            : }
     528                 :            : #elif defined(RTE_ARCH_PPC_64)
     529                 :            : bool
     530                 :            : pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
     531                 :            : {
     532                 :            :         /*
     533                 :            :          * All POWER systems support an IOMMU, but only IOMMUv2 supports
     534                 :            :          * IOVA = VA in DPDK. Check contents of /proc/cpuinfo to find the
     535                 :            :          * system.
     536                 :            :          *
     537                 :            :          * Platform | Model |  IOMMU  | VA? |             Comment
     538                 :            :          * ---------+-------+---------+-----+---------------------------------
     539                 :            :          *  PowerNV |  N/A  | IOMMUv2 | Yes | OpenPOWER (Bare Metal)
     540                 :            :          *  pSeries | ~qemu | IOMMUv2 | Yes | PowerVM Logical Partition (LPAR)
     541                 :            :          *  pSeries |  qemu | IOMMUv1 |  No | QEMU Virtual Machine
     542                 :            :          */
     543                 :            : 
     544                 :            :         char *line = NULL;
     545                 :            :         size_t len = 0;
     546                 :            :         char filename[PATH_MAX] = "/proc/cpuinfo";
     547                 :            :         FILE *fp = fopen(filename, "r");
     548                 :            :         bool pseries = false, powernv = false, qemu = false;
     549                 :            :         bool ret = false;
     550                 :            : 
     551                 :            :         if (fp == NULL) {
     552                 :            :                 PCI_LOG(ERR, "%s(): can't open %s: %s",
     553                 :            :                         __func__, filename, strerror(errno));
     554                 :            :                 return ret;
     555                 :            :         }
     556                 :            : 
     557                 :            :         /* Check the "platform" and "model" fields */
     558                 :            :         while (getline(&line, &len, fp) != -1) {
     559                 :            :                 if (strstr(line, "platform") != NULL) {
     560                 :            :                         if (strstr(line, "PowerNV") != NULL) {
     561                 :            :                                 PCI_LOG(DEBUG, "Running on a PowerNV platform");
     562                 :            :                                 powernv = true;
     563                 :            :                         } else if (strstr(line, "pSeries") != NULL) {
     564                 :            :                                 PCI_LOG(DEBUG, "Running on a pSeries platform");
     565                 :            :                                 pseries = true;
     566                 :            :                         }
     567                 :            :                 } else if (strstr(line, "model") != NULL) {
     568                 :            :                         if (strstr(line, "qemu") != NULL) {
     569                 :            :                                 PCI_LOG(DEBUG, "Found qemu emulation");
     570                 :            :                                 qemu = true;
     571                 :            :                         }
     572                 :            :                 }
     573                 :            :         }
     574                 :            : 
     575                 :            :         free(line);
     576                 :            :         fclose(fp);
     577                 :            : 
     578                 :            :         if (powernv || (pseries && !qemu))
     579                 :            :                 ret = true;
     580                 :            :         return ret;
     581                 :            : }
     582                 :            : #else
     583                 :            : bool
     584                 :            : pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
     585                 :            : {
     586                 :            :         return true;
     587                 :            : }
     588                 :            : #endif
     589                 :            : 
     590                 :            : enum rte_iova_mode
     591                 :        176 : pci_device_iova_mode(const struct rte_pci_driver *pdrv,
     592                 :            :                      const struct rte_pci_device *pdev)
     593                 :            : {
     594                 :            :         enum rte_iova_mode iova_mode = RTE_IOVA_DC;
     595                 :            : 
     596      [ -  +  - ]:        176 :         switch (pdev->kdrv) {
     597                 :          0 :         case RTE_PCI_KDRV_VFIO: {
     598                 :            :                 static int is_vfio_noiommu_enabled = -1;
     599                 :            : 
     600         [ #  # ]:          0 :                 if (is_vfio_noiommu_enabled == -1) {
     601         [ #  # ]:          0 :                         if (rte_vfio_noiommu_is_enabled() == 1)
     602                 :          0 :                                 is_vfio_noiommu_enabled = 1;
     603                 :            :                         else
     604                 :          0 :                                 is_vfio_noiommu_enabled = 0;
     605                 :            :                 }
     606         [ #  # ]:          0 :                 if (is_vfio_noiommu_enabled != 0)
     607                 :            :                         iova_mode = RTE_IOVA_PA;
     608         [ #  # ]:          0 :                 else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
     609                 :            :                         iova_mode = RTE_IOVA_VA;
     610                 :            :                 break;
     611                 :            :         }
     612                 :            : 
     613                 :            :         case RTE_PCI_KDRV_IGB_UIO:
     614                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     615                 :            :                 iova_mode = RTE_IOVA_PA;
     616                 :            :                 break;
     617                 :            : 
     618                 :        176 :         default:
     619         [ -  + ]:        176 :                 if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
     620                 :            :                         iova_mode = RTE_IOVA_VA;
     621                 :            :                 break;
     622                 :            :         }
     623                 :        176 :         return iova_mode;
     624                 :            : }
     625                 :            : 
     626                 :            : /* Read PCI config space. */
     627                 :            : RTE_EXPORT_SYMBOL(rte_pci_read_config)
     628                 :          0 : int rte_pci_read_config(const struct rte_pci_device *device,
     629                 :            :                 void *buf, size_t len, off_t offset)
     630                 :            : {
     631                 :          0 :         char devname[RTE_DEV_NAME_MAX_LEN] = "";
     632                 :          0 :         const struct rte_intr_handle *intr_handle = device->intr_handle;
     633                 :            : 
     634      [ #  #  # ]:          0 :         switch (device->kdrv) {
     635                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     636                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     637                 :          0 :                 return pci_uio_read_config(intr_handle, buf, len, offset);
     638                 :          0 :         case RTE_PCI_KDRV_VFIO:
     639                 :          0 :                 return pci_vfio_read_config(device, buf, len, offset);
     640                 :          0 :         default:
     641                 :          0 :                 rte_pci_device_name(&device->addr, devname,
     642                 :            :                                     RTE_DEV_NAME_MAX_LEN);
     643                 :          0 :                 PCI_LOG(ERR, "Unknown driver type for %s", devname);
     644                 :          0 :                 return -1;
     645                 :            :         }
     646                 :            : }
     647                 :            : 
     648                 :            : /* Write PCI config space. */
     649                 :            : RTE_EXPORT_SYMBOL(rte_pci_write_config)
     650                 :          0 : int rte_pci_write_config(const struct rte_pci_device *device,
     651                 :            :                 const void *buf, size_t len, off_t offset)
     652                 :            : {
     653                 :          0 :         char devname[RTE_DEV_NAME_MAX_LEN] = "";
     654                 :          0 :         const struct rte_intr_handle *intr_handle = device->intr_handle;
     655                 :            : 
     656      [ #  #  # ]:          0 :         switch (device->kdrv) {
     657                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     658                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     659                 :          0 :                 return pci_uio_write_config(intr_handle, buf, len, offset);
     660                 :          0 :         case RTE_PCI_KDRV_VFIO:
     661                 :          0 :                 return pci_vfio_write_config(device, buf, len, offset);
     662                 :          0 :         default:
     663                 :          0 :                 rte_pci_device_name(&device->addr, devname,
     664                 :            :                                     RTE_DEV_NAME_MAX_LEN);
     665                 :          0 :                 PCI_LOG(ERR, "Unknown driver type for %s", devname);
     666                 :          0 :                 return -1;
     667                 :            :         }
     668                 :            : }
     669                 :            : 
     670                 :            : /* Read PCI MMIO space. */
     671                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_mmio_read, 23.07)
     672                 :          0 : int rte_pci_mmio_read(const struct rte_pci_device *device, int bar,
     673                 :            :                 void *buf, size_t len, off_t offset)
     674                 :            : {
     675                 :          0 :         char devname[RTE_DEV_NAME_MAX_LEN] = "";
     676                 :            : 
     677      [ #  #  # ]:          0 :         switch (device->kdrv) {
     678                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     679                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     680                 :          0 :                 return pci_uio_mmio_read(device, bar, buf, len, offset);
     681                 :          0 :         case RTE_PCI_KDRV_VFIO:
     682                 :          0 :                 return pci_vfio_mmio_read(device, bar, buf, len, offset);
     683                 :          0 :         default:
     684                 :          0 :                 rte_pci_device_name(&device->addr, devname,
     685                 :            :                                     RTE_DEV_NAME_MAX_LEN);
     686                 :          0 :                 PCI_LOG(ERR, "Unknown driver type for %s", devname);
     687                 :          0 :                 return -1;
     688                 :            :         }
     689                 :            : }
     690                 :            : 
     691                 :            : /* Write PCI MMIO space. */
     692                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_mmio_write, 23.07)
     693                 :          0 : int rte_pci_mmio_write(const struct rte_pci_device *device, int bar,
     694                 :            :                 const void *buf, size_t len, off_t offset)
     695                 :            : {
     696                 :          0 :         char devname[RTE_DEV_NAME_MAX_LEN] = "";
     697                 :            : 
     698      [ #  #  # ]:          0 :         switch (device->kdrv) {
     699                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     700                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     701                 :          0 :                 return pci_uio_mmio_write(device, bar, buf, len, offset);
     702                 :          0 :         case RTE_PCI_KDRV_VFIO:
     703                 :          0 :                 return pci_vfio_mmio_write(device, bar, buf, len, offset);
     704                 :          0 :         default:
     705                 :          0 :                 rte_pci_device_name(&device->addr, devname,
     706                 :            :                                     RTE_DEV_NAME_MAX_LEN);
     707                 :          0 :                 PCI_LOG(ERR, "Unknown driver type for %s", devname);
     708                 :          0 :                 return -1;
     709                 :            :         }
     710                 :            : }
     711                 :            : 
     712                 :            : RTE_EXPORT_SYMBOL(rte_pci_ioport_map)
     713                 :            : int
     714                 :          0 : rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
     715                 :            :                 struct rte_pci_ioport *p)
     716                 :            : {
     717                 :            :         int ret = -1;
     718                 :            : 
     719      [ #  #  # ]:          0 :         switch (dev->kdrv) {
     720                 :          0 :         case RTE_PCI_KDRV_VFIO:
     721         [ #  # ]:          0 :                 if (pci_vfio_is_enabled())
     722                 :          0 :                         ret = pci_vfio_ioport_map(dev, bar, p);
     723                 :            :                 break;
     724                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     725                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     726                 :          0 :                 ret = pci_uio_ioport_map(dev, bar, p);
     727                 :          0 :                 break;
     728                 :            :         default:
     729                 :            :                 break;
     730                 :            :         }
     731                 :            : 
     732         [ #  # ]:          0 :         if (!ret)
     733                 :          0 :                 p->dev = dev;
     734                 :            : 
     735                 :          0 :         return ret;
     736                 :            : }
     737                 :            : 
     738                 :            : RTE_EXPORT_SYMBOL(rte_pci_ioport_read)
     739                 :            : void
     740                 :          0 : rte_pci_ioport_read(struct rte_pci_ioport *p,
     741                 :            :                 void *data, size_t len, off_t offset)
     742                 :            : {
     743      [ #  #  # ]:          0 :         switch (p->dev->kdrv) {
     744                 :          0 :         case RTE_PCI_KDRV_VFIO:
     745                 :          0 :                 pci_vfio_ioport_read(p, data, len, offset);
     746                 :          0 :                 break;
     747                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     748                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     749                 :          0 :                 pci_uio_ioport_read(p, data, len, offset);
     750                 :          0 :                 break;
     751                 :            :         default:
     752                 :            :                 break;
     753                 :            :         }
     754                 :          0 : }
     755                 :            : 
     756                 :            : RTE_EXPORT_SYMBOL(rte_pci_ioport_write)
     757                 :            : void
     758                 :          0 : rte_pci_ioport_write(struct rte_pci_ioport *p,
     759                 :            :                 const void *data, size_t len, off_t offset)
     760                 :            : {
     761      [ #  #  # ]:          0 :         switch (p->dev->kdrv) {
     762                 :          0 :         case RTE_PCI_KDRV_VFIO:
     763                 :          0 :                 pci_vfio_ioport_write(p, data, len, offset);
     764                 :          0 :                 break;
     765                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     766                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     767                 :          0 :                 pci_uio_ioport_write(p, data, len, offset);
     768                 :          0 :                 break;
     769                 :            :         default:
     770                 :            :                 break;
     771                 :            :         }
     772                 :          0 : }
     773                 :            : 
     774                 :            : RTE_EXPORT_SYMBOL(rte_pci_ioport_unmap)
     775                 :            : int
     776                 :          0 : rte_pci_ioport_unmap(struct rte_pci_ioport *p)
     777                 :            : {
     778                 :            :         int ret = -1;
     779                 :            : 
     780      [ #  #  # ]:          0 :         switch (p->dev->kdrv) {
     781                 :          0 :         case RTE_PCI_KDRV_VFIO:
     782         [ #  # ]:          0 :                 if (pci_vfio_is_enabled())
     783                 :          0 :                         ret = pci_vfio_ioport_unmap(p);
     784                 :            :                 break;
     785                 :          0 :         case RTE_PCI_KDRV_IGB_UIO:
     786                 :            :         case RTE_PCI_KDRV_UIO_GENERIC:
     787                 :          0 :                 ret = pci_uio_ioport_unmap(p);
     788                 :          0 :                 break;
     789                 :            :         default:
     790                 :            :                 break;
     791                 :            :         }
     792                 :            : 
     793                 :          0 :         return ret;
     794                 :            : }

Generated by: LCOV version 1.14