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