blob: 31eda32559669f512f15f25ed0b539ac3535ec22 [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);
Edward Creee2835462014-04-16 19:27:48 +010055static void efx_mcdi_abandon(struct efx_nic *efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000056
Ben Hutchingsf073dde2012-09-18 02:33:55 +010057int efx_mcdi_init(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000058{
59 struct efx_mcdi_iface *mcdi;
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010060 bool already_attached;
Edward Cree75aba2a2015-05-27 13:13:54 +010061 int rc = -ENOMEM;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000062
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010063 efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
64 if (!efx->mcdi)
Edward Cree75aba2a2015-05-27 13:13:54 +010065 goto fail;
Ben Hutchingsf3ad5002012-09-18 02:33:56 +010066
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000067 mcdi = efx_mcdi(efx);
Ben Hutchingscade7152013-08-27 23:12:31 +010068 mcdi->efx = efx;
Edward Cree75aba2a2015-05-27 13:13:54 +010069#ifdef CONFIG_SFC_MCDI_LOGGING
70 /* consuming code assumes buffer is page-sized */
71 mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
72 if (!mcdi->logging_buffer)
73 goto fail1;
74#endif
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000075 init_waitqueue_head(&mcdi->wq);
76 spin_lock_init(&mcdi->iface_lock);
Ben Hutchings251111d2013-08-27 23:04:29 +010077 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000078 mcdi->mode = MCDI_MODE_POLL;
Ben Hutchingscade7152013-08-27 23:12:31 +010079 spin_lock_init(&mcdi->async_lock);
80 INIT_LIST_HEAD(&mcdi->async_list);
81 setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
82 (unsigned long)mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +000083
84 (void) efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +010085 mcdi->new_epoch = true;
Ben Hutchingsf073dde2012-09-18 02:33:55 +010086
87 /* Recover from a failed assertion before probing */
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010088 rc = efx_mcdi_handle_assertion(efx);
89 if (rc)
Edward Cree75aba2a2015-05-27 13:13:54 +010090 goto fail2;
Ben Hutchings4c75b43a2013-08-29 19:04:03 +010091
92 /* Let the MC (and BMC, if this is a LOM) know that the driver
93 * is loaded. We should do this before we reset the NIC.
94 */
95 rc = efx_mcdi_drv_attach(efx, true, &already_attached);
96 if (rc) {
97 netif_err(efx, probe, efx->net_dev,
98 "Unable to register driver with MCPU\n");
Edward Cree75aba2a2015-05-27 13:13:54 +010099 goto fail2;
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100100 }
101 if (already_attached)
102 /* Not a fatal error */
103 netif_err(efx, probe, efx->net_dev,
104 "Host already registered with MCPU\n");
105
Ben Hutchings0bcf4a62013-10-18 19:21:45 +0100106 if (efx->mcdi->fn_flags &
107 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
108 efx->primary = efx;
109
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100110 return 0;
Edward Cree75aba2a2015-05-27 13:13:54 +0100111fail2:
112#ifdef CONFIG_SFC_MCDI_LOGGING
113 free_page((unsigned long)mcdi->logging_buffer);
114fail1:
115#endif
116 kfree(efx->mcdi);
117 efx->mcdi = NULL;
118fail:
119 return rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000120}
121
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100122void efx_mcdi_fini(struct efx_nic *efx)
123{
Ben Hutchings4c75b43a2013-08-29 19:04:03 +0100124 if (!efx->mcdi)
125 return;
126
127 BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
128
129 /* Relinquish the device (back to the BMC, if this is a LOM) */
130 efx_mcdi_drv_attach(efx, false, NULL);
131
Edward Cree75aba2a2015-05-27 13:13:54 +0100132#ifdef CONFIG_SFC_MCDI_LOGGING
133 free_page((unsigned long)efx->mcdi->iface.logging_buffer);
134#endif
135
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100136 kfree(efx->mcdi);
137}
138
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100139static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
140 const efx_dword_t *inbuf, size_t inlen)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000141{
142 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Edward Cree75aba2a2015-05-27 13:13:54 +0100143#ifdef CONFIG_SFC_MCDI_LOGGING
144 char *buf = mcdi->logging_buffer; /* page-sized */
145#endif
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100146 efx_dword_t hdr[2];
147 size_t hdr_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000148 u32 xflags, seqno;
149
Ben Hutchings251111d2013-08-27 23:04:29 +0100150 BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000151
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100152 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
153 spin_lock_bh(&mcdi->iface_lock);
154 ++mcdi->seqno;
155 spin_unlock_bh(&mcdi->iface_lock);
156
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000157 seqno = mcdi->seqno & SEQ_MASK;
158 xflags = 0;
159 if (mcdi->mode == MCDI_MODE_EVENTS)
160 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
161
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100162 if (efx->type->mcdi_max_ver == 1) {
163 /* MCDI v1 */
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100164 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100165 MCDI_HEADER_RESPONSE, 0,
166 MCDI_HEADER_RESYNC, 1,
167 MCDI_HEADER_CODE, cmd,
168 MCDI_HEADER_DATALEN, inlen,
169 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100170 MCDI_HEADER_XFLAGS, xflags,
171 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100172 hdr_len = 4;
173 } else {
174 /* MCDI v2 */
175 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100176 EFX_POPULATE_DWORD_7(hdr[0],
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100177 MCDI_HEADER_RESPONSE, 0,
178 MCDI_HEADER_RESYNC, 1,
179 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
180 MCDI_HEADER_DATALEN, 0,
181 MCDI_HEADER_SEQ, seqno,
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100182 MCDI_HEADER_XFLAGS, xflags,
183 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100184 EFX_POPULATE_DWORD_2(hdr[1],
185 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
186 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
187 hdr_len = 8;
188 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000189
Edward Cree75aba2a2015-05-27 13:13:54 +0100190#ifdef CONFIG_SFC_MCDI_LOGGING
191 if (!WARN_ON_ONCE(!buf)) {
192 int bytes = 0;
193 int i;
194 /* Lengths should always be a whole number of dwords, so scream
195 * if they're not.
196 */
197 WARN_ON_ONCE(hdr_len % 4);
198 WARN_ON_ONCE(inlen % 4);
199
200 /* We own the logging buffer, as only one MCDI can be in
201 * progress on a NIC at any one time. So no need for locking.
202 */
203 for (i = 0; i < hdr_len / 4 && bytes < PAGE_SIZE; i++)
204 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
205 " %08x", le32_to_cpu(hdr[i].u32[0]));
206
207 for (i = 0; i < inlen / 4 && bytes < PAGE_SIZE; i++)
208 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
209 " %08x", le32_to_cpu(inbuf[i].u32[0]));
210
211 netif_info(efx, hw, efx->net_dev, "MCDI RPC REQ:%s\n", buf);
212 }
213#endif
214
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100215 efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100216
217 mcdi->new_epoch = false;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000218}
219
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100220static int efx_mcdi_errno(unsigned int mcdi_err)
221{
222 switch (mcdi_err) {
223 case 0:
224 return 0;
225#define TRANSLATE_ERROR(name) \
226 case MC_CMD_ERR_ ## name: \
227 return -name;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100228 TRANSLATE_ERROR(EPERM);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100229 TRANSLATE_ERROR(ENOENT);
230 TRANSLATE_ERROR(EINTR);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100231 TRANSLATE_ERROR(EAGAIN);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100232 TRANSLATE_ERROR(EACCES);
233 TRANSLATE_ERROR(EBUSY);
234 TRANSLATE_ERROR(EINVAL);
235 TRANSLATE_ERROR(EDEADLK);
236 TRANSLATE_ERROR(ENOSYS);
237 TRANSLATE_ERROR(ETIME);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100238 TRANSLATE_ERROR(EALREADY);
239 TRANSLATE_ERROR(ENOSPC);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100240#undef TRANSLATE_ERROR
Ben Hutchingsea136ae2013-10-08 16:36:58 +0100241 case MC_CMD_ERR_ENOTSUP:
242 return -EOPNOTSUPP;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100243 case MC_CMD_ERR_ALLOC_FAIL:
244 return -ENOBUFS;
245 case MC_CMD_ERR_MAC_EXIST:
246 return -EADDRINUSE;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100247 default:
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100248 return -EPROTO;
249 }
250}
251
252static void efx_mcdi_read_response_header(struct efx_nic *efx)
253{
254 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
255 unsigned int respseq, respcmd, error;
Edward Cree75aba2a2015-05-27 13:13:54 +0100256#ifdef CONFIG_SFC_MCDI_LOGGING
257 char *buf = mcdi->logging_buffer; /* page-sized */
258#endif
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100259 efx_dword_t hdr;
260
261 efx->type->mcdi_read_response(efx, &hdr, 0, 4);
262 respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
263 respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
264 error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
265
266 if (respcmd != MC_CMD_V2_EXTN) {
267 mcdi->resp_hdr_len = 4;
268 mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
269 } else {
270 efx->type->mcdi_read_response(efx, &hdr, 4, 4);
271 mcdi->resp_hdr_len = 8;
272 mcdi->resp_data_len =
273 EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
274 }
275
Edward Cree75aba2a2015-05-27 13:13:54 +0100276#ifdef CONFIG_SFC_MCDI_LOGGING
277 if (!WARN_ON_ONCE(!buf)) {
278 size_t hdr_len, data_len;
279 int bytes = 0;
280 int i;
281
282 WARN_ON_ONCE(mcdi->resp_hdr_len % 4);
283 hdr_len = mcdi->resp_hdr_len / 4;
284 /* MCDI_DECLARE_BUF ensures that underlying buffer is padded
285 * to dword size, and the MCDI buffer is always dword size
286 */
287 data_len = DIV_ROUND_UP(mcdi->resp_data_len, 4);
288
289 /* We own the logging buffer, as only one MCDI can be in
290 * progress on a NIC at any one time. So no need for locking.
291 */
292 for (i = 0; i < hdr_len && bytes < PAGE_SIZE; i++) {
293 efx->type->mcdi_read_response(efx, &hdr, (i * 4), 4);
294 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
295 " %08x", le32_to_cpu(hdr.u32[0]));
296 }
297
298 for (i = 0; i < data_len && bytes < PAGE_SIZE; i++) {
299 efx->type->mcdi_read_response(efx, &hdr,
300 mcdi->resp_hdr_len + (i * 4), 4);
301 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
302 " %08x", le32_to_cpu(hdr.u32[0]));
303 }
304
305 netif_info(efx, hw, efx->net_dev, "MCDI RPC RESP:%s\n", buf);
306 }
307#endif
308
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100309 if (error && mcdi->resp_data_len == 0) {
310 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
311 mcdi->resprc = -EIO;
312 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
313 netif_err(efx, hw, efx->net_dev,
314 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
315 respseq, mcdi->seqno);
316 mcdi->resprc = -EIO;
317 } else if (error) {
318 efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
319 mcdi->resprc =
320 efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
321 } else {
322 mcdi->resprc = 0;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100323 }
324}
325
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100326static bool efx_mcdi_poll_once(struct efx_nic *efx)
327{
328 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
329
330 rmb();
331 if (!efx->type->mcdi_poll_response(efx))
332 return false;
333
334 spin_lock_bh(&mcdi->iface_lock);
335 efx_mcdi_read_response_header(efx);
336 spin_unlock_bh(&mcdi->iface_lock);
337
338 return true;
339}
340
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000341static int efx_mcdi_poll(struct efx_nic *efx)
342{
343 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000344 unsigned long time, finish;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100345 unsigned int spins;
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100346 int rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000347
348 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100349 rc = efx_mcdi_poll_reboot(efx);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100350 if (rc) {
Ben Hutchings369327f2012-10-26 17:53:12 +0100351 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100352 mcdi->resprc = rc;
353 mcdi->resp_hdr_len = 0;
354 mcdi->resp_data_len = 0;
Ben Hutchings369327f2012-10-26 17:53:12 +0100355 spin_unlock_bh(&mcdi->iface_lock);
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100356 return 0;
357 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000358
359 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
360 * because generally mcdi responses are fast. After that, back off
361 * and poll once a jiffy (approximately)
362 */
363 spins = TICK_USEC;
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000364 finish = jiffies + MCDI_RPC_TIMEOUT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000365
366 while (1) {
367 if (spins != 0) {
368 --spins;
369 udelay(1);
Ben Hutchings55029c12010-01-13 04:34:25 +0000370 } else {
371 schedule_timeout_uninterruptible(1);
372 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000373
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000374 time = jiffies;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000375
Robert Stonehouse5731d7b2013-10-09 11:52:43 +0100376 if (efx_mcdi_poll_once(efx))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000377 break;
378
Ben Hutchingsebf98e72012-12-01 02:21:17 +0000379 if (time_after(time, finish))
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000380 return -ETIMEDOUT;
381 }
382
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000383 /* Return rc=0 like wait_event_timeout() */
384 return 0;
385}
386
Ben Hutchings876be082012-10-01 20:58:35 +0100387/* Test and clear MC-rebooted flag for this port/function; reset
388 * software state as necessary.
389 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000390int efx_mcdi_poll_reboot(struct efx_nic *efx)
391{
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100392 if (!efx->mcdi)
393 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000394
Ben Hutchingscd0ecc92012-12-14 21:52:56 +0000395 return efx->type->mcdi_poll_reboot(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000396}
397
Ben Hutchingscade7152013-08-27 23:12:31 +0100398static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
399{
400 return cmpxchg(&mcdi->state,
401 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
402 MCDI_STATE_QUIESCENT;
403}
404
405static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000406{
407 /* Wait until the interface becomes QUIESCENT and we win the race
Ben Hutchingscade7152013-08-27 23:12:31 +0100408 * to mark it RUNNING_SYNC.
409 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000410 wait_event(mcdi->wq,
Ben Hutchings251111d2013-08-27 23:04:29 +0100411 cmpxchg(&mcdi->state,
Ben Hutchingscade7152013-08-27 23:12:31 +0100412 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
Ben Hutchings251111d2013-08-27 23:04:29 +0100413 MCDI_STATE_QUIESCENT);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000414}
415
416static int efx_mcdi_await_completion(struct efx_nic *efx)
417{
418 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
419
Ben Hutchings251111d2013-08-27 23:04:29 +0100420 if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
421 MCDI_RPC_TIMEOUT) == 0)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000422 return -ETIMEDOUT;
423
424 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
425 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
426 * completed the request first, then we'll just end up completing the
427 * request again, which is safe.
428 *
429 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
430 * wait_event_timeout() implicitly provides.
431 */
432 if (mcdi->mode == MCDI_MODE_POLL)
433 return efx_mcdi_poll(efx);
434
435 return 0;
436}
437
Ben Hutchingscade7152013-08-27 23:12:31 +0100438/* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
439 * requester. Return whether this was done. Does not take any locks.
440 */
441static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000442{
Ben Hutchingscade7152013-08-27 23:12:31 +0100443 if (cmpxchg(&mcdi->state,
444 MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
445 MCDI_STATE_RUNNING_SYNC) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000446 wake_up(&mcdi->wq);
447 return true;
448 }
449
450 return false;
451}
452
453static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
454{
Ben Hutchingscade7152013-08-27 23:12:31 +0100455 if (mcdi->mode == MCDI_MODE_EVENTS) {
456 struct efx_mcdi_async_param *async;
457 struct efx_nic *efx = mcdi->efx;
458
459 /* Process the asynchronous request queue */
460 spin_lock_bh(&mcdi->async_lock);
461 async = list_first_entry_or_null(
462 &mcdi->async_list, struct efx_mcdi_async_param, list);
463 if (async) {
464 mcdi->state = MCDI_STATE_RUNNING_ASYNC;
465 efx_mcdi_send_request(efx, async->cmd,
466 (const efx_dword_t *)(async + 1),
467 async->inlen);
468 mod_timer(&mcdi->async_timer,
469 jiffies + MCDI_RPC_TIMEOUT);
470 }
471 spin_unlock_bh(&mcdi->async_lock);
472
473 if (async)
474 return;
475 }
476
Ben Hutchings251111d2013-08-27 23:04:29 +0100477 mcdi->state = MCDI_STATE_QUIESCENT;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000478 wake_up(&mcdi->wq);
479}
480
Ben Hutchingscade7152013-08-27 23:12:31 +0100481/* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
482 * asynchronous completion function, and release the interface.
483 * Return whether this was done. Must be called in bh-disabled
484 * context. Will take iface_lock and async_lock.
485 */
486static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
487{
488 struct efx_nic *efx = mcdi->efx;
489 struct efx_mcdi_async_param *async;
Edward Cree1e0b8122013-05-31 18:36:12 +0100490 size_t hdr_len, data_len, err_len;
Ben Hutchingscade7152013-08-27 23:12:31 +0100491 efx_dword_t *outbuf;
Jon Cooperaa09a3d2015-05-20 11:10:41 +0100492 MCDI_DECLARE_BUF_ERR(errbuf);
Ben Hutchingscade7152013-08-27 23:12:31 +0100493 int rc;
494
495 if (cmpxchg(&mcdi->state,
496 MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
497 MCDI_STATE_RUNNING_ASYNC)
498 return false;
499
500 spin_lock(&mcdi->iface_lock);
501 if (timeout) {
502 /* Ensure that if the completion event arrives later,
503 * the seqno check in efx_mcdi_ev_cpl() will fail
504 */
505 ++mcdi->seqno;
506 ++mcdi->credits;
507 rc = -ETIMEDOUT;
508 hdr_len = 0;
509 data_len = 0;
510 } else {
511 rc = mcdi->resprc;
512 hdr_len = mcdi->resp_hdr_len;
513 data_len = mcdi->resp_data_len;
514 }
515 spin_unlock(&mcdi->iface_lock);
516
517 /* Stop the timer. In case the timer function is running, we
518 * must wait for it to return so that there is no possibility
519 * of it aborting the next request.
520 */
521 if (!timeout)
522 del_timer_sync(&mcdi->async_timer);
523
524 spin_lock(&mcdi->async_lock);
525 async = list_first_entry(&mcdi->async_list,
526 struct efx_mcdi_async_param, list);
527 list_del(&async->list);
528 spin_unlock(&mcdi->async_lock);
529
530 outbuf = (efx_dword_t *)(async + 1);
531 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
532 min(async->outlen, data_len));
Edward Cree1e0b8122013-05-31 18:36:12 +0100533 if (!timeout && rc && !async->quiet) {
534 err_len = min(sizeof(errbuf), data_len);
535 efx->type->mcdi_read_response(efx, errbuf, hdr_len,
536 sizeof(errbuf));
537 efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf,
538 err_len, rc);
539 }
Ben Hutchingscade7152013-08-27 23:12:31 +0100540 async->complete(efx, async->cookie, rc, outbuf, data_len);
541 kfree(async);
542
543 efx_mcdi_release(mcdi);
544
545 return true;
546}
547
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000548static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100549 unsigned int datalen, unsigned int mcdi_err)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000550{
551 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
552 bool wake = false;
553
554 spin_lock(&mcdi->iface_lock);
555
556 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
557 if (mcdi->credits)
558 /* The request has been cancelled */
559 --mcdi->credits;
560 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000561 netif_err(efx, hw, efx->net_dev,
562 "MC response mismatch tx seq 0x%x rx "
563 "seq 0x%x\n", seqno, mcdi->seqno);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000564 } else {
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100565 if (efx->type->mcdi_max_ver >= 2) {
566 /* MCDI v2 responses don't fit in an event */
567 efx_mcdi_read_response_header(efx);
568 } else {
569 mcdi->resprc = efx_mcdi_errno(mcdi_err);
570 mcdi->resp_hdr_len = 4;
571 mcdi->resp_data_len = datalen;
572 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000573
574 wake = true;
575 }
576
577 spin_unlock(&mcdi->iface_lock);
578
Ben Hutchingscade7152013-08-27 23:12:31 +0100579 if (wake) {
580 if (!efx_mcdi_complete_async(mcdi, false))
581 (void) efx_mcdi_complete_sync(mcdi);
582
583 /* If the interface isn't RUNNING_ASYNC or
584 * RUNNING_SYNC then we've received a duplicate
585 * completion after we've already transitioned back to
586 * QUIESCENT. [A subsequent invocation would increment
587 * seqno, so would have failed the seqno check].
588 */
589 }
590}
591
592static void efx_mcdi_timeout_async(unsigned long context)
593{
594 struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
595
596 efx_mcdi_complete_async(mcdi, true);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000597}
598
Ben Hutchings2f4bcdc2013-08-22 22:06:09 +0100599static int
600efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
601{
602 if (efx->type->mcdi_max_ver < 0 ||
603 (efx->type->mcdi_max_ver < 2 &&
604 cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
605 return -EINVAL;
606
607 if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
608 (efx->type->mcdi_max_ver < 2 &&
609 inlen > MCDI_CTL_SDU_LEN_MAX_V1))
610 return -EMSGSIZE;
611
612 return 0;
613}
614
Edward Cree1e0b8122013-05-31 18:36:12 +0100615static int _efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
616 efx_dword_t *outbuf, size_t outlen,
617 size_t *outlen_actual, bool quiet)
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100618{
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000619 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
Jon Cooperaa09a3d2015-05-20 11:10:41 +0100620 MCDI_DECLARE_BUF_ERR(errbuf);
Stuart Hodgsonc3cba722012-07-16 17:40:47 +0100621 int rc;
622
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000623 if (mcdi->mode == MCDI_MODE_POLL)
624 rc = efx_mcdi_poll(efx);
625 else
626 rc = efx_mcdi_await_completion(efx);
627
628 if (rc != 0) {
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100629 netif_err(efx, hw, efx->net_dev,
630 "MC command 0x%x inlen %d mode %d timed out\n",
631 cmd, (int)inlen, mcdi->mode);
632
633 if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
634 netif_err(efx, hw, efx->net_dev,
635 "MCDI request was completed without an event\n");
636 rc = 0;
637 }
638
Edward Creee2835462014-04-16 19:27:48 +0100639 efx_mcdi_abandon(efx);
640
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000641 /* Close the race with efx_mcdi_ev_cpl() executing just too late
642 * and completing a request we've just cancelled, by ensuring
643 * that the seqno check therein fails.
644 */
645 spin_lock_bh(&mcdi->iface_lock);
646 ++mcdi->seqno;
647 ++mcdi->credits;
648 spin_unlock_bh(&mcdi->iface_lock);
Robert Stonehouse6b294b82013-10-09 11:52:48 +0100649 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000650
Edward Cree1e0b8122013-05-31 18:36:12 +0100651 if (rc != 0) {
652 if (outlen_actual)
653 *outlen_actual = 0;
654 } else {
655 size_t hdr_len, data_len, err_len;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000656
657 /* At the very least we need a memory barrier here to ensure
658 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
659 * a spurious efx_mcdi_ev_cpl() running concurrently by
660 * acquiring the iface_lock. */
661 spin_lock_bh(&mcdi->iface_lock);
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100662 rc = mcdi->resprc;
Ben Hutchings369327f2012-10-26 17:53:12 +0100663 hdr_len = mcdi->resp_hdr_len;
664 data_len = mcdi->resp_data_len;
Edward Cree1e0b8122013-05-31 18:36:12 +0100665 err_len = min(sizeof(errbuf), data_len);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000666 spin_unlock_bh(&mcdi->iface_lock);
667
Ben Hutchings5bc283e2012-10-08 21:43:00 +0100668 BUG_ON(rc > 0);
669
Edward Cree1e0b8122013-05-31 18:36:12 +0100670 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
671 min(outlen, data_len));
672 if (outlen_actual)
673 *outlen_actual = data_len;
674
675 efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len);
676
677 if (cmd == MC_CMD_REBOOT && rc == -EIO) {
678 /* Don't reset if MC_CMD_REBOOT returns EIO */
679 } else if (rc == -EIO || rc == -EINTR) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000680 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
681 -rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000682 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
Edward Cree1e0b8122013-05-31 18:36:12 +0100683 } else if (rc && !quiet) {
684 efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len,
685 rc);
686 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000687
688 if (rc == -EIO || rc == -EINTR) {
689 msleep(MCDI_STATUS_SLEEP_MS);
690 efx_mcdi_poll_reboot(efx);
Daniel Pieczkod36a08b2013-06-20 11:40:07 +0100691 mcdi->new_epoch = true;
Ben Hutchings3f713bf2011-12-20 23:39:31 +0000692 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000693 }
694
695 efx_mcdi_release(mcdi);
696 return rc;
697}
698
Edward Cree1e0b8122013-05-31 18:36:12 +0100699static int _efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
700 const efx_dword_t *inbuf, size_t inlen,
701 efx_dword_t *outbuf, size_t outlen,
702 size_t *outlen_actual, bool quiet)
703{
704 int rc;
705
706 rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
707 if (rc) {
708 if (outlen_actual)
709 *outlen_actual = 0;
710 return rc;
711 }
712 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
713 outlen_actual, quiet);
714}
715
716int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
717 const efx_dword_t *inbuf, size_t inlen,
718 efx_dword_t *outbuf, size_t outlen,
719 size_t *outlen_actual)
720{
721 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
722 outlen_actual, false);
723}
724
725/* Normally, on receiving an error code in the MCDI response,
726 * efx_mcdi_rpc will log an error message containing (among other
727 * things) the raw error code, by means of efx_mcdi_display_error.
728 * This _quiet version suppresses that; if the caller wishes to log
729 * the error conditionally on the return code, it should call this
730 * function and is then responsible for calling efx_mcdi_display_error
731 * as needed.
732 */
733int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
734 const efx_dword_t *inbuf, size_t inlen,
735 efx_dword_t *outbuf, size_t outlen,
736 size_t *outlen_actual)
737{
738 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
739 outlen_actual, true);
740}
741
742int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
743 const efx_dword_t *inbuf, size_t inlen)
744{
745 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
746 int rc;
747
748 rc = efx_mcdi_check_supported(efx, cmd, inlen);
749 if (rc)
750 return rc;
751
752 if (efx->mc_bist_for_other_fn)
753 return -ENETDOWN;
754
Edward Creee2835462014-04-16 19:27:48 +0100755 if (mcdi->mode == MCDI_MODE_FAIL)
756 return -ENETDOWN;
757
Edward Cree1e0b8122013-05-31 18:36:12 +0100758 efx_mcdi_acquire_sync(mcdi);
759 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
760 return 0;
761}
762
763static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
764 const efx_dword_t *inbuf, size_t inlen,
765 size_t outlen,
766 efx_mcdi_async_completer *complete,
767 unsigned long cookie, bool quiet)
768{
769 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
770 struct efx_mcdi_async_param *async;
771 int rc;
772
773 rc = efx_mcdi_check_supported(efx, cmd, inlen);
774 if (rc)
775 return rc;
776
777 if (efx->mc_bist_for_other_fn)
778 return -ENETDOWN;
779
780 async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
781 GFP_ATOMIC);
782 if (!async)
783 return -ENOMEM;
784
785 async->cmd = cmd;
786 async->inlen = inlen;
787 async->outlen = outlen;
788 async->quiet = quiet;
789 async->complete = complete;
790 async->cookie = cookie;
791 memcpy(async + 1, inbuf, inlen);
792
793 spin_lock_bh(&mcdi->async_lock);
794
795 if (mcdi->mode == MCDI_MODE_EVENTS) {
796 list_add_tail(&async->list, &mcdi->async_list);
797
798 /* If this is at the front of the queue, try to start it
799 * immediately
800 */
801 if (mcdi->async_list.next == &async->list &&
802 efx_mcdi_acquire_async(mcdi)) {
803 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
804 mod_timer(&mcdi->async_timer,
805 jiffies + MCDI_RPC_TIMEOUT);
806 }
807 } else {
808 kfree(async);
809 rc = -ENETDOWN;
810 }
811
812 spin_unlock_bh(&mcdi->async_lock);
813
814 return rc;
815}
816
817/**
818 * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
819 * @efx: NIC through which to issue the command
820 * @cmd: Command type number
821 * @inbuf: Command parameters
822 * @inlen: Length of command parameters, in bytes
823 * @outlen: Length to allocate for response buffer, in bytes
824 * @complete: Function to be called on completion or cancellation.
825 * @cookie: Arbitrary value to be passed to @complete.
826 *
827 * This function does not sleep and therefore may be called in atomic
828 * context. It will fail if event queues are disabled or if MCDI
829 * event completions have been disabled due to an error.
830 *
831 * If it succeeds, the @complete function will be called exactly once
832 * in atomic context, when one of the following occurs:
833 * (a) the completion event is received (in NAPI context)
834 * (b) event queues are disabled (in the process that disables them)
835 * (c) the request times-out (in timer context)
836 */
837int
838efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
839 const efx_dword_t *inbuf, size_t inlen, size_t outlen,
840 efx_mcdi_async_completer *complete, unsigned long cookie)
841{
842 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
843 cookie, false);
844}
845
846int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
847 const efx_dword_t *inbuf, size_t inlen,
848 size_t outlen, efx_mcdi_async_completer *complete,
849 unsigned long cookie)
850{
851 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
852 cookie, true);
853}
854
855int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
856 efx_dword_t *outbuf, size_t outlen,
857 size_t *outlen_actual)
858{
859 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
860 outlen_actual, false);
861}
862
863int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd, size_t inlen,
864 efx_dword_t *outbuf, size_t outlen,
865 size_t *outlen_actual)
866{
867 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
868 outlen_actual, true);
869}
870
871void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
872 size_t inlen, efx_dword_t *outbuf,
873 size_t outlen, int rc)
874{
875 int code = 0, err_arg = 0;
876
877 if (outlen >= MC_CMD_ERR_CODE_OFST + 4)
878 code = MCDI_DWORD(outbuf, ERR_CODE);
879 if (outlen >= MC_CMD_ERR_ARG_OFST + 4)
880 err_arg = MCDI_DWORD(outbuf, ERR_ARG);
881 netif_err(efx, hw, efx->net_dev,
882 "MC command 0x%x inlen %d failed rc=%d (raw=%d) arg=%d\n",
883 cmd, (int)inlen, rc, code, err_arg);
884}
885
Ben Hutchingscade7152013-08-27 23:12:31 +0100886/* Switch to polled MCDI completions. This can be called in various
887 * error conditions with various locks held, so it must be lockless.
888 * Caller is responsible for flushing asynchronous requests later.
889 */
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000890void efx_mcdi_mode_poll(struct efx_nic *efx)
891{
892 struct efx_mcdi_iface *mcdi;
893
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100894 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000895 return;
896
897 mcdi = efx_mcdi(efx);
Edward Creee2835462014-04-16 19:27:48 +0100898 /* If already in polling mode, nothing to do.
899 * If in fail-fast state, don't switch to polled completion.
900 * FLR recovery will do that later.
901 */
902 if (mcdi->mode == MCDI_MODE_POLL || mcdi->mode == MCDI_MODE_FAIL)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000903 return;
904
905 /* We can switch from event completion to polled completion, because
906 * mcdi requests are always completed in shared memory. We do this by
907 * switching the mode to POLL'd then completing the request.
908 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
909 *
910 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
Ben Hutchingscade7152013-08-27 23:12:31 +0100911 * which efx_mcdi_complete_sync() provides for us.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000912 */
913 mcdi->mode = MCDI_MODE_POLL;
914
Ben Hutchingscade7152013-08-27 23:12:31 +0100915 efx_mcdi_complete_sync(mcdi);
916}
917
918/* Flush any running or queued asynchronous requests, after event processing
919 * is stopped
920 */
921void efx_mcdi_flush_async(struct efx_nic *efx)
922{
923 struct efx_mcdi_async_param *async, *next;
924 struct efx_mcdi_iface *mcdi;
925
926 if (!efx->mcdi)
927 return;
928
929 mcdi = efx_mcdi(efx);
930
Edward Creee2835462014-04-16 19:27:48 +0100931 /* We must be in poll or fail mode so no more requests can be queued */
932 BUG_ON(mcdi->mode == MCDI_MODE_EVENTS);
Ben Hutchingscade7152013-08-27 23:12:31 +0100933
934 del_timer_sync(&mcdi->async_timer);
935
936 /* If a request is still running, make sure we give the MC
937 * time to complete it so that the response won't overwrite our
938 * next request.
939 */
940 if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
941 efx_mcdi_poll(efx);
942 mcdi->state = MCDI_STATE_QUIESCENT;
943 }
944
945 /* Nothing else will access the async list now, so it is safe
946 * to walk it without holding async_lock. If we hold it while
947 * calling a completer then lockdep may warn that we have
948 * acquired locks in the wrong order.
949 */
950 list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
951 async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
952 list_del(&async->list);
953 kfree(async);
954 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000955}
956
957void efx_mcdi_mode_event(struct efx_nic *efx)
958{
959 struct efx_mcdi_iface *mcdi;
960
Ben Hutchingsf3ad5002012-09-18 02:33:56 +0100961 if (!efx->mcdi)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000962 return;
963
964 mcdi = efx_mcdi(efx);
Edward Creee2835462014-04-16 19:27:48 +0100965 /* If already in event completion mode, nothing to do.
966 * If in fail-fast state, don't switch to event completion. FLR
967 * recovery will do that later.
968 */
969 if (mcdi->mode == MCDI_MODE_EVENTS || mcdi->mode == MCDI_MODE_FAIL)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000970 return;
971
972 /* We can't switch from polled to event completion in the middle of a
973 * request, because the completion method is specified in the request.
974 * So acquire the interface to serialise the requestors. We don't need
975 * to acquire the iface_lock to change the mode here, but we do need a
976 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
977 * efx_mcdi_acquire() provides.
978 */
Ben Hutchingscade7152013-08-27 23:12:31 +0100979 efx_mcdi_acquire_sync(mcdi);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +0000980 mcdi->mode = MCDI_MODE_EVENTS;
981 efx_mcdi_release(mcdi);
982}
983
984static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
985{
986 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
987
988 /* If there is an outstanding MCDI request, it has been terminated
989 * either by a BADASSERT or REBOOT event. If the mcdi interface is
990 * in polled mode, then do nothing because the MC reboot handler will
991 * set the header correctly. However, if the mcdi interface is waiting
992 * for a CMDDONE event it won't receive it [and since all MCDI events
993 * are sent to the same queue, we can't be racing with
994 * efx_mcdi_ev_cpl()]
995 *
Ben Hutchingscade7152013-08-27 23:12:31 +0100996 * If there is an outstanding asynchronous request, we can't
997 * complete it now (efx_mcdi_complete() would deadlock). The
998 * reset process will take care of this.
999 *
1000 * There's a race here with efx_mcdi_send_request(), because
1001 * we might receive a REBOOT event *before* the request has
1002 * been copied out. In polled mode (during startup) this is
1003 * irrelevant, because efx_mcdi_complete_sync() is ignored. In
1004 * event mode, this condition is just an edge-case of
1005 * receiving a REBOOT event after posting the MCDI
1006 * request. Did the mc reboot before or after the copyout? The
1007 * best we can do always is just return failure.
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001008 */
1009 spin_lock(&mcdi->iface_lock);
Ben Hutchingscade7152013-08-27 23:12:31 +01001010 if (efx_mcdi_complete_sync(mcdi)) {
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001011 if (mcdi->mode == MCDI_MODE_EVENTS) {
1012 mcdi->resprc = rc;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +01001013 mcdi->resp_hdr_len = 0;
1014 mcdi->resp_data_len = 0;
Steve Hodgson18e3ee22010-12-02 13:46:55 +00001015 ++mcdi->credits;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001016 }
Ben Hutchings3f713bf2011-12-20 23:39:31 +00001017 } else {
1018 int count;
1019
Ben Hutchings3f713bf2011-12-20 23:39:31 +00001020 /* Consume the status word since efx_mcdi_rpc_finish() won't */
1021 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
1022 if (efx_mcdi_poll_reboot(efx))
1023 break;
1024 udelay(MCDI_STATUS_DELAY_US);
1025 }
Daniel Pieczkod36a08b2013-06-20 11:40:07 +01001026 mcdi->new_epoch = true;
Daniel Pieczkodfdaa952013-09-18 10:16:24 +01001027
1028 /* Nobody was waiting for an MCDI request, so trigger a reset */
1029 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
Ben Hutchings3f713bf2011-12-20 23:39:31 +00001030 }
1031
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001032 spin_unlock(&mcdi->iface_lock);
1033}
1034
Jon Cooper74cd60a2013-09-16 14:18:51 +01001035/* The MC is going down in to BIST mode. set the BIST flag to block
1036 * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
1037 * (which doesn't actually execute a reset, it waits for the controlling
1038 * function to reset it).
1039 */
1040static void efx_mcdi_ev_bist(struct efx_nic *efx)
1041{
1042 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1043
1044 spin_lock(&mcdi->iface_lock);
1045 efx->mc_bist_for_other_fn = true;
1046 if (efx_mcdi_complete_sync(mcdi)) {
1047 if (mcdi->mode == MCDI_MODE_EVENTS) {
1048 mcdi->resprc = -EIO;
1049 mcdi->resp_hdr_len = 0;
1050 mcdi->resp_data_len = 0;
1051 ++mcdi->credits;
1052 }
1053 }
1054 mcdi->new_epoch = true;
1055 efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
1056 spin_unlock(&mcdi->iface_lock);
1057}
1058
Edward Creee2835462014-04-16 19:27:48 +01001059/* MCDI timeouts seen, so make all MCDI calls fail-fast and issue an FLR to try
1060 * to recover.
1061 */
1062static void efx_mcdi_abandon(struct efx_nic *efx)
1063{
1064 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1065
1066 if (xchg(&mcdi->mode, MCDI_MODE_FAIL) == MCDI_MODE_FAIL)
1067 return; /* it had already been done */
1068 netif_dbg(efx, hw, efx->net_dev, "MCDI is timing out; trying to recover\n");
1069 efx_schedule_reset(efx, RESET_TYPE_MCDI_TIMEOUT);
1070}
1071
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001072/* Called from falcon_process_eventq for MCDI events */
1073void efx_mcdi_process_event(struct efx_channel *channel,
1074 efx_qword_t *event)
1075{
1076 struct efx_nic *efx = channel->efx;
1077 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
1078 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
1079
1080 switch (code) {
1081 case MCDI_EVENT_CODE_BADSSERT:
Ben Hutchings62776d02010-06-23 11:30:07 +00001082 netif_err(efx, hw, efx->net_dev,
1083 "MC watchdog or assertion failure at 0x%x\n", data);
Ben Hutchings5bc283e2012-10-08 21:43:00 +01001084 efx_mcdi_ev_death(efx, -EINTR);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001085 break;
1086
1087 case MCDI_EVENT_CODE_PMNOTICE:
Ben Hutchings62776d02010-06-23 11:30:07 +00001088 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001089 break;
1090
1091 case MCDI_EVENT_CODE_CMDDONE:
1092 efx_mcdi_ev_cpl(efx,
1093 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
1094 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
1095 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
1096 break;
1097
1098 case MCDI_EVENT_CODE_LINKCHANGE:
1099 efx_mcdi_process_link_change(efx, event);
1100 break;
1101 case MCDI_EVENT_CODE_SENSOREVT:
1102 efx_mcdi_sensor_event(efx, event);
1103 break;
1104 case MCDI_EVENT_CODE_SCHEDERR:
Robert Stonehouse2d9955b2013-10-07 18:44:17 +01001105 netif_dbg(efx, hw, efx->net_dev,
1106 "MC Scheduler alert (0x%x)\n", data);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001107 break;
1108 case MCDI_EVENT_CODE_REBOOT:
Ben Hutchings8127d662013-08-29 19:19:29 +01001109 case MCDI_EVENT_CODE_MC_REBOOT:
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
Ben Hutchings5bc283e2012-10-08 21:43:00 +01001111 efx_mcdi_ev_death(efx, -EIO);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001112 break;
Jon Cooper74cd60a2013-09-16 14:18:51 +01001113 case MCDI_EVENT_CODE_MC_BIST:
1114 netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
1115 efx_mcdi_ev_bist(efx);
1116 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001117 case MCDI_EVENT_CODE_MAC_STATS_DMA:
1118 /* MAC stats are gather lazily. We can ignore this. */
1119 break;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001120 case MCDI_EVENT_CODE_FLR:
Shradha Shah7fa8d542015-05-06 00:55:13 +01001121 if (efx->type->sriov_flr)
1122 efx->type->sriov_flr(efx,
1123 MCDI_EVENT_FIELD(*event, FLR_VF));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001124 break;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001125 case MCDI_EVENT_CODE_PTP_RX:
1126 case MCDI_EVENT_CODE_PTP_FAULT:
1127 case MCDI_EVENT_CODE_PTP_PPS:
1128 efx_ptp_event(efx, event);
1129 break;
Jon Cooperbd9a2652013-11-18 12:54:41 +00001130 case MCDI_EVENT_CODE_PTP_TIME:
1131 efx_time_sync_event(channel, event);
1132 break;
Ben Hutchings8127d662013-08-29 19:19:29 +01001133 case MCDI_EVENT_CODE_TX_FLUSH:
1134 case MCDI_EVENT_CODE_RX_FLUSH:
1135 /* Two flush events will be sent: one to the same event
1136 * queue as completions, and one to event queue 0.
1137 * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
1138 * flag will be set, and we should ignore the event
1139 * because we want to wait for all completions.
1140 */
1141 BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
1142 MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
1143 if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
1144 efx_ef10_handle_drain_event(efx);
1145 break;
Alexandre Rames3de82b92013-06-13 11:36:15 +01001146 case MCDI_EVENT_CODE_TX_ERR:
1147 case MCDI_EVENT_CODE_RX_ERR:
1148 netif_err(efx, hw, efx->net_dev,
1149 "%s DMA error (event: "EFX_QWORD_FMT")\n",
1150 code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
1151 EFX_QWORD_VAL(*event));
1152 efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
1153 break;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001154 default:
Ben Hutchings62776d02010-06-23 11:30:07 +00001155 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
1156 code);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001157 }
1158}
1159
1160/**************************************************************************
1161 *
1162 * Specific request functions
1163 *
1164 **************************************************************************
1165 */
1166
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001167void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001168{
Daniel Pieczko8d9f9dd2015-05-06 00:56:55 +01001169 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001170 size_t outlength;
1171 const __le16 *ver_words;
Ben Hutchings8127d662013-08-29 19:19:29 +01001172 size_t offset;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001173 int rc;
1174
1175 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001176 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
1177 outbuf, sizeof(outbuf), &outlength);
1178 if (rc)
1179 goto fail;
Ben Hutchings05a93202011-12-20 00:44:06 +00001180 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001181 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001182 goto fail;
1183 }
1184
1185 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
Ben Hutchings8127d662013-08-29 19:19:29 +01001186 offset = snprintf(buf, len, "%u.%u.%u.%u",
1187 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
1188 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
1189
1190 /* EF10 may have multiple datapath firmware variants within a
1191 * single version. Report which variants are running.
1192 */
1193 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
Daniel Pieczko8d9f9dd2015-05-06 00:56:55 +01001194 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1195
1196 offset += snprintf(buf + offset, len - offset, " rx%x tx%x",
1197 nic_data->rx_dpcpu_fw_id,
1198 nic_data->tx_dpcpu_fw_id);
Ben Hutchings8127d662013-08-29 19:19:29 +01001199
1200 /* It's theoretically possible for the string to exceed 31
1201 * characters, though in practice the first three version
1202 * components are short enough that this doesn't happen.
1203 */
1204 if (WARN_ON(offset >= len))
1205 buf[0] = 0;
1206 }
1207
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001208 return;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001209
1210fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001211 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingse5f0fd22011-02-24 23:57:47 +00001212 buf[0] = 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001213}
1214
Ben Hutchings4c75b43a2013-08-29 19:04:03 +01001215static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
1216 bool *was_attached)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001217{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001218 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001219 MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001220 size_t outlen;
1221 int rc;
1222
1223 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
1224 driver_operating ? 1 : 0);
1225 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
Ben Hutchingsf2b0bef2013-08-20 20:35:50 +01001226 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001227
Edward Cree267d9d72015-05-06 00:59:18 +01001228 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
1229 outbuf, sizeof(outbuf), &outlen);
1230 /* If we're not the primary PF, trying to ATTACH with a FIRMWARE_ID
1231 * specified will fail with EPERM, and we have to tell the MC we don't
1232 * care what firmware we get.
1233 */
1234 if (rc == -EPERM) {
1235 netif_dbg(efx, probe, efx->net_dev,
1236 "efx_mcdi_drv_attach with fw-variant setting failed EPERM, trying without it\n");
1237 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID,
1238 MC_CMD_FW_DONT_CARE);
1239 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf,
1240 sizeof(inbuf), outbuf, sizeof(outbuf),
1241 &outlen);
1242 }
1243 if (rc) {
1244 efx_mcdi_display_error(efx, MC_CMD_DRV_ATTACH, sizeof(inbuf),
1245 outbuf, outlen, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001246 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01001247 }
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001248 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
1249 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001250 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001251 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001252
Ben Hutchings8349f7f2013-10-16 18:32:34 +01001253 if (driver_operating) {
1254 if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) {
1255 efx->mcdi->fn_flags =
1256 MCDI_DWORD(outbuf,
1257 DRV_ATTACH_EXT_OUT_FUNC_FLAGS);
1258 } else {
1259 /* Synthesise flags for Siena */
1260 efx->mcdi->fn_flags =
1261 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1262 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED |
1263 (efx_port_num(efx) == 0) <<
1264 MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY;
1265 }
1266 }
1267
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001268 /* We currently assume we have control of the external link
1269 * and are completely trusted by firmware. Abort probing
1270 * if that's not true for this function.
1271 */
Ben Hutchingsecb1c9c2013-10-07 20:10:11 +01001272
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001273 if (was_attached != NULL)
1274 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
1275 return 0;
1276
1277fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001278 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001279 return rc;
1280}
1281
1282int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001283 u16 *fw_subtype_list, u32 *capabilities)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001284{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001285 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001286 size_t outlen, i;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001287 int port_num = efx_port_num(efx);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001288 int rc;
1289
1290 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
Edward Creecd84ff42014-03-07 18:27:41 +00001291 /* we need __aligned(2) for ether_addr_copy */
1292 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1);
1293 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001294
1295 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
1296 outbuf, sizeof(outbuf), &outlen);
1297 if (rc)
1298 goto fail;
1299
Ben Hutchings05a93202011-12-20 00:44:06 +00001300 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001301 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001302 goto fail;
1303 }
1304
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001305 if (mac_address)
Edward Creecd84ff42014-03-07 18:27:41 +00001306 ether_addr_copy(mac_address,
1307 port_num ?
1308 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
1309 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0));
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001310 if (fw_subtype_list) {
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001311 for (i = 0;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001312 i < MCDI_VAR_ARRAY_LEN(outlen,
1313 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
1314 i++)
1315 fw_subtype_list[i] = MCDI_ARRAY_WORD(
1316 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
1317 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
1318 fw_subtype_list[i] = 0;
Ben Hutchingsbfeed902012-09-07 00:58:10 +01001319 }
Matthew Slattery6aa9c7f2010-07-14 15:36:19 +01001320 if (capabilities) {
1321 if (port_num)
1322 *capabilities = MCDI_DWORD(outbuf,
1323 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1324 else
1325 *capabilities = MCDI_DWORD(outbuf,
1326 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1327 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001328
1329 return 0;
1330
1331fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001332 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
1333 __func__, rc, (int)outlen);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001334
1335 return rc;
1336}
1337
1338int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
1339{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001340 MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001341 u32 dest = 0;
1342 int rc;
1343
1344 if (uart)
1345 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
1346 if (evq)
1347 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
1348
1349 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
1350 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
1351
1352 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
1353
1354 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
1355 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001356 return rc;
1357}
1358
1359int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
1360{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001361 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001362 size_t outlen;
1363 int rc;
1364
1365 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
1366
1367 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
1368 outbuf, sizeof(outbuf), &outlen);
1369 if (rc)
1370 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001371 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
1372 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001373 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001374 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001375
1376 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
1377 return 0;
1378
1379fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001380 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1381 __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001382 return rc;
1383}
1384
1385int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
1386 size_t *size_out, size_t *erase_size_out,
1387 bool *protected_out)
1388{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001389 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
1390 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001391 size_t outlen;
1392 int rc;
1393
1394 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
1395
1396 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
1397 outbuf, sizeof(outbuf), &outlen);
1398 if (rc)
1399 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001400 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
1401 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001402 goto fail;
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001403 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001404
1405 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
1406 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
1407 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
Ben Hutchings05a93202011-12-20 00:44:06 +00001408 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001409 return 0;
1410
1411fail:
Ben Hutchings62776d02010-06-23 11:30:07 +00001412 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001413 return rc;
1414}
1415
Ben Hutchings2e803402010-02-03 09:31:01 +00001416static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
1417{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001418 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
1419 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
Ben Hutchings2e803402010-02-03 09:31:01 +00001420 int rc;
1421
1422 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
1423
1424 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
1425 outbuf, sizeof(outbuf), NULL);
1426 if (rc)
1427 return rc;
1428
1429 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
1430 case MC_CMD_NVRAM_TEST_PASS:
1431 case MC_CMD_NVRAM_TEST_NOTSUPP:
1432 return 0;
1433 default:
1434 return -EIO;
1435 }
1436}
1437
1438int efx_mcdi_nvram_test_all(struct efx_nic *efx)
1439{
1440 u32 nvram_types;
1441 unsigned int type;
1442 int rc;
1443
1444 rc = efx_mcdi_nvram_types(efx, &nvram_types);
1445 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001446 goto fail1;
Ben Hutchings2e803402010-02-03 09:31:01 +00001447
1448 type = 0;
1449 while (nvram_types != 0) {
1450 if (nvram_types & 1) {
1451 rc = efx_mcdi_nvram_test(efx, type);
1452 if (rc)
Ben Hutchingsb548a982010-04-28 09:28:36 +00001453 goto fail2;
Ben Hutchings2e803402010-02-03 09:31:01 +00001454 }
1455 type++;
1456 nvram_types >>= 1;
1457 }
1458
1459 return 0;
Ben Hutchingsb548a982010-04-28 09:28:36 +00001460
1461fail2:
Ben Hutchings62776d02010-06-23 11:30:07 +00001462 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
1463 __func__, type);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001464fail1:
Ben Hutchings62776d02010-06-23 11:30:07 +00001465 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsb548a982010-04-28 09:28:36 +00001466 return rc;
Ben Hutchings2e803402010-02-03 09:31:01 +00001467}
1468
Edward Cree267d9d72015-05-06 00:59:18 +01001469/* Returns 1 if an assertion was read, 0 if no assertion had fired,
1470 * negative on error.
1471 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001472static int efx_mcdi_read_assertion(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001473{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001474 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
Jon Cooperaa09a3d2015-05-20 11:10:41 +01001475 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001476 unsigned int flags, index;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001477 const char *reason;
1478 size_t outlen;
1479 int retry;
1480 int rc;
1481
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001482 /* Attempt to read any stored assertion state before we reboot
1483 * the mcfw out of the assertion handler. Retry twice, once
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001484 * because a boot-time assertion might cause this command to fail
1485 * with EINTR. And once again because GET_ASSERTS can race with
1486 * MC_CMD_REBOOT running on the other port. */
1487 retry = 2;
1488 do {
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001489 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
Edward Cree1e0b8122013-05-31 18:36:12 +01001490 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS,
1491 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1492 outbuf, sizeof(outbuf), &outlen);
Edward Cree267d9d72015-05-06 00:59:18 +01001493 if (rc == -EPERM)
1494 return 0;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001495 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1496
Edward Cree1e0b8122013-05-31 18:36:12 +01001497 if (rc) {
1498 efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS,
1499 MC_CMD_GET_ASSERTS_IN_LEN, outbuf,
1500 outlen, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001501 return rc;
Edward Cree1e0b8122013-05-31 18:36:12 +01001502 }
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001503 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001504 return -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001505
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001506 /* Print out any recorded assertion state */
1507 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001508 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1509 return 0;
1510
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001511 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1512 ? "system-level assertion"
1513 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1514 ? "thread-level assertion"
1515 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1516 ? "watchdog reset"
1517 : "unknown assertion";
Ben Hutchings62776d02010-06-23 11:30:07 +00001518 netif_err(efx, hw, efx->net_dev,
1519 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1520 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1521 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001522
1523 /* Print out the registers */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001524 for (index = 0;
1525 index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1526 index++)
1527 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
1528 1 + index,
1529 MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
1530 index));
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001531
Edward Cree267d9d72015-05-06 00:59:18 +01001532 return 1;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001533}
1534
Edward Cree267d9d72015-05-06 00:59:18 +01001535static int efx_mcdi_exit_assertion(struct efx_nic *efx)
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001536{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001537 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Edward Cree267d9d72015-05-06 00:59:18 +01001538 int rc;
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001539
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001540 /* If the MC is running debug firmware, it might now be
1541 * waiting for a debugger to attach, but we just want it to
1542 * reboot. We set a flag that makes the command a no-op if it
Edward Cree267d9d72015-05-06 00:59:18 +01001543 * has already done so.
1544 * The MCDI will thus return either 0 or -EIO.
Ben Hutchings0f1e54a2012-07-02 23:37:40 +01001545 */
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001546 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1547 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1548 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
Edward Cree267d9d72015-05-06 00:59:18 +01001549 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1550 NULL, 0, NULL);
1551 if (rc == -EIO)
1552 rc = 0;
1553 if (rc)
1554 efx_mcdi_display_error(efx, MC_CMD_REBOOT, MC_CMD_REBOOT_IN_LEN,
1555 NULL, 0, rc);
1556 return rc;
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001557}
1558
1559int efx_mcdi_handle_assertion(struct efx_nic *efx)
1560{
1561 int rc;
1562
1563 rc = efx_mcdi_read_assertion(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001564 if (rc <= 0)
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001565 return rc;
1566
Edward Cree267d9d72015-05-06 00:59:18 +01001567 return efx_mcdi_exit_assertion(efx);
Steve Hodgson8b2103a2010-02-03 09:30:17 +00001568}
1569
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001570void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1571{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001572 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001573 int rc;
1574
1575 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1576 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1577 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1578
1579 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1580
1581 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1582
1583 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1584 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001585}
1586
Jon Cooper3e336262014-01-17 19:48:06 +00001587static int efx_mcdi_reset_func(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001588{
Jon Cooper3e336262014-01-17 19:48:06 +00001589 MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN);
1590 int rc;
1591
1592 BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0);
1593 MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG,
1594 ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1595 rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf),
1596 NULL, 0, NULL);
1597 return rc;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001598}
1599
Ben Hutchings6bff8612012-09-18 02:33:52 +01001600static int efx_mcdi_reset_mc(struct efx_nic *efx)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001601{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001602 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001603 int rc;
1604
1605 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1606 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1607 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1608 NULL, 0, NULL);
1609 /* White is black, and up is down */
1610 if (rc == -EIO)
1611 return 0;
1612 if (rc == 0)
1613 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001614 return rc;
1615}
1616
Ben Hutchings6bff8612012-09-18 02:33:52 +01001617enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1618{
1619 return RESET_TYPE_RECOVER_OR_ALL;
1620}
1621
1622int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1623{
1624 int rc;
1625
Edward Creee2835462014-04-16 19:27:48 +01001626 /* If MCDI is down, we can't handle_assertion */
1627 if (method == RESET_TYPE_MCDI_TIMEOUT) {
1628 rc = pci_reset_function(efx->pci_dev);
1629 if (rc)
1630 return rc;
1631 /* Re-enable polled MCDI completion */
1632 if (efx->mcdi) {
1633 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1634 mcdi->mode = MCDI_MODE_POLL;
1635 }
1636 return 0;
1637 }
1638
Ben Hutchings6bff8612012-09-18 02:33:52 +01001639 /* Recover from a failed assertion pre-reset */
1640 rc = efx_mcdi_handle_assertion(efx);
1641 if (rc)
1642 return rc;
1643
Jon Cooper087e9022015-05-20 11:11:35 +01001644 if (method == RESET_TYPE_DATAPATH)
1645 return 0;
1646 else if (method == RESET_TYPE_WORLD)
Ben Hutchings6bff8612012-09-18 02:33:52 +01001647 return efx_mcdi_reset_mc(efx);
1648 else
Jon Cooper3e336262014-01-17 19:48:06 +00001649 return efx_mcdi_reset_func(efx);
Ben Hutchings6bff8612012-09-18 02:33:52 +01001650}
1651
stephen hemmingerd2156972010-10-18 05:27:31 +00001652static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1653 const u8 *mac, int *id_out)
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001654{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001655 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1656 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001657 size_t outlen;
1658 int rc;
1659
1660 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1661 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1662 MC_CMD_FILTER_MODE_SIMPLE);
Edward Creecd84ff42014-03-07 18:27:41 +00001663 ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001664
1665 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1666 outbuf, sizeof(outbuf), &outlen);
1667 if (rc)
1668 goto fail;
1669
1670 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001671 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001672 goto fail;
1673 }
1674
1675 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1676
1677 return 0;
1678
1679fail:
1680 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001681 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001682 return rc;
1683
1684}
1685
1686
1687int
1688efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1689{
1690 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1691}
1692
1693
1694int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1695{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001696 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001697 size_t outlen;
1698 int rc;
1699
1700 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1701 outbuf, sizeof(outbuf), &outlen);
1702 if (rc)
1703 goto fail;
1704
1705 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
Ben Hutchings00bbb4a2010-04-28 09:27:14 +00001706 rc = -EIO;
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001707 goto fail;
1708 }
1709
1710 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1711
1712 return 0;
1713
1714fail:
1715 *id_out = -1;
Ben Hutchings62776d02010-06-23 11:30:07 +00001716 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001717 return rc;
1718}
1719
1720
1721int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1722{
Ben Hutchings59cfc472012-09-14 17:30:10 +01001723 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001724 int rc;
1725
1726 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1727
1728 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1729 NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001730 return rc;
1731}
1732
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001733int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1734{
1735 struct efx_channel *channel;
1736 struct efx_rx_queue *rx_queue;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001737 MCDI_DECLARE_BUF(inbuf,
1738 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001739 int rc, count;
1740
Ben Hutchings45078372012-09-19 02:53:34 +01001741 BUILD_BUG_ON(EFX_MAX_CHANNELS >
1742 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1743
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001744 count = 0;
1745 efx_for_each_channel(channel, efx) {
1746 efx_for_each_channel_rx_queue(rx_queue, channel) {
1747 if (rx_queue->flush_pending) {
1748 rx_queue->flush_pending = false;
1749 atomic_dec(&efx->rxq_flush_pending);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001750 MCDI_SET_ARRAY_DWORD(
1751 inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1752 count, efx_rx_queue_index(rx_queue));
1753 count++;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001754 }
1755 }
1756 }
1757
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +01001758 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1759 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
Ben Hutchingsbbec9692012-09-11 18:25:13 +01001760 WARN_ON(rc < 0);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001761
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001762 return rc;
1763}
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001764
1765int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1766{
1767 int rc;
1768
1769 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
Ben Hutchingsafd4aea2009-11-29 15:15:25 +00001770 return rc;
1771}
1772
Ben Hutchings8127d662013-08-29 19:19:29 +01001773int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled)
1774{
1775 MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
1776
1777 BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
1778 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
1779 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
1780 return efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
1781 NULL, 0, NULL);
1782}
1783
Edward Cree267d9d72015-05-06 00:59:18 +01001784int efx_mcdi_get_workarounds(struct efx_nic *efx, unsigned int *impl_out,
1785 unsigned int *enabled_out)
1786{
Jon Cooperaa09a3d2015-05-20 11:10:41 +01001787 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_WORKAROUNDS_OUT_LEN);
Edward Cree267d9d72015-05-06 00:59:18 +01001788 size_t outlen;
1789 int rc;
1790
1791 rc = efx_mcdi_rpc(efx, MC_CMD_GET_WORKAROUNDS, NULL, 0,
1792 outbuf, sizeof(outbuf), &outlen);
1793 if (rc)
1794 goto fail;
1795
1796 if (outlen < MC_CMD_GET_WORKAROUNDS_OUT_LEN) {
1797 rc = -EIO;
1798 goto fail;
1799 }
1800
1801 if (impl_out)
1802 *impl_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_IMPLEMENTED);
1803
1804 if (enabled_out)
1805 *enabled_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_ENABLED);
1806
1807 return 0;
1808
1809fail:
1810 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1811 return rc;
1812}
1813
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001814#ifdef CONFIG_SFC_MTD
1815
1816#define EFX_MCDI_NVRAM_LEN_MAX 128
1817
1818static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
1819{
1820 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
1821 int rc;
1822
1823 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
1824
1825 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
1826
1827 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
1828 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001829 return rc;
1830}
1831
1832static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
1833 loff_t offset, u8 *buffer, size_t length)
1834{
1835 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
1836 MCDI_DECLARE_BUF(outbuf,
1837 MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1838 size_t outlen;
1839 int rc;
1840
1841 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
1842 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
1843 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
1844
1845 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
1846 outbuf, sizeof(outbuf), &outlen);
1847 if (rc)
Edward Cree1e0b8122013-05-31 18:36:12 +01001848 return rc;
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001849
1850 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
1851 return 0;
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001852}
1853
1854static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
1855 loff_t offset, const u8 *buffer, size_t length)
1856{
1857 MCDI_DECLARE_BUF(inbuf,
1858 MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1859 int rc;
1860
1861 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
1862 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
1863 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
1864 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
1865
1866 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
1867
1868 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
1869 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
1870 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001871 return rc;
1872}
1873
1874static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
1875 loff_t offset, size_t length)
1876{
1877 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
1878 int rc;
1879
1880 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
1881 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
1882 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
1883
1884 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
1885
1886 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
1887 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001888 return rc;
1889}
1890
1891static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
1892{
1893 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
1894 int rc;
1895
1896 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
1897
1898 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
1899
1900 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
1901 NULL, 0, NULL);
Ben Hutchings45a3fd52012-11-28 04:38:14 +00001902 return rc;
1903}
1904
1905int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
1906 size_t len, size_t *retlen, u8 *buffer)
1907{
1908 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1909 struct efx_nic *efx = mtd->priv;
1910 loff_t offset = start;
1911 loff_t end = min_t(loff_t, start + len, mtd->size);
1912 size_t chunk;
1913 int rc = 0;
1914
1915 while (offset < end) {
1916 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1917 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
1918 buffer, chunk);
1919 if (rc)
1920 goto out;
1921 offset += chunk;
1922 buffer += chunk;
1923 }
1924out:
1925 *retlen = offset - start;
1926 return rc;
1927}
1928
1929int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
1930{
1931 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1932 struct efx_nic *efx = mtd->priv;
1933 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
1934 loff_t end = min_t(loff_t, start + len, mtd->size);
1935 size_t chunk = part->common.mtd.erasesize;
1936 int rc = 0;
1937
1938 if (!part->updating) {
1939 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1940 if (rc)
1941 goto out;
1942 part->updating = true;
1943 }
1944
1945 /* The MCDI interface can in fact do multiple erase blocks at once;
1946 * but erasing may be slow, so we make multiple calls here to avoid
1947 * tripping the MCDI RPC timeout. */
1948 while (offset < end) {
1949 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
1950 chunk);
1951 if (rc)
1952 goto out;
1953 offset += chunk;
1954 }
1955out:
1956 return rc;
1957}
1958
1959int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
1960 size_t len, size_t *retlen, const u8 *buffer)
1961{
1962 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1963 struct efx_nic *efx = mtd->priv;
1964 loff_t offset = start;
1965 loff_t end = min_t(loff_t, start + len, mtd->size);
1966 size_t chunk;
1967 int rc = 0;
1968
1969 if (!part->updating) {
1970 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1971 if (rc)
1972 goto out;
1973 part->updating = true;
1974 }
1975
1976 while (offset < end) {
1977 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1978 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
1979 buffer, chunk);
1980 if (rc)
1981 goto out;
1982 offset += chunk;
1983 buffer += chunk;
1984 }
1985out:
1986 *retlen = offset - start;
1987 return rc;
1988}
1989
1990int efx_mcdi_mtd_sync(struct mtd_info *mtd)
1991{
1992 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1993 struct efx_nic *efx = mtd->priv;
1994 int rc = 0;
1995
1996 if (part->updating) {
1997 part->updating = false;
1998 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
1999 }
2000
2001 return rc;
2002}
2003
2004void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
2005{
2006 struct efx_mcdi_mtd_partition *mcdi_part =
2007 container_of(part, struct efx_mcdi_mtd_partition, common);
2008 struct efx_nic *efx = part->mtd.priv;
2009
2010 snprintf(part->name, sizeof(part->name), "%s %s:%02x",
2011 efx->name, part->type_name, mcdi_part->fw_subtype);
2012}
2013
2014#endif /* CONFIG_SFC_MTD */