[Accel-config] [PATCH v2 02/12] accel-config: Create mdev structures and load mdevs into device context

ramesh.thomas at intel.com
Newsgroups dev.linux.lists.accel-config
Message-ID <[email protected]>
From: Ramesh Thomas <ramesh.thomas(a)intel.com>

Lays the foundation for the mdev implementation

- Create private and exported structures for mdev types and mdev list
- Create routines to scan and classify mdevs in sysfs
- Store mdev details in device contexts during init

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/lib/libaccfg.c    | 81 +++++++++++++++++++++++++++++++++++++++++
 accfg/lib/private.h     |  8 ++++
 accfg/libaccel_config.h |  7 ++++
 3 files changed, 96 insertions(+)

diff --git a/accfg/lib/libaccfg.c b/accfg/lib/libaccfg.c
index 8b35602..27f097f 100644
--- a/accfg/lib/libaccfg.c
+++ b/accfg/lib/libaccfg.c
@@ -43,6 +43,12 @@ ACCFG_EXPORT char *accfg_basenames[] = {
 	NULL
 };
 
+ACCFG_EXPORT char *accfg_mdev_basenames[] = {
+	[ACCFG_MDEV_TYPE_1_DWQ]      = "1dwq",
+	[ACCFG_MDEV_TYPE_1_SWQ]      = "1swq",
+	NULL
+};
+
 enum {
 	ACCFG_CMD_STATUS_MAX = 0x45,
 };
@@ -407,6 +413,77 @@ static int device_parse_type(struct accfg_device *device)
 	return 0;
 }
 
+static int mdev_str_to_type(char *mdev_type_str)
+{
+	char **b;
+	char *s;
+	int l, i;
+
+	s = strchr(mdev_type_str, '-');
+	if (!s)
+		s = mdev_type_str;
+	else
+		s++;
+	l = (int) (strchrnul(s, '-') - s);
+
+	for (b = accfg_mdev_basenames, i = 0; *b != NULL; b++, i++)
+		if (!strncmp(*b, s, l))
+			return i;
+
+	return ACCFG_MDEV_TYPE_UNKNOWN;
+}
+
+static int add_device_mdevs(struct accfg_ctx *ctx, struct accfg_device *dev)
+{
+	struct accfg_device_mdev *dev_mdev;
+	uuid_t uu;
+	struct dirent **d;
+	char *f, *mdev_type_str;
+	char p[PATH_MAX];
+	char mdev_path[PATH_MAX];
+	int n, n1, rc = 0;
+
+	n1 = n = scandir(dev->mdev_path, &d, NULL, alphasort);
+	if (n < 0) {
+		err(ctx, "scandir failed\n");
+		return -ENOENT;
+	}
+
+	while (n--) {
+		f = &d[n]->d_name[0];
+		if (*f == '.' || uuid_parse(f, uu))
+			continue;
+		sprintf(p, "%s/%s/mdev_type", dev->mdev_path, f);
+		if (!realpath(p, mdev_path))
+			continue;
+		dev_mdev = calloc(1,
+			sizeof(struct accfg_device_mdev));
+		if (!dev_mdev) {
+			err(ctx, "allocation failed\n");
+			rc = -ENOMEM;
+			goto exit_add_mdev;
+		}
+		uuid_copy(dev_mdev->uuid, uu);
+		mdev_type_str = strrchr(mdev_path, '/') + 1;
+		dev_mdev->device = dev;
+		dev_mdev->type = mdev_str_to_type(mdev_type_str);
+		if (dev_mdev->type == ACCFG_MDEV_TYPE_UNKNOWN) {
+			err(ctx, "mdev type error\n");
+			free(dev_mdev);
+			rc = -EINVAL;
+			goto exit_add_mdev;
+		}
+		list_add_tail(&dev->mdev_list, &dev_mdev->list);
+	}
+
+exit_add_mdev:
+	while (n1--)
+		free(d[n1]);
+	free(d);
+
+	return rc;
+}
+
 static void *add_device(void *parent, int id, const char *ctl_base, char *dev_prefix)
 {
 	struct accfg_ctx *ctx = parent;
@@ -439,6 +516,7 @@ static void *add_device(void *parent, int id, const char *ctl_base, char *dev_pr
 	list_head_init(&device->groups);
 	list_head_init(&device->wqs);
 	list_head_init(&device->engines);
+	list_head_init(&device->mdev_list);
 
 	device->ctx = ctx;
 	device->id = id;
@@ -501,6 +579,9 @@ static void *add_device(void *parent, int id, const char *ctl_base, char *dev_pr
 	list_add_tail(&ctx->devices, &device->list);
 	free(path);
 
+	if (add_device_mdevs(ctx, device))
+		goto err_dev_path;
+
 	return device;
 
 err_dev_path:
diff --git a/accfg/lib/private.h b/accfg/lib/private.h
index 365d484..305d73e 100644
--- a/accfg/lib/private.h
+++ b/accfg/lib/private.h
@@ -35,6 +35,7 @@ struct accfg_device {
 	char *device_type_str;
 	enum accfg_device_type type;
         size_t buf_len;
+	struct list_head mdev_list;
 
 	/* Device Attributes */
 	struct accfg_error errors;
@@ -56,6 +57,13 @@ struct accfg_device {
 	char *pasid_enabled;
 };
 
+struct accfg_device_mdev {
+	struct accfg_device *device;
+	uuid_t uuid;
+	enum accfg_mdev_type type;
+	struct list_node list;
+};
+
 struct accfg_group {
         struct accfg_device *device;
         int id;
diff --git a/accfg/libaccel_config.h b/accfg/libaccel_config.h
index f85670c..e89bd40 100644
--- a/accfg/libaccel_config.h
+++ b/accfg/libaccel_config.h
@@ -67,6 +67,12 @@ enum accfg_control_flag {
 	ACCFG_WQ_DISABLE,
 };
 
+enum accfg_mdev_type {
+	ACCFG_MDEV_TYPE_1_DWQ,
+	ACCFG_MDEV_TYPE_1_SWQ,
+	ACCFG_MDEV_TYPE_UNKNOWN,
+};
+
 /* no need to save device error */
 struct accfg_error {
         uint64_t val[4];
@@ -78,6 +84,7 @@ struct dev_parameters {
 };
 
 extern char *accfg_basenames[];
+extern char *accfg_mdev_basenames[];
 
 struct group_parameters {
 	unsigned int tokens_reserved;
-- 
2.26.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.