d2adc66a5e407b8a6a766a7f32c339b66ac04efe
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <dirent.h>
36 #include <sys/mman.h>
37
38 #include <rte_log.h>
39 #include <rte_pci.h>
40 #include <rte_eal_memconfig.h>
41 #include <rte_malloc.h>
42 #include <rte_devargs.h>
43 #include <rte_memcpy.h>
44
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_private.h"
48 #include "eal_pci_init.h"
49
50 /**
51  * @file
52  * PCI probing under linux
53  *
54  * This code is used to simulate a PCI probe by parsing information in sysfs.
55  * When a registered device matches a driver, it is then initialized with
56  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
57  */
58
59 /* unbind kernel driver for this device */
60 static int
61 pci_unbind_kernel_driver(struct rte_pci_device *dev)
62 {
63         int n;
64         FILE *f;
65         char filename[PATH_MAX];
66         char buf[BUFSIZ];
67         struct rte_pci_addr *loc = &dev->addr;
68
69         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
70         snprintf(filename, sizeof(filename),
71                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
72                  loc->domain, loc->bus, loc->devid, loc->function);
73
74         f = fopen(filename, "w");
75         if (f == NULL) /* device was not bound */
76                 return 0;
77
78         n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
79                      loc->domain, loc->bus, loc->devid, loc->function);
80         if ((n < 0) || (n >= (int)sizeof(buf))) {
81                 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
82                 goto error;
83         }
84         if (fwrite(buf, n, 1, f) == 0) {
85                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
86                                 filename);
87                 goto error;
88         }
89
90         fclose(f);
91         return 0;
92
93 error:
94         fclose(f);
95         return -1;
96 }
97
98 static int
99 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
100 {
101         int count;
102         char path[PATH_MAX];
103         char *name;
104
105         if (!filename || !dri_name)
106                 return -1;
107
108         count = readlink(filename, path, PATH_MAX);
109         if (count >= PATH_MAX)
110                 return -1;
111
112         /* For device does not have a driver */
113         if (count < 0)
114                 return 1;
115
116         path[count] = '\0';
117
118         name = strrchr(path, '/');
119         if (name) {
120                 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
121                 return 0;
122         }
123
124         return -1;
125 }
126
127 void *
128 pci_find_max_end_va(void)
129 {
130         const struct rte_memseg *seg = rte_eal_get_physmem_layout();
131         const struct rte_memseg *last = seg;
132         unsigned i = 0;
133
134         for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
135                 if (seg->addr == NULL)
136                         break;
137
138                 if (seg->addr > last->addr)
139                         last = seg;
140
141         }
142         return RTE_PTR_ADD(last->addr, last->len);
143 }
144
145
146 /* map a particular resource from a file */
147 void *
148 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
149                  int additional_flags)
150 {
151         void *mapaddr;
152
153         /* Map the PCI memory resource of device */
154         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
155                         MAP_SHARED | additional_flags, fd, offset);
156         if (mapaddr == MAP_FAILED) {
157                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
158                         __func__, fd, requested_addr,
159                         (unsigned long)size, (unsigned long)offset,
160                         strerror(errno), mapaddr);
161         } else {
162                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
163         }
164
165         return mapaddr;
166 }
167
168 /* unmap a particular resource */
169 void
170 pci_unmap_resource(void *requested_addr, size_t size)
171 {
172         if (requested_addr == NULL)
173                 return;
174
175         /* Unmap the PCI memory resource of device */
176         if (munmap(requested_addr, size)) {
177                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
178                         __func__, requested_addr, (unsigned long)size,
179                         strerror(errno));
180         } else
181                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
182                                 requested_addr);
183 }
184
185 /* parse the "resource" sysfs file */
186 static int
187 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
188 {
189         FILE *f;
190         char buf[BUFSIZ];
191         union pci_resource_info {
192                 struct {
193                         char *phys_addr;
194                         char *end_addr;
195                         char *flags;
196                 };
197                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
198         } res_info;
199         int i;
200         uint64_t phys_addr, end_addr, flags;
201
202         f = fopen(filename, "r");
203         if (f == NULL) {
204                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
205                 return -1;
206         }
207
208         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
209
210                 if (fgets(buf, sizeof(buf), f) == NULL) {
211                         RTE_LOG(ERR, EAL,
212                                 "%s(): cannot read resource\n", __func__);
213                         goto error;
214                 }
215
216                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
217                         RTE_LOG(ERR, EAL,
218                                 "%s(): bad resource format\n", __func__);
219                         goto error;
220                 }
221                 errno = 0;
222                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
223                 end_addr = strtoull(res_info.end_addr, NULL, 16);
224                 flags = strtoull(res_info.flags, NULL, 16);
225                 if (errno != 0) {
226                         RTE_LOG(ERR, EAL,
227                                 "%s(): bad resource format\n", __func__);
228                         goto error;
229                 }
230
231                 if (flags & IORESOURCE_MEM) {
232                         dev->mem_resource[i].phys_addr = phys_addr;
233                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
234                         /* not mapped for now */
235                         dev->mem_resource[i].addr = NULL;
236                 }
237         }
238         fclose(f);
239         return 0;
240
241 error:
242         fclose(f);
243         return -1;
244 }
245
246 /* Scan one pci sysfs entry, and fill the devices list from it. */
247 static int
248 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
249              uint8_t devid, uint8_t function)
250 {
251         char filename[PATH_MAX];
252         unsigned long tmp;
253         struct rte_pci_device *dev;
254         char driver[PATH_MAX];
255         int ret;
256
257         dev = malloc(sizeof(*dev));
258         if (dev == NULL)
259                 return -1;
260
261         memset(dev, 0, sizeof(*dev));
262         dev->addr.domain = domain;
263         dev->addr.bus = bus;
264         dev->addr.devid = devid;
265         dev->addr.function = function;
266
267         /* get vendor id */
268         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
269         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
270                 free(dev);
271                 return -1;
272         }
273         dev->id.vendor_id = (uint16_t)tmp;
274
275         /* get device id */
276         snprintf(filename, sizeof(filename), "%s/device", dirname);
277         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
278                 free(dev);
279                 return -1;
280         }
281         dev->id.device_id = (uint16_t)tmp;
282
283         /* get subsystem_vendor id */
284         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
285                  dirname);
286         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
287                 free(dev);
288                 return -1;
289         }
290         dev->id.subsystem_vendor_id = (uint16_t)tmp;
291
292         /* get subsystem_device id */
293         snprintf(filename, sizeof(filename), "%s/subsystem_device",
294                  dirname);
295         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
296                 free(dev);
297                 return -1;
298         }
299         dev->id.subsystem_device_id = (uint16_t)tmp;
300
301         /* get max_vfs */
302         dev->max_vfs = 0;
303         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
304         if (!access(filename, F_OK) &&
305             eal_parse_sysfs_value(filename, &tmp) == 0)
306                 dev->max_vfs = (uint16_t)tmp;
307         else {
308                 /* for non igb_uio driver, need kernel version >= 3.8 */
309                 snprintf(filename, sizeof(filename),
310                          "%s/sriov_numvfs", dirname);
311                 if (!access(filename, F_OK) &&
312                     eal_parse_sysfs_value(filename, &tmp) == 0)
313                         dev->max_vfs = (uint16_t)tmp;
314         }
315
316         /* get numa node */
317         snprintf(filename, sizeof(filename), "%s/numa_node",
318                  dirname);
319         if (access(filename, R_OK) != 0) {
320                 /* if no NUMA support just set node to -1 */
321                 dev->numa_node = -1;
322         } else {
323                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
324                         free(dev);
325                         return -1;
326                 }
327                 dev->numa_node = tmp;
328         }
329
330         /* parse resources */
331         snprintf(filename, sizeof(filename), "%s/resource", dirname);
332         if (pci_parse_sysfs_resource(filename, dev) < 0) {
333                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
334                 free(dev);
335                 return -1;
336         }
337
338         /* parse driver */
339         snprintf(filename, sizeof(filename), "%s/driver", dirname);
340         ret = pci_get_kernel_driver_by_path(filename, driver);
341         if (!ret) {
342                 if (!strcmp(driver, "vfio-pci"))
343                         dev->kdrv = RTE_KDRV_VFIO;
344                 else if (!strcmp(driver, "igb_uio"))
345                         dev->kdrv = RTE_KDRV_IGB_UIO;
346                 else if (!strcmp(driver, "uio_pci_generic"))
347                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
348                 else
349                         dev->kdrv = RTE_KDRV_UNKNOWN;
350         } else if (ret < 0) {
351                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
352                 free(dev);
353                 return -1;
354         } else
355                 dev->kdrv = RTE_KDRV_UNKNOWN;
356
357         /* device is valid, add in list (sorted) */
358         if (TAILQ_EMPTY(&pci_device_list)) {
359                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
360         }
361         else {
362                 struct rte_pci_device *dev2 = NULL;
363                 int ret;
364
365                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
366                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
367                         if (ret > 0)
368                                 continue;
369                         else if (ret < 0) {
370                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
371                                 return 0;
372                         } else { /* already registered */
373                                 dev2->kdrv = dev->kdrv;
374                                 dev2->max_vfs = dev->max_vfs;
375                                 memmove(dev2->mem_resource,
376                                         dev->mem_resource,
377                                         sizeof(dev->mem_resource));
378                                 free(dev);
379                                 return 0;
380                         }
381                 }
382                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
383         }
384
385         return 0;
386 }
387
388 /*
389  * split up a pci address into its constituent parts.
390  */
391 static int
392 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
393                 uint8_t *bus, uint8_t *devid, uint8_t *function)
394 {
395         /* first split on ':' */
396         union splitaddr {
397                 struct {
398                         char *domain;
399                         char *bus;
400                         char *devid;
401                         char *function;
402                 };
403                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
404         } splitaddr;
405
406         char *buf_copy = strndup(buf, bufsize);
407         if (buf_copy == NULL)
408                 return -1;
409
410         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
411                         != PCI_FMT_NVAL - 1)
412                 goto error;
413         /* final split is on '.' between devid and function */
414         splitaddr.function = strchr(splitaddr.devid,'.');
415         if (splitaddr.function == NULL)
416                 goto error;
417         *splitaddr.function++ = '\0';
418
419         /* now convert to int values */
420         errno = 0;
421         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
422         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
423         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
424         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
425         if (errno != 0)
426                 goto error;
427
428         free(buf_copy); /* free the copy made with strdup */
429         return 0;
430 error:
431         free(buf_copy);
432         return -1;
433 }
434
435 /*
436  * Scan the content of the PCI bus, and the devices in the devices
437  * list
438  */
439 int
440 rte_eal_pci_scan(void)
441 {
442         struct dirent *e;
443         DIR *dir;
444         char dirname[PATH_MAX];
445         uint16_t domain;
446         uint8_t bus, devid, function;
447
448         dir = opendir(SYSFS_PCI_DEVICES);
449         if (dir == NULL) {
450                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
451                         __func__, strerror(errno));
452                 return -1;
453         }
454
455         while ((e = readdir(dir)) != NULL) {
456                 if (e->d_name[0] == '.')
457                         continue;
458
459                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
460                                 &bus, &devid, &function) != 0)
461                         continue;
462
463                 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
464                          e->d_name);
465                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
466                         goto error;
467         }
468         closedir(dir);
469         return 0;
470
471 error:
472         closedir(dir);
473         return -1;
474 }
475
476 #ifdef RTE_PCI_CONFIG
477 static int
478 pci_config_extended_tag(struct rte_pci_device *dev)
479 {
480         struct rte_pci_addr *loc = &dev->addr;
481         char filename[PATH_MAX];
482         char buf[BUFSIZ];
483         FILE *f;
484
485         /* not configured, let it as is */
486         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
487                 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
488                 return 0;
489
490         snprintf(filename, sizeof(filename),
491                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
492                 loc->domain, loc->bus, loc->devid, loc->function);
493         f = fopen(filename, "rw+");
494         if (!f)
495                 return -1;
496
497         fgets(buf, sizeof(buf), f);
498         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
499                 /* enable Extended Tag*/
500                 if (strncmp(buf, "on", 2) != 0) {
501                         fseek(f, 0, SEEK_SET);
502                         fputs("on", f);
503                 }
504         } else {
505                 /* disable Extended Tag */
506                 if (strncmp(buf, "off", 3) != 0) {
507                         fseek(f, 0, SEEK_SET);
508                         fputs("off", f);
509                 }
510         }
511         fclose(f);
512
513         return 0;
514 }
515
516 static int
517 pci_config_max_read_request_size(struct rte_pci_device *dev)
518 {
519         struct rte_pci_addr *loc = &dev->addr;
520         char filename[PATH_MAX];
521         char buf[BUFSIZ], param[BUFSIZ];
522         FILE *f;
523         /* size can be 128, 256, 512, 1024, 2048, 4096 */
524         uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
525
526         /* not configured, let it as is */
527         if (!max_size)
528                 return 0;
529
530         snprintf(filename, sizeof(filename),
531                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
532                         loc->domain, loc->bus, loc->devid, loc->function);
533         f = fopen(filename, "rw+");
534         if (!f)
535                 return -1;
536
537         fgets(buf, sizeof(buf), f);
538         snprintf(param, sizeof(param), "%d", max_size);
539
540         /* check if the size to be set is the same as current */
541         if (strcmp(buf, param) == 0) {
542                 fclose(f);
543                 return 0;
544         }
545         fseek(f, 0, SEEK_SET);
546         fputs(param, f);
547         fclose(f);
548
549         return 0;
550 }
551
552 static void
553 pci_config_space_set(struct rte_pci_device *dev)
554 {
555         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
556                 return;
557
558         /* configure extended tag */
559         pci_config_extended_tag(dev);
560
561         /* configure max read request size */
562         pci_config_max_read_request_size(dev);
563 }
564 #endif
565
566 static int
567 pci_map_device(struct rte_pci_device *dev)
568 {
569         int ret = -1;
570
571         /* try mapping the NIC resources using VFIO if it exists */
572         switch (dev->kdrv) {
573         case RTE_KDRV_VFIO:
574 #ifdef VFIO_PRESENT
575                 if (pci_vfio_is_enabled())
576                         ret = pci_vfio_map_resource(dev);
577 #endif
578                 break;
579         case RTE_KDRV_IGB_UIO:
580         case RTE_KDRV_UIO_GENERIC:
581                 /* map resources for devices that use uio */
582                 ret = pci_uio_map_resource(dev);
583                 break;
584         default:
585                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
586                         " skipped\n");
587                 ret = 1;
588                 break;
589         }
590
591         return ret;
592 }
593
594 #ifdef RTE_LIBRTE_EAL_HOTPLUG
595 static void
596 pci_unmap_device(struct rte_pci_device *dev)
597 {
598         if (dev == NULL)
599                 return;
600
601         /* try unmapping the NIC resources using VFIO if it exists */
602         switch (dev->kdrv) {
603         case RTE_KDRV_VFIO:
604                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
605                 break;
606         case RTE_KDRV_IGB_UIO:
607         case RTE_KDRV_UIO_GENERIC:
608                 /* unmap resources for devices that use uio */
609                 pci_uio_unmap_resource(dev);
610                 break;
611         default:
612                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
613                         " skipped\n");
614                 break;
615         }
616 }
617 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
618
619 /*
620  * If vendor/device ID match, call the devinit() function of the
621  * driver.
622  */
623 int
624 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
625 {
626         int ret;
627         const struct rte_pci_id *id_table;
628
629         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
630
631                 /* check if device's identifiers match the driver's ones */
632                 if (id_table->vendor_id != dev->id.vendor_id &&
633                                 id_table->vendor_id != PCI_ANY_ID)
634                         continue;
635                 if (id_table->device_id != dev->id.device_id &&
636                                 id_table->device_id != PCI_ANY_ID)
637                         continue;
638                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
639                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
640                         continue;
641                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
642                                 id_table->subsystem_device_id != PCI_ANY_ID)
643                         continue;
644
645                 struct rte_pci_addr *loc = &dev->addr;
646
647                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
648                                 loc->domain, loc->bus, loc->devid, loc->function,
649                                 dev->numa_node);
650
651                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
652                                 dev->id.device_id, dr->name);
653
654                 /* no initialization when blacklisted, return without error */
655                 if (dev->devargs != NULL &&
656                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
657                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
658                         return 1;
659                 }
660
661                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
662 #ifdef RTE_PCI_CONFIG
663                         /*
664                          * Set PCIe config space for high performance.
665                          * Return value can be ignored.
666                          */
667                         pci_config_space_set(dev);
668 #endif
669                         /* map resources for devices that use igb_uio */
670                         ret = pci_map_device(dev);
671                         if (ret != 0)
672                                 return ret;
673                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
674                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
675                         /* unbind current driver */
676                         if (pci_unbind_kernel_driver(dev) < 0)
677                                 return -1;
678                 }
679
680                 /* reference driver structure */
681                 dev->driver = dr;
682
683                 /* call the driver devinit() function */
684                 return dr->devinit(dr, dev);
685         }
686         /* return positive value if driver is not found */
687         return 1;
688 }
689
690 #ifdef RTE_LIBRTE_EAL_HOTPLUG
691 /*
692  * If vendor/device ID match, call the devuninit() function of the
693  * driver.
694  */
695 int
696 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
697                 struct rte_pci_device *dev)
698 {
699         const struct rte_pci_id *id_table;
700
701         if ((dr == NULL) || (dev == NULL))
702                 return -EINVAL;
703
704         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
705
706                 /* check if device's identifiers match the driver's ones */
707                 if (id_table->vendor_id != dev->id.vendor_id &&
708                     id_table->vendor_id != PCI_ANY_ID)
709                         continue;
710                 if (id_table->device_id != dev->id.device_id &&
711                     id_table->device_id != PCI_ANY_ID)
712                         continue;
713                 if (id_table->subsystem_vendor_id !=
714                     dev->id.subsystem_vendor_id &&
715                     id_table->subsystem_vendor_id != PCI_ANY_ID)
716                         continue;
717                 if (id_table->subsystem_device_id !=
718                     dev->id.subsystem_device_id &&
719                     id_table->subsystem_device_id != PCI_ANY_ID)
720                         continue;
721
722                 struct rte_pci_addr *loc = &dev->addr;
723
724                 RTE_LOG(DEBUG, EAL,
725                                 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
726                                 loc->domain, loc->bus, loc->devid,
727                                 loc->function, dev->numa_node);
728
729                 RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n",
730                                 dev->id.vendor_id, dev->id.device_id,
731                                 dr->name);
732
733                 /* call the driver devuninit() function */
734                 if (dr->devuninit && (dr->devuninit(dev) < 0))
735                         return -1;      /* negative value is an error */
736
737                 /* clear driver structure */
738                 dev->driver = NULL;
739
740                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
741                         /* unmap resources for devices that use igb_uio */
742                         pci_unmap_device(dev);
743
744                 return 0;
745         }
746         /* return positive value if driver is not found */
747         return 1;
748 }
749 #else /* RTE_LIBRTE_EAL_HOTPLUG */
750 int
751 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
752                 struct rte_pci_device *dev __rte_unused)
753 {
754         RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
755         return -1;
756 }
757 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
758
759 /* Init the PCI EAL subsystem */
760 int
761 rte_eal_pci_init(void)
762 {
763         TAILQ_INIT(&pci_driver_list);
764         TAILQ_INIT(&pci_device_list);
765
766         /* for debug purposes, PCI can be disabled */
767         if (internal_config.no_pci)
768                 return 0;
769
770         if (rte_eal_pci_scan() < 0) {
771                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
772                 return -1;
773         }
774 #ifdef VFIO_PRESENT
775         pci_vfio_enable();
776
777         if (pci_vfio_is_enabled()) {
778
779                 /* if we are primary process, create a thread to communicate with
780                  * secondary processes. the thread will use a socket to wait for
781                  * requests from secondary process to send open file descriptors,
782                  * because VFIO does not allow multiple open descriptors on a group or
783                  * VFIO container.
784                  */
785                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
786                                 pci_vfio_mp_sync_setup() < 0)
787                         return -1;
788         }
789 #endif
790         return 0;
791 }