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