1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2008-2013 Solarflare Communications Inc.
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.
10 #include <linux/delay.h>
11 #include <linux/moduleparam.h>
12 #include <asm/cmpxchg.h>
13 #include "net_driver.h"
16 #include "farch_regs.h"
17 #include "mcdi_pcol.h"
20 /**************************************************************************
22 * Management-Controller-to-Driver Interface
24 **************************************************************************
27 #define MCDI_RPC_TIMEOUT (10 * HZ)
29 /* A reboot/assertion causes the MCDI status word to be set after the
30 * command word is set or a REBOOT event is sent. If we notice a reboot
31 * via these mechanisms then wait 250ms for the status word to be set.
33 #define MCDI_STATUS_DELAY_US 100
34 #define MCDI_STATUS_DELAY_COUNT 2500
35 #define MCDI_STATUS_SLEEP_MS \
36 (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
39 EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
41 struct efx_mcdi_async_param {
42 struct list_head list;
47 efx_mcdi_async_completer *complete;
49 /* followed by request/response buffer */
52 static void efx_mcdi_timeout_async(unsigned long context);
53 static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
54 bool *was_attached_out);
55 static bool efx_mcdi_poll_once(struct efx_nic *efx);
56 static void efx_mcdi_abandon(struct efx_nic *efx);
58 #ifdef CONFIG_SFC_MCDI_LOGGING
59 static bool mcdi_logging_default;
60 module_param(mcdi_logging_default, bool, 0644);
61 MODULE_PARM_DESC(mcdi_logging_default,
62 "Enable MCDI logging on newly-probed functions");
65 int efx_mcdi_init(struct efx_nic *efx)
67 struct efx_mcdi_iface *mcdi;
68 bool already_attached;
71 efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
77 #ifdef CONFIG_SFC_MCDI_LOGGING
78 /* consuming code assumes buffer is page-sized */
79 mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
80 if (!mcdi->logging_buffer)
82 mcdi->logging_enabled = mcdi_logging_default;
84 init_waitqueue_head(&mcdi->wq);
85 spin_lock_init(&mcdi->iface_lock);
86 mcdi->state = MCDI_STATE_QUIESCENT;
87 mcdi->mode = MCDI_MODE_POLL;
88 spin_lock_init(&mcdi->async_lock);
89 INIT_LIST_HEAD(&mcdi->async_list);
90 setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
93 (void) efx_mcdi_poll_reboot(efx);
94 mcdi->new_epoch = true;
96 /* Recover from a failed assertion before probing */
97 rc = efx_mcdi_handle_assertion(efx);
101 /* Let the MC (and BMC, if this is a LOM) know that the driver
102 * is loaded. We should do this before we reset the NIC.
104 rc = efx_mcdi_drv_attach(efx, true, &already_attached);
106 netif_err(efx, probe, efx->net_dev,
107 "Unable to register driver with MCPU\n");
110 if (already_attached)
111 /* Not a fatal error */
112 netif_err(efx, probe, efx->net_dev,
113 "Host already registered with MCPU\n");
115 if (efx->mcdi->fn_flags &
116 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
121 #ifdef CONFIG_SFC_MCDI_LOGGING
122 free_page((unsigned long)mcdi->logging_buffer);
131 void efx_mcdi_fini(struct efx_nic *efx)
136 BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
138 /* Relinquish the device (back to the BMC, if this is a LOM) */
139 efx_mcdi_drv_attach(efx, false, NULL);
141 #ifdef CONFIG_SFC_MCDI_LOGGING
142 free_page((unsigned long)efx->mcdi->iface.logging_buffer);
148 static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
149 const efx_dword_t *inbuf, size_t inlen)
151 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
152 #ifdef CONFIG_SFC_MCDI_LOGGING
153 char *buf = mcdi->logging_buffer; /* page-sized */
159 BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
161 /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
162 spin_lock_bh(&mcdi->iface_lock);
164 spin_unlock_bh(&mcdi->iface_lock);
166 seqno = mcdi->seqno & SEQ_MASK;
168 if (mcdi->mode == MCDI_MODE_EVENTS)
169 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
171 if (efx->type->mcdi_max_ver == 1) {
173 EFX_POPULATE_DWORD_7(hdr[0],
174 MCDI_HEADER_RESPONSE, 0,
175 MCDI_HEADER_RESYNC, 1,
176 MCDI_HEADER_CODE, cmd,
177 MCDI_HEADER_DATALEN, inlen,
178 MCDI_HEADER_SEQ, seqno,
179 MCDI_HEADER_XFLAGS, xflags,
180 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
184 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
185 EFX_POPULATE_DWORD_7(hdr[0],
186 MCDI_HEADER_RESPONSE, 0,
187 MCDI_HEADER_RESYNC, 1,
188 MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
189 MCDI_HEADER_DATALEN, 0,
190 MCDI_HEADER_SEQ, seqno,
191 MCDI_HEADER_XFLAGS, xflags,
192 MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
193 EFX_POPULATE_DWORD_2(hdr[1],
194 MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
195 MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
199 #ifdef CONFIG_SFC_MCDI_LOGGING
200 if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) {
203 /* Lengths should always be a whole number of dwords, so scream
206 WARN_ON_ONCE(hdr_len % 4);
207 WARN_ON_ONCE(inlen % 4);
209 /* We own the logging buffer, as only one MCDI can be in
210 * progress on a NIC at any one time. So no need for locking.
212 for (i = 0; i < hdr_len / 4 && bytes < PAGE_SIZE; i++)
213 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
214 " %08x", le32_to_cpu(hdr[i].u32[0]));
216 for (i = 0; i < inlen / 4 && bytes < PAGE_SIZE; i++)
217 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
218 " %08x", le32_to_cpu(inbuf[i].u32[0]));
220 netif_info(efx, hw, efx->net_dev, "MCDI RPC REQ:%s\n", buf);
224 efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
226 mcdi->new_epoch = false;
229 static int efx_mcdi_errno(unsigned int mcdi_err)
234 #define TRANSLATE_ERROR(name) \
235 case MC_CMD_ERR_ ## name: \
237 TRANSLATE_ERROR(EPERM);
238 TRANSLATE_ERROR(ENOENT);
239 TRANSLATE_ERROR(EINTR);
240 TRANSLATE_ERROR(EAGAIN);
241 TRANSLATE_ERROR(EACCES);
242 TRANSLATE_ERROR(EBUSY);
243 TRANSLATE_ERROR(EINVAL);
244 TRANSLATE_ERROR(EDEADLK);
245 TRANSLATE_ERROR(ENOSYS);
246 TRANSLATE_ERROR(ETIME);
247 TRANSLATE_ERROR(EALREADY);
248 TRANSLATE_ERROR(ENOSPC);
249 #undef TRANSLATE_ERROR
250 case MC_CMD_ERR_ENOTSUP:
252 case MC_CMD_ERR_ALLOC_FAIL:
254 case MC_CMD_ERR_MAC_EXIST:
261 static void efx_mcdi_read_response_header(struct efx_nic *efx)
263 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
264 unsigned int respseq, respcmd, error;
265 #ifdef CONFIG_SFC_MCDI_LOGGING
266 char *buf = mcdi->logging_buffer; /* page-sized */
270 efx->type->mcdi_read_response(efx, &hdr, 0, 4);
271 respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
272 respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
273 error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
275 if (respcmd != MC_CMD_V2_EXTN) {
276 mcdi->resp_hdr_len = 4;
277 mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
279 efx->type->mcdi_read_response(efx, &hdr, 4, 4);
280 mcdi->resp_hdr_len = 8;
281 mcdi->resp_data_len =
282 EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
285 #ifdef CONFIG_SFC_MCDI_LOGGING
286 if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) {
287 size_t hdr_len, data_len;
291 WARN_ON_ONCE(mcdi->resp_hdr_len % 4);
292 hdr_len = mcdi->resp_hdr_len / 4;
293 /* MCDI_DECLARE_BUF ensures that underlying buffer is padded
294 * to dword size, and the MCDI buffer is always dword size
296 data_len = DIV_ROUND_UP(mcdi->resp_data_len, 4);
298 /* We own the logging buffer, as only one MCDI can be in
299 * progress on a NIC at any one time. So no need for locking.
301 for (i = 0; i < hdr_len && bytes < PAGE_SIZE; i++) {
302 efx->type->mcdi_read_response(efx, &hdr, (i * 4), 4);
303 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
304 " %08x", le32_to_cpu(hdr.u32[0]));
307 for (i = 0; i < data_len && bytes < PAGE_SIZE; i++) {
308 efx->type->mcdi_read_response(efx, &hdr,
309 mcdi->resp_hdr_len + (i * 4), 4);
310 bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
311 " %08x", le32_to_cpu(hdr.u32[0]));
314 netif_info(efx, hw, efx->net_dev, "MCDI RPC RESP:%s\n", buf);
318 if (error && mcdi->resp_data_len == 0) {
319 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
321 } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
322 netif_err(efx, hw, efx->net_dev,
323 "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
324 respseq, mcdi->seqno);
327 efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
329 efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
335 static bool efx_mcdi_poll_once(struct efx_nic *efx)
337 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
340 if (!efx->type->mcdi_poll_response(efx))
343 spin_lock_bh(&mcdi->iface_lock);
344 efx_mcdi_read_response_header(efx);
345 spin_unlock_bh(&mcdi->iface_lock);
350 static int efx_mcdi_poll(struct efx_nic *efx)
352 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
353 unsigned long time, finish;
357 /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
358 rc = efx_mcdi_poll_reboot(efx);
360 spin_lock_bh(&mcdi->iface_lock);
362 mcdi->resp_hdr_len = 0;
363 mcdi->resp_data_len = 0;
364 spin_unlock_bh(&mcdi->iface_lock);
368 /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
369 * because generally mcdi responses are fast. After that, back off
370 * and poll once a jiffy (approximately)
373 finish = jiffies + MCDI_RPC_TIMEOUT;
380 schedule_timeout_uninterruptible(1);
385 if (efx_mcdi_poll_once(efx))
388 if (time_after(time, finish))
392 /* Return rc=0 like wait_event_timeout() */
396 /* Test and clear MC-rebooted flag for this port/function; reset
397 * software state as necessary.
399 int efx_mcdi_poll_reboot(struct efx_nic *efx)
404 return efx->type->mcdi_poll_reboot(efx);
407 static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
409 return cmpxchg(&mcdi->state,
410 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
411 MCDI_STATE_QUIESCENT;
414 static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
416 /* Wait until the interface becomes QUIESCENT and we win the race
417 * to mark it RUNNING_SYNC.
420 cmpxchg(&mcdi->state,
421 MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
422 MCDI_STATE_QUIESCENT);
425 static int efx_mcdi_await_completion(struct efx_nic *efx)
427 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
429 if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
430 MCDI_RPC_TIMEOUT) == 0)
433 /* Check if efx_mcdi_set_mode() switched us back to polled completions.
434 * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
435 * completed the request first, then we'll just end up completing the
436 * request again, which is safe.
438 * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
439 * wait_event_timeout() implicitly provides.
441 if (mcdi->mode == MCDI_MODE_POLL)
442 return efx_mcdi_poll(efx);
447 /* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
448 * requester. Return whether this was done. Does not take any locks.
450 static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
452 if (cmpxchg(&mcdi->state,
453 MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
454 MCDI_STATE_RUNNING_SYNC) {
462 static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
464 if (mcdi->mode == MCDI_MODE_EVENTS) {
465 struct efx_mcdi_async_param *async;
466 struct efx_nic *efx = mcdi->efx;
468 /* Process the asynchronous request queue */
469 spin_lock_bh(&mcdi->async_lock);
470 async = list_first_entry_or_null(
471 &mcdi->async_list, struct efx_mcdi_async_param, list);
473 mcdi->state = MCDI_STATE_RUNNING_ASYNC;
474 efx_mcdi_send_request(efx, async->cmd,
475 (const efx_dword_t *)(async + 1),
477 mod_timer(&mcdi->async_timer,
478 jiffies + MCDI_RPC_TIMEOUT);
480 spin_unlock_bh(&mcdi->async_lock);
486 mcdi->state = MCDI_STATE_QUIESCENT;
490 /* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
491 * asynchronous completion function, and release the interface.
492 * Return whether this was done. Must be called in bh-disabled
493 * context. Will take iface_lock and async_lock.
495 static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
497 struct efx_nic *efx = mcdi->efx;
498 struct efx_mcdi_async_param *async;
499 size_t hdr_len, data_len, err_len;
501 MCDI_DECLARE_BUF_ERR(errbuf);
504 if (cmpxchg(&mcdi->state,
505 MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
506 MCDI_STATE_RUNNING_ASYNC)
509 spin_lock(&mcdi->iface_lock);
511 /* Ensure that if the completion event arrives later,
512 * the seqno check in efx_mcdi_ev_cpl() will fail
521 hdr_len = mcdi->resp_hdr_len;
522 data_len = mcdi->resp_data_len;
524 spin_unlock(&mcdi->iface_lock);
526 /* Stop the timer. In case the timer function is running, we
527 * must wait for it to return so that there is no possibility
528 * of it aborting the next request.
531 del_timer_sync(&mcdi->async_timer);
533 spin_lock(&mcdi->async_lock);
534 async = list_first_entry(&mcdi->async_list,
535 struct efx_mcdi_async_param, list);
536 list_del(&async->list);
537 spin_unlock(&mcdi->async_lock);
539 outbuf = (efx_dword_t *)(async + 1);
540 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
541 min(async->outlen, data_len));
542 if (!timeout && rc && !async->quiet) {
543 err_len = min(sizeof(errbuf), data_len);
544 efx->type->mcdi_read_response(efx, errbuf, hdr_len,
546 efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf,
549 async->complete(efx, async->cookie, rc, outbuf, data_len);
552 efx_mcdi_release(mcdi);
557 static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
558 unsigned int datalen, unsigned int mcdi_err)
560 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
563 spin_lock(&mcdi->iface_lock);
565 if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
567 /* The request has been cancelled */
570 netif_err(efx, hw, efx->net_dev,
571 "MC response mismatch tx seq 0x%x rx "
572 "seq 0x%x\n", seqno, mcdi->seqno);
574 if (efx->type->mcdi_max_ver >= 2) {
575 /* MCDI v2 responses don't fit in an event */
576 efx_mcdi_read_response_header(efx);
578 mcdi->resprc = efx_mcdi_errno(mcdi_err);
579 mcdi->resp_hdr_len = 4;
580 mcdi->resp_data_len = datalen;
586 spin_unlock(&mcdi->iface_lock);
589 if (!efx_mcdi_complete_async(mcdi, false))
590 (void) efx_mcdi_complete_sync(mcdi);
592 /* If the interface isn't RUNNING_ASYNC or
593 * RUNNING_SYNC then we've received a duplicate
594 * completion after we've already transitioned back to
595 * QUIESCENT. [A subsequent invocation would increment
596 * seqno, so would have failed the seqno check].
601 static void efx_mcdi_timeout_async(unsigned long context)
603 struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
605 efx_mcdi_complete_async(mcdi, true);
609 efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
611 if (efx->type->mcdi_max_ver < 0 ||
612 (efx->type->mcdi_max_ver < 2 &&
613 cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
616 if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
617 (efx->type->mcdi_max_ver < 2 &&
618 inlen > MCDI_CTL_SDU_LEN_MAX_V1))
624 static int _efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
625 efx_dword_t *outbuf, size_t outlen,
626 size_t *outlen_actual, bool quiet)
628 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
629 MCDI_DECLARE_BUF_ERR(errbuf);
632 if (mcdi->mode == MCDI_MODE_POLL)
633 rc = efx_mcdi_poll(efx);
635 rc = efx_mcdi_await_completion(efx);
638 netif_err(efx, hw, efx->net_dev,
639 "MC command 0x%x inlen %d mode %d timed out\n",
640 cmd, (int)inlen, mcdi->mode);
642 if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
643 netif_err(efx, hw, efx->net_dev,
644 "MCDI request was completed without an event\n");
648 efx_mcdi_abandon(efx);
650 /* Close the race with efx_mcdi_ev_cpl() executing just too late
651 * and completing a request we've just cancelled, by ensuring
652 * that the seqno check therein fails.
654 spin_lock_bh(&mcdi->iface_lock);
657 spin_unlock_bh(&mcdi->iface_lock);
664 size_t hdr_len, data_len, err_len;
666 /* At the very least we need a memory barrier here to ensure
667 * we pick up changes from efx_mcdi_ev_cpl(). Protect against
668 * a spurious efx_mcdi_ev_cpl() running concurrently by
669 * acquiring the iface_lock. */
670 spin_lock_bh(&mcdi->iface_lock);
672 hdr_len = mcdi->resp_hdr_len;
673 data_len = mcdi->resp_data_len;
674 err_len = min(sizeof(errbuf), data_len);
675 spin_unlock_bh(&mcdi->iface_lock);
679 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
680 min(outlen, data_len));
682 *outlen_actual = data_len;
684 efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len);
686 if (cmd == MC_CMD_REBOOT && rc == -EIO) {
687 /* Don't reset if MC_CMD_REBOOT returns EIO */
688 } else if (rc == -EIO || rc == -EINTR) {
689 netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
691 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
692 } else if (rc && !quiet) {
693 efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len,
697 if (rc == -EIO || rc == -EINTR) {
698 msleep(MCDI_STATUS_SLEEP_MS);
699 efx_mcdi_poll_reboot(efx);
700 mcdi->new_epoch = true;
704 efx_mcdi_release(mcdi);
708 static int _efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
709 const efx_dword_t *inbuf, size_t inlen,
710 efx_dword_t *outbuf, size_t outlen,
711 size_t *outlen_actual, bool quiet)
715 rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
721 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
722 outlen_actual, quiet);
725 int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
726 const efx_dword_t *inbuf, size_t inlen,
727 efx_dword_t *outbuf, size_t outlen,
728 size_t *outlen_actual)
730 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
731 outlen_actual, false);
734 /* Normally, on receiving an error code in the MCDI response,
735 * efx_mcdi_rpc will log an error message containing (among other
736 * things) the raw error code, by means of efx_mcdi_display_error.
737 * This _quiet version suppresses that; if the caller wishes to log
738 * the error conditionally on the return code, it should call this
739 * function and is then responsible for calling efx_mcdi_display_error
742 int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
743 const efx_dword_t *inbuf, size_t inlen,
744 efx_dword_t *outbuf, size_t outlen,
745 size_t *outlen_actual)
747 return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
748 outlen_actual, true);
751 int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
752 const efx_dword_t *inbuf, size_t inlen)
754 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
757 rc = efx_mcdi_check_supported(efx, cmd, inlen);
761 if (efx->mc_bist_for_other_fn)
764 if (mcdi->mode == MCDI_MODE_FAIL)
767 efx_mcdi_acquire_sync(mcdi);
768 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
772 static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
773 const efx_dword_t *inbuf, size_t inlen,
775 efx_mcdi_async_completer *complete,
776 unsigned long cookie, bool quiet)
778 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
779 struct efx_mcdi_async_param *async;
782 rc = efx_mcdi_check_supported(efx, cmd, inlen);
786 if (efx->mc_bist_for_other_fn)
789 async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
795 async->inlen = inlen;
796 async->outlen = outlen;
797 async->quiet = quiet;
798 async->complete = complete;
799 async->cookie = cookie;
800 memcpy(async + 1, inbuf, inlen);
802 spin_lock_bh(&mcdi->async_lock);
804 if (mcdi->mode == MCDI_MODE_EVENTS) {
805 list_add_tail(&async->list, &mcdi->async_list);
807 /* If this is at the front of the queue, try to start it
810 if (mcdi->async_list.next == &async->list &&
811 efx_mcdi_acquire_async(mcdi)) {
812 efx_mcdi_send_request(efx, cmd, inbuf, inlen);
813 mod_timer(&mcdi->async_timer,
814 jiffies + MCDI_RPC_TIMEOUT);
821 spin_unlock_bh(&mcdi->async_lock);
827 * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
828 * @efx: NIC through which to issue the command
829 * @cmd: Command type number
830 * @inbuf: Command parameters
831 * @inlen: Length of command parameters, in bytes
832 * @outlen: Length to allocate for response buffer, in bytes
833 * @complete: Function to be called on completion or cancellation.
834 * @cookie: Arbitrary value to be passed to @complete.
836 * This function does not sleep and therefore may be called in atomic
837 * context. It will fail if event queues are disabled or if MCDI
838 * event completions have been disabled due to an error.
840 * If it succeeds, the @complete function will be called exactly once
841 * in atomic context, when one of the following occurs:
842 * (a) the completion event is received (in NAPI context)
843 * (b) event queues are disabled (in the process that disables them)
844 * (c) the request times-out (in timer context)
847 efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
848 const efx_dword_t *inbuf, size_t inlen, size_t outlen,
849 efx_mcdi_async_completer *complete, unsigned long cookie)
851 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
855 int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
856 const efx_dword_t *inbuf, size_t inlen,
857 size_t outlen, efx_mcdi_async_completer *complete,
858 unsigned long cookie)
860 return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
864 int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
865 efx_dword_t *outbuf, size_t outlen,
866 size_t *outlen_actual)
868 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
869 outlen_actual, false);
872 int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd, size_t inlen,
873 efx_dword_t *outbuf, size_t outlen,
874 size_t *outlen_actual)
876 return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
877 outlen_actual, true);
880 void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
881 size_t inlen, efx_dword_t *outbuf,
882 size_t outlen, int rc)
884 int code = 0, err_arg = 0;
886 if (outlen >= MC_CMD_ERR_CODE_OFST + 4)
887 code = MCDI_DWORD(outbuf, ERR_CODE);
888 if (outlen >= MC_CMD_ERR_ARG_OFST + 4)
889 err_arg = MCDI_DWORD(outbuf, ERR_ARG);
890 netif_err(efx, hw, efx->net_dev,
891 "MC command 0x%x inlen %d failed rc=%d (raw=%d) arg=%d\n",
892 cmd, (int)inlen, rc, code, err_arg);
895 /* Switch to polled MCDI completions. This can be called in various
896 * error conditions with various locks held, so it must be lockless.
897 * Caller is responsible for flushing asynchronous requests later.
899 void efx_mcdi_mode_poll(struct efx_nic *efx)
901 struct efx_mcdi_iface *mcdi;
906 mcdi = efx_mcdi(efx);
907 /* If already in polling mode, nothing to do.
908 * If in fail-fast state, don't switch to polled completion.
909 * FLR recovery will do that later.
911 if (mcdi->mode == MCDI_MODE_POLL || mcdi->mode == MCDI_MODE_FAIL)
914 /* We can switch from event completion to polled completion, because
915 * mcdi requests are always completed in shared memory. We do this by
916 * switching the mode to POLL'd then completing the request.
917 * efx_mcdi_await_completion() will then call efx_mcdi_poll().
919 * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
920 * which efx_mcdi_complete_sync() provides for us.
922 mcdi->mode = MCDI_MODE_POLL;
924 efx_mcdi_complete_sync(mcdi);
927 /* Flush any running or queued asynchronous requests, after event processing
930 void efx_mcdi_flush_async(struct efx_nic *efx)
932 struct efx_mcdi_async_param *async, *next;
933 struct efx_mcdi_iface *mcdi;
938 mcdi = efx_mcdi(efx);
940 /* We must be in poll or fail mode so no more requests can be queued */
941 BUG_ON(mcdi->mode == MCDI_MODE_EVENTS);
943 del_timer_sync(&mcdi->async_timer);
945 /* If a request is still running, make sure we give the MC
946 * time to complete it so that the response won't overwrite our
949 if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
951 mcdi->state = MCDI_STATE_QUIESCENT;
954 /* Nothing else will access the async list now, so it is safe
955 * to walk it without holding async_lock. If we hold it while
956 * calling a completer then lockdep may warn that we have
957 * acquired locks in the wrong order.
959 list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
960 async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
961 list_del(&async->list);
966 void efx_mcdi_mode_event(struct efx_nic *efx)
968 struct efx_mcdi_iface *mcdi;
973 mcdi = efx_mcdi(efx);
974 /* If already in event completion mode, nothing to do.
975 * If in fail-fast state, don't switch to event completion. FLR
976 * recovery will do that later.
978 if (mcdi->mode == MCDI_MODE_EVENTS || mcdi->mode == MCDI_MODE_FAIL)
981 /* We can't switch from polled to event completion in the middle of a
982 * request, because the completion method is specified in the request.
983 * So acquire the interface to serialise the requestors. We don't need
984 * to acquire the iface_lock to change the mode here, but we do need a
985 * write memory barrier ensure that efx_mcdi_rpc() sees it, which
986 * efx_mcdi_acquire() provides.
988 efx_mcdi_acquire_sync(mcdi);
989 mcdi->mode = MCDI_MODE_EVENTS;
990 efx_mcdi_release(mcdi);
993 static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
995 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
997 /* If there is an outstanding MCDI request, it has been terminated
998 * either by a BADASSERT or REBOOT event. If the mcdi interface is
999 * in polled mode, then do nothing because the MC reboot handler will
1000 * set the header correctly. However, if the mcdi interface is waiting
1001 * for a CMDDONE event it won't receive it [and since all MCDI events
1002 * are sent to the same queue, we can't be racing with
1003 * efx_mcdi_ev_cpl()]
1005 * If there is an outstanding asynchronous request, we can't
1006 * complete it now (efx_mcdi_complete() would deadlock). The
1007 * reset process will take care of this.
1009 * There's a race here with efx_mcdi_send_request(), because
1010 * we might receive a REBOOT event *before* the request has
1011 * been copied out. In polled mode (during startup) this is
1012 * irrelevant, because efx_mcdi_complete_sync() is ignored. In
1013 * event mode, this condition is just an edge-case of
1014 * receiving a REBOOT event after posting the MCDI
1015 * request. Did the mc reboot before or after the copyout? The
1016 * best we can do always is just return failure.
1018 spin_lock(&mcdi->iface_lock);
1019 if (efx_mcdi_complete_sync(mcdi)) {
1020 if (mcdi->mode == MCDI_MODE_EVENTS) {
1022 mcdi->resp_hdr_len = 0;
1023 mcdi->resp_data_len = 0;
1029 /* Consume the status word since efx_mcdi_rpc_finish() won't */
1030 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
1031 if (efx_mcdi_poll_reboot(efx))
1033 udelay(MCDI_STATUS_DELAY_US);
1035 mcdi->new_epoch = true;
1037 /* Nobody was waiting for an MCDI request, so trigger a reset */
1038 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
1041 spin_unlock(&mcdi->iface_lock);
1044 /* The MC is going down in to BIST mode. set the BIST flag to block
1045 * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
1046 * (which doesn't actually execute a reset, it waits for the controlling
1047 * function to reset it).
1049 static void efx_mcdi_ev_bist(struct efx_nic *efx)
1051 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1053 spin_lock(&mcdi->iface_lock);
1054 efx->mc_bist_for_other_fn = true;
1055 if (efx_mcdi_complete_sync(mcdi)) {
1056 if (mcdi->mode == MCDI_MODE_EVENTS) {
1057 mcdi->resprc = -EIO;
1058 mcdi->resp_hdr_len = 0;
1059 mcdi->resp_data_len = 0;
1063 mcdi->new_epoch = true;
1064 efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
1065 spin_unlock(&mcdi->iface_lock);
1068 /* MCDI timeouts seen, so make all MCDI calls fail-fast and issue an FLR to try
1071 static void efx_mcdi_abandon(struct efx_nic *efx)
1073 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1075 if (xchg(&mcdi->mode, MCDI_MODE_FAIL) == MCDI_MODE_FAIL)
1076 return; /* it had already been done */
1077 netif_dbg(efx, hw, efx->net_dev, "MCDI is timing out; trying to recover\n");
1078 efx_schedule_reset(efx, RESET_TYPE_MCDI_TIMEOUT);
1081 /* Called from falcon_process_eventq for MCDI events */
1082 void efx_mcdi_process_event(struct efx_channel *channel,
1085 struct efx_nic *efx = channel->efx;
1086 int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
1087 u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
1090 case MCDI_EVENT_CODE_BADSSERT:
1091 netif_err(efx, hw, efx->net_dev,
1092 "MC watchdog or assertion failure at 0x%x\n", data);
1093 efx_mcdi_ev_death(efx, -EINTR);
1096 case MCDI_EVENT_CODE_PMNOTICE:
1097 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
1100 case MCDI_EVENT_CODE_CMDDONE:
1101 efx_mcdi_ev_cpl(efx,
1102 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
1103 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
1104 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
1107 case MCDI_EVENT_CODE_LINKCHANGE:
1108 efx_mcdi_process_link_change(efx, event);
1110 case MCDI_EVENT_CODE_SENSOREVT:
1111 efx_mcdi_sensor_event(efx, event);
1113 case MCDI_EVENT_CODE_SCHEDERR:
1114 netif_dbg(efx, hw, efx->net_dev,
1115 "MC Scheduler alert (0x%x)\n", data);
1117 case MCDI_EVENT_CODE_REBOOT:
1118 case MCDI_EVENT_CODE_MC_REBOOT:
1119 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
1120 efx_mcdi_ev_death(efx, -EIO);
1122 case MCDI_EVENT_CODE_MC_BIST:
1123 netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
1124 efx_mcdi_ev_bist(efx);
1126 case MCDI_EVENT_CODE_MAC_STATS_DMA:
1127 /* MAC stats are gather lazily. We can ignore this. */
1129 case MCDI_EVENT_CODE_FLR:
1130 if (efx->type->sriov_flr)
1131 efx->type->sriov_flr(efx,
1132 MCDI_EVENT_FIELD(*event, FLR_VF));
1134 case MCDI_EVENT_CODE_PTP_RX:
1135 case MCDI_EVENT_CODE_PTP_FAULT:
1136 case MCDI_EVENT_CODE_PTP_PPS:
1137 efx_ptp_event(efx, event);
1139 case MCDI_EVENT_CODE_PTP_TIME:
1140 efx_time_sync_event(channel, event);
1142 case MCDI_EVENT_CODE_TX_FLUSH:
1143 case MCDI_EVENT_CODE_RX_FLUSH:
1144 /* Two flush events will be sent: one to the same event
1145 * queue as completions, and one to event queue 0.
1146 * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
1147 * flag will be set, and we should ignore the event
1148 * because we want to wait for all completions.
1150 BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
1151 MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
1152 if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
1153 efx_ef10_handle_drain_event(efx);
1155 case MCDI_EVENT_CODE_TX_ERR:
1156 case MCDI_EVENT_CODE_RX_ERR:
1157 netif_err(efx, hw, efx->net_dev,
1158 "%s DMA error (event: "EFX_QWORD_FMT")\n",
1159 code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
1160 EFX_QWORD_VAL(*event));
1161 efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
1164 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
1169 /**************************************************************************
1171 * Specific request functions
1173 **************************************************************************
1176 void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
1178 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN);
1180 const __le16 *ver_words;
1184 BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
1185 rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
1186 outbuf, sizeof(outbuf), &outlength);
1189 if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
1194 ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
1195 offset = snprintf(buf, len, "%u.%u.%u.%u",
1196 le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
1197 le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
1199 /* EF10 may have multiple datapath firmware variants within a
1200 * single version. Report which variants are running.
1202 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1203 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1205 offset += snprintf(buf + offset, len - offset, " rx%x tx%x",
1206 nic_data->rx_dpcpu_fw_id,
1207 nic_data->tx_dpcpu_fw_id);
1209 /* It's theoretically possible for the string to exceed 31
1210 * characters, though in practice the first three version
1211 * components are short enough that this doesn't happen.
1213 if (WARN_ON(offset >= len))
1220 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1224 static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
1227 MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
1228 MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
1232 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
1233 driver_operating ? 1 : 0);
1234 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
1235 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
1237 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
1238 outbuf, sizeof(outbuf), &outlen);
1239 /* If we're not the primary PF, trying to ATTACH with a FIRMWARE_ID
1240 * specified will fail with EPERM, and we have to tell the MC we don't
1241 * care what firmware we get.
1244 netif_dbg(efx, probe, efx->net_dev,
1245 "efx_mcdi_drv_attach with fw-variant setting failed EPERM, trying without it\n");
1246 MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID,
1247 MC_CMD_FW_DONT_CARE);
1248 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf,
1249 sizeof(inbuf), outbuf, sizeof(outbuf),
1253 efx_mcdi_display_error(efx, MC_CMD_DRV_ATTACH, sizeof(inbuf),
1254 outbuf, outlen, rc);
1257 if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
1262 if (driver_operating) {
1263 if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) {
1264 efx->mcdi->fn_flags =
1266 DRV_ATTACH_EXT_OUT_FUNC_FLAGS);
1268 /* Synthesise flags for Siena */
1269 efx->mcdi->fn_flags =
1270 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1271 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED |
1272 (efx_port_num(efx) == 0) <<
1273 MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY;
1277 /* We currently assume we have control of the external link
1278 * and are completely trusted by firmware. Abort probing
1279 * if that's not true for this function.
1282 if (was_attached != NULL)
1283 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
1287 netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1291 int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
1292 u16 *fw_subtype_list, u32 *capabilities)
1294 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
1296 int port_num = efx_port_num(efx);
1299 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
1300 /* we need __aligned(2) for ether_addr_copy */
1301 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1);
1302 BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1);
1304 rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
1305 outbuf, sizeof(outbuf), &outlen);
1309 if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
1315 ether_addr_copy(mac_address,
1317 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
1318 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0));
1319 if (fw_subtype_list) {
1321 i < MCDI_VAR_ARRAY_LEN(outlen,
1322 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
1324 fw_subtype_list[i] = MCDI_ARRAY_WORD(
1325 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
1326 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
1327 fw_subtype_list[i] = 0;
1331 *capabilities = MCDI_DWORD(outbuf,
1332 GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1334 *capabilities = MCDI_DWORD(outbuf,
1335 GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1341 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
1342 __func__, rc, (int)outlen);
1347 int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
1349 MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
1354 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
1356 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
1358 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
1359 MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
1361 BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
1363 rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
1368 int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
1370 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
1374 BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
1376 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
1377 outbuf, sizeof(outbuf), &outlen);
1380 if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
1385 *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
1389 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1394 int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
1395 size_t *size_out, size_t *erase_size_out,
1396 bool *protected_out)
1398 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
1399 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
1403 MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
1405 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
1406 outbuf, sizeof(outbuf), &outlen);
1409 if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
1414 *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
1415 *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
1416 *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
1417 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
1421 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1425 static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
1427 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
1428 MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
1431 MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
1433 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
1434 outbuf, sizeof(outbuf), NULL);
1438 switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
1439 case MC_CMD_NVRAM_TEST_PASS:
1440 case MC_CMD_NVRAM_TEST_NOTSUPP:
1447 int efx_mcdi_nvram_test_all(struct efx_nic *efx)
1453 rc = efx_mcdi_nvram_types(efx, &nvram_types);
1458 while (nvram_types != 0) {
1459 if (nvram_types & 1) {
1460 rc = efx_mcdi_nvram_test(efx, type);
1471 netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
1474 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1478 /* Returns 1 if an assertion was read, 0 if no assertion had fired,
1479 * negative on error.
1481 static int efx_mcdi_read_assertion(struct efx_nic *efx)
1483 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
1484 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
1485 unsigned int flags, index;
1491 /* Attempt to read any stored assertion state before we reboot
1492 * the mcfw out of the assertion handler. Retry twice, once
1493 * because a boot-time assertion might cause this command to fail
1494 * with EINTR. And once again because GET_ASSERTS can race with
1495 * MC_CMD_REBOOT running on the other port. */
1498 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
1499 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS,
1500 inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1501 outbuf, sizeof(outbuf), &outlen);
1504 } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1507 efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS,
1508 MC_CMD_GET_ASSERTS_IN_LEN, outbuf,
1512 if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
1515 /* Print out any recorded assertion state */
1516 flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
1517 if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1520 reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1521 ? "system-level assertion"
1522 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1523 ? "thread-level assertion"
1524 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1526 : "unknown assertion";
1527 netif_err(efx, hw, efx->net_dev,
1528 "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1529 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1530 MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
1532 /* Print out the registers */
1534 index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1536 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
1538 MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
1544 static int efx_mcdi_exit_assertion(struct efx_nic *efx)
1546 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
1549 /* If the MC is running debug firmware, it might now be
1550 * waiting for a debugger to attach, but we just want it to
1551 * reboot. We set a flag that makes the command a no-op if it
1552 * has already done so.
1553 * The MCDI will thus return either 0 or -EIO.
1555 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1556 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1557 MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
1558 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1563 efx_mcdi_display_error(efx, MC_CMD_REBOOT, MC_CMD_REBOOT_IN_LEN,
1568 int efx_mcdi_handle_assertion(struct efx_nic *efx)
1572 rc = efx_mcdi_read_assertion(efx);
1576 return efx_mcdi_exit_assertion(efx);
1579 void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1581 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
1584 BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1585 BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1586 BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1588 BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1590 MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1592 rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1596 static int efx_mcdi_reset_func(struct efx_nic *efx)
1598 MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN);
1601 BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0);
1602 MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG,
1603 ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1604 rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf),
1609 static int efx_mcdi_reset_mc(struct efx_nic *efx)
1611 MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
1614 BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1615 MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1616 rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1618 /* White is black, and up is down */
1626 enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1628 return RESET_TYPE_RECOVER_OR_ALL;
1631 int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1635 /* If MCDI is down, we can't handle_assertion */
1636 if (method == RESET_TYPE_MCDI_TIMEOUT) {
1637 rc = pci_reset_function(efx->pci_dev);
1640 /* Re-enable polled MCDI completion */
1642 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1643 mcdi->mode = MCDI_MODE_POLL;
1648 /* Recover from a failed assertion pre-reset */
1649 rc = efx_mcdi_handle_assertion(efx);
1653 if (method == RESET_TYPE_DATAPATH)
1655 else if (method == RESET_TYPE_WORLD)
1656 return efx_mcdi_reset_mc(efx);
1658 return efx_mcdi_reset_func(efx);
1661 static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1662 const u8 *mac, int *id_out)
1664 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1665 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
1669 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1670 MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1671 MC_CMD_FILTER_MODE_SIMPLE);
1672 ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac);
1674 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1675 outbuf, sizeof(outbuf), &outlen);
1679 if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
1684 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1690 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1697 efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
1699 return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1703 int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1705 MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
1709 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1710 outbuf, sizeof(outbuf), &outlen);
1714 if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
1719 *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1725 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1730 int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1732 MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
1735 MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1737 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1742 int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1744 struct efx_channel *channel;
1745 struct efx_rx_queue *rx_queue;
1746 MCDI_DECLARE_BUF(inbuf,
1747 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
1750 BUILD_BUG_ON(EFX_MAX_CHANNELS >
1751 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1754 efx_for_each_channel(channel, efx) {
1755 efx_for_each_channel_rx_queue(rx_queue, channel) {
1756 if (rx_queue->flush_pending) {
1757 rx_queue->flush_pending = false;
1758 atomic_dec(&efx->rxq_flush_pending);
1759 MCDI_SET_ARRAY_DWORD(
1760 inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1761 count, efx_rx_queue_index(rx_queue));
1767 rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1768 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
1774 int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1778 rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1782 int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled,
1783 unsigned int *flags)
1785 MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
1786 MCDI_DECLARE_BUF(outbuf, MC_CMD_WORKAROUND_EXT_OUT_LEN);
1790 BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
1791 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
1792 MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
1793 rc = efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
1794 outbuf, sizeof(outbuf), &outlen);
1801 if (outlen >= MC_CMD_WORKAROUND_EXT_OUT_LEN)
1802 *flags = MCDI_DWORD(outbuf, WORKAROUND_EXT_OUT_FLAGS);
1809 int efx_mcdi_get_workarounds(struct efx_nic *efx, unsigned int *impl_out,
1810 unsigned int *enabled_out)
1812 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_WORKAROUNDS_OUT_LEN);
1816 rc = efx_mcdi_rpc(efx, MC_CMD_GET_WORKAROUNDS, NULL, 0,
1817 outbuf, sizeof(outbuf), &outlen);
1821 if (outlen < MC_CMD_GET_WORKAROUNDS_OUT_LEN) {
1827 *impl_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_IMPLEMENTED);
1830 *enabled_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_ENABLED);
1835 /* Older firmware lacks GET_WORKAROUNDS and this isn't especially
1836 * terrifying. The call site will have to deal with it though.
1838 netif_printk(efx, hw, rc == -ENOSYS ? KERN_DEBUG : KERN_ERR,
1839 efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1843 #ifdef CONFIG_SFC_MTD
1845 #define EFX_MCDI_NVRAM_LEN_MAX 128
1847 static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
1849 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
1852 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
1854 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
1856 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
1861 static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
1862 loff_t offset, u8 *buffer, size_t length)
1864 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
1865 MCDI_DECLARE_BUF(outbuf,
1866 MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1870 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
1871 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
1872 MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
1874 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
1875 outbuf, sizeof(outbuf), &outlen);
1879 memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
1883 static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
1884 loff_t offset, const u8 *buffer, size_t length)
1886 MCDI_DECLARE_BUF(inbuf,
1887 MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1890 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
1891 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
1892 MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
1893 memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
1895 BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
1897 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
1898 ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
1903 static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
1904 loff_t offset, size_t length)
1906 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
1909 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
1910 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
1911 MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
1913 BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
1915 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
1920 static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
1922 MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
1925 MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
1927 BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
1929 rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
1934 int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
1935 size_t len, size_t *retlen, u8 *buffer)
1937 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1938 struct efx_nic *efx = mtd->priv;
1939 loff_t offset = start;
1940 loff_t end = min_t(loff_t, start + len, mtd->size);
1944 while (offset < end) {
1945 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1946 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
1954 *retlen = offset - start;
1958 int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
1960 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1961 struct efx_nic *efx = mtd->priv;
1962 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
1963 loff_t end = min_t(loff_t, start + len, mtd->size);
1964 size_t chunk = part->common.mtd.erasesize;
1967 if (!part->updating) {
1968 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1971 part->updating = true;
1974 /* The MCDI interface can in fact do multiple erase blocks at once;
1975 * but erasing may be slow, so we make multiple calls here to avoid
1976 * tripping the MCDI RPC timeout. */
1977 while (offset < end) {
1978 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
1988 int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
1989 size_t len, size_t *retlen, const u8 *buffer)
1991 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1992 struct efx_nic *efx = mtd->priv;
1993 loff_t offset = start;
1994 loff_t end = min_t(loff_t, start + len, mtd->size);
1998 if (!part->updating) {
1999 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
2002 part->updating = true;
2005 while (offset < end) {
2006 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
2007 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
2015 *retlen = offset - start;
2019 int efx_mcdi_mtd_sync(struct mtd_info *mtd)
2021 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
2022 struct efx_nic *efx = mtd->priv;
2025 if (part->updating) {
2026 part->updating = false;
2027 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
2033 void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
2035 struct efx_mcdi_mtd_partition *mcdi_part =
2036 container_of(part, struct efx_mcdi_mtd_partition, common);
2037 struct efx_nic *efx = part->mtd.priv;
2039 snprintf(part->name, sizeof(part->name), "%s %s:%02x",
2040 efx->name, part->type_name, mcdi_part->fw_subtype);
2043 #endif /* CONFIG_SFC_MTD */