]> Pileus Git - ~andy/linux/blob - drivers/net/ethernet/sfc/mcdi_port.c
sfc: Move efx_mcdi_mac_reconfigure() to siena.c and rename
[~andy/linux] / drivers / net / ethernet / sfc / mcdi_port.c
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2009-2010 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 "nic.h"
20 #include "selftest.h"
21
22 struct efx_mcdi_phy_data {
23         u32 flags;
24         u32 type;
25         u32 supported_cap;
26         u32 channel;
27         u32 port;
28         u32 stats_mask;
29         u8 name[20];
30         u32 media;
31         u32 mmd_mask;
32         u8 revision[20];
33         u32 forced_cap;
34 };
35
36 static int
37 efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
38 {
39         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_CFG_OUT_LEN);
40         size_t outlen;
41         int rc;
42
43         BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
44         BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
45
46         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
47                           outbuf, sizeof(outbuf), &outlen);
48         if (rc)
49                 goto fail;
50
51         if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
52                 rc = -EIO;
53                 goto fail;
54         }
55
56         cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
57         cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
58         cfg->supported_cap =
59                 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
60         cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
61         cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
62         cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
63         memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
64                sizeof(cfg->name));
65         cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
66         cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
67         memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
68                sizeof(cfg->revision));
69
70         return 0;
71
72 fail:
73         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
74         return rc;
75 }
76
77 static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
78                              u32 flags, u32 loopback_mode,
79                              u32 loopback_speed)
80 {
81         MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_LINK_IN_LEN);
82         int rc;
83
84         BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
85
86         MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
87         MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
88         MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
89         MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
90
91         rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
92                           NULL, 0, NULL);
93         if (rc)
94                 goto fail;
95
96         return 0;
97
98 fail:
99         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
100         return rc;
101 }
102
103 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
104 {
105         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
106         size_t outlen;
107         int rc;
108
109         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
110                           outbuf, sizeof(outbuf), &outlen);
111         if (rc)
112                 goto fail;
113
114         if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
115                 rc = -EIO;
116                 goto fail;
117         }
118
119         *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
120
121         return 0;
122
123 fail:
124         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
125         return rc;
126 }
127
128 static int efx_mcdi_mdio_read(struct net_device *net_dev,
129                               int prtad, int devad, u16 addr)
130 {
131         struct efx_nic *efx = netdev_priv(net_dev);
132         MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
133         MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
134         size_t outlen;
135         int rc;
136
137         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
138         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
139         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
140         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
141
142         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
143                           outbuf, sizeof(outbuf), &outlen);
144         if (rc)
145                 goto fail;
146
147         if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
148             MC_CMD_MDIO_STATUS_GOOD)
149                 return -EIO;
150
151         return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
152
153 fail:
154         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
155         return rc;
156 }
157
158 static int efx_mcdi_mdio_write(struct net_device *net_dev,
159                                int prtad, int devad, u16 addr, u16 value)
160 {
161         struct efx_nic *efx = netdev_priv(net_dev);
162         MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
163         MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
164         size_t outlen;
165         int rc;
166
167         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
168         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
169         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
170         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
171         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
172
173         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
174                           outbuf, sizeof(outbuf), &outlen);
175         if (rc)
176                 goto fail;
177
178         if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
179             MC_CMD_MDIO_STATUS_GOOD)
180                 return -EIO;
181
182         return 0;
183
184 fail:
185         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
186         return rc;
187 }
188
189 static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
190 {
191         u32 result = 0;
192
193         switch (media) {
194         case MC_CMD_MEDIA_KX4:
195                 result |= SUPPORTED_Backplane;
196                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
197                         result |= SUPPORTED_1000baseKX_Full;
198                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
199                         result |= SUPPORTED_10000baseKX4_Full;
200                 break;
201
202         case MC_CMD_MEDIA_XFP:
203         case MC_CMD_MEDIA_SFP_PLUS:
204                 result |= SUPPORTED_FIBRE;
205                 break;
206
207         case MC_CMD_MEDIA_BASE_T:
208                 result |= SUPPORTED_TP;
209                 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
210                         result |= SUPPORTED_10baseT_Half;
211                 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
212                         result |= SUPPORTED_10baseT_Full;
213                 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
214                         result |= SUPPORTED_100baseT_Half;
215                 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
216                         result |= SUPPORTED_100baseT_Full;
217                 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
218                         result |= SUPPORTED_1000baseT_Half;
219                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
220                         result |= SUPPORTED_1000baseT_Full;
221                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
222                         result |= SUPPORTED_10000baseT_Full;
223                 break;
224         }
225
226         if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
227                 result |= SUPPORTED_Pause;
228         if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
229                 result |= SUPPORTED_Asym_Pause;
230         if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
231                 result |= SUPPORTED_Autoneg;
232
233         return result;
234 }
235
236 static u32 ethtool_to_mcdi_cap(u32 cap)
237 {
238         u32 result = 0;
239
240         if (cap & SUPPORTED_10baseT_Half)
241                 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
242         if (cap & SUPPORTED_10baseT_Full)
243                 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
244         if (cap & SUPPORTED_100baseT_Half)
245                 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
246         if (cap & SUPPORTED_100baseT_Full)
247                 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
248         if (cap & SUPPORTED_1000baseT_Half)
249                 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
250         if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
251                 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
252         if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
253                 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
254         if (cap & SUPPORTED_Pause)
255                 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
256         if (cap & SUPPORTED_Asym_Pause)
257                 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
258         if (cap & SUPPORTED_Autoneg)
259                 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
260
261         return result;
262 }
263
264 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
265 {
266         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
267         enum efx_phy_mode mode, supported;
268         u32 flags;
269
270         /* TODO: Advertise the capabilities supported by this PHY */
271         supported = 0;
272         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
273                 supported |= PHY_MODE_TX_DISABLED;
274         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
275                 supported |= PHY_MODE_LOW_POWER;
276         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
277                 supported |= PHY_MODE_OFF;
278
279         mode = efx->phy_mode & supported;
280
281         flags = 0;
282         if (mode & PHY_MODE_TX_DISABLED)
283                 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
284         if (mode & PHY_MODE_LOW_POWER)
285                 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
286         if (mode & PHY_MODE_OFF)
287                 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
288
289         return flags;
290 }
291
292 static u32 mcdi_to_ethtool_media(u32 media)
293 {
294         switch (media) {
295         case MC_CMD_MEDIA_XAUI:
296         case MC_CMD_MEDIA_CX4:
297         case MC_CMD_MEDIA_KX4:
298                 return PORT_OTHER;
299
300         case MC_CMD_MEDIA_XFP:
301         case MC_CMD_MEDIA_SFP_PLUS:
302                 return PORT_FIBRE;
303
304         case MC_CMD_MEDIA_BASE_T:
305                 return PORT_TP;
306
307         default:
308                 return PORT_OTHER;
309         }
310 }
311
312 static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
313                               struct efx_link_state *link_state,
314                               u32 speed, u32 flags, u32 fcntl)
315 {
316         switch (fcntl) {
317         case MC_CMD_FCNTL_AUTO:
318                 WARN_ON(1);     /* This is not a link mode */
319                 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
320                 break;
321         case MC_CMD_FCNTL_BIDIR:
322                 link_state->fc = EFX_FC_TX | EFX_FC_RX;
323                 break;
324         case MC_CMD_FCNTL_RESPOND:
325                 link_state->fc = EFX_FC_RX;
326                 break;
327         default:
328                 WARN_ON(1);
329         case MC_CMD_FCNTL_OFF:
330                 link_state->fc = 0;
331                 break;
332         }
333
334         link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
335         link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
336         link_state->speed = speed;
337 }
338
339 static int efx_mcdi_phy_probe(struct efx_nic *efx)
340 {
341         struct efx_mcdi_phy_data *phy_data;
342         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
343         u32 caps;
344         int rc;
345
346         /* Initialise and populate phy_data */
347         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
348         if (phy_data == NULL)
349                 return -ENOMEM;
350
351         rc = efx_mcdi_get_phy_cfg(efx, phy_data);
352         if (rc != 0)
353                 goto fail;
354
355         /* Read initial link advertisement */
356         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
357         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
358                           outbuf, sizeof(outbuf), NULL);
359         if (rc)
360                 goto fail;
361
362         /* Fill out nic state */
363         efx->phy_data = phy_data;
364         efx->phy_type = phy_data->type;
365
366         efx->mdio_bus = phy_data->channel;
367         efx->mdio.prtad = phy_data->port;
368         efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
369         efx->mdio.mode_support = 0;
370         if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
371                 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
372         if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
373                 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
374
375         caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
376         if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
377                 efx->link_advertising =
378                         mcdi_to_ethtool_cap(phy_data->media, caps);
379         else
380                 phy_data->forced_cap = caps;
381
382         /* Assert that we can map efx -> mcdi loopback modes */
383         BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
384         BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
385         BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
386         BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
387         BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
388         BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
389         BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
390         BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
391         BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
392         BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
393         BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
394         BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
395         BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
396         BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
397         BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
398         BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
399         BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
400         BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
401         BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
402         BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
403         BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
404         BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
405         BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
406         BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
407         BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
408         BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
409         BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
410
411         rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
412         if (rc != 0)
413                 goto fail;
414         /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
415          * but by convention we don't */
416         efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
417
418         /* Set the initial link mode */
419         efx_mcdi_phy_decode_link(
420                 efx, &efx->link_state,
421                 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
422                 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
423                 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
424
425         /* Default to Autonegotiated flow control if the PHY supports it */
426         efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
427         if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
428                 efx->wanted_fc |= EFX_FC_AUTO;
429         efx_link_set_wanted_fc(efx, efx->wanted_fc);
430
431         return 0;
432
433 fail:
434         kfree(phy_data);
435         return rc;
436 }
437
438 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
439 {
440         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
441         u32 caps = (efx->link_advertising ?
442                     ethtool_to_mcdi_cap(efx->link_advertising) :
443                     phy_cfg->forced_cap);
444
445         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
446                                  efx->loopback_mode, 0);
447 }
448
449 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
450  * supported by the link partner. Warn the user if this isn't the case
451  */
452 static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
453 {
454         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
455         u32 rmtadv;
456
457         /* The link partner capabilities are only relevant if the
458          * link supports flow control autonegotiation */
459         if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
460                 return;
461
462         /* If flow control autoneg is supported and enabled, then fine */
463         if (efx->wanted_fc & EFX_FC_AUTO)
464                 return;
465
466         rmtadv = 0;
467         if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
468                 rmtadv |= ADVERTISED_Pause;
469         if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
470                 rmtadv |=  ADVERTISED_Asym_Pause;
471
472         if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
473                 netif_err(efx, link, efx->net_dev,
474                           "warning: link partner doesn't support pause frames");
475 }
476
477 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
478 {
479         struct efx_link_state old_state = efx->link_state;
480         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
481         int rc;
482
483         WARN_ON(!mutex_is_locked(&efx->mac_lock));
484
485         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
486
487         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
488                           outbuf, sizeof(outbuf), NULL);
489         if (rc) {
490                 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
491                           __func__, rc);
492                 efx->link_state.up = false;
493         } else {
494                 efx_mcdi_phy_decode_link(
495                         efx, &efx->link_state,
496                         MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
497                         MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
498                         MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
499         }
500
501         return !efx_link_state_equal(&efx->link_state, &old_state);
502 }
503
504 static void efx_mcdi_phy_remove(struct efx_nic *efx)
505 {
506         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
507
508         efx->phy_data = NULL;
509         kfree(phy_data);
510 }
511
512 static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
513 {
514         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
515         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
516         int rc;
517
518         ecmd->supported =
519                 mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
520         ecmd->advertising = efx->link_advertising;
521         ethtool_cmd_speed_set(ecmd, efx->link_state.speed);
522         ecmd->duplex = efx->link_state.fd;
523         ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
524         ecmd->phy_address = phy_cfg->port;
525         ecmd->transceiver = XCVR_INTERNAL;
526         ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
527         ecmd->mdio_support = (efx->mdio.mode_support &
528                               (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
529
530         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
531         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
532                           outbuf, sizeof(outbuf), NULL);
533         if (rc) {
534                 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
535                           __func__, rc);
536                 return;
537         }
538         ecmd->lp_advertising =
539                 mcdi_to_ethtool_cap(phy_cfg->media,
540                                     MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
541 }
542
543 static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
544 {
545         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
546         u32 caps;
547         int rc;
548
549         if (ecmd->autoneg) {
550                 caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
551                          1 << MC_CMD_PHY_CAP_AN_LBN);
552         } else if (ecmd->duplex) {
553                 switch (ethtool_cmd_speed(ecmd)) {
554                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;    break;
555                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;   break;
556                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;  break;
557                 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
558                 default:    return -EINVAL;
559                 }
560         } else {
561                 switch (ethtool_cmd_speed(ecmd)) {
562                 case 10:    caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;    break;
563                 case 100:   caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;   break;
564                 case 1000:  caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;  break;
565                 default:    return -EINVAL;
566                 }
567         }
568
569         rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
570                                efx->loopback_mode, 0);
571         if (rc)
572                 return rc;
573
574         if (ecmd->autoneg) {
575                 efx_link_set_advertising(
576                         efx, ecmd->advertising | ADVERTISED_Autoneg);
577                 phy_cfg->forced_cap = 0;
578         } else {
579                 efx_link_set_advertising(efx, 0);
580                 phy_cfg->forced_cap = caps;
581         }
582         return 0;
583 }
584
585 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
586 {
587         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
588         size_t outlen;
589         int rc;
590
591         BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
592
593         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
594                           outbuf, sizeof(outbuf), &outlen);
595         if (rc)
596                 return rc;
597
598         if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
599                 return -EIO;
600         if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
601                 return -EINVAL;
602
603         return 0;
604 }
605
606 static const char *const mcdi_sft9001_cable_diag_names[] = {
607         "cable.pairA.length",
608         "cable.pairB.length",
609         "cable.pairC.length",
610         "cable.pairD.length",
611         "cable.pairA.status",
612         "cable.pairB.status",
613         "cable.pairC.status",
614         "cable.pairD.status",
615 };
616
617 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
618                          int *results)
619 {
620         unsigned int retry, i, count = 0;
621         size_t outlen;
622         u32 status;
623         MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
624         MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
625         u8 *ptr;
626         int rc;
627
628         BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
629         MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
630         rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
631                           inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
632         if (rc)
633                 goto out;
634
635         /* Wait up to 10s for BIST to finish */
636         for (retry = 0; retry < 100; ++retry) {
637                 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
638                 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
639                                   outbuf, sizeof(outbuf), &outlen);
640                 if (rc)
641                         goto out;
642
643                 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
644                 if (status != MC_CMD_POLL_BIST_RUNNING)
645                         goto finished;
646
647                 msleep(100);
648         }
649
650         rc = -ETIMEDOUT;
651         goto out;
652
653 finished:
654         results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
655
656         /* SFT9001 specific cable diagnostics output */
657         if (efx->phy_type == PHY_TYPE_SFT9001B &&
658             (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
659              bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
660                 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
661                 if (status == MC_CMD_POLL_BIST_PASSED &&
662                     outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
663                         for (i = 0; i < 8; i++) {
664                                 results[count + i] =
665                                         EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
666                                                         EFX_DWORD_0);
667                         }
668                 }
669                 count += 8;
670         }
671         rc = count;
672
673 out:
674         return rc;
675 }
676
677 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
678                                   unsigned flags)
679 {
680         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
681         u32 mode;
682         int rc;
683
684         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
685                 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
686                 if (rc < 0)
687                         return rc;
688
689                 results += rc;
690         }
691
692         /* If we support both LONG and SHORT, then run each in response to
693          * break or not. Otherwise, run the one we support */
694         mode = 0;
695         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
696                 if ((flags & ETH_TEST_FL_OFFLINE) &&
697                     (phy_cfg->flags &
698                      (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
699                         mode = MC_CMD_PHY_BIST_CABLE_LONG;
700                 else
701                         mode = MC_CMD_PHY_BIST_CABLE_SHORT;
702         } else if (phy_cfg->flags &
703                    (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
704                 mode = MC_CMD_PHY_BIST_CABLE_LONG;
705
706         if (mode != 0) {
707                 rc = efx_mcdi_bist(efx, mode, results);
708                 if (rc < 0)
709                         return rc;
710                 results += rc;
711         }
712
713         return 0;
714 }
715
716 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
717                                           unsigned int index)
718 {
719         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
720
721         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
722                 if (index == 0)
723                         return "bist";
724                 --index;
725         }
726
727         if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
728                               (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
729                 if (index == 0)
730                         return "cable";
731                 --index;
732
733                 if (efx->phy_type == PHY_TYPE_SFT9001B) {
734                         if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
735                                 return mcdi_sft9001_cable_diag_names[index];
736                         index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
737                 }
738         }
739
740         return NULL;
741 }
742
743 #define SFP_PAGE_SIZE   128
744 #define SFP_NUM_PAGES   2
745 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
746                                           struct ethtool_eeprom *ee, u8 *data)
747 {
748         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
749         MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
750         size_t outlen;
751         int rc;
752         unsigned int payload_len;
753         unsigned int space_remaining = ee->len;
754         unsigned int page;
755         unsigned int page_off;
756         unsigned int to_copy;
757         u8 *user_data = data;
758
759         BUILD_BUG_ON(SFP_PAGE_SIZE * SFP_NUM_PAGES != ETH_MODULE_SFF_8079_LEN);
760
761         page_off = ee->offset % SFP_PAGE_SIZE;
762         page = ee->offset / SFP_PAGE_SIZE;
763
764         while (space_remaining && (page < SFP_NUM_PAGES)) {
765                 MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
766
767                 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_MEDIA_INFO,
768                                   inbuf, sizeof(inbuf),
769                                   outbuf, sizeof(outbuf),
770                                   &outlen);
771                 if (rc)
772                         return rc;
773
774                 if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
775                               SFP_PAGE_SIZE))
776                         return -EIO;
777
778                 payload_len = MCDI_DWORD(outbuf,
779                                          GET_PHY_MEDIA_INFO_OUT_DATALEN);
780                 if (payload_len != SFP_PAGE_SIZE)
781                         return -EIO;
782
783                 /* Copy as much as we can into data */
784                 payload_len -= page_off;
785                 to_copy = (space_remaining < payload_len) ?
786                         space_remaining : payload_len;
787
788                 memcpy(user_data,
789                        MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + page_off,
790                        to_copy);
791
792                 space_remaining -= to_copy;
793                 user_data += to_copy;
794                 page_off = 0;
795                 page++;
796         }
797
798         return 0;
799 }
800
801 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
802                                         struct ethtool_modinfo *modinfo)
803 {
804         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
805
806         switch (phy_cfg->media) {
807         case MC_CMD_MEDIA_SFP_PLUS:
808                 modinfo->type = ETH_MODULE_SFF_8079;
809                 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
810                 return 0;
811         default:
812                 return -EOPNOTSUPP;
813         }
814 }
815
816 static const struct efx_phy_operations efx_mcdi_phy_ops = {
817         .probe          = efx_mcdi_phy_probe,
818         .init           = efx_port_dummy_op_int,
819         .reconfigure    = efx_mcdi_port_reconfigure,
820         .poll           = efx_mcdi_phy_poll,
821         .fini           = efx_port_dummy_op_void,
822         .remove         = efx_mcdi_phy_remove,
823         .get_settings   = efx_mcdi_phy_get_settings,
824         .set_settings   = efx_mcdi_phy_set_settings,
825         .test_alive     = efx_mcdi_phy_test_alive,
826         .run_tests      = efx_mcdi_phy_run_tests,
827         .test_name      = efx_mcdi_phy_test_name,
828         .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
829         .get_module_info = efx_mcdi_phy_get_module_info,
830 };
831
832 static unsigned int efx_mcdi_event_link_speed[] = {
833         [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
834         [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
835         [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
836 };
837
838 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
839 {
840         u32 flags, fcntl, speed, lpa;
841
842         speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
843         EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
844         speed = efx_mcdi_event_link_speed[speed];
845
846         flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
847         fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
848         lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
849
850         /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
851          * which is only run after flushing the event queues. Therefore, it
852          * is safe to modify the link state outside of the mac_lock here.
853          */
854         efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
855
856         efx_mcdi_phy_check_fcntl(efx, lpa);
857
858         efx_link_status_changed(efx);
859 }
860
861 int efx_mcdi_set_mac(struct efx_nic *efx)
862 {
863         u32 reject, fcntl;
864         MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
865
866         BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
867
868         memcpy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
869                efx->net_dev->dev_addr, ETH_ALEN);
870
871         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
872                         EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
873         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
874
875         /* The MCDI command provides for controlling accept/reject
876          * of broadcast packets too, but the driver doesn't currently
877          * expose this. */
878         reject = (efx->promiscuous) ? 0 :
879                 (1 << MC_CMD_SET_MAC_IN_REJECT_UNCST_LBN);
880         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_REJECT, reject);
881
882         switch (efx->wanted_fc) {
883         case EFX_FC_RX | EFX_FC_TX:
884                 fcntl = MC_CMD_FCNTL_BIDIR;
885                 break;
886         case EFX_FC_RX:
887                 fcntl = MC_CMD_FCNTL_RESPOND;
888                 break;
889         default:
890                 fcntl = MC_CMD_FCNTL_OFF;
891                 break;
892         }
893         if (efx->wanted_fc & EFX_FC_AUTO)
894                 fcntl = MC_CMD_FCNTL_AUTO;
895         if (efx->fc_disable)
896                 fcntl = MC_CMD_FCNTL_OFF;
897
898         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
899
900         return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
901                             NULL, 0, NULL);
902 }
903
904 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
905 {
906         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
907         size_t outlength;
908         int rc;
909
910         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
911
912         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
913                           outbuf, sizeof(outbuf), &outlength);
914         if (rc) {
915                 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
916                           __func__, rc);
917                 return true;
918         }
919
920         return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
921 }
922
923 static int efx_mcdi_mac_stats(struct efx_nic *efx, dma_addr_t dma_addr,
924                               u32 dma_len, int enable, int clear)
925 {
926         MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
927         int rc;
928         efx_dword_t *cmd_ptr;
929         int period = enable ? 1000 : 0;
930
931         BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
932
933         MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
934         cmd_ptr = (efx_dword_t *)MCDI_PTR(inbuf, MAC_STATS_IN_CMD);
935         EFX_POPULATE_DWORD_7(*cmd_ptr,
936                              MC_CMD_MAC_STATS_IN_DMA, !!enable,
937                              MC_CMD_MAC_STATS_IN_CLEAR, clear,
938                              MC_CMD_MAC_STATS_IN_PERIODIC_CHANGE, 1,
939                              MC_CMD_MAC_STATS_IN_PERIODIC_ENABLE, !!enable,
940                              MC_CMD_MAC_STATS_IN_PERIODIC_CLEAR, 0,
941                              MC_CMD_MAC_STATS_IN_PERIODIC_NOEVENT, 1,
942                              MC_CMD_MAC_STATS_IN_PERIOD_MS, period);
943         MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
944
945         rc = efx_mcdi_rpc(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
946                           NULL, 0, NULL);
947         if (rc)
948                 goto fail;
949
950         return 0;
951
952 fail:
953         netif_err(efx, hw, efx->net_dev, "%s: %s failed rc=%d\n",
954                   __func__, enable ? "enable" : "disable", rc);
955         return rc;
956 }
957
958 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
959 {
960         __le64 *dma_stats = efx->stats_buffer.addr;
961
962         dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
963
964         efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr,
965                            MC_CMD_MAC_NSTATS * sizeof(u64), 1, 0);
966 }
967
968 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
969 {
970         efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr, 0, 0, 0);
971 }
972
973 int efx_mcdi_port_probe(struct efx_nic *efx)
974 {
975         int rc;
976
977         /* Hook in PHY operations table */
978         efx->phy_op = &efx_mcdi_phy_ops;
979
980         /* Set up MDIO structure for PHY */
981         efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
982         efx->mdio.mdio_read = efx_mcdi_mdio_read;
983         efx->mdio.mdio_write = efx_mcdi_mdio_write;
984
985         /* Fill out MDIO structure, loopback modes, and initial link state */
986         rc = efx->phy_op->probe(efx);
987         if (rc != 0)
988                 return rc;
989
990         /* Allocate buffer for stats */
991         rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
992                                   MC_CMD_MAC_NSTATS * sizeof(u64));
993         if (rc)
994                 return rc;
995         netif_dbg(efx, probe, efx->net_dev,
996                   "stats buffer at %llx (virt %p phys %llx)\n",
997                   (u64)efx->stats_buffer.dma_addr,
998                   efx->stats_buffer.addr,
999                   (u64)virt_to_phys(efx->stats_buffer.addr));
1000
1001         efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr, 0, 0, 1);
1002
1003         return 0;
1004 }
1005
1006 void efx_mcdi_port_remove(struct efx_nic *efx)
1007 {
1008         efx->phy_op->remove(efx);
1009         efx_nic_free_buffer(efx, &efx->stats_buffer);
1010 }