xref: /linux/drivers/cxl/cxlmem.h (revision 3d8f7ccaa611a743ae3a1e6f605346993d37c513)
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>
75161a55cSBen Widawsky #include "cxl.h"
85161a55cSBen Widawsky 
95161a55cSBen Widawsky /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
105161a55cSBen Widawsky #define CXLMDEV_STATUS_OFFSET 0x0
115161a55cSBen Widawsky #define   CXLMDEV_DEV_FATAL BIT(0)
125161a55cSBen Widawsky #define   CXLMDEV_FW_HALT BIT(1)
135161a55cSBen Widawsky #define   CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
145161a55cSBen Widawsky #define     CXLMDEV_MS_NOT_READY 0
155161a55cSBen Widawsky #define     CXLMDEV_MS_READY 1
165161a55cSBen Widawsky #define     CXLMDEV_MS_ERROR 2
175161a55cSBen Widawsky #define     CXLMDEV_MS_DISABLED 3
185161a55cSBen Widawsky #define CXLMDEV_READY(status)                                                  \
195161a55cSBen Widawsky 	(FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) ==                \
205161a55cSBen Widawsky 	 CXLMDEV_MS_READY)
215161a55cSBen Widawsky #define   CXLMDEV_MBOX_IF_READY BIT(4)
225161a55cSBen Widawsky #define   CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
235161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_NOT 0
245161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_COLD 1
255161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_WARM 2
265161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_HOT 3
275161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_CXL 4
285161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED(status)                                           \
295161a55cSBen Widawsky 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
305161a55cSBen Widawsky 	 CXLMDEV_RESET_NEEDED_NOT)
315161a55cSBen Widawsky 
325161a55cSBen Widawsky /**
335161a55cSBen Widawsky  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
345161a55cSBen Widawsky  * @dev: driver core device object
355161a55cSBen Widawsky  * @cdev: char dev core object for ioctl operations
365e2411aeSIra Weiny  * @cxlds: The device state backing this device
378dd2bc0fSBen Widawsky  * @detach_work: active memdev lost a port in its ancestry
38f17b558dSDan Williams  * @cxl_nvb: coordinate removal of @cxl_nvd if present
39f17b558dSDan Williams  * @cxl_nvd: optional bridge to an nvdimm if the device supports pmem
405161a55cSBen Widawsky  * @id: id number of this memdev instance.
412345df54SDan Williams  * @depth: endpoint port depth
425161a55cSBen Widawsky  */
435161a55cSBen Widawsky struct cxl_memdev {
445161a55cSBen Widawsky 	struct device dev;
455161a55cSBen Widawsky 	struct cdev cdev;
465e2411aeSIra Weiny 	struct cxl_dev_state *cxlds;
478dd2bc0fSBen Widawsky 	struct work_struct detach_work;
48f17b558dSDan Williams 	struct cxl_nvdimm_bridge *cxl_nvb;
49f17b558dSDan Williams 	struct cxl_nvdimm *cxl_nvd;
505161a55cSBen Widawsky 	int id;
512345df54SDan Williams 	int depth;
525161a55cSBen Widawsky };
535161a55cSBen Widawsky 
543d135db5SBen Widawsky static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
553d135db5SBen Widawsky {
563d135db5SBen Widawsky 	return container_of(dev, struct cxl_memdev, dev);
573d135db5SBen Widawsky }
583d135db5SBen Widawsky 
599c57cde0SDan Williams static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled)
609c57cde0SDan Williams {
619c57cde0SDan Williams 	return to_cxl_port(cxled->cxld.dev.parent);
629c57cde0SDan Williams }
639c57cde0SDan Williams 
64384e624bSDan Williams static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd)
65384e624bSDan Williams {
66384e624bSDan Williams 	return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
67384e624bSDan Williams }
68384e624bSDan Williams 
699c57cde0SDan Williams static inline struct cxl_memdev *
709c57cde0SDan Williams cxled_to_memdev(struct cxl_endpoint_decoder *cxled)
719c57cde0SDan Williams {
729c57cde0SDan Williams 	struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent);
739c57cde0SDan Williams 
749c57cde0SDan Williams 	return to_cxl_memdev(port->uport);
759c57cde0SDan Williams }
769c57cde0SDan Williams 
778dd2bc0fSBen Widawsky bool is_cxl_memdev(struct device *dev);
788dd2bc0fSBen Widawsky static inline bool is_cxl_endpoint(struct cxl_port *port)
798dd2bc0fSBen Widawsky {
808dd2bc0fSBen Widawsky 	return is_cxl_memdev(port->uport);
818dd2bc0fSBen Widawsky }
828dd2bc0fSBen Widawsky 
835e2411aeSIra Weiny struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds);
84*3d8f7ccaSDan Williams int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
85*3d8f7ccaSDan Williams 			 resource_size_t base, resource_size_t len,
86*3d8f7ccaSDan Williams 			 resource_size_t skipped);
873d135db5SBen Widawsky 
887592d935SDan Williams static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
897592d935SDan Williams 					 struct cxl_memdev *cxlmd)
907592d935SDan Williams {
917592d935SDan Williams 	if (!port)
927592d935SDan Williams 		return NULL;
937592d935SDan Williams 
947592d935SDan Williams 	return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev);
957592d935SDan Williams }
967592d935SDan Williams 
975161a55cSBen Widawsky /**
98b64955a9SDan Williams  * struct cxl_mbox_cmd - A command to be submitted to hardware.
99b64955a9SDan Williams  * @opcode: (input) The command set and command submitted to hardware.
100b64955a9SDan Williams  * @payload_in: (input) Pointer to the input payload.
101b64955a9SDan Williams  * @payload_out: (output) Pointer to the output payload. Must be allocated by
102b64955a9SDan Williams  *		 the caller.
103b64955a9SDan Williams  * @size_in: (input) Number of bytes to load from @payload_in.
104b64955a9SDan Williams  * @size_out: (input) Max number of bytes loaded into @payload_out.
105b64955a9SDan Williams  *            (output) Number of bytes generated by the device. For fixed size
106b64955a9SDan Williams  *            outputs commands this is always expected to be deterministic. For
107b64955a9SDan Williams  *            variable sized output commands, it tells the exact number of bytes
108b64955a9SDan Williams  *            written.
1092aeaf663SDan Williams  * @min_out: (input) internal command output payload size validation
110b64955a9SDan Williams  * @return_code: (output) Error code returned from hardware.
111b64955a9SDan Williams  *
112b64955a9SDan Williams  * This is the primary mechanism used to send commands to the hardware.
113b64955a9SDan Williams  * All the fields except @payload_* correspond exactly to the fields described in
114b64955a9SDan Williams  * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and
115b64955a9SDan Williams  * @payload_out are written to, and read from the Command Payload Registers
116b64955a9SDan Williams  * defined in CXL 2.0 8.2.8.4.8.
117b64955a9SDan Williams  */
118b64955a9SDan Williams struct cxl_mbox_cmd {
119b64955a9SDan Williams 	u16 opcode;
120b64955a9SDan Williams 	void *payload_in;
121b64955a9SDan Williams 	void *payload_out;
122b64955a9SDan Williams 	size_t size_in;
123b64955a9SDan Williams 	size_t size_out;
1242aeaf663SDan Williams 	size_t min_out;
125b64955a9SDan Williams 	u16 return_code;
126b64955a9SDan Williams };
127b64955a9SDan Williams 
128b64955a9SDan Williams /*
12992fcc1abSDavidlohr Bueso  * Per CXL 2.0 Section 8.2.8.4.5.1
13092fcc1abSDavidlohr Bueso  */
13192fcc1abSDavidlohr Bueso #define CMD_CMD_RC_TABLE							\
13292fcc1abSDavidlohr Bueso 	C(SUCCESS, 0, NULL),							\
13392fcc1abSDavidlohr Bueso 	C(BACKGROUND, -ENXIO, "background cmd started successfully"),           \
13492fcc1abSDavidlohr Bueso 	C(INPUT, -ENXIO, "cmd input was invalid"),				\
13592fcc1abSDavidlohr Bueso 	C(UNSUPPORTED, -ENXIO, "cmd is not supported"),				\
13692fcc1abSDavidlohr Bueso 	C(INTERNAL, -ENXIO, "internal device error"),				\
13792fcc1abSDavidlohr Bueso 	C(RETRY, -ENXIO, "temporary error, retry once"),			\
13892fcc1abSDavidlohr Bueso 	C(BUSY, -ENXIO, "ongoing background operation"),			\
13992fcc1abSDavidlohr Bueso 	C(MEDIADISABLED, -ENXIO, "media access is disabled"),			\
14092fcc1abSDavidlohr Bueso 	C(FWINPROGRESS, -ENXIO,	"one FW package can be transferred at a time"), \
14192fcc1abSDavidlohr Bueso 	C(FWOOO, -ENXIO, "FW package content was transferred out of order"),    \
14292fcc1abSDavidlohr Bueso 	C(FWAUTH, -ENXIO, "FW package authentication failed"),			\
14392fcc1abSDavidlohr Bueso 	C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"),  \
14492fcc1abSDavidlohr Bueso 	C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"),         \
14592fcc1abSDavidlohr Bueso 	C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"),		\
14692fcc1abSDavidlohr Bueso 	C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"),     \
14792fcc1abSDavidlohr Bueso 	C(PADDR, -ENXIO, "physical address specified is invalid"),		\
14892fcc1abSDavidlohr Bueso 	C(POISONLMT, -ENXIO, "poison injection limit has been reached"),        \
14992fcc1abSDavidlohr Bueso 	C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"),		\
15092fcc1abSDavidlohr Bueso 	C(ABORT, -ENXIO, "background cmd was aborted by device"),               \
15192fcc1abSDavidlohr Bueso 	C(SECURITY, -ENXIO, "not valid in the current security state"),         \
15292fcc1abSDavidlohr Bueso 	C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"),   \
15392fcc1abSDavidlohr Bueso 	C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\
15492fcc1abSDavidlohr Bueso 	C(PAYLOADLEN, -ENXIO, "invalid payload length")
15592fcc1abSDavidlohr Bueso 
15692fcc1abSDavidlohr Bueso #undef C
15792fcc1abSDavidlohr Bueso #define C(a, b, c) CXL_MBOX_CMD_RC_##a
15892fcc1abSDavidlohr Bueso enum  { CMD_CMD_RC_TABLE };
15992fcc1abSDavidlohr Bueso #undef C
16092fcc1abSDavidlohr Bueso #define C(a, b, c) { b, c }
16192fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc {
16292fcc1abSDavidlohr Bueso 	int err;
16392fcc1abSDavidlohr Bueso 	const char *desc;
16492fcc1abSDavidlohr Bueso };
16592fcc1abSDavidlohr Bueso 
16692fcc1abSDavidlohr Bueso static const
16792fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
16892fcc1abSDavidlohr Bueso #undef C
16992fcc1abSDavidlohr Bueso 
17092fcc1abSDavidlohr Bueso static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
17192fcc1abSDavidlohr Bueso {
17292fcc1abSDavidlohr Bueso 	return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
17392fcc1abSDavidlohr Bueso }
17492fcc1abSDavidlohr Bueso 
17592fcc1abSDavidlohr Bueso static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
17692fcc1abSDavidlohr Bueso {
17792fcc1abSDavidlohr Bueso 	return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
17892fcc1abSDavidlohr Bueso }
17992fcc1abSDavidlohr Bueso 
18092fcc1abSDavidlohr Bueso /*
181b64955a9SDan Williams  * CXL 2.0 - Memory capacity multiplier
182b64955a9SDan Williams  * See Section 8.2.9.5
183b64955a9SDan Williams  *
184b64955a9SDan Williams  * Volatile, Persistent, and Partition capacities are specified to be in
185b64955a9SDan Williams  * multiples of 256MB - define a multiplier to convert to/from bytes.
186b64955a9SDan Williams  */
187b64955a9SDan Williams #define CXL_CAPACITY_MULTIPLIER SZ_256M
188b64955a9SDan Williams 
189b64955a9SDan Williams /**
190560f7855SBen Widawsky  * struct cxl_endpoint_dvsec_info - Cached DVSEC info
191560f7855SBen Widawsky  * @mem_enabled: cached value of mem_enabled in the DVSEC, PCIE_DEVICE
192560f7855SBen Widawsky  * @ranges: Number of active HDM ranges this device uses.
193560f7855SBen Widawsky  * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE
194560f7855SBen Widawsky  */
195560f7855SBen Widawsky struct cxl_endpoint_dvsec_info {
196560f7855SBen Widawsky 	bool mem_enabled;
197560f7855SBen Widawsky 	int ranges;
198560f7855SBen Widawsky 	struct range dvsec_range[2];
199560f7855SBen Widawsky };
200560f7855SBen Widawsky 
201560f7855SBen Widawsky /**
2025e2411aeSIra Weiny  * struct cxl_dev_state - The driver device state
2035e2411aeSIra Weiny  *
2045e2411aeSIra Weiny  * cxl_dev_state represents the CXL driver/device state.  It provides an
2055e2411aeSIra Weiny  * interface to mailbox commands as well as some cached data about the device.
2065e2411aeSIra Weiny  * Currently only memory devices are represented.
2075e2411aeSIra Weiny  *
2085e2411aeSIra Weiny  * @dev: The device associated with this CXL state
2092905cb52SDan Williams  * @cxlmd: The device representing the CXL.mem capabilities of @dev
2105161a55cSBen Widawsky  * @regs: Parsed register blocks
21106e279e5SBen Widawsky  * @cxl_dvsec: Offset to the PCIe device DVSEC
2120a19bfc8SDan Williams  * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
2135161a55cSBen Widawsky  * @payload_size: Size of space for payload
2145161a55cSBen Widawsky  *                (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
2155161a55cSBen Widawsky  * @lsa_size: Size of Label Storage Area
2165161a55cSBen Widawsky  *                (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
2175161a55cSBen Widawsky  * @mbox_mutex: Mutex to synchronize mailbox access.
2185161a55cSBen Widawsky  * @firmware_version: Firmware version for the memory device.
2195161a55cSBen Widawsky  * @enabled_cmds: Hardware commands found enabled in CEL.
22012f3856aSDan Williams  * @exclusive_cmds: Commands that are kernel-internal only
221d3b75029SDan Williams  * @dpa_res: Overall DPA resource tree for the device
222d3b75029SDan Williams  * @pmem_res: Active Persistent memory capacity configuration
223d3b75029SDan Williams  * @ram_res: Active Volatile memory capacity configuration
22413e7749dSDan Williams  * @total_bytes: sum of all possible capacities
22513e7749dSDan Williams  * @volatile_only_bytes: hard volatile capacity
22613e7749dSDan Williams  * @persistent_only_bytes: hard persistent capacity
22713e7749dSDan Williams  * @partition_align_bytes: alignment size for partition-able capacity
22813e7749dSDan Williams  * @active_volatile_bytes: sum of hard + soft volatile
22913e7749dSDan Williams  * @active_persistent_bytes: sum of hard + soft persistent
23013e7749dSDan Williams  * @next_volatile_bytes: volatile capacity change pending device reset
23113e7749dSDan Williams  * @next_persistent_bytes: persistent capacity change pending device reset
2324112a08dSBen Widawsky  * @component_reg_phys: register base of component registers
233560f7855SBen Widawsky  * @info: Cached DVSEC information about the device.
234bcc79ea3SDan Williams  * @serial: PCIe Device Serial Number
2353eddcc93SIra Weiny  * @doe_mbs: PCI DOE mailbox array
236b64955a9SDan Williams  * @mbox_send: @dev specific transport for transmitting mailbox commands
23713e7749dSDan Williams  *
23813e7749dSDan Williams  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
23913e7749dSDan Williams  * details on capacity parameters.
2405161a55cSBen Widawsky  */
2415e2411aeSIra Weiny struct cxl_dev_state {
24299e222a5SDan Williams 	struct device *dev;
2432905cb52SDan Williams 	struct cxl_memdev *cxlmd;
2445161a55cSBen Widawsky 
2455161a55cSBen Widawsky 	struct cxl_regs regs;
24606e279e5SBen Widawsky 	int cxl_dvsec;
2475161a55cSBen Widawsky 
2480a19bfc8SDan Williams 	bool rcd;
2495161a55cSBen Widawsky 	size_t payload_size;
2505161a55cSBen Widawsky 	size_t lsa_size;
2515161a55cSBen Widawsky 	struct mutex mbox_mutex; /* Protects device mailbox and firmware */
2525161a55cSBen Widawsky 	char firmware_version[0x10];
253ff56ab9eSDan Williams 	DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
25412f3856aSDan Williams 	DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
2555161a55cSBen Widawsky 
256d3b75029SDan Williams 	struct resource dpa_res;
257d3b75029SDan Williams 	struct resource pmem_res;
258d3b75029SDan Williams 	struct resource ram_res;
2590b9159d0SIra Weiny 	u64 total_bytes;
2600b9159d0SIra Weiny 	u64 volatile_only_bytes;
2610b9159d0SIra Weiny 	u64 persistent_only_bytes;
2620b9159d0SIra Weiny 	u64 partition_align_bytes;
263f847502aSIra Weiny 
264f847502aSIra Weiny 	u64 active_volatile_bytes;
265f847502aSIra Weiny 	u64 active_persistent_bytes;
266f847502aSIra Weiny 	u64 next_volatile_bytes;
267f847502aSIra Weiny 	u64 next_persistent_bytes;
268b64955a9SDan Williams 
2694112a08dSBen Widawsky 	resource_size_t component_reg_phys;
270bcc79ea3SDan Williams 	u64 serial;
2714112a08dSBen Widawsky 
2723eddcc93SIra Weiny 	struct xarray doe_mbs;
2733eddcc93SIra Weiny 
2745e2411aeSIra Weiny 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
2755161a55cSBen Widawsky };
2764faf31b4SDan Williams 
2774faf31b4SDan Williams enum cxl_opcode {
2784faf31b4SDan Williams 	CXL_MBOX_OP_INVALID		= 0x0000,
2794faf31b4SDan Williams 	CXL_MBOX_OP_RAW			= CXL_MBOX_OP_INVALID,
2804faf31b4SDan Williams 	CXL_MBOX_OP_GET_FW_INFO		= 0x0200,
2814faf31b4SDan Williams 	CXL_MBOX_OP_ACTIVATE_FW		= 0x0202,
2824faf31b4SDan Williams 	CXL_MBOX_OP_GET_SUPPORTED_LOGS	= 0x0400,
2834faf31b4SDan Williams 	CXL_MBOX_OP_GET_LOG		= 0x0401,
2844faf31b4SDan Williams 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
2854faf31b4SDan Williams 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
2864faf31b4SDan Williams 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
2874faf31b4SDan Williams 	CXL_MBOX_OP_GET_LSA		= 0x4102,
2884faf31b4SDan Williams 	CXL_MBOX_OP_SET_LSA		= 0x4103,
2894faf31b4SDan Williams 	CXL_MBOX_OP_GET_HEALTH_INFO	= 0x4200,
2904faf31b4SDan Williams 	CXL_MBOX_OP_GET_ALERT_CONFIG	= 0x4201,
2914faf31b4SDan Williams 	CXL_MBOX_OP_SET_ALERT_CONFIG	= 0x4202,
2924faf31b4SDan Williams 	CXL_MBOX_OP_GET_SHUTDOWN_STATE	= 0x4203,
2934faf31b4SDan Williams 	CXL_MBOX_OP_SET_SHUTDOWN_STATE	= 0x4204,
2944faf31b4SDan Williams 	CXL_MBOX_OP_GET_POISON		= 0x4300,
2954faf31b4SDan Williams 	CXL_MBOX_OP_INJECT_POISON	= 0x4301,
2964faf31b4SDan Williams 	CXL_MBOX_OP_CLEAR_POISON	= 0x4302,
2974faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS	= 0x4303,
2984faf31b4SDan Williams 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
2994faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
30032828115SDave Jiang 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
30199746940SDave Jiang 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
302c4ef680dSDave Jiang 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
3032bb692f7SDave Jiang 	CXL_MBOX_OP_UNLOCK		= 0x4503,
304a072f7b7SDave Jiang 	CXL_MBOX_OP_FREEZE_SECURITY	= 0x4504,
3053b502e88SDave Jiang 	CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE	= 0x4505,
3064faf31b4SDan Williams 	CXL_MBOX_OP_MAX			= 0x10000
3074faf31b4SDan Williams };
3084faf31b4SDan Williams 
30949be6dd8SDan Williams #define DEFINE_CXL_CEL_UUID                                                    \
31049be6dd8SDan Williams 	UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62,     \
31149be6dd8SDan Williams 		  0x3b, 0x3f, 0x17)
31249be6dd8SDan Williams 
31349be6dd8SDan Williams #define DEFINE_CXL_VENDOR_DEBUG_UUID                                           \
31449be6dd8SDan Williams 	UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19,     \
31549be6dd8SDan Williams 		  0x40, 0x3d, 0x86)
31649be6dd8SDan Williams 
31749be6dd8SDan Williams struct cxl_mbox_get_supported_logs {
31849be6dd8SDan Williams 	__le16 entries;
31949be6dd8SDan Williams 	u8 rsvd[6];
32049be6dd8SDan Williams 	struct cxl_gsl_entry {
32149be6dd8SDan Williams 		uuid_t uuid;
32249be6dd8SDan Williams 		__le32 size;
32349be6dd8SDan Williams 	} __packed entry[];
32449be6dd8SDan Williams }  __packed;
32549be6dd8SDan Williams 
32649be6dd8SDan Williams struct cxl_cel_entry {
32749be6dd8SDan Williams 	__le16 opcode;
32849be6dd8SDan Williams 	__le16 effect;
32949be6dd8SDan Williams } __packed;
33049be6dd8SDan Williams 
33149be6dd8SDan Williams struct cxl_mbox_get_log {
33249be6dd8SDan Williams 	uuid_t uuid;
33349be6dd8SDan Williams 	__le32 offset;
33449be6dd8SDan Williams 	__le32 length;
33549be6dd8SDan Williams } __packed;
33649be6dd8SDan Williams 
33749be6dd8SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
33849be6dd8SDan Williams struct cxl_mbox_identify {
33949be6dd8SDan Williams 	char fw_revision[0x10];
34049be6dd8SDan Williams 	__le64 total_capacity;
34149be6dd8SDan Williams 	__le64 volatile_capacity;
34249be6dd8SDan Williams 	__le64 persistent_capacity;
34349be6dd8SDan Williams 	__le64 partition_align;
34449be6dd8SDan Williams 	__le16 info_event_log_size;
34549be6dd8SDan Williams 	__le16 warning_event_log_size;
34649be6dd8SDan Williams 	__le16 failure_event_log_size;
34749be6dd8SDan Williams 	__le16 fatal_event_log_size;
34849be6dd8SDan Williams 	__le32 lsa_size;
34949be6dd8SDan Williams 	u8 poison_list_max_mer[3];
35049be6dd8SDan Williams 	__le16 inject_poison_limit;
35149be6dd8SDan Williams 	u8 poison_caps;
35249be6dd8SDan Williams 	u8 qos_telemetry_caps;
35349be6dd8SDan Williams } __packed;
35449be6dd8SDan Williams 
355e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info {
356e7ad1bf6SDan Williams 	__le64 active_volatile_cap;
357e7ad1bf6SDan Williams 	__le64 active_persistent_cap;
358e7ad1bf6SDan Williams 	__le64 next_volatile_cap;
359e7ad1bf6SDan Williams 	__le64 next_persistent_cap;
360e7ad1bf6SDan Williams } __packed;
361e7ad1bf6SDan Williams 
36249be6dd8SDan Williams struct cxl_mbox_get_lsa {
3638a664875SAlison Schofield 	__le32 offset;
3648a664875SAlison Schofield 	__le32 length;
36549be6dd8SDan Williams } __packed;
36649be6dd8SDan Williams 
36749be6dd8SDan Williams struct cxl_mbox_set_lsa {
3688a664875SAlison Schofield 	__le32 offset;
3698a664875SAlison Schofield 	__le32 reserved;
37049be6dd8SDan Williams 	u8 data[];
37149be6dd8SDan Williams } __packed;
37249be6dd8SDan Williams 
3736179045cSAlison Schofield struct cxl_mbox_set_partition_info {
3746179045cSAlison Schofield 	__le64 volatile_capacity;
3756179045cSAlison Schofield 	u8 flags;
3766179045cSAlison Schofield } __packed;
3776179045cSAlison Schofield 
3786179045cSAlison Schofield #define  CXL_SET_PARTITION_IMMEDIATE_FLAG	BIT(0)
3796179045cSAlison Schofield 
3804faf31b4SDan Williams /**
3814faf31b4SDan Williams  * struct cxl_mem_command - Driver representation of a memory device command
3824faf31b4SDan Williams  * @info: Command information as it exists for the UAPI
3834faf31b4SDan Williams  * @opcode: The actual bits used for the mailbox protocol
3844faf31b4SDan Williams  * @flags: Set of flags effecting driver behavior.
3854faf31b4SDan Williams  *
3864faf31b4SDan Williams  *  * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
3874faf31b4SDan Williams  *    will be enabled by the driver regardless of what hardware may have
3884faf31b4SDan Williams  *    advertised.
3894faf31b4SDan Williams  *
3904faf31b4SDan Williams  * The cxl_mem_command is the driver's internal representation of commands that
3914faf31b4SDan Williams  * are supported by the driver. Some of these commands may not be supported by
3924faf31b4SDan Williams  * the hardware. The driver will use @info to validate the fields passed in by
3934faf31b4SDan Williams  * the user then submit the @opcode to the hardware.
3944faf31b4SDan Williams  *
3954faf31b4SDan Williams  * See struct cxl_command_info.
3964faf31b4SDan Williams  */
3974faf31b4SDan Williams struct cxl_mem_command {
3984faf31b4SDan Williams 	struct cxl_command_info info;
3994faf31b4SDan Williams 	enum cxl_opcode opcode;
4004faf31b4SDan Williams 	u32 flags;
4014faf31b4SDan Williams #define CXL_CMD_FLAG_NONE 0
4024faf31b4SDan Williams #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
4034faf31b4SDan Williams };
4044faf31b4SDan Williams 
40532828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PASS_SET	0x01
40632828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET	0x02
40732828115SDave Jiang #define CXL_PMEM_SEC_STATE_LOCKED		0x04
40832828115SDave Jiang #define CXL_PMEM_SEC_STATE_FROZEN		0x08
40932828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PLIMIT		0x10
41032828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT	0x20
41132828115SDave Jiang 
41299746940SDave Jiang /* set passphrase input payload */
41399746940SDave Jiang struct cxl_set_pass {
41499746940SDave Jiang 	u8 type;
41599746940SDave Jiang 	u8 reserved[31];
41699746940SDave Jiang 	/* CXL field using NVDIMM define, same length */
41799746940SDave Jiang 	u8 old_pass[NVDIMM_PASSPHRASE_LEN];
41899746940SDave Jiang 	u8 new_pass[NVDIMM_PASSPHRASE_LEN];
41999746940SDave Jiang } __packed;
42099746940SDave Jiang 
421c4ef680dSDave Jiang /* disable passphrase input payload */
422c4ef680dSDave Jiang struct cxl_disable_pass {
423c4ef680dSDave Jiang 	u8 type;
424c4ef680dSDave Jiang 	u8 reserved[31];
425c4ef680dSDave Jiang 	u8 pass[NVDIMM_PASSPHRASE_LEN];
426c4ef680dSDave Jiang } __packed;
427c4ef680dSDave Jiang 
4283b502e88SDave Jiang /* passphrase secure erase payload */
4293b502e88SDave Jiang struct cxl_pass_erase {
4303b502e88SDave Jiang 	u8 type;
4313b502e88SDave Jiang 	u8 reserved[31];
4323b502e88SDave Jiang 	u8 pass[NVDIMM_PASSPHRASE_LEN];
4333b502e88SDave Jiang } __packed;
4343b502e88SDave Jiang 
43599746940SDave Jiang enum {
43699746940SDave Jiang 	CXL_PMEM_SEC_PASS_MASTER = 0,
43799746940SDave Jiang 	CXL_PMEM_SEC_PASS_USER,
43899746940SDave Jiang };
43999746940SDave Jiang 
4405331cdf4SDan Williams int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
4415331cdf4SDan Williams 			  struct cxl_mbox_cmd *cmd);
4425e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds);
4432e4ba0ecSDan Williams int cxl_await_media_ready(struct cxl_dev_state *cxlds);
4445e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds);
4455e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds);
4465e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev);
4475e2411aeSIra Weiny void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
4485e2411aeSIra Weiny void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
4499ea4dcf4SDan Williams #ifdef CONFIG_CXL_SUSPEND
4509ea4dcf4SDan Williams void cxl_mem_active_inc(void);
4519ea4dcf4SDan Williams void cxl_mem_active_dec(void);
4529ea4dcf4SDan Williams #else
4539ea4dcf4SDan Williams static inline void cxl_mem_active_inc(void)
4549ea4dcf4SDan Williams {
4559ea4dcf4SDan Williams }
4569ea4dcf4SDan Williams static inline void cxl_mem_active_dec(void)
4579ea4dcf4SDan Williams {
4589ea4dcf4SDan Williams }
4599ea4dcf4SDan Williams #endif
460d17d0540SDan Williams 
461d17d0540SDan Williams struct cxl_hdm {
462d17d0540SDan Williams 	struct cxl_component_regs regs;
463d17d0540SDan Williams 	unsigned int decoder_count;
464d17d0540SDan Williams 	unsigned int target_count;
465d17d0540SDan Williams 	unsigned int interleave_mask;
466d17d0540SDan Williams 	struct cxl_port *port;
467d17d0540SDan Williams };
468cc2a4878SDan Williams 
469cc2a4878SDan Williams struct seq_file;
470cc2a4878SDan Williams struct dentry *cxl_debugfs_create_dir(const char *dir);
471cc2a4878SDan Williams void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
4725161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */
473