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