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" 114faf31b4SDan Williams 124faf31b4SDan Williams static bool cxl_raw_allow_all; 134faf31b4SDan Williams 144faf31b4SDan Williams /** 154faf31b4SDan Williams * DOC: cxl mbox 164faf31b4SDan Williams * 174faf31b4SDan Williams * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The 184faf31b4SDan Williams * implementation is used by the cxl_pci driver to initialize the device 194faf31b4SDan Williams * and implement the cxl_mem.h IOCTL UAPI. It also implements the 204faf31b4SDan Williams * backend of the cxl_pmem_ctl() transport for LIBNVDIMM. 214faf31b4SDan Williams */ 224faf31b4SDan Williams 234faf31b4SDan Williams #define cxl_for_each_cmd(cmd) \ 244faf31b4SDan Williams for ((cmd) = &cxl_mem_commands[0]; \ 254faf31b4SDan Williams ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++) 264faf31b4SDan Williams 274faf31b4SDan Williams #define CXL_CMD(_id, sin, sout, _flags) \ 284faf31b4SDan Williams [CXL_MEM_COMMAND_ID_##_id] = { \ 294faf31b4SDan Williams .info = { \ 304faf31b4SDan Williams .id = CXL_MEM_COMMAND_ID_##_id, \ 314faf31b4SDan Williams .size_in = sin, \ 324faf31b4SDan Williams .size_out = sout, \ 334faf31b4SDan Williams }, \ 344faf31b4SDan Williams .opcode = CXL_MBOX_OP_##_id, \ 354faf31b4SDan Williams .flags = _flags, \ 364faf31b4SDan Williams } 374faf31b4SDan Williams 3826f89535SAlison Schofield #define CXL_VARIABLE_PAYLOAD ~0U 394faf31b4SDan Williams /* 404faf31b4SDan Williams * This table defines the supported mailbox commands for the driver. This table 414faf31b4SDan Williams * is made up of a UAPI structure. Non-negative values as parameters in the 424faf31b4SDan Williams * table will be validated against the user's input. For example, if size_in is 434faf31b4SDan Williams * 0, and the user passed in 1, it is an error. 444faf31b4SDan Williams */ 454faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = { 464faf31b4SDan Williams CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE), 474faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS 4826f89535SAlison Schofield CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0), 494faf31b4SDan Williams #endif 5026f89535SAlison Schofield CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 514faf31b4SDan Williams CXL_CMD(GET_FW_INFO, 0, 0x50, 0), 524faf31b4SDan Williams CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0), 5326f89535SAlison Schofield CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0), 544faf31b4SDan Williams CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0), 5526f89535SAlison Schofield CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 564faf31b4SDan Williams CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0), 5726f89535SAlison Schofield CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0), 584faf31b4SDan Williams CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0), 594faf31b4SDan Williams CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0), 604faf31b4SDan Williams CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0), 614faf31b4SDan Williams CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0), 6226f89535SAlison Schofield CXL_CMD(GET_POISON, 0x10, CXL_VARIABLE_PAYLOAD, 0), 634faf31b4SDan Williams CXL_CMD(INJECT_POISON, 0x8, 0, 0), 644faf31b4SDan Williams CXL_CMD(CLEAR_POISON, 0x48, 0, 0), 654faf31b4SDan Williams CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0), 664faf31b4SDan Williams CXL_CMD(SCAN_MEDIA, 0x11, 0, 0), 6726f89535SAlison Schofield CXL_CMD(GET_SCAN_MEDIA, 0, CXL_VARIABLE_PAYLOAD, 0), 684faf31b4SDan Williams }; 694faf31b4SDan Williams 704faf31b4SDan Williams /* 714faf31b4SDan Williams * Commands that RAW doesn't permit. The rationale for each: 724faf31b4SDan Williams * 734faf31b4SDan Williams * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / 744faf31b4SDan Williams * coordination of transaction timeout values at the root bridge level. 754faf31b4SDan Williams * 764faf31b4SDan Williams * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live 774faf31b4SDan Williams * and needs to be coordinated with HDM updates. 784faf31b4SDan Williams * 794faf31b4SDan Williams * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the 804faf31b4SDan Williams * driver and any writes from userspace invalidates those contents. 814faf31b4SDan Williams * 824faf31b4SDan Williams * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes 834faf31b4SDan Williams * to the device after it is marked clean, userspace can not make that 844faf31b4SDan Williams * assertion. 854faf31b4SDan Williams * 864faf31b4SDan Williams * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that 874faf31b4SDan Williams * is kept up to date with patrol notifications and error management. 884faf31b4SDan Williams */ 894faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = { 904faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW, 914faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO, 924faf31b4SDan Williams CXL_MBOX_OP_SET_LSA, 934faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE, 944faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA, 954faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA, 964faf31b4SDan Williams }; 974faf31b4SDan Williams 984faf31b4SDan Williams /* 994faf31b4SDan Williams * Command sets that RAW doesn't permit. All opcodes in this set are 1004faf31b4SDan Williams * disabled because they pass plain text security payloads over the 1014faf31b4SDan Williams * user/kernel boundary. This functionality is intended to be wrapped 1024faf31b4SDan Williams * behind the keys ABI which allows for encrypted payloads in the UAPI 1034faf31b4SDan Williams */ 1044faf31b4SDan Williams static u8 security_command_sets[] = { 1054faf31b4SDan Williams 0x44, /* Sanitize */ 1064faf31b4SDan Williams 0x45, /* Persistent Memory Data-at-rest Security */ 1074faf31b4SDan Williams 0x46, /* Security Passthrough */ 1084faf31b4SDan Williams }; 1094faf31b4SDan Williams 1104faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode) 1114faf31b4SDan Williams { 1124faf31b4SDan Williams int i; 1134faf31b4SDan Williams 1144faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(security_command_sets); i++) 1154faf31b4SDan Williams if (security_command_sets[i] == (opcode >> 8)) 1164faf31b4SDan Williams return true; 1174faf31b4SDan Williams return false; 1184faf31b4SDan Williams } 1194faf31b4SDan Williams 1204faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) 1214faf31b4SDan Williams { 1224faf31b4SDan Williams struct cxl_mem_command *c; 1234faf31b4SDan Williams 1244faf31b4SDan Williams cxl_for_each_cmd(c) 1254faf31b4SDan Williams if (c->opcode == opcode) 1264faf31b4SDan Williams return c; 1274faf31b4SDan Williams 1284faf31b4SDan Williams return NULL; 1294faf31b4SDan Williams } 1304faf31b4SDan Williams 13182b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode) 13282b8ba29SAlison Schofield { 13382b8ba29SAlison Schofield struct cxl_mem_command *c; 13482b8ba29SAlison Schofield 13582b8ba29SAlison Schofield c = cxl_mem_find_command(opcode); 13682b8ba29SAlison Schofield if (!c) 13782b8ba29SAlison Schofield return NULL; 13882b8ba29SAlison Schofield 13982b8ba29SAlison Schofield return cxl_command_names[c->info.id].name; 14082b8ba29SAlison Schofield } 14182b8ba29SAlison Schofield 1424faf31b4SDan Williams /** 1435e2411aeSIra Weiny * cxl_mbox_send_cmd() - Send a mailbox command to a device. 1445e2411aeSIra Weiny * @cxlds: The device data for the operation 1454faf31b4SDan Williams * @opcode: Opcode for the mailbox command. 1464faf31b4SDan Williams * @in: The input payload for the mailbox command. 1474faf31b4SDan Williams * @in_size: The length of the input payload 1484faf31b4SDan Williams * @out: Caller allocated buffer for the output. 1494faf31b4SDan Williams * @out_size: Expected size of output. 1504faf31b4SDan Williams * 151ee92c7e2SDavidlohr Bueso * Context: Any context. 1524faf31b4SDan Williams * Return: 1534faf31b4SDan Williams * * %>=0 - Number of bytes returned in @out. 1544faf31b4SDan Williams * * %-E2BIG - Payload is too large for hardware. 1554faf31b4SDan Williams * * %-EBUSY - Couldn't acquire exclusive mailbox access. 1564faf31b4SDan Williams * * %-EFAULT - Hardware error occurred. 1574faf31b4SDan Williams * * %-ENXIO - Command completed, but device reported an error. 1584faf31b4SDan Williams * * %-EIO - Unexpected output size. 1594faf31b4SDan Williams * 1604faf31b4SDan Williams * Mailbox commands may execute successfully yet the device itself reported an 1614faf31b4SDan Williams * error. While this distinction can be useful for commands from userspace, the 1624faf31b4SDan Williams * kernel will only be able to use results when both are successful. 1634faf31b4SDan Williams */ 1645e2411aeSIra Weiny int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in, 1654faf31b4SDan Williams size_t in_size, void *out, size_t out_size) 1664faf31b4SDan Williams { 1674faf31b4SDan Williams const struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 1684faf31b4SDan Williams struct cxl_mbox_cmd mbox_cmd = { 1694faf31b4SDan Williams .opcode = opcode, 1704faf31b4SDan Williams .payload_in = in, 1714faf31b4SDan Williams .size_in = in_size, 1724faf31b4SDan Williams .size_out = out_size, 1734faf31b4SDan Williams .payload_out = out, 1744faf31b4SDan Williams }; 1754faf31b4SDan Williams int rc; 1764faf31b4SDan Williams 1775e2411aeSIra Weiny if (out_size > cxlds->payload_size) 1784faf31b4SDan Williams return -E2BIG; 1794faf31b4SDan Williams 1805e2411aeSIra Weiny rc = cxlds->mbox_send(cxlds, &mbox_cmd); 1814faf31b4SDan Williams if (rc) 1824faf31b4SDan Williams return rc; 1834faf31b4SDan Williams 18492fcc1abSDavidlohr Bueso if (mbox_cmd.return_code != CXL_MBOX_CMD_RC_SUCCESS) 185c43e036dSDavidlohr Bueso return cxl_mbox_cmd_rc2errno(&mbox_cmd); 1864faf31b4SDan Williams 1874faf31b4SDan Williams /* 1884faf31b4SDan Williams * Variable sized commands can't be validated and so it's up to the 1894faf31b4SDan Williams * caller to do that if they wish. 1904faf31b4SDan Williams */ 19126f89535SAlison Schofield if (cmd->info.size_out != CXL_VARIABLE_PAYLOAD) { 19226f89535SAlison Schofield if (mbox_cmd.size_out != out_size) 1934faf31b4SDan Williams return -EIO; 19426f89535SAlison Schofield } 1954faf31b4SDan Williams return 0; 1964faf31b4SDan Williams } 197affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mbox_send_cmd, CXL); 1984faf31b4SDan Williams 1994faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode) 2004faf31b4SDan Williams { 2014faf31b4SDan Williams int i; 2024faf31b4SDan Williams 2034faf31b4SDan Williams if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS)) 2044faf31b4SDan Williams return false; 2054faf31b4SDan Williams 2064faf31b4SDan Williams if (security_locked_down(LOCKDOWN_PCI_ACCESS)) 2074faf31b4SDan Williams return false; 2084faf31b4SDan Williams 2094faf31b4SDan Williams if (cxl_raw_allow_all) 2104faf31b4SDan Williams return true; 2114faf31b4SDan Williams 2124faf31b4SDan Williams if (cxl_is_security_command(opcode)) 2134faf31b4SDan Williams return false; 2144faf31b4SDan Williams 2154faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++) 2164faf31b4SDan Williams if (cxl_disabled_raw_commands[i] == opcode) 2174faf31b4SDan Williams return false; 2184faf31b4SDan Williams 2194faf31b4SDan Williams return true; 2204faf31b4SDan Williams } 2214faf31b4SDan Williams 2226179045cSAlison Schofield /** 2236179045cSAlison Schofield * cxl_payload_from_user_allowed() - Check contents of in_payload. 2246179045cSAlison Schofield * @opcode: The mailbox command opcode. 2256179045cSAlison Schofield * @payload_in: Pointer to the input payload passed in from user space. 2266179045cSAlison Schofield * 2276179045cSAlison Schofield * Return: 2286179045cSAlison Schofield * * true - payload_in passes check for @opcode. 2296179045cSAlison Schofield * * false - payload_in contains invalid or unsupported values. 2306179045cSAlison Schofield * 2316179045cSAlison Schofield * The driver may inspect payload contents before sending a mailbox 2326179045cSAlison Schofield * command from user space to the device. The intent is to reject 2336179045cSAlison Schofield * commands with input payloads that are known to be unsafe. This 2346179045cSAlison Schofield * check is not intended to replace the users careful selection of 2356179045cSAlison Schofield * mailbox command parameters and makes no guarantee that the user 2366179045cSAlison Schofield * command will succeed, nor that it is appropriate. 2376179045cSAlison Schofield * 2386179045cSAlison Schofield * The specific checks are determined by the opcode. 2396179045cSAlison Schofield */ 2406179045cSAlison Schofield static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in) 2416179045cSAlison Schofield { 2426179045cSAlison Schofield switch (opcode) { 2436179045cSAlison Schofield case CXL_MBOX_OP_SET_PARTITION_INFO: { 2446179045cSAlison Schofield struct cxl_mbox_set_partition_info *pi = payload_in; 2456179045cSAlison Schofield 246*35e01667SDan Carpenter if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG) 2476179045cSAlison Schofield return false; 2486179045cSAlison Schofield break; 2496179045cSAlison Schofield } 2506179045cSAlison Schofield default: 2516179045cSAlison Schofield break; 2526179045cSAlison Schofield } 2536179045cSAlison Schofield return true; 2546179045cSAlison Schofield } 2556179045cSAlison Schofield 25663cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, 25763cf60b7SAlison Schofield struct cxl_dev_state *cxlds, u16 opcode, 25863cf60b7SAlison Schofield size_t in_size, size_t out_size, u64 in_payload) 25963cf60b7SAlison Schofield { 26063cf60b7SAlison Schofield *mbox = (struct cxl_mbox_cmd) { 26163cf60b7SAlison Schofield .opcode = opcode, 26263cf60b7SAlison Schofield .size_in = in_size, 26363cf60b7SAlison Schofield }; 26463cf60b7SAlison Schofield 26563cf60b7SAlison Schofield if (in_size) { 26663cf60b7SAlison Schofield mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload), 26763cf60b7SAlison Schofield in_size); 268280302f0SAlison Schofield if (IS_ERR(mbox->payload_in)) 26963cf60b7SAlison Schofield return PTR_ERR(mbox->payload_in); 2706179045cSAlison Schofield 2716179045cSAlison Schofield if (!cxl_payload_from_user_allowed(opcode, mbox->payload_in)) { 2726179045cSAlison Schofield dev_dbg(cxlds->dev, "%s: input payload not allowed\n", 2736179045cSAlison Schofield cxl_mem_opcode_to_name(opcode)); 2746179045cSAlison Schofield kvfree(mbox->payload_in); 2756179045cSAlison Schofield return -EBUSY; 2766179045cSAlison Schofield } 27763cf60b7SAlison Schofield } 27863cf60b7SAlison Schofield 27963cf60b7SAlison Schofield /* Prepare to handle a full payload for variable sized output */ 28026f89535SAlison Schofield if (out_size == CXL_VARIABLE_PAYLOAD) 28163cf60b7SAlison Schofield mbox->size_out = cxlds->payload_size; 28263cf60b7SAlison Schofield else 28363cf60b7SAlison Schofield mbox->size_out = out_size; 28463cf60b7SAlison Schofield 28563cf60b7SAlison Schofield if (mbox->size_out) { 28663cf60b7SAlison Schofield mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL); 28763cf60b7SAlison Schofield if (!mbox->payload_out) { 28863cf60b7SAlison Schofield kvfree(mbox->payload_in); 28963cf60b7SAlison Schofield return -ENOMEM; 29063cf60b7SAlison Schofield } 29163cf60b7SAlison Schofield } 29263cf60b7SAlison Schofield return 0; 29363cf60b7SAlison Schofield } 29463cf60b7SAlison Schofield 29563cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox) 29663cf60b7SAlison Schofield { 29763cf60b7SAlison Schofield kvfree(mbox->payload_in); 29863cf60b7SAlison Schofield kvfree(mbox->payload_out); 29963cf60b7SAlison Schofield } 30063cf60b7SAlison Schofield 3016dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, 3026dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3036dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3046dd0e5ccSAlison Schofield { 3056dd0e5ccSAlison Schofield if (send_cmd->raw.rsvd) 3066dd0e5ccSAlison Schofield return -EINVAL; 3076dd0e5ccSAlison Schofield 3086dd0e5ccSAlison Schofield /* 3096dd0e5ccSAlison Schofield * Unlike supported commands, the output size of RAW commands 3106dd0e5ccSAlison Schofield * gets passed along without further checking, so it must be 3116dd0e5ccSAlison Schofield * validated here. 3126dd0e5ccSAlison Schofield */ 3136dd0e5ccSAlison Schofield if (send_cmd->out.size > cxlds->payload_size) 3146dd0e5ccSAlison Schofield return -EINVAL; 3156dd0e5ccSAlison Schofield 3166dd0e5ccSAlison Schofield if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode)) 3176dd0e5ccSAlison Schofield return -EPERM; 3186dd0e5ccSAlison Schofield 31939ed8da4SAlison Schofield dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n"); 32039ed8da4SAlison Schofield 3216dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3226dd0e5ccSAlison Schofield .info = { 3236dd0e5ccSAlison Schofield .id = CXL_MEM_COMMAND_ID_RAW, 3246dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3256dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3266dd0e5ccSAlison Schofield }, 3276dd0e5ccSAlison Schofield .opcode = send_cmd->raw.opcode 3286dd0e5ccSAlison Schofield }; 3296dd0e5ccSAlison Schofield 3306dd0e5ccSAlison Schofield return 0; 3316dd0e5ccSAlison Schofield } 3326dd0e5ccSAlison Schofield 3336dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, 3346dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 3356dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 3366dd0e5ccSAlison Schofield { 3376dd0e5ccSAlison Schofield struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; 3386dd0e5ccSAlison Schofield const struct cxl_command_info *info = &c->info; 3396dd0e5ccSAlison Schofield 3406dd0e5ccSAlison Schofield if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK) 3416dd0e5ccSAlison Schofield return -EINVAL; 3426dd0e5ccSAlison Schofield 3436dd0e5ccSAlison Schofield if (send_cmd->rsvd) 3446dd0e5ccSAlison Schofield return -EINVAL; 3456dd0e5ccSAlison Schofield 3466dd0e5ccSAlison Schofield if (send_cmd->in.rsvd || send_cmd->out.rsvd) 3476dd0e5ccSAlison Schofield return -EINVAL; 3486dd0e5ccSAlison Schofield 3496dd0e5ccSAlison Schofield /* Check that the command is enabled for hardware */ 3506dd0e5ccSAlison Schofield if (!test_bit(info->id, cxlds->enabled_cmds)) 3516dd0e5ccSAlison Schofield return -ENOTTY; 3526dd0e5ccSAlison Schofield 3536dd0e5ccSAlison Schofield /* Check that the command is not claimed for exclusive kernel use */ 3546dd0e5ccSAlison Schofield if (test_bit(info->id, cxlds->exclusive_cmds)) 3556dd0e5ccSAlison Schofield return -EBUSY; 3566dd0e5ccSAlison Schofield 3576dd0e5ccSAlison Schofield /* Check the input buffer is the expected size */ 35826f89535SAlison Schofield if (info->size_in != send_cmd->in.size) 3596dd0e5ccSAlison Schofield return -ENOMEM; 3606dd0e5ccSAlison Schofield 3616dd0e5ccSAlison Schofield /* Check the output buffer is at least large enough */ 36226f89535SAlison Schofield if (send_cmd->out.size < info->size_out) 3636dd0e5ccSAlison Schofield return -ENOMEM; 3646dd0e5ccSAlison Schofield 3656dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3666dd0e5ccSAlison Schofield .info = { 3676dd0e5ccSAlison Schofield .id = info->id, 3686dd0e5ccSAlison Schofield .flags = info->flags, 3696dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3706dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3716dd0e5ccSAlison Schofield }, 3726dd0e5ccSAlison Schofield .opcode = c->opcode 3736dd0e5ccSAlison Schofield }; 3746dd0e5ccSAlison Schofield 3756dd0e5ccSAlison Schofield return 0; 3766dd0e5ccSAlison Schofield } 3776dd0e5ccSAlison Schofield 3784faf31b4SDan Williams /** 3794faf31b4SDan Williams * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. 3809ae016aeSAlison Schofield * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. 3815e2411aeSIra Weiny * @cxlds: The device data for the operation 3824faf31b4SDan Williams * @send_cmd: &struct cxl_send_command copied in from userspace. 3834faf31b4SDan Williams * 3844faf31b4SDan Williams * Return: 3854faf31b4SDan Williams * * %0 - @out_cmd is ready to send. 3864faf31b4SDan Williams * * %-ENOTTY - Invalid command specified. 3874faf31b4SDan Williams * * %-EINVAL - Reserved fields or invalid values were used. 3884faf31b4SDan Williams * * %-ENOMEM - Input or output buffer wasn't sized properly. 3894faf31b4SDan Williams * * %-EPERM - Attempted to use a protected command. 39012f3856aSDan Williams * * %-EBUSY - Kernel has claimed exclusive access to this opcode 3914faf31b4SDan Williams * 3922dd5600aSAlison Schofield * The result of this command is a fully validated command in @mbox_cmd that is 3934faf31b4SDan Williams * safe to send to the hardware. 3944faf31b4SDan Williams */ 3959ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd, 3969ae016aeSAlison Schofield struct cxl_dev_state *cxlds, 3972dd5600aSAlison Schofield const struct cxl_send_command *send_cmd) 3984faf31b4SDan Williams { 3992dd5600aSAlison Schofield struct cxl_mem_command mem_cmd; 4009ae016aeSAlison Schofield int rc; 4019ae016aeSAlison Schofield 4024faf31b4SDan Williams if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) 4034faf31b4SDan Williams return -ENOTTY; 4044faf31b4SDan Williams 4054faf31b4SDan Williams /* 4064faf31b4SDan Williams * The user can never specify an input payload larger than what hardware 4074faf31b4SDan Williams * supports, but output can be arbitrarily large (simply write out as 4084faf31b4SDan Williams * much data as the hardware provides). 4094faf31b4SDan Williams */ 4105e2411aeSIra Weiny if (send_cmd->in.size > cxlds->payload_size) 4114faf31b4SDan Williams return -EINVAL; 4124faf31b4SDan Williams 4136dd0e5ccSAlison Schofield /* Sanitize and construct a cxl_mem_command */ 4146dd0e5ccSAlison Schofield if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW) 4152dd5600aSAlison Schofield rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, cxlds); 4166dd0e5ccSAlison Schofield else 4172dd5600aSAlison Schofield rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, cxlds); 4189ae016aeSAlison Schofield 4199ae016aeSAlison Schofield if (rc) 4209ae016aeSAlison Schofield return rc; 4219ae016aeSAlison Schofield 4229ae016aeSAlison Schofield /* Sanitize and construct a cxl_mbox_cmd */ 4232dd5600aSAlison Schofield return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, mem_cmd.opcode, 4242dd5600aSAlison Schofield mem_cmd.info.size_in, mem_cmd.info.size_out, 4259ae016aeSAlison Schofield send_cmd->in.payload); 4264faf31b4SDan Williams } 4274faf31b4SDan Williams 4284faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd, 4294faf31b4SDan Williams struct cxl_mem_query_commands __user *q) 4304faf31b4SDan Williams { 4314faf31b4SDan Williams struct device *dev = &cxlmd->dev; 4324faf31b4SDan Williams struct cxl_mem_command *cmd; 4334faf31b4SDan Williams u32 n_commands; 4344faf31b4SDan Williams int j = 0; 4354faf31b4SDan Williams 4364faf31b4SDan Williams dev_dbg(dev, "Query IOCTL\n"); 4374faf31b4SDan Williams 4384faf31b4SDan Williams if (get_user(n_commands, &q->n_commands)) 4394faf31b4SDan Williams return -EFAULT; 4404faf31b4SDan Williams 4414faf31b4SDan Williams /* returns the total number if 0 elements are requested. */ 4424faf31b4SDan Williams if (n_commands == 0) 443ff56ab9eSDan Williams return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands); 4444faf31b4SDan Williams 4454faf31b4SDan Williams /* 4464faf31b4SDan Williams * otherwise, return max(n_commands, total commands) cxl_command_info 4474faf31b4SDan Williams * structures. 4484faf31b4SDan Williams */ 4494faf31b4SDan Williams cxl_for_each_cmd(cmd) { 4504faf31b4SDan Williams const struct cxl_command_info *info = &cmd->info; 4514faf31b4SDan Williams 4524faf31b4SDan Williams if (copy_to_user(&q->commands[j++], info, sizeof(*info))) 4534faf31b4SDan Williams return -EFAULT; 4544faf31b4SDan Williams 4554faf31b4SDan Williams if (j == n_commands) 4564faf31b4SDan Williams break; 4574faf31b4SDan Williams } 4584faf31b4SDan Williams 4594faf31b4SDan Williams return 0; 4604faf31b4SDan Williams } 4614faf31b4SDan Williams 4624faf31b4SDan Williams /** 4634faf31b4SDan Williams * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. 4645e2411aeSIra Weiny * @cxlds: The device data for the operation 465d97fe8eeSAlison Schofield * @mbox_cmd: The validated mailbox command. 4664faf31b4SDan Williams * @out_payload: Pointer to userspace's output payload. 4674faf31b4SDan Williams * @size_out: (Input) Max payload size to copy out. 4684faf31b4SDan Williams * (Output) Payload size hardware generated. 4694faf31b4SDan Williams * @retval: Hardware generated return code from the operation. 4704faf31b4SDan Williams * 4714faf31b4SDan Williams * Return: 4724faf31b4SDan Williams * * %0 - Mailbox transaction succeeded. This implies the mailbox 4734faf31b4SDan Williams * protocol completed successfully not that the operation itself 4744faf31b4SDan Williams * was successful. 4754faf31b4SDan Williams * * %-ENOMEM - Couldn't allocate a bounce buffer. 4764faf31b4SDan Williams * * %-EFAULT - Something happened with copy_to/from_user. 4774faf31b4SDan Williams * * %-EINTR - Mailbox acquisition interrupted. 4784faf31b4SDan Williams * * %-EXXX - Transaction level failures. 4794faf31b4SDan Williams * 480d97fe8eeSAlison Schofield * Dispatches a mailbox command on behalf of a userspace request. 481d97fe8eeSAlison Schofield * The output payload is copied to userspace. 4824faf31b4SDan Williams * 4834faf31b4SDan Williams * See cxl_send_cmd(). 4844faf31b4SDan Williams */ 4855e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, 486d97fe8eeSAlison Schofield struct cxl_mbox_cmd *mbox_cmd, 487d97fe8eeSAlison Schofield u64 out_payload, s32 *size_out, 488d97fe8eeSAlison Schofield u32 *retval) 4894faf31b4SDan Williams { 4905e2411aeSIra Weiny struct device *dev = cxlds->dev; 4914faf31b4SDan Williams int rc; 4924faf31b4SDan Williams 4934faf31b4SDan Williams dev_dbg(dev, 4944faf31b4SDan Williams "Submitting %s command for user\n" 4954faf31b4SDan Williams "\topcode: %x\n" 49682b8ba29SAlison Schofield "\tsize: %zx\n", 497d97fe8eeSAlison Schofield cxl_mem_opcode_to_name(mbox_cmd->opcode), 498d97fe8eeSAlison Schofield mbox_cmd->opcode, mbox_cmd->size_in); 4994faf31b4SDan Williams 500d97fe8eeSAlison Schofield rc = cxlds->mbox_send(cxlds, mbox_cmd); 5014faf31b4SDan Williams if (rc) 5024faf31b4SDan Williams goto out; 5034faf31b4SDan Williams 5044faf31b4SDan Williams /* 5054faf31b4SDan Williams * @size_out contains the max size that's allowed to be written back out 5064faf31b4SDan Williams * to userspace. While the payload may have written more output than 5074faf31b4SDan Williams * this it will have to be ignored. 5084faf31b4SDan Williams */ 509d97fe8eeSAlison Schofield if (mbox_cmd->size_out) { 510d97fe8eeSAlison Schofield dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out, 5114faf31b4SDan Williams "Invalid return size\n"); 5124faf31b4SDan Williams if (copy_to_user(u64_to_user_ptr(out_payload), 513d97fe8eeSAlison Schofield mbox_cmd->payload_out, mbox_cmd->size_out)) { 5144faf31b4SDan Williams rc = -EFAULT; 5154faf31b4SDan Williams goto out; 5164faf31b4SDan Williams } 5174faf31b4SDan Williams } 5184faf31b4SDan Williams 519d97fe8eeSAlison Schofield *size_out = mbox_cmd->size_out; 520d97fe8eeSAlison Schofield *retval = mbox_cmd->return_code; 5214faf31b4SDan Williams 5224faf31b4SDan Williams out: 523d97fe8eeSAlison Schofield cxl_mbox_cmd_dtor(mbox_cmd); 5244faf31b4SDan Williams return rc; 5254faf31b4SDan Williams } 5264faf31b4SDan Williams 5274faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s) 5284faf31b4SDan Williams { 5295e2411aeSIra Weiny struct cxl_dev_state *cxlds = cxlmd->cxlds; 5304faf31b4SDan Williams struct device *dev = &cxlmd->dev; 5314faf31b4SDan Williams struct cxl_send_command send; 5329ae016aeSAlison Schofield struct cxl_mbox_cmd mbox_cmd; 5334faf31b4SDan Williams int rc; 5344faf31b4SDan Williams 5354faf31b4SDan Williams dev_dbg(dev, "Send IOCTL\n"); 5364faf31b4SDan Williams 5374faf31b4SDan Williams if (copy_from_user(&send, s, sizeof(send))) 5384faf31b4SDan Williams return -EFAULT; 5394faf31b4SDan Williams 5402dd5600aSAlison Schofield rc = cxl_validate_cmd_from_user(&mbox_cmd, cxlmd->cxlds, &send); 5414faf31b4SDan Williams if (rc) 5424faf31b4SDan Williams return rc; 5434faf31b4SDan Williams 544d97fe8eeSAlison Schofield rc = handle_mailbox_cmd_from_user(cxlds, &mbox_cmd, send.out.payload, 545d97fe8eeSAlison Schofield &send.out.size, &send.retval); 5464faf31b4SDan Williams if (rc) 5474faf31b4SDan Williams return rc; 5484faf31b4SDan Williams 5494faf31b4SDan Williams if (copy_to_user(s, &send, sizeof(send))) 5504faf31b4SDan Williams return -EFAULT; 5514faf31b4SDan Williams 5524faf31b4SDan Williams return 0; 5534faf31b4SDan Williams } 5544faf31b4SDan Williams 5555e2411aeSIra Weiny static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8 *out) 5564faf31b4SDan Williams { 5574faf31b4SDan Williams u32 remaining = size; 5584faf31b4SDan Williams u32 offset = 0; 5594faf31b4SDan Williams 5604faf31b4SDan Williams while (remaining) { 5615e2411aeSIra Weiny u32 xfer_size = min_t(u32, remaining, cxlds->payload_size); 56249be6dd8SDan Williams struct cxl_mbox_get_log log = { 5634faf31b4SDan Williams .uuid = *uuid, 5644faf31b4SDan Williams .offset = cpu_to_le32(offset), 5654faf31b4SDan Williams .length = cpu_to_le32(xfer_size) 5664faf31b4SDan Williams }; 5674faf31b4SDan Williams int rc; 5684faf31b4SDan Williams 5695e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_LOG, &log, sizeof(log), 5705e2411aeSIra Weiny out, xfer_size); 5714faf31b4SDan Williams if (rc < 0) 5724faf31b4SDan Williams return rc; 5734faf31b4SDan Williams 5744faf31b4SDan Williams out += xfer_size; 5754faf31b4SDan Williams remaining -= xfer_size; 5764faf31b4SDan Williams offset += xfer_size; 5774faf31b4SDan Williams } 5784faf31b4SDan Williams 5794faf31b4SDan Williams return 0; 5804faf31b4SDan Williams } 5814faf31b4SDan Williams 5824faf31b4SDan Williams /** 5834faf31b4SDan Williams * cxl_walk_cel() - Walk through the Command Effects Log. 5845e2411aeSIra Weiny * @cxlds: The device data for the operation 5854faf31b4SDan Williams * @size: Length of the Command Effects Log. 5864faf31b4SDan Williams * @cel: CEL 5874faf31b4SDan Williams * 5884faf31b4SDan Williams * Iterate over each entry in the CEL and determine if the driver supports the 5894faf31b4SDan Williams * command. If so, the command is enabled for the device and can be used later. 5904faf31b4SDan Williams */ 5915e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) 5924faf31b4SDan Williams { 59349be6dd8SDan Williams struct cxl_cel_entry *cel_entry; 5944faf31b4SDan Williams const int cel_entries = size / sizeof(*cel_entry); 5954faf31b4SDan Williams int i; 5964faf31b4SDan Williams 59749be6dd8SDan Williams cel_entry = (struct cxl_cel_entry *) cel; 5984faf31b4SDan Williams 5994faf31b4SDan Williams for (i = 0; i < cel_entries; i++) { 6004faf31b4SDan Williams u16 opcode = le16_to_cpu(cel_entry[i].opcode); 6014faf31b4SDan Williams struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 6024faf31b4SDan Williams 6034faf31b4SDan Williams if (!cmd) { 6045e2411aeSIra Weiny dev_dbg(cxlds->dev, 6054faf31b4SDan Williams "Opcode 0x%04x unsupported by driver", opcode); 6064faf31b4SDan Williams continue; 6074faf31b4SDan Williams } 6084faf31b4SDan Williams 6095e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 6104faf31b4SDan Williams } 6114faf31b4SDan Williams } 6124faf31b4SDan Williams 6135e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds) 6144faf31b4SDan Williams { 6154faf31b4SDan Williams struct cxl_mbox_get_supported_logs *ret; 6164faf31b4SDan Williams int rc; 6174faf31b4SDan Williams 6185e2411aeSIra Weiny ret = kvmalloc(cxlds->payload_size, GFP_KERNEL); 6194faf31b4SDan Williams if (!ret) 6204faf31b4SDan Williams return ERR_PTR(-ENOMEM); 6214faf31b4SDan Williams 6225e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_SUPPORTED_LOGS, NULL, 0, ret, 6235e2411aeSIra Weiny cxlds->payload_size); 6244faf31b4SDan Williams if (rc < 0) { 6254faf31b4SDan Williams kvfree(ret); 6264faf31b4SDan Williams return ERR_PTR(rc); 6274faf31b4SDan Williams } 6284faf31b4SDan Williams 6294faf31b4SDan Williams return ret; 6304faf31b4SDan Williams } 6314faf31b4SDan Williams 6324faf31b4SDan Williams enum { 6334faf31b4SDan Williams CEL_UUID, 6344faf31b4SDan Williams VENDOR_DEBUG_UUID, 6354faf31b4SDan Williams }; 6364faf31b4SDan Williams 6374faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */ 6384faf31b4SDan Williams static const uuid_t log_uuid[] = { 63949be6dd8SDan Williams [CEL_UUID] = DEFINE_CXL_CEL_UUID, 64049be6dd8SDan Williams [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID, 6414faf31b4SDan Williams }; 6424faf31b4SDan Williams 6434faf31b4SDan Williams /** 6445e2411aeSIra Weiny * cxl_enumerate_cmds() - Enumerate commands for a device. 6455e2411aeSIra Weiny * @cxlds: The device data for the operation 6464faf31b4SDan Williams * 6474faf31b4SDan Williams * Returns 0 if enumerate completed successfully. 6484faf31b4SDan Williams * 6494faf31b4SDan Williams * CXL devices have optional support for certain commands. This function will 6504faf31b4SDan Williams * determine the set of supported commands for the hardware and update the 6515e2411aeSIra Weiny * enabled_cmds bitmap in the @cxlds. 6524faf31b4SDan Williams */ 6535e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) 6544faf31b4SDan Williams { 6554faf31b4SDan Williams struct cxl_mbox_get_supported_logs *gsl; 6565e2411aeSIra Weiny struct device *dev = cxlds->dev; 6574faf31b4SDan Williams struct cxl_mem_command *cmd; 6584faf31b4SDan Williams int i, rc; 6594faf31b4SDan Williams 6605e2411aeSIra Weiny gsl = cxl_get_gsl(cxlds); 6614faf31b4SDan Williams if (IS_ERR(gsl)) 6624faf31b4SDan Williams return PTR_ERR(gsl); 6634faf31b4SDan Williams 6644faf31b4SDan Williams rc = -ENOENT; 6654faf31b4SDan Williams for (i = 0; i < le16_to_cpu(gsl->entries); i++) { 6664faf31b4SDan Williams u32 size = le32_to_cpu(gsl->entry[i].size); 6674faf31b4SDan Williams uuid_t uuid = gsl->entry[i].uuid; 6684faf31b4SDan Williams u8 *log; 6694faf31b4SDan Williams 6704faf31b4SDan Williams dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size); 6714faf31b4SDan Williams 6724faf31b4SDan Williams if (!uuid_equal(&uuid, &log_uuid[CEL_UUID])) 6734faf31b4SDan Williams continue; 6744faf31b4SDan Williams 6754faf31b4SDan Williams log = kvmalloc(size, GFP_KERNEL); 6764faf31b4SDan Williams if (!log) { 6774faf31b4SDan Williams rc = -ENOMEM; 6784faf31b4SDan Williams goto out; 6794faf31b4SDan Williams } 6804faf31b4SDan Williams 6815e2411aeSIra Weiny rc = cxl_xfer_log(cxlds, &uuid, size, log); 6824faf31b4SDan Williams if (rc) { 6834faf31b4SDan Williams kvfree(log); 6844faf31b4SDan Williams goto out; 6854faf31b4SDan Williams } 6864faf31b4SDan Williams 6875e2411aeSIra Weiny cxl_walk_cel(cxlds, size, log); 6884faf31b4SDan Williams kvfree(log); 6894faf31b4SDan Williams 6904faf31b4SDan Williams /* In case CEL was bogus, enable some default commands. */ 6914faf31b4SDan Williams cxl_for_each_cmd(cmd) 6924faf31b4SDan Williams if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE) 6935e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 6944faf31b4SDan Williams 6954faf31b4SDan Williams /* Found the required CEL */ 6964faf31b4SDan Williams rc = 0; 6974faf31b4SDan Williams } 6984faf31b4SDan Williams 6994faf31b4SDan Williams out: 7004faf31b4SDan Williams kvfree(gsl); 7014faf31b4SDan Williams return rc; 7024faf31b4SDan Williams } 703affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL); 7044faf31b4SDan Williams 7054faf31b4SDan Williams /** 7064faf31b4SDan Williams * cxl_mem_get_partition_info - Get partition info 7075e2411aeSIra Weiny * @cxlds: The device data for the operation 7084faf31b4SDan Williams * 7094faf31b4SDan Williams * Retrieve the current partition info for the device specified. The active 7104faf31b4SDan Williams * values are the current capacity in bytes. If not 0, the 'next' values are 7114faf31b4SDan Williams * the pending values, in bytes, which take affect on next cold reset. 7124faf31b4SDan Williams * 7134faf31b4SDan Williams * Return: 0 if no error: or the result of the mailbox command. 7144faf31b4SDan Williams * 7154faf31b4SDan Williams * See CXL @8.2.9.5.2.1 Get Partition Info 7164faf31b4SDan Williams */ 7175e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds) 7184faf31b4SDan Williams { 7194faf31b4SDan Williams struct cxl_mbox_get_partition_info { 7204faf31b4SDan Williams __le64 active_volatile_cap; 7214faf31b4SDan Williams __le64 active_persistent_cap; 7224faf31b4SDan Williams __le64 next_volatile_cap; 7234faf31b4SDan Williams __le64 next_persistent_cap; 7244faf31b4SDan Williams } __packed pi; 7254faf31b4SDan Williams int rc; 7264faf31b4SDan Williams 7275e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_PARTITION_INFO, NULL, 0, 7285e2411aeSIra Weiny &pi, sizeof(pi)); 7294faf31b4SDan Williams 7304faf31b4SDan Williams if (rc) 7314faf31b4SDan Williams return rc; 7324faf31b4SDan Williams 7335e2411aeSIra Weiny cxlds->active_volatile_bytes = 7344faf31b4SDan Williams le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 7355e2411aeSIra Weiny cxlds->active_persistent_bytes = 7364faf31b4SDan Williams le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER; 7375e2411aeSIra Weiny cxlds->next_volatile_bytes = 7384faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 7395e2411aeSIra Weiny cxlds->next_persistent_bytes = 7404faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 7414faf31b4SDan Williams 7424faf31b4SDan Williams return 0; 7434faf31b4SDan Williams } 7444faf31b4SDan Williams 7454faf31b4SDan Williams /** 7465e2411aeSIra Weiny * cxl_dev_state_identify() - Send the IDENTIFY command to the device. 7475e2411aeSIra Weiny * @cxlds: The device data for the operation 7484faf31b4SDan Williams * 7494faf31b4SDan Williams * Return: 0 if identify was executed successfully. 7504faf31b4SDan Williams * 7514faf31b4SDan Williams * This will dispatch the identify command to the device and on success populate 7524faf31b4SDan Williams * structures to be exported to sysfs. 7534faf31b4SDan Williams */ 7545e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds) 7554faf31b4SDan Williams { 7564faf31b4SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 75749be6dd8SDan Williams struct cxl_mbox_identify id; 7584faf31b4SDan Williams int rc; 7594faf31b4SDan Williams 7605e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_IDENTIFY, NULL, 0, &id, 7614faf31b4SDan Williams sizeof(id)); 7624faf31b4SDan Williams if (rc < 0) 7634faf31b4SDan Williams return rc; 7644faf31b4SDan Williams 7655e2411aeSIra Weiny cxlds->total_bytes = 7664faf31b4SDan Williams le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER; 7675e2411aeSIra Weiny cxlds->volatile_only_bytes = 7684faf31b4SDan Williams le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER; 7695e2411aeSIra Weiny cxlds->persistent_only_bytes = 7704faf31b4SDan Williams le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER; 7715e2411aeSIra Weiny cxlds->partition_align_bytes = 7724faf31b4SDan Williams le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER; 7734faf31b4SDan Williams 7745e2411aeSIra Weiny dev_dbg(cxlds->dev, 7754faf31b4SDan Williams "Identify Memory Device\n" 7764faf31b4SDan Williams " total_bytes = %#llx\n" 7774faf31b4SDan Williams " volatile_only_bytes = %#llx\n" 7784faf31b4SDan Williams " persistent_only_bytes = %#llx\n" 7794faf31b4SDan Williams " partition_align_bytes = %#llx\n", 7805e2411aeSIra Weiny cxlds->total_bytes, cxlds->volatile_only_bytes, 7815e2411aeSIra Weiny cxlds->persistent_only_bytes, cxlds->partition_align_bytes); 7824faf31b4SDan Williams 7835e2411aeSIra Weiny cxlds->lsa_size = le32_to_cpu(id.lsa_size); 7845e2411aeSIra Weiny memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision)); 7854faf31b4SDan Williams 7864faf31b4SDan Williams return 0; 7874faf31b4SDan Williams } 788affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL); 7894faf31b4SDan Williams 7905e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds) 7914faf31b4SDan Williams { 7924faf31b4SDan Williams int rc; 7934faf31b4SDan Williams 7945e2411aeSIra Weiny if (cxlds->partition_align_bytes == 0) { 7955e2411aeSIra Weiny cxlds->ram_range.start = 0; 7965e2411aeSIra Weiny cxlds->ram_range.end = cxlds->volatile_only_bytes - 1; 7975e2411aeSIra Weiny cxlds->pmem_range.start = cxlds->volatile_only_bytes; 7985e2411aeSIra Weiny cxlds->pmem_range.end = cxlds->volatile_only_bytes + 7995e2411aeSIra Weiny cxlds->persistent_only_bytes - 1; 8004faf31b4SDan Williams return 0; 8014faf31b4SDan Williams } 8024faf31b4SDan Williams 8035e2411aeSIra Weiny rc = cxl_mem_get_partition_info(cxlds); 8044faf31b4SDan Williams if (rc) { 8055e2411aeSIra Weiny dev_err(cxlds->dev, "Failed to query partition information\n"); 8064faf31b4SDan Williams return rc; 8074faf31b4SDan Williams } 8084faf31b4SDan Williams 8095e2411aeSIra Weiny dev_dbg(cxlds->dev, 8104faf31b4SDan Williams "Get Partition Info\n" 8114faf31b4SDan Williams " active_volatile_bytes = %#llx\n" 8124faf31b4SDan Williams " active_persistent_bytes = %#llx\n" 8134faf31b4SDan Williams " next_volatile_bytes = %#llx\n" 8144faf31b4SDan Williams " next_persistent_bytes = %#llx\n", 8155e2411aeSIra Weiny cxlds->active_volatile_bytes, cxlds->active_persistent_bytes, 8165e2411aeSIra Weiny cxlds->next_volatile_bytes, cxlds->next_persistent_bytes); 8174faf31b4SDan Williams 8185e2411aeSIra Weiny cxlds->ram_range.start = 0; 8195e2411aeSIra Weiny cxlds->ram_range.end = cxlds->active_volatile_bytes - 1; 8204faf31b4SDan Williams 8215e2411aeSIra Weiny cxlds->pmem_range.start = cxlds->active_volatile_bytes; 8225e2411aeSIra Weiny cxlds->pmem_range.end = 8235e2411aeSIra Weiny cxlds->active_volatile_bytes + cxlds->active_persistent_bytes - 1; 8244faf31b4SDan Williams 8254faf31b4SDan Williams return 0; 8264faf31b4SDan Williams } 827affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL); 8284faf31b4SDan Williams 8295e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev) 8304faf31b4SDan Williams { 8315e2411aeSIra Weiny struct cxl_dev_state *cxlds; 8324faf31b4SDan Williams 8335e2411aeSIra Weiny cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL); 8345e2411aeSIra Weiny if (!cxlds) { 8354faf31b4SDan Williams dev_err(dev, "No memory available\n"); 8364faf31b4SDan Williams return ERR_PTR(-ENOMEM); 8374faf31b4SDan Williams } 8384faf31b4SDan Williams 8395e2411aeSIra Weiny mutex_init(&cxlds->mbox_mutex); 8405e2411aeSIra Weiny cxlds->dev = dev; 8414faf31b4SDan Williams 8425e2411aeSIra Weiny return cxlds; 8434faf31b4SDan Williams } 844affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL); 8454faf31b4SDan Williams 8464faf31b4SDan Williams static struct dentry *cxl_debugfs; 8474faf31b4SDan Williams 8484faf31b4SDan Williams void __init cxl_mbox_init(void) 8494faf31b4SDan Williams { 8504faf31b4SDan Williams struct dentry *mbox_debugfs; 8514faf31b4SDan Williams 8524faf31b4SDan Williams cxl_debugfs = debugfs_create_dir("cxl", NULL); 8534faf31b4SDan Williams mbox_debugfs = debugfs_create_dir("mbox", cxl_debugfs); 8544faf31b4SDan Williams debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs, 8554faf31b4SDan Williams &cxl_raw_allow_all); 8564faf31b4SDan Williams } 8574faf31b4SDan Williams 8584faf31b4SDan Williams void cxl_mbox_exit(void) 8594faf31b4SDan Williams { 8604faf31b4SDan Williams debugfs_remove_recursive(cxl_debugfs); 8614faf31b4SDan Williams } 862