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