blob: 7bd4b14bf3b32f627457eb1c48f6231922838670 [file] [log] [blame]
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
3 * Copyright 2008-2013 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>
Ben Hutchings251111d2013-08-27 23:04:29 +010011#include <asm/cmpxchg.h>
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000012#include "net_driver.h"
13#include "nic.h"
14#include "io.h"
Ben Hutchings8b8a95a2012-09-18 01:57:07 +010015#include "farch_regs.h"
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000016#include "mcdi_pcol.h"
17#include "phy.h"
18
19/**************************************************************************
20 *
21 * Management-Controller-to-Driver Interface
22 *
23 **************************************************************************
24 */
25
Ben Hutchingsebf98e72012-12-01 02:21:17 +000026#define MCDI_RPC_TIMEOUT (10 * HZ)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000027
Ben Hutchings3f713bf2011-12-20 23:39:31 +000028/* A reboot/assertion causes the MCDI status word to be set after the
29 * command word is set or a REBOOT event is sent. If we notice a reboot
Daniel Pieczkob2d32f02013-09-20 16:45:10 +010030 * via these mechanisms then wait 250ms for the status word to be set.
Daniel Pieczkod36a08b2013-06-20 11:40:07 +010031 */
Ben Hutchings3f713bf2011-12-20 23:39:31 +000032#define MCDI_STATUS_DELAY_US 100
Daniel Pieczkob2d32f02013-09-20 16:45:10 +010033#define MCDI_STATUS_DELAY_COUNT 2500
Ben Hutchings3f713bf2011-12-20 23:39:31 +000034#define MCDI_STATUS_SLEEP_MS \
35 (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000036
37#define SEQ_MASK \
38 EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
39
Ben Hutchingscade7152013-08-27 23:12:31 +010040struct efx_mcdi_async_param {
41 struct list_head list;
42 unsigned int cmd;
43 size_t inlen;
44 size_t outlen;
Edward Cree1e0b8122013-05-31 18:36:12 +010045 bool quiet;
Ben Hutchingscade7152013-08-27 23:12:31 +010046 efx_mcdi_async_completer *complete;
47 unsigned long cookie;
48 /* followed by request/response buffer */
49};
50
51static void efx_mcdi_timeout_async(unsigned long context);
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010052static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
53 bool *was_attached_out);
Robert Stonehouse5731d7b2013-10-09 11:52:43 +010054static bool efx_mcdi_poll_once(struct efx_nic *efx);
Ben Hutchingscade7152013-08-27 23:12:31 +010055
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000056static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
57{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010058 EFX_BUG_ON_PARANOID(!efx->mcdi);
59 return &efx->mcdi->iface;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000060}
61
Ben Hutchingsf073dde2012-09-18 02:33:55 +010062int efx_mcdi_init(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000063{
64 struct efx_mcdi_iface *mcdi;
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010065 bool already_attached;
66 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000067
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010068 efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
69 if (!efx->mcdi)
70 return -ENOMEM;
71
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000072 mcdi = efx_mcdi(efx);
Ben Hutchingscade7152013-08-27 23:12:31 +010073 mcdi->efx = efx;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000074 init_waitqueue_head(&mcdi->wq);
75 spin_lock_init(&mcdi->iface_lock);
Ben Hutchings251111d2013-08-27 23:04:29 +010076 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000077 mcdi->mode = MCDI_MODE_POLL;
Ben Hutchingscade7152013-08-27 23:12:31 +010078 spin_lock_init(&mcdi->async_lock);
79 INIT_LIST_HEAD(&mcdi->async_list);
80 setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
81 (unsigned long)mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000082
83 (void) efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +010084 mcdi->new_epoch = true;
Ben Hutchingsf073dde2012-09-18 02:33:55 +010085
86 /* Recover from a failed assertion before probing */
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010087 rc = efx_mcdi_handle_assertion(efx);
88 if (rc)
89 return rc;
90
91 /* Let the MC (and BMC, if this is a LOM) know that the driver
92 * is loaded. We should do this before we reset the NIC.
93 */
94 rc = efx_mcdi_drv_attach(efx, true, &already_attached);
95 if (rc) {
96 netif_err(efx, probe, efx->net_dev,
97 "Unable to register driver with MCPU\n");
98 return rc;
99 }
100 if (already_attached)
101 /* Not a fatal error */
102 netif_err(efx, probe, efx->net_dev,
103 "Host already registered with MCPU\n");
104
Ben Hutchings0bcf4a62013-10-18 19:21:45 +0100105 if (efx->mcdi->fn_flags &
106 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
107 efx->primary = efx;
108
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100109 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000110}
111
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100112void efx_mcdi_fini(struct efx_nic *efx)
113{
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100114 if (!efx->mcdi)
115 return;
116
117 BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
118
119 /* Relinquish the device (back to the BMC, if this is a LOM) */
120 efx_mcdi_drv_attach(efx, false, NULL);
121
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100122 kfree(efx->mcdi);
123}
124
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100125static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
126 const efx_dword_t *inbuf, size_t inlen)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000127{
128 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100129 efx_dword_t hdr[2];
130 size_t hdr_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000131 u32 xflags, seqno;
132
Ben Hutchings251111d2013-08-27 23:04:29 +0100133 BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000134
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100135 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
136 spin_lock_bh(&mcdi->iface_lock);
137 ++mcdi->seqno;
138 spin_unlock_bh(&mcdi->iface_lock);
139
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000140 seqno = mcdi->seqno & SEQ_MASK;
141 xflags = 0;
142 if (mcdi->mode == MCDI_MODE_EVENTS)
143 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
144
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100145 if (efx->type->mcdi_max_ver == 1) {
146 /* MCDI v1 */
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100147 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100148 MCDI_HEADER_RESPONSE, 0,
149 MCDI_HEADER_RESYNC, 1,
150 MCDI_HEADER_CODE, cmd,
151 MCDI_HEADER_DATALEN, inlen,
152 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100153 MCDI_HEADER_XFLAGS, xflags,
154 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100155 hdr_len = 4;
156 } else {
157 /* MCDI v2 */
158 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100159 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100160 MCDI_HEADER_RESPONSE, 0,
161 MCDI_HEADER_RESYNC, 1,
162 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
163 MCDI_HEADER_DATALEN, 0,
164 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100165 MCDI_HEADER_XFLAGS, xflags,
166 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100167 EFX_POPULATE_DWORD_2(hdr[1],
168 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
169 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
170 hdr_len = 8;
171 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000172
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100173 efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100174
175 mcdi->new_epoch = false;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000176}
177
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100178static int efx_mcdi_errno(unsigned int mcdi_err)
179{
180 switch (mcdi_err) {
181 case 0:
182 return 0;
183#define TRANSLATE_ERROR(name) \
184 case MC_CMD_ERR_ ## name: \
185 return -name;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100186 TRANSLATE_ERROR(EPERM);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100187 TRANSLATE_ERROR(ENOENT);
188 TRANSLATE_ERROR(EINTR);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100189 TRANSLATE_ERROR(EAGAIN);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100190 TRANSLATE_ERROR(EACCES);
191 TRANSLATE_ERROR(EBUSY);
192 TRANSLATE_ERROR(EINVAL);
193 TRANSLATE_ERROR(EDEADLK);
194 TRANSLATE_ERROR(ENOSYS);
195 TRANSLATE_ERROR(ETIME);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100196 TRANSLATE_ERROR(EALREADY);
197 TRANSLATE_ERROR(ENOSPC);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100198#undef TRANSLATE_ERROR
Ben Hutchingsea136ae2013-10-08 16:36:58 +0100199 case MC_CMD_ERR_ENOTSUP:
200 return -EOPNOTSUPP;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100201 case MC_CMD_ERR_ALLOC_FAIL:
202 return -ENOBUFS;
203 case MC_CMD_ERR_MAC_EXIST:
204 return -EADDRINUSE;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100205 default:
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100206 return -EPROTO;
207 }
208}
209
210static void efx_mcdi_read_response_header(struct efx_nic *efx)
211{
212 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
213 unsigned int respseq, respcmd, error;
214 efx_dword_t hdr;
215
216 efx->type->mcdi_read_response(efx, &hdr, 0, 4);
217 respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
218 respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
219 error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
220
221 if (respcmd != MC_CMD_V2_EXTN) {
222 mcdi->resp_hdr_len = 4;
223 mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
224 } else {
225 efx->type->mcdi_read_response(efx, &hdr, 4, 4);
226 mcdi->resp_hdr_len = 8;
227 mcdi->resp_data_len =
228 EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
229 }
230
231 if (error && mcdi->resp_data_len == 0) {
232 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
233 mcdi->resprc = -EIO;
234 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
235 netif_err(efx, hw, efx->net_dev,
236 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
237 respseq, mcdi->seqno);
238 mcdi->resprc = -EIO;
239 } else if (error) {
240 efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
241 mcdi->resprc =
242 efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
243 } else {
244 mcdi->resprc = 0;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100245 }
246}
247
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100248static bool efx_mcdi_poll_once(struct efx_nic *efx)
249{
250 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
251
252 rmb();
253 if (!efx->type->mcdi_poll_response(efx))
254 return false;
255
256 spin_lock_bh(&mcdi->iface_lock);
257 efx_mcdi_read_response_header(efx);
258 spin_unlock_bh(&mcdi->iface_lock);
259
260 return true;
261}
262
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000263static int efx_mcdi_poll(struct efx_nic *efx)
264{
265 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000266 unsigned long time, finish;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100267 unsigned int spins;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100268 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000269
270 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100271 rc = efx_mcdi_poll_reboot(efx);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100272 if (rc) {
Ben Hutchings369327f2012-10-26 17:53:12 +0100273 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100274 mcdi->resprc = rc;
275 mcdi->resp_hdr_len = 0;
276 mcdi->resp_data_len = 0;
Ben Hutchings369327f2012-10-26 17:53:12 +0100277 spin_unlock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100278 return 0;
279 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000280
281 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
282 * because generally mcdi responses are fast. After that, back off
283 * and poll once a jiffy (approximately)
284 */
285 spins = TICK_USEC;
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000286 finish = jiffies + MCDI_RPC_TIMEOUT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000287
288 while (1) {
289 if (spins != 0) {
290 --spins;
291 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000292 } else {
293 schedule_timeout_uninterruptible(1);
294 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000295
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000296 time = jiffies;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000297
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100298 if (efx_mcdi_poll_once(efx))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000299 break;
300
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000301 if (time_after(time, finish))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000302 return -ETIMEDOUT;
303 }
304
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000305 /* Return rc=0 like wait_event_timeout() */
306 return 0;
307}
308
Ben Hutchings876be082012-10-01 20:58:35 +0100309/* Test and clear MC-rebooted flag for this port/function; reset
310 * software state as necessary.
311 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000312int efx_mcdi_poll_reboot(struct efx_nic *efx)
313{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100314 if (!efx->mcdi)
315 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000316
Ben Hutchingscd0ecc92012-12-14 21:52:56 +0000317 return efx->type->mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000318}
319
Ben Hutchingscade7152013-08-27 23:12:31 +0100320static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
321{
322 return cmpxchg(&mcdi->state,
323 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
324 MCDI_STATE_QUIESCENT;
325}
326
327static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000328{
329 /* Wait until the interface becomes QUIESCENT and we win the race
Ben Hutchingscade7152013-08-27 23:12:31 +0100330 * to mark it RUNNING_SYNC.
331 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000332 wait_event(mcdi->wq,
Ben Hutchings251111d2013-08-27 23:04:29 +0100333 cmpxchg(&mcdi->state,
Ben Hutchingscade7152013-08-27 23:12:31 +0100334 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
Ben Hutchings251111d2013-08-27 23:04:29 +0100335 MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000336}
337
338static int efx_mcdi_await_completion(struct efx_nic *efx)
339{
340 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
341
Ben Hutchings251111d2013-08-27 23:04:29 +0100342 if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
343 MCDI_RPC_TIMEOUT) == 0)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000344 return -ETIMEDOUT;
345
346 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
347 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
348 * completed the request first, then we'll just end up completing the
349 * request again, which is safe.
350 *
351 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
352 * wait_event_timeout() implicitly provides.
353 */
354 if (mcdi->mode == MCDI_MODE_POLL)
355 return efx_mcdi_poll(efx);
356
357 return 0;
358}
359
Ben Hutchingscade7152013-08-27 23:12:31 +0100360/* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
361 * requester. Return whether this was done. Does not take any locks.
362 */
363static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000364{
Ben Hutchingscade7152013-08-27 23:12:31 +0100365 if (cmpxchg(&mcdi->state,
366 MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
367 MCDI_STATE_RUNNING_SYNC) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000368 wake_up(&mcdi->wq);
369 return true;
370 }
371
372 return false;
373}
374
375static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
376{
Ben Hutchingscade7152013-08-27 23:12:31 +0100377 if (mcdi->mode == MCDI_MODE_EVENTS) {
378 struct efx_mcdi_async_param *async;
379 struct efx_nic *efx = mcdi->efx;
380
381 /* Process the asynchronous request queue */
382 spin_lock_bh(&mcdi->async_lock);
383 async = list_first_entry_or_null(
384 &mcdi->async_list, struct efx_mcdi_async_param, list);
385 if (async) {
386 mcdi->state = MCDI_STATE_RUNNING_ASYNC;
387 efx_mcdi_send_request(efx, async->cmd,
388 (const efx_dword_t *)(async + 1),
389 async->inlen);
390 mod_timer(&mcdi->async_timer,
391 jiffies + MCDI_RPC_TIMEOUT);
392 }
393 spin_unlock_bh(&mcdi->async_lock);
394
395 if (async)
396 return;
397 }
398
Ben Hutchings251111d2013-08-27 23:04:29 +0100399 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000400 wake_up(&mcdi->wq);
401}
402
Ben Hutchingscade7152013-08-27 23:12:31 +0100403/* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
404 * asynchronous completion function, and release the interface.
405 * Return whether this was done. Must be called in bh-disabled
406 * context. Will take iface_lock and async_lock.
407 */
408static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
409{
410 struct efx_nic *efx = mcdi->efx;
411 struct efx_mcdi_async_param *async;
Edward Cree1e0b8122013-05-31 18:36:12 +0100412 size_t hdr_len, data_len, err_len;
Ben Hutchingscade7152013-08-27 23:12:31 +0100413 efx_dword_t *outbuf;
Edward Cree1e0b8122013-05-31 18:36:12 +0100414 MCDI_DECLARE_BUF_OUT_OR_ERR(errbuf, 0);
Ben Hutchingscade7152013-08-27 23:12:31 +0100415 int rc;
416
417 if (cmpxchg(&mcdi->state,
418 MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
419 MCDI_STATE_RUNNING_ASYNC)
420 return false;
421
422 spin_lock(&mcdi->iface_lock);
423 if (timeout) {
424 /* Ensure that if the completion event arrives later,
425 * the seqno check in efx_mcdi_ev_cpl() will fail
426 */
427 ++mcdi->seqno;
428 ++mcdi->credits;
429 rc = -ETIMEDOUT;
430 hdr_len = 0;
431 data_len = 0;
432 } else {
433 rc = mcdi->resprc;
434 hdr_len = mcdi->resp_hdr_len;
435 data_len = mcdi->resp_data_len;
436 }
437 spin_unlock(&mcdi->iface_lock);
438
439 /* Stop the timer. In case the timer function is running, we
440 * must wait for it to return so that there is no possibility
441 * of it aborting the next request.
442 */
443 if (!timeout)
444 del_timer_sync(&mcdi->async_timer);
445
446 spin_lock(&mcdi->async_lock);
447 async = list_first_entry(&mcdi->async_list,
448 struct efx_mcdi_async_param, list);
449 list_del(&async->list);
450 spin_unlock(&mcdi->async_lock);
451
452 outbuf = (efx_dword_t *)(async + 1);
453 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
454 min(async->outlen, data_len));
Edward Cree1e0b8122013-05-31 18:36:12 +0100455 if (!timeout && rc && !async->quiet) {
456 err_len = min(sizeof(errbuf), data_len);
457 efx->type->mcdi_read_response(efx, errbuf, hdr_len,
458 sizeof(errbuf));
459 efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf,
460 err_len, rc);
461 }
Ben Hutchingscade7152013-08-27 23:12:31 +0100462 async->complete(efx, async->cookie, rc, outbuf, data_len);
463 kfree(async);
464
465 efx_mcdi_release(mcdi);
466
467 return true;
468}
469
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000470static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100471 unsigned int datalen, unsigned int mcdi_err)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000472{
473 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
474 bool wake = false;
475
476 spin_lock(&mcdi->iface_lock);
477
478 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
479 if (mcdi->credits)
480 /* The request has been cancelled */
481 --mcdi->credits;
482 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000483 netif_err(efx, hw, efx->net_dev,
484 "MC response mismatch tx seq 0x%x rx "
485 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000486 } else {
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100487 if (efx->type->mcdi_max_ver >= 2) {
488 /* MCDI v2 responses don't fit in an event */
489 efx_mcdi_read_response_header(efx);
490 } else {
491 mcdi->resprc = efx_mcdi_errno(mcdi_err);
492 mcdi->resp_hdr_len = 4;
493 mcdi->resp_data_len = datalen;
494 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000495
496 wake = true;
497 }
498
499 spin_unlock(&mcdi->iface_lock);
500
Ben Hutchingscade7152013-08-27 23:12:31 +0100501 if (wake) {
502 if (!efx_mcdi_complete_async(mcdi, false))
503 (void) efx_mcdi_complete_sync(mcdi);
504
505 /* If the interface isn't RUNNING_ASYNC or
506 * RUNNING_SYNC then we've received a duplicate
507 * completion after we've already transitioned back to
508 * QUIESCENT. [A subsequent invocation would increment
509 * seqno, so would have failed the seqno check].
510 */
511 }
512}
513
514static void efx_mcdi_timeout_async(unsigned long context)
515{
516 struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
517
518 efx_mcdi_complete_async(mcdi, true);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000519}
520
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100521static int
522efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
523{
524 if (efx->type->mcdi_max_ver < 0 ||
525 (efx->type->mcdi_max_ver < 2 &&
526 cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
527 return -EINVAL;
528
529 if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
530 (efx->type->mcdi_max_ver < 2 &&
531 inlen > MCDI_CTL_SDU_LEN_MAX_V1))
532 return -EMSGSIZE;
533
534 return 0;
535}
536
Edward Cree1e0b8122013-05-31 18:36:12 +0100537static int _efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
538 efx_dword_t *outbuf, size_t outlen,
539 size_t *outlen_actual, bool quiet)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100540{
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000541 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Edward Cree1e0b8122013-05-31 18:36:12 +0100542 MCDI_DECLARE_BUF_OUT_OR_ERR(errbuf, 0);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100543 int rc;
544
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000545 if (mcdi->mode == MCDI_MODE_POLL)
546 rc = efx_mcdi_poll(efx);
547 else
548 rc = efx_mcdi_await_completion(efx);
549
550 if (rc != 0) {
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100551 netif_err(efx, hw, efx->net_dev,
552 "MC command 0x%x inlen %d mode %d timed out\n",
553 cmd, (int)inlen, mcdi->mode);
554
555 if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
556 netif_err(efx, hw, efx->net_dev,
557 "MCDI request was completed without an event\n");
558 rc = 0;
559 }
560
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000561 /* Close the race with efx_mcdi_ev_cpl() executing just too late
562 * and completing a request we've just cancelled, by ensuring
563 * that the seqno check therein fails.
564 */
565 spin_lock_bh(&mcdi->iface_lock);
566 ++mcdi->seqno;
567 ++mcdi->credits;
568 spin_unlock_bh(&mcdi->iface_lock);
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100569 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000570
Edward Cree1e0b8122013-05-31 18:36:12 +0100571 if (rc != 0) {
572 if (outlen_actual)
573 *outlen_actual = 0;
574 } else {
575 size_t hdr_len, data_len, err_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000576
577 /* At the very least we need a memory barrier here to ensure
578 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
579 * a spurious efx_mcdi_ev_cpl() running concurrently by
580 * acquiring the iface_lock. */
581 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100582 rc = mcdi->resprc;
Ben Hutchings369327f2012-10-26 17:53:12 +0100583 hdr_len = mcdi->resp_hdr_len;
584 data_len = mcdi->resp_data_len;
Edward Cree1e0b8122013-05-31 18:36:12 +0100585 err_len = min(sizeof(errbuf), data_len);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000586 spin_unlock_bh(&mcdi->iface_lock);
587
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100588 BUG_ON(rc > 0);
589
Edward Cree1e0b8122013-05-31 18:36:12 +0100590 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
591 min(outlen, data_len));
592 if (outlen_actual)
593 *outlen_actual = data_len;
594
595 efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len);
596
597 if (cmd == MC_CMD_REBOOT && rc == -EIO) {
598 /* Don't reset if MC_CMD_REBOOT returns EIO */
599 } else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000600 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
601 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000602 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
Edward Cree1e0b8122013-05-31 18:36:12 +0100603 } else if (rc && !quiet) {
604 efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len,
605 rc);
606 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000607
608 if (rc == -EIO || rc == -EINTR) {
609 msleep(MCDI_STATUS_SLEEP_MS);
610 efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100611 mcdi->new_epoch = true;
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000612 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000613 }
614
615 efx_mcdi_release(mcdi);
616 return rc;
617}
618
Edward Cree1e0b8122013-05-31 18:36:12 +0100619static int _efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
620 const efx_dword_t *inbuf, size_t inlen,
621 efx_dword_t *outbuf, size_t outlen,
622 size_t *outlen_actual, bool quiet)
623{
624 int rc;
625
626 rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
627 if (rc) {
628 if (outlen_actual)
629 *outlen_actual = 0;
630 return rc;
631 }
632 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
633 outlen_actual, quiet);
634}
635
636int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
637 const efx_dword_t *inbuf, size_t inlen,
638 efx_dword_t *outbuf, size_t outlen,
639 size_t *outlen_actual)
640{
641 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
642 outlen_actual, false);
643}
644
645/* Normally, on receiving an error code in the MCDI response,
646 * efx_mcdi_rpc will log an error message containing (among other
647 * things) the raw error code, by means of efx_mcdi_display_error.
648 * This _quiet version suppresses that; if the caller wishes to log
649 * the error conditionally on the return code, it should call this
650 * function and is then responsible for calling efx_mcdi_display_error
651 * as needed.
652 */
653int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
654 const efx_dword_t *inbuf, size_t inlen,
655 efx_dword_t *outbuf, size_t outlen,
656 size_t *outlen_actual)
657{
658 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
659 outlen_actual, true);
660}
661
662int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
663 const efx_dword_t *inbuf, size_t inlen)
664{
665 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
666 int rc;
667
668 rc = efx_mcdi_check_supported(efx, cmd, inlen);
669 if (rc)
670 return rc;
671
672 if (efx->mc_bist_for_other_fn)
673 return -ENETDOWN;
674
675 efx_mcdi_acquire_sync(mcdi);
676 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
677 return 0;
678}
679
680static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
681 const efx_dword_t *inbuf, size_t inlen,
682 size_t outlen,
683 efx_mcdi_async_completer *complete,
684 unsigned long cookie, bool quiet)
685{
686 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
687 struct efx_mcdi_async_param *async;
688 int rc;
689
690 rc = efx_mcdi_check_supported(efx, cmd, inlen);
691 if (rc)
692 return rc;
693
694 if (efx->mc_bist_for_other_fn)
695 return -ENETDOWN;
696
697 async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
698 GFP_ATOMIC);
699 if (!async)
700 return -ENOMEM;
701
702 async->cmd = cmd;
703 async->inlen = inlen;
704 async->outlen = outlen;
705 async->quiet = quiet;
706 async->complete = complete;
707 async->cookie = cookie;
708 memcpy(async + 1, inbuf, inlen);
709
710 spin_lock_bh(&mcdi->async_lock);
711
712 if (mcdi->mode == MCDI_MODE_EVENTS) {
713 list_add_tail(&async->list, &mcdi->async_list);
714
715 /* If this is at the front of the queue, try to start it
716 * immediately
717 */
718 if (mcdi->async_list.next == &async->list &&
719 efx_mcdi_acquire_async(mcdi)) {
720 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
721 mod_timer(&mcdi->async_timer,
722 jiffies + MCDI_RPC_TIMEOUT);
723 }
724 } else {
725 kfree(async);
726 rc = -ENETDOWN;
727 }
728
729 spin_unlock_bh(&mcdi->async_lock);
730
731 return rc;
732}
733
734/**
735 * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
736 * @efx: NIC through which to issue the command
737 * @cmd: Command type number
738 * @inbuf: Command parameters
739 * @inlen: Length of command parameters, in bytes
740 * @outlen: Length to allocate for response buffer, in bytes
741 * @complete: Function to be called on completion or cancellation.
742 * @cookie: Arbitrary value to be passed to @complete.
743 *
744 * This function does not sleep and therefore may be called in atomic
745 * context. It will fail if event queues are disabled or if MCDI
746 * event completions have been disabled due to an error.
747 *
748 * If it succeeds, the @complete function will be called exactly once
749 * in atomic context, when one of the following occurs:
750 * (a) the completion event is received (in NAPI context)
751 * (b) event queues are disabled (in the process that disables them)
752 * (c) the request times-out (in timer context)
753 */
754int
755efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
756 const efx_dword_t *inbuf, size_t inlen, size_t outlen,
757 efx_mcdi_async_completer *complete, unsigned long cookie)
758{
759 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
760 cookie, false);
761}
762
763int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
764 const efx_dword_t *inbuf, size_t inlen,
765 size_t outlen, efx_mcdi_async_completer *complete,
766 unsigned long cookie)
767{
768 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
769 cookie, true);
770}
771
772int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
773 efx_dword_t *outbuf, size_t outlen,
774 size_t *outlen_actual)
775{
776 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
777 outlen_actual, false);
778}
779
780int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd, size_t inlen,
781 efx_dword_t *outbuf, size_t outlen,
782 size_t *outlen_actual)
783{
784 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
785 outlen_actual, true);
786}
787
788void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
789 size_t inlen, efx_dword_t *outbuf,
790 size_t outlen, int rc)
791{
792 int code = 0, err_arg = 0;
793
794 if (outlen >= MC_CMD_ERR_CODE_OFST + 4)
795 code = MCDI_DWORD(outbuf, ERR_CODE);
796 if (outlen >= MC_CMD_ERR_ARG_OFST + 4)
797 err_arg = MCDI_DWORD(outbuf, ERR_ARG);
798 netif_err(efx, hw, efx->net_dev,
799 "MC command 0x%x inlen %d failed rc=%d (raw=%d) arg=%d\n",
800 cmd, (int)inlen, rc, code, err_arg);
801}
802
Ben Hutchingscade7152013-08-27 23:12:31 +0100803/* Switch to polled MCDI completions. This can be called in various
804 * error conditions with various locks held, so it must be lockless.
805 * Caller is responsible for flushing asynchronous requests later.
806 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000807void efx_mcdi_mode_poll(struct efx_nic *efx)
808{
809 struct efx_mcdi_iface *mcdi;
810
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100811 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000812 return;
813
814 mcdi = efx_mcdi(efx);
815 if (mcdi->mode == MCDI_MODE_POLL)
816 return;
817
818 /* We can switch from event completion to polled completion, because
819 * mcdi requests are always completed in shared memory. We do this by
820 * switching the mode to POLL'd then completing the request.
821 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
822 *
823 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
Ben Hutchingscade7152013-08-27 23:12:31 +0100824 * which efx_mcdi_complete_sync() provides for us.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000825 */
826 mcdi->mode = MCDI_MODE_POLL;
827
Ben Hutchingscade7152013-08-27 23:12:31 +0100828 efx_mcdi_complete_sync(mcdi);
829}
830
831/* Flush any running or queued asynchronous requests, after event processing
832 * is stopped
833 */
834void efx_mcdi_flush_async(struct efx_nic *efx)
835{
836 struct efx_mcdi_async_param *async, *next;
837 struct efx_mcdi_iface *mcdi;
838
839 if (!efx->mcdi)
840 return;
841
842 mcdi = efx_mcdi(efx);
843
844 /* We must be in polling mode so no more requests can be queued */
845 BUG_ON(mcdi->mode != MCDI_MODE_POLL);
846
847 del_timer_sync(&mcdi->async_timer);
848
849 /* If a request is still running, make sure we give the MC
850 * time to complete it so that the response won't overwrite our
851 * next request.
852 */
853 if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
854 efx_mcdi_poll(efx);
855 mcdi->state = MCDI_STATE_QUIESCENT;
856 }
857
858 /* Nothing else will access the async list now, so it is safe
859 * to walk it without holding async_lock. If we hold it while
860 * calling a completer then lockdep may warn that we have
861 * acquired locks in the wrong order.
862 */
863 list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
864 async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
865 list_del(&async->list);
866 kfree(async);
867 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000868}
869
870void efx_mcdi_mode_event(struct efx_nic *efx)
871{
872 struct efx_mcdi_iface *mcdi;
873
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100874 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000875 return;
876
877 mcdi = efx_mcdi(efx);
878
879 if (mcdi->mode == MCDI_MODE_EVENTS)
880 return;
881
882 /* We can't switch from polled to event completion in the middle of a
883 * request, because the completion method is specified in the request.
884 * So acquire the interface to serialise the requestors. We don't need
885 * to acquire the iface_lock to change the mode here, but we do need a
886 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
887 * efx_mcdi_acquire() provides.
888 */
Ben Hutchingscade7152013-08-27 23:12:31 +0100889 efx_mcdi_acquire_sync(mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000890 mcdi->mode = MCDI_MODE_EVENTS;
891 efx_mcdi_release(mcdi);
892}
893
894static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
895{
896 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
897
898 /* If there is an outstanding MCDI request, it has been terminated
899 * either by a BADASSERT or REBOOT event. If the mcdi interface is
900 * in polled mode, then do nothing because the MC reboot handler will
901 * set the header correctly. However, if the mcdi interface is waiting
902 * for a CMDDONE event it won't receive it [and since all MCDI events
903 * are sent to the same queue, we can't be racing with
904 * efx_mcdi_ev_cpl()]
905 *
Ben Hutchingscade7152013-08-27 23:12:31 +0100906 * If there is an outstanding asynchronous request, we can't
907 * complete it now (efx_mcdi_complete() would deadlock). The
908 * reset process will take care of this.
909 *
910 * There's a race here with efx_mcdi_send_request(), because
911 * we might receive a REBOOT event *before* the request has
912 * been copied out. In polled mode (during startup) this is
913 * irrelevant, because efx_mcdi_complete_sync() is ignored. In
914 * event mode, this condition is just an edge-case of
915 * receiving a REBOOT event after posting the MCDI
916 * request. Did the mc reboot before or after the copyout? The
917 * best we can do always is just return failure.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000918 */
919 spin_lock(&mcdi->iface_lock);
Ben Hutchingscade7152013-08-27 23:12:31 +0100920 if (efx_mcdi_complete_sync(mcdi)) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000921 if (mcdi->mode == MCDI_MODE_EVENTS) {
922 mcdi->resprc = rc;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100923 mcdi->resp_hdr_len = 0;
924 mcdi->resp_data_len = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +0000925 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000926 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000927 } else {
928 int count;
929
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000930 /* Consume the status word since efx_mcdi_rpc_finish() won't */
931 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
932 if (efx_mcdi_poll_reboot(efx))
933 break;
934 udelay(MCDI_STATUS_DELAY_US);
935 }
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100936 mcdi->new_epoch = true;
Daniel Pieczkodfdaa952013-09-18 10:16:24 +0100937
938 /* Nobody was waiting for an MCDI request, so trigger a reset */
939 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000940 }
941
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000942 spin_unlock(&mcdi->iface_lock);
943}
944
Jon Cooper74cd60a2013-09-16 14:18:51 +0100945/* The MC is going down in to BIST mode. set the BIST flag to block
946 * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
947 * (which doesn't actually execute a reset, it waits for the controlling
948 * function to reset it).
949 */
950static void efx_mcdi_ev_bist(struct efx_nic *efx)
951{
952 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
953
954 spin_lock(&mcdi->iface_lock);
955 efx->mc_bist_for_other_fn = true;
956 if (efx_mcdi_complete_sync(mcdi)) {
957 if (mcdi->mode == MCDI_MODE_EVENTS) {
958 mcdi->resprc = -EIO;
959 mcdi->resp_hdr_len = 0;
960 mcdi->resp_data_len = 0;
961 ++mcdi->credits;
962 }
963 }
964 mcdi->new_epoch = true;
965 efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
966 spin_unlock(&mcdi->iface_lock);
967}
968
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000969/* Called from falcon_process_eventq for MCDI events */
970void efx_mcdi_process_event(struct efx_channel *channel,
971 efx_qword_t *event)
972{
973 struct efx_nic *efx = channel->efx;
974 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
975 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
976
977 switch (code) {
978 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000979 netif_err(efx, hw, efx->net_dev,
980 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100981 efx_mcdi_ev_death(efx, -EINTR);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000982 break;
983
984 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +0000985 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000986 break;
987
988 case MCDI_EVENT_CODE_CMDDONE:
989 efx_mcdi_ev_cpl(efx,
990 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
991 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
992 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
993 break;
994
995 case MCDI_EVENT_CODE_LINKCHANGE:
996 efx_mcdi_process_link_change(efx, event);
997 break;
998 case MCDI_EVENT_CODE_SENSOREVT:
999 efx_mcdi_sensor_event(efx, event);
1000 break;
1001 case MCDI_EVENT_CODE_SCHEDERR:
Robert Stonehouse2d9955b2013-10-07 18:44:17 +01001002 netif_dbg(efx, hw, efx->net_dev,
1003 "MC Scheduler alert (0x%x)\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001004 break;
1005 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings8127d662013-08-29 19:19:29 +01001006 case MCDI_EVENT_CODE_MC_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +00001007 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchings5bc283e2012-10-08 21:43:00 +01001008 efx_mcdi_ev_death(efx, -EIO);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001009 break;
Jon Cooper74cd60a2013-09-16 14:18:51 +01001010 case MCDI_EVENT_CODE_MC_BIST:
1011 netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
1012 efx_mcdi_ev_bist(efx);
1013 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001014 case MCDI_EVENT_CODE_MAC_STATS_DMA:
1015 /* MAC stats are gather lazily. We can ignore this. */
1016 break;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001017 case MCDI_EVENT_CODE_FLR:
1018 efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF));
1019 break;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001020 case MCDI_EVENT_CODE_PTP_RX:
1021 case MCDI_EVENT_CODE_PTP_FAULT:
1022 case MCDI_EVENT_CODE_PTP_PPS:
1023 efx_ptp_event(efx, event);
1024 break;
Jon Cooperbd9a2652013-11-18 12:54:41 +00001025 case MCDI_EVENT_CODE_PTP_TIME:
1026 efx_time_sync_event(channel, event);
1027 break;
Ben Hutchings8127d662013-08-29 19:19:29 +01001028 case MCDI_EVENT_CODE_TX_FLUSH:
1029 case MCDI_EVENT_CODE_RX_FLUSH:
1030 /* Two flush events will be sent: one to the same event
1031 * queue as completions, and one to event queue 0.
1032 * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
1033 * flag will be set, and we should ignore the event
1034 * because we want to wait for all completions.
1035 */
1036 BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
1037 MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
1038 if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
1039 efx_ef10_handle_drain_event(efx);
1040 break;
Alexandre Rames3de82b92013-06-13 11:36:15 +01001041 case MCDI_EVENT_CODE_TX_ERR:
1042 case MCDI_EVENT_CODE_RX_ERR:
1043 netif_err(efx, hw, efx->net_dev,
1044 "%s DMA error (event: "EFX_QWORD_FMT")\n",
1045 code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
1046 EFX_QWORD_VAL(*event));
1047 efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
1048 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001049 default:
Ben Hutchings62776d02010-06-23 11:30:07 +00001050 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
1051 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001052 }
1053}
1054
1055/**************************************************************************
1056 *
1057 * Specific request functions
1058 *
1059 **************************************************************************
1060 */
1061
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001062void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001063{
Ben Hutchings8127d662013-08-29 19:19:29 +01001064 MCDI_DECLARE_BUF(outbuf,
1065 max(MC_CMD_GET_VERSION_OUT_LEN,
1066 MC_CMD_GET_CAPABILITIES_OUT_LEN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001067 size_t outlength;
1068 const __le16 *ver_words;
Ben Hutchings8127d662013-08-29 19:19:29 +01001069 size_t offset;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001070 int rc;
1071
1072 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001073 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
1074 outbuf, sizeof(outbuf), &outlength);
1075 if (rc)
1076 goto fail;
Ben Hutchings05a93202011-12-20 00:44:06 +00001077 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001078 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001079 goto fail;
1080 }
1081
1082 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchings8127d662013-08-29 19:19:29 +01001083 offset = snprintf(buf, len, "%u.%u.%u.%u",
1084 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
1085 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
1086
1087 /* EF10 may have multiple datapath firmware variants within a
1088 * single version. Report which variants are running.
1089 */
1090 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1091 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
1092 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
1093 outbuf, sizeof(outbuf), &outlength);
1094 if (rc || outlength < MC_CMD_GET_CAPABILITIES_OUT_LEN)
1095 offset += snprintf(
1096 buf + offset, len - offset, " rx? tx?");
1097 else
1098 offset += snprintf(
1099 buf + offset, len - offset, " rx%x tx%x",
1100 MCDI_WORD(outbuf,
1101 GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID),
1102 MCDI_WORD(outbuf,
1103 GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID));
1104
1105 /* It's theoretically possible for the string to exceed 31
1106 * characters, though in practice the first three version
1107 * components are short enough that this doesn't happen.
1108 */
1109 if (WARN_ON(offset >= len))
1110 buf[0] = 0;
1111 }
1112
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001113 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001114
1115fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001116 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001117 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001118}
1119
Ben Hutchings4c75b43a2013-08-29 19:04:03 +01001120static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
1121 bool *was_attached)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001122{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001123 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001124 MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001125 size_t outlen;
1126 int rc;
1127
1128 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
1129 driver_operating ? 1 : 0);
1130 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
Ben Hutchingsf2b0bef2013-08-20 20:35:50 +01001131 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001132
1133 rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
1134 outbuf, sizeof(outbuf), &outlen);
1135 if (rc)
1136 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001137 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
1138 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001139 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001140 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001141
Ben Hutchings8349f7f2013-10-16 18:32:34 +01001142 if (driver_operating) {
1143 if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) {
1144 efx->mcdi->fn_flags =
1145 MCDI_DWORD(outbuf,
1146 DRV_ATTACH_EXT_OUT_FUNC_FLAGS);
1147 } else {
1148 /* Synthesise flags for Siena */
1149 efx->mcdi->fn_flags =
1150 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1151 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED |
1152 (efx_port_num(efx) == 0) <<
1153 MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY;
1154 }
1155 }
1156
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001157 /* We currently assume we have control of the external link
1158 * and are completely trusted by firmware. Abort probing
1159 * if that's not true for this function.
1160 */
1161 if (driver_operating &&
Ben Hutchings8349f7f2013-10-16 18:32:34 +01001162 (efx->mcdi->fn_flags &
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001163 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1164 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) !=
1165 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1166 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) {
1167 netif_err(efx, probe, efx->net_dev,
1168 "This driver version only supports one function per port\n");
1169 return -ENODEV;
1170 }
1171
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001172 if (was_attached != NULL)
1173 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
1174 return 0;
1175
1176fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001177 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001178 return rc;
1179}
1180
1181int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001182 u16 *fw_subtype_list, u32 *capabilities)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001183{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001184 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001185 size_t outlen, i;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001186 int port_num = efx_port_num(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001187 int rc;
1188
1189 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
Edward Creecd84ff42014-03-07 18:27:41 +00001190 /* we need __aligned(2) for ether_addr_copy */
1191 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1);
1192 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001193
1194 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
1195 outbuf, sizeof(outbuf), &outlen);
1196 if (rc)
1197 goto fail;
1198
Ben Hutchings05a93202011-12-20 00:44:06 +00001199 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001200 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001201 goto fail;
1202 }
1203
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001204 if (mac_address)
Edward Creecd84ff42014-03-07 18:27:41 +00001205 ether_addr_copy(mac_address,
1206 port_num ?
1207 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
1208 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0));
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001209 if (fw_subtype_list) {
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001210 for (i = 0;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001211 i < MCDI_VAR_ARRAY_LEN(outlen,
1212 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
1213 i++)
1214 fw_subtype_list[i] = MCDI_ARRAY_WORD(
1215 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
1216 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
1217 fw_subtype_list[i] = 0;
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001218 }
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001219 if (capabilities) {
1220 if (port_num)
1221 *capabilities = MCDI_DWORD(outbuf,
1222 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1223 else
1224 *capabilities = MCDI_DWORD(outbuf,
1225 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1226 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001227
1228 return 0;
1229
1230fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001231 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
1232 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001233
1234 return rc;
1235}
1236
1237int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
1238{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001239 MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001240 u32 dest = 0;
1241 int rc;
1242
1243 if (uart)
1244 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
1245 if (evq)
1246 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
1247
1248 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
1249 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
1250
1251 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
1252
1253 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
1254 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001255 return rc;
1256}
1257
1258int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
1259{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001260 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001261 size_t outlen;
1262 int rc;
1263
1264 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
1265
1266 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
1267 outbuf, sizeof(outbuf), &outlen);
1268 if (rc)
1269 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001270 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
1271 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001272 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001273 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001274
1275 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
1276 return 0;
1277
1278fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001279 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1280 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001281 return rc;
1282}
1283
1284int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
1285 size_t *size_out, size_t *erase_size_out,
1286 bool *protected_out)
1287{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001288 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
1289 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001290 size_t outlen;
1291 int rc;
1292
1293 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
1294
1295 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
1296 outbuf, sizeof(outbuf), &outlen);
1297 if (rc)
1298 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001299 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
1300 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001301 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001302 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001303
1304 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
1305 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
1306 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +00001307 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001308 return 0;
1309
1310fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001311 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001312 return rc;
1313}
1314
Ben Hutchings2e803402010-02-03 09:31:01 +00001315static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
1316{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001317 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
1318 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
Ben Hutchings2e803402010-02-03 09:31:01 +00001319 int rc;
1320
1321 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
1322
1323 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
1324 outbuf, sizeof(outbuf), NULL);
1325 if (rc)
1326 return rc;
1327
1328 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
1329 case MC_CMD_NVRAM_TEST_PASS:
1330 case MC_CMD_NVRAM_TEST_NOTSUPP:
1331 return 0;
1332 default:
1333 return -EIO;
1334 }
1335}
1336
1337int efx_mcdi_nvram_test_all(struct efx_nic *efx)
1338{
1339 u32 nvram_types;
1340 unsigned int type;
1341 int rc;
1342
1343 rc = efx_mcdi_nvram_types(efx, &nvram_types);
1344 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001345 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +00001346
1347 type = 0;
1348 while (nvram_types != 0) {
1349 if (nvram_types & 1) {
1350 rc = efx_mcdi_nvram_test(efx, type);
1351 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001352 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +00001353 }
1354 type++;
1355 nvram_types >>= 1;
1356 }
1357
1358 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +00001359
1360fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +00001361 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
1362 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001363fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +00001364 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001365 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +00001366}
1367
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001368static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001369{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001370 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +01001371 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001372 unsigned int flags, index;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001373 const char *reason;
1374 size_t outlen;
1375 int retry;
1376 int rc;
1377
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001378 /* Attempt to read any stored assertion state before we reboot
1379 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001380 * because a boot-time assertion might cause this command to fail
1381 * with EINTR. And once again because GET_ASSERTS can race with
1382 * MC_CMD_REBOOT running on the other port. */
1383 retry = 2;
1384 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001385 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Edward Cree1e0b8122013-05-31 18:36:12 +01001386 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS,
1387 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1388 outbuf, sizeof(outbuf), &outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001389 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1390
Edward Cree1e0b8122013-05-31 18:36:12 +01001391 if (rc) {
1392 efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS,
1393 MC_CMD_GET_ASSERTS_IN_LEN, outbuf,
1394 outlen, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001395 return rc;
Edward Cree1e0b8122013-05-31 18:36:12 +01001396 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001397 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001398 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001399
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001400 /* Print out any recorded assertion state */
1401 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001402 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1403 return 0;
1404
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001405 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1406 ? "system-level assertion"
1407 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1408 ? "thread-level assertion"
1409 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1410 ? "watchdog reset"
1411 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +00001412 netif_err(efx, hw, efx->net_dev,
1413 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1414 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1415 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001416
1417 /* Print out the registers */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001418 for (index = 0;
1419 index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1420 index++)
1421 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
1422 1 + index,
1423 MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
1424 index));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001425
1426 return 0;
1427}
1428
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001429static void efx_mcdi_exit_assertion(struct efx_nic *efx)
1430{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001431 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001432
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001433 /* If the MC is running debug firmware, it might now be
1434 * waiting for a debugger to attach, but we just want it to
1435 * reboot. We set a flag that makes the command a no-op if it
1436 * has already done so. We don't know what return code to
1437 * expect (0 or -EIO), so ignore it.
1438 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001439 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1440 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1441 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001442 (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1443 NULL, 0, NULL);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001444}
1445
1446int efx_mcdi_handle_assertion(struct efx_nic *efx)
1447{
1448 int rc;
1449
1450 rc = efx_mcdi_read_assertion(efx);
1451 if (rc)
1452 return rc;
1453
1454 efx_mcdi_exit_assertion(efx);
1455
1456 return 0;
1457}
1458
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001459void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1460{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001461 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001462 int rc;
1463
1464 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1465 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1466 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1467
1468 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1469
1470 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1471
1472 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1473 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001474}
1475
Jon Cooper3e336262014-01-17 19:48:06 +00001476static int efx_mcdi_reset_func(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001477{
Jon Cooper3e336262014-01-17 19:48:06 +00001478 MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN);
1479 int rc;
1480
1481 BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0);
1482 MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG,
1483 ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1484 rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf),
1485 NULL, 0, NULL);
1486 return rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001487}
1488
Ben Hutchings6bff8612012-09-18 02:33:52 +01001489static int efx_mcdi_reset_mc(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001490{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001491 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001492 int rc;
1493
1494 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1495 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1496 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1497 NULL, 0, NULL);
1498 /* White is black, and up is down */
1499 if (rc == -EIO)
1500 return 0;
1501 if (rc == 0)
1502 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001503 return rc;
1504}
1505
Ben Hutchings6bff8612012-09-18 02:33:52 +01001506enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1507{
1508 return RESET_TYPE_RECOVER_OR_ALL;
1509}
1510
1511int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1512{
1513 int rc;
1514
1515 /* Recover from a failed assertion pre-reset */
1516 rc = efx_mcdi_handle_assertion(efx);
1517 if (rc)
1518 return rc;
1519
1520 if (method == RESET_TYPE_WORLD)
1521 return efx_mcdi_reset_mc(efx);
1522 else
Jon Cooper3e336262014-01-17 19:48:06 +00001523 return efx_mcdi_reset_func(efx);
Ben Hutchings6bff8612012-09-18 02:33:52 +01001524}
1525
stephen hemmingerd2156972010-10-18 05:27:31 +00001526static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1527 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001528{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001529 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1530 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001531 size_t outlen;
1532 int rc;
1533
1534 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1535 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1536 MC_CMD_FILTER_MODE_SIMPLE);
Edward Creecd84ff42014-03-07 18:27:41 +00001537 ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001538
1539 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1540 outbuf, sizeof(outbuf), &outlen);
1541 if (rc)
1542 goto fail;
1543
1544 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001545 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001546 goto fail;
1547 }
1548
1549 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1550
1551 return 0;
1552
1553fail:
1554 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001555 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001556 return rc;
1557
1558}
1559
1560
1561int
1562efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1563{
1564 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1565}
1566
1567
1568int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1569{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001570 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001571 size_t outlen;
1572 int rc;
1573
1574 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1575 outbuf, sizeof(outbuf), &outlen);
1576 if (rc)
1577 goto fail;
1578
1579 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001580 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001581 goto fail;
1582 }
1583
1584 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1585
1586 return 0;
1587
1588fail:
1589 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001590 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001591 return rc;
1592}
1593
1594
1595int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1596{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001597 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001598 int rc;
1599
1600 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1601
1602 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1603 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001604 return rc;
1605}
1606
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001607int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1608{
1609 struct efx_channel *channel;
1610 struct efx_rx_queue *rx_queue;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001611 MCDI_DECLARE_BUF(inbuf,
1612 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001613 int rc, count;
1614
Ben Hutchings45078372012-09-19 02:53:34 +01001615 BUILD_BUG_ON(EFX_MAX_CHANNELS >
1616 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1617
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001618 count = 0;
1619 efx_for_each_channel(channel, efx) {
1620 efx_for_each_channel_rx_queue(rx_queue, channel) {
1621 if (rx_queue->flush_pending) {
1622 rx_queue->flush_pending = false;
1623 atomic_dec(&efx->rxq_flush_pending);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001624 MCDI_SET_ARRAY_DWORD(
1625 inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1626 count, efx_rx_queue_index(rx_queue));
1627 count++;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001628 }
1629 }
1630 }
1631
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001632 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1633 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
Ben Hutchingsbbec9692012-09-11 18:25:13 +01001634 WARN_ON(rc < 0);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001635
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001636 return rc;
1637}
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001638
1639int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1640{
1641 int rc;
1642
1643 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001644 return rc;
1645}
1646
Ben Hutchings8127d662013-08-29 19:19:29 +01001647int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled)
1648{
1649 MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
1650
1651 BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
1652 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
1653 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
1654 return efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
1655 NULL, 0, NULL);
1656}
1657
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001658#ifdef CONFIG_SFC_MTD
1659
1660#define EFX_MCDI_NVRAM_LEN_MAX 128
1661
1662static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
1663{
1664 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
1665 int rc;
1666
1667 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
1668
1669 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
1670
1671 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
1672 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001673 return rc;
1674}
1675
1676static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
1677 loff_t offset, u8 *buffer, size_t length)
1678{
1679 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
1680 MCDI_DECLARE_BUF(outbuf,
1681 MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1682 size_t outlen;
1683 int rc;
1684
1685 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
1686 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
1687 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
1688
1689 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
1690 outbuf, sizeof(outbuf), &outlen);
1691 if (rc)
Edward Cree1e0b8122013-05-31 18:36:12 +01001692 return rc;
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001693
1694 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
1695 return 0;
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001696}
1697
1698static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
1699 loff_t offset, const u8 *buffer, size_t length)
1700{
1701 MCDI_DECLARE_BUF(inbuf,
1702 MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1703 int rc;
1704
1705 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
1706 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
1707 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
1708 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
1709
1710 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
1711
1712 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
1713 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
1714 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001715 return rc;
1716}
1717
1718static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
1719 loff_t offset, size_t length)
1720{
1721 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
1722 int rc;
1723
1724 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
1725 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
1726 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
1727
1728 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
1729
1730 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
1731 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001732 return rc;
1733}
1734
1735static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
1736{
1737 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
1738 int rc;
1739
1740 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
1741
1742 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
1743
1744 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
1745 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001746 return rc;
1747}
1748
1749int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
1750 size_t len, size_t *retlen, u8 *buffer)
1751{
1752 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1753 struct efx_nic *efx = mtd->priv;
1754 loff_t offset = start;
1755 loff_t end = min_t(loff_t, start + len, mtd->size);
1756 size_t chunk;
1757 int rc = 0;
1758
1759 while (offset < end) {
1760 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1761 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
1762 buffer, chunk);
1763 if (rc)
1764 goto out;
1765 offset += chunk;
1766 buffer += chunk;
1767 }
1768out:
1769 *retlen = offset - start;
1770 return rc;
1771}
1772
1773int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
1774{
1775 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1776 struct efx_nic *efx = mtd->priv;
1777 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
1778 loff_t end = min_t(loff_t, start + len, mtd->size);
1779 size_t chunk = part->common.mtd.erasesize;
1780 int rc = 0;
1781
1782 if (!part->updating) {
1783 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1784 if (rc)
1785 goto out;
1786 part->updating = true;
1787 }
1788
1789 /* The MCDI interface can in fact do multiple erase blocks at once;
1790 * but erasing may be slow, so we make multiple calls here to avoid
1791 * tripping the MCDI RPC timeout. */
1792 while (offset < end) {
1793 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
1794 chunk);
1795 if (rc)
1796 goto out;
1797 offset += chunk;
1798 }
1799out:
1800 return rc;
1801}
1802
1803int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
1804 size_t len, size_t *retlen, const u8 *buffer)
1805{
1806 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1807 struct efx_nic *efx = mtd->priv;
1808 loff_t offset = start;
1809 loff_t end = min_t(loff_t, start + len, mtd->size);
1810 size_t chunk;
1811 int rc = 0;
1812
1813 if (!part->updating) {
1814 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1815 if (rc)
1816 goto out;
1817 part->updating = true;
1818 }
1819
1820 while (offset < end) {
1821 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1822 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
1823 buffer, chunk);
1824 if (rc)
1825 goto out;
1826 offset += chunk;
1827 buffer += chunk;
1828 }
1829out:
1830 *retlen = offset - start;
1831 return rc;
1832}
1833
1834int efx_mcdi_mtd_sync(struct mtd_info *mtd)
1835{
1836 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1837 struct efx_nic *efx = mtd->priv;
1838 int rc = 0;
1839
1840 if (part->updating) {
1841 part->updating = false;
1842 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
1843 }
1844
1845 return rc;
1846}
1847
1848void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
1849{
1850 struct efx_mcdi_mtd_partition *mcdi_part =
1851 container_of(part, struct efx_mcdi_mtd_partition, common);
1852 struct efx_nic *efx = part->mtd.priv;
1853
1854 snprintf(part->name, sizeof(part->name), "%s %s:%02x",
1855 efx->name, part->type_name, mcdi_part->fw_subtype);
1856}
1857
1858#endif /* CONFIG_SFC_MTD */