xref: /linux/drivers/cxl/cxlmem.h (revision f6b8ab32e3ec48ecc02d1b4a42ee03576040ddd2)
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 /*
130bfe58458SDavidlohr Bueso  * Per CXL 3.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"),     \
1487ff6ad10SAlison Schofield 	C(PADDR, -EFAULT, "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"),\
155bfe58458SDavidlohr Bueso 	C(PAYLOADLEN, -ENXIO, "invalid payload length"),			\
156bfe58458SDavidlohr Bueso 	C(LOG, -ENXIO, "invalid or unsupported log page"),			\
157bfe58458SDavidlohr Bueso 	C(INTERRUPTED, -ENXIO, "asynchronous event occured"),			\
158bfe58458SDavidlohr Bueso 	C(FEATUREVERSION, -ENXIO, "unsupported feature version"),		\
159bfe58458SDavidlohr Bueso 	C(FEATURESELVALUE, -ENXIO, "unsupported feature selection value"),	\
160bfe58458SDavidlohr Bueso 	C(FEATURETRANSFERIP, -ENXIO, "feature transfer in progress"),		\
161bfe58458SDavidlohr Bueso 	C(FEATURETRANSFEROOO, -ENXIO, "feature transfer out of order"),		\
162bfe58458SDavidlohr Bueso 	C(RESOURCEEXHAUSTED, -ENXIO, "resources are exhausted"),		\
163bfe58458SDavidlohr Bueso 	C(EXTLIST, -ENXIO, "invalid Extent List"),				\
16492fcc1abSDavidlohr Bueso 
16592fcc1abSDavidlohr Bueso #undef C
16692fcc1abSDavidlohr Bueso #define C(a, b, c) CXL_MBOX_CMD_RC_##a
16792fcc1abSDavidlohr Bueso enum  { CMD_CMD_RC_TABLE };
16892fcc1abSDavidlohr Bueso #undef C
16992fcc1abSDavidlohr Bueso #define C(a, b, c) { b, c }
17092fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc {
17192fcc1abSDavidlohr Bueso 	int err;
17292fcc1abSDavidlohr Bueso 	const char *desc;
17392fcc1abSDavidlohr Bueso };
17492fcc1abSDavidlohr Bueso 
17592fcc1abSDavidlohr Bueso static const
17692fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
17792fcc1abSDavidlohr Bueso #undef C
17892fcc1abSDavidlohr Bueso 
17992fcc1abSDavidlohr Bueso static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
18092fcc1abSDavidlohr Bueso {
18192fcc1abSDavidlohr Bueso 	return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
18292fcc1abSDavidlohr Bueso }
18392fcc1abSDavidlohr Bueso 
18492fcc1abSDavidlohr Bueso static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
18592fcc1abSDavidlohr Bueso {
18692fcc1abSDavidlohr Bueso 	return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
18792fcc1abSDavidlohr Bueso }
18892fcc1abSDavidlohr Bueso 
18992fcc1abSDavidlohr Bueso /*
190b64955a9SDan Williams  * CXL 2.0 - Memory capacity multiplier
191b64955a9SDan Williams  * See Section 8.2.9.5
192b64955a9SDan Williams  *
193b64955a9SDan Williams  * Volatile, Persistent, and Partition capacities are specified to be in
194b64955a9SDan Williams  * multiples of 256MB - define a multiplier to convert to/from bytes.
195b64955a9SDan Williams  */
196b64955a9SDan Williams #define CXL_CAPACITY_MULTIPLIER SZ_256M
197b64955a9SDan Williams 
198c192e543SDan Williams /*
199a49aa814SDavidlohr Bueso  * Event Interrupt Policy
200a49aa814SDavidlohr Bueso  *
201a49aa814SDavidlohr Bueso  * CXL rev 3.0 section 8.2.9.2.4; Table 8-52
202560f7855SBen Widawsky  */
203a49aa814SDavidlohr Bueso enum cxl_event_int_mode {
204a49aa814SDavidlohr Bueso 	CXL_INT_NONE		= 0x00,
205a49aa814SDavidlohr Bueso 	CXL_INT_MSI_MSIX	= 0x01,
206a49aa814SDavidlohr Bueso 	CXL_INT_FW		= 0x02
207a49aa814SDavidlohr Bueso };
208a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy {
209a49aa814SDavidlohr Bueso 	u8 info_settings;
210a49aa814SDavidlohr Bueso 	u8 warn_settings;
211a49aa814SDavidlohr Bueso 	u8 failure_settings;
212a49aa814SDavidlohr Bueso 	u8 fatal_settings;
213a49aa814SDavidlohr Bueso } __packed;
214a49aa814SDavidlohr Bueso 
215a49aa814SDavidlohr Bueso /**
2166ebe28f9SIra Weiny  * struct cxl_event_state - Event log driver state
2176ebe28f9SIra Weiny  *
218c192e543SDan Williams  * @buf: Buffer to receive event data
219c192e543SDan Williams  * @log_lock: Serialize event_buf and log use
2206ebe28f9SIra Weiny  */
2216ebe28f9SIra Weiny struct cxl_event_state {
2226ebe28f9SIra Weiny 	struct cxl_get_event_payload *buf;
2236ebe28f9SIra Weiny 	struct mutex log_lock;
224560f7855SBen Widawsky };
225560f7855SBen Widawsky 
226d0abf578SAlison Schofield /* Device enabled poison commands */
227d0abf578SAlison Schofield enum poison_cmd_enabled_bits {
228d0abf578SAlison Schofield 	CXL_POISON_ENABLED_LIST,
229d0abf578SAlison Schofield 	CXL_POISON_ENABLED_INJECT,
230d0abf578SAlison Schofield 	CXL_POISON_ENABLED_CLEAR,
231d0abf578SAlison Schofield 	CXL_POISON_ENABLED_SCAN_CAPS,
232d0abf578SAlison Schofield 	CXL_POISON_ENABLED_SCAN_MEDIA,
233d0abf578SAlison Schofield 	CXL_POISON_ENABLED_SCAN_RESULTS,
234d0abf578SAlison Schofield 	CXL_POISON_ENABLED_MAX
235d0abf578SAlison Schofield };
236d0abf578SAlison Schofield 
237d0abf578SAlison Schofield /**
238d0abf578SAlison Schofield  * struct cxl_poison_state - Driver poison state info
239d0abf578SAlison Schofield  *
240d0abf578SAlison Schofield  * @max_errors: Maximum media error records held in device cache
241d0abf578SAlison Schofield  * @enabled_cmds: All poison commands enabled in the CEL
242d0abf578SAlison Schofield  * @list_out: The poison list payload returned by device
243d0abf578SAlison Schofield  * @lock: Protect reads of the poison list
244d0abf578SAlison Schofield  *
245d0abf578SAlison Schofield  * Reads of the poison list are synchronized to ensure that a reader
246d0abf578SAlison Schofield  * does not get an incomplete list because their request overlapped
247d0abf578SAlison Schofield  * (was interrupted or preceded by) another read request of the same
248d0abf578SAlison Schofield  * DPA range. CXL Spec 3.0 Section 8.2.9.8.4.1
249d0abf578SAlison Schofield  */
250d0abf578SAlison Schofield struct cxl_poison_state {
251d0abf578SAlison Schofield 	u32 max_errors;
252d0abf578SAlison Schofield 	DECLARE_BITMAP(enabled_cmds, CXL_POISON_ENABLED_MAX);
253d0abf578SAlison Schofield 	struct cxl_mbox_poison_out *list_out;
254d0abf578SAlison Schofield 	struct mutex lock;  /* Protect reads of poison list */
255d0abf578SAlison Schofield };
256d0abf578SAlison Schofield 
257*f6b8ab32SDan Williams /*
258*f6b8ab32SDan Williams  * enum cxl_devtype - delineate type-2 from a generic type-3 device
259*f6b8ab32SDan Williams  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
260*f6b8ab32SDan Williams  *			 HDM-DB, no requirement that this device implements a
261*f6b8ab32SDan Williams  *			 mailbox, or other memory-device-standard manageability
262*f6b8ab32SDan Williams  *			 flows.
263*f6b8ab32SDan Williams  * @CXL_DEVTYPE_CLASSMEM - Common class definition of a CXL Type-3 device with
264*f6b8ab32SDan Williams  *			   HDM-H and class-mandatory memory device registers
265*f6b8ab32SDan Williams  */
266*f6b8ab32SDan Williams enum cxl_devtype {
267*f6b8ab32SDan Williams 	CXL_DEVTYPE_DEVMEM,
268*f6b8ab32SDan Williams 	CXL_DEVTYPE_CLASSMEM,
269*f6b8ab32SDan Williams };
270*f6b8ab32SDan Williams 
271560f7855SBen Widawsky /**
2725e2411aeSIra Weiny  * struct cxl_dev_state - The driver device state
2735e2411aeSIra Weiny  *
2745e2411aeSIra Weiny  * cxl_dev_state represents the CXL driver/device state.  It provides an
2755e2411aeSIra Weiny  * interface to mailbox commands as well as some cached data about the device.
2765e2411aeSIra Weiny  * Currently only memory devices are represented.
2775e2411aeSIra Weiny  *
2785e2411aeSIra Weiny  * @dev: The device associated with this CXL state
2792905cb52SDan Williams  * @cxlmd: The device representing the CXL.mem capabilities of @dev
2805161a55cSBen Widawsky  * @regs: Parsed register blocks
28106e279e5SBen Widawsky  * @cxl_dvsec: Offset to the PCIe device DVSEC
2820a19bfc8SDan Williams  * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
283e764f122SDave Jiang  * @media_ready: Indicate whether the device media is usable
28459f8d151SDan Williams  * @dpa_res: Overall DPA resource tree for the device
28559f8d151SDan Williams  * @pmem_res: Active Persistent memory capacity configuration
28659f8d151SDan Williams  * @ram_res: Active Volatile memory capacity configuration
28759f8d151SDan Williams  * @component_reg_phys: register base of component registers
28859f8d151SDan Williams  * @serial: PCIe Device Serial Number
289*f6b8ab32SDan Williams  * @type: Generic Memory Class device or Vendor Specific Memory device
29059f8d151SDan Williams  */
29159f8d151SDan Williams struct cxl_dev_state {
29259f8d151SDan Williams 	struct device *dev;
29359f8d151SDan Williams 	struct cxl_memdev *cxlmd;
29459f8d151SDan Williams 	struct cxl_regs regs;
29559f8d151SDan Williams 	int cxl_dvsec;
29659f8d151SDan Williams 	bool rcd;
29759f8d151SDan Williams 	bool media_ready;
29859f8d151SDan Williams 	struct resource dpa_res;
29959f8d151SDan Williams 	struct resource pmem_res;
30059f8d151SDan Williams 	struct resource ram_res;
30159f8d151SDan Williams 	resource_size_t component_reg_phys;
30259f8d151SDan Williams 	u64 serial;
303*f6b8ab32SDan Williams 	enum cxl_devtype type;
30459f8d151SDan Williams };
30559f8d151SDan Williams 
30659f8d151SDan Williams /**
30759f8d151SDan Williams  * struct cxl_memdev_state - Generic Type-3 Memory Device Class driver data
30859f8d151SDan Williams  *
30959f8d151SDan Williams  * CXL 8.1.12.1 PCI Header - Class Code Register Memory Device defines
31059f8d151SDan Williams  * common memory device functionality like the presence of a mailbox and
31159f8d151SDan Williams  * the functionality related to that like Identify Memory Device and Get
31259f8d151SDan Williams  * Partition Info
31359f8d151SDan Williams  * @cxlds: Core driver state common across Type-2 and Type-3 devices
3145161a55cSBen Widawsky  * @payload_size: Size of space for payload
3155161a55cSBen Widawsky  *                (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
3165161a55cSBen Widawsky  * @lsa_size: Size of Label Storage Area
3175161a55cSBen Widawsky  *                (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
3185161a55cSBen Widawsky  * @mbox_mutex: Mutex to synchronize mailbox access.
3195161a55cSBen Widawsky  * @firmware_version: Firmware version for the memory device.
3205161a55cSBen Widawsky  * @enabled_cmds: Hardware commands found enabled in CEL.
32112f3856aSDan Williams  * @exclusive_cmds: Commands that are kernel-internal only
32213e7749dSDan Williams  * @total_bytes: sum of all possible capacities
32313e7749dSDan Williams  * @volatile_only_bytes: hard volatile capacity
32413e7749dSDan Williams  * @persistent_only_bytes: hard persistent capacity
32513e7749dSDan Williams  * @partition_align_bytes: alignment size for partition-able capacity
32613e7749dSDan Williams  * @active_volatile_bytes: sum of hard + soft volatile
32713e7749dSDan Williams  * @active_persistent_bytes: sum of hard + soft persistent
32813e7749dSDan Williams  * @next_volatile_bytes: volatile capacity change pending device reset
32913e7749dSDan Williams  * @next_persistent_bytes: persistent capacity change pending device reset
3301bb31131SAlison Schofield  * @event: event log driver state
331d0abf578SAlison Schofield  * @poison: poison driver state info
332b64955a9SDan Williams  * @mbox_send: @dev specific transport for transmitting mailbox commands
33313e7749dSDan Williams  *
33459f8d151SDan Williams  * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
33513e7749dSDan Williams  * details on capacity parameters.
3365161a55cSBen Widawsky  */
33759f8d151SDan Williams struct cxl_memdev_state {
33859f8d151SDan Williams 	struct cxl_dev_state cxlds;
3395161a55cSBen Widawsky 	size_t payload_size;
3405161a55cSBen Widawsky 	size_t lsa_size;
3415161a55cSBen Widawsky 	struct mutex mbox_mutex; /* Protects device mailbox and firmware */
3425161a55cSBen Widawsky 	char firmware_version[0x10];
343ff56ab9eSDan Williams 	DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
34412f3856aSDan Williams 	DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
3450b9159d0SIra Weiny 	u64 total_bytes;
3460b9159d0SIra Weiny 	u64 volatile_only_bytes;
3470b9159d0SIra Weiny 	u64 persistent_only_bytes;
3480b9159d0SIra Weiny 	u64 partition_align_bytes;
349f847502aSIra Weiny 	u64 active_volatile_bytes;
350f847502aSIra Weiny 	u64 active_persistent_bytes;
351f847502aSIra Weiny 	u64 next_volatile_bytes;
352f847502aSIra Weiny 	u64 next_persistent_bytes;
3536ebe28f9SIra Weiny 	struct cxl_event_state event;
354d0abf578SAlison Schofield 	struct cxl_poison_state poison;
35559f8d151SDan Williams 	int (*mbox_send)(struct cxl_memdev_state *mds,
35659f8d151SDan Williams 			 struct cxl_mbox_cmd *cmd);
3575161a55cSBen Widawsky };
3584faf31b4SDan Williams 
35959f8d151SDan Williams static inline struct cxl_memdev_state *
36059f8d151SDan Williams to_cxl_memdev_state(struct cxl_dev_state *cxlds)
36159f8d151SDan Williams {
362*f6b8ab32SDan Williams 	if (cxlds->type != CXL_DEVTYPE_CLASSMEM)
363*f6b8ab32SDan Williams 		return NULL;
36459f8d151SDan Williams 	return container_of(cxlds, struct cxl_memdev_state, cxlds);
36559f8d151SDan Williams }
36659f8d151SDan Williams 
3674faf31b4SDan Williams enum cxl_opcode {
3684faf31b4SDan Williams 	CXL_MBOX_OP_INVALID		= 0x0000,
3694faf31b4SDan Williams 	CXL_MBOX_OP_RAW			= CXL_MBOX_OP_INVALID,
3706ebe28f9SIra Weiny 	CXL_MBOX_OP_GET_EVENT_RECORD	= 0x0100,
3716ebe28f9SIra Weiny 	CXL_MBOX_OP_CLEAR_EVENT_RECORD	= 0x0101,
372a49aa814SDavidlohr Bueso 	CXL_MBOX_OP_GET_EVT_INT_POLICY	= 0x0102,
373a49aa814SDavidlohr Bueso 	CXL_MBOX_OP_SET_EVT_INT_POLICY	= 0x0103,
3744faf31b4SDan Williams 	CXL_MBOX_OP_GET_FW_INFO		= 0x0200,
3754faf31b4SDan Williams 	CXL_MBOX_OP_ACTIVATE_FW		= 0x0202,
376fa884345SJonathan Cameron 	CXL_MBOX_OP_SET_TIMESTAMP	= 0x0301,
3774faf31b4SDan Williams 	CXL_MBOX_OP_GET_SUPPORTED_LOGS	= 0x0400,
3784faf31b4SDan Williams 	CXL_MBOX_OP_GET_LOG		= 0x0401,
3794faf31b4SDan Williams 	CXL_MBOX_OP_IDENTIFY		= 0x4000,
3804faf31b4SDan Williams 	CXL_MBOX_OP_GET_PARTITION_INFO	= 0x4100,
3814faf31b4SDan Williams 	CXL_MBOX_OP_SET_PARTITION_INFO	= 0x4101,
3824faf31b4SDan Williams 	CXL_MBOX_OP_GET_LSA		= 0x4102,
3834faf31b4SDan Williams 	CXL_MBOX_OP_SET_LSA		= 0x4103,
3844faf31b4SDan Williams 	CXL_MBOX_OP_GET_HEALTH_INFO	= 0x4200,
3854faf31b4SDan Williams 	CXL_MBOX_OP_GET_ALERT_CONFIG	= 0x4201,
3864faf31b4SDan Williams 	CXL_MBOX_OP_SET_ALERT_CONFIG	= 0x4202,
3874faf31b4SDan Williams 	CXL_MBOX_OP_GET_SHUTDOWN_STATE	= 0x4203,
3884faf31b4SDan Williams 	CXL_MBOX_OP_SET_SHUTDOWN_STATE	= 0x4204,
3894faf31b4SDan Williams 	CXL_MBOX_OP_GET_POISON		= 0x4300,
3904faf31b4SDan Williams 	CXL_MBOX_OP_INJECT_POISON	= 0x4301,
3914faf31b4SDan Williams 	CXL_MBOX_OP_CLEAR_POISON	= 0x4302,
3924faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS	= 0x4303,
3934faf31b4SDan Williams 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
3944faf31b4SDan Williams 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
39532828115SDave Jiang 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
39699746940SDave Jiang 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
397c4ef680dSDave Jiang 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
3982bb692f7SDave Jiang 	CXL_MBOX_OP_UNLOCK		= 0x4503,
399a072f7b7SDave Jiang 	CXL_MBOX_OP_FREEZE_SECURITY	= 0x4504,
4003b502e88SDave Jiang 	CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE	= 0x4505,
4014faf31b4SDan Williams 	CXL_MBOX_OP_MAX			= 0x10000
4024faf31b4SDan Williams };
4034faf31b4SDan Williams 
40449be6dd8SDan Williams #define DEFINE_CXL_CEL_UUID                                                    \
40549be6dd8SDan Williams 	UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62,     \
40649be6dd8SDan Williams 		  0x3b, 0x3f, 0x17)
40749be6dd8SDan Williams 
40849be6dd8SDan Williams #define DEFINE_CXL_VENDOR_DEBUG_UUID                                           \
40949be6dd8SDan Williams 	UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19,     \
41049be6dd8SDan Williams 		  0x40, 0x3d, 0x86)
41149be6dd8SDan Williams 
41249be6dd8SDan Williams struct cxl_mbox_get_supported_logs {
41349be6dd8SDan Williams 	__le16 entries;
41449be6dd8SDan Williams 	u8 rsvd[6];
41549be6dd8SDan Williams 	struct cxl_gsl_entry {
41649be6dd8SDan Williams 		uuid_t uuid;
41749be6dd8SDan Williams 		__le32 size;
41849be6dd8SDan Williams 	} __packed entry[];
41949be6dd8SDan Williams }  __packed;
42049be6dd8SDan Williams 
42149be6dd8SDan Williams struct cxl_cel_entry {
42249be6dd8SDan Williams 	__le16 opcode;
42349be6dd8SDan Williams 	__le16 effect;
42449be6dd8SDan Williams } __packed;
42549be6dd8SDan Williams 
42649be6dd8SDan Williams struct cxl_mbox_get_log {
42749be6dd8SDan Williams 	uuid_t uuid;
42849be6dd8SDan Williams 	__le32 offset;
42949be6dd8SDan Williams 	__le32 length;
43049be6dd8SDan Williams } __packed;
43149be6dd8SDan Williams 
43249be6dd8SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
43349be6dd8SDan Williams struct cxl_mbox_identify {
43449be6dd8SDan Williams 	char fw_revision[0x10];
43549be6dd8SDan Williams 	__le64 total_capacity;
43649be6dd8SDan Williams 	__le64 volatile_capacity;
43749be6dd8SDan Williams 	__le64 persistent_capacity;
43849be6dd8SDan Williams 	__le64 partition_align;
43949be6dd8SDan Williams 	__le16 info_event_log_size;
44049be6dd8SDan Williams 	__le16 warning_event_log_size;
44149be6dd8SDan Williams 	__le16 failure_event_log_size;
44249be6dd8SDan Williams 	__le16 fatal_event_log_size;
44349be6dd8SDan Williams 	__le32 lsa_size;
44449be6dd8SDan Williams 	u8 poison_list_max_mer[3];
44549be6dd8SDan Williams 	__le16 inject_poison_limit;
44649be6dd8SDan Williams 	u8 poison_caps;
44749be6dd8SDan Williams 	u8 qos_telemetry_caps;
44849be6dd8SDan Williams } __packed;
44949be6dd8SDan Williams 
4506ebe28f9SIra Weiny /*
4516ebe28f9SIra Weiny  * Common Event Record Format
4526ebe28f9SIra Weiny  * CXL rev 3.0 section 8.2.9.2.1; Table 8-42
4536ebe28f9SIra Weiny  */
4546ebe28f9SIra Weiny struct cxl_event_record_hdr {
4556ebe28f9SIra Weiny 	uuid_t id;
4566ebe28f9SIra Weiny 	u8 length;
4576ebe28f9SIra Weiny 	u8 flags[3];
4586ebe28f9SIra Weiny 	__le16 handle;
4596ebe28f9SIra Weiny 	__le16 related_handle;
4606ebe28f9SIra Weiny 	__le64 timestamp;
4616ebe28f9SIra Weiny 	u8 maint_op_class;
4626ebe28f9SIra Weiny 	u8 reserved[15];
4636ebe28f9SIra Weiny } __packed;
4646ebe28f9SIra Weiny 
4656ebe28f9SIra Weiny #define CXL_EVENT_RECORD_DATA_LENGTH 0x50
4666ebe28f9SIra Weiny struct cxl_event_record_raw {
4676ebe28f9SIra Weiny 	struct cxl_event_record_hdr hdr;
4686ebe28f9SIra Weiny 	u8 data[CXL_EVENT_RECORD_DATA_LENGTH];
4696ebe28f9SIra Weiny } __packed;
4706ebe28f9SIra Weiny 
4716ebe28f9SIra Weiny /*
4726ebe28f9SIra Weiny  * Get Event Records output payload
4736ebe28f9SIra Weiny  * CXL rev 3.0 section 8.2.9.2.2; Table 8-50
4746ebe28f9SIra Weiny  */
4756ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_OVERFLOW		BIT(0)
4766ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_MORE_RECORDS		BIT(1)
4776ebe28f9SIra Weiny struct cxl_get_event_payload {
4786ebe28f9SIra Weiny 	u8 flags;
4796ebe28f9SIra Weiny 	u8 reserved1;
4806ebe28f9SIra Weiny 	__le16 overflow_err_count;
4816ebe28f9SIra Weiny 	__le64 first_overflow_timestamp;
4826ebe28f9SIra Weiny 	__le64 last_overflow_timestamp;
4836ebe28f9SIra Weiny 	__le16 record_count;
4846ebe28f9SIra Weiny 	u8 reserved2[10];
4856ebe28f9SIra Weiny 	struct cxl_event_record_raw records[];
4866ebe28f9SIra Weiny } __packed;
4876ebe28f9SIra Weiny 
4886ebe28f9SIra Weiny /*
4896ebe28f9SIra Weiny  * CXL rev 3.0 section 8.2.9.2.2; Table 8-49
4906ebe28f9SIra Weiny  */
4916ebe28f9SIra Weiny enum cxl_event_log_type {
4926ebe28f9SIra Weiny 	CXL_EVENT_TYPE_INFO = 0x00,
4936ebe28f9SIra Weiny 	CXL_EVENT_TYPE_WARN,
4946ebe28f9SIra Weiny 	CXL_EVENT_TYPE_FAIL,
4956ebe28f9SIra Weiny 	CXL_EVENT_TYPE_FATAL,
4966ebe28f9SIra Weiny 	CXL_EVENT_TYPE_MAX
4976ebe28f9SIra Weiny };
4986ebe28f9SIra Weiny 
4996ebe28f9SIra Weiny /*
5006ebe28f9SIra Weiny  * Clear Event Records input payload
5016ebe28f9SIra Weiny  * CXL rev 3.0 section 8.2.9.2.3; Table 8-51
5026ebe28f9SIra Weiny  */
5036ebe28f9SIra Weiny struct cxl_mbox_clear_event_payload {
5046ebe28f9SIra Weiny 	u8 event_log;		/* enum cxl_event_log_type */
5056ebe28f9SIra Weiny 	u8 clear_flags;
5066ebe28f9SIra Weiny 	u8 nr_recs;
5076ebe28f9SIra Weiny 	u8 reserved[3];
5086ebe28f9SIra Weiny 	__le16 handles[];
5096ebe28f9SIra Weiny } __packed;
5106ebe28f9SIra Weiny #define CXL_CLEAR_EVENT_MAX_HANDLES U8_MAX
5116ebe28f9SIra Weiny 
512d54a531aSIra Weiny /*
513d54a531aSIra Weiny  * General Media Event Record
514d54a531aSIra Weiny  * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43
515d54a531aSIra Weiny  */
516d54a531aSIra Weiny #define CXL_EVENT_GEN_MED_COMP_ID_SIZE	0x10
517d54a531aSIra Weiny struct cxl_event_gen_media {
518d54a531aSIra Weiny 	struct cxl_event_record_hdr hdr;
519d54a531aSIra Weiny 	__le64 phys_addr;
520d54a531aSIra Weiny 	u8 descriptor;
521d54a531aSIra Weiny 	u8 type;
522d54a531aSIra Weiny 	u8 transaction_type;
523d54a531aSIra Weiny 	u8 validity_flags[2];
524d54a531aSIra Weiny 	u8 channel;
525d54a531aSIra Weiny 	u8 rank;
526d54a531aSIra Weiny 	u8 device[3];
527d54a531aSIra Weiny 	u8 component_id[CXL_EVENT_GEN_MED_COMP_ID_SIZE];
528d54a531aSIra Weiny 	u8 reserved[46];
529d54a531aSIra Weiny } __packed;
530d54a531aSIra Weiny 
5312d6c1e6dSIra Weiny /*
5322d6c1e6dSIra Weiny  * DRAM Event Record - DER
5332d6c1e6dSIra Weiny  * CXL rev 3.0 section 8.2.9.2.1.2; Table 3-44
5342d6c1e6dSIra Weiny  */
5352d6c1e6dSIra Weiny #define CXL_EVENT_DER_CORRECTION_MASK_SIZE	0x20
5362d6c1e6dSIra Weiny struct cxl_event_dram {
5372d6c1e6dSIra Weiny 	struct cxl_event_record_hdr hdr;
5382d6c1e6dSIra Weiny 	__le64 phys_addr;
5392d6c1e6dSIra Weiny 	u8 descriptor;
5402d6c1e6dSIra Weiny 	u8 type;
5412d6c1e6dSIra Weiny 	u8 transaction_type;
5422d6c1e6dSIra Weiny 	u8 validity_flags[2];
5432d6c1e6dSIra Weiny 	u8 channel;
5442d6c1e6dSIra Weiny 	u8 rank;
5452d6c1e6dSIra Weiny 	u8 nibble_mask[3];
5462d6c1e6dSIra Weiny 	u8 bank_group;
5472d6c1e6dSIra Weiny 	u8 bank;
5482d6c1e6dSIra Weiny 	u8 row[3];
5492d6c1e6dSIra Weiny 	u8 column[2];
5502d6c1e6dSIra Weiny 	u8 correction_mask[CXL_EVENT_DER_CORRECTION_MASK_SIZE];
5512d6c1e6dSIra Weiny 	u8 reserved[0x17];
5522d6c1e6dSIra Weiny } __packed;
5532d6c1e6dSIra Weiny 
55495b49479SIra Weiny /*
55595b49479SIra Weiny  * Get Health Info Record
55695b49479SIra Weiny  * CXL rev 3.0 section 8.2.9.8.3.1; Table 8-100
55795b49479SIra Weiny  */
55895b49479SIra Weiny struct cxl_get_health_info {
55995b49479SIra Weiny 	u8 health_status;
56095b49479SIra Weiny 	u8 media_status;
56195b49479SIra Weiny 	u8 add_status;
56295b49479SIra Weiny 	u8 life_used;
56395b49479SIra Weiny 	u8 device_temp[2];
56495b49479SIra Weiny 	u8 dirty_shutdown_cnt[4];
56595b49479SIra Weiny 	u8 cor_vol_err_cnt[4];
56695b49479SIra Weiny 	u8 cor_per_err_cnt[4];
56795b49479SIra Weiny } __packed;
56895b49479SIra Weiny 
56995b49479SIra Weiny /*
57095b49479SIra Weiny  * Memory Module Event Record
57195b49479SIra Weiny  * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45
57295b49479SIra Weiny  */
57395b49479SIra Weiny struct cxl_event_mem_module {
57495b49479SIra Weiny 	struct cxl_event_record_hdr hdr;
57595b49479SIra Weiny 	u8 event_type;
57695b49479SIra Weiny 	struct cxl_get_health_info info;
57795b49479SIra Weiny 	u8 reserved[0x3d];
57895b49479SIra Weiny } __packed;
57995b49479SIra Weiny 
580e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info {
581e7ad1bf6SDan Williams 	__le64 active_volatile_cap;
582e7ad1bf6SDan Williams 	__le64 active_persistent_cap;
583e7ad1bf6SDan Williams 	__le64 next_volatile_cap;
584e7ad1bf6SDan Williams 	__le64 next_persistent_cap;
585e7ad1bf6SDan Williams } __packed;
586e7ad1bf6SDan Williams 
58749be6dd8SDan Williams struct cxl_mbox_get_lsa {
5888a664875SAlison Schofield 	__le32 offset;
5898a664875SAlison Schofield 	__le32 length;
59049be6dd8SDan Williams } __packed;
59149be6dd8SDan Williams 
59249be6dd8SDan Williams struct cxl_mbox_set_lsa {
5938a664875SAlison Schofield 	__le32 offset;
5948a664875SAlison Schofield 	__le32 reserved;
59549be6dd8SDan Williams 	u8 data[];
59649be6dd8SDan Williams } __packed;
59749be6dd8SDan Williams 
5986179045cSAlison Schofield struct cxl_mbox_set_partition_info {
5996179045cSAlison Schofield 	__le64 volatile_capacity;
6006179045cSAlison Schofield 	u8 flags;
6016179045cSAlison Schofield } __packed;
6026179045cSAlison Schofield 
6036179045cSAlison Schofield #define  CXL_SET_PARTITION_IMMEDIATE_FLAG	BIT(0)
6046179045cSAlison Schofield 
605fa884345SJonathan Cameron /* Set Timestamp CXL 3.0 Spec 8.2.9.4.2 */
606fa884345SJonathan Cameron struct cxl_mbox_set_timestamp_in {
607fa884345SJonathan Cameron 	__le64 timestamp;
608fa884345SJonathan Cameron 
609fa884345SJonathan Cameron } __packed;
610fa884345SJonathan Cameron 
611ed83f7caSAlison Schofield /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
612ed83f7caSAlison Schofield struct cxl_mbox_poison_in {
613ed83f7caSAlison Schofield 	__le64 offset;
614ed83f7caSAlison Schofield 	__le64 length;
615ed83f7caSAlison Schofield } __packed;
616ed83f7caSAlison Schofield 
617ed83f7caSAlison Schofield struct cxl_mbox_poison_out {
618ed83f7caSAlison Schofield 	u8 flags;
619ed83f7caSAlison Schofield 	u8 rsvd1;
620ed83f7caSAlison Schofield 	__le64 overflow_ts;
621ed83f7caSAlison Schofield 	__le16 count;
622ed83f7caSAlison Schofield 	u8 rsvd2[20];
623ed83f7caSAlison Schofield 	struct cxl_poison_record {
624ed83f7caSAlison Schofield 		__le64 address;
625ed83f7caSAlison Schofield 		__le32 length;
626ed83f7caSAlison Schofield 		__le32 rsvd;
627ed83f7caSAlison Schofield 	} __packed record[];
628ed83f7caSAlison Schofield } __packed;
629ed83f7caSAlison Schofield 
630ed83f7caSAlison Schofield /*
631ed83f7caSAlison Schofield  * Get Poison List address field encodes the starting
632ed83f7caSAlison Schofield  * address of poison, and the source of the poison.
633ed83f7caSAlison Schofield  */
634ed83f7caSAlison Schofield #define CXL_POISON_START_MASK		GENMASK_ULL(63, 6)
635ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_MASK		GENMASK(2, 0)
636ed83f7caSAlison Schofield 
637ed83f7caSAlison Schofield /* Get Poison List record length is in units of 64 bytes */
638ed83f7caSAlison Schofield #define CXL_POISON_LEN_MULT	64
639ed83f7caSAlison Schofield 
640ed83f7caSAlison Schofield /* Kernel defined maximum for a list of poison errors */
641ed83f7caSAlison Schofield #define CXL_POISON_LIST_MAX	1024
642ed83f7caSAlison Schofield 
643ed83f7caSAlison Schofield /* Get Poison List: Payload out flags */
644ed83f7caSAlison Schofield #define CXL_POISON_FLAG_MORE            BIT(0)
645ed83f7caSAlison Schofield #define CXL_POISON_FLAG_OVERFLOW        BIT(1)
646ed83f7caSAlison Schofield #define CXL_POISON_FLAG_SCANNING        BIT(2)
647ed83f7caSAlison Schofield 
648ed83f7caSAlison Schofield /* Get Poison List: Poison Source */
649ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_UNKNOWN	0
650ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_EXTERNAL	1
651ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_INTERNAL	2
652ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_INJECTED	3
653ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_VENDOR	7
654ed83f7caSAlison Schofield 
655d2fbc486SAlison Schofield /* Inject & Clear Poison  CXL 3.0 Spec 8.2.9.8.4.2/3 */
656d2fbc486SAlison Schofield struct cxl_mbox_inject_poison {
657d2fbc486SAlison Schofield 	__le64 address;
658d2fbc486SAlison Schofield };
659d2fbc486SAlison Schofield 
6609690b077SAlison Schofield /* Clear Poison  CXL 3.0 Spec 8.2.9.8.4.3 */
6619690b077SAlison Schofield struct cxl_mbox_clear_poison {
6629690b077SAlison Schofield 	__le64 address;
6639690b077SAlison Schofield 	u8 write_data[CXL_POISON_LEN_MULT];
6649690b077SAlison Schofield } __packed;
6659690b077SAlison Schofield 
6664faf31b4SDan Williams /**
6674faf31b4SDan Williams  * struct cxl_mem_command - Driver representation of a memory device command
6684faf31b4SDan Williams  * @info: Command information as it exists for the UAPI
6694faf31b4SDan Williams  * @opcode: The actual bits used for the mailbox protocol
6704faf31b4SDan Williams  * @flags: Set of flags effecting driver behavior.
6714faf31b4SDan Williams  *
6724faf31b4SDan Williams  *  * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
6734faf31b4SDan Williams  *    will be enabled by the driver regardless of what hardware may have
6744faf31b4SDan Williams  *    advertised.
6754faf31b4SDan Williams  *
6764faf31b4SDan Williams  * The cxl_mem_command is the driver's internal representation of commands that
6774faf31b4SDan Williams  * are supported by the driver. Some of these commands may not be supported by
6784faf31b4SDan Williams  * the hardware. The driver will use @info to validate the fields passed in by
6794faf31b4SDan Williams  * the user then submit the @opcode to the hardware.
6804faf31b4SDan Williams  *
6814faf31b4SDan Williams  * See struct cxl_command_info.
6824faf31b4SDan Williams  */
6834faf31b4SDan Williams struct cxl_mem_command {
6844faf31b4SDan Williams 	struct cxl_command_info info;
6854faf31b4SDan Williams 	enum cxl_opcode opcode;
6864faf31b4SDan Williams 	u32 flags;
6874faf31b4SDan Williams #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
6884faf31b4SDan Williams };
6894faf31b4SDan Williams 
69032828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PASS_SET	0x01
69132828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET	0x02
69232828115SDave Jiang #define CXL_PMEM_SEC_STATE_LOCKED		0x04
69332828115SDave Jiang #define CXL_PMEM_SEC_STATE_FROZEN		0x08
69432828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PLIMIT		0x10
69532828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT	0x20
69632828115SDave Jiang 
69799746940SDave Jiang /* set passphrase input payload */
69899746940SDave Jiang struct cxl_set_pass {
69999746940SDave Jiang 	u8 type;
70099746940SDave Jiang 	u8 reserved[31];
70199746940SDave Jiang 	/* CXL field using NVDIMM define, same length */
70299746940SDave Jiang 	u8 old_pass[NVDIMM_PASSPHRASE_LEN];
70399746940SDave Jiang 	u8 new_pass[NVDIMM_PASSPHRASE_LEN];
70499746940SDave Jiang } __packed;
70599746940SDave Jiang 
706c4ef680dSDave Jiang /* disable passphrase input payload */
707c4ef680dSDave Jiang struct cxl_disable_pass {
708c4ef680dSDave Jiang 	u8 type;
709c4ef680dSDave Jiang 	u8 reserved[31];
710c4ef680dSDave Jiang 	u8 pass[NVDIMM_PASSPHRASE_LEN];
711c4ef680dSDave Jiang } __packed;
712c4ef680dSDave Jiang 
7133b502e88SDave Jiang /* passphrase secure erase payload */
7143b502e88SDave Jiang struct cxl_pass_erase {
7153b502e88SDave Jiang 	u8 type;
7163b502e88SDave Jiang 	u8 reserved[31];
7173b502e88SDave Jiang 	u8 pass[NVDIMM_PASSPHRASE_LEN];
7183b502e88SDave Jiang } __packed;
7193b502e88SDave Jiang 
72099746940SDave Jiang enum {
72199746940SDave Jiang 	CXL_PMEM_SEC_PASS_MASTER = 0,
72299746940SDave Jiang 	CXL_PMEM_SEC_PASS_USER,
72399746940SDave Jiang };
72499746940SDave Jiang 
72559f8d151SDan Williams int cxl_internal_send_cmd(struct cxl_memdev_state *mds,
7265331cdf4SDan Williams 			  struct cxl_mbox_cmd *cmd);
72759f8d151SDan Williams int cxl_dev_state_identify(struct cxl_memdev_state *mds);
7282e4ba0ecSDan Williams int cxl_await_media_ready(struct cxl_dev_state *cxlds);
72959f8d151SDan Williams int cxl_enumerate_cmds(struct cxl_memdev_state *mds);
73059f8d151SDan Williams int cxl_mem_create_range_info(struct cxl_memdev_state *mds);
73159f8d151SDan Williams struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev);
73259f8d151SDan Williams void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
73359f8d151SDan Williams 				unsigned long *cmds);
73459f8d151SDan Williams void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds,
73559f8d151SDan Williams 				  unsigned long *cmds);
73659f8d151SDan Williams void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status);
73759f8d151SDan Williams int cxl_set_timestamp(struct cxl_memdev_state *mds);
73859f8d151SDan Williams int cxl_poison_state_init(struct cxl_memdev_state *mds);
739ed83f7caSAlison Schofield int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
740ed83f7caSAlison Schofield 		       struct cxl_region *cxlr);
7417ff6ad10SAlison Schofield int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
742d2fbc486SAlison Schofield int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
7439690b077SAlison Schofield int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
744fa884345SJonathan Cameron 
7459ea4dcf4SDan Williams #ifdef CONFIG_CXL_SUSPEND
7469ea4dcf4SDan Williams void cxl_mem_active_inc(void);
7479ea4dcf4SDan Williams void cxl_mem_active_dec(void);
7489ea4dcf4SDan Williams #else
7499ea4dcf4SDan Williams static inline void cxl_mem_active_inc(void)
7509ea4dcf4SDan Williams {
7519ea4dcf4SDan Williams }
7529ea4dcf4SDan Williams static inline void cxl_mem_active_dec(void)
7539ea4dcf4SDan Williams {
7549ea4dcf4SDan Williams }
7559ea4dcf4SDan Williams #endif
756d17d0540SDan Williams 
757d17d0540SDan Williams struct cxl_hdm {
758d17d0540SDan Williams 	struct cxl_component_regs regs;
759d17d0540SDan Williams 	unsigned int decoder_count;
760d17d0540SDan Williams 	unsigned int target_count;
761d17d0540SDan Williams 	unsigned int interleave_mask;
762d17d0540SDan Williams 	struct cxl_port *port;
763d17d0540SDan Williams };
764cc2a4878SDan Williams 
765cc2a4878SDan Williams struct seq_file;
766cc2a4878SDan Williams struct dentry *cxl_debugfs_create_dir(const char *dir);
767cc2a4878SDan Williams void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
7685161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */
769