blob: 4f3301d84f64dc02b08bedf1c0e86d26b82d4b6b [file] [log] [blame]
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00003 * Copyright 2008-2011 Solarflare Communications Inc.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00004 *
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#include <linux/delay.h>
11#include "net_driver.h"
12#include "nic.h"
13#include "io.h"
Ben Hutchings8b8a95a2012-09-18 01:57:07 +010014#include "farch_regs.h"
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000015#include "mcdi_pcol.h"
16#include "phy.h"
17
18/**************************************************************************
19 *
20 * Management-Controller-to-Driver Interface
21 *
22 **************************************************************************
23 */
24
Ben Hutchingsebf98e72012-12-01 02:21:17 +000025#define MCDI_RPC_TIMEOUT (10 * HZ)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000026
Ben Hutchings3f713bf2011-12-20 23:39:31 +000027/* A reboot/assertion causes the MCDI status word to be set after the
28 * command word is set or a REBOOT event is sent. If we notice a reboot
29 * via these mechanisms then wait 10ms for the status word to be set. */
30#define MCDI_STATUS_DELAY_US 100
31#define MCDI_STATUS_DELAY_COUNT 100
32#define MCDI_STATUS_SLEEP_MS \
33 (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000034
35#define SEQ_MASK \
36 EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
37
38static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
39{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010040 EFX_BUG_ON_PARANOID(!efx->mcdi);
41 return &efx->mcdi->iface;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000042}
43
Ben Hutchingsf073dde2012-09-18 02:33:55 +010044int efx_mcdi_init(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000045{
46 struct efx_mcdi_iface *mcdi;
47
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010048 efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
49 if (!efx->mcdi)
50 return -ENOMEM;
51
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000052 mcdi = efx_mcdi(efx);
53 init_waitqueue_head(&mcdi->wq);
54 spin_lock_init(&mcdi->iface_lock);
55 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
56 mcdi->mode = MCDI_MODE_POLL;
57
58 (void) efx_mcdi_poll_reboot(efx);
Ben Hutchingsf073dde2012-09-18 02:33:55 +010059
60 /* Recover from a failed assertion before probing */
61 return efx_mcdi_handle_assertion(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000062}
63
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010064void efx_mcdi_fini(struct efx_nic *efx)
65{
66 BUG_ON(efx->mcdi &&
67 atomic_read(&efx->mcdi->iface.state) != MCDI_STATE_QUIESCENT);
68 kfree(efx->mcdi);
69}
70
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000071static void efx_mcdi_copyin(struct efx_nic *efx, unsigned cmd,
Ben Hutchings9528b922012-09-14 17:31:41 +010072 const efx_dword_t *inbuf, size_t inlen)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000073{
74 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000075 efx_dword_t hdr;
76 u32 xflags, seqno;
77
78 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
Ben Hutchings9528b922012-09-14 17:31:41 +010079 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000080
81 seqno = mcdi->seqno & SEQ_MASK;
82 xflags = 0;
83 if (mcdi->mode == MCDI_MODE_EVENTS)
84 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
85
86 EFX_POPULATE_DWORD_6(hdr,
87 MCDI_HEADER_RESPONSE, 0,
88 MCDI_HEADER_RESYNC, 1,
89 MCDI_HEADER_CODE, cmd,
90 MCDI_HEADER_DATALEN, inlen,
91 MCDI_HEADER_SEQ, seqno,
92 MCDI_HEADER_XFLAGS, xflags);
93
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010094 efx->type->mcdi_request(efx, &hdr, 4, inbuf, inlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000095}
96
Ben Hutchings9528b922012-09-14 17:31:41 +010097static void
98efx_mcdi_copyout(struct efx_nic *efx, efx_dword_t *outbuf, size_t outlen)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000099{
100 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000101
102 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
Ben Hutchings9528b922012-09-14 17:31:41 +0100103 BUG_ON(outlen > MCDI_CTL_SDU_LEN_MAX_V1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000104
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100105 efx->type->mcdi_read_response(efx, outbuf, 4, outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000106}
107
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100108static int efx_mcdi_errno(unsigned int mcdi_err)
109{
110 switch (mcdi_err) {
111 case 0:
112 return 0;
113#define TRANSLATE_ERROR(name) \
114 case MC_CMD_ERR_ ## name: \
115 return -name;
116 TRANSLATE_ERROR(ENOENT);
117 TRANSLATE_ERROR(EINTR);
118 TRANSLATE_ERROR(EACCES);
119 TRANSLATE_ERROR(EBUSY);
120 TRANSLATE_ERROR(EINVAL);
121 TRANSLATE_ERROR(EDEADLK);
122 TRANSLATE_ERROR(ENOSYS);
123 TRANSLATE_ERROR(ETIME);
124#undef TRANSLATE_ERROR
125 default:
126 return -EIO;
127 }
128}
129
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000130static int efx_mcdi_poll(struct efx_nic *efx)
131{
132 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000133 unsigned long time, finish;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000134 unsigned int respseq, respcmd, error;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100135 unsigned int spins;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000136 efx_dword_t reg;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100137 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000138
139 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100140 rc = efx_mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000141 if (rc)
142 goto out;
143
144 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
145 * because generally mcdi responses are fast. After that, back off
146 * and poll once a jiffy (approximately)
147 */
148 spins = TICK_USEC;
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000149 finish = jiffies + MCDI_RPC_TIMEOUT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000150
151 while (1) {
152 if (spins != 0) {
153 --spins;
154 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000155 } else {
156 schedule_timeout_uninterruptible(1);
157 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000158
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000159 time = jiffies;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000160
Ben Hutchings86c432c2011-09-01 12:09:29 +0000161 rmb();
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100162 if (efx->type->mcdi_poll_response(efx))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000163 break;
164
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000165 if (time_after(time, finish))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000166 return -ETIMEDOUT;
167 }
168
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100169 efx->type->mcdi_read_response(efx, &reg, 0, 4);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000170 mcdi->resplen = EFX_DWORD_FIELD(reg, MCDI_HEADER_DATALEN);
171 respseq = EFX_DWORD_FIELD(reg, MCDI_HEADER_SEQ);
172 respcmd = EFX_DWORD_FIELD(reg, MCDI_HEADER_CODE);
173 error = EFX_DWORD_FIELD(reg, MCDI_HEADER_ERROR);
174
175 if (error && mcdi->resplen == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000176 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100177 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000178 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000179 netif_err(efx, hw, efx->net_dev,
180 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
181 respseq, mcdi->seqno);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100182 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000183 } else if (error) {
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100184 efx->type->mcdi_read_response(efx, &reg, 4, 4);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100185 rc = efx_mcdi_errno(EFX_DWORD_FIELD(reg, EFX_DWORD_0));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000186 } else
187 rc = 0;
188
189out:
190 mcdi->resprc = rc;
191 if (rc)
192 mcdi->resplen = 0;
193
194 /* Return rc=0 like wait_event_timeout() */
195 return 0;
196}
197
Ben Hutchings876be082012-10-01 20:58:35 +0100198/* Test and clear MC-rebooted flag for this port/function; reset
199 * software state as necessary.
200 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000201int efx_mcdi_poll_reboot(struct efx_nic *efx)
202{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100203 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000204
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100205 if (!efx->mcdi)
206 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000207
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100208 rc = efx->type->mcdi_poll_reboot(efx);
209 if (!rc)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000210 return 0;
211
Ben Hutchings876be082012-10-01 20:58:35 +0100212 /* MAC statistics have been cleared on the NIC; clear our copy
213 * so that efx_update_diff_stat() can continue to work.
214 */
215 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
216
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100217 return rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000218}
219
220static void efx_mcdi_acquire(struct efx_mcdi_iface *mcdi)
221{
222 /* Wait until the interface becomes QUIESCENT and we win the race
223 * to mark it RUNNING. */
224 wait_event(mcdi->wq,
225 atomic_cmpxchg(&mcdi->state,
226 MCDI_STATE_QUIESCENT,
227 MCDI_STATE_RUNNING)
228 == MCDI_STATE_QUIESCENT);
229}
230
231static int efx_mcdi_await_completion(struct efx_nic *efx)
232{
233 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
234
235 if (wait_event_timeout(
236 mcdi->wq,
237 atomic_read(&mcdi->state) == MCDI_STATE_COMPLETED,
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000238 MCDI_RPC_TIMEOUT) == 0)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000239 return -ETIMEDOUT;
240
241 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
242 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
243 * completed the request first, then we'll just end up completing the
244 * request again, which is safe.
245 *
246 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
247 * wait_event_timeout() implicitly provides.
248 */
249 if (mcdi->mode == MCDI_MODE_POLL)
250 return efx_mcdi_poll(efx);
251
252 return 0;
253}
254
255static bool efx_mcdi_complete(struct efx_mcdi_iface *mcdi)
256{
257 /* If the interface is RUNNING, then move to COMPLETED and wake any
258 * waiters. If the interface isn't in RUNNING then we've received a
259 * duplicate completion after we've already transitioned back to
260 * QUIESCENT. [A subsequent invocation would increment seqno, so would
261 * have failed the seqno check].
262 */
263 if (atomic_cmpxchg(&mcdi->state,
264 MCDI_STATE_RUNNING,
265 MCDI_STATE_COMPLETED) == MCDI_STATE_RUNNING) {
266 wake_up(&mcdi->wq);
267 return true;
268 }
269
270 return false;
271}
272
273static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
274{
275 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
276 wake_up(&mcdi->wq);
277}
278
279static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100280 unsigned int datalen, unsigned int mcdi_err)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000281{
282 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
283 bool wake = false;
284
285 spin_lock(&mcdi->iface_lock);
286
287 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
288 if (mcdi->credits)
289 /* The request has been cancelled */
290 --mcdi->credits;
291 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000292 netif_err(efx, hw, efx->net_dev,
293 "MC response mismatch tx seq 0x%x rx "
294 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000295 } else {
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100296 mcdi->resprc = efx_mcdi_errno(mcdi_err);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000297 mcdi->resplen = datalen;
298
299 wake = true;
300 }
301
302 spin_unlock(&mcdi->iface_lock);
303
304 if (wake)
305 efx_mcdi_complete(mcdi);
306}
307
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000308int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
Ben Hutchings9528b922012-09-14 17:31:41 +0100309 const efx_dword_t *inbuf, size_t inlen,
310 efx_dword_t *outbuf, size_t outlen,
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000311 size_t *outlen_actual)
312{
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100313 efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
314 return efx_mcdi_rpc_finish(efx, cmd, inlen,
315 outbuf, outlen, outlen_actual);
316}
317
Ben Hutchings9528b922012-09-14 17:31:41 +0100318void efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
319 const efx_dword_t *inbuf, size_t inlen)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100320{
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000321 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100322
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000323 efx_mcdi_acquire(mcdi);
324
325 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
326 spin_lock_bh(&mcdi->iface_lock);
327 ++mcdi->seqno;
328 spin_unlock_bh(&mcdi->iface_lock);
329
330 efx_mcdi_copyin(efx, cmd, inbuf, inlen);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100331}
332
333int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
Ben Hutchings9528b922012-09-14 17:31:41 +0100334 efx_dword_t *outbuf, size_t outlen,
335 size_t *outlen_actual)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100336{
337 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
338 int rc;
339
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000340 if (mcdi->mode == MCDI_MODE_POLL)
341 rc = efx_mcdi_poll(efx);
342 else
343 rc = efx_mcdi_await_completion(efx);
344
345 if (rc != 0) {
346 /* Close the race with efx_mcdi_ev_cpl() executing just too late
347 * and completing a request we've just cancelled, by ensuring
348 * that the seqno check therein fails.
349 */
350 spin_lock_bh(&mcdi->iface_lock);
351 ++mcdi->seqno;
352 ++mcdi->credits;
353 spin_unlock_bh(&mcdi->iface_lock);
354
Ben Hutchings62776d02010-06-23 11:30:07 +0000355 netif_err(efx, hw, efx->net_dev,
356 "MC command 0x%x inlen %d mode %d timed out\n",
357 cmd, (int)inlen, mcdi->mode);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000358 } else {
359 size_t resplen;
360
361 /* At the very least we need a memory barrier here to ensure
362 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
363 * a spurious efx_mcdi_ev_cpl() running concurrently by
364 * acquiring the iface_lock. */
365 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100366 rc = mcdi->resprc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000367 resplen = mcdi->resplen;
368 spin_unlock_bh(&mcdi->iface_lock);
369
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100370 BUG_ON(rc > 0);
371
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000372 if (rc == 0) {
373 efx_mcdi_copyout(efx, outbuf,
Ben Hutchings9528b922012-09-14 17:31:41 +0100374 min(outlen, mcdi->resplen));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000375 if (outlen_actual != NULL)
376 *outlen_actual = resplen;
377 } else if (cmd == MC_CMD_REBOOT && rc == -EIO)
378 ; /* Don't reset if MC_CMD_REBOOT returns EIO */
379 else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000380 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
381 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000382 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
383 } else
Ben Hutchingsf18ca362010-12-02 13:46:09 +0000384 netif_dbg(efx, hw, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000385 "MC command 0x%x inlen %d failed rc=%d\n",
386 cmd, (int)inlen, -rc);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000387
388 if (rc == -EIO || rc == -EINTR) {
389 msleep(MCDI_STATUS_SLEEP_MS);
390 efx_mcdi_poll_reboot(efx);
391 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000392 }
393
394 efx_mcdi_release(mcdi);
395 return rc;
396}
397
398void efx_mcdi_mode_poll(struct efx_nic *efx)
399{
400 struct efx_mcdi_iface *mcdi;
401
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100402 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000403 return;
404
405 mcdi = efx_mcdi(efx);
406 if (mcdi->mode == MCDI_MODE_POLL)
407 return;
408
409 /* We can switch from event completion to polled completion, because
410 * mcdi requests are always completed in shared memory. We do this by
411 * switching the mode to POLL'd then completing the request.
412 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
413 *
414 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
415 * which efx_mcdi_complete() provides for us.
416 */
417 mcdi->mode = MCDI_MODE_POLL;
418
419 efx_mcdi_complete(mcdi);
420}
421
422void efx_mcdi_mode_event(struct efx_nic *efx)
423{
424 struct efx_mcdi_iface *mcdi;
425
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100426 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000427 return;
428
429 mcdi = efx_mcdi(efx);
430
431 if (mcdi->mode == MCDI_MODE_EVENTS)
432 return;
433
434 /* We can't switch from polled to event completion in the middle of a
435 * request, because the completion method is specified in the request.
436 * So acquire the interface to serialise the requestors. We don't need
437 * to acquire the iface_lock to change the mode here, but we do need a
438 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
439 * efx_mcdi_acquire() provides.
440 */
441 efx_mcdi_acquire(mcdi);
442 mcdi->mode = MCDI_MODE_EVENTS;
443 efx_mcdi_release(mcdi);
444}
445
446static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
447{
448 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
449
450 /* If there is an outstanding MCDI request, it has been terminated
451 * either by a BADASSERT or REBOOT event. If the mcdi interface is
452 * in polled mode, then do nothing because the MC reboot handler will
453 * set the header correctly. However, if the mcdi interface is waiting
454 * for a CMDDONE event it won't receive it [and since all MCDI events
455 * are sent to the same queue, we can't be racing with
456 * efx_mcdi_ev_cpl()]
457 *
458 * There's a race here with efx_mcdi_rpc(), because we might receive
459 * a REBOOT event *before* the request has been copied out. In polled
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300460 * mode (during startup) this is irrelevant, because efx_mcdi_complete()
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000461 * is ignored. In event mode, this condition is just an edge-case of
462 * receiving a REBOOT event after posting the MCDI request. Did the mc
463 * reboot before or after the copyout? The best we can do always is
464 * just return failure.
465 */
466 spin_lock(&mcdi->iface_lock);
467 if (efx_mcdi_complete(mcdi)) {
468 if (mcdi->mode == MCDI_MODE_EVENTS) {
469 mcdi->resprc = rc;
470 mcdi->resplen = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +0000471 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000472 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000473 } else {
474 int count;
475
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000476 /* Nobody was waiting for an MCDI request, so trigger a reset */
477 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
478
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000479 /* Consume the status word since efx_mcdi_rpc_finish() won't */
480 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
481 if (efx_mcdi_poll_reboot(efx))
482 break;
483 udelay(MCDI_STATUS_DELAY_US);
484 }
485 }
486
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000487 spin_unlock(&mcdi->iface_lock);
488}
489
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000490/* Called from falcon_process_eventq for MCDI events */
491void efx_mcdi_process_event(struct efx_channel *channel,
492 efx_qword_t *event)
493{
494 struct efx_nic *efx = channel->efx;
495 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
496 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
497
498 switch (code) {
499 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000500 netif_err(efx, hw, efx->net_dev,
501 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100502 efx_mcdi_ev_death(efx, -EINTR);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000503 break;
504
505 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +0000506 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000507 break;
508
509 case MCDI_EVENT_CODE_CMDDONE:
510 efx_mcdi_ev_cpl(efx,
511 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
512 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
513 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
514 break;
515
516 case MCDI_EVENT_CODE_LINKCHANGE:
517 efx_mcdi_process_link_change(efx, event);
518 break;
519 case MCDI_EVENT_CODE_SENSOREVT:
520 efx_mcdi_sensor_event(efx, event);
521 break;
522 case MCDI_EVENT_CODE_SCHEDERR:
Ben Hutchings62776d02010-06-23 11:30:07 +0000523 netif_info(efx, hw, efx->net_dev,
524 "MC Scheduler error address=0x%x\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000525 break;
526 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000527 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100528 efx_mcdi_ev_death(efx, -EIO);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000529 break;
530 case MCDI_EVENT_CODE_MAC_STATS_DMA:
531 /* MAC stats are gather lazily. We can ignore this. */
532 break;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +0000533 case MCDI_EVENT_CODE_FLR:
534 efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF));
535 break;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100536 case MCDI_EVENT_CODE_PTP_RX:
537 case MCDI_EVENT_CODE_PTP_FAULT:
538 case MCDI_EVENT_CODE_PTP_PPS:
539 efx_ptp_event(efx, event);
540 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000541
542 default:
Ben Hutchings62776d02010-06-23 11:30:07 +0000543 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
544 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000545 }
546}
547
548/**************************************************************************
549 *
550 * Specific request functions
551 *
552 **************************************************************************
553 */
554
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000555void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000556{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100557 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000558 size_t outlength;
559 const __le16 *ver_words;
560 int rc;
561
562 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
563
564 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
565 outbuf, sizeof(outbuf), &outlength);
566 if (rc)
567 goto fail;
568
Ben Hutchings05a93202011-12-20 00:44:06 +0000569 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000570 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000571 goto fail;
572 }
573
574 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000575 snprintf(buf, len, "%u.%u.%u.%u",
576 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
577 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
578 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000579
580fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000581 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000582 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000583}
584
585int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
586 bool *was_attached)
587{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100588 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
589 MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000590 size_t outlen;
591 int rc;
592
593 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
594 driver_operating ? 1 : 0);
595 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
596
597 rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
598 outbuf, sizeof(outbuf), &outlen);
599 if (rc)
600 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000601 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
602 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000603 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000604 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000605
606 if (was_attached != NULL)
607 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
608 return 0;
609
610fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000611 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000612 return rc;
613}
614
615int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +0100616 u16 *fw_subtype_list, u32 *capabilities)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000617{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100618 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100619 size_t outlen, i;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000620 int port_num = efx_port_num(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000621 int rc;
622
623 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
624
625 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
626 outbuf, sizeof(outbuf), &outlen);
627 if (rc)
628 goto fail;
629
Ben Hutchings05a93202011-12-20 00:44:06 +0000630 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000631 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000632 goto fail;
633 }
634
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000635 if (mac_address)
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100636 memcpy(mac_address,
637 port_num ?
638 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
639 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0),
640 ETH_ALEN);
Ben Hutchingsbfeed902012-09-07 00:58:10 +0100641 if (fw_subtype_list) {
Ben Hutchingsbfeed902012-09-07 00:58:10 +0100642 for (i = 0;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100643 i < MCDI_VAR_ARRAY_LEN(outlen,
644 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
645 i++)
646 fw_subtype_list[i] = MCDI_ARRAY_WORD(
647 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
648 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
649 fw_subtype_list[i] = 0;
Ben Hutchingsbfeed902012-09-07 00:58:10 +0100650 }
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +0100651 if (capabilities) {
652 if (port_num)
653 *capabilities = MCDI_DWORD(outbuf,
654 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
655 else
656 *capabilities = MCDI_DWORD(outbuf,
657 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
658 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000659
660 return 0;
661
662fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000663 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
664 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000665
666 return rc;
667}
668
669int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
670{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100671 MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000672 u32 dest = 0;
673 int rc;
674
675 if (uart)
676 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
677 if (evq)
678 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
679
680 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
681 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
682
683 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
684
685 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
686 NULL, 0, NULL);
687 if (rc)
688 goto fail;
689
690 return 0;
691
692fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000693 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000694 return rc;
695}
696
697int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
698{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100699 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000700 size_t outlen;
701 int rc;
702
703 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
704
705 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
706 outbuf, sizeof(outbuf), &outlen);
707 if (rc)
708 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000709 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
710 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000711 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000712 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000713
714 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
715 return 0;
716
717fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000718 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
719 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000720 return rc;
721}
722
723int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
724 size_t *size_out, size_t *erase_size_out,
725 bool *protected_out)
726{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100727 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
728 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000729 size_t outlen;
730 int rc;
731
732 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
733
734 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
735 outbuf, sizeof(outbuf), &outlen);
736 if (rc)
737 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000738 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
739 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000740 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000741 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000742
743 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
744 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
745 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +0000746 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000747 return 0;
748
749fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000750 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000751 return rc;
752}
753
754int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
755{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100756 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000757 int rc;
758
759 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
760
761 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
762
763 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
764 NULL, 0, NULL);
765 if (rc)
766 goto fail;
767
768 return 0;
769
770fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000771 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000772 return rc;
773}
774
775int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
776 loff_t offset, u8 *buffer, size_t length)
777{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100778 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
779 MCDI_DECLARE_BUF(outbuf,
780 MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000781 size_t outlen;
782 int rc;
783
784 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
785 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
786 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
787
788 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
789 outbuf, sizeof(outbuf), &outlen);
790 if (rc)
791 goto fail;
792
793 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
794 return 0;
795
796fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000797 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000798 return rc;
799}
800
801int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
802 loff_t offset, const u8 *buffer, size_t length)
803{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100804 MCDI_DECLARE_BUF(inbuf,
805 MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000806 int rc;
807
808 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
809 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
810 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
811 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
812
813 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
814
Ben Hutchings5a27e862010-01-25 15:49:59 -0800815 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
816 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000817 NULL, 0, NULL);
818 if (rc)
819 goto fail;
820
821 return 0;
822
823fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000824 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000825 return rc;
826}
827
828int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
829 loff_t offset, size_t length)
830{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100831 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000832 int rc;
833
834 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
835 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
836 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
837
838 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
839
840 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
841 NULL, 0, NULL);
842 if (rc)
843 goto fail;
844
845 return 0;
846
847fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000848 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000849 return rc;
850}
851
852int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
853{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100854 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000855 int rc;
856
857 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
858
859 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
860
861 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
862 NULL, 0, NULL);
863 if (rc)
864 goto fail;
865
866 return 0;
867
868fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000869 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000870 return rc;
871}
872
Ben Hutchings2e803402010-02-03 09:31:01 +0000873static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
874{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100875 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
876 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
Ben Hutchings2e803402010-02-03 09:31:01 +0000877 int rc;
878
879 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
880
881 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
882 outbuf, sizeof(outbuf), NULL);
883 if (rc)
884 return rc;
885
886 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
887 case MC_CMD_NVRAM_TEST_PASS:
888 case MC_CMD_NVRAM_TEST_NOTSUPP:
889 return 0;
890 default:
891 return -EIO;
892 }
893}
894
895int efx_mcdi_nvram_test_all(struct efx_nic *efx)
896{
897 u32 nvram_types;
898 unsigned int type;
899 int rc;
900
901 rc = efx_mcdi_nvram_types(efx, &nvram_types);
902 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000903 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +0000904
905 type = 0;
906 while (nvram_types != 0) {
907 if (nvram_types & 1) {
908 rc = efx_mcdi_nvram_test(efx, type);
909 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000910 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +0000911 }
912 type++;
913 nvram_types >>= 1;
914 }
915
916 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +0000917
918fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +0000919 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
920 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000921fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +0000922 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000923 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +0000924}
925
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000926static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000927{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100928 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
929 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100930 unsigned int flags, index;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000931 const char *reason;
932 size_t outlen;
933 int retry;
934 int rc;
935
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000936 /* Attempt to read any stored assertion state before we reboot
937 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000938 * because a boot-time assertion might cause this command to fail
939 * with EINTR. And once again because GET_ASSERTS can race with
940 * MC_CMD_REBOOT running on the other port. */
941 retry = 2;
942 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000943 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000944 rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS,
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000945 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
946 outbuf, sizeof(outbuf), &outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000947 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
948
949 if (rc)
950 return rc;
951 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000952 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000953
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000954 /* Print out any recorded assertion state */
955 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000956 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
957 return 0;
958
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000959 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
960 ? "system-level assertion"
961 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
962 ? "thread-level assertion"
963 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
964 ? "watchdog reset"
965 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +0000966 netif_err(efx, hw, efx->net_dev,
967 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
968 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
969 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000970
971 /* Print out the registers */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100972 for (index = 0;
973 index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
974 index++)
975 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
976 1 + index,
977 MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
978 index));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000979
980 return 0;
981}
982
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000983static void efx_mcdi_exit_assertion(struct efx_nic *efx)
984{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100985 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000986
Ben Hutchings0f1e54a2012-07-02 23:37:40 +0100987 /* If the MC is running debug firmware, it might now be
988 * waiting for a debugger to attach, but we just want it to
989 * reboot. We set a flag that makes the command a no-op if it
990 * has already done so. We don't know what return code to
991 * expect (0 or -EIO), so ignore it.
992 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000993 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
994 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
995 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
Ben Hutchings0f1e54a2012-07-02 23:37:40 +0100996 (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
997 NULL, 0, NULL);
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000998}
999
1000int efx_mcdi_handle_assertion(struct efx_nic *efx)
1001{
1002 int rc;
1003
1004 rc = efx_mcdi_read_assertion(efx);
1005 if (rc)
1006 return rc;
1007
1008 efx_mcdi_exit_assertion(efx);
1009
1010 return 0;
1011}
1012
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001013void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1014{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001015 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001016 int rc;
1017
1018 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1019 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1020 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1021
1022 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1023
1024 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1025
1026 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1027 NULL, 0, NULL);
1028 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001029 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1030 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001031}
1032
Ben Hutchings6bff8612012-09-18 02:33:52 +01001033static int efx_mcdi_reset_port(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001034{
Ben Hutchings05a93202011-12-20 00:44:06 +00001035 int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001036 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001037 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1038 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001039 return rc;
1040}
1041
Ben Hutchings6bff8612012-09-18 02:33:52 +01001042static int efx_mcdi_reset_mc(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001043{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001044 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001045 int rc;
1046
1047 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1048 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1049 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1050 NULL, 0, NULL);
1051 /* White is black, and up is down */
1052 if (rc == -EIO)
1053 return 0;
1054 if (rc == 0)
1055 rc = -EIO;
Ben Hutchings62776d02010-06-23 11:30:07 +00001056 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001057 return rc;
1058}
1059
Ben Hutchings6bff8612012-09-18 02:33:52 +01001060enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1061{
1062 return RESET_TYPE_RECOVER_OR_ALL;
1063}
1064
1065int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1066{
1067 int rc;
1068
1069 /* Recover from a failed assertion pre-reset */
1070 rc = efx_mcdi_handle_assertion(efx);
1071 if (rc)
1072 return rc;
1073
1074 if (method == RESET_TYPE_WORLD)
1075 return efx_mcdi_reset_mc(efx);
1076 else
1077 return efx_mcdi_reset_port(efx);
1078}
1079
stephen hemmingerd2156972010-10-18 05:27:31 +00001080static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1081 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001082{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001083 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1084 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001085 size_t outlen;
1086 int rc;
1087
1088 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1089 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1090 MC_CMD_FILTER_MODE_SIMPLE);
1091 memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN);
1092
1093 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1094 outbuf, sizeof(outbuf), &outlen);
1095 if (rc)
1096 goto fail;
1097
1098 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001099 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001100 goto fail;
1101 }
1102
1103 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1104
1105 return 0;
1106
1107fail:
1108 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001109 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001110 return rc;
1111
1112}
1113
1114
1115int
1116efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1117{
1118 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1119}
1120
1121
1122int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1123{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001124 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001125 size_t outlen;
1126 int rc;
1127
1128 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1129 outbuf, sizeof(outbuf), &outlen);
1130 if (rc)
1131 goto fail;
1132
1133 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001134 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001135 goto fail;
1136 }
1137
1138 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1139
1140 return 0;
1141
1142fail:
1143 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001144 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001145 return rc;
1146}
1147
1148
1149int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1150{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001151 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001152 int rc;
1153
1154 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1155
1156 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1157 NULL, 0, NULL);
1158 if (rc)
1159 goto fail;
1160
1161 return 0;
1162
1163fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001164 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001165 return rc;
1166}
1167
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001168int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1169{
1170 struct efx_channel *channel;
1171 struct efx_rx_queue *rx_queue;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001172 MCDI_DECLARE_BUF(inbuf,
1173 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001174 int rc, count;
1175
Ben Hutchings45078372012-09-19 02:53:34 +01001176 BUILD_BUG_ON(EFX_MAX_CHANNELS >
1177 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1178
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001179 count = 0;
1180 efx_for_each_channel(channel, efx) {
1181 efx_for_each_channel_rx_queue(rx_queue, channel) {
1182 if (rx_queue->flush_pending) {
1183 rx_queue->flush_pending = false;
1184 atomic_dec(&efx->rxq_flush_pending);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001185 MCDI_SET_ARRAY_DWORD(
1186 inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1187 count, efx_rx_queue_index(rx_queue));
1188 count++;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001189 }
1190 }
1191 }
1192
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001193 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1194 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
Ben Hutchingsbbec9692012-09-11 18:25:13 +01001195 WARN_ON(rc < 0);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001196
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001197 return rc;
1198}
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001199
1200int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1201{
1202 int rc;
1203
1204 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1205 if (rc)
1206 goto fail;
1207
1208 return 0;
1209
1210fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001211 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001212 return rc;
1213}
1214