9a28ede9c9779fb95848368dcfc05a9b76499d81
[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_pci.h>
39 #include <rte_eal_memconfig.h>
40 #include <rte_malloc.h>
41 #include <rte_devargs.h>
42 #include <rte_memcpy.h>
43
44 #include "rte_pci_dev_ids.h"
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 /* unbind kernel driver for this device */
59 int
60 pci_unbind_kernel_driver(struct rte_pci_device *dev)
61 {
62         int n;
63         FILE *f;
64         char filename[PATH_MAX];
65         char buf[BUFSIZ];
66         struct rte_pci_addr *loc = &dev->addr;
67
68         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
69         snprintf(filename, sizeof(filename),
70                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
71                  loc->domain, loc->bus, loc->devid, loc->function);
72
73         f = fopen(filename, "w");
74         if (f == NULL) /* device was not bound */
75                 return 0;
76
77         n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
78                      loc->domain, loc->bus, loc->devid, loc->function);
79         if ((n < 0) || (n >= (int)sizeof(buf))) {
80                 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
81                 goto error;
82         }
83         if (fwrite(buf, n, 1, f) == 0) {
84                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
85                                 filename);
86                 goto error;
87         }
88
89         fclose(f);
90         return 0;
91
92 error:
93         fclose(f);
94         return -1;
95 }
96
97 static int
98 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
99 {
100         int count;
101         char path[PATH_MAX];
102         char *name;
103
104         if (!filename || !dri_name)
105                 return -1;
106
107         count = readlink(filename, path, PATH_MAX);
108         if (count >= PATH_MAX)
109                 return -1;
110
111         /* For device does not have a driver */
112         if (count < 0)
113                 return 1;
114
115         path[count] = '\0';
116
117         name = strrchr(path, '/');
118         if (name) {
119                 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
120                 return 0;
121         }
122
123         return -1;
124 }
125
126 /* Map pci device */
127 int
128 pci_map_device(struct rte_pci_device *dev)
129 {
130         int ret = -1;
131
132         /* try mapping the NIC resources using VFIO if it exists */
133         switch (dev->kdrv) {
134         case RTE_KDRV_VFIO:
135 #ifdef VFIO_PRESENT
136                 if (pci_vfio_is_enabled())
137                         ret = pci_vfio_map_resource(dev);
138 #endif
139                 break;
140         case RTE_KDRV_IGB_UIO:
141         case RTE_KDRV_UIO_GENERIC:
142                 /* map resources for devices that use uio */
143                 ret = pci_uio_map_resource(dev);
144                 break;
145         default:
146                 RTE_LOG(DEBUG, EAL,
147                         "  Not managed by a supported kernel driver, skipped\n");
148                 ret = 1;
149                 break;
150         }
151
152         return ret;
153 }
154
155 /* Unmap pci device */
156 void
157 pci_unmap_device(struct rte_pci_device *dev)
158 {
159         /* try unmapping the NIC resources using VFIO if it exists */
160         switch (dev->kdrv) {
161         case RTE_KDRV_VFIO:
162                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
163                 break;
164         case RTE_KDRV_IGB_UIO:
165         case RTE_KDRV_UIO_GENERIC:
166                 /* unmap resources for devices that use uio */
167                 pci_uio_unmap_resource(dev);
168                 break;
169         default:
170                 RTE_LOG(DEBUG, EAL,
171                         "  Not managed by a supported kernel driver, skipped\n");
172                 break;
173         }
174 }
175
176 void *
177 pci_find_max_end_va(void)
178 {
179         const struct rte_memseg *seg = rte_eal_get_physmem_layout();
180         const struct rte_memseg *last = seg;
181         unsigned i = 0;
182
183         for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
184                 if (seg->addr == NULL)
185                         break;
186
187                 if (seg->addr > last->addr)
188                         last = seg;
189
190         }
191         return RTE_PTR_ADD(last->addr, last->len);
192 }
193
194 /* parse the "resource" sysfs file */
195 static int
196 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
197 {
198         FILE *f;
199         char buf[BUFSIZ];
200         union pci_resource_info {
201                 struct {
202                         char *phys_addr;
203                         char *end_addr;
204                         char *flags;
205                 };
206                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
207         } res_info;
208         int i;
209         uint64_t phys_addr, end_addr, flags;
210
211         f = fopen(filename, "r");
212         if (f == NULL) {
213                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
214                 return -1;
215         }
216
217         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
218
219                 if (fgets(buf, sizeof(buf), f) == NULL) {
220                         RTE_LOG(ERR, EAL,
221                                 "%s(): cannot read resource\n", __func__);
222                         goto error;
223                 }
224
225                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
226                         RTE_LOG(ERR, EAL,
227                                 "%s(): bad resource format\n", __func__);
228                         goto error;
229                 }
230                 errno = 0;
231                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
232                 end_addr = strtoull(res_info.end_addr, NULL, 16);
233                 flags = strtoull(res_info.flags, NULL, 16);
234                 if (errno != 0) {
235                         RTE_LOG(ERR, EAL,
236                                 "%s(): bad resource format\n", __func__);
237                         goto error;
238                 }
239
240                 if (flags & IORESOURCE_MEM) {
241                         dev->mem_resource[i].phys_addr = phys_addr;
242                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
243                         /* not mapped for now */
244                         dev->mem_resource[i].addr = NULL;
245                 }
246         }
247         fclose(f);
248         return 0;
249
250 error:
251         fclose(f);
252         return -1;
253 }
254
255 /* Scan one pci sysfs entry, and fill the devices list from it. */
256 static int
257 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
258              uint8_t devid, uint8_t function)
259 {
260         char filename[PATH_MAX];
261         unsigned long tmp;
262         struct rte_pci_device *dev;
263         char driver[PATH_MAX];
264         int ret;
265
266         dev = malloc(sizeof(*dev));
267         if (dev == NULL)
268                 return -1;
269
270         memset(dev, 0, sizeof(*dev));
271         dev->addr.domain = domain;
272         dev->addr.bus = bus;
273         dev->addr.devid = devid;
274         dev->addr.function = function;
275
276         /* get vendor id */
277         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
278         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
279                 free(dev);
280                 return -1;
281         }
282         dev->id.vendor_id = (uint16_t)tmp;
283
284         /* get device id */
285         snprintf(filename, sizeof(filename), "%s/device", dirname);
286         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
287                 free(dev);
288                 return -1;
289         }
290         dev->id.device_id = (uint16_t)tmp;
291
292         /* get subsystem_vendor id */
293         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
294                  dirname);
295         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
296                 free(dev);
297                 return -1;
298         }
299         dev->id.subsystem_vendor_id = (uint16_t)tmp;
300
301         /* get subsystem_device id */
302         snprintf(filename, sizeof(filename), "%s/subsystem_device",
303                  dirname);
304         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
305                 free(dev);
306                 return -1;
307         }
308         dev->id.subsystem_device_id = (uint16_t)tmp;
309
310         /* get max_vfs */
311         dev->max_vfs = 0;
312         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
313         if (!access(filename, F_OK) &&
314             eal_parse_sysfs_value(filename, &tmp) == 0)
315                 dev->max_vfs = (uint16_t)tmp;
316         else {
317                 /* for non igb_uio driver, need kernel version >= 3.8 */
318                 snprintf(filename, sizeof(filename),
319                          "%s/sriov_numvfs", dirname);
320                 if (!access(filename, F_OK) &&
321                     eal_parse_sysfs_value(filename, &tmp) == 0)
322                         dev->max_vfs = (uint16_t)tmp;
323         }
324
325         /* get numa node */
326         snprintf(filename, sizeof(filename), "%s/numa_node",
327                  dirname);
328         if (access(filename, R_OK) != 0) {
329                 /* if no NUMA support just set node to -1 */
330                 dev->numa_node = -1;
331         } else {
332                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
333                         free(dev);
334                         return -1;
335                 }
336                 dev->numa_node = tmp;
337         }
338
339         /* parse resources */
340         snprintf(filename, sizeof(filename), "%s/resource", dirname);
341         if (pci_parse_sysfs_resource(filename, dev) < 0) {
342                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
343                 free(dev);
344                 return -1;
345         }
346
347         /* parse driver */
348         snprintf(filename, sizeof(filename), "%s/driver", dirname);
349         ret = pci_get_kernel_driver_by_path(filename, driver);
350         if (!ret) {
351                 if (!strcmp(driver, "vfio-pci"))
352                         dev->kdrv = RTE_KDRV_VFIO;
353                 else if (!strcmp(driver, "igb_uio"))
354                         dev->kdrv = RTE_KDRV_IGB_UIO;
355                 else if (!strcmp(driver, "uio_pci_generic"))
356                         dev->kdrv = RTE_KDRV_UIO_GENERIC;
357                 else
358                         dev->kdrv = RTE_KDRV_UNKNOWN;
359         } else if (ret < 0) {
360                 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
361                 free(dev);
362                 return -1;
363         } else
364                 dev->kdrv = RTE_KDRV_UNKNOWN;
365
366         /* device is valid, add in list (sorted) */
367         if (TAILQ_EMPTY(&pci_device_list)) {
368                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
369         }
370         else {
371                 struct rte_pci_device *dev2 = NULL;
372                 int ret;
373
374                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
375                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
376                         if (ret > 0)
377                                 continue;
378                         else if (ret < 0) {
379                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
380                                 return 0;
381                         } else { /* already registered */
382                                 dev2->kdrv = dev->kdrv;
383                                 dev2->max_vfs = dev->max_vfs;
384                                 memmove(dev2->mem_resource,
385                                         dev->mem_resource,
386                                         sizeof(dev->mem_resource));
387                                 free(dev);
388                                 return 0;
389                         }
390                 }
391                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
392         }
393
394         return 0;
395 }
396
397 /*
398  * split up a pci address into its constituent parts.
399  */
400 static int
401 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
402                 uint8_t *bus, uint8_t *devid, uint8_t *function)
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         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
431         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
432         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
433         *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         uint16_t domain;
455         uint8_t bus, devid, function;
456
457         dir = opendir(SYSFS_PCI_DEVICES);
458         if (dir == NULL) {
459                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
460                         __func__, strerror(errno));
461                 return -1;
462         }
463
464         while ((e = readdir(dir)) != NULL) {
465                 if (e->d_name[0] == '.')
466                         continue;
467
468                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
469                                 &bus, &devid, &function) != 0)
470                         continue;
471
472                 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
473                          e->d_name);
474                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
475                         goto error;
476         }
477         closedir(dir);
478         return 0;
479
480 error:
481         closedir(dir);
482         return -1;
483 }
484
485 #ifdef RTE_PCI_CONFIG
486 static int
487 pci_config_extended_tag(struct rte_pci_device *dev)
488 {
489         struct rte_pci_addr *loc = &dev->addr;
490         char filename[PATH_MAX];
491         char buf[BUFSIZ];
492         FILE *f;
493
494         /* not configured, let it as is */
495         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
496                 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
497                 return 0;
498
499         snprintf(filename, sizeof(filename),
500                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
501                 loc->domain, loc->bus, loc->devid, loc->function);
502         f = fopen(filename, "rw+");
503         if (!f)
504                 return -1;
505
506         fgets(buf, sizeof(buf), f);
507         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
508                 /* enable Extended Tag*/
509                 if (strncmp(buf, "on", 2) != 0) {
510                         fseek(f, 0, SEEK_SET);
511                         fputs("on", f);
512                 }
513         } else {
514                 /* disable Extended Tag */
515                 if (strncmp(buf, "off", 3) != 0) {
516                         fseek(f, 0, SEEK_SET);
517                         fputs("off", f);
518                 }
519         }
520         fclose(f);
521
522         return 0;
523 }
524
525 static int
526 pci_config_max_read_request_size(struct rte_pci_device *dev)
527 {
528         struct rte_pci_addr *loc = &dev->addr;
529         char filename[PATH_MAX];
530         char buf[BUFSIZ], param[BUFSIZ];
531         FILE *f;
532         /* size can be 128, 256, 512, 1024, 2048, 4096 */
533         uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
534
535         /* not configured, let it as is */
536         if (!max_size)
537                 return 0;
538
539         snprintf(filename, sizeof(filename),
540                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
541                         loc->domain, loc->bus, loc->devid, loc->function);
542         f = fopen(filename, "rw+");
543         if (!f)
544                 return -1;
545
546         fgets(buf, sizeof(buf), f);
547         snprintf(param, sizeof(param), "%d", max_size);
548
549         /* check if the size to be set is the same as current */
550         if (strcmp(buf, param) == 0) {
551                 fclose(f);
552                 return 0;
553         }
554         fseek(f, 0, SEEK_SET);
555         fputs(param, f);
556         fclose(f);
557
558         return 0;
559 }
560
561 static void
562 pci_config_space_set(struct rte_pci_device *dev)
563 {
564         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
565                 return;
566
567         /* configure extended tag */
568         pci_config_extended_tag(dev);
569
570         /* configure max read request size */
571         pci_config_max_read_request_size(dev);
572 }
573 #endif
574
575 /* Init the PCI EAL subsystem */
576 int
577 rte_eal_pci_init(void)
578 {
579         TAILQ_INIT(&pci_driver_list);
580         TAILQ_INIT(&pci_device_list);
581
582         /* for debug purposes, PCI can be disabled */
583         if (internal_config.no_pci)
584                 return 0;
585
586         if (rte_eal_pci_scan() < 0) {
587                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
588                 return -1;
589         }
590 #ifdef VFIO_PRESENT
591         pci_vfio_enable();
592
593         if (pci_vfio_is_enabled()) {
594
595                 /* if we are primary process, create a thread to communicate with
596                  * secondary processes. the thread will use a socket to wait for
597                  * requests from secondary process to send open file descriptors,
598                  * because VFIO does not allow multiple open descriptors on a group or
599                  * VFIO container.
600                  */
601                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
602                                 pci_vfio_mp_sync_setup() < 0)
603                         return -1;
604         }
605 #endif
606         return 0;
607 }