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 384faf31b4SDan Williams /* 394faf31b4SDan Williams * This table defines the supported mailbox commands for the driver. This table 404faf31b4SDan Williams * is made up of a UAPI structure. Non-negative values as parameters in the 414faf31b4SDan Williams * table will be validated against the user's input. For example, if size_in is 424faf31b4SDan Williams * 0, and the user passed in 1, it is an error. 434faf31b4SDan Williams */ 444faf31b4SDan Williams static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = { 454faf31b4SDan Williams CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE), 464faf31b4SDan Williams #ifdef CONFIG_CXL_MEM_RAW_COMMANDS 474faf31b4SDan Williams CXL_CMD(RAW, ~0, ~0, 0), 484faf31b4SDan Williams #endif 494faf31b4SDan Williams CXL_CMD(GET_SUPPORTED_LOGS, 0, ~0, CXL_CMD_FLAG_FORCE_ENABLE), 504faf31b4SDan Williams CXL_CMD(GET_FW_INFO, 0, 0x50, 0), 514faf31b4SDan Williams CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0), 524faf31b4SDan Williams CXL_CMD(GET_LSA, 0x8, ~0, 0), 534faf31b4SDan Williams CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0), 544faf31b4SDan Williams CXL_CMD(GET_LOG, 0x18, ~0, CXL_CMD_FLAG_FORCE_ENABLE), 554faf31b4SDan Williams CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0), 564faf31b4SDan Williams CXL_CMD(SET_LSA, ~0, 0, 0), 574faf31b4SDan Williams CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0), 584faf31b4SDan Williams CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0), 594faf31b4SDan Williams CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0), 604faf31b4SDan Williams CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0), 614faf31b4SDan Williams CXL_CMD(GET_POISON, 0x10, ~0, 0), 624faf31b4SDan Williams CXL_CMD(INJECT_POISON, 0x8, 0, 0), 634faf31b4SDan Williams CXL_CMD(CLEAR_POISON, 0x48, 0, 0), 644faf31b4SDan Williams CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0), 654faf31b4SDan Williams CXL_CMD(SCAN_MEDIA, 0x11, 0, 0), 664faf31b4SDan Williams CXL_CMD(GET_SCAN_MEDIA, 0, ~0, 0), 674faf31b4SDan Williams }; 684faf31b4SDan Williams 694faf31b4SDan Williams /* 704faf31b4SDan Williams * Commands that RAW doesn't permit. The rationale for each: 714faf31b4SDan Williams * 724faf31b4SDan Williams * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / 734faf31b4SDan Williams * coordination of transaction timeout values at the root bridge level. 744faf31b4SDan Williams * 754faf31b4SDan Williams * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live 764faf31b4SDan Williams * and needs to be coordinated with HDM updates. 774faf31b4SDan Williams * 784faf31b4SDan Williams * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the 794faf31b4SDan Williams * driver and any writes from userspace invalidates those contents. 804faf31b4SDan Williams * 814faf31b4SDan Williams * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes 824faf31b4SDan Williams * to the device after it is marked clean, userspace can not make that 834faf31b4SDan Williams * assertion. 844faf31b4SDan Williams * 854faf31b4SDan Williams * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that 864faf31b4SDan Williams * is kept up to date with patrol notifications and error management. 874faf31b4SDan Williams */ 884faf31b4SDan Williams static u16 cxl_disabled_raw_commands[] = { 894faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW, 904faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO, 914faf31b4SDan Williams CXL_MBOX_OP_SET_LSA, 924faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE, 934faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA, 944faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA, 954faf31b4SDan Williams }; 964faf31b4SDan Williams 974faf31b4SDan Williams /* 984faf31b4SDan Williams * Command sets that RAW doesn't permit. All opcodes in this set are 994faf31b4SDan Williams * disabled because they pass plain text security payloads over the 1004faf31b4SDan Williams * user/kernel boundary. This functionality is intended to be wrapped 1014faf31b4SDan Williams * behind the keys ABI which allows for encrypted payloads in the UAPI 1024faf31b4SDan Williams */ 1034faf31b4SDan Williams static u8 security_command_sets[] = { 1044faf31b4SDan Williams 0x44, /* Sanitize */ 1054faf31b4SDan Williams 0x45, /* Persistent Memory Data-at-rest Security */ 1064faf31b4SDan Williams 0x46, /* Security Passthrough */ 1074faf31b4SDan Williams }; 1084faf31b4SDan Williams 1094faf31b4SDan Williams static bool cxl_is_security_command(u16 opcode) 1104faf31b4SDan Williams { 1114faf31b4SDan Williams int i; 1124faf31b4SDan Williams 1134faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(security_command_sets); i++) 1144faf31b4SDan Williams if (security_command_sets[i] == (opcode >> 8)) 1154faf31b4SDan Williams return true; 1164faf31b4SDan Williams return false; 1174faf31b4SDan Williams } 1184faf31b4SDan Williams 1194faf31b4SDan Williams static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) 1204faf31b4SDan Williams { 1214faf31b4SDan Williams struct cxl_mem_command *c; 1224faf31b4SDan Williams 1234faf31b4SDan Williams cxl_for_each_cmd(c) 1244faf31b4SDan Williams if (c->opcode == opcode) 1254faf31b4SDan Williams return c; 1264faf31b4SDan Williams 1274faf31b4SDan Williams return NULL; 1284faf31b4SDan Williams } 1294faf31b4SDan Williams 13082b8ba29SAlison Schofield static const char *cxl_mem_opcode_to_name(u16 opcode) 13182b8ba29SAlison Schofield { 13282b8ba29SAlison Schofield struct cxl_mem_command *c; 13382b8ba29SAlison Schofield 13482b8ba29SAlison Schofield c = cxl_mem_find_command(opcode); 13582b8ba29SAlison Schofield if (!c) 13682b8ba29SAlison Schofield return NULL; 13782b8ba29SAlison Schofield 13882b8ba29SAlison Schofield return cxl_command_names[c->info.id].name; 13982b8ba29SAlison Schofield } 14082b8ba29SAlison Schofield 1414faf31b4SDan Williams /** 1425e2411aeSIra Weiny * cxl_mbox_send_cmd() - Send a mailbox command to a device. 1435e2411aeSIra Weiny * @cxlds: The device data for the operation 1444faf31b4SDan Williams * @opcode: Opcode for the mailbox command. 1454faf31b4SDan Williams * @in: The input payload for the mailbox command. 1464faf31b4SDan Williams * @in_size: The length of the input payload 1474faf31b4SDan Williams * @out: Caller allocated buffer for the output. 1484faf31b4SDan Williams * @out_size: Expected size of output. 1494faf31b4SDan Williams * 1504faf31b4SDan Williams * Context: Any context. Will acquire and release mbox_mutex. 1514faf31b4SDan Williams * Return: 1524faf31b4SDan Williams * * %>=0 - Number of bytes returned in @out. 1534faf31b4SDan Williams * * %-E2BIG - Payload is too large for hardware. 1544faf31b4SDan Williams * * %-EBUSY - Couldn't acquire exclusive mailbox access. 1554faf31b4SDan Williams * * %-EFAULT - Hardware error occurred. 1564faf31b4SDan Williams * * %-ENXIO - Command completed, but device reported an error. 1574faf31b4SDan Williams * * %-EIO - Unexpected output size. 1584faf31b4SDan Williams * 1594faf31b4SDan Williams * Mailbox commands may execute successfully yet the device itself reported an 1604faf31b4SDan Williams * error. While this distinction can be useful for commands from userspace, the 1614faf31b4SDan Williams * kernel will only be able to use results when both are successful. 1624faf31b4SDan Williams */ 1635e2411aeSIra Weiny int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in, 1644faf31b4SDan Williams size_t in_size, void *out, size_t out_size) 1654faf31b4SDan Williams { 1664faf31b4SDan Williams const struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 1674faf31b4SDan Williams struct cxl_mbox_cmd mbox_cmd = { 1684faf31b4SDan Williams .opcode = opcode, 1694faf31b4SDan Williams .payload_in = in, 1704faf31b4SDan Williams .size_in = in_size, 1714faf31b4SDan Williams .size_out = out_size, 1724faf31b4SDan Williams .payload_out = out, 1734faf31b4SDan Williams }; 1744faf31b4SDan Williams int rc; 1754faf31b4SDan Williams 1765e2411aeSIra Weiny if (out_size > cxlds->payload_size) 1774faf31b4SDan Williams return -E2BIG; 1784faf31b4SDan Williams 1795e2411aeSIra Weiny rc = cxlds->mbox_send(cxlds, &mbox_cmd); 1804faf31b4SDan Williams if (rc) 1814faf31b4SDan Williams return rc; 1824faf31b4SDan Williams 1834faf31b4SDan Williams /* TODO: Map return code to proper kernel style errno */ 1844faf31b4SDan Williams if (mbox_cmd.return_code != CXL_MBOX_SUCCESS) 1854faf31b4SDan Williams return -ENXIO; 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 */ 1914faf31b4SDan Williams if (cmd->info.size_out >= 0 && mbox_cmd.size_out != out_size) 1924faf31b4SDan Williams return -EIO; 1934faf31b4SDan Williams 1944faf31b4SDan Williams return 0; 1954faf31b4SDan Williams } 196affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mbox_send_cmd, CXL); 1974faf31b4SDan Williams 1984faf31b4SDan Williams static bool cxl_mem_raw_command_allowed(u16 opcode) 1994faf31b4SDan Williams { 2004faf31b4SDan Williams int i; 2014faf31b4SDan Williams 2024faf31b4SDan Williams if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS)) 2034faf31b4SDan Williams return false; 2044faf31b4SDan Williams 2054faf31b4SDan Williams if (security_locked_down(LOCKDOWN_PCI_ACCESS)) 2064faf31b4SDan Williams return false; 2074faf31b4SDan Williams 2084faf31b4SDan Williams if (cxl_raw_allow_all) 2094faf31b4SDan Williams return true; 2104faf31b4SDan Williams 2114faf31b4SDan Williams if (cxl_is_security_command(opcode)) 2124faf31b4SDan Williams return false; 2134faf31b4SDan Williams 2144faf31b4SDan Williams for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++) 2154faf31b4SDan Williams if (cxl_disabled_raw_commands[i] == opcode) 2164faf31b4SDan Williams return false; 2174faf31b4SDan Williams 2184faf31b4SDan Williams return true; 2194faf31b4SDan Williams } 2204faf31b4SDan Williams 22163cf60b7SAlison Schofield static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, 22263cf60b7SAlison Schofield struct cxl_dev_state *cxlds, u16 opcode, 22363cf60b7SAlison Schofield size_t in_size, size_t out_size, u64 in_payload) 22463cf60b7SAlison Schofield { 22563cf60b7SAlison Schofield *mbox = (struct cxl_mbox_cmd) { 22663cf60b7SAlison Schofield .opcode = opcode, 22763cf60b7SAlison Schofield .size_in = in_size, 22863cf60b7SAlison Schofield }; 22963cf60b7SAlison Schofield 23063cf60b7SAlison Schofield if (in_size) { 23163cf60b7SAlison Schofield mbox->payload_in = vmemdup_user(u64_to_user_ptr(in_payload), 23263cf60b7SAlison Schofield in_size); 23363cf60b7SAlison Schofield if (!mbox->payload_in) 23463cf60b7SAlison Schofield return PTR_ERR(mbox->payload_in); 23563cf60b7SAlison Schofield } 23663cf60b7SAlison Schofield 23763cf60b7SAlison Schofield /* Prepare to handle a full payload for variable sized output */ 23863cf60b7SAlison Schofield if (out_size < 0) 23963cf60b7SAlison Schofield mbox->size_out = cxlds->payload_size; 24063cf60b7SAlison Schofield else 24163cf60b7SAlison Schofield mbox->size_out = out_size; 24263cf60b7SAlison Schofield 24363cf60b7SAlison Schofield if (mbox->size_out) { 24463cf60b7SAlison Schofield mbox->payload_out = kvzalloc(mbox->size_out, GFP_KERNEL); 24563cf60b7SAlison Schofield if (!mbox->payload_out) { 24663cf60b7SAlison Schofield kvfree(mbox->payload_in); 24763cf60b7SAlison Schofield return -ENOMEM; 24863cf60b7SAlison Schofield } 24963cf60b7SAlison Schofield } 25063cf60b7SAlison Schofield return 0; 25163cf60b7SAlison Schofield } 25263cf60b7SAlison Schofield 25363cf60b7SAlison Schofield static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox) 25463cf60b7SAlison Schofield { 25563cf60b7SAlison Schofield kvfree(mbox->payload_in); 25663cf60b7SAlison Schofield kvfree(mbox->payload_out); 25763cf60b7SAlison Schofield } 25863cf60b7SAlison Schofield 2596dd0e5ccSAlison Schofield static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, 2606dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 2616dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 2626dd0e5ccSAlison Schofield { 2636dd0e5ccSAlison Schofield if (send_cmd->raw.rsvd) 2646dd0e5ccSAlison Schofield return -EINVAL; 2656dd0e5ccSAlison Schofield 2666dd0e5ccSAlison Schofield /* 2676dd0e5ccSAlison Schofield * Unlike supported commands, the output size of RAW commands 2686dd0e5ccSAlison Schofield * gets passed along without further checking, so it must be 2696dd0e5ccSAlison Schofield * validated here. 2706dd0e5ccSAlison Schofield */ 2716dd0e5ccSAlison Schofield if (send_cmd->out.size > cxlds->payload_size) 2726dd0e5ccSAlison Schofield return -EINVAL; 2736dd0e5ccSAlison Schofield 2746dd0e5ccSAlison Schofield if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode)) 2756dd0e5ccSAlison Schofield return -EPERM; 2766dd0e5ccSAlison Schofield 27739ed8da4SAlison Schofield dev_WARN_ONCE(cxlds->dev, true, "raw command path used\n"); 27839ed8da4SAlison Schofield 2796dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 2806dd0e5ccSAlison Schofield .info = { 2816dd0e5ccSAlison Schofield .id = CXL_MEM_COMMAND_ID_RAW, 2826dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 2836dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 2846dd0e5ccSAlison Schofield }, 2856dd0e5ccSAlison Schofield .opcode = send_cmd->raw.opcode 2866dd0e5ccSAlison Schofield }; 2876dd0e5ccSAlison Schofield 2886dd0e5ccSAlison Schofield return 0; 2896dd0e5ccSAlison Schofield } 2906dd0e5ccSAlison Schofield 2916dd0e5ccSAlison Schofield static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, 2926dd0e5ccSAlison Schofield const struct cxl_send_command *send_cmd, 2936dd0e5ccSAlison Schofield struct cxl_dev_state *cxlds) 2946dd0e5ccSAlison Schofield { 2956dd0e5ccSAlison Schofield struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; 2966dd0e5ccSAlison Schofield const struct cxl_command_info *info = &c->info; 2976dd0e5ccSAlison Schofield 2986dd0e5ccSAlison Schofield if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK) 2996dd0e5ccSAlison Schofield return -EINVAL; 3006dd0e5ccSAlison Schofield 3016dd0e5ccSAlison Schofield if (send_cmd->rsvd) 3026dd0e5ccSAlison Schofield return -EINVAL; 3036dd0e5ccSAlison Schofield 3046dd0e5ccSAlison Schofield if (send_cmd->in.rsvd || send_cmd->out.rsvd) 3056dd0e5ccSAlison Schofield return -EINVAL; 3066dd0e5ccSAlison Schofield 3076dd0e5ccSAlison Schofield /* Check that the command is enabled for hardware */ 3086dd0e5ccSAlison Schofield if (!test_bit(info->id, cxlds->enabled_cmds)) 3096dd0e5ccSAlison Schofield return -ENOTTY; 3106dd0e5ccSAlison Schofield 3116dd0e5ccSAlison Schofield /* Check that the command is not claimed for exclusive kernel use */ 3126dd0e5ccSAlison Schofield if (test_bit(info->id, cxlds->exclusive_cmds)) 3136dd0e5ccSAlison Schofield return -EBUSY; 3146dd0e5ccSAlison Schofield 3156dd0e5ccSAlison Schofield /* Check the input buffer is the expected size */ 3166dd0e5ccSAlison Schofield if (info->size_in >= 0 && info->size_in != send_cmd->in.size) 3176dd0e5ccSAlison Schofield return -ENOMEM; 3186dd0e5ccSAlison Schofield 3196dd0e5ccSAlison Schofield /* Check the output buffer is at least large enough */ 3206dd0e5ccSAlison Schofield if (info->size_out >= 0 && send_cmd->out.size < info->size_out) 3216dd0e5ccSAlison Schofield return -ENOMEM; 3226dd0e5ccSAlison Schofield 3236dd0e5ccSAlison Schofield *mem_cmd = (struct cxl_mem_command) { 3246dd0e5ccSAlison Schofield .info = { 3256dd0e5ccSAlison Schofield .id = info->id, 3266dd0e5ccSAlison Schofield .flags = info->flags, 3276dd0e5ccSAlison Schofield .size_in = send_cmd->in.size, 3286dd0e5ccSAlison Schofield .size_out = send_cmd->out.size, 3296dd0e5ccSAlison Schofield }, 3306dd0e5ccSAlison Schofield .opcode = c->opcode 3316dd0e5ccSAlison Schofield }; 3326dd0e5ccSAlison Schofield 3336dd0e5ccSAlison Schofield return 0; 3346dd0e5ccSAlison Schofield } 3356dd0e5ccSAlison Schofield 3364faf31b4SDan Williams /** 3374faf31b4SDan Williams * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. 3389ae016aeSAlison Schofield * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. 3395e2411aeSIra Weiny * @cxlds: The device data for the operation 3404faf31b4SDan Williams * @send_cmd: &struct cxl_send_command copied in from userspace. 3414faf31b4SDan Williams * @out_cmd: Sanitized and populated &struct cxl_mem_command. 3424faf31b4SDan Williams * 3434faf31b4SDan Williams * Return: 3444faf31b4SDan Williams * * %0 - @out_cmd is ready to send. 3454faf31b4SDan Williams * * %-ENOTTY - Invalid command specified. 3464faf31b4SDan Williams * * %-EINVAL - Reserved fields or invalid values were used. 3474faf31b4SDan Williams * * %-ENOMEM - Input or output buffer wasn't sized properly. 3484faf31b4SDan Williams * * %-EPERM - Attempted to use a protected command. 34912f3856aSDan Williams * * %-EBUSY - Kernel has claimed exclusive access to this opcode 3504faf31b4SDan Williams * 3514faf31b4SDan Williams * The result of this command is a fully validated command in @out_cmd that is 3524faf31b4SDan Williams * safe to send to the hardware. 3534faf31b4SDan Williams * 3544faf31b4SDan Williams * See handle_mailbox_cmd_from_user() 3554faf31b4SDan Williams */ 3569ae016aeSAlison Schofield static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd, 3579ae016aeSAlison Schofield struct cxl_dev_state *cxlds, 3584faf31b4SDan Williams const struct cxl_send_command *send_cmd, 3594faf31b4SDan Williams struct cxl_mem_command *out_cmd) 3604faf31b4SDan Williams { 3619ae016aeSAlison Schofield int rc; 3629ae016aeSAlison Schofield 3634faf31b4SDan Williams if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) 3644faf31b4SDan Williams return -ENOTTY; 3654faf31b4SDan Williams 3664faf31b4SDan Williams /* 3674faf31b4SDan Williams * The user can never specify an input payload larger than what hardware 3684faf31b4SDan Williams * supports, but output can be arbitrarily large (simply write out as 3694faf31b4SDan Williams * much data as the hardware provides). 3704faf31b4SDan Williams */ 3715e2411aeSIra Weiny if (send_cmd->in.size > cxlds->payload_size) 3724faf31b4SDan Williams return -EINVAL; 3734faf31b4SDan Williams 3746dd0e5ccSAlison Schofield /* Sanitize and construct a cxl_mem_command */ 3756dd0e5ccSAlison Schofield if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW) 3769ae016aeSAlison Schofield rc = cxl_to_mem_cmd_raw(out_cmd, send_cmd, cxlds); 3776dd0e5ccSAlison Schofield else 3789ae016aeSAlison Schofield rc = cxl_to_mem_cmd(out_cmd, send_cmd, cxlds); 3799ae016aeSAlison Schofield 3809ae016aeSAlison Schofield if (rc) 3819ae016aeSAlison Schofield return rc; 3829ae016aeSAlison Schofield 3839ae016aeSAlison Schofield /* Sanitize and construct a cxl_mbox_cmd */ 3849ae016aeSAlison Schofield return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, out_cmd->opcode, 3859ae016aeSAlison Schofield out_cmd->info.size_in, out_cmd->info.size_out, 3869ae016aeSAlison Schofield send_cmd->in.payload); 3874faf31b4SDan Williams } 3884faf31b4SDan Williams 3894faf31b4SDan Williams int cxl_query_cmd(struct cxl_memdev *cxlmd, 3904faf31b4SDan Williams struct cxl_mem_query_commands __user *q) 3914faf31b4SDan Williams { 3924faf31b4SDan Williams struct device *dev = &cxlmd->dev; 3934faf31b4SDan Williams struct cxl_mem_command *cmd; 3944faf31b4SDan Williams u32 n_commands; 3954faf31b4SDan Williams int j = 0; 3964faf31b4SDan Williams 3974faf31b4SDan Williams dev_dbg(dev, "Query IOCTL\n"); 3984faf31b4SDan Williams 3994faf31b4SDan Williams if (get_user(n_commands, &q->n_commands)) 4004faf31b4SDan Williams return -EFAULT; 4014faf31b4SDan Williams 4024faf31b4SDan Williams /* returns the total number if 0 elements are requested. */ 4034faf31b4SDan Williams if (n_commands == 0) 404ff56ab9eSDan Williams return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands); 4054faf31b4SDan Williams 4064faf31b4SDan Williams /* 4074faf31b4SDan Williams * otherwise, return max(n_commands, total commands) cxl_command_info 4084faf31b4SDan Williams * structures. 4094faf31b4SDan Williams */ 4104faf31b4SDan Williams cxl_for_each_cmd(cmd) { 4114faf31b4SDan Williams const struct cxl_command_info *info = &cmd->info; 4124faf31b4SDan Williams 4134faf31b4SDan Williams if (copy_to_user(&q->commands[j++], info, sizeof(*info))) 4144faf31b4SDan Williams return -EFAULT; 4154faf31b4SDan Williams 4164faf31b4SDan Williams if (j == n_commands) 4174faf31b4SDan Williams break; 4184faf31b4SDan Williams } 4194faf31b4SDan Williams 4204faf31b4SDan Williams return 0; 4214faf31b4SDan Williams } 4224faf31b4SDan Williams 4234faf31b4SDan Williams /** 4244faf31b4SDan Williams * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. 4255e2411aeSIra Weiny * @cxlds: The device data for the operation 426*d97fe8eeSAlison Schofield * @mbox_cmd: The validated mailbox command. 4274faf31b4SDan Williams * @out_payload: Pointer to userspace's output payload. 4284faf31b4SDan Williams * @size_out: (Input) Max payload size to copy out. 4294faf31b4SDan Williams * (Output) Payload size hardware generated. 4304faf31b4SDan Williams * @retval: Hardware generated return code from the operation. 4314faf31b4SDan Williams * 4324faf31b4SDan Williams * Return: 4334faf31b4SDan Williams * * %0 - Mailbox transaction succeeded. This implies the mailbox 4344faf31b4SDan Williams * protocol completed successfully not that the operation itself 4354faf31b4SDan Williams * was successful. 4364faf31b4SDan Williams * * %-ENOMEM - Couldn't allocate a bounce buffer. 4374faf31b4SDan Williams * * %-EFAULT - Something happened with copy_to/from_user. 4384faf31b4SDan Williams * * %-EINTR - Mailbox acquisition interrupted. 4394faf31b4SDan Williams * * %-EXXX - Transaction level failures. 4404faf31b4SDan Williams * 441*d97fe8eeSAlison Schofield * Dispatches a mailbox command on behalf of a userspace request. 442*d97fe8eeSAlison Schofield * The output payload is copied to userspace. 4434faf31b4SDan Williams * 4444faf31b4SDan Williams * See cxl_send_cmd(). 4454faf31b4SDan Williams */ 4465e2411aeSIra Weiny static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, 447*d97fe8eeSAlison Schofield struct cxl_mbox_cmd *mbox_cmd, 448*d97fe8eeSAlison Schofield u64 out_payload, s32 *size_out, 449*d97fe8eeSAlison Schofield u32 *retval) 4504faf31b4SDan Williams { 4515e2411aeSIra Weiny struct device *dev = cxlds->dev; 4524faf31b4SDan Williams int rc; 4534faf31b4SDan Williams 4544faf31b4SDan Williams dev_dbg(dev, 4554faf31b4SDan Williams "Submitting %s command for user\n" 4564faf31b4SDan Williams "\topcode: %x\n" 45782b8ba29SAlison Schofield "\tsize: %zx\n", 458*d97fe8eeSAlison Schofield cxl_mem_opcode_to_name(mbox_cmd->opcode), 459*d97fe8eeSAlison Schofield mbox_cmd->opcode, mbox_cmd->size_in); 4604faf31b4SDan Williams 461*d97fe8eeSAlison Schofield rc = cxlds->mbox_send(cxlds, mbox_cmd); 4624faf31b4SDan Williams if (rc) 4634faf31b4SDan Williams goto out; 4644faf31b4SDan Williams 4654faf31b4SDan Williams /* 4664faf31b4SDan Williams * @size_out contains the max size that's allowed to be written back out 4674faf31b4SDan Williams * to userspace. While the payload may have written more output than 4684faf31b4SDan Williams * this it will have to be ignored. 4694faf31b4SDan Williams */ 470*d97fe8eeSAlison Schofield if (mbox_cmd->size_out) { 471*d97fe8eeSAlison Schofield dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out, 4724faf31b4SDan Williams "Invalid return size\n"); 4734faf31b4SDan Williams if (copy_to_user(u64_to_user_ptr(out_payload), 474*d97fe8eeSAlison Schofield mbox_cmd->payload_out, mbox_cmd->size_out)) { 4754faf31b4SDan Williams rc = -EFAULT; 4764faf31b4SDan Williams goto out; 4774faf31b4SDan Williams } 4784faf31b4SDan Williams } 4794faf31b4SDan Williams 480*d97fe8eeSAlison Schofield *size_out = mbox_cmd->size_out; 481*d97fe8eeSAlison Schofield *retval = mbox_cmd->return_code; 4824faf31b4SDan Williams 4834faf31b4SDan Williams out: 484*d97fe8eeSAlison Schofield cxl_mbox_cmd_dtor(mbox_cmd); 4854faf31b4SDan Williams return rc; 4864faf31b4SDan Williams } 4874faf31b4SDan Williams 4884faf31b4SDan Williams int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s) 4894faf31b4SDan Williams { 4905e2411aeSIra Weiny struct cxl_dev_state *cxlds = cxlmd->cxlds; 4914faf31b4SDan Williams struct device *dev = &cxlmd->dev; 4924faf31b4SDan Williams struct cxl_send_command send; 4934faf31b4SDan Williams struct cxl_mem_command c; 4949ae016aeSAlison Schofield struct cxl_mbox_cmd mbox_cmd; 4954faf31b4SDan Williams int rc; 4964faf31b4SDan Williams 4974faf31b4SDan Williams dev_dbg(dev, "Send IOCTL\n"); 4984faf31b4SDan Williams 4994faf31b4SDan Williams if (copy_from_user(&send, s, sizeof(send))) 5004faf31b4SDan Williams return -EFAULT; 5014faf31b4SDan Williams 5029ae016aeSAlison Schofield rc = cxl_validate_cmd_from_user(&mbox_cmd, cxlmd->cxlds, &send, &c); 5034faf31b4SDan Williams if (rc) 5044faf31b4SDan Williams return rc; 5054faf31b4SDan Williams 506*d97fe8eeSAlison Schofield rc = handle_mailbox_cmd_from_user(cxlds, &mbox_cmd, send.out.payload, 507*d97fe8eeSAlison Schofield &send.out.size, &send.retval); 5084faf31b4SDan Williams if (rc) 5094faf31b4SDan Williams return rc; 5104faf31b4SDan Williams 5114faf31b4SDan Williams if (copy_to_user(s, &send, sizeof(send))) 5124faf31b4SDan Williams return -EFAULT; 5134faf31b4SDan Williams 5144faf31b4SDan Williams return 0; 5154faf31b4SDan Williams } 5164faf31b4SDan Williams 5175e2411aeSIra Weiny static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8 *out) 5184faf31b4SDan Williams { 5194faf31b4SDan Williams u32 remaining = size; 5204faf31b4SDan Williams u32 offset = 0; 5214faf31b4SDan Williams 5224faf31b4SDan Williams while (remaining) { 5235e2411aeSIra Weiny u32 xfer_size = min_t(u32, remaining, cxlds->payload_size); 52449be6dd8SDan Williams struct cxl_mbox_get_log log = { 5254faf31b4SDan Williams .uuid = *uuid, 5264faf31b4SDan Williams .offset = cpu_to_le32(offset), 5274faf31b4SDan Williams .length = cpu_to_le32(xfer_size) 5284faf31b4SDan Williams }; 5294faf31b4SDan Williams int rc; 5304faf31b4SDan Williams 5315e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_LOG, &log, sizeof(log), 5325e2411aeSIra Weiny out, xfer_size); 5334faf31b4SDan Williams if (rc < 0) 5344faf31b4SDan Williams return rc; 5354faf31b4SDan Williams 5364faf31b4SDan Williams out += xfer_size; 5374faf31b4SDan Williams remaining -= xfer_size; 5384faf31b4SDan Williams offset += xfer_size; 5394faf31b4SDan Williams } 5404faf31b4SDan Williams 5414faf31b4SDan Williams return 0; 5424faf31b4SDan Williams } 5434faf31b4SDan Williams 5444faf31b4SDan Williams /** 5454faf31b4SDan Williams * cxl_walk_cel() - Walk through the Command Effects Log. 5465e2411aeSIra Weiny * @cxlds: The device data for the operation 5474faf31b4SDan Williams * @size: Length of the Command Effects Log. 5484faf31b4SDan Williams * @cel: CEL 5494faf31b4SDan Williams * 5504faf31b4SDan Williams * Iterate over each entry in the CEL and determine if the driver supports the 5514faf31b4SDan Williams * command. If so, the command is enabled for the device and can be used later. 5524faf31b4SDan Williams */ 5535e2411aeSIra Weiny static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) 5544faf31b4SDan Williams { 55549be6dd8SDan Williams struct cxl_cel_entry *cel_entry; 5564faf31b4SDan Williams const int cel_entries = size / sizeof(*cel_entry); 5574faf31b4SDan Williams int i; 5584faf31b4SDan Williams 55949be6dd8SDan Williams cel_entry = (struct cxl_cel_entry *) cel; 5604faf31b4SDan Williams 5614faf31b4SDan Williams for (i = 0; i < cel_entries; i++) { 5624faf31b4SDan Williams u16 opcode = le16_to_cpu(cel_entry[i].opcode); 5634faf31b4SDan Williams struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 5644faf31b4SDan Williams 5654faf31b4SDan Williams if (!cmd) { 5665e2411aeSIra Weiny dev_dbg(cxlds->dev, 5674faf31b4SDan Williams "Opcode 0x%04x unsupported by driver", opcode); 5684faf31b4SDan Williams continue; 5694faf31b4SDan Williams } 5704faf31b4SDan Williams 5715e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 5724faf31b4SDan Williams } 5734faf31b4SDan Williams } 5744faf31b4SDan Williams 5755e2411aeSIra Weiny static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_dev_state *cxlds) 5764faf31b4SDan Williams { 5774faf31b4SDan Williams struct cxl_mbox_get_supported_logs *ret; 5784faf31b4SDan Williams int rc; 5794faf31b4SDan Williams 5805e2411aeSIra Weiny ret = kvmalloc(cxlds->payload_size, GFP_KERNEL); 5814faf31b4SDan Williams if (!ret) 5824faf31b4SDan Williams return ERR_PTR(-ENOMEM); 5834faf31b4SDan Williams 5845e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_SUPPORTED_LOGS, NULL, 0, ret, 5855e2411aeSIra Weiny cxlds->payload_size); 5864faf31b4SDan Williams if (rc < 0) { 5874faf31b4SDan Williams kvfree(ret); 5884faf31b4SDan Williams return ERR_PTR(rc); 5894faf31b4SDan Williams } 5904faf31b4SDan Williams 5914faf31b4SDan Williams return ret; 5924faf31b4SDan Williams } 5934faf31b4SDan Williams 5944faf31b4SDan Williams enum { 5954faf31b4SDan Williams CEL_UUID, 5964faf31b4SDan Williams VENDOR_DEBUG_UUID, 5974faf31b4SDan Williams }; 5984faf31b4SDan Williams 5994faf31b4SDan Williams /* See CXL 2.0 Table 170. Get Log Input Payload */ 6004faf31b4SDan Williams static const uuid_t log_uuid[] = { 60149be6dd8SDan Williams [CEL_UUID] = DEFINE_CXL_CEL_UUID, 60249be6dd8SDan Williams [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID, 6034faf31b4SDan Williams }; 6044faf31b4SDan Williams 6054faf31b4SDan Williams /** 6065e2411aeSIra Weiny * cxl_enumerate_cmds() - Enumerate commands for a device. 6075e2411aeSIra Weiny * @cxlds: The device data for the operation 6084faf31b4SDan Williams * 6094faf31b4SDan Williams * Returns 0 if enumerate completed successfully. 6104faf31b4SDan Williams * 6114faf31b4SDan Williams * CXL devices have optional support for certain commands. This function will 6124faf31b4SDan Williams * determine the set of supported commands for the hardware and update the 6135e2411aeSIra Weiny * enabled_cmds bitmap in the @cxlds. 6144faf31b4SDan Williams */ 6155e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) 6164faf31b4SDan Williams { 6174faf31b4SDan Williams struct cxl_mbox_get_supported_logs *gsl; 6185e2411aeSIra Weiny struct device *dev = cxlds->dev; 6194faf31b4SDan Williams struct cxl_mem_command *cmd; 6204faf31b4SDan Williams int i, rc; 6214faf31b4SDan Williams 6225e2411aeSIra Weiny gsl = cxl_get_gsl(cxlds); 6234faf31b4SDan Williams if (IS_ERR(gsl)) 6244faf31b4SDan Williams return PTR_ERR(gsl); 6254faf31b4SDan Williams 6264faf31b4SDan Williams rc = -ENOENT; 6274faf31b4SDan Williams for (i = 0; i < le16_to_cpu(gsl->entries); i++) { 6284faf31b4SDan Williams u32 size = le32_to_cpu(gsl->entry[i].size); 6294faf31b4SDan Williams uuid_t uuid = gsl->entry[i].uuid; 6304faf31b4SDan Williams u8 *log; 6314faf31b4SDan Williams 6324faf31b4SDan Williams dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size); 6334faf31b4SDan Williams 6344faf31b4SDan Williams if (!uuid_equal(&uuid, &log_uuid[CEL_UUID])) 6354faf31b4SDan Williams continue; 6364faf31b4SDan Williams 6374faf31b4SDan Williams log = kvmalloc(size, GFP_KERNEL); 6384faf31b4SDan Williams if (!log) { 6394faf31b4SDan Williams rc = -ENOMEM; 6404faf31b4SDan Williams goto out; 6414faf31b4SDan Williams } 6424faf31b4SDan Williams 6435e2411aeSIra Weiny rc = cxl_xfer_log(cxlds, &uuid, size, log); 6444faf31b4SDan Williams if (rc) { 6454faf31b4SDan Williams kvfree(log); 6464faf31b4SDan Williams goto out; 6474faf31b4SDan Williams } 6484faf31b4SDan Williams 6495e2411aeSIra Weiny cxl_walk_cel(cxlds, size, log); 6504faf31b4SDan Williams kvfree(log); 6514faf31b4SDan Williams 6524faf31b4SDan Williams /* In case CEL was bogus, enable some default commands. */ 6534faf31b4SDan Williams cxl_for_each_cmd(cmd) 6544faf31b4SDan Williams if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE) 6555e2411aeSIra Weiny set_bit(cmd->info.id, cxlds->enabled_cmds); 6564faf31b4SDan Williams 6574faf31b4SDan Williams /* Found the required CEL */ 6584faf31b4SDan Williams rc = 0; 6594faf31b4SDan Williams } 6604faf31b4SDan Williams 6614faf31b4SDan Williams out: 6624faf31b4SDan Williams kvfree(gsl); 6634faf31b4SDan Williams return rc; 6644faf31b4SDan Williams } 665affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, CXL); 6664faf31b4SDan Williams 6674faf31b4SDan Williams /** 6684faf31b4SDan Williams * cxl_mem_get_partition_info - Get partition info 6695e2411aeSIra Weiny * @cxlds: The device data for the operation 6704faf31b4SDan Williams * 6714faf31b4SDan Williams * Retrieve the current partition info for the device specified. The active 6724faf31b4SDan Williams * values are the current capacity in bytes. If not 0, the 'next' values are 6734faf31b4SDan Williams * the pending values, in bytes, which take affect on next cold reset. 6744faf31b4SDan Williams * 6754faf31b4SDan Williams * Return: 0 if no error: or the result of the mailbox command. 6764faf31b4SDan Williams * 6774faf31b4SDan Williams * See CXL @8.2.9.5.2.1 Get Partition Info 6784faf31b4SDan Williams */ 6795e2411aeSIra Weiny static int cxl_mem_get_partition_info(struct cxl_dev_state *cxlds) 6804faf31b4SDan Williams { 6814faf31b4SDan Williams struct cxl_mbox_get_partition_info { 6824faf31b4SDan Williams __le64 active_volatile_cap; 6834faf31b4SDan Williams __le64 active_persistent_cap; 6844faf31b4SDan Williams __le64 next_volatile_cap; 6854faf31b4SDan Williams __le64 next_persistent_cap; 6864faf31b4SDan Williams } __packed pi; 6874faf31b4SDan Williams int rc; 6884faf31b4SDan Williams 6895e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_PARTITION_INFO, NULL, 0, 6905e2411aeSIra Weiny &pi, sizeof(pi)); 6914faf31b4SDan Williams 6924faf31b4SDan Williams if (rc) 6934faf31b4SDan Williams return rc; 6944faf31b4SDan Williams 6955e2411aeSIra Weiny cxlds->active_volatile_bytes = 6964faf31b4SDan Williams le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 6975e2411aeSIra Weiny cxlds->active_persistent_bytes = 6984faf31b4SDan Williams le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER; 6995e2411aeSIra Weiny cxlds->next_volatile_bytes = 7004faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 7015e2411aeSIra Weiny cxlds->next_persistent_bytes = 7024faf31b4SDan Williams le64_to_cpu(pi.next_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 7034faf31b4SDan Williams 7044faf31b4SDan Williams return 0; 7054faf31b4SDan Williams } 7064faf31b4SDan Williams 7074faf31b4SDan Williams /** 7085e2411aeSIra Weiny * cxl_dev_state_identify() - Send the IDENTIFY command to the device. 7095e2411aeSIra Weiny * @cxlds: The device data for the operation 7104faf31b4SDan Williams * 7114faf31b4SDan Williams * Return: 0 if identify was executed successfully. 7124faf31b4SDan Williams * 7134faf31b4SDan Williams * This will dispatch the identify command to the device and on success populate 7144faf31b4SDan Williams * structures to be exported to sysfs. 7154faf31b4SDan Williams */ 7165e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds) 7174faf31b4SDan Williams { 7184faf31b4SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 71949be6dd8SDan Williams struct cxl_mbox_identify id; 7204faf31b4SDan Williams int rc; 7214faf31b4SDan Williams 7225e2411aeSIra Weiny rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_IDENTIFY, NULL, 0, &id, 7234faf31b4SDan Williams sizeof(id)); 7244faf31b4SDan Williams if (rc < 0) 7254faf31b4SDan Williams return rc; 7264faf31b4SDan Williams 7275e2411aeSIra Weiny cxlds->total_bytes = 7284faf31b4SDan Williams le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER; 7295e2411aeSIra Weiny cxlds->volatile_only_bytes = 7304faf31b4SDan Williams le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER; 7315e2411aeSIra Weiny cxlds->persistent_only_bytes = 7324faf31b4SDan Williams le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER; 7335e2411aeSIra Weiny cxlds->partition_align_bytes = 7344faf31b4SDan Williams le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER; 7354faf31b4SDan Williams 7365e2411aeSIra Weiny dev_dbg(cxlds->dev, 7374faf31b4SDan Williams "Identify Memory Device\n" 7384faf31b4SDan Williams " total_bytes = %#llx\n" 7394faf31b4SDan Williams " volatile_only_bytes = %#llx\n" 7404faf31b4SDan Williams " persistent_only_bytes = %#llx\n" 7414faf31b4SDan Williams " partition_align_bytes = %#llx\n", 7425e2411aeSIra Weiny cxlds->total_bytes, cxlds->volatile_only_bytes, 7435e2411aeSIra Weiny cxlds->persistent_only_bytes, cxlds->partition_align_bytes); 7444faf31b4SDan Williams 7455e2411aeSIra Weiny cxlds->lsa_size = le32_to_cpu(id.lsa_size); 7465e2411aeSIra Weiny memcpy(cxlds->firmware_version, id.fw_revision, sizeof(id.fw_revision)); 7474faf31b4SDan Williams 7484faf31b4SDan Williams return 0; 7494faf31b4SDan Williams } 750affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, CXL); 7514faf31b4SDan Williams 7525e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds) 7534faf31b4SDan Williams { 7544faf31b4SDan Williams int rc; 7554faf31b4SDan Williams 7565e2411aeSIra Weiny if (cxlds->partition_align_bytes == 0) { 7575e2411aeSIra Weiny cxlds->ram_range.start = 0; 7585e2411aeSIra Weiny cxlds->ram_range.end = cxlds->volatile_only_bytes - 1; 7595e2411aeSIra Weiny cxlds->pmem_range.start = cxlds->volatile_only_bytes; 7605e2411aeSIra Weiny cxlds->pmem_range.end = cxlds->volatile_only_bytes + 7615e2411aeSIra Weiny cxlds->persistent_only_bytes - 1; 7624faf31b4SDan Williams return 0; 7634faf31b4SDan Williams } 7644faf31b4SDan Williams 7655e2411aeSIra Weiny rc = cxl_mem_get_partition_info(cxlds); 7664faf31b4SDan Williams if (rc) { 7675e2411aeSIra Weiny dev_err(cxlds->dev, "Failed to query partition information\n"); 7684faf31b4SDan Williams return rc; 7694faf31b4SDan Williams } 7704faf31b4SDan Williams 7715e2411aeSIra Weiny dev_dbg(cxlds->dev, 7724faf31b4SDan Williams "Get Partition Info\n" 7734faf31b4SDan Williams " active_volatile_bytes = %#llx\n" 7744faf31b4SDan Williams " active_persistent_bytes = %#llx\n" 7754faf31b4SDan Williams " next_volatile_bytes = %#llx\n" 7764faf31b4SDan Williams " next_persistent_bytes = %#llx\n", 7775e2411aeSIra Weiny cxlds->active_volatile_bytes, cxlds->active_persistent_bytes, 7785e2411aeSIra Weiny cxlds->next_volatile_bytes, cxlds->next_persistent_bytes); 7794faf31b4SDan Williams 7805e2411aeSIra Weiny cxlds->ram_range.start = 0; 7815e2411aeSIra Weiny cxlds->ram_range.end = cxlds->active_volatile_bytes - 1; 7824faf31b4SDan Williams 7835e2411aeSIra Weiny cxlds->pmem_range.start = cxlds->active_volatile_bytes; 7845e2411aeSIra Weiny cxlds->pmem_range.end = 7855e2411aeSIra Weiny cxlds->active_volatile_bytes + cxlds->active_persistent_bytes - 1; 7864faf31b4SDan Williams 7874faf31b4SDan Williams return 0; 7884faf31b4SDan Williams } 789affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_mem_create_range_info, CXL); 7904faf31b4SDan Williams 7915e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev) 7924faf31b4SDan Williams { 7935e2411aeSIra Weiny struct cxl_dev_state *cxlds; 7944faf31b4SDan Williams 7955e2411aeSIra Weiny cxlds = devm_kzalloc(dev, sizeof(*cxlds), GFP_KERNEL); 7965e2411aeSIra Weiny if (!cxlds) { 7974faf31b4SDan Williams dev_err(dev, "No memory available\n"); 7984faf31b4SDan Williams return ERR_PTR(-ENOMEM); 7994faf31b4SDan Williams } 8004faf31b4SDan Williams 8015e2411aeSIra Weiny mutex_init(&cxlds->mbox_mutex); 8025e2411aeSIra Weiny cxlds->dev = dev; 8034faf31b4SDan Williams 8045e2411aeSIra Weiny return cxlds; 8054faf31b4SDan Williams } 806affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_dev_state_create, CXL); 8074faf31b4SDan Williams 8084faf31b4SDan Williams static struct dentry *cxl_debugfs; 8094faf31b4SDan Williams 8104faf31b4SDan Williams void __init cxl_mbox_init(void) 8114faf31b4SDan Williams { 8124faf31b4SDan Williams struct dentry *mbox_debugfs; 8134faf31b4SDan Williams 8144faf31b4SDan Williams cxl_debugfs = debugfs_create_dir("cxl", NULL); 8154faf31b4SDan Williams mbox_debugfs = debugfs_create_dir("mbox", cxl_debugfs); 8164faf31b4SDan Williams debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs, 8174faf31b4SDan Williams &cxl_raw_allow_all); 8184faf31b4SDan Williams } 8194faf31b4SDan Williams 8204faf31b4SDan Williams void cxl_mbox_exit(void) 8214faf31b4SDan Williams { 8224faf31b4SDan Williams debugfs_remove_recursive(cxl_debugfs); 8234faf31b4SDan Williams } 824