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 u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
179 case MC_CMD_MEDIA_KX4:
180 result |= SUPPORTED_Backplane;
181 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
182 result |= SUPPORTED_1000baseKX_Full;
183 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
184 result |= SUPPORTED_10000baseKX4_Full;
185 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
186 result |= SUPPORTED_40000baseKR4_Full;
189 case MC_CMD_MEDIA_XFP:
190 case MC_CMD_MEDIA_SFP_PLUS:
191 case MC_CMD_MEDIA_QSFP_PLUS:
192 result |= SUPPORTED_FIBRE;
193 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
194 result |= SUPPORTED_1000baseT_Full;
195 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
196 result |= SUPPORTED_10000baseT_Full;
197 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
198 result |= SUPPORTED_40000baseCR4_Full;
201 case MC_CMD_MEDIA_BASE_T:
202 result |= SUPPORTED_TP;
203 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
204 result |= SUPPORTED_10baseT_Half;
205 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
206 result |= SUPPORTED_10baseT_Full;
207 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
208 result |= SUPPORTED_100baseT_Half;
209 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
210 result |= SUPPORTED_100baseT_Full;
211 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
212 result |= SUPPORTED_1000baseT_Half;
213 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
214 result |= SUPPORTED_1000baseT_Full;
215 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
216 result |= SUPPORTED_10000baseT_Full;
220 if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
221 result |= SUPPORTED_Pause;
222 if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
223 result |= SUPPORTED_Asym_Pause;
224 if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
225 result |= SUPPORTED_Autoneg;
230 static u32 ethtool_to_mcdi_cap(u32 cap)
234 if (cap & SUPPORTED_10baseT_Half)
235 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
236 if (cap & SUPPORTED_10baseT_Full)
237 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
238 if (cap & SUPPORTED_100baseT_Half)
239 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
240 if (cap & SUPPORTED_100baseT_Full)
241 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
242 if (cap & SUPPORTED_1000baseT_Half)
243 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
244 if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
245 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
246 if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
247 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
248 if (cap & (SUPPORTED_40000baseCR4_Full | SUPPORTED_40000baseKR4_Full))
249 result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
250 if (cap & SUPPORTED_Pause)
251 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
252 if (cap & SUPPORTED_Asym_Pause)
253 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
254 if (cap & SUPPORTED_Autoneg)
255 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
260 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
262 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
263 enum efx_phy_mode mode, supported;
266 /* TODO: Advertise the capabilities supported by this PHY */
268 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
269 supported |= PHY_MODE_TX_DISABLED;
270 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
271 supported |= PHY_MODE_LOW_POWER;
272 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
273 supported |= PHY_MODE_OFF;
275 mode = efx->phy_mode & supported;
278 if (mode & PHY_MODE_TX_DISABLED)
279 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
280 if (mode & PHY_MODE_LOW_POWER)
281 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
282 if (mode & PHY_MODE_OFF)
283 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
288 static u32 mcdi_to_ethtool_media(u32 media)
291 case MC_CMD_MEDIA_XAUI:
292 case MC_CMD_MEDIA_CX4:
293 case MC_CMD_MEDIA_KX4:
296 case MC_CMD_MEDIA_XFP:
297 case MC_CMD_MEDIA_SFP_PLUS:
298 case MC_CMD_MEDIA_QSFP_PLUS:
301 case MC_CMD_MEDIA_BASE_T:
309 static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
310 struct efx_link_state *link_state,
311 u32 speed, u32 flags, u32 fcntl)
314 case MC_CMD_FCNTL_AUTO:
315 WARN_ON(1); /* This is not a link mode */
316 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
318 case MC_CMD_FCNTL_BIDIR:
319 link_state->fc = EFX_FC_TX | EFX_FC_RX;
321 case MC_CMD_FCNTL_RESPOND:
322 link_state->fc = EFX_FC_RX;
326 case MC_CMD_FCNTL_OFF:
331 link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
332 link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
333 link_state->speed = speed;
336 static int efx_mcdi_phy_probe(struct efx_nic *efx)
338 struct efx_mcdi_phy_data *phy_data;
339 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
343 /* Initialise and populate phy_data */
344 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
345 if (phy_data == NULL)
348 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
352 /* Read initial link advertisement */
353 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
354 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
355 outbuf, sizeof(outbuf), NULL);
359 /* Fill out nic state */
360 efx->phy_data = phy_data;
361 efx->phy_type = phy_data->type;
363 efx->mdio_bus = phy_data->channel;
364 efx->mdio.prtad = phy_data->port;
365 efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
366 efx->mdio.mode_support = 0;
367 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
368 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
369 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
370 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
372 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
373 if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
374 efx->link_advertising =
375 mcdi_to_ethtool_cap(phy_data->media, caps);
377 phy_data->forced_cap = caps;
379 /* Assert that we can map efx -> mcdi loopback modes */
380 BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
381 BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
382 BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
383 BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
384 BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
385 BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
386 BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
387 BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
388 BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
389 BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
390 BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
391 BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
392 BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
393 BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
394 BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
395 BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
396 BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
397 BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
398 BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
399 BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
400 BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
401 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
402 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
403 BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
404 BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
405 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
406 BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
408 rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
411 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
412 * but by convention we don't */
413 efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
415 /* Set the initial link mode */
416 efx_mcdi_phy_decode_link(
417 efx, &efx->link_state,
418 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
419 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
420 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
422 /* Default to Autonegotiated flow control if the PHY supports it */
423 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
424 if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
425 efx->wanted_fc |= EFX_FC_AUTO;
426 efx_link_set_wanted_fc(efx, efx->wanted_fc);
435 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
437 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
438 u32 caps = (efx->link_advertising ?
439 ethtool_to_mcdi_cap(efx->link_advertising) :
440 phy_cfg->forced_cap);
442 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
443 efx->loopback_mode, 0);
446 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
447 * supported by the link partner. Warn the user if this isn't the case
449 static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
451 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
454 /* The link partner capabilities are only relevant if the
455 * link supports flow control autonegotiation */
456 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
459 /* If flow control autoneg is supported and enabled, then fine */
460 if (efx->wanted_fc & EFX_FC_AUTO)
464 if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
465 rmtadv |= ADVERTISED_Pause;
466 if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
467 rmtadv |= ADVERTISED_Asym_Pause;
469 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
470 netif_err(efx, link, efx->net_dev,
471 "warning: link partner doesn't support pause frames");
474 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
476 struct efx_link_state old_state = efx->link_state;
477 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
480 WARN_ON(!mutex_is_locked(&efx->mac_lock));
482 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
484 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
485 outbuf, sizeof(outbuf), NULL);
487 efx->link_state.up = false;
489 efx_mcdi_phy_decode_link(
490 efx, &efx->link_state,
491 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
492 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
493 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
495 return !efx_link_state_equal(&efx->link_state, &old_state);
498 static void efx_mcdi_phy_remove(struct efx_nic *efx)
500 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
502 efx->phy_data = NULL;
506 static void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx,
507 struct ethtool_link_ksettings *cmd)
509 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
510 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
512 u32 supported, advertising, lp_advertising;
514 supported = mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
515 advertising = efx->link_advertising;
516 cmd->base.speed = efx->link_state.speed;
517 cmd->base.duplex = efx->link_state.fd;
518 cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
519 cmd->base.phy_address = phy_cfg->port;
520 cmd->base.autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
521 cmd->base.mdio_support = (efx->mdio.mode_support &
522 (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
524 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
526 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
529 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
530 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
531 outbuf, sizeof(outbuf), NULL);
535 mcdi_to_ethtool_cap(phy_cfg->media,
536 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
538 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
543 efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx,
544 const struct ethtool_link_ksettings *cmd)
546 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
551 ethtool_convert_link_mode_to_legacy_u32(&advertising,
552 cmd->link_modes.advertising);
554 if (cmd->base.autoneg) {
555 caps = (ethtool_to_mcdi_cap(advertising) |
556 1 << MC_CMD_PHY_CAP_AN_LBN);
557 } else if (cmd->base.duplex) {
558 switch (cmd->base.speed) {
559 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
560 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
561 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
562 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
563 case 40000: caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN; break;
564 default: return -EINVAL;
567 switch (cmd->base.speed) {
568 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
569 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
570 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
571 default: return -EINVAL;
575 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
576 efx->loopback_mode, 0);
580 if (cmd->base.autoneg) {
581 efx_link_set_advertising(
582 efx, advertising | ADVERTISED_Autoneg);
583 phy_cfg->forced_cap = 0;
585 efx_link_set_advertising(efx, 0);
586 phy_cfg->forced_cap = caps;
591 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
593 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
597 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
599 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
600 outbuf, sizeof(outbuf), &outlen);
604 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
606 if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
612 static const char *const mcdi_sft9001_cable_diag_names[] = {
613 "cable.pairA.length",
614 "cable.pairB.length",
615 "cable.pairC.length",
616 "cable.pairD.length",
617 "cable.pairA.status",
618 "cable.pairB.status",
619 "cable.pairC.status",
620 "cable.pairD.status",
623 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
626 unsigned int retry, i, count = 0;
629 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
630 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
634 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
635 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
636 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
637 inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
641 /* Wait up to 10s for BIST to finish */
642 for (retry = 0; retry < 100; ++retry) {
643 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
644 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
645 outbuf, sizeof(outbuf), &outlen);
649 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
650 if (status != MC_CMD_POLL_BIST_RUNNING)
660 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
662 /* SFT9001 specific cable diagnostics output */
663 if (efx->phy_type == PHY_TYPE_SFT9001B &&
664 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
665 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
666 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
667 if (status == MC_CMD_POLL_BIST_PASSED &&
668 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
669 for (i = 0; i < 8; i++) {
671 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
683 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
686 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
690 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
691 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
698 /* If we support both LONG and SHORT, then run each in response to
699 * break or not. Otherwise, run the one we support */
701 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
702 if ((flags & ETH_TEST_FL_OFFLINE) &&
704 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
705 mode = MC_CMD_PHY_BIST_CABLE_LONG;
707 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
708 } else if (phy_cfg->flags &
709 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
710 mode = MC_CMD_PHY_BIST_CABLE_LONG;
713 rc = efx_mcdi_bist(efx, mode, results);
722 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
725 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
727 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
733 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
734 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
739 if (efx->phy_type == PHY_TYPE_SFT9001B) {
740 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
741 return mcdi_sft9001_cable_diag_names[index];
742 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
749 #define SFP_PAGE_SIZE 128
750 #define SFF_DIAG_TYPE_OFFSET 92
751 #define SFF_DIAG_ADDR_CHANGE BIT(2)
752 #define SFF_8079_NUM_PAGES 2
753 #define SFF_8472_NUM_PAGES 4
754 #define SFF_8436_NUM_PAGES 5
755 #define SFF_DMT_LEVEL_OFFSET 94
757 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
759 * @page: EEPROM page number
760 * @data: Destination data pointer
761 * @offset: Offset in page to copy from in to data
762 * @space: Space available in data
765 * >=0 - amount of data copied
768 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
770 u8 *data, ssize_t offset,
773 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
774 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
776 unsigned int payload_len;
777 unsigned int to_copy;
780 if (offset > SFP_PAGE_SIZE)
783 to_copy = min(space, SFP_PAGE_SIZE - offset);
785 MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
786 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
787 inbuf, sizeof(inbuf),
788 outbuf, sizeof(outbuf),
794 if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
798 payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
799 if (payload_len != SFP_PAGE_SIZE)
802 memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
808 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
815 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
822 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
824 /* Page zero of the EEPROM includes the diagnostic type at byte 92. */
825 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
826 SFF_DIAG_TYPE_OFFSET);
829 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
831 /* Page zero of the EEPROM includes the DMT level at byte 94. */
832 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
833 SFF_DMT_LEVEL_OFFSET);
836 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
838 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
840 if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
841 return phy_data->media;
843 /* A QSFP+ NIC may actually have an SFP+ module attached.
844 * The ID is page 0, byte 0.
846 switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
848 return MC_CMD_MEDIA_SFP_PLUS;
851 return MC_CMD_MEDIA_QSFP_PLUS;
857 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
858 struct ethtool_eeprom *ee, u8 *data)
861 ssize_t space_remaining = ee->len;
862 unsigned int page_off;
867 switch (efx_mcdi_phy_module_type(efx)) {
868 case MC_CMD_MEDIA_SFP_PLUS:
869 num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
870 SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
872 ignore_missing = false;
874 case MC_CMD_MEDIA_QSFP_PLUS:
875 num_pages = SFF_8436_NUM_PAGES;
876 page = -1; /* We obtain the lower page by asking for -1. */
877 ignore_missing = true; /* Ignore missing pages after page 0. */
883 page_off = ee->offset % SFP_PAGE_SIZE;
884 page += ee->offset / SFP_PAGE_SIZE;
886 while (space_remaining && (page < num_pages)) {
887 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
892 space_remaining -= rc;
896 } else if (rc == 0) {
898 } else if (ignore_missing && (page > 0)) {
899 int intended_size = SFP_PAGE_SIZE - page_off;
901 space_remaining -= intended_size;
902 if (space_remaining < 0) {
905 memset(data, 0, intended_size);
906 data += intended_size;
919 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
920 struct ethtool_modinfo *modinfo)
925 switch (efx_mcdi_phy_module_type(efx)) {
926 case MC_CMD_MEDIA_SFP_PLUS:
927 sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
929 /* If we can't read the diagnostics level we have none. */
930 if (sff_8472_level < 0)
933 /* Check if this module requires the (unsupported) address
936 diag_type = efx_mcdi_phy_diag_type(efx);
938 if ((sff_8472_level == 0) ||
939 (diag_type & SFF_DIAG_ADDR_CHANGE)) {
940 modinfo->type = ETH_MODULE_SFF_8079;
941 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
943 modinfo->type = ETH_MODULE_SFF_8472;
944 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
948 case MC_CMD_MEDIA_QSFP_PLUS:
949 modinfo->type = ETH_MODULE_SFF_8436;
950 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
960 static const struct efx_phy_operations efx_mcdi_phy_ops = {
961 .probe = efx_mcdi_phy_probe,
962 .init = efx_port_dummy_op_int,
963 .reconfigure = efx_mcdi_port_reconfigure,
964 .poll = efx_mcdi_phy_poll,
965 .fini = efx_port_dummy_op_void,
966 .remove = efx_mcdi_phy_remove,
967 .get_link_ksettings = efx_mcdi_phy_get_link_ksettings,
968 .set_link_ksettings = efx_mcdi_phy_set_link_ksettings,
969 .test_alive = efx_mcdi_phy_test_alive,
970 .run_tests = efx_mcdi_phy_run_tests,
971 .test_name = efx_mcdi_phy_test_name,
972 .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
973 .get_module_info = efx_mcdi_phy_get_module_info,
976 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
978 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
980 return phy_data->supported_cap;
983 static unsigned int efx_mcdi_event_link_speed[] = {
984 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
985 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
986 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
987 [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
990 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
992 u32 flags, fcntl, speed, lpa;
994 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
995 EFX_WARN_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
996 speed = efx_mcdi_event_link_speed[speed];
998 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
999 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
1000 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
1002 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
1003 * which is only run after flushing the event queues. Therefore, it
1004 * is safe to modify the link state outside of the mac_lock here.
1006 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
1008 efx_mcdi_phy_check_fcntl(efx, lpa);
1010 efx_link_status_changed(efx);
1013 int efx_mcdi_set_mac(struct efx_nic *efx)
1016 MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
1018 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1020 /* This has no effect on EF10 */
1021 ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
1022 efx->net_dev->dev_addr);
1024 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
1025 EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
1026 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
1028 /* Set simple MAC filter for Siena */
1029 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
1030 SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
1032 switch (efx->wanted_fc) {
1033 case EFX_FC_RX | EFX_FC_TX:
1034 fcntl = MC_CMD_FCNTL_BIDIR;
1037 fcntl = MC_CMD_FCNTL_RESPOND;
1040 fcntl = MC_CMD_FCNTL_OFF;
1043 if (efx->wanted_fc & EFX_FC_AUTO)
1044 fcntl = MC_CMD_FCNTL_AUTO;
1045 if (efx->fc_disable)
1046 fcntl = MC_CMD_FCNTL_OFF;
1048 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
1050 return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
1054 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
1056 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
1060 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
1062 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
1063 outbuf, sizeof(outbuf), &outlength);
1067 return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
1070 enum efx_stats_action {
1076 static int efx_mcdi_mac_stats(struct efx_nic *efx,
1077 enum efx_stats_action action, int clear)
1079 MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
1081 int change = action == EFX_STATS_PULL ? 0 : 1;
1082 int enable = action == EFX_STATS_ENABLE ? 1 : 0;
1083 int period = action == EFX_STATS_ENABLE ? 1000 : 0;
1084 dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
1085 u32 dma_len = action != EFX_STATS_DISABLE ?
1086 MC_CMD_MAC_NSTATS * sizeof(u64) : 0;
1088 BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
1090 MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
1091 MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
1092 MAC_STATS_IN_DMA, !!enable,
1093 MAC_STATS_IN_CLEAR, clear,
1094 MAC_STATS_IN_PERIODIC_CHANGE, change,
1095 MAC_STATS_IN_PERIODIC_ENABLE, enable,
1096 MAC_STATS_IN_PERIODIC_CLEAR, 0,
1097 MAC_STATS_IN_PERIODIC_NOEVENT, 1,
1098 MAC_STATS_IN_PERIOD_MS, period);
1099 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
1101 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1102 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1104 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
1107 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
1109 /* Expect ENOENT if DMA queues have not been set up */
1110 if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
1111 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
1116 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
1118 __le64 *dma_stats = efx->stats_buffer.addr;
1120 dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
1122 efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
1125 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
1127 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
1130 #define EFX_MAC_STATS_WAIT_US 100
1131 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
1133 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
1135 __le64 *dma_stats = efx->stats_buffer.addr;
1136 int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
1138 dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
1139 efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
1141 while (dma_stats[MC_CMD_MAC_GENERATION_END] ==
1142 EFX_MC_STATS_GENERATION_INVALID &&
1144 udelay(EFX_MAC_STATS_WAIT_US);
1147 int efx_mcdi_port_probe(struct efx_nic *efx)
1151 /* Hook in PHY operations table */
1152 efx->phy_op = &efx_mcdi_phy_ops;
1154 /* Set up MDIO structure for PHY */
1155 efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
1156 efx->mdio.mdio_read = efx_mcdi_mdio_read;
1157 efx->mdio.mdio_write = efx_mcdi_mdio_write;
1159 /* Fill out MDIO structure, loopback modes, and initial link state */
1160 rc = efx->phy_op->probe(efx);
1164 /* Allocate buffer for stats */
1165 rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
1166 MC_CMD_MAC_NSTATS * sizeof(u64), GFP_KERNEL);
1169 netif_dbg(efx, probe, efx->net_dev,
1170 "stats buffer at %llx (virt %p phys %llx)\n",
1171 (u64)efx->stats_buffer.dma_addr,
1172 efx->stats_buffer.addr,
1173 (u64)virt_to_phys(efx->stats_buffer.addr));
1175 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
1180 void efx_mcdi_port_remove(struct efx_nic *efx)
1182 efx->phy_op->remove(efx);
1183 efx_nic_free_buffer(efx, &efx->stats_buffer);
1186 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
1187 int efx_mcdi_port_get_number(struct efx_nic *efx)
1189 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
1192 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
1193 outbuf, sizeof(outbuf), NULL);
1197 return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);