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