c992742446b167d2a660665bda43eced3445fcab
[pandora-kernel.git] / drivers / net / sfc / mcdi_phy.c
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
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"
22
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;
35 };
36
37 static int
38 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
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) {
53                 rc = -EIO;
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
73 fail:
74         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
75         return rc;
76 }
77
78 static 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
99 fail:
100         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
101         return rc;
102 }
103
104 static 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) {
116                 rc = -EIO;
117                 goto fail;
118         }
119
120         *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
121
122         return 0;
123
124 fail:
125         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
126         return rc;
127 }
128
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)
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
152 fail:
153         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
154         return rc;
155 }
156
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)
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
180 fail:
181         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
182         return rc;
183 }
184
185 static 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
232 static 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
260 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
261 {
262         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
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
288 static 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
308 static int efx_mcdi_phy_probe(struct efx_nic *efx)
309 {
310         struct efx_mcdi_phy_data *phy_data;
311         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
312         u32 caps;
313         int rc;
314
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);
321         if (rc != 0)
322                 goto fail;
323
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;
334
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;
343
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
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
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;
398         efx_link_set_wanted_fc(efx, efx->wanted_fc);
399
400         return 0;
401
402 fail:
403         kfree(phy_data);
404         return rc;
405 }
406
407 int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
408 {
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);
413
414         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
415                                  efx->loopback_mode, 0);
416 }
417
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)
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  */
448 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
449 {
450         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
451         u32 rmtadv;
452
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;
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                 netif_err(efx, link, efx->net_dev,
470                           "warning: link partner doesn't support pause frames");
471 }
472
473 static 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                 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));
495         }
496
497         return !efx_link_state_equal(&efx->link_state, &old_state);
498 }
499
500 static void efx_mcdi_phy_remove(struct efx_nic *efx)
501 {
502         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
503
504         efx->phy_data = NULL;
505         kfree(phy_data);
506 }
507
508 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
509 {
510         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
511         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
512         int rc;
513
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));
525
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;
533         }
534         ecmd->lp_advertising =
535                 mcdi_to_ethtool_cap(phy_cfg->media,
536                                     MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
537 }
538
539 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
540 {
541         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
542         u32 caps;
543         int rc;
544
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;
555                 }
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;
562                 }
563         }
564
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;
569
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;
577         }
578         return 0;
579 }
580
581 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
582 {
583         u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
584         size_t outlen;
585         int rc;
586
587         BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
588
589         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
590                           outbuf, sizeof(outbuf), &outlen);
591         if (rc)
592                 return rc;
593
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;
598
599         return 0;
600 }
601
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",
611 };
612
613 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
614                          int *results)
615 {
616         unsigned int retry, i, count = 0;
617         size_t outlen;
618         u32 status;
619         u8 *buf, *ptr;
620         int rc;
621
622         buf = kzalloc(0x100, GFP_KERNEL);
623         if (buf == NULL)
624                 return -ENOMEM;
625
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;
632
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;
640
641                 status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
642                 if (status != MC_CMD_POLL_BIST_RUNNING)
643                         goto finished;
644
645                 msleep(100);
646         }
647
648         rc = -ETIMEDOUT;
649         goto out;
650
651 finished:
652         results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
653
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);
665                         }
666                 }
667                 count += 8;
668         }
669         rc = count;
670
671 out:
672         kfree(buf);
673
674         return rc;
675 }
676
677 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
678                                   unsigned flags)
679 {
680         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
681         u32 mode;
682         int rc;
683
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;
688
689                 results += rc;
690         }
691
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;
705
706         if (mode != 0) {
707                 rc = efx_mcdi_bist(efx, mode, results);
708                 if (rc < 0)
709                         return rc;
710                 results += rc;
711         }
712
713         return 0;
714 }
715
716 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
717                                           unsigned int index)
718 {
719         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
720
721         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
722                 if (index == 0)
723                         return "bist";
724                 --index;
725         }
726
727         if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
728                               (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
729                 if (index == 0)
730                         return "cable";
731                 --index;
732
733                 if (efx->phy_type == PHY_TYPE_SFT9001B) {
734                         if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
735                                 return mcdi_sft9001_cable_diag_names[index];
736                         index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
737                 }
738         }
739
740         return NULL;
741 }
742
743 struct efx_phy_operations efx_mcdi_phy_ops = {
744         .probe          = efx_mcdi_phy_probe,
745         .init           = efx_port_dummy_op_int,
746         .reconfigure    = efx_mcdi_phy_reconfigure,
747         .poll           = efx_mcdi_phy_poll,
748         .fini           = efx_port_dummy_op_void,
749         .remove         = efx_mcdi_phy_remove,
750         .get_settings   = efx_mcdi_phy_get_settings,
751         .set_settings   = efx_mcdi_phy_set_settings,
752         .test_alive     = efx_mcdi_phy_test_alive,
753         .run_tests      = efx_mcdi_phy_run_tests,
754         .test_name      = efx_mcdi_phy_test_name,
755 };