xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.c (revision 4c74ad3e16d41c0b61e3b4217f2ffc9a70a1d0d1)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdio.h>
9 #ifdef RTE_IBVERBS_LINK_DLOPEN
10 #include <dlfcn.h>
11 #endif
12 #include <dirent.h>
13 #include <net/if.h>
14 
15 #include <rte_errno.h>
16 #include <rte_string_fns.h>
17 #include <rte_bus_pci.h>
18 #include <rte_bus_auxiliary.h>
19 
20 #include "mlx5_common.h"
21 #include "mlx5_nl.h"
22 #include "mlx5_common_log.h"
23 #include "mlx5_common_private.h"
24 #include "mlx5_common_defs.h"
25 #include "mlx5_common_os.h"
26 #include "mlx5_glue.h"
27 
28 #ifdef MLX5_GLUE
29 const struct mlx5_glue *mlx5_glue;
30 #endif
31 
32 int
33 mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
34 {
35 	FILE *file;
36 	char line[32];
37 	int rc = -ENOENT;
38 	MKSTR(path, "%s/device/uevent", dev_path);
39 
40 	file = fopen(path, "rb");
41 	if (file == NULL) {
42 		rte_errno = errno;
43 		return -rte_errno;
44 	}
45 	while (fgets(line, sizeof(line), file) == line) {
46 		size_t len = strlen(line);
47 
48 		/* Truncate long lines. */
49 		if (len == (sizeof(line) - 1)) {
50 			while (line[(len - 1)] != '\n') {
51 				int ret = fgetc(file);
52 
53 				if (ret == EOF)
54 					goto exit;
55 				line[(len - 1)] = ret;
56 			}
57 			/* No match for long lines. */
58 			continue;
59 		}
60 		/* Extract information. */
61 		if (sscanf(line,
62 			   "PCI_SLOT_NAME="
63 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
64 			   &pci_addr->domain,
65 			   &pci_addr->bus,
66 			   &pci_addr->devid,
67 			   &pci_addr->function) == 4) {
68 			rc = 0;
69 			break;
70 		}
71 	}
72 exit:
73 	fclose(file);
74 	if (rc)
75 		rte_errno = -rc;
76 	return rc;
77 }
78 
79 /**
80  * Extract port name, as a number, from sysfs or netlink information.
81  *
82  * @param[in] port_name_in
83  *   String representing the port name.
84  * @param[out] port_info_out
85  *   Port information, including port name as a number and port name
86  *   type if recognized
87  *
88  * @return
89  *   port_name field set according to recognized name format.
90  */
91 void
92 mlx5_translate_port_name(const char *port_name_in,
93 			 struct mlx5_switch_info *port_info_out)
94 {
95 	char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
96 	char *end;
97 	int sc_items;
98 
99 	sc_items = sscanf(port_name_in, "%c%d",
100 			  &ctrl, &port_info_out->ctrl_num);
101 	if (sc_items == 2 && ctrl == 'c') {
102 		port_name_in++; /* 'c' */
103 		port_name_in += snprintf(NULL, 0, "%d",
104 					  port_info_out->ctrl_num);
105 	}
106 	/* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
107 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
108 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
109 			  &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
110 	if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
111 		if (vf_c1 == 'v' && vf_c2 == 'f') {
112 			/* Kernel ver >= 5.0 or OFED ver >= 4.6 */
113 			port_info_out->name_type =
114 					MLX5_PHYS_PORT_NAME_TYPE_PFVF;
115 			return;
116 		}
117 		if (vf_c1 == 's' && vf_c2 == 'f') {
118 			/* Kernel ver >= 5.11 or OFED ver >= 5.1 */
119 			port_info_out->name_type =
120 					MLX5_PHYS_PORT_NAME_TYPE_PFSF;
121 			return;
122 		}
123 	}
124 	/*
125 	 * Check for port-name as a string of the form p0
126 	 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
127 	 */
128 	sc_items = sscanf(port_name_in, "%c%d%c",
129 			  &pf_c1, &port_info_out->port_name, &eol);
130 	if (sc_items == 2 && pf_c1 == 'p') {
131 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
132 		return;
133 	}
134 	/*
135 	 * Check for port-name as a string of the form pf0
136 	 * (support kernel ver >= 5.7 for HPF representor on BF).
137 	 */
138 	sc_items = sscanf(port_name_in, "%c%c%d%c",
139 			  &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
140 	if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
141 		port_info_out->port_name = -1;
142 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
143 		return;
144 	}
145 	/* Check for port-name as a number (support kernel ver < 5.0 */
146 	errno = 0;
147 	port_info_out->port_name = strtol(port_name_in, &end, 0);
148 	if (!errno &&
149 	    (size_t)(end - port_name_in) == strlen(port_name_in)) {
150 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
151 		return;
152 	}
153 	port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
154 }
155 
156 int
157 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
158 {
159 	DIR *dir;
160 	struct dirent *dent;
161 	unsigned int dev_type = 0;
162 	unsigned int dev_port_prev = ~0u;
163 	char match[IF_NAMESIZE] = "";
164 
165 	MLX5_ASSERT(ibdev_path);
166 	{
167 		MKSTR(path, "%s/device/net", ibdev_path);
168 
169 		dir = opendir(path);
170 		if (dir == NULL) {
171 			rte_errno = errno;
172 			return -rte_errno;
173 		}
174 	}
175 	while ((dent = readdir(dir)) != NULL) {
176 		char *name = dent->d_name;
177 		FILE *file;
178 		unsigned int dev_port;
179 		int r;
180 
181 		if ((name[0] == '.') &&
182 		    ((name[1] == '\0') ||
183 		     ((name[1] == '.') && (name[2] == '\0'))))
184 			continue;
185 
186 		MKSTR(path, "%s/device/net/%s/%s",
187 		      ibdev_path, name,
188 		      (dev_type ? "dev_id" : "dev_port"));
189 
190 		file = fopen(path, "rb");
191 		if (file == NULL) {
192 			if (errno != ENOENT)
193 				continue;
194 			/*
195 			 * Switch to dev_id when dev_port does not exist as
196 			 * is the case with Linux kernel versions < 3.15.
197 			 */
198 try_dev_id:
199 			match[0] = '\0';
200 			if (dev_type)
201 				break;
202 			dev_type = 1;
203 			dev_port_prev = ~0u;
204 			rewinddir(dir);
205 			continue;
206 		}
207 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
208 		fclose(file);
209 		if (r != 1)
210 			continue;
211 		/*
212 		 * Switch to dev_id when dev_port returns the same value for
213 		 * all ports. May happen when using a MOFED release older than
214 		 * 3.0 with a Linux kernel >= 3.15.
215 		 */
216 		if (dev_port == dev_port_prev)
217 			goto try_dev_id;
218 		dev_port_prev = dev_port;
219 		if (dev_port == 0)
220 			strlcpy(match, name, IF_NAMESIZE);
221 	}
222 	closedir(dir);
223 	if (match[0] == '\0') {
224 		rte_errno = ENOENT;
225 		return -rte_errno;
226 	}
227 	strncpy(ifname, match, IF_NAMESIZE);
228 	return 0;
229 }
230 
231 #ifdef MLX5_GLUE
232 
233 /**
234  * Suffix RTE_EAL_PMD_PATH with "-glue".
235  *
236  * This function performs a sanity check on RTE_EAL_PMD_PATH before
237  * suffixing its last component.
238  *
239  * @param buf[out]
240  *   Output buffer, should be large enough otherwise NULL is returned.
241  * @param size
242  *   Size of @p out.
243  *
244  * @return
245  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
246  */
247 static char *
248 mlx5_glue_path(char *buf, size_t size)
249 {
250 	static const char *const bad[] = { "/", ".", "..", NULL };
251 	const char *path = RTE_EAL_PMD_PATH;
252 	size_t len = strlen(path);
253 	size_t off;
254 	int i;
255 
256 	while (len && path[len - 1] == '/')
257 		--len;
258 	for (off = len; off && path[off - 1] != '/'; --off)
259 		;
260 	for (i = 0; bad[i]; ++i)
261 		if (!strncmp(path + off, bad[i], (int)(len - off)))
262 			goto error;
263 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
264 	if (i == -1 || (size_t)i >= size)
265 		goto error;
266 	return buf;
267 error:
268 	RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
269 		" RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
270 		" re-configure DPDK");
271 	return NULL;
272 }
273 
274 static int
275 mlx5_glue_dlopen(void)
276 {
277 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
278 	void *handle = NULL;
279 
280 	char const *path[] = {
281 		/*
282 		 * A basic security check is necessary before trusting
283 		 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
284 		 */
285 		(geteuid() == getuid() && getegid() == getgid() ?
286 		 getenv("MLX5_GLUE_PATH") : NULL),
287 		/*
288 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
289 		 * variant, otherwise let dlopen() look up libraries on its
290 		 * own.
291 		 */
292 		(*RTE_EAL_PMD_PATH ?
293 		 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
294 	};
295 	unsigned int i = 0;
296 	void **sym;
297 	const char *dlmsg;
298 
299 	while (!handle && i != RTE_DIM(path)) {
300 		const char *end;
301 		size_t len;
302 		int ret;
303 
304 		if (!path[i]) {
305 			++i;
306 			continue;
307 		}
308 		end = strpbrk(path[i], ":;");
309 		if (!end)
310 			end = path[i] + strlen(path[i]);
311 		len = end - path[i];
312 		ret = 0;
313 		do {
314 			char name[ret + 1];
315 
316 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
317 				       (int)len, path[i],
318 				       (!len || *(end - 1) == '/') ? "" : "/");
319 			if (ret == -1)
320 				break;
321 			if (sizeof(name) != (size_t)ret + 1)
322 				continue;
323 			DRV_LOG(DEBUG, "Looking for rdma-core glue as "
324 				"\"%s\"", name);
325 			handle = dlopen(name, RTLD_LAZY);
326 			break;
327 		} while (1);
328 		path[i] = end + 1;
329 		if (!*end)
330 			++i;
331 	}
332 	if (!handle) {
333 		rte_errno = EINVAL;
334 		dlmsg = dlerror();
335 		if (dlmsg)
336 			DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
337 		goto glue_error;
338 	}
339 	sym = dlsym(handle, "mlx5_glue");
340 	if (!sym || !*sym) {
341 		rte_errno = EINVAL;
342 		dlmsg = dlerror();
343 		if (dlmsg)
344 			DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
345 		goto glue_error;
346 	}
347 	mlx5_glue = *sym;
348 	return 0;
349 
350 glue_error:
351 	if (handle)
352 		dlclose(handle);
353 	return -1;
354 }
355 
356 #endif
357 
358 /**
359  * Initialization routine for run-time dependency on rdma-core.
360  */
361 void
362 mlx5_glue_constructor(void)
363 {
364 	/*
365 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
366 	 * huge pages. Calling ibv_fork_init() during init allows
367 	 * applications to use fork() safely for purposes other than
368 	 * using this PMD, which is not supported in forked processes.
369 	 */
370 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
371 	/* Match the size of Rx completion entry to the size of a cacheline. */
372 	if (RTE_CACHE_LINE_SIZE == 128)
373 		setenv("MLX5_CQE_SIZE", "128", 0);
374 	/*
375 	 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
376 	 * cleanup all the Verbs resources even when the device was removed.
377 	 */
378 	setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
379 
380 #ifdef MLX5_GLUE
381 	if (mlx5_glue_dlopen() != 0)
382 		goto glue_error;
383 #endif
384 
385 #ifdef RTE_LIBRTE_MLX5_DEBUG
386 	/* Glue structure must not contain any NULL pointers. */
387 	{
388 		unsigned int i;
389 
390 		for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
391 			MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
392 	}
393 #endif
394 	if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
395 		rte_errno = EINVAL;
396 		DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
397 			"required", mlx5_glue->version, MLX5_GLUE_VERSION);
398 		goto glue_error;
399 	}
400 	mlx5_glue->fork_init();
401 	return;
402 
403 glue_error:
404 	DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
405 		" run-time dependency on rdma-core libraries (libibverbs,"
406 		" libmlx5)");
407 	mlx5_glue = NULL;
408 }
409 
410 /**
411  * Allocate Protection Domain object and extract its pdn using DV API.
412  *
413  * @param[out] cdev
414  *   Pointer to the mlx5 device.
415  *
416  * @return
417  *   0 on success, a negative errno value otherwise and rte_errno is set.
418  */
419 int
420 mlx5_os_pd_create(struct mlx5_common_device *cdev)
421 {
422 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
423 	struct mlx5dv_obj obj;
424 	struct mlx5dv_pd pd_info;
425 	int ret;
426 #endif
427 
428 	cdev->pd = mlx5_glue->alloc_pd(cdev->ctx);
429 	if (cdev->pd == NULL) {
430 		DRV_LOG(ERR, "Failed to allocate PD.");
431 		return errno ? -errno : -ENOMEM;
432 	}
433 	if (cdev->config.devx == 0)
434 		return 0;
435 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
436 	obj.pd.in = cdev->pd;
437 	obj.pd.out = &pd_info;
438 	ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
439 	if (ret != 0) {
440 		DRV_LOG(ERR, "Fail to get PD object info.");
441 		mlx5_glue->dealloc_pd(cdev->pd);
442 		cdev->pd = NULL;
443 		return -errno;
444 	}
445 	cdev->pdn = pd_info.pdn;
446 	return 0;
447 #else
448 	DRV_LOG(ERR, "Cannot get pdn - no DV support.");
449 	return -ENOTSUP;
450 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
451 }
452 
453 static struct ibv_device *
454 mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
455 {
456 	int n;
457 	struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
458 	struct ibv_device *ibv_match = NULL;
459 
460 	if (ibv_list == NULL) {
461 		rte_errno = ENOSYS;
462 		return NULL;
463 	}
464 	while (n-- > 0) {
465 		struct rte_pci_addr paddr;
466 
467 		DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
468 		if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
469 			continue;
470 		if (rte_pci_addr_cmp(addr, &paddr) != 0)
471 			continue;
472 		ibv_match = ibv_list[n];
473 		break;
474 	}
475 	if (ibv_match == NULL) {
476 		DRV_LOG(WARNING,
477 			"No Verbs device matches PCI device " PCI_PRI_FMT ","
478 			" are kernel drivers loaded?",
479 			addr->domain, addr->bus, addr->devid, addr->function);
480 		rte_errno = ENOENT;
481 	}
482 	mlx5_glue->free_device_list(ibv_list);
483 	return ibv_match;
484 }
485 
486 /* Try to disable ROCE by Netlink\Devlink. */
487 static int
488 mlx5_nl_roce_disable(const char *addr)
489 {
490 	int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
491 	int devlink_id;
492 	int enable;
493 	int ret;
494 
495 	if (nlsk_fd < 0)
496 		return nlsk_fd;
497 	devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
498 	if (devlink_id < 0) {
499 		ret = devlink_id;
500 		DRV_LOG(DEBUG,
501 			"Failed to get devlink id for ROCE operations by Netlink.");
502 		goto close;
503 	}
504 	ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
505 	if (ret) {
506 		DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
507 			ret);
508 		goto close;
509 	} else if (!enable) {
510 		DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
511 		goto close;
512 	}
513 	ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
514 	if (ret)
515 		DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
516 	else
517 		DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
518 close:
519 	close(nlsk_fd);
520 	return ret;
521 }
522 
523 /* Try to disable ROCE by sysfs. */
524 static int
525 mlx5_sys_roce_disable(const char *addr)
526 {
527 	FILE *file_o;
528 	int enable;
529 	int ret;
530 
531 	MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
532 	file_o = fopen(file_p, "rb");
533 	if (!file_o) {
534 		rte_errno = ENOTSUP;
535 		return -ENOTSUP;
536 	}
537 	ret = fscanf(file_o, "%d", &enable);
538 	if (ret != 1) {
539 		rte_errno = EINVAL;
540 		ret = EINVAL;
541 		goto close;
542 	} else if (!enable) {
543 		ret = 0;
544 		DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
545 		goto close;
546 	}
547 	fclose(file_o);
548 	file_o = fopen(file_p, "wb");
549 	if (!file_o) {
550 		rte_errno = ENOTSUP;
551 		return -ENOTSUP;
552 	}
553 	fprintf(file_o, "0\n");
554 	ret = 0;
555 close:
556 	if (ret)
557 		DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
558 	else
559 		DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
560 	fclose(file_o);
561 	return ret;
562 }
563 
564 static int
565 mlx5_roce_disable(const struct rte_device *dev)
566 {
567 	char pci_addr[PCI_PRI_STR_SIZE] = { 0 };
568 
569 	if (mlx5_dev_to_pci_str(dev, pci_addr, sizeof(pci_addr)) < 0)
570 		return -rte_errno;
571 	/* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
572 	if (mlx5_nl_roce_disable(pci_addr) != 0 &&
573 	    mlx5_sys_roce_disable(pci_addr) != 0)
574 		return -rte_errno;
575 	return 0;
576 }
577 
578 static struct ibv_device *
579 mlx5_os_get_ibv_dev(const struct rte_device *dev)
580 {
581 	struct ibv_device *ibv;
582 
583 	if (mlx5_dev_is_pci(dev))
584 		ibv = mlx5_os_get_ibv_device(&RTE_DEV_TO_PCI_CONST(dev)->addr);
585 	else
586 		ibv = mlx5_get_aux_ibv_device(RTE_DEV_TO_AUXILIARY_CONST(dev));
587 	if (ibv == NULL) {
588 		rte_errno = ENODEV;
589 		DRV_LOG(ERR, "Verbs device not found: %s", dev->name);
590 	}
591 	return ibv;
592 }
593 
594 static struct ibv_device *
595 mlx5_vdpa_get_ibv_dev(const struct rte_device *dev)
596 {
597 	struct ibv_device *ibv;
598 	int retry;
599 
600 	if (mlx5_roce_disable(dev) != 0) {
601 		DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
602 			dev->name);
603 		return NULL;
604 	}
605 	/* Wait for the IB device to appear again after reload. */
606 	for (retry = MLX5_VDPA_MAX_RETRIES; retry > 0; --retry) {
607 		ibv = mlx5_os_get_ibv_dev(dev);
608 		if (ibv != NULL)
609 			return ibv;
610 		usleep(MLX5_VDPA_USEC);
611 	}
612 	DRV_LOG(ERR,
613 		"Cannot get IB device after disabling RoCE for \"%s\", retries exceed %d.",
614 		dev->name, MLX5_VDPA_MAX_RETRIES);
615 	rte_errno = EAGAIN;
616 	return NULL;
617 }
618 
619 static int
620 mlx5_config_doorbell_mapping_env(int dbnc)
621 {
622 	char *env;
623 	int value;
624 
625 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
626 	/* Get environment variable to store. */
627 	env = getenv(MLX5_SHUT_UP_BF);
628 	value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
629 	if (dbnc == MLX5_ARG_UNSET)
630 		setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
631 	else
632 		setenv(MLX5_SHUT_UP_BF,
633 		       dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
634 	return value;
635 }
636 
637 static void
638 mlx5_restore_doorbell_mapping_env(int value)
639 {
640 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
641 	/* Restore the original environment variable state. */
642 	if (value == MLX5_ARG_UNSET)
643 		unsetenv(MLX5_SHUT_UP_BF);
644 	else
645 		setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
646 }
647 
648 /**
649  * Function API to open IB device.
650  *
651  *
652  * @param cdev
653  *   Pointer to the mlx5 device.
654  * @param classes
655  *   Chosen classes come from device arguments.
656  *
657  * @return
658  *   0 on success, a negative errno value otherwise and rte_errno is set.
659  */
660 int
661 mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes)
662 {
663 	struct ibv_device *ibv;
664 	struct ibv_context *ctx = NULL;
665 	int dbmap_env;
666 
667 	if (classes & MLX5_CLASS_VDPA)
668 		ibv = mlx5_vdpa_get_ibv_dev(cdev->dev);
669 	else
670 		ibv = mlx5_os_get_ibv_dev(cdev->dev);
671 	if (!ibv)
672 		return -rte_errno;
673 	DRV_LOG(INFO, "Dev information matches for device \"%s\".", ibv->name);
674 	/*
675 	 * Configure environment variable "MLX5_BF_SHUT_UP" before the device
676 	 * creation. The rdma_core library checks the variable at device
677 	 * creation and stores the result internally.
678 	 */
679 	dbmap_env = mlx5_config_doorbell_mapping_env(cdev->config.dbnc);
680 	/* Try to open IB device with DV first, then usual Verbs. */
681 	errno = 0;
682 	ctx = mlx5_glue->dv_open_device(ibv);
683 	if (ctx) {
684 		cdev->config.devx = 1;
685 		DRV_LOG(DEBUG, "DevX is supported.");
686 	} else if (classes == MLX5_CLASS_ETH) {
687 		/* The environment variable is still configured. */
688 		ctx = mlx5_glue->open_device(ibv);
689 		if (ctx == NULL)
690 			goto error;
691 		DRV_LOG(DEBUG, "DevX is NOT supported.");
692 	} else {
693 		goto error;
694 	}
695 	/* The device is created, no need for environment. */
696 	mlx5_restore_doorbell_mapping_env(dbmap_env);
697 	/* Hint libmlx5 to use PMD allocator for data plane resources */
698 	mlx5_set_context_attr(cdev->dev, ctx);
699 	cdev->ctx = ctx;
700 	return 0;
701 error:
702 	rte_errno = errno ? errno : ENODEV;
703 	/* The device creation is failed, no need for environment. */
704 	mlx5_restore_doorbell_mapping_env(dbmap_env);
705 	DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
706 	return -rte_errno;
707 }
708 int
709 mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len)
710 {
711 	char tmp[512];
712 	char cur_ifname[IF_NAMESIZE + 1];
713 	FILE *id_file;
714 	DIR *dir;
715 	struct dirent *ptr;
716 	int ret;
717 
718 	if (guid == NULL || len < sizeof(u_int64_t) + 1)
719 		return -1;
720 	memset(guid, 0, len);
721 	snprintf(tmp, sizeof(tmp), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/net",
722 			dev->domain, dev->bus, dev->devid, dev->function);
723 	dir = opendir(tmp);
724 	if (dir == NULL)
725 		return -1;
726 	/* Traverse to identify PF interface */
727 	do {
728 		ptr = readdir(dir);
729 		if (ptr == NULL || ptr->d_type != DT_DIR) {
730 			closedir(dir);
731 			return -1;
732 		}
733 	} while (strchr(ptr->d_name, '.') || strchr(ptr->d_name, '_') ||
734 		 strchr(ptr->d_name, 'v'));
735 	snprintf(cur_ifname, sizeof(cur_ifname), "%s", ptr->d_name);
736 	closedir(dir);
737 	snprintf(tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
738 			"/%s/phys_switch_id", cur_ifname);
739 	/* Older OFED like 5.3 doesn't support read */
740 	id_file = fopen(tmp, "r");
741 	if (!id_file)
742 		return 0;
743 	ret = fscanf(id_file, "%16s", guid);
744 	fclose(id_file);
745 	return ret;
746 }
747