xref: /onnv-gate/usr/src/uts/common/io/rge/rge_chip.c (revision 11499:6190684fb6c4)
1744Sgs150176 /*
2744Sgs150176  * CDDL HEADER START
3744Sgs150176  *
4744Sgs150176  * The contents of this file are subject to the terms of the
52311Sseb  * Common Development and Distribution License (the "License").
62311Sseb  * You may not use this file except in compliance with the License.
7744Sgs150176  *
8744Sgs150176  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9744Sgs150176  * or http://www.opensolaris.org/os/licensing.
10744Sgs150176  * See the License for the specific language governing permissions
11744Sgs150176  * and limitations under the License.
12744Sgs150176  *
13744Sgs150176  * When distributing Covered Code, include this CDDL HEADER in each
14744Sgs150176  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15744Sgs150176  * If applicable, add the following below this CDDL HEADER, with the
16744Sgs150176  * fields enclosed by brackets "[]" replaced with your own identifying
17744Sgs150176  * information: Portions Copyright [yyyy] [name of copyright owner]
18744Sgs150176  *
19744Sgs150176  * CDDL HEADER END
20744Sgs150176  */
21744Sgs150176 /*
2211477SLi-Zhen.You@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23744Sgs150176  * Use is subject to license terms.
24744Sgs150176  */
25744Sgs150176 
26744Sgs150176 #include "rge.h"
27744Sgs150176 
28744Sgs150176 #define	REG32(rgep, reg)	((uint32_t *)(rgep->io_regs+(reg)))
29744Sgs150176 #define	REG16(rgep, reg)	((uint16_t *)(rgep->io_regs+(reg)))
30744Sgs150176 #define	REG8(rgep, reg)		((uint8_t *)(rgep->io_regs+(reg)))
31744Sgs150176 #define	PIO_ADDR(rgep, offset)	((void *)(rgep->io_regs+(offset)))
32744Sgs150176 
33744Sgs150176 /*
34744Sgs150176  * Patchable globals:
35744Sgs150176  *
36744Sgs150176  *	rge_autorecover
37744Sgs150176  *		Enables/disables automatic recovery after fault detection
38744Sgs150176  */
39744Sgs150176 static uint32_t rge_autorecover = 1;
40744Sgs150176 
41744Sgs150176 /*
42744Sgs150176  * globals:
43744Sgs150176  */
44744Sgs150176 #define	RGE_DBG		RGE_DBG_REGS	/* debug flag for this code	*/
4511410SLi-Zhen.You@Sun.COM static uint32_t rge_watchdog_count	= 1 << 5;
46*11499SZhen.W@Sun.COM static uint32_t rge_rx_watchdog_count	= 1 << 3;
47744Sgs150176 
48744Sgs150176 /*
49744Sgs150176  * Operating register get/set access routines
50744Sgs150176  */
51744Sgs150176 
52744Sgs150176 static uint32_t rge_reg_get32(rge_t *rgep, uintptr_t regno);
53744Sgs150176 #pragma	inline(rge_reg_get32)
54744Sgs150176 
55744Sgs150176 static uint32_t
56744Sgs150176 rge_reg_get32(rge_t *rgep, uintptr_t regno)
57744Sgs150176 {
58744Sgs150176 	RGE_TRACE(("rge_reg_get32($%p, 0x%lx)",
595735Smx205022 	    (void *)rgep, regno));
60744Sgs150176 
61744Sgs150176 	return (ddi_get32(rgep->io_handle, REG32(rgep, regno)));
62744Sgs150176 }
63744Sgs150176 
64744Sgs150176 static void rge_reg_put32(rge_t *rgep, uintptr_t regno, uint32_t data);
65744Sgs150176 #pragma	inline(rge_reg_put32)
66744Sgs150176 
67744Sgs150176 static void
68744Sgs150176 rge_reg_put32(rge_t *rgep, uintptr_t regno, uint32_t data)
69744Sgs150176 {
70744Sgs150176 	RGE_TRACE(("rge_reg_put32($%p, 0x%lx, 0x%x)",
715735Smx205022 	    (void *)rgep, regno, data));
72744Sgs150176 
73744Sgs150176 	ddi_put32(rgep->io_handle, REG32(rgep, regno), data);
74744Sgs150176 }
75744Sgs150176 
76744Sgs150176 static void rge_reg_set32(rge_t *rgep, uintptr_t regno, uint32_t bits);
77744Sgs150176 #pragma	inline(rge_reg_set32)
78744Sgs150176 
79744Sgs150176 static void
80744Sgs150176 rge_reg_set32(rge_t *rgep, uintptr_t regno, uint32_t bits)
81744Sgs150176 {
82744Sgs150176 	uint32_t regval;
83744Sgs150176 
84744Sgs150176 	RGE_TRACE(("rge_reg_set32($%p, 0x%lx, 0x%x)",
855735Smx205022 	    (void *)rgep, regno, bits));
86744Sgs150176 
87744Sgs150176 	regval = rge_reg_get32(rgep, regno);
88744Sgs150176 	regval |= bits;
89744Sgs150176 	rge_reg_put32(rgep, regno, regval);
90744Sgs150176 }
91744Sgs150176 
92744Sgs150176 static void rge_reg_clr32(rge_t *rgep, uintptr_t regno, uint32_t bits);
93744Sgs150176 #pragma	inline(rge_reg_clr32)
94744Sgs150176 
95744Sgs150176 static void
96744Sgs150176 rge_reg_clr32(rge_t *rgep, uintptr_t regno, uint32_t bits)
97744Sgs150176 {
98744Sgs150176 	uint32_t regval;
99744Sgs150176 
100744Sgs150176 	RGE_TRACE(("rge_reg_clr32($%p, 0x%lx, 0x%x)",
1015735Smx205022 	    (void *)rgep, regno, bits));
102744Sgs150176 
103744Sgs150176 	regval = rge_reg_get32(rgep, regno);
104744Sgs150176 	regval &= ~bits;
105744Sgs150176 	rge_reg_put32(rgep, regno, regval);
106744Sgs150176 }
107744Sgs150176 
108744Sgs150176 static uint16_t rge_reg_get16(rge_t *rgep, uintptr_t regno);
109744Sgs150176 #pragma	inline(rge_reg_get16)
110744Sgs150176 
111744Sgs150176 static uint16_t
112744Sgs150176 rge_reg_get16(rge_t *rgep, uintptr_t regno)
113744Sgs150176 {
114744Sgs150176 	RGE_TRACE(("rge_reg_get16($%p, 0x%lx)",
1155735Smx205022 	    (void *)rgep, regno));
116744Sgs150176 
117744Sgs150176 	return (ddi_get16(rgep->io_handle, REG16(rgep, regno)));
118744Sgs150176 }
119744Sgs150176 
120744Sgs150176 static void rge_reg_put16(rge_t *rgep, uintptr_t regno, uint16_t data);
121744Sgs150176 #pragma	inline(rge_reg_put16)
122744Sgs150176 
123744Sgs150176 static void
124744Sgs150176 rge_reg_put16(rge_t *rgep, uintptr_t regno, uint16_t data)
125744Sgs150176 {
126744Sgs150176 	RGE_TRACE(("rge_reg_put16($%p, 0x%lx, 0x%x)",
1275735Smx205022 	    (void *)rgep, regno, data));
128744Sgs150176 
129744Sgs150176 	ddi_put16(rgep->io_handle, REG16(rgep, regno), data);
130744Sgs150176 }
131744Sgs150176 
132744Sgs150176 static uint8_t rge_reg_get8(rge_t *rgep, uintptr_t regno);
133744Sgs150176 #pragma	inline(rge_reg_get8)
134744Sgs150176 
135744Sgs150176 static uint8_t
136744Sgs150176 rge_reg_get8(rge_t *rgep, uintptr_t regno)
137744Sgs150176 {
138744Sgs150176 	RGE_TRACE(("rge_reg_get8($%p, 0x%lx)",
1395735Smx205022 	    (void *)rgep, regno));
140744Sgs150176 
141744Sgs150176 	return (ddi_get8(rgep->io_handle, REG8(rgep, regno)));
142744Sgs150176 }
143744Sgs150176 
144744Sgs150176 static void rge_reg_put8(rge_t *rgep, uintptr_t regno, uint8_t data);
145744Sgs150176 #pragma	inline(rge_reg_put8)
146744Sgs150176 
147744Sgs150176 static void
148744Sgs150176 rge_reg_put8(rge_t *rgep, uintptr_t regno, uint8_t data)
149744Sgs150176 {
150744Sgs150176 	RGE_TRACE(("rge_reg_put8($%p, 0x%lx, 0x%x)",
1515735Smx205022 	    (void *)rgep, regno, data));
152744Sgs150176 
153744Sgs150176 	ddi_put8(rgep->io_handle, REG8(rgep, regno), data);
154744Sgs150176 }
155744Sgs150176 
156744Sgs150176 static void rge_reg_set8(rge_t *rgep, uintptr_t regno, uint8_t bits);
157744Sgs150176 #pragma	inline(rge_reg_set8)
158744Sgs150176 
159744Sgs150176 static void
160744Sgs150176 rge_reg_set8(rge_t *rgep, uintptr_t regno, uint8_t bits)
161744Sgs150176 {
162744Sgs150176 	uint8_t regval;
163744Sgs150176 
164744Sgs150176 	RGE_TRACE(("rge_reg_set8($%p, 0x%lx, 0x%x)",
1655735Smx205022 	    (void *)rgep, regno, bits));
166744Sgs150176 
167744Sgs150176 	regval = rge_reg_get8(rgep, regno);
168744Sgs150176 	regval |= bits;
169744Sgs150176 	rge_reg_put8(rgep, regno, regval);
170744Sgs150176 }
171744Sgs150176 
172744Sgs150176 static void rge_reg_clr8(rge_t *rgep, uintptr_t regno, uint8_t bits);
173744Sgs150176 #pragma	inline(rge_reg_clr8)
174744Sgs150176 
175744Sgs150176 static void
176744Sgs150176 rge_reg_clr8(rge_t *rgep, uintptr_t regno, uint8_t bits)
177744Sgs150176 {
178744Sgs150176 	uint8_t regval;
179744Sgs150176 
180744Sgs150176 	RGE_TRACE(("rge_reg_clr8($%p, 0x%lx, 0x%x)",
1815735Smx205022 	    (void *)rgep, regno, bits));
182744Sgs150176 
183744Sgs150176 	regval = rge_reg_get8(rgep, regno);
184744Sgs150176 	regval &= ~bits;
185744Sgs150176 	rge_reg_put8(rgep, regno, regval);
186744Sgs150176 }
187744Sgs150176 
188744Sgs150176 uint16_t rge_mii_get16(rge_t *rgep, uintptr_t mii);
189744Sgs150176 #pragma	no_inline(rge_mii_get16)
190744Sgs150176 
191744Sgs150176 uint16_t
192744Sgs150176 rge_mii_get16(rge_t *rgep, uintptr_t mii)
193744Sgs150176 {
194744Sgs150176 	uint32_t regval;
195744Sgs150176 	uint32_t val32;
196744Sgs150176 	uint32_t i;
197744Sgs150176 
198744Sgs150176 	regval = (mii & PHY_REG_MASK) << PHY_REG_SHIFT;
199744Sgs150176 	rge_reg_put32(rgep, PHY_ACCESS_REG, regval);
200744Sgs150176 
201744Sgs150176 	/*
202744Sgs150176 	 * Waiting for PHY reading OK
203744Sgs150176 	 */
204744Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2054533Sgs150176 		drv_usecwait(1000);
206744Sgs150176 		val32 = rge_reg_get32(rgep, PHY_ACCESS_REG);
207744Sgs150176 		if (val32 & PHY_ACCESS_WR_FLAG)
2082544Sgs150176 			return ((uint16_t)(val32 & 0xffff));
209744Sgs150176 	}
210744Sgs150176 
211744Sgs150176 	RGE_REPORT((rgep, "rge_mii_get16(0x%x) fail, val = %x", mii, val32));
212744Sgs150176 	return ((uint16_t)~0u);
213744Sgs150176 }
214744Sgs150176 
215744Sgs150176 void rge_mii_put16(rge_t *rgep, uintptr_t mii, uint16_t data);
216744Sgs150176 #pragma	no_inline(rge_mii_put16)
217744Sgs150176 
218744Sgs150176 void
219744Sgs150176 rge_mii_put16(rge_t *rgep, uintptr_t mii, uint16_t data)
220744Sgs150176 {
221744Sgs150176 	uint32_t regval;
222744Sgs150176 	uint32_t val32;
223744Sgs150176 	uint32_t i;
224744Sgs150176 
225744Sgs150176 	regval = (mii & PHY_REG_MASK) << PHY_REG_SHIFT;
226744Sgs150176 	regval |= data & PHY_DATA_MASK;
227744Sgs150176 	regval |= PHY_ACCESS_WR_FLAG;
228744Sgs150176 	rge_reg_put32(rgep, PHY_ACCESS_REG, regval);
229744Sgs150176 
230744Sgs150176 	/*
231744Sgs150176 	 * Waiting for PHY writing OK
232744Sgs150176 	 */
233744Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2344533Sgs150176 		drv_usecwait(1000);
235744Sgs150176 		val32 = rge_reg_get32(rgep, PHY_ACCESS_REG);
236744Sgs150176 		if (!(val32 & PHY_ACCESS_WR_FLAG))
237744Sgs150176 			return;
238744Sgs150176 	}
239744Sgs150176 	RGE_REPORT((rgep, "rge_mii_put16(0x%lx, 0x%x) fail",
240744Sgs150176 	    mii, data));
241744Sgs150176 }
242744Sgs150176 
2432544Sgs150176 void rge_ephy_put16(rge_t *rgep, uintptr_t emii, uint16_t data);
2442544Sgs150176 #pragma	no_inline(rge_ephy_put16)
2452544Sgs150176 
2462544Sgs150176 void
2472544Sgs150176 rge_ephy_put16(rge_t *rgep, uintptr_t emii, uint16_t data)
2482544Sgs150176 {
2492544Sgs150176 	uint32_t regval;
2502544Sgs150176 	uint32_t val32;
2512544Sgs150176 	uint32_t i;
2522544Sgs150176 
2532544Sgs150176 	regval = (emii & EPHY_REG_MASK) << EPHY_REG_SHIFT;
2542544Sgs150176 	regval |= data & EPHY_DATA_MASK;
2552544Sgs150176 	regval |= EPHY_ACCESS_WR_FLAG;
2562544Sgs150176 	rge_reg_put32(rgep, EPHY_ACCESS_REG, regval);
2572544Sgs150176 
2582544Sgs150176 	/*
2592544Sgs150176 	 * Waiting for PHY writing OK
2602544Sgs150176 	 */
2612544Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2624533Sgs150176 		drv_usecwait(1000);
2632544Sgs150176 		val32 = rge_reg_get32(rgep, EPHY_ACCESS_REG);
2642544Sgs150176 		if (!(val32 & EPHY_ACCESS_WR_FLAG))
2652544Sgs150176 			return;
2662544Sgs150176 	}
2672544Sgs150176 	RGE_REPORT((rgep, "rge_ephy_put16(0x%lx, 0x%x) fail",
2682544Sgs150176 	    emii, data));
2692544Sgs150176 }
2702544Sgs150176 
271744Sgs150176 /*
272744Sgs150176  * Atomically shift a 32-bit word left, returning
273744Sgs150176  * the value it had *before* the shift was applied
274744Sgs150176  */
275744Sgs150176 static uint32_t rge_atomic_shl32(uint32_t *sp, uint_t count);
276744Sgs150176 #pragma	inline(rge_mii_put16)
277744Sgs150176 
278744Sgs150176 static uint32_t
279744Sgs150176 rge_atomic_shl32(uint32_t *sp, uint_t count)
280744Sgs150176 {
281744Sgs150176 	uint32_t oldval;
282744Sgs150176 	uint32_t newval;
283744Sgs150176 
284744Sgs150176 	/* ATOMICALLY */
285744Sgs150176 	do {
286744Sgs150176 		oldval = *sp;
287744Sgs150176 		newval = oldval << count;
288744Sgs150176 	} while (cas32(sp, oldval, newval) != oldval);
289744Sgs150176 
290744Sgs150176 	return (oldval);
291744Sgs150176 }
292744Sgs150176 
293744Sgs150176 /*
294744Sgs150176  * PHY operation routines
295744Sgs150176  */
296744Sgs150176 #if	RGE_DEBUGGING
297744Sgs150176 
2986990Sgd78059 void
299744Sgs150176 rge_phydump(rge_t *rgep)
300744Sgs150176 {
301744Sgs150176 	uint16_t regs[32];
302744Sgs150176 	int i;
303744Sgs150176 
304744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
305744Sgs150176 
306744Sgs150176 	for (i = 0; i < 32; ++i) {
307744Sgs150176 		regs[i] = rge_mii_get16(rgep, i);
308744Sgs150176 	}
309744Sgs150176 
310744Sgs150176 	for (i = 0; i < 32; i += 8)
311744Sgs150176 		RGE_DEBUG(("rge_phydump: "
3125735Smx205022 		    "0x%04x %04x %04x %04x %04x %04x %04x %04x",
3135735Smx205022 		    regs[i+0], regs[i+1], regs[i+2], regs[i+3],
3145735Smx205022 		    regs[i+4], regs[i+5], regs[i+6], regs[i+7]));
315744Sgs150176 }
316744Sgs150176 
317744Sgs150176 #endif	/* RGE_DEBUGGING */
318744Sgs150176 
319744Sgs150176 static void
320744Sgs150176 rge_phy_check(rge_t *rgep)
321744Sgs150176 {
322744Sgs150176 	uint16_t gig_ctl;
323744Sgs150176 
324744Sgs150176 	if (rgep->param_link_up  == LINK_STATE_DOWN) {
325744Sgs150176 		/*
326744Sgs150176 		 * RTL8169S/8110S PHY has the "PCS bug".  Need reset PHY
327744Sgs150176 		 * every 15 seconds whin link down & advertise is 1000.
328744Sgs150176 		 */
329744Sgs150176 		if (rgep->chipid.phy_ver == PHY_VER_S) {
330744Sgs150176 			gig_ctl = rge_mii_get16(rgep, MII_1000BASE_T_CONTROL);
331744Sgs150176 			if (gig_ctl & MII_1000BT_CTL_ADV_FDX) {
332744Sgs150176 				rgep->link_down_count++;
333744Sgs150176 				if (rgep->link_down_count > 15) {
334744Sgs150176 					(void) rge_phy_reset(rgep);
335744Sgs150176 					rgep->stats.phy_reset++;
336744Sgs150176 					rgep->link_down_count = 0;
337744Sgs150176 				}
338744Sgs150176 			}
339744Sgs150176 		}
340744Sgs150176 	} else {
341744Sgs150176 		rgep->link_down_count = 0;
342744Sgs150176 	}
343744Sgs150176 }
344744Sgs150176 
345744Sgs150176 /*
346744Sgs150176  * Basic low-level function to reset the PHY.
347744Sgs150176  * Doesn't incorporate any special-case workarounds.
348744Sgs150176  *
349744Sgs150176  * Returns TRUE on success, FALSE if the RESET bit doesn't clear
350744Sgs150176  */
351744Sgs150176 boolean_t
352744Sgs150176 rge_phy_reset(rge_t *rgep)
353744Sgs150176 {
354744Sgs150176 	uint16_t control;
355744Sgs150176 	uint_t count;
356744Sgs150176 
357744Sgs150176 	/*
358744Sgs150176 	 * Set the PHY RESET bit, then wait up to 5 ms for it to self-clear
359744Sgs150176 	 */
360744Sgs150176 	control = rge_mii_get16(rgep, MII_CONTROL);
361744Sgs150176 	rge_mii_put16(rgep, MII_CONTROL, control | MII_CONTROL_RESET);
3624533Sgs150176 	for (count = 0; count < 5; count++) {
363744Sgs150176 		drv_usecwait(100);
364744Sgs150176 		control = rge_mii_get16(rgep, MII_CONTROL);
365744Sgs150176 		if (BIC(control, MII_CONTROL_RESET))
366744Sgs150176 			return (B_TRUE);
367744Sgs150176 	}
368744Sgs150176 
369744Sgs150176 	RGE_REPORT((rgep, "rge_phy_reset: FAILED, control now 0x%x", control));
370744Sgs150176 	return (B_FALSE);
371744Sgs150176 }
372744Sgs150176 
373744Sgs150176 /*
374744Sgs150176  * Synchronise the PHY's speed/duplex/autonegotiation capabilities
375744Sgs150176  * and advertisements with the required settings as specified by the various
376744Sgs150176  * param_* variables that can be poked via the NDD interface.
377744Sgs150176  *
378744Sgs150176  * We always reset the PHY and reprogram *all* the relevant registers,
379744Sgs150176  * not just those changed.  This should cause the link to go down, and then
380744Sgs150176  * back up again once the link is stable and autonegotiation (if enabled)
381744Sgs150176  * is complete.  We should get a link state change interrupt somewhere along
382744Sgs150176  * the way ...
383744Sgs150176  *
384744Sgs150176  * NOTE: <genlock> must already be held by the caller
385744Sgs150176  */
386744Sgs150176 void
387744Sgs150176 rge_phy_update(rge_t *rgep)
388744Sgs150176 {
389744Sgs150176 	boolean_t adv_autoneg;
390744Sgs150176 	boolean_t adv_pause;
391744Sgs150176 	boolean_t adv_asym_pause;
392744Sgs150176 	boolean_t adv_1000fdx;
393744Sgs150176 	boolean_t adv_1000hdx;
394744Sgs150176 	boolean_t adv_100fdx;
395744Sgs150176 	boolean_t adv_100hdx;
396744Sgs150176 	boolean_t adv_10fdx;
397744Sgs150176 	boolean_t adv_10hdx;
398744Sgs150176 
399744Sgs150176 	uint16_t control;
400744Sgs150176 	uint16_t gigctrl;
401744Sgs150176 	uint16_t anar;
402744Sgs150176 
403744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
404744Sgs150176 
405744Sgs150176 	RGE_DEBUG(("rge_phy_update: autoneg %d "
4065735Smx205022 	    "pause %d asym_pause %d "
4075735Smx205022 	    "1000fdx %d 1000hdx %d "
4085735Smx205022 	    "100fdx %d 100hdx %d "
4095735Smx205022 	    "10fdx %d 10hdx %d ",
4105735Smx205022 	    rgep->param_adv_autoneg,
4115735Smx205022 	    rgep->param_adv_pause, rgep->param_adv_asym_pause,
4125735Smx205022 	    rgep->param_adv_1000fdx, rgep->param_adv_1000hdx,
4135735Smx205022 	    rgep->param_adv_100fdx, rgep->param_adv_100hdx,
4145735Smx205022 	    rgep->param_adv_10fdx, rgep->param_adv_10hdx));
415744Sgs150176 
416744Sgs150176 	control = gigctrl = anar = 0;
417744Sgs150176 
418744Sgs150176 	/*
419744Sgs150176 	 * PHY settings are normally based on the param_* variables,
420744Sgs150176 	 * but if any loopback mode is in effect, that takes precedence.
421744Sgs150176 	 *
422744Sgs150176 	 * RGE supports MAC-internal loopback, PHY-internal loopback,
423744Sgs150176 	 * and External loopback at a variety of speeds (with a special
424744Sgs150176 	 * cable).  In all cases, autoneg is turned OFF, full-duplex
425744Sgs150176 	 * is turned ON, and the speed/mastership is forced.
426744Sgs150176 	 */
427744Sgs150176 	switch (rgep->param_loop_mode) {
428744Sgs150176 	case RGE_LOOP_NONE:
429744Sgs150176 	default:
430744Sgs150176 		adv_autoneg = rgep->param_adv_autoneg;
431744Sgs150176 		adv_pause = rgep->param_adv_pause;
432744Sgs150176 		adv_asym_pause = rgep->param_adv_asym_pause;
433744Sgs150176 		adv_1000fdx = rgep->param_adv_1000fdx;
434744Sgs150176 		adv_1000hdx = rgep->param_adv_1000hdx;
435744Sgs150176 		adv_100fdx = rgep->param_adv_100fdx;
436744Sgs150176 		adv_100hdx = rgep->param_adv_100hdx;
437744Sgs150176 		adv_10fdx = rgep->param_adv_10fdx;
438744Sgs150176 		adv_10hdx = rgep->param_adv_10hdx;
439744Sgs150176 		break;
440744Sgs150176 
441744Sgs150176 	case RGE_LOOP_INTERNAL_PHY:
442744Sgs150176 	case RGE_LOOP_INTERNAL_MAC:
443744Sgs150176 		adv_autoneg = adv_pause = adv_asym_pause = B_FALSE;
444744Sgs150176 		adv_1000fdx = adv_100fdx = adv_10fdx = B_FALSE;
445744Sgs150176 		adv_1000hdx = adv_100hdx = adv_10hdx = B_FALSE;
446744Sgs150176 		rgep->param_link_duplex = LINK_DUPLEX_FULL;
447744Sgs150176 
448744Sgs150176 		switch (rgep->param_loop_mode) {
449744Sgs150176 		case RGE_LOOP_INTERNAL_PHY:
4505735Smx205022 			if (rgep->chipid.mac_ver != MAC_VER_8101E) {
4515735Smx205022 				rgep->param_link_speed = 1000;
4525735Smx205022 				adv_1000fdx = B_TRUE;
4535735Smx205022 			} else {
4545735Smx205022 				rgep->param_link_speed = 100;
4555735Smx205022 				adv_100fdx = B_TRUE;
4565735Smx205022 			}
457744Sgs150176 			control = MII_CONTROL_LOOPBACK;
458744Sgs150176 			break;
459744Sgs150176 
460744Sgs150176 		case RGE_LOOP_INTERNAL_MAC:
4615735Smx205022 			if (rgep->chipid.mac_ver != MAC_VER_8101E) {
4625735Smx205022 				rgep->param_link_speed = 1000;
4635735Smx205022 				adv_1000fdx = B_TRUE;
4645735Smx205022 			} else {
4655735Smx205022 				rgep->param_link_speed = 100;
4665735Smx205022 				adv_100fdx = B_TRUE;
467744Sgs150176 			break;
468744Sgs150176 		}
469744Sgs150176 	}
470744Sgs150176 
471744Sgs150176 	RGE_DEBUG(("rge_phy_update: autoneg %d "
4725735Smx205022 	    "pause %d asym_pause %d "
4735735Smx205022 	    "1000fdx %d 1000hdx %d "
4745735Smx205022 	    "100fdx %d 100hdx %d "
4755735Smx205022 	    "10fdx %d 10hdx %d ",
4765735Smx205022 	    adv_autoneg,
4775735Smx205022 	    adv_pause, adv_asym_pause,
4785735Smx205022 	    adv_1000fdx, adv_1000hdx,
4795735Smx205022 	    adv_100fdx, adv_100hdx,
4805735Smx205022 	    adv_10fdx, adv_10hdx));
481744Sgs150176 
482744Sgs150176 	/*
483744Sgs150176 	 * We should have at least one technology capability set;
484744Sgs150176 	 * if not, we select a default of 1000Mb/s full-duplex
485744Sgs150176 	 */
486744Sgs150176 	if (!adv_1000fdx && !adv_100fdx && !adv_10fdx &&
4875735Smx205022 	    !adv_1000hdx && !adv_100hdx && !adv_10hdx) {
4885735Smx205022 		if (rgep->chipid.mac_ver != MAC_VER_8101E)
4895735Smx205022 			adv_1000fdx = B_TRUE;
4905735Smx205022 		} else {
4915735Smx205022 			adv_1000fdx = B_FALSE;
4925735Smx205022 			adv_100fdx = B_TRUE;
4935735Smx205022 		}
4945735Smx205022 	}
495744Sgs150176 
496744Sgs150176 	/*
497744Sgs150176 	 * Now transform the adv_* variables into the proper settings
498744Sgs150176 	 * of the PHY registers ...
499744Sgs150176 	 *
500744Sgs150176 	 * If autonegotiation is (now) enabled, we want to trigger
501744Sgs150176 	 * a new autonegotiation cycle once the PHY has been
502744Sgs150176 	 * programmed with the capabilities to be advertised.
503744Sgs150176 	 *
504744Sgs150176 	 * RTL8169/8110 doesn't support 1000Mb/s half-duplex.
505744Sgs150176 	 */
506744Sgs150176 	if (adv_autoneg)
507744Sgs150176 		control |= MII_CONTROL_ANE|MII_CONTROL_RSAN;
508744Sgs150176 
509744Sgs150176 	if (adv_1000fdx)
5109860Sgdamore@opensolaris.org 		control |= MII_CONTROL_1GB|MII_CONTROL_FDUPLEX;
511744Sgs150176 	else if (adv_1000hdx)
5129860Sgdamore@opensolaris.org 		control |= MII_CONTROL_1GB;
513744Sgs150176 	else if (adv_100fdx)
514744Sgs150176 		control |= MII_CONTROL_100MB|MII_CONTROL_FDUPLEX;
515744Sgs150176 	else if (adv_100hdx)
516744Sgs150176 		control |= MII_CONTROL_100MB;
517744Sgs150176 	else if (adv_10fdx)
518744Sgs150176 		control |= MII_CONTROL_FDUPLEX;
519744Sgs150176 	else if (adv_10hdx)
520744Sgs150176 		control |= 0;
521744Sgs150176 	else
522744Sgs150176 		{ _NOTE(EMPTY); }	/* Can't get here anyway ...	*/
523744Sgs150176 
524744Sgs150176 	if (adv_1000fdx) {
525744Sgs150176 		gigctrl |= MII_1000BT_CTL_ADV_FDX;
526744Sgs150176 		/*
527744Sgs150176 		 * Chipset limitation: need set other capabilities to true
528744Sgs150176 		 */
5292544Sgs150176 		if (rgep->chipid.is_pcie)
5302544Sgs150176 			adv_1000hdx = B_TRUE;
531744Sgs150176 		adv_100fdx = B_TRUE;
532744Sgs150176 		adv_100hdx  = B_TRUE;
533744Sgs150176 		adv_10fdx = B_TRUE;
534744Sgs150176 		adv_10hdx = B_TRUE;
535744Sgs150176 	}
536744Sgs150176 
537744Sgs150176 	if (adv_1000hdx)
538744Sgs150176 		gigctrl |= MII_1000BT_CTL_ADV_HDX;
539744Sgs150176 
540744Sgs150176 	if (adv_100fdx)
541744Sgs150176 		anar |= MII_ABILITY_100BASE_TX_FD;
542744Sgs150176 	if (adv_100hdx)
543744Sgs150176 		anar |= MII_ABILITY_100BASE_TX;
544744Sgs150176 	if (adv_10fdx)
545744Sgs150176 		anar |= MII_ABILITY_10BASE_T_FD;
546744Sgs150176 	if (adv_10hdx)
547744Sgs150176 		anar |= MII_ABILITY_10BASE_T;
548744Sgs150176 
549744Sgs150176 	if (adv_pause)
550744Sgs150176 		anar |= MII_ABILITY_PAUSE;
551744Sgs150176 	if (adv_asym_pause)
5529860Sgdamore@opensolaris.org 		anar |= MII_ABILITY_ASMPAUSE;
553744Sgs150176 
554744Sgs150176 	/*
555744Sgs150176 	 * Munge in any other fixed bits we require ...
556744Sgs150176 	 */
557744Sgs150176 	anar |= MII_AN_SELECTOR_8023;
558744Sgs150176 
559744Sgs150176 	/*
560744Sgs150176 	 * Restart the PHY and write the new values.  Note the
561744Sgs150176 	 * time, so that we can say whether subsequent link state
562744Sgs150176 	 * changes can be attributed to our reprogramming the PHY
563744Sgs150176 	 */
564744Sgs150176 	rge_phy_init(rgep);
5657192Sgs150176 	if (rgep->chipid.mac_ver == MAC_VER_8168B_B ||
5667192Sgs150176 	    rgep->chipid.mac_ver == MAC_VER_8168B_C) {
5677192Sgs150176 		/* power up PHY for RTL8168B chipset */
5687192Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
5697192Sgs150176 		rge_mii_put16(rgep, PHY_0E_REG, 0x0000);
5707192Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
5717192Sgs150176 	}
572744Sgs150176 	rge_mii_put16(rgep, MII_AN_ADVERT, anar);
5732544Sgs150176 	rge_mii_put16(rgep, MII_1000BASE_T_CONTROL, gigctrl);
574744Sgs150176 	rge_mii_put16(rgep, MII_CONTROL, control);
575744Sgs150176 
576744Sgs150176 	RGE_DEBUG(("rge_phy_update: anar <- 0x%x", anar));
577744Sgs150176 	RGE_DEBUG(("rge_phy_update: control <- 0x%x", control));
578744Sgs150176 	RGE_DEBUG(("rge_phy_update: gigctrl <- 0x%x", gigctrl));
579744Sgs150176 }
580744Sgs150176 
581744Sgs150176 void rge_phy_init(rge_t *rgep);
582744Sgs150176 #pragma	no_inline(rge_phy_init)
583744Sgs150176 
584744Sgs150176 void
585744Sgs150176 rge_phy_init(rge_t *rgep)
586744Sgs150176 {
587744Sgs150176 	rgep->phy_mii_addr = 1;
588744Sgs150176 
589744Sgs150176 	/*
590744Sgs150176 	 * Below phy config steps are copied from the Programming Guide
591744Sgs150176 	 * (there's no detail comments for these steps.)
592744Sgs150176 	 */
5932544Sgs150176 	switch (rgep->chipid.mac_ver) {
5942544Sgs150176 	case MAC_VER_8169S_D:
5952544Sgs150176 	case MAC_VER_8169S_E :
5962544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
597744Sgs150176 		rge_mii_put16(rgep, PHY_15_REG, 0x1000);
598744Sgs150176 		rge_mii_put16(rgep, PHY_18_REG, 0x65c7);
5992544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
600744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0x00a1);
601744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0x0008);
602744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x1020);
603744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x1000);
6042544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0800);
6052544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
6062544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7000);
607744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xff41);
608744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xde60);
609744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x0140);
610744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x0077);
6112544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7800);
6122544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7000);
6132544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa000);
614744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xdf01);
615744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xdf20);
616744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0xff95);
617744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0xfa00);
6182544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa800);
6192544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa000);
6202544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb000);
621744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xff41);
622744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xde20);
623744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x0140);
624744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x00bb);
6252544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb800);
6262544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb000);
6272544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf000);
628744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xdf01);
629744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xdf20);
630744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0xff95);
631744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0xbf00);
6322544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf800);
6332544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf000);
6342544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
635744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
636744Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x0000);
6372544Sgs150176 		break;
638744Sgs150176 
6392544Sgs150176 	case MAC_VER_8169SB:
640744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6412544Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xD41E);
6422544Sgs150176 		rge_mii_put16(rgep, PHY_0E_REG, 0x7bff);
643744Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, GBCR_DEFAULT);
644744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0002);
645744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x90D0);
646744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6472544Sgs150176 		break;
6482544Sgs150176 
6494533Sgs150176 	case MAC_VER_8169SC:
6504533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6514533Sgs150176 		rge_mii_put16(rgep, PHY_ANER_REG, 0x0078);
6524533Sgs150176 		rge_mii_put16(rgep, PHY_ANNPRR_REG, 0x05dc);
6534533Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, 0x2672);
6544533Sgs150176 		rge_mii_put16(rgep, PHY_GBSR_REG, 0x6a14);
6554533Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x7cb0);
6564533Sgs150176 		rge_mii_put16(rgep, PHY_0C_REG, 0xdb80);
6574533Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xc414);
6584533Sgs150176 		rge_mii_put16(rgep, PHY_1C_REG, 0xef03);
6594533Sgs150176 		rge_mii_put16(rgep, PHY_1D_REG, 0x3dc8);
6604533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0003);
6614533Sgs150176 		rge_mii_put16(rgep, PHY_13_REG, 0x0600);
6624533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6634533Sgs150176 		break;
6644533Sgs150176 
6652544Sgs150176 	case MAC_VER_8168:
6662544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6672544Sgs150176 		rge_mii_put16(rgep, PHY_ANER_REG, 0x00aa);
6682544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPTR_REG, 0x3173);
6692544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPRR_REG, 0x08fc);
6702544Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, 0xe2d0);
6712544Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x941a);
6722544Sgs150176 		rge_mii_put16(rgep, PHY_18_REG, 0x65fe);
6732544Sgs150176 		rge_mii_put16(rgep, PHY_1C_REG, 0x1e02);
6742544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0002);
6752544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPTR_REG, 0x103e);
6762544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6772544Sgs150176 		break;
6782544Sgs150176 
6792544Sgs150176 	case MAC_VER_8168B_B:
6802544Sgs150176 	case MAC_VER_8168B_C:
6812544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6822544Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x94b0);
6832544Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xc416);
6842544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0003);
6852544Sgs150176 		rge_mii_put16(rgep, PHY_12_REG, 0x6096);
6862544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6872544Sgs150176 		break;
688744Sgs150176 	}
689744Sgs150176 }
690744Sgs150176 
691744Sgs150176 void rge_chip_ident(rge_t *rgep);
692744Sgs150176 #pragma	no_inline(rge_chip_ident)
693744Sgs150176 
694744Sgs150176 void
695744Sgs150176 rge_chip_ident(rge_t *rgep)
696744Sgs150176 {
697744Sgs150176 	chip_id_t *chip = &rgep->chipid;
698744Sgs150176 	uint32_t val32;
699744Sgs150176 	uint16_t val16;
700744Sgs150176 
7012544Sgs150176 	/*
7022544Sgs150176 	 * Read and record MAC version
7032544Sgs150176 	 */
704744Sgs150176 	val32 = rge_reg_get32(rgep, TX_CONFIG_REG);
705744Sgs150176 	val32 &= HW_VERSION_ID_0 | HW_VERSION_ID_1;
706744Sgs150176 	chip->mac_ver = val32;
70711365SZhen.W@Sun.COM 	chip->is_pcie = pci_lcap_locate(rgep->cfg_handle,
70811365SZhen.W@Sun.COM 	    PCI_CAP_ID_PCI_E, &val16) == DDI_SUCCESS;
7092544Sgs150176 
7102544Sgs150176 	/*
7112544Sgs150176 	 * Read and record PHY version
7122544Sgs150176 	 */
713744Sgs150176 	val16 = rge_mii_get16(rgep, PHY_ID_REG_2);
714744Sgs150176 	val16 &= PHY_VER_MASK;
715744Sgs150176 	chip->phy_ver = val16;
716744Sgs150176 
7172544Sgs150176 	/* set pci latency timer */
7182544Sgs150176 	if (chip->mac_ver == MAC_VER_8169 ||
7194533Sgs150176 	    chip->mac_ver == MAC_VER_8169S_D ||
720*11499SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8169S_E ||
7214533Sgs150176 	    chip->mac_ver == MAC_VER_8169SC)
7222544Sgs150176 		pci_config_put8(rgep->cfg_handle, PCI_CONF_LATENCY_TIMER, 0x40);
7232544Sgs150176 
7244533Sgs150176 	if (chip->mac_ver == MAC_VER_8169SC) {
7254533Sgs150176 		val16 = rge_reg_get16(rgep, RT_CONFIG_1_REG);
7264533Sgs150176 		val16 &= 0x0300;
7274533Sgs150176 		if (val16 == 0x1)	/* 66Mhz PCI */
728*11499SZhen.W@Sun.COM 			rge_reg_put32(rgep, 0x7c, 0x000700ff);
7294533Sgs150176 		else if (val16 == 0x0) /* 33Mhz PCI */
730*11499SZhen.W@Sun.COM 			rge_reg_put32(rgep, 0x7c, 0x0007ff00);
7314533Sgs150176 	}
7324533Sgs150176 
7332544Sgs150176 	/*
7342544Sgs150176 	 * PCIE chipset require the Rx buffer start address must be
7352544Sgs150176 	 * 8-byte alignment and the Rx buffer size must be multiple of 8.
7362544Sgs150176 	 * We'll just use bcopy in receive procedure for the PCIE chipset.
7372544Sgs150176 	 */
7382544Sgs150176 	if (chip->is_pcie) {
7392544Sgs150176 		rgep->chip_flags |= CHIP_FLAG_FORCE_BCOPY;
7402544Sgs150176 		if (rgep->default_mtu > ETHERMTU) {
7412544Sgs150176 			rge_notice(rgep, "Jumbo packets not supported "
7422544Sgs150176 			    "for this PCIE chipset");
7432544Sgs150176 			rgep->default_mtu = ETHERMTU;
7442544Sgs150176 		}
7452544Sgs150176 	}
7462544Sgs150176 	if (rgep->chip_flags & CHIP_FLAG_FORCE_BCOPY)
7472544Sgs150176 		rgep->head_room = 0;
7482544Sgs150176 	else
7492544Sgs150176 		rgep->head_room = RGE_HEADROOM;
7502544Sgs150176 
7512544Sgs150176 	/*
7522544Sgs150176 	 * Initialize other variables.
7532544Sgs150176 	 */
7542544Sgs150176 	if (rgep->default_mtu < ETHERMTU || rgep->default_mtu > RGE_JUMBO_MTU)
7552544Sgs150176 		rgep->default_mtu = ETHERMTU;
7562544Sgs150176 	if (rgep->default_mtu > ETHERMTU) {
757744Sgs150176 		rgep->rxbuf_size = RGE_BUFF_SIZE_JUMBO;
758744Sgs150176 		rgep->txbuf_size = RGE_BUFF_SIZE_JUMBO;
759744Sgs150176 		rgep->ethmax_size = RGE_JUMBO_SIZE;
760744Sgs150176 	} else {
761744Sgs150176 		rgep->rxbuf_size = RGE_BUFF_SIZE_STD;
762744Sgs150176 		rgep->txbuf_size = RGE_BUFF_SIZE_STD;
763744Sgs150176 		rgep->ethmax_size = ETHERMAX;
764744Sgs150176 	}
765744Sgs150176 	chip->rxconfig = RX_CONFIG_DEFAULT;
766744Sgs150176 	chip->txconfig = TX_CONFIG_DEFAULT;
767744Sgs150176 
76811365SZhen.W@Sun.COM 	/* interval to update statistics for polling mode */
76911365SZhen.W@Sun.COM 	rgep->tick_delta = drv_usectohz(1000*1000/CLK_TICK);
77011365SZhen.W@Sun.COM 
77111365SZhen.W@Sun.COM 	/* ensure we are not in polling mode */
77211365SZhen.W@Sun.COM 	rgep->curr_tick = ddi_get_lbolt() - 2*rgep->tick_delta;
773744Sgs150176 	RGE_TRACE(("%s: MAC version = %x, PHY version = %x",
774744Sgs150176 	    rgep->ifname, chip->mac_ver, chip->phy_ver));
775744Sgs150176 }
776744Sgs150176 
777744Sgs150176 /*
778744Sgs150176  * Perform first-stage chip (re-)initialisation, using only config-space
779744Sgs150176  * accesses:
780744Sgs150176  *
781744Sgs150176  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
782744Sgs150176  *   returning the data in the structure pointed to by <idp>.
783744Sgs150176  * + Enable Memory Space accesses.
784744Sgs150176  * + Enable Bus Mastering according.
785744Sgs150176  */
786744Sgs150176 void rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp);
787744Sgs150176 #pragma	no_inline(rge_chip_cfg_init)
788744Sgs150176 
789744Sgs150176 void
790744Sgs150176 rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp)
791744Sgs150176 {
792744Sgs150176 	ddi_acc_handle_t handle;
793744Sgs150176 	uint16_t commd;
794744Sgs150176 
795744Sgs150176 	handle = rgep->cfg_handle;
796744Sgs150176 
797744Sgs150176 	/*
798744Sgs150176 	 * Save PCI cache line size and subsystem vendor ID
799744Sgs150176 	 */
800744Sgs150176 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
801744Sgs150176 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
802744Sgs150176 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
803744Sgs150176 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
804744Sgs150176 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
805744Sgs150176 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
806744Sgs150176 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
807744Sgs150176 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
808744Sgs150176 
809744Sgs150176 	/*
810744Sgs150176 	 * Turn on Master Enable (DMA) and IO Enable bits.
811744Sgs150176 	 * Enable PCI Memory Space accesses
812744Sgs150176 	 */
813744Sgs150176 	commd = cidp->command;
814744Sgs150176 	commd |= PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
815744Sgs150176 	pci_config_put16(handle, PCI_CONF_COMM, commd);
816744Sgs150176 
817744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
8185735Smx205022 	    cidp->vendor, cidp->device, cidp->revision));
819744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: subven 0x%x subdev 0x%x",
8205735Smx205022 	    cidp->subven, cidp->subdev));
821744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: clsize %d latency %d command 0x%x",
8225735Smx205022 	    cidp->clsize, cidp->latency, cidp->command));
823744Sgs150176 }
824744Sgs150176 
825744Sgs150176 int rge_chip_reset(rge_t *rgep);
826744Sgs150176 #pragma	no_inline(rge_chip_reset)
827744Sgs150176 
828744Sgs150176 int
829744Sgs150176 rge_chip_reset(rge_t *rgep)
830744Sgs150176 {
831744Sgs150176 	int i;
832744Sgs150176 	uint8_t val8;
833744Sgs150176 
834744Sgs150176 	/*
835744Sgs150176 	 * Chip should be in STOP state
836744Sgs150176 	 */
837744Sgs150176 	rge_reg_clr8(rgep, RT_COMMAND_REG,
838744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
839744Sgs150176 
840744Sgs150176 	/*
841744Sgs150176 	 * Disable interrupt
842744Sgs150176 	 */
843744Sgs150176 	rgep->int_mask = INT_MASK_NONE;
8442544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
845744Sgs150176 
846744Sgs150176 	/*
847744Sgs150176 	 * Clear pended interrupt
848744Sgs150176 	 */
849744Sgs150176 	rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
850744Sgs150176 
851744Sgs150176 	/*
852744Sgs150176 	 * Reset chip
853744Sgs150176 	 */
854744Sgs150176 	rge_reg_set8(rgep, RT_COMMAND_REG, RT_COMMAND_RESET);
855744Sgs150176 
856744Sgs150176 	/*
857744Sgs150176 	 * Wait for reset success
858744Sgs150176 	 */
859744Sgs150176 	for (i = 0; i < CHIP_RESET_LOOP; i++) {
860744Sgs150176 		drv_usecwait(10);
861744Sgs150176 		val8 = rge_reg_get8(rgep, RT_COMMAND_REG);
862744Sgs150176 		if (!(val8 & RT_COMMAND_RESET)) {
863744Sgs150176 			rgep->rge_chip_state = RGE_CHIP_RESET;
864744Sgs150176 			return (0);
865744Sgs150176 		}
866744Sgs150176 	}
867744Sgs150176 	RGE_REPORT((rgep, "rge_chip_reset fail."));
868744Sgs150176 	return (-1);
869744Sgs150176 }
870744Sgs150176 
871744Sgs150176 void rge_chip_init(rge_t *rgep);
872744Sgs150176 #pragma	no_inline(rge_chip_init)
873744Sgs150176 
874744Sgs150176 void
875744Sgs150176 rge_chip_init(rge_t *rgep)
876744Sgs150176 {
877744Sgs150176 	uint32_t val32;
8782544Sgs150176 	uint32_t val16;
8792544Sgs150176 	uint32_t *hashp;
8802544Sgs150176 	chip_id_t *chip = &rgep->chipid;
8812544Sgs150176 
88211365SZhen.W@Sun.COM 	/*
88311365SZhen.W@Sun.COM 	 * Increase the threshold voltage of RX sensitivity
88411365SZhen.W@Sun.COM 	 */
88511365SZhen.W@Sun.COM 	if (chip->mac_ver == MAC_VER_8168B_B ||
88611365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8168B_C ||
88711365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8101E ||
88811365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8101E_C) {
88911365SZhen.W@Sun.COM 		rge_ephy_put16(rgep, 0x01, 0x1bd3);
89011365SZhen.W@Sun.COM 	}
8912544Sgs150176 
89211365SZhen.W@Sun.COM 	if (chip->mac_ver == MAC_VER_8168 ||
89311365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8168B_B) {
8942544Sgs150176 		val16 = rge_reg_get8(rgep, PHY_STATUS_REG);
8952544Sgs150176 		val16 = 0x12<<8 | val16;
89611365SZhen.W@Sun.COM 		rge_reg_put16(rgep, PHY_STATUS_REG, val16);
89711365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00021c01);
89811365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f088);
89911365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00004000);
90011365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f0b0);
90111365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x0000f068);
90211365SZhen.W@Sun.COM 		val32 = rge_reg_get32(rgep, RT_CSI_DATA_REG);
90311365SZhen.W@Sun.COM 		val32 |= 0x7000;
90411365SZhen.W@Sun.COM 		val32 &= 0xffff5fff;
90511365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, val32);
90611365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f068);
9072544Sgs150176 	}
908744Sgs150176 
909744Sgs150176 	/*
910744Sgs150176 	 * Config MII register
911744Sgs150176 	 */
912744Sgs150176 	rgep->param_link_up = LINK_STATE_DOWN;
913744Sgs150176 	rge_phy_update(rgep);
914744Sgs150176 
915744Sgs150176 	/*
916744Sgs150176 	 * Enable Rx checksum offload.
917744Sgs150176 	 *  Then for vlan support, we must enable receive vlan de-tagging.
918744Sgs150176 	 *  Otherwise, there'll be checksum error.
919744Sgs150176 	 */
9202544Sgs150176 	val16 = rge_reg_get16(rgep, CPLUS_COMMAND_REG);
9212544Sgs150176 	val16 |= RX_CKSM_OFFLOAD | RX_VLAN_DETAG;
9222544Sgs150176 	if (chip->mac_ver == MAC_VER_8169S_D) {
9232544Sgs150176 		val16 |= CPLUS_BIT14 | MUL_PCI_RW_ENABLE;
924744Sgs150176 		rge_reg_put8(rgep, RESV_82_REG, 0x01);
925744Sgs150176 	}
926*11499SZhen.W@Sun.COM 	if (chip->mac_ver == MAC_VER_8169S_E ||
927*11499SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8169SC) {
928*11499SZhen.W@Sun.COM 		val16 |= MUL_PCI_RW_ENABLE;
929*11499SZhen.W@Sun.COM 	}
9302544Sgs150176 	rge_reg_put16(rgep, CPLUS_COMMAND_REG, val16 & (~0x03));
931744Sgs150176 
932744Sgs150176 	/*
933744Sgs150176 	 * Start transmit/receive before set tx/rx configuration register
934744Sgs150176 	 */
9352544Sgs150176 	if (!chip->is_pcie)
9362544Sgs150176 		rge_reg_set8(rgep, RT_COMMAND_REG,
9372544Sgs150176 		    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
938744Sgs150176 
939744Sgs150176 	/*
940744Sgs150176 	 * Set dump tally counter register
941744Sgs150176 	 */
942744Sgs150176 	val32 = rgep->dma_area_stats.cookie.dmac_laddress >> 32;
943744Sgs150176 	rge_reg_put32(rgep, DUMP_COUNTER_REG_1, val32);
944744Sgs150176 	val32 = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
945744Sgs150176 	val32 &= DUMP_COUNTER_REG_RESV;
946744Sgs150176 	val32 |= rgep->dma_area_stats.cookie.dmac_laddress;
947744Sgs150176 	rge_reg_put32(rgep, DUMP_COUNTER_REG_0, val32);
948744Sgs150176 
949744Sgs150176 	/*
950744Sgs150176 	 * Change to config register write enable mode
951744Sgs150176 	 */
952744Sgs150176 	rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
953744Sgs150176 
954744Sgs150176 	/*
955744Sgs150176 	 * Set Tx/Rx maximum packet size
956744Sgs150176 	 */
9572544Sgs150176 	if (rgep->default_mtu > ETHERMTU) {
958744Sgs150176 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_JUMBO);
959744Sgs150176 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_JUMBO);
9605735Smx205022 	} else if (rgep->chipid.mac_ver != MAC_VER_8101E) {
961744Sgs150176 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD);
962744Sgs150176 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD);
9635735Smx205022 	} else {
9645735Smx205022 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD_8101E);
9655735Smx205022 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD_8101E);
966744Sgs150176 	}
967744Sgs150176 
968744Sgs150176 	/*
969744Sgs150176 	 * Set receive configuration register
970744Sgs150176 	 */
971744Sgs150176 	val32 = rge_reg_get32(rgep, RX_CONFIG_REG);
972744Sgs150176 	val32 &= RX_CONFIG_REG_RESV;
973744Sgs150176 	if (rgep->promisc)
974744Sgs150176 		val32 |= RX_ACCEPT_ALL_PKT;
9752544Sgs150176 	rge_reg_put32(rgep, RX_CONFIG_REG, val32 | chip->rxconfig);
976744Sgs150176 
977744Sgs150176 	/*
978744Sgs150176 	 * Set transmit configuration register
979744Sgs150176 	 */
980744Sgs150176 	val32 = rge_reg_get32(rgep, TX_CONFIG_REG);
981744Sgs150176 	val32 &= TX_CONFIG_REG_RESV;
9822544Sgs150176 	rge_reg_put32(rgep, TX_CONFIG_REG, val32 | chip->txconfig);
983744Sgs150176 
984744Sgs150176 	/*
985744Sgs150176 	 * Set Tx/Rx descriptor register
986744Sgs150176 	 */
987744Sgs150176 	val32 = rgep->tx_desc.cookie.dmac_laddress;
988744Sgs150176 	rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_LO_REG, val32);
989744Sgs150176 	val32 = rgep->tx_desc.cookie.dmac_laddress >> 32;
990744Sgs150176 	rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_HI_REG, val32);
991744Sgs150176 	rge_reg_put32(rgep, HIGH_TX_RING_ADDR_LO_REG, 0);
992744Sgs150176 	rge_reg_put32(rgep, HIGH_TX_RING_ADDR_HI_REG, 0);
993744Sgs150176 	val32 = rgep->rx_desc.cookie.dmac_laddress;
994744Sgs150176 	rge_reg_put32(rgep, RX_RING_ADDR_LO_REG, val32);
995744Sgs150176 	val32 = rgep->rx_desc.cookie.dmac_laddress >> 32;
996744Sgs150176 	rge_reg_put32(rgep, RX_RING_ADDR_HI_REG, val32);
997744Sgs150176 
998744Sgs150176 	/*
999744Sgs150176 	 * Suggested setting from Realtek
1000744Sgs150176 	 */
10015735Smx205022 	if (rgep->chipid.mac_ver != MAC_VER_8101E)
10025735Smx205022 		rge_reg_put16(rgep, RESV_E2_REG, 0x282a);
10035735Smx205022 	else
10045735Smx205022 		rge_reg_put16(rgep, RESV_E2_REG, 0x0000);
1005744Sgs150176 
1006744Sgs150176 	/*
1007744Sgs150176 	 * Set multicast register
1008744Sgs150176 	 */
10092544Sgs150176 	hashp = (uint32_t *)rgep->mcast_hash;
101010814SKHF04453@nifty.ne.jp 	if (rgep->promisc) {
101110814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
101210814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
101310814SKHF04453@nifty.ne.jp 	} else {
101410814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
101510814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
101610814SKHF04453@nifty.ne.jp 	}
1017744Sgs150176 
1018744Sgs150176 	/*
1019744Sgs150176 	 * Msic register setting:
1020744Sgs150176 	 *   -- Missed packet counter: clear it
1021744Sgs150176 	 *   -- TimerInt Register
1022744Sgs150176 	 *   -- Timer count register
1023744Sgs150176 	 */
1024744Sgs150176 	rge_reg_put32(rgep, RX_PKT_MISS_COUNT_REG, 0);
1025744Sgs150176 	rge_reg_put32(rgep, TIMER_INT_REG, TIMER_INT_NONE);
1026744Sgs150176 	rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
10274533Sgs150176 
10284533Sgs150176 	/*
10297825SMin.Xu@Sun.COM 	 * disable the Unicast Wakeup Frame capability
10307825SMin.Xu@Sun.COM 	 */
10317825SMin.Xu@Sun.COM 	rge_reg_clr8(rgep, RT_CONFIG_5_REG, RT_UNI_WAKE_FRAME);
10327825SMin.Xu@Sun.COM 
10337825SMin.Xu@Sun.COM 	/*
10344533Sgs150176 	 * Return to normal network/host communication mode
10354533Sgs150176 	 */
10364533Sgs150176 	rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
10374533Sgs150176 	drv_usecwait(20);
1038744Sgs150176 }
1039744Sgs150176 
1040744Sgs150176 /*
1041744Sgs150176  * rge_chip_start() -- start the chip transmitting and/or receiving,
1042744Sgs150176  * including enabling interrupts
1043744Sgs150176  */
1044744Sgs150176 void rge_chip_start(rge_t *rgep);
1045744Sgs150176 #pragma	no_inline(rge_chip_start)
1046744Sgs150176 
1047744Sgs150176 void
1048744Sgs150176 rge_chip_start(rge_t *rgep)
1049744Sgs150176 {
1050744Sgs150176 	/*
1051744Sgs150176 	 * Clear statistics
1052744Sgs150176 	 */
1053744Sgs150176 	bzero(&rgep->stats, sizeof (rge_stats_t));
1054744Sgs150176 	DMA_ZERO(rgep->dma_area_stats);
1055744Sgs150176 
1056744Sgs150176 	/*
1057744Sgs150176 	 * Start transmit/receive
1058744Sgs150176 	 */
1059744Sgs150176 	rge_reg_set8(rgep, RT_COMMAND_REG,
1060744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1061744Sgs150176 
1062744Sgs150176 	/*
1063744Sgs150176 	 * Enable interrupt
1064744Sgs150176 	 */
1065744Sgs150176 	rgep->int_mask = RGE_INT_MASK;
106611365SZhen.W@Sun.COM 	if (rgep->chipid.is_pcie) {
106711365SZhen.W@Sun.COM 		rgep->int_mask |= NO_TXDESC_INT;
106811365SZhen.W@Sun.COM 	}
1069*11499SZhen.W@Sun.COM 	rgep->rx_fifo_ovf = 0;
1070*11499SZhen.W@Sun.COM 	rgep->int_mask |= RX_FIFO_OVERFLOW_INT;
10712544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1072744Sgs150176 
1073744Sgs150176 	/*
1074744Sgs150176 	 * All done!
1075744Sgs150176 	 */
1076744Sgs150176 	rgep->rge_chip_state = RGE_CHIP_RUNNING;
1077744Sgs150176 }
1078744Sgs150176 
1079744Sgs150176 /*
1080744Sgs150176  * rge_chip_stop() -- stop board receiving
10817656SSherry.Moore@Sun.COM  *
10827656SSherry.Moore@Sun.COM  * Since this function is also invoked by rge_quiesce(), it
10837656SSherry.Moore@Sun.COM  * must not block; also, no tracing or logging takes place
10847656SSherry.Moore@Sun.COM  * when invoked by rge_quiesce().
1085744Sgs150176  */
1086744Sgs150176 void rge_chip_stop(rge_t *rgep, boolean_t fault);
1087744Sgs150176 #pragma	no_inline(rge_chip_stop)
1088744Sgs150176 
1089744Sgs150176 void
1090744Sgs150176 rge_chip_stop(rge_t *rgep, boolean_t fault)
1091744Sgs150176 {
1092744Sgs150176 	/*
1093744Sgs150176 	 * Disable interrupt
1094744Sgs150176 	 */
1095744Sgs150176 	rgep->int_mask = INT_MASK_NONE;
10962544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1097744Sgs150176 
1098744Sgs150176 	/*
1099744Sgs150176 	 * Clear pended interrupt
1100744Sgs150176 	 */
11016764Smx205022 	if (!rgep->suspended) {
11026764Smx205022 		rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
11036764Smx205022 	}
1104744Sgs150176 
1105744Sgs150176 	/*
1106744Sgs150176 	 * Stop the board and disable transmit/receive
1107744Sgs150176 	 */
1108744Sgs150176 	rge_reg_clr8(rgep, RT_COMMAND_REG,
1109744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1110744Sgs150176 
1111744Sgs150176 	if (fault)
1112744Sgs150176 		rgep->rge_chip_state = RGE_CHIP_FAULT;
1113744Sgs150176 	else
1114744Sgs150176 		rgep->rge_chip_state = RGE_CHIP_STOPPED;
1115744Sgs150176 }
1116744Sgs150176 
1117744Sgs150176 /*
1118744Sgs150176  * rge_get_mac_addr() -- get the MAC address on NIC
1119744Sgs150176  */
1120744Sgs150176 static void rge_get_mac_addr(rge_t *rgep);
1121744Sgs150176 #pragma	inline(rge_get_mac_addr)
1122744Sgs150176 
1123744Sgs150176 static void
1124744Sgs150176 rge_get_mac_addr(rge_t *rgep)
1125744Sgs150176 {
1126744Sgs150176 	uint8_t *macaddr = rgep->netaddr;
1127744Sgs150176 	uint32_t val32;
1128744Sgs150176 
1129744Sgs150176 	/*
1130744Sgs150176 	 * Read first 4-byte of mac address
1131744Sgs150176 	 */
1132744Sgs150176 	val32 = rge_reg_get32(rgep, ID_0_REG);
1133744Sgs150176 	macaddr[0] = val32 & 0xff;
1134744Sgs150176 	val32 = val32 >> 8;
1135744Sgs150176 	macaddr[1] = val32 & 0xff;
1136744Sgs150176 	val32 = val32 >> 8;
1137744Sgs150176 	macaddr[2] = val32 & 0xff;
1138744Sgs150176 	val32 = val32 >> 8;
1139744Sgs150176 	macaddr[3] = val32 & 0xff;
1140744Sgs150176 
1141744Sgs150176 	/*
1142744Sgs150176 	 * Read last 2-byte of mac address
1143744Sgs150176 	 */
1144744Sgs150176 	val32 = rge_reg_get32(rgep, ID_4_REG);
1145744Sgs150176 	macaddr[4] = val32 & 0xff;
1146744Sgs150176 	val32 = val32 >> 8;
1147744Sgs150176 	macaddr[5] = val32 & 0xff;
1148744Sgs150176 }
1149744Sgs150176 
1150744Sgs150176 static void rge_set_mac_addr(rge_t *rgep);
1151744Sgs150176 #pragma	inline(rge_set_mac_addr)
1152744Sgs150176 
1153744Sgs150176 static void
1154744Sgs150176 rge_set_mac_addr(rge_t *rgep)
1155744Sgs150176 {
1156744Sgs150176 	uint8_t *p = rgep->netaddr;
1157744Sgs150176 	uint32_t val32;
1158744Sgs150176 
1159744Sgs150176 	/*
1160744Sgs150176 	 * Change to config register write enable mode
1161744Sgs150176 	 */
1162744Sgs150176 	rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1163744Sgs150176 
1164744Sgs150176 	/*
1165744Sgs150176 	 * Get first 4 bytes of mac address
1166744Sgs150176 	 */
1167744Sgs150176 	val32 = p[3];
1168744Sgs150176 	val32 = val32 << 8;
1169744Sgs150176 	val32 |= p[2];
1170744Sgs150176 	val32 = val32 << 8;
1171744Sgs150176 	val32 |= p[1];
1172744Sgs150176 	val32 = val32 << 8;
1173744Sgs150176 	val32 |= p[0];
1174744Sgs150176 
1175744Sgs150176 	/*
1176744Sgs150176 	 * Set first 4 bytes of mac address
1177744Sgs150176 	 */
1178744Sgs150176 	rge_reg_put32(rgep, ID_0_REG, val32);
1179744Sgs150176 
1180744Sgs150176 	/*
1181744Sgs150176 	 * Get last 2 bytes of mac address
1182744Sgs150176 	 */
1183744Sgs150176 	val32 = p[5];
1184744Sgs150176 	val32 = val32 << 8;
1185744Sgs150176 	val32 |= p[4];
1186744Sgs150176 
1187744Sgs150176 	/*
1188744Sgs150176 	 * Set last 2 bytes of mac address
1189744Sgs150176 	 */
1190744Sgs150176 	val32 |= rge_reg_get32(rgep, ID_4_REG) & ~0xffff;
1191744Sgs150176 	rge_reg_put32(rgep, ID_4_REG, val32);
1192744Sgs150176 
1193744Sgs150176 	/*
1194744Sgs150176 	 * Return to normal network/host communication mode
1195744Sgs150176 	 */
1196744Sgs150176 	rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1197744Sgs150176 }
1198744Sgs150176 
1199744Sgs150176 static void rge_set_multi_addr(rge_t *rgep);
1200744Sgs150176 #pragma	inline(rge_set_multi_addr)
1201744Sgs150176 
1202744Sgs150176 static void
1203744Sgs150176 rge_set_multi_addr(rge_t *rgep)
1204744Sgs150176 {
1205744Sgs150176 	uint32_t *hashp;
1206744Sgs150176 
12072544Sgs150176 	hashp = (uint32_t *)rgep->mcast_hash;
12084533Sgs150176 
12094533Sgs150176 	/*
12104533Sgs150176 	 * Change to config register write enable mode
12114533Sgs150176 	 */
12127825SMin.Xu@Sun.COM 	if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12134533Sgs150176 		rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12147825SMin.Xu@Sun.COM 	}
121510814SKHF04453@nifty.ne.jp 	if (rgep->promisc) {
121610814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
121710814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
121810814SKHF04453@nifty.ne.jp 	} else {
121910814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
122010814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
122110814SKHF04453@nifty.ne.jp 	}
12224533Sgs150176 
12234533Sgs150176 	/*
12244533Sgs150176 	 * Return to normal network/host communication mode
12254533Sgs150176 	 */
12267825SMin.Xu@Sun.COM 	if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12274533Sgs150176 		rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12287825SMin.Xu@Sun.COM 	}
1229744Sgs150176 }
1230744Sgs150176 
1231744Sgs150176 static void rge_set_promisc(rge_t *rgep);
1232744Sgs150176 #pragma	inline(rge_set_promisc)
1233744Sgs150176 
1234744Sgs150176 static void
1235744Sgs150176 rge_set_promisc(rge_t *rgep)
1236744Sgs150176 {
1237744Sgs150176 	if (rgep->promisc)
1238744Sgs150176 		rge_reg_set32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1239744Sgs150176 	else
1240744Sgs150176 		rge_reg_clr32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1241744Sgs150176 }
1242744Sgs150176 
1243744Sgs150176 /*
1244744Sgs150176  * rge_chip_sync() -- program the chip with the unicast MAC address,
1245744Sgs150176  * the multicast hash table, the required level of promiscuity, and
1246744Sgs150176  * the current loopback mode ...
1247744Sgs150176  */
1248744Sgs150176 void rge_chip_sync(rge_t *rgep, enum rge_sync_op todo);
1249744Sgs150176 #pragma	no_inline(rge_chip_sync)
1250744Sgs150176 
1251744Sgs150176 void
1252744Sgs150176 rge_chip_sync(rge_t *rgep, enum rge_sync_op todo)
1253744Sgs150176 {
1254744Sgs150176 	switch (todo) {
1255744Sgs150176 	case RGE_GET_MAC:
1256744Sgs150176 		rge_get_mac_addr(rgep);
1257744Sgs150176 		break;
1258744Sgs150176 	case RGE_SET_MAC:
1259744Sgs150176 		/* Reprogram the unicast MAC address(es) ... */
1260744Sgs150176 		rge_set_mac_addr(rgep);
1261744Sgs150176 		break;
1262744Sgs150176 	case RGE_SET_MUL:
1263744Sgs150176 		/* Reprogram the hashed multicast address table ... */
1264744Sgs150176 		rge_set_multi_addr(rgep);
1265744Sgs150176 		break;
1266744Sgs150176 	case RGE_SET_PROMISC:
1267744Sgs150176 		/* Set or clear the PROMISCUOUS mode bit */
126810814SKHF04453@nifty.ne.jp 		rge_set_multi_addr(rgep);
1269744Sgs150176 		rge_set_promisc(rgep);
1270744Sgs150176 		break;
1271744Sgs150176 	default:
1272744Sgs150176 		break;
1273744Sgs150176 	}
1274744Sgs150176 }
1275744Sgs150176 
12768275SEric Cheng void rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag);
1277744Sgs150176 #pragma	no_inline(rge_chip_blank)
1278744Sgs150176 
12798275SEric Cheng /* ARGSUSED */
1280744Sgs150176 void
12818275SEric Cheng rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag)
1282744Sgs150176 {
1283744Sgs150176 	_NOTE(ARGUNUSED(arg, ticks, count));
1284744Sgs150176 }
1285744Sgs150176 
1286744Sgs150176 void rge_tx_trigger(rge_t *rgep);
1287744Sgs150176 #pragma	no_inline(rge_tx_trigger)
1288744Sgs150176 
1289744Sgs150176 void
1290744Sgs150176 rge_tx_trigger(rge_t *rgep)
1291744Sgs150176 {
129211365SZhen.W@Sun.COM 	rge_reg_put8(rgep, TX_RINGS_POLL_REG, NORMAL_TX_RING_POLL);
1293744Sgs150176 }
1294744Sgs150176 
1295744Sgs150176 void rge_hw_stats_dump(rge_t *rgep);
1296744Sgs150176 #pragma	no_inline(rge_tx_trigger)
1297744Sgs150176 
1298744Sgs150176 void
1299744Sgs150176 rge_hw_stats_dump(rge_t *rgep)
1300744Sgs150176 {
1301744Sgs150176 	int i = 0;
130211477SLi-Zhen.You@Sun.COM 	uint32_t regval = 0;
1303744Sgs150176 
130411477SLi-Zhen.You@Sun.COM 	if (rgep->rge_mac_state == RGE_MAC_STOPPED)
130511477SLi-Zhen.You@Sun.COM 		return;
130611477SLi-Zhen.You@Sun.COM 
130711477SLi-Zhen.You@Sun.COM 	regval = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
130811477SLi-Zhen.You@Sun.COM 	while (regval & DUMP_START) {
1309744Sgs150176 		drv_usecwait(100);
1310744Sgs150176 		if (++i > STATS_DUMP_LOOP) {
1311744Sgs150176 			RGE_DEBUG(("rge h/w statistics dump fail!"));
1312744Sgs150176 			rgep->rge_chip_state = RGE_CHIP_ERROR;
1313744Sgs150176 			return;
1314744Sgs150176 		}
131511477SLi-Zhen.You@Sun.COM 		regval = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
1316744Sgs150176 	}
1317744Sgs150176 	DMA_SYNC(rgep->dma_area_stats, DDI_DMA_SYNC_FORKERNEL);
1318744Sgs150176 
1319744Sgs150176 	/*
1320744Sgs150176 	 * Start H/W statistics dump for RTL8169 chip
1321744Sgs150176 	 */
1322744Sgs150176 	rge_reg_set32(rgep, DUMP_COUNTER_REG_0, DUMP_START);
1323744Sgs150176 }
1324744Sgs150176 
1325744Sgs150176 /*
1326744Sgs150176  * ========== Hardware interrupt handler ==========
1327744Sgs150176  */
1328744Sgs150176 
1329744Sgs150176 #undef	RGE_DBG
1330744Sgs150176 #define	RGE_DBG		RGE_DBG_INT	/* debug flag for this code	*/
1331744Sgs150176 
1332744Sgs150176 static void rge_wake_factotum(rge_t *rgep);
1333744Sgs150176 #pragma	inline(rge_wake_factotum)
1334744Sgs150176 
1335744Sgs150176 static void
1336744Sgs150176 rge_wake_factotum(rge_t *rgep)
1337744Sgs150176 {
1338744Sgs150176 	if (rgep->factotum_flag == 0) {
1339744Sgs150176 		rgep->factotum_flag = 1;
13402544Sgs150176 		(void) ddi_intr_trigger_softint(rgep->factotum_hdl, NULL);
1341744Sgs150176 	}
1342744Sgs150176 }
1343744Sgs150176 
1344744Sgs150176 /*
1345744Sgs150176  *	rge_intr() -- handle chip interrupts
1346744Sgs150176  */
13472544Sgs150176 uint_t rge_intr(caddr_t arg1, caddr_t arg2);
1348744Sgs150176 #pragma	no_inline(rge_intr)
1349744Sgs150176 
1350744Sgs150176 uint_t
13512544Sgs150176 rge_intr(caddr_t arg1, caddr_t arg2)
1352744Sgs150176 {
13532544Sgs150176 	rge_t *rgep = (rge_t *)arg1;
1354744Sgs150176 	uint16_t int_status;
135511365SZhen.W@Sun.COM 	clock_t	now;
135611365SZhen.W@Sun.COM 	uint32_t tx_pkts;
135711365SZhen.W@Sun.COM 	uint32_t rx_pkts;
135811365SZhen.W@Sun.COM 	uint32_t poll_rate;
135911365SZhen.W@Sun.COM 	uint32_t opt_pkts;
136011365SZhen.W@Sun.COM 	uint32_t opt_intrs;
136111365SZhen.W@Sun.COM 	boolean_t update_int_mask = B_FALSE;
136211365SZhen.W@Sun.COM 	uint32_t itimer;
1363744Sgs150176 
13642544Sgs150176 	_NOTE(ARGUNUSED(arg2))
13652544Sgs150176 
1366744Sgs150176 	mutex_enter(rgep->genlock);
13676764Smx205022 
13686764Smx205022 	if (rgep->suspended) {
13696764Smx205022 		mutex_exit(rgep->genlock);
13706764Smx205022 		return (DDI_INTR_UNCLAIMED);
13716764Smx205022 	}
13726764Smx205022 
1373744Sgs150176 	/*
1374744Sgs150176 	 * Was this interrupt caused by our device...
1375744Sgs150176 	 */
1376744Sgs150176 	int_status = rge_reg_get16(rgep, INT_STATUS_REG);
1377744Sgs150176 	if (!(int_status & rgep->int_mask)) {
1378744Sgs150176 		mutex_exit(rgep->genlock);
1379744Sgs150176 		return (DDI_INTR_UNCLAIMED);
1380744Sgs150176 				/* indicate it wasn't our interrupt */
1381744Sgs150176 	}
1382744Sgs150176 	rgep->stats.intr++;
1383744Sgs150176 
1384744Sgs150176 	/*
1385744Sgs150176 	 * Clear interrupt
13862544Sgs150176 	 *	For PCIE chipset, we need disable interrupt first.
1387744Sgs150176 	 */
138811365SZhen.W@Sun.COM 	if (rgep->chipid.is_pcie) {
13892544Sgs150176 		rge_reg_put16(rgep, INT_MASK_REG, INT_MASK_NONE);
139011365SZhen.W@Sun.COM 		update_int_mask = B_TRUE;
139111365SZhen.W@Sun.COM 	}
1392744Sgs150176 	rge_reg_put16(rgep, INT_STATUS_REG, int_status);
1393744Sgs150176 
1394744Sgs150176 	/*
139511365SZhen.W@Sun.COM 	 * Calculate optimal polling interval
139611365SZhen.W@Sun.COM 	 */
139711365SZhen.W@Sun.COM 	now = ddi_get_lbolt();
139811365SZhen.W@Sun.COM 	if (now - rgep->curr_tick >= rgep->tick_delta &&
139911365SZhen.W@Sun.COM 	    (rgep->param_link_speed == RGE_SPEED_1000M ||
140011365SZhen.W@Sun.COM 	    rgep->param_link_speed == RGE_SPEED_100M)) {
140111365SZhen.W@Sun.COM 		/* number of rx and tx packets in the last tick */
140211365SZhen.W@Sun.COM 		tx_pkts = rgep->stats.opackets - rgep->last_opackets;
140311365SZhen.W@Sun.COM 		rx_pkts = rgep->stats.rpackets - rgep->last_rpackets;
140411365SZhen.W@Sun.COM 
140511365SZhen.W@Sun.COM 		rgep->last_opackets = rgep->stats.opackets;
140611365SZhen.W@Sun.COM 		rgep->last_rpackets = rgep->stats.rpackets;
140711365SZhen.W@Sun.COM 
140811365SZhen.W@Sun.COM 		/* restore interrupt mask */
140911365SZhen.W@Sun.COM 		rgep->int_mask |= TX_OK_INT | RX_OK_INT;
141011365SZhen.W@Sun.COM 		if (rgep->chipid.is_pcie) {
141111365SZhen.W@Sun.COM 			rgep->int_mask |= NO_TXDESC_INT;
141211365SZhen.W@Sun.COM 		}
141311365SZhen.W@Sun.COM 
141411365SZhen.W@Sun.COM 		/* optimal number of packets in a tick */
141511365SZhen.W@Sun.COM 		if (rgep->param_link_speed == RGE_SPEED_1000M) {
141611365SZhen.W@Sun.COM 			opt_pkts = (1000*1000*1000/8)/ETHERMTU/CLK_TICK;
141711365SZhen.W@Sun.COM 		} else {
141811365SZhen.W@Sun.COM 			opt_pkts = (100*1000*1000/8)/ETHERMTU/CLK_TICK;
141911365SZhen.W@Sun.COM 		}
142011365SZhen.W@Sun.COM 
142111365SZhen.W@Sun.COM 		/*
142211365SZhen.W@Sun.COM 		 * calculate polling interval based on rx and tx packets
142311365SZhen.W@Sun.COM 		 * in the last tick
142411365SZhen.W@Sun.COM 		 */
142511365SZhen.W@Sun.COM 		poll_rate = 0;
142611365SZhen.W@Sun.COM 		if (now - rgep->curr_tick < 2*rgep->tick_delta) {
142711365SZhen.W@Sun.COM 			opt_intrs = opt_pkts/TX_COALESC;
142811365SZhen.W@Sun.COM 			if (tx_pkts > opt_intrs) {
142911365SZhen.W@Sun.COM 				poll_rate = max(tx_pkts/TX_COALESC, opt_intrs);
143011365SZhen.W@Sun.COM 				rgep->int_mask &= ~(TX_OK_INT | NO_TXDESC_INT);
143111365SZhen.W@Sun.COM 			}
143211365SZhen.W@Sun.COM 
143311365SZhen.W@Sun.COM 			opt_intrs = opt_pkts/RX_COALESC;
143411365SZhen.W@Sun.COM 			if (rx_pkts > opt_intrs) {
143511365SZhen.W@Sun.COM 				opt_intrs = max(rx_pkts/RX_COALESC, opt_intrs);
143611365SZhen.W@Sun.COM 				poll_rate = max(opt_intrs, poll_rate);
143711365SZhen.W@Sun.COM 				rgep->int_mask &= ~RX_OK_INT;
143811365SZhen.W@Sun.COM 			}
143911365SZhen.W@Sun.COM 			/* ensure poll_rate reasonable */
144011365SZhen.W@Sun.COM 			poll_rate = min(poll_rate, opt_pkts*4);
144111365SZhen.W@Sun.COM 		}
144211365SZhen.W@Sun.COM 
144311365SZhen.W@Sun.COM 		if (poll_rate) {
144411365SZhen.W@Sun.COM 			/* move to polling mode */
144511365SZhen.W@Sun.COM 			if (rgep->chipid.is_pcie) {
144611365SZhen.W@Sun.COM 				itimer = (TIMER_CLK_PCIE/CLK_TICK)/poll_rate;
144711365SZhen.W@Sun.COM 			} else {
144811365SZhen.W@Sun.COM 				itimer = (TIMER_CLK_PCI/CLK_TICK)/poll_rate;
144911365SZhen.W@Sun.COM 			}
145011365SZhen.W@Sun.COM 		} else {
145111365SZhen.W@Sun.COM 			/* move to normal mode */
145211365SZhen.W@Sun.COM 			itimer = 0;
145311365SZhen.W@Sun.COM 		}
145411365SZhen.W@Sun.COM 		RGE_DEBUG(("%s: poll: itimer:%d int_mask:0x%x",
145511365SZhen.W@Sun.COM 		    __func__, itimer, rgep->int_mask));
145611365SZhen.W@Sun.COM 		rge_reg_put32(rgep, TIMER_INT_REG, itimer);
145711365SZhen.W@Sun.COM 
145811365SZhen.W@Sun.COM 		/* update timestamp for statistics */
145911365SZhen.W@Sun.COM 		rgep->curr_tick = now;
146011365SZhen.W@Sun.COM 
146111365SZhen.W@Sun.COM 		/* reset timer */
146211365SZhen.W@Sun.COM 		int_status |= TIME_OUT_INT;
146311365SZhen.W@Sun.COM 
146411365SZhen.W@Sun.COM 		update_int_mask = B_TRUE;
146511365SZhen.W@Sun.COM 	}
146611365SZhen.W@Sun.COM 
146711365SZhen.W@Sun.COM 	if (int_status & TIME_OUT_INT) {
146811365SZhen.W@Sun.COM 		rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
146911365SZhen.W@Sun.COM 	}
147011365SZhen.W@Sun.COM 
147111365SZhen.W@Sun.COM 	/* flush post writes */
147211365SZhen.W@Sun.COM 	(void) rge_reg_get16(rgep, INT_STATUS_REG);
147311365SZhen.W@Sun.COM 
147411365SZhen.W@Sun.COM 	/*
1475744Sgs150176 	 * Cable link change interrupt
1476744Sgs150176 	 */
1477744Sgs150176 	if (int_status & LINK_CHANGE_INT) {
1478744Sgs150176 		rge_chip_cyclic(rgep);
1479744Sgs150176 	}
14802544Sgs150176 
1481*11499SZhen.W@Sun.COM 	if (int_status & RX_FIFO_OVERFLOW_INT) {
1482*11499SZhen.W@Sun.COM 		/* start rx watchdog timeout detection */
1483*11499SZhen.W@Sun.COM 		rgep->rx_fifo_ovf = 1;
1484*11499SZhen.W@Sun.COM 		if (rgep->int_mask & RX_FIFO_OVERFLOW_INT) {
1485*11499SZhen.W@Sun.COM 			rgep->int_mask &= ~RX_FIFO_OVERFLOW_INT;
1486*11499SZhen.W@Sun.COM 			update_int_mask = B_TRUE;
1487*11499SZhen.W@Sun.COM 		}
1488*11499SZhen.W@Sun.COM 	} else if (int_status & RGE_RX_INT) {
1489*11499SZhen.W@Sun.COM 		/* stop rx watchdog timeout detection */
1490*11499SZhen.W@Sun.COM 		rgep->rx_fifo_ovf = 0;
1491*11499SZhen.W@Sun.COM 		if ((rgep->int_mask & RX_FIFO_OVERFLOW_INT) == 0) {
1492*11499SZhen.W@Sun.COM 			rgep->int_mask |= RX_FIFO_OVERFLOW_INT;
1493*11499SZhen.W@Sun.COM 			update_int_mask = B_TRUE;
1494*11499SZhen.W@Sun.COM 		}
1495*11499SZhen.W@Sun.COM 	}
1496*11499SZhen.W@Sun.COM 
1497744Sgs150176 	mutex_exit(rgep->genlock);
1498744Sgs150176 
1499744Sgs150176 	/*
1500744Sgs150176 	 * Receive interrupt
1501744Sgs150176 	 */
15022544Sgs150176 	if (int_status & RGE_RX_INT)
1503744Sgs150176 		rge_receive(rgep);
1504744Sgs150176 
15052544Sgs150176 	/*
150611365SZhen.W@Sun.COM 	 * Transmit interrupt
15072544Sgs150176 	 */
150811365SZhen.W@Sun.COM 	if (int_status & TX_ERR_INT) {
150911365SZhen.W@Sun.COM 		RGE_REPORT((rgep, "tx error happened, resetting the chip "));
151011365SZhen.W@Sun.COM 		mutex_enter(rgep->genlock);
151111365SZhen.W@Sun.COM 		rgep->rge_chip_state = RGE_CHIP_ERROR;
151211365SZhen.W@Sun.COM 		mutex_exit(rgep->genlock);
151311365SZhen.W@Sun.COM 	} else if ((rgep->chipid.is_pcie && (int_status & NO_TXDESC_INT)) ||
151411365SZhen.W@Sun.COM 	    ((int_status & TX_OK_INT) && rgep->tx_free < RGE_SEND_SLOTS/8)) {
151511365SZhen.W@Sun.COM 		(void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
151611365SZhen.W@Sun.COM 	}
151711365SZhen.W@Sun.COM 
151811365SZhen.W@Sun.COM 	/*
151911410SLi-Zhen.You@Sun.COM 	 * System error interrupt
152011410SLi-Zhen.You@Sun.COM 	 */
152111410SLi-Zhen.You@Sun.COM 	if (int_status & SYS_ERR_INT) {
152211410SLi-Zhen.You@Sun.COM 		RGE_REPORT((rgep, "sys error happened, resetting the chip "));
152311410SLi-Zhen.You@Sun.COM 		mutex_enter(rgep->genlock);
152411410SLi-Zhen.You@Sun.COM 		rgep->rge_chip_state = RGE_CHIP_ERROR;
152511410SLi-Zhen.You@Sun.COM 		mutex_exit(rgep->genlock);
152611410SLi-Zhen.You@Sun.COM 	}
152711410SLi-Zhen.You@Sun.COM 
152811410SLi-Zhen.You@Sun.COM 	/*
152911365SZhen.W@Sun.COM 	 * Re-enable interrupt for PCIE chipset or install new int_mask
153011365SZhen.W@Sun.COM 	 */
153111365SZhen.W@Sun.COM 	if (update_int_mask)
15322544Sgs150176 		rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
15332544Sgs150176 
1534744Sgs150176 	return (DDI_INTR_CLAIMED);	/* indicate it was our interrupt */
1535744Sgs150176 }
1536744Sgs150176 
1537744Sgs150176 /*
1538744Sgs150176  * ========== Factotum, implemented as a softint handler ==========
1539744Sgs150176  */
1540744Sgs150176 
1541744Sgs150176 #undef	RGE_DBG
1542744Sgs150176 #define	RGE_DBG		RGE_DBG_FACT	/* debug flag for this code	*/
1543744Sgs150176 
1544744Sgs150176 static boolean_t rge_factotum_link_check(rge_t *rgep);
1545744Sgs150176 #pragma	no_inline(rge_factotum_link_check)
1546744Sgs150176 
1547744Sgs150176 static boolean_t
1548744Sgs150176 rge_factotum_link_check(rge_t *rgep)
1549744Sgs150176 {
1550744Sgs150176 	uint8_t media_status;
1551744Sgs150176 	int32_t link;
1552744Sgs150176 
1553744Sgs150176 	media_status = rge_reg_get8(rgep, PHY_STATUS_REG);
1554744Sgs150176 	link = (media_status & PHY_STATUS_LINK_UP) ?
1555744Sgs150176 	    LINK_STATE_UP : LINK_STATE_DOWN;
1556744Sgs150176 	if (rgep->param_link_up != link) {
1557744Sgs150176 		/*
15584403Sgd78059 		 * Link change.
1559744Sgs150176 		 */
1560744Sgs150176 		rgep->param_link_up = link;
1561744Sgs150176 
1562744Sgs150176 		if (link == LINK_STATE_UP) {
1563744Sgs150176 			if (media_status & PHY_STATUS_1000MF) {
1564744Sgs150176 				rgep->param_link_speed = RGE_SPEED_1000M;
1565744Sgs150176 				rgep->param_link_duplex = LINK_DUPLEX_FULL;
1566744Sgs150176 			} else {
1567744Sgs150176 				rgep->param_link_speed =
1568744Sgs150176 				    (media_status & PHY_STATUS_100M) ?
1569744Sgs150176 				    RGE_SPEED_100M : RGE_SPEED_10M;
1570744Sgs150176 				rgep->param_link_duplex =
1571744Sgs150176 				    (media_status & PHY_STATUS_DUPLEX_FULL) ?
1572744Sgs150176 				    LINK_DUPLEX_FULL : LINK_DUPLEX_HALF;
1573744Sgs150176 			}
1574744Sgs150176 		}
1575744Sgs150176 		return (B_TRUE);
1576744Sgs150176 	}
1577744Sgs150176 	return (B_FALSE);
1578744Sgs150176 }
1579744Sgs150176 
1580744Sgs150176 /*
1581744Sgs150176  * Factotum routine to check for Tx stall, using the 'watchdog' counter
1582744Sgs150176  */
1583744Sgs150176 static boolean_t rge_factotum_stall_check(rge_t *rgep);
1584744Sgs150176 #pragma	no_inline(rge_factotum_stall_check)
1585744Sgs150176 
1586744Sgs150176 static boolean_t
1587744Sgs150176 rge_factotum_stall_check(rge_t *rgep)
1588744Sgs150176 {
1589744Sgs150176 	uint32_t dogval;
1590744Sgs150176 
1591744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
1592744Sgs150176 
1593744Sgs150176 	/*
1594*11499SZhen.W@Sun.COM 	 * Specific check for RX stall ...
1595*11499SZhen.W@Sun.COM 	 */
1596*11499SZhen.W@Sun.COM 	rgep->rx_fifo_ovf <<= 1;
1597*11499SZhen.W@Sun.COM 	if (rgep->rx_fifo_ovf > rge_rx_watchdog_count) {
1598*11499SZhen.W@Sun.COM 		RGE_REPORT((rgep, "rx_hang detected"));
1599*11499SZhen.W@Sun.COM 		return (B_TRUE);
1600*11499SZhen.W@Sun.COM 	}
1601*11499SZhen.W@Sun.COM 
1602*11499SZhen.W@Sun.COM 	/*
1603744Sgs150176 	 * Specific check for Tx stall ...
1604744Sgs150176 	 *
1605744Sgs150176 	 * The 'watchdog' counter is incremented whenever a packet
1606744Sgs150176 	 * is queued, reset to 1 when some (but not all) buffers
1607744Sgs150176 	 * are reclaimed, reset to 0 (disabled) when all buffers
1608744Sgs150176 	 * are reclaimed, and shifted left here.  If it exceeds the
1609744Sgs150176 	 * threshold value, the chip is assumed to have stalled and
1610744Sgs150176 	 * is put into the ERROR state.  The factotum will then reset
1611744Sgs150176 	 * it on the next pass.
1612744Sgs150176 	 *
1613744Sgs150176 	 * All of which should ensure that we don't get into a state
1614744Sgs150176 	 * where packets are left pending indefinitely!
1615744Sgs150176 	 */
16162544Sgs150176 	if (rgep->resched_needed)
16172544Sgs150176 		(void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
1618744Sgs150176 	dogval = rge_atomic_shl32(&rgep->watchdog, 1);
1619744Sgs150176 	if (dogval < rge_watchdog_count)
1620744Sgs150176 		return (B_FALSE);
1621744Sgs150176 
1622744Sgs150176 	RGE_REPORT((rgep, "Tx stall detected, watchdog code 0x%x", dogval));
1623744Sgs150176 	return (B_TRUE);
1624744Sgs150176 
1625744Sgs150176 }
1626744Sgs150176 
1627744Sgs150176 /*
1628744Sgs150176  * The factotum is woken up when there's something to do that we'd rather
1629744Sgs150176  * not do from inside a hardware interrupt handler or high-level cyclic.
1630744Sgs150176  * Its two main tasks are:
1631744Sgs150176  *	reset & restart the chip after an error
1632744Sgs150176  *	check the link status whenever necessary
1633744Sgs150176  */
16342544Sgs150176 uint_t rge_chip_factotum(caddr_t arg1, caddr_t arg2);
1635744Sgs150176 #pragma	no_inline(rge_chip_factotum)
1636744Sgs150176 
1637744Sgs150176 uint_t
16382544Sgs150176 rge_chip_factotum(caddr_t arg1, caddr_t arg2)
1639744Sgs150176 {
1640744Sgs150176 	rge_t *rgep;
1641744Sgs150176 	uint_t result;
1642744Sgs150176 	boolean_t error;
1643744Sgs150176 	boolean_t linkchg;
1644744Sgs150176 
16452544Sgs150176 	rgep = (rge_t *)arg1;
16462544Sgs150176 	_NOTE(ARGUNUSED(arg2))
1647744Sgs150176 
1648744Sgs150176 	if (rgep->factotum_flag == 0)
1649744Sgs150176 		return (DDI_INTR_UNCLAIMED);
1650744Sgs150176 
1651744Sgs150176 	rgep->factotum_flag = 0;
1652744Sgs150176 	result = DDI_INTR_CLAIMED;
1653744Sgs150176 	error = B_FALSE;
1654744Sgs150176 	linkchg = B_FALSE;
1655744Sgs150176 
1656744Sgs150176 	mutex_enter(rgep->genlock);
1657744Sgs150176 	switch (rgep->rge_chip_state) {
1658744Sgs150176 	default:
1659744Sgs150176 		break;
1660744Sgs150176 
1661744Sgs150176 	case RGE_CHIP_RUNNING:
1662744Sgs150176 		linkchg = rge_factotum_link_check(rgep);
1663744Sgs150176 		error = rge_factotum_stall_check(rgep);
1664744Sgs150176 		break;
1665744Sgs150176 
1666744Sgs150176 	case RGE_CHIP_ERROR:
1667744Sgs150176 		error = B_TRUE;
1668744Sgs150176 		break;
1669744Sgs150176 
1670744Sgs150176 	case RGE_CHIP_FAULT:
1671744Sgs150176 		/*
1672744Sgs150176 		 * Fault detected, time to reset ...
1673744Sgs150176 		 */
1674744Sgs150176 		if (rge_autorecover) {
1675744Sgs150176 			RGE_REPORT((rgep, "automatic recovery activated"));
1676744Sgs150176 			rge_restart(rgep);
1677744Sgs150176 		}
1678744Sgs150176 		break;
1679744Sgs150176 	}
1680744Sgs150176 
1681744Sgs150176 	/*
1682744Sgs150176 	 * If an error is detected, stop the chip now, marking it as
1683744Sgs150176 	 * faulty, so that it will be reset next time through ...
1684744Sgs150176 	 */
1685744Sgs150176 	if (error)
1686744Sgs150176 		rge_chip_stop(rgep, B_TRUE);
1687744Sgs150176 	mutex_exit(rgep->genlock);
1688744Sgs150176 
1689744Sgs150176 	/*
1690744Sgs150176 	 * If the link state changed, tell the world about it.
1691744Sgs150176 	 * Note: can't do this while still holding the mutex.
1692744Sgs150176 	 */
1693744Sgs150176 	if (linkchg)
16942311Sseb 		mac_link_update(rgep->mh, rgep->param_link_up);
1695744Sgs150176 
1696744Sgs150176 	return (result);
1697744Sgs150176 }
1698744Sgs150176 
1699744Sgs150176 /*
1700744Sgs150176  * High-level cyclic handler
1701744Sgs150176  *
1702744Sgs150176  * This routine schedules a (low-level) softint callback to the
1703744Sgs150176  * factotum, and prods the chip to update the status block (which
1704744Sgs150176  * will cause a hardware interrupt when complete).
1705744Sgs150176  */
1706744Sgs150176 void rge_chip_cyclic(void *arg);
1707744Sgs150176 #pragma	no_inline(rge_chip_cyclic)
1708744Sgs150176 
1709744Sgs150176 void
1710744Sgs150176 rge_chip_cyclic(void *arg)
1711744Sgs150176 {
1712744Sgs150176 	rge_t *rgep;
1713744Sgs150176 
1714744Sgs150176 	rgep = arg;
1715744Sgs150176 
1716744Sgs150176 	switch (rgep->rge_chip_state) {
1717744Sgs150176 	default:
1718744Sgs150176 		return;
1719744Sgs150176 
1720744Sgs150176 	case RGE_CHIP_RUNNING:
1721744Sgs150176 		rge_phy_check(rgep);
172211410SLi-Zhen.You@Sun.COM 		if (rgep->tx_free < RGE_SEND_SLOTS)
172311410SLi-Zhen.You@Sun.COM 			rge_send_recycle(rgep);
1724744Sgs150176 		break;
1725744Sgs150176 
1726744Sgs150176 	case RGE_CHIP_FAULT:
1727744Sgs150176 	case RGE_CHIP_ERROR:
1728744Sgs150176 		break;
1729744Sgs150176 	}
1730744Sgs150176 
1731744Sgs150176 	rge_wake_factotum(rgep);
1732744Sgs150176 }
1733744Sgs150176 
1734744Sgs150176 
1735744Sgs150176 /*
1736744Sgs150176  * ========== Ioctl subfunctions ==========
1737744Sgs150176  */
1738744Sgs150176 
1739744Sgs150176 #undef	RGE_DBG
1740744Sgs150176 #define	RGE_DBG		RGE_DBG_PPIO	/* debug flag for this code	*/
1741744Sgs150176 
1742744Sgs150176 #if	RGE_DEBUGGING || RGE_DO_PPIO
1743744Sgs150176 
1744744Sgs150176 static void rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1745744Sgs150176 #pragma	no_inline(rge_chip_peek_cfg)
1746744Sgs150176 
1747744Sgs150176 static void
1748744Sgs150176 rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1749744Sgs150176 {
1750744Sgs150176 	uint64_t regval;
1751744Sgs150176 	uint64_t regno;
1752744Sgs150176 
1753744Sgs150176 	RGE_TRACE(("rge_chip_peek_cfg($%p, $%p)",
17545735Smx205022 	    (void *)rgep, (void *)ppd));
1755744Sgs150176 
1756744Sgs150176 	regno = ppd->pp_acc_offset;
1757744Sgs150176 
1758744Sgs150176 	switch (ppd->pp_acc_size) {
1759744Sgs150176 	case 1:
1760744Sgs150176 		regval = pci_config_get8(rgep->cfg_handle, regno);
1761744Sgs150176 		break;
1762744Sgs150176 
1763744Sgs150176 	case 2:
1764744Sgs150176 		regval = pci_config_get16(rgep->cfg_handle, regno);
1765744Sgs150176 		break;
1766744Sgs150176 
1767744Sgs150176 	case 4:
1768744Sgs150176 		regval = pci_config_get32(rgep->cfg_handle, regno);
1769744Sgs150176 		break;
1770744Sgs150176 
1771744Sgs150176 	case 8:
1772744Sgs150176 		regval = pci_config_get64(rgep->cfg_handle, regno);
1773744Sgs150176 		break;
1774744Sgs150176 	}
1775744Sgs150176 
1776744Sgs150176 	ppd->pp_acc_data = regval;
1777744Sgs150176 }
1778744Sgs150176 
1779744Sgs150176 static void rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1780744Sgs150176 #pragma	no_inline(rge_chip_poke_cfg)
1781744Sgs150176 
1782744Sgs150176 static void
1783744Sgs150176 rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1784744Sgs150176 {
1785744Sgs150176 	uint64_t regval;
1786744Sgs150176 	uint64_t regno;
1787744Sgs150176 
1788744Sgs150176 	RGE_TRACE(("rge_chip_poke_cfg($%p, $%p)",
17895735Smx205022 	    (void *)rgep, (void *)ppd));
1790744Sgs150176 
1791744Sgs150176 	regno = ppd->pp_acc_offset;
1792744Sgs150176 	regval = ppd->pp_acc_data;
1793744Sgs150176 
1794744Sgs150176 	switch (ppd->pp_acc_size) {
1795744Sgs150176 	case 1:
1796744Sgs150176 		pci_config_put8(rgep->cfg_handle, regno, regval);
1797744Sgs150176 		break;
1798744Sgs150176 
1799744Sgs150176 	case 2:
1800744Sgs150176 		pci_config_put16(rgep->cfg_handle, regno, regval);
1801744Sgs150176 		break;
1802744Sgs150176 
1803744Sgs150176 	case 4:
1804744Sgs150176 		pci_config_put32(rgep->cfg_handle, regno, regval);
1805744Sgs150176 		break;
1806744Sgs150176 
1807744Sgs150176 	case 8:
1808744Sgs150176 		pci_config_put64(rgep->cfg_handle, regno, regval);
1809744Sgs150176 		break;
1810744Sgs150176 	}
1811744Sgs150176 }
1812744Sgs150176 
1813744Sgs150176 static void rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1814744Sgs150176 #pragma	no_inline(rge_chip_peek_reg)
1815744Sgs150176 
1816744Sgs150176 static void
1817744Sgs150176 rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1818744Sgs150176 {
1819744Sgs150176 	uint64_t regval;
1820744Sgs150176 	void *regaddr;
1821744Sgs150176 
1822744Sgs150176 	RGE_TRACE(("rge_chip_peek_reg($%p, $%p)",
18235735Smx205022 	    (void *)rgep, (void *)ppd));
1824744Sgs150176 
1825744Sgs150176 	regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1826744Sgs150176 
1827744Sgs150176 	switch (ppd->pp_acc_size) {
1828744Sgs150176 	case 1:
1829744Sgs150176 		regval = ddi_get8(rgep->io_handle, regaddr);
1830744Sgs150176 		break;
1831744Sgs150176 
1832744Sgs150176 	case 2:
1833744Sgs150176 		regval = ddi_get16(rgep->io_handle, regaddr);
1834744Sgs150176 		break;
1835744Sgs150176 
1836744Sgs150176 	case 4:
1837744Sgs150176 		regval = ddi_get32(rgep->io_handle, regaddr);
1838744Sgs150176 		break;
1839744Sgs150176 
1840744Sgs150176 	case 8:
1841744Sgs150176 		regval = ddi_get64(rgep->io_handle, regaddr);
1842744Sgs150176 		break;
1843744Sgs150176 	}
1844744Sgs150176 
1845744Sgs150176 	ppd->pp_acc_data = regval;
1846744Sgs150176 }
1847744Sgs150176 
1848744Sgs150176 static void rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1849744Sgs150176 #pragma	no_inline(rge_chip_peek_reg)
1850744Sgs150176 
1851744Sgs150176 static void
1852744Sgs150176 rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1853744Sgs150176 {
1854744Sgs150176 	uint64_t regval;
1855744Sgs150176 	void *regaddr;
1856744Sgs150176 
1857744Sgs150176 	RGE_TRACE(("rge_chip_poke_reg($%p, $%p)",
18585735Smx205022 	    (void *)rgep, (void *)ppd));
1859744Sgs150176 
1860744Sgs150176 	regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1861744Sgs150176 	regval = ppd->pp_acc_data;
1862744Sgs150176 
1863744Sgs150176 	switch (ppd->pp_acc_size) {
1864744Sgs150176 	case 1:
1865744Sgs150176 		ddi_put8(rgep->io_handle, regaddr, regval);
1866744Sgs150176 		break;
1867744Sgs150176 
1868744Sgs150176 	case 2:
1869744Sgs150176 		ddi_put16(rgep->io_handle, regaddr, regval);
1870744Sgs150176 		break;
1871744Sgs150176 
1872744Sgs150176 	case 4:
1873744Sgs150176 		ddi_put32(rgep->io_handle, regaddr, regval);
1874744Sgs150176 		break;
1875744Sgs150176 
1876744Sgs150176 	case 8:
1877744Sgs150176 		ddi_put64(rgep->io_handle, regaddr, regval);
1878744Sgs150176 		break;
1879744Sgs150176 	}
1880744Sgs150176 }
1881744Sgs150176 
1882744Sgs150176 static void rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1883744Sgs150176 #pragma	no_inline(rge_chip_peek_mii)
1884744Sgs150176 
1885744Sgs150176 static void
1886744Sgs150176 rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1887744Sgs150176 {
1888744Sgs150176 	RGE_TRACE(("rge_chip_peek_mii($%p, $%p)",
18895735Smx205022 	    (void *)rgep, (void *)ppd));
1890744Sgs150176 
1891744Sgs150176 	ppd->pp_acc_data = rge_mii_get16(rgep, ppd->pp_acc_offset/2);
1892744Sgs150176 }
1893744Sgs150176 
1894744Sgs150176 static void rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1895744Sgs150176 #pragma	no_inline(rge_chip_poke_mii)
1896744Sgs150176 
1897744Sgs150176 static void
1898744Sgs150176 rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1899744Sgs150176 {
1900744Sgs150176 	RGE_TRACE(("rge_chip_poke_mii($%p, $%p)",
19015735Smx205022 	    (void *)rgep, (void *)ppd));
1902744Sgs150176 
1903744Sgs150176 	rge_mii_put16(rgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
1904744Sgs150176 }
1905744Sgs150176 
1906744Sgs150176 static void rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1907744Sgs150176 #pragma	no_inline(rge_chip_peek_mem)
1908744Sgs150176 
1909744Sgs150176 static void
1910744Sgs150176 rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1911744Sgs150176 {
1912744Sgs150176 	uint64_t regval;
1913744Sgs150176 	void *vaddr;
1914744Sgs150176 
1915744Sgs150176 	RGE_TRACE(("rge_chip_peek_rge($%p, $%p)",
19165735Smx205022 	    (void *)rgep, (void *)ppd));
1917744Sgs150176 
1918744Sgs150176 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1919744Sgs150176 
1920744Sgs150176 	switch (ppd->pp_acc_size) {
1921744Sgs150176 	case 1:
1922744Sgs150176 		regval = *(uint8_t *)vaddr;
1923744Sgs150176 		break;
1924744Sgs150176 
1925744Sgs150176 	case 2:
1926744Sgs150176 		regval = *(uint16_t *)vaddr;
1927744Sgs150176 		break;
1928744Sgs150176 
1929744Sgs150176 	case 4:
1930744Sgs150176 		regval = *(uint32_t *)vaddr;
1931744Sgs150176 		break;
1932744Sgs150176 
1933744Sgs150176 	case 8:
1934744Sgs150176 		regval = *(uint64_t *)vaddr;
1935744Sgs150176 		break;
1936744Sgs150176 	}
1937744Sgs150176 
1938744Sgs150176 	RGE_DEBUG(("rge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
19395735Smx205022 	    (void *)rgep, (void *)ppd, regval, vaddr));
1940744Sgs150176 
1941744Sgs150176 	ppd->pp_acc_data = regval;
1942744Sgs150176 }
1943744Sgs150176 
1944744Sgs150176 static void rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1945744Sgs150176 #pragma	no_inline(rge_chip_poke_mem)
1946744Sgs150176 
1947744Sgs150176 static void
1948744Sgs150176 rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1949744Sgs150176 {
1950744Sgs150176 	uint64_t regval;
1951744Sgs150176 	void *vaddr;
1952744Sgs150176 
1953744Sgs150176 	RGE_TRACE(("rge_chip_poke_mem($%p, $%p)",
19545735Smx205022 	    (void *)rgep, (void *)ppd));
1955744Sgs150176 
1956744Sgs150176 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1957744Sgs150176 	regval = ppd->pp_acc_data;
1958744Sgs150176 
1959744Sgs150176 	RGE_DEBUG(("rge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
19605735Smx205022 	    (void *)rgep, (void *)ppd, regval, vaddr));
1961744Sgs150176 
1962744Sgs150176 	switch (ppd->pp_acc_size) {
1963744Sgs150176 	case 1:
1964744Sgs150176 		*(uint8_t *)vaddr = (uint8_t)regval;
1965744Sgs150176 		break;
1966744Sgs150176 
1967744Sgs150176 	case 2:
1968744Sgs150176 		*(uint16_t *)vaddr = (uint16_t)regval;
1969744Sgs150176 		break;
1970744Sgs150176 
1971744Sgs150176 	case 4:
1972744Sgs150176 		*(uint32_t *)vaddr = (uint32_t)regval;
1973744Sgs150176 		break;
1974744Sgs150176 
1975744Sgs150176 	case 8:
1976744Sgs150176 		*(uint64_t *)vaddr = (uint64_t)regval;
1977744Sgs150176 		break;
1978744Sgs150176 	}
1979744Sgs150176 }
1980744Sgs150176 
1981744Sgs150176 static enum ioc_reply rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
1982744Sgs150176 					struct iocblk *iocp);
1983744Sgs150176 #pragma	no_inline(rge_pp_ioctl)
1984744Sgs150176 
1985744Sgs150176 static enum ioc_reply
1986744Sgs150176 rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
1987744Sgs150176 {
1988744Sgs150176 	void (*ppfn)(rge_t *rgep, rge_peekpoke_t *ppd);
1989744Sgs150176 	rge_peekpoke_t *ppd;
1990744Sgs150176 	dma_area_t *areap;
1991744Sgs150176 	uint64_t sizemask;
1992744Sgs150176 	uint64_t mem_va;
1993744Sgs150176 	uint64_t maxoff;
1994744Sgs150176 	boolean_t peek;
1995744Sgs150176 
1996744Sgs150176 	switch (cmd) {
1997744Sgs150176 	default:
1998744Sgs150176 		/* NOTREACHED */
1999744Sgs150176 		rge_error(rgep, "rge_pp_ioctl: invalid cmd 0x%x", cmd);
2000744Sgs150176 		return (IOC_INVAL);
2001744Sgs150176 
2002744Sgs150176 	case RGE_PEEK:
2003744Sgs150176 		peek = B_TRUE;
2004744Sgs150176 		break;
2005744Sgs150176 
2006744Sgs150176 	case RGE_POKE:
2007744Sgs150176 		peek = B_FALSE;
2008744Sgs150176 		break;
2009744Sgs150176 	}
2010744Sgs150176 
2011744Sgs150176 	/*
2012744Sgs150176 	 * Validate format of ioctl
2013744Sgs150176 	 */
2014744Sgs150176 	if (iocp->ioc_count != sizeof (rge_peekpoke_t))
2015744Sgs150176 		return (IOC_INVAL);
2016744Sgs150176 	if (mp->b_cont == NULL)
2017744Sgs150176 		return (IOC_INVAL);
2018744Sgs150176 	ppd = (rge_peekpoke_t *)mp->b_cont->b_rptr;
2019744Sgs150176 
2020744Sgs150176 	/*
2021744Sgs150176 	 * Validate request parameters
2022744Sgs150176 	 */
2023744Sgs150176 	switch (ppd->pp_acc_space) {
2024744Sgs150176 	default:
2025744Sgs150176 		return (IOC_INVAL);
2026744Sgs150176 
2027744Sgs150176 	case RGE_PP_SPACE_CFG:
2028744Sgs150176 		/*
2029744Sgs150176 		 * Config space
2030744Sgs150176 		 */
2031744Sgs150176 		sizemask = 8|4|2|1;
2032744Sgs150176 		mem_va = 0;
2033744Sgs150176 		maxoff = PCI_CONF_HDR_SIZE;
2034744Sgs150176 		ppfn = peek ? rge_chip_peek_cfg : rge_chip_poke_cfg;
2035744Sgs150176 		break;
2036744Sgs150176 
2037744Sgs150176 	case RGE_PP_SPACE_REG:
2038744Sgs150176 		/*
2039744Sgs150176 		 * Memory-mapped I/O space
2040744Sgs150176 		 */
2041744Sgs150176 		sizemask = 8|4|2|1;
2042744Sgs150176 		mem_va = 0;
2043744Sgs150176 		maxoff = RGE_REGISTER_MAX;
2044744Sgs150176 		ppfn = peek ? rge_chip_peek_reg : rge_chip_poke_reg;
2045744Sgs150176 		break;
2046744Sgs150176 
2047744Sgs150176 	case RGE_PP_SPACE_MII:
2048744Sgs150176 		/*
2049744Sgs150176 		 * PHY's MII registers
2050744Sgs150176 		 * NB: all PHY registers are two bytes, but the
2051744Sgs150176 		 * addresses increment in ones (word addressing).
2052744Sgs150176 		 * So we scale the address here, then undo the
2053744Sgs150176 		 * transformation inside the peek/poke functions.
2054744Sgs150176 		 */
2055744Sgs150176 		ppd->pp_acc_offset *= 2;
2056744Sgs150176 		sizemask = 2;
2057744Sgs150176 		mem_va = 0;
2058744Sgs150176 		maxoff = (MII_MAXREG+1)*2;
2059744Sgs150176 		ppfn = peek ? rge_chip_peek_mii : rge_chip_poke_mii;
2060744Sgs150176 		break;
2061744Sgs150176 
2062744Sgs150176 	case RGE_PP_SPACE_RGE:
2063744Sgs150176 		/*
2064744Sgs150176 		 * RGE data structure!
2065744Sgs150176 		 */
2066744Sgs150176 		sizemask = 8|4|2|1;
2067744Sgs150176 		mem_va = (uintptr_t)rgep;
2068744Sgs150176 		maxoff = sizeof (*rgep);
2069744Sgs150176 		ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2070744Sgs150176 		break;
2071744Sgs150176 
2072744Sgs150176 	case RGE_PP_SPACE_STATISTICS:
2073744Sgs150176 	case RGE_PP_SPACE_TXDESC:
2074744Sgs150176 	case RGE_PP_SPACE_TXBUFF:
2075744Sgs150176 	case RGE_PP_SPACE_RXDESC:
2076744Sgs150176 	case RGE_PP_SPACE_RXBUFF:
2077744Sgs150176 		/*
2078744Sgs150176 		 * Various DMA_AREAs
2079744Sgs150176 		 */
2080744Sgs150176 		switch (ppd->pp_acc_space) {
2081744Sgs150176 		case RGE_PP_SPACE_TXDESC:
2082744Sgs150176 			areap = &rgep->dma_area_txdesc;
2083744Sgs150176 			break;
2084744Sgs150176 		case RGE_PP_SPACE_RXDESC:
2085744Sgs150176 			areap = &rgep->dma_area_rxdesc;
2086744Sgs150176 			break;
2087744Sgs150176 		case RGE_PP_SPACE_STATISTICS:
2088744Sgs150176 			areap = &rgep->dma_area_stats;
2089744Sgs150176 			break;
2090744Sgs150176 		}
2091744Sgs150176 
2092744Sgs150176 		sizemask = 8|4|2|1;
2093744Sgs150176 		mem_va = (uintptr_t)areap->mem_va;
2094744Sgs150176 		maxoff = areap->alength;
2095744Sgs150176 		ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2096744Sgs150176 		break;
2097744Sgs150176 	}
2098744Sgs150176 
2099744Sgs150176 	switch (ppd->pp_acc_size) {
2100744Sgs150176 	default:
2101744Sgs150176 		return (IOC_INVAL);
2102744Sgs150176 
2103744Sgs150176 	case 8:
2104744Sgs150176 	case 4:
2105744Sgs150176 	case 2:
2106744Sgs150176 	case 1:
2107744Sgs150176 		if ((ppd->pp_acc_size & sizemask) == 0)
2108744Sgs150176 			return (IOC_INVAL);
2109744Sgs150176 		break;
2110744Sgs150176 	}
2111744Sgs150176 
2112744Sgs150176 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
2113744Sgs150176 		return (IOC_INVAL);
2114744Sgs150176 
2115744Sgs150176 	if (ppd->pp_acc_offset >= maxoff)
2116744Sgs150176 		return (IOC_INVAL);
2117744Sgs150176 
2118744Sgs150176 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
2119744Sgs150176 		return (IOC_INVAL);
2120744Sgs150176 
2121744Sgs150176 	/*
2122744Sgs150176 	 * All OK - go do it!
2123744Sgs150176 	 */
2124744Sgs150176 	ppd->pp_acc_offset += mem_va;
2125744Sgs150176 	(*ppfn)(rgep, ppd);
2126744Sgs150176 	return (peek ? IOC_REPLY : IOC_ACK);
2127744Sgs150176 }
2128744Sgs150176 
2129744Sgs150176 static enum ioc_reply rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2130744Sgs150176 					struct iocblk *iocp);
2131744Sgs150176 #pragma	no_inline(rge_diag_ioctl)
2132744Sgs150176 
2133744Sgs150176 static enum ioc_reply
2134744Sgs150176 rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2135744Sgs150176 {
2136744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
2137744Sgs150176 
2138744Sgs150176 	switch (cmd) {
2139744Sgs150176 	default:
2140744Sgs150176 		/* NOTREACHED */
2141744Sgs150176 		rge_error(rgep, "rge_diag_ioctl: invalid cmd 0x%x", cmd);
2142744Sgs150176 		return (IOC_INVAL);
2143744Sgs150176 
2144744Sgs150176 	case RGE_DIAG:
2145744Sgs150176 		/*
2146744Sgs150176 		 * Currently a no-op
2147744Sgs150176 		 */
2148744Sgs150176 		return (IOC_ACK);
2149744Sgs150176 
2150744Sgs150176 	case RGE_PEEK:
2151744Sgs150176 	case RGE_POKE:
2152744Sgs150176 		return (rge_pp_ioctl(rgep, cmd, mp, iocp));
2153744Sgs150176 
2154744Sgs150176 	case RGE_PHY_RESET:
2155744Sgs150176 		return (IOC_RESTART_ACK);
2156744Sgs150176 
2157744Sgs150176 	case RGE_SOFT_RESET:
2158744Sgs150176 	case RGE_HARD_RESET:
2159744Sgs150176 		/*
2160744Sgs150176 		 * Reset and reinitialise the 570x hardware
2161744Sgs150176 		 */
2162744Sgs150176 		rge_restart(rgep);
2163744Sgs150176 		return (IOC_ACK);
2164744Sgs150176 	}
2165744Sgs150176 
2166744Sgs150176 	/* NOTREACHED */
2167744Sgs150176 }
2168744Sgs150176 
2169744Sgs150176 #endif	/* RGE_DEBUGGING || RGE_DO_PPIO */
2170744Sgs150176 
2171744Sgs150176 static enum ioc_reply rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2172744Sgs150176 				    struct iocblk *iocp);
2173744Sgs150176 #pragma	no_inline(rge_mii_ioctl)
2174744Sgs150176 
2175744Sgs150176 static enum ioc_reply
2176744Sgs150176 rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2177744Sgs150176 {
2178744Sgs150176 	struct rge_mii_rw *miirwp;
2179744Sgs150176 
2180744Sgs150176 	/*
2181744Sgs150176 	 * Validate format of ioctl
2182744Sgs150176 	 */
2183744Sgs150176 	if (iocp->ioc_count != sizeof (struct rge_mii_rw))
2184744Sgs150176 		return (IOC_INVAL);
2185744Sgs150176 	if (mp->b_cont == NULL)
2186744Sgs150176 		return (IOC_INVAL);
2187744Sgs150176 	miirwp = (struct rge_mii_rw *)mp->b_cont->b_rptr;
2188744Sgs150176 
2189744Sgs150176 	/*
2190744Sgs150176 	 * Validate request parameters ...
2191744Sgs150176 	 */
2192744Sgs150176 	if (miirwp->mii_reg > MII_MAXREG)
2193744Sgs150176 		return (IOC_INVAL);
2194744Sgs150176 
2195744Sgs150176 	switch (cmd) {
2196744Sgs150176 	default:
2197744Sgs150176 		/* NOTREACHED */
2198744Sgs150176 		rge_error(rgep, "rge_mii_ioctl: invalid cmd 0x%x", cmd);
2199744Sgs150176 		return (IOC_INVAL);
2200744Sgs150176 
2201744Sgs150176 	case RGE_MII_READ:
2202744Sgs150176 		miirwp->mii_data = rge_mii_get16(rgep, miirwp->mii_reg);
2203744Sgs150176 		return (IOC_REPLY);
2204744Sgs150176 
2205744Sgs150176 	case RGE_MII_WRITE:
2206744Sgs150176 		rge_mii_put16(rgep, miirwp->mii_reg, miirwp->mii_data);
2207744Sgs150176 		return (IOC_ACK);
2208744Sgs150176 	}
2209744Sgs150176 
2210744Sgs150176 	/* NOTREACHED */
2211744Sgs150176 }
2212744Sgs150176 
2213744Sgs150176 enum ioc_reply rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp,
2214744Sgs150176 				struct iocblk *iocp);
2215744Sgs150176 #pragma	no_inline(rge_chip_ioctl)
2216744Sgs150176 
2217744Sgs150176 enum ioc_reply
2218744Sgs150176 rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
2219744Sgs150176 {
2220744Sgs150176 	int cmd;
2221744Sgs150176 
2222744Sgs150176 	RGE_TRACE(("rge_chip_ioctl($%p, $%p, $%p, $%p)",
22235735Smx205022 	    (void *)rgep, (void *)wq, (void *)mp, (void *)iocp));
2224744Sgs150176 
2225744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
2226744Sgs150176 
2227744Sgs150176 	cmd = iocp->ioc_cmd;
2228744Sgs150176 	switch (cmd) {
2229744Sgs150176 	default:
2230744Sgs150176 		/* NOTREACHED */
2231744Sgs150176 		rge_error(rgep, "rge_chip_ioctl: invalid cmd 0x%x", cmd);
2232744Sgs150176 		return (IOC_INVAL);
2233744Sgs150176 
2234744Sgs150176 	case RGE_DIAG:
2235744Sgs150176 	case RGE_PEEK:
2236744Sgs150176 	case RGE_POKE:
2237744Sgs150176 	case RGE_PHY_RESET:
2238744Sgs150176 	case RGE_SOFT_RESET:
2239744Sgs150176 	case RGE_HARD_RESET:
2240744Sgs150176 #if	RGE_DEBUGGING || RGE_DO_PPIO
2241744Sgs150176 		return (rge_diag_ioctl(rgep, cmd, mp, iocp));
2242744Sgs150176 #else
2243744Sgs150176 		return (IOC_INVAL);
2244744Sgs150176 #endif	/* RGE_DEBUGGING || RGE_DO_PPIO */
2245744Sgs150176 
2246744Sgs150176 	case RGE_MII_READ:
2247744Sgs150176 	case RGE_MII_WRITE:
2248744Sgs150176 		return (rge_mii_ioctl(rgep, cmd, mp, iocp));
2249744Sgs150176 
2250744Sgs150176 	}
2251744Sgs150176 
2252744Sgs150176 	/* NOTREACHED */
2253744Sgs150176 }
2254