Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
Ben Hutchings | 0a6f40c | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3 | * Copyright 2008-2011 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> |
| 11 | #include "net_driver.h" |
| 12 | #include "nic.h" |
| 13 | #include "io.h" |
Ben Hutchings | 8b8a95a | 2012-09-18 01:57:07 +0100 | [diff] [blame] | 14 | #include "farch_regs.h" |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 15 | #include "mcdi_pcol.h" |
| 16 | #include "phy.h" |
| 17 | |
| 18 | /************************************************************************** |
| 19 | * |
| 20 | * Management-Controller-to-Driver Interface |
| 21 | * |
| 22 | ************************************************************************** |
| 23 | */ |
| 24 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 25 | #define MCDI_RPC_TIMEOUT (10 * HZ) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 26 | |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 27 | /* A reboot/assertion causes the MCDI status word to be set after the |
| 28 | * command word is set or a REBOOT event is sent. If we notice a reboot |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 29 | * via these mechanisms then wait 20ms for the status word to be set. |
| 30 | */ |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 31 | #define MCDI_STATUS_DELAY_US 100 |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 32 | #define MCDI_STATUS_DELAY_COUNT 200 |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 33 | #define MCDI_STATUS_SLEEP_MS \ |
| 34 | (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 35 | |
| 36 | #define SEQ_MASK \ |
| 37 | EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ)) |
| 38 | |
| 39 | static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx) |
| 40 | { |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 41 | EFX_BUG_ON_PARANOID(!efx->mcdi); |
| 42 | return &efx->mcdi->iface; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Ben Hutchings | f073dde | 2012-09-18 02:33:55 +0100 | [diff] [blame] | 45 | int efx_mcdi_init(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 46 | { |
| 47 | struct efx_mcdi_iface *mcdi; |
| 48 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 49 | efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL); |
| 50 | if (!efx->mcdi) |
| 51 | return -ENOMEM; |
| 52 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 53 | mcdi = efx_mcdi(efx); |
| 54 | init_waitqueue_head(&mcdi->wq); |
| 55 | spin_lock_init(&mcdi->iface_lock); |
| 56 | atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT); |
| 57 | mcdi->mode = MCDI_MODE_POLL; |
| 58 | |
| 59 | (void) efx_mcdi_poll_reboot(efx); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 60 | mcdi->new_epoch = true; |
Ben Hutchings | f073dde | 2012-09-18 02:33:55 +0100 | [diff] [blame] | 61 | |
| 62 | /* Recover from a failed assertion before probing */ |
| 63 | return efx_mcdi_handle_assertion(efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 66 | void efx_mcdi_fini(struct efx_nic *efx) |
| 67 | { |
| 68 | BUG_ON(efx->mcdi && |
| 69 | atomic_read(&efx->mcdi->iface.state) != MCDI_STATE_QUIESCENT); |
| 70 | kfree(efx->mcdi); |
| 71 | } |
| 72 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 73 | static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd, |
| 74 | const efx_dword_t *inbuf, size_t inlen) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 75 | { |
| 76 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 77 | efx_dword_t hdr[2]; |
| 78 | size_t hdr_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 79 | u32 xflags, seqno; |
| 80 | |
| 81 | BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 82 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 83 | /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */ |
| 84 | spin_lock_bh(&mcdi->iface_lock); |
| 85 | ++mcdi->seqno; |
| 86 | spin_unlock_bh(&mcdi->iface_lock); |
| 87 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 88 | seqno = mcdi->seqno & SEQ_MASK; |
| 89 | xflags = 0; |
| 90 | if (mcdi->mode == MCDI_MODE_EVENTS) |
| 91 | xflags |= MCDI_HEADER_XFLAGS_EVREQ; |
| 92 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 93 | if (efx->type->mcdi_max_ver == 1) { |
| 94 | /* MCDI v1 */ |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 95 | EFX_POPULATE_DWORD_7(hdr[0], |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 96 | MCDI_HEADER_RESPONSE, 0, |
| 97 | MCDI_HEADER_RESYNC, 1, |
| 98 | MCDI_HEADER_CODE, cmd, |
| 99 | MCDI_HEADER_DATALEN, inlen, |
| 100 | MCDI_HEADER_SEQ, seqno, |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 101 | MCDI_HEADER_XFLAGS, xflags, |
| 102 | MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 103 | hdr_len = 4; |
| 104 | } else { |
| 105 | /* MCDI v2 */ |
| 106 | BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 107 | EFX_POPULATE_DWORD_7(hdr[0], |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 108 | MCDI_HEADER_RESPONSE, 0, |
| 109 | MCDI_HEADER_RESYNC, 1, |
| 110 | MCDI_HEADER_CODE, MC_CMD_V2_EXTN, |
| 111 | MCDI_HEADER_DATALEN, 0, |
| 112 | MCDI_HEADER_SEQ, seqno, |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 113 | MCDI_HEADER_XFLAGS, xflags, |
| 114 | MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 115 | EFX_POPULATE_DWORD_2(hdr[1], |
| 116 | MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd, |
| 117 | MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen); |
| 118 | hdr_len = 8; |
| 119 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 120 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 121 | efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen); |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 122 | |
| 123 | mcdi->new_epoch = false; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 126 | static int efx_mcdi_errno(unsigned int mcdi_err) |
| 127 | { |
| 128 | switch (mcdi_err) { |
| 129 | case 0: |
| 130 | return 0; |
| 131 | #define TRANSLATE_ERROR(name) \ |
| 132 | case MC_CMD_ERR_ ## name: \ |
| 133 | return -name; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 134 | TRANSLATE_ERROR(EPERM); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 135 | TRANSLATE_ERROR(ENOENT); |
| 136 | TRANSLATE_ERROR(EINTR); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 137 | TRANSLATE_ERROR(EAGAIN); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 138 | TRANSLATE_ERROR(EACCES); |
| 139 | TRANSLATE_ERROR(EBUSY); |
| 140 | TRANSLATE_ERROR(EINVAL); |
| 141 | TRANSLATE_ERROR(EDEADLK); |
| 142 | TRANSLATE_ERROR(ENOSYS); |
| 143 | TRANSLATE_ERROR(ETIME); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 144 | TRANSLATE_ERROR(EALREADY); |
| 145 | TRANSLATE_ERROR(ENOSPC); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 146 | #undef TRANSLATE_ERROR |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 147 | case MC_CMD_ERR_ALLOC_FAIL: |
| 148 | return -ENOBUFS; |
| 149 | case MC_CMD_ERR_MAC_EXIST: |
| 150 | return -EADDRINUSE; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 151 | default: |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 152 | return -EPROTO; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void efx_mcdi_read_response_header(struct efx_nic *efx) |
| 157 | { |
| 158 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 159 | unsigned int respseq, respcmd, error; |
| 160 | efx_dword_t hdr; |
| 161 | |
| 162 | efx->type->mcdi_read_response(efx, &hdr, 0, 4); |
| 163 | respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ); |
| 164 | respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE); |
| 165 | error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR); |
| 166 | |
| 167 | if (respcmd != MC_CMD_V2_EXTN) { |
| 168 | mcdi->resp_hdr_len = 4; |
| 169 | mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN); |
| 170 | } else { |
| 171 | efx->type->mcdi_read_response(efx, &hdr, 4, 4); |
| 172 | mcdi->resp_hdr_len = 8; |
| 173 | mcdi->resp_data_len = |
| 174 | EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN); |
| 175 | } |
| 176 | |
| 177 | if (error && mcdi->resp_data_len == 0) { |
| 178 | netif_err(efx, hw, efx->net_dev, "MC rebooted\n"); |
| 179 | mcdi->resprc = -EIO; |
| 180 | } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) { |
| 181 | netif_err(efx, hw, efx->net_dev, |
| 182 | "MC response mismatch tx seq 0x%x rx seq 0x%x\n", |
| 183 | respseq, mcdi->seqno); |
| 184 | mcdi->resprc = -EIO; |
| 185 | } else if (error) { |
| 186 | efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4); |
| 187 | mcdi->resprc = |
| 188 | efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0)); |
| 189 | } else { |
| 190 | mcdi->resprc = 0; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 194 | static int efx_mcdi_poll(struct efx_nic *efx) |
| 195 | { |
| 196 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 197 | unsigned long time, finish; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 198 | unsigned int spins; |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 199 | int rc; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 200 | |
| 201 | /* Check for a reboot atomically with respect to efx_mcdi_copyout() */ |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 202 | rc = efx_mcdi_poll_reboot(efx); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 203 | if (rc) { |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 204 | spin_lock_bh(&mcdi->iface_lock); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 205 | mcdi->resprc = rc; |
| 206 | mcdi->resp_hdr_len = 0; |
| 207 | mcdi->resp_data_len = 0; |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 208 | spin_unlock_bh(&mcdi->iface_lock); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 211 | |
| 212 | /* Poll for completion. Poll quickly (once a us) for the 1st jiffy, |
| 213 | * because generally mcdi responses are fast. After that, back off |
| 214 | * and poll once a jiffy (approximately) |
| 215 | */ |
| 216 | spins = TICK_USEC; |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 217 | finish = jiffies + MCDI_RPC_TIMEOUT; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 218 | |
| 219 | while (1) { |
| 220 | if (spins != 0) { |
| 221 | --spins; |
| 222 | udelay(1); |
Ben Hutchings | 55029c1 | 2010-01-13 04:34:25 +0000 | [diff] [blame] | 223 | } else { |
| 224 | schedule_timeout_uninterruptible(1); |
| 225 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 226 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 227 | time = jiffies; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 228 | |
Ben Hutchings | 86c432c | 2011-09-01 12:09:29 +0000 | [diff] [blame] | 229 | rmb(); |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 230 | if (efx->type->mcdi_poll_response(efx)) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 231 | break; |
| 232 | |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 233 | if (time_after(time, finish)) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 234 | return -ETIMEDOUT; |
| 235 | } |
| 236 | |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 237 | spin_lock_bh(&mcdi->iface_lock); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 238 | efx_mcdi_read_response_header(efx); |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 239 | spin_unlock_bh(&mcdi->iface_lock); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 240 | |
| 241 | /* Return rc=0 like wait_event_timeout() */ |
| 242 | return 0; |
| 243 | } |
| 244 | |
Ben Hutchings | 876be08 | 2012-10-01 20:58:35 +0100 | [diff] [blame] | 245 | /* Test and clear MC-rebooted flag for this port/function; reset |
| 246 | * software state as necessary. |
| 247 | */ |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 248 | int efx_mcdi_poll_reboot(struct efx_nic *efx) |
| 249 | { |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 250 | if (!efx->mcdi) |
| 251 | return 0; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 252 | |
Ben Hutchings | cd0ecc9 | 2012-12-14 21:52:56 +0000 | [diff] [blame] | 253 | return efx->type->mcdi_poll_reboot(efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void efx_mcdi_acquire(struct efx_mcdi_iface *mcdi) |
| 257 | { |
| 258 | /* Wait until the interface becomes QUIESCENT and we win the race |
| 259 | * to mark it RUNNING. */ |
| 260 | wait_event(mcdi->wq, |
| 261 | atomic_cmpxchg(&mcdi->state, |
| 262 | MCDI_STATE_QUIESCENT, |
| 263 | MCDI_STATE_RUNNING) |
| 264 | == MCDI_STATE_QUIESCENT); |
| 265 | } |
| 266 | |
| 267 | static int efx_mcdi_await_completion(struct efx_nic *efx) |
| 268 | { |
| 269 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 270 | |
| 271 | if (wait_event_timeout( |
| 272 | mcdi->wq, |
| 273 | atomic_read(&mcdi->state) == MCDI_STATE_COMPLETED, |
Ben Hutchings | ebf98e7 | 2012-12-01 02:21:17 +0000 | [diff] [blame] | 274 | MCDI_RPC_TIMEOUT) == 0) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 275 | return -ETIMEDOUT; |
| 276 | |
| 277 | /* Check if efx_mcdi_set_mode() switched us back to polled completions. |
| 278 | * In which case, poll for completions directly. If efx_mcdi_ev_cpl() |
| 279 | * completed the request first, then we'll just end up completing the |
| 280 | * request again, which is safe. |
| 281 | * |
| 282 | * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which |
| 283 | * wait_event_timeout() implicitly provides. |
| 284 | */ |
| 285 | if (mcdi->mode == MCDI_MODE_POLL) |
| 286 | return efx_mcdi_poll(efx); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static bool efx_mcdi_complete(struct efx_mcdi_iface *mcdi) |
| 292 | { |
| 293 | /* If the interface is RUNNING, then move to COMPLETED and wake any |
| 294 | * waiters. If the interface isn't in RUNNING then we've received a |
| 295 | * duplicate completion after we've already transitioned back to |
| 296 | * QUIESCENT. [A subsequent invocation would increment seqno, so would |
| 297 | * have failed the seqno check]. |
| 298 | */ |
| 299 | if (atomic_cmpxchg(&mcdi->state, |
| 300 | MCDI_STATE_RUNNING, |
| 301 | MCDI_STATE_COMPLETED) == MCDI_STATE_RUNNING) { |
| 302 | wake_up(&mcdi->wq); |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | static void efx_mcdi_release(struct efx_mcdi_iface *mcdi) |
| 310 | { |
| 311 | atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT); |
| 312 | wake_up(&mcdi->wq); |
| 313 | } |
| 314 | |
| 315 | 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] | 316 | unsigned int datalen, unsigned int mcdi_err) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 317 | { |
| 318 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 319 | bool wake = false; |
| 320 | |
| 321 | spin_lock(&mcdi->iface_lock); |
| 322 | |
| 323 | if ((seqno ^ mcdi->seqno) & SEQ_MASK) { |
| 324 | if (mcdi->credits) |
| 325 | /* The request has been cancelled */ |
| 326 | --mcdi->credits; |
| 327 | else |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 328 | netif_err(efx, hw, efx->net_dev, |
| 329 | "MC response mismatch tx seq 0x%x rx " |
| 330 | "seq 0x%x\n", seqno, mcdi->seqno); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 331 | } else { |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 332 | if (efx->type->mcdi_max_ver >= 2) { |
| 333 | /* MCDI v2 responses don't fit in an event */ |
| 334 | efx_mcdi_read_response_header(efx); |
| 335 | } else { |
| 336 | mcdi->resprc = efx_mcdi_errno(mcdi_err); |
| 337 | mcdi->resp_hdr_len = 4; |
| 338 | mcdi->resp_data_len = datalen; |
| 339 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 340 | |
| 341 | wake = true; |
| 342 | } |
| 343 | |
| 344 | spin_unlock(&mcdi->iface_lock); |
| 345 | |
| 346 | if (wake) |
| 347 | efx_mcdi_complete(mcdi); |
| 348 | } |
| 349 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 350 | static int |
| 351 | efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen) |
| 352 | { |
| 353 | if (efx->type->mcdi_max_ver < 0 || |
| 354 | (efx->type->mcdi_max_ver < 2 && |
| 355 | cmd > MC_CMD_CMD_SPACE_ESCAPE_7)) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 || |
| 359 | (efx->type->mcdi_max_ver < 2 && |
| 360 | inlen > MCDI_CTL_SDU_LEN_MAX_V1)) |
| 361 | return -EMSGSIZE; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 366 | int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd, |
Ben Hutchings | 9528b92 | 2012-09-14 17:31:41 +0100 | [diff] [blame] | 367 | const efx_dword_t *inbuf, size_t inlen, |
| 368 | efx_dword_t *outbuf, size_t outlen, |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 369 | size_t *outlen_actual) |
| 370 | { |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 371 | int rc; |
| 372 | |
| 373 | rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen); |
| 374 | if (rc) |
| 375 | return rc; |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 376 | return efx_mcdi_rpc_finish(efx, cmd, inlen, |
| 377 | outbuf, outlen, outlen_actual); |
| 378 | } |
| 379 | |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 380 | int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd, |
| 381 | const efx_dword_t *inbuf, size_t inlen) |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 382 | { |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 383 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 384 | int rc; |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 385 | |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 386 | rc = efx_mcdi_check_supported(efx, cmd, inlen); |
| 387 | if (rc) |
| 388 | return rc; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 389 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 390 | efx_mcdi_acquire(mcdi); |
Ben Hutchings | 2f4bcdc | 2013-08-22 22:06:09 +0100 | [diff] [blame^] | 391 | efx_mcdi_send_request(efx, cmd, inbuf, inlen); |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 392 | return 0; |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen, |
Ben Hutchings | 9528b92 | 2012-09-14 17:31:41 +0100 | [diff] [blame] | 396 | efx_dword_t *outbuf, size_t outlen, |
| 397 | size_t *outlen_actual) |
Stuart Hodgson | c3cba72 | 2012-07-16 17:40:47 +0100 | [diff] [blame] | 398 | { |
| 399 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 400 | int rc; |
| 401 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 402 | if (mcdi->mode == MCDI_MODE_POLL) |
| 403 | rc = efx_mcdi_poll(efx); |
| 404 | else |
| 405 | rc = efx_mcdi_await_completion(efx); |
| 406 | |
| 407 | if (rc != 0) { |
| 408 | /* Close the race with efx_mcdi_ev_cpl() executing just too late |
| 409 | * and completing a request we've just cancelled, by ensuring |
| 410 | * that the seqno check therein fails. |
| 411 | */ |
| 412 | spin_lock_bh(&mcdi->iface_lock); |
| 413 | ++mcdi->seqno; |
| 414 | ++mcdi->credits; |
| 415 | spin_unlock_bh(&mcdi->iface_lock); |
| 416 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 417 | netif_err(efx, hw, efx->net_dev, |
| 418 | "MC command 0x%x inlen %d mode %d timed out\n", |
| 419 | cmd, (int)inlen, mcdi->mode); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 420 | } else { |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 421 | size_t hdr_len, data_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 422 | |
| 423 | /* At the very least we need a memory barrier here to ensure |
| 424 | * we pick up changes from efx_mcdi_ev_cpl(). Protect against |
| 425 | * a spurious efx_mcdi_ev_cpl() running concurrently by |
| 426 | * acquiring the iface_lock. */ |
| 427 | spin_lock_bh(&mcdi->iface_lock); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 428 | rc = mcdi->resprc; |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 429 | hdr_len = mcdi->resp_hdr_len; |
| 430 | data_len = mcdi->resp_data_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 431 | spin_unlock_bh(&mcdi->iface_lock); |
| 432 | |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 433 | BUG_ON(rc > 0); |
| 434 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 435 | if (rc == 0) { |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 436 | efx->type->mcdi_read_response(efx, outbuf, hdr_len, |
| 437 | min(outlen, data_len)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 438 | if (outlen_actual != NULL) |
Ben Hutchings | 369327f | 2012-10-26 17:53:12 +0100 | [diff] [blame] | 439 | *outlen_actual = data_len; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 440 | } else if (cmd == MC_CMD_REBOOT && rc == -EIO) |
| 441 | ; /* Don't reset if MC_CMD_REBOOT returns EIO */ |
| 442 | else if (rc == -EIO || rc == -EINTR) { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 443 | netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n", |
| 444 | -rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 445 | efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE); |
| 446 | } else |
Ben Hutchings | f18ca36 | 2010-12-02 13:46:09 +0000 | [diff] [blame] | 447 | netif_dbg(efx, hw, efx->net_dev, |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 448 | "MC command 0x%x inlen %d failed rc=%d\n", |
| 449 | cmd, (int)inlen, -rc); |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 450 | |
| 451 | if (rc == -EIO || rc == -EINTR) { |
| 452 | msleep(MCDI_STATUS_SLEEP_MS); |
| 453 | efx_mcdi_poll_reboot(efx); |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 454 | mcdi->new_epoch = true; |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 455 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | efx_mcdi_release(mcdi); |
| 459 | return rc; |
| 460 | } |
| 461 | |
| 462 | void efx_mcdi_mode_poll(struct efx_nic *efx) |
| 463 | { |
| 464 | struct efx_mcdi_iface *mcdi; |
| 465 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 466 | if (!efx->mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 467 | return; |
| 468 | |
| 469 | mcdi = efx_mcdi(efx); |
| 470 | if (mcdi->mode == MCDI_MODE_POLL) |
| 471 | return; |
| 472 | |
| 473 | /* We can switch from event completion to polled completion, because |
| 474 | * mcdi requests are always completed in shared memory. We do this by |
| 475 | * switching the mode to POLL'd then completing the request. |
| 476 | * efx_mcdi_await_completion() will then call efx_mcdi_poll(). |
| 477 | * |
| 478 | * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(), |
| 479 | * which efx_mcdi_complete() provides for us. |
| 480 | */ |
| 481 | mcdi->mode = MCDI_MODE_POLL; |
| 482 | |
| 483 | efx_mcdi_complete(mcdi); |
| 484 | } |
| 485 | |
| 486 | void efx_mcdi_mode_event(struct efx_nic *efx) |
| 487 | { |
| 488 | struct efx_mcdi_iface *mcdi; |
| 489 | |
Ben Hutchings | f3ad500 | 2012-09-18 02:33:56 +0100 | [diff] [blame] | 490 | if (!efx->mcdi) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 491 | return; |
| 492 | |
| 493 | mcdi = efx_mcdi(efx); |
| 494 | |
| 495 | if (mcdi->mode == MCDI_MODE_EVENTS) |
| 496 | return; |
| 497 | |
| 498 | /* We can't switch from polled to event completion in the middle of a |
| 499 | * request, because the completion method is specified in the request. |
| 500 | * So acquire the interface to serialise the requestors. We don't need |
| 501 | * to acquire the iface_lock to change the mode here, but we do need a |
| 502 | * write memory barrier ensure that efx_mcdi_rpc() sees it, which |
| 503 | * efx_mcdi_acquire() provides. |
| 504 | */ |
| 505 | efx_mcdi_acquire(mcdi); |
| 506 | mcdi->mode = MCDI_MODE_EVENTS; |
| 507 | efx_mcdi_release(mcdi); |
| 508 | } |
| 509 | |
| 510 | static void efx_mcdi_ev_death(struct efx_nic *efx, int rc) |
| 511 | { |
| 512 | struct efx_mcdi_iface *mcdi = efx_mcdi(efx); |
| 513 | |
| 514 | /* If there is an outstanding MCDI request, it has been terminated |
| 515 | * either by a BADASSERT or REBOOT event. If the mcdi interface is |
| 516 | * in polled mode, then do nothing because the MC reboot handler will |
| 517 | * set the header correctly. However, if the mcdi interface is waiting |
| 518 | * for a CMDDONE event it won't receive it [and since all MCDI events |
| 519 | * are sent to the same queue, we can't be racing with |
| 520 | * efx_mcdi_ev_cpl()] |
| 521 | * |
| 522 | * There's a race here with efx_mcdi_rpc(), because we might receive |
| 523 | * a REBOOT event *before* the request has been copied out. In polled |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 524 | * mode (during startup) this is irrelevant, because efx_mcdi_complete() |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 525 | * is ignored. In event mode, this condition is just an edge-case of |
| 526 | * receiving a REBOOT event after posting the MCDI request. Did the mc |
| 527 | * reboot before or after the copyout? The best we can do always is |
| 528 | * just return failure. |
| 529 | */ |
| 530 | spin_lock(&mcdi->iface_lock); |
| 531 | if (efx_mcdi_complete(mcdi)) { |
| 532 | if (mcdi->mode == MCDI_MODE_EVENTS) { |
| 533 | mcdi->resprc = rc; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 534 | mcdi->resp_hdr_len = 0; |
| 535 | mcdi->resp_data_len = 0; |
Steve Hodgson | 18e3ee2 | 2010-12-02 13:46:55 +0000 | [diff] [blame] | 536 | ++mcdi->credits; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 537 | } |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 538 | } else { |
| 539 | int count; |
| 540 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 541 | /* Nobody was waiting for an MCDI request, so trigger a reset */ |
| 542 | efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE); |
| 543 | |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 544 | /* Consume the status word since efx_mcdi_rpc_finish() won't */ |
| 545 | for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) { |
| 546 | if (efx_mcdi_poll_reboot(efx)) |
| 547 | break; |
| 548 | udelay(MCDI_STATUS_DELAY_US); |
| 549 | } |
Daniel Pieczko | d36a08b | 2013-06-20 11:40:07 +0100 | [diff] [blame] | 550 | mcdi->new_epoch = true; |
Ben Hutchings | 3f713bf | 2011-12-20 23:39:31 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 553 | spin_unlock(&mcdi->iface_lock); |
| 554 | } |
| 555 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 556 | /* Called from falcon_process_eventq for MCDI events */ |
| 557 | void efx_mcdi_process_event(struct efx_channel *channel, |
| 558 | efx_qword_t *event) |
| 559 | { |
| 560 | struct efx_nic *efx = channel->efx; |
| 561 | int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE); |
| 562 | u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA); |
| 563 | |
| 564 | switch (code) { |
| 565 | case MCDI_EVENT_CODE_BADSSERT: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 566 | netif_err(efx, hw, efx->net_dev, |
| 567 | "MC watchdog or assertion failure at 0x%x\n", data); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 568 | efx_mcdi_ev_death(efx, -EINTR); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case MCDI_EVENT_CODE_PMNOTICE: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 572 | netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n"); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | case MCDI_EVENT_CODE_CMDDONE: |
| 576 | efx_mcdi_ev_cpl(efx, |
| 577 | MCDI_EVENT_FIELD(*event, CMDDONE_SEQ), |
| 578 | MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN), |
| 579 | MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO)); |
| 580 | break; |
| 581 | |
| 582 | case MCDI_EVENT_CODE_LINKCHANGE: |
| 583 | efx_mcdi_process_link_change(efx, event); |
| 584 | break; |
| 585 | case MCDI_EVENT_CODE_SENSOREVT: |
| 586 | efx_mcdi_sensor_event(efx, event); |
| 587 | break; |
| 588 | case MCDI_EVENT_CODE_SCHEDERR: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 589 | netif_info(efx, hw, efx->net_dev, |
| 590 | "MC Scheduler error address=0x%x\n", data); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 591 | break; |
| 592 | case MCDI_EVENT_CODE_REBOOT: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 593 | netif_info(efx, hw, efx->net_dev, "MC Reboot\n"); |
Ben Hutchings | 5bc283e | 2012-10-08 21:43:00 +0100 | [diff] [blame] | 594 | efx_mcdi_ev_death(efx, -EIO); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 595 | break; |
| 596 | case MCDI_EVENT_CODE_MAC_STATS_DMA: |
| 597 | /* MAC stats are gather lazily. We can ignore this. */ |
| 598 | break; |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 599 | case MCDI_EVENT_CODE_FLR: |
| 600 | efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF)); |
| 601 | break; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 602 | case MCDI_EVENT_CODE_PTP_RX: |
| 603 | case MCDI_EVENT_CODE_PTP_FAULT: |
| 604 | case MCDI_EVENT_CODE_PTP_PPS: |
| 605 | efx_ptp_event(efx, event); |
| 606 | break; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 607 | |
Alexandre Rames | 3de82b9 | 2013-06-13 11:36:15 +0100 | [diff] [blame] | 608 | case MCDI_EVENT_CODE_TX_ERR: |
| 609 | case MCDI_EVENT_CODE_RX_ERR: |
| 610 | netif_err(efx, hw, efx->net_dev, |
| 611 | "%s DMA error (event: "EFX_QWORD_FMT")\n", |
| 612 | code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX", |
| 613 | EFX_QWORD_VAL(*event)); |
| 614 | efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR); |
| 615 | break; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 616 | default: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 617 | netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n", |
| 618 | code); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | /************************************************************************** |
| 623 | * |
| 624 | * Specific request functions |
| 625 | * |
| 626 | ************************************************************************** |
| 627 | */ |
| 628 | |
Ben Hutchings | e5f0fd2 | 2011-02-24 23:57:47 +0000 | [diff] [blame] | 629 | 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] | 630 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 631 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 632 | size_t outlength; |
| 633 | const __le16 *ver_words; |
| 634 | int rc; |
| 635 | |
| 636 | BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0); |
| 637 | |
| 638 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0, |
| 639 | outbuf, sizeof(outbuf), &outlength); |
| 640 | if (rc) |
| 641 | goto fail; |
| 642 | |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 643 | if (outlength < MC_CMD_GET_VERSION_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 644 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 645 | goto fail; |
| 646 | } |
| 647 | |
| 648 | ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION); |
Ben Hutchings | e5f0fd2 | 2011-02-24 23:57:47 +0000 | [diff] [blame] | 649 | snprintf(buf, len, "%u.%u.%u.%u", |
| 650 | le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]), |
| 651 | le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3])); |
| 652 | return; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 653 | |
| 654 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 655 | 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] | 656 | buf[0] = 0; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating, |
| 660 | bool *was_attached) |
| 661 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 662 | MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN); |
| 663 | MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 664 | size_t outlen; |
| 665 | int rc; |
| 666 | |
| 667 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE, |
| 668 | driver_operating ? 1 : 0); |
| 669 | MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1); |
Ben Hutchings | f2b0bef | 2013-08-20 20:35:50 +0100 | [diff] [blame] | 670 | 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] | 671 | |
| 672 | rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf), |
| 673 | outbuf, sizeof(outbuf), &outlen); |
| 674 | if (rc) |
| 675 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 676 | if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) { |
| 677 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 678 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 679 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 680 | |
| 681 | if (was_attached != NULL) |
| 682 | *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE); |
| 683 | return 0; |
| 684 | |
| 685 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 686 | 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] | 687 | return rc; |
| 688 | } |
| 689 | |
| 690 | 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] | 691 | u16 *fw_subtype_list, u32 *capabilities) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 692 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 693 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 694 | size_t outlen, i; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 695 | int port_num = efx_port_num(efx); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 696 | int rc; |
| 697 | |
| 698 | BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0); |
| 699 | |
| 700 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0, |
| 701 | outbuf, sizeof(outbuf), &outlen); |
| 702 | if (rc) |
| 703 | goto fail; |
| 704 | |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 705 | if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 706 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 707 | goto fail; |
| 708 | } |
| 709 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 710 | if (mac_address) |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 711 | memcpy(mac_address, |
| 712 | port_num ? |
| 713 | MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) : |
| 714 | MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0), |
| 715 | ETH_ALEN); |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 716 | if (fw_subtype_list) { |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 717 | for (i = 0; |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 718 | i < MCDI_VAR_ARRAY_LEN(outlen, |
| 719 | GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST); |
| 720 | i++) |
| 721 | fw_subtype_list[i] = MCDI_ARRAY_WORD( |
| 722 | outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i); |
| 723 | for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++) |
| 724 | fw_subtype_list[i] = 0; |
Ben Hutchings | bfeed90 | 2012-09-07 00:58:10 +0100 | [diff] [blame] | 725 | } |
Matthew Slattery | 6aa9c7f | 2010-07-14 15:36:19 +0100 | [diff] [blame] | 726 | if (capabilities) { |
| 727 | if (port_num) |
| 728 | *capabilities = MCDI_DWORD(outbuf, |
| 729 | GET_BOARD_CFG_OUT_CAPABILITIES_PORT1); |
| 730 | else |
| 731 | *capabilities = MCDI_DWORD(outbuf, |
| 732 | GET_BOARD_CFG_OUT_CAPABILITIES_PORT0); |
| 733 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | |
| 737 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 738 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n", |
| 739 | __func__, rc, (int)outlen); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 740 | |
| 741 | return rc; |
| 742 | } |
| 743 | |
| 744 | int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq) |
| 745 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 746 | MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 747 | u32 dest = 0; |
| 748 | int rc; |
| 749 | |
| 750 | if (uart) |
| 751 | dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART; |
| 752 | if (evq) |
| 753 | dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ; |
| 754 | |
| 755 | MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest); |
| 756 | MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq); |
| 757 | |
| 758 | BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0); |
| 759 | |
| 760 | rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf), |
| 761 | NULL, 0, NULL); |
| 762 | if (rc) |
| 763 | goto fail; |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 768 | 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] | 769 | return rc; |
| 770 | } |
| 771 | |
| 772 | int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out) |
| 773 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 774 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 775 | size_t outlen; |
| 776 | int rc; |
| 777 | |
| 778 | BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0); |
| 779 | |
| 780 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0, |
| 781 | outbuf, sizeof(outbuf), &outlen); |
| 782 | if (rc) |
| 783 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 784 | if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) { |
| 785 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 786 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 787 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 788 | |
| 789 | *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES); |
| 790 | return 0; |
| 791 | |
| 792 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 793 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
| 794 | __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 795 | return rc; |
| 796 | } |
| 797 | |
| 798 | int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type, |
| 799 | size_t *size_out, size_t *erase_size_out, |
| 800 | bool *protected_out) |
| 801 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 802 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN); |
| 803 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 804 | size_t outlen; |
| 805 | int rc; |
| 806 | |
| 807 | MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type); |
| 808 | |
| 809 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf), |
| 810 | outbuf, sizeof(outbuf), &outlen); |
| 811 | if (rc) |
| 812 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 813 | if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) { |
| 814 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 815 | goto fail; |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 816 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 817 | |
| 818 | *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE); |
| 819 | *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE); |
| 820 | *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) & |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 821 | (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 822 | return 0; |
| 823 | |
| 824 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 825 | 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] | 826 | return rc; |
| 827 | } |
| 828 | |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 829 | static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type) |
| 830 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 831 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN); |
| 832 | MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN); |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 833 | int rc; |
| 834 | |
| 835 | MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type); |
| 836 | |
| 837 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf), |
| 838 | outbuf, sizeof(outbuf), NULL); |
| 839 | if (rc) |
| 840 | return rc; |
| 841 | |
| 842 | switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) { |
| 843 | case MC_CMD_NVRAM_TEST_PASS: |
| 844 | case MC_CMD_NVRAM_TEST_NOTSUPP: |
| 845 | return 0; |
| 846 | default: |
| 847 | return -EIO; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | int efx_mcdi_nvram_test_all(struct efx_nic *efx) |
| 852 | { |
| 853 | u32 nvram_types; |
| 854 | unsigned int type; |
| 855 | int rc; |
| 856 | |
| 857 | rc = efx_mcdi_nvram_types(efx, &nvram_types); |
| 858 | if (rc) |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 859 | goto fail1; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 860 | |
| 861 | type = 0; |
| 862 | while (nvram_types != 0) { |
| 863 | if (nvram_types & 1) { |
| 864 | rc = efx_mcdi_nvram_test(efx, type); |
| 865 | if (rc) |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 866 | goto fail2; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 867 | } |
| 868 | type++; |
| 869 | nvram_types >>= 1; |
| 870 | } |
| 871 | |
| 872 | return 0; |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 873 | |
| 874 | fail2: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 875 | netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n", |
| 876 | __func__, type); |
Ben Hutchings | b548a98 | 2010-04-28 09:28:36 +0000 | [diff] [blame] | 877 | fail1: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 878 | 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] | 879 | return rc; |
Ben Hutchings | 2e80340 | 2010-02-03 09:31:01 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 882 | static int efx_mcdi_read_assertion(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 883 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 884 | MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN); |
| 885 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 886 | unsigned int flags, index; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 887 | const char *reason; |
| 888 | size_t outlen; |
| 889 | int retry; |
| 890 | int rc; |
| 891 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 892 | /* Attempt to read any stored assertion state before we reboot |
| 893 | * the mcfw out of the assertion handler. Retry twice, once |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 894 | * because a boot-time assertion might cause this command to fail |
| 895 | * with EINTR. And once again because GET_ASSERTS can race with |
| 896 | * MC_CMD_REBOOT running on the other port. */ |
| 897 | retry = 2; |
| 898 | do { |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 899 | MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 900 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS, |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 901 | inbuf, MC_CMD_GET_ASSERTS_IN_LEN, |
| 902 | outbuf, sizeof(outbuf), &outlen); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 903 | } while ((rc == -EINTR || rc == -EIO) && retry-- > 0); |
| 904 | |
| 905 | if (rc) |
| 906 | return rc; |
| 907 | if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN) |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 908 | return -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 909 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 910 | /* Print out any recorded assertion state */ |
| 911 | flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 912 | if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS) |
| 913 | return 0; |
| 914 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 915 | reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL) |
| 916 | ? "system-level assertion" |
| 917 | : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL) |
| 918 | ? "thread-level assertion" |
| 919 | : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED) |
| 920 | ? "watchdog reset" |
| 921 | : "unknown assertion"; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 922 | netif_err(efx, hw, efx->net_dev, |
| 923 | "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason, |
| 924 | MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS), |
| 925 | MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 926 | |
| 927 | /* Print out the registers */ |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 928 | for (index = 0; |
| 929 | index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM; |
| 930 | index++) |
| 931 | netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n", |
| 932 | 1 + index, |
| 933 | MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS, |
| 934 | index)); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 939 | static void efx_mcdi_exit_assertion(struct efx_nic *efx) |
| 940 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 941 | MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN); |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 942 | |
Ben Hutchings | 0f1e54a | 2012-07-02 23:37:40 +0100 | [diff] [blame] | 943 | /* If the MC is running debug firmware, it might now be |
| 944 | * waiting for a debugger to attach, but we just want it to |
| 945 | * reboot. We set a flag that makes the command a no-op if it |
| 946 | * has already done so. We don't know what return code to |
| 947 | * expect (0 or -EIO), so ignore it. |
| 948 | */ |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 949 | BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0); |
| 950 | MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, |
| 951 | MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION); |
Ben Hutchings | 0f1e54a | 2012-07-02 23:37:40 +0100 | [diff] [blame] | 952 | (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN, |
| 953 | NULL, 0, NULL); |
Steve Hodgson | 8b2103a | 2010-02-03 09:30:17 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | int efx_mcdi_handle_assertion(struct efx_nic *efx) |
| 957 | { |
| 958 | int rc; |
| 959 | |
| 960 | rc = efx_mcdi_read_assertion(efx); |
| 961 | if (rc) |
| 962 | return rc; |
| 963 | |
| 964 | efx_mcdi_exit_assertion(efx); |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 969 | void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode) |
| 970 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 971 | MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 972 | int rc; |
| 973 | |
| 974 | BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF); |
| 975 | BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON); |
| 976 | BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT); |
| 977 | |
| 978 | BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0); |
| 979 | |
| 980 | MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode); |
| 981 | |
| 982 | rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf), |
| 983 | NULL, 0, NULL); |
| 984 | if (rc) |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 985 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
| 986 | __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 989 | static int efx_mcdi_reset_port(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 990 | { |
Ben Hutchings | 05a9320 | 2011-12-20 00:44:06 +0000 | [diff] [blame] | 991 | int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 992 | if (rc) |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 993 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
| 994 | __func__, rc); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 995 | return rc; |
| 996 | } |
| 997 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 998 | static int efx_mcdi_reset_mc(struct efx_nic *efx) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 999 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1000 | MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1001 | int rc; |
| 1002 | |
| 1003 | BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0); |
| 1004 | MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0); |
| 1005 | rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf), |
| 1006 | NULL, 0, NULL); |
| 1007 | /* White is black, and up is down */ |
| 1008 | if (rc == -EIO) |
| 1009 | return 0; |
| 1010 | if (rc == 0) |
| 1011 | rc = -EIO; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1012 | 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] | 1013 | return rc; |
| 1014 | } |
| 1015 | |
Ben Hutchings | 6bff861 | 2012-09-18 02:33:52 +0100 | [diff] [blame] | 1016 | enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason) |
| 1017 | { |
| 1018 | return RESET_TYPE_RECOVER_OR_ALL; |
| 1019 | } |
| 1020 | |
| 1021 | int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method) |
| 1022 | { |
| 1023 | int rc; |
| 1024 | |
| 1025 | /* Recover from a failed assertion pre-reset */ |
| 1026 | rc = efx_mcdi_handle_assertion(efx); |
| 1027 | if (rc) |
| 1028 | return rc; |
| 1029 | |
| 1030 | if (method == RESET_TYPE_WORLD) |
| 1031 | return efx_mcdi_reset_mc(efx); |
| 1032 | else |
| 1033 | return efx_mcdi_reset_port(efx); |
| 1034 | } |
| 1035 | |
stephen hemminger | d215697 | 2010-10-18 05:27:31 +0000 | [diff] [blame] | 1036 | static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type, |
| 1037 | const u8 *mac, int *id_out) |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1038 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1039 | MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN); |
| 1040 | MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1041 | size_t outlen; |
| 1042 | int rc; |
| 1043 | |
| 1044 | MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type); |
| 1045 | MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE, |
| 1046 | MC_CMD_FILTER_MODE_SIMPLE); |
| 1047 | memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN); |
| 1048 | |
| 1049 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf), |
| 1050 | outbuf, sizeof(outbuf), &outlen); |
| 1051 | if (rc) |
| 1052 | goto fail; |
| 1053 | |
| 1054 | if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1055 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1056 | goto fail; |
| 1057 | } |
| 1058 | |
| 1059 | *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID); |
| 1060 | |
| 1061 | return 0; |
| 1062 | |
| 1063 | fail: |
| 1064 | *id_out = -1; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1065 | 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] | 1066 | return rc; |
| 1067 | |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | int |
| 1072 | efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out) |
| 1073 | { |
| 1074 | return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out); |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out) |
| 1079 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1080 | MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1081 | size_t outlen; |
| 1082 | int rc; |
| 1083 | |
| 1084 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0, |
| 1085 | outbuf, sizeof(outbuf), &outlen); |
| 1086 | if (rc) |
| 1087 | goto fail; |
| 1088 | |
| 1089 | if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) { |
Ben Hutchings | 00bbb4a | 2010-04-28 09:27:14 +0000 | [diff] [blame] | 1090 | rc = -EIO; |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1091 | goto fail; |
| 1092 | } |
| 1093 | |
| 1094 | *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID); |
| 1095 | |
| 1096 | return 0; |
| 1097 | |
| 1098 | fail: |
| 1099 | *id_out = -1; |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1100 | 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] | 1101 | return rc; |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id) |
| 1106 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1107 | MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN); |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1108 | int rc; |
| 1109 | |
| 1110 | MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id); |
| 1111 | |
| 1112 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf), |
| 1113 | NULL, 0, NULL); |
| 1114 | if (rc) |
| 1115 | goto fail; |
| 1116 | |
| 1117 | return 0; |
| 1118 | |
| 1119 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1120 | 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] | 1121 | return rc; |
| 1122 | } |
| 1123 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1124 | int efx_mcdi_flush_rxqs(struct efx_nic *efx) |
| 1125 | { |
| 1126 | struct efx_channel *channel; |
| 1127 | struct efx_rx_queue *rx_queue; |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1128 | MCDI_DECLARE_BUF(inbuf, |
| 1129 | MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS)); |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1130 | int rc, count; |
| 1131 | |
Ben Hutchings | 4507837 | 2012-09-19 02:53:34 +0100 | [diff] [blame] | 1132 | BUILD_BUG_ON(EFX_MAX_CHANNELS > |
| 1133 | MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM); |
| 1134 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1135 | count = 0; |
| 1136 | efx_for_each_channel(channel, efx) { |
| 1137 | efx_for_each_channel_rx_queue(rx_queue, channel) { |
| 1138 | if (rx_queue->flush_pending) { |
| 1139 | rx_queue->flush_pending = false; |
| 1140 | atomic_dec(&efx->rxq_flush_pending); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1141 | MCDI_SET_ARRAY_DWORD( |
| 1142 | inbuf, FLUSH_RX_QUEUES_IN_QID_OFST, |
| 1143 | count, efx_rx_queue_index(rx_queue)); |
| 1144 | count++; |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 1149 | rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf, |
| 1150 | MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL); |
Ben Hutchings | bbec969 | 2012-09-11 18:25:13 +0100 | [diff] [blame] | 1151 | WARN_ON(rc < 0); |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1152 | |
Ben Hutchings | cd2d5b5 | 2012-02-14 00:48:07 +0000 | [diff] [blame] | 1153 | return rc; |
| 1154 | } |
Ben Hutchings | afd4aea | 2009-11-29 15:15:25 +0000 | [diff] [blame] | 1155 | |
| 1156 | int efx_mcdi_wol_filter_reset(struct efx_nic *efx) |
| 1157 | { |
| 1158 | int rc; |
| 1159 | |
| 1160 | rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL); |
| 1161 | if (rc) |
| 1162 | goto fail; |
| 1163 | |
| 1164 | return 0; |
| 1165 | |
| 1166 | fail: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1167 | 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] | 1168 | return rc; |
| 1169 | } |
| 1170 | |
Ben Hutchings | 45a3fd5 | 2012-11-28 04:38:14 +0000 | [diff] [blame] | 1171 | #ifdef CONFIG_SFC_MTD |
| 1172 | |
| 1173 | #define EFX_MCDI_NVRAM_LEN_MAX 128 |
| 1174 | |
| 1175 | static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type) |
| 1176 | { |
| 1177 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN); |
| 1178 | int rc; |
| 1179 | |
| 1180 | MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type); |
| 1181 | |
| 1182 | BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0); |
| 1183 | |
| 1184 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf), |
| 1185 | NULL, 0, NULL); |
| 1186 | if (rc) |
| 1187 | goto fail; |
| 1188 | |
| 1189 | return 0; |
| 1190 | |
| 1191 | fail: |
| 1192 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
| 1193 | return rc; |
| 1194 | } |
| 1195 | |
| 1196 | static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type, |
| 1197 | loff_t offset, u8 *buffer, size_t length) |
| 1198 | { |
| 1199 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN); |
| 1200 | MCDI_DECLARE_BUF(outbuf, |
| 1201 | MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX)); |
| 1202 | size_t outlen; |
| 1203 | int rc; |
| 1204 | |
| 1205 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type); |
| 1206 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset); |
| 1207 | MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length); |
| 1208 | |
| 1209 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf), |
| 1210 | outbuf, sizeof(outbuf), &outlen); |
| 1211 | if (rc) |
| 1212 | goto fail; |
| 1213 | |
| 1214 | memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length); |
| 1215 | return 0; |
| 1216 | |
| 1217 | fail: |
| 1218 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
| 1219 | return rc; |
| 1220 | } |
| 1221 | |
| 1222 | static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type, |
| 1223 | loff_t offset, const u8 *buffer, size_t length) |
| 1224 | { |
| 1225 | MCDI_DECLARE_BUF(inbuf, |
| 1226 | MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX)); |
| 1227 | int rc; |
| 1228 | |
| 1229 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type); |
| 1230 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset); |
| 1231 | MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length); |
| 1232 | memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length); |
| 1233 | |
| 1234 | BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0); |
| 1235 | |
| 1236 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf, |
| 1237 | ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4), |
| 1238 | NULL, 0, NULL); |
| 1239 | if (rc) |
| 1240 | goto fail; |
| 1241 | |
| 1242 | return 0; |
| 1243 | |
| 1244 | fail: |
| 1245 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
| 1246 | return rc; |
| 1247 | } |
| 1248 | |
| 1249 | static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type, |
| 1250 | loff_t offset, size_t length) |
| 1251 | { |
| 1252 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN); |
| 1253 | int rc; |
| 1254 | |
| 1255 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type); |
| 1256 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset); |
| 1257 | MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length); |
| 1258 | |
| 1259 | BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0); |
| 1260 | |
| 1261 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf), |
| 1262 | NULL, 0, NULL); |
| 1263 | if (rc) |
| 1264 | goto fail; |
| 1265 | |
| 1266 | return 0; |
| 1267 | |
| 1268 | fail: |
| 1269 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
| 1270 | return rc; |
| 1271 | } |
| 1272 | |
| 1273 | static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type) |
| 1274 | { |
| 1275 | MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN); |
| 1276 | int rc; |
| 1277 | |
| 1278 | MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type); |
| 1279 | |
| 1280 | BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0); |
| 1281 | |
| 1282 | rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf), |
| 1283 | NULL, 0, NULL); |
| 1284 | if (rc) |
| 1285 | goto fail; |
| 1286 | |
| 1287 | return 0; |
| 1288 | |
| 1289 | fail: |
| 1290 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
| 1291 | return rc; |
| 1292 | } |
| 1293 | |
| 1294 | int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start, |
| 1295 | size_t len, size_t *retlen, u8 *buffer) |
| 1296 | { |
| 1297 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1298 | struct efx_nic *efx = mtd->priv; |
| 1299 | loff_t offset = start; |
| 1300 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 1301 | size_t chunk; |
| 1302 | int rc = 0; |
| 1303 | |
| 1304 | while (offset < end) { |
| 1305 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
| 1306 | rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset, |
| 1307 | buffer, chunk); |
| 1308 | if (rc) |
| 1309 | goto out; |
| 1310 | offset += chunk; |
| 1311 | buffer += chunk; |
| 1312 | } |
| 1313 | out: |
| 1314 | *retlen = offset - start; |
| 1315 | return rc; |
| 1316 | } |
| 1317 | |
| 1318 | int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len) |
| 1319 | { |
| 1320 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1321 | struct efx_nic *efx = mtd->priv; |
| 1322 | loff_t offset = start & ~((loff_t)(mtd->erasesize - 1)); |
| 1323 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 1324 | size_t chunk = part->common.mtd.erasesize; |
| 1325 | int rc = 0; |
| 1326 | |
| 1327 | if (!part->updating) { |
| 1328 | rc = efx_mcdi_nvram_update_start(efx, part->nvram_type); |
| 1329 | if (rc) |
| 1330 | goto out; |
| 1331 | part->updating = true; |
| 1332 | } |
| 1333 | |
| 1334 | /* The MCDI interface can in fact do multiple erase blocks at once; |
| 1335 | * but erasing may be slow, so we make multiple calls here to avoid |
| 1336 | * tripping the MCDI RPC timeout. */ |
| 1337 | while (offset < end) { |
| 1338 | rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset, |
| 1339 | chunk); |
| 1340 | if (rc) |
| 1341 | goto out; |
| 1342 | offset += chunk; |
| 1343 | } |
| 1344 | out: |
| 1345 | return rc; |
| 1346 | } |
| 1347 | |
| 1348 | int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start, |
| 1349 | size_t len, size_t *retlen, const u8 *buffer) |
| 1350 | { |
| 1351 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1352 | struct efx_nic *efx = mtd->priv; |
| 1353 | loff_t offset = start; |
| 1354 | loff_t end = min_t(loff_t, start + len, mtd->size); |
| 1355 | size_t chunk; |
| 1356 | int rc = 0; |
| 1357 | |
| 1358 | if (!part->updating) { |
| 1359 | rc = efx_mcdi_nvram_update_start(efx, part->nvram_type); |
| 1360 | if (rc) |
| 1361 | goto out; |
| 1362 | part->updating = true; |
| 1363 | } |
| 1364 | |
| 1365 | while (offset < end) { |
| 1366 | chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX); |
| 1367 | rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset, |
| 1368 | buffer, chunk); |
| 1369 | if (rc) |
| 1370 | goto out; |
| 1371 | offset += chunk; |
| 1372 | buffer += chunk; |
| 1373 | } |
| 1374 | out: |
| 1375 | *retlen = offset - start; |
| 1376 | return rc; |
| 1377 | } |
| 1378 | |
| 1379 | int efx_mcdi_mtd_sync(struct mtd_info *mtd) |
| 1380 | { |
| 1381 | struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd); |
| 1382 | struct efx_nic *efx = mtd->priv; |
| 1383 | int rc = 0; |
| 1384 | |
| 1385 | if (part->updating) { |
| 1386 | part->updating = false; |
| 1387 | rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type); |
| 1388 | } |
| 1389 | |
| 1390 | return rc; |
| 1391 | } |
| 1392 | |
| 1393 | void efx_mcdi_mtd_rename(struct efx_mtd_partition *part) |
| 1394 | { |
| 1395 | struct efx_mcdi_mtd_partition *mcdi_part = |
| 1396 | container_of(part, struct efx_mcdi_mtd_partition, common); |
| 1397 | struct efx_nic *efx = part->mtd.priv; |
| 1398 | |
| 1399 | snprintf(part->name, sizeof(part->name), "%s %s:%02x", |
| 1400 | efx->name, part->type_name, mcdi_part->fw_subtype); |
| 1401 | } |
| 1402 | |
| 1403 | #endif /* CONFIG_SFC_MTD */ |