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