blob: f16145d9281757ffcece8d578b8ca905e0c9a164 [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
25/* Software-defined structure to the shared-memory */
26#define CMD_NOTIFY_PORT0 0
27#define CMD_NOTIFY_PORT1 4
28#define CMD_PDU_PORT0 0x008
29#define CMD_PDU_PORT1 0x108
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000030
31#define MCDI_RPC_TIMEOUT 10 /*seconds */
32
33#define MCDI_PDU(efx) \
34 (efx_port_num(efx) ? CMD_PDU_PORT1 : CMD_PDU_PORT0)
35#define MCDI_DOORBELL(efx) \
36 (efx_port_num(efx) ? CMD_NOTIFY_PORT1 : CMD_NOTIFY_PORT0)
Ben Hutchings3f713bf2011-12-20 23:39:31 +000037#define MCDI_STATUS(efx) \
38 (efx_port_num(efx) ? MC_SMEM_P1_STATUS_OFST : MC_SMEM_P0_STATUS_OFST)
39
40/* A reboot/assertion causes the MCDI status word to be set after the
41 * command word is set or a REBOOT event is sent. If we notice a reboot
42 * via these mechanisms then wait 10ms for the status word to be set. */
43#define MCDI_STATUS_DELAY_US 100
44#define MCDI_STATUS_DELAY_COUNT 100
45#define MCDI_STATUS_SLEEP_MS \
46 (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000047
48#define SEQ_MASK \
49 EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
50
51static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
52{
53 struct siena_nic_data *nic_data;
54 EFX_BUG_ON_PARANOID(efx_nic_rev(efx) < EFX_REV_SIENA_A0);
55 nic_data = efx->nic_data;
56 return &nic_data->mcdi;
57}
58
59void efx_mcdi_init(struct efx_nic *efx)
60{
61 struct efx_mcdi_iface *mcdi;
62
63 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
64 return;
65
66 mcdi = efx_mcdi(efx);
67 init_waitqueue_head(&mcdi->wq);
68 spin_lock_init(&mcdi->iface_lock);
69 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
70 mcdi->mode = MCDI_MODE_POLL;
71
72 (void) efx_mcdi_poll_reboot(efx);
73}
74
75static void efx_mcdi_copyin(struct efx_nic *efx, unsigned cmd,
76 const u8 *inbuf, size_t inlen)
77{
78 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchings86c432c2011-09-01 12:09:29 +000079 unsigned pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
80 unsigned doorbell = FR_CZ_MC_TREG_SMEM + MCDI_DOORBELL(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000081 unsigned int i;
82 efx_dword_t hdr;
83 u32 xflags, seqno;
84
85 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
86 BUG_ON(inlen & 3 || inlen >= 0x100);
87
88 seqno = mcdi->seqno & SEQ_MASK;
89 xflags = 0;
90 if (mcdi->mode == MCDI_MODE_EVENTS)
91 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
92
93 EFX_POPULATE_DWORD_6(hdr,
94 MCDI_HEADER_RESPONSE, 0,
95 MCDI_HEADER_RESYNC, 1,
96 MCDI_HEADER_CODE, cmd,
97 MCDI_HEADER_DATALEN, inlen,
98 MCDI_HEADER_SEQ, seqno,
99 MCDI_HEADER_XFLAGS, xflags);
100
Ben Hutchings86c432c2011-09-01 12:09:29 +0000101 efx_writed(efx, &hdr, pdu);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000102
Ben Hutchings747df222011-05-11 17:41:18 +0100103 for (i = 0; i < inlen; i += 4)
Ben Hutchings86c432c2011-09-01 12:09:29 +0000104 _efx_writed(efx, *((__le32 *)(inbuf + i)), pdu + 4 + i);
105
106 /* Ensure the payload is written out before the header */
107 wmb();
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000108
109 /* ring the doorbell with a distinctive value */
Ben Hutchings86c432c2011-09-01 12:09:29 +0000110 _efx_writed(efx, (__force __le32) 0x45789abc, doorbell);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000111}
112
113static void efx_mcdi_copyout(struct efx_nic *efx, u8 *outbuf, size_t outlen)
114{
115 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchings86c432c2011-09-01 12:09:29 +0000116 unsigned int pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000117 int i;
118
119 BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
120 BUG_ON(outlen & 3 || outlen >= 0x100);
121
122 for (i = 0; i < outlen; i += 4)
Ben Hutchings86c432c2011-09-01 12:09:29 +0000123 *((__le32 *)(outbuf + i)) = _efx_readd(efx, pdu + 4 + i);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000124}
125
126static int efx_mcdi_poll(struct efx_nic *efx)
127{
128 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
129 unsigned int time, finish;
130 unsigned int respseq, respcmd, error;
Ben Hutchings86c432c2011-09-01 12:09:29 +0000131 unsigned int pdu = FR_CZ_MC_TREG_SMEM + MCDI_PDU(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000132 unsigned int rc, spins;
133 efx_dword_t reg;
134
135 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchingse0bf54c2010-02-19 13:29:27 +0000136 rc = -efx_mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000137 if (rc)
138 goto out;
139
140 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
141 * because generally mcdi responses are fast. After that, back off
142 * and poll once a jiffy (approximately)
143 */
144 spins = TICK_USEC;
145 finish = get_seconds() + MCDI_RPC_TIMEOUT;
146
147 while (1) {
148 if (spins != 0) {
149 --spins;
150 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000151 } else {
152 schedule_timeout_uninterruptible(1);
153 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000154
155 time = get_seconds();
156
Ben Hutchings86c432c2011-09-01 12:09:29 +0000157 rmb();
158 efx_readd(efx, &reg, pdu);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000159
160 /* All 1's indicates that shared memory is in reset (and is
161 * not a valid header). Wait for it to come out reset before
162 * completing the command */
163 if (EFX_DWORD_FIELD(reg, EFX_DWORD_0) != 0xffffffff &&
164 EFX_DWORD_FIELD(reg, MCDI_HEADER_RESPONSE))
165 break;
166
167 if (time >= finish)
168 return -ETIMEDOUT;
169 }
170
171 mcdi->resplen = EFX_DWORD_FIELD(reg, MCDI_HEADER_DATALEN);
172 respseq = EFX_DWORD_FIELD(reg, MCDI_HEADER_SEQ);
173 respcmd = EFX_DWORD_FIELD(reg, MCDI_HEADER_CODE);
174 error = EFX_DWORD_FIELD(reg, MCDI_HEADER_ERROR);
175
176 if (error && mcdi->resplen == 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000177 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000178 rc = EIO;
179 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000180 netif_err(efx, hw, efx->net_dev,
181 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
182 respseq, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000183 rc = EIO;
184 } else if (error) {
Ben Hutchings86c432c2011-09-01 12:09:29 +0000185 efx_readd(efx, &reg, pdu + 4);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000186 switch (EFX_DWORD_FIELD(reg, EFX_DWORD_0)) {
187#define TRANSLATE_ERROR(name) \
188 case MC_CMD_ERR_ ## name: \
189 rc = name; \
190 break
191 TRANSLATE_ERROR(ENOENT);
192 TRANSLATE_ERROR(EINTR);
193 TRANSLATE_ERROR(EACCES);
194 TRANSLATE_ERROR(EBUSY);
195 TRANSLATE_ERROR(EINVAL);
196 TRANSLATE_ERROR(EDEADLK);
197 TRANSLATE_ERROR(ENOSYS);
198 TRANSLATE_ERROR(ETIME);
199#undef TRANSLATE_ERROR
200 default:
201 rc = EIO;
202 break;
203 }
204 } else
205 rc = 0;
206
207out:
208 mcdi->resprc = rc;
209 if (rc)
210 mcdi->resplen = 0;
211
212 /* Return rc=0 like wait_event_timeout() */
213 return 0;
214}
215
216/* Test and clear MC-rebooted flag for this port/function */
217int efx_mcdi_poll_reboot(struct efx_nic *efx)
218{
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000219 unsigned int addr = FR_CZ_MC_TREG_SMEM + MCDI_STATUS(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000220 efx_dword_t reg;
221 uint32_t value;
222
223 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
224 return false;
225
Ben Hutchings86c432c2011-09-01 12:09:29 +0000226 efx_readd(efx, &reg, addr);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000227 value = EFX_DWORD_FIELD(reg, EFX_DWORD_0);
228
229 if (value == 0)
230 return 0;
231
232 EFX_ZERO_DWORD(reg);
Ben Hutchings86c432c2011-09-01 12:09:29 +0000233 efx_writed(efx, &reg, addr);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000234
235 if (value == MC_STATUS_DWORD_ASSERT)
236 return -EINTR;
237 else
238 return -EIO;
239}
240
241static void efx_mcdi_acquire(struct efx_mcdi_iface *mcdi)
242{
243 /* Wait until the interface becomes QUIESCENT and we win the race
244 * to mark it RUNNING. */
245 wait_event(mcdi->wq,
246 atomic_cmpxchg(&mcdi->state,
247 MCDI_STATE_QUIESCENT,
248 MCDI_STATE_RUNNING)
249 == MCDI_STATE_QUIESCENT);
250}
251
252static int efx_mcdi_await_completion(struct efx_nic *efx)
253{
254 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
255
256 if (wait_event_timeout(
257 mcdi->wq,
258 atomic_read(&mcdi->state) == MCDI_STATE_COMPLETED,
259 msecs_to_jiffies(MCDI_RPC_TIMEOUT * 1000)) == 0)
260 return -ETIMEDOUT;
261
262 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
263 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
264 * completed the request first, then we'll just end up completing the
265 * request again, which is safe.
266 *
267 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
268 * wait_event_timeout() implicitly provides.
269 */
270 if (mcdi->mode == MCDI_MODE_POLL)
271 return efx_mcdi_poll(efx);
272
273 return 0;
274}
275
276static bool efx_mcdi_complete(struct efx_mcdi_iface *mcdi)
277{
278 /* If the interface is RUNNING, then move to COMPLETED and wake any
279 * waiters. If the interface isn't in RUNNING then we've received a
280 * duplicate completion after we've already transitioned back to
281 * QUIESCENT. [A subsequent invocation would increment seqno, so would
282 * have failed the seqno check].
283 */
284 if (atomic_cmpxchg(&mcdi->state,
285 MCDI_STATE_RUNNING,
286 MCDI_STATE_COMPLETED) == MCDI_STATE_RUNNING) {
287 wake_up(&mcdi->wq);
288 return true;
289 }
290
291 return false;
292}
293
294static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
295{
296 atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
297 wake_up(&mcdi->wq);
298}
299
300static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
301 unsigned int datalen, unsigned int errno)
302{
303 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
304 bool wake = false;
305
306 spin_lock(&mcdi->iface_lock);
307
308 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
309 if (mcdi->credits)
310 /* The request has been cancelled */
311 --mcdi->credits;
312 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000313 netif_err(efx, hw, efx->net_dev,
314 "MC response mismatch tx seq 0x%x rx "
315 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000316 } else {
317 mcdi->resprc = errno;
318 mcdi->resplen = datalen;
319
320 wake = true;
321 }
322
323 spin_unlock(&mcdi->iface_lock);
324
325 if (wake)
326 efx_mcdi_complete(mcdi);
327}
328
329/* Issue the given command by writing the data into the shared memory PDU,
330 * ring the doorbell and wait for completion. Copyout the result. */
331int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
332 const u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen,
333 size_t *outlen_actual)
334{
335 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
336 int rc;
337 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);
347
348 if (mcdi->mode == MCDI_MODE_POLL)
349 rc = efx_mcdi_poll(efx);
350 else
351 rc = efx_mcdi_await_completion(efx);
352
353 if (rc != 0) {
354 /* Close the race with efx_mcdi_ev_cpl() executing just too late
355 * and completing a request we've just cancelled, by ensuring
356 * that the seqno check therein fails.
357 */
358 spin_lock_bh(&mcdi->iface_lock);
359 ++mcdi->seqno;
360 ++mcdi->credits;
361 spin_unlock_bh(&mcdi->iface_lock);
362
Ben Hutchings62776d02010-06-23 11:30:07 +0000363 netif_err(efx, hw, efx->net_dev,
364 "MC command 0x%x inlen %d mode %d timed out\n",
365 cmd, (int)inlen, mcdi->mode);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000366 } else {
367 size_t resplen;
368
369 /* At the very least we need a memory barrier here to ensure
370 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
371 * a spurious efx_mcdi_ev_cpl() running concurrently by
372 * acquiring the iface_lock. */
373 spin_lock_bh(&mcdi->iface_lock);
374 rc = -mcdi->resprc;
375 resplen = mcdi->resplen;
376 spin_unlock_bh(&mcdi->iface_lock);
377
378 if (rc == 0) {
379 efx_mcdi_copyout(efx, outbuf,
380 min(outlen, mcdi->resplen + 3) & ~0x3);
381 if (outlen_actual != NULL)
382 *outlen_actual = resplen;
383 } else if (cmd == MC_CMD_REBOOT && rc == -EIO)
384 ; /* Don't reset if MC_CMD_REBOOT returns EIO */
385 else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000386 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
387 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000388 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
389 } else
Ben Hutchingsf18ca362010-12-02 13:46:09 +0000390 netif_dbg(efx, hw, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000391 "MC command 0x%x inlen %d failed rc=%d\n",
392 cmd, (int)inlen, -rc);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000393
394 if (rc == -EIO || rc == -EINTR) {
395 msleep(MCDI_STATUS_SLEEP_MS);
396 efx_mcdi_poll_reboot(efx);
397 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000398 }
399
400 efx_mcdi_release(mcdi);
401 return rc;
402}
403
404void efx_mcdi_mode_poll(struct efx_nic *efx)
405{
406 struct efx_mcdi_iface *mcdi;
407
408 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
409 return;
410
411 mcdi = efx_mcdi(efx);
412 if (mcdi->mode == MCDI_MODE_POLL)
413 return;
414
415 /* We can switch from event completion to polled completion, because
416 * mcdi requests are always completed in shared memory. We do this by
417 * switching the mode to POLL'd then completing the request.
418 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
419 *
420 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
421 * which efx_mcdi_complete() provides for us.
422 */
423 mcdi->mode = MCDI_MODE_POLL;
424
425 efx_mcdi_complete(mcdi);
426}
427
428void efx_mcdi_mode_event(struct efx_nic *efx)
429{
430 struct efx_mcdi_iface *mcdi;
431
432 if (efx_nic_rev(efx) < EFX_REV_SIENA_A0)
433 return;
434
435 mcdi = efx_mcdi(efx);
436
437 if (mcdi->mode == MCDI_MODE_EVENTS)
438 return;
439
440 /* We can't switch from polled to event completion in the middle of a
441 * request, because the completion method is specified in the request.
442 * So acquire the interface to serialise the requestors. We don't need
443 * to acquire the iface_lock to change the mode here, but we do need a
444 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
445 * efx_mcdi_acquire() provides.
446 */
447 efx_mcdi_acquire(mcdi);
448 mcdi->mode = MCDI_MODE_EVENTS;
449 efx_mcdi_release(mcdi);
450}
451
452static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
453{
454 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
455
456 /* If there is an outstanding MCDI request, it has been terminated
457 * either by a BADASSERT or REBOOT event. If the mcdi interface is
458 * in polled mode, then do nothing because the MC reboot handler will
459 * set the header correctly. However, if the mcdi interface is waiting
460 * for a CMDDONE event it won't receive it [and since all MCDI events
461 * are sent to the same queue, we can't be racing with
462 * efx_mcdi_ev_cpl()]
463 *
464 * There's a race here with efx_mcdi_rpc(), because we might receive
465 * a REBOOT event *before* the request has been copied out. In polled
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300466 * mode (during startup) this is irrelevant, because efx_mcdi_complete()
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000467 * is ignored. In event mode, this condition is just an edge-case of
468 * receiving a REBOOT event after posting the MCDI request. Did the mc
469 * reboot before or after the copyout? The best we can do always is
470 * just return failure.
471 */
472 spin_lock(&mcdi->iface_lock);
473 if (efx_mcdi_complete(mcdi)) {
474 if (mcdi->mode == MCDI_MODE_EVENTS) {
475 mcdi->resprc = rc;
476 mcdi->resplen = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +0000477 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000478 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000479 } else {
480 int count;
481
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000482 /* Nobody was waiting for an MCDI request, so trigger a reset */
483 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
484
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000485 /* Consume the status word since efx_mcdi_rpc_finish() won't */
486 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
487 if (efx_mcdi_poll_reboot(efx))
488 break;
489 udelay(MCDI_STATUS_DELAY_US);
490 }
491 }
492
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000493 spin_unlock(&mcdi->iface_lock);
494}
495
496static unsigned int efx_mcdi_event_link_speed[] = {
497 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
498 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
499 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
500};
501
502
503static void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
504{
505 u32 flags, fcntl, speed, lpa;
506
507 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
508 EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
509 speed = efx_mcdi_event_link_speed[speed];
510
511 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
512 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
513 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
514
515 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
516 * which is only run after flushing the event queues. Therefore, it
517 * is safe to modify the link state outside of the mac_lock here.
518 */
519 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
520
521 efx_mcdi_phy_check_fcntl(efx, lpa);
522
523 efx_link_status_changed(efx);
524}
525
Ben Hutchings18e83e42012-01-05 19:05:20 +0000526static const char *const sensor_names[] = {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000527 [MC_CMD_SENSOR_CONTROLLER_TEMP] = "Controller temp. sensor",
528 [MC_CMD_SENSOR_PHY_COMMON_TEMP] = "PHY shared temp. sensor",
529 [MC_CMD_SENSOR_CONTROLLER_COOLING] = "Controller cooling",
530 [MC_CMD_SENSOR_PHY0_TEMP] = "PHY 0 temp. sensor",
531 [MC_CMD_SENSOR_PHY0_COOLING] = "PHY 0 cooling",
532 [MC_CMD_SENSOR_PHY1_TEMP] = "PHY 1 temp. sensor",
533 [MC_CMD_SENSOR_PHY1_COOLING] = "PHY 1 cooling",
534 [MC_CMD_SENSOR_IN_1V0] = "1.0V supply sensor",
535 [MC_CMD_SENSOR_IN_1V2] = "1.2V supply sensor",
536 [MC_CMD_SENSOR_IN_1V8] = "1.8V supply sensor",
537 [MC_CMD_SENSOR_IN_2V5] = "2.5V supply sensor",
538 [MC_CMD_SENSOR_IN_3V3] = "3.3V supply sensor",
539 [MC_CMD_SENSOR_IN_12V0] = "12V supply sensor"
540};
541
Ben Hutchings18e83e42012-01-05 19:05:20 +0000542static const char *const sensor_status_names[] = {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000543 [MC_CMD_SENSOR_STATE_OK] = "OK",
544 [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
545 [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
546 [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
547};
548
549static void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
550{
551 unsigned int monitor, state, value;
552 const char *name, *state_txt;
553 monitor = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
554 state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
555 value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
556 /* Deal gracefully with the board having more drivers than we
557 * know about, but do not expect new sensor states. */
558 name = (monitor >= ARRAY_SIZE(sensor_names))
559 ? "No sensor name available" :
560 sensor_names[monitor];
561 EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
562 state_txt = sensor_status_names[state];
563
Ben Hutchings62776d02010-06-23 11:30:07 +0000564 netif_err(efx, hw, efx->net_dev,
565 "Sensor %d (%s) reports condition '%s' for raw value %d\n",
566 monitor, name, state_txt, value);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000567}
568
569/* Called from falcon_process_eventq for MCDI events */
570void efx_mcdi_process_event(struct efx_channel *channel,
571 efx_qword_t *event)
572{
573 struct efx_nic *efx = channel->efx;
574 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
575 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
576
577 switch (code) {
578 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000579 netif_err(efx, hw, efx->net_dev,
580 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000581 efx_mcdi_ev_death(efx, EINTR);
582 break;
583
584 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +0000585 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000586 break;
587
588 case MCDI_EVENT_CODE_CMDDONE:
589 efx_mcdi_ev_cpl(efx,
590 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
591 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
592 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
593 break;
594
595 case MCDI_EVENT_CODE_LINKCHANGE:
596 efx_mcdi_process_link_change(efx, event);
597 break;
598 case MCDI_EVENT_CODE_SENSOREVT:
599 efx_mcdi_sensor_event(efx, event);
600 break;
601 case MCDI_EVENT_CODE_SCHEDERR:
Ben Hutchings62776d02010-06-23 11:30:07 +0000602 netif_info(efx, hw, efx->net_dev,
603 "MC Scheduler error address=0x%x\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000604 break;
605 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000606 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000607 efx_mcdi_ev_death(efx, EIO);
608 break;
609 case MCDI_EVENT_CODE_MAC_STATS_DMA:
610 /* MAC stats are gather lazily. We can ignore this. */
611 break;
612
613 default:
Ben Hutchings62776d02010-06-23 11:30:07 +0000614 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
615 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000616 }
617}
618
619/**************************************************************************
620 *
621 * Specific request functions
622 *
623 **************************************************************************
624 */
625
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000626void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000627{
Ben Hutchings05a93202011-12-20 00:44:06 +0000628 u8 outbuf[ALIGN(MC_CMD_GET_VERSION_OUT_LEN, 4)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000629 size_t outlength;
630 const __le16 *ver_words;
631 int rc;
632
633 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
634
635 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
636 outbuf, sizeof(outbuf), &outlength);
637 if (rc)
638 goto fail;
639
Ben Hutchings05a93202011-12-20 00:44:06 +0000640 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000641 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000642 goto fail;
643 }
644
645 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000646 snprintf(buf, len, "%u.%u.%u.%u",
647 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
648 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
649 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000650
651fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000652 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000653 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000654}
655
656int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
657 bool *was_attached)
658{
659 u8 inbuf[MC_CMD_DRV_ATTACH_IN_LEN];
660 u8 outbuf[MC_CMD_DRV_ATTACH_OUT_LEN];
661 size_t outlen;
662 int rc;
663
664 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
665 driver_operating ? 1 : 0);
666 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
667
668 rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
669 outbuf, sizeof(outbuf), &outlen);
670 if (rc)
671 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000672 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
673 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000674 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000675 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000676
677 if (was_attached != NULL)
678 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
679 return 0;
680
681fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000682 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000683 return rc;
684}
685
686int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
687 u16 *fw_subtype_list)
688{
Ben Hutchings05a93202011-12-20 00:44:06 +0000689 uint8_t outbuf[MC_CMD_GET_BOARD_CFG_OUT_LENMIN];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000690 size_t outlen;
691 int port_num = efx_port_num(efx);
692 int offset;
693 int rc;
694
695 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
696
697 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
698 outbuf, sizeof(outbuf), &outlen);
699 if (rc)
700 goto fail;
701
Ben Hutchings05a93202011-12-20 00:44:06 +0000702 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000703 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000704 goto fail;
705 }
706
707 offset = (port_num)
708 ? MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST
709 : MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST;
710 if (mac_address)
711 memcpy(mac_address, outbuf + offset, ETH_ALEN);
712 if (fw_subtype_list)
713 memcpy(fw_subtype_list,
714 outbuf + MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST,
Ben Hutchings05a93202011-12-20 00:44:06 +0000715 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MINNUM *
716 sizeof(fw_subtype_list[0]));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000717
718 return 0;
719
720fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000721 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
722 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000723
724 return rc;
725}
726
727int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
728{
729 u8 inbuf[MC_CMD_LOG_CTRL_IN_LEN];
730 u32 dest = 0;
731 int rc;
732
733 if (uart)
734 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
735 if (evq)
736 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
737
738 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
739 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
740
741 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
742
743 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
744 NULL, 0, NULL);
745 if (rc)
746 goto fail;
747
748 return 0;
749
750fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000751 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000752 return rc;
753}
754
755int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
756{
757 u8 outbuf[MC_CMD_NVRAM_TYPES_OUT_LEN];
758 size_t outlen;
759 int rc;
760
761 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
762
763 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
764 outbuf, sizeof(outbuf), &outlen);
765 if (rc)
766 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000767 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
768 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000769 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000770 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000771
772 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
773 return 0;
774
775fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000776 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
777 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000778 return rc;
779}
780
781int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
782 size_t *size_out, size_t *erase_size_out,
783 bool *protected_out)
784{
785 u8 inbuf[MC_CMD_NVRAM_INFO_IN_LEN];
786 u8 outbuf[MC_CMD_NVRAM_INFO_OUT_LEN];
787 size_t outlen;
788 int rc;
789
790 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
791
792 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
793 outbuf, sizeof(outbuf), &outlen);
794 if (rc)
795 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000796 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
797 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000798 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000799 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000800
801 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
802 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
803 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +0000804 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000805 return 0;
806
807fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000808 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000809 return rc;
810}
811
812int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
813{
814 u8 inbuf[MC_CMD_NVRAM_UPDATE_START_IN_LEN];
815 int rc;
816
817 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
818
819 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
820
821 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
822 NULL, 0, NULL);
823 if (rc)
824 goto fail;
825
826 return 0;
827
828fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000829 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000830 return rc;
831}
832
833int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
834 loff_t offset, u8 *buffer, size_t length)
835{
836 u8 inbuf[MC_CMD_NVRAM_READ_IN_LEN];
Ben Hutchings5a27e862010-01-25 15:49:59 -0800837 u8 outbuf[MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000838 size_t outlen;
839 int rc;
840
841 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
842 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
843 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
844
845 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
846 outbuf, sizeof(outbuf), &outlen);
847 if (rc)
848 goto fail;
849
850 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
851 return 0;
852
853fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000854 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000855 return rc;
856}
857
858int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
859 loff_t offset, const u8 *buffer, size_t length)
860{
Ben Hutchings5a27e862010-01-25 15:49:59 -0800861 u8 inbuf[MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX)];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000862 int rc;
863
864 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
865 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
866 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
867 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
868
869 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
870
Ben Hutchings5a27e862010-01-25 15:49:59 -0800871 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
872 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000873 NULL, 0, NULL);
874 if (rc)
875 goto fail;
876
877 return 0;
878
879fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000880 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000881 return rc;
882}
883
884int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
885 loff_t offset, size_t length)
886{
887 u8 inbuf[MC_CMD_NVRAM_ERASE_IN_LEN];
888 int rc;
889
890 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
891 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
892 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
893
894 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
895
896 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
897 NULL, 0, NULL);
898 if (rc)
899 goto fail;
900
901 return 0;
902
903fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000904 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000905 return rc;
906}
907
908int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
909{
910 u8 inbuf[MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN];
911 int rc;
912
913 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
914
915 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
916
917 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
918 NULL, 0, NULL);
919 if (rc)
920 goto fail;
921
922 return 0;
923
924fail:
Ben Hutchings62776d02010-06-23 11:30:07 +0000925 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000926 return rc;
927}
928
Ben Hutchings2e803402010-02-03 09:31:01 +0000929static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
930{
931 u8 inbuf[MC_CMD_NVRAM_TEST_IN_LEN];
932 u8 outbuf[MC_CMD_NVRAM_TEST_OUT_LEN];
933 int rc;
934
935 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
936
937 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
938 outbuf, sizeof(outbuf), NULL);
939 if (rc)
940 return rc;
941
942 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
943 case MC_CMD_NVRAM_TEST_PASS:
944 case MC_CMD_NVRAM_TEST_NOTSUPP:
945 return 0;
946 default:
947 return -EIO;
948 }
949}
950
951int efx_mcdi_nvram_test_all(struct efx_nic *efx)
952{
953 u32 nvram_types;
954 unsigned int type;
955 int rc;
956
957 rc = efx_mcdi_nvram_types(efx, &nvram_types);
958 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000959 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +0000960
961 type = 0;
962 while (nvram_types != 0) {
963 if (nvram_types & 1) {
964 rc = efx_mcdi_nvram_test(efx, type);
965 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +0000966 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +0000967 }
968 type++;
969 nvram_types >>= 1;
970 }
971
972 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +0000973
974fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +0000975 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
976 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000977fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +0000978 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +0000979 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +0000980}
981
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000982static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000983{
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000984 u8 inbuf[MC_CMD_GET_ASSERTS_IN_LEN];
985 u8 outbuf[MC_CMD_GET_ASSERTS_OUT_LEN];
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000986 unsigned int flags, index, ofst;
987 const char *reason;
988 size_t outlen;
989 int retry;
990 int rc;
991
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000992 /* Attempt to read any stored assertion state before we reboot
993 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000994 * because a boot-time assertion might cause this command to fail
995 * with EINTR. And once again because GET_ASSERTS can race with
996 * MC_CMD_REBOOT running on the other port. */
997 retry = 2;
998 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +0000999 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001000 rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS,
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001001 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1002 outbuf, sizeof(outbuf), &outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001003 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1004
1005 if (rc)
1006 return rc;
1007 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001008 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001009
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001010 /* Print out any recorded assertion state */
1011 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001012 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1013 return 0;
1014
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001015 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1016 ? "system-level assertion"
1017 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1018 ? "thread-level assertion"
1019 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1020 ? "watchdog reset"
1021 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +00001022 netif_err(efx, hw, efx->net_dev,
1023 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1024 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1025 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001026
1027 /* Print out the registers */
1028 ofst = MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_OFST;
1029 for (index = 1; index < 32; index++) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001030 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n", index,
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001031 MCDI_DWORD2(outbuf, ofst));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001032 ofst += sizeof(efx_dword_t);
1033 }
1034
1035 return 0;
1036}
1037
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001038static void efx_mcdi_exit_assertion(struct efx_nic *efx)
1039{
1040 u8 inbuf[MC_CMD_REBOOT_IN_LEN];
1041
1042 /* Atomically reboot the mcfw out of the assertion handler */
1043 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1044 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1045 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
1046 efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1047 NULL, 0, NULL);
1048}
1049
1050int efx_mcdi_handle_assertion(struct efx_nic *efx)
1051{
1052 int rc;
1053
1054 rc = efx_mcdi_read_assertion(efx);
1055 if (rc)
1056 return rc;
1057
1058 efx_mcdi_exit_assertion(efx);
1059
1060 return 0;
1061}
1062
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001063void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1064{
1065 u8 inbuf[MC_CMD_SET_ID_LED_IN_LEN];
1066 int rc;
1067
1068 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1069 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1070 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1071
1072 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1073
1074 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1075
1076 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1077 NULL, 0, NULL);
1078 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001079 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1080 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001081}
1082
1083int efx_mcdi_reset_port(struct efx_nic *efx)
1084{
Ben Hutchings05a93202011-12-20 00:44:06 +00001085 int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001086 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001087 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1088 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001089 return rc;
1090}
1091
1092int efx_mcdi_reset_mc(struct efx_nic *efx)
1093{
1094 u8 inbuf[MC_CMD_REBOOT_IN_LEN];
1095 int rc;
1096
1097 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1098 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1099 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1100 NULL, 0, NULL);
1101 /* White is black, and up is down */
1102 if (rc == -EIO)
1103 return 0;
1104 if (rc == 0)
1105 rc = -EIO;
Ben Hutchings62776d02010-06-23 11:30:07 +00001106 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001107 return rc;
1108}
1109
stephen hemmingerd2156972010-10-18 05:27:31 +00001110static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1111 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001112{
1113 u8 inbuf[MC_CMD_WOL_FILTER_SET_IN_LEN];
1114 u8 outbuf[MC_CMD_WOL_FILTER_SET_OUT_LEN];
1115 size_t outlen;
1116 int rc;
1117
1118 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1119 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1120 MC_CMD_FILTER_MODE_SIMPLE);
1121 memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN);
1122
1123 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1124 outbuf, sizeof(outbuf), &outlen);
1125 if (rc)
1126 goto fail;
1127
1128 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001129 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001130 goto fail;
1131 }
1132
1133 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1134
1135 return 0;
1136
1137fail:
1138 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001139 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001140 return rc;
1141
1142}
1143
1144
1145int
1146efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1147{
1148 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1149}
1150
1151
1152int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1153{
1154 u8 outbuf[MC_CMD_WOL_FILTER_GET_OUT_LEN];
1155 size_t outlen;
1156 int rc;
1157
1158 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1159 outbuf, sizeof(outbuf), &outlen);
1160 if (rc)
1161 goto fail;
1162
1163 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001164 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001165 goto fail;
1166 }
1167
1168 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1169
1170 return 0;
1171
1172fail:
1173 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001174 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001175 return rc;
1176}
1177
1178
1179int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1180{
1181 u8 inbuf[MC_CMD_WOL_FILTER_REMOVE_IN_LEN];
1182 int rc;
1183
1184 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1185
1186 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1187 NULL, 0, NULL);
1188 if (rc)
1189 goto fail;
1190
1191 return 0;
1192
1193fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001194 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001195 return rc;
1196}
1197
1198
1199int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1200{
1201 int rc;
1202
1203 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1204 if (rc)
1205 goto fail;
1206
1207 return 0;
1208
1209fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001210 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001211 return rc;
1212}
1213