xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.c (revision 9d936f4f1a5e5b18d3d4582ebb1a0d3a638826b4)
179aa4307SOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
279aa4307SOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
379aa4307SOphir Munk  */
479aa4307SOphir Munk 
54c74ad3eSRongwei Liu #include <sys/types.h>
679aa4307SOphir Munk #include <unistd.h>
779aa4307SOphir Munk #include <string.h>
879aa4307SOphir Munk #include <stdio.h>
979aa4307SOphir Munk #ifdef RTE_IBVERBS_LINK_DLOPEN
1079aa4307SOphir Munk #include <dlfcn.h>
1179aa4307SOphir Munk #endif
12aec086c9SMatan Azrad #include <dirent.h>
13aec086c9SMatan Azrad #include <net/if.h>
1479aa4307SOphir Munk 
1579aa4307SOphir Munk #include <rte_errno.h>
16aec086c9SMatan Azrad #include <rte_string_fns.h>
17662d0dc6SMichael Baum #include <rte_bus_pci.h>
18662d0dc6SMichael Baum #include <rte_bus_auxiliary.h>
1979aa4307SOphir Munk 
2079aa4307SOphir Munk #include "mlx5_common.h"
21662d0dc6SMichael Baum #include "mlx5_nl.h"
2225245d5dSShiri Kuzin #include "mlx5_common_log.h"
23662d0dc6SMichael Baum #include "mlx5_common_private.h"
24887183efSMichael Baum #include "mlx5_common_defs.h"
25c31f3f7fSShiri Kuzin #include "mlx5_common_os.h"
2679aa4307SOphir Munk #include "mlx5_glue.h"
2779aa4307SOphir Munk 
2879aa4307SOphir Munk #ifdef MLX5_GLUE
2979aa4307SOphir Munk const struct mlx5_glue *mlx5_glue;
3079aa4307SOphir Munk #endif
3179aa4307SOphir Munk 
3279aa4307SOphir Munk int
334d567938SThomas Monjalon mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
3479aa4307SOphir Munk {
3579aa4307SOphir Munk 	FILE *file;
3679aa4307SOphir Munk 	char line[32];
37482a1d34SViacheslav Ovsiienko 	int rc = -ENOENT;
3879aa4307SOphir Munk 	MKSTR(path, "%s/device/uevent", dev_path);
3979aa4307SOphir Munk 
4079aa4307SOphir Munk 	file = fopen(path, "rb");
4179aa4307SOphir Munk 	if (file == NULL) {
4279aa4307SOphir Munk 		rte_errno = errno;
4379aa4307SOphir Munk 		return -rte_errno;
4479aa4307SOphir Munk 	}
4579aa4307SOphir Munk 	while (fgets(line, sizeof(line), file) == line) {
4679aa4307SOphir Munk 		size_t len = strlen(line);
4779aa4307SOphir Munk 
4879aa4307SOphir Munk 		/* Truncate long lines. */
49482a1d34SViacheslav Ovsiienko 		if (len == (sizeof(line) - 1)) {
5079aa4307SOphir Munk 			while (line[(len - 1)] != '\n') {
51482a1d34SViacheslav Ovsiienko 				int ret = fgetc(file);
52482a1d34SViacheslav Ovsiienko 
5379aa4307SOphir Munk 				if (ret == EOF)
54482a1d34SViacheslav Ovsiienko 					goto exit;
5579aa4307SOphir Munk 				line[(len - 1)] = ret;
5679aa4307SOphir Munk 			}
57482a1d34SViacheslav Ovsiienko 			/* No match for long lines. */
58482a1d34SViacheslav Ovsiienko 			continue;
59482a1d34SViacheslav Ovsiienko 		}
6079aa4307SOphir Munk 		/* Extract information. */
6179aa4307SOphir Munk 		if (sscanf(line,
6279aa4307SOphir Munk 			   "PCI_SLOT_NAME="
6379aa4307SOphir Munk 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
6479aa4307SOphir Munk 			   &pci_addr->domain,
6579aa4307SOphir Munk 			   &pci_addr->bus,
6679aa4307SOphir Munk 			   &pci_addr->devid,
6779aa4307SOphir Munk 			   &pci_addr->function) == 4) {
68482a1d34SViacheslav Ovsiienko 			rc = 0;
6979aa4307SOphir Munk 			break;
7079aa4307SOphir Munk 		}
7179aa4307SOphir Munk 	}
72482a1d34SViacheslav Ovsiienko exit:
7379aa4307SOphir Munk 	fclose(file);
74482a1d34SViacheslav Ovsiienko 	if (rc)
75482a1d34SViacheslav Ovsiienko 		rte_errno = -rc;
76482a1d34SViacheslav Ovsiienko 	return rc;
7779aa4307SOphir Munk }
7879aa4307SOphir Munk 
7979aa4307SOphir Munk /**
8079aa4307SOphir Munk  * Extract port name, as a number, from sysfs or netlink information.
8179aa4307SOphir Munk  *
8279aa4307SOphir Munk  * @param[in] port_name_in
8379aa4307SOphir Munk  *   String representing the port name.
8479aa4307SOphir Munk  * @param[out] port_info_out
8579aa4307SOphir Munk  *   Port information, including port name as a number and port name
8679aa4307SOphir Munk  *   type if recognized
8779aa4307SOphir Munk  *
8879aa4307SOphir Munk  * @return
8979aa4307SOphir Munk  *   port_name field set according to recognized name format.
9079aa4307SOphir Munk  */
9179aa4307SOphir Munk void
9279aa4307SOphir Munk mlx5_translate_port_name(const char *port_name_in,
9379aa4307SOphir Munk 			 struct mlx5_switch_info *port_info_out)
9479aa4307SOphir Munk {
9559df97f1SXueming Li 	char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
9679aa4307SOphir Munk 	char *end;
9779aa4307SOphir Munk 	int sc_items;
9879aa4307SOphir Munk 
9959df97f1SXueming Li 	sc_items = sscanf(port_name_in, "%c%d",
10059df97f1SXueming Li 			  &ctrl, &port_info_out->ctrl_num);
10159df97f1SXueming Li 	if (sc_items == 2 && ctrl == 'c') {
10259df97f1SXueming Li 		port_name_in++; /* 'c' */
10359df97f1SXueming Li 		port_name_in += snprintf(NULL, 0, "%d",
10459df97f1SXueming Li 					  port_info_out->ctrl_num);
10559df97f1SXueming Li 	}
10659df97f1SXueming Li 	/* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
1073590881bSViacheslav Ovsiienko 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
10879aa4307SOphir Munk 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
1093590881bSViacheslav Ovsiienko 			  &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
11059df97f1SXueming Li 	if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
11159df97f1SXueming Li 		if (vf_c1 == 'v' && vf_c2 == 'f') {
11259df97f1SXueming Li 			/* Kernel ver >= 5.0 or OFED ver >= 4.6 */
11359df97f1SXueming Li 			port_info_out->name_type =
11459df97f1SXueming Li 					MLX5_PHYS_PORT_NAME_TYPE_PFVF;
11579aa4307SOphir Munk 			return;
11679aa4307SOphir Munk 		}
11759df97f1SXueming Li 		if (vf_c1 == 's' && vf_c2 == 'f') {
11859df97f1SXueming Li 			/* Kernel ver >= 5.11 or OFED ver >= 5.1 */
11959df97f1SXueming Li 			port_info_out->name_type =
12059df97f1SXueming Li 					MLX5_PHYS_PORT_NAME_TYPE_PFSF;
12159df97f1SXueming Li 			return;
12259df97f1SXueming Li 		}
12359df97f1SXueming Li 	}
12479aa4307SOphir Munk 	/*
12579aa4307SOphir Munk 	 * Check for port-name as a string of the form p0
12679aa4307SOphir Munk 	 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
12779aa4307SOphir Munk 	 */
1283590881bSViacheslav Ovsiienko 	sc_items = sscanf(port_name_in, "%c%d%c",
1293590881bSViacheslav Ovsiienko 			  &pf_c1, &port_info_out->port_name, &eol);
13079aa4307SOphir Munk 	if (sc_items == 2 && pf_c1 == 'p') {
13179aa4307SOphir Munk 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
13279aa4307SOphir Munk 		return;
13379aa4307SOphir Munk 	}
134420bbdaeSViacheslav Ovsiienko 	/*
135420bbdaeSViacheslav Ovsiienko 	 * Check for port-name as a string of the form pf0
136420bbdaeSViacheslav Ovsiienko 	 * (support kernel ver >= 5.7 for HPF representor on BF).
137420bbdaeSViacheslav Ovsiienko 	 */
1383590881bSViacheslav Ovsiienko 	sc_items = sscanf(port_name_in, "%c%c%d%c",
1393590881bSViacheslav Ovsiienko 			  &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
140420bbdaeSViacheslav Ovsiienko 	if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
141420bbdaeSViacheslav Ovsiienko 		port_info_out->port_name = -1;
142420bbdaeSViacheslav Ovsiienko 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
143420bbdaeSViacheslav Ovsiienko 		return;
144420bbdaeSViacheslav Ovsiienko 	}
14579aa4307SOphir Munk 	/* Check for port-name as a number (support kernel ver < 5.0 */
14679aa4307SOphir Munk 	errno = 0;
14779aa4307SOphir Munk 	port_info_out->port_name = strtol(port_name_in, &end, 0);
14879aa4307SOphir Munk 	if (!errno &&
14979aa4307SOphir Munk 	    (size_t)(end - port_name_in) == strlen(port_name_in)) {
15079aa4307SOphir Munk 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
15179aa4307SOphir Munk 		return;
15279aa4307SOphir Munk 	}
15379aa4307SOphir Munk 	port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
15479aa4307SOphir Munk }
15579aa4307SOphir Munk 
156aec086c9SMatan Azrad int
157aec086c9SMatan Azrad mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
158aec086c9SMatan Azrad {
159aec086c9SMatan Azrad 	DIR *dir;
160aec086c9SMatan Azrad 	struct dirent *dent;
161aec086c9SMatan Azrad 	unsigned int dev_type = 0;
162aec086c9SMatan Azrad 	unsigned int dev_port_prev = ~0u;
163aec086c9SMatan Azrad 	char match[IF_NAMESIZE] = "";
164aec086c9SMatan Azrad 
165aec086c9SMatan Azrad 	MLX5_ASSERT(ibdev_path);
166aec086c9SMatan Azrad 	{
167aec086c9SMatan Azrad 		MKSTR(path, "%s/device/net", ibdev_path);
168aec086c9SMatan Azrad 
169aec086c9SMatan Azrad 		dir = opendir(path);
170aec086c9SMatan Azrad 		if (dir == NULL) {
171aec086c9SMatan Azrad 			rte_errno = errno;
172aec086c9SMatan Azrad 			return -rte_errno;
173aec086c9SMatan Azrad 		}
174aec086c9SMatan Azrad 	}
175aec086c9SMatan Azrad 	while ((dent = readdir(dir)) != NULL) {
176aec086c9SMatan Azrad 		char *name = dent->d_name;
177aec086c9SMatan Azrad 		FILE *file;
178aec086c9SMatan Azrad 		unsigned int dev_port;
179aec086c9SMatan Azrad 		int r;
180aec086c9SMatan Azrad 
181aec086c9SMatan Azrad 		if ((name[0] == '.') &&
182aec086c9SMatan Azrad 		    ((name[1] == '\0') ||
183aec086c9SMatan Azrad 		     ((name[1] == '.') && (name[2] == '\0'))))
184aec086c9SMatan Azrad 			continue;
185aec086c9SMatan Azrad 
186aec086c9SMatan Azrad 		MKSTR(path, "%s/device/net/%s/%s",
187aec086c9SMatan Azrad 		      ibdev_path, name,
188aec086c9SMatan Azrad 		      (dev_type ? "dev_id" : "dev_port"));
189aec086c9SMatan Azrad 
190aec086c9SMatan Azrad 		file = fopen(path, "rb");
191aec086c9SMatan Azrad 		if (file == NULL) {
192aec086c9SMatan Azrad 			if (errno != ENOENT)
193aec086c9SMatan Azrad 				continue;
194aec086c9SMatan Azrad 			/*
195aec086c9SMatan Azrad 			 * Switch to dev_id when dev_port does not exist as
196aec086c9SMatan Azrad 			 * is the case with Linux kernel versions < 3.15.
197aec086c9SMatan Azrad 			 */
198aec086c9SMatan Azrad try_dev_id:
199aec086c9SMatan Azrad 			match[0] = '\0';
200aec086c9SMatan Azrad 			if (dev_type)
201aec086c9SMatan Azrad 				break;
202aec086c9SMatan Azrad 			dev_type = 1;
203aec086c9SMatan Azrad 			dev_port_prev = ~0u;
204aec086c9SMatan Azrad 			rewinddir(dir);
205aec086c9SMatan Azrad 			continue;
206aec086c9SMatan Azrad 		}
207aec086c9SMatan Azrad 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
208aec086c9SMatan Azrad 		fclose(file);
209aec086c9SMatan Azrad 		if (r != 1)
210aec086c9SMatan Azrad 			continue;
211aec086c9SMatan Azrad 		/*
212aec086c9SMatan Azrad 		 * Switch to dev_id when dev_port returns the same value for
213aec086c9SMatan Azrad 		 * all ports. May happen when using a MOFED release older than
214aec086c9SMatan Azrad 		 * 3.0 with a Linux kernel >= 3.15.
215aec086c9SMatan Azrad 		 */
216aec086c9SMatan Azrad 		if (dev_port == dev_port_prev)
217aec086c9SMatan Azrad 			goto try_dev_id;
218aec086c9SMatan Azrad 		dev_port_prev = dev_port;
219aec086c9SMatan Azrad 		if (dev_port == 0)
220aec086c9SMatan Azrad 			strlcpy(match, name, IF_NAMESIZE);
221aec086c9SMatan Azrad 	}
222aec086c9SMatan Azrad 	closedir(dir);
223aec086c9SMatan Azrad 	if (match[0] == '\0') {
224aec086c9SMatan Azrad 		rte_errno = ENOENT;
225aec086c9SMatan Azrad 		return -rte_errno;
226aec086c9SMatan Azrad 	}
227aec086c9SMatan Azrad 	strncpy(ifname, match, IF_NAMESIZE);
228aec086c9SMatan Azrad 	return 0;
229aec086c9SMatan Azrad }
230aec086c9SMatan Azrad 
23179aa4307SOphir Munk #ifdef MLX5_GLUE
23279aa4307SOphir Munk 
23379aa4307SOphir Munk /**
23479aa4307SOphir Munk  * Suffix RTE_EAL_PMD_PATH with "-glue".
23579aa4307SOphir Munk  *
23679aa4307SOphir Munk  * This function performs a sanity check on RTE_EAL_PMD_PATH before
23779aa4307SOphir Munk  * suffixing its last component.
23879aa4307SOphir Munk  *
23979aa4307SOphir Munk  * @param buf[out]
24079aa4307SOphir Munk  *   Output buffer, should be large enough otherwise NULL is returned.
24179aa4307SOphir Munk  * @param size
24279aa4307SOphir Munk  *   Size of @p out.
24379aa4307SOphir Munk  *
24479aa4307SOphir Munk  * @return
24579aa4307SOphir Munk  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
24679aa4307SOphir Munk  */
24779aa4307SOphir Munk static char *
24879aa4307SOphir Munk mlx5_glue_path(char *buf, size_t size)
24979aa4307SOphir Munk {
25079aa4307SOphir Munk 	static const char *const bad[] = { "/", ".", "..", NULL };
25179aa4307SOphir Munk 	const char *path = RTE_EAL_PMD_PATH;
25279aa4307SOphir Munk 	size_t len = strlen(path);
25379aa4307SOphir Munk 	size_t off;
25479aa4307SOphir Munk 	int i;
25579aa4307SOphir Munk 
25679aa4307SOphir Munk 	while (len && path[len - 1] == '/')
25779aa4307SOphir Munk 		--len;
25879aa4307SOphir Munk 	for (off = len; off && path[off - 1] != '/'; --off)
25979aa4307SOphir Munk 		;
26079aa4307SOphir Munk 	for (i = 0; bad[i]; ++i)
26179aa4307SOphir Munk 		if (!strncmp(path + off, bad[i], (int)(len - off)))
26279aa4307SOphir Munk 			goto error;
26379aa4307SOphir Munk 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
26479aa4307SOphir Munk 	if (i == -1 || (size_t)i >= size)
26579aa4307SOphir Munk 		goto error;
26679aa4307SOphir Munk 	return buf;
26779aa4307SOphir Munk error:
26879aa4307SOphir Munk 	RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
26979aa4307SOphir Munk 		" RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
27079aa4307SOphir Munk 		" re-configure DPDK");
27179aa4307SOphir Munk 	return NULL;
27279aa4307SOphir Munk }
27379aa4307SOphir Munk 
27479aa4307SOphir Munk static int
27579aa4307SOphir Munk mlx5_glue_dlopen(void)
27679aa4307SOphir Munk {
27779aa4307SOphir Munk 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
27879aa4307SOphir Munk 	void *handle = NULL;
27979aa4307SOphir Munk 
28079aa4307SOphir Munk 	char const *path[] = {
28179aa4307SOphir Munk 		/*
28279aa4307SOphir Munk 		 * A basic security check is necessary before trusting
28379aa4307SOphir Munk 		 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
28479aa4307SOphir Munk 		 */
28579aa4307SOphir Munk 		(geteuid() == getuid() && getegid() == getgid() ?
28679aa4307SOphir Munk 		 getenv("MLX5_GLUE_PATH") : NULL),
28779aa4307SOphir Munk 		/*
28879aa4307SOphir Munk 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
28979aa4307SOphir Munk 		 * variant, otherwise let dlopen() look up libraries on its
29079aa4307SOphir Munk 		 * own.
29179aa4307SOphir Munk 		 */
29279aa4307SOphir Munk 		(*RTE_EAL_PMD_PATH ?
29379aa4307SOphir Munk 		 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
29479aa4307SOphir Munk 	};
29579aa4307SOphir Munk 	unsigned int i = 0;
29679aa4307SOphir Munk 	void **sym;
29779aa4307SOphir Munk 	const char *dlmsg;
29879aa4307SOphir Munk 
29979aa4307SOphir Munk 	while (!handle && i != RTE_DIM(path)) {
30079aa4307SOphir Munk 		const char *end;
30179aa4307SOphir Munk 		size_t len;
30279aa4307SOphir Munk 		int ret;
30379aa4307SOphir Munk 
30479aa4307SOphir Munk 		if (!path[i]) {
30579aa4307SOphir Munk 			++i;
30679aa4307SOphir Munk 			continue;
30779aa4307SOphir Munk 		}
30879aa4307SOphir Munk 		end = strpbrk(path[i], ":;");
30979aa4307SOphir Munk 		if (!end)
31079aa4307SOphir Munk 			end = path[i] + strlen(path[i]);
31179aa4307SOphir Munk 		len = end - path[i];
31279aa4307SOphir Munk 		ret = 0;
31379aa4307SOphir Munk 		do {
31479aa4307SOphir Munk 			char name[ret + 1];
31579aa4307SOphir Munk 
31679aa4307SOphir Munk 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
31779aa4307SOphir Munk 				       (int)len, path[i],
31879aa4307SOphir Munk 				       (!len || *(end - 1) == '/') ? "" : "/");
31979aa4307SOphir Munk 			if (ret == -1)
32079aa4307SOphir Munk 				break;
32179aa4307SOphir Munk 			if (sizeof(name) != (size_t)ret + 1)
32279aa4307SOphir Munk 				continue;
32379aa4307SOphir Munk 			DRV_LOG(DEBUG, "Looking for rdma-core glue as "
32479aa4307SOphir Munk 				"\"%s\"", name);
32579aa4307SOphir Munk 			handle = dlopen(name, RTLD_LAZY);
32679aa4307SOphir Munk 			break;
32779aa4307SOphir Munk 		} while (1);
32879aa4307SOphir Munk 		path[i] = end + 1;
32979aa4307SOphir Munk 		if (!*end)
33079aa4307SOphir Munk 			++i;
33179aa4307SOphir Munk 	}
33279aa4307SOphir Munk 	if (!handle) {
33379aa4307SOphir Munk 		rte_errno = EINVAL;
33479aa4307SOphir Munk 		dlmsg = dlerror();
33579aa4307SOphir Munk 		if (dlmsg)
33679aa4307SOphir Munk 			DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
33779aa4307SOphir Munk 		goto glue_error;
33879aa4307SOphir Munk 	}
33979aa4307SOphir Munk 	sym = dlsym(handle, "mlx5_glue");
34079aa4307SOphir Munk 	if (!sym || !*sym) {
34179aa4307SOphir Munk 		rte_errno = EINVAL;
34279aa4307SOphir Munk 		dlmsg = dlerror();
34379aa4307SOphir Munk 		if (dlmsg)
34479aa4307SOphir Munk 			DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
34579aa4307SOphir Munk 		goto glue_error;
34679aa4307SOphir Munk 	}
34779aa4307SOphir Munk 	mlx5_glue = *sym;
34879aa4307SOphir Munk 	return 0;
34979aa4307SOphir Munk 
35079aa4307SOphir Munk glue_error:
35179aa4307SOphir Munk 	if (handle)
35279aa4307SOphir Munk 		dlclose(handle);
35379aa4307SOphir Munk 	return -1;
35479aa4307SOphir Munk }
35579aa4307SOphir Munk 
35679aa4307SOphir Munk #endif
35779aa4307SOphir Munk 
35879aa4307SOphir Munk /**
35979aa4307SOphir Munk  * Initialization routine for run-time dependency on rdma-core.
36079aa4307SOphir Munk  */
36179aa4307SOphir Munk void
36279aa4307SOphir Munk mlx5_glue_constructor(void)
36379aa4307SOphir Munk {
36479aa4307SOphir Munk 	/*
36579aa4307SOphir Munk 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
36679aa4307SOphir Munk 	 * huge pages. Calling ibv_fork_init() during init allows
36779aa4307SOphir Munk 	 * applications to use fork() safely for purposes other than
36879aa4307SOphir Munk 	 * using this PMD, which is not supported in forked processes.
36979aa4307SOphir Munk 	 */
37079aa4307SOphir Munk 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
37179aa4307SOphir Munk 	/* Match the size of Rx completion entry to the size of a cacheline. */
37279aa4307SOphir Munk 	if (RTE_CACHE_LINE_SIZE == 128)
37379aa4307SOphir Munk 		setenv("MLX5_CQE_SIZE", "128", 0);
37479aa4307SOphir Munk 	/*
37579aa4307SOphir Munk 	 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
37679aa4307SOphir Munk 	 * cleanup all the Verbs resources even when the device was removed.
37779aa4307SOphir Munk 	 */
37879aa4307SOphir Munk 	setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
37979aa4307SOphir Munk 
38079aa4307SOphir Munk #ifdef MLX5_GLUE
38179aa4307SOphir Munk 	if (mlx5_glue_dlopen() != 0)
38279aa4307SOphir Munk 		goto glue_error;
38379aa4307SOphir Munk #endif
38479aa4307SOphir Munk 
38579aa4307SOphir Munk #ifdef RTE_LIBRTE_MLX5_DEBUG
38679aa4307SOphir Munk 	/* Glue structure must not contain any NULL pointers. */
38779aa4307SOphir Munk 	{
38879aa4307SOphir Munk 		unsigned int i;
38979aa4307SOphir Munk 
39079aa4307SOphir Munk 		for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
39179aa4307SOphir Munk 			MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
39279aa4307SOphir Munk 	}
39379aa4307SOphir Munk #endif
39479aa4307SOphir Munk 	if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
39579aa4307SOphir Munk 		rte_errno = EINVAL;
39679aa4307SOphir Munk 		DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
39779aa4307SOphir Munk 			"required", mlx5_glue->version, MLX5_GLUE_VERSION);
39879aa4307SOphir Munk 		goto glue_error;
39979aa4307SOphir Munk 	}
40079aa4307SOphir Munk 	mlx5_glue->fork_init();
40179aa4307SOphir Munk 	return;
40279aa4307SOphir Munk 
40379aa4307SOphir Munk glue_error:
40479aa4307SOphir Munk 	DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
40579aa4307SOphir Munk 		" run-time dependency on rdma-core libraries (libibverbs,"
40679aa4307SOphir Munk 		" libmlx5)");
40779aa4307SOphir Munk 	mlx5_glue = NULL;
40879aa4307SOphir Munk }
409262c7ad0SOri Kam 
410e35ccf24SMichael Baum /**
411*9d936f4fSMichael Baum  * Validate user arguments for remote PD and CTX.
412*9d936f4fSMichael Baum  *
413*9d936f4fSMichael Baum  * @param config
414*9d936f4fSMichael Baum  *   Pointer to device configuration structure.
415*9d936f4fSMichael Baum  *
416*9d936f4fSMichael Baum  * @return
417*9d936f4fSMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
418*9d936f4fSMichael Baum  */
419*9d936f4fSMichael Baum int
420*9d936f4fSMichael Baum mlx5_os_remote_pd_and_ctx_validate(struct mlx5_common_dev_config *config)
421*9d936f4fSMichael Baum {
422*9d936f4fSMichael Baum 	int device_fd = config->device_fd;
423*9d936f4fSMichael Baum 	int pd_handle = config->pd_handle;
424*9d936f4fSMichael Baum 
425*9d936f4fSMichael Baum #ifdef HAVE_MLX5_IBV_IMPORT_CTX_PD_AND_MR
426*9d936f4fSMichael Baum 	if (device_fd == MLX5_ARG_UNSET && pd_handle != MLX5_ARG_UNSET) {
427*9d936f4fSMichael Baum 		DRV_LOG(ERR, "Remote PD without CTX is not supported.");
428*9d936f4fSMichael Baum 		rte_errno = EINVAL;
429*9d936f4fSMichael Baum 		return -rte_errno;
430*9d936f4fSMichael Baum 	}
431*9d936f4fSMichael Baum 	if (device_fd != MLX5_ARG_UNSET && pd_handle == MLX5_ARG_UNSET) {
432*9d936f4fSMichael Baum 		DRV_LOG(ERR, "Remote CTX without PD is not supported.");
433*9d936f4fSMichael Baum 		rte_errno = EINVAL;
434*9d936f4fSMichael Baum 		return -rte_errno;
435*9d936f4fSMichael Baum 	}
436*9d936f4fSMichael Baum 	DRV_LOG(DEBUG, "Remote PD and CTX is supported: (cmd_fd=%d, "
437*9d936f4fSMichael Baum 		"pd_handle=%d).", device_fd, pd_handle);
438*9d936f4fSMichael Baum #else
439*9d936f4fSMichael Baum 	if (pd_handle != MLX5_ARG_UNSET || device_fd != MLX5_ARG_UNSET) {
440*9d936f4fSMichael Baum 		DRV_LOG(ERR,
441*9d936f4fSMichael Baum 			"Remote PD and CTX is not supported - maybe old rdma-core version?");
442*9d936f4fSMichael Baum 		rte_errno = ENOTSUP;
443*9d936f4fSMichael Baum 		return -rte_errno;
444*9d936f4fSMichael Baum 	}
445*9d936f4fSMichael Baum #endif
446*9d936f4fSMichael Baum 	return 0;
447*9d936f4fSMichael Baum }
448*9d936f4fSMichael Baum 
449*9d936f4fSMichael Baum /**
450*9d936f4fSMichael Baum  * Release Protection Domain object.
451*9d936f4fSMichael Baum  *
452*9d936f4fSMichael Baum  * @param[out] cdev
453*9d936f4fSMichael Baum  *   Pointer to the mlx5 device.
454*9d936f4fSMichael Baum  *
455*9d936f4fSMichael Baum  * @return
456*9d936f4fSMichael Baum  *   0 on success, a negative errno value otherwise.
457*9d936f4fSMichael Baum  */
458*9d936f4fSMichael Baum int
459*9d936f4fSMichael Baum mlx5_os_pd_release(struct mlx5_common_device *cdev)
460*9d936f4fSMichael Baum {
461*9d936f4fSMichael Baum 	if (cdev->config.pd_handle == MLX5_ARG_UNSET)
462*9d936f4fSMichael Baum 		return mlx5_glue->dealloc_pd(cdev->pd);
463*9d936f4fSMichael Baum 	else
464*9d936f4fSMichael Baum 		return mlx5_glue->unimport_pd(cdev->pd);
465*9d936f4fSMichael Baum }
466*9d936f4fSMichael Baum 
467*9d936f4fSMichael Baum /**
468*9d936f4fSMichael Baum  * Allocate Protection Domain object.
469*9d936f4fSMichael Baum  *
470*9d936f4fSMichael Baum  * @param[out] cdev
471*9d936f4fSMichael Baum  *   Pointer to the mlx5 device.
472*9d936f4fSMichael Baum  *
473*9d936f4fSMichael Baum  * @return
474*9d936f4fSMichael Baum  *   0 on success, a negative errno value otherwise.
475*9d936f4fSMichael Baum  */
476*9d936f4fSMichael Baum static int
477*9d936f4fSMichael Baum mlx5_os_pd_create(struct mlx5_common_device *cdev)
478*9d936f4fSMichael Baum {
479*9d936f4fSMichael Baum 	cdev->pd = mlx5_glue->alloc_pd(cdev->ctx);
480*9d936f4fSMichael Baum 	if (cdev->pd == NULL) {
481*9d936f4fSMichael Baum 		DRV_LOG(ERR, "Failed to allocate PD: %s", rte_strerror(errno));
482*9d936f4fSMichael Baum 		return errno ? -errno : -ENOMEM;
483*9d936f4fSMichael Baum 	}
484*9d936f4fSMichael Baum 	return 0;
485*9d936f4fSMichael Baum }
486*9d936f4fSMichael Baum 
487*9d936f4fSMichael Baum /**
488*9d936f4fSMichael Baum  * Import Protection Domain object according to given PD handle.
489*9d936f4fSMichael Baum  *
490*9d936f4fSMichael Baum  * @param[out] cdev
491*9d936f4fSMichael Baum  *   Pointer to the mlx5 device.
492*9d936f4fSMichael Baum  *
493*9d936f4fSMichael Baum  * @return
494*9d936f4fSMichael Baum  *   0 on success, a negative errno value otherwise.
495*9d936f4fSMichael Baum  */
496*9d936f4fSMichael Baum static int
497*9d936f4fSMichael Baum mlx5_os_pd_import(struct mlx5_common_device *cdev)
498*9d936f4fSMichael Baum {
499*9d936f4fSMichael Baum 	cdev->pd = mlx5_glue->import_pd(cdev->ctx, cdev->config.pd_handle);
500*9d936f4fSMichael Baum 	if (cdev->pd == NULL) {
501*9d936f4fSMichael Baum 		DRV_LOG(ERR, "Failed to import PD using handle=%d: %s",
502*9d936f4fSMichael Baum 			cdev->config.pd_handle, rte_strerror(errno));
503*9d936f4fSMichael Baum 		return errno ? -errno : -ENOMEM;
504*9d936f4fSMichael Baum 	}
505*9d936f4fSMichael Baum 	return 0;
506*9d936f4fSMichael Baum }
507*9d936f4fSMichael Baum 
508*9d936f4fSMichael Baum /**
509*9d936f4fSMichael Baum  * Prepare Protection Domain object and extract its pdn using DV API.
510e35ccf24SMichael Baum  *
511e35ccf24SMichael Baum  * @param[out] cdev
512e35ccf24SMichael Baum  *   Pointer to the mlx5 device.
513e35ccf24SMichael Baum  *
514e35ccf24SMichael Baum  * @return
515e35ccf24SMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
516e35ccf24SMichael Baum  */
517e35ccf24SMichael Baum int
518*9d936f4fSMichael Baum mlx5_os_pd_prepare(struct mlx5_common_device *cdev)
519e35ccf24SMichael Baum {
520e35ccf24SMichael Baum #ifdef HAVE_IBV_FLOW_DV_SUPPORT
521e35ccf24SMichael Baum 	struct mlx5dv_obj obj;
522e35ccf24SMichael Baum 	struct mlx5dv_pd pd_info;
523e35ccf24SMichael Baum #endif
524*9d936f4fSMichael Baum 	int ret;
525e35ccf24SMichael Baum 
526*9d936f4fSMichael Baum 	if (cdev->config.pd_handle == MLX5_ARG_UNSET)
527*9d936f4fSMichael Baum 		ret = mlx5_os_pd_create(cdev);
528*9d936f4fSMichael Baum 	else
529*9d936f4fSMichael Baum 		ret = mlx5_os_pd_import(cdev);
530*9d936f4fSMichael Baum 	if (ret) {
531*9d936f4fSMichael Baum 		rte_errno = -ret;
532*9d936f4fSMichael Baum 		return ret;
533e35ccf24SMichael Baum 	}
534e35ccf24SMichael Baum 	if (cdev->config.devx == 0)
535e35ccf24SMichael Baum 		return 0;
536e35ccf24SMichael Baum #ifdef HAVE_IBV_FLOW_DV_SUPPORT
537e35ccf24SMichael Baum 	obj.pd.in = cdev->pd;
538e35ccf24SMichael Baum 	obj.pd.out = &pd_info;
539e35ccf24SMichael Baum 	ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
540e35ccf24SMichael Baum 	if (ret != 0) {
541e35ccf24SMichael Baum 		DRV_LOG(ERR, "Fail to get PD object info.");
542*9d936f4fSMichael Baum 		rte_errno = errno;
543*9d936f4fSMichael Baum 		claim_zero(mlx5_os_pd_release(cdev));
544e35ccf24SMichael Baum 		cdev->pd = NULL;
545*9d936f4fSMichael Baum 		return -rte_errno;
546e35ccf24SMichael Baum 	}
547e35ccf24SMichael Baum 	cdev->pdn = pd_info.pdn;
548e35ccf24SMichael Baum 	return 0;
549e35ccf24SMichael Baum #else
550e35ccf24SMichael Baum 	DRV_LOG(ERR, "Cannot get pdn - no DV support.");
551*9d936f4fSMichael Baum 	rte_errno = ENOTSUP;
552*9d936f4fSMichael Baum 	return -rte_errno;
553e35ccf24SMichael Baum #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
554e35ccf24SMichael Baum }
555e35ccf24SMichael Baum 
556662d0dc6SMichael Baum static struct ibv_device *
557ad435d32SXueming Li mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
558c31f3f7fSShiri Kuzin {
559c31f3f7fSShiri Kuzin 	int n;
560c31f3f7fSShiri Kuzin 	struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
561c31f3f7fSShiri Kuzin 	struct ibv_device *ibv_match = NULL;
562c31f3f7fSShiri Kuzin 
563c31f3f7fSShiri Kuzin 	if (ibv_list == NULL) {
564c31f3f7fSShiri Kuzin 		rte_errno = ENOSYS;
565c31f3f7fSShiri Kuzin 		return NULL;
566c31f3f7fSShiri Kuzin 	}
567c31f3f7fSShiri Kuzin 	while (n-- > 0) {
568c31f3f7fSShiri Kuzin 		struct rte_pci_addr paddr;
569c31f3f7fSShiri Kuzin 
570c31f3f7fSShiri Kuzin 		DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
5714d567938SThomas Monjalon 		if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
572c31f3f7fSShiri Kuzin 			continue;
573c31f3f7fSShiri Kuzin 		if (rte_pci_addr_cmp(addr, &paddr) != 0)
574c31f3f7fSShiri Kuzin 			continue;
575c31f3f7fSShiri Kuzin 		ibv_match = ibv_list[n];
576c31f3f7fSShiri Kuzin 		break;
577c31f3f7fSShiri Kuzin 	}
578ca1418ceSMichael Baum 	if (ibv_match == NULL) {
579ca1418ceSMichael Baum 		DRV_LOG(WARNING,
580ca1418ceSMichael Baum 			"No Verbs device matches PCI device " PCI_PRI_FMT ","
581ca1418ceSMichael Baum 			" are kernel drivers loaded?",
582ca1418ceSMichael Baum 			addr->domain, addr->bus, addr->devid, addr->function);
583c31f3f7fSShiri Kuzin 		rte_errno = ENOENT;
584ca1418ceSMichael Baum 	}
585c31f3f7fSShiri Kuzin 	mlx5_glue->free_device_list(ibv_list);
586c31f3f7fSShiri Kuzin 	return ibv_match;
587c31f3f7fSShiri Kuzin }
588887183efSMichael Baum 
589662d0dc6SMichael Baum /* Try to disable ROCE by Netlink\Devlink. */
590662d0dc6SMichael Baum static int
591662d0dc6SMichael Baum mlx5_nl_roce_disable(const char *addr)
592662d0dc6SMichael Baum {
593662d0dc6SMichael Baum 	int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
594662d0dc6SMichael Baum 	int devlink_id;
595662d0dc6SMichael Baum 	int enable;
596662d0dc6SMichael Baum 	int ret;
597662d0dc6SMichael Baum 
598662d0dc6SMichael Baum 	if (nlsk_fd < 0)
599662d0dc6SMichael Baum 		return nlsk_fd;
600662d0dc6SMichael Baum 	devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
601662d0dc6SMichael Baum 	if (devlink_id < 0) {
602662d0dc6SMichael Baum 		ret = devlink_id;
603662d0dc6SMichael Baum 		DRV_LOG(DEBUG,
604662d0dc6SMichael Baum 			"Failed to get devlink id for ROCE operations by Netlink.");
605662d0dc6SMichael Baum 		goto close;
606662d0dc6SMichael Baum 	}
607662d0dc6SMichael Baum 	ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
608662d0dc6SMichael Baum 	if (ret) {
609662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
610662d0dc6SMichael Baum 			ret);
611662d0dc6SMichael Baum 		goto close;
612662d0dc6SMichael Baum 	} else if (!enable) {
613662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
614662d0dc6SMichael Baum 		goto close;
615662d0dc6SMichael Baum 	}
616662d0dc6SMichael Baum 	ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
617662d0dc6SMichael Baum 	if (ret)
618662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
619662d0dc6SMichael Baum 	else
620662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
621662d0dc6SMichael Baum close:
622662d0dc6SMichael Baum 	close(nlsk_fd);
623662d0dc6SMichael Baum 	return ret;
624662d0dc6SMichael Baum }
625662d0dc6SMichael Baum 
626662d0dc6SMichael Baum /* Try to disable ROCE by sysfs. */
627662d0dc6SMichael Baum static int
628662d0dc6SMichael Baum mlx5_sys_roce_disable(const char *addr)
629662d0dc6SMichael Baum {
630662d0dc6SMichael Baum 	FILE *file_o;
631662d0dc6SMichael Baum 	int enable;
632662d0dc6SMichael Baum 	int ret;
633662d0dc6SMichael Baum 
634662d0dc6SMichael Baum 	MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
635662d0dc6SMichael Baum 	file_o = fopen(file_p, "rb");
636662d0dc6SMichael Baum 	if (!file_o) {
637662d0dc6SMichael Baum 		rte_errno = ENOTSUP;
638662d0dc6SMichael Baum 		return -ENOTSUP;
639662d0dc6SMichael Baum 	}
640662d0dc6SMichael Baum 	ret = fscanf(file_o, "%d", &enable);
641662d0dc6SMichael Baum 	if (ret != 1) {
642662d0dc6SMichael Baum 		rte_errno = EINVAL;
643662d0dc6SMichael Baum 		ret = EINVAL;
644662d0dc6SMichael Baum 		goto close;
645662d0dc6SMichael Baum 	} else if (!enable) {
646662d0dc6SMichael Baum 		ret = 0;
647662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
648662d0dc6SMichael Baum 		goto close;
649662d0dc6SMichael Baum 	}
650662d0dc6SMichael Baum 	fclose(file_o);
651662d0dc6SMichael Baum 	file_o = fopen(file_p, "wb");
652662d0dc6SMichael Baum 	if (!file_o) {
653662d0dc6SMichael Baum 		rte_errno = ENOTSUP;
654662d0dc6SMichael Baum 		return -ENOTSUP;
655662d0dc6SMichael Baum 	}
656662d0dc6SMichael Baum 	fprintf(file_o, "0\n");
657662d0dc6SMichael Baum 	ret = 0;
658662d0dc6SMichael Baum close:
659662d0dc6SMichael Baum 	if (ret)
660662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
661662d0dc6SMichael Baum 	else
662662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
663662d0dc6SMichael Baum 	fclose(file_o);
664662d0dc6SMichael Baum 	return ret;
665662d0dc6SMichael Baum }
666662d0dc6SMichael Baum 
667662d0dc6SMichael Baum static int
668662d0dc6SMichael Baum mlx5_roce_disable(const struct rte_device *dev)
669662d0dc6SMichael Baum {
670662d0dc6SMichael Baum 	char pci_addr[PCI_PRI_STR_SIZE] = { 0 };
671662d0dc6SMichael Baum 
672662d0dc6SMichael Baum 	if (mlx5_dev_to_pci_str(dev, pci_addr, sizeof(pci_addr)) < 0)
673662d0dc6SMichael Baum 		return -rte_errno;
674662d0dc6SMichael Baum 	/* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
675662d0dc6SMichael Baum 	if (mlx5_nl_roce_disable(pci_addr) != 0 &&
676662d0dc6SMichael Baum 	    mlx5_sys_roce_disable(pci_addr) != 0)
677662d0dc6SMichael Baum 		return -rte_errno;
678662d0dc6SMichael Baum 	return 0;
679662d0dc6SMichael Baum }
680662d0dc6SMichael Baum 
681662d0dc6SMichael Baum static struct ibv_device *
682662d0dc6SMichael Baum mlx5_os_get_ibv_dev(const struct rte_device *dev)
683662d0dc6SMichael Baum {
684662d0dc6SMichael Baum 	struct ibv_device *ibv;
685662d0dc6SMichael Baum 
686662d0dc6SMichael Baum 	if (mlx5_dev_is_pci(dev))
687662d0dc6SMichael Baum 		ibv = mlx5_os_get_ibv_device(&RTE_DEV_TO_PCI_CONST(dev)->addr);
688662d0dc6SMichael Baum 	else
689662d0dc6SMichael Baum 		ibv = mlx5_get_aux_ibv_device(RTE_DEV_TO_AUXILIARY_CONST(dev));
690662d0dc6SMichael Baum 	if (ibv == NULL) {
691662d0dc6SMichael Baum 		rte_errno = ENODEV;
692662d0dc6SMichael Baum 		DRV_LOG(ERR, "Verbs device not found: %s", dev->name);
693662d0dc6SMichael Baum 	}
694662d0dc6SMichael Baum 	return ibv;
695662d0dc6SMichael Baum }
696662d0dc6SMichael Baum 
697662d0dc6SMichael Baum static struct ibv_device *
698662d0dc6SMichael Baum mlx5_vdpa_get_ibv_dev(const struct rte_device *dev)
699662d0dc6SMichael Baum {
700662d0dc6SMichael Baum 	struct ibv_device *ibv;
701662d0dc6SMichael Baum 	int retry;
702662d0dc6SMichael Baum 
703662d0dc6SMichael Baum 	if (mlx5_roce_disable(dev) != 0) {
704662d0dc6SMichael Baum 		DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
705662d0dc6SMichael Baum 			dev->name);
706662d0dc6SMichael Baum 		return NULL;
707662d0dc6SMichael Baum 	}
708662d0dc6SMichael Baum 	/* Wait for the IB device to appear again after reload. */
709662d0dc6SMichael Baum 	for (retry = MLX5_VDPA_MAX_RETRIES; retry > 0; --retry) {
710662d0dc6SMichael Baum 		ibv = mlx5_os_get_ibv_dev(dev);
711662d0dc6SMichael Baum 		if (ibv != NULL)
712662d0dc6SMichael Baum 			return ibv;
713662d0dc6SMichael Baum 		usleep(MLX5_VDPA_USEC);
714662d0dc6SMichael Baum 	}
715662d0dc6SMichael Baum 	DRV_LOG(ERR,
716662d0dc6SMichael Baum 		"Cannot get IB device after disabling RoCE for \"%s\", retries exceed %d.",
717662d0dc6SMichael Baum 		dev->name, MLX5_VDPA_MAX_RETRIES);
718662d0dc6SMichael Baum 	rte_errno = EAGAIN;
719662d0dc6SMichael Baum 	return NULL;
720662d0dc6SMichael Baum }
721662d0dc6SMichael Baum 
722887183efSMichael Baum static int
723887183efSMichael Baum mlx5_config_doorbell_mapping_env(int dbnc)
724887183efSMichael Baum {
725887183efSMichael Baum 	char *env;
726887183efSMichael Baum 	int value;
727887183efSMichael Baum 
728887183efSMichael Baum 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
729887183efSMichael Baum 	/* Get environment variable to store. */
730887183efSMichael Baum 	env = getenv(MLX5_SHUT_UP_BF);
731887183efSMichael Baum 	value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
732887183efSMichael Baum 	if (dbnc == MLX5_ARG_UNSET)
733887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
734887183efSMichael Baum 	else
735887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF,
736a6b9d5a5SMichael Baum 		       dbnc == MLX5_SQ_DB_NCACHED ? "1" : "0", 1);
737887183efSMichael Baum 	return value;
738887183efSMichael Baum }
739887183efSMichael Baum 
740887183efSMichael Baum static void
741887183efSMichael Baum mlx5_restore_doorbell_mapping_env(int value)
742887183efSMichael Baum {
743887183efSMichael Baum 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
744887183efSMichael Baum 	/* Restore the original environment variable state. */
745887183efSMichael Baum 	if (value == MLX5_ARG_UNSET)
746887183efSMichael Baum 		unsetenv(MLX5_SHUT_UP_BF);
747887183efSMichael Baum 	else
748887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
749887183efSMichael Baum }
750887183efSMichael Baum 
751887183efSMichael Baum /**
752887183efSMichael Baum  * Function API to open IB device.
753887183efSMichael Baum  *
754887183efSMichael Baum  * @param cdev
755887183efSMichael Baum  *   Pointer to the mlx5 device.
756ca1418ceSMichael Baum  * @param classes
757ca1418ceSMichael Baum  *   Chosen classes come from device arguments.
758887183efSMichael Baum  *
759887183efSMichael Baum  * @return
760*9d936f4fSMichael Baum  *   Pointer to ibv_context on success, NULL otherwise and rte_errno is set.
761887183efSMichael Baum  */
762*9d936f4fSMichael Baum static struct ibv_context *
763*9d936f4fSMichael Baum mlx5_open_device(struct mlx5_common_device *cdev, uint32_t classes)
764887183efSMichael Baum {
765887183efSMichael Baum 	struct ibv_device *ibv;
766887183efSMichael Baum 	struct ibv_context *ctx = NULL;
767887183efSMichael Baum 	int dbmap_env;
768887183efSMichael Baum 
769*9d936f4fSMichael Baum 	MLX5_ASSERT(cdev->config.device_fd == MLX5_ARG_UNSET);
770662d0dc6SMichael Baum 	if (classes & MLX5_CLASS_VDPA)
771662d0dc6SMichael Baum 		ibv = mlx5_vdpa_get_ibv_dev(cdev->dev);
772662d0dc6SMichael Baum 	else
773887183efSMichael Baum 		ibv = mlx5_os_get_ibv_dev(cdev->dev);
774887183efSMichael Baum 	if (!ibv)
775*9d936f4fSMichael Baum 		return NULL;
776887183efSMichael Baum 	DRV_LOG(INFO, "Dev information matches for device \"%s\".", ibv->name);
777887183efSMichael Baum 	/*
778887183efSMichael Baum 	 * Configure environment variable "MLX5_BF_SHUT_UP" before the device
779887183efSMichael Baum 	 * creation. The rdma_core library checks the variable at device
780887183efSMichael Baum 	 * creation and stores the result internally.
781887183efSMichael Baum 	 */
782887183efSMichael Baum 	dbmap_env = mlx5_config_doorbell_mapping_env(cdev->config.dbnc);
783887183efSMichael Baum 	/* Try to open IB device with DV first, then usual Verbs. */
784887183efSMichael Baum 	errno = 0;
785887183efSMichael Baum 	ctx = mlx5_glue->dv_open_device(ibv);
786887183efSMichael Baum 	if (ctx) {
787887183efSMichael Baum 		cdev->config.devx = 1;
788ca1418ceSMichael Baum 	} else if (classes == MLX5_CLASS_ETH) {
789887183efSMichael Baum 		/* The environment variable is still configured. */
790887183efSMichael Baum 		ctx = mlx5_glue->open_device(ibv);
791887183efSMichael Baum 		if (ctx == NULL)
792887183efSMichael Baum 			goto error;
793ca1418ceSMichael Baum 	} else {
794ca1418ceSMichael Baum 		goto error;
795887183efSMichael Baum 	}
796887183efSMichael Baum 	/* The device is created, no need for environment. */
797887183efSMichael Baum 	mlx5_restore_doorbell_mapping_env(dbmap_env);
798*9d936f4fSMichael Baum 	return ctx;
799887183efSMichael Baum error:
800887183efSMichael Baum 	rte_errno = errno ? errno : ENODEV;
801887183efSMichael Baum 	/* The device creation is failed, no need for environment. */
802887183efSMichael Baum 	mlx5_restore_doorbell_mapping_env(dbmap_env);
803887183efSMichael Baum 	DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
804*9d936f4fSMichael Baum 	return NULL;
805887183efSMichael Baum }
806*9d936f4fSMichael Baum 
807*9d936f4fSMichael Baum /**
808*9d936f4fSMichael Baum  * Function API to import IB device.
809*9d936f4fSMichael Baum  *
810*9d936f4fSMichael Baum  * @param cdev
811*9d936f4fSMichael Baum  *   Pointer to the mlx5 device.
812*9d936f4fSMichael Baum  *
813*9d936f4fSMichael Baum  * @return
814*9d936f4fSMichael Baum  *   Pointer to ibv_context on success, NULL otherwise and rte_errno is set.
815*9d936f4fSMichael Baum  */
816*9d936f4fSMichael Baum static struct ibv_context *
817*9d936f4fSMichael Baum mlx5_import_device(struct mlx5_common_device *cdev)
818*9d936f4fSMichael Baum {
819*9d936f4fSMichael Baum 	struct ibv_context *ctx = NULL;
820*9d936f4fSMichael Baum 
821*9d936f4fSMichael Baum 	MLX5_ASSERT(cdev->config.device_fd != MLX5_ARG_UNSET);
822*9d936f4fSMichael Baum 	ctx = mlx5_glue->import_device(cdev->config.device_fd);
823*9d936f4fSMichael Baum 	if (!ctx) {
824*9d936f4fSMichael Baum 		DRV_LOG(ERR, "Failed to import device for fd=%d: %s",
825*9d936f4fSMichael Baum 			cdev->config.device_fd, rte_strerror(errno));
826*9d936f4fSMichael Baum 		rte_errno = errno;
827*9d936f4fSMichael Baum 	}
828*9d936f4fSMichael Baum 	return ctx;
829*9d936f4fSMichael Baum }
830*9d936f4fSMichael Baum 
831*9d936f4fSMichael Baum /**
832*9d936f4fSMichael Baum  * Function API to prepare IB device.
833*9d936f4fSMichael Baum  *
834*9d936f4fSMichael Baum  * @param cdev
835*9d936f4fSMichael Baum  *   Pointer to the mlx5 device.
836*9d936f4fSMichael Baum  * @param classes
837*9d936f4fSMichael Baum  *   Chosen classes come from device arguments.
838*9d936f4fSMichael Baum  *
839*9d936f4fSMichael Baum  * @return
840*9d936f4fSMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
841*9d936f4fSMichael Baum  */
842*9d936f4fSMichael Baum int
843*9d936f4fSMichael Baum mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes)
844*9d936f4fSMichael Baum {
845*9d936f4fSMichael Baum 
846*9d936f4fSMichael Baum 	struct ibv_context *ctx = NULL;
847*9d936f4fSMichael Baum 
848*9d936f4fSMichael Baum 	if (cdev->config.device_fd == MLX5_ARG_UNSET)
849*9d936f4fSMichael Baum 		ctx = mlx5_open_device(cdev, classes);
850*9d936f4fSMichael Baum 	else
851*9d936f4fSMichael Baum 		ctx = mlx5_import_device(cdev);
852*9d936f4fSMichael Baum 	if (ctx == NULL)
853*9d936f4fSMichael Baum 		return -rte_errno;
854*9d936f4fSMichael Baum 	/* Hint libmlx5 to use PMD allocator for data plane resources */
855*9d936f4fSMichael Baum 	mlx5_set_context_attr(cdev->dev, ctx);
856*9d936f4fSMichael Baum 	cdev->ctx = ctx;
857*9d936f4fSMichael Baum 	return 0;
858*9d936f4fSMichael Baum }
859*9d936f4fSMichael Baum 
8604c74ad3eSRongwei Liu int
8614c74ad3eSRongwei Liu mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len)
8624c74ad3eSRongwei Liu {
8634c74ad3eSRongwei Liu 	char tmp[512];
8644c74ad3eSRongwei Liu 	char cur_ifname[IF_NAMESIZE + 1];
8654c74ad3eSRongwei Liu 	FILE *id_file;
8664c74ad3eSRongwei Liu 	DIR *dir;
8674c74ad3eSRongwei Liu 	struct dirent *ptr;
8684c74ad3eSRongwei Liu 	int ret;
8694c74ad3eSRongwei Liu 
8704c74ad3eSRongwei Liu 	if (guid == NULL || len < sizeof(u_int64_t) + 1)
8714c74ad3eSRongwei Liu 		return -1;
8724c74ad3eSRongwei Liu 	memset(guid, 0, len);
8734c74ad3eSRongwei Liu 	snprintf(tmp, sizeof(tmp), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/net",
8744c74ad3eSRongwei Liu 			dev->domain, dev->bus, dev->devid, dev->function);
8754c74ad3eSRongwei Liu 	dir = opendir(tmp);
8764c74ad3eSRongwei Liu 	if (dir == NULL)
8774c74ad3eSRongwei Liu 		return -1;
8784c74ad3eSRongwei Liu 	/* Traverse to identify PF interface */
8794c74ad3eSRongwei Liu 	do {
8804c74ad3eSRongwei Liu 		ptr = readdir(dir);
8814c74ad3eSRongwei Liu 		if (ptr == NULL || ptr->d_type != DT_DIR) {
8824c74ad3eSRongwei Liu 			closedir(dir);
8834c74ad3eSRongwei Liu 			return -1;
8844c74ad3eSRongwei Liu 		}
8854c74ad3eSRongwei Liu 	} while (strchr(ptr->d_name, '.') || strchr(ptr->d_name, '_') ||
8864c74ad3eSRongwei Liu 		 strchr(ptr->d_name, 'v'));
8874c74ad3eSRongwei Liu 	snprintf(cur_ifname, sizeof(cur_ifname), "%s", ptr->d_name);
8884c74ad3eSRongwei Liu 	closedir(dir);
8894c74ad3eSRongwei Liu 	snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
8904c74ad3eSRongwei Liu 			"/%s/phys_switch_id", cur_ifname);
8914c74ad3eSRongwei Liu 	/* Older OFED like 5.3 doesn't support read */
8924c74ad3eSRongwei Liu 	id_file = fopen(tmp, "r");
8934c74ad3eSRongwei Liu 	if (!id_file)
8944c74ad3eSRongwei Liu 		return 0;
8954c74ad3eSRongwei Liu 	ret = fscanf(id_file, "%16s", guid);
8964c74ad3eSRongwei Liu 	fclose(id_file);
8974c74ad3eSRongwei Liu 	return ret;
8984c74ad3eSRongwei Liu }
89976b5bdf8SMatan Azrad 
90076b5bdf8SMatan Azrad /*
90176b5bdf8SMatan Azrad  * Create direct mkey using the kernel ibv_reg_mr API and wrap it with a new
90276b5bdf8SMatan Azrad  * indirect mkey created by the DevX API.
90376b5bdf8SMatan Azrad  * This mkey should be used for DevX commands requesting mkey as a parameter.
90476b5bdf8SMatan Azrad  */
90576b5bdf8SMatan Azrad int
90676b5bdf8SMatan Azrad mlx5_os_wrapped_mkey_create(void *ctx, void *pd, uint32_t pdn, void *addr,
90776b5bdf8SMatan Azrad 			    size_t length, struct mlx5_pmd_wrapped_mr *pmd_mr)
90876b5bdf8SMatan Azrad {
90976b5bdf8SMatan Azrad 	struct mlx5_klm klm = {
91076b5bdf8SMatan Azrad 		.byte_count = length,
91176b5bdf8SMatan Azrad 		.address = (uintptr_t)addr,
91276b5bdf8SMatan Azrad 	};
91376b5bdf8SMatan Azrad 	struct mlx5_devx_mkey_attr mkey_attr = {
91476b5bdf8SMatan Azrad 		.pd = pdn,
91576b5bdf8SMatan Azrad 		.klm_array = &klm,
91676b5bdf8SMatan Azrad 		.klm_num = 1,
91776b5bdf8SMatan Azrad 	};
91876b5bdf8SMatan Azrad 	struct mlx5_devx_obj *mkey;
91976b5bdf8SMatan Azrad 	struct ibv_mr *ibv_mr = mlx5_glue->reg_mr(pd, addr, length,
92076b5bdf8SMatan Azrad 						  IBV_ACCESS_LOCAL_WRITE |
92176b5bdf8SMatan Azrad 						  (haswell_broadwell_cpu ? 0 :
92276b5bdf8SMatan Azrad 						  IBV_ACCESS_RELAXED_ORDERING));
92376b5bdf8SMatan Azrad 
92476b5bdf8SMatan Azrad 	if (!ibv_mr) {
92576b5bdf8SMatan Azrad 		rte_errno = errno;
92676b5bdf8SMatan Azrad 		return -rte_errno;
92776b5bdf8SMatan Azrad 	}
92876b5bdf8SMatan Azrad 	klm.mkey = ibv_mr->lkey;
92976b5bdf8SMatan Azrad 	mkey_attr.addr = (uintptr_t)addr;
93076b5bdf8SMatan Azrad 	mkey_attr.size = length;
93176b5bdf8SMatan Azrad 	mkey = mlx5_devx_cmd_mkey_create(ctx, &mkey_attr);
93276b5bdf8SMatan Azrad 	if (!mkey) {
93376b5bdf8SMatan Azrad 		claim_zero(mlx5_glue->dereg_mr(ibv_mr));
93476b5bdf8SMatan Azrad 		return -rte_errno;
93576b5bdf8SMatan Azrad 	}
93676b5bdf8SMatan Azrad 	pmd_mr->addr = addr;
93776b5bdf8SMatan Azrad 	pmd_mr->len = length;
93876b5bdf8SMatan Azrad 	pmd_mr->obj = (void *)ibv_mr;
93976b5bdf8SMatan Azrad 	pmd_mr->imkey = mkey;
94076b5bdf8SMatan Azrad 	pmd_mr->lkey = mkey->id;
94176b5bdf8SMatan Azrad 	return 0;
94276b5bdf8SMatan Azrad }
94376b5bdf8SMatan Azrad 
94476b5bdf8SMatan Azrad void
94576b5bdf8SMatan Azrad mlx5_os_wrapped_mkey_destroy(struct mlx5_pmd_wrapped_mr *pmd_mr)
94676b5bdf8SMatan Azrad {
94776b5bdf8SMatan Azrad 	if (!pmd_mr)
94876b5bdf8SMatan Azrad 		return;
94976b5bdf8SMatan Azrad 	if (pmd_mr->imkey)
95076b5bdf8SMatan Azrad 		claim_zero(mlx5_devx_cmd_destroy(pmd_mr->imkey));
95176b5bdf8SMatan Azrad 	if (pmd_mr->obj)
95276b5bdf8SMatan Azrad 		claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj));
95376b5bdf8SMatan Azrad 	memset(pmd_mr, 0, sizeof(*pmd_mr));
95476b5bdf8SMatan Azrad }
955