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>
18 #include "mcdi_pcol.h"
22 struct efx_mcdi_phy_data {
37 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
39 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_CFG_OUT_LEN);
43 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
44 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
46 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
47 outbuf, sizeof(outbuf), &outlen);
51 if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
56 cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
57 cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
59 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
60 cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
61 cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
62 cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
63 memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
65 cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
66 cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
67 memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
68 sizeof(cfg->revision));
73 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
77 static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
78 u32 flags, u32 loopback_mode,
81 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_LINK_IN_LEN);
84 BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
86 MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
87 MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
88 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
89 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
91 rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
96 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
98 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
102 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
103 outbuf, sizeof(outbuf), &outlen);
107 if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST +
108 MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) {
113 *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
118 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
122 static int efx_mcdi_mdio_read(struct net_device *net_dev,
123 int prtad, int devad, u16 addr)
125 struct efx_nic *efx = netdev_priv(net_dev);
126 MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
127 MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
131 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
132 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
133 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
134 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
136 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
137 outbuf, sizeof(outbuf), &outlen);
141 if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
142 MC_CMD_MDIO_STATUS_GOOD)
145 return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
148 static int efx_mcdi_mdio_write(struct net_device *net_dev,
149 int prtad, int devad, u16 addr, u16 value)
151 struct efx_nic *efx = netdev_priv(net_dev);
152 MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
153 MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
157 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
158 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
159 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
160 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
161 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
163 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
164 outbuf, sizeof(outbuf), &outlen);
168 if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
169 MC_CMD_MDIO_STATUS_GOOD)
175 static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
180 case MC_CMD_MEDIA_KX4:
181 result |= SUPPORTED_Backplane;
182 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
183 result |= SUPPORTED_1000baseKX_Full;
184 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
185 result |= SUPPORTED_10000baseKX4_Full;
186 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
187 result |= SUPPORTED_40000baseKR4_Full;
190 case MC_CMD_MEDIA_XFP:
191 case MC_CMD_MEDIA_SFP_PLUS:
192 case MC_CMD_MEDIA_QSFP_PLUS:
193 result |= SUPPORTED_FIBRE;
194 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
195 result |= SUPPORTED_1000baseT_Full;
196 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
197 result |= SUPPORTED_10000baseT_Full;
198 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
199 result |= SUPPORTED_40000baseCR4_Full;
202 case MC_CMD_MEDIA_BASE_T:
203 result |= SUPPORTED_TP;
204 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
205 result |= SUPPORTED_10baseT_Half;
206 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
207 result |= SUPPORTED_10baseT_Full;
208 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
209 result |= SUPPORTED_100baseT_Half;
210 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
211 result |= SUPPORTED_100baseT_Full;
212 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
213 result |= SUPPORTED_1000baseT_Half;
214 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
215 result |= SUPPORTED_1000baseT_Full;
216 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
217 result |= SUPPORTED_10000baseT_Full;
221 if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
222 result |= SUPPORTED_Pause;
223 if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
224 result |= SUPPORTED_Asym_Pause;
225 if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
226 result |= SUPPORTED_Autoneg;
231 static u32 ethtool_to_mcdi_cap(u32 cap)
235 if (cap & SUPPORTED_10baseT_Half)
236 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
237 if (cap & SUPPORTED_10baseT_Full)
238 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
239 if (cap & SUPPORTED_100baseT_Half)
240 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
241 if (cap & SUPPORTED_100baseT_Full)
242 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
243 if (cap & SUPPORTED_1000baseT_Half)
244 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
245 if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
246 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
247 if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
248 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
249 if (cap & (SUPPORTED_40000baseCR4_Full | SUPPORTED_40000baseKR4_Full))
250 result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
251 if (cap & SUPPORTED_Pause)
252 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
253 if (cap & SUPPORTED_Asym_Pause)
254 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
255 if (cap & SUPPORTED_Autoneg)
256 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
261 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
263 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
264 enum efx_phy_mode mode, supported;
267 /* TODO: Advertise the capabilities supported by this PHY */
269 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
270 supported |= PHY_MODE_TX_DISABLED;
271 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
272 supported |= PHY_MODE_LOW_POWER;
273 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
274 supported |= PHY_MODE_OFF;
276 mode = efx->phy_mode & supported;
279 if (mode & PHY_MODE_TX_DISABLED)
280 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
281 if (mode & PHY_MODE_LOW_POWER)
282 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
283 if (mode & PHY_MODE_OFF)
284 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
289 static u32 mcdi_to_ethtool_media(u32 media)
292 case MC_CMD_MEDIA_XAUI:
293 case MC_CMD_MEDIA_CX4:
294 case MC_CMD_MEDIA_KX4:
297 case MC_CMD_MEDIA_XFP:
298 case MC_CMD_MEDIA_SFP_PLUS:
299 case MC_CMD_MEDIA_QSFP_PLUS:
302 case MC_CMD_MEDIA_BASE_T:
310 static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
311 struct efx_link_state *link_state,
312 u32 speed, u32 flags, u32 fcntl)
315 case MC_CMD_FCNTL_AUTO:
316 WARN_ON(1); /* This is not a link mode */
317 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
319 case MC_CMD_FCNTL_BIDIR:
320 link_state->fc = EFX_FC_TX | EFX_FC_RX;
322 case MC_CMD_FCNTL_RESPOND:
323 link_state->fc = EFX_FC_RX;
327 case MC_CMD_FCNTL_OFF:
332 link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
333 link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
334 link_state->speed = speed;
337 static int efx_mcdi_phy_probe(struct efx_nic *efx)
339 struct efx_mcdi_phy_data *phy_data;
340 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
344 /* Initialise and populate phy_data */
345 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
346 if (phy_data == NULL)
349 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
353 /* Read initial link advertisement */
354 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
355 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
356 outbuf, sizeof(outbuf), NULL);
360 /* Fill out nic state */
361 efx->phy_data = phy_data;
362 efx->phy_type = phy_data->type;
364 efx->mdio_bus = phy_data->channel;
365 efx->mdio.prtad = phy_data->port;
366 efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
367 efx->mdio.mode_support = 0;
368 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
369 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
370 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
371 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
373 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
374 if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
375 efx->link_advertising =
376 mcdi_to_ethtool_cap(phy_data->media, caps);
378 phy_data->forced_cap = caps;
380 /* Assert that we can map efx -> mcdi loopback modes */
381 BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
382 BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
383 BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
384 BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
385 BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
386 BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
387 BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
388 BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
389 BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
390 BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
391 BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
392 BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
393 BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
394 BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
395 BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
396 BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
397 BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
398 BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
399 BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
400 BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
401 BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
402 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
403 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
404 BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
405 BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
406 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
407 BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
409 rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
412 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
413 * but by convention we don't */
414 efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
416 /* Set the initial link mode */
417 efx_mcdi_phy_decode_link(
418 efx, &efx->link_state,
419 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
420 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
421 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
423 /* Default to Autonegotiated flow control if the PHY supports it */
424 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
425 if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
426 efx->wanted_fc |= EFX_FC_AUTO;
427 efx_link_set_wanted_fc(efx, efx->wanted_fc);
436 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
438 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
439 u32 caps = (efx->link_advertising ?
440 ethtool_to_mcdi_cap(efx->link_advertising) :
441 phy_cfg->forced_cap);
443 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
444 efx->loopback_mode, 0);
447 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
448 * supported by the link partner. Warn the user if this isn't the case
450 static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
452 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
455 /* The link partner capabilities are only relevant if the
456 * link supports flow control autonegotiation */
457 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
460 /* If flow control autoneg is supported and enabled, then fine */
461 if (efx->wanted_fc & EFX_FC_AUTO)
465 if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
466 rmtadv |= ADVERTISED_Pause;
467 if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
468 rmtadv |= ADVERTISED_Asym_Pause;
470 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
471 netif_err(efx, link, efx->net_dev,
472 "warning: link partner doesn't support pause frames");
475 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
477 struct efx_link_state old_state = efx->link_state;
478 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
481 WARN_ON(!mutex_is_locked(&efx->mac_lock));
483 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
485 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
486 outbuf, sizeof(outbuf), NULL);
488 efx->link_state.up = false;
490 efx_mcdi_phy_decode_link(
491 efx, &efx->link_state,
492 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
493 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
494 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
496 return !efx_link_state_equal(&efx->link_state, &old_state);
499 static void efx_mcdi_phy_remove(struct efx_nic *efx)
501 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
503 efx->phy_data = NULL;
507 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
509 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
510 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
514 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
515 ecmd->advertising = efx->link_advertising;
516 ethtool_cmd_speed_set(ecmd, efx->link_state.speed);
517 ecmd->duplex = efx->link_state.fd;
518 ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
519 ecmd->phy_address = phy_cfg->port;
520 ecmd->transceiver = XCVR_INTERNAL;
521 ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
522 ecmd->mdio_support = (efx->mdio.mode_support &
523 (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
525 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
526 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
527 outbuf, sizeof(outbuf), NULL);
530 ecmd->lp_advertising =
531 mcdi_to_ethtool_cap(phy_cfg->media,
532 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
535 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
537 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
542 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
543 1 << MC_CMD_PHY_CAP_AN_LBN);
544 } else if (ecmd->duplex) {
545 switch (ethtool_cmd_speed(ecmd)) {
546 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
547 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
548 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
549 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
550 case 40000: caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN; break;
551 default: return -EINVAL;
554 switch (ethtool_cmd_speed(ecmd)) {
555 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
556 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
557 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
558 default: return -EINVAL;
562 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
563 efx->loopback_mode, 0);
568 efx_link_set_advertising(
569 efx, ecmd->advertising | ADVERTISED_Autoneg);
570 phy_cfg->forced_cap = 0;
572 efx_link_set_advertising(efx, 0);
573 phy_cfg->forced_cap = caps;
578 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
580 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
584 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
586 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
587 outbuf, sizeof(outbuf), &outlen);
591 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
593 if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
599 static const char *const mcdi_sft9001_cable_diag_names[] = {
600 "cable.pairA.length",
601 "cable.pairB.length",
602 "cable.pairC.length",
603 "cable.pairD.length",
604 "cable.pairA.status",
605 "cable.pairB.status",
606 "cable.pairC.status",
607 "cable.pairD.status",
610 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
613 unsigned int retry, i, count = 0;
616 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
617 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
621 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
622 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
623 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
624 inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
628 /* Wait up to 10s for BIST to finish */
629 for (retry = 0; retry < 100; ++retry) {
630 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
631 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
632 outbuf, sizeof(outbuf), &outlen);
636 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
637 if (status != MC_CMD_POLL_BIST_RUNNING)
647 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
649 /* SFT9001 specific cable diagnostics output */
650 if (efx->phy_type == PHY_TYPE_SFT9001B &&
651 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
652 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
653 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
654 if (status == MC_CMD_POLL_BIST_PASSED &&
655 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
656 for (i = 0; i < 8; i++) {
658 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
670 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
673 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
677 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
678 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
685 /* If we support both LONG and SHORT, then run each in response to
686 * break or not. Otherwise, run the one we support */
688 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
689 if ((flags & ETH_TEST_FL_OFFLINE) &&
691 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
692 mode = MC_CMD_PHY_BIST_CABLE_LONG;
694 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
695 } else if (phy_cfg->flags &
696 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
697 mode = MC_CMD_PHY_BIST_CABLE_LONG;
700 rc = efx_mcdi_bist(efx, mode, results);
709 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
712 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
714 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
720 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
721 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
726 if (efx->phy_type == PHY_TYPE_SFT9001B) {
727 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
728 return mcdi_sft9001_cable_diag_names[index];
729 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
736 #define SFP_PAGE_SIZE 128
737 #define SFP_NUM_PAGES 2
738 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
739 struct ethtool_eeprom *ee, u8 *data)
741 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
742 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
745 unsigned int payload_len;
746 unsigned int space_remaining = ee->len;
748 unsigned int page_off;
749 unsigned int to_copy;
750 u8 *user_data = data;
752 BUILD_BUG_ON(SFP_PAGE_SIZE * SFP_NUM_PAGES != ETH_MODULE_SFF_8079_LEN);
754 page_off = ee->offset % SFP_PAGE_SIZE;
755 page = ee->offset / SFP_PAGE_SIZE;
757 while (space_remaining && (page < SFP_NUM_PAGES)) {
758 MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
760 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_MEDIA_INFO,
761 inbuf, sizeof(inbuf),
762 outbuf, sizeof(outbuf),
767 if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
771 payload_len = MCDI_DWORD(outbuf,
772 GET_PHY_MEDIA_INFO_OUT_DATALEN);
773 if (payload_len != SFP_PAGE_SIZE)
776 /* Copy as much as we can into data */
777 payload_len -= page_off;
778 to_copy = (space_remaining < payload_len) ?
779 space_remaining : payload_len;
782 MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + page_off,
785 space_remaining -= to_copy;
786 user_data += to_copy;
794 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
795 struct ethtool_modinfo *modinfo)
797 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
799 switch (phy_cfg->media) {
800 case MC_CMD_MEDIA_SFP_PLUS:
801 modinfo->type = ETH_MODULE_SFF_8079;
802 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
809 static const struct efx_phy_operations efx_mcdi_phy_ops = {
810 .probe = efx_mcdi_phy_probe,
811 .init = efx_port_dummy_op_int,
812 .reconfigure = efx_mcdi_port_reconfigure,
813 .poll = efx_mcdi_phy_poll,
814 .fini = efx_port_dummy_op_void,
815 .remove = efx_mcdi_phy_remove,
816 .get_settings = efx_mcdi_phy_get_settings,
817 .set_settings = efx_mcdi_phy_set_settings,
818 .test_alive = efx_mcdi_phy_test_alive,
819 .run_tests = efx_mcdi_phy_run_tests,
820 .test_name = efx_mcdi_phy_test_name,
821 .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
822 .get_module_info = efx_mcdi_phy_get_module_info,
825 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
827 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
829 return phy_data->supported_cap;
832 static unsigned int efx_mcdi_event_link_speed[] = {
833 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
834 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
835 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
836 [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
839 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
841 u32 flags, fcntl, speed, lpa;
843 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
844 EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
845 speed = efx_mcdi_event_link_speed[speed];
847 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
848 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
849 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
851 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
852 * which is only run after flushing the event queues. Therefore, it
853 * is safe to modify the link state outside of the mac_lock here.
855 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
857 efx_mcdi_phy_check_fcntl(efx, lpa);
859 efx_link_status_changed(efx);
862 int efx_mcdi_set_mac(struct efx_nic *efx)
865 MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
867 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
869 /* This has no effect on EF10 */
870 ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
871 efx->net_dev->dev_addr);
873 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
874 EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
875 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
877 /* Set simple MAC filter for Siena */
878 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
879 SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
881 switch (efx->wanted_fc) {
882 case EFX_FC_RX | EFX_FC_TX:
883 fcntl = MC_CMD_FCNTL_BIDIR;
886 fcntl = MC_CMD_FCNTL_RESPOND;
889 fcntl = MC_CMD_FCNTL_OFF;
892 if (efx->wanted_fc & EFX_FC_AUTO)
893 fcntl = MC_CMD_FCNTL_AUTO;
895 fcntl = MC_CMD_FCNTL_OFF;
897 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
899 return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
903 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
905 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
909 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
911 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
912 outbuf, sizeof(outbuf), &outlength);
916 return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
919 enum efx_stats_action {
925 static int efx_mcdi_mac_stats(struct efx_nic *efx,
926 enum efx_stats_action action, int clear)
928 struct efx_ef10_nic_data *nic_data = efx->nic_data;
929 MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
931 int change = action == EFX_STATS_PULL ? 0 : 1;
932 int enable = action == EFX_STATS_ENABLE ? 1 : 0;
933 int period = action == EFX_STATS_ENABLE ? 1000 : 0;
934 dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
935 u32 dma_len = action != EFX_STATS_DISABLE ?
936 MC_CMD_MAC_NSTATS * sizeof(u64) : 0;
938 BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
940 MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
941 MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
942 MAC_STATS_IN_DMA, !!enable,
943 MAC_STATS_IN_CLEAR, clear,
944 MAC_STATS_IN_PERIODIC_CHANGE, change,
945 MAC_STATS_IN_PERIODIC_ENABLE, enable,
946 MAC_STATS_IN_PERIODIC_CLEAR, 0,
947 MAC_STATS_IN_PERIODIC_NOEVENT, 1,
948 MAC_STATS_IN_PERIOD_MS, period);
949 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
950 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
952 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
954 /* Expect ENOENT if DMA queues have not been set up */
955 if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
956 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
961 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
963 __le64 *dma_stats = efx->stats_buffer.addr;
965 dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
967 efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
970 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
972 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
975 #define EFX_MAC_STATS_WAIT_US 100
976 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
978 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
980 __le64 *dma_stats = efx->stats_buffer.addr;
981 int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
983 dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
984 efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
986 while (dma_stats[MC_CMD_MAC_GENERATION_END] ==
987 EFX_MC_STATS_GENERATION_INVALID &&
989 udelay(EFX_MAC_STATS_WAIT_US);
992 int efx_mcdi_port_probe(struct efx_nic *efx)
996 /* Hook in PHY operations table */
997 efx->phy_op = &efx_mcdi_phy_ops;
999 /* Set up MDIO structure for PHY */
1000 efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
1001 efx->mdio.mdio_read = efx_mcdi_mdio_read;
1002 efx->mdio.mdio_write = efx_mcdi_mdio_write;
1004 /* Fill out MDIO structure, loopback modes, and initial link state */
1005 rc = efx->phy_op->probe(efx);
1009 /* Allocate buffer for stats */
1010 rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
1011 MC_CMD_MAC_NSTATS * sizeof(u64), GFP_KERNEL);
1014 netif_dbg(efx, probe, efx->net_dev,
1015 "stats buffer at %llx (virt %p phys %llx)\n",
1016 (u64)efx->stats_buffer.dma_addr,
1017 efx->stats_buffer.addr,
1018 (u64)virt_to_phys(efx->stats_buffer.addr));
1020 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
1025 void efx_mcdi_port_remove(struct efx_nic *efx)
1027 efx->phy_op->remove(efx);
1028 efx_nic_free_buffer(efx, &efx->stats_buffer);
1031 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
1032 int efx_mcdi_port_get_number(struct efx_nic *efx)
1034 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
1037 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
1038 outbuf, sizeof(outbuf), NULL);
1042 return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);