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