xref: /linux/drivers/cxl/core/mbox.c (revision f9c683386f5bc0364615138ce2b14be50848dbcf)
14faf31b4SDan Williams // SPDX-License-Identifier: GPL-2.0-only
24faf31b4SDan Williams /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
34faf31b4SDan Williams #include <linux/security.h>
44faf31b4SDan Williams #include <linux/debugfs.h>
5fa884345SJonathan Cameron #include <linux/ktime.h>
64faf31b4SDan Williams #include <linux/mutex.h>
7ed83f7caSAlison Schofield #include <asm/unaligned.h>
8d0abf578SAlison Schofield #include <cxlpci.h>
94faf31b4SDan Williams #include <cxlmem.h>
104faf31b4SDan Williams #include <cxl.h>
114faf31b4SDan Williams 
124faf31b4SDan Williams #include "core.h"
136ebe28f9SIra Weiny #include "trace.h"
144faf31b4SDan Williams 
154faf31b4SDan Williams static bool cxl_raw_allow_all;
164faf31b4SDan Williams 
174faf31b4SDan Williams /**
184faf31b4SDan Williams  * DOC: cxl mbox
194faf31b4SDan Williams  *
204faf31b4SDan Williams  * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The
214faf31b4SDan Williams  * implementation is used by the cxl_pci driver to initialize the device
224faf31b4SDan Williams  * and implement the cxl_mem.h IOCTL UAPI. It also implements the
234faf31b4SDan Williams  * backend of the cxl_pmem_ctl() transport for LIBNVDIMM.
244faf31b4SDan Williams  */
254faf31b4SDan Williams 
264faf31b4SDan Williams #define cxl_for_each_cmd(cmd)                                                  \
274faf31b4SDan Williams 	for ((cmd) = &cxl_mem_commands[0];                                     \
284faf31b4SDan Williams 	     ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++)
294faf31b4SDan Williams 
304faf31b4SDan Williams #define CXL_CMD(_id, sin, sout, _flags)                                        \
314faf31b4SDan Williams 	[CXL_MEM_COMMAND_ID_##_id] = {                                         \
324faf31b4SDan Williams 	.info =	{                                                              \
334faf31b4SDan Williams 			.id = CXL_MEM_COMMAND_ID_##_id,                        \
344faf31b4SDan Williams 			.size_in = sin,                                        \
354faf31b4SDan Williams 			.size_out = sout,                                      \
364faf31b4SDan Williams 		},                                                             \
374faf31b4SDan Williams 	.opcode = CXL_MBOX_OP_##_id,                                           \
384faf31b4SDan Williams 	.flags = _flags,                                                       \
394faf31b4SDan Williams 	}
404faf31b4SDan Williams 
4126f89535SAlison Schofield #define CXL_VARIABLE_PAYLOAD	~0U
424faf31b4SDan Williams /*
434faf31b4SDan Williams  * This table defines the supported mailbox commands for the driver. This table
444faf31b4SDan Williams  * is made up of a UAPI structure. Non-negative values as parameters in the
454faf31b4SDan Williams  * table will be validated against the user's input. For example, if size_in is
464faf31b4SDan Williams  * 0, and the user passed in 1, it is an error.
474faf31b4SDan Williams  */
484faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
494faf31b4SDan Williams 	CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE),
504faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS
5126f89535SAlison Schofield 	CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0),
524faf31b4SDan Williams #endif
5326f89535SAlison Schofield 	CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE),
544faf31b4SDan Williams 	CXL_CMD(GET_FW_INFO, 0, 0x50, 0),
554faf31b4SDan Williams 	CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0),
5626f89535SAlison Schofield 	CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0),
574faf31b4SDan Williams 	CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0),
5826f89535SAlison Schofield 	CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE),
594faf31b4SDan Williams 	CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0),
6026f89535SAlison Schofield 	CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0),
614faf31b4SDan Williams 	CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
624faf31b4SDan Williams 	CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0),
634faf31b4SDan Williams 	CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0),
644faf31b4SDan Williams 	CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0),
654faf31b4SDan Williams 	CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0),
664faf31b4SDan Williams };
674faf31b4SDan Williams 
684faf31b4SDan Williams /*
694faf31b4SDan Williams  * Commands that RAW doesn't permit. The rationale for each:
704faf31b4SDan Williams  *
714faf31b4SDan Williams  * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment /
724faf31b4SDan Williams  * coordination of transaction timeout values at the root bridge level.
734faf31b4SDan Williams  *
744faf31b4SDan Williams  * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live
754faf31b4SDan Williams  * and needs to be coordinated with HDM updates.
764faf31b4SDan Williams  *
774faf31b4SDan Williams  * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the
784faf31b4SDan Williams  * driver and any writes from userspace invalidates those contents.
794faf31b4SDan Williams  *
804faf31b4SDan Williams  * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes
814faf31b4SDan Williams  * to the device after it is marked clean, userspace can not make that
824faf31b4SDan Williams  * assertion.
834faf31b4SDan Williams  *
844faf31b4SDan Williams  * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that
854faf31b4SDan Williams  * is kept up to date with patrol notifications and error management.
86dec441d3SAlison Schofield  *
87dec441d3SAlison Schofield  * CXL_MBOX_OP_[GET_,INJECT_,CLEAR_]POISON: These commands require kernel
88dec441d3SAlison Schofield  * driver orchestration for safety.
894faf31b4SDan Williams  */
904faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = {
914faf31b4SDan Williams 	CXL_MBOX_OP_ACTIVATE_FW,
924faf31b4SDan Williams 	CXL_MBOX_OP_SET_PARTITION_INFO,
934faf31b4SDan Williams 	CXL_MBOX_OP_SET_LSA,
944faf31b4SDan Williams 	CXL_MBOX_OP_SET_SHUTDOWN_STATE,
954faf31b4SDan Williams 	CXL_MBOX_OP_SCAN_MEDIA,
964faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA,
97dec441d3SAlison Schofield 	CXL_MBOX_OP_GET_POISON,
98dec441d3SAlison Schofield 	CXL_MBOX_OP_INJECT_POISON,
99dec441d3SAlison Schofield 	CXL_MBOX_OP_CLEAR_POISON,
1004faf31b4SDan Williams };
1014faf31b4SDan Williams 
1024faf31b4SDan Williams /*
1034faf31b4SDan Williams  * Command sets that RAW doesn't permit. All opcodes in this set are
1044faf31b4SDan Williams  * disabled because they pass plain text security payloads over the
1054faf31b4SDan Williams  * user/kernel boundary. This functionality is intended to be wrapped
1064faf31b4SDan Williams  * behind the keys ABI which allows for encrypted payloads in the UAPI
1074faf31b4SDan Williams  */
1084faf31b4SDan Williams static u8 security_command_sets[] = {
1094faf31b4SDan Williams 	0x44, /* Sanitize */
1104faf31b4SDan Williams 	0x45, /* Persistent Memory Data-at-rest Security */
1114faf31b4SDan Williams 	0x46, /* Security Passthrough */
1124faf31b4SDan Williams };
1134faf31b4SDan Williams 
1144faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode)
1154faf31b4SDan Williams {
1164faf31b4SDan Williams 	int i;
1174faf31b4SDan Williams 
1184faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(security_command_sets); i++)
1194faf31b4SDan Williams 		if (security_command_sets[i] == (opcode >> 8))
1204faf31b4SDan Williams 			return true;
1214faf31b4SDan Williams 	return false;
1224faf31b4SDan Williams }
1234faf31b4SDan Williams 
124ad64f595SDavidlohr Bueso static void cxl_set_security_cmd_enabled(struct cxl_security_state *security,
125ad64f595SDavidlohr Bueso 					 u16 opcode)
126ad64f595SDavidlohr Bueso {
127ad64f595SDavidlohr Bueso 	switch (opcode) {
128ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SANITIZE:
129ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SANITIZE, security->enabled_cmds);
130ad64f595SDavidlohr Bueso 		break;
131ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SECURE_ERASE:
132ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SECURE_ERASE,
133ad64f595SDavidlohr Bueso 			security->enabled_cmds);
134ad64f595SDavidlohr Bueso 		break;
135ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_GET_SECURITY_STATE:
136ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_GET_SECURITY_STATE,
137ad64f595SDavidlohr Bueso 			security->enabled_cmds);
138ad64f595SDavidlohr Bueso 		break;
139ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SET_PASSPHRASE:
140ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SET_PASSPHRASE,
141ad64f595SDavidlohr Bueso 			security->enabled_cmds);
142ad64f595SDavidlohr Bueso 		break;
143ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_DISABLE_PASSPHRASE:
144ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
145ad64f595SDavidlohr Bueso 			security->enabled_cmds);
146ad64f595SDavidlohr Bueso 		break;
147ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_UNLOCK:
148ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_UNLOCK, security->enabled_cmds);
149ad64f595SDavidlohr Bueso 		break;
150ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_FREEZE_SECURITY:
151ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_FREEZE_SECURITY,
152ad64f595SDavidlohr Bueso 			security->enabled_cmds);
153ad64f595SDavidlohr Bueso 		break;
154ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE:
155ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
156ad64f595SDavidlohr Bueso 			security->enabled_cmds);
157ad64f595SDavidlohr Bueso 		break;
158ad64f595SDavidlohr Bueso 	default:
159ad64f595SDavidlohr Bueso 		break;
160ad64f595SDavidlohr Bueso 	}
161ad64f595SDavidlohr Bueso }
162ad64f595SDavidlohr Bueso 
163d0abf578SAlison Schofield static bool cxl_is_poison_command(u16 opcode)
164d0abf578SAlison Schofield {
165d0abf578SAlison Schofield #define CXL_MBOX_OP_POISON_CMDS 0x43
166d0abf578SAlison Schofield 
167d0abf578SAlison Schofield 	if ((opcode >> 8) == CXL_MBOX_OP_POISON_CMDS)
168d0abf578SAlison Schofield 		return true;
169d0abf578SAlison Schofield 
170d0abf578SAlison Schofield 	return false;
171d0abf578SAlison Schofield }
172d0abf578SAlison Schofield 
173d0abf578SAlison Schofield static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison,
174d0abf578SAlison Schofield 				       u16 opcode)
175d0abf578SAlison Schofield {
176d0abf578SAlison Schofield 	switch (opcode) {
177d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_POISON:
178d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_LIST, poison->enabled_cmds);
179d0abf578SAlison Schofield 		break;
180d0abf578SAlison Schofield 	case CXL_MBOX_OP_INJECT_POISON:
181d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_INJECT, poison->enabled_cmds);
182d0abf578SAlison Schofield 		break;
183d0abf578SAlison Schofield 	case CXL_MBOX_OP_CLEAR_POISON:
184d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_CLEAR, poison->enabled_cmds);
185d0abf578SAlison Schofield 		break;
186d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS:
187d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_CAPS, poison->enabled_cmds);
188d0abf578SAlison Schofield 		break;
189d0abf578SAlison Schofield 	case CXL_MBOX_OP_SCAN_MEDIA:
190d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_MEDIA, poison->enabled_cmds);
191d0abf578SAlison Schofield 		break;
192d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_SCAN_MEDIA:
193d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_RESULTS, poison->enabled_cmds);
194d0abf578SAlison Schofield 		break;
195d0abf578SAlison Schofield 	default:
196d0abf578SAlison Schofield 		break;
197d0abf578SAlison Schofield 	}
198d0abf578SAlison Schofield }
199d0abf578SAlison Schofield 
2004faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
2014faf31b4SDan Williams {
2024faf31b4SDan Williams 	struct cxl_mem_command *c;
2034faf31b4SDan Williams 
2044faf31b4SDan Williams 	cxl_for_each_cmd(c)
2054faf31b4SDan Williams 		if (c->opcode == opcode)
2064faf31b4SDan Williams 			return c;
2074faf31b4SDan Williams 
2084faf31b4SDan Williams 	return NULL;
2094faf31b4SDan Williams }
2104faf31b4SDan Williams 
21182b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode)
21282b8ba29SAlison Schofield {
21382b8ba29SAlison Schofield 	struct cxl_mem_command *c;
21482b8ba29SAlison Schofield 
21582b8ba29SAlison Schofield 	c = cxl_mem_find_command(opcode);
21682b8ba29SAlison Schofield 	if (!c)
21782b8ba29SAlison Schofield 		return NULL;
21882b8ba29SAlison Schofield 
21982b8ba29SAlison Schofield 	return cxl_command_names[c->info.id].name;
22082b8ba29SAlison Schofield }
22182b8ba29SAlison Schofield 
2224faf31b4SDan Williams /**
2235331cdf4SDan Williams  * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command
22459f8d151SDan Williams  * @mds: The driver data for the operation
2255331cdf4SDan Williams  * @mbox_cmd: initialized command to execute
2264faf31b4SDan Williams  *
227ee92c7e2SDavidlohr Bueso  * Context: Any context.
2284faf31b4SDan Williams  * Return:
2294faf31b4SDan Williams  *  * %>=0	- Number of bytes returned in @out.
2304faf31b4SDan Williams  *  * %-E2BIG	- Payload is too large for hardware.
2314faf31b4SDan Williams  *  * %-EBUSY	- Couldn't acquire exclusive mailbox access.
2324faf31b4SDan Williams  *  * %-EFAULT	- Hardware error occurred.
2334faf31b4SDan Williams  *  * %-ENXIO	- Command completed, but device reported an error.
2344faf31b4SDan Williams  *  * %-EIO	- Unexpected output size.
2354faf31b4SDan Williams  *
2364faf31b4SDan Williams  * Mailbox commands may execute successfully yet the device itself reported an
2374faf31b4SDan Williams  * error. While this distinction can be useful for commands from userspace, the
2384faf31b4SDan Williams  * kernel will only be able to use results when both are successful.
2394faf31b4SDan Williams  */
24059f8d151SDan Williams int cxl_internal_send_cmd(struct cxl_memdev_state *mds,
2415331cdf4SDan Williams 			  struct cxl_mbox_cmd *mbox_cmd)
2424faf31b4SDan Williams {
2432aeaf663SDan Williams 	size_t out_size, min_out;
2444faf31b4SDan Williams 	int rc;
2454faf31b4SDan Williams 
24659f8d151SDan Williams 	if (mbox_cmd->size_in > mds->payload_size ||
24759f8d151SDan Williams 	    mbox_cmd->size_out > mds->payload_size)
2484faf31b4SDan Williams 		return -E2BIG;
2494faf31b4SDan Williams 
2505331cdf4SDan Williams 	out_size = mbox_cmd->size_out;
2512aeaf663SDan Williams 	min_out = mbox_cmd->min_out;
25259f8d151SDan Williams 	rc = mds->mbox_send(mds, mbox_cmd);
253623c0751SRobert Richter 	/*
254623c0751SRobert Richter 	 * EIO is reserved for a payload size mismatch and mbox_send()
255623c0751SRobert Richter 	 * may not return this error.
256623c0751SRobert Richter 	 */
257623c0751SRobert Richter 	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))
258623c0751SRobert Richter 		return -ENXIO;
2594faf31b4SDan Williams 	if (rc)
2604faf31b4SDan Williams 		return rc;
2614faf31b4SDan Williams 
262ccadf131SDavidlohr Bueso 	if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS &&
263ccadf131SDavidlohr Bueso 	    mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND)
2645331cdf4SDan Williams 		return cxl_mbox_cmd_rc2errno(mbox_cmd);
2654faf31b4SDan Williams 
2662aeaf663SDan Williams 	if (!out_size)
2672aeaf663SDan Williams 		return 0;
2682aeaf663SDan Williams 
2694faf31b4SDan Williams 	/*
2702aeaf663SDan Williams 	 * Variable sized output needs to at least satisfy the caller's
2712aeaf663SDan Williams 	 * minimum if not the fully requested size.
2724faf31b4SDan Williams 	 */
2732aeaf663SDan Williams 	if (min_out == 0)
2742aeaf663SDan Williams 		min_out = out_size;
2752aeaf663SDan Williams 
2762aeaf663SDan Williams 	if (mbox_cmd->size_out < min_out)
2774faf31b4SDan Williams 		return -EIO;
2784faf31b4SDan Williams 	return 0;
2794faf31b4SDan Williams }
2805331cdf4SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, CXL);
2814faf31b4SDan Williams 
2824faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode)
2834faf31b4SDan Williams {
2844faf31b4SDan Williams 	int i;
2854faf31b4SDan Williams 
2864faf31b4SDan Williams 	if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS))
2874faf31b4SDan Williams 		return false;
2884faf31b4SDan Williams 
2894faf31b4SDan Williams 	if (security_locked_down(LOCKDOWN_PCI_ACCESS))
2904faf31b4SDan Williams 		return false;
2914faf31b4SDan Williams 
2924faf31b4SDan Williams 	if (cxl_raw_allow_all)
2934faf31b4SDan Williams 		return true;
2944faf31b4SDan Williams 
2954faf31b4SDan Williams 	if (cxl_is_security_command(opcode))
2964faf31b4SDan Williams 		return false;
2974faf31b4SDan Williams 
2984faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++)
2994faf31b4SDan Williams 		if (cxl_disabled_raw_commands[i] == opcode)
3004faf31b4SDan Williams 			return false;
3014faf31b4SDan Williams 
3024faf31b4SDan Williams 	return true;
3034faf31b4SDan Williams }
3044faf31b4SDan Williams 
3056179045cSAlison Schofield /**
3066179045cSAlison Schofield  * cxl_payload_from_user_allowed() - Check contents of in_payload.
3076179045cSAlison Schofield  * @opcode: The mailbox command opcode.
3086179045cSAlison Schofield  * @payload_in: Pointer to the input payload passed in from user space.
3096179045cSAlison Schofield  *
3106179045cSAlison Schofield  * Return:
3116179045cSAlison Schofield  *  * true	- payload_in passes check for @opcode.
3126179045cSAlison Schofield  *  * false	- payload_in contains invalid or unsupported values.
3136179045cSAlison Schofield  *
3146179045cSAlison Schofield  * The driver may inspect payload contents before sending a mailbox
3156179045cSAlison Schofield  * command from user space to the device. The intent is to reject
3166179045cSAlison Schofield  * commands with input payloads that are known to be unsafe. This
3176179045cSAlison Schofield  * check is not intended to replace the users careful selection of
3186179045cSAlison Schofield  * mailbox command parameters and makes no guarantee that the user
3196179045cSAlison Schofield  * command will succeed, nor that it is appropriate.
3206179045cSAlison Schofield  *
3216179045cSAlison Schofield  * The specific checks are determined by the opcode.
3226179045cSAlison Schofield  */
3236179045cSAlison Schofield static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in)
3246179045cSAlison Schofield {
3256179045cSAlison Schofield 	switch (opcode) {
3266179045cSAlison Schofield 	case CXL_MBOX_OP_SET_PARTITION_INFO: {
3276179045cSAlison Schofield 		struct cxl_mbox_set_partition_info *pi = payload_in;
3286179045cSAlison Schofield 
32935e01667SDan Carpenter 		if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG)
3306179045cSAlison Schofield 			return false;
3316179045cSAlison Schofield 		break;
3326179045cSAlison Schofield 	}
3336179045cSAlison Schofield 	default:
3346179045cSAlison Schofield 		break;
3356179045cSAlison Schofield 	}
3366179045cSAlison Schofield 	return true;
3376179045cSAlison Schofield }
3386179045cSAlison Schofield 
33963cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
34059f8d151SDan Williams 			     struct cxl_memdev_state *mds, u16 opcode,
34163cf60b7SAlison Schofield 			     size_t in_size, size_t out_size, u64 in_payload)
34263cf60b7SAlison Schofield {
34363cf60b7SAlison Schofield 	*mbox = (struct cxl_mbox_cmd) {
34463cf60b7SAlison Schofield 		.opcode = opcode,
34563cf60b7SAlison Schofield 		.size_in = in_size,
34663cf60b7SAlison Schofield 	};
34763cf60b7SAlison Schofield 
34863cf60b7SAlison Schofield 	if (in_size) {
34963cf60b7SAlison Schofield 		mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload),
35063cf60b7SAlison Schofield 						in_size);
351280302f0SAlison Schofield 		if (IS_ERR(mbox->payload_in))
35263cf60b7SAlison Schofield 			return PTR_ERR(mbox->payload_in);
3536179045cSAlison Schofield 
3546179045cSAlison Schofield 		if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) {
35559f8d151SDan Williams 			dev_dbg(mds->cxlds.dev, "%s: input payload not allowed\n",
3566179045cSAlison Schofield 				cxl_mem_opcode_to_name(opcode));
3576179045cSAlison Schofield 			kvfree(mbox->payload_in);
3586179045cSAlison Schofield 			return -EBUSY;
3596179045cSAlison Schofield 		}
36063cf60b7SAlison Schofield 	}
36163cf60b7SAlison Schofield 
36263cf60b7SAlison Schofield 	/* Prepare to handle a full payload for variable sized output */
36326f89535SAlison Schofield 	if (out_size == CXL_VARIABLE_PAYLOAD)
36459f8d151SDan Williams 		mbox->size_out = mds->payload_size;
36563cf60b7SAlison Schofield 	else
36663cf60b7SAlison Schofield 		mbox->size_out = out_size;
36763cf60b7SAlison Schofield 
36863cf60b7SAlison Schofield 	if (mbox->size_out) {
36963cf60b7SAlison Schofield 		mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL);
37063cf60b7SAlison Schofield 		if (!mbox->payload_out) {
37163cf60b7SAlison Schofield 			kvfree(mbox->payload_in);
37263cf60b7SAlison Schofield 			return -ENOMEM;
37363cf60b7SAlison Schofield 		}
37463cf60b7SAlison Schofield 	}
37563cf60b7SAlison Schofield 	return 0;
37663cf60b7SAlison Schofield }
37763cf60b7SAlison Schofield 
37863cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox)
37963cf60b7SAlison Schofield {
38063cf60b7SAlison Schofield 	kvfree(mbox->payload_in);
38163cf60b7SAlison Schofield 	kvfree(mbox->payload_out);
38263cf60b7SAlison Schofield }
38363cf60b7SAlison Schofield 
3846dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,
3856dd0e5ccSAlison Schofield 			      const struct cxl_send_command *send_cmd,
38659f8d151SDan Williams 			      struct cxl_memdev_state *mds)
3876dd0e5ccSAlison Schofield {
3886dd0e5ccSAlison Schofield 	if (send_cmd->raw.rsvd)
3896dd0e5ccSAlison Schofield 		return -EINVAL;
3906dd0e5ccSAlison Schofield 
3916dd0e5ccSAlison Schofield 	/*
3926dd0e5ccSAlison Schofield 	 * Unlike supported commands, the output size of RAW commands
3936dd0e5ccSAlison Schofield 	 * gets passed along without further checking, so it must be
3946dd0e5ccSAlison Schofield 	 * validated here.
3956dd0e5ccSAlison Schofield 	 */
39659f8d151SDan Williams 	if (send_cmd->out.size > mds->payload_size)
3976dd0e5ccSAlison Schofield 		return -EINVAL;
3986dd0e5ccSAlison Schofield 
3996dd0e5ccSAlison Schofield 	if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode))
4006dd0e5ccSAlison Schofield 		return -EPERM;
4016dd0e5ccSAlison Schofield 
40259f8d151SDan Williams 	dev_WARN_ONCE(mds->cxlds.dev, true, "raw command path used\n");
40339ed8da4SAlison Schofield 
4046dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
4056dd0e5ccSAlison Schofield 		.info = {
4066dd0e5ccSAlison Schofield 			.id = CXL_MEM_COMMAND_ID_RAW,
4076dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
4086dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
4096dd0e5ccSAlison Schofield 		},
4106dd0e5ccSAlison Schofield 		.opcode = send_cmd->raw.opcode
4116dd0e5ccSAlison Schofield 	};
4126dd0e5ccSAlison Schofield 
4136dd0e5ccSAlison Schofield 	return 0;
4146dd0e5ccSAlison Schofield }
4156dd0e5ccSAlison Schofield 
4166dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,
4176dd0e5ccSAlison Schofield 			  const struct cxl_send_command *send_cmd,
41859f8d151SDan Williams 			  struct cxl_memdev_state *mds)
4196dd0e5ccSAlison Schofield {
4206dd0e5ccSAlison Schofield 	struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id];
4216dd0e5ccSAlison Schofield 	const struct cxl_command_info *info = &c->info;
4226dd0e5ccSAlison Schofield 
4236dd0e5ccSAlison Schofield 	if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK)
4246dd0e5ccSAlison Schofield 		return -EINVAL;
4256dd0e5ccSAlison Schofield 
4266dd0e5ccSAlison Schofield 	if (send_cmd->rsvd)
4276dd0e5ccSAlison Schofield 		return -EINVAL;
4286dd0e5ccSAlison Schofield 
4296dd0e5ccSAlison Schofield 	if (send_cmd->in.rsvd || send_cmd->out.rsvd)
4306dd0e5ccSAlison Schofield 		return -EINVAL;
4316dd0e5ccSAlison Schofield 
4326dd0e5ccSAlison Schofield 	/* Check that the command is enabled for hardware */
43359f8d151SDan Williams 	if (!test_bit(info->id, mds->enabled_cmds))
4346dd0e5ccSAlison Schofield 		return -ENOTTY;
4356dd0e5ccSAlison Schofield 
4366dd0e5ccSAlison Schofield 	/* Check that the command is not claimed for exclusive kernel use */
43759f8d151SDan Williams 	if (test_bit(info->id, mds->exclusive_cmds))
4386dd0e5ccSAlison Schofield 		return -EBUSY;
4396dd0e5ccSAlison Schofield 
4406dd0e5ccSAlison Schofield 	/* Check the input buffer is the expected size */
441e35f5718SVishal Verma 	if ((info->size_in != CXL_VARIABLE_PAYLOAD) &&
442e35f5718SVishal Verma 	    (info->size_in != send_cmd->in.size))
4436dd0e5ccSAlison Schofield 		return -ENOMEM;
4446dd0e5ccSAlison Schofield 
4456dd0e5ccSAlison Schofield 	/* Check the output buffer is at least large enough */
446e35f5718SVishal Verma 	if ((info->size_out != CXL_VARIABLE_PAYLOAD) &&
447e35f5718SVishal Verma 	    (send_cmd->out.size < info->size_out))
4486dd0e5ccSAlison Schofield 		return -ENOMEM;
4496dd0e5ccSAlison Schofield 
4506dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
4516dd0e5ccSAlison Schofield 		.info = {
4526dd0e5ccSAlison Schofield 			.id = info->id,
4536dd0e5ccSAlison Schofield 			.flags = info->flags,
4546dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
4556dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
4566dd0e5ccSAlison Schofield 		},
4576dd0e5ccSAlison Schofield 		.opcode = c->opcode
4586dd0e5ccSAlison Schofield 	};
4596dd0e5ccSAlison Schofield 
4606dd0e5ccSAlison Schofield 	return 0;
4616dd0e5ccSAlison Schofield }
4626dd0e5ccSAlison Schofield 
4634faf31b4SDan Williams /**
4644faf31b4SDan Williams  * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
4659ae016aeSAlison Schofield  * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd.
46659f8d151SDan Williams  * @mds: The driver data for the operation
4674faf31b4SDan Williams  * @send_cmd: &struct cxl_send_command copied in from userspace.
4684faf31b4SDan Williams  *
4694faf31b4SDan Williams  * Return:
4704faf31b4SDan Williams  *  * %0	- @out_cmd is ready to send.
4714faf31b4SDan Williams  *  * %-ENOTTY	- Invalid command specified.
4724faf31b4SDan Williams  *  * %-EINVAL	- Reserved fields or invalid values were used.
4734faf31b4SDan Williams  *  * %-ENOMEM	- Input or output buffer wasn't sized properly.
4744faf31b4SDan Williams  *  * %-EPERM	- Attempted to use a protected command.
47512f3856aSDan Williams  *  * %-EBUSY	- Kernel has claimed exclusive access to this opcode
4764faf31b4SDan Williams  *
4772dd5600aSAlison Schofield  * The result of this command is a fully validated command in @mbox_cmd that is
4784faf31b4SDan Williams  * safe to send to the hardware.
4794faf31b4SDan Williams  */
4809ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,
48159f8d151SDan Williams 				      struct cxl_memdev_state *mds,
4822dd5600aSAlison Schofield 				      const struct cxl_send_command *send_cmd)
4834faf31b4SDan Williams {
4842dd5600aSAlison Schofield 	struct cxl_mem_command mem_cmd;
4859ae016aeSAlison Schofield 	int rc;
4869ae016aeSAlison Schofield 
4874faf31b4SDan Williams 	if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX)
4884faf31b4SDan Williams 		return -ENOTTY;
4894faf31b4SDan Williams 
4904faf31b4SDan Williams 	/*
4914faf31b4SDan Williams 	 * The user can never specify an input payload larger than what hardware
4924faf31b4SDan Williams 	 * supports, but output can be arbitrarily large (simply write out as
4934faf31b4SDan Williams 	 * much data as the hardware provides).
4944faf31b4SDan Williams 	 */
49559f8d151SDan Williams 	if (send_cmd->in.size > mds->payload_size)
4964faf31b4SDan Williams 		return -EINVAL;
4974faf31b4SDan Williams 
4986dd0e5ccSAlison Schofield 	/* Sanitize and construct a cxl_mem_command */
4996dd0e5ccSAlison Schofield 	if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW)
50059f8d151SDan Williams 		rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, mds);
5016dd0e5ccSAlison Schofield 	else
50259f8d151SDan Williams 		rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, mds);
5039ae016aeSAlison Schofield 
5049ae016aeSAlison Schofield 	if (rc)
5059ae016aeSAlison Schofield 		return rc;
5069ae016aeSAlison Schofield 
5079ae016aeSAlison Schofield 	/* Sanitize and construct a cxl_mbox_cmd */
50859f8d151SDan Williams 	return cxl_mbox_cmd_ctor(mbox_cmd, mds, mem_cmd.opcode,
5092dd5600aSAlison Schofield 				 mem_cmd.info.size_in, mem_cmd.info.size_out,
5109ae016aeSAlison Schofield 				 send_cmd->in.payload);
5114faf31b4SDan Williams }
5124faf31b4SDan Williams 
5134faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd,
5144faf31b4SDan Williams 		  struct cxl_mem_query_commands __user *q)
5154faf31b4SDan Williams {
51659f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
5174faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
5184faf31b4SDan Williams 	struct cxl_mem_command *cmd;
5194faf31b4SDan Williams 	u32 n_commands;
5204faf31b4SDan Williams 	int j = 0;
5214faf31b4SDan Williams 
5224faf31b4SDan Williams 	dev_dbg(dev, "Query IOCTL\n");
5234faf31b4SDan Williams 
5244faf31b4SDan Williams 	if (get_user(n_commands, &q->n_commands))
5254faf31b4SDan Williams 		return -EFAULT;
5264faf31b4SDan Williams 
5274faf31b4SDan Williams 	/* returns the total number if 0 elements are requested. */
5284faf31b4SDan Williams 	if (n_commands == 0)
529ff56ab9eSDan Williams 		return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands);
5304faf31b4SDan Williams 
5314faf31b4SDan Williams 	/*
5324faf31b4SDan Williams 	 * otherwise, return max(n_commands, total commands) cxl_command_info
5334faf31b4SDan Williams 	 * structures.
5344faf31b4SDan Williams 	 */
5354faf31b4SDan Williams 	cxl_for_each_cmd(cmd) {
536814a15f3SIra Weiny 		struct cxl_command_info info = cmd->info;
5374faf31b4SDan Williams 
53859f8d151SDan Williams 		if (test_bit(info.id, mds->enabled_cmds))
539814a15f3SIra Weiny 			info.flags |= CXL_MEM_COMMAND_FLAG_ENABLED;
54059f8d151SDan Williams 		if (test_bit(info.id, mds->exclusive_cmds))
541814a15f3SIra Weiny 			info.flags |= CXL_MEM_COMMAND_FLAG_EXCLUSIVE;
542814a15f3SIra Weiny 
543814a15f3SIra Weiny 		if (copy_to_user(&q->commands[j++], &info, sizeof(info)))
5444faf31b4SDan Williams 			return -EFAULT;
5454faf31b4SDan Williams 
5464faf31b4SDan Williams 		if (j == n_commands)
5474faf31b4SDan Williams 			break;
5484faf31b4SDan Williams 	}
5494faf31b4SDan Williams 
5504faf31b4SDan Williams 	return 0;
5514faf31b4SDan Williams }
5524faf31b4SDan Williams 
5534faf31b4SDan Williams /**
5544faf31b4SDan Williams  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace.
55559f8d151SDan Williams  * @mds: The driver data for the operation
556d97fe8eeSAlison Schofield  * @mbox_cmd: The validated mailbox command.
5574faf31b4SDan Williams  * @out_payload: Pointer to userspace's output payload.
5584faf31b4SDan Williams  * @size_out: (Input) Max payload size to copy out.
5594faf31b4SDan Williams  *            (Output) Payload size hardware generated.
5604faf31b4SDan Williams  * @retval: Hardware generated return code from the operation.
5614faf31b4SDan Williams  *
5624faf31b4SDan Williams  * Return:
5634faf31b4SDan Williams  *  * %0	- Mailbox transaction succeeded. This implies the mailbox
5644faf31b4SDan Williams  *		  protocol completed successfully not that the operation itself
5654faf31b4SDan Williams  *		  was successful.
5664faf31b4SDan Williams  *  * %-ENOMEM  - Couldn't allocate a bounce buffer.
5674faf31b4SDan Williams  *  * %-EFAULT	- Something happened with copy_to/from_user.
5684faf31b4SDan Williams  *  * %-EINTR	- Mailbox acquisition interrupted.
5694faf31b4SDan Williams  *  * %-EXXX	- Transaction level failures.
5704faf31b4SDan Williams  *
571d97fe8eeSAlison Schofield  * Dispatches a mailbox command on behalf of a userspace request.
572d97fe8eeSAlison Schofield  * The output payload is copied to userspace.
5734faf31b4SDan Williams  *
5744faf31b4SDan Williams  * See cxl_send_cmd().
5754faf31b4SDan Williams  */
57659f8d151SDan Williams static int handle_mailbox_cmd_from_user(struct cxl_memdev_state *mds,
577d97fe8eeSAlison Schofield 					struct cxl_mbox_cmd *mbox_cmd,
578d97fe8eeSAlison Schofield 					u64 out_payload, s32 *size_out,
579d97fe8eeSAlison Schofield 					u32 *retval)
5804faf31b4SDan Williams {
58159f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
5824faf31b4SDan Williams 	int rc;
5834faf31b4SDan Williams 
5844faf31b4SDan Williams 	dev_dbg(dev,
5854faf31b4SDan Williams 		"Submitting %s command for user\n"
5864faf31b4SDan Williams 		"\topcode: %x\n"
58782b8ba29SAlison Schofield 		"\tsize: %zx\n",
588d97fe8eeSAlison Schofield 		cxl_mem_opcode_to_name(mbox_cmd->opcode),
589d97fe8eeSAlison Schofield 		mbox_cmd->opcode, mbox_cmd->size_in);
5904faf31b4SDan Williams 
59159f8d151SDan Williams 	rc = mds->mbox_send(mds, mbox_cmd);
5924faf31b4SDan Williams 	if (rc)
5934faf31b4SDan Williams 		goto out;
5944faf31b4SDan Williams 
5954faf31b4SDan Williams 	/*
5964faf31b4SDan Williams 	 * @size_out contains the max size that's allowed to be written back out
5974faf31b4SDan Williams 	 * to userspace. While the payload may have written more output than
5984faf31b4SDan Williams 	 * this it will have to be ignored.
5994faf31b4SDan Williams 	 */
600d97fe8eeSAlison Schofield 	if (mbox_cmd->size_out) {
601d97fe8eeSAlison Schofield 		dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out,
6024faf31b4SDan Williams 			      "Invalid return size\n");
6034faf31b4SDan Williams 		if (copy_to_user(u64_to_user_ptr(out_payload),
604d97fe8eeSAlison Schofield 				 mbox_cmd->payload_out, mbox_cmd->size_out)) {
6054faf31b4SDan Williams 			rc = -EFAULT;
6064faf31b4SDan Williams 			goto out;
6074faf31b4SDan Williams 		}
6084faf31b4SDan Williams 	}
6094faf31b4SDan Williams 
610d97fe8eeSAlison Schofield 	*size_out = mbox_cmd->size_out;
611d97fe8eeSAlison Schofield 	*retval = mbox_cmd->return_code;
6124faf31b4SDan Williams 
6134faf31b4SDan Williams out:
614d97fe8eeSAlison Schofield 	cxl_mbox_cmd_dtor(mbox_cmd);
6154faf31b4SDan Williams 	return rc;
6164faf31b4SDan Williams }
6174faf31b4SDan Williams 
6184faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)
6194faf31b4SDan Williams {
62059f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
6214faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
6224faf31b4SDan Williams 	struct cxl_send_command send;
6239ae016aeSAlison Schofield 	struct cxl_mbox_cmd mbox_cmd;
6244faf31b4SDan Williams 	int rc;
6254faf31b4SDan Williams 
6264faf31b4SDan Williams 	dev_dbg(dev, "Send IOCTL\n");
6274faf31b4SDan Williams 
6284faf31b4SDan Williams 	if (copy_from_user(&send, s, sizeof(send)))
6294faf31b4SDan Williams 		return -EFAULT;
6304faf31b4SDan Williams 
63159f8d151SDan Williams 	rc = cxl_validate_cmd_from_user(&mbox_cmd, mds, &send);
6324faf31b4SDan Williams 	if (rc)
6334faf31b4SDan Williams 		return rc;
6344faf31b4SDan Williams 
63559f8d151SDan Williams 	rc = handle_mailbox_cmd_from_user(mds, &mbox_cmd, send.out.payload,
636d97fe8eeSAlison Schofield 					  &send.out.size, &send.retval);
6374faf31b4SDan Williams 	if (rc)
6384faf31b4SDan Williams 		return rc;
6394faf31b4SDan Williams 
6404faf31b4SDan Williams 	if (copy_to_user(s, &send, sizeof(send)))
6414faf31b4SDan Williams 		return -EFAULT;
6424faf31b4SDan Williams 
6434faf31b4SDan Williams 	return 0;
6444faf31b4SDan Williams }
6454faf31b4SDan Williams 
64659f8d151SDan Williams static int cxl_xfer_log(struct cxl_memdev_state *mds, uuid_t *uuid,
64759f8d151SDan Williams 			u32 *size, u8 *out)
6484faf31b4SDan Williams {
649623c0751SRobert Richter 	u32 remaining = *size;
6504faf31b4SDan Williams 	u32 offset = 0;
6514faf31b4SDan Williams 
6524faf31b4SDan Williams 	while (remaining) {
65359f8d151SDan Williams 		u32 xfer_size = min_t(u32, remaining, mds->payload_size);
6545331cdf4SDan Williams 		struct cxl_mbox_cmd mbox_cmd;
6555331cdf4SDan Williams 		struct cxl_mbox_get_log log;
6564faf31b4SDan Williams 		int rc;
6574faf31b4SDan Williams 
6585331cdf4SDan Williams 		log = (struct cxl_mbox_get_log) {
6595331cdf4SDan Williams 			.uuid = *uuid,
6605331cdf4SDan Williams 			.offset = cpu_to_le32(offset),
6615331cdf4SDan Williams 			.length = cpu_to_le32(xfer_size),
6625331cdf4SDan Williams 		};
6635331cdf4SDan Williams 
6645331cdf4SDan Williams 		mbox_cmd = (struct cxl_mbox_cmd) {
6655331cdf4SDan Williams 			.opcode = CXL_MBOX_OP_GET_LOG,
6665331cdf4SDan Williams 			.size_in = sizeof(log),
6675331cdf4SDan Williams 			.payload_in = &log,
6685331cdf4SDan Williams 			.size_out = xfer_size,
6695331cdf4SDan Williams 			.payload_out = out,
6705331cdf4SDan Williams 		};
6715331cdf4SDan Williams 
67259f8d151SDan Williams 		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
673623c0751SRobert Richter 
674623c0751SRobert Richter 		/*
675623c0751SRobert Richter 		 * The output payload length that indicates the number
676623c0751SRobert Richter 		 * of valid bytes can be smaller than the Log buffer
677623c0751SRobert Richter 		 * size.
678623c0751SRobert Richter 		 */
679623c0751SRobert Richter 		if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
680623c0751SRobert Richter 			offset += mbox_cmd.size_out;
681623c0751SRobert Richter 			break;
682623c0751SRobert Richter 		}
683623c0751SRobert Richter 
6844faf31b4SDan Williams 		if (rc < 0)
6854faf31b4SDan Williams 			return rc;
6864faf31b4SDan Williams 
6874faf31b4SDan Williams 		out += xfer_size;
6884faf31b4SDan Williams 		remaining -= xfer_size;
6894faf31b4SDan Williams 		offset += xfer_size;
6904faf31b4SDan Williams 	}
6914faf31b4SDan Williams 
692623c0751SRobert Richter 	*size = offset;
693623c0751SRobert Richter 
6944faf31b4SDan Williams 	return 0;
6954faf31b4SDan Williams }
6964faf31b4SDan Williams 
6974faf31b4SDan Williams /**
6984faf31b4SDan Williams  * cxl_walk_cel() - Walk through the Command Effects Log.
69959f8d151SDan Williams  * @mds: The driver data for the operation
7004faf31b4SDan Williams  * @size: Length of the Command Effects Log.
7014faf31b4SDan Williams  * @cel: CEL
7024faf31b4SDan Williams  *
7034faf31b4SDan Williams  * Iterate over each entry in the CEL and determine if the driver supports the
7044faf31b4SDan Williams  * command. If so, the command is enabled for the device and can be used later.
7054faf31b4SDan Williams  */
70659f8d151SDan Williams static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
7074faf31b4SDan Williams {
70849be6dd8SDan Williams 	struct cxl_cel_entry *cel_entry;
7094faf31b4SDan Williams 	const int cel_entries = size / sizeof(*cel_entry);
71059f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
7114faf31b4SDan Williams 	int i;
7124faf31b4SDan Williams 
71349be6dd8SDan Williams 	cel_entry = (struct cxl_cel_entry *) cel;
7144faf31b4SDan Williams 
7154faf31b4SDan Williams 	for (i = 0; i < cel_entries; i++) {
7164faf31b4SDan Williams 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
7174faf31b4SDan Williams 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
718d2f70605SIra Weiny 		int enabled = 0;
7194faf31b4SDan Williams 
720d2f70605SIra Weiny 		if (cmd) {
721d2f70605SIra Weiny 			set_bit(cmd->info.id, mds->enabled_cmds);
722d2f70605SIra Weiny 			enabled++;
7234faf31b4SDan Williams 		}
7244faf31b4SDan Williams 
725d2f70605SIra Weiny 		if (cxl_is_poison_command(opcode)) {
72659f8d151SDan Williams 			cxl_set_poison_cmd_enabled(&mds->poison, opcode);
727d2f70605SIra Weiny 			enabled++;
728d2f70605SIra Weiny 		}
729d0abf578SAlison Schofield 
730d2f70605SIra Weiny 		if (cxl_is_security_command(opcode)) {
731ad64f595SDavidlohr Bueso 			cxl_set_security_cmd_enabled(&mds->security, opcode);
732d2f70605SIra Weiny 			enabled++;
733d2f70605SIra Weiny 		}
734ad64f595SDavidlohr Bueso 
735d2f70605SIra Weiny 		dev_dbg(dev, "Opcode 0x%04x %s\n", opcode,
736d2f70605SIra Weiny 			enabled ? "enabled" : "unsupported by driver");
7374faf31b4SDan Williams 	}
7384faf31b4SDan Williams }
7394faf31b4SDan Williams 
74059f8d151SDan Williams static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_memdev_state *mds)
7414faf31b4SDan Williams {
7424faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *ret;
7435331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
7444faf31b4SDan Williams 	int rc;
7454faf31b4SDan Williams 
74659f8d151SDan Williams 	ret = kvmalloc(mds->payload_size, GFP_KERNEL);
7474faf31b4SDan Williams 	if (!ret)
7484faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
7494faf31b4SDan Williams 
7505331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
7515331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS,
75259f8d151SDan Williams 		.size_out = mds->payload_size,
7535331cdf4SDan Williams 		.payload_out = ret,
7542aeaf663SDan Williams 		/* At least the record number field must be valid */
7552aeaf663SDan Williams 		.min_out = 2,
7565331cdf4SDan Williams 	};
75759f8d151SDan Williams 	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
7584faf31b4SDan Williams 	if (rc < 0) {
7594faf31b4SDan Williams 		kvfree(ret);
7604faf31b4SDan Williams 		return ERR_PTR(rc);
7614faf31b4SDan Williams 	}
7624faf31b4SDan Williams 
7635331cdf4SDan Williams 
7644faf31b4SDan Williams 	return ret;
7654faf31b4SDan Williams }
7664faf31b4SDan Williams 
7674faf31b4SDan Williams enum {
7684faf31b4SDan Williams 	CEL_UUID,
7694faf31b4SDan Williams 	VENDOR_DEBUG_UUID,
7704faf31b4SDan Williams };
7714faf31b4SDan Williams 
7724faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */
7734faf31b4SDan Williams static const uuid_t log_uuid[] = {
77449be6dd8SDan Williams 	[CEL_UUID] = DEFINE_CXL_CEL_UUID,
77549be6dd8SDan Williams 	[VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID,
7764faf31b4SDan Williams };
7774faf31b4SDan Williams 
7784faf31b4SDan Williams /**
7795e2411aeSIra Weiny  * cxl_enumerate_cmds() - Enumerate commands for a device.
78059f8d151SDan Williams  * @mds: The driver data for the operation
7814faf31b4SDan Williams  *
7824faf31b4SDan Williams  * Returns 0 if enumerate completed successfully.
7834faf31b4SDan Williams  *
7844faf31b4SDan Williams  * CXL devices have optional support for certain commands. This function will
7854faf31b4SDan Williams  * determine the set of supported commands for the hardware and update the
78659f8d151SDan Williams  * enabled_cmds bitmap in the @mds.
7874faf31b4SDan Williams  */
78859f8d151SDan Williams int cxl_enumerate_cmds(struct cxl_memdev_state *mds)
7894faf31b4SDan Williams {
7904faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *gsl;
79159f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
7924faf31b4SDan Williams 	struct cxl_mem_command *cmd;
7934faf31b4SDan Williams 	int i, rc;
7944faf31b4SDan Williams 
79559f8d151SDan Williams 	gsl = cxl_get_gsl(mds);
7964faf31b4SDan Williams 	if (IS_ERR(gsl))
7974faf31b4SDan Williams 		return PTR_ERR(gsl);
7984faf31b4SDan Williams 
7994faf31b4SDan Williams 	rc = -ENOENT;
8004faf31b4SDan Williams 	for (i = 0; i < le16_to_cpu(gsl->entries); i++) {
8014faf31b4SDan Williams 		u32 size = le32_to_cpu(gsl->entry[i].size);
8024faf31b4SDan Williams 		uuid_t uuid = gsl->entry[i].uuid;
8034faf31b4SDan Williams 		u8 *log;
8044faf31b4SDan Williams 
8054faf31b4SDan Williams 		dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size);
8064faf31b4SDan Williams 
8074faf31b4SDan Williams 		if (!uuid_equal(&uuid, &log_uuid[CEL_UUID]))
8084faf31b4SDan Williams 			continue;
8094faf31b4SDan Williams 
8104faf31b4SDan Williams 		log = kvmalloc(size, GFP_KERNEL);
8114faf31b4SDan Williams 		if (!log) {
8124faf31b4SDan Williams 			rc = -ENOMEM;
8134faf31b4SDan Williams 			goto out;
8144faf31b4SDan Williams 		}
8154faf31b4SDan Williams 
81659f8d151SDan Williams 		rc = cxl_xfer_log(mds, &uuid, &size, log);
8174faf31b4SDan Williams 		if (rc) {
8184faf31b4SDan Williams 			kvfree(log);
8194faf31b4SDan Williams 			goto out;
8204faf31b4SDan Williams 		}
8214faf31b4SDan Williams 
82259f8d151SDan Williams 		cxl_walk_cel(mds, size, log);
8234faf31b4SDan Williams 		kvfree(log);
8244faf31b4SDan Williams 
8254faf31b4SDan Williams 		/* In case CEL was bogus, enable some default commands. */
8264faf31b4SDan Williams 		cxl_for_each_cmd(cmd)
8274faf31b4SDan Williams 			if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE)
82859f8d151SDan Williams 				set_bit(cmd->info.id, mds->enabled_cmds);
8294faf31b4SDan Williams 
8304faf31b4SDan Williams 		/* Found the required CEL */
8314faf31b4SDan Williams 		rc = 0;
8324faf31b4SDan Williams 	}
8334faf31b4SDan Williams out:
8344faf31b4SDan Williams 	kvfree(gsl);
8354faf31b4SDan Williams 	return rc;
8364faf31b4SDan Williams }
837affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL);
8384faf31b4SDan Williams 
8390c8393dcSIra Weiny static void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
840d54a531aSIra Weiny 				   enum cxl_event_log_type type,
841d54a531aSIra Weiny 				   struct cxl_event_record_raw *record)
842d54a531aSIra Weiny {
843*f9c68338SIra Weiny 	union cxl_event *evt = &record->event;
8446eade110SIra Weiny 	uuid_t *id = &record->id;
845d54a531aSIra Weiny 
846*f9c68338SIra Weiny 	if (uuid_equal(id, &CXL_EVENT_GEN_MEDIA_UUID))
847*f9c68338SIra Weiny 		trace_cxl_general_media(cxlmd, type, &evt->gen_media);
848*f9c68338SIra Weiny 	else if (uuid_equal(id, &CXL_EVENT_DRAM_UUID))
849*f9c68338SIra Weiny 		trace_cxl_dram(cxlmd, type, &evt->dram);
850*f9c68338SIra Weiny 	else if (uuid_equal(id, &CXL_EVENT_MEM_MODULE_UUID))
851*f9c68338SIra Weiny 		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
852*f9c68338SIra Weiny 	else
853*f9c68338SIra Weiny 		trace_cxl_generic_event(cxlmd, type, id, &evt->generic);
854d54a531aSIra Weiny }
855d54a531aSIra Weiny 
85659f8d151SDan Williams static int cxl_clear_event_record(struct cxl_memdev_state *mds,
8576ebe28f9SIra Weiny 				  enum cxl_event_log_type log,
8586ebe28f9SIra Weiny 				  struct cxl_get_event_payload *get_pl)
8596ebe28f9SIra Weiny {
8606ebe28f9SIra Weiny 	struct cxl_mbox_clear_event_payload *payload;
8616ebe28f9SIra Weiny 	u16 total = le16_to_cpu(get_pl->record_count);
8626ebe28f9SIra Weiny 	u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES;
8636ebe28f9SIra Weiny 	size_t pl_size = struct_size(payload, handles, max_handles);
8646ebe28f9SIra Weiny 	struct cxl_mbox_cmd mbox_cmd;
8656ebe28f9SIra Weiny 	u16 cnt;
8666ebe28f9SIra Weiny 	int rc = 0;
8676ebe28f9SIra Weiny 	int i;
8686ebe28f9SIra Weiny 
8696ebe28f9SIra Weiny 	/* Payload size may limit the max handles */
87059f8d151SDan Williams 	if (pl_size > mds->payload_size) {
87159f8d151SDan Williams 		max_handles = (mds->payload_size - sizeof(*payload)) /
8726ebe28f9SIra Weiny 			      sizeof(__le16);
8736ebe28f9SIra Weiny 		pl_size = struct_size(payload, handles, max_handles);
8746ebe28f9SIra Weiny 	}
8756ebe28f9SIra Weiny 
8766ebe28f9SIra Weiny 	payload = kvzalloc(pl_size, GFP_KERNEL);
8776ebe28f9SIra Weiny 	if (!payload)
8786ebe28f9SIra Weiny 		return -ENOMEM;
8796ebe28f9SIra Weiny 
8806ebe28f9SIra Weiny 	*payload = (struct cxl_mbox_clear_event_payload) {
8816ebe28f9SIra Weiny 		.event_log = log,
8826ebe28f9SIra Weiny 	};
8836ebe28f9SIra Weiny 
8846ebe28f9SIra Weiny 	mbox_cmd = (struct cxl_mbox_cmd) {
8856ebe28f9SIra Weiny 		.opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD,
8866ebe28f9SIra Weiny 		.payload_in = payload,
8876ebe28f9SIra Weiny 		.size_in = pl_size,
8886ebe28f9SIra Weiny 	};
8896ebe28f9SIra Weiny 
8906ebe28f9SIra Weiny 	/*
8916ebe28f9SIra Weiny 	 * Clear Event Records uses u8 for the handle cnt while Get Event
8926ebe28f9SIra Weiny 	 * Record can return up to 0xffff records.
8936ebe28f9SIra Weiny 	 */
8946ebe28f9SIra Weiny 	i = 0;
8956ebe28f9SIra Weiny 	for (cnt = 0; cnt < total; cnt++) {
896*f9c68338SIra Weiny 		struct cxl_event_record_raw *raw = &get_pl->records[cnt];
897*f9c68338SIra Weiny 		struct cxl_event_generic *gen = &raw->event.generic;
898*f9c68338SIra Weiny 
899*f9c68338SIra Weiny 		payload->handles[i++] = gen->hdr.handle;
90059f8d151SDan Williams 		dev_dbg(mds->cxlds.dev, "Event log '%d': Clearing %u\n", log,
90159f8d151SDan Williams 			le16_to_cpu(payload->handles[i]));
9026ebe28f9SIra Weiny 
9036ebe28f9SIra Weiny 		if (i == max_handles) {
9046ebe28f9SIra Weiny 			payload->nr_recs = i;
90559f8d151SDan Williams 			rc = cxl_internal_send_cmd(mds, &mbox_cmd);
9066ebe28f9SIra Weiny 			if (rc)
9076ebe28f9SIra Weiny 				goto free_pl;
9086ebe28f9SIra Weiny 			i = 0;
9096ebe28f9SIra Weiny 		}
9106ebe28f9SIra Weiny 	}
9116ebe28f9SIra Weiny 
9126ebe28f9SIra Weiny 	/* Clear what is left if any */
9136ebe28f9SIra Weiny 	if (i) {
9146ebe28f9SIra Weiny 		payload->nr_recs = i;
9156ebe28f9SIra Weiny 		mbox_cmd.size_in = struct_size(payload, handles, i);
91659f8d151SDan Williams 		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
9176ebe28f9SIra Weiny 		if (rc)
9186ebe28f9SIra Weiny 			goto free_pl;
9196ebe28f9SIra Weiny 	}
9206ebe28f9SIra Weiny 
9216ebe28f9SIra Weiny free_pl:
9226ebe28f9SIra Weiny 	kvfree(payload);
9236ebe28f9SIra Weiny 	return rc;
9246ebe28f9SIra Weiny }
9256ebe28f9SIra Weiny 
92659f8d151SDan Williams static void cxl_mem_get_records_log(struct cxl_memdev_state *mds,
9276ebe28f9SIra Weiny 				    enum cxl_event_log_type type)
9286ebe28f9SIra Weiny {
92959f8d151SDan Williams 	struct cxl_memdev *cxlmd = mds->cxlds.cxlmd;
93059f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
9316ebe28f9SIra Weiny 	struct cxl_get_event_payload *payload;
9326ebe28f9SIra Weiny 	struct cxl_mbox_cmd mbox_cmd;
9336ebe28f9SIra Weiny 	u8 log_type = type;
9346ebe28f9SIra Weiny 	u16 nr_rec;
9356ebe28f9SIra Weiny 
93659f8d151SDan Williams 	mutex_lock(&mds->event.log_lock);
93759f8d151SDan Williams 	payload = mds->event.buf;
9386ebe28f9SIra Weiny 
9396ebe28f9SIra Weiny 	mbox_cmd = (struct cxl_mbox_cmd) {
9406ebe28f9SIra Weiny 		.opcode = CXL_MBOX_OP_GET_EVENT_RECORD,
9416ebe28f9SIra Weiny 		.payload_in = &log_type,
9426ebe28f9SIra Weiny 		.size_in = sizeof(log_type),
9436ebe28f9SIra Weiny 		.payload_out = payload,
94459f8d151SDan Williams 		.size_out = mds->payload_size,
9456ebe28f9SIra Weiny 		.min_out = struct_size(payload, records, 0),
9466ebe28f9SIra Weiny 	};
9476ebe28f9SIra Weiny 
9486ebe28f9SIra Weiny 	do {
9496ebe28f9SIra Weiny 		int rc, i;
9506ebe28f9SIra Weiny 
95159f8d151SDan Williams 		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
9526ebe28f9SIra Weiny 		if (rc) {
95359f8d151SDan Williams 			dev_err_ratelimited(dev,
9546ebe28f9SIra Weiny 				"Event log '%d': Failed to query event records : %d",
9556ebe28f9SIra Weiny 				type, rc);
9566ebe28f9SIra Weiny 			break;
9576ebe28f9SIra Weiny 		}
9586ebe28f9SIra Weiny 
9596ebe28f9SIra Weiny 		nr_rec = le16_to_cpu(payload->record_count);
9606ebe28f9SIra Weiny 		if (!nr_rec)
9616ebe28f9SIra Weiny 			break;
9626ebe28f9SIra Weiny 
9636ebe28f9SIra Weiny 		for (i = 0; i < nr_rec; i++)
96459f8d151SDan Williams 			cxl_event_trace_record(cxlmd, type,
9656ebe28f9SIra Weiny 					       &payload->records[i]);
9666ebe28f9SIra Weiny 
9676ebe28f9SIra Weiny 		if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW)
96859f8d151SDan Williams 			trace_cxl_overflow(cxlmd, type, payload);
9696ebe28f9SIra Weiny 
97059f8d151SDan Williams 		rc = cxl_clear_event_record(mds, type, payload);
9716ebe28f9SIra Weiny 		if (rc) {
97259f8d151SDan Williams 			dev_err_ratelimited(dev,
9736ebe28f9SIra Weiny 				"Event log '%d': Failed to clear events : %d",
9746ebe28f9SIra Weiny 				type, rc);
9756ebe28f9SIra Weiny 			break;
9766ebe28f9SIra Weiny 		}
9776ebe28f9SIra Weiny 	} while (nr_rec);
9786ebe28f9SIra Weiny 
97959f8d151SDan Williams 	mutex_unlock(&mds->event.log_lock);
9806ebe28f9SIra Weiny }
9816ebe28f9SIra Weiny 
9826ebe28f9SIra Weiny /**
9836ebe28f9SIra Weiny  * cxl_mem_get_event_records - Get Event Records from the device
98459f8d151SDan Williams  * @mds: The driver data for the operation
9857ebf38c9SJonathan Cameron  * @status: Event Status register value identifying which events are available.
9866ebe28f9SIra Weiny  *
9876ebe28f9SIra Weiny  * Retrieve all event records available on the device, report them as trace
9886ebe28f9SIra Weiny  * events, and clear them.
9896ebe28f9SIra Weiny  *
9906ebe28f9SIra Weiny  * See CXL rev 3.0 @8.2.9.2.2 Get Event Records
9916ebe28f9SIra Weiny  * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records
9926ebe28f9SIra Weiny  */
99359f8d151SDan Williams void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status)
9946ebe28f9SIra Weiny {
99559f8d151SDan Williams 	dev_dbg(mds->cxlds.dev, "Reading event logs: %x\n", status);
9966ebe28f9SIra Weiny 
9976ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_FATAL)
99859f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FATAL);
9996ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_FAIL)
100059f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FAIL);
10016ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_WARN)
100259f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_WARN);
10036ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_INFO)
100459f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_INFO);
10056ebe28f9SIra Weiny }
10066ebe28f9SIra Weiny EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, CXL);
10076ebe28f9SIra Weiny 
10084faf31b4SDan Williams /**
10094faf31b4SDan Williams  * cxl_mem_get_partition_info - Get partition info
101059f8d151SDan Williams  * @mds: The driver data for the operation
10114faf31b4SDan Williams  *
10124faf31b4SDan Williams  * Retrieve the current partition info for the device specified.  The active
10134faf31b4SDan Williams  * values are the current capacity in bytes.  If not 0, the 'next' values are
10144faf31b4SDan Williams  * the pending values, in bytes, which take affect on next cold reset.
10154faf31b4SDan Williams  *
10164faf31b4SDan Williams  * Return: 0 if no error: or the result of the mailbox command.
10174faf31b4SDan Williams  *
10184faf31b4SDan Williams  * See CXL @8.2.9.5.2.1 Get Partition Info
10194faf31b4SDan Williams  */
102059f8d151SDan Williams static int cxl_mem_get_partition_info(struct cxl_memdev_state *mds)
10214faf31b4SDan Williams {
1022e7ad1bf6SDan Williams 	struct cxl_mbox_get_partition_info pi;
10235331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
10244faf31b4SDan Williams 	int rc;
10254faf31b4SDan Williams 
10265331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
10275331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_GET_PARTITION_INFO,
10285331cdf4SDan Williams 		.size_out = sizeof(pi),
10295331cdf4SDan Williams 		.payload_out = &pi,
10305331cdf4SDan Williams 	};
103159f8d151SDan Williams 	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
10324faf31b4SDan Williams 	if (rc)
10334faf31b4SDan Williams 		return rc;
10344faf31b4SDan Williams 
103559f8d151SDan Williams 	mds->active_volatile_bytes =
10364faf31b4SDan Williams 		le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
103759f8d151SDan Williams 	mds->active_persistent_bytes =
10384faf31b4SDan Williams 		le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER;
103959f8d151SDan Williams 	mds->next_volatile_bytes =
10404faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
104159f8d151SDan Williams 	mds->next_persistent_bytes =
10424faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
10434faf31b4SDan Williams 
10444faf31b4SDan Williams 	return 0;
10454faf31b4SDan Williams }
10464faf31b4SDan Williams 
10474faf31b4SDan Williams /**
10485e2411aeSIra Weiny  * cxl_dev_state_identify() - Send the IDENTIFY command to the device.
104959f8d151SDan Williams  * @mds: The driver data for the operation
10504faf31b4SDan Williams  *
1051e764f122SDave Jiang  * Return: 0 if identify was executed successfully or media not ready.
10524faf31b4SDan Williams  *
10534faf31b4SDan Williams  * This will dispatch the identify command to the device and on success populate
10544faf31b4SDan Williams  * structures to be exported to sysfs.
10554faf31b4SDan Williams  */
105659f8d151SDan Williams int cxl_dev_state_identify(struct cxl_memdev_state *mds)
10574faf31b4SDan Williams {
10584faf31b4SDan Williams 	/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
105949be6dd8SDan Williams 	struct cxl_mbox_identify id;
10605331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
1061ed83f7caSAlison Schofield 	u32 val;
10624faf31b4SDan Williams 	int rc;
10634faf31b4SDan Williams 
106459f8d151SDan Williams 	if (!mds->cxlds.media_ready)
1065e764f122SDave Jiang 		return 0;
1066e764f122SDave Jiang 
10675331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
10685331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_IDENTIFY,
10695331cdf4SDan Williams 		.size_out = sizeof(id),
10705331cdf4SDan Williams 		.payload_out = &id,
10715331cdf4SDan Williams 	};
107259f8d151SDan Williams 	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
10734faf31b4SDan Williams 	if (rc < 0)
10744faf31b4SDan Williams 		return rc;
10754faf31b4SDan Williams 
107659f8d151SDan Williams 	mds->total_bytes =
10774faf31b4SDan Williams 		le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER;
107859f8d151SDan Williams 	mds->volatile_only_bytes =
10794faf31b4SDan Williams 		le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER;
108059f8d151SDan Williams 	mds->persistent_only_bytes =
10814faf31b4SDan Williams 		le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER;
108259f8d151SDan Williams 	mds->partition_align_bytes =
10834faf31b4SDan Williams 		le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER;
10844faf31b4SDan Williams 
108559f8d151SDan Williams 	mds->lsa_size = le32_to_cpu(id.lsa_size);
108659f8d151SDan Williams 	memcpy(mds->firmware_version, id.fw_revision,
108759f8d151SDan Williams 	       sizeof(id.fw_revision));
10884faf31b4SDan Williams 
108959f8d151SDan Williams 	if (test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) {
1090ed83f7caSAlison Schofield 		val = get_unaligned_le24(id.poison_list_max_mer);
109159f8d151SDan Williams 		mds->poison.max_errors = min_t(u32, val, CXL_POISON_LIST_MAX);
1092ed83f7caSAlison Schofield 	}
1093ed83f7caSAlison Schofield 
10944faf31b4SDan Williams 	return 0;
10954faf31b4SDan Williams }
1096affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL);
10974faf31b4SDan Williams 
109833981838SDan Williams static int __cxl_mem_sanitize(struct cxl_memdev_state *mds, u16 cmd)
109948dcdbb1SDavidlohr Bueso {
110048dcdbb1SDavidlohr Bueso 	int rc;
110148dcdbb1SDavidlohr Bueso 	u32 sec_out = 0;
110248dcdbb1SDavidlohr Bueso 	struct cxl_get_security_output {
110348dcdbb1SDavidlohr Bueso 		__le32 flags;
110448dcdbb1SDavidlohr Bueso 	} out;
110548dcdbb1SDavidlohr Bueso 	struct cxl_mbox_cmd sec_cmd = {
110648dcdbb1SDavidlohr Bueso 		.opcode = CXL_MBOX_OP_GET_SECURITY_STATE,
110748dcdbb1SDavidlohr Bueso 		.payload_out = &out,
110848dcdbb1SDavidlohr Bueso 		.size_out = sizeof(out),
110948dcdbb1SDavidlohr Bueso 	};
111048dcdbb1SDavidlohr Bueso 	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
1111aeaefabcSDan Williams 	struct cxl_dev_state *cxlds = &mds->cxlds;
111248dcdbb1SDavidlohr Bueso 
1113180ffd33SDavidlohr Bueso 	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
111448dcdbb1SDavidlohr Bueso 		return -EINVAL;
111548dcdbb1SDavidlohr Bueso 
1116aeaefabcSDan Williams 	rc = cxl_internal_send_cmd(mds, &sec_cmd);
111748dcdbb1SDavidlohr Bueso 	if (rc < 0) {
111848dcdbb1SDavidlohr Bueso 		dev_err(cxlds->dev, "Failed to get security state : %d", rc);
111948dcdbb1SDavidlohr Bueso 		return rc;
112048dcdbb1SDavidlohr Bueso 	}
112148dcdbb1SDavidlohr Bueso 
112248dcdbb1SDavidlohr Bueso 	/*
112348dcdbb1SDavidlohr Bueso 	 * Prior to using these commands, any security applied to
112448dcdbb1SDavidlohr Bueso 	 * the user data areas of the device shall be DISABLED (or
112548dcdbb1SDavidlohr Bueso 	 * UNLOCKED for secure erase case).
112648dcdbb1SDavidlohr Bueso 	 */
112748dcdbb1SDavidlohr Bueso 	sec_out = le32_to_cpu(out.flags);
112848dcdbb1SDavidlohr Bueso 	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
112948dcdbb1SDavidlohr Bueso 		return -EINVAL;
113048dcdbb1SDavidlohr Bueso 
1131180ffd33SDavidlohr Bueso 	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
1132180ffd33SDavidlohr Bueso 	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
1133180ffd33SDavidlohr Bueso 		return -EINVAL;
1134180ffd33SDavidlohr Bueso 
1135aeaefabcSDan Williams 	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
113648dcdbb1SDavidlohr Bueso 	if (rc < 0) {
113748dcdbb1SDavidlohr Bueso 		dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
113848dcdbb1SDavidlohr Bueso 		return rc;
113948dcdbb1SDavidlohr Bueso 	}
114048dcdbb1SDavidlohr Bueso 
114148dcdbb1SDavidlohr Bueso 	return 0;
114248dcdbb1SDavidlohr Bueso }
114333981838SDan Williams 
114433981838SDan Williams 
114533981838SDan Williams /**
114633981838SDan Williams  * cxl_mem_sanitize() - Send a sanitization command to the device.
114733981838SDan Williams  * @cxlmd: The device for the operation
114833981838SDan Williams  * @cmd: The specific sanitization command opcode
114933981838SDan Williams  *
115033981838SDan Williams  * Return: 0 if the command was executed successfully, regardless of
115133981838SDan Williams  * whether or not the actual security operation is done in the background,
115233981838SDan Williams  * such as for the Sanitize case.
115333981838SDan Williams  * Error return values can be the result of the mailbox command, -EINVAL
115433981838SDan Williams  * when security requirements are not met or invalid contexts, or -EBUSY
115533981838SDan Williams  * if the sanitize operation is already in flight.
115633981838SDan Williams  *
115733981838SDan Williams  * See CXL 3.0 @8.2.9.8.5.1 Sanitize and @8.2.9.8.5.2 Secure Erase.
115833981838SDan Williams  */
115933981838SDan Williams int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd)
116033981838SDan Williams {
116133981838SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
116233981838SDan Williams 	struct cxl_port  *endpoint;
116333981838SDan Williams 	int rc;
116433981838SDan Williams 
116533981838SDan Williams 	/* synchronize with cxl_mem_probe() and decoder write operations */
116633981838SDan Williams 	device_lock(&cxlmd->dev);
116733981838SDan Williams 	endpoint = cxlmd->endpoint;
116833981838SDan Williams 	down_read(&cxl_region_rwsem);
116933981838SDan Williams 	/*
117033981838SDan Williams 	 * Require an endpoint to be safe otherwise the driver can not
117133981838SDan Williams 	 * be sure that the device is unmapped.
117233981838SDan Williams 	 */
1173458ba818SDave Jiang 	if (endpoint && cxl_num_decoders_committed(endpoint) == 0)
117433981838SDan Williams 		rc = __cxl_mem_sanitize(mds, cmd);
117533981838SDan Williams 	else
117633981838SDan Williams 		rc = -EBUSY;
117733981838SDan Williams 	up_read(&cxl_region_rwsem);
117833981838SDan Williams 	device_unlock(&cxlmd->dev);
117933981838SDan Williams 
118033981838SDan Williams 	return rc;
118133981838SDan Williams }
118248dcdbb1SDavidlohr Bueso 
1183d3b75029SDan Williams static int add_dpa_res(struct device *dev, struct resource *parent,
1184d3b75029SDan Williams 		       struct resource *res, resource_size_t start,
1185d3b75029SDan Williams 		       resource_size_t size, const char *type)
11864faf31b4SDan Williams {
11874faf31b4SDan Williams 	int rc;
11884faf31b4SDan Williams 
1189d3b75029SDan Williams 	res->name = type;
1190d3b75029SDan Williams 	res->start = start;
1191d3b75029SDan Williams 	res->end = start + size - 1;
1192d3b75029SDan Williams 	res->flags = IORESOURCE_MEM;
1193d3b75029SDan Williams 	if (resource_size(res) == 0) {
1194d3b75029SDan Williams 		dev_dbg(dev, "DPA(%s): no capacity\n", res->name);
11954faf31b4SDan Williams 		return 0;
11964faf31b4SDan Williams 	}
1197d3b75029SDan Williams 	rc = request_resource(parent, res);
1198d3b75029SDan Williams 	if (rc) {
1199d3b75029SDan Williams 		dev_err(dev, "DPA(%s): failed to track %pr (%d)\n", res->name,
1200d3b75029SDan Williams 			res, rc);
1201d3b75029SDan Williams 		return rc;
1202d3b75029SDan Williams 	}
1203d3b75029SDan Williams 
1204d3b75029SDan Williams 	dev_dbg(dev, "DPA(%s): %pr\n", res->name, res);
1205d3b75029SDan Williams 
1206d3b75029SDan Williams 	return 0;
1207d3b75029SDan Williams }
1208d3b75029SDan Williams 
120959f8d151SDan Williams int cxl_mem_create_range_info(struct cxl_memdev_state *mds)
1210d3b75029SDan Williams {
121159f8d151SDan Williams 	struct cxl_dev_state *cxlds = &mds->cxlds;
1212d3b75029SDan Williams 	struct device *dev = cxlds->dev;
1213d3b75029SDan Williams 	int rc;
1214d3b75029SDan Williams 
1215793a539aSDave Jiang 	if (!cxlds->media_ready) {
1216793a539aSDave Jiang 		cxlds->dpa_res = DEFINE_RES_MEM(0, 0);
1217793a539aSDave Jiang 		cxlds->ram_res = DEFINE_RES_MEM(0, 0);
1218793a539aSDave Jiang 		cxlds->pmem_res = DEFINE_RES_MEM(0, 0);
1219793a539aSDave Jiang 		return 0;
1220793a539aSDave Jiang 	}
1221793a539aSDave Jiang 
12229214c9d5SAlison Schofield 	cxlds->dpa_res = DEFINE_RES_MEM(0, mds->total_bytes);
1223d3b75029SDan Williams 
122459f8d151SDan Williams 	if (mds->partition_align_bytes == 0) {
1225d3b75029SDan Williams 		rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
122659f8d151SDan Williams 				 mds->volatile_only_bytes, "ram");
1227d3b75029SDan Williams 		if (rc)
1228d3b75029SDan Williams 			return rc;
1229d3b75029SDan Williams 		return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
123059f8d151SDan Williams 				   mds->volatile_only_bytes,
123159f8d151SDan Williams 				   mds->persistent_only_bytes, "pmem");
1232d3b75029SDan Williams 	}
12334faf31b4SDan Williams 
123459f8d151SDan Williams 	rc = cxl_mem_get_partition_info(mds);
12354faf31b4SDan Williams 	if (rc) {
1236d3b75029SDan Williams 		dev_err(dev, "Failed to query partition information\n");
12374faf31b4SDan Williams 		return rc;
12384faf31b4SDan Williams 	}
12394faf31b4SDan Williams 
1240d3b75029SDan Williams 	rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
124159f8d151SDan Williams 			 mds->active_volatile_bytes, "ram");
1242d3b75029SDan Williams 	if (rc)
1243d3b75029SDan Williams 		return rc;
1244d3b75029SDan Williams 	return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
124559f8d151SDan Williams 			   mds->active_volatile_bytes,
124659f8d151SDan Williams 			   mds->active_persistent_bytes, "pmem");
12474faf31b4SDan Williams }
1248affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL);
12494faf31b4SDan Williams 
125059f8d151SDan Williams int cxl_set_timestamp(struct cxl_memdev_state *mds)
1251fa884345SJonathan Cameron {
1252fa884345SJonathan Cameron 	struct cxl_mbox_cmd mbox_cmd;
1253fa884345SJonathan Cameron 	struct cxl_mbox_set_timestamp_in pi;
1254fa884345SJonathan Cameron 	int rc;
1255fa884345SJonathan Cameron 
1256fa884345SJonathan Cameron 	pi.timestamp = cpu_to_le64(ktime_get_real_ns());
1257fa884345SJonathan Cameron 	mbox_cmd = (struct cxl_mbox_cmd) {
1258fa884345SJonathan Cameron 		.opcode = CXL_MBOX_OP_SET_TIMESTAMP,
1259fa884345SJonathan Cameron 		.size_in = sizeof(pi),
1260fa884345SJonathan Cameron 		.payload_in = &pi,
1261fa884345SJonathan Cameron 	};
1262fa884345SJonathan Cameron 
126359f8d151SDan Williams 	rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1264fa884345SJonathan Cameron 	/*
1265fa884345SJonathan Cameron 	 * Command is optional. Devices may have another way of providing
1266fa884345SJonathan Cameron 	 * a timestamp, or may return all 0s in timestamp fields.
1267fa884345SJonathan Cameron 	 * Don't report an error if this command isn't supported
1268fa884345SJonathan Cameron 	 */
1269fa884345SJonathan Cameron 	if (rc && (mbox_cmd.return_code != CXL_MBOX_CMD_RC_UNSUPPORTED))
1270fa884345SJonathan Cameron 		return rc;
1271fa884345SJonathan Cameron 
1272fa884345SJonathan Cameron 	return 0;
1273fa884345SJonathan Cameron }
1274fa884345SJonathan Cameron EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL);
1275fa884345SJonathan Cameron 
1276ed83f7caSAlison Schofield int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
1277ed83f7caSAlison Schofield 		       struct cxl_region *cxlr)
1278ed83f7caSAlison Schofield {
127959f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
1280ed83f7caSAlison Schofield 	struct cxl_mbox_poison_out *po;
1281ed83f7caSAlison Schofield 	struct cxl_mbox_poison_in pi;
1282ed83f7caSAlison Schofield 	struct cxl_mbox_cmd mbox_cmd;
1283ed83f7caSAlison Schofield 	int nr_records = 0;
1284ed83f7caSAlison Schofield 	int rc;
1285ed83f7caSAlison Schofield 
128659f8d151SDan Williams 	rc = mutex_lock_interruptible(&mds->poison.lock);
1287ed83f7caSAlison Schofield 	if (rc)
1288ed83f7caSAlison Schofield 		return rc;
1289ed83f7caSAlison Schofield 
129059f8d151SDan Williams 	po = mds->poison.list_out;
1291ed83f7caSAlison Schofield 	pi.offset = cpu_to_le64(offset);
1292ed83f7caSAlison Schofield 	pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT);
1293ed83f7caSAlison Schofield 
1294ed83f7caSAlison Schofield 	mbox_cmd = (struct cxl_mbox_cmd) {
1295ed83f7caSAlison Schofield 		.opcode = CXL_MBOX_OP_GET_POISON,
1296ed83f7caSAlison Schofield 		.size_in = sizeof(pi),
1297ed83f7caSAlison Schofield 		.payload_in = &pi,
129859f8d151SDan Williams 		.size_out = mds->payload_size,
1299ed83f7caSAlison Schofield 		.payload_out = po,
1300ed83f7caSAlison Schofield 		.min_out = struct_size(po, record, 0),
1301ed83f7caSAlison Schofield 	};
1302ed83f7caSAlison Schofield 
1303ed83f7caSAlison Schofield 	do {
130459f8d151SDan Williams 		rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1305ed83f7caSAlison Schofield 		if (rc)
1306ed83f7caSAlison Schofield 			break;
1307ed83f7caSAlison Schofield 
1308ddf49d57SAlison Schofield 		for (int i = 0; i < le16_to_cpu(po->count); i++)
1309ddf49d57SAlison Schofield 			trace_cxl_poison(cxlmd, cxlr, &po->record[i],
1310ddf49d57SAlison Schofield 					 po->flags, po->overflow_ts,
1311ddf49d57SAlison Schofield 					 CXL_POISON_TRACE_LIST);
1312ed83f7caSAlison Schofield 
1313ed83f7caSAlison Schofield 		/* Protect against an uncleared _FLAG_MORE */
1314ed83f7caSAlison Schofield 		nr_records = nr_records + le16_to_cpu(po->count);
131559f8d151SDan Williams 		if (nr_records >= mds->poison.max_errors) {
1316ed83f7caSAlison Schofield 			dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n",
1317ed83f7caSAlison Schofield 				nr_records);
1318ed83f7caSAlison Schofield 			break;
1319ed83f7caSAlison Schofield 		}
1320ed83f7caSAlison Schofield 	} while (po->flags & CXL_POISON_FLAG_MORE);
1321ed83f7caSAlison Schofield 
132259f8d151SDan Williams 	mutex_unlock(&mds->poison.lock);
1323ed83f7caSAlison Schofield 	return rc;
1324ed83f7caSAlison Schofield }
1325ed83f7caSAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_mem_get_poison, CXL);
1326ed83f7caSAlison Schofield 
1327d0abf578SAlison Schofield static void free_poison_buf(void *buf)
1328d0abf578SAlison Schofield {
1329d0abf578SAlison Schofield 	kvfree(buf);
1330d0abf578SAlison Schofield }
1331d0abf578SAlison Schofield 
133259f8d151SDan Williams /* Get Poison List output buffer is protected by mds->poison.lock */
133359f8d151SDan Williams static int cxl_poison_alloc_buf(struct cxl_memdev_state *mds)
1334d0abf578SAlison Schofield {
133559f8d151SDan Williams 	mds->poison.list_out = kvmalloc(mds->payload_size, GFP_KERNEL);
133659f8d151SDan Williams 	if (!mds->poison.list_out)
1337d0abf578SAlison Schofield 		return -ENOMEM;
1338d0abf578SAlison Schofield 
133959f8d151SDan Williams 	return devm_add_action_or_reset(mds->cxlds.dev, free_poison_buf,
134059f8d151SDan Williams 					mds->poison.list_out);
1341d0abf578SAlison Schofield }
1342d0abf578SAlison Schofield 
134359f8d151SDan Williams int cxl_poison_state_init(struct cxl_memdev_state *mds)
1344d0abf578SAlison Schofield {
1345d0abf578SAlison Schofield 	int rc;
1346d0abf578SAlison Schofield 
134759f8d151SDan Williams 	if (!test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds))
1348d0abf578SAlison Schofield 		return 0;
1349d0abf578SAlison Schofield 
135059f8d151SDan Williams 	rc = cxl_poison_alloc_buf(mds);
1351d0abf578SAlison Schofield 	if (rc) {
135259f8d151SDan Williams 		clear_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds);
1353d0abf578SAlison Schofield 		return rc;
1354d0abf578SAlison Schofield 	}
1355d0abf578SAlison Schofield 
135659f8d151SDan Williams 	mutex_init(&mds->poison.lock);
1357d0abf578SAlison Schofield 	return 0;
1358d0abf578SAlison Schofield }
1359d0abf578SAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_poison_state_init, CXL);
1360d0abf578SAlison Schofield 
136159f8d151SDan Williams struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev)
13624faf31b4SDan Williams {
136359f8d151SDan Williams 	struct cxl_memdev_state *mds;
13644faf31b4SDan Williams 
136559f8d151SDan Williams 	mds = devm_kzalloc(dev, sizeof(*mds), GFP_KERNEL);
136659f8d151SDan Williams 	if (!mds) {
13674faf31b4SDan Williams 		dev_err(dev, "No memory available\n");
13684faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
13694faf31b4SDan Williams 	}
13704faf31b4SDan Williams 
137159f8d151SDan Williams 	mutex_init(&mds->mbox_mutex);
137259f8d151SDan Williams 	mutex_init(&mds->event.log_lock);
137359f8d151SDan Williams 	mds->cxlds.dev = dev;
13742dd18279SRobert Richter 	mds->cxlds.reg_map.host = dev;
13752dd18279SRobert Richter 	mds->cxlds.reg_map.resource = CXL_RESOURCE_NONE;
1376f6b8ab32SDan Williams 	mds->cxlds.type = CXL_DEVTYPE_CLASSMEM;
13774faf31b4SDan Williams 
137859f8d151SDan Williams 	return mds;
13794faf31b4SDan Williams }
138059f8d151SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_memdev_state_create, CXL);
13814faf31b4SDan Williams 
13824faf31b4SDan Williams void __init cxl_mbox_init(void)
13834faf31b4SDan Williams {
13844faf31b4SDan Williams 	struct dentry *mbox_debugfs;
13854faf31b4SDan Williams 
13869b99ecf5SDan Williams 	mbox_debugfs = cxl_debugfs_create_dir("mbox");
13874faf31b4SDan Williams 	debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs,
13884faf31b4SDan Williams 			    &cxl_raw_allow_all);
13894faf31b4SDan Williams }
1390