15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 25566a3e3SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 3c752998bSGaetan Rivet */ 4c752998bSGaetan Rivet 5c752998bSGaetan Rivet #include <ctype.h> 6c752998bSGaetan Rivet #include <stdio.h> 7c752998bSGaetan Rivet #include <stdlib.h> 8c752998bSGaetan Rivet #include <string.h> 9c752998bSGaetan Rivet #include <stdarg.h> 10c752998bSGaetan Rivet #include <unistd.h> 11c752998bSGaetan Rivet #include <inttypes.h> 12c752998bSGaetan Rivet #include <sys/types.h> 13c752998bSGaetan Rivet #include <sys/stat.h> 14c752998bSGaetan Rivet #include <fcntl.h> 15c752998bSGaetan Rivet #include <errno.h> 16c752998bSGaetan Rivet #include <dirent.h> 17c752998bSGaetan Rivet #include <limits.h> 18c752998bSGaetan Rivet #include <sys/queue.h> 19c752998bSGaetan Rivet #include <sys/mman.h> 20c752998bSGaetan Rivet #include <sys/ioctl.h> 21c752998bSGaetan Rivet #include <sys/pciio.h> 22c752998bSGaetan Rivet #include <dev/pci/pcireg.h> 23c752998bSGaetan Rivet 24c752998bSGaetan Rivet #if defined(RTE_ARCH_X86) 25c752998bSGaetan Rivet #include <machine/cpufunc.h> 26c752998bSGaetan Rivet #endif 27c752998bSGaetan Rivet 28c752998bSGaetan Rivet #include <rte_interrupts.h> 29c752998bSGaetan Rivet #include <rte_log.h> 30c752998bSGaetan Rivet #include <rte_pci.h> 31c752998bSGaetan Rivet #include <rte_bus_pci.h> 32c752998bSGaetan Rivet #include <rte_common.h> 33c752998bSGaetan Rivet #include <rte_launch.h> 34c752998bSGaetan Rivet #include <rte_memory.h> 35c752998bSGaetan Rivet #include <rte_eal.h> 36c752998bSGaetan Rivet #include <rte_eal_memconfig.h> 37c752998bSGaetan Rivet #include <rte_per_lcore.h> 38c752998bSGaetan Rivet #include <rte_lcore.h> 39c752998bSGaetan Rivet #include <rte_malloc.h> 40c752998bSGaetan Rivet #include <rte_string_fns.h> 41c752998bSGaetan Rivet #include <rte_debug.h> 42c752998bSGaetan Rivet #include <rte_devargs.h> 43c752998bSGaetan Rivet 44c752998bSGaetan Rivet #include "eal_filesystem.h" 45c752998bSGaetan Rivet #include "private.h" 46c752998bSGaetan Rivet 47c752998bSGaetan Rivet /** 48c752998bSGaetan Rivet * @file 4982bf1cafSJerin Jacob * PCI probing under BSD 50c752998bSGaetan Rivet * 51c752998bSGaetan Rivet * This code is used to simulate a PCI probe by parsing information in 52c752998bSGaetan Rivet * sysfs. Moreover, when a registered driver matches a device, the 53c752998bSGaetan Rivet * kernel driver currently using it is unloaded and replaced by 54c752998bSGaetan Rivet * igb_uio module, which is a very minimal userland driver for Intel 55c752998bSGaetan Rivet * network card, only providing access to PCI BAR to applications, and 56c752998bSGaetan Rivet * enabling bus master. 57c752998bSGaetan Rivet */ 58c752998bSGaetan Rivet 59c752998bSGaetan Rivet extern struct rte_pci_bus rte_pci_bus; 60c752998bSGaetan Rivet 61c752998bSGaetan Rivet /* Map pci device */ 62c752998bSGaetan Rivet int 63c752998bSGaetan Rivet rte_pci_map_device(struct rte_pci_device *dev) 64c752998bSGaetan Rivet { 65c752998bSGaetan Rivet int ret = -1; 66c752998bSGaetan Rivet 67c752998bSGaetan Rivet /* try mapping the NIC resources */ 68c752998bSGaetan Rivet switch (dev->kdrv) { 69c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 70c752998bSGaetan Rivet /* map resources for devices that use uio */ 71c752998bSGaetan Rivet ret = pci_uio_map_resource(dev); 72c752998bSGaetan Rivet break; 73c752998bSGaetan Rivet default: 74c752998bSGaetan Rivet RTE_LOG(DEBUG, EAL, 75c752998bSGaetan Rivet " Not managed by a supported kernel driver, skipped\n"); 76c752998bSGaetan Rivet ret = 1; 77c752998bSGaetan Rivet break; 78c752998bSGaetan Rivet } 79c752998bSGaetan Rivet 80c752998bSGaetan Rivet return ret; 81c752998bSGaetan Rivet } 82c752998bSGaetan Rivet 83c752998bSGaetan Rivet /* Unmap pci device */ 84c752998bSGaetan Rivet void 85c752998bSGaetan Rivet rte_pci_unmap_device(struct rte_pci_device *dev) 86c752998bSGaetan Rivet { 87c752998bSGaetan Rivet /* try unmapping the NIC resources */ 88c752998bSGaetan Rivet switch (dev->kdrv) { 89c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 90c752998bSGaetan Rivet /* unmap resources for devices that use uio */ 91c752998bSGaetan Rivet pci_uio_unmap_resource(dev); 92c752998bSGaetan Rivet break; 93c752998bSGaetan Rivet default: 94c752998bSGaetan Rivet RTE_LOG(DEBUG, EAL, 95c752998bSGaetan Rivet " Not managed by a supported kernel driver, skipped\n"); 96c752998bSGaetan Rivet break; 97c752998bSGaetan Rivet } 98c752998bSGaetan Rivet } 99c752998bSGaetan Rivet 100c752998bSGaetan Rivet void 101c752998bSGaetan Rivet pci_uio_free_resource(struct rte_pci_device *dev, 102c752998bSGaetan Rivet struct mapped_pci_resource *uio_res) 103c752998bSGaetan Rivet { 104c752998bSGaetan Rivet rte_free(uio_res); 105c752998bSGaetan Rivet 106c752998bSGaetan Rivet if (dev->intr_handle.fd) { 107c752998bSGaetan Rivet close(dev->intr_handle.fd); 108c752998bSGaetan Rivet dev->intr_handle.fd = -1; 109c752998bSGaetan Rivet dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN; 110c752998bSGaetan Rivet } 111c752998bSGaetan Rivet } 112c752998bSGaetan Rivet 113c752998bSGaetan Rivet int 114c752998bSGaetan Rivet pci_uio_alloc_resource(struct rte_pci_device *dev, 115c752998bSGaetan Rivet struct mapped_pci_resource **uio_res) 116c752998bSGaetan Rivet { 117c752998bSGaetan Rivet char devname[PATH_MAX]; /* contains the /dev/uioX */ 118c752998bSGaetan Rivet struct rte_pci_addr *loc; 119c752998bSGaetan Rivet 120c752998bSGaetan Rivet loc = &dev->addr; 121c752998bSGaetan Rivet 122c752998bSGaetan Rivet snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u", 123c752998bSGaetan Rivet dev->addr.bus, dev->addr.devid, dev->addr.function); 124c752998bSGaetan Rivet 125c752998bSGaetan Rivet if (access(devname, O_RDWR) < 0) { 126c752998bSGaetan Rivet RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, " 127c752998bSGaetan Rivet "skipping\n", loc->domain, loc->bus, loc->devid, loc->function); 128c752998bSGaetan Rivet return 1; 129c752998bSGaetan Rivet } 130c752998bSGaetan Rivet 131c752998bSGaetan Rivet /* save fd if in primary process */ 132c752998bSGaetan Rivet dev->intr_handle.fd = open(devname, O_RDWR); 133c752998bSGaetan Rivet if (dev->intr_handle.fd < 0) { 134c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", 135c752998bSGaetan Rivet devname, strerror(errno)); 136c752998bSGaetan Rivet goto error; 137c752998bSGaetan Rivet } 138c752998bSGaetan Rivet dev->intr_handle.type = RTE_INTR_HANDLE_UIO; 139c752998bSGaetan Rivet 140c752998bSGaetan Rivet /* allocate the mapping details for secondary processes*/ 141c752998bSGaetan Rivet *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0); 142c752998bSGaetan Rivet if (*uio_res == NULL) { 143c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 144c752998bSGaetan Rivet "%s(): cannot store uio mmap details\n", __func__); 145c752998bSGaetan Rivet goto error; 146c752998bSGaetan Rivet } 147c752998bSGaetan Rivet 148c752998bSGaetan Rivet snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname); 149c752998bSGaetan Rivet memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr)); 150c752998bSGaetan Rivet 151c752998bSGaetan Rivet return 0; 152c752998bSGaetan Rivet 153c752998bSGaetan Rivet error: 154c752998bSGaetan Rivet pci_uio_free_resource(dev, *uio_res); 155c752998bSGaetan Rivet return -1; 156c752998bSGaetan Rivet } 157c752998bSGaetan Rivet 158c752998bSGaetan Rivet int 159c752998bSGaetan Rivet pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx, 160c752998bSGaetan Rivet struct mapped_pci_resource *uio_res, int map_idx) 161c752998bSGaetan Rivet { 162c752998bSGaetan Rivet int fd; 163c752998bSGaetan Rivet char *devname; 164c752998bSGaetan Rivet void *mapaddr; 165c752998bSGaetan Rivet uint64_t offset; 166c752998bSGaetan Rivet uint64_t pagesz; 167c752998bSGaetan Rivet struct pci_map *maps; 168c752998bSGaetan Rivet 169c752998bSGaetan Rivet maps = uio_res->maps; 170c752998bSGaetan Rivet devname = uio_res->path; 171c752998bSGaetan Rivet pagesz = sysconf(_SC_PAGESIZE); 172c752998bSGaetan Rivet 173c752998bSGaetan Rivet /* allocate memory to keep path */ 174c752998bSGaetan Rivet maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0); 175c752998bSGaetan Rivet if (maps[map_idx].path == NULL) { 176c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n", 177c752998bSGaetan Rivet strerror(errno)); 178c752998bSGaetan Rivet return -1; 179c752998bSGaetan Rivet } 180c752998bSGaetan Rivet 181c752998bSGaetan Rivet /* 182c752998bSGaetan Rivet * open resource file, to mmap it 183c752998bSGaetan Rivet */ 184c752998bSGaetan Rivet fd = open(devname, O_RDWR); 185c752998bSGaetan Rivet if (fd < 0) { 186c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", 187c752998bSGaetan Rivet devname, strerror(errno)); 188c752998bSGaetan Rivet goto error; 189c752998bSGaetan Rivet } 190c752998bSGaetan Rivet 191c752998bSGaetan Rivet /* if matching map is found, then use it */ 192c752998bSGaetan Rivet offset = res_idx * pagesz; 193c752998bSGaetan Rivet mapaddr = pci_map_resource(NULL, fd, (off_t)offset, 194c752998bSGaetan Rivet (size_t)dev->mem_resource[res_idx].len, 0); 195c752998bSGaetan Rivet close(fd); 196c752998bSGaetan Rivet if (mapaddr == MAP_FAILED) 197c752998bSGaetan Rivet goto error; 198c752998bSGaetan Rivet 199c752998bSGaetan Rivet maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr; 200c752998bSGaetan Rivet maps[map_idx].size = dev->mem_resource[res_idx].len; 201c752998bSGaetan Rivet maps[map_idx].addr = mapaddr; 202c752998bSGaetan Rivet maps[map_idx].offset = offset; 203c752998bSGaetan Rivet strcpy(maps[map_idx].path, devname); 204c752998bSGaetan Rivet dev->mem_resource[res_idx].addr = mapaddr; 205c752998bSGaetan Rivet 206c752998bSGaetan Rivet return 0; 207c752998bSGaetan Rivet 208c752998bSGaetan Rivet error: 209c752998bSGaetan Rivet rte_free(maps[map_idx].path); 210c752998bSGaetan Rivet return -1; 211c752998bSGaetan Rivet } 212c752998bSGaetan Rivet 213c752998bSGaetan Rivet static int 214c752998bSGaetan Rivet pci_scan_one(int dev_pci_fd, struct pci_conf *conf) 215c752998bSGaetan Rivet { 216c752998bSGaetan Rivet struct rte_pci_device *dev; 217c752998bSGaetan Rivet struct pci_bar_io bar; 218c752998bSGaetan Rivet unsigned i, max; 219c752998bSGaetan Rivet 220c752998bSGaetan Rivet dev = malloc(sizeof(*dev)); 221c752998bSGaetan Rivet if (dev == NULL) { 222c752998bSGaetan Rivet return -1; 223c752998bSGaetan Rivet } 224c752998bSGaetan Rivet 225c752998bSGaetan Rivet memset(dev, 0, sizeof(*dev)); 2266844d146SThomas Monjalon dev->device.bus = &rte_pci_bus.bus; 2276844d146SThomas Monjalon 228c752998bSGaetan Rivet dev->addr.domain = conf->pc_sel.pc_domain; 229c752998bSGaetan Rivet dev->addr.bus = conf->pc_sel.pc_bus; 230c752998bSGaetan Rivet dev->addr.devid = conf->pc_sel.pc_dev; 231c752998bSGaetan Rivet dev->addr.function = conf->pc_sel.pc_func; 232c752998bSGaetan Rivet 233c752998bSGaetan Rivet /* get vendor id */ 234c752998bSGaetan Rivet dev->id.vendor_id = conf->pc_vendor; 235c752998bSGaetan Rivet 236c752998bSGaetan Rivet /* get device id */ 237c752998bSGaetan Rivet dev->id.device_id = conf->pc_device; 238c752998bSGaetan Rivet 239c752998bSGaetan Rivet /* get subsystem_vendor id */ 240c752998bSGaetan Rivet dev->id.subsystem_vendor_id = conf->pc_subvendor; 241c752998bSGaetan Rivet 242c752998bSGaetan Rivet /* get subsystem_device id */ 243c752998bSGaetan Rivet dev->id.subsystem_device_id = conf->pc_subdevice; 244c752998bSGaetan Rivet 245c752998bSGaetan Rivet /* get class id */ 246c752998bSGaetan Rivet dev->id.class_id = (conf->pc_class << 16) | 247c752998bSGaetan Rivet (conf->pc_subclass << 8) | 248c752998bSGaetan Rivet (conf->pc_progif); 249c752998bSGaetan Rivet 250c752998bSGaetan Rivet /* TODO: get max_vfs */ 251c752998bSGaetan Rivet dev->max_vfs = 0; 252c752998bSGaetan Rivet 253c752998bSGaetan Rivet /* FreeBSD has no NUMA support (yet) */ 254c752998bSGaetan Rivet dev->device.numa_node = 0; 255c752998bSGaetan Rivet 256c752998bSGaetan Rivet pci_name_set(dev); 257c752998bSGaetan Rivet 258c752998bSGaetan Rivet /* FreeBSD has only one pass through driver */ 259c752998bSGaetan Rivet dev->kdrv = RTE_KDRV_NIC_UIO; 260c752998bSGaetan Rivet 261c752998bSGaetan Rivet /* parse resources */ 262c752998bSGaetan Rivet switch (conf->pc_hdr & PCIM_HDRTYPE) { 263c752998bSGaetan Rivet case PCIM_HDRTYPE_NORMAL: 264c752998bSGaetan Rivet max = PCIR_MAX_BAR_0; 265c752998bSGaetan Rivet break; 266c752998bSGaetan Rivet case PCIM_HDRTYPE_BRIDGE: 267c752998bSGaetan Rivet max = PCIR_MAX_BAR_1; 268c752998bSGaetan Rivet break; 269c752998bSGaetan Rivet case PCIM_HDRTYPE_CARDBUS: 270c752998bSGaetan Rivet max = PCIR_MAX_BAR_2; 271c752998bSGaetan Rivet break; 272c752998bSGaetan Rivet default: 273c752998bSGaetan Rivet goto skipdev; 274c752998bSGaetan Rivet } 275c752998bSGaetan Rivet 276c752998bSGaetan Rivet for (i = 0; i <= max; i++) { 277c752998bSGaetan Rivet bar.pbi_sel = conf->pc_sel; 278c752998bSGaetan Rivet bar.pbi_reg = PCIR_BAR(i); 279c752998bSGaetan Rivet if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0) 280c752998bSGaetan Rivet continue; 281c752998bSGaetan Rivet 282c752998bSGaetan Rivet dev->mem_resource[i].len = bar.pbi_length; 283c752998bSGaetan Rivet if (PCI_BAR_IO(bar.pbi_base)) { 284c752998bSGaetan Rivet dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf)); 285c752998bSGaetan Rivet continue; 286c752998bSGaetan Rivet } 287c752998bSGaetan Rivet dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf); 288c752998bSGaetan Rivet } 289c752998bSGaetan Rivet 290c752998bSGaetan Rivet /* device is valid, add in list (sorted) */ 291c752998bSGaetan Rivet if (TAILQ_EMPTY(&rte_pci_bus.device_list)) { 292c752998bSGaetan Rivet rte_pci_add_device(dev); 293c752998bSGaetan Rivet } 294c752998bSGaetan Rivet else { 295c752998bSGaetan Rivet struct rte_pci_device *dev2 = NULL; 296c752998bSGaetan Rivet int ret; 297c752998bSGaetan Rivet 298c752998bSGaetan Rivet TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) { 2990e3ef055SGaetan Rivet ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr); 300c752998bSGaetan Rivet if (ret > 0) 301c752998bSGaetan Rivet continue; 302c752998bSGaetan Rivet else if (ret < 0) { 303c752998bSGaetan Rivet rte_pci_insert_device(dev2, dev); 304c752998bSGaetan Rivet } else { /* already registered */ 305c752998bSGaetan Rivet dev2->kdrv = dev->kdrv; 306c752998bSGaetan Rivet dev2->max_vfs = dev->max_vfs; 307c752998bSGaetan Rivet pci_name_set(dev2); 308c752998bSGaetan Rivet memmove(dev2->mem_resource, 309c752998bSGaetan Rivet dev->mem_resource, 310c752998bSGaetan Rivet sizeof(dev->mem_resource)); 311c752998bSGaetan Rivet free(dev); 312c752998bSGaetan Rivet } 313c752998bSGaetan Rivet return 0; 314c752998bSGaetan Rivet } 315c752998bSGaetan Rivet rte_pci_add_device(dev); 316c752998bSGaetan Rivet } 317c752998bSGaetan Rivet 318c752998bSGaetan Rivet return 0; 319c752998bSGaetan Rivet 320c752998bSGaetan Rivet skipdev: 321c752998bSGaetan Rivet free(dev); 322c752998bSGaetan Rivet return 0; 323c752998bSGaetan Rivet } 324c752998bSGaetan Rivet 325c752998bSGaetan Rivet /* 326c752998bSGaetan Rivet * Scan the content of the PCI bus, and add the devices in the devices 327c752998bSGaetan Rivet * list. Call pci_scan_one() for each pci entry found. 328c752998bSGaetan Rivet */ 329c752998bSGaetan Rivet int 330c752998bSGaetan Rivet rte_pci_scan(void) 331c752998bSGaetan Rivet { 332c752998bSGaetan Rivet int fd; 333c752998bSGaetan Rivet unsigned dev_count = 0; 334c752998bSGaetan Rivet struct pci_conf matches[16]; 335c752998bSGaetan Rivet struct pci_conf_io conf_io = { 336c752998bSGaetan Rivet .pat_buf_len = 0, 337c752998bSGaetan Rivet .num_patterns = 0, 338c752998bSGaetan Rivet .patterns = NULL, 339c752998bSGaetan Rivet .match_buf_len = sizeof(matches), 340c752998bSGaetan Rivet .matches = &matches[0], 341c752998bSGaetan Rivet }; 342c752998bSGaetan Rivet 343c752998bSGaetan Rivet /* for debug purposes, PCI can be disabled */ 344c752998bSGaetan Rivet if (!rte_eal_has_pci()) 345c752998bSGaetan Rivet return 0; 346c752998bSGaetan Rivet 347c752998bSGaetan Rivet fd = open("/dev/pci", O_RDONLY); 348c752998bSGaetan Rivet if (fd < 0) { 349c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__); 350c752998bSGaetan Rivet goto error; 351c752998bSGaetan Rivet } 352c752998bSGaetan Rivet 353c752998bSGaetan Rivet do { 354c752998bSGaetan Rivet unsigned i; 355c752998bSGaetan Rivet if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) { 356c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n", 357c752998bSGaetan Rivet __func__, strerror(errno)); 358c752998bSGaetan Rivet goto error; 359c752998bSGaetan Rivet } 360c752998bSGaetan Rivet 361c752998bSGaetan Rivet for (i = 0; i < conf_io.num_matches; i++) 362c752998bSGaetan Rivet if (pci_scan_one(fd, &matches[i]) < 0) 363c752998bSGaetan Rivet goto error; 364c752998bSGaetan Rivet 365c752998bSGaetan Rivet dev_count += conf_io.num_matches; 366c752998bSGaetan Rivet } while(conf_io.status == PCI_GETCONF_MORE_DEVS); 367c752998bSGaetan Rivet 368c752998bSGaetan Rivet close(fd); 369c752998bSGaetan Rivet 370c752998bSGaetan Rivet RTE_LOG(DEBUG, EAL, "PCI scan found %u devices\n", dev_count); 371c752998bSGaetan Rivet return 0; 372c752998bSGaetan Rivet 373c752998bSGaetan Rivet error: 374c752998bSGaetan Rivet if (fd >= 0) 375c752998bSGaetan Rivet close(fd); 376c752998bSGaetan Rivet return -1; 377c752998bSGaetan Rivet } 378c752998bSGaetan Rivet 379c752998bSGaetan Rivet /* 380c752998bSGaetan Rivet * Get iommu class of PCI devices on the bus. 381c752998bSGaetan Rivet */ 382c752998bSGaetan Rivet enum rte_iova_mode 383c752998bSGaetan Rivet rte_pci_get_iommu_class(void) 384c752998bSGaetan Rivet { 385c752998bSGaetan Rivet /* Supports only RTE_KDRV_NIC_UIO */ 386c752998bSGaetan Rivet return RTE_IOVA_PA; 387c752998bSGaetan Rivet } 388c752998bSGaetan Rivet 389c752998bSGaetan Rivet int 390c752998bSGaetan Rivet pci_update_device(const struct rte_pci_addr *addr) 391c752998bSGaetan Rivet { 392c752998bSGaetan Rivet int fd; 393c752998bSGaetan Rivet struct pci_conf matches[2]; 394c752998bSGaetan Rivet struct pci_match_conf match = { 395c752998bSGaetan Rivet .pc_sel = { 396c752998bSGaetan Rivet .pc_domain = addr->domain, 397c752998bSGaetan Rivet .pc_bus = addr->bus, 398c752998bSGaetan Rivet .pc_dev = addr->devid, 399c752998bSGaetan Rivet .pc_func = addr->function, 400c752998bSGaetan Rivet }, 401c752998bSGaetan Rivet }; 402c752998bSGaetan Rivet struct pci_conf_io conf_io = { 403c752998bSGaetan Rivet .pat_buf_len = 0, 404c752998bSGaetan Rivet .num_patterns = 1, 405c752998bSGaetan Rivet .patterns = &match, 406c752998bSGaetan Rivet .match_buf_len = sizeof(matches), 407c752998bSGaetan Rivet .matches = &matches[0], 408c752998bSGaetan Rivet }; 409c752998bSGaetan Rivet 410c752998bSGaetan Rivet fd = open("/dev/pci", O_RDONLY); 411c752998bSGaetan Rivet if (fd < 0) { 412c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__); 413c752998bSGaetan Rivet goto error; 414c752998bSGaetan Rivet } 415c752998bSGaetan Rivet 416c752998bSGaetan Rivet if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) { 417c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n", 418c752998bSGaetan Rivet __func__, strerror(errno)); 419c752998bSGaetan Rivet goto error; 420c752998bSGaetan Rivet } 421c752998bSGaetan Rivet 422c752998bSGaetan Rivet if (conf_io.num_matches != 1) 423c752998bSGaetan Rivet goto error; 424c752998bSGaetan Rivet 425c752998bSGaetan Rivet if (pci_scan_one(fd, &matches[0]) < 0) 426c752998bSGaetan Rivet goto error; 427c752998bSGaetan Rivet 428c752998bSGaetan Rivet close(fd); 429c752998bSGaetan Rivet 430c752998bSGaetan Rivet return 0; 431c752998bSGaetan Rivet 432c752998bSGaetan Rivet error: 433c752998bSGaetan Rivet if (fd >= 0) 434c752998bSGaetan Rivet close(fd); 435c752998bSGaetan Rivet return -1; 436c752998bSGaetan Rivet } 437c752998bSGaetan Rivet 438c752998bSGaetan Rivet /* Read PCI config space. */ 439c752998bSGaetan Rivet int rte_pci_read_config(const struct rte_pci_device *dev, 440c752998bSGaetan Rivet void *buf, size_t len, off_t offset) 441c752998bSGaetan Rivet { 442c752998bSGaetan Rivet int fd = -1; 443c752998bSGaetan Rivet int size; 444*e8d435f1SLuca Boccassi /* Copy Linux implementation's behaviour */ 445*e8d435f1SLuca Boccassi const int return_len = len; 446c752998bSGaetan Rivet struct pci_io pi = { 447c752998bSGaetan Rivet .pi_sel = { 448c752998bSGaetan Rivet .pc_domain = dev->addr.domain, 449c752998bSGaetan Rivet .pc_bus = dev->addr.bus, 450c752998bSGaetan Rivet .pc_dev = dev->addr.devid, 451c752998bSGaetan Rivet .pc_func = dev->addr.function, 452c752998bSGaetan Rivet }, 453c752998bSGaetan Rivet .pi_reg = offset, 454c752998bSGaetan Rivet }; 455c752998bSGaetan Rivet 456c752998bSGaetan Rivet fd = open("/dev/pci", O_RDWR); 457c752998bSGaetan Rivet if (fd < 0) { 458c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__); 459c752998bSGaetan Rivet goto error; 460c752998bSGaetan Rivet } 461c752998bSGaetan Rivet 462c752998bSGaetan Rivet while (len > 0) { 463c752998bSGaetan Rivet size = (len >= 4) ? 4 : ((len >= 2) ? 2 : 1); 464c752998bSGaetan Rivet pi.pi_width = size; 465c752998bSGaetan Rivet 466c752998bSGaetan Rivet if (ioctl(fd, PCIOCREAD, &pi) < 0) 467c752998bSGaetan Rivet goto error; 468c752998bSGaetan Rivet memcpy(buf, &pi.pi_data, size); 469c752998bSGaetan Rivet 470c752998bSGaetan Rivet buf = (char *)buf + size; 471c752998bSGaetan Rivet pi.pi_reg += size; 472c752998bSGaetan Rivet len -= size; 473c752998bSGaetan Rivet } 474c752998bSGaetan Rivet close(fd); 475c752998bSGaetan Rivet 476*e8d435f1SLuca Boccassi return return_len; 477c752998bSGaetan Rivet 478c752998bSGaetan Rivet error: 479c752998bSGaetan Rivet if (fd >= 0) 480c752998bSGaetan Rivet close(fd); 481c752998bSGaetan Rivet return -1; 482c752998bSGaetan Rivet } 483c752998bSGaetan Rivet 484c752998bSGaetan Rivet /* Write PCI config space. */ 485c752998bSGaetan Rivet int rte_pci_write_config(const struct rte_pci_device *dev, 486c752998bSGaetan Rivet const void *buf, size_t len, off_t offset) 487c752998bSGaetan Rivet { 488c752998bSGaetan Rivet int fd = -1; 489c752998bSGaetan Rivet 490c752998bSGaetan Rivet struct pci_io pi = { 491c752998bSGaetan Rivet .pi_sel = { 492c752998bSGaetan Rivet .pc_domain = dev->addr.domain, 493c752998bSGaetan Rivet .pc_bus = dev->addr.bus, 494c752998bSGaetan Rivet .pc_dev = dev->addr.devid, 495c752998bSGaetan Rivet .pc_func = dev->addr.function, 496c752998bSGaetan Rivet }, 497c752998bSGaetan Rivet .pi_reg = offset, 498c752998bSGaetan Rivet .pi_data = *(const uint32_t *)buf, 499c752998bSGaetan Rivet .pi_width = len, 500c752998bSGaetan Rivet }; 501c752998bSGaetan Rivet 502c752998bSGaetan Rivet if (len == 3 || len > sizeof(pi.pi_data)) { 503c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__); 504c752998bSGaetan Rivet goto error; 505c752998bSGaetan Rivet } 506c752998bSGaetan Rivet 507c752998bSGaetan Rivet memcpy(&pi.pi_data, buf, len); 508c752998bSGaetan Rivet 509c752998bSGaetan Rivet fd = open("/dev/pci", O_RDWR); 510c752998bSGaetan Rivet if (fd < 0) { 511c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__); 512c752998bSGaetan Rivet goto error; 513c752998bSGaetan Rivet } 514c752998bSGaetan Rivet 515c752998bSGaetan Rivet if (ioctl(fd, PCIOCWRITE, &pi) < 0) 516c752998bSGaetan Rivet goto error; 517c752998bSGaetan Rivet 518c752998bSGaetan Rivet close(fd); 519c752998bSGaetan Rivet return 0; 520c752998bSGaetan Rivet 521c752998bSGaetan Rivet error: 522c752998bSGaetan Rivet if (fd >= 0) 523c752998bSGaetan Rivet close(fd); 524c752998bSGaetan Rivet return -1; 525c752998bSGaetan Rivet } 526c752998bSGaetan Rivet 527c752998bSGaetan Rivet int 528c752998bSGaetan Rivet rte_pci_ioport_map(struct rte_pci_device *dev, int bar, 529c752998bSGaetan Rivet struct rte_pci_ioport *p) 530c752998bSGaetan Rivet { 531c752998bSGaetan Rivet int ret; 532c752998bSGaetan Rivet 533c752998bSGaetan Rivet switch (dev->kdrv) { 534c752998bSGaetan Rivet #if defined(RTE_ARCH_X86) 535c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 536c752998bSGaetan Rivet if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) { 537c752998bSGaetan Rivet p->base = (uintptr_t)dev->mem_resource[bar].addr; 538c752998bSGaetan Rivet ret = 0; 539c752998bSGaetan Rivet } else 540c752998bSGaetan Rivet ret = -1; 541c752998bSGaetan Rivet break; 542c752998bSGaetan Rivet #endif 543c752998bSGaetan Rivet default: 544c752998bSGaetan Rivet ret = -1; 545c752998bSGaetan Rivet break; 546c752998bSGaetan Rivet } 547c752998bSGaetan Rivet 548c752998bSGaetan Rivet if (!ret) 549c752998bSGaetan Rivet p->dev = dev; 550c752998bSGaetan Rivet 551c752998bSGaetan Rivet return ret; 552c752998bSGaetan Rivet } 553c752998bSGaetan Rivet 554c752998bSGaetan Rivet static void 555c752998bSGaetan Rivet pci_uio_ioport_read(struct rte_pci_ioport *p, 556c752998bSGaetan Rivet void *data, size_t len, off_t offset) 557c752998bSGaetan Rivet { 558c752998bSGaetan Rivet #if defined(RTE_ARCH_X86) 559c752998bSGaetan Rivet uint8_t *d; 560c752998bSGaetan Rivet int size; 561c752998bSGaetan Rivet unsigned short reg = p->base + offset; 562c752998bSGaetan Rivet 563c752998bSGaetan Rivet for (d = data; len > 0; d += size, reg += size, len -= size) { 564c752998bSGaetan Rivet if (len >= 4) { 565c752998bSGaetan Rivet size = 4; 566c752998bSGaetan Rivet *(uint32_t *)d = inl(reg); 567c752998bSGaetan Rivet } else if (len >= 2) { 568c752998bSGaetan Rivet size = 2; 569c752998bSGaetan Rivet *(uint16_t *)d = inw(reg); 570c752998bSGaetan Rivet } else { 571c752998bSGaetan Rivet size = 1; 572c752998bSGaetan Rivet *d = inb(reg); 573c752998bSGaetan Rivet } 574c752998bSGaetan Rivet } 575c752998bSGaetan Rivet #else 576c752998bSGaetan Rivet RTE_SET_USED(p); 577c752998bSGaetan Rivet RTE_SET_USED(data); 578c752998bSGaetan Rivet RTE_SET_USED(len); 579c752998bSGaetan Rivet RTE_SET_USED(offset); 580c752998bSGaetan Rivet #endif 581c752998bSGaetan Rivet } 582c752998bSGaetan Rivet 583c752998bSGaetan Rivet void 584c752998bSGaetan Rivet rte_pci_ioport_read(struct rte_pci_ioport *p, 585c752998bSGaetan Rivet void *data, size_t len, off_t offset) 586c752998bSGaetan Rivet { 587c752998bSGaetan Rivet switch (p->dev->kdrv) { 588c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 589c752998bSGaetan Rivet pci_uio_ioport_read(p, data, len, offset); 590c752998bSGaetan Rivet break; 591c752998bSGaetan Rivet default: 592c752998bSGaetan Rivet break; 593c752998bSGaetan Rivet } 594c752998bSGaetan Rivet } 595c752998bSGaetan Rivet 596c752998bSGaetan Rivet static void 597c752998bSGaetan Rivet pci_uio_ioport_write(struct rte_pci_ioport *p, 598c752998bSGaetan Rivet const void *data, size_t len, off_t offset) 599c752998bSGaetan Rivet { 600c752998bSGaetan Rivet #if defined(RTE_ARCH_X86) 601c752998bSGaetan Rivet const uint8_t *s; 602c752998bSGaetan Rivet int size; 603c752998bSGaetan Rivet unsigned short reg = p->base + offset; 604c752998bSGaetan Rivet 605c752998bSGaetan Rivet for (s = data; len > 0; s += size, reg += size, len -= size) { 606c752998bSGaetan Rivet if (len >= 4) { 607c752998bSGaetan Rivet size = 4; 608c752998bSGaetan Rivet outl(reg, *(const uint32_t *)s); 609c752998bSGaetan Rivet } else if (len >= 2) { 610c752998bSGaetan Rivet size = 2; 611c752998bSGaetan Rivet outw(reg, *(const uint16_t *)s); 612c752998bSGaetan Rivet } else { 613c752998bSGaetan Rivet size = 1; 614c752998bSGaetan Rivet outb(reg, *s); 615c752998bSGaetan Rivet } 616c752998bSGaetan Rivet } 617c752998bSGaetan Rivet #else 618c752998bSGaetan Rivet RTE_SET_USED(p); 619c752998bSGaetan Rivet RTE_SET_USED(data); 620c752998bSGaetan Rivet RTE_SET_USED(len); 621c752998bSGaetan Rivet RTE_SET_USED(offset); 622c752998bSGaetan Rivet #endif 623c752998bSGaetan Rivet } 624c752998bSGaetan Rivet 625c752998bSGaetan Rivet void 626c752998bSGaetan Rivet rte_pci_ioport_write(struct rte_pci_ioport *p, 627c752998bSGaetan Rivet const void *data, size_t len, off_t offset) 628c752998bSGaetan Rivet { 629c752998bSGaetan Rivet switch (p->dev->kdrv) { 630c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 631c752998bSGaetan Rivet pci_uio_ioport_write(p, data, len, offset); 632c752998bSGaetan Rivet break; 633c752998bSGaetan Rivet default: 634c752998bSGaetan Rivet break; 635c752998bSGaetan Rivet } 636c752998bSGaetan Rivet } 637c752998bSGaetan Rivet 638c752998bSGaetan Rivet int 639c752998bSGaetan Rivet rte_pci_ioport_unmap(struct rte_pci_ioport *p) 640c752998bSGaetan Rivet { 641c752998bSGaetan Rivet int ret; 642c752998bSGaetan Rivet 643c752998bSGaetan Rivet switch (p->dev->kdrv) { 644c752998bSGaetan Rivet #if defined(RTE_ARCH_X86) 645c752998bSGaetan Rivet case RTE_KDRV_NIC_UIO: 646c752998bSGaetan Rivet ret = 0; 647c752998bSGaetan Rivet break; 648c752998bSGaetan Rivet #endif 649c752998bSGaetan Rivet default: 650c752998bSGaetan Rivet ret = -1; 651c752998bSGaetan Rivet break; 652c752998bSGaetan Rivet } 653c752998bSGaetan Rivet 654c752998bSGaetan Rivet return ret; 655c752998bSGaetan Rivet } 656