1 /* SPDX-License-Identifier: GPL-2.0
3 * Copyright 2008-2013 Solarflare Communications Inc.
4 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
10 #include <linux/mutex.h>
11 #include <linux/kref.h>
12 #include <linux/rpmsg.h>
15 #include "mc_cdx_pcol.h"
18 #define CDX_WARN_ON_ONCE_PARANOID(x) WARN_ON_ONCE(x)
19 #define CDX_WARN_ON_PARANOID(x) WARN_ON(x)
21 #define CDX_WARN_ON_ONCE_PARANOID(x) do {} while (0)
22 #define CDX_WARN_ON_PARANOID(x) do {} while (0)
26 * enum cdx_mcdi_mode - MCDI transaction mode
27 * @MCDI_MODE_EVENTS: wait for an mcdi response callback.
28 * @MCDI_MODE_FAIL: we think MCDI is dead, so fail-fast all calls
35 #define MCDI_RPC_TIMEOUT (10 * HZ)
36 #define MCDI_RPC_LONG_TIMEOU (60 * HZ)
37 #define MCDI_RPC_POST_RST_TIME (10 * HZ)
39 #define MCDI_BUF_LEN (8 + MCDI_CTL_SDU_LEN_MAX)
42 * enum cdx_mcdi_cmd_state - State for an individual MCDI command
43 * @MCDI_STATE_QUEUED: Command not started and is waiting to run.
44 * @MCDI_STATE_RETRY: Command was submitted and MC rejected with no resources,
45 * as MC have too many outstanding commands. Command will be retried once
46 * another command returns.
47 * @MCDI_STATE_RUNNING: Command was accepted and is running.
48 * @MCDI_STATE_RUNNING_CANCELLED: Command is running but the issuer cancelled
50 * @MCDI_STATE_FINISHED: Processing of this command has completed.
53 enum cdx_mcdi_cmd_state {
57 MCDI_STATE_RUNNING_CANCELLED,
62 * struct cdx_mcdi - CDX MCDI Firmware interface, to interact
63 * with CDX controller.
64 * @mcdi: MCDI interface
65 * @mcdi_ops: MCDI operations
66 * @r5_rproc : R5 Remoteproc device handle
67 * @rpdev: RPMsg device
68 * @ept: RPMsg endpoint
69 * @work: Post probe work
73 struct cdx_mcdi_data *mcdi;
74 const struct cdx_mcdi_ops *mcdi_ops;
76 struct rproc *r5_rproc;
77 struct rpmsg_device *rpdev;
78 struct rpmsg_endpoint *ept;
79 struct work_struct work;
83 void (*mcdi_request)(struct cdx_mcdi *cdx,
84 const struct cdx_dword *hdr, size_t hdr_len,
85 const struct cdx_dword *sdu, size_t sdu_len);
86 unsigned int (*mcdi_rpc_timeout)(struct cdx_mcdi *cdx, unsigned int cmd);
89 typedef void cdx_mcdi_async_completer(struct cdx_mcdi *cdx,
90 unsigned long cookie, int rc,
91 struct cdx_dword *outbuf,
92 size_t outlen_actual);
95 * struct cdx_mcdi_cmd - An outstanding MCDI command
96 * @ref: Reference count. There will be one reference if the command is
97 * in the mcdi_iface cmd_list, another if it's on a cleanup list,
98 * and a third if it's queued in the work queue.
99 * @list: The data for this entry in mcdi->cmd_list
100 * @cleanup_list: The data for this entry in a cleanup list
101 * @work: The work item for this command, queued in mcdi->workqueue
102 * @mcdi: The mcdi_iface for this command
103 * @state: The state of this command
104 * @inlen: inbuf length
105 * @inbuf: Input buffer
106 * @quiet: Whether to silence errors
107 * @reboot_seen: Whether a reboot has been seen during this command,
108 * to prevent duplicates
109 * @seq: Sequence number
110 * @started: Jiffies this command was started at
111 * @cookie: Context for completion function
112 * @completer: Completion function
113 * @handle: Command handle
114 * @cmd: Command number
116 * @outlen: Length of output buffer
117 * @outbuf: Output buffer
119 struct cdx_mcdi_cmd {
121 struct list_head list;
122 struct list_head cleanup_list;
123 struct work_struct work;
124 struct cdx_mcdi_iface *mcdi;
125 enum cdx_mcdi_cmd_state state;
127 const struct cdx_dword *inbuf;
131 unsigned long started;
132 unsigned long cookie;
133 cdx_mcdi_async_completer *completer;
138 struct cdx_dword *outbuf;
139 /* followed by inbuf data if necessary */
143 * struct cdx_mcdi_iface - MCDI protocol context
144 * @cdx: The associated NIC
145 * @iface_lock: Serialise access to this structure
146 * @outstanding_cleanups: Count of cleanups
147 * @cmd_list: List of outstanding and running commands
148 * @workqueue: Workqueue used for delayed processing
149 * @cmd_complete_wq: Waitqueue for command completion
150 * @db_held_by: Command the MC doorbell is in use by
151 * @seq_held_by: Command each sequence number is in use by
152 * @prev_handle: The last used command handle
153 * @mode: Poll for mcdi completion, or wait for an mcdi_event
154 * @prev_seq: The last used sequence number
155 * @new_epoch: Indicates start of day or start of MC reboot recovery
156 * @logging_buffer: Buffer that may be used to build MCDI tracing messages
157 * @logging_enabled: Whether to trace MCDI
159 struct cdx_mcdi_iface {
160 struct cdx_mcdi *cdx;
161 /* Serialise access */
162 struct mutex iface_lock;
163 unsigned int outstanding_cleanups;
164 struct list_head cmd_list;
165 struct workqueue_struct *workqueue;
166 wait_queue_head_t cmd_complete_wq;
167 struct cdx_mcdi_cmd *db_held_by;
168 struct cdx_mcdi_cmd *seq_held_by[16];
169 unsigned int prev_handle;
170 enum cdx_mcdi_mode mode;
173 #ifdef CONFIG_MCDI_LOGGING
174 bool logging_enabled;
175 char *logging_buffer;
180 * struct cdx_mcdi_data - extra state for NICs that implement MCDI
181 * @iface: Interface/protocol state
182 * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH.
184 struct cdx_mcdi_data {
185 struct cdx_mcdi_iface iface;
189 static inline struct cdx_mcdi_iface *cdx_mcdi_if(struct cdx_mcdi *cdx)
191 return cdx->mcdi ? &cdx->mcdi->iface : NULL;
194 int cdx_mcdi_init(struct cdx_mcdi *cdx);
195 void cdx_mcdi_finish(struct cdx_mcdi *cdx);
197 void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len);
198 int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
199 const struct cdx_dword *inbuf, size_t inlen,
200 struct cdx_dword *outbuf, size_t outlen, size_t *outlen_actual);
201 int cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
202 const struct cdx_dword *inbuf, size_t inlen,
203 cdx_mcdi_async_completer *complete,
204 unsigned long cookie);
205 int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
206 unsigned int timeout_jiffies);
209 * We expect that 16- and 32-bit fields in MCDI requests and responses
210 * are appropriately aligned, but 64-bit fields are only
213 #define MCDI_DECLARE_BUF(_name, _len) struct cdx_dword _name[DIV_ROUND_UP(_len, 4)] = {{0}}
214 #define _MCDI_PTR(_buf, _offset) \
215 ((u8 *)(_buf) + (_offset))
216 #define MCDI_PTR(_buf, _field) \
217 _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
218 #define _MCDI_CHECK_ALIGN(_ofst, _align) \
219 ((void)BUILD_BUG_ON_ZERO((_ofst) & ((_align) - 1)), \
221 #define _MCDI_DWORD(_buf, _field) \
222 ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
224 #define MCDI_BYTE(_buf, _field) \
225 ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 1), \
226 *MCDI_PTR(_buf, _field))
227 #define MCDI_WORD(_buf, _field) \
228 ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2), \
229 le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
230 #define MCDI_SET_DWORD(_buf, _field, _value) \
231 CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), CDX_DWORD, _value)
232 #define MCDI_DWORD(_buf, _field) \
233 CDX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), CDX_DWORD)
234 #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \
235 CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \
236 MC_CMD_ ## _name1, _value1)
237 #define MCDI_SET_QWORD(_buf, _field, _value) \
239 CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \
240 CDX_DWORD, (u32)(_value)); \
241 CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \
242 CDX_DWORD, (u64)(_value) >> 32); \
244 #define MCDI_QWORD(_buf, _field) \
245 (CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], CDX_DWORD) | \
246 (u64)CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], CDX_DWORD) << 32)
248 #endif /* CDX_MCDI_H */