Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1 | /**************************************************************************** |
Ben Hutchings | f7a6d2c | 2013-08-29 23:32:48 +0100 | [diff] [blame] | 2 | * Driver for Solarflare network controllers and boards |
| 3 | * Copyright 2008-2013 Solarflare Communications Inc. |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 4 | * |
| 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> |
Edward Cree | 42ca087 | 2015-05-27 13:14:26 +0100 | [diff] [blame] | 11 | #include <linux/moduleparam.h> |
Boqun Feng | 8456799 | 2015-08-26 19:52:46 +0800 | [diff] [blame] | 12 | #include <linux/atomic.h> |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 13 | #include "net_driver.h" |
| 14 | #include "nic.h" |
| 15 | #include "io.h" |
Ben Hutchings | 8b8a95a | 2012-09-18 01:57:07 +0100 | [diff] [blame] | 16 | #include "farch_regs.h" |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 17 | #include "mcdi_pcol.h" |
| 18 | #include "phy.h" |
| 19 | |
| 20 | /************************************************************************** |
| 21 | * |
| 22 | * Management-Controller-to-Driver Interface |
| 23 | * |
| 24 | ************************************************************************** |
| 25 | */ |
| 26 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 27 | #define MCDI_RPC_TIMEOUT (10 * HZ) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 28 | |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 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 |
Daniel Pieczko | b2d32f0 | 2013-09-20 16:45:10 +0100 | [diff] [blame] | 31 | * via these mechanisms then wait 250ms for the status word to be set. |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 32 | */ |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 33 | #define MCDI_STATUS_DELAY_US 100 |
Daniel Pieczko | b2d32f0 | 2013-09-20 16:45:10 +0100 | [diff] [blame] | 34 | #define MCDI_STATUS_DELAY_COUNT 2500 |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 35 | #define MCDI_STATUS_SLEEP_MS \ |
| 36 | (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 37 | |
| 38 | #define SEQ_MASK \ |
| 39 | EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ)) |
| 40 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 41 | struct efx_mcdi_async_param { |
| 42 | struct list_head list; |
| 43 | unsigned int cmd; |
| 44 | size_t inlen; |
| 45 | size_t outlen; |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 46 | bool quiet; |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 47 | efx_mcdi_async_completer *complete; |
| 48 | unsigned long cookie; |
| 49 | /* followed by request/response buffer */ |
| 50 | }; |
| 51 | |
| 52 | static void efx_mcdi_timeout_async(unsigned long context); |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 53 | static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating, |
| 54 | bool *was_attached_out); |
Robert Stonehouse | 5731d7b | 2013-10-09 11:52:43 +0100 | [diff] [blame] | 55 | static bool efx_mcdi_poll_once(struct efx_nic *efx); |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 56 | static void efx_mcdi_abandon(struct efx_nic *efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 57 | |
Edward Cree | 42ca087 | 2015-05-27 13:14:26 +0100 | [diff] [blame] | 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"); |
| 63 | #endif |
| 64 | |
Ben Hutchings | f073dde | 2012-09-18 02:33:55 +0100 | [diff] [blame] | 65 | int efx_mcdi_init(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 66 | { |
| 67 | struct efx_mcdi_iface *mcdi; |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 68 | bool already_attached; |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 69 | int rc = -ENOMEM; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 70 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 71 | efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL); |
| 72 | if (!efx->mcdi) |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 73 | goto fail; |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 74 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 75 | mcdi = efx_mcdi(efx); |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 76 | mcdi->efx = efx; |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 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) |
| 81 | goto fail1; |
Edward Cree | 42ca087 | 2015-05-27 13:14:26 +0100 | [diff] [blame] | 82 | mcdi->logging_enabled = mcdi_logging_default; |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 83 | #endif |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 84 | init_waitqueue_head(&mcdi->wq); |
| 85 | spin_lock_init(&mcdi->iface_lock); |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 86 | mcdi->state = MCDI_STATE_QUIESCENT; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 87 | mcdi->mode = MCDI_MODE_POLL; |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 88 | spin_lock_init(&mcdi->async_lock); |
| 89 | INIT_LIST_HEAD(&mcdi->async_list); |
| 90 | setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async, |
| 91 | (unsigned long)mcdi); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 92 | |
| 93 | (void) efx_mcdi_poll_reboot(efx); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 94 | mcdi->new_epoch = true; |
Ben Hutchings | f073dde | 2012-09-18 02:33:55 +0100 | [diff] [blame] | 95 | |
| 96 | /* Recover from a failed assertion before probing */ |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 97 | rc = efx_mcdi_handle_assertion(efx); |
| 98 | if (rc) |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 99 | goto fail2; |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 100 | |
| 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. |
| 103 | */ |
| 104 | rc = efx_mcdi_drv_attach(efx, true, &already_attached); |
| 105 | if (rc) { |
| 106 | netif_err(efx, probe, efx->net_dev, |
| 107 | "Unable to register driver with MCPU\n"); |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 108 | goto fail2; |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 109 | } |
| 110 | if (already_attached) |
| 111 | /* Not a fatal error */ |
| 112 | netif_err(efx, probe, efx->net_dev, |
| 113 | "Host already registered with MCPU\n"); |
| 114 | |
Ben Hutchings | 0bcf4a6 | 2013-10-18 19:21:45 +0100 | [diff] [blame] | 115 | if (efx->mcdi->fn_flags & |
| 116 | (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) |
| 117 | efx->primary = efx; |
| 118 | |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 119 | return 0; |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 120 | fail2: |
| 121 | #ifdef CONFIG_SFC_MCDI_LOGGING |
| 122 | free_page((unsigned long)mcdi->logging_buffer); |
| 123 | fail1: |
| 124 | #endif |
| 125 | kfree(efx->mcdi); |
| 126 | efx->mcdi = NULL; |
| 127 | fail: |
| 128 | return rc; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 131 | void efx_mcdi_fini(struct efx_nic *efx) |
| 132 | { |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 133 | if (!efx->mcdi) |
| 134 | return; |
| 135 | |
| 136 | BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT); |
| 137 | |
| 138 | /* Relinquish the device (back to the BMC, if this is a LOM) */ |
| 139 | efx_mcdi_drv_attach(efx, false, NULL); |
| 140 | |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_SFC_MCDI_LOGGING |
| 142 | free_page((unsigned long)efx->mcdi->iface.logging_buffer); |
| 143 | #endif |
| 144 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 145 | kfree(efx->mcdi); |
| 146 | } |
| 147 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame] | 148 | static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd, |
| 149 | const efx_dword_t *inbuf, size_t inlen) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 150 | { |
| 151 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_SFC_MCDI_LOGGING |
| 153 | char *buf = mcdi->logging_buffer; /* page-sized */ |
| 154 | #endif |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 155 | efx_dword_t hdr[2]; |
| 156 | size_t hdr_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 157 | u32 xflags, seqno; |
| 158 | |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 159 | BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 160 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame] | 161 | /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */ |
| 162 | spin_lock_bh(&mcdi->iface_lock); |
| 163 | ++mcdi->seqno; |
| 164 | spin_unlock_bh(&mcdi->iface_lock); |
| 165 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 166 | seqno = mcdi->seqno & SEQ_MASK; |
| 167 | xflags = 0; |
| 168 | if (mcdi->mode == MCDI_MODE_EVENTS) |
| 169 | xflags |= MCDI_HEADER_XFLAGS_EVREQ; |
| 170 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 171 | if (efx->type->mcdi_max_ver == 1) { |
| 172 | /* MCDI v1 */ |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 173 | EFX_POPULATE_DWORD_7(hdr[0], |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 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, |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 179 | MCDI_HEADER_XFLAGS, xflags, |
| 180 | MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 181 | hdr_len = 4; |
| 182 | } else { |
| 183 | /* MCDI v2 */ |
| 184 | BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 185 | EFX_POPULATE_DWORD_7(hdr[0], |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 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, |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 191 | MCDI_HEADER_XFLAGS, xflags, |
| 192 | MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 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); |
| 196 | hdr_len = 8; |
| 197 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 198 | |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 199 | #ifdef CONFIG_SFC_MCDI_LOGGING |
Edward Cree | e7fef9b | 2015-05-27 13:14:01 +0100 | [diff] [blame] | 200 | if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) { |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 201 | int bytes = 0; |
| 202 | int i; |
| 203 | /* Lengths should always be a whole number of dwords, so scream |
| 204 | * if they're not. |
| 205 | */ |
| 206 | WARN_ON_ONCE(hdr_len % 4); |
| 207 | WARN_ON_ONCE(inlen % 4); |
| 208 | |
| 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. |
| 211 | */ |
| 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])); |
| 215 | |
| 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])); |
| 219 | |
| 220 | netif_info(efx, hw, efx->net_dev, "MCDI RPC REQ:%s\n", buf); |
| 221 | } |
| 222 | #endif |
| 223 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 224 | efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen); |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame] | 225 | |
| 226 | mcdi->new_epoch = false; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 229 | static int efx_mcdi_errno(unsigned int mcdi_err) |
| 230 | { |
| 231 | switch (mcdi_err) { |
| 232 | case 0: |
| 233 | return 0; |
| 234 | #define TRANSLATE_ERROR(name) \ |
| 235 | case MC_CMD_ERR_ ## name: \ |
| 236 | return -name; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 237 | TRANSLATE_ERROR(EPERM); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 238 | TRANSLATE_ERROR(ENOENT); |
| 239 | TRANSLATE_ERROR(EINTR); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 240 | TRANSLATE_ERROR(EAGAIN); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 241 | TRANSLATE_ERROR(EACCES); |
| 242 | TRANSLATE_ERROR(EBUSY); |
| 243 | TRANSLATE_ERROR(EINVAL); |
| 244 | TRANSLATE_ERROR(EDEADLK); |
| 245 | TRANSLATE_ERROR(ENOSYS); |
| 246 | TRANSLATE_ERROR(ETIME); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 247 | TRANSLATE_ERROR(EALREADY); |
| 248 | TRANSLATE_ERROR(ENOSPC); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 249 | #undef TRANSLATE_ERROR |
Ben Hutchings | ea136ae | 2013-10-08 16:36:58 +0100 | [diff] [blame] | 250 | case MC_CMD_ERR_ENOTSUP: |
| 251 | return -EOPNOTSUPP; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 252 | case MC_CMD_ERR_ALLOC_FAIL: |
| 253 | return -ENOBUFS; |
| 254 | case MC_CMD_ERR_MAC_EXIST: |
| 255 | return -EADDRINUSE; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 256 | default: |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 257 | return -EPROTO; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void efx_mcdi_read_response_header(struct efx_nic *efx) |
| 262 | { |
| 263 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 264 | unsigned int respseq, respcmd, error; |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 265 | #ifdef CONFIG_SFC_MCDI_LOGGING |
| 266 | char *buf = mcdi->logging_buffer; /* page-sized */ |
| 267 | #endif |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 268 | efx_dword_t hdr; |
| 269 | |
| 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); |
| 274 | |
| 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); |
| 278 | } else { |
| 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); |
| 283 | } |
| 284 | |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 285 | #ifdef CONFIG_SFC_MCDI_LOGGING |
Edward Cree | e7fef9b | 2015-05-27 13:14:01 +0100 | [diff] [blame] | 286 | if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) { |
Edward Cree | 75aba2a | 2015-05-27 13:13:54 +0100 | [diff] [blame] | 287 | size_t hdr_len, data_len; |
| 288 | int bytes = 0; |
| 289 | int i; |
| 290 | |
| 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 |
| 295 | */ |
| 296 | data_len = DIV_ROUND_UP(mcdi->resp_data_len, 4); |
| 297 | |
| 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. |
| 300 | */ |
| 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])); |
| 305 | } |
| 306 | |
| 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])); |
| 312 | } |
| 313 | |
| 314 | netif_info(efx, hw, efx->net_dev, "MCDI RPC RESP:%s\n", buf); |
| 315 | } |
| 316 | #endif |
| 317 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 318 | if (error && mcdi->resp_data_len == 0) { |
| 319 | netif_err(efx, hw, efx->net_dev, "MC rebooted\n"); |
| 320 | mcdi->resprc = -EIO; |
| 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); |
| 325 | mcdi->resprc = -EIO; |
| 326 | } else if (error) { |
| 327 | efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4); |
| 328 | mcdi->resprc = |
| 329 | efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0)); |
| 330 | } else { |
| 331 | mcdi->resprc = 0; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Robert Stonehouse | 5731d7b | 2013-10-09 11:52:43 +0100 | [diff] [blame] | 335 | static bool efx_mcdi_poll_once(struct efx_nic *efx) |
| 336 | { |
| 337 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 338 | |
| 339 | rmb(); |
| 340 | if (!efx->type->mcdi_poll_response(efx)) |
| 341 | return false; |
| 342 | |
| 343 | spin_lock_bh(&mcdi->iface_lock); |
| 344 | efx_mcdi_read_response_header(efx); |
| 345 | spin_unlock_bh(&mcdi->iface_lock); |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 350 | static int efx_mcdi_poll(struct efx_nic *efx) |
| 351 | { |
| 352 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 353 | unsigned long time, finish; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 354 | unsigned int spins; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 355 | int rc; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 356 | |
| 357 | /* Check for a reboot atomically with respect to efx_mcdi_copyout() */ |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 358 | rc = efx_mcdi_poll_reboot(efx); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 359 | if (rc) { |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 360 | spin_lock_bh(&mcdi->iface_lock); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 361 | mcdi->resprc = rc; |
| 362 | mcdi->resp_hdr_len = 0; |
| 363 | mcdi->resp_data_len = 0; |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 364 | spin_unlock_bh(&mcdi->iface_lock); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 365 | return 0; |
| 366 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 367 | |
| 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) |
| 371 | */ |
| 372 | spins = TICK_USEC; |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 373 | finish = jiffies + MCDI_RPC_TIMEOUT; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 374 | |
| 375 | while (1) { |
| 376 | if (spins != 0) { |
| 377 | --spins; |
| 378 | udelay(1); |
Ben Hutchings | 55029c1 | 2010-01-13 04:34:25 +0000 | [diff] [blame] | 379 | } else { |
| 380 | schedule_timeout_uninterruptible(1); |
| 381 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 382 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 383 | time = jiffies; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 384 | |
Robert Stonehouse | 5731d7b | 2013-10-09 11:52:43 +0100 | [diff] [blame] | 385 | if (efx_mcdi_poll_once(efx)) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 386 | break; |
| 387 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 388 | if (time_after(time, finish)) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 389 | return -ETIMEDOUT; |
| 390 | } |
| 391 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 392 | /* Return rc=0 like wait_event_timeout() */ |
| 393 | return 0; |
| 394 | } |
| 395 | |
Ben Hutchings | 876be08 | 2012-10-01 20:58:35 +0100 | [diff] [blame] | 396 | /* Test and clear MC-rebooted flag for this port/function; reset |
| 397 | * software state as necessary. |
| 398 | */ |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 399 | int efx_mcdi_poll_reboot(struct efx_nic *efx) |
| 400 | { |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 401 | if (!efx->mcdi) |
| 402 | return 0; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 403 | |
Ben Hutchings | cd0ecc9 | 2012-12-14 21:52:56 +0000 | [diff] [blame] | 404 | return efx->type->mcdi_poll_reboot(efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 407 | static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi) |
| 408 | { |
| 409 | return cmpxchg(&mcdi->state, |
| 410 | MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) == |
| 411 | MCDI_STATE_QUIESCENT; |
| 412 | } |
| 413 | |
| 414 | static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 415 | { |
| 416 | /* Wait until the interface becomes QUIESCENT and we win the race |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 417 | * to mark it RUNNING_SYNC. |
| 418 | */ |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 419 | wait_event(mcdi->wq, |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 420 | cmpxchg(&mcdi->state, |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 421 | MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) == |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 422 | MCDI_STATE_QUIESCENT); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static int efx_mcdi_await_completion(struct efx_nic *efx) |
| 426 | { |
| 427 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 428 | |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 429 | if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED, |
| 430 | MCDI_RPC_TIMEOUT) == 0) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 431 | return -ETIMEDOUT; |
| 432 | |
| 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. |
| 437 | * |
| 438 | * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which |
| 439 | * wait_event_timeout() implicitly provides. |
| 440 | */ |
| 441 | if (mcdi->mode == MCDI_MODE_POLL) |
| 442 | return efx_mcdi_poll(efx); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 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. |
| 449 | */ |
| 450 | static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 451 | { |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 452 | if (cmpxchg(&mcdi->state, |
| 453 | MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) == |
| 454 | MCDI_STATE_RUNNING_SYNC) { |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 455 | wake_up(&mcdi->wq); |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | static void efx_mcdi_release(struct efx_mcdi_iface *mcdi) |
| 463 | { |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 464 | if (mcdi->mode == MCDI_MODE_EVENTS) { |
| 465 | struct efx_mcdi_async_param *async; |
| 466 | struct efx_nic *efx = mcdi->efx; |
| 467 | |
| 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); |
| 472 | if (async) { |
| 473 | mcdi->state = MCDI_STATE_RUNNING_ASYNC; |
| 474 | efx_mcdi_send_request(efx, async->cmd, |
| 475 | (const efx_dword_t *)(async + 1), |
| 476 | async->inlen); |
| 477 | mod_timer(&mcdi->async_timer, |
| 478 | jiffies + MCDI_RPC_TIMEOUT); |
| 479 | } |
| 480 | spin_unlock_bh(&mcdi->async_lock); |
| 481 | |
| 482 | if (async) |
| 483 | return; |
| 484 | } |
| 485 | |
Ben Hutchings | 251111d | 2013-08-27 23:04:29 +0100 | [diff] [blame] | 486 | mcdi->state = MCDI_STATE_QUIESCENT; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 487 | wake_up(&mcdi->wq); |
| 488 | } |
| 489 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 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. |
| 494 | */ |
| 495 | static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout) |
| 496 | { |
| 497 | struct efx_nic *efx = mcdi->efx; |
| 498 | struct efx_mcdi_async_param *async; |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 499 | size_t hdr_len, data_len, err_len; |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 500 | efx_dword_t *outbuf; |
Jon Cooper | aa09a3d | 2015-05-20 11:10:41 +0100 | [diff] [blame] | 501 | MCDI_DECLARE_BUF_ERR(errbuf); |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 502 | int rc; |
| 503 | |
| 504 | if (cmpxchg(&mcdi->state, |
| 505 | MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) != |
| 506 | MCDI_STATE_RUNNING_ASYNC) |
| 507 | return false; |
| 508 | |
| 509 | spin_lock(&mcdi->iface_lock); |
| 510 | if (timeout) { |
| 511 | /* Ensure that if the completion event arrives later, |
| 512 | * the seqno check in efx_mcdi_ev_cpl() will fail |
| 513 | */ |
| 514 | ++mcdi->seqno; |
| 515 | ++mcdi->credits; |
| 516 | rc = -ETIMEDOUT; |
| 517 | hdr_len = 0; |
| 518 | data_len = 0; |
| 519 | } else { |
| 520 | rc = mcdi->resprc; |
| 521 | hdr_len = mcdi->resp_hdr_len; |
| 522 | data_len = mcdi->resp_data_len; |
| 523 | } |
| 524 | spin_unlock(&mcdi->iface_lock); |
| 525 | |
| 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. |
| 529 | */ |
| 530 | if (!timeout) |
| 531 | del_timer_sync(&mcdi->async_timer); |
| 532 | |
| 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); |
| 538 | |
| 539 | outbuf = (efx_dword_t *)(async + 1); |
| 540 | efx->type->mcdi_read_response(efx, outbuf, hdr_len, |
| 541 | min(async->outlen, data_len)); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 542 | if (!timeout && rc && !async->quiet) { |
| 543 | err_len = min(sizeof(errbuf), data_len); |
| 544 | efx->type->mcdi_read_response(efx, errbuf, hdr_len, |
| 545 | sizeof(errbuf)); |
| 546 | efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf, |
| 547 | err_len, rc); |
| 548 | } |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 549 | async->complete(efx, async->cookie, rc, outbuf, data_len); |
| 550 | kfree(async); |
| 551 | |
| 552 | efx_mcdi_release(mcdi); |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 557 | static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno, |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 558 | unsigned int datalen, unsigned int mcdi_err) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 559 | { |
| 560 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 561 | bool wake = false; |
| 562 | |
| 563 | spin_lock(&mcdi->iface_lock); |
| 564 | |
| 565 | if ((seqno ^ mcdi->seqno) & SEQ_MASK) { |
| 566 | if (mcdi->credits) |
| 567 | /* The request has been cancelled */ |
| 568 | --mcdi->credits; |
| 569 | else |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 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); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 573 | } else { |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 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); |
| 577 | } else { |
| 578 | mcdi->resprc = efx_mcdi_errno(mcdi_err); |
| 579 | mcdi->resp_hdr_len = 4; |
| 580 | mcdi->resp_data_len = datalen; |
| 581 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 582 | |
| 583 | wake = true; |
| 584 | } |
| 585 | |
| 586 | spin_unlock(&mcdi->iface_lock); |
| 587 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 588 | if (wake) { |
| 589 | if (!efx_mcdi_complete_async(mcdi, false)) |
| 590 | (void) efx_mcdi_complete_sync(mcdi); |
| 591 | |
| 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]. |
| 597 | */ |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | static void efx_mcdi_timeout_async(unsigned long context) |
| 602 | { |
| 603 | struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context; |
| 604 | |
| 605 | efx_mcdi_complete_async(mcdi, true); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame] | 608 | static int |
| 609 | efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen) |
| 610 | { |
| 611 | if (efx->type->mcdi_max_ver < 0 || |
| 612 | (efx->type->mcdi_max_ver < 2 && |
| 613 | cmd > MC_CMD_CMD_SPACE_ESCAPE_7)) |
| 614 | return -EINVAL; |
| 615 | |
| 616 | if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 || |
| 617 | (efx->type->mcdi_max_ver < 2 && |
| 618 | inlen > MCDI_CTL_SDU_LEN_MAX_V1)) |
| 619 | return -EMSGSIZE; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 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) |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 627 | { |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 628 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Jon Cooper | aa09a3d | 2015-05-20 11:10:41 +0100 | [diff] [blame] | 629 | MCDI_DECLARE_BUF_ERR(errbuf); |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 630 | int rc; |
| 631 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 632 | if (mcdi->mode == MCDI_MODE_POLL) |
| 633 | rc = efx_mcdi_poll(efx); |
| 634 | else |
| 635 | rc = efx_mcdi_await_completion(efx); |
| 636 | |
| 637 | if (rc != 0) { |
Robert Stonehouse | 6b294b8 | 2013-10-09 11:52:48 +0100 | [diff] [blame] | 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); |
| 641 | |
| 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"); |
| 645 | rc = 0; |
| 646 | } |
| 647 | |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 648 | efx_mcdi_abandon(efx); |
| 649 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 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. |
| 653 | */ |
| 654 | spin_lock_bh(&mcdi->iface_lock); |
| 655 | ++mcdi->seqno; |
| 656 | ++mcdi->credits; |
| 657 | spin_unlock_bh(&mcdi->iface_lock); |
Robert Stonehouse | 6b294b8 | 2013-10-09 11:52:48 +0100 | [diff] [blame] | 658 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 659 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 660 | if (rc != 0) { |
| 661 | if (outlen_actual) |
| 662 | *outlen_actual = 0; |
| 663 | } else { |
| 664 | size_t hdr_len, data_len, err_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 665 | |
| 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); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 671 | rc = mcdi->resprc; |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 672 | hdr_len = mcdi->resp_hdr_len; |
| 673 | data_len = mcdi->resp_data_len; |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 674 | err_len = min(sizeof(errbuf), data_len); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 675 | spin_unlock_bh(&mcdi->iface_lock); |
| 676 | |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 677 | BUG_ON(rc > 0); |
| 678 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 679 | efx->type->mcdi_read_response(efx, outbuf, hdr_len, |
| 680 | min(outlen, data_len)); |
| 681 | if (outlen_actual) |
| 682 | *outlen_actual = data_len; |
| 683 | |
| 684 | efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len); |
| 685 | |
| 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) { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 689 | netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n", |
| 690 | -rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 691 | efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 692 | } else if (rc && !quiet) { |
| 693 | efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len, |
| 694 | rc); |
| 695 | } |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 696 | |
| 697 | if (rc == -EIO || rc == -EINTR) { |
| 698 | msleep(MCDI_STATUS_SLEEP_MS); |
| 699 | efx_mcdi_poll_reboot(efx); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 700 | mcdi->new_epoch = true; |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 701 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | efx_mcdi_release(mcdi); |
| 705 | return rc; |
| 706 | } |
| 707 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 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) |
| 712 | { |
| 713 | int rc; |
| 714 | |
| 715 | rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen); |
| 716 | if (rc) { |
| 717 | if (outlen_actual) |
| 718 | *outlen_actual = 0; |
| 719 | return rc; |
| 720 | } |
| 721 | return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen, |
| 722 | outlen_actual, quiet); |
| 723 | } |
| 724 | |
| 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) |
| 729 | { |
| 730 | return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen, |
| 731 | outlen_actual, false); |
| 732 | } |
| 733 | |
| 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 |
| 740 | * as needed. |
| 741 | */ |
| 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) |
| 746 | { |
| 747 | return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen, |
| 748 | outlen_actual, true); |
| 749 | } |
| 750 | |
| 751 | int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd, |
| 752 | const efx_dword_t *inbuf, size_t inlen) |
| 753 | { |
| 754 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 755 | int rc; |
| 756 | |
| 757 | rc = efx_mcdi_check_supported(efx, cmd, inlen); |
| 758 | if (rc) |
| 759 | return rc; |
| 760 | |
| 761 | if (efx->mc_bist_for_other_fn) |
| 762 | return -ENETDOWN; |
| 763 | |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 764 | if (mcdi->mode == MCDI_MODE_FAIL) |
| 765 | return -ENETDOWN; |
| 766 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 767 | efx_mcdi_acquire_sync(mcdi); |
| 768 | efx_mcdi_send_request(efx, cmd, inbuf, inlen); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd, |
| 773 | const efx_dword_t *inbuf, size_t inlen, |
| 774 | size_t outlen, |
| 775 | efx_mcdi_async_completer *complete, |
| 776 | unsigned long cookie, bool quiet) |
| 777 | { |
| 778 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 779 | struct efx_mcdi_async_param *async; |
| 780 | int rc; |
| 781 | |
| 782 | rc = efx_mcdi_check_supported(efx, cmd, inlen); |
| 783 | if (rc) |
| 784 | return rc; |
| 785 | |
| 786 | if (efx->mc_bist_for_other_fn) |
| 787 | return -ENETDOWN; |
| 788 | |
| 789 | async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4), |
| 790 | GFP_ATOMIC); |
| 791 | if (!async) |
| 792 | return -ENOMEM; |
| 793 | |
| 794 | async->cmd = cmd; |
| 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); |
| 801 | |
| 802 | spin_lock_bh(&mcdi->async_lock); |
| 803 | |
| 804 | if (mcdi->mode == MCDI_MODE_EVENTS) { |
| 805 | list_add_tail(&async->list, &mcdi->async_list); |
| 806 | |
| 807 | /* If this is at the front of the queue, try to start it |
| 808 | * immediately |
| 809 | */ |
| 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); |
| 815 | } |
| 816 | } else { |
| 817 | kfree(async); |
| 818 | rc = -ENETDOWN; |
| 819 | } |
| 820 | |
| 821 | spin_unlock_bh(&mcdi->async_lock); |
| 822 | |
| 823 | return rc; |
| 824 | } |
| 825 | |
| 826 | /** |
| 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. |
| 835 | * |
| 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. |
| 839 | * |
| 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) |
| 845 | */ |
| 846 | int |
| 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) |
| 850 | { |
| 851 | return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete, |
| 852 | cookie, false); |
| 853 | } |
| 854 | |
| 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) |
| 859 | { |
| 860 | return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete, |
| 861 | cookie, true); |
| 862 | } |
| 863 | |
| 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) |
| 867 | { |
| 868 | return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen, |
| 869 | outlen_actual, false); |
| 870 | } |
| 871 | |
| 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) |
| 875 | { |
| 876 | return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen, |
| 877 | outlen_actual, true); |
| 878 | } |
| 879 | |
| 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) |
| 883 | { |
| 884 | int code = 0, err_arg = 0; |
| 885 | |
| 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); |
| 893 | } |
| 894 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 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. |
| 898 | */ |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 899 | void efx_mcdi_mode_poll(struct efx_nic *efx) |
| 900 | { |
| 901 | struct efx_mcdi_iface *mcdi; |
| 902 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 903 | if (!efx->mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 904 | return; |
| 905 | |
| 906 | mcdi = efx_mcdi(efx); |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 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. |
| 910 | */ |
| 911 | if (mcdi->mode == MCDI_MODE_POLL || mcdi->mode == MCDI_MODE_FAIL) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 912 | return; |
| 913 | |
| 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(). |
| 918 | * |
| 919 | * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(), |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 920 | * which efx_mcdi_complete_sync() provides for us. |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 921 | */ |
| 922 | mcdi->mode = MCDI_MODE_POLL; |
| 923 | |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 924 | efx_mcdi_complete_sync(mcdi); |
| 925 | } |
| 926 | |
| 927 | /* Flush any running or queued asynchronous requests, after event processing |
| 928 | * is stopped |
| 929 | */ |
| 930 | void efx_mcdi_flush_async(struct efx_nic *efx) |
| 931 | { |
| 932 | struct efx_mcdi_async_param *async, *next; |
| 933 | struct efx_mcdi_iface *mcdi; |
| 934 | |
| 935 | if (!efx->mcdi) |
| 936 | return; |
| 937 | |
| 938 | mcdi = efx_mcdi(efx); |
| 939 | |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 940 | /* We must be in poll or fail mode so no more requests can be queued */ |
| 941 | BUG_ON(mcdi->mode == MCDI_MODE_EVENTS); |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 942 | |
| 943 | del_timer_sync(&mcdi->async_timer); |
| 944 | |
| 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 |
| 947 | * next request. |
| 948 | */ |
| 949 | if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) { |
| 950 | efx_mcdi_poll(efx); |
| 951 | mcdi->state = MCDI_STATE_QUIESCENT; |
| 952 | } |
| 953 | |
| 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. |
| 958 | */ |
| 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); |
| 962 | kfree(async); |
| 963 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | void efx_mcdi_mode_event(struct efx_nic *efx) |
| 967 | { |
| 968 | struct efx_mcdi_iface *mcdi; |
| 969 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 970 | if (!efx->mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 971 | return; |
| 972 | |
| 973 | mcdi = efx_mcdi(efx); |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 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. |
| 977 | */ |
| 978 | if (mcdi->mode == MCDI_MODE_EVENTS || mcdi->mode == MCDI_MODE_FAIL) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 979 | return; |
| 980 | |
| 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. |
| 987 | */ |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 988 | efx_mcdi_acquire_sync(mcdi); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 989 | mcdi->mode = MCDI_MODE_EVENTS; |
| 990 | efx_mcdi_release(mcdi); |
| 991 | } |
| 992 | |
| 993 | static void efx_mcdi_ev_death(struct efx_nic *efx, int rc) |
| 994 | { |
| 995 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 996 | |
| 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()] |
| 1004 | * |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 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. |
| 1008 | * |
| 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. |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1017 | */ |
| 1018 | spin_lock(&mcdi->iface_lock); |
Ben Hutchings | cade715 | 2013-08-27 23:12:31 +0100 | [diff] [blame] | 1019 | if (efx_mcdi_complete_sync(mcdi)) { |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1020 | if (mcdi->mode == MCDI_MODE_EVENTS) { |
| 1021 | mcdi->resprc = rc; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 1022 | mcdi->resp_hdr_len = 0; |
| 1023 | mcdi->resp_data_len = 0; |
Steve Hodgson | 18e3ee2 | 2010-12-02 13:46:55 +0000 | [diff] [blame] | 1024 | ++mcdi->credits; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1025 | } |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 1026 | } else { |
| 1027 | int count; |
| 1028 | |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 1029 | /* Consume the status word since efx_mcdi_rpc_finish() won't */ |
| 1030 | for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) { |
Daniel Pieczko | c577e59 | 2015-10-09 10:40:35 +0100 | [diff] [blame] | 1031 | rc = efx_mcdi_poll_reboot(efx); |
| 1032 | if (rc) |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | udelay(MCDI_STATUS_DELAY_US); |
| 1035 | } |
Daniel Pieczko | c577e59 | 2015-10-09 10:40:35 +0100 | [diff] [blame] | 1036 | |
| 1037 | /* On EF10, a CODE_MC_REBOOT event can be received without the |
| 1038 | * reboot detection in efx_mcdi_poll_reboot() being triggered. |
| 1039 | * If zero was returned from the final call to |
| 1040 | * efx_mcdi_poll_reboot(), the MC reboot wasn't noticed but the |
| 1041 | * MC has definitely rebooted so prepare for the reset. |
| 1042 | */ |
| 1043 | if (!rc && efx->type->mcdi_reboot_detected) |
| 1044 | efx->type->mcdi_reboot_detected(efx); |
| 1045 | |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 1046 | mcdi->new_epoch = true; |
Daniel Pieczko | dfdaa95 | 2013-09-18 10:16:24 +0100 | [diff] [blame] | 1047 | |
| 1048 | /* Nobody was waiting for an MCDI request, so trigger a reset */ |
| 1049 | efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE); |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1052 | spin_unlock(&mcdi->iface_lock); |
| 1053 | } |
| 1054 | |
Jon Cooper | 74cd60a | 2013-09-16 14:18:51 +0100 | [diff] [blame] | 1055 | /* The MC is going down in to BIST mode. set the BIST flag to block |
| 1056 | * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset |
| 1057 | * (which doesn't actually execute a reset, it waits for the controlling |
| 1058 | * function to reset it). |
| 1059 | */ |
| 1060 | static void efx_mcdi_ev_bist(struct efx_nic *efx) |
| 1061 | { |
| 1062 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 1063 | |
| 1064 | spin_lock(&mcdi->iface_lock); |
| 1065 | efx->mc_bist_for_other_fn = true; |
| 1066 | if (efx_mcdi_complete_sync(mcdi)) { |
| 1067 | if (mcdi->mode == MCDI_MODE_EVENTS) { |
| 1068 | mcdi->resprc = -EIO; |
| 1069 | mcdi->resp_hdr_len = 0; |
| 1070 | mcdi->resp_data_len = 0; |
| 1071 | ++mcdi->credits; |
| 1072 | } |
| 1073 | } |
| 1074 | mcdi->new_epoch = true; |
| 1075 | efx_schedule_reset(efx, RESET_TYPE_MC_BIST); |
| 1076 | spin_unlock(&mcdi->iface_lock); |
| 1077 | } |
| 1078 | |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 1079 | /* MCDI timeouts seen, so make all MCDI calls fail-fast and issue an FLR to try |
| 1080 | * to recover. |
| 1081 | */ |
| 1082 | static void efx_mcdi_abandon(struct efx_nic *efx) |
| 1083 | { |
| 1084 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 1085 | |
| 1086 | if (xchg(&mcdi->mode, MCDI_MODE_FAIL) == MCDI_MODE_FAIL) |
| 1087 | return; /* it had already been done */ |
| 1088 | netif_dbg(efx, hw, efx->net_dev, "MCDI is timing out; trying to recover\n"); |
| 1089 | efx_schedule_reset(efx, RESET_TYPE_MCDI_TIMEOUT); |
| 1090 | } |
| 1091 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1092 | /* Called from falcon_process_eventq for MCDI events */ |
| 1093 | void efx_mcdi_process_event(struct efx_channel *channel, |
| 1094 | efx_qword_t *event) |
| 1095 | { |
| 1096 | struct efx_nic *efx = channel->efx; |
| 1097 | int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE); |
| 1098 | u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA); |
| 1099 | |
| 1100 | switch (code) { |
| 1101 | case MCDI_EVENT_CODE_BADSSERT: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1102 | netif_err(efx, hw, efx->net_dev, |
| 1103 | "MC watchdog or assertion failure at 0x%x\n", data); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 1104 | efx_mcdi_ev_death(efx, -EINTR); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | |
| 1107 | case MCDI_EVENT_CODE_PMNOTICE: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1108 | netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n"); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | |
| 1111 | case MCDI_EVENT_CODE_CMDDONE: |
| 1112 | efx_mcdi_ev_cpl(efx, |
| 1113 | MCDI_EVENT_FIELD(*event, CMDDONE_SEQ), |
| 1114 | MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN), |
| 1115 | MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO)); |
| 1116 | break; |
| 1117 | |
| 1118 | case MCDI_EVENT_CODE_LINKCHANGE: |
| 1119 | efx_mcdi_process_link_change(efx, event); |
| 1120 | break; |
| 1121 | case MCDI_EVENT_CODE_SENSOREVT: |
| 1122 | efx_mcdi_sensor_event(efx, event); |
| 1123 | break; |
| 1124 | case MCDI_EVENT_CODE_SCHEDERR: |
Robert Stonehouse | 2d9955b | 2013-10-07 18:44:17 +0100 | [diff] [blame] | 1125 | netif_dbg(efx, hw, efx->net_dev, |
| 1126 | "MC Scheduler alert (0x%x)\n", data); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | case MCDI_EVENT_CODE_REBOOT: |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1129 | case MCDI_EVENT_CODE_MC_REBOOT: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1130 | netif_info(efx, hw, efx->net_dev, "MC Reboot\n"); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 1131 | efx_mcdi_ev_death(efx, -EIO); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1132 | break; |
Jon Cooper | 74cd60a | 2013-09-16 14:18:51 +0100 | [diff] [blame] | 1133 | case MCDI_EVENT_CODE_MC_BIST: |
| 1134 | netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n"); |
| 1135 | efx_mcdi_ev_bist(efx); |
| 1136 | break; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1137 | case MCDI_EVENT_CODE_MAC_STATS_DMA: |
| 1138 | /* MAC stats are gather lazily. We can ignore this. */ |
| 1139 | break; |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1140 | case MCDI_EVENT_CODE_FLR: |
Shradha Shah | 7fa8d54 | 2015-05-06 00:55:13 +0100 | [diff] [blame] | 1141 | if (efx->type->sriov_flr) |
| 1142 | efx->type->sriov_flr(efx, |
| 1143 | MCDI_EVENT_FIELD(*event, FLR_VF)); |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1144 | break; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1145 | case MCDI_EVENT_CODE_PTP_RX: |
| 1146 | case MCDI_EVENT_CODE_PTP_FAULT: |
| 1147 | case MCDI_EVENT_CODE_PTP_PPS: |
| 1148 | efx_ptp_event(efx, event); |
| 1149 | break; |
Jon Cooper | bd9a265 | 2013-11-18 12:54:41 +0000 | [diff] [blame] | 1150 | case MCDI_EVENT_CODE_PTP_TIME: |
| 1151 | efx_time_sync_event(channel, event); |
| 1152 | break; |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1153 | case MCDI_EVENT_CODE_TX_FLUSH: |
| 1154 | case MCDI_EVENT_CODE_RX_FLUSH: |
| 1155 | /* Two flush events will be sent: one to the same event |
| 1156 | * queue as completions, and one to event queue 0. |
| 1157 | * In the latter case the {RX,TX}_FLUSH_TO_DRIVER |
| 1158 | * flag will be set, and we should ignore the event |
| 1159 | * because we want to wait for all completions. |
| 1160 | */ |
| 1161 | BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN != |
| 1162 | MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN); |
| 1163 | if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER)) |
| 1164 | efx_ef10_handle_drain_event(efx); |
| 1165 | break; |
Alexandre Rames | 3de82b9 | 2013-06-13 11:36:15 +0100 | [diff] [blame] | 1166 | case MCDI_EVENT_CODE_TX_ERR: |
| 1167 | case MCDI_EVENT_CODE_RX_ERR: |
| 1168 | netif_err(efx, hw, efx->net_dev, |
| 1169 | "%s DMA error (event: "EFX_QWORD_FMT")\n", |
| 1170 | code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX", |
| 1171 | EFX_QWORD_VAL(*event)); |
| 1172 | efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR); |
| 1173 | break; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1174 | default: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1175 | netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n", |
| 1176 | code); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /************************************************************************** |
| 1181 | * |
| 1182 | * Specific request functions |
| 1183 | * |
| 1184 | ************************************************************************** |
| 1185 | */ |
| 1186 | |
Ben Hutchings | e5f0fd2 | 2011-02-24 23:57:47 +0000 | [diff] [blame] | 1187 | void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1188 | { |
Daniel Pieczko | 8d9f9dd | 2015-05-06 00:56:55 +0100 | [diff] [blame] | 1189 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1190 | size_t outlength; |
| 1191 | const __le16 *ver_words; |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1192 | size_t offset; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1193 | int rc; |
| 1194 | |
| 1195 | BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1196 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0, |
| 1197 | outbuf, sizeof(outbuf), &outlength); |
| 1198 | if (rc) |
| 1199 | goto fail; |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 1200 | if (outlength < MC_CMD_GET_VERSION_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1201 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1202 | goto fail; |
| 1203 | } |
| 1204 | |
| 1205 | ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION); |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1206 | offset = snprintf(buf, len, "%u.%u.%u.%u", |
| 1207 | le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]), |
| 1208 | le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3])); |
| 1209 | |
| 1210 | /* EF10 may have multiple datapath firmware variants within a |
| 1211 | * single version. Report which variants are running. |
| 1212 | */ |
| 1213 | if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) { |
Daniel Pieczko | 8d9f9dd | 2015-05-06 00:56:55 +0100 | [diff] [blame] | 1214 | struct efx_ef10_nic_data *nic_data = efx->nic_data; |
| 1215 | |
| 1216 | offset += snprintf(buf + offset, len - offset, " rx%x tx%x", |
| 1217 | nic_data->rx_dpcpu_fw_id, |
| 1218 | nic_data->tx_dpcpu_fw_id); |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* It's theoretically possible for the string to exceed 31 |
| 1221 | * characters, though in practice the first three version |
| 1222 | * components are short enough that this doesn't happen. |
| 1223 | */ |
| 1224 | if (WARN_ON(offset >= len)) |
| 1225 | buf[0] = 0; |
| 1226 | } |
| 1227 | |
Ben Hutchings | e5f0fd2 | 2011-02-24 23:57:47 +0000 | [diff] [blame] | 1228 | return; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1229 | |
| 1230 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1231 | netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | e5f0fd2 | 2011-02-24 23:57:47 +0000 | [diff] [blame] | 1232 | buf[0] = 0; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Ben Hutchings | 4c75b43a | 2013-08-29 19:04:03 +0100 | [diff] [blame] | 1235 | static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating, |
| 1236 | bool *was_attached) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1237 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1238 | MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN); |
Ben Hutchings | ecb1c9c | 2013-10-07 20:10:11 +0100 | [diff] [blame] | 1239 | MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1240 | size_t outlen; |
| 1241 | int rc; |
| 1242 | |
| 1243 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE, |
| 1244 | driver_operating ? 1 : 0); |
| 1245 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1); |
Ben Hutchings | f2b0bef | 2013-08-20 20:35:50 +0100 | [diff] [blame] | 1246 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1247 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1248 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf), |
| 1249 | outbuf, sizeof(outbuf), &outlen); |
| 1250 | /* If we're not the primary PF, trying to ATTACH with a FIRMWARE_ID |
| 1251 | * specified will fail with EPERM, and we have to tell the MC we don't |
| 1252 | * care what firmware we get. |
| 1253 | */ |
| 1254 | if (rc == -EPERM) { |
| 1255 | netif_dbg(efx, probe, efx->net_dev, |
| 1256 | "efx_mcdi_drv_attach with fw-variant setting failed EPERM, trying without it\n"); |
| 1257 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, |
| 1258 | MC_CMD_FW_DONT_CARE); |
| 1259 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf, |
| 1260 | sizeof(inbuf), outbuf, sizeof(outbuf), |
| 1261 | &outlen); |
| 1262 | } |
| 1263 | if (rc) { |
| 1264 | efx_mcdi_display_error(efx, MC_CMD_DRV_ATTACH, sizeof(inbuf), |
| 1265 | outbuf, outlen, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1266 | goto fail; |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1267 | } |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1268 | if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) { |
| 1269 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1270 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1271 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1272 | |
Ben Hutchings | 8349f7f | 2013-10-16 18:32:34 +0100 | [diff] [blame] | 1273 | if (driver_operating) { |
| 1274 | if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) { |
| 1275 | efx->mcdi->fn_flags = |
| 1276 | MCDI_DWORD(outbuf, |
| 1277 | DRV_ATTACH_EXT_OUT_FUNC_FLAGS); |
| 1278 | } else { |
| 1279 | /* Synthesise flags for Siena */ |
| 1280 | efx->mcdi->fn_flags = |
| 1281 | 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL | |
| 1282 | 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED | |
| 1283 | (efx_port_num(efx) == 0) << |
| 1284 | MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY; |
| 1285 | } |
| 1286 | } |
| 1287 | |
Ben Hutchings | ecb1c9c | 2013-10-07 20:10:11 +0100 | [diff] [blame] | 1288 | /* We currently assume we have control of the external link |
| 1289 | * and are completely trusted by firmware. Abort probing |
| 1290 | * if that's not true for this function. |
| 1291 | */ |
Ben Hutchings | ecb1c9c | 2013-10-07 20:10:11 +0100 | [diff] [blame] | 1292 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1293 | if (was_attached != NULL) |
| 1294 | *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE); |
| 1295 | return 0; |
| 1296 | |
| 1297 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1298 | netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1299 | return rc; |
| 1300 | } |
| 1301 | |
| 1302 | int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address, |
Matthew Slattery | 6aa9c7f | 2010-07-14 15:36:19 +0100 | [diff] [blame] | 1303 | u16 *fw_subtype_list, u32 *capabilities) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1304 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1305 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1306 | size_t outlen, i; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1307 | int port_num = efx_port_num(efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1308 | int rc; |
| 1309 | |
| 1310 | BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0); |
Edward Cree | cd84ff4 | 2014-03-07 18:27:41 +0000 | [diff] [blame] | 1311 | /* we need __aligned(2) for ether_addr_copy */ |
| 1312 | BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1); |
| 1313 | BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1314 | |
| 1315 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0, |
| 1316 | outbuf, sizeof(outbuf), &outlen); |
| 1317 | if (rc) |
| 1318 | goto fail; |
| 1319 | |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 1320 | if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1321 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1322 | goto fail; |
| 1323 | } |
| 1324 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1325 | if (mac_address) |
Edward Cree | cd84ff4 | 2014-03-07 18:27:41 +0000 | [diff] [blame] | 1326 | ether_addr_copy(mac_address, |
| 1327 | port_num ? |
| 1328 | MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) : |
| 1329 | MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0)); |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 1330 | if (fw_subtype_list) { |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 1331 | for (i = 0; |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1332 | i < MCDI_VAR_ARRAY_LEN(outlen, |
| 1333 | GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST); |
| 1334 | i++) |
| 1335 | fw_subtype_list[i] = MCDI_ARRAY_WORD( |
| 1336 | outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i); |
| 1337 | for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++) |
| 1338 | fw_subtype_list[i] = 0; |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 1339 | } |
Matthew Slattery | 6aa9c7f | 2010-07-14 15:36:19 +0100 | [diff] [blame] | 1340 | if (capabilities) { |
| 1341 | if (port_num) |
| 1342 | *capabilities = MCDI_DWORD(outbuf, |
| 1343 | GET_BOARD_CFG_OUT_CAPABILITIES_PORT1); |
| 1344 | else |
| 1345 | *capabilities = MCDI_DWORD(outbuf, |
| 1346 | GET_BOARD_CFG_OUT_CAPABILITIES_PORT0); |
| 1347 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1348 | |
| 1349 | return 0; |
| 1350 | |
| 1351 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1352 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n", |
| 1353 | __func__, rc, (int)outlen); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1354 | |
| 1355 | return rc; |
| 1356 | } |
| 1357 | |
| 1358 | int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq) |
| 1359 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1360 | MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1361 | u32 dest = 0; |
| 1362 | int rc; |
| 1363 | |
| 1364 | if (uart) |
| 1365 | dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART; |
| 1366 | if (evq) |
| 1367 | dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ; |
| 1368 | |
| 1369 | MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest); |
| 1370 | MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq); |
| 1371 | |
| 1372 | BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0); |
| 1373 | |
| 1374 | rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf), |
| 1375 | NULL, 0, NULL); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1376 | return rc; |
| 1377 | } |
| 1378 | |
| 1379 | int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out) |
| 1380 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1381 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1382 | size_t outlen; |
| 1383 | int rc; |
| 1384 | |
| 1385 | BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0); |
| 1386 | |
| 1387 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0, |
| 1388 | outbuf, sizeof(outbuf), &outlen); |
| 1389 | if (rc) |
| 1390 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1391 | if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) { |
| 1392 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1393 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1394 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1395 | |
| 1396 | *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES); |
| 1397 | return 0; |
| 1398 | |
| 1399 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1400 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
| 1401 | __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1402 | return rc; |
| 1403 | } |
| 1404 | |
| 1405 | int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type, |
| 1406 | size_t *size_out, size_t *erase_size_out, |
| 1407 | bool *protected_out) |
| 1408 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1409 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN); |
| 1410 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1411 | size_t outlen; |
| 1412 | int rc; |
| 1413 | |
| 1414 | MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type); |
| 1415 | |
| 1416 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf), |
| 1417 | outbuf, sizeof(outbuf), &outlen); |
| 1418 | if (rc) |
| 1419 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1420 | if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) { |
| 1421 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1422 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1423 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1424 | |
| 1425 | *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE); |
| 1426 | *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE); |
| 1427 | *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) & |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 1428 | (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1429 | return 0; |
| 1430 | |
| 1431 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1432 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1433 | return rc; |
| 1434 | } |
| 1435 | |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 1436 | static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type) |
| 1437 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1438 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN); |
| 1439 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN); |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 1440 | int rc; |
| 1441 | |
| 1442 | MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type); |
| 1443 | |
| 1444 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf), |
| 1445 | outbuf, sizeof(outbuf), NULL); |
| 1446 | if (rc) |
| 1447 | return rc; |
| 1448 | |
| 1449 | switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) { |
| 1450 | case MC_CMD_NVRAM_TEST_PASS: |
| 1451 | case MC_CMD_NVRAM_TEST_NOTSUPP: |
| 1452 | return 0; |
| 1453 | default: |
| 1454 | return -EIO; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | int efx_mcdi_nvram_test_all(struct efx_nic *efx) |
| 1459 | { |
| 1460 | u32 nvram_types; |
| 1461 | unsigned int type; |
| 1462 | int rc; |
| 1463 | |
| 1464 | rc = efx_mcdi_nvram_types(efx, &nvram_types); |
| 1465 | if (rc) |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 1466 | goto fail1; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 1467 | |
| 1468 | type = 0; |
| 1469 | while (nvram_types != 0) { |
| 1470 | if (nvram_types & 1) { |
| 1471 | rc = efx_mcdi_nvram_test(efx, type); |
| 1472 | if (rc) |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 1473 | goto fail2; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 1474 | } |
| 1475 | type++; |
| 1476 | nvram_types >>= 1; |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 1480 | |
| 1481 | fail2: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1482 | netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n", |
| 1483 | __func__, type); |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 1484 | fail1: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1485 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 1486 | return rc; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1489 | /* Returns 1 if an assertion was read, 0 if no assertion had fired, |
| 1490 | * negative on error. |
| 1491 | */ |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1492 | static int efx_mcdi_read_assertion(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1493 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1494 | MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN); |
Jon Cooper | aa09a3d | 2015-05-20 11:10:41 +0100 | [diff] [blame] | 1495 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1496 | unsigned int flags, index; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1497 | const char *reason; |
| 1498 | size_t outlen; |
| 1499 | int retry; |
| 1500 | int rc; |
| 1501 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1502 | /* Attempt to read any stored assertion state before we reboot |
| 1503 | * the mcfw out of the assertion handler. Retry twice, once |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1504 | * because a boot-time assertion might cause this command to fail |
| 1505 | * with EINTR. And once again because GET_ASSERTS can race with |
| 1506 | * MC_CMD_REBOOT running on the other port. */ |
| 1507 | retry = 2; |
| 1508 | do { |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1509 | MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 1510 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS, |
| 1511 | inbuf, MC_CMD_GET_ASSERTS_IN_LEN, |
| 1512 | outbuf, sizeof(outbuf), &outlen); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1513 | if (rc == -EPERM) |
| 1514 | return 0; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1515 | } while ((rc == -EINTR || rc == -EIO) && retry-- > 0); |
| 1516 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 1517 | if (rc) { |
| 1518 | efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS, |
| 1519 | MC_CMD_GET_ASSERTS_IN_LEN, outbuf, |
| 1520 | outlen, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1521 | return rc; |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 1522 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1523 | if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN) |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1524 | return -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1525 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1526 | /* Print out any recorded assertion state */ |
| 1527 | flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1528 | if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS) |
| 1529 | return 0; |
| 1530 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1531 | reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL) |
| 1532 | ? "system-level assertion" |
| 1533 | : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL) |
| 1534 | ? "thread-level assertion" |
| 1535 | : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED) |
| 1536 | ? "watchdog reset" |
| 1537 | : "unknown assertion"; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1538 | netif_err(efx, hw, efx->net_dev, |
| 1539 | "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason, |
| 1540 | MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS), |
| 1541 | MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* Print out the registers */ |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1544 | for (index = 0; |
| 1545 | index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM; |
| 1546 | index++) |
| 1547 | netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n", |
| 1548 | 1 + index, |
| 1549 | MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS, |
| 1550 | index)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1551 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1552 | return 1; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1555 | static int efx_mcdi_exit_assertion(struct efx_nic *efx) |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1556 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1557 | MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1558 | int rc; |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1559 | |
Ben Hutchings | 0f1e54a | 2012-07-02 23:37:40 +0100 | [diff] [blame] | 1560 | /* If the MC is running debug firmware, it might now be |
| 1561 | * waiting for a debugger to attach, but we just want it to |
| 1562 | * reboot. We set a flag that makes the command a no-op if it |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1563 | * has already done so. |
| 1564 | * The MCDI will thus return either 0 or -EIO. |
Ben Hutchings | 0f1e54a | 2012-07-02 23:37:40 +0100 | [diff] [blame] | 1565 | */ |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1566 | BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0); |
| 1567 | MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, |
| 1568 | MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1569 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN, |
| 1570 | NULL, 0, NULL); |
| 1571 | if (rc == -EIO) |
| 1572 | rc = 0; |
| 1573 | if (rc) |
| 1574 | efx_mcdi_display_error(efx, MC_CMD_REBOOT, MC_CMD_REBOOT_IN_LEN, |
| 1575 | NULL, 0, rc); |
| 1576 | return rc; |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | int efx_mcdi_handle_assertion(struct efx_nic *efx) |
| 1580 | { |
| 1581 | int rc; |
| 1582 | |
| 1583 | rc = efx_mcdi_read_assertion(efx); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1584 | if (rc <= 0) |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1585 | return rc; |
| 1586 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1587 | return efx_mcdi_exit_assertion(efx); |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1590 | void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode) |
| 1591 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1592 | MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1593 | int rc; |
| 1594 | |
| 1595 | BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF); |
| 1596 | BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON); |
| 1597 | BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT); |
| 1598 | |
| 1599 | BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0); |
| 1600 | |
| 1601 | MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode); |
| 1602 | |
| 1603 | rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf), |
| 1604 | NULL, 0, NULL); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Jon Cooper | 3e33626 | 2014-01-17 19:48:06 +0000 | [diff] [blame] | 1607 | static int efx_mcdi_reset_func(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1608 | { |
Jon Cooper | 3e33626 | 2014-01-17 19:48:06 +0000 | [diff] [blame] | 1609 | MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN); |
| 1610 | int rc; |
| 1611 | |
| 1612 | BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0); |
| 1613 | MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG, |
| 1614 | ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1); |
| 1615 | rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf), |
| 1616 | NULL, 0, NULL); |
| 1617 | return rc; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1620 | static int efx_mcdi_reset_mc(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1621 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1622 | MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1623 | int rc; |
| 1624 | |
| 1625 | BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0); |
| 1626 | MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0); |
| 1627 | rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf), |
| 1628 | NULL, 0, NULL); |
| 1629 | /* White is black, and up is down */ |
| 1630 | if (rc == -EIO) |
| 1631 | return 0; |
| 1632 | if (rc == 0) |
| 1633 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1634 | return rc; |
| 1635 | } |
| 1636 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1637 | enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason) |
| 1638 | { |
| 1639 | return RESET_TYPE_RECOVER_OR_ALL; |
| 1640 | } |
| 1641 | |
| 1642 | int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method) |
| 1643 | { |
| 1644 | int rc; |
| 1645 | |
Edward Cree | e283546 | 2014-04-16 19:27:48 +0100 | [diff] [blame] | 1646 | /* If MCDI is down, we can't handle_assertion */ |
| 1647 | if (method == RESET_TYPE_MCDI_TIMEOUT) { |
| 1648 | rc = pci_reset_function(efx->pci_dev); |
| 1649 | if (rc) |
| 1650 | return rc; |
| 1651 | /* Re-enable polled MCDI completion */ |
| 1652 | if (efx->mcdi) { |
| 1653 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 1654 | mcdi->mode = MCDI_MODE_POLL; |
| 1655 | } |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1659 | /* Recover from a failed assertion pre-reset */ |
| 1660 | rc = efx_mcdi_handle_assertion(efx); |
| 1661 | if (rc) |
| 1662 | return rc; |
| 1663 | |
Jon Cooper | 087e902 | 2015-05-20 11:11:35 +0100 | [diff] [blame] | 1664 | if (method == RESET_TYPE_DATAPATH) |
| 1665 | return 0; |
| 1666 | else if (method == RESET_TYPE_WORLD) |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1667 | return efx_mcdi_reset_mc(efx); |
| 1668 | else |
Jon Cooper | 3e33626 | 2014-01-17 19:48:06 +0000 | [diff] [blame] | 1669 | return efx_mcdi_reset_func(efx); |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
stephen hemminger | d215697 | 2010-10-18 05:27:31 +0000 | [diff] [blame] | 1672 | static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type, |
| 1673 | const u8 *mac, int *id_out) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1674 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1675 | MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN); |
| 1676 | MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1677 | size_t outlen; |
| 1678 | int rc; |
| 1679 | |
| 1680 | MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type); |
| 1681 | MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE, |
| 1682 | MC_CMD_FILTER_MODE_SIMPLE); |
Edward Cree | cd84ff4 | 2014-03-07 18:27:41 +0000 | [diff] [blame] | 1683 | ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1684 | |
| 1685 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf), |
| 1686 | outbuf, sizeof(outbuf), &outlen); |
| 1687 | if (rc) |
| 1688 | goto fail; |
| 1689 | |
| 1690 | if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1691 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1692 | goto fail; |
| 1693 | } |
| 1694 | |
| 1695 | *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID); |
| 1696 | |
| 1697 | return 0; |
| 1698 | |
| 1699 | fail: |
| 1700 | *id_out = -1; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1701 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1702 | return rc; |
| 1703 | |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | int |
| 1708 | efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out) |
| 1709 | { |
| 1710 | return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out); |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out) |
| 1715 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1716 | MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1717 | size_t outlen; |
| 1718 | int rc; |
| 1719 | |
| 1720 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0, |
| 1721 | outbuf, sizeof(outbuf), &outlen); |
| 1722 | if (rc) |
| 1723 | goto fail; |
| 1724 | |
| 1725 | if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1726 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1727 | goto fail; |
| 1728 | } |
| 1729 | |
| 1730 | *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID); |
| 1731 | |
| 1732 | return 0; |
| 1733 | |
| 1734 | fail: |
| 1735 | *id_out = -1; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1736 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1737 | return rc; |
| 1738 | } |
| 1739 | |
| 1740 | |
| 1741 | int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id) |
| 1742 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1743 | MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1744 | int rc; |
| 1745 | |
| 1746 | MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id); |
| 1747 | |
| 1748 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf), |
| 1749 | NULL, 0, NULL); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1750 | return rc; |
| 1751 | } |
| 1752 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1753 | int efx_mcdi_flush_rxqs(struct efx_nic *efx) |
| 1754 | { |
| 1755 | struct efx_channel *channel; |
| 1756 | struct efx_rx_queue *rx_queue; |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1757 | MCDI_DECLARE_BUF(inbuf, |
| 1758 | MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS)); |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1759 | int rc, count; |
| 1760 | |
Ben Hutchings | 4507837 | 2012-09-19 02:53:34 +0100 | [diff] [blame] | 1761 | BUILD_BUG_ON(EFX_MAX_CHANNELS > |
| 1762 | MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM); |
| 1763 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1764 | count = 0; |
| 1765 | efx_for_each_channel(channel, efx) { |
| 1766 | efx_for_each_channel_rx_queue(rx_queue, channel) { |
| 1767 | if (rx_queue->flush_pending) { |
| 1768 | rx_queue->flush_pending = false; |
| 1769 | atomic_dec(&efx->rxq_flush_pending); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1770 | MCDI_SET_ARRAY_DWORD( |
| 1771 | inbuf, FLUSH_RX_QUEUES_IN_QID_OFST, |
| 1772 | count, efx_rx_queue_index(rx_queue)); |
| 1773 | count++; |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1778 | rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf, |
| 1779 | MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL); |
Ben Hutchings | bbec969 | 2012-09-11 18:25:13 +0100 | [diff] [blame] | 1780 | WARN_ON(rc < 0); |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1781 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1782 | return rc; |
| 1783 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1784 | |
| 1785 | int efx_mcdi_wol_filter_reset(struct efx_nic *efx) |
| 1786 | { |
| 1787 | int rc; |
| 1788 | |
| 1789 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1790 | return rc; |
| 1791 | } |
| 1792 | |
Daniel Pieczko | 34ccfe6 | 2015-07-21 15:09:43 +0100 | [diff] [blame] | 1793 | int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled, |
| 1794 | unsigned int *flags) |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1795 | { |
| 1796 | MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN); |
Daniel Pieczko | 34ccfe6 | 2015-07-21 15:09:43 +0100 | [diff] [blame] | 1797 | MCDI_DECLARE_BUF(outbuf, MC_CMD_WORKAROUND_EXT_OUT_LEN); |
| 1798 | size_t outlen; |
| 1799 | int rc; |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1800 | |
| 1801 | BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0); |
| 1802 | MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type); |
| 1803 | MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled); |
Daniel Pieczko | 34ccfe6 | 2015-07-21 15:09:43 +0100 | [diff] [blame] | 1804 | rc = efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf), |
| 1805 | outbuf, sizeof(outbuf), &outlen); |
| 1806 | if (rc) |
| 1807 | return rc; |
| 1808 | |
| 1809 | if (!flags) |
| 1810 | return 0; |
| 1811 | |
| 1812 | if (outlen >= MC_CMD_WORKAROUND_EXT_OUT_LEN) |
| 1813 | *flags = MCDI_DWORD(outbuf, WORKAROUND_EXT_OUT_FLAGS); |
| 1814 | else |
| 1815 | *flags = 0; |
| 1816 | |
| 1817 | return 0; |
Ben Hutchings | 8127d66 | 2013-08-29 19:19:29 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1820 | int efx_mcdi_get_workarounds(struct efx_nic *efx, unsigned int *impl_out, |
| 1821 | unsigned int *enabled_out) |
| 1822 | { |
Jon Cooper | aa09a3d | 2015-05-20 11:10:41 +0100 | [diff] [blame] | 1823 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_WORKAROUNDS_OUT_LEN); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1824 | size_t outlen; |
| 1825 | int rc; |
| 1826 | |
| 1827 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_WORKAROUNDS, NULL, 0, |
| 1828 | outbuf, sizeof(outbuf), &outlen); |
| 1829 | if (rc) |
| 1830 | goto fail; |
| 1831 | |
| 1832 | if (outlen < MC_CMD_GET_WORKAROUNDS_OUT_LEN) { |
| 1833 | rc = -EIO; |
| 1834 | goto fail; |
| 1835 | } |
| 1836 | |
| 1837 | if (impl_out) |
| 1838 | *impl_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_IMPLEMENTED); |
| 1839 | |
| 1840 | if (enabled_out) |
| 1841 | *enabled_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_ENABLED); |
| 1842 | |
| 1843 | return 0; |
| 1844 | |
| 1845 | fail: |
Edward Cree | 832dc9e | 2015-07-21 15:09:31 +0100 | [diff] [blame] | 1846 | /* Older firmware lacks GET_WORKAROUNDS and this isn't especially |
| 1847 | * terrifying. The call site will have to deal with it though. |
| 1848 | */ |
| 1849 | netif_printk(efx, hw, rc == -ENOSYS ? KERN_DEBUG : KERN_ERR, |
| 1850 | efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
Edward Cree | 267d9d7 | 2015-05-06 00:59:18 +0100 | [diff] [blame] | 1851 | return rc; |
| 1852 | } |
| 1853 | |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1854 | #ifdef CONFIG_SFC_MTD |
| 1855 | |
| 1856 | #define EFX_MCDI_NVRAM_LEN_MAX 128 |
| 1857 | |
| 1858 | static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type) |
| 1859 | { |
| 1860 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN); |
| 1861 | int rc; |
| 1862 | |
| 1863 | MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type); |
| 1864 | |
| 1865 | BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0); |
| 1866 | |
| 1867 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf), |
| 1868 | NULL, 0, NULL); |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1869 | return rc; |
| 1870 | } |
| 1871 | |
| 1872 | static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type, |
| 1873 | loff_t offset, u8 *buffer, size_t length) |
| 1874 | { |
| 1875 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN); |
| 1876 | MCDI_DECLARE_BUF(outbuf, |
| 1877 | MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX)); |
| 1878 | size_t outlen; |
| 1879 | int rc; |
| 1880 | |
| 1881 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type); |
| 1882 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset); |
| 1883 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length); |
| 1884 | |
| 1885 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf), |
| 1886 | outbuf, sizeof(outbuf), &outlen); |
| 1887 | if (rc) |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 1888 | return rc; |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1889 | |
| 1890 | memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length); |
| 1891 | return 0; |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type, |
| 1895 | loff_t offset, const u8 *buffer, size_t length) |
| 1896 | { |
| 1897 | MCDI_DECLARE_BUF(inbuf, |
| 1898 | MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX)); |
| 1899 | int rc; |
| 1900 | |
| 1901 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type); |
| 1902 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset); |
| 1903 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length); |
| 1904 | memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length); |
| 1905 | |
| 1906 | BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0); |
| 1907 | |
| 1908 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf, |
| 1909 | ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4), |
| 1910 | NULL, 0, NULL); |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1911 | return rc; |
| 1912 | } |
| 1913 | |
| 1914 | static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type, |
| 1915 | loff_t offset, size_t length) |
| 1916 | { |
| 1917 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN); |
| 1918 | int rc; |
| 1919 | |
| 1920 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type); |
| 1921 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset); |
| 1922 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length); |
| 1923 | |
| 1924 | BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0); |
| 1925 | |
| 1926 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf), |
| 1927 | NULL, 0, NULL); |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1928 | return rc; |
| 1929 | } |
| 1930 | |
| 1931 | static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type) |
| 1932 | { |
| 1933 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN); |
| 1934 | int rc; |
| 1935 | |
| 1936 | MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type); |
| 1937 | |
| 1938 | BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0); |
| 1939 | |
| 1940 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf), |
| 1941 | NULL, 0, NULL); |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1942 | return rc; |
| 1943 | } |
| 1944 | |
| 1945 | int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start, |
| 1946 | size_t len, size_t *retlen, u8 *buffer) |
| 1947 | { |
| 1948 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1949 | struct efx_nic *efx = mtd->priv; |
| 1950 | loff_t offset = start; |
| 1951 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 1952 | size_t chunk; |
| 1953 | int rc = 0; |
| 1954 | |
| 1955 | while (offset < end) { |
| 1956 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
| 1957 | rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset, |
| 1958 | buffer, chunk); |
| 1959 | if (rc) |
| 1960 | goto out; |
| 1961 | offset += chunk; |
| 1962 | buffer += chunk; |
| 1963 | } |
| 1964 | out: |
| 1965 | *retlen = offset - start; |
| 1966 | return rc; |
| 1967 | } |
| 1968 | |
| 1969 | int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len) |
| 1970 | { |
| 1971 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1972 | struct efx_nic *efx = mtd->priv; |
| 1973 | loff_t offset = start & ~((loff_t)(mtd->erasesize - 1)); |
| 1974 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 1975 | size_t chunk = part->common.mtd.erasesize; |
| 1976 | int rc = 0; |
| 1977 | |
| 1978 | if (!part->updating) { |
| 1979 | rc = efx_mcdi_nvram_update_start(efx, part->nvram_type); |
| 1980 | if (rc) |
| 1981 | goto out; |
| 1982 | part->updating = true; |
| 1983 | } |
| 1984 | |
| 1985 | /* The MCDI interface can in fact do multiple erase blocks at once; |
| 1986 | * but erasing may be slow, so we make multiple calls here to avoid |
| 1987 | * tripping the MCDI RPC timeout. */ |
| 1988 | while (offset < end) { |
| 1989 | rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset, |
| 1990 | chunk); |
| 1991 | if (rc) |
| 1992 | goto out; |
| 1993 | offset += chunk; |
| 1994 | } |
| 1995 | out: |
| 1996 | return rc; |
| 1997 | } |
| 1998 | |
| 1999 | int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start, |
| 2000 | size_t len, size_t *retlen, const u8 *buffer) |
| 2001 | { |
| 2002 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 2003 | struct efx_nic *efx = mtd->priv; |
| 2004 | loff_t offset = start; |
| 2005 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 2006 | size_t chunk; |
| 2007 | int rc = 0; |
| 2008 | |
| 2009 | if (!part->updating) { |
| 2010 | rc = efx_mcdi_nvram_update_start(efx, part->nvram_type); |
| 2011 | if (rc) |
| 2012 | goto out; |
| 2013 | part->updating = true; |
| 2014 | } |
| 2015 | |
| 2016 | while (offset < end) { |
| 2017 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
| 2018 | rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset, |
| 2019 | buffer, chunk); |
| 2020 | if (rc) |
| 2021 | goto out; |
| 2022 | offset += chunk; |
| 2023 | buffer += chunk; |
| 2024 | } |
| 2025 | out: |
| 2026 | *retlen = offset - start; |
| 2027 | return rc; |
| 2028 | } |
| 2029 | |
| 2030 | int efx_mcdi_mtd_sync(struct mtd_info *mtd) |
| 2031 | { |
| 2032 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 2033 | struct efx_nic *efx = mtd->priv; |
| 2034 | int rc = 0; |
| 2035 | |
| 2036 | if (part->updating) { |
| 2037 | part->updating = false; |
| 2038 | rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type); |
| 2039 | } |
| 2040 | |
| 2041 | return rc; |
| 2042 | } |
| 2043 | |
| 2044 | void efx_mcdi_mtd_rename(struct efx_mtd_partition *part) |
| 2045 | { |
| 2046 | struct efx_mcdi_mtd_partition *mcdi_part = |
| 2047 | container_of(part, struct efx_mcdi_mtd_partition, common); |
| 2048 | struct efx_nic *efx = part->mtd.priv; |
| 2049 | |
| 2050 | snprintf(part->name, sizeof(part->name), "%s %s:%02x", |
| 2051 | efx->name, part->type_name, mcdi_part->fw_subtype); |
| 2052 | } |
| 2053 | |
| 2054 | #endif /* CONFIG_SFC_MTD */ |