xref: /linux/drivers/cxl/core/mbox.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
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>
7*5f60d5f6SAl Viro #include <linux/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),
59940325adSSrinivasulu Thanneeru 	CXL_CMD(GET_LOG_CAPS, 0x10, 0x4, 0),
60206f9fa9SSrinivasulu Thanneeru 	CXL_CMD(CLEAR_LOG, 0x10, 0, 0),
61940325adSSrinivasulu Thanneeru 	CXL_CMD(GET_SUP_LOG_SUBLIST, 0x2, CXL_VARIABLE_PAYLOAD, 0),
624faf31b4SDan Williams 	CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0),
6326f89535SAlison Schofield 	CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0),
644faf31b4SDan Williams 	CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
654faf31b4SDan Williams 	CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0),
664faf31b4SDan Williams 	CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0),
674faf31b4SDan Williams 	CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0),
684faf31b4SDan Williams 	CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0),
69cb46fca8SDavidlohr Bueso 	CXL_CMD(GET_TIMESTAMP, 0, 0x8, 0),
704faf31b4SDan Williams };
714faf31b4SDan Williams 
724faf31b4SDan Williams /*
734faf31b4SDan Williams  * Commands that RAW doesn't permit. The rationale for each:
744faf31b4SDan Williams  *
754faf31b4SDan Williams  * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment /
764faf31b4SDan Williams  * coordination of transaction timeout values at the root bridge level.
774faf31b4SDan Williams  *
784faf31b4SDan Williams  * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live
794faf31b4SDan Williams  * and needs to be coordinated with HDM updates.
804faf31b4SDan Williams  *
814faf31b4SDan Williams  * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the
824faf31b4SDan Williams  * driver and any writes from userspace invalidates those contents.
834faf31b4SDan Williams  *
844faf31b4SDan Williams  * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes
854faf31b4SDan Williams  * to the device after it is marked clean, userspace can not make that
864faf31b4SDan Williams  * assertion.
874faf31b4SDan Williams  *
884faf31b4SDan Williams  * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that
894faf31b4SDan Williams  * is kept up to date with patrol notifications and error management.
90dec441d3SAlison Schofield  *
91dec441d3SAlison Schofield  * CXL_MBOX_OP_[GET_,INJECT_,CLEAR_]POISON: These commands require kernel
92dec441d3SAlison Schofield  * driver orchestration for safety.
934faf31b4SDan Williams  */
944faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = {
954faf31b4SDan Williams 	CXL_MBOX_OP_ACTIVATE_FW,
964faf31b4SDan Williams 	CXL_MBOX_OP_SET_PARTITION_INFO,
974faf31b4SDan Williams 	CXL_MBOX_OP_SET_LSA,
984faf31b4SDan Williams 	CXL_MBOX_OP_SET_SHUTDOWN_STATE,
994faf31b4SDan Williams 	CXL_MBOX_OP_SCAN_MEDIA,
1004faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA,
101dec441d3SAlison Schofield 	CXL_MBOX_OP_GET_POISON,
102dec441d3SAlison Schofield 	CXL_MBOX_OP_INJECT_POISON,
103dec441d3SAlison Schofield 	CXL_MBOX_OP_CLEAR_POISON,
1044faf31b4SDan Williams };
1054faf31b4SDan Williams 
1064faf31b4SDan Williams /*
1074faf31b4SDan Williams  * Command sets that RAW doesn't permit. All opcodes in this set are
1084faf31b4SDan Williams  * disabled because they pass plain text security payloads over the
1094faf31b4SDan Williams  * user/kernel boundary. This functionality is intended to be wrapped
1104faf31b4SDan Williams  * behind the keys ABI which allows for encrypted payloads in the UAPI
1114faf31b4SDan Williams  */
1124faf31b4SDan Williams static u8 security_command_sets[] = {
1134faf31b4SDan Williams 	0x44, /* Sanitize */
1144faf31b4SDan Williams 	0x45, /* Persistent Memory Data-at-rest Security */
1154faf31b4SDan Williams 	0x46, /* Security Passthrough */
1164faf31b4SDan Williams };
1174faf31b4SDan Williams 
cxl_is_security_command(u16 opcode)1184faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode)
1194faf31b4SDan Williams {
1204faf31b4SDan Williams 	int i;
1214faf31b4SDan Williams 
1224faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(security_command_sets); i++)
1234faf31b4SDan Williams 		if (security_command_sets[i] == (opcode >> 8))
1244faf31b4SDan Williams 			return true;
1254faf31b4SDan Williams 	return false;
1264faf31b4SDan Williams }
1274faf31b4SDan Williams 
cxl_set_security_cmd_enabled(struct cxl_security_state * security,u16 opcode)128ad64f595SDavidlohr Bueso static void cxl_set_security_cmd_enabled(struct cxl_security_state *security,
129ad64f595SDavidlohr Bueso 					 u16 opcode)
130ad64f595SDavidlohr Bueso {
131ad64f595SDavidlohr Bueso 	switch (opcode) {
132ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SANITIZE:
133ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SANITIZE, security->enabled_cmds);
134ad64f595SDavidlohr Bueso 		break;
135ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SECURE_ERASE:
136ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SECURE_ERASE,
137ad64f595SDavidlohr Bueso 			security->enabled_cmds);
138ad64f595SDavidlohr Bueso 		break;
139ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_GET_SECURITY_STATE:
140ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_GET_SECURITY_STATE,
141ad64f595SDavidlohr Bueso 			security->enabled_cmds);
142ad64f595SDavidlohr Bueso 		break;
143ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_SET_PASSPHRASE:
144ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_SET_PASSPHRASE,
145ad64f595SDavidlohr Bueso 			security->enabled_cmds);
146ad64f595SDavidlohr Bueso 		break;
147ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_DISABLE_PASSPHRASE:
148ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
149ad64f595SDavidlohr Bueso 			security->enabled_cmds);
150ad64f595SDavidlohr Bueso 		break;
151ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_UNLOCK:
152ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_UNLOCK, security->enabled_cmds);
153ad64f595SDavidlohr Bueso 		break;
154ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_FREEZE_SECURITY:
155ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_FREEZE_SECURITY,
156ad64f595SDavidlohr Bueso 			security->enabled_cmds);
157ad64f595SDavidlohr Bueso 		break;
158ad64f595SDavidlohr Bueso 	case CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE:
159ad64f595SDavidlohr Bueso 		set_bit(CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
160ad64f595SDavidlohr Bueso 			security->enabled_cmds);
161ad64f595SDavidlohr Bueso 		break;
162ad64f595SDavidlohr Bueso 	default:
163ad64f595SDavidlohr Bueso 		break;
164ad64f595SDavidlohr Bueso 	}
165ad64f595SDavidlohr Bueso }
166ad64f595SDavidlohr Bueso 
cxl_is_poison_command(u16 opcode)167d0abf578SAlison Schofield static bool cxl_is_poison_command(u16 opcode)
168d0abf578SAlison Schofield {
169d0abf578SAlison Schofield #define CXL_MBOX_OP_POISON_CMDS 0x43
170d0abf578SAlison Schofield 
171d0abf578SAlison Schofield 	if ((opcode >> 8) == CXL_MBOX_OP_POISON_CMDS)
172d0abf578SAlison Schofield 		return true;
173d0abf578SAlison Schofield 
174d0abf578SAlison Schofield 	return false;
175d0abf578SAlison Schofield }
176d0abf578SAlison Schofield 
cxl_set_poison_cmd_enabled(struct cxl_poison_state * poison,u16 opcode)177d0abf578SAlison Schofield static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison,
178d0abf578SAlison Schofield 				       u16 opcode)
179d0abf578SAlison Schofield {
180d0abf578SAlison Schofield 	switch (opcode) {
181d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_POISON:
182d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_LIST, poison->enabled_cmds);
183d0abf578SAlison Schofield 		break;
184d0abf578SAlison Schofield 	case CXL_MBOX_OP_INJECT_POISON:
185d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_INJECT, poison->enabled_cmds);
186d0abf578SAlison Schofield 		break;
187d0abf578SAlison Schofield 	case CXL_MBOX_OP_CLEAR_POISON:
188d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_CLEAR, poison->enabled_cmds);
189d0abf578SAlison Schofield 		break;
190d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS:
191d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_CAPS, poison->enabled_cmds);
192d0abf578SAlison Schofield 		break;
193d0abf578SAlison Schofield 	case CXL_MBOX_OP_SCAN_MEDIA:
194d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_MEDIA, poison->enabled_cmds);
195d0abf578SAlison Schofield 		break;
196d0abf578SAlison Schofield 	case CXL_MBOX_OP_GET_SCAN_MEDIA:
197d0abf578SAlison Schofield 		set_bit(CXL_POISON_ENABLED_SCAN_RESULTS, poison->enabled_cmds);
198d0abf578SAlison Schofield 		break;
199d0abf578SAlison Schofield 	default:
200d0abf578SAlison Schofield 		break;
201d0abf578SAlison Schofield 	}
202d0abf578SAlison Schofield }
203d0abf578SAlison Schofield 
cxl_mem_find_command(u16 opcode)2044faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
2054faf31b4SDan Williams {
2064faf31b4SDan Williams 	struct cxl_mem_command *c;
2074faf31b4SDan Williams 
2084faf31b4SDan Williams 	cxl_for_each_cmd(c)
2094faf31b4SDan Williams 		if (c->opcode == opcode)
2104faf31b4SDan Williams 			return c;
2114faf31b4SDan Williams 
2124faf31b4SDan Williams 	return NULL;
2134faf31b4SDan Williams }
2144faf31b4SDan Williams 
cxl_mem_opcode_to_name(u16 opcode)21582b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode)
21682b8ba29SAlison Schofield {
21782b8ba29SAlison Schofield 	struct cxl_mem_command *c;
21882b8ba29SAlison Schofield 
21982b8ba29SAlison Schofield 	c = cxl_mem_find_command(opcode);
22082b8ba29SAlison Schofield 	if (!c)
22182b8ba29SAlison Schofield 		return NULL;
22282b8ba29SAlison Schofield 
22382b8ba29SAlison Schofield 	return cxl_command_names[c->info.id].name;
22482b8ba29SAlison Schofield }
22582b8ba29SAlison Schofield 
2264faf31b4SDan Williams /**
2275331cdf4SDan Williams  * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command
228b5209da3SDave Jiang  * @cxl_mbox: CXL mailbox context
2295331cdf4SDan Williams  * @mbox_cmd: initialized command to execute
2304faf31b4SDan Williams  *
231ee92c7e2SDavidlohr Bueso  * Context: Any context.
2324faf31b4SDan Williams  * Return:
2334faf31b4SDan Williams  *  * %>=0	- Number of bytes returned in @out.
2344faf31b4SDan Williams  *  * %-E2BIG	- Payload is too large for hardware.
2354faf31b4SDan Williams  *  * %-EBUSY	- Couldn't acquire exclusive mailbox access.
2364faf31b4SDan Williams  *  * %-EFAULT	- Hardware error occurred.
2374faf31b4SDan Williams  *  * %-ENXIO	- Command completed, but device reported an error.
2384faf31b4SDan Williams  *  * %-EIO	- Unexpected output size.
2394faf31b4SDan Williams  *
2404faf31b4SDan Williams  * Mailbox commands may execute successfully yet the device itself reported an
2414faf31b4SDan Williams  * error. While this distinction can be useful for commands from userspace, the
2424faf31b4SDan Williams  * kernel will only be able to use results when both are successful.
2434faf31b4SDan Williams  */
cxl_internal_send_cmd(struct cxl_mailbox * cxl_mbox,struct cxl_mbox_cmd * mbox_cmd)244b5209da3SDave Jiang int cxl_internal_send_cmd(struct cxl_mailbox *cxl_mbox,
2455331cdf4SDan Williams 			  struct cxl_mbox_cmd *mbox_cmd)
2464faf31b4SDan Williams {
2472aeaf663SDan Williams 	size_t out_size, min_out;
2484faf31b4SDan Williams 	int rc;
2494faf31b4SDan Williams 
2508d8081ceSDave Jiang 	if (mbox_cmd->size_in > cxl_mbox->payload_size ||
2518d8081ceSDave Jiang 	    mbox_cmd->size_out > cxl_mbox->payload_size)
2524faf31b4SDan Williams 		return -E2BIG;
2534faf31b4SDan Williams 
2545331cdf4SDan Williams 	out_size = mbox_cmd->size_out;
2552aeaf663SDan Williams 	min_out = mbox_cmd->min_out;
2568d8081ceSDave Jiang 	rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd);
257623c0751SRobert Richter 	/*
258623c0751SRobert Richter 	 * EIO is reserved for a payload size mismatch and mbox_send()
259623c0751SRobert Richter 	 * may not return this error.
260623c0751SRobert Richter 	 */
261623c0751SRobert Richter 	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))
262623c0751SRobert Richter 		return -ENXIO;
2634faf31b4SDan Williams 	if (rc)
2644faf31b4SDan Williams 		return rc;
2654faf31b4SDan Williams 
266ccadf131SDavidlohr Bueso 	if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS &&
267ccadf131SDavidlohr Bueso 	    mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND)
2685331cdf4SDan Williams 		return cxl_mbox_cmd_rc2errno(mbox_cmd);
2694faf31b4SDan Williams 
2702aeaf663SDan Williams 	if (!out_size)
2712aeaf663SDan Williams 		return 0;
2722aeaf663SDan Williams 
2734faf31b4SDan Williams 	/*
2742aeaf663SDan Williams 	 * Variable sized output needs to at least satisfy the caller's
2752aeaf663SDan Williams 	 * minimum if not the fully requested size.
2764faf31b4SDan Williams 	 */
2772aeaf663SDan Williams 	if (min_out == 0)
2782aeaf663SDan Williams 		min_out = out_size;
2792aeaf663SDan Williams 
2802aeaf663SDan Williams 	if (mbox_cmd->size_out < min_out)
2814faf31b4SDan Williams 		return -EIO;
2824faf31b4SDan Williams 	return 0;
2834faf31b4SDan Williams }
2845331cdf4SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, CXL);
2854faf31b4SDan Williams 
cxl_mem_raw_command_allowed(u16 opcode)2864faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode)
2874faf31b4SDan Williams {
2884faf31b4SDan Williams 	int i;
2894faf31b4SDan Williams 
2904faf31b4SDan Williams 	if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS))
2914faf31b4SDan Williams 		return false;
2924faf31b4SDan Williams 
2934faf31b4SDan Williams 	if (security_locked_down(LOCKDOWN_PCI_ACCESS))
2944faf31b4SDan Williams 		return false;
2954faf31b4SDan Williams 
2964faf31b4SDan Williams 	if (cxl_raw_allow_all)
2974faf31b4SDan Williams 		return true;
2984faf31b4SDan Williams 
2994faf31b4SDan Williams 	if (cxl_is_security_command(opcode))
3004faf31b4SDan Williams 		return false;
3014faf31b4SDan Williams 
3024faf31b4SDan Williams 	for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++)
3034faf31b4SDan Williams 		if (cxl_disabled_raw_commands[i] == opcode)
3044faf31b4SDan Williams 			return false;
3054faf31b4SDan Williams 
3064faf31b4SDan Williams 	return true;
3074faf31b4SDan Williams }
3084faf31b4SDan Williams 
3096179045cSAlison Schofield /**
3106179045cSAlison Schofield  * cxl_payload_from_user_allowed() - Check contents of in_payload.
3116179045cSAlison Schofield  * @opcode: The mailbox command opcode.
3126179045cSAlison Schofield  * @payload_in: Pointer to the input payload passed in from user space.
3136179045cSAlison Schofield  *
3146179045cSAlison Schofield  * Return:
3156179045cSAlison Schofield  *  * true	- payload_in passes check for @opcode.
3166179045cSAlison Schofield  *  * false	- payload_in contains invalid or unsupported values.
3176179045cSAlison Schofield  *
3186179045cSAlison Schofield  * The driver may inspect payload contents before sending a mailbox
3196179045cSAlison Schofield  * command from user space to the device. The intent is to reject
3206179045cSAlison Schofield  * commands with input payloads that are known to be unsafe. This
3216179045cSAlison Schofield  * check is not intended to replace the users careful selection of
3226179045cSAlison Schofield  * mailbox command parameters and makes no guarantee that the user
3236179045cSAlison Schofield  * command will succeed, nor that it is appropriate.
3246179045cSAlison Schofield  *
3256179045cSAlison Schofield  * The specific checks are determined by the opcode.
3266179045cSAlison Schofield  */
cxl_payload_from_user_allowed(u16 opcode,void * payload_in)3276179045cSAlison Schofield static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in)
3286179045cSAlison Schofield {
3296179045cSAlison Schofield 	switch (opcode) {
3306179045cSAlison Schofield 	case CXL_MBOX_OP_SET_PARTITION_INFO: {
3316179045cSAlison Schofield 		struct cxl_mbox_set_partition_info *pi = payload_in;
3326179045cSAlison Schofield 
33335e01667SDan Carpenter 		if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG)
3346179045cSAlison Schofield 			return false;
3356179045cSAlison Schofield 		break;
3366179045cSAlison Schofield 	}
337206f9fa9SSrinivasulu Thanneeru 	case CXL_MBOX_OP_CLEAR_LOG: {
338206f9fa9SSrinivasulu Thanneeru 		const uuid_t *uuid = (uuid_t *)payload_in;
339206f9fa9SSrinivasulu Thanneeru 
340206f9fa9SSrinivasulu Thanneeru 		/*
341206f9fa9SSrinivasulu Thanneeru 		 * Restrict the ‘Clear log’ action to only apply to
342206f9fa9SSrinivasulu Thanneeru 		 * Vendor debug logs.
343206f9fa9SSrinivasulu Thanneeru 		 */
344206f9fa9SSrinivasulu Thanneeru 		return uuid_equal(uuid, &DEFINE_CXL_VENDOR_DEBUG_UUID);
345206f9fa9SSrinivasulu Thanneeru 	}
3466179045cSAlison Schofield 	default:
3476179045cSAlison Schofield 		break;
3486179045cSAlison Schofield 	}
3496179045cSAlison Schofield 	return true;
3506179045cSAlison Schofield }
3516179045cSAlison Schofield 
cxl_mbox_cmd_ctor(struct cxl_mbox_cmd * mbox,struct cxl_memdev_state * mds,u16 opcode,size_t in_size,size_t out_size,u64 in_payload)35263cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
35359f8d151SDan Williams 			     struct cxl_memdev_state *mds, u16 opcode,
35463cf60b7SAlison Schofield 			     size_t in_size, size_t out_size, u64 in_payload)
35563cf60b7SAlison Schofield {
3568d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
35763cf60b7SAlison Schofield 	*mbox = (struct cxl_mbox_cmd) {
35863cf60b7SAlison Schofield 		.opcode = opcode,
35963cf60b7SAlison Schofield 		.size_in = in_size,
36063cf60b7SAlison Schofield 	};
36163cf60b7SAlison Schofield 
36263cf60b7SAlison Schofield 	if (in_size) {
36363cf60b7SAlison Schofield 		mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload),
36463cf60b7SAlison Schofield 						in_size);
365280302f0SAlison Schofield 		if (IS_ERR(mbox->payload_in))
36663cf60b7SAlison Schofield 			return PTR_ERR(mbox->payload_in);
3676179045cSAlison Schofield 
3686179045cSAlison Schofield 		if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) {
36959f8d151SDan Williams 			dev_dbg(mds->cxlds.dev, "%s: input payload not allowed\n",
3706179045cSAlison Schofield 				cxl_mem_opcode_to_name(opcode));
3716179045cSAlison Schofield 			kvfree(mbox->payload_in);
3726179045cSAlison Schofield 			return -EBUSY;
3736179045cSAlison Schofield 		}
37463cf60b7SAlison Schofield 	}
37563cf60b7SAlison Schofield 
37663cf60b7SAlison Schofield 	/* Prepare to handle a full payload for variable sized output */
37726f89535SAlison Schofield 	if (out_size == CXL_VARIABLE_PAYLOAD)
3788d8081ceSDave Jiang 		mbox->size_out = cxl_mbox->payload_size;
37963cf60b7SAlison Schofield 	else
38063cf60b7SAlison Schofield 		mbox->size_out = out_size;
38163cf60b7SAlison Schofield 
38263cf60b7SAlison Schofield 	if (mbox->size_out) {
38363cf60b7SAlison Schofield 		mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL);
38463cf60b7SAlison Schofield 		if (!mbox->payload_out) {
38563cf60b7SAlison Schofield 			kvfree(mbox->payload_in);
38663cf60b7SAlison Schofield 			return -ENOMEM;
38763cf60b7SAlison Schofield 		}
38863cf60b7SAlison Schofield 	}
38963cf60b7SAlison Schofield 	return 0;
39063cf60b7SAlison Schofield }
39163cf60b7SAlison Schofield 
cxl_mbox_cmd_dtor(struct cxl_mbox_cmd * mbox)39263cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox)
39363cf60b7SAlison Schofield {
39463cf60b7SAlison Schofield 	kvfree(mbox->payload_in);
39563cf60b7SAlison Schofield 	kvfree(mbox->payload_out);
39663cf60b7SAlison Schofield }
39763cf60b7SAlison Schofield 
cxl_to_mem_cmd_raw(struct cxl_mem_command * mem_cmd,const struct cxl_send_command * send_cmd,struct cxl_memdev_state * mds)3986dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,
3996dd0e5ccSAlison Schofield 			      const struct cxl_send_command *send_cmd,
40059f8d151SDan Williams 			      struct cxl_memdev_state *mds)
4016dd0e5ccSAlison Schofield {
4028d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
4038d8081ceSDave Jiang 
4046dd0e5ccSAlison Schofield 	if (send_cmd->raw.rsvd)
4056dd0e5ccSAlison Schofield 		return -EINVAL;
4066dd0e5ccSAlison Schofield 
4076dd0e5ccSAlison Schofield 	/*
4086dd0e5ccSAlison Schofield 	 * Unlike supported commands, the output size of RAW commands
4096dd0e5ccSAlison Schofield 	 * gets passed along without further checking, so it must be
4106dd0e5ccSAlison Schofield 	 * validated here.
4116dd0e5ccSAlison Schofield 	 */
4128d8081ceSDave Jiang 	if (send_cmd->out.size > cxl_mbox->payload_size)
4136dd0e5ccSAlison Schofield 		return -EINVAL;
4146dd0e5ccSAlison Schofield 
4156dd0e5ccSAlison Schofield 	if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode))
4166dd0e5ccSAlison Schofield 		return -EPERM;
4176dd0e5ccSAlison Schofield 
41859f8d151SDan Williams 	dev_WARN_ONCE(mds->cxlds.dev, true, "raw command path used\n");
41939ed8da4SAlison Schofield 
4206dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
4216dd0e5ccSAlison Schofield 		.info = {
4226dd0e5ccSAlison Schofield 			.id = CXL_MEM_COMMAND_ID_RAW,
4236dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
4246dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
4256dd0e5ccSAlison Schofield 		},
4266dd0e5ccSAlison Schofield 		.opcode = send_cmd->raw.opcode
4276dd0e5ccSAlison Schofield 	};
4286dd0e5ccSAlison Schofield 
4296dd0e5ccSAlison Schofield 	return 0;
4306dd0e5ccSAlison Schofield }
4316dd0e5ccSAlison Schofield 
cxl_to_mem_cmd(struct cxl_mem_command * mem_cmd,const struct cxl_send_command * send_cmd,struct cxl_memdev_state * mds)4326dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,
4336dd0e5ccSAlison Schofield 			  const struct cxl_send_command *send_cmd,
43459f8d151SDan Williams 			  struct cxl_memdev_state *mds)
4356dd0e5ccSAlison Schofield {
4366dd0e5ccSAlison Schofield 	struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id];
4376dd0e5ccSAlison Schofield 	const struct cxl_command_info *info = &c->info;
4386dd0e5ccSAlison Schofield 
4396dd0e5ccSAlison Schofield 	if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK)
4406dd0e5ccSAlison Schofield 		return -EINVAL;
4416dd0e5ccSAlison Schofield 
4426dd0e5ccSAlison Schofield 	if (send_cmd->rsvd)
4436dd0e5ccSAlison Schofield 		return -EINVAL;
4446dd0e5ccSAlison Schofield 
4456dd0e5ccSAlison Schofield 	if (send_cmd->in.rsvd || send_cmd->out.rsvd)
4466dd0e5ccSAlison Schofield 		return -EINVAL;
4476dd0e5ccSAlison Schofield 
4486dd0e5ccSAlison Schofield 	/* Check that the command is enabled for hardware */
44959f8d151SDan Williams 	if (!test_bit(info->id, mds->enabled_cmds))
4506dd0e5ccSAlison Schofield 		return -ENOTTY;
4516dd0e5ccSAlison Schofield 
4526dd0e5ccSAlison Schofield 	/* Check that the command is not claimed for exclusive kernel use */
45359f8d151SDan Williams 	if (test_bit(info->id, mds->exclusive_cmds))
4546dd0e5ccSAlison Schofield 		return -EBUSY;
4556dd0e5ccSAlison Schofield 
4566dd0e5ccSAlison Schofield 	/* Check the input buffer is the expected size */
457e35f5718SVishal Verma 	if ((info->size_in != CXL_VARIABLE_PAYLOAD) &&
458e35f5718SVishal Verma 	    (info->size_in != send_cmd->in.size))
4596dd0e5ccSAlison Schofield 		return -ENOMEM;
4606dd0e5ccSAlison Schofield 
4616dd0e5ccSAlison Schofield 	/* Check the output buffer is at least large enough */
462e35f5718SVishal Verma 	if ((info->size_out != CXL_VARIABLE_PAYLOAD) &&
463e35f5718SVishal Verma 	    (send_cmd->out.size < info->size_out))
4646dd0e5ccSAlison Schofield 		return -ENOMEM;
4656dd0e5ccSAlison Schofield 
4666dd0e5ccSAlison Schofield 	*mem_cmd = (struct cxl_mem_command) {
4676dd0e5ccSAlison Schofield 		.info = {
4686dd0e5ccSAlison Schofield 			.id = info->id,
4696dd0e5ccSAlison Schofield 			.flags = info->flags,
4706dd0e5ccSAlison Schofield 			.size_in = send_cmd->in.size,
4716dd0e5ccSAlison Schofield 			.size_out = send_cmd->out.size,
4726dd0e5ccSAlison Schofield 		},
4736dd0e5ccSAlison Schofield 		.opcode = c->opcode
4746dd0e5ccSAlison Schofield 	};
4756dd0e5ccSAlison Schofield 
4766dd0e5ccSAlison Schofield 	return 0;
4776dd0e5ccSAlison Schofield }
4786dd0e5ccSAlison Schofield 
4794faf31b4SDan Williams /**
4804faf31b4SDan Williams  * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
4819ae016aeSAlison Schofield  * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd.
48259f8d151SDan Williams  * @mds: The driver data for the operation
4834faf31b4SDan Williams  * @send_cmd: &struct cxl_send_command copied in from userspace.
4844faf31b4SDan Williams  *
4854faf31b4SDan Williams  * Return:
4864faf31b4SDan Williams  *  * %0	- @out_cmd is ready to send.
4874faf31b4SDan Williams  *  * %-ENOTTY	- Invalid command specified.
4884faf31b4SDan Williams  *  * %-EINVAL	- Reserved fields or invalid values were used.
4894faf31b4SDan Williams  *  * %-ENOMEM	- Input or output buffer wasn't sized properly.
4904faf31b4SDan Williams  *  * %-EPERM	- Attempted to use a protected command.
49112f3856aSDan Williams  *  * %-EBUSY	- Kernel has claimed exclusive access to this opcode
4924faf31b4SDan Williams  *
4932dd5600aSAlison Schofield  * The result of this command is a fully validated command in @mbox_cmd that is
4944faf31b4SDan Williams  * safe to send to the hardware.
4954faf31b4SDan Williams  */
cxl_validate_cmd_from_user(struct cxl_mbox_cmd * mbox_cmd,struct cxl_memdev_state * mds,const struct cxl_send_command * send_cmd)4969ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,
49759f8d151SDan Williams 				      struct cxl_memdev_state *mds,
4982dd5600aSAlison Schofield 				      const struct cxl_send_command *send_cmd)
4994faf31b4SDan Williams {
5008d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
5012dd5600aSAlison Schofield 	struct cxl_mem_command mem_cmd;
5029ae016aeSAlison Schofield 	int rc;
5039ae016aeSAlison Schofield 
5044faf31b4SDan Williams 	if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX)
5054faf31b4SDan Williams 		return -ENOTTY;
5064faf31b4SDan Williams 
5074faf31b4SDan Williams 	/*
5084faf31b4SDan Williams 	 * The user can never specify an input payload larger than what hardware
5094faf31b4SDan Williams 	 * supports, but output can be arbitrarily large (simply write out as
5104faf31b4SDan Williams 	 * much data as the hardware provides).
5114faf31b4SDan Williams 	 */
5128d8081ceSDave Jiang 	if (send_cmd->in.size > cxl_mbox->payload_size)
5134faf31b4SDan Williams 		return -EINVAL;
5144faf31b4SDan Williams 
5156dd0e5ccSAlison Schofield 	/* Sanitize and construct a cxl_mem_command */
5166dd0e5ccSAlison Schofield 	if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW)
51759f8d151SDan Williams 		rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, mds);
5186dd0e5ccSAlison Schofield 	else
51959f8d151SDan Williams 		rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, mds);
5209ae016aeSAlison Schofield 
5219ae016aeSAlison Schofield 	if (rc)
5229ae016aeSAlison Schofield 		return rc;
5239ae016aeSAlison Schofield 
5249ae016aeSAlison Schofield 	/* Sanitize and construct a cxl_mbox_cmd */
52559f8d151SDan Williams 	return cxl_mbox_cmd_ctor(mbox_cmd, mds, mem_cmd.opcode,
5262dd5600aSAlison Schofield 				 mem_cmd.info.size_in, mem_cmd.info.size_out,
5279ae016aeSAlison Schofield 				 send_cmd->in.payload);
5284faf31b4SDan Williams }
5294faf31b4SDan Williams 
cxl_query_cmd(struct cxl_memdev * cxlmd,struct cxl_mem_query_commands __user * q)5304faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd,
5314faf31b4SDan Williams 		  struct cxl_mem_query_commands __user *q)
5324faf31b4SDan Williams {
53359f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
5344faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
5354faf31b4SDan Williams 	struct cxl_mem_command *cmd;
5364faf31b4SDan Williams 	u32 n_commands;
5374faf31b4SDan Williams 	int j = 0;
5384faf31b4SDan Williams 
5394faf31b4SDan Williams 	dev_dbg(dev, "Query IOCTL\n");
5404faf31b4SDan Williams 
5414faf31b4SDan Williams 	if (get_user(n_commands, &q->n_commands))
5424faf31b4SDan Williams 		return -EFAULT;
5434faf31b4SDan Williams 
5444faf31b4SDan Williams 	/* returns the total number if 0 elements are requested. */
5454faf31b4SDan Williams 	if (n_commands == 0)
546ff56ab9eSDan Williams 		return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands);
5474faf31b4SDan Williams 
5484faf31b4SDan Williams 	/*
549423c9baaSDave Jiang 	 * otherwise, return min(n_commands, total commands) cxl_command_info
5504faf31b4SDan Williams 	 * structures.
5514faf31b4SDan Williams 	 */
5524faf31b4SDan Williams 	cxl_for_each_cmd(cmd) {
553814a15f3SIra Weiny 		struct cxl_command_info info = cmd->info;
5544faf31b4SDan Williams 
55559f8d151SDan Williams 		if (test_bit(info.id, mds->enabled_cmds))
556814a15f3SIra Weiny 			info.flags |= CXL_MEM_COMMAND_FLAG_ENABLED;
55759f8d151SDan Williams 		if (test_bit(info.id, mds->exclusive_cmds))
558814a15f3SIra Weiny 			info.flags |= CXL_MEM_COMMAND_FLAG_EXCLUSIVE;
559814a15f3SIra Weiny 
560814a15f3SIra Weiny 		if (copy_to_user(&q->commands[j++], &info, sizeof(info)))
5614faf31b4SDan Williams 			return -EFAULT;
5624faf31b4SDan Williams 
5634faf31b4SDan Williams 		if (j == n_commands)
5644faf31b4SDan Williams 			break;
5654faf31b4SDan Williams 	}
5664faf31b4SDan Williams 
5674faf31b4SDan Williams 	return 0;
5684faf31b4SDan Williams }
5694faf31b4SDan Williams 
5704faf31b4SDan Williams /**
5714faf31b4SDan Williams  * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace.
57259f8d151SDan Williams  * @mds: The driver data for the operation
573d97fe8eeSAlison Schofield  * @mbox_cmd: The validated mailbox command.
5744faf31b4SDan Williams  * @out_payload: Pointer to userspace's output payload.
5754faf31b4SDan Williams  * @size_out: (Input) Max payload size to copy out.
5764faf31b4SDan Williams  *            (Output) Payload size hardware generated.
5774faf31b4SDan Williams  * @retval: Hardware generated return code from the operation.
5784faf31b4SDan Williams  *
5794faf31b4SDan Williams  * Return:
5804faf31b4SDan Williams  *  * %0	- Mailbox transaction succeeded. This implies the mailbox
5814faf31b4SDan Williams  *		  protocol completed successfully not that the operation itself
5824faf31b4SDan Williams  *		  was successful.
5834faf31b4SDan Williams  *  * %-ENOMEM  - Couldn't allocate a bounce buffer.
5844faf31b4SDan Williams  *  * %-EFAULT	- Something happened with copy_to/from_user.
5854faf31b4SDan Williams  *  * %-EINTR	- Mailbox acquisition interrupted.
5864faf31b4SDan Williams  *  * %-EXXX	- Transaction level failures.
5874faf31b4SDan Williams  *
588d97fe8eeSAlison Schofield  * Dispatches a mailbox command on behalf of a userspace request.
589d97fe8eeSAlison Schofield  * The output payload is copied to userspace.
5904faf31b4SDan Williams  *
5914faf31b4SDan Williams  * See cxl_send_cmd().
5924faf31b4SDan Williams  */
handle_mailbox_cmd_from_user(struct cxl_memdev_state * mds,struct cxl_mbox_cmd * mbox_cmd,u64 out_payload,s32 * size_out,u32 * retval)59359f8d151SDan Williams static int handle_mailbox_cmd_from_user(struct cxl_memdev_state *mds,
594d97fe8eeSAlison Schofield 					struct cxl_mbox_cmd *mbox_cmd,
595d97fe8eeSAlison Schofield 					u64 out_payload, s32 *size_out,
596d97fe8eeSAlison Schofield 					u32 *retval)
5974faf31b4SDan Williams {
5988d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
59959f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
6004faf31b4SDan Williams 	int rc;
6014faf31b4SDan Williams 
6024faf31b4SDan Williams 	dev_dbg(dev,
6034faf31b4SDan Williams 		"Submitting %s command for user\n"
6044faf31b4SDan Williams 		"\topcode: %x\n"
60582b8ba29SAlison Schofield 		"\tsize: %zx\n",
606d97fe8eeSAlison Schofield 		cxl_mem_opcode_to_name(mbox_cmd->opcode),
607d97fe8eeSAlison Schofield 		mbox_cmd->opcode, mbox_cmd->size_in);
6084faf31b4SDan Williams 
6098d8081ceSDave Jiang 	rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd);
6104faf31b4SDan Williams 	if (rc)
6114faf31b4SDan Williams 		goto out;
6124faf31b4SDan Williams 
6134faf31b4SDan Williams 	/*
6144faf31b4SDan Williams 	 * @size_out contains the max size that's allowed to be written back out
6154faf31b4SDan Williams 	 * to userspace. While the payload may have written more output than
6164faf31b4SDan Williams 	 * this it will have to be ignored.
6174faf31b4SDan Williams 	 */
618d97fe8eeSAlison Schofield 	if (mbox_cmd->size_out) {
619d97fe8eeSAlison Schofield 		dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out,
6204faf31b4SDan Williams 			      "Invalid return size\n");
6214faf31b4SDan Williams 		if (copy_to_user(u64_to_user_ptr(out_payload),
622d97fe8eeSAlison Schofield 				 mbox_cmd->payload_out, mbox_cmd->size_out)) {
6234faf31b4SDan Williams 			rc = -EFAULT;
6244faf31b4SDan Williams 			goto out;
6254faf31b4SDan Williams 		}
6264faf31b4SDan Williams 	}
6274faf31b4SDan Williams 
628d97fe8eeSAlison Schofield 	*size_out = mbox_cmd->size_out;
629d97fe8eeSAlison Schofield 	*retval = mbox_cmd->return_code;
6304faf31b4SDan Williams 
6314faf31b4SDan Williams out:
632d97fe8eeSAlison Schofield 	cxl_mbox_cmd_dtor(mbox_cmd);
6334faf31b4SDan Williams 	return rc;
6344faf31b4SDan Williams }
6354faf31b4SDan Williams 
cxl_send_cmd(struct cxl_memdev * cxlmd,struct cxl_send_command __user * s)6364faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)
6374faf31b4SDan Williams {
63859f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
6394faf31b4SDan Williams 	struct device *dev = &cxlmd->dev;
6404faf31b4SDan Williams 	struct cxl_send_command send;
6419ae016aeSAlison Schofield 	struct cxl_mbox_cmd mbox_cmd;
6424faf31b4SDan Williams 	int rc;
6434faf31b4SDan Williams 
6444faf31b4SDan Williams 	dev_dbg(dev, "Send IOCTL\n");
6454faf31b4SDan Williams 
6464faf31b4SDan Williams 	if (copy_from_user(&send, s, sizeof(send)))
6474faf31b4SDan Williams 		return -EFAULT;
6484faf31b4SDan Williams 
64959f8d151SDan Williams 	rc = cxl_validate_cmd_from_user(&mbox_cmd, mds, &send);
6504faf31b4SDan Williams 	if (rc)
6514faf31b4SDan Williams 		return rc;
6524faf31b4SDan Williams 
65359f8d151SDan Williams 	rc = handle_mailbox_cmd_from_user(mds, &mbox_cmd, send.out.payload,
654d97fe8eeSAlison Schofield 					  &send.out.size, &send.retval);
6554faf31b4SDan Williams 	if (rc)
6564faf31b4SDan Williams 		return rc;
6574faf31b4SDan Williams 
6584faf31b4SDan Williams 	if (copy_to_user(s, &send, sizeof(send)))
6594faf31b4SDan Williams 		return -EFAULT;
6604faf31b4SDan Williams 
6614faf31b4SDan Williams 	return 0;
6624faf31b4SDan Williams }
6634faf31b4SDan Williams 
cxl_xfer_log(struct cxl_memdev_state * mds,uuid_t * uuid,u32 * size,u8 * out)66459f8d151SDan Williams static int cxl_xfer_log(struct cxl_memdev_state *mds, uuid_t *uuid,
66559f8d151SDan Williams 			u32 *size, u8 *out)
6664faf31b4SDan Williams {
6678d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
668623c0751SRobert Richter 	u32 remaining = *size;
6694faf31b4SDan Williams 	u32 offset = 0;
6704faf31b4SDan Williams 
6714faf31b4SDan Williams 	while (remaining) {
6728d8081ceSDave Jiang 		u32 xfer_size = min_t(u32, remaining, cxl_mbox->payload_size);
6735331cdf4SDan Williams 		struct cxl_mbox_cmd mbox_cmd;
6745331cdf4SDan Williams 		struct cxl_mbox_get_log log;
6754faf31b4SDan Williams 		int rc;
6764faf31b4SDan Williams 
6775331cdf4SDan Williams 		log = (struct cxl_mbox_get_log) {
6785331cdf4SDan Williams 			.uuid = *uuid,
6795331cdf4SDan Williams 			.offset = cpu_to_le32(offset),
6805331cdf4SDan Williams 			.length = cpu_to_le32(xfer_size),
6815331cdf4SDan Williams 		};
6825331cdf4SDan Williams 
6835331cdf4SDan Williams 		mbox_cmd = (struct cxl_mbox_cmd) {
6845331cdf4SDan Williams 			.opcode = CXL_MBOX_OP_GET_LOG,
6855331cdf4SDan Williams 			.size_in = sizeof(log),
6865331cdf4SDan Williams 			.payload_in = &log,
6875331cdf4SDan Williams 			.size_out = xfer_size,
6885331cdf4SDan Williams 			.payload_out = out,
6895331cdf4SDan Williams 		};
6905331cdf4SDan Williams 
691b5209da3SDave Jiang 		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
692623c0751SRobert Richter 
693623c0751SRobert Richter 		/*
694623c0751SRobert Richter 		 * The output payload length that indicates the number
695623c0751SRobert Richter 		 * of valid bytes can be smaller than the Log buffer
696623c0751SRobert Richter 		 * size.
697623c0751SRobert Richter 		 */
698623c0751SRobert Richter 		if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
699623c0751SRobert Richter 			offset += mbox_cmd.size_out;
700623c0751SRobert Richter 			break;
701623c0751SRobert Richter 		}
702623c0751SRobert Richter 
7034faf31b4SDan Williams 		if (rc < 0)
7044faf31b4SDan Williams 			return rc;
7054faf31b4SDan Williams 
7064faf31b4SDan Williams 		out += xfer_size;
7074faf31b4SDan Williams 		remaining -= xfer_size;
7084faf31b4SDan Williams 		offset += xfer_size;
7094faf31b4SDan Williams 	}
7104faf31b4SDan Williams 
711623c0751SRobert Richter 	*size = offset;
712623c0751SRobert Richter 
7134faf31b4SDan Williams 	return 0;
7144faf31b4SDan Williams }
7154faf31b4SDan Williams 
7164faf31b4SDan Williams /**
7174faf31b4SDan Williams  * cxl_walk_cel() - Walk through the Command Effects Log.
71859f8d151SDan Williams  * @mds: The driver data for the operation
7194faf31b4SDan Williams  * @size: Length of the Command Effects Log.
7204faf31b4SDan Williams  * @cel: CEL
7214faf31b4SDan Williams  *
7224faf31b4SDan Williams  * Iterate over each entry in the CEL and determine if the driver supports the
7234faf31b4SDan Williams  * command. If so, the command is enabled for the device and can be used later.
7244faf31b4SDan Williams  */
cxl_walk_cel(struct cxl_memdev_state * mds,size_t size,u8 * cel)72559f8d151SDan Williams static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
7264faf31b4SDan Williams {
72749be6dd8SDan Williams 	struct cxl_cel_entry *cel_entry;
7284faf31b4SDan Williams 	const int cel_entries = size / sizeof(*cel_entry);
72959f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
7304faf31b4SDan Williams 	int i;
7314faf31b4SDan Williams 
73249be6dd8SDan Williams 	cel_entry = (struct cxl_cel_entry *) cel;
7334faf31b4SDan Williams 
7344faf31b4SDan Williams 	for (i = 0; i < cel_entries; i++) {
7354faf31b4SDan Williams 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
7364faf31b4SDan Williams 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
737d2f70605SIra Weiny 		int enabled = 0;
7384faf31b4SDan Williams 
739d2f70605SIra Weiny 		if (cmd) {
740d2f70605SIra Weiny 			set_bit(cmd->info.id, mds->enabled_cmds);
741d2f70605SIra Weiny 			enabled++;
7424faf31b4SDan Williams 		}
7434faf31b4SDan Williams 
744d2f70605SIra Weiny 		if (cxl_is_poison_command(opcode)) {
74559f8d151SDan Williams 			cxl_set_poison_cmd_enabled(&mds->poison, opcode);
746d2f70605SIra Weiny 			enabled++;
747d2f70605SIra Weiny 		}
748d0abf578SAlison Schofield 
749d2f70605SIra Weiny 		if (cxl_is_security_command(opcode)) {
750ad64f595SDavidlohr Bueso 			cxl_set_security_cmd_enabled(&mds->security, opcode);
751d2f70605SIra Weiny 			enabled++;
752d2f70605SIra Weiny 		}
753ad64f595SDavidlohr Bueso 
754d2f70605SIra Weiny 		dev_dbg(dev, "Opcode 0x%04x %s\n", opcode,
755d2f70605SIra Weiny 			enabled ? "enabled" : "unsupported by driver");
7564faf31b4SDan Williams 	}
7574faf31b4SDan Williams }
7584faf31b4SDan Williams 
cxl_get_gsl(struct cxl_memdev_state * mds)75959f8d151SDan Williams static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_memdev_state *mds)
7604faf31b4SDan Williams {
7618d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
7624faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *ret;
7635331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
7644faf31b4SDan Williams 	int rc;
7654faf31b4SDan Williams 
7668d8081ceSDave Jiang 	ret = kvmalloc(cxl_mbox->payload_size, GFP_KERNEL);
7674faf31b4SDan Williams 	if (!ret)
7684faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
7694faf31b4SDan Williams 
7705331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
7715331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS,
7728d8081ceSDave Jiang 		.size_out = cxl_mbox->payload_size,
7735331cdf4SDan Williams 		.payload_out = ret,
7742aeaf663SDan Williams 		/* At least the record number field must be valid */
7752aeaf663SDan Williams 		.min_out = 2,
7765331cdf4SDan Williams 	};
777b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
7784faf31b4SDan Williams 	if (rc < 0) {
7794faf31b4SDan Williams 		kvfree(ret);
7804faf31b4SDan Williams 		return ERR_PTR(rc);
7814faf31b4SDan Williams 	}
7824faf31b4SDan Williams 
7835331cdf4SDan Williams 
7844faf31b4SDan Williams 	return ret;
7854faf31b4SDan Williams }
7864faf31b4SDan Williams 
7874faf31b4SDan Williams enum {
7884faf31b4SDan Williams 	CEL_UUID,
7894faf31b4SDan Williams 	VENDOR_DEBUG_UUID,
7904faf31b4SDan Williams };
7914faf31b4SDan Williams 
7924faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */
7934faf31b4SDan Williams static const uuid_t log_uuid[] = {
79449be6dd8SDan Williams 	[CEL_UUID] = DEFINE_CXL_CEL_UUID,
79549be6dd8SDan Williams 	[VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID,
7964faf31b4SDan Williams };
7974faf31b4SDan Williams 
7984faf31b4SDan Williams /**
7995e2411aeSIra Weiny  * cxl_enumerate_cmds() - Enumerate commands for a device.
80059f8d151SDan Williams  * @mds: The driver data for the operation
8014faf31b4SDan Williams  *
8024faf31b4SDan Williams  * Returns 0 if enumerate completed successfully.
8034faf31b4SDan Williams  *
8044faf31b4SDan Williams  * CXL devices have optional support for certain commands. This function will
8054faf31b4SDan Williams  * determine the set of supported commands for the hardware and update the
80659f8d151SDan Williams  * enabled_cmds bitmap in the @mds.
8074faf31b4SDan Williams  */
cxl_enumerate_cmds(struct cxl_memdev_state * mds)80859f8d151SDan Williams int cxl_enumerate_cmds(struct cxl_memdev_state *mds)
8094faf31b4SDan Williams {
8104faf31b4SDan Williams 	struct cxl_mbox_get_supported_logs *gsl;
81159f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
8124faf31b4SDan Williams 	struct cxl_mem_command *cmd;
8134faf31b4SDan Williams 	int i, rc;
8144faf31b4SDan Williams 
81559f8d151SDan Williams 	gsl = cxl_get_gsl(mds);
8164faf31b4SDan Williams 	if (IS_ERR(gsl))
8174faf31b4SDan Williams 		return PTR_ERR(gsl);
8184faf31b4SDan Williams 
8194faf31b4SDan Williams 	rc = -ENOENT;
8204faf31b4SDan Williams 	for (i = 0; i < le16_to_cpu(gsl->entries); i++) {
8214faf31b4SDan Williams 		u32 size = le32_to_cpu(gsl->entry[i].size);
8224faf31b4SDan Williams 		uuid_t uuid = gsl->entry[i].uuid;
8234faf31b4SDan Williams 		u8 *log;
8244faf31b4SDan Williams 
8254faf31b4SDan Williams 		dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size);
8264faf31b4SDan Williams 
8274faf31b4SDan Williams 		if (!uuid_equal(&uuid, &log_uuid[CEL_UUID]))
8284faf31b4SDan Williams 			continue;
8294faf31b4SDan Williams 
8304faf31b4SDan Williams 		log = kvmalloc(size, GFP_KERNEL);
8314faf31b4SDan Williams 		if (!log) {
8324faf31b4SDan Williams 			rc = -ENOMEM;
8334faf31b4SDan Williams 			goto out;
8344faf31b4SDan Williams 		}
8354faf31b4SDan Williams 
83659f8d151SDan Williams 		rc = cxl_xfer_log(mds, &uuid, &size, log);
8374faf31b4SDan Williams 		if (rc) {
8384faf31b4SDan Williams 			kvfree(log);
8394faf31b4SDan Williams 			goto out;
8404faf31b4SDan Williams 		}
8414faf31b4SDan Williams 
84259f8d151SDan Williams 		cxl_walk_cel(mds, size, log);
8434faf31b4SDan Williams 		kvfree(log);
8444faf31b4SDan Williams 
8454faf31b4SDan Williams 		/* In case CEL was bogus, enable some default commands. */
8464faf31b4SDan Williams 		cxl_for_each_cmd(cmd)
8474faf31b4SDan Williams 			if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE)
84859f8d151SDan Williams 				set_bit(cmd->info.id, mds->enabled_cmds);
8494faf31b4SDan Williams 
8504faf31b4SDan Williams 		/* Found the required CEL */
8514faf31b4SDan Williams 		rc = 0;
8524faf31b4SDan Williams 	}
8534faf31b4SDan Williams out:
8544faf31b4SDan Williams 	kvfree(gsl);
8554faf31b4SDan Williams 	return rc;
8564faf31b4SDan Williams }
857affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL);
8584faf31b4SDan Williams 
cxl_event_trace_record(const struct cxl_memdev * cxlmd,enum cxl_event_log_type type,enum cxl_event_type event_type,const uuid_t * uuid,union cxl_event * evt)859dc97f634SIra Weiny void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
860dc97f634SIra Weiny 			    enum cxl_event_log_type type,
861dc97f634SIra Weiny 			    enum cxl_event_type event_type,
862dc97f634SIra Weiny 			    const uuid_t *uuid, union cxl_event *evt)
863dc97f634SIra Weiny {
8646aec0013SAlison Schofield 	if (event_type == CXL_CPER_EVENT_MEM_MODULE) {
865dc97f634SIra Weiny 		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
8666aec0013SAlison Schofield 		return;
8676aec0013SAlison Schofield 	}
8686aec0013SAlison Schofield 	if (event_type == CXL_CPER_EVENT_GENERIC) {
869dc97f634SIra Weiny 		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
8706aec0013SAlison Schofield 		return;
8716aec0013SAlison Schofield 	}
8726aec0013SAlison Schofield 
8736aec0013SAlison Schofield 	if (trace_cxl_general_media_enabled() || trace_cxl_dram_enabled()) {
8746aec0013SAlison Schofield 		u64 dpa, hpa = ULLONG_MAX;
8756aec0013SAlison Schofield 		struct cxl_region *cxlr;
8766aec0013SAlison Schofield 
8776aec0013SAlison Schofield 		/*
8786aec0013SAlison Schofield 		 * These trace points are annotated with HPA and region
8796aec0013SAlison Schofield 		 * translations. Take topology mutation locks and lookup
8806aec0013SAlison Schofield 		 * { HPA, REGION } from { DPA, MEMDEV } in the event record.
8816aec0013SAlison Schofield 		 */
8826aec0013SAlison Schofield 		guard(rwsem_read)(&cxl_region_rwsem);
8836aec0013SAlison Schofield 		guard(rwsem_read)(&cxl_dpa_rwsem);
8846aec0013SAlison Schofield 
885675e979dSFabio M. De Francesco 		dpa = le64_to_cpu(evt->media_hdr.phys_addr) & CXL_DPA_MASK;
8866aec0013SAlison Schofield 		cxlr = cxl_dpa_to_region(cxlmd, dpa);
8876aec0013SAlison Schofield 		if (cxlr)
8889aa5f623SAlison Schofield 			hpa = cxl_dpa_to_hpa(cxlr, cxlmd, dpa);
8896aec0013SAlison Schofield 
8906aec0013SAlison Schofield 		if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
8916aec0013SAlison Schofield 			trace_cxl_general_media(cxlmd, type, cxlr, hpa,
8926aec0013SAlison Schofield 						&evt->gen_media);
8936aec0013SAlison Schofield 		else if (event_type == CXL_CPER_EVENT_DRAM)
8946aec0013SAlison Schofield 			trace_cxl_dram(cxlmd, type, cxlr, hpa, &evt->dram);
8956aec0013SAlison Schofield 	}
896dc97f634SIra Weiny }
897dc97f634SIra Weiny EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, CXL);
898d54a531aSIra Weiny 
__cxl_event_trace_record(const struct cxl_memdev * cxlmd,enum cxl_event_log_type type,struct cxl_event_record_raw * record)899dc97f634SIra Weiny static void __cxl_event_trace_record(const struct cxl_memdev *cxlmd,
900d54a531aSIra Weiny 				     enum cxl_event_log_type type,
901d54a531aSIra Weiny 				     struct cxl_event_record_raw *record)
902d54a531aSIra Weiny {
903dc97f634SIra Weiny 	enum cxl_event_type ev_type = CXL_CPER_EVENT_GENERIC;
904dc97f634SIra Weiny 	const uuid_t *uuid = &record->id;
905d54a531aSIra Weiny 
906dc97f634SIra Weiny 	if (uuid_equal(uuid, &CXL_EVENT_GEN_MEDIA_UUID))
907dc97f634SIra Weiny 		ev_type = CXL_CPER_EVENT_GEN_MEDIA;
908dc97f634SIra Weiny 	else if (uuid_equal(uuid, &CXL_EVENT_DRAM_UUID))
909dc97f634SIra Weiny 		ev_type = CXL_CPER_EVENT_DRAM;
910dc97f634SIra Weiny 	else if (uuid_equal(uuid, &CXL_EVENT_MEM_MODULE_UUID))
911dc97f634SIra Weiny 		ev_type = CXL_CPER_EVENT_MEM_MODULE;
912d54a531aSIra Weiny 
913dc97f634SIra Weiny 	cxl_event_trace_record(cxlmd, type, ev_type, uuid, &record->event);
914d54a531aSIra Weiny }
915d54a531aSIra Weiny 
cxl_clear_event_record(struct cxl_memdev_state * mds,enum cxl_event_log_type log,struct cxl_get_event_payload * get_pl)91659f8d151SDan Williams static int cxl_clear_event_record(struct cxl_memdev_state *mds,
9176ebe28f9SIra Weiny 				  enum cxl_event_log_type log,
9186ebe28f9SIra Weiny 				  struct cxl_get_event_payload *get_pl)
9196ebe28f9SIra Weiny {
9208d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
9216ebe28f9SIra Weiny 	struct cxl_mbox_clear_event_payload *payload;
9226ebe28f9SIra Weiny 	u16 total = le16_to_cpu(get_pl->record_count);
9236ebe28f9SIra Weiny 	u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES;
9246ebe28f9SIra Weiny 	size_t pl_size = struct_size(payload, handles, max_handles);
9256ebe28f9SIra Weiny 	struct cxl_mbox_cmd mbox_cmd;
9266ebe28f9SIra Weiny 	u16 cnt;
9276ebe28f9SIra Weiny 	int rc = 0;
9286ebe28f9SIra Weiny 	int i;
9296ebe28f9SIra Weiny 
9306ebe28f9SIra Weiny 	/* Payload size may limit the max handles */
9318d8081ceSDave Jiang 	if (pl_size > cxl_mbox->payload_size) {
9328d8081ceSDave Jiang 		max_handles = (cxl_mbox->payload_size - sizeof(*payload)) /
9336ebe28f9SIra Weiny 			      sizeof(__le16);
9346ebe28f9SIra Weiny 		pl_size = struct_size(payload, handles, max_handles);
9356ebe28f9SIra Weiny 	}
9366ebe28f9SIra Weiny 
9376ebe28f9SIra Weiny 	payload = kvzalloc(pl_size, GFP_KERNEL);
9386ebe28f9SIra Weiny 	if (!payload)
9396ebe28f9SIra Weiny 		return -ENOMEM;
9406ebe28f9SIra Weiny 
9416ebe28f9SIra Weiny 	*payload = (struct cxl_mbox_clear_event_payload) {
9426ebe28f9SIra Weiny 		.event_log = log,
9436ebe28f9SIra Weiny 	};
9446ebe28f9SIra Weiny 
9456ebe28f9SIra Weiny 	mbox_cmd = (struct cxl_mbox_cmd) {
9466ebe28f9SIra Weiny 		.opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD,
9476ebe28f9SIra Weiny 		.payload_in = payload,
9486ebe28f9SIra Weiny 		.size_in = pl_size,
9496ebe28f9SIra Weiny 	};
9506ebe28f9SIra Weiny 
9516ebe28f9SIra Weiny 	/*
9526ebe28f9SIra Weiny 	 * Clear Event Records uses u8 for the handle cnt while Get Event
9536ebe28f9SIra Weiny 	 * Record can return up to 0xffff records.
9546ebe28f9SIra Weiny 	 */
9556ebe28f9SIra Weiny 	i = 0;
9566ebe28f9SIra Weiny 	for (cnt = 0; cnt < total; cnt++) {
957f9c68338SIra Weiny 		struct cxl_event_record_raw *raw = &get_pl->records[cnt];
958f9c68338SIra Weiny 		struct cxl_event_generic *gen = &raw->event.generic;
959f9c68338SIra Weiny 
960f9c68338SIra Weiny 		payload->handles[i++] = gen->hdr.handle;
96159f8d151SDan Williams 		dev_dbg(mds->cxlds.dev, "Event log '%d': Clearing %u\n", log,
962b7c59b03SYuquan Wang 			le16_to_cpu(payload->handles[i - 1]));
9636ebe28f9SIra Weiny 
9646ebe28f9SIra Weiny 		if (i == max_handles) {
9656ebe28f9SIra Weiny 			payload->nr_recs = i;
966b5209da3SDave Jiang 			rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
9676ebe28f9SIra Weiny 			if (rc)
9686ebe28f9SIra Weiny 				goto free_pl;
9696ebe28f9SIra Weiny 			i = 0;
9706ebe28f9SIra Weiny 		}
9716ebe28f9SIra Weiny 	}
9726ebe28f9SIra Weiny 
9736ebe28f9SIra Weiny 	/* Clear what is left if any */
9746ebe28f9SIra Weiny 	if (i) {
9756ebe28f9SIra Weiny 		payload->nr_recs = i;
9766ebe28f9SIra Weiny 		mbox_cmd.size_in = struct_size(payload, handles, i);
977b5209da3SDave Jiang 		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
9786ebe28f9SIra Weiny 		if (rc)
9796ebe28f9SIra Weiny 			goto free_pl;
9806ebe28f9SIra Weiny 	}
9816ebe28f9SIra Weiny 
9826ebe28f9SIra Weiny free_pl:
9836ebe28f9SIra Weiny 	kvfree(payload);
9846ebe28f9SIra Weiny 	return rc;
9856ebe28f9SIra Weiny }
9866ebe28f9SIra Weiny 
cxl_mem_get_records_log(struct cxl_memdev_state * mds,enum cxl_event_log_type type)98759f8d151SDan Williams static void cxl_mem_get_records_log(struct cxl_memdev_state *mds,
9886ebe28f9SIra Weiny 				    enum cxl_event_log_type type)
9896ebe28f9SIra Weiny {
9908d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
99159f8d151SDan Williams 	struct cxl_memdev *cxlmd = mds->cxlds.cxlmd;
99259f8d151SDan Williams 	struct device *dev = mds->cxlds.dev;
9936ebe28f9SIra Weiny 	struct cxl_get_event_payload *payload;
9946ebe28f9SIra Weiny 	u8 log_type = type;
9956ebe28f9SIra Weiny 	u16 nr_rec;
9966ebe28f9SIra Weiny 
99759f8d151SDan Williams 	mutex_lock(&mds->event.log_lock);
99859f8d151SDan Williams 	payload = mds->event.buf;
9996ebe28f9SIra Weiny 
10004b759dd5SDan Williams 	do {
10014b759dd5SDan Williams 		int rc, i;
10024b759dd5SDan Williams 		struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd) {
10036ebe28f9SIra Weiny 			.opcode = CXL_MBOX_OP_GET_EVENT_RECORD,
10046ebe28f9SIra Weiny 			.payload_in = &log_type,
10056ebe28f9SIra Weiny 			.size_in = sizeof(log_type),
10066ebe28f9SIra Weiny 			.payload_out = payload,
10078d8081ceSDave Jiang 			.size_out = cxl_mbox->payload_size,
10086ebe28f9SIra Weiny 			.min_out = struct_size(payload, records, 0),
10096ebe28f9SIra Weiny 		};
10106ebe28f9SIra Weiny 
1011b5209da3SDave Jiang 		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
10126ebe28f9SIra Weiny 		if (rc) {
101359f8d151SDan Williams 			dev_err_ratelimited(dev,
10146ebe28f9SIra Weiny 				"Event log '%d': Failed to query event records : %d",
10156ebe28f9SIra Weiny 				type, rc);
10166ebe28f9SIra Weiny 			break;
10176ebe28f9SIra Weiny 		}
10186ebe28f9SIra Weiny 
10196ebe28f9SIra Weiny 		nr_rec = le16_to_cpu(payload->record_count);
10206ebe28f9SIra Weiny 		if (!nr_rec)
10216ebe28f9SIra Weiny 			break;
10226ebe28f9SIra Weiny 
10236ebe28f9SIra Weiny 		for (i = 0; i < nr_rec; i++)
1024dc97f634SIra Weiny 			__cxl_event_trace_record(cxlmd, type,
10256ebe28f9SIra Weiny 						 &payload->records[i]);
10266ebe28f9SIra Weiny 
10276ebe28f9SIra Weiny 		if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW)
102859f8d151SDan Williams 			trace_cxl_overflow(cxlmd, type, payload);
10296ebe28f9SIra Weiny 
103059f8d151SDan Williams 		rc = cxl_clear_event_record(mds, type, payload);
10316ebe28f9SIra Weiny 		if (rc) {
103259f8d151SDan Williams 			dev_err_ratelimited(dev,
10336ebe28f9SIra Weiny 				"Event log '%d': Failed to clear events : %d",
10346ebe28f9SIra Weiny 				type, rc);
10356ebe28f9SIra Weiny 			break;
10366ebe28f9SIra Weiny 		}
10376ebe28f9SIra Weiny 	} while (nr_rec);
10386ebe28f9SIra Weiny 
103959f8d151SDan Williams 	mutex_unlock(&mds->event.log_lock);
10406ebe28f9SIra Weiny }
10416ebe28f9SIra Weiny 
10426ebe28f9SIra Weiny /**
10436ebe28f9SIra Weiny  * cxl_mem_get_event_records - Get Event Records from the device
104459f8d151SDan Williams  * @mds: The driver data for the operation
10457ebf38c9SJonathan Cameron  * @status: Event Status register value identifying which events are available.
10466ebe28f9SIra Weiny  *
10476ebe28f9SIra Weiny  * Retrieve all event records available on the device, report them as trace
10486ebe28f9SIra Weiny  * events, and clear them.
10496ebe28f9SIra Weiny  *
10506ebe28f9SIra Weiny  * See CXL rev 3.0 @8.2.9.2.2 Get Event Records
10516ebe28f9SIra Weiny  * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records
10526ebe28f9SIra Weiny  */
cxl_mem_get_event_records(struct cxl_memdev_state * mds,u32 status)105359f8d151SDan Williams void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status)
10546ebe28f9SIra Weiny {
105559f8d151SDan Williams 	dev_dbg(mds->cxlds.dev, "Reading event logs: %x\n", status);
10566ebe28f9SIra Weiny 
10576ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_FATAL)
105859f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FATAL);
10596ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_FAIL)
106059f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FAIL);
10616ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_WARN)
106259f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_WARN);
10636ebe28f9SIra Weiny 	if (status & CXLDEV_EVENT_STATUS_INFO)
106459f8d151SDan Williams 		cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_INFO);
10656ebe28f9SIra Weiny }
10666ebe28f9SIra Weiny EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, CXL);
10676ebe28f9SIra Weiny 
10684faf31b4SDan Williams /**
10694faf31b4SDan Williams  * cxl_mem_get_partition_info - Get partition info
107059f8d151SDan Williams  * @mds: The driver data for the operation
10714faf31b4SDan Williams  *
10724faf31b4SDan Williams  * Retrieve the current partition info for the device specified.  The active
10734faf31b4SDan Williams  * values are the current capacity in bytes.  If not 0, the 'next' values are
10744faf31b4SDan Williams  * the pending values, in bytes, which take affect on next cold reset.
10754faf31b4SDan Williams  *
10764faf31b4SDan Williams  * Return: 0 if no error: or the result of the mailbox command.
10774faf31b4SDan Williams  *
10784faf31b4SDan Williams  * See CXL @8.2.9.5.2.1 Get Partition Info
10794faf31b4SDan Williams  */
cxl_mem_get_partition_info(struct cxl_memdev_state * mds)108059f8d151SDan Williams static int cxl_mem_get_partition_info(struct cxl_memdev_state *mds)
10814faf31b4SDan Williams {
1082b5209da3SDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
1083e7ad1bf6SDan Williams 	struct cxl_mbox_get_partition_info pi;
10845331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
10854faf31b4SDan Williams 	int rc;
10864faf31b4SDan Williams 
10875331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
10885331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_GET_PARTITION_INFO,
10895331cdf4SDan Williams 		.size_out = sizeof(pi),
10905331cdf4SDan Williams 		.payload_out = &pi,
10915331cdf4SDan Williams 	};
1092b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
10934faf31b4SDan Williams 	if (rc)
10944faf31b4SDan Williams 		return rc;
10954faf31b4SDan Williams 
109659f8d151SDan Williams 	mds->active_volatile_bytes =
10974faf31b4SDan Williams 		le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
109859f8d151SDan Williams 	mds->active_persistent_bytes =
10994faf31b4SDan Williams 		le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER;
110059f8d151SDan Williams 	mds->next_volatile_bytes =
11014faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
110259f8d151SDan Williams 	mds->next_persistent_bytes =
11034faf31b4SDan Williams 		le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
11044faf31b4SDan Williams 
11054faf31b4SDan Williams 	return 0;
11064faf31b4SDan Williams }
11074faf31b4SDan Williams 
11084faf31b4SDan Williams /**
11095e2411aeSIra Weiny  * cxl_dev_state_identify() - Send the IDENTIFY command to the device.
111059f8d151SDan Williams  * @mds: The driver data for the operation
11114faf31b4SDan Williams  *
1112e764f122SDave Jiang  * Return: 0 if identify was executed successfully or media not ready.
11134faf31b4SDan Williams  *
11144faf31b4SDan Williams  * This will dispatch the identify command to the device and on success populate
11154faf31b4SDan Williams  * structures to be exported to sysfs.
11164faf31b4SDan Williams  */
cxl_dev_state_identify(struct cxl_memdev_state * mds)111759f8d151SDan Williams int cxl_dev_state_identify(struct cxl_memdev_state *mds)
11184faf31b4SDan Williams {
1119b5209da3SDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
11204faf31b4SDan Williams 	/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
112149be6dd8SDan Williams 	struct cxl_mbox_identify id;
11225331cdf4SDan Williams 	struct cxl_mbox_cmd mbox_cmd;
1123ed83f7caSAlison Schofield 	u32 val;
11244faf31b4SDan Williams 	int rc;
11254faf31b4SDan Williams 
112659f8d151SDan Williams 	if (!mds->cxlds.media_ready)
1127e764f122SDave Jiang 		return 0;
1128e764f122SDave Jiang 
11295331cdf4SDan Williams 	mbox_cmd = (struct cxl_mbox_cmd) {
11305331cdf4SDan Williams 		.opcode = CXL_MBOX_OP_IDENTIFY,
11315331cdf4SDan Williams 		.size_out = sizeof(id),
11325331cdf4SDan Williams 		.payload_out = &id,
11335331cdf4SDan Williams 	};
1134b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
11354faf31b4SDan Williams 	if (rc < 0)
11364faf31b4SDan Williams 		return rc;
11374faf31b4SDan Williams 
113859f8d151SDan Williams 	mds->total_bytes =
11394faf31b4SDan Williams 		le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER;
114059f8d151SDan Williams 	mds->volatile_only_bytes =
11414faf31b4SDan Williams 		le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER;
114259f8d151SDan Williams 	mds->persistent_only_bytes =
11434faf31b4SDan Williams 		le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER;
114459f8d151SDan Williams 	mds->partition_align_bytes =
11454faf31b4SDan Williams 		le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER;
11464faf31b4SDan Williams 
114759f8d151SDan Williams 	mds->lsa_size = le32_to_cpu(id.lsa_size);
114859f8d151SDan Williams 	memcpy(mds->firmware_version, id.fw_revision,
114959f8d151SDan Williams 	       sizeof(id.fw_revision));
11504faf31b4SDan Williams 
115159f8d151SDan Williams 	if (test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) {
1152ed83f7caSAlison Schofield 		val = get_unaligned_le24(id.poison_list_max_mer);
115359f8d151SDan Williams 		mds->poison.max_errors = min_t(u32, val, CXL_POISON_LIST_MAX);
1154ed83f7caSAlison Schofield 	}
1155ed83f7caSAlison Schofield 
11564faf31b4SDan Williams 	return 0;
11574faf31b4SDan Williams }
1158affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL);
11594faf31b4SDan Williams 
__cxl_mem_sanitize(struct cxl_memdev_state * mds,u16 cmd)116033981838SDan Williams static int __cxl_mem_sanitize(struct cxl_memdev_state *mds, u16 cmd)
116148dcdbb1SDavidlohr Bueso {
1162b5209da3SDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
116348dcdbb1SDavidlohr Bueso 	int rc;
116448dcdbb1SDavidlohr Bueso 	u32 sec_out = 0;
116548dcdbb1SDavidlohr Bueso 	struct cxl_get_security_output {
116648dcdbb1SDavidlohr Bueso 		__le32 flags;
116748dcdbb1SDavidlohr Bueso 	} out;
116848dcdbb1SDavidlohr Bueso 	struct cxl_mbox_cmd sec_cmd = {
116948dcdbb1SDavidlohr Bueso 		.opcode = CXL_MBOX_OP_GET_SECURITY_STATE,
117048dcdbb1SDavidlohr Bueso 		.payload_out = &out,
117148dcdbb1SDavidlohr Bueso 		.size_out = sizeof(out),
117248dcdbb1SDavidlohr Bueso 	};
117348dcdbb1SDavidlohr Bueso 	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
117448dcdbb1SDavidlohr Bueso 
1175180ffd33SDavidlohr Bueso 	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
117648dcdbb1SDavidlohr Bueso 		return -EINVAL;
117748dcdbb1SDavidlohr Bueso 
1178b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &sec_cmd);
117948dcdbb1SDavidlohr Bueso 	if (rc < 0) {
1180b5209da3SDave Jiang 		dev_err(cxl_mbox->host, "Failed to get security state : %d", rc);
118148dcdbb1SDavidlohr Bueso 		return rc;
118248dcdbb1SDavidlohr Bueso 	}
118348dcdbb1SDavidlohr Bueso 
118448dcdbb1SDavidlohr Bueso 	/*
118548dcdbb1SDavidlohr Bueso 	 * Prior to using these commands, any security applied to
118648dcdbb1SDavidlohr Bueso 	 * the user data areas of the device shall be DISABLED (or
118748dcdbb1SDavidlohr Bueso 	 * UNLOCKED for secure erase case).
118848dcdbb1SDavidlohr Bueso 	 */
118948dcdbb1SDavidlohr Bueso 	sec_out = le32_to_cpu(out.flags);
119048dcdbb1SDavidlohr Bueso 	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
119148dcdbb1SDavidlohr Bueso 		return -EINVAL;
119248dcdbb1SDavidlohr Bueso 
1193180ffd33SDavidlohr Bueso 	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
1194180ffd33SDavidlohr Bueso 	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
1195180ffd33SDavidlohr Bueso 		return -EINVAL;
1196180ffd33SDavidlohr Bueso 
1197b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
119848dcdbb1SDavidlohr Bueso 	if (rc < 0) {
1199b5209da3SDave Jiang 		dev_err(cxl_mbox->host, "Failed to sanitize device : %d", rc);
120048dcdbb1SDavidlohr Bueso 		return rc;
120148dcdbb1SDavidlohr Bueso 	}
120248dcdbb1SDavidlohr Bueso 
120348dcdbb1SDavidlohr Bueso 	return 0;
120448dcdbb1SDavidlohr Bueso }
120533981838SDan Williams 
120633981838SDan Williams 
120733981838SDan Williams /**
120833981838SDan Williams  * cxl_mem_sanitize() - Send a sanitization command to the device.
120933981838SDan Williams  * @cxlmd: The device for the operation
121033981838SDan Williams  * @cmd: The specific sanitization command opcode
121133981838SDan Williams  *
121233981838SDan Williams  * Return: 0 if the command was executed successfully, regardless of
121333981838SDan Williams  * whether or not the actual security operation is done in the background,
121433981838SDan Williams  * such as for the Sanitize case.
121533981838SDan Williams  * Error return values can be the result of the mailbox command, -EINVAL
121633981838SDan Williams  * when security requirements are not met or invalid contexts, or -EBUSY
121733981838SDan Williams  * if the sanitize operation is already in flight.
121833981838SDan Williams  *
121933981838SDan Williams  * See CXL 3.0 @8.2.9.8.5.1 Sanitize and @8.2.9.8.5.2 Secure Erase.
122033981838SDan Williams  */
cxl_mem_sanitize(struct cxl_memdev * cxlmd,u16 cmd)122133981838SDan Williams int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd)
122233981838SDan Williams {
122333981838SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
122433981838SDan Williams 	struct cxl_port  *endpoint;
122533981838SDan Williams 	int rc;
122633981838SDan Williams 
122733981838SDan Williams 	/* synchronize with cxl_mem_probe() and decoder write operations */
12287f569e91SLi Ming 	guard(device)(&cxlmd->dev);
122933981838SDan Williams 	endpoint = cxlmd->endpoint;
123033981838SDan Williams 	down_read(&cxl_region_rwsem);
123133981838SDan Williams 	/*
123233981838SDan Williams 	 * Require an endpoint to be safe otherwise the driver can not
123333981838SDan Williams 	 * be sure that the device is unmapped.
123433981838SDan Williams 	 */
1235458ba818SDave Jiang 	if (endpoint && cxl_num_decoders_committed(endpoint) == 0)
123633981838SDan Williams 		rc = __cxl_mem_sanitize(mds, cmd);
123733981838SDan Williams 	else
123833981838SDan Williams 		rc = -EBUSY;
123933981838SDan Williams 	up_read(&cxl_region_rwsem);
124033981838SDan Williams 
124133981838SDan Williams 	return rc;
124233981838SDan Williams }
124348dcdbb1SDavidlohr Bueso 
add_dpa_res(struct device * dev,struct resource * parent,struct resource * res,resource_size_t start,resource_size_t size,const char * type)1244d3b75029SDan Williams static int add_dpa_res(struct device *dev, struct resource *parent,
1245d3b75029SDan Williams 		       struct resource *res, resource_size_t start,
1246d3b75029SDan Williams 		       resource_size_t size, const char *type)
12474faf31b4SDan Williams {
12484faf31b4SDan Williams 	int rc;
12494faf31b4SDan Williams 
1250d3b75029SDan Williams 	res->name = type;
1251d3b75029SDan Williams 	res->start = start;
1252d3b75029SDan Williams 	res->end = start + size - 1;
1253d3b75029SDan Williams 	res->flags = IORESOURCE_MEM;
1254d3b75029SDan Williams 	if (resource_size(res) == 0) {
1255d3b75029SDan Williams 		dev_dbg(dev, "DPA(%s): no capacity\n", res->name);
12564faf31b4SDan Williams 		return 0;
12574faf31b4SDan Williams 	}
1258d3b75029SDan Williams 	rc = request_resource(parent, res);
1259d3b75029SDan Williams 	if (rc) {
1260d3b75029SDan Williams 		dev_err(dev, "DPA(%s): failed to track %pr (%d)\n", res->name,
1261d3b75029SDan Williams 			res, rc);
1262d3b75029SDan Williams 		return rc;
1263d3b75029SDan Williams 	}
1264d3b75029SDan Williams 
1265d3b75029SDan Williams 	dev_dbg(dev, "DPA(%s): %pr\n", res->name, res);
1266d3b75029SDan Williams 
1267d3b75029SDan Williams 	return 0;
1268d3b75029SDan Williams }
1269d3b75029SDan Williams 
cxl_mem_create_range_info(struct cxl_memdev_state * mds)127059f8d151SDan Williams int cxl_mem_create_range_info(struct cxl_memdev_state *mds)
1271d3b75029SDan Williams {
127259f8d151SDan Williams 	struct cxl_dev_state *cxlds = &mds->cxlds;
1273d3b75029SDan Williams 	struct device *dev = cxlds->dev;
1274d3b75029SDan Williams 	int rc;
1275d3b75029SDan Williams 
1276793a539aSDave Jiang 	if (!cxlds->media_ready) {
1277793a539aSDave Jiang 		cxlds->dpa_res = DEFINE_RES_MEM(0, 0);
1278793a539aSDave Jiang 		cxlds->ram_res = DEFINE_RES_MEM(0, 0);
1279793a539aSDave Jiang 		cxlds->pmem_res = DEFINE_RES_MEM(0, 0);
1280793a539aSDave Jiang 		return 0;
1281793a539aSDave Jiang 	}
1282793a539aSDave Jiang 
12839214c9d5SAlison Schofield 	cxlds->dpa_res = DEFINE_RES_MEM(0, mds->total_bytes);
1284d3b75029SDan Williams 
128559f8d151SDan Williams 	if (mds->partition_align_bytes == 0) {
1286d3b75029SDan Williams 		rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
128759f8d151SDan Williams 				 mds->volatile_only_bytes, "ram");
1288d3b75029SDan Williams 		if (rc)
1289d3b75029SDan Williams 			return rc;
1290d3b75029SDan Williams 		return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
129159f8d151SDan Williams 				   mds->volatile_only_bytes,
129259f8d151SDan Williams 				   mds->persistent_only_bytes, "pmem");
1293d3b75029SDan Williams 	}
12944faf31b4SDan Williams 
129559f8d151SDan Williams 	rc = cxl_mem_get_partition_info(mds);
12964faf31b4SDan Williams 	if (rc) {
1297d3b75029SDan Williams 		dev_err(dev, "Failed to query partition information\n");
12984faf31b4SDan Williams 		return rc;
12994faf31b4SDan Williams 	}
13004faf31b4SDan Williams 
1301d3b75029SDan Williams 	rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
130259f8d151SDan Williams 			 mds->active_volatile_bytes, "ram");
1303d3b75029SDan Williams 	if (rc)
1304d3b75029SDan Williams 		return rc;
1305d3b75029SDan Williams 	return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
130659f8d151SDan Williams 			   mds->active_volatile_bytes,
130759f8d151SDan Williams 			   mds->active_persistent_bytes, "pmem");
13084faf31b4SDan Williams }
1309affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL);
13104faf31b4SDan Williams 
cxl_set_timestamp(struct cxl_memdev_state * mds)131159f8d151SDan Williams int cxl_set_timestamp(struct cxl_memdev_state *mds)
1312fa884345SJonathan Cameron {
1313b5209da3SDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
1314fa884345SJonathan Cameron 	struct cxl_mbox_cmd mbox_cmd;
1315fa884345SJonathan Cameron 	struct cxl_mbox_set_timestamp_in pi;
1316fa884345SJonathan Cameron 	int rc;
1317fa884345SJonathan Cameron 
1318fa884345SJonathan Cameron 	pi.timestamp = cpu_to_le64(ktime_get_real_ns());
1319fa884345SJonathan Cameron 	mbox_cmd = (struct cxl_mbox_cmd) {
1320fa884345SJonathan Cameron 		.opcode = CXL_MBOX_OP_SET_TIMESTAMP,
1321fa884345SJonathan Cameron 		.size_in = sizeof(pi),
1322fa884345SJonathan Cameron 		.payload_in = &pi,
1323fa884345SJonathan Cameron 	};
1324fa884345SJonathan Cameron 
1325b5209da3SDave Jiang 	rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
1326fa884345SJonathan Cameron 	/*
1327fa884345SJonathan Cameron 	 * Command is optional. Devices may have another way of providing
1328fa884345SJonathan Cameron 	 * a timestamp, or may return all 0s in timestamp fields.
1329fa884345SJonathan Cameron 	 * Don't report an error if this command isn't supported
1330fa884345SJonathan Cameron 	 */
1331fa884345SJonathan Cameron 	if (rc && (mbox_cmd.return_code != CXL_MBOX_CMD_RC_UNSUPPORTED))
1332fa884345SJonathan Cameron 		return rc;
1333fa884345SJonathan Cameron 
1334fa884345SJonathan Cameron 	return 0;
1335fa884345SJonathan Cameron }
1336fa884345SJonathan Cameron EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL);
1337fa884345SJonathan Cameron 
cxl_mem_get_poison(struct cxl_memdev * cxlmd,u64 offset,u64 len,struct cxl_region * cxlr)1338ed83f7caSAlison Schofield int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
1339ed83f7caSAlison Schofield 		       struct cxl_region *cxlr)
1340ed83f7caSAlison Schofield {
134159f8d151SDan Williams 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
1342b5209da3SDave Jiang 	struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;
1343ed83f7caSAlison Schofield 	struct cxl_mbox_poison_out *po;
1344ed83f7caSAlison Schofield 	struct cxl_mbox_poison_in pi;
1345ed83f7caSAlison Schofield 	int nr_records = 0;
1346ed83f7caSAlison Schofield 	int rc;
1347ed83f7caSAlison Schofield 
134859f8d151SDan Williams 	rc = mutex_lock_interruptible(&mds->poison.lock);
1349ed83f7caSAlison Schofield 	if (rc)
1350ed83f7caSAlison Schofield 		return rc;
1351ed83f7caSAlison Schofield 
135259f8d151SDan Williams 	po = mds->poison.list_out;
1353ed83f7caSAlison Schofield 	pi.offset = cpu_to_le64(offset);
1354ed83f7caSAlison Schofield 	pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT);
1355ed83f7caSAlison Schofield 
13564b759dd5SDan Williams 	do {
13574b759dd5SDan Williams 		struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd){
1358ed83f7caSAlison Schofield 			.opcode = CXL_MBOX_OP_GET_POISON,
1359ed83f7caSAlison Schofield 			.size_in = sizeof(pi),
1360ed83f7caSAlison Schofield 			.payload_in = &pi,
13618d8081ceSDave Jiang 			.size_out = cxl_mbox->payload_size,
1362ed83f7caSAlison Schofield 			.payload_out = po,
1363ed83f7caSAlison Schofield 			.min_out = struct_size(po, record, 0),
1364ed83f7caSAlison Schofield 		};
1365ed83f7caSAlison Schofield 
1366b5209da3SDave Jiang 		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
1367ed83f7caSAlison Schofield 		if (rc)
1368ed83f7caSAlison Schofield 			break;
1369ed83f7caSAlison Schofield 
1370ddf49d57SAlison Schofield 		for (int i = 0; i < le16_to_cpu(po->count); i++)
1371ddf49d57SAlison Schofield 			trace_cxl_poison(cxlmd, cxlr, &po->record[i],
1372ddf49d57SAlison Schofield 					 po->flags, po->overflow_ts,
1373ddf49d57SAlison Schofield 					 CXL_POISON_TRACE_LIST);
1374ed83f7caSAlison Schofield 
1375ed83f7caSAlison Schofield 		/* Protect against an uncleared _FLAG_MORE */
1376ed83f7caSAlison Schofield 		nr_records = nr_records + le16_to_cpu(po->count);
137759f8d151SDan Williams 		if (nr_records >= mds->poison.max_errors) {
1378ed83f7caSAlison Schofield 			dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n",
1379ed83f7caSAlison Schofield 				nr_records);
1380ed83f7caSAlison Schofield 			break;
1381ed83f7caSAlison Schofield 		}
1382ed83f7caSAlison Schofield 	} while (po->flags & CXL_POISON_FLAG_MORE);
1383ed83f7caSAlison Schofield 
138459f8d151SDan Williams 	mutex_unlock(&mds->poison.lock);
1385ed83f7caSAlison Schofield 	return rc;
1386ed83f7caSAlison Schofield }
1387ed83f7caSAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_mem_get_poison, CXL);
1388ed83f7caSAlison Schofield 
free_poison_buf(void * buf)1389d0abf578SAlison Schofield static void free_poison_buf(void *buf)
1390d0abf578SAlison Schofield {
1391d0abf578SAlison Schofield 	kvfree(buf);
1392d0abf578SAlison Schofield }
1393d0abf578SAlison Schofield 
139459f8d151SDan Williams /* Get Poison List output buffer is protected by mds->poison.lock */
cxl_poison_alloc_buf(struct cxl_memdev_state * mds)139559f8d151SDan Williams static int cxl_poison_alloc_buf(struct cxl_memdev_state *mds)
1396d0abf578SAlison Schofield {
13978d8081ceSDave Jiang 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
13988d8081ceSDave Jiang 
13998d8081ceSDave Jiang 	mds->poison.list_out = kvmalloc(cxl_mbox->payload_size, GFP_KERNEL);
140059f8d151SDan Williams 	if (!mds->poison.list_out)
1401d0abf578SAlison Schofield 		return -ENOMEM;
1402d0abf578SAlison Schofield 
140359f8d151SDan Williams 	return devm_add_action_or_reset(mds->cxlds.dev, free_poison_buf,
140459f8d151SDan Williams 					mds->poison.list_out);
1405d0abf578SAlison Schofield }
1406d0abf578SAlison Schofield 
cxl_poison_state_init(struct cxl_memdev_state * mds)140759f8d151SDan Williams int cxl_poison_state_init(struct cxl_memdev_state *mds)
1408d0abf578SAlison Schofield {
1409d0abf578SAlison Schofield 	int rc;
1410d0abf578SAlison Schofield 
141159f8d151SDan Williams 	if (!test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds))
1412d0abf578SAlison Schofield 		return 0;
1413d0abf578SAlison Schofield 
141459f8d151SDan Williams 	rc = cxl_poison_alloc_buf(mds);
1415d0abf578SAlison Schofield 	if (rc) {
141659f8d151SDan Williams 		clear_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds);
1417d0abf578SAlison Schofield 		return rc;
1418d0abf578SAlison Schofield 	}
1419d0abf578SAlison Schofield 
142059f8d151SDan Williams 	mutex_init(&mds->poison.lock);
1421d0abf578SAlison Schofield 	return 0;
1422d0abf578SAlison Schofield }
1423d0abf578SAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_poison_state_init, CXL);
1424d0abf578SAlison Schofield 
cxl_mailbox_init(struct cxl_mailbox * cxl_mbox,struct device * host)14258d8081ceSDave Jiang int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host)
14268d8081ceSDave Jiang {
14278d8081ceSDave Jiang 	if (!cxl_mbox || !host)
14288d8081ceSDave Jiang 		return -EINVAL;
14298d8081ceSDave Jiang 
14308d8081ceSDave Jiang 	cxl_mbox->host = host;
14318d8081ceSDave Jiang 	mutex_init(&cxl_mbox->mbox_mutex);
14328d8081ceSDave Jiang 	rcuwait_init(&cxl_mbox->mbox_wait);
14338d8081ceSDave Jiang 
14348d8081ceSDave Jiang 	return 0;
14358d8081ceSDave Jiang }
14368d8081ceSDave Jiang EXPORT_SYMBOL_NS_GPL(cxl_mailbox_init, CXL);
14378d8081ceSDave Jiang 
cxl_memdev_state_create(struct device * dev)143859f8d151SDan Williams struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev)
14394faf31b4SDan Williams {
144059f8d151SDan Williams 	struct cxl_memdev_state *mds;
14414faf31b4SDan Williams 
144259f8d151SDan Williams 	mds = devm_kzalloc(dev, sizeof(*mds), GFP_KERNEL);
144359f8d151SDan Williams 	if (!mds) {
14444faf31b4SDan Williams 		dev_err(dev, "No memory available\n");
14454faf31b4SDan Williams 		return ERR_PTR(-ENOMEM);
14464faf31b4SDan Williams 	}
14474faf31b4SDan Williams 
144859f8d151SDan Williams 	mutex_init(&mds->event.log_lock);
144959f8d151SDan Williams 	mds->cxlds.dev = dev;
14502dd18279SRobert Richter 	mds->cxlds.reg_map.host = dev;
14512dd18279SRobert Richter 	mds->cxlds.reg_map.resource = CXL_RESOURCE_NONE;
1452f6b8ab32SDan Williams 	mds->cxlds.type = CXL_DEVTYPE_CLASSMEM;
145300413c15SDave Jiang 	mds->ram_perf.qos_class = CXL_QOS_CLASS_INVALID;
145400413c15SDave Jiang 	mds->pmem_perf.qos_class = CXL_QOS_CLASS_INVALID;
14554faf31b4SDan Williams 
145659f8d151SDan Williams 	return mds;
14574faf31b4SDan Williams }
145859f8d151SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_memdev_state_create, CXL);
14594faf31b4SDan Williams 
cxl_mbox_init(void)14604faf31b4SDan Williams void __init cxl_mbox_init(void)
14614faf31b4SDan Williams {
14624faf31b4SDan Williams 	struct dentry *mbox_debugfs;
14634faf31b4SDan Williams 
14649b99ecf5SDan Williams 	mbox_debugfs = cxl_debugfs_create_dir("mbox");
14654faf31b4SDan Williams 	debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs,
14664faf31b4SDan Williams 			    &cxl_raw_allow_all);
14674faf31b4SDan Williams }
1468