]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/sfc/mcdi_phy.c
Merge branch 'master' of /repos/git/net-next-2.6
[net-next-2.6.git] / drivers / net / sfc / mcdi_phy.c
CommitLineData
afd4aea0
BH
1/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2009 Solarflare Communications Inc.
4 *
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 */
9
10/*
11 * Driver for PHY related operations via MCDI.
12 */
13
5a0e3ad6 14#include <linux/slab.h>
afd4aea0
BH
15#include "efx.h"
16#include "phy.h"
17#include "mcdi.h"
18#include "mcdi_pcol.h"
19#include "mdio_10g.h"
affaf485
SH
20#include "nic.h"
21#include "selftest.h"
afd4aea0 22
3bd93035 23struct efx_mcdi_phy_data {
afd4aea0
BH
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;
35};
36
37static int
3bd93035 38efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
afd4aea0
BH
39{
40 u8 outbuf[MC_CMD_GET_PHY_CFG_OUT_LEN];
41 size_t outlen;
42 int rc;
43
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));
46
47 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
48 outbuf, sizeof(outbuf), &outlen);
49 if (rc)
50 goto fail;
51
52 if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
00bbb4a5 53 rc = -EIO;
afd4aea0
BH
54 goto fail;
55 }
56
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));
70
71 return 0;
72
73fail:
74 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
75 return rc;
76}
77
78static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
79 u32 flags, u32 loopback_mode,
80 u32 loopback_speed)
81{
82 u8 inbuf[MC_CMD_SET_LINK_IN_LEN];
83 int rc;
84
85 BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
86
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);
91
92 rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
93 NULL, 0, NULL);
94 if (rc)
95 goto fail;
96
97 return 0;
98
99fail:
100 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
101 return rc;
102}
103
104static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
105{
106 u8 outbuf[MC_CMD_GET_LOOPBACK_MODES_OUT_LEN];
107 size_t outlen;
108 int rc;
109
110 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
111 outbuf, sizeof(outbuf), &outlen);
112 if (rc)
113 goto fail;
114
115 if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
00bbb4a5 116 rc = -EIO;
afd4aea0
BH
117 goto fail;
118 }
119
120 *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
121
122 return 0;
123
124fail:
125 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
126 return rc;
127}
128
129int 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)
132{
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;
137
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);
142
143 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
144 outbuf, sizeof(outbuf), &outlen);
145 if (rc)
146 goto fail;
147
148 *value_out = (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
149 *status_out = MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS);
150 return 0;
151
152fail:
153 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
154 return rc;
155}
156
157int 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)
160{
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;
165
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);
171
172 rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
173 outbuf, sizeof(outbuf), &outlen);
174 if (rc)
175 goto fail;
176
177 *status_out = MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS);
178 return 0;
179
180fail:
181 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
182 return rc;
183}
184
185static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
186{
187 u32 result = 0;
188
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;
197
198 case MC_CMD_MEDIA_XFP:
199 case MC_CMD_MEDIA_SFP_PLUS:
200 result |= SUPPORTED_FIBRE;
201 break;
202
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;
220 }
221
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;
228
229 return result;
230}
231
232static u32 ethtool_to_mcdi_cap(u32 cap)
233{
234 u32 result = 0;
235
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);
256
257 return result;
258}
259
260static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
261{
3bd93035 262 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
afd4aea0
BH
263 enum efx_phy_mode mode, supported;
264 u32 flags;
265
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;
274
275 mode = efx->phy_mode & supported;
276
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);
284
285 return flags;
286}
287
288static u32 mcdi_to_ethtool_media(u32 media)
289{
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;
295
296 case MC_CMD_MEDIA_XFP:
297 case MC_CMD_MEDIA_SFP_PLUS:
298 return PORT_FIBRE;
299
300 case MC_CMD_MEDIA_BASE_T:
301 return PORT_TP;
302
303 default:
304 return PORT_OTHER;
305 }
306}
307
308static int efx_mcdi_phy_probe(struct efx_nic *efx)
309{
3bd93035 310 struct efx_mcdi_phy_data *phy_data;
ff3b00a0
SH
311 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
312 u32 caps;
afd4aea0
BH
313 int rc;
314
ff3b00a0
SH
315 /* Initialise and populate phy_data */
316 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
317 if (phy_data == NULL)
318 return -ENOMEM;
319
320 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
afd4aea0
BH
321 if (rc != 0)
322 goto fail;
323
ff3b00a0
SH
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;
330
331 /* Fill out nic state */
332 efx->phy_data = phy_data;
333 efx->phy_type = phy_data->type;
afd4aea0 334
ff3b00a0
SH
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);
afd4aea0 338 efx->mdio.mode_support = 0;
ff3b00a0 339 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
afd4aea0 340 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
ff3b00a0 341 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
afd4aea0
BH
342 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
343
ff3b00a0
SH
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;
350
afd4aea0
BH
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);
379
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);
386
7a6b8f6f
SH
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));
393
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;
cffe9d4c 398 efx_link_set_wanted_fc(efx, efx->wanted_fc);
7a6b8f6f 399
afd4aea0
BH
400 return 0;
401
402fail:
403 kfree(phy_data);
404 return rc;
405}
406
407int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
408{
3bd93035 409 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
afd4aea0
BH
410 u32 caps = (efx->link_advertising ?
411 ethtool_to_mcdi_cap(efx->link_advertising) :
412 phy_cfg->forced_cap);
413
414 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
415 efx->loopback_mode, 0);
416}
417
418void efx_mcdi_phy_decode_link(struct efx_nic *efx,
419 struct efx_link_state *link_state,
420 u32 speed, u32 flags, u32 fcntl)
421{
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;
438 }
439
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;
443}
444
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
447 */
448void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
449{
3bd93035 450 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
afd4aea0
BH
451 u32 rmtadv;
452
453 /* The link partner capabilities are only relevent if the
454 * link supports flow control autonegotiation */
7a6b8f6f 455 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
afd4aea0
BH
456 return;
457
458 /* If flow control autoneg is supported and enabled, then fine */
459 if (efx->wanted_fc & EFX_FC_AUTO)
460 return;
461
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;
467
468 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
469 EFX_ERR(efx, "warning: link partner doesn't support "
470 "pause frames");
471}
472
473static bool efx_mcdi_phy_poll(struct efx_nic *efx)
474{
475 struct efx_link_state old_state = efx->link_state;
476 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
477 int rc;
478
479 WARN_ON(!mutex_is_locked(&efx->mac_lock));
480
481 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
482
483 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
484 outbuf, sizeof(outbuf), NULL);
485 if (rc) {
486 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
487 efx->link_state.up = false;
488 } else {
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));
494 }
495
496 return !efx_link_state_equal(&efx->link_state, &old_state);
497}
498
ff3b00a0 499static void efx_mcdi_phy_remove(struct efx_nic *efx)
afd4aea0
BH
500{
501 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
502
503 efx->phy_data = NULL;
504 kfree(phy_data);
505}
506
507static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
508{
3bd93035 509 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
afd4aea0
BH
510 u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
511 int rc;
512
513 ecmd->supported =
514 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
515 ecmd->advertising = efx->link_advertising;
516 ecmd->speed = 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));
524
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);
528 if (rc) {
529 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
530 return;
531 }
532 ecmd->lp_advertising =
533 mcdi_to_ethtool_cap(phy_cfg->media,
534 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
535}
536
537static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
538{
3bd93035 539 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
afd4aea0
BH
540 u32 caps;
541 int rc;
542
543 if (ecmd->autoneg) {
544 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
545 1 << MC_CMD_PHY_CAP_AN_LBN);
546 } else if (ecmd->duplex) {
547 switch (ecmd->speed) {
548 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
549 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
550 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
551 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
552 default: return -EINVAL;
553 }
554 } else {
555 switch (ecmd->speed) {
556 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
557 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
558 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
559 default: return -EINVAL;
560 }
561 }
562
563 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
564 efx->loopback_mode, 0);
565 if (rc)
566 return rc;
567
568 if (ecmd->autoneg) {
569 efx_link_set_advertising(
570 efx, ecmd->advertising | ADVERTISED_Autoneg);
571 phy_cfg->forced_cap = 0;
572 } else {
573 efx_link_set_advertising(efx, 0);
574 phy_cfg->forced_cap = caps;
575 }
576 return 0;
577}
578
4f16c073
BH
579static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
580{
581 u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
582 size_t outlen;
583 int rc;
584
585 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
586
587 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
588 outbuf, sizeof(outbuf), &outlen);
589 if (rc)
590 return rc;
591
592 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
00bbb4a5 593 return -EIO;
4f16c073
BH
594 if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
595 return -EINVAL;
596
597 return 0;
598}
599
affaf485
SH
600static const char *const mcdi_sft9001_cable_diag_names[] = {
601 "cable.pairA.length",
602 "cable.pairB.length",
603 "cable.pairC.length",
604 "cable.pairD.length",
605 "cable.pairA.status",
606 "cable.pairB.status",
607 "cable.pairC.status",
608 "cable.pairD.status",
609};
610
611static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
612 int *results)
613{
614 unsigned int retry, i, count = 0;
615 size_t outlen;
616 u32 status;
617 u8 *buf, *ptr;
618 int rc;
619
620 buf = kzalloc(0x100, GFP_KERNEL);
621 if (buf == NULL)
622 return -ENOMEM;
623
624 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
625 MCDI_SET_DWORD(buf, START_BIST_IN_TYPE, bist_mode);
626 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, buf, MC_CMD_START_BIST_IN_LEN,
627 NULL, 0, NULL);
628 if (rc)
629 goto out;
630
631 /* Wait up to 10s for BIST to finish */
632 for (retry = 0; retry < 100; ++retry) {
633 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
634 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
635 buf, 0x100, &outlen);
636 if (rc)
637 goto out;
638
639 status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
640 if (status != MC_CMD_POLL_BIST_RUNNING)
641 goto finished;
642
643 msleep(100);
644 }
645
646 rc = -ETIMEDOUT;
647 goto out;
648
649finished:
650 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
651
652 /* SFT9001 specific cable diagnostics output */
653 if (efx->phy_type == PHY_TYPE_SFT9001B &&
654 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
655 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
656 ptr = MCDI_PTR(buf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
657 if (status == MC_CMD_POLL_BIST_PASSED &&
658 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
659 for (i = 0; i < 8; i++) {
660 results[count + i] =
661 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
662 EFX_DWORD_0);
663 }
664 }
665 count += 8;
666 }
667 rc = count;
668
669out:
670 kfree(buf);
671
672 return rc;
673}
674
675static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
676 unsigned flags)
677{
3bd93035 678 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
affaf485
SH
679 u32 mode;
680 int rc;
681
682 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
683 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
684 if (rc < 0)
685 return rc;
686
687 results += rc;
688 }
689
690 /* If we support both LONG and SHORT, then run each in response to
691 * break or not. Otherwise, run the one we support */
692 mode = 0;
693 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN)) {
694 if ((flags & ETH_TEST_FL_OFFLINE) &&
695 (phy_cfg->flags &
696 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN)))
697 mode = MC_CMD_PHY_BIST_CABLE_LONG;
698 else
699 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
700 } else if (phy_cfg->flags &
701 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))
702 mode = MC_CMD_PHY_BIST_CABLE_LONG;
703
704 if (mode != 0) {
705 rc = efx_mcdi_bist(efx, mode, results);
706 if (rc < 0)
707 return rc;
708 results += rc;
709 }
710
711 return 0;
712}
713
714const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
715{
3bd93035 716 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
affaf485
SH
717
718 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
719 if (index == 0)
720 return "bist";
721 --index;
722 }
723
724 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
725 (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
726 if (index == 0)
727 return "cable";
728 --index;
729
730 if (efx->phy_type == PHY_TYPE_SFT9001B) {
731 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
732 return mcdi_sft9001_cable_diag_names[index];
733 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
734 }
735 }
736
737 return NULL;
738}
739
afd4aea0
BH
740struct efx_phy_operations efx_mcdi_phy_ops = {
741 .probe = efx_mcdi_phy_probe,
ff3b00a0 742 .init = efx_port_dummy_op_int,
afd4aea0
BH
743 .reconfigure = efx_mcdi_phy_reconfigure,
744 .poll = efx_mcdi_phy_poll,
ff3b00a0
SH
745 .fini = efx_port_dummy_op_void,
746 .remove = efx_mcdi_phy_remove,
afd4aea0
BH
747 .get_settings = efx_mcdi_phy_get_settings,
748 .set_settings = efx_mcdi_phy_set_settings,
4f16c073 749 .test_alive = efx_mcdi_phy_test_alive,
affaf485
SH
750 .run_tests = efx_mcdi_phy_run_tests,
751 .test_name = efx_mcdi_phy_test_name,
afd4aea0 752};