anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

Blame SOURCES/0041-libcxl-add-interfaces-for-label-operations.patch

e0018b
From 6255d23452809ddc6d48083c35fc935e4fa420d8 Mon Sep 17 00:00:00 2001
e0018b
From: Vishal Verma <vishal.l.verma@intel.com>
e0018b
Date: Thu, 7 Oct 2021 02:21:34 -0600
e0018b
Subject: [PATCH 041/217] libcxl: add interfaces for label operations
e0018b
e0018b
Add libcxl interfaces to allow performinfg label (LSA) manipulations.
e0018b
Add a 'cxl_cmd_new_set_lsa' interface to create a 'Set LSA' mailbox
e0018b
command payload, and interfaces to read, write, and zero the LSA area on
e0018b
a memdev.
e0018b
e0018b
Cc: Dan Williams <dan.j.williams@intel.com>
e0018b
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 cxl/lib/libcxl.c   | 158 +++++++++++++++++++++++++++++++++++++++++++++
e0018b
 cxl/lib/libcxl.sym |   4 ++
e0018b
 cxl/lib/private.h  |   6 ++
e0018b
 cxl/libcxl.h       |   8 +++
e0018b
 4 files changed, 176 insertions(+)
e0018b
e0018b
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
e0018b
index 60ed646..f0664be 100644
e0018b
--- a/cxl/lib/libcxl.c
e0018b
+++ b/cxl/lib/libcxl.c
e0018b
@@ -1197,3 +1197,161 @@ CXL_EXPORT int cxl_cmd_get_out_size(struct cxl_cmd *cmd)
e0018b
 {
e0018b
 	return cmd->send_cmd->out.size;
e0018b
 }
e0018b
+
e0018b
+CXL_EXPORT struct cxl_cmd *cxl_cmd_new_write_label(struct cxl_memdev *memdev,
e0018b
+		void *lsa_buf, unsigned int offset, unsigned int length)
e0018b
+{
e0018b
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
e0018b
+	struct cxl_cmd_set_lsa *set_lsa;
e0018b
+	struct cxl_cmd *cmd;
e0018b
+	int rc;
e0018b
+
e0018b
+	cmd = cxl_cmd_new_generic(memdev, CXL_MEM_COMMAND_ID_SET_LSA);
e0018b
+	if (!cmd)
e0018b
+		return NULL;
e0018b
+
e0018b
+	/* this will allocate 'in.payload' */
e0018b
+	rc = cxl_cmd_set_input_payload(cmd, NULL, sizeof(*set_lsa) + length);
e0018b
+	if (rc) {
e0018b
+		err(ctx, "%s: cmd setup failed: %s\n",
e0018b
+			cxl_memdev_get_devname(memdev), strerror(-rc));
e0018b
+		goto out_fail;
e0018b
+	}
e0018b
+	set_lsa = (struct cxl_cmd_set_lsa *)cmd->send_cmd->in.payload;
e0018b
+	set_lsa->offset = cpu_to_le32(offset);
e0018b
+	memcpy(set_lsa->lsa_data, lsa_buf, length);
e0018b
+
e0018b
+	return cmd;
e0018b
+
e0018b
+out_fail:
e0018b
+	cxl_cmd_unref(cmd);
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
+enum lsa_op {
e0018b
+	LSA_OP_GET,
e0018b
+	LSA_OP_SET,
e0018b
+	LSA_OP_ZERO,
e0018b
+};
e0018b
+
e0018b
+static int __lsa_op(struct cxl_memdev *memdev, int op, void *buf,
e0018b
+		size_t length, size_t offset)
e0018b
+{
e0018b
+	const char *devname = cxl_memdev_get_devname(memdev);
e0018b
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
e0018b
+	void *zero_buf = NULL;
e0018b
+	struct cxl_cmd *cmd;
e0018b
+	ssize_t ret_len;
e0018b
+	int rc = 0;
e0018b
+
e0018b
+	switch (op) {
e0018b
+	case LSA_OP_GET:
e0018b
+		cmd = cxl_cmd_new_read_label(memdev, offset, length);
e0018b
+		if (!cmd)
e0018b
+			return -ENOMEM;
e0018b
+		rc = cxl_cmd_set_output_payload(cmd, buf, length);
e0018b
+		if (rc) {
e0018b
+			err(ctx, "%s: cmd setup failed: %s\n",
e0018b
+			    cxl_memdev_get_devname(memdev), strerror(-rc));
e0018b
+			goto out;
e0018b
+		}
e0018b
+		break;
e0018b
+	case LSA_OP_ZERO:
e0018b
+		zero_buf = calloc(1, length);
e0018b
+		if (!zero_buf)
e0018b
+			return -ENOMEM;
e0018b
+		buf = zero_buf;
e0018b
+		/* fall through */
e0018b
+	case LSA_OP_SET:
e0018b
+		cmd = cxl_cmd_new_write_label(memdev, buf, offset, length);
e0018b
+		if (!cmd) {
e0018b
+			rc = -ENOMEM;
e0018b
+			goto out_free;
e0018b
+		}
e0018b
+		break;
e0018b
+	default:
e0018b
+		return -EOPNOTSUPP;
e0018b
+	}
e0018b
+
e0018b
+	rc = cxl_cmd_submit(cmd);
e0018b
+	if (rc < 0) {
e0018b
+		err(ctx, "%s: cmd submission failed: %s\n",
e0018b
+			devname, strerror(-rc));
e0018b
+		goto out;
e0018b
+	}
e0018b
+
e0018b
+	rc = cxl_cmd_get_mbox_status(cmd);
e0018b
+	if (rc != 0) {
e0018b
+		err(ctx, "%s: firmware status: %d\n",
e0018b
+			devname, rc);
e0018b
+		rc = -ENXIO;
e0018b
+		goto out;
e0018b
+	}
e0018b
+
e0018b
+	if (op == LSA_OP_GET) {
e0018b
+		ret_len = cxl_cmd_read_label_get_payload(cmd, buf, length);
e0018b
+		if (ret_len < 0) {
e0018b
+			rc = ret_len;
e0018b
+			goto out;
e0018b
+		}
e0018b
+	}
e0018b
+
e0018b
+out:
e0018b
+	cxl_cmd_unref(cmd);
e0018b
+out_free:
e0018b
+	free(zero_buf);
e0018b
+	return rc;
e0018b
+
e0018b
+}
e0018b
+
e0018b
+static int lsa_op(struct cxl_memdev *memdev, int op, void *buf,
e0018b
+		size_t length, size_t offset)
e0018b
+{
e0018b
+	const char *devname = cxl_memdev_get_devname(memdev);
e0018b
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
e0018b
+	size_t remaining = length, cur_len, cur_off = 0;
e0018b
+	int label_iter_max, rc = 0;
e0018b
+
e0018b
+	if (op != LSA_OP_ZERO && buf == NULL) {
e0018b
+		err(ctx, "%s: LSA buffer cannot be NULL\n", devname);
e0018b
+		return -EINVAL;
e0018b
+	}
e0018b
+
e0018b
+	if (length == 0)
e0018b
+		return 0;
e0018b
+
e0018b
+	label_iter_max = memdev->payload_max - sizeof(struct cxl_cmd_set_lsa);
e0018b
+	while (remaining) {
e0018b
+		cur_len = min((size_t)label_iter_max, remaining);
e0018b
+		rc = __lsa_op(memdev, op, buf + cur_off,
e0018b
+				cur_len, offset + cur_off);
e0018b
+		if (rc)
e0018b
+			break;
e0018b
+
e0018b
+		remaining -= cur_len;
e0018b
+		cur_off += cur_len;
e0018b
+	}
e0018b
+
e0018b
+	if (rc && (op == LSA_OP_SET))
e0018b
+		err(ctx, "%s: labels may be in an inconsistent state\n",
e0018b
+			devname);
e0018b
+	return rc;
e0018b
+}
e0018b
+
e0018b
+CXL_EXPORT int cxl_memdev_zero_label(struct cxl_memdev *memdev, size_t length,
e0018b
+		size_t offset)
e0018b
+{
e0018b
+	return lsa_op(memdev, LSA_OP_ZERO, NULL, length, offset);
e0018b
+}
e0018b
+
e0018b
+CXL_EXPORT int cxl_memdev_write_label(struct cxl_memdev *memdev, void *buf,
e0018b
+		size_t length, size_t offset)
e0018b
+{
e0018b
+	return lsa_op(memdev, LSA_OP_SET, buf, length, offset);
e0018b
+}
e0018b
+
e0018b
+CXL_EXPORT int cxl_memdev_read_label(struct cxl_memdev *memdev, void *buf,
e0018b
+		size_t length, size_t offset)
e0018b
+{
e0018b
+	return lsa_op(memdev, LSA_OP_GET, buf, length, offset);
e0018b
+}
e0018b
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
e0018b
index f3b0c63..077d104 100644
e0018b
--- a/cxl/lib/libcxl.sym
e0018b
+++ b/cxl/lib/libcxl.sym
e0018b
@@ -66,6 +66,10 @@ global:
e0018b
 	cxl_cmd_read_label_get_payload;
e0018b
 	cxl_memdev_get_label_size;
e0018b
 	cxl_memdev_nvdimm_bridge_active;
e0018b
+	cxl_cmd_new_write_label;
e0018b
+	cxl_memdev_zero_label;
e0018b
+	cxl_memdev_write_label;
e0018b
+	cxl_memdev_read_label;
e0018b
 local:
e0018b
         *;
e0018b
 };
e0018b
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
e0018b
index 525c41e..a1b8b50 100644
e0018b
--- a/cxl/lib/private.h
e0018b
+++ b/cxl/lib/private.h
e0018b
@@ -87,6 +87,12 @@ struct cxl_cmd_get_lsa_in {
e0018b
 	le32 length;
e0018b
 } __attribute__((packed));
e0018b
 
e0018b
+struct cxl_cmd_set_lsa {
e0018b
+	le32 offset;
e0018b
+	le32 rsvd;
e0018b
+	unsigned char lsa_data[0];
e0018b
+} __attribute__ ((packed));
e0018b
+
e0018b
 struct cxl_cmd_get_health_info {
e0018b
 	u8 health_status;
e0018b
 	u8 media_status;
e0018b
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
e0018b
index 535e349..89d35ba 100644
e0018b
--- a/cxl/libcxl.h
e0018b
+++ b/cxl/libcxl.h
e0018b
@@ -44,6 +44,12 @@ unsigned long long cxl_memdev_get_ram_size(struct cxl_memdev *memdev);
e0018b
 const char *cxl_memdev_get_firmware_verison(struct cxl_memdev *memdev);
e0018b
 size_t cxl_memdev_get_label_size(struct cxl_memdev *memdev);
e0018b
 int cxl_memdev_nvdimm_bridge_active(struct cxl_memdev *memdev);
e0018b
+int cxl_memdev_zero_label(struct cxl_memdev *memdev, size_t length,
e0018b
+		size_t offset);
e0018b
+int cxl_memdev_read_label(struct cxl_memdev *memdev, void *buf, size_t length,
e0018b
+		size_t offset);
e0018b
+int cxl_memdev_write_label(struct cxl_memdev *memdev, void *buf, size_t length,
e0018b
+		size_t offset);
e0018b
 
e0018b
 #define cxl_memdev_foreach(ctx, memdev) \
e0018b
         for (memdev = cxl_memdev_get_first(ctx); \
e0018b
@@ -101,6 +107,8 @@ struct cxl_cmd *cxl_cmd_new_read_label(struct cxl_memdev *memdev,
e0018b
 		unsigned int offset, unsigned int length);
e0018b
 ssize_t cxl_cmd_read_label_get_payload(struct cxl_cmd *cmd, void *buf,
e0018b
 		unsigned int length);
e0018b
+struct cxl_cmd *cxl_cmd_new_write_label(struct cxl_memdev *memdev,
e0018b
+		void *buf, unsigned int offset, unsigned int length);
e0018b
 
e0018b
 #ifdef __cplusplus
e0018b
 } /* extern "C" */
e0018b
-- 
e0018b
2.27.0
e0018b