xref: /freebsd/sys/dev/sfxge/common/ef10_phy.c (revision eb9da1ada8b6b2c74378a5c17029ec5a7fb199e6)
1 /*-
2  * Copyright (c) 2012-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "efx.h"
35 #include "efx_impl.h"
36 
37 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
38 
39 static			void
40 mcdi_phy_decode_cap(
41 	__in		uint32_t mcdi_cap,
42 	__out		uint32_t *maskp)
43 {
44 	uint32_t mask;
45 
46 	mask = 0;
47 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
48 		mask |= (1 << EFX_PHY_CAP_10HDX);
49 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
50 		mask |= (1 << EFX_PHY_CAP_10FDX);
51 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
52 		mask |= (1 << EFX_PHY_CAP_100HDX);
53 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
54 		mask |= (1 << EFX_PHY_CAP_100FDX);
55 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
56 		mask |= (1 << EFX_PHY_CAP_1000HDX);
57 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
58 		mask |= (1 << EFX_PHY_CAP_1000FDX);
59 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
60 		mask |= (1 << EFX_PHY_CAP_10000FDX);
61 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
62 		mask |= (1 << EFX_PHY_CAP_40000FDX);
63 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
64 		mask |= (1 << EFX_PHY_CAP_PAUSE);
65 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
66 		mask |= (1 << EFX_PHY_CAP_ASYM);
67 	if (mcdi_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
68 		mask |= (1 << EFX_PHY_CAP_AN);
69 
70 	*maskp = mask;
71 }
72 
73 static			void
74 mcdi_phy_decode_link_mode(
75 	__in		efx_nic_t *enp,
76 	__in		uint32_t link_flags,
77 	__in		unsigned int speed,
78 	__in		unsigned int fcntl,
79 	__out		efx_link_mode_t *link_modep,
80 	__out		unsigned int *fcntlp)
81 {
82 	boolean_t fd = !!(link_flags &
83 		    (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
84 	boolean_t up = !!(link_flags &
85 		    (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
86 
87 	_NOTE(ARGUNUSED(enp))
88 
89 	if (!up)
90 		*link_modep = EFX_LINK_DOWN;
91 	else if (speed == 40000 && fd)
92 		*link_modep = EFX_LINK_40000FDX;
93 	else if (speed == 10000 && fd)
94 		*link_modep = EFX_LINK_10000FDX;
95 	else if (speed == 1000)
96 		*link_modep = fd ? EFX_LINK_1000FDX : EFX_LINK_1000HDX;
97 	else if (speed == 100)
98 		*link_modep = fd ? EFX_LINK_100FDX : EFX_LINK_100HDX;
99 	else if (speed == 10)
100 		*link_modep = fd ? EFX_LINK_10FDX : EFX_LINK_10HDX;
101 	else
102 		*link_modep = EFX_LINK_UNKNOWN;
103 
104 	if (fcntl == MC_CMD_FCNTL_OFF)
105 		*fcntlp = 0;
106 	else if (fcntl == MC_CMD_FCNTL_RESPOND)
107 		*fcntlp = EFX_FCNTL_RESPOND;
108 	else if (fcntl == MC_CMD_FCNTL_GENERATE)
109 		*fcntlp = EFX_FCNTL_GENERATE;
110 	else if (fcntl == MC_CMD_FCNTL_BIDIR)
111 		*fcntlp = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
112 	else {
113 		EFSYS_PROBE1(mc_pcol_error, int, fcntl);
114 		*fcntlp = 0;
115 	}
116 }
117 
118 
119 			void
120 ef10_phy_link_ev(
121 	__in		efx_nic_t *enp,
122 	__in		efx_qword_t *eqp,
123 	__out		efx_link_mode_t *link_modep)
124 {
125 	efx_port_t *epp = &(enp->en_port);
126 	unsigned int link_flags;
127 	unsigned int speed;
128 	unsigned int fcntl;
129 	efx_link_mode_t link_mode;
130 	uint32_t lp_cap_mask;
131 
132 	/*
133 	 * Convert the LINKCHANGE speed enumeration into mbit/s, in the
134 	 * same way as GET_LINK encodes the speed
135 	 */
136 	switch (MCDI_EV_FIELD(eqp, LINKCHANGE_SPEED)) {
137 	case MCDI_EVENT_LINKCHANGE_SPEED_100M:
138 		speed = 100;
139 		break;
140 	case MCDI_EVENT_LINKCHANGE_SPEED_1G:
141 		speed = 1000;
142 		break;
143 	case MCDI_EVENT_LINKCHANGE_SPEED_10G:
144 		speed = 10000;
145 		break;
146 	case MCDI_EVENT_LINKCHANGE_SPEED_40G:
147 		speed = 40000;
148 		break;
149 	default:
150 		speed = 0;
151 		break;
152 	}
153 
154 	link_flags = MCDI_EV_FIELD(eqp, LINKCHANGE_LINK_FLAGS);
155 	mcdi_phy_decode_link_mode(enp, link_flags, speed,
156 				    MCDI_EV_FIELD(eqp, LINKCHANGE_FCNTL),
157 				    &link_mode, &fcntl);
158 	mcdi_phy_decode_cap(MCDI_EV_FIELD(eqp, LINKCHANGE_LP_CAP),
159 			    &lp_cap_mask);
160 
161 	/*
162 	 * It's safe to update ep_lp_cap_mask without the driver's port lock
163 	 * because presumably any concurrently running efx_port_poll() is
164 	 * only going to arrive at the same value.
165 	 *
166 	 * ep_fcntl has two meanings. It's either the link common fcntl
167 	 * (if the PHY supports AN), or it's the forced link state. If
168 	 * the former, it's safe to update the value for the same reason as
169 	 * for ep_lp_cap_mask. If the latter, then just ignore the value,
170 	 * because we can race with efx_mac_fcntl_set().
171 	 */
172 	epp->ep_lp_cap_mask = lp_cap_mask;
173 	epp->ep_fcntl = fcntl;
174 
175 	*link_modep = link_mode;
176 }
177 
178 	__checkReturn	efx_rc_t
179 ef10_phy_power(
180 	__in		efx_nic_t *enp,
181 	__in		boolean_t power)
182 {
183 	efx_rc_t rc;
184 
185 	if (!power)
186 		return (0);
187 
188 	/* Check if the PHY is a zombie */
189 	if ((rc = ef10_phy_verify(enp)) != 0)
190 		goto fail1;
191 
192 	enp->en_reset_flags |= EFX_RESET_PHY;
193 
194 	return (0);
195 
196 fail1:
197 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
198 
199 	return (rc);
200 }
201 
202 	__checkReturn	efx_rc_t
203 ef10_phy_get_link(
204 	__in		efx_nic_t *enp,
205 	__out		ef10_link_state_t *elsp)
206 {
207 	efx_mcdi_req_t req;
208 	uint8_t payload[MAX(MC_CMD_GET_LINK_IN_LEN,
209 			    MC_CMD_GET_LINK_OUT_LEN)];
210 	efx_rc_t rc;
211 
212 	(void) memset(payload, 0, sizeof (payload));
213 	req.emr_cmd = MC_CMD_GET_LINK;
214 	req.emr_in_buf = payload;
215 	req.emr_in_length = MC_CMD_GET_LINK_IN_LEN;
216 	req.emr_out_buf = payload;
217 	req.emr_out_length = MC_CMD_GET_LINK_OUT_LEN;
218 
219 	efx_mcdi_execute(enp, &req);
220 
221 	if (req.emr_rc != 0) {
222 		rc = req.emr_rc;
223 		goto fail1;
224 	}
225 
226 	if (req.emr_out_length_used < MC_CMD_GET_LINK_OUT_LEN) {
227 		rc = EMSGSIZE;
228 		goto fail2;
229 	}
230 
231 	mcdi_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_CAP),
232 			    &elsp->els_adv_cap_mask);
233 	mcdi_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_LP_CAP),
234 			    &elsp->els_lp_cap_mask);
235 
236 	mcdi_phy_decode_link_mode(enp, MCDI_OUT_DWORD(req, GET_LINK_OUT_FLAGS),
237 			    MCDI_OUT_DWORD(req, GET_LINK_OUT_LINK_SPEED),
238 			    MCDI_OUT_DWORD(req, GET_LINK_OUT_FCNTL),
239 			    &elsp->els_link_mode, &elsp->els_fcntl);
240 
241 #if EFSYS_OPT_LOOPBACK
242 	/* Assert the MC_CMD_LOOPBACK and EFX_LOOPBACK namespace agree */
243 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_NONE == EFX_LOOPBACK_OFF);
244 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_DATA == EFX_LOOPBACK_DATA);
245 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_GMAC == EFX_LOOPBACK_GMAC);
246 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XGMII == EFX_LOOPBACK_XGMII);
247 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XGXS == EFX_LOOPBACK_XGXS);
248 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XAUI == EFX_LOOPBACK_XAUI);
249 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_GMII == EFX_LOOPBACK_GMII);
250 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_SGMII == EFX_LOOPBACK_SGMII);
251 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XGBR == EFX_LOOPBACK_XGBR);
252 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XFI == EFX_LOOPBACK_XFI);
253 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XAUI_FAR == EFX_LOOPBACK_XAUI_FAR);
254 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_GMII_FAR == EFX_LOOPBACK_GMII_FAR);
255 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_SGMII_FAR == EFX_LOOPBACK_SGMII_FAR);
256 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_XFI_FAR == EFX_LOOPBACK_XFI_FAR);
257 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_GPHY == EFX_LOOPBACK_GPHY);
258 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_PHYXS == EFX_LOOPBACK_PHY_XS);
259 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_PCS == EFX_LOOPBACK_PCS);
260 	EFX_STATIC_ASSERT(MC_CMD_LOOPBACK_PMAPMD == EFX_LOOPBACK_PMA_PMD);
261 
262 	elsp->els_loopback = MCDI_OUT_DWORD(req, GET_LINK_OUT_LOOPBACK_MODE);
263 #endif	/* EFSYS_OPT_LOOPBACK */
264 
265 	elsp->els_mac_up = MCDI_OUT_DWORD(req, GET_LINK_OUT_MAC_FAULT) == 0;
266 
267 	return (0);
268 
269 fail2:
270 	EFSYS_PROBE(fail2);
271 fail1:
272 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
273 
274 	return (rc);
275 }
276 
277 	__checkReturn	efx_rc_t
278 ef10_phy_reconfigure(
279 	__in		efx_nic_t *enp)
280 {
281 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
282 	efx_port_t *epp = &(enp->en_port);
283 	efx_mcdi_req_t req;
284 	uint8_t payload[MAX(MC_CMD_SET_LINK_IN_LEN,
285 			    MC_CMD_SET_LINK_OUT_LEN)];
286 	uint32_t cap_mask;
287 	unsigned int led_mode;
288 	unsigned int speed;
289 	efx_rc_t rc;
290 
291 	if (~encp->enc_func_flags & EFX_NIC_FUNC_LINKCTRL)
292 		goto out;
293 
294 	(void) memset(payload, 0, sizeof (payload));
295 	req.emr_cmd = MC_CMD_SET_LINK;
296 	req.emr_in_buf = payload;
297 	req.emr_in_length = MC_CMD_SET_LINK_IN_LEN;
298 	req.emr_out_buf = payload;
299 	req.emr_out_length = MC_CMD_SET_LINK_OUT_LEN;
300 
301 	cap_mask = epp->ep_adv_cap_mask;
302 	MCDI_IN_POPULATE_DWORD_10(req, SET_LINK_IN_CAP,
303 		PHY_CAP_10HDX, (cap_mask >> EFX_PHY_CAP_10HDX) & 0x1,
304 		PHY_CAP_10FDX, (cap_mask >> EFX_PHY_CAP_10FDX) & 0x1,
305 		PHY_CAP_100HDX, (cap_mask >> EFX_PHY_CAP_100HDX) & 0x1,
306 		PHY_CAP_100FDX, (cap_mask >> EFX_PHY_CAP_100FDX) & 0x1,
307 		PHY_CAP_1000HDX, (cap_mask >> EFX_PHY_CAP_1000HDX) & 0x1,
308 		PHY_CAP_1000FDX, (cap_mask >> EFX_PHY_CAP_1000FDX) & 0x1,
309 		PHY_CAP_10000FDX, (cap_mask >> EFX_PHY_CAP_10000FDX) & 0x1,
310 		PHY_CAP_PAUSE, (cap_mask >> EFX_PHY_CAP_PAUSE) & 0x1,
311 		PHY_CAP_ASYM, (cap_mask >> EFX_PHY_CAP_ASYM) & 0x1,
312 		PHY_CAP_AN, (cap_mask >> EFX_PHY_CAP_AN) & 0x1);
313 	/* Too many fields for for POPULATE macros, so insert this afterwards */
314 	MCDI_IN_SET_DWORD_FIELD(req, SET_LINK_IN_CAP,
315 	    PHY_CAP_40000FDX, (cap_mask >> EFX_PHY_CAP_40000FDX) & 0x1);
316 
317 #if EFSYS_OPT_LOOPBACK
318 	MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_MODE,
319 		    epp->ep_loopback_type);
320 	switch (epp->ep_loopback_link_mode) {
321 	case EFX_LINK_100FDX:
322 		speed = 100;
323 		break;
324 	case EFX_LINK_1000FDX:
325 		speed = 1000;
326 		break;
327 	case EFX_LINK_10000FDX:
328 		speed = 10000;
329 		break;
330 	case EFX_LINK_40000FDX:
331 		speed = 40000;
332 		break;
333 	default:
334 		speed = 0;
335 	}
336 #else
337 	MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_MODE, MC_CMD_LOOPBACK_NONE);
338 	speed = 0;
339 #endif	/* EFSYS_OPT_LOOPBACK */
340 	MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_SPEED, speed);
341 
342 #if EFSYS_OPT_PHY_FLAGS
343 	MCDI_IN_SET_DWORD(req, SET_LINK_IN_FLAGS, epp->ep_phy_flags);
344 #else
345 	MCDI_IN_SET_DWORD(req, SET_LINK_IN_FLAGS, 0);
346 #endif	/* EFSYS_OPT_PHY_FLAGS */
347 
348 	efx_mcdi_execute(enp, &req);
349 
350 	if (req.emr_rc != 0) {
351 		rc = req.emr_rc;
352 		goto fail1;
353 	}
354 
355 	/* And set the blink mode */
356 	(void) memset(payload, 0, sizeof (payload));
357 	req.emr_cmd = MC_CMD_SET_ID_LED;
358 	req.emr_in_buf = payload;
359 	req.emr_in_length = MC_CMD_SET_ID_LED_IN_LEN;
360 	req.emr_out_buf = payload;
361 	req.emr_out_length = MC_CMD_SET_ID_LED_OUT_LEN;
362 
363 #if EFSYS_OPT_PHY_LED_CONTROL
364 	switch (epp->ep_phy_led_mode) {
365 	case EFX_PHY_LED_DEFAULT:
366 		led_mode = MC_CMD_LED_DEFAULT;
367 		break;
368 	case EFX_PHY_LED_OFF:
369 		led_mode = MC_CMD_LED_OFF;
370 		break;
371 	case EFX_PHY_LED_ON:
372 		led_mode = MC_CMD_LED_ON;
373 		break;
374 	default:
375 		EFSYS_ASSERT(0);
376 		led_mode = MC_CMD_LED_DEFAULT;
377 	}
378 
379 	MCDI_IN_SET_DWORD(req, SET_ID_LED_IN_STATE, led_mode);
380 #else
381 	MCDI_IN_SET_DWORD(req, SET_ID_LED_IN_STATE, MC_CMD_LED_DEFAULT);
382 #endif	/* EFSYS_OPT_PHY_LED_CONTROL */
383 
384 	efx_mcdi_execute(enp, &req);
385 
386 	if (req.emr_rc != 0) {
387 		rc = req.emr_rc;
388 		goto fail2;
389 	}
390 out:
391 	return (0);
392 
393 fail2:
394 	EFSYS_PROBE(fail2);
395 fail1:
396 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
397 
398 	return (rc);
399 }
400 
401 	__checkReturn	efx_rc_t
402 ef10_phy_verify(
403 	__in		efx_nic_t *enp)
404 {
405 	efx_mcdi_req_t req;
406 	uint8_t payload[MAX(MC_CMD_GET_PHY_STATE_IN_LEN,
407 			    MC_CMD_GET_PHY_STATE_OUT_LEN)];
408 	uint32_t state;
409 	efx_rc_t rc;
410 
411 	(void) memset(payload, 0, sizeof (payload));
412 	req.emr_cmd = MC_CMD_GET_PHY_STATE;
413 	req.emr_in_buf = payload;
414 	req.emr_in_length = MC_CMD_GET_PHY_STATE_IN_LEN;
415 	req.emr_out_buf = payload;
416 	req.emr_out_length = MC_CMD_GET_PHY_STATE_OUT_LEN;
417 
418 	efx_mcdi_execute(enp, &req);
419 
420 	if (req.emr_rc != 0) {
421 		rc = req.emr_rc;
422 		goto fail1;
423 	}
424 
425 	if (req.emr_out_length_used < MC_CMD_GET_PHY_STATE_OUT_LEN) {
426 		rc = EMSGSIZE;
427 		goto fail2;
428 	}
429 
430 	state = MCDI_OUT_DWORD(req, GET_PHY_STATE_OUT_STATE);
431 	if (state != MC_CMD_PHY_STATE_OK) {
432 		if (state != MC_CMD_PHY_STATE_ZOMBIE)
433 			EFSYS_PROBE1(mc_pcol_error, int, state);
434 		rc = ENOTACTIVE;
435 		goto fail3;
436 	}
437 
438 	return (0);
439 
440 fail3:
441 	EFSYS_PROBE(fail3);
442 fail2:
443 	EFSYS_PROBE(fail2);
444 fail1:
445 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
446 
447 	return (rc);
448 }
449 
450 	__checkReturn	efx_rc_t
451 ef10_phy_oui_get(
452 	__in		efx_nic_t *enp,
453 	__out		uint32_t *ouip)
454 {
455 	_NOTE(ARGUNUSED(enp, ouip))
456 
457 	return (ENOTSUP);
458 }
459 
460 #if EFSYS_OPT_PHY_STATS
461 
462 	__checkReturn				efx_rc_t
463 ef10_phy_stats_update(
464 	__in					efx_nic_t *enp,
465 	__in					efsys_mem_t *esmp,
466 	__inout_ecount(EFX_PHY_NSTATS)		uint32_t *stat)
467 {
468 	/* TBD: no stats support in firmware yet */
469 	_NOTE(ARGUNUSED(enp, esmp))
470 	memset(stat, 0, EFX_PHY_NSTATS * sizeof (*stat));
471 
472 	return (0);
473 }
474 
475 #endif	/* EFSYS_OPT_PHY_STATS */
476 
477 #endif	/* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
478