Blame SOURCES/0137-cxl-add-command-cxl-set-partition.patch

26ccd9
From e2d5cbab4a4b530b679172ae7ca59cc506d1d4cc Mon Sep 17 00:00:00 2001
26ccd9
From: Alison Schofield <alison.schofield@intel.com>
26ccd9
Date: Tue, 22 Feb 2022 11:56:08 -0800
26ccd9
Subject: [PATCH 137/217] cxl: add command 'cxl set-partition'
26ccd9
26ccd9
CXL devices may support both volatile and persistent memory capacity.
26ccd9
The amount of device capacity set aside for each type is typically
26ccd9
established at the factory, but some devices also allow for dynamic
26ccd9
re-partitioning. Add a command for this purpose.
26ccd9
26ccd9
 usage: cxl set-partition <mem0> [<mem1>..<memN>] [<options>]
26ccd9
26ccd9
    -v, --verbose         turn on debug
26ccd9
    -S, --serial          use serial numbers to id memdevs
26ccd9
    -t, --type <type>     'pmem' or 'volatile' (Default: 'pmem')
26ccd9
    -s, --size <size>     size in bytes (Default: all available capacity)
26ccd9
    -a, --align           auto-align --size per device's requirement
26ccd9
26ccd9
Link: https://lore.kernel.org/r/e7accc7ba93def81b48304cf5fb483345757410d.1645558189.git.alison.schofield@intel.com
26ccd9
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Alison Schofield <alison.schofield@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 Documentation/cxl/cxl-set-partition.txt |  68 ++++++++
26ccd9
 Documentation/cxl/meson.build           |   1 +
26ccd9
 cxl/builtin.h                           |   1 +
26ccd9
 cxl/cxl.c                               |   1 +
26ccd9
 cxl/memdev.c                            | 206 ++++++++++++++++++++++++
26ccd9
 5 files changed, 277 insertions(+)
26ccd9
 create mode 100644 Documentation/cxl/cxl-set-partition.txt
26ccd9
26ccd9
diff --git a/Documentation/cxl/cxl-set-partition.txt b/Documentation/cxl/cxl-set-partition.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..1e548af
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-set-partition.txt
26ccd9
@@ -0,0 +1,68 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-set-partition(1)
26ccd9
+====================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-set-partition - set the partitioning between volatile and persistent capacity on a CXL memdev
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl set-partition <mem0> [ [<mem1>..<memN>] [<options>]'
26ccd9
+
26ccd9
+DESCRIPTION
26ccd9
+-----------
26ccd9
+CXL devices may support both volatile and persistent memory capacity.
26ccd9
+The amount of device capacity set aside for each type is typically
26ccd9
+established at the factory, but some devices also allow for dynamic
26ccd9
+re-partitioning.
26ccd9
+
26ccd9
+Use this command to partition a device into volatile and persistent
26ccd9
+capacity. The change in partitioning becomes the “next” configuration,
26ccd9
+to become active on the next device reset.
26ccd9
+
26ccd9
+Use "cxl list -m <memdev> -I" to examine the partitioning capabilities
26ccd9
+of a device. A partition_alignment_size value of zero means there is
26ccd9
+no available capacity and therefore the partitions cannot be changed.
26ccd9
+
26ccd9
+Using this command to change the size of the persistent capacity shall
26ccd9
+result in the loss of data stored.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+<memory device(s)>::
26ccd9
+include::memdev-option.txt[]
26ccd9
+
26ccd9
+-t::
26ccd9
+--type=::
26ccd9
+	Type of partition, 'pmem' or 'volatile', to modify.
26ccd9
+	Default: 'pmem'
26ccd9
+
26ccd9
+-s::
26ccd9
+--size=::
26ccd9
+	Size of the <type> partition in bytes. Size must align to the
26ccd9
+	devices alignment requirement. Use 'cxl list -m <memdev> -I'
26ccd9
+	to find 'partition_alignment_size', or, use the --align option.
26ccd9
+	Default: All available capacity is assigned to <type>.
26ccd9
+
26ccd9
+-a::
26ccd9
+--align::
26ccd9
+	Select this option to allow the automatic alignment of --size
26ccd9
+	to meet device alignment requirements. When using this option,
26ccd9
+	specify the minimum --size of the --type partition needed. When
26ccd9
+	this option is omitted, the command fails if --size is not
26ccd9
+	properly aligned. Use 'cxl list -m <memdev> -I' to examine the
26ccd9
+	partition_alignment_size.
26ccd9
+
26ccd9
+-v::
26ccd9
+        Turn on verbose debug messages in the library (if libcxl was built with
26ccd9
+        logging and debug enabled).
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-list[1],
26ccd9
+CXL-2.0 8.2.9.5.2
26ccd9
diff --git a/Documentation/cxl/meson.build b/Documentation/cxl/meson.build
26ccd9
index 96f4666..e927644 100644
26ccd9
--- a/Documentation/cxl/meson.build
26ccd9
+++ b/Documentation/cxl/meson.build
26ccd9
@@ -34,6 +34,7 @@ cxl_manpages = [
26ccd9
   'cxl-disable-memdev.txt',
26ccd9
   'cxl-enable-port.txt',
26ccd9
   'cxl-disable-port.txt',
26ccd9
+  'cxl-set-partition.txt',
26ccd9
 ]
26ccd9
 
26ccd9
 foreach man : cxl_manpages
26ccd9
diff --git a/cxl/builtin.h b/cxl/builtin.h
26ccd9
index 3123d5e..7bbad98 100644
26ccd9
--- a/cxl/builtin.h
26ccd9
+++ b/cxl/builtin.h
26ccd9
@@ -14,4 +14,5 @@ int cmd_disable_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_enable_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_disable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 #endif /* _CXL_BUILTIN_H_ */
26ccd9
diff --git a/cxl/cxl.c b/cxl/cxl.c
26ccd9
index c20c569..ab4bbec 100644
26ccd9
--- a/cxl/cxl.c
26ccd9
+++ b/cxl/cxl.c
26ccd9
@@ -68,6 +68,7 @@ static struct cmd_struct commands[] = {
26ccd9
 	{ "enable-memdev", .c_fn = cmd_enable_memdev },
26ccd9
 	{ "disable-port", .c_fn = cmd_disable_port },
26ccd9
 	{ "enable-port", .c_fn = cmd_enable_port },
26ccd9
+	{ "set-partition", .c_fn = cmd_set_partition },
26ccd9
 };
26ccd9
 
26ccd9
 int main(int argc, const char **argv)
26ccd9
diff --git a/cxl/memdev.c b/cxl/memdev.c
26ccd9
index 90b33e1..91d914d 100644
26ccd9
--- a/cxl/memdev.c
26ccd9
+++ b/cxl/memdev.c
26ccd9
@@ -6,11 +6,14 @@
26ccd9
 #include <unistd.h>
26ccd9
 #include <limits.h>
26ccd9
 #include <util/log.h>
26ccd9
+#include <util/json.h>
26ccd9
+#include <util/size.h>
26ccd9
 #include <cxl/libcxl.h>
26ccd9
 #include <util/parse-options.h>
26ccd9
 #include <ccan/minmax/minmax.h>
26ccd9
 #include <ccan/array_size/array_size.h>
26ccd9
 
26ccd9
+#include "json.h"
26ccd9
 #include "filter.h"
26ccd9
 
26ccd9
 struct action_context {
26ccd9
@@ -26,10 +29,18 @@ static struct parameters {
26ccd9
 	bool verbose;
26ccd9
 	bool serial;
26ccd9
 	bool force;
26ccd9
+	bool align;
26ccd9
+	const char *type;
26ccd9
+	const char *size;
26ccd9
 } param;
26ccd9
 
26ccd9
 static struct log_ctx ml;
26ccd9
 
26ccd9
+enum cxl_setpart_type {
26ccd9
+	CXL_SETPART_PMEM,
26ccd9
+	CXL_SETPART_VOLATILE,
26ccd9
+};
26ccd9
+
26ccd9
 #define BASE_OPTIONS() \
26ccd9
 OPT_BOOLEAN('v',"verbose", &param.verbose, "turn on debug"), \
26ccd9
 OPT_BOOLEAN('S', "serial", &param.serial, "use serial numbers to id memdevs")
26ccd9
@@ -51,6 +62,14 @@ OPT_UINTEGER('O', "offset", &param.offset, \
26ccd9
 OPT_BOOLEAN('f', "force", &param.force,                                \
26ccd9
 	    "DANGEROUS: override active memdev safety checks")
26ccd9
 
26ccd9
+#define SET_PARTITION_OPTIONS() \
26ccd9
+OPT_STRING('t', "type",  &param.type, "type",			\
26ccd9
+	"'pmem' or 'volatile' (Default: 'pmem')"),		\
26ccd9
+OPT_STRING('s', "size",  &param.size, "size",			\
26ccd9
+	"size in bytes (Default: all available capacity)"),	\
26ccd9
+OPT_BOOLEAN('a', "align",  &param.align,			\
26ccd9
+	"auto-align --size per device's requirement")
26ccd9
+
26ccd9
 static const struct option read_options[] = {
26ccd9
 	BASE_OPTIONS(),
26ccd9
 	LABEL_OPTIONS(),
26ccd9
@@ -82,6 +101,12 @@ static const struct option enable_options[] = {
26ccd9
 	OPT_END(),
26ccd9
 };
26ccd9
 
26ccd9
+static const struct option set_partition_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	SET_PARTITION_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
 static int action_disable(struct cxl_memdev *memdev, struct action_context *actx)
26ccd9
 {
26ccd9
 	if (!cxl_memdev_is_enabled(memdev))
26ccd9
@@ -209,6 +234,176 @@ out:
26ccd9
 	return rc;
26ccd9
 }
26ccd9
 
26ccd9
+static unsigned long long
26ccd9
+partition_align(const char *devname, enum cxl_setpart_type type,
26ccd9
+		unsigned long long volatile_size, unsigned long long alignment,
26ccd9
+		unsigned long long available)
26ccd9
+{
26ccd9
+	if (IS_ALIGNED(volatile_size, alignment))
26ccd9
+		return volatile_size;
26ccd9
+
26ccd9
+	if (!param.align) {
26ccd9
+		log_err(&ml, "%s: size %lld is not partition aligned %lld\n",
26ccd9
+			devname, volatile_size, alignment);
26ccd9
+		return ULLONG_MAX;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* Align based on partition type to fulfill users size request */
26ccd9
+	if (type == CXL_SETPART_PMEM)
26ccd9
+		volatile_size = ALIGN_DOWN(volatile_size, alignment);
26ccd9
+	else
26ccd9
+		volatile_size = ALIGN(volatile_size, alignment);
26ccd9
+
26ccd9
+	/* Fail if the align pushes size over the available limit. */
26ccd9
+	if (volatile_size > available) {
26ccd9
+		log_err(&ml, "%s: aligned partition size %lld exceeds available size %lld\n",
26ccd9
+			devname, volatile_size, available);
26ccd9
+		volatile_size = ULLONG_MAX;
26ccd9
+	}
26ccd9
+
26ccd9
+	return volatile_size;
26ccd9
+}
26ccd9
+
26ccd9
+static unsigned long long
26ccd9
+param_size_to_volatile_size(const char *devname, enum cxl_setpart_type type,
26ccd9
+		unsigned long long size, unsigned long long available)
26ccd9
+{
26ccd9
+	/* User omits size option. Apply all available capacity to type. */
26ccd9
+	if (size == ULLONG_MAX) {
26ccd9
+		if (type == CXL_SETPART_PMEM)
26ccd9
+			return 0;
26ccd9
+		return available;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* User includes a size option. Apply it to type */
26ccd9
+	if (size > available) {
26ccd9
+		log_err(&ml, "%s: %lld exceeds available capacity %lld\n",
26ccd9
+			devname, size, available);
26ccd9
+			return ULLONG_MAX;
26ccd9
+	}
26ccd9
+	if (type == CXL_SETPART_PMEM)
26ccd9
+		return available - size;
26ccd9
+	return size;
26ccd9
+}
26ccd9
+
26ccd9
+/*
26ccd9
+ * Return the volatile_size to use in the CXL set paritition
26ccd9
+ * command, or ULLONG_MAX if unable to validate the partition
26ccd9
+ * request.
26ccd9
+ */
26ccd9
+static unsigned long long
26ccd9
+validate_partition(struct cxl_memdev *memdev, enum cxl_setpart_type type,
26ccd9
+		unsigned long long size)
26ccd9
+{
26ccd9
+	unsigned long long total_cap, volatile_only, persistent_only;
26ccd9
+	const char *devname = cxl_memdev_get_devname(memdev);
26ccd9
+	unsigned long long volatile_size = ULLONG_MAX;
26ccd9
+	unsigned long long available, alignment;
26ccd9
+	struct cxl_cmd *cmd;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	cmd = cxl_cmd_new_identify(memdev);
26ccd9
+	if (!cmd)
26ccd9
+		return ULLONG_MAX;
26ccd9
+	rc = cxl_cmd_submit(cmd);
26ccd9
+	if (rc < 0)
26ccd9
+		goto out;
26ccd9
+	rc = cxl_cmd_get_mbox_status(cmd);
26ccd9
+	if (rc != 0)
26ccd9
+		goto out;
26ccd9
+
26ccd9
+	alignment = cxl_cmd_identify_get_partition_align(cmd);
26ccd9
+	if (alignment == 0) {
26ccd9
+		log_err(&ml, "%s: no available capacity\n", devname);
26ccd9
+		goto out;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* Calculate the actual available capacity */
26ccd9
+	total_cap = cxl_cmd_identify_get_total_size(cmd);
26ccd9
+	volatile_only = cxl_cmd_identify_get_volatile_only_size(cmd);
26ccd9
+	persistent_only = cxl_cmd_identify_get_persistent_only_size(cmd);
26ccd9
+	available = total_cap - volatile_only - persistent_only;
26ccd9
+
26ccd9
+	/* Translate the users size request into an aligned volatile_size */
26ccd9
+	volatile_size = param_size_to_volatile_size(devname, type, size,
26ccd9
+				available);
26ccd9
+	if (volatile_size == ULLONG_MAX)
26ccd9
+		goto out;
26ccd9
+
26ccd9
+	volatile_size = partition_align(devname, type, volatile_size, alignment,
26ccd9
+				available);
26ccd9
+
26ccd9
+out:
26ccd9
+	cxl_cmd_unref(cmd);
26ccd9
+	return volatile_size;
26ccd9
+}
26ccd9
+
26ccd9
+static int action_setpartition(struct cxl_memdev *memdev,
26ccd9
+		struct action_context *actx)
26ccd9
+{
26ccd9
+	const char *devname = cxl_memdev_get_devname(memdev);
26ccd9
+	enum cxl_setpart_type type = CXL_SETPART_PMEM;
26ccd9
+	unsigned long long size = ULLONG_MAX;
26ccd9
+	struct json_object *jmemdev;
26ccd9
+	struct cxl_cmd *cmd;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (param.type) {
26ccd9
+		if (strcmp(param.type, "pmem") == 0)
26ccd9
+			/* default */;
26ccd9
+		else if (strcmp(param.type, "volatile") == 0)
26ccd9
+			type = CXL_SETPART_VOLATILE;
26ccd9
+		else {
26ccd9
+			log_err(&ml, "invalid type '%s'\n", param.type);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (param.size) {
26ccd9
+		size = parse_size64(param.size);
26ccd9
+		if (size == ULLONG_MAX) {
26ccd9
+			log_err(&ml, "%s: failed to parse size option '%s'\n",
26ccd9
+			devname, param.size);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	size = validate_partition(memdev, type, size);
26ccd9
+	if (size == ULLONG_MAX)
26ccd9
+		return -EINVAL;
26ccd9
+
26ccd9
+	cmd = cxl_cmd_new_set_partition(memdev, size);
26ccd9
+	if (!cmd) {
26ccd9
+		rc = -ENXIO;
26ccd9
+		goto out_err;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_cmd_submit(cmd);
26ccd9
+	if (rc < 0) {
26ccd9
+		log_err(&ml, "cmd submission failed: %s\n", strerror(-rc));
26ccd9
+		goto out_cmd;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_cmd_get_mbox_status(cmd);
26ccd9
+	if (rc != 0) {
26ccd9
+		log_err(&ml, "%s: mbox status: %d\n", __func__, rc);
26ccd9
+		rc = -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+out_cmd:
26ccd9
+	cxl_cmd_unref(cmd);
26ccd9
+out_err:
26ccd9
+	if (rc)
26ccd9
+		log_err(&ml, "%s error: %s\n", devname, strerror(-rc));
26ccd9
+
26ccd9
+	jmemdev = util_cxl_memdev_to_json(memdev, UTIL_JSON_PARTITION);
26ccd9
+	if (jmemdev)
26ccd9
+		printf("%s\n", json_object_to_json_string_ext(jmemdev,
26ccd9
+		       JSON_C_TO_STRING_PRETTY));
26ccd9
+
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
 static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
 			 int (*action)(struct cxl_memdev *memdev,
26ccd9
 				       struct action_context *actx),
26ccd9
@@ -398,3 +593,14 @@ int cmd_enable_memdev(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
 		 count > 1 ? "s" : "");
26ccd9
 	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
 }
26ccd9
+
26ccd9
+int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(argc, argv, ctx, action_setpartition,
26ccd9
+			set_partition_options,
26ccd9
+			"cxl set-partition <mem0> [<mem1>..<memN>] [<options>]");
26ccd9
+	log_info(&ml, "set_partition %d mem%s\n", count >= 0 ? count : 0,
26ccd9
+		 count > 1 ? "s" : "");
26ccd9
+
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
-- 
26ccd9
2.27.0
26ccd9