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 /**
32 * enum efx_mcdi_mode - MCDI transaction mode
33 * @MCDI_MODE_POLL: poll for MCDI completion, until timeout
34 * @MCDI_MODE_EVENTS: wait for an mcdi_event. On timeout, poll once
35 * @MCDI_MODE_FAIL: we think MCDI is dead, so fail-fast all calls
36 */
37 enum efx_mcdi_mode {
38 MCDI_MODE_POLL,
39 MCDI_MODE_EVENTS,
40 MCDI_MODE_FAIL,
41 };
42
43 /**
44 * struct efx_mcdi_iface - MCDI protocol context
45 * @efx: The associated NIC.
46 * @state: Request handling state. Waited for by @wq.
47 * @mode: Poll for mcdi completion, or wait for an mcdi_event.
48 * @wq: Wait queue for threads waiting for @state != %MCDI_STATE_RUNNING
49 * @new_epoch: Indicates start of day or start of MC reboot recovery
50 * @iface_lock: Serialises access to @seqno, @credits and response metadata
51 * @seqno: The next sequence number to use for mcdi requests.
52 * @credits: Number of spurious MCDI completion events allowed before we
53 * trigger a fatal error
54 * @resprc: Response error/success code (Linux numbering)
55 * @resp_hdr_len: Response header length
56 * @resp_data_len: Response data (SDU or error) length
57 * @async_lock: Serialises access to @async_list while event processing is
58 * enabled
59 * @async_list: Queue of asynchronous requests
60 * @async_timer: Timer for asynchronous request timeout
61 * @logging_buffer: buffer that may be used to build MCDI tracing messages
62 * @logging_enabled: whether to trace MCDI
63 */
64 struct efx_mcdi_iface {
65 struct efx_nic *efx;
66 enum efx_mcdi_state state;
67 enum efx_mcdi_mode mode;
68 wait_queue_head_t wq;
69 spinlock_t iface_lock;
70 bool new_epoch;
71 unsigned int credits;
72 unsigned int seqno;
73 int resprc;
74 size_t resp_hdr_len;
75 size_t resp_data_len;
76 spinlock_t async_lock;
77 struct list_head async_list;
78 struct timer_list async_timer;
79 #ifdef CONFIG_SFC_MCDI_LOGGING
80 char *logging_buffer;
81 bool logging_enabled;
82 #endif
83 };
84
85 struct efx_mcdi_mon {
86 struct efx_buffer dma_buf;
87 struct mutex update_lock;
88 unsigned long last_update;
89 struct device *device;
90 struct efx_mcdi_mon_attribute *attrs;
91 struct attribute_group group;
92 const struct attribute_group *groups[2];
93 unsigned int n_attrs;
94 };
95
96 struct efx_mcdi_mtd_partition {
97 struct efx_mtd_partition common;
98 bool updating;
99 u16 nvram_type;
100 u16 fw_subtype;
101 };
102
103 #define to_efx_mcdi_mtd_partition(mtd) \
104 container_of(mtd, struct efx_mcdi_mtd_partition, common.mtd)
105
106 /**
107 * struct efx_mcdi_data - extra state for NICs that implement MCDI
108 * @iface: Interface/protocol state
109 * @hwmon: Hardware monitor state
110 * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH.
111 */
112 struct efx_mcdi_data {
113 struct efx_mcdi_iface iface;
114 #ifdef CONFIG_SFC_MCDI_MON
115 struct efx_mcdi_mon hwmon;
116 #endif
117 u32 fn_flags;
118 };
119
efx_mcdi(struct efx_nic * efx)120 static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
121 {
122 EFX_BUG_ON_PARANOID(!efx->mcdi);
123 return &efx->mcdi->iface;
124 }
125
126 #ifdef CONFIG_SFC_MCDI_MON
efx_mcdi_mon(struct efx_nic * efx)127 static inline struct efx_mcdi_mon *efx_mcdi_mon(struct efx_nic *efx)
128 {
129 EFX_BUG_ON_PARANOID(!efx->mcdi);
130 return &efx->mcdi->hwmon;
131 }
132 #endif
133
134 int efx_mcdi_init(struct efx_nic *efx);
135 void efx_mcdi_fini(struct efx_nic *efx);
136
137 int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd, const efx_dword_t *inbuf,
138 size_t inlen, efx_dword_t *outbuf, size_t outlen,
139 size_t *outlen_actual);
140 int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
141 const efx_dword_t *inbuf, size_t inlen,
142 efx_dword_t *outbuf, size_t outlen,
143 size_t *outlen_actual);
144
145 int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
146 const efx_dword_t *inbuf, size_t inlen);
147 int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
148 efx_dword_t *outbuf, size_t outlen,
149 size_t *outlen_actual);
150 int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd,
151 size_t inlen, efx_dword_t *outbuf,
152 size_t outlen, size_t *outlen_actual);
153
154 typedef void efx_mcdi_async_completer(struct efx_nic *efx,
155 unsigned long cookie, int rc,
156 efx_dword_t *outbuf,
157 size_t outlen_actual);
158 int efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
159 const efx_dword_t *inbuf, size_t inlen, size_t outlen,
160 efx_mcdi_async_completer *complete,
161 unsigned long cookie);
162 int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
163 const efx_dword_t *inbuf, size_t inlen,
164 size_t outlen,
165 efx_mcdi_async_completer *complete,
166 unsigned long cookie);
167
168 void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
169 size_t inlen, efx_dword_t *outbuf,
170 size_t outlen, int rc);
171
172 int efx_mcdi_poll_reboot(struct efx_nic *efx);
173 void efx_mcdi_mode_poll(struct efx_nic *efx);
174 void efx_mcdi_mode_event(struct efx_nic *efx);
175 void efx_mcdi_flush_async(struct efx_nic *efx);
176
177 void efx_mcdi_process_event(struct efx_channel *channel, efx_qword_t *event);
178 void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev);
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. Also, on Siena we must copy to the MC shared
183 * memory strictly 32 bits at a time, so add any necessary padding.
184 */
185 #define _MCDI_DECLARE_BUF(_name, _len) \
186 efx_dword_t _name[DIV_ROUND_UP(_len, 4)]
187 #define MCDI_DECLARE_BUF(_name, _len) \
188 _MCDI_DECLARE_BUF(_name, _len) = {{{0}}}
189 #define MCDI_DECLARE_BUF_ERR(_name) \
190 MCDI_DECLARE_BUF(_name, 8)
191 #define _MCDI_PTR(_buf, _offset) \
192 ((u8 *)(_buf) + (_offset))
193 #define MCDI_PTR(_buf, _field) \
194 _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
195 #define _MCDI_CHECK_ALIGN(_ofst, _align) \
196 ((_ofst) + BUILD_BUG_ON_ZERO((_ofst) & (_align - 1)))
197 #define _MCDI_DWORD(_buf, _field) \
198 ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
199
200 #define MCDI_WORD(_buf, _field) \
201 ((u16)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) + \
202 le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
203 #define MCDI_SET_DWORD(_buf, _field, _value) \
204 EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0, _value)
205 #define MCDI_DWORD(_buf, _field) \
206 EFX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0)
207 #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \
208 EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \
209 MC_CMD_ ## _name1, _value1)
210 #define MCDI_POPULATE_DWORD_2(_buf, _field, _name1, _value1, \
211 _name2, _value2) \
212 EFX_POPULATE_DWORD_2(*_MCDI_DWORD(_buf, _field), \
213 MC_CMD_ ## _name1, _value1, \
214 MC_CMD_ ## _name2, _value2)
215 #define MCDI_POPULATE_DWORD_3(_buf, _field, _name1, _value1, \
216 _name2, _value2, _name3, _value3) \
217 EFX_POPULATE_DWORD_3(*_MCDI_DWORD(_buf, _field), \
218 MC_CMD_ ## _name1, _value1, \
219 MC_CMD_ ## _name2, _value2, \
220 MC_CMD_ ## _name3, _value3)
221 #define MCDI_POPULATE_DWORD_4(_buf, _field, _name1, _value1, \
222 _name2, _value2, _name3, _value3, \
223 _name4, _value4) \
224 EFX_POPULATE_DWORD_4(*_MCDI_DWORD(_buf, _field), \
225 MC_CMD_ ## _name1, _value1, \
226 MC_CMD_ ## _name2, _value2, \
227 MC_CMD_ ## _name3, _value3, \
228 MC_CMD_ ## _name4, _value4)
229 #define MCDI_POPULATE_DWORD_5(_buf, _field, _name1, _value1, \
230 _name2, _value2, _name3, _value3, \
231 _name4, _value4, _name5, _value5) \
232 EFX_POPULATE_DWORD_5(*_MCDI_DWORD(_buf, _field), \
233 MC_CMD_ ## _name1, _value1, \
234 MC_CMD_ ## _name2, _value2, \
235 MC_CMD_ ## _name3, _value3, \
236 MC_CMD_ ## _name4, _value4, \
237 MC_CMD_ ## _name5, _value5)
238 #define MCDI_POPULATE_DWORD_6(_buf, _field, _name1, _value1, \
239 _name2, _value2, _name3, _value3, \
240 _name4, _value4, _name5, _value5, \
241 _name6, _value6) \
242 EFX_POPULATE_DWORD_6(*_MCDI_DWORD(_buf, _field), \
243 MC_CMD_ ## _name1, _value1, \
244 MC_CMD_ ## _name2, _value2, \
245 MC_CMD_ ## _name3, _value3, \
246 MC_CMD_ ## _name4, _value4, \
247 MC_CMD_ ## _name5, _value5, \
248 MC_CMD_ ## _name6, _value6)
249 #define MCDI_POPULATE_DWORD_7(_buf, _field, _name1, _value1, \
250 _name2, _value2, _name3, _value3, \
251 _name4, _value4, _name5, _value5, \
252 _name6, _value6, _name7, _value7) \
253 EFX_POPULATE_DWORD_7(*_MCDI_DWORD(_buf, _field), \
254 MC_CMD_ ## _name1, _value1, \
255 MC_CMD_ ## _name2, _value2, \
256 MC_CMD_ ## _name3, _value3, \
257 MC_CMD_ ## _name4, _value4, \
258 MC_CMD_ ## _name5, _value5, \
259 MC_CMD_ ## _name6, _value6, \
260 MC_CMD_ ## _name7, _value7)
261 #define MCDI_SET_QWORD(_buf, _field, _value) \
262 do { \
263 EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \
264 EFX_DWORD_0, (u32)(_value)); \
265 EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \
266 EFX_DWORD_0, (u64)(_value) >> 32); \
267 } while (0)
268 #define MCDI_QWORD(_buf, _field) \
269 (EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], EFX_DWORD_0) | \
270 (u64)EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], EFX_DWORD_0) << 32)
271 #define MCDI_FIELD(_ptr, _type, _field) \
272 EFX_EXTRACT_DWORD( \
273 *(efx_dword_t *) \
274 _MCDI_PTR(_ptr, MC_CMD_ ## _type ## _ ## _field ## _OFST & ~3),\
275 MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f, \
276 (MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f) + \
277 MC_CMD_ ## _type ## _ ## _field ## _WIDTH - 1)
278
279 #define _MCDI_ARRAY_PTR(_buf, _field, _index, _align) \
280 (_MCDI_PTR(_buf, _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, _align))\
281 + (_index) * _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _LEN, _align))
282 #define MCDI_DECLARE_STRUCT_PTR(_name) \
283 efx_dword_t *_name
284 #define MCDI_ARRAY_STRUCT_PTR(_buf, _field, _index) \
285 ((efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
286 #define MCDI_VAR_ARRAY_LEN(_len, _field) \
287 min_t(size_t, MC_CMD_ ## _field ## _MAXNUM, \
288 ((_len) - MC_CMD_ ## _field ## _OFST) / MC_CMD_ ## _field ## _LEN)
289 #define MCDI_ARRAY_WORD(_buf, _field, _index) \
290 (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) + \
291 le16_to_cpu(*(__force const __le16 *) \
292 _MCDI_ARRAY_PTR(_buf, _field, _index, 2)))
293 #define _MCDI_ARRAY_DWORD(_buf, _field, _index) \
294 (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 4) + \
295 (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
296 #define MCDI_SET_ARRAY_DWORD(_buf, _field, _index, _value) \
297 EFX_SET_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index), \
298 EFX_DWORD_0, _value)
299 #define MCDI_ARRAY_DWORD(_buf, _field, _index) \
300 EFX_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index), EFX_DWORD_0)
301 #define _MCDI_ARRAY_QWORD(_buf, _field, _index) \
302 (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 8) + \
303 (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
304 #define MCDI_SET_ARRAY_QWORD(_buf, _field, _index, _value) \
305 do { \
306 EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[0],\
307 EFX_DWORD_0, (u32)(_value)); \
308 EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[1],\
309 EFX_DWORD_0, (u64)(_value) >> 32); \
310 } while (0)
311 #define MCDI_ARRAY_FIELD(_buf, _field1, _type, _index, _field2) \
312 MCDI_FIELD(MCDI_ARRAY_STRUCT_PTR(_buf, _field1, _index), \
313 _type ## _TYPEDEF, _field2)
314
315 #define MCDI_EVENT_FIELD(_ev, _field) \
316 EFX_QWORD_FIELD(_ev, MCDI_EVENT_ ## _field)
317
318 void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len);
319 int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
320 u16 *fw_subtype_list, u32 *capabilities);
321 int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq);
322 int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out);
323 int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
324 size_t *size_out, size_t *erase_size_out,
325 bool *protected_out);
326 int efx_mcdi_nvram_test_all(struct efx_nic *efx);
327 int efx_mcdi_handle_assertion(struct efx_nic *efx);
328 void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode);
329 int efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac,
330 int *id_out);
331 int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out);
332 int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id);
333 int efx_mcdi_wol_filter_reset(struct efx_nic *efx);
334 int efx_mcdi_flush_rxqs(struct efx_nic *efx);
335 int efx_mcdi_port_probe(struct efx_nic *efx);
336 void efx_mcdi_port_remove(struct efx_nic *efx);
337 int efx_mcdi_port_reconfigure(struct efx_nic *efx);
338 int efx_mcdi_port_get_number(struct efx_nic *efx);
339 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx);
340 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev);
341 int efx_mcdi_set_mac(struct efx_nic *efx);
342 #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
343 void efx_mcdi_mac_start_stats(struct efx_nic *efx);
344 void efx_mcdi_mac_stop_stats(struct efx_nic *efx);
345 void efx_mcdi_mac_pull_stats(struct efx_nic *efx);
346 bool efx_mcdi_mac_check_fault(struct efx_nic *efx);
347 enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason);
348 int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method);
349 int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled,
350 unsigned int *flags);
351 int efx_mcdi_get_workarounds(struct efx_nic *efx, unsigned int *impl_out,
352 unsigned int *enabled_out);
353
354 #ifdef CONFIG_SFC_MCDI_MON
355 int efx_mcdi_mon_probe(struct efx_nic *efx);
356 void efx_mcdi_mon_remove(struct efx_nic *efx);
357 #else
efx_mcdi_mon_probe(struct efx_nic * efx)358 static inline int efx_mcdi_mon_probe(struct efx_nic *efx) { return 0; }
efx_mcdi_mon_remove(struct efx_nic * efx)359 static inline void efx_mcdi_mon_remove(struct efx_nic *efx) {}
360 #endif
361
362 #ifdef CONFIG_SFC_MTD
363 int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start, size_t len,
364 size_t *retlen, u8 *buffer);
365 int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len);
366 int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start, size_t len,
367 size_t *retlen, const u8 *buffer);
368 int efx_mcdi_mtd_sync(struct mtd_info *mtd);
369 void efx_mcdi_mtd_rename(struct efx_mtd_partition *part);
370 #endif
371
372 #endif /* EFX_MCDI_H */
373