diff mbox series

[08/19] common/mlx5: derive PCI addr in FreeBSD

Message ID 20210927133450.10653-9-srikanth.k@oneconvergence.com (mailing list archive)
State Superseded, archived
Delegated to: Raslan Darawsheh
Headers show
Series MLX5 FreeBSD support | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Srikanth Kaka Sept. 27, 2021, 1:34 p.m. UTC
The FreeBSD OFED gives the Linux path as the IBV device path.
The PCI addr is derived by getting the IBV dev interface index and
finding the corresponding sysctl variable pertaining to it.

Signed-off-by: Srikanth Kaka <srikanth.k@oneconvergence.com>
Signed-off-by: Vag Singh <vag.singh@oneconvergence.com>
Signed-off-by: Anand Thulasiram <avelu@juniper.net>
---
 drivers/common/mlx5/freebsd/mlx5_common_os.c | 79 ++++++++++----------
 drivers/common/mlx5/freebsd/mlx5_common_os.h |  2 +
 2 files changed, 41 insertions(+), 40 deletions(-)
diff mbox series

Patch

diff --git a/drivers/common/mlx5/freebsd/mlx5_common_os.c b/drivers/common/mlx5/freebsd/mlx5_common_os.c
index 9e0c823c97..af28ff756d 100644
--- a/drivers/common/mlx5/freebsd/mlx5_common_os.c
+++ b/drivers/common/mlx5/freebsd/mlx5_common_os.c
@@ -10,6 +10,7 @@ 
 #endif
 #include <dirent.h>
 #include <net/if.h>
+#include <sys/sysctl.h>
 
 #include <rte_errno.h>
 #include <rte_string_fns.h>
@@ -26,48 +27,46 @@  const struct mlx5_glue *mlx5_glue;
 int
 mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
 {
-	FILE *file;
-	char line[32];
-	int rc = -ENOENT;
-	MKSTR(path, "%s/device/uevent", dev_path);
-
-	file = fopen(path, "rb");
-	if (file == NULL) {
-		rte_errno = errno;
-		return -rte_errno;
-	}
-	while (fgets(line, sizeof(line), file) == line) {
-		size_t len = strlen(line);
-
-		/* Truncate long lines. */
-		if (len == (sizeof(line) - 1)) {
-			while (line[(len - 1)] != '\n') {
-				int ret = fgetc(file);
-
-				if (ret == EOF)
-					goto exit;
-				line[(len - 1)] = ret;
-			}
-			/* No match for long lines. */
-			continue;
-		}
-		/* Extract information. */
-		if (sscanf(line,
-			   "PCI_SLOT_NAME="
-			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
-			   &pci_addr->domain,
-			   &pci_addr->bus,
-			   &pci_addr->devid,
-			   &pci_addr->function) == 4) {
-			rc = 0;
+	char id[10];
+	char searchstr[MLX5_SYSCTL_BY_NAME_SIZE];
+	size_t length = MLX5_SYSCTL_BY_NAME_SIZE;
+	char name[MLX5_SYSCTL_BY_NAME_SIZE];
+	int traverse = 0;
+
+	strncpy(id, &dev_path[strlen("/sys/class/infiniband/mlx5_")],
+			strlen(dev_path) - strlen("/sys/class/infiniband/mlx5_"));
+	sprintf(searchstr, "dev.mlx5_core.%s.%slocation", id, "%");
+	sysctlbyname(searchstr, &name, &length, NULL, 0);
+
+	while (name[traverse]) {
+		if (name[traverse] == 'p' && name[traverse + 1] == 'c' &&
+				name[traverse + 2] == 'i')
 			break;
-		}
+		++traverse;
 	}
-exit:
-	fclose(file);
-	if (rc)
-		rte_errno = -rc;
-	return rc;
+	traverse += 3;
+
+	int end = traverse;
+
+	while (name[end] != ' ')
+		++end;
+
+	char address[end - traverse + 1];
+
+	memcpy(address, &name[traverse], end - traverse);
+	address[end - traverse] = '\0';
+
+	char *ptr = strtok(address, " :");
+
+	pci_addr->domain = (uint32_t)atoi(ptr);
+	ptr = strtok(NULL, " :");
+	pci_addr->bus = (uint8_t)atoi(ptr);
+	ptr =   strtok(NULL, " :");
+	pci_addr->devid = (uint8_t)atoi(ptr);
+	ptr =   strtok(NULL, " :");
+	pci_addr->function = (uint8_t)atoi(ptr);
+
+	return 0;
 }
 
 /**
diff --git a/drivers/common/mlx5/freebsd/mlx5_common_os.h b/drivers/common/mlx5/freebsd/mlx5_common_os.h
index c3202b6786..8fb681444f 100644
--- a/drivers/common/mlx5/freebsd/mlx5_common_os.h
+++ b/drivers/common/mlx5/freebsd/mlx5_common_os.h
@@ -19,6 +19,8 @@ 
 #include "mlx5_glue.h"
 #include "mlx5_malloc.h"
 
+#define MLX5_SYSCTL_BY_NAME_SIZE 100
+
 /**
  * Get device name. Given an ibv_device pointer - return a
  * pointer to the corresponding device name.