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