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
157 struct cdx_mcdi_iface
{
158 struct cdx_mcdi
*cdx
;
159 /* Serialise access */
160 struct mutex iface_lock
;
161 unsigned int outstanding_cleanups
;
162 struct list_head cmd_list
;
163 struct workqueue_struct
*workqueue
;
164 wait_queue_head_t cmd_complete_wq
;
165 struct cdx_mcdi_cmd
*db_held_by
;
166 struct cdx_mcdi_cmd
*seq_held_by
[16];
167 unsigned int prev_handle
;
168 enum cdx_mcdi_mode mode
;
174 * struct cdx_mcdi_data - extra state for NICs that implement MCDI
175 * @iface: Interface/protocol state
176 * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH.
178 struct cdx_mcdi_data
{
179 struct cdx_mcdi_iface iface
;
183 static inline struct cdx_mcdi_iface
*cdx_mcdi_if(struct cdx_mcdi
*cdx
)
185 return cdx
->mcdi
? &cdx
->mcdi
->iface
: NULL
;
188 int cdx_mcdi_init(struct cdx_mcdi
*cdx
);
189 void cdx_mcdi_finish(struct cdx_mcdi
*cdx
);
191 void cdx_mcdi_process_cmd(struct cdx_mcdi
*cdx
, struct cdx_dword
*outbuf
, int len
);
192 int cdx_mcdi_rpc(struct cdx_mcdi
*cdx
, unsigned int cmd
,
193 const struct cdx_dword
*inbuf
, size_t inlen
,
194 struct cdx_dword
*outbuf
, size_t outlen
, size_t *outlen_actual
);
195 int cdx_mcdi_rpc_async(struct cdx_mcdi
*cdx
, unsigned int cmd
,
196 const struct cdx_dword
*inbuf
, size_t inlen
,
197 cdx_mcdi_async_completer
*complete
,
198 unsigned long cookie
);
199 int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi
*cdx
,
200 unsigned int timeout_jiffies
);
203 * We expect that 16- and 32-bit fields in MCDI requests and responses
204 * are appropriately aligned, but 64-bit fields are only
207 #define MCDI_DECLARE_BUF(_name, _len) struct cdx_dword _name[DIV_ROUND_UP(_len, 4)] = {{0}}
208 #define _MCDI_PTR(_buf, _offset) \
209 ((u8 *)(_buf) + (_offset))
210 #define MCDI_PTR(_buf, _field) \
211 _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
212 #define _MCDI_CHECK_ALIGN(_ofst, _align) \
213 ((void)BUILD_BUG_ON_ZERO((_ofst) & ((_align) - 1)), \
215 #define _MCDI_DWORD(_buf, _field) \
216 ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
218 #define MCDI_BYTE(_buf, _field) \
219 ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 1), \
220 *MCDI_PTR(_buf, _field))
221 #define MCDI_WORD(_buf, _field) \
222 ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2), \
223 le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
224 #define MCDI_SET_DWORD(_buf, _field, _value) \
225 CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), CDX_DWORD, _value)
226 #define MCDI_DWORD(_buf, _field) \
227 CDX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), CDX_DWORD)
228 #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \
229 CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \
230 MC_CMD_ ## _name1, _value1)
231 #define MCDI_SET_QWORD(_buf, _field, _value) \
233 CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \
234 CDX_DWORD, (u32)(_value)); \
235 CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \
236 CDX_DWORD, (u64)(_value) >> 32); \
238 #define MCDI_QWORD(_buf, _field) \
239 (CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], CDX_DWORD) | \
240 (u64)CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], CDX_DWORD) << 32)
242 #endif /* CDX_MCDI_H */