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