blob: da14e24289440a4fa386f0a07bc5f6adfb6d761a [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;
45 efx_mcdi_async_completer *complete;
46 unsigned long cookie;
47 /* followed by request/response buffer */
48};
49
50static void efx_mcdi_timeout_async(unsigned long context);
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010051static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
52 bool *was_attached_out);
Robert Stonehouse5731d7b2013-10-09 11:52:43 +010053static bool efx_mcdi_poll_once(struct efx_nic *efx);
Ben Hutchingscade7152013-08-27 23:12:31 +010054
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000055static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
56{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010057 EFX_BUG_ON_PARANOID(!efx->mcdi);
58 return &efx->mcdi->iface;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000059}
60
Ben Hutchingsf073dde2012-09-18 02:33:55 +010061int efx_mcdi_init(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000062{
63 struct efx_mcdi_iface *mcdi;
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010064 bool already_attached;
65 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000066
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010067 efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
68 if (!efx->mcdi)
69 return -ENOMEM;
70
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000071 mcdi = efx_mcdi(efx);
Ben Hutchingscade7152013-08-27 23:12:31 +010072 mcdi->efx = efx;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000073 init_waitqueue_head(&mcdi->wq);
74 spin_lock_init(&mcdi->iface_lock);
Ben Hutchings251111d2013-08-27 23:04:29 +010075 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000076 mcdi->mode = MCDI_MODE_POLL;
Ben Hutchingscade7152013-08-27 23:12:31 +010077 spin_lock_init(&mcdi->async_lock);
78 INIT_LIST_HEAD(&mcdi->async_list);
79 setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
80 (unsigned long)mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000081
82 (void) efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +010083 mcdi->new_epoch = true;
Ben Hutchingsf073dde2012-09-18 02:33:55 +010084
85 /* Recover from a failed assertion before probing */
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010086 rc = efx_mcdi_handle_assertion(efx);
87 if (rc)
88 return rc;
89
90 /* Let the MC (and BMC, if this is a LOM) know that the driver
91 * is loaded. We should do this before we reset the NIC.
92 */
93 rc = efx_mcdi_drv_attach(efx, true, &already_attached);
94 if (rc) {
95 netif_err(efx, probe, efx->net_dev,
96 "Unable to register driver with MCPU\n");
97 return rc;
98 }
99 if (already_attached)
100 /* Not a fatal error */
101 netif_err(efx, probe, efx->net_dev,
102 "Host already registered with MCPU\n");
103
104 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000105}
106
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100107void efx_mcdi_fini(struct efx_nic *efx)
108{
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100109 if (!efx->mcdi)
110 return;
111
112 BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
113
114 /* Relinquish the device (back to the BMC, if this is a LOM) */
115 efx_mcdi_drv_attach(efx, false, NULL);
116
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100117 kfree(efx->mcdi);
118}
119
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100120static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
121 const efx_dword_t *inbuf, size_t inlen)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000122{
123 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100124 efx_dword_t hdr[2];
125 size_t hdr_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000126 u32 xflags, seqno;
127
Ben Hutchings251111d2013-08-27 23:04:29 +0100128 BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000129
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100130 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
131 spin_lock_bh(&mcdi->iface_lock);
132 ++mcdi->seqno;
133 spin_unlock_bh(&mcdi->iface_lock);
134
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000135 seqno = mcdi->seqno & SEQ_MASK;
136 xflags = 0;
137 if (mcdi->mode == MCDI_MODE_EVENTS)
138 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
139
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100140 if (efx->type->mcdi_max_ver == 1) {
141 /* MCDI v1 */
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100142 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100143 MCDI_HEADER_RESPONSE, 0,
144 MCDI_HEADER_RESYNC, 1,
145 MCDI_HEADER_CODE, cmd,
146 MCDI_HEADER_DATALEN, inlen,
147 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100148 MCDI_HEADER_XFLAGS, xflags,
149 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100150 hdr_len = 4;
151 } else {
152 /* MCDI v2 */
153 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100154 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100155 MCDI_HEADER_RESPONSE, 0,
156 MCDI_HEADER_RESYNC, 1,
157 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
158 MCDI_HEADER_DATALEN, 0,
159 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100160 MCDI_HEADER_XFLAGS, xflags,
161 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100162 EFX_POPULATE_DWORD_2(hdr[1],
163 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
164 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
165 hdr_len = 8;
166 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000167
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100168 efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100169
170 mcdi->new_epoch = false;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000171}
172
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100173static int efx_mcdi_errno(unsigned int mcdi_err)
174{
175 switch (mcdi_err) {
176 case 0:
177 return 0;
178#define TRANSLATE_ERROR(name) \
179 case MC_CMD_ERR_ ## name: \
180 return -name;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100181 TRANSLATE_ERROR(EPERM);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100182 TRANSLATE_ERROR(ENOENT);
183 TRANSLATE_ERROR(EINTR);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100184 TRANSLATE_ERROR(EAGAIN);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100185 TRANSLATE_ERROR(EACCES);
186 TRANSLATE_ERROR(EBUSY);
187 TRANSLATE_ERROR(EINVAL);
188 TRANSLATE_ERROR(EDEADLK);
189 TRANSLATE_ERROR(ENOSYS);
190 TRANSLATE_ERROR(ETIME);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100191 TRANSLATE_ERROR(EALREADY);
192 TRANSLATE_ERROR(ENOSPC);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100193#undef TRANSLATE_ERROR
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100194 case MC_CMD_ERR_ALLOC_FAIL:
195 return -ENOBUFS;
196 case MC_CMD_ERR_MAC_EXIST:
197 return -EADDRINUSE;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100198 default:
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100199 return -EPROTO;
200 }
201}
202
203static void efx_mcdi_read_response_header(struct efx_nic *efx)
204{
205 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
206 unsigned int respseq, respcmd, error;
207 efx_dword_t hdr;
208
209 efx->type->mcdi_read_response(efx, &hdr, 0, 4);
210 respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
211 respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
212 error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
213
214 if (respcmd != MC_CMD_V2_EXTN) {
215 mcdi->resp_hdr_len = 4;
216 mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
217 } else {
218 efx->type->mcdi_read_response(efx, &hdr, 4, 4);
219 mcdi->resp_hdr_len = 8;
220 mcdi->resp_data_len =
221 EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
222 }
223
224 if (error && mcdi->resp_data_len == 0) {
225 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
226 mcdi->resprc = -EIO;
227 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
228 netif_err(efx, hw, efx->net_dev,
229 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
230 respseq, mcdi->seqno);
231 mcdi->resprc = -EIO;
232 } else if (error) {
233 efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
234 mcdi->resprc =
235 efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
236 } else {
237 mcdi->resprc = 0;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100238 }
239}
240
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100241static bool efx_mcdi_poll_once(struct efx_nic *efx)
242{
243 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
244
245 rmb();
246 if (!efx->type->mcdi_poll_response(efx))
247 return false;
248
249 spin_lock_bh(&mcdi->iface_lock);
250 efx_mcdi_read_response_header(efx);
251 spin_unlock_bh(&mcdi->iface_lock);
252
253 return true;
254}
255
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000256static int efx_mcdi_poll(struct efx_nic *efx)
257{
258 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000259 unsigned long time, finish;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100260 unsigned int spins;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100261 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000262
263 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100264 rc = efx_mcdi_poll_reboot(efx);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100265 if (rc) {
Ben Hutchings369327f2012-10-26 17:53:12 +0100266 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100267 mcdi->resprc = rc;
268 mcdi->resp_hdr_len = 0;
269 mcdi->resp_data_len = 0;
Ben Hutchings369327f2012-10-26 17:53:12 +0100270 spin_unlock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100271 return 0;
272 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000273
274 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
275 * because generally mcdi responses are fast. After that, back off
276 * and poll once a jiffy (approximately)
277 */
278 spins = TICK_USEC;
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000279 finish = jiffies + MCDI_RPC_TIMEOUT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000280
281 while (1) {
282 if (spins != 0) {
283 --spins;
284 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000285 } else {
286 schedule_timeout_uninterruptible(1);
287 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000288
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000289 time = jiffies;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000290
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100291 if (efx_mcdi_poll_once(efx))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000292 break;
293
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000294 if (time_after(time, finish))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000295 return -ETIMEDOUT;
296 }
297
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000298 /* Return rc=0 like wait_event_timeout() */
299 return 0;
300}
301
Ben Hutchings876be082012-10-01 20:58:35 +0100302/* Test and clear MC-rebooted flag for this port/function; reset
303 * software state as necessary.
304 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000305int efx_mcdi_poll_reboot(struct efx_nic *efx)
306{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100307 if (!efx->mcdi)
308 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000309
Ben Hutchingscd0ecc92012-12-14 21:52:56 +0000310 return efx->type->mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000311}
312
Ben Hutchingscade7152013-08-27 23:12:31 +0100313static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
314{
315 return cmpxchg(&mcdi->state,
316 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
317 MCDI_STATE_QUIESCENT;
318}
319
320static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000321{
322 /* Wait until the interface becomes QUIESCENT and we win the race
Ben Hutchingscade7152013-08-27 23:12:31 +0100323 * to mark it RUNNING_SYNC.
324 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000325 wait_event(mcdi->wq,
Ben Hutchings251111d2013-08-27 23:04:29 +0100326 cmpxchg(&mcdi->state,
Ben Hutchingscade7152013-08-27 23:12:31 +0100327 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
Ben Hutchings251111d2013-08-27 23:04:29 +0100328 MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000329}
330
331static int efx_mcdi_await_completion(struct efx_nic *efx)
332{
333 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
334
Ben Hutchings251111d2013-08-27 23:04:29 +0100335 if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
336 MCDI_RPC_TIMEOUT) == 0)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000337 return -ETIMEDOUT;
338
339 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
340 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
341 * completed the request first, then we'll just end up completing the
342 * request again, which is safe.
343 *
344 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
345 * wait_event_timeout() implicitly provides.
346 */
347 if (mcdi->mode == MCDI_MODE_POLL)
348 return efx_mcdi_poll(efx);
349
350 return 0;
351}
352
Ben Hutchingscade7152013-08-27 23:12:31 +0100353/* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
354 * requester. Return whether this was done. Does not take any locks.
355 */
356static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000357{
Ben Hutchingscade7152013-08-27 23:12:31 +0100358 if (cmpxchg(&mcdi->state,
359 MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
360 MCDI_STATE_RUNNING_SYNC) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000361 wake_up(&mcdi->wq);
362 return true;
363 }
364
365 return false;
366}
367
368static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
369{
Ben Hutchingscade7152013-08-27 23:12:31 +0100370 if (mcdi->mode == MCDI_MODE_EVENTS) {
371 struct efx_mcdi_async_param *async;
372 struct efx_nic *efx = mcdi->efx;
373
374 /* Process the asynchronous request queue */
375 spin_lock_bh(&mcdi->async_lock);
376 async = list_first_entry_or_null(
377 &mcdi->async_list, struct efx_mcdi_async_param, list);
378 if (async) {
379 mcdi->state = MCDI_STATE_RUNNING_ASYNC;
380 efx_mcdi_send_request(efx, async->cmd,
381 (const efx_dword_t *)(async + 1),
382 async->inlen);
383 mod_timer(&mcdi->async_timer,
384 jiffies + MCDI_RPC_TIMEOUT);
385 }
386 spin_unlock_bh(&mcdi->async_lock);
387
388 if (async)
389 return;
390 }
391
Ben Hutchings251111d2013-08-27 23:04:29 +0100392 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000393 wake_up(&mcdi->wq);
394}
395
Ben Hutchingscade7152013-08-27 23:12:31 +0100396/* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
397 * asynchronous completion function, and release the interface.
398 * Return whether this was done. Must be called in bh-disabled
399 * context. Will take iface_lock and async_lock.
400 */
401static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
402{
403 struct efx_nic *efx = mcdi->efx;
404 struct efx_mcdi_async_param *async;
405 size_t hdr_len, data_len;
406 efx_dword_t *outbuf;
407 int rc;
408
409 if (cmpxchg(&mcdi->state,
410 MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
411 MCDI_STATE_RUNNING_ASYNC)
412 return false;
413
414 spin_lock(&mcdi->iface_lock);
415 if (timeout) {
416 /* Ensure that if the completion event arrives later,
417 * the seqno check in efx_mcdi_ev_cpl() will fail
418 */
419 ++mcdi->seqno;
420 ++mcdi->credits;
421 rc = -ETIMEDOUT;
422 hdr_len = 0;
423 data_len = 0;
424 } else {
425 rc = mcdi->resprc;
426 hdr_len = mcdi->resp_hdr_len;
427 data_len = mcdi->resp_data_len;
428 }
429 spin_unlock(&mcdi->iface_lock);
430
431 /* Stop the timer. In case the timer function is running, we
432 * must wait for it to return so that there is no possibility
433 * of it aborting the next request.
434 */
435 if (!timeout)
436 del_timer_sync(&mcdi->async_timer);
437
438 spin_lock(&mcdi->async_lock);
439 async = list_first_entry(&mcdi->async_list,
440 struct efx_mcdi_async_param, list);
441 list_del(&async->list);
442 spin_unlock(&mcdi->async_lock);
443
444 outbuf = (efx_dword_t *)(async + 1);
445 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
446 min(async->outlen, data_len));
447 async->complete(efx, async->cookie, rc, outbuf, data_len);
448 kfree(async);
449
450 efx_mcdi_release(mcdi);
451
452 return true;
453}
454
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000455static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100456 unsigned int datalen, unsigned int mcdi_err)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000457{
458 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
459 bool wake = false;
460
461 spin_lock(&mcdi->iface_lock);
462
463 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
464 if (mcdi->credits)
465 /* The request has been cancelled */
466 --mcdi->credits;
467 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000468 netif_err(efx, hw, efx->net_dev,
469 "MC response mismatch tx seq 0x%x rx "
470 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000471 } else {
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100472 if (efx->type->mcdi_max_ver >= 2) {
473 /* MCDI v2 responses don't fit in an event */
474 efx_mcdi_read_response_header(efx);
475 } else {
476 mcdi->resprc = efx_mcdi_errno(mcdi_err);
477 mcdi->resp_hdr_len = 4;
478 mcdi->resp_data_len = datalen;
479 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000480
481 wake = true;
482 }
483
484 spin_unlock(&mcdi->iface_lock);
485
Ben Hutchingscade7152013-08-27 23:12:31 +0100486 if (wake) {
487 if (!efx_mcdi_complete_async(mcdi, false))
488 (void) efx_mcdi_complete_sync(mcdi);
489
490 /* If the interface isn't RUNNING_ASYNC or
491 * RUNNING_SYNC then we've received a duplicate
492 * completion after we've already transitioned back to
493 * QUIESCENT. [A subsequent invocation would increment
494 * seqno, so would have failed the seqno check].
495 */
496 }
497}
498
499static void efx_mcdi_timeout_async(unsigned long context)
500{
501 struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
502
503 efx_mcdi_complete_async(mcdi, true);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000504}
505
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100506static int
507efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
508{
509 if (efx->type->mcdi_max_ver < 0 ||
510 (efx->type->mcdi_max_ver < 2 &&
511 cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
512 return -EINVAL;
513
514 if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
515 (efx->type->mcdi_max_ver < 2 &&
516 inlen > MCDI_CTL_SDU_LEN_MAX_V1))
517 return -EMSGSIZE;
518
519 return 0;
520}
521
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000522int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
Ben Hutchings9528b922012-09-14 17:31:41 +0100523 const efx_dword_t *inbuf, size_t inlen,
524 efx_dword_t *outbuf, size_t outlen,
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000525 size_t *outlen_actual)
526{
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100527 int rc;
528
529 rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
530 if (rc)
531 return rc;
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100532 return efx_mcdi_rpc_finish(efx, cmd, inlen,
533 outbuf, outlen, outlen_actual);
534}
535
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100536int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
537 const efx_dword_t *inbuf, size_t inlen)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100538{
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000539 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100540 int rc;
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100541
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100542 rc = efx_mcdi_check_supported(efx, cmd, inlen);
543 if (rc)
544 return rc;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100545
Jon Cooper74cd60a2013-09-16 14:18:51 +0100546 if (efx->mc_bist_for_other_fn)
547 return -ENETDOWN;
548
Ben Hutchingscade7152013-08-27 23:12:31 +0100549 efx_mcdi_acquire_sync(mcdi);
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100550 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100551 return 0;
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100552}
553
Ben Hutchingscade7152013-08-27 23:12:31 +0100554/**
555 * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
556 * @efx: NIC through which to issue the command
557 * @cmd: Command type number
558 * @inbuf: Command parameters
559 * @inlen: Length of command parameters, in bytes
560 * @outlen: Length to allocate for response buffer, in bytes
561 * @complete: Function to be called on completion or cancellation.
562 * @cookie: Arbitrary value to be passed to @complete.
563 *
564 * This function does not sleep and therefore may be called in atomic
565 * context. It will fail if event queues are disabled or if MCDI
566 * event completions have been disabled due to an error.
567 *
568 * If it succeeds, the @complete function will be called exactly once
569 * in atomic context, when one of the following occurs:
570 * (a) the completion event is received (in NAPI context)
571 * (b) event queues are disabled (in the process that disables them)
572 * (c) the request times-out (in timer context)
573 */
574int
575efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
576 const efx_dword_t *inbuf, size_t inlen, size_t outlen,
577 efx_mcdi_async_completer *complete, unsigned long cookie)
578{
579 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
580 struct efx_mcdi_async_param *async;
581 int rc;
582
583 rc = efx_mcdi_check_supported(efx, cmd, inlen);
584 if (rc)
585 return rc;
586
Jon Cooper74cd60a2013-09-16 14:18:51 +0100587 if (efx->mc_bist_for_other_fn)
588 return -ENETDOWN;
589
Ben Hutchingscade7152013-08-27 23:12:31 +0100590 async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
591 GFP_ATOMIC);
592 if (!async)
593 return -ENOMEM;
594
595 async->cmd = cmd;
596 async->inlen = inlen;
597 async->outlen = outlen;
598 async->complete = complete;
599 async->cookie = cookie;
600 memcpy(async + 1, inbuf, inlen);
601
602 spin_lock_bh(&mcdi->async_lock);
603
604 if (mcdi->mode == MCDI_MODE_EVENTS) {
605 list_add_tail(&async->list, &mcdi->async_list);
606
607 /* If this is at the front of the queue, try to start it
608 * immediately
609 */
610 if (mcdi->async_list.next == &async->list &&
611 efx_mcdi_acquire_async(mcdi)) {
612 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
613 mod_timer(&mcdi->async_timer,
614 jiffies + MCDI_RPC_TIMEOUT);
615 }
616 } else {
617 kfree(async);
618 rc = -ENETDOWN;
619 }
620
621 spin_unlock_bh(&mcdi->async_lock);
622
623 return rc;
624}
625
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100626int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
Ben Hutchings9528b922012-09-14 17:31:41 +0100627 efx_dword_t *outbuf, size_t outlen,
628 size_t *outlen_actual)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100629{
630 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
631 int rc;
632
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000633 if (mcdi->mode == MCDI_MODE_POLL)
634 rc = efx_mcdi_poll(efx);
635 else
636 rc = efx_mcdi_await_completion(efx);
637
638 if (rc != 0) {
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100639 netif_err(efx, hw, efx->net_dev,
640 "MC command 0x%x inlen %d mode %d timed out\n",
641 cmd, (int)inlen, mcdi->mode);
642
643 if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
644 netif_err(efx, hw, efx->net_dev,
645 "MCDI request was completed without an event\n");
646 rc = 0;
647 }
648
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000649 /* Close the race with efx_mcdi_ev_cpl() executing just too late
650 * and completing a request we've just cancelled, by ensuring
651 * that the seqno check therein fails.
652 */
653 spin_lock_bh(&mcdi->iface_lock);
654 ++mcdi->seqno;
655 ++mcdi->credits;
656 spin_unlock_bh(&mcdi->iface_lock);
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100657 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000658
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100659 if (rc == 0) {
Ben Hutchings369327f2012-10-26 17:53:12 +0100660 size_t hdr_len, data_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000661
662 /* At the very least we need a memory barrier here to ensure
663 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
664 * a spurious efx_mcdi_ev_cpl() running concurrently by
665 * acquiring the iface_lock. */
666 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100667 rc = mcdi->resprc;
Ben Hutchings369327f2012-10-26 17:53:12 +0100668 hdr_len = mcdi->resp_hdr_len;
669 data_len = mcdi->resp_data_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000670 spin_unlock_bh(&mcdi->iface_lock);
671
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100672 BUG_ON(rc > 0);
673
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000674 if (rc == 0) {
Ben Hutchings369327f2012-10-26 17:53:12 +0100675 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
676 min(outlen, data_len));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000677 if (outlen_actual != NULL)
Ben Hutchings369327f2012-10-26 17:53:12 +0100678 *outlen_actual = data_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000679 } else if (cmd == MC_CMD_REBOOT && rc == -EIO)
680 ; /* Don't reset if MC_CMD_REBOOT returns EIO */
681 else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000682 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
683 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000684 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
685 } else
Ben Hutchingsf18ca362010-12-02 13:46:09 +0000686 netif_dbg(efx, hw, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000687 "MC command 0x%x inlen %d failed rc=%d\n",
688 cmd, (int)inlen, -rc);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000689
690 if (rc == -EIO || rc == -EINTR) {
691 msleep(MCDI_STATUS_SLEEP_MS);
692 efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100693 mcdi->new_epoch = true;
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000694 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000695 }
696
697 efx_mcdi_release(mcdi);
698 return rc;
699}
700
Ben Hutchingscade7152013-08-27 23:12:31 +0100701/* Switch to polled MCDI completions. This can be called in various
702 * error conditions with various locks held, so it must be lockless.
703 * Caller is responsible for flushing asynchronous requests later.
704 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000705void efx_mcdi_mode_poll(struct efx_nic *efx)
706{
707 struct efx_mcdi_iface *mcdi;
708
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100709 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000710 return;
711
712 mcdi = efx_mcdi(efx);
713 if (mcdi->mode == MCDI_MODE_POLL)
714 return;
715
716 /* We can switch from event completion to polled completion, because
717 * mcdi requests are always completed in shared memory. We do this by
718 * switching the mode to POLL'd then completing the request.
719 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
720 *
721 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
Ben Hutchingscade7152013-08-27 23:12:31 +0100722 * which efx_mcdi_complete_sync() provides for us.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000723 */
724 mcdi->mode = MCDI_MODE_POLL;
725
Ben Hutchingscade7152013-08-27 23:12:31 +0100726 efx_mcdi_complete_sync(mcdi);
727}
728
729/* Flush any running or queued asynchronous requests, after event processing
730 * is stopped
731 */
732void efx_mcdi_flush_async(struct efx_nic *efx)
733{
734 struct efx_mcdi_async_param *async, *next;
735 struct efx_mcdi_iface *mcdi;
736
737 if (!efx->mcdi)
738 return;
739
740 mcdi = efx_mcdi(efx);
741
742 /* We must be in polling mode so no more requests can be queued */
743 BUG_ON(mcdi->mode != MCDI_MODE_POLL);
744
745 del_timer_sync(&mcdi->async_timer);
746
747 /* If a request is still running, make sure we give the MC
748 * time to complete it so that the response won't overwrite our
749 * next request.
750 */
751 if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
752 efx_mcdi_poll(efx);
753 mcdi->state = MCDI_STATE_QUIESCENT;
754 }
755
756 /* Nothing else will access the async list now, so it is safe
757 * to walk it without holding async_lock. If we hold it while
758 * calling a completer then lockdep may warn that we have
759 * acquired locks in the wrong order.
760 */
761 list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
762 async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
763 list_del(&async->list);
764 kfree(async);
765 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000766}
767
768void efx_mcdi_mode_event(struct efx_nic *efx)
769{
770 struct efx_mcdi_iface *mcdi;
771
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100772 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000773 return;
774
775 mcdi = efx_mcdi(efx);
776
777 if (mcdi->mode == MCDI_MODE_EVENTS)
778 return;
779
780 /* We can't switch from polled to event completion in the middle of a
781 * request, because the completion method is specified in the request.
782 * So acquire the interface to serialise the requestors. We don't need
783 * to acquire the iface_lock to change the mode here, but we do need a
784 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
785 * efx_mcdi_acquire() provides.
786 */
Ben Hutchingscade7152013-08-27 23:12:31 +0100787 efx_mcdi_acquire_sync(mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000788 mcdi->mode = MCDI_MODE_EVENTS;
789 efx_mcdi_release(mcdi);
790}
791
792static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
793{
794 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
795
796 /* If there is an outstanding MCDI request, it has been terminated
797 * either by a BADASSERT or REBOOT event. If the mcdi interface is
798 * in polled mode, then do nothing because the MC reboot handler will
799 * set the header correctly. However, if the mcdi interface is waiting
800 * for a CMDDONE event it won't receive it [and since all MCDI events
801 * are sent to the same queue, we can't be racing with
802 * efx_mcdi_ev_cpl()]
803 *
Ben Hutchingscade7152013-08-27 23:12:31 +0100804 * If there is an outstanding asynchronous request, we can't
805 * complete it now (efx_mcdi_complete() would deadlock). The
806 * reset process will take care of this.
807 *
808 * There's a race here with efx_mcdi_send_request(), because
809 * we might receive a REBOOT event *before* the request has
810 * been copied out. In polled mode (during startup) this is
811 * irrelevant, because efx_mcdi_complete_sync() is ignored. In
812 * event mode, this condition is just an edge-case of
813 * receiving a REBOOT event after posting the MCDI
814 * request. Did the mc reboot before or after the copyout? The
815 * best we can do always is just return failure.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000816 */
817 spin_lock(&mcdi->iface_lock);
Ben Hutchingscade7152013-08-27 23:12:31 +0100818 if (efx_mcdi_complete_sync(mcdi)) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000819 if (mcdi->mode == MCDI_MODE_EVENTS) {
820 mcdi->resprc = rc;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100821 mcdi->resp_hdr_len = 0;
822 mcdi->resp_data_len = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +0000823 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000824 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000825 } else {
826 int count;
827
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000828 /* Consume the status word since efx_mcdi_rpc_finish() won't */
829 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
830 if (efx_mcdi_poll_reboot(efx))
831 break;
832 udelay(MCDI_STATUS_DELAY_US);
833 }
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100834 mcdi->new_epoch = true;
Daniel Pieczkodfdaa952013-09-18 10:16:24 +0100835
836 /* Nobody was waiting for an MCDI request, so trigger a reset */
837 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000838 }
839
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000840 spin_unlock(&mcdi->iface_lock);
841}
842
Jon Cooper74cd60a2013-09-16 14:18:51 +0100843/* The MC is going down in to BIST mode. set the BIST flag to block
844 * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
845 * (which doesn't actually execute a reset, it waits for the controlling
846 * function to reset it).
847 */
848static void efx_mcdi_ev_bist(struct efx_nic *efx)
849{
850 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
851
852 spin_lock(&mcdi->iface_lock);
853 efx->mc_bist_for_other_fn = true;
854 if (efx_mcdi_complete_sync(mcdi)) {
855 if (mcdi->mode == MCDI_MODE_EVENTS) {
856 mcdi->resprc = -EIO;
857 mcdi->resp_hdr_len = 0;
858 mcdi->resp_data_len = 0;
859 ++mcdi->credits;
860 }
861 }
862 mcdi->new_epoch = true;
863 efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
864 spin_unlock(&mcdi->iface_lock);
865}
866
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000867/* Called from falcon_process_eventq for MCDI events */
868void efx_mcdi_process_event(struct efx_channel *channel,
869 efx_qword_t *event)
870{
871 struct efx_nic *efx = channel->efx;
872 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
873 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
874
875 switch (code) {
876 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000877 netif_err(efx, hw, efx->net_dev,
878 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100879 efx_mcdi_ev_death(efx, -EINTR);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000880 break;
881
882 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +0000883 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000884 break;
885
886 case MCDI_EVENT_CODE_CMDDONE:
887 efx_mcdi_ev_cpl(efx,
888 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
889 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
890 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
891 break;
892
893 case MCDI_EVENT_CODE_LINKCHANGE:
894 efx_mcdi_process_link_change(efx, event);
895 break;
896 case MCDI_EVENT_CODE_SENSOREVT:
897 efx_mcdi_sensor_event(efx, event);
898 break;
899 case MCDI_EVENT_CODE_SCHEDERR:
Robert Stonehouse2d9955b2013-10-07 18:44:17 +0100900 netif_dbg(efx, hw, efx->net_dev,
901 "MC Scheduler alert (0x%x)\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000902 break;
903 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings8127d662013-08-29 19:19:29 +0100904 case MCDI_EVENT_CODE_MC_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +0000905 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100906 efx_mcdi_ev_death(efx, -EIO);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000907 break;
Jon Cooper74cd60a2013-09-16 14:18:51 +0100908 case MCDI_EVENT_CODE_MC_BIST:
909 netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
910 efx_mcdi_ev_bist(efx);
911 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000912 case MCDI_EVENT_CODE_MAC_STATS_DMA:
913 /* MAC stats are gather lazily. We can ignore this. */
914 break;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +0000915 case MCDI_EVENT_CODE_FLR:
916 efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF));
917 break;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100918 case MCDI_EVENT_CODE_PTP_RX:
919 case MCDI_EVENT_CODE_PTP_FAULT:
920 case MCDI_EVENT_CODE_PTP_PPS:
921 efx_ptp_event(efx, event);
922 break;
Ben Hutchings8127d662013-08-29 19:19:29 +0100923 case MCDI_EVENT_CODE_TX_FLUSH:
924 case MCDI_EVENT_CODE_RX_FLUSH:
925 /* Two flush events will be sent: one to the same event
926 * queue as completions, and one to event queue 0.
927 * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
928 * flag will be set, and we should ignore the event
929 * because we want to wait for all completions.
930 */
931 BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
932 MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
933 if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
934 efx_ef10_handle_drain_event(efx);
935 break;
Alexandre Rames3de82b92013-06-13 11:36:15 +0100936 case MCDI_EVENT_CODE_TX_ERR:
937 case MCDI_EVENT_CODE_RX_ERR:
938 netif_err(efx, hw, efx->net_dev,
939 "%s DMA error (event: "EFX_QWORD_FMT")\n",
940 code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
941 EFX_QWORD_VAL(*event));
942 efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
943 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000944 default:
Ben Hutchings62776d02010-06-23 11:30:07 +0000945 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
946 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000947 }
948}
949
950/**************************************************************************
951 *
952 * Specific request functions
953 *
954 **************************************************************************
955 */
956
Ben Hutchingse5f0fd22011-02-24 23:57:47 +0000957void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000958{
Ben Hutchings8127d662013-08-29 19:19:29 +0100959 MCDI_DECLARE_BUF(outbuf,
960 max(MC_CMD_GET_VERSION_OUT_LEN,
961 MC_CMD_GET_CAPABILITIES_OUT_LEN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000962 size_t outlength;
963 const __le16 *ver_words;
Ben Hutchings8127d662013-08-29 19:19:29 +0100964 size_t offset;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000965 int rc;
966
967 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000968 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
969 outbuf, sizeof(outbuf), &outlength);
970 if (rc)
971 goto fail;
Ben Hutchings05a93202011-12-20 00:44:06 +0000972 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +0000973 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000974 goto fail;
975 }
976
977 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchings8127d662013-08-29 19:19:29 +0100978 offset = snprintf(buf, len, "%u.%u.%u.%u",
979 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
980 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
981
982 /* EF10 may have multiple datapath firmware variants within a
983 * single version. Report which variants are running.
984 */
985 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
986 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
987 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
988 outbuf, sizeof(outbuf), &outlength);
989 if (rc || outlength < MC_CMD_GET_CAPABILITIES_OUT_LEN)
990 offset += snprintf(
991 buf + offset, len - offset, " rx? tx?");
992 else
993 offset += snprintf(
994 buf + offset, len - offset, " rx%x tx%x",
995 MCDI_WORD(outbuf,
996 GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID),
997 MCDI_WORD(outbuf,
998 GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID));
999
1000 /* It's theoretically possible for the string to exceed 31
1001 * characters, though in practice the first three version
1002 * components are short enough that this doesn't happen.
1003 */
1004 if (WARN_ON(offset >= len))
1005 buf[0] = 0;
1006 }
1007
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001008 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001009
1010fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001011 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001012 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001013}
1014
Ben Hutchings4c75b43a2013-08-29 19:04:03 +01001015static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
1016 bool *was_attached)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001017{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001018 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001019 MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001020 size_t outlen;
1021 int rc;
1022
1023 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
1024 driver_operating ? 1 : 0);
1025 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
Ben Hutchingsf2b0bef2013-08-20 20:35:50 +01001026 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001027
1028 rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
1029 outbuf, sizeof(outbuf), &outlen);
1030 if (rc)
1031 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001032 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
1033 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001034 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001035 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001036
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001037 /* We currently assume we have control of the external link
1038 * and are completely trusted by firmware. Abort probing
1039 * if that's not true for this function.
1040 */
1041 if (driver_operating &&
1042 outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN &&
1043 (MCDI_DWORD(outbuf, DRV_ATTACH_EXT_OUT_FUNC_FLAGS) &
1044 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1045 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) !=
1046 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1047 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) {
1048 netif_err(efx, probe, efx->net_dev,
1049 "This driver version only supports one function per port\n");
1050 return -ENODEV;
1051 }
1052
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001053 if (was_attached != NULL)
1054 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
1055 return 0;
1056
1057fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001058 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001059 return rc;
1060}
1061
1062int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001063 u16 *fw_subtype_list, u32 *capabilities)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001064{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001065 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001066 size_t outlen, i;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001067 int port_num = efx_port_num(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001068 int rc;
1069
1070 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
1071
1072 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
1073 outbuf, sizeof(outbuf), &outlen);
1074 if (rc)
1075 goto fail;
1076
Ben Hutchings05a93202011-12-20 00:44:06 +00001077 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001078 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001079 goto fail;
1080 }
1081
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001082 if (mac_address)
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001083 memcpy(mac_address,
1084 port_num ?
1085 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
1086 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0),
1087 ETH_ALEN);
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001088 if (fw_subtype_list) {
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001089 for (i = 0;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001090 i < MCDI_VAR_ARRAY_LEN(outlen,
1091 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
1092 i++)
1093 fw_subtype_list[i] = MCDI_ARRAY_WORD(
1094 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
1095 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
1096 fw_subtype_list[i] = 0;
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001097 }
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001098 if (capabilities) {
1099 if (port_num)
1100 *capabilities = MCDI_DWORD(outbuf,
1101 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1102 else
1103 *capabilities = MCDI_DWORD(outbuf,
1104 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1105 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001106
1107 return 0;
1108
1109fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
1111 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001112
1113 return rc;
1114}
1115
1116int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
1117{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001118 MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001119 u32 dest = 0;
1120 int rc;
1121
1122 if (uart)
1123 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
1124 if (evq)
1125 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
1126
1127 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
1128 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
1129
1130 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
1131
1132 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
1133 NULL, 0, NULL);
1134 if (rc)
1135 goto fail;
1136
1137 return 0;
1138
1139fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001141 return rc;
1142}
1143
1144int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
1145{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001146 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001147 size_t outlen;
1148 int rc;
1149
1150 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
1151
1152 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
1153 outbuf, sizeof(outbuf), &outlen);
1154 if (rc)
1155 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001156 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
1157 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001158 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001159 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001160
1161 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
1162 return 0;
1163
1164fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1166 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001167 return rc;
1168}
1169
1170int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
1171 size_t *size_out, size_t *erase_size_out,
1172 bool *protected_out)
1173{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001174 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
1175 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001176 size_t outlen;
1177 int rc;
1178
1179 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
1180
1181 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
1182 outbuf, sizeof(outbuf), &outlen);
1183 if (rc)
1184 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001185 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
1186 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001187 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001188 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001189
1190 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
1191 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
1192 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +00001193 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001194 return 0;
1195
1196fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001197 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001198 return rc;
1199}
1200
Ben Hutchings2e803402010-02-03 09:31:01 +00001201static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
1202{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001203 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
1204 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
Ben Hutchings2e803402010-02-03 09:31:01 +00001205 int rc;
1206
1207 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
1208
1209 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
1210 outbuf, sizeof(outbuf), NULL);
1211 if (rc)
1212 return rc;
1213
1214 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
1215 case MC_CMD_NVRAM_TEST_PASS:
1216 case MC_CMD_NVRAM_TEST_NOTSUPP:
1217 return 0;
1218 default:
1219 return -EIO;
1220 }
1221}
1222
1223int efx_mcdi_nvram_test_all(struct efx_nic *efx)
1224{
1225 u32 nvram_types;
1226 unsigned int type;
1227 int rc;
1228
1229 rc = efx_mcdi_nvram_types(efx, &nvram_types);
1230 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001231 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +00001232
1233 type = 0;
1234 while (nvram_types != 0) {
1235 if (nvram_types & 1) {
1236 rc = efx_mcdi_nvram_test(efx, type);
1237 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001238 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +00001239 }
1240 type++;
1241 nvram_types >>= 1;
1242 }
1243
1244 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +00001245
1246fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +00001247 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
1248 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001249fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +00001250 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001251 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +00001252}
1253
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001254static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001255{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001256 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
1257 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001258 unsigned int flags, index;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001259 const char *reason;
1260 size_t outlen;
1261 int retry;
1262 int rc;
1263
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001264 /* Attempt to read any stored assertion state before we reboot
1265 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001266 * because a boot-time assertion might cause this command to fail
1267 * with EINTR. And once again because GET_ASSERTS can race with
1268 * MC_CMD_REBOOT running on the other port. */
1269 retry = 2;
1270 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001271 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001272 rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS,
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001273 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1274 outbuf, sizeof(outbuf), &outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001275 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1276
1277 if (rc)
1278 return rc;
1279 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001280 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001281
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001282 /* Print out any recorded assertion state */
1283 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001284 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1285 return 0;
1286
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001287 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1288 ? "system-level assertion"
1289 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1290 ? "thread-level assertion"
1291 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1292 ? "watchdog reset"
1293 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +00001294 netif_err(efx, hw, efx->net_dev,
1295 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1296 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1297 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001298
1299 /* Print out the registers */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001300 for (index = 0;
1301 index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1302 index++)
1303 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
1304 1 + index,
1305 MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
1306 index));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001307
1308 return 0;
1309}
1310
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001311static void efx_mcdi_exit_assertion(struct efx_nic *efx)
1312{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001313 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001314
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001315 /* If the MC is running debug firmware, it might now be
1316 * waiting for a debugger to attach, but we just want it to
1317 * reboot. We set a flag that makes the command a no-op if it
1318 * has already done so. We don't know what return code to
1319 * expect (0 or -EIO), so ignore it.
1320 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001321 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1322 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1323 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001324 (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1325 NULL, 0, NULL);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001326}
1327
1328int efx_mcdi_handle_assertion(struct efx_nic *efx)
1329{
1330 int rc;
1331
1332 rc = efx_mcdi_read_assertion(efx);
1333 if (rc)
1334 return rc;
1335
1336 efx_mcdi_exit_assertion(efx);
1337
1338 return 0;
1339}
1340
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001341void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1342{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001343 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001344 int rc;
1345
1346 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1347 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1348 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1349
1350 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1351
1352 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1353
1354 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1355 NULL, 0, NULL);
1356 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001357 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1358 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001359}
1360
Ben Hutchings6bff8612012-09-18 02:33:52 +01001361static int efx_mcdi_reset_port(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001362{
Ben Hutchings05a93202011-12-20 00:44:06 +00001363 int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001364 if (rc)
Ben Hutchings62776d02010-06-23 11:30:07 +00001365 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1366 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001367 return rc;
1368}
1369
Ben Hutchings6bff8612012-09-18 02:33:52 +01001370static int efx_mcdi_reset_mc(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001371{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001372 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001373 int rc;
1374
1375 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1376 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1377 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1378 NULL, 0, NULL);
1379 /* White is black, and up is down */
1380 if (rc == -EIO)
1381 return 0;
1382 if (rc == 0)
1383 rc = -EIO;
Ben Hutchings62776d02010-06-23 11:30:07 +00001384 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001385 return rc;
1386}
1387
Ben Hutchings6bff8612012-09-18 02:33:52 +01001388enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1389{
1390 return RESET_TYPE_RECOVER_OR_ALL;
1391}
1392
1393int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1394{
1395 int rc;
1396
1397 /* Recover from a failed assertion pre-reset */
1398 rc = efx_mcdi_handle_assertion(efx);
1399 if (rc)
1400 return rc;
1401
1402 if (method == RESET_TYPE_WORLD)
1403 return efx_mcdi_reset_mc(efx);
1404 else
1405 return efx_mcdi_reset_port(efx);
1406}
1407
stephen hemmingerd2156972010-10-18 05:27:31 +00001408static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1409 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001410{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001411 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1412 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001413 size_t outlen;
1414 int rc;
1415
1416 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1417 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1418 MC_CMD_FILTER_MODE_SIMPLE);
1419 memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN);
1420
1421 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1422 outbuf, sizeof(outbuf), &outlen);
1423 if (rc)
1424 goto fail;
1425
1426 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001427 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001428 goto fail;
1429 }
1430
1431 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1432
1433 return 0;
1434
1435fail:
1436 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001437 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001438 return rc;
1439
1440}
1441
1442
1443int
1444efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1445{
1446 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1447}
1448
1449
1450int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1451{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001452 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001453 size_t outlen;
1454 int rc;
1455
1456 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1457 outbuf, sizeof(outbuf), &outlen);
1458 if (rc)
1459 goto fail;
1460
1461 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001462 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001463 goto fail;
1464 }
1465
1466 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1467
1468 return 0;
1469
1470fail:
1471 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001472 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001473 return rc;
1474}
1475
1476
1477int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1478{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001479 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001480 int rc;
1481
1482 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1483
1484 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1485 NULL, 0, NULL);
1486 if (rc)
1487 goto fail;
1488
1489 return 0;
1490
1491fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001492 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001493 return rc;
1494}
1495
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001496int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1497{
1498 struct efx_channel *channel;
1499 struct efx_rx_queue *rx_queue;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001500 MCDI_DECLARE_BUF(inbuf,
1501 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001502 int rc, count;
1503
Ben Hutchings45078372012-09-19 02:53:34 +01001504 BUILD_BUG_ON(EFX_MAX_CHANNELS >
1505 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1506
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001507 count = 0;
1508 efx_for_each_channel(channel, efx) {
1509 efx_for_each_channel_rx_queue(rx_queue, channel) {
1510 if (rx_queue->flush_pending) {
1511 rx_queue->flush_pending = false;
1512 atomic_dec(&efx->rxq_flush_pending);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001513 MCDI_SET_ARRAY_DWORD(
1514 inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1515 count, efx_rx_queue_index(rx_queue));
1516 count++;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001517 }
1518 }
1519 }
1520
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001521 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1522 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
Ben Hutchingsbbec9692012-09-11 18:25:13 +01001523 WARN_ON(rc < 0);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001524
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001525 return rc;
1526}
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001527
1528int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1529{
1530 int rc;
1531
1532 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1533 if (rc)
1534 goto fail;
1535
1536 return 0;
1537
1538fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001539 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001540 return rc;
1541}
1542
Ben Hutchings8127d662013-08-29 19:19:29 +01001543int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled)
1544{
1545 MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
1546
1547 BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
1548 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
1549 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
1550 return efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
1551 NULL, 0, NULL);
1552}
1553
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001554#ifdef CONFIG_SFC_MTD
1555
1556#define EFX_MCDI_NVRAM_LEN_MAX 128
1557
1558static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
1559{
1560 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
1561 int rc;
1562
1563 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
1564
1565 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
1566
1567 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
1568 NULL, 0, NULL);
1569 if (rc)
1570 goto fail;
1571
1572 return 0;
1573
1574fail:
1575 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1576 return rc;
1577}
1578
1579static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
1580 loff_t offset, u8 *buffer, size_t length)
1581{
1582 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
1583 MCDI_DECLARE_BUF(outbuf,
1584 MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1585 size_t outlen;
1586 int rc;
1587
1588 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
1589 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
1590 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
1591
1592 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
1593 outbuf, sizeof(outbuf), &outlen);
1594 if (rc)
1595 goto fail;
1596
1597 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
1598 return 0;
1599
1600fail:
1601 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1602 return rc;
1603}
1604
1605static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
1606 loff_t offset, const u8 *buffer, size_t length)
1607{
1608 MCDI_DECLARE_BUF(inbuf,
1609 MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1610 int rc;
1611
1612 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
1613 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
1614 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
1615 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
1616
1617 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
1618
1619 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
1620 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
1621 NULL, 0, NULL);
1622 if (rc)
1623 goto fail;
1624
1625 return 0;
1626
1627fail:
1628 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1629 return rc;
1630}
1631
1632static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
1633 loff_t offset, size_t length)
1634{
1635 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
1636 int rc;
1637
1638 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
1639 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
1640 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
1641
1642 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
1643
1644 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
1645 NULL, 0, NULL);
1646 if (rc)
1647 goto fail;
1648
1649 return 0;
1650
1651fail:
1652 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1653 return rc;
1654}
1655
1656static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
1657{
1658 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
1659 int rc;
1660
1661 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
1662
1663 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
1664
1665 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
1666 NULL, 0, NULL);
1667 if (rc)
1668 goto fail;
1669
1670 return 0;
1671
1672fail:
1673 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1674 return rc;
1675}
1676
1677int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
1678 size_t len, size_t *retlen, u8 *buffer)
1679{
1680 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1681 struct efx_nic *efx = mtd->priv;
1682 loff_t offset = start;
1683 loff_t end = min_t(loff_t, start + len, mtd->size);
1684 size_t chunk;
1685 int rc = 0;
1686
1687 while (offset < end) {
1688 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1689 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
1690 buffer, chunk);
1691 if (rc)
1692 goto out;
1693 offset += chunk;
1694 buffer += chunk;
1695 }
1696out:
1697 *retlen = offset - start;
1698 return rc;
1699}
1700
1701int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
1702{
1703 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1704 struct efx_nic *efx = mtd->priv;
1705 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
1706 loff_t end = min_t(loff_t, start + len, mtd->size);
1707 size_t chunk = part->common.mtd.erasesize;
1708 int rc = 0;
1709
1710 if (!part->updating) {
1711 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1712 if (rc)
1713 goto out;
1714 part->updating = true;
1715 }
1716
1717 /* The MCDI interface can in fact do multiple erase blocks at once;
1718 * but erasing may be slow, so we make multiple calls here to avoid
1719 * tripping the MCDI RPC timeout. */
1720 while (offset < end) {
1721 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
1722 chunk);
1723 if (rc)
1724 goto out;
1725 offset += chunk;
1726 }
1727out:
1728 return rc;
1729}
1730
1731int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
1732 size_t len, size_t *retlen, const u8 *buffer)
1733{
1734 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1735 struct efx_nic *efx = mtd->priv;
1736 loff_t offset = start;
1737 loff_t end = min_t(loff_t, start + len, mtd->size);
1738 size_t chunk;
1739 int rc = 0;
1740
1741 if (!part->updating) {
1742 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1743 if (rc)
1744 goto out;
1745 part->updating = true;
1746 }
1747
1748 while (offset < end) {
1749 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1750 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
1751 buffer, chunk);
1752 if (rc)
1753 goto out;
1754 offset += chunk;
1755 buffer += chunk;
1756 }
1757out:
1758 *retlen = offset - start;
1759 return rc;
1760}
1761
1762int efx_mcdi_mtd_sync(struct mtd_info *mtd)
1763{
1764 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1765 struct efx_nic *efx = mtd->priv;
1766 int rc = 0;
1767
1768 if (part->updating) {
1769 part->updating = false;
1770 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
1771 }
1772
1773 return rc;
1774}
1775
1776void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
1777{
1778 struct efx_mcdi_mtd_partition *mcdi_part =
1779 container_of(part, struct efx_mcdi_mtd_partition, common);
1780 struct efx_nic *efx = part->mtd.priv;
1781
1782 snprintf(part->name, sizeof(part->name), "%s %s:%02x",
1783 efx->name, part->type_name, mcdi_part->fw_subtype);
1784}
1785
1786#endif /* CONFIG_SFC_MTD */