4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <rte_tailq.h>
43 #include <rte_eal_memconfig.h>
44 #include <rte_malloc.h>
45 #include <rte_devargs.h>
47 #include "rte_pci_dev_ids.h"
48 #include "eal_filesystem.h"
49 #include "eal_private.h"
53 * PCI probing under linux
55 * This code is used to simulate a PCI probe by parsing information in sysfs.
56 * When a registered device matches a driver, it is then initialized with
57 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
68 * For multi-process we need to reproduce all PCI mappings in secondary
69 * processes, so save them in a tailq.
71 struct mapped_pci_resource {
72 TAILQ_ENTRY(mapped_pci_resource) next;
74 struct rte_pci_addr pci_addr;
77 struct pci_map maps[PCI_MAX_RESOURCE];
80 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
81 static struct mapped_pci_res_list *pci_res_list;
83 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
85 /* unbind kernel driver for this device */
87 pci_unbind_kernel_driver(struct rte_pci_device *dev)
91 char filename[PATH_MAX];
93 struct rte_pci_addr *loc = &dev->addr;
95 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
96 rte_snprintf(filename, sizeof(filename),
97 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
98 loc->domain, loc->bus, loc->devid, loc->function);
100 f = fopen(filename, "w");
101 if (f == NULL) /* device was not bound */
104 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
105 loc->domain, loc->bus, loc->devid, loc->function);
106 if ((n < 0) || (n >= (int)sizeof(buf))) {
107 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
110 if (fwrite(buf, n, 1, f) == 0) {
111 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
124 /* map a particular resource from a file */
126 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size)
130 /* Map the PCI memory resource of device */
131 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
132 MAP_SHARED, fd, offset);
133 if (mapaddr == MAP_FAILED ||
134 (requested_addr != NULL && mapaddr != requested_addr)) {
135 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
136 __func__, fd, requested_addr,
137 (unsigned long)size, (unsigned long)offset,
138 strerror(errno), mapaddr);
142 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
150 #define OFF_MAX ((uint64_t)(off_t)-1)
152 pci_uio_get_mappings(const char *devname, struct pci_map maps[], int nb_maps)
155 char dirname[PATH_MAX];
156 char filename[PATH_MAX];
157 uint64_t offset, size;
159 for (i = 0; i != nb_maps; i++) {
161 /* check if map directory exists */
162 rte_snprintf(dirname, sizeof(dirname),
163 "%s/maps/map%u", devname, i);
165 if (access(dirname, F_OK) != 0)
168 /* get mapping offset */
169 rte_snprintf(filename, sizeof(filename),
170 "%s/offset", dirname);
171 if (pci_parse_sysfs_value(filename, &offset) < 0) {
173 "%s(): cannot parse offset of %s\n",
178 /* get mapping size */
179 rte_snprintf(filename, sizeof(filename),
181 if (pci_parse_sysfs_value(filename, &size) < 0) {
183 "%s(): cannot parse size of %s\n",
188 /* get mapping physical address */
189 rte_snprintf(filename, sizeof(filename),
191 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
193 "%s(): cannot parse addr of %s\n",
198 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
200 "%s(): offset/size exceed system max value\n",
205 maps[i].offset = offset;
212 pci_uio_map_secondary(struct rte_pci_device *dev)
215 struct mapped_pci_resource *uio_res;
217 TAILQ_FOREACH(uio_res, pci_res_list, next) {
219 /* skip this element if it doesn't match our PCI address */
220 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
223 for (i = 0; i != uio_res->nb_maps; i++) {
225 * open devname, to mmap it
227 fd = open(uio_res->path, O_RDWR);
229 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
230 uio_res->path, strerror(errno));
234 if (pci_map_resource(uio_res->maps[i].addr, fd,
235 (off_t)uio_res->maps[i].offset,
236 (size_t)uio_res->maps[i].size)
237 != uio_res->maps[i].addr) {
239 "Cannot mmap device resource\n");
243 /* fd is not needed in slave process, close it */
249 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
254 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
257 char filename[PATH_MAX];
259 unsigned major, minor;
262 /* get the name of the sysfs file that contains the major and minor
263 * of the uio device and read its content */
264 rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
266 f = fopen(filename, "r");
268 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
273 ret = fscanf(f, "%d:%d", &major, &minor);
275 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
282 /* create the char device "mknod /dev/uioX c major minor" */
283 rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
284 dev = makedev(major, minor);
285 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
287 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
288 __func__, strerror(errno));
296 * Return the uioX char device used for a pci device. On success, return
297 * the UIO number and fill dstbuf string with the path of the device in
298 * sysfs. On error, return a negative value. In this case dstbuf is
302 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
305 struct rte_pci_addr *loc = &dev->addr;
306 unsigned int uio_num;
309 char dirname[PATH_MAX];
311 /* depending on kernel version, uio can be located in uio/uioX
314 rte_snprintf(dirname, sizeof(dirname),
315 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
316 loc->domain, loc->bus, loc->devid, loc->function);
318 dir = opendir(dirname);
320 /* retry with the parent directory */
321 rte_snprintf(dirname, sizeof(dirname),
322 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
323 loc->domain, loc->bus, loc->devid, loc->function);
324 dir = opendir(dirname);
327 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
332 /* take the first file starting with "uio" */
333 while ((e = readdir(dir)) != NULL) {
334 /* format could be uio%d ...*/
335 int shortprefix_len = sizeof("uio") - 1;
336 /* ... or uio:uio%d */
337 int longprefix_len = sizeof("uio:uio") - 1;
340 if (strncmp(e->d_name, "uio", 3) != 0)
343 /* first try uio%d */
345 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
346 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
347 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
351 /* then try uio:uio%d */
353 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
354 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
355 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
361 /* No uio resource found */
365 /* create uio device if we've been asked to */
366 if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
367 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
372 /* map the PCI resource of a PCI device in virtual memory */
374 pci_uio_map_resource(struct rte_pci_device *dev)
377 char dirname[PATH_MAX];
378 char devname[PATH_MAX]; /* contains the /dev/uioX */
385 struct rte_pci_addr *loc = &dev->addr;
386 struct mapped_pci_resource *uio_res;
387 struct pci_map *maps;
389 dev->intr_handle.fd = -1;
390 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
392 /* secondary processes - use already recorded details */
393 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
394 return (pci_uio_map_secondary(dev));
396 /* find uio resource */
397 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
399 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
400 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
403 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
405 /* save fd if in primary process */
406 dev->intr_handle.fd = open(devname, O_RDWR);
407 if (dev->intr_handle.fd < 0) {
408 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
409 devname, strerror(errno));
412 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
414 /* allocate the mapping details for secondary processes*/
415 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
417 "%s(): cannot store uio mmap details\n", __func__);
421 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
422 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
424 /* collect info about device mappings */
425 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
426 RTE_DIM(uio_res->maps));
432 uio_res->nb_maps = nb_maps;
435 pagesz = sysconf(_SC_PAGESIZE);
437 maps = uio_res->maps;
438 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
442 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
445 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
446 dev->mem_resource[i].len != maps[j].size);
450 /* if matching map is found, then use it */
455 * open devname, to mmap it
457 fd = open(devname, O_RDWR);
459 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
460 devname, strerror(errno));
464 if (maps[j].addr != NULL ||
465 (mapaddr = pci_map_resource(NULL, fd,
467 (size_t)maps[j].size)
475 maps[j].addr = mapaddr;
476 maps[j].offset = offset;
477 dev->mem_resource[i].addr = mapaddr;
481 TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);
486 /* parse the "resource" sysfs file */
487 #define IORESOURCE_MEM 0x00000200
490 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
494 union pci_resource_info {
500 char *ptrs[PCI_RESOURCE_FMT_NVAL];
503 uint64_t phys_addr, end_addr, flags;
505 f = fopen(filename, "r");
507 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
511 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
513 if (fgets(buf, sizeof(buf), f) == NULL) {
515 "%s(): cannot read resource\n", __func__);
519 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
521 "%s(): bad resource format\n", __func__);
525 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
526 end_addr = strtoull(res_info.end_addr, NULL, 16);
527 flags = strtoull(res_info.flags, NULL, 16);
530 "%s(): bad resource format\n", __func__);
534 if (flags & IORESOURCE_MEM) {
535 dev->mem_resource[i].phys_addr = phys_addr;
536 dev->mem_resource[i].len = end_addr - phys_addr + 1;
537 /* not mapped for now */
538 dev->mem_resource[i].addr = NULL;
550 * parse a sysfs file containing one integer value
551 * different to the eal version, as it needs to work with 64-bit values
554 pci_parse_sysfs_value(const char *filename, uint64_t *val)
560 f = fopen(filename, "r");
562 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
567 if (fgets(buf, sizeof(buf), f) == NULL) {
568 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
573 *val = strtoull(buf, &end, 0);
574 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
575 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
584 /* Compare two PCI device addresses. */
586 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
588 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
589 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
591 if (dev_addr > dev_addr2)
598 /* Scan one pci sysfs entry, and fill the devices list from it. */
600 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
601 uint8_t devid, uint8_t function)
603 char filename[PATH_MAX];
605 struct rte_pci_device *dev;
607 dev = malloc(sizeof(*dev));
612 memset(dev, 0, sizeof(*dev));
613 dev->addr.domain = domain;
615 dev->addr.devid = devid;
616 dev->addr.function = function;
619 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
620 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
624 dev->id.vendor_id = (uint16_t)tmp;
627 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
628 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
632 dev->id.device_id = (uint16_t)tmp;
634 /* get subsystem_vendor id */
635 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
637 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
641 dev->id.subsystem_vendor_id = (uint16_t)tmp;
643 /* get subsystem_device id */
644 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
646 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
650 dev->id.subsystem_device_id = (uint16_t)tmp;
654 rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
655 if (!access(filename, F_OK) &&
656 eal_parse_sysfs_value(filename, &tmp) == 0) {
657 dev->max_vfs = (uint16_t)tmp;
661 rte_snprintf(filename, sizeof(filename), "%s/numa_node",
663 if (access(filename, R_OK) != 0) {
664 /* if no NUMA support just set node to 0 */
667 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
671 dev->numa_node = tmp;
674 /* parse resources */
675 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
676 if (pci_parse_sysfs_resource(filename, dev) < 0) {
677 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
682 /* device is valid, add in list (sorted) */
683 if (TAILQ_EMPTY(&pci_device_list)) {
684 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
687 struct rte_pci_device *dev2 = NULL;
689 TAILQ_FOREACH(dev2, &pci_device_list, next) {
690 if (pci_addr_comparison(&dev->addr, &dev2->addr))
693 TAILQ_INSERT_BEFORE(dev2, dev, next);
697 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
704 * split up a pci address into its constituent parts.
707 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
708 uint8_t *bus, uint8_t *devid, uint8_t *function)
710 /* first split on ':' */
718 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
721 char *buf_copy = strndup(buf, bufsize);
722 if (buf_copy == NULL)
725 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
728 /* final split is on '.' between devid and function */
729 splitaddr.function = strchr(splitaddr.devid,'.');
730 if (splitaddr.function == NULL)
732 *splitaddr.function++ = '\0';
734 /* now convert to int values */
736 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
737 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
738 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
739 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
743 free(buf_copy); /* free the copy made with strdup */
751 * Scan the content of the PCI bus, and the devices in the devices
759 char dirname[PATH_MAX];
761 uint8_t bus, devid, function;
763 dir = opendir(SYSFS_PCI_DEVICES);
765 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
766 __func__, strerror(errno));
770 while ((e = readdir(dir)) != NULL) {
771 if (e->d_name[0] == '.')
774 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
775 &bus, &devid, &function) != 0)
778 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
780 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
792 * If vendor/device ID match, call the devinit() function of the
796 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
798 struct rte_pci_id *id_table;
800 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
802 /* check if device's identifiers match the driver's ones */
803 if (id_table->vendor_id != dev->id.vendor_id &&
804 id_table->vendor_id != PCI_ANY_ID)
806 if (id_table->device_id != dev->id.device_id &&
807 id_table->device_id != PCI_ANY_ID)
809 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
810 id_table->subsystem_vendor_id != PCI_ANY_ID)
812 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
813 id_table->subsystem_device_id != PCI_ANY_ID)
816 struct rte_pci_addr *loc = &dev->addr;
818 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
819 loc->domain, loc->bus, loc->devid, loc->function,
822 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
823 dev->id.device_id, dr->name);
825 /* no initialization when blacklisted, return without error */
826 if (dev->devargs != NULL &&
827 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
828 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
832 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
833 /* map resources for devices that use igb_uio */
834 if (pci_uio_map_resource(dev) < 0)
836 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
837 rte_eal_process_type() == RTE_PROC_PRIMARY) {
838 /* unbind current driver */
839 if (pci_unbind_kernel_driver(dev) < 0)
843 /* reference driver structure */
846 /* call the driver devinit() function */
847 return dr->devinit(dr, dev);
849 /* return positive value if driver is not found */
853 /* Init the PCI EAL subsystem */
855 rte_eal_pci_init(void)
857 TAILQ_INIT(&pci_driver_list);
858 TAILQ_INIT(&pci_device_list);
859 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
860 mapped_pci_res_list);
862 /* for debug purposes, PCI can be disabled */
863 if (internal_config.no_pci)
866 if (pci_scan() < 0) {
867 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);