[Accel-config] [PATCH v2 05/12] accel-config: Implement create and remove mdev library API

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

Adds accfg_create_mdev and accfg_remove_mdev library functions.

Signed-off-by: Ramesh Thomas <ramesh.thomas(a)intel.com>
---
 accfg/lib/libaccel-config.sym |   2 +
 accfg/lib/libaccfg.c          | 104 ++++++++++++++++++++++++++++++++++
 accfg/libaccel_config.h       |   3 +
 3 files changed, 109 insertions(+)

diff --git a/accfg/lib/libaccel-config.sym b/accfg/lib/libaccel-config.sym
index a857152..73c8417 100644
--- a/accfg/lib/libaccel-config.sym
+++ b/accfg/lib/libaccel-config.sym
@@ -141,4 +141,6 @@ global:
 	accfg_mdev_get_uuid;
 	accfg_mdev_get_type;
 	accfg_mdev_basenames;
+	accfg_create_mdev;
+	accfg_remove_mdev;
 } LIBACCFG_7;
diff --git a/accfg/lib/libaccfg.c b/accfg/lib/libaccfg.c
index 2cd4e3c..106e21e 100644
--- a/accfg/lib/libaccfg.c
+++ b/accfg/lib/libaccfg.c
@@ -979,6 +979,110 @@ ACCFG_EXPORT enum accfg_mdev_type accfg_mdev_get_type(struct accfg_device_mdev *
 	return mdev->type;
 }
 
+static void accfg_gen_uuid(struct accfg_device *device, uuid_t uuid)
+{
+	struct accfg_device_mdev *entry, *next;
+
+	uuid_clear(uuid);
+	while (1) {
+		uuid_generate(uuid);
+		if (list_empty(&device->mdev_list))
+			return;
+		list_for_each_safe(&device->mdev_list, entry, next, list) {
+			if (uuid_compare(uuid, entry->uuid) != 0)
+				return;
+		}
+	}
+}
+
+ACCFG_EXPORT int accfg_create_mdev(struct accfg_device *device,
+		enum accfg_mdev_type type, uuid_t uuid)
+{
+	struct accfg_ctx *ctx = accfg_device_get_ctx(device);
+	struct accfg_device_mdev *mdev;
+	char uuid_str[UUID_STR_LEN];
+	char mdev_path[PATH_MAX];
+	int rc;
+
+	if (type >= ACCFG_MDEV_TYPE_UNKNOWN || type < 0)
+		return -EINVAL;
+
+	mdev = calloc(1, sizeof(struct accfg_device_mdev));
+	if (!mdev) {
+		err(ctx, "mdev allocation failed\n");
+		return -ENOMEM;
+	}
+	accfg_gen_uuid(device, mdev->uuid);
+	mdev->type = type;
+
+	uuid_unparse(mdev->uuid, uuid_str);
+	sprintf(mdev_path, "%s/%s/idxd-%s/create", device->mdev_path,
+			MDEV_POSTFIX, accfg_mdev_basenames[type]);
+	rc = sysfs_write_attr(ctx, mdev_path, uuid_str);
+	if (rc < 0) {
+		err(ctx, "create mdev failed %d\n", rc);
+		goto create_err;
+	}
+
+	list_add_tail(&device->mdev_list, &mdev->list);
+	uuid_copy(uuid, mdev->uuid);
+	return 0;
+
+create_err:
+	free(mdev);
+	return rc;
+}
+
+static int accfg_device_mdev_remove(struct accfg_device *device,
+		struct accfg_device_mdev *mdev)
+{
+	struct accfg_ctx *ctx = accfg_device_get_ctx(device);
+	char uuid_str[UUID_STR_LEN];
+	char mdev_path[PATH_MAX];
+	int rc;
+
+	uuid_unparse(mdev->uuid, uuid_str);
+	sprintf(mdev_path, "%s/%s/remove", device->mdev_path, uuid_str);
+	rc = sysfs_write_attr(ctx, mdev_path, "1");
+	if (rc < 0)
+		return rc;
+
+	list_del(&mdev->list);
+	free(mdev);
+
+	return 0;
+}
+
+ACCFG_EXPORT int accfg_remove_mdev(struct accfg_device *device, uuid_t uuid)
+{
+	struct accfg_ctx *ctx = accfg_device_get_ctx(device);
+	struct accfg_device_mdev *entry, *next;
+	int rc, all;
+
+	/* remove all mdevs if null uuid is passed */
+	all = uuid_is_null(uuid);
+	list_for_each_safe(&device->mdev_list, entry, next, list) {
+		if (all || !uuid_compare(entry->uuid, uuid)) {
+			rc = accfg_device_mdev_remove(device, entry);
+			if (rc < 0)
+				goto remove_err;
+			if (!all)
+				return 0;
+		}
+	}
+
+	if (!all) {
+		err(ctx, "mdev uuid not found\n");
+		return -EINVAL;
+	}
+
+	return 0;
+
+remove_err:
+	err(ctx, "remove mdev failed %d\n", rc);
+	return rc;
+}
+
 /**
  * accfg_device_get_first - retrieve first device in the system
  * @ctx: context established by accfg_new
diff --git a/accfg/libaccel_config.h b/accfg/libaccel_config.h
index a55bfde..95678fb 100644
--- a/accfg/libaccel_config.h
+++ b/accfg/libaccel_config.h
@@ -189,6 +189,9 @@ struct accfg_device_mdev *accfg_device_first_mdev(struct accfg_device *device);
 struct accfg_device_mdev *accfg_device_next_mdev(struct accfg_device_mdev *mdev);
 void accfg_mdev_get_uuid(struct accfg_device_mdev *mdev, uuid_t uuid);
 enum accfg_mdev_type accfg_mdev_get_type(struct accfg_device_mdev *mdev);
+int accfg_create_mdev(struct accfg_device *device, enum accfg_mdev_type type,
+		uuid_t uuid);
+int accfg_remove_mdev(struct accfg_device *device, uuid_t uuid);
 
 #define accfg_device_mdev_foreach(device, mdev) \
 	for (mdev = accfg_device_first_mdev(device); \
-- 
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.