]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/sfc/mcdi_phy.c
sfc: Add support for SFC9000 family (1)
[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 "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_cfg;
308         int rc;
309
310         /* TODO: Move phy_data initialisation to
311          * phy_op->probe/remove, rather than init/fini */
312         phy_cfg = kzalloc(sizeof(*phy_cfg), GFP_KERNEL);
313         if (phy_cfg == NULL) {
314                 rc = -ENOMEM;
315                 goto fail_alloc;
316         }
317         rc = efx_mcdi_get_phy_cfg(efx, phy_cfg);
318         if (rc != 0)
319                 goto fail;
320
321         efx->phy_type = phy_cfg->type;
322
323         efx->mdio_bus = phy_cfg->channel;
324         efx->mdio.prtad = phy_cfg->port;
325         efx->mdio.mmds = phy_cfg->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
326         efx->mdio.mode_support = 0;
327         if (phy_cfg->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
328                 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
329         if (phy_cfg->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
330                 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
331
332         /* Assert that we can map efx -> mcdi loopback modes */
333         BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
334         BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
335         BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
336         BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
337         BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
338         BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
339         BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
340         BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
341         BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
342         BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
343         BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
344         BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
345         BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
346         BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
347         BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
348         BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
349         BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
350         BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
351         BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
352         BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
353         BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
354         BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
355         BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
356         BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
357         BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
358         BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
359         BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
360
361         rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
362         if (rc != 0)
363                 goto fail;
364         /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
365          * but by convention we don't */
366         efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
367
368         kfree(phy_cfg);
369
370         return 0;
371
372 fail:
373         kfree(phy_cfg);
374 fail_alloc:
375         return rc;
376 }
377
378 static int efx_mcdi_phy_init(struct efx_nic *efx)
379 {
380         struct efx_mcdi_phy_cfg *phy_data;
381         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
382         u32 caps;
383         int rc;
384
385         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
386         if (phy_data == NULL)
387                 return -ENOMEM;
388
389         rc = efx_mcdi_get_phy_cfg(efx, phy_data);
390         if (rc != 0)
391                 goto fail;
392
393         efx->phy_data = phy_data;
394
395         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
396         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
397                           outbuf, sizeof(outbuf), NULL);
398         if (rc)
399                 goto fail;
400
401         caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
402         if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
403                 efx->link_advertising =
404                         mcdi_to_ethtool_cap(phy_data->media, caps);
405         else
406                 phy_data->forced_cap = caps;
407
408         return 0;
409
410 fail:
411         kfree(phy_data);
412         return rc;
413 }
414
415 int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
416 {
417         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
418         u32 caps = (efx->link_advertising ?
419                     ethtool_to_mcdi_cap(efx->link_advertising) :
420                     phy_cfg->forced_cap);
421
422         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
423                                  efx->loopback_mode, 0);
424 }
425
426 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
427                               struct efx_link_state *link_state,
428                               u32 speed, u32 flags, u32 fcntl)
429 {
430         switch (fcntl) {
431         case MC_CMD_FCNTL_AUTO:
432                 WARN_ON(1);     /* This is not a link mode */
433                 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
434                 break;
435         case MC_CMD_FCNTL_BIDIR:
436                 link_state->fc = EFX_FC_TX | EFX_FC_RX;
437                 break;
438         case MC_CMD_FCNTL_RESPOND:
439                 link_state->fc = EFX_FC_RX;
440                 break;
441         default:
442                 WARN_ON(1);
443         case MC_CMD_FCNTL_OFF:
444                 link_state->fc = 0;
445                 break;
446         }
447
448         link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
449         link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
450         link_state->speed = speed;
451 }
452
453 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
454  * supported by the link partner. Warn the user if this isn't the case
455  */
456 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
457 {
458         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
459         u32 rmtadv;
460
461         /* The link partner capabilities are only relevent if the
462          * link supports flow control autonegotiation */
463         if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
464                 return;
465
466         /* If flow control autoneg is supported and enabled, then fine */
467         if (efx->wanted_fc & EFX_FC_AUTO)
468                 return;
469
470         rmtadv = 0;
471         if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
472                 rmtadv |= ADVERTISED_Pause;
473         if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
474                 rmtadv |=  ADVERTISED_Asym_Pause;
475
476         if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
477                 EFX_ERR(efx, "warning: link partner doesn't support "
478                         "pause frames");
479 }
480
481 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
482 {
483         struct efx_link_state old_state = efx->link_state;
484         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
485         int rc;
486
487         WARN_ON(!mutex_is_locked(&efx->mac_lock));
488
489         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
490
491         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
492                           outbuf, sizeof(outbuf), NULL);
493         if (rc) {
494                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
495                 efx->link_state.up = false;
496         } else {
497                 efx_mcdi_phy_decode_link(
498                         efx, &efx->link_state,
499                         MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
500                         MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
501                         MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
502         }
503
504         return !efx_link_state_equal(&efx->link_state, &old_state);
505 }
506
507 static void efx_mcdi_phy_fini(struct efx_nic *efx)
508 {
509         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
510
511         efx->phy_data = NULL;
512         kfree(phy_data);
513 }
514
515 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
516 {
517         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
518         u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
519         int rc;
520
521         ecmd->supported =
522                 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
523         ecmd->advertising = efx->link_advertising;
524         ecmd->speed = efx->link_state.speed;
525         ecmd->duplex = efx->link_state.fd;
526         ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
527         ecmd->phy_address = phy_cfg->port;
528         ecmd->transceiver = XCVR_INTERNAL;
529         ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
530         ecmd->mdio_support = (efx->mdio.mode_support &
531                               (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
532
533         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
534         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
535                           outbuf, sizeof(outbuf), NULL);
536         if (rc) {
537                 EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
538                 return;
539         }
540         ecmd->lp_advertising =
541                 mcdi_to_ethtool_cap(phy_cfg->media,
542                                     MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
543 }
544
545 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
546 {
547         struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
548         u32 caps;
549         int rc;
550
551         if (ecmd->autoneg) {
552                 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
553                          1 << MC_CMD_PHY_CAP_AN_LBN);
554         } else if (ecmd->duplex) {
555                 switch (ecmd->speed) {
556                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;    break;
557                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;   break;
558                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;  break;
559                 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
560                 default:    return -EINVAL;
561                 }
562         } else {
563                 switch (ecmd->speed) {
564                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;    break;
565                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;   break;
566                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;  break;
567                 default:    return -EINVAL;
568                 }
569         }
570
571         rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
572                                efx->loopback_mode, 0);
573         if (rc)
574                 return rc;
575
576         if (ecmd->autoneg) {
577                 efx_link_set_advertising(
578                         efx, ecmd->advertising | ADVERTISED_Autoneg);
579                 phy_cfg->forced_cap = 0;
580         } else {
581                 efx_link_set_advertising(efx, 0);
582                 phy_cfg->forced_cap = caps;
583         }
584         return 0;
585 }
586
587 struct efx_phy_operations efx_mcdi_phy_ops = {
588         .probe          = efx_mcdi_phy_probe,
589         .init           = efx_mcdi_phy_init,
590         .reconfigure    = efx_mcdi_phy_reconfigure,
591         .poll           = efx_mcdi_phy_poll,
592         .fini           = efx_mcdi_phy_fini,
593         .get_settings   = efx_mcdi_phy_get_settings,
594         .set_settings   = efx_mcdi_phy_set_settings,
595         .run_tests      = NULL,
596         .test_name      = NULL,
597 };