xref: /linux/drivers/cxl/core/mbox.c (revision 63cf60b7e0a556b3542e6e915dfe9c93eaa559fd)
14faf31b4SDan Williams // SPDX-License-Identifier: GPL-2.0-only
24faf31b4SDan Williams /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
34faf31b4SDan Williams #include <linux/io-64-nonatomic-lo-hi.h>
44faf31b4SDan Williams #include <linux/security.h>
54faf31b4SDan Williams #include <linux/debugfs.h>
64faf31b4SDan Williams #include <linux/mutex.h>
74faf31b4SDan Williams #include <cxlmem.h>
84faf31b4SDan Williams #include <cxl.h>
94faf31b4SDan Williams 
104faf31b4SDan Williams #include "core.h"
114faf31b4SDan Williams 
124faf31b4SDan Williams static bool cxl_raw_allow_all;
134faf31b4SDan Williams 
144faf31b4SDan Williams /**
154faf31b4SDan Williams  * DOC: cxl mbox
164faf31b4SDan Williams  *
174faf31b4SDan Williams  * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The
184faf31b4SDan Williams  * implementation is used by the cxl_pci driver to initialize the device
194faf31b4SDan Williams  * and implement the cxl_mem.h IOCTL UAPI. It also implements the
204faf31b4SDan Williams  * backend of the cxl_pmem_ctl() transport for LIBNVDIMM.
214faf31b4SDan Williams  */
224faf31b4SDan Williams 
234faf31b4SDan Williams #define cxl_for_each_cmd(cmd)                                                  \
244faf31b4SDan Williams 	for ((cmd) = &cxl_mem_commands[0];                                     \
254faf31b4SDan Williams 	     ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++)
264faf31b4SDan Williams 
274faf31b4SDan Williams #define CXL_CMD(_id, sin, sout, _flags)                                        \
284faf31b4SDan Williams 	[CXL_MEM_COMMAND_ID_##_id] = {                                         \
294faf31b4SDan Williams 	.info =	{                                                              \
304faf31b4SDan Williams 			.id = CXL_MEM_COMMAND_ID_##_id,                        \
314faf31b4SDan Williams 			.size_in = sin,                                        \
324faf31b4SDan Williams 			.size_out = sout,                                      \
334faf31b4SDan Williams 		},                                                             \
344faf31b4SDan Williams 	.opcode = CXL_MBOX_OP_##_id,                                           \
354faf31b4SDan Williams 	.flags = _flags,                                                       \
364faf31b4SDan Williams 	}
374faf31b4SDan Williams 
384faf31b4SDan Williams /*
394faf31b4SDan Williams  * This table defines the supported mailbox commands for the driver. This table
404faf31b4SDan Williams  * is made up of a UAPI structure. Non-negative values as parameters in the
414faf31b4SDan Williams  * table will be validated against the user's input. For example, if size_in is
424faf31b4SDan Williams  * 0, and the user passed in 1, it is an error.
434faf31b4SDan Williams  */
444faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
454faf31b4SDan Williams 	CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE),
464faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS
474faf31b4SDan Williams 	CXL_CMD(RAW, ~0, ~0, 0),
484faf31b4SDan Williams #endif
494faf31b4SDan Williams 	CXL_CMD(GET_SUPPORTED_LOGS, 0, ~0, CXL_CMD_FLAG_FORCE_ENABLE),
504faf31b4SDan Williams 	CXL_CMD(GET_FW_INFO, 0, 0x50, 0),
514faf31b4SDan Williams 	CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0),
524faf31b4SDan Williams 	CXL_CMD(GET_LSA, 0x8, ~0, 0),
534faf31b4SDan Williams 	CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0),
544faf31b4SDan Williams 	CXL_CMD(GET_LOG, 0x18, ~0, CXL_CMD_FLAG_FORCE_ENABLE),
554faf31b4SDan Williams 	CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0),
564faf31b4SDan Williams 	CXL_CMD(SET_LSA, ~0, 0, 0),
574faf31b4SDan Williams 	CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
584faf31b4SDan Williams 	CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0),
594faf31b4SDan Williams 	CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0),
604faf31b4SDan Williams 	CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0),
614faf31b4SDan Williams 	CXL_CMD(GET_POISON, 0x10, ~0, 0),
624faf31b4SDan Williams 	CXL_CMD(INJECT_POISON, 0x8, 0, 0),
634faf31b4SDan Williams 	CXL_CMD(CLEAR_POISON, 0x48, 0, 0),
644faf31b4SDan Williams 	CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0),
654faf31b4SDan Williams 	CXL_CMD(SCAN_MEDIA, 0x11, 0, 0),
664faf31b4SDan Williams 	CXL_CMD(GET_SCAN_MEDIA, 0, ~0, 0),
674faf31b4SDan Williams };
684faf31b4SDan Williams 
694faf31b4SDan Williams /*
704faf31b4SDan Williams  * Commands that RAW doesn't permit. The rationale for each:
714faf31b4SDan Williams  *
724faf31b4SDan Williams  * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment /
734faf31b4SDan Williams  * coordination of transaction timeout values at the root bridge level.
744faf31b4SDan Williams  *
754faf31b4SDan Williams  * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live
764faf31b4SDan Williams  * and needs to be coordinated with HDM updates.
774faf31b4SDan Williams  *
784faf31b4SDan Williams  * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the
794faf31b4SDan Williams  * driver and any writes from userspace invalidates those contents.
804faf31b4SDan Williams  *
814faf31b4SDan Williams  * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes
824faf31b4SDan Williams  * to the device after it is marked clean, userspace can not make that
834faf31b4SDan Williams  * assertion.
844faf31b4SDan Williams  *
854faf31b4SDan Williams  * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that
864faf31b4SDan Williams  * is kept up to date with patrol notifications and error management.
874faf31b4SDan Williams  */
884faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = {
894faf31b4SDan Williams 	CXL_MBOX_OP_ACTIVATE_FW,
904faf31b4SDan Williams 	CXL_MBOX_OP_SET_PARTITION_INFO,
914faf31b4SDan Williams 	CXL_MBOX_OP_SET_LSA,
924faf31b4SDan Williams 	CXL_MBOX_OP_SET_SHUTDOWN_STATE,
934faf31b4SDan Williams 	CXL_MBOX_OP_SCAN_MEDIA,
944faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA,
954faf31b4SDan Williams };
964faf31b4SDan Williams 
974faf31b4SDan Williams /*
984faf31b4SDan Williams  * Command sets that RAW doesn't permit. All opcodes in this set are
994faf31b4SDan Williams  * disabled because they pass plain text security payloads over the
1004faf31b4SDan Williams  * user/kernel boundary. This functionality is intended to be wrapped
1014faf31b4SDan Williams  * behind the keys ABI which allows for encrypted payloads in the UAPI
1024faf31b4SDan Williams  */
1034faf31b4SDan Williams static u8 security_command_sets[] = {
1044faf31b4SDan Williams 	0x44, /* Sanitize */
1054faf31b4SDan Williams 	0x45, /* Persistent Memory Data-at-rest Security */
1064faf31b4SDan Williams 	0x46, /* Security Passthrough */
1074faf31b4SDan Williams };
1084faf31b4SDan Williams 
1094faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode)
1104faf31b4SDan Williams {
1114faf31b4SDan Williams 	int i;
1124faf31b4SDan Williams 
1134faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(security_command_sets); i++)
1144faf31b4SDan Williams 		if (security_command_sets[i] == (opcode >> 8))
1154faf31b4SDan Williams 			return true;
1164faf31b4SDan Williams 	return false;
1174faf31b4SDan Williams }
1184faf31b4SDan Williams 
1194faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
1204faf31b4SDan Williams {
1214faf31b4SDan Williams 	struct cxl_mem_command *c;
1224faf31b4SDan Williams 
1234faf31b4SDan Williams 	cxl_for_each_cmd(c)
1244faf31b4SDan Williams 		if (c->opcode == opcode)
1254faf31b4SDan Williams 			return c;
1264faf31b4SDan Williams 
1274faf31b4SDan Williams 	return NULL;
1284faf31b4SDan Williams }
1294faf31b4SDan Williams 
1304faf31b4SDan Williams /**
1315e2411aeSIra Weiny  * cxl_mbox_send_cmd() - Send a mailbox command to a device.
1325e2411aeSIra Weiny  * @cxlds: The device data for the operation
1334faf31b4SDan Williams  * @opcode: Opcode for the mailbox command.
1344faf31b4SDan Williams  * @in: The input payload for the mailbox command.
1354faf31b4SDan Williams  * @in_size: The length of the input payload
1364faf31b4SDan Williams  * @out: Caller allocated buffer for the output.
1374faf31b4SDan Williams  * @out_size: Expected size of output.
1384faf31b4SDan Williams  *
1394faf31b4SDan Williams  * Context: Any context. Will acquire and release mbox_mutex.
1404faf31b4SDan Williams  * Return:
1414faf31b4SDan Williams  *  * %>=0	- Number of bytes returned in @out.
1424faf31b4SDan Williams  *  * %-E2BIG	- Payload is too large for hardware.
1434faf31b4SDan Williams  *  * %-EBUSY	- Couldn't acquire exclusive mailbox access.
1444faf31b4SDan Williams  *  * %-EFAULT	- Hardware error occurred.
1454faf31b4SDan Williams  *  * %-ENXIO	- Command completed, but device reported an error.
1464faf31b4SDan Williams  *  * %-EIO	- Unexpected output size.
1474faf31b4SDan Williams  *
1484faf31b4SDan Williams  * Mailbox commands may execute successfully yet the device itself reported an
1494faf31b4SDan Williams  * error. While this distinction can be useful for commands from userspace, the
1504faf31b4SDan Williams  * kernel will only be able to use results when both are successful.
1514faf31b4SDan Williams  */
1525e2411aeSIra Weiny int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in,
1534faf31b4SDan Williams 		      size_t in_size, void *out, size_t out_size)
1544faf31b4SDan Williams {
1554faf31b4SDan Williams 	const struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
1564faf31b4SDan Williams 	struct cxl_mbox_cmd mbox_cmd = {
1574faf31b4SDan Williams 		.opcode = opcode,
1584faf31b4SDan Williams 		.payload_in = in,
1594faf31b4SDan Williams 		.size_in = in_size,
1604faf31b4SDan Williams 		.size_out = out_size,
1614faf31b4SDan Williams 		.payload_out = out,
1624faf31b4SDan Williams 	};
1634faf31b4SDan Williams 	int rc;
1644faf31b4SDan Williams 
1655e2411aeSIra Weiny 	if (out_size > cxlds->payload_size)
1664faf31b4SDan Williams 		return -E2BIG;
1674faf31b4SDan Williams 
1685e2411aeSIra Weiny 	rc = cxlds->mbox_send(cxlds, &mbox_cmd);
1694faf31b4SDan Williams 	if (rc)
1704faf31b4SDan Williams 		return rc;
1714faf31b4SDan Williams 
1724faf31b4SDan Williams 	/* TODO: Map return code to proper kernel style errno */
1734faf31b4SDan Williams 	if (mbox_cmd.return_code != CXL_MBOX_SUCCESS)
1744faf31b4SDan Williams 		return -ENXIO;
1754faf31b4SDan Williams 
1764faf31b4SDan Williams 	/*
1774faf31b4SDan Williams 	 * Variable sized commands can't be validated and so it's up to the
1784faf31b4SDan Williams 	 * caller to do that if they wish.
1794faf31b4SDan Williams 	 */
1804faf31b4SDan Williams 	if (cmd->info.size_out >= 0 && mbox_cmd.size_out != out_size)
1814faf31b4SDan Williams 		return -EIO;
1824faf31b4SDan Williams 
1834faf31b4SDan Williams 	return 0;
1844faf31b4SDan Williams }
185affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mbox_send_cmd, CXL);
1864faf31b4SDan Williams 
1874faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode)
1884faf31b4SDan Williams {
1894faf31b4SDan Williams 	int i;
1904faf31b4SDan Williams 
1914faf31b4SDan Williams 	if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS))
1924faf31b4SDan Williams 		return false;
1934faf31b4SDan Williams 
1944faf31b4SDan Williams 	if (security_locked_down(LOCKDOWN_PCI_ACCESS))
1954faf31b4SDan Williams 		return false;
1964faf31b4SDan Williams 
1974faf31b4SDan Williams 	if (cxl_raw_allow_all)
1984faf31b4SDan Williams 		return true;
1994faf31b4SDan Williams 
2004faf31b4SDan Williams 	if (cxl_is_security_command(opcode))
2014faf31b4SDan Williams 		return false;
2024faf31b4SDan Williams 
2034faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++)
2044faf31b4SDan Williams 		if (cxl_disabled_raw_commands[i] == opcode)
2054faf31b4SDan Williams 			return false;
2064faf31b4SDan Williams 
2074faf31b4SDan Williams 	return true;
2084faf31b4SDan Williams }
2094faf31b4SDan Williams 
210*63cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
211*63cf60b7SAlison Schofield 			     struct cxl_dev_state *cxlds, u16 opcode,
212*63cf60b7SAlison Schofield 			     size_t in_size, size_t out_size, u64 in_payload)
213*63cf60b7SAlison Schofield {
214*63cf60b7SAlison Schofield 	*mbox = (struct cxl_mbox_cmd) {
215*63cf60b7SAlison Schofield 		.opcode = opcode,
216*63cf60b7SAlison Schofield 		.size_in = in_size,
217*63cf60b7SAlison Schofield 	};
218*63cf60b7SAlison Schofield 
219*63cf60b7SAlison Schofield 	if (in_size) {
220*63cf60b7SAlison Schofield 		mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload),
221*63cf60b7SAlison Schofield 						in_size);
222*63cf60b7SAlison Schofield 		if (!mbox->payload_in)
223*63cf60b7SAlison Schofield 			return PTR_ERR(mbox->payload_in);
224*63cf60b7SAlison Schofield 	}
225*63cf60b7SAlison Schofield 
226*63cf60b7SAlison Schofield 	/* Prepare to handle a full payload for variable sized output */
227*63cf60b7SAlison Schofield 	if (out_size < 0)
228*63cf60b7SAlison Schofield 		mbox->size_out = cxlds->payload_size;
229*63cf60b7SAlison Schofield 	else
230*63cf60b7SAlison Schofield 		mbox->size_out = out_size;
231*63cf60b7SAlison Schofield 
232*63cf60b7SAlison Schofield 	if (mbox->size_out) {
233*63cf60b7SAlison Schofield 		mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL);
234*63cf60b7SAlison Schofield 		if (!mbox->payload_out) {
235*63cf60b7SAlison Schofield 			kvfree(mbox->payload_in);
236*63cf60b7SAlison Schofield 			return -ENOMEM;
237*63cf60b7SAlison Schofield 		}
238*63cf60b7SAlison Schofield 	}
239*63cf60b7SAlison Schofield 	return 0;
240*63cf60b7SAlison Schofield }
241*63cf60b7SAlison Schofield 
242*63cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox)
243*63cf60b7SAlison Schofield {
244*63cf60b7SAlison Schofield 	kvfree(mbox->payload_in);
245*63cf60b7SAlison Schofield 	kvfree(mbox->payload_out);
246*63cf60b7SAlison Schofield }
247*63cf60b7SAlison Schofield 
2486dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,
2496dd0e5ccSAlison Schofield 			      const struct cxl_send_command *send_cmd,
2506dd0e5ccSAlison Schofield 			      struct cxl_dev_state *cxlds)
2516dd0e5ccSAlison Schofield {
2526dd0e5ccSAlison Schofield 	if (send_cmd->raw.rsvd)
2536dd0e5ccSAlison Schofield 		return -EINVAL;
2546dd0e5ccSAlison Schofield 
2556dd0e5ccSAlison Schofield 	/*
2566dd0e5ccSAlison Schofield 	 * Unlike supported commands, the output size of RAW commands
2576dd0e5ccSAlison Schofield 	 * gets passed along without further checking, so it must be
2586dd0e5ccSAlison Schofield 	 * validated here.
2596dd0e5ccSAlison Schofield 	 */
2606dd0e5ccSAlison Schofield 	if (send_cmd->out.size > cxlds->payload_size)
2616dd0e5ccSAlison Schofield 		return -EINVAL;
2626dd0e5ccSAlison Schofield 
2636dd0e5ccSAlison Schofield 	if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode))
2646dd0e5ccSAlison Schofield 		return -EPERM;
2656dd0e5ccSAlison Schofield 
26639ed8da4SAlison Schofield 	dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n");
26739ed8da4SAlison Schofield 
2686dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
2696dd0e5ccSAlison Schofield 		.info = {
2706dd0e5ccSAlison Schofield 			.id = CXL_MEM_COMMAND_ID_RAW,
2716dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
2726dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
2736dd0e5ccSAlison Schofield 		},
2746dd0e5ccSAlison Schofield 		.opcode = send_cmd->raw.opcode
2756dd0e5ccSAlison Schofield 	};
2766dd0e5ccSAlison Schofield 
2776dd0e5ccSAlison Schofield 	return 0;
2786dd0e5ccSAlison Schofield }
2796dd0e5ccSAlison Schofield 
2806dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,
2816dd0e5ccSAlison Schofield 			  const struct cxl_send_command *send_cmd,
2826dd0e5ccSAlison Schofield 			  struct cxl_dev_state *cxlds)
2836dd0e5ccSAlison Schofield {
2846dd0e5ccSAlison Schofield 	struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id];
2856dd0e5ccSAlison Schofield 	const struct cxl_command_info *info = &c->info;
2866dd0e5ccSAlison Schofield 
2876dd0e5ccSAlison Schofield 	if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK)
2886dd0e5ccSAlison Schofield 		return -EINVAL;
2896dd0e5ccSAlison Schofield 
2906dd0e5ccSAlison Schofield 	if (send_cmd->rsvd)
2916dd0e5ccSAlison Schofield 		return -EINVAL;
2926dd0e5ccSAlison Schofield 
2936dd0e5ccSAlison Schofield 	if (send_cmd->in.rsvd || send_cmd->out.rsvd)
2946dd0e5ccSAlison Schofield 		return -EINVAL;
2956dd0e5ccSAlison Schofield 
2966dd0e5ccSAlison Schofield 	/* Check that the command is enabled for hardware */
2976dd0e5ccSAlison Schofield 	if (!test_bit(info->id, cxlds->enabled_cmds))
2986dd0e5ccSAlison Schofield 		return -ENOTTY;
2996dd0e5ccSAlison Schofield 
3006dd0e5ccSAlison Schofield 	/* Check that the command is not claimed for exclusive kernel use */
3016dd0e5ccSAlison Schofield 	if (test_bit(info->id, cxlds->exclusive_cmds))
3026dd0e5ccSAlison Schofield 		return -EBUSY;
3036dd0e5ccSAlison Schofield 
3046dd0e5ccSAlison Schofield 	/* Check the input buffer is the expected size */
3056dd0e5ccSAlison Schofield 	if (info->size_in >= 0 && info->size_in != send_cmd->in.size)
3066dd0e5ccSAlison Schofield 		return -ENOMEM;
3076dd0e5ccSAlison Schofield 
3086dd0e5ccSAlison Schofield 	/* Check the output buffer is at least large enough */
3096dd0e5ccSAlison Schofield 	if (info->size_out >= 0 && send_cmd->out.size < info->size_out)
3106dd0e5ccSAlison Schofield 		return -ENOMEM;
3116dd0e5ccSAlison Schofield 
3126dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
3136dd0e5ccSAlison Schofield 		.info = {
3146dd0e5ccSAlison Schofield 			.id = info->id,
3156dd0e5ccSAlison Schofield 			.flags = info->flags,
3166dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
3176dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
3186dd0e5ccSAlison Schofield 		},
3196dd0e5ccSAlison Schofield 		.opcode = c->opcode
3206dd0e5ccSAlison Schofield 	};
3216dd0e5ccSAlison Schofield 
3226dd0e5ccSAlison Schofield 	return 0;
3236dd0e5ccSAlison Schofield }
3246dd0e5ccSAlison Schofield 
3254faf31b4SDan Williams /**
3264faf31b4SDan Williams  * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
3275e2411aeSIra Weiny  * @cxlds: The device data for the operation
3284faf31b4SDan Williams  * @send_cmd: &struct cxl_send_command copied in from userspace.
3294faf31b4SDan Williams  * @out_cmd: Sanitized and populated &struct cxl_mem_command.
3304faf31b4SDan Williams  *
3314faf31b4SDan Williams  * Return:
3324faf31b4SDan Williams  *  * %0	- @out_cmd is ready to send.
3334faf31b4SDan Williams  *  * %-ENOTTY	- Invalid command specified.
3344faf31b4SDan Williams  *  * %-EINVAL	- Reserved fields or invalid values were used.
3354faf31b4SDan Williams  *  * %-ENOMEM	- Input or output buffer wasn't sized properly.
3364faf31b4SDan Williams  *  * %-EPERM	- Attempted to use a protected command.
33712f3856aSDan Williams  *  * %-EBUSY	- Kernel has claimed exclusive access to this opcode
3384faf31b4SDan Williams  *
3394faf31b4SDan Williams  * The result of this command is a fully validated command in @out_cmd that is
3404faf31b4SDan Williams  * safe to send to the hardware.
3414faf31b4SDan Williams  *
3424faf31b4SDan Williams  * See handle_mailbox_cmd_from_user()
3434faf31b4SDan Williams  */
3445e2411aeSIra Weiny static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,
3454faf31b4SDan Williams 				      const struct cxl_send_command *send_cmd,
3464faf31b4SDan Williams 				      struct cxl_mem_command *out_cmd)
3474faf31b4SDan Williams {
3484faf31b4SDan Williams 	if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX)
3494faf31b4SDan Williams 		return -ENOTTY;
3504faf31b4SDan Williams 
3514faf31b4SDan Williams 	/*
3524faf31b4SDan Williams 	 * The user can never specify an input payload larger than what hardware
3534faf31b4SDan Williams 	 * supports, but output can be arbitrarily large (simply write out as
3544faf31b4SDan Williams 	 * much data as the hardware provides).
3554faf31b4SDan Williams 	 */
3565e2411aeSIra Weiny 	if (send_cmd->in.size > cxlds->payload_size)
3574faf31b4SDan Williams 		return -EINVAL;
3584faf31b4SDan Williams 
3596dd0e5ccSAlison Schofield 	/* Sanitize and construct a cxl_mem_command */
3606dd0e5ccSAlison Schofield 	if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW)
3616dd0e5ccSAlison Schofield 		return cxl_to_mem_cmd_raw(out_cmd, send_cmd, cxlds);
3626dd0e5ccSAlison Schofield 	else
3636dd0e5ccSAlison Schofield 		return cxl_to_mem_cmd(out_cmd, send_cmd, cxlds);
3644faf31b4SDan Williams }
3654faf31b4SDan Williams 
3664faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd,
3674faf31b4SDan Williams 		  struct cxl_mem_query_commands __user *q)
3684faf31b4SDan Williams {
3694faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
3704faf31b4SDan Williams 	struct cxl_mem_command *cmd;
3714faf31b4SDan Williams 	u32 n_commands;
3724faf31b4SDan Williams 	int j = 0;
3734faf31b4SDan Williams 
3744faf31b4SDan Williams 	dev_dbg(dev, "Query IOCTL\n");
3754faf31b4SDan Williams 
3764faf31b4SDan Williams 	if (get_user(n_commands, &q->n_commands))
3774faf31b4SDan Williams 		return -EFAULT;
3784faf31b4SDan Williams 
3794faf31b4SDan Williams 	/* returns the total number if 0 elements are requested. */
3804faf31b4SDan Williams 	if (n_commands == 0)
381ff56ab9eSDan Williams 		return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands);
3824faf31b4SDan Williams 
3834faf31b4SDan Williams 	/*
3844faf31b4SDan Williams 	 * otherwise, return max(n_commands, total commands) cxl_command_info
3854faf31b4SDan Williams 	 * structures.
3864faf31b4SDan Williams 	 */
3874faf31b4SDan Williams 	cxl_for_each_cmd(cmd) {
3884faf31b4SDan Williams 		const struct cxl_command_info *info = &cmd->info;
3894faf31b4SDan Williams 
3904faf31b4SDan Williams 		if (copy_to_user(&q->commands[j++], info, sizeof(*info)))
3914faf31b4SDan Williams 			return -EFAULT;
3924faf31b4SDan Williams 
3934faf31b4SDan Williams 		if (j == n_commands)
3944faf31b4SDan Williams 			break;
3954faf31b4SDan Williams 	}
3964faf31b4SDan Williams 
3974faf31b4SDan Williams 	return 0;
3984faf31b4SDan Williams }
3994faf31b4SDan Williams 
4004faf31b4SDan Williams /**
4014faf31b4SDan Williams  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace.
4025e2411aeSIra Weiny  * @cxlds: The device data for the operation
4034faf31b4SDan Williams  * @cmd: The validated command.
4044faf31b4SDan Williams  * @in_payload: Pointer to userspace's input payload.
4054faf31b4SDan Williams  * @out_payload: Pointer to userspace's output payload.
4064faf31b4SDan Williams  * @size_out: (Input) Max payload size to copy out.
4074faf31b4SDan Williams  *            (Output) Payload size hardware generated.
4084faf31b4SDan Williams  * @retval: Hardware generated return code from the operation.
4094faf31b4SDan Williams  *
4104faf31b4SDan Williams  * Return:
4114faf31b4SDan Williams  *  * %0	- Mailbox transaction succeeded. This implies the mailbox
4124faf31b4SDan Williams  *		  protocol completed successfully not that the operation itself
4134faf31b4SDan Williams  *		  was successful.
4144faf31b4SDan Williams  *  * %-ENOMEM  - Couldn't allocate a bounce buffer.
4154faf31b4SDan Williams  *  * %-EFAULT	- Something happened with copy_to/from_user.
4164faf31b4SDan Williams  *  * %-EINTR	- Mailbox acquisition interrupted.
4174faf31b4SDan Williams  *  * %-EXXX	- Transaction level failures.
4184faf31b4SDan Williams  *
4194faf31b4SDan Williams  * Creates the appropriate mailbox command and dispatches it on behalf of a
4204faf31b4SDan Williams  * userspace request. The input and output payloads are copied between
4214faf31b4SDan Williams  * userspace.
4224faf31b4SDan Williams  *
4234faf31b4SDan Williams  * See cxl_send_cmd().
4244faf31b4SDan Williams  */
4255e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,
4264faf31b4SDan Williams 					const struct cxl_mem_command *cmd,
4274faf31b4SDan Williams 					u64 in_payload, u64 out_payload,
4284faf31b4SDan Williams 					s32 *size_out, u32 *retval)
4294faf31b4SDan Williams {
4305e2411aeSIra Weiny 	struct device *dev = cxlds->dev;
431*63cf60b7SAlison Schofield 	struct cxl_mbox_cmd mbox_cmd;
4324faf31b4SDan Williams 	int rc;
4334faf31b4SDan Williams 
434*63cf60b7SAlison Schofield 	rc = cxl_mbox_cmd_ctor(&mbox_cmd, cxlds, cmd->opcode,
435*63cf60b7SAlison Schofield 			       cmd->info.size_in, cmd->info.size_out,
436*63cf60b7SAlison Schofield 			       in_payload);
437*63cf60b7SAlison Schofield 	if (rc)
438*63cf60b7SAlison Schofield 		return rc;
4394faf31b4SDan Williams 
4404faf31b4SDan Williams 	dev_dbg(dev,
4414faf31b4SDan Williams 		"Submitting %s command for user\n"
4424faf31b4SDan Williams 		"\topcode: %x\n"
4434faf31b4SDan Williams 		"\tsize: %ub\n",
4444faf31b4SDan Williams 		cxl_command_names[cmd->info.id].name, mbox_cmd.opcode,
4454faf31b4SDan Williams 		cmd->info.size_in);
4464faf31b4SDan Williams 
4475e2411aeSIra Weiny 	rc = cxlds->mbox_send(cxlds, &mbox_cmd);
4484faf31b4SDan Williams 	if (rc)
4494faf31b4SDan Williams 		goto out;
4504faf31b4SDan Williams 
4514faf31b4SDan Williams 	/*
4524faf31b4SDan Williams 	 * @size_out contains the max size that's allowed to be written back out
4534faf31b4SDan Williams 	 * to userspace. While the payload may have written more output than
4544faf31b4SDan Williams 	 * this it will have to be ignored.
4554faf31b4SDan Williams 	 */
4564faf31b4SDan Williams 	if (mbox_cmd.size_out) {
4574faf31b4SDan Williams 		dev_WARN_ONCE(dev, mbox_cmd.size_out > *size_out,
4584faf31b4SDan Williams 			      "Invalid return size\n");
4594faf31b4SDan Williams 		if (copy_to_user(u64_to_user_ptr(out_payload),
4604faf31b4SDan Williams 				 mbox_cmd.payload_out, mbox_cmd.size_out)) {
4614faf31b4SDan Williams 			rc = -EFAULT;
4624faf31b4SDan Williams 			goto out;
4634faf31b4SDan Williams 		}
4644faf31b4SDan Williams 	}
4654faf31b4SDan Williams 
4664faf31b4SDan Williams 	*size_out = mbox_cmd.size_out;
4674faf31b4SDan Williams 	*retval = mbox_cmd.return_code;
4684faf31b4SDan Williams 
4694faf31b4SDan Williams out:
470*63cf60b7SAlison Schofield 	cxl_mbox_cmd_dtor(&mbox_cmd);
4714faf31b4SDan Williams 	return rc;
4724faf31b4SDan Williams }
4734faf31b4SDan Williams 
4744faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)
4754faf31b4SDan Williams {
4765e2411aeSIra Weiny 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
4774faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
4784faf31b4SDan Williams 	struct cxl_send_command send;
4794faf31b4SDan Williams 	struct cxl_mem_command c;
4804faf31b4SDan Williams 	int rc;
4814faf31b4SDan Williams 
4824faf31b4SDan Williams 	dev_dbg(dev, "Send IOCTL\n");
4834faf31b4SDan Williams 
4844faf31b4SDan Williams 	if (copy_from_user(&send, s, sizeof(send)))
4854faf31b4SDan Williams 		return -EFAULT;
4864faf31b4SDan Williams 
4875e2411aeSIra Weiny 	rc = cxl_validate_cmd_from_user(cxlmd->cxlds, &send, &c);
4884faf31b4SDan Williams 	if (rc)
4894faf31b4SDan Williams 		return rc;
4904faf31b4SDan Williams 
4915e2411aeSIra Weiny 	rc = handle_mailbox_cmd_from_user(cxlds, &c, send.in.payload,
4924faf31b4SDan Williams 					  send.out.payload, &send.out.size,
4934faf31b4SDan Williams 					  &send.retval);
4944faf31b4SDan Williams 	if (rc)
4954faf31b4SDan Williams 		return rc;
4964faf31b4SDan Williams 
4974faf31b4SDan Williams 	if (copy_to_user(s, &send, sizeof(send)))
4984faf31b4SDan Williams 		return -EFAULT;
4994faf31b4SDan Williams 
5004faf31b4SDan Williams 	return 0;
5014faf31b4SDan Williams }
5024faf31b4SDan Williams 
5035e2411aeSIra Weiny static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8 *out)
5044faf31b4SDan Williams {
5054faf31b4SDan Williams 	u32 remaining = size;
5064faf31b4SDan Williams 	u32 offset = 0;
5074faf31b4SDan Williams 
5084faf31b4SDan Williams 	while (remaining) {
5095e2411aeSIra Weiny 		u32 xfer_size = min_t(u32, remaining, cxlds->payload_size);
51049be6dd8SDan Williams 		struct cxl_mbox_get_log log = {
5114faf31b4SDan Williams 			.uuid = *uuid,
5124faf31b4SDan Williams 			.offset = cpu_to_le32(offset),
5134faf31b4SDan Williams 			.length = cpu_to_le32(xfer_size)
5144faf31b4SDan Williams 		};
5154faf31b4SDan Williams 		int rc;
5164faf31b4SDan Williams 
5175e2411aeSIra Weiny 		rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_LOG, &log, sizeof(log),
5185e2411aeSIra Weiny 				       out, xfer_size);
5194faf31b4SDan Williams 		if (rc < 0)
5204faf31b4SDan Williams 			return rc;
5214faf31b4SDan Williams 
5224faf31b4SDan Williams 		out += xfer_size;
5234faf31b4SDan Williams 		remaining -= xfer_size;
5244faf31b4SDan Williams 		offset += xfer_size;
5254faf31b4SDan Williams 	}
5264faf31b4SDan Williams 
5274faf31b4SDan Williams 	return 0;
5284faf31b4SDan Williams }
5294faf31b4SDan Williams 
5304faf31b4SDan Williams /**
5314faf31b4SDan Williams  * cxl_walk_cel() - Walk through the Command Effects Log.
5325e2411aeSIra Weiny  * @cxlds: The device data for the operation
5334faf31b4SDan Williams  * @size: Length of the Command Effects Log.
5344faf31b4SDan Williams  * @cel: CEL
5354faf31b4SDan Williams  *
5364faf31b4SDan Williams  * Iterate over each entry in the CEL and determine if the driver supports the
5374faf31b4SDan Williams  * command. If so, the command is enabled for the device and can be used later.
5384faf31b4SDan Williams  */
5395e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel)
5404faf31b4SDan Williams {
54149be6dd8SDan Williams 	struct cxl_cel_entry *cel_entry;
5424faf31b4SDan Williams 	const int cel_entries = size / sizeof(*cel_entry);
5434faf31b4SDan Williams 	int i;
5444faf31b4SDan Williams 
54549be6dd8SDan Williams 	cel_entry = (struct cxl_cel_entry *) cel;
5464faf31b4SDan Williams 
5474faf31b4SDan Williams 	for (i = 0; i < cel_entries; i++) {
5484faf31b4SDan Williams 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
5494faf31b4SDan Williams 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
5504faf31b4SDan Williams 
5514faf31b4SDan Williams 		if (!cmd) {
5525e2411aeSIra Weiny 			dev_dbg(cxlds->dev,
5534faf31b4SDan Williams 				"Opcode 0x%04x unsupported by driver", opcode);
5544faf31b4SDan Williams 			continue;
5554faf31b4SDan Williams 		}
5564faf31b4SDan Williams 
5575e2411aeSIra Weiny 		set_bit(cmd->info.id, cxlds->enabled_cmds);
5584faf31b4SDan Williams 	}
5594faf31b4SDan Williams }
5604faf31b4SDan Williams 
5615e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds)
5624faf31b4SDan Williams {
5634faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *ret;
5644faf31b4SDan Williams 	int rc;
5654faf31b4SDan Williams 
5665e2411aeSIra Weiny 	ret = kvmalloc(cxlds->payload_size, GFP_KERNEL);
5674faf31b4SDan Williams 	if (!ret)
5684faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
5694faf31b4SDan Williams 
5705e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_SUPPORTED_LOGS, NULL, 0, ret,
5715e2411aeSIra Weiny 			       cxlds->payload_size);
5724faf31b4SDan Williams 	if (rc < 0) {
5734faf31b4SDan Williams 		kvfree(ret);
5744faf31b4SDan Williams 		return ERR_PTR(rc);
5754faf31b4SDan Williams 	}
5764faf31b4SDan Williams 
5774faf31b4SDan Williams 	return ret;
5784faf31b4SDan Williams }
5794faf31b4SDan Williams 
5804faf31b4SDan Williams enum {
5814faf31b4SDan Williams 	CEL_UUID,
5824faf31b4SDan Williams 	VENDOR_DEBUG_UUID,
5834faf31b4SDan Williams };
5844faf31b4SDan Williams 
5854faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */
5864faf31b4SDan Williams static const uuid_t log_uuid[] = {
58749be6dd8SDan Williams 	[CEL_UUID] = DEFINE_CXL_CEL_UUID,
58849be6dd8SDan Williams 	[VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID,
5894faf31b4SDan Williams };
5904faf31b4SDan Williams 
5914faf31b4SDan Williams /**
5925e2411aeSIra Weiny  * cxl_enumerate_cmds() - Enumerate commands for a device.
5935e2411aeSIra Weiny  * @cxlds: The device data for the operation
5944faf31b4SDan Williams  *
5954faf31b4SDan Williams  * Returns 0 if enumerate completed successfully.
5964faf31b4SDan Williams  *
5974faf31b4SDan Williams  * CXL devices have optional support for certain commands. This function will
5984faf31b4SDan Williams  * determine the set of supported commands for the hardware and update the
5995e2411aeSIra Weiny  * enabled_cmds bitmap in the @cxlds.
6004faf31b4SDan Williams  */
6015e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds)
6024faf31b4SDan Williams {
6034faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *gsl;
6045e2411aeSIra Weiny 	struct device *dev = cxlds->dev;
6054faf31b4SDan Williams 	struct cxl_mem_command *cmd;
6064faf31b4SDan Williams 	int i, rc;
6074faf31b4SDan Williams 
6085e2411aeSIra Weiny 	gsl = cxl_get_gsl(cxlds);
6094faf31b4SDan Williams 	if (IS_ERR(gsl))
6104faf31b4SDan Williams 		return PTR_ERR(gsl);
6114faf31b4SDan Williams 
6124faf31b4SDan Williams 	rc = -ENOENT;
6134faf31b4SDan Williams 	for (i = 0; i < le16_to_cpu(gsl->entries); i++) {
6144faf31b4SDan Williams 		u32 size = le32_to_cpu(gsl->entry[i].size);
6154faf31b4SDan Williams 		uuid_t uuid = gsl->entry[i].uuid;
6164faf31b4SDan Williams 		u8 *log;
6174faf31b4SDan Williams 
6184faf31b4SDan Williams 		dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size);
6194faf31b4SDan Williams 
6204faf31b4SDan Williams 		if (!uuid_equal(&uuid, &log_uuid[CEL_UUID]))
6214faf31b4SDan Williams 			continue;
6224faf31b4SDan Williams 
6234faf31b4SDan Williams 		log = kvmalloc(size, GFP_KERNEL);
6244faf31b4SDan Williams 		if (!log) {
6254faf31b4SDan Williams 			rc = -ENOMEM;
6264faf31b4SDan Williams 			goto out;
6274faf31b4SDan Williams 		}
6284faf31b4SDan Williams 
6295e2411aeSIra Weiny 		rc = cxl_xfer_log(cxlds, &uuid, size, log);
6304faf31b4SDan Williams 		if (rc) {
6314faf31b4SDan Williams 			kvfree(log);
6324faf31b4SDan Williams 			goto out;
6334faf31b4SDan Williams 		}
6344faf31b4SDan Williams 
6355e2411aeSIra Weiny 		cxl_walk_cel(cxlds, size, log);
6364faf31b4SDan Williams 		kvfree(log);
6374faf31b4SDan Williams 
6384faf31b4SDan Williams 		/* In case CEL was bogus, enable some default commands. */
6394faf31b4SDan Williams 		cxl_for_each_cmd(cmd)
6404faf31b4SDan Williams 			if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE)
6415e2411aeSIra Weiny 				set_bit(cmd->info.id, cxlds->enabled_cmds);
6424faf31b4SDan Williams 
6434faf31b4SDan Williams 		/* Found the required CEL */
6444faf31b4SDan Williams 		rc = 0;
6454faf31b4SDan Williams 	}
6464faf31b4SDan Williams 
6474faf31b4SDan Williams out:
6484faf31b4SDan Williams 	kvfree(gsl);
6494faf31b4SDan Williams 	return rc;
6504faf31b4SDan Williams }
651affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL);
6524faf31b4SDan Williams 
6534faf31b4SDan Williams /**
6544faf31b4SDan Williams  * cxl_mem_get_partition_info - Get partition info
6555e2411aeSIra Weiny  * @cxlds: The device data for the operation
6564faf31b4SDan Williams  *
6574faf31b4SDan Williams  * Retrieve the current partition info for the device specified.  The active
6584faf31b4SDan Williams  * values are the current capacity in bytes.  If not 0, the 'next' values are
6594faf31b4SDan Williams  * the pending values, in bytes, which take affect on next cold reset.
6604faf31b4SDan Williams  *
6614faf31b4SDan Williams  * Return: 0 if no error: or the result of the mailbox command.
6624faf31b4SDan Williams  *
6634faf31b4SDan Williams  * See CXL @8.2.9.5.2.1 Get Partition Info
6644faf31b4SDan Williams  */
6655e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds)
6664faf31b4SDan Williams {
6674faf31b4SDan Williams 	struct cxl_mbox_get_partition_info {
6684faf31b4SDan Williams 		__le64 active_volatile_cap;
6694faf31b4SDan Williams 		__le64 active_persistent_cap;
6704faf31b4SDan Williams 		__le64 next_volatile_cap;
6714faf31b4SDan Williams 		__le64 next_persistent_cap;
6724faf31b4SDan Williams 	} __packed pi;
6734faf31b4SDan Williams 	int rc;
6744faf31b4SDan Williams 
6755e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_PARTITION_INFO, NULL, 0,
6765e2411aeSIra Weiny 			       &pi, sizeof(pi));
6774faf31b4SDan Williams 
6784faf31b4SDan Williams 	if (rc)
6794faf31b4SDan Williams 		return rc;
6804faf31b4SDan Williams 
6815e2411aeSIra Weiny 	cxlds->active_volatile_bytes =
6824faf31b4SDan Williams 		le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6835e2411aeSIra Weiny 	cxlds->active_persistent_bytes =
6844faf31b4SDan Williams 		le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER;
6855e2411aeSIra Weiny 	cxlds->next_volatile_bytes =
6864faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6875e2411aeSIra Weiny 	cxlds->next_persistent_bytes =
6884faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6894faf31b4SDan Williams 
6904faf31b4SDan Williams 	return 0;
6914faf31b4SDan Williams }
6924faf31b4SDan Williams 
6934faf31b4SDan Williams /**
6945e2411aeSIra Weiny  * cxl_dev_state_identify() - Send the IDENTIFY command to the device.
6955e2411aeSIra Weiny  * @cxlds: The device data for the operation
6964faf31b4SDan Williams  *
6974faf31b4SDan Williams  * Return: 0 if identify was executed successfully.
6984faf31b4SDan Williams  *
6994faf31b4SDan Williams  * This will dispatch the identify command to the device and on success populate
7004faf31b4SDan Williams  * structures to be exported to sysfs.
7014faf31b4SDan Williams  */
7025e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds)
7034faf31b4SDan Williams {
7044faf31b4SDan Williams 	/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
70549be6dd8SDan Williams 	struct cxl_mbox_identify id;
7064faf31b4SDan Williams 	int rc;
7074faf31b4SDan Williams 
7085e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_IDENTIFY, NULL, 0, &id,
7094faf31b4SDan Williams 			       sizeof(id));
7104faf31b4SDan Williams 	if (rc < 0)
7114faf31b4SDan Williams 		return rc;
7124faf31b4SDan Williams 
7135e2411aeSIra Weiny 	cxlds->total_bytes =
7144faf31b4SDan Williams 		le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER;
7155e2411aeSIra Weiny 	cxlds->volatile_only_bytes =
7164faf31b4SDan Williams 		le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER;
7175e2411aeSIra Weiny 	cxlds->persistent_only_bytes =
7184faf31b4SDan Williams 		le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER;
7195e2411aeSIra Weiny 	cxlds->partition_align_bytes =
7204faf31b4SDan Williams 		le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER;
7214faf31b4SDan Williams 
7225e2411aeSIra Weiny 	dev_dbg(cxlds->dev,
7234faf31b4SDan Williams 		"Identify Memory Device\n"
7244faf31b4SDan Williams 		"     total_bytes = %#llx\n"
7254faf31b4SDan Williams 		"     volatile_only_bytes = %#llx\n"
7264faf31b4SDan Williams 		"     persistent_only_bytes = %#llx\n"
7274faf31b4SDan Williams 		"     partition_align_bytes = %#llx\n",
7285e2411aeSIra Weiny 		cxlds->total_bytes, cxlds->volatile_only_bytes,
7295e2411aeSIra Weiny 		cxlds->persistent_only_bytes, cxlds->partition_align_bytes);
7304faf31b4SDan Williams 
7315e2411aeSIra Weiny 	cxlds->lsa_size = le32_to_cpu(id.lsa_size);
7325e2411aeSIra Weiny 	memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision));
7334faf31b4SDan Williams 
7344faf31b4SDan Williams 	return 0;
7354faf31b4SDan Williams }
736affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL);
7374faf31b4SDan Williams 
7385e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds)
7394faf31b4SDan Williams {
7404faf31b4SDan Williams 	int rc;
7414faf31b4SDan Williams 
7425e2411aeSIra Weiny 	if (cxlds->partition_align_bytes == 0) {
7435e2411aeSIra Weiny 		cxlds->ram_range.start = 0;
7445e2411aeSIra Weiny 		cxlds->ram_range.end = cxlds->volatile_only_bytes - 1;
7455e2411aeSIra Weiny 		cxlds->pmem_range.start = cxlds->volatile_only_bytes;
7465e2411aeSIra Weiny 		cxlds->pmem_range.end = cxlds->volatile_only_bytes +
7475e2411aeSIra Weiny 				       cxlds->persistent_only_bytes - 1;
7484faf31b4SDan Williams 		return 0;
7494faf31b4SDan Williams 	}
7504faf31b4SDan Williams 
7515e2411aeSIra Weiny 	rc = cxl_mem_get_partition_info(cxlds);
7524faf31b4SDan Williams 	if (rc) {
7535e2411aeSIra Weiny 		dev_err(cxlds->dev, "Failed to query partition information\n");
7544faf31b4SDan Williams 		return rc;
7554faf31b4SDan Williams 	}
7564faf31b4SDan Williams 
7575e2411aeSIra Weiny 	dev_dbg(cxlds->dev,
7584faf31b4SDan Williams 		"Get Partition Info\n"
7594faf31b4SDan Williams 		"     active_volatile_bytes = %#llx\n"
7604faf31b4SDan Williams 		"     active_persistent_bytes = %#llx\n"
7614faf31b4SDan Williams 		"     next_volatile_bytes = %#llx\n"
7624faf31b4SDan Williams 		"     next_persistent_bytes = %#llx\n",
7635e2411aeSIra Weiny 		cxlds->active_volatile_bytes, cxlds->active_persistent_bytes,
7645e2411aeSIra Weiny 		cxlds->next_volatile_bytes, cxlds->next_persistent_bytes);
7654faf31b4SDan Williams 
7665e2411aeSIra Weiny 	cxlds->ram_range.start = 0;
7675e2411aeSIra Weiny 	cxlds->ram_range.end = cxlds->active_volatile_bytes - 1;
7684faf31b4SDan Williams 
7695e2411aeSIra Weiny 	cxlds->pmem_range.start = cxlds->active_volatile_bytes;
7705e2411aeSIra Weiny 	cxlds->pmem_range.end =
7715e2411aeSIra Weiny 		cxlds->active_volatile_bytes + cxlds->active_persistent_bytes - 1;
7724faf31b4SDan Williams 
7734faf31b4SDan Williams 	return 0;
7744faf31b4SDan Williams }
775affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL);
7764faf31b4SDan Williams 
7775e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev)
7784faf31b4SDan Williams {
7795e2411aeSIra Weiny 	struct cxl_dev_state *cxlds;
7804faf31b4SDan Williams 
7815e2411aeSIra Weiny 	cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL);
7825e2411aeSIra Weiny 	if (!cxlds) {
7834faf31b4SDan Williams 		dev_err(dev, "No memory available\n");
7844faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
7854faf31b4SDan Williams 	}
7864faf31b4SDan Williams 
7875e2411aeSIra Weiny 	mutex_init(&cxlds->mbox_mutex);
7885e2411aeSIra Weiny 	cxlds->dev = dev;
7894faf31b4SDan Williams 
7905e2411aeSIra Weiny 	return cxlds;
7914faf31b4SDan Williams }
792affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL);
7934faf31b4SDan Williams 
7944faf31b4SDan Williams static struct dentry *cxl_debugfs;
7954faf31b4SDan Williams 
7964faf31b4SDan Williams void __init cxl_mbox_init(void)
7974faf31b4SDan Williams {
7984faf31b4SDan Williams 	struct dentry *mbox_debugfs;
7994faf31b4SDan Williams 
8004faf31b4SDan Williams 	cxl_debugfs = debugfs_create_dir("cxl", NULL);
8014faf31b4SDan Williams 	mbox_debugfs = debugfs_create_dir("mbox", cxl_debugfs);
8024faf31b4SDan Williams 	debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs,
8034faf31b4SDan Williams 			    &cxl_raw_allow_all);
8044faf31b4SDan Williams }
8054faf31b4SDan Williams 
8064faf31b4SDan Williams void cxl_mbox_exit(void)
8074faf31b4SDan Williams {
8084faf31b4SDan Williams 	debugfs_remove_recursive(cxl_debugfs);
8094faf31b4SDan Williams }
810