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;
4611499SZhen.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
rge_reg_get32(rge_t * rgep,uintptr_t regno)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
rge_reg_put32(rge_t * rgep,uintptr_t regno,uint32_t data)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
rge_reg_set32(rge_t * rgep,uintptr_t regno,uint32_t bits)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
rge_reg_clr32(rge_t * rgep,uintptr_t regno,uint32_t bits)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
rge_reg_get16(rge_t * rgep,uintptr_t regno)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
rge_reg_put16(rge_t * rgep,uintptr_t regno,uint16_t data)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
rge_reg_get8(rge_t * rgep,uintptr_t regno)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
rge_reg_put8(rge_t * rgep,uintptr_t regno,uint8_t data)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
rge_reg_set8(rge_t * rgep,uintptr_t regno,uint8_t bits)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
rge_reg_clr8(rge_t * rgep,uintptr_t regno,uint8_t bits)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
rge_mii_get16(rge_t * rgep,uintptr_t mii)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
rge_mii_put16(rge_t * rgep,uintptr_t mii,uint16_t data)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
rge_ephy_put16(rge_t * rgep,uintptr_t emii,uint16_t data)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
rge_atomic_shl32(uint32_t * sp,uint_t count)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
rge_phydump(rge_t * rgep)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
rge_phy_check(rge_t * rgep)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
rge_phy_reset(rge_t * rgep)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
rge_phy_update(rge_t * rgep)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
rge_phy_init(rge_t * rgep)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
rge_chip_ident(rge_t * rgep)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 /*
711*11618SLi-Zhen.You@Sun.COM * Workaround for 8101E_C
712*11618SLi-Zhen.You@Sun.COM */
713*11618SLi-Zhen.You@Sun.COM if (chip->mac_ver == MAC_VER_8101E_C) {
714*11618SLi-Zhen.You@Sun.COM chip->is_pcie = B_FALSE;
715*11618SLi-Zhen.You@Sun.COM }
716*11618SLi-Zhen.You@Sun.COM
717*11618SLi-Zhen.You@Sun.COM /*
7182544Sgs150176 * Read and record PHY version
7192544Sgs150176 */
720744Sgs150176 val16 = rge_mii_get16(rgep, PHY_ID_REG_2);
721744Sgs150176 val16 &= PHY_VER_MASK;
722744Sgs150176 chip->phy_ver = val16;
723744Sgs150176
7242544Sgs150176 /* set pci latency timer */
7252544Sgs150176 if (chip->mac_ver == MAC_VER_8169 ||
7264533Sgs150176 chip->mac_ver == MAC_VER_8169S_D ||
72711499SZhen.W@Sun.COM chip->mac_ver == MAC_VER_8169S_E ||
7284533Sgs150176 chip->mac_ver == MAC_VER_8169SC)
7292544Sgs150176 pci_config_put8(rgep->cfg_handle, PCI_CONF_LATENCY_TIMER, 0x40);
7302544Sgs150176
7314533Sgs150176 if (chip->mac_ver == MAC_VER_8169SC) {
7324533Sgs150176 val16 = rge_reg_get16(rgep, RT_CONFIG_1_REG);
7334533Sgs150176 val16 &= 0x0300;
7344533Sgs150176 if (val16 == 0x1) /* 66Mhz PCI */
73511499SZhen.W@Sun.COM rge_reg_put32(rgep, 0x7c, 0x000700ff);
7364533Sgs150176 else if (val16 == 0x0) /* 33Mhz PCI */
73711499SZhen.W@Sun.COM rge_reg_put32(rgep, 0x7c, 0x0007ff00);
7384533Sgs150176 }
7394533Sgs150176
7402544Sgs150176 /*
7412544Sgs150176 * PCIE chipset require the Rx buffer start address must be
7422544Sgs150176 * 8-byte alignment and the Rx buffer size must be multiple of 8.
7432544Sgs150176 * We'll just use bcopy in receive procedure for the PCIE chipset.
7442544Sgs150176 */
7452544Sgs150176 if (chip->is_pcie) {
7462544Sgs150176 rgep->chip_flags |= CHIP_FLAG_FORCE_BCOPY;
7472544Sgs150176 if (rgep->default_mtu > ETHERMTU) {
7482544Sgs150176 rge_notice(rgep, "Jumbo packets not supported "
7492544Sgs150176 "for this PCIE chipset");
7502544Sgs150176 rgep->default_mtu = ETHERMTU;
7512544Sgs150176 }
7522544Sgs150176 }
7532544Sgs150176 if (rgep->chip_flags & CHIP_FLAG_FORCE_BCOPY)
7542544Sgs150176 rgep->head_room = 0;
7552544Sgs150176 else
7562544Sgs150176 rgep->head_room = RGE_HEADROOM;
7572544Sgs150176
7582544Sgs150176 /*
7592544Sgs150176 * Initialize other variables.
7602544Sgs150176 */
7612544Sgs150176 if (rgep->default_mtu < ETHERMTU || rgep->default_mtu > RGE_JUMBO_MTU)
7622544Sgs150176 rgep->default_mtu = ETHERMTU;
7632544Sgs150176 if (rgep->default_mtu > ETHERMTU) {
764744Sgs150176 rgep->rxbuf_size = RGE_BUFF_SIZE_JUMBO;
765744Sgs150176 rgep->txbuf_size = RGE_BUFF_SIZE_JUMBO;
766744Sgs150176 rgep->ethmax_size = RGE_JUMBO_SIZE;
767744Sgs150176 } else {
768744Sgs150176 rgep->rxbuf_size = RGE_BUFF_SIZE_STD;
769744Sgs150176 rgep->txbuf_size = RGE_BUFF_SIZE_STD;
770744Sgs150176 rgep->ethmax_size = ETHERMAX;
771744Sgs150176 }
772744Sgs150176 chip->rxconfig = RX_CONFIG_DEFAULT;
773744Sgs150176 chip->txconfig = TX_CONFIG_DEFAULT;
774744Sgs150176
77511365SZhen.W@Sun.COM /* interval to update statistics for polling mode */
77611365SZhen.W@Sun.COM rgep->tick_delta = drv_usectohz(1000*1000/CLK_TICK);
77711365SZhen.W@Sun.COM
77811365SZhen.W@Sun.COM /* ensure we are not in polling mode */
77911365SZhen.W@Sun.COM rgep->curr_tick = ddi_get_lbolt() - 2*rgep->tick_delta;
780744Sgs150176 RGE_TRACE(("%s: MAC version = %x, PHY version = %x",
781744Sgs150176 rgep->ifname, chip->mac_ver, chip->phy_ver));
782744Sgs150176 }
783744Sgs150176
784744Sgs150176 /*
785744Sgs150176 * Perform first-stage chip (re-)initialisation, using only config-space
786744Sgs150176 * accesses:
787744Sgs150176 *
788744Sgs150176 * + Read the vendor/device/revision/subsystem/cache-line-size registers,
789744Sgs150176 * returning the data in the structure pointed to by <idp>.
790744Sgs150176 * + Enable Memory Space accesses.
791744Sgs150176 * + Enable Bus Mastering according.
792744Sgs150176 */
793744Sgs150176 void rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp);
794744Sgs150176 #pragma no_inline(rge_chip_cfg_init)
795744Sgs150176
796744Sgs150176 void
rge_chip_cfg_init(rge_t * rgep,chip_id_t * cidp)797744Sgs150176 rge_chip_cfg_init(rge_t *rgep, chip_id_t *cidp)
798744Sgs150176 {
799744Sgs150176 ddi_acc_handle_t handle;
800744Sgs150176 uint16_t commd;
801744Sgs150176
802744Sgs150176 handle = rgep->cfg_handle;
803744Sgs150176
804744Sgs150176 /*
805744Sgs150176 * Save PCI cache line size and subsystem vendor ID
806744Sgs150176 */
807744Sgs150176 cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
808744Sgs150176 cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
809744Sgs150176 cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
810744Sgs150176 cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
811744Sgs150176 cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
812744Sgs150176 cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
813744Sgs150176 cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
814744Sgs150176 cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
815744Sgs150176
816744Sgs150176 /*
817744Sgs150176 * Turn on Master Enable (DMA) and IO Enable bits.
818744Sgs150176 * Enable PCI Memory Space accesses
819744Sgs150176 */
820744Sgs150176 commd = cidp->command;
821744Sgs150176 commd |= PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
822744Sgs150176 pci_config_put16(handle, PCI_CONF_COMM, commd);
823744Sgs150176
824744Sgs150176 RGE_DEBUG(("rge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
8255735Smx205022 cidp->vendor, cidp->device, cidp->revision));
826744Sgs150176 RGE_DEBUG(("rge_chip_cfg_init: subven 0x%x subdev 0x%x",
8275735Smx205022 cidp->subven, cidp->subdev));
828744Sgs150176 RGE_DEBUG(("rge_chip_cfg_init: clsize %d latency %d command 0x%x",
8295735Smx205022 cidp->clsize, cidp->latency, cidp->command));
830744Sgs150176 }
831744Sgs150176
832744Sgs150176 int rge_chip_reset(rge_t *rgep);
833744Sgs150176 #pragma no_inline(rge_chip_reset)
834744Sgs150176
835744Sgs150176 int
rge_chip_reset(rge_t * rgep)836744Sgs150176 rge_chip_reset(rge_t *rgep)
837744Sgs150176 {
838744Sgs150176 int i;
839744Sgs150176 uint8_t val8;
840744Sgs150176
841744Sgs150176 /*
842744Sgs150176 * Chip should be in STOP state
843744Sgs150176 */
844744Sgs150176 rge_reg_clr8(rgep, RT_COMMAND_REG,
845744Sgs150176 RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
846744Sgs150176
847744Sgs150176 /*
848744Sgs150176 * Disable interrupt
849744Sgs150176 */
850744Sgs150176 rgep->int_mask = INT_MASK_NONE;
8512544Sgs150176 rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
852744Sgs150176
853744Sgs150176 /*
854744Sgs150176 * Clear pended interrupt
855744Sgs150176 */
856744Sgs150176 rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
857744Sgs150176
858744Sgs150176 /*
859744Sgs150176 * Reset chip
860744Sgs150176 */
861744Sgs150176 rge_reg_set8(rgep, RT_COMMAND_REG, RT_COMMAND_RESET);
862744Sgs150176
863744Sgs150176 /*
864744Sgs150176 * Wait for reset success
865744Sgs150176 */
866744Sgs150176 for (i = 0; i < CHIP_RESET_LOOP; i++) {
867744Sgs150176 drv_usecwait(10);
868744Sgs150176 val8 = rge_reg_get8(rgep, RT_COMMAND_REG);
869744Sgs150176 if (!(val8 & RT_COMMAND_RESET)) {
870744Sgs150176 rgep->rge_chip_state = RGE_CHIP_RESET;
871744Sgs150176 return (0);
872744Sgs150176 }
873744Sgs150176 }
874744Sgs150176 RGE_REPORT((rgep, "rge_chip_reset fail."));
875744Sgs150176 return (-1);
876744Sgs150176 }
877744Sgs150176
878744Sgs150176 void rge_chip_init(rge_t *rgep);
879744Sgs150176 #pragma no_inline(rge_chip_init)
880744Sgs150176
881744Sgs150176 void
rge_chip_init(rge_t * rgep)882744Sgs150176 rge_chip_init(rge_t *rgep)
883744Sgs150176 {
884744Sgs150176 uint32_t val32;
8852544Sgs150176 uint32_t val16;
8862544Sgs150176 uint32_t *hashp;
8872544Sgs150176 chip_id_t *chip = &rgep->chipid;
8882544Sgs150176
88911365SZhen.W@Sun.COM /*
89011365SZhen.W@Sun.COM * Increase the threshold voltage of RX sensitivity
89111365SZhen.W@Sun.COM */
89211365SZhen.W@Sun.COM if (chip->mac_ver == MAC_VER_8168B_B ||
89311365SZhen.W@Sun.COM chip->mac_ver == MAC_VER_8168B_C ||
894*11618SLi-Zhen.You@Sun.COM chip->mac_ver == MAC_VER_8101E) {
89511365SZhen.W@Sun.COM rge_ephy_put16(rgep, 0x01, 0x1bd3);
89611365SZhen.W@Sun.COM }
8972544Sgs150176
89811365SZhen.W@Sun.COM if (chip->mac_ver == MAC_VER_8168 ||
89911365SZhen.W@Sun.COM chip->mac_ver == MAC_VER_8168B_B) {
9002544Sgs150176 val16 = rge_reg_get8(rgep, PHY_STATUS_REG);
9012544Sgs150176 val16 = 0x12<<8 | val16;
90211365SZhen.W@Sun.COM rge_reg_put16(rgep, PHY_STATUS_REG, val16);
90311365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00021c01);
90411365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f088);
90511365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_DATA_REG, 0x00004000);
90611365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f0b0);
90711365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x0000f068);
90811365SZhen.W@Sun.COM val32 = rge_reg_get32(rgep, RT_CSI_DATA_REG);
90911365SZhen.W@Sun.COM val32 |= 0x7000;
91011365SZhen.W@Sun.COM val32 &= 0xffff5fff;
91111365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_DATA_REG, val32);
91211365SZhen.W@Sun.COM rge_reg_put32(rgep, RT_CSI_ACCESS_REG, 0x8000f068);
9132544Sgs150176 }
914744Sgs150176
915744Sgs150176 /*
916744Sgs150176 * Config MII register
917744Sgs150176 */
918744Sgs150176 rgep->param_link_up = LINK_STATE_DOWN;
919744Sgs150176 rge_phy_update(rgep);
920744Sgs150176
921744Sgs150176 /*
922744Sgs150176 * Enable Rx checksum offload.
923744Sgs150176 * Then for vlan support, we must enable receive vlan de-tagging.
924744Sgs150176 * Otherwise, there'll be checksum error.
925744Sgs150176 */
9262544Sgs150176 val16 = rge_reg_get16(rgep, CPLUS_COMMAND_REG);
9272544Sgs150176 val16 |= RX_CKSM_OFFLOAD | RX_VLAN_DETAG;
9282544Sgs150176 if (chip->mac_ver == MAC_VER_8169S_D) {
9292544Sgs150176 val16 |= CPLUS_BIT14 | MUL_PCI_RW_ENABLE;
930744Sgs150176 rge_reg_put8(rgep, RESV_82_REG, 0x01);
931744Sgs150176 }
93211499SZhen.W@Sun.COM if (chip->mac_ver == MAC_VER_8169S_E ||
93311499SZhen.W@Sun.COM chip->mac_ver == MAC_VER_8169SC) {
93411499SZhen.W@Sun.COM val16 |= MUL_PCI_RW_ENABLE;
93511499SZhen.W@Sun.COM }
9362544Sgs150176 rge_reg_put16(rgep, CPLUS_COMMAND_REG, val16 & (~0x03));
937744Sgs150176
938744Sgs150176 /*
939744Sgs150176 * Start transmit/receive before set tx/rx configuration register
940744Sgs150176 */
9412544Sgs150176 if (!chip->is_pcie)
9422544Sgs150176 rge_reg_set8(rgep, RT_COMMAND_REG,
9432544Sgs150176 RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
944744Sgs150176
945744Sgs150176 /*
946744Sgs150176 * Set dump tally counter register
947744Sgs150176 */
948744Sgs150176 val32 = rgep->dma_area_stats.cookie.dmac_laddress >> 32;
949744Sgs150176 rge_reg_put32(rgep, DUMP_COUNTER_REG_1, val32);
950744Sgs150176 val32 = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
951744Sgs150176 val32 &= DUMP_COUNTER_REG_RESV;
952744Sgs150176 val32 |= rgep->dma_area_stats.cookie.dmac_laddress;
953744Sgs150176 rge_reg_put32(rgep, DUMP_COUNTER_REG_0, val32);
954744Sgs150176
955744Sgs150176 /*
956744Sgs150176 * Change to config register write enable mode
957744Sgs150176 */
958744Sgs150176 rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
959744Sgs150176
960744Sgs150176 /*
961744Sgs150176 * Set Tx/Rx maximum packet size
962744Sgs150176 */
9632544Sgs150176 if (rgep->default_mtu > ETHERMTU) {
964744Sgs150176 rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_JUMBO);
965744Sgs150176 rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_JUMBO);
9665735Smx205022 } else if (rgep->chipid.mac_ver != MAC_VER_8101E) {
967744Sgs150176 rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD);
968744Sgs150176 rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD);
9695735Smx205022 } else {
9705735Smx205022 rge_reg_put8(rgep, TX_MAX_PKTSIZE_REG, TX_PKTSIZE_STD_8101E);
9715735Smx205022 rge_reg_put16(rgep, RX_MAX_PKTSIZE_REG, RX_PKTSIZE_STD_8101E);
972744Sgs150176 }
973744Sgs150176
974744Sgs150176 /*
975744Sgs150176 * Set receive configuration register
976744Sgs150176 */
977744Sgs150176 val32 = rge_reg_get32(rgep, RX_CONFIG_REG);
978744Sgs150176 val32 &= RX_CONFIG_REG_RESV;
979744Sgs150176 if (rgep->promisc)
980744Sgs150176 val32 |= RX_ACCEPT_ALL_PKT;
9812544Sgs150176 rge_reg_put32(rgep, RX_CONFIG_REG, val32 | chip->rxconfig);
982744Sgs150176
983744Sgs150176 /*
984744Sgs150176 * Set transmit configuration register
985744Sgs150176 */
986744Sgs150176 val32 = rge_reg_get32(rgep, TX_CONFIG_REG);
987744Sgs150176 val32 &= TX_CONFIG_REG_RESV;
9882544Sgs150176 rge_reg_put32(rgep, TX_CONFIG_REG, val32 | chip->txconfig);
989744Sgs150176
990744Sgs150176 /*
991744Sgs150176 * Set Tx/Rx descriptor register
992744Sgs150176 */
993744Sgs150176 val32 = rgep->tx_desc.cookie.dmac_laddress;
994744Sgs150176 rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_LO_REG, val32);
995744Sgs150176 val32 = rgep->tx_desc.cookie.dmac_laddress >> 32;
996744Sgs150176 rge_reg_put32(rgep, NORMAL_TX_RING_ADDR_HI_REG, val32);
997744Sgs150176 rge_reg_put32(rgep, HIGH_TX_RING_ADDR_LO_REG, 0);
998744Sgs150176 rge_reg_put32(rgep, HIGH_TX_RING_ADDR_HI_REG, 0);
999744Sgs150176 val32 = rgep->rx_desc.cookie.dmac_laddress;
1000744Sgs150176 rge_reg_put32(rgep, RX_RING_ADDR_LO_REG, val32);
1001744Sgs150176 val32 = rgep->rx_desc.cookie.dmac_laddress >> 32;
1002744Sgs150176 rge_reg_put32(rgep, RX_RING_ADDR_HI_REG, val32);
1003744Sgs150176
1004744Sgs150176 /*
1005744Sgs150176 * Suggested setting from Realtek
1006744Sgs150176 */
10075735Smx205022 if (rgep->chipid.mac_ver != MAC_VER_8101E)
10085735Smx205022 rge_reg_put16(rgep, RESV_E2_REG, 0x282a);
10095735Smx205022 else
10105735Smx205022 rge_reg_put16(rgep, RESV_E2_REG, 0x0000);
1011744Sgs150176
1012744Sgs150176 /*
1013744Sgs150176 * Set multicast register
1014744Sgs150176 */
10152544Sgs150176 hashp = (uint32_t *)rgep->mcast_hash;
101610814SKHF04453@nifty.ne.jp if (rgep->promisc) {
101710814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
101810814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
101910814SKHF04453@nifty.ne.jp } else {
102010814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
102110814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
102210814SKHF04453@nifty.ne.jp }
1023744Sgs150176
1024744Sgs150176 /*
1025744Sgs150176 * Msic register setting:
1026744Sgs150176 * -- Missed packet counter: clear it
1027744Sgs150176 * -- TimerInt Register
1028744Sgs150176 * -- Timer count register
1029744Sgs150176 */
1030744Sgs150176 rge_reg_put32(rgep, RX_PKT_MISS_COUNT_REG, 0);
1031744Sgs150176 rge_reg_put32(rgep, TIMER_INT_REG, TIMER_INT_NONE);
1032744Sgs150176 rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
10334533Sgs150176
10344533Sgs150176 /*
10357825SMin.Xu@Sun.COM * disable the Unicast Wakeup Frame capability
10367825SMin.Xu@Sun.COM */
10377825SMin.Xu@Sun.COM rge_reg_clr8(rgep, RT_CONFIG_5_REG, RT_UNI_WAKE_FRAME);
10387825SMin.Xu@Sun.COM
10397825SMin.Xu@Sun.COM /*
10404533Sgs150176 * Return to normal network/host communication mode
10414533Sgs150176 */
10424533Sgs150176 rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
10434533Sgs150176 drv_usecwait(20);
1044744Sgs150176 }
1045744Sgs150176
1046744Sgs150176 /*
1047744Sgs150176 * rge_chip_start() -- start the chip transmitting and/or receiving,
1048744Sgs150176 * including enabling interrupts
1049744Sgs150176 */
1050744Sgs150176 void rge_chip_start(rge_t *rgep);
1051744Sgs150176 #pragma no_inline(rge_chip_start)
1052744Sgs150176
1053744Sgs150176 void
rge_chip_start(rge_t * rgep)1054744Sgs150176 rge_chip_start(rge_t *rgep)
1055744Sgs150176 {
1056744Sgs150176 /*
1057744Sgs150176 * Clear statistics
1058744Sgs150176 */
1059744Sgs150176 bzero(&rgep->stats, sizeof (rge_stats_t));
1060744Sgs150176 DMA_ZERO(rgep->dma_area_stats);
1061744Sgs150176
1062744Sgs150176 /*
1063744Sgs150176 * Start transmit/receive
1064744Sgs150176 */
1065744Sgs150176 rge_reg_set8(rgep, RT_COMMAND_REG,
1066744Sgs150176 RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1067744Sgs150176
1068744Sgs150176 /*
1069744Sgs150176 * Enable interrupt
1070744Sgs150176 */
1071744Sgs150176 rgep->int_mask = RGE_INT_MASK;
107211365SZhen.W@Sun.COM if (rgep->chipid.is_pcie) {
107311365SZhen.W@Sun.COM rgep->int_mask |= NO_TXDESC_INT;
107411365SZhen.W@Sun.COM }
107511499SZhen.W@Sun.COM rgep->rx_fifo_ovf = 0;
107611499SZhen.W@Sun.COM rgep->int_mask |= RX_FIFO_OVERFLOW_INT;
10772544Sgs150176 rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1078744Sgs150176
1079744Sgs150176 /*
1080744Sgs150176 * All done!
1081744Sgs150176 */
1082744Sgs150176 rgep->rge_chip_state = RGE_CHIP_RUNNING;
1083744Sgs150176 }
1084744Sgs150176
1085744Sgs150176 /*
1086744Sgs150176 * rge_chip_stop() -- stop board receiving
10877656SSherry.Moore@Sun.COM *
10887656SSherry.Moore@Sun.COM * Since this function is also invoked by rge_quiesce(), it
10897656SSherry.Moore@Sun.COM * must not block; also, no tracing or logging takes place
10907656SSherry.Moore@Sun.COM * when invoked by rge_quiesce().
1091744Sgs150176 */
1092744Sgs150176 void rge_chip_stop(rge_t *rgep, boolean_t fault);
1093744Sgs150176 #pragma no_inline(rge_chip_stop)
1094744Sgs150176
1095744Sgs150176 void
rge_chip_stop(rge_t * rgep,boolean_t fault)1096744Sgs150176 rge_chip_stop(rge_t *rgep, boolean_t fault)
1097744Sgs150176 {
1098744Sgs150176 /*
1099744Sgs150176 * Disable interrupt
1100744Sgs150176 */
1101744Sgs150176 rgep->int_mask = INT_MASK_NONE;
11022544Sgs150176 rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
1103744Sgs150176
1104744Sgs150176 /*
1105744Sgs150176 * Clear pended interrupt
1106744Sgs150176 */
11076764Smx205022 if (!rgep->suspended) {
11086764Smx205022 rge_reg_put16(rgep, INT_STATUS_REG, INT_MASK_ALL);
11096764Smx205022 }
1110744Sgs150176
1111744Sgs150176 /*
1112744Sgs150176 * Stop the board and disable transmit/receive
1113744Sgs150176 */
1114744Sgs150176 rge_reg_clr8(rgep, RT_COMMAND_REG,
1115744Sgs150176 RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1116744Sgs150176
1117744Sgs150176 if (fault)
1118744Sgs150176 rgep->rge_chip_state = RGE_CHIP_FAULT;
1119744Sgs150176 else
1120744Sgs150176 rgep->rge_chip_state = RGE_CHIP_STOPPED;
1121744Sgs150176 }
1122744Sgs150176
1123744Sgs150176 /*
1124744Sgs150176 * rge_get_mac_addr() -- get the MAC address on NIC
1125744Sgs150176 */
1126744Sgs150176 static void rge_get_mac_addr(rge_t *rgep);
1127744Sgs150176 #pragma inline(rge_get_mac_addr)
1128744Sgs150176
1129744Sgs150176 static void
rge_get_mac_addr(rge_t * rgep)1130744Sgs150176 rge_get_mac_addr(rge_t *rgep)
1131744Sgs150176 {
1132744Sgs150176 uint8_t *macaddr = rgep->netaddr;
1133744Sgs150176 uint32_t val32;
1134744Sgs150176
1135744Sgs150176 /*
1136744Sgs150176 * Read first 4-byte of mac address
1137744Sgs150176 */
1138744Sgs150176 val32 = rge_reg_get32(rgep, ID_0_REG);
1139744Sgs150176 macaddr[0] = val32 & 0xff;
1140744Sgs150176 val32 = val32 >> 8;
1141744Sgs150176 macaddr[1] = val32 & 0xff;
1142744Sgs150176 val32 = val32 >> 8;
1143744Sgs150176 macaddr[2] = val32 & 0xff;
1144744Sgs150176 val32 = val32 >> 8;
1145744Sgs150176 macaddr[3] = val32 & 0xff;
1146744Sgs150176
1147744Sgs150176 /*
1148744Sgs150176 * Read last 2-byte of mac address
1149744Sgs150176 */
1150744Sgs150176 val32 = rge_reg_get32(rgep, ID_4_REG);
1151744Sgs150176 macaddr[4] = val32 & 0xff;
1152744Sgs150176 val32 = val32 >> 8;
1153744Sgs150176 macaddr[5] = val32 & 0xff;
1154744Sgs150176 }
1155744Sgs150176
1156744Sgs150176 static void rge_set_mac_addr(rge_t *rgep);
1157744Sgs150176 #pragma inline(rge_set_mac_addr)
1158744Sgs150176
1159744Sgs150176 static void
rge_set_mac_addr(rge_t * rgep)1160744Sgs150176 rge_set_mac_addr(rge_t *rgep)
1161744Sgs150176 {
1162744Sgs150176 uint8_t *p = rgep->netaddr;
1163744Sgs150176 uint32_t val32;
1164744Sgs150176
1165744Sgs150176 /*
1166744Sgs150176 * Change to config register write enable mode
1167744Sgs150176 */
1168744Sgs150176 rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1169744Sgs150176
1170744Sgs150176 /*
1171744Sgs150176 * Get first 4 bytes of mac address
1172744Sgs150176 */
1173744Sgs150176 val32 = p[3];
1174744Sgs150176 val32 = val32 << 8;
1175744Sgs150176 val32 |= p[2];
1176744Sgs150176 val32 = val32 << 8;
1177744Sgs150176 val32 |= p[1];
1178744Sgs150176 val32 = val32 << 8;
1179744Sgs150176 val32 |= p[0];
1180744Sgs150176
1181744Sgs150176 /*
1182744Sgs150176 * Set first 4 bytes of mac address
1183744Sgs150176 */
1184744Sgs150176 rge_reg_put32(rgep, ID_0_REG, val32);
1185744Sgs150176
1186744Sgs150176 /*
1187744Sgs150176 * Get last 2 bytes of mac address
1188744Sgs150176 */
1189744Sgs150176 val32 = p[5];
1190744Sgs150176 val32 = val32 << 8;
1191744Sgs150176 val32 |= p[4];
1192744Sgs150176
1193744Sgs150176 /*
1194744Sgs150176 * Set last 2 bytes of mac address
1195744Sgs150176 */
1196744Sgs150176 val32 |= rge_reg_get32(rgep, ID_4_REG) & ~0xffff;
1197744Sgs150176 rge_reg_put32(rgep, ID_4_REG, val32);
1198744Sgs150176
1199744Sgs150176 /*
1200744Sgs150176 * Return to normal network/host communication mode
1201744Sgs150176 */
1202744Sgs150176 rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
1203744Sgs150176 }
1204744Sgs150176
1205744Sgs150176 static void rge_set_multi_addr(rge_t *rgep);
1206744Sgs150176 #pragma inline(rge_set_multi_addr)
1207744Sgs150176
1208744Sgs150176 static void
rge_set_multi_addr(rge_t * rgep)1209744Sgs150176 rge_set_multi_addr(rge_t *rgep)
1210744Sgs150176 {
1211744Sgs150176 uint32_t *hashp;
1212744Sgs150176
12132544Sgs150176 hashp = (uint32_t *)rgep->mcast_hash;
12144533Sgs150176
12154533Sgs150176 /*
12164533Sgs150176 * Change to config register write enable mode
12174533Sgs150176 */
12187825SMin.Xu@Sun.COM if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12194533Sgs150176 rge_reg_set8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12207825SMin.Xu@Sun.COM }
122110814SKHF04453@nifty.ne.jp if (rgep->promisc) {
122210814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_0_REG, ~0U);
122310814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_4_REG, ~0U);
122410814SKHF04453@nifty.ne.jp } else {
122510814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_0_REG, RGE_BSWAP_32(hashp[0]));
122610814SKHF04453@nifty.ne.jp rge_reg_put32(rgep, MULTICAST_4_REG, RGE_BSWAP_32(hashp[1]));
122710814SKHF04453@nifty.ne.jp }
12284533Sgs150176
12294533Sgs150176 /*
12304533Sgs150176 * Return to normal network/host communication mode
12314533Sgs150176 */
12327825SMin.Xu@Sun.COM if (rgep->chipid.mac_ver == MAC_VER_8169SC) {
12334533Sgs150176 rge_reg_clr8(rgep, RT_93c46_COMMOND_REG, RT_93c46_MODE_CONFIG);
12347825SMin.Xu@Sun.COM }
1235744Sgs150176 }
1236744Sgs150176
1237744Sgs150176 static void rge_set_promisc(rge_t *rgep);
1238744Sgs150176 #pragma inline(rge_set_promisc)
1239744Sgs150176
1240744Sgs150176 static void
rge_set_promisc(rge_t * rgep)1241744Sgs150176 rge_set_promisc(rge_t *rgep)
1242744Sgs150176 {
1243744Sgs150176 if (rgep->promisc)
1244744Sgs150176 rge_reg_set32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1245744Sgs150176 else
1246744Sgs150176 rge_reg_clr32(rgep, RX_CONFIG_REG, RX_ACCEPT_ALL_PKT);
1247744Sgs150176 }
1248744Sgs150176
1249744Sgs150176 /*
1250744Sgs150176 * rge_chip_sync() -- program the chip with the unicast MAC address,
1251744Sgs150176 * the multicast hash table, the required level of promiscuity, and
1252744Sgs150176 * the current loopback mode ...
1253744Sgs150176 */
1254744Sgs150176 void rge_chip_sync(rge_t *rgep, enum rge_sync_op todo);
1255744Sgs150176 #pragma no_inline(rge_chip_sync)
1256744Sgs150176
1257744Sgs150176 void
rge_chip_sync(rge_t * rgep,enum rge_sync_op todo)1258744Sgs150176 rge_chip_sync(rge_t *rgep, enum rge_sync_op todo)
1259744Sgs150176 {
1260744Sgs150176 switch (todo) {
1261744Sgs150176 case RGE_GET_MAC:
1262744Sgs150176 rge_get_mac_addr(rgep);
1263744Sgs150176 break;
1264744Sgs150176 case RGE_SET_MAC:
1265744Sgs150176 /* Reprogram the unicast MAC address(es) ... */
1266744Sgs150176 rge_set_mac_addr(rgep);
1267744Sgs150176 break;
1268744Sgs150176 case RGE_SET_MUL:
1269744Sgs150176 /* Reprogram the hashed multicast address table ... */
1270744Sgs150176 rge_set_multi_addr(rgep);
1271744Sgs150176 break;
1272744Sgs150176 case RGE_SET_PROMISC:
1273744Sgs150176 /* Set or clear the PROMISCUOUS mode bit */
127410814SKHF04453@nifty.ne.jp rge_set_multi_addr(rgep);
1275744Sgs150176 rge_set_promisc(rgep);
1276744Sgs150176 break;
1277744Sgs150176 default:
1278744Sgs150176 break;
1279744Sgs150176 }
1280744Sgs150176 }
1281744Sgs150176
12828275SEric Cheng void rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag);
1283744Sgs150176 #pragma no_inline(rge_chip_blank)
1284744Sgs150176
12858275SEric Cheng /* ARGSUSED */
1286744Sgs150176 void
rge_chip_blank(void * arg,time_t ticks,uint_t count,int flag)12878275SEric Cheng rge_chip_blank(void *arg, time_t ticks, uint_t count, int flag)
1288744Sgs150176 {
1289744Sgs150176 _NOTE(ARGUNUSED(arg, ticks, count));
1290744Sgs150176 }
1291744Sgs150176
1292744Sgs150176 void rge_tx_trigger(rge_t *rgep);
1293744Sgs150176 #pragma no_inline(rge_tx_trigger)
1294744Sgs150176
1295744Sgs150176 void
rge_tx_trigger(rge_t * rgep)1296744Sgs150176 rge_tx_trigger(rge_t *rgep)
1297744Sgs150176 {
129811365SZhen.W@Sun.COM rge_reg_put8(rgep, TX_RINGS_POLL_REG, NORMAL_TX_RING_POLL);
1299744Sgs150176 }
1300744Sgs150176
1301744Sgs150176 void rge_hw_stats_dump(rge_t *rgep);
1302744Sgs150176 #pragma no_inline(rge_tx_trigger)
1303744Sgs150176
1304744Sgs150176 void
rge_hw_stats_dump(rge_t * rgep)1305744Sgs150176 rge_hw_stats_dump(rge_t *rgep)
1306744Sgs150176 {
1307744Sgs150176 int i = 0;
130811477SLi-Zhen.You@Sun.COM uint32_t regval = 0;
1309744Sgs150176
131011477SLi-Zhen.You@Sun.COM if (rgep->rge_mac_state == RGE_MAC_STOPPED)
131111477SLi-Zhen.You@Sun.COM return;
131211477SLi-Zhen.You@Sun.COM
131311477SLi-Zhen.You@Sun.COM regval = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
131411477SLi-Zhen.You@Sun.COM while (regval & DUMP_START) {
1315744Sgs150176 drv_usecwait(100);
1316744Sgs150176 if (++i > STATS_DUMP_LOOP) {
1317744Sgs150176 RGE_DEBUG(("rge h/w statistics dump fail!"));
1318744Sgs150176 rgep->rge_chip_state = RGE_CHIP_ERROR;
1319744Sgs150176 return;
1320744Sgs150176 }
132111477SLi-Zhen.You@Sun.COM regval = rge_reg_get32(rgep, DUMP_COUNTER_REG_0);
1322744Sgs150176 }
1323744Sgs150176 DMA_SYNC(rgep->dma_area_stats, DDI_DMA_SYNC_FORKERNEL);
1324744Sgs150176
1325744Sgs150176 /*
1326744Sgs150176 * Start H/W statistics dump for RTL8169 chip
1327744Sgs150176 */
1328744Sgs150176 rge_reg_set32(rgep, DUMP_COUNTER_REG_0, DUMP_START);
1329744Sgs150176 }
1330744Sgs150176
1331744Sgs150176 /*
1332744Sgs150176 * ========== Hardware interrupt handler ==========
1333744Sgs150176 */
1334744Sgs150176
1335744Sgs150176 #undef RGE_DBG
1336744Sgs150176 #define RGE_DBG RGE_DBG_INT /* debug flag for this code */
1337744Sgs150176
1338744Sgs150176 static void rge_wake_factotum(rge_t *rgep);
1339744Sgs150176 #pragma inline(rge_wake_factotum)
1340744Sgs150176
1341744Sgs150176 static void
rge_wake_factotum(rge_t * rgep)1342744Sgs150176 rge_wake_factotum(rge_t *rgep)
1343744Sgs150176 {
1344744Sgs150176 if (rgep->factotum_flag == 0) {
1345744Sgs150176 rgep->factotum_flag = 1;
13462544Sgs150176 (void) ddi_intr_trigger_softint(rgep->factotum_hdl, NULL);
1347744Sgs150176 }
1348744Sgs150176 }
1349744Sgs150176
1350744Sgs150176 /*
1351744Sgs150176 * rge_intr() -- handle chip interrupts
1352744Sgs150176 */
13532544Sgs150176 uint_t rge_intr(caddr_t arg1, caddr_t arg2);
1354744Sgs150176 #pragma no_inline(rge_intr)
1355744Sgs150176
1356744Sgs150176 uint_t
rge_intr(caddr_t arg1,caddr_t arg2)13572544Sgs150176 rge_intr(caddr_t arg1, caddr_t arg2)
1358744Sgs150176 {
13592544Sgs150176 rge_t *rgep = (rge_t *)arg1;
1360744Sgs150176 uint16_t int_status;
136111365SZhen.W@Sun.COM clock_t now;
136211365SZhen.W@Sun.COM uint32_t tx_pkts;
136311365SZhen.W@Sun.COM uint32_t rx_pkts;
136411365SZhen.W@Sun.COM uint32_t poll_rate;
136511365SZhen.W@Sun.COM uint32_t opt_pkts;
136611365SZhen.W@Sun.COM uint32_t opt_intrs;
136711365SZhen.W@Sun.COM boolean_t update_int_mask = B_FALSE;
136811365SZhen.W@Sun.COM uint32_t itimer;
1369744Sgs150176
13702544Sgs150176 _NOTE(ARGUNUSED(arg2))
13712544Sgs150176
1372744Sgs150176 mutex_enter(rgep->genlock);
13736764Smx205022
13746764Smx205022 if (rgep->suspended) {
13756764Smx205022 mutex_exit(rgep->genlock);
13766764Smx205022 return (DDI_INTR_UNCLAIMED);
13776764Smx205022 }
13786764Smx205022
1379744Sgs150176 /*
1380744Sgs150176 * Was this interrupt caused by our device...
1381744Sgs150176 */
1382744Sgs150176 int_status = rge_reg_get16(rgep, INT_STATUS_REG);
1383744Sgs150176 if (!(int_status & rgep->int_mask)) {
1384744Sgs150176 mutex_exit(rgep->genlock);
1385744Sgs150176 return (DDI_INTR_UNCLAIMED);
1386744Sgs150176 /* indicate it wasn't our interrupt */
1387744Sgs150176 }
1388744Sgs150176 rgep->stats.intr++;
1389744Sgs150176
1390744Sgs150176 /*
1391744Sgs150176 * Clear interrupt
13922544Sgs150176 * For PCIE chipset, we need disable interrupt first.
1393744Sgs150176 */
139411365SZhen.W@Sun.COM if (rgep->chipid.is_pcie) {
13952544Sgs150176 rge_reg_put16(rgep, INT_MASK_REG, INT_MASK_NONE);
139611365SZhen.W@Sun.COM update_int_mask = B_TRUE;
139711365SZhen.W@Sun.COM }
1398744Sgs150176 rge_reg_put16(rgep, INT_STATUS_REG, int_status);
1399744Sgs150176
1400744Sgs150176 /*
140111365SZhen.W@Sun.COM * Calculate optimal polling interval
140211365SZhen.W@Sun.COM */
140311365SZhen.W@Sun.COM now = ddi_get_lbolt();
140411365SZhen.W@Sun.COM if (now - rgep->curr_tick >= rgep->tick_delta &&
140511365SZhen.W@Sun.COM (rgep->param_link_speed == RGE_SPEED_1000M ||
140611365SZhen.W@Sun.COM rgep->param_link_speed == RGE_SPEED_100M)) {
140711365SZhen.W@Sun.COM /* number of rx and tx packets in the last tick */
140811365SZhen.W@Sun.COM tx_pkts = rgep->stats.opackets - rgep->last_opackets;
140911365SZhen.W@Sun.COM rx_pkts = rgep->stats.rpackets - rgep->last_rpackets;
141011365SZhen.W@Sun.COM
141111365SZhen.W@Sun.COM rgep->last_opackets = rgep->stats.opackets;
141211365SZhen.W@Sun.COM rgep->last_rpackets = rgep->stats.rpackets;
141311365SZhen.W@Sun.COM
141411365SZhen.W@Sun.COM /* restore interrupt mask */
141511365SZhen.W@Sun.COM rgep->int_mask |= TX_OK_INT | RX_OK_INT;
141611365SZhen.W@Sun.COM if (rgep->chipid.is_pcie) {
141711365SZhen.W@Sun.COM rgep->int_mask |= NO_TXDESC_INT;
141811365SZhen.W@Sun.COM }
141911365SZhen.W@Sun.COM
142011365SZhen.W@Sun.COM /* optimal number of packets in a tick */
142111365SZhen.W@Sun.COM if (rgep->param_link_speed == RGE_SPEED_1000M) {
142211365SZhen.W@Sun.COM opt_pkts = (1000*1000*1000/8)/ETHERMTU/CLK_TICK;
142311365SZhen.W@Sun.COM } else {
142411365SZhen.W@Sun.COM opt_pkts = (100*1000*1000/8)/ETHERMTU/CLK_TICK;
142511365SZhen.W@Sun.COM }
142611365SZhen.W@Sun.COM
142711365SZhen.W@Sun.COM /*
142811365SZhen.W@Sun.COM * calculate polling interval based on rx and tx packets
142911365SZhen.W@Sun.COM * in the last tick
143011365SZhen.W@Sun.COM */
143111365SZhen.W@Sun.COM poll_rate = 0;
143211365SZhen.W@Sun.COM if (now - rgep->curr_tick < 2*rgep->tick_delta) {
143311365SZhen.W@Sun.COM opt_intrs = opt_pkts/TX_COALESC;
143411365SZhen.W@Sun.COM if (tx_pkts > opt_intrs) {
143511365SZhen.W@Sun.COM poll_rate = max(tx_pkts/TX_COALESC, opt_intrs);
143611365SZhen.W@Sun.COM rgep->int_mask &= ~(TX_OK_INT | NO_TXDESC_INT);
143711365SZhen.W@Sun.COM }
143811365SZhen.W@Sun.COM
143911365SZhen.W@Sun.COM opt_intrs = opt_pkts/RX_COALESC;
144011365SZhen.W@Sun.COM if (rx_pkts > opt_intrs) {
144111365SZhen.W@Sun.COM opt_intrs = max(rx_pkts/RX_COALESC, opt_intrs);
144211365SZhen.W@Sun.COM poll_rate = max(opt_intrs, poll_rate);
144311365SZhen.W@Sun.COM rgep->int_mask &= ~RX_OK_INT;
144411365SZhen.W@Sun.COM }
144511365SZhen.W@Sun.COM /* ensure poll_rate reasonable */
144611365SZhen.W@Sun.COM poll_rate = min(poll_rate, opt_pkts*4);
144711365SZhen.W@Sun.COM }
144811365SZhen.W@Sun.COM
144911365SZhen.W@Sun.COM if (poll_rate) {
145011365SZhen.W@Sun.COM /* move to polling mode */
145111365SZhen.W@Sun.COM if (rgep->chipid.is_pcie) {
145211365SZhen.W@Sun.COM itimer = (TIMER_CLK_PCIE/CLK_TICK)/poll_rate;
145311365SZhen.W@Sun.COM } else {
145411365SZhen.W@Sun.COM itimer = (TIMER_CLK_PCI/CLK_TICK)/poll_rate;
145511365SZhen.W@Sun.COM }
145611365SZhen.W@Sun.COM } else {
145711365SZhen.W@Sun.COM /* move to normal mode */
145811365SZhen.W@Sun.COM itimer = 0;
145911365SZhen.W@Sun.COM }
146011365SZhen.W@Sun.COM RGE_DEBUG(("%s: poll: itimer:%d int_mask:0x%x",
146111365SZhen.W@Sun.COM __func__, itimer, rgep->int_mask));
146211365SZhen.W@Sun.COM rge_reg_put32(rgep, TIMER_INT_REG, itimer);
146311365SZhen.W@Sun.COM
146411365SZhen.W@Sun.COM /* update timestamp for statistics */
146511365SZhen.W@Sun.COM rgep->curr_tick = now;
146611365SZhen.W@Sun.COM
146711365SZhen.W@Sun.COM /* reset timer */
146811365SZhen.W@Sun.COM int_status |= TIME_OUT_INT;
146911365SZhen.W@Sun.COM
147011365SZhen.W@Sun.COM update_int_mask = B_TRUE;
147111365SZhen.W@Sun.COM }
147211365SZhen.W@Sun.COM
147311365SZhen.W@Sun.COM if (int_status & TIME_OUT_INT) {
147411365SZhen.W@Sun.COM rge_reg_put32(rgep, TIMER_COUNT_REG, 0);
147511365SZhen.W@Sun.COM }
147611365SZhen.W@Sun.COM
147711365SZhen.W@Sun.COM /* flush post writes */
147811365SZhen.W@Sun.COM (void) rge_reg_get16(rgep, INT_STATUS_REG);
147911365SZhen.W@Sun.COM
148011365SZhen.W@Sun.COM /*
1481744Sgs150176 * Cable link change interrupt
1482744Sgs150176 */
1483744Sgs150176 if (int_status & LINK_CHANGE_INT) {
1484744Sgs150176 rge_chip_cyclic(rgep);
1485744Sgs150176 }
14862544Sgs150176
148711499SZhen.W@Sun.COM if (int_status & RX_FIFO_OVERFLOW_INT) {
148811499SZhen.W@Sun.COM /* start rx watchdog timeout detection */
148911499SZhen.W@Sun.COM rgep->rx_fifo_ovf = 1;
149011499SZhen.W@Sun.COM if (rgep->int_mask & RX_FIFO_OVERFLOW_INT) {
149111499SZhen.W@Sun.COM rgep->int_mask &= ~RX_FIFO_OVERFLOW_INT;
149211499SZhen.W@Sun.COM update_int_mask = B_TRUE;
149311499SZhen.W@Sun.COM }
149411499SZhen.W@Sun.COM } else if (int_status & RGE_RX_INT) {
149511499SZhen.W@Sun.COM /* stop rx watchdog timeout detection */
149611499SZhen.W@Sun.COM rgep->rx_fifo_ovf = 0;
149711499SZhen.W@Sun.COM if ((rgep->int_mask & RX_FIFO_OVERFLOW_INT) == 0) {
149811499SZhen.W@Sun.COM rgep->int_mask |= RX_FIFO_OVERFLOW_INT;
149911499SZhen.W@Sun.COM update_int_mask = B_TRUE;
150011499SZhen.W@Sun.COM }
150111499SZhen.W@Sun.COM }
150211499SZhen.W@Sun.COM
1503744Sgs150176 mutex_exit(rgep->genlock);
1504744Sgs150176
1505744Sgs150176 /*
1506744Sgs150176 * Receive interrupt
1507744Sgs150176 */
15082544Sgs150176 if (int_status & RGE_RX_INT)
1509744Sgs150176 rge_receive(rgep);
1510744Sgs150176
15112544Sgs150176 /*
151211365SZhen.W@Sun.COM * Transmit interrupt
15132544Sgs150176 */
151411365SZhen.W@Sun.COM if (int_status & TX_ERR_INT) {
151511365SZhen.W@Sun.COM RGE_REPORT((rgep, "tx error happened, resetting the chip "));
151611365SZhen.W@Sun.COM mutex_enter(rgep->genlock);
151711365SZhen.W@Sun.COM rgep->rge_chip_state = RGE_CHIP_ERROR;
151811365SZhen.W@Sun.COM mutex_exit(rgep->genlock);
151911365SZhen.W@Sun.COM } else if ((rgep->chipid.is_pcie && (int_status & NO_TXDESC_INT)) ||
152011365SZhen.W@Sun.COM ((int_status & TX_OK_INT) && rgep->tx_free < RGE_SEND_SLOTS/8)) {
152111365SZhen.W@Sun.COM (void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
152211365SZhen.W@Sun.COM }
152311365SZhen.W@Sun.COM
152411365SZhen.W@Sun.COM /*
152511410SLi-Zhen.You@Sun.COM * System error interrupt
152611410SLi-Zhen.You@Sun.COM */
152711410SLi-Zhen.You@Sun.COM if (int_status & SYS_ERR_INT) {
152811410SLi-Zhen.You@Sun.COM RGE_REPORT((rgep, "sys error happened, resetting the chip "));
152911410SLi-Zhen.You@Sun.COM mutex_enter(rgep->genlock);
153011410SLi-Zhen.You@Sun.COM rgep->rge_chip_state = RGE_CHIP_ERROR;
153111410SLi-Zhen.You@Sun.COM mutex_exit(rgep->genlock);
153211410SLi-Zhen.You@Sun.COM }
153311410SLi-Zhen.You@Sun.COM
153411410SLi-Zhen.You@Sun.COM /*
153511365SZhen.W@Sun.COM * Re-enable interrupt for PCIE chipset or install new int_mask
153611365SZhen.W@Sun.COM */
153711365SZhen.W@Sun.COM if (update_int_mask)
15382544Sgs150176 rge_reg_put16(rgep, INT_MASK_REG, rgep->int_mask);
15392544Sgs150176
1540744Sgs150176 return (DDI_INTR_CLAIMED); /* indicate it was our interrupt */
1541744Sgs150176 }
1542744Sgs150176
1543744Sgs150176 /*
1544744Sgs150176 * ========== Factotum, implemented as a softint handler ==========
1545744Sgs150176 */
1546744Sgs150176
1547744Sgs150176 #undef RGE_DBG
1548744Sgs150176 #define RGE_DBG RGE_DBG_FACT /* debug flag for this code */
1549744Sgs150176
1550744Sgs150176 static boolean_t rge_factotum_link_check(rge_t *rgep);
1551744Sgs150176 #pragma no_inline(rge_factotum_link_check)
1552744Sgs150176
1553744Sgs150176 static boolean_t
rge_factotum_link_check(rge_t * rgep)1554744Sgs150176 rge_factotum_link_check(rge_t *rgep)
1555744Sgs150176 {
1556744Sgs150176 uint8_t media_status;
1557744Sgs150176 int32_t link;
1558744Sgs150176
1559744Sgs150176 media_status = rge_reg_get8(rgep, PHY_STATUS_REG);
1560744Sgs150176 link = (media_status & PHY_STATUS_LINK_UP) ?
1561744Sgs150176 LINK_STATE_UP : LINK_STATE_DOWN;
1562744Sgs150176 if (rgep->param_link_up != link) {
1563744Sgs150176 /*
15644403Sgd78059 * Link change.
1565744Sgs150176 */
1566744Sgs150176 rgep->param_link_up = link;
1567744Sgs150176
1568744Sgs150176 if (link == LINK_STATE_UP) {
1569744Sgs150176 if (media_status & PHY_STATUS_1000MF) {
1570744Sgs150176 rgep->param_link_speed = RGE_SPEED_1000M;
1571744Sgs150176 rgep->param_link_duplex = LINK_DUPLEX_FULL;
1572744Sgs150176 } else {
1573744Sgs150176 rgep->param_link_speed =
1574744Sgs150176 (media_status & PHY_STATUS_100M) ?
1575744Sgs150176 RGE_SPEED_100M : RGE_SPEED_10M;
1576744Sgs150176 rgep->param_link_duplex =
1577744Sgs150176 (media_status & PHY_STATUS_DUPLEX_FULL) ?
1578744Sgs150176 LINK_DUPLEX_FULL : LINK_DUPLEX_HALF;
1579744Sgs150176 }
1580744Sgs150176 }
1581744Sgs150176 return (B_TRUE);
1582744Sgs150176 }
1583744Sgs150176 return (B_FALSE);
1584744Sgs150176 }
1585744Sgs150176
1586744Sgs150176 /*
1587744Sgs150176 * Factotum routine to check for Tx stall, using the 'watchdog' counter
1588744Sgs150176 */
1589744Sgs150176 static boolean_t rge_factotum_stall_check(rge_t *rgep);
1590744Sgs150176 #pragma no_inline(rge_factotum_stall_check)
1591744Sgs150176
1592744Sgs150176 static boolean_t
rge_factotum_stall_check(rge_t * rgep)1593744Sgs150176 rge_factotum_stall_check(rge_t *rgep)
1594744Sgs150176 {
1595744Sgs150176 uint32_t dogval;
1596744Sgs150176
1597744Sgs150176 ASSERT(mutex_owned(rgep->genlock));
1598744Sgs150176
1599744Sgs150176 /*
160011499SZhen.W@Sun.COM * Specific check for RX stall ...
160111499SZhen.W@Sun.COM */
160211499SZhen.W@Sun.COM rgep->rx_fifo_ovf <<= 1;
160311499SZhen.W@Sun.COM if (rgep->rx_fifo_ovf > rge_rx_watchdog_count) {
160411499SZhen.W@Sun.COM RGE_REPORT((rgep, "rx_hang detected"));
160511499SZhen.W@Sun.COM return (B_TRUE);
160611499SZhen.W@Sun.COM }
160711499SZhen.W@Sun.COM
160811499SZhen.W@Sun.COM /*
1609744Sgs150176 * Specific check for Tx stall ...
1610744Sgs150176 *
1611744Sgs150176 * The 'watchdog' counter is incremented whenever a packet
1612744Sgs150176 * is queued, reset to 1 when some (but not all) buffers
1613744Sgs150176 * are reclaimed, reset to 0 (disabled) when all buffers
1614744Sgs150176 * are reclaimed, and shifted left here. If it exceeds the
1615744Sgs150176 * threshold value, the chip is assumed to have stalled and
1616744Sgs150176 * is put into the ERROR state. The factotum will then reset
1617744Sgs150176 * it on the next pass.
1618744Sgs150176 *
1619744Sgs150176 * All of which should ensure that we don't get into a state
1620744Sgs150176 * where packets are left pending indefinitely!
1621744Sgs150176 */
16222544Sgs150176 if (rgep->resched_needed)
16232544Sgs150176 (void) ddi_intr_trigger_softint(rgep->resched_hdl, NULL);
1624744Sgs150176 dogval = rge_atomic_shl32(&rgep->watchdog, 1);
1625744Sgs150176 if (dogval < rge_watchdog_count)
1626744Sgs150176 return (B_FALSE);
1627744Sgs150176
1628744Sgs150176 RGE_REPORT((rgep, "Tx stall detected, watchdog code 0x%x", dogval));
1629744Sgs150176 return (B_TRUE);
1630744Sgs150176
1631744Sgs150176 }
1632744Sgs150176
1633744Sgs150176 /*
1634744Sgs150176 * The factotum is woken up when there's something to do that we'd rather
1635744Sgs150176 * not do from inside a hardware interrupt handler or high-level cyclic.
1636744Sgs150176 * Its two main tasks are:
1637744Sgs150176 * reset & restart the chip after an error
1638744Sgs150176 * check the link status whenever necessary
1639744Sgs150176 */
16402544Sgs150176 uint_t rge_chip_factotum(caddr_t arg1, caddr_t arg2);
1641744Sgs150176 #pragma no_inline(rge_chip_factotum)
1642744Sgs150176
1643744Sgs150176 uint_t
rge_chip_factotum(caddr_t arg1,caddr_t arg2)16442544Sgs150176 rge_chip_factotum(caddr_t arg1, caddr_t arg2)
1645744Sgs150176 {
1646744Sgs150176 rge_t *rgep;
1647744Sgs150176 uint_t result;
1648744Sgs150176 boolean_t error;
1649744Sgs150176 boolean_t linkchg;
1650744Sgs150176
16512544Sgs150176 rgep = (rge_t *)arg1;
16522544Sgs150176 _NOTE(ARGUNUSED(arg2))
1653744Sgs150176
1654744Sgs150176 if (rgep->factotum_flag == 0)
1655744Sgs150176 return (DDI_INTR_UNCLAIMED);
1656744Sgs150176
1657744Sgs150176 rgep->factotum_flag = 0;
1658744Sgs150176 result = DDI_INTR_CLAIMED;
1659744Sgs150176 error = B_FALSE;
1660744Sgs150176 linkchg = B_FALSE;
1661744Sgs150176
1662744Sgs150176 mutex_enter(rgep->genlock);
1663744Sgs150176 switch (rgep->rge_chip_state) {
1664744Sgs150176 default:
1665744Sgs150176 break;
1666744Sgs150176
1667744Sgs150176 case RGE_CHIP_RUNNING:
1668744Sgs150176 linkchg = rge_factotum_link_check(rgep);
1669744Sgs150176 error = rge_factotum_stall_check(rgep);
1670744Sgs150176 break;
1671744Sgs150176
1672744Sgs150176 case RGE_CHIP_ERROR:
1673744Sgs150176 error = B_TRUE;
1674744Sgs150176 break;
1675744Sgs150176
1676744Sgs150176 case RGE_CHIP_FAULT:
1677744Sgs150176 /*
1678744Sgs150176 * Fault detected, time to reset ...
1679744Sgs150176 */
1680744Sgs150176 if (rge_autorecover) {
1681744Sgs150176 RGE_REPORT((rgep, "automatic recovery activated"));
1682744Sgs150176 rge_restart(rgep);
1683744Sgs150176 }
1684744Sgs150176 break;
1685744Sgs150176 }
1686744Sgs150176
1687744Sgs150176 /*
1688744Sgs150176 * If an error is detected, stop the chip now, marking it as
1689744Sgs150176 * faulty, so that it will be reset next time through ...
1690744Sgs150176 */
1691744Sgs150176 if (error)
1692744Sgs150176 rge_chip_stop(rgep, B_TRUE);
1693744Sgs150176 mutex_exit(rgep->genlock);
1694744Sgs150176
1695744Sgs150176 /*
1696744Sgs150176 * If the link state changed, tell the world about it.
1697744Sgs150176 * Note: can't do this while still holding the mutex.
1698744Sgs150176 */
1699744Sgs150176 if (linkchg)
17002311Sseb mac_link_update(rgep->mh, rgep->param_link_up);
1701744Sgs150176
1702744Sgs150176 return (result);
1703744Sgs150176 }
1704744Sgs150176
1705744Sgs150176 /*
1706744Sgs150176 * High-level cyclic handler
1707744Sgs150176 *
1708744Sgs150176 * This routine schedules a (low-level) softint callback to the
1709744Sgs150176 * factotum, and prods the chip to update the status block (which
1710744Sgs150176 * will cause a hardware interrupt when complete).
1711744Sgs150176 */
1712744Sgs150176 void rge_chip_cyclic(void *arg);
1713744Sgs150176 #pragma no_inline(rge_chip_cyclic)
1714744Sgs150176
1715744Sgs150176 void
rge_chip_cyclic(void * arg)1716744Sgs150176 rge_chip_cyclic(void *arg)
1717744Sgs150176 {
1718744Sgs150176 rge_t *rgep;
1719744Sgs150176
1720744Sgs150176 rgep = arg;
1721744Sgs150176
1722744Sgs150176 switch (rgep->rge_chip_state) {
1723744Sgs150176 default:
1724744Sgs150176 return;
1725744Sgs150176
1726744Sgs150176 case RGE_CHIP_RUNNING:
1727744Sgs150176 rge_phy_check(rgep);
172811410SLi-Zhen.You@Sun.COM if (rgep->tx_free < RGE_SEND_SLOTS)
172911410SLi-Zhen.You@Sun.COM rge_send_recycle(rgep);
1730744Sgs150176 break;
1731744Sgs150176
1732744Sgs150176 case RGE_CHIP_FAULT:
1733744Sgs150176 case RGE_CHIP_ERROR:
1734744Sgs150176 break;
1735744Sgs150176 }
1736744Sgs150176
1737744Sgs150176 rge_wake_factotum(rgep);
1738744Sgs150176 }
1739744Sgs150176
1740744Sgs150176
1741744Sgs150176 /*
1742744Sgs150176 * ========== Ioctl subfunctions ==========
1743744Sgs150176 */
1744744Sgs150176
1745744Sgs150176 #undef RGE_DBG
1746744Sgs150176 #define RGE_DBG RGE_DBG_PPIO /* debug flag for this code */
1747744Sgs150176
1748744Sgs150176 #if RGE_DEBUGGING || RGE_DO_PPIO
1749744Sgs150176
1750744Sgs150176 static void rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1751744Sgs150176 #pragma no_inline(rge_chip_peek_cfg)
1752744Sgs150176
1753744Sgs150176 static void
rge_chip_peek_cfg(rge_t * rgep,rge_peekpoke_t * ppd)1754744Sgs150176 rge_chip_peek_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1755744Sgs150176 {
1756744Sgs150176 uint64_t regval;
1757744Sgs150176 uint64_t regno;
1758744Sgs150176
1759744Sgs150176 RGE_TRACE(("rge_chip_peek_cfg($%p, $%p)",
17605735Smx205022 (void *)rgep, (void *)ppd));
1761744Sgs150176
1762744Sgs150176 regno = ppd->pp_acc_offset;
1763744Sgs150176
1764744Sgs150176 switch (ppd->pp_acc_size) {
1765744Sgs150176 case 1:
1766744Sgs150176 regval = pci_config_get8(rgep->cfg_handle, regno);
1767744Sgs150176 break;
1768744Sgs150176
1769744Sgs150176 case 2:
1770744Sgs150176 regval = pci_config_get16(rgep->cfg_handle, regno);
1771744Sgs150176 break;
1772744Sgs150176
1773744Sgs150176 case 4:
1774744Sgs150176 regval = pci_config_get32(rgep->cfg_handle, regno);
1775744Sgs150176 break;
1776744Sgs150176
1777744Sgs150176 case 8:
1778744Sgs150176 regval = pci_config_get64(rgep->cfg_handle, regno);
1779744Sgs150176 break;
1780744Sgs150176 }
1781744Sgs150176
1782744Sgs150176 ppd->pp_acc_data = regval;
1783744Sgs150176 }
1784744Sgs150176
1785744Sgs150176 static void rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd);
1786744Sgs150176 #pragma no_inline(rge_chip_poke_cfg)
1787744Sgs150176
1788744Sgs150176 static void
rge_chip_poke_cfg(rge_t * rgep,rge_peekpoke_t * ppd)1789744Sgs150176 rge_chip_poke_cfg(rge_t *rgep, rge_peekpoke_t *ppd)
1790744Sgs150176 {
1791744Sgs150176 uint64_t regval;
1792744Sgs150176 uint64_t regno;
1793744Sgs150176
1794744Sgs150176 RGE_TRACE(("rge_chip_poke_cfg($%p, $%p)",
17955735Smx205022 (void *)rgep, (void *)ppd));
1796744Sgs150176
1797744Sgs150176 regno = ppd->pp_acc_offset;
1798744Sgs150176 regval = ppd->pp_acc_data;
1799744Sgs150176
1800744Sgs150176 switch (ppd->pp_acc_size) {
1801744Sgs150176 case 1:
1802744Sgs150176 pci_config_put8(rgep->cfg_handle, regno, regval);
1803744Sgs150176 break;
1804744Sgs150176
1805744Sgs150176 case 2:
1806744Sgs150176 pci_config_put16(rgep->cfg_handle, regno, regval);
1807744Sgs150176 break;
1808744Sgs150176
1809744Sgs150176 case 4:
1810744Sgs150176 pci_config_put32(rgep->cfg_handle, regno, regval);
1811744Sgs150176 break;
1812744Sgs150176
1813744Sgs150176 case 8:
1814744Sgs150176 pci_config_put64(rgep->cfg_handle, regno, regval);
1815744Sgs150176 break;
1816744Sgs150176 }
1817744Sgs150176 }
1818744Sgs150176
1819744Sgs150176 static void rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1820744Sgs150176 #pragma no_inline(rge_chip_peek_reg)
1821744Sgs150176
1822744Sgs150176 static void
rge_chip_peek_reg(rge_t * rgep,rge_peekpoke_t * ppd)1823744Sgs150176 rge_chip_peek_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1824744Sgs150176 {
1825744Sgs150176 uint64_t regval;
1826744Sgs150176 void *regaddr;
1827744Sgs150176
1828744Sgs150176 RGE_TRACE(("rge_chip_peek_reg($%p, $%p)",
18295735Smx205022 (void *)rgep, (void *)ppd));
1830744Sgs150176
1831744Sgs150176 regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1832744Sgs150176
1833744Sgs150176 switch (ppd->pp_acc_size) {
1834744Sgs150176 case 1:
1835744Sgs150176 regval = ddi_get8(rgep->io_handle, regaddr);
1836744Sgs150176 break;
1837744Sgs150176
1838744Sgs150176 case 2:
1839744Sgs150176 regval = ddi_get16(rgep->io_handle, regaddr);
1840744Sgs150176 break;
1841744Sgs150176
1842744Sgs150176 case 4:
1843744Sgs150176 regval = ddi_get32(rgep->io_handle, regaddr);
1844744Sgs150176 break;
1845744Sgs150176
1846744Sgs150176 case 8:
1847744Sgs150176 regval = ddi_get64(rgep->io_handle, regaddr);
1848744Sgs150176 break;
1849744Sgs150176 }
1850744Sgs150176
1851744Sgs150176 ppd->pp_acc_data = regval;
1852744Sgs150176 }
1853744Sgs150176
1854744Sgs150176 static void rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd);
1855744Sgs150176 #pragma no_inline(rge_chip_peek_reg)
1856744Sgs150176
1857744Sgs150176 static void
rge_chip_poke_reg(rge_t * rgep,rge_peekpoke_t * ppd)1858744Sgs150176 rge_chip_poke_reg(rge_t *rgep, rge_peekpoke_t *ppd)
1859744Sgs150176 {
1860744Sgs150176 uint64_t regval;
1861744Sgs150176 void *regaddr;
1862744Sgs150176
1863744Sgs150176 RGE_TRACE(("rge_chip_poke_reg($%p, $%p)",
18645735Smx205022 (void *)rgep, (void *)ppd));
1865744Sgs150176
1866744Sgs150176 regaddr = PIO_ADDR(rgep, ppd->pp_acc_offset);
1867744Sgs150176 regval = ppd->pp_acc_data;
1868744Sgs150176
1869744Sgs150176 switch (ppd->pp_acc_size) {
1870744Sgs150176 case 1:
1871744Sgs150176 ddi_put8(rgep->io_handle, regaddr, regval);
1872744Sgs150176 break;
1873744Sgs150176
1874744Sgs150176 case 2:
1875744Sgs150176 ddi_put16(rgep->io_handle, regaddr, regval);
1876744Sgs150176 break;
1877744Sgs150176
1878744Sgs150176 case 4:
1879744Sgs150176 ddi_put32(rgep->io_handle, regaddr, regval);
1880744Sgs150176 break;
1881744Sgs150176
1882744Sgs150176 case 8:
1883744Sgs150176 ddi_put64(rgep->io_handle, regaddr, regval);
1884744Sgs150176 break;
1885744Sgs150176 }
1886744Sgs150176 }
1887744Sgs150176
1888744Sgs150176 static void rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1889744Sgs150176 #pragma no_inline(rge_chip_peek_mii)
1890744Sgs150176
1891744Sgs150176 static void
rge_chip_peek_mii(rge_t * rgep,rge_peekpoke_t * ppd)1892744Sgs150176 rge_chip_peek_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1893744Sgs150176 {
1894744Sgs150176 RGE_TRACE(("rge_chip_peek_mii($%p, $%p)",
18955735Smx205022 (void *)rgep, (void *)ppd));
1896744Sgs150176
1897744Sgs150176 ppd->pp_acc_data = rge_mii_get16(rgep, ppd->pp_acc_offset/2);
1898744Sgs150176 }
1899744Sgs150176
1900744Sgs150176 static void rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd);
1901744Sgs150176 #pragma no_inline(rge_chip_poke_mii)
1902744Sgs150176
1903744Sgs150176 static void
rge_chip_poke_mii(rge_t * rgep,rge_peekpoke_t * ppd)1904744Sgs150176 rge_chip_poke_mii(rge_t *rgep, rge_peekpoke_t *ppd)
1905744Sgs150176 {
1906744Sgs150176 RGE_TRACE(("rge_chip_poke_mii($%p, $%p)",
19075735Smx205022 (void *)rgep, (void *)ppd));
1908744Sgs150176
1909744Sgs150176 rge_mii_put16(rgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
1910744Sgs150176 }
1911744Sgs150176
1912744Sgs150176 static void rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1913744Sgs150176 #pragma no_inline(rge_chip_peek_mem)
1914744Sgs150176
1915744Sgs150176 static void
rge_chip_peek_mem(rge_t * rgep,rge_peekpoke_t * ppd)1916744Sgs150176 rge_chip_peek_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1917744Sgs150176 {
1918744Sgs150176 uint64_t regval;
1919744Sgs150176 void *vaddr;
1920744Sgs150176
1921744Sgs150176 RGE_TRACE(("rge_chip_peek_rge($%p, $%p)",
19225735Smx205022 (void *)rgep, (void *)ppd));
1923744Sgs150176
1924744Sgs150176 vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1925744Sgs150176
1926744Sgs150176 switch (ppd->pp_acc_size) {
1927744Sgs150176 case 1:
1928744Sgs150176 regval = *(uint8_t *)vaddr;
1929744Sgs150176 break;
1930744Sgs150176
1931744Sgs150176 case 2:
1932744Sgs150176 regval = *(uint16_t *)vaddr;
1933744Sgs150176 break;
1934744Sgs150176
1935744Sgs150176 case 4:
1936744Sgs150176 regval = *(uint32_t *)vaddr;
1937744Sgs150176 break;
1938744Sgs150176
1939744Sgs150176 case 8:
1940744Sgs150176 regval = *(uint64_t *)vaddr;
1941744Sgs150176 break;
1942744Sgs150176 }
1943744Sgs150176
1944744Sgs150176 RGE_DEBUG(("rge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
19455735Smx205022 (void *)rgep, (void *)ppd, regval, vaddr));
1946744Sgs150176
1947744Sgs150176 ppd->pp_acc_data = regval;
1948744Sgs150176 }
1949744Sgs150176
1950744Sgs150176 static void rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd);
1951744Sgs150176 #pragma no_inline(rge_chip_poke_mem)
1952744Sgs150176
1953744Sgs150176 static void
rge_chip_poke_mem(rge_t * rgep,rge_peekpoke_t * ppd)1954744Sgs150176 rge_chip_poke_mem(rge_t *rgep, rge_peekpoke_t *ppd)
1955744Sgs150176 {
1956744Sgs150176 uint64_t regval;
1957744Sgs150176 void *vaddr;
1958744Sgs150176
1959744Sgs150176 RGE_TRACE(("rge_chip_poke_mem($%p, $%p)",
19605735Smx205022 (void *)rgep, (void *)ppd));
1961744Sgs150176
1962744Sgs150176 vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
1963744Sgs150176 regval = ppd->pp_acc_data;
1964744Sgs150176
1965744Sgs150176 RGE_DEBUG(("rge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
19665735Smx205022 (void *)rgep, (void *)ppd, regval, vaddr));
1967744Sgs150176
1968744Sgs150176 switch (ppd->pp_acc_size) {
1969744Sgs150176 case 1:
1970744Sgs150176 *(uint8_t *)vaddr = (uint8_t)regval;
1971744Sgs150176 break;
1972744Sgs150176
1973744Sgs150176 case 2:
1974744Sgs150176 *(uint16_t *)vaddr = (uint16_t)regval;
1975744Sgs150176 break;
1976744Sgs150176
1977744Sgs150176 case 4:
1978744Sgs150176 *(uint32_t *)vaddr = (uint32_t)regval;
1979744Sgs150176 break;
1980744Sgs150176
1981744Sgs150176 case 8:
1982744Sgs150176 *(uint64_t *)vaddr = (uint64_t)regval;
1983744Sgs150176 break;
1984744Sgs150176 }
1985744Sgs150176 }
1986744Sgs150176
1987744Sgs150176 static enum ioc_reply rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
1988744Sgs150176 struct iocblk *iocp);
1989744Sgs150176 #pragma no_inline(rge_pp_ioctl)
1990744Sgs150176
1991744Sgs150176 static enum ioc_reply
rge_pp_ioctl(rge_t * rgep,int cmd,mblk_t * mp,struct iocblk * iocp)1992744Sgs150176 rge_pp_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
1993744Sgs150176 {
1994744Sgs150176 void (*ppfn)(rge_t *rgep, rge_peekpoke_t *ppd);
1995744Sgs150176 rge_peekpoke_t *ppd;
1996744Sgs150176 dma_area_t *areap;
1997744Sgs150176 uint64_t sizemask;
1998744Sgs150176 uint64_t mem_va;
1999744Sgs150176 uint64_t maxoff;
2000744Sgs150176 boolean_t peek;
2001744Sgs150176
2002744Sgs150176 switch (cmd) {
2003744Sgs150176 default:
2004744Sgs150176 /* NOTREACHED */
2005744Sgs150176 rge_error(rgep, "rge_pp_ioctl: invalid cmd 0x%x", cmd);
2006744Sgs150176 return (IOC_INVAL);
2007744Sgs150176
2008744Sgs150176 case RGE_PEEK:
2009744Sgs150176 peek = B_TRUE;
2010744Sgs150176 break;
2011744Sgs150176
2012744Sgs150176 case RGE_POKE:
2013744Sgs150176 peek = B_FALSE;
2014744Sgs150176 break;
2015744Sgs150176 }
2016744Sgs150176
2017744Sgs150176 /*
2018744Sgs150176 * Validate format of ioctl
2019744Sgs150176 */
2020744Sgs150176 if (iocp->ioc_count != sizeof (rge_peekpoke_t))
2021744Sgs150176 return (IOC_INVAL);
2022744Sgs150176 if (mp->b_cont == NULL)
2023744Sgs150176 return (IOC_INVAL);
2024744Sgs150176 ppd = (rge_peekpoke_t *)mp->b_cont->b_rptr;
2025744Sgs150176
2026744Sgs150176 /*
2027744Sgs150176 * Validate request parameters
2028744Sgs150176 */
2029744Sgs150176 switch (ppd->pp_acc_space) {
2030744Sgs150176 default:
2031744Sgs150176 return (IOC_INVAL);
2032744Sgs150176
2033744Sgs150176 case RGE_PP_SPACE_CFG:
2034744Sgs150176 /*
2035744Sgs150176 * Config space
2036744Sgs150176 */
2037744Sgs150176 sizemask = 8|4|2|1;
2038744Sgs150176 mem_va = 0;
2039744Sgs150176 maxoff = PCI_CONF_HDR_SIZE;
2040744Sgs150176 ppfn = peek ? rge_chip_peek_cfg : rge_chip_poke_cfg;
2041744Sgs150176 break;
2042744Sgs150176
2043744Sgs150176 case RGE_PP_SPACE_REG:
2044744Sgs150176 /*
2045744Sgs150176 * Memory-mapped I/O space
2046744Sgs150176 */
2047744Sgs150176 sizemask = 8|4|2|1;
2048744Sgs150176 mem_va = 0;
2049744Sgs150176 maxoff = RGE_REGISTER_MAX;
2050744Sgs150176 ppfn = peek ? rge_chip_peek_reg : rge_chip_poke_reg;
2051744Sgs150176 break;
2052744Sgs150176
2053744Sgs150176 case RGE_PP_SPACE_MII:
2054744Sgs150176 /*
2055744Sgs150176 * PHY's MII registers
2056744Sgs150176 * NB: all PHY registers are two bytes, but the
2057744Sgs150176 * addresses increment in ones (word addressing).
2058744Sgs150176 * So we scale the address here, then undo the
2059744Sgs150176 * transformation inside the peek/poke functions.
2060744Sgs150176 */
2061744Sgs150176 ppd->pp_acc_offset *= 2;
2062744Sgs150176 sizemask = 2;
2063744Sgs150176 mem_va = 0;
2064744Sgs150176 maxoff = (MII_MAXREG+1)*2;
2065744Sgs150176 ppfn = peek ? rge_chip_peek_mii : rge_chip_poke_mii;
2066744Sgs150176 break;
2067744Sgs150176
2068744Sgs150176 case RGE_PP_SPACE_RGE:
2069744Sgs150176 /*
2070744Sgs150176 * RGE data structure!
2071744Sgs150176 */
2072744Sgs150176 sizemask = 8|4|2|1;
2073744Sgs150176 mem_va = (uintptr_t)rgep;
2074744Sgs150176 maxoff = sizeof (*rgep);
2075744Sgs150176 ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2076744Sgs150176 break;
2077744Sgs150176
2078744Sgs150176 case RGE_PP_SPACE_STATISTICS:
2079744Sgs150176 case RGE_PP_SPACE_TXDESC:
2080744Sgs150176 case RGE_PP_SPACE_TXBUFF:
2081744Sgs150176 case RGE_PP_SPACE_RXDESC:
2082744Sgs150176 case RGE_PP_SPACE_RXBUFF:
2083744Sgs150176 /*
2084744Sgs150176 * Various DMA_AREAs
2085744Sgs150176 */
2086744Sgs150176 switch (ppd->pp_acc_space) {
2087744Sgs150176 case RGE_PP_SPACE_TXDESC:
2088744Sgs150176 areap = &rgep->dma_area_txdesc;
2089744Sgs150176 break;
2090744Sgs150176 case RGE_PP_SPACE_RXDESC:
2091744Sgs150176 areap = &rgep->dma_area_rxdesc;
2092744Sgs150176 break;
2093744Sgs150176 case RGE_PP_SPACE_STATISTICS:
2094744Sgs150176 areap = &rgep->dma_area_stats;
2095744Sgs150176 break;
2096744Sgs150176 }
2097744Sgs150176
2098744Sgs150176 sizemask = 8|4|2|1;
2099744Sgs150176 mem_va = (uintptr_t)areap->mem_va;
2100744Sgs150176 maxoff = areap->alength;
2101744Sgs150176 ppfn = peek ? rge_chip_peek_mem : rge_chip_poke_mem;
2102744Sgs150176 break;
2103744Sgs150176 }
2104744Sgs150176
2105744Sgs150176 switch (ppd->pp_acc_size) {
2106744Sgs150176 default:
2107744Sgs150176 return (IOC_INVAL);
2108744Sgs150176
2109744Sgs150176 case 8:
2110744Sgs150176 case 4:
2111744Sgs150176 case 2:
2112744Sgs150176 case 1:
2113744Sgs150176 if ((ppd->pp_acc_size & sizemask) == 0)
2114744Sgs150176 return (IOC_INVAL);
2115744Sgs150176 break;
2116744Sgs150176 }
2117744Sgs150176
2118744Sgs150176 if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
2119744Sgs150176 return (IOC_INVAL);
2120744Sgs150176
2121744Sgs150176 if (ppd->pp_acc_offset >= maxoff)
2122744Sgs150176 return (IOC_INVAL);
2123744Sgs150176
2124744Sgs150176 if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
2125744Sgs150176 return (IOC_INVAL);
2126744Sgs150176
2127744Sgs150176 /*
2128744Sgs150176 * All OK - go do it!
2129744Sgs150176 */
2130744Sgs150176 ppd->pp_acc_offset += mem_va;
2131744Sgs150176 (*ppfn)(rgep, ppd);
2132744Sgs150176 return (peek ? IOC_REPLY : IOC_ACK);
2133744Sgs150176 }
2134744Sgs150176
2135744Sgs150176 static enum ioc_reply rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2136744Sgs150176 struct iocblk *iocp);
2137744Sgs150176 #pragma no_inline(rge_diag_ioctl)
2138744Sgs150176
2139744Sgs150176 static enum ioc_reply
rge_diag_ioctl(rge_t * rgep,int cmd,mblk_t * mp,struct iocblk * iocp)2140744Sgs150176 rge_diag_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2141744Sgs150176 {
2142744Sgs150176 ASSERT(mutex_owned(rgep->genlock));
2143744Sgs150176
2144744Sgs150176 switch (cmd) {
2145744Sgs150176 default:
2146744Sgs150176 /* NOTREACHED */
2147744Sgs150176 rge_error(rgep, "rge_diag_ioctl: invalid cmd 0x%x", cmd);
2148744Sgs150176 return (IOC_INVAL);
2149744Sgs150176
2150744Sgs150176 case RGE_DIAG:
2151744Sgs150176 /*
2152744Sgs150176 * Currently a no-op
2153744Sgs150176 */
2154744Sgs150176 return (IOC_ACK);
2155744Sgs150176
2156744Sgs150176 case RGE_PEEK:
2157744Sgs150176 case RGE_POKE:
2158744Sgs150176 return (rge_pp_ioctl(rgep, cmd, mp, iocp));
2159744Sgs150176
2160744Sgs150176 case RGE_PHY_RESET:
2161744Sgs150176 return (IOC_RESTART_ACK);
2162744Sgs150176
2163744Sgs150176 case RGE_SOFT_RESET:
2164744Sgs150176 case RGE_HARD_RESET:
2165744Sgs150176 /*
2166744Sgs150176 * Reset and reinitialise the 570x hardware
2167744Sgs150176 */
2168744Sgs150176 rge_restart(rgep);
2169744Sgs150176 return (IOC_ACK);
2170744Sgs150176 }
2171744Sgs150176
2172744Sgs150176 /* NOTREACHED */
2173744Sgs150176 }
2174744Sgs150176
2175744Sgs150176 #endif /* RGE_DEBUGGING || RGE_DO_PPIO */
2176744Sgs150176
2177744Sgs150176 static enum ioc_reply rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp,
2178744Sgs150176 struct iocblk *iocp);
2179744Sgs150176 #pragma no_inline(rge_mii_ioctl)
2180744Sgs150176
2181744Sgs150176 static enum ioc_reply
rge_mii_ioctl(rge_t * rgep,int cmd,mblk_t * mp,struct iocblk * iocp)2182744Sgs150176 rge_mii_ioctl(rge_t *rgep, int cmd, mblk_t *mp, struct iocblk *iocp)
2183744Sgs150176 {
2184744Sgs150176 struct rge_mii_rw *miirwp;
2185744Sgs150176
2186744Sgs150176 /*
2187744Sgs150176 * Validate format of ioctl
2188744Sgs150176 */
2189744Sgs150176 if (iocp->ioc_count != sizeof (struct rge_mii_rw))
2190744Sgs150176 return (IOC_INVAL);
2191744Sgs150176 if (mp->b_cont == NULL)
2192744Sgs150176 return (IOC_INVAL);
2193744Sgs150176 miirwp = (struct rge_mii_rw *)mp->b_cont->b_rptr;
2194744Sgs150176
2195744Sgs150176 /*
2196744Sgs150176 * Validate request parameters ...
2197744Sgs150176 */
2198744Sgs150176 if (miirwp->mii_reg > MII_MAXREG)
2199744Sgs150176 return (IOC_INVAL);
2200744Sgs150176
2201744Sgs150176 switch (cmd) {
2202744Sgs150176 default:
2203744Sgs150176 /* NOTREACHED */
2204744Sgs150176 rge_error(rgep, "rge_mii_ioctl: invalid cmd 0x%x", cmd);
2205744Sgs150176 return (IOC_INVAL);
2206744Sgs150176
2207744Sgs150176 case RGE_MII_READ:
2208744Sgs150176 miirwp->mii_data = rge_mii_get16(rgep, miirwp->mii_reg);
2209744Sgs150176 return (IOC_REPLY);
2210744Sgs150176
2211744Sgs150176 case RGE_MII_WRITE:
2212744Sgs150176 rge_mii_put16(rgep, miirwp->mii_reg, miirwp->mii_data);
2213744Sgs150176 return (IOC_ACK);
2214744Sgs150176 }
2215744Sgs150176
2216744Sgs150176 /* NOTREACHED */
2217744Sgs150176 }
2218744Sgs150176
2219744Sgs150176 enum ioc_reply rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp,
2220744Sgs150176 struct iocblk *iocp);
2221744Sgs150176 #pragma no_inline(rge_chip_ioctl)
2222744Sgs150176
2223744Sgs150176 enum ioc_reply
rge_chip_ioctl(rge_t * rgep,queue_t * wq,mblk_t * mp,struct iocblk * iocp)2224744Sgs150176 rge_chip_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
2225744Sgs150176 {
2226744Sgs150176 int cmd;
2227744Sgs150176
2228744Sgs150176 RGE_TRACE(("rge_chip_ioctl($%p, $%p, $%p, $%p)",
22295735Smx205022 (void *)rgep, (void *)wq, (void *)mp, (void *)iocp));
2230744Sgs150176
2231744Sgs150176 ASSERT(mutex_owned(rgep->genlock));
2232744Sgs150176
2233744Sgs150176 cmd = iocp->ioc_cmd;
2234744Sgs150176 switch (cmd) {
2235744Sgs150176 default:
2236744Sgs150176 /* NOTREACHED */
2237744Sgs150176 rge_error(rgep, "rge_chip_ioctl: invalid cmd 0x%x", cmd);
2238744Sgs150176 return (IOC_INVAL);
2239744Sgs150176
2240744Sgs150176 case RGE_DIAG:
2241744Sgs150176 case RGE_PEEK:
2242744Sgs150176 case RGE_POKE:
2243744Sgs150176 case RGE_PHY_RESET:
2244744Sgs150176 case RGE_SOFT_RESET:
2245744Sgs150176 case RGE_HARD_RESET:
2246744Sgs150176 #if RGE_DEBUGGING || RGE_DO_PPIO
2247744Sgs150176 return (rge_diag_ioctl(rgep, cmd, mp, iocp));
2248744Sgs150176 #else
2249744Sgs150176 return (IOC_INVAL);
2250744Sgs150176 #endif /* RGE_DEBUGGING || RGE_DO_PPIO */
2251744Sgs150176
2252744Sgs150176 case RGE_MII_READ:
2253744Sgs150176 case RGE_MII_WRITE:
2254744Sgs150176 return (rge_mii_ioctl(rgep, cmd, mp, iocp));
2255744Sgs150176
2256744Sgs150176 }
2257744Sgs150176
2258744Sgs150176 /* NOTREACHED */
2259744Sgs150176 }
2260