595622b2129f70480ed7a31ffd4ee877452f0fd5
[deb_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_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_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         rte_pci_device_name(addr, dev->name, sizeof(dev->name));
328         dev->device.name = dev->name;
329
330         /* parse resources */
331         snprintf(filename, sizeof(filename), "%s/resource", dirname);
332         if (pci_parse_sysfs_resource(filename, dev) < 0) {
333                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
334                 free(dev);
335                 return -1;
336         }
337
338         /* parse driver */
339         snprintf(filename, sizeof(filename), "%s/driver", dirname);
340         ret = pci_get_kernel_driver_by_path(filename, driver);
341         if (ret < 0) {
342                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
343                 free(dev);
344                 return -1;
345         }
346
347         if (!ret) {
348                 if (!strcmp(driver, "vfio-pci"))
349                         dev->kdrv = RTE_KDRV_VFIO;
350                 else if (!strcmp(driver, "igb_uio"))
351                         dev->kdrv = RTE_KDRV_IGB_UIO;
352                 else if (!strcmp(driver, "uio_pci_generic"))
353                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
354                 else
355                         dev->kdrv = RTE_KDRV_UNKNOWN;
356         } else
357                 dev->kdrv = RTE_KDRV_NONE;
358
359         /* device is valid, add in list (sorted) */
360         if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
361                 rte_pci_add_device(dev);
362         } else {
363                 struct rte_pci_device *dev2;
364                 int ret;
365
366                 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
367                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
368                         if (ret > 0)
369                                 continue;
370
371                         if (ret < 0) {
372                                 rte_pci_insert_device(dev2, dev);
373                         } else { /* already registered */
374                                 dev2->kdrv = dev->kdrv;
375                                 dev2->max_vfs = dev->max_vfs;
376                                 memmove(dev2->mem_resource, dev->mem_resource,
377                                         sizeof(dev->mem_resource));
378                                 free(dev);
379                         }
380                         return 0;
381                 }
382
383                 rte_pci_add_device(dev);
384         }
385
386         return 0;
387 }
388
389 int
390 pci_update_device(const struct rte_pci_addr *addr)
391 {
392         char filename[PATH_MAX];
393
394         snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
395                  pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
396                  addr->function);
397
398         return pci_scan_one(filename, addr);
399 }
400
401 /*
402  * split up a pci address into its constituent parts.
403  */
404 static int
405 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
406 {
407         /* first split on ':' */
408         union splitaddr {
409                 struct {
410                         char *domain;
411                         char *bus;
412                         char *devid;
413                         char *function;
414                 };
415                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
416         } splitaddr;
417
418         char *buf_copy = strndup(buf, bufsize);
419         if (buf_copy == NULL)
420                 return -1;
421
422         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
423                         != PCI_FMT_NVAL - 1)
424                 goto error;
425         /* final split is on '.' between devid and function */
426         splitaddr.function = strchr(splitaddr.devid,'.');
427         if (splitaddr.function == NULL)
428                 goto error;
429         *splitaddr.function++ = '\0';
430
431         /* now convert to int values */
432         errno = 0;
433         addr->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
434         addr->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
435         addr->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
436         addr->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
437         if (errno != 0)
438                 goto error;
439
440         free(buf_copy); /* free the copy made with strdup */
441         return 0;
442 error:
443         free(buf_copy);
444         return -1;
445 }
446
447 /*
448  * Scan the content of the PCI bus, and the devices in the devices
449  * list
450  */
451 int
452 rte_pci_scan(void)
453 {
454         struct dirent *e;
455         DIR *dir;
456         char dirname[PATH_MAX];
457         struct rte_pci_addr addr;
458
459         /* for debug purposes, PCI can be disabled */
460         if (internal_config.no_pci)
461                 return 0;
462
463         dir = opendir(pci_get_sysfs_path());
464         if (dir == NULL) {
465                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
466                         __func__, strerror(errno));
467                 return -1;
468         }
469
470         while ((e = readdir(dir)) != NULL) {
471                 if (e->d_name[0] == '.')
472                         continue;
473
474                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
475                         continue;
476
477                 snprintf(dirname, sizeof(dirname), "%s/%s",
478                                 pci_get_sysfs_path(), e->d_name);
479
480                 if (pci_scan_one(dirname, &addr) < 0)
481                         goto error;
482         }
483         closedir(dir);
484         return 0;
485
486 error:
487         closedir(dir);
488         return -1;
489 }
490
491 /* Read PCI config space. */
492 int rte_pci_read_config(const struct rte_pci_device *device,
493                 void *buf, size_t len, off_t offset)
494 {
495         const struct rte_intr_handle *intr_handle = &device->intr_handle;
496
497         switch (intr_handle->type) {
498         case RTE_INTR_HANDLE_UIO:
499         case RTE_INTR_HANDLE_UIO_INTX:
500                 return pci_uio_read_config(intr_handle, buf, len, offset);
501
502 #ifdef VFIO_PRESENT
503         case RTE_INTR_HANDLE_VFIO_MSIX:
504         case RTE_INTR_HANDLE_VFIO_MSI:
505         case RTE_INTR_HANDLE_VFIO_LEGACY:
506                 return pci_vfio_read_config(intr_handle, buf, len, offset);
507 #endif
508         default:
509                 RTE_LOG(ERR, EAL,
510                         "Unknown handle type of fd %d\n",
511                                         intr_handle->fd);
512                 return -1;
513         }
514 }
515
516 /* Write PCI config space. */
517 int rte_pci_write_config(const struct rte_pci_device *device,
518                 const void *buf, size_t len, off_t offset)
519 {
520         const struct rte_intr_handle *intr_handle = &device->intr_handle;
521
522         switch (intr_handle->type) {
523         case RTE_INTR_HANDLE_UIO:
524         case RTE_INTR_HANDLE_UIO_INTX:
525                 return pci_uio_write_config(intr_handle, buf, len, offset);
526
527 #ifdef VFIO_PRESENT
528         case RTE_INTR_HANDLE_VFIO_MSIX:
529         case RTE_INTR_HANDLE_VFIO_MSI:
530         case RTE_INTR_HANDLE_VFIO_LEGACY:
531                 return pci_vfio_write_config(intr_handle, buf, len, offset);
532 #endif
533         default:
534                 RTE_LOG(ERR, EAL,
535                         "Unknown handle type of fd %d\n",
536                                         intr_handle->fd);
537                 return -1;
538         }
539 }
540
541 #if defined(RTE_ARCH_X86)
542 static int
543 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
544                 struct rte_pci_ioport *p)
545 {
546         uint16_t start, end;
547         FILE *fp;
548         char *line = NULL;
549         char pci_id[16];
550         int found = 0;
551         size_t linesz;
552
553         snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
554                  dev->addr.domain, dev->addr.bus,
555                  dev->addr.devid, dev->addr.function);
556
557         fp = fopen("/proc/ioports", "r");
558         if (fp == NULL) {
559                 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
560                 return -1;
561         }
562
563         while (getdelim(&line, &linesz, '\n', fp) > 0) {
564                 char *ptr = line;
565                 char *left;
566                 int n;
567
568                 n = strcspn(ptr, ":");
569                 ptr[n] = 0;
570                 left = &ptr[n + 1];
571
572                 while (*left && isspace(*left))
573                         left++;
574
575                 if (!strncmp(left, pci_id, strlen(pci_id))) {
576                         found = 1;
577
578                         while (*ptr && isspace(*ptr))
579                                 ptr++;
580
581                         sscanf(ptr, "%04hx-%04hx", &start, &end);
582
583                         break;
584                 }
585         }
586
587         free(line);
588         fclose(fp);
589
590         if (!found)
591                 return -1;
592
593         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
594         p->base = start;
595         RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
596
597         return 0;
598 }
599 #endif
600
601 int
602 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
603                 struct rte_pci_ioport *p)
604 {
605         int ret = -1;
606
607         switch (dev->kdrv) {
608 #ifdef VFIO_PRESENT
609         case RTE_KDRV_VFIO:
610                 if (pci_vfio_is_enabled())
611                         ret = pci_vfio_ioport_map(dev, bar, p);
612                 break;
613 #endif
614         case RTE_KDRV_IGB_UIO:
615                 ret = pci_uio_ioport_map(dev, bar, p);
616                 break;
617         case RTE_KDRV_UIO_GENERIC:
618 #if defined(RTE_ARCH_X86)
619                 ret = pci_ioport_map(dev, bar, p);
620 #else
621                 ret = pci_uio_ioport_map(dev, bar, p);
622 #endif
623                 break;
624         case RTE_KDRV_NONE:
625 #if defined(RTE_ARCH_X86)
626                 ret = pci_ioport_map(dev, bar, p);
627 #endif
628                 break;
629         default:
630                 break;
631         }
632
633         if (!ret)
634                 p->dev = dev;
635
636         return ret;
637 }
638
639 void
640 rte_pci_ioport_read(struct rte_pci_ioport *p,
641                 void *data, size_t len, off_t offset)
642 {
643         switch (p->dev->kdrv) {
644 #ifdef VFIO_PRESENT
645         case RTE_KDRV_VFIO:
646                 pci_vfio_ioport_read(p, data, len, offset);
647                 break;
648 #endif
649         case RTE_KDRV_IGB_UIO:
650                 pci_uio_ioport_read(p, data, len, offset);
651                 break;
652         case RTE_KDRV_UIO_GENERIC:
653                 pci_uio_ioport_read(p, data, len, offset);
654                 break;
655         case RTE_KDRV_NONE:
656 #if defined(RTE_ARCH_X86)
657                 pci_uio_ioport_read(p, data, len, offset);
658 #endif
659                 break;
660         default:
661                 break;
662         }
663 }
664
665 void
666 rte_pci_ioport_write(struct rte_pci_ioport *p,
667                 const void *data, size_t len, off_t offset)
668 {
669         switch (p->dev->kdrv) {
670 #ifdef VFIO_PRESENT
671         case RTE_KDRV_VFIO:
672                 pci_vfio_ioport_write(p, data, len, offset);
673                 break;
674 #endif
675         case RTE_KDRV_IGB_UIO:
676                 pci_uio_ioport_write(p, data, len, offset);
677                 break;
678         case RTE_KDRV_UIO_GENERIC:
679                 pci_uio_ioport_write(p, data, len, offset);
680                 break;
681         case RTE_KDRV_NONE:
682 #if defined(RTE_ARCH_X86)
683                 pci_uio_ioport_write(p, data, len, offset);
684 #endif
685                 break;
686         default:
687                 break;
688         }
689 }
690
691 int
692 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
693 {
694         int ret = -1;
695
696         switch (p->dev->kdrv) {
697 #ifdef VFIO_PRESENT
698         case RTE_KDRV_VFIO:
699                 if (pci_vfio_is_enabled())
700                         ret = pci_vfio_ioport_unmap(p);
701                 break;
702 #endif
703         case RTE_KDRV_IGB_UIO:
704                 ret = pci_uio_ioport_unmap(p);
705                 break;
706         case RTE_KDRV_UIO_GENERIC:
707 #if defined(RTE_ARCH_X86)
708                 ret = 0;
709 #else
710                 ret = pci_uio_ioport_unmap(p);
711 #endif
712                 break;
713         case RTE_KDRV_NONE:
714 #if defined(RTE_ARCH_X86)
715                 ret = 0;
716 #endif
717                 break;
718         default:
719                 break;
720         }
721
722         return ret;
723 }