xref: /linux/drivers/cxl/core/mbox.c (revision 2dd5600a0e4e1aca8541e1c5bb2bc26bc0441d4d)
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 
13082b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode)
13182b8ba29SAlison Schofield {
13282b8ba29SAlison Schofield 	struct cxl_mem_command *c;
13382b8ba29SAlison Schofield 
13482b8ba29SAlison Schofield 	c = cxl_mem_find_command(opcode);
13582b8ba29SAlison Schofield 	if (!c)
13682b8ba29SAlison Schofield 		return NULL;
13782b8ba29SAlison Schofield 
13882b8ba29SAlison Schofield 	return cxl_command_names[c->info.id].name;
13982b8ba29SAlison Schofield }
14082b8ba29SAlison Schofield 
1414faf31b4SDan Williams /**
1425e2411aeSIra Weiny  * cxl_mbox_send_cmd() - Send a mailbox command to a device.
1435e2411aeSIra Weiny  * @cxlds: The device data for the operation
1444faf31b4SDan Williams  * @opcode: Opcode for the mailbox command.
1454faf31b4SDan Williams  * @in: The input payload for the mailbox command.
1464faf31b4SDan Williams  * @in_size: The length of the input payload
1474faf31b4SDan Williams  * @out: Caller allocated buffer for the output.
1484faf31b4SDan Williams  * @out_size: Expected size of output.
1494faf31b4SDan Williams  *
1504faf31b4SDan Williams  * Context: Any context. Will acquire and release mbox_mutex.
1514faf31b4SDan Williams  * Return:
1524faf31b4SDan Williams  *  * %>=0	- Number of bytes returned in @out.
1534faf31b4SDan Williams  *  * %-E2BIG	- Payload is too large for hardware.
1544faf31b4SDan Williams  *  * %-EBUSY	- Couldn't acquire exclusive mailbox access.
1554faf31b4SDan Williams  *  * %-EFAULT	- Hardware error occurred.
1564faf31b4SDan Williams  *  * %-ENXIO	- Command completed, but device reported an error.
1574faf31b4SDan Williams  *  * %-EIO	- Unexpected output size.
1584faf31b4SDan Williams  *
1594faf31b4SDan Williams  * Mailbox commands may execute successfully yet the device itself reported an
1604faf31b4SDan Williams  * error. While this distinction can be useful for commands from userspace, the
1614faf31b4SDan Williams  * kernel will only be able to use results when both are successful.
1624faf31b4SDan Williams  */
1635e2411aeSIra Weiny int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in,
1644faf31b4SDan Williams 		      size_t in_size, void *out, size_t out_size)
1654faf31b4SDan Williams {
1664faf31b4SDan Williams 	const struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
1674faf31b4SDan Williams 	struct cxl_mbox_cmd mbox_cmd = {
1684faf31b4SDan Williams 		.opcode = opcode,
1694faf31b4SDan Williams 		.payload_in = in,
1704faf31b4SDan Williams 		.size_in = in_size,
1714faf31b4SDan Williams 		.size_out = out_size,
1724faf31b4SDan Williams 		.payload_out = out,
1734faf31b4SDan Williams 	};
1744faf31b4SDan Williams 	int rc;
1754faf31b4SDan Williams 
1765e2411aeSIra Weiny 	if (out_size > cxlds->payload_size)
1774faf31b4SDan Williams 		return -E2BIG;
1784faf31b4SDan Williams 
1795e2411aeSIra Weiny 	rc = cxlds->mbox_send(cxlds, &mbox_cmd);
1804faf31b4SDan Williams 	if (rc)
1814faf31b4SDan Williams 		return rc;
1824faf31b4SDan Williams 
1834faf31b4SDan Williams 	/* TODO: Map return code to proper kernel style errno */
1844faf31b4SDan Williams 	if (mbox_cmd.return_code != CXL_MBOX_SUCCESS)
1854faf31b4SDan Williams 		return -ENXIO;
1864faf31b4SDan Williams 
1874faf31b4SDan Williams 	/*
1884faf31b4SDan Williams 	 * Variable sized commands can't be validated and so it's up to the
1894faf31b4SDan Williams 	 * caller to do that if they wish.
1904faf31b4SDan Williams 	 */
1914faf31b4SDan Williams 	if (cmd->info.size_out >= 0 && mbox_cmd.size_out != out_size)
1924faf31b4SDan Williams 		return -EIO;
1934faf31b4SDan Williams 
1944faf31b4SDan Williams 	return 0;
1954faf31b4SDan Williams }
196affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mbox_send_cmd, CXL);
1974faf31b4SDan Williams 
1984faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode)
1994faf31b4SDan Williams {
2004faf31b4SDan Williams 	int i;
2014faf31b4SDan Williams 
2024faf31b4SDan Williams 	if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS))
2034faf31b4SDan Williams 		return false;
2044faf31b4SDan Williams 
2054faf31b4SDan Williams 	if (security_locked_down(LOCKDOWN_PCI_ACCESS))
2064faf31b4SDan Williams 		return false;
2074faf31b4SDan Williams 
2084faf31b4SDan Williams 	if (cxl_raw_allow_all)
2094faf31b4SDan Williams 		return true;
2104faf31b4SDan Williams 
2114faf31b4SDan Williams 	if (cxl_is_security_command(opcode))
2124faf31b4SDan Williams 		return false;
2134faf31b4SDan Williams 
2144faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++)
2154faf31b4SDan Williams 		if (cxl_disabled_raw_commands[i] == opcode)
2164faf31b4SDan Williams 			return false;
2174faf31b4SDan Williams 
2184faf31b4SDan Williams 	return true;
2194faf31b4SDan Williams }
2204faf31b4SDan Williams 
22163cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
22263cf60b7SAlison Schofield 			     struct cxl_dev_state *cxlds, u16 opcode,
22363cf60b7SAlison Schofield 			     size_t in_size, size_t out_size, u64 in_payload)
22463cf60b7SAlison Schofield {
22563cf60b7SAlison Schofield 	*mbox = (struct cxl_mbox_cmd) {
22663cf60b7SAlison Schofield 		.opcode = opcode,
22763cf60b7SAlison Schofield 		.size_in = in_size,
22863cf60b7SAlison Schofield 	};
22963cf60b7SAlison Schofield 
23063cf60b7SAlison Schofield 	if (in_size) {
23163cf60b7SAlison Schofield 		mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload),
23263cf60b7SAlison Schofield 						in_size);
23363cf60b7SAlison Schofield 		if (!mbox->payload_in)
23463cf60b7SAlison Schofield 			return PTR_ERR(mbox->payload_in);
23563cf60b7SAlison Schofield 	}
23663cf60b7SAlison Schofield 
23763cf60b7SAlison Schofield 	/* Prepare to handle a full payload for variable sized output */
23863cf60b7SAlison Schofield 	if (out_size < 0)
23963cf60b7SAlison Schofield 		mbox->size_out = cxlds->payload_size;
24063cf60b7SAlison Schofield 	else
24163cf60b7SAlison Schofield 		mbox->size_out = out_size;
24263cf60b7SAlison Schofield 
24363cf60b7SAlison Schofield 	if (mbox->size_out) {
24463cf60b7SAlison Schofield 		mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL);
24563cf60b7SAlison Schofield 		if (!mbox->payload_out) {
24663cf60b7SAlison Schofield 			kvfree(mbox->payload_in);
24763cf60b7SAlison Schofield 			return -ENOMEM;
24863cf60b7SAlison Schofield 		}
24963cf60b7SAlison Schofield 	}
25063cf60b7SAlison Schofield 	return 0;
25163cf60b7SAlison Schofield }
25263cf60b7SAlison Schofield 
25363cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox)
25463cf60b7SAlison Schofield {
25563cf60b7SAlison Schofield 	kvfree(mbox->payload_in);
25663cf60b7SAlison Schofield 	kvfree(mbox->payload_out);
25763cf60b7SAlison Schofield }
25863cf60b7SAlison Schofield 
2596dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,
2606dd0e5ccSAlison Schofield 			      const struct cxl_send_command *send_cmd,
2616dd0e5ccSAlison Schofield 			      struct cxl_dev_state *cxlds)
2626dd0e5ccSAlison Schofield {
2636dd0e5ccSAlison Schofield 	if (send_cmd->raw.rsvd)
2646dd0e5ccSAlison Schofield 		return -EINVAL;
2656dd0e5ccSAlison Schofield 
2666dd0e5ccSAlison Schofield 	/*
2676dd0e5ccSAlison Schofield 	 * Unlike supported commands, the output size of RAW commands
2686dd0e5ccSAlison Schofield 	 * gets passed along without further checking, so it must be
2696dd0e5ccSAlison Schofield 	 * validated here.
2706dd0e5ccSAlison Schofield 	 */
2716dd0e5ccSAlison Schofield 	if (send_cmd->out.size > cxlds->payload_size)
2726dd0e5ccSAlison Schofield 		return -EINVAL;
2736dd0e5ccSAlison Schofield 
2746dd0e5ccSAlison Schofield 	if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode))
2756dd0e5ccSAlison Schofield 		return -EPERM;
2766dd0e5ccSAlison Schofield 
27739ed8da4SAlison Schofield 	dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n");
27839ed8da4SAlison Schofield 
2796dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
2806dd0e5ccSAlison Schofield 		.info = {
2816dd0e5ccSAlison Schofield 			.id = CXL_MEM_COMMAND_ID_RAW,
2826dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
2836dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
2846dd0e5ccSAlison Schofield 		},
2856dd0e5ccSAlison Schofield 		.opcode = send_cmd->raw.opcode
2866dd0e5ccSAlison Schofield 	};
2876dd0e5ccSAlison Schofield 
2886dd0e5ccSAlison Schofield 	return 0;
2896dd0e5ccSAlison Schofield }
2906dd0e5ccSAlison Schofield 
2916dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,
2926dd0e5ccSAlison Schofield 			  const struct cxl_send_command *send_cmd,
2936dd0e5ccSAlison Schofield 			  struct cxl_dev_state *cxlds)
2946dd0e5ccSAlison Schofield {
2956dd0e5ccSAlison Schofield 	struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id];
2966dd0e5ccSAlison Schofield 	const struct cxl_command_info *info = &c->info;
2976dd0e5ccSAlison Schofield 
2986dd0e5ccSAlison Schofield 	if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK)
2996dd0e5ccSAlison Schofield 		return -EINVAL;
3006dd0e5ccSAlison Schofield 
3016dd0e5ccSAlison Schofield 	if (send_cmd->rsvd)
3026dd0e5ccSAlison Schofield 		return -EINVAL;
3036dd0e5ccSAlison Schofield 
3046dd0e5ccSAlison Schofield 	if (send_cmd->in.rsvd || send_cmd->out.rsvd)
3056dd0e5ccSAlison Schofield 		return -EINVAL;
3066dd0e5ccSAlison Schofield 
3076dd0e5ccSAlison Schofield 	/* Check that the command is enabled for hardware */
3086dd0e5ccSAlison Schofield 	if (!test_bit(info->id, cxlds->enabled_cmds))
3096dd0e5ccSAlison Schofield 		return -ENOTTY;
3106dd0e5ccSAlison Schofield 
3116dd0e5ccSAlison Schofield 	/* Check that the command is not claimed for exclusive kernel use */
3126dd0e5ccSAlison Schofield 	if (test_bit(info->id, cxlds->exclusive_cmds))
3136dd0e5ccSAlison Schofield 		return -EBUSY;
3146dd0e5ccSAlison Schofield 
3156dd0e5ccSAlison Schofield 	/* Check the input buffer is the expected size */
3166dd0e5ccSAlison Schofield 	if (info->size_in >= 0 && info->size_in != send_cmd->in.size)
3176dd0e5ccSAlison Schofield 		return -ENOMEM;
3186dd0e5ccSAlison Schofield 
3196dd0e5ccSAlison Schofield 	/* Check the output buffer is at least large enough */
3206dd0e5ccSAlison Schofield 	if (info->size_out >= 0 && send_cmd->out.size < info->size_out)
3216dd0e5ccSAlison Schofield 		return -ENOMEM;
3226dd0e5ccSAlison Schofield 
3236dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
3246dd0e5ccSAlison Schofield 		.info = {
3256dd0e5ccSAlison Schofield 			.id = info->id,
3266dd0e5ccSAlison Schofield 			.flags = info->flags,
3276dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
3286dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
3296dd0e5ccSAlison Schofield 		},
3306dd0e5ccSAlison Schofield 		.opcode = c->opcode
3316dd0e5ccSAlison Schofield 	};
3326dd0e5ccSAlison Schofield 
3336dd0e5ccSAlison Schofield 	return 0;
3346dd0e5ccSAlison Schofield }
3356dd0e5ccSAlison Schofield 
3364faf31b4SDan Williams /**
3374faf31b4SDan Williams  * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
3389ae016aeSAlison Schofield  * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd.
3395e2411aeSIra Weiny  * @cxlds: The device data for the operation
3404faf31b4SDan Williams  * @send_cmd: &struct cxl_send_command copied in from userspace.
3414faf31b4SDan Williams  *
3424faf31b4SDan Williams  * Return:
3434faf31b4SDan Williams  *  * %0	- @out_cmd is ready to send.
3444faf31b4SDan Williams  *  * %-ENOTTY	- Invalid command specified.
3454faf31b4SDan Williams  *  * %-EINVAL	- Reserved fields or invalid values were used.
3464faf31b4SDan Williams  *  * %-ENOMEM	- Input or output buffer wasn't sized properly.
3474faf31b4SDan Williams  *  * %-EPERM	- Attempted to use a protected command.
34812f3856aSDan Williams  *  * %-EBUSY	- Kernel has claimed exclusive access to this opcode
3494faf31b4SDan Williams  *
350*2dd5600aSAlison Schofield  * The result of this command is a fully validated command in @mbox_cmd that is
3514faf31b4SDan Williams  * safe to send to the hardware.
3524faf31b4SDan Williams  */
3539ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,
3549ae016aeSAlison Schofield 				      struct cxl_dev_state *cxlds,
355*2dd5600aSAlison Schofield 				      const struct cxl_send_command *send_cmd)
3564faf31b4SDan Williams {
357*2dd5600aSAlison Schofield 	struct cxl_mem_command mem_cmd;
3589ae016aeSAlison Schofield 	int rc;
3599ae016aeSAlison Schofield 
3604faf31b4SDan Williams 	if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX)
3614faf31b4SDan Williams 		return -ENOTTY;
3624faf31b4SDan Williams 
3634faf31b4SDan Williams 	/*
3644faf31b4SDan Williams 	 * The user can never specify an input payload larger than what hardware
3654faf31b4SDan Williams 	 * supports, but output can be arbitrarily large (simply write out as
3664faf31b4SDan Williams 	 * much data as the hardware provides).
3674faf31b4SDan Williams 	 */
3685e2411aeSIra Weiny 	if (send_cmd->in.size > cxlds->payload_size)
3694faf31b4SDan Williams 		return -EINVAL;
3704faf31b4SDan Williams 
3716dd0e5ccSAlison Schofield 	/* Sanitize and construct a cxl_mem_command */
3726dd0e5ccSAlison Schofield 	if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW)
373*2dd5600aSAlison Schofield 		rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, cxlds);
3746dd0e5ccSAlison Schofield 	else
375*2dd5600aSAlison Schofield 		rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, cxlds);
3769ae016aeSAlison Schofield 
3779ae016aeSAlison Schofield 	if (rc)
3789ae016aeSAlison Schofield 		return rc;
3799ae016aeSAlison Schofield 
3809ae016aeSAlison Schofield 	/* Sanitize and construct a cxl_mbox_cmd */
381*2dd5600aSAlison Schofield 	return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, mem_cmd.opcode,
382*2dd5600aSAlison Schofield 				 mem_cmd.info.size_in, mem_cmd.info.size_out,
3839ae016aeSAlison Schofield 				 send_cmd->in.payload);
3844faf31b4SDan Williams }
3854faf31b4SDan Williams 
3864faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd,
3874faf31b4SDan Williams 		  struct cxl_mem_query_commands __user *q)
3884faf31b4SDan Williams {
3894faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
3904faf31b4SDan Williams 	struct cxl_mem_command *cmd;
3914faf31b4SDan Williams 	u32 n_commands;
3924faf31b4SDan Williams 	int j = 0;
3934faf31b4SDan Williams 
3944faf31b4SDan Williams 	dev_dbg(dev, "Query IOCTL\n");
3954faf31b4SDan Williams 
3964faf31b4SDan Williams 	if (get_user(n_commands, &q->n_commands))
3974faf31b4SDan Williams 		return -EFAULT;
3984faf31b4SDan Williams 
3994faf31b4SDan Williams 	/* returns the total number if 0 elements are requested. */
4004faf31b4SDan Williams 	if (n_commands == 0)
401ff56ab9eSDan Williams 		return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands);
4024faf31b4SDan Williams 
4034faf31b4SDan Williams 	/*
4044faf31b4SDan Williams 	 * otherwise, return max(n_commands, total commands) cxl_command_info
4054faf31b4SDan Williams 	 * structures.
4064faf31b4SDan Williams 	 */
4074faf31b4SDan Williams 	cxl_for_each_cmd(cmd) {
4084faf31b4SDan Williams 		const struct cxl_command_info *info = &cmd->info;
4094faf31b4SDan Williams 
4104faf31b4SDan Williams 		if (copy_to_user(&q->commands[j++], info, sizeof(*info)))
4114faf31b4SDan Williams 			return -EFAULT;
4124faf31b4SDan Williams 
4134faf31b4SDan Williams 		if (j == n_commands)
4144faf31b4SDan Williams 			break;
4154faf31b4SDan Williams 	}
4164faf31b4SDan Williams 
4174faf31b4SDan Williams 	return 0;
4184faf31b4SDan Williams }
4194faf31b4SDan Williams 
4204faf31b4SDan Williams /**
4214faf31b4SDan Williams  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace.
4225e2411aeSIra Weiny  * @cxlds: The device data for the operation
423d97fe8eeSAlison Schofield  * @mbox_cmd: The validated mailbox command.
4244faf31b4SDan Williams  * @out_payload: Pointer to userspace's output payload.
4254faf31b4SDan Williams  * @size_out: (Input) Max payload size to copy out.
4264faf31b4SDan Williams  *            (Output) Payload size hardware generated.
4274faf31b4SDan Williams  * @retval: Hardware generated return code from the operation.
4284faf31b4SDan Williams  *
4294faf31b4SDan Williams  * Return:
4304faf31b4SDan Williams  *  * %0	- Mailbox transaction succeeded. This implies the mailbox
4314faf31b4SDan Williams  *		  protocol completed successfully not that the operation itself
4324faf31b4SDan Williams  *		  was successful.
4334faf31b4SDan Williams  *  * %-ENOMEM  - Couldn't allocate a bounce buffer.
4344faf31b4SDan Williams  *  * %-EFAULT	- Something happened with copy_to/from_user.
4354faf31b4SDan Williams  *  * %-EINTR	- Mailbox acquisition interrupted.
4364faf31b4SDan Williams  *  * %-EXXX	- Transaction level failures.
4374faf31b4SDan Williams  *
438d97fe8eeSAlison Schofield  * Dispatches a mailbox command on behalf of a userspace request.
439d97fe8eeSAlison Schofield  * The output payload is copied to userspace.
4404faf31b4SDan Williams  *
4414faf31b4SDan Williams  * See cxl_send_cmd().
4424faf31b4SDan Williams  */
4435e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,
444d97fe8eeSAlison Schofield 					struct cxl_mbox_cmd *mbox_cmd,
445d97fe8eeSAlison Schofield 					u64 out_payload, s32 *size_out,
446d97fe8eeSAlison Schofield 					u32 *retval)
4474faf31b4SDan Williams {
4485e2411aeSIra Weiny 	struct device *dev = cxlds->dev;
4494faf31b4SDan Williams 	int rc;
4504faf31b4SDan Williams 
4514faf31b4SDan Williams 	dev_dbg(dev,
4524faf31b4SDan Williams 		"Submitting %s command for user\n"
4534faf31b4SDan Williams 		"\topcode: %x\n"
45482b8ba29SAlison Schofield 		"\tsize: %zx\n",
455d97fe8eeSAlison Schofield 		cxl_mem_opcode_to_name(mbox_cmd->opcode),
456d97fe8eeSAlison Schofield 		mbox_cmd->opcode, mbox_cmd->size_in);
4574faf31b4SDan Williams 
458d97fe8eeSAlison Schofield 	rc = cxlds->mbox_send(cxlds, mbox_cmd);
4594faf31b4SDan Williams 	if (rc)
4604faf31b4SDan Williams 		goto out;
4614faf31b4SDan Williams 
4624faf31b4SDan Williams 	/*
4634faf31b4SDan Williams 	 * @size_out contains the max size that's allowed to be written back out
4644faf31b4SDan Williams 	 * to userspace. While the payload may have written more output than
4654faf31b4SDan Williams 	 * this it will have to be ignored.
4664faf31b4SDan Williams 	 */
467d97fe8eeSAlison Schofield 	if (mbox_cmd->size_out) {
468d97fe8eeSAlison Schofield 		dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out,
4694faf31b4SDan Williams 			      "Invalid return size\n");
4704faf31b4SDan Williams 		if (copy_to_user(u64_to_user_ptr(out_payload),
471d97fe8eeSAlison Schofield 				 mbox_cmd->payload_out, mbox_cmd->size_out)) {
4724faf31b4SDan Williams 			rc = -EFAULT;
4734faf31b4SDan Williams 			goto out;
4744faf31b4SDan Williams 		}
4754faf31b4SDan Williams 	}
4764faf31b4SDan Williams 
477d97fe8eeSAlison Schofield 	*size_out = mbox_cmd->size_out;
478d97fe8eeSAlison Schofield 	*retval = mbox_cmd->return_code;
4794faf31b4SDan Williams 
4804faf31b4SDan Williams out:
481d97fe8eeSAlison Schofield 	cxl_mbox_cmd_dtor(mbox_cmd);
4824faf31b4SDan Williams 	return rc;
4834faf31b4SDan Williams }
4844faf31b4SDan Williams 
4854faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)
4864faf31b4SDan Williams {
4875e2411aeSIra Weiny 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
4884faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
4894faf31b4SDan Williams 	struct cxl_send_command send;
4909ae016aeSAlison Schofield 	struct cxl_mbox_cmd mbox_cmd;
4914faf31b4SDan Williams 	int rc;
4924faf31b4SDan Williams 
4934faf31b4SDan Williams 	dev_dbg(dev, "Send IOCTL\n");
4944faf31b4SDan Williams 
4954faf31b4SDan Williams 	if (copy_from_user(&send, s, sizeof(send)))
4964faf31b4SDan Williams 		return -EFAULT;
4974faf31b4SDan Williams 
498*2dd5600aSAlison Schofield 	rc = cxl_validate_cmd_from_user(&mbox_cmd, cxlmd->cxlds, &send);
4994faf31b4SDan Williams 	if (rc)
5004faf31b4SDan Williams 		return rc;
5014faf31b4SDan Williams 
502d97fe8eeSAlison Schofield 	rc = handle_mailbox_cmd_from_user(cxlds, &mbox_cmd, send.out.payload,
503d97fe8eeSAlison Schofield 					  &send.out.size, &send.retval);
5044faf31b4SDan Williams 	if (rc)
5054faf31b4SDan Williams 		return rc;
5064faf31b4SDan Williams 
5074faf31b4SDan Williams 	if (copy_to_user(s, &send, sizeof(send)))
5084faf31b4SDan Williams 		return -EFAULT;
5094faf31b4SDan Williams 
5104faf31b4SDan Williams 	return 0;
5114faf31b4SDan Williams }
5124faf31b4SDan Williams 
5135e2411aeSIra Weiny static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8 *out)
5144faf31b4SDan Williams {
5154faf31b4SDan Williams 	u32 remaining = size;
5164faf31b4SDan Williams 	u32 offset = 0;
5174faf31b4SDan Williams 
5184faf31b4SDan Williams 	while (remaining) {
5195e2411aeSIra Weiny 		u32 xfer_size = min_t(u32, remaining, cxlds->payload_size);
52049be6dd8SDan Williams 		struct cxl_mbox_get_log log = {
5214faf31b4SDan Williams 			.uuid = *uuid,
5224faf31b4SDan Williams 			.offset = cpu_to_le32(offset),
5234faf31b4SDan Williams 			.length = cpu_to_le32(xfer_size)
5244faf31b4SDan Williams 		};
5254faf31b4SDan Williams 		int rc;
5264faf31b4SDan Williams 
5275e2411aeSIra Weiny 		rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_LOG, &log, sizeof(log),
5285e2411aeSIra Weiny 				       out, xfer_size);
5294faf31b4SDan Williams 		if (rc < 0)
5304faf31b4SDan Williams 			return rc;
5314faf31b4SDan Williams 
5324faf31b4SDan Williams 		out += xfer_size;
5334faf31b4SDan Williams 		remaining -= xfer_size;
5344faf31b4SDan Williams 		offset += xfer_size;
5354faf31b4SDan Williams 	}
5364faf31b4SDan Williams 
5374faf31b4SDan Williams 	return 0;
5384faf31b4SDan Williams }
5394faf31b4SDan Williams 
5404faf31b4SDan Williams /**
5414faf31b4SDan Williams  * cxl_walk_cel() - Walk through the Command Effects Log.
5425e2411aeSIra Weiny  * @cxlds: The device data for the operation
5434faf31b4SDan Williams  * @size: Length of the Command Effects Log.
5444faf31b4SDan Williams  * @cel: CEL
5454faf31b4SDan Williams  *
5464faf31b4SDan Williams  * Iterate over each entry in the CEL and determine if the driver supports the
5474faf31b4SDan Williams  * command. If so, the command is enabled for the device and can be used later.
5484faf31b4SDan Williams  */
5495e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel)
5504faf31b4SDan Williams {
55149be6dd8SDan Williams 	struct cxl_cel_entry *cel_entry;
5524faf31b4SDan Williams 	const int cel_entries = size / sizeof(*cel_entry);
5534faf31b4SDan Williams 	int i;
5544faf31b4SDan Williams 
55549be6dd8SDan Williams 	cel_entry = (struct cxl_cel_entry *) cel;
5564faf31b4SDan Williams 
5574faf31b4SDan Williams 	for (i = 0; i < cel_entries; i++) {
5584faf31b4SDan Williams 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
5594faf31b4SDan Williams 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
5604faf31b4SDan Williams 
5614faf31b4SDan Williams 		if (!cmd) {
5625e2411aeSIra Weiny 			dev_dbg(cxlds->dev,
5634faf31b4SDan Williams 				"Opcode 0x%04x unsupported by driver", opcode);
5644faf31b4SDan Williams 			continue;
5654faf31b4SDan Williams 		}
5664faf31b4SDan Williams 
5675e2411aeSIra Weiny 		set_bit(cmd->info.id, cxlds->enabled_cmds);
5684faf31b4SDan Williams 	}
5694faf31b4SDan Williams }
5704faf31b4SDan Williams 
5715e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds)
5724faf31b4SDan Williams {
5734faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *ret;
5744faf31b4SDan Williams 	int rc;
5754faf31b4SDan Williams 
5765e2411aeSIra Weiny 	ret = kvmalloc(cxlds->payload_size, GFP_KERNEL);
5774faf31b4SDan Williams 	if (!ret)
5784faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
5794faf31b4SDan Williams 
5805e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_SUPPORTED_LOGS, NULL, 0, ret,
5815e2411aeSIra Weiny 			       cxlds->payload_size);
5824faf31b4SDan Williams 	if (rc < 0) {
5834faf31b4SDan Williams 		kvfree(ret);
5844faf31b4SDan Williams 		return ERR_PTR(rc);
5854faf31b4SDan Williams 	}
5864faf31b4SDan Williams 
5874faf31b4SDan Williams 	return ret;
5884faf31b4SDan Williams }
5894faf31b4SDan Williams 
5904faf31b4SDan Williams enum {
5914faf31b4SDan Williams 	CEL_UUID,
5924faf31b4SDan Williams 	VENDOR_DEBUG_UUID,
5934faf31b4SDan Williams };
5944faf31b4SDan Williams 
5954faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */
5964faf31b4SDan Williams static const uuid_t log_uuid[] = {
59749be6dd8SDan Williams 	[CEL_UUID] = DEFINE_CXL_CEL_UUID,
59849be6dd8SDan Williams 	[VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID,
5994faf31b4SDan Williams };
6004faf31b4SDan Williams 
6014faf31b4SDan Williams /**
6025e2411aeSIra Weiny  * cxl_enumerate_cmds() - Enumerate commands for a device.
6035e2411aeSIra Weiny  * @cxlds: The device data for the operation
6044faf31b4SDan Williams  *
6054faf31b4SDan Williams  * Returns 0 if enumerate completed successfully.
6064faf31b4SDan Williams  *
6074faf31b4SDan Williams  * CXL devices have optional support for certain commands. This function will
6084faf31b4SDan Williams  * determine the set of supported commands for the hardware and update the
6095e2411aeSIra Weiny  * enabled_cmds bitmap in the @cxlds.
6104faf31b4SDan Williams  */
6115e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds)
6124faf31b4SDan Williams {
6134faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *gsl;
6145e2411aeSIra Weiny 	struct device *dev = cxlds->dev;
6154faf31b4SDan Williams 	struct cxl_mem_command *cmd;
6164faf31b4SDan Williams 	int i, rc;
6174faf31b4SDan Williams 
6185e2411aeSIra Weiny 	gsl = cxl_get_gsl(cxlds);
6194faf31b4SDan Williams 	if (IS_ERR(gsl))
6204faf31b4SDan Williams 		return PTR_ERR(gsl);
6214faf31b4SDan Williams 
6224faf31b4SDan Williams 	rc = -ENOENT;
6234faf31b4SDan Williams 	for (i = 0; i < le16_to_cpu(gsl->entries); i++) {
6244faf31b4SDan Williams 		u32 size = le32_to_cpu(gsl->entry[i].size);
6254faf31b4SDan Williams 		uuid_t uuid = gsl->entry[i].uuid;
6264faf31b4SDan Williams 		u8 *log;
6274faf31b4SDan Williams 
6284faf31b4SDan Williams 		dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size);
6294faf31b4SDan Williams 
6304faf31b4SDan Williams 		if (!uuid_equal(&uuid, &log_uuid[CEL_UUID]))
6314faf31b4SDan Williams 			continue;
6324faf31b4SDan Williams 
6334faf31b4SDan Williams 		log = kvmalloc(size, GFP_KERNEL);
6344faf31b4SDan Williams 		if (!log) {
6354faf31b4SDan Williams 			rc = -ENOMEM;
6364faf31b4SDan Williams 			goto out;
6374faf31b4SDan Williams 		}
6384faf31b4SDan Williams 
6395e2411aeSIra Weiny 		rc = cxl_xfer_log(cxlds, &uuid, size, log);
6404faf31b4SDan Williams 		if (rc) {
6414faf31b4SDan Williams 			kvfree(log);
6424faf31b4SDan Williams 			goto out;
6434faf31b4SDan Williams 		}
6444faf31b4SDan Williams 
6455e2411aeSIra Weiny 		cxl_walk_cel(cxlds, size, log);
6464faf31b4SDan Williams 		kvfree(log);
6474faf31b4SDan Williams 
6484faf31b4SDan Williams 		/* In case CEL was bogus, enable some default commands. */
6494faf31b4SDan Williams 		cxl_for_each_cmd(cmd)
6504faf31b4SDan Williams 			if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE)
6515e2411aeSIra Weiny 				set_bit(cmd->info.id, cxlds->enabled_cmds);
6524faf31b4SDan Williams 
6534faf31b4SDan Williams 		/* Found the required CEL */
6544faf31b4SDan Williams 		rc = 0;
6554faf31b4SDan Williams 	}
6564faf31b4SDan Williams 
6574faf31b4SDan Williams out:
6584faf31b4SDan Williams 	kvfree(gsl);
6594faf31b4SDan Williams 	return rc;
6604faf31b4SDan Williams }
661affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL);
6624faf31b4SDan Williams 
6634faf31b4SDan Williams /**
6644faf31b4SDan Williams  * cxl_mem_get_partition_info - Get partition info
6655e2411aeSIra Weiny  * @cxlds: The device data for the operation
6664faf31b4SDan Williams  *
6674faf31b4SDan Williams  * Retrieve the current partition info for the device specified.  The active
6684faf31b4SDan Williams  * values are the current capacity in bytes.  If not 0, the 'next' values are
6694faf31b4SDan Williams  * the pending values, in bytes, which take affect on next cold reset.
6704faf31b4SDan Williams  *
6714faf31b4SDan Williams  * Return: 0 if no error: or the result of the mailbox command.
6724faf31b4SDan Williams  *
6734faf31b4SDan Williams  * See CXL @8.2.9.5.2.1 Get Partition Info
6744faf31b4SDan Williams  */
6755e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds)
6764faf31b4SDan Williams {
6774faf31b4SDan Williams 	struct cxl_mbox_get_partition_info {
6784faf31b4SDan Williams 		__le64 active_volatile_cap;
6794faf31b4SDan Williams 		__le64 active_persistent_cap;
6804faf31b4SDan Williams 		__le64 next_volatile_cap;
6814faf31b4SDan Williams 		__le64 next_persistent_cap;
6824faf31b4SDan Williams 	} __packed pi;
6834faf31b4SDan Williams 	int rc;
6844faf31b4SDan Williams 
6855e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_PARTITION_INFO, NULL, 0,
6865e2411aeSIra Weiny 			       &pi, sizeof(pi));
6874faf31b4SDan Williams 
6884faf31b4SDan Williams 	if (rc)
6894faf31b4SDan Williams 		return rc;
6904faf31b4SDan Williams 
6915e2411aeSIra Weiny 	cxlds->active_volatile_bytes =
6924faf31b4SDan Williams 		le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6935e2411aeSIra Weiny 	cxlds->active_persistent_bytes =
6944faf31b4SDan Williams 		le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER;
6955e2411aeSIra Weiny 	cxlds->next_volatile_bytes =
6964faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6975e2411aeSIra Weiny 	cxlds->next_persistent_bytes =
6984faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
6994faf31b4SDan Williams 
7004faf31b4SDan Williams 	return 0;
7014faf31b4SDan Williams }
7024faf31b4SDan Williams 
7034faf31b4SDan Williams /**
7045e2411aeSIra Weiny  * cxl_dev_state_identify() - Send the IDENTIFY command to the device.
7055e2411aeSIra Weiny  * @cxlds: The device data for the operation
7064faf31b4SDan Williams  *
7074faf31b4SDan Williams  * Return: 0 if identify was executed successfully.
7084faf31b4SDan Williams  *
7094faf31b4SDan Williams  * This will dispatch the identify command to the device and on success populate
7104faf31b4SDan Williams  * structures to be exported to sysfs.
7114faf31b4SDan Williams  */
7125e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds)
7134faf31b4SDan Williams {
7144faf31b4SDan Williams 	/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
71549be6dd8SDan Williams 	struct cxl_mbox_identify id;
7164faf31b4SDan Williams 	int rc;
7174faf31b4SDan Williams 
7185e2411aeSIra Weiny 	rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_IDENTIFY, NULL, 0, &id,
7194faf31b4SDan Williams 			       sizeof(id));
7204faf31b4SDan Williams 	if (rc < 0)
7214faf31b4SDan Williams 		return rc;
7224faf31b4SDan Williams 
7235e2411aeSIra Weiny 	cxlds->total_bytes =
7244faf31b4SDan Williams 		le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER;
7255e2411aeSIra Weiny 	cxlds->volatile_only_bytes =
7264faf31b4SDan Williams 		le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER;
7275e2411aeSIra Weiny 	cxlds->persistent_only_bytes =
7284faf31b4SDan Williams 		le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER;
7295e2411aeSIra Weiny 	cxlds->partition_align_bytes =
7304faf31b4SDan Williams 		le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER;
7314faf31b4SDan Williams 
7325e2411aeSIra Weiny 	dev_dbg(cxlds->dev,
7334faf31b4SDan Williams 		"Identify Memory Device\n"
7344faf31b4SDan Williams 		"     total_bytes = %#llx\n"
7354faf31b4SDan Williams 		"     volatile_only_bytes = %#llx\n"
7364faf31b4SDan Williams 		"     persistent_only_bytes = %#llx\n"
7374faf31b4SDan Williams 		"     partition_align_bytes = %#llx\n",
7385e2411aeSIra Weiny 		cxlds->total_bytes, cxlds->volatile_only_bytes,
7395e2411aeSIra Weiny 		cxlds->persistent_only_bytes, cxlds->partition_align_bytes);
7404faf31b4SDan Williams 
7415e2411aeSIra Weiny 	cxlds->lsa_size = le32_to_cpu(id.lsa_size);
7425e2411aeSIra Weiny 	memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision));
7434faf31b4SDan Williams 
7444faf31b4SDan Williams 	return 0;
7454faf31b4SDan Williams }
746affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL);
7474faf31b4SDan Williams 
7485e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds)
7494faf31b4SDan Williams {
7504faf31b4SDan Williams 	int rc;
7514faf31b4SDan Williams 
7525e2411aeSIra Weiny 	if (cxlds->partition_align_bytes == 0) {
7535e2411aeSIra Weiny 		cxlds->ram_range.start = 0;
7545e2411aeSIra Weiny 		cxlds->ram_range.end = cxlds->volatile_only_bytes - 1;
7555e2411aeSIra Weiny 		cxlds->pmem_range.start = cxlds->volatile_only_bytes;
7565e2411aeSIra Weiny 		cxlds->pmem_range.end = cxlds->volatile_only_bytes +
7575e2411aeSIra Weiny 				       cxlds->persistent_only_bytes - 1;
7584faf31b4SDan Williams 		return 0;
7594faf31b4SDan Williams 	}
7604faf31b4SDan Williams 
7615e2411aeSIra Weiny 	rc = cxl_mem_get_partition_info(cxlds);
7624faf31b4SDan Williams 	if (rc) {
7635e2411aeSIra Weiny 		dev_err(cxlds->dev, "Failed to query partition information\n");
7644faf31b4SDan Williams 		return rc;
7654faf31b4SDan Williams 	}
7664faf31b4SDan Williams 
7675e2411aeSIra Weiny 	dev_dbg(cxlds->dev,
7684faf31b4SDan Williams 		"Get Partition Info\n"
7694faf31b4SDan Williams 		"     active_volatile_bytes = %#llx\n"
7704faf31b4SDan Williams 		"     active_persistent_bytes = %#llx\n"
7714faf31b4SDan Williams 		"     next_volatile_bytes = %#llx\n"
7724faf31b4SDan Williams 		"     next_persistent_bytes = %#llx\n",
7735e2411aeSIra Weiny 		cxlds->active_volatile_bytes, cxlds->active_persistent_bytes,
7745e2411aeSIra Weiny 		cxlds->next_volatile_bytes, cxlds->next_persistent_bytes);
7754faf31b4SDan Williams 
7765e2411aeSIra Weiny 	cxlds->ram_range.start = 0;
7775e2411aeSIra Weiny 	cxlds->ram_range.end = cxlds->active_volatile_bytes - 1;
7784faf31b4SDan Williams 
7795e2411aeSIra Weiny 	cxlds->pmem_range.start = cxlds->active_volatile_bytes;
7805e2411aeSIra Weiny 	cxlds->pmem_range.end =
7815e2411aeSIra Weiny 		cxlds->active_volatile_bytes + cxlds->active_persistent_bytes - 1;
7824faf31b4SDan Williams 
7834faf31b4SDan Williams 	return 0;
7844faf31b4SDan Williams }
785affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL);
7864faf31b4SDan Williams 
7875e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev)
7884faf31b4SDan Williams {
7895e2411aeSIra Weiny 	struct cxl_dev_state *cxlds;
7904faf31b4SDan Williams 
7915e2411aeSIra Weiny 	cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL);
7925e2411aeSIra Weiny 	if (!cxlds) {
7934faf31b4SDan Williams 		dev_err(dev, "No memory available\n");
7944faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
7954faf31b4SDan Williams 	}
7964faf31b4SDan Williams 
7975e2411aeSIra Weiny 	mutex_init(&cxlds->mbox_mutex);
7985e2411aeSIra Weiny 	cxlds->dev = dev;
7994faf31b4SDan Williams 
8005e2411aeSIra Weiny 	return cxlds;
8014faf31b4SDan Williams }
802affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL);
8034faf31b4SDan Williams 
8044faf31b4SDan Williams static struct dentry *cxl_debugfs;
8054faf31b4SDan Williams 
8064faf31b4SDan Williams void __init cxl_mbox_init(void)
8074faf31b4SDan Williams {
8084faf31b4SDan Williams 	struct dentry *mbox_debugfs;
8094faf31b4SDan Williams 
8104faf31b4SDan Williams 	cxl_debugfs = debugfs_create_dir("cxl", NULL);
8114faf31b4SDan Williams 	mbox_debugfs = debugfs_create_dir("mbox", cxl_debugfs);
8124faf31b4SDan Williams 	debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs,
8134faf31b4SDan Williams 			    &cxl_raw_allow_all);
8144faf31b4SDan Williams }
8154faf31b4SDan Williams 
8164faf31b4SDan Williams void cxl_mbox_exit(void)
8174faf31b4SDan Williams {
8184faf31b4SDan Williams 	debugfs_remove_recursive(cxl_debugfs);
8194faf31b4SDan Williams }
820