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