xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.c (revision 4c74ad3e16d41c0b61e3b4217f2ffc9a70a1d0d1)
179aa4307SOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
279aa4307SOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
379aa4307SOphir Munk  */
479aa4307SOphir Munk 
5*4c74ad3eSRongwei 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 /**
411e35ccf24SMichael Baum  * Allocate Protection Domain object and extract its pdn using DV API.
412e35ccf24SMichael Baum  *
413e35ccf24SMichael Baum  * @param[out] cdev
414e35ccf24SMichael Baum  *   Pointer to the mlx5 device.
415e35ccf24SMichael Baum  *
416e35ccf24SMichael Baum  * @return
417e35ccf24SMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
418e35ccf24SMichael Baum  */
419e35ccf24SMichael Baum int
420e35ccf24SMichael Baum mlx5_os_pd_create(struct mlx5_common_device *cdev)
421e35ccf24SMichael Baum {
422e35ccf24SMichael Baum #ifdef HAVE_IBV_FLOW_DV_SUPPORT
423e35ccf24SMichael Baum 	struct mlx5dv_obj obj;
424e35ccf24SMichael Baum 	struct mlx5dv_pd pd_info;
425e35ccf24SMichael Baum 	int ret;
426e35ccf24SMichael Baum #endif
427e35ccf24SMichael Baum 
428e35ccf24SMichael Baum 	cdev->pd = mlx5_glue->alloc_pd(cdev->ctx);
429e35ccf24SMichael Baum 	if (cdev->pd == NULL) {
430e35ccf24SMichael Baum 		DRV_LOG(ERR, "Failed to allocate PD.");
431e35ccf24SMichael Baum 		return errno ? -errno : -ENOMEM;
432e35ccf24SMichael Baum 	}
433e35ccf24SMichael Baum 	if (cdev->config.devx == 0)
434e35ccf24SMichael Baum 		return 0;
435e35ccf24SMichael Baum #ifdef HAVE_IBV_FLOW_DV_SUPPORT
436e35ccf24SMichael Baum 	obj.pd.in = cdev->pd;
437e35ccf24SMichael Baum 	obj.pd.out = &pd_info;
438e35ccf24SMichael Baum 	ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
439e35ccf24SMichael Baum 	if (ret != 0) {
440e35ccf24SMichael Baum 		DRV_LOG(ERR, "Fail to get PD object info.");
441e35ccf24SMichael Baum 		mlx5_glue->dealloc_pd(cdev->pd);
442e35ccf24SMichael Baum 		cdev->pd = NULL;
443e35ccf24SMichael Baum 		return -errno;
444e35ccf24SMichael Baum 	}
445e35ccf24SMichael Baum 	cdev->pdn = pd_info.pdn;
446e35ccf24SMichael Baum 	return 0;
447e35ccf24SMichael Baum #else
448e35ccf24SMichael Baum 	DRV_LOG(ERR, "Cannot get pdn - no DV support.");
449e35ccf24SMichael Baum 	return -ENOTSUP;
450e35ccf24SMichael Baum #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
451e35ccf24SMichael Baum }
452e35ccf24SMichael Baum 
453662d0dc6SMichael Baum static struct ibv_device *
454ad435d32SXueming Li mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
455c31f3f7fSShiri Kuzin {
456c31f3f7fSShiri Kuzin 	int n;
457c31f3f7fSShiri Kuzin 	struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
458c31f3f7fSShiri Kuzin 	struct ibv_device *ibv_match = NULL;
459c31f3f7fSShiri Kuzin 
460c31f3f7fSShiri Kuzin 	if (ibv_list == NULL) {
461c31f3f7fSShiri Kuzin 		rte_errno = ENOSYS;
462c31f3f7fSShiri Kuzin 		return NULL;
463c31f3f7fSShiri Kuzin 	}
464c31f3f7fSShiri Kuzin 	while (n-- > 0) {
465c31f3f7fSShiri Kuzin 		struct rte_pci_addr paddr;
466c31f3f7fSShiri Kuzin 
467c31f3f7fSShiri Kuzin 		DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
4684d567938SThomas Monjalon 		if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
469c31f3f7fSShiri Kuzin 			continue;
470c31f3f7fSShiri Kuzin 		if (rte_pci_addr_cmp(addr, &paddr) != 0)
471c31f3f7fSShiri Kuzin 			continue;
472c31f3f7fSShiri Kuzin 		ibv_match = ibv_list[n];
473c31f3f7fSShiri Kuzin 		break;
474c31f3f7fSShiri Kuzin 	}
475ca1418ceSMichael Baum 	if (ibv_match == NULL) {
476ca1418ceSMichael Baum 		DRV_LOG(WARNING,
477ca1418ceSMichael Baum 			"No Verbs device matches PCI device " PCI_PRI_FMT ","
478ca1418ceSMichael Baum 			" are kernel drivers loaded?",
479ca1418ceSMichael Baum 			addr->domain, addr->bus, addr->devid, addr->function);
480c31f3f7fSShiri Kuzin 		rte_errno = ENOENT;
481ca1418ceSMichael Baum 	}
482c31f3f7fSShiri Kuzin 	mlx5_glue->free_device_list(ibv_list);
483c31f3f7fSShiri Kuzin 	return ibv_match;
484c31f3f7fSShiri Kuzin }
485887183efSMichael Baum 
486662d0dc6SMichael Baum /* Try to disable ROCE by Netlink\Devlink. */
487662d0dc6SMichael Baum static int
488662d0dc6SMichael Baum mlx5_nl_roce_disable(const char *addr)
489662d0dc6SMichael Baum {
490662d0dc6SMichael Baum 	int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
491662d0dc6SMichael Baum 	int devlink_id;
492662d0dc6SMichael Baum 	int enable;
493662d0dc6SMichael Baum 	int ret;
494662d0dc6SMichael Baum 
495662d0dc6SMichael Baum 	if (nlsk_fd < 0)
496662d0dc6SMichael Baum 		return nlsk_fd;
497662d0dc6SMichael Baum 	devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
498662d0dc6SMichael Baum 	if (devlink_id < 0) {
499662d0dc6SMichael Baum 		ret = devlink_id;
500662d0dc6SMichael Baum 		DRV_LOG(DEBUG,
501662d0dc6SMichael Baum 			"Failed to get devlink id for ROCE operations by Netlink.");
502662d0dc6SMichael Baum 		goto close;
503662d0dc6SMichael Baum 	}
504662d0dc6SMichael Baum 	ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
505662d0dc6SMichael Baum 	if (ret) {
506662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
507662d0dc6SMichael Baum 			ret);
508662d0dc6SMichael Baum 		goto close;
509662d0dc6SMichael Baum 	} else if (!enable) {
510662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
511662d0dc6SMichael Baum 		goto close;
512662d0dc6SMichael Baum 	}
513662d0dc6SMichael Baum 	ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
514662d0dc6SMichael Baum 	if (ret)
515662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
516662d0dc6SMichael Baum 	else
517662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
518662d0dc6SMichael Baum close:
519662d0dc6SMichael Baum 	close(nlsk_fd);
520662d0dc6SMichael Baum 	return ret;
521662d0dc6SMichael Baum }
522662d0dc6SMichael Baum 
523662d0dc6SMichael Baum /* Try to disable ROCE by sysfs. */
524662d0dc6SMichael Baum static int
525662d0dc6SMichael Baum mlx5_sys_roce_disable(const char *addr)
526662d0dc6SMichael Baum {
527662d0dc6SMichael Baum 	FILE *file_o;
528662d0dc6SMichael Baum 	int enable;
529662d0dc6SMichael Baum 	int ret;
530662d0dc6SMichael Baum 
531662d0dc6SMichael Baum 	MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
532662d0dc6SMichael Baum 	file_o = fopen(file_p, "rb");
533662d0dc6SMichael Baum 	if (!file_o) {
534662d0dc6SMichael Baum 		rte_errno = ENOTSUP;
535662d0dc6SMichael Baum 		return -ENOTSUP;
536662d0dc6SMichael Baum 	}
537662d0dc6SMichael Baum 	ret = fscanf(file_o, "%d", &enable);
538662d0dc6SMichael Baum 	if (ret != 1) {
539662d0dc6SMichael Baum 		rte_errno = EINVAL;
540662d0dc6SMichael Baum 		ret = EINVAL;
541662d0dc6SMichael Baum 		goto close;
542662d0dc6SMichael Baum 	} else if (!enable) {
543662d0dc6SMichael Baum 		ret = 0;
544662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
545662d0dc6SMichael Baum 		goto close;
546662d0dc6SMichael Baum 	}
547662d0dc6SMichael Baum 	fclose(file_o);
548662d0dc6SMichael Baum 	file_o = fopen(file_p, "wb");
549662d0dc6SMichael Baum 	if (!file_o) {
550662d0dc6SMichael Baum 		rte_errno = ENOTSUP;
551662d0dc6SMichael Baum 		return -ENOTSUP;
552662d0dc6SMichael Baum 	}
553662d0dc6SMichael Baum 	fprintf(file_o, "0\n");
554662d0dc6SMichael Baum 	ret = 0;
555662d0dc6SMichael Baum close:
556662d0dc6SMichael Baum 	if (ret)
557662d0dc6SMichael Baum 		DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
558662d0dc6SMichael Baum 	else
559662d0dc6SMichael Baum 		DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
560662d0dc6SMichael Baum 	fclose(file_o);
561662d0dc6SMichael Baum 	return ret;
562662d0dc6SMichael Baum }
563662d0dc6SMichael Baum 
564662d0dc6SMichael Baum static int
565662d0dc6SMichael Baum mlx5_roce_disable(const struct rte_device *dev)
566662d0dc6SMichael Baum {
567662d0dc6SMichael Baum 	char pci_addr[PCI_PRI_STR_SIZE] = { 0 };
568662d0dc6SMichael Baum 
569662d0dc6SMichael Baum 	if (mlx5_dev_to_pci_str(dev, pci_addr, sizeof(pci_addr)) < 0)
570662d0dc6SMichael Baum 		return -rte_errno;
571662d0dc6SMichael Baum 	/* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
572662d0dc6SMichael Baum 	if (mlx5_nl_roce_disable(pci_addr) != 0 &&
573662d0dc6SMichael Baum 	    mlx5_sys_roce_disable(pci_addr) != 0)
574662d0dc6SMichael Baum 		return -rte_errno;
575662d0dc6SMichael Baum 	return 0;
576662d0dc6SMichael Baum }
577662d0dc6SMichael Baum 
578662d0dc6SMichael Baum static struct ibv_device *
579662d0dc6SMichael Baum mlx5_os_get_ibv_dev(const struct rte_device *dev)
580662d0dc6SMichael Baum {
581662d0dc6SMichael Baum 	struct ibv_device *ibv;
582662d0dc6SMichael Baum 
583662d0dc6SMichael Baum 	if (mlx5_dev_is_pci(dev))
584662d0dc6SMichael Baum 		ibv = mlx5_os_get_ibv_device(&RTE_DEV_TO_PCI_CONST(dev)->addr);
585662d0dc6SMichael Baum 	else
586662d0dc6SMichael Baum 		ibv = mlx5_get_aux_ibv_device(RTE_DEV_TO_AUXILIARY_CONST(dev));
587662d0dc6SMichael Baum 	if (ibv == NULL) {
588662d0dc6SMichael Baum 		rte_errno = ENODEV;
589662d0dc6SMichael Baum 		DRV_LOG(ERR, "Verbs device not found: %s", dev->name);
590662d0dc6SMichael Baum 	}
591662d0dc6SMichael Baum 	return ibv;
592662d0dc6SMichael Baum }
593662d0dc6SMichael Baum 
594662d0dc6SMichael Baum static struct ibv_device *
595662d0dc6SMichael Baum mlx5_vdpa_get_ibv_dev(const struct rte_device *dev)
596662d0dc6SMichael Baum {
597662d0dc6SMichael Baum 	struct ibv_device *ibv;
598662d0dc6SMichael Baum 	int retry;
599662d0dc6SMichael Baum 
600662d0dc6SMichael Baum 	if (mlx5_roce_disable(dev) != 0) {
601662d0dc6SMichael Baum 		DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
602662d0dc6SMichael Baum 			dev->name);
603662d0dc6SMichael Baum 		return NULL;
604662d0dc6SMichael Baum 	}
605662d0dc6SMichael Baum 	/* Wait for the IB device to appear again after reload. */
606662d0dc6SMichael Baum 	for (retry = MLX5_VDPA_MAX_RETRIES; retry > 0; --retry) {
607662d0dc6SMichael Baum 		ibv = mlx5_os_get_ibv_dev(dev);
608662d0dc6SMichael Baum 		if (ibv != NULL)
609662d0dc6SMichael Baum 			return ibv;
610662d0dc6SMichael Baum 		usleep(MLX5_VDPA_USEC);
611662d0dc6SMichael Baum 	}
612662d0dc6SMichael Baum 	DRV_LOG(ERR,
613662d0dc6SMichael Baum 		"Cannot get IB device after disabling RoCE for \"%s\", retries exceed %d.",
614662d0dc6SMichael Baum 		dev->name, MLX5_VDPA_MAX_RETRIES);
615662d0dc6SMichael Baum 	rte_errno = EAGAIN;
616662d0dc6SMichael Baum 	return NULL;
617662d0dc6SMichael Baum }
618662d0dc6SMichael Baum 
619887183efSMichael Baum static int
620887183efSMichael Baum mlx5_config_doorbell_mapping_env(int dbnc)
621887183efSMichael Baum {
622887183efSMichael Baum 	char *env;
623887183efSMichael Baum 	int value;
624887183efSMichael Baum 
625887183efSMichael Baum 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
626887183efSMichael Baum 	/* Get environment variable to store. */
627887183efSMichael Baum 	env = getenv(MLX5_SHUT_UP_BF);
628887183efSMichael Baum 	value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
629887183efSMichael Baum 	if (dbnc == MLX5_ARG_UNSET)
630887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
631887183efSMichael Baum 	else
632887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF,
633887183efSMichael Baum 		       dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
634887183efSMichael Baum 	return value;
635887183efSMichael Baum }
636887183efSMichael Baum 
637887183efSMichael Baum static void
638887183efSMichael Baum mlx5_restore_doorbell_mapping_env(int value)
639887183efSMichael Baum {
640887183efSMichael Baum 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
641887183efSMichael Baum 	/* Restore the original environment variable state. */
642887183efSMichael Baum 	if (value == MLX5_ARG_UNSET)
643887183efSMichael Baum 		unsetenv(MLX5_SHUT_UP_BF);
644887183efSMichael Baum 	else
645887183efSMichael Baum 		setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
646887183efSMichael Baum }
647887183efSMichael Baum 
648887183efSMichael Baum /**
649887183efSMichael Baum  * Function API to open IB device.
650887183efSMichael Baum  *
651887183efSMichael Baum  *
652887183efSMichael Baum  * @param cdev
653887183efSMichael Baum  *   Pointer to the mlx5 device.
654ca1418ceSMichael Baum  * @param classes
655ca1418ceSMichael Baum  *   Chosen classes come from device arguments.
656887183efSMichael Baum  *
657887183efSMichael Baum  * @return
658887183efSMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
659887183efSMichael Baum  */
660887183efSMichael Baum int
661ca1418ceSMichael Baum mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes)
662887183efSMichael Baum {
663887183efSMichael Baum 	struct ibv_device *ibv;
664887183efSMichael Baum 	struct ibv_context *ctx = NULL;
665887183efSMichael Baum 	int dbmap_env;
666887183efSMichael Baum 
667662d0dc6SMichael Baum 	if (classes & MLX5_CLASS_VDPA)
668662d0dc6SMichael Baum 		ibv = mlx5_vdpa_get_ibv_dev(cdev->dev);
669662d0dc6SMichael Baum 	else
670887183efSMichael Baum 		ibv = mlx5_os_get_ibv_dev(cdev->dev);
671887183efSMichael Baum 	if (!ibv)
672887183efSMichael Baum 		return -rte_errno;
673887183efSMichael Baum 	DRV_LOG(INFO, "Dev information matches for device \"%s\".", ibv->name);
674887183efSMichael Baum 	/*
675887183efSMichael Baum 	 * Configure environment variable "MLX5_BF_SHUT_UP" before the device
676887183efSMichael Baum 	 * creation. The rdma_core library checks the variable at device
677887183efSMichael Baum 	 * creation and stores the result internally.
678887183efSMichael Baum 	 */
679887183efSMichael Baum 	dbmap_env = mlx5_config_doorbell_mapping_env(cdev->config.dbnc);
680887183efSMichael Baum 	/* Try to open IB device with DV first, then usual Verbs. */
681887183efSMichael Baum 	errno = 0;
682887183efSMichael Baum 	ctx = mlx5_glue->dv_open_device(ibv);
683887183efSMichael Baum 	if (ctx) {
684887183efSMichael Baum 		cdev->config.devx = 1;
685887183efSMichael Baum 		DRV_LOG(DEBUG, "DevX is supported.");
686ca1418ceSMichael Baum 	} else if (classes == MLX5_CLASS_ETH) {
687887183efSMichael Baum 		/* The environment variable is still configured. */
688887183efSMichael Baum 		ctx = mlx5_glue->open_device(ibv);
689887183efSMichael Baum 		if (ctx == NULL)
690887183efSMichael Baum 			goto error;
691887183efSMichael Baum 		DRV_LOG(DEBUG, "DevX is NOT supported.");
692ca1418ceSMichael Baum 	} else {
693ca1418ceSMichael Baum 		goto error;
694887183efSMichael Baum 	}
695887183efSMichael Baum 	/* The device is created, no need for environment. */
696887183efSMichael Baum 	mlx5_restore_doorbell_mapping_env(dbmap_env);
697887183efSMichael Baum 	/* Hint libmlx5 to use PMD allocator for data plane resources */
698887183efSMichael Baum 	mlx5_set_context_attr(cdev->dev, ctx);
699ca1418ceSMichael Baum 	cdev->ctx = ctx;
700887183efSMichael Baum 	return 0;
701887183efSMichael Baum error:
702887183efSMichael Baum 	rte_errno = errno ? errno : ENODEV;
703887183efSMichael Baum 	/* The device creation is failed, no need for environment. */
704887183efSMichael Baum 	mlx5_restore_doorbell_mapping_env(dbmap_env);
705887183efSMichael Baum 	DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
706887183efSMichael Baum 	return -rte_errno;
707887183efSMichael Baum }
708*4c74ad3eSRongwei Liu int
709*4c74ad3eSRongwei Liu mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len)
710*4c74ad3eSRongwei Liu {
711*4c74ad3eSRongwei Liu 	char tmp[512];
712*4c74ad3eSRongwei Liu 	char cur_ifname[IF_NAMESIZE + 1];
713*4c74ad3eSRongwei Liu 	FILE *id_file;
714*4c74ad3eSRongwei Liu 	DIR *dir;
715*4c74ad3eSRongwei Liu 	struct dirent *ptr;
716*4c74ad3eSRongwei Liu 	int ret;
717*4c74ad3eSRongwei Liu 
718*4c74ad3eSRongwei Liu 	if (guid == NULL || len < sizeof(u_int64_t) + 1)
719*4c74ad3eSRongwei Liu 		return -1;
720*4c74ad3eSRongwei Liu 	memset(guid, 0, len);
721*4c74ad3eSRongwei Liu 	snprintf(tmp, sizeof(tmp), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/net",
722*4c74ad3eSRongwei Liu 			dev->domain, dev->bus, dev->devid, dev->function);
723*4c74ad3eSRongwei Liu 	dir = opendir(tmp);
724*4c74ad3eSRongwei Liu 	if (dir == NULL)
725*4c74ad3eSRongwei Liu 		return -1;
726*4c74ad3eSRongwei Liu 	/* Traverse to identify PF interface */
727*4c74ad3eSRongwei Liu 	do {
728*4c74ad3eSRongwei Liu 		ptr = readdir(dir);
729*4c74ad3eSRongwei Liu 		if (ptr == NULL || ptr->d_type != DT_DIR) {
730*4c74ad3eSRongwei Liu 			closedir(dir);
731*4c74ad3eSRongwei Liu 			return -1;
732*4c74ad3eSRongwei Liu 		}
733*4c74ad3eSRongwei Liu 	} while (strchr(ptr->d_name, '.') || strchr(ptr->d_name, '_') ||
734*4c74ad3eSRongwei Liu 		 strchr(ptr->d_name, 'v'));
735*4c74ad3eSRongwei Liu 	snprintf(cur_ifname, sizeof(cur_ifname), "%s", ptr->d_name);
736*4c74ad3eSRongwei Liu 	closedir(dir);
737*4c74ad3eSRongwei Liu 	snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
738*4c74ad3eSRongwei Liu 			"/%s/phys_switch_id", cur_ifname);
739*4c74ad3eSRongwei Liu 	/* Older OFED like 5.3 doesn't support read */
740*4c74ad3eSRongwei Liu 	id_file = fopen(tmp, "r");
741*4c74ad3eSRongwei Liu 	if (!id_file)
742*4c74ad3eSRongwei Liu 		return 0;
743*4c74ad3eSRongwei Liu 	ret = fscanf(id_file, "%16s", guid);
744*4c74ad3eSRongwei Liu 	fclose(id_file);
745*4c74ad3eSRongwei Liu 	return ret;
746*4c74ad3eSRongwei Liu }
747