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

Generated by: LCOV version 1.14