eal: introduce PCI ioport API
[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_UNKNOWN;
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 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 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 /* Read PCI config space. */
575 int rte_eal_pci_read_config(const struct rte_pci_device *device,
576                             void *buf, size_t len, off_t offset)
577 {
578         const struct rte_intr_handle *intr_handle = &device->intr_handle;
579
580         switch (intr_handle->type) {
581         case RTE_INTR_HANDLE_UIO:
582         case RTE_INTR_HANDLE_UIO_INTX:
583                 return pci_uio_read_config(intr_handle, buf, len, offset);
584
585 #ifdef VFIO_PRESENT
586         case RTE_INTR_HANDLE_VFIO_MSIX:
587         case RTE_INTR_HANDLE_VFIO_MSI:
588         case RTE_INTR_HANDLE_VFIO_LEGACY:
589                 return pci_vfio_read_config(intr_handle, buf, len, offset);
590 #endif
591         default:
592                 RTE_LOG(ERR, EAL,
593                         "Unknown handle type of fd %d\n",
594                                         intr_handle->fd);
595                 return -1;
596         }
597 }
598
599 /* Write PCI config space. */
600 int rte_eal_pci_write_config(const struct rte_pci_device *device,
601                              const void *buf, size_t len, off_t offset)
602 {
603         const struct rte_intr_handle *intr_handle = &device->intr_handle;
604
605         switch (intr_handle->type) {
606         case RTE_INTR_HANDLE_UIO:
607         case RTE_INTR_HANDLE_UIO_INTX:
608                 return pci_uio_write_config(intr_handle, buf, len, offset);
609
610 #ifdef VFIO_PRESENT
611         case RTE_INTR_HANDLE_VFIO_MSIX:
612         case RTE_INTR_HANDLE_VFIO_MSI:
613         case RTE_INTR_HANDLE_VFIO_LEGACY:
614                 return pci_vfio_write_config(intr_handle, buf, len, offset);
615 #endif
616         default:
617                 RTE_LOG(ERR, EAL,
618                         "Unknown handle type of fd %d\n",
619                                         intr_handle->fd);
620                 return -1;
621         }
622 }
623
624 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
625 static int
626 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
627                struct rte_pci_ioport *p)
628 {
629         uint16_t start, end;
630         FILE *fp;
631         char *line = NULL;
632         char pci_id[16];
633         int found = 0;
634         size_t linesz;
635
636         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
637                  dev->addr.domain, dev->addr.bus,
638                  dev->addr.devid, dev->addr.function);
639
640         fp = fopen("/proc/ioports", "r");
641         if (fp == NULL) {
642                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
643                 return -1;
644         }
645
646         while (getdelim(&line, &linesz, '\n', fp) > 0) {
647                 char *ptr = line;
648                 char *left;
649                 int n;
650
651                 n = strcspn(ptr, ":");
652                 ptr[n] = 0;
653                 left = &ptr[n + 1];
654
655                 while (*left && isspace(*left))
656                         left++;
657
658                 if (!strncmp(left, pci_id, strlen(pci_id))) {
659                         found = 1;
660
661                         while (*ptr && isspace(*ptr))
662                                 ptr++;
663
664                         sscanf(ptr, "%04hx-%04hx", &start, &end);
665
666                         break;
667                 }
668         }
669
670         free(line);
671         fclose(fp);
672
673         if (!found)
674                 return -1;
675
676         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
677         p->base = start;
678         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
679
680         return 0;
681 }
682 #endif
683
684 int
685 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
686                        struct rte_pci_ioport *p)
687 {
688         int ret;
689
690         switch (dev->kdrv) {
691 #ifdef VFIO_PRESENT
692         case RTE_KDRV_VFIO:
693                 ret = -1;
694                 if (pci_vfio_is_enabled())
695                         ret = pci_vfio_ioport_map(dev, bar, p);
696                 break;
697 #endif
698         case RTE_KDRV_IGB_UIO:
699         case RTE_KDRV_UIO_GENERIC:
700                 ret = pci_uio_ioport_map(dev, bar, p);
701                 break;
702         default:
703 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
704                 /* special case for x86 ... */
705                 ret = pci_ioport_map(dev, bar, p);
706 #else
707                 ret = -1;
708 #endif
709                 break;
710         }
711
712         if (!ret)
713                 p->dev = dev;
714
715         return ret;
716 }
717
718 void
719 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
720                         void *data, size_t len, off_t offset)
721 {
722         switch (p->dev->kdrv) {
723 #ifdef VFIO_PRESENT
724         case RTE_KDRV_VFIO:
725                 pci_vfio_ioport_read(p, data, len, offset);
726                 break;
727 #endif
728         case RTE_KDRV_IGB_UIO:
729         case RTE_KDRV_UIO_GENERIC:
730                 pci_uio_ioport_read(p, data, len, offset);
731                 break;
732         default:
733 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
734                 /* special case for x86 ... */
735                 pci_uio_ioport_read(p, data, len, offset);
736 #endif
737                 break;
738         }
739 }
740
741 void
742 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
743                          const void *data, size_t len, off_t offset)
744 {
745         switch (p->dev->kdrv) {
746 #ifdef VFIO_PRESENT
747         case RTE_KDRV_VFIO:
748                 pci_vfio_ioport_write(p, data, len, offset);
749                 break;
750 #endif
751         case RTE_KDRV_IGB_UIO:
752         case RTE_KDRV_UIO_GENERIC:
753                 pci_uio_ioport_write(p, data, len, offset);
754                 break;
755         default:
756 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
757                 /* special case for x86 ... */
758                 pci_uio_ioport_write(p, data, len, offset);
759 #endif
760                 break;
761         }
762 }
763
764 int
765 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
766 {
767         int ret;
768
769         switch (p->dev->kdrv) {
770 #ifdef VFIO_PRESENT
771         case RTE_KDRV_VFIO:
772                 ret = -1;
773                 if (pci_vfio_is_enabled())
774                         ret = pci_vfio_ioport_unmap(p);
775                 break;
776 #endif
777         case RTE_KDRV_IGB_UIO:
778         case RTE_KDRV_UIO_GENERIC:
779                 ret = pci_uio_ioport_unmap(p);
780                 break;
781         default:
782 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
783                 /* special case for x86 ... nothing to do */
784                 ret = 0;
785 #else
786                 ret = -1;
787 #endif
788                 break;
789         }
790
791         return ret;
792 }
793
794 /* Init the PCI EAL subsystem */
795 int
796 rte_eal_pci_init(void)
797 {
798         TAILQ_INIT(&pci_driver_list);
799         TAILQ_INIT(&pci_device_list);
800
801         /* for debug purposes, PCI can be disabled */
802         if (internal_config.no_pci)
803                 return 0;
804
805         if (rte_eal_pci_scan() < 0) {
806                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
807                 return -1;
808         }
809 #ifdef VFIO_PRESENT
810         pci_vfio_enable();
811
812         if (pci_vfio_is_enabled()) {
813
814                 /* if we are primary process, create a thread to communicate with
815                  * secondary processes. the thread will use a socket to wait for
816                  * requests from secondary process to send open file descriptors,
817                  * because VFIO does not allow multiple open descriptors on a group or
818                  * VFIO container.
819                  */
820                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
821                                 pci_vfio_mp_sync_setup() < 0)
822                         return -1;
823         }
824 #endif
825         return 0;
826 }