xref: /onnv-gate/usr/src/uts/common/io/rge/rge_chip.c (revision 11365:a93255969e3e)
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 /*
229860Sgdamore@opensolaris.org  * Copyright 2009 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	*/
45744Sgs150176 static uint32_t rge_watchdog_count	= 1 << 16;
46744Sgs150176 
47744Sgs150176 /*
48744Sgs150176  * Operating register get/set access routines
49744Sgs150176  */
50744Sgs150176 
51744Sgs150176 static uint32_t rge_reg_get32(rge_t *rgep, uintptr_t regno);
52744Sgs150176 #pragma	inline(rge_reg_get32)
53744Sgs150176 
54744Sgs150176 static uint32_t
55744Sgs150176 rge_reg_get32(rge_t *rgep, uintptr_t regno)
56744Sgs150176 {
57744Sgs150176 	RGE_TRACE(("rge_reg_get32($%p, 0x%lx)",
585735Smx205022 	    (void *)rgep, regno));
59744Sgs150176 
60744Sgs150176 	return (ddi_get32(rgep->io_handle, REG32(rgep, regno)));
61744Sgs150176 }
62744Sgs150176 
63744Sgs150176 static void rge_reg_put32(rge_t *rgep, uintptr_t regno, uint32_t data);
64744Sgs150176 #pragma	inline(rge_reg_put32)
65744Sgs150176 
66744Sgs150176 static void
67744Sgs150176 rge_reg_put32(rge_t *rgep, uintptr_t regno, uint32_t data)
68744Sgs150176 {
69744Sgs150176 	RGE_TRACE(("rge_reg_put32($%p, 0x%lx, 0x%x)",
705735Smx205022 	    (void *)rgep, regno, data));
71744Sgs150176 
72744Sgs150176 	ddi_put32(rgep->io_handle, REG32(rgep, regno), data);
73744Sgs150176 }
74744Sgs150176 
75744Sgs150176 static void rge_reg_set32(rge_t *rgep, uintptr_t regno, uint32_t bits);
76744Sgs150176 #pragma	inline(rge_reg_set32)
77744Sgs150176 
78744Sgs150176 static void
79744Sgs150176 rge_reg_set32(rge_t *rgep, uintptr_t regno, uint32_t bits)
80744Sgs150176 {
81744Sgs150176 	uint32_t regval;
82744Sgs150176 
83744Sgs150176 	RGE_TRACE(("rge_reg_set32($%p, 0x%lx, 0x%x)",
845735Smx205022 	    (void *)rgep, regno, bits));
85744Sgs150176 
86744Sgs150176 	regval = rge_reg_get32(rgep, regno);
87744Sgs150176 	regval |= bits;
88744Sgs150176 	rge_reg_put32(rgep, regno, regval);
89744Sgs150176 }
90744Sgs150176 
91744Sgs150176 static void rge_reg_clr32(rge_t *rgep, uintptr_t regno, uint32_t bits);
92744Sgs150176 #pragma	inline(rge_reg_clr32)
93744Sgs150176 
94744Sgs150176 static void
95744Sgs150176 rge_reg_clr32(rge_t *rgep, uintptr_t regno, uint32_t bits)
96744Sgs150176 {
97744Sgs150176 	uint32_t regval;
98744Sgs150176 
99744Sgs150176 	RGE_TRACE(("rge_reg_clr32($%p, 0x%lx, 0x%x)",
1005735Smx205022 	    (void *)rgep, regno, bits));
101744Sgs150176 
102744Sgs150176 	regval = rge_reg_get32(rgep, regno);
103744Sgs150176 	regval &= ~bits;
104744Sgs150176 	rge_reg_put32(rgep, regno, regval);
105744Sgs150176 }
106744Sgs150176 
107744Sgs150176 static uint16_t rge_reg_get16(rge_t *rgep, uintptr_t regno);
108744Sgs150176 #pragma	inline(rge_reg_get16)
109744Sgs150176 
110744Sgs150176 static uint16_t
111744Sgs150176 rge_reg_get16(rge_t *rgep, uintptr_t regno)
112744Sgs150176 {
113744Sgs150176 	RGE_TRACE(("rge_reg_get16($%p, 0x%lx)",
1145735Smx205022 	    (void *)rgep, regno));
115744Sgs150176 
116744Sgs150176 	return (ddi_get16(rgep->io_handle, REG16(rgep, regno)));
117744Sgs150176 }
118744Sgs150176 
119744Sgs150176 static void rge_reg_put16(rge_t *rgep, uintptr_t regno, uint16_t data);
120744Sgs150176 #pragma	inline(rge_reg_put16)
121744Sgs150176 
122744Sgs150176 static void
123744Sgs150176 rge_reg_put16(rge_t *rgep, uintptr_t regno, uint16_t data)
124744Sgs150176 {
125744Sgs150176 	RGE_TRACE(("rge_reg_put16($%p, 0x%lx, 0x%x)",
1265735Smx205022 	    (void *)rgep, regno, data));
127744Sgs150176 
128744Sgs150176 	ddi_put16(rgep->io_handle, REG16(rgep, regno), data);
129744Sgs150176 }
130744Sgs150176 
131744Sgs150176 static uint8_t rge_reg_get8(rge_t *rgep, uintptr_t regno);
132744Sgs150176 #pragma	inline(rge_reg_get8)
133744Sgs150176 
134744Sgs150176 static uint8_t
135744Sgs150176 rge_reg_get8(rge_t *rgep, uintptr_t regno)
136744Sgs150176 {
137744Sgs150176 	RGE_TRACE(("rge_reg_get8($%p, 0x%lx)",
1385735Smx205022 	    (void *)rgep, regno));
139744Sgs150176 
140744Sgs150176 	return (ddi_get8(rgep->io_handle, REG8(rgep, regno)));
141744Sgs150176 }
142744Sgs150176 
143744Sgs150176 static void rge_reg_put8(rge_t *rgep, uintptr_t regno, uint8_t data);
144744Sgs150176 #pragma	inline(rge_reg_put8)
145744Sgs150176 
146744Sgs150176 static void
147744Sgs150176 rge_reg_put8(rge_t *rgep, uintptr_t regno, uint8_t data)
148744Sgs150176 {
149744Sgs150176 	RGE_TRACE(("rge_reg_put8($%p, 0x%lx, 0x%x)",
1505735Smx205022 	    (void *)rgep, regno, data));
151744Sgs150176 
152744Sgs150176 	ddi_put8(rgep->io_handle, REG8(rgep, regno), data);
153744Sgs150176 }
154744Sgs150176 
155744Sgs150176 static void rge_reg_set8(rge_t *rgep, uintptr_t regno, uint8_t bits);
156744Sgs150176 #pragma	inline(rge_reg_set8)
157744Sgs150176 
158744Sgs150176 static void
159744Sgs150176 rge_reg_set8(rge_t *rgep, uintptr_t regno, uint8_t bits)
160744Sgs150176 {
161744Sgs150176 	uint8_t regval;
162744Sgs150176 
163744Sgs150176 	RGE_TRACE(("rge_reg_set8($%p, 0x%lx, 0x%x)",
1645735Smx205022 	    (void *)rgep, regno, bits));
165744Sgs150176 
166744Sgs150176 	regval = rge_reg_get8(rgep, regno);
167744Sgs150176 	regval |= bits;
168744Sgs150176 	rge_reg_put8(rgep, regno, regval);
169744Sgs150176 }
170744Sgs150176 
171744Sgs150176 static void rge_reg_clr8(rge_t *rgep, uintptr_t regno, uint8_t bits);
172744Sgs150176 #pragma	inline(rge_reg_clr8)
173744Sgs150176 
174744Sgs150176 static void
175744Sgs150176 rge_reg_clr8(rge_t *rgep, uintptr_t regno, uint8_t bits)
176744Sgs150176 {
177744Sgs150176 	uint8_t regval;
178744Sgs150176 
179744Sgs150176 	RGE_TRACE(("rge_reg_clr8($%p, 0x%lx, 0x%x)",
1805735Smx205022 	    (void *)rgep, regno, bits));
181744Sgs150176 
182744Sgs150176 	regval = rge_reg_get8(rgep, regno);
183744Sgs150176 	regval &= ~bits;
184744Sgs150176 	rge_reg_put8(rgep, regno, regval);
185744Sgs150176 }
186744Sgs150176 
187744Sgs150176 uint16_t rge_mii_get16(rge_t *rgep, uintptr_t mii);
188744Sgs150176 #pragma	no_inline(rge_mii_get16)
189744Sgs150176 
190744Sgs150176 uint16_t
191744Sgs150176 rge_mii_get16(rge_t *rgep, uintptr_t mii)
192744Sgs150176 {
193744Sgs150176 	uint32_t regval;
194744Sgs150176 	uint32_t val32;
195744Sgs150176 	uint32_t i;
196744Sgs150176 
197744Sgs150176 	regval = (mii & PHY_REG_MASK) << PHY_REG_SHIFT;
198744Sgs150176 	rge_reg_put32(rgep, PHY_ACCESS_REG, regval);
199744Sgs150176 
200744Sgs150176 	/*
201744Sgs150176 	 * Waiting for PHY reading OK
202744Sgs150176 	 */
203744Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2044533Sgs150176 		drv_usecwait(1000);
205744Sgs150176 		val32 = rge_reg_get32(rgep, PHY_ACCESS_REG);
206744Sgs150176 		if (val32 & PHY_ACCESS_WR_FLAG)
2072544Sgs150176 			return ((uint16_t)(val32 & 0xffff));
208744Sgs150176 	}
209744Sgs150176 
210744Sgs150176 	RGE_REPORT((rgep, "rge_mii_get16(0x%x) fail, val = %x", mii, val32));
211744Sgs150176 	return ((uint16_t)~0u);
212744Sgs150176 }
213744Sgs150176 
214744Sgs150176 void rge_mii_put16(rge_t *rgep, uintptr_t mii, uint16_t data);
215744Sgs150176 #pragma	no_inline(rge_mii_put16)
216744Sgs150176 
217744Sgs150176 void
218744Sgs150176 rge_mii_put16(rge_t *rgep, uintptr_t mii, uint16_t data)
219744Sgs150176 {
220744Sgs150176 	uint32_t regval;
221744Sgs150176 	uint32_t val32;
222744Sgs150176 	uint32_t i;
223744Sgs150176 
224744Sgs150176 	regval = (mii & PHY_REG_MASK) << PHY_REG_SHIFT;
225744Sgs150176 	regval |= data & PHY_DATA_MASK;
226744Sgs150176 	regval |= PHY_ACCESS_WR_FLAG;
227744Sgs150176 	rge_reg_put32(rgep, PHY_ACCESS_REG, regval);
228744Sgs150176 
229744Sgs150176 	/*
230744Sgs150176 	 * Waiting for PHY writing OK
231744Sgs150176 	 */
232744Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2334533Sgs150176 		drv_usecwait(1000);
234744Sgs150176 		val32 = rge_reg_get32(rgep, PHY_ACCESS_REG);
235744Sgs150176 		if (!(val32 & PHY_ACCESS_WR_FLAG))
236744Sgs150176 			return;
237744Sgs150176 	}
238744Sgs150176 	RGE_REPORT((rgep, "rge_mii_put16(0x%lx, 0x%x) fail",
239744Sgs150176 	    mii, data));
240744Sgs150176 }
241744Sgs150176 
2422544Sgs150176 void rge_ephy_put16(rge_t *rgep, uintptr_t emii, uint16_t data);
2432544Sgs150176 #pragma	no_inline(rge_ephy_put16)
2442544Sgs150176 
2452544Sgs150176 void
2462544Sgs150176 rge_ephy_put16(rge_t *rgep, uintptr_t emii, uint16_t data)
2472544Sgs150176 {
2482544Sgs150176 	uint32_t regval;
2492544Sgs150176 	uint32_t val32;
2502544Sgs150176 	uint32_t i;
2512544Sgs150176 
2522544Sgs150176 	regval = (emii & EPHY_REG_MASK) << EPHY_REG_SHIFT;
2532544Sgs150176 	regval |= data & EPHY_DATA_MASK;
2542544Sgs150176 	regval |= EPHY_ACCESS_WR_FLAG;
2552544Sgs150176 	rge_reg_put32(rgep, EPHY_ACCESS_REG, regval);
2562544Sgs150176 
2572544Sgs150176 	/*
2582544Sgs150176 	 * Waiting for PHY writing OK
2592544Sgs150176 	 */
2602544Sgs150176 	for (i = 0; i < PHY_RESET_LOOP; i++) {
2614533Sgs150176 		drv_usecwait(1000);
2622544Sgs150176 		val32 = rge_reg_get32(rgep, EPHY_ACCESS_REG);
2632544Sgs150176 		if (!(val32 & EPHY_ACCESS_WR_FLAG))
2642544Sgs150176 			return;
2652544Sgs150176 	}
2662544Sgs150176 	RGE_REPORT((rgep, "rge_ephy_put16(0x%lx, 0x%x) fail",
2672544Sgs150176 	    emii, data));
2682544Sgs150176 }
2692544Sgs150176 
270744Sgs150176 /*
271744Sgs150176  * Atomically shift a 32-bit word left, returning
272744Sgs150176  * the value it had *before* the shift was applied
273744Sgs150176  */
274744Sgs150176 static uint32_t rge_atomic_shl32(uint32_t *sp, uint_t count);
275744Sgs150176 #pragma	inline(rge_mii_put16)
276744Sgs150176 
277744Sgs150176 static uint32_t
278744Sgs150176 rge_atomic_shl32(uint32_t *sp, uint_t count)
279744Sgs150176 {
280744Sgs150176 	uint32_t oldval;
281744Sgs150176 	uint32_t newval;
282744Sgs150176 
283744Sgs150176 	/* ATOMICALLY */
284744Sgs150176 	do {
285744Sgs150176 		oldval = *sp;
286744Sgs150176 		newval = oldval << count;
287744Sgs150176 	} while (cas32(sp, oldval, newval) != oldval);
288744Sgs150176 
289744Sgs150176 	return (oldval);
290744Sgs150176 }
291744Sgs150176 
292744Sgs150176 /*
293744Sgs150176  * PHY operation routines
294744Sgs150176  */
295744Sgs150176 #if	RGE_DEBUGGING
296744Sgs150176 
2976990Sgd78059 void
298744Sgs150176 rge_phydump(rge_t *rgep)
299744Sgs150176 {
300744Sgs150176 	uint16_t regs[32];
301744Sgs150176 	int i;
302744Sgs150176 
303744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
304744Sgs150176 
305744Sgs150176 	for (i = 0; i < 32; ++i) {
306744Sgs150176 		regs[i] = rge_mii_get16(rgep, i);
307744Sgs150176 	}
308744Sgs150176 
309744Sgs150176 	for (i = 0; i < 32; i += 8)
310744Sgs150176 		RGE_DEBUG(("rge_phydump: "
3115735Smx205022 		    "0x%04x %04x %04x %04x %04x %04x %04x %04x",
3125735Smx205022 		    regs[i+0], regs[i+1], regs[i+2], regs[i+3],
3135735Smx205022 		    regs[i+4], regs[i+5], regs[i+6], regs[i+7]));
314744Sgs150176 }
315744Sgs150176 
316744Sgs150176 #endif	/* RGE_DEBUGGING */
317744Sgs150176 
318744Sgs150176 static void
319744Sgs150176 rge_phy_check(rge_t *rgep)
320744Sgs150176 {
321744Sgs150176 	uint16_t gig_ctl;
322744Sgs150176 
323744Sgs150176 	if (rgep->param_link_up  == LINK_STATE_DOWN) {
324744Sgs150176 		/*
325744Sgs150176 		 * RTL8169S/8110S PHY has the "PCS bug".  Need reset PHY
326744Sgs150176 		 * every 15 seconds whin link down & advertise is 1000.
327744Sgs150176 		 */
328744Sgs150176 		if (rgep->chipid.phy_ver == PHY_VER_S) {
329744Sgs150176 			gig_ctl = rge_mii_get16(rgep, MII_1000BASE_T_CONTROL);
330744Sgs150176 			if (gig_ctl & MII_1000BT_CTL_ADV_FDX) {
331744Sgs150176 				rgep->link_down_count++;
332744Sgs150176 				if (rgep->link_down_count > 15) {
333744Sgs150176 					(void) rge_phy_reset(rgep);
334744Sgs150176 					rgep->stats.phy_reset++;
335744Sgs150176 					rgep->link_down_count = 0;
336744Sgs150176 				}
337744Sgs150176 			}
338744Sgs150176 		}
339744Sgs150176 	} else {
340744Sgs150176 		rgep->link_down_count = 0;
341744Sgs150176 	}
342744Sgs150176 }
343744Sgs150176 
344744Sgs150176 /*
345744Sgs150176  * Basic low-level function to reset the PHY.
346744Sgs150176  * Doesn't incorporate any special-case workarounds.
347744Sgs150176  *
348744Sgs150176  * Returns TRUE on success, FALSE if the RESET bit doesn't clear
349744Sgs150176  */
350744Sgs150176 boolean_t
351744Sgs150176 rge_phy_reset(rge_t *rgep)
352744Sgs150176 {
353744Sgs150176 	uint16_t control;
354744Sgs150176 	uint_t count;
355744Sgs150176 
356744Sgs150176 	/*
357744Sgs150176 	 * Set the PHY RESET bit, then wait up to 5 ms for it to self-clear
358744Sgs150176 	 */
359744Sgs150176 	control = rge_mii_get16(rgep, MII_CONTROL);
360744Sgs150176 	rge_mii_put16(rgep, MII_CONTROL, control | MII_CONTROL_RESET);
3614533Sgs150176 	for (count = 0; count < 5; count++) {
362744Sgs150176 		drv_usecwait(100);
363744Sgs150176 		control = rge_mii_get16(rgep, MII_CONTROL);
364744Sgs150176 		if (BIC(control, MII_CONTROL_RESET))
365744Sgs150176 			return (B_TRUE);
366744Sgs150176 	}
367744Sgs150176 
368744Sgs150176 	RGE_REPORT((rgep, "rge_phy_reset: FAILED, control now 0x%x", control));
369744Sgs150176 	return (B_FALSE);
370744Sgs150176 }
371744Sgs150176 
372744Sgs150176 /*
373744Sgs150176  * Synchronise the PHY's speed/duplex/autonegotiation capabilities
374744Sgs150176  * and advertisements with the required settings as specified by the various
375744Sgs150176  * param_* variables that can be poked via the NDD interface.
376744Sgs150176  *
377744Sgs150176  * We always reset the PHY and reprogram *all* the relevant registers,
378744Sgs150176  * not just those changed.  This should cause the link to go down, and then
379744Sgs150176  * back up again once the link is stable and autonegotiation (if enabled)
380744Sgs150176  * is complete.  We should get a link state change interrupt somewhere along
381744Sgs150176  * the way ...
382744Sgs150176  *
383744Sgs150176  * NOTE: <genlock> must already be held by the caller
384744Sgs150176  */
385744Sgs150176 void
386744Sgs150176 rge_phy_update(rge_t *rgep)
387744Sgs150176 {
388744Sgs150176 	boolean_t adv_autoneg;
389744Sgs150176 	boolean_t adv_pause;
390744Sgs150176 	boolean_t adv_asym_pause;
391744Sgs150176 	boolean_t adv_1000fdx;
392744Sgs150176 	boolean_t adv_1000hdx;
393744Sgs150176 	boolean_t adv_100fdx;
394744Sgs150176 	boolean_t adv_100hdx;
395744Sgs150176 	boolean_t adv_10fdx;
396744Sgs150176 	boolean_t adv_10hdx;
397744Sgs150176 
398744Sgs150176 	uint16_t control;
399744Sgs150176 	uint16_t gigctrl;
400744Sgs150176 	uint16_t anar;
401744Sgs150176 
402744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
403744Sgs150176 
404744Sgs150176 	RGE_DEBUG(("rge_phy_update: autoneg %d "
4055735Smx205022 	    "pause %d asym_pause %d "
4065735Smx205022 	    "1000fdx %d 1000hdx %d "
4075735Smx205022 	    "100fdx %d 100hdx %d "
4085735Smx205022 	    "10fdx %d 10hdx %d ",
4095735Smx205022 	    rgep->param_adv_autoneg,
4105735Smx205022 	    rgep->param_adv_pause, rgep->param_adv_asym_pause,
4115735Smx205022 	    rgep->param_adv_1000fdx, rgep->param_adv_1000hdx,
4125735Smx205022 	    rgep->param_adv_100fdx, rgep->param_adv_100hdx,
4135735Smx205022 	    rgep->param_adv_10fdx, rgep->param_adv_10hdx));
414744Sgs150176 
415744Sgs150176 	control = gigctrl = anar = 0;
416744Sgs150176 
417744Sgs150176 	/*
418744Sgs150176 	 * PHY settings are normally based on the param_* variables,
419744Sgs150176 	 * but if any loopback mode is in effect, that takes precedence.
420744Sgs150176 	 *
421744Sgs150176 	 * RGE supports MAC-internal loopback, PHY-internal loopback,
422744Sgs150176 	 * and External loopback at a variety of speeds (with a special
423744Sgs150176 	 * cable).  In all cases, autoneg is turned OFF, full-duplex
424744Sgs150176 	 * is turned ON, and the speed/mastership is forced.
425744Sgs150176 	 */
426744Sgs150176 	switch (rgep->param_loop_mode) {
427744Sgs150176 	case RGE_LOOP_NONE:
428744Sgs150176 	default:
429744Sgs150176 		adv_autoneg = rgep->param_adv_autoneg;
430744Sgs150176 		adv_pause = rgep->param_adv_pause;
431744Sgs150176 		adv_asym_pause = rgep->param_adv_asym_pause;
432744Sgs150176 		adv_1000fdx = rgep->param_adv_1000fdx;
433744Sgs150176 		adv_1000hdx = rgep->param_adv_1000hdx;
434744Sgs150176 		adv_100fdx = rgep->param_adv_100fdx;
435744Sgs150176 		adv_100hdx = rgep->param_adv_100hdx;
436744Sgs150176 		adv_10fdx = rgep->param_adv_10fdx;
437744Sgs150176 		adv_10hdx = rgep->param_adv_10hdx;
438744Sgs150176 		break;
439744Sgs150176 
440744Sgs150176 	case RGE_LOOP_INTERNAL_PHY:
441744Sgs150176 	case RGE_LOOP_INTERNAL_MAC:
442744Sgs150176 		adv_autoneg = adv_pause = adv_asym_pause = B_FALSE;
443744Sgs150176 		adv_1000fdx = adv_100fdx = adv_10fdx = B_FALSE;
444744Sgs150176 		adv_1000hdx = adv_100hdx = adv_10hdx = B_FALSE;
445744Sgs150176 		rgep->param_link_duplex = LINK_DUPLEX_FULL;
446744Sgs150176 
447744Sgs150176 		switch (rgep->param_loop_mode) {
448744Sgs150176 		case RGE_LOOP_INTERNAL_PHY:
4495735Smx205022 			if (rgep->chipid.mac_ver != MAC_VER_8101E) {
4505735Smx205022 				rgep->param_link_speed = 1000;
4515735Smx205022 				adv_1000fdx = B_TRUE;
4525735Smx205022 			} else {
4535735Smx205022 				rgep->param_link_speed = 100;
4545735Smx205022 				adv_100fdx = B_TRUE;
4555735Smx205022 			}
456744Sgs150176 			control = MII_CONTROL_LOOPBACK;
457744Sgs150176 			break;
458744Sgs150176 
459744Sgs150176 		case RGE_LOOP_INTERNAL_MAC:
4605735Smx205022 			if (rgep->chipid.mac_ver != MAC_VER_8101E) {
4615735Smx205022 				rgep->param_link_speed = 1000;
4625735Smx205022 				adv_1000fdx = B_TRUE;
4635735Smx205022 			} else {
4645735Smx205022 				rgep->param_link_speed = 100;
4655735Smx205022 				adv_100fdx = B_TRUE;
466744Sgs150176 			break;
467744Sgs150176 		}
468744Sgs150176 	}
469744Sgs150176 
470744Sgs150176 	RGE_DEBUG(("rge_phy_update: autoneg %d "
4715735Smx205022 	    "pause %d asym_pause %d "
4725735Smx205022 	    "1000fdx %d 1000hdx %d "
4735735Smx205022 	    "100fdx %d 100hdx %d "
4745735Smx205022 	    "10fdx %d 10hdx %d ",
4755735Smx205022 	    adv_autoneg,
4765735Smx205022 	    adv_pause, adv_asym_pause,
4775735Smx205022 	    adv_1000fdx, adv_1000hdx,
4785735Smx205022 	    adv_100fdx, adv_100hdx,
4795735Smx205022 	    adv_10fdx, adv_10hdx));
480744Sgs150176 
481744Sgs150176 	/*
482744Sgs150176 	 * We should have at least one technology capability set;
483744Sgs150176 	 * if not, we select a default of 1000Mb/s full-duplex
484744Sgs150176 	 */
485744Sgs150176 	if (!adv_1000fdx && !adv_100fdx && !adv_10fdx &&
4865735Smx205022 	    !adv_1000hdx && !adv_100hdx && !adv_10hdx) {
4875735Smx205022 		if (rgep->chipid.mac_ver != MAC_VER_8101E)
4885735Smx205022 			adv_1000fdx = B_TRUE;
4895735Smx205022 		} else {
4905735Smx205022 			adv_1000fdx = B_FALSE;
4915735Smx205022 			adv_100fdx = B_TRUE;
4925735Smx205022 		}
4935735Smx205022 	}
494744Sgs150176 
495744Sgs150176 	/*
496744Sgs150176 	 * Now transform the adv_* variables into the proper settings
497744Sgs150176 	 * of the PHY registers ...
498744Sgs150176 	 *
499744Sgs150176 	 * If autonegotiation is (now) enabled, we want to trigger
500744Sgs150176 	 * a new autonegotiation cycle once the PHY has been
501744Sgs150176 	 * programmed with the capabilities to be advertised.
502744Sgs150176 	 *
503744Sgs150176 	 * RTL8169/8110 doesn't support 1000Mb/s half-duplex.
504744Sgs150176 	 */
505744Sgs150176 	if (adv_autoneg)
506744Sgs150176 		control |= MII_CONTROL_ANE|MII_CONTROL_RSAN;
507744Sgs150176 
508744Sgs150176 	if (adv_1000fdx)
5099860Sgdamore@opensolaris.org 		control |= MII_CONTROL_1GB|MII_CONTROL_FDUPLEX;
510744Sgs150176 	else if (adv_1000hdx)
5119860Sgdamore@opensolaris.org 		control |= MII_CONTROL_1GB;
512744Sgs150176 	else if (adv_100fdx)
513744Sgs150176 		control |= MII_CONTROL_100MB|MII_CONTROL_FDUPLEX;
514744Sgs150176 	else if (adv_100hdx)
515744Sgs150176 		control |= MII_CONTROL_100MB;
516744Sgs150176 	else if (adv_10fdx)
517744Sgs150176 		control |= MII_CONTROL_FDUPLEX;
518744Sgs150176 	else if (adv_10hdx)
519744Sgs150176 		control |= 0;
520744Sgs150176 	else
521744Sgs150176 		{ _NOTE(EMPTY); }	/* Can't get here anyway ...	*/
522744Sgs150176 
523744Sgs150176 	if (adv_1000fdx) {
524744Sgs150176 		gigctrl |= MII_1000BT_CTL_ADV_FDX;
525744Sgs150176 		/*
526744Sgs150176 		 * Chipset limitation: need set other capabilities to true
527744Sgs150176 		 */
5282544Sgs150176 		if (rgep->chipid.is_pcie)
5292544Sgs150176 			adv_1000hdx = B_TRUE;
530744Sgs150176 		adv_100fdx = B_TRUE;
531744Sgs150176 		adv_100hdx  = B_TRUE;
532744Sgs150176 		adv_10fdx = B_TRUE;
533744Sgs150176 		adv_10hdx = B_TRUE;
534744Sgs150176 	}
535744Sgs150176 
536744Sgs150176 	if (adv_1000hdx)
537744Sgs150176 		gigctrl |= MII_1000BT_CTL_ADV_HDX;
538744Sgs150176 
539744Sgs150176 	if (adv_100fdx)
540744Sgs150176 		anar |= MII_ABILITY_100BASE_TX_FD;
541744Sgs150176 	if (adv_100hdx)
542744Sgs150176 		anar |= MII_ABILITY_100BASE_TX;
543744Sgs150176 	if (adv_10fdx)
544744Sgs150176 		anar |= MII_ABILITY_10BASE_T_FD;
545744Sgs150176 	if (adv_10hdx)
546744Sgs150176 		anar |= MII_ABILITY_10BASE_T;
547744Sgs150176 
548744Sgs150176 	if (adv_pause)
549744Sgs150176 		anar |= MII_ABILITY_PAUSE;
550744Sgs150176 	if (adv_asym_pause)
5519860Sgdamore@opensolaris.org 		anar |= MII_ABILITY_ASMPAUSE;
552744Sgs150176 
553744Sgs150176 	/*
554744Sgs150176 	 * Munge in any other fixed bits we require ...
555744Sgs150176 	 */
556744Sgs150176 	anar |= MII_AN_SELECTOR_8023;
557744Sgs150176 
558744Sgs150176 	/*
559744Sgs150176 	 * Restart the PHY and write the new values.  Note the
560744Sgs150176 	 * time, so that we can say whether subsequent link state
561744Sgs150176 	 * changes can be attributed to our reprogramming the PHY
562744Sgs150176 	 */
563744Sgs150176 	rge_phy_init(rgep);
5647192Sgs150176 	if (rgep->chipid.mac_ver == MAC_VER_8168B_B ||
5657192Sgs150176 	    rgep->chipid.mac_ver == MAC_VER_8168B_C) {
5667192Sgs150176 		/* power up PHY for RTL8168B chipset */
5677192Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
5687192Sgs150176 		rge_mii_put16(rgep, PHY_0E_REG, 0x0000);
5697192Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
5707192Sgs150176 	}
571744Sgs150176 	rge_mii_put16(rgep, MII_AN_ADVERT, anar);
5722544Sgs150176 	rge_mii_put16(rgep, MII_1000BASE_T_CONTROL, gigctrl);
573744Sgs150176 	rge_mii_put16(rgep, MII_CONTROL, control);
574744Sgs150176 
575744Sgs150176 	RGE_DEBUG(("rge_phy_update: anar <- 0x%x", anar));
576744Sgs150176 	RGE_DEBUG(("rge_phy_update: control <- 0x%x", control));
577744Sgs150176 	RGE_DEBUG(("rge_phy_update: gigctrl <- 0x%x", gigctrl));
578744Sgs150176 }
579744Sgs150176 
580744Sgs150176 void rge_phy_init(rge_t *rgep);
581744Sgs150176 #pragma	no_inline(rge_phy_init)
582744Sgs150176 
583744Sgs150176 void
584744Sgs150176 rge_phy_init(rge_t *rgep)
585744Sgs150176 {
586744Sgs150176 	rgep->phy_mii_addr = 1;
587744Sgs150176 
588744Sgs150176 	/*
589744Sgs150176 	 * Below phy config steps are copied from the Programming Guide
590744Sgs150176 	 * (there's no detail comments for these steps.)
591744Sgs150176 	 */
5922544Sgs150176 	switch (rgep->chipid.mac_ver) {
5932544Sgs150176 	case MAC_VER_8169S_D:
5942544Sgs150176 	case MAC_VER_8169S_E :
5952544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
596744Sgs150176 		rge_mii_put16(rgep, PHY_15_REG, 0x1000);
597744Sgs150176 		rge_mii_put16(rgep, PHY_18_REG, 0x65c7);
5982544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
599744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0x00a1);
600744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0x0008);
601744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x1020);
602744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x1000);
6032544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0800);
6042544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
6052544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7000);
606744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xff41);
607744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xde60);
608744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x0140);
609744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x0077);
6102544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7800);
6112544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x7000);
6122544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa000);
613744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xdf01);
614744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xdf20);
615744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0xff95);
616744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0xfa00);
6172544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa800);
6182544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xa000);
6192544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb000);
620744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xff41);
621744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xde20);
622744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x0140);
623744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0x00bb);
6242544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb800);
6252544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xb000);
6262544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf000);
627744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_2, 0xdf01);
628744Sgs150176 		rge_mii_put16(rgep, PHY_ID_REG_1, 0xdf20);
629744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0xff95);
630744Sgs150176 		rge_mii_put16(rgep, PHY_BMCR_REG, 0xbf00);
6312544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf800);
6322544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0xf000);
6332544Sgs150176 		rge_mii_put16(rgep, PHY_ANAR_REG, 0x0000);
634744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
635744Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x0000);
6362544Sgs150176 		break;
637744Sgs150176 
6382544Sgs150176 	case MAC_VER_8169SB:
639744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6402544Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xD41E);
6412544Sgs150176 		rge_mii_put16(rgep, PHY_0E_REG, 0x7bff);
642744Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, GBCR_DEFAULT);
643744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0002);
644744Sgs150176 		rge_mii_put16(rgep, PHY_BMSR_REG, 0x90D0);
645744Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6462544Sgs150176 		break;
6472544Sgs150176 
6484533Sgs150176 	case MAC_VER_8169SC:
6494533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6504533Sgs150176 		rge_mii_put16(rgep, PHY_ANER_REG, 0x0078);
6514533Sgs150176 		rge_mii_put16(rgep, PHY_ANNPRR_REG, 0x05dc);
6524533Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, 0x2672);
6534533Sgs150176 		rge_mii_put16(rgep, PHY_GBSR_REG, 0x6a14);
6544533Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x7cb0);
6554533Sgs150176 		rge_mii_put16(rgep, PHY_0C_REG, 0xdb80);
6564533Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xc414);
6574533Sgs150176 		rge_mii_put16(rgep, PHY_1C_REG, 0xef03);
6584533Sgs150176 		rge_mii_put16(rgep, PHY_1D_REG, 0x3dc8);
6594533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0003);
6604533Sgs150176 		rge_mii_put16(rgep, PHY_13_REG, 0x0600);
6614533Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6624533Sgs150176 		break;
6634533Sgs150176 
6642544Sgs150176 	case MAC_VER_8168:
6652544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6662544Sgs150176 		rge_mii_put16(rgep, PHY_ANER_REG, 0x00aa);
6672544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPTR_REG, 0x3173);
6682544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPRR_REG, 0x08fc);
6692544Sgs150176 		rge_mii_put16(rgep, PHY_GBCR_REG, 0xe2d0);
6702544Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x941a);
6712544Sgs150176 		rge_mii_put16(rgep, PHY_18_REG, 0x65fe);
6722544Sgs150176 		rge_mii_put16(rgep, PHY_1C_REG, 0x1e02);
6732544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0002);
6742544Sgs150176 		rge_mii_put16(rgep, PHY_ANNPTR_REG, 0x103e);
6752544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6762544Sgs150176 		break;
6772544Sgs150176 
6782544Sgs150176 	case MAC_VER_8168B_B:
6792544Sgs150176 	case MAC_VER_8168B_C:
6802544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0001);
6812544Sgs150176 		rge_mii_put16(rgep, PHY_0B_REG, 0x94b0);
6822544Sgs150176 		rge_mii_put16(rgep, PHY_1B_REG, 0xc416);
6832544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0003);
6842544Sgs150176 		rge_mii_put16(rgep, PHY_12_REG, 0x6096);
6852544Sgs150176 		rge_mii_put16(rgep, PHY_1F_REG, 0x0000);
6862544Sgs150176 		break;
687744Sgs150176 	}
688744Sgs150176 }
689744Sgs150176 
690744Sgs150176 void rge_chip_ident(rge_t *rgep);
691744Sgs150176 #pragma	no_inline(rge_chip_ident)
692744Sgs150176 
693744Sgs150176 void
694744Sgs150176 rge_chip_ident(rge_t *rgep)
695744Sgs150176 {
696744Sgs150176 	chip_id_t *chip = &rgep->chipid;
697744Sgs150176 	uint32_t val32;
698744Sgs150176 	uint16_t val16;
699744Sgs150176 
7002544Sgs150176 	/*
7012544Sgs150176 	 * Read and record MAC version
7022544Sgs150176 	 */
703744Sgs150176 	val32 = rge_reg_get32(rgep, TX_CONFIG_REG);
704744Sgs150176 	val32 &= HW_VERSION_ID_0 | HW_VERSION_ID_1;
705744Sgs150176 	chip->mac_ver = val32;
706*11365SZhen.W@Sun.COM 	chip->is_pcie = pci_lcap_locate(rgep->cfg_handle,
707*11365SZhen.W@Sun.COM 	    PCI_CAP_ID_PCI_E, &val16) == DDI_SUCCESS;
7082544Sgs150176 
7092544Sgs150176 	/*
7102544Sgs150176 	 * Read and record PHY version
7112544Sgs150176 	 */
712744Sgs150176 	val16 = rge_mii_get16(rgep, PHY_ID_REG_2);
713744Sgs150176 	val16 &= PHY_VER_MASK;
714744Sgs150176 	chip->phy_ver = val16;
715744Sgs150176 
7162544Sgs150176 	/* set pci latency timer */
7172544Sgs150176 	if (chip->mac_ver == MAC_VER_8169 ||
7184533Sgs150176 	    chip->mac_ver == MAC_VER_8169S_D ||
7194533Sgs150176 	    chip->mac_ver == MAC_VER_8169SC)
7202544Sgs150176 		pci_config_put8(rgep->cfg_handle, PCI_CONF_LATENCY_TIMER, 0x40);
7212544Sgs150176 
7224533Sgs150176 	if (chip->mac_ver == MAC_VER_8169SC) {
7234533Sgs150176 		val16 = rge_reg_get16(rgep, RT_CONFIG_1_REG);
7244533Sgs150176 		val16 &= 0x0300;
7254533Sgs150176 		if (val16 == 0x1)	/* 66Mhz PCI */
7264533Sgs150176 			pci_config_put32(rgep->cfg_handle, 0x7c, 0x00ff00ff);
7274533Sgs150176 		else if (val16 == 0x0) /* 33Mhz PCI */
7284533Sgs150176 			pci_config_put32(rgep->cfg_handle, 0x7c, 0x00ffff00);
7294533Sgs150176 	}
7304533Sgs150176 
7312544Sgs150176 	/*
7322544Sgs150176 	 * PCIE chipset require the Rx buffer start address must be
7332544Sgs150176 	 * 8-byte alignment and the Rx buffer size must be multiple of 8.
7342544Sgs150176 	 * We'll just use bcopy in receive procedure for the PCIE chipset.
7352544Sgs150176 	 */
7362544Sgs150176 	if (chip->is_pcie) {
7372544Sgs150176 		rgep->chip_flags |= CHIP_FLAG_FORCE_BCOPY;
7382544Sgs150176 		if (rgep->default_mtu > ETHERMTU) {
7392544Sgs150176 			rge_notice(rgep, "Jumbo packets not supported "
7402544Sgs150176 			    "for this PCIE chipset");
7412544Sgs150176 			rgep->default_mtu = ETHERMTU;
7422544Sgs150176 		}
7432544Sgs150176 	}
7442544Sgs150176 	if (rgep->chip_flags & CHIP_FLAG_FORCE_BCOPY)
7452544Sgs150176 		rgep->head_room = 0;
7462544Sgs150176 	else
7472544Sgs150176 		rgep->head_room = RGE_HEADROOM;
7482544Sgs150176 
7492544Sgs150176 	/*
7502544Sgs150176 	 * Initialize other variables.
7512544Sgs150176 	 */
7522544Sgs150176 	if (rgep->default_mtu < ETHERMTU || rgep->default_mtu > RGE_JUMBO_MTU)
7532544Sgs150176 		rgep->default_mtu = ETHERMTU;
7542544Sgs150176 	if (rgep->default_mtu > ETHERMTU) {
755744Sgs150176 		rgep->rxbuf_size = RGE_BUFF_SIZE_JUMBO;
756744Sgs150176 		rgep->txbuf_size = RGE_BUFF_SIZE_JUMBO;
757744Sgs150176 		rgep->ethmax_size = RGE_JUMBO_SIZE;
758744Sgs150176 	} else {
759744Sgs150176 		rgep->rxbuf_size = RGE_BUFF_SIZE_STD;
760744Sgs150176 		rgep->txbuf_size = RGE_BUFF_SIZE_STD;
761744Sgs150176 		rgep->ethmax_size = ETHERMAX;
762744Sgs150176 	}
763744Sgs150176 	chip->rxconfig = RX_CONFIG_DEFAULT;
764744Sgs150176 	chip->txconfig = TX_CONFIG_DEFAULT;
765744Sgs150176 
766*11365SZhen.W@Sun.COM 	/* interval to update statistics for polling mode */
767*11365SZhen.W@Sun.COM 	rgep->tick_delta = drv_usectohz(1000*1000/CLK_TICK);
768*11365SZhen.W@Sun.COM 
769*11365SZhen.W@Sun.COM 	/* ensure we are not in polling mode */
770*11365SZhen.W@Sun.COM 	rgep->curr_tick = ddi_get_lbolt() - 2*rgep->tick_delta;
771744Sgs150176 	RGE_TRACE(("%s: MAC version = %x, PHY version = %x",
772744Sgs150176 	    rgep->ifname, chip->mac_ver, chip->phy_ver));
773744Sgs150176 }
774744Sgs150176 
775744Sgs150176 /*
776744Sgs150176  * Perform first-stage chip (re-)initialisation, using only config-space
777744Sgs150176  * accesses:
778744Sgs150176  *
779744Sgs150176  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
780744Sgs150176  *   returning the data in the structure pointed to by <idp>.
781744Sgs150176  * + Enable Memory Space accesses.
782744Sgs150176  * + Enable Bus Mastering according.
783744Sgs150176  */
784744Sgs150176 void rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp);
785744Sgs150176 #pragma	no_inline(rge_chip_cfg_init)
786744Sgs150176 
787744Sgs150176 void
788744Sgs150176 rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp)
789744Sgs150176 {
790744Sgs150176 	ddi_acc_handle_t handle;
791744Sgs150176 	uint16_t commd;
792744Sgs150176 
793744Sgs150176 	handle = rgep->cfg_handle;
794744Sgs150176 
795744Sgs150176 	/*
796744Sgs150176 	 * Save PCI cache line size and subsystem vendor ID
797744Sgs150176 	 */
798744Sgs150176 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
799744Sgs150176 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
800744Sgs150176 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
801744Sgs150176 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
802744Sgs150176 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
803744Sgs150176 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
804744Sgs150176 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
805744Sgs150176 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
806744Sgs150176 
807744Sgs150176 	/*
808744Sgs150176 	 * Turn on Master Enable (DMA) and IO Enable bits.
809744Sgs150176 	 * Enable PCI Memory Space accesses
810744Sgs150176 	 */
811744Sgs150176 	commd = cidp->command;
812744Sgs150176 	commd |= PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
813744Sgs150176 	pci_config_put16(handle, PCI_CONF_COMM, commd);
814744Sgs150176 
815744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
8165735Smx205022 	    cidp->vendor, cidp->device, cidp->revision));
817744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: subven 0x%x subdev 0x%x",
8185735Smx205022 	    cidp->subven, cidp->subdev));
819744Sgs150176 	RGE_DEBUG(("rge_chip_cfg_init: clsize %d latency %d command 0x%x",
8205735Smx205022 	    cidp->clsize, cidp->latency, cidp->command));
821744Sgs150176 }
822744Sgs150176 
823744Sgs150176 int rge_chip_reset(rge_t *rgep);
824744Sgs150176 #pragma	no_inline(rge_chip_reset)
825744Sgs150176 
826744Sgs150176 int
827744Sgs150176 rge_chip_reset(rge_t *rgep)
828744Sgs150176 {
829744Sgs150176 	int i;
830744Sgs150176 	uint8_t val8;
831744Sgs150176 
832744Sgs150176 	/*
833744Sgs150176 	 * Chip should be in STOP state
834744Sgs150176 	 */
835744Sgs150176 	rge_reg_clr8(rgep, RT_COMMAND_REG,
836744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
837744Sgs150176 
838744Sgs150176 	/*
839744Sgs150176 	 * Disable interrupt
840744Sgs150176 	 */
841744Sgs150176 	rgep->int_mask = INT_MASK_NONE;
8422544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
843744Sgs150176 
844744Sgs150176 	/*
845744Sgs150176 	 * Clear pended interrupt
846744Sgs150176 	 */
847744Sgs150176 	rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
848744Sgs150176 
849744Sgs150176 	/*
850744Sgs150176 	 * Reset chip
851744Sgs150176 	 */
852744Sgs150176 	rge_reg_set8(rgep, RT_COMMAND_REG, RT_COMMAND_RESET);
853744Sgs150176 
854744Sgs150176 	/*
855744Sgs150176 	 * Wait for reset success
856744Sgs150176 	 */
857744Sgs150176 	for (i = 0; i < CHIP_RESET_LOOP; i++) {
858744Sgs150176 		drv_usecwait(10);
859744Sgs150176 		val8 = rge_reg_get8(rgep, RT_COMMAND_REG);
860744Sgs150176 		if (!(val8 & RT_COMMAND_RESET)) {
861744Sgs150176 			rgep->rge_chip_state = RGE_CHIP_RESET;
862744Sgs150176 			return (0);
863744Sgs150176 		}
864744Sgs150176 	}
865744Sgs150176 	RGE_REPORT((rgep, "rge_chip_reset fail."));
866744Sgs150176 	return (-1);
867744Sgs150176 }
868744Sgs150176 
869744Sgs150176 void rge_chip_init(rge_t *rgep);
870744Sgs150176 #pragma	no_inline(rge_chip_init)
871744Sgs150176 
872744Sgs150176 void
873744Sgs150176 rge_chip_init(rge_t *rgep)
874744Sgs150176 {
875744Sgs150176 	uint32_t val32;
8762544Sgs150176 	uint32_t val16;
8772544Sgs150176 	uint32_t *hashp;
8782544Sgs150176 	chip_id_t *chip = &rgep->chipid;
8792544Sgs150176 
880*11365SZhen.W@Sun.COM 	/*
881*11365SZhen.W@Sun.COM 	 * Increase the threshold voltage of RX sensitivity
882*11365SZhen.W@Sun.COM 	 */
883*11365SZhen.W@Sun.COM 	if (chip->mac_ver == MAC_VER_8168B_B ||
884*11365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8168B_C ||
885*11365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8101E ||
886*11365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8101E_C) {
887*11365SZhen.W@Sun.COM 		rge_ephy_put16(rgep, 0x01, 0x1bd3);
888*11365SZhen.W@Sun.COM 	}
8892544Sgs150176 
890*11365SZhen.W@Sun.COM 	if (chip->mac_ver == MAC_VER_8168 ||
891*11365SZhen.W@Sun.COM 	    chip->mac_ver == MAC_VER_8168B_B) {
8922544Sgs150176 		val16 = rge_reg_get8(rgep, PHY_STATUS_REG);
8932544Sgs150176 		val16 = 0x12<<8 | val16;
894*11365SZhen.W@Sun.COM 		rge_reg_put16(rgep, PHY_STATUS_REG, val16);
895*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00021c01);
896*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f088);
897*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00004000);
898*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f0b0);
899*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x0000f068);
900*11365SZhen.W@Sun.COM 		val32 = rge_reg_get32(rgep, RT_CSI_DATA_REG);
901*11365SZhen.W@Sun.COM 		val32 |= 0x7000;
902*11365SZhen.W@Sun.COM 		val32 &= 0xffff5fff;
903*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_DATA_REG, val32);
904*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f068);
9052544Sgs150176 	}
906744Sgs150176 
907744Sgs150176 	/*
908744Sgs150176 	 * Config MII register
909744Sgs150176 	 */
910744Sgs150176 	rgep->param_link_up = LINK_STATE_DOWN;
911744Sgs150176 	rge_phy_update(rgep);
912744Sgs150176 
913744Sgs150176 	/*
914744Sgs150176 	 * Enable Rx checksum offload.
915744Sgs150176 	 *  Then for vlan support, we must enable receive vlan de-tagging.
916744Sgs150176 	 *  Otherwise, there'll be checksum error.
917744Sgs150176 	 */
9182544Sgs150176 	val16 = rge_reg_get16(rgep, CPLUS_COMMAND_REG);
9192544Sgs150176 	val16 |= RX_CKSM_OFFLOAD | RX_VLAN_DETAG;
9202544Sgs150176 	if (chip->mac_ver == MAC_VER_8169S_D) {
9212544Sgs150176 		val16 |= CPLUS_BIT14 | MUL_PCI_RW_ENABLE;
922744Sgs150176 		rge_reg_put8(rgep, RESV_82_REG, 0x01);
923744Sgs150176 	}
9242544Sgs150176 	rge_reg_put16(rgep, CPLUS_COMMAND_REG, val16 & (~0x03));
925744Sgs150176 
926744Sgs150176 	/*
927744Sgs150176 	 * Start transmit/receive before set tx/rx configuration register
928744Sgs150176 	 */
9292544Sgs150176 	if (!chip->is_pcie)
9302544Sgs150176 		rge_reg_set8(rgep, RT_COMMAND_REG,
9312544Sgs150176 		    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
932744Sgs150176 
933744Sgs150176 	/*
934744Sgs150176 	 * Set dump tally counter register
935744Sgs150176 	 */
936744Sgs150176 	val32 = rgep->dma_area_stats.cookie.dmac_laddress >> 32;
937744Sgs150176 	rge_reg_put32(rgep, DUMP_COUNTER_REG_1, val32);
938744Sgs150176 	val32 = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
939744Sgs150176 	val32 &= DUMP_COUNTER_REG_RESV;
940744Sgs150176 	val32 |= rgep->dma_area_stats.cookie.dmac_laddress;
941744Sgs150176 	rge_reg_put32(rgep, DUMP_COUNTER_REG_0, val32);
942744Sgs150176 
943744Sgs150176 	/*
944744Sgs150176 	 * Change to config register write enable mode
945744Sgs150176 	 */
946744Sgs150176 	rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
947744Sgs150176 
948744Sgs150176 	/*
949744Sgs150176 	 * Set Tx/Rx maximum packet size
950744Sgs150176 	 */
9512544Sgs150176 	if (rgep->default_mtu > ETHERMTU) {
952744Sgs150176 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_JUMBO);
953744Sgs150176 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_JUMBO);
9545735Smx205022 	} else if (rgep->chipid.mac_ver != MAC_VER_8101E) {
955744Sgs150176 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD);
956744Sgs150176 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD);
9575735Smx205022 	} else {
9585735Smx205022 		rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD_8101E);
9595735Smx205022 		rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD_8101E);
960744Sgs150176 	}
961744Sgs150176 
962744Sgs150176 	/*
963744Sgs150176 	 * Set receive configuration register
964744Sgs150176 	 */
965744Sgs150176 	val32 = rge_reg_get32(rgep, RX_CONFIG_REG);
966744Sgs150176 	val32 &= RX_CONFIG_REG_RESV;
967744Sgs150176 	if (rgep->promisc)
968744Sgs150176 		val32 |= RX_ACCEPT_ALL_PKT;
9692544Sgs150176 	rge_reg_put32(rgep, RX_CONFIG_REG, val32 | chip->rxconfig);
970744Sgs150176 
971744Sgs150176 	/*
972744Sgs150176 	 * Set transmit configuration register
973744Sgs150176 	 */
974744Sgs150176 	val32 = rge_reg_get32(rgep, TX_CONFIG_REG);
975744Sgs150176 	val32 &= TX_CONFIG_REG_RESV;
9762544Sgs150176 	rge_reg_put32(rgep, TX_CONFIG_REG, val32 | chip->txconfig);
977744Sgs150176 
978744Sgs150176 	/*
979744Sgs150176 	 * Set Tx/Rx descriptor register
980744Sgs150176 	 */
981744Sgs150176 	val32 = rgep->tx_desc.cookie.dmac_laddress;
982744Sgs150176 	rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_LO_REG, val32);
983744Sgs150176 	val32 = rgep->tx_desc.cookie.dmac_laddress >> 32;
984744Sgs150176 	rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_HI_REG, val32);
985744Sgs150176 	rge_reg_put32(rgep, HIGH_TX_RING_ADDR_LO_REG, 0);
986744Sgs150176 	rge_reg_put32(rgep, HIGH_TX_RING_ADDR_HI_REG, 0);
987744Sgs150176 	val32 = rgep->rx_desc.cookie.dmac_laddress;
988744Sgs150176 	rge_reg_put32(rgep, RX_RING_ADDR_LO_REG, val32);
989744Sgs150176 	val32 = rgep->rx_desc.cookie.dmac_laddress >> 32;
990744Sgs150176 	rge_reg_put32(rgep, RX_RING_ADDR_HI_REG, val32);
991744Sgs150176 
992744Sgs150176 	/*
993744Sgs150176 	 * Suggested setting from Realtek
994744Sgs150176 	 */
9955735Smx205022 	if (rgep->chipid.mac_ver != MAC_VER_8101E)
9965735Smx205022 		rge_reg_put16(rgep, RESV_E2_REG, 0x282a);
9975735Smx205022 	else
9985735Smx205022 		rge_reg_put16(rgep, RESV_E2_REG, 0x0000);
999744Sgs150176 
1000744Sgs150176 	/*
1001744Sgs150176 	 * Set multicast register
1002744Sgs150176 	 */
10032544Sgs150176 	hashp = (uint32_t *)rgep->mcast_hash;
100410814SKHF04453@nifty.ne.jp 	if (rgep->promisc) {
100510814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
100610814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
100710814SKHF04453@nifty.ne.jp 	} else {
100810814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
100910814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
101010814SKHF04453@nifty.ne.jp 	}
1011744Sgs150176 
1012744Sgs150176 	/*
1013744Sgs150176 	 * Msic register setting:
1014744Sgs150176 	 *   -- Missed packet counter: clear it
1015744Sgs150176 	 *   -- TimerInt Register
1016744Sgs150176 	 *   -- Timer count register
1017744Sgs150176 	 */
1018744Sgs150176 	rge_reg_put32(rgep, RX_PKT_MISS_COUNT_REG, 0);
1019744Sgs150176 	rge_reg_put32(rgep, TIMER_INT_REG, TIMER_INT_NONE);
1020744Sgs150176 	rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
10214533Sgs150176 
10224533Sgs150176 	/*
10237825SMin.Xu@Sun.COM 	 * disable the Unicast Wakeup Frame capability
10247825SMin.Xu@Sun.COM 	 */
10257825SMin.Xu@Sun.COM 	rge_reg_clr8(rgep, RT_CONFIG_5_REG, RT_UNI_WAKE_FRAME);
10267825SMin.Xu@Sun.COM 
10277825SMin.Xu@Sun.COM 	/*
10284533Sgs150176 	 * Return to normal network/host communication mode
10294533Sgs150176 	 */
10304533Sgs150176 	rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
10314533Sgs150176 	drv_usecwait(20);
1032744Sgs150176 }
1033744Sgs150176 
1034744Sgs150176 /*
1035744Sgs150176  * rge_chip_start() -- start the chip transmitting and/or receiving,
1036744Sgs150176  * including enabling interrupts
1037744Sgs150176  */
1038744Sgs150176 void rge_chip_start(rge_t *rgep);
1039744Sgs150176 #pragma	no_inline(rge_chip_start)
1040744Sgs150176 
1041744Sgs150176 void
1042744Sgs150176 rge_chip_start(rge_t *rgep)
1043744Sgs150176 {
1044744Sgs150176 	/*
1045744Sgs150176 	 * Clear statistics
1046744Sgs150176 	 */
1047744Sgs150176 	bzero(&rgep->stats, sizeof (rge_stats_t));
1048744Sgs150176 	DMA_ZERO(rgep->dma_area_stats);
1049744Sgs150176 
1050744Sgs150176 	/*
1051744Sgs150176 	 * Start transmit/receive
1052744Sgs150176 	 */
1053744Sgs150176 	rge_reg_set8(rgep, RT_COMMAND_REG,
1054744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1055744Sgs150176 
1056744Sgs150176 	/*
1057744Sgs150176 	 * Enable interrupt
1058744Sgs150176 	 */
1059744Sgs150176 	rgep->int_mask = RGE_INT_MASK;
1060*11365SZhen.W@Sun.COM 	if (rgep->chipid.is_pcie) {
1061*11365SZhen.W@Sun.COM 		rgep->int_mask |= NO_TXDESC_INT;
1062*11365SZhen.W@Sun.COM 	}
10632544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1064744Sgs150176 
1065744Sgs150176 	/*
1066744Sgs150176 	 * All done!
1067744Sgs150176 	 */
1068744Sgs150176 	rgep->rge_chip_state = RGE_CHIP_RUNNING;
1069744Sgs150176 }
1070744Sgs150176 
1071744Sgs150176 /*
1072744Sgs150176  * rge_chip_stop() -- stop board receiving
10737656SSherry.Moore@Sun.COM  *
10747656SSherry.Moore@Sun.COM  * Since this function is also invoked by rge_quiesce(), it
10757656SSherry.Moore@Sun.COM  * must not block; also, no tracing or logging takes place
10767656SSherry.Moore@Sun.COM  * when invoked by rge_quiesce().
1077744Sgs150176  */
1078744Sgs150176 void rge_chip_stop(rge_t *rgep, boolean_t fault);
1079744Sgs150176 #pragma	no_inline(rge_chip_stop)
1080744Sgs150176 
1081744Sgs150176 void
1082744Sgs150176 rge_chip_stop(rge_t *rgep, boolean_t fault)
1083744Sgs150176 {
1084744Sgs150176 	/*
1085744Sgs150176 	 * Disable interrupt
1086744Sgs150176 	 */
1087744Sgs150176 	rgep->int_mask = INT_MASK_NONE;
10882544Sgs150176 	rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1089744Sgs150176 
1090744Sgs150176 	/*
1091744Sgs150176 	 * Clear pended interrupt
1092744Sgs150176 	 */
10936764Smx205022 	if (!rgep->suspended) {
10946764Smx205022 		rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
10956764Smx205022 	}
1096744Sgs150176 
1097744Sgs150176 	/*
1098744Sgs150176 	 * Stop the board and disable transmit/receive
1099744Sgs150176 	 */
1100744Sgs150176 	rge_reg_clr8(rgep, RT_COMMAND_REG,
1101744Sgs150176 	    RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1102744Sgs150176 
1103744Sgs150176 	if (fault)
1104744Sgs150176 		rgep->rge_chip_state = RGE_CHIP_FAULT;
1105744Sgs150176 	else
1106744Sgs150176 		rgep->rge_chip_state = RGE_CHIP_STOPPED;
1107744Sgs150176 }
1108744Sgs150176 
1109744Sgs150176 /*
1110744Sgs150176  * rge_get_mac_addr() -- get the MAC address on NIC
1111744Sgs150176  */
1112744Sgs150176 static void rge_get_mac_addr(rge_t *rgep);
1113744Sgs150176 #pragma	inline(rge_get_mac_addr)
1114744Sgs150176 
1115744Sgs150176 static void
1116744Sgs150176 rge_get_mac_addr(rge_t *rgep)
1117744Sgs150176 {
1118744Sgs150176 	uint8_t *macaddr = rgep->netaddr;
1119744Sgs150176 	uint32_t val32;
1120744Sgs150176 
1121744Sgs150176 	/*
1122744Sgs150176 	 * Read first 4-byte of mac address
1123744Sgs150176 	 */
1124744Sgs150176 	val32 = rge_reg_get32(rgep, ID_0_REG);
1125744Sgs150176 	macaddr[0] = val32 & 0xff;
1126744Sgs150176 	val32 = val32 >> 8;
1127744Sgs150176 	macaddr[1] = val32 & 0xff;
1128744Sgs150176 	val32 = val32 >> 8;
1129744Sgs150176 	macaddr[2] = val32 & 0xff;
1130744Sgs150176 	val32 = val32 >> 8;
1131744Sgs150176 	macaddr[3] = val32 & 0xff;
1132744Sgs150176 
1133744Sgs150176 	/*
1134744Sgs150176 	 * Read last 2-byte of mac address
1135744Sgs150176 	 */
1136744Sgs150176 	val32 = rge_reg_get32(rgep, ID_4_REG);
1137744Sgs150176 	macaddr[4] = val32 & 0xff;
1138744Sgs150176 	val32 = val32 >> 8;
1139744Sgs150176 	macaddr[5] = val32 & 0xff;
1140744Sgs150176 }
1141744Sgs150176 
1142744Sgs150176 static void rge_set_mac_addr(rge_t *rgep);
1143744Sgs150176 #pragma	inline(rge_set_mac_addr)
1144744Sgs150176 
1145744Sgs150176 static void
1146744Sgs150176 rge_set_mac_addr(rge_t *rgep)
1147744Sgs150176 {
1148744Sgs150176 	uint8_t *p = rgep->netaddr;
1149744Sgs150176 	uint32_t val32;
1150744Sgs150176 
1151744Sgs150176 	/*
1152744Sgs150176 	 * Change to config register write enable mode
1153744Sgs150176 	 */
1154744Sgs150176 	rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1155744Sgs150176 
1156744Sgs150176 	/*
1157744Sgs150176 	 * Get first 4 bytes of mac address
1158744Sgs150176 	 */
1159744Sgs150176 	val32 = p[3];
1160744Sgs150176 	val32 = val32 << 8;
1161744Sgs150176 	val32 |= p[2];
1162744Sgs150176 	val32 = val32 << 8;
1163744Sgs150176 	val32 |= p[1];
1164744Sgs150176 	val32 = val32 << 8;
1165744Sgs150176 	val32 |= p[0];
1166744Sgs150176 
1167744Sgs150176 	/*
1168744Sgs150176 	 * Set first 4 bytes of mac address
1169744Sgs150176 	 */
1170744Sgs150176 	rge_reg_put32(rgep, ID_0_REG, val32);
1171744Sgs150176 
1172744Sgs150176 	/*
1173744Sgs150176 	 * Get last 2 bytes of mac address
1174744Sgs150176 	 */
1175744Sgs150176 	val32 = p[5];
1176744Sgs150176 	val32 = val32 << 8;
1177744Sgs150176 	val32 |= p[4];
1178744Sgs150176 
1179744Sgs150176 	/*
1180744Sgs150176 	 * Set last 2 bytes of mac address
1181744Sgs150176 	 */
1182744Sgs150176 	val32 |= rge_reg_get32(rgep, ID_4_REG) & ~0xffff;
1183744Sgs150176 	rge_reg_put32(rgep, ID_4_REG, val32);
1184744Sgs150176 
1185744Sgs150176 	/*
1186744Sgs150176 	 * Return to normal network/host communication mode
1187744Sgs150176 	 */
1188744Sgs150176 	rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1189744Sgs150176 }
1190744Sgs150176 
1191744Sgs150176 static void rge_set_multi_addr(rge_t *rgep);
1192744Sgs150176 #pragma	inline(rge_set_multi_addr)
1193744Sgs150176 
1194744Sgs150176 static void
1195744Sgs150176 rge_set_multi_addr(rge_t *rgep)
1196744Sgs150176 {
1197744Sgs150176 	uint32_t *hashp;
1198744Sgs150176 
11992544Sgs150176 	hashp = (uint32_t *)rgep->mcast_hash;
12004533Sgs150176 
12014533Sgs150176 	/*
12024533Sgs150176 	 * Change to config register write enable mode
12034533Sgs150176 	 */
12047825SMin.Xu@Sun.COM 	if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12054533Sgs150176 		rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12067825SMin.Xu@Sun.COM 	}
120710814SKHF04453@nifty.ne.jp 	if (rgep->promisc) {
120810814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
120910814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
121010814SKHF04453@nifty.ne.jp 	} else {
121110814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
121210814SKHF04453@nifty.ne.jp 		rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
121310814SKHF04453@nifty.ne.jp 	}
12144533Sgs150176 
12154533Sgs150176 	/*
12164533Sgs150176 	 * Return to normal network/host communication mode
12174533Sgs150176 	 */
12187825SMin.Xu@Sun.COM 	if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12194533Sgs150176 		rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12207825SMin.Xu@Sun.COM 	}
1221744Sgs150176 }
1222744Sgs150176 
1223744Sgs150176 static void rge_set_promisc(rge_t *rgep);
1224744Sgs150176 #pragma	inline(rge_set_promisc)
1225744Sgs150176 
1226744Sgs150176 static void
1227744Sgs150176 rge_set_promisc(rge_t *rgep)
1228744Sgs150176 {
1229744Sgs150176 	if (rgep->promisc)
1230744Sgs150176 		rge_reg_set32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1231744Sgs150176 	else
1232744Sgs150176 		rge_reg_clr32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1233744Sgs150176 }
1234744Sgs150176 
1235744Sgs150176 /*
1236744Sgs150176  * rge_chip_sync() -- program the chip with the unicast MAC address,
1237744Sgs150176  * the multicast hash table, the required level of promiscuity, and
1238744Sgs150176  * the current loopback mode ...
1239744Sgs150176  */
1240744Sgs150176 void rge_chip_sync(rge_t *rgep, enum rge_sync_op todo);
1241744Sgs150176 #pragma	no_inline(rge_chip_sync)
1242744Sgs150176 
1243744Sgs150176 void
1244744Sgs150176 rge_chip_sync(rge_t *rgep, enum rge_sync_op todo)
1245744Sgs150176 {
1246744Sgs150176 	switch (todo) {
1247744Sgs150176 	case RGE_GET_MAC:
1248744Sgs150176 		rge_get_mac_addr(rgep);
1249744Sgs150176 		break;
1250744Sgs150176 	case RGE_SET_MAC:
1251744Sgs150176 		/* Reprogram the unicast MAC address(es) ... */
1252744Sgs150176 		rge_set_mac_addr(rgep);
1253744Sgs150176 		break;
1254744Sgs150176 	case RGE_SET_MUL:
1255744Sgs150176 		/* Reprogram the hashed multicast address table ... */
1256744Sgs150176 		rge_set_multi_addr(rgep);
1257744Sgs150176 		break;
1258744Sgs150176 	case RGE_SET_PROMISC:
1259744Sgs150176 		/* Set or clear the PROMISCUOUS mode bit */
126010814SKHF04453@nifty.ne.jp 		rge_set_multi_addr(rgep);
1261744Sgs150176 		rge_set_promisc(rgep);
1262744Sgs150176 		break;
1263744Sgs150176 	default:
1264744Sgs150176 		break;
1265744Sgs150176 	}
1266744Sgs150176 }
1267744Sgs150176 
12688275SEric Cheng void rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag);
1269744Sgs150176 #pragma	no_inline(rge_chip_blank)
1270744Sgs150176 
12718275SEric Cheng /* ARGSUSED */
1272744Sgs150176 void
12738275SEric Cheng rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag)
1274744Sgs150176 {
1275744Sgs150176 	_NOTE(ARGUNUSED(arg, ticks, count));
1276744Sgs150176 }
1277744Sgs150176 
1278744Sgs150176 void rge_tx_trigger(rge_t *rgep);
1279744Sgs150176 #pragma	no_inline(rge_tx_trigger)
1280744Sgs150176 
1281744Sgs150176 void
1282744Sgs150176 rge_tx_trigger(rge_t *rgep)
1283744Sgs150176 {
1284*11365SZhen.W@Sun.COM 	rge_reg_put8(rgep, TX_RINGS_POLL_REG, NORMAL_TX_RING_POLL);
1285744Sgs150176 }
1286744Sgs150176 
1287744Sgs150176 void rge_hw_stats_dump(rge_t *rgep);
1288744Sgs150176 #pragma	no_inline(rge_tx_trigger)
1289744Sgs150176 
1290744Sgs150176 void
1291744Sgs150176 rge_hw_stats_dump(rge_t *rgep)
1292744Sgs150176 {
1293744Sgs150176 	int i = 0;
1294744Sgs150176 
1295744Sgs150176 	while (rge_reg_get32(rgep, DUMP_COUNTER_REG_0) & DUMP_START) {
1296744Sgs150176 		drv_usecwait(100);
1297744Sgs150176 		if (++i > STATS_DUMP_LOOP) {
1298744Sgs150176 			RGE_DEBUG(("rge h/w statistics dump fail!"));
1299744Sgs150176 			rgep->rge_chip_state = RGE_CHIP_ERROR;
1300744Sgs150176 			return;
1301744Sgs150176 		}
1302744Sgs150176 	}
1303744Sgs150176 	DMA_SYNC(rgep->dma_area_stats, DDI_DMA_SYNC_FORKERNEL);
1304744Sgs150176 
1305744Sgs150176 	/*
1306744Sgs150176 	 * Start H/W statistics dump for RTL8169 chip
1307744Sgs150176 	 */
1308744Sgs150176 	rge_reg_set32(rgep, DUMP_COUNTER_REG_0, DUMP_START);
1309744Sgs150176 }
1310744Sgs150176 
1311744Sgs150176 /*
1312744Sgs150176  * ========== Hardware interrupt handler ==========
1313744Sgs150176  */
1314744Sgs150176 
1315744Sgs150176 #undef	RGE_DBG
1316744Sgs150176 #define	RGE_DBG		RGE_DBG_INT	/* debug flag for this code	*/
1317744Sgs150176 
1318744Sgs150176 static void rge_wake_factotum(rge_t *rgep);
1319744Sgs150176 #pragma	inline(rge_wake_factotum)
1320744Sgs150176 
1321744Sgs150176 static void
1322744Sgs150176 rge_wake_factotum(rge_t *rgep)
1323744Sgs150176 {
1324744Sgs150176 	if (rgep->factotum_flag == 0) {
1325744Sgs150176 		rgep->factotum_flag = 1;
13262544Sgs150176 		(void) ddi_intr_trigger_softint(rgep->factotum_hdl, NULL);
1327744Sgs150176 	}
1328744Sgs150176 }
1329744Sgs150176 
1330744Sgs150176 /*
1331744Sgs150176  *	rge_intr() -- handle chip interrupts
1332744Sgs150176  */
13332544Sgs150176 uint_t rge_intr(caddr_t arg1, caddr_t arg2);
1334744Sgs150176 #pragma	no_inline(rge_intr)
1335744Sgs150176 
1336744Sgs150176 uint_t
13372544Sgs150176 rge_intr(caddr_t arg1, caddr_t arg2)
1338744Sgs150176 {
13392544Sgs150176 	rge_t *rgep = (rge_t *)arg1;
1340744Sgs150176 	uint16_t int_status;
1341*11365SZhen.W@Sun.COM 	clock_t	now;
1342*11365SZhen.W@Sun.COM 	uint32_t tx_pkts;
1343*11365SZhen.W@Sun.COM 	uint32_t rx_pkts;
1344*11365SZhen.W@Sun.COM 	uint32_t poll_rate;
1345*11365SZhen.W@Sun.COM 	uint32_t opt_pkts;
1346*11365SZhen.W@Sun.COM 	uint32_t opt_intrs;
1347*11365SZhen.W@Sun.COM 	boolean_t update_int_mask = B_FALSE;
1348*11365SZhen.W@Sun.COM 	uint32_t itimer;
1349744Sgs150176 
13502544Sgs150176 	_NOTE(ARGUNUSED(arg2))
13512544Sgs150176 
1352744Sgs150176 	mutex_enter(rgep->genlock);
13536764Smx205022 
13546764Smx205022 	if (rgep->suspended) {
13556764Smx205022 		mutex_exit(rgep->genlock);
13566764Smx205022 		return (DDI_INTR_UNCLAIMED);
13576764Smx205022 	}
13586764Smx205022 
1359744Sgs150176 	/*
1360744Sgs150176 	 * Was this interrupt caused by our device...
1361744Sgs150176 	 */
1362744Sgs150176 	int_status = rge_reg_get16(rgep, INT_STATUS_REG);
1363744Sgs150176 	if (!(int_status & rgep->int_mask)) {
1364744Sgs150176 		mutex_exit(rgep->genlock);
1365744Sgs150176 		return (DDI_INTR_UNCLAIMED);
1366744Sgs150176 				/* indicate it wasn't our interrupt */
1367744Sgs150176 	}
1368744Sgs150176 	rgep->stats.intr++;
1369744Sgs150176 
1370744Sgs150176 	/*
1371744Sgs150176 	 * Clear interrupt
13722544Sgs150176 	 *	For PCIE chipset, we need disable interrupt first.
1373744Sgs150176 	 */
1374*11365SZhen.W@Sun.COM 	if (rgep->chipid.is_pcie) {
13752544Sgs150176 		rge_reg_put16(rgep, INT_MASK_REG, INT_MASK_NONE);
1376*11365SZhen.W@Sun.COM 		update_int_mask = B_TRUE;
1377*11365SZhen.W@Sun.COM 	}
1378744Sgs150176 	rge_reg_put16(rgep, INT_STATUS_REG, int_status);
1379744Sgs150176 
1380744Sgs150176 	/*
1381*11365SZhen.W@Sun.COM 	 * Calculate optimal polling interval
1382*11365SZhen.W@Sun.COM 	 */
1383*11365SZhen.W@Sun.COM 	now = ddi_get_lbolt();
1384*11365SZhen.W@Sun.COM 	if (now - rgep->curr_tick >= rgep->tick_delta &&
1385*11365SZhen.W@Sun.COM 	    (rgep->param_link_speed == RGE_SPEED_1000M ||
1386*11365SZhen.W@Sun.COM 	    rgep->param_link_speed == RGE_SPEED_100M)) {
1387*11365SZhen.W@Sun.COM 		/* number of rx and tx packets in the last tick */
1388*11365SZhen.W@Sun.COM 		tx_pkts = rgep->stats.opackets - rgep->last_opackets;
1389*11365SZhen.W@Sun.COM 		rx_pkts = rgep->stats.rpackets - rgep->last_rpackets;
1390*11365SZhen.W@Sun.COM 
1391*11365SZhen.W@Sun.COM 		rgep->last_opackets = rgep->stats.opackets;
1392*11365SZhen.W@Sun.COM 		rgep->last_rpackets = rgep->stats.rpackets;
1393*11365SZhen.W@Sun.COM 
1394*11365SZhen.W@Sun.COM 		/* restore interrupt mask */
1395*11365SZhen.W@Sun.COM 		rgep->int_mask |= TX_OK_INT | RX_OK_INT;
1396*11365SZhen.W@Sun.COM 		if (rgep->chipid.is_pcie) {
1397*11365SZhen.W@Sun.COM 			rgep->int_mask |= NO_TXDESC_INT;
1398*11365SZhen.W@Sun.COM 		}
1399*11365SZhen.W@Sun.COM 
1400*11365SZhen.W@Sun.COM 		/* optimal number of packets in a tick */
1401*11365SZhen.W@Sun.COM 		if (rgep->param_link_speed == RGE_SPEED_1000M) {
1402*11365SZhen.W@Sun.COM 			opt_pkts = (1000*1000*1000/8)/ETHERMTU/CLK_TICK;
1403*11365SZhen.W@Sun.COM 		} else {
1404*11365SZhen.W@Sun.COM 			opt_pkts = (100*1000*1000/8)/ETHERMTU/CLK_TICK;
1405*11365SZhen.W@Sun.COM 		}
1406*11365SZhen.W@Sun.COM 
1407*11365SZhen.W@Sun.COM 		/*
1408*11365SZhen.W@Sun.COM 		 * calculate polling interval based on rx and tx packets
1409*11365SZhen.W@Sun.COM 		 * in the last tick
1410*11365SZhen.W@Sun.COM 		 */
1411*11365SZhen.W@Sun.COM 		poll_rate = 0;
1412*11365SZhen.W@Sun.COM 		if (now - rgep->curr_tick < 2*rgep->tick_delta) {
1413*11365SZhen.W@Sun.COM 			opt_intrs = opt_pkts/TX_COALESC;
1414*11365SZhen.W@Sun.COM 			if (tx_pkts > opt_intrs) {
1415*11365SZhen.W@Sun.COM 				poll_rate = max(tx_pkts/TX_COALESC, opt_intrs);
1416*11365SZhen.W@Sun.COM 				rgep->int_mask &= ~(TX_OK_INT | NO_TXDESC_INT);
1417*11365SZhen.W@Sun.COM 			}
1418*11365SZhen.W@Sun.COM 
1419*11365SZhen.W@Sun.COM 			opt_intrs = opt_pkts/RX_COALESC;
1420*11365SZhen.W@Sun.COM 			if (rx_pkts > opt_intrs) {
1421*11365SZhen.W@Sun.COM 				opt_intrs = max(rx_pkts/RX_COALESC, opt_intrs);
1422*11365SZhen.W@Sun.COM 				poll_rate = max(opt_intrs, poll_rate);
1423*11365SZhen.W@Sun.COM 				rgep->int_mask &= ~RX_OK_INT;
1424*11365SZhen.W@Sun.COM 			}
1425*11365SZhen.W@Sun.COM 			/* ensure poll_rate reasonable */
1426*11365SZhen.W@Sun.COM 			poll_rate = min(poll_rate, opt_pkts*4);
1427*11365SZhen.W@Sun.COM 		}
1428*11365SZhen.W@Sun.COM 
1429*11365SZhen.W@Sun.COM 		if (poll_rate) {
1430*11365SZhen.W@Sun.COM 			/* move to polling mode */
1431*11365SZhen.W@Sun.COM 			if (rgep->chipid.is_pcie) {
1432*11365SZhen.W@Sun.COM 				itimer = (TIMER_CLK_PCIE/CLK_TICK)/poll_rate;
1433*11365SZhen.W@Sun.COM 			} else {
1434*11365SZhen.W@Sun.COM 				itimer = (TIMER_CLK_PCI/CLK_TICK)/poll_rate;
1435*11365SZhen.W@Sun.COM 			}
1436*11365SZhen.W@Sun.COM 		} else {
1437*11365SZhen.W@Sun.COM 			/* move to normal mode */
1438*11365SZhen.W@Sun.COM 			itimer = 0;
1439*11365SZhen.W@Sun.COM 		}
1440*11365SZhen.W@Sun.COM 		RGE_DEBUG(("%s: poll: itimer:%d int_mask:0x%x",
1441*11365SZhen.W@Sun.COM 		    __func__, itimer, rgep->int_mask));
1442*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, TIMER_INT_REG, itimer);
1443*11365SZhen.W@Sun.COM 
1444*11365SZhen.W@Sun.COM 		/* update timestamp for statistics */
1445*11365SZhen.W@Sun.COM 		rgep->curr_tick = now;
1446*11365SZhen.W@Sun.COM 
1447*11365SZhen.W@Sun.COM 		/* reset timer */
1448*11365SZhen.W@Sun.COM 		int_status |= TIME_OUT_INT;
1449*11365SZhen.W@Sun.COM 
1450*11365SZhen.W@Sun.COM 		update_int_mask = B_TRUE;
1451*11365SZhen.W@Sun.COM 	}
1452*11365SZhen.W@Sun.COM 
1453*11365SZhen.W@Sun.COM 	if (int_status & TIME_OUT_INT) {
1454*11365SZhen.W@Sun.COM 		rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
1455*11365SZhen.W@Sun.COM 	}
1456*11365SZhen.W@Sun.COM 
1457*11365SZhen.W@Sun.COM 	/* flush post writes */
1458*11365SZhen.W@Sun.COM 	(void) rge_reg_get16(rgep, INT_STATUS_REG);
1459*11365SZhen.W@Sun.COM 
1460*11365SZhen.W@Sun.COM 	/*
1461744Sgs150176 	 * Cable link change interrupt
1462744Sgs150176 	 */
1463744Sgs150176 	if (int_status & LINK_CHANGE_INT) {
1464744Sgs150176 		rge_chip_cyclic(rgep);
1465744Sgs150176 	}
14662544Sgs150176 
1467744Sgs150176 	mutex_exit(rgep->genlock);
1468744Sgs150176 
1469744Sgs150176 	/*
1470744Sgs150176 	 * Receive interrupt
1471744Sgs150176 	 */
14722544Sgs150176 	if (int_status & RGE_RX_INT)
1473744Sgs150176 		rge_receive(rgep);
1474744Sgs150176 
14752544Sgs150176 	/*
1476*11365SZhen.W@Sun.COM 	 * Transmit interrupt
14772544Sgs150176 	 */
1478*11365SZhen.W@Sun.COM 	if (int_status & TX_ERR_INT) {
1479*11365SZhen.W@Sun.COM 		RGE_REPORT((rgep, "tx error happened, resetting the chip "));
1480*11365SZhen.W@Sun.COM 		mutex_enter(rgep->genlock);
1481*11365SZhen.W@Sun.COM 		rgep->rge_chip_state = RGE_CHIP_ERROR;
1482*11365SZhen.W@Sun.COM 		mutex_exit(rgep->genlock);
1483*11365SZhen.W@Sun.COM 	} else if ((rgep->chipid.is_pcie && (int_status & NO_TXDESC_INT)) ||
1484*11365SZhen.W@Sun.COM 	    ((int_status & TX_OK_INT) && rgep->tx_free < RGE_SEND_SLOTS/8)) {
1485*11365SZhen.W@Sun.COM 		(void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
1486*11365SZhen.W@Sun.COM 	}
1487*11365SZhen.W@Sun.COM 
1488*11365SZhen.W@Sun.COM 	/*
1489*11365SZhen.W@Sun.COM 	 * Re-enable interrupt for PCIE chipset or install new int_mask
1490*11365SZhen.W@Sun.COM 	 */
1491*11365SZhen.W@Sun.COM 	if (update_int_mask)
14922544Sgs150176 		rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
14932544Sgs150176 
1494744Sgs150176 	return (DDI_INTR_CLAIMED);	/* indicate it was our interrupt */
1495744Sgs150176 }
1496744Sgs150176 
1497744Sgs150176 /*
1498744Sgs150176  * ========== Factotum, implemented as a softint handler ==========
1499744Sgs150176  */
1500744Sgs150176 
1501744Sgs150176 #undef	RGE_DBG
1502744Sgs150176 #define	RGE_DBG		RGE_DBG_FACT	/* debug flag for this code	*/
1503744Sgs150176 
1504744Sgs150176 static boolean_t rge_factotum_link_check(rge_t *rgep);
1505744Sgs150176 #pragma	no_inline(rge_factotum_link_check)
1506744Sgs150176 
1507744Sgs150176 static boolean_t
1508744Sgs150176 rge_factotum_link_check(rge_t *rgep)
1509744Sgs150176 {
1510744Sgs150176 	uint8_t media_status;
1511744Sgs150176 	int32_t link;
1512744Sgs150176 
1513744Sgs150176 	media_status = rge_reg_get8(rgep, PHY_STATUS_REG);
1514744Sgs150176 	link = (media_status & PHY_STATUS_LINK_UP) ?
1515744Sgs150176 	    LINK_STATE_UP : LINK_STATE_DOWN;
1516744Sgs150176 	if (rgep->param_link_up != link) {
1517744Sgs150176 		/*
15184403Sgd78059 		 * Link change.
1519744Sgs150176 		 */
1520744Sgs150176 		rgep->param_link_up = link;
1521744Sgs150176 
1522744Sgs150176 		if (link == LINK_STATE_UP) {
1523744Sgs150176 			if (media_status & PHY_STATUS_1000MF) {
1524744Sgs150176 				rgep->param_link_speed = RGE_SPEED_1000M;
1525744Sgs150176 				rgep->param_link_duplex = LINK_DUPLEX_FULL;
1526744Sgs150176 			} else {
1527744Sgs150176 				rgep->param_link_speed =
1528744Sgs150176 				    (media_status & PHY_STATUS_100M) ?
1529744Sgs150176 				    RGE_SPEED_100M : RGE_SPEED_10M;
1530744Sgs150176 				rgep->param_link_duplex =
1531744Sgs150176 				    (media_status & PHY_STATUS_DUPLEX_FULL) ?
1532744Sgs150176 				    LINK_DUPLEX_FULL : LINK_DUPLEX_HALF;
1533744Sgs150176 			}
1534744Sgs150176 		}
1535744Sgs150176 		return (B_TRUE);
1536744Sgs150176 	}
1537744Sgs150176 	return (B_FALSE);
1538744Sgs150176 }
1539744Sgs150176 
1540744Sgs150176 /*
1541744Sgs150176  * Factotum routine to check for Tx stall, using the 'watchdog' counter
1542744Sgs150176  */
1543744Sgs150176 static boolean_t rge_factotum_stall_check(rge_t *rgep);
1544744Sgs150176 #pragma	no_inline(rge_factotum_stall_check)
1545744Sgs150176 
1546744Sgs150176 static boolean_t
1547744Sgs150176 rge_factotum_stall_check(rge_t *rgep)
1548744Sgs150176 {
1549744Sgs150176 	uint32_t dogval;
1550744Sgs150176 
1551744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
1552744Sgs150176 
1553744Sgs150176 	/*
1554744Sgs150176 	 * Specific check for Tx stall ...
1555744Sgs150176 	 *
1556744Sgs150176 	 * The 'watchdog' counter is incremented whenever a packet
1557744Sgs150176 	 * is queued, reset to 1 when some (but not all) buffers
1558744Sgs150176 	 * are reclaimed, reset to 0 (disabled) when all buffers
1559744Sgs150176 	 * are reclaimed, and shifted left here.  If it exceeds the
1560744Sgs150176 	 * threshold value, the chip is assumed to have stalled and
1561744Sgs150176 	 * is put into the ERROR state.  The factotum will then reset
1562744Sgs150176 	 * it on the next pass.
1563744Sgs150176 	 *
1564744Sgs150176 	 * All of which should ensure that we don't get into a state
1565744Sgs150176 	 * where packets are left pending indefinitely!
1566744Sgs150176 	 */
15672544Sgs150176 	if (rgep->resched_needed)
15682544Sgs150176 		(void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
1569744Sgs150176 	dogval = rge_atomic_shl32(&rgep->watchdog, 1);
1570744Sgs150176 	if (dogval < rge_watchdog_count)
1571744Sgs150176 		return (B_FALSE);
1572744Sgs150176 
1573744Sgs150176 	RGE_REPORT((rgep, "Tx stall detected, watchdog code 0x%x", dogval));
1574744Sgs150176 	return (B_TRUE);
1575744Sgs150176 
1576744Sgs150176 }
1577744Sgs150176 
1578744Sgs150176 /*
1579744Sgs150176  * The factotum is woken up when there's something to do that we'd rather
1580744Sgs150176  * not do from inside a hardware interrupt handler or high-level cyclic.
1581744Sgs150176  * Its two main tasks are:
1582744Sgs150176  *	reset & restart the chip after an error
1583744Sgs150176  *	check the link status whenever necessary
1584744Sgs150176  */
15852544Sgs150176 uint_t rge_chip_factotum(caddr_t arg1, caddr_t arg2);
1586744Sgs150176 #pragma	no_inline(rge_chip_factotum)
1587744Sgs150176 
1588744Sgs150176 uint_t
15892544Sgs150176 rge_chip_factotum(caddr_t arg1, caddr_t arg2)
1590744Sgs150176 {
1591744Sgs150176 	rge_t *rgep;
1592744Sgs150176 	uint_t result;
1593744Sgs150176 	boolean_t error;
1594744Sgs150176 	boolean_t linkchg;
1595744Sgs150176 
15962544Sgs150176 	rgep = (rge_t *)arg1;
15972544Sgs150176 	_NOTE(ARGUNUSED(arg2))
1598744Sgs150176 
1599744Sgs150176 	if (rgep->factotum_flag == 0)
1600744Sgs150176 		return (DDI_INTR_UNCLAIMED);
1601744Sgs150176 
1602744Sgs150176 	rgep->factotum_flag = 0;
1603744Sgs150176 	result = DDI_INTR_CLAIMED;
1604744Sgs150176 	error = B_FALSE;
1605744Sgs150176 	linkchg = B_FALSE;
1606744Sgs150176 
1607744Sgs150176 	mutex_enter(rgep->genlock);
1608744Sgs150176 	switch (rgep->rge_chip_state) {
1609744Sgs150176 	default:
1610744Sgs150176 		break;
1611744Sgs150176 
1612744Sgs150176 	case RGE_CHIP_RUNNING:
1613744Sgs150176 		linkchg = rge_factotum_link_check(rgep);
1614744Sgs150176 		error = rge_factotum_stall_check(rgep);
1615744Sgs150176 		break;
1616744Sgs150176 
1617744Sgs150176 	case RGE_CHIP_ERROR:
1618744Sgs150176 		error = B_TRUE;
1619744Sgs150176 		break;
1620744Sgs150176 
1621744Sgs150176 	case RGE_CHIP_FAULT:
1622744Sgs150176 		/*
1623744Sgs150176 		 * Fault detected, time to reset ...
1624744Sgs150176 		 */
1625744Sgs150176 		if (rge_autorecover) {
1626744Sgs150176 			RGE_REPORT((rgep, "automatic recovery activated"));
1627744Sgs150176 			rge_restart(rgep);
1628744Sgs150176 		}
1629744Sgs150176 		break;
1630744Sgs150176 	}
1631744Sgs150176 
1632744Sgs150176 	/*
1633744Sgs150176 	 * If an error is detected, stop the chip now, marking it as
1634744Sgs150176 	 * faulty, so that it will be reset next time through ...
1635744Sgs150176 	 */
1636744Sgs150176 	if (error)
1637744Sgs150176 		rge_chip_stop(rgep, B_TRUE);
1638744Sgs150176 	mutex_exit(rgep->genlock);
1639744Sgs150176 
1640744Sgs150176 	/*
1641744Sgs150176 	 * If the link state changed, tell the world about it.
1642744Sgs150176 	 * Note: can't do this while still holding the mutex.
1643744Sgs150176 	 */
1644744Sgs150176 	if (linkchg)
16452311Sseb 		mac_link_update(rgep->mh, rgep->param_link_up);
1646744Sgs150176 
1647744Sgs150176 	return (result);
1648744Sgs150176 }
1649744Sgs150176 
1650744Sgs150176 /*
1651744Sgs150176  * High-level cyclic handler
1652744Sgs150176  *
1653744Sgs150176  * This routine schedules a (low-level) softint callback to the
1654744Sgs150176  * factotum, and prods the chip to update the status block (which
1655744Sgs150176  * will cause a hardware interrupt when complete).
1656744Sgs150176  */
1657744Sgs150176 void rge_chip_cyclic(void *arg);
1658744Sgs150176 #pragma	no_inline(rge_chip_cyclic)
1659744Sgs150176 
1660744Sgs150176 void
1661744Sgs150176 rge_chip_cyclic(void *arg)
1662744Sgs150176 {
1663744Sgs150176 	rge_t *rgep;
1664744Sgs150176 
1665744Sgs150176 	rgep = arg;
1666744Sgs150176 
1667744Sgs150176 	switch (rgep->rge_chip_state) {
1668744Sgs150176 	default:
1669744Sgs150176 		return;
1670744Sgs150176 
1671744Sgs150176 	case RGE_CHIP_RUNNING:
1672744Sgs150176 		rge_phy_check(rgep);
1673744Sgs150176 		break;
1674744Sgs150176 
1675744Sgs150176 	case RGE_CHIP_FAULT:
1676744Sgs150176 	case RGE_CHIP_ERROR:
1677744Sgs150176 		break;
1678744Sgs150176 	}
1679744Sgs150176 
1680744Sgs150176 	rge_wake_factotum(rgep);
1681744Sgs150176 }
1682744Sgs150176 
1683744Sgs150176 
1684744Sgs150176 /*
1685744Sgs150176  * ========== Ioctl subfunctions ==========
1686744Sgs150176  */
1687744Sgs150176 
1688744Sgs150176 #undef	RGE_DBG
1689744Sgs150176 #define	RGE_DBG		RGE_DBG_PPIO	/* debug flag for this code	*/
1690744Sgs150176 
1691744Sgs150176 #if	RGE_DEBUGGING || RGE_DO_PPIO
1692744Sgs150176 
1693744Sgs150176 static void rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1694744Sgs150176 #pragma	no_inline(rge_chip_peek_cfg)
1695744Sgs150176 
1696744Sgs150176 static void
1697744Sgs150176 rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1698744Sgs150176 {
1699744Sgs150176 	uint64_t regval;
1700744Sgs150176 	uint64_t regno;
1701744Sgs150176 
1702744Sgs150176 	RGE_TRACE(("rge_chip_peek_cfg($%p, $%p)",
17035735Smx205022 	    (void *)rgep, (void *)ppd));
1704744Sgs150176 
1705744Sgs150176 	regno = ppd->pp_acc_offset;
1706744Sgs150176 
1707744Sgs150176 	switch (ppd->pp_acc_size) {
1708744Sgs150176 	case 1:
1709744Sgs150176 		regval = pci_config_get8(rgep->cfg_handle, regno);
1710744Sgs150176 		break;
1711744Sgs150176 
1712744Sgs150176 	case 2:
1713744Sgs150176 		regval = pci_config_get16(rgep->cfg_handle, regno);
1714744Sgs150176 		break;
1715744Sgs150176 
1716744Sgs150176 	case 4:
1717744Sgs150176 		regval = pci_config_get32(rgep->cfg_handle, regno);
1718744Sgs150176 		break;
1719744Sgs150176 
1720744Sgs150176 	case 8:
1721744Sgs150176 		regval = pci_config_get64(rgep->cfg_handle, regno);
1722744Sgs150176 		break;
1723744Sgs150176 	}
1724744Sgs150176 
1725744Sgs150176 	ppd->pp_acc_data = regval;
1726744Sgs150176 }
1727744Sgs150176 
1728744Sgs150176 static void rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1729744Sgs150176 #pragma	no_inline(rge_chip_poke_cfg)
1730744Sgs150176 
1731744Sgs150176 static void
1732744Sgs150176 rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1733744Sgs150176 {
1734744Sgs150176 	uint64_t regval;
1735744Sgs150176 	uint64_t regno;
1736744Sgs150176 
1737744Sgs150176 	RGE_TRACE(("rge_chip_poke_cfg($%p, $%p)",
17385735Smx205022 	    (void *)rgep, (void *)ppd));
1739744Sgs150176 
1740744Sgs150176 	regno = ppd->pp_acc_offset;
1741744Sgs150176 	regval = ppd->pp_acc_data;
1742744Sgs150176 
1743744Sgs150176 	switch (ppd->pp_acc_size) {
1744744Sgs150176 	case 1:
1745744Sgs150176 		pci_config_put8(rgep->cfg_handle, regno, regval);
1746744Sgs150176 		break;
1747744Sgs150176 
1748744Sgs150176 	case 2:
1749744Sgs150176 		pci_config_put16(rgep->cfg_handle, regno, regval);
1750744Sgs150176 		break;
1751744Sgs150176 
1752744Sgs150176 	case 4:
1753744Sgs150176 		pci_config_put32(rgep->cfg_handle, regno, regval);
1754744Sgs150176 		break;
1755744Sgs150176 
1756744Sgs150176 	case 8:
1757744Sgs150176 		pci_config_put64(rgep->cfg_handle, regno, regval);
1758744Sgs150176 		break;
1759744Sgs150176 	}
1760744Sgs150176 }
1761744Sgs150176 
1762744Sgs150176 static void rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1763744Sgs150176 #pragma	no_inline(rge_chip_peek_reg)
1764744Sgs150176 
1765744Sgs150176 static void
1766744Sgs150176 rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1767744Sgs150176 {
1768744Sgs150176 	uint64_t regval;
1769744Sgs150176 	void *regaddr;
1770744Sgs150176 
1771744Sgs150176 	RGE_TRACE(("rge_chip_peek_reg($%p, $%p)",
17725735Smx205022 	    (void *)rgep, (void *)ppd));
1773744Sgs150176 
1774744Sgs150176 	regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1775744Sgs150176 
1776744Sgs150176 	switch (ppd->pp_acc_size) {
1777744Sgs150176 	case 1:
1778744Sgs150176 		regval = ddi_get8(rgep->io_handle, regaddr);
1779744Sgs150176 		break;
1780744Sgs150176 
1781744Sgs150176 	case 2:
1782744Sgs150176 		regval = ddi_get16(rgep->io_handle, regaddr);
1783744Sgs150176 		break;
1784744Sgs150176 
1785744Sgs150176 	case 4:
1786744Sgs150176 		regval = ddi_get32(rgep->io_handle, regaddr);
1787744Sgs150176 		break;
1788744Sgs150176 
1789744Sgs150176 	case 8:
1790744Sgs150176 		regval = ddi_get64(rgep->io_handle, regaddr);
1791744Sgs150176 		break;
1792744Sgs150176 	}
1793744Sgs150176 
1794744Sgs150176 	ppd->pp_acc_data = regval;
1795744Sgs150176 }
1796744Sgs150176 
1797744Sgs150176 static void rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1798744Sgs150176 #pragma	no_inline(rge_chip_peek_reg)
1799744Sgs150176 
1800744Sgs150176 static void
1801744Sgs150176 rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1802744Sgs150176 {
1803744Sgs150176 	uint64_t regval;
1804744Sgs150176 	void *regaddr;
1805744Sgs150176 
1806744Sgs150176 	RGE_TRACE(("rge_chip_poke_reg($%p, $%p)",
18075735Smx205022 	    (void *)rgep, (void *)ppd));
1808744Sgs150176 
1809744Sgs150176 	regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1810744Sgs150176 	regval = ppd->pp_acc_data;
1811744Sgs150176 
1812744Sgs150176 	switch (ppd->pp_acc_size) {
1813744Sgs150176 	case 1:
1814744Sgs150176 		ddi_put8(rgep->io_handle, regaddr, regval);
1815744Sgs150176 		break;
1816744Sgs150176 
1817744Sgs150176 	case 2:
1818744Sgs150176 		ddi_put16(rgep->io_handle, regaddr, regval);
1819744Sgs150176 		break;
1820744Sgs150176 
1821744Sgs150176 	case 4:
1822744Sgs150176 		ddi_put32(rgep->io_handle, regaddr, regval);
1823744Sgs150176 		break;
1824744Sgs150176 
1825744Sgs150176 	case 8:
1826744Sgs150176 		ddi_put64(rgep->io_handle, regaddr, regval);
1827744Sgs150176 		break;
1828744Sgs150176 	}
1829744Sgs150176 }
1830744Sgs150176 
1831744Sgs150176 static void rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1832744Sgs150176 #pragma	no_inline(rge_chip_peek_mii)
1833744Sgs150176 
1834744Sgs150176 static void
1835744Sgs150176 rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1836744Sgs150176 {
1837744Sgs150176 	RGE_TRACE(("rge_chip_peek_mii($%p, $%p)",
18385735Smx205022 	    (void *)rgep, (void *)ppd));
1839744Sgs150176 
1840744Sgs150176 	ppd->pp_acc_data = rge_mii_get16(rgep, ppd->pp_acc_offset/2);
1841744Sgs150176 }
1842744Sgs150176 
1843744Sgs150176 static void rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1844744Sgs150176 #pragma	no_inline(rge_chip_poke_mii)
1845744Sgs150176 
1846744Sgs150176 static void
1847744Sgs150176 rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1848744Sgs150176 {
1849744Sgs150176 	RGE_TRACE(("rge_chip_poke_mii($%p, $%p)",
18505735Smx205022 	    (void *)rgep, (void *)ppd));
1851744Sgs150176 
1852744Sgs150176 	rge_mii_put16(rgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
1853744Sgs150176 }
1854744Sgs150176 
1855744Sgs150176 static void rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1856744Sgs150176 #pragma	no_inline(rge_chip_peek_mem)
1857744Sgs150176 
1858744Sgs150176 static void
1859744Sgs150176 rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1860744Sgs150176 {
1861744Sgs150176 	uint64_t regval;
1862744Sgs150176 	void *vaddr;
1863744Sgs150176 
1864744Sgs150176 	RGE_TRACE(("rge_chip_peek_rge($%p, $%p)",
18655735Smx205022 	    (void *)rgep, (void *)ppd));
1866744Sgs150176 
1867744Sgs150176 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1868744Sgs150176 
1869744Sgs150176 	switch (ppd->pp_acc_size) {
1870744Sgs150176 	case 1:
1871744Sgs150176 		regval = *(uint8_t *)vaddr;
1872744Sgs150176 		break;
1873744Sgs150176 
1874744Sgs150176 	case 2:
1875744Sgs150176 		regval = *(uint16_t *)vaddr;
1876744Sgs150176 		break;
1877744Sgs150176 
1878744Sgs150176 	case 4:
1879744Sgs150176 		regval = *(uint32_t *)vaddr;
1880744Sgs150176 		break;
1881744Sgs150176 
1882744Sgs150176 	case 8:
1883744Sgs150176 		regval = *(uint64_t *)vaddr;
1884744Sgs150176 		break;
1885744Sgs150176 	}
1886744Sgs150176 
1887744Sgs150176 	RGE_DEBUG(("rge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
18885735Smx205022 	    (void *)rgep, (void *)ppd, regval, vaddr));
1889744Sgs150176 
1890744Sgs150176 	ppd->pp_acc_data = regval;
1891744Sgs150176 }
1892744Sgs150176 
1893744Sgs150176 static void rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1894744Sgs150176 #pragma	no_inline(rge_chip_poke_mem)
1895744Sgs150176 
1896744Sgs150176 static void
1897744Sgs150176 rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1898744Sgs150176 {
1899744Sgs150176 	uint64_t regval;
1900744Sgs150176 	void *vaddr;
1901744Sgs150176 
1902744Sgs150176 	RGE_TRACE(("rge_chip_poke_mem($%p, $%p)",
19035735Smx205022 	    (void *)rgep, (void *)ppd));
1904744Sgs150176 
1905744Sgs150176 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1906744Sgs150176 	regval = ppd->pp_acc_data;
1907744Sgs150176 
1908744Sgs150176 	RGE_DEBUG(("rge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
19095735Smx205022 	    (void *)rgep, (void *)ppd, regval, vaddr));
1910744Sgs150176 
1911744Sgs150176 	switch (ppd->pp_acc_size) {
1912744Sgs150176 	case 1:
1913744Sgs150176 		*(uint8_t *)vaddr = (uint8_t)regval;
1914744Sgs150176 		break;
1915744Sgs150176 
1916744Sgs150176 	case 2:
1917744Sgs150176 		*(uint16_t *)vaddr = (uint16_t)regval;
1918744Sgs150176 		break;
1919744Sgs150176 
1920744Sgs150176 	case 4:
1921744Sgs150176 		*(uint32_t *)vaddr = (uint32_t)regval;
1922744Sgs150176 		break;
1923744Sgs150176 
1924744Sgs150176 	case 8:
1925744Sgs150176 		*(uint64_t *)vaddr = (uint64_t)regval;
1926744Sgs150176 		break;
1927744Sgs150176 	}
1928744Sgs150176 }
1929744Sgs150176 
1930744Sgs150176 static enum ioc_reply rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
1931744Sgs150176 					struct iocblk *iocp);
1932744Sgs150176 #pragma	no_inline(rge_pp_ioctl)
1933744Sgs150176 
1934744Sgs150176 static enum ioc_reply
1935744Sgs150176 rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
1936744Sgs150176 {
1937744Sgs150176 	void (*ppfn)(rge_t *rgep, rge_peekpoke_t *ppd);
1938744Sgs150176 	rge_peekpoke_t *ppd;
1939744Sgs150176 	dma_area_t *areap;
1940744Sgs150176 	uint64_t sizemask;
1941744Sgs150176 	uint64_t mem_va;
1942744Sgs150176 	uint64_t maxoff;
1943744Sgs150176 	boolean_t peek;
1944744Sgs150176 
1945744Sgs150176 	switch (cmd) {
1946744Sgs150176 	default:
1947744Sgs150176 		/* NOTREACHED */
1948744Sgs150176 		rge_error(rgep, "rge_pp_ioctl: invalid cmd 0x%x", cmd);
1949744Sgs150176 		return (IOC_INVAL);
1950744Sgs150176 
1951744Sgs150176 	case RGE_PEEK:
1952744Sgs150176 		peek = B_TRUE;
1953744Sgs150176 		break;
1954744Sgs150176 
1955744Sgs150176 	case RGE_POKE:
1956744Sgs150176 		peek = B_FALSE;
1957744Sgs150176 		break;
1958744Sgs150176 	}
1959744Sgs150176 
1960744Sgs150176 	/*
1961744Sgs150176 	 * Validate format of ioctl
1962744Sgs150176 	 */
1963744Sgs150176 	if (iocp->ioc_count != sizeof (rge_peekpoke_t))
1964744Sgs150176 		return (IOC_INVAL);
1965744Sgs150176 	if (mp->b_cont == NULL)
1966744Sgs150176 		return (IOC_INVAL);
1967744Sgs150176 	ppd = (rge_peekpoke_t *)mp->b_cont->b_rptr;
1968744Sgs150176 
1969744Sgs150176 	/*
1970744Sgs150176 	 * Validate request parameters
1971744Sgs150176 	 */
1972744Sgs150176 	switch (ppd->pp_acc_space) {
1973744Sgs150176 	default:
1974744Sgs150176 		return (IOC_INVAL);
1975744Sgs150176 
1976744Sgs150176 	case RGE_PP_SPACE_CFG:
1977744Sgs150176 		/*
1978744Sgs150176 		 * Config space
1979744Sgs150176 		 */
1980744Sgs150176 		sizemask = 8|4|2|1;
1981744Sgs150176 		mem_va = 0;
1982744Sgs150176 		maxoff = PCI_CONF_HDR_SIZE;
1983744Sgs150176 		ppfn = peek ? rge_chip_peek_cfg : rge_chip_poke_cfg;
1984744Sgs150176 		break;
1985744Sgs150176 
1986744Sgs150176 	case RGE_PP_SPACE_REG:
1987744Sgs150176 		/*
1988744Sgs150176 		 * Memory-mapped I/O space
1989744Sgs150176 		 */
1990744Sgs150176 		sizemask = 8|4|2|1;
1991744Sgs150176 		mem_va = 0;
1992744Sgs150176 		maxoff = RGE_REGISTER_MAX;
1993744Sgs150176 		ppfn = peek ? rge_chip_peek_reg : rge_chip_poke_reg;
1994744Sgs150176 		break;
1995744Sgs150176 
1996744Sgs150176 	case RGE_PP_SPACE_MII:
1997744Sgs150176 		/*
1998744Sgs150176 		 * PHY's MII registers
1999744Sgs150176 		 * NB: all PHY registers are two bytes, but the
2000744Sgs150176 		 * addresses increment in ones (word addressing).
2001744Sgs150176 		 * So we scale the address here, then undo the
2002744Sgs150176 		 * transformation inside the peek/poke functions.
2003744Sgs150176 		 */
2004744Sgs150176 		ppd->pp_acc_offset *= 2;
2005744Sgs150176 		sizemask = 2;
2006744Sgs150176 		mem_va = 0;
2007744Sgs150176 		maxoff = (MII_MAXREG+1)*2;
2008744Sgs150176 		ppfn = peek ? rge_chip_peek_mii : rge_chip_poke_mii;
2009744Sgs150176 		break;
2010744Sgs150176 
2011744Sgs150176 	case RGE_PP_SPACE_RGE:
2012744Sgs150176 		/*
2013744Sgs150176 		 * RGE data structure!
2014744Sgs150176 		 */
2015744Sgs150176 		sizemask = 8|4|2|1;
2016744Sgs150176 		mem_va = (uintptr_t)rgep;
2017744Sgs150176 		maxoff = sizeof (*rgep);
2018744Sgs150176 		ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2019744Sgs150176 		break;
2020744Sgs150176 
2021744Sgs150176 	case RGE_PP_SPACE_STATISTICS:
2022744Sgs150176 	case RGE_PP_SPACE_TXDESC:
2023744Sgs150176 	case RGE_PP_SPACE_TXBUFF:
2024744Sgs150176 	case RGE_PP_SPACE_RXDESC:
2025744Sgs150176 	case RGE_PP_SPACE_RXBUFF:
2026744Sgs150176 		/*
2027744Sgs150176 		 * Various DMA_AREAs
2028744Sgs150176 		 */
2029744Sgs150176 		switch (ppd->pp_acc_space) {
2030744Sgs150176 		case RGE_PP_SPACE_TXDESC:
2031744Sgs150176 			areap = &rgep->dma_area_txdesc;
2032744Sgs150176 			break;
2033744Sgs150176 		case RGE_PP_SPACE_RXDESC:
2034744Sgs150176 			areap = &rgep->dma_area_rxdesc;
2035744Sgs150176 			break;
2036744Sgs150176 		case RGE_PP_SPACE_STATISTICS:
2037744Sgs150176 			areap = &rgep->dma_area_stats;
2038744Sgs150176 			break;
2039744Sgs150176 		}
2040744Sgs150176 
2041744Sgs150176 		sizemask = 8|4|2|1;
2042744Sgs150176 		mem_va = (uintptr_t)areap->mem_va;
2043744Sgs150176 		maxoff = areap->alength;
2044744Sgs150176 		ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2045744Sgs150176 		break;
2046744Sgs150176 	}
2047744Sgs150176 
2048744Sgs150176 	switch (ppd->pp_acc_size) {
2049744Sgs150176 	default:
2050744Sgs150176 		return (IOC_INVAL);
2051744Sgs150176 
2052744Sgs150176 	case 8:
2053744Sgs150176 	case 4:
2054744Sgs150176 	case 2:
2055744Sgs150176 	case 1:
2056744Sgs150176 		if ((ppd->pp_acc_size & sizemask) == 0)
2057744Sgs150176 			return (IOC_INVAL);
2058744Sgs150176 		break;
2059744Sgs150176 	}
2060744Sgs150176 
2061744Sgs150176 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
2062744Sgs150176 		return (IOC_INVAL);
2063744Sgs150176 
2064744Sgs150176 	if (ppd->pp_acc_offset >= maxoff)
2065744Sgs150176 		return (IOC_INVAL);
2066744Sgs150176 
2067744Sgs150176 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
2068744Sgs150176 		return (IOC_INVAL);
2069744Sgs150176 
2070744Sgs150176 	/*
2071744Sgs150176 	 * All OK - go do it!
2072744Sgs150176 	 */
2073744Sgs150176 	ppd->pp_acc_offset += mem_va;
2074744Sgs150176 	(*ppfn)(rgep, ppd);
2075744Sgs150176 	return (peek ? IOC_REPLY : IOC_ACK);
2076744Sgs150176 }
2077744Sgs150176 
2078744Sgs150176 static enum ioc_reply rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2079744Sgs150176 					struct iocblk *iocp);
2080744Sgs150176 #pragma	no_inline(rge_diag_ioctl)
2081744Sgs150176 
2082744Sgs150176 static enum ioc_reply
2083744Sgs150176 rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2084744Sgs150176 {
2085744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
2086744Sgs150176 
2087744Sgs150176 	switch (cmd) {
2088744Sgs150176 	default:
2089744Sgs150176 		/* NOTREACHED */
2090744Sgs150176 		rge_error(rgep, "rge_diag_ioctl: invalid cmd 0x%x", cmd);
2091744Sgs150176 		return (IOC_INVAL);
2092744Sgs150176 
2093744Sgs150176 	case RGE_DIAG:
2094744Sgs150176 		/*
2095744Sgs150176 		 * Currently a no-op
2096744Sgs150176 		 */
2097744Sgs150176 		return (IOC_ACK);
2098744Sgs150176 
2099744Sgs150176 	case RGE_PEEK:
2100744Sgs150176 	case RGE_POKE:
2101744Sgs150176 		return (rge_pp_ioctl(rgep, cmd, mp, iocp));
2102744Sgs150176 
2103744Sgs150176 	case RGE_PHY_RESET:
2104744Sgs150176 		return (IOC_RESTART_ACK);
2105744Sgs150176 
2106744Sgs150176 	case RGE_SOFT_RESET:
2107744Sgs150176 	case RGE_HARD_RESET:
2108744Sgs150176 		/*
2109744Sgs150176 		 * Reset and reinitialise the 570x hardware
2110744Sgs150176 		 */
2111744Sgs150176 		rge_restart(rgep);
2112744Sgs150176 		return (IOC_ACK);
2113744Sgs150176 	}
2114744Sgs150176 
2115744Sgs150176 	/* NOTREACHED */
2116744Sgs150176 }
2117744Sgs150176 
2118744Sgs150176 #endif	/* RGE_DEBUGGING || RGE_DO_PPIO */
2119744Sgs150176 
2120744Sgs150176 static enum ioc_reply rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2121744Sgs150176 				    struct iocblk *iocp);
2122744Sgs150176 #pragma	no_inline(rge_mii_ioctl)
2123744Sgs150176 
2124744Sgs150176 static enum ioc_reply
2125744Sgs150176 rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2126744Sgs150176 {
2127744Sgs150176 	struct rge_mii_rw *miirwp;
2128744Sgs150176 
2129744Sgs150176 	/*
2130744Sgs150176 	 * Validate format of ioctl
2131744Sgs150176 	 */
2132744Sgs150176 	if (iocp->ioc_count != sizeof (struct rge_mii_rw))
2133744Sgs150176 		return (IOC_INVAL);
2134744Sgs150176 	if (mp->b_cont == NULL)
2135744Sgs150176 		return (IOC_INVAL);
2136744Sgs150176 	miirwp = (struct rge_mii_rw *)mp->b_cont->b_rptr;
2137744Sgs150176 
2138744Sgs150176 	/*
2139744Sgs150176 	 * Validate request parameters ...
2140744Sgs150176 	 */
2141744Sgs150176 	if (miirwp->mii_reg > MII_MAXREG)
2142744Sgs150176 		return (IOC_INVAL);
2143744Sgs150176 
2144744Sgs150176 	switch (cmd) {
2145744Sgs150176 	default:
2146744Sgs150176 		/* NOTREACHED */
2147744Sgs150176 		rge_error(rgep, "rge_mii_ioctl: invalid cmd 0x%x", cmd);
2148744Sgs150176 		return (IOC_INVAL);
2149744Sgs150176 
2150744Sgs150176 	case RGE_MII_READ:
2151744Sgs150176 		miirwp->mii_data = rge_mii_get16(rgep, miirwp->mii_reg);
2152744Sgs150176 		return (IOC_REPLY);
2153744Sgs150176 
2154744Sgs150176 	case RGE_MII_WRITE:
2155744Sgs150176 		rge_mii_put16(rgep, miirwp->mii_reg, miirwp->mii_data);
2156744Sgs150176 		return (IOC_ACK);
2157744Sgs150176 	}
2158744Sgs150176 
2159744Sgs150176 	/* NOTREACHED */
2160744Sgs150176 }
2161744Sgs150176 
2162744Sgs150176 enum ioc_reply rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp,
2163744Sgs150176 				struct iocblk *iocp);
2164744Sgs150176 #pragma	no_inline(rge_chip_ioctl)
2165744Sgs150176 
2166744Sgs150176 enum ioc_reply
2167744Sgs150176 rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
2168744Sgs150176 {
2169744Sgs150176 	int cmd;
2170744Sgs150176 
2171744Sgs150176 	RGE_TRACE(("rge_chip_ioctl($%p, $%p, $%p, $%p)",
21725735Smx205022 	    (void *)rgep, (void *)wq, (void *)mp, (void *)iocp));
2173744Sgs150176 
2174744Sgs150176 	ASSERT(mutex_owned(rgep->genlock));
2175744Sgs150176 
2176744Sgs150176 	cmd = iocp->ioc_cmd;
2177744Sgs150176 	switch (cmd) {
2178744Sgs150176 	default:
2179744Sgs150176 		/* NOTREACHED */
2180744Sgs150176 		rge_error(rgep, "rge_chip_ioctl: invalid cmd 0x%x", cmd);
2181744Sgs150176 		return (IOC_INVAL);
2182744Sgs150176 
2183744Sgs150176 	case RGE_DIAG:
2184744Sgs150176 	case RGE_PEEK:
2185744Sgs150176 	case RGE_POKE:
2186744Sgs150176 	case RGE_PHY_RESET:
2187744Sgs150176 	case RGE_SOFT_RESET:
2188744Sgs150176 	case RGE_HARD_RESET:
2189744Sgs150176 #if	RGE_DEBUGGING || RGE_DO_PPIO
2190744Sgs150176 		return (rge_diag_ioctl(rgep, cmd, mp, iocp));
2191744Sgs150176 #else
2192744Sgs150176 		return (IOC_INVAL);
2193744Sgs150176 #endif	/* RGE_DEBUGGING || RGE_DO_PPIO */
2194744Sgs150176 
2195744Sgs150176 	case RGE_MII_READ:
2196744Sgs150176 	case RGE_MII_WRITE:
2197744Sgs150176 		return (rge_mii_ioctl(rgep, cmd, mp, iocp));
2198744Sgs150176 
2199744Sgs150176 	}
2200744Sgs150176 
2201744Sgs150176 	/* NOTREACHED */
2202744Sgs150176 }
2203