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