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>
68 #include "rte_pci_dev_ids.h"
69 #include "eal_filesystem.h"
70 #include "eal_private.h"
74 * PCI probing under linux
76 * This code is used to simulate a PCI probe by parsing information in
77 * sysfs. Moreover, when a registered driver matches a device, the
78 * kernel driver currently using it is unloaded and replaced by
79 * igb_uio module, which is a very minimal userland driver for Intel
80 * network card, only providing access to PCI BAR to applications, and
81 * enabling bus master.
92 * For multi-process we need to reproduce all PCI mappings in secondary
93 * processes, so save them in a tailq.
96 TAILQ_ENTRY(uio_resource) next;
98 struct rte_pci_addr pci_addr;
101 struct uio_map maps[PCI_MAX_RESOURCE];
104 TAILQ_HEAD(uio_res_list, uio_resource);
106 static struct uio_res_list *uio_res_list = NULL;
107 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
109 /* forward prototype of function called in pci_switch_module below */
110 static int pci_uio_map_resource(struct rte_pci_device *dev);
112 #ifdef RTE_EAL_UNBIND_PORTS
113 #define PROC_MODULES "/proc/modules"
115 #define IGB_UIO_NAME "igb_uio"
117 #define UIO_DRV_PATH "/sys/bus/pci/drivers/%s"
119 /* maximum time to wait that /dev/uioX appears */
120 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
123 * Check that a kernel module is loaded. Returns 0 on success, or if the
124 * parameter is NULL, or -1 if the module is not loaded.
127 pci_uio_check_module(const char *module_name)
133 if (module_name == NULL)
136 f = fopen(PROC_MODULES, "r");
138 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES": %s\n",
143 while(fgets(buf, sizeof(buf), f) != NULL) {
145 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
150 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
159 /* bind a PCI to the kernel module driver */
161 pci_bind_device(struct rte_pci_device *dev, char dr_path[])
166 char dev_bind[PATH_MAX];
167 struct rte_pci_addr *loc = &dev->addr;
169 n = rte_snprintf(dev_bind, sizeof(dev_bind), "%s/bind", dr_path);
170 if ((n < 0) || (n >= (int)sizeof(buf))) {
171 RTE_LOG(ERR, EAL, "Cannot rte_snprintf device bind path\n");
175 f = fopen(dev_bind, "w");
177 RTE_LOG(ERR, EAL, "Cannot open %s\n", dev_bind);
180 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
181 loc->domain, loc->bus, loc->devid, loc->function);
182 if ((n < 0) || (n >= (int)sizeof(buf))) {
183 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
187 if (fwrite(buf, n, 1, f) == 0) {
197 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
202 char uio_newid[PATH_MAX];
203 char uio_bind[PATH_MAX];
205 n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_DRV_PATH "/new_id", module_name);
206 if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
207 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
211 n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_DRV_PATH, module_name);
212 if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
213 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
217 n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
218 dev->id.vendor_id, dev->id.device_id);
219 if ((n < 0) || (n >= (int)sizeof(buf))) {
220 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
224 f = fopen(uio_newid, "w");
226 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
229 if (fwrite(buf, n, 1, f) == 0) {
235 pci_bind_device(dev, uio_bind);
239 /* unbind kernel driver for this device */
241 pci_unbind_kernel_driver(struct rte_pci_device *dev)
245 char filename[PATH_MAX];
247 struct rte_pci_addr *loc = &dev->addr;
249 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
250 rte_snprintf(filename, sizeof(filename),
251 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
252 loc->domain, loc->bus, loc->devid, loc->function);
254 f = fopen(filename, "w");
255 if (f == NULL) /* device was not bound */
258 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
259 loc->domain, loc->bus, loc->devid, loc->function);
260 if ((n < 0) || (n >= (int)sizeof(buf))) {
261 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
264 if (fwrite(buf, n, 1, f) == 0) {
265 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
280 pci_switch_module(struct rte_pci_driver *dr, struct rte_pci_device *dev,
281 int uio_status, const char *module_name)
283 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
284 /* check that our driver is loaded */
285 if (uio_status != 0 &&
286 (uio_status = pci_uio_check_module(module_name)) != 0)
287 rte_exit(EXIT_FAILURE, "The %s module is required by the "
288 "%s driver\n", module_name, dr->name);
290 /* unbind current driver, bind ours */
291 if (pci_unbind_kernel_driver(dev) < 0)
293 if (pci_uio_bind_device(dev, module_name) < 0)
296 /* map the NIC resources */
297 if (pci_uio_map_resource(dev) < 0)
303 #endif /* ifdef EAL_UNBIND_PORTS */
305 /* map a particular resource from a file */
307 pci_map_resource(struct rte_pci_device *dev, void *requested_addr,
308 const char *devname, off_t offset, size_t size)
313 #ifdef RTE_EAL_UNBIND_PORTS
315 * open devname, and mmap it: it can take some time to
316 * appear, so we wait some time before returning an error
319 fd = dev->intr_handle.fd;
320 for (n = 0; n < UIO_DEV_WAIT_TIMEOUT*10 && fd < 0; 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);
341 if (mapaddr == MAP_FAILED ||
342 (requested_addr != NULL && mapaddr != requested_addr)) {
343 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
344 " %s (%p)\n", __func__, devname, fd, requested_addr,
345 (unsigned long)size, (unsigned long)offset,
346 strerror(errno), mapaddr);
350 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
351 /* save fd if in primary process */
352 dev->intr_handle.fd = fd;
353 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
355 /* fd is not needed in slave process, close it */
356 dev->intr_handle.fd = -1;
357 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
361 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
366 dev->intr_handle.fd = -1;
367 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
372 #define OFF_MAX ((uint64_t)(off_t)-1)
374 pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps)
377 char dirname[PATH_MAX];
378 char filename[PATH_MAX];
379 uint64_t offset, size;
381 for (i = 0; i != nb_maps; i++) {
383 /* check if map directory exists */
384 rte_snprintf(dirname, sizeof(dirname),
385 "%s/maps/map%u", devname, i);
387 if (access(dirname, F_OK) != 0)
390 /* get mapping offset */
391 rte_snprintf(filename, sizeof(filename),
392 "%s/offset", dirname);
393 if (pci_parse_sysfs_value(filename, &offset) < 0) {
395 "%s(): cannot parse offset of %s\n",
400 /* get mapping size */
401 rte_snprintf(filename, sizeof(filename),
403 if (pci_parse_sysfs_value(filename, &size) < 0) {
405 "%s(): cannot parse size of %s\n",
410 /* get mapping physical address */
411 rte_snprintf(filename, sizeof(filename),
413 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
415 "%s(): cannot parse addr of %s\n",
420 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
422 "%s(): offset/size exceed system max value\n",
427 maps[i].offset = offset;
434 pci_uio_map_secondary(struct rte_pci_device *dev)
437 struct uio_resource *uio_res;
439 TAILQ_FOREACH(uio_res, uio_res_list, next) {
441 /* skip this element if it doesn't match our PCI address */
442 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
445 for (i = 0; i != uio_res->nb_maps; i++) {
446 if (pci_map_resource(dev, uio_res->maps[i].addr,
448 (off_t)uio_res->maps[i].offset,
449 (size_t)uio_res->maps[i].size) !=
450 uio_res->maps[i].addr) {
452 "Cannot mmap device resource\n");
459 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
464 * Return the uioX char device used for a pci device. On success, return
465 * the UIO number and fill dstbuf string with the path of the device in
466 * sysfs. On error, return a negative value. In this case dstbuf is
469 static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
472 struct rte_pci_addr *loc = &dev->addr;
473 unsigned int uio_num;
476 char dirname[PATH_MAX];
478 /* depending on kernel version, uio can be located in uio/uioX
481 rte_snprintf(dirname, sizeof(dirname),
482 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
483 loc->domain, loc->bus, loc->devid, loc->function);
485 dir = opendir(dirname);
487 /* retry with the parent directory */
488 rte_snprintf(dirname, sizeof(dirname),
489 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
490 loc->domain, loc->bus, loc->devid, loc->function);
491 dir = opendir(dirname);
494 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
499 /* take the first file starting with "uio" */
500 while ((e = readdir(dir)) != NULL) {
501 /* format could be uio%d ...*/
502 int shortprefix_len = sizeof("uio") - 1;
503 /* ... or uio:uio%d */
504 int longprefix_len = sizeof("uio:uio") - 1;
507 if (strncmp(e->d_name, "uio", 3) != 0)
510 /* first try uio%d */
512 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
513 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
514 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
518 /* then try uio:uio%d */
520 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
521 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
522 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
528 /* No uio resource found */
535 /* map the PCI resource of a PCI device in virtual memory */
537 pci_uio_map_resource(struct rte_pci_device *dev)
540 char dirname[PATH_MAX];
541 char filename[PATH_MAX];
542 char devname[PATH_MAX]; /* contains the /dev/uioX */
545 unsigned long start,size;
550 struct rte_pci_addr *loc = &dev->addr;
551 struct uio_resource *uio_res;
552 struct uio_map *maps;
554 dev->intr_handle.fd = -1;
556 /* secondary processes - use already recorded details */
557 if ((rte_eal_process_type() != RTE_PROC_PRIMARY) &&
558 (dev->id.vendor_id != PCI_VENDOR_ID_QUMRANET))
559 return (pci_uio_map_secondary(dev));
561 /* find uio resource */
562 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
564 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
565 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
569 if(dev->id.vendor_id == PCI_VENDOR_ID_QUMRANET) {
570 /* get portio size */
571 rte_snprintf(filename, sizeof(filename),
572 "%s/portio/port0/size", dirname);
573 if (eal_parse_sysfs_value(filename, &size) < 0) {
574 RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
579 /* get portio start */
580 rte_snprintf(filename, sizeof(filename),
581 "%s/portio/port0/start", dirname);
582 if (eal_parse_sysfs_value(filename, &start) < 0) {
583 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
587 dev->mem_resource[0].addr = (void *)(uintptr_t)start;
588 dev->mem_resource[0].len = (uint64_t)size;
589 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx with size=0x%lx\n", start, size);
590 /* rte_virtio_pmd does not need any other bar even if available */
594 /* allocate the mapping details for secondary processes*/
595 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
597 "%s(): cannot store uio mmap details\n", __func__);
601 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
602 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
603 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
605 /* collect info about device mappings */
606 if ((nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
607 sizeof (uio_res->maps) / sizeof (uio_res->maps[0])))
611 uio_res->nb_maps = nb_maps;
614 pagesz = sysconf(_SC_PAGESIZE);
616 maps = uio_res->maps;
617 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
620 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
623 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
624 dev->mem_resource[i].len != maps[j].size);
628 /* if matching map is found, then use it */
631 if (maps[j].addr != NULL ||
632 (mapaddr = pci_map_resource(dev,
633 NULL, devname, (off_t)offset,
634 (size_t)maps[j].size)) == NULL) {
638 maps[j].addr = mapaddr;
639 maps[j].offset = offset;
640 dev->mem_resource[i].addr = mapaddr;
644 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
649 /* parse the "resource" sysfs file */
650 #define IORESOURCE_MEM 0x00000200
653 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
657 union pci_resource_info {
663 char *ptrs[PCI_RESOURCE_FMT_NVAL];
666 uint64_t phys_addr, end_addr, flags;
668 f = fopen(filename, "r");
670 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
674 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
676 if (fgets(buf, sizeof(buf), f) == NULL) {
678 "%s(): cannot read resource\n", __func__);
682 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
684 "%s(): bad resource format\n", __func__);
688 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
689 end_addr = strtoull(res_info.end_addr, NULL, 16);
690 flags = strtoull(res_info.flags, NULL, 16);
693 "%s(): bad resource format\n", __func__);
697 if (flags & IORESOURCE_MEM) {
698 dev->mem_resource[i].phys_addr = phys_addr;
699 dev->mem_resource[i].len = end_addr - phys_addr + 1;
700 /* not mapped for now */
701 dev->mem_resource[i].addr = NULL;
713 * parse a sysfs file containing one integer value
714 * different to the eal version, as it needs to work with 64-bit values
717 pci_parse_sysfs_value(const char *filename, uint64_t *val)
723 f = fopen(filename, "r");
725 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
730 if (fgets(buf, sizeof(buf), f) == NULL) {
731 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
736 *val = strtoull(buf, &end, 0);
737 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
738 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
747 /* Compare two PCI device addresses. */
749 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
751 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
752 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
754 if (dev_addr > dev_addr2)
761 /* Scan one pci sysfs entry, and fill the devices list from it. */
763 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
764 uint8_t devid, uint8_t function)
766 char filename[PATH_MAX];
768 struct rte_pci_device *dev;
770 dev = malloc(sizeof(*dev));
775 memset(dev, 0, sizeof(*dev));
776 dev->addr.domain = domain;
778 dev->addr.devid = devid;
779 dev->addr.function = function;
782 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
783 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
787 dev->id.vendor_id = (uint16_t)tmp;
790 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
791 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
795 dev->id.device_id = (uint16_t)tmp;
797 /* get subsystem_vendor id */
798 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
800 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
804 dev->id.subsystem_vendor_id = (uint16_t)tmp;
806 /* get subsystem_device id */
807 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
809 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
813 dev->id.subsystem_device_id = (uint16_t)tmp;
817 rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
818 if (!access(filename, F_OK) &&
819 eal_parse_sysfs_value(filename, &tmp) == 0) {
820 dev->max_vfs = (uint16_t)tmp;
824 rte_snprintf(filename, sizeof(filename), "%s/numa_node",
826 if (access(filename, R_OK) != 0) {
827 /* if no NUMA support just set node to 0 */
830 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
834 dev->numa_node = tmp;
837 /* parse resources */
838 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
839 if (pci_parse_sysfs_resource(filename, dev) < 0) {
840 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
845 /* device is valid, add in list (sorted) */
846 if (TAILQ_EMPTY(&device_list)) {
847 TAILQ_INSERT_TAIL(&device_list, dev, next);
850 struct rte_pci_device *dev2 = NULL;
852 TAILQ_FOREACH(dev2, &device_list, next) {
853 if (pci_addr_comparison(&dev->addr, &dev2->addr))
856 TAILQ_INSERT_BEFORE(dev2, dev, next);
860 TAILQ_INSERT_TAIL(&device_list, dev, next);
867 * split up a pci address into its constituent parts.
870 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
871 uint8_t *bus, uint8_t *devid, uint8_t *function)
873 /* first split on ':' */
881 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
884 char *buf_copy = strndup(buf, bufsize);
885 if (buf_copy == NULL)
888 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
891 /* final split is on '.' between devid and function */
892 splitaddr.function = strchr(splitaddr.devid,'.');
893 if (splitaddr.function == NULL)
895 *splitaddr.function++ = '\0';
897 /* now convert to int values */
899 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
900 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
901 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
902 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
906 free(buf_copy); /* free the copy made with strdup */
914 * Scan the content of the PCI bus, and the devices in the devices
922 char dirname[PATH_MAX];
924 uint8_t bus, devid, function;
926 dir = opendir(SYSFS_PCI_DEVICES);
928 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
929 __func__, strerror(errno));
933 while ((e = readdir(dir)) != NULL) {
934 if (e->d_name[0] == '.')
937 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
938 &bus, &devid, &function) != 0)
941 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
943 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
955 * If vendor/device ID match, call the devinit() function of the
959 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
961 struct rte_pci_id *id_table;
963 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
965 /* check if device's identifiers match the driver's ones */
966 if (id_table->vendor_id != dev->id.vendor_id &&
967 id_table->vendor_id != PCI_ANY_ID)
969 if (id_table->device_id != dev->id.device_id &&
970 id_table->device_id != PCI_ANY_ID)
972 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
973 id_table->subsystem_vendor_id != PCI_ANY_ID)
975 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
976 id_table->subsystem_device_id != PCI_ANY_ID)
979 struct rte_pci_addr *loc = &dev->addr;
981 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
982 loc->domain, loc->bus, loc->devid, loc->function,
985 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
986 dev->id.device_id, dr->name);
988 /* no initialization when blacklisted, return without error */
989 if (dev->blacklisted) {
990 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
994 #ifdef RTE_EAL_UNBIND_PORTS
995 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
996 /* unbind driver and load uio resources for Intel NICs */
997 if (pci_switch_module(dr, dev, 1, IGB_UIO_NAME) < 0)
999 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
1000 rte_eal_process_type() == RTE_PROC_PRIMARY) {
1001 /* unbind current driver */
1002 if (pci_unbind_kernel_driver(dev) < 0)
1006 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
1007 /* just map resources for Intel NICs */
1008 if (pci_uio_map_resource(dev) < 0)
1012 /* reference driver structure */
1015 /* call the driver devinit() function */
1016 return dr->devinit(dr, dev);
1018 /* return positive value if driver is not found */
1022 /* Init the PCI EAL subsystem */
1024 rte_eal_pci_init(void)
1026 TAILQ_INIT(&driver_list);
1027 TAILQ_INIT(&device_list);
1028 uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
1030 /* for debug purposes, PCI can be disabled */
1031 if (internal_config.no_pci)
1034 if (pci_scan() < 0) {
1035 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);