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