14faf31b4SDan Williams // SPDX-License-Identifier: GPL-2.0-only 24faf31b4SDan Williams /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 34faf31b4SDan Williams #include <linux/security.h> 44faf31b4SDan Williams #include <linux/debugfs.h> 5fa884345SJonathan Cameron #include <linux/ktime.h> 64faf31b4SDan Williams #include <linux/mutex.h> 7ed83f7caSAlison Schofield #include <asm/unaligned.h> 8d0abf578SAlison Schofield #include <cxlpci.h> 94faf31b4SDan Williams #include <cxlmem.h> 104faf31b4SDan Williams #include <cxl.h> 114faf31b4SDan Williams 124faf31b4SDan Williams #include "core.h" 136ebe28f9SIra Weiny #include "trace.h" 144faf31b4SDan Williams 154faf31b4SDan Williams static bool cxl_raw_allow_all; 164faf31b4SDan Williams 174faf31b4SDan Williams /** 184faf31b4SDan Williams * DOC: cxl mbox 194faf31b4SDan Williams * 204faf31b4SDan Williams * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The 214faf31b4SDan Williams * implementation is used by the cxl_pci driver to initialize the device 224faf31b4SDan Williams * and implement the cxl_mem.h IOCTL UAPI. It also implements the 234faf31b4SDan Williams * backend of the cxl_pmem_ctl() transport for LIBNVDIMM. 244faf31b4SDan Williams */ 254faf31b4SDan Williams 264faf31b4SDan Williams #define cxl_for_each_cmd(cmd) \ 274faf31b4SDan Williams for ((cmd) = &cxl_mem_commands[0]; \ 284faf31b4SDan Williams ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++) 294faf31b4SDan Williams 304faf31b4SDan Williams #define CXL_CMD(_id, sin, sout, _flags) \ 314faf31b4SDan Williams [CXL_MEM_COMMAND_ID_##_id] = { \ 324faf31b4SDan Williams .info = { \ 334faf31b4SDan Williams .id = CXL_MEM_COMMAND_ID_##_id, \ 344faf31b4SDan Williams .size_in = sin, \ 354faf31b4SDan Williams .size_out = sout, \ 364faf31b4SDan Williams }, \ 374faf31b4SDan Williams .opcode = CXL_MBOX_OP_##_id, \ 384faf31b4SDan Williams .flags = _flags, \ 394faf31b4SDan Williams } 404faf31b4SDan Williams 4126f89535SAlison Schofield #define CXL_VARIABLE_PAYLOAD ~0U 424faf31b4SDan Williams /* 434faf31b4SDan Williams * This table defines the supported mailbox commands for the driver. This table 444faf31b4SDan Williams * is made up of a UAPI structure. Non-negative values as parameters in the 454faf31b4SDan Williams * table will be validated against the user's input. For example, if size_in is 464faf31b4SDan Williams * 0, and the user passed in 1, it is an error. 474faf31b4SDan Williams */ 484faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = { 494faf31b4SDan Williams CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE), 504faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS 5126f89535SAlison Schofield CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0), 524faf31b4SDan Williams #endif 5326f89535SAlison Schofield CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 544faf31b4SDan Williams CXL_CMD(GET_FW_INFO, 0, 0x50, 0), 554faf31b4SDan Williams CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0), 5626f89535SAlison Schofield CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0), 574faf31b4SDan Williams CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0), 5826f89535SAlison Schofield CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 594faf31b4SDan Williams CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0), 6026f89535SAlison Schofield CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0), 614faf31b4SDan Williams CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0), 624faf31b4SDan Williams CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0), 634faf31b4SDan Williams CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0), 644faf31b4SDan Williams CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0), 654faf31b4SDan Williams CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0), 664faf31b4SDan Williams }; 674faf31b4SDan Williams 684faf31b4SDan Williams /* 694faf31b4SDan Williams * Commands that RAW doesn't permit. The rationale for each: 704faf31b4SDan Williams * 714faf31b4SDan Williams * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / 724faf31b4SDan Williams * coordination of transaction timeout values at the root bridge level. 734faf31b4SDan Williams * 744faf31b4SDan Williams * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live 754faf31b4SDan Williams * and needs to be coordinated with HDM updates. 764faf31b4SDan Williams * 774faf31b4SDan Williams * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the 784faf31b4SDan Williams * driver and any writes from userspace invalidates those contents. 794faf31b4SDan Williams * 804faf31b4SDan Williams * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes 814faf31b4SDan Williams * to the device after it is marked clean, userspace can not make that 824faf31b4SDan Williams * assertion. 834faf31b4SDan Williams * 844faf31b4SDan Williams * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that 854faf31b4SDan Williams * is kept up to date with patrol notifications and error management. 86dec441d3SAlison Schofield * 87dec441d3SAlison Schofield * CXL_MBOX_OP_[GET_,INJECT_,CLEAR_]POISON: These commands require kernel 88dec441d3SAlison Schofield * driver orchestration for safety. 894faf31b4SDan Williams */ 904faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = { 914faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW, 924faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO, 934faf31b4SDan Williams CXL_MBOX_OP_SET_LSA, 944faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE, 954faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA, 964faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA, 97dec441d3SAlison Schofield CXL_MBOX_OP_GET_POISON, 98dec441d3SAlison Schofield CXL_MBOX_OP_INJECT_POISON, 99dec441d3SAlison Schofield CXL_MBOX_OP_CLEAR_POISON, 1004faf31b4SDan Williams }; 1014faf31b4SDan Williams 1024faf31b4SDan Williams /* 1034faf31b4SDan Williams * Command sets that RAW doesn't permit. All opcodes in this set are 1044faf31b4SDan Williams * disabled because they pass plain text security payloads over the 1054faf31b4SDan Williams * user/kernel boundary. This functionality is intended to be wrapped 1064faf31b4SDan Williams * behind the keys ABI which allows for encrypted payloads in the UAPI 1074faf31b4SDan Williams */ 1084faf31b4SDan Williams static u8 security_command_sets[] = { 1094faf31b4SDan Williams 0x44, /* Sanitize */ 1104faf31b4SDan Williams 0x45, /* Persistent Memory Data-at-rest Security */ 1114faf31b4SDan Williams 0x46, /* Security Passthrough */ 1124faf31b4SDan Williams }; 1134faf31b4SDan Williams 1144faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode) 1154faf31b4SDan Williams { 1164faf31b4SDan Williams int i; 1174faf31b4SDan Williams 1184faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(security_command_sets); i++) 1194faf31b4SDan Williams if (security_command_sets[i] == (opcode >> 8)) 1204faf31b4SDan Williams return true; 1214faf31b4SDan Williams return false; 1224faf31b4SDan Williams } 1234faf31b4SDan Williams 124d0abf578SAlison Schofield static bool cxl_is_poison_command(u16 opcode) 125d0abf578SAlison Schofield { 126d0abf578SAlison Schofield #define CXL_MBOX_OP_POISON_CMDS 0x43 127d0abf578SAlison Schofield 128d0abf578SAlison Schofield if ((opcode >> 8) == CXL_MBOX_OP_POISON_CMDS) 129d0abf578SAlison Schofield return true; 130d0abf578SAlison Schofield 131d0abf578SAlison Schofield return false; 132d0abf578SAlison Schofield } 133d0abf578SAlison Schofield 134d0abf578SAlison Schofield static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison, 135d0abf578SAlison Schofield u16 opcode) 136d0abf578SAlison Schofield { 137d0abf578SAlison Schofield switch (opcode) { 138d0abf578SAlison Schofield case CXL_MBOX_OP_GET_POISON: 139d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_LIST, poison->enabled_cmds); 140d0abf578SAlison Schofield break; 141d0abf578SAlison Schofield case CXL_MBOX_OP_INJECT_POISON: 142d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_INJECT, poison->enabled_cmds); 143d0abf578SAlison Schofield break; 144d0abf578SAlison Schofield case CXL_MBOX_OP_CLEAR_POISON: 145d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_CLEAR, poison->enabled_cmds); 146d0abf578SAlison Schofield break; 147d0abf578SAlison Schofield case CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS: 148d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_SCAN_CAPS, poison->enabled_cmds); 149d0abf578SAlison Schofield break; 150d0abf578SAlison Schofield case CXL_MBOX_OP_SCAN_MEDIA: 151d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_SCAN_MEDIA, poison->enabled_cmds); 152d0abf578SAlison Schofield break; 153d0abf578SAlison Schofield case CXL_MBOX_OP_GET_SCAN_MEDIA: 154d0abf578SAlison Schofield set_bit(CXL_POISON_ENABLED_SCAN_RESULTS, poison->enabled_cmds); 155d0abf578SAlison Schofield break; 156d0abf578SAlison Schofield default: 157d0abf578SAlison Schofield break; 158d0abf578SAlison Schofield } 159d0abf578SAlison Schofield } 160d0abf578SAlison Schofield 1614faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) 1624faf31b4SDan Williams { 1634faf31b4SDan Williams struct cxl_mem_command *c; 1644faf31b4SDan Williams 1654faf31b4SDan Williams cxl_for_each_cmd(c) 1664faf31b4SDan Williams if (c->opcode == opcode) 1674faf31b4SDan Williams return c; 1684faf31b4SDan Williams 1694faf31b4SDan Williams return NULL; 1704faf31b4SDan Williams } 1714faf31b4SDan Williams 17282b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode) 17382b8ba29SAlison Schofield { 17482b8ba29SAlison Schofield struct cxl_mem_command *c; 17582b8ba29SAlison Schofield 17682b8ba29SAlison Schofield c = cxl_mem_find_command(opcode); 17782b8ba29SAlison Schofield if (!c) 17882b8ba29SAlison Schofield return NULL; 17982b8ba29SAlison Schofield 18082b8ba29SAlison Schofield return cxl_command_names[c->info.id].name; 18182b8ba29SAlison Schofield } 18282b8ba29SAlison Schofield 1834faf31b4SDan Williams /** 1845331cdf4SDan Williams * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command 1855e2411aeSIra Weiny * @cxlds: The device data for the operation 1865331cdf4SDan Williams * @mbox_cmd: initialized command to execute 1874faf31b4SDan Williams * 188ee92c7e2SDavidlohr Bueso * Context: Any context. 1894faf31b4SDan Williams * Return: 1904faf31b4SDan Williams * * %>=0 - Number of bytes returned in @out. 1914faf31b4SDan Williams * * %-E2BIG - Payload is too large for hardware. 1924faf31b4SDan Williams * * %-EBUSY - Couldn't acquire exclusive mailbox access. 1934faf31b4SDan Williams * * %-EFAULT - Hardware error occurred. 1944faf31b4SDan Williams * * %-ENXIO - Command completed, but device reported an error. 1954faf31b4SDan Williams * * %-EIO - Unexpected output size. 1964faf31b4SDan Williams * 1974faf31b4SDan Williams * Mailbox commands may execute successfully yet the device itself reported an 1984faf31b4SDan Williams * error. While this distinction can be useful for commands from userspace, the 1994faf31b4SDan Williams * kernel will only be able to use results when both are successful. 2004faf31b4SDan Williams */ 2015331cdf4SDan Williams int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, 2025331cdf4SDan Williams struct cxl_mbox_cmd *mbox_cmd) 2034faf31b4SDan Williams { 2042aeaf663SDan Williams size_t out_size, min_out; 2054faf31b4SDan Williams int rc; 2064faf31b4SDan Williams 2075331cdf4SDan Williams if (mbox_cmd->size_in > cxlds->payload_size || 2085331cdf4SDan Williams mbox_cmd->size_out > cxlds->payload_size) 2094faf31b4SDan Williams return -E2BIG; 2104faf31b4SDan Williams 2115331cdf4SDan Williams out_size = mbox_cmd->size_out; 2122aeaf663SDan Williams min_out = mbox_cmd->min_out; 2135331cdf4SDan Williams rc = cxlds->mbox_send(cxlds, mbox_cmd); 214623c0751SRobert Richter /* 215623c0751SRobert Richter * EIO is reserved for a payload size mismatch and mbox_send() 216623c0751SRobert Richter * may not return this error. 217623c0751SRobert Richter */ 218623c0751SRobert Richter if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO")) 219623c0751SRobert Richter return -ENXIO; 2204faf31b4SDan Williams if (rc) 2214faf31b4SDan Williams return rc; 2224faf31b4SDan Williams 223ccadf131SDavidlohr Bueso if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS && 224ccadf131SDavidlohr Bueso mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND) 2255331cdf4SDan Williams return cxl_mbox_cmd_rc2errno(mbox_cmd); 2264faf31b4SDan Williams 2272aeaf663SDan Williams if (!out_size) 2282aeaf663SDan Williams return 0; 2292aeaf663SDan Williams 2304faf31b4SDan Williams /* 2312aeaf663SDan Williams * Variable sized output needs to at least satisfy the caller's 2322aeaf663SDan Williams * minimum if not the fully requested size. 2334faf31b4SDan Williams */ 2342aeaf663SDan Williams if (min_out == 0) 2352aeaf663SDan Williams min_out = out_size; 2362aeaf663SDan Williams 2372aeaf663SDan Williams if (mbox_cmd->size_out < min_out) 2384faf31b4SDan Williams return -EIO; 2394faf31b4SDan Williams return 0; 2404faf31b4SDan Williams } 2415331cdf4SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, CXL); 2424faf31b4SDan Williams 2434faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode) 2444faf31b4SDan Williams { 2454faf31b4SDan Williams int i; 2464faf31b4SDan Williams 2474faf31b4SDan Williams if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS)) 2484faf31b4SDan Williams return false; 2494faf31b4SDan Williams 2504faf31b4SDan Williams if (security_locked_down(LOCKDOWN_PCI_ACCESS)) 2514faf31b4SDan Williams return false; 2524faf31b4SDan Williams 2534faf31b4SDan Williams if (cxl_raw_allow_all) 2544faf31b4SDan Williams return true; 2554faf31b4SDan Williams 2564faf31b4SDan Williams if (cxl_is_security_command(opcode)) 2574faf31b4SDan Williams return false; 2584faf31b4SDan Williams 2594faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++) 2604faf31b4SDan Williams if (cxl_disabled_raw_commands[i] == opcode) 2614faf31b4SDan Williams return false; 2624faf31b4SDan Williams 2634faf31b4SDan Williams return true; 2644faf31b4SDan Williams } 2654faf31b4SDan Williams 2666179045cSAlison Schofield /** 2676179045cSAlison Schofield * cxl_payload_from_user_allowed() - Check contents of in_payload. 2686179045cSAlison Schofield * @opcode: The mailbox command opcode. 2696179045cSAlison Schofield * @payload_in: Pointer to the input payload passed in from user space. 2706179045cSAlison Schofield * 2716179045cSAlison Schofield * Return: 2726179045cSAlison Schofield * * true - payload_in passes check for @opcode. 2736179045cSAlison Schofield * * false - payload_in contains invalid or unsupported values. 2746179045cSAlison Schofield * 2756179045cSAlison Schofield * The driver may inspect payload contents before sending a mailbox 2766179045cSAlison Schofield * command from user space to the device. The intent is to reject 2776179045cSAlison Schofield * commands with input payloads that are known to be unsafe. This 2786179045cSAlison Schofield * check is not intended to replace the users careful selection of 2796179045cSAlison Schofield * mailbox command parameters and makes no guarantee that the user 2806179045cSAlison Schofield * command will succeed, nor that it is appropriate. 2816179045cSAlison Schofield * 2826179045cSAlison Schofield * The specific checks are determined by the opcode. 2836179045cSAlison Schofield */ 2846179045cSAlison Schofield static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in) 2856179045cSAlison Schofield { 2866179045cSAlison Schofield switch (opcode) { 2876179045cSAlison Schofield case CXL_MBOX_OP_SET_PARTITION_INFO: { 2886179045cSAlison Schofield struct cxl_mbox_set_partition_info *pi = payload_in; 2896179045cSAlison Schofield 29035e01667SDan Carpenter if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG) 2916179045cSAlison Schofield return false; 2926179045cSAlison Schofield break; 2936179045cSAlison Schofield } 2946179045cSAlison Schofield default: 2956179045cSAlison Schofield break; 2966179045cSAlison Schofield } 2976179045cSAlison Schofield return true; 2986179045cSAlison Schofield } 2996179045cSAlison Schofield 30063cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, 30163cf60b7SAlison Schofield struct cxl_dev_state *cxlds, u16 opcode, 30263cf60b7SAlison Schofield size_t in_size, size_t out_size, u64 in_payload) 30363cf60b7SAlison Schofield { 30463cf60b7SAlison Schofield *mbox = (struct cxl_mbox_cmd) { 30563cf60b7SAlison Schofield .opcode = opcode, 30663cf60b7SAlison Schofield .size_in = in_size, 30763cf60b7SAlison Schofield }; 30863cf60b7SAlison Schofield 30963cf60b7SAlison Schofield if (in_size) { 31063cf60b7SAlison Schofield mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload), 31163cf60b7SAlison Schofield in_size); 312280302f0SAlison Schofield if (IS_ERR(mbox->payload_in)) 31363cf60b7SAlison Schofield return PTR_ERR(mbox->payload_in); 3146179045cSAlison Schofield 3156179045cSAlison Schofield if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) { 3166179045cSAlison Schofield dev_dbg(cxlds->dev, "%s: input payload not allowed\n", 3176179045cSAlison Schofield cxl_mem_opcode_to_name(opcode)); 3186179045cSAlison Schofield kvfree(mbox->payload_in); 3196179045cSAlison Schofield return -EBUSY; 3206179045cSAlison Schofield } 32163cf60b7SAlison Schofield } 32263cf60b7SAlison Schofield 32363cf60b7SAlison Schofield /* Prepare to handle a full payload for variable sized output */ 32426f89535SAlison Schofield if (out_size == CXL_VARIABLE_PAYLOAD) 32563cf60b7SAlison Schofield mbox->size_out = cxlds->payload_size; 32663cf60b7SAlison Schofield else 32763cf60b7SAlison Schofield mbox->size_out = out_size; 32863cf60b7SAlison Schofield 32963cf60b7SAlison Schofield if (mbox->size_out) { 33063cf60b7SAlison Schofield mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL); 33163cf60b7SAlison Schofield if (!mbox->payload_out) { 33263cf60b7SAlison Schofield kvfree(mbox->payload_in); 33363cf60b7SAlison Schofield return -ENOMEM; 33463cf60b7SAlison Schofield } 33563cf60b7SAlison Schofield } 33663cf60b7SAlison Schofield return 0; 33763cf60b7SAlison Schofield } 33863cf60b7SAlison Schofield 33963cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox) 34063cf60b7SAlison Schofield { 34163cf60b7SAlison Schofield kvfree(mbox->payload_in); 34263cf60b7SAlison Schofield kvfree(mbox->payload_out); 34363cf60b7SAlison Schofield } 34463cf60b7SAlison Schofield 3456dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, 3466dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3476dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3486dd0e5ccSAlison Schofield { 3496dd0e5ccSAlison Schofield if (send_cmd->raw.rsvd) 3506dd0e5ccSAlison Schofield return -EINVAL; 3516dd0e5ccSAlison Schofield 3526dd0e5ccSAlison Schofield /* 3536dd0e5ccSAlison Schofield * Unlike supported commands, the output size of RAW commands 3546dd0e5ccSAlison Schofield * gets passed along without further checking, so it must be 3556dd0e5ccSAlison Schofield * validated here. 3566dd0e5ccSAlison Schofield */ 3576dd0e5ccSAlison Schofield if (send_cmd->out.size > cxlds->payload_size) 3586dd0e5ccSAlison Schofield return -EINVAL; 3596dd0e5ccSAlison Schofield 3606dd0e5ccSAlison Schofield if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode)) 3616dd0e5ccSAlison Schofield return -EPERM; 3626dd0e5ccSAlison Schofield 36339ed8da4SAlison Schofield dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n"); 36439ed8da4SAlison Schofield 3656dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3666dd0e5ccSAlison Schofield .info = { 3676dd0e5ccSAlison Schofield .id = CXL_MEM_COMMAND_ID_RAW, 3686dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3696dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3706dd0e5ccSAlison Schofield }, 3716dd0e5ccSAlison Schofield .opcode = send_cmd->raw.opcode 3726dd0e5ccSAlison Schofield }; 3736dd0e5ccSAlison Schofield 3746dd0e5ccSAlison Schofield return 0; 3756dd0e5ccSAlison Schofield } 3766dd0e5ccSAlison Schofield 3776dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, 3786dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3796dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3806dd0e5ccSAlison Schofield { 3816dd0e5ccSAlison Schofield struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; 3826dd0e5ccSAlison Schofield const struct cxl_command_info *info = &c->info; 3836dd0e5ccSAlison Schofield 3846dd0e5ccSAlison Schofield if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK) 3856dd0e5ccSAlison Schofield return -EINVAL; 3866dd0e5ccSAlison Schofield 3876dd0e5ccSAlison Schofield if (send_cmd->rsvd) 3886dd0e5ccSAlison Schofield return -EINVAL; 3896dd0e5ccSAlison Schofield 3906dd0e5ccSAlison Schofield if (send_cmd->in.rsvd || send_cmd->out.rsvd) 3916dd0e5ccSAlison Schofield return -EINVAL; 3926dd0e5ccSAlison Schofield 3936dd0e5ccSAlison Schofield /* Check that the command is enabled for hardware */ 3946dd0e5ccSAlison Schofield if (!test_bit(info->id, cxlds->enabled_cmds)) 3956dd0e5ccSAlison Schofield return -ENOTTY; 3966dd0e5ccSAlison Schofield 3976dd0e5ccSAlison Schofield /* Check that the command is not claimed for exclusive kernel use */ 3986dd0e5ccSAlison Schofield if (test_bit(info->id, cxlds->exclusive_cmds)) 3996dd0e5ccSAlison Schofield return -EBUSY; 4006dd0e5ccSAlison Schofield 4016dd0e5ccSAlison Schofield /* Check the input buffer is the expected size */ 402e35f5718SVishal Verma if ((info->size_in != CXL_VARIABLE_PAYLOAD) && 403e35f5718SVishal Verma (info->size_in != send_cmd->in.size)) 4046dd0e5ccSAlison Schofield return -ENOMEM; 4056dd0e5ccSAlison Schofield 4066dd0e5ccSAlison Schofield /* Check the output buffer is at least large enough */ 407e35f5718SVishal Verma if ((info->size_out != CXL_VARIABLE_PAYLOAD) && 408e35f5718SVishal Verma (send_cmd->out.size < info->size_out)) 4096dd0e5ccSAlison Schofield return -ENOMEM; 4106dd0e5ccSAlison Schofield 4116dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 4126dd0e5ccSAlison Schofield .info = { 4136dd0e5ccSAlison Schofield .id = info->id, 4146dd0e5ccSAlison Schofield .flags = info->flags, 4156dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 4166dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 4176dd0e5ccSAlison Schofield }, 4186dd0e5ccSAlison Schofield .opcode = c->opcode 4196dd0e5ccSAlison Schofield }; 4206dd0e5ccSAlison Schofield 4216dd0e5ccSAlison Schofield return 0; 4226dd0e5ccSAlison Schofield } 4236dd0e5ccSAlison Schofield 4244faf31b4SDan Williams /** 4254faf31b4SDan Williams * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. 4269ae016aeSAlison Schofield * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. 4275e2411aeSIra Weiny * @cxlds: The device data for the operation 4284faf31b4SDan Williams * @send_cmd: &struct cxl_send_command copied in from userspace. 4294faf31b4SDan Williams * 4304faf31b4SDan Williams * Return: 4314faf31b4SDan Williams * * %0 - @out_cmd is ready to send. 4324faf31b4SDan Williams * * %-ENOTTY - Invalid command specified. 4334faf31b4SDan Williams * * %-EINVAL - Reserved fields or invalid values were used. 4344faf31b4SDan Williams * * %-ENOMEM - Input or output buffer wasn't sized properly. 4354faf31b4SDan Williams * * %-EPERM - Attempted to use a protected command. 43612f3856aSDan Williams * * %-EBUSY - Kernel has claimed exclusive access to this opcode 4374faf31b4SDan Williams * 4382dd5600aSAlison Schofield * The result of this command is a fully validated command in @mbox_cmd that is 4394faf31b4SDan Williams * safe to send to the hardware. 4404faf31b4SDan Williams */ 4419ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd, 4429ae016aeSAlison Schofield struct cxl_dev_state *cxlds, 4432dd5600aSAlison Schofield const struct cxl_send_command *send_cmd) 4444faf31b4SDan Williams { 4452dd5600aSAlison Schofield struct cxl_mem_command mem_cmd; 4469ae016aeSAlison Schofield int rc; 4479ae016aeSAlison Schofield 4484faf31b4SDan Williams if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) 4494faf31b4SDan Williams return -ENOTTY; 4504faf31b4SDan Williams 4514faf31b4SDan Williams /* 4524faf31b4SDan Williams * The user can never specify an input payload larger than what hardware 4534faf31b4SDan Williams * supports, but output can be arbitrarily large (simply write out as 4544faf31b4SDan Williams * much data as the hardware provides). 4554faf31b4SDan Williams */ 4565e2411aeSIra Weiny if (send_cmd->in.size > cxlds->payload_size) 4574faf31b4SDan Williams return -EINVAL; 4584faf31b4SDan Williams 4596dd0e5ccSAlison Schofield /* Sanitize and construct a cxl_mem_command */ 4606dd0e5ccSAlison Schofield if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW) 4612dd5600aSAlison Schofield rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, cxlds); 4626dd0e5ccSAlison Schofield else 4632dd5600aSAlison Schofield rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, cxlds); 4649ae016aeSAlison Schofield 4659ae016aeSAlison Schofield if (rc) 4669ae016aeSAlison Schofield return rc; 4679ae016aeSAlison Schofield 4689ae016aeSAlison Schofield /* Sanitize and construct a cxl_mbox_cmd */ 4692dd5600aSAlison Schofield return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, mem_cmd.opcode, 4702dd5600aSAlison Schofield mem_cmd.info.size_in, mem_cmd.info.size_out, 4719ae016aeSAlison Schofield send_cmd->in.payload); 4724faf31b4SDan Williams } 4734faf31b4SDan Williams 4744faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd, 4754faf31b4SDan Williams struct cxl_mem_query_commands __user *q) 4764faf31b4SDan Williams { 4774faf31b4SDan Williams struct device *dev = &cxlmd->dev; 4784faf31b4SDan Williams struct cxl_mem_command *cmd; 4794faf31b4SDan Williams u32 n_commands; 4804faf31b4SDan Williams int j = 0; 4814faf31b4SDan Williams 4824faf31b4SDan Williams dev_dbg(dev, "Query IOCTL\n"); 4834faf31b4SDan Williams 4844faf31b4SDan Williams if (get_user(n_commands, &q->n_commands)) 4854faf31b4SDan Williams return -EFAULT; 4864faf31b4SDan Williams 4874faf31b4SDan Williams /* returns the total number if 0 elements are requested. */ 4884faf31b4SDan Williams if (n_commands == 0) 489ff56ab9eSDan Williams return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands); 4904faf31b4SDan Williams 4914faf31b4SDan Williams /* 4924faf31b4SDan Williams * otherwise, return max(n_commands, total commands) cxl_command_info 4934faf31b4SDan Williams * structures. 4944faf31b4SDan Williams */ 4954faf31b4SDan Williams cxl_for_each_cmd(cmd) { 496814a15f3SIra Weiny struct cxl_command_info info = cmd->info; 4974faf31b4SDan Williams 498814a15f3SIra Weiny if (test_bit(info.id, cxlmd->cxlds->enabled_cmds)) 499814a15f3SIra Weiny info.flags |= CXL_MEM_COMMAND_FLAG_ENABLED; 500814a15f3SIra Weiny if (test_bit(info.id, cxlmd->cxlds->exclusive_cmds)) 501814a15f3SIra Weiny info.flags |= CXL_MEM_COMMAND_FLAG_EXCLUSIVE; 502814a15f3SIra Weiny 503814a15f3SIra Weiny if (copy_to_user(&q->commands[j++], &info, sizeof(info))) 5044faf31b4SDan Williams return -EFAULT; 5054faf31b4SDan Williams 5064faf31b4SDan Williams if (j == n_commands) 5074faf31b4SDan Williams break; 5084faf31b4SDan Williams } 5094faf31b4SDan Williams 5104faf31b4SDan Williams return 0; 5114faf31b4SDan Williams } 5124faf31b4SDan Williams 5134faf31b4SDan Williams /** 5144faf31b4SDan Williams * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. 5155e2411aeSIra Weiny * @cxlds: The device data for the operation 516d97fe8eeSAlison Schofield * @mbox_cmd: The validated mailbox command. 5174faf31b4SDan Williams * @out_payload: Pointer to userspace's output payload. 5184faf31b4SDan Williams * @size_out: (Input) Max payload size to copy out. 5194faf31b4SDan Williams * (Output) Payload size hardware generated. 5204faf31b4SDan Williams * @retval: Hardware generated return code from the operation. 5214faf31b4SDan Williams * 5224faf31b4SDan Williams * Return: 5234faf31b4SDan Williams * * %0 - Mailbox transaction succeeded. This implies the mailbox 5244faf31b4SDan Williams * protocol completed successfully not that the operation itself 5254faf31b4SDan Williams * was successful. 5264faf31b4SDan Williams * * %-ENOMEM - Couldn't allocate a bounce buffer. 5274faf31b4SDan Williams * * %-EFAULT - Something happened with copy_to/from_user. 5284faf31b4SDan Williams * * %-EINTR - Mailbox acquisition interrupted. 5294faf31b4SDan Williams * * %-EXXX - Transaction level failures. 5304faf31b4SDan Williams * 531d97fe8eeSAlison Schofield * Dispatches a mailbox command on behalf of a userspace request. 532d97fe8eeSAlison Schofield * The output payload is copied to userspace. 5334faf31b4SDan Williams * 5344faf31b4SDan Williams * See cxl_send_cmd(). 5354faf31b4SDan Williams */ 5365e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, 537d97fe8eeSAlison Schofield struct cxl_mbox_cmd *mbox_cmd, 538d97fe8eeSAlison Schofield u64 out_payload, s32 *size_out, 539d97fe8eeSAlison Schofield u32 *retval) 5404faf31b4SDan Williams { 5415e2411aeSIra Weiny struct device *dev = cxlds->dev; 5424faf31b4SDan Williams int rc; 5434faf31b4SDan Williams 5444faf31b4SDan Williams dev_dbg(dev, 5454faf31b4SDan Williams "Submitting %s command for user\n" 5464faf31b4SDan Williams "\topcode: %x\n" 54782b8ba29SAlison Schofield "\tsize: %zx\n", 548d97fe8eeSAlison Schofield cxl_mem_opcode_to_name(mbox_cmd->opcode), 549d97fe8eeSAlison Schofield mbox_cmd->opcode, mbox_cmd->size_in); 5504faf31b4SDan Williams 551d97fe8eeSAlison Schofield rc = cxlds->mbox_send(cxlds, mbox_cmd); 5524faf31b4SDan Williams if (rc) 5534faf31b4SDan Williams goto out; 5544faf31b4SDan Williams 5554faf31b4SDan Williams /* 5564faf31b4SDan Williams * @size_out contains the max size that's allowed to be written back out 5574faf31b4SDan Williams * to userspace. While the payload may have written more output than 5584faf31b4SDan Williams * this it will have to be ignored. 5594faf31b4SDan Williams */ 560d97fe8eeSAlison Schofield if (mbox_cmd->size_out) { 561d97fe8eeSAlison Schofield dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out, 5624faf31b4SDan Williams "Invalid return size\n"); 5634faf31b4SDan Williams if (copy_to_user(u64_to_user_ptr(out_payload), 564d97fe8eeSAlison Schofield mbox_cmd->payload_out, mbox_cmd->size_out)) { 5654faf31b4SDan Williams rc = -EFAULT; 5664faf31b4SDan Williams goto out; 5674faf31b4SDan Williams } 5684faf31b4SDan Williams } 5694faf31b4SDan Williams 570d97fe8eeSAlison Schofield *size_out = mbox_cmd->size_out; 571d97fe8eeSAlison Schofield *retval = mbox_cmd->return_code; 5724faf31b4SDan Williams 5734faf31b4SDan Williams out: 574d97fe8eeSAlison Schofield cxl_mbox_cmd_dtor(mbox_cmd); 5754faf31b4SDan Williams return rc; 5764faf31b4SDan Williams } 5774faf31b4SDan Williams 5784faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s) 5794faf31b4SDan Williams { 5805e2411aeSIra Weiny struct cxl_dev_state *cxlds = cxlmd->cxlds; 5814faf31b4SDan Williams struct device *dev = &cxlmd->dev; 5824faf31b4SDan Williams struct cxl_send_command send; 5839ae016aeSAlison Schofield struct cxl_mbox_cmd mbox_cmd; 5844faf31b4SDan Williams int rc; 5854faf31b4SDan Williams 5864faf31b4SDan Williams dev_dbg(dev, "Send IOCTL\n"); 5874faf31b4SDan Williams 5884faf31b4SDan Williams if (copy_from_user(&send, s, sizeof(send))) 5894faf31b4SDan Williams return -EFAULT; 5904faf31b4SDan Williams 5912dd5600aSAlison Schofield rc = cxl_validate_cmd_from_user(&mbox_cmd, cxlmd->cxlds, &send); 5924faf31b4SDan Williams if (rc) 5934faf31b4SDan Williams return rc; 5944faf31b4SDan Williams 595d97fe8eeSAlison Schofield rc = handle_mailbox_cmd_from_user(cxlds, &mbox_cmd, send.out.payload, 596d97fe8eeSAlison Schofield &send.out.size, &send.retval); 5974faf31b4SDan Williams if (rc) 5984faf31b4SDan Williams return rc; 5994faf31b4SDan Williams 6004faf31b4SDan Williams if (copy_to_user(s, &send, sizeof(send))) 6014faf31b4SDan Williams return -EFAULT; 6024faf31b4SDan Williams 6034faf31b4SDan Williams return 0; 6044faf31b4SDan Williams } 6054faf31b4SDan Williams 606623c0751SRobert Richter static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 *size, u8 *out) 6074faf31b4SDan Williams { 608623c0751SRobert Richter u32 remaining = *size; 6094faf31b4SDan Williams u32 offset = 0; 6104faf31b4SDan Williams 6114faf31b4SDan Williams while (remaining) { 6125e2411aeSIra Weiny u32 xfer_size = min_t(u32, remaining, cxlds->payload_size); 6135331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 6145331cdf4SDan Williams struct cxl_mbox_get_log log; 6154faf31b4SDan Williams int rc; 6164faf31b4SDan Williams 6175331cdf4SDan Williams log = (struct cxl_mbox_get_log) { 6185331cdf4SDan Williams .uuid = *uuid, 6195331cdf4SDan Williams .offset = cpu_to_le32(offset), 6205331cdf4SDan Williams .length = cpu_to_le32(xfer_size), 6215331cdf4SDan Williams }; 6225331cdf4SDan Williams 6235331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 6245331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_LOG, 6255331cdf4SDan Williams .size_in = sizeof(log), 6265331cdf4SDan Williams .payload_in = &log, 6275331cdf4SDan Williams .size_out = xfer_size, 6285331cdf4SDan Williams .payload_out = out, 6295331cdf4SDan Williams }; 6305331cdf4SDan Williams 6315331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 632623c0751SRobert Richter 633623c0751SRobert Richter /* 634623c0751SRobert Richter * The output payload length that indicates the number 635623c0751SRobert Richter * of valid bytes can be smaller than the Log buffer 636623c0751SRobert Richter * size. 637623c0751SRobert Richter */ 638623c0751SRobert Richter if (rc == -EIO && mbox_cmd.size_out < xfer_size) { 639623c0751SRobert Richter offset += mbox_cmd.size_out; 640623c0751SRobert Richter break; 641623c0751SRobert Richter } 642623c0751SRobert Richter 6434faf31b4SDan Williams if (rc < 0) 6444faf31b4SDan Williams return rc; 6454faf31b4SDan Williams 6464faf31b4SDan Williams out += xfer_size; 6474faf31b4SDan Williams remaining -= xfer_size; 6484faf31b4SDan Williams offset += xfer_size; 6494faf31b4SDan Williams } 6504faf31b4SDan Williams 651623c0751SRobert Richter *size = offset; 652623c0751SRobert Richter 6534faf31b4SDan Williams return 0; 6544faf31b4SDan Williams } 6554faf31b4SDan Williams 6564faf31b4SDan Williams /** 6574faf31b4SDan Williams * cxl_walk_cel() - Walk through the Command Effects Log. 6585e2411aeSIra Weiny * @cxlds: The device data for the operation 6594faf31b4SDan Williams * @size: Length of the Command Effects Log. 6604faf31b4SDan Williams * @cel: CEL 6614faf31b4SDan Williams * 6624faf31b4SDan Williams * Iterate over each entry in the CEL and determine if the driver supports the 6634faf31b4SDan Williams * command. If so, the command is enabled for the device and can be used later. 6644faf31b4SDan Williams */ 6655e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) 6664faf31b4SDan Williams { 66749be6dd8SDan Williams struct cxl_cel_entry *cel_entry; 6684faf31b4SDan Williams const int cel_entries = size / sizeof(*cel_entry); 6694faf31b4SDan Williams int i; 6704faf31b4SDan Williams 67149be6dd8SDan Williams cel_entry = (struct cxl_cel_entry *) cel; 6724faf31b4SDan Williams 6734faf31b4SDan Williams for (i = 0; i < cel_entries; i++) { 6744faf31b4SDan Williams u16 opcode = le16_to_cpu(cel_entry[i].opcode); 6754faf31b4SDan Williams struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 6764faf31b4SDan Williams 677d0abf578SAlison Schofield if (!cmd && !cxl_is_poison_command(opcode)) { 6785e2411aeSIra Weiny dev_dbg(cxlds->dev, 679ee611e5eSRobert Richter "Opcode 0x%04x unsupported by driver\n", opcode); 6804faf31b4SDan Williams continue; 6814faf31b4SDan Williams } 6824faf31b4SDan Williams 683d0abf578SAlison Schofield if (cmd) 6845e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 685d0abf578SAlison Schofield 686d0abf578SAlison Schofield if (cxl_is_poison_command(opcode)) 687d0abf578SAlison Schofield cxl_set_poison_cmd_enabled(&cxlds->poison, opcode); 688d0abf578SAlison Schofield 689ee611e5eSRobert Richter dev_dbg(cxlds->dev, "Opcode 0x%04x enabled\n", opcode); 6904faf31b4SDan Williams } 6914faf31b4SDan Williams } 6924faf31b4SDan Williams 6935e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds) 6944faf31b4SDan Williams { 6954faf31b4SDan Williams struct cxl_mbox_get_supported_logs *ret; 6965331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 6974faf31b4SDan Williams int rc; 6984faf31b4SDan Williams 6995e2411aeSIra Weiny ret = kvmalloc(cxlds->payload_size, GFP_KERNEL); 7004faf31b4SDan Williams if (!ret) 7014faf31b4SDan Williams return ERR_PTR(-ENOMEM); 7024faf31b4SDan Williams 7035331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 7045331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS, 7055331cdf4SDan Williams .size_out = cxlds->payload_size, 7065331cdf4SDan Williams .payload_out = ret, 7072aeaf663SDan Williams /* At least the record number field must be valid */ 7082aeaf663SDan Williams .min_out = 2, 7095331cdf4SDan Williams }; 7105331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 7114faf31b4SDan Williams if (rc < 0) { 7124faf31b4SDan Williams kvfree(ret); 7134faf31b4SDan Williams return ERR_PTR(rc); 7144faf31b4SDan Williams } 7154faf31b4SDan Williams 7165331cdf4SDan Williams 7174faf31b4SDan Williams return ret; 7184faf31b4SDan Williams } 7194faf31b4SDan Williams 7204faf31b4SDan Williams enum { 7214faf31b4SDan Williams CEL_UUID, 7224faf31b4SDan Williams VENDOR_DEBUG_UUID, 7234faf31b4SDan Williams }; 7244faf31b4SDan Williams 7254faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */ 7264faf31b4SDan Williams static const uuid_t log_uuid[] = { 72749be6dd8SDan Williams [CEL_UUID] = DEFINE_CXL_CEL_UUID, 72849be6dd8SDan Williams [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID, 7294faf31b4SDan Williams }; 7304faf31b4SDan Williams 7314faf31b4SDan Williams /** 7325e2411aeSIra Weiny * cxl_enumerate_cmds() - Enumerate commands for a device. 7335e2411aeSIra Weiny * @cxlds: The device data for the operation 7344faf31b4SDan Williams * 7354faf31b4SDan Williams * Returns 0 if enumerate completed successfully. 7364faf31b4SDan Williams * 7374faf31b4SDan Williams * CXL devices have optional support for certain commands. This function will 7384faf31b4SDan Williams * determine the set of supported commands for the hardware and update the 7395e2411aeSIra Weiny * enabled_cmds bitmap in the @cxlds. 7404faf31b4SDan Williams */ 7415e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) 7424faf31b4SDan Williams { 7434faf31b4SDan Williams struct cxl_mbox_get_supported_logs *gsl; 7445e2411aeSIra Weiny struct device *dev = cxlds->dev; 7454faf31b4SDan Williams struct cxl_mem_command *cmd; 7464faf31b4SDan Williams int i, rc; 7474faf31b4SDan Williams 7485e2411aeSIra Weiny gsl = cxl_get_gsl(cxlds); 7494faf31b4SDan Williams if (IS_ERR(gsl)) 7504faf31b4SDan Williams return PTR_ERR(gsl); 7514faf31b4SDan Williams 7524faf31b4SDan Williams rc = -ENOENT; 7534faf31b4SDan Williams for (i = 0; i < le16_to_cpu(gsl->entries); i++) { 7544faf31b4SDan Williams u32 size = le32_to_cpu(gsl->entry[i].size); 7554faf31b4SDan Williams uuid_t uuid = gsl->entry[i].uuid; 7564faf31b4SDan Williams u8 *log; 7574faf31b4SDan Williams 7584faf31b4SDan Williams dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size); 7594faf31b4SDan Williams 7604faf31b4SDan Williams if (!uuid_equal(&uuid, &log_uuid[CEL_UUID])) 7614faf31b4SDan Williams continue; 7624faf31b4SDan Williams 7634faf31b4SDan Williams log = kvmalloc(size, GFP_KERNEL); 7644faf31b4SDan Williams if (!log) { 7654faf31b4SDan Williams rc = -ENOMEM; 7664faf31b4SDan Williams goto out; 7674faf31b4SDan Williams } 7684faf31b4SDan Williams 769623c0751SRobert Richter rc = cxl_xfer_log(cxlds, &uuid, &size, log); 7704faf31b4SDan Williams if (rc) { 7714faf31b4SDan Williams kvfree(log); 7724faf31b4SDan Williams goto out; 7734faf31b4SDan Williams } 7744faf31b4SDan Williams 7755e2411aeSIra Weiny cxl_walk_cel(cxlds, size, log); 7764faf31b4SDan Williams kvfree(log); 7774faf31b4SDan Williams 7784faf31b4SDan Williams /* In case CEL was bogus, enable some default commands. */ 7794faf31b4SDan Williams cxl_for_each_cmd(cmd) 7804faf31b4SDan Williams if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE) 7815e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 7824faf31b4SDan Williams 7834faf31b4SDan Williams /* Found the required CEL */ 7844faf31b4SDan Williams rc = 0; 7854faf31b4SDan Williams } 7864faf31b4SDan Williams out: 7874faf31b4SDan Williams kvfree(gsl); 7884faf31b4SDan Williams return rc; 7894faf31b4SDan Williams } 790affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL); 7914faf31b4SDan Williams 792d54a531aSIra Weiny /* 793d54a531aSIra Weiny * General Media Event Record 794d54a531aSIra Weiny * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43 795d54a531aSIra Weiny */ 796d54a531aSIra Weiny static const uuid_t gen_media_event_uuid = 797d54a531aSIra Weiny UUID_INIT(0xfbcd0a77, 0xc260, 0x417f, 798d54a531aSIra Weiny 0x85, 0xa9, 0x08, 0x8b, 0x16, 0x21, 0xeb, 0xa6); 799d54a531aSIra Weiny 8002d6c1e6dSIra Weiny /* 8012d6c1e6dSIra Weiny * DRAM Event Record 8022d6c1e6dSIra Weiny * CXL rev 3.0 section 8.2.9.2.1.2; Table 8-44 8032d6c1e6dSIra Weiny */ 8042d6c1e6dSIra Weiny static const uuid_t dram_event_uuid = 8052d6c1e6dSIra Weiny UUID_INIT(0x601dcbb3, 0x9c06, 0x4eab, 8062d6c1e6dSIra Weiny 0xb8, 0xaf, 0x4e, 0x9b, 0xfb, 0x5c, 0x96, 0x24); 8072d6c1e6dSIra Weiny 80895b49479SIra Weiny /* 80995b49479SIra Weiny * Memory Module Event Record 81095b49479SIra Weiny * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45 81195b49479SIra Weiny */ 81295b49479SIra Weiny static const uuid_t mem_mod_event_uuid = 81395b49479SIra Weiny UUID_INIT(0xfe927475, 0xdd59, 0x4339, 81495b49479SIra Weiny 0xa5, 0x86, 0x79, 0xba, 0xb1, 0x13, 0xb7, 0x74); 81595b49479SIra Weiny 8160c8393dcSIra Weiny static void cxl_event_trace_record(const struct cxl_memdev *cxlmd, 817d54a531aSIra Weiny enum cxl_event_log_type type, 818d54a531aSIra Weiny struct cxl_event_record_raw *record) 819d54a531aSIra Weiny { 820d54a531aSIra Weiny uuid_t *id = &record->hdr.id; 821d54a531aSIra Weiny 822d54a531aSIra Weiny if (uuid_equal(id, &gen_media_event_uuid)) { 823d54a531aSIra Weiny struct cxl_event_gen_media *rec = 824d54a531aSIra Weiny (struct cxl_event_gen_media *)record; 825d54a531aSIra Weiny 8260c8393dcSIra Weiny trace_cxl_general_media(cxlmd, type, rec); 8272d6c1e6dSIra Weiny } else if (uuid_equal(id, &dram_event_uuid)) { 8282d6c1e6dSIra Weiny struct cxl_event_dram *rec = (struct cxl_event_dram *)record; 8292d6c1e6dSIra Weiny 8300c8393dcSIra Weiny trace_cxl_dram(cxlmd, type, rec); 83195b49479SIra Weiny } else if (uuid_equal(id, &mem_mod_event_uuid)) { 83295b49479SIra Weiny struct cxl_event_mem_module *rec = 83395b49479SIra Weiny (struct cxl_event_mem_module *)record; 83495b49479SIra Weiny 8350c8393dcSIra Weiny trace_cxl_memory_module(cxlmd, type, rec); 836d54a531aSIra Weiny } else { 837d54a531aSIra Weiny /* For unknown record types print just the header */ 8380c8393dcSIra Weiny trace_cxl_generic_event(cxlmd, type, record); 839d54a531aSIra Weiny } 840d54a531aSIra Weiny } 841d54a531aSIra Weiny 8426ebe28f9SIra Weiny static int cxl_clear_event_record(struct cxl_dev_state *cxlds, 8436ebe28f9SIra Weiny enum cxl_event_log_type log, 8446ebe28f9SIra Weiny struct cxl_get_event_payload *get_pl) 8456ebe28f9SIra Weiny { 8466ebe28f9SIra Weiny struct cxl_mbox_clear_event_payload *payload; 8476ebe28f9SIra Weiny u16 total = le16_to_cpu(get_pl->record_count); 8486ebe28f9SIra Weiny u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES; 8496ebe28f9SIra Weiny size_t pl_size = struct_size(payload, handles, max_handles); 8506ebe28f9SIra Weiny struct cxl_mbox_cmd mbox_cmd; 8516ebe28f9SIra Weiny u16 cnt; 8526ebe28f9SIra Weiny int rc = 0; 8536ebe28f9SIra Weiny int i; 8546ebe28f9SIra Weiny 8556ebe28f9SIra Weiny /* Payload size may limit the max handles */ 8566ebe28f9SIra Weiny if (pl_size > cxlds->payload_size) { 8576ebe28f9SIra Weiny max_handles = (cxlds->payload_size - sizeof(*payload)) / 8586ebe28f9SIra Weiny sizeof(__le16); 8596ebe28f9SIra Weiny pl_size = struct_size(payload, handles, max_handles); 8606ebe28f9SIra Weiny } 8616ebe28f9SIra Weiny 8626ebe28f9SIra Weiny payload = kvzalloc(pl_size, GFP_KERNEL); 8636ebe28f9SIra Weiny if (!payload) 8646ebe28f9SIra Weiny return -ENOMEM; 8656ebe28f9SIra Weiny 8666ebe28f9SIra Weiny *payload = (struct cxl_mbox_clear_event_payload) { 8676ebe28f9SIra Weiny .event_log = log, 8686ebe28f9SIra Weiny }; 8696ebe28f9SIra Weiny 8706ebe28f9SIra Weiny mbox_cmd = (struct cxl_mbox_cmd) { 8716ebe28f9SIra Weiny .opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD, 8726ebe28f9SIra Weiny .payload_in = payload, 8736ebe28f9SIra Weiny .size_in = pl_size, 8746ebe28f9SIra Weiny }; 8756ebe28f9SIra Weiny 8766ebe28f9SIra Weiny /* 8776ebe28f9SIra Weiny * Clear Event Records uses u8 for the handle cnt while Get Event 8786ebe28f9SIra Weiny * Record can return up to 0xffff records. 8796ebe28f9SIra Weiny */ 8806ebe28f9SIra Weiny i = 0; 8816ebe28f9SIra Weiny for (cnt = 0; cnt < total; cnt++) { 8826ebe28f9SIra Weiny payload->handles[i++] = get_pl->records[cnt].hdr.handle; 8836ebe28f9SIra Weiny dev_dbg(cxlds->dev, "Event log '%d': Clearing %u\n", 8846ebe28f9SIra Weiny log, le16_to_cpu(payload->handles[i])); 8856ebe28f9SIra Weiny 8866ebe28f9SIra Weiny if (i == max_handles) { 8876ebe28f9SIra Weiny payload->nr_recs = i; 8886ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 8896ebe28f9SIra Weiny if (rc) 8906ebe28f9SIra Weiny goto free_pl; 8916ebe28f9SIra Weiny i = 0; 8926ebe28f9SIra Weiny } 8936ebe28f9SIra Weiny } 8946ebe28f9SIra Weiny 8956ebe28f9SIra Weiny /* Clear what is left if any */ 8966ebe28f9SIra Weiny if (i) { 8976ebe28f9SIra Weiny payload->nr_recs = i; 8986ebe28f9SIra Weiny mbox_cmd.size_in = struct_size(payload, handles, i); 8996ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 9006ebe28f9SIra Weiny if (rc) 9016ebe28f9SIra Weiny goto free_pl; 9026ebe28f9SIra Weiny } 9036ebe28f9SIra Weiny 9046ebe28f9SIra Weiny free_pl: 9056ebe28f9SIra Weiny kvfree(payload); 9066ebe28f9SIra Weiny return rc; 9076ebe28f9SIra Weiny } 9086ebe28f9SIra Weiny 9096ebe28f9SIra Weiny static void cxl_mem_get_records_log(struct cxl_dev_state *cxlds, 9106ebe28f9SIra Weiny enum cxl_event_log_type type) 9116ebe28f9SIra Weiny { 9126ebe28f9SIra Weiny struct cxl_get_event_payload *payload; 9136ebe28f9SIra Weiny struct cxl_mbox_cmd mbox_cmd; 9146ebe28f9SIra Weiny u8 log_type = type; 9156ebe28f9SIra Weiny u16 nr_rec; 9166ebe28f9SIra Weiny 9176ebe28f9SIra Weiny mutex_lock(&cxlds->event.log_lock); 9186ebe28f9SIra Weiny payload = cxlds->event.buf; 9196ebe28f9SIra Weiny 9206ebe28f9SIra Weiny mbox_cmd = (struct cxl_mbox_cmd) { 9216ebe28f9SIra Weiny .opcode = CXL_MBOX_OP_GET_EVENT_RECORD, 9226ebe28f9SIra Weiny .payload_in = &log_type, 9236ebe28f9SIra Weiny .size_in = sizeof(log_type), 9246ebe28f9SIra Weiny .payload_out = payload, 9256ebe28f9SIra Weiny .size_out = cxlds->payload_size, 9266ebe28f9SIra Weiny .min_out = struct_size(payload, records, 0), 9276ebe28f9SIra Weiny }; 9286ebe28f9SIra Weiny 9296ebe28f9SIra Weiny do { 9306ebe28f9SIra Weiny int rc, i; 9316ebe28f9SIra Weiny 9326ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 9336ebe28f9SIra Weiny if (rc) { 9346ebe28f9SIra Weiny dev_err_ratelimited(cxlds->dev, 9356ebe28f9SIra Weiny "Event log '%d': Failed to query event records : %d", 9366ebe28f9SIra Weiny type, rc); 9376ebe28f9SIra Weiny break; 9386ebe28f9SIra Weiny } 9396ebe28f9SIra Weiny 9406ebe28f9SIra Weiny nr_rec = le16_to_cpu(payload->record_count); 9416ebe28f9SIra Weiny if (!nr_rec) 9426ebe28f9SIra Weiny break; 9436ebe28f9SIra Weiny 9446ebe28f9SIra Weiny for (i = 0; i < nr_rec; i++) 9450c8393dcSIra Weiny cxl_event_trace_record(cxlds->cxlmd, type, 9466ebe28f9SIra Weiny &payload->records[i]); 9476ebe28f9SIra Weiny 9486ebe28f9SIra Weiny if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW) 9490c8393dcSIra Weiny trace_cxl_overflow(cxlds->cxlmd, type, payload); 9506ebe28f9SIra Weiny 9516ebe28f9SIra Weiny rc = cxl_clear_event_record(cxlds, type, payload); 9526ebe28f9SIra Weiny if (rc) { 9536ebe28f9SIra Weiny dev_err_ratelimited(cxlds->dev, 9546ebe28f9SIra Weiny "Event log '%d': Failed to clear events : %d", 9556ebe28f9SIra Weiny type, rc); 9566ebe28f9SIra Weiny break; 9576ebe28f9SIra Weiny } 9586ebe28f9SIra Weiny } while (nr_rec); 9596ebe28f9SIra Weiny 9606ebe28f9SIra Weiny mutex_unlock(&cxlds->event.log_lock); 9616ebe28f9SIra Weiny } 9626ebe28f9SIra Weiny 9636ebe28f9SIra Weiny /** 9646ebe28f9SIra Weiny * cxl_mem_get_event_records - Get Event Records from the device 9656ebe28f9SIra Weiny * @cxlds: The device data for the operation 9667ebf38c9SJonathan Cameron * @status: Event Status register value identifying which events are available. 9676ebe28f9SIra Weiny * 9686ebe28f9SIra Weiny * Retrieve all event records available on the device, report them as trace 9696ebe28f9SIra Weiny * events, and clear them. 9706ebe28f9SIra Weiny * 9716ebe28f9SIra Weiny * See CXL rev 3.0 @8.2.9.2.2 Get Event Records 9726ebe28f9SIra Weiny * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records 9736ebe28f9SIra Weiny */ 9746ebe28f9SIra Weiny void cxl_mem_get_event_records(struct cxl_dev_state *cxlds, u32 status) 9756ebe28f9SIra Weiny { 9766ebe28f9SIra Weiny dev_dbg(cxlds->dev, "Reading event logs: %x\n", status); 9776ebe28f9SIra Weiny 9786ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_FATAL) 9796ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_FATAL); 9806ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_FAIL) 9816ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_FAIL); 9826ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_WARN) 9836ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_WARN); 9846ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_INFO) 9856ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_INFO); 9866ebe28f9SIra Weiny } 9876ebe28f9SIra Weiny EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, CXL); 9886ebe28f9SIra Weiny 9894faf31b4SDan Williams /** 9904faf31b4SDan Williams * cxl_mem_get_partition_info - Get partition info 9915e2411aeSIra Weiny * @cxlds: The device data for the operation 9924faf31b4SDan Williams * 9934faf31b4SDan Williams * Retrieve the current partition info for the device specified. The active 9944faf31b4SDan Williams * values are the current capacity in bytes. If not 0, the 'next' values are 9954faf31b4SDan Williams * the pending values, in bytes, which take affect on next cold reset. 9964faf31b4SDan Williams * 9974faf31b4SDan Williams * Return: 0 if no error: or the result of the mailbox command. 9984faf31b4SDan Williams * 9994faf31b4SDan Williams * See CXL @8.2.9.5.2.1 Get Partition Info 10004faf31b4SDan Williams */ 10015e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds) 10024faf31b4SDan Williams { 1003e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info pi; 10045331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 10054faf31b4SDan Williams int rc; 10064faf31b4SDan Williams 10075331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 10085331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_PARTITION_INFO, 10095331cdf4SDan Williams .size_out = sizeof(pi), 10105331cdf4SDan Williams .payload_out = &pi, 10115331cdf4SDan Williams }; 10125331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 10134faf31b4SDan Williams if (rc) 10144faf31b4SDan Williams return rc; 10154faf31b4SDan Williams 10165e2411aeSIra Weiny cxlds->active_volatile_bytes = 10174faf31b4SDan Williams le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 10185e2411aeSIra Weiny cxlds->active_persistent_bytes = 10194faf31b4SDan Williams le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER; 10205e2411aeSIra Weiny cxlds->next_volatile_bytes = 10214faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 10225e2411aeSIra Weiny cxlds->next_persistent_bytes = 10234faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 10244faf31b4SDan Williams 10254faf31b4SDan Williams return 0; 10264faf31b4SDan Williams } 10274faf31b4SDan Williams 10284faf31b4SDan Williams /** 10295e2411aeSIra Weiny * cxl_dev_state_identify() - Send the IDENTIFY command to the device. 10305e2411aeSIra Weiny * @cxlds: The device data for the operation 10314faf31b4SDan Williams * 1032e764f122SDave Jiang * Return: 0 if identify was executed successfully or media not ready. 10334faf31b4SDan Williams * 10344faf31b4SDan Williams * This will dispatch the identify command to the device and on success populate 10354faf31b4SDan Williams * structures to be exported to sysfs. 10364faf31b4SDan Williams */ 10375e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds) 10384faf31b4SDan Williams { 10394faf31b4SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 104049be6dd8SDan Williams struct cxl_mbox_identify id; 10415331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 1042ed83f7caSAlison Schofield u32 val; 10434faf31b4SDan Williams int rc; 10444faf31b4SDan Williams 1045e764f122SDave Jiang if (!cxlds->media_ready) 1046e764f122SDave Jiang return 0; 1047e764f122SDave Jiang 10485331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 10495331cdf4SDan Williams .opcode = CXL_MBOX_OP_IDENTIFY, 10505331cdf4SDan Williams .size_out = sizeof(id), 10515331cdf4SDan Williams .payload_out = &id, 10525331cdf4SDan Williams }; 10535331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 10544faf31b4SDan Williams if (rc < 0) 10554faf31b4SDan Williams return rc; 10564faf31b4SDan Williams 10575e2411aeSIra Weiny cxlds->total_bytes = 10584faf31b4SDan Williams le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER; 10595e2411aeSIra Weiny cxlds->volatile_only_bytes = 10604faf31b4SDan Williams le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER; 10615e2411aeSIra Weiny cxlds->persistent_only_bytes = 10624faf31b4SDan Williams le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER; 10635e2411aeSIra Weiny cxlds->partition_align_bytes = 10644faf31b4SDan Williams le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER; 10654faf31b4SDan Williams 10665e2411aeSIra Weiny cxlds->lsa_size = le32_to_cpu(id.lsa_size); 10675e2411aeSIra Weiny memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision)); 10684faf31b4SDan Williams 1069ed83f7caSAlison Schofield if (test_bit(CXL_POISON_ENABLED_LIST, cxlds->poison.enabled_cmds)) { 1070ed83f7caSAlison Schofield val = get_unaligned_le24(id.poison_list_max_mer); 1071ed83f7caSAlison Schofield cxlds->poison.max_errors = min_t(u32, val, CXL_POISON_LIST_MAX); 1072ed83f7caSAlison Schofield } 1073ed83f7caSAlison Schofield 10744faf31b4SDan Williams return 0; 10754faf31b4SDan Williams } 1076affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL); 10774faf31b4SDan Williams 1078*48dcdbb1SDavidlohr Bueso /** 1079*48dcdbb1SDavidlohr Bueso * cxl_mem_sanitize() - Send a sanitization command to the device. 1080*48dcdbb1SDavidlohr Bueso * @cxlds: The device data for the operation 1081*48dcdbb1SDavidlohr Bueso * @cmd: The specific sanitization command opcode 1082*48dcdbb1SDavidlohr Bueso * 1083*48dcdbb1SDavidlohr Bueso * Return: 0 if the command was executed successfully, regardless of 1084*48dcdbb1SDavidlohr Bueso * whether or not the actual security operation is done in the background, 1085*48dcdbb1SDavidlohr Bueso * such as for the Sanitize case. 1086*48dcdbb1SDavidlohr Bueso * Error return values can be the result of the mailbox command, -EINVAL 1087*48dcdbb1SDavidlohr Bueso * when security requirements are not met or invalid contexts. 1088*48dcdbb1SDavidlohr Bueso * 1089*48dcdbb1SDavidlohr Bueso * See CXL 3.0 @8.2.9.8.5.1 Sanitize and @8.2.9.8.5.2 Secure Erase. 1090*48dcdbb1SDavidlohr Bueso */ 1091*48dcdbb1SDavidlohr Bueso int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd) 1092*48dcdbb1SDavidlohr Bueso { 1093*48dcdbb1SDavidlohr Bueso int rc; 1094*48dcdbb1SDavidlohr Bueso u32 sec_out = 0; 1095*48dcdbb1SDavidlohr Bueso struct cxl_get_security_output { 1096*48dcdbb1SDavidlohr Bueso __le32 flags; 1097*48dcdbb1SDavidlohr Bueso } out; 1098*48dcdbb1SDavidlohr Bueso struct cxl_mbox_cmd sec_cmd = { 1099*48dcdbb1SDavidlohr Bueso .opcode = CXL_MBOX_OP_GET_SECURITY_STATE, 1100*48dcdbb1SDavidlohr Bueso .payload_out = &out, 1101*48dcdbb1SDavidlohr Bueso .size_out = sizeof(out), 1102*48dcdbb1SDavidlohr Bueso }; 1103*48dcdbb1SDavidlohr Bueso struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd }; 1104*48dcdbb1SDavidlohr Bueso 1105*48dcdbb1SDavidlohr Bueso if (cmd != CXL_MBOX_OP_SANITIZE) 1106*48dcdbb1SDavidlohr Bueso return -EINVAL; 1107*48dcdbb1SDavidlohr Bueso 1108*48dcdbb1SDavidlohr Bueso rc = cxl_internal_send_cmd(cxlds, &sec_cmd); 1109*48dcdbb1SDavidlohr Bueso if (rc < 0) { 1110*48dcdbb1SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get security state : %d", rc); 1111*48dcdbb1SDavidlohr Bueso return rc; 1112*48dcdbb1SDavidlohr Bueso } 1113*48dcdbb1SDavidlohr Bueso 1114*48dcdbb1SDavidlohr Bueso /* 1115*48dcdbb1SDavidlohr Bueso * Prior to using these commands, any security applied to 1116*48dcdbb1SDavidlohr Bueso * the user data areas of the device shall be DISABLED (or 1117*48dcdbb1SDavidlohr Bueso * UNLOCKED for secure erase case). 1118*48dcdbb1SDavidlohr Bueso */ 1119*48dcdbb1SDavidlohr Bueso sec_out = le32_to_cpu(out.flags); 1120*48dcdbb1SDavidlohr Bueso if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET) 1121*48dcdbb1SDavidlohr Bueso return -EINVAL; 1122*48dcdbb1SDavidlohr Bueso 1123*48dcdbb1SDavidlohr Bueso rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 1124*48dcdbb1SDavidlohr Bueso if (rc < 0) { 1125*48dcdbb1SDavidlohr Bueso dev_err(cxlds->dev, "Failed to sanitize device : %d", rc); 1126*48dcdbb1SDavidlohr Bueso return rc; 1127*48dcdbb1SDavidlohr Bueso } 1128*48dcdbb1SDavidlohr Bueso 1129*48dcdbb1SDavidlohr Bueso return 0; 1130*48dcdbb1SDavidlohr Bueso } 1131*48dcdbb1SDavidlohr Bueso EXPORT_SYMBOL_NS_GPL(cxl_mem_sanitize, CXL); 1132*48dcdbb1SDavidlohr Bueso 1133d3b75029SDan Williams static int add_dpa_res(struct device *dev, struct resource *parent, 1134d3b75029SDan Williams struct resource *res, resource_size_t start, 1135d3b75029SDan Williams resource_size_t size, const char *type) 11364faf31b4SDan Williams { 11374faf31b4SDan Williams int rc; 11384faf31b4SDan Williams 1139d3b75029SDan Williams res->name = type; 1140d3b75029SDan Williams res->start = start; 1141d3b75029SDan Williams res->end = start + size - 1; 1142d3b75029SDan Williams res->flags = IORESOURCE_MEM; 1143d3b75029SDan Williams if (resource_size(res) == 0) { 1144d3b75029SDan Williams dev_dbg(dev, "DPA(%s): no capacity\n", res->name); 11454faf31b4SDan Williams return 0; 11464faf31b4SDan Williams } 1147d3b75029SDan Williams rc = request_resource(parent, res); 1148d3b75029SDan Williams if (rc) { 1149d3b75029SDan Williams dev_err(dev, "DPA(%s): failed to track %pr (%d)\n", res->name, 1150d3b75029SDan Williams res, rc); 1151d3b75029SDan Williams return rc; 1152d3b75029SDan Williams } 1153d3b75029SDan Williams 1154d3b75029SDan Williams dev_dbg(dev, "DPA(%s): %pr\n", res->name, res); 1155d3b75029SDan Williams 1156d3b75029SDan Williams return 0; 1157d3b75029SDan Williams } 1158d3b75029SDan Williams 1159d3b75029SDan Williams int cxl_mem_create_range_info(struct cxl_dev_state *cxlds) 1160d3b75029SDan Williams { 1161d3b75029SDan Williams struct device *dev = cxlds->dev; 1162d3b75029SDan Williams int rc; 1163d3b75029SDan Williams 1164d3b75029SDan Williams cxlds->dpa_res = 1165d3b75029SDan Williams (struct resource)DEFINE_RES_MEM(0, cxlds->total_bytes); 1166d3b75029SDan Williams 1167d3b75029SDan Williams if (cxlds->partition_align_bytes == 0) { 1168d3b75029SDan Williams rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0, 1169d3b75029SDan Williams cxlds->volatile_only_bytes, "ram"); 1170d3b75029SDan Williams if (rc) 1171d3b75029SDan Williams return rc; 1172d3b75029SDan Williams return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res, 1173d3b75029SDan Williams cxlds->volatile_only_bytes, 1174d3b75029SDan Williams cxlds->persistent_only_bytes, "pmem"); 1175d3b75029SDan Williams } 11764faf31b4SDan Williams 1177e764f122SDave Jiang if (cxlds->media_ready) { 11785e2411aeSIra Weiny rc = cxl_mem_get_partition_info(cxlds); 11794faf31b4SDan Williams if (rc) { 1180d3b75029SDan Williams dev_err(dev, "Failed to query partition information\n"); 11814faf31b4SDan Williams return rc; 11824faf31b4SDan Williams } 1183e764f122SDave Jiang } 11844faf31b4SDan Williams 1185d3b75029SDan Williams rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0, 1186d3b75029SDan Williams cxlds->active_volatile_bytes, "ram"); 1187d3b75029SDan Williams if (rc) 1188d3b75029SDan Williams return rc; 1189d3b75029SDan Williams return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res, 1190d3b75029SDan Williams cxlds->active_volatile_bytes, 1191d3b75029SDan Williams cxlds->active_persistent_bytes, "pmem"); 11924faf31b4SDan Williams } 1193affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL); 11944faf31b4SDan Williams 1195fa884345SJonathan Cameron int cxl_set_timestamp(struct cxl_dev_state *cxlds) 1196fa884345SJonathan Cameron { 1197fa884345SJonathan Cameron struct cxl_mbox_cmd mbox_cmd; 1198fa884345SJonathan Cameron struct cxl_mbox_set_timestamp_in pi; 1199fa884345SJonathan Cameron int rc; 1200fa884345SJonathan Cameron 1201fa884345SJonathan Cameron pi.timestamp = cpu_to_le64(ktime_get_real_ns()); 1202fa884345SJonathan Cameron mbox_cmd = (struct cxl_mbox_cmd) { 1203fa884345SJonathan Cameron .opcode = CXL_MBOX_OP_SET_TIMESTAMP, 1204fa884345SJonathan Cameron .size_in = sizeof(pi), 1205fa884345SJonathan Cameron .payload_in = &pi, 1206fa884345SJonathan Cameron }; 1207fa884345SJonathan Cameron 1208fa884345SJonathan Cameron rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 1209fa884345SJonathan Cameron /* 1210fa884345SJonathan Cameron * Command is optional. Devices may have another way of providing 1211fa884345SJonathan Cameron * a timestamp, or may return all 0s in timestamp fields. 1212fa884345SJonathan Cameron * Don't report an error if this command isn't supported 1213fa884345SJonathan Cameron */ 1214fa884345SJonathan Cameron if (rc && (mbox_cmd.return_code != CXL_MBOX_CMD_RC_UNSUPPORTED)) 1215fa884345SJonathan Cameron return rc; 1216fa884345SJonathan Cameron 1217fa884345SJonathan Cameron return 0; 1218fa884345SJonathan Cameron } 1219fa884345SJonathan Cameron EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL); 1220fa884345SJonathan Cameron 1221ed83f7caSAlison Schofield int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, 1222ed83f7caSAlison Schofield struct cxl_region *cxlr) 1223ed83f7caSAlison Schofield { 1224ed83f7caSAlison Schofield struct cxl_dev_state *cxlds = cxlmd->cxlds; 1225ed83f7caSAlison Schofield struct cxl_mbox_poison_out *po; 1226ed83f7caSAlison Schofield struct cxl_mbox_poison_in pi; 1227ed83f7caSAlison Schofield struct cxl_mbox_cmd mbox_cmd; 1228ed83f7caSAlison Schofield int nr_records = 0; 1229ed83f7caSAlison Schofield int rc; 1230ed83f7caSAlison Schofield 1231ed83f7caSAlison Schofield rc = mutex_lock_interruptible(&cxlds->poison.lock); 1232ed83f7caSAlison Schofield if (rc) 1233ed83f7caSAlison Schofield return rc; 1234ed83f7caSAlison Schofield 1235ed83f7caSAlison Schofield po = cxlds->poison.list_out; 1236ed83f7caSAlison Schofield pi.offset = cpu_to_le64(offset); 1237ed83f7caSAlison Schofield pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT); 1238ed83f7caSAlison Schofield 1239ed83f7caSAlison Schofield mbox_cmd = (struct cxl_mbox_cmd) { 1240ed83f7caSAlison Schofield .opcode = CXL_MBOX_OP_GET_POISON, 1241ed83f7caSAlison Schofield .size_in = sizeof(pi), 1242ed83f7caSAlison Schofield .payload_in = &pi, 1243ed83f7caSAlison Schofield .size_out = cxlds->payload_size, 1244ed83f7caSAlison Schofield .payload_out = po, 1245ed83f7caSAlison Schofield .min_out = struct_size(po, record, 0), 1246ed83f7caSAlison Schofield }; 1247ed83f7caSAlison Schofield 1248ed83f7caSAlison Schofield do { 1249ed83f7caSAlison Schofield rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 1250ed83f7caSAlison Schofield if (rc) 1251ed83f7caSAlison Schofield break; 1252ed83f7caSAlison Schofield 1253ddf49d57SAlison Schofield for (int i = 0; i < le16_to_cpu(po->count); i++) 1254ddf49d57SAlison Schofield trace_cxl_poison(cxlmd, cxlr, &po->record[i], 1255ddf49d57SAlison Schofield po->flags, po->overflow_ts, 1256ddf49d57SAlison Schofield CXL_POISON_TRACE_LIST); 1257ed83f7caSAlison Schofield 1258ed83f7caSAlison Schofield /* Protect against an uncleared _FLAG_MORE */ 1259ed83f7caSAlison Schofield nr_records = nr_records + le16_to_cpu(po->count); 1260ed83f7caSAlison Schofield if (nr_records >= cxlds->poison.max_errors) { 1261ed83f7caSAlison Schofield dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n", 1262ed83f7caSAlison Schofield nr_records); 1263ed83f7caSAlison Schofield break; 1264ed83f7caSAlison Schofield } 1265ed83f7caSAlison Schofield } while (po->flags & CXL_POISON_FLAG_MORE); 1266ed83f7caSAlison Schofield 1267ed83f7caSAlison Schofield mutex_unlock(&cxlds->poison.lock); 1268ed83f7caSAlison Schofield return rc; 1269ed83f7caSAlison Schofield } 1270ed83f7caSAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_mem_get_poison, CXL); 1271ed83f7caSAlison Schofield 1272d0abf578SAlison Schofield static void free_poison_buf(void *buf) 1273d0abf578SAlison Schofield { 1274d0abf578SAlison Schofield kvfree(buf); 1275d0abf578SAlison Schofield } 1276d0abf578SAlison Schofield 1277d0abf578SAlison Schofield /* Get Poison List output buffer is protected by cxlds->poison.lock */ 1278d0abf578SAlison Schofield static int cxl_poison_alloc_buf(struct cxl_dev_state *cxlds) 1279d0abf578SAlison Schofield { 1280d0abf578SAlison Schofield cxlds->poison.list_out = kvmalloc(cxlds->payload_size, GFP_KERNEL); 1281d0abf578SAlison Schofield if (!cxlds->poison.list_out) 1282d0abf578SAlison Schofield return -ENOMEM; 1283d0abf578SAlison Schofield 1284d0abf578SAlison Schofield return devm_add_action_or_reset(cxlds->dev, free_poison_buf, 1285d0abf578SAlison Schofield cxlds->poison.list_out); 1286d0abf578SAlison Schofield } 1287d0abf578SAlison Schofield 1288d0abf578SAlison Schofield int cxl_poison_state_init(struct cxl_dev_state *cxlds) 1289d0abf578SAlison Schofield { 1290d0abf578SAlison Schofield int rc; 1291d0abf578SAlison Schofield 1292d0abf578SAlison Schofield if (!test_bit(CXL_POISON_ENABLED_LIST, cxlds->poison.enabled_cmds)) 1293d0abf578SAlison Schofield return 0; 1294d0abf578SAlison Schofield 1295d0abf578SAlison Schofield rc = cxl_poison_alloc_buf(cxlds); 1296d0abf578SAlison Schofield if (rc) { 1297d0abf578SAlison Schofield clear_bit(CXL_POISON_ENABLED_LIST, cxlds->poison.enabled_cmds); 1298d0abf578SAlison Schofield return rc; 1299d0abf578SAlison Schofield } 1300d0abf578SAlison Schofield 1301d0abf578SAlison Schofield mutex_init(&cxlds->poison.lock); 1302d0abf578SAlison Schofield return 0; 1303d0abf578SAlison Schofield } 1304d0abf578SAlison Schofield EXPORT_SYMBOL_NS_GPL(cxl_poison_state_init, CXL); 1305d0abf578SAlison Schofield 13065e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev) 13074faf31b4SDan Williams { 13085e2411aeSIra Weiny struct cxl_dev_state *cxlds; 13094faf31b4SDan Williams 13105e2411aeSIra Weiny cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL); 13115e2411aeSIra Weiny if (!cxlds) { 13124faf31b4SDan Williams dev_err(dev, "No memory available\n"); 13134faf31b4SDan Williams return ERR_PTR(-ENOMEM); 13144faf31b4SDan Williams } 13154faf31b4SDan Williams 13165e2411aeSIra Weiny mutex_init(&cxlds->mbox_mutex); 13176ebe28f9SIra Weiny mutex_init(&cxlds->event.log_lock); 13185e2411aeSIra Weiny cxlds->dev = dev; 13194faf31b4SDan Williams 13205e2411aeSIra Weiny return cxlds; 13214faf31b4SDan Williams } 1322affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL); 13234faf31b4SDan Williams 13244faf31b4SDan Williams void __init cxl_mbox_init(void) 13254faf31b4SDan Williams { 13264faf31b4SDan Williams struct dentry *mbox_debugfs; 13274faf31b4SDan Williams 13289b99ecf5SDan Williams mbox_debugfs = cxl_debugfs_create_dir("mbox"); 13294faf31b4SDan Williams debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs, 13304faf31b4SDan Williams &cxl_raw_allow_all); 13314faf31b4SDan Williams } 1332