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