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>
8ccadf131SDavidlohr Bueso #include <linux/rcuwait.h>
95161a55cSBen Widawsky #include "cxl.h"
105161a55cSBen Widawsky
115161a55cSBen Widawsky /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
125161a55cSBen Widawsky #define CXLMDEV_STATUS_OFFSET 0x0
135161a55cSBen Widawsky #define CXLMDEV_DEV_FATAL BIT(0)
145161a55cSBen Widawsky #define CXLMDEV_FW_HALT BIT(1)
155161a55cSBen Widawsky #define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
165161a55cSBen Widawsky #define CXLMDEV_MS_NOT_READY 0
175161a55cSBen Widawsky #define CXLMDEV_MS_READY 1
185161a55cSBen Widawsky #define CXLMDEV_MS_ERROR 2
195161a55cSBen Widawsky #define CXLMDEV_MS_DISABLED 3
205161a55cSBen Widawsky #define CXLMDEV_READY(status) \
215161a55cSBen Widawsky (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \
225161a55cSBen Widawsky CXLMDEV_MS_READY)
235161a55cSBen Widawsky #define CXLMDEV_MBOX_IF_READY BIT(4)
245161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
255161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_NOT 0
265161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_COLD 1
275161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_WARM 2
285161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_HOT 3
295161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_CXL 4
305161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED(status) \
315161a55cSBen Widawsky (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \
325161a55cSBen Widawsky CXLMDEV_RESET_NEEDED_NOT)
335161a55cSBen Widawsky
345161a55cSBen Widawsky /**
355161a55cSBen Widawsky * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
365161a55cSBen Widawsky * @dev: driver core device object
375161a55cSBen Widawsky * @cdev: char dev core object for ioctl operations
385e2411aeSIra Weiny * @cxlds: The device state backing this device
398dd2bc0fSBen Widawsky * @detach_work: active memdev lost a port in its ancestry
40f17b558dSDan Williams * @cxl_nvb: coordinate removal of @cxl_nvd if present
41f17b558dSDan Williams * @cxl_nvd: optional bridge to an nvdimm if the device supports pmem
42516b300cSDan Williams * @endpoint: connection to the CXL port topology for this memory device
435161a55cSBen Widawsky * @id: id number of this memdev instance.
442345df54SDan Williams * @depth: endpoint port depth
455161a55cSBen Widawsky */
465161a55cSBen Widawsky struct cxl_memdev {
475161a55cSBen Widawsky struct device dev;
485161a55cSBen Widawsky struct cdev cdev;
495e2411aeSIra Weiny struct cxl_dev_state *cxlds;
508dd2bc0fSBen Widawsky struct work_struct detach_work;
51f17b558dSDan Williams struct cxl_nvdimm_bridge *cxl_nvb;
52f17b558dSDan Williams struct cxl_nvdimm *cxl_nvd;
53516b300cSDan Williams struct cxl_port *endpoint;
545161a55cSBen Widawsky int id;
552345df54SDan Williams int depth;
565161a55cSBen Widawsky };
575161a55cSBen Widawsky
to_cxl_memdev(struct device * dev)583d135db5SBen Widawsky static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
593d135db5SBen Widawsky {
603d135db5SBen Widawsky return container_of(dev, struct cxl_memdev, dev);
613d135db5SBen Widawsky }
623d135db5SBen Widawsky
cxled_to_port(struct cxl_endpoint_decoder * cxled)639c57cde0SDan Williams static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled)
649c57cde0SDan Williams {
659c57cde0SDan Williams return to_cxl_port(cxled->cxld.dev.parent);
669c57cde0SDan Williams }
679c57cde0SDan Williams
cxlrd_to_port(struct cxl_root_decoder * cxlrd)68384e624bSDan Williams static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd)
69384e624bSDan Williams {
70384e624bSDan Williams return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
71384e624bSDan Williams }
72384e624bSDan Williams
739c57cde0SDan Williams static inline struct cxl_memdev *
cxled_to_memdev(struct cxl_endpoint_decoder * cxled)749c57cde0SDan Williams cxled_to_memdev(struct cxl_endpoint_decoder *cxled)
759c57cde0SDan Williams {
769c57cde0SDan Williams struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent);
779c57cde0SDan Williams
787481653dSDan Williams return to_cxl_memdev(port->uport_dev);
799c57cde0SDan Williams }
809c57cde0SDan Williams
812a81ada3SGreg Kroah-Hartman bool is_cxl_memdev(const struct device *dev);
is_cxl_endpoint(struct cxl_port * port)828dd2bc0fSBen Widawsky static inline bool is_cxl_endpoint(struct cxl_port *port)
838dd2bc0fSBen Widawsky {
847481653dSDan Williams return is_cxl_memdev(port->uport_dev);
858dd2bc0fSBen Widawsky }
868dd2bc0fSBen Widawsky
8722c9bb1eSDan Williams struct cxl_memdev *devm_cxl_add_memdev(struct device *host,
8822c9bb1eSDan Williams struct cxl_dev_state *cxlds);
89d4e21e7bSDan Williams int devm_cxl_sanitize_setup_notifier(struct device *host,
90d4e21e7bSDan Williams struct cxl_memdev *cxlmd);
91aeaefabcSDan Williams struct cxl_memdev_state;
9222c9bb1eSDan Williams int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds);
933d8f7ccaSDan Williams int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
943d8f7ccaSDan Williams resource_size_t base, resource_size_t len,
953d8f7ccaSDan Williams resource_size_t skipped);
963d135db5SBen Widawsky
cxl_ep_load(struct cxl_port * port,struct cxl_memdev * cxlmd)977592d935SDan Williams static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
987592d935SDan Williams struct cxl_memdev *cxlmd)
997592d935SDan Williams {
1007592d935SDan Williams if (!port)
1017592d935SDan Williams return NULL;
1027592d935SDan Williams
1037592d935SDan Williams return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev);
1047592d935SDan Williams }
1057592d935SDan Williams
1065161a55cSBen Widawsky /**
107b64955a9SDan Williams * struct cxl_mbox_cmd - A command to be submitted to hardware.
108b64955a9SDan Williams * @opcode: (input) The command set and command submitted to hardware.
109b64955a9SDan Williams * @payload_in: (input) Pointer to the input payload.
110b64955a9SDan Williams * @payload_out: (output) Pointer to the output payload. Must be allocated by
111b64955a9SDan Williams * the caller.
112b64955a9SDan Williams * @size_in: (input) Number of bytes to load from @payload_in.
113b64955a9SDan Williams * @size_out: (input) Max number of bytes loaded into @payload_out.
114b64955a9SDan Williams * (output) Number of bytes generated by the device. For fixed size
115b64955a9SDan Williams * outputs commands this is always expected to be deterministic. For
116b64955a9SDan Williams * variable sized output commands, it tells the exact number of bytes
117b64955a9SDan Williams * written.
1182aeaf663SDan Williams * @min_out: (input) internal command output payload size validation
119ccadf131SDavidlohr Bueso * @poll_count: (input) Number of timeouts to attempt.
120ccadf131SDavidlohr Bueso * @poll_interval_ms: (input) Time between mailbox background command polling
121ccadf131SDavidlohr Bueso * interval timeouts.
122b64955a9SDan Williams * @return_code: (output) Error code returned from hardware.
123b64955a9SDan Williams *
124b64955a9SDan Williams * This is the primary mechanism used to send commands to the hardware.
125b64955a9SDan Williams * All the fields except @payload_* correspond exactly to the fields described in
126b64955a9SDan Williams * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and
127b64955a9SDan Williams * @payload_out are written to, and read from the Command Payload Registers
128b64955a9SDan Williams * defined in CXL 2.0 8.2.8.4.8.
129b64955a9SDan Williams */
130b64955a9SDan Williams struct cxl_mbox_cmd {
131b64955a9SDan Williams u16 opcode;
132b64955a9SDan Williams void *payload_in;
133b64955a9SDan Williams void *payload_out;
134b64955a9SDan Williams size_t size_in;
135b64955a9SDan Williams size_t size_out;
1362aeaf663SDan Williams size_t min_out;
137ccadf131SDavidlohr Bueso int poll_count;
138ccadf131SDavidlohr Bueso int poll_interval_ms;
139b64955a9SDan Williams u16 return_code;
140b64955a9SDan Williams };
141b64955a9SDan Williams
142b64955a9SDan Williams /*
143bfe58458SDavidlohr Bueso * Per CXL 3.0 Section 8.2.8.4.5.1
14492fcc1abSDavidlohr Bueso */
14592fcc1abSDavidlohr Bueso #define CMD_CMD_RC_TABLE \
14692fcc1abSDavidlohr Bueso C(SUCCESS, 0, NULL), \
14792fcc1abSDavidlohr Bueso C(BACKGROUND, -ENXIO, "background cmd started successfully"), \
14892fcc1abSDavidlohr Bueso C(INPUT, -ENXIO, "cmd input was invalid"), \
14992fcc1abSDavidlohr Bueso C(UNSUPPORTED, -ENXIO, "cmd is not supported"), \
15092fcc1abSDavidlohr Bueso C(INTERNAL, -ENXIO, "internal device error"), \
15192fcc1abSDavidlohr Bueso C(RETRY, -ENXIO, "temporary error, retry once"), \
15292fcc1abSDavidlohr Bueso C(BUSY, -ENXIO, "ongoing background operation"), \
15392fcc1abSDavidlohr Bueso C(MEDIADISABLED, -ENXIO, "media access is disabled"), \
15492fcc1abSDavidlohr Bueso C(FWINPROGRESS, -ENXIO, "one FW package can be transferred at a time"), \
15592fcc1abSDavidlohr Bueso C(FWOOO, -ENXIO, "FW package content was transferred out of order"), \
15692fcc1abSDavidlohr Bueso C(FWAUTH, -ENXIO, "FW package authentication failed"), \
15792fcc1abSDavidlohr Bueso C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"), \
15892fcc1abSDavidlohr Bueso C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"), \
15992fcc1abSDavidlohr Bueso C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"), \
16092fcc1abSDavidlohr Bueso C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"), \
1617ff6ad10SAlison Schofield C(PADDR, -EFAULT, "physical address specified is invalid"), \
16292fcc1abSDavidlohr Bueso C(POISONLMT, -ENXIO, "poison injection limit has been reached"), \
16392fcc1abSDavidlohr Bueso C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"), \
16492fcc1abSDavidlohr Bueso C(ABORT, -ENXIO, "background cmd was aborted by device"), \
16592fcc1abSDavidlohr Bueso C(SECURITY, -ENXIO, "not valid in the current security state"), \
16692fcc1abSDavidlohr Bueso C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"), \
16792fcc1abSDavidlohr Bueso C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\
168bfe58458SDavidlohr Bueso C(PAYLOADLEN, -ENXIO, "invalid payload length"), \
169bfe58458SDavidlohr Bueso C(LOG, -ENXIO, "invalid or unsupported log page"), \
170bfe58458SDavidlohr Bueso C(INTERRUPTED, -ENXIO, "asynchronous event occured"), \
171bfe58458SDavidlohr Bueso C(FEATUREVERSION, -ENXIO, "unsupported feature version"), \
172bfe58458SDavidlohr Bueso C(FEATURESELVALUE, -ENXIO, "unsupported feature selection value"), \
173bfe58458SDavidlohr Bueso C(FEATURETRANSFERIP, -ENXIO, "feature transfer in progress"), \
174bfe58458SDavidlohr Bueso C(FEATURETRANSFEROOO, -ENXIO, "feature transfer out of order"), \
175bfe58458SDavidlohr Bueso C(RESOURCEEXHAUSTED, -ENXIO, "resources are exhausted"), \
176bfe58458SDavidlohr Bueso C(EXTLIST, -ENXIO, "invalid Extent List"), \
17792fcc1abSDavidlohr Bueso
17892fcc1abSDavidlohr Bueso #undef C
17992fcc1abSDavidlohr Bueso #define C(a, b, c) CXL_MBOX_CMD_RC_##a
18092fcc1abSDavidlohr Bueso enum { CMD_CMD_RC_TABLE };
18192fcc1abSDavidlohr Bueso #undef C
18292fcc1abSDavidlohr Bueso #define C(a, b, c) { b, c }
18392fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc {
18492fcc1abSDavidlohr Bueso int err;
18592fcc1abSDavidlohr Bueso const char *desc;
18692fcc1abSDavidlohr Bueso };
18792fcc1abSDavidlohr Bueso
18892fcc1abSDavidlohr Bueso static const
18992fcc1abSDavidlohr Bueso struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
19092fcc1abSDavidlohr Bueso #undef C
19192fcc1abSDavidlohr Bueso
cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd * mbox_cmd)19292fcc1abSDavidlohr Bueso static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
19392fcc1abSDavidlohr Bueso {
19492fcc1abSDavidlohr Bueso return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
19592fcc1abSDavidlohr Bueso }
19692fcc1abSDavidlohr Bueso
cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd * mbox_cmd)19792fcc1abSDavidlohr Bueso static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
19892fcc1abSDavidlohr Bueso {
19992fcc1abSDavidlohr Bueso return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
20092fcc1abSDavidlohr Bueso }
20192fcc1abSDavidlohr Bueso
20292fcc1abSDavidlohr Bueso /*
203b64955a9SDan Williams * CXL 2.0 - Memory capacity multiplier
204b64955a9SDan Williams * See Section 8.2.9.5
205b64955a9SDan Williams *
206b64955a9SDan Williams * Volatile, Persistent, and Partition capacities are specified to be in
207b64955a9SDan Williams * multiples of 256MB - define a multiplier to convert to/from bytes.
208b64955a9SDan Williams */
209b64955a9SDan Williams #define CXL_CAPACITY_MULTIPLIER SZ_256M
210b64955a9SDan Williams
211c192e543SDan Williams /*
212a49aa814SDavidlohr Bueso * Event Interrupt Policy
213a49aa814SDavidlohr Bueso *
214a49aa814SDavidlohr Bueso * CXL rev 3.0 section 8.2.9.2.4; Table 8-52
215560f7855SBen Widawsky */
216a49aa814SDavidlohr Bueso enum cxl_event_int_mode {
217a49aa814SDavidlohr Bueso CXL_INT_NONE = 0x00,
218a49aa814SDavidlohr Bueso CXL_INT_MSI_MSIX = 0x01,
219a49aa814SDavidlohr Bueso CXL_INT_FW = 0x02
220a49aa814SDavidlohr Bueso };
221a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy {
222a49aa814SDavidlohr Bueso u8 info_settings;
223a49aa814SDavidlohr Bueso u8 warn_settings;
224a49aa814SDavidlohr Bueso u8 failure_settings;
225a49aa814SDavidlohr Bueso u8 fatal_settings;
226a49aa814SDavidlohr Bueso } __packed;
227a49aa814SDavidlohr Bueso
228a49aa814SDavidlohr Bueso /**
2296ebe28f9SIra Weiny * struct cxl_event_state - Event log driver state
2306ebe28f9SIra Weiny *
231c192e543SDan Williams * @buf: Buffer to receive event data
232c192e543SDan Williams * @log_lock: Serialize event_buf and log use
2336ebe28f9SIra Weiny */
2346ebe28f9SIra Weiny struct cxl_event_state {
2356ebe28f9SIra Weiny struct cxl_get_event_payload *buf;
2366ebe28f9SIra Weiny struct mutex log_lock;
237560f7855SBen Widawsky };
238560f7855SBen Widawsky
239d0abf578SAlison Schofield /* Device enabled poison commands */
240d0abf578SAlison Schofield enum poison_cmd_enabled_bits {
241d0abf578SAlison Schofield CXL_POISON_ENABLED_LIST,
242d0abf578SAlison Schofield CXL_POISON_ENABLED_INJECT,
243d0abf578SAlison Schofield CXL_POISON_ENABLED_CLEAR,
244d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_CAPS,
245d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_MEDIA,
246d0abf578SAlison Schofield CXL_POISON_ENABLED_SCAN_RESULTS,
247d0abf578SAlison Schofield CXL_POISON_ENABLED_MAX
248d0abf578SAlison Schofield };
249d0abf578SAlison Schofield
250ad64f595SDavidlohr Bueso /* Device enabled security commands */
251ad64f595SDavidlohr Bueso enum security_cmd_enabled_bits {
252ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_SANITIZE,
253ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_SECURE_ERASE,
254ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_GET_SECURITY_STATE,
255ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_SET_PASSPHRASE,
256ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
257ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_UNLOCK,
258ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_FREEZE_SECURITY,
259ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
260ad64f595SDavidlohr Bueso CXL_SEC_ENABLED_MAX
261ad64f595SDavidlohr Bueso };
262ad64f595SDavidlohr Bueso
263d0abf578SAlison Schofield /**
264d0abf578SAlison Schofield * struct cxl_poison_state - Driver poison state info
265d0abf578SAlison Schofield *
266d0abf578SAlison Schofield * @max_errors: Maximum media error records held in device cache
267d0abf578SAlison Schofield * @enabled_cmds: All poison commands enabled in the CEL
268d0abf578SAlison Schofield * @list_out: The poison list payload returned by device
269d0abf578SAlison Schofield * @lock: Protect reads of the poison list
270d0abf578SAlison Schofield *
271d0abf578SAlison Schofield * Reads of the poison list are synchronized to ensure that a reader
272d0abf578SAlison Schofield * does not get an incomplete list because their request overlapped
273d0abf578SAlison Schofield * (was interrupted or preceded by) another read request of the same
274d0abf578SAlison Schofield * DPA range. CXL Spec 3.0 Section 8.2.9.8.4.1
275d0abf578SAlison Schofield */
276d0abf578SAlison Schofield struct cxl_poison_state {
277d0abf578SAlison Schofield u32 max_errors;
278d0abf578SAlison Schofield DECLARE_BITMAP(enabled_cmds, CXL_POISON_ENABLED_MAX);
279d0abf578SAlison Schofield struct cxl_mbox_poison_out *list_out;
280d0abf578SAlison Schofield struct mutex lock; /* Protect reads of poison list */
281d0abf578SAlison Schofield };
282d0abf578SAlison Schofield
2839521875bSVishal Verma /*
2849521875bSVishal Verma * Get FW Info
2859521875bSVishal Verma * CXL rev 3.0 section 8.2.9.3.1; Table 8-56
2869521875bSVishal Verma */
2879521875bSVishal Verma struct cxl_mbox_get_fw_info {
2889521875bSVishal Verma u8 num_slots;
2899521875bSVishal Verma u8 slot_info;
2909521875bSVishal Verma u8 activation_cap;
2919521875bSVishal Verma u8 reserved[13];
2929521875bSVishal Verma char slot_1_revision[16];
2939521875bSVishal Verma char slot_2_revision[16];
2949521875bSVishal Verma char slot_3_revision[16];
2959521875bSVishal Verma char slot_4_revision[16];
2969521875bSVishal Verma } __packed;
2979521875bSVishal Verma
2989521875bSVishal Verma #define CXL_FW_INFO_SLOT_INFO_CUR_MASK GENMASK(2, 0)
2999521875bSVishal Verma #define CXL_FW_INFO_SLOT_INFO_NEXT_MASK GENMASK(5, 3)
3009521875bSVishal Verma #define CXL_FW_INFO_SLOT_INFO_NEXT_SHIFT 3
3019521875bSVishal Verma #define CXL_FW_INFO_ACTIVATION_CAP_HAS_LIVE_ACTIVATE BIT(0)
3029521875bSVishal Verma
3039521875bSVishal Verma /*
3049521875bSVishal Verma * Transfer FW Input Payload
3059521875bSVishal Verma * CXL rev 3.0 section 8.2.9.3.2; Table 8-57
3069521875bSVishal Verma */
3079521875bSVishal Verma struct cxl_mbox_transfer_fw {
3089521875bSVishal Verma u8 action;
3099521875bSVishal Verma u8 slot;
3109521875bSVishal Verma u8 reserved[2];
3119521875bSVishal Verma __le32 offset;
3129521875bSVishal Verma u8 reserved2[0x78];
3139521875bSVishal Verma u8 data[];
3149521875bSVishal Verma } __packed;
3159521875bSVishal Verma
3169521875bSVishal Verma #define CXL_FW_TRANSFER_ACTION_FULL 0x0
3179521875bSVishal Verma #define CXL_FW_TRANSFER_ACTION_INITIATE 0x1
3189521875bSVishal Verma #define CXL_FW_TRANSFER_ACTION_CONTINUE 0x2
3199521875bSVishal Verma #define CXL_FW_TRANSFER_ACTION_END 0x3
3209521875bSVishal Verma #define CXL_FW_TRANSFER_ACTION_ABORT 0x4
3219521875bSVishal Verma
3229521875bSVishal Verma /*
3239521875bSVishal Verma * CXL rev 3.0 section 8.2.9.3.2 mandates 128-byte alignment for FW packages
3249521875bSVishal Verma * and for each part transferred in a Transfer FW command.
3259521875bSVishal Verma */
3269521875bSVishal Verma #define CXL_FW_TRANSFER_ALIGNMENT 128
3279521875bSVishal Verma
3289521875bSVishal Verma /*
3299521875bSVishal Verma * Activate FW Input Payload
3309521875bSVishal Verma * CXL rev 3.0 section 8.2.9.3.3; Table 8-58
3319521875bSVishal Verma */
3329521875bSVishal Verma struct cxl_mbox_activate_fw {
3339521875bSVishal Verma u8 action;
3349521875bSVishal Verma u8 slot;
3359521875bSVishal Verma } __packed;
3369521875bSVishal Verma
3379521875bSVishal Verma #define CXL_FW_ACTIVATE_ONLINE 0x0
3389521875bSVishal Verma #define CXL_FW_ACTIVATE_OFFLINE 0x1
3399521875bSVishal Verma
3409521875bSVishal Verma /* FW state bits */
3419521875bSVishal Verma #define CXL_FW_STATE_BITS 32
34295c6bff7SDan Carpenter #define CXL_FW_CANCEL 0
3439521875bSVishal Verma
3449521875bSVishal Verma /**
3459521875bSVishal Verma * struct cxl_fw_state - Firmware upload / activation state
3469521875bSVishal Verma *
3479521875bSVishal Verma * @state: fw_uploader state bitmask
3489521875bSVishal Verma * @oneshot: whether the fw upload fits in a single transfer
3499521875bSVishal Verma * @num_slots: Number of FW slots available
3509521875bSVishal Verma * @cur_slot: Slot number currently active
3519521875bSVishal Verma * @next_slot: Slot number for the new firmware
3529521875bSVishal Verma */
3539521875bSVishal Verma struct cxl_fw_state {
3549521875bSVishal Verma DECLARE_BITMAP(state, CXL_FW_STATE_BITS);
3559521875bSVishal Verma bool oneshot;
3569521875bSVishal Verma int num_slots;
3579521875bSVishal Verma int cur_slot;
3589521875bSVishal Verma int next_slot;
3599521875bSVishal Verma };
3609521875bSVishal Verma
361560f7855SBen Widawsky /**
3629968c9ddSDavidlohr Bueso * struct cxl_security_state - Device security state
3639968c9ddSDavidlohr Bueso *
3649968c9ddSDavidlohr Bueso * @state: state of last security operation
365ad64f595SDavidlohr Bueso * @enabled_cmds: All security commands enabled in the CEL
3660c36b6adSDavidlohr Bueso * @poll_tmo_secs: polling timeout
367d1d13a09SDan Williams * @sanitize_active: sanitize completion pending
3680c36b6adSDavidlohr Bueso * @poll_dwork: polling work item
36948dcdbb1SDavidlohr Bueso * @sanitize_node: sanitation sysfs file to notify
3709968c9ddSDavidlohr Bueso */
3719968c9ddSDavidlohr Bueso struct cxl_security_state {
3729968c9ddSDavidlohr Bueso unsigned long state;
373ad64f595SDavidlohr Bueso DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX);
3740c36b6adSDavidlohr Bueso int poll_tmo_secs;
375d1d13a09SDan Williams bool sanitize_active;
3760c36b6adSDavidlohr Bueso struct delayed_work poll_dwork;
37748dcdbb1SDavidlohr Bueso struct kernfs_node *sanitize_node;
3789968c9ddSDavidlohr Bueso };
3799968c9ddSDavidlohr Bueso
380aeaefabcSDan Williams /*
381f6b8ab32SDan Williams * enum cxl_devtype - delineate type-2 from a generic type-3 device
382f6b8ab32SDan Williams * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
383f6b8ab32SDan Williams * HDM-DB, no requirement that this device implements a
384f6b8ab32SDan Williams * mailbox, or other memory-device-standard manageability
385f6b8ab32SDan Williams * flows.
386f6b8ab32SDan Williams * @CXL_DEVTYPE_CLASSMEM - Common class definition of a CXL Type-3 device with
387f6b8ab32SDan Williams * HDM-H and class-mandatory memory device registers
388f6b8ab32SDan Williams */
389f6b8ab32SDan Williams enum cxl_devtype {
390f6b8ab32SDan Williams CXL_DEVTYPE_DEVMEM,
391f6b8ab32SDan Williams CXL_DEVTYPE_CLASSMEM,
392f6b8ab32SDan Williams };
393f6b8ab32SDan Williams
3949968c9ddSDavidlohr Bueso /**
3955e2411aeSIra Weiny * struct cxl_dev_state - The driver device state
3965e2411aeSIra Weiny *
3975e2411aeSIra Weiny * cxl_dev_state represents the CXL driver/device state. It provides an
3985e2411aeSIra Weiny * interface to mailbox commands as well as some cached data about the device.
3995e2411aeSIra Weiny * Currently only memory devices are represented.
4005e2411aeSIra Weiny *
4015e2411aeSIra Weiny * @dev: The device associated with this CXL state
4022905cb52SDan Williams * @cxlmd: The device representing the CXL.mem capabilities of @dev
4035161a55cSBen Widawsky * @regs: Parsed register blocks
40406e279e5SBen Widawsky * @cxl_dvsec: Offset to the PCIe device DVSEC
4050a19bfc8SDan Williams * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
406e764f122SDave Jiang * @media_ready: Indicate whether the device media is usable
40759f8d151SDan Williams * @dpa_res: Overall DPA resource tree for the device
40859f8d151SDan Williams * @pmem_res: Active Persistent memory capacity configuration
40959f8d151SDan Williams * @ram_res: Active Volatile memory capacity configuration
41059f8d151SDan Williams * @component_reg_phys: register base of component registers
41159f8d151SDan Williams * @serial: PCIe Device Serial Number
412f6b8ab32SDan Williams * @type: Generic Memory Class device or Vendor Specific Memory device
41359f8d151SDan Williams */
41459f8d151SDan Williams struct cxl_dev_state {
41559f8d151SDan Williams struct device *dev;
41659f8d151SDan Williams struct cxl_memdev *cxlmd;
41759f8d151SDan Williams struct cxl_regs regs;
41859f8d151SDan Williams int cxl_dvsec;
41959f8d151SDan Williams bool rcd;
42059f8d151SDan Williams bool media_ready;
42159f8d151SDan Williams struct resource dpa_res;
42259f8d151SDan Williams struct resource pmem_res;
42359f8d151SDan Williams struct resource ram_res;
42459f8d151SDan Williams resource_size_t component_reg_phys;
42559f8d151SDan Williams u64 serial;
426f6b8ab32SDan Williams enum cxl_devtype type;
42759f8d151SDan Williams };
42859f8d151SDan Williams
42959f8d151SDan Williams /**
43059f8d151SDan Williams * struct cxl_memdev_state - Generic Type-3 Memory Device Class driver data
43159f8d151SDan Williams *
43259f8d151SDan Williams * CXL 8.1.12.1 PCI Header - Class Code Register Memory Device defines
43359f8d151SDan Williams * common memory device functionality like the presence of a mailbox and
43459f8d151SDan Williams * the functionality related to that like Identify Memory Device and Get
43559f8d151SDan Williams * Partition Info
43659f8d151SDan Williams * @cxlds: Core driver state common across Type-2 and Type-3 devices
4375161a55cSBen Widawsky * @payload_size: Size of space for payload
4385161a55cSBen Widawsky * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
4395161a55cSBen Widawsky * @lsa_size: Size of Label Storage Area
4405161a55cSBen Widawsky * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
4415161a55cSBen Widawsky * @mbox_mutex: Mutex to synchronize mailbox access.
4425161a55cSBen Widawsky * @firmware_version: Firmware version for the memory device.
4435161a55cSBen Widawsky * @enabled_cmds: Hardware commands found enabled in CEL.
44412f3856aSDan Williams * @exclusive_cmds: Commands that are kernel-internal only
44513e7749dSDan Williams * @total_bytes: sum of all possible capacities
44613e7749dSDan Williams * @volatile_only_bytes: hard volatile capacity
44713e7749dSDan Williams * @persistent_only_bytes: hard persistent capacity
44813e7749dSDan Williams * @partition_align_bytes: alignment size for partition-able capacity
44913e7749dSDan Williams * @active_volatile_bytes: sum of hard + soft volatile
45013e7749dSDan Williams * @active_persistent_bytes: sum of hard + soft persistent
45113e7749dSDan Williams * @next_volatile_bytes: volatile capacity change pending device reset
45213e7749dSDan Williams * @next_persistent_bytes: persistent capacity change pending device reset
4531bb31131SAlison Schofield * @event: event log driver state
454d0abf578SAlison Schofield * @poison: poison driver state info
4553de8cd22SDavidlohr Bueso * @security: security driver state info
4569521875bSVishal Verma * @fw: firmware upload / activation state
457b64955a9SDan Williams * @mbox_send: @dev specific transport for transmitting mailbox commands
45813e7749dSDan Williams *
45959f8d151SDan Williams * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
46013e7749dSDan Williams * details on capacity parameters.
4615161a55cSBen Widawsky */
46259f8d151SDan Williams struct cxl_memdev_state {
46359f8d151SDan Williams struct cxl_dev_state cxlds;
4645161a55cSBen Widawsky size_t payload_size;
4655161a55cSBen Widawsky size_t lsa_size;
4665161a55cSBen Widawsky struct mutex mbox_mutex; /* Protects device mailbox and firmware */
4675161a55cSBen Widawsky char firmware_version[0x10];
468ff56ab9eSDan Williams DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
46912f3856aSDan Williams DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
4700b9159d0SIra Weiny u64 total_bytes;
4710b9159d0SIra Weiny u64 volatile_only_bytes;
4720b9159d0SIra Weiny u64 persistent_only_bytes;
4730b9159d0SIra Weiny u64 partition_align_bytes;
474f847502aSIra Weiny u64 active_volatile_bytes;
475f847502aSIra Weiny u64 active_persistent_bytes;
476f847502aSIra Weiny u64 next_volatile_bytes;
477f847502aSIra Weiny u64 next_persistent_bytes;
4786ebe28f9SIra Weiny struct cxl_event_state event;
479d0abf578SAlison Schofield struct cxl_poison_state poison;
4809968c9ddSDavidlohr Bueso struct cxl_security_state security;
4819521875bSVishal Verma struct cxl_fw_state fw;
4826ebe28f9SIra Weiny
483ccadf131SDavidlohr Bueso struct rcuwait mbox_wait;
48459f8d151SDan Williams int (*mbox_send)(struct cxl_memdev_state *mds,
48559f8d151SDan Williams struct cxl_mbox_cmd *cmd);
4865161a55cSBen Widawsky };
4874faf31b4SDan Williams
48859f8d151SDan Williams static inline struct cxl_memdev_state *
to_cxl_memdev_state(struct cxl_dev_state * cxlds)48959f8d151SDan Williams to_cxl_memdev_state(struct cxl_dev_state *cxlds)
49059f8d151SDan Williams {
491f6b8ab32SDan Williams if (cxlds->type != CXL_DEVTYPE_CLASSMEM)
492f6b8ab32SDan Williams return NULL;
49359f8d151SDan Williams return container_of(cxlds, struct cxl_memdev_state, cxlds);
49459f8d151SDan Williams }
49559f8d151SDan Williams
4964faf31b4SDan Williams enum cxl_opcode {
4974faf31b4SDan Williams CXL_MBOX_OP_INVALID = 0x0000,
4984faf31b4SDan Williams CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
4996ebe28f9SIra Weiny CXL_MBOX_OP_GET_EVENT_RECORD = 0x0100,
5006ebe28f9SIra Weiny CXL_MBOX_OP_CLEAR_EVENT_RECORD = 0x0101,
501a49aa814SDavidlohr Bueso CXL_MBOX_OP_GET_EVT_INT_POLICY = 0x0102,
502a49aa814SDavidlohr Bueso CXL_MBOX_OP_SET_EVT_INT_POLICY = 0x0103,
5034faf31b4SDan Williams CXL_MBOX_OP_GET_FW_INFO = 0x0200,
5049521875bSVishal Verma CXL_MBOX_OP_TRANSFER_FW = 0x0201,
5054faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW = 0x0202,
506fa884345SJonathan Cameron CXL_MBOX_OP_SET_TIMESTAMP = 0x0301,
5074faf31b4SDan Williams CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400,
5084faf31b4SDan Williams CXL_MBOX_OP_GET_LOG = 0x0401,
5094faf31b4SDan Williams CXL_MBOX_OP_IDENTIFY = 0x4000,
5104faf31b4SDan Williams CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100,
5114faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101,
5124faf31b4SDan Williams CXL_MBOX_OP_GET_LSA = 0x4102,
5134faf31b4SDan Williams CXL_MBOX_OP_SET_LSA = 0x4103,
5144faf31b4SDan Williams CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200,
5154faf31b4SDan Williams CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201,
5164faf31b4SDan Williams CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202,
5174faf31b4SDan Williams CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203,
5184faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204,
5194faf31b4SDan Williams CXL_MBOX_OP_GET_POISON = 0x4300,
5204faf31b4SDan Williams CXL_MBOX_OP_INJECT_POISON = 0x4301,
5214faf31b4SDan Williams CXL_MBOX_OP_CLEAR_POISON = 0x4302,
5224faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
5234faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA = 0x4304,
5244faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305,
5250c36b6adSDavidlohr Bueso CXL_MBOX_OP_SANITIZE = 0x4400,
526180ffd33SDavidlohr Bueso CXL_MBOX_OP_SECURE_ERASE = 0x4401,
52732828115SDave Jiang CXL_MBOX_OP_GET_SECURITY_STATE = 0x4500,
52899746940SDave Jiang CXL_MBOX_OP_SET_PASSPHRASE = 0x4501,
529c4ef680dSDave Jiang CXL_MBOX_OP_DISABLE_PASSPHRASE = 0x4502,
5302bb692f7SDave Jiang CXL_MBOX_OP_UNLOCK = 0x4503,
531a072f7b7SDave Jiang CXL_MBOX_OP_FREEZE_SECURITY = 0x4504,
5323b502e88SDave Jiang CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE = 0x4505,
5334faf31b4SDan Williams CXL_MBOX_OP_MAX = 0x10000
5344faf31b4SDan Williams };
5354faf31b4SDan Williams
53649be6dd8SDan Williams #define DEFINE_CXL_CEL_UUID \
53749be6dd8SDan Williams UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \
53849be6dd8SDan Williams 0x3b, 0x3f, 0x17)
53949be6dd8SDan Williams
54049be6dd8SDan Williams #define DEFINE_CXL_VENDOR_DEBUG_UUID \
541*8295194aSpeng guo UUID_INIT(0x5e1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \
54249be6dd8SDan Williams 0x40, 0x3d, 0x86)
54349be6dd8SDan Williams
54449be6dd8SDan Williams struct cxl_mbox_get_supported_logs {
54549be6dd8SDan Williams __le16 entries;
54649be6dd8SDan Williams u8 rsvd[6];
54749be6dd8SDan Williams struct cxl_gsl_entry {
54849be6dd8SDan Williams uuid_t uuid;
54949be6dd8SDan Williams __le32 size;
55049be6dd8SDan Williams } __packed entry[];
55149be6dd8SDan Williams } __packed;
55249be6dd8SDan Williams
55349be6dd8SDan Williams struct cxl_cel_entry {
55449be6dd8SDan Williams __le16 opcode;
55549be6dd8SDan Williams __le16 effect;
55649be6dd8SDan Williams } __packed;
55749be6dd8SDan Williams
55849be6dd8SDan Williams struct cxl_mbox_get_log {
55949be6dd8SDan Williams uuid_t uuid;
56049be6dd8SDan Williams __le32 offset;
56149be6dd8SDan Williams __le32 length;
56249be6dd8SDan Williams } __packed;
56349be6dd8SDan Williams
56449be6dd8SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
56549be6dd8SDan Williams struct cxl_mbox_identify {
56649be6dd8SDan Williams char fw_revision[0x10];
56749be6dd8SDan Williams __le64 total_capacity;
56849be6dd8SDan Williams __le64 volatile_capacity;
56949be6dd8SDan Williams __le64 persistent_capacity;
57049be6dd8SDan Williams __le64 partition_align;
57149be6dd8SDan Williams __le16 info_event_log_size;
57249be6dd8SDan Williams __le16 warning_event_log_size;
57349be6dd8SDan Williams __le16 failure_event_log_size;
57449be6dd8SDan Williams __le16 fatal_event_log_size;
57549be6dd8SDan Williams __le32 lsa_size;
57649be6dd8SDan Williams u8 poison_list_max_mer[3];
57749be6dd8SDan Williams __le16 inject_poison_limit;
57849be6dd8SDan Williams u8 poison_caps;
57949be6dd8SDan Williams u8 qos_telemetry_caps;
58049be6dd8SDan Williams } __packed;
58149be6dd8SDan Williams
5826ebe28f9SIra Weiny /*
5836ebe28f9SIra Weiny * Common Event Record Format
5846ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.1; Table 8-42
5856ebe28f9SIra Weiny */
5866ebe28f9SIra Weiny struct cxl_event_record_hdr {
5876ebe28f9SIra Weiny uuid_t id;
5886ebe28f9SIra Weiny u8 length;
5896ebe28f9SIra Weiny u8 flags[3];
5906ebe28f9SIra Weiny __le16 handle;
5916ebe28f9SIra Weiny __le16 related_handle;
5926ebe28f9SIra Weiny __le64 timestamp;
5936ebe28f9SIra Weiny u8 maint_op_class;
5946ebe28f9SIra Weiny u8 reserved[15];
5956ebe28f9SIra Weiny } __packed;
5966ebe28f9SIra Weiny
5976ebe28f9SIra Weiny #define CXL_EVENT_RECORD_DATA_LENGTH 0x50
5986ebe28f9SIra Weiny struct cxl_event_record_raw {
5996ebe28f9SIra Weiny struct cxl_event_record_hdr hdr;
6006ebe28f9SIra Weiny u8 data[CXL_EVENT_RECORD_DATA_LENGTH];
6016ebe28f9SIra Weiny } __packed;
6026ebe28f9SIra Weiny
6036ebe28f9SIra Weiny /*
6046ebe28f9SIra Weiny * Get Event Records output payload
6056ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.2; Table 8-50
6066ebe28f9SIra Weiny */
6076ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_OVERFLOW BIT(0)
6086ebe28f9SIra Weiny #define CXL_GET_EVENT_FLAG_MORE_RECORDS BIT(1)
6096ebe28f9SIra Weiny struct cxl_get_event_payload {
6106ebe28f9SIra Weiny u8 flags;
6116ebe28f9SIra Weiny u8 reserved1;
6126ebe28f9SIra Weiny __le16 overflow_err_count;
6136ebe28f9SIra Weiny __le64 first_overflow_timestamp;
6146ebe28f9SIra Weiny __le64 last_overflow_timestamp;
6156ebe28f9SIra Weiny __le16 record_count;
6166ebe28f9SIra Weiny u8 reserved2[10];
6176ebe28f9SIra Weiny struct cxl_event_record_raw records[];
6186ebe28f9SIra Weiny } __packed;
6196ebe28f9SIra Weiny
6206ebe28f9SIra Weiny /*
6216ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.2; Table 8-49
6226ebe28f9SIra Weiny */
6236ebe28f9SIra Weiny enum cxl_event_log_type {
6246ebe28f9SIra Weiny CXL_EVENT_TYPE_INFO = 0x00,
6256ebe28f9SIra Weiny CXL_EVENT_TYPE_WARN,
6266ebe28f9SIra Weiny CXL_EVENT_TYPE_FAIL,
6276ebe28f9SIra Weiny CXL_EVENT_TYPE_FATAL,
6286ebe28f9SIra Weiny CXL_EVENT_TYPE_MAX
6296ebe28f9SIra Weiny };
6306ebe28f9SIra Weiny
6316ebe28f9SIra Weiny /*
6326ebe28f9SIra Weiny * Clear Event Records input payload
6336ebe28f9SIra Weiny * CXL rev 3.0 section 8.2.9.2.3; Table 8-51
6346ebe28f9SIra Weiny */
6356ebe28f9SIra Weiny struct cxl_mbox_clear_event_payload {
6366ebe28f9SIra Weiny u8 event_log; /* enum cxl_event_log_type */
6376ebe28f9SIra Weiny u8 clear_flags;
6386ebe28f9SIra Weiny u8 nr_recs;
6396ebe28f9SIra Weiny u8 reserved[3];
6406ebe28f9SIra Weiny __le16 handles[];
6416ebe28f9SIra Weiny } __packed;
6426ebe28f9SIra Weiny #define CXL_CLEAR_EVENT_MAX_HANDLES U8_MAX
6436ebe28f9SIra Weiny
644d54a531aSIra Weiny /*
645d54a531aSIra Weiny * General Media Event Record
646d54a531aSIra Weiny * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43
647d54a531aSIra Weiny */
648d54a531aSIra Weiny #define CXL_EVENT_GEN_MED_COMP_ID_SIZE 0x10
649d54a531aSIra Weiny struct cxl_event_gen_media {
650d54a531aSIra Weiny struct cxl_event_record_hdr hdr;
651d54a531aSIra Weiny __le64 phys_addr;
652d54a531aSIra Weiny u8 descriptor;
653d54a531aSIra Weiny u8 type;
654d54a531aSIra Weiny u8 transaction_type;
655d54a531aSIra Weiny u8 validity_flags[2];
656d54a531aSIra Weiny u8 channel;
657d54a531aSIra Weiny u8 rank;
658d54a531aSIra Weiny u8 device[3];
659d54a531aSIra Weiny u8 component_id[CXL_EVENT_GEN_MED_COMP_ID_SIZE];
660d54a531aSIra Weiny u8 reserved[46];
661d54a531aSIra Weiny } __packed;
662d54a531aSIra Weiny
6632d6c1e6dSIra Weiny /*
6642d6c1e6dSIra Weiny * DRAM Event Record - DER
6652d6c1e6dSIra Weiny * CXL rev 3.0 section 8.2.9.2.1.2; Table 3-44
6662d6c1e6dSIra Weiny */
6672d6c1e6dSIra Weiny #define CXL_EVENT_DER_CORRECTION_MASK_SIZE 0x20
6682d6c1e6dSIra Weiny struct cxl_event_dram {
6692d6c1e6dSIra Weiny struct cxl_event_record_hdr hdr;
6702d6c1e6dSIra Weiny __le64 phys_addr;
6712d6c1e6dSIra Weiny u8 descriptor;
6722d6c1e6dSIra Weiny u8 type;
6732d6c1e6dSIra Weiny u8 transaction_type;
6742d6c1e6dSIra Weiny u8 validity_flags[2];
6752d6c1e6dSIra Weiny u8 channel;
6762d6c1e6dSIra Weiny u8 rank;
6772d6c1e6dSIra Weiny u8 nibble_mask[3];
6782d6c1e6dSIra Weiny u8 bank_group;
6792d6c1e6dSIra Weiny u8 bank;
6802d6c1e6dSIra Weiny u8 row[3];
6812d6c1e6dSIra Weiny u8 column[2];
6822d6c1e6dSIra Weiny u8 correction_mask[CXL_EVENT_DER_CORRECTION_MASK_SIZE];
6832d6c1e6dSIra Weiny u8 reserved[0x17];
6842d6c1e6dSIra Weiny } __packed;
6852d6c1e6dSIra Weiny
68695b49479SIra Weiny /*
68795b49479SIra Weiny * Get Health Info Record
68895b49479SIra Weiny * CXL rev 3.0 section 8.2.9.8.3.1; Table 8-100
68995b49479SIra Weiny */
69095b49479SIra Weiny struct cxl_get_health_info {
69195b49479SIra Weiny u8 health_status;
69295b49479SIra Weiny u8 media_status;
69395b49479SIra Weiny u8 add_status;
69495b49479SIra Weiny u8 life_used;
69595b49479SIra Weiny u8 device_temp[2];
69695b49479SIra Weiny u8 dirty_shutdown_cnt[4];
69795b49479SIra Weiny u8 cor_vol_err_cnt[4];
69895b49479SIra Weiny u8 cor_per_err_cnt[4];
69995b49479SIra Weiny } __packed;
70095b49479SIra Weiny
70195b49479SIra Weiny /*
70295b49479SIra Weiny * Memory Module Event Record
70395b49479SIra Weiny * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45
70495b49479SIra Weiny */
70595b49479SIra Weiny struct cxl_event_mem_module {
70695b49479SIra Weiny struct cxl_event_record_hdr hdr;
70795b49479SIra Weiny u8 event_type;
70895b49479SIra Weiny struct cxl_get_health_info info;
70995b49479SIra Weiny u8 reserved[0x3d];
71095b49479SIra Weiny } __packed;
71195b49479SIra Weiny
712e7ad1bf6SDan Williams struct cxl_mbox_get_partition_info {
713e7ad1bf6SDan Williams __le64 active_volatile_cap;
714e7ad1bf6SDan Williams __le64 active_persistent_cap;
715e7ad1bf6SDan Williams __le64 next_volatile_cap;
716e7ad1bf6SDan Williams __le64 next_persistent_cap;
717e7ad1bf6SDan Williams } __packed;
718e7ad1bf6SDan Williams
71949be6dd8SDan Williams struct cxl_mbox_get_lsa {
7208a664875SAlison Schofield __le32 offset;
7218a664875SAlison Schofield __le32 length;
72249be6dd8SDan Williams } __packed;
72349be6dd8SDan Williams
72449be6dd8SDan Williams struct cxl_mbox_set_lsa {
7258a664875SAlison Schofield __le32 offset;
7268a664875SAlison Schofield __le32 reserved;
72749be6dd8SDan Williams u8 data[];
72849be6dd8SDan Williams } __packed;
72949be6dd8SDan Williams
7306179045cSAlison Schofield struct cxl_mbox_set_partition_info {
7316179045cSAlison Schofield __le64 volatile_capacity;
7326179045cSAlison Schofield u8 flags;
7336179045cSAlison Schofield } __packed;
7346179045cSAlison Schofield
7356179045cSAlison Schofield #define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0)
7366179045cSAlison Schofield
737fa884345SJonathan Cameron /* Set Timestamp CXL 3.0 Spec 8.2.9.4.2 */
738fa884345SJonathan Cameron struct cxl_mbox_set_timestamp_in {
739fa884345SJonathan Cameron __le64 timestamp;
740fa884345SJonathan Cameron
741fa884345SJonathan Cameron } __packed;
742fa884345SJonathan Cameron
743ed83f7caSAlison Schofield /* Get Poison List CXL 3.0 Spec 8.2.9.8.4.1 */
744ed83f7caSAlison Schofield struct cxl_mbox_poison_in {
745ed83f7caSAlison Schofield __le64 offset;
746ed83f7caSAlison Schofield __le64 length;
747ed83f7caSAlison Schofield } __packed;
748ed83f7caSAlison Schofield
749ed83f7caSAlison Schofield struct cxl_mbox_poison_out {
750ed83f7caSAlison Schofield u8 flags;
751ed83f7caSAlison Schofield u8 rsvd1;
752ed83f7caSAlison Schofield __le64 overflow_ts;
753ed83f7caSAlison Schofield __le16 count;
754ed83f7caSAlison Schofield u8 rsvd2[20];
755ed83f7caSAlison Schofield struct cxl_poison_record {
756ed83f7caSAlison Schofield __le64 address;
757ed83f7caSAlison Schofield __le32 length;
758ed83f7caSAlison Schofield __le32 rsvd;
759ed83f7caSAlison Schofield } __packed record[];
760ed83f7caSAlison Schofield } __packed;
761ed83f7caSAlison Schofield
762ed83f7caSAlison Schofield /*
763ed83f7caSAlison Schofield * Get Poison List address field encodes the starting
764ed83f7caSAlison Schofield * address of poison, and the source of the poison.
765ed83f7caSAlison Schofield */
766ed83f7caSAlison Schofield #define CXL_POISON_START_MASK GENMASK_ULL(63, 6)
767ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_MASK GENMASK(2, 0)
768ed83f7caSAlison Schofield
769ed83f7caSAlison Schofield /* Get Poison List record length is in units of 64 bytes */
770ed83f7caSAlison Schofield #define CXL_POISON_LEN_MULT 64
771ed83f7caSAlison Schofield
772ed83f7caSAlison Schofield /* Kernel defined maximum for a list of poison errors */
773ed83f7caSAlison Schofield #define CXL_POISON_LIST_MAX 1024
774ed83f7caSAlison Schofield
775ed83f7caSAlison Schofield /* Get Poison List: Payload out flags */
776ed83f7caSAlison Schofield #define CXL_POISON_FLAG_MORE BIT(0)
777ed83f7caSAlison Schofield #define CXL_POISON_FLAG_OVERFLOW BIT(1)
778ed83f7caSAlison Schofield #define CXL_POISON_FLAG_SCANNING BIT(2)
779ed83f7caSAlison Schofield
780ed83f7caSAlison Schofield /* Get Poison List: Poison Source */
781ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_UNKNOWN 0
782ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_EXTERNAL 1
783ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_INTERNAL 2
784ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_INJECTED 3
785ed83f7caSAlison Schofield #define CXL_POISON_SOURCE_VENDOR 7
786ed83f7caSAlison Schofield
787d2fbc486SAlison Schofield /* Inject & Clear Poison CXL 3.0 Spec 8.2.9.8.4.2/3 */
788d2fbc486SAlison Schofield struct cxl_mbox_inject_poison {
789d2fbc486SAlison Schofield __le64 address;
790d2fbc486SAlison Schofield };
791d2fbc486SAlison Schofield
7929690b077SAlison Schofield /* Clear Poison CXL 3.0 Spec 8.2.9.8.4.3 */
7939690b077SAlison Schofield struct cxl_mbox_clear_poison {
7949690b077SAlison Schofield __le64 address;
7959690b077SAlison Schofield u8 write_data[CXL_POISON_LEN_MULT];
7969690b077SAlison Schofield } __packed;
7979690b077SAlison Schofield
7984faf31b4SDan Williams /**
7994faf31b4SDan Williams * struct cxl_mem_command - Driver representation of a memory device command
8004faf31b4SDan Williams * @info: Command information as it exists for the UAPI
8014faf31b4SDan Williams * @opcode: The actual bits used for the mailbox protocol
8024faf31b4SDan Williams * @flags: Set of flags effecting driver behavior.
8034faf31b4SDan Williams *
8044faf31b4SDan Williams * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
8054faf31b4SDan Williams * will be enabled by the driver regardless of what hardware may have
8064faf31b4SDan Williams * advertised.
8074faf31b4SDan Williams *
8084faf31b4SDan Williams * The cxl_mem_command is the driver's internal representation of commands that
8094faf31b4SDan Williams * are supported by the driver. Some of these commands may not be supported by
8104faf31b4SDan Williams * the hardware. The driver will use @info to validate the fields passed in by
8114faf31b4SDan Williams * the user then submit the @opcode to the hardware.
8124faf31b4SDan Williams *
8134faf31b4SDan Williams * See struct cxl_command_info.
8144faf31b4SDan Williams */
8154faf31b4SDan Williams struct cxl_mem_command {
8164faf31b4SDan Williams struct cxl_command_info info;
8174faf31b4SDan Williams enum cxl_opcode opcode;
8184faf31b4SDan Williams u32 flags;
8194faf31b4SDan Williams #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
8204faf31b4SDan Williams };
8214faf31b4SDan Williams
82232828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PASS_SET 0x01
82332828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET 0x02
82432828115SDave Jiang #define CXL_PMEM_SEC_STATE_LOCKED 0x04
82532828115SDave Jiang #define CXL_PMEM_SEC_STATE_FROZEN 0x08
82632828115SDave Jiang #define CXL_PMEM_SEC_STATE_USER_PLIMIT 0x10
82732828115SDave Jiang #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT 0x20
82832828115SDave Jiang
82999746940SDave Jiang /* set passphrase input payload */
83099746940SDave Jiang struct cxl_set_pass {
83199746940SDave Jiang u8 type;
83299746940SDave Jiang u8 reserved[31];
83399746940SDave Jiang /* CXL field using NVDIMM define, same length */
83499746940SDave Jiang u8 old_pass[NVDIMM_PASSPHRASE_LEN];
83599746940SDave Jiang u8 new_pass[NVDIMM_PASSPHRASE_LEN];
83699746940SDave Jiang } __packed;
83799746940SDave Jiang
838c4ef680dSDave Jiang /* disable passphrase input payload */
839c4ef680dSDave Jiang struct cxl_disable_pass {
840c4ef680dSDave Jiang u8 type;
841c4ef680dSDave Jiang u8 reserved[31];
842c4ef680dSDave Jiang u8 pass[NVDIMM_PASSPHRASE_LEN];
843c4ef680dSDave Jiang } __packed;
844c4ef680dSDave Jiang
8453b502e88SDave Jiang /* passphrase secure erase payload */
8463b502e88SDave Jiang struct cxl_pass_erase {
8473b502e88SDave Jiang u8 type;
8483b502e88SDave Jiang u8 reserved[31];
8493b502e88SDave Jiang u8 pass[NVDIMM_PASSPHRASE_LEN];
8503b502e88SDave Jiang } __packed;
8513b502e88SDave Jiang
85299746940SDave Jiang enum {
85399746940SDave Jiang CXL_PMEM_SEC_PASS_MASTER = 0,
85499746940SDave Jiang CXL_PMEM_SEC_PASS_USER,
85599746940SDave Jiang };
85699746940SDave Jiang
85759f8d151SDan Williams int cxl_internal_send_cmd(struct cxl_memdev_state *mds,
8585331cdf4SDan Williams struct cxl_mbox_cmd *cmd);
85959f8d151SDan Williams int cxl_dev_state_identify(struct cxl_memdev_state *mds);
8602e4ba0ecSDan Williams int cxl_await_media_ready(struct cxl_dev_state *cxlds);
86159f8d151SDan Williams int cxl_enumerate_cmds(struct cxl_memdev_state *mds);
86259f8d151SDan Williams int cxl_mem_create_range_info(struct cxl_memdev_state *mds);
86359f8d151SDan Williams struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev);
86459f8d151SDan Williams void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
86559f8d151SDan Williams unsigned long *cmds);
86659f8d151SDan Williams void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds,
86759f8d151SDan Williams unsigned long *cmds);
86859f8d151SDan Williams void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status);
86959f8d151SDan Williams int cxl_set_timestamp(struct cxl_memdev_state *mds);
87059f8d151SDan Williams int cxl_poison_state_init(struct cxl_memdev_state *mds);
871ed83f7caSAlison Schofield int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
872ed83f7caSAlison Schofield struct cxl_region *cxlr);
8737ff6ad10SAlison Schofield int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
874d2fbc486SAlison Schofield int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
8759690b077SAlison Schofield int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
876fa884345SJonathan Cameron
8779ea4dcf4SDan Williams #ifdef CONFIG_CXL_SUSPEND
8789ea4dcf4SDan Williams void cxl_mem_active_inc(void);
8799ea4dcf4SDan Williams void cxl_mem_active_dec(void);
8809ea4dcf4SDan Williams #else
cxl_mem_active_inc(void)8819ea4dcf4SDan Williams static inline void cxl_mem_active_inc(void)
8829ea4dcf4SDan Williams {
8839ea4dcf4SDan Williams }
cxl_mem_active_dec(void)8849ea4dcf4SDan Williams static inline void cxl_mem_active_dec(void)
8859ea4dcf4SDan Williams {
8869ea4dcf4SDan Williams }
8879ea4dcf4SDan Williams #endif
888d17d0540SDan Williams
889d1d13a09SDan Williams int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd);
89048dcdbb1SDavidlohr Bueso
891843836bfSYao Xingtao /**
892843836bfSYao Xingtao * struct cxl_hdm - HDM Decoder registers and cached / decoded capabilities
893843836bfSYao Xingtao * @regs: mapped registers, see devm_cxl_setup_hdm()
894843836bfSYao Xingtao * @decoder_count: number of decoders for this port
895843836bfSYao Xingtao * @target_count: for switch decoders, max downstream port targets
896843836bfSYao Xingtao * @interleave_mask: interleave granularity capability, see check_interleave_cap()
897843836bfSYao Xingtao * @iw_cap_mask: bitmask of supported interleave ways, see check_interleave_cap()
898843836bfSYao Xingtao * @port: mapped cxl_port, see devm_cxl_setup_hdm()
899843836bfSYao Xingtao */
900d17d0540SDan Williams struct cxl_hdm {
901d17d0540SDan Williams struct cxl_component_regs regs;
902d17d0540SDan Williams unsigned int decoder_count;
903d17d0540SDan Williams unsigned int target_count;
904d17d0540SDan Williams unsigned int interleave_mask;
905843836bfSYao Xingtao unsigned long iw_cap_mask;
906d17d0540SDan Williams struct cxl_port *port;
907d17d0540SDan Williams };
908cc2a4878SDan Williams
909cc2a4878SDan Williams struct seq_file;
910cc2a4878SDan Williams struct dentry *cxl_debugfs_create_dir(const char *dir);
911cc2a4878SDan Williams void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
9125161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */
913