1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2018 Solarflare Communications Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11 #include "mcdi_port_common.h"
12 #include "efx_common.h"
13 #include "nic.h"
14
efx_mcdi_get_phy_cfg(struct efx_nic * efx,struct efx_mcdi_phy_data * cfg)15 int efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg)
16 {
17 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_CFG_OUT_LEN);
18 size_t outlen;
19 int rc;
20
21 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
22 BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
23
24 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
25 outbuf, sizeof(outbuf), &outlen);
26 if (rc)
27 goto fail;
28
29 if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
30 rc = -EIO;
31 goto fail;
32 }
33
34 cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
35 cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
36 cfg->supported_cap =
37 MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
38 cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
39 cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
40 cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
41 memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
42 sizeof(cfg->name));
43 cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
44 cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
45 memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
46 sizeof(cfg->revision));
47
48 return 0;
49
50 fail:
51 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
52 return rc;
53 }
54
efx_link_set_advertising(struct efx_nic * efx,const unsigned long * advertising)55 void efx_link_set_advertising(struct efx_nic *efx,
56 const unsigned long *advertising)
57 {
58 memcpy(efx->link_advertising, advertising,
59 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
60
61 efx->link_advertising[0] |= ADVERTISED_Autoneg;
62 if (advertising[0] & ADVERTISED_Pause)
63 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
64 else
65 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
66 if (advertising[0] & ADVERTISED_Asym_Pause)
67 efx->wanted_fc ^= EFX_FC_TX;
68 }
69
efx_mcdi_set_link(struct efx_nic * efx,u32 capabilities,u32 flags,u32 loopback_mode,u32 loopback_speed)70 int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
71 u32 flags, u32 loopback_mode, u32 loopback_speed)
72 {
73 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_LINK_IN_LEN);
74 int rc;
75
76 BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
77
78 MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
79 MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
80 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
81 MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
82
83 rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
84 NULL, 0, NULL);
85 return rc;
86 }
87
efx_mcdi_loopback_modes(struct efx_nic * efx,u64 * loopback_modes)88 int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
89 {
90 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
91 size_t outlen;
92 int rc;
93
94 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
95 outbuf, sizeof(outbuf), &outlen);
96 if (rc)
97 goto fail;
98
99 if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST +
100 MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) {
101 rc = -EIO;
102 goto fail;
103 }
104
105 *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
106
107 return 0;
108
109 fail:
110 netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
111 return rc;
112 }
113
mcdi_to_ethtool_linkset(u32 media,u32 cap,unsigned long * linkset)114 void mcdi_to_ethtool_linkset(u32 media, u32 cap, unsigned long *linkset)
115 {
116 #define SET_BIT(name) __set_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
117 linkset)
118
119 bitmap_zero(linkset, __ETHTOOL_LINK_MODE_MASK_NBITS);
120 switch (media) {
121 case MC_CMD_MEDIA_KX4:
122 SET_BIT(Backplane);
123 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
124 SET_BIT(1000baseKX_Full);
125 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
126 SET_BIT(10000baseKX4_Full);
127 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
128 SET_BIT(40000baseKR4_Full);
129 break;
130
131 case MC_CMD_MEDIA_XFP:
132 case MC_CMD_MEDIA_SFP_PLUS:
133 case MC_CMD_MEDIA_QSFP_PLUS:
134 SET_BIT(FIBRE);
135 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN)) {
136 SET_BIT(1000baseT_Full);
137 SET_BIT(1000baseX_Full);
138 }
139 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN)) {
140 SET_BIT(10000baseCR_Full);
141 SET_BIT(10000baseLR_Full);
142 SET_BIT(10000baseSR_Full);
143 }
144 if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN)) {
145 SET_BIT(40000baseCR4_Full);
146 SET_BIT(40000baseSR4_Full);
147 }
148 if (cap & (1 << MC_CMD_PHY_CAP_100000FDX_LBN)) {
149 SET_BIT(100000baseCR4_Full);
150 SET_BIT(100000baseSR4_Full);
151 }
152 if (cap & (1 << MC_CMD_PHY_CAP_25000FDX_LBN)) {
153 SET_BIT(25000baseCR_Full);
154 SET_BIT(25000baseSR_Full);
155 }
156 if (cap & (1 << MC_CMD_PHY_CAP_50000FDX_LBN))
157 SET_BIT(50000baseCR2_Full);
158 break;
159
160 case MC_CMD_MEDIA_BASE_T:
161 SET_BIT(TP);
162 if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
163 SET_BIT(10baseT_Half);
164 if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
165 SET_BIT(10baseT_Full);
166 if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
167 SET_BIT(100baseT_Half);
168 if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
169 SET_BIT(100baseT_Full);
170 if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
171 SET_BIT(1000baseT_Half);
172 if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
173 SET_BIT(1000baseT_Full);
174 if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
175 SET_BIT(10000baseT_Full);
176 break;
177 }
178
179 if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
180 SET_BIT(Pause);
181 if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
182 SET_BIT(Asym_Pause);
183 if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
184 SET_BIT(Autoneg);
185
186 #undef SET_BIT
187 }
188
ethtool_linkset_to_mcdi_cap(const unsigned long * linkset)189 u32 ethtool_linkset_to_mcdi_cap(const unsigned long *linkset)
190 {
191 u32 result = 0;
192
193 #define TEST_BIT(name) test_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
194 linkset)
195
196 if (TEST_BIT(10baseT_Half))
197 result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
198 if (TEST_BIT(10baseT_Full))
199 result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
200 if (TEST_BIT(100baseT_Half))
201 result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
202 if (TEST_BIT(100baseT_Full))
203 result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
204 if (TEST_BIT(1000baseT_Half))
205 result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
206 if (TEST_BIT(1000baseT_Full) || TEST_BIT(1000baseKX_Full) ||
207 TEST_BIT(1000baseX_Full))
208 result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
209 if (TEST_BIT(10000baseT_Full) || TEST_BIT(10000baseKX4_Full) ||
210 TEST_BIT(10000baseCR_Full) || TEST_BIT(10000baseLR_Full) ||
211 TEST_BIT(10000baseSR_Full))
212 result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
213 if (TEST_BIT(40000baseCR4_Full) || TEST_BIT(40000baseKR4_Full) ||
214 TEST_BIT(40000baseSR4_Full))
215 result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
216 if (TEST_BIT(100000baseCR4_Full) || TEST_BIT(100000baseSR4_Full))
217 result |= (1 << MC_CMD_PHY_CAP_100000FDX_LBN);
218 if (TEST_BIT(25000baseCR_Full) || TEST_BIT(25000baseSR_Full))
219 result |= (1 << MC_CMD_PHY_CAP_25000FDX_LBN);
220 if (TEST_BIT(50000baseCR2_Full))
221 result |= (1 << MC_CMD_PHY_CAP_50000FDX_LBN);
222 if (TEST_BIT(Pause))
223 result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
224 if (TEST_BIT(Asym_Pause))
225 result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
226 if (TEST_BIT(Autoneg))
227 result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
228
229 #undef TEST_BIT
230
231 return result;
232 }
233
efx_get_mcdi_phy_flags(struct efx_nic * efx)234 u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
235 {
236 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
237 enum efx_phy_mode mode, supported;
238 u32 flags;
239
240 /* TODO: Advertise the capabilities supported by this PHY */
241 supported = 0;
242 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
243 supported |= PHY_MODE_TX_DISABLED;
244 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
245 supported |= PHY_MODE_LOW_POWER;
246 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
247 supported |= PHY_MODE_OFF;
248
249 mode = efx->phy_mode & supported;
250
251 flags = 0;
252 if (mode & PHY_MODE_TX_DISABLED)
253 flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
254 if (mode & PHY_MODE_LOW_POWER)
255 flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
256 if (mode & PHY_MODE_OFF)
257 flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
258
259 return flags;
260 }
261
mcdi_to_ethtool_media(u32 media)262 u8 mcdi_to_ethtool_media(u32 media)
263 {
264 switch (media) {
265 case MC_CMD_MEDIA_XAUI:
266 case MC_CMD_MEDIA_CX4:
267 case MC_CMD_MEDIA_KX4:
268 return PORT_OTHER;
269
270 case MC_CMD_MEDIA_XFP:
271 case MC_CMD_MEDIA_SFP_PLUS:
272 case MC_CMD_MEDIA_QSFP_PLUS:
273 return PORT_FIBRE;
274
275 case MC_CMD_MEDIA_BASE_T:
276 return PORT_TP;
277
278 default:
279 return PORT_OTHER;
280 }
281 }
282
efx_mcdi_phy_decode_link(struct efx_nic * efx,struct efx_link_state * link_state,u32 speed,u32 flags,u32 fcntl)283 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
284 struct efx_link_state *link_state,
285 u32 speed, u32 flags, u32 fcntl)
286 {
287 switch (fcntl) {
288 case MC_CMD_FCNTL_AUTO:
289 WARN_ON(1); /* This is not a link mode */
290 link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
291 break;
292 case MC_CMD_FCNTL_BIDIR:
293 link_state->fc = EFX_FC_TX | EFX_FC_RX;
294 break;
295 case MC_CMD_FCNTL_RESPOND:
296 link_state->fc = EFX_FC_RX;
297 break;
298 default:
299 WARN_ON(1);
300 fallthrough;
301 case MC_CMD_FCNTL_OFF:
302 link_state->fc = 0;
303 break;
304 }
305
306 link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
307 link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
308 link_state->speed = speed;
309 }
310
311 /* The semantics of the ethtool FEC mode bitmask are not well defined,
312 * particularly the meaning of combinations of bits. Which means we get to
313 * define our own semantics, as follows:
314 * OFF overrides any other bits, and means "disable all FEC" (with the
315 * exception of 25G KR4/CR4, where it is not possible to reject it if AN
316 * partner requests it).
317 * AUTO on its own means use cable requirements and link partner autoneg with
318 * fw-default preferences for the cable type.
319 * AUTO and either RS or BASER means use the specified FEC type if cable and
320 * link partner support it, otherwise autoneg/fw-default.
321 * RS or BASER alone means use the specified FEC type if cable and link partner
322 * support it and either requests it, otherwise no FEC.
323 * Both RS and BASER (whether AUTO or not) means use FEC if cable and link
324 * partner support it, preferring RS to BASER.
325 */
ethtool_fec_caps_to_mcdi(u32 supported_cap,u32 ethtool_cap)326 u32 ethtool_fec_caps_to_mcdi(u32 supported_cap, u32 ethtool_cap)
327 {
328 u32 ret = 0;
329
330 if (ethtool_cap & ETHTOOL_FEC_OFF)
331 return 0;
332
333 if (ethtool_cap & ETHTOOL_FEC_AUTO)
334 ret |= ((1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
335 (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
336 (1 << MC_CMD_PHY_CAP_RS_FEC_LBN)) & supported_cap;
337 if (ethtool_cap & ETHTOOL_FEC_RS &&
338 supported_cap & (1 << MC_CMD_PHY_CAP_RS_FEC_LBN))
339 ret |= (1 << MC_CMD_PHY_CAP_RS_FEC_LBN) |
340 (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN);
341 if (ethtool_cap & ETHTOOL_FEC_BASER) {
342 if (supported_cap & (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN))
343 ret |= (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
344 (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN);
345 if (supported_cap & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN))
346 ret |= (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
347 (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN);
348 }
349 return ret;
350 }
351
352 /* Invert ethtool_fec_caps_to_mcdi. There are two combinations that function
353 * can never produce, (baser xor rs) and neither req; the implementation below
354 * maps both of those to AUTO. This should never matter, and it's not clear
355 * what a better mapping would be anyway.
356 */
mcdi_fec_caps_to_ethtool(u32 caps,bool is_25g)357 u32 mcdi_fec_caps_to_ethtool(u32 caps, bool is_25g)
358 {
359 bool rs = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_LBN),
360 rs_req = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN),
361 baser = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN)
362 : caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN),
363 baser_req = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN)
364 : caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN);
365
366 if (!baser && !rs)
367 return ETHTOOL_FEC_OFF;
368 return (rs_req ? ETHTOOL_FEC_RS : 0) |
369 (baser_req ? ETHTOOL_FEC_BASER : 0) |
370 (baser == baser_req && rs == rs_req ? 0 : ETHTOOL_FEC_AUTO);
371 }
372
373 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
374 * supported by the link partner. Warn the user if this isn't the case
375 */
efx_mcdi_phy_check_fcntl(struct efx_nic * efx,u32 lpa)376 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
377 {
378 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
379 u32 rmtadv;
380
381 /* The link partner capabilities are only relevant if the
382 * link supports flow control autonegotiation
383 */
384 if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
385 return;
386
387 /* If flow control autoneg is supported and enabled, then fine */
388 if (efx->wanted_fc & EFX_FC_AUTO)
389 return;
390
391 rmtadv = 0;
392 if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
393 rmtadv |= ADVERTISED_Pause;
394 if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
395 rmtadv |= ADVERTISED_Asym_Pause;
396
397 if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
398 netif_err(efx, link, efx->net_dev,
399 "warning: link partner doesn't support pause frames");
400 }
401
efx_mcdi_phy_poll(struct efx_nic * efx)402 bool efx_mcdi_phy_poll(struct efx_nic *efx)
403 {
404 struct efx_link_state old_state = efx->link_state;
405 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
406 int rc;
407
408 WARN_ON(!mutex_is_locked(&efx->mac_lock));
409
410 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
411
412 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
413 outbuf, sizeof(outbuf), NULL);
414 if (rc)
415 efx->link_state.up = false;
416 else
417 efx_mcdi_phy_decode_link(
418 efx, &efx->link_state,
419 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
420 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
421 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
422
423 return !efx_link_state_equal(&efx->link_state, &old_state);
424 }
425
efx_mcdi_phy_probe(struct efx_nic * efx)426 int efx_mcdi_phy_probe(struct efx_nic *efx)
427 {
428 struct efx_mcdi_phy_data *phy_data;
429 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
430 u32 caps;
431 int rc;
432
433 /* Initialise and populate phy_data */
434 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
435 if (phy_data == NULL)
436 return -ENOMEM;
437
438 rc = efx_mcdi_get_phy_cfg(efx, phy_data);
439 if (rc != 0)
440 goto fail;
441
442 /* Read initial link advertisement */
443 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
444 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
445 outbuf, sizeof(outbuf), NULL);
446 if (rc)
447 goto fail;
448
449 /* Fill out nic state */
450 efx->phy_data = phy_data;
451 efx->phy_type = phy_data->type;
452
453 efx->mdio_bus = phy_data->channel;
454 efx->mdio.prtad = phy_data->port;
455 efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
456 efx->mdio.mode_support = 0;
457 if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
458 efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
459 if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
460 efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
461
462 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
463 if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
464 mcdi_to_ethtool_linkset(phy_data->media, caps,
465 efx->link_advertising);
466 else
467 phy_data->forced_cap = caps;
468
469 /* Assert that we can map efx -> mcdi loopback modes */
470 BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
471 BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
472 BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
473 BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
474 BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
475 BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
476 BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
477 BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
478 BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
479 BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
480 BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
481 BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
482 BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
483 BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
484 BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
485 BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
486 BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
487 BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
488 BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
489 BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
490 BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
491 BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
492 BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
493 BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
494 BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
495 BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
496 BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
497
498 rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
499 if (rc != 0)
500 goto fail;
501 /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
502 * but by convention we don't
503 */
504 efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
505
506 /* Set the initial link mode */
507 efx_mcdi_phy_decode_link(efx, &efx->link_state,
508 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
509 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
510 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
511
512 /* Record the initial FEC configuration (or nearest approximation
513 * representable in the ethtool configuration space)
514 */
515 efx->fec_config = mcdi_fec_caps_to_ethtool(caps,
516 efx->link_state.speed == 25000 ||
517 efx->link_state.speed == 50000);
518
519 /* Default to Autonegotiated flow control if the PHY supports it */
520 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
521 if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
522 efx->wanted_fc |= EFX_FC_AUTO;
523 efx_link_set_wanted_fc(efx, efx->wanted_fc);
524
525 return 0;
526
527 fail:
528 kfree(phy_data);
529 return rc;
530 }
531
efx_mcdi_phy_remove(struct efx_nic * efx)532 void efx_mcdi_phy_remove(struct efx_nic *efx)
533 {
534 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
535
536 efx->phy_data = NULL;
537 kfree(phy_data);
538 }
539
efx_mcdi_phy_get_link_ksettings(struct efx_nic * efx,struct ethtool_link_ksettings * cmd)540 void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx, struct ethtool_link_ksettings *cmd)
541 {
542 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
543 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
544 int rc;
545
546 cmd->base.speed = efx->link_state.speed;
547 cmd->base.duplex = efx->link_state.fd;
548 cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
549 cmd->base.phy_address = phy_cfg->port;
550 cmd->base.autoneg = !!(efx->link_advertising[0] & ADVERTISED_Autoneg);
551 cmd->base.mdio_support = (efx->mdio.mode_support &
552 (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
553
554 mcdi_to_ethtool_linkset(phy_cfg->media, phy_cfg->supported_cap,
555 cmd->link_modes.supported);
556 memcpy(cmd->link_modes.advertising, efx->link_advertising,
557 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
558
559 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
560 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
561 outbuf, sizeof(outbuf), NULL);
562 if (rc)
563 return;
564 mcdi_to_ethtool_linkset(phy_cfg->media,
565 MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP),
566 cmd->link_modes.lp_advertising);
567 }
568
efx_mcdi_phy_set_link_ksettings(struct efx_nic * efx,const struct ethtool_link_ksettings * cmd)569 int efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx, const struct ethtool_link_ksettings *cmd)
570 {
571 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
572 u32 caps;
573 int rc;
574
575 if (cmd->base.autoneg) {
576 caps = (ethtool_linkset_to_mcdi_cap(cmd->link_modes.advertising) |
577 1 << MC_CMD_PHY_CAP_AN_LBN);
578 } else if (cmd->base.duplex) {
579 switch (cmd->base.speed) {
580 case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
581 case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
582 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
583 case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
584 case 40000: caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN; break;
585 case 100000: caps = 1 << MC_CMD_PHY_CAP_100000FDX_LBN; break;
586 case 25000: caps = 1 << MC_CMD_PHY_CAP_25000FDX_LBN; break;
587 case 50000: caps = 1 << MC_CMD_PHY_CAP_50000FDX_LBN; break;
588 default: return -EINVAL;
589 }
590 } else {
591 switch (cmd->base.speed) {
592 case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
593 case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
594 case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
595 default: return -EINVAL;
596 }
597 }
598
599 caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, efx->fec_config);
600
601 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
602 efx->loopback_mode, 0);
603 if (rc)
604 return rc;
605
606 if (cmd->base.autoneg) {
607 efx_link_set_advertising(efx, cmd->link_modes.advertising);
608 phy_cfg->forced_cap = 0;
609 } else {
610 efx_link_clear_advertising(efx);
611 phy_cfg->forced_cap = caps;
612 }
613 return 0;
614 }
615
efx_mcdi_phy_get_fecparam(struct efx_nic * efx,struct ethtool_fecparam * fec)616 int efx_mcdi_phy_get_fecparam(struct efx_nic *efx, struct ethtool_fecparam *fec)
617 {
618 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_V2_LEN);
619 u32 caps, active, speed; /* MCDI format */
620 bool is_25g = false;
621 size_t outlen;
622 int rc;
623
624 BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
625 rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
626 outbuf, sizeof(outbuf), &outlen);
627 if (rc)
628 return rc;
629 if (outlen < MC_CMD_GET_LINK_OUT_V2_LEN)
630 return -EOPNOTSUPP;
631
632 /* behaviour for 25G/50G links depends on 25G BASER bit */
633 speed = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_LINK_SPEED);
634 is_25g = speed == 25000 || speed == 50000;
635
636 caps = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_CAP);
637 fec->fec = mcdi_fec_caps_to_ethtool(caps, is_25g);
638 /* BASER is never supported on 100G */
639 if (speed == 100000)
640 fec->fec &= ~ETHTOOL_FEC_BASER;
641
642 active = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_FEC_TYPE);
643 switch (active) {
644 case MC_CMD_FEC_NONE:
645 fec->active_fec = ETHTOOL_FEC_OFF;
646 break;
647 case MC_CMD_FEC_BASER:
648 fec->active_fec = ETHTOOL_FEC_BASER;
649 break;
650 case MC_CMD_FEC_RS:
651 fec->active_fec = ETHTOOL_FEC_RS;
652 break;
653 default:
654 netif_warn(efx, hw, efx->net_dev,
655 "Firmware reports unrecognised FEC_TYPE %u\n",
656 active);
657 /* We don't know what firmware has picked. AUTO is as good a
658 * "can't happen" value as any other.
659 */
660 fec->active_fec = ETHTOOL_FEC_AUTO;
661 break;
662 }
663
664 return 0;
665 }
666
667 /* Basic validation to ensure that the caps we are going to attempt to set are
668 * in fact supported by the adapter. Note that 'no FEC' is always supported.
669 */
ethtool_fec_supported(u32 supported_cap,u32 ethtool_cap)670 static int ethtool_fec_supported(u32 supported_cap, u32 ethtool_cap)
671 {
672 if (ethtool_cap & ETHTOOL_FEC_OFF)
673 return 0;
674
675 if (ethtool_cap &&
676 !ethtool_fec_caps_to_mcdi(supported_cap, ethtool_cap))
677 return -EINVAL;
678 return 0;
679 }
680
efx_mcdi_phy_set_fecparam(struct efx_nic * efx,const struct ethtool_fecparam * fec)681 int efx_mcdi_phy_set_fecparam(struct efx_nic *efx, const struct ethtool_fecparam *fec)
682 {
683 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
684 u32 caps;
685 int rc;
686
687 rc = ethtool_fec_supported(phy_cfg->supported_cap, fec->fec);
688 if (rc)
689 return rc;
690
691 /* Work out what efx_mcdi_phy_set_link_ksettings() would produce from
692 * saved advertising bits
693 */
694 if (test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, efx->link_advertising))
695 caps = (ethtool_linkset_to_mcdi_cap(efx->link_advertising) |
696 1 << MC_CMD_PHY_CAP_AN_LBN);
697 else
698 caps = phy_cfg->forced_cap;
699
700 caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, fec->fec);
701 rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
702 efx->loopback_mode, 0);
703 if (rc)
704 return rc;
705
706 /* Record the new FEC setting for subsequent set_link calls */
707 efx->fec_config = fec->fec;
708 return 0;
709 }
710
efx_mcdi_phy_test_alive(struct efx_nic * efx)711 int efx_mcdi_phy_test_alive(struct efx_nic *efx)
712 {
713 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
714 size_t outlen;
715 int rc;
716
717 BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
718
719 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
720 outbuf, sizeof(outbuf), &outlen);
721 if (rc)
722 return rc;
723
724 if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
725 return -EIO;
726 if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
727 return -EINVAL;
728
729 return 0;
730 }
731
efx_mcdi_port_reconfigure(struct efx_nic * efx)732 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
733 {
734 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
735 u32 caps = (efx->link_advertising[0] ?
736 ethtool_linkset_to_mcdi_cap(efx->link_advertising) :
737 phy_cfg->forced_cap);
738
739 caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, efx->fec_config);
740
741 return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
742 efx->loopback_mode, 0);
743 }
744
745 static const char *const mcdi_sft9001_cable_diag_names[] = {
746 "cable.pairA.length",
747 "cable.pairB.length",
748 "cable.pairC.length",
749 "cable.pairD.length",
750 "cable.pairA.status",
751 "cable.pairB.status",
752 "cable.pairC.status",
753 "cable.pairD.status",
754 };
755
efx_mcdi_bist(struct efx_nic * efx,unsigned int bist_mode,int * results)756 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
757 int *results)
758 {
759 unsigned int retry, i, count = 0;
760 size_t outlen;
761 u32 status;
762 MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
763 MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
764 u8 *ptr;
765 int rc;
766
767 BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
768 MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
769 rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
770 inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
771 if (rc)
772 goto out;
773
774 /* Wait up to 10s for BIST to finish */
775 for (retry = 0; retry < 100; ++retry) {
776 BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
777 rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
778 outbuf, sizeof(outbuf), &outlen);
779 if (rc)
780 goto out;
781
782 status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
783 if (status != MC_CMD_POLL_BIST_RUNNING)
784 goto finished;
785
786 msleep(100);
787 }
788
789 rc = -ETIMEDOUT;
790 goto out;
791
792 finished:
793 results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
794
795 /* SFT9001 specific cable diagnostics output */
796 if (efx->phy_type == PHY_TYPE_SFT9001B &&
797 (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
798 bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
799 ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
800 if (status == MC_CMD_POLL_BIST_PASSED &&
801 outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
802 for (i = 0; i < 8; i++) {
803 results[count + i] =
804 EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
805 EFX_DWORD_0);
806 }
807 }
808 count += 8;
809 }
810 rc = count;
811
812 out:
813 return rc;
814 }
815
efx_mcdi_phy_run_tests(struct efx_nic * efx,int * results,unsigned int flags)816 int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results, unsigned int flags)
817 {
818 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
819 u32 mode;
820 int rc;
821
822 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
823 rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
824 if (rc < 0)
825 return rc;
826
827 results += rc;
828 }
829
830 /* If we support both LONG and SHORT, then run each in response to
831 * break or not. Otherwise, run the one we support
832 */
833 mode = 0;
834 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
835 if ((flags & ETH_TEST_FL_OFFLINE) &&
836 (phy_cfg->flags &
837 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
838 mode = MC_CMD_PHY_BIST_CABLE_LONG;
839 else
840 mode = MC_CMD_PHY_BIST_CABLE_SHORT;
841 } else if (phy_cfg->flags &
842 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
843 mode = MC_CMD_PHY_BIST_CABLE_LONG;
844
845 if (mode != 0) {
846 rc = efx_mcdi_bist(efx, mode, results);
847 if (rc < 0)
848 return rc;
849 results += rc;
850 }
851
852 return 0;
853 }
854
efx_mcdi_phy_test_name(struct efx_nic * efx,unsigned int index)855 const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
856 {
857 struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
858
859 if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
860 if (index == 0)
861 return "bist";
862 --index;
863 }
864
865 if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
866 (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
867 if (index == 0)
868 return "cable";
869 --index;
870
871 if (efx->phy_type == PHY_TYPE_SFT9001B) {
872 if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
873 return mcdi_sft9001_cable_diag_names[index];
874 index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
875 }
876 }
877
878 return NULL;
879 }
880
881 #define SFP_PAGE_SIZE 128
882 #define SFF_DIAG_TYPE_OFFSET 92
883 #define SFF_DIAG_ADDR_CHANGE BIT(2)
884 #define SFF_8079_NUM_PAGES 2
885 #define SFF_8472_NUM_PAGES 4
886 #define SFF_8436_NUM_PAGES 5
887 #define SFF_DMT_LEVEL_OFFSET 94
888
889 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
890 * @efx: NIC context
891 * @page: EEPROM page number
892 * @data: Destination data pointer
893 * @offset: Offset in page to copy from in to data
894 * @space: Space available in data
895 *
896 * Return:
897 * >=0 - amount of data copied
898 * <0 - error
899 */
efx_mcdi_phy_get_module_eeprom_page(struct efx_nic * efx,unsigned int page,u8 * data,ssize_t offset,ssize_t space)900 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
901 unsigned int page,
902 u8 *data, ssize_t offset,
903 ssize_t space)
904 {
905 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
906 MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
907 unsigned int payload_len;
908 unsigned int to_copy;
909 size_t outlen;
910 int rc;
911
912 if (offset > SFP_PAGE_SIZE)
913 return -EINVAL;
914
915 to_copy = min(space, SFP_PAGE_SIZE - offset);
916
917 MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
918 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
919 inbuf, sizeof(inbuf),
920 outbuf, sizeof(outbuf),
921 &outlen);
922
923 if (rc)
924 return rc;
925
926 if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
927 SFP_PAGE_SIZE))
928 return -EIO;
929
930 payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
931 if (payload_len != SFP_PAGE_SIZE)
932 return -EIO;
933
934 memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
935 to_copy);
936
937 return to_copy;
938 }
939
efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic * efx,unsigned int page,u8 byte)940 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
941 unsigned int page,
942 u8 byte)
943 {
944 u8 data;
945 int rc;
946
947 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
948 if (rc == 1)
949 return data;
950
951 return rc;
952 }
953
efx_mcdi_phy_diag_type(struct efx_nic * efx)954 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
955 {
956 /* Page zero of the EEPROM includes the diagnostic type at byte 92. */
957 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
958 SFF_DIAG_TYPE_OFFSET);
959 }
960
efx_mcdi_phy_sff_8472_level(struct efx_nic * efx)961 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
962 {
963 /* Page zero of the EEPROM includes the DMT level at byte 94. */
964 return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
965 SFF_DMT_LEVEL_OFFSET);
966 }
967
efx_mcdi_phy_module_type(struct efx_nic * efx)968 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
969 {
970 struct efx_mcdi_phy_data *phy_data = efx->phy_data;
971
972 if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
973 return phy_data->media;
974
975 /* A QSFP+ NIC may actually have an SFP+ module attached.
976 * The ID is page 0, byte 0.
977 * QSFP28 is of type SFF_8636, however, this is treated
978 * the same by ethtool, so we can also treat them the same.
979 */
980 switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
981 case 0x3: /* SFP */
982 return MC_CMD_MEDIA_SFP_PLUS;
983 case 0xc: /* QSFP */
984 case 0xd: /* QSFP+ */
985 case 0x11: /* QSFP28 */
986 return MC_CMD_MEDIA_QSFP_PLUS;
987 default:
988 return 0;
989 }
990 }
991
efx_mcdi_phy_get_module_eeprom(struct efx_nic * efx,struct ethtool_eeprom * ee,u8 * data)992 int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx, struct ethtool_eeprom *ee, u8 *data)
993 {
994 int rc;
995 ssize_t space_remaining = ee->len;
996 unsigned int page_off;
997 bool ignore_missing;
998 int num_pages;
999 int page;
1000
1001 switch (efx_mcdi_phy_module_type(efx)) {
1002 case MC_CMD_MEDIA_SFP_PLUS:
1003 num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
1004 SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
1005 page = 0;
1006 ignore_missing = false;
1007 break;
1008 case MC_CMD_MEDIA_QSFP_PLUS:
1009 num_pages = SFF_8436_NUM_PAGES;
1010 page = -1; /* We obtain the lower page by asking for -1. */
1011 ignore_missing = true; /* Ignore missing pages after page 0. */
1012 break;
1013 default:
1014 return -EOPNOTSUPP;
1015 }
1016
1017 page_off = ee->offset % SFP_PAGE_SIZE;
1018 page += ee->offset / SFP_PAGE_SIZE;
1019
1020 while (space_remaining && (page < num_pages)) {
1021 rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
1022 data, page_off,
1023 space_remaining);
1024
1025 if (rc > 0) {
1026 space_remaining -= rc;
1027 data += rc;
1028 page_off = 0;
1029 page++;
1030 } else if (rc == 0) {
1031 space_remaining = 0;
1032 } else if (ignore_missing && (page > 0)) {
1033 int intended_size = SFP_PAGE_SIZE - page_off;
1034
1035 space_remaining -= intended_size;
1036 if (space_remaining < 0) {
1037 space_remaining = 0;
1038 } else {
1039 memset(data, 0, intended_size);
1040 data += intended_size;
1041 page_off = 0;
1042 page++;
1043 rc = 0;
1044 }
1045 } else {
1046 return rc;
1047 }
1048 }
1049
1050 return 0;
1051 }
1052
efx_mcdi_phy_get_module_info(struct efx_nic * efx,struct ethtool_modinfo * modinfo)1053 int efx_mcdi_phy_get_module_info(struct efx_nic *efx, struct ethtool_modinfo *modinfo)
1054 {
1055 int sff_8472_level;
1056 int diag_type;
1057
1058 switch (efx_mcdi_phy_module_type(efx)) {
1059 case MC_CMD_MEDIA_SFP_PLUS:
1060 sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
1061
1062 /* If we can't read the diagnostics level we have none. */
1063 if (sff_8472_level < 0)
1064 return -EOPNOTSUPP;
1065
1066 /* Check if this module requires the (unsupported) address
1067 * change operation.
1068 */
1069 diag_type = efx_mcdi_phy_diag_type(efx);
1070
1071 if (sff_8472_level == 0 ||
1072 (diag_type & SFF_DIAG_ADDR_CHANGE)) {
1073 modinfo->type = ETH_MODULE_SFF_8079;
1074 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1075 } else {
1076 modinfo->type = ETH_MODULE_SFF_8472;
1077 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1078 }
1079 break;
1080
1081 case MC_CMD_MEDIA_QSFP_PLUS:
1082 modinfo->type = ETH_MODULE_SFF_8436;
1083 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1084 break;
1085
1086 default:
1087 return -EOPNOTSUPP;
1088 }
1089
1090 return 0;
1091 }
1092
efx_calc_mac_mtu(struct efx_nic * efx)1093 static unsigned int efx_calc_mac_mtu(struct efx_nic *efx)
1094 {
1095 return EFX_MAX_FRAME_LEN(efx->net_dev->mtu);
1096 }
1097
efx_mcdi_set_mac(struct efx_nic * efx)1098 int efx_mcdi_set_mac(struct efx_nic *efx)
1099 {
1100 u32 fcntl;
1101 MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
1102
1103 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1104
1105 /* This has no effect on EF10 */
1106 ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
1107 efx->net_dev->dev_addr);
1108
1109 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU, efx_calc_mac_mtu(efx));
1110 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
1111
1112 /* Set simple MAC filter for Siena */
1113 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
1114 SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
1115
1116 MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_FLAGS,
1117 SET_MAC_IN_FLAG_INCLUDE_FCS,
1118 !!(efx->net_dev->features & NETIF_F_RXFCS));
1119
1120 switch (efx->wanted_fc) {
1121 case EFX_FC_RX | EFX_FC_TX:
1122 fcntl = MC_CMD_FCNTL_BIDIR;
1123 break;
1124 case EFX_FC_RX:
1125 fcntl = MC_CMD_FCNTL_RESPOND;
1126 break;
1127 default:
1128 fcntl = MC_CMD_FCNTL_OFF;
1129 break;
1130 }
1131 if (efx->wanted_fc & EFX_FC_AUTO)
1132 fcntl = MC_CMD_FCNTL_AUTO;
1133 if (efx->fc_disable)
1134 fcntl = MC_CMD_FCNTL_OFF;
1135
1136 MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
1137
1138 return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
1139 NULL, 0, NULL);
1140 }
1141
efx_mcdi_set_mtu(struct efx_nic * efx)1142 int efx_mcdi_set_mtu(struct efx_nic *efx)
1143 {
1144 MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_MAC_EXT_IN_LEN);
1145
1146 BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1147
1148 MCDI_SET_DWORD(inbuf, SET_MAC_EXT_IN_MTU, efx_calc_mac_mtu(efx));
1149
1150 MCDI_POPULATE_DWORD_1(inbuf, SET_MAC_EXT_IN_CONTROL,
1151 SET_MAC_EXT_IN_CFG_MTU, 1);
1152
1153 return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, inbuf, sizeof(inbuf),
1154 NULL, 0, NULL);
1155 }
1156
1157 enum efx_stats_action {
1158 EFX_STATS_ENABLE,
1159 EFX_STATS_DISABLE,
1160 EFX_STATS_PULL,
1161 };
1162
efx_mcdi_mac_stats(struct efx_nic * efx,enum efx_stats_action action,int clear)1163 static int efx_mcdi_mac_stats(struct efx_nic *efx,
1164 enum efx_stats_action action, int clear)
1165 {
1166 MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
1167 int rc;
1168 int change = action == EFX_STATS_PULL ? 0 : 1;
1169 int enable = action == EFX_STATS_ENABLE ? 1 : 0;
1170 int period = action == EFX_STATS_ENABLE ? 1000 : 0;
1171 dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
1172 u32 dma_len = action != EFX_STATS_DISABLE ?
1173 efx->num_mac_stats * sizeof(u64) : 0;
1174
1175 BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
1176
1177 MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
1178 MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
1179 MAC_STATS_IN_DMA, !!enable,
1180 MAC_STATS_IN_CLEAR, clear,
1181 MAC_STATS_IN_PERIODIC_CHANGE, change,
1182 MAC_STATS_IN_PERIODIC_ENABLE, enable,
1183 MAC_STATS_IN_PERIODIC_CLEAR, 0,
1184 MAC_STATS_IN_PERIODIC_NOEVENT, 1,
1185 MAC_STATS_IN_PERIOD_MS, period);
1186 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
1187
1188 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
1189 MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, efx->vport_id);
1190
1191 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
1192 NULL, 0, NULL);
1193 /* Expect ENOENT if DMA queues have not been set up */
1194 if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
1195 efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
1196 NULL, 0, rc);
1197 return rc;
1198 }
1199
efx_mcdi_mac_start_stats(struct efx_nic * efx)1200 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
1201 {
1202 __le64 *dma_stats = efx->stats_buffer.addr;
1203
1204 dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1205
1206 efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
1207 }
1208
efx_mcdi_mac_stop_stats(struct efx_nic * efx)1209 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
1210 {
1211 efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
1212 }
1213
1214 #define EFX_MAC_STATS_WAIT_US 100
1215 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
1216
efx_mcdi_mac_pull_stats(struct efx_nic * efx)1217 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
1218 {
1219 __le64 *dma_stats = efx->stats_buffer.addr;
1220 int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
1221
1222 dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
1223 efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
1224
1225 while (dma_stats[efx->num_mac_stats - 1] ==
1226 EFX_MC_STATS_GENERATION_INVALID &&
1227 attempts-- != 0)
1228 udelay(EFX_MAC_STATS_WAIT_US);
1229 }
1230
efx_mcdi_mac_init_stats(struct efx_nic * efx)1231 int efx_mcdi_mac_init_stats(struct efx_nic *efx)
1232 {
1233 int rc;
1234
1235 if (!efx->num_mac_stats)
1236 return 0;
1237
1238 /* Allocate buffer for stats */
1239 rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
1240 efx->num_mac_stats * sizeof(u64), GFP_KERNEL);
1241 if (rc) {
1242 netif_warn(efx, probe, efx->net_dev,
1243 "failed to allocate DMA buffer: %d\n", rc);
1244 return rc;
1245 }
1246
1247 netif_dbg(efx, probe, efx->net_dev,
1248 "stats buffer at %llx (virt %p phys %llx)\n",
1249 (u64) efx->stats_buffer.dma_addr,
1250 efx->stats_buffer.addr,
1251 (u64) virt_to_phys(efx->stats_buffer.addr));
1252
1253 return 0;
1254 }
1255
efx_mcdi_mac_fini_stats(struct efx_nic * efx)1256 void efx_mcdi_mac_fini_stats(struct efx_nic *efx)
1257 {
1258 efx_nic_free_buffer(efx, &efx->stats_buffer);
1259 }
1260
1261 /* Get physical port number (EF10 only; on Siena it is same as PF number) */
efx_mcdi_port_get_number(struct efx_nic * efx)1262 int efx_mcdi_port_get_number(struct efx_nic *efx)
1263 {
1264 MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
1265 int rc;
1266
1267 rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
1268 outbuf, sizeof(outbuf), NULL);
1269 if (rc)
1270 return rc;
1271
1272 return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);
1273 }
1274
1275 static unsigned int efx_mcdi_event_link_speed[] = {
1276 [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
1277 [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
1278 [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
1279 [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
1280 [MCDI_EVENT_LINKCHANGE_SPEED_25G] = 25000,
1281 [MCDI_EVENT_LINKCHANGE_SPEED_50G] = 50000,
1282 [MCDI_EVENT_LINKCHANGE_SPEED_100G] = 100000,
1283 };
1284
efx_mcdi_process_link_change(struct efx_nic * efx,efx_qword_t * ev)1285 void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
1286 {
1287 u32 flags, fcntl, speed, lpa;
1288
1289 speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
1290 EFX_WARN_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
1291 speed = efx_mcdi_event_link_speed[speed];
1292
1293 flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
1294 fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
1295 lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
1296
1297 /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
1298 * which is only run after flushing the event queues. Therefore, it
1299 * is safe to modify the link state outside of the mac_lock here.
1300 */
1301 efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
1302
1303 efx_mcdi_phy_check_fcntl(efx, lpa);
1304
1305 efx_link_status_changed(efx);
1306 }
1307