15161a55cSBen Widawsky /* SPDX-License-Identifier: GPL-2.0-only */ 25161a55cSBen Widawsky /* Copyright(c) 2020-2021 Intel Corporation. */ 35161a55cSBen Widawsky #ifndef __CXL_MEM_H__ 45161a55cSBen Widawsky #define __CXL_MEM_H__ 54faf31b4SDan Williams #include <uapi/linux/cxl_mem.h> 65161a55cSBen Widawsky #include <linux/cdev.h> 76ebe28f9SIra Weiny #include <linux/uuid.h> 85161a55cSBen Widawsky #include "cxl.h" 95161a55cSBen Widawsky 105161a55cSBen Widawsky /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */ 115161a55cSBen Widawsky #define CXLMDEV_STATUS_OFFSET 0x0 125161a55cSBen Widawsky #define CXLMDEV_DEV_FATAL BIT(0) 135161a55cSBen Widawsky #define CXLMDEV_FW_HALT BIT(1) 145161a55cSBen Widawsky #define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2) 155161a55cSBen Widawsky #define CXLMDEV_MS_NOT_READY 0 165161a55cSBen Widawsky #define CXLMDEV_MS_READY 1 175161a55cSBen Widawsky #define CXLMDEV_MS_ERROR 2 185161a55cSBen Widawsky #define CXLMDEV_MS_DISABLED 3 195161a55cSBen Widawsky #define CXLMDEV_READY(status) \ 205161a55cSBen Widawsky (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \ 215161a55cSBen Widawsky CXLMDEV_MS_READY) 225161a55cSBen Widawsky #define CXLMDEV_MBOX_IF_READY BIT(4) 235161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5) 245161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_NOT 0 255161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_COLD 1 265161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_WARM 2 275161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_HOT 3 285161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_CXL 4 295161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED(status) \ 305161a55cSBen Widawsky (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \ 315161a55cSBen Widawsky CXLMDEV_RESET_NEEDED_NOT) 325161a55cSBen Widawsky 335161a55cSBen Widawsky /** 345161a55cSBen Widawsky * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device 355161a55cSBen Widawsky * @dev: driver core device object 365161a55cSBen Widawsky * @cdev: char dev core object for ioctl operations 375e2411aeSIra Weiny * @cxlds: The device state backing this device 388dd2bc0fSBen Widawsky * @detach_work: active memdev lost a port in its ancestry 39f17b558dSDan Williams * @cxl_nvb: coordinate removal of @cxl_nvd if present 40f17b558dSDan Williams * @cxl_nvd: optional bridge to an nvdimm if the device supports pmem 415161a55cSBen Widawsky * @id: id number of this memdev instance. 422345df54SDan Williams * @depth: endpoint port depth 435161a55cSBen Widawsky */ 445161a55cSBen Widawsky struct cxl_memdev { 455161a55cSBen Widawsky struct device dev; 465161a55cSBen Widawsky struct cdev cdev; 475e2411aeSIra Weiny struct cxl_dev_state *cxlds; 488dd2bc0fSBen Widawsky struct work_struct detach_work; 49f17b558dSDan Williams struct cxl_nvdimm_bridge *cxl_nvb; 50f17b558dSDan Williams struct cxl_nvdimm *cxl_nvd; 515161a55cSBen Widawsky int id; 522345df54SDan Williams int depth; 535161a55cSBen Widawsky }; 545161a55cSBen Widawsky 553d135db5SBen Widawsky static inline struct cxl_memdev *to_cxl_memdev(struct device *dev) 563d135db5SBen Widawsky { 573d135db5SBen Widawsky return container_of(dev, struct cxl_memdev, dev); 583d135db5SBen Widawsky } 593d135db5SBen Widawsky 609c57cde0SDan Williams static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled) 619c57cde0SDan Williams { 629c57cde0SDan Williams return to_cxl_port(cxled->cxld.dev.parent); 639c57cde0SDan Williams } 649c57cde0SDan Williams 65384e624bSDan Williams static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd) 66384e624bSDan Williams { 67384e624bSDan Williams return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent); 68384e624bSDan Williams } 69384e624bSDan Williams 709c57cde0SDan Williams static inline struct cxl_memdev * 719c57cde0SDan Williams cxled_to_memdev(struct cxl_endpoint_decoder *cxled) 729c57cde0SDan Williams { 739c57cde0SDan Williams struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent); 749c57cde0SDan Williams 759c57cde0SDan Williams return to_cxl_memdev(port->uport); 769c57cde0SDan Williams } 779c57cde0SDan Williams 782a81ada3SGreg Kroah-Hartman bool is_cxl_memdev(const struct device *dev); 798dd2bc0fSBen Widawsky static inline bool is_cxl_endpoint(struct cxl_port *port) 808dd2bc0fSBen Widawsky { 818dd2bc0fSBen Widawsky return is_cxl_memdev(port->uport); 828dd2bc0fSBen Widawsky } 838dd2bc0fSBen Widawsky 845e2411aeSIra Weiny struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds); 853d8f7ccaSDan Williams int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled, 863d8f7ccaSDan Williams resource_size_t base, resource_size_t len, 873d8f7ccaSDan Williams resource_size_t skipped); 883d135db5SBen Widawsky 897592d935SDan Williams static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port, 907592d935SDan Williams struct cxl_memdev *cxlmd) 917592d935SDan Williams { 927592d935SDan Williams if (!port) 937592d935SDan Williams return NULL; 947592d935SDan Williams 957592d935SDan Williams return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev); 967592d935SDan Williams } 977592d935SDan Williams 985161a55cSBen Widawsky /** 99b64955a9SDan Williams * struct cxl_mbox_cmd - A command to be submitted to hardware. 100b64955a9SDan Williams * @opcode: (input) The command set and command submitted to hardware. 101b64955a9SDan Williams * @payload_in: (input) Pointer to the input payload. 102b64955a9SDan Williams * @payload_out: (output) Pointer to the output payload. Must be allocated by 103b64955a9SDan Williams * the caller. 104b64955a9SDan Williams * @size_in: (input) Number of bytes to load from @payload_in. 105b64955a9SDan Williams * @size_out: (input) Max number of bytes loaded into @payload_out. 106b64955a9SDan Williams * (output) Number of bytes generated by the device. For fixed size 107b64955a9SDan Williams * outputs commands this is always expected to be deterministic. For 108b64955a9SDan Williams * variable sized output commands, it tells the exact number of bytes 109b64955a9SDan Williams * written. 1102aeaf663SDan Williams * @min_out: (input) internal command output payload size validation 111b64955a9SDan Williams * @return_code: (output) Error code returned from hardware. 112b64955a9SDan Williams * 113b64955a9SDan Williams * This is the primary mechanism used to send commands to the hardware. 114b64955a9SDan Williams * All the fields except @payload_* correspond exactly to the fields described in 115b64955a9SDan Williams * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and 116b64955a9SDan Williams * @payload_out are written to, and read from the Command Payload Registers 117b64955a9SDan Williams * defined in CXL 2.0 8.2.8.4.8. 118b64955a9SDan Williams */ 119b64955a9SDan Williams struct cxl_mbox_cmd { 120b64955a9SDan Williams u16 opcode; 121b64955a9SDan Williams void *payload_in; 122b64955a9SDan Williams void *payload_out; 123b64955a9SDan Williams size_t size_in; 124b64955a9SDan Williams size_t size_out; 1252aeaf663SDan Williams size_t min_out; 126b64955a9SDan Williams u16 return_code; 127b64955a9SDan Williams }; 128b64955a9SDan Williams 129b64955a9SDan Williams /* 13092fcc1abSDavidlohr Bueso * Per CXL 2.0 Section 8.2.8.4.5.1 13192fcc1abSDavidlohr Bueso */ 13292fcc1abSDavidlohr Bueso #define CMD_CMD_RC_TABLE \ 13392fcc1abSDavidlohr Bueso C(SUCCESS, 0, NULL), \ 13492fcc1abSDavidlohr Bueso C(BACKGROUND, -ENXIO, "background cmd started successfully"), \ 13592fcc1abSDavidlohr Bueso C(INPUT, -ENXIO, "cmd input was invalid"), \ 13692fcc1abSDavidlohr Bueso C(UNSUPPORTED, -ENXIO, "cmd is not supported"), \ 13792fcc1abSDavidlohr Bueso C(INTERNAL, -ENXIO, "internal device error"), \ 13892fcc1abSDavidlohr Bueso C(RETRY, -ENXIO, "temporary error, retry once"), \ 13992fcc1abSDavidlohr Bueso C(BUSY, -ENXIO, "ongoing background operation"), \ 14092fcc1abSDavidlohr Bueso C(MEDIADISABLED, -ENXIO, "media access is disabled"), \ 14192fcc1abSDavidlohr Bueso C(FWINPROGRESS, -ENXIO, "one FW package can be transferred at a time"), \ 14292fcc1abSDavidlohr Bueso C(FWOOO, -ENXIO, "FW package content was transferred out of order"), \ 14392fcc1abSDavidlohr Bueso C(FWAUTH, -ENXIO, "FW package authentication failed"), \ 14492fcc1abSDavidlohr Bueso C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"), \ 14592fcc1abSDavidlohr Bueso C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"), \ 14692fcc1abSDavidlohr Bueso C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"), \ 14792fcc1abSDavidlohr Bueso C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"), \ 14892fcc1abSDavidlohr Bueso C(PADDR, -ENXIO, "physical address specified is invalid"), \ 14992fcc1abSDavidlohr Bueso C(POISONLMT, -ENXIO, "poison injection limit has been reached"), \ 15092fcc1abSDavidlohr Bueso C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"), \ 15192fcc1abSDavidlohr Bueso C(ABORT, -ENXIO, "background cmd was aborted by device"), \ 15292fcc1abSDavidlohr Bueso C(SECURITY, -ENXIO, "not valid in the current security state"), \ 15392fcc1abSDavidlohr Bueso C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"), \ 15492fcc1abSDavidlohr Bueso C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\ 15592fcc1abSDavidlohr Bueso C(PAYLOADLEN, -ENXIO, "invalid payload length") 15692fcc1abSDavidlohr Bueso 15792fcc1abSDavidlohr Bueso #undef C 15892fcc1abSDavidlohr Bueso #define C(a, b, c) CXL_MBOX_CMD_RC_##a 15992fcc1abSDavidlohr Bueso enum { CMD_CMD_RC_TABLE }; 16092fcc1abSDavidlohr Bueso #undef C 16192fcc1abSDavidlohr Bueso #define C(a, b, c) { b, c } 16292fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc { 16392fcc1abSDavidlohr Bueso int err; 16492fcc1abSDavidlohr Bueso const char *desc; 16592fcc1abSDavidlohr Bueso }; 16692fcc1abSDavidlohr Bueso 16792fcc1abSDavidlohr Bueso static const 16892fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE }; 16992fcc1abSDavidlohr Bueso #undef C 17092fcc1abSDavidlohr Bueso 17192fcc1abSDavidlohr Bueso static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd) 17292fcc1abSDavidlohr Bueso { 17392fcc1abSDavidlohr Bueso return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc; 17492fcc1abSDavidlohr Bueso } 17592fcc1abSDavidlohr Bueso 17692fcc1abSDavidlohr Bueso static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd) 17792fcc1abSDavidlohr Bueso { 17892fcc1abSDavidlohr Bueso return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err; 17992fcc1abSDavidlohr Bueso } 18092fcc1abSDavidlohr Bueso 18192fcc1abSDavidlohr Bueso /* 182b64955a9SDan Williams * CXL 2.0 - Memory capacity multiplier 183b64955a9SDan Williams * See Section 8.2.9.5 184b64955a9SDan Williams * 185b64955a9SDan Williams * Volatile, Persistent, and Partition capacities are specified to be in 186b64955a9SDan Williams * multiples of 256MB - define a multiplier to convert to/from bytes. 187b64955a9SDan Williams */ 188b64955a9SDan Williams #define CXL_CAPACITY_MULTIPLIER SZ_256M 189b64955a9SDan Williams 190b64955a9SDan Williams /** 191a49aa814SDavidlohr Bueso * Event Interrupt Policy 192a49aa814SDavidlohr Bueso * 193a49aa814SDavidlohr Bueso * CXL rev 3.0 section 8.2.9.2.4; Table 8-52 194560f7855SBen Widawsky */ 195a49aa814SDavidlohr Bueso enum cxl_event_int_mode { 196a49aa814SDavidlohr Bueso CXL_INT_NONE = 0x00, 197a49aa814SDavidlohr Bueso CXL_INT_MSI_MSIX = 0x01, 198a49aa814SDavidlohr Bueso CXL_INT_FW = 0x02 199a49aa814SDavidlohr Bueso }; 200a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy { 201a49aa814SDavidlohr Bueso u8 info_settings; 202a49aa814SDavidlohr Bueso u8 warn_settings; 203a49aa814SDavidlohr Bueso u8 failure_settings; 204a49aa814SDavidlohr Bueso u8 fatal_settings; 205a49aa814SDavidlohr Bueso } __packed; 206a49aa814SDavidlohr Bueso 207a49aa814SDavidlohr Bueso /** 2086ebe28f9SIra Weiny * struct cxl_event_state - Event log driver state 2096ebe28f9SIra Weiny * 2106ebe28f9SIra Weiny * @event_buf: Buffer to receive event data 2116ebe28f9SIra Weiny * @event_log_lock: Serialize event_buf and log use 2126ebe28f9SIra Weiny */ 2136ebe28f9SIra Weiny struct cxl_event_state { 2146ebe28f9SIra Weiny struct cxl_get_event_payload *buf; 2156ebe28f9SIra Weiny struct mutex log_lock; 216560f7855SBen Widawsky }; 217560f7855SBen Widawsky 218*d0abf578SAlison Schofield /* Device enabled poison commands */ 219*d0abf578SAlison Schofield enum poison_cmd_enabled_bits { 220*d0abf578SAlison Schofield CXL_POISON_ENABLED_LIST, 221*d0abf578SAlison Schofield CXL_POISON_ENABLED_INJECT, 222*d0abf578SAlison Schofield CXL_POISON_ENABLED_CLEAR, 223*d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_CAPS, 224*d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_MEDIA, 225*d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_RESULTS, 226*d0abf578SAlison Schofield CXL_POISON_ENABLED_MAX 227*d0abf578SAlison Schofield }; 228*d0abf578SAlison Schofield 229*d0abf578SAlison Schofield /** 230*d0abf578SAlison Schofield * struct cxl_poison_state - Driver poison state info 231*d0abf578SAlison Schofield * 232*d0abf578SAlison Schofield * @max_errors: Maximum media error records held in device cache 233*d0abf578SAlison Schofield * @enabled_cmds: All poison commands enabled in the CEL 234*d0abf578SAlison Schofield * @list_out: The poison list payload returned by device 235*d0abf578SAlison Schofield * @lock: Protect reads of the poison list 236*d0abf578SAlison Schofield * 237*d0abf578SAlison Schofield * Reads of the poison list are synchronized to ensure that a reader 238*d0abf578SAlison Schofield * does not get an incomplete list because their request overlapped 239*d0abf578SAlison Schofield * (was interrupted or preceded by) another read request of the same 240*d0abf578SAlison Schofield * DPA range. CXL Spec 3.0 Section 8.2.9.8.4.1 241*d0abf578SAlison Schofield */ 242*d0abf578SAlison Schofield struct cxl_poison_state { 243*d0abf578SAlison Schofield u32 max_errors; 244*d0abf578SAlison Schofield DECLARE_BITMAP(enabled_cmds, CXL_POISON_ENABLED_MAX); 245*d0abf578SAlison Schofield struct cxl_mbox_poison_out *list_out; 246*d0abf578SAlison Schofield struct mutex lock; /* Protect reads of poison list */ 247*d0abf578SAlison Schofield }; 248*d0abf578SAlison Schofield 249560f7855SBen Widawsky /** 2505e2411aeSIra Weiny * struct cxl_dev_state - The driver device state 2515e2411aeSIra Weiny * 2525e2411aeSIra Weiny * cxl_dev_state represents the CXL driver/device state. It provides an 2535e2411aeSIra Weiny * interface to mailbox commands as well as some cached data about the device. 2545e2411aeSIra Weiny * Currently only memory devices are represented. 2555e2411aeSIra Weiny * 2565e2411aeSIra Weiny * @dev: The device associated with this CXL state 2572905cb52SDan Williams * @cxlmd: The device representing the CXL.mem capabilities of @dev 2585161a55cSBen Widawsky * @regs: Parsed register blocks 25906e279e5SBen Widawsky * @cxl_dvsec: Offset to the PCIe device DVSEC 2600a19bfc8SDan Williams * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH) 2615161a55cSBen Widawsky * @payload_size: Size of space for payload 2625161a55cSBen Widawsky * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) 2635161a55cSBen Widawsky * @lsa_size: Size of Label Storage Area 2645161a55cSBen Widawsky * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device) 2655161a55cSBen Widawsky * @mbox_mutex: Mutex to synchronize mailbox access. 2665161a55cSBen Widawsky * @firmware_version: Firmware version for the memory device. 2675161a55cSBen Widawsky * @enabled_cmds: Hardware commands found enabled in CEL. 26812f3856aSDan Williams * @exclusive_cmds: Commands that are kernel-internal only 269d3b75029SDan Williams * @dpa_res: Overall DPA resource tree for the device 270d3b75029SDan Williams * @pmem_res: Active Persistent memory capacity configuration 271d3b75029SDan Williams * @ram_res: Active Volatile memory capacity configuration 27213e7749dSDan Williams * @total_bytes: sum of all possible capacities 27313e7749dSDan Williams * @volatile_only_bytes: hard volatile capacity 27413e7749dSDan Williams * @persistent_only_bytes: hard persistent capacity 27513e7749dSDan Williams * @partition_align_bytes: alignment size for partition-able capacity 27613e7749dSDan Williams * @active_volatile_bytes: sum of hard + soft volatile 27713e7749dSDan Williams * @active_persistent_bytes: sum of hard + soft persistent 27813e7749dSDan Williams * @next_volatile_bytes: volatile capacity change pending device reset 27913e7749dSDan Williams * @next_persistent_bytes: persistent capacity change pending device reset 2804112a08dSBen Widawsky * @component_reg_phys: register base of component registers 281560f7855SBen Widawsky * @info: Cached DVSEC information about the device. 282bcc79ea3SDan Williams * @serial: PCIe Device Serial Number 2833eddcc93SIra Weiny * @doe_mbs: PCI DOE mailbox array 2841bb31131SAlison Schofield * @event: event log driver state 285*d0abf578SAlison Schofield * @poison: poison driver state info 286b64955a9SDan Williams * @mbox_send: @dev specific transport for transmitting mailbox commands 28713e7749dSDan Williams * 28813e7749dSDan Williams * See section 8.2.9.5.2 Capacity Configuration and Label Storage for 28913e7749dSDan Williams * details on capacity parameters. 2905161a55cSBen Widawsky */ 2915e2411aeSIra Weiny struct cxl_dev_state { 29299e222a5SDan Williams struct device *dev; 2932905cb52SDan Williams struct cxl_memdev *cxlmd; 2945161a55cSBen Widawsky 2955161a55cSBen Widawsky struct cxl_regs regs; 29606e279e5SBen Widawsky int cxl_dvsec; 2975161a55cSBen Widawsky 2980a19bfc8SDan Williams bool rcd; 2995161a55cSBen Widawsky size_t payload_size; 3005161a55cSBen Widawsky size_t lsa_size; 3015161a55cSBen Widawsky struct mutex mbox_mutex; /* Protects device mailbox and firmware */ 3025161a55cSBen Widawsky char firmware_version[0x10]; 303ff56ab9eSDan Williams DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX); 30412f3856aSDan Williams DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX); 3055161a55cSBen Widawsky 306d3b75029SDan Williams struct resource dpa_res; 307d3b75029SDan Williams struct resource pmem_res; 308d3b75029SDan Williams struct resource ram_res; 3090b9159d0SIra Weiny u64 total_bytes; 3100b9159d0SIra Weiny u64 volatile_only_bytes; 3110b9159d0SIra Weiny u64 persistent_only_bytes; 3120b9159d0SIra Weiny u64 partition_align_bytes; 313f847502aSIra Weiny 314f847502aSIra Weiny u64 active_volatile_bytes; 315f847502aSIra Weiny u64 active_persistent_bytes; 316f847502aSIra Weiny u64 next_volatile_bytes; 317f847502aSIra Weiny u64 next_persistent_bytes; 318b64955a9SDan Williams 3194112a08dSBen Widawsky resource_size_t component_reg_phys; 320bcc79ea3SDan Williams u64 serial; 3214112a08dSBen Widawsky 3223eddcc93SIra Weiny struct xarray doe_mbs; 3233eddcc93SIra Weiny 3246ebe28f9SIra Weiny struct cxl_event_state event; 325*d0abf578SAlison Schofield struct cxl_poison_state poison; 3266ebe28f9SIra Weiny 3275e2411aeSIra Weiny int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd); 3285161a55cSBen Widawsky }; 3294faf31b4SDan Williams 3304faf31b4SDan Williams enum cxl_opcode { 3314faf31b4SDan Williams CXL_MBOX_OP_INVALID = 0x0000, 3324faf31b4SDan Williams CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID, 3336ebe28f9SIra Weiny CXL_MBOX_OP_GET_EVENT_RECORD = 0x0100, 3346ebe28f9SIra Weiny CXL_MBOX_OP_CLEAR_EVENT_RECORD = 0x0101, 335a49aa814SDavidlohr Bueso CXL_MBOX_OP_GET_EVT_INT_POLICY = 0x0102, 336a49aa814SDavidlohr Bueso CXL_MBOX_OP_SET_EVT_INT_POLICY = 0x0103, 3374faf31b4SDan Williams CXL_MBOX_OP_GET_FW_INFO = 0x0200, 3384faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW = 0x0202, 339fa884345SJonathan Cameron CXL_MBOX_OP_SET_TIMESTAMP = 0x0301, 3404faf31b4SDan Williams CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400, 3414faf31b4SDan Williams CXL_MBOX_OP_GET_LOG = 0x0401, 3424faf31b4SDan Williams CXL_MBOX_OP_IDENTIFY = 0x4000, 3434faf31b4SDan Williams CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100, 3444faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101, 3454faf31b4SDan Williams CXL_MBOX_OP_GET_LSA = 0x4102, 3464faf31b4SDan Williams CXL_MBOX_OP_SET_LSA = 0x4103, 3474faf31b4SDan Williams CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200, 3484faf31b4SDan Williams CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201, 3494faf31b4SDan Williams CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202, 3504faf31b4SDan Williams CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203, 3514faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204, 3524faf31b4SDan Williams CXL_MBOX_OP_GET_POISON = 0x4300, 3534faf31b4SDan Williams CXL_MBOX_OP_INJECT_POISON = 0x4301, 3544faf31b4SDan Williams CXL_MBOX_OP_CLEAR_POISON = 0x4302, 3554faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303, 3564faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA = 0x4304, 3574faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305, 35832828115SDave Jiang CXL_MBOX_OP_GET_SECURITY_STATE = 0x4500, 35999746940SDave Jiang CXL_MBOX_OP_SET_PASSPHRASE = 0x4501, 360c4ef680dSDave Jiang CXL_MBOX_OP_DISABLE_PASSPHRASE = 0x4502, 3612bb692f7SDave Jiang CXL_MBOX_OP_UNLOCK = 0x4503, 362a072f7b7SDave Jiang CXL_MBOX_OP_FREEZE_SECURITY = 0x4504, 3633b502e88SDave Jiang CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE = 0x4505, 3644faf31b4SDan Williams CXL_MBOX_OP_MAX = 0x10000 3654faf31b4SDan Williams }; 3664faf31b4SDan Williams 36749be6dd8SDan Williams #define DEFINE_CXL_CEL_UUID \ 36849be6dd8SDan Williams UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \ 36949be6dd8SDan Williams 0x3b, 0x3f, 0x17) 37049be6dd8SDan Williams 37149be6dd8SDan Williams #define DEFINE_CXL_VENDOR_DEBUG_UUID \ 37249be6dd8SDan Williams UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \ 37349be6dd8SDan Williams 0x40, 0x3d, 0x86) 37449be6dd8SDan Williams 37549be6dd8SDan Williams struct cxl_mbox_get_supported_logs { 37649be6dd8SDan Williams __le16 entries; 37749be6dd8SDan Williams u8 rsvd[6]; 37849be6dd8SDan Williams struct cxl_gsl_entry { 37949be6dd8SDan Williams uuid_t uuid; 38049be6dd8SDan Williams __le32 size; 38149be6dd8SDan Williams } __packed entry[]; 38249be6dd8SDan Williams } __packed; 38349be6dd8SDan Williams 38449be6dd8SDan Williams struct cxl_cel_entry { 38549be6dd8SDan Williams __le16 opcode; 38649be6dd8SDan Williams __le16 effect; 38749be6dd8SDan Williams } __packed; 38849be6dd8SDan Williams 38949be6dd8SDan Williams struct cxl_mbox_get_log { 39049be6dd8SDan Williams uuid_t uuid; 39149be6dd8SDan Williams __le32 offset; 39249be6dd8SDan Williams __le32 length; 39349be6dd8SDan Williams } __packed; 39449be6dd8SDan Williams 39549be6dd8SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 39649be6dd8SDan Williams struct cxl_mbox_identify { 39749be6dd8SDan Williams char fw_revision[0x10]; 39849be6dd8SDan Williams __le64 total_capacity; 39949be6dd8SDan Williams __le64 volatile_capacity; 40049be6dd8SDan Williams __le64 persistent_capacity; 40149be6dd8SDan Williams __le64 partition_align; 40249be6dd8SDan Williams __le16 info_event_log_size; 40349be6dd8SDan Williams __le16 warning_event_log_size; 40449be6dd8SDan Williams __le16 failure_event_log_size; 40549be6dd8SDan Williams __le16 fatal_event_log_size; 40649be6dd8SDan Williams __le32 lsa_size; 40749be6dd8SDan Williams u8 poison_list_max_mer[3]; 40849be6dd8SDan Williams __le16 inject_poison_limit; 40949be6dd8SDan Williams u8 poison_caps; 41049be6dd8SDan Williams u8 qos_telemetry_caps; 41149be6dd8SDan Williams } __packed; 41249be6dd8SDan Williams 4136ebe28f9SIra Weiny /* 4146ebe28f9SIra Weiny * Common Event Record Format 4156ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.1; Table 8-42 4166ebe28f9SIra Weiny */ 4176ebe28f9SIra Weiny struct cxl_event_record_hdr { 4186ebe28f9SIra Weiny uuid_t id; 4196ebe28f9SIra Weiny u8 length; 4206ebe28f9SIra Weiny u8 flags[3]; 4216ebe28f9SIra Weiny __le16 handle; 4226ebe28f9SIra Weiny __le16 related_handle; 4236ebe28f9SIra Weiny __le64 timestamp; 4246ebe28f9SIra Weiny u8 maint_op_class; 4256ebe28f9SIra Weiny u8 reserved[15]; 4266ebe28f9SIra Weiny } __packed; 4276ebe28f9SIra Weiny 4286ebe28f9SIra Weiny #define CXL_EVENT_RECORD_DATA_LENGTH 0x50 4296ebe28f9SIra Weiny struct cxl_event_record_raw { 4306ebe28f9SIra Weiny struct cxl_event_record_hdr hdr; 4316ebe28f9SIra Weiny u8 data[CXL_EVENT_RECORD_DATA_LENGTH]; 4326ebe28f9SIra Weiny } __packed; 4336ebe28f9SIra Weiny 4346ebe28f9SIra Weiny /* 4356ebe28f9SIra Weiny * Get Event Records output payload 4366ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.2; Table 8-50 4376ebe28f9SIra Weiny */ 4386ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_OVERFLOW BIT(0) 4396ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_MORE_RECORDS BIT(1) 4406ebe28f9SIra Weiny struct cxl_get_event_payload { 4416ebe28f9SIra Weiny u8 flags; 4426ebe28f9SIra Weiny u8 reserved1; 4436ebe28f9SIra Weiny __le16 overflow_err_count; 4446ebe28f9SIra Weiny __le64 first_overflow_timestamp; 4456ebe28f9SIra Weiny __le64 last_overflow_timestamp; 4466ebe28f9SIra Weiny __le16 record_count; 4476ebe28f9SIra Weiny u8 reserved2[10]; 4486ebe28f9SIra Weiny struct cxl_event_record_raw records[]; 4496ebe28f9SIra Weiny } __packed; 4506ebe28f9SIra Weiny 4516ebe28f9SIra Weiny /* 4526ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.2; Table 8-49 4536ebe28f9SIra Weiny */ 4546ebe28f9SIra Weiny enum cxl_event_log_type { 4556ebe28f9SIra Weiny CXL_EVENT_TYPE_INFO = 0x00, 4566ebe28f9SIra Weiny CXL_EVENT_TYPE_WARN, 4576ebe28f9SIra Weiny CXL_EVENT_TYPE_FAIL, 4586ebe28f9SIra Weiny CXL_EVENT_TYPE_FATAL, 4596ebe28f9SIra Weiny CXL_EVENT_TYPE_MAX 4606ebe28f9SIra Weiny }; 4616ebe28f9SIra Weiny 4626ebe28f9SIra Weiny /* 4636ebe28f9SIra Weiny * Clear Event Records input payload 4646ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.3; Table 8-51 4656ebe28f9SIra Weiny */ 4666ebe28f9SIra Weiny struct cxl_mbox_clear_event_payload { 4676ebe28f9SIra Weiny u8 event_log; /* enum cxl_event_log_type */ 4686ebe28f9SIra Weiny u8 clear_flags; 4696ebe28f9SIra Weiny u8 nr_recs; 4706ebe28f9SIra Weiny u8 reserved[3]; 4716ebe28f9SIra Weiny __le16 handles[]; 4726ebe28f9SIra Weiny } __packed; 4736ebe28f9SIra Weiny #define CXL_CLEAR_EVENT_MAX_HANDLES U8_MAX 4746ebe28f9SIra Weiny 475d54a531aSIra Weiny /* 476d54a531aSIra Weiny * General Media Event Record 477d54a531aSIra Weiny * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43 478d54a531aSIra Weiny */ 479d54a531aSIra Weiny #define CXL_EVENT_GEN_MED_COMP_ID_SIZE 0x10 480d54a531aSIra Weiny struct cxl_event_gen_media { 481d54a531aSIra Weiny struct cxl_event_record_hdr hdr; 482d54a531aSIra Weiny __le64 phys_addr; 483d54a531aSIra Weiny u8 descriptor; 484d54a531aSIra Weiny u8 type; 485d54a531aSIra Weiny u8 transaction_type; 486d54a531aSIra Weiny u8 validity_flags[2]; 487d54a531aSIra Weiny u8 channel; 488d54a531aSIra Weiny u8 rank; 489d54a531aSIra Weiny u8 device[3]; 490d54a531aSIra Weiny u8 component_id[CXL_EVENT_GEN_MED_COMP_ID_SIZE]; 491d54a531aSIra Weiny u8 reserved[46]; 492d54a531aSIra Weiny } __packed; 493d54a531aSIra Weiny 4942d6c1e6dSIra Weiny /* 4952d6c1e6dSIra Weiny * DRAM Event Record - DER 4962d6c1e6dSIra Weiny * CXL rev 3.0 section 8.2.9.2.1.2; Table 3-44 4972d6c1e6dSIra Weiny */ 4982d6c1e6dSIra Weiny #define CXL_EVENT_DER_CORRECTION_MASK_SIZE 0x20 4992d6c1e6dSIra Weiny struct cxl_event_dram { 5002d6c1e6dSIra Weiny struct cxl_event_record_hdr hdr; 5012d6c1e6dSIra Weiny __le64 phys_addr; 5022d6c1e6dSIra Weiny u8 descriptor; 5032d6c1e6dSIra Weiny u8 type; 5042d6c1e6dSIra Weiny u8 transaction_type; 5052d6c1e6dSIra Weiny u8 validity_flags[2]; 5062d6c1e6dSIra Weiny u8 channel; 5072d6c1e6dSIra Weiny u8 rank; 5082d6c1e6dSIra Weiny u8 nibble_mask[3]; 5092d6c1e6dSIra Weiny u8 bank_group; 5102d6c1e6dSIra Weiny u8 bank; 5112d6c1e6dSIra Weiny u8 row[3]; 5122d6c1e6dSIra Weiny u8 column[2]; 5132d6c1e6dSIra Weiny u8 correction_mask[CXL_EVENT_DER_CORRECTION_MASK_SIZE]; 5142d6c1e6dSIra Weiny u8 reserved[0x17]; 5152d6c1e6dSIra Weiny } __packed; 5162d6c1e6dSIra Weiny 51795b49479SIra Weiny /* 51895b49479SIra Weiny * Get Health Info Record 51995b49479SIra Weiny * CXL rev 3.0 section 8.2.9.8.3.1; Table 8-100 52095b49479SIra Weiny */ 52195b49479SIra Weiny struct cxl_get_health_info { 52295b49479SIra Weiny u8 health_status; 52395b49479SIra Weiny u8 media_status; 52495b49479SIra Weiny u8 add_status; 52595b49479SIra Weiny u8 life_used; 52695b49479SIra Weiny u8 device_temp[2]; 52795b49479SIra Weiny u8 dirty_shutdown_cnt[4]; 52895b49479SIra Weiny u8 cor_vol_err_cnt[4]; 52995b49479SIra Weiny u8 cor_per_err_cnt[4]; 53095b49479SIra Weiny } __packed; 53195b49479SIra Weiny 53295b49479SIra Weiny /* 53395b49479SIra Weiny * Memory Module Event Record 53495b49479SIra Weiny * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45 53595b49479SIra Weiny */ 53695b49479SIra Weiny struct cxl_event_mem_module { 53795b49479SIra Weiny struct cxl_event_record_hdr hdr; 53895b49479SIra Weiny u8 event_type; 53995b49479SIra Weiny struct cxl_get_health_info info; 54095b49479SIra Weiny u8 reserved[0x3d]; 54195b49479SIra Weiny } __packed; 54295b49479SIra Weiny 543e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info { 544e7ad1bf6SDan Williams __le64 active_volatile_cap; 545e7ad1bf6SDan Williams __le64 active_persistent_cap; 546e7ad1bf6SDan Williams __le64 next_volatile_cap; 547e7ad1bf6SDan Williams __le64 next_persistent_cap; 548e7ad1bf6SDan Williams } __packed; 549e7ad1bf6SDan Williams 55049be6dd8SDan Williams struct cxl_mbox_get_lsa { 5518a664875SAlison Schofield __le32 offset; 5528a664875SAlison Schofield __le32 length; 55349be6dd8SDan Williams } __packed; 55449be6dd8SDan Williams 55549be6dd8SDan Williams struct cxl_mbox_set_lsa { 5568a664875SAlison Schofield __le32 offset; 5578a664875SAlison Schofield __le32 reserved; 55849be6dd8SDan Williams u8 data[]; 55949be6dd8SDan Williams } __packed; 56049be6dd8SDan Williams 5616179045cSAlison Schofield struct cxl_mbox_set_partition_info { 5626179045cSAlison Schofield __le64 volatile_capacity; 5636179045cSAlison Schofield u8 flags; 5646179045cSAlison Schofield } __packed; 5656179045cSAlison Schofield 5666179045cSAlison Schofield #define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0) 5676179045cSAlison Schofield 568fa884345SJonathan Cameron /* Set Timestamp CXL 3.0 Spec 8.2.9.4.2 */ 569fa884345SJonathan Cameron struct cxl_mbox_set_timestamp_in { 570fa884345SJonathan Cameron __le64 timestamp; 571fa884345SJonathan Cameron 572fa884345SJonathan Cameron } __packed; 573fa884345SJonathan Cameron 5744faf31b4SDan Williams /** 5754faf31b4SDan Williams * struct cxl_mem_command - Driver representation of a memory device command 5764faf31b4SDan Williams * @info: Command information as it exists for the UAPI 5774faf31b4SDan Williams * @opcode: The actual bits used for the mailbox protocol 5784faf31b4SDan Williams * @flags: Set of flags effecting driver behavior. 5794faf31b4SDan Williams * 5804faf31b4SDan Williams * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag 5814faf31b4SDan Williams * will be enabled by the driver regardless of what hardware may have 5824faf31b4SDan Williams * advertised. 5834faf31b4SDan Williams * 5844faf31b4SDan Williams * The cxl_mem_command is the driver's internal representation of commands that 5854faf31b4SDan Williams * are supported by the driver. Some of these commands may not be supported by 5864faf31b4SDan Williams * the hardware. The driver will use @info to validate the fields passed in by 5874faf31b4SDan Williams * the user then submit the @opcode to the hardware. 5884faf31b4SDan Williams * 5894faf31b4SDan Williams * See struct cxl_command_info. 5904faf31b4SDan Williams */ 5914faf31b4SDan Williams struct cxl_mem_command { 5924faf31b4SDan Williams struct cxl_command_info info; 5934faf31b4SDan Williams enum cxl_opcode opcode; 5944faf31b4SDan Williams u32 flags; 5954faf31b4SDan Williams #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0) 5964faf31b4SDan Williams }; 5974faf31b4SDan Williams 59832828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PASS_SET 0x01 59932828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET 0x02 60032828115SDave Jiang #define CXL_PMEM_SEC_STATE_LOCKED 0x04 60132828115SDave Jiang #define CXL_PMEM_SEC_STATE_FROZEN 0x08 60232828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PLIMIT 0x10 60332828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT 0x20 60432828115SDave Jiang 60599746940SDave Jiang /* set passphrase input payload */ 60699746940SDave Jiang struct cxl_set_pass { 60799746940SDave Jiang u8 type; 60899746940SDave Jiang u8 reserved[31]; 60999746940SDave Jiang /* CXL field using NVDIMM define, same length */ 61099746940SDave Jiang u8 old_pass[NVDIMM_PASSPHRASE_LEN]; 61199746940SDave Jiang u8 new_pass[NVDIMM_PASSPHRASE_LEN]; 61299746940SDave Jiang } __packed; 61399746940SDave Jiang 614c4ef680dSDave Jiang /* disable passphrase input payload */ 615c4ef680dSDave Jiang struct cxl_disable_pass { 616c4ef680dSDave Jiang u8 type; 617c4ef680dSDave Jiang u8 reserved[31]; 618c4ef680dSDave Jiang u8 pass[NVDIMM_PASSPHRASE_LEN]; 619c4ef680dSDave Jiang } __packed; 620c4ef680dSDave Jiang 6213b502e88SDave Jiang /* passphrase secure erase payload */ 6223b502e88SDave Jiang struct cxl_pass_erase { 6233b502e88SDave Jiang u8 type; 6243b502e88SDave Jiang u8 reserved[31]; 6253b502e88SDave Jiang u8 pass[NVDIMM_PASSPHRASE_LEN]; 6263b502e88SDave Jiang } __packed; 6273b502e88SDave Jiang 62899746940SDave Jiang enum { 62999746940SDave Jiang CXL_PMEM_SEC_PASS_MASTER = 0, 63099746940SDave Jiang CXL_PMEM_SEC_PASS_USER, 63199746940SDave Jiang }; 63299746940SDave Jiang 6335331cdf4SDan Williams int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, 6345331cdf4SDan Williams struct cxl_mbox_cmd *cmd); 6355e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds); 6362e4ba0ecSDan Williams int cxl_await_media_ready(struct cxl_dev_state *cxlds); 6375e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds); 6385e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds); 6395e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev); 6405e2411aeSIra Weiny void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds); 6415e2411aeSIra Weiny void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds); 6426ebe28f9SIra Weiny void cxl_mem_get_event_records(struct cxl_dev_state *cxlds, u32 status); 643fa884345SJonathan Cameron int cxl_set_timestamp(struct cxl_dev_state *cxlds); 644*d0abf578SAlison Schofield int cxl_poison_state_init(struct cxl_dev_state *cxlds); 645fa884345SJonathan Cameron 6469ea4dcf4SDan Williams #ifdef CONFIG_CXL_SUSPEND 6479ea4dcf4SDan Williams void cxl_mem_active_inc(void); 6489ea4dcf4SDan Williams void cxl_mem_active_dec(void); 6499ea4dcf4SDan Williams #else 6509ea4dcf4SDan Williams static inline void cxl_mem_active_inc(void) 6519ea4dcf4SDan Williams { 6529ea4dcf4SDan Williams } 6539ea4dcf4SDan Williams static inline void cxl_mem_active_dec(void) 6549ea4dcf4SDan Williams { 6559ea4dcf4SDan Williams } 6569ea4dcf4SDan Williams #endif 657d17d0540SDan Williams 658d17d0540SDan Williams struct cxl_hdm { 659d17d0540SDan Williams struct cxl_component_regs regs; 660d17d0540SDan Williams unsigned int decoder_count; 661d17d0540SDan Williams unsigned int target_count; 662d17d0540SDan Williams unsigned int interleave_mask; 663d17d0540SDan Williams struct cxl_port *port; 664d17d0540SDan Williams }; 665cc2a4878SDan Williams 666cc2a4878SDan Williams struct seq_file; 667cc2a4878SDan Williams struct dentry *cxl_debugfs_create_dir(const char *dir); 668cc2a4878SDan Williams void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds); 6695161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */ 670