sfc: add bits for 25/50/100G supported/advertised speeds
[linux-2.6-microblaze.git] / drivers / net / ethernet / sfc / mcdi_port.c
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2009-2013 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 "mcdi.h"
17 #include "mcdi_pcol.h"
18 #include "nic.h"
19 #include "selftest.h"
20
21 struct efx_mcdi_phy_data {
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_data *cfg)
37 {
38         MCDI_DECLARE_BUF(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 = -EIO;
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         netif_err(efx, hw, efx->net_dev, "%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         MCDI_DECLARE_BUF(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         return rc;
93 }
94
95 static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
96 {
97         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
98         size_t outlen;
99         int rc;
100
101         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
102                           outbuf, sizeof(outbuf), &outlen);
103         if (rc)
104                 goto fail;
105
106         if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST +
107                       MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) {
108                 rc = -EIO;
109                 goto fail;
110         }
111
112         *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
113
114         return 0;
115
116 fail:
117         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
118         return rc;
119 }
120
121 static int efx_mcdi_mdio_read(struct net_device *net_dev,
122                               int prtad, int devad, u16 addr)
123 {
124         struct efx_nic *efx = netdev_priv(net_dev);
125         MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
126         MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
127         size_t outlen;
128         int rc;
129
130         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
131         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
132         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
133         MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
134
135         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
136                           outbuf, sizeof(outbuf), &outlen);
137         if (rc)
138                 return rc;
139
140         if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
141             MC_CMD_MDIO_STATUS_GOOD)
142                 return -EIO;
143
144         return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
145 }
146
147 static int efx_mcdi_mdio_write(struct net_device *net_dev,
148                                int prtad, int devad, u16 addr, u16 value)
149 {
150         struct efx_nic *efx = netdev_priv(net_dev);
151         MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
152         MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
153         size_t outlen;
154         int rc;
155
156         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
157         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
158         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
159         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
160         MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
161
162         rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
163                           outbuf, sizeof(outbuf), &outlen);
164         if (rc)
165                 return rc;
166
167         if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
168             MC_CMD_MDIO_STATUS_GOOD)
169                 return -EIO;
170
171         return 0;
172 }
173
174 static void mcdi_to_ethtool_linkset(u32 media, u32 cap, unsigned long *linkset)
175 {
176         #define SET_BIT(name)   __set_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
177                                           linkset)
178
179         bitmap_zero(linkset, __ETHTOOL_LINK_MODE_MASK_NBITS);
180         switch (media) {
181         case MC_CMD_MEDIA_KX4:
182                 SET_BIT(Backplane);
183                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
184                         SET_BIT(1000baseKX_Full);
185                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
186                         SET_BIT(10000baseKX4_Full);
187                 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
188                         SET_BIT(40000baseKR4_Full);
189                 break;
190
191         case MC_CMD_MEDIA_XFP:
192         case MC_CMD_MEDIA_SFP_PLUS:
193         case MC_CMD_MEDIA_QSFP_PLUS:
194                 SET_BIT(FIBRE);
195                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
196                         SET_BIT(1000baseT_Full);
197                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
198                         SET_BIT(10000baseT_Full);
199                 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
200                         SET_BIT(40000baseCR4_Full);
201                 if (cap & (1 << MC_CMD_PHY_CAP_100000FDX_LBN))
202                         SET_BIT(100000baseCR4_Full);
203                 if (cap & (1 << MC_CMD_PHY_CAP_25000FDX_LBN))
204                         SET_BIT(25000baseCR_Full);
205                 if (cap & (1 << MC_CMD_PHY_CAP_50000FDX_LBN))
206                         SET_BIT(50000baseCR2_Full);
207                 break;
208
209         case MC_CMD_MEDIA_BASE_T:
210                 SET_BIT(TP);
211                 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
212                         SET_BIT(10baseT_Half);
213                 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
214                         SET_BIT(10baseT_Full);
215                 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
216                         SET_BIT(100baseT_Half);
217                 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
218                         SET_BIT(100baseT_Full);
219                 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
220                         SET_BIT(1000baseT_Half);
221                 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
222                         SET_BIT(1000baseT_Full);
223                 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
224                         SET_BIT(10000baseT_Full);
225                 break;
226         }
227
228         if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
229                 SET_BIT(Pause);
230         if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
231                 SET_BIT(Asym_Pause);
232         if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
233                 SET_BIT(Autoneg);
234
235         #undef SET_BIT
236 }
237
238 static u32 ethtool_linkset_to_mcdi_cap(const unsigned long *linkset)
239 {
240         u32 result = 0;
241
242         #define TEST_BIT(name)  test_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
243                                          linkset)
244
245         if (TEST_BIT(10baseT_Half))
246                 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
247         if (TEST_BIT(10baseT_Full))
248                 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
249         if (TEST_BIT(100baseT_Half))
250                 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
251         if (TEST_BIT(100baseT_Full))
252                 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
253         if (TEST_BIT(1000baseT_Half))
254                 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
255         if (TEST_BIT(1000baseT_Full) || TEST_BIT(1000baseKX_Full))
256                 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
257         if (TEST_BIT(10000baseT_Full) || TEST_BIT(10000baseKX4_Full))
258                 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
259         if (TEST_BIT(40000baseCR4_Full) || TEST_BIT(40000baseKR4_Full))
260                 result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
261         if (TEST_BIT(100000baseCR4_Full))
262                 result |= (1 << MC_CMD_PHY_CAP_100000FDX_LBN);
263         if (TEST_BIT(25000baseCR_Full))
264                 result |= (1 << MC_CMD_PHY_CAP_25000FDX_LBN);
265         if (TEST_BIT(50000baseCR2_Full))
266                 result |= (1 << MC_CMD_PHY_CAP_50000FDX_LBN);
267         if (TEST_BIT(Pause))
268                 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
269         if (TEST_BIT(Asym_Pause))
270                 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
271         if (TEST_BIT(Autoneg))
272                 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
273
274         #undef TEST_BIT
275
276         return result;
277 }
278
279 static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
280 {
281         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
282         enum efx_phy_mode mode, supported;
283         u32 flags;
284
285         /* TODO: Advertise the capabilities supported by this PHY */
286         supported = 0;
287         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
288                 supported |= PHY_MODE_TX_DISABLED;
289         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
290                 supported |= PHY_MODE_LOW_POWER;
291         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
292                 supported |= PHY_MODE_OFF;
293
294         mode = efx->phy_mode & supported;
295
296         flags = 0;
297         if (mode & PHY_MODE_TX_DISABLED)
298                 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
299         if (mode & PHY_MODE_LOW_POWER)
300                 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
301         if (mode & PHY_MODE_OFF)
302                 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
303
304         return flags;
305 }
306
307 static u8 mcdi_to_ethtool_media(u32 media)
308 {
309         switch (media) {
310         case MC_CMD_MEDIA_XAUI:
311         case MC_CMD_MEDIA_CX4:
312         case MC_CMD_MEDIA_KX4:
313                 return PORT_OTHER;
314
315         case MC_CMD_MEDIA_XFP:
316         case MC_CMD_MEDIA_SFP_PLUS:
317         case MC_CMD_MEDIA_QSFP_PLUS:
318                 return PORT_FIBRE;
319
320         case MC_CMD_MEDIA_BASE_T:
321                 return PORT_TP;
322
323         default:
324                 return PORT_OTHER;
325         }
326 }
327
328 static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
329                               struct efx_link_state *link_state,
330                               u32 speed, u32 flags, u32 fcntl)
331 {
332         switch (fcntl) {
333         case MC_CMD_FCNTL_AUTO:
334                 WARN_ON(1);     /* This is not a link mode */
335                 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
336                 break;
337         case MC_CMD_FCNTL_BIDIR:
338                 link_state->fc = EFX_FC_TX | EFX_FC_RX;
339                 break;
340         case MC_CMD_FCNTL_RESPOND:
341                 link_state->fc = EFX_FC_RX;
342                 break;
343         default:
344                 WARN_ON(1);
345         case MC_CMD_FCNTL_OFF:
346                 link_state->fc = 0;
347                 break;
348         }
349
350         link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
351         link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
352         link_state->speed = speed;
353 }
354
355 static int efx_mcdi_phy_probe(struct efx_nic *efx)
356 {
357         struct efx_mcdi_phy_data *phy_data;
358         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
359         u32 caps;
360         int rc;
361
362         /* Initialise and populate phy_data */
363         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
364         if (phy_data == NULL)
365                 return -ENOMEM;
366
367         rc = efx_mcdi_get_phy_cfg(efx, phy_data);
368         if (rc != 0)
369                 goto fail;
370
371         /* Read initial link advertisement */
372         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
373         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
374                           outbuf, sizeof(outbuf), NULL);
375         if (rc)
376                 goto fail;
377
378         /* Fill out nic state */
379         efx->phy_data = phy_data;
380         efx->phy_type = phy_data->type;
381
382         efx->mdio_bus = phy_data->channel;
383         efx->mdio.prtad = phy_data->port;
384         efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
385         efx->mdio.mode_support = 0;
386         if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
387                 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
388         if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
389                 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
390
391         caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
392         if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
393                 mcdi_to_ethtool_linkset(phy_data->media, caps,
394                                         efx->link_advertising);
395         else
396                 phy_data->forced_cap = caps;
397
398         /* Assert that we can map efx -> mcdi loopback modes */
399         BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
400         BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
401         BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
402         BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
403         BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
404         BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
405         BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
406         BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
407         BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
408         BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
409         BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
410         BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
411         BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
412         BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
413         BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
414         BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
415         BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
416         BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
417         BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
418         BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
419         BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
420         BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
421         BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
422         BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
423         BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
424         BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
425         BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
426
427         rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
428         if (rc != 0)
429                 goto fail;
430         /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
431          * but by convention we don't */
432         efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
433
434         /* Set the initial link mode */
435         efx_mcdi_phy_decode_link(
436                 efx, &efx->link_state,
437                 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
438                 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
439                 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
440
441         /* Default to Autonegotiated flow control if the PHY supports it */
442         efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
443         if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
444                 efx->wanted_fc |= EFX_FC_AUTO;
445         efx_link_set_wanted_fc(efx, efx->wanted_fc);
446
447         return 0;
448
449 fail:
450         kfree(phy_data);
451         return rc;
452 }
453
454 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
455 {
456         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
457         u32 caps = (efx->link_advertising[0] ?
458                     ethtool_linkset_to_mcdi_cap(efx->link_advertising) :
459                     phy_cfg->forced_cap);
460
461         return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
462                                  efx->loopback_mode, 0);
463 }
464
465 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
466  * supported by the link partner. Warn the user if this isn't the case
467  */
468 static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
469 {
470         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
471         u32 rmtadv;
472
473         /* The link partner capabilities are only relevant if the
474          * link supports flow control autonegotiation */
475         if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
476                 return;
477
478         /* If flow control autoneg is supported and enabled, then fine */
479         if (efx->wanted_fc & EFX_FC_AUTO)
480                 return;
481
482         rmtadv = 0;
483         if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
484                 rmtadv |= ADVERTISED_Pause;
485         if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
486                 rmtadv |=  ADVERTISED_Asym_Pause;
487
488         if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
489                 netif_err(efx, link, efx->net_dev,
490                           "warning: link partner doesn't support pause frames");
491 }
492
493 static bool efx_mcdi_phy_poll(struct efx_nic *efx)
494 {
495         struct efx_link_state old_state = efx->link_state;
496         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
497         int rc;
498
499         WARN_ON(!mutex_is_locked(&efx->mac_lock));
500
501         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
502
503         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
504                           outbuf, sizeof(outbuf), NULL);
505         if (rc)
506                 efx->link_state.up = false;
507         else
508                 efx_mcdi_phy_decode_link(
509                         efx, &efx->link_state,
510                         MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
511                         MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
512                         MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
513
514         return !efx_link_state_equal(&efx->link_state, &old_state);
515 }
516
517 static void efx_mcdi_phy_remove(struct efx_nic *efx)
518 {
519         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
520
521         efx->phy_data = NULL;
522         kfree(phy_data);
523 }
524
525 static void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx,
526                                             struct ethtool_link_ksettings *cmd)
527 {
528         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
529         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
530         int rc;
531
532         cmd->base.speed = efx->link_state.speed;
533         cmd->base.duplex = efx->link_state.fd;
534         cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
535         cmd->base.phy_address = phy_cfg->port;
536         cmd->base.autoneg = !!(efx->link_advertising[0] & ADVERTISED_Autoneg);
537         cmd->base.mdio_support = (efx->mdio.mode_support &
538                               (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
539
540         mcdi_to_ethtool_linkset(phy_cfg->media, phy_cfg->supported_cap,
541                                 cmd->link_modes.supported);
542         memcpy(cmd->link_modes.advertising, efx->link_advertising,
543                sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
544
545         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
546         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
547                           outbuf, sizeof(outbuf), NULL);
548         if (rc)
549                 return;
550         mcdi_to_ethtool_linkset(phy_cfg->media,
551                                 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP),
552                                 cmd->link_modes.lp_advertising);
553 }
554
555 static int
556 efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx,
557                                 const struct ethtool_link_ksettings *cmd)
558 {
559         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
560         u32 caps;
561         int rc;
562
563         if (cmd->base.autoneg) {
564                 caps = (ethtool_linkset_to_mcdi_cap(cmd->link_modes.advertising) |
565                         1 << MC_CMD_PHY_CAP_AN_LBN);
566         } else if (cmd->base.duplex) {
567                 switch (cmd->base.speed) {
568                 case 10:     caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;     break;
569                 case 100:    caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;    break;
570                 case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;   break;
571                 case 10000:  caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN;  break;
572                 case 40000:  caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN;  break;
573                 case 100000: caps = 1 << MC_CMD_PHY_CAP_100000FDX_LBN; break;
574                 case 25000:  caps = 1 << MC_CMD_PHY_CAP_25000FDX_LBN;  break;
575                 case 50000:  caps = 1 << MC_CMD_PHY_CAP_50000FDX_LBN;  break;
576                 default:     return -EINVAL;
577                 }
578         } else {
579                 switch (cmd->base.speed) {
580                 case 10:     caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;     break;
581                 case 100:    caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;    break;
582                 case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;   break;
583                 default:     return -EINVAL;
584                 }
585         }
586
587         rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
588                                efx->loopback_mode, 0);
589         if (rc)
590                 return rc;
591
592         if (cmd->base.autoneg) {
593                 efx_link_set_advertising(efx, cmd->link_modes.advertising);
594                 phy_cfg->forced_cap = 0;
595         } else {
596                 efx_link_clear_advertising(efx);
597                 phy_cfg->forced_cap = caps;
598         }
599         return 0;
600 }
601
602 static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
603 {
604         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
605         size_t outlen;
606         int rc;
607
608         BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
609
610         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
611                           outbuf, sizeof(outbuf), &outlen);
612         if (rc)
613                 return rc;
614
615         if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
616                 return -EIO;
617         if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
618                 return -EINVAL;
619
620         return 0;
621 }
622
623 static const char *const mcdi_sft9001_cable_diag_names[] = {
624         "cable.pairA.length",
625         "cable.pairB.length",
626         "cable.pairC.length",
627         "cable.pairD.length",
628         "cable.pairA.status",
629         "cable.pairB.status",
630         "cable.pairC.status",
631         "cable.pairD.status",
632 };
633
634 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
635                          int *results)
636 {
637         unsigned int retry, i, count = 0;
638         size_t outlen;
639         u32 status;
640         MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
641         MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
642         u8 *ptr;
643         int rc;
644
645         BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
646         MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
647         rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
648                           inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
649         if (rc)
650                 goto out;
651
652         /* Wait up to 10s for BIST to finish */
653         for (retry = 0; retry < 100; ++retry) {
654                 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
655                 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
656                                   outbuf, sizeof(outbuf), &outlen);
657                 if (rc)
658                         goto out;
659
660                 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
661                 if (status != MC_CMD_POLL_BIST_RUNNING)
662                         goto finished;
663
664                 msleep(100);
665         }
666
667         rc = -ETIMEDOUT;
668         goto out;
669
670 finished:
671         results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
672
673         /* SFT9001 specific cable diagnostics output */
674         if (efx->phy_type == PHY_TYPE_SFT9001B &&
675             (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
676              bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
677                 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
678                 if (status == MC_CMD_POLL_BIST_PASSED &&
679                     outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
680                         for (i = 0; i < 8; i++) {
681                                 results[count + i] =
682                                         EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
683                                                         EFX_DWORD_0);
684                         }
685                 }
686                 count += 8;
687         }
688         rc = count;
689
690 out:
691         return rc;
692 }
693
694 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
695                                   unsigned flags)
696 {
697         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
698         u32 mode;
699         int rc;
700
701         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
702                 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
703                 if (rc < 0)
704                         return rc;
705
706                 results += rc;
707         }
708
709         /* If we support both LONG and SHORT, then run each in response to
710          * break or not. Otherwise, run the one we support */
711         mode = 0;
712         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
713                 if ((flags & ETH_TEST_FL_OFFLINE) &&
714                     (phy_cfg->flags &
715                      (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
716                         mode = MC_CMD_PHY_BIST_CABLE_LONG;
717                 else
718                         mode = MC_CMD_PHY_BIST_CABLE_SHORT;
719         } else if (phy_cfg->flags &
720                    (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
721                 mode = MC_CMD_PHY_BIST_CABLE_LONG;
722
723         if (mode != 0) {
724                 rc = efx_mcdi_bist(efx, mode, results);
725                 if (rc < 0)
726                         return rc;
727                 results += rc;
728         }
729
730         return 0;
731 }
732
733 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
734                                           unsigned int index)
735 {
736         struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
737
738         if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
739                 if (index == 0)
740                         return "bist";
741                 --index;
742         }
743
744         if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
745                               (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
746                 if (index == 0)
747                         return "cable";
748                 --index;
749
750                 if (efx->phy_type == PHY_TYPE_SFT9001B) {
751                         if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
752                                 return mcdi_sft9001_cable_diag_names[index];
753                         index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
754                 }
755         }
756
757         return NULL;
758 }
759
760 #define SFP_PAGE_SIZE           128
761 #define SFF_DIAG_TYPE_OFFSET    92
762 #define SFF_DIAG_ADDR_CHANGE    BIT(2)
763 #define SFF_8079_NUM_PAGES      2
764 #define SFF_8472_NUM_PAGES      4
765 #define SFF_8436_NUM_PAGES      5
766 #define SFF_DMT_LEVEL_OFFSET    94
767
768 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
769  * @efx:        NIC context
770  * @page:       EEPROM page number
771  * @data:       Destination data pointer
772  * @offset:     Offset in page to copy from in to data
773  * @space:      Space available in data
774  *
775  * Return:
776  *   >=0 - amount of data copied
777  *   <0  - error
778  */
779 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
780                                                unsigned int page,
781                                                u8 *data, ssize_t offset,
782                                                ssize_t space)
783 {
784         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
785         MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
786         size_t outlen;
787         unsigned int payload_len;
788         unsigned int to_copy;
789         int rc;
790
791         if (offset > SFP_PAGE_SIZE)
792                 return -EINVAL;
793
794         to_copy = min(space, SFP_PAGE_SIZE - offset);
795
796         MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
797         rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
798                                 inbuf, sizeof(inbuf),
799                                 outbuf, sizeof(outbuf),
800                                 &outlen);
801
802         if (rc)
803                 return rc;
804
805         if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
806                         SFP_PAGE_SIZE))
807                 return -EIO;
808
809         payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
810         if (payload_len != SFP_PAGE_SIZE)
811                 return -EIO;
812
813         memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
814                to_copy);
815
816         return to_copy;
817 }
818
819 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
820                                                unsigned int page,
821                                                u8 byte)
822 {
823         int rc;
824         u8 data;
825
826         rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
827         if (rc == 1)
828                 return data;
829
830         return rc;
831 }
832
833 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
834 {
835         /* Page zero of the EEPROM includes the diagnostic type at byte 92. */
836         return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
837                                                    SFF_DIAG_TYPE_OFFSET);
838 }
839
840 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
841 {
842         /* Page zero of the EEPROM includes the DMT level at byte 94. */
843         return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
844                                                    SFF_DMT_LEVEL_OFFSET);
845 }
846
847 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
848 {
849         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
850
851         if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
852                 return phy_data->media;
853
854         /* A QSFP+ NIC may actually have an SFP+ module attached.
855          * The ID is page 0, byte 0.
856          */
857         switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
858         case 0x3:
859                 return MC_CMD_MEDIA_SFP_PLUS;
860         case 0xc:
861         case 0xd:
862                 return MC_CMD_MEDIA_QSFP_PLUS;
863         default:
864                 return 0;
865         }
866 }
867
868 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
869                                           struct ethtool_eeprom *ee, u8 *data)
870 {
871         int rc;
872         ssize_t space_remaining = ee->len;
873         unsigned int page_off;
874         bool ignore_missing;
875         int num_pages;
876         int page;
877
878         switch (efx_mcdi_phy_module_type(efx)) {
879         case MC_CMD_MEDIA_SFP_PLUS:
880                 num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
881                                 SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
882                 page = 0;
883                 ignore_missing = false;
884                 break;
885         case MC_CMD_MEDIA_QSFP_PLUS:
886                 num_pages = SFF_8436_NUM_PAGES;
887                 page = -1; /* We obtain the lower page by asking for -1. */
888                 ignore_missing = true; /* Ignore missing pages after page 0. */
889                 break;
890         default:
891                 return -EOPNOTSUPP;
892         }
893
894         page_off = ee->offset % SFP_PAGE_SIZE;
895         page += ee->offset / SFP_PAGE_SIZE;
896
897         while (space_remaining && (page < num_pages)) {
898                 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
899                                                          data, page_off,
900                                                          space_remaining);
901
902                 if (rc > 0) {
903                         space_remaining -= rc;
904                         data += rc;
905                         page_off = 0;
906                         page++;
907                 } else if (rc == 0) {
908                         space_remaining = 0;
909                 } else if (ignore_missing && (page > 0)) {
910                         int intended_size = SFP_PAGE_SIZE - page_off;
911
912                         space_remaining -= intended_size;
913                         if (space_remaining < 0) {
914                                 space_remaining = 0;
915                         } else {
916                                 memset(data, 0, intended_size);
917                                 data += intended_size;
918                                 page_off = 0;
919                                 page++;
920                                 rc = 0;
921                         }
922                 } else {
923                         return rc;
924                 }
925         }
926
927         return 0;
928 }
929
930 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
931                                         struct ethtool_modinfo *modinfo)
932 {
933         int sff_8472_level;
934         int diag_type;
935
936         switch (efx_mcdi_phy_module_type(efx)) {
937         case MC_CMD_MEDIA_SFP_PLUS:
938                 sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
939
940                 /* If we can't read the diagnostics level we have none. */
941                 if (sff_8472_level < 0)
942                         return -EOPNOTSUPP;
943
944                 /* Check if this module requires the (unsupported) address
945                  * change operation.
946                  */
947                 diag_type = efx_mcdi_phy_diag_type(efx);
948
949                 if ((sff_8472_level == 0) ||
950                     (diag_type & SFF_DIAG_ADDR_CHANGE)) {
951                         modinfo->type = ETH_MODULE_SFF_8079;
952                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
953                 } else {
954                         modinfo->type = ETH_MODULE_SFF_8472;
955                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
956                 }
957                 break;
958
959         case MC_CMD_MEDIA_QSFP_PLUS:
960                 modinfo->type = ETH_MODULE_SFF_8436;
961                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
962                 break;
963
964         default:
965                 return -EOPNOTSUPP;
966         }
967
968         return 0;
969 }
970
971 static const struct efx_phy_operations efx_mcdi_phy_ops = {
972         .probe          = efx_mcdi_phy_probe,
973         .init           = efx_port_dummy_op_int,
974         .reconfigure    = efx_mcdi_port_reconfigure,
975         .poll           = efx_mcdi_phy_poll,
976         .fini           = efx_port_dummy_op_void,
977         .remove         = efx_mcdi_phy_remove,
978         .get_link_ksettings = efx_mcdi_phy_get_link_ksettings,
979         .set_link_ksettings = efx_mcdi_phy_set_link_ksettings,
980         .test_alive     = efx_mcdi_phy_test_alive,
981         .run_tests      = efx_mcdi_phy_run_tests,
982         .test_name      = efx_mcdi_phy_test_name,
983         .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
984         .get_module_info = efx_mcdi_phy_get_module_info,
985 };
986
987 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
988 {
989         struct efx_mcdi_phy_data *phy_data = efx->phy_data;
990
991         return phy_data->supported_cap;
992 }
993
994 static unsigned int efx_mcdi_event_link_speed[] = {
995         [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
996         [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
997         [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
998         [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
999         [MCDI_EVENT_LINKCHANGE_SPEED_25G] = 25000,
1000         [MCDI_EVENT_LINKCHANGE_SPEED_50G] = 50000,
1001         [MCDI_EVENT_LINKCHANGE_SPEED_100G] = 100000,
1002 };
1003
1004 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
1005 {
1006         u32 flags, fcntl, speed, lpa;
1007
1008         speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
1009         EFX_WARN_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
1010         speed = efx_mcdi_event_link_speed[speed];
1011
1012         flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
1013         fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
1014         lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
1015
1016         /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
1017          * which is only run after flushing the event queues. Therefore, it
1018          * is safe to modify the link state outside of the mac_lock here.
1019          */
1020         efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
1021
1022         efx_mcdi_phy_check_fcntl(efx, lpa);
1023
1024         efx_link_status_changed(efx);
1025 }
1026
1027 int efx_mcdi_set_mac(struct efx_nic *efx)
1028 {
1029         u32 fcntl;
1030         MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
1031
1032         BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1033
1034         /* This has no effect on EF10 */
1035         ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
1036                         efx->net_dev->dev_addr);
1037
1038         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
1039                         EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
1040         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
1041
1042         /* Set simple MAC filter for Siena */
1043         MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
1044                               SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
1045
1046         MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_FLAGS,
1047                               SET_MAC_IN_FLAG_INCLUDE_FCS,
1048                               !!(efx->net_dev->features & NETIF_F_RXFCS));
1049
1050         switch (efx->wanted_fc) {
1051         case EFX_FC_RX | EFX_FC_TX:
1052                 fcntl = MC_CMD_FCNTL_BIDIR;
1053                 break;
1054         case EFX_FC_RX:
1055                 fcntl = MC_CMD_FCNTL_RESPOND;
1056                 break;
1057         default:
1058                 fcntl = MC_CMD_FCNTL_OFF;
1059                 break;
1060         }
1061         if (efx->wanted_fc & EFX_FC_AUTO)
1062                 fcntl = MC_CMD_FCNTL_AUTO;
1063         if (efx->fc_disable)
1064                 fcntl = MC_CMD_FCNTL_OFF;
1065
1066         MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
1067
1068         return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
1069                             NULL, 0, NULL);
1070 }
1071
1072 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
1073 {
1074         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
1075         size_t outlength;
1076         int rc;
1077
1078         BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
1079
1080         rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
1081                           outbuf, sizeof(outbuf), &outlength);
1082         if (rc)
1083                 return true;
1084
1085         return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
1086 }
1087
1088 enum efx_stats_action {
1089         EFX_STATS_ENABLE,
1090         EFX_STATS_DISABLE,
1091         EFX_STATS_PULL,
1092 };
1093
1094 static int efx_mcdi_mac_stats(struct efx_nic *efx,
1095                               enum efx_stats_action action, int clear)
1096 {
1097         MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
1098         int rc;
1099         int change = action == EFX_STATS_PULL ? 0 : 1;
1100         int enable = action == EFX_STATS_ENABLE ? 1 : 0;
1101         int period = action == EFX_STATS_ENABLE ? 1000 : 0;
1102         dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
1103         u32 dma_len = action != EFX_STATS_DISABLE ?
1104                 efx->num_mac_stats * sizeof(u64) : 0;
1105
1106         BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
1107
1108         MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
1109         MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
1110                               MAC_STATS_IN_DMA, !!enable,
1111                               MAC_STATS_IN_CLEAR, clear,
1112                               MAC_STATS_IN_PERIODIC_CHANGE, change,
1113                               MAC_STATS_IN_PERIODIC_ENABLE, enable,
1114                               MAC_STATS_IN_PERIODIC_CLEAR, 0,
1115                               MAC_STATS_IN_PERIODIC_NOEVENT, 1,
1116                               MAC_STATS_IN_PERIOD_MS, period);
1117         MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
1118
1119         if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1120                 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1121
1122                 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
1123         }
1124
1125         rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
1126                                 NULL, 0, NULL);
1127         /* Expect ENOENT if DMA queues have not been set up */
1128         if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
1129                 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
1130                                        NULL, 0, rc);
1131         return rc;
1132 }
1133
1134 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
1135 {
1136         __le64 *dma_stats = efx->stats_buffer.addr;
1137
1138         dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1139
1140         efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
1141 }
1142
1143 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
1144 {
1145         efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
1146 }
1147
1148 #define EFX_MAC_STATS_WAIT_US 100
1149 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
1150
1151 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
1152 {
1153         __le64 *dma_stats = efx->stats_buffer.addr;
1154         int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
1155
1156         dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1157         efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
1158
1159         while (dma_stats[efx->num_mac_stats - 1] ==
1160                                 EFX_MC_STATS_GENERATION_INVALID &&
1161                         attempts-- != 0)
1162                 udelay(EFX_MAC_STATS_WAIT_US);
1163 }
1164
1165 int efx_mcdi_port_probe(struct efx_nic *efx)
1166 {
1167         int rc;
1168
1169         /* Hook in PHY operations table */
1170         efx->phy_op = &efx_mcdi_phy_ops;
1171
1172         /* Set up MDIO structure for PHY */
1173         efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
1174         efx->mdio.mdio_read = efx_mcdi_mdio_read;
1175         efx->mdio.mdio_write = efx_mcdi_mdio_write;
1176
1177         /* Fill out MDIO structure, loopback modes, and initial link state */
1178         rc = efx->phy_op->probe(efx);
1179         if (rc != 0)
1180                 return rc;
1181
1182         /* Allocate buffer for stats */
1183         rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
1184                                   efx->num_mac_stats * sizeof(u64), GFP_KERNEL);
1185         if (rc)
1186                 return rc;
1187         netif_dbg(efx, probe, efx->net_dev,
1188                   "stats buffer at %llx (virt %p phys %llx)\n",
1189                   (u64)efx->stats_buffer.dma_addr,
1190                   efx->stats_buffer.addr,
1191                   (u64)virt_to_phys(efx->stats_buffer.addr));
1192
1193         efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
1194
1195         return 0;
1196 }
1197
1198 void efx_mcdi_port_remove(struct efx_nic *efx)
1199 {
1200         efx->phy_op->remove(efx);
1201         efx_nic_free_buffer(efx, &efx->stats_buffer);
1202 }
1203
1204 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
1205 int efx_mcdi_port_get_number(struct efx_nic *efx)
1206 {
1207         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
1208         int rc;
1209
1210         rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
1211                           outbuf, sizeof(outbuf), NULL);
1212         if (rc)
1213                 return rc;
1214
1215         return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);
1216 }