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.
41 #include <sys/types.h>
48 #include <sys/queue.h>
50 #include <sys/ioctl.h>
52 #include <rte_interrupts.h>
55 #include <rte_common.h>
56 #include <rte_launch.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_tailq.h>
61 #include <rte_eal_memconfig.h>
62 #include <rte_per_lcore.h>
63 #include <rte_lcore.h>
64 #include <rte_malloc.h>
65 #include <rte_string_fns.h>
66 #include <rte_debug.h>
67 #include <rte_devargs.h>
69 #include "rte_pci_dev_ids.h"
70 #include "eal_filesystem.h"
71 #include "eal_private.h"
75 * PCI probing under linux
77 * This code is used to simulate a PCI probe by parsing information in
78 * sysfs. Moreover, when a registered driver matches a device, the
79 * kernel driver currently using it is unloaded and replaced by
80 * igb_uio module, which is a very minimal userland driver for Intel
81 * network card, only providing access to PCI BAR to applications, and
82 * enabling bus master.
93 * For multi-process we need to reproduce all PCI mappings in secondary
94 * processes, so save them in a tailq.
97 TAILQ_ENTRY(uio_resource) next;
99 struct rte_pci_addr pci_addr;
102 struct uio_map maps[PCI_MAX_RESOURCE];
105 TAILQ_HEAD(uio_res_list, uio_resource);
107 static struct uio_res_list *uio_res_list = NULL;
108 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
110 /* forward prototype of function called in pci_switch_module below */
111 static int pci_uio_map_resource(struct rte_pci_device *dev);
113 #ifdef RTE_EAL_UNBIND_PORTS
114 #define PROC_MODULES "/proc/modules"
116 #define IGB_UIO_NAME "igb_uio"
118 #define UIO_DRV_PATH "/sys/bus/pci/drivers/%s"
120 /* maximum time to wait that /dev/uioX appears */
121 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
124 * Check that a kernel module is loaded. Returns 0 on success, or if the
125 * parameter is NULL, or -1 if the module is not loaded.
128 pci_uio_check_module(const char *module_name)
134 if (module_name == NULL)
137 f = fopen(PROC_MODULES, "r");
139 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES": %s\n",
144 while(fgets(buf, sizeof(buf), f) != NULL) {
146 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
151 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
160 /* bind a PCI to the kernel module driver */
162 pci_bind_device(struct rte_pci_device *dev, char dr_path[])
167 char dev_bind[PATH_MAX];
168 struct rte_pci_addr *loc = &dev->addr;
170 n = rte_snprintf(dev_bind, sizeof(dev_bind), "%s/bind", dr_path);
171 if ((n < 0) || (n >= (int)sizeof(buf))) {
172 RTE_LOG(ERR, EAL, "Cannot rte_snprintf device bind path\n");
176 f = fopen(dev_bind, "w");
178 RTE_LOG(ERR, EAL, "Cannot open %s\n", dev_bind);
181 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
182 loc->domain, loc->bus, loc->devid, loc->function);
183 if ((n < 0) || (n >= (int)sizeof(buf))) {
184 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
188 if (fwrite(buf, n, 1, f) == 0) {
198 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
203 char uio_newid[PATH_MAX];
204 char uio_bind[PATH_MAX];
206 n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_DRV_PATH "/new_id", module_name);
207 if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
208 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
212 n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_DRV_PATH, module_name);
213 if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
214 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
218 n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
219 dev->id.vendor_id, dev->id.device_id);
220 if ((n < 0) || (n >= (int)sizeof(buf))) {
221 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
225 f = fopen(uio_newid, "w");
227 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
230 if (fwrite(buf, n, 1, f) == 0) {
236 pci_bind_device(dev, uio_bind);
240 /* unbind kernel driver for this device */
242 pci_unbind_kernel_driver(struct rte_pci_device *dev)
246 char filename[PATH_MAX];
248 struct rte_pci_addr *loc = &dev->addr;
250 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
251 rte_snprintf(filename, sizeof(filename),
252 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
253 loc->domain, loc->bus, loc->devid, loc->function);
255 f = fopen(filename, "w");
256 if (f == NULL) /* device was not bound */
259 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
260 loc->domain, loc->bus, loc->devid, loc->function);
261 if ((n < 0) || (n >= (int)sizeof(buf))) {
262 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
265 if (fwrite(buf, n, 1, f) == 0) {
266 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
281 pci_switch_module(struct rte_pci_driver *dr, struct rte_pci_device *dev,
282 int uio_status, const char *module_name)
284 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
285 /* check that our driver is loaded */
286 if (uio_status != 0 &&
287 (uio_status = pci_uio_check_module(module_name)) != 0)
288 rte_exit(EXIT_FAILURE, "The %s module is required by the "
289 "%s driver\n", module_name, dr->name);
291 /* unbind current driver, bind ours */
292 if (pci_unbind_kernel_driver(dev) < 0)
294 if (pci_uio_bind_device(dev, module_name) < 0)
297 /* map the NIC resources */
298 if (pci_uio_map_resource(dev) < 0)
304 #endif /* ifdef EAL_UNBIND_PORTS */
306 /* map a particular resource from a file */
308 pci_map_resource(void *requested_addr, const char *devname, off_t offset,
314 #ifdef RTE_EAL_UNBIND_PORTS
316 * open devname, and mmap it: it can take some time to
317 * appear, so we wait some time before returning an error
320 for (n = 0; n < UIO_DEV_WAIT_TIMEOUT*10; n++) {
322 if ((fd = open(devname, O_RDWR)) < 0 && errno != ENOENT)
328 * open devname, to mmap it
330 fd = open(devname, O_RDWR);
333 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
334 devname, strerror(errno));
338 /* Map the PCI memory resource of device */
339 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
340 MAP_SHARED, fd, offset);
342 if (mapaddr == MAP_FAILED ||
343 (requested_addr != NULL && mapaddr != requested_addr)) {
344 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
345 " %s (%p)\n", __func__, devname, fd, requested_addr,
346 (unsigned long)size, (unsigned long)offset,
347 strerror(errno), mapaddr);
351 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
359 #define OFF_MAX ((uint64_t)(off_t)-1)
361 pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps)
364 char dirname[PATH_MAX];
365 char filename[PATH_MAX];
366 uint64_t offset, size;
368 for (i = 0; i != nb_maps; i++) {
370 /* check if map directory exists */
371 rte_snprintf(dirname, sizeof(dirname),
372 "%s/maps/map%u", devname, i);
374 if (access(dirname, F_OK) != 0)
377 /* get mapping offset */
378 rte_snprintf(filename, sizeof(filename),
379 "%s/offset", dirname);
380 if (pci_parse_sysfs_value(filename, &offset) < 0) {
382 "%s(): cannot parse offset of %s\n",
387 /* get mapping size */
388 rte_snprintf(filename, sizeof(filename),
390 if (pci_parse_sysfs_value(filename, &size) < 0) {
392 "%s(): cannot parse size of %s\n",
397 /* get mapping physical address */
398 rte_snprintf(filename, sizeof(filename),
400 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
402 "%s(): cannot parse addr of %s\n",
407 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
409 "%s(): offset/size exceed system max value\n",
414 maps[i].offset = offset;
421 pci_uio_map_secondary(struct rte_pci_device *dev)
424 struct uio_resource *uio_res;
426 TAILQ_FOREACH(uio_res, uio_res_list, next) {
428 /* skip this element if it doesn't match our PCI address */
429 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
432 for (i = 0; i != uio_res->nb_maps; i++) {
433 if (pci_map_resource(uio_res->maps[i].addr,
435 (off_t)uio_res->maps[i].offset,
436 (size_t)uio_res->maps[i].size)
437 != uio_res->maps[i].addr) {
439 "Cannot mmap device resource\n");
446 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
450 static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
453 char filename[PATH_MAX];
455 unsigned major, minor;
458 /* get the name of the sysfs file that contains the major and minor
459 * of the uio device and read its content */
460 rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
462 f = fopen(filename, "r");
464 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
469 ret = fscanf(f, "%d:%d", &major, &minor);
471 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
478 /* create the char device "mknod /dev/uioX c major minor" */
479 rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
480 dev = makedev(major, minor);
481 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
483 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
484 __func__, strerror(errno));
492 * Return the uioX char device used for a pci device. On success, return
493 * the UIO number and fill dstbuf string with the path of the device in
494 * sysfs. On error, return a negative value. In this case dstbuf is
497 static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
500 struct rte_pci_addr *loc = &dev->addr;
501 unsigned int uio_num;
504 char dirname[PATH_MAX];
506 /* depending on kernel version, uio can be located in uio/uioX
509 rte_snprintf(dirname, sizeof(dirname),
510 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
511 loc->domain, loc->bus, loc->devid, loc->function);
513 dir = opendir(dirname);
515 /* retry with the parent directory */
516 rte_snprintf(dirname, sizeof(dirname),
517 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
518 loc->domain, loc->bus, loc->devid, loc->function);
519 dir = opendir(dirname);
522 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
527 /* take the first file starting with "uio" */
528 while ((e = readdir(dir)) != NULL) {
529 /* format could be uio%d ...*/
530 int shortprefix_len = sizeof("uio") - 1;
531 /* ... or uio:uio%d */
532 int longprefix_len = sizeof("uio:uio") - 1;
535 if (strncmp(e->d_name, "uio", 3) != 0)
538 /* first try uio%d */
540 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
541 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
542 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
546 /* then try uio:uio%d */
548 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
549 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
550 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
556 /* No uio resource found */
560 /* create uio device if we've been asked to */
561 if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
562 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
567 /* map the PCI resource of a PCI device in virtual memory */
569 pci_uio_map_resource(struct rte_pci_device *dev)
572 char dirname[PATH_MAX];
573 char devname[PATH_MAX]; /* contains the /dev/uioX */
580 struct rte_pci_addr *loc = &dev->addr;
581 struct uio_resource *uio_res;
582 struct uio_map *maps;
584 dev->intr_handle.fd = -1;
585 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
587 /* secondary processes - use already recorded details */
588 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
589 return (pci_uio_map_secondary(dev));
591 /* find uio resource */
592 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
594 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
595 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
598 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
600 /* save fd if in primary process */
601 dev->intr_handle.fd = open(devname, O_RDWR);
602 if (dev->intr_handle.fd < 0) {
603 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
604 devname, strerror(errno));
607 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
609 /* allocate the mapping details for secondary processes*/
610 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
612 "%s(): cannot store uio mmap details\n", __func__);
616 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
617 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
619 /* collect info about device mappings */
620 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
621 RTE_DIM(uio_res->maps));
627 uio_res->nb_maps = nb_maps;
630 pagesz = sysconf(_SC_PAGESIZE);
632 maps = uio_res->maps;
633 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
636 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
639 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
640 dev->mem_resource[i].len != maps[j].size);
644 /* if matching map is found, then use it */
647 if (maps[j].addr != NULL ||
648 (mapaddr = pci_map_resource(NULL, devname,
650 (size_t)maps[j].size)
656 maps[j].addr = mapaddr;
657 maps[j].offset = offset;
658 dev->mem_resource[i].addr = mapaddr;
662 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
667 /* parse the "resource" sysfs file */
668 #define IORESOURCE_MEM 0x00000200
671 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
675 union pci_resource_info {
681 char *ptrs[PCI_RESOURCE_FMT_NVAL];
684 uint64_t phys_addr, end_addr, flags;
686 f = fopen(filename, "r");
688 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
692 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
694 if (fgets(buf, sizeof(buf), f) == NULL) {
696 "%s(): cannot read resource\n", __func__);
700 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
702 "%s(): bad resource format\n", __func__);
706 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
707 end_addr = strtoull(res_info.end_addr, NULL, 16);
708 flags = strtoull(res_info.flags, NULL, 16);
711 "%s(): bad resource format\n", __func__);
715 if (flags & IORESOURCE_MEM) {
716 dev->mem_resource[i].phys_addr = phys_addr;
717 dev->mem_resource[i].len = end_addr - phys_addr + 1;
718 /* not mapped for now */
719 dev->mem_resource[i].addr = NULL;
731 * parse a sysfs file containing one integer value
732 * different to the eal version, as it needs to work with 64-bit values
735 pci_parse_sysfs_value(const char *filename, uint64_t *val)
741 f = fopen(filename, "r");
743 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
748 if (fgets(buf, sizeof(buf), f) == NULL) {
749 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
754 *val = strtoull(buf, &end, 0);
755 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
756 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
765 /* Compare two PCI device addresses. */
767 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
769 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
770 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
772 if (dev_addr > dev_addr2)
779 /* Scan one pci sysfs entry, and fill the devices list from it. */
781 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
782 uint8_t devid, uint8_t function)
784 char filename[PATH_MAX];
786 struct rte_pci_device *dev;
788 dev = malloc(sizeof(*dev));
793 memset(dev, 0, sizeof(*dev));
794 dev->addr.domain = domain;
796 dev->addr.devid = devid;
797 dev->addr.function = function;
800 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
801 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
805 dev->id.vendor_id = (uint16_t)tmp;
808 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
809 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
813 dev->id.device_id = (uint16_t)tmp;
815 /* get subsystem_vendor id */
816 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
818 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
822 dev->id.subsystem_vendor_id = (uint16_t)tmp;
824 /* get subsystem_device id */
825 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
827 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
831 dev->id.subsystem_device_id = (uint16_t)tmp;
835 rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
836 if (!access(filename, F_OK) &&
837 eal_parse_sysfs_value(filename, &tmp) == 0) {
838 dev->max_vfs = (uint16_t)tmp;
842 rte_snprintf(filename, sizeof(filename), "%s/numa_node",
844 if (access(filename, R_OK) != 0) {
845 /* if no NUMA support just set node to 0 */
848 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
852 dev->numa_node = tmp;
855 /* parse resources */
856 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
857 if (pci_parse_sysfs_resource(filename, dev) < 0) {
858 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
863 /* device is valid, add in list (sorted) */
864 if (TAILQ_EMPTY(&pci_device_list)) {
865 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
868 struct rte_pci_device *dev2 = NULL;
870 TAILQ_FOREACH(dev2, &pci_device_list, next) {
871 if (pci_addr_comparison(&dev->addr, &dev2->addr))
874 TAILQ_INSERT_BEFORE(dev2, dev, next);
878 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
885 * split up a pci address into its constituent parts.
888 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
889 uint8_t *bus, uint8_t *devid, uint8_t *function)
891 /* first split on ':' */
899 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
902 char *buf_copy = strndup(buf, bufsize);
903 if (buf_copy == NULL)
906 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
909 /* final split is on '.' between devid and function */
910 splitaddr.function = strchr(splitaddr.devid,'.');
911 if (splitaddr.function == NULL)
913 *splitaddr.function++ = '\0';
915 /* now convert to int values */
917 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
918 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
919 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
920 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
924 free(buf_copy); /* free the copy made with strdup */
932 * Scan the content of the PCI bus, and the devices in the devices
940 char dirname[PATH_MAX];
942 uint8_t bus, devid, function;
944 dir = opendir(SYSFS_PCI_DEVICES);
946 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
947 __func__, strerror(errno));
951 while ((e = readdir(dir)) != NULL) {
952 if (e->d_name[0] == '.')
955 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
956 &bus, &devid, &function) != 0)
959 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
961 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
973 * If vendor/device ID match, call the devinit() function of the
977 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
979 struct rte_pci_id *id_table;
981 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
983 /* check if device's identifiers match the driver's ones */
984 if (id_table->vendor_id != dev->id.vendor_id &&
985 id_table->vendor_id != PCI_ANY_ID)
987 if (id_table->device_id != dev->id.device_id &&
988 id_table->device_id != PCI_ANY_ID)
990 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
991 id_table->subsystem_vendor_id != PCI_ANY_ID)
993 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
994 id_table->subsystem_device_id != PCI_ANY_ID)
997 struct rte_pci_addr *loc = &dev->addr;
999 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
1000 loc->domain, loc->bus, loc->devid, loc->function,
1003 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
1004 dev->id.device_id, dr->name);
1006 /* no initialization when blacklisted, return without error */
1007 if (dev->devargs != NULL &&
1008 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
1009 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
1013 #ifdef RTE_EAL_UNBIND_PORTS
1014 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
1015 /* unbind driver and load uio resources for Intel NICs */
1016 if (pci_switch_module(dr, dev, 1, IGB_UIO_NAME) < 0)
1018 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
1019 rte_eal_process_type() == RTE_PROC_PRIMARY) {
1020 /* unbind current driver */
1021 if (pci_unbind_kernel_driver(dev) < 0)
1025 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
1026 /* just map resources for Intel NICs */
1027 if (pci_uio_map_resource(dev) < 0)
1031 /* reference driver structure */
1034 /* call the driver devinit() function */
1035 return dr->devinit(dr, dev);
1037 /* return positive value if driver is not found */
1041 /* Init the PCI EAL subsystem */
1043 rte_eal_pci_init(void)
1045 TAILQ_INIT(&pci_driver_list);
1046 TAILQ_INIT(&pci_device_list);
1047 uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
1049 /* for debug purposes, PCI can be disabled */
1050 if (internal_config.no_pci)
1053 if (pci_scan() < 0) {
1054 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);