15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 25566a3e3SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 3c752998bSGaetan Rivet */ 4c752998bSGaetan Rivet 5c752998bSGaetan Rivet #include <string.h> 6c752998bSGaetan Rivet #include <dirent.h> 7c752998bSGaetan Rivet 8c752998bSGaetan Rivet #include <rte_log.h> 9c752998bSGaetan Rivet #include <rte_pci.h> 10c752998bSGaetan Rivet #include <rte_bus_pci.h> 11c752998bSGaetan Rivet #include <rte_malloc.h> 12c752998bSGaetan Rivet #include <rte_devargs.h> 13c752998bSGaetan Rivet #include <rte_memcpy.h> 14c752998bSGaetan Rivet #include <rte_vfio.h> 15c752998bSGaetan Rivet 16c752998bSGaetan Rivet #include "eal_filesystem.h" 17c752998bSGaetan Rivet 18c752998bSGaetan Rivet #include "private.h" 19c752998bSGaetan Rivet #include "pci_init.h" 20c752998bSGaetan Rivet 21c752998bSGaetan Rivet /** 22c752998bSGaetan Rivet * @file 23aa777f00SThomas Monjalon * PCI probing using Linux sysfs. 24c752998bSGaetan Rivet */ 25c752998bSGaetan Rivet 26c752998bSGaetan Rivet static int 2752f711f7SAndy Green pci_get_kernel_driver_by_path(const char *filename, char *dri_name, 2852f711f7SAndy Green size_t len) 29c752998bSGaetan Rivet { 30c752998bSGaetan Rivet int count; 31c752998bSGaetan Rivet char path[PATH_MAX]; 32c752998bSGaetan Rivet char *name; 33c752998bSGaetan Rivet 34c752998bSGaetan Rivet if (!filename || !dri_name) 35c752998bSGaetan Rivet return -1; 36c752998bSGaetan Rivet 37c752998bSGaetan Rivet count = readlink(filename, path, PATH_MAX); 38c752998bSGaetan Rivet if (count >= PATH_MAX) 39c752998bSGaetan Rivet return -1; 40c752998bSGaetan Rivet 41c752998bSGaetan Rivet /* For device does not have a driver */ 42c752998bSGaetan Rivet if (count < 0) 43c752998bSGaetan Rivet return 1; 44c752998bSGaetan Rivet 45c752998bSGaetan Rivet path[count] = '\0'; 46c752998bSGaetan Rivet 47c752998bSGaetan Rivet name = strrchr(path, '/'); 48c752998bSGaetan Rivet if (name) { 4952f711f7SAndy Green strlcpy(dri_name, name + 1, len); 50c752998bSGaetan Rivet return 0; 51c752998bSGaetan Rivet } 52c752998bSGaetan Rivet 53c752998bSGaetan Rivet return -1; 54c752998bSGaetan Rivet } 55c752998bSGaetan Rivet 56c752998bSGaetan Rivet /* Map pci device */ 57c752998bSGaetan Rivet int 58c752998bSGaetan Rivet rte_pci_map_device(struct rte_pci_device *dev) 59c752998bSGaetan Rivet { 60c752998bSGaetan Rivet int ret = -1; 61c752998bSGaetan Rivet 62c752998bSGaetan Rivet /* try mapping the NIC resources using VFIO if it exists */ 63c752998bSGaetan Rivet switch (dev->kdrv) { 647c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 65c752998bSGaetan Rivet #ifdef VFIO_PRESENT 66c752998bSGaetan Rivet if (pci_vfio_is_enabled()) 67c752998bSGaetan Rivet ret = pci_vfio_map_resource(dev); 68c752998bSGaetan Rivet #endif 69c752998bSGaetan Rivet break; 707c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 717c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 72c752998bSGaetan Rivet if (rte_eal_using_phys_addrs()) { 73c752998bSGaetan Rivet /* map resources for devices that use uio */ 74c752998bSGaetan Rivet ret = pci_uio_map_resource(dev); 75c752998bSGaetan Rivet } 76c752998bSGaetan Rivet break; 77c752998bSGaetan Rivet default: 78c752998bSGaetan Rivet RTE_LOG(DEBUG, EAL, 79c752998bSGaetan Rivet " Not managed by a supported kernel driver, skipped\n"); 80c752998bSGaetan Rivet ret = 1; 81c752998bSGaetan Rivet break; 82c752998bSGaetan Rivet } 83c752998bSGaetan Rivet 84c752998bSGaetan Rivet return ret; 85c752998bSGaetan Rivet } 86c752998bSGaetan Rivet 87c752998bSGaetan Rivet /* Unmap pci device */ 88c752998bSGaetan Rivet void 89c752998bSGaetan Rivet rte_pci_unmap_device(struct rte_pci_device *dev) 90c752998bSGaetan Rivet { 91c752998bSGaetan Rivet /* try unmapping the NIC resources using VFIO if it exists */ 92c752998bSGaetan Rivet switch (dev->kdrv) { 937c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 94c752998bSGaetan Rivet #ifdef VFIO_PRESENT 95c752998bSGaetan Rivet if (pci_vfio_is_enabled()) 96c752998bSGaetan Rivet pci_vfio_unmap_resource(dev); 97c752998bSGaetan Rivet #endif 98c752998bSGaetan Rivet break; 997c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 1007c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 101c752998bSGaetan Rivet /* unmap resources for devices that use uio */ 102c752998bSGaetan Rivet pci_uio_unmap_resource(dev); 103c752998bSGaetan Rivet break; 104c752998bSGaetan Rivet default: 105c752998bSGaetan Rivet RTE_LOG(DEBUG, EAL, 106c752998bSGaetan Rivet " Not managed by a supported kernel driver, skipped\n"); 107c752998bSGaetan Rivet break; 108c752998bSGaetan Rivet } 109c752998bSGaetan Rivet } 110c752998bSGaetan Rivet 1117411d032SAnatoly Burakov static int 11266cc45e2SAnatoly Burakov find_max_end_va(const struct rte_memseg_list *msl, void *arg) 1137411d032SAnatoly Burakov { 1144104b2a4SAnatoly Burakov size_t sz = msl->len; 11566cc45e2SAnatoly Burakov void *end_va = RTE_PTR_ADD(msl->base_va, sz); 1167411d032SAnatoly Burakov void **max_va = arg; 1177411d032SAnatoly Burakov 1187411d032SAnatoly Burakov if (*max_va < end_va) 1197411d032SAnatoly Burakov *max_va = end_va; 1207411d032SAnatoly Burakov return 0; 1217411d032SAnatoly Burakov } 1227411d032SAnatoly Burakov 123c752998bSGaetan Rivet void * 124c752998bSGaetan Rivet pci_find_max_end_va(void) 125c752998bSGaetan Rivet { 1267411d032SAnatoly Burakov void *va = NULL; 127c752998bSGaetan Rivet 12866cc45e2SAnatoly Burakov rte_memseg_list_walk(find_max_end_va, &va); 1297411d032SAnatoly Burakov return va; 130c752998bSGaetan Rivet } 131c752998bSGaetan Rivet 13266cc45e2SAnatoly Burakov 133c752998bSGaetan Rivet /* parse one line of the "resource" sysfs file (note that the 'line' 134c752998bSGaetan Rivet * string is modified) 135c752998bSGaetan Rivet */ 136c752998bSGaetan Rivet int 137c752998bSGaetan Rivet pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr, 138c752998bSGaetan Rivet uint64_t *end_addr, uint64_t *flags) 139c752998bSGaetan Rivet { 140c752998bSGaetan Rivet union pci_resource_info { 141c752998bSGaetan Rivet struct { 142c752998bSGaetan Rivet char *phys_addr; 143c752998bSGaetan Rivet char *end_addr; 144c752998bSGaetan Rivet char *flags; 145c752998bSGaetan Rivet }; 146c752998bSGaetan Rivet char *ptrs[PCI_RESOURCE_FMT_NVAL]; 147c752998bSGaetan Rivet } res_info; 148c752998bSGaetan Rivet 149c752998bSGaetan Rivet if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) { 150c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 151c752998bSGaetan Rivet "%s(): bad resource format\n", __func__); 152c752998bSGaetan Rivet return -1; 153c752998bSGaetan Rivet } 154c752998bSGaetan Rivet errno = 0; 155c752998bSGaetan Rivet *phys_addr = strtoull(res_info.phys_addr, NULL, 16); 156c752998bSGaetan Rivet *end_addr = strtoull(res_info.end_addr, NULL, 16); 157c752998bSGaetan Rivet *flags = strtoull(res_info.flags, NULL, 16); 158c752998bSGaetan Rivet if (errno != 0) { 159c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 160c752998bSGaetan Rivet "%s(): bad resource format\n", __func__); 161c752998bSGaetan Rivet return -1; 162c752998bSGaetan Rivet } 163c752998bSGaetan Rivet 164c752998bSGaetan Rivet return 0; 165c752998bSGaetan Rivet } 166c752998bSGaetan Rivet 167c752998bSGaetan Rivet /* parse the "resource" sysfs file */ 168c752998bSGaetan Rivet static int 169c752998bSGaetan Rivet pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev) 170c752998bSGaetan Rivet { 171c752998bSGaetan Rivet FILE *f; 172c752998bSGaetan Rivet char buf[BUFSIZ]; 173c752998bSGaetan Rivet int i; 174c752998bSGaetan Rivet uint64_t phys_addr, end_addr, flags; 175c752998bSGaetan Rivet 176c752998bSGaetan Rivet f = fopen(filename, "r"); 177c752998bSGaetan Rivet if (f == NULL) { 178c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n"); 179c752998bSGaetan Rivet return -1; 180c752998bSGaetan Rivet } 181c752998bSGaetan Rivet 182c752998bSGaetan Rivet for (i = 0; i<PCI_MAX_RESOURCE; i++) { 183c752998bSGaetan Rivet 184c752998bSGaetan Rivet if (fgets(buf, sizeof(buf), f) == NULL) { 185c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 186c752998bSGaetan Rivet "%s(): cannot read resource\n", __func__); 187c752998bSGaetan Rivet goto error; 188c752998bSGaetan Rivet } 189c752998bSGaetan Rivet if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr, 190c752998bSGaetan Rivet &end_addr, &flags) < 0) 191c752998bSGaetan Rivet goto error; 192c752998bSGaetan Rivet 193c752998bSGaetan Rivet if (flags & IORESOURCE_MEM) { 194c752998bSGaetan Rivet dev->mem_resource[i].phys_addr = phys_addr; 195c752998bSGaetan Rivet dev->mem_resource[i].len = end_addr - phys_addr + 1; 196c752998bSGaetan Rivet /* not mapped for now */ 197c752998bSGaetan Rivet dev->mem_resource[i].addr = NULL; 198c752998bSGaetan Rivet } 199c752998bSGaetan Rivet } 200c752998bSGaetan Rivet fclose(f); 201c752998bSGaetan Rivet return 0; 202c752998bSGaetan Rivet 203c752998bSGaetan Rivet error: 204c752998bSGaetan Rivet fclose(f); 205c752998bSGaetan Rivet return -1; 206c752998bSGaetan Rivet } 207c752998bSGaetan Rivet 208c752998bSGaetan Rivet /* Scan one pci sysfs entry, and fill the devices list from it. */ 209c752998bSGaetan Rivet static int 210c752998bSGaetan Rivet pci_scan_one(const char *dirname, const struct rte_pci_addr *addr) 211c752998bSGaetan Rivet { 212c752998bSGaetan Rivet char filename[PATH_MAX]; 213c752998bSGaetan Rivet unsigned long tmp; 21487a02023SChenbo Xia struct rte_pci_device_internal *pdev; 215c752998bSGaetan Rivet struct rte_pci_device *dev; 216c752998bSGaetan Rivet char driver[PATH_MAX]; 217c752998bSGaetan Rivet int ret; 218c752998bSGaetan Rivet 21987a02023SChenbo Xia pdev = malloc(sizeof(*pdev)); 22087a02023SChenbo Xia if (pdev == NULL) { 22187a02023SChenbo Xia RTE_LOG(ERR, EAL, "Cannot allocate memory for internal pci device\n"); 222c752998bSGaetan Rivet return -1; 22387a02023SChenbo Xia } 224c752998bSGaetan Rivet 22587a02023SChenbo Xia memset(pdev, 0, sizeof(*pdev)); 22687a02023SChenbo Xia dev = &pdev->device; 2276844d146SThomas Monjalon dev->device.bus = &rte_pci_bus.bus; 228c752998bSGaetan Rivet dev->addr = *addr; 229c752998bSGaetan Rivet 230c752998bSGaetan Rivet /* get vendor id */ 231c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/vendor", dirname); 232c752998bSGaetan Rivet if (eal_parse_sysfs_value(filename, &tmp) < 0) { 23387a02023SChenbo Xia pci_free(pdev); 234c752998bSGaetan Rivet return -1; 235c752998bSGaetan Rivet } 236c752998bSGaetan Rivet dev->id.vendor_id = (uint16_t)tmp; 237c752998bSGaetan Rivet 238c752998bSGaetan Rivet /* get device id */ 239c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/device", dirname); 240c752998bSGaetan Rivet if (eal_parse_sysfs_value(filename, &tmp) < 0) { 24187a02023SChenbo Xia pci_free(pdev); 242c752998bSGaetan Rivet return -1; 243c752998bSGaetan Rivet } 244c752998bSGaetan Rivet dev->id.device_id = (uint16_t)tmp; 245c752998bSGaetan Rivet 246c752998bSGaetan Rivet /* get subsystem_vendor id */ 247c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/subsystem_vendor", 248c752998bSGaetan Rivet dirname); 249c752998bSGaetan Rivet if (eal_parse_sysfs_value(filename, &tmp) < 0) { 25087a02023SChenbo Xia pci_free(pdev); 251c752998bSGaetan Rivet return -1; 252c752998bSGaetan Rivet } 253c752998bSGaetan Rivet dev->id.subsystem_vendor_id = (uint16_t)tmp; 254c752998bSGaetan Rivet 255c752998bSGaetan Rivet /* get subsystem_device id */ 256c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/subsystem_device", 257c752998bSGaetan Rivet dirname); 258c752998bSGaetan Rivet if (eal_parse_sysfs_value(filename, &tmp) < 0) { 25987a02023SChenbo Xia pci_free(pdev); 260c752998bSGaetan Rivet return -1; 261c752998bSGaetan Rivet } 262c752998bSGaetan Rivet dev->id.subsystem_device_id = (uint16_t)tmp; 263c752998bSGaetan Rivet 264c752998bSGaetan Rivet /* get class_id */ 265c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/class", 266c752998bSGaetan Rivet dirname); 267c752998bSGaetan Rivet if (eal_parse_sysfs_value(filename, &tmp) < 0) { 26887a02023SChenbo Xia pci_free(pdev); 269c752998bSGaetan Rivet return -1; 270c752998bSGaetan Rivet } 271c752998bSGaetan Rivet /* the least 24 bits are valid: class, subclass, program interface */ 272c752998bSGaetan Rivet dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID; 273c752998bSGaetan Rivet 274c752998bSGaetan Rivet /* get max_vfs */ 275c752998bSGaetan Rivet dev->max_vfs = 0; 276c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/max_vfs", dirname); 277c752998bSGaetan Rivet if (!access(filename, F_OK) && 278c752998bSGaetan Rivet eal_parse_sysfs_value(filename, &tmp) == 0) 279c752998bSGaetan Rivet dev->max_vfs = (uint16_t)tmp; 280c752998bSGaetan Rivet else { 281c752998bSGaetan Rivet /* for non igb_uio driver, need kernel version >= 3.8 */ 282c752998bSGaetan Rivet snprintf(filename, sizeof(filename), 283c752998bSGaetan Rivet "%s/sriov_numvfs", dirname); 284c752998bSGaetan Rivet if (!access(filename, F_OK) && 285c752998bSGaetan Rivet eal_parse_sysfs_value(filename, &tmp) == 0) 286c752998bSGaetan Rivet dev->max_vfs = (uint16_t)tmp; 287c752998bSGaetan Rivet } 288c752998bSGaetan Rivet 289c752998bSGaetan Rivet /* get numa node, default to 0 if not present */ 2907dcd73e3SOlivier Matz snprintf(filename, sizeof(filename), "%s/numa_node", dirname); 291c752998bSGaetan Rivet 2927dcd73e3SOlivier Matz if (access(filename, F_OK) == 0 && 2937dcd73e3SOlivier Matz eal_parse_sysfs_value(filename, &tmp) == 0) 294c752998bSGaetan Rivet dev->device.numa_node = tmp; 295c752998bSGaetan Rivet else 2967dcd73e3SOlivier Matz dev->device.numa_node = SOCKET_ID_ANY; 297c752998bSGaetan Rivet 2988f4de2dbSDavid Marchand pci_common_set(dev); 299c752998bSGaetan Rivet 300c752998bSGaetan Rivet /* parse resources */ 301c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/resource", dirname); 302c752998bSGaetan Rivet if (pci_parse_sysfs_resource(filename, dev) < 0) { 303c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__); 30487a02023SChenbo Xia pci_free(pdev); 305c752998bSGaetan Rivet return -1; 306c752998bSGaetan Rivet } 307c752998bSGaetan Rivet 308c752998bSGaetan Rivet /* parse driver */ 309c752998bSGaetan Rivet snprintf(filename, sizeof(filename), "%s/driver", dirname); 31052f711f7SAndy Green ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver)); 311c752998bSGaetan Rivet if (ret < 0) { 312c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "Fail to get kernel driver\n"); 31387a02023SChenbo Xia pci_free(pdev); 314c752998bSGaetan Rivet return -1; 315c752998bSGaetan Rivet } 316c752998bSGaetan Rivet 317c752998bSGaetan Rivet if (!ret) { 318c752998bSGaetan Rivet if (!strcmp(driver, "vfio-pci")) 3197c0d798aSDavid Marchand dev->kdrv = RTE_PCI_KDRV_VFIO; 320c752998bSGaetan Rivet else if (!strcmp(driver, "igb_uio")) 3217c0d798aSDavid Marchand dev->kdrv = RTE_PCI_KDRV_IGB_UIO; 322c752998bSGaetan Rivet else if (!strcmp(driver, "uio_pci_generic")) 3237c0d798aSDavid Marchand dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC; 324c752998bSGaetan Rivet else 3257c0d798aSDavid Marchand dev->kdrv = RTE_PCI_KDRV_UNKNOWN; 326c79a1c67SJerin Jacob } else { 32787a02023SChenbo Xia pci_free(pdev); 328c79a1c67SJerin Jacob return 0; 329c79a1c67SJerin Jacob } 330c752998bSGaetan Rivet /* device is valid, add in list (sorted) */ 331c752998bSGaetan Rivet if (TAILQ_EMPTY(&rte_pci_bus.device_list)) { 332c752998bSGaetan Rivet rte_pci_add_device(dev); 333c752998bSGaetan Rivet } else { 334c752998bSGaetan Rivet struct rte_pci_device *dev2; 335c752998bSGaetan Rivet int ret; 336c752998bSGaetan Rivet 337c752998bSGaetan Rivet TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) { 3380e3ef055SGaetan Rivet ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr); 339c752998bSGaetan Rivet if (ret > 0) 340c752998bSGaetan Rivet continue; 341c752998bSGaetan Rivet 342c752998bSGaetan Rivet if (ret < 0) { 343c752998bSGaetan Rivet rte_pci_insert_device(dev2, dev); 344c752998bSGaetan Rivet } else { /* already registered */ 34555e411b3SQi Zhang if (!rte_dev_is_probed(&dev2->device)) { 346c752998bSGaetan Rivet dev2->kdrv = dev->kdrv; 347c752998bSGaetan Rivet dev2->max_vfs = dev->max_vfs; 3489d3ad80aSJim Harris dev2->id = dev->id; 3498f4de2dbSDavid Marchand pci_common_set(dev2); 35055e411b3SQi Zhang memmove(dev2->mem_resource, 35155e411b3SQi Zhang dev->mem_resource, 352c752998bSGaetan Rivet sizeof(dev->mem_resource)); 35355e411b3SQi Zhang } else { 35455e411b3SQi Zhang /** 35555e411b3SQi Zhang * If device is plugged and driver is 35655e411b3SQi Zhang * probed already, (This happens when 35755e411b3SQi Zhang * we call rte_dev_probe which will 35855e411b3SQi Zhang * scan all device on the bus) we don't 35955e411b3SQi Zhang * need to do anything here unless... 36055e411b3SQi Zhang **/ 36155e411b3SQi Zhang if (dev2->kdrv != dev->kdrv || 3629d3ad80aSJim Harris dev2->max_vfs != dev->max_vfs || 3639d3ad80aSJim Harris memcmp(&dev2->id, &dev->id, sizeof(dev2->id))) 36455e411b3SQi Zhang /* 36555e411b3SQi Zhang * This should not happens. 36655e411b3SQi Zhang * But it is still possible if 36755e411b3SQi Zhang * we unbind a device from 36855e411b3SQi Zhang * vfio or uio before hotplug 36955e411b3SQi Zhang * remove and rebind it with 37055e411b3SQi Zhang * a different configure. 37155e411b3SQi Zhang * So we just print out the 37255e411b3SQi Zhang * error as an alarm. 37355e411b3SQi Zhang */ 37455e411b3SQi Zhang RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n", 37555e411b3SQi Zhang filename); 376fc67ae91SSomnath Kotur else if (dev2->device.devargs != 377fc67ae91SSomnath Kotur dev->device.devargs) { 378fc67ae91SSomnath Kotur rte_devargs_remove(dev2->device.devargs); 3798f4de2dbSDavid Marchand pci_common_set(dev2); 380fc67ae91SSomnath Kotur } 38155e411b3SQi Zhang } 38287a02023SChenbo Xia pci_free(pdev); 383c752998bSGaetan Rivet } 384c752998bSGaetan Rivet return 0; 385c752998bSGaetan Rivet } 386c752998bSGaetan Rivet 387c752998bSGaetan Rivet rte_pci_add_device(dev); 388c752998bSGaetan Rivet } 389c752998bSGaetan Rivet 390c752998bSGaetan Rivet return 0; 391c752998bSGaetan Rivet } 392c752998bSGaetan Rivet 393c752998bSGaetan Rivet /* 394c752998bSGaetan Rivet * split up a pci address into its constituent parts. 395c752998bSGaetan Rivet */ 396c752998bSGaetan Rivet static int 397c752998bSGaetan Rivet parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr) 398c752998bSGaetan Rivet { 399c752998bSGaetan Rivet /* first split on ':' */ 400c752998bSGaetan Rivet union splitaddr { 401c752998bSGaetan Rivet struct { 402c752998bSGaetan Rivet char *domain; 403c752998bSGaetan Rivet char *bus; 404c752998bSGaetan Rivet char *devid; 405c752998bSGaetan Rivet char *function; 406c752998bSGaetan Rivet }; 407c752998bSGaetan Rivet char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */ 408c752998bSGaetan Rivet } splitaddr; 409c752998bSGaetan Rivet 410c752998bSGaetan Rivet char *buf_copy = strndup(buf, bufsize); 411c752998bSGaetan Rivet if (buf_copy == NULL) 412c752998bSGaetan Rivet return -1; 413c752998bSGaetan Rivet 414c752998bSGaetan Rivet if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':') 415c752998bSGaetan Rivet != PCI_FMT_NVAL - 1) 416c752998bSGaetan Rivet goto error; 417c752998bSGaetan Rivet /* final split is on '.' between devid and function */ 418c752998bSGaetan Rivet splitaddr.function = strchr(splitaddr.devid,'.'); 419c752998bSGaetan Rivet if (splitaddr.function == NULL) 420c752998bSGaetan Rivet goto error; 421c752998bSGaetan Rivet *splitaddr.function++ = '\0'; 422c752998bSGaetan Rivet 423c752998bSGaetan Rivet /* now convert to int values */ 424c752998bSGaetan Rivet errno = 0; 425c752998bSGaetan Rivet addr->domain = strtoul(splitaddr.domain, NULL, 16); 426c752998bSGaetan Rivet addr->bus = strtoul(splitaddr.bus, NULL, 16); 427c752998bSGaetan Rivet addr->devid = strtoul(splitaddr.devid, NULL, 16); 428c752998bSGaetan Rivet addr->function = strtoul(splitaddr.function, NULL, 10); 429c752998bSGaetan Rivet if (errno != 0) 430c752998bSGaetan Rivet goto error; 431c752998bSGaetan Rivet 432c752998bSGaetan Rivet free(buf_copy); /* free the copy made with strdup */ 433c752998bSGaetan Rivet return 0; 434c752998bSGaetan Rivet error: 435c752998bSGaetan Rivet free(buf_copy); 436c752998bSGaetan Rivet return -1; 437c752998bSGaetan Rivet } 438c752998bSGaetan Rivet 439c752998bSGaetan Rivet /* 440c752998bSGaetan Rivet * Scan the content of the PCI bus, and the devices in the devices 441c752998bSGaetan Rivet * list 442c752998bSGaetan Rivet */ 443c752998bSGaetan Rivet int 444c752998bSGaetan Rivet rte_pci_scan(void) 445c752998bSGaetan Rivet { 446c752998bSGaetan Rivet struct dirent *e; 447c752998bSGaetan Rivet DIR *dir; 448c752998bSGaetan Rivet char dirname[PATH_MAX]; 449c752998bSGaetan Rivet struct rte_pci_addr addr; 450c752998bSGaetan Rivet 451c752998bSGaetan Rivet /* for debug purposes, PCI can be disabled */ 452c752998bSGaetan Rivet if (!rte_eal_has_pci()) 453c752998bSGaetan Rivet return 0; 454c752998bSGaetan Rivet 455c52dd394SThomas Monjalon dir = opendir(rte_pci_get_sysfs_path()); 456c752998bSGaetan Rivet if (dir == NULL) { 457c752998bSGaetan Rivet RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n", 458c752998bSGaetan Rivet __func__, strerror(errno)); 459c752998bSGaetan Rivet return -1; 460c752998bSGaetan Rivet } 461c752998bSGaetan Rivet 462c752998bSGaetan Rivet while ((e = readdir(dir)) != NULL) { 463c752998bSGaetan Rivet if (e->d_name[0] == '.') 464c752998bSGaetan Rivet continue; 465c752998bSGaetan Rivet 466c752998bSGaetan Rivet if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0) 467c752998bSGaetan Rivet continue; 468c752998bSGaetan Rivet 469463a5245SSunil Kumar Kori if (rte_pci_ignore_device(&addr)) 470463a5245SSunil Kumar Kori continue; 471463a5245SSunil Kumar Kori 472c752998bSGaetan Rivet snprintf(dirname, sizeof(dirname), "%s/%s", 473c52dd394SThomas Monjalon rte_pci_get_sysfs_path(), e->d_name); 474c752998bSGaetan Rivet 475c752998bSGaetan Rivet if (pci_scan_one(dirname, &addr) < 0) 476c752998bSGaetan Rivet goto error; 477c752998bSGaetan Rivet } 478c752998bSGaetan Rivet closedir(dir); 479c752998bSGaetan Rivet return 0; 480c752998bSGaetan Rivet 481c752998bSGaetan Rivet error: 482c752998bSGaetan Rivet closedir(dir); 483c752998bSGaetan Rivet return -1; 484c752998bSGaetan Rivet } 485c752998bSGaetan Rivet 48654a328f5SMaxime Coquelin #if defined(RTE_ARCH_X86) 48766d3724bSDavid Marchand bool 48866d3724bSDavid Marchand pci_device_iommu_support_va(const struct rte_pci_device *dev) 48954a328f5SMaxime Coquelin { 49054a328f5SMaxime Coquelin #define VTD_CAP_MGAW_SHIFT 16 49154a328f5SMaxime Coquelin #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT) 492703458e1SBen Walker const struct rte_pci_addr *addr = &dev->addr; 49354a328f5SMaxime Coquelin char filename[PATH_MAX]; 49454a328f5SMaxime Coquelin FILE *fp; 49554a328f5SMaxime Coquelin uint64_t mgaw, vtd_cap_reg = 0; 49654a328f5SMaxime Coquelin 49754a328f5SMaxime Coquelin snprintf(filename, sizeof(filename), 49854a328f5SMaxime Coquelin "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap", 49954a328f5SMaxime Coquelin rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid, 50054a328f5SMaxime Coquelin addr->function); 50154a328f5SMaxime Coquelin 50254a328f5SMaxime Coquelin fp = fopen(filename, "r"); 50354a328f5SMaxime Coquelin if (fp == NULL) { 5042e8d5cf7SStephen Hemminger /* We don't have an Intel IOMMU, assume VA supported */ 5052e8d5cf7SStephen Hemminger if (errno == ENOENT) 5062e8d5cf7SStephen Hemminger return true; 5072e8d5cf7SStephen Hemminger 5082e8d5cf7SStephen Hemminger RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n", 5092e8d5cf7SStephen Hemminger __func__, filename, strerror(errno)); 51054a328f5SMaxime Coquelin return false; 51154a328f5SMaxime Coquelin } 51254a328f5SMaxime Coquelin 5132e8d5cf7SStephen Hemminger /* We have an Intel IOMMU */ 51454a328f5SMaxime Coquelin if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) { 51554a328f5SMaxime Coquelin RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename); 51654a328f5SMaxime Coquelin fclose(fp); 51754a328f5SMaxime Coquelin return false; 51854a328f5SMaxime Coquelin } 51954a328f5SMaxime Coquelin 52054a328f5SMaxime Coquelin fclose(fp); 52154a328f5SMaxime Coquelin 52254a328f5SMaxime Coquelin mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1; 52354a328f5SMaxime Coquelin 524ec200687SAlejandro Lucero /* 525ec200687SAlejandro Lucero * Assuming there is no limitation by now. We can not know at this point 526ec200687SAlejandro Lucero * because the memory has not been initialized yet. Setting the dma mask 527ec200687SAlejandro Lucero * will force a check once memory initialization is done. We can not do 528ec200687SAlejandro Lucero * a fallback to IOVA PA now, but if the dma check fails, the error 529ec200687SAlejandro Lucero * message should advice for using '--iova-mode pa' if IOVA VA is the 530ec200687SAlejandro Lucero * current mode. 531ec200687SAlejandro Lucero */ 532ec200687SAlejandro Lucero rte_mem_set_dma_mask(mgaw); 533ec200687SAlejandro Lucero return true; 53454a328f5SMaxime Coquelin } 53554a328f5SMaxime Coquelin #elif defined(RTE_ARCH_PPC_64) 53666d3724bSDavid Marchand bool 53766d3724bSDavid Marchand pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev) 53854a328f5SMaxime Coquelin { 53990521573SDavid Christensen /* 540fc5bffb8SDavid Christensen * All POWER systems support an IOMMU, but only IOMMUv2 supports 541fc5bffb8SDavid Christensen * IOVA = VA in DPDK. Check contents of /proc/cpuinfo to find the 542fc5bffb8SDavid Christensen * system. 543fc5bffb8SDavid Christensen * 544fc5bffb8SDavid Christensen * Platform | Model | IOMMU | VA? | Comment 545fc5bffb8SDavid Christensen * ---------+-------+---------+-----+--------------------------------- 546fc5bffb8SDavid Christensen * PowerNV | N/A | IOMMUv2 | Yes | OpenPOWER (Bare Metal) 547fc5bffb8SDavid Christensen * pSeries | ~qemu | IOMMUv2 | Yes | PowerVM Logical Partition (LPAR) 548fc5bffb8SDavid Christensen * pSeries | qemu | IOMMUv1 | No | QEMU Virtual Machine 54990521573SDavid Christensen */ 55090521573SDavid Christensen 55190521573SDavid Christensen char *line = NULL; 55290521573SDavid Christensen size_t len = 0; 55390521573SDavid Christensen char filename[PATH_MAX] = "/proc/cpuinfo"; 55490521573SDavid Christensen FILE *fp = fopen(filename, "r"); 555fc5bffb8SDavid Christensen bool pseries = false, powernv = false, qemu = false; 55690521573SDavid Christensen bool ret = false; 55790521573SDavid Christensen 55890521573SDavid Christensen if (fp == NULL) { 55990521573SDavid Christensen RTE_LOG(ERR, EAL, "%s(): can't open %s: %s\n", 56090521573SDavid Christensen __func__, filename, strerror(errno)); 56190521573SDavid Christensen return ret; 56290521573SDavid Christensen } 56390521573SDavid Christensen 564fc5bffb8SDavid Christensen /* Check the "platform" and "model" fields */ 56590521573SDavid Christensen while (getline(&line, &len, fp) != -1) { 566fc5bffb8SDavid Christensen if (strstr(line, "platform") != NULL) { 56790521573SDavid Christensen if (strstr(line, "PowerNV") != NULL) { 568fc5bffb8SDavid Christensen RTE_LOG(DEBUG, EAL, "Running on a PowerNV platform\n"); 569fc5bffb8SDavid Christensen powernv = true; 570fc5bffb8SDavid Christensen } else if (strstr(line, "pSeries") != NULL) { 571fc5bffb8SDavid Christensen RTE_LOG(DEBUG, EAL, "Running on a pSeries platform\n"); 572fc5bffb8SDavid Christensen pseries = true; 573fc5bffb8SDavid Christensen } 574fc5bffb8SDavid Christensen } else if (strstr(line, "model") != NULL) { 575fc5bffb8SDavid Christensen if (strstr(line, "qemu") != NULL) { 576fc5bffb8SDavid Christensen RTE_LOG(DEBUG, EAL, "Found qemu emulation\n"); 577fc5bffb8SDavid Christensen qemu = true; 578fc5bffb8SDavid Christensen } 57990521573SDavid Christensen } 58090521573SDavid Christensen } 58190521573SDavid Christensen 58290521573SDavid Christensen free(line); 58390521573SDavid Christensen fclose(fp); 584fc5bffb8SDavid Christensen 585fc5bffb8SDavid Christensen if (powernv || (pseries && !qemu)) 586fc5bffb8SDavid Christensen ret = true; 58790521573SDavid Christensen return ret; 58854a328f5SMaxime Coquelin } 58954a328f5SMaxime Coquelin #else 59066d3724bSDavid Marchand bool 59166d3724bSDavid Marchand pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev) 59254a328f5SMaxime Coquelin { 59354a328f5SMaxime Coquelin return true; 59454a328f5SMaxime Coquelin } 59554a328f5SMaxime Coquelin #endif 59654a328f5SMaxime Coquelin 597c752998bSGaetan Rivet enum rte_iova_mode 598703458e1SBen Walker pci_device_iova_mode(const struct rte_pci_driver *pdrv, 599703458e1SBen Walker const struct rte_pci_device *pdev) 600c752998bSGaetan Rivet { 601703458e1SBen Walker enum rte_iova_mode iova_mode = RTE_IOVA_DC; 602c752998bSGaetan Rivet 603703458e1SBen Walker switch (pdev->kdrv) { 6047c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: { 605c752998bSGaetan Rivet #ifdef VFIO_PRESENT 606703458e1SBen Walker static int is_vfio_noiommu_enabled = -1; 607703458e1SBen Walker 608703458e1SBen Walker if (is_vfio_noiommu_enabled == -1) { 609703458e1SBen Walker if (rte_vfio_noiommu_is_enabled() == 1) 610703458e1SBen Walker is_vfio_noiommu_enabled = 1; 611703458e1SBen Walker else 612703458e1SBen Walker is_vfio_noiommu_enabled = 0; 613703458e1SBen Walker } 614b76fafb1SDavid Marchand if (is_vfio_noiommu_enabled != 0) 615703458e1SBen Walker iova_mode = RTE_IOVA_PA; 616d622cad8SJerin Jacob else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0) 617b76fafb1SDavid Marchand iova_mode = RTE_IOVA_VA; 618c752998bSGaetan Rivet #endif 619703458e1SBen Walker break; 620c752998bSGaetan Rivet } 621c752998bSGaetan Rivet 6227c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 6237c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 624703458e1SBen Walker iova_mode = RTE_IOVA_PA; 625703458e1SBen Walker break; 626703458e1SBen Walker 627703458e1SBen Walker default: 628d622cad8SJerin Jacob if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0) 629b76fafb1SDavid Marchand iova_mode = RTE_IOVA_VA; 630703458e1SBen Walker break; 631703458e1SBen Walker } 632703458e1SBen Walker return iova_mode; 633c752998bSGaetan Rivet } 634c752998bSGaetan Rivet 635c752998bSGaetan Rivet /* Read PCI config space. */ 636c752998bSGaetan Rivet int rte_pci_read_config(const struct rte_pci_device *device, 637c752998bSGaetan Rivet void *buf, size_t len, off_t offset) 638c752998bSGaetan Rivet { 639630deed6SAlejandro Lucero char devname[RTE_DEV_NAME_MAX_LEN] = ""; 640d61138d4SHarman Kalra const struct rte_intr_handle *intr_handle = device->intr_handle; 641c752998bSGaetan Rivet 642630deed6SAlejandro Lucero switch (device->kdrv) { 6437c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 6447c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 645c752998bSGaetan Rivet return pci_uio_read_config(intr_handle, buf, len, offset); 646c752998bSGaetan Rivet #ifdef VFIO_PRESENT 6477c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 648*4b741542SChenbo Xia return pci_vfio_read_config(device, buf, len, offset); 649c752998bSGaetan Rivet #endif 650c752998bSGaetan Rivet default: 651630deed6SAlejandro Lucero rte_pci_device_name(&device->addr, devname, 652630deed6SAlejandro Lucero RTE_DEV_NAME_MAX_LEN); 653c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 654630deed6SAlejandro Lucero "Unknown driver type for %s\n", devname); 655c752998bSGaetan Rivet return -1; 656c752998bSGaetan Rivet } 657c752998bSGaetan Rivet } 658c752998bSGaetan Rivet 659c752998bSGaetan Rivet /* Write PCI config space. */ 660c752998bSGaetan Rivet int rte_pci_write_config(const struct rte_pci_device *device, 661c752998bSGaetan Rivet const void *buf, size_t len, off_t offset) 662c752998bSGaetan Rivet { 663630deed6SAlejandro Lucero char devname[RTE_DEV_NAME_MAX_LEN] = ""; 664d61138d4SHarman Kalra const struct rte_intr_handle *intr_handle = device->intr_handle; 665c752998bSGaetan Rivet 666630deed6SAlejandro Lucero switch (device->kdrv) { 6677c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 6687c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 669c752998bSGaetan Rivet return pci_uio_write_config(intr_handle, buf, len, offset); 670c752998bSGaetan Rivet #ifdef VFIO_PRESENT 6717c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 672*4b741542SChenbo Xia return pci_vfio_write_config(device, buf, len, offset); 673c752998bSGaetan Rivet #endif 674c752998bSGaetan Rivet default: 675630deed6SAlejandro Lucero rte_pci_device_name(&device->addr, devname, 676630deed6SAlejandro Lucero RTE_DEV_NAME_MAX_LEN); 677c752998bSGaetan Rivet RTE_LOG(ERR, EAL, 678630deed6SAlejandro Lucero "Unknown driver type for %s\n", devname); 679c752998bSGaetan Rivet return -1; 680c752998bSGaetan Rivet } 681c752998bSGaetan Rivet } 682c752998bSGaetan Rivet 683c752998bSGaetan Rivet int 684c752998bSGaetan Rivet rte_pci_ioport_map(struct rte_pci_device *dev, int bar, 685c752998bSGaetan Rivet struct rte_pci_ioport *p) 686c752998bSGaetan Rivet { 687c752998bSGaetan Rivet int ret = -1; 688c752998bSGaetan Rivet 689c752998bSGaetan Rivet switch (dev->kdrv) { 690c752998bSGaetan Rivet #ifdef VFIO_PRESENT 6917c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 692c752998bSGaetan Rivet if (pci_vfio_is_enabled()) 693c752998bSGaetan Rivet ret = pci_vfio_ioport_map(dev, bar, p); 694c752998bSGaetan Rivet break; 695c752998bSGaetan Rivet #endif 6967c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 6977c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 698c752998bSGaetan Rivet ret = pci_uio_ioport_map(dev, bar, p); 699c752998bSGaetan Rivet break; 700c752998bSGaetan Rivet default: 701c752998bSGaetan Rivet break; 702c752998bSGaetan Rivet } 703c752998bSGaetan Rivet 704c752998bSGaetan Rivet if (!ret) 705c752998bSGaetan Rivet p->dev = dev; 706c752998bSGaetan Rivet 707c752998bSGaetan Rivet return ret; 708c752998bSGaetan Rivet } 709c752998bSGaetan Rivet 710c752998bSGaetan Rivet void 711c752998bSGaetan Rivet rte_pci_ioport_read(struct rte_pci_ioport *p, 712c752998bSGaetan Rivet void *data, size_t len, off_t offset) 713c752998bSGaetan Rivet { 714c752998bSGaetan Rivet switch (p->dev->kdrv) { 715c752998bSGaetan Rivet #ifdef VFIO_PRESENT 7167c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 717c752998bSGaetan Rivet pci_vfio_ioport_read(p, data, len, offset); 718c752998bSGaetan Rivet break; 719c752998bSGaetan Rivet #endif 7207c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 7217c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 722c752998bSGaetan Rivet pci_uio_ioport_read(p, data, len, offset); 723c752998bSGaetan Rivet break; 724c752998bSGaetan Rivet default: 725c752998bSGaetan Rivet break; 726c752998bSGaetan Rivet } 727c752998bSGaetan Rivet } 728c752998bSGaetan Rivet 729c752998bSGaetan Rivet void 730c752998bSGaetan Rivet rte_pci_ioport_write(struct rte_pci_ioport *p, 731c752998bSGaetan Rivet const void *data, size_t len, off_t offset) 732c752998bSGaetan Rivet { 733c752998bSGaetan Rivet switch (p->dev->kdrv) { 734c752998bSGaetan Rivet #ifdef VFIO_PRESENT 7357c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 736c752998bSGaetan Rivet pci_vfio_ioport_write(p, data, len, offset); 737c752998bSGaetan Rivet break; 738c752998bSGaetan Rivet #endif 7397c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 7407c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 741c752998bSGaetan Rivet pci_uio_ioport_write(p, data, len, offset); 742c752998bSGaetan Rivet break; 743c752998bSGaetan Rivet default: 744c752998bSGaetan Rivet break; 745c752998bSGaetan Rivet } 746c752998bSGaetan Rivet } 747c752998bSGaetan Rivet 748c752998bSGaetan Rivet int 749c752998bSGaetan Rivet rte_pci_ioport_unmap(struct rte_pci_ioport *p) 750c752998bSGaetan Rivet { 751c752998bSGaetan Rivet int ret = -1; 752c752998bSGaetan Rivet 753c752998bSGaetan Rivet switch (p->dev->kdrv) { 754c752998bSGaetan Rivet #ifdef VFIO_PRESENT 7557c0d798aSDavid Marchand case RTE_PCI_KDRV_VFIO: 756c752998bSGaetan Rivet if (pci_vfio_is_enabled()) 757c752998bSGaetan Rivet ret = pci_vfio_ioport_unmap(p); 758c752998bSGaetan Rivet break; 759c752998bSGaetan Rivet #endif 7607c0d798aSDavid Marchand case RTE_PCI_KDRV_IGB_UIO: 7617c0d798aSDavid Marchand case RTE_PCI_KDRV_UIO_GENERIC: 762c752998bSGaetan Rivet ret = pci_uio_ioport_unmap(p); 763c752998bSGaetan Rivet break; 764c752998bSGaetan Rivet default: 765c752998bSGaetan Rivet break; 766c752998bSGaetan Rivet } 767c752998bSGaetan Rivet 768c752998bSGaetan Rivet return ret; 769c752998bSGaetan Rivet } 770