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