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