blob: 578e5f72aabe5a0b055344e4ca76f25927899d03 [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"
14#include "regs.h"
15#include "mcdi_pcol.h"
16#include "phy.h"
17
18/**************************************************************************
19 *
20 * Management-Controller-to-Driver Interface
21 *
22 **************************************************************************
23 */
24
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000025#define MCDI_RPC_TIMEOUT 10 /*seconds */
26
27#define MCDI_PDU(efx) \
Ben Hutchings788ec412011-12-20 23:52:02 +000028 (efx_port_num(efx) ? MC_SMEM_P1_PDU_OFST : MC_SMEM_P0_PDU_OFST)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000029#define MCDI_DOORBELL(efx) \
Ben Hutchings788ec412011-12-20 23:52:02 +000030 (efx_port_num(efx) ? MC_SMEM_P1_DOORBELL_OFST : MC_SMEM_P0_DOORBELL_OFST)
Ben Hutchings3f713bf2011-12-20 23:39:31 +000031#define MCDI_STATUS(efx) \
32 (efx_port_num(efx) ? MC_SMEM_P1_STATUS_OFST : MC_SMEM_P0_STATUS_OFST)
33
34/* A reboot/assertion causes the MCDI status word to be set after the
35 * command word is set or a REBOOT event is sent. If we notice a reboot
36 * via these mechanisms then wait 10ms for the status word to be set. */
37#define MCDI_STATUS_DELAY_US 100
38#define MCDI_STATUS_DELAY_COUNT 100
39#define MCDI_STATUS_SLEEP_MS \
40 (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000041
42#define SEQ_MASK \
43 EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
44
45static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
46{
47 struct siena_nic_data *nic_data;
48 EFX_BUG_ON_PARANOID(efx_nic_rev(efx) < EFX_REV_SIENA_A0);
49 nic_data = efx->nic_data;
50 return &nic_data->mcdi;
51}
52
53void efx_mcdi_init(struct efx_nic *efx)
54{
55 struct efx_mcdi_iface *mcdi;
56
57 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
58 return;
59
60 mcdi = efx_mcdi(efx);
61 init_waitqueue_head(&mcdi->wq);
62 spin_lock_init(&mcdi->iface_lock);
63 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
64 mcdi->mode = MCDI_MODE_POLL;
65
66 (void) efx_mcdi_poll_reboot(efx);
67}
68
69static void efx_mcdi_copyin(struct efx_nic *efx, unsigned cmd,
70 const u8 *inbuf, size_t inlen)
71{
72 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchings86c432c2011-09-01 12:09:29 +000073 unsigned pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
74 unsigned doorbell = FR_CZ_MC_TREG_SMEM + MCDI_DOORBELL(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000075 unsigned int i;
76 efx_dword_t hdr;
77 u32 xflags, seqno;
78
79 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
Ben Hutchings788ec412011-12-20 23:52:02 +000080 BUG_ON(inlen & 3 || inlen >= MC_SMEM_PDU_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000081
82 seqno = mcdi->seqno & SEQ_MASK;
83 xflags = 0;
84 if (mcdi->mode == MCDI_MODE_EVENTS)
85 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
86
87 EFX_POPULATE_DWORD_6(hdr,
88 MCDI_HEADER_RESPONSE, 0,
89 MCDI_HEADER_RESYNC, 1,
90 MCDI_HEADER_CODE, cmd,
91 MCDI_HEADER_DATALEN, inlen,
92 MCDI_HEADER_SEQ, seqno,
93 MCDI_HEADER_XFLAGS, xflags);
94
Ben Hutchings86c432c2011-09-01 12:09:29 +000095 efx_writed(efx, &hdr, pdu);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000096
Ben Hutchings747df222011-05-11 17:41:18 +010097 for (i = 0; i < inlen; i += 4)
Ben Hutchings86c432c2011-09-01 12:09:29 +000098 _efx_writed(efx, *((__le32 *)(inbuf + i)), pdu + 4 + i);
99
100 /* Ensure the payload is written out before the header */
101 wmb();
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000102
103 /* ring the doorbell with a distinctive value */
Ben Hutchings86c432c2011-09-01 12:09:29 +0000104 _efx_writed(efx, (__force __le32) 0x45789abc, doorbell);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000105}
106
107static void efx_mcdi_copyout(struct efx_nic *efx, u8 *outbuf, size_t outlen)
108{
109 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchings86c432c2011-09-01 12:09:29 +0000110 unsigned int pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000111 int i;
112
113 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
Ben Hutchings788ec412011-12-20 23:52:02 +0000114 BUG_ON(outlen & 3 || outlen >= MC_SMEM_PDU_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000115
116 for (i = 0; i < outlen; i += 4)
Ben Hutchings86c432c2011-09-01 12:09:29 +0000117 *((__le32 *)(outbuf + i)) = _efx_readd(efx, pdu + 4 + i);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000118}
119
120static int efx_mcdi_poll(struct efx_nic *efx)
121{
122 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
123 unsigned int time, finish;
124 unsigned int respseq, respcmd, error;
Ben Hutchings86c432c2011-09-01 12:09:29 +0000125 unsigned int pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000126 unsigned int rc, spins;
127 efx_dword_t reg;
128
129 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchingse0bf54c2010-02-19 13:29:27 +0000130 rc = -efx_mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000131 if (rc)
132 goto out;
133
134 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
135 * because generally mcdi responses are fast. After that, back off
136 * and poll once a jiffy (approximately)
137 */
138 spins = TICK_USEC;
139 finish = get_seconds() + MCDI_RPC_TIMEOUT;
140
141 while (1) {
142 if (spins != 0) {
143 --spins;
144 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000145 } else {
146 schedule_timeout_uninterruptible(1);
147 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000148
149 time = get_seconds();
150
Ben Hutchings86c432c2011-09-01 12:09:29 +0000151 rmb();
152 efx_readd(efx, &reg, pdu);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000153
154 /* All 1's indicates that shared memory is in reset (and is
155 * not a valid header). Wait for it to come out reset before
156 * completing the command */
157 if (EFX_DWORD_FIELD(reg, EFX_DWORD_0) != 0xffffffff &&
158 EFX_DWORD_FIELD(reg, MCDI_HEADER_RESPONSE))
159 break;
160
161 if (time >= finish)
162 return -ETIMEDOUT;
163 }
164
165 mcdi->resplen = EFX_DWORD_FIELD(reg, MCDI_HEADER_DATALEN);
166 respseq = EFX_DWORD_FIELD(reg, MCDI_HEADER_SEQ);
167 respcmd = EFX_DWORD_FIELD(reg, MCDI_HEADER_CODE);
168 error = EFX_DWORD_FIELD(reg, MCDI_HEADER_ERROR);
169
170 if (error && mcdi->resplen == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000171 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000172 rc = EIO;
173 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000174 netif_err(efx, hw, efx->net_dev,
175 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
176 respseq, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000177 rc = EIO;
178 } else if (error) {
Ben Hutchings86c432c2011-09-01 12:09:29 +0000179 efx_readd(efx, &reg, pdu + 4);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000180 switch (EFX_DWORD_FIELD(reg, EFX_DWORD_0)) {
181#define TRANSLATE_ERROR(name) \
182 case MC_CMD_ERR_ ## name: \
183 rc = name; \
184 break
185 TRANSLATE_ERROR(ENOENT);
186 TRANSLATE_ERROR(EINTR);
187 TRANSLATE_ERROR(EACCES);
188 TRANSLATE_ERROR(EBUSY);
189 TRANSLATE_ERROR(EINVAL);
190 TRANSLATE_ERROR(EDEADLK);
191 TRANSLATE_ERROR(ENOSYS);
192 TRANSLATE_ERROR(ETIME);
193#undef TRANSLATE_ERROR
194 default:
195 rc = EIO;
196 break;
197 }
198 } else
199 rc = 0;
200
201out:
202 mcdi->resprc = rc;
203 if (rc)
204 mcdi->resplen = 0;
205
206 /* Return rc=0 like wait_event_timeout() */
207 return 0;
208}
209
210/* Test and clear MC-rebooted flag for this port/function */
211int efx_mcdi_poll_reboot(struct efx_nic *efx)
212{
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000213 unsigned int addr = FR_CZ_MC_TREG_SMEM + MCDI_STATUS(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000214 efx_dword_t reg;
215 uint32_t value;
216
217 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
218 return false;
219
Ben Hutchings86c432c2011-09-01 12:09:29 +0000220 efx_readd(efx, &reg, addr);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000221 value = EFX_DWORD_FIELD(reg, EFX_DWORD_0);
222
223 if (value == 0)
224 return 0;
225
226 EFX_ZERO_DWORD(reg);
Ben Hutchings86c432c2011-09-01 12:09:29 +0000227 efx_writed(efx, &reg, addr);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000228
229 if (value == MC_STATUS_DWORD_ASSERT)
230 return -EINTR;
231 else
232 return -EIO;
233}
234
235static void efx_mcdi_acquire(struct efx_mcdi_iface *mcdi)
236{
237 /* Wait until the interface becomes QUIESCENT and we win the race
238 * to mark it RUNNING. */
239 wait_event(mcdi->wq,
240 atomic_cmpxchg(&mcdi->state,
241 MCDI_STATE_QUIESCENT,
242 MCDI_STATE_RUNNING)
243 == MCDI_STATE_QUIESCENT);
244}
245
246static int efx_mcdi_await_completion(struct efx_nic *efx)
247{
248 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
249
250 if (wait_event_timeout(
251 mcdi->wq,
252 atomic_read(&mcdi->state) == MCDI_STATE_COMPLETED,
253 msecs_to_jiffies(MCDI_RPC_TIMEOUT * 1000)) == 0)
254 return -ETIMEDOUT;
255
256 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
257 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
258 * completed the request first, then we'll just end up completing the
259 * request again, which is safe.
260 *
261 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
262 * wait_event_timeout() implicitly provides.
263 */
264 if (mcdi->mode == MCDI_MODE_POLL)
265 return efx_mcdi_poll(efx);
266
267 return 0;
268}
269
270static bool efx_mcdi_complete(struct efx_mcdi_iface *mcdi)
271{
272 /* If the interface is RUNNING, then move to COMPLETED and wake any
273 * waiters. If the interface isn't in RUNNING then we've received a
274 * duplicate completion after we've already transitioned back to
275 * QUIESCENT. [A subsequent invocation would increment seqno, so would
276 * have failed the seqno check].
277 */
278 if (atomic_cmpxchg(&mcdi->state,
279 MCDI_STATE_RUNNING,
280 MCDI_STATE_COMPLETED) == MCDI_STATE_RUNNING) {
281 wake_up(&mcdi->wq);
282 return true;
283 }
284
285 return false;
286}
287
288static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
289{
290 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
291 wake_up(&mcdi->wq);
292}
293
294static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
295 unsigned int datalen, unsigned int errno)
296{
297 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
298 bool wake = false;
299
300 spin_lock(&mcdi->iface_lock);
301
302 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
303 if (mcdi->credits)
304 /* The request has been cancelled */
305 --mcdi->credits;
306 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000307 netif_err(efx, hw, efx->net_dev,
308 "MC response mismatch tx seq 0x%x rx "
309 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000310 } else {
311 mcdi->resprc = errno;
312 mcdi->resplen = datalen;
313
314 wake = true;
315 }
316
317 spin_unlock(&mcdi->iface_lock);
318
319 if (wake)
320 efx_mcdi_complete(mcdi);
321}
322
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000323int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
324 const u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen,
325 size_t *outlen_actual)
326{
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100327 efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
328 return efx_mcdi_rpc_finish(efx, cmd, inlen,
329 outbuf, outlen, outlen_actual);
330}
331
332void efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd, const u8 *inbuf,
333 size_t inlen)
334{
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000335 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100336
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000337 BUG_ON(efx_nic_rev(efx) < EFX_REV_SIENA_A0);
338
339 efx_mcdi_acquire(mcdi);
340
341 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
342 spin_lock_bh(&mcdi->iface_lock);
343 ++mcdi->seqno;
344 spin_unlock_bh(&mcdi->iface_lock);
345
346 efx_mcdi_copyin(efx, cmd, inbuf, inlen);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100347}
348
349int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
350 u8 *outbuf, size_t outlen, size_t *outlen_actual)
351{
352 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
353 int rc;
354
355 BUG_ON(efx_nic_rev(efx) < EFX_REV_SIENA_A0);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000356
357 if (mcdi->mode == MCDI_MODE_POLL)
358 rc = efx_mcdi_poll(efx);
359 else
360 rc = efx_mcdi_await_completion(efx);
361
362 if (rc != 0) {
363 /* Close the race with efx_mcdi_ev_cpl() executing just too late
364 * and completing a request we've just cancelled, by ensuring
365 * that the seqno check therein fails.
366 */
367 spin_lock_bh(&mcdi->iface_lock);
368 ++mcdi->seqno;
369 ++mcdi->credits;
370 spin_unlock_bh(&mcdi->iface_lock);
371
Ben Hutchings62776d02010-06-23 11:30:07 +0000372 netif_err(efx, hw, efx->net_dev,
373 "MC command 0x%x inlen %d mode %d timed out\n",
374 cmd, (int)inlen, mcdi->mode);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000375 } else {
376 size_t resplen;
377
378 /* At the very least we need a memory barrier here to ensure
379 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
380 * a spurious efx_mcdi_ev_cpl() running concurrently by
381 * acquiring the iface_lock. */
382 spin_lock_bh(&mcdi->iface_lock);
383 rc = -mcdi->resprc;
384 resplen = mcdi->resplen;
385 spin_unlock_bh(&mcdi->iface_lock);
386
387 if (rc == 0) {
388 efx_mcdi_copyout(efx, outbuf,
389 min(outlen, mcdi->resplen + 3) & ~0x3);
390 if (outlen_actual != NULL)
391 *outlen_actual = resplen;
392 } else if (cmd == MC_CMD_REBOOT && rc == -EIO)
393 ; /* Don't reset if MC_CMD_REBOOT returns EIO */
394 else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000395 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
396 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000397 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
398 } else
Ben Hutchingsf18ca362010-12-02 13:46:09 +0000399 netif_dbg(efx, hw, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000400 "MC command 0x%x inlen %d failed rc=%d\n",
401 cmd, (int)inlen, -rc);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000402
403 if (rc == -EIO || rc == -EINTR) {
404 msleep(MCDI_STATUS_SLEEP_MS);
405 efx_mcdi_poll_reboot(efx);
406 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000407 }
408
409 efx_mcdi_release(mcdi);
410 return rc;
411}
412
413void efx_mcdi_mode_poll(struct efx_nic *efx)
414{
415 struct efx_mcdi_iface *mcdi;
416
417 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
418 return;
419
420 mcdi = efx_mcdi(efx);
421 if (mcdi->mode == MCDI_MODE_POLL)
422 return;
423
424 /* We can switch from event completion to polled completion, because
425 * mcdi requests are always completed in shared memory. We do this by
426 * switching the mode to POLL'd then completing the request.
427 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
428 *
429 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
430 * which efx_mcdi_complete() provides for us.
431 */
432 mcdi->mode = MCDI_MODE_POLL;
433
434 efx_mcdi_complete(mcdi);
435}
436
437void efx_mcdi_mode_event(struct efx_nic *efx)
438{
439 struct efx_mcdi_iface *mcdi;
440
441 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
442 return;
443
444 mcdi = efx_mcdi(efx);
445
446 if (mcdi->mode == MCDI_MODE_EVENTS)
447 return;
448
449 /* We can't switch from polled to event completion in the middle of a
450 * request, because the completion method is specified in the request.
451 * So acquire the interface to serialise the requestors. We don't need
452 * to acquire the iface_lock to change the mode here, but we do need a
453 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
454 * efx_mcdi_acquire() provides.
455 */
456 efx_mcdi_acquire(mcdi);
457 mcdi->mode = MCDI_MODE_EVENTS;
458 efx_mcdi_release(mcdi);
459}
460
461static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
462{
463 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
464
465 /* If there is an outstanding MCDI request, it has been terminated
466 * either by a BADASSERT or REBOOT event. If the mcdi interface is
467 * in polled mode, then do nothing because the MC reboot handler will
468 * set the header correctly. However, if the mcdi interface is waiting
469 * for a CMDDONE event it won't receive it [and since all MCDI events
470 * are sent to the same queue, we can't be racing with
471 * efx_mcdi_ev_cpl()]
472 *
473 * There's a race here with efx_mcdi_rpc(), because we might receive
474 * a REBOOT event *before* the request has been copied out. In polled
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300475 * mode (during startup) this is irrelevant, because efx_mcdi_complete()
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000476 * is ignored. In event mode, this condition is just an edge-case of
477 * receiving a REBOOT event after posting the MCDI request. Did the mc
478 * reboot before or after the copyout? The best we can do always is
479 * just return failure.
480 */
481 spin_lock(&mcdi->iface_lock);
482 if (efx_mcdi_complete(mcdi)) {
483 if (mcdi->mode == MCDI_MODE_EVENTS) {
484 mcdi->resprc = rc;
485 mcdi->resplen = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +0000486 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000487 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000488 } else {
489 int count;
490
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000491 /* Nobody was waiting for an MCDI request, so trigger a reset */
492 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
493
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000494 /* Consume the status word since efx_mcdi_rpc_finish() won't */
495 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
496 if (efx_mcdi_poll_reboot(efx))
497 break;
498 udelay(MCDI_STATUS_DELAY_US);
499 }
500 }
501
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000502 spin_unlock(&mcdi->iface_lock);
503}
504
505static unsigned int efx_mcdi_event_link_speed[] = {
506 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
507 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
508 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
509};
510
511
512static void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
513{
514 u32 flags, fcntl, speed, lpa;
515
516 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
517 EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
518 speed = efx_mcdi_event_link_speed[speed];
519
520 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
521 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
522 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
523
524 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
525 * which is only run after flushing the event queues. Therefore, it
526 * is safe to modify the link state outside of the mac_lock here.
527 */
528 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
529
530 efx_mcdi_phy_check_fcntl(efx, lpa);
531
532 efx_link_status_changed(efx);
533}
534
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000535/* Called from falcon_process_eventq for MCDI events */
536void efx_mcdi_process_event(struct efx_channel *channel,
537 efx_qword_t *event)
538{
539 struct efx_nic *efx = channel->efx;
540 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
541 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
542
543 switch (code) {
544 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000545 netif_err(efx, hw, efx->net_dev,
546 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000547 efx_mcdi_ev_death(efx, EINTR);
548 break;
549
550 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +0000551 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000552 break;
553
554 case MCDI_EVENT_CODE_CMDDONE:
555 efx_mcdi_ev_cpl(efx,
556 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
557 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
558 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
559 break;
560
561 case MCDI_EVENT_CODE_LINKCHANGE:
562 efx_mcdi_process_link_change(efx, event);
563 break;
564 case MCDI_EVENT_CODE_SENSOREVT:
565 efx_mcdi_sensor_event(efx, event);
566 break;
567 case MCDI_EVENT_CODE_SCHEDERR:
Ben Hutchings62776d02010-06-23 11:30:07 +0000568 netif_info(efx, hw, efx->net_dev,
569 "MC Scheduler error address=0x%x\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000570 break;
571 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000572 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000573 efx_mcdi_ev_death(efx, EIO);
574 break;
575 case MCDI_EVENT_CODE_MAC_STATS_DMA:
576 /* MAC stats are gather lazily. We can ignore this. */
577 break;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +0000578 case MCDI_EVENT_CODE_FLR:
579 efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF));
580 break;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100581 case MCDI_EVENT_CODE_PTP_RX:
582 case MCDI_EVENT_CODE_PTP_FAULT:
583 case MCDI_EVENT_CODE_PTP_PPS:
584 efx_ptp_event(efx, event);
585 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000586
587 default:
Ben Hutchings62776d02010-06-23 11:30:07 +0000588 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
589 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000590 }
591}
592
593/**************************************************************************
594 *
595 * Specific request functions
596 *
597 **************************************************************************
598 */
599
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000600void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000601{
Ben Hutchings05a93202011-12-20 00:44:06 +0000602 u8 outbuf[ALIGN(MC_CMD_GET_VERSION_OUT_LEN, 4)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000603 size_t outlength;
604 const __le16 *ver_words;
605 int rc;
606
607 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
608
609 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
610 outbuf, sizeof(outbuf), &outlength);
611 if (rc)
612 goto fail;
613
Ben Hutchings05a93202011-12-20 00:44:06 +0000614 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000615 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000616 goto fail;
617 }
618
619 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000620 snprintf(buf, len, "%u.%u.%u.%u",
621 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
622 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
623 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000624
625fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000626 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000627 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000628}
629
630int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
631 bool *was_attached)
632{
633 u8 inbuf[MC_CMD_DRV_ATTACH_IN_LEN];
634 u8 outbuf[MC_CMD_DRV_ATTACH_OUT_LEN];
635 size_t outlen;
636 int rc;
637
638 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
639 driver_operating ? 1 : 0);
640 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
641
642 rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
643 outbuf, sizeof(outbuf), &outlen);
644 if (rc)
645 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000646 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
647 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000648 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000649 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000650
651 if (was_attached != NULL)
652 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
653 return 0;
654
655fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000656 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000657 return rc;
658}
659
660int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +0100661 u16 *fw_subtype_list, u32 *capabilities)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000662{
Ben Hutchings05a93202011-12-20 00:44:06 +0000663 uint8_t outbuf[MC_CMD_GET_BOARD_CFG_OUT_LENMIN];
Ben Hutchingsbfeed902012-09-07 00:58:10 +0100664 size_t outlen, offset, i;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000665 int port_num = efx_port_num(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000666 int rc;
667
668 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
669
670 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
671 outbuf, sizeof(outbuf), &outlen);
672 if (rc)
673 goto fail;
674
Ben Hutchings05a93202011-12-20 00:44:06 +0000675 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000676 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000677 goto fail;
678 }
679
680 offset = (port_num)
681 ? MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST
682 : MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST;
683 if (mac_address)
684 memcpy(mac_address, outbuf + offset, ETH_ALEN);
Ben Hutchingsbfeed902012-09-07 00:58:10 +0100685 if (fw_subtype_list) {
686 offset = MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST;
687 for (i = 0;
688 i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MINNUM;
689 i++) {
690 fw_subtype_list[i] =
691 le16_to_cpup((__le16 *)(outbuf + offset));
692 offset += 2;
693 }
694 }
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +0100695 if (capabilities) {
696 if (port_num)
697 *capabilities = MCDI_DWORD(outbuf,
698 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
699 else
700 *capabilities = MCDI_DWORD(outbuf,
701 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
702 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000703
704 return 0;
705
706fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000707 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
708 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000709
710 return rc;
711}
712
713int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
714{
715 u8 inbuf[MC_CMD_LOG_CTRL_IN_LEN];
716 u32 dest = 0;
717 int rc;
718
719 if (uart)
720 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
721 if (evq)
722 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
723
724 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
725 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
726
727 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
728
729 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
730 NULL, 0, NULL);
731 if (rc)
732 goto fail;
733
734 return 0;
735
736fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000737 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000738 return rc;
739}
740
741int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
742{
743 u8 outbuf[MC_CMD_NVRAM_TYPES_OUT_LEN];
744 size_t outlen;
745 int rc;
746
747 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
748
749 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
750 outbuf, sizeof(outbuf), &outlen);
751 if (rc)
752 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000753 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
754 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000755 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000756 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000757
758 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
759 return 0;
760
761fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000762 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
763 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000764 return rc;
765}
766
767int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
768 size_t *size_out, size_t *erase_size_out,
769 bool *protected_out)
770{
771 u8 inbuf[MC_CMD_NVRAM_INFO_IN_LEN];
772 u8 outbuf[MC_CMD_NVRAM_INFO_OUT_LEN];
773 size_t outlen;
774 int rc;
775
776 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
777
778 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
779 outbuf, sizeof(outbuf), &outlen);
780 if (rc)
781 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000782 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
783 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000784 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000785 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000786
787 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
788 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
789 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +0000790 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000791 return 0;
792
793fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000794 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000795 return rc;
796}
797
798int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
799{
800 u8 inbuf[MC_CMD_NVRAM_UPDATE_START_IN_LEN];
801 int rc;
802
803 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
804
805 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
806
807 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
808 NULL, 0, NULL);
809 if (rc)
810 goto fail;
811
812 return 0;
813
814fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000815 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000816 return rc;
817}
818
819int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
820 loff_t offset, u8 *buffer, size_t length)
821{
822 u8 inbuf[MC_CMD_NVRAM_READ_IN_LEN];
Ben Hutchings5a27e862010-01-25 15:49:59 -0800823 u8 outbuf[MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000824 size_t outlen;
825 int rc;
826
827 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
828 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
829 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
830
831 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
832 outbuf, sizeof(outbuf), &outlen);
833 if (rc)
834 goto fail;
835
836 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
837 return 0;
838
839fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000840 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000841 return rc;
842}
843
844int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
845 loff_t offset, const u8 *buffer, size_t length)
846{
Ben Hutchings5a27e862010-01-25 15:49:59 -0800847 u8 inbuf[MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000848 int rc;
849
850 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
851 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
852 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
853 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
854
855 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
856
Ben Hutchings5a27e862010-01-25 15:49:59 -0800857 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
858 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000859 NULL, 0, NULL);
860 if (rc)
861 goto fail;
862
863 return 0;
864
865fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000866 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000867 return rc;
868}
869
870int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
871 loff_t offset, size_t length)
872{
873 u8 inbuf[MC_CMD_NVRAM_ERASE_IN_LEN];
874 int rc;
875
876 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
877 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
878 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
879
880 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
881
882 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
883 NULL, 0, NULL);
884 if (rc)
885 goto fail;
886
887 return 0;
888
889fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000890 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000891 return rc;
892}
893
894int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
895{
896 u8 inbuf[MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN];
897 int rc;
898
899 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
900
901 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
902
903 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
904 NULL, 0, NULL);
905 if (rc)
906 goto fail;
907
908 return 0;
909
910fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000911 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000912 return rc;
913}
914
Ben Hutchings2e803402010-02-03 09:31:01 +0000915static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
916{
917 u8 inbuf[MC_CMD_NVRAM_TEST_IN_LEN];
918 u8 outbuf[MC_CMD_NVRAM_TEST_OUT_LEN];
919 int rc;
920
921 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
922
923 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
924 outbuf, sizeof(outbuf), NULL);
925 if (rc)
926 return rc;
927
928 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
929 case MC_CMD_NVRAM_TEST_PASS:
930 case MC_CMD_NVRAM_TEST_NOTSUPP:
931 return 0;
932 default:
933 return -EIO;
934 }
935}
936
937int efx_mcdi_nvram_test_all(struct efx_nic *efx)
938{
939 u32 nvram_types;
940 unsigned int type;
941 int rc;
942
943 rc = efx_mcdi_nvram_types(efx, &nvram_types);
944 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000945 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +0000946
947 type = 0;
948 while (nvram_types != 0) {
949 if (nvram_types & 1) {
950 rc = efx_mcdi_nvram_test(efx, type);
951 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000952 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +0000953 }
954 type++;
955 nvram_types >>= 1;
956 }
957
958 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +0000959
960fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +0000961 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
962 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000963fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +0000964 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000965 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +0000966}
967
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000968static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000969{
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000970 u8 inbuf[MC_CMD_GET_ASSERTS_IN_LEN];
971 u8 outbuf[MC_CMD_GET_ASSERTS_OUT_LEN];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000972 unsigned int flags, index, ofst;
973 const char *reason;
974 size_t outlen;
975 int retry;
976 int rc;
977
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000978 /* Attempt to read any stored assertion state before we reboot
979 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000980 * because a boot-time assertion might cause this command to fail
981 * with EINTR. And once again because GET_ASSERTS can race with
982 * MC_CMD_REBOOT running on the other port. */
983 retry = 2;
984 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000985 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000986 rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS,
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000987 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
988 outbuf, sizeof(outbuf), &outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000989 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
990
991 if (rc)
992 return rc;
993 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000994 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000995
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000996 /* Print out any recorded assertion state */
997 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000998 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
999 return 0;
1000
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001001 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1002 ? "system-level assertion"
1003 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1004 ? "thread-level assertion"
1005 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1006 ? "watchdog reset"
1007 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +00001008 netif_err(efx, hw, efx->net_dev,
1009 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1010 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1011 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001012
1013 /* Print out the registers */
1014 ofst = MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_OFST;
1015 for (index = 1; index < 32; index++) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001016 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n", index,
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001017 MCDI_DWORD2(outbuf, ofst));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001018 ofst += sizeof(efx_dword_t);
1019 }
1020
1021 return 0;
1022}
1023
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001024static void efx_mcdi_exit_assertion(struct efx_nic *efx)
1025{
1026 u8 inbuf[MC_CMD_REBOOT_IN_LEN];
1027
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001028 /* If the MC is running debug firmware, it might now be
1029 * waiting for a debugger to attach, but we just want it to
1030 * reboot. We set a flag that makes the command a no-op if it
1031 * has already done so. We don't know what return code to
1032 * expect (0 or -EIO), so ignore it.
1033 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001034 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1035 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1036 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001037 (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1038 NULL, 0, NULL);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001039}
1040
1041int efx_mcdi_handle_assertion(struct efx_nic *efx)
1042{
1043 int rc;
1044
1045 rc = efx_mcdi_read_assertion(efx);
1046 if (rc)
1047 return rc;
1048
1049 efx_mcdi_exit_assertion(efx);
1050
1051 return 0;
1052}
1053
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001054void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1055{
1056 u8 inbuf[MC_CMD_SET_ID_LED_IN_LEN];
1057 int rc;
1058
1059 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1060 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1061 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1062
1063 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1064
1065 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1066
1067 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1068 NULL, 0, NULL);
1069 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001070 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1071 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001072}
1073
1074int efx_mcdi_reset_port(struct efx_nic *efx)
1075{
Ben Hutchings05a93202011-12-20 00:44:06 +00001076 int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001077 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001078 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1079 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001080 return rc;
1081}
1082
1083int efx_mcdi_reset_mc(struct efx_nic *efx)
1084{
1085 u8 inbuf[MC_CMD_REBOOT_IN_LEN];
1086 int rc;
1087
1088 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1089 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1090 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1091 NULL, 0, NULL);
1092 /* White is black, and up is down */
1093 if (rc == -EIO)
1094 return 0;
1095 if (rc == 0)
1096 rc = -EIO;
Ben Hutchings62776d02010-06-23 11:30:07 +00001097 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001098 return rc;
1099}
1100
stephen hemmingerd2156972010-10-18 05:27:31 +00001101static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1102 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001103{
1104 u8 inbuf[MC_CMD_WOL_FILTER_SET_IN_LEN];
1105 u8 outbuf[MC_CMD_WOL_FILTER_SET_OUT_LEN];
1106 size_t outlen;
1107 int rc;
1108
1109 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1110 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1111 MC_CMD_FILTER_MODE_SIMPLE);
1112 memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN);
1113
1114 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1115 outbuf, sizeof(outbuf), &outlen);
1116 if (rc)
1117 goto fail;
1118
1119 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001120 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001121 goto fail;
1122 }
1123
1124 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1125
1126 return 0;
1127
1128fail:
1129 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001130 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001131 return rc;
1132
1133}
1134
1135
1136int
1137efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1138{
1139 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1140}
1141
1142
1143int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1144{
1145 u8 outbuf[MC_CMD_WOL_FILTER_GET_OUT_LEN];
1146 size_t outlen;
1147 int rc;
1148
1149 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1150 outbuf, sizeof(outbuf), &outlen);
1151 if (rc)
1152 goto fail;
1153
1154 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001155 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001156 goto fail;
1157 }
1158
1159 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1160
1161 return 0;
1162
1163fail:
1164 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001166 return rc;
1167}
1168
1169
1170int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1171{
1172 u8 inbuf[MC_CMD_WOL_FILTER_REMOVE_IN_LEN];
1173 int rc;
1174
1175 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1176
1177 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1178 NULL, 0, NULL);
1179 if (rc)
1180 goto fail;
1181
1182 return 0;
1183
1184fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001185 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001186 return rc;
1187}
1188
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001189int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1190{
1191 struct efx_channel *channel;
1192 struct efx_rx_queue *rx_queue;
1193 __le32 *qid;
1194 int rc, count;
1195
1196 qid = kmalloc(EFX_MAX_CHANNELS * sizeof(*qid), GFP_KERNEL);
1197 if (qid == NULL)
1198 return -ENOMEM;
1199
1200 count = 0;
1201 efx_for_each_channel(channel, efx) {
1202 efx_for_each_channel_rx_queue(rx_queue, channel) {
1203 if (rx_queue->flush_pending) {
1204 rx_queue->flush_pending = false;
1205 atomic_dec(&efx->rxq_flush_pending);
1206 qid[count++] = cpu_to_le32(
1207 efx_rx_queue_index(rx_queue));
1208 }
1209 }
1210 }
1211
1212 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, (u8 *)qid,
1213 count * sizeof(*qid), NULL, 0, NULL);
1214 WARN_ON(rc > 0);
1215
1216 kfree(qid);
1217
1218 return rc;
1219}
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001220
1221int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1222{
1223 int rc;
1224
1225 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1226 if (rc)
1227 goto fail;
1228
1229 return 0;
1230
1231fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001232 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001233 return rc;
1234}
1235