LCOV - code coverage report
Current view: top level - drivers/bus/pci/linux - pci_uio.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 222 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 13 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 136 0.0 %

           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 <unistd.h>
       7                 :            : #include <fcntl.h>
       8                 :            : #include <dirent.h>
       9                 :            : #include <inttypes.h>
      10                 :            : #include <sys/stat.h>
      11                 :            : #include <sys/mman.h>
      12                 :            : #include <sys/sysmacros.h>
      13                 :            : 
      14                 :            : #if defined(RTE_ARCH_X86)
      15                 :            : #include <sys/io.h>
      16                 :            : #endif
      17                 :            : 
      18                 :            : #include <rte_string_fns.h>
      19                 :            : #include <rte_log.h>
      20                 :            : #include <rte_pci.h>
      21                 :            : #include <rte_bus_pci.h>
      22                 :            : #include <rte_common.h>
      23                 :            : #include <rte_malloc.h>
      24                 :            : 
      25                 :            : #include "eal_filesystem.h"
      26                 :            : #include "pci_init.h"
      27                 :            : #include "private.h"
      28                 :            : 
      29                 :            : void *pci_map_addr = NULL;
      30                 :            : 
      31                 :            : #define OFF_MAX              ((uint64_t)(off_t)-1)
      32                 :            : 
      33                 :            : int
      34                 :          0 : pci_uio_read_config(const struct rte_intr_handle *intr_handle,
      35                 :            :                     void *buf, size_t len, off_t offset)
      36                 :            : {
      37                 :          0 :         int uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
      38                 :            : 
      39         [ #  # ]:          0 :         if (uio_cfg_fd < 0)
      40                 :            :                 return -1;
      41                 :            : 
      42                 :          0 :         return pread(uio_cfg_fd, buf, len, offset);
      43                 :            : }
      44                 :            : 
      45                 :            : int
      46                 :          0 : pci_uio_write_config(const struct rte_intr_handle *intr_handle,
      47                 :            :                      const void *buf, size_t len, off_t offset)
      48                 :            : {
      49                 :          0 :         int uio_cfg_fd = rte_intr_dev_fd_get(intr_handle);
      50                 :            : 
      51         [ #  # ]:          0 :         if (uio_cfg_fd < 0)
      52                 :            :                 return -1;
      53                 :            : 
      54                 :          0 :         return pwrite(uio_cfg_fd, buf, len, offset);
      55                 :            : }
      56                 :            : 
      57                 :            : int
      58                 :          0 : pci_uio_mmio_read(const struct rte_pci_device *dev, int bar,
      59                 :            :                   void *buf, size_t len, off_t offset)
      60                 :            : {
      61   [ #  #  #  # ]:          0 :         if (bar >= PCI_MAX_RESOURCE || dev->mem_resource[bar].addr == NULL ||
      62         [ #  # ]:          0 :                         (uint64_t)offset + len > dev->mem_resource[bar].len)
      63                 :            :                 return -1;
      64                 :          0 :         memcpy(buf, (uint8_t *)dev->mem_resource[bar].addr + offset, len);
      65                 :          0 :         return len;
      66                 :            : }
      67                 :            : 
      68                 :            : int
      69                 :          0 : pci_uio_mmio_write(const struct rte_pci_device *dev, int bar,
      70                 :            :                    const void *buf, size_t len, off_t offset)
      71                 :            : {
      72   [ #  #  #  # ]:          0 :         if (bar >= PCI_MAX_RESOURCE || dev->mem_resource[bar].addr == NULL ||
      73         [ #  # ]:          0 :                         (uint64_t)offset + len > dev->mem_resource[bar].len)
      74                 :            :                 return -1;
      75                 :          0 :         memcpy((uint8_t *)dev->mem_resource[bar].addr + offset, buf, len);
      76                 :          0 :         return len;
      77                 :            : }
      78                 :            : 
      79                 :            : static int
      80                 :          0 : pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
      81                 :            : {
      82                 :            :         FILE *f;
      83                 :            :         char filename[PATH_MAX];
      84                 :            :         int ret;
      85                 :            :         unsigned major, minor;
      86                 :            :         dev_t dev;
      87                 :            : 
      88                 :            :         /* get the name of the sysfs file that contains the major and minor
      89                 :            :          * of the uio device and read its content */
      90                 :            :         snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
      91                 :            : 
      92                 :          0 :         f = fopen(filename, "r");
      93         [ #  # ]:          0 :         if (f == NULL) {
      94                 :          0 :                 PCI_LOG(ERR, "%s(): cannot open sysfs to get major:minor", __func__);
      95                 :          0 :                 return -1;
      96                 :            :         }
      97                 :            : 
      98                 :          0 :         ret = fscanf(f, "%u:%u", &major, &minor);
      99         [ #  # ]:          0 :         if (ret != 2) {
     100                 :          0 :                 PCI_LOG(ERR, "%s(): cannot parse sysfs to get major:minor", __func__);
     101                 :          0 :                 fclose(f);
     102                 :          0 :                 return -1;
     103                 :            :         }
     104                 :          0 :         fclose(f);
     105                 :            : 
     106                 :            :         /* create the char device "mknod /dev/uioX c major minor" */
     107                 :            :         snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
     108                 :          0 :         dev = makedev(major, minor);
     109                 :          0 :         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
     110         [ #  # ]:          0 :         if (ret != 0) {
     111                 :          0 :                 PCI_LOG(ERR, "%s(): mknod() failed %s", __func__, strerror(errno));
     112                 :          0 :                 return -1;
     113                 :            :         }
     114                 :            : 
     115                 :            :         return ret;
     116                 :            : }
     117                 :            : 
     118                 :            : /*
     119                 :            :  * Return the uioX char device used for a pci device. On success, return
     120                 :            :  * the UIO number and fill dstbuf string with the path of the device in
     121                 :            :  * sysfs. On error, return a negative value. In this case dstbuf is
     122                 :            :  * invalid.
     123                 :            :  */
     124                 :            : static int
     125                 :          0 : pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
     126                 :            :                            unsigned int buflen, int create)
     127                 :            : {
     128                 :            :         struct rte_pci_addr *loc = &dev->addr;
     129                 :            :         int uio_num = -1;
     130                 :            :         struct dirent *e;
     131                 :            :         DIR *dir;
     132                 :            :         char dirname[PATH_MAX];
     133                 :            : 
     134                 :            :         /* depending on kernel version, uio can be located in uio/uioX
     135                 :            :          * or uio:uioX */
     136                 :            : 
     137                 :          0 :         snprintf(dirname, sizeof(dirname),
     138                 :            :                         "%s/" PCI_PRI_FMT "/uio", rte_pci_get_sysfs_path(),
     139                 :          0 :                         loc->domain, loc->bus, loc->devid, loc->function);
     140                 :            : 
     141                 :          0 :         dir = opendir(dirname);
     142         [ #  # ]:          0 :         if (dir == NULL) {
     143                 :            :                 /* retry with the parent directory */
     144                 :          0 :                 snprintf(dirname, sizeof(dirname),
     145                 :            :                                 "%s/" PCI_PRI_FMT, rte_pci_get_sysfs_path(),
     146                 :          0 :                                 loc->domain, loc->bus, loc->devid, loc->function);
     147                 :          0 :                 dir = opendir(dirname);
     148                 :            : 
     149         [ #  # ]:          0 :                 if (dir == NULL) {
     150                 :          0 :                         PCI_LOG(ERR, "Cannot opendir %s", dirname);
     151                 :          0 :                         return -1;
     152                 :            :                 }
     153                 :            :         }
     154                 :            : 
     155                 :            :         /* take the first file starting with "uio" */
     156         [ #  # ]:          0 :         while ((e = readdir(dir)) != NULL) {
     157                 :            :                 /* format could be uio%d ...*/
     158                 :            :                 int shortprefix_len = sizeof("uio") - 1;
     159                 :            :                 /* ... or uio:uio%d */
     160                 :            :                 int longprefix_len = sizeof("uio:uio") - 1;
     161                 :            :                 char *endptr;
     162                 :            : 
     163         [ #  # ]:          0 :                 if (strncmp(e->d_name, "uio", 3) != 0)
     164                 :          0 :                         continue;
     165                 :            : 
     166                 :            :                 /* first try uio%d */
     167                 :          0 :                 errno = 0;
     168                 :          0 :                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
     169   [ #  #  #  # ]:          0 :                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
     170                 :          0 :                         snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
     171                 :          0 :                         break;
     172                 :            :                 }
     173                 :            : 
     174                 :            :                 /* then try uio:uio%d */
     175                 :          0 :                 errno = 0;
     176                 :          0 :                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
     177   [ #  #  #  # ]:          0 :                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
     178                 :          0 :                         snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
     179                 :            :                         break;
     180                 :            :                 }
     181                 :            :         }
     182                 :          0 :         closedir(dir);
     183                 :            : 
     184                 :            :         /* No uio resource found */
     185         [ #  # ]:          0 :         if (e == NULL)
     186                 :            :                 return -1;
     187                 :            : 
     188                 :            :         /* create uio device if we've been asked to */
     189   [ #  #  #  #  :          0 :         if (rte_eal_create_uio_dev() && create &&
                   #  # ]
     190                 :          0 :                         pci_mknod_uio_dev(dstbuf, uio_num) < 0)
     191                 :          0 :                 PCI_LOG(WARNING, "Cannot create /dev/uio%u", uio_num);
     192                 :            : 
     193                 :            :         return uio_num;
     194                 :            : }
     195                 :            : 
     196                 :            : void
     197                 :          0 : pci_uio_free_resource(struct rte_pci_device *dev,
     198                 :            :                 struct mapped_pci_resource *uio_res)
     199                 :            : {
     200                 :          0 :         rte_free(uio_res);
     201                 :            : 
     202                 :          0 :         rte_intr_dev_fd_close(dev->intr_handle);
     203                 :          0 :         rte_intr_fd_close(dev->intr_handle);
     204                 :          0 :         rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);
     205                 :          0 : }
     206                 :            : 
     207                 :            : int
     208                 :          0 : pci_uio_alloc_resource(struct rte_pci_device *dev,
     209                 :            :                 struct mapped_pci_resource **uio_res)
     210                 :            : {
     211                 :            :         char dirname[PATH_MAX];
     212                 :            :         char cfgname[PATH_MAX];
     213                 :            :         char devname[PATH_MAX]; /* contains the /dev/uioX */
     214                 :            :         int uio_num, fd, uio_cfg_fd;
     215                 :            :         struct rte_pci_addr *loc;
     216                 :            : 
     217                 :            :         loc = &dev->addr;
     218                 :            : 
     219                 :            :         /* find uio resource */
     220                 :          0 :         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
     221         [ #  # ]:          0 :         if (uio_num < 0) {
     222                 :          0 :                 PCI_LOG(WARNING, "  "PCI_PRI_FMT" not managed by UIO driver, skipping",
     223                 :            :                         loc->domain, loc->bus, loc->devid, loc->function);
     224                 :          0 :                 return 1;
     225                 :            :         }
     226                 :            :         snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
     227                 :            : 
     228                 :            :         /* save fd */
     229                 :            :         fd = open(devname, O_RDWR);
     230         [ #  # ]:          0 :         if (fd < 0) {
     231                 :          0 :                 PCI_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
     232                 :          0 :                 goto error;
     233                 :            :         }
     234                 :            : 
     235         [ #  # ]:          0 :         if (rte_intr_fd_set(dev->intr_handle, fd))
     236                 :          0 :                 goto error;
     237                 :            : 
     238                 :            :         snprintf(cfgname, sizeof(cfgname),
     239                 :            :                         "/sys/class/uio/uio%u/device/config", uio_num);
     240                 :            : 
     241                 :            :         uio_cfg_fd = open(cfgname, O_RDWR);
     242         [ #  # ]:          0 :         if (uio_cfg_fd < 0) {
     243                 :          0 :                 PCI_LOG(ERR, "Cannot open %s: %s", cfgname, strerror(errno));
     244                 :          0 :                 goto error;
     245                 :            :         }
     246                 :            : 
     247         [ #  # ]:          0 :         if (rte_intr_dev_fd_set(dev->intr_handle, uio_cfg_fd))
     248                 :          0 :                 goto error;
     249                 :            : 
     250         [ #  # ]:          0 :         if (dev->kdrv == RTE_PCI_KDRV_IGB_UIO) {
     251         [ #  # ]:          0 :                 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
     252                 :          0 :                         goto error;
     253                 :            :         } else {
     254         [ #  # ]:          0 :                 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO_INTX))
     255                 :          0 :                         goto error;
     256                 :            : 
     257                 :            :                 /* set bus master that is not done by uio_pci_generic */
     258         [ #  # ]:          0 :                 if (rte_pci_set_bus_master(dev, true)) {
     259                 :          0 :                         PCI_LOG(ERR, "Cannot set up bus mastering!");
     260                 :          0 :                         goto error;
     261                 :            :                 }
     262                 :            :         }
     263                 :            : 
     264         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
     265                 :            :                 return 0;
     266                 :            : 
     267                 :            :         /* allocate the mapping details for secondary processes*/
     268                 :          0 :         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
     269         [ #  # ]:          0 :         if (*uio_res == NULL) {
     270                 :          0 :                 PCI_LOG(ERR, "%s(): cannot store uio mmap details", __func__);
     271                 :          0 :                 goto error;
     272                 :            :         }
     273                 :            : 
     274                 :          0 :         strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
     275                 :          0 :         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
     276                 :            : 
     277                 :          0 :         return 0;
     278                 :            : 
     279                 :          0 : error:
     280                 :          0 :         pci_uio_free_resource(dev, *uio_res);
     281                 :          0 :         return -1;
     282                 :            : }
     283                 :            : 
     284                 :            : int
     285                 :          0 : pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
     286                 :            :                 struct mapped_pci_resource *uio_res, int map_idx)
     287                 :            : {
     288                 :            :         int fd = -1;
     289                 :            :         char devname[PATH_MAX];
     290                 :            :         void *mapaddr;
     291                 :            :         struct rte_pci_addr *loc;
     292                 :            :         struct pci_map *maps;
     293                 :            :         int wc_activate = 0;
     294                 :            : 
     295         [ #  # ]:          0 :         if (dev->device.driver != NULL) {
     296                 :            :                 const struct rte_pci_driver *pdrv = RTE_BUS_DRIVER(dev->device.driver, *pdrv);
     297                 :          0 :                 wc_activate = pdrv->drv_flags & RTE_PCI_DRV_WC_ACTIVATE;
     298                 :            :         }
     299                 :            : 
     300                 :            :         loc = &dev->addr;
     301                 :          0 :         maps = uio_res->maps;
     302                 :            : 
     303                 :            :         /* allocate memory to keep path */
     304                 :          0 :         maps[map_idx].path = rte_malloc(NULL, sizeof(devname), 0);
     305         [ #  # ]:          0 :         if (maps[map_idx].path == NULL) {
     306                 :          0 :                 PCI_LOG(ERR, "Cannot allocate memory for path: %s", strerror(errno));
     307                 :          0 :                 return -1;
     308                 :            :         }
     309                 :            : 
     310                 :            :         /*
     311                 :            :          * open resource file, to mmap it
     312                 :            :          */
     313         [ #  # ]:          0 :         if (wc_activate) {
     314                 :            :                 /* update devname for mmap  */
     315                 :          0 :                 snprintf(devname, sizeof(devname),
     316                 :            :                         "%s/" PCI_PRI_FMT "/resource%d_wc",
     317                 :            :                         rte_pci_get_sysfs_path(),
     318                 :          0 :                         loc->domain, loc->bus, loc->devid,
     319                 :          0 :                         loc->function, res_idx);
     320                 :            : 
     321                 :            :                 fd = open(devname, O_RDWR);
     322   [ #  #  #  # ]:          0 :                 if (fd < 0 && errno != ENOENT) {
     323                 :          0 :                         PCI_LOG(INFO, "%s cannot be mapped. Fall-back to non prefetchable mode.",
     324                 :            :                                 devname);
     325                 :            :                 }
     326                 :            :         }
     327                 :            : 
     328         [ #  # ]:          0 :         if (!wc_activate || fd < 0) {
     329                 :          0 :                 snprintf(devname, sizeof(devname),
     330                 :            :                         "%s/" PCI_PRI_FMT "/resource%d",
     331                 :            :                         rte_pci_get_sysfs_path(),
     332                 :          0 :                         loc->domain, loc->bus, loc->devid,
     333                 :          0 :                         loc->function, res_idx);
     334                 :            : 
     335                 :            :                 /* then try to map resource file */
     336                 :            :                 fd = open(devname, O_RDWR);
     337         [ #  # ]:          0 :                 if (fd < 0) {
     338                 :          0 :                         PCI_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
     339                 :          0 :                         goto error;
     340                 :            :                 }
     341                 :            :         }
     342                 :            : 
     343                 :            :         /* try mapping somewhere close to the end of hugepages */
     344         [ #  # ]:          0 :         if (pci_map_addr == NULL)
     345                 :          0 :                 pci_map_addr = pci_find_max_end_va();
     346                 :            : 
     347                 :          0 :         mapaddr = pci_map_resource(pci_map_addr, fd, 0,
     348                 :          0 :                         (size_t)dev->mem_resource[res_idx].len, 0);
     349                 :          0 :         close(fd);
     350         [ #  # ]:          0 :         if (mapaddr == NULL)
     351                 :          0 :                 goto error;
     352                 :            : 
     353                 :          0 :         pci_map_addr = RTE_PTR_ADD(mapaddr,
     354                 :            :                         (size_t)dev->mem_resource[res_idx].len);
     355                 :            : 
     356                 :          0 :         pci_map_addr = RTE_PTR_ALIGN(pci_map_addr, sysconf(_SC_PAGE_SIZE));
     357                 :            : 
     358                 :          0 :         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
     359                 :          0 :         maps[map_idx].size = dev->mem_resource[res_idx].len;
     360                 :          0 :         maps[map_idx].addr = mapaddr;
     361                 :          0 :         maps[map_idx].offset = 0;
     362                 :          0 :         strcpy(maps[map_idx].path, devname);
     363                 :          0 :         dev->mem_resource[res_idx].addr = mapaddr;
     364                 :            : 
     365                 :          0 :         return 0;
     366                 :            : 
     367                 :          0 : error:
     368                 :          0 :         rte_free(maps[map_idx].path);
     369                 :          0 :         return -1;
     370                 :            : }
     371                 :            : 
     372                 :            : #define PIO_MAX 0x10000
     373                 :            : 
     374                 :            : #if defined(RTE_ARCH_X86)
     375                 :            : int
     376                 :          0 : pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
     377                 :            :                    struct rte_pci_ioport *p)
     378                 :            : {
     379                 :            :         FILE *f = NULL;
     380                 :            :         char dirname[PATH_MAX];
     381                 :            :         char filename[PATH_MAX];
     382                 :            :         char buf[BUFSIZ];
     383                 :            :         uint64_t phys_addr, end_addr, flags;
     384                 :            :         unsigned long base;
     385                 :            :         int i, fd;
     386                 :            : 
     387                 :            :         /* open and read addresses of the corresponding resource in sysfs */
     388                 :          0 :         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
     389                 :          0 :                 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
     390                 :          0 :                 dev->addr.devid, dev->addr.function);
     391                 :          0 :         f = fopen(filename, "r");
     392         [ #  # ]:          0 :         if (f == NULL) {
     393                 :          0 :                 PCI_LOG(ERR, "%s(): Cannot open sysfs resource: %s", __func__, strerror(errno));
     394                 :          0 :                 return -1;
     395                 :            :         }
     396                 :            : 
     397         [ #  # ]:          0 :         for (i = 0; i < bar + 1; i++) {
     398         [ #  # ]:          0 :                 if (fgets(buf, sizeof(buf), f) == NULL) {
     399                 :          0 :                         PCI_LOG(ERR, "%s(): Cannot read sysfs resource", __func__);
     400                 :          0 :                         goto error;
     401                 :            :                 }
     402                 :            :         }
     403         [ #  # ]:          0 :         if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
     404                 :            :                 &end_addr, &flags) < 0)
     405                 :          0 :                 goto error;
     406                 :            : 
     407         [ #  # ]:          0 :         if (flags & IORESOURCE_IO) {
     408         [ #  # ]:          0 :                 if (rte_eal_iopl_init()) {
     409                 :          0 :                         PCI_LOG(ERR, "%s(): insufficient ioport permissions for PCI device %s",
     410                 :            :                                 __func__, dev->name);
     411                 :          0 :                         goto error;
     412                 :            :                 }
     413                 :            : 
     414                 :          0 :                 base = (unsigned long)phys_addr;
     415         [ #  # ]:          0 :                 if (base > PIO_MAX) {
     416                 :          0 :                         PCI_LOG(ERR, "%s(): %08lx too large PIO resource", __func__, base);
     417                 :          0 :                         goto error;
     418                 :            :                 }
     419                 :            : 
     420                 :          0 :                 PCI_LOG(DEBUG, "%s(): PIO BAR %08lx detected", __func__, base);
     421         [ #  # ]:          0 :         } else if (flags & IORESOURCE_MEM) {
     422                 :          0 :                 base = (unsigned long)dev->mem_resource[bar].addr;
     423                 :          0 :                 PCI_LOG(DEBUG, "%s(): MMIO BAR %08lx detected", __func__, base);
     424                 :            :         } else {
     425                 :          0 :                 PCI_LOG(ERR, "%s(): unknown BAR type", __func__);
     426                 :          0 :                 goto error;
     427                 :            :         }
     428                 :            : 
     429                 :            :         /* FIXME only for primary process ? */
     430         [ #  # ]:          0 :         if (rte_intr_type_get(dev->intr_handle) ==
     431                 :            :                                         RTE_INTR_HANDLE_UNKNOWN) {
     432                 :          0 :                 int uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
     433         [ #  # ]:          0 :                 if (uio_num < 0) {
     434                 :          0 :                         PCI_LOG(ERR, "cannot open %s: %s", dirname, strerror(errno));
     435                 :          0 :                         goto error;
     436                 :            :                 }
     437                 :            : 
     438                 :            :                 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
     439                 :            :                 fd = open(filename, O_RDWR);
     440         [ #  # ]:          0 :                 if (fd < 0) {
     441                 :          0 :                         PCI_LOG(ERR, "Cannot open %s: %s", filename, strerror(errno));
     442                 :          0 :                         goto error;
     443                 :            :                 }
     444         [ #  # ]:          0 :                 if (rte_intr_fd_set(dev->intr_handle, fd))
     445                 :          0 :                         goto error;
     446                 :            : 
     447         [ #  # ]:          0 :                 if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
     448                 :          0 :                         goto error;
     449                 :            :         }
     450                 :            : 
     451                 :          0 :         PCI_LOG(DEBUG, "PCI Port IO found start=0x%lx", base);
     452                 :            : 
     453                 :          0 :         p->base = base;
     454                 :          0 :         p->len = 0;
     455                 :          0 :         fclose(f);
     456                 :          0 :         return 0;
     457                 :          0 : error:
     458                 :            :         if (f)
     459                 :          0 :                 fclose(f);
     460                 :          0 :         return -1;
     461                 :            : }
     462                 :            : #else
     463                 :            : int
     464                 :            : pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
     465                 :            :                    struct rte_pci_ioport *p)
     466                 :            : {
     467                 :            :         FILE *f;
     468                 :            :         char buf[BUFSIZ];
     469                 :            :         char filename[PATH_MAX];
     470                 :            :         uint64_t phys_addr, end_addr, flags;
     471                 :            :         int fd, i;
     472                 :            :         void *addr;
     473                 :            : 
     474                 :            :         /* open and read addresses of the corresponding resource in sysfs */
     475                 :            :         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
     476                 :            :                 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
     477                 :            :                 dev->addr.devid, dev->addr.function);
     478                 :            :         f = fopen(filename, "r");
     479                 :            :         if (f == NULL) {
     480                 :            :                 PCI_LOG(ERR, "Cannot open sysfs resource: %s", strerror(errno));
     481                 :            :                 return -1;
     482                 :            :         }
     483                 :            :         for (i = 0; i < bar + 1; i++) {
     484                 :            :                 if (fgets(buf, sizeof(buf), f) == NULL) {
     485                 :            :                         PCI_LOG(ERR, "Cannot read sysfs resource");
     486                 :            :                         goto error;
     487                 :            :                 }
     488                 :            :         }
     489                 :            :         if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
     490                 :            :                         &end_addr, &flags) < 0)
     491                 :            :                 goto error;
     492                 :            :         if ((flags & IORESOURCE_IO) == 0) {
     493                 :            :                 PCI_LOG(ERR, "BAR %d is not an IO resource", bar);
     494                 :            :                 goto error;
     495                 :            :         }
     496                 :            :         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
     497                 :            :                 rte_pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
     498                 :            :                 dev->addr.devid, dev->addr.function, bar);
     499                 :            : 
     500                 :            :         /* mmap the pci resource */
     501                 :            :         fd = open(filename, O_RDWR);
     502                 :            :         if (fd < 0) {
     503                 :            :                 PCI_LOG(ERR, "Cannot open %s: %s", filename, strerror(errno));
     504                 :            :                 goto error;
     505                 :            :         }
     506                 :            :         addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
     507                 :            :                 MAP_SHARED, fd, 0);
     508                 :            :         close(fd);
     509                 :            :         if (addr == MAP_FAILED) {
     510                 :            :                 PCI_LOG(ERR, "Cannot mmap IO port resource: %s", strerror(errno));
     511                 :            :                 goto error;
     512                 :            :         }
     513                 :            : 
     514                 :            :         /* strangely, the base address is mmap addr + phys_addr */
     515                 :            :         p->base = (uintptr_t)addr + phys_addr;
     516                 :            :         p->len = end_addr + 1;
     517                 :            :         PCI_LOG(DEBUG, "PCI Port IO found start=0x%"PRIx64, p->base);
     518                 :            :         fclose(f);
     519                 :            : 
     520                 :            :         return 0;
     521                 :            : 
     522                 :            : error:
     523                 :            :         fclose(f);
     524                 :            :         return -1;
     525                 :            : }
     526                 :            : #endif
     527                 :            : 
     528                 :            : #if defined(RTE_ARCH_X86)
     529                 :            : 
     530                 :            : static inline uint8_t ioread8(void *addr)
     531                 :            : {
     532                 :            :         uint8_t val;
     533                 :            : 
     534                 :          0 :         val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     535                 :            :                 *(volatile uint8_t *)addr :
     536                 :            : #ifdef __GLIBC__
     537                 :          0 :                 inb_p((unsigned long)addr);
     538                 :            : #else
     539                 :            :                 inb((unsigned long)addr);
     540                 :            : #endif
     541                 :            : 
     542                 :            :         return val;
     543                 :            : }
     544                 :            : 
     545                 :            : static inline uint16_t ioread16(void *addr)
     546                 :            : {
     547                 :            :         uint16_t val;
     548                 :            : 
     549                 :          0 :         val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     550                 :            :                 *(volatile uint16_t *)addr :
     551                 :            : #ifdef __GLIBC__
     552                 :          0 :                 inw_p((unsigned long)addr);
     553                 :            : #else
     554                 :            :                 inw((unsigned long)addr);
     555                 :            : #endif
     556                 :            : 
     557                 :            :         return val;
     558                 :            : }
     559                 :            : 
     560                 :            : static inline uint32_t ioread32(void *addr)
     561                 :            : {
     562                 :            :         uint32_t val;
     563                 :            : 
     564                 :            :         val = (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     565                 :          0 :                 *(volatile uint32_t *)addr :
     566                 :            : #ifdef __GLIBC__
     567                 :          0 :                 inl_p((unsigned long)addr);
     568                 :            : #else
     569                 :            :                 inl((unsigned long)addr);
     570                 :            : #endif
     571                 :            : 
     572                 :            :         return val;
     573                 :            : }
     574                 :            : 
     575                 :            : static inline void iowrite8(uint8_t val, void *addr)
     576                 :            : {
     577                 :            :         (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     578                 :          0 :                 *(volatile uint8_t *)addr = val :
     579                 :            : #ifdef __GLIBC__
     580                 :          0 :                 outb_p(val, (unsigned long)addr);
     581                 :            : #else
     582                 :            :                 outb(val, (unsigned long)addr);
     583                 :            : #endif
     584                 :            : }
     585                 :            : 
     586                 :            : static inline void iowrite16(uint16_t val, void *addr)
     587                 :            : {
     588                 :            :         (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     589                 :          0 :                 *(volatile uint16_t *)addr = val :
     590                 :            : #ifdef __GLIBC__
     591                 :          0 :                 outw_p(val, (unsigned long)addr);
     592                 :            : #else
     593                 :            :                 outw(val, (unsigned long)addr);
     594                 :            : #endif
     595                 :            : }
     596                 :            : 
     597                 :            : static inline void iowrite32(uint32_t val, void *addr)
     598                 :            : {
     599                 :            :         (uint64_t)(uintptr_t)addr >= PIO_MAX ?
     600                 :          0 :                 *(volatile uint32_t *)addr = val :
     601                 :            : #ifdef __GLIBC__
     602                 :          0 :                 outl_p(val, (unsigned long)addr);
     603                 :            : #else
     604                 :            :                 outl(val, (unsigned long)addr);
     605                 :            : #endif
     606                 :            : }
     607                 :            : 
     608                 :            : #else /* !RTE_ARCH_X86 */
     609                 :            : 
     610                 :            : static inline uint8_t ioread8(void *addr)
     611                 :            : {
     612                 :            :         return *(volatile uint8_t *)addr;
     613                 :            : }
     614                 :            : 
     615                 :            : static inline uint16_t ioread16(void *addr)
     616                 :            : {
     617                 :            :         return *(volatile uint16_t *)addr;
     618                 :            : }
     619                 :            : 
     620                 :            : static inline uint32_t ioread32(void *addr)
     621                 :            : {
     622                 :            :         return *(volatile uint32_t *)addr;
     623                 :            : }
     624                 :            : 
     625                 :            : static inline void iowrite8(uint8_t val, void *addr)
     626                 :            : {
     627                 :            :         *(volatile uint8_t *)addr = val;
     628                 :            : }
     629                 :            : 
     630                 :            : static inline void iowrite16(uint16_t val, void *addr)
     631                 :            : {
     632                 :            :         *(volatile uint16_t *)addr = val;
     633                 :            : }
     634                 :            : 
     635                 :            : static inline void iowrite32(uint32_t val, void *addr)
     636                 :            : {
     637                 :            :         *(volatile uint32_t *)addr = val;
     638                 :            : }
     639                 :            : 
     640                 :            : #endif /* !RTE_ARCH_X86 */
     641                 :            : 
     642                 :            : void
     643                 :          0 : pci_uio_ioport_read(struct rte_pci_ioport *p,
     644                 :            :                     void *data, size_t len, off_t offset)
     645                 :            : {
     646                 :            :         uint8_t *d;
     647                 :            :         int size;
     648                 :          0 :         uintptr_t reg = p->base + offset;
     649                 :            : 
     650         [ #  # ]:          0 :         for (d = data; len > 0; d += size, reg += size, len -= size) {
     651         [ #  # ]:          0 :                 if (len >= 4) {
     652                 :            :                         size = 4;
     653         [ #  # ]:          0 :                         *(uint32_t *)d = ioread32((void *)reg);
     654         [ #  # ]:          0 :                 } else if (len >= 2) {
     655                 :            :                         size = 2;
     656         [ #  # ]:          0 :                         *(uint16_t *)d = ioread16((void *)reg);
     657                 :            :                 } else {
     658                 :            :                         size = 1;
     659         [ #  # ]:          0 :                         *d = ioread8((void *)reg);
     660                 :            :                 }
     661                 :            :         }
     662                 :          0 : }
     663                 :            : 
     664                 :            : void
     665                 :          0 : pci_uio_ioport_write(struct rte_pci_ioport *p,
     666                 :            :                      const void *data, size_t len, off_t offset)
     667                 :            : {
     668                 :            :         const uint8_t *s;
     669                 :            :         int size;
     670                 :          0 :         uintptr_t reg = p->base + offset;
     671                 :            : 
     672         [ #  # ]:          0 :         for (s = data; len > 0; s += size, reg += size, len -= size) {
     673         [ #  # ]:          0 :                 if (len >= 4) {
     674                 :            :                         size = 4;
     675         [ #  # ]:          0 :                         iowrite32(*(const uint32_t *)s, (void *)reg);
     676         [ #  # ]:          0 :                 } else if (len >= 2) {
     677                 :            :                         size = 2;
     678         [ #  # ]:          0 :                         iowrite16(*(const uint16_t *)s, (void *)reg);
     679                 :            :                 } else {
     680                 :            :                         size = 1;
     681         [ #  # ]:          0 :                         iowrite8(*s, (void *)reg);
     682                 :            :                 }
     683                 :            :         }
     684                 :          0 : }
     685                 :            : 
     686                 :            : int
     687                 :          0 : pci_uio_ioport_unmap(struct rte_pci_ioport *p)
     688                 :            : {
     689                 :            : #if defined(RTE_ARCH_X86)
     690                 :            :         RTE_SET_USED(p);
     691                 :            :         /* FIXME close intr fd ? */
     692                 :          0 :         return 0;
     693                 :            : #else
     694                 :            :         return munmap((void *)(uintptr_t)p->base, p->len);
     695                 :            : #endif
     696                 :            : }

Generated by: LCOV version 1.14