sfc: Replace PHY MDIO test with an 'alive' test
[safe/jmp/linux-2.6] / 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 "efx.h"
15 #include "phy.h"
16 #include "mcdi.h"
17 #include "mcdi_pcol.h"
18 #include "mdio_10g.h"
19
20 struct efx_mcdi_phy_cfg {
21         u32 flags;
22         u32 type;
23         u32 supported_cap;
24         u32 channel;
25         u32 port;
26         u32 stats_mask;
27         u8 name[20];
28         u32 media;
29         u32 mmd_mask;
30         u8 revision[20];
31         u32 forced_cap;
32 };
33
34 static int
35 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_cfg *cfg)
36 {
37         u8 outbuf[MC_CMD_GET_PHY_CFG_OUT_LEN];
38         size_t outlen;
39         int rc;
40
41         BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
42         BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
43
44         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
45                           outbuf, sizeof(outbuf), &outlen);
46         if (rc)
47                 goto fail;
48
49         if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
50                 rc = -EMSGSIZE;
51                 goto fail;
52         }
53
54         cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
55         cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
56         cfg->supported_cap =
57                 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
58         cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
59         cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
60         cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
61         memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
62                sizeof(cfg->name));
63         cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
64         cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
65         memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
66                sizeof(cfg->revision));
67
68         return 0;
69
70 fail:
71         EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
72         return rc;
73 }
74
75 static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
76                              u32 flags, u32 loopback_mode,
77                              u32 loopback_speed)
78 {
79         u8 inbuf[MC_CMD_SET_LINK_IN_LEN];
80         int rc;
81
82         BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
83
84         MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
85         MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
86         MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
87         MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
88
89         rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
90                           NULL, 0, NULL);
91         if (rc)
92                 goto fail;
93
94         return 0;
95
96 fail:
97         EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
98         return rc;
99 }
100
101 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
102 {
103         u8 outbuf[MC_CMD_GET_LOOPBACK_MODES_OUT_LEN];
104         size_t outlen;
105         int rc;
106
107         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
108                           outbuf, sizeof(outbuf), &outlen);
109         if (rc)
110                 goto fail;
111
112         if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
113                 rc = -EMSGSIZE;
114                 goto fail;
115         }
116
117         *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
118
119         return 0;
120
121 fail:
122         EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
123         return rc;
124 }
125
126 int efx_mcdi_mdio_read(struct efx_nic *efx, unsigned int bus,
127                          unsigned int prtad, unsigned int devad, u16 addr,
128                          u16 *value_out, u32 *status_out)
129 {
130         u8 inbuf[MC_CMD_MDIO_READ_IN_LEN];
131         u8 outbuf[MC_CMD_MDIO_READ_OUT_LEN];
132         size_t outlen;
133         int rc;
134
135         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, bus);
136         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
137         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
138         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
139
140         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
141                           outbuf, sizeof(outbuf), &outlen);
142         if (rc)
143                 goto fail;
144
145         *value_out = (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
146         *status_out = MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS);
147         return 0;
148
149 fail:
150         EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
151         return rc;
152 }
153
154 int efx_mcdi_mdio_write(struct efx_nic *efx, unsigned int bus,
155                           unsigned int prtad, unsigned int devad, u16 addr,
156                           u16 value, u32 *status_out)
157 {
158         u8 inbuf[MC_CMD_MDIO_WRITE_IN_LEN];
159         u8 outbuf[MC_CMD_MDIO_WRITE_OUT_LEN];
160         size_t outlen;
161         int rc;
162
163         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, bus);
164         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
165         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
166         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
167         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
168
169         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
170                           outbuf, sizeof(outbuf), &outlen);
171         if (rc)
172                 goto fail;
173
174         *status_out = MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS);
175         return 0;
176
177 fail:
178         EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
179         return rc;
180 }
181
182 static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
183 {
184         u32 result = 0;
185
186         switch (media) {
187         case MC_CMD_MEDIA_KX4:
188                 result |= SUPPORTED_Backplane;
189                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
190                         result |= SUPPORTED_1000baseKX_Full;
191                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
192                         result |= SUPPORTED_10000baseKX4_Full;
193                 break;
194
195         case MC_CMD_MEDIA_XFP:
196         case MC_CMD_MEDIA_SFP_PLUS:
197                 result |= SUPPORTED_FIBRE;
198                 break;
199
200         case MC_CMD_MEDIA_BASE_T:
201                 result |= SUPPORTED_TP;
202                 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
203                         result |= SUPPORTED_10baseT_Half;
204                 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
205                         result |= SUPPORTED_10baseT_Full;
206                 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
207                         result |= SUPPORTED_100baseT_Half;
208                 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
209                         result |= SUPPORTED_100baseT_Full;
210                 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
211                         result |= SUPPORTED_1000baseT_Half;
212                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
213                         result |= SUPPORTED_1000baseT_Full;
214                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
215                         result |= SUPPORTED_10000baseT_Full;
216                 break;
217         }
218
219         if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
220                 result |= SUPPORTED_Pause;
221         if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
222                 result |= SUPPORTED_Asym_Pause;
223         if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
224                 result |= SUPPORTED_Autoneg;
225
226         return result;
227 }
228
229 static u32 ethtool_to_mcdi_cap(u32 cap)
230 {
231         u32 result = 0;
232
233         if (cap & SUPPORTED_10baseT_Half)
234                 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
235         if (cap & SUPPORTED_10baseT_Full)
236                 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
237         if (cap & SUPPORTED_100baseT_Half)
238                 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
239         if (cap & SUPPORTED_100baseT_Full)
240                 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
241         if (cap & SUPPORTED_1000baseT_Half)
242                 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
243         if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
244                 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
245         if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
246                 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
247         if (cap & SUPPORTED_Pause)
248                 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
249         if (cap & SUPPORTED_Asym_Pause)
250                 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
251         if (cap & SUPPORTED_Autoneg)
252                 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
253
254         return result;
255 }
256
257 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
258 {
259         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
260         enum efx_phy_mode mode, supported;
261         u32 flags;
262
263         /* TODO: Advertise the capabilities supported by this PHY */
264         supported = 0;
265         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_TXDIS_LBN))
266                 supported |= PHY_MODE_TX_DISABLED;
267         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_LOWPOWER_LBN))
268                 supported |= PHY_MODE_LOW_POWER;
269         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_POWEROFF_LBN))
270                 supported |= PHY_MODE_OFF;
271
272         mode = efx->phy_mode & supported;
273
274         flags = 0;
275         if (mode & PHY_MODE_TX_DISABLED)
276                 flags |= (1 << MC_CMD_SET_LINK_TXDIS_LBN);
277         if (mode & PHY_MODE_LOW_POWER)
278                 flags |= (1 << MC_CMD_SET_LINK_LOWPOWER_LBN);
279         if (mode & PHY_MODE_OFF)
280                 flags |= (1 << MC_CMD_SET_LINK_POWEROFF_LBN);
281
282         return flags;
283 }
284
285 static u32 mcdi_to_ethtool_media(u32 media)
286 {
287         switch (media) {
288         case MC_CMD_MEDIA_XAUI:
289         case MC_CMD_MEDIA_CX4:
290         case MC_CMD_MEDIA_KX4:
291                 return PORT_OTHER;
292
293         case MC_CMD_MEDIA_XFP:
294         case MC_CMD_MEDIA_SFP_PLUS:
295                 return PORT_FIBRE;
296
297         case MC_CMD_MEDIA_BASE_T:
298                 return PORT_TP;
299
300         default:
301                 return PORT_OTHER;
302         }
303 }
304
305 static int efx_mcdi_phy_probe(struct efx_nic *efx)
306 {
307         struct efx_mcdi_phy_cfg *phy_data;
308         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
309         u32 caps;
310         int rc;
311
312         /* Initialise and populate phy_data */
313         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
314         if (phy_data == NULL)
315                 return -ENOMEM;
316
317         rc = efx_mcdi_get_phy_cfg(efx, phy_data);
318         if (rc != 0)
319                 goto fail;
320
321         /* Read initial link advertisement */
322         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
323         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
324                           outbuf, sizeof(outbuf), NULL);
325         if (rc)
326                 goto fail;
327
328         /* Fill out nic state */
329         efx->phy_data = phy_data;
330         efx->phy_type = phy_data->type;
331
332         efx->mdio_bus = phy_data->channel;
333         efx->mdio.prtad = phy_data->port;
334         efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
335         efx->mdio.mode_support = 0;
336         if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
337                 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
338         if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
339                 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
340
341         caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
342         if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
343                 efx->link_advertising =
344                         mcdi_to_ethtool_cap(phy_data->media, caps);
345         else
346                 phy_data->forced_cap = caps;
347
348         /* Assert that we can map efx -> mcdi loopback modes */
349         BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
350         BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
351         BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
352         BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
353         BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
354         BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
355         BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
356         BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
357         BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
358         BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
359         BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
360         BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
361         BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
362         BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
363         BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
364         BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
365         BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
366         BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
367         BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
368         BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
369         BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
370         BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
371         BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
372         BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
373         BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
374         BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
375         BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
376
377         rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
378         if (rc != 0)
379                 goto fail;
380         /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
381          * but by convention we don't */
382         efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
383
384         /* Set the initial link mode */
385         efx_mcdi_phy_decode_link(
386                 efx, &efx->link_state,
387                 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
388                 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
389                 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
390
391         /* Default to Autonegotiated flow control if the PHY supports it */
392         efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
393         if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
394                 efx->wanted_fc |= EFX_FC_AUTO;
395
396         return 0;
397
398 fail:
399         kfree(phy_data);
400         return rc;
401 }
402
403 int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
404 {
405         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
406         u32 caps = (efx->link_advertising ?
407                     ethtool_to_mcdi_cap(efx->link_advertising) :
408                     phy_cfg->forced_cap);
409
410         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
411                                  efx->loopback_mode, 0);
412 }
413
414 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
415                               struct efx_link_state *link_state,
416                               u32 speed, u32 flags, u32 fcntl)
417 {
418         switch (fcntl) {
419         case MC_CMD_FCNTL_AUTO:
420                 WARN_ON(1);     /* This is not a link mode */
421                 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
422                 break;
423         case MC_CMD_FCNTL_BIDIR:
424                 link_state->fc = EFX_FC_TX | EFX_FC_RX;
425                 break;
426         case MC_CMD_FCNTL_RESPOND:
427                 link_state->fc = EFX_FC_RX;
428                 break;
429         default:
430                 WARN_ON(1);
431         case MC_CMD_FCNTL_OFF:
432                 link_state->fc = 0;
433                 break;
434         }
435
436         link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
437         link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
438         link_state->speed = speed;
439 }
440
441 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
442  * supported by the link partner. Warn the user if this isn't the case
443  */
444 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
445 {
446         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
447         u32 rmtadv;
448
449         /* The link partner capabilities are only relevent if the
450          * link supports flow control autonegotiation */
451         if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
452                 return;
453
454         /* If flow control autoneg is supported and enabled, then fine */
455         if (efx->wanted_fc & EFX_FC_AUTO)
456                 return;
457
458         rmtadv = 0;
459         if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
460                 rmtadv |= ADVERTISED_Pause;
461         if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
462                 rmtadv |=  ADVERTISED_Asym_Pause;
463
464         if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
465                 EFX_ERR(efx, "warning: link partner doesn't support "
466                         "pause frames");
467 }
468
469 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
470 {
471         struct efx_link_state old_state = efx->link_state;
472         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
473         int rc;
474
475         WARN_ON(!mutex_is_locked(&efx->mac_lock));
476
477         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
478
479         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
480                           outbuf, sizeof(outbuf), NULL);
481         if (rc) {
482                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
483                 efx->link_state.up = false;
484         } else {
485                 efx_mcdi_phy_decode_link(
486                         efx, &efx->link_state,
487                         MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
488                         MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
489                         MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
490         }
491
492         return !efx_link_state_equal(&efx->link_state, &old_state);
493 }
494
495 static void efx_mcdi_phy_remove(struct efx_nic *efx)
496 {
497         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
498
499         efx->phy_data = NULL;
500         kfree(phy_data);
501 }
502
503 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
504 {
505         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
506         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
507         int rc;
508
509         ecmd->supported =
510                 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
511         ecmd->advertising = efx->link_advertising;
512         ecmd->speed = efx->link_state.speed;
513         ecmd->duplex = efx->link_state.fd;
514         ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
515         ecmd->phy_address = phy_cfg->port;
516         ecmd->transceiver = XCVR_INTERNAL;
517         ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
518         ecmd->mdio_support = (efx->mdio.mode_support &
519                               (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
520
521         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
522         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
523                           outbuf, sizeof(outbuf), NULL);
524         if (rc) {
525                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
526                 return;
527         }
528         ecmd->lp_advertising =
529                 mcdi_to_ethtool_cap(phy_cfg->media,
530                                     MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
531 }
532
533 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
534 {
535         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
536         u32 caps;
537         int rc;
538
539         if (ecmd->autoneg) {
540                 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
541                          1 << MC_CMD_PHY_CAP_AN_LBN);
542         } else if (ecmd->duplex) {
543                 switch (ecmd->speed) {
544                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;    break;
545                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;   break;
546                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;  break;
547                 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
548                 default:    return -EINVAL;
549                 }
550         } else {
551                 switch (ecmd->speed) {
552                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;    break;
553                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;   break;
554                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;  break;
555                 default:    return -EINVAL;
556                 }
557         }
558
559         rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
560                                efx->loopback_mode, 0);
561         if (rc)
562                 return rc;
563
564         if (ecmd->autoneg) {
565                 efx_link_set_advertising(
566                         efx, ecmd->advertising | ADVERTISED_Autoneg);
567                 phy_cfg->forced_cap = 0;
568         } else {
569                 efx_link_set_advertising(efx, 0);
570                 phy_cfg->forced_cap = caps;
571         }
572         return 0;
573 }
574
575 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
576 {
577         u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
578         size_t outlen;
579         int rc;
580
581         BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
582
583         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
584                           outbuf, sizeof(outbuf), &outlen);
585         if (rc)
586                 return rc;
587
588         if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
589                 return -EMSGSIZE;
590         if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
591                 return -EINVAL;
592
593         return 0;
594 }
595
596 struct efx_phy_operations efx_mcdi_phy_ops = {
597         .probe          = efx_mcdi_phy_probe,
598         .init           = efx_port_dummy_op_int,
599         .reconfigure    = efx_mcdi_phy_reconfigure,
600         .poll           = efx_mcdi_phy_poll,
601         .fini           = efx_port_dummy_op_void,
602         .remove         = efx_mcdi_phy_remove,
603         .get_settings   = efx_mcdi_phy_get_settings,
604         .set_settings   = efx_mcdi_phy_set_settings,
605         .test_alive     = efx_mcdi_phy_test_alive,
606         .run_tests      = NULL,
607         .test_name      = NULL,
608 };