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