1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
3 #include <linux/security.h>
4 #include <linux/debugfs.h>
5 #include <linux/ktime.h>
6 #include <linux/mutex.h>
7 #include <asm/unaligned.h>
15 static bool cxl_raw_allow_all;
20 * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The
21 * implementation is used by the cxl_pci driver to initialize the device
22 * and implement the cxl_mem.h IOCTL UAPI. It also implements the
23 * backend of the cxl_pmem_ctl() transport for LIBNVDIMM.
26 #define cxl_for_each_cmd(cmd) \
27 for ((cmd) = &cxl_mem_commands[0]; \
28 ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++)
30 #define CXL_CMD(_id, sin, sout, _flags) \
31 [CXL_MEM_COMMAND_ID_##_id] = { \
33 .id = CXL_MEM_COMMAND_ID_##_id, \
37 .opcode = CXL_MBOX_OP_##_id, \
41 #define CXL_VARIABLE_PAYLOAD ~0U
43 * This table defines the supported mailbox commands for the driver. This table
44 * is made up of a UAPI structure. Non-negative values as parameters in the
45 * table will be validated against the user's input. For example, if size_in is
46 * 0, and the user passed in 1, it is an error.
48 static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
49 CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE),
50 #ifdef CONFIG_CXL_MEM_RAW_COMMANDS
51 CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0),
53 CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE),
54 CXL_CMD(GET_FW_INFO, 0, 0x50, 0),
55 CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0),
56 CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0),
57 CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0),
58 CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE),
59 CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0),
60 CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0),
61 CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
62 CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0),
63 CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0),
64 CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0),
65 CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0),
69 * Commands that RAW doesn't permit. The rationale for each:
71 * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment /
72 * coordination of transaction timeout values at the root bridge level.
74 * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live
75 * and needs to be coordinated with HDM updates.
77 * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the
78 * driver and any writes from userspace invalidates those contents.
80 * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes
81 * to the device after it is marked clean, userspace can not make that
84 * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that
85 * is kept up to date with patrol notifications and error management.
87 * CXL_MBOX_OP_[GET_,INJECT_,CLEAR_]POISON: These commands require kernel
88 * driver orchestration for safety.
90 static u16 cxl_disabled_raw_commands[] = {
91 CXL_MBOX_OP_ACTIVATE_FW,
92 CXL_MBOX_OP_SET_PARTITION_INFO,
94 CXL_MBOX_OP_SET_SHUTDOWN_STATE,
95 CXL_MBOX_OP_SCAN_MEDIA,
96 CXL_MBOX_OP_GET_SCAN_MEDIA,
97 CXL_MBOX_OP_GET_POISON,
98 CXL_MBOX_OP_INJECT_POISON,
99 CXL_MBOX_OP_CLEAR_POISON,
103 * Command sets that RAW doesn't permit. All opcodes in this set are
104 * disabled because they pass plain text security payloads over the
105 * user/kernel boundary. This functionality is intended to be wrapped
106 * behind the keys ABI which allows for encrypted payloads in the UAPI
108 static u8 security_command_sets[] = {
110 0x45, /* Persistent Memory Data-at-rest Security */
111 0x46, /* Security Passthrough */
114 static bool cxl_is_security_command(u16 opcode)
118 for (i = 0; i < ARRAY_SIZE(security_command_sets); i++)
119 if (security_command_sets[i] == (opcode >> 8))
124 static void cxl_set_security_cmd_enabled(struct cxl_security_state *security,
128 case CXL_MBOX_OP_SANITIZE:
129 set_bit(CXL_SEC_ENABLED_SANITIZE, security->enabled_cmds);
131 case CXL_MBOX_OP_SECURE_ERASE:
132 set_bit(CXL_SEC_ENABLED_SECURE_ERASE,
133 security->enabled_cmds);
135 case CXL_MBOX_OP_GET_SECURITY_STATE:
136 set_bit(CXL_SEC_ENABLED_GET_SECURITY_STATE,
137 security->enabled_cmds);
139 case CXL_MBOX_OP_SET_PASSPHRASE:
140 set_bit(CXL_SEC_ENABLED_SET_PASSPHRASE,
141 security->enabled_cmds);
143 case CXL_MBOX_OP_DISABLE_PASSPHRASE:
144 set_bit(CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
145 security->enabled_cmds);
147 case CXL_MBOX_OP_UNLOCK:
148 set_bit(CXL_SEC_ENABLED_UNLOCK, security->enabled_cmds);
150 case CXL_MBOX_OP_FREEZE_SECURITY:
151 set_bit(CXL_SEC_ENABLED_FREEZE_SECURITY,
152 security->enabled_cmds);
154 case CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE:
155 set_bit(CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
156 security->enabled_cmds);
163 static bool cxl_is_poison_command(u16 opcode)
165 #define CXL_MBOX_OP_POISON_CMDS 0x43
167 if ((opcode >> 8) == CXL_MBOX_OP_POISON_CMDS)
173 static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison,
177 case CXL_MBOX_OP_GET_POISON:
178 set_bit(CXL_POISON_ENABLED_LIST, poison->enabled_cmds);
180 case CXL_MBOX_OP_INJECT_POISON:
181 set_bit(CXL_POISON_ENABLED_INJECT, poison->enabled_cmds);
183 case CXL_MBOX_OP_CLEAR_POISON:
184 set_bit(CXL_POISON_ENABLED_CLEAR, poison->enabled_cmds);
186 case CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS:
187 set_bit(CXL_POISON_ENABLED_SCAN_CAPS, poison->enabled_cmds);
189 case CXL_MBOX_OP_SCAN_MEDIA:
190 set_bit(CXL_POISON_ENABLED_SCAN_MEDIA, poison->enabled_cmds);
192 case CXL_MBOX_OP_GET_SCAN_MEDIA:
193 set_bit(CXL_POISON_ENABLED_SCAN_RESULTS, poison->enabled_cmds);
200 static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
202 struct cxl_mem_command *c;
205 if (c->opcode == opcode)
211 static const char *cxl_mem_opcode_to_name(u16 opcode)
213 struct cxl_mem_command *c;
215 c = cxl_mem_find_command(opcode);
219 return cxl_command_names[c->info.id].name;
223 * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command
224 * @mds: The driver data for the operation
225 * @mbox_cmd: initialized command to execute
227 * Context: Any context.
229 * * %>=0 - Number of bytes returned in @out.
230 * * %-E2BIG - Payload is too large for hardware.
231 * * %-EBUSY - Couldn't acquire exclusive mailbox access.
232 * * %-EFAULT - Hardware error occurred.
233 * * %-ENXIO - Command completed, but device reported an error.
234 * * %-EIO - Unexpected output size.
236 * Mailbox commands may execute successfully yet the device itself reported an
237 * error. While this distinction can be useful for commands from userspace, the
238 * kernel will only be able to use results when both are successful.
240 int cxl_internal_send_cmd(struct cxl_memdev_state *mds,
241 struct cxl_mbox_cmd *mbox_cmd)
243 size_t out_size, min_out;
246 if (mbox_cmd->size_in > mds->payload_size ||
247 mbox_cmd->size_out > mds->payload_size)
250 out_size = mbox_cmd->size_out;
251 min_out = mbox_cmd->min_out;
252 rc = mds->mbox_send(mds, mbox_cmd);
254 * EIO is reserved for a payload size mismatch and mbox_send()
255 * may not return this error.
257 if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))
262 if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS &&
263 mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND)
264 return cxl_mbox_cmd_rc2errno(mbox_cmd);
270 * Variable sized output needs to at least satisfy the caller's
271 * minimum if not the fully requested size.
276 if (mbox_cmd->size_out < min_out)
280 EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, CXL);
282 static bool cxl_mem_raw_command_allowed(u16 opcode)
286 if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS))
289 if (security_locked_down(LOCKDOWN_PCI_ACCESS))
292 if (cxl_raw_allow_all)
295 if (cxl_is_security_command(opcode))
298 for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++)
299 if (cxl_disabled_raw_commands[i] == opcode)
306 * cxl_payload_from_user_allowed() - Check contents of in_payload.
307 * @opcode: The mailbox command opcode.
308 * @payload_in: Pointer to the input payload passed in from user space.
311 * * true - payload_in passes check for @opcode.
312 * * false - payload_in contains invalid or unsupported values.
314 * The driver may inspect payload contents before sending a mailbox
315 * command from user space to the device. The intent is to reject
316 * commands with input payloads that are known to be unsafe. This
317 * check is not intended to replace the users careful selection of
318 * mailbox command parameters and makes no guarantee that the user
319 * command will succeed, nor that it is appropriate.
321 * The specific checks are determined by the opcode.
323 static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in)
326 case CXL_MBOX_OP_SET_PARTITION_INFO: {
327 struct cxl_mbox_set_partition_info *pi = payload_in;
329 if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG)
339 static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
340 struct cxl_memdev_state *mds, u16 opcode,
341 size_t in_size, size_t out_size, u64 in_payload)
343 *mbox = (struct cxl_mbox_cmd) {
349 mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload),
351 if (IS_ERR(mbox->payload_in))
352 return PTR_ERR(mbox->payload_in);
354 if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) {
355 dev_dbg(mds->cxlds.dev, "%s: input payload not allowed\n",
356 cxl_mem_opcode_to_name(opcode));
357 kvfree(mbox->payload_in);
362 /* Prepare to handle a full payload for variable sized output */
363 if (out_size == CXL_VARIABLE_PAYLOAD)
364 mbox->size_out = mds->payload_size;
366 mbox->size_out = out_size;
368 if (mbox->size_out) {
369 mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL);
370 if (!mbox->payload_out) {
371 kvfree(mbox->payload_in);
378 static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox)
380 kvfree(mbox->payload_in);
381 kvfree(mbox->payload_out);
384 static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,
385 const struct cxl_send_command *send_cmd,
386 struct cxl_memdev_state *mds)
388 if (send_cmd->raw.rsvd)
392 * Unlike supported commands, the output size of RAW commands
393 * gets passed along without further checking, so it must be
396 if (send_cmd->out.size > mds->payload_size)
399 if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode))
402 dev_WARN_ONCE(mds->cxlds.dev, true, "raw command path used\n");
404 *mem_cmd = (struct cxl_mem_command) {
406 .id = CXL_MEM_COMMAND_ID_RAW,
407 .size_in = send_cmd->in.size,
408 .size_out = send_cmd->out.size,
410 .opcode = send_cmd->raw.opcode
416 static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,
417 const struct cxl_send_command *send_cmd,
418 struct cxl_memdev_state *mds)
420 struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id];
421 const struct cxl_command_info *info = &c->info;
423 if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK)
429 if (send_cmd->in.rsvd || send_cmd->out.rsvd)
432 /* Check that the command is enabled for hardware */
433 if (!test_bit(info->id, mds->enabled_cmds))
436 /* Check that the command is not claimed for exclusive kernel use */
437 if (test_bit(info->id, mds->exclusive_cmds))
440 /* Check the input buffer is the expected size */
441 if ((info->size_in != CXL_VARIABLE_PAYLOAD) &&
442 (info->size_in != send_cmd->in.size))
445 /* Check the output buffer is at least large enough */
446 if ((info->size_out != CXL_VARIABLE_PAYLOAD) &&
447 (send_cmd->out.size < info->size_out))
450 *mem_cmd = (struct cxl_mem_command) {
453 .flags = info->flags,
454 .size_in = send_cmd->in.size,
455 .size_out = send_cmd->out.size,
464 * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND.
465 * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd.
466 * @mds: The driver data for the operation
467 * @send_cmd: &struct cxl_send_command copied in from userspace.
470 * * %0 - @out_cmd is ready to send.
471 * * %-ENOTTY - Invalid command specified.
472 * * %-EINVAL - Reserved fields or invalid values were used.
473 * * %-ENOMEM - Input or output buffer wasn't sized properly.
474 * * %-EPERM - Attempted to use a protected command.
475 * * %-EBUSY - Kernel has claimed exclusive access to this opcode
477 * The result of this command is a fully validated command in @mbox_cmd that is
478 * safe to send to the hardware.
480 static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,
481 struct cxl_memdev_state *mds,
482 const struct cxl_send_command *send_cmd)
484 struct cxl_mem_command mem_cmd;
487 if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX)
491 * The user can never specify an input payload larger than what hardware
492 * supports, but output can be arbitrarily large (simply write out as
493 * much data as the hardware provides).
495 if (send_cmd->in.size > mds->payload_size)
498 /* Sanitize and construct a cxl_mem_command */
499 if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW)
500 rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, mds);
502 rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, mds);
507 /* Sanitize and construct a cxl_mbox_cmd */
508 return cxl_mbox_cmd_ctor(mbox_cmd, mds, mem_cmd.opcode,
509 mem_cmd.info.size_in, mem_cmd.info.size_out,
510 send_cmd->in.payload);
513 int cxl_query_cmd(struct cxl_memdev *cxlmd,
514 struct cxl_mem_query_commands __user *q)
516 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
517 struct device *dev = &cxlmd->dev;
518 struct cxl_mem_command *cmd;
522 dev_dbg(dev, "Query IOCTL\n");
524 if (get_user(n_commands, &q->n_commands))
527 /* returns the total number if 0 elements are requested. */
529 return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands);
532 * otherwise, return max(n_commands, total commands) cxl_command_info
535 cxl_for_each_cmd(cmd) {
536 struct cxl_command_info info = cmd->info;
538 if (test_bit(info.id, mds->enabled_cmds))
539 info.flags |= CXL_MEM_COMMAND_FLAG_ENABLED;
540 if (test_bit(info.id, mds->exclusive_cmds))
541 info.flags |= CXL_MEM_COMMAND_FLAG_EXCLUSIVE;
543 if (copy_to_user(&q->commands[j++], &info, sizeof(info)))
554 * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace.
555 * @mds: The driver data for the operation
556 * @mbox_cmd: The validated mailbox command.
557 * @out_payload: Pointer to userspace's output payload.
558 * @size_out: (Input) Max payload size to copy out.
559 * (Output) Payload size hardware generated.
560 * @retval: Hardware generated return code from the operation.
563 * * %0 - Mailbox transaction succeeded. This implies the mailbox
564 * protocol completed successfully not that the operation itself
566 * * %-ENOMEM - Couldn't allocate a bounce buffer.
567 * * %-EFAULT - Something happened with copy_to/from_user.
568 * * %-EINTR - Mailbox acquisition interrupted.
569 * * %-EXXX - Transaction level failures.
571 * Dispatches a mailbox command on behalf of a userspace request.
572 * The output payload is copied to userspace.
574 * See cxl_send_cmd().
576 static int handle_mailbox_cmd_from_user(struct cxl_memdev_state *mds,
577 struct cxl_mbox_cmd *mbox_cmd,
578 u64 out_payload, s32 *size_out,
581 struct device *dev = mds->cxlds.dev;
585 "Submitting %s command for user\n"
588 cxl_mem_opcode_to_name(mbox_cmd->opcode),
589 mbox_cmd->opcode, mbox_cmd->size_in);
591 rc = mds->mbox_send(mds, mbox_cmd);
596 * @size_out contains the max size that's allowed to be written back out
597 * to userspace. While the payload may have written more output than
598 * this it will have to be ignored.
600 if (mbox_cmd->size_out) {
601 dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out,
602 "Invalid return size\n");
603 if (copy_to_user(u64_to_user_ptr(out_payload),
604 mbox_cmd->payload_out, mbox_cmd->size_out)) {
610 *size_out = mbox_cmd->size_out;
611 *retval = mbox_cmd->return_code;
614 cxl_mbox_cmd_dtor(mbox_cmd);
618 int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)
620 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
621 struct device *dev = &cxlmd->dev;
622 struct cxl_send_command send;
623 struct cxl_mbox_cmd mbox_cmd;
626 dev_dbg(dev, "Send IOCTL\n");
628 if (copy_from_user(&send, s, sizeof(send)))
631 rc = cxl_validate_cmd_from_user(&mbox_cmd, mds, &send);
635 rc = handle_mailbox_cmd_from_user(mds, &mbox_cmd, send.out.payload,
636 &send.out.size, &send.retval);
640 if (copy_to_user(s, &send, sizeof(send)))
646 static int cxl_xfer_log(struct cxl_memdev_state *mds, uuid_t *uuid,
649 u32 remaining = *size;
653 u32 xfer_size = min_t(u32, remaining, mds->payload_size);
654 struct cxl_mbox_cmd mbox_cmd;
655 struct cxl_mbox_get_log log;
658 log = (struct cxl_mbox_get_log) {
660 .offset = cpu_to_le32(offset),
661 .length = cpu_to_le32(xfer_size),
664 mbox_cmd = (struct cxl_mbox_cmd) {
665 .opcode = CXL_MBOX_OP_GET_LOG,
666 .size_in = sizeof(log),
668 .size_out = xfer_size,
672 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
675 * The output payload length that indicates the number
676 * of valid bytes can be smaller than the Log buffer
679 if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
680 offset += mbox_cmd.size_out;
688 remaining -= xfer_size;
698 * cxl_walk_cel() - Walk through the Command Effects Log.
699 * @mds: The driver data for the operation
700 * @size: Length of the Command Effects Log.
703 * Iterate over each entry in the CEL and determine if the driver supports the
704 * command. If so, the command is enabled for the device and can be used later.
706 static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
708 struct cxl_cel_entry *cel_entry;
709 const int cel_entries = size / sizeof(*cel_entry);
710 struct device *dev = mds->cxlds.dev;
713 cel_entry = (struct cxl_cel_entry *) cel;
715 for (i = 0; i < cel_entries; i++) {
716 u16 opcode = le16_to_cpu(cel_entry[i].opcode);
717 struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
719 if (!cmd && (!cxl_is_poison_command(opcode) ||
720 !cxl_is_security_command(opcode))) {
722 "Opcode 0x%04x unsupported by driver\n", opcode);
727 set_bit(cmd->info.id, mds->enabled_cmds);
729 if (cxl_is_poison_command(opcode))
730 cxl_set_poison_cmd_enabled(&mds->poison, opcode);
732 if (cxl_is_security_command(opcode))
733 cxl_set_security_cmd_enabled(&mds->security, opcode);
735 dev_dbg(dev, "Opcode 0x%04x enabled\n", opcode);
739 static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_memdev_state *mds)
741 struct cxl_mbox_get_supported_logs *ret;
742 struct cxl_mbox_cmd mbox_cmd;
745 ret = kvmalloc(mds->payload_size, GFP_KERNEL);
747 return ERR_PTR(-ENOMEM);
749 mbox_cmd = (struct cxl_mbox_cmd) {
750 .opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS,
751 .size_out = mds->payload_size,
753 /* At least the record number field must be valid */
756 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
771 /* See CXL 2.0 Table 170. Get Log Input Payload */
772 static const uuid_t log_uuid[] = {
773 [CEL_UUID] = DEFINE_CXL_CEL_UUID,
774 [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID,
778 * cxl_enumerate_cmds() - Enumerate commands for a device.
779 * @mds: The driver data for the operation
781 * Returns 0 if enumerate completed successfully.
783 * CXL devices have optional support for certain commands. This function will
784 * determine the set of supported commands for the hardware and update the
785 * enabled_cmds bitmap in the @mds.
787 int cxl_enumerate_cmds(struct cxl_memdev_state *mds)
789 struct cxl_mbox_get_supported_logs *gsl;
790 struct device *dev = mds->cxlds.dev;
791 struct cxl_mem_command *cmd;
794 gsl = cxl_get_gsl(mds);
799 for (i = 0; i < le16_to_cpu(gsl->entries); i++) {
800 u32 size = le32_to_cpu(gsl->entry[i].size);
801 uuid_t uuid = gsl->entry[i].uuid;
804 dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size);
806 if (!uuid_equal(&uuid, &log_uuid[CEL_UUID]))
809 log = kvmalloc(size, GFP_KERNEL);
815 rc = cxl_xfer_log(mds, &uuid, &size, log);
821 cxl_walk_cel(mds, size, log);
824 /* In case CEL was bogus, enable some default commands. */
825 cxl_for_each_cmd(cmd)
826 if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE)
827 set_bit(cmd->info.id, mds->enabled_cmds);
829 /* Found the required CEL */
836 EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL);
839 * General Media Event Record
840 * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43
842 static const uuid_t gen_media_event_uuid =
843 UUID_INIT(0xfbcd0a77, 0xc260, 0x417f,
844 0x85, 0xa9, 0x08, 0x8b, 0x16, 0x21, 0xeb, 0xa6);
848 * CXL rev 3.0 section 8.2.9.2.1.2; Table 8-44
850 static const uuid_t dram_event_uuid =
851 UUID_INIT(0x601dcbb3, 0x9c06, 0x4eab,
852 0xb8, 0xaf, 0x4e, 0x9b, 0xfb, 0x5c, 0x96, 0x24);
855 * Memory Module Event Record
856 * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45
858 static const uuid_t mem_mod_event_uuid =
859 UUID_INIT(0xfe927475, 0xdd59, 0x4339,
860 0xa5, 0x86, 0x79, 0xba, 0xb1, 0x13, 0xb7, 0x74);
862 static void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
863 enum cxl_event_log_type type,
864 struct cxl_event_record_raw *record)
866 uuid_t *id = &record->hdr.id;
868 if (uuid_equal(id, &gen_media_event_uuid)) {
869 struct cxl_event_gen_media *rec =
870 (struct cxl_event_gen_media *)record;
872 trace_cxl_general_media(cxlmd, type, rec);
873 } else if (uuid_equal(id, &dram_event_uuid)) {
874 struct cxl_event_dram *rec = (struct cxl_event_dram *)record;
876 trace_cxl_dram(cxlmd, type, rec);
877 } else if (uuid_equal(id, &mem_mod_event_uuid)) {
878 struct cxl_event_mem_module *rec =
879 (struct cxl_event_mem_module *)record;
881 trace_cxl_memory_module(cxlmd, type, rec);
883 /* For unknown record types print just the header */
884 trace_cxl_generic_event(cxlmd, type, record);
888 static int cxl_clear_event_record(struct cxl_memdev_state *mds,
889 enum cxl_event_log_type log,
890 struct cxl_get_event_payload *get_pl)
892 struct cxl_mbox_clear_event_payload *payload;
893 u16 total = le16_to_cpu(get_pl->record_count);
894 u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES;
895 size_t pl_size = struct_size(payload, handles, max_handles);
896 struct cxl_mbox_cmd mbox_cmd;
901 /* Payload size may limit the max handles */
902 if (pl_size > mds->payload_size) {
903 max_handles = (mds->payload_size - sizeof(*payload)) /
905 pl_size = struct_size(payload, handles, max_handles);
908 payload = kvzalloc(pl_size, GFP_KERNEL);
912 *payload = (struct cxl_mbox_clear_event_payload) {
916 mbox_cmd = (struct cxl_mbox_cmd) {
917 .opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD,
918 .payload_in = payload,
923 * Clear Event Records uses u8 for the handle cnt while Get Event
924 * Record can return up to 0xffff records.
927 for (cnt = 0; cnt < total; cnt++) {
928 payload->handles[i++] = get_pl->records[cnt].hdr.handle;
929 dev_dbg(mds->cxlds.dev, "Event log '%d': Clearing %u\n", log,
930 le16_to_cpu(payload->handles[i]));
932 if (i == max_handles) {
933 payload->nr_recs = i;
934 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
941 /* Clear what is left if any */
943 payload->nr_recs = i;
944 mbox_cmd.size_in = struct_size(payload, handles, i);
945 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
955 static void cxl_mem_get_records_log(struct cxl_memdev_state *mds,
956 enum cxl_event_log_type type)
958 struct cxl_memdev *cxlmd = mds->cxlds.cxlmd;
959 struct device *dev = mds->cxlds.dev;
960 struct cxl_get_event_payload *payload;
961 struct cxl_mbox_cmd mbox_cmd;
965 mutex_lock(&mds->event.log_lock);
966 payload = mds->event.buf;
968 mbox_cmd = (struct cxl_mbox_cmd) {
969 .opcode = CXL_MBOX_OP_GET_EVENT_RECORD,
970 .payload_in = &log_type,
971 .size_in = sizeof(log_type),
972 .payload_out = payload,
973 .size_out = mds->payload_size,
974 .min_out = struct_size(payload, records, 0),
980 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
982 dev_err_ratelimited(dev,
983 "Event log '%d': Failed to query event records : %d",
988 nr_rec = le16_to_cpu(payload->record_count);
992 for (i = 0; i < nr_rec; i++)
993 cxl_event_trace_record(cxlmd, type,
994 &payload->records[i]);
996 if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW)
997 trace_cxl_overflow(cxlmd, type, payload);
999 rc = cxl_clear_event_record(mds, type, payload);
1001 dev_err_ratelimited(dev,
1002 "Event log '%d': Failed to clear events : %d",
1008 mutex_unlock(&mds->event.log_lock);
1012 * cxl_mem_get_event_records - Get Event Records from the device
1013 * @mds: The driver data for the operation
1014 * @status: Event Status register value identifying which events are available.
1016 * Retrieve all event records available on the device, report them as trace
1017 * events, and clear them.
1019 * See CXL rev 3.0 @8.2.9.2.2 Get Event Records
1020 * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records
1022 void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status)
1024 dev_dbg(mds->cxlds.dev, "Reading event logs: %x\n", status);
1026 if (status & CXLDEV_EVENT_STATUS_FATAL)
1027 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FATAL);
1028 if (status & CXLDEV_EVENT_STATUS_FAIL)
1029 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FAIL);
1030 if (status & CXLDEV_EVENT_STATUS_WARN)
1031 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_WARN);
1032 if (status & CXLDEV_EVENT_STATUS_INFO)
1033 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_INFO);
1035 EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, CXL);
1038 * cxl_mem_get_partition_info - Get partition info
1039 * @mds: The driver data for the operation
1041 * Retrieve the current partition info for the device specified. The active
1042 * values are the current capacity in bytes. If not 0, the 'next' values are
1043 * the pending values, in bytes, which take affect on next cold reset.
1045 * Return: 0 if no error: or the result of the mailbox command.
1047 * See CXL @8.2.9.5.2.1 Get Partition Info
1049 static int cxl_mem_get_partition_info(struct cxl_memdev_state *mds)
1051 struct cxl_mbox_get_partition_info pi;
1052 struct cxl_mbox_cmd mbox_cmd;
1055 mbox_cmd = (struct cxl_mbox_cmd) {
1056 .opcode = CXL_MBOX_OP_GET_PARTITION_INFO,
1057 .size_out = sizeof(pi),
1060 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1064 mds->active_volatile_bytes =
1065 le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
1066 mds->active_persistent_bytes =
1067 le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER;
1068 mds->next_volatile_bytes =
1069 le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
1070 mds->next_persistent_bytes =
1071 le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER;
1077 * cxl_dev_state_identify() - Send the IDENTIFY command to the device.
1078 * @mds: The driver data for the operation
1080 * Return: 0 if identify was executed successfully or media not ready.
1082 * This will dispatch the identify command to the device and on success populate
1083 * structures to be exported to sysfs.
1085 int cxl_dev_state_identify(struct cxl_memdev_state *mds)
1087 /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
1088 struct cxl_mbox_identify id;
1089 struct cxl_mbox_cmd mbox_cmd;
1093 if (!mds->cxlds.media_ready)
1096 mbox_cmd = (struct cxl_mbox_cmd) {
1097 .opcode = CXL_MBOX_OP_IDENTIFY,
1098 .size_out = sizeof(id),
1101 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1106 le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER;
1107 mds->volatile_only_bytes =
1108 le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER;
1109 mds->persistent_only_bytes =
1110 le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER;
1111 mds->partition_align_bytes =
1112 le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER;
1114 mds->lsa_size = le32_to_cpu(id.lsa_size);
1115 memcpy(mds->firmware_version, id.fw_revision,
1116 sizeof(id.fw_revision));
1118 if (test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) {
1119 val = get_unaligned_le24(id.poison_list_max_mer);
1120 mds->poison.max_errors = min_t(u32, val, CXL_POISON_LIST_MAX);
1125 EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL);
1128 * cxl_mem_sanitize() - Send a sanitization command to the device.
1129 * @mds: The device data for the operation
1130 * @cmd: The specific sanitization command opcode
1132 * Return: 0 if the command was executed successfully, regardless of
1133 * whether or not the actual security operation is done in the background,
1134 * such as for the Sanitize case.
1135 * Error return values can be the result of the mailbox command, -EINVAL
1136 * when security requirements are not met or invalid contexts.
1138 * See CXL 3.0 @8.2.9.8.5.1 Sanitize and @8.2.9.8.5.2 Secure Erase.
1140 int cxl_mem_sanitize(struct cxl_memdev_state *mds, u16 cmd)
1144 struct cxl_get_security_output {
1147 struct cxl_mbox_cmd sec_cmd = {
1148 .opcode = CXL_MBOX_OP_GET_SECURITY_STATE,
1149 .payload_out = &out,
1150 .size_out = sizeof(out),
1152 struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
1153 struct cxl_dev_state *cxlds = &mds->cxlds;
1155 if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
1158 rc = cxl_internal_send_cmd(mds, &sec_cmd);
1160 dev_err(cxlds->dev, "Failed to get security state : %d", rc);
1165 * Prior to using these commands, any security applied to
1166 * the user data areas of the device shall be DISABLED (or
1167 * UNLOCKED for secure erase case).
1169 sec_out = le32_to_cpu(out.flags);
1170 if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
1173 if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
1174 sec_out & CXL_PMEM_SEC_STATE_LOCKED)
1177 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1179 dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
1185 EXPORT_SYMBOL_NS_GPL(cxl_mem_sanitize, CXL);
1187 static int add_dpa_res(struct device *dev, struct resource *parent,
1188 struct resource *res, resource_size_t start,
1189 resource_size_t size, const char *type)
1195 res->end = start + size - 1;
1196 res->flags = IORESOURCE_MEM;
1197 if (resource_size(res) == 0) {
1198 dev_dbg(dev, "DPA(%s): no capacity\n", res->name);
1201 rc = request_resource(parent, res);
1203 dev_err(dev, "DPA(%s): failed to track %pr (%d)\n", res->name,
1208 dev_dbg(dev, "DPA(%s): %pr\n", res->name, res);
1213 int cxl_mem_create_range_info(struct cxl_memdev_state *mds)
1215 struct cxl_dev_state *cxlds = &mds->cxlds;
1216 struct device *dev = cxlds->dev;
1219 if (!cxlds->media_ready) {
1220 cxlds->dpa_res = DEFINE_RES_MEM(0, 0);
1221 cxlds->ram_res = DEFINE_RES_MEM(0, 0);
1222 cxlds->pmem_res = DEFINE_RES_MEM(0, 0);
1227 (struct resource)DEFINE_RES_MEM(0, mds->total_bytes);
1229 if (mds->partition_align_bytes == 0) {
1230 rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
1231 mds->volatile_only_bytes, "ram");
1234 return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
1235 mds->volatile_only_bytes,
1236 mds->persistent_only_bytes, "pmem");
1239 rc = cxl_mem_get_partition_info(mds);
1241 dev_err(dev, "Failed to query partition information\n");
1245 rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0,
1246 mds->active_volatile_bytes, "ram");
1249 return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res,
1250 mds->active_volatile_bytes,
1251 mds->active_persistent_bytes, "pmem");
1253 EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL);
1255 int cxl_set_timestamp(struct cxl_memdev_state *mds)
1257 struct cxl_mbox_cmd mbox_cmd;
1258 struct cxl_mbox_set_timestamp_in pi;
1261 pi.timestamp = cpu_to_le64(ktime_get_real_ns());
1262 mbox_cmd = (struct cxl_mbox_cmd) {
1263 .opcode = CXL_MBOX_OP_SET_TIMESTAMP,
1264 .size_in = sizeof(pi),
1268 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1270 * Command is optional. Devices may have another way of providing
1271 * a timestamp, or may return all 0s in timestamp fields.
1272 * Don't report an error if this command isn't supported
1274 if (rc && (mbox_cmd.return_code != CXL_MBOX_CMD_RC_UNSUPPORTED))
1279 EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL);
1281 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
1282 struct cxl_region *cxlr)
1284 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
1285 struct cxl_mbox_poison_out *po;
1286 struct cxl_mbox_poison_in pi;
1287 struct cxl_mbox_cmd mbox_cmd;
1291 rc = mutex_lock_interruptible(&mds->poison.lock);
1295 po = mds->poison.list_out;
1296 pi.offset = cpu_to_le64(offset);
1297 pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT);
1299 mbox_cmd = (struct cxl_mbox_cmd) {
1300 .opcode = CXL_MBOX_OP_GET_POISON,
1301 .size_in = sizeof(pi),
1303 .size_out = mds->payload_size,
1305 .min_out = struct_size(po, record, 0),
1309 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
1313 for (int i = 0; i < le16_to_cpu(po->count); i++)
1314 trace_cxl_poison(cxlmd, cxlr, &po->record[i],
1315 po->flags, po->overflow_ts,
1316 CXL_POISON_TRACE_LIST);
1318 /* Protect against an uncleared _FLAG_MORE */
1319 nr_records = nr_records + le16_to_cpu(po->count);
1320 if (nr_records >= mds->poison.max_errors) {
1321 dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n",
1325 } while (po->flags & CXL_POISON_FLAG_MORE);
1327 mutex_unlock(&mds->poison.lock);
1330 EXPORT_SYMBOL_NS_GPL(cxl_mem_get_poison, CXL);
1332 static void free_poison_buf(void *buf)
1337 /* Get Poison List output buffer is protected by mds->poison.lock */
1338 static int cxl_poison_alloc_buf(struct cxl_memdev_state *mds)
1340 mds->poison.list_out = kvmalloc(mds->payload_size, GFP_KERNEL);
1341 if (!mds->poison.list_out)
1344 return devm_add_action_or_reset(mds->cxlds.dev, free_poison_buf,
1345 mds->poison.list_out);
1348 int cxl_poison_state_init(struct cxl_memdev_state *mds)
1352 if (!test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds))
1355 rc = cxl_poison_alloc_buf(mds);
1357 clear_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds);
1361 mutex_init(&mds->poison.lock);
1364 EXPORT_SYMBOL_NS_GPL(cxl_poison_state_init, CXL);
1366 struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev)
1368 struct cxl_memdev_state *mds;
1370 mds = devm_kzalloc(dev, sizeof(*mds), GFP_KERNEL);
1372 dev_err(dev, "No memory available\n");
1373 return ERR_PTR(-ENOMEM);
1376 mutex_init(&mds->mbox_mutex);
1377 mutex_init(&mds->event.log_lock);
1378 mds->cxlds.dev = dev;
1379 mds->cxlds.type = CXL_DEVTYPE_CLASSMEM;
1383 EXPORT_SYMBOL_NS_GPL(cxl_memdev_state_create, CXL);
1385 void __init cxl_mbox_init(void)
1387 struct dentry *mbox_debugfs;
1389 mbox_debugfs = cxl_debugfs_create_dir("mbox");
1390 debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs,
1391 &cxl_raw_allow_all);