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

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