Imported Upstream version 16.04
[deb_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 "eal_filesystem.h"
45 #include "eal_private.h"
46 #include "eal_pci_init.h"
47
48 /**
49  * @file
50  * PCI probing under linux
51  *
52  * This code is used to simulate a PCI probe by parsing information in sysfs.
53  * When a registered device matches a driver, it is then initialized with
54  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
55  */
56
57 /* unbind kernel driver for this device */
58 int
59 pci_unbind_kernel_driver(struct rte_pci_device *dev)
60 {
61         int n;
62         FILE *f;
63         char filename[PATH_MAX];
64         char buf[BUFSIZ];
65         struct rte_pci_addr *loc = &dev->addr;
66
67         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
68         snprintf(filename, sizeof(filename),
69                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
70                  loc->domain, loc->bus, loc->devid, loc->function);
71
72         f = fopen(filename, "w");
73         if (f == NULL) /* device was not bound */
74                 return 0;
75
76         n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
77                      loc->domain, loc->bus, loc->devid, loc->function);
78         if ((n < 0) || (n >= (int)sizeof(buf))) {
79                 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
80                 goto error;
81         }
82         if (fwrite(buf, n, 1, f) == 0) {
83                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
84                                 filename);
85                 goto error;
86         }
87
88         fclose(f);
89         return 0;
90
91 error:
92         fclose(f);
93         return -1;
94 }
95
96 static int
97 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
98 {
99         int count;
100         char path[PATH_MAX];
101         char *name;
102
103         if (!filename || !dri_name)
104                 return -1;
105
106         count = readlink(filename, path, PATH_MAX);
107         if (count >= PATH_MAX)
108                 return -1;
109
110         /* For device does not have a driver */
111         if (count < 0)
112                 return 1;
113
114         path[count] = '\0';
115
116         name = strrchr(path, '/');
117         if (name) {
118                 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
119                 return 0;
120         }
121
122         return -1;
123 }
124
125 /* Map pci device */
126 int
127 rte_eal_pci_map_device(struct rte_pci_device *dev)
128 {
129         int ret = -1;
130
131         /* try mapping the NIC resources using VFIO if it exists */
132         switch (dev->kdrv) {
133         case RTE_KDRV_VFIO:
134 #ifdef VFIO_PRESENT
135                 if (pci_vfio_is_enabled())
136                         ret = pci_vfio_map_resource(dev);
137 #endif
138                 break;
139         case RTE_KDRV_IGB_UIO:
140         case RTE_KDRV_UIO_GENERIC:
141                 /* map resources for devices that use uio */
142                 ret = pci_uio_map_resource(dev);
143                 break;
144         default:
145                 RTE_LOG(DEBUG, EAL,
146                         "  Not managed by a supported kernel driver, skipped\n");
147                 ret = 1;
148                 break;
149         }
150
151         return ret;
152 }
153
154 /* Unmap pci device */
155 void
156 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
157 {
158         /* try unmapping the NIC resources using VFIO if it exists */
159         switch (dev->kdrv) {
160         case RTE_KDRV_VFIO:
161                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
162                 break;
163         case RTE_KDRV_IGB_UIO:
164         case RTE_KDRV_UIO_GENERIC:
165                 /* unmap resources for devices that use uio */
166                 pci_uio_unmap_resource(dev);
167                 break;
168         default:
169                 RTE_LOG(DEBUG, EAL,
170                         "  Not managed by a supported kernel driver, skipped\n");
171                 break;
172         }
173 }
174
175 void *
176 pci_find_max_end_va(void)
177 {
178         const struct rte_memseg *seg = rte_eal_get_physmem_layout();
179         const struct rte_memseg *last = seg;
180         unsigned i = 0;
181
182         for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
183                 if (seg->addr == NULL)
184                         break;
185
186                 if (seg->addr > last->addr)
187                         last = seg;
188
189         }
190         return RTE_PTR_ADD(last->addr, last->len);
191 }
192
193 /* parse the "resource" sysfs file */
194 static int
195 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
196 {
197         FILE *f;
198         char buf[BUFSIZ];
199         union pci_resource_info {
200                 struct {
201                         char *phys_addr;
202                         char *end_addr;
203                         char *flags;
204                 };
205                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
206         } res_info;
207         int i;
208         uint64_t phys_addr, end_addr, flags;
209
210         f = fopen(filename, "r");
211         if (f == NULL) {
212                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
213                 return -1;
214         }
215
216         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
217
218                 if (fgets(buf, sizeof(buf), f) == NULL) {
219                         RTE_LOG(ERR, EAL,
220                                 "%s(): cannot read resource\n", __func__);
221                         goto error;
222                 }
223
224                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
225                         RTE_LOG(ERR, EAL,
226                                 "%s(): bad resource format\n", __func__);
227                         goto error;
228                 }
229                 errno = 0;
230                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
231                 end_addr = strtoull(res_info.end_addr, NULL, 16);
232                 flags = strtoull(res_info.flags, NULL, 16);
233                 if (errno != 0) {
234                         RTE_LOG(ERR, EAL,
235                                 "%s(): bad resource format\n", __func__);
236                         goto error;
237                 }
238
239                 if (flags & IORESOURCE_MEM) {
240                         dev->mem_resource[i].phys_addr = phys_addr;
241                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
242                         /* not mapped for now */
243                         dev->mem_resource[i].addr = NULL;
244                 }
245         }
246         fclose(f);
247         return 0;
248
249 error:
250         fclose(f);
251         return -1;
252 }
253
254 /* Scan one pci sysfs entry, and fill the devices list from it. */
255 static int
256 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
257              uint8_t devid, uint8_t function)
258 {
259         char filename[PATH_MAX];
260         unsigned long tmp;
261         struct rte_pci_device *dev;
262         char driver[PATH_MAX];
263         int ret;
264
265         dev = malloc(sizeof(*dev));
266         if (dev == NULL)
267                 return -1;
268
269         memset(dev, 0, sizeof(*dev));
270         dev->addr.domain = domain;
271         dev->addr.bus = bus;
272         dev->addr.devid = devid;
273         dev->addr.function = function;
274
275         /* get vendor id */
276         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
277         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
278                 free(dev);
279                 return -1;
280         }
281         dev->id.vendor_id = (uint16_t)tmp;
282
283         /* get device id */
284         snprintf(filename, sizeof(filename), "%s/device", dirname);
285         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
286                 free(dev);
287                 return -1;
288         }
289         dev->id.device_id = (uint16_t)tmp;
290
291         /* get subsystem_vendor id */
292         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
293                  dirname);
294         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
295                 free(dev);
296                 return -1;
297         }
298         dev->id.subsystem_vendor_id = (uint16_t)tmp;
299
300         /* get subsystem_device id */
301         snprintf(filename, sizeof(filename), "%s/subsystem_device",
302                  dirname);
303         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
304                 free(dev);
305                 return -1;
306         }
307         dev->id.subsystem_device_id = (uint16_t)tmp;
308
309         /* get max_vfs */
310         dev->max_vfs = 0;
311         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
312         if (!access(filename, F_OK) &&
313             eal_parse_sysfs_value(filename, &tmp) == 0)
314                 dev->max_vfs = (uint16_t)tmp;
315         else {
316                 /* for non igb_uio driver, need kernel version >= 3.8 */
317                 snprintf(filename, sizeof(filename),
318                          "%s/sriov_numvfs", dirname);
319                 if (!access(filename, F_OK) &&
320                     eal_parse_sysfs_value(filename, &tmp) == 0)
321                         dev->max_vfs = (uint16_t)tmp;
322         }
323
324         /* get numa node */
325         snprintf(filename, sizeof(filename), "%s/numa_node",
326                  dirname);
327         if (access(filename, R_OK) != 0) {
328                 /* if no NUMA support, set default to 0 */
329                 dev->numa_node = 0;
330         } else {
331                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
332                         free(dev);
333                         return -1;
334                 }
335                 dev->numa_node = tmp;
336         }
337
338         /* parse resources */
339         snprintf(filename, sizeof(filename), "%s/resource", dirname);
340         if (pci_parse_sysfs_resource(filename, dev) < 0) {
341                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
342                 free(dev);
343                 return -1;
344         }
345
346         /* parse driver */
347         snprintf(filename, sizeof(filename), "%s/driver", dirname);
348         ret = pci_get_kernel_driver_by_path(filename, driver);
349         if (ret < 0) {
350                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
351                 free(dev);
352                 return -1;
353         }
354
355         if (!ret) {
356                 if (!strcmp(driver, "vfio-pci"))
357                         dev->kdrv = RTE_KDRV_VFIO;
358                 else if (!strcmp(driver, "igb_uio"))
359                         dev->kdrv = RTE_KDRV_IGB_UIO;
360                 else if (!strcmp(driver, "uio_pci_generic"))
361                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
362                 else
363                         dev->kdrv = RTE_KDRV_UNKNOWN;
364         } else
365                 dev->kdrv = RTE_KDRV_NONE;
366
367         /* device is valid, add in list (sorted) */
368         if (TAILQ_EMPTY(&pci_device_list)) {
369                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
370         } else {
371                 struct rte_pci_device *dev2;
372                 int ret;
373
374                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
375                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
376                         if (ret > 0)
377                                 continue;
378
379                         if (ret < 0) {
380                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
381                         } else { /* already registered */
382                                 dev2->kdrv = dev->kdrv;
383                                 dev2->max_vfs = dev->max_vfs;
384                                 memmove(dev2->mem_resource, dev->mem_resource,
385                                         sizeof(dev->mem_resource));
386                                 free(dev);
387                         }
388                         return 0;
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 /*
486  * It is deprecated, all its configurations have been moved into
487  * each PMD respectively.
488  */
489 void
490 pci_config_space_set(__rte_unused struct rte_pci_device *dev)
491 {
492         RTE_LOG(DEBUG, EAL, "Nothing here, as it is deprecated\n");
493 }
494 #endif
495
496 /* Read PCI config space. */
497 int rte_eal_pci_read_config(const struct rte_pci_device *device,
498                             void *buf, size_t len, off_t offset)
499 {
500         const struct rte_intr_handle *intr_handle = &device->intr_handle;
501
502         switch (intr_handle->type) {
503         case RTE_INTR_HANDLE_UIO:
504         case RTE_INTR_HANDLE_UIO_INTX:
505                 return pci_uio_read_config(intr_handle, buf, len, offset);
506
507 #ifdef VFIO_PRESENT
508         case RTE_INTR_HANDLE_VFIO_MSIX:
509         case RTE_INTR_HANDLE_VFIO_MSI:
510         case RTE_INTR_HANDLE_VFIO_LEGACY:
511                 return pci_vfio_read_config(intr_handle, buf, len, offset);
512 #endif
513         default:
514                 RTE_LOG(ERR, EAL,
515                         "Unknown handle type of fd %d\n",
516                                         intr_handle->fd);
517                 return -1;
518         }
519 }
520
521 /* Write PCI config space. */
522 int rte_eal_pci_write_config(const struct rte_pci_device *device,
523                              const void *buf, size_t len, off_t offset)
524 {
525         const struct rte_intr_handle *intr_handle = &device->intr_handle;
526
527         switch (intr_handle->type) {
528         case RTE_INTR_HANDLE_UIO:
529         case RTE_INTR_HANDLE_UIO_INTX:
530                 return pci_uio_write_config(intr_handle, buf, len, offset);
531
532 #ifdef VFIO_PRESENT
533         case RTE_INTR_HANDLE_VFIO_MSIX:
534         case RTE_INTR_HANDLE_VFIO_MSI:
535         case RTE_INTR_HANDLE_VFIO_LEGACY:
536                 return pci_vfio_write_config(intr_handle, buf, len, offset);
537 #endif
538         default:
539                 RTE_LOG(ERR, EAL,
540                         "Unknown handle type of fd %d\n",
541                                         intr_handle->fd);
542                 return -1;
543         }
544 }
545
546 #if defined(RTE_ARCH_X86)
547 static int
548 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
549                struct rte_pci_ioport *p)
550 {
551         uint16_t start, end;
552         FILE *fp;
553         char *line = NULL;
554         char pci_id[16];
555         int found = 0;
556         size_t linesz;
557
558         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
559                  dev->addr.domain, dev->addr.bus,
560                  dev->addr.devid, dev->addr.function);
561
562         fp = fopen("/proc/ioports", "r");
563         if (fp == NULL) {
564                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
565                 return -1;
566         }
567
568         while (getdelim(&line, &linesz, '\n', fp) > 0) {
569                 char *ptr = line;
570                 char *left;
571                 int n;
572
573                 n = strcspn(ptr, ":");
574                 ptr[n] = 0;
575                 left = &ptr[n + 1];
576
577                 while (*left && isspace(*left))
578                         left++;
579
580                 if (!strncmp(left, pci_id, strlen(pci_id))) {
581                         found = 1;
582
583                         while (*ptr && isspace(*ptr))
584                                 ptr++;
585
586                         sscanf(ptr, "%04hx-%04hx", &start, &end);
587
588                         break;
589                 }
590         }
591
592         free(line);
593         fclose(fp);
594
595         if (!found)
596                 return -1;
597
598         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
599         p->base = start;
600         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
601
602         return 0;
603 }
604 #endif
605
606 int
607 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
608                        struct rte_pci_ioport *p)
609 {
610         int ret = -1;
611
612         switch (dev->kdrv) {
613 #ifdef VFIO_PRESENT
614         case RTE_KDRV_VFIO:
615                 if (pci_vfio_is_enabled())
616                         ret = pci_vfio_ioport_map(dev, bar, p);
617                 break;
618 #endif
619         case RTE_KDRV_IGB_UIO:
620                 ret = pci_uio_ioport_map(dev, bar, p);
621                 break;
622         case RTE_KDRV_UIO_GENERIC:
623 #if defined(RTE_ARCH_X86)
624                 ret = pci_ioport_map(dev, bar, p);
625 #else
626                 ret = pci_uio_ioport_map(dev, bar, p);
627 #endif
628                 break;
629         case RTE_KDRV_NONE:
630 #if defined(RTE_ARCH_X86)
631                 ret = pci_ioport_map(dev, bar, p);
632 #endif
633                 break;
634         default:
635                 break;
636         }
637
638         if (!ret)
639                 p->dev = dev;
640
641         return ret;
642 }
643
644 void
645 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
646                         void *data, size_t len, off_t offset)
647 {
648         switch (p->dev->kdrv) {
649 #ifdef VFIO_PRESENT
650         case RTE_KDRV_VFIO:
651                 pci_vfio_ioport_read(p, data, len, offset);
652                 break;
653 #endif
654         case RTE_KDRV_IGB_UIO:
655                 pci_uio_ioport_read(p, data, len, offset);
656                 break;
657         case RTE_KDRV_UIO_GENERIC:
658                 pci_uio_ioport_read(p, data, len, offset);
659                 break;
660         case RTE_KDRV_NONE:
661 #if defined(RTE_ARCH_X86)
662                 pci_uio_ioport_read(p, data, len, offset);
663 #endif
664                 break;
665         default:
666                 break;
667         }
668 }
669
670 void
671 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
672                          const void *data, size_t len, off_t offset)
673 {
674         switch (p->dev->kdrv) {
675 #ifdef VFIO_PRESENT
676         case RTE_KDRV_VFIO:
677                 pci_vfio_ioport_write(p, data, len, offset);
678                 break;
679 #endif
680         case RTE_KDRV_IGB_UIO:
681                 pci_uio_ioport_write(p, data, len, offset);
682                 break;
683         case RTE_KDRV_UIO_GENERIC:
684                 pci_uio_ioport_write(p, data, len, offset);
685                 break;
686         case RTE_KDRV_NONE:
687 #if defined(RTE_ARCH_X86)
688                 pci_uio_ioport_write(p, data, len, offset);
689 #endif
690                 break;
691         default:
692                 break;
693         }
694 }
695
696 int
697 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
698 {
699         int ret = -1;
700
701         switch (p->dev->kdrv) {
702 #ifdef VFIO_PRESENT
703         case RTE_KDRV_VFIO:
704                 if (pci_vfio_is_enabled())
705                         ret = pci_vfio_ioport_unmap(p);
706                 break;
707 #endif
708         case RTE_KDRV_IGB_UIO:
709                 ret = pci_uio_ioport_unmap(p);
710                 break;
711         case RTE_KDRV_UIO_GENERIC:
712 #if defined(RTE_ARCH_X86)
713                 ret = 0;
714 #else
715                 ret = pci_uio_ioport_unmap(p);
716 #endif
717                 break;
718         case RTE_KDRV_NONE:
719 #if defined(RTE_ARCH_X86)
720                 ret = 0;
721 #endif
722                 break;
723         default:
724                 break;
725         }
726
727         return ret;
728 }
729
730 /* Init the PCI EAL subsystem */
731 int
732 rte_eal_pci_init(void)
733 {
734         TAILQ_INIT(&pci_driver_list);
735         TAILQ_INIT(&pci_device_list);
736
737         /* for debug purposes, PCI can be disabled */
738         if (internal_config.no_pci)
739                 return 0;
740
741         if (rte_eal_pci_scan() < 0) {
742                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
743                 return -1;
744         }
745 #ifdef VFIO_PRESENT
746         pci_vfio_enable();
747
748         if (pci_vfio_is_enabled()) {
749
750                 /* if we are primary process, create a thread to communicate with
751                  * secondary processes. the thread will use a socket to wait for
752                  * requests from secondary process to send open file descriptors,
753                  * because VFIO does not allow multiple open descriptors on a group or
754                  * VFIO container.
755                  */
756                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
757                                 pci_vfio_mp_sync_setup() < 0)
758                         return -1;
759         }
760 #endif
761         return 0;
762 }