Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[sfrench/cifs-2.6.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_cfg {
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_cfg *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         EFX_ERR(efx, "%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         EFX_ERR(efx, "%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         EFX_ERR(efx, "%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         EFX_ERR(efx, "%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         EFX_ERR(efx, "%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_cfg *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_cfg *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
399         return 0;
400
401 fail:
402         kfree(phy_data);
403         return rc;
404 }
405
406 int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
407 {
408         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
409         u32 caps = (efx->link_advertising ?
410                     ethtool_to_mcdi_cap(efx->link_advertising) :
411                     phy_cfg->forced_cap);
412
413         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
414                                  efx->loopback_mode, 0);
415 }
416
417 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
418                               struct efx_link_state *link_state,
419                               u32 speed, u32 flags, u32 fcntl)
420 {
421         switch (fcntl) {
422         case MC_CMD_FCNTL_AUTO:
423                 WARN_ON(1);     /* This is not a link mode */
424                 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
425                 break;
426         case MC_CMD_FCNTL_BIDIR:
427                 link_state->fc = EFX_FC_TX | EFX_FC_RX;
428                 break;
429         case MC_CMD_FCNTL_RESPOND:
430                 link_state->fc = EFX_FC_RX;
431                 break;
432         default:
433                 WARN_ON(1);
434         case MC_CMD_FCNTL_OFF:
435                 link_state->fc = 0;
436                 break;
437         }
438
439         link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
440         link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
441         link_state->speed = speed;
442 }
443
444 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
445  * supported by the link partner. Warn the user if this isn't the case
446  */
447 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
448 {
449         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
450         u32 rmtadv;
451
452         /* The link partner capabilities are only relevent if the
453          * link supports flow control autonegotiation */
454         if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
455                 return;
456
457         /* If flow control autoneg is supported and enabled, then fine */
458         if (efx->wanted_fc & EFX_FC_AUTO)
459                 return;
460
461         rmtadv = 0;
462         if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
463                 rmtadv |= ADVERTISED_Pause;
464         if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
465                 rmtadv |=  ADVERTISED_Asym_Pause;
466
467         if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
468                 EFX_ERR(efx, "warning: link partner doesn't support "
469                         "pause frames");
470 }
471
472 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
473 {
474         struct efx_link_state old_state = efx->link_state;
475         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
476         int rc;
477
478         WARN_ON(!mutex_is_locked(&efx->mac_lock));
479
480         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
481
482         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
483                           outbuf, sizeof(outbuf), NULL);
484         if (rc) {
485                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
486                 efx->link_state.up = false;
487         } else {
488                 efx_mcdi_phy_decode_link(
489                         efx, &efx->link_state,
490                         MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
491                         MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
492                         MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
493         }
494
495         return !efx_link_state_equal(&efx->link_state, &old_state);
496 }
497
498 static void efx_mcdi_phy_remove(struct efx_nic *efx)
499 {
500         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
501
502         efx->phy_data = NULL;
503         kfree(phy_data);
504 }
505
506 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
507 {
508         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
509         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
510         int rc;
511
512         ecmd->supported =
513                 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
514         ecmd->advertising = efx->link_advertising;
515         ecmd->speed = efx->link_state.speed;
516         ecmd->duplex = efx->link_state.fd;
517         ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
518         ecmd->phy_address = phy_cfg->port;
519         ecmd->transceiver = XCVR_INTERNAL;
520         ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
521         ecmd->mdio_support = (efx->mdio.mode_support &
522                               (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
523
524         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
525         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
526                           outbuf, sizeof(outbuf), NULL);
527         if (rc) {
528                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
529                 return;
530         }
531         ecmd->lp_advertising =
532                 mcdi_to_ethtool_cap(phy_cfg->media,
533                                     MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
534 }
535
536 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
537 {
538         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
539         u32 caps;
540         int rc;
541
542         if (ecmd->autoneg) {
543                 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
544                          1 << MC_CMD_PHY_CAP_AN_LBN);
545         } else if (ecmd->duplex) {
546                 switch (ecmd->speed) {
547                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;    break;
548                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;   break;
549                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;  break;
550                 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
551                 default:    return -EINVAL;
552                 }
553         } else {
554                 switch (ecmd->speed) {
555                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;    break;
556                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;   break;
557                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;  break;
558                 default:    return -EINVAL;
559                 }
560         }
561
562         rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
563                                efx->loopback_mode, 0);
564         if (rc)
565                 return rc;
566
567         if (ecmd->autoneg) {
568                 efx_link_set_advertising(
569                         efx, ecmd->advertising | ADVERTISED_Autoneg);
570                 phy_cfg->forced_cap = 0;
571         } else {
572                 efx_link_set_advertising(efx, 0);
573                 phy_cfg->forced_cap = caps;
574         }
575         return 0;
576 }
577
578 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
579 {
580         u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
581         size_t outlen;
582         int rc;
583
584         BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
585
586         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
587                           outbuf, sizeof(outbuf), &outlen);
588         if (rc)
589                 return rc;
590
591         if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
592                 return -EIO;
593         if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
594                 return -EINVAL;
595
596         return 0;
597 }
598
599 static const char *const mcdi_sft9001_cable_diag_names[] = {
600         "cable.pairA.length",
601         "cable.pairB.length",
602         "cable.pairC.length",
603         "cable.pairD.length",
604         "cable.pairA.status",
605         "cable.pairB.status",
606         "cable.pairC.status",
607         "cable.pairD.status",
608 };
609
610 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
611                          int *results)
612 {
613         unsigned int retry, i, count = 0;
614         size_t outlen;
615         u32 status;
616         u8 *buf, *ptr;
617         int rc;
618
619         buf = kzalloc(0x100, GFP_KERNEL);
620         if (buf == NULL)
621                 return -ENOMEM;
622
623         BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
624         MCDI_SET_DWORD(buf, START_BIST_IN_TYPE, bist_mode);
625         rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, buf, MC_CMD_START_BIST_IN_LEN,
626                           NULL, 0, NULL);
627         if (rc)
628                 goto out;
629
630         /* Wait up to 10s for BIST to finish */
631         for (retry = 0; retry < 100; ++retry) {
632                 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
633                 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
634                                   buf, 0x100, &outlen);
635                 if (rc)
636                         goto out;
637
638                 status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
639                 if (status != MC_CMD_POLL_BIST_RUNNING)
640                         goto finished;
641
642                 msleep(100);
643         }
644
645         rc = -ETIMEDOUT;
646         goto out;
647
648 finished:
649         results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
650
651         /* SFT9001 specific cable diagnostics output */
652         if (efx->phy_type == PHY_TYPE_SFT9001B &&
653             (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
654              bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
655                 ptr = MCDI_PTR(buf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
656                 if (status == MC_CMD_POLL_BIST_PASSED &&
657                     outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
658                         for (i = 0; i < 8; i++) {
659                                 results[count + i] =
660                                         EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
661                                                         EFX_DWORD_0);
662                         }
663                 }
664                 count += 8;
665         }
666         rc = count;
667
668 out:
669         kfree(buf);
670
671         return rc;
672 }
673
674 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
675                                   unsigned flags)
676 {
677         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
678         u32 mode;
679         int rc;
680
681         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
682                 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
683                 if (rc < 0)
684                         return rc;
685
686                 results += rc;
687         }
688
689         /* If we support both LONG and SHORT, then run each in response to
690          * break or not. Otherwise, run the one we support */
691         mode = 0;
692         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN)) {
693                 if ((flags & ETH_TEST_FL_OFFLINE) &&
694                     (phy_cfg->flags &
695                      (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN)))
696                         mode = MC_CMD_PHY_BIST_CABLE_LONG;
697                 else
698                         mode = MC_CMD_PHY_BIST_CABLE_SHORT;
699         } else if (phy_cfg->flags &
700                    (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))
701                 mode = MC_CMD_PHY_BIST_CABLE_LONG;
702
703         if (mode != 0) {
704                 rc = efx_mcdi_bist(efx, mode, results);
705                 if (rc < 0)
706                         return rc;
707                 results += rc;
708         }
709
710         return 0;
711 }
712
713 const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
714 {
715         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
716
717         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
718                 if (index == 0)
719                         return "bist";
720                 --index;
721         }
722
723         if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
724                               (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
725                 if (index == 0)
726                         return "cable";
727                 --index;
728
729                 if (efx->phy_type == PHY_TYPE_SFT9001B) {
730                         if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
731                                 return mcdi_sft9001_cable_diag_names[index];
732                         index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
733                 }
734         }
735
736         return NULL;
737 }
738
739 struct efx_phy_operations efx_mcdi_phy_ops = {
740         .probe          = efx_mcdi_phy_probe,
741         .init           = efx_port_dummy_op_int,
742         .reconfigure    = efx_mcdi_phy_reconfigure,
743         .poll           = efx_mcdi_phy_poll,
744         .fini           = efx_port_dummy_op_void,
745         .remove         = efx_mcdi_phy_remove,
746         .get_settings   = efx_mcdi_phy_get_settings,
747         .set_settings   = efx_mcdi_phy_set_settings,
748         .test_alive     = efx_mcdi_phy_test_alive,
749         .run_tests      = efx_mcdi_phy_run_tests,
750         .test_name      = efx_mcdi_phy_test_name,
751 };