cxl/pci: Clarify devm host for memdev relative setup
[platform/kernel/linux-starfive.git] / drivers / cxl / cxlmem.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright(c) 2020-2021 Intel Corporation. */
3 #ifndef __CXL_MEM_H__
4 #define __CXL_MEM_H__
5 #include <uapi/linux/cxl_mem.h>
6 #include <linux/cdev.h>
7 #include <linux/uuid.h>
8 #include <linux/rcuwait.h>
9 #include "cxl.h"
10
11 /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
12 #define CXLMDEV_STATUS_OFFSET 0x0
13 #define   CXLMDEV_DEV_FATAL BIT(0)
14 #define   CXLMDEV_FW_HALT BIT(1)
15 #define   CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
16 #define     CXLMDEV_MS_NOT_READY 0
17 #define     CXLMDEV_MS_READY 1
18 #define     CXLMDEV_MS_ERROR 2
19 #define     CXLMDEV_MS_DISABLED 3
20 #define CXLMDEV_READY(status)                                                  \
21         (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) ==                \
22          CXLMDEV_MS_READY)
23 #define   CXLMDEV_MBOX_IF_READY BIT(4)
24 #define   CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
25 #define     CXLMDEV_RESET_NEEDED_NOT 0
26 #define     CXLMDEV_RESET_NEEDED_COLD 1
27 #define     CXLMDEV_RESET_NEEDED_WARM 2
28 #define     CXLMDEV_RESET_NEEDED_HOT 3
29 #define     CXLMDEV_RESET_NEEDED_CXL 4
30 #define CXLMDEV_RESET_NEEDED(status)                                           \
31         (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
32          CXLMDEV_RESET_NEEDED_NOT)
33
34 /**
35  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
36  * @dev: driver core device object
37  * @cdev: char dev core object for ioctl operations
38  * @cxlds: The device state backing this device
39  * @detach_work: active memdev lost a port in its ancestry
40  * @cxl_nvb: coordinate removal of @cxl_nvd if present
41  * @cxl_nvd: optional bridge to an nvdimm if the device supports pmem
42  * @endpoint: connection to the CXL port topology for this memory device
43  * @id: id number of this memdev instance.
44  * @depth: endpoint port depth
45  */
46 struct cxl_memdev {
47         struct device dev;
48         struct cdev cdev;
49         struct cxl_dev_state *cxlds;
50         struct work_struct detach_work;
51         struct cxl_nvdimm_bridge *cxl_nvb;
52         struct cxl_nvdimm *cxl_nvd;
53         struct cxl_port *endpoint;
54         int id;
55         int depth;
56 };
57
58 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
59 {
60         return container_of(dev, struct cxl_memdev, dev);
61 }
62
63 static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled)
64 {
65         return to_cxl_port(cxled->cxld.dev.parent);
66 }
67
68 static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd)
69 {
70         return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
71 }
72
73 static inline struct cxl_memdev *
74 cxled_to_memdev(struct cxl_endpoint_decoder *cxled)
75 {
76         struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent);
77
78         return to_cxl_memdev(port->uport_dev);
79 }
80
81 bool is_cxl_memdev(const struct device *dev);
82 static inline bool is_cxl_endpoint(struct cxl_port *port)
83 {
84         return is_cxl_memdev(port->uport_dev);
85 }
86
87 struct cxl_memdev *devm_cxl_add_memdev(struct device *host,
88                                        struct cxl_dev_state *cxlds);
89 struct cxl_memdev_state;
90 int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds);
91 int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
92                          resource_size_t base, resource_size_t len,
93                          resource_size_t skipped);
94
95 static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
96                                          struct cxl_memdev *cxlmd)
97 {
98         if (!port)
99                 return NULL;
100
101         return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev);
102 }
103
104 /**
105  * struct cxl_mbox_cmd - A command to be submitted to hardware.
106  * @opcode: (input) The command set and command submitted to hardware.
107  * @payload_in: (input) Pointer to the input payload.
108  * @payload_out: (output) Pointer to the output payload. Must be allocated by
109  *               the caller.
110  * @size_in: (input) Number of bytes to load from @payload_in.
111  * @size_out: (input) Max number of bytes loaded into @payload_out.
112  *            (output) Number of bytes generated by the device. For fixed size
113  *            outputs commands this is always expected to be deterministic. For
114  *            variable sized output commands, it tells the exact number of bytes
115  *            written.
116  * @min_out: (input) internal command output payload size validation
117  * @poll_count: (input) Number of timeouts to attempt.
118  * @poll_interval_ms: (input) Time between mailbox background command polling
119  *                    interval timeouts.
120  * @return_code: (output) Error code returned from hardware.
121  *
122  * This is the primary mechanism used to send commands to the hardware.
123  * All the fields except @payload_* correspond exactly to the fields described in
124  * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and
125  * @payload_out are written to, and read from the Command Payload Registers
126  * defined in CXL 2.0 8.2.8.4.8.
127  */
128 struct cxl_mbox_cmd {
129         u16 opcode;
130         void *payload_in;
131         void *payload_out;
132         size_t size_in;
133         size_t size_out;
134         size_t min_out;
135         int poll_count;
136         int poll_interval_ms;
137         u16 return_code;
138 };
139
140 /*
141  * Per CXL 3.0 Section 8.2.8.4.5.1
142  */
143 #define CMD_CMD_RC_TABLE                                                        \
144         C(SUCCESS, 0, NULL),                                                    \
145         C(BACKGROUND, -ENXIO, "background cmd started successfully"),           \
146         C(INPUT, -ENXIO, "cmd input was invalid"),                              \
147         C(UNSUPPORTED, -ENXIO, "cmd is not supported"),                         \
148         C(INTERNAL, -ENXIO, "internal device error"),                           \
149         C(RETRY, -ENXIO, "temporary error, retry once"),                        \
150         C(BUSY, -ENXIO, "ongoing background operation"),                        \
151         C(MEDIADISABLED, -ENXIO, "media access is disabled"),                   \
152         C(FWINPROGRESS, -ENXIO, "one FW package can be transferred at a time"), \
153         C(FWOOO, -ENXIO, "FW package content was transferred out of order"),    \
154         C(FWAUTH, -ENXIO, "FW package authentication failed"),                  \
155         C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"),  \
156         C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"),         \
157         C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"),          \
158         C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"),     \
159         C(PADDR, -EFAULT, "physical address specified is invalid"),             \
160         C(POISONLMT, -ENXIO, "poison injection limit has been reached"),        \
161         C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"),              \
162         C(ABORT, -ENXIO, "background cmd was aborted by device"),               \
163         C(SECURITY, -ENXIO, "not valid in the current security state"),         \
164         C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"),   \
165         C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\
166         C(PAYLOADLEN, -ENXIO, "invalid payload length"),                        \
167         C(LOG, -ENXIO, "invalid or unsupported log page"),                      \
168         C(INTERRUPTED, -ENXIO, "asynchronous event occured"),                   \
169         C(FEATUREVERSION, -ENXIO, "unsupported feature version"),               \
170         C(FEATURESELVALUE, -ENXIO, "unsupported feature selection value"),      \
171         C(FEATURETRANSFERIP, -ENXIO, "feature transfer in progress"),           \
172         C(FEATURETRANSFEROOO, -ENXIO, "feature transfer out of order"),         \
173         C(RESOURCEEXHAUSTED, -ENXIO, "resources are exhausted"),                \
174         C(EXTLIST, -ENXIO, "invalid Extent List"),                              \
175
176 #undef C
177 #define C(a, b, c) CXL_MBOX_CMD_RC_##a
178 enum  { CMD_CMD_RC_TABLE };
179 #undef C
180 #define C(a, b, c) { b, c }
181 struct cxl_mbox_cmd_rc {
182         int err;
183         const char *desc;
184 };
185
186 static const
187 struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
188 #undef C
189
190 static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
191 {
192         return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
193 }
194
195 static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
196 {
197         return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
198 }
199
200 /*
201  * CXL 2.0 - Memory capacity multiplier
202  * See Section 8.2.9.5
203  *
204  * Volatile, Persistent, and Partition capacities are specified to be in
205  * multiples of 256MB - define a multiplier to convert to/from bytes.
206  */
207 #define CXL_CAPACITY_MULTIPLIER SZ_256M
208
209 /*
210  * Event Interrupt Policy
211  *
212  * CXL rev 3.0 section 8.2.9.2.4; Table 8-52
213  */
214 enum cxl_event_int_mode {
215         CXL_INT_NONE            = 0x00,
216         CXL_INT_MSI_MSIX        = 0x01,
217         CXL_INT_FW              = 0x02
218 };
219 struct cxl_event_interrupt_policy {
220         u8 info_settings;
221         u8 warn_settings;
222         u8 failure_settings;
223         u8 fatal_settings;
224 } __packed;
225
226 /**
227  * struct cxl_event_state - Event log driver state
228  *
229  * @buf: Buffer to receive event data
230  * @log_lock: Serialize event_buf and log use
231  */
232 struct cxl_event_state {
233         struct cxl_get_event_payload *buf;
234         struct mutex log_lock;
235 };
236
237 /* Device enabled poison commands */
238 enum poison_cmd_enabled_bits {
239         CXL_POISON_ENABLED_LIST,
240         CXL_POISON_ENABLED_INJECT,
241         CXL_POISON_ENABLED_CLEAR,
242         CXL_POISON_ENABLED_SCAN_CAPS,
243         CXL_POISON_ENABLED_SCAN_MEDIA,
244         CXL_POISON_ENABLED_SCAN_RESULTS,
245         CXL_POISON_ENABLED_MAX
246 };
247
248 /* Device enabled security commands */
249 enum security_cmd_enabled_bits {
250         CXL_SEC_ENABLED_SANITIZE,
251         CXL_SEC_ENABLED_SECURE_ERASE,
252         CXL_SEC_ENABLED_GET_SECURITY_STATE,
253         CXL_SEC_ENABLED_SET_PASSPHRASE,
254         CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
255         CXL_SEC_ENABLED_UNLOCK,
256         CXL_SEC_ENABLED_FREEZE_SECURITY,
257         CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
258         CXL_SEC_ENABLED_MAX
259 };
260
261 /**
262  * struct cxl_poison_state - Driver poison state info
263  *
264  * @max_errors: Maximum media error records held in device cache
265  * @enabled_cmds: All poison commands enabled in the CEL
266  * @list_out: The poison list payload returned by device
267  * @lock: Protect reads of the poison list
268  *
269  * Reads of the poison list are synchronized to ensure that a reader
270  * does not get an incomplete list because their request overlapped
271  * (was interrupted or preceded by) another read request of the same
272  * DPA range. CXL Spec 3.0 Section 8.2.9.8.4.1
273  */
274 struct cxl_poison_state {
275         u32 max_errors;
276         DECLARE_BITMAP(enabled_cmds, CXL_POISON_ENABLED_MAX);
277         struct cxl_mbox_poison_out *list_out;
278         struct mutex lock;  /* Protect reads of poison list */
279 };
280
281 /*
282  * Get FW Info
283  * CXL rev 3.0 section 8.2.9.3.1; Table 8-56
284  */
285 struct cxl_mbox_get_fw_info {
286         u8 num_slots;
287         u8 slot_info;
288         u8 activation_cap;
289         u8 reserved[13];
290         char slot_1_revision[16];
291         char slot_2_revision[16];
292         char slot_3_revision[16];
293         char slot_4_revision[16];
294 } __packed;
295
296 #define CXL_FW_INFO_SLOT_INFO_CUR_MASK                  GENMASK(2, 0)
297 #define CXL_FW_INFO_SLOT_INFO_NEXT_MASK                 GENMASK(5, 3)
298 #define CXL_FW_INFO_SLOT_INFO_NEXT_SHIFT                3
299 #define CXL_FW_INFO_ACTIVATION_CAP_HAS_LIVE_ACTIVATE    BIT(0)
300
301 /*
302  * Transfer FW Input Payload
303  * CXL rev 3.0 section 8.2.9.3.2; Table 8-57
304  */
305 struct cxl_mbox_transfer_fw {
306         u8 action;
307         u8 slot;
308         u8 reserved[2];
309         __le32 offset;
310         u8 reserved2[0x78];
311         u8 data[];
312 } __packed;
313
314 #define CXL_FW_TRANSFER_ACTION_FULL     0x0
315 #define CXL_FW_TRANSFER_ACTION_INITIATE 0x1
316 #define CXL_FW_TRANSFER_ACTION_CONTINUE 0x2
317 #define CXL_FW_TRANSFER_ACTION_END      0x3
318 #define CXL_FW_TRANSFER_ACTION_ABORT    0x4
319
320 /*
321  * CXL rev 3.0 section 8.2.9.3.2 mandates 128-byte alignment for FW packages
322  * and for each part transferred in a Transfer FW command.
323  */
324 #define CXL_FW_TRANSFER_ALIGNMENT       128
325
326 /*
327  * Activate FW Input Payload
328  * CXL rev 3.0 section 8.2.9.3.3; Table 8-58
329  */
330 struct cxl_mbox_activate_fw {
331         u8 action;
332         u8 slot;
333 } __packed;
334
335 #define CXL_FW_ACTIVATE_ONLINE          0x0
336 #define CXL_FW_ACTIVATE_OFFLINE         0x1
337
338 /* FW state bits */
339 #define CXL_FW_STATE_BITS               32
340 #define CXL_FW_CANCEL                   0
341
342 /**
343  * struct cxl_fw_state - Firmware upload / activation state
344  *
345  * @state: fw_uploader state bitmask
346  * @oneshot: whether the fw upload fits in a single transfer
347  * @num_slots: Number of FW slots available
348  * @cur_slot: Slot number currently active
349  * @next_slot: Slot number for the new firmware
350  */
351 struct cxl_fw_state {
352         DECLARE_BITMAP(state, CXL_FW_STATE_BITS);
353         bool oneshot;
354         int num_slots;
355         int cur_slot;
356         int next_slot;
357 };
358
359 /**
360  * struct cxl_security_state - Device security state
361  *
362  * @state: state of last security operation
363  * @enabled_cmds: All security commands enabled in the CEL
364  * @poll_tmo_secs: polling timeout
365  * @poll_dwork: polling work item
366  * @sanitize_node: sanitation sysfs file to notify
367  */
368 struct cxl_security_state {
369         unsigned long state;
370         DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX);
371         int poll_tmo_secs;
372         struct delayed_work poll_dwork;
373         struct kernfs_node *sanitize_node;
374 };
375
376 /*
377  * enum cxl_devtype - delineate type-2 from a generic type-3 device
378  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
379  *                       HDM-DB, no requirement that this device implements a
380  *                       mailbox, or other memory-device-standard manageability
381  *                       flows.
382  * @CXL_DEVTYPE_CLASSMEM - Common class definition of a CXL Type-3 device with
383  *                         HDM-H and class-mandatory memory device registers
384  */
385 enum cxl_devtype {
386         CXL_DEVTYPE_DEVMEM,
387         CXL_DEVTYPE_CLASSMEM,
388 };
389
390 /**
391  * struct cxl_dev_state - The driver device state
392  *
393  * cxl_dev_state represents the CXL driver/device state.  It provides an
394  * interface to mailbox commands as well as some cached data about the device.
395  * Currently only memory devices are represented.
396  *
397  * @dev: The device associated with this CXL state
398  * @cxlmd: The device representing the CXL.mem capabilities of @dev
399  * @regs: Parsed register blocks
400  * @cxl_dvsec: Offset to the PCIe device DVSEC
401  * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
402  * @media_ready: Indicate whether the device media is usable
403  * @dpa_res: Overall DPA resource tree for the device
404  * @pmem_res: Active Persistent memory capacity configuration
405  * @ram_res: Active Volatile memory capacity configuration
406  * @component_reg_phys: register base of component registers
407  * @serial: PCIe Device Serial Number
408  * @type: Generic Memory Class device or Vendor Specific Memory device
409  */
410 struct cxl_dev_state {
411         struct device *dev;
412         struct cxl_memdev *cxlmd;
413         struct cxl_regs regs;
414         int cxl_dvsec;
415         bool rcd;
416         bool media_ready;
417         struct resource dpa_res;
418         struct resource pmem_res;
419         struct resource ram_res;
420         resource_size_t component_reg_phys;
421         u64 serial;
422         enum cxl_devtype type;
423 };
424
425 /**
426  * struct cxl_memdev_state - Generic Type-3 Memory Device Class driver data
427  *
428  * CXL 8.1.12.1 PCI Header - Class Code Register Memory Device defines
429  * common memory device functionality like the presence of a mailbox and
430  * the functionality related to that like Identify Memory Device and Get
431  * Partition Info
432  * @cxlds: Core driver state common across Type-2 and Type-3 devices
433  * @payload_size: Size of space for payload
434  *                (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
435  * @lsa_size: Size of Label Storage Area
436  *                (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
437  * @mbox_mutex: Mutex to synchronize mailbox access.
438  * @firmware_version: Firmware version for the memory device.
439  * @enabled_cmds: Hardware commands found enabled in CEL.
440  * @exclusive_cmds: Commands that are kernel-internal only
441  * @total_bytes: sum of all possible capacities
442  * @volatile_only_bytes: hard volatile capacity
443  * @persistent_only_bytes: hard persistent capacity
444  * @partition_align_bytes: alignment size for partition-able capacity
445  * @active_volatile_bytes: sum of hard + soft volatile
446  * @active_persistent_bytes: sum of hard + soft persistent
447  * @next_volatile_bytes: volatile capacity change pending device reset
448  * @next_persistent_bytes: persistent capacity change pending device reset
449  * @event: event log driver state
450  * @poison: poison driver state info
451  * @security: security driver state info
452  * @fw: firmware upload / activation state
453  * @mbox_send: @dev specific transport for transmitting mailbox commands
454  *
455  * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
456  * details on capacity parameters.
457  */
458 struct cxl_memdev_state {
459         struct cxl_dev_state cxlds;
460         size_t payload_size;
461         size_t lsa_size;
462         struct mutex mbox_mutex; /* Protects device mailbox and firmware */
463         char firmware_version[0x10];
464         DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
465         DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
466         u64 total_bytes;
467         u64 volatile_only_bytes;
468         u64 persistent_only_bytes;
469         u64 partition_align_bytes;
470         u64 active_volatile_bytes;
471         u64 active_persistent_bytes;
472         u64 next_volatile_bytes;
473         u64 next_persistent_bytes;
474         struct cxl_event_state event;
475         struct cxl_poison_state poison;
476         struct cxl_security_state security;
477         struct cxl_fw_state fw;
478
479         struct rcuwait mbox_wait;
480         int (*mbox_send)(struct cxl_memdev_state *mds,
481                          struct cxl_mbox_cmd *cmd);
482 };
483
484 static inline struct cxl_memdev_state *
485 to_cxl_memdev_state(struct cxl_dev_state *cxlds)
486 {
487         if (cxlds->type != CXL_DEVTYPE_CLASSMEM)
488                 return NULL;
489         return container_of(cxlds, struct cxl_memdev_state, cxlds);
490 }
491
492 enum cxl_opcode {
493         CXL_MBOX_OP_INVALID             = 0x0000,
494         CXL_MBOX_OP_RAW                 = CXL_MBOX_OP_INVALID,
495         CXL_MBOX_OP_GET_EVENT_RECORD    = 0x0100,
496         CXL_MBOX_OP_CLEAR_EVENT_RECORD  = 0x0101,
497         CXL_MBOX_OP_GET_EVT_INT_POLICY  = 0x0102,
498         CXL_MBOX_OP_SET_EVT_INT_POLICY  = 0x0103,
499         CXL_MBOX_OP_GET_FW_INFO         = 0x0200,
500         CXL_MBOX_OP_TRANSFER_FW         = 0x0201,
501         CXL_MBOX_OP_ACTIVATE_FW         = 0x0202,
502         CXL_MBOX_OP_SET_TIMESTAMP       = 0x0301,
503         CXL_MBOX_OP_GET_SUPPORTED_LOGS  = 0x0400,
504         CXL_MBOX_OP_GET_LOG             = 0x0401,
505         CXL_MBOX_OP_IDENTIFY            = 0x4000,
506         CXL_MBOX_OP_GET_PARTITION_INFO  = 0x4100,
507         CXL_MBOX_OP_SET_PARTITION_INFO  = 0x4101,
508         CXL_MBOX_OP_GET_LSA             = 0x4102,
509         CXL_MBOX_OP_SET_LSA             = 0x4103,
510         CXL_MBOX_OP_GET_HEALTH_INFO     = 0x4200,
511         CXL_MBOX_OP_GET_ALERT_CONFIG    = 0x4201,
512         CXL_MBOX_OP_SET_ALERT_CONFIG    = 0x4202,
513         CXL_MBOX_OP_GET_SHUTDOWN_STATE  = 0x4203,
514         CXL_MBOX_OP_SET_SHUTDOWN_STATE  = 0x4204,
515         CXL_MBOX_OP_GET_POISON          = 0x4300,
516         CXL_MBOX_OP_INJECT_POISON       = 0x4301,
517         CXL_MBOX_OP_CLEAR_POISON        = 0x4302,
518         CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
519         CXL_MBOX_OP_SCAN_MEDIA          = 0x4304,
520         CXL_MBOX_OP_GET_SCAN_MEDIA      = 0x4305,
521         CXL_MBOX_OP_SANITIZE            = 0x4400,
522         CXL_MBOX_OP_SECURE_ERASE        = 0x4401,
523         CXL_MBOX_OP_GET_SECURITY_STATE  = 0x4500,
524         CXL_MBOX_OP_SET_PASSPHRASE      = 0x4501,
525         CXL_MBOX_OP_DISABLE_PASSPHRASE  = 0x4502,
526         CXL_MBOX_OP_UNLOCK              = 0x4503,
527         CXL_MBOX_OP_FREEZE_SECURITY     = 0x4504,
528         CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE     = 0x4505,
529         CXL_MBOX_OP_MAX                 = 0x10000
530 };
531
532 #define DEFINE_CXL_CEL_UUID                                                    \
533         UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62,     \
534                   0x3b, 0x3f, 0x17)
535
536 #define DEFINE_CXL_VENDOR_DEBUG_UUID                                           \
537         UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19,     \
538                   0x40, 0x3d, 0x86)
539
540 struct cxl_mbox_get_supported_logs {
541         __le16 entries;
542         u8 rsvd[6];
543         struct cxl_gsl_entry {
544                 uuid_t uuid;
545                 __le32 size;
546         } __packed entry[];
547 }  __packed;
548
549 struct cxl_cel_entry {
550         __le16 opcode;
551         __le16 effect;
552 } __packed;
553
554 struct cxl_mbox_get_log {
555         uuid_t uuid;
556         __le32 offset;
557         __le32 length;
558 } __packed;
559
560 /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
561 struct cxl_mbox_identify {
562         char fw_revision[0x10];
563         __le64 total_capacity;
564         __le64 volatile_capacity;
565         __le64 persistent_capacity;
566         __le64 partition_align;
567         __le16 info_event_log_size;
568         __le16 warning_event_log_size;
569         __le16 failure_event_log_size;
570         __le16 fatal_event_log_size;
571         __le32 lsa_size;
572         u8 poison_list_max_mer[3];
573         __le16 inject_poison_limit;
574         u8 poison_caps;
575         u8 qos_telemetry_caps;
576 } __packed;
577
578 /*
579  * Common Event Record Format
580  * CXL rev 3.0 section 8.2.9.2.1; Table 8-42
581  */
582 struct cxl_event_record_hdr {
583         uuid_t id;
584         u8 length;
585         u8 flags[3];
586         __le16 handle;
587         __le16 related_handle;
588         __le64 timestamp;
589         u8 maint_op_class;
590         u8 reserved[15];
591 } __packed;
592
593 #define CXL_EVENT_RECORD_DATA_LENGTH 0x50
594 struct cxl_event_record_raw {
595         struct cxl_event_record_hdr hdr;
596         u8 data[CXL_EVENT_RECORD_DATA_LENGTH];
597 } __packed;
598
599 /*
600  * Get Event Records output payload
601  * CXL rev 3.0 section 8.2.9.2.2; Table 8-50
602  */
603 #define CXL_GET_EVENT_FLAG_OVERFLOW             BIT(0)
604 #define CXL_GET_EVENT_FLAG_MORE_RECORDS         BIT(1)
605 struct cxl_get_event_payload {
606         u8 flags;
607         u8 reserved1;
608         __le16 overflow_err_count;
609         __le64 first_overflow_timestamp;
610         __le64 last_overflow_timestamp;
611         __le16 record_count;
612         u8 reserved2[10];
613         struct cxl_event_record_raw records[];
614 } __packed;
615
616 /*
617  * CXL rev 3.0 section 8.2.9.2.2; Table 8-49
618  */
619 enum cxl_event_log_type {
620         CXL_EVENT_TYPE_INFO = 0x00,
621         CXL_EVENT_TYPE_WARN,
622         CXL_EVENT_TYPE_FAIL,
623         CXL_EVENT_TYPE_FATAL,
624         CXL_EVENT_TYPE_MAX
625 };
626
627 /*
628  * Clear Event Records input payload
629  * CXL rev 3.0 section 8.2.9.2.3; Table 8-51
630  */
631 struct cxl_mbox_clear_event_payload {
632         u8 event_log;           /* enum cxl_event_log_type */
633         u8 clear_flags;
634         u8 nr_recs;
635         u8 reserved[3];
636         __le16 handles[];
637 } __packed;
638 #define CXL_CLEAR_EVENT_MAX_HANDLES U8_MAX
639
640 /*
641  * General Media Event Record
642  * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43
643  */
644 #define CXL_EVENT_GEN_MED_COMP_ID_SIZE  0x10
645 struct cxl_event_gen_media {
646         struct cxl_event_record_hdr hdr;
647         __le64 phys_addr;
648         u8 descriptor;
649         u8 type;
650         u8 transaction_type;
651         u8 validity_flags[2];
652         u8 channel;
653         u8 rank;
654         u8 device[3];
655         u8 component_id[CXL_EVENT_GEN_MED_COMP_ID_SIZE];
656         u8 reserved[46];
657 } __packed;
658
659 /*
660  * DRAM Event Record - DER
661  * CXL rev 3.0 section 8.2.9.2.1.2; Table 3-44
662  */
663 #define CXL_EVENT_DER_CORRECTION_MASK_SIZE      0x20
664 struct cxl_event_dram {
665         struct cxl_event_record_hdr hdr;
666         __le64 phys_addr;
667         u8 descriptor;
668         u8 type;
669         u8 transaction_type;
670         u8 validity_flags[2];
671         u8 channel;
672         u8 rank;
673         u8 nibble_mask[3];
674         u8 bank_group;
675         u8 bank;
676         u8 row[3];
677         u8 column[2];
678         u8 correction_mask[CXL_EVENT_DER_CORRECTION_MASK_SIZE];
679         u8 reserved[0x17];
680 } __packed;
681
682 /*
683  * Get Health Info Record
684  * CXL rev 3.0 section 8.2.9.8.3.1; Table 8-100
685  */
686 struct cxl_get_health_info {
687         u8 health_status;
688         u8 media_status;
689         u8 add_status;
690         u8 life_used;
691         u8 device_temp[2];
692         u8 dirty_shutdown_cnt[4];
693         u8 cor_vol_err_cnt[4];
694         u8 cor_per_err_cnt[4];
695 } __packed;
696
697 /*
698  * Memory Module Event Record
699  * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45
700  */
701 struct cxl_event_mem_module {
702         struct cxl_event_record_hdr hdr;
703         u8 event_type;
704         struct cxl_get_health_info info;
705         u8 reserved[0x3d];
706 } __packed;
707
708 struct cxl_mbox_get_partition_info {
709         __le64 active_volatile_cap;
710         __le64 active_persistent_cap;
711         __le64 next_volatile_cap;
712         __le64 next_persistent_cap;
713 } __packed;
714
715 struct cxl_mbox_get_lsa {
716         __le32 offset;
717         __le32 length;
718 } __packed;
719
720 struct cxl_mbox_set_lsa {
721         __le32 offset;
722         __le32 reserved;
723         u8 data[];
724 } __packed;
725
726 struct cxl_mbox_set_partition_info {
727         __le64 volatile_capacity;
728         u8 flags;
729 } __packed;
730
731 #define  CXL_SET_PARTITION_IMMEDIATE_FLAG       BIT(0)
732
733 /* Set Timestamp CXL 3.0 Spec 8.2.9.4.2 */
734 struct cxl_mbox_set_timestamp_in {
735         __le64 timestamp;
736
737 } __packed;
738
739 /* Get Poison List  CXL 3.0 Spec 8.2.9.8.4.1 */
740 struct cxl_mbox_poison_in {
741         __le64 offset;
742         __le64 length;
743 } __packed;
744
745 struct cxl_mbox_poison_out {
746         u8 flags;
747         u8 rsvd1;
748         __le64 overflow_ts;
749         __le16 count;
750         u8 rsvd2[20];
751         struct cxl_poison_record {
752                 __le64 address;
753                 __le32 length;
754                 __le32 rsvd;
755         } __packed record[];
756 } __packed;
757
758 /*
759  * Get Poison List address field encodes the starting
760  * address of poison, and the source of the poison.
761  */
762 #define CXL_POISON_START_MASK           GENMASK_ULL(63, 6)
763 #define CXL_POISON_SOURCE_MASK          GENMASK(2, 0)
764
765 /* Get Poison List record length is in units of 64 bytes */
766 #define CXL_POISON_LEN_MULT     64
767
768 /* Kernel defined maximum for a list of poison errors */
769 #define CXL_POISON_LIST_MAX     1024
770
771 /* Get Poison List: Payload out flags */
772 #define CXL_POISON_FLAG_MORE            BIT(0)
773 #define CXL_POISON_FLAG_OVERFLOW        BIT(1)
774 #define CXL_POISON_FLAG_SCANNING        BIT(2)
775
776 /* Get Poison List: Poison Source */
777 #define CXL_POISON_SOURCE_UNKNOWN       0
778 #define CXL_POISON_SOURCE_EXTERNAL      1
779 #define CXL_POISON_SOURCE_INTERNAL      2
780 #define CXL_POISON_SOURCE_INJECTED      3
781 #define CXL_POISON_SOURCE_VENDOR        7
782
783 /* Inject & Clear Poison  CXL 3.0 Spec 8.2.9.8.4.2/3 */
784 struct cxl_mbox_inject_poison {
785         __le64 address;
786 };
787
788 /* Clear Poison  CXL 3.0 Spec 8.2.9.8.4.3 */
789 struct cxl_mbox_clear_poison {
790         __le64 address;
791         u8 write_data[CXL_POISON_LEN_MULT];
792 } __packed;
793
794 /**
795  * struct cxl_mem_command - Driver representation of a memory device command
796  * @info: Command information as it exists for the UAPI
797  * @opcode: The actual bits used for the mailbox protocol
798  * @flags: Set of flags effecting driver behavior.
799  *
800  *  * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
801  *    will be enabled by the driver regardless of what hardware may have
802  *    advertised.
803  *
804  * The cxl_mem_command is the driver's internal representation of commands that
805  * are supported by the driver. Some of these commands may not be supported by
806  * the hardware. The driver will use @info to validate the fields passed in by
807  * the user then submit the @opcode to the hardware.
808  *
809  * See struct cxl_command_info.
810  */
811 struct cxl_mem_command {
812         struct cxl_command_info info;
813         enum cxl_opcode opcode;
814         u32 flags;
815 #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
816 };
817
818 #define CXL_PMEM_SEC_STATE_USER_PASS_SET        0x01
819 #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET      0x02
820 #define CXL_PMEM_SEC_STATE_LOCKED               0x04
821 #define CXL_PMEM_SEC_STATE_FROZEN               0x08
822 #define CXL_PMEM_SEC_STATE_USER_PLIMIT          0x10
823 #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT        0x20
824
825 /* set passphrase input payload */
826 struct cxl_set_pass {
827         u8 type;
828         u8 reserved[31];
829         /* CXL field using NVDIMM define, same length */
830         u8 old_pass[NVDIMM_PASSPHRASE_LEN];
831         u8 new_pass[NVDIMM_PASSPHRASE_LEN];
832 } __packed;
833
834 /* disable passphrase input payload */
835 struct cxl_disable_pass {
836         u8 type;
837         u8 reserved[31];
838         u8 pass[NVDIMM_PASSPHRASE_LEN];
839 } __packed;
840
841 /* passphrase secure erase payload */
842 struct cxl_pass_erase {
843         u8 type;
844         u8 reserved[31];
845         u8 pass[NVDIMM_PASSPHRASE_LEN];
846 } __packed;
847
848 enum {
849         CXL_PMEM_SEC_PASS_MASTER = 0,
850         CXL_PMEM_SEC_PASS_USER,
851 };
852
853 int cxl_internal_send_cmd(struct cxl_memdev_state *mds,
854                           struct cxl_mbox_cmd *cmd);
855 int cxl_dev_state_identify(struct cxl_memdev_state *mds);
856 int cxl_await_media_ready(struct cxl_dev_state *cxlds);
857 int cxl_enumerate_cmds(struct cxl_memdev_state *mds);
858 int cxl_mem_create_range_info(struct cxl_memdev_state *mds);
859 struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev);
860 void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
861                                 unsigned long *cmds);
862 void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds,
863                                   unsigned long *cmds);
864 void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status);
865 int cxl_set_timestamp(struct cxl_memdev_state *mds);
866 int cxl_poison_state_init(struct cxl_memdev_state *mds);
867 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
868                        struct cxl_region *cxlr);
869 int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
870 int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
871 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
872
873 #ifdef CONFIG_CXL_SUSPEND
874 void cxl_mem_active_inc(void);
875 void cxl_mem_active_dec(void);
876 #else
877 static inline void cxl_mem_active_inc(void)
878 {
879 }
880 static inline void cxl_mem_active_dec(void)
881 {
882 }
883 #endif
884
885 int cxl_mem_sanitize(struct cxl_memdev_state *mds, u16 cmd);
886
887 struct cxl_hdm {
888         struct cxl_component_regs regs;
889         unsigned int decoder_count;
890         unsigned int target_count;
891         unsigned int interleave_mask;
892         struct cxl_port *port;
893 };
894
895 struct seq_file;
896 struct dentry *cxl_debugfs_create_dir(const char *dir);
897 void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
898 #endif /* __CXL_MEM_H__ */