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
;
188 case MC_CMD_MEDIA_XFP
:
189 case MC_CMD_MEDIA_SFP_PLUS
:
190 result
|= SUPPORTED_FIBRE
;
193 case MC_CMD_MEDIA_BASE_T
:
194 result
|= SUPPORTED_TP
;
195 if (cap
& (1 << MC_CMD_PHY_CAP_10HDX_LBN
))
196 result
|= SUPPORTED_10baseT_Half
;
197 if (cap
& (1 << MC_CMD_PHY_CAP_10FDX_LBN
))
198 result
|= SUPPORTED_10baseT_Full
;
199 if (cap
& (1 << MC_CMD_PHY_CAP_100HDX_LBN
))
200 result
|= SUPPORTED_100baseT_Half
;
201 if (cap
& (1 << MC_CMD_PHY_CAP_100FDX_LBN
))
202 result
|= SUPPORTED_100baseT_Full
;
203 if (cap
& (1 << MC_CMD_PHY_CAP_1000HDX_LBN
))
204 result
|= SUPPORTED_1000baseT_Half
;
205 if (cap
& (1 << MC_CMD_PHY_CAP_1000FDX_LBN
))
206 result
|= SUPPORTED_1000baseT_Full
;
207 if (cap
& (1 << MC_CMD_PHY_CAP_10000FDX_LBN
))
208 result
|= SUPPORTED_10000baseT_Full
;
212 if (cap
& (1 << MC_CMD_PHY_CAP_PAUSE_LBN
))
213 result
|= SUPPORTED_Pause
;
214 if (cap
& (1 << MC_CMD_PHY_CAP_ASYM_LBN
))
215 result
|= SUPPORTED_Asym_Pause
;
216 if (cap
& (1 << MC_CMD_PHY_CAP_AN_LBN
))
217 result
|= SUPPORTED_Autoneg
;
222 static u32
ethtool_to_mcdi_cap(u32 cap
)
226 if (cap
& SUPPORTED_10baseT_Half
)
227 result
|= (1 << MC_CMD_PHY_CAP_10HDX_LBN
);
228 if (cap
& SUPPORTED_10baseT_Full
)
229 result
|= (1 << MC_CMD_PHY_CAP_10FDX_LBN
);
230 if (cap
& SUPPORTED_100baseT_Half
)
231 result
|= (1 << MC_CMD_PHY_CAP_100HDX_LBN
);
232 if (cap
& SUPPORTED_100baseT_Full
)
233 result
|= (1 << MC_CMD_PHY_CAP_100FDX_LBN
);
234 if (cap
& SUPPORTED_1000baseT_Half
)
235 result
|= (1 << MC_CMD_PHY_CAP_1000HDX_LBN
);
236 if (cap
& (SUPPORTED_1000baseT_Full
| SUPPORTED_1000baseKX_Full
))
237 result
|= (1 << MC_CMD_PHY_CAP_1000FDX_LBN
);
238 if (cap
& (SUPPORTED_10000baseT_Full
| SUPPORTED_10000baseKX4_Full
))
239 result
|= (1 << MC_CMD_PHY_CAP_10000FDX_LBN
);
240 if (cap
& SUPPORTED_Pause
)
241 result
|= (1 << MC_CMD_PHY_CAP_PAUSE_LBN
);
242 if (cap
& SUPPORTED_Asym_Pause
)
243 result
|= (1 << MC_CMD_PHY_CAP_ASYM_LBN
);
244 if (cap
& SUPPORTED_Autoneg
)
245 result
|= (1 << MC_CMD_PHY_CAP_AN_LBN
);
250 static u32
efx_get_mcdi_phy_flags(struct efx_nic
*efx
)
252 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
253 enum efx_phy_mode mode
, supported
;
256 /* TODO: Advertise the capabilities supported by this PHY */
258 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN
))
259 supported
|= PHY_MODE_TX_DISABLED
;
260 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN
))
261 supported
|= PHY_MODE_LOW_POWER
;
262 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN
))
263 supported
|= PHY_MODE_OFF
;
265 mode
= efx
->phy_mode
& supported
;
268 if (mode
& PHY_MODE_TX_DISABLED
)
269 flags
|= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN
);
270 if (mode
& PHY_MODE_LOW_POWER
)
271 flags
|= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN
);
272 if (mode
& PHY_MODE_OFF
)
273 flags
|= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN
);
278 static u32
mcdi_to_ethtool_media(u32 media
)
281 case MC_CMD_MEDIA_XAUI
:
282 case MC_CMD_MEDIA_CX4
:
283 case MC_CMD_MEDIA_KX4
:
286 case MC_CMD_MEDIA_XFP
:
287 case MC_CMD_MEDIA_SFP_PLUS
:
290 case MC_CMD_MEDIA_BASE_T
:
298 static void efx_mcdi_phy_decode_link(struct efx_nic
*efx
,
299 struct efx_link_state
*link_state
,
300 u32 speed
, u32 flags
, u32 fcntl
)
303 case MC_CMD_FCNTL_AUTO
:
304 WARN_ON(1); /* This is not a link mode */
305 link_state
->fc
= EFX_FC_AUTO
| EFX_FC_TX
| EFX_FC_RX
;
307 case MC_CMD_FCNTL_BIDIR
:
308 link_state
->fc
= EFX_FC_TX
| EFX_FC_RX
;
310 case MC_CMD_FCNTL_RESPOND
:
311 link_state
->fc
= EFX_FC_RX
;
315 case MC_CMD_FCNTL_OFF
:
320 link_state
->up
= !!(flags
& (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN
));
321 link_state
->fd
= !!(flags
& (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN
));
322 link_state
->speed
= speed
;
325 static int efx_mcdi_phy_probe(struct efx_nic
*efx
)
327 struct efx_mcdi_phy_data
*phy_data
;
328 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_LINK_OUT_LEN
);
332 /* Initialise and populate phy_data */
333 phy_data
= kzalloc(sizeof(*phy_data
), GFP_KERNEL
);
334 if (phy_data
== NULL
)
337 rc
= efx_mcdi_get_phy_cfg(efx
, phy_data
);
341 /* Read initial link advertisement */
342 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN
!= 0);
343 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_LINK
, NULL
, 0,
344 outbuf
, sizeof(outbuf
), NULL
);
348 /* Fill out nic state */
349 efx
->phy_data
= phy_data
;
350 efx
->phy_type
= phy_data
->type
;
352 efx
->mdio_bus
= phy_data
->channel
;
353 efx
->mdio
.prtad
= phy_data
->port
;
354 efx
->mdio
.mmds
= phy_data
->mmd_mask
& ~(1 << MC_CMD_MMD_CLAUSE22
);
355 efx
->mdio
.mode_support
= 0;
356 if (phy_data
->mmd_mask
& (1 << MC_CMD_MMD_CLAUSE22
))
357 efx
->mdio
.mode_support
|= MDIO_SUPPORTS_C22
;
358 if (phy_data
->mmd_mask
& ~(1 << MC_CMD_MMD_CLAUSE22
))
359 efx
->mdio
.mode_support
|= MDIO_SUPPORTS_C45
| MDIO_EMULATE_C22
;
361 caps
= MCDI_DWORD(outbuf
, GET_LINK_OUT_CAP
);
362 if (caps
& (1 << MC_CMD_PHY_CAP_AN_LBN
))
363 efx
->link_advertising
=
364 mcdi_to_ethtool_cap(phy_data
->media
, caps
);
366 phy_data
->forced_cap
= caps
;
368 /* Assert that we can map efx -> mcdi loopback modes */
369 BUILD_BUG_ON(LOOPBACK_NONE
!= MC_CMD_LOOPBACK_NONE
);
370 BUILD_BUG_ON(LOOPBACK_DATA
!= MC_CMD_LOOPBACK_DATA
);
371 BUILD_BUG_ON(LOOPBACK_GMAC
!= MC_CMD_LOOPBACK_GMAC
);
372 BUILD_BUG_ON(LOOPBACK_XGMII
!= MC_CMD_LOOPBACK_XGMII
);
373 BUILD_BUG_ON(LOOPBACK_XGXS
!= MC_CMD_LOOPBACK_XGXS
);
374 BUILD_BUG_ON(LOOPBACK_XAUI
!= MC_CMD_LOOPBACK_XAUI
);
375 BUILD_BUG_ON(LOOPBACK_GMII
!= MC_CMD_LOOPBACK_GMII
);
376 BUILD_BUG_ON(LOOPBACK_SGMII
!= MC_CMD_LOOPBACK_SGMII
);
377 BUILD_BUG_ON(LOOPBACK_XGBR
!= MC_CMD_LOOPBACK_XGBR
);
378 BUILD_BUG_ON(LOOPBACK_XFI
!= MC_CMD_LOOPBACK_XFI
);
379 BUILD_BUG_ON(LOOPBACK_XAUI_FAR
!= MC_CMD_LOOPBACK_XAUI_FAR
);
380 BUILD_BUG_ON(LOOPBACK_GMII_FAR
!= MC_CMD_LOOPBACK_GMII_FAR
);
381 BUILD_BUG_ON(LOOPBACK_SGMII_FAR
!= MC_CMD_LOOPBACK_SGMII_FAR
);
382 BUILD_BUG_ON(LOOPBACK_XFI_FAR
!= MC_CMD_LOOPBACK_XFI_FAR
);
383 BUILD_BUG_ON(LOOPBACK_GPHY
!= MC_CMD_LOOPBACK_GPHY
);
384 BUILD_BUG_ON(LOOPBACK_PHYXS
!= MC_CMD_LOOPBACK_PHYXS
);
385 BUILD_BUG_ON(LOOPBACK_PCS
!= MC_CMD_LOOPBACK_PCS
);
386 BUILD_BUG_ON(LOOPBACK_PMAPMD
!= MC_CMD_LOOPBACK_PMAPMD
);
387 BUILD_BUG_ON(LOOPBACK_XPORT
!= MC_CMD_LOOPBACK_XPORT
);
388 BUILD_BUG_ON(LOOPBACK_XGMII_WS
!= MC_CMD_LOOPBACK_XGMII_WS
);
389 BUILD_BUG_ON(LOOPBACK_XAUI_WS
!= MC_CMD_LOOPBACK_XAUI_WS
);
390 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR
!= MC_CMD_LOOPBACK_XAUI_WS_FAR
);
391 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR
!= MC_CMD_LOOPBACK_XAUI_WS_NEAR
);
392 BUILD_BUG_ON(LOOPBACK_GMII_WS
!= MC_CMD_LOOPBACK_GMII_WS
);
393 BUILD_BUG_ON(LOOPBACK_XFI_WS
!= MC_CMD_LOOPBACK_XFI_WS
);
394 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR
!= MC_CMD_LOOPBACK_XFI_WS_FAR
);
395 BUILD_BUG_ON(LOOPBACK_PHYXS_WS
!= MC_CMD_LOOPBACK_PHYXS_WS
);
397 rc
= efx_mcdi_loopback_modes(efx
, &efx
->loopback_modes
);
400 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
401 * but by convention we don't */
402 efx
->loopback_modes
&= ~(1 << LOOPBACK_NONE
);
404 /* Set the initial link mode */
405 efx_mcdi_phy_decode_link(
406 efx
, &efx
->link_state
,
407 MCDI_DWORD(outbuf
, GET_LINK_OUT_LINK_SPEED
),
408 MCDI_DWORD(outbuf
, GET_LINK_OUT_FLAGS
),
409 MCDI_DWORD(outbuf
, GET_LINK_OUT_FCNTL
));
411 /* Default to Autonegotiated flow control if the PHY supports it */
412 efx
->wanted_fc
= EFX_FC_RX
| EFX_FC_TX
;
413 if (phy_data
->supported_cap
& (1 << MC_CMD_PHY_CAP_AN_LBN
))
414 efx
->wanted_fc
|= EFX_FC_AUTO
;
415 efx_link_set_wanted_fc(efx
, efx
->wanted_fc
);
424 int efx_mcdi_port_reconfigure(struct efx_nic
*efx
)
426 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
427 u32 caps
= (efx
->link_advertising
?
428 ethtool_to_mcdi_cap(efx
->link_advertising
) :
429 phy_cfg
->forced_cap
);
431 return efx_mcdi_set_link(efx
, caps
, efx_get_mcdi_phy_flags(efx
),
432 efx
->loopback_mode
, 0);
435 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
436 * supported by the link partner. Warn the user if this isn't the case
438 static void efx_mcdi_phy_check_fcntl(struct efx_nic
*efx
, u32 lpa
)
440 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
443 /* The link partner capabilities are only relevant if the
444 * link supports flow control autonegotiation */
445 if (~phy_cfg
->supported_cap
& (1 << MC_CMD_PHY_CAP_AN_LBN
))
448 /* If flow control autoneg is supported and enabled, then fine */
449 if (efx
->wanted_fc
& EFX_FC_AUTO
)
453 if (lpa
& (1 << MC_CMD_PHY_CAP_PAUSE_LBN
))
454 rmtadv
|= ADVERTISED_Pause
;
455 if (lpa
& (1 << MC_CMD_PHY_CAP_ASYM_LBN
))
456 rmtadv
|= ADVERTISED_Asym_Pause
;
458 if ((efx
->wanted_fc
& EFX_FC_TX
) && rmtadv
== ADVERTISED_Asym_Pause
)
459 netif_err(efx
, link
, efx
->net_dev
,
460 "warning: link partner doesn't support pause frames");
463 static bool efx_mcdi_phy_poll(struct efx_nic
*efx
)
465 struct efx_link_state old_state
= efx
->link_state
;
466 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_LINK_OUT_LEN
);
469 WARN_ON(!mutex_is_locked(&efx
->mac_lock
));
471 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN
!= 0);
473 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_LINK
, NULL
, 0,
474 outbuf
, sizeof(outbuf
), NULL
);
476 efx
->link_state
.up
= false;
478 efx_mcdi_phy_decode_link(
479 efx
, &efx
->link_state
,
480 MCDI_DWORD(outbuf
, GET_LINK_OUT_LINK_SPEED
),
481 MCDI_DWORD(outbuf
, GET_LINK_OUT_FLAGS
),
482 MCDI_DWORD(outbuf
, GET_LINK_OUT_FCNTL
));
484 return !efx_link_state_equal(&efx
->link_state
, &old_state
);
487 static void efx_mcdi_phy_remove(struct efx_nic
*efx
)
489 struct efx_mcdi_phy_data
*phy_data
= efx
->phy_data
;
491 efx
->phy_data
= NULL
;
495 static void efx_mcdi_phy_get_settings(struct efx_nic
*efx
, struct ethtool_cmd
*ecmd
)
497 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
498 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_LINK_OUT_LEN
);
502 mcdi_to_ethtool_cap(phy_cfg
->media
, phy_cfg
->supported_cap
);
503 ecmd
->advertising
= efx
->link_advertising
;
504 ethtool_cmd_speed_set(ecmd
, efx
->link_state
.speed
);
505 ecmd
->duplex
= efx
->link_state
.fd
;
506 ecmd
->port
= mcdi_to_ethtool_media(phy_cfg
->media
);
507 ecmd
->phy_address
= phy_cfg
->port
;
508 ecmd
->transceiver
= XCVR_INTERNAL
;
509 ecmd
->autoneg
= !!(efx
->link_advertising
& ADVERTISED_Autoneg
);
510 ecmd
->mdio_support
= (efx
->mdio
.mode_support
&
511 (MDIO_SUPPORTS_C45
| MDIO_SUPPORTS_C22
));
513 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN
!= 0);
514 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_LINK
, NULL
, 0,
515 outbuf
, sizeof(outbuf
), NULL
);
518 ecmd
->lp_advertising
=
519 mcdi_to_ethtool_cap(phy_cfg
->media
,
520 MCDI_DWORD(outbuf
, GET_LINK_OUT_LP_CAP
));
523 static int efx_mcdi_phy_set_settings(struct efx_nic
*efx
, struct ethtool_cmd
*ecmd
)
525 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
530 caps
= (ethtool_to_mcdi_cap(ecmd
->advertising
) |
531 1 << MC_CMD_PHY_CAP_AN_LBN
);
532 } else if (ecmd
->duplex
) {
533 switch (ethtool_cmd_speed(ecmd
)) {
534 case 10: caps
= 1 << MC_CMD_PHY_CAP_10FDX_LBN
; break;
535 case 100: caps
= 1 << MC_CMD_PHY_CAP_100FDX_LBN
; break;
536 case 1000: caps
= 1 << MC_CMD_PHY_CAP_1000FDX_LBN
; break;
537 case 10000: caps
= 1 << MC_CMD_PHY_CAP_10000FDX_LBN
; break;
538 case 40000: caps
= 1 << MC_CMD_PHY_CAP_40000FDX_LBN
; break;
539 default: return -EINVAL
;
542 switch (ethtool_cmd_speed(ecmd
)) {
543 case 10: caps
= 1 << MC_CMD_PHY_CAP_10HDX_LBN
; break;
544 case 100: caps
= 1 << MC_CMD_PHY_CAP_100HDX_LBN
; break;
545 case 1000: caps
= 1 << MC_CMD_PHY_CAP_1000HDX_LBN
; break;
546 default: return -EINVAL
;
550 rc
= efx_mcdi_set_link(efx
, caps
, efx_get_mcdi_phy_flags(efx
),
551 efx
->loopback_mode
, 0);
556 efx_link_set_advertising(
557 efx
, ecmd
->advertising
| ADVERTISED_Autoneg
);
558 phy_cfg
->forced_cap
= 0;
560 efx_link_set_advertising(efx
, 0);
561 phy_cfg
->forced_cap
= caps
;
566 static int efx_mcdi_phy_test_alive(struct efx_nic
*efx
)
568 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_PHY_STATE_OUT_LEN
);
572 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN
!= 0);
574 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_PHY_STATE
, NULL
, 0,
575 outbuf
, sizeof(outbuf
), &outlen
);
579 if (outlen
< MC_CMD_GET_PHY_STATE_OUT_LEN
)
581 if (MCDI_DWORD(outbuf
, GET_PHY_STATE_OUT_STATE
) != MC_CMD_PHY_STATE_OK
)
587 static const char *const mcdi_sft9001_cable_diag_names
[] = {
588 "cable.pairA.length",
589 "cable.pairB.length",
590 "cable.pairC.length",
591 "cable.pairD.length",
592 "cable.pairA.status",
593 "cable.pairB.status",
594 "cable.pairC.status",
595 "cable.pairD.status",
598 static int efx_mcdi_bist(struct efx_nic
*efx
, unsigned int bist_mode
,
601 unsigned int retry
, i
, count
= 0;
604 MCDI_DECLARE_BUF(inbuf
, MC_CMD_START_BIST_IN_LEN
);
605 MCDI_DECLARE_BUF(outbuf
, MC_CMD_POLL_BIST_OUT_SFT9001_LEN
);
609 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN
!= 0);
610 MCDI_SET_DWORD(inbuf
, START_BIST_IN_TYPE
, bist_mode
);
611 rc
= efx_mcdi_rpc(efx
, MC_CMD_START_BIST
,
612 inbuf
, MC_CMD_START_BIST_IN_LEN
, NULL
, 0, NULL
);
616 /* Wait up to 10s for BIST to finish */
617 for (retry
= 0; retry
< 100; ++retry
) {
618 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN
!= 0);
619 rc
= efx_mcdi_rpc(efx
, MC_CMD_POLL_BIST
, NULL
, 0,
620 outbuf
, sizeof(outbuf
), &outlen
);
624 status
= MCDI_DWORD(outbuf
, POLL_BIST_OUT_RESULT
);
625 if (status
!= MC_CMD_POLL_BIST_RUNNING
)
635 results
[count
++] = (status
== MC_CMD_POLL_BIST_PASSED
) ? 1 : -1;
637 /* SFT9001 specific cable diagnostics output */
638 if (efx
->phy_type
== PHY_TYPE_SFT9001B
&&
639 (bist_mode
== MC_CMD_PHY_BIST_CABLE_SHORT
||
640 bist_mode
== MC_CMD_PHY_BIST_CABLE_LONG
)) {
641 ptr
= MCDI_PTR(outbuf
, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A
);
642 if (status
== MC_CMD_POLL_BIST_PASSED
&&
643 outlen
>= MC_CMD_POLL_BIST_OUT_SFT9001_LEN
) {
644 for (i
= 0; i
< 8; i
++) {
646 EFX_DWORD_FIELD(((efx_dword_t
*)ptr
)[i
],
658 static int efx_mcdi_phy_run_tests(struct efx_nic
*efx
, int *results
,
661 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
665 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN
)) {
666 rc
= efx_mcdi_bist(efx
, MC_CMD_PHY_BIST
, results
);
673 /* If we support both LONG and SHORT, then run each in response to
674 * break or not. Otherwise, run the one we support */
676 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN
)) {
677 if ((flags
& ETH_TEST_FL_OFFLINE
) &&
679 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN
)))
680 mode
= MC_CMD_PHY_BIST_CABLE_LONG
;
682 mode
= MC_CMD_PHY_BIST_CABLE_SHORT
;
683 } else if (phy_cfg
->flags
&
684 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN
))
685 mode
= MC_CMD_PHY_BIST_CABLE_LONG
;
688 rc
= efx_mcdi_bist(efx
, mode
, results
);
697 static const char *efx_mcdi_phy_test_name(struct efx_nic
*efx
,
700 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
702 if (phy_cfg
->flags
& (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN
)) {
708 if (phy_cfg
->flags
& ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN
) |
709 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN
))) {
714 if (efx
->phy_type
== PHY_TYPE_SFT9001B
) {
715 if (index
< ARRAY_SIZE(mcdi_sft9001_cable_diag_names
))
716 return mcdi_sft9001_cable_diag_names
[index
];
717 index
-= ARRAY_SIZE(mcdi_sft9001_cable_diag_names
);
724 #define SFP_PAGE_SIZE 128
725 #define SFP_NUM_PAGES 2
726 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic
*efx
,
727 struct ethtool_eeprom
*ee
, u8
*data
)
729 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX
);
730 MCDI_DECLARE_BUF(inbuf
, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN
);
733 unsigned int payload_len
;
734 unsigned int space_remaining
= ee
->len
;
736 unsigned int page_off
;
737 unsigned int to_copy
;
738 u8
*user_data
= data
;
740 BUILD_BUG_ON(SFP_PAGE_SIZE
* SFP_NUM_PAGES
!= ETH_MODULE_SFF_8079_LEN
);
742 page_off
= ee
->offset
% SFP_PAGE_SIZE
;
743 page
= ee
->offset
/ SFP_PAGE_SIZE
;
745 while (space_remaining
&& (page
< SFP_NUM_PAGES
)) {
746 MCDI_SET_DWORD(inbuf
, GET_PHY_MEDIA_INFO_IN_PAGE
, page
);
748 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_PHY_MEDIA_INFO
,
749 inbuf
, sizeof(inbuf
),
750 outbuf
, sizeof(outbuf
),
755 if (outlen
< (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST
+
759 payload_len
= MCDI_DWORD(outbuf
,
760 GET_PHY_MEDIA_INFO_OUT_DATALEN
);
761 if (payload_len
!= SFP_PAGE_SIZE
)
764 /* Copy as much as we can into data */
765 payload_len
-= page_off
;
766 to_copy
= (space_remaining
< payload_len
) ?
767 space_remaining
: payload_len
;
770 MCDI_PTR(outbuf
, GET_PHY_MEDIA_INFO_OUT_DATA
) + page_off
,
773 space_remaining
-= to_copy
;
774 user_data
+= to_copy
;
782 static int efx_mcdi_phy_get_module_info(struct efx_nic
*efx
,
783 struct ethtool_modinfo
*modinfo
)
785 struct efx_mcdi_phy_data
*phy_cfg
= efx
->phy_data
;
787 switch (phy_cfg
->media
) {
788 case MC_CMD_MEDIA_SFP_PLUS
:
789 modinfo
->type
= ETH_MODULE_SFF_8079
;
790 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
797 static const struct efx_phy_operations efx_mcdi_phy_ops
= {
798 .probe
= efx_mcdi_phy_probe
,
799 .init
= efx_port_dummy_op_int
,
800 .reconfigure
= efx_mcdi_port_reconfigure
,
801 .poll
= efx_mcdi_phy_poll
,
802 .fini
= efx_port_dummy_op_void
,
803 .remove
= efx_mcdi_phy_remove
,
804 .get_settings
= efx_mcdi_phy_get_settings
,
805 .set_settings
= efx_mcdi_phy_set_settings
,
806 .test_alive
= efx_mcdi_phy_test_alive
,
807 .run_tests
= efx_mcdi_phy_run_tests
,
808 .test_name
= efx_mcdi_phy_test_name
,
809 .get_module_eeprom
= efx_mcdi_phy_get_module_eeprom
,
810 .get_module_info
= efx_mcdi_phy_get_module_info
,
813 u32
efx_mcdi_phy_get_caps(struct efx_nic
*efx
)
815 struct efx_mcdi_phy_data
*phy_data
= efx
->phy_data
;
817 return phy_data
->supported_cap
;
820 static unsigned int efx_mcdi_event_link_speed
[] = {
821 [MCDI_EVENT_LINKCHANGE_SPEED_100M
] = 100,
822 [MCDI_EVENT_LINKCHANGE_SPEED_1G
] = 1000,
823 [MCDI_EVENT_LINKCHANGE_SPEED_10G
] = 10000,
824 [MCDI_EVENT_LINKCHANGE_SPEED_40G
] = 40000,
827 void efx_mcdi_process_link_change(struct efx_nic
*efx
, efx_qword_t
*ev
)
829 u32 flags
, fcntl
, speed
, lpa
;
831 speed
= EFX_QWORD_FIELD(*ev
, MCDI_EVENT_LINKCHANGE_SPEED
);
832 EFX_BUG_ON_PARANOID(speed
>= ARRAY_SIZE(efx_mcdi_event_link_speed
));
833 speed
= efx_mcdi_event_link_speed
[speed
];
835 flags
= EFX_QWORD_FIELD(*ev
, MCDI_EVENT_LINKCHANGE_LINK_FLAGS
);
836 fcntl
= EFX_QWORD_FIELD(*ev
, MCDI_EVENT_LINKCHANGE_FCNTL
);
837 lpa
= EFX_QWORD_FIELD(*ev
, MCDI_EVENT_LINKCHANGE_LP_CAP
);
839 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
840 * which is only run after flushing the event queues. Therefore, it
841 * is safe to modify the link state outside of the mac_lock here.
843 efx_mcdi_phy_decode_link(efx
, &efx
->link_state
, speed
, flags
, fcntl
);
845 efx_mcdi_phy_check_fcntl(efx
, lpa
);
847 efx_link_status_changed(efx
);
850 int efx_mcdi_set_mac(struct efx_nic
*efx
)
853 MCDI_DECLARE_BUF(cmdbytes
, MC_CMD_SET_MAC_IN_LEN
);
855 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN
!= 0);
857 memcpy(MCDI_PTR(cmdbytes
, SET_MAC_IN_ADDR
),
858 efx
->net_dev
->dev_addr
, ETH_ALEN
);
860 MCDI_SET_DWORD(cmdbytes
, SET_MAC_IN_MTU
,
861 EFX_MAX_FRAME_LEN(efx
->net_dev
->mtu
));
862 MCDI_SET_DWORD(cmdbytes
, SET_MAC_IN_DRAIN
, 0);
864 /* Set simple MAC filter for Siena */
865 MCDI_POPULATE_DWORD_1(cmdbytes
, SET_MAC_IN_REJECT
,
866 SET_MAC_IN_REJECT_UNCST
, efx
->unicast_filter
);
868 switch (efx
->wanted_fc
) {
869 case EFX_FC_RX
| EFX_FC_TX
:
870 fcntl
= MC_CMD_FCNTL_BIDIR
;
873 fcntl
= MC_CMD_FCNTL_RESPOND
;
876 fcntl
= MC_CMD_FCNTL_OFF
;
879 if (efx
->wanted_fc
& EFX_FC_AUTO
)
880 fcntl
= MC_CMD_FCNTL_AUTO
;
882 fcntl
= MC_CMD_FCNTL_OFF
;
884 MCDI_SET_DWORD(cmdbytes
, SET_MAC_IN_FCNTL
, fcntl
);
886 return efx_mcdi_rpc(efx
, MC_CMD_SET_MAC
, cmdbytes
, sizeof(cmdbytes
),
890 bool efx_mcdi_mac_check_fault(struct efx_nic
*efx
)
892 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_LINK_OUT_LEN
);
896 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN
!= 0);
898 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_LINK
, NULL
, 0,
899 outbuf
, sizeof(outbuf
), &outlength
);
903 return MCDI_DWORD(outbuf
, GET_LINK_OUT_MAC_FAULT
) != 0;
906 enum efx_stats_action
{
912 static int efx_mcdi_mac_stats(struct efx_nic
*efx
,
913 enum efx_stats_action action
, int clear
)
915 MCDI_DECLARE_BUF(inbuf
, MC_CMD_MAC_STATS_IN_LEN
);
917 int change
= action
== EFX_STATS_PULL
? 0 : 1;
918 int enable
= action
== EFX_STATS_ENABLE
? 1 : 0;
919 int period
= action
== EFX_STATS_ENABLE
? 1000 : 0;
920 dma_addr_t dma_addr
= efx
->stats_buffer
.dma_addr
;
921 u32 dma_len
= action
!= EFX_STATS_DISABLE
?
922 MC_CMD_MAC_NSTATS
* sizeof(u64
) : 0;
924 BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN
!= 0);
926 MCDI_SET_QWORD(inbuf
, MAC_STATS_IN_DMA_ADDR
, dma_addr
);
927 MCDI_POPULATE_DWORD_7(inbuf
, MAC_STATS_IN_CMD
,
928 MAC_STATS_IN_DMA
, !!enable
,
929 MAC_STATS_IN_CLEAR
, clear
,
930 MAC_STATS_IN_PERIODIC_CHANGE
, change
,
931 MAC_STATS_IN_PERIODIC_ENABLE
, enable
,
932 MAC_STATS_IN_PERIODIC_CLEAR
, 0,
933 MAC_STATS_IN_PERIODIC_NOEVENT
, 1,
934 MAC_STATS_IN_PERIOD_MS
, period
);
935 MCDI_SET_DWORD(inbuf
, MAC_STATS_IN_DMA_LEN
, dma_len
);
937 rc
= efx_mcdi_rpc(efx
, MC_CMD_MAC_STATS
, inbuf
, sizeof(inbuf
),
942 void efx_mcdi_mac_start_stats(struct efx_nic
*efx
)
944 __le64
*dma_stats
= efx
->stats_buffer
.addr
;
946 dma_stats
[MC_CMD_MAC_GENERATION_END
] = EFX_MC_STATS_GENERATION_INVALID
;
948 efx_mcdi_mac_stats(efx
, EFX_STATS_ENABLE
, 0);
951 void efx_mcdi_mac_stop_stats(struct efx_nic
*efx
)
953 efx_mcdi_mac_stats(efx
, EFX_STATS_DISABLE
, 0);
956 #define EFX_MAC_STATS_WAIT_US 100
957 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
959 void efx_mcdi_mac_pull_stats(struct efx_nic
*efx
)
961 __le64
*dma_stats
= efx
->stats_buffer
.addr
;
962 int attempts
= EFX_MAC_STATS_WAIT_ATTEMPTS
;
964 dma_stats
[MC_CMD_MAC_GENERATION_END
] = EFX_MC_STATS_GENERATION_INVALID
;
965 efx_mcdi_mac_stats(efx
, EFX_STATS_PULL
, 0);
967 while (dma_stats
[MC_CMD_MAC_GENERATION_END
] ==
968 EFX_MC_STATS_GENERATION_INVALID
&&
970 udelay(EFX_MAC_STATS_WAIT_US
);
973 int efx_mcdi_port_probe(struct efx_nic
*efx
)
977 /* Hook in PHY operations table */
978 efx
->phy_op
= &efx_mcdi_phy_ops
;
980 /* Set up MDIO structure for PHY */
981 efx
->mdio
.mode_support
= MDIO_SUPPORTS_C45
| MDIO_EMULATE_C22
;
982 efx
->mdio
.mdio_read
= efx_mcdi_mdio_read
;
983 efx
->mdio
.mdio_write
= efx_mcdi_mdio_write
;
985 /* Fill out MDIO structure, loopback modes, and initial link state */
986 rc
= efx
->phy_op
->probe(efx
);
990 /* Allocate buffer for stats */
991 rc
= efx_nic_alloc_buffer(efx
, &efx
->stats_buffer
,
992 MC_CMD_MAC_NSTATS
* sizeof(u64
), GFP_KERNEL
);
995 netif_dbg(efx
, probe
, efx
->net_dev
,
996 "stats buffer at %llx (virt %p phys %llx)\n",
997 (u64
)efx
->stats_buffer
.dma_addr
,
998 efx
->stats_buffer
.addr
,
999 (u64
)virt_to_phys(efx
->stats_buffer
.addr
));
1001 efx_mcdi_mac_stats(efx
, EFX_STATS_DISABLE
, 1);
1006 void efx_mcdi_port_remove(struct efx_nic
*efx
)
1008 efx
->phy_op
->remove(efx
);
1009 efx_nic_free_buffer(efx
, &efx
->stats_buffer
);
1012 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
1013 int efx_mcdi_port_get_number(struct efx_nic
*efx
)
1015 MCDI_DECLARE_BUF(outbuf
, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN
);
1018 rc
= efx_mcdi_rpc(efx
, MC_CMD_GET_PORT_ASSIGNMENT
, NULL
, 0,
1019 outbuf
, sizeof(outbuf
), NULL
);
1023 return MCDI_DWORD(outbuf
, GET_PORT_ASSIGNMENT_OUT_PORT
);