common/mlx5: move doorbell record from net driver
[dpdk.git] / drivers / common / mlx5 / linux / mlx5_common_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8 #ifdef RTE_IBVERBS_LINK_DLOPEN
9 #include <dlfcn.h>
10 #endif
11 #include <dirent.h>
12 #include <net/if.h>
13
14 #include <rte_errno.h>
15 #include <rte_string_fns.h>
16
17 #include "mlx5_common.h"
18 #include "mlx5_common_utils.h"
19 #include "mlx5_glue.h"
20
21 #ifdef MLX5_GLUE
22 const struct mlx5_glue *mlx5_glue;
23 #endif
24
25 /**
26  * Get PCI information by sysfs device path.
27  *
28  * @param dev_path
29  *   Pointer to device sysfs folder name.
30  * @param[out] pci_addr
31  *   PCI bus address output buffer.
32  *
33  * @return
34  *   0 on success, a negative errno value otherwise and rte_errno is set.
35  */
36 int
37 mlx5_dev_to_pci_addr(const char *dev_path,
38                      struct rte_pci_addr *pci_addr)
39 {
40         FILE *file;
41         char line[32];
42         MKSTR(path, "%s/device/uevent", dev_path);
43
44         file = fopen(path, "rb");
45         if (file == NULL) {
46                 rte_errno = errno;
47                 return -rte_errno;
48         }
49         while (fgets(line, sizeof(line), file) == line) {
50                 size_t len = strlen(line);
51                 int ret;
52
53                 /* Truncate long lines. */
54                 if (len == (sizeof(line) - 1))
55                         while (line[(len - 1)] != '\n') {
56                                 ret = fgetc(file);
57                                 if (ret == EOF)
58                                         break;
59                                 line[(len - 1)] = ret;
60                         }
61                 /* Extract information. */
62                 if (sscanf(line,
63                            "PCI_SLOT_NAME="
64                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
65                            &pci_addr->domain,
66                            &pci_addr->bus,
67                            &pci_addr->devid,
68                            &pci_addr->function) == 4) {
69                         ret = 0;
70                         break;
71                 }
72         }
73         fclose(file);
74         return 0;
75 }
76
77 /**
78  * Extract port name, as a number, from sysfs or netlink information.
79  *
80  * @param[in] port_name_in
81  *   String representing the port name.
82  * @param[out] port_info_out
83  *   Port information, including port name as a number and port name
84  *   type if recognized
85  *
86  * @return
87  *   port_name field set according to recognized name format.
88  */
89 void
90 mlx5_translate_port_name(const char *port_name_in,
91                          struct mlx5_switch_info *port_info_out)
92 {
93         char pf_c1, pf_c2, vf_c1, vf_c2;
94         char *end;
95         int sc_items;
96
97         /*
98          * Check for port-name as a string of the form pf0vf0
99          * (support kernel ver >= 5.0 or OFED ver >= 4.6).
100          */
101         sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
102                           &pf_c1, &pf_c2, &port_info_out->pf_num,
103                           &vf_c1, &vf_c2, &port_info_out->port_name);
104         if (sc_items == 6 &&
105             pf_c1 == 'p' && pf_c2 == 'f' &&
106             vf_c1 == 'v' && vf_c2 == 'f') {
107                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
108                 return;
109         }
110         /*
111          * Check for port-name as a string of the form p0
112          * (support kernel ver >= 5.0, or OFED ver >= 4.6).
113          */
114         sc_items = sscanf(port_name_in, "%c%d",
115                           &pf_c1, &port_info_out->port_name);
116         if (sc_items == 2 && pf_c1 == 'p') {
117                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
118                 return;
119         }
120         /* Check for port-name as a number (support kernel ver < 5.0 */
121         errno = 0;
122         port_info_out->port_name = strtol(port_name_in, &end, 0);
123         if (!errno &&
124             (size_t)(end - port_name_in) == strlen(port_name_in)) {
125                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
126                 return;
127         }
128         port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
129 }
130
131 /**
132  * Get kernel interface name from IB device path.
133  *
134  * @param[in] ibdev_path
135  *   Pointer to IB device path.
136  * @param[out] ifname
137  *   Interface name output buffer.
138  *
139  * @return
140  *   0 on success, a negative errno value otherwise and rte_errno is set.
141  */
142 int
143 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
144 {
145         DIR *dir;
146         struct dirent *dent;
147         unsigned int dev_type = 0;
148         unsigned int dev_port_prev = ~0u;
149         char match[IF_NAMESIZE] = "";
150
151         MLX5_ASSERT(ibdev_path);
152         {
153                 MKSTR(path, "%s/device/net", ibdev_path);
154
155                 dir = opendir(path);
156                 if (dir == NULL) {
157                         rte_errno = errno;
158                         return -rte_errno;
159                 }
160         }
161         while ((dent = readdir(dir)) != NULL) {
162                 char *name = dent->d_name;
163                 FILE *file;
164                 unsigned int dev_port;
165                 int r;
166
167                 if ((name[0] == '.') &&
168                     ((name[1] == '\0') ||
169                      ((name[1] == '.') && (name[2] == '\0'))))
170                         continue;
171
172                 MKSTR(path, "%s/device/net/%s/%s",
173                       ibdev_path, name,
174                       (dev_type ? "dev_id" : "dev_port"));
175
176                 file = fopen(path, "rb");
177                 if (file == NULL) {
178                         if (errno != ENOENT)
179                                 continue;
180                         /*
181                          * Switch to dev_id when dev_port does not exist as
182                          * is the case with Linux kernel versions < 3.15.
183                          */
184 try_dev_id:
185                         match[0] = '\0';
186                         if (dev_type)
187                                 break;
188                         dev_type = 1;
189                         dev_port_prev = ~0u;
190                         rewinddir(dir);
191                         continue;
192                 }
193                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
194                 fclose(file);
195                 if (r != 1)
196                         continue;
197                 /*
198                  * Switch to dev_id when dev_port returns the same value for
199                  * all ports. May happen when using a MOFED release older than
200                  * 3.0 with a Linux kernel >= 3.15.
201                  */
202                 if (dev_port == dev_port_prev)
203                         goto try_dev_id;
204                 dev_port_prev = dev_port;
205                 if (dev_port == 0)
206                         strlcpy(match, name, IF_NAMESIZE);
207         }
208         closedir(dir);
209         if (match[0] == '\0') {
210                 rte_errno = ENOENT;
211                 return -rte_errno;
212         }
213         strncpy(ifname, match, IF_NAMESIZE);
214         return 0;
215 }
216
217 #ifdef MLX5_GLUE
218
219 /**
220  * Suffix RTE_EAL_PMD_PATH with "-glue".
221  *
222  * This function performs a sanity check on RTE_EAL_PMD_PATH before
223  * suffixing its last component.
224  *
225  * @param buf[out]
226  *   Output buffer, should be large enough otherwise NULL is returned.
227  * @param size
228  *   Size of @p out.
229  *
230  * @return
231  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
232  */
233 static char *
234 mlx5_glue_path(char *buf, size_t size)
235 {
236         static const char *const bad[] = { "/", ".", "..", NULL };
237         const char *path = RTE_EAL_PMD_PATH;
238         size_t len = strlen(path);
239         size_t off;
240         int i;
241
242         while (len && path[len - 1] == '/')
243                 --len;
244         for (off = len; off && path[off - 1] != '/'; --off)
245                 ;
246         for (i = 0; bad[i]; ++i)
247                 if (!strncmp(path + off, bad[i], (int)(len - off)))
248                         goto error;
249         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
250         if (i == -1 || (size_t)i >= size)
251                 goto error;
252         return buf;
253 error:
254         RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
255                 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
256                 " re-configure DPDK");
257         return NULL;
258 }
259
260 static int
261 mlx5_glue_dlopen(void)
262 {
263         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
264         void *handle = NULL;
265
266         char const *path[] = {
267                 /*
268                  * A basic security check is necessary before trusting
269                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
270                  */
271                 (geteuid() == getuid() && getegid() == getgid() ?
272                  getenv("MLX5_GLUE_PATH") : NULL),
273                 /*
274                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
275                  * variant, otherwise let dlopen() look up libraries on its
276                  * own.
277                  */
278                 (*RTE_EAL_PMD_PATH ?
279                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
280         };
281         unsigned int i = 0;
282         void **sym;
283         const char *dlmsg;
284
285         while (!handle && i != RTE_DIM(path)) {
286                 const char *end;
287                 size_t len;
288                 int ret;
289
290                 if (!path[i]) {
291                         ++i;
292                         continue;
293                 }
294                 end = strpbrk(path[i], ":;");
295                 if (!end)
296                         end = path[i] + strlen(path[i]);
297                 len = end - path[i];
298                 ret = 0;
299                 do {
300                         char name[ret + 1];
301
302                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
303                                        (int)len, path[i],
304                                        (!len || *(end - 1) == '/') ? "" : "/");
305                         if (ret == -1)
306                                 break;
307                         if (sizeof(name) != (size_t)ret + 1)
308                                 continue;
309                         DRV_LOG(DEBUG, "Looking for rdma-core glue as "
310                                 "\"%s\"", name);
311                         handle = dlopen(name, RTLD_LAZY);
312                         break;
313                 } while (1);
314                 path[i] = end + 1;
315                 if (!*end)
316                         ++i;
317         }
318         if (!handle) {
319                 rte_errno = EINVAL;
320                 dlmsg = dlerror();
321                 if (dlmsg)
322                         DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
323                 goto glue_error;
324         }
325         sym = dlsym(handle, "mlx5_glue");
326         if (!sym || !*sym) {
327                 rte_errno = EINVAL;
328                 dlmsg = dlerror();
329                 if (dlmsg)
330                         DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
331                 goto glue_error;
332         }
333         mlx5_glue = *sym;
334         return 0;
335
336 glue_error:
337         if (handle)
338                 dlclose(handle);
339         return -1;
340 }
341
342 #endif
343
344 /**
345  * Initialization routine for run-time dependency on rdma-core.
346  */
347 void
348 mlx5_glue_constructor(void)
349 {
350         /*
351          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
352          * huge pages. Calling ibv_fork_init() during init allows
353          * applications to use fork() safely for purposes other than
354          * using this PMD, which is not supported in forked processes.
355          */
356         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
357         /* Match the size of Rx completion entry to the size of a cacheline. */
358         if (RTE_CACHE_LINE_SIZE == 128)
359                 setenv("MLX5_CQE_SIZE", "128", 0);
360         /*
361          * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
362          * cleanup all the Verbs resources even when the device was removed.
363          */
364         setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
365
366 #ifdef MLX5_GLUE
367         if (mlx5_glue_dlopen() != 0)
368                 goto glue_error;
369 #endif
370
371 #ifdef RTE_LIBRTE_MLX5_DEBUG
372         /* Glue structure must not contain any NULL pointers. */
373         {
374                 unsigned int i;
375
376                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
377                         MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
378         }
379 #endif
380         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
381                 rte_errno = EINVAL;
382                 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
383                         "required", mlx5_glue->version, MLX5_GLUE_VERSION);
384                 goto glue_error;
385         }
386         mlx5_glue->fork_init();
387         return;
388
389 glue_error:
390         DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
391                 " run-time dependency on rdma-core libraries (libibverbs,"
392                 " libmlx5)");
393         mlx5_glue = NULL;
394 }
395