pci: use bus driver for scan/probe
[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 <dirent.h>
36
37 #include <rte_log.h>
38 #include <rte_bus.h>
39 #include <rte_pci.h>
40 #include <rte_eal_memconfig.h>
41 #include <rte_malloc.h>
42 #include <rte_devargs.h>
43 #include <rte_memcpy.h>
44
45 #include "eal_filesystem.h"
46 #include "eal_private.h"
47 #include "eal_pci_init.h"
48
49 /**
50  * @file
51  * PCI probing under linux
52  *
53  * This code is used to simulate a PCI probe by parsing information in sysfs.
54  * When a registered device matches a driver, it is then initialized with
55  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
56  */
57
58 extern struct rte_pci_bus rte_pci_bus;
59
60 static int
61 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
62 {
63         int count;
64         char path[PATH_MAX];
65         char *name;
66
67         if (!filename || !dri_name)
68                 return -1;
69
70         count = readlink(filename, path, PATH_MAX);
71         if (count >= PATH_MAX)
72                 return -1;
73
74         /* For device does not have a driver */
75         if (count < 0)
76                 return 1;
77
78         path[count] = '\0';
79
80         name = strrchr(path, '/');
81         if (name) {
82                 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
83                 return 0;
84         }
85
86         return -1;
87 }
88
89 /* Map pci device */
90 int
91 rte_eal_pci_map_device(struct rte_pci_device *dev)
92 {
93         int ret = -1;
94
95         /* try mapping the NIC resources using VFIO if it exists */
96         switch (dev->kdrv) {
97         case RTE_KDRV_VFIO:
98 #ifdef VFIO_PRESENT
99                 if (pci_vfio_is_enabled())
100                         ret = pci_vfio_map_resource(dev);
101 #endif
102                 break;
103         case RTE_KDRV_IGB_UIO:
104         case RTE_KDRV_UIO_GENERIC:
105                 if (rte_eal_using_phys_addrs()) {
106                         /* map resources for devices that use uio */
107                         ret = pci_uio_map_resource(dev);
108                 }
109                 break;
110         default:
111                 RTE_LOG(DEBUG, EAL,
112                         "  Not managed by a supported kernel driver, skipped\n");
113                 ret = 1;
114                 break;
115         }
116
117         return ret;
118 }
119
120 /* Unmap pci device */
121 void
122 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
123 {
124         /* try unmapping the NIC resources using VFIO if it exists */
125         switch (dev->kdrv) {
126         case RTE_KDRV_VFIO:
127 #ifdef VFIO_PRESENT
128                 if (pci_vfio_is_enabled())
129                         pci_vfio_unmap_resource(dev);
130 #endif
131                 break;
132         case RTE_KDRV_IGB_UIO:
133         case RTE_KDRV_UIO_GENERIC:
134                 /* unmap resources for devices that use uio */
135                 pci_uio_unmap_resource(dev);
136                 break;
137         default:
138                 RTE_LOG(DEBUG, EAL,
139                         "  Not managed by a supported kernel driver, skipped\n");
140                 break;
141         }
142 }
143
144 void *
145 pci_find_max_end_va(void)
146 {
147         const struct rte_memseg *seg = rte_eal_get_physmem_layout();
148         const struct rte_memseg *last = seg;
149         unsigned i = 0;
150
151         for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
152                 if (seg->addr == NULL)
153                         break;
154
155                 if (seg->addr > last->addr)
156                         last = seg;
157
158         }
159         return RTE_PTR_ADD(last->addr, last->len);
160 }
161
162 /* parse one line of the "resource" sysfs file (note that the 'line'
163  * string is modified)
164  */
165 int
166 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
167         uint64_t *end_addr, uint64_t *flags)
168 {
169         union pci_resource_info {
170                 struct {
171                         char *phys_addr;
172                         char *end_addr;
173                         char *flags;
174                 };
175                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
176         } res_info;
177
178         if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
179                 RTE_LOG(ERR, EAL,
180                         "%s(): bad resource format\n", __func__);
181                 return -1;
182         }
183         errno = 0;
184         *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
185         *end_addr = strtoull(res_info.end_addr, NULL, 16);
186         *flags = strtoull(res_info.flags, NULL, 16);
187         if (errno != 0) {
188                 RTE_LOG(ERR, EAL,
189                         "%s(): bad resource format\n", __func__);
190                 return -1;
191         }
192
193         return 0;
194 }
195
196 /* parse the "resource" sysfs file */
197 static int
198 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
199 {
200         FILE *f;
201         char buf[BUFSIZ];
202         int i;
203         uint64_t phys_addr, end_addr, flags;
204
205         f = fopen(filename, "r");
206         if (f == NULL) {
207                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
208                 return -1;
209         }
210
211         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
212
213                 if (fgets(buf, sizeof(buf), f) == NULL) {
214                         RTE_LOG(ERR, EAL,
215                                 "%s(): cannot read resource\n", __func__);
216                         goto error;
217                 }
218                 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
219                                 &end_addr, &flags) < 0)
220                         goto error;
221
222                 if (flags & IORESOURCE_MEM) {
223                         dev->mem_resource[i].phys_addr = phys_addr;
224                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
225                         /* not mapped for now */
226                         dev->mem_resource[i].addr = NULL;
227                 }
228         }
229         fclose(f);
230         return 0;
231
232 error:
233         fclose(f);
234         return -1;
235 }
236
237 /* Scan one pci sysfs entry, and fill the devices list from it. */
238 static int
239 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
240 {
241         char filename[PATH_MAX];
242         unsigned long tmp;
243         struct rte_pci_device *dev;
244         char driver[PATH_MAX];
245         int ret;
246
247         dev = malloc(sizeof(*dev));
248         if (dev == NULL)
249                 return -1;
250
251         memset(dev, 0, sizeof(*dev));
252         dev->addr = *addr;
253
254         /* get vendor id */
255         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
256         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
257                 free(dev);
258                 return -1;
259         }
260         dev->id.vendor_id = (uint16_t)tmp;
261
262         /* get device id */
263         snprintf(filename, sizeof(filename), "%s/device", dirname);
264         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
265                 free(dev);
266                 return -1;
267         }
268         dev->id.device_id = (uint16_t)tmp;
269
270         /* get subsystem_vendor id */
271         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
272                  dirname);
273         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
274                 free(dev);
275                 return -1;
276         }
277         dev->id.subsystem_vendor_id = (uint16_t)tmp;
278
279         /* get subsystem_device id */
280         snprintf(filename, sizeof(filename), "%s/subsystem_device",
281                  dirname);
282         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
283                 free(dev);
284                 return -1;
285         }
286         dev->id.subsystem_device_id = (uint16_t)tmp;
287
288         /* get class_id */
289         snprintf(filename, sizeof(filename), "%s/class",
290                  dirname);
291         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
292                 free(dev);
293                 return -1;
294         }
295         /* the least 24 bits are valid: class, subclass, program interface */
296         dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
297
298         /* get max_vfs */
299         dev->max_vfs = 0;
300         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
301         if (!access(filename, F_OK) &&
302             eal_parse_sysfs_value(filename, &tmp) == 0)
303                 dev->max_vfs = (uint16_t)tmp;
304         else {
305                 /* for non igb_uio driver, need kernel version >= 3.8 */
306                 snprintf(filename, sizeof(filename),
307                          "%s/sriov_numvfs", dirname);
308                 if (!access(filename, F_OK) &&
309                     eal_parse_sysfs_value(filename, &tmp) == 0)
310                         dev->max_vfs = (uint16_t)tmp;
311         }
312
313         /* get numa node */
314         snprintf(filename, sizeof(filename), "%s/numa_node",
315                  dirname);
316         if (access(filename, R_OK) != 0) {
317                 /* if no NUMA support, set default to 0 */
318                 dev->device.numa_node = 0;
319         } else {
320                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
321                         free(dev);
322                         return -1;
323                 }
324                 dev->device.numa_node = tmp;
325         }
326
327         /* parse resources */
328         snprintf(filename, sizeof(filename), "%s/resource", dirname);
329         if (pci_parse_sysfs_resource(filename, dev) < 0) {
330                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
331                 free(dev);
332                 return -1;
333         }
334
335         /* parse driver */
336         snprintf(filename, sizeof(filename), "%s/driver", dirname);
337         ret = pci_get_kernel_driver_by_path(filename, driver);
338         if (ret < 0) {
339                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
340                 free(dev);
341                 return -1;
342         }
343
344         if (!ret) {
345                 if (!strcmp(driver, "vfio-pci"))
346                         dev->kdrv = RTE_KDRV_VFIO;
347                 else if (!strcmp(driver, "igb_uio"))
348                         dev->kdrv = RTE_KDRV_IGB_UIO;
349                 else if (!strcmp(driver, "uio_pci_generic"))
350                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
351                 else
352                         dev->kdrv = RTE_KDRV_UNKNOWN;
353         } else
354                 dev->kdrv = RTE_KDRV_NONE;
355
356         /* device is valid, add in list (sorted) */
357         if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
358                 rte_eal_pci_add_device(dev);
359         } else {
360                 struct rte_pci_device *dev2;
361                 int ret;
362
363                 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
364                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
365                         if (ret > 0)
366                                 continue;
367
368                         if (ret < 0) {
369                                 rte_eal_pci_insert_device(dev2, dev);
370                         } else { /* already registered */
371                                 dev2->kdrv = dev->kdrv;
372                                 dev2->max_vfs = dev->max_vfs;
373                                 memmove(dev2->mem_resource, dev->mem_resource,
374                                         sizeof(dev->mem_resource));
375                                 free(dev);
376                         }
377                         return 0;
378                 }
379
380                 rte_eal_pci_add_device(dev);
381         }
382
383         return 0;
384 }
385
386 int
387 pci_update_device(const struct rte_pci_addr *addr)
388 {
389         char filename[PATH_MAX];
390
391         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
392                  pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
393                  addr->function);
394
395         return pci_scan_one(filename, addr);
396 }
397
398 /*
399  * split up a pci address into its constituent parts.
400  */
401 static int
402 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
403 {
404         /* first split on ':' */
405         union splitaddr {
406                 struct {
407                         char *domain;
408                         char *bus;
409                         char *devid;
410                         char *function;
411                 };
412                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
413         } splitaddr;
414
415         char *buf_copy = strndup(buf, bufsize);
416         if (buf_copy == NULL)
417                 return -1;
418
419         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
420                         != PCI_FMT_NVAL - 1)
421                 goto error;
422         /* final split is on '.' between devid and function */
423         splitaddr.function = strchr(splitaddr.devid,'.');
424         if (splitaddr.function == NULL)
425                 goto error;
426         *splitaddr.function++ = '\0';
427
428         /* now convert to int values */
429         errno = 0;
430         addr->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
431         addr->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
432         addr->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
433         addr->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
434         if (errno != 0)
435                 goto error;
436
437         free(buf_copy); /* free the copy made with strdup */
438         return 0;
439 error:
440         free(buf_copy);
441         return -1;
442 }
443
444 /*
445  * Scan the content of the PCI bus, and the devices in the devices
446  * list
447  */
448 int
449 rte_eal_pci_scan(void)
450 {
451         struct dirent *e;
452         DIR *dir;
453         char dirname[PATH_MAX];
454         struct rte_pci_addr addr;
455
456         /* for debug purposes, PCI can be disabled */
457         if (internal_config.no_pci)
458                 return 0;
459
460         dir = opendir(pci_get_sysfs_path());
461         if (dir == NULL) {
462                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
463                         __func__, strerror(errno));
464                 return -1;
465         }
466
467         while ((e = readdir(dir)) != NULL) {
468                 if (e->d_name[0] == '.')
469                         continue;
470
471                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
472                         continue;
473
474                 snprintf(dirname, sizeof(dirname), "%s/%s",
475                                 pci_get_sysfs_path(), e->d_name);
476
477                 if (pci_scan_one(dirname, &addr) < 0)
478                         goto error;
479         }
480         closedir(dir);
481         return 0;
482
483 error:
484         closedir(dir);
485         return -1;
486 }
487
488 /* Read PCI config space. */
489 int rte_eal_pci_read_config(const struct rte_pci_device *device,
490                             void *buf, size_t len, off_t offset)
491 {
492         const struct rte_intr_handle *intr_handle = &device->intr_handle;
493
494         switch (intr_handle->type) {
495         case RTE_INTR_HANDLE_UIO:
496         case RTE_INTR_HANDLE_UIO_INTX:
497                 return pci_uio_read_config(intr_handle, buf, len, offset);
498
499 #ifdef VFIO_PRESENT
500         case RTE_INTR_HANDLE_VFIO_MSIX:
501         case RTE_INTR_HANDLE_VFIO_MSI:
502         case RTE_INTR_HANDLE_VFIO_LEGACY:
503                 return pci_vfio_read_config(intr_handle, buf, len, offset);
504 #endif
505         default:
506                 RTE_LOG(ERR, EAL,
507                         "Unknown handle type of fd %d\n",
508                                         intr_handle->fd);
509                 return -1;
510         }
511 }
512
513 /* Write PCI config space. */
514 int rte_eal_pci_write_config(const struct rte_pci_device *device,
515                              const void *buf, size_t len, off_t offset)
516 {
517         const struct rte_intr_handle *intr_handle = &device->intr_handle;
518
519         switch (intr_handle->type) {
520         case RTE_INTR_HANDLE_UIO:
521         case RTE_INTR_HANDLE_UIO_INTX:
522                 return pci_uio_write_config(intr_handle, buf, len, offset);
523
524 #ifdef VFIO_PRESENT
525         case RTE_INTR_HANDLE_VFIO_MSIX:
526         case RTE_INTR_HANDLE_VFIO_MSI:
527         case RTE_INTR_HANDLE_VFIO_LEGACY:
528                 return pci_vfio_write_config(intr_handle, buf, len, offset);
529 #endif
530         default:
531                 RTE_LOG(ERR, EAL,
532                         "Unknown handle type of fd %d\n",
533                                         intr_handle->fd);
534                 return -1;
535         }
536 }
537
538 #if defined(RTE_ARCH_X86)
539 static int
540 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
541                struct rte_pci_ioport *p)
542 {
543         uint16_t start, end;
544         FILE *fp;
545         char *line = NULL;
546         char pci_id[16];
547         int found = 0;
548         size_t linesz;
549
550         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
551                  dev->addr.domain, dev->addr.bus,
552                  dev->addr.devid, dev->addr.function);
553
554         fp = fopen("/proc/ioports", "r");
555         if (fp == NULL) {
556                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
557                 return -1;
558         }
559
560         while (getdelim(&line, &linesz, '\n', fp) > 0) {
561                 char *ptr = line;
562                 char *left;
563                 int n;
564
565                 n = strcspn(ptr, ":");
566                 ptr[n] = 0;
567                 left = &ptr[n + 1];
568
569                 while (*left && isspace(*left))
570                         left++;
571
572                 if (!strncmp(left, pci_id, strlen(pci_id))) {
573                         found = 1;
574
575                         while (*ptr && isspace(*ptr))
576                                 ptr++;
577
578                         sscanf(ptr, "%04hx-%04hx", &start, &end);
579
580                         break;
581                 }
582         }
583
584         free(line);
585         fclose(fp);
586
587         if (!found)
588                 return -1;
589
590         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
591         p->base = start;
592         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
593
594         return 0;
595 }
596 #endif
597
598 int
599 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
600                        struct rte_pci_ioport *p)
601 {
602         int ret = -1;
603
604         switch (dev->kdrv) {
605 #ifdef VFIO_PRESENT
606         case RTE_KDRV_VFIO:
607                 if (pci_vfio_is_enabled())
608                         ret = pci_vfio_ioport_map(dev, bar, p);
609                 break;
610 #endif
611         case RTE_KDRV_IGB_UIO:
612                 ret = pci_uio_ioport_map(dev, bar, p);
613                 break;
614         case RTE_KDRV_UIO_GENERIC:
615 #if defined(RTE_ARCH_X86)
616                 ret = pci_ioport_map(dev, bar, p);
617 #else
618                 ret = pci_uio_ioport_map(dev, bar, p);
619 #endif
620                 break;
621         case RTE_KDRV_NONE:
622 #if defined(RTE_ARCH_X86)
623                 ret = pci_ioport_map(dev, bar, p);
624 #endif
625                 break;
626         default:
627                 break;
628         }
629
630         if (!ret)
631                 p->dev = dev;
632
633         return ret;
634 }
635
636 void
637 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
638                         void *data, size_t len, off_t offset)
639 {
640         switch (p->dev->kdrv) {
641 #ifdef VFIO_PRESENT
642         case RTE_KDRV_VFIO:
643                 pci_vfio_ioport_read(p, data, len, offset);
644                 break;
645 #endif
646         case RTE_KDRV_IGB_UIO:
647                 pci_uio_ioport_read(p, data, len, offset);
648                 break;
649         case RTE_KDRV_UIO_GENERIC:
650                 pci_uio_ioport_read(p, data, len, offset);
651                 break;
652         case RTE_KDRV_NONE:
653 #if defined(RTE_ARCH_X86)
654                 pci_uio_ioport_read(p, data, len, offset);
655 #endif
656                 break;
657         default:
658                 break;
659         }
660 }
661
662 void
663 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
664                          const void *data, size_t len, off_t offset)
665 {
666         switch (p->dev->kdrv) {
667 #ifdef VFIO_PRESENT
668         case RTE_KDRV_VFIO:
669                 pci_vfio_ioport_write(p, data, len, offset);
670                 break;
671 #endif
672         case RTE_KDRV_IGB_UIO:
673                 pci_uio_ioport_write(p, data, len, offset);
674                 break;
675         case RTE_KDRV_UIO_GENERIC:
676                 pci_uio_ioport_write(p, data, len, offset);
677                 break;
678         case RTE_KDRV_NONE:
679 #if defined(RTE_ARCH_X86)
680                 pci_uio_ioport_write(p, data, len, offset);
681 #endif
682                 break;
683         default:
684                 break;
685         }
686 }
687
688 int
689 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
690 {
691         int ret = -1;
692
693         switch (p->dev->kdrv) {
694 #ifdef VFIO_PRESENT
695         case RTE_KDRV_VFIO:
696                 if (pci_vfio_is_enabled())
697                         ret = pci_vfio_ioport_unmap(p);
698                 break;
699 #endif
700         case RTE_KDRV_IGB_UIO:
701                 ret = pci_uio_ioport_unmap(p);
702                 break;
703         case RTE_KDRV_UIO_GENERIC:
704 #if defined(RTE_ARCH_X86)
705                 ret = 0;
706 #else
707                 ret = pci_uio_ioport_unmap(p);
708 #endif
709                 break;
710         case RTE_KDRV_NONE:
711 #if defined(RTE_ARCH_X86)
712                 ret = 0;
713 #endif
714                 break;
715         default:
716                 break;
717         }
718
719         return ret;
720 }