1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2009-2013 Solarflare Communications Inc.
5  */
6 
7 /*
8  * Driver for PHY related operations via MCDI.
9  */
10 
11 #include <linux/slab.h>
12 #include "efx.h"
13 #include "mcdi.h"
14 #include "mcdi_pcol.h"
15 #include "nic.h"
16 #include "selftest.h"
17 #include "mcdi_port_common.h"
18 
19 static int efx_mcdi_mdio_read(struct net_device *net_dev,
20 			      int prtad, int devad, u16 addr)
21 {
22 	struct efx_nic *efx = netdev_priv(net_dev);
23 	MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
24 	MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
25 	size_t outlen;
26 	int rc;
27 
28 	MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
29 	MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
30 	MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
31 	MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
32 
33 	rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
34 			  outbuf, sizeof(outbuf), &outlen);
35 	if (rc)
36 		return rc;
37 
38 	if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
39 	    MC_CMD_MDIO_STATUS_GOOD)
40 		return -EIO;
41 
42 	return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
43 }
44 
45 static int efx_mcdi_mdio_write(struct net_device *net_dev,
46 			       int prtad, int devad, u16 addr, u16 value)
47 {
48 	struct efx_nic *efx = netdev_priv(net_dev);
49 	MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
50 	MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
51 	size_t outlen;
52 	int rc;
53 
54 	MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
55 	MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
56 	MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
57 	MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
58 	MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
59 
60 	rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
61 			  outbuf, sizeof(outbuf), &outlen);
62 	if (rc)
63 		return rc;
64 
65 	if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
66 	    MC_CMD_MDIO_STATUS_GOOD)
67 		return -EIO;
68 
69 	return 0;
70 }
71 
72 static int efx_mcdi_phy_probe(struct efx_nic *efx)
73 {
74 	struct efx_mcdi_phy_data *phy_data;
75 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
76 	u32 caps;
77 	int rc;
78 
79 	/* Initialise and populate phy_data */
80 	phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
81 	if (phy_data == NULL)
82 		return -ENOMEM;
83 
84 	rc = efx_mcdi_get_phy_cfg(efx, phy_data);
85 	if (rc != 0)
86 		goto fail;
87 
88 	/* Read initial link advertisement */
89 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
90 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
91 			  outbuf, sizeof(outbuf), NULL);
92 	if (rc)
93 		goto fail;
94 
95 	/* Fill out nic state */
96 	efx->phy_data = phy_data;
97 	efx->phy_type = phy_data->type;
98 
99 	efx->mdio_bus = phy_data->channel;
100 	efx->mdio.prtad = phy_data->port;
101 	efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
102 	efx->mdio.mode_support = 0;
103 	if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
104 		efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
105 	if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
106 		efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
107 
108 	caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
109 	if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
110 		mcdi_to_ethtool_linkset(phy_data->media, caps,
111 					efx->link_advertising);
112 	else
113 		phy_data->forced_cap = caps;
114 
115 	/* Assert that we can map efx -> mcdi loopback modes */
116 	BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
117 	BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
118 	BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
119 	BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
120 	BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
121 	BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
122 	BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
123 	BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
124 	BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
125 	BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
126 	BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
127 	BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
128 	BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
129 	BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
130 	BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
131 	BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
132 	BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
133 	BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
134 	BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
135 	BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
136 	BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
137 	BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
138 	BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
139 	BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
140 	BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
141 	BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
142 	BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
143 
144 	rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
145 	if (rc != 0)
146 		goto fail;
147 	/* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
148 	 * but by convention we don't */
149 	efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
150 
151 	/* Set the initial link mode */
152 	efx_mcdi_phy_decode_link(
153 		efx, &efx->link_state,
154 		MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
155 		MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
156 		MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
157 
158 	/* Record the initial FEC configuration (or nearest approximation
159 	 * representable in the ethtool configuration space)
160 	 */
161 	efx->fec_config = mcdi_fec_caps_to_ethtool(caps,
162 						   efx->link_state.speed == 25000 ||
163 						   efx->link_state.speed == 50000);
164 
165 	/* Default to Autonegotiated flow control if the PHY supports it */
166 	efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
167 	if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
168 		efx->wanted_fc |= EFX_FC_AUTO;
169 	efx_link_set_wanted_fc(efx, efx->wanted_fc);
170 
171 	return 0;
172 
173 fail:
174 	kfree(phy_data);
175 	return rc;
176 }
177 
178 int efx_mcdi_port_reconfigure(struct efx_nic *efx)
179 {
180 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
181 	u32 caps = (efx->link_advertising[0] ?
182 		    ethtool_linkset_to_mcdi_cap(efx->link_advertising) :
183 		    phy_cfg->forced_cap);
184 
185 	caps |= ethtool_fec_caps_to_mcdi(efx->fec_config);
186 
187 	return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
188 				 efx->loopback_mode, 0);
189 }
190 
191 static void efx_mcdi_phy_remove(struct efx_nic *efx)
192 {
193 	struct efx_mcdi_phy_data *phy_data = efx->phy_data;
194 
195 	efx->phy_data = NULL;
196 	kfree(phy_data);
197 }
198 
199 static void efx_mcdi_phy_get_link_ksettings(struct efx_nic *efx,
200 					    struct ethtool_link_ksettings *cmd)
201 {
202 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
203 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
204 	int rc;
205 
206 	cmd->base.speed = efx->link_state.speed;
207 	cmd->base.duplex = efx->link_state.fd;
208 	cmd->base.port = mcdi_to_ethtool_media(phy_cfg->media);
209 	cmd->base.phy_address = phy_cfg->port;
210 	cmd->base.autoneg = !!(efx->link_advertising[0] & ADVERTISED_Autoneg);
211 	cmd->base.mdio_support = (efx->mdio.mode_support &
212 			      (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
213 
214 	mcdi_to_ethtool_linkset(phy_cfg->media, phy_cfg->supported_cap,
215 				cmd->link_modes.supported);
216 	memcpy(cmd->link_modes.advertising, efx->link_advertising,
217 	       sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
218 
219 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
220 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
221 			  outbuf, sizeof(outbuf), NULL);
222 	if (rc)
223 		return;
224 	mcdi_to_ethtool_linkset(phy_cfg->media,
225 				MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP),
226 				cmd->link_modes.lp_advertising);
227 }
228 
229 static int
230 efx_mcdi_phy_set_link_ksettings(struct efx_nic *efx,
231 				const struct ethtool_link_ksettings *cmd)
232 {
233 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
234 	u32 caps;
235 	int rc;
236 
237 	if (cmd->base.autoneg) {
238 		caps = (ethtool_linkset_to_mcdi_cap(cmd->link_modes.advertising) |
239 			1 << MC_CMD_PHY_CAP_AN_LBN);
240 	} else if (cmd->base.duplex) {
241 		switch (cmd->base.speed) {
242 		case 10:     caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN;     break;
243 		case 100:    caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN;    break;
244 		case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN;   break;
245 		case 10000:  caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN;  break;
246 		case 40000:  caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN;  break;
247 		case 100000: caps = 1 << MC_CMD_PHY_CAP_100000FDX_LBN; break;
248 		case 25000:  caps = 1 << MC_CMD_PHY_CAP_25000FDX_LBN;  break;
249 		case 50000:  caps = 1 << MC_CMD_PHY_CAP_50000FDX_LBN;  break;
250 		default:     return -EINVAL;
251 		}
252 	} else {
253 		switch (cmd->base.speed) {
254 		case 10:     caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN;     break;
255 		case 100:    caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN;    break;
256 		case 1000:   caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN;   break;
257 		default:     return -EINVAL;
258 		}
259 	}
260 
261 	caps |= ethtool_fec_caps_to_mcdi(efx->fec_config);
262 
263 	rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
264 			       efx->loopback_mode, 0);
265 	if (rc)
266 		return rc;
267 
268 	if (cmd->base.autoneg) {
269 		efx_link_set_advertising(efx, cmd->link_modes.advertising);
270 		phy_cfg->forced_cap = 0;
271 	} else {
272 		efx_link_clear_advertising(efx);
273 		phy_cfg->forced_cap = caps;
274 	}
275 	return 0;
276 }
277 
278 static int efx_mcdi_phy_set_fecparam(struct efx_nic *efx,
279 				     const struct ethtool_fecparam *fec)
280 {
281 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
282 	u32 caps;
283 	int rc;
284 
285 	/* Work out what efx_mcdi_phy_set_link_ksettings() would produce from
286 	 * saved advertising bits
287 	 */
288 	if (test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, efx->link_advertising))
289 		caps = (ethtool_linkset_to_mcdi_cap(efx->link_advertising) |
290 			1 << MC_CMD_PHY_CAP_AN_LBN);
291 	else
292 		caps = phy_cfg->forced_cap;
293 
294 	caps |= ethtool_fec_caps_to_mcdi(fec->fec);
295 	rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
296 			       efx->loopback_mode, 0);
297 	if (rc)
298 		return rc;
299 
300 	/* Record the new FEC setting for subsequent set_link calls */
301 	efx->fec_config = fec->fec;
302 	return 0;
303 }
304 
305 static const char *const mcdi_sft9001_cable_diag_names[] = {
306 	"cable.pairA.length",
307 	"cable.pairB.length",
308 	"cable.pairC.length",
309 	"cable.pairD.length",
310 	"cable.pairA.status",
311 	"cable.pairB.status",
312 	"cable.pairC.status",
313 	"cable.pairD.status",
314 };
315 
316 static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
317 			 int *results)
318 {
319 	unsigned int retry, i, count = 0;
320 	size_t outlen;
321 	u32 status;
322 	MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
323 	MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
324 	u8 *ptr;
325 	int rc;
326 
327 	BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
328 	MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
329 	rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
330 			  inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
331 	if (rc)
332 		goto out;
333 
334 	/* Wait up to 10s for BIST to finish */
335 	for (retry = 0; retry < 100; ++retry) {
336 		BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
337 		rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
338 				  outbuf, sizeof(outbuf), &outlen);
339 		if (rc)
340 			goto out;
341 
342 		status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
343 		if (status != MC_CMD_POLL_BIST_RUNNING)
344 			goto finished;
345 
346 		msleep(100);
347 	}
348 
349 	rc = -ETIMEDOUT;
350 	goto out;
351 
352 finished:
353 	results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
354 
355 	/* SFT9001 specific cable diagnostics output */
356 	if (efx->phy_type == PHY_TYPE_SFT9001B &&
357 	    (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
358 	     bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
359 		ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
360 		if (status == MC_CMD_POLL_BIST_PASSED &&
361 		    outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
362 			for (i = 0; i < 8; i++) {
363 				results[count + i] =
364 					EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
365 							EFX_DWORD_0);
366 			}
367 		}
368 		count += 8;
369 	}
370 	rc = count;
371 
372 out:
373 	return rc;
374 }
375 
376 static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
377 				  unsigned flags)
378 {
379 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
380 	u32 mode;
381 	int rc;
382 
383 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
384 		rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
385 		if (rc < 0)
386 			return rc;
387 
388 		results += rc;
389 	}
390 
391 	/* If we support both LONG and SHORT, then run each in response to
392 	 * break or not. Otherwise, run the one we support */
393 	mode = 0;
394 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
395 		if ((flags & ETH_TEST_FL_OFFLINE) &&
396 		    (phy_cfg->flags &
397 		     (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
398 			mode = MC_CMD_PHY_BIST_CABLE_LONG;
399 		else
400 			mode = MC_CMD_PHY_BIST_CABLE_SHORT;
401 	} else if (phy_cfg->flags &
402 		   (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
403 		mode = MC_CMD_PHY_BIST_CABLE_LONG;
404 
405 	if (mode != 0) {
406 		rc = efx_mcdi_bist(efx, mode, results);
407 		if (rc < 0)
408 			return rc;
409 		results += rc;
410 	}
411 
412 	return 0;
413 }
414 
415 static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
416 					  unsigned int index)
417 {
418 	struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
419 
420 	if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
421 		if (index == 0)
422 			return "bist";
423 		--index;
424 	}
425 
426 	if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
427 			      (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
428 		if (index == 0)
429 			return "cable";
430 		--index;
431 
432 		if (efx->phy_type == PHY_TYPE_SFT9001B) {
433 			if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
434 				return mcdi_sft9001_cable_diag_names[index];
435 			index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
436 		}
437 	}
438 
439 	return NULL;
440 }
441 
442 #define SFP_PAGE_SIZE		128
443 #define SFF_DIAG_TYPE_OFFSET	92
444 #define SFF_DIAG_ADDR_CHANGE	BIT(2)
445 #define SFF_8079_NUM_PAGES	2
446 #define SFF_8472_NUM_PAGES	4
447 #define SFF_8436_NUM_PAGES	5
448 #define SFF_DMT_LEVEL_OFFSET	94
449 
450 /** efx_mcdi_phy_get_module_eeprom_page() - Get a single page of module eeprom
451  * @efx:	NIC context
452  * @page:	EEPROM page number
453  * @data:	Destination data pointer
454  * @offset:	Offset in page to copy from in to data
455  * @space:	Space available in data
456  *
457  * Return:
458  *   >=0 - amount of data copied
459  *   <0  - error
460  */
461 static int efx_mcdi_phy_get_module_eeprom_page(struct efx_nic *efx,
462 					       unsigned int page,
463 					       u8 *data, ssize_t offset,
464 					       ssize_t space)
465 {
466 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
467 	MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
468 	size_t outlen;
469 	unsigned int payload_len;
470 	unsigned int to_copy;
471 	int rc;
472 
473 	if (offset > SFP_PAGE_SIZE)
474 		return -EINVAL;
475 
476 	to_copy = min(space, SFP_PAGE_SIZE - offset);
477 
478 	MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
479 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_PHY_MEDIA_INFO,
480 				inbuf, sizeof(inbuf),
481 				outbuf, sizeof(outbuf),
482 				&outlen);
483 
484 	if (rc)
485 		return rc;
486 
487 	if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
488 			SFP_PAGE_SIZE))
489 		return -EIO;
490 
491 	payload_len = MCDI_DWORD(outbuf, GET_PHY_MEDIA_INFO_OUT_DATALEN);
492 	if (payload_len != SFP_PAGE_SIZE)
493 		return -EIO;
494 
495 	memcpy(data, MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
496 	       to_copy);
497 
498 	return to_copy;
499 }
500 
501 static int efx_mcdi_phy_get_module_eeprom_byte(struct efx_nic *efx,
502 					       unsigned int page,
503 					       u8 byte)
504 {
505 	int rc;
506 	u8 data;
507 
508 	rc = efx_mcdi_phy_get_module_eeprom_page(efx, page, &data, byte, 1);
509 	if (rc == 1)
510 		return data;
511 
512 	return rc;
513 }
514 
515 static int efx_mcdi_phy_diag_type(struct efx_nic *efx)
516 {
517 	/* Page zero of the EEPROM includes the diagnostic type at byte 92. */
518 	return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
519 						   SFF_DIAG_TYPE_OFFSET);
520 }
521 
522 static int efx_mcdi_phy_sff_8472_level(struct efx_nic *efx)
523 {
524 	/* Page zero of the EEPROM includes the DMT level at byte 94. */
525 	return efx_mcdi_phy_get_module_eeprom_byte(efx, 0,
526 						   SFF_DMT_LEVEL_OFFSET);
527 }
528 
529 static u32 efx_mcdi_phy_module_type(struct efx_nic *efx)
530 {
531 	struct efx_mcdi_phy_data *phy_data = efx->phy_data;
532 
533 	if (phy_data->media != MC_CMD_MEDIA_QSFP_PLUS)
534 		return phy_data->media;
535 
536 	/* A QSFP+ NIC may actually have an SFP+ module attached.
537 	 * The ID is page 0, byte 0.
538 	 */
539 	switch (efx_mcdi_phy_get_module_eeprom_byte(efx, 0, 0)) {
540 	case 0x3:
541 		return MC_CMD_MEDIA_SFP_PLUS;
542 	case 0xc:
543 	case 0xd:
544 		return MC_CMD_MEDIA_QSFP_PLUS;
545 	default:
546 		return 0;
547 	}
548 }
549 
550 static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
551 					  struct ethtool_eeprom *ee, u8 *data)
552 {
553 	int rc;
554 	ssize_t space_remaining = ee->len;
555 	unsigned int page_off;
556 	bool ignore_missing;
557 	int num_pages;
558 	int page;
559 
560 	switch (efx_mcdi_phy_module_type(efx)) {
561 	case MC_CMD_MEDIA_SFP_PLUS:
562 		num_pages = efx_mcdi_phy_sff_8472_level(efx) > 0 ?
563 				SFF_8472_NUM_PAGES : SFF_8079_NUM_PAGES;
564 		page = 0;
565 		ignore_missing = false;
566 		break;
567 	case MC_CMD_MEDIA_QSFP_PLUS:
568 		num_pages = SFF_8436_NUM_PAGES;
569 		page = -1; /* We obtain the lower page by asking for -1. */
570 		ignore_missing = true; /* Ignore missing pages after page 0. */
571 		break;
572 	default:
573 		return -EOPNOTSUPP;
574 	}
575 
576 	page_off = ee->offset % SFP_PAGE_SIZE;
577 	page += ee->offset / SFP_PAGE_SIZE;
578 
579 	while (space_remaining && (page < num_pages)) {
580 		rc = efx_mcdi_phy_get_module_eeprom_page(efx, page,
581 							 data, page_off,
582 							 space_remaining);
583 
584 		if (rc > 0) {
585 			space_remaining -= rc;
586 			data += rc;
587 			page_off = 0;
588 			page++;
589 		} else if (rc == 0) {
590 			space_remaining = 0;
591 		} else if (ignore_missing && (page > 0)) {
592 			int intended_size = SFP_PAGE_SIZE - page_off;
593 
594 			space_remaining -= intended_size;
595 			if (space_remaining < 0) {
596 				space_remaining = 0;
597 			} else {
598 				memset(data, 0, intended_size);
599 				data += intended_size;
600 				page_off = 0;
601 				page++;
602 				rc = 0;
603 			}
604 		} else {
605 			return rc;
606 		}
607 	}
608 
609 	return 0;
610 }
611 
612 static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
613 					struct ethtool_modinfo *modinfo)
614 {
615 	int sff_8472_level;
616 	int diag_type;
617 
618 	switch (efx_mcdi_phy_module_type(efx)) {
619 	case MC_CMD_MEDIA_SFP_PLUS:
620 		sff_8472_level = efx_mcdi_phy_sff_8472_level(efx);
621 
622 		/* If we can't read the diagnostics level we have none. */
623 		if (sff_8472_level < 0)
624 			return -EOPNOTSUPP;
625 
626 		/* Check if this module requires the (unsupported) address
627 		 * change operation.
628 		 */
629 		diag_type = efx_mcdi_phy_diag_type(efx);
630 
631 		if ((sff_8472_level == 0) ||
632 		    (diag_type & SFF_DIAG_ADDR_CHANGE)) {
633 			modinfo->type = ETH_MODULE_SFF_8079;
634 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
635 		} else {
636 			modinfo->type = ETH_MODULE_SFF_8472;
637 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
638 		}
639 		break;
640 
641 	case MC_CMD_MEDIA_QSFP_PLUS:
642 		modinfo->type = ETH_MODULE_SFF_8436;
643 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
644 		break;
645 
646 	default:
647 		return -EOPNOTSUPP;
648 	}
649 
650 	return 0;
651 }
652 
653 static const struct efx_phy_operations efx_mcdi_phy_ops = {
654 	.probe		= efx_mcdi_phy_probe,
655 	.init		= efx_port_dummy_op_int,
656 	.reconfigure	= efx_mcdi_port_reconfigure,
657 	.poll		= efx_mcdi_phy_poll,
658 	.fini		= efx_port_dummy_op_void,
659 	.remove		= efx_mcdi_phy_remove,
660 	.get_link_ksettings = efx_mcdi_phy_get_link_ksettings,
661 	.set_link_ksettings = efx_mcdi_phy_set_link_ksettings,
662 	.get_fecparam	= efx_mcdi_phy_get_fecparam,
663 	.set_fecparam	= efx_mcdi_phy_set_fecparam,
664 	.test_alive	= efx_mcdi_phy_test_alive,
665 	.run_tests	= efx_mcdi_phy_run_tests,
666 	.test_name	= efx_mcdi_phy_test_name,
667 	.get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
668 	.get_module_info = efx_mcdi_phy_get_module_info,
669 };
670 
671 u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
672 {
673 	struct efx_mcdi_phy_data *phy_data = efx->phy_data;
674 
675 	return phy_data->supported_cap;
676 }
677 
678 bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
679 {
680 	MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
681 	size_t outlength;
682 	int rc;
683 
684 	BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
685 
686 	rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
687 			  outbuf, sizeof(outbuf), &outlength);
688 	if (rc)
689 		return true;
690 
691 	return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
692 }
693 
694 enum efx_stats_action {
695 	EFX_STATS_ENABLE,
696 	EFX_STATS_DISABLE,
697 	EFX_STATS_PULL,
698 };
699 
700 static int efx_mcdi_mac_stats(struct efx_nic *efx,
701 			      enum efx_stats_action action, int clear)
702 {
703 	MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
704 	int rc;
705 	int change = action == EFX_STATS_PULL ? 0 : 1;
706 	int enable = action == EFX_STATS_ENABLE ? 1 : 0;
707 	int period = action == EFX_STATS_ENABLE ? 1000 : 0;
708 	dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
709 	u32 dma_len = action != EFX_STATS_DISABLE ?
710 		efx->num_mac_stats * sizeof(u64) : 0;
711 
712 	BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
713 
714 	MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
715 	MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
716 			      MAC_STATS_IN_DMA, !!enable,
717 			      MAC_STATS_IN_CLEAR, clear,
718 			      MAC_STATS_IN_PERIODIC_CHANGE, change,
719 			      MAC_STATS_IN_PERIODIC_ENABLE, enable,
720 			      MAC_STATS_IN_PERIODIC_CLEAR, 0,
721 			      MAC_STATS_IN_PERIODIC_NOEVENT, 1,
722 			      MAC_STATS_IN_PERIOD_MS, period);
723 	MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
724 
725 	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
726 		struct efx_ef10_nic_data *nic_data = efx->nic_data;
727 
728 		MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
729 	}
730 
731 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
732 				NULL, 0, NULL);
733 	/* Expect ENOENT if DMA queues have not been set up */
734 	if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
735 		efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
736 				       NULL, 0, rc);
737 	return rc;
738 }
739 
740 void efx_mcdi_mac_start_stats(struct efx_nic *efx)
741 {
742 	__le64 *dma_stats = efx->stats_buffer.addr;
743 
744 	dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
745 
746 	efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
747 }
748 
749 void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
750 {
751 	efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
752 }
753 
754 #define EFX_MAC_STATS_WAIT_US 100
755 #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
756 
757 void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
758 {
759 	__le64 *dma_stats = efx->stats_buffer.addr;
760 	int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
761 
762 	dma_stats[efx->num_mac_stats - 1] = EFX_MC_STATS_GENERATION_INVALID;
763 	efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
764 
765 	while (dma_stats[efx->num_mac_stats - 1] ==
766 				EFX_MC_STATS_GENERATION_INVALID &&
767 			attempts-- != 0)
768 		udelay(EFX_MAC_STATS_WAIT_US);
769 }
770 
771 int efx_mcdi_port_probe(struct efx_nic *efx)
772 {
773 	int rc;
774 
775 	/* Hook in PHY operations table */
776 	efx->phy_op = &efx_mcdi_phy_ops;
777 
778 	/* Set up MDIO structure for PHY */
779 	efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
780 	efx->mdio.mdio_read = efx_mcdi_mdio_read;
781 	efx->mdio.mdio_write = efx_mcdi_mdio_write;
782 
783 	/* Fill out MDIO structure, loopback modes, and initial link state */
784 	rc = efx->phy_op->probe(efx);
785 	if (rc != 0)
786 		return rc;
787 
788 	/* Allocate buffer for stats */
789 	rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
790 				  efx->num_mac_stats * sizeof(u64), GFP_KERNEL);
791 	if (rc)
792 		return rc;
793 	netif_dbg(efx, probe, efx->net_dev,
794 		  "stats buffer at %llx (virt %p phys %llx)\n",
795 		  (u64)efx->stats_buffer.dma_addr,
796 		  efx->stats_buffer.addr,
797 		  (u64)virt_to_phys(efx->stats_buffer.addr));
798 
799 	efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
800 
801 	return 0;
802 }
803 
804 void efx_mcdi_port_remove(struct efx_nic *efx)
805 {
806 	efx->phy_op->remove(efx);
807 	efx_nic_free_buffer(efx, &efx->stats_buffer);
808 }
809