eal: fix tailq init for uio and vfio resources
[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->pt_driver = RTE_PT_VFIO;
344                 else if (!strcmp(driver, "igb_uio"))
345                         dev->pt_driver = RTE_PT_IGB_UIO;
346                 else if (!strcmp(driver, "uio_pci_generic"))
347                         dev->pt_driver = RTE_PT_UIO_GENERIC;
348                 else
349                         dev->pt_driver = RTE_PT_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->pt_driver = RTE_PT_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                                 /* update pt_driver */
374                                 dev2->pt_driver = dev->pt_driver;
375                                 dev2->max_vfs = dev->max_vfs;
376                                 memmove(dev2->mem_resource,
377                                         dev->mem_resource,
378                                         sizeof(dev->mem_resource));
379                                 free(dev);
380                                 return 0;
381                         }
382                 }
383                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
384         }
385
386         return 0;
387 }
388
389 /*
390  * split up a pci address into its constituent parts.
391  */
392 static int
393 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
394                 uint8_t *bus, uint8_t *devid, uint8_t *function)
395 {
396         /* first split on ':' */
397         union splitaddr {
398                 struct {
399                         char *domain;
400                         char *bus;
401                         char *devid;
402                         char *function;
403                 };
404                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
405         } splitaddr;
406
407         char *buf_copy = strndup(buf, bufsize);
408         if (buf_copy == NULL)
409                 return -1;
410
411         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
412                         != PCI_FMT_NVAL - 1)
413                 goto error;
414         /* final split is on '.' between devid and function */
415         splitaddr.function = strchr(splitaddr.devid,'.');
416         if (splitaddr.function == NULL)
417                 goto error;
418         *splitaddr.function++ = '\0';
419
420         /* now convert to int values */
421         errno = 0;
422         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
423         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
424         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
425         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
426         if (errno != 0)
427                 goto error;
428
429         free(buf_copy); /* free the copy made with strdup */
430         return 0;
431 error:
432         free(buf_copy);
433         return -1;
434 }
435
436 /*
437  * Scan the content of the PCI bus, and the devices in the devices
438  * list
439  */
440 int
441 rte_eal_pci_scan(void)
442 {
443         struct dirent *e;
444         DIR *dir;
445         char dirname[PATH_MAX];
446         uint16_t domain;
447         uint8_t bus, devid, function;
448
449         dir = opendir(SYSFS_PCI_DEVICES);
450         if (dir == NULL) {
451                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
452                         __func__, strerror(errno));
453                 return -1;
454         }
455
456         while ((e = readdir(dir)) != NULL) {
457                 if (e->d_name[0] == '.')
458                         continue;
459
460                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
461                                 &bus, &devid, &function) != 0)
462                         continue;
463
464                 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
465                          e->d_name);
466                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
467                         goto error;
468         }
469         closedir(dir);
470         return 0;
471
472 error:
473         closedir(dir);
474         return -1;
475 }
476
477 #ifdef RTE_PCI_CONFIG
478 static int
479 pci_config_extended_tag(struct rte_pci_device *dev)
480 {
481         struct rte_pci_addr *loc = &dev->addr;
482         char filename[PATH_MAX];
483         char buf[BUFSIZ];
484         FILE *f;
485
486         /* not configured, let it as is */
487         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
488                 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
489                 return 0;
490
491         snprintf(filename, sizeof(filename),
492                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
493                 loc->domain, loc->bus, loc->devid, loc->function);
494         f = fopen(filename, "rw+");
495         if (!f)
496                 return -1;
497
498         fgets(buf, sizeof(buf), f);
499         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
500                 /* enable Extended Tag*/
501                 if (strncmp(buf, "on", 2) != 0) {
502                         fseek(f, 0, SEEK_SET);
503                         fputs("on", f);
504                 }
505         } else {
506                 /* disable Extended Tag */
507                 if (strncmp(buf, "off", 3) != 0) {
508                         fseek(f, 0, SEEK_SET);
509                         fputs("off", f);
510                 }
511         }
512         fclose(f);
513
514         return 0;
515 }
516
517 static int
518 pci_config_max_read_request_size(struct rte_pci_device *dev)
519 {
520         struct rte_pci_addr *loc = &dev->addr;
521         char filename[PATH_MAX];
522         char buf[BUFSIZ], param[BUFSIZ];
523         FILE *f;
524         /* size can be 128, 256, 512, 1024, 2048, 4096 */
525         uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
526
527         /* not configured, let it as is */
528         if (!max_size)
529                 return 0;
530
531         snprintf(filename, sizeof(filename),
532                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
533                         loc->domain, loc->bus, loc->devid, loc->function);
534         f = fopen(filename, "rw+");
535         if (!f)
536                 return -1;
537
538         fgets(buf, sizeof(buf), f);
539         snprintf(param, sizeof(param), "%d", max_size);
540
541         /* check if the size to be set is the same as current */
542         if (strcmp(buf, param) == 0) {
543                 fclose(f);
544                 return 0;
545         }
546         fseek(f, 0, SEEK_SET);
547         fputs(param, f);
548         fclose(f);
549
550         return 0;
551 }
552
553 static void
554 pci_config_space_set(struct rte_pci_device *dev)
555 {
556         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
557                 return;
558
559         /* configure extended tag */
560         pci_config_extended_tag(dev);
561
562         /* configure max read request size */
563         pci_config_max_read_request_size(dev);
564 }
565 #endif
566
567 static int
568 pci_map_device(struct rte_pci_device *dev)
569 {
570         int ret = -1;
571
572         /* try mapping the NIC resources using VFIO if it exists */
573         switch (dev->pt_driver) {
574         case RTE_PT_VFIO:
575 #ifdef VFIO_PRESENT
576                 if (pci_vfio_is_enabled())
577                         ret = pci_vfio_map_resource(dev);
578 #endif
579                 break;
580         case RTE_PT_IGB_UIO:
581         case RTE_PT_UIO_GENERIC:
582                 /* map resources for devices that use uio */
583                 ret = pci_uio_map_resource(dev);
584                 break;
585         default:
586                 RTE_LOG(DEBUG, EAL, "  Not managed by known pt driver,"
587                         " skipped\n");
588                 ret = 1;
589                 break;
590         }
591
592         return ret;
593 }
594
595 #ifdef RTE_LIBRTE_EAL_HOTPLUG
596 static void
597 pci_unmap_device(struct rte_pci_device *dev)
598 {
599         if (dev == NULL)
600                 return;
601
602         /* try unmapping the NIC resources using VFIO if it exists */
603         switch (dev->pt_driver) {
604         case RTE_PT_VFIO:
605                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
606                 break;
607         case RTE_PT_IGB_UIO:
608         case RTE_PT_UIO_GENERIC:
609                 /* unmap resources for devices that use uio */
610                 pci_uio_unmap_resource(dev);
611                 break;
612         default:
613                 RTE_LOG(DEBUG, EAL, "  Not managed by known pt driver,"
614                         " skipped\n");
615                 break;
616         }
617 }
618 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
619
620 /*
621  * If vendor/device ID match, call the devinit() function of the
622  * driver.
623  */
624 int
625 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
626 {
627         int ret;
628         struct rte_pci_id *id_table;
629
630         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
631
632                 /* check if device's identifiers match the driver's ones */
633                 if (id_table->vendor_id != dev->id.vendor_id &&
634                                 id_table->vendor_id != PCI_ANY_ID)
635                         continue;
636                 if (id_table->device_id != dev->id.device_id &&
637                                 id_table->device_id != PCI_ANY_ID)
638                         continue;
639                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
640                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
641                         continue;
642                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
643                                 id_table->subsystem_device_id != PCI_ANY_ID)
644                         continue;
645
646                 struct rte_pci_addr *loc = &dev->addr;
647
648                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
649                                 loc->domain, loc->bus, loc->devid, loc->function,
650                                 dev->numa_node);
651
652                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
653                                 dev->id.device_id, dr->name);
654
655                 /* no initialization when blacklisted, return without error */
656                 if (dev->devargs != NULL &&
657                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
658                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
659                         return 1;
660                 }
661
662                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
663 #ifdef RTE_PCI_CONFIG
664                         /*
665                          * Set PCIe config space for high performance.
666                          * Return value can be ignored.
667                          */
668                         pci_config_space_set(dev);
669 #endif
670                         /* map resources for devices that use igb_uio */
671                         ret = pci_map_device(dev);
672                         if (ret != 0)
673                                 return ret;
674                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
675                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
676                         /* unbind current driver */
677                         if (pci_unbind_kernel_driver(dev) < 0)
678                                 return -1;
679                 }
680
681                 /* reference driver structure */
682                 dev->driver = dr;
683
684                 /* call the driver devinit() function */
685                 return dr->devinit(dr, dev);
686         }
687         /* return positive value if driver is not found */
688         return 1;
689 }
690
691 #ifdef RTE_LIBRTE_EAL_HOTPLUG
692 /*
693  * If vendor/device ID match, call the devuninit() function of the
694  * driver.
695  */
696 int
697 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
698                 struct rte_pci_device *dev)
699 {
700         struct rte_pci_id *id_table;
701
702         if ((dr == NULL) || (dev == NULL))
703                 return -EINVAL;
704
705         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
706
707                 /* check if device's identifiers match the driver's ones */
708                 if (id_table->vendor_id != dev->id.vendor_id &&
709                     id_table->vendor_id != PCI_ANY_ID)
710                         continue;
711                 if (id_table->device_id != dev->id.device_id &&
712                     id_table->device_id != PCI_ANY_ID)
713                         continue;
714                 if (id_table->subsystem_vendor_id !=
715                     dev->id.subsystem_vendor_id &&
716                     id_table->subsystem_vendor_id != PCI_ANY_ID)
717                         continue;
718                 if (id_table->subsystem_device_id !=
719                     dev->id.subsystem_device_id &&
720                     id_table->subsystem_device_id != PCI_ANY_ID)
721                         continue;
722
723                 struct rte_pci_addr *loc = &dev->addr;
724
725                 RTE_LOG(DEBUG, EAL,
726                                 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
727                                 loc->domain, loc->bus, loc->devid,
728                                 loc->function, dev->numa_node);
729
730                 RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n",
731                                 dev->id.vendor_id, dev->id.device_id,
732                                 dr->name);
733
734                 /* call the driver devuninit() function */
735                 if (dr->devuninit && (dr->devuninit(dev) < 0))
736                         return -1;      /* negative value is an error */
737
738                 /* clear driver structure */
739                 dev->driver = NULL;
740
741                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
742                         /* unmap resources for devices that use igb_uio */
743                         pci_unmap_device(dev);
744
745                 return 0;
746         }
747         /* return positive value if driver is not found */
748         return 1;
749 }
750 #else /* RTE_LIBRTE_EAL_HOTPLUG */
751 int
752 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
753                 struct rte_pci_device *dev __rte_unused)
754 {
755         RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
756         return -1;
757 }
758 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
759
760 /* Init the PCI EAL subsystem */
761 int
762 rte_eal_pci_init(void)
763 {
764         TAILQ_INIT(&pci_driver_list);
765         TAILQ_INIT(&pci_device_list);
766
767         /* for debug purposes, PCI can be disabled */
768         if (internal_config.no_pci)
769                 return 0;
770
771         if (rte_eal_pci_scan() < 0) {
772                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
773                 return -1;
774         }
775 #ifdef VFIO_PRESENT
776         pci_vfio_enable();
777
778         if (pci_vfio_is_enabled()) {
779
780                 /* if we are primary process, create a thread to communicate with
781                  * secondary processes. the thread will use a socket to wait for
782                  * requests from secondary process to send open file descriptors,
783                  * because VFIO does not allow multiple open descriptors on a group or
784                  * VFIO container.
785                  */
786                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
787                                 pci_vfio_mp_sync_setup() < 0)
788                         return -1;
789         }
790 #endif
791         return 0;
792 }