common/mlx5: share PCI device detection
[dpdk.git] / drivers / common / mlx5 / mlx5_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #include <dlfcn.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #include <rte_errno.h>
11
12 #include "mlx5_common.h"
13 #include "mlx5_common_utils.h"
14 #include "mlx5_glue.h"
15
16
17 int mlx5_common_logtype;
18
19
20 /**
21  * Get PCI information by sysfs device path.
22  *
23  * @param dev_path
24  *   Pointer to device sysfs folder name.
25  * @param[out] pci_addr
26  *   PCI bus address output buffer.
27  *
28  * @return
29  *   0 on success, a negative errno value otherwise and rte_errno is set.
30  */
31 int
32 mlx5_dev_to_pci_addr(const char *dev_path,
33                      struct rte_pci_addr *pci_addr)
34 {
35         FILE *file;
36         char line[32];
37         MKSTR(path, "%s/device/uevent", dev_path);
38
39         file = fopen(path, "rb");
40         if (file == NULL) {
41                 rte_errno = errno;
42                 return -rte_errno;
43         }
44         while (fgets(line, sizeof(line), file) == line) {
45                 size_t len = strlen(line);
46                 int ret;
47
48                 /* Truncate long lines. */
49                 if (len == (sizeof(line) - 1))
50                         while (line[(len - 1)] != '\n') {
51                                 ret = fgetc(file);
52                                 if (ret == EOF)
53                                         break;
54                                 line[(len - 1)] = ret;
55                         }
56                 /* Extract information. */
57                 if (sscanf(line,
58                            "PCI_SLOT_NAME="
59                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
60                            &pci_addr->domain,
61                            &pci_addr->bus,
62                            &pci_addr->devid,
63                            &pci_addr->function) == 4) {
64                         ret = 0;
65                         break;
66                 }
67         }
68         fclose(file);
69         return 0;
70 }
71
72 #ifdef RTE_IBVERBS_LINK_DLOPEN
73
74 /**
75  * Suffix RTE_EAL_PMD_PATH with "-glue".
76  *
77  * This function performs a sanity check on RTE_EAL_PMD_PATH before
78  * suffixing its last component.
79  *
80  * @param buf[out]
81  *   Output buffer, should be large enough otherwise NULL is returned.
82  * @param size
83  *   Size of @p out.
84  *
85  * @return
86  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
87  */
88 static char *
89 mlx5_glue_path(char *buf, size_t size)
90 {
91         static const char *const bad[] = { "/", ".", "..", NULL };
92         const char *path = RTE_EAL_PMD_PATH;
93         size_t len = strlen(path);
94         size_t off;
95         int i;
96
97         while (len && path[len - 1] == '/')
98                 --len;
99         for (off = len; off && path[off - 1] != '/'; --off)
100                 ;
101         for (i = 0; bad[i]; ++i)
102                 if (!strncmp(path + off, bad[i], (int)(len - off)))
103                         goto error;
104         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
105         if (i == -1 || (size_t)i >= size)
106                 goto error;
107         return buf;
108 error:
109         RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
110                 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
111                 " re-configure DPDK");
112         return NULL;
113 }
114 #endif
115
116 /**
117  * Initialization routine for run-time dependency on rdma-core.
118  */
119 RTE_INIT_PRIO(mlx5_glue_init, CLASS)
120 {
121         void *handle = NULL;
122
123         /* Initialize common log type. */
124         mlx5_common_logtype = rte_log_register("pmd.common.mlx5");
125         if (mlx5_common_logtype >= 0)
126                 rte_log_set_level(mlx5_common_logtype, RTE_LOG_NOTICE);
127         /*
128          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
129          * huge pages. Calling ibv_fork_init() during init allows
130          * applications to use fork() safely for purposes other than
131          * using this PMD, which is not supported in forked processes.
132          */
133         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
134         /* Match the size of Rx completion entry to the size of a cacheline. */
135         if (RTE_CACHE_LINE_SIZE == 128)
136                 setenv("MLX5_CQE_SIZE", "128", 0);
137         /*
138          * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
139          * cleanup all the Verbs resources even when the device was removed.
140          */
141         setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
142         /* The glue initialization was done earlier by mlx5 common library. */
143 #ifdef RTE_IBVERBS_LINK_DLOPEN
144         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
145         const char *path[] = {
146                 /*
147                  * A basic security check is necessary before trusting
148                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
149                  */
150                 (geteuid() == getuid() && getegid() == getgid() ?
151                  getenv("MLX5_GLUE_PATH") : NULL),
152                 /*
153                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
154                  * variant, otherwise let dlopen() look up libraries on its
155                  * own.
156                  */
157                 (*RTE_EAL_PMD_PATH ?
158                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
159         };
160         unsigned int i = 0;
161         void **sym;
162         const char *dlmsg;
163
164         while (!handle && i != RTE_DIM(path)) {
165                 const char *end;
166                 size_t len;
167                 int ret;
168
169                 if (!path[i]) {
170                         ++i;
171                         continue;
172                 }
173                 end = strpbrk(path[i], ":;");
174                 if (!end)
175                         end = path[i] + strlen(path[i]);
176                 len = end - path[i];
177                 ret = 0;
178                 do {
179                         char name[ret + 1];
180
181                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
182                                        (int)len, path[i],
183                                        (!len || *(end - 1) == '/') ? "" : "/");
184                         if (ret == -1)
185                                 break;
186                         if (sizeof(name) != (size_t)ret + 1)
187                                 continue;
188                         DRV_LOG(DEBUG, "Looking for rdma-core glue as "
189                                 "\"%s\"", name);
190                         handle = dlopen(name, RTLD_LAZY);
191                         break;
192                 } while (1);
193                 path[i] = end + 1;
194                 if (!*end)
195                         ++i;
196         }
197         if (!handle) {
198                 rte_errno = EINVAL;
199                 dlmsg = dlerror();
200                 if (dlmsg)
201                         DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
202                 goto glue_error;
203         }
204         sym = dlsym(handle, "mlx5_glue");
205         if (!sym || !*sym) {
206                 rte_errno = EINVAL;
207                 dlmsg = dlerror();
208                 if (dlmsg)
209                         DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
210                 goto glue_error;
211         }
212         mlx5_glue = *sym;
213 #endif /* RTE_IBVERBS_LINK_DLOPEN */
214 #ifndef NDEBUG
215         /* Glue structure must not contain any NULL pointers. */
216         {
217                 unsigned int i;
218
219                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
220                         assert(((const void *const *)mlx5_glue)[i]);
221         }
222 #endif
223         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
224                 rte_errno = EINVAL;
225                 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
226                         "required", mlx5_glue->version, MLX5_GLUE_VERSION);
227                 goto glue_error;
228         }
229         mlx5_glue->fork_init();
230         return;
231 glue_error:
232         if (handle)
233                 dlclose(handle);
234         DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
235                 " run-time dependency on rdma-core libraries (libibverbs,"
236                 " libmlx5)");
237         mlx5_glue = NULL;
238         return;
239 }