5c7f8140261d57814d1f9d133ba9f7e4aa370d13
[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
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46 #include <errno.h>
47 #include <dirent.h>
48 #include <limits.h>
49 #include <sys/queue.h>
50 #include <sys/mman.h>
51 #include <sys/ioctl.h>
52
53 #include <rte_interrupts.h>
54 #include <rte_log.h>
55 #include <rte_pci.h>
56 #include <rte_common.h>
57 #include <rte_launch.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_tailq.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_debug.h>
68
69 #include "eal_filesystem.h"
70 #include "eal_private.h"
71
72 /**
73  * @file
74  * PCI probing under linux
75  *
76  * This code is used to simulate a PCI probe by parsing information in
77  * sysfs. Moreover, when a registered driver matches a device, the
78  * kernel driver currently using it is unloaded and replaced by
79  * igb_uio module, which is a very minimal userland driver for Intel
80  * network card, only providing access to PCI BAR to applications, and
81  * enabling bus master.
82  */
83
84 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 /* map the PCI resource of a PCI device in virtual memory */
464 static int
465 pci_uio_map_resource(struct rte_pci_device *dev)
466 {
467         int i, j;
468         struct dirent *e;
469         DIR *dir;
470         char dirname[PATH_MAX];
471         char dirname2[PATH_MAX];
472         char devname[PATH_MAX]; /* contains the /dev/uioX */
473         void *mapaddr;
474         unsigned uio_num;
475         uint64_t phaddr;
476         uint64_t offset;
477         uint64_t pagesz;
478         ssize_t nb_maps;
479         struct rte_pci_addr *loc = &dev->addr;
480         struct uio_resource *uio_res;
481         struct uio_map *maps;
482
483         dev->intr_handle.fd = -1;
484
485         /* secondary processes - use already recorded details */
486         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
487                 return (pci_uio_map_secondary(dev));
488
489         /* depending on kernel version, uio can be located in uio/uioX
490          * or uio:uioX */
491
492         rte_snprintf(dirname, sizeof(dirname),
493                  "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
494                  loc->domain, loc->bus, loc->devid, loc->function);
495
496         dir = opendir(dirname);
497         if (dir == NULL) {
498                 /* retry with the parent directory */
499                 rte_snprintf(dirname, sizeof(dirname),
500                          "/sys/bus/pci/devices/" PCI_PRI_FMT,
501                          loc->domain, loc->bus, loc->devid, loc->function);
502                 dir = opendir(dirname);
503
504                 if (dir == NULL) {
505                         RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
506                         return -1;
507                 }
508         }
509
510         /* take the first file starting with "uio" */
511         while ((e = readdir(dir)) != NULL) {
512                 /* format could be uio%d ...*/
513                 int shortprefix_len = sizeof("uio") - 1;
514                 /* ... or uio:uio%d */
515                 int longprefix_len = sizeof("uio:uio") - 1; 
516                 char *endptr;
517
518                 if (strncmp(e->d_name, "uio", 3) != 0)
519                         continue;
520
521                 /* first try uio%d */
522                 errno = 0;
523                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
524                 if (errno == 0 && endptr != e->d_name) {
525                         rte_snprintf(dirname2, sizeof(dirname2),
526                                  "%s/uio%u", dirname, uio_num);
527                         break;
528                 }
529
530                 /* then try uio:uio%d */
531                 errno = 0;
532                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
533                 if (errno == 0 && endptr != e->d_name) {
534                         rte_snprintf(dirname2, sizeof(dirname2),
535                                  "%s/uio:uio%u", dirname, uio_num);
536                         break;
537                 }
538         }
539         closedir(dir);
540
541         /* No uio resource found */
542         if (e == NULL) {
543                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
544                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
545                 return -1;
546         }
547
548         /* allocate the mapping details for secondary processes*/
549         if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
550                 RTE_LOG(ERR, EAL,
551                         "%s(): cannot store uio mmap details\n", __func__);
552                 return (-1);
553         }
554  
555         rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
556         rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
557         memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
558  
559         /* collect info about device mappings */
560         if ((nb_maps = pci_uio_get_mappings(dirname2, uio_res->maps,
561                         sizeof (uio_res->maps) / sizeof (uio_res->maps[0])))
562                         < 0)
563                 return (nb_maps);
564  
565         uio_res->nb_maps = nb_maps;
566
567         /* Map all BARs */
568         pagesz = sysconf(_SC_PAGESIZE);
569  
570         maps = uio_res->maps;
571         for (i = 0; i != PCI_MAX_RESOURCE; i++) {
572     
573                 /* skip empty BAR */
574                 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
575                         continue;
576  
577                 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
578                                 dev->mem_resource[i].len != maps[j].size);
579                                 j++)
580                         ;
581  
582                 /* if matching map is found, then use it */
583                 if (j != nb_maps) {
584                         offset = j * pagesz;
585                         if (maps[j].addr != NULL ||
586                                         (mapaddr = pci_map_resource(dev,
587                                         NULL, devname, (off_t)offset,
588                                         (size_t)maps[j].size)) == NULL) {
589                                 return (-1);
590                         }
591  
592                         maps[j].addr = mapaddr;
593                         maps[j].offset = offset;
594                         dev->mem_resource[i].addr = mapaddr;
595                 }
596         }
597
598         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
599
600         return (0);
601 }
602
603 /* parse the "resource" sysfs file */
604 #define IORESOURCE_MEM  0x00000200
605
606 static int
607 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
608 {
609         FILE *f;
610         char buf[BUFSIZ];
611         union pci_resource_info {
612                 struct {
613                         char *phys_addr;
614                         char *end_addr;
615                         char *flags;
616                 };
617                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
618         } res_info;
619         int i;
620         uint64_t phys_addr, end_addr, flags;
621
622         f = fopen(filename, "r");
623         if (f == NULL) {
624                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
625                 return -1;
626         }
627
628         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
629
630                 if (fgets(buf, sizeof(buf), f) == NULL) {
631                         RTE_LOG(ERR, EAL, 
632                                 "%s(): cannot read resource\n", __func__);
633                         goto error;
634                 }
635
636                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
637                         RTE_LOG(ERR, EAL, 
638                                 "%s(): bad resource format\n", __func__);
639                         goto error;
640                 }
641                 errno = 0;
642                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
643                 end_addr = strtoull(res_info.end_addr, NULL, 16);
644                 flags = strtoull(res_info.flags, NULL, 16);
645                 if (errno != 0) {
646                         RTE_LOG(ERR, EAL, 
647                                 "%s(): bad resource format\n", __func__);
648                         goto error;
649                 }
650
651                 if (flags & IORESOURCE_MEM) {
652                         dev->mem_resource[i].phys_addr = phys_addr;
653                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
654                         /* not mapped for now */
655                         dev->mem_resource[i].addr = NULL; 
656                 }
657         }
658         fclose(f);
659         return 0;
660
661 error:
662         fclose(f);
663         return -1;
664 }
665
666 /* 
667  * parse a sysfs file containing one integer value 
668  * different to the eal version, as it needs to work with 64-bit values
669  */ 
670 static int 
671 pci_parse_sysfs_value(const char *filename, uint64_t *val) 
672 {
673         FILE *f;
674         char buf[BUFSIZ];
675         char *end = NULL;
676  
677         f = fopen(filename, "r");
678         if (f == NULL) {
679                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
680                         __func__, filename);
681                 return -1;
682         }
683  
684         if (fgets(buf, sizeof(buf), f) == NULL) {
685                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
686                         __func__, filename);
687                 fclose(f);
688                 return -1;
689         }
690         *val = strtoull(buf, &end, 0);
691         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
692                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
693                                 __func__, filename);
694                 fclose(f);
695                 return -1;
696         }
697         fclose(f);
698         return 0;
699 }
700
701 /* Compare two PCI device addresses. */
702 static int
703 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
704 {
705         uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
706         uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
707
708         if (dev_addr > dev_addr2)
709                 return 1;
710         else
711                 return 0;
712 }
713
714
715 /* Scan one pci sysfs entry, and fill the devices list from it. */
716 static int
717 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
718              uint8_t devid, uint8_t function)
719 {
720         char filename[PATH_MAX];
721         unsigned long tmp;
722         struct rte_pci_device *dev;
723
724         dev = malloc(sizeof(*dev));
725         if (dev == NULL) {
726                 return -1;
727         }
728
729         memset(dev, 0, sizeof(*dev));
730         dev->addr.domain = domain;
731         dev->addr.bus = bus;
732         dev->addr.devid = devid;
733         dev->addr.function = function;
734
735         /* get vendor id */
736         rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
737         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
738                 free(dev);
739                 return -1;
740         }
741         dev->id.vendor_id = (uint16_t)tmp;
742
743         /* get device id */
744         rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
745         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
746                 free(dev);
747                 return -1;
748         }
749         dev->id.device_id = (uint16_t)tmp;
750
751         /* get subsystem_vendor id */
752         rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
753                  dirname);
754         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
755                 free(dev);
756                 return -1;
757         }
758         dev->id.subsystem_vendor_id = (uint16_t)tmp;
759
760         /* get subsystem_device id */
761         rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
762                  dirname);
763         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
764                 free(dev);
765                 return -1;
766         }
767         dev->id.subsystem_device_id = (uint16_t)tmp;
768
769         /* get max_vfs */
770         dev->max_vfs = 0;
771         rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
772         if (!access(filename, F_OK) && 
773             eal_parse_sysfs_value(filename, &tmp) == 0) {
774                 dev->max_vfs = (uint16_t)tmp;
775         }
776
777         /* get numa node */
778         rte_snprintf(filename, sizeof(filename), "%s/numa_node",
779                  dirname);
780         if (access(filename, R_OK) != 0) {
781                 /* if no NUMA support just set node to 0 */
782                 dev->numa_node = -1;
783         } else {
784                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
785                         free(dev);
786                         return -1;
787                 }
788                 dev->numa_node = tmp;
789         }
790
791         /* parse resources */
792         rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
793         if (pci_parse_sysfs_resource(filename, dev) < 0) {
794                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
795                 free(dev);
796                 return -1;
797         }
798
799         /* device is valid, add in list (sorted) */
800         if (TAILQ_EMPTY(&device_list)) {
801                 TAILQ_INSERT_TAIL(&device_list, dev, next);
802         }       
803         else {
804                 struct rte_pci_device *dev2 = NULL;
805
806                 TAILQ_FOREACH(dev2, &device_list, next) {
807                         if (pci_addr_comparison(&dev->addr, &dev2->addr))
808                                 continue;
809                         else {
810                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
811                                 return 0;
812                         }
813                 }
814                 TAILQ_INSERT_TAIL(&device_list, dev, next);
815         }
816                                 
817         return 0;
818 }
819
820 /*
821  * split up a pci address into its constituent parts.
822  */
823 static int
824 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
825                 uint8_t *bus, uint8_t *devid, uint8_t *function)
826 {
827         /* first split on ':' */
828         union splitaddr {
829                 struct {
830                         char *domain;
831                         char *bus;
832                         char *devid;
833                         char *function;
834                 };
835                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
836         } splitaddr;
837
838         char *buf_copy = strndup(buf, bufsize);
839         if (buf_copy == NULL)
840                 return -1;
841
842         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
843                         != PCI_FMT_NVAL - 1)
844                 goto error;
845         /* final split is on '.' between devid and function */
846         splitaddr.function = strchr(splitaddr.devid,'.');
847         if (splitaddr.function == NULL)
848                 goto error;
849         *splitaddr.function++ = '\0';
850
851         /* now convert to int values */
852         errno = 0;
853         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
854         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
855         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
856         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
857         if (errno != 0)
858                 goto error;
859
860         free(buf_copy); /* free the copy made with strdup */
861         return 0;
862 error:
863         free(buf_copy);
864         return -1;
865 }
866
867 /*
868  * Scan the content of the PCI bus, and the devices in the devices
869  * list
870  */
871 static int
872 pci_scan(void)
873 {
874         struct dirent *e;
875         DIR *dir;
876         char dirname[PATH_MAX];
877         uint16_t domain;
878         uint8_t bus, devid, function;
879
880         dir = opendir(SYSFS_PCI_DEVICES);
881         if (dir == NULL) {
882                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
883                         __func__, strerror(errno));
884                 return -1;
885         }
886
887         while ((e = readdir(dir)) != NULL) {
888                 if (e->d_name[0] == '.')
889                         continue;
890
891                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
892                                 &bus, &devid, &function) != 0)
893                         continue;
894
895                 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
896                          e->d_name);
897                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
898                         goto error;
899         }
900         closedir(dir);
901         return 0;
902
903 error:
904         closedir(dir);
905         return -1;
906 }
907
908 /*
909  * If vendor/device ID match, call the devinit() function of the
910  * driver.
911  */
912 int
913 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
914 {
915         struct rte_pci_id *id_table;
916 #ifdef RTE_EAL_UNBIND_PORTS
917         const char *module_name = NULL;
918         int uio_status = -1;
919
920         if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
921                 module_name = IGB_UIO_NAME;
922 #endif
923
924         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
925
926                 /* check if device's identifiers match the driver's ones */
927                 if (id_table->vendor_id != dev->id.vendor_id &&
928                                 id_table->vendor_id != PCI_ANY_ID)
929                         continue;
930                 if (id_table->device_id != dev->id.device_id &&
931                                 id_table->device_id != PCI_ANY_ID)
932                         continue;
933                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
934                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
935                         continue;
936                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
937                                 id_table->subsystem_device_id != PCI_ANY_ID)
938                         continue;
939
940                 struct rte_pci_addr *loc = &dev->addr;
941
942                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
943                                 loc->domain, loc->bus, loc->devid, loc->function,
944                                 dev->numa_node);
945
946                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
947                                 dev->id.device_id, dr->name);
948
949                 /* no initialization when blacklisted, return without error */
950                 if (dev->blacklisted) {
951                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
952                         return 0;
953                 }
954
955 #ifdef RTE_EAL_UNBIND_PORTS
956                 /* Unbind PCI devices if needed */
957                 if (module_name != NULL)
958                         if (pci_switch_module(dr, dev, uio_status, module_name) < 0)
959                                 return -1;
960 #else
961                 /* just map the NIC resources */
962                 if (pci_uio_map_resource(dev) < 0)
963                         return -1;
964 #endif
965
966                 /* We always should have BAR0 mapped */
967                 if (rte_eal_process_type() == RTE_PROC_PRIMARY && 
968                         dev->mem_resource[0].addr == NULL) {
969                         RTE_LOG(ERR, EAL,
970                                 "%s(): BAR0 is not mapped\n",
971                                 __func__);
972                         return (-1);
973                 }
974  
975                 /* reference driver structure */
976                 dev->driver = dr;
977
978                 /* call the driver devinit() function */
979                 return dr->devinit(dr, dev);
980         }
981         /* return positive value if driver is not found */
982         return 1;
983 }
984
985 /* Init the PCI EAL subsystem */
986 int
987 rte_eal_pci_init(void)
988 {
989         TAILQ_INIT(&driver_list);
990         TAILQ_INIT(&device_list);
991         uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
992
993         /* for debug purposes, PCI can be disabled */
994         if (internal_config.no_pci)
995                 return 0;
996
997         if (pci_scan() < 0) {
998                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
999                 return -1;
1000         }
1001         return 0;
1002 }