xref: /openbmc/linux/drivers/net/ethernet/sfc/mcdi.h (revision 089a49b6)
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2008-2013 Solarflare Communications Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, incorporated herein by reference.
8  */
9 
10 #ifndef EFX_MCDI_H
11 #define EFX_MCDI_H
12 
13 /**
14  * enum efx_mcdi_state - MCDI request handling state
15  * @MCDI_STATE_QUIESCENT: No pending MCDI requests. If the caller holds the
16  *	mcdi @iface_lock then they are able to move to %MCDI_STATE_RUNNING
17  * @MCDI_STATE_RUNNING_SYNC: There is a synchronous MCDI request pending.
18  *	Only the thread that moved into this state is allowed to move out of it.
19  * @MCDI_STATE_RUNNING_ASYNC: There is an asynchronous MCDI request pending.
20  * @MCDI_STATE_COMPLETED: An MCDI request has completed, but the owning thread
21  *	has not yet consumed the result. For all other threads, equivalent to
22  *	%MCDI_STATE_RUNNING.
23  */
24 enum efx_mcdi_state {
25 	MCDI_STATE_QUIESCENT,
26 	MCDI_STATE_RUNNING_SYNC,
27 	MCDI_STATE_RUNNING_ASYNC,
28 	MCDI_STATE_COMPLETED,
29 };
30 
31 enum efx_mcdi_mode {
32 	MCDI_MODE_POLL,
33 	MCDI_MODE_EVENTS,
34 };
35 
36 /**
37  * struct efx_mcdi_iface - MCDI protocol context
38  * @efx: The associated NIC.
39  * @state: Request handling state. Waited for by @wq.
40  * @mode: Poll for mcdi completion, or wait for an mcdi_event.
41  * @wq: Wait queue for threads waiting for @state != %MCDI_STATE_RUNNING
42  * @new_epoch: Indicates start of day or start of MC reboot recovery
43  * @iface_lock: Serialises access to @seqno, @credits and response metadata
44  * @seqno: The next sequence number to use for mcdi requests.
45  * @credits: Number of spurious MCDI completion events allowed before we
46  *     trigger a fatal error
47  * @resprc: Response error/success code (Linux numbering)
48  * @resp_hdr_len: Response header length
49  * @resp_data_len: Response data (SDU or error) length
50  * @async_lock: Serialises access to @async_list while event processing is
51  *	enabled
52  * @async_list: Queue of asynchronous requests
53  * @async_timer: Timer for asynchronous request timeout
54  */
55 struct efx_mcdi_iface {
56 	struct efx_nic *efx;
57 	enum efx_mcdi_state state;
58 	enum efx_mcdi_mode mode;
59 	wait_queue_head_t wq;
60 	spinlock_t iface_lock;
61 	bool new_epoch;
62 	unsigned int credits;
63 	unsigned int seqno;
64 	int resprc;
65 	size_t resp_hdr_len;
66 	size_t resp_data_len;
67 	spinlock_t async_lock;
68 	struct list_head async_list;
69 	struct timer_list async_timer;
70 };
71 
72 struct efx_mcdi_mon {
73 	struct efx_buffer dma_buf;
74 	struct mutex update_lock;
75 	unsigned long last_update;
76 	struct device *device;
77 	struct efx_mcdi_mon_attribute *attrs;
78 	unsigned int n_attrs;
79 };
80 
81 struct efx_mcdi_mtd_partition {
82 	struct efx_mtd_partition common;
83 	bool updating;
84 	u16 nvram_type;
85 	u16 fw_subtype;
86 };
87 
88 #define to_efx_mcdi_mtd_partition(mtd)				\
89 	container_of(mtd, struct efx_mcdi_mtd_partition, common.mtd)
90 
91 /**
92  * struct efx_mcdi_data - extra state for NICs that implement MCDI
93  * @iface: Interface/protocol state
94  * @hwmon: Hardware monitor state
95  */
96 struct efx_mcdi_data {
97 	struct efx_mcdi_iface iface;
98 #ifdef CONFIG_SFC_MCDI_MON
99 	struct efx_mcdi_mon hwmon;
100 #endif
101 };
102 
103 #ifdef CONFIG_SFC_MCDI_MON
104 static inline struct efx_mcdi_mon *efx_mcdi_mon(struct efx_nic *efx)
105 {
106 	EFX_BUG_ON_PARANOID(!efx->mcdi);
107 	return &efx->mcdi->hwmon;
108 }
109 #endif
110 
111 extern int efx_mcdi_init(struct efx_nic *efx);
112 extern void efx_mcdi_fini(struct efx_nic *efx);
113 
114 extern int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
115 			const efx_dword_t *inbuf, size_t inlen,
116 			efx_dword_t *outbuf, size_t outlen,
117 			size_t *outlen_actual);
118 
119 extern int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
120 			      const efx_dword_t *inbuf, size_t inlen);
121 extern int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
122 			       efx_dword_t *outbuf, size_t outlen,
123 			       size_t *outlen_actual);
124 
125 typedef void efx_mcdi_async_completer(struct efx_nic *efx,
126 				      unsigned long cookie, int rc,
127 				      efx_dword_t *outbuf,
128 				      size_t outlen_actual);
129 extern int efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
130 			      const efx_dword_t *inbuf, size_t inlen,
131 			      size_t outlen,
132 			      efx_mcdi_async_completer *complete,
133 			      unsigned long cookie);
134 
135 extern int efx_mcdi_poll_reboot(struct efx_nic *efx);
136 extern void efx_mcdi_mode_poll(struct efx_nic *efx);
137 extern void efx_mcdi_mode_event(struct efx_nic *efx);
138 extern void efx_mcdi_flush_async(struct efx_nic *efx);
139 
140 extern void efx_mcdi_process_event(struct efx_channel *channel,
141 				   efx_qword_t *event);
142 extern void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev);
143 
144 /* We expect that 16- and 32-bit fields in MCDI requests and responses
145  * are appropriately aligned, but 64-bit fields are only
146  * 32-bit-aligned.  Also, on Siena we must copy to the MC shared
147  * memory strictly 32 bits at a time, so add any necessary padding.
148  */
149 #define MCDI_DECLARE_BUF(_name, _len)					\
150 	efx_dword_t _name[DIV_ROUND_UP(_len, 4)]
151 #define _MCDI_PTR(_buf, _offset)					\
152 	((u8 *)(_buf) + (_offset))
153 #define MCDI_PTR(_buf, _field)						\
154 	_MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
155 #define _MCDI_CHECK_ALIGN(_ofst, _align)				\
156 	((_ofst) + BUILD_BUG_ON_ZERO((_ofst) & (_align - 1)))
157 #define _MCDI_DWORD(_buf, _field)					\
158 	((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
159 
160 #define MCDI_WORD(_buf, _field)						\
161 	((u16)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) +	\
162 	 le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
163 #define MCDI_SET_DWORD(_buf, _field, _value)				\
164 	EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0, _value)
165 #define MCDI_DWORD(_buf, _field)					\
166 	EFX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0)
167 #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1)		\
168 	EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field),		\
169 			     MC_CMD_ ## _name1, _value1)
170 #define MCDI_POPULATE_DWORD_2(_buf, _field, _name1, _value1,		\
171 			      _name2, _value2)				\
172 	EFX_POPULATE_DWORD_2(*_MCDI_DWORD(_buf, _field),		\
173 			     MC_CMD_ ## _name1, _value1,		\
174 			     MC_CMD_ ## _name2, _value2)
175 #define MCDI_POPULATE_DWORD_3(_buf, _field, _name1, _value1,		\
176 			      _name2, _value2, _name3, _value3)		\
177 	EFX_POPULATE_DWORD_3(*_MCDI_DWORD(_buf, _field),		\
178 			     MC_CMD_ ## _name1, _value1,		\
179 			     MC_CMD_ ## _name2, _value2,		\
180 			     MC_CMD_ ## _name3, _value3)
181 #define MCDI_POPULATE_DWORD_4(_buf, _field, _name1, _value1,		\
182 			      _name2, _value2, _name3, _value3,		\
183 			      _name4, _value4)				\
184 	EFX_POPULATE_DWORD_4(*_MCDI_DWORD(_buf, _field),		\
185 			     MC_CMD_ ## _name1, _value1,		\
186 			     MC_CMD_ ## _name2, _value2,		\
187 			     MC_CMD_ ## _name3, _value3,		\
188 			     MC_CMD_ ## _name4, _value4)
189 #define MCDI_POPULATE_DWORD_5(_buf, _field, _name1, _value1,		\
190 			      _name2, _value2, _name3, _value3,		\
191 			      _name4, _value4, _name5, _value5)		\
192 	EFX_POPULATE_DWORD_5(*_MCDI_DWORD(_buf, _field),		\
193 			     MC_CMD_ ## _name1, _value1,		\
194 			     MC_CMD_ ## _name2, _value2,		\
195 			     MC_CMD_ ## _name3, _value3,		\
196 			     MC_CMD_ ## _name4, _value4,		\
197 			     MC_CMD_ ## _name5, _value5)
198 #define MCDI_POPULATE_DWORD_6(_buf, _field, _name1, _value1,		\
199 			      _name2, _value2, _name3, _value3,		\
200 			      _name4, _value4, _name5, _value5,		\
201 			      _name6, _value6)				\
202 	EFX_POPULATE_DWORD_6(*_MCDI_DWORD(_buf, _field),		\
203 			     MC_CMD_ ## _name1, _value1,		\
204 			     MC_CMD_ ## _name2, _value2,		\
205 			     MC_CMD_ ## _name3, _value3,		\
206 			     MC_CMD_ ## _name4, _value4,		\
207 			     MC_CMD_ ## _name5, _value5,		\
208 			     MC_CMD_ ## _name6, _value6)
209 #define MCDI_POPULATE_DWORD_7(_buf, _field, _name1, _value1,		\
210 			      _name2, _value2, _name3, _value3,		\
211 			      _name4, _value4, _name5, _value5,		\
212 			      _name6, _value6, _name7, _value7)		\
213 	EFX_POPULATE_DWORD_7(*_MCDI_DWORD(_buf, _field),		\
214 			     MC_CMD_ ## _name1, _value1,		\
215 			     MC_CMD_ ## _name2, _value2,		\
216 			     MC_CMD_ ## _name3, _value3,		\
217 			     MC_CMD_ ## _name4, _value4,		\
218 			     MC_CMD_ ## _name5, _value5,		\
219 			     MC_CMD_ ## _name6, _value6,		\
220 			     MC_CMD_ ## _name7, _value7)
221 #define MCDI_SET_QWORD(_buf, _field, _value)				\
222 	do {								\
223 		EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0],	\
224 				     EFX_DWORD_0, (u32)(_value));	\
225 		EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1],	\
226 				     EFX_DWORD_0, (u64)(_value) >> 32);	\
227 	} while (0)
228 #define MCDI_QWORD(_buf, _field)					\
229 	(EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], EFX_DWORD_0) |	\
230 	(u64)EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], EFX_DWORD_0) << 32)
231 #define MCDI_FIELD(_ptr, _type, _field)					\
232 	EFX_EXTRACT_DWORD(						\
233 		*(efx_dword_t *)					\
234 		_MCDI_PTR(_ptr, MC_CMD_ ## _type ## _ ## _field ## _OFST & ~3),\
235 		MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f,	\
236 		(MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f) +	\
237 		MC_CMD_ ## _type ## _ ## _field ## _WIDTH - 1)
238 
239 #define _MCDI_ARRAY_PTR(_buf, _field, _index, _align)			\
240 	(_MCDI_PTR(_buf, _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, _align))\
241 	 + (_index) * _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _LEN, _align))
242 #define MCDI_DECLARE_STRUCT_PTR(_name)					\
243 	efx_dword_t *_name
244 #define MCDI_ARRAY_STRUCT_PTR(_buf, _field, _index)			\
245 	((efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
246 #define MCDI_VAR_ARRAY_LEN(_len, _field)				\
247 	min_t(size_t, MC_CMD_ ## _field ## _MAXNUM,			\
248 	      ((_len) - MC_CMD_ ## _field ## _OFST) / MC_CMD_ ## _field ## _LEN)
249 #define MCDI_ARRAY_WORD(_buf, _field, _index)				\
250 	(BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) +		\
251 	 le16_to_cpu(*(__force const __le16 *)				\
252 		     _MCDI_ARRAY_PTR(_buf, _field, _index, 2)))
253 #define _MCDI_ARRAY_DWORD(_buf, _field, _index)				\
254 	(BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 4) +		\
255 	 (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
256 #define MCDI_SET_ARRAY_DWORD(_buf, _field, _index, _value)		\
257 	EFX_SET_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index),	\
258 			    EFX_DWORD_0, _value)
259 #define MCDI_ARRAY_DWORD(_buf, _field, _index)				\
260 	EFX_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index), EFX_DWORD_0)
261 #define _MCDI_ARRAY_QWORD(_buf, _field, _index)				\
262 	(BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 8) +		\
263 	 (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
264 #define MCDI_SET_ARRAY_QWORD(_buf, _field, _index, _value)		\
265 	do {								\
266 		EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[0],\
267 				    EFX_DWORD_0, (u32)(_value));	\
268 		EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[1],\
269 				    EFX_DWORD_0, (u64)(_value) >> 32);	\
270 	} while (0)
271 #define MCDI_ARRAY_FIELD(_buf, _field1, _type, _index, _field2)		\
272 	MCDI_FIELD(MCDI_ARRAY_STRUCT_PTR(_buf, _field1, _index),	\
273 		   _type ## _TYPEDEF, _field2)
274 
275 #define MCDI_EVENT_FIELD(_ev, _field)			\
276 	EFX_QWORD_FIELD(_ev, MCDI_EVENT_ ## _field)
277 
278 extern void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len);
279 extern int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
280 				  u16 *fw_subtype_list, u32 *capabilities);
281 extern int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart,
282 			     u32 dest_evq);
283 extern int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out);
284 extern int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
285 			       size_t *size_out, size_t *erase_size_out,
286 			       bool *protected_out);
287 extern int efx_mcdi_nvram_test_all(struct efx_nic *efx);
288 extern int efx_mcdi_handle_assertion(struct efx_nic *efx);
289 extern void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode);
290 extern int efx_mcdi_wol_filter_set_magic(struct efx_nic *efx,
291 					 const u8 *mac, int *id_out);
292 extern int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out);
293 extern int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id);
294 extern int efx_mcdi_wol_filter_reset(struct efx_nic *efx);
295 extern int efx_mcdi_flush_rxqs(struct efx_nic *efx);
296 extern int efx_mcdi_port_probe(struct efx_nic *efx);
297 extern void efx_mcdi_port_remove(struct efx_nic *efx);
298 extern int efx_mcdi_port_reconfigure(struct efx_nic *efx);
299 extern int efx_mcdi_port_get_number(struct efx_nic *efx);
300 extern u32 efx_mcdi_phy_get_caps(struct efx_nic *efx);
301 extern void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev);
302 extern int efx_mcdi_set_mac(struct efx_nic *efx);
303 #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
304 extern void efx_mcdi_mac_start_stats(struct efx_nic *efx);
305 extern void efx_mcdi_mac_stop_stats(struct efx_nic *efx);
306 extern bool efx_mcdi_mac_check_fault(struct efx_nic *efx);
307 extern enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason);
308 extern int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method);
309 extern int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled);
310 
311 #ifdef CONFIG_SFC_MCDI_MON
312 extern int efx_mcdi_mon_probe(struct efx_nic *efx);
313 extern void efx_mcdi_mon_remove(struct efx_nic *efx);
314 #else
315 static inline int efx_mcdi_mon_probe(struct efx_nic *efx) { return 0; }
316 static inline void efx_mcdi_mon_remove(struct efx_nic *efx) {}
317 #endif
318 
319 #ifdef CONFIG_SFC_MTD
320 extern int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
321 			     size_t len, size_t *retlen, u8 *buffer);
322 extern int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len);
323 extern int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
324 			      size_t len, size_t *retlen, const u8 *buffer);
325 extern int efx_mcdi_mtd_sync(struct mtd_info *mtd);
326 extern void efx_mcdi_mtd_rename(struct efx_mtd_partition *part);
327 #endif
328 
329 #endif /* EFX_MCDI_H */
330