[v4,06/14] bus/vmbus: get device resource values using sysctl

Message ID 20220418042915.5765-7-srikanth.k@oneconvergence.com (mailing list archive)
State Superseded, archived
Headers
Series add FreeBSD support to VMBUS & NetVSC PMDs |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Srikanth Kaka April 18, 2022, 4:29 a.m. UTC
The UIO device's attribute (relid, monitor id, etc) values are
retrieved using following sysctl variables:
$ sysctl dev.hv_uio.0
dev.hv_uio.0.send_buf.gpadl: 925241
dev.hv_uio.0.send_buf.size: 16777216
dev.hv_uio.0.recv_buf.gpadl: 925240
dev.hv_uio.0.recv_buf.size: 32505856
dev.hv_uio.0.monitor_page.size: 4096
dev.hv_uio.0.int_page.size: 4096

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/bus/vmbus/freebsd/vmbus_uio.c | 105 ++++++++++++++++++++++++++
 drivers/bus/vmbus/linux/vmbus_uio.c   |  16 ++++
 drivers/bus/vmbus/osi/vmbus_osi.h     |   4 +
 drivers/bus/vmbus/osi/vmbus_osi_uio.c |   6 +-
 4 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100644 drivers/bus/vmbus/freebsd/vmbus_uio.c
  

Patch

diff --git a/drivers/bus/vmbus/freebsd/vmbus_uio.c b/drivers/bus/vmbus/freebsd/vmbus_uio.c
new file mode 100644
index 0000000000..b622388ce9
--- /dev/null
+++ b/drivers/bus/vmbus/freebsd/vmbus_uio.c
@@ -0,0 +1,105 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2018, Microsoft Corporation.
+ * All Rights Reserved.
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/ioctl.h>
+
+#include <rte_log.h>
+#include <rte_bus.h>
+#include <rte_malloc.h>
+#include <rte_bus_vmbus.h>
+
+#include "private.h"
+#include "vmbus_osi.h"
+
+const char *driver_name = "hv_uio";
+
+/* Check map names with kernel names */
+static const char *map_names[VMBUS_MAX_RESOURCE] = {
+	[HV_TXRX_RING_MAP] = "txrx_rings",
+	[HV_INT_PAGE_MAP]  = "int_page",
+	[HV_MON_PAGE_MAP]  = "monitor_page",
+	[HV_RECV_BUF_MAP]  = "recv_buf",
+	[HV_SEND_BUF_MAP]  = "send_buf",
+};
+
+static int
+sysctl_get_vmbus_device_info(struct rte_vmbus_device *dev)
+{
+	char sysctlBuffer[PATH_MAX];
+	char sysctlVar[PATH_MAX];
+	size_t len = PATH_MAX, sysctl_len;
+	unsigned long tmp;
+	int i;
+
+	snprintf(sysctlBuffer, len, "dev.%s.%d", driver_name, dev->uio_num);
+
+	sysctl_len = sizeof(unsigned long);
+	/* get relid */
+	snprintf(sysctlVar, len, "%s.channel.ch_id", sysctlBuffer);
+	if (sysctlbyname(sysctlVar, &tmp, &sysctl_len, NULL, 0) < 0) {
+		VMBUS_LOG(ERR, "could not read %s", sysctlVar);
+		goto error;
+	}
+	dev->relid = tmp;
+
+	/* get monitor id */
+	snprintf(sysctlVar, len, "%s.channel.%u.monitor_id", sysctlBuffer,
+		 dev->relid);
+	if (sysctlbyname(sysctlVar, &tmp, &sysctl_len, NULL, 0) < 0) {
+		VMBUS_LOG(ERR, "could not read %s", sysctlVar);
+		goto error;
+	}
+	dev->monitor_id = tmp;
+
+	/* Extract resource value */
+	for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
+		struct rte_mem_resource *res = &dev->resource[i];
+		unsigned long size, gpad = 0;
+		size_t sizelen = sizeof(len);
+
+		snprintf(sysctlVar, sizeof(sysctlVar), "%s.%s.size",
+			 sysctlBuffer, map_names[i]);
+		if (sysctlbyname(sysctlVar, &size, &sizelen, NULL, 0) < 0) {
+			VMBUS_LOG(ERR,
+				"could not read %s", sysctlVar);
+			goto error;
+		}
+		res->len = size;
+
+		if (i == HV_RECV_BUF_MAP || i == HV_SEND_BUF_MAP) {
+			snprintf(sysctlVar, sizeof(sysctlVar), "%s.%s.gpadl",
+				 sysctlBuffer, map_names[i]);
+			if (sysctlbyname(sysctlVar, &gpad, &sizelen, NULL, 0) < 0) {
+				VMBUS_LOG(ERR,
+					"could not read %s", sysctlVar);
+				goto error;
+			}
+			/* put the GPAD value in physical address */
+			res->phys_addr = gpad;
+		}
+	}
+	return 0;
+error:
+	return -1;
+}
+
+/*
+ * On FreeBSD, the device is opened first to ensure kernel UIO driver
+ * is properly initialized before reading device attributes
+ */
+int vmbus_get_device_info_os(struct rte_vmbus_device *dev)
+{
+	return sysctl_get_vmbus_device_info(dev);
+}
+
+const char *get_devname_os(void)
+{
+	return "/dev/hv_uio";
+}
diff --git a/drivers/bus/vmbus/linux/vmbus_uio.c b/drivers/bus/vmbus/linux/vmbus_uio.c
index ea6df21409..669551a4d4 100644
--- a/drivers/bus/vmbus/linux/vmbus_uio.c
+++ b/drivers/bus/vmbus/linux/vmbus_uio.c
@@ -199,3 +199,19 @@  int vmbus_uio_get_subchan(struct vmbus_channel *primary,
 	closedir(chan_dir);
 	return err;
 }
+
+/*
+ * In Linux the device info is fetched from SYSFS and doesn't need
+ * opening of the device before reading its attributes
+ * This is a stub function and it should always succeed.
+ */
+int vmbus_get_device_info_os(struct rte_vmbus_device *dev)
+{
+	RTE_SET_USED(dev);
+	return 0;
+}
+
+const char *get_devname_os(void)
+{
+	return "/dev/uio";
+}
diff --git a/drivers/bus/vmbus/osi/vmbus_osi.h b/drivers/bus/vmbus/osi/vmbus_osi.h
index 579c4bb99c..59afc102f1 100644
--- a/drivers/bus/vmbus/osi/vmbus_osi.h
+++ b/drivers/bus/vmbus/osi/vmbus_osi.h
@@ -20,4 +20,8 @@  int vmbus_uio_map_subchan_os(const struct rte_vmbus_device *dev,
 bool vmbus_isnew_subchannel(struct vmbus_channel *primary,
 			    unsigned long id);
 
+int vmbus_get_device_info_os(struct rte_vmbus_device *dev);
+
+const char *get_devname_os(void);
+
 #endif /* _VMBUS_BUS_OSI_H_ */
diff --git a/drivers/bus/vmbus/osi/vmbus_osi_uio.c b/drivers/bus/vmbus/osi/vmbus_osi_uio.c
index 35106e247e..3374813cc8 100644
--- a/drivers/bus/vmbus/osi/vmbus_osi_uio.c
+++ b/drivers/bus/vmbus/osi/vmbus_osi_uio.c
@@ -82,7 +82,8 @@  vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
 	int fd;
 
 	/* save fd if in primary process */
-	snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
+	snprintf(devname, sizeof(devname), "%s%u", get_devname_os(),
+		 dev->uio_num);
 	fd = open(devname, O_RDWR);
 	if (fd < 0) {
 		VMBUS_LOG(ERR, "Cannot open %s: %s",
@@ -106,6 +107,9 @@  vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
 	strlcpy((*uio_res)->path, devname, PATH_MAX);
 	rte_uuid_copy((*uio_res)->id, dev->device_id);
 
+	if (vmbus_get_device_info_os(dev) < 0)
+		goto error;
+
 	return 0;
 
 error: