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