xref: /openbmc/linux/drivers/cdx/controller/mcdi.h (revision b1c8ea3c)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright 2008-2013 Solarflare Communications Inc.
4  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
5  */
6 
7 #ifndef CDX_MCDI_H
8 #define CDX_MCDI_H
9 
10 #include <linux/mutex.h>
11 #include <linux/kref.h>
12 #include <linux/rpmsg.h>
13 
14 #include "bitfield.h"
15 #include "mc_cdx_pcol.h"
16 
17 #ifdef DEBUG
18 #define CDX_WARN_ON_ONCE_PARANOID(x) WARN_ON_ONCE(x)
19 #define CDX_WARN_ON_PARANOID(x) WARN_ON(x)
20 #else
21 #define CDX_WARN_ON_ONCE_PARANOID(x) do {} while (0)
22 #define CDX_WARN_ON_PARANOID(x) do {} while (0)
23 #endif
24 
25 /**
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
29  */
30 enum cdx_mcdi_mode {
31 	MCDI_MODE_EVENTS,
32 	MCDI_MODE_FAIL,
33 };
34 
35 #define MCDI_RPC_TIMEOUT	(10 * HZ)
36 #define MCDI_RPC_LONG_TIMEOU	(60 * HZ)
37 #define MCDI_RPC_POST_RST_TIME	(10 * HZ)
38 
39 #define MCDI_BUF_LEN (8 + MCDI_CTL_SDU_LEN_MAX)
40 
41 /**
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
49  *	the command.
50  * @MCDI_STATE_FINISHED: Processing of this command has completed.
51  */
52 
53 enum cdx_mcdi_cmd_state {
54 	MCDI_STATE_QUEUED,
55 	MCDI_STATE_RETRY,
56 	MCDI_STATE_RUNNING,
57 	MCDI_STATE_RUNNING_CANCELLED,
58 	MCDI_STATE_FINISHED,
59 };
60 
61 /**
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
70  */
71 struct cdx_mcdi {
72 	/* MCDI interface */
73 	struct cdx_mcdi_data *mcdi;
74 	const struct cdx_mcdi_ops *mcdi_ops;
75 
76 	struct rproc *r5_rproc;
77 	struct rpmsg_device *rpdev;
78 	struct rpmsg_endpoint *ept;
79 	struct work_struct work;
80 };
81 
82 struct cdx_mcdi_ops {
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);
87 };
88 
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);
93 
94 /**
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
115  * @rc: Return code
116  * @outlen: Length of output buffer
117  * @outbuf: Output buffer
118  */
119 struct cdx_mcdi_cmd {
120 	struct kref ref;
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;
126 	size_t inlen;
127 	const struct cdx_dword *inbuf;
128 	bool quiet;
129 	bool reboot_seen;
130 	u8 seq;
131 	unsigned long started;
132 	unsigned long cookie;
133 	cdx_mcdi_async_completer *completer;
134 	unsigned int handle;
135 	unsigned int cmd;
136 	int rc;
137 	size_t outlen;
138 	struct cdx_dword *outbuf;
139 	/* followed by inbuf data if necessary */
140 };
141 
142 /**
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  */
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;
169 	u8 prev_seq;
170 	bool new_epoch;
171 };
172 
173 /**
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.
177  */
178 struct cdx_mcdi_data {
179 	struct cdx_mcdi_iface iface;
180 	u32 fn_flags;
181 };
182 
cdx_mcdi_if(struct cdx_mcdi * cdx)183 static inline struct cdx_mcdi_iface *cdx_mcdi_if(struct cdx_mcdi *cdx)
184 {
185 	return cdx->mcdi ? &cdx->mcdi->iface : NULL;
186 }
187 
188 int cdx_mcdi_init(struct cdx_mcdi *cdx);
189 void cdx_mcdi_finish(struct cdx_mcdi *cdx);
190 
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);
201 
202 /*
203  * We expect that 16- and 32-bit fields in MCDI requests and responses
204  * are appropriately aligned, but 64-bit fields are only
205  * 32-bit-aligned.
206  */
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)),		\
214 	 (_ofst))
215 #define _MCDI_DWORD(_buf, _field)					\
216 	((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
217 
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)				\
232 	do {								\
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);	\
237 	} while (0)
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)
241 
242 #endif /* CDX_MCDI_H */
243