1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
8 #ifdef RTE_IBVERBS_LINK_DLOPEN
14 #include <rte_errno.h>
15 #include <rte_string_fns.h>
16 #include <rte_bus_pci.h>
17 #include <rte_bus_auxiliary.h>
19 #include "mlx5_common.h"
21 #include "mlx5_common_log.h"
22 #include "mlx5_common_private.h"
23 #include "mlx5_common_defs.h"
24 #include "mlx5_common_os.h"
25 #include "mlx5_glue.h"
28 const struct mlx5_glue *mlx5_glue;
32 mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
37 MKSTR(path, "%s/device/uevent", dev_path);
39 file = fopen(path, "rb");
44 while (fgets(line, sizeof(line), file) == line) {
45 size_t len = strlen(line);
47 /* Truncate long lines. */
48 if (len == (sizeof(line) - 1)) {
49 while (line[(len - 1)] != '\n') {
50 int ret = fgetc(file);
54 line[(len - 1)] = ret;
56 /* No match for long lines. */
59 /* Extract information. */
62 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
66 &pci_addr->function) == 4) {
79 * Extract port name, as a number, from sysfs or netlink information.
81 * @param[in] port_name_in
82 * String representing the port name.
83 * @param[out] port_info_out
84 * Port information, including port name as a number and port name
88 * port_name field set according to recognized name format.
91 mlx5_translate_port_name(const char *port_name_in,
92 struct mlx5_switch_info *port_info_out)
94 char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
98 sc_items = sscanf(port_name_in, "%c%d",
99 &ctrl, &port_info_out->ctrl_num);
100 if (sc_items == 2 && ctrl == 'c') {
101 port_name_in++; /* 'c' */
102 port_name_in += snprintf(NULL, 0, "%d",
103 port_info_out->ctrl_num);
105 /* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
106 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
107 &pf_c1, &pf_c2, &port_info_out->pf_num,
108 &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
109 if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
110 if (vf_c1 == 'v' && vf_c2 == 'f') {
111 /* Kernel ver >= 5.0 or OFED ver >= 4.6 */
112 port_info_out->name_type =
113 MLX5_PHYS_PORT_NAME_TYPE_PFVF;
116 if (vf_c1 == 's' && vf_c2 == 'f') {
117 /* Kernel ver >= 5.11 or OFED ver >= 5.1 */
118 port_info_out->name_type =
119 MLX5_PHYS_PORT_NAME_TYPE_PFSF;
124 * Check for port-name as a string of the form p0
125 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
127 sc_items = sscanf(port_name_in, "%c%d%c",
128 &pf_c1, &port_info_out->port_name, &eol);
129 if (sc_items == 2 && pf_c1 == 'p') {
130 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
134 * Check for port-name as a string of the form pf0
135 * (support kernel ver >= 5.7 for HPF representor on BF).
137 sc_items = sscanf(port_name_in, "%c%c%d%c",
138 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
139 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
140 port_info_out->port_name = -1;
141 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
144 /* Check for port-name as a number (support kernel ver < 5.0 */
146 port_info_out->port_name = strtol(port_name_in, &end, 0);
148 (size_t)(end - port_name_in) == strlen(port_name_in)) {
149 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
152 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
156 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
160 unsigned int dev_type = 0;
161 unsigned int dev_port_prev = ~0u;
162 char match[IF_NAMESIZE] = "";
164 MLX5_ASSERT(ibdev_path);
166 MKSTR(path, "%s/device/net", ibdev_path);
174 while ((dent = readdir(dir)) != NULL) {
175 char *name = dent->d_name;
177 unsigned int dev_port;
180 if ((name[0] == '.') &&
181 ((name[1] == '\0') ||
182 ((name[1] == '.') && (name[2] == '\0'))))
185 MKSTR(path, "%s/device/net/%s/%s",
187 (dev_type ? "dev_id" : "dev_port"));
189 file = fopen(path, "rb");
194 * Switch to dev_id when dev_port does not exist as
195 * is the case with Linux kernel versions < 3.15.
206 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
211 * Switch to dev_id when dev_port returns the same value for
212 * all ports. May happen when using a MOFED release older than
213 * 3.0 with a Linux kernel >= 3.15.
215 if (dev_port == dev_port_prev)
217 dev_port_prev = dev_port;
219 strlcpy(match, name, IF_NAMESIZE);
222 if (match[0] == '\0') {
226 strncpy(ifname, match, IF_NAMESIZE);
233 * Suffix RTE_EAL_PMD_PATH with "-glue".
235 * This function performs a sanity check on RTE_EAL_PMD_PATH before
236 * suffixing its last component.
239 * Output buffer, should be large enough otherwise NULL is returned.
244 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
247 mlx5_glue_path(char *buf, size_t size)
249 static const char *const bad[] = { "/", ".", "..", NULL };
250 const char *path = RTE_EAL_PMD_PATH;
251 size_t len = strlen(path);
255 while (len && path[len - 1] == '/')
257 for (off = len; off && path[off - 1] != '/'; --off)
259 for (i = 0; bad[i]; ++i)
260 if (!strncmp(path + off, bad[i], (int)(len - off)))
262 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
263 if (i == -1 || (size_t)i >= size)
267 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
268 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
269 " re-configure DPDK");
274 mlx5_glue_dlopen(void)
276 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
279 char const *path[] = {
281 * A basic security check is necessary before trusting
282 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
284 (geteuid() == getuid() && getegid() == getgid() ?
285 getenv("MLX5_GLUE_PATH") : NULL),
287 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
288 * variant, otherwise let dlopen() look up libraries on its
292 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
298 while (!handle && i != RTE_DIM(path)) {
307 end = strpbrk(path[i], ":;");
309 end = path[i] + strlen(path[i]);
315 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
317 (!len || *(end - 1) == '/') ? "" : "/");
320 if (sizeof(name) != (size_t)ret + 1)
322 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
324 handle = dlopen(name, RTLD_LAZY);
335 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
338 sym = dlsym(handle, "mlx5_glue");
343 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
358 * Initialization routine for run-time dependency on rdma-core.
361 mlx5_glue_constructor(void)
364 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
365 * huge pages. Calling ibv_fork_init() during init allows
366 * applications to use fork() safely for purposes other than
367 * using this PMD, which is not supported in forked processes.
369 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
370 /* Match the size of Rx completion entry to the size of a cacheline. */
371 if (RTE_CACHE_LINE_SIZE == 128)
372 setenv("MLX5_CQE_SIZE", "128", 0);
374 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
375 * cleanup all the Verbs resources even when the device was removed.
377 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
380 if (mlx5_glue_dlopen() != 0)
384 #ifdef RTE_LIBRTE_MLX5_DEBUG
385 /* Glue structure must not contain any NULL pointers. */
389 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
390 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
393 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
395 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
396 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
399 mlx5_glue->fork_init();
403 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
404 " run-time dependency on rdma-core libraries (libibverbs,"
410 * Allocate Protection Domain object and extract its pdn using DV API.
413 * Pointer to the mlx5 device.
416 * 0 on success, a negative errno value otherwise and rte_errno is set.
419 mlx5_os_pd_create(struct mlx5_common_device *cdev)
421 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
422 struct mlx5dv_obj obj;
423 struct mlx5dv_pd pd_info;
427 cdev->pd = mlx5_glue->alloc_pd(cdev->ctx);
428 if (cdev->pd == NULL) {
429 DRV_LOG(ERR, "Failed to allocate PD.");
430 return errno ? -errno : -ENOMEM;
432 if (cdev->config.devx == 0)
434 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
435 obj.pd.in = cdev->pd;
436 obj.pd.out = &pd_info;
437 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
439 DRV_LOG(ERR, "Fail to get PD object info.");
440 mlx5_glue->dealloc_pd(cdev->pd);
444 cdev->pdn = pd_info.pdn;
447 DRV_LOG(ERR, "Cannot get pdn - no DV support.");
449 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
452 static struct ibv_device *
453 mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
456 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
457 struct ibv_device *ibv_match = NULL;
459 if (ibv_list == NULL) {
464 struct rte_pci_addr paddr;
466 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
467 if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
469 if (rte_pci_addr_cmp(addr, &paddr) != 0)
471 ibv_match = ibv_list[n];
474 if (ibv_match == NULL) {
476 "No Verbs device matches PCI device " PCI_PRI_FMT ","
477 " are kernel drivers loaded?",
478 addr->domain, addr->bus, addr->devid, addr->function);
481 mlx5_glue->free_device_list(ibv_list);
485 /* Try to disable ROCE by Netlink\Devlink. */
487 mlx5_nl_roce_disable(const char *addr)
489 int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
496 devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
497 if (devlink_id < 0) {
500 "Failed to get devlink id for ROCE operations by Netlink.");
503 ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
505 DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
508 } else if (!enable) {
509 DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
512 ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
514 DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
516 DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
522 /* Try to disable ROCE by sysfs. */
524 mlx5_sys_roce_disable(const char *addr)
530 MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
531 file_o = fopen(file_p, "rb");
536 ret = fscanf(file_o, "%d", &enable);
541 } else if (!enable) {
543 DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
547 file_o = fopen(file_p, "wb");
552 fprintf(file_o, "0\n");
556 DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
558 DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
564 mlx5_roce_disable(const struct rte_device *dev)
566 char pci_addr[PCI_PRI_STR_SIZE] = { 0 };
568 if (mlx5_dev_to_pci_str(dev, pci_addr, sizeof(pci_addr)) < 0)
570 /* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
571 if (mlx5_nl_roce_disable(pci_addr) != 0 &&
572 mlx5_sys_roce_disable(pci_addr) != 0)
577 static struct ibv_device *
578 mlx5_os_get_ibv_dev(const struct rte_device *dev)
580 struct ibv_device *ibv;
582 if (mlx5_dev_is_pci(dev))
583 ibv = mlx5_os_get_ibv_device(&RTE_DEV_TO_PCI_CONST(dev)->addr);
585 ibv = mlx5_get_aux_ibv_device(RTE_DEV_TO_AUXILIARY_CONST(dev));
588 DRV_LOG(ERR, "Verbs device not found: %s", dev->name);
593 static struct ibv_device *
594 mlx5_vdpa_get_ibv_dev(const struct rte_device *dev)
596 struct ibv_device *ibv;
599 if (mlx5_roce_disable(dev) != 0) {
600 DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
604 /* Wait for the IB device to appear again after reload. */
605 for (retry = MLX5_VDPA_MAX_RETRIES; retry > 0; --retry) {
606 ibv = mlx5_os_get_ibv_dev(dev);
609 usleep(MLX5_VDPA_USEC);
612 "Cannot get IB device after disabling RoCE for \"%s\", retries exceed %d.",
613 dev->name, MLX5_VDPA_MAX_RETRIES);
619 mlx5_config_doorbell_mapping_env(int dbnc)
624 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
625 /* Get environment variable to store. */
626 env = getenv(MLX5_SHUT_UP_BF);
627 value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
628 if (dbnc == MLX5_ARG_UNSET)
629 setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
631 setenv(MLX5_SHUT_UP_BF,
632 dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
637 mlx5_restore_doorbell_mapping_env(int value)
639 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
640 /* Restore the original environment variable state. */
641 if (value == MLX5_ARG_UNSET)
642 unsetenv(MLX5_SHUT_UP_BF);
644 setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
648 * Function API to open IB device.
652 * Pointer to the mlx5 device.
654 * Chosen classes come from device arguments.
657 * 0 on success, a negative errno value otherwise and rte_errno is set.
660 mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes)
662 struct ibv_device *ibv;
663 struct ibv_context *ctx = NULL;
666 if (classes & MLX5_CLASS_VDPA)
667 ibv = mlx5_vdpa_get_ibv_dev(cdev->dev);
669 ibv = mlx5_os_get_ibv_dev(cdev->dev);
672 DRV_LOG(INFO, "Dev information matches for device \"%s\".", ibv->name);
674 * Configure environment variable "MLX5_BF_SHUT_UP" before the device
675 * creation. The rdma_core library checks the variable at device
676 * creation and stores the result internally.
678 dbmap_env = mlx5_config_doorbell_mapping_env(cdev->config.dbnc);
679 /* Try to open IB device with DV first, then usual Verbs. */
681 ctx = mlx5_glue->dv_open_device(ibv);
683 cdev->config.devx = 1;
684 DRV_LOG(DEBUG, "DevX is supported.");
685 } else if (classes == MLX5_CLASS_ETH) {
686 /* The environment variable is still configured. */
687 ctx = mlx5_glue->open_device(ibv);
690 DRV_LOG(DEBUG, "DevX is NOT supported.");
694 /* The device is created, no need for environment. */
695 mlx5_restore_doorbell_mapping_env(dbmap_env);
696 /* Hint libmlx5 to use PMD allocator for data plane resources */
697 mlx5_set_context_attr(cdev->dev, ctx);
701 rte_errno = errno ? errno : ENODEV;
702 /* The device creation is failed, no need for environment. */
703 mlx5_restore_doorbell_mapping_env(dbmap_env);
704 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);