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 
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 
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 
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 
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 
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 		if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
138 			SET_BIT(10000baseT_Full);
139 		if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
140 			SET_BIT(40000baseCR4_Full);
141 		if (cap & (1 << MC_CMD_PHY_CAP_100000FDX_LBN))
142 			SET_BIT(100000baseCR4_Full);
143 		if (cap & (1 << MC_CMD_PHY_CAP_25000FDX_LBN))
144 			SET_BIT(25000baseCR_Full);
145 		if (cap & (1 << MC_CMD_PHY_CAP_50000FDX_LBN))
146 			SET_BIT(50000baseCR2_Full);
147 		break;
148 
149 	case MC_CMD_MEDIA_BASE_T:
150 		SET_BIT(TP);
151 		if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
152 			SET_BIT(10baseT_Half);
153 		if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
154 			SET_BIT(10baseT_Full);
155 		if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
156 			SET_BIT(100baseT_Half);
157 		if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
158 			SET_BIT(100baseT_Full);
159 		if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
160 			SET_BIT(1000baseT_Half);
161 		if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
162 			SET_BIT(1000baseT_Full);
163 		if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
164 			SET_BIT(10000baseT_Full);
165 		break;
166 	}
167 
168 	if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
169 		SET_BIT(Pause);
170 	if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
171 		SET_BIT(Asym_Pause);
172 	if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
173 		SET_BIT(Autoneg);
174 
175 	#undef SET_BIT
176 }
177 
178 u32 ethtool_linkset_to_mcdi_cap(const unsigned long *linkset)
179 {
180 	u32 result = 0;
181 
182 	#define TEST_BIT(name)	test_bit(ETHTOOL_LINK_MODE_ ## name ## _BIT, \
183 					 linkset)
184 
185 	if (TEST_BIT(10baseT_Half))
186 		result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
187 	if (TEST_BIT(10baseT_Full))
188 		result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
189 	if (TEST_BIT(100baseT_Half))
190 		result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
191 	if (TEST_BIT(100baseT_Full))
192 		result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
193 	if (TEST_BIT(1000baseT_Half))
194 		result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
195 	if (TEST_BIT(1000baseT_Full) || TEST_BIT(1000baseKX_Full))
196 		result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
197 	if (TEST_BIT(10000baseT_Full) || TEST_BIT(10000baseKX4_Full))
198 		result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
199 	if (TEST_BIT(40000baseCR4_Full) || TEST_BIT(40000baseKR4_Full))
200 		result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
201 	if (TEST_BIT(100000baseCR4_Full))
202 		result |= (1 << MC_CMD_PHY_CAP_100000FDX_LBN);
203 	if (TEST_BIT(25000baseCR_Full))
204 		result |= (1 << MC_CMD_PHY_CAP_25000FDX_LBN);
205 	if (TEST_BIT(50000baseCR2_Full))
206 		result |= (1 << MC_CMD_PHY_CAP_50000FDX_LBN);
207 	if (TEST_BIT(Pause))
208 		result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
209 	if (TEST_BIT(Asym_Pause))
210 		result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
211 	if (TEST_BIT(Autoneg))
212 		result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
213 
214 	#undef TEST_BIT
215 
216 	return result;
217 }
218 
219 u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
220 {
221 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
222 	enum efx_phy_mode mode, supported;
223 	u32 flags;
224 
225 	/* TODO: Advertise the capabilities supported by this PHY */
226 	supported = 0;
227 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
228 		supported |= PHY_MODE_TX_DISABLED;
229 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
230 		supported |= PHY_MODE_LOW_POWER;
231 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
232 		supported |= PHY_MODE_OFF;
233 
234 	mode = efx->phy_mode & supported;
235 
236 	flags = 0;
237 	if (mode & PHY_MODE_TX_DISABLED)
238 		flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
239 	if (mode & PHY_MODE_LOW_POWER)
240 		flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
241 	if (mode & PHY_MODE_OFF)
242 		flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
243 
244 	return flags;
245 }
246 
247 u8 mcdi_to_ethtool_media(u32 media)
248 {
249 	switch (media) {
250 	case MC_CMD_MEDIA_XAUI:
251 	case MC_CMD_MEDIA_CX4:
252 	case MC_CMD_MEDIA_KX4:
253 		return PORT_OTHER;
254 
255 	case MC_CMD_MEDIA_XFP:
256 	case MC_CMD_MEDIA_SFP_PLUS:
257 	case MC_CMD_MEDIA_QSFP_PLUS:
258 		return PORT_FIBRE;
259 
260 	case MC_CMD_MEDIA_BASE_T:
261 		return PORT_TP;
262 
263 	default:
264 		return PORT_OTHER;
265 	}
266 }
267 
268 void efx_mcdi_phy_decode_link(struct efx_nic *efx,
269 			      struct efx_link_state *link_state,
270 			      u32 speed, u32 flags, u32 fcntl)
271 {
272 	switch (fcntl) {
273 	case MC_CMD_FCNTL_AUTO:
274 		WARN_ON(1);	/* This is not a link mode */
275 		link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
276 		break;
277 	case MC_CMD_FCNTL_BIDIR:
278 		link_state->fc = EFX_FC_TX | EFX_FC_RX;
279 		break;
280 	case MC_CMD_FCNTL_RESPOND:
281 		link_state->fc = EFX_FC_RX;
282 		break;
283 	default:
284 		WARN_ON(1);
285 		fallthrough;
286 	case MC_CMD_FCNTL_OFF:
287 		link_state->fc = 0;
288 		break;
289 	}
290 
291 	link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
292 	link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
293 	link_state->speed = speed;
294 }
295 
296 /* The semantics of the ethtool FEC mode bitmask are not well defined,
297  * particularly the meaning of combinations of bits.  Which means we get to
298  * define our own semantics, as follows:
299  * OFF overrides any other bits, and means "disable all FEC" (with the
300  * exception of 25G KR4/CR4, where it is not possible to reject it if AN
301  * partner requests it).
302  * AUTO on its own means use cable requirements and link partner autoneg with
303  * fw-default preferences for the cable type.
304  * AUTO and either RS or BASER means use the specified FEC type if cable and
305  * link partner support it, otherwise autoneg/fw-default.
306  * RS or BASER alone means use the specified FEC type if cable and link partner
307  * support it and either requests it, otherwise no FEC.
308  * Both RS and BASER (whether AUTO or not) means use FEC if cable and link
309  * partner support it, preferring RS to BASER.
310  */
311 u32 ethtool_fec_caps_to_mcdi(u32 supported_cap, u32 ethtool_cap)
312 {
313 	u32 ret = 0;
314 
315 	if (ethtool_cap & ETHTOOL_FEC_OFF)
316 		return 0;
317 
318 	if (ethtool_cap & ETHTOOL_FEC_AUTO)
319 		ret |= ((1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
320 			(1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
321 			(1 << MC_CMD_PHY_CAP_RS_FEC_LBN)) & supported_cap;
322 	if (ethtool_cap & ETHTOOL_FEC_RS &&
323 	    supported_cap & (1 << MC_CMD_PHY_CAP_RS_FEC_LBN))
324 		ret |= (1 << MC_CMD_PHY_CAP_RS_FEC_LBN) |
325 		       (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN);
326 	if (ethtool_cap & ETHTOOL_FEC_BASER) {
327 		if (supported_cap & (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN))
328 			ret |= (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN) |
329 			       (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN);
330 		if (supported_cap & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN))
331 			ret |= (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN) |
332 			       (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN);
333 	}
334 	return ret;
335 }
336 
337 /* Invert ethtool_fec_caps_to_mcdi.  There are two combinations that function
338  * can never produce, (baser xor rs) and neither req; the implementation below
339  * maps both of those to AUTO.  This should never matter, and it's not clear
340  * what a better mapping would be anyway.
341  */
342 u32 mcdi_fec_caps_to_ethtool(u32 caps, bool is_25g)
343 {
344 	bool rs = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_LBN),
345 	     rs_req = caps & (1 << MC_CMD_PHY_CAP_RS_FEC_REQUESTED_LBN),
346 	     baser = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_LBN)
347 			    : caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_LBN),
348 	     baser_req = is_25g ? caps & (1 << MC_CMD_PHY_CAP_25G_BASER_FEC_REQUESTED_LBN)
349 				: caps & (1 << MC_CMD_PHY_CAP_BASER_FEC_REQUESTED_LBN);
350 
351 	if (!baser && !rs)
352 		return ETHTOOL_FEC_OFF;
353 	return (rs_req ? ETHTOOL_FEC_RS : 0) |
354 	       (baser_req ? ETHTOOL_FEC_BASER : 0) |
355 	       (baser == baser_req && rs == rs_req ? 0 : ETHTOOL_FEC_AUTO);
356 }
357 
358 /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
359  * supported by the link partner. Warn the user if this isn't the case
360  */
361 void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
362 {
363 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
364 	u32 rmtadv;
365 
366 	/* The link partner capabilities are only relevant if the
367 	 * link supports flow control autonegotiation
368 	 */
369 	if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
370 		return;
371 
372 	/* If flow control autoneg is supported and enabled, then fine */
373 	if (efx->wanted_fc & EFX_FC_AUTO)
374 		return;
375 
376 	rmtadv = 0;
377 	if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
378 		rmtadv |= ADVERTISED_Pause;
379 	if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
380 		rmtadv |=  ADVERTISED_Asym_Pause;
381 
382 	if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
383 		netif_err(efx, link, efx->net_dev,
384 			  "warning: link partner doesn't support pause frames");
385 }
386 
387 bool efx_mcdi_phy_poll(struct efx_nic *efx)
388 {
389 	struct efx_link_state old_state = efx->link_state;
390 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
391 	int rc;
392 
393 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
394 
395 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
396 
397 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
398 			  outbuf, sizeof(outbuf), NULL);
399 	if (rc)
400 		efx->link_state.up = false;
401 	else
402 		efx_mcdi_phy_decode_link(
403 			efx, &efx->link_state,
404 			MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
405 			MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
406 			MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
407 
408 	return !efx_link_state_equal(&efx->link_state, &old_state);
409 }
410 
411 int efx_mcdi_phy_probe(struct efx_nic *efx)
412 {
413 	struct efx_mcdi_phy_data *phy_data;
414 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
415 	u32 caps;
416 	int rc;
417 
418 	/* Initialise and populate phy_data */
419 	phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
420 	if (phy_data == NULL)
421 		return -ENOMEM;
422 
423 	rc = efx_mcdi_get_phy_cfg(efx, phy_data);
424 	if (rc != 0)
425 		goto fail;
426 
427 	/* Read initial link advertisement */
428 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
429 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
430 			  outbuf, sizeof(outbuf), NULL);
431 	if (rc)
432 		goto fail;
433 
434 	/* Fill out nic state */
435 	efx->phy_data = phy_data;
436 	efx->phy_type = phy_data->type;
437 
438 	efx->mdio_bus = phy_data->channel;
439 	efx->mdio.prtad = phy_data->port;
440 	efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
441 	efx->mdio.mode_support = 0;
442 	if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
443 		efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
444 	if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
445 		efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
446 
447 	caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
448 	if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
449 		mcdi_to_ethtool_linkset(phy_data->media, caps,
450 					efx->link_advertising);
451 	else
452 		phy_data->forced_cap = caps;
453 
454 	/* Assert that we can map efx -> mcdi loopback modes */
455 	BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
456 	BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
457 	BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
458 	BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
459 	BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
460 	BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
461 	BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
462 	BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
463 	BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
464 	BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
465 	BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
466 	BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
467 	BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
468 	BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
469 	BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
470 	BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
471 	BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
472 	BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
473 	BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
474 	BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
475 	BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
476 	BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
477 	BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
478 	BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
479 	BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
480 	BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
481 	BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
482 
483 	rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
484 	if (rc != 0)
485 		goto fail;
486 	/* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
487 	 * but by convention we don't
488 	 */
489 	efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
490 
491 	/* Set the initial link mode */
492 	efx_mcdi_phy_decode_link(efx, &efx->link_state,
493 				 MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
494 				 MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
495 				 MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
496 
497 	/* Record the initial FEC configuration (or nearest approximation
498 	 * representable in the ethtool configuration space)
499 	 */
500 	efx->fec_config = mcdi_fec_caps_to_ethtool(caps,
501 						   efx->link_state.speed == 25000 ||
502 						   efx->link_state.speed == 50000);
503 
504 	/* Default to Autonegotiated flow control if the PHY supports it */
505 	efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
506 	if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
507 		efx->wanted_fc |= EFX_FC_AUTO;
508 	efx_link_set_wanted_fc(efx, efx->wanted_fc);
509 
510 	return 0;
511 
512 fail:
513 	kfree(phy_data);
514 	return rc;
515 }
516 
517 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 void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx, struct ethtool_link_ksettings *cmd)
526 {
527 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
528 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
529 	int rc;
530 
531 	cmd->base.speed = efx->link_state.speed;
532 	cmd->base.duplex = efx->link_state.fd;
533 	cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
534 	cmd->base.phy_address = phy_cfg->port;
535 	cmd->base.autoneg = !!(efx->link_advertising[0] & ADVERTISED_Autoneg);
536 	cmd->base.mdio_support = (efx->mdio.mode_support &
537 			      (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
538 
539 	mcdi_to_ethtool_linkset(phy_cfg->media, phy_cfg->supported_cap,
540 				cmd->link_modes.supported);
541 	memcpy(cmd->link_modes.advertising, efx->link_advertising,
542 	       sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
543 
544 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
545 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
546 			  outbuf, sizeof(outbuf), NULL);
547 	if (rc)
548 		return;
549 	mcdi_to_ethtool_linkset(phy_cfg->media,
550 				MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP),
551 				cmd->link_modes.lp_advertising);
552 }
553 
554 int efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx, const struct ethtool_link_ksettings *cmd)
555 {
556 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
557 	u32 caps;
558 	int rc;
559 
560 	if (cmd->base.autoneg) {
561 		caps = (ethtool_linkset_to_mcdi_cap(cmd->link_modes.advertising) |
562 			1 << MC_CMD_PHY_CAP_AN_LBN);
563 	} else if (cmd->base.duplex) {
564 		switch (cmd->base.speed) {
565 		case 10:     caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;     break;
566 		case 100:    caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;    break;
567 		case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;   break;
568 		case 10000:  caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN;  break;
569 		case 40000:  caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN;  break;
570 		case 100000: caps = 1 << MC_CMD_PHY_CAP_100000FDX_LBN; break;
571 		case 25000:  caps = 1 << MC_CMD_PHY_CAP_25000FDX_LBN;  break;
572 		case 50000:  caps = 1 << MC_CMD_PHY_CAP_50000FDX_LBN;  break;
573 		default:     return -EINVAL;
574 		}
575 	} else {
576 		switch (cmd->base.speed) {
577 		case 10:     caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;     break;
578 		case 100:    caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;    break;
579 		case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;   break;
580 		default:     return -EINVAL;
581 		}
582 	}
583 
584 	caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, efx->fec_config);
585 
586 	rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
587 			       efx->loopback_mode, 0);
588 	if (rc)
589 		return rc;
590 
591 	if (cmd->base.autoneg) {
592 		efx_link_set_advertising(efx, cmd->link_modes.advertising);
593 		phy_cfg->forced_cap = 0;
594 	} else {
595 		efx_link_clear_advertising(efx);
596 		phy_cfg->forced_cap = caps;
597 	}
598 	return 0;
599 }
600 
601 int efx_mcdi_phy_get_fecparam(struct efx_nic *efx, struct ethtool_fecparam *fec)
602 {
603 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_V2_LEN);
604 	u32 caps, active, speed; /* MCDI format */
605 	bool is_25g = false;
606 	size_t outlen;
607 	int rc;
608 
609 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
610 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
611 			  outbuf, sizeof(outbuf), &outlen);
612 	if (rc)
613 		return rc;
614 	if (outlen < MC_CMD_GET_LINK_OUT_V2_LEN)
615 		return -EOPNOTSUPP;
616 
617 	/* behaviour for 25G/50G links depends on 25G BASER bit */
618 	speed = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_LINK_SPEED);
619 	is_25g = speed == 25000 || speed == 50000;
620 
621 	caps = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_CAP);
622 	fec->fec = mcdi_fec_caps_to_ethtool(caps, is_25g);
623 	/* BASER is never supported on 100G */
624 	if (speed == 100000)
625 		fec->fec &= ~ETHTOOL_FEC_BASER;
626 
627 	active = MCDI_DWORD(outbuf, GET_LINK_OUT_V2_FEC_TYPE);
628 	switch (active) {
629 	case MC_CMD_FEC_NONE:
630 		fec->active_fec = ETHTOOL_FEC_OFF;
631 		break;
632 	case MC_CMD_FEC_BASER:
633 		fec->active_fec = ETHTOOL_FEC_BASER;
634 		break;
635 	case MC_CMD_FEC_RS:
636 		fec->active_fec = ETHTOOL_FEC_RS;
637 		break;
638 	default:
639 		netif_warn(efx, hw, efx->net_dev,
640 			   "Firmware reports unrecognised FEC_TYPE %u\n",
641 			   active);
642 		/* We don't know what firmware has picked.  AUTO is as good a
643 		 * "can't happen" value as any other.
644 		 */
645 		fec->active_fec = ETHTOOL_FEC_AUTO;
646 		break;
647 	}
648 
649 	return 0;
650 }
651 
652 /* Basic validation to ensure that the caps we are going to attempt to set are
653  * in fact supported by the adapter.  Note that 'no FEC' is always supported.
654  */
655 static int ethtool_fec_supported(u32 supported_cap, u32 ethtool_cap)
656 {
657 	if (ethtool_cap & ETHTOOL_FEC_OFF)
658 		return 0;
659 
660 	if (ethtool_cap &&
661 	    !ethtool_fec_caps_to_mcdi(supported_cap, ethtool_cap))
662 		return -EINVAL;
663 	return 0;
664 }
665 
666 int efx_mcdi_phy_set_fecparam(struct efx_nic *efx, const struct ethtool_fecparam *fec)
667 {
668 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
669 	u32 caps;
670 	int rc;
671 
672 	rc = ethtool_fec_supported(phy_cfg->supported_cap, fec->fec);
673 	if (rc)
674 		return rc;
675 
676 	/* Work out what efx_mcdi_phy_set_link_ksettings() would produce from
677 	 * saved advertising bits
678 	 */
679 	if (test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, efx->link_advertising))
680 		caps = (ethtool_linkset_to_mcdi_cap(efx->link_advertising) |
681 			1 << MC_CMD_PHY_CAP_AN_LBN);
682 	else
683 		caps = phy_cfg->forced_cap;
684 
685 	caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, fec->fec);
686 	rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
687 			       efx->loopback_mode, 0);
688 	if (rc)
689 		return rc;
690 
691 	/* Record the new FEC setting for subsequent set_link calls */
692 	efx->fec_config = fec->fec;
693 	return 0;
694 }
695 
696 int efx_mcdi_phy_test_alive(struct efx_nic *efx)
697 {
698 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN);
699 	size_t outlen;
700 	int rc;
701 
702 	BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
703 
704 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
705 			  outbuf, sizeof(outbuf), &outlen);
706 	if (rc)
707 		return rc;
708 
709 	if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
710 		return -EIO;
711 	if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK)
712 		return -EINVAL;
713 
714 	return 0;
715 }
716 
717 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
718 {
719 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
720 	u32 caps = (efx->link_advertising[0] ?
721 		    ethtool_linkset_to_mcdi_cap(efx->link_advertising) :
722 		    phy_cfg->forced_cap);
723 
724 	caps |= ethtool_fec_caps_to_mcdi(phy_cfg->supported_cap, efx->fec_config);
725 
726 	return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
727 				 efx->loopback_mode, 0);
728 }
729 
730 static const char *const mcdi_sft9001_cable_diag_names[] = {
731 	"cable.pairA.length",
732 	"cable.pairB.length",
733 	"cable.pairC.length",
734 	"cable.pairD.length",
735 	"cable.pairA.status",
736 	"cable.pairB.status",
737 	"cable.pairC.status",
738 	"cable.pairD.status",
739 };
740 
741 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
742 			 int *results)
743 {
744 	unsigned int retry, i, count = 0;
745 	size_t outlen;
746 	u32 status;
747 	MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
748 	MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
749 	u8 *ptr;
750 	int rc;
751 
752 	BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
753 	MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
754 	rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
755 			  inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
756 	if (rc)
757 		goto out;
758 
759 	/* Wait up to 10s for BIST to finish */
760 	for (retry = 0; retry < 100; ++retry) {
761 		BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
762 		rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
763 				  outbuf, sizeof(outbuf), &outlen);
764 		if (rc)
765 			goto out;
766 
767 		status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
768 		if (status != MC_CMD_POLL_BIST_RUNNING)
769 			goto finished;
770 
771 		msleep(100);
772 	}
773 
774 	rc = -ETIMEDOUT;
775 	goto out;
776 
777 finished:
778 	results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
779 
780 	/* SFT9001 specific cable diagnostics output */
781 	if (efx->phy_type == PHY_TYPE_SFT9001B &&
782 	    (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
783 	     bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
784 		ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
785 		if (status == MC_CMD_POLL_BIST_PASSED &&
786 		    outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
787 			for (i = 0; i < 8; i++) {
788 				results[count + i] =
789 					EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
790 							EFX_DWORD_0);
791 			}
792 		}
793 		count += 8;
794 	}
795 	rc = count;
796 
797 out:
798 	return rc;
799 }
800 
801 int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results, unsigned int flags)
802 {
803 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
804 	u32 mode;
805 	int rc;
806 
807 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
808 		rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
809 		if (rc < 0)
810 			return rc;
811 
812 		results += rc;
813 	}
814 
815 	/* If we support both LONG and SHORT, then run each in response to
816 	 * break or not. Otherwise, run the one we support
817 	 */
818 	mode = 0;
819 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
820 		if ((flags & ETH_TEST_FL_OFFLINE) &&
821 		    (phy_cfg->flags &
822 		     (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
823 			mode = MC_CMD_PHY_BIST_CABLE_LONG;
824 		else
825 			mode = MC_CMD_PHY_BIST_CABLE_SHORT;
826 	} else if (phy_cfg->flags &
827 		   (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
828 		mode = MC_CMD_PHY_BIST_CABLE_LONG;
829 
830 	if (mode != 0) {
831 		rc = efx_mcdi_bist(efx, mode, results);
832 		if (rc < 0)
833 			return rc;
834 		results += rc;
835 	}
836 
837 	return 0;
838 }
839 
840 const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
841 {
842 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
843 
844 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
845 		if (index == 0)
846 			return "bist";
847 		--index;
848 	}
849 
850 	if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
851 			      (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
852 		if (index == 0)
853 			return "cable";
854 		--index;
855 
856 		if (efx->phy_type == PHY_TYPE_SFT9001B) {
857 			if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
858 				return mcdi_sft9001_cable_diag_names[index];
859 			index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
860 		}
861 	}
862 
863 	return NULL;
864 }
865 
866 #define SFP_PAGE_SIZE		128
867 #define SFF_DIAG_TYPE_OFFSET	92
868 #define SFF_DIAG_ADDR_CHANGE	BIT(2)
869 #define SFF_8079_NUM_PAGES	2
870 #define SFF_8472_NUM_PAGES	4
871 #define SFF_8436_NUM_PAGES	5
872 #define SFF_DMT_LEVEL_OFFSET	94
873 
874 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
875  * @efx:	NIC context
876  * @page:	EEPROM page number
877  * @data:	Destination data pointer
878  * @offset:	Offset in page to copy from in to data
879  * @space:	Space available in data
880  *
881  * Return:
882  *   >=0 - amount of data copied
883  *   <0  - error
884  */
885 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
886 					       unsigned int page,
887 					       u8 *data, ssize_t offset,
888 					       ssize_t space)
889 {
890 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
891 	MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
892 	unsigned int payload_len;
893 	unsigned int to_copy;
894 	size_t outlen;
895 	int rc;
896 
897 	if (offset > SFP_PAGE_SIZE)
898 		return -EINVAL;
899 
900 	to_copy = min(space, SFP_PAGE_SIZE - offset);
901 
902 	MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
903 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
904 				inbuf, sizeof(inbuf),
905 				outbuf, sizeof(outbuf),
906 				&outlen);
907 
908 	if (rc)
909 		return rc;
910 
911 	if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
912 			SFP_PAGE_SIZE))
913 		return -EIO;
914 
915 	payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
916 	if (payload_len != SFP_PAGE_SIZE)
917 		return -EIO;
918 
919 	memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
920 	       to_copy);
921 
922 	return to_copy;
923 }
924 
925 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
926 					       unsigned int page,
927 					       u8 byte)
928 {
929 	u8 data;
930 	int rc;
931 
932 	rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
933 	if (rc == 1)
934 		return data;
935 
936 	return rc;
937 }
938 
939 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
940 {
941 	/* Page zero of the EEPROM includes the diagnostic type at byte 92. */
942 	return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
943 						   SFF_DIAG_TYPE_OFFSET);
944 }
945 
946 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
947 {
948 	/* Page zero of the EEPROM includes the DMT level at byte 94. */
949 	return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
950 						   SFF_DMT_LEVEL_OFFSET);
951 }
952 
953 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
954 {
955 	struct efx_mcdi_phy_data *phy_data = efx->phy_data;
956 
957 	if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
958 		return phy_data->media;
959 
960 	/* A QSFP+ NIC may actually have an SFP+ module attached.
961 	 * The ID is page 0, byte 0.
962 	 */
963 	switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
964 	case 0x3:
965 		return MC_CMD_MEDIA_SFP_PLUS;
966 	case 0xc:
967 	case 0xd:
968 		return MC_CMD_MEDIA_QSFP_PLUS;
969 	default:
970 		return 0;
971 	}
972 }
973 
974 int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx, struct ethtool_eeprom *ee, u8 *data)
975 {
976 	int rc;
977 	ssize_t space_remaining = ee->len;
978 	unsigned int page_off;
979 	bool ignore_missing;
980 	int num_pages;
981 	int page;
982 
983 	switch (efx_mcdi_phy_module_type(efx)) {
984 	case MC_CMD_MEDIA_SFP_PLUS:
985 		num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
986 				SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
987 		page = 0;
988 		ignore_missing = false;
989 		break;
990 	case MC_CMD_MEDIA_QSFP_PLUS:
991 		num_pages = SFF_8436_NUM_PAGES;
992 		page = -1; /* We obtain the lower page by asking for -1. */
993 		ignore_missing = true; /* Ignore missing pages after page 0. */
994 		break;
995 	default:
996 		return -EOPNOTSUPP;
997 	}
998 
999 	page_off = ee->offset % SFP_PAGE_SIZE;
1000 	page += ee->offset / SFP_PAGE_SIZE;
1001 
1002 	while (space_remaining && (page < num_pages)) {
1003 		rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
1004 							 data, page_off,
1005 							 space_remaining);
1006 
1007 		if (rc > 0) {
1008 			space_remaining -= rc;
1009 			data += rc;
1010 			page_off = 0;
1011 			page++;
1012 		} else if (rc == 0) {
1013 			space_remaining = 0;
1014 		} else if (ignore_missing && (page > 0)) {
1015 			int intended_size = SFP_PAGE_SIZE - page_off;
1016 
1017 			space_remaining -= intended_size;
1018 			if (space_remaining < 0) {
1019 				space_remaining = 0;
1020 			} else {
1021 				memset(data, 0, intended_size);
1022 				data += intended_size;
1023 				page_off = 0;
1024 				page++;
1025 				rc = 0;
1026 			}
1027 		} else {
1028 			return rc;
1029 		}
1030 	}
1031 
1032 	return 0;
1033 }
1034 
1035 int efx_mcdi_phy_get_module_info(struct efx_nic *efx, struct ethtool_modinfo *modinfo)
1036 {
1037 	int sff_8472_level;
1038 	int diag_type;
1039 
1040 	switch (efx_mcdi_phy_module_type(efx)) {
1041 	case MC_CMD_MEDIA_SFP_PLUS:
1042 		sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
1043 
1044 		/* If we can't read the diagnostics level we have none. */
1045 		if (sff_8472_level < 0)
1046 			return -EOPNOTSUPP;
1047 
1048 		/* Check if this module requires the (unsupported) address
1049 		 * change operation.
1050 		 */
1051 		diag_type = efx_mcdi_phy_diag_type(efx);
1052 
1053 		if (sff_8472_level == 0 ||
1054 		    (diag_type & SFF_DIAG_ADDR_CHANGE)) {
1055 			modinfo->type = ETH_MODULE_SFF_8079;
1056 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1057 		} else {
1058 			modinfo->type = ETH_MODULE_SFF_8472;
1059 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1060 		}
1061 		break;
1062 
1063 	case MC_CMD_MEDIA_QSFP_PLUS:
1064 		modinfo->type = ETH_MODULE_SFF_8436;
1065 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1066 		break;
1067 
1068 	default:
1069 		return -EOPNOTSUPP;
1070 	}
1071 
1072 	return 0;
1073 }
1074 
1075 static unsigned int efx_calc_mac_mtu(struct efx_nic *efx)
1076 {
1077 	return EFX_MAX_FRAME_LEN(efx->net_dev->mtu);
1078 }
1079 
1080 int efx_mcdi_set_mac(struct efx_nic *efx)
1081 {
1082 	u32 fcntl;
1083 	MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
1084 
1085 	BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
1086 
1087 	/* This has no effect on EF10 */
1088 	ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
1089 			efx->net_dev->dev_addr);
1090 
1091 	MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU, efx_calc_mac_mtu(efx));
1092 	MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
1093 
1094 	/* Set simple MAC filter for Siena */
1095 	MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
1096 			      SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
1097 
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 
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 
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 
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 
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 
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 
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 
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) */
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 
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