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