lib: sysfs parsing
[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_per_lcore.h>
63 #include <rte_lcore.h>
64 #include <rte_malloc.h>
65 #include <rte_string_fns.h>
66 #include <rte_debug.h>
67
68 #include "eal_internal_cfg.h"
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_NEWID "/sys/bus/pci/drivers/%s/new_id"
90 #define UIO_BIND  "/sys/bus/pci/drivers/%s/bind"
91
92 /* maximum time to wait that /dev/uioX appears */
93 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
94
95 /*
96  * For multi-process we need to reproduce all PCI mappings in secondary
97  * processes, so save them in a tailq.
98  */
99 struct uio_resource {
100         TAILQ_ENTRY(uio_resource) next;
101
102         struct rte_pci_addr pci_addr;
103         void *addr;
104         char path[PATH_MAX];
105         unsigned long size;
106         unsigned long offset;
107 };
108
109 TAILQ_HEAD(uio_res_list, uio_resource);
110
111 static struct uio_res_list *uio_res_list = NULL;
112
113 /*
114  * Check that a kernel module is loaded. Returns 0 on success, or if the
115  * parameter is NULL, or -1 if the module is not loaded.
116  */
117 static int
118 pci_uio_check_module(const char *module_name)
119 {
120         FILE *f;
121         unsigned i;
122         char buf[BUFSIZ];
123
124         if (module_name == NULL)
125                 return 0;
126
127         f = fopen(PROC_MODULES, "r");
128         if (f == NULL) {
129                 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES"\n");
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         RTE_LOG(ERR, EAL, "Cannot find %s in "PROC_MODULES"\n", module_name);
147         return -1;
148 }
149
150 /* bind a PCI to the kernel module driver */
151 static int
152 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
153 {
154         FILE *f;
155         int n;
156         char buf[BUFSIZ];
157         char uio_newid[PATH_MAX];
158         char uio_bind[PATH_MAX];
159         struct rte_pci_addr *loc = &dev->addr;
160
161         RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT" to %s driver\n",
162                 loc->domain, loc->bus, loc->devid, loc->function, module_name);
163
164         n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_NEWID, module_name);
165         if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
166                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
167                 return -1;
168         }
169         n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_BIND, module_name);
170         if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
171                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
172                 return -1;
173         }
174
175         n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
176                         dev->id.vendor_id, dev->id.device_id);
177         if ((n < 0) || (n >= (int)sizeof(buf))) {
178                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
179                 return -1;
180         }
181
182         f = fopen(uio_newid, "w");
183         if (f == NULL) {
184                 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
185                 return -1;
186         }
187         if (fwrite(buf, n, 1, f) == 0) {
188                 fclose(f);
189                 return -1;
190         }
191         fclose(f);
192
193         f = fopen(uio_bind, "w");
194         if (f == NULL) {
195                 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_bind);
196                 return -1;
197         }
198         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
199                          loc->domain, loc->bus, loc->devid, loc->function);
200         if ((n < 0) || (n >= (int)sizeof(buf))) {
201                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
202                 fclose(f);
203                 return -1;
204         }
205         if (fwrite(buf, n, 1, f) == 0) {
206                 fclose(f);
207                 return -1;
208         }
209
210         RTE_LOG(DEBUG, EAL, "Device bound\n");
211
212         fclose(f);
213         return 0;
214 }
215
216 /* map a particular resource from a file */
217 static void *
218 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, const char *devname,
219                 unsigned long offset, unsigned long size)
220 {
221         unsigned n;
222         int fd;
223         void *mapaddr;
224
225         /*
226          * open devname, and mmap it: it can take some time to
227          * appear, so we wait some time before returning an error
228          */
229         for (n=0; n<UIO_DEV_WAIT_TIMEOUT*10; n++) {
230                 fd = open(devname, O_RDWR);
231                 if (fd >= 0)
232                         break;
233                 if (errno != ENOENT)
234                         break;
235                 usleep(100000);
236         }
237         if (fd < 0) {
238                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", devname, strerror(errno));
239                 goto fail;
240         }
241
242         /* Map the PCI memory resource of device */
243         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
244                         MAP_SHARED, fd, offset);
245         if (mapaddr == MAP_FAILED ||
246                         (requested_addr != NULL && mapaddr != requested_addr)) {
247                 RTE_LOG(ERR, EAL, "%s(): cannot mmap %s: %s\n", __func__,
248                         devname, strerror(errno));
249                 close(fd);
250                 goto fail;
251         }
252         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
253                 /* save fd if in primary process */
254                 dev->intr_handle.fd = fd;
255                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
256         } else {
257                 /* fd is not needed in slave process, close it */
258                 dev->intr_handle.fd = -1;
259                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
260                 close(fd);
261         }
262
263         RTE_LOG(DEBUG, EAL, "PCI memory mapped at %p\n", mapaddr);
264
265         return mapaddr;
266
267 fail:
268         dev->intr_handle.fd = -1;
269         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
270
271         return NULL;
272 }
273 /* map the PCI resource of a PCI device in virtual memory */
274 static int
275 pci_uio_map_resource(struct rte_pci_device *dev)
276 {
277         struct dirent *e;
278         DIR *dir;
279         char dirname[PATH_MAX];
280         char dirname2[PATH_MAX];
281         char filename[PATH_MAX];
282         char devname[PATH_MAX]; /* contains the /dev/uioX */
283         void *mapaddr;
284         unsigned uio_num;
285         unsigned long size, offset;
286         struct rte_pci_addr *loc = &dev->addr;
287         struct uio_resource *uio_res;
288
289         RTE_LOG(DEBUG, EAL, "map PCI resource for device "PCI_PRI_FMT"\n",
290                 loc->domain, loc->bus, loc->devid, loc->function);
291
292         /* secondary processes - use already recorded details */
293         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
294
295                         TAILQ_FOREACH(uio_res, uio_res_list, next) {
296                                 /* skip this element if it doesn't match our PCI address */
297                                 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
298                                         continue;
299
300                                 if (pci_map_resource(dev, uio_res->addr, uio_res->path, \
301                                                 uio_res->offset, uio_res->size) == uio_res->addr)
302                                         return 0;
303                                 else {
304                                         RTE_LOG(ERR, EAL, "Cannot mmap device resource\n");
305                                         return -1;
306                                 }
307                         }
308                         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
309                         return -1;
310         }
311
312         /* depending on kernel version, uio can be located in uio/uioX
313          * or uio:uioX */
314
315         rte_snprintf(dirname, sizeof(dirname),
316                  "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
317                  loc->domain, loc->bus, loc->devid, loc->function);
318
319         dir = opendir(dirname);
320         if (dir == NULL) {
321                 /* retry with the parent directory */
322                 rte_snprintf(dirname, sizeof(dirname),
323                          "/sys/bus/pci/devices/" PCI_PRI_FMT,
324                          loc->domain, loc->bus, loc->devid, loc->function);
325                 dir = opendir(dirname);
326
327                 if (dir == NULL) {
328                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
329                         return -1;
330                 }
331         }
332
333         /* take the first file starting with "uio" */
334         while ((e = readdir(dir)) != NULL) {
335                 int shortprefix_len = sizeof("uio") - 1; /* format could be uio%d ...*/
336                 int longprefix_len = sizeof("uio:uio") - 1; /* ... or uio:uio%d */
337                 char *endptr;
338
339                 if (strncmp(e->d_name, "uio", 3) != 0)
340                         continue;
341
342                 /* first try uio%d */
343                 errno = 0;
344                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
345                 if (errno == 0 && endptr != e->d_name) {
346                         rte_snprintf(dirname2, sizeof(dirname2),
347                                  "%s/uio%u", dirname, uio_num);
348                         break;
349                 }
350
351                 /* then try uio:uio%d */
352                 errno = 0;
353                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
354                 if (errno == 0 && endptr != e->d_name) {
355                         rte_snprintf(dirname2, sizeof(dirname2),
356                                  "%s/uio:uio%u", dirname, uio_num);
357                         break;
358                 }
359         }
360         closedir(dir);
361
362         /* No uio resource found */
363         if (e == NULL)
364                 return 0;
365
366         /* get mapping offset */
367         rte_snprintf(filename, sizeof(filename),
368                  "%s/maps/map0/offset", dirname2);
369         if (eal_parse_sysfs_value(filename, &offset) < 0) {
370                 RTE_LOG(ERR, EAL, "%s(): cannot parse offset\n",
371                         __func__);
372                 return -1;
373         }
374
375         /* get mapping size */
376         rte_snprintf(filename, sizeof(filename),
377                  "%s/maps/map0/size", dirname2);
378         if (eal_parse_sysfs_value(filename, &size) < 0) {
379                 RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
380                         __func__);
381                 return -1;
382         }
383
384         /* open and mmap /dev/uioX */
385         rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
386         mapaddr = pci_map_resource(dev, NULL, devname, offset, size);
387         if (mapaddr == NULL)
388                 return -1;
389         dev->mem_resource.addr = mapaddr;
390
391         /* save the mapping details for secondary processes*/
392         uio_res = rte_malloc("UIO_RES", sizeof(*uio_res), 0);
393         if (uio_res == NULL){
394                 RTE_LOG(ERR, EAL, "%s(): cannot store uio mmap details\n", __func__);
395                 return -1;
396         }
397         uio_res->addr = mapaddr;
398         uio_res->offset = offset;
399         uio_res->size = size;
400         rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
401         memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
402
403         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
404
405         return 0;
406 }
407
408 /* parse the "resource" sysfs file */
409 #define IORESOURCE_MEM  0x00000200
410
411 static int
412 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
413 {
414         FILE *f;
415         char buf[BUFSIZ];
416         union pci_resource_info {
417                 struct {
418                         char *phys_addr;
419                         char *end_addr;
420                         char *flags;
421                 };
422                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
423         } res_info;
424         int i;
425         uint64_t phys_addr, end_addr, flags;
426
427         f = fopen(filename, "r");
428         if (f == NULL) {
429                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
430                 return -1;
431         }
432
433         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
434
435                 if (fgets(buf, sizeof(buf), f) == NULL) {
436                         RTE_LOG(ERR, EAL, "%s(): cannot read resource\n", __func__);
437                         goto error;
438                 }
439
440                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
441                         RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
442                         goto error;
443                 }
444                 errno = 0;
445                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
446                 end_addr = strtoull(res_info.end_addr, NULL, 16);
447                 flags = strtoull(res_info.flags, NULL, 16);
448                 if (errno != 0) {
449                         RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
450                         goto error;
451                 }
452
453                 if (flags & IORESOURCE_MEM) {
454                         dev->mem_resource.phys_addr = phys_addr;
455                         dev->mem_resource.len = end_addr - phys_addr + 1;
456                         dev->mem_resource.addr = NULL; /* not mapped for now */
457                         break;
458                 }
459         }
460         fclose(f);
461         return 0;
462
463 error:
464         fclose(f);
465         return -1;
466 }
467
468
469 /* Scan one pci sysfs entry, and fill the devices list from it. */
470 static int
471 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
472              uint8_t devid, uint8_t function)
473 {
474         char filename[PATH_MAX];
475         unsigned long tmp;
476         struct rte_pci_device *dev;
477
478         dev = malloc(sizeof(*dev));
479         if (dev == NULL) {
480                 return -1;
481         }
482
483         memset(dev, 0, sizeof(*dev));
484         dev->addr.domain = domain;
485         dev->addr.bus = bus;
486         dev->addr.devid = devid;
487         dev->addr.function = function;
488
489         /* get vendor id */
490         rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
491         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
492                 free(dev);
493                 return -1;
494         }
495         dev->id.vendor_id = (uint16_t)tmp;
496
497         /* get device id */
498         rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
499         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
500                 free(dev);
501                 return -1;
502         }
503         dev->id.device_id = (uint16_t)tmp;
504
505         /* get subsystem_vendor id */
506         rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
507                  dirname);
508         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
509                 free(dev);
510                 return -1;
511         }
512         dev->id.subsystem_vendor_id = (uint16_t)tmp;
513
514         /* get subsystem_device id */
515         rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
516                  dirname);
517         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
518                 free(dev);
519                 return -1;
520         }
521         dev->id.subsystem_device_id = (uint16_t)tmp;
522
523         /* parse resources */
524         rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
525         if (pci_parse_sysfs_resource(filename, dev) < 0) {
526                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
527                 free(dev);
528                 return -1;
529         }
530
531         /* device is valid, add in list */
532         TAILQ_INSERT_TAIL(&device_list, dev, next);
533
534         return 0;
535 }
536
537 /*
538  * split up a pci address into its constituent parts.
539  */
540 static int
541 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
542                 uint8_t *bus, uint8_t *devid, uint8_t *function)
543 {
544         /* first split on ':' */
545         union splitaddr {
546                 struct {
547                         char *domain;
548                         char *bus;
549                         char *devid;
550                         char *function;
551                 };
552                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
553         } splitaddr;
554
555         char *buf_copy = strndup(buf, bufsize);
556         if (buf_copy == NULL)
557                 return -1;
558
559         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
560                         != PCI_FMT_NVAL - 1)
561                 goto error;
562         /* final split is on '.' between devid and function */
563         splitaddr.function = strchr(splitaddr.devid,'.');
564         if (splitaddr.function == NULL)
565                 goto error;
566         *splitaddr.function++ = '\0';
567
568         /* now convert to int values */
569         errno = 0;
570         *domain = (uint8_t)strtoul(splitaddr.domain, NULL, 16);
571         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
572         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
573         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
574         if (errno != 0)
575                 goto error;
576
577         free(buf_copy); /* free the copy made with strdup */
578         return 0;
579 error:
580         free(buf_copy);
581         return -1;
582 }
583
584 /*
585  * Scan the content of the PCI bus, and the devices in the devices
586  * list
587  */
588 static int
589 pci_scan(void)
590 {
591         struct dirent *e;
592         DIR *dir;
593         char dirname[PATH_MAX];
594         uint16_t domain;
595         uint8_t bus, devid, function;
596
597         dir = opendir(SYSFS_PCI_DEVICES);
598         if (dir == NULL) {
599                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
600                         __func__, strerror(errno));
601                 return -1;
602         }
603
604         while ((e = readdir(dir)) != NULL) {
605                 if (e->d_name[0] == '.')
606                         continue;
607
608                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
609                                 &bus, &devid, &function) != 0)
610                         continue;
611
612                 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
613                          e->d_name);
614                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
615                         goto error;
616         }
617         closedir(dir);
618         return 0;
619
620 error:
621         closedir(dir);
622         return -1;
623 }
624
625 /* unbind kernel driver for this device */
626 static int
627 pci_unbind_kernel_driver(struct rte_pci_device *dev)
628 {
629         int n;
630         FILE *f;
631         char filename[PATH_MAX];
632         char buf[BUFSIZ];
633         struct rte_pci_addr *loc = &dev->addr;
634
635         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
636         rte_snprintf(filename, sizeof(filename),
637                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
638                  loc->domain, loc->bus, loc->devid, loc->function);
639
640         RTE_LOG(DEBUG, EAL, "unbind kernel driver %s\n", filename);
641
642         f = fopen(filename, "w");
643         if (f == NULL) /* device was not bound */
644                 return 0;
645
646         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
647                      loc->domain, loc->bus, loc->devid, loc->function);
648         if ((n < 0) || (n >= (int)sizeof(buf))) {
649                 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
650                 goto error;
651         }
652         if (fwrite(buf, n, 1, f) == 0)
653                 goto error;
654
655         fclose(f);
656         return 0;
657
658 error:
659         fclose(f);
660         return -1;
661 }
662
663 /*
664  * If vendor/device ID match, call the devinit() function of the
665  * driver.
666  */
667 int
668 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
669 {
670         struct rte_pci_id *id_table;
671         const char *module_name = NULL;
672         int ret;
673
674         if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
675                 module_name = IGB_UIO_NAME;
676
677         ret = pci_uio_check_module(module_name);
678         if (ret != 0)
679                 rte_exit(1, "The %s module is required by the %s driver\n",
680                                 module_name, dr->name);
681
682         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
683
684                 /* check if device's identifiers match the driver's ones */
685                 if (id_table->vendor_id != dev->id.vendor_id &&
686                     id_table->vendor_id != PCI_ANY_ID)
687                         continue;
688                 if (id_table->device_id != dev->id.device_id &&
689                     id_table->device_id != PCI_ANY_ID)
690                         continue;
691                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
692                     id_table->subsystem_vendor_id != PCI_ANY_ID)
693                         continue;
694                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
695                     id_table->subsystem_device_id != PCI_ANY_ID)
696                         continue;
697
698                 RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n",
699                 dev->id.vendor_id, dev->id.device_id, dr->name);
700
701                 /* Unbind PCI devices if needed */
702                 if (module_name != NULL) {
703                         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
704                         /* unbind current driver, bind ours */
705                                 if (pci_unbind_kernel_driver(dev) < 0)
706                                         return -1;
707                                 if (pci_uio_bind_device(dev, module_name) < 0)
708                                         return -1;
709                         }
710                         /* map the NIC resources */
711                         if (pci_uio_map_resource(dev) < 0)
712                                 return -1;
713                 }
714                 /* call the driver devinit() function */
715                 return dr->devinit(dr, dev);
716
717         }
718         return -1;
719 }
720
721 /* Init the PCI EAL subsystem */
722 int
723 rte_eal_pci_init(void)
724 {
725         TAILQ_INIT(&driver_list);
726         TAILQ_INIT(&device_list);
727         uio_res_list = RTE_TAILQ_RESERVE("PCI_RESOURCE_LIST", uio_res_list);
728
729         /* for debug purposes, PCI can be disabled */
730         if (internal_config.no_pci)
731                 return 0;
732
733         if (pci_scan() < 0) {
734                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
735                 return -1;
736         }
737         return 0;
738 }