eal/linux: enable uio_pci_generic support
[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 #include <sys/mman.h>
37
38 #include <rte_log.h>
39 #include <rte_pci.h>
40 #include <rte_tailq.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_malloc.h>
43 #include <rte_devargs.h>
44
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_private.h"
48 #include "eal_pci_init.h"
49
50 /**
51  * @file
52  * PCI probing under linux
53  *
54  * This code is used to simulate a PCI probe by parsing information in sysfs.
55  * When a registered device matches a driver, it is then initialized with
56  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
57  */
58
59 struct mapped_pci_res_list *pci_res_list = NULL;
60
61 /* unbind kernel driver for this device */
62 static int
63 pci_unbind_kernel_driver(struct rte_pci_device *dev)
64 {
65         int n;
66         FILE *f;
67         char filename[PATH_MAX];
68         char buf[BUFSIZ];
69         struct rte_pci_addr *loc = &dev->addr;
70
71         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
72         snprintf(filename, sizeof(filename),
73                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
74                  loc->domain, loc->bus, loc->devid, loc->function);
75
76         f = fopen(filename, "w");
77         if (f == NULL) /* device was not bound */
78                 return 0;
79
80         n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
81                      loc->domain, loc->bus, loc->devid, loc->function);
82         if ((n < 0) || (n >= (int)sizeof(buf))) {
83                 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
84                 goto error;
85         }
86         if (fwrite(buf, n, 1, f) == 0) {
87                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
88                                 filename);
89                 goto error;
90         }
91
92         fclose(f);
93         return 0;
94
95 error:
96         fclose(f);
97         return -1;
98 }
99
100 void *
101 pci_find_max_end_va(void)
102 {
103         const struct rte_memseg *seg = rte_eal_get_physmem_layout();
104         const struct rte_memseg *last = seg;
105         unsigned i = 0;
106
107         for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
108                 if (seg->addr == NULL)
109                         break;
110
111                 if (seg->addr > last->addr)
112                         last = seg;
113
114         }
115         return RTE_PTR_ADD(last->addr, last->len);
116 }
117
118
119 /* map a particular resource from a file */
120 void *
121 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size)
122 {
123         void *mapaddr;
124
125         /* Map the PCI memory resource of device */
126         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
127                         MAP_SHARED, fd, offset);
128         if (mapaddr == MAP_FAILED) {
129                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
130                         __func__, fd, requested_addr,
131                         (unsigned long)size, (unsigned long)offset,
132                         strerror(errno), mapaddr);
133         } else {
134                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
135         }
136
137         return mapaddr;
138 }
139
140 /* parse the "resource" sysfs file */
141 static int
142 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
143 {
144         FILE *f;
145         char buf[BUFSIZ];
146         union pci_resource_info {
147                 struct {
148                         char *phys_addr;
149                         char *end_addr;
150                         char *flags;
151                 };
152                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
153         } res_info;
154         int i;
155         uint64_t phys_addr, end_addr, flags;
156
157         f = fopen(filename, "r");
158         if (f == NULL) {
159                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
160                 return -1;
161         }
162
163         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
164
165                 if (fgets(buf, sizeof(buf), f) == NULL) {
166                         RTE_LOG(ERR, EAL,
167                                 "%s(): cannot read resource\n", __func__);
168                         goto error;
169                 }
170
171                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
172                         RTE_LOG(ERR, EAL,
173                                 "%s(): bad resource format\n", __func__);
174                         goto error;
175                 }
176                 errno = 0;
177                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
178                 end_addr = strtoull(res_info.end_addr, NULL, 16);
179                 flags = strtoull(res_info.flags, NULL, 16);
180                 if (errno != 0) {
181                         RTE_LOG(ERR, EAL,
182                                 "%s(): bad resource format\n", __func__);
183                         goto error;
184                 }
185
186                 if (flags & IORESOURCE_MEM) {
187                         dev->mem_resource[i].phys_addr = phys_addr;
188                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
189                         /* not mapped for now */
190                         dev->mem_resource[i].addr = NULL;
191                 }
192         }
193         fclose(f);
194         return 0;
195
196 error:
197         fclose(f);
198         return -1;
199 }
200
201 /* Compare two PCI device addresses. */
202 static int
203 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
204 {
205         uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
206         uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
207
208         if (dev_addr > dev_addr2)
209                 return 1;
210         else
211                 return 0;
212 }
213
214
215 /* Scan one pci sysfs entry, and fill the devices list from it. */
216 static int
217 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
218              uint8_t devid, uint8_t function)
219 {
220         char filename[PATH_MAX];
221         unsigned long tmp;
222         struct rte_pci_device *dev;
223
224         dev = malloc(sizeof(*dev));
225         if (dev == NULL) {
226                 return -1;
227         }
228
229         memset(dev, 0, sizeof(*dev));
230         dev->addr.domain = domain;
231         dev->addr.bus = bus;
232         dev->addr.devid = devid;
233         dev->addr.function = function;
234
235         /* get vendor id */
236         snprintf(filename, sizeof(filename), "%s/vendor", dirname);
237         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
238                 free(dev);
239                 return -1;
240         }
241         dev->id.vendor_id = (uint16_t)tmp;
242
243         /* get device id */
244         snprintf(filename, sizeof(filename), "%s/device", dirname);
245         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
246                 free(dev);
247                 return -1;
248         }
249         dev->id.device_id = (uint16_t)tmp;
250
251         /* get subsystem_vendor id */
252         snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
253                  dirname);
254         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
255                 free(dev);
256                 return -1;
257         }
258         dev->id.subsystem_vendor_id = (uint16_t)tmp;
259
260         /* get subsystem_device id */
261         snprintf(filename, sizeof(filename), "%s/subsystem_device",
262                  dirname);
263         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
264                 free(dev);
265                 return -1;
266         }
267         dev->id.subsystem_device_id = (uint16_t)tmp;
268
269         /* get max_vfs */
270         dev->max_vfs = 0;
271         snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
272         if (!access(filename, F_OK) &&
273             eal_parse_sysfs_value(filename, &tmp) == 0)
274                 dev->max_vfs = (uint16_t)tmp;
275         else {
276                 /* for non igb_uio driver, need kernel version >= 3.8 */
277                 snprintf(filename, sizeof(filename),
278                          "%s/sriov_numvfs", dirname);
279                 if (!access(filename, F_OK) &&
280                     eal_parse_sysfs_value(filename, &tmp) == 0)
281                         dev->max_vfs = (uint16_t)tmp;
282         }
283
284         /* get numa node */
285         snprintf(filename, sizeof(filename), "%s/numa_node",
286                  dirname);
287         if (access(filename, R_OK) != 0) {
288                 /* if no NUMA support just set node to -1 */
289                 dev->numa_node = -1;
290         } else {
291                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
292                         free(dev);
293                         return -1;
294                 }
295                 dev->numa_node = tmp;
296         }
297
298         /* parse resources */
299         snprintf(filename, sizeof(filename), "%s/resource", dirname);
300         if (pci_parse_sysfs_resource(filename, dev) < 0) {
301                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
302                 free(dev);
303                 return -1;
304         }
305
306         /* device is valid, add in list (sorted) */
307         if (TAILQ_EMPTY(&pci_device_list)) {
308                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
309         }
310         else {
311                 struct rte_pci_device *dev2 = NULL;
312
313                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
314                         if (pci_addr_comparison(&dev->addr, &dev2->addr))
315                                 continue;
316                         else {
317                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
318                                 return 0;
319                         }
320                 }
321                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
322         }
323
324         return 0;
325 }
326
327 /*
328  * split up a pci address into its constituent parts.
329  */
330 static int
331 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
332                 uint8_t *bus, uint8_t *devid, uint8_t *function)
333 {
334         /* first split on ':' */
335         union splitaddr {
336                 struct {
337                         char *domain;
338                         char *bus;
339                         char *devid;
340                         char *function;
341                 };
342                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
343         } splitaddr;
344
345         char *buf_copy = strndup(buf, bufsize);
346         if (buf_copy == NULL)
347                 return -1;
348
349         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
350                         != PCI_FMT_NVAL - 1)
351                 goto error;
352         /* final split is on '.' between devid and function */
353         splitaddr.function = strchr(splitaddr.devid,'.');
354         if (splitaddr.function == NULL)
355                 goto error;
356         *splitaddr.function++ = '\0';
357
358         /* now convert to int values */
359         errno = 0;
360         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
361         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
362         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
363         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
364         if (errno != 0)
365                 goto error;
366
367         free(buf_copy); /* free the copy made with strdup */
368         return 0;
369 error:
370         free(buf_copy);
371         return -1;
372 }
373
374 /*
375  * Scan the content of the PCI bus, and the devices in the devices
376  * list
377  */
378 static int
379 pci_scan(void)
380 {
381         struct dirent *e;
382         DIR *dir;
383         char dirname[PATH_MAX];
384         uint16_t domain;
385         uint8_t bus, devid, function;
386
387         dir = opendir(SYSFS_PCI_DEVICES);
388         if (dir == NULL) {
389                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
390                         __func__, strerror(errno));
391                 return -1;
392         }
393
394         while ((e = readdir(dir)) != NULL) {
395                 if (e->d_name[0] == '.')
396                         continue;
397
398                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
399                                 &bus, &devid, &function) != 0)
400                         continue;
401
402                 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
403                          e->d_name);
404                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
405                         goto error;
406         }
407         closedir(dir);
408         return 0;
409
410 error:
411         closedir(dir);
412         return -1;
413 }
414
415 #ifdef RTE_PCI_CONFIG
416 static int
417 pci_config_extended_tag(struct rte_pci_device *dev)
418 {
419         struct rte_pci_addr *loc = &dev->addr;
420         char filename[PATH_MAX];
421         char buf[BUFSIZ];
422         FILE *f;
423
424         /* not configured, let it as is */
425         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
426                 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
427                 return 0;
428
429         snprintf(filename, sizeof(filename),
430                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
431                 loc->domain, loc->bus, loc->devid, loc->function);
432         f = fopen(filename, "rw+");
433         if (!f)
434                 return -1;
435
436         fgets(buf, sizeof(buf), f);
437         if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
438                 /* enable Extended Tag*/
439                 if (strncmp(buf, "on", 2) != 0) {
440                         fseek(f, 0, SEEK_SET);
441                         fputs("on", f);
442                 }
443         } else {
444                 /* disable Extended Tag */
445                 if (strncmp(buf, "off", 3) != 0) {
446                         fseek(f, 0, SEEK_SET);
447                         fputs("off", f);
448                 }
449         }
450         fclose(f);
451
452         return 0;
453 }
454
455 static int
456 pci_config_max_read_request_size(struct rte_pci_device *dev)
457 {
458         struct rte_pci_addr *loc = &dev->addr;
459         char filename[PATH_MAX];
460         char buf[BUFSIZ], param[BUFSIZ];
461         FILE *f;
462         /* size can be 128, 256, 512, 1024, 2048, 4096 */
463         uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
464
465         /* not configured, let it as is */
466         if (!max_size)
467                 return 0;
468
469         snprintf(filename, sizeof(filename),
470                 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
471                         loc->domain, loc->bus, loc->devid, loc->function);
472         f = fopen(filename, "rw+");
473         if (!f)
474                 return -1;
475
476         fgets(buf, sizeof(buf), f);
477         snprintf(param, sizeof(param), "%d", max_size);
478
479         /* check if the size to be set is the same as current */
480         if (strcmp(buf, param) == 0) {
481                 fclose(f);
482                 return 0;
483         }
484         fseek(f, 0, SEEK_SET);
485         fputs(param, f);
486         fclose(f);
487
488         return 0;
489 }
490
491 static void
492 pci_config_space_set(struct rte_pci_device *dev)
493 {
494         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
495                 return;
496
497         /* configure extended tag */
498         pci_config_extended_tag(dev);
499
500         /* configure max read request size */
501         pci_config_max_read_request_size(dev);
502 }
503 #endif
504
505 static int
506 pci_map_device(struct rte_pci_device *dev)
507 {
508         int ret, mapped = 0;
509
510         /* try mapping the NIC resources using VFIO if it exists */
511 #ifdef VFIO_PRESENT
512         if (pci_vfio_is_enabled()) {
513                 ret = pci_vfio_map_resource(dev);
514                 if (ret == 0)
515                         mapped = 1;
516                 else if (ret < 0)
517                         return ret;
518         }
519 #endif
520         /* map resources for devices that use uio_pci_generic or igb_uio */
521         if (!mapped) {
522                 ret = pci_uio_map_resource(dev);
523                 if (ret != 0)
524                         return ret;
525         }
526         return 0;
527 }
528
529 /*
530  * If vendor/device ID match, call the devinit() function of the
531  * driver.
532  */
533 int
534 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
535 {
536         int ret;
537         struct rte_pci_id *id_table;
538
539         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
540
541                 /* check if device's identifiers match the driver's ones */
542                 if (id_table->vendor_id != dev->id.vendor_id &&
543                                 id_table->vendor_id != PCI_ANY_ID)
544                         continue;
545                 if (id_table->device_id != dev->id.device_id &&
546                                 id_table->device_id != PCI_ANY_ID)
547                         continue;
548                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
549                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
550                         continue;
551                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
552                                 id_table->subsystem_device_id != PCI_ANY_ID)
553                         continue;
554
555                 struct rte_pci_addr *loc = &dev->addr;
556
557                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
558                                 loc->domain, loc->bus, loc->devid, loc->function,
559                                 dev->numa_node);
560
561                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
562                                 dev->id.device_id, dr->name);
563
564                 /* no initialization when blacklisted, return without error */
565                 if (dev->devargs != NULL &&
566                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
567                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
568                         return 1;
569                 }
570
571                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
572 #ifdef RTE_PCI_CONFIG
573                         /*
574                          * Set PCIe config space for high performance.
575                          * Return value can be ignored.
576                          */
577                         pci_config_space_set(dev);
578 #endif
579                         /* map resources for devices that use igb_uio */
580                         ret = pci_map_device(dev);
581                         if (ret != 0)
582                                 return ret;
583                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
584                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
585                         /* unbind current driver */
586                         if (pci_unbind_kernel_driver(dev) < 0)
587                                 return -1;
588                 }
589
590                 /* reference driver structure */
591                 dev->driver = dr;
592
593                 /* call the driver devinit() function */
594                 return dr->devinit(dr, dev);
595         }
596         /* return positive value if driver is not found */
597         return 1;
598 }
599
600 /* Init the PCI EAL subsystem */
601 int
602 rte_eal_pci_init(void)
603 {
604         TAILQ_INIT(&pci_driver_list);
605         TAILQ_INIT(&pci_device_list);
606         pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
607                         mapped_pci_res_list);
608
609         /* for debug purposes, PCI can be disabled */
610         if (internal_config.no_pci)
611                 return 0;
612
613         if (pci_scan() < 0) {
614                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
615                 return -1;
616         }
617 #ifdef VFIO_PRESENT
618         pci_vfio_enable();
619
620         if (pci_vfio_is_enabled()) {
621
622                 /* if we are primary process, create a thread to communicate with
623                  * secondary processes. the thread will use a socket to wait for
624                  * requests from secondary process to send open file descriptors,
625                  * because VFIO does not allow multiple open descriptors on a group or
626                  * VFIO container.
627                  */
628                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
629                                 pci_vfio_mp_sync_setup() < 0)
630                         return -1;
631         }
632 #endif
633         return 0;
634 }