pci: rename device and driver lists
[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 <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <inttypes.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <stdarg.h>
45 #include <errno.h>
46 #include <dirent.h>
47 #include <limits.h>
48 #include <sys/queue.h>
49 #include <sys/mman.h>
50 #include <sys/ioctl.h>
51
52 #include <rte_interrupts.h>
53 #include <rte_log.h>
54 #include <rte_pci.h>
55 #include <rte_common.h>
56 #include <rte_launch.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_tailq.h>
60 #include <rte_eal.h>
61 #include <rte_eal_memconfig.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 "rte_pci_dev_ids.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 struct uio_map {
85         void *addr;
86         uint64_t offset;
87         uint64_t size;
88         uint64_t phaddr;
89 };
90
91 /*
92  * For multi-process we need to reproduce all PCI mappings in secondary
93  * processes, so save them in a tailq.
94  */
95 struct uio_resource {
96         TAILQ_ENTRY(uio_resource) next;
97
98         struct rte_pci_addr pci_addr;
99         char path[PATH_MAX];
100         size_t nb_maps;
101         struct uio_map maps[PCI_MAX_RESOURCE];
102 };
103
104 TAILQ_HEAD(uio_res_list, uio_resource);
105
106 static struct uio_res_list *uio_res_list = NULL;
107 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
108
109 /* forward prototype of function called in pci_switch_module below */
110 static int pci_uio_map_resource(struct rte_pci_device *dev);
111
112 #ifdef RTE_EAL_UNBIND_PORTS
113 #define PROC_MODULES "/proc/modules"
114
115 #define IGB_UIO_NAME "igb_uio"
116
117 #define UIO_DRV_PATH  "/sys/bus/pci/drivers/%s"
118
119 /* maximum time to wait that /dev/uioX appears */
120 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
121
122 /*
123  * Check that a kernel module is loaded. Returns 0 on success, or if the
124  * parameter is NULL, or -1 if the module is not loaded.
125  */
126 static int
127 pci_uio_check_module(const char *module_name)
128 {
129         FILE *f;
130         unsigned i;
131         char buf[BUFSIZ];
132
133         if (module_name == NULL)
134                 return 0;
135
136         f = fopen(PROC_MODULES, "r");
137         if (f == NULL) {
138                 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES": %s\n", 
139                                 strerror(errno));
140                 return -1;
141         }
142
143         while(fgets(buf, sizeof(buf), f) != NULL) {
144
145                 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
146                         if (isspace(buf[i]))
147                             buf[i] = '\0';
148                 }
149
150                 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
151                         fclose(f);
152                         return 0;
153                 }
154         }
155         fclose(f);
156         return -1;
157 }
158
159 /* bind a PCI to the kernel module driver */
160 static int
161 pci_bind_device(struct rte_pci_device *dev, char dr_path[])
162 {
163         FILE *f;
164         int n;
165         char buf[BUFSIZ];
166         char dev_bind[PATH_MAX];
167         struct rte_pci_addr *loc = &dev->addr;
168
169         n = rte_snprintf(dev_bind, sizeof(dev_bind), "%s/bind", dr_path);
170         if ((n < 0) || (n >= (int)sizeof(buf))) {
171                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf device bind path\n");
172                 return -1;
173         }
174
175         f = fopen(dev_bind, "w");
176         if (f == NULL) {
177                 RTE_LOG(ERR, EAL, "Cannot open %s\n", dev_bind);
178                 return -1;
179         }
180         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
181                          loc->domain, loc->bus, loc->devid, loc->function);
182         if ((n < 0) || (n >= (int)sizeof(buf))) {
183                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
184                 fclose(f);
185                 return -1;
186         }
187         if (fwrite(buf, n, 1, f) == 0) {
188                 fclose(f);
189                 return -1;
190         }
191
192         fclose(f);
193         return 0;
194 }
195
196 static int
197 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
198 {
199         FILE *f;
200         int n;
201         char buf[BUFSIZ];
202         char uio_newid[PATH_MAX];
203         char uio_bind[PATH_MAX];
204
205         n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_DRV_PATH "/new_id", module_name);
206         if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
207                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
208                 return -1;
209         }
210
211         n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_DRV_PATH, module_name);
212         if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
213                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
214                 return -1;
215         }
216
217         n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
218                         dev->id.vendor_id, dev->id.device_id);
219         if ((n < 0) || (n >= (int)sizeof(buf))) {
220                 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
221                 return -1;
222         }
223
224         f = fopen(uio_newid, "w");
225         if (f == NULL) {
226                 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
227                 return -1;
228         }
229         if (fwrite(buf, n, 1, f) == 0) {
230                 fclose(f);
231                 return -1;
232         }
233         fclose(f);
234
235         pci_bind_device(dev, uio_bind);
236         return 0;
237 }
238
239 /* unbind kernel driver for this device */
240 static int
241 pci_unbind_kernel_driver(struct rte_pci_device *dev)
242 {
243         int n;
244         FILE *f;
245         char filename[PATH_MAX];
246         char buf[BUFSIZ];
247         struct rte_pci_addr *loc = &dev->addr;
248
249         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
250         rte_snprintf(filename, sizeof(filename),
251                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
252                  loc->domain, loc->bus, loc->devid, loc->function);
253
254         f = fopen(filename, "w");
255         if (f == NULL) /* device was not bound */
256                 return 0;
257
258         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
259                      loc->domain, loc->bus, loc->devid, loc->function);
260         if ((n < 0) || (n >= (int)sizeof(buf))) {
261                 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
262                 goto error;
263         }
264         if (fwrite(buf, n, 1, f) == 0) {
265                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
266                                 filename);
267                 goto error;
268         }
269
270         fclose(f);
271         return 0;
272
273 error:
274         fclose(f);
275         return -1;
276 }
277
278
279 static int
280 pci_switch_module(struct rte_pci_driver *dr, struct rte_pci_device *dev,
281                 int uio_status, const char *module_name)
282 {
283         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
284                 /* check that our driver is loaded */
285                 if (uio_status != 0 &&
286                                 (uio_status = pci_uio_check_module(module_name)) != 0)
287                         rte_exit(EXIT_FAILURE, "The %s module is required by the "
288                                         "%s driver\n", module_name, dr->name);
289
290                 /* unbind current driver, bind ours */
291                 if (pci_unbind_kernel_driver(dev) < 0)
292                         return -1;
293                 if (pci_uio_bind_device(dev, module_name) < 0)
294                         return -1;
295         }
296         /* map the NIC resources */
297         if (pci_uio_map_resource(dev) < 0)
298                 return -1;
299
300         return 0;
301 }
302
303 #endif /* ifdef EAL_UNBIND_PORTS */
304
305 /* map a particular resource from a file */
306 static void *
307 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, 
308                 const char *devname, off_t offset, size_t size)
309 {
310         int fd;
311         void *mapaddr;
312
313 #ifdef RTE_EAL_UNBIND_PORTS
314         /*
315          * open devname, and mmap it: it can take some time to
316          * appear, so we wait some time before returning an error
317          */
318         unsigned n;
319         fd = dev->intr_handle.fd;
320         for (n = 0; n < UIO_DEV_WAIT_TIMEOUT*10 && fd < 0; n++) {
321                 errno = 0;
322                 if ((fd = open(devname, O_RDWR)) < 0 && errno != ENOENT)
323                         break;
324                 usleep(100000);
325         }
326 #else
327         /*
328          * open devname, to mmap it
329          */
330         fd = open(devname, O_RDWR);
331 #endif
332         if (fd < 0) {
333                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", 
334                         devname, strerror(errno));
335                 goto fail;
336         }
337
338         /* Map the PCI memory resource of device */
339         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
340                         MAP_SHARED, fd, offset);
341         if (mapaddr == MAP_FAILED ||
342                         (requested_addr != NULL && mapaddr != requested_addr)) {
343                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
344                         " %s (%p)\n", __func__, devname, fd, requested_addr, 
345                         (unsigned long)size, (unsigned long)offset,
346                         strerror(errno), mapaddr);
347                 close(fd);
348                 goto fail;
349         }
350         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
351                 /* save fd if in primary process */
352                 dev->intr_handle.fd = fd;
353                 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
354         } else {
355                 /* fd is not needed in slave process, close it */
356                 dev->intr_handle.fd = -1;
357                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
358                 close(fd);
359         }
360
361         RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
362
363         return mapaddr;
364
365 fail:
366         dev->intr_handle.fd = -1;
367         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
368
369         return NULL;
370 }
371
372 #define OFF_MAX              ((uint64_t)(off_t)-1)
373 static ssize_t
374 pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps)
375 {
376         size_t i;
377         char dirname[PATH_MAX];
378         char filename[PATH_MAX];
379         uint64_t offset, size;
380
381         for (i = 0; i != nb_maps; i++) {
382  
383                 /* check if map directory exists */
384                 rte_snprintf(dirname, sizeof(dirname), 
385                         "%s/maps/map%u", devname, i);
386  
387                 if (access(dirname, F_OK) != 0)
388                         break;
389  
390                 /* get mapping offset */
391                 rte_snprintf(filename, sizeof(filename),
392                         "%s/offset", dirname);
393                 if (pci_parse_sysfs_value(filename, &offset) < 0) {
394                         RTE_LOG(ERR, EAL,
395                                 "%s(): cannot parse offset of %s\n",
396                                 __func__, dirname);
397                         return (-1);
398                 }
399  
400                 /* get mapping size */
401                 rte_snprintf(filename, sizeof(filename),
402                         "%s/size", dirname);
403                 if (pci_parse_sysfs_value(filename, &size) < 0) {
404                         RTE_LOG(ERR, EAL,
405                                 "%s(): cannot parse size of %s\n",
406                                 __func__, dirname);
407                         return (-1);
408                 }
409  
410                 /* get mapping physical address */
411                 rte_snprintf(filename, sizeof(filename),
412                         "%s/addr", dirname);
413                 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
414                         RTE_LOG(ERR, EAL,
415                                 "%s(): cannot parse addr of %s\n",
416                                 __func__, dirname);
417                         return (-1);
418                 }
419
420                 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
421                         RTE_LOG(ERR, EAL,
422                                 "%s(): offset/size exceed system max value\n",
423                                 __func__); 
424                         return (-1);
425                 }
426
427                 maps[i].offset = offset;
428                 maps[i].size = size;
429         }
430         return (i);
431 }
432
433 static int
434 pci_uio_map_secondary(struct rte_pci_device *dev)
435 {
436         size_t i;
437         struct uio_resource *uio_res;
438  
439         TAILQ_FOREACH(uio_res, uio_res_list, next) {
440  
441                 /* skip this element if it doesn't match our PCI address */
442                 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
443                         continue;
444                 
445                 for (i = 0; i != uio_res->nb_maps; i++) {
446                         if (pci_map_resource(dev, uio_res->maps[i].addr,
447                                         uio_res->path,
448                                         (off_t)uio_res->maps[i].offset,
449                                         (size_t)uio_res->maps[i].size) != 
450                                         uio_res->maps[i].addr) {
451                                 RTE_LOG(ERR, EAL,
452                                         "Cannot mmap device resource\n");
453                                 return (-1);
454                         }
455                 }
456                 return (0);
457         }
458
459         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
460         return -1;
461 }
462
463 static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
464 {
465         FILE *f;
466         char filename[PATH_MAX];
467         int ret;
468         unsigned major, minor;
469         dev_t dev;
470
471         /* get the name of the sysfs file that contains the major and minor
472          * of the uio device and read its content */
473         rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
474
475         f = fopen(filename, "r");
476         if (f == NULL) {
477                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
478                         __func__);
479                 return -1;
480         }
481
482         ret = fscanf(f, "%d:%d", &major, &minor);
483         if (ret != 2) {
484                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
485                         __func__);
486                 fclose(f);
487                 return -1;
488         }
489         fclose(f);
490
491         /* create the char device "mknod /dev/uioX c major minor" */
492         rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
493         dev = makedev(major, minor);
494         ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
495         if (f == NULL) {
496                 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
497                         __func__, strerror(errno));
498                 return -1;
499         }
500
501         return ret;
502 }
503
504 /*
505  * Return the uioX char device used for a pci device. On success, return
506  * the UIO number and fill dstbuf string with the path of the device in
507  * sysfs. On error, return a negative value. In this case dstbuf is
508  * invalid.
509  */
510 static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
511                            unsigned int buflen)
512 {
513         struct rte_pci_addr *loc = &dev->addr;
514         unsigned int uio_num;
515         struct dirent *e;
516         DIR *dir;
517         char dirname[PATH_MAX];
518
519         /* depending on kernel version, uio can be located in uio/uioX
520          * or uio:uioX */
521
522         rte_snprintf(dirname, sizeof(dirname),
523                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
524                  loc->domain, loc->bus, loc->devid, loc->function);
525
526         dir = opendir(dirname);
527         if (dir == NULL) {
528                 /* retry with the parent directory */
529                 rte_snprintf(dirname, sizeof(dirname),
530                          SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
531                          loc->domain, loc->bus, loc->devid, loc->function);
532                 dir = opendir(dirname);
533
534                 if (dir == NULL) {
535                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
536                         return -1;
537                 }
538         }
539
540         /* take the first file starting with "uio" */
541         while ((e = readdir(dir)) != NULL) {
542                 /* format could be uio%d ...*/
543                 int shortprefix_len = sizeof("uio") - 1;
544                 /* ... or uio:uio%d */
545                 int longprefix_len = sizeof("uio:uio") - 1; 
546                 char *endptr;
547
548                 if (strncmp(e->d_name, "uio", 3) != 0)
549                         continue;
550
551                 /* first try uio%d */
552                 errno = 0;
553                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
554                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
555                         rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
556                         break;
557                 }
558
559                 /* then try uio:uio%d */
560                 errno = 0;
561                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
562                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
563                         rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
564                         break;
565                 }
566         }
567         closedir(dir);
568
569         /* No uio resource found */
570         if (e == NULL)
571                 return -1;
572
573         /* create uio device if we've been asked to */
574         if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
575                 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
576
577         return uio_num;
578 }
579
580 /* map the PCI resource of a PCI device in virtual memory */
581 static int
582 pci_uio_map_resource(struct rte_pci_device *dev)
583 {
584         int i, j;
585         char dirname[PATH_MAX];
586         char filename[PATH_MAX];
587         char devname[PATH_MAX]; /* contains the /dev/uioX */
588         void *mapaddr;
589         int uio_num;
590         unsigned long start,size;
591         uint64_t phaddr;
592         uint64_t offset;
593         uint64_t pagesz;
594         ssize_t nb_maps;
595         struct rte_pci_addr *loc = &dev->addr;
596         struct uio_resource *uio_res;
597         struct uio_map *maps;
598
599         dev->intr_handle.fd = -1;
600
601         /* secondary processes - use already recorded details */
602         if ((rte_eal_process_type() != RTE_PROC_PRIMARY) &&
603             (dev->id.vendor_id != PCI_VENDOR_ID_QUMRANET))
604                 return (pci_uio_map_secondary(dev));
605
606         /* find uio resource */
607         uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
608         if (uio_num < 0) {
609                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
610                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
611                 return -1;
612         }
613
614         if(dev->id.vendor_id == PCI_VENDOR_ID_QUMRANET) {
615                 /* get portio size */
616                 rte_snprintf(filename, sizeof(filename),
617                          "%s/portio/port0/size", dirname);
618                 if (eal_parse_sysfs_value(filename, &size) < 0) {
619                         RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
620                                 __func__);
621                         return -1;
622                 }
623
624                 /* get portio start */
625                 rte_snprintf(filename, sizeof(filename),
626                          "%s/portio/port0/start", dirname);
627                 if (eal_parse_sysfs_value(filename, &start) < 0) {
628                         RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
629                                 __func__);
630                         return -1;
631                 }
632                 dev->mem_resource[0].addr = (void *)(uintptr_t)start;
633                 dev->mem_resource[0].len =  (uint64_t)size;
634                 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx with size=0x%lx\n", start, size);
635                 /* rte_virtio_pmd does not need any other bar even if available */
636                 return (0);
637         }
638         
639         /* allocate the mapping details for secondary processes*/
640         if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
641                 RTE_LOG(ERR, EAL,
642                         "%s(): cannot store uio mmap details\n", __func__);
643                 return (-1);
644         }
645
646         rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
647         rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
648         memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
649
650         /* collect info about device mappings */
651         if ((nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
652                         sizeof (uio_res->maps) / sizeof (uio_res->maps[0])))
653                         < 0)
654                 return (nb_maps);
655  
656         uio_res->nb_maps = nb_maps;
657
658         /* Map all BARs */
659         pagesz = sysconf(_SC_PAGESIZE);
660  
661         maps = uio_res->maps;
662         for (i = 0; i != PCI_MAX_RESOURCE; i++) {
663     
664                 /* skip empty BAR */
665                 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
666                         continue;
667  
668                 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
669                                 dev->mem_resource[i].len != maps[j].size);
670                                 j++)
671                         ;
672  
673                 /* if matching map is found, then use it */
674                 if (j != nb_maps) {
675                         offset = j * pagesz;
676                         if (maps[j].addr != NULL ||
677                                         (mapaddr = pci_map_resource(dev,
678                                         NULL, devname, (off_t)offset,
679                                         (size_t)maps[j].size)) == NULL) {
680                                 return (-1);
681                         }
682  
683                         maps[j].addr = mapaddr;
684                         maps[j].offset = offset;
685                         dev->mem_resource[i].addr = mapaddr;
686                 }
687         }
688
689         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
690
691         return (0);
692 }
693
694 /* parse the "resource" sysfs file */
695 #define IORESOURCE_MEM  0x00000200
696
697 static int
698 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
699 {
700         FILE *f;
701         char buf[BUFSIZ];
702         union pci_resource_info {
703                 struct {
704                         char *phys_addr;
705                         char *end_addr;
706                         char *flags;
707                 };
708                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
709         } res_info;
710         int i;
711         uint64_t phys_addr, end_addr, flags;
712
713         f = fopen(filename, "r");
714         if (f == NULL) {
715                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
716                 return -1;
717         }
718
719         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
720
721                 if (fgets(buf, sizeof(buf), f) == NULL) {
722                         RTE_LOG(ERR, EAL, 
723                                 "%s(): cannot read resource\n", __func__);
724                         goto error;
725                 }
726
727                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
728                         RTE_LOG(ERR, EAL, 
729                                 "%s(): bad resource format\n", __func__);
730                         goto error;
731                 }
732                 errno = 0;
733                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
734                 end_addr = strtoull(res_info.end_addr, NULL, 16);
735                 flags = strtoull(res_info.flags, NULL, 16);
736                 if (errno != 0) {
737                         RTE_LOG(ERR, EAL, 
738                                 "%s(): bad resource format\n", __func__);
739                         goto error;
740                 }
741
742                 if (flags & IORESOURCE_MEM) {
743                         dev->mem_resource[i].phys_addr = phys_addr;
744                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
745                         /* not mapped for now */
746                         dev->mem_resource[i].addr = NULL; 
747                 }
748         }
749         fclose(f);
750         return 0;
751
752 error:
753         fclose(f);
754         return -1;
755 }
756
757 /* 
758  * parse a sysfs file containing one integer value 
759  * different to the eal version, as it needs to work with 64-bit values
760  */ 
761 static int 
762 pci_parse_sysfs_value(const char *filename, uint64_t *val) 
763 {
764         FILE *f;
765         char buf[BUFSIZ];
766         char *end = NULL;
767  
768         f = fopen(filename, "r");
769         if (f == NULL) {
770                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
771                         __func__, filename);
772                 return -1;
773         }
774  
775         if (fgets(buf, sizeof(buf), f) == NULL) {
776                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
777                         __func__, filename);
778                 fclose(f);
779                 return -1;
780         }
781         *val = strtoull(buf, &end, 0);
782         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
783                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
784                                 __func__, filename);
785                 fclose(f);
786                 return -1;
787         }
788         fclose(f);
789         return 0;
790 }
791
792 /* Compare two PCI device addresses. */
793 static int
794 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
795 {
796         uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
797         uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
798
799         if (dev_addr > dev_addr2)
800                 return 1;
801         else
802                 return 0;
803 }
804
805
806 /* Scan one pci sysfs entry, and fill the devices list from it. */
807 static int
808 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
809              uint8_t devid, uint8_t function)
810 {
811         char filename[PATH_MAX];
812         unsigned long tmp;
813         struct rte_pci_device *dev;
814
815         dev = malloc(sizeof(*dev));
816         if (dev == NULL) {
817                 return -1;
818         }
819
820         memset(dev, 0, sizeof(*dev));
821         dev->addr.domain = domain;
822         dev->addr.bus = bus;
823         dev->addr.devid = devid;
824         dev->addr.function = function;
825
826         /* get vendor id */
827         rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
828         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
829                 free(dev);
830                 return -1;
831         }
832         dev->id.vendor_id = (uint16_t)tmp;
833
834         /* get device id */
835         rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
836         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
837                 free(dev);
838                 return -1;
839         }
840         dev->id.device_id = (uint16_t)tmp;
841
842         /* get subsystem_vendor id */
843         rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
844                  dirname);
845         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
846                 free(dev);
847                 return -1;
848         }
849         dev->id.subsystem_vendor_id = (uint16_t)tmp;
850
851         /* get subsystem_device id */
852         rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
853                  dirname);
854         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
855                 free(dev);
856                 return -1;
857         }
858         dev->id.subsystem_device_id = (uint16_t)tmp;
859
860         /* get max_vfs */
861         dev->max_vfs = 0;
862         rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
863         if (!access(filename, F_OK) && 
864             eal_parse_sysfs_value(filename, &tmp) == 0) {
865                 dev->max_vfs = (uint16_t)tmp;
866         }
867
868         /* get numa node */
869         rte_snprintf(filename, sizeof(filename), "%s/numa_node",
870                  dirname);
871         if (access(filename, R_OK) != 0) {
872                 /* if no NUMA support just set node to 0 */
873                 dev->numa_node = -1;
874         } else {
875                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
876                         free(dev);
877                         return -1;
878                 }
879                 dev->numa_node = tmp;
880         }
881
882         /* parse resources */
883         rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
884         if (pci_parse_sysfs_resource(filename, dev) < 0) {
885                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
886                 free(dev);
887                 return -1;
888         }
889
890         /* device is valid, add in list (sorted) */
891         if (TAILQ_EMPTY(&pci_device_list)) {
892                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
893         }       
894         else {
895                 struct rte_pci_device *dev2 = NULL;
896
897                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
898                         if (pci_addr_comparison(&dev->addr, &dev2->addr))
899                                 continue;
900                         else {
901                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
902                                 return 0;
903                         }
904                 }
905                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
906         }
907                                 
908         return 0;
909 }
910
911 /*
912  * split up a pci address into its constituent parts.
913  */
914 static int
915 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
916                 uint8_t *bus, uint8_t *devid, uint8_t *function)
917 {
918         /* first split on ':' */
919         union splitaddr {
920                 struct {
921                         char *domain;
922                         char *bus;
923                         char *devid;
924                         char *function;
925                 };
926                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
927         } splitaddr;
928
929         char *buf_copy = strndup(buf, bufsize);
930         if (buf_copy == NULL)
931                 return -1;
932
933         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
934                         != PCI_FMT_NVAL - 1)
935                 goto error;
936         /* final split is on '.' between devid and function */
937         splitaddr.function = strchr(splitaddr.devid,'.');
938         if (splitaddr.function == NULL)
939                 goto error;
940         *splitaddr.function++ = '\0';
941
942         /* now convert to int values */
943         errno = 0;
944         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
945         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
946         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
947         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
948         if (errno != 0)
949                 goto error;
950
951         free(buf_copy); /* free the copy made with strdup */
952         return 0;
953 error:
954         free(buf_copy);
955         return -1;
956 }
957
958 /*
959  * Scan the content of the PCI bus, and the devices in the devices
960  * list
961  */
962 static int
963 pci_scan(void)
964 {
965         struct dirent *e;
966         DIR *dir;
967         char dirname[PATH_MAX];
968         uint16_t domain;
969         uint8_t bus, devid, function;
970
971         dir = opendir(SYSFS_PCI_DEVICES);
972         if (dir == NULL) {
973                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
974                         __func__, strerror(errno));
975                 return -1;
976         }
977
978         while ((e = readdir(dir)) != NULL) {
979                 if (e->d_name[0] == '.')
980                         continue;
981
982                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
983                                 &bus, &devid, &function) != 0)
984                         continue;
985
986                 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
987                          e->d_name);
988                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
989                         goto error;
990         }
991         closedir(dir);
992         return 0;
993
994 error:
995         closedir(dir);
996         return -1;
997 }
998
999 /*
1000  * If vendor/device ID match, call the devinit() function of the
1001  * driver.
1002  */
1003 int
1004 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
1005 {
1006         struct rte_pci_id *id_table;
1007
1008         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
1009
1010                 /* check if device's identifiers match the driver's ones */
1011                 if (id_table->vendor_id != dev->id.vendor_id &&
1012                                 id_table->vendor_id != PCI_ANY_ID)
1013                         continue;
1014                 if (id_table->device_id != dev->id.device_id &&
1015                                 id_table->device_id != PCI_ANY_ID)
1016                         continue;
1017                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
1018                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
1019                         continue;
1020                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
1021                                 id_table->subsystem_device_id != PCI_ANY_ID)
1022                         continue;
1023
1024                 struct rte_pci_addr *loc = &dev->addr;
1025
1026                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
1027                                 loc->domain, loc->bus, loc->devid, loc->function,
1028                                 dev->numa_node);
1029
1030                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
1031                                 dev->id.device_id, dr->name);
1032
1033                 /* no initialization when blacklisted, return without error */
1034                 if (dev->blacklisted) {
1035                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
1036                         return 0;
1037                 }
1038
1039 #ifdef RTE_EAL_UNBIND_PORTS
1040                 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
1041                         /* unbind driver and load uio resources for Intel NICs */
1042                         if (pci_switch_module(dr, dev, 1, IGB_UIO_NAME) < 0)
1043                                 return -1;
1044                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
1045                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
1046                         /* unbind current driver */
1047                         if (pci_unbind_kernel_driver(dev) < 0)
1048                                 return -1;
1049                 }
1050 #else
1051                 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
1052                         /* just map resources for Intel NICs */
1053                         if (pci_uio_map_resource(dev) < 0)
1054                                 return -1;
1055 #endif
1056
1057                 /* reference driver structure */
1058                 dev->driver = dr;
1059
1060                 /* call the driver devinit() function */
1061                 return dr->devinit(dr, dev);
1062         }
1063         /* return positive value if driver is not found */
1064         return 1;
1065 }
1066
1067 /* Init the PCI EAL subsystem */
1068 int
1069 rte_eal_pci_init(void)
1070 {
1071         TAILQ_INIT(&pci_driver_list);
1072         TAILQ_INIT(&pci_device_list);
1073         uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
1074
1075         /* for debug purposes, PCI can be disabled */
1076         if (internal_config.no_pci)
1077                 return 0;
1078
1079         if (pci_scan() < 0) {
1080                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
1081                 return -1;
1082         }
1083         return 0;
1084 }