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

Generated by: LCOV version 1.14