14faf31b4SDan Williams // SPDX-License-Identifier: GPL-2.0-only 24faf31b4SDan Williams /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 34faf31b4SDan Williams #include <linux/io-64-nonatomic-lo-hi.h> 44faf31b4SDan Williams #include <linux/security.h> 54faf31b4SDan Williams #include <linux/debugfs.h> 64faf31b4SDan Williams #include <linux/mutex.h> 74faf31b4SDan Williams #include <cxlmem.h> 84faf31b4SDan Williams #include <cxl.h> 94faf31b4SDan Williams 104faf31b4SDan Williams #include "core.h" 116ebe28f9SIra Weiny #include "trace.h" 124faf31b4SDan Williams 134faf31b4SDan Williams static bool cxl_raw_allow_all; 144faf31b4SDan Williams 154faf31b4SDan Williams /** 164faf31b4SDan Williams * DOC: cxl mbox 174faf31b4SDan Williams * 184faf31b4SDan Williams * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The 194faf31b4SDan Williams * implementation is used by the cxl_pci driver to initialize the device 204faf31b4SDan Williams * and implement the cxl_mem.h IOCTL UAPI. It also implements the 214faf31b4SDan Williams * backend of the cxl_pmem_ctl() transport for LIBNVDIMM. 224faf31b4SDan Williams */ 234faf31b4SDan Williams 244faf31b4SDan Williams #define cxl_for_each_cmd(cmd) \ 254faf31b4SDan Williams for ((cmd) = &cxl_mem_commands[0]; \ 264faf31b4SDan Williams ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++) 274faf31b4SDan Williams 284faf31b4SDan Williams #define CXL_CMD(_id, sin, sout, _flags) \ 294faf31b4SDan Williams [CXL_MEM_COMMAND_ID_##_id] = { \ 304faf31b4SDan Williams .info = { \ 314faf31b4SDan Williams .id = CXL_MEM_COMMAND_ID_##_id, \ 324faf31b4SDan Williams .size_in = sin, \ 334faf31b4SDan Williams .size_out = sout, \ 344faf31b4SDan Williams }, \ 354faf31b4SDan Williams .opcode = CXL_MBOX_OP_##_id, \ 364faf31b4SDan Williams .flags = _flags, \ 374faf31b4SDan Williams } 384faf31b4SDan Williams 3926f89535SAlison Schofield #define CXL_VARIABLE_PAYLOAD ~0U 404faf31b4SDan Williams /* 414faf31b4SDan Williams * This table defines the supported mailbox commands for the driver. This table 424faf31b4SDan Williams * is made up of a UAPI structure. Non-negative values as parameters in the 434faf31b4SDan Williams * table will be validated against the user's input. For example, if size_in is 444faf31b4SDan Williams * 0, and the user passed in 1, it is an error. 454faf31b4SDan Williams */ 464faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = { 474faf31b4SDan Williams CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE), 484faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS 4926f89535SAlison Schofield CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0), 504faf31b4SDan Williams #endif 5126f89535SAlison Schofield CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 524faf31b4SDan Williams CXL_CMD(GET_FW_INFO, 0, 0x50, 0), 534faf31b4SDan Williams CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0), 5426f89535SAlison Schofield CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0), 554faf31b4SDan Williams CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0), 5626f89535SAlison Schofield CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 574faf31b4SDan Williams CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0), 5826f89535SAlison Schofield CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0), 594faf31b4SDan Williams CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0), 604faf31b4SDan Williams CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0), 614faf31b4SDan Williams CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0), 624faf31b4SDan Williams CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0), 6326f89535SAlison Schofield CXL_CMD(GET_POISON, 0x10, CXL_VARIABLE_PAYLOAD, 0), 644faf31b4SDan Williams CXL_CMD(INJECT_POISON, 0x8, 0, 0), 654faf31b4SDan Williams CXL_CMD(CLEAR_POISON, 0x48, 0, 0), 664faf31b4SDan Williams CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0), 674faf31b4SDan Williams CXL_CMD(SCAN_MEDIA, 0x11, 0, 0), 6826f89535SAlison Schofield CXL_CMD(GET_SCAN_MEDIA, 0, CXL_VARIABLE_PAYLOAD, 0), 694faf31b4SDan Williams }; 704faf31b4SDan Williams 714faf31b4SDan Williams /* 724faf31b4SDan Williams * Commands that RAW doesn't permit. The rationale for each: 734faf31b4SDan Williams * 744faf31b4SDan Williams * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / 754faf31b4SDan Williams * coordination of transaction timeout values at the root bridge level. 764faf31b4SDan Williams * 774faf31b4SDan Williams * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live 784faf31b4SDan Williams * and needs to be coordinated with HDM updates. 794faf31b4SDan Williams * 804faf31b4SDan Williams * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the 814faf31b4SDan Williams * driver and any writes from userspace invalidates those contents. 824faf31b4SDan Williams * 834faf31b4SDan Williams * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes 844faf31b4SDan Williams * to the device after it is marked clean, userspace can not make that 854faf31b4SDan Williams * assertion. 864faf31b4SDan Williams * 874faf31b4SDan Williams * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that 884faf31b4SDan Williams * is kept up to date with patrol notifications and error management. 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, 974faf31b4SDan Williams }; 984faf31b4SDan Williams 994faf31b4SDan Williams /* 1004faf31b4SDan Williams * Command sets that RAW doesn't permit. All opcodes in this set are 1014faf31b4SDan Williams * disabled because they pass plain text security payloads over the 1024faf31b4SDan Williams * user/kernel boundary. This functionality is intended to be wrapped 1034faf31b4SDan Williams * behind the keys ABI which allows for encrypted payloads in the UAPI 1044faf31b4SDan Williams */ 1054faf31b4SDan Williams static u8 security_command_sets[] = { 1064faf31b4SDan Williams 0x44, /* Sanitize */ 1074faf31b4SDan Williams 0x45, /* Persistent Memory Data-at-rest Security */ 1084faf31b4SDan Williams 0x46, /* Security Passthrough */ 1094faf31b4SDan Williams }; 1104faf31b4SDan Williams 1114faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode) 1124faf31b4SDan Williams { 1134faf31b4SDan Williams int i; 1144faf31b4SDan Williams 1154faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(security_command_sets); i++) 1164faf31b4SDan Williams if (security_command_sets[i] == (opcode >> 8)) 1174faf31b4SDan Williams return true; 1184faf31b4SDan Williams return false; 1194faf31b4SDan Williams } 1204faf31b4SDan Williams 1214faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) 1224faf31b4SDan Williams { 1234faf31b4SDan Williams struct cxl_mem_command *c; 1244faf31b4SDan Williams 1254faf31b4SDan Williams cxl_for_each_cmd(c) 1264faf31b4SDan Williams if (c->opcode == opcode) 1274faf31b4SDan Williams return c; 1284faf31b4SDan Williams 1294faf31b4SDan Williams return NULL; 1304faf31b4SDan Williams } 1314faf31b4SDan Williams 13282b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode) 13382b8ba29SAlison Schofield { 13482b8ba29SAlison Schofield struct cxl_mem_command *c; 13582b8ba29SAlison Schofield 13682b8ba29SAlison Schofield c = cxl_mem_find_command(opcode); 13782b8ba29SAlison Schofield if (!c) 13882b8ba29SAlison Schofield return NULL; 13982b8ba29SAlison Schofield 14082b8ba29SAlison Schofield return cxl_command_names[c->info.id].name; 14182b8ba29SAlison Schofield } 14282b8ba29SAlison Schofield 1434faf31b4SDan Williams /** 1445331cdf4SDan Williams * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command 1455e2411aeSIra Weiny * @cxlds: The device data for the operation 1465331cdf4SDan Williams * @mbox_cmd: initialized command to execute 1474faf31b4SDan Williams * 148ee92c7e2SDavidlohr Bueso * Context: Any context. 1494faf31b4SDan Williams * Return: 1504faf31b4SDan Williams * * %>=0 - Number of bytes returned in @out. 1514faf31b4SDan Williams * * %-E2BIG - Payload is too large for hardware. 1524faf31b4SDan Williams * * %-EBUSY - Couldn't acquire exclusive mailbox access. 1534faf31b4SDan Williams * * %-EFAULT - Hardware error occurred. 1544faf31b4SDan Williams * * %-ENXIO - Command completed, but device reported an error. 1554faf31b4SDan Williams * * %-EIO - Unexpected output size. 1564faf31b4SDan Williams * 1574faf31b4SDan Williams * Mailbox commands may execute successfully yet the device itself reported an 1584faf31b4SDan Williams * error. While this distinction can be useful for commands from userspace, the 1594faf31b4SDan Williams * kernel will only be able to use results when both are successful. 1604faf31b4SDan Williams */ 1615331cdf4SDan Williams int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, 1625331cdf4SDan Williams struct cxl_mbox_cmd *mbox_cmd) 1634faf31b4SDan Williams { 1642aeaf663SDan Williams size_t out_size, min_out; 1654faf31b4SDan Williams int rc; 1664faf31b4SDan Williams 1675331cdf4SDan Williams if (mbox_cmd->size_in > cxlds->payload_size || 1685331cdf4SDan Williams mbox_cmd->size_out > cxlds->payload_size) 1694faf31b4SDan Williams return -E2BIG; 1704faf31b4SDan Williams 1715331cdf4SDan Williams out_size = mbox_cmd->size_out; 1722aeaf663SDan Williams min_out = mbox_cmd->min_out; 1735331cdf4SDan Williams rc = cxlds->mbox_send(cxlds, mbox_cmd); 1744faf31b4SDan Williams if (rc) 1754faf31b4SDan Williams return rc; 1764faf31b4SDan Williams 1775331cdf4SDan Williams if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) 1785331cdf4SDan Williams return cxl_mbox_cmd_rc2errno(mbox_cmd); 1794faf31b4SDan Williams 1802aeaf663SDan Williams if (!out_size) 1812aeaf663SDan Williams return 0; 1822aeaf663SDan Williams 1834faf31b4SDan Williams /* 1842aeaf663SDan Williams * Variable sized output needs to at least satisfy the caller's 1852aeaf663SDan Williams * minimum if not the fully requested size. 1864faf31b4SDan Williams */ 1872aeaf663SDan Williams if (min_out == 0) 1882aeaf663SDan Williams min_out = out_size; 1892aeaf663SDan Williams 1902aeaf663SDan Williams if (mbox_cmd->size_out < min_out) 1914faf31b4SDan Williams return -EIO; 1924faf31b4SDan Williams return 0; 1934faf31b4SDan Williams } 1945331cdf4SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, CXL); 1954faf31b4SDan Williams 1964faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode) 1974faf31b4SDan Williams { 1984faf31b4SDan Williams int i; 1994faf31b4SDan Williams 2004faf31b4SDan Williams if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS)) 2014faf31b4SDan Williams return false; 2024faf31b4SDan Williams 2034faf31b4SDan Williams if (security_locked_down(LOCKDOWN_PCI_ACCESS)) 2044faf31b4SDan Williams return false; 2054faf31b4SDan Williams 2064faf31b4SDan Williams if (cxl_raw_allow_all) 2074faf31b4SDan Williams return true; 2084faf31b4SDan Williams 2094faf31b4SDan Williams if (cxl_is_security_command(opcode)) 2104faf31b4SDan Williams return false; 2114faf31b4SDan Williams 2124faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++) 2134faf31b4SDan Williams if (cxl_disabled_raw_commands[i] == opcode) 2144faf31b4SDan Williams return false; 2154faf31b4SDan Williams 2164faf31b4SDan Williams return true; 2174faf31b4SDan Williams } 2184faf31b4SDan Williams 2196179045cSAlison Schofield /** 2206179045cSAlison Schofield * cxl_payload_from_user_allowed() - Check contents of in_payload. 2216179045cSAlison Schofield * @opcode: The mailbox command opcode. 2226179045cSAlison Schofield * @payload_in: Pointer to the input payload passed in from user space. 2236179045cSAlison Schofield * 2246179045cSAlison Schofield * Return: 2256179045cSAlison Schofield * * true - payload_in passes check for @opcode. 2266179045cSAlison Schofield * * false - payload_in contains invalid or unsupported values. 2276179045cSAlison Schofield * 2286179045cSAlison Schofield * The driver may inspect payload contents before sending a mailbox 2296179045cSAlison Schofield * command from user space to the device. The intent is to reject 2306179045cSAlison Schofield * commands with input payloads that are known to be unsafe. This 2316179045cSAlison Schofield * check is not intended to replace the users careful selection of 2326179045cSAlison Schofield * mailbox command parameters and makes no guarantee that the user 2336179045cSAlison Schofield * command will succeed, nor that it is appropriate. 2346179045cSAlison Schofield * 2356179045cSAlison Schofield * The specific checks are determined by the opcode. 2366179045cSAlison Schofield */ 2376179045cSAlison Schofield static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in) 2386179045cSAlison Schofield { 2396179045cSAlison Schofield switch (opcode) { 2406179045cSAlison Schofield case CXL_MBOX_OP_SET_PARTITION_INFO: { 2416179045cSAlison Schofield struct cxl_mbox_set_partition_info *pi = payload_in; 2426179045cSAlison Schofield 24335e01667SDan Carpenter if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG) 2446179045cSAlison Schofield return false; 2456179045cSAlison Schofield break; 2466179045cSAlison Schofield } 2476179045cSAlison Schofield default: 2486179045cSAlison Schofield break; 2496179045cSAlison Schofield } 2506179045cSAlison Schofield return true; 2516179045cSAlison Schofield } 2526179045cSAlison Schofield 25363cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, 25463cf60b7SAlison Schofield struct cxl_dev_state *cxlds, u16 opcode, 25563cf60b7SAlison Schofield size_t in_size, size_t out_size, u64 in_payload) 25663cf60b7SAlison Schofield { 25763cf60b7SAlison Schofield *mbox = (struct cxl_mbox_cmd) { 25863cf60b7SAlison Schofield .opcode = opcode, 25963cf60b7SAlison Schofield .size_in = in_size, 26063cf60b7SAlison Schofield }; 26163cf60b7SAlison Schofield 26263cf60b7SAlison Schofield if (in_size) { 26363cf60b7SAlison Schofield mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload), 26463cf60b7SAlison Schofield in_size); 265280302f0SAlison Schofield if (IS_ERR(mbox->payload_in)) 26663cf60b7SAlison Schofield return PTR_ERR(mbox->payload_in); 2676179045cSAlison Schofield 2686179045cSAlison Schofield if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) { 2696179045cSAlison Schofield dev_dbg(cxlds->dev, "%s: input payload not allowed\n", 2706179045cSAlison Schofield cxl_mem_opcode_to_name(opcode)); 2716179045cSAlison Schofield kvfree(mbox->payload_in); 2726179045cSAlison Schofield return -EBUSY; 2736179045cSAlison Schofield } 27463cf60b7SAlison Schofield } 27563cf60b7SAlison Schofield 27663cf60b7SAlison Schofield /* Prepare to handle a full payload for variable sized output */ 27726f89535SAlison Schofield if (out_size == CXL_VARIABLE_PAYLOAD) 27863cf60b7SAlison Schofield mbox->size_out = cxlds->payload_size; 27963cf60b7SAlison Schofield else 28063cf60b7SAlison Schofield mbox->size_out = out_size; 28163cf60b7SAlison Schofield 28263cf60b7SAlison Schofield if (mbox->size_out) { 28363cf60b7SAlison Schofield mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL); 28463cf60b7SAlison Schofield if (!mbox->payload_out) { 28563cf60b7SAlison Schofield kvfree(mbox->payload_in); 28663cf60b7SAlison Schofield return -ENOMEM; 28763cf60b7SAlison Schofield } 28863cf60b7SAlison Schofield } 28963cf60b7SAlison Schofield return 0; 29063cf60b7SAlison Schofield } 29163cf60b7SAlison Schofield 29263cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox) 29363cf60b7SAlison Schofield { 29463cf60b7SAlison Schofield kvfree(mbox->payload_in); 29563cf60b7SAlison Schofield kvfree(mbox->payload_out); 29663cf60b7SAlison Schofield } 29763cf60b7SAlison Schofield 2986dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, 2996dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3006dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3016dd0e5ccSAlison Schofield { 3026dd0e5ccSAlison Schofield if (send_cmd->raw.rsvd) 3036dd0e5ccSAlison Schofield return -EINVAL; 3046dd0e5ccSAlison Schofield 3056dd0e5ccSAlison Schofield /* 3066dd0e5ccSAlison Schofield * Unlike supported commands, the output size of RAW commands 3076dd0e5ccSAlison Schofield * gets passed along without further checking, so it must be 3086dd0e5ccSAlison Schofield * validated here. 3096dd0e5ccSAlison Schofield */ 3106dd0e5ccSAlison Schofield if (send_cmd->out.size > cxlds->payload_size) 3116dd0e5ccSAlison Schofield return -EINVAL; 3126dd0e5ccSAlison Schofield 3136dd0e5ccSAlison Schofield if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode)) 3146dd0e5ccSAlison Schofield return -EPERM; 3156dd0e5ccSAlison Schofield 31639ed8da4SAlison Schofield dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n"); 31739ed8da4SAlison Schofield 3186dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3196dd0e5ccSAlison Schofield .info = { 3206dd0e5ccSAlison Schofield .id = CXL_MEM_COMMAND_ID_RAW, 3216dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3226dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3236dd0e5ccSAlison Schofield }, 3246dd0e5ccSAlison Schofield .opcode = send_cmd->raw.opcode 3256dd0e5ccSAlison Schofield }; 3266dd0e5ccSAlison Schofield 3276dd0e5ccSAlison Schofield return 0; 3286dd0e5ccSAlison Schofield } 3296dd0e5ccSAlison Schofield 3306dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, 3316dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3326dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3336dd0e5ccSAlison Schofield { 3346dd0e5ccSAlison Schofield struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; 3356dd0e5ccSAlison Schofield const struct cxl_command_info *info = &c->info; 3366dd0e5ccSAlison Schofield 3376dd0e5ccSAlison Schofield if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK) 3386dd0e5ccSAlison Schofield return -EINVAL; 3396dd0e5ccSAlison Schofield 3406dd0e5ccSAlison Schofield if (send_cmd->rsvd) 3416dd0e5ccSAlison Schofield return -EINVAL; 3426dd0e5ccSAlison Schofield 3436dd0e5ccSAlison Schofield if (send_cmd->in.rsvd || send_cmd->out.rsvd) 3446dd0e5ccSAlison Schofield return -EINVAL; 3456dd0e5ccSAlison Schofield 3466dd0e5ccSAlison Schofield /* Check that the command is enabled for hardware */ 3476dd0e5ccSAlison Schofield if (!test_bit(info->id, cxlds->enabled_cmds)) 3486dd0e5ccSAlison Schofield return -ENOTTY; 3496dd0e5ccSAlison Schofield 3506dd0e5ccSAlison Schofield /* Check that the command is not claimed for exclusive kernel use */ 3516dd0e5ccSAlison Schofield if (test_bit(info->id, cxlds->exclusive_cmds)) 3526dd0e5ccSAlison Schofield return -EBUSY; 3536dd0e5ccSAlison Schofield 3546dd0e5ccSAlison Schofield /* Check the input buffer is the expected size */ 355e35f5718SVishal Verma if ((info->size_in != CXL_VARIABLE_PAYLOAD) && 356e35f5718SVishal Verma (info->size_in != send_cmd->in.size)) 3576dd0e5ccSAlison Schofield return -ENOMEM; 3586dd0e5ccSAlison Schofield 3596dd0e5ccSAlison Schofield /* Check the output buffer is at least large enough */ 360e35f5718SVishal Verma if ((info->size_out != CXL_VARIABLE_PAYLOAD) && 361e35f5718SVishal Verma (send_cmd->out.size < info->size_out)) 3626dd0e5ccSAlison Schofield return -ENOMEM; 3636dd0e5ccSAlison Schofield 3646dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3656dd0e5ccSAlison Schofield .info = { 3666dd0e5ccSAlison Schofield .id = info->id, 3676dd0e5ccSAlison Schofield .flags = info->flags, 3686dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3696dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3706dd0e5ccSAlison Schofield }, 3716dd0e5ccSAlison Schofield .opcode = c->opcode 3726dd0e5ccSAlison Schofield }; 3736dd0e5ccSAlison Schofield 3746dd0e5ccSAlison Schofield return 0; 3756dd0e5ccSAlison Schofield } 3766dd0e5ccSAlison Schofield 3774faf31b4SDan Williams /** 3784faf31b4SDan Williams * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. 3799ae016aeSAlison Schofield * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. 3805e2411aeSIra Weiny * @cxlds: The device data for the operation 3814faf31b4SDan Williams * @send_cmd: &struct cxl_send_command copied in from userspace. 3824faf31b4SDan Williams * 3834faf31b4SDan Williams * Return: 3844faf31b4SDan Williams * * %0 - @out_cmd is ready to send. 3854faf31b4SDan Williams * * %-ENOTTY - Invalid command specified. 3864faf31b4SDan Williams * * %-EINVAL - Reserved fields or invalid values were used. 3874faf31b4SDan Williams * * %-ENOMEM - Input or output buffer wasn't sized properly. 3884faf31b4SDan Williams * * %-EPERM - Attempted to use a protected command. 38912f3856aSDan Williams * * %-EBUSY - Kernel has claimed exclusive access to this opcode 3904faf31b4SDan Williams * 3912dd5600aSAlison Schofield * The result of this command is a fully validated command in @mbox_cmd that is 3924faf31b4SDan Williams * safe to send to the hardware. 3934faf31b4SDan Williams */ 3949ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd, 3959ae016aeSAlison Schofield struct cxl_dev_state *cxlds, 3962dd5600aSAlison Schofield const struct cxl_send_command *send_cmd) 3974faf31b4SDan Williams { 3982dd5600aSAlison Schofield struct cxl_mem_command mem_cmd; 3999ae016aeSAlison Schofield int rc; 4009ae016aeSAlison Schofield 4014faf31b4SDan Williams if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) 4024faf31b4SDan Williams return -ENOTTY; 4034faf31b4SDan Williams 4044faf31b4SDan Williams /* 4054faf31b4SDan Williams * The user can never specify an input payload larger than what hardware 4064faf31b4SDan Williams * supports, but output can be arbitrarily large (simply write out as 4074faf31b4SDan Williams * much data as the hardware provides). 4084faf31b4SDan Williams */ 4095e2411aeSIra Weiny if (send_cmd->in.size > cxlds->payload_size) 4104faf31b4SDan Williams return -EINVAL; 4114faf31b4SDan Williams 4126dd0e5ccSAlison Schofield /* Sanitize and construct a cxl_mem_command */ 4136dd0e5ccSAlison Schofield if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW) 4142dd5600aSAlison Schofield rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, cxlds); 4156dd0e5ccSAlison Schofield else 4162dd5600aSAlison Schofield rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, cxlds); 4179ae016aeSAlison Schofield 4189ae016aeSAlison Schofield if (rc) 4199ae016aeSAlison Schofield return rc; 4209ae016aeSAlison Schofield 4219ae016aeSAlison Schofield /* Sanitize and construct a cxl_mbox_cmd */ 4222dd5600aSAlison Schofield return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, mem_cmd.opcode, 4232dd5600aSAlison Schofield mem_cmd.info.size_in, mem_cmd.info.size_out, 4249ae016aeSAlison Schofield send_cmd->in.payload); 4254faf31b4SDan Williams } 4264faf31b4SDan Williams 4274faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd, 4284faf31b4SDan Williams struct cxl_mem_query_commands __user *q) 4294faf31b4SDan Williams { 4304faf31b4SDan Williams struct device *dev = &cxlmd->dev; 4314faf31b4SDan Williams struct cxl_mem_command *cmd; 4324faf31b4SDan Williams u32 n_commands; 4334faf31b4SDan Williams int j = 0; 4344faf31b4SDan Williams 4354faf31b4SDan Williams dev_dbg(dev, "Query IOCTL\n"); 4364faf31b4SDan Williams 4374faf31b4SDan Williams if (get_user(n_commands, &q->n_commands)) 4384faf31b4SDan Williams return -EFAULT; 4394faf31b4SDan Williams 4404faf31b4SDan Williams /* returns the total number if 0 elements are requested. */ 4414faf31b4SDan Williams if (n_commands == 0) 442ff56ab9eSDan Williams return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands); 4434faf31b4SDan Williams 4444faf31b4SDan Williams /* 4454faf31b4SDan Williams * otherwise, return max(n_commands, total commands) cxl_command_info 4464faf31b4SDan Williams * structures. 4474faf31b4SDan Williams */ 4484faf31b4SDan Williams cxl_for_each_cmd(cmd) { 4494faf31b4SDan Williams const struct cxl_command_info *info = &cmd->info; 4504faf31b4SDan Williams 4514faf31b4SDan Williams if (copy_to_user(&q->commands[j++], info, sizeof(*info))) 4524faf31b4SDan Williams return -EFAULT; 4534faf31b4SDan Williams 4544faf31b4SDan Williams if (j == n_commands) 4554faf31b4SDan Williams break; 4564faf31b4SDan Williams } 4574faf31b4SDan Williams 4584faf31b4SDan Williams return 0; 4594faf31b4SDan Williams } 4604faf31b4SDan Williams 4614faf31b4SDan Williams /** 4624faf31b4SDan Williams * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. 4635e2411aeSIra Weiny * @cxlds: The device data for the operation 464d97fe8eeSAlison Schofield * @mbox_cmd: The validated mailbox command. 4654faf31b4SDan Williams * @out_payload: Pointer to userspace's output payload. 4664faf31b4SDan Williams * @size_out: (Input) Max payload size to copy out. 4674faf31b4SDan Williams * (Output) Payload size hardware generated. 4684faf31b4SDan Williams * @retval: Hardware generated return code from the operation. 4694faf31b4SDan Williams * 4704faf31b4SDan Williams * Return: 4714faf31b4SDan Williams * * %0 - Mailbox transaction succeeded. This implies the mailbox 4724faf31b4SDan Williams * protocol completed successfully not that the operation itself 4734faf31b4SDan Williams * was successful. 4744faf31b4SDan Williams * * %-ENOMEM - Couldn't allocate a bounce buffer. 4754faf31b4SDan Williams * * %-EFAULT - Something happened with copy_to/from_user. 4764faf31b4SDan Williams * * %-EINTR - Mailbox acquisition interrupted. 4774faf31b4SDan Williams * * %-EXXX - Transaction level failures. 4784faf31b4SDan Williams * 479d97fe8eeSAlison Schofield * Dispatches a mailbox command on behalf of a userspace request. 480d97fe8eeSAlison Schofield * The output payload is copied to userspace. 4814faf31b4SDan Williams * 4824faf31b4SDan Williams * See cxl_send_cmd(). 4834faf31b4SDan Williams */ 4845e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, 485d97fe8eeSAlison Schofield struct cxl_mbox_cmd *mbox_cmd, 486d97fe8eeSAlison Schofield u64 out_payload, s32 *size_out, 487d97fe8eeSAlison Schofield u32 *retval) 4884faf31b4SDan Williams { 4895e2411aeSIra Weiny struct device *dev = cxlds->dev; 4904faf31b4SDan Williams int rc; 4914faf31b4SDan Williams 4924faf31b4SDan Williams dev_dbg(dev, 4934faf31b4SDan Williams "Submitting %s command for user\n" 4944faf31b4SDan Williams "\topcode: %x\n" 49582b8ba29SAlison Schofield "\tsize: %zx\n", 496d97fe8eeSAlison Schofield cxl_mem_opcode_to_name(mbox_cmd->opcode), 497d97fe8eeSAlison Schofield mbox_cmd->opcode, mbox_cmd->size_in); 4984faf31b4SDan Williams 499d97fe8eeSAlison Schofield rc = cxlds->mbox_send(cxlds, mbox_cmd); 5004faf31b4SDan Williams if (rc) 5014faf31b4SDan Williams goto out; 5024faf31b4SDan Williams 5034faf31b4SDan Williams /* 5044faf31b4SDan Williams * @size_out contains the max size that's allowed to be written back out 5054faf31b4SDan Williams * to userspace. While the payload may have written more output than 5064faf31b4SDan Williams * this it will have to be ignored. 5074faf31b4SDan Williams */ 508d97fe8eeSAlison Schofield if (mbox_cmd->size_out) { 509d97fe8eeSAlison Schofield dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out, 5104faf31b4SDan Williams "Invalid return size\n"); 5114faf31b4SDan Williams if (copy_to_user(u64_to_user_ptr(out_payload), 512d97fe8eeSAlison Schofield mbox_cmd->payload_out, mbox_cmd->size_out)) { 5134faf31b4SDan Williams rc = -EFAULT; 5144faf31b4SDan Williams goto out; 5154faf31b4SDan Williams } 5164faf31b4SDan Williams } 5174faf31b4SDan Williams 518d97fe8eeSAlison Schofield *size_out = mbox_cmd->size_out; 519d97fe8eeSAlison Schofield *retval = mbox_cmd->return_code; 5204faf31b4SDan Williams 5214faf31b4SDan Williams out: 522d97fe8eeSAlison Schofield cxl_mbox_cmd_dtor(mbox_cmd); 5234faf31b4SDan Williams return rc; 5244faf31b4SDan Williams } 5254faf31b4SDan Williams 5264faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s) 5274faf31b4SDan Williams { 5285e2411aeSIra Weiny struct cxl_dev_state *cxlds = cxlmd->cxlds; 5294faf31b4SDan Williams struct device *dev = &cxlmd->dev; 5304faf31b4SDan Williams struct cxl_send_command send; 5319ae016aeSAlison Schofield struct cxl_mbox_cmd mbox_cmd; 5324faf31b4SDan Williams int rc; 5334faf31b4SDan Williams 5344faf31b4SDan Williams dev_dbg(dev, "Send IOCTL\n"); 5354faf31b4SDan Williams 5364faf31b4SDan Williams if (copy_from_user(&send, s, sizeof(send))) 5374faf31b4SDan Williams return -EFAULT; 5384faf31b4SDan Williams 5392dd5600aSAlison Schofield rc = cxl_validate_cmd_from_user(&mbox_cmd, cxlmd->cxlds, &send); 5404faf31b4SDan Williams if (rc) 5414faf31b4SDan Williams return rc; 5424faf31b4SDan Williams 543d97fe8eeSAlison Schofield rc = handle_mailbox_cmd_from_user(cxlds, &mbox_cmd, send.out.payload, 544d97fe8eeSAlison Schofield &send.out.size, &send.retval); 5454faf31b4SDan Williams if (rc) 5464faf31b4SDan Williams return rc; 5474faf31b4SDan Williams 5484faf31b4SDan Williams if (copy_to_user(s, &send, sizeof(send))) 5494faf31b4SDan Williams return -EFAULT; 5504faf31b4SDan Williams 5514faf31b4SDan Williams return 0; 5524faf31b4SDan Williams } 5534faf31b4SDan Williams 5545e2411aeSIra Weiny static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8 *out) 5554faf31b4SDan Williams { 5564faf31b4SDan Williams u32 remaining = size; 5574faf31b4SDan Williams u32 offset = 0; 5584faf31b4SDan Williams 5594faf31b4SDan Williams while (remaining) { 5605e2411aeSIra Weiny u32 xfer_size = min_t(u32, remaining, cxlds->payload_size); 5615331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 5625331cdf4SDan Williams struct cxl_mbox_get_log log; 5634faf31b4SDan Williams int rc; 5644faf31b4SDan Williams 5655331cdf4SDan Williams log = (struct cxl_mbox_get_log) { 5665331cdf4SDan Williams .uuid = *uuid, 5675331cdf4SDan Williams .offset = cpu_to_le32(offset), 5685331cdf4SDan Williams .length = cpu_to_le32(xfer_size), 5695331cdf4SDan Williams }; 5705331cdf4SDan Williams 5715331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 5725331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_LOG, 5735331cdf4SDan Williams .size_in = sizeof(log), 5745331cdf4SDan Williams .payload_in = &log, 5755331cdf4SDan Williams .size_out = xfer_size, 5765331cdf4SDan Williams .payload_out = out, 5775331cdf4SDan Williams }; 5785331cdf4SDan Williams 5795331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 5804faf31b4SDan Williams if (rc < 0) 5814faf31b4SDan Williams return rc; 5824faf31b4SDan Williams 5834faf31b4SDan Williams out += xfer_size; 5844faf31b4SDan Williams remaining -= xfer_size; 5854faf31b4SDan Williams offset += xfer_size; 5864faf31b4SDan Williams } 5874faf31b4SDan Williams 5884faf31b4SDan Williams return 0; 5894faf31b4SDan Williams } 5904faf31b4SDan Williams 5914faf31b4SDan Williams /** 5924faf31b4SDan Williams * cxl_walk_cel() - Walk through the Command Effects Log. 5935e2411aeSIra Weiny * @cxlds: The device data for the operation 5944faf31b4SDan Williams * @size: Length of the Command Effects Log. 5954faf31b4SDan Williams * @cel: CEL 5964faf31b4SDan Williams * 5974faf31b4SDan Williams * Iterate over each entry in the CEL and determine if the driver supports the 5984faf31b4SDan Williams * command. If so, the command is enabled for the device and can be used later. 5994faf31b4SDan Williams */ 6005e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) 6014faf31b4SDan Williams { 60249be6dd8SDan Williams struct cxl_cel_entry *cel_entry; 6034faf31b4SDan Williams const int cel_entries = size / sizeof(*cel_entry); 6044faf31b4SDan Williams int i; 6054faf31b4SDan Williams 60649be6dd8SDan Williams cel_entry = (struct cxl_cel_entry *) cel; 6074faf31b4SDan Williams 6084faf31b4SDan Williams for (i = 0; i < cel_entries; i++) { 6094faf31b4SDan Williams u16 opcode = le16_to_cpu(cel_entry[i].opcode); 6104faf31b4SDan Williams struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 6114faf31b4SDan Williams 6124faf31b4SDan Williams if (!cmd) { 6135e2411aeSIra Weiny dev_dbg(cxlds->dev, 6144faf31b4SDan Williams "Opcode 0x%04x unsupported by driver", opcode); 6154faf31b4SDan Williams continue; 6164faf31b4SDan Williams } 6174faf31b4SDan Williams 6185e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 6194faf31b4SDan Williams } 6204faf31b4SDan Williams } 6214faf31b4SDan Williams 6225e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds) 6234faf31b4SDan Williams { 6244faf31b4SDan Williams struct cxl_mbox_get_supported_logs *ret; 6255331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 6264faf31b4SDan Williams int rc; 6274faf31b4SDan Williams 6285e2411aeSIra Weiny ret = kvmalloc(cxlds->payload_size, GFP_KERNEL); 6294faf31b4SDan Williams if (!ret) 6304faf31b4SDan Williams return ERR_PTR(-ENOMEM); 6314faf31b4SDan Williams 6325331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 6335331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS, 6345331cdf4SDan Williams .size_out = cxlds->payload_size, 6355331cdf4SDan Williams .payload_out = ret, 6362aeaf663SDan Williams /* At least the record number field must be valid */ 6372aeaf663SDan Williams .min_out = 2, 6385331cdf4SDan Williams }; 6395331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 6404faf31b4SDan Williams if (rc < 0) { 6414faf31b4SDan Williams kvfree(ret); 6424faf31b4SDan Williams return ERR_PTR(rc); 6434faf31b4SDan Williams } 6444faf31b4SDan Williams 6455331cdf4SDan Williams 6464faf31b4SDan Williams return ret; 6474faf31b4SDan Williams } 6484faf31b4SDan Williams 6494faf31b4SDan Williams enum { 6504faf31b4SDan Williams CEL_UUID, 6514faf31b4SDan Williams VENDOR_DEBUG_UUID, 6524faf31b4SDan Williams }; 6534faf31b4SDan Williams 6544faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */ 6554faf31b4SDan Williams static const uuid_t log_uuid[] = { 65649be6dd8SDan Williams [CEL_UUID] = DEFINE_CXL_CEL_UUID, 65749be6dd8SDan Williams [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID, 6584faf31b4SDan Williams }; 6594faf31b4SDan Williams 6604faf31b4SDan Williams /** 6615e2411aeSIra Weiny * cxl_enumerate_cmds() - Enumerate commands for a device. 6625e2411aeSIra Weiny * @cxlds: The device data for the operation 6634faf31b4SDan Williams * 6644faf31b4SDan Williams * Returns 0 if enumerate completed successfully. 6654faf31b4SDan Williams * 6664faf31b4SDan Williams * CXL devices have optional support for certain commands. This function will 6674faf31b4SDan Williams * determine the set of supported commands for the hardware and update the 6685e2411aeSIra Weiny * enabled_cmds bitmap in the @cxlds. 6694faf31b4SDan Williams */ 6705e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) 6714faf31b4SDan Williams { 6724faf31b4SDan Williams struct cxl_mbox_get_supported_logs *gsl; 6735e2411aeSIra Weiny struct device *dev = cxlds->dev; 6744faf31b4SDan Williams struct cxl_mem_command *cmd; 6754faf31b4SDan Williams int i, rc; 6764faf31b4SDan Williams 6775e2411aeSIra Weiny gsl = cxl_get_gsl(cxlds); 6784faf31b4SDan Williams if (IS_ERR(gsl)) 6794faf31b4SDan Williams return PTR_ERR(gsl); 6804faf31b4SDan Williams 6814faf31b4SDan Williams rc = -ENOENT; 6824faf31b4SDan Williams for (i = 0; i < le16_to_cpu(gsl->entries); i++) { 6834faf31b4SDan Williams u32 size = le32_to_cpu(gsl->entry[i].size); 6844faf31b4SDan Williams uuid_t uuid = gsl->entry[i].uuid; 6854faf31b4SDan Williams u8 *log; 6864faf31b4SDan Williams 6874faf31b4SDan Williams dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size); 6884faf31b4SDan Williams 6894faf31b4SDan Williams if (!uuid_equal(&uuid, &log_uuid[CEL_UUID])) 6904faf31b4SDan Williams continue; 6914faf31b4SDan Williams 6924faf31b4SDan Williams log = kvmalloc(size, GFP_KERNEL); 6934faf31b4SDan Williams if (!log) { 6944faf31b4SDan Williams rc = -ENOMEM; 6954faf31b4SDan Williams goto out; 6964faf31b4SDan Williams } 6974faf31b4SDan Williams 6985e2411aeSIra Weiny rc = cxl_xfer_log(cxlds, &uuid, size, log); 6994faf31b4SDan Williams if (rc) { 7004faf31b4SDan Williams kvfree(log); 7014faf31b4SDan Williams goto out; 7024faf31b4SDan Williams } 7034faf31b4SDan Williams 7045e2411aeSIra Weiny cxl_walk_cel(cxlds, size, log); 7054faf31b4SDan Williams kvfree(log); 7064faf31b4SDan Williams 7074faf31b4SDan Williams /* In case CEL was bogus, enable some default commands. */ 7084faf31b4SDan Williams cxl_for_each_cmd(cmd) 7094faf31b4SDan Williams if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE) 7105e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 7114faf31b4SDan Williams 7124faf31b4SDan Williams /* Found the required CEL */ 7134faf31b4SDan Williams rc = 0; 7144faf31b4SDan Williams } 7154faf31b4SDan Williams out: 7164faf31b4SDan Williams kvfree(gsl); 7174faf31b4SDan Williams return rc; 7184faf31b4SDan Williams } 719affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL); 7204faf31b4SDan Williams 721*d54a531aSIra Weiny /* 722*d54a531aSIra Weiny * General Media Event Record 723*d54a531aSIra Weiny * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43 724*d54a531aSIra Weiny */ 725*d54a531aSIra Weiny static const uuid_t gen_media_event_uuid = 726*d54a531aSIra Weiny UUID_INIT(0xfbcd0a77, 0xc260, 0x417f, 727*d54a531aSIra Weiny 0x85, 0xa9, 0x08, 0x8b, 0x16, 0x21, 0xeb, 0xa6); 728*d54a531aSIra Weiny 729*d54a531aSIra Weiny static void cxl_event_trace_record(const struct device *dev, 730*d54a531aSIra Weiny enum cxl_event_log_type type, 731*d54a531aSIra Weiny struct cxl_event_record_raw *record) 732*d54a531aSIra Weiny { 733*d54a531aSIra Weiny uuid_t *id = &record->hdr.id; 734*d54a531aSIra Weiny 735*d54a531aSIra Weiny if (uuid_equal(id, &gen_media_event_uuid)) { 736*d54a531aSIra Weiny struct cxl_event_gen_media *rec = 737*d54a531aSIra Weiny (struct cxl_event_gen_media *)record; 738*d54a531aSIra Weiny 739*d54a531aSIra Weiny trace_cxl_general_media(dev, type, rec); 740*d54a531aSIra Weiny } else { 741*d54a531aSIra Weiny /* For unknown record types print just the header */ 742*d54a531aSIra Weiny trace_cxl_generic_event(dev, type, record); 743*d54a531aSIra Weiny } 744*d54a531aSIra Weiny } 745*d54a531aSIra Weiny 7466ebe28f9SIra Weiny static int cxl_clear_event_record(struct cxl_dev_state *cxlds, 7476ebe28f9SIra Weiny enum cxl_event_log_type log, 7486ebe28f9SIra Weiny struct cxl_get_event_payload *get_pl) 7496ebe28f9SIra Weiny { 7506ebe28f9SIra Weiny struct cxl_mbox_clear_event_payload *payload; 7516ebe28f9SIra Weiny u16 total = le16_to_cpu(get_pl->record_count); 7526ebe28f9SIra Weiny u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES; 7536ebe28f9SIra Weiny size_t pl_size = struct_size(payload, handles, max_handles); 7546ebe28f9SIra Weiny struct cxl_mbox_cmd mbox_cmd; 7556ebe28f9SIra Weiny u16 cnt; 7566ebe28f9SIra Weiny int rc = 0; 7576ebe28f9SIra Weiny int i; 7586ebe28f9SIra Weiny 7596ebe28f9SIra Weiny /* Payload size may limit the max handles */ 7606ebe28f9SIra Weiny if (pl_size > cxlds->payload_size) { 7616ebe28f9SIra Weiny max_handles = (cxlds->payload_size - sizeof(*payload)) / 7626ebe28f9SIra Weiny sizeof(__le16); 7636ebe28f9SIra Weiny pl_size = struct_size(payload, handles, max_handles); 7646ebe28f9SIra Weiny } 7656ebe28f9SIra Weiny 7666ebe28f9SIra Weiny payload = kvzalloc(pl_size, GFP_KERNEL); 7676ebe28f9SIra Weiny if (!payload) 7686ebe28f9SIra Weiny return -ENOMEM; 7696ebe28f9SIra Weiny 7706ebe28f9SIra Weiny *payload = (struct cxl_mbox_clear_event_payload) { 7716ebe28f9SIra Weiny .event_log = log, 7726ebe28f9SIra Weiny }; 7736ebe28f9SIra Weiny 7746ebe28f9SIra Weiny mbox_cmd = (struct cxl_mbox_cmd) { 7756ebe28f9SIra Weiny .opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD, 7766ebe28f9SIra Weiny .payload_in = payload, 7776ebe28f9SIra Weiny .size_in = pl_size, 7786ebe28f9SIra Weiny }; 7796ebe28f9SIra Weiny 7806ebe28f9SIra Weiny /* 7816ebe28f9SIra Weiny * Clear Event Records uses u8 for the handle cnt while Get Event 7826ebe28f9SIra Weiny * Record can return up to 0xffff records. 7836ebe28f9SIra Weiny */ 7846ebe28f9SIra Weiny i = 0; 7856ebe28f9SIra Weiny for (cnt = 0; cnt < total; cnt++) { 7866ebe28f9SIra Weiny payload->handles[i++] = get_pl->records[cnt].hdr.handle; 7876ebe28f9SIra Weiny dev_dbg(cxlds->dev, "Event log '%d': Clearing %u\n", 7886ebe28f9SIra Weiny log, le16_to_cpu(payload->handles[i])); 7896ebe28f9SIra Weiny 7906ebe28f9SIra Weiny if (i == max_handles) { 7916ebe28f9SIra Weiny payload->nr_recs = i; 7926ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 7936ebe28f9SIra Weiny if (rc) 7946ebe28f9SIra Weiny goto free_pl; 7956ebe28f9SIra Weiny i = 0; 7966ebe28f9SIra Weiny } 7976ebe28f9SIra Weiny } 7986ebe28f9SIra Weiny 7996ebe28f9SIra Weiny /* Clear what is left if any */ 8006ebe28f9SIra Weiny if (i) { 8016ebe28f9SIra Weiny payload->nr_recs = i; 8026ebe28f9SIra Weiny mbox_cmd.size_in = struct_size(payload, handles, i); 8036ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 8046ebe28f9SIra Weiny if (rc) 8056ebe28f9SIra Weiny goto free_pl; 8066ebe28f9SIra Weiny } 8076ebe28f9SIra Weiny 8086ebe28f9SIra Weiny free_pl: 8096ebe28f9SIra Weiny kvfree(payload); 8106ebe28f9SIra Weiny return rc; 8116ebe28f9SIra Weiny } 8126ebe28f9SIra Weiny 8136ebe28f9SIra Weiny static void cxl_mem_get_records_log(struct cxl_dev_state *cxlds, 8146ebe28f9SIra Weiny enum cxl_event_log_type type) 8156ebe28f9SIra Weiny { 8166ebe28f9SIra Weiny struct cxl_get_event_payload *payload; 8176ebe28f9SIra Weiny struct cxl_mbox_cmd mbox_cmd; 8186ebe28f9SIra Weiny u8 log_type = type; 8196ebe28f9SIra Weiny u16 nr_rec; 8206ebe28f9SIra Weiny 8216ebe28f9SIra Weiny mutex_lock(&cxlds->event.log_lock); 8226ebe28f9SIra Weiny payload = cxlds->event.buf; 8236ebe28f9SIra Weiny 8246ebe28f9SIra Weiny mbox_cmd = (struct cxl_mbox_cmd) { 8256ebe28f9SIra Weiny .opcode = CXL_MBOX_OP_GET_EVENT_RECORD, 8266ebe28f9SIra Weiny .payload_in = &log_type, 8276ebe28f9SIra Weiny .size_in = sizeof(log_type), 8286ebe28f9SIra Weiny .payload_out = payload, 8296ebe28f9SIra Weiny .size_out = cxlds->payload_size, 8306ebe28f9SIra Weiny .min_out = struct_size(payload, records, 0), 8316ebe28f9SIra Weiny }; 8326ebe28f9SIra Weiny 8336ebe28f9SIra Weiny do { 8346ebe28f9SIra Weiny int rc, i; 8356ebe28f9SIra Weiny 8366ebe28f9SIra Weiny rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 8376ebe28f9SIra Weiny if (rc) { 8386ebe28f9SIra Weiny dev_err_ratelimited(cxlds->dev, 8396ebe28f9SIra Weiny "Event log '%d': Failed to query event records : %d", 8406ebe28f9SIra Weiny type, rc); 8416ebe28f9SIra Weiny break; 8426ebe28f9SIra Weiny } 8436ebe28f9SIra Weiny 8446ebe28f9SIra Weiny nr_rec = le16_to_cpu(payload->record_count); 8456ebe28f9SIra Weiny if (!nr_rec) 8466ebe28f9SIra Weiny break; 8476ebe28f9SIra Weiny 8486ebe28f9SIra Weiny for (i = 0; i < nr_rec; i++) 849*d54a531aSIra Weiny cxl_event_trace_record(cxlds->dev, type, 8506ebe28f9SIra Weiny &payload->records[i]); 8516ebe28f9SIra Weiny 8526ebe28f9SIra Weiny if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW) 8536ebe28f9SIra Weiny trace_cxl_overflow(cxlds->dev, type, payload); 8546ebe28f9SIra Weiny 8556ebe28f9SIra Weiny rc = cxl_clear_event_record(cxlds, type, payload); 8566ebe28f9SIra Weiny if (rc) { 8576ebe28f9SIra Weiny dev_err_ratelimited(cxlds->dev, 8586ebe28f9SIra Weiny "Event log '%d': Failed to clear events : %d", 8596ebe28f9SIra Weiny type, rc); 8606ebe28f9SIra Weiny break; 8616ebe28f9SIra Weiny } 8626ebe28f9SIra Weiny } while (nr_rec); 8636ebe28f9SIra Weiny 8646ebe28f9SIra Weiny mutex_unlock(&cxlds->event.log_lock); 8656ebe28f9SIra Weiny } 8666ebe28f9SIra Weiny 8676ebe28f9SIra Weiny /** 8686ebe28f9SIra Weiny * cxl_mem_get_event_records - Get Event Records from the device 8696ebe28f9SIra Weiny * @cxlds: The device data for the operation 8706ebe28f9SIra Weiny * 8716ebe28f9SIra Weiny * Retrieve all event records available on the device, report them as trace 8726ebe28f9SIra Weiny * events, and clear them. 8736ebe28f9SIra Weiny * 8746ebe28f9SIra Weiny * See CXL rev 3.0 @8.2.9.2.2 Get Event Records 8756ebe28f9SIra Weiny * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records 8766ebe28f9SIra Weiny */ 8776ebe28f9SIra Weiny void cxl_mem_get_event_records(struct cxl_dev_state *cxlds, u32 status) 8786ebe28f9SIra Weiny { 8796ebe28f9SIra Weiny dev_dbg(cxlds->dev, "Reading event logs: %x\n", status); 8806ebe28f9SIra Weiny 8816ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_FATAL) 8826ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_FATAL); 8836ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_FAIL) 8846ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_FAIL); 8856ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_WARN) 8866ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_WARN); 8876ebe28f9SIra Weiny if (status & CXLDEV_EVENT_STATUS_INFO) 8886ebe28f9SIra Weiny cxl_mem_get_records_log(cxlds, CXL_EVENT_TYPE_INFO); 8896ebe28f9SIra Weiny } 8906ebe28f9SIra Weiny EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, CXL); 8916ebe28f9SIra Weiny 8924faf31b4SDan Williams /** 8934faf31b4SDan Williams * cxl_mem_get_partition_info - Get partition info 8945e2411aeSIra Weiny * @cxlds: The device data for the operation 8954faf31b4SDan Williams * 8964faf31b4SDan Williams * Retrieve the current partition info for the device specified. The active 8974faf31b4SDan Williams * values are the current capacity in bytes. If not 0, the 'next' values are 8984faf31b4SDan Williams * the pending values, in bytes, which take affect on next cold reset. 8994faf31b4SDan Williams * 9004faf31b4SDan Williams * Return: 0 if no error: or the result of the mailbox command. 9014faf31b4SDan Williams * 9024faf31b4SDan Williams * See CXL @8.2.9.5.2.1 Get Partition Info 9034faf31b4SDan Williams */ 9045e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds) 9054faf31b4SDan Williams { 906e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info pi; 9075331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 9084faf31b4SDan Williams int rc; 9094faf31b4SDan Williams 9105331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 9115331cdf4SDan Williams .opcode = CXL_MBOX_OP_GET_PARTITION_INFO, 9125331cdf4SDan Williams .size_out = sizeof(pi), 9135331cdf4SDan Williams .payload_out = &pi, 9145331cdf4SDan Williams }; 9155331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 9164faf31b4SDan Williams if (rc) 9174faf31b4SDan Williams return rc; 9184faf31b4SDan Williams 9195e2411aeSIra Weiny cxlds->active_volatile_bytes = 9204faf31b4SDan Williams le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 9215e2411aeSIra Weiny cxlds->active_persistent_bytes = 9224faf31b4SDan Williams le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER; 9235e2411aeSIra Weiny cxlds->next_volatile_bytes = 9244faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 9255e2411aeSIra Weiny cxlds->next_persistent_bytes = 9264faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 9274faf31b4SDan Williams 9284faf31b4SDan Williams return 0; 9294faf31b4SDan Williams } 9304faf31b4SDan Williams 9314faf31b4SDan Williams /** 9325e2411aeSIra Weiny * cxl_dev_state_identify() - Send the IDENTIFY command to the device. 9335e2411aeSIra Weiny * @cxlds: The device data for the operation 9344faf31b4SDan Williams * 9354faf31b4SDan Williams * Return: 0 if identify was executed successfully. 9364faf31b4SDan Williams * 9374faf31b4SDan Williams * This will dispatch the identify command to the device and on success populate 9384faf31b4SDan Williams * structures to be exported to sysfs. 9394faf31b4SDan Williams */ 9405e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds) 9414faf31b4SDan Williams { 9424faf31b4SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 94349be6dd8SDan Williams struct cxl_mbox_identify id; 9445331cdf4SDan Williams struct cxl_mbox_cmd mbox_cmd; 9454faf31b4SDan Williams int rc; 9464faf31b4SDan Williams 9475331cdf4SDan Williams mbox_cmd = (struct cxl_mbox_cmd) { 9485331cdf4SDan Williams .opcode = CXL_MBOX_OP_IDENTIFY, 9495331cdf4SDan Williams .size_out = sizeof(id), 9505331cdf4SDan Williams .payload_out = &id, 9515331cdf4SDan Williams }; 9525331cdf4SDan Williams rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 9534faf31b4SDan Williams if (rc < 0) 9544faf31b4SDan Williams return rc; 9554faf31b4SDan Williams 9565e2411aeSIra Weiny cxlds->total_bytes = 9574faf31b4SDan Williams le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER; 9585e2411aeSIra Weiny cxlds->volatile_only_bytes = 9594faf31b4SDan Williams le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER; 9605e2411aeSIra Weiny cxlds->persistent_only_bytes = 9614faf31b4SDan Williams le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER; 9625e2411aeSIra Weiny cxlds->partition_align_bytes = 9634faf31b4SDan Williams le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER; 9644faf31b4SDan Williams 9655e2411aeSIra Weiny cxlds->lsa_size = le32_to_cpu(id.lsa_size); 9665e2411aeSIra Weiny memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision)); 9674faf31b4SDan Williams 9684faf31b4SDan Williams return 0; 9694faf31b4SDan Williams } 970affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL); 9714faf31b4SDan Williams 972d3b75029SDan Williams static int add_dpa_res(struct device *dev, struct resource *parent, 973d3b75029SDan Williams struct resource *res, resource_size_t start, 974d3b75029SDan Williams resource_size_t size, const char *type) 9754faf31b4SDan Williams { 9764faf31b4SDan Williams int rc; 9774faf31b4SDan Williams 978d3b75029SDan Williams res->name = type; 979d3b75029SDan Williams res->start = start; 980d3b75029SDan Williams res->end = start + size - 1; 981d3b75029SDan Williams res->flags = IORESOURCE_MEM; 982d3b75029SDan Williams if (resource_size(res) == 0) { 983d3b75029SDan Williams dev_dbg(dev, "DPA(%s): no capacity\n", res->name); 9844faf31b4SDan Williams return 0; 9854faf31b4SDan Williams } 986d3b75029SDan Williams rc = request_resource(parent, res); 987d3b75029SDan Williams if (rc) { 988d3b75029SDan Williams dev_err(dev, "DPA(%s): failed to track %pr (%d)\n", res->name, 989d3b75029SDan Williams res, rc); 990d3b75029SDan Williams return rc; 991d3b75029SDan Williams } 992d3b75029SDan Williams 993d3b75029SDan Williams dev_dbg(dev, "DPA(%s): %pr\n", res->name, res); 994d3b75029SDan Williams 995d3b75029SDan Williams return 0; 996d3b75029SDan Williams } 997d3b75029SDan Williams 998d3b75029SDan Williams int cxl_mem_create_range_info(struct cxl_dev_state *cxlds) 999d3b75029SDan Williams { 1000d3b75029SDan Williams struct device *dev = cxlds->dev; 1001d3b75029SDan Williams int rc; 1002d3b75029SDan Williams 1003d3b75029SDan Williams cxlds->dpa_res = 1004d3b75029SDan Williams (struct resource)DEFINE_RES_MEM(0, cxlds->total_bytes); 1005d3b75029SDan Williams 1006d3b75029SDan Williams if (cxlds->partition_align_bytes == 0) { 1007d3b75029SDan Williams rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0, 1008d3b75029SDan Williams cxlds->volatile_only_bytes, "ram"); 1009d3b75029SDan Williams if (rc) 1010d3b75029SDan Williams return rc; 1011d3b75029SDan Williams return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res, 1012d3b75029SDan Williams cxlds->volatile_only_bytes, 1013d3b75029SDan Williams cxlds->persistent_only_bytes, "pmem"); 1014d3b75029SDan Williams } 10154faf31b4SDan Williams 10165e2411aeSIra Weiny rc = cxl_mem_get_partition_info(cxlds); 10174faf31b4SDan Williams if (rc) { 1018d3b75029SDan Williams dev_err(dev, "Failed to query partition information\n"); 10194faf31b4SDan Williams return rc; 10204faf31b4SDan Williams } 10214faf31b4SDan Williams 1022d3b75029SDan Williams rc = add_dpa_res(dev, &cxlds->dpa_res, &cxlds->ram_res, 0, 1023d3b75029SDan Williams cxlds->active_volatile_bytes, "ram"); 1024d3b75029SDan Williams if (rc) 1025d3b75029SDan Williams return rc; 1026d3b75029SDan Williams return add_dpa_res(dev, &cxlds->dpa_res, &cxlds->pmem_res, 1027d3b75029SDan Williams cxlds->active_volatile_bytes, 1028d3b75029SDan Williams cxlds->active_persistent_bytes, "pmem"); 10294faf31b4SDan Williams } 1030affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL); 10314faf31b4SDan Williams 10325e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev) 10334faf31b4SDan Williams { 10345e2411aeSIra Weiny struct cxl_dev_state *cxlds; 10354faf31b4SDan Williams 10365e2411aeSIra Weiny cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL); 10375e2411aeSIra Weiny if (!cxlds) { 10384faf31b4SDan Williams dev_err(dev, "No memory available\n"); 10394faf31b4SDan Williams return ERR_PTR(-ENOMEM); 10404faf31b4SDan Williams } 10414faf31b4SDan Williams 10425e2411aeSIra Weiny mutex_init(&cxlds->mbox_mutex); 10436ebe28f9SIra Weiny mutex_init(&cxlds->event.log_lock); 10445e2411aeSIra Weiny cxlds->dev = dev; 10454faf31b4SDan Williams 10465e2411aeSIra Weiny return cxlds; 10474faf31b4SDan Williams } 1048affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL); 10494faf31b4SDan Williams 10504faf31b4SDan Williams void __init cxl_mbox_init(void) 10514faf31b4SDan Williams { 10524faf31b4SDan Williams struct dentry *mbox_debugfs; 10534faf31b4SDan Williams 10549b99ecf5SDan Williams mbox_debugfs = cxl_debugfs_create_dir("mbox"); 10554faf31b4SDan Williams debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs, 10564faf31b4SDan Williams &cxl_raw_allow_all); 10574faf31b4SDan Williams } 1058