GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / sfc / mcdi_phy.c
blob0121e71702bf9a5183ece1870c544f409d44b05f
1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2009 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.
8 */
11 * Driver for PHY related operations via MCDI.
14 #include <linux/slab.h>
15 #include "efx.h"
16 #include "phy.h"
17 #include "mcdi.h"
18 #include "mcdi_pcol.h"
19 #include "mdio_10g.h"
20 #include "nic.h"
21 #include "selftest.h"
23 struct efx_mcdi_phy_data {
24 u32 flags;
25 u32 type;
26 u32 supported_cap;
27 u32 channel;
28 u32 port;
29 u32 stats_mask;
30 u8 name[20];
31 u32 media;
32 u32 mmd_mask;
33 u8 revision[20];
34 u32 forced_cap;
37 static int
38 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
40 u8 outbuf[MC_CMD_GET_PHY_CFG_OUT_LEN];
41 size_t outlen;
42 int rc;
44 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
45 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
47 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
48 outbuf, sizeof(outbuf), &outlen);
49 if (rc)
50 goto fail;
52 if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
53 rc = -EIO;
54 goto fail;
57 cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
58 cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
59 cfg->supported_cap =
60 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
61 cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
62 cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
63 cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
64 memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
65 sizeof(cfg->name));
66 cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
67 cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
68 memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
69 sizeof(cfg->revision));
71 return 0;
73 fail:
74 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
75 return rc;
78 static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
79 u32 flags, u32 loopback_mode,
80 u32 loopback_speed)
82 u8 inbuf[MC_CMD_SET_LINK_IN_LEN];
83 int rc;
85 BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
87 MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
88 MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
89 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
90 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
92 rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
93 NULL, 0, NULL);
94 if (rc)
95 goto fail;
97 return 0;
99 fail:
100 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
101 return rc;
104 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
106 u8 outbuf[MC_CMD_GET_LOOPBACK_MODES_OUT_LEN];
107 size_t outlen;
108 int rc;
110 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
111 outbuf, sizeof(outbuf), &outlen);
112 if (rc)
113 goto fail;
115 if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
116 rc = -EIO;
117 goto fail;
120 *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
122 return 0;
124 fail:
125 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
126 return rc;
129 int efx_mcdi_mdio_read(struct efx_nic *efx, unsigned int bus,
130 unsigned int prtad, unsigned int devad, u16 addr,
131 u16 *value_out, u32 *status_out)
133 u8 inbuf[MC_CMD_MDIO_READ_IN_LEN];
134 u8 outbuf[MC_CMD_MDIO_READ_OUT_LEN];
135 size_t outlen;
136 int rc;
138 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, bus);
139 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
140 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
141 MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
143 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
144 outbuf, sizeof(outbuf), &outlen);
145 if (rc)
146 goto fail;
148 *value_out = (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
149 *status_out = MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS);
150 return 0;
152 fail:
153 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
154 return rc;
157 int efx_mcdi_mdio_write(struct efx_nic *efx, unsigned int bus,
158 unsigned int prtad, unsigned int devad, u16 addr,
159 u16 value, u32 *status_out)
161 u8 inbuf[MC_CMD_MDIO_WRITE_IN_LEN];
162 u8 outbuf[MC_CMD_MDIO_WRITE_OUT_LEN];
163 size_t outlen;
164 int rc;
166 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, bus);
167 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
168 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
169 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
170 MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
172 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
173 outbuf, sizeof(outbuf), &outlen);
174 if (rc)
175 goto fail;
177 *status_out = MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS);
178 return 0;
180 fail:
181 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
182 return rc;
185 static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
187 u32 result = 0;
189 switch (media) {
190 case MC_CMD_MEDIA_KX4:
191 result |= SUPPORTED_Backplane;
192 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
193 result |= SUPPORTED_1000baseKX_Full;
194 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
195 result |= SUPPORTED_10000baseKX4_Full;
196 break;
198 case MC_CMD_MEDIA_XFP:
199 case MC_CMD_MEDIA_SFP_PLUS:
200 result |= SUPPORTED_FIBRE;
201 break;
203 case MC_CMD_MEDIA_BASE_T:
204 result |= SUPPORTED_TP;
205 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
206 result |= SUPPORTED_10baseT_Half;
207 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
208 result |= SUPPORTED_10baseT_Full;
209 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
210 result |= SUPPORTED_100baseT_Half;
211 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
212 result |= SUPPORTED_100baseT_Full;
213 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
214 result |= SUPPORTED_1000baseT_Half;
215 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
216 result |= SUPPORTED_1000baseT_Full;
217 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
218 result |= SUPPORTED_10000baseT_Full;
219 break;
222 if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
223 result |= SUPPORTED_Pause;
224 if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
225 result |= SUPPORTED_Asym_Pause;
226 if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
227 result |= SUPPORTED_Autoneg;
229 return result;
232 static u32 ethtool_to_mcdi_cap(u32 cap)
234 u32 result = 0;
236 if (cap & SUPPORTED_10baseT_Half)
237 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
238 if (cap & SUPPORTED_10baseT_Full)
239 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
240 if (cap & SUPPORTED_100baseT_Half)
241 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
242 if (cap & SUPPORTED_100baseT_Full)
243 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
244 if (cap & SUPPORTED_1000baseT_Half)
245 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
246 if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
247 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
248 if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
249 result |= (1 << MC_CMD_PHY_CAP_10000FDX_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);
257 return result;
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;
264 u32 flags;
266 /* TODO: Advertise the capabilities supported by this PHY */
267 supported = 0;
268 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_TXDIS_LBN))
269 supported |= PHY_MODE_TX_DISABLED;
270 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_LOWPOWER_LBN))
271 supported |= PHY_MODE_LOW_POWER;
272 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_POWEROFF_LBN))
273 supported |= PHY_MODE_OFF;
275 mode = efx->phy_mode & supported;
277 flags = 0;
278 if (mode & PHY_MODE_TX_DISABLED)
279 flags |= (1 << MC_CMD_SET_LINK_TXDIS_LBN);
280 if (mode & PHY_MODE_LOW_POWER)
281 flags |= (1 << MC_CMD_SET_LINK_LOWPOWER_LBN);
282 if (mode & PHY_MODE_OFF)
283 flags |= (1 << MC_CMD_SET_LINK_POWEROFF_LBN);
285 return flags;
288 static u32 mcdi_to_ethtool_media(u32 media)
290 switch (media) {
291 case MC_CMD_MEDIA_XAUI:
292 case MC_CMD_MEDIA_CX4:
293 case MC_CMD_MEDIA_KX4:
294 return PORT_OTHER;
296 case MC_CMD_MEDIA_XFP:
297 case MC_CMD_MEDIA_SFP_PLUS:
298 return PORT_FIBRE;
300 case MC_CMD_MEDIA_BASE_T:
301 return PORT_TP;
303 default:
304 return PORT_OTHER;
308 static int efx_mcdi_phy_probe(struct efx_nic *efx)
310 struct efx_mcdi_phy_data *phy_data;
311 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
312 u32 caps;
313 int rc;
315 /* Initialise and populate phy_data */
316 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
317 if (phy_data == NULL)
318 return -ENOMEM;
320 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
321 if (rc != 0)
322 goto fail;
324 /* Read initial link advertisement */
325 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
326 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
327 outbuf, sizeof(outbuf), NULL);
328 if (rc)
329 goto fail;
331 /* Fill out nic state */
332 efx->phy_data = phy_data;
333 efx->phy_type = phy_data->type;
335 efx->mdio_bus = phy_data->channel;
336 efx->mdio.prtad = phy_data->port;
337 efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
338 efx->mdio.mode_support = 0;
339 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
340 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
341 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
342 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
344 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
345 if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
346 efx->link_advertising =
347 mcdi_to_ethtool_cap(phy_data->media, caps);
348 else
349 phy_data->forced_cap = caps;
351 /* Assert that we can map efx -> mcdi loopback modes */
352 BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
353 BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
354 BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
355 BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
356 BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
357 BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
358 BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
359 BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
360 BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
361 BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
362 BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
363 BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
364 BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
365 BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
366 BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
367 BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
368 BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
369 BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
370 BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
371 BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
372 BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
373 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
374 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
375 BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
376 BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
377 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
378 BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
380 rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
381 if (rc != 0)
382 goto fail;
383 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
384 * but by convention we don't */
385 efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
387 /* Set the initial link mode */
388 efx_mcdi_phy_decode_link(
389 efx, &efx->link_state,
390 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
391 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
392 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
394 /* Default to Autonegotiated flow control if the PHY supports it */
395 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
396 if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
397 efx->wanted_fc |= EFX_FC_AUTO;
398 efx_link_set_wanted_fc(efx, efx->wanted_fc);
400 return 0;
402 fail:
403 kfree(phy_data);
404 return rc;
407 int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
409 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
410 u32 caps = (efx->link_advertising ?
411 ethtool_to_mcdi_cap(efx->link_advertising) :
412 phy_cfg->forced_cap);
414 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
415 efx->loopback_mode, 0);
418 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
419 struct efx_link_state *link_state,
420 u32 speed, u32 flags, u32 fcntl)
422 switch (fcntl) {
423 case MC_CMD_FCNTL_AUTO:
424 WARN_ON(1); /* This is not a link mode */
425 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
426 break;
427 case MC_CMD_FCNTL_BIDIR:
428 link_state->fc = EFX_FC_TX | EFX_FC_RX;
429 break;
430 case MC_CMD_FCNTL_RESPOND:
431 link_state->fc = EFX_FC_RX;
432 break;
433 default:
434 WARN_ON(1);
435 case MC_CMD_FCNTL_OFF:
436 link_state->fc = 0;
437 break;
440 link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
441 link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
442 link_state->speed = speed;
445 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
446 * supported by the link partner. Warn the user if this isn't the case
448 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
450 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
451 u32 rmtadv;
453 /* The link partner capabilities are only relevent if the
454 * link supports flow control autonegotiation */
455 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
456 return;
458 /* If flow control autoneg is supported and enabled, then fine */
459 if (efx->wanted_fc & EFX_FC_AUTO)
460 return;
462 rmtadv = 0;
463 if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
464 rmtadv |= ADVERTISED_Pause;
465 if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
466 rmtadv |= ADVERTISED_Asym_Pause;
468 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
469 netif_err(efx, link, efx->net_dev,
470 "warning: link partner doesn't support pause frames");
473 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
475 struct efx_link_state old_state = efx->link_state;
476 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
477 int rc;
479 WARN_ON(!mutex_is_locked(&efx->mac_lock));
481 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
483 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
484 outbuf, sizeof(outbuf), NULL);
485 if (rc) {
486 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
487 __func__, rc);
488 efx->link_state.up = false;
489 } else {
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));
497 return !efx_link_state_equal(&efx->link_state, &old_state);
500 static void efx_mcdi_phy_remove(struct efx_nic *efx)
502 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
504 efx->phy_data = NULL;
505 kfree(phy_data);
508 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
510 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
511 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
512 int rc;
514 ecmd->supported =
515 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
516 ecmd->advertising = efx->link_advertising;
517 ecmd->speed = efx->link_state.speed;
518 ecmd->duplex = efx->link_state.fd;
519 ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
520 ecmd->phy_address = phy_cfg->port;
521 ecmd->transceiver = XCVR_INTERNAL;
522 ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
523 ecmd->mdio_support = (efx->mdio.mode_support &
524 (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
526 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
527 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
528 outbuf, sizeof(outbuf), NULL);
529 if (rc) {
530 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
531 __func__, rc);
532 return;
534 ecmd->lp_advertising =
535 mcdi_to_ethtool_cap(phy_cfg->media,
536 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
539 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
541 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
542 u32 caps;
543 int rc;
545 if (ecmd->autoneg) {
546 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
547 1 << MC_CMD_PHY_CAP_AN_LBN);
548 } else if (ecmd->duplex) {
549 switch (ecmd->speed) {
550 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
551 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
552 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
553 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
554 default: return -EINVAL;
556 } else {
557 switch (ecmd->speed) {
558 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
559 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
560 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
561 default: return -EINVAL;
565 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
566 efx->loopback_mode, 0);
567 if (rc)
568 return rc;
570 if (ecmd->autoneg) {
571 efx_link_set_advertising(
572 efx, ecmd->advertising | ADVERTISED_Autoneg);
573 phy_cfg->forced_cap = 0;
574 } else {
575 efx_link_set_advertising(efx, 0);
576 phy_cfg->forced_cap = caps;
578 return 0;
581 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
583 u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
584 size_t outlen;
585 int rc;
587 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
589 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
590 outbuf, sizeof(outbuf), &outlen);
591 if (rc)
592 return rc;
594 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
595 return -EIO;
596 if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
597 return -EINVAL;
599 return 0;
602 static const char *const mcdi_sft9001_cable_diag_names[] = {
603 "cable.pairA.length",
604 "cable.pairB.length",
605 "cable.pairC.length",
606 "cable.pairD.length",
607 "cable.pairA.status",
608 "cable.pairB.status",
609 "cable.pairC.status",
610 "cable.pairD.status",
613 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
614 int *results)
616 unsigned int retry, i, count = 0;
617 size_t outlen;
618 u32 status;
619 u8 *buf, *ptr;
620 int rc;
622 buf = kzalloc(0x100, GFP_KERNEL);
623 if (buf == NULL)
624 return -ENOMEM;
626 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
627 MCDI_SET_DWORD(buf, START_BIST_IN_TYPE, bist_mode);
628 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, buf, MC_CMD_START_BIST_IN_LEN,
629 NULL, 0, NULL);
630 if (rc)
631 goto out;
633 /* Wait up to 10s for BIST to finish */
634 for (retry = 0; retry < 100; ++retry) {
635 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
636 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
637 buf, 0x100, &outlen);
638 if (rc)
639 goto out;
641 status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
642 if (status != MC_CMD_POLL_BIST_RUNNING)
643 goto finished;
645 msleep(100);
648 rc = -ETIMEDOUT;
649 goto out;
651 finished:
652 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
654 /* SFT9001 specific cable diagnostics output */
655 if (efx->phy_type == PHY_TYPE_SFT9001B &&
656 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
657 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
658 ptr = MCDI_PTR(buf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
659 if (status == MC_CMD_POLL_BIST_PASSED &&
660 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
661 for (i = 0; i < 8; i++) {
662 results[count + i] =
663 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
664 EFX_DWORD_0);
667 count += 8;
669 rc = count;
671 out:
672 kfree(buf);
674 return rc;
677 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
678 unsigned flags)
680 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
681 u32 mode;
682 int rc;
684 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
685 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
686 if (rc < 0)
687 return rc;
689 results += rc;
692 /* If we support both LONG and SHORT, then run each in response to
693 * break or not. Otherwise, run the one we support */
694 mode = 0;
695 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN)) {
696 if ((flags & ETH_TEST_FL_OFFLINE) &&
697 (phy_cfg->flags &
698 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN)))
699 mode = MC_CMD_PHY_BIST_CABLE_LONG;
700 else
701 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
702 } else if (phy_cfg->flags &
703 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))
704 mode = MC_CMD_PHY_BIST_CABLE_LONG;
706 if (mode != 0) {
707 rc = efx_mcdi_bist(efx, mode, results);
708 if (rc < 0)
709 return rc;
710 results += rc;
713 return 0;
716 const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
718 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
720 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
721 if (index == 0)
722 return "bist";
723 --index;
726 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
727 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
728 if (index == 0)
729 return "cable";
730 --index;
732 if (efx->phy_type == PHY_TYPE_SFT9001B) {
733 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
734 return mcdi_sft9001_cable_diag_names[index];
735 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
739 return NULL;
742 struct efx_phy_operations efx_mcdi_phy_ops = {
743 .probe = efx_mcdi_phy_probe,
744 .init = efx_port_dummy_op_int,
745 .reconfigure = efx_mcdi_phy_reconfigure,
746 .poll = efx_mcdi_phy_poll,
747 .fini = efx_port_dummy_op_void,
748 .remove = efx_mcdi_phy_remove,
749 .get_settings = efx_mcdi_phy_get_settings,
750 .set_settings = efx_mcdi_phy_set_settings,
751 .test_alive = efx_mcdi_phy_test_alive,
752 .run_tests = efx_mcdi_phy_run_tests,
753 .test_name = efx_mcdi_phy_test_name,