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