xref: /titanic_51/usr/src/uts/common/io/bge/bge_mii.c (revision 43a291055ab3951f6372241323fd4e2486098fff)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "sys/bge_impl.h"
30 
31 /*
32  * Bit test macros, returning boolean_t values
33  */
34 #define	BIS(w, b)	(((w) & (b)) ? B_TRUE : B_FALSE)
35 #define	BIC(w, b)	(((w) & (b)) ? B_FALSE : B_TRUE)
36 #define	UPORDOWN(x)	((x) ? "up" : "down")
37 
38 /*
39  * ========== Copper (PHY) support ==========
40  */
41 
42 #define	BGE_DBG		BGE_DBG_PHY	/* debug flag for this code	*/
43 
44 /*
45  * #defines:
46  *	BGE_COPPER_WIRESPEED controls whether the Broadcom WireSpeed(tm)
47  *	feature is enabled.  We need to recheck whether this can be
48  *	enabled; at one time it seemed to interact unpleasantly with the
49  *	loopback modes.
50  *
51  *	BGE_COPPER_IDLEOFF controls whether the (copper) PHY power is
52  *	turned off when the PHY is idled i.e. during driver suspend().
53  *	For now this is disabled because the chip doesn't seem to
54  *	resume cleanly if the PHY power is turned off.
55  */
56 #define	BGE_COPPER_WIRESPEED	B_TRUE
57 #define	BGE_COPPER_IDLEOFF	B_FALSE
58 
59 /*
60  * The arrays below can be indexed by the MODE bits from the Auxiliary
61  * Status register to determine the current speed/duplex settings.
62  */
63 static const int16_t bge_copper_link_speed[] = {
64 	0,				/* MII_AUX_STATUS_MODE_NONE	*/
65 	10,				/* MII_AUX_STATUS_MODE_10_H	*/
66 	10,				/* MII_AUX_STATUS_MODE_10_F	*/
67 	100,				/* MII_AUX_STATUS_MODE_100_H	*/
68 	0,				/* MII_AUX_STATUS_MODE_100_4	*/
69 	100,				/* MII_AUX_STATUS_MODE_100_F	*/
70 	1000,				/* MII_AUX_STATUS_MODE_1000_H	*/
71 	1000				/* MII_AUX_STATUS_MODE_1000_F	*/
72 };
73 
74 static const int8_t bge_copper_link_duplex[] = {
75 	LINK_DUPLEX_UNKNOWN,		/* MII_AUX_STATUS_MODE_NONE	*/
76 	LINK_DUPLEX_HALF,		/* MII_AUX_STATUS_MODE_10_H	*/
77 	LINK_DUPLEX_FULL,		/* MII_AUX_STATUS_MODE_10_F	*/
78 	LINK_DUPLEX_HALF,		/* MII_AUX_STATUS_MODE_100_H	*/
79 	LINK_DUPLEX_UNKNOWN,		/* MII_AUX_STATUS_MODE_100_4	*/
80 	LINK_DUPLEX_FULL,		/* MII_AUX_STATUS_MODE_100_F	*/
81 	LINK_DUPLEX_HALF,		/* MII_AUX_STATUS_MODE_1000_H	*/
82 	LINK_DUPLEX_FULL		/* MII_AUX_STATUS_MODE_1000_F	*/
83 };
84 
85 static const char * const bge_copper_link_text[] = {
86 	"down",				/* MII_AUX_STATUS_MODE_NONE	*/
87 	"up 10Mbps Half-Duplex",	/* MII_AUX_STATUS_MODE_10_H	*/
88 	"up 10Mbps Full-Duplex",	/* MII_AUX_STATUS_MODE_10_F	*/
89 	"up 100Mbps Half-Duplex",	/* MII_AUX_STATUS_MODE_100_H	*/
90 	"down",				/* MII_AUX_STATUS_MODE_100_4	*/
91 	"up 100Mbps Full-Duplex",	/* MII_AUX_STATUS_MODE_100_F	*/
92 	"up 1000Mbps Half-Duplex",	/* MII_AUX_STATUS_MODE_1000_H	*/
93 	"up 1000Mbps Full-Duplex"	/* MII_AUX_STATUS_MODE_1000_F	*/
94 };
95 
96 #if	BGE_DEBUGGING
97 
98 static void
99 bge_phydump(bge_t *bgep, uint16_t mii_status, uint16_t aux)
100 {
101 	uint16_t regs[32];
102 	int i;
103 
104 	ASSERT(mutex_owned(bgep->genlock));
105 
106 	for (i = 0; i < 32; ++i)
107 		switch (i) {
108 		default:
109 			regs[i] = bge_mii_get16(bgep, i);
110 			break;
111 
112 		case MII_STATUS:
113 			regs[i] = mii_status;
114 			break;
115 
116 		case MII_AUX_STATUS:
117 			regs[i] = aux;
118 			break;
119 
120 		case 0x0b: case 0x0c: case 0x0d: case 0x0e:
121 		case 0x15: case 0x16: case 0x17:
122 		case 0x1c:
123 		case 0x1f:
124 			/* reserved registers -- don't read these */
125 			regs[i] = 0;
126 			break;
127 		}
128 
129 	for (i = 0; i < 32; i += 8)
130 		BGE_DEBUG(("bge_phydump: "
131 				"0x%04x %04x %04x %04x %04x %04x %04x %04x",
132 			regs[i+0], regs[i+1], regs[i+2], regs[i+3],
133 			regs[i+4], regs[i+5], regs[i+6], regs[i+7]));
134 }
135 
136 #endif	/* BGE_DEBUGGING */
137 
138 /*
139  * Basic low-level function to probe for a PHY
140  *
141  * Returns TRUE if the PHY responds with valid data, FALSE otherwise
142  */
143 static boolean_t
144 bge_phy_probe(bge_t *bgep)
145 {
146 	uint16_t phy_status;
147 
148 	BGE_TRACE(("bge_phy_probe($%p)", (void *)bgep));
149 
150 	ASSERT(mutex_owned(bgep->genlock));
151 
152 	/*
153 	 * Read the MII_STATUS register twice, in
154 	 * order to clear any sticky bits (but they should
155 	 * have been cleared by the RESET, I think).
156 	 */
157 	phy_status = bge_mii_get16(bgep, MII_STATUS);
158 	phy_status = bge_mii_get16(bgep, MII_STATUS);
159 	BGE_DEBUG(("bge_phy_probe: status 0x%x", phy_status));
160 
161 	/*
162 	 * Now check the value read; it should have at least one bit set
163 	 * (for the device capabilities) and at least one clear (one of
164 	 * the error bits). So if we see all 0s or all 1s, there's a
165 	 * problem.  In particular, bge_mii_get16() returns all 1s if
166 	 * communications fails ...
167 	 */
168 	switch (phy_status) {
169 	case 0x0000:
170 	case 0xffff:
171 		return (B_FALSE);
172 
173 	default :
174 		return (B_TRUE);
175 	}
176 }
177 
178 /*
179  * Basic low-level function to reset the PHY.
180  * Doesn't incorporate any special-case workarounds.
181  *
182  * Returns TRUE on success, FALSE if the RESET bit doesn't clear
183  */
184 static boolean_t
185 bge_phy_reset(bge_t *bgep)
186 {
187 	uint16_t control;
188 	uint_t count;
189 
190 	BGE_TRACE(("bge_phy_reset($%p)", (void *)bgep));
191 
192 	ASSERT(mutex_owned(bgep->genlock));
193 
194 	/*
195 	 * Set the PHY RESET bit, then wait up to 5 ms for it to self-clear
196 	 */
197 	bge_mii_put16(bgep, MII_CONTROL, MII_CONTROL_RESET);
198 	for (count = 0; ++count < 1000; ) {
199 		drv_usecwait(5);
200 		control = bge_mii_get16(bgep, MII_CONTROL);
201 		if (BIC(control, MII_CONTROL_RESET))
202 			return (B_TRUE);
203 	}
204 
205 	BGE_DEBUG(("bge_phy_reset: FAILED, control now 0x%x", control));
206 
207 	return (B_FALSE);
208 }
209 
210 /*
211  * Basic low-level function to powerdown the PHY, if supported
212  * If powerdown support is compiled out, this function does nothing.
213  */
214 static void
215 bge_phy_powerdown(bge_t *bgep)
216 {
217 	BGE_TRACE(("bge_phy_powerdown"));
218 #if	BGE_COPPER_IDLEOFF
219 	bge_mii_put16(bgep, MII_CONTROL, MII_CONTROL_PWRDN);
220 #endif	/* BGE_COPPER_IDLEOFF */
221 }
222 
223 /*
224  * The following functions are based on sample code provided by
225  * Broadcom (20-June-2003), and implement workarounds said to be
226  * required on the early revisions of the BCM5703/4C.
227  *
228  * The registers and values used are mostly UNDOCUMENTED, and
229  * therefore don't have symbolic names ;-(
230  *
231  * Many of the comments are straight out of the Broadcom code:
232  * even where the code has been restructured, the original
233  * comments have been preserved in order to explain what these
234  * undocumented registers & values are all about ...
235  */
236 
237 static void
238 bge_phy_macro_wait(bge_t *bgep)
239 {
240 	uint_t count;
241 
242 	for (count = 100; --count; )
243 		if ((bge_mii_get16(bgep, 0x16) & 0x1000) == 0)
244 			break;
245 }
246 
247 /*
248  * PHY test data pattern:
249  *
250  * For 5703/04, each DFE TAP has 21-bits (low word 15, hi word 6)
251  * For 5705,    each DFE TAP has 19-bits (low word 15, hi word 4)
252  * For simplicity, we check only 19-bits, so we don't have to
253  * distinguish which chip it is.
254  * the LO word contains 15 bits, make sure pattern data is < 0x7fff
255  * the HI word contains  6 bits, make sure pattern data is < 0x003f
256  */
257 #define	N_CHANNELS	4
258 #define	N_TAPS		3
259 
260 static struct {
261 	uint16_t	lo;
262 	uint16_t	hi;
263 } tap_data[N_CHANNELS][N_TAPS] = {
264 	{
265 		{ 0x5555, 0x0005 },	/* ch0, TAP 0, LO/HI pattern */
266 		{ 0x2aaa, 0x000a },	/* ch0, TAP 1, LO/HI pattern */
267 		{ 0x3456, 0x0003 }	/* ch0, TAP 2, LO/HI pattern */
268 	},
269 	{
270 		{ 0x2aaa, 0x000a },	/* ch1, TAP 0, LO/HI pattern */
271 		{ 0x3333, 0x0003 },	/* ch1, TAP 1, LO/HI pattern */
272 		{ 0x789a, 0x0005 }	/* ch1, TAP 2, LO/HI pattern */
273 	},
274 	{
275 		{ 0x5a5a, 0x0005 },	/* ch2, TAP 0, LO/HI pattern */
276 		{ 0x2a6a, 0x000a },	/* ch2, TAP 1, LO/HI pattern */
277 		{ 0x1bcd, 0x0003 }	/* ch2, TAP 2, LO/HI pattern */
278 	},
279 	{
280 		{ 0x2a5a, 0x000a },	/* ch3, TAP 0, LO/HI pattern */
281 		{ 0x33c3, 0x0003 },	/* ch3, TAP 1, LO/HI pattern */
282 		{ 0x2ef1, 0x0005 }	/* ch3, TAP 2, LO/HI pattern */
283 	}
284 };
285 
286 /*
287  * Check whether the PHY has locked up after a RESET.
288  *
289  * Returns TRUE if it did, FALSE is it's OK ;-)
290  */
291 static boolean_t
292 bge_phy_locked_up(bge_t *bgep)
293 {
294 	uint16_t dataLo;
295 	uint16_t dataHi;
296 	uint_t chan;
297 	uint_t tap;
298 
299 	/*
300 	 * Check TAPs for all 4 channels, as soon as we see a lockup
301 	 * we'll stop checking.
302 	 */
303 	for (chan = 0; chan < N_CHANNELS; ++chan) {
304 		/* Select channel and set TAP index to 0 */
305 		bge_mii_put16(bgep, 0x17, (chan << 13) | 0x0200);
306 		/* Freeze filter again just to be safe */
307 		bge_mii_put16(bgep, 0x16, 0x0002);
308 
309 		/*
310 		 * Write fixed pattern to the RAM, 3 TAPs for
311 		 * each channel, each TAP have 2 WORDs (LO/HI)
312 		 */
313 		for (tap = 0; tap < N_TAPS; ++tap) {
314 			bge_mii_put16(bgep, 0x15, tap_data[chan][tap].lo);
315 			bge_mii_put16(bgep, 0x15, tap_data[chan][tap].hi);
316 		}
317 
318 		/*
319 		 * Active PHY's Macro operation to write DFE
320 		 * TAP from RAM, and wait for Macro to complete.
321 		 */
322 		bge_mii_put16(bgep, 0x16, 0x0202);
323 		bge_phy_macro_wait(bgep);
324 
325 		/*
326 		 * Done with write phase, now begin read phase.
327 		 */
328 
329 		/* Select channel and set TAP index to 0 */
330 		bge_mii_put16(bgep, 0x17, (chan << 13) | 0x0200);
331 
332 		/*
333 		 * Active PHY's Macro operation to load DFE
334 		 * TAP to RAM, and wait for Macro to complete
335 		 */
336 		bge_mii_put16(bgep, 0x16, 0x0082);
337 		bge_phy_macro_wait(bgep);
338 
339 		/* Enable "pre-fetch" */
340 		bge_mii_put16(bgep, 0x16, 0x0802);
341 		bge_phy_macro_wait(bgep);
342 
343 		/*
344 		 * Read back the TAP values.  3 TAPs for each
345 		 * channel, each TAP have 2 WORDs (LO/HI)
346 		 */
347 		for (tap = 0; tap < N_TAPS; ++tap) {
348 			/*
349 			 * Read Lo/Hi then wait for 'done' is faster.
350 			 * For DFE TAP, the HI word contains 6 bits,
351 			 * LO word contains 15 bits
352 			 */
353 			dataLo = bge_mii_get16(bgep, 0x15) & 0x7fff;
354 			dataHi = bge_mii_get16(bgep, 0x15) & 0x003f;
355 			bge_phy_macro_wait(bgep);
356 
357 			/*
358 			 * Check if what we wrote is what we read back.
359 			 * If failed, then the PHY is locked up, we need
360 			 * to do PHY reset again
361 			 */
362 			if (dataLo != tap_data[chan][tap].lo)
363 				return (B_TRUE);	/* wedged!	*/
364 
365 			if (dataHi != tap_data[chan][tap].hi)
366 				return (B_TRUE);	/* wedged!	*/
367 		}
368 	}
369 
370 	/*
371 	 * The PHY isn't locked up ;-)
372 	 */
373 	return (B_FALSE);
374 }
375 
376 /*
377  * Special-case code to reset the PHY on the 5702/5703/5704C/5705/5782.
378  * Tries up to 5 times to recover from failure to reset or PHY lockup.
379  *
380  * Returns TRUE on success, FALSE if there's an unrecoverable problem
381  */
382 static boolean_t
383 bge_phy_reset_and_check(bge_t *bgep)
384 {
385 	boolean_t reset_success;
386 	boolean_t phy_locked;
387 	uint16_t extctrl;
388 	uint_t retries;
389 
390 	for (retries = 0; retries < 5; ++retries) {
391 		/* Issue a phy reset, and wait for reset to complete */
392 		/* Assuming reset is successful first */
393 		reset_success = bge_phy_reset(bgep);
394 
395 		/*
396 		 * Now go check the DFE TAPs to see if locked up, but
397 		 * first, we need to set up PHY so we can read DFE
398 		 * TAPs.
399 		 */
400 
401 		/*
402 		 * Disable Transmitter and Interrupt, while we play
403 		 * with the PHY registers, so the link partner won't
404 		 * see any strange data and the Driver won't see any
405 		 * interrupts.
406 		 */
407 		extctrl = bge_mii_get16(bgep, 0x10);
408 		bge_mii_put16(bgep, 0x10, extctrl | 0x3000);
409 
410 		/* Setup Full-Duplex, 1000 mbps */
411 		bge_mii_put16(bgep, 0x0, 0x0140);
412 
413 		/* Set to Master mode */
414 		bge_mii_put16(bgep, 0x9, 0x1800);
415 
416 		/* Enable SM_DSP_CLOCK & 6dB */
417 		bge_mii_put16(bgep, 0x18, 0x0c00);	/* "the ADC fix" */
418 
419 		/* Work-arounds */
420 		bge_mii_put16(bgep, 0x17, 0x201f);
421 		bge_mii_put16(bgep, 0x15, 0x2aaa);
422 
423 		/* More workarounds */
424 		bge_mii_put16(bgep, 0x17, 0x000a);
425 		bge_mii_put16(bgep, 0x15, 0x0323);	/* "the Gamma fix" */
426 
427 		/* Blocks the PHY control access */
428 		bge_mii_put16(bgep, 0x17, 0x8005);
429 		bge_mii_put16(bgep, 0x15, 0x0800);
430 
431 		/* Test whether PHY locked up ;-( */
432 		phy_locked = bge_phy_locked_up(bgep);
433 		if (reset_success && !phy_locked)
434 			break;
435 
436 		/*
437 		 * Some problem here ... log it & retry
438 		 */
439 		if (!reset_success)
440 			BGE_REPORT((bgep, "PHY didn't reset!"));
441 		if (phy_locked)
442 			BGE_REPORT((bgep, "PHY locked up!"));
443 	}
444 
445 	/* Remove block phy control */
446 	bge_mii_put16(bgep, 0x17, 0x8005);
447 	bge_mii_put16(bgep, 0x15, 0x0000);
448 
449 	/* Unfreeze DFE TAP filter for all channels */
450 	bge_mii_put16(bgep, 0x17, 0x8200);
451 	bge_mii_put16(bgep, 0x16, 0x0000);
452 
453 	/* Restore PHY back to operating state */
454 	bge_mii_put16(bgep, 0x18, 0x0400);
455 
456 	/* Enable transmitter and interrupt */
457 	extctrl = bge_mii_get16(bgep, 0x10);
458 	bge_mii_put16(bgep, 0x10, extctrl & ~0x3000);
459 
460 	return (reset_success && !phy_locked);
461 }
462 
463 static void
464 bge_phy_tweak_gmii(bge_t *bgep)
465 {
466 	/* Tweak GMII timing */
467 	bge_mii_put16(bgep, 0x1c, 0x8d68);
468 	bge_mii_put16(bgep, 0x1c, 0x8d68);
469 }
470 
471 /*
472  * End of Broadcom-derived workaround code				*
473  */
474 
475 static void
476 bge_restart_copper(bge_t *bgep, boolean_t powerdown)
477 {
478 	uint16_t phy_status;
479 	boolean_t reset_ok;
480 
481 	BGE_TRACE(("bge_restart_copper($%p, %d)", (void *)bgep, powerdown));
482 
483 	ASSERT(mutex_owned(bgep->genlock));
484 
485 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
486 	default:
487 		/*
488 		 * Shouldn't happen; it means we don't recognise this chip.
489 		 * It's probably a new one, so we'll try our best anyway ...
490 		 */
491 	case MHCR_CHIP_ASIC_REV_5703:
492 	case MHCR_CHIP_ASIC_REV_5704:
493 	case MHCR_CHIP_ASIC_REV_5705:
494 	case MHCR_CHIP_ASIC_REV_5721_5751:
495 	case MHCR_CHIP_ASIC_REV_5714:
496 		reset_ok = bge_phy_reset_and_check(bgep);
497 		break;
498 
499 	case MHCR_CHIP_ASIC_REV_5700:
500 	case MHCR_CHIP_ASIC_REV_5701:
501 		/*
502 		 * Just a plain reset; the "check" code breaks these chips
503 		 */
504 		reset_ok = bge_phy_reset(bgep);
505 		break;
506 	}
507 	if (!reset_ok)
508 		bge_problem(bgep, "PHY failed to reset correctly");
509 
510 	/*
511 	 * Step 5: disable WOL (not required after RESET)
512 	 *
513 	 * Step 6: refer to errata
514 	 */
515 	switch (bgep->chipid.asic_rev) {
516 	default:
517 		break;
518 
519 	case MHCR_CHIP_REV_5704_A0:
520 		bge_phy_tweak_gmii(bgep);
521 		break;
522 	}
523 
524 	/*
525 	 * Step 7: read the MII_INTR_STATUS register twice,
526 	 * in order to clear any sticky bits (but they should
527 	 * have been cleared by the RESET, I think), and we're
528 	 * not using PHY interrupts anyway.
529 	 *
530 	 * Step 8: enable the PHY to interrupt on link status
531 	 * change (not required)
532 	 *
533 	 * Step 9: configure PHY LED Mode - not applicable?
534 	 *
535 	 * Step 10: read the MII_STATUS register twice, in
536 	 * order to clear any sticky bits (but they should
537 	 * have been cleared by the RESET, I think).
538 	 */
539 	phy_status = bge_mii_get16(bgep, MII_STATUS);
540 	phy_status = bge_mii_get16(bgep, MII_STATUS);
541 	BGE_DEBUG(("bge_restart_copper: status 0x%x", phy_status));
542 
543 	/*
544 	 * Finally, shut down the PHY, if required
545 	 */
546 	if (powerdown)
547 		bge_phy_powerdown(bgep);
548 }
549 
550 /*
551  * Synchronise the (copper) PHY's speed/duplex/autonegotiation capabilities
552  * and advertisements with the required settings as specified by the various
553  * param_* variables that can be poked via the NDD interface.
554  *
555  * We always reset the PHY and reprogram *all* the relevant registers,
556  * not just those changed.  This should cause the link to go down, and then
557  * back up again once the link is stable and autonegotiation (if enabled)
558  * is complete.  We should get a link state change interrupt somewhere along
559  * the way ...
560  *
561  * NOTE: <genlock> must already be held by the caller
562  */
563 static void
564 bge_update_copper(bge_t *bgep)
565 {
566 	boolean_t adv_autoneg;
567 	boolean_t adv_pause;
568 	boolean_t adv_asym_pause;
569 	boolean_t adv_1000fdx;
570 	boolean_t adv_1000hdx;
571 	boolean_t adv_100fdx;
572 	boolean_t adv_100hdx;
573 	boolean_t adv_10fdx;
574 	boolean_t adv_10hdx;
575 
576 	uint16_t control;
577 	uint16_t gigctrl;
578 	uint16_t auxctrl;
579 	uint16_t anar;
580 
581 	BGE_TRACE(("bge_update_copper($%p)", (void *)bgep));
582 
583 	ASSERT(mutex_owned(bgep->genlock));
584 
585 	BGE_DEBUG(("bge_update_copper: autoneg %d "
586 			"pause %d asym_pause %d "
587 			"1000fdx %d 1000hdx %d "
588 			"100fdx %d 100hdx %d "
589 			"10fdx %d 10hdx %d ",
590 		bgep->param_adv_autoneg,
591 		bgep->param_adv_pause, bgep->param_adv_asym_pause,
592 		bgep->param_adv_1000fdx, bgep->param_adv_1000hdx,
593 		bgep->param_adv_100fdx, bgep->param_adv_100hdx,
594 		bgep->param_adv_10fdx, bgep->param_adv_10hdx));
595 
596 	control = gigctrl = auxctrl = anar = 0;
597 
598 	/*
599 	 * PHY settings are normally based on the param_* variables,
600 	 * but if any loopback mode is in effect, that takes precedence.
601 	 *
602 	 * BGE supports MAC-internal loopback, PHY-internal loopback,
603 	 * and External loopback at a variety of speeds (with a special
604 	 * cable).  In all cases, autoneg is turned OFF, full-duplex
605 	 * is turned ON, and the speed/mastership is forced.
606 	 */
607 	switch (bgep->param_loop_mode) {
608 	case BGE_LOOP_NONE:
609 	default:
610 		adv_autoneg = bgep->param_adv_autoneg;
611 		adv_pause = bgep->param_adv_pause;
612 		adv_asym_pause = bgep->param_adv_asym_pause;
613 		adv_1000fdx = bgep->param_adv_1000fdx;
614 		adv_1000hdx = bgep->param_adv_1000hdx;
615 		adv_100fdx = bgep->param_adv_100fdx;
616 		adv_100hdx = bgep->param_adv_100hdx;
617 		adv_10fdx = bgep->param_adv_10fdx;
618 		adv_10hdx = bgep->param_adv_10hdx;
619 		break;
620 
621 	case BGE_LOOP_EXTERNAL_1000:
622 	case BGE_LOOP_EXTERNAL_100:
623 	case BGE_LOOP_EXTERNAL_10:
624 	case BGE_LOOP_INTERNAL_PHY:
625 	case BGE_LOOP_INTERNAL_MAC:
626 		adv_autoneg = adv_pause = adv_asym_pause = B_FALSE;
627 		adv_1000fdx = adv_100fdx = adv_10fdx = B_FALSE;
628 		adv_1000hdx = adv_100hdx = adv_10hdx = B_FALSE;
629 		bgep->param_link_duplex = LINK_DUPLEX_FULL;
630 
631 		switch (bgep->param_loop_mode) {
632 		case BGE_LOOP_EXTERNAL_1000:
633 			bgep->param_link_speed = 1000;
634 			adv_1000fdx = B_TRUE;
635 			auxctrl = MII_AUX_CTRL_NORM_EXT_LOOPBACK;
636 			gigctrl |= MII_1000BT_CTL_MASTER_CFG;
637 			gigctrl |= MII_1000BT_CTL_MASTER_SEL;
638 			break;
639 
640 		case BGE_LOOP_EXTERNAL_100:
641 			bgep->param_link_speed = 100;
642 			adv_100fdx = B_TRUE;
643 			auxctrl = MII_AUX_CTRL_NORM_EXT_LOOPBACK;
644 			break;
645 
646 		case BGE_LOOP_EXTERNAL_10:
647 			bgep->param_link_speed = 10;
648 			adv_10fdx = B_TRUE;
649 			auxctrl = MII_AUX_CTRL_NORM_EXT_LOOPBACK;
650 			break;
651 
652 		case BGE_LOOP_INTERNAL_PHY:
653 			bgep->param_link_speed = 1000;
654 			adv_1000fdx = B_TRUE;
655 			control = MII_CONTROL_LOOPBACK;
656 			break;
657 
658 		case BGE_LOOP_INTERNAL_MAC:
659 			bgep->param_link_speed = 1000;
660 			adv_1000fdx = B_TRUE;
661 			break;
662 		}
663 	}
664 
665 	BGE_DEBUG(("bge_update_copper: autoneg %d "
666 			"pause %d asym_pause %d "
667 			"1000fdx %d 1000hdx %d "
668 			"100fdx %d 100hdx %d "
669 			"10fdx %d 10hdx %d ",
670 		adv_autoneg,
671 		adv_pause, adv_asym_pause,
672 		adv_1000fdx, adv_1000hdx,
673 		adv_100fdx, adv_100hdx,
674 		adv_10fdx, adv_10hdx));
675 
676 	/*
677 	 * We should have at least one technology capability set;
678 	 * if not, we select a default of 1000Mb/s full-duplex
679 	 */
680 	if (!adv_1000fdx && !adv_100fdx && !adv_10fdx &&
681 	    !adv_1000hdx && !adv_100hdx && !adv_10hdx)
682 		adv_1000fdx = B_TRUE;
683 
684 	/*
685 	 * Now transform the adv_* variables into the proper settings
686 	 * of the PHY registers ...
687 	 *
688 	 * If autonegotiation is (now) enabled, we want to trigger
689 	 * a new autonegotiation cycle once the PHY has been
690 	 * programmed with the capabilities to be advertised.
691 	 */
692 	if (adv_autoneg)
693 		control |= MII_CONTROL_ANE|MII_CONTROL_RSAN;
694 
695 	if (adv_1000fdx)
696 		control |= MII_CONTROL_1000MB|MII_CONTROL_FDUPLEX;
697 	else if (adv_1000hdx)
698 		control |= MII_CONTROL_1000MB;
699 	else if (adv_100fdx)
700 		control |= MII_CONTROL_100MB|MII_CONTROL_FDUPLEX;
701 	else if (adv_100hdx)
702 		control |= MII_CONTROL_100MB;
703 	else if (adv_10fdx)
704 		control |= MII_CONTROL_FDUPLEX;
705 	else if (adv_10hdx)
706 		control |= 0;
707 	else
708 		{ _NOTE(EMPTY); }	/* Can't get here anyway ...	*/
709 
710 	if (adv_1000fdx)
711 		gigctrl |= MII_1000BT_CTL_ADV_FDX;
712 	if (adv_1000hdx)
713 		gigctrl |= MII_1000BT_CTL_ADV_HDX;
714 
715 	if (adv_100fdx)
716 		anar |= MII_ABILITY_100BASE_TX_FD;
717 	if (adv_100hdx)
718 		anar |= MII_ABILITY_100BASE_TX;
719 	if (adv_10fdx)
720 		anar |= MII_ABILITY_10BASE_T_FD;
721 	if (adv_10hdx)
722 		anar |= MII_ABILITY_10BASE_T;
723 
724 	if (adv_pause)
725 		anar |= MII_ABILITY_PAUSE;
726 	if (adv_asym_pause)
727 		anar |= MII_ABILITY_ASYM_PAUSE;
728 
729 	/*
730 	 * Munge in any other fixed bits we require ...
731 	 */
732 	anar |= MII_AN_SELECTOR_8023;
733 	auxctrl |= MII_AUX_CTRL_NORM_TX_MODE;
734 	auxctrl |= MII_AUX_CTRL_NORMAL;
735 
736 	/*
737 	 * Restart the PHY and write the new values.  Note the
738 	 * time, so that we can say whether subsequent link state
739 	 * changes can be attributed to our reprogramming the PHY
740 	 */
741 	bgep->phys_write_time = gethrtime();
742 	(*bgep->physops->phys_restart)(bgep, B_FALSE);
743 	bge_mii_put16(bgep, MII_AN_ADVERT, anar);
744 	bge_mii_put16(bgep, MII_CONTROL, control);
745 	bge_mii_put16(bgep, MII_AUX_CONTROL, auxctrl);
746 	bge_mii_put16(bgep, MII_1000BASE_T_CONTROL, gigctrl);
747 
748 	BGE_DEBUG(("bge_update_copper: anar <- 0x%x", anar));
749 	BGE_DEBUG(("bge_update_copper: control <- 0x%x", control));
750 	BGE_DEBUG(("bge_update_copper: auxctrl <- 0x%x", auxctrl));
751 	BGE_DEBUG(("bge_update_copper: gigctrl <- 0x%x", gigctrl));
752 
753 #if	BGE_COPPER_WIRESPEED
754 	/*
755 	 * Enable the 'wire-speed' feature, if the chip supports it
756 	 * and we haven't got (any) loopback mode selected.
757 	 */
758 	switch (bgep->chipid.device) {
759 	case DEVICE_ID_5700:
760 	case DEVICE_ID_5700x:
761 	case DEVICE_ID_5705C:
762 	case DEVICE_ID_5782:
763 		/*
764 		 * These chips are known or assumed not to support it
765 		 */
766 		break;
767 
768 	default:
769 		/*
770 		 * All other Broadcom chips are expected to support it.
771 		 */
772 		if (bgep->param_loop_mode == BGE_LOOP_NONE)
773 			bge_mii_put16(bgep, MII_AUX_CONTROL,
774 					MII_AUX_CTRL_MISC_WRITE_ENABLE |
775 					MII_AUX_CTRL_MISC_WIRE_SPEED |
776 					MII_AUX_CTRL_MISC);
777 		break;
778 	}
779 #endif	/* BGE_COPPER_WIRESPEED */
780 }
781 
782 static boolean_t
783 bge_check_copper(bge_t *bgep, boolean_t recheck)
784 {
785 	uint32_t emac_status;
786 	uint16_t mii_status;
787 	uint16_t aux;
788 	uint_t mode;
789 	boolean_t linkup;
790 
791 	/*
792 	 * Step 10: read the status from the PHY (which is self-clearing
793 	 * on read!); also read & clear the main (Ethernet) MAC status
794 	 * (the relevant bits of this are write-one-to-clear).
795 	 */
796 	mii_status = bge_mii_get16(bgep, MII_STATUS);
797 	emac_status = bge_reg_get32(bgep, ETHERNET_MAC_STATUS_REG);
798 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, emac_status);
799 
800 	BGE_DEBUG(("bge_check_copper: link %d/%s, MII status 0x%x "
801 			"(was 0x%x), Ethernet MAC status 0x%x",
802 		bgep->link_state, UPORDOWN(bgep->param_link_up), mii_status,
803 		bgep->phy_gen_status, emac_status));
804 
805 	/*
806 	 * If the PHY status hasn't changed since last we looked, and
807 	 * we not forcing a recheck (i.e. the link state was already
808 	 * known), there's nothing to do.
809 	 */
810 	if (mii_status == bgep->phy_gen_status && !recheck)
811 		return (B_FALSE);
812 
813 	do {
814 		/*
815 		 * If the PHY status changed, record the time
816 		 */
817 		if (mii_status != bgep->phy_gen_status)
818 			bgep->phys_event_time = gethrtime();
819 
820 		/*
821 		 * Step 11: read AUX STATUS register to find speed/duplex
822 		 */
823 		aux = bge_mii_get16(bgep, MII_AUX_STATUS);
824 		BGE_CDB(bge_phydump, (bgep, mii_status, aux));
825 
826 		/*
827 		 * We will only consider the link UP if all the readings
828 		 * are consistent and give meaningful results ...
829 		 */
830 		mode = aux & MII_AUX_STATUS_MODE_MASK;
831 		mode >>= MII_AUX_STATUS_MODE_SHIFT;
832 		linkup = bge_copper_link_speed[mode] > 0;
833 		linkup &= bge_copper_link_duplex[mode] != LINK_DUPLEX_UNKNOWN;
834 		linkup &= BIS(aux, MII_AUX_STATUS_LINKUP);
835 		linkup &= BIS(mii_status, MII_STATUS_LINKUP);
836 
837 		BGE_DEBUG(("bge_check_copper: MII status 0x%x aux 0x%x "
838 				"=> mode %d (%s)",
839 			mii_status, aux,
840 			mode, UPORDOWN(linkup)));
841 
842 		/*
843 		 * Record current register values, then reread status
844 		 * register & loop until it stabilises ...
845 		 */
846 		bgep->phy_aux_status = aux;
847 		bgep->phy_gen_status = mii_status;
848 		mii_status = bge_mii_get16(bgep, MII_STATUS);
849 	} while (mii_status != bgep->phy_gen_status);
850 
851 	/*
852 	 * Assume very little ...
853 	 */
854 	bgep->param_lp_autoneg = B_FALSE;
855 	bgep->param_lp_1000fdx = B_FALSE;
856 	bgep->param_lp_1000hdx = B_FALSE;
857 	bgep->param_lp_100fdx = B_FALSE;
858 	bgep->param_lp_100hdx = B_FALSE;
859 	bgep->param_lp_10fdx = B_FALSE;
860 	bgep->param_lp_10hdx = B_FALSE;
861 	bgep->param_lp_pause = B_FALSE;
862 	bgep->param_lp_asym_pause = B_FALSE;
863 	bgep->param_link_autoneg = B_FALSE;
864 	bgep->param_link_tx_pause = B_FALSE;
865 	if (bgep->param_adv_autoneg)
866 		bgep->param_link_rx_pause = B_FALSE;
867 	else
868 		bgep->param_link_rx_pause = bgep->param_adv_pause;
869 
870 	/*
871 	 * Discover all the link partner's abilities.
872 	 * These are scattered through various registters ...
873 	 */
874 	if (BIS(aux, MII_AUX_STATUS_LP_ANEG_ABLE)) {
875 		bgep->param_lp_autoneg = B_TRUE;
876 		bgep->param_link_autoneg = B_TRUE;
877 		bgep->param_link_tx_pause = BIS(aux, MII_AUX_STATUS_TX_PAUSE);
878 		bgep->param_link_rx_pause = BIS(aux, MII_AUX_STATUS_RX_PAUSE);
879 
880 		aux = bge_mii_get16(bgep, MII_1000BASE_T_STATUS);
881 		bgep->param_lp_1000fdx = BIS(aux, MII_1000BT_STAT_LP_FDX_CAP);
882 		bgep->param_lp_1000hdx = BIS(aux, MII_1000BT_STAT_LP_HDX_CAP);
883 
884 		aux = bge_mii_get16(bgep, MII_AN_LPABLE);
885 		bgep->param_lp_100fdx = BIS(aux, MII_ABILITY_100BASE_TX_FD);
886 		bgep->param_lp_100hdx = BIS(aux, MII_ABILITY_100BASE_TX);
887 		bgep->param_lp_10fdx = BIS(aux, MII_ABILITY_10BASE_T_FD);
888 		bgep->param_lp_10hdx = BIS(aux, MII_ABILITY_10BASE_T);
889 		bgep->param_lp_pause = BIS(aux, MII_ABILITY_PAUSE);
890 		bgep->param_lp_asym_pause = BIS(aux, MII_ABILITY_ASYM_PAUSE);
891 	}
892 
893 	/*
894 	 * Step 12: update ndd-visible state parameters, BUT!
895 	 * we don't transfer the new state to <link_state> just yet;
896 	 * instead we mark the <link_state> as UNKNOWN, and our caller
897 	 * will resolve it once the status has stopped changing and
898 	 * been stable for several seconds.
899 	 */
900 	BGE_DEBUG(("bge_check_copper: link was %s speed %d duplex %d",
901 		UPORDOWN(bgep->param_link_up),
902 		bgep->param_link_speed,
903 		bgep->param_link_duplex));
904 
905 	if (!linkup)
906 		mode = MII_AUX_STATUS_MODE_NONE;
907 	bgep->param_link_up = linkup;
908 	bgep->param_link_speed = bge_copper_link_speed[mode];
909 	bgep->param_link_duplex = bge_copper_link_duplex[mode];
910 	bgep->link_mode_msg = bge_copper_link_text[mode];
911 	bgep->link_state = LINK_STATE_UNKNOWN;
912 
913 	BGE_DEBUG(("bge_check_copper: link now %s speed %d duplex %d",
914 		UPORDOWN(bgep->param_link_up),
915 		bgep->param_link_speed,
916 		bgep->param_link_duplex));
917 
918 	return (B_TRUE);
919 }
920 
921 static const phys_ops_t copper_ops = {
922 	bge_restart_copper,
923 	bge_update_copper,
924 	bge_check_copper
925 };
926 
927 
928 /*
929  * ========== SerDes support ==========
930  */
931 
932 #undef	BGE_DBG
933 #define	BGE_DBG		BGE_DBG_SERDES	/* debug flag for this code	*/
934 
935 /*
936  * Reinitialise the SerDes interface.  Note that it normally powers
937  * up in the disabled state, so we need to explicitly activate it.
938  */
939 static void
940 bge_restart_serdes(bge_t *bgep, boolean_t powerdown)
941 {
942 	uint32_t macmode;
943 
944 	BGE_TRACE(("bge_restart_serdes($%p, %d)", (void *)bgep, powerdown));
945 
946 	ASSERT(mutex_owned(bgep->genlock));
947 
948 	/*
949 	 * Ensure that the main Ethernet MAC mode register is programmed
950 	 * appropriately for the SerDes interface ...
951 	 */
952 	macmode = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
953 	macmode &= ~ETHERNET_MODE_LINK_POLARITY;
954 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
955 	macmode |= ETHERNET_MODE_PORTMODE_TBI;
956 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
957 
958 	/*
959 	 * Ensure that loopback is OFF and comma detection is enabled.  Then
960 	 * disable the SerDes output (the first time through, it may/will
961 	 * already be disabled).  If we're shutting down, leave it disabled.
962 	 */
963 	bge_reg_clr32(bgep, SERDES_CONTROL_REG, SERDES_CONTROL_TBI_LOOPBACK);
964 	bge_reg_set32(bgep, SERDES_CONTROL_REG, SERDES_CONTROL_COMMA_DETECT);
965 	bge_reg_set32(bgep, SERDES_CONTROL_REG, SERDES_CONTROL_TX_DISABLE);
966 	if (powerdown)
967 		return;
968 
969 	/*
970 	 * Otherwise, pause, (re-)enable the SerDes output, and send
971 	 * all-zero config words in order to force autoneg restart.
972 	 * Invalidate the saved "link partners received configs", as
973 	 * we're starting over ...
974 	 */
975 	drv_usecwait(10000);
976 	bge_reg_clr32(bgep, SERDES_CONTROL_REG, SERDES_CONTROL_TX_DISABLE);
977 	bge_reg_put32(bgep, TX_1000BASEX_AUTONEG_REG, 0);
978 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG, ETHERNET_MODE_SEND_CFGS);
979 	drv_usecwait(10);
980 	bge_reg_clr32(bgep, ETHERNET_MAC_MODE_REG, ETHERNET_MODE_SEND_CFGS);
981 	bgep->serdes_lpadv = AUTONEG_CODE_FAULT_ANEG_ERR;
982 	bgep->serdes_status = ~0U;
983 }
984 
985 /*
986  * Synchronise the SerDes speed/duplex/autonegotiation capabilities and
987  * advertisements with the required settings as specified by the various
988  * param_* variables that can be poked via the NDD interface.
989  *
990  * We always reinitalise the SerDes; this should cause the link to go down,
991  * and then back up again once the link is stable and autonegotiation
992  * (if enabled) is complete.  We should get a link state change interrupt
993  * somewhere along the way ...
994  *
995  * NOTE: SerDes only supports 1000FDX/HDX (with or without pause) so the
996  * param_* variables relating to lower speeds are ignored.
997  *
998  * NOTE: <genlock> must already be held by the caller
999  */
1000 static void
1001 bge_update_serdes(bge_t *bgep)
1002 {
1003 	boolean_t adv_autoneg;
1004 	boolean_t adv_pause;
1005 	boolean_t adv_asym_pause;
1006 	boolean_t adv_1000fdx;
1007 	boolean_t adv_1000hdx;
1008 
1009 	uint32_t serdes;
1010 	uint32_t advert;
1011 
1012 	BGE_TRACE(("bge_update_serdes($%p)", (void *)bgep));
1013 
1014 	ASSERT(mutex_owned(bgep->genlock));
1015 
1016 	BGE_DEBUG(("bge_update_serdes: autoneg %d "
1017 			"pause %d asym_pause %d "
1018 			"1000fdx %d 1000hdx %d "
1019 			"100fdx %d 100hdx %d "
1020 			"10fdx %d 10hdx %d ",
1021 		bgep->param_adv_autoneg,
1022 		bgep->param_adv_pause, bgep->param_adv_asym_pause,
1023 		bgep->param_adv_1000fdx, bgep->param_adv_1000hdx,
1024 		bgep->param_adv_100fdx, bgep->param_adv_100hdx,
1025 		bgep->param_adv_10fdx, bgep->param_adv_10hdx));
1026 
1027 	serdes = advert = 0;
1028 
1029 	/*
1030 	 * SerDes settings are normally based on the param_* variables,
1031 	 * but if any loopback mode is in effect, that takes precedence.
1032 	 *
1033 	 * BGE supports MAC-internal loopback, PHY-internal loopback,
1034 	 * and External loopback at a variety of speeds (with a special
1035 	 * cable).  In all cases, autoneg is turned OFF, full-duplex
1036 	 * is turned ON, and the speed/mastership is forced.
1037 	 *
1038 	 * Note: for the SerDes interface, "PHY" internal loopback is
1039 	 * interpreted as SerDes internal loopback, and all external
1040 	 * loopback modes are treated equivalently, as 1Gb/external.
1041 	 */
1042 	switch (bgep->param_loop_mode) {
1043 	case BGE_LOOP_NONE:
1044 	default:
1045 		adv_autoneg = bgep->param_adv_autoneg;
1046 		adv_pause = bgep->param_adv_pause;
1047 		adv_asym_pause = bgep->param_adv_asym_pause;
1048 		adv_1000fdx = bgep->param_adv_1000fdx;
1049 		adv_1000hdx = bgep->param_adv_1000hdx;
1050 		break;
1051 
1052 	case BGE_LOOP_INTERNAL_PHY:
1053 		serdes |= SERDES_CONTROL_TBI_LOOPBACK;
1054 		/* FALLTHRU */
1055 	case BGE_LOOP_INTERNAL_MAC:
1056 	case BGE_LOOP_EXTERNAL_1000:
1057 	case BGE_LOOP_EXTERNAL_100:
1058 	case BGE_LOOP_EXTERNAL_10:
1059 		adv_autoneg = adv_pause = adv_asym_pause = B_FALSE;
1060 		adv_1000fdx = B_TRUE;
1061 		adv_1000hdx = B_FALSE;
1062 		break;
1063 	}
1064 
1065 	BGE_DEBUG(("bge_update_serdes: autoneg %d "
1066 			"pause %d asym_pause %d "
1067 			"1000fdx %d 1000hdx %d ",
1068 		adv_autoneg,
1069 		adv_pause, adv_asym_pause,
1070 		adv_1000fdx, adv_1000hdx));
1071 
1072 	/*
1073 	 * We should have at least one gigabit technology capability
1074 	 * set; if not, we select a default of 1000Mb/s full-duplex
1075 	 */
1076 	if (!adv_1000fdx && !adv_1000hdx)
1077 		adv_1000fdx = B_TRUE;
1078 
1079 	/*
1080 	 * Now transform the adv_* variables into the proper settings
1081 	 * of the SerDes registers ...
1082 	 *
1083 	 * If autonegotiation is (now) not enabled, pretend it's been
1084 	 * done and failed ...
1085 	 */
1086 	if (!adv_autoneg)
1087 		advert |= AUTONEG_CODE_FAULT_ANEG_ERR;
1088 
1089 	if (adv_1000fdx) {
1090 		advert |= AUTONEG_CODE_FULL_DUPLEX;
1091 		bgep->param_adv_1000fdx = adv_1000fdx;
1092 		bgep->param_link_duplex = LINK_DUPLEX_FULL;
1093 		bgep->param_link_speed = 1000;
1094 	}
1095 	if (adv_1000hdx) {
1096 		advert |= AUTONEG_CODE_HALF_DUPLEX;
1097 		bgep->param_adv_1000hdx = adv_1000hdx;
1098 		bgep->param_link_duplex = LINK_DUPLEX_HALF;
1099 		bgep->param_link_speed = 1000;
1100 	}
1101 
1102 	if (adv_pause)
1103 		advert |= AUTONEG_CODE_PAUSE;
1104 	if (adv_asym_pause)
1105 		advert |= AUTONEG_CODE_ASYM_PAUSE;
1106 
1107 	/*
1108 	 * Restart the SerDes and write the new values.  Note the
1109 	 * time, so that we can say whether subsequent link state
1110 	 * changes can be attributed to our reprogramming the SerDes
1111 	 */
1112 	bgep->serdes_advert = advert;
1113 	bgep->phys_write_time = gethrtime();
1114 	bge_restart_serdes(bgep, B_FALSE);
1115 	bge_reg_set32(bgep, SERDES_CONTROL_REG, serdes);
1116 
1117 	BGE_DEBUG(("bge_update_serdes: serdes |= 0x%x, advert 0x%x",
1118 		serdes, advert));
1119 }
1120 
1121 /*
1122  * Bare-minimum autoneg protocol
1123  *
1124  * This code is only called when the link is up and we're receiving config
1125  * words, which implies that the link partner wants to autonegotiate
1126  * (otherwise, we wouldn't see configs and wouldn't reach this code).
1127  */
1128 static void
1129 bge_autoneg_serdes(bge_t *bgep)
1130 {
1131 	boolean_t ack;
1132 
1133 	bgep->serdes_lpadv = bge_reg_get32(bgep, RX_1000BASEX_AUTONEG_REG);
1134 	ack = BIS(bgep->serdes_lpadv, AUTONEG_CODE_ACKNOWLEDGE);
1135 
1136 	if (!ack) {
1137 		/*
1138 		 * Phase 1: after SerDes reset, we send a few zero configs
1139 		 * but then stop.  Here the partner is sending configs, but
1140 		 * not ACKing ours; we assume that's 'cos we're not sending
1141 		 * any.  So here we send ours, with ACK already set.
1142 		 */
1143 		bge_reg_put32(bgep, TX_1000BASEX_AUTONEG_REG,
1144 			bgep->serdes_advert | AUTONEG_CODE_ACKNOWLEDGE);
1145 		bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
1146 			ETHERNET_MODE_SEND_CFGS);
1147 	} else {
1148 		/*
1149 		 * Phase 2: partner has ACKed our configs, so now we can
1150 		 * stop sending; once our partner also stops sending, we
1151 		 * can resolve the Tx/Rx configs.
1152 		 */
1153 		bge_reg_clr32(bgep, ETHERNET_MAC_MODE_REG,
1154 			ETHERNET_MODE_SEND_CFGS);
1155 	}
1156 
1157 	BGE_DEBUG(("bge_autoneg_serdes: Rx 0x%x %s Tx 0x%x",
1158 		bgep->serdes_lpadv,
1159 		ack ? "stop" : "send",
1160 		bgep->serdes_advert));
1161 }
1162 
1163 static boolean_t
1164 bge_check_serdes(bge_t *bgep, boolean_t recheck)
1165 {
1166 	uint32_t emac_status;
1167 	uint32_t lpadv;
1168 	boolean_t linkup;
1169 
1170 	for (;;) {
1171 		/*
1172 		 * Step 10: read & clear the main (Ethernet) MAC status
1173 		 * (the relevant bits of this are write-one-to-clear).
1174 		 */
1175 		emac_status = bge_reg_get32(bgep, ETHERNET_MAC_STATUS_REG);
1176 		bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, emac_status);
1177 
1178 		BGE_DEBUG(("bge_check_serdes: link %d/%s, "
1179 				"MAC status 0x%x (was 0x%x)",
1180 			bgep->link_state, UPORDOWN(bgep->param_link_up),
1181 			emac_status, bgep->serdes_status));
1182 
1183 		/*
1184 		 * We will only consider the link UP if all the readings
1185 		 * are consistent and give meaningful results ...
1186 		 */
1187 		bgep->serdes_status = emac_status;
1188 		linkup = BIS(emac_status, ETHERNET_STATUS_SIGNAL_DETECT);
1189 		linkup &= BIS(emac_status, ETHERNET_STATUS_PCS_SYNCHED);
1190 
1191 		/*
1192 		 * Now some fiddling with the interpretation:
1193 		 *	if there's been an error at the PCS level, treat
1194 		 *	it as a link change (the h/w doesn't do this)
1195 		 *
1196 		 *	if there's been a change, but it's only a PCS sync
1197 		 *	change (not a config change), AND the link already
1198 		 *	was & is still UP, then ignore the change
1199 		 */
1200 		if (BIS(emac_status, ETHERNET_STATUS_PCS_ERROR))
1201 			emac_status |= ETHERNET_STATUS_LINK_CHANGED;
1202 		else if (BIC(emac_status, ETHERNET_STATUS_CFG_CHANGED))
1203 			if (bgep->param_link_up && linkup)
1204 				emac_status &= ~ETHERNET_STATUS_LINK_CHANGED;
1205 
1206 		BGE_DEBUG(("bge_check_serdes: status 0x%x => 0x%x %s",
1207 			bgep->serdes_status, emac_status, UPORDOWN(linkup)));
1208 
1209 		/*
1210 		 * If we're receiving configs, run the autoneg protocol
1211 		 */
1212 		if (linkup && BIS(emac_status, ETHERNET_STATUS_RECEIVING_CFG))
1213 			bge_autoneg_serdes(bgep);
1214 
1215 		/*
1216 		 * If the SerDes status hasn't changed, we're done ...
1217 		 */
1218 		if (BIC(emac_status, ETHERNET_STATUS_LINK_CHANGED))
1219 			break;
1220 
1221 		/*
1222 		 * Record when the SerDes status changed, then go
1223 		 * round again until we no longer see a change ...
1224 		 */
1225 		bgep->phys_event_time = gethrtime();
1226 		recheck = B_TRUE;
1227 	}
1228 
1229 	/*
1230 	 * If we're not forcing a recheck (i.e. the link state was already
1231 	 * known), and we didn't see the hardware flag a change, there's
1232 	 * no more to do (and we tell the caller nothing happened).
1233 	 */
1234 	if (!recheck)
1235 		return (B_FALSE);
1236 
1237 	/*
1238 	 * Don't resolve autoneg until we're no longer receiving configs
1239 	 */
1240 	if (linkup && BIS(emac_status, ETHERNET_STATUS_RECEIVING_CFG))
1241 		return (B_FALSE);
1242 
1243 	/*
1244 	 * Assume very little ...
1245 	 */
1246 	bgep->param_lp_autoneg = B_FALSE;
1247 	bgep->param_lp_1000fdx = B_FALSE;
1248 	bgep->param_lp_1000hdx = B_FALSE;
1249 	bgep->param_lp_100fdx = B_FALSE;
1250 	bgep->param_lp_100hdx = B_FALSE;
1251 	bgep->param_lp_10fdx = B_FALSE;
1252 	bgep->param_lp_10hdx = B_FALSE;
1253 	bgep->param_lp_pause = B_FALSE;
1254 	bgep->param_lp_asym_pause = B_FALSE;
1255 	bgep->param_link_autoneg = B_FALSE;
1256 	bgep->param_link_tx_pause = B_FALSE;
1257 	if (bgep->param_adv_autoneg)
1258 		bgep->param_link_rx_pause = B_FALSE;
1259 	else
1260 		bgep->param_link_rx_pause = bgep->param_adv_pause;
1261 
1262 	/*
1263 	 * Discover all the link partner's abilities.
1264 	 */
1265 	lpadv = bgep->serdes_lpadv;
1266 	if (lpadv != 0 && BIC(lpadv, AUTONEG_CODE_FAULT_MASK)) {
1267 		/*
1268 		 * No fault, so derive partner's capabilities
1269 		 */
1270 		bgep->param_lp_autoneg = B_TRUE;
1271 		bgep->param_lp_1000fdx = BIS(lpadv, AUTONEG_CODE_FULL_DUPLEX);
1272 		bgep->param_lp_1000hdx = BIS(lpadv, AUTONEG_CODE_HALF_DUPLEX);
1273 		bgep->param_lp_pause = BIS(lpadv, AUTONEG_CODE_PAUSE);
1274 		bgep->param_lp_asym_pause = BIS(lpadv, AUTONEG_CODE_ASYM_PAUSE);
1275 
1276 		/*
1277 		 * Pause direction resolution
1278 		 */
1279 		bgep->param_link_autoneg = B_TRUE;
1280 		if (bgep->param_adv_pause &&
1281 		    bgep->param_lp_pause) {
1282 			bgep->param_link_tx_pause = B_TRUE;
1283 			bgep->param_link_rx_pause = B_TRUE;
1284 		}
1285 		if (bgep->param_adv_asym_pause &&
1286 		    bgep->param_lp_asym_pause) {
1287 			if (bgep->param_adv_pause)
1288 				bgep->param_link_rx_pause = B_TRUE;
1289 			if (bgep->param_lp_pause)
1290 				bgep->param_link_tx_pause = B_TRUE;
1291 		}
1292 	}
1293 
1294 	/*
1295 	 * Step 12: update ndd-visible state parameters, BUT!
1296 	 * we don't transfer the new state to <link_state> just yet;
1297 	 * instead we mark the <link_state> as UNKNOWN, and our caller
1298 	 * will resolve it once the status has stopped changing and
1299 	 * been stable for several seconds.
1300 	 */
1301 	BGE_DEBUG(("bge_check_serdes: link was %s speed %d duplex %d",
1302 		UPORDOWN(bgep->param_link_up),
1303 		bgep->param_link_speed,
1304 		bgep->param_link_duplex));
1305 
1306 	if (linkup) {
1307 		bgep->param_link_up = B_TRUE;
1308 		bgep->param_link_speed = 1000;
1309 		if (bgep->param_adv_1000fdx)
1310 			bgep->param_link_duplex = LINK_DUPLEX_FULL;
1311 		else
1312 			bgep->param_link_duplex = LINK_DUPLEX_HALF;
1313 		if (bgep->param_lp_autoneg && !bgep->param_lp_1000fdx)
1314 			bgep->param_link_duplex = LINK_DUPLEX_HALF;
1315 	} else {
1316 		bgep->param_link_up = B_FALSE;
1317 		bgep->param_link_speed = 0;
1318 		bgep->param_link_duplex = LINK_DUPLEX_UNKNOWN;
1319 	}
1320 	switch (bgep->param_link_duplex) {
1321 	default:
1322 	case LINK_DUPLEX_UNKNOWN:
1323 		bgep->link_mode_msg = "down";
1324 		break;
1325 
1326 	case LINK_DUPLEX_HALF:
1327 		bgep->link_mode_msg = "up 1000Mbps Half-Duplex";
1328 		break;
1329 
1330 	case LINK_DUPLEX_FULL:
1331 		bgep->link_mode_msg = "up 1000Mbps Full-Duplex";
1332 		break;
1333 	}
1334 	bgep->link_state = LINK_STATE_UNKNOWN;
1335 
1336 	BGE_DEBUG(("bge_check_serdes: link now %s speed %d duplex %d",
1337 		UPORDOWN(bgep->param_link_up),
1338 		bgep->param_link_speed,
1339 		bgep->param_link_duplex));
1340 
1341 	return (B_TRUE);
1342 }
1343 
1344 static const phys_ops_t serdes_ops = {
1345 	bge_restart_serdes,
1346 	bge_update_serdes,
1347 	bge_check_serdes
1348 };
1349 
1350 /*
1351  * ========== Exported physical layer control routines ==========
1352  */
1353 
1354 #undef	BGE_DBG
1355 #define	BGE_DBG		BGE_DBG_PHYS	/* debug flag for this code	*/
1356 
1357 /*
1358  * Here we have to determine which media we're using (copper or serdes).
1359  * Once that's done, we can initialise the physical layer appropriately.
1360  */
1361 void
1362 bge_phys_init(bge_t *bgep)
1363 {
1364 	BGE_TRACE(("bge_phys_init($%p)", (void *)bgep));
1365 
1366 	mutex_enter(bgep->genlock);
1367 
1368 	/*
1369 	 * Probe for the (internal) PHY.  If it's not there, we'll assume
1370 	 * that this is a 5703/4S, with a SerDes interface rather than a PHY.
1371 	 */
1372 	bgep->phy_mii_addr = 1;
1373 	if (bge_phy_probe(bgep)) {
1374 		bgep->chipid.flags &= ~CHIP_FLAG_SERDES;
1375 		bgep->phys_delta_time = BGE_PHY_STABLE_TIME;
1376 		bgep->physops = &copper_ops;
1377 	} else {
1378 		bgep->chipid.flags |= CHIP_FLAG_SERDES;
1379 		bgep->phys_delta_time = BGE_SERDES_STABLE_TIME;
1380 		bgep->physops = &serdes_ops;
1381 	}
1382 
1383 	(*bgep->physops->phys_restart)(bgep, B_FALSE);
1384 	mutex_exit(bgep->genlock);
1385 }
1386 
1387 /*
1388  * Reset the physical layer
1389  */
1390 void
1391 bge_phys_reset(bge_t *bgep)
1392 {
1393 	BGE_TRACE(("bge_phys_reset($%p)", (void *)bgep));
1394 
1395 	mutex_enter(bgep->genlock);
1396 	(*bgep->physops->phys_restart)(bgep, B_FALSE);
1397 	mutex_exit(bgep->genlock);
1398 }
1399 
1400 /*
1401  * Reset and power off the physical layer.
1402  *
1403  * Another RESET should get it back to working, but it may take a few
1404  * seconds it may take a few moments to return to normal operation ...
1405  */
1406 void
1407 bge_phys_idle(bge_t *bgep)
1408 {
1409 	BGE_TRACE(("bge_phys_idle($%p)", (void *)bgep));
1410 
1411 	ASSERT(mutex_owned(bgep->genlock));
1412 	(*bgep->physops->phys_restart)(bgep, B_TRUE);
1413 }
1414 
1415 /*
1416  * Synchronise the PHYSICAL layer's speed/duplex/autonegotiation capabilities
1417  * and advertisements with the required settings as specified by the various
1418  * param_* variables that can be poked via the NDD interface.
1419  *
1420  * We always reset the PHYSICAL layer and reprogram *all* relevant registers.
1421  * This is expected to cause the link to go down, and then back up again once
1422  * the link is stable and autonegotiation (if enabled) is complete.  We should
1423  * get a link state change interrupt somewhere along the way ...
1424  *
1425  * NOTE: <genlock> must already be held by the caller
1426  */
1427 void
1428 bge_phys_update(bge_t *bgep)
1429 {
1430 	BGE_TRACE(("bge_phys_update($%p)", (void *)bgep));
1431 
1432 	ASSERT(mutex_owned(bgep->genlock));
1433 	(*bgep->physops->phys_update)(bgep);
1434 }
1435 
1436 #undef	BGE_DBG
1437 #define	BGE_DBG		BGE_DBG_LINK	/* debug flag for this code	*/
1438 
1439 /*
1440  * Read the link status and determine whether anything's changed ...
1441  *
1442  * This routine should be called whenever the chip flags a change
1443  * in the hardware link state, and repeatedly for several seconds
1444  * afterwards, until we're sure the state has stabilised (sometimes
1445  * it goes up and down several times during autonegotiation before
1446  * settling on the proper configuration).  This routine applies
1447  * timing-based heuristics to determine when the state is stable.
1448  *
1449  * This routine returns B_FALSE if the link state has not changed,
1450  * or if it has changed, but hasn't settled for long enough yet.  It
1451  * returns B_TRUE when the change to the new state should be accepted.
1452  * In such a case, the param_* variables give the new hardware state,
1453  * which the caller should use to update link_state etc.
1454  *
1455  * The caller must already hold <genlock>
1456  */
1457 boolean_t
1458 bge_phys_check(bge_t *bgep)
1459 {
1460 	int32_t orig_state;
1461 	boolean_t recheck;
1462 	boolean_t linkup;
1463 	hrtime_t deltat;
1464 	hrtime_t now;
1465 
1466 	BGE_TRACE(("bge_phys_check($%p)", (void *)bgep));
1467 
1468 	ASSERT(mutex_owned(bgep->genlock));
1469 
1470 	linkup = bgep->param_link_up;
1471 	orig_state = bgep->link_state;
1472 	recheck = orig_state == LINK_STATE_UNKNOWN;
1473 	recheck = (*bgep->physops->phys_check)(bgep, recheck);
1474 	if (!recheck)
1475 		return (B_FALSE);
1476 
1477 	/*
1478 	 * At this point, the check_*_link() function above has detected
1479 	 * a change and updated the param_* variables to show what the
1480 	 * latest hardware state seems to be -- but it might still be
1481 	 * changing.
1482 	 *
1483 	 * The link_state must now be UNKNOWN, but if it was previously
1484 	 * UP, we want to recognise this immediately, whereas in any other
1485 	 * case (e.g. DOWN->UP) we don't accept it until a few seconds have
1486 	 * elapsed, to give the hardware time to settle.
1487 	 */
1488 	now = gethrtime();
1489 	deltat = now - bgep->phys_event_time;
1490 
1491 	BGE_DEBUG(("bge_phys_check: link was %d/%s now %d/%s",
1492 		orig_state, UPORDOWN(linkup),
1493 		bgep->link_state, UPORDOWN(bgep->param_link_up)));
1494 	BGE_DEBUG(("bge_phys_check: update %lld change %lld "
1495 			"now %lld delta %lld",
1496 		bgep->phys_write_time, bgep->phys_event_time, now, deltat));
1497 
1498 	if (orig_state == LINK_STATE_UP)
1499 		return (B_TRUE);
1500 	else
1501 		return (deltat > bgep->phys_delta_time);
1502 }
1503