|
|
26ccd9 |
From 96afebd1b32ff839129f3bc0ba323ab5f04674ea Mon Sep 17 00:00:00 2001
|
|
|
26ccd9 |
From: Vishal Verma <vishal.l.verma@intel.com>
|
|
|
26ccd9 |
Date: Thu, 7 Oct 2021 02:21:27 -0600
|
|
|
26ccd9 |
Subject: [PATCH 035/217] libcxl: add support for command query and submission
|
|
|
26ccd9 |
|
|
|
26ccd9 |
Add a set of APIs around 'cxl_cmd' for querying the kernel for supported
|
|
|
26ccd9 |
commands, allocating and validating command structures against the
|
|
|
26ccd9 |
supported set, and submitting the commands.
|
|
|
26ccd9 |
|
|
|
26ccd9 |
'Query Commands' and 'Send Command' are implemented as IOCTLs in the
|
|
|
26ccd9 |
kernel. 'Query Commands' returns information about each supported
|
|
|
26ccd9 |
command, such as flags governing its use, or input and output payload
|
|
|
26ccd9 |
sizes. This information is used to validate command support, as well as
|
|
|
26ccd9 |
set up input and output buffers for command submission.
|
|
|
26ccd9 |
|
|
|
26ccd9 |
Cc: Ben Widawsky <ben.widawsky@intel.com>
|
|
|
26ccd9 |
Cc: Dan Williams <dan.j.williams@intel.com>
|
|
|
26ccd9 |
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
|
|
|
26ccd9 |
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
|
|
|
26ccd9 |
---
|
|
|
26ccd9 |
cxl/lib/libcxl.c | 390 +++++++++++++++++++++++++++++++++++++++++++++
|
|
|
26ccd9 |
cxl/lib/libcxl.sym | 9 ++
|
|
|
26ccd9 |
cxl/lib/private.h | 33 ++++
|
|
|
26ccd9 |
cxl/libcxl.h | 11 ++
|
|
|
26ccd9 |
4 files changed, 443 insertions(+)
|
|
|
26ccd9 |
|
|
|
26ccd9 |
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
|
|
|
26ccd9 |
index c15e987..727d599 100644
|
|
|
26ccd9 |
--- a/cxl/lib/libcxl.c
|
|
|
26ccd9 |
+++ b/cxl/lib/libcxl.c
|
|
|
26ccd9 |
@@ -9,14 +9,17 @@
|
|
|
26ccd9 |
#include <unistd.h>
|
|
|
26ccd9 |
#include <sys/stat.h>
|
|
|
26ccd9 |
#include <sys/types.h>
|
|
|
26ccd9 |
+#include <sys/ioctl.h>
|
|
|
26ccd9 |
#include <sys/sysmacros.h>
|
|
|
26ccd9 |
#include <uuid/uuid.h>
|
|
|
26ccd9 |
#include <ccan/list/list.h>
|
|
|
26ccd9 |
#include <ccan/array_size/array_size.h>
|
|
|
26ccd9 |
|
|
|
26ccd9 |
#include <util/log.h>
|
|
|
26ccd9 |
+#include <util/size.h>
|
|
|
26ccd9 |
#include <util/sysfs.h>
|
|
|
26ccd9 |
#include <util/bitmap.h>
|
|
|
26ccd9 |
+#include <cxl/cxl_mem.h>
|
|
|
26ccd9 |
#include <cxl/libcxl.h>
|
|
|
26ccd9 |
#include "private.h"
|
|
|
26ccd9 |
|
|
|
26ccd9 |
@@ -343,3 +346,390 @@ CXL_EXPORT const char *cxl_memdev_get_firmware_verison(struct cxl_memdev *memdev
|
|
|
26ccd9 |
{
|
|
|
26ccd9 |
return memdev->firmware_version;
|
|
|
26ccd9 |
}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT void cxl_cmd_unref(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ if (!cmd)
|
|
|
26ccd9 |
+ return;
|
|
|
26ccd9 |
+ if (--cmd->refcount == 0) {
|
|
|
26ccd9 |
+ free(cmd->query_cmd);
|
|
|
26ccd9 |
+ free(cmd->send_cmd);
|
|
|
26ccd9 |
+ free(cmd->input_payload);
|
|
|
26ccd9 |
+ free(cmd->output_payload);
|
|
|
26ccd9 |
+ free(cmd);
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT void cxl_cmd_ref(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ cmd->refcount++;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int cxl_cmd_alloc_query(struct cxl_cmd *cmd, int num_cmds)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ size_t size;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (!cmd)
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (cmd->query_cmd != NULL)
|
|
|
26ccd9 |
+ free(cmd->query_cmd);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ size = struct_size(cmd->query_cmd, commands, num_cmds);
|
|
|
26ccd9 |
+ if (size == SIZE_MAX)
|
|
|
26ccd9 |
+ return -EOVERFLOW;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->query_cmd = calloc(1, size);
|
|
|
26ccd9 |
+ if (!cmd->query_cmd)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->query_cmd->n_commands = num_cmds;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static struct cxl_cmd *cxl_cmd_new(struct cxl_memdev *memdev)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_cmd *cmd;
|
|
|
26ccd9 |
+ size_t size;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ size = sizeof(*cmd);
|
|
|
26ccd9 |
+ cmd = calloc(1, size);
|
|
|
26ccd9 |
+ if (!cmd)
|
|
|
26ccd9 |
+ return NULL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cxl_cmd_ref(cmd);
|
|
|
26ccd9 |
+ cmd->memdev = memdev;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return cmd;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int __do_cmd(struct cxl_cmd *cmd, int ioctl_cmd, int fd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ void *cmd_buf;
|
|
|
26ccd9 |
+ int rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ switch (ioctl_cmd) {
|
|
|
26ccd9 |
+ case CXL_MEM_QUERY_COMMANDS:
|
|
|
26ccd9 |
+ cmd_buf = cmd->query_cmd;
|
|
|
26ccd9 |
+ break;
|
|
|
26ccd9 |
+ case CXL_MEM_SEND_COMMAND:
|
|
|
26ccd9 |
+ cmd_buf = cmd->send_cmd;
|
|
|
26ccd9 |
+ break;
|
|
|
26ccd9 |
+ default:
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = ioctl(fd, ioctl_cmd, cmd_buf);
|
|
|
26ccd9 |
+ if (rc < 0)
|
|
|
26ccd9 |
+ rc = -errno;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int do_cmd(struct cxl_cmd *cmd, int ioctl_cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ char *path;
|
|
|
26ccd9 |
+ struct stat st;
|
|
|
26ccd9 |
+ unsigned int major, minor;
|
|
|
26ccd9 |
+ int rc = 0, fd;
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
|
|
|
26ccd9 |
+ const char *devname = cxl_memdev_get_devname(memdev);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ major = cxl_memdev_get_major(memdev);
|
|
|
26ccd9 |
+ minor = cxl_memdev_get_minor(memdev);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (asprintf(&path, "/dev/cxl/%s", devname) < 0)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ fd = open(path, O_RDWR);
|
|
|
26ccd9 |
+ if (fd < 0) {
|
|
|
26ccd9 |
+ err(ctx, "failed to open %s: %s\n", path, strerror(errno));
|
|
|
26ccd9 |
+ rc = -errno;
|
|
|
26ccd9 |
+ goto out;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (fstat(fd, &st) >= 0 && S_ISCHR(st.st_mode)
|
|
|
26ccd9 |
+ && major(st.st_rdev) == major
|
|
|
26ccd9 |
+ && minor(st.st_rdev) == minor) {
|
|
|
26ccd9 |
+ rc = __do_cmd(cmd, ioctl_cmd, fd);
|
|
|
26ccd9 |
+ } else {
|
|
|
26ccd9 |
+ err(ctx, "failed to validate %s as a CXL memdev node\n", path);
|
|
|
26ccd9 |
+ rc = -ENXIO;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+ close(fd);
|
|
|
26ccd9 |
+out:
|
|
|
26ccd9 |
+ free(path);
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int alloc_do_query(struct cxl_cmd *cmd, int num_cmds)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(cmd->memdev);
|
|
|
26ccd9 |
+ int rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = cxl_cmd_alloc_query(cmd, num_cmds);
|
|
|
26ccd9 |
+ if (rc)
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = do_cmd(cmd, CXL_MEM_QUERY_COMMANDS);
|
|
|
26ccd9 |
+ if (rc < 0)
|
|
|
26ccd9 |
+ err(ctx, "%s: query commands failed: %s\n",
|
|
|
26ccd9 |
+ cxl_memdev_get_devname(cmd->memdev),
|
|
|
26ccd9 |
+ strerror(-rc));
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int cxl_cmd_do_query(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
|
|
|
26ccd9 |
+ const char *devname = cxl_memdev_get_devname(memdev);
|
|
|
26ccd9 |
+ int rc, n_commands;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ switch (cmd->query_status) {
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_OK:
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_UNSUPPORTED:
|
|
|
26ccd9 |
+ return -EOPNOTSUPP;
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_NOT_RUN:
|
|
|
26ccd9 |
+ break;
|
|
|
26ccd9 |
+ default:
|
|
|
26ccd9 |
+ err(ctx, "%s: Unknown query_status %d\n",
|
|
|
26ccd9 |
+ devname, cmd->query_status);
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = alloc_do_query(cmd, 0);
|
|
|
26ccd9 |
+ if (rc)
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ n_commands = cmd->query_cmd->n_commands;
|
|
|
26ccd9 |
+ dbg(ctx, "%s: supports %d commands\n", devname, n_commands);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return alloc_do_query(cmd, n_commands);
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int cxl_cmd_validate(struct cxl_cmd *cmd, u32 cmd_id)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+ struct cxl_mem_query_commands *query = cmd->query_cmd;
|
|
|
26ccd9 |
+ const char *devname = cxl_memdev_get_devname(memdev);
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
|
|
|
26ccd9 |
+ u32 i;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ for (i = 0; i < query->n_commands; i++) {
|
|
|
26ccd9 |
+ struct cxl_command_info *cinfo = &query->commands[i];
|
|
|
26ccd9 |
+ const char *cmd_name = cxl_command_names[cinfo->id].name;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (cinfo->id != cmd_id)
|
|
|
26ccd9 |
+ continue;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ dbg(ctx, "%s: %s: in: %d, out %d, flags: %#08x\n",
|
|
|
26ccd9 |
+ devname, cmd_name, cinfo->size_in,
|
|
|
26ccd9 |
+ cinfo->size_out, cinfo->flags);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->query_idx = i;
|
|
|
26ccd9 |
+ cmd->query_status = CXL_CMD_QUERY_OK;
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+ cmd->query_status = CXL_CMD_QUERY_UNSUPPORTED;
|
|
|
26ccd9 |
+ return -EOPNOTSUPP;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT int cxl_cmd_set_input_payload(struct cxl_cmd *cmd, void *buf,
|
|
|
26ccd9 |
+ int size)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (size > memdev->payload_max || size < 0)
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (!buf) {
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ /* If the user didn't supply a buffer, allocate it */
|
|
|
26ccd9 |
+ cmd->input_payload = calloc(1, size);
|
|
|
26ccd9 |
+ if (!cmd->input_payload)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+ cmd->send_cmd->in.payload = (u64)cmd->input_payload;
|
|
|
26ccd9 |
+ } else {
|
|
|
26ccd9 |
+ /*
|
|
|
26ccd9 |
+ * Use user-buffer as is. If an automatic allocation was
|
|
|
26ccd9 |
+ * previously made (based on a fixed size from query),
|
|
|
26ccd9 |
+ * it will get freed during unref.
|
|
|
26ccd9 |
+ */
|
|
|
26ccd9 |
+ cmd->send_cmd->in.payload = (u64)buf;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+ cmd->send_cmd->in.size = size;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT int cxl_cmd_set_output_payload(struct cxl_cmd *cmd, void *buf,
|
|
|
26ccd9 |
+ int size)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (size > memdev->payload_max || size < 0)
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (!buf) {
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ /* If the user didn't supply a buffer, allocate it */
|
|
|
26ccd9 |
+ cmd->output_payload = calloc(1, size);
|
|
|
26ccd9 |
+ if (!cmd->output_payload)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+ cmd->send_cmd->out.payload = (u64)cmd->output_payload;
|
|
|
26ccd9 |
+ } else {
|
|
|
26ccd9 |
+ /*
|
|
|
26ccd9 |
+ * Use user-buffer as is. If an automatic allocation was
|
|
|
26ccd9 |
+ * previously made (based on a fixed size from query),
|
|
|
26ccd9 |
+ * it will get freed during unref.
|
|
|
26ccd9 |
+ */
|
|
|
26ccd9 |
+ cmd->send_cmd->out.payload = (u64)buf;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+ cmd->send_cmd->out.size = size;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static int cxl_cmd_alloc_send(struct cxl_cmd *cmd, u32 cmd_id)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_mem_query_commands *query = cmd->query_cmd;
|
|
|
26ccd9 |
+ struct cxl_command_info *cinfo = &query->commands[cmd->query_idx];
|
|
|
26ccd9 |
+ size_t size;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (!query)
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ size = sizeof(struct cxl_send_command);
|
|
|
26ccd9 |
+ cmd->send_cmd = calloc(1, size);
|
|
|
26ccd9 |
+ if (!cmd->send_cmd)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (cinfo->id != cmd_id)
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->send_cmd->id = cmd_id;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ if (cinfo->size_in > 0) {
|
|
|
26ccd9 |
+ cmd->input_payload = calloc(1, cinfo->size_in);
|
|
|
26ccd9 |
+ if (!cmd->input_payload)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+ cmd->send_cmd->in.payload = (u64)cmd->input_payload;
|
|
|
26ccd9 |
+ cmd->send_cmd->in.size = cinfo->size_in;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+ if (cinfo->size_out > 0) {
|
|
|
26ccd9 |
+ cmd->output_payload = calloc(1, cinfo->size_out);
|
|
|
26ccd9 |
+ if (!cmd->output_payload)
|
|
|
26ccd9 |
+ return -ENOMEM;
|
|
|
26ccd9 |
+ cmd->send_cmd->out.payload = (u64)cmd->output_payload;
|
|
|
26ccd9 |
+ cmd->send_cmd->out.size = cinfo->size_out;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return 0;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+static struct cxl_cmd *cxl_cmd_new_generic(struct cxl_memdev *memdev,
|
|
|
26ccd9 |
+ u32 cmd_id)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ const char *devname = cxl_memdev_get_devname(memdev);
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
|
|
|
26ccd9 |
+ struct cxl_cmd *cmd;
|
|
|
26ccd9 |
+ int rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd = cxl_cmd_new(memdev);
|
|
|
26ccd9 |
+ if (!cmd)
|
|
|
26ccd9 |
+ return NULL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = cxl_cmd_do_query(cmd);
|
|
|
26ccd9 |
+ if (rc) {
|
|
|
26ccd9 |
+ err(ctx, "%s: query returned: %s\n", devname, strerror(-rc));
|
|
|
26ccd9 |
+ goto fail;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = cxl_cmd_validate(cmd, cmd_id);
|
|
|
26ccd9 |
+ if (rc) {
|
|
|
26ccd9 |
+ errno = -rc;
|
|
|
26ccd9 |
+ goto fail;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ rc = cxl_cmd_alloc_send(cmd, cmd_id);
|
|
|
26ccd9 |
+ if (rc) {
|
|
|
26ccd9 |
+ errno = -rc;
|
|
|
26ccd9 |
+ goto fail;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->status = 1;
|
|
|
26ccd9 |
+ return cmd;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+fail:
|
|
|
26ccd9 |
+ cxl_cmd_unref(cmd);
|
|
|
26ccd9 |
+ return NULL;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT const char *cxl_cmd_get_devname(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ return cxl_memdev_get_devname(cmd->memdev);
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT struct cxl_cmd *cxl_cmd_new_raw(struct cxl_memdev *memdev,
|
|
|
26ccd9 |
+ int opcode)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_cmd *cmd;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ /* opcode '0' is reserved */
|
|
|
26ccd9 |
+ if (opcode <= 0) {
|
|
|
26ccd9 |
+ errno = EINVAL;
|
|
|
26ccd9 |
+ return NULL;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd = cxl_cmd_new_generic(memdev, CXL_MEM_COMMAND_ID_RAW);
|
|
|
26ccd9 |
+ if (!cmd)
|
|
|
26ccd9 |
+ return NULL;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ cmd->send_cmd->raw.opcode = opcode;
|
|
|
26ccd9 |
+ return cmd;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT int cxl_cmd_submit(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev = cmd->memdev;
|
|
|
26ccd9 |
+ const char *devname = cxl_memdev_get_devname(memdev);
|
|
|
26ccd9 |
+ struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
|
|
|
26ccd9 |
+ int rc;
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ switch (cmd->query_status) {
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_OK:
|
|
|
26ccd9 |
+ break;
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_UNSUPPORTED:
|
|
|
26ccd9 |
+ return -EOPNOTSUPP;
|
|
|
26ccd9 |
+ case CXL_CMD_QUERY_NOT_RUN:
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+ default:
|
|
|
26ccd9 |
+ err(ctx, "%s: Unknown query_status %d\n",
|
|
|
26ccd9 |
+ devname, cmd->query_status);
|
|
|
26ccd9 |
+ return -EINVAL;
|
|
|
26ccd9 |
+ }
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ dbg(ctx, "%s: submitting SEND cmd: in: %d, out: %d\n", devname,
|
|
|
26ccd9 |
+ cmd->send_cmd->in.size, cmd->send_cmd->out.size);
|
|
|
26ccd9 |
+ rc = do_cmd(cmd, CXL_MEM_SEND_COMMAND);
|
|
|
26ccd9 |
+ cmd->status = cmd->send_cmd->retval;
|
|
|
26ccd9 |
+ dbg(ctx, "%s: got SEND cmd: in: %d, out: %d, retval: %d, status: %d\n",
|
|
|
26ccd9 |
+ devname, cmd->send_cmd->in.size, cmd->send_cmd->out.size,
|
|
|
26ccd9 |
+ rc, cmd->status);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+ return rc;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT int cxl_cmd_get_mbox_status(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ return cmd->status;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+CXL_EXPORT int cxl_cmd_get_out_size(struct cxl_cmd *cmd)
|
|
|
26ccd9 |
+{
|
|
|
26ccd9 |
+ return cmd->send_cmd->out.size;
|
|
|
26ccd9 |
+}
|
|
|
26ccd9 |
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
|
|
|
26ccd9 |
index 2616e5c..3900f90 100644
|
|
|
26ccd9 |
--- a/cxl/lib/libcxl.sym
|
|
|
26ccd9 |
+++ b/cxl/lib/libcxl.sym
|
|
|
26ccd9 |
@@ -20,6 +20,15 @@ global:
|
|
|
26ccd9 |
cxl_memdev_get_pmem_size;
|
|
|
26ccd9 |
cxl_memdev_get_ram_size;
|
|
|
26ccd9 |
cxl_memdev_get_firmware_verison;
|
|
|
26ccd9 |
+ cxl_cmd_get_devname;
|
|
|
26ccd9 |
+ cxl_cmd_new_raw;
|
|
|
26ccd9 |
+ cxl_cmd_set_input_payload;
|
|
|
26ccd9 |
+ cxl_cmd_set_output_payload;
|
|
|
26ccd9 |
+ cxl_cmd_ref;
|
|
|
26ccd9 |
+ cxl_cmd_unref;
|
|
|
26ccd9 |
+ cxl_cmd_submit;
|
|
|
26ccd9 |
+ cxl_cmd_get_mbox_status;
|
|
|
26ccd9 |
+ cxl_cmd_get_out_size;
|
|
|
26ccd9 |
local:
|
|
|
26ccd9 |
*;
|
|
|
26ccd9 |
};
|
|
|
26ccd9 |
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
|
|
|
26ccd9 |
index fc88fa1..87ca17e 100644
|
|
|
26ccd9 |
--- a/cxl/lib/private.h
|
|
|
26ccd9 |
+++ b/cxl/lib/private.h
|
|
|
26ccd9 |
@@ -4,6 +4,9 @@
|
|
|
26ccd9 |
#define _LIBCXL_PRIVATE_H_
|
|
|
26ccd9 |
|
|
|
26ccd9 |
#include <libkmod.h>
|
|
|
26ccd9 |
+#include <cxl/cxl_mem.h>
|
|
|
26ccd9 |
+#include <ccan/endian/endian.h>
|
|
|
26ccd9 |
+#include <ccan/short_types/short_types.h>
|
|
|
26ccd9 |
|
|
|
26ccd9 |
#define CXL_EXPORT __attribute__ ((visibility("default")))
|
|
|
26ccd9 |
|
|
|
26ccd9 |
@@ -21,6 +24,36 @@ struct cxl_memdev {
|
|
|
26ccd9 |
struct kmod_module *module;
|
|
|
26ccd9 |
};
|
|
|
26ccd9 |
|
|
|
26ccd9 |
+enum cxl_cmd_query_status {
|
|
|
26ccd9 |
+ CXL_CMD_QUERY_NOT_RUN = 0,
|
|
|
26ccd9 |
+ CXL_CMD_QUERY_OK,
|
|
|
26ccd9 |
+ CXL_CMD_QUERY_UNSUPPORTED,
|
|
|
26ccd9 |
+};
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
+/**
|
|
|
26ccd9 |
+ * struct cxl_cmd - CXL memdev command
|
|
|
26ccd9 |
+ * @memdev: the memory device to which the command is being sent
|
|
|
26ccd9 |
+ * @query_cmd: structure for the Linux 'Query commands' ioctl
|
|
|
26ccd9 |
+ * @send_cmd: structure for the Linux 'Send command' ioctl
|
|
|
26ccd9 |
+ * @input_payload: buffer for input payload managed by libcxl
|
|
|
26ccd9 |
+ * @output_payload: buffer for output payload managed by libcxl
|
|
|
26ccd9 |
+ * @refcount: reference for passing command buffer around
|
|
|
26ccd9 |
+ * @query_status: status from query_commands
|
|
|
26ccd9 |
+ * @query_idx: index of 'this' command in the query_commands array
|
|
|
26ccd9 |
+ * @status: command return status from the device
|
|
|
26ccd9 |
+ */
|
|
|
26ccd9 |
+struct cxl_cmd {
|
|
|
26ccd9 |
+ struct cxl_memdev *memdev;
|
|
|
26ccd9 |
+ struct cxl_mem_query_commands *query_cmd;
|
|
|
26ccd9 |
+ struct cxl_send_command *send_cmd;
|
|
|
26ccd9 |
+ void *input_payload;
|
|
|
26ccd9 |
+ void *output_payload;
|
|
|
26ccd9 |
+ int refcount;
|
|
|
26ccd9 |
+ int query_status;
|
|
|
26ccd9 |
+ int query_idx;
|
|
|
26ccd9 |
+ int status;
|
|
|
26ccd9 |
+};
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
static inline int check_kmod(struct kmod_ctx *kmod_ctx)
|
|
|
26ccd9 |
{
|
|
|
26ccd9 |
return kmod_ctx ? 0 : -ENXIO;
|
|
|
26ccd9 |
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
|
|
|
26ccd9 |
index fd06790..6e87b80 100644
|
|
|
26ccd9 |
--- a/cxl/libcxl.h
|
|
|
26ccd9 |
+++ b/cxl/libcxl.h
|
|
|
26ccd9 |
@@ -48,6 +48,17 @@ const char *cxl_memdev_get_firmware_verison(struct cxl_memdev *memdev);
|
|
|
26ccd9 |
memdev != NULL; \
|
|
|
26ccd9 |
memdev = cxl_memdev_get_next(memdev))
|
|
|
26ccd9 |
|
|
|
26ccd9 |
+struct cxl_cmd;
|
|
|
26ccd9 |
+const char *cxl_cmd_get_devname(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+struct cxl_cmd *cxl_cmd_new_raw(struct cxl_memdev *memdev, int opcode);
|
|
|
26ccd9 |
+int cxl_cmd_set_input_payload(struct cxl_cmd *cmd, void *in, int size);
|
|
|
26ccd9 |
+int cxl_cmd_set_output_payload(struct cxl_cmd *cmd, void *out, int size);
|
|
|
26ccd9 |
+void cxl_cmd_ref(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+void cxl_cmd_unref(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+int cxl_cmd_submit(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+int cxl_cmd_get_mbox_status(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+int cxl_cmd_get_out_size(struct cxl_cmd *cmd);
|
|
|
26ccd9 |
+
|
|
|
26ccd9 |
#ifdef __cplusplus
|
|
|
26ccd9 |
} /* extern "C" */
|
|
|
26ccd9 |
#endif
|
|
|
26ccd9 |
--
|
|
|
26ccd9 |
2.27.0
|
|
|
26ccd9 |
|