1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2009-2013 Solarflare Communications Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, incorporated herein by reference.
11 * Driver for PHY related operations via MCDI.
14 #include <linux/slab.h>
17 #include "mcdi_pcol.h"
21 struct efx_mcdi_phy_data {
36 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
38 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_CFG_OUT_LEN);
42 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
43 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
45 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
46 outbuf, sizeof(outbuf), &outlen);
50 if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
55 cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
56 cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
58 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
59 cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
60 cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
61 cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
62 memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
64 cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
65 cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
66 memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
67 sizeof(cfg->revision));
72 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
76 static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
77 u32 flags, u32 loopback_mode,
80 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_LINK_IN_LEN);
83 BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
85 MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
86 MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
87 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
88 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
90 rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
95 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
97 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
101 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
102 outbuf, sizeof(outbuf), &outlen);
106 if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST +
107 MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) {
112 *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
117 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
121 static int efx_mcdi_mdio_read(struct net_device *net_dev,
122 int prtad, int devad, u16 addr)
124 struct efx_nic *efx = netdev_priv(net_dev);
125 MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
126 MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
130 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
131 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
132 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
133 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
135 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
136 outbuf, sizeof(outbuf), &outlen);
140 if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
141 MC_CMD_MDIO_STATUS_GOOD)
144 return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
147 static int efx_mcdi_mdio_write(struct net_device *net_dev,
148 int prtad, int devad, u16 addr, u16 value)
150 struct efx_nic *efx = netdev_priv(net_dev);
151 MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
152 MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
156 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
157 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
158 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
159 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
160 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
162 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
163 outbuf, sizeof(outbuf), &outlen);
167 if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
168 MC_CMD_MDIO_STATUS_GOOD)
174 static void mcdi_to_ethtool_linkset(u32 media, u32 cap, unsigned long *linkset)
176 #define SET_BIT(name) __set_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
179 bitmap_zero(linkset, __ETHTOOL_LINK_MODE_MASK_NBITS);
181 case MC_CMD_MEDIA_KX4:
183 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
184 SET_BIT(1000baseKX_Full);
185 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
186 SET_BIT(10000baseKX4_Full);
187 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
188 SET_BIT(40000baseKR4_Full);
191 case MC_CMD_MEDIA_XFP:
192 case MC_CMD_MEDIA_SFP_PLUS:
193 case MC_CMD_MEDIA_QSFP_PLUS:
195 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
196 SET_BIT(1000baseT_Full);
197 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
198 SET_BIT(10000baseT_Full);
199 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
200 SET_BIT(40000baseCR4_Full);
201 if (cap & (1 << MC_CMD_PHY_CAP_100000FDX_LBN))
202 SET_BIT(100000baseCR4_Full);
203 if (cap & (1 << MC_CMD_PHY_CAP_25000FDX_LBN))
204 SET_BIT(25000baseCR_Full);
205 if (cap & (1 << MC_CMD_PHY_CAP_50000FDX_LBN))
206 SET_BIT(50000baseCR2_Full);
209 case MC_CMD_MEDIA_BASE_T:
211 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
212 SET_BIT(10baseT_Half);
213 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
214 SET_BIT(10baseT_Full);
215 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
216 SET_BIT(100baseT_Half);
217 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
218 SET_BIT(100baseT_Full);
219 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
220 SET_BIT(1000baseT_Half);
221 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
222 SET_BIT(1000baseT_Full);
223 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
224 SET_BIT(10000baseT_Full);
228 if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
230 if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
232 if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
238 static u32 ethtool_linkset_to_mcdi_cap(const unsigned long *linkset)
242 #define TEST_BIT(name) test_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
245 if (TEST_BIT(10baseT_Half))
246 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
247 if (TEST_BIT(10baseT_Full))
248 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
249 if (TEST_BIT(100baseT_Half))
250 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
251 if (TEST_BIT(100baseT_Full))
252 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
253 if (TEST_BIT(1000baseT_Half))
254 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
255 if (TEST_BIT(1000baseT_Full) || TEST_BIT(1000baseKX_Full))
256 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
257 if (TEST_BIT(10000baseT_Full) || TEST_BIT(10000baseKX4_Full))
258 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
259 if (TEST_BIT(40000baseCR4_Full) || TEST_BIT(40000baseKR4_Full))
260 result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
261 if (TEST_BIT(100000baseCR4_Full))
262 result |= (1 << MC_CMD_PHY_CAP_100000FDX_LBN);
263 if (TEST_BIT(25000baseCR_Full))
264 result |= (1 << MC_CMD_PHY_CAP_25000FDX_LBN);
265 if (TEST_BIT(50000baseCR2_Full))
266 result |= (1 << MC_CMD_PHY_CAP_50000FDX_LBN);
268 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
269 if (TEST_BIT(Asym_Pause))
270 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
271 if (TEST_BIT(Autoneg))
272 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
279 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
281 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
282 enum efx_phy_mode mode, supported;
285 /* TODO: Advertise the capabilities supported by this PHY */
287 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
288 supported |= PHY_MODE_TX_DISABLED;
289 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
290 supported |= PHY_MODE_LOW_POWER;
291 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
292 supported |= PHY_MODE_OFF;
294 mode = efx->phy_mode & supported;
297 if (mode & PHY_MODE_TX_DISABLED)
298 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
299 if (mode & PHY_MODE_LOW_POWER)
300 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
301 if (mode & PHY_MODE_OFF)
302 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
307 static u8 mcdi_to_ethtool_media(u32 media)
310 case MC_CMD_MEDIA_XAUI:
311 case MC_CMD_MEDIA_CX4:
312 case MC_CMD_MEDIA_KX4:
315 case MC_CMD_MEDIA_XFP:
316 case MC_CMD_MEDIA_SFP_PLUS:
317 case MC_CMD_MEDIA_QSFP_PLUS:
320 case MC_CMD_MEDIA_BASE_T:
328 static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
329 struct efx_link_state *link_state,
330 u32 speed, u32 flags, u32 fcntl)
333 case MC_CMD_FCNTL_AUTO:
334 WARN_ON(1); /* This is not a link mode */
335 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
337 case MC_CMD_FCNTL_BIDIR:
338 link_state->fc = EFX_FC_TX | EFX_FC_RX;
340 case MC_CMD_FCNTL_RESPOND:
341 link_state->fc = EFX_FC_RX;
346 case MC_CMD_FCNTL_OFF:
351 link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
352 link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
353 link_state->speed = speed;
356 /* The semantics of the ethtool FEC mode bitmask are not well defined,
357 * particularly the meaning of combinations of bits. Which means we get to
358 * define our own semantics, as follows:
359 * OFF overrides any other bits, and means "disable all FEC" (with the
360 * exception of 25G KR4/CR4, where it is not possible to reject it if AN
361 * partner requests it).
362 * AUTO on its own means use cable requirements and link partner autoneg with
363 * fw-default preferences for the cable type.
364 * AUTO and either RS or BASER means use the specified FEC type if cable and
365 * link partner support it, otherwise autoneg/fw-default.
366 * RS or BASER alone means use the specified FEC type if cable and link partner
367 * support it and either requests it, otherwise no FEC.
368 * Both RS and BASER (whether AUTO or not) means use FEC if cable and link
369 * partner support it, preferring RS to BASER.
371 static u32 ethtool_fec_caps_to_mcdi(u32 ethtool_cap)
375 if (ethtool_cap & ETHTOOL_FEC_OFF)
378 if (ethtool_cap & ETHTOOL_FEC_AUTO)
379 ret |= (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
380 (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
381 (1 << MC_CMD_PHY_CAP_RS_FEC_LBN);
382 if (ethtool_cap & ETHTOOL_FEC_RS)
383 ret |= (1 << MC_CMD_PHY_CAP_RS_FEC_LBN) |
384 (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN);
385 if (ethtool_cap & ETHTOOL_FEC_BASER)
386 ret |= (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
387 (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
388 (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN) |
389 (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN);
393 /* Invert ethtool_fec_caps_to_mcdi. There are two combinations that function
394 * can never produce, (baser xor rs) and neither req; the implementation below
395 * maps both of those to AUTO. This should never matter, and it's not clear
396 * what a better mapping would be anyway.
398 static u32 mcdi_fec_caps_to_ethtool(u32 caps, bool is_25g)
400 bool rs = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_LBN),
401 rs_req = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN),
402 baser = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN)
403 : caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN),
404 baser_req = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN)
405 : caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN);
408 return ETHTOOL_FEC_OFF;
409 return (rs_req ? ETHTOOL_FEC_RS : 0) |
410 (baser_req ? ETHTOOL_FEC_BASER : 0) |
411 (baser == baser_req && rs == rs_req ? 0 : ETHTOOL_FEC_AUTO);
414 static int efx_mcdi_phy_probe(struct efx_nic *efx)
416 struct efx_mcdi_phy_data *phy_data;
417 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
421 /* Initialise and populate phy_data */
422 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
423 if (phy_data == NULL)
426 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
430 /* Read initial link advertisement */
431 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
432 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
433 outbuf, sizeof(outbuf), NULL);
437 /* Fill out nic state */
438 efx->phy_data = phy_data;
439 efx->phy_type = phy_data->type;
441 efx->mdio_bus = phy_data->channel;
442 efx->mdio.prtad = phy_data->port;
443 efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
444 efx->mdio.mode_support = 0;
445 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
446 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
447 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
448 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
450 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
451 if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
452 mcdi_to_ethtool_linkset(phy_data->media, caps,
453 efx->link_advertising);
455 phy_data->forced_cap = caps;
457 /* Assert that we can map efx -> mcdi loopback modes */
458 BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
459 BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
460 BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
461 BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
462 BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
463 BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
464 BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
465 BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
466 BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
467 BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
468 BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
469 BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
470 BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
471 BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
472 BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
473 BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
474 BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
475 BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
476 BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
477 BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
478 BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
479 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
480 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
481 BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
482 BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
483 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
484 BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
486 rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
489 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
490 * but by convention we don't */
491 efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
493 /* Set the initial link mode */
494 efx_mcdi_phy_decode_link(
495 efx, &efx->link_state,
496 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
497 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
498 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
500 /* Record the initial FEC configuration (or nearest approximation
501 * representable in the ethtool configuration space)
503 efx->fec_config = mcdi_fec_caps_to_ethtool(caps,
504 efx->link_state.speed == 25000 ||
505 efx->link_state.speed == 50000);
507 /* Default to Autonegotiated flow control if the PHY supports it */
508 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
509 if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
510 efx->wanted_fc |= EFX_FC_AUTO;
511 efx_link_set_wanted_fc(efx, efx->wanted_fc);
520 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
522 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
523 u32 caps = (efx->link_advertising[0] ?
524 ethtool_linkset_to_mcdi_cap(efx->link_advertising) :
525 phy_cfg->forced_cap);
527 caps |= ethtool_fec_caps_to_mcdi(efx->fec_config);
529 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
530 efx->loopback_mode, 0);
533 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
534 * supported by the link partner. Warn the user if this isn't the case
536 static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
538 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
541 /* The link partner capabilities are only relevant if the
542 * link supports flow control autonegotiation */
543 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
546 /* If flow control autoneg is supported and enabled, then fine */
547 if (efx->wanted_fc & EFX_FC_AUTO)
551 if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
552 rmtadv |= ADVERTISED_Pause;
553 if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
554 rmtadv |= ADVERTISED_Asym_Pause;
556 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
557 netif_err(efx, link, efx->net_dev,
558 "warning: link partner doesn't support pause frames");
561 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
563 struct efx_link_state old_state = efx->link_state;
564 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
567 WARN_ON(!mutex_is_locked(&efx->mac_lock));
569 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
571 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
572 outbuf, sizeof(outbuf), NULL);
574 efx->link_state.up = false;
576 efx_mcdi_phy_decode_link(
577 efx, &efx->link_state,
578 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
579 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
580 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
582 return !efx_link_state_equal(&efx->link_state, &old_state);
585 static void efx_mcdi_phy_remove(struct efx_nic *efx)
587 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
589 efx->phy_data = NULL;
593 static void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx,
594 struct ethtool_link_ksettings *cmd)
596 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
597 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
600 cmd->base.speed = efx->link_state.speed;
601 cmd->base.duplex = efx->link_state.fd;
602 cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
603 cmd->base.phy_address = phy_cfg->port;
604 cmd->base.autoneg = !!(efx->link_advertising[0] & ADVERTISED_Autoneg);
605 cmd->base.mdio_support = (efx->mdio.mode_support &
606 (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
608 mcdi_to_ethtool_linkset(phy_cfg->media, phy_cfg->supported_cap,
609 cmd->link_modes.supported);
610 memcpy(cmd->link_modes.advertising, efx->link_advertising,
611 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
613 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
614 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
615 outbuf, sizeof(outbuf), NULL);
618 mcdi_to_ethtool_linkset(phy_cfg->media,
619 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP),
620 cmd->link_modes.lp_advertising);
624 efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx,
625 const struct ethtool_link_ksettings *cmd)
627 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
631 if (cmd->base.autoneg) {
632 caps = (ethtool_linkset_to_mcdi_cap(cmd->link_modes.advertising) |
633 1 << MC_CMD_PHY_CAP_AN_LBN);
634 } else if (cmd->base.duplex) {
635 switch (cmd->base.speed) {
636 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
637 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
638 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
639 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
640 case 40000: caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN; break;
641 case 100000: caps = 1 << MC_CMD_PHY_CAP_100000FDX_LBN; break;
642 case 25000: caps = 1 << MC_CMD_PHY_CAP_25000FDX_LBN; break;
643 case 50000: caps = 1 << MC_CMD_PHY_CAP_50000FDX_LBN; break;
644 default: return -EINVAL;
647 switch (cmd->base.speed) {
648 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
649 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
650 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
651 default: return -EINVAL;
655 caps |= ethtool_fec_caps_to_mcdi(efx->fec_config);
657 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
658 efx->loopback_mode, 0);
662 if (cmd->base.autoneg) {
663 efx_link_set_advertising(efx, cmd->link_modes.advertising);
664 phy_cfg->forced_cap = 0;
666 efx_link_clear_advertising(efx);
667 phy_cfg->forced_cap = caps;
672 static int efx_mcdi_phy_get_fecparam(struct efx_nic *efx,
673 struct ethtool_fecparam *fec)
675 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_V2_LEN);
676 u32 caps, active, speed; /* MCDI format */
681 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
682 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
683 outbuf, sizeof(outbuf), &outlen);
686 if (outlen < MC_CMD_GET_LINK_OUT_V2_LEN)
689 /* behaviour for 25G/50G links depends on 25G BASER bit */
690 speed = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_LINK_SPEED);
691 is_25g = speed == 25000 || speed == 50000;
693 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_CAP);
694 fec->fec = mcdi_fec_caps_to_ethtool(caps, is_25g);
695 /* BASER is never supported on 100G */
697 fec->fec &= ~ETHTOOL_FEC_BASER;
699 active = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_FEC_TYPE);
701 case MC_CMD_FEC_NONE:
702 fec->active_fec = ETHTOOL_FEC_OFF;
704 case MC_CMD_FEC_BASER:
705 fec->active_fec = ETHTOOL_FEC_BASER;
708 fec->active_fec = ETHTOOL_FEC_RS;
711 netif_warn(efx, hw, efx->net_dev,
712 "Firmware reports unrecognised FEC_TYPE %u\n",
714 /* We don't know what firmware has picked. AUTO is as good a
715 * "can't happen" value as any other.
717 fec->active_fec = ETHTOOL_FEC_AUTO;
724 static int efx_mcdi_phy_set_fecparam(struct efx_nic *efx,
725 const struct ethtool_fecparam *fec)
727 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
731 /* Work out what efx_mcdi_phy_set_link_ksettings() would produce from
732 * saved advertising bits
734 if (test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, efx->link_advertising))
735 caps = (ethtool_linkset_to_mcdi_cap(efx->link_advertising) |
736 1 << MC_CMD_PHY_CAP_AN_LBN);
738 caps = phy_cfg->forced_cap;
740 caps |= ethtool_fec_caps_to_mcdi(fec->fec);
741 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
742 efx->loopback_mode, 0);
746 /* Record the new FEC setting for subsequent set_link calls */
747 efx->fec_config = fec->fec;
751 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
753 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
757 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
759 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
760 outbuf, sizeof(outbuf), &outlen);
764 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
766 if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
772 static const char *const mcdi_sft9001_cable_diag_names[] = {
773 "cable.pairA.length",
774 "cable.pairB.length",
775 "cable.pairC.length",
776 "cable.pairD.length",
777 "cable.pairA.status",
778 "cable.pairB.status",
779 "cable.pairC.status",
780 "cable.pairD.status",
783 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
786 unsigned int retry, i, count = 0;
789 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
790 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
794 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
795 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
796 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
797 inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
801 /* Wait up to 10s for BIST to finish */
802 for (retry = 0; retry < 100; ++retry) {
803 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
804 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
805 outbuf, sizeof(outbuf), &outlen);
809 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
810 if (status != MC_CMD_POLL_BIST_RUNNING)
820 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
822 /* SFT9001 specific cable diagnostics output */
823 if (efx->phy_type == PHY_TYPE_SFT9001B &&
824 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
825 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
826 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
827 if (status == MC_CMD_POLL_BIST_PASSED &&
828 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
829 for (i = 0; i < 8; i++) {
831 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
843 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
846 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
850 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
851 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
858 /* If we support both LONG and SHORT, then run each in response to
859 * break or not. Otherwise, run the one we support */
861 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
862 if ((flags & ETH_TEST_FL_OFFLINE) &&
864 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
865 mode = MC_CMD_PHY_BIST_CABLE_LONG;
867 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
868 } else if (phy_cfg->flags &
869 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
870 mode = MC_CMD_PHY_BIST_CABLE_LONG;
873 rc = efx_mcdi_bist(efx, mode, results);
882 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
885 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
887 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
893 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
894 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
899 if (efx->phy_type == PHY_TYPE_SFT9001B) {
900 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
901 return mcdi_sft9001_cable_diag_names[index];
902 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
909 #define SFP_PAGE_SIZE 128
910 #define SFF_DIAG_TYPE_OFFSET 92
911 #define SFF_DIAG_ADDR_CHANGE BIT(2)
912 #define SFF_8079_NUM_PAGES 2
913 #define SFF_8472_NUM_PAGES 4
914 #define SFF_8436_NUM_PAGES 5
915 #define SFF_DMT_LEVEL_OFFSET 94
917 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
919 * @page: EEPROM page number
920 * @data: Destination data pointer
921 * @offset: Offset in page to copy from in to data
922 * @space: Space available in data
925 * >=0 - amount of data copied
928 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
930 u8 *data, ssize_t offset,
933 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
934 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
936 unsigned int payload_len;
937 unsigned int to_copy;
940 if (offset > SFP_PAGE_SIZE)
943 to_copy = min(space, SFP_PAGE_SIZE - offset);
945 MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
946 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
947 inbuf, sizeof(inbuf),
948 outbuf, sizeof(outbuf),
954 if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
958 payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
959 if (payload_len != SFP_PAGE_SIZE)
962 memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
968 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
975 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
982 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
984 /* Page zero of the EEPROM includes the diagnostic type at byte 92. */
985 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
986 SFF_DIAG_TYPE_OFFSET);
989 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
991 /* Page zero of the EEPROM includes the DMT level at byte 94. */
992 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
993 SFF_DMT_LEVEL_OFFSET);
996 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
998 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
1000 if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
1001 return phy_data->media;
1003 /* A QSFP+ NIC may actually have an SFP+ module attached.
1004 * The ID is page 0, byte 0.
1006 switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
1008 return MC_CMD_MEDIA_SFP_PLUS;
1011 return MC_CMD_MEDIA_QSFP_PLUS;
1017 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
1018 struct ethtool_eeprom *ee, u8 *data)
1021 ssize_t space_remaining = ee->len;
1022 unsigned int page_off;
1023 bool ignore_missing;
1027 switch (efx_mcdi_phy_module_type(efx)) {
1028 case MC_CMD_MEDIA_SFP_PLUS:
1029 num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
1030 SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
1032 ignore_missing = false;
1034 case MC_CMD_MEDIA_QSFP_PLUS:
1035 num_pages = SFF_8436_NUM_PAGES;
1036 page = -1; /* We obtain the lower page by asking for -1. */
1037 ignore_missing = true; /* Ignore missing pages after page 0. */
1043 page_off = ee->offset % SFP_PAGE_SIZE;
1044 page += ee->offset / SFP_PAGE_SIZE;
1046 while (space_remaining && (page < num_pages)) {
1047 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
1052 space_remaining -= rc;
1056 } else if (rc == 0) {
1057 space_remaining = 0;
1058 } else if (ignore_missing && (page > 0)) {
1059 int intended_size = SFP_PAGE_SIZE - page_off;
1061 space_remaining -= intended_size;
1062 if (space_remaining < 0) {
1063 space_remaining = 0;
1065 memset(data, 0, intended_size);
1066 data += intended_size;
1079 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
1080 struct ethtool_modinfo *modinfo)
1085 switch (efx_mcdi_phy_module_type(efx)) {
1086 case MC_CMD_MEDIA_SFP_PLUS:
1087 sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
1089 /* If we can't read the diagnostics level we have none. */
1090 if (sff_8472_level < 0)
1093 /* Check if this module requires the (unsupported) address
1096 diag_type = efx_mcdi_phy_diag_type(efx);
1098 if ((sff_8472_level == 0) ||
1099 (diag_type & SFF_DIAG_ADDR_CHANGE)) {
1100 modinfo->type = ETH_MODULE_SFF_8079;
1101 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1103 modinfo->type = ETH_MODULE_SFF_8472;
1104 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1108 case MC_CMD_MEDIA_QSFP_PLUS:
1109 modinfo->type = ETH_MODULE_SFF_8436;
1110 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1120 static const struct efx_phy_operations efx_mcdi_phy_ops = {
1121 .probe = efx_mcdi_phy_probe,
1122 .init = efx_port_dummy_op_int,
1123 .reconfigure = efx_mcdi_port_reconfigure,
1124 .poll = efx_mcdi_phy_poll,
1125 .fini = efx_port_dummy_op_void,
1126 .remove = efx_mcdi_phy_remove,
1127 .get_link_ksettings = efx_mcdi_phy_get_link_ksettings,
1128 .set_link_ksettings = efx_mcdi_phy_set_link_ksettings,
1129 .get_fecparam = efx_mcdi_phy_get_fecparam,
1130 .set_fecparam = efx_mcdi_phy_set_fecparam,
1131 .test_alive = efx_mcdi_phy_test_alive,
1132 .run_tests = efx_mcdi_phy_run_tests,
1133 .test_name = efx_mcdi_phy_test_name,
1134 .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
1135 .get_module_info = efx_mcdi_phy_get_module_info,
1138 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
1140 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
1142 return phy_data->supported_cap;
1145 static unsigned int efx_mcdi_event_link_speed[] = {
1146 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
1147 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
1148 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
1149 [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
1150 [MCDI_EVENT_LINKCHANGE_SPEED_25G] = 25000,
1151 [MCDI_EVENT_LINKCHANGE_SPEED_50G] = 50000,
1152 [MCDI_EVENT_LINKCHANGE_SPEED_100G] = 100000,
1155 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
1157 u32 flags, fcntl, speed, lpa;
1159 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
1160 EFX_WARN_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
1161 speed = efx_mcdi_event_link_speed[speed];
1163 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
1164 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
1165 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
1167 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
1168 * which is only run after flushing the event queues. Therefore, it
1169 * is safe to modify the link state outside of the mac_lock here.
1171 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
1173 efx_mcdi_phy_check_fcntl(efx, lpa);
1175 efx_link_status_changed(efx);
1178 int efx_mcdi_set_mac(struct efx_nic *efx)
1181 MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
1183 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1185 /* This has no effect on EF10 */
1186 ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
1187 efx->net_dev->dev_addr);
1189 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
1190 EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
1191 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
1193 /* Set simple MAC filter for Siena */
1194 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
1195 SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
1197 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_FLAGS,
1198 SET_MAC_IN_FLAG_INCLUDE_FCS,
1199 !!(efx->net_dev->features & NETIF_F_RXFCS));
1201 switch (efx->wanted_fc) {
1202 case EFX_FC_RX | EFX_FC_TX:
1203 fcntl = MC_CMD_FCNTL_BIDIR;
1206 fcntl = MC_CMD_FCNTL_RESPOND;
1209 fcntl = MC_CMD_FCNTL_OFF;
1212 if (efx->wanted_fc & EFX_FC_AUTO)
1213 fcntl = MC_CMD_FCNTL_AUTO;
1214 if (efx->fc_disable)
1215 fcntl = MC_CMD_FCNTL_OFF;
1217 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
1219 return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
1223 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
1225 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
1229 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
1231 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
1232 outbuf, sizeof(outbuf), &outlength);
1236 return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
1239 enum efx_stats_action {
1245 static int efx_mcdi_mac_stats(struct efx_nic *efx,
1246 enum efx_stats_action action, int clear)
1248 MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
1250 int change = action == EFX_STATS_PULL ? 0 : 1;
1251 int enable = action == EFX_STATS_ENABLE ? 1 : 0;
1252 int period = action == EFX_STATS_ENABLE ? 1000 : 0;
1253 dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
1254 u32 dma_len = action != EFX_STATS_DISABLE ?
1255 efx->num_mac_stats * sizeof(u64) : 0;
1257 BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
1259 MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
1260 MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
1261 MAC_STATS_IN_DMA, !!enable,
1262 MAC_STATS_IN_CLEAR, clear,
1263 MAC_STATS_IN_PERIODIC_CHANGE, change,
1264 MAC_STATS_IN_PERIODIC_ENABLE, enable,
1265 MAC_STATS_IN_PERIODIC_CLEAR, 0,
1266 MAC_STATS_IN_PERIODIC_NOEVENT, 1,
1267 MAC_STATS_IN_PERIOD_MS, period);
1268 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
1270 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1271 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1273 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
1276 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
1278 /* Expect ENOENT if DMA queues have not been set up */
1279 if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
1280 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
1285 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
1287 __le64 *dma_stats = efx->stats_buffer.addr;
1289 dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1291 efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
1294 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
1296 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
1299 #define EFX_MAC_STATS_WAIT_US 100
1300 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
1302 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
1304 __le64 *dma_stats = efx->stats_buffer.addr;
1305 int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
1307 dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1308 efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
1310 while (dma_stats[efx->num_mac_stats - 1] ==
1311 EFX_MC_STATS_GENERATION_INVALID &&
1313 udelay(EFX_MAC_STATS_WAIT_US);
1316 int efx_mcdi_port_probe(struct efx_nic *efx)
1320 /* Hook in PHY operations table */
1321 efx->phy_op = &efx_mcdi_phy_ops;
1323 /* Set up MDIO structure for PHY */
1324 efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
1325 efx->mdio.mdio_read = efx_mcdi_mdio_read;
1326 efx->mdio.mdio_write = efx_mcdi_mdio_write;
1328 /* Fill out MDIO structure, loopback modes, and initial link state */
1329 rc = efx->phy_op->probe(efx);
1333 /* Allocate buffer for stats */
1334 rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
1335 efx->num_mac_stats * sizeof(u64), GFP_KERNEL);
1338 netif_dbg(efx, probe, efx->net_dev,
1339 "stats buffer at %llx (virt %p phys %llx)\n",
1340 (u64)efx->stats_buffer.dma_addr,
1341 efx->stats_buffer.addr,
1342 (u64)virt_to_phys(efx->stats_buffer.addr));
1344 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
1349 void efx_mcdi_port_remove(struct efx_nic *efx)
1351 efx->phy_op->remove(efx);
1352 efx_nic_free_buffer(efx, &efx->stats_buffer);
1355 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
1356 int efx_mcdi_port_get_number(struct efx_nic *efx)
1358 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
1361 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
1362 outbuf, sizeof(outbuf), NULL);
1366 return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);