ec5de8da688126e5898bb7999cf64969a58dcefd
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46 #include <errno.h>
47 #include <dirent.h>
48 #include <limits.h>
49 #include <sys/queue.h>
50 #include <sys/mman.h>
51 #include <sys/ioctl.h>
52
53 #include <rte_interrupts.h>
54 #include <rte_log.h>
55 #include <rte_pci.h>
56 #include <rte_common.h>
57 #include <rte_launch.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_tailq.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_debug.h>
68
69 #include "eal_filesystem.h"
70 #include "eal_private.h"
71
72 /**
73  * @file
74  * PCI probing under linux
75  *
76  * This code is used to simulate a PCI probe by parsing information in
77  * sysfs. Moreover, when a registered driver matches a device, the
78  * kernel driver currently using it is unloaded and replaced by
79  * igb_uio module, which is a very minimal userland driver for Intel
80  * network card, only providing access to PCI BAR to applications, and
81  * enabling bus master.
82  */
83
84
85 #define PROC_MODULES "/proc/modules"
86
87 #define IGB_UIO_NAME "igb_uio"
88
89 #define UIO_DRV_PATH  "/sys/bus/pci/drivers/%s"
90
91 /* maximum time to wait that /dev/uioX appears */
92 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
93
94 /*
95  * For multi-process we need to reproduce all PCI mappings in secondary
96  * processes, so save them in a tailq.
97  */
98 struct uio_resource {
99         TAILQ_ENTRY(uio_resource) next;
100
101         struct rte_pci_addr pci_addr;
102         void *addr;
103         char path[PATH_MAX];
104         unsigned long size;
105         unsigned long offset;
106 };
107
108 TAILQ_HEAD(uio_res_list, uio_resource);
109
110 static struct uio_res_list *uio_res_list = NULL;
111
112 /*
113  * Check that a kernel module is loaded. Returns 0 on success, or if the
114  * parameter is NULL, or -1 if the module is not loaded.
115  */
116 static int
117 pci_uio_check_module(const char *module_name)
118 {
119         FILE *f;
120         unsigned i;
121         char buf[BUFSIZ];
122
123         if (module_name == NULL)
124                 return 0;
125
126         f = fopen(PROC_MODULES, "r");
127         if (f == NULL) {
128                 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES": %s\n", 
129                                 strerror(errno));
130                 return -1;
131         }
132
133         while(fgets(buf, sizeof(buf), f) != NULL) {
134
135                 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
136                         if (isspace(buf[i]))
137                             buf[i] = '\0';
138                 }
139
140                 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
141                         fclose(f);
142                         return 0;
143                 }
144         }
145         fclose(f);
146         return -1;
147 }
148
149 /* bind a PCI to the kernel module driver */
150 static int
151 pci_bind_device(struct rte_pci_device *dev, char dr_path[])
152 {
153         FILE *f;
154         int n;
155         char buf[BUFSIZ];
156         char dev_bind[PATH_MAX];
157         struct rte_pci_addr *loc = &dev->addr;
158
159         RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT"\n",
160                         loc->domain, loc->bus, loc->devid, loc->function);
161
162         n = rte_snprintf(dev_bind, sizeof(dev_bind), "%s/bind", dr_path);
163         if ((n < 0) || (n >= (int)sizeof(buf))) {
164                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf device bind path\n");
165                 return -1;
166         }
167
168         f = fopen(dev_bind, "w");
169         if (f == NULL) {
170                 RTE_LOG(ERR, EAL, "Cannot open %s\n", dev->previous_dr);
171                 return -1;
172         }
173         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
174                          loc->domain, loc->bus, loc->devid, loc->function);
175         if ((n < 0) || (n >= (int)sizeof(buf))) {
176                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
177                 fclose(f);
178                 return -1;
179         }
180         if (fwrite(buf, n, 1, f) == 0) {
181                 fclose(f);
182                 return -1;
183         }
184
185         RTE_LOG(DEBUG, EAL, "Device bound\n");
186
187         fclose(f);
188         return 0;
189 }
190
191 static int
192 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
193 {
194         FILE *f;
195         int n;
196         char buf[BUFSIZ];
197         char uio_newid[PATH_MAX];
198         char uio_bind[PATH_MAX];
199
200         n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_DRV_PATH "/new_id", module_name);
201         if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
202                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
203                 return -1;
204         }
205
206         n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_DRV_PATH, module_name);
207         if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
208                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
209                 return -1;
210         }
211
212         n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
213                         dev->id.vendor_id, dev->id.device_id);
214         if ((n < 0) || (n >= (int)sizeof(buf))) {
215                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
216                 return -1;
217         }
218
219         f = fopen(uio_newid, "w");
220         if (f == NULL) {
221                 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
222                 return -1;
223         }
224         if (fwrite(buf, n, 1, f) == 0) {
225                 fclose(f);
226                 return -1;
227         }
228         fclose(f);
229
230         pci_bind_device(dev, uio_bind);
231         return 0;
232 }
233
234
235 /* map a particular resource from a file */
236 static void *
237 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, const char *devname,
238                 unsigned long offset, unsigned long size)
239 {
240         unsigned n;
241         int fd;
242         void *mapaddr;
243
244         /*
245          * open devname, and mmap it: it can take some time to
246          * appear, so we wait some time before returning an error
247          */
248         for (n=0; n<UIO_DEV_WAIT_TIMEOUT*10; n++) {
249                 fd = open(devname, O_RDWR);
250                 if (fd >= 0)
251                         break;
252                 if (errno != ENOENT)
253                         break;
254                 usleep(100000);
255         }
256         if (fd < 0) {
257                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", devname, strerror(errno));
258                 goto fail;
259         }
260
261         /* Map the PCI memory resource of device */
262         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
263                         MAP_SHARED, fd, offset);
264         if (mapaddr == MAP_FAILED ||
265                         (requested_addr != NULL && mapaddr != requested_addr)) {
266                 RTE_LOG(ERR, EAL, "%s(): cannot mmap %s: %s\n", __func__,
267                         devname, strerror(errno));
268                 close(fd);
269                 goto fail;
270         }
271         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
272                 /* save fd if in primary process */
273                 dev->intr_handle.fd = fd;
274                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
275         } else {
276                 /* fd is not needed in slave process, close it */
277                 dev->intr_handle.fd = -1;
278                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
279                 close(fd);
280         }
281
282         RTE_LOG(DEBUG, EAL, "PCI memory mapped at %p\n", mapaddr);
283
284         return mapaddr;
285
286 fail:
287         dev->intr_handle.fd = -1;
288         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
289
290         return NULL;
291 }
292 /* map the PCI resource of a PCI device in virtual memory */
293 static int
294 pci_uio_map_resource(struct rte_pci_device *dev)
295 {
296         struct dirent *e;
297         DIR *dir;
298         char dirname[PATH_MAX];
299         char dirname2[PATH_MAX];
300         char filename[PATH_MAX];
301         char devname[PATH_MAX]; /* contains the /dev/uioX */
302         void *mapaddr;
303         unsigned uio_num;
304         unsigned long size, offset;
305         struct rte_pci_addr *loc = &dev->addr;
306         struct uio_resource *uio_res;
307
308         RTE_LOG(DEBUG, EAL, "map PCI resource for device "PCI_PRI_FMT"\n",
309                 loc->domain, loc->bus, loc->devid, loc->function);
310
311         /* secondary processes - use already recorded details */
312         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
313
314                         TAILQ_FOREACH(uio_res, uio_res_list, next) {
315                                 /* skip this element if it doesn't match our PCI address */
316                                 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
317                                         continue;
318
319                                 if (pci_map_resource(dev, uio_res->addr, uio_res->path, \
320                                                 uio_res->offset, uio_res->size) == uio_res->addr)
321                                         return 0;
322                                 else {
323                                         RTE_LOG(ERR, EAL, "Cannot mmap device resource\n");
324                                         return -1;
325                                 }
326                         }
327                         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
328                         return -1;
329         }
330
331         /* depending on kernel version, uio can be located in uio/uioX
332          * or uio:uioX */
333
334         rte_snprintf(dirname, sizeof(dirname),
335                  "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
336                  loc->domain, loc->bus, loc->devid, loc->function);
337
338         dir = opendir(dirname);
339         if (dir == NULL) {
340                 /* retry with the parent directory */
341                 rte_snprintf(dirname, sizeof(dirname),
342                          "/sys/bus/pci/devices/" PCI_PRI_FMT,
343                          loc->domain, loc->bus, loc->devid, loc->function);
344                 dir = opendir(dirname);
345
346                 if (dir == NULL) {
347                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
348                         return -1;
349                 }
350         }
351
352         /* take the first file starting with "uio" */
353         while ((e = readdir(dir)) != NULL) {
354                 int shortprefix_len = sizeof("uio") - 1; /* format could be uio%d ...*/
355                 int longprefix_len = sizeof("uio:uio") - 1; /* ... or uio:uio%d */
356                 char *endptr;
357
358                 if (strncmp(e->d_name, "uio", 3) != 0)
359                         continue;
360
361                 /* first try uio%d */
362                 errno = 0;
363                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
364                 if (errno == 0 && endptr != e->d_name) {
365                         rte_snprintf(dirname2, sizeof(dirname2),
366                                  "%s/uio%u", dirname, uio_num);
367                         break;
368                 }
369
370                 /* then try uio:uio%d */
371                 errno = 0;
372                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
373                 if (errno == 0 && endptr != e->d_name) {
374                         rte_snprintf(dirname2, sizeof(dirname2),
375                                  "%s/uio:uio%u", dirname, uio_num);
376                         break;
377                 }
378         }
379         closedir(dir);
380
381         /* No uio resource found */
382         if (e == NULL)
383                 return 0;
384
385         /* get mapping offset */
386         rte_snprintf(filename, sizeof(filename),
387                  "%s/maps/map0/offset", dirname2);
388         if (eal_parse_sysfs_value(filename, &offset) < 0) {
389                 RTE_LOG(ERR, EAL, "%s(): cannot parse offset\n",
390                         __func__);
391                 return -1;
392         }
393
394         /* get mapping size */
395         rte_snprintf(filename, sizeof(filename),
396                  "%s/maps/map0/size", dirname2);
397         if (eal_parse_sysfs_value(filename, &size) < 0) {
398                 RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
399                         __func__);
400                 return -1;
401         }
402
403         /* open and mmap /dev/uioX */
404         rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
405         mapaddr = pci_map_resource(dev, NULL, devname, offset, size);
406         if (mapaddr == NULL)
407                 return -1;
408         dev->mem_resource.addr = mapaddr;
409
410         /* save the mapping details for secondary processes*/
411         uio_res = rte_malloc("UIO_RES", sizeof(*uio_res), 0);
412         if (uio_res == NULL) {
413                 RTE_LOG(ERR, EAL, "%s(): cannot store uio mmap details\n", __func__);
414                 return -1;
415         }
416         uio_res->addr = mapaddr;
417         uio_res->offset = offset;
418         uio_res->size = size;
419         rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
420         memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
421
422         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
423
424         return 0;
425 }
426
427 /* parse the "resource" sysfs file */
428 #define IORESOURCE_MEM  0x00000200
429
430 static int
431 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
432 {
433         FILE *f;
434         char buf[BUFSIZ];
435         union pci_resource_info {
436                 struct {
437                         char *phys_addr;
438                         char *end_addr;
439                         char *flags;
440                 };
441                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
442         } res_info;
443         int i;
444         uint64_t phys_addr, end_addr, flags;
445
446         f = fopen(filename, "r");
447         if (f == NULL) {
448                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
449                 return -1;
450         }
451
452         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
453
454                 if (fgets(buf, sizeof(buf), f) == NULL) {
455                         RTE_LOG(ERR, EAL, "%s(): cannot read resource\n", __func__);
456                         goto error;
457                 }
458
459                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
460                         RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
461                         goto error;
462                 }
463                 errno = 0;
464                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
465                 end_addr = strtoull(res_info.end_addr, NULL, 16);
466                 flags = strtoull(res_info.flags, NULL, 16);
467                 if (errno != 0) {
468                         RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
469                         goto error;
470                 }
471
472                 if (flags & IORESOURCE_MEM) {
473                         dev->mem_resource.phys_addr = phys_addr;
474                         dev->mem_resource.len = end_addr - phys_addr + 1;
475                         dev->mem_resource.addr = NULL; /* not mapped for now */
476                         break;
477                 }
478         }
479         fclose(f);
480         return 0;
481
482 error:
483         fclose(f);
484         return -1;
485 }
486
487
488 /* Compare two PCI device addresses. */
489 static int
490 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
491 {
492         uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
493         uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
494
495         if (dev_addr > dev_addr2) 
496                 return 1;       
497         else 
498                 return 0;
499 }
500
501
502 /* Scan one pci sysfs entry, and fill the devices list from it. */
503 static int
504 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
505              uint8_t devid, uint8_t function)
506 {
507         char filename[PATH_MAX];
508         unsigned long tmp;
509         struct rte_pci_device *dev;
510
511         dev = malloc(sizeof(*dev));
512         if (dev == NULL) {
513                 return -1;
514         }
515
516         memset(dev, 0, sizeof(*dev));
517         dev->addr.domain = domain;
518         dev->addr.bus = bus;
519         dev->addr.devid = devid;
520         dev->addr.function = function;
521
522         /* get vendor id */
523         rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
524         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
525                 free(dev);
526                 return -1;
527         }
528         dev->id.vendor_id = (uint16_t)tmp;
529
530         /* get device id */
531         rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
532         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
533                 free(dev);
534                 return -1;
535         }
536         dev->id.device_id = (uint16_t)tmp;
537
538         /* get subsystem_vendor id */
539         rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
540                  dirname);
541         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
542                 free(dev);
543                 return -1;
544         }
545         dev->id.subsystem_vendor_id = (uint16_t)tmp;
546
547         /* get subsystem_device id */
548         rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
549                  dirname);
550         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
551                 free(dev);
552                 return -1;
553         }
554         dev->id.subsystem_device_id = (uint16_t)tmp;
555
556         /* parse resources */
557         rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
558         if (pci_parse_sysfs_resource(filename, dev) < 0) {
559                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
560                 free(dev);
561                 return -1;
562         }
563
564         /* device is valid, add in list (sorted) */
565         if (TAILQ_EMPTY(&device_list)) {
566                 TAILQ_INSERT_TAIL(&device_list, dev, next);
567         }       
568         else {
569                 struct rte_pci_device *dev2 = NULL;
570
571                 TAILQ_FOREACH(dev2, &device_list, next) {
572                         if (pci_addr_comparison(&dev->addr, &dev2->addr))
573                                 continue;
574                         else {
575                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
576                                 return 0;
577                         }
578                 }
579                 TAILQ_INSERT_TAIL(&device_list, dev, next);
580         }
581                                 
582         return 0;
583 }
584
585 /*
586  * split up a pci address into its constituent parts.
587  */
588 static int
589 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
590                 uint8_t *bus, uint8_t *devid, uint8_t *function)
591 {
592         /* first split on ':' */
593         union splitaddr {
594                 struct {
595                         char *domain;
596                         char *bus;
597                         char *devid;
598                         char *function;
599                 };
600                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
601         } splitaddr;
602
603         char *buf_copy = strndup(buf, bufsize);
604         if (buf_copy == NULL)
605                 return -1;
606
607         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
608                         != PCI_FMT_NVAL - 1)
609                 goto error;
610         /* final split is on '.' between devid and function */
611         splitaddr.function = strchr(splitaddr.devid,'.');
612         if (splitaddr.function == NULL)
613                 goto error;
614         *splitaddr.function++ = '\0';
615
616         /* now convert to int values */
617         errno = 0;
618         *domain = (uint8_t)strtoul(splitaddr.domain, NULL, 16);
619         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
620         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
621         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
622         if (errno != 0)
623                 goto error;
624
625         free(buf_copy); /* free the copy made with strdup */
626         return 0;
627 error:
628         free(buf_copy);
629         return -1;
630 }
631
632 /*
633  * Scan the content of the PCI bus, and the devices in the devices
634  * list
635  */
636 static int
637 pci_scan(void)
638 {
639         struct dirent *e;
640         DIR *dir;
641         char dirname[PATH_MAX];
642         uint16_t domain;
643         uint8_t bus, devid, function;
644
645         dir = opendir(SYSFS_PCI_DEVICES);
646         if (dir == NULL) {
647                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
648                         __func__, strerror(errno));
649                 return -1;
650         }
651
652         while ((e = readdir(dir)) != NULL) {
653                 if (e->d_name[0] == '.')
654                         continue;
655
656                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
657                                 &bus, &devid, &function) != 0)
658                         continue;
659
660                 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
661                          e->d_name);
662                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
663                         goto error;
664         }
665         closedir(dir);
666         return 0;
667
668 error:
669         closedir(dir);
670         return -1;
671 }
672
673 /* unbind kernel driver for this device */
674 static int
675 pci_unbind_kernel_driver(struct rte_pci_device *dev)
676 {
677         int n;
678         FILE *f;
679         char filename[PATH_MAX];
680         char buf[BUFSIZ];
681         struct rte_pci_addr *loc = &dev->addr;
682
683         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
684         rte_snprintf(filename, sizeof(filename),
685                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
686                  loc->domain, loc->bus, loc->devid, loc->function);
687
688         RTE_LOG(DEBUG, EAL, "unbind kernel driver %s\n", filename);
689
690         f = fopen(filename, "w");
691         if (f == NULL) /* device was not bound */
692                 return 0;
693
694         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
695                      loc->domain, loc->bus, loc->devid, loc->function);
696         if ((n < 0) || (n >= (int)sizeof(buf))) {
697                 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
698                 goto error;
699         }
700         if (fwrite(buf, n, 1, f) == 0) {
701                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
702                                 filename);
703                 goto error;
704         }
705
706         fclose(f);
707         return 0;
708
709 error:
710         fclose(f);
711         return -1;
712 }
713
714 static int
715 pci_exit_process(struct rte_pci_device *dev)
716 {
717         if (munmap(dev->mem_resource.addr, dev->mem_resource.len) == -1){
718                 RTE_LOG(ERR, EAL, "Error with munmap\n");
719                 return -1;
720         }
721         if (close(dev->intr_handle.fd) == -1){
722                 RTE_LOG(ERR, EAL, "Error closing interrupt handle\n");
723                 return -1;
724         }
725         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
726                 if (pci_unbind_kernel_driver(dev) < 0){
727                         RTE_LOG(ERR, EAL, "Error unbinding\n");
728                         return -1;
729                 }
730                 if (pci_bind_device(dev, dev->previous_dr) < 0){
731                         RTE_LOG(ERR, EAL, "Error binding\n");
732                         return -1;
733                 }
734         }
735
736         return 0;
737 }
738
739 static void
740 pci_get_previous_driver_path(struct rte_pci_device *dev)
741 {
742         int n;
743         char dev_path[PATH_MAX];
744         char dr_path[PATH_MAX];
745         struct rte_pci_addr *loc = &dev->addr;
746
747         n = rte_snprintf(dev_path, sizeof(dev_path), SYSFS_PCI_DEVICES "/"
748                         PCI_PRI_FMT "/driver", loc->domain, loc->bus, loc->devid, loc->function );
749         if ((n < 0) || (n >= (int)sizeof(dev_path)))
750                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf device filepath\n");
751
752         n = readlink(dev_path, dr_path, sizeof(dr_path));
753         if ((n < 0) || (n >= (int)sizeof(dr_path))){
754                 RTE_LOG(ERR, EAL, "Cannot readlink driver filepath\n");
755                 dev->previous_dr[0] = '\0';
756                 return;
757         }
758         dr_path[n] = '\0';
759
760         if (dr_path[0] != '/')
761                 n = rte_snprintf(dev->previous_dr, sizeof(dev->previous_dr),
762                                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/%s",
763                                 loc->domain, loc->bus, loc->devid, loc->function, dr_path);
764         else
765                 n = rte_snprintf(dev->previous_dr, sizeof(dev->previous_dr),
766                                 "%s", dr_path);
767         if ((n < 0) || (n >= (int)sizeof(dev_path)))
768                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf driver filepath\n");
769 }
770
771 /*
772  * If vendor/device ID match, call the devinit() function of the
773  * driver.
774  */
775 int
776 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
777 {
778         struct rte_pci_id *id_table;
779         const char *module_name = NULL;
780         int uio_status = -1;
781
782         if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
783                 module_name = IGB_UIO_NAME;
784
785         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
786
787                 /* check if device's identifiers match the driver's ones */
788                 if (id_table->vendor_id != dev->id.vendor_id &&
789                                 id_table->vendor_id != PCI_ANY_ID)
790                         continue;
791                 if (id_table->device_id != dev->id.device_id &&
792                                 id_table->device_id != PCI_ANY_ID)
793                         continue;
794                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
795                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
796                         continue;
797                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
798                                 id_table->subsystem_device_id != PCI_ANY_ID)
799                         continue;
800
801                 RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n", dev->id.vendor_id,
802                                 dev->id.device_id, dr->name);
803
804                 /* reference driver structure */
805                 dev->driver = dr;
806
807                 /* no initialization when blacklisted, return without error */
808                 if (dev->blacklisted)
809                         return 0;
810
811                 /* Unbind PCI devices if needed */
812                 if (module_name != NULL) {
813
814                         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
815                                 /* check that our driver is loaded */
816                                 if (uio_status != 0 &&
817                                                 (uio_status = pci_uio_check_module(module_name)) != 0)
818                                         rte_exit(EXIT_FAILURE, "The %s module is required by the "
819                                                         "%s driver\n", module_name, dr->name);
820
821                                 /* unbind current driver, bind ours */
822                                 pci_get_previous_driver_path(dev);
823                                 if (pci_unbind_kernel_driver(dev) < 0)
824                                         return -1;
825                                 if (pci_uio_bind_device(dev, module_name) < 0)
826                                         return -1;
827                         }
828                         /* map the NIC resources */
829                         if (pci_uio_map_resource(dev) < 0)
830                                 return -1;
831                 }
832
833                 /* call the driver devinit() function */
834                 return dr->devinit(dr, dev);
835         }
836         return -1;
837 }
838
839 /*Start the exit process for each dev in use*/
840 void
841 rte_eal_pci_exit(void)
842 {
843         struct rte_pci_device *dev = NULL;
844
845         TAILQ_FOREACH(dev, &device_list, next){
846                 if (dev->previous_dr[0] == '\0')
847                         continue;
848                 if(pci_exit_process(dev) == 1)
849                         RTE_LOG(ERR, EAL, "Exit process failure\n");
850         }
851 }
852
853 /* Init the PCI EAL subsystem */
854 int
855 rte_eal_pci_init(void)
856 {
857         TAILQ_INIT(&driver_list);
858         TAILQ_INIT(&device_list);
859         uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
860
861         /* for debug purposes, PCI can be disabled */
862         if (internal_config.no_pci)
863                 return 0;
864
865         if (pci_scan() < 0) {
866                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
867                 return -1;
868         }
869         return 0;
870 }