xref: /freebsd/sys/dev/rl/if_rl.c (revision b2d3d26fa011fbb70bed9caedf4bcfd8889e6ec6)
1*b2d3d26fSGleb Smirnoff /*-
2*b2d3d26fSGleb Smirnoff  * Copyright (c) 1997, 1998
3*b2d3d26fSGleb Smirnoff  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4*b2d3d26fSGleb Smirnoff  *
5*b2d3d26fSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6*b2d3d26fSGleb Smirnoff  * modification, are permitted provided that the following conditions
7*b2d3d26fSGleb Smirnoff  * are met:
8*b2d3d26fSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9*b2d3d26fSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10*b2d3d26fSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11*b2d3d26fSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12*b2d3d26fSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13*b2d3d26fSGleb Smirnoff  * 3. All advertising materials mentioning features or use of this software
14*b2d3d26fSGleb Smirnoff  *    must display the following acknowledgement:
15*b2d3d26fSGleb Smirnoff  *	This product includes software developed by Bill Paul.
16*b2d3d26fSGleb Smirnoff  * 4. Neither the name of the author nor the names of any co-contributors
17*b2d3d26fSGleb Smirnoff  *    may be used to endorse or promote products derived from this software
18*b2d3d26fSGleb Smirnoff  *    without specific prior written permission.
19*b2d3d26fSGleb Smirnoff  *
20*b2d3d26fSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21*b2d3d26fSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*b2d3d26fSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*b2d3d26fSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24*b2d3d26fSGleb Smirnoff  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*b2d3d26fSGleb Smirnoff  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*b2d3d26fSGleb Smirnoff  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*b2d3d26fSGleb Smirnoff  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*b2d3d26fSGleb Smirnoff  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*b2d3d26fSGleb Smirnoff  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30*b2d3d26fSGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
31*b2d3d26fSGleb Smirnoff  */
32*b2d3d26fSGleb Smirnoff 
33*b2d3d26fSGleb Smirnoff #include <sys/cdefs.h>
34*b2d3d26fSGleb Smirnoff __FBSDID("$FreeBSD$");
35*b2d3d26fSGleb Smirnoff 
36*b2d3d26fSGleb Smirnoff /*
37*b2d3d26fSGleb Smirnoff  * RealTek 8129/8139 PCI NIC driver
38*b2d3d26fSGleb Smirnoff  *
39*b2d3d26fSGleb Smirnoff  * Supports several extremely cheap PCI 10/100 adapters based on
40*b2d3d26fSGleb Smirnoff  * the RealTek chipset. Datasheets can be obtained from
41*b2d3d26fSGleb Smirnoff  * www.realtek.com.tw.
42*b2d3d26fSGleb Smirnoff  *
43*b2d3d26fSGleb Smirnoff  * Written by Bill Paul <wpaul@ctr.columbia.edu>
44*b2d3d26fSGleb Smirnoff  * Electrical Engineering Department
45*b2d3d26fSGleb Smirnoff  * Columbia University, New York City
46*b2d3d26fSGleb Smirnoff  */
47*b2d3d26fSGleb Smirnoff /*
48*b2d3d26fSGleb Smirnoff  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
49*b2d3d26fSGleb Smirnoff  * probably the worst PCI ethernet controller ever made, with the possible
50*b2d3d26fSGleb Smirnoff  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
51*b2d3d26fSGleb Smirnoff  * DMA, but it has a terrible interface that nullifies any performance
52*b2d3d26fSGleb Smirnoff  * gains that bus-master DMA usually offers.
53*b2d3d26fSGleb Smirnoff  *
54*b2d3d26fSGleb Smirnoff  * For transmission, the chip offers a series of four TX descriptor
55*b2d3d26fSGleb Smirnoff  * registers. Each transmit frame must be in a contiguous buffer, aligned
56*b2d3d26fSGleb Smirnoff  * on a longword (32-bit) boundary. This means we almost always have to
57*b2d3d26fSGleb Smirnoff  * do mbuf copies in order to transmit a frame, except in the unlikely
58*b2d3d26fSGleb Smirnoff  * case where a) the packet fits into a single mbuf, and b) the packet
59*b2d3d26fSGleb Smirnoff  * is 32-bit aligned within the mbuf's data area. The presence of only
60*b2d3d26fSGleb Smirnoff  * four descriptor registers means that we can never have more than four
61*b2d3d26fSGleb Smirnoff  * packets queued for transmission at any one time.
62*b2d3d26fSGleb Smirnoff  *
63*b2d3d26fSGleb Smirnoff  * Reception is not much better. The driver has to allocate a single large
64*b2d3d26fSGleb Smirnoff  * buffer area (up to 64K in size) into which the chip will DMA received
65*b2d3d26fSGleb Smirnoff  * frames. Because we don't know where within this region received packets
66*b2d3d26fSGleb Smirnoff  * will begin or end, we have no choice but to copy data from the buffer
67*b2d3d26fSGleb Smirnoff  * area into mbufs in order to pass the packets up to the higher protocol
68*b2d3d26fSGleb Smirnoff  * levels.
69*b2d3d26fSGleb Smirnoff  *
70*b2d3d26fSGleb Smirnoff  * It's impossible given this rotten design to really achieve decent
71*b2d3d26fSGleb Smirnoff  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
72*b2d3d26fSGleb Smirnoff  * some equally overmuscled CPU to drive it.
73*b2d3d26fSGleb Smirnoff  *
74*b2d3d26fSGleb Smirnoff  * On the bright side, the 8139 does have a built-in PHY, although
75*b2d3d26fSGleb Smirnoff  * rather than using an MDIO serial interface like most other NICs, the
76*b2d3d26fSGleb Smirnoff  * PHY registers are directly accessible through the 8139's register
77*b2d3d26fSGleb Smirnoff  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
78*b2d3d26fSGleb Smirnoff  * filter.
79*b2d3d26fSGleb Smirnoff  *
80*b2d3d26fSGleb Smirnoff  * The 8129 chip is an older version of the 8139 that uses an external PHY
81*b2d3d26fSGleb Smirnoff  * chip. The 8129 has a serial MDIO interface for accessing the MII where
82*b2d3d26fSGleb Smirnoff  * the 8139 lets you directly access the on-board PHY registers. We need
83*b2d3d26fSGleb Smirnoff  * to select which interface to use depending on the chip type.
84*b2d3d26fSGleb Smirnoff  */
85*b2d3d26fSGleb Smirnoff 
86*b2d3d26fSGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
87*b2d3d26fSGleb Smirnoff #include "opt_device_polling.h"
88*b2d3d26fSGleb Smirnoff #endif
89*b2d3d26fSGleb Smirnoff 
90*b2d3d26fSGleb Smirnoff #include <sys/param.h>
91*b2d3d26fSGleb Smirnoff #include <sys/endian.h>
92*b2d3d26fSGleb Smirnoff #include <sys/systm.h>
93*b2d3d26fSGleb Smirnoff #include <sys/sockio.h>
94*b2d3d26fSGleb Smirnoff #include <sys/mbuf.h>
95*b2d3d26fSGleb Smirnoff #include <sys/malloc.h>
96*b2d3d26fSGleb Smirnoff #include <sys/kernel.h>
97*b2d3d26fSGleb Smirnoff #include <sys/module.h>
98*b2d3d26fSGleb Smirnoff #include <sys/socket.h>
99*b2d3d26fSGleb Smirnoff #include <sys/sysctl.h>
100*b2d3d26fSGleb Smirnoff 
101*b2d3d26fSGleb Smirnoff #include <net/if.h>
102*b2d3d26fSGleb Smirnoff #include <net/if_var.h>
103*b2d3d26fSGleb Smirnoff #include <net/if_arp.h>
104*b2d3d26fSGleb Smirnoff #include <net/ethernet.h>
105*b2d3d26fSGleb Smirnoff #include <net/if_dl.h>
106*b2d3d26fSGleb Smirnoff #include <net/if_media.h>
107*b2d3d26fSGleb Smirnoff #include <net/if_types.h>
108*b2d3d26fSGleb Smirnoff 
109*b2d3d26fSGleb Smirnoff #include <net/bpf.h>
110*b2d3d26fSGleb Smirnoff 
111*b2d3d26fSGleb Smirnoff #include <machine/bus.h>
112*b2d3d26fSGleb Smirnoff #include <machine/resource.h>
113*b2d3d26fSGleb Smirnoff #include <sys/bus.h>
114*b2d3d26fSGleb Smirnoff #include <sys/rman.h>
115*b2d3d26fSGleb Smirnoff 
116*b2d3d26fSGleb Smirnoff #include <dev/mii/mii.h>
117*b2d3d26fSGleb Smirnoff #include <dev/mii/mii_bitbang.h>
118*b2d3d26fSGleb Smirnoff #include <dev/mii/miivar.h>
119*b2d3d26fSGleb Smirnoff 
120*b2d3d26fSGleb Smirnoff #include <dev/pci/pcireg.h>
121*b2d3d26fSGleb Smirnoff #include <dev/pci/pcivar.h>
122*b2d3d26fSGleb Smirnoff 
123*b2d3d26fSGleb Smirnoff MODULE_DEPEND(rl, pci, 1, 1, 1);
124*b2d3d26fSGleb Smirnoff MODULE_DEPEND(rl, ether, 1, 1, 1);
125*b2d3d26fSGleb Smirnoff MODULE_DEPEND(rl, miibus, 1, 1, 1);
126*b2d3d26fSGleb Smirnoff 
127*b2d3d26fSGleb Smirnoff /* "device miibus" required.  See GENERIC if you get errors here. */
128*b2d3d26fSGleb Smirnoff #include "miibus_if.h"
129*b2d3d26fSGleb Smirnoff 
130*b2d3d26fSGleb Smirnoff #include <dev/rl/if_rlreg.h>
131*b2d3d26fSGleb Smirnoff 
132*b2d3d26fSGleb Smirnoff /*
133*b2d3d26fSGleb Smirnoff  * Various supported device vendors/types and their names.
134*b2d3d26fSGleb Smirnoff  */
135*b2d3d26fSGleb Smirnoff static const struct rl_type rl_devs[] = {
136*b2d3d26fSGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8129, RL_8129,
137*b2d3d26fSGleb Smirnoff 		"RealTek 8129 10/100BaseTX" },
138*b2d3d26fSGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8139, RL_8139,
139*b2d3d26fSGleb Smirnoff 		"RealTek 8139 10/100BaseTX" },
140*b2d3d26fSGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8139D, RL_8139,
141*b2d3d26fSGleb Smirnoff 		"RealTek 8139 10/100BaseTX" },
142*b2d3d26fSGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8138, RL_8139,
143*b2d3d26fSGleb Smirnoff 		"RealTek 8139 10/100BaseTX CardBus" },
144*b2d3d26fSGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8100, RL_8139,
145*b2d3d26fSGleb Smirnoff 		"RealTek 8100 10/100BaseTX" },
146*b2d3d26fSGleb Smirnoff 	{ ACCTON_VENDORID, ACCTON_DEVICEID_5030, RL_8139,
147*b2d3d26fSGleb Smirnoff 		"Accton MPX 5030/5038 10/100BaseTX" },
148*b2d3d26fSGleb Smirnoff 	{ DELTA_VENDORID, DELTA_DEVICEID_8139, RL_8139,
149*b2d3d26fSGleb Smirnoff 		"Delta Electronics 8139 10/100BaseTX" },
150*b2d3d26fSGleb Smirnoff 	{ ADDTRON_VENDORID, ADDTRON_DEVICEID_8139, RL_8139,
151*b2d3d26fSGleb Smirnoff 		"Addtron Technology 8139 10/100BaseTX" },
152*b2d3d26fSGleb Smirnoff 	{ DLINK_VENDORID, DLINK_DEVICEID_520TX_REVC1, RL_8139,
153*b2d3d26fSGleb Smirnoff 		"D-Link DFE-520TX (rev. C1) 10/100BaseTX" },
154*b2d3d26fSGleb Smirnoff 	{ DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS, RL_8139,
155*b2d3d26fSGleb Smirnoff 		"D-Link DFE-530TX+ 10/100BaseTX" },
156*b2d3d26fSGleb Smirnoff 	{ DLINK_VENDORID, DLINK_DEVICEID_690TXD, RL_8139,
157*b2d3d26fSGleb Smirnoff 		"D-Link DFE-690TXD 10/100BaseTX" },
158*b2d3d26fSGleb Smirnoff 	{ NORTEL_VENDORID, ACCTON_DEVICEID_5030, RL_8139,
159*b2d3d26fSGleb Smirnoff 		"Nortel Networks 10/100BaseTX" },
160*b2d3d26fSGleb Smirnoff 	{ COREGA_VENDORID, COREGA_DEVICEID_FETHERCBTXD, RL_8139,
161*b2d3d26fSGleb Smirnoff 		"Corega FEther CB-TXD" },
162*b2d3d26fSGleb Smirnoff 	{ COREGA_VENDORID, COREGA_DEVICEID_FETHERIICBTXD, RL_8139,
163*b2d3d26fSGleb Smirnoff 		"Corega FEtherII CB-TXD" },
164*b2d3d26fSGleb Smirnoff 	{ PEPPERCON_VENDORID, PEPPERCON_DEVICEID_ROLF, RL_8139,
165*b2d3d26fSGleb Smirnoff 		"Peppercon AG ROL-F" },
166*b2d3d26fSGleb Smirnoff 	{ PLANEX_VENDORID, PLANEX_DEVICEID_FNW3603TX, RL_8139,
167*b2d3d26fSGleb Smirnoff 		"Planex FNW-3603-TX" },
168*b2d3d26fSGleb Smirnoff 	{ PLANEX_VENDORID, PLANEX_DEVICEID_FNW3800TX, RL_8139,
169*b2d3d26fSGleb Smirnoff 		"Planex FNW-3800-TX" },
170*b2d3d26fSGleb Smirnoff 	{ CP_VENDORID, RT_DEVICEID_8139, RL_8139,
171*b2d3d26fSGleb Smirnoff 		"Compaq HNE-300" },
172*b2d3d26fSGleb Smirnoff 	{ LEVEL1_VENDORID, LEVEL1_DEVICEID_FPC0106TX, RL_8139,
173*b2d3d26fSGleb Smirnoff 		"LevelOne FPC-0106TX" },
174*b2d3d26fSGleb Smirnoff 	{ EDIMAX_VENDORID, EDIMAX_DEVICEID_EP4103DL, RL_8139,
175*b2d3d26fSGleb Smirnoff 		"Edimax EP-4103DL CardBus" }
176*b2d3d26fSGleb Smirnoff };
177*b2d3d26fSGleb Smirnoff 
178*b2d3d26fSGleb Smirnoff static int rl_attach(device_t);
179*b2d3d26fSGleb Smirnoff static int rl_detach(device_t);
180*b2d3d26fSGleb Smirnoff static void rl_dmamap_cb(void *, bus_dma_segment_t *, int, int);
181*b2d3d26fSGleb Smirnoff static int rl_dma_alloc(struct rl_softc *);
182*b2d3d26fSGleb Smirnoff static void rl_dma_free(struct rl_softc *);
183*b2d3d26fSGleb Smirnoff static void rl_eeprom_putbyte(struct rl_softc *, int);
184*b2d3d26fSGleb Smirnoff static void rl_eeprom_getword(struct rl_softc *, int, uint16_t *);
185*b2d3d26fSGleb Smirnoff static int rl_encap(struct rl_softc *, struct mbuf **);
186*b2d3d26fSGleb Smirnoff static int rl_list_tx_init(struct rl_softc *);
187*b2d3d26fSGleb Smirnoff static int rl_list_rx_init(struct rl_softc *);
188*b2d3d26fSGleb Smirnoff static int rl_ifmedia_upd(struct ifnet *);
189*b2d3d26fSGleb Smirnoff static void rl_ifmedia_sts(struct ifnet *, struct ifmediareq *);
190*b2d3d26fSGleb Smirnoff static int rl_ioctl(struct ifnet *, u_long, caddr_t);
191*b2d3d26fSGleb Smirnoff static void rl_intr(void *);
192*b2d3d26fSGleb Smirnoff static void rl_init(void *);
193*b2d3d26fSGleb Smirnoff static void rl_init_locked(struct rl_softc *sc);
194*b2d3d26fSGleb Smirnoff static int rl_miibus_readreg(device_t, int, int);
195*b2d3d26fSGleb Smirnoff static void rl_miibus_statchg(device_t);
196*b2d3d26fSGleb Smirnoff static int rl_miibus_writereg(device_t, int, int, int);
197*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
198*b2d3d26fSGleb Smirnoff static int rl_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
199*b2d3d26fSGleb Smirnoff static int rl_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count);
200*b2d3d26fSGleb Smirnoff #endif
201*b2d3d26fSGleb Smirnoff static int rl_probe(device_t);
202*b2d3d26fSGleb Smirnoff static void rl_read_eeprom(struct rl_softc *, uint8_t *, int, int, int);
203*b2d3d26fSGleb Smirnoff static void rl_reset(struct rl_softc *);
204*b2d3d26fSGleb Smirnoff static int rl_resume(device_t);
205*b2d3d26fSGleb Smirnoff static int rl_rxeof(struct rl_softc *);
206*b2d3d26fSGleb Smirnoff static void rl_rxfilter(struct rl_softc *);
207*b2d3d26fSGleb Smirnoff static int rl_shutdown(device_t);
208*b2d3d26fSGleb Smirnoff static void rl_start(struct ifnet *);
209*b2d3d26fSGleb Smirnoff static void rl_start_locked(struct ifnet *);
210*b2d3d26fSGleb Smirnoff static void rl_stop(struct rl_softc *);
211*b2d3d26fSGleb Smirnoff static int rl_suspend(device_t);
212*b2d3d26fSGleb Smirnoff static void rl_tick(void *);
213*b2d3d26fSGleb Smirnoff static void rl_txeof(struct rl_softc *);
214*b2d3d26fSGleb Smirnoff static void rl_watchdog(struct rl_softc *);
215*b2d3d26fSGleb Smirnoff static void rl_setwol(struct rl_softc *);
216*b2d3d26fSGleb Smirnoff static void rl_clrwol(struct rl_softc *);
217*b2d3d26fSGleb Smirnoff 
218*b2d3d26fSGleb Smirnoff /*
219*b2d3d26fSGleb Smirnoff  * MII bit-bang glue
220*b2d3d26fSGleb Smirnoff  */
221*b2d3d26fSGleb Smirnoff static uint32_t rl_mii_bitbang_read(device_t);
222*b2d3d26fSGleb Smirnoff static void rl_mii_bitbang_write(device_t, uint32_t);
223*b2d3d26fSGleb Smirnoff 
224*b2d3d26fSGleb Smirnoff static const struct mii_bitbang_ops rl_mii_bitbang_ops = {
225*b2d3d26fSGleb Smirnoff 	rl_mii_bitbang_read,
226*b2d3d26fSGleb Smirnoff 	rl_mii_bitbang_write,
227*b2d3d26fSGleb Smirnoff 	{
228*b2d3d26fSGleb Smirnoff 		RL_MII_DATAOUT,	/* MII_BIT_MDO */
229*b2d3d26fSGleb Smirnoff 		RL_MII_DATAIN,	/* MII_BIT_MDI */
230*b2d3d26fSGleb Smirnoff 		RL_MII_CLK,	/* MII_BIT_MDC */
231*b2d3d26fSGleb Smirnoff 		RL_MII_DIR,	/* MII_BIT_DIR_HOST_PHY */
232*b2d3d26fSGleb Smirnoff 		0,		/* MII_BIT_DIR_PHY_HOST */
233*b2d3d26fSGleb Smirnoff 	}
234*b2d3d26fSGleb Smirnoff };
235*b2d3d26fSGleb Smirnoff 
236*b2d3d26fSGleb Smirnoff static device_method_t rl_methods[] = {
237*b2d3d26fSGleb Smirnoff 	/* Device interface */
238*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_probe,		rl_probe),
239*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_attach,	rl_attach),
240*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_detach,	rl_detach),
241*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_suspend,	rl_suspend),
242*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_resume,	rl_resume),
243*b2d3d26fSGleb Smirnoff 	DEVMETHOD(device_shutdown,	rl_shutdown),
244*b2d3d26fSGleb Smirnoff 
245*b2d3d26fSGleb Smirnoff 	/* MII interface */
246*b2d3d26fSGleb Smirnoff 	DEVMETHOD(miibus_readreg,	rl_miibus_readreg),
247*b2d3d26fSGleb Smirnoff 	DEVMETHOD(miibus_writereg,	rl_miibus_writereg),
248*b2d3d26fSGleb Smirnoff 	DEVMETHOD(miibus_statchg,	rl_miibus_statchg),
249*b2d3d26fSGleb Smirnoff 
250*b2d3d26fSGleb Smirnoff 	DEVMETHOD_END
251*b2d3d26fSGleb Smirnoff };
252*b2d3d26fSGleb Smirnoff 
253*b2d3d26fSGleb Smirnoff static driver_t rl_driver = {
254*b2d3d26fSGleb Smirnoff 	"rl",
255*b2d3d26fSGleb Smirnoff 	rl_methods,
256*b2d3d26fSGleb Smirnoff 	sizeof(struct rl_softc)
257*b2d3d26fSGleb Smirnoff };
258*b2d3d26fSGleb Smirnoff 
259*b2d3d26fSGleb Smirnoff static devclass_t rl_devclass;
260*b2d3d26fSGleb Smirnoff 
261*b2d3d26fSGleb Smirnoff DRIVER_MODULE(rl, pci, rl_driver, rl_devclass, 0, 0);
262*b2d3d26fSGleb Smirnoff DRIVER_MODULE(rl, cardbus, rl_driver, rl_devclass, 0, 0);
263*b2d3d26fSGleb Smirnoff DRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0);
264*b2d3d26fSGleb Smirnoff 
265*b2d3d26fSGleb Smirnoff #define EE_SET(x)					\
266*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD,			\
267*b2d3d26fSGleb Smirnoff 		CSR_READ_1(sc, RL_EECMD) | x)
268*b2d3d26fSGleb Smirnoff 
269*b2d3d26fSGleb Smirnoff #define EE_CLR(x)					\
270*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD,			\
271*b2d3d26fSGleb Smirnoff 		CSR_READ_1(sc, RL_EECMD) & ~x)
272*b2d3d26fSGleb Smirnoff 
273*b2d3d26fSGleb Smirnoff /*
274*b2d3d26fSGleb Smirnoff  * Send a read command and address to the EEPROM, check for ACK.
275*b2d3d26fSGleb Smirnoff  */
276*b2d3d26fSGleb Smirnoff static void
277*b2d3d26fSGleb Smirnoff rl_eeprom_putbyte(struct rl_softc *sc, int addr)
278*b2d3d26fSGleb Smirnoff {
279*b2d3d26fSGleb Smirnoff 	register int		d, i;
280*b2d3d26fSGleb Smirnoff 
281*b2d3d26fSGleb Smirnoff 	d = addr | sc->rl_eecmd_read;
282*b2d3d26fSGleb Smirnoff 
283*b2d3d26fSGleb Smirnoff 	/*
284*b2d3d26fSGleb Smirnoff 	 * Feed in each bit and strobe the clock.
285*b2d3d26fSGleb Smirnoff 	 */
286*b2d3d26fSGleb Smirnoff 	for (i = 0x400; i; i >>= 1) {
287*b2d3d26fSGleb Smirnoff 		if (d & i) {
288*b2d3d26fSGleb Smirnoff 			EE_SET(RL_EE_DATAIN);
289*b2d3d26fSGleb Smirnoff 		} else {
290*b2d3d26fSGleb Smirnoff 			EE_CLR(RL_EE_DATAIN);
291*b2d3d26fSGleb Smirnoff 		}
292*b2d3d26fSGleb Smirnoff 		DELAY(100);
293*b2d3d26fSGleb Smirnoff 		EE_SET(RL_EE_CLK);
294*b2d3d26fSGleb Smirnoff 		DELAY(150);
295*b2d3d26fSGleb Smirnoff 		EE_CLR(RL_EE_CLK);
296*b2d3d26fSGleb Smirnoff 		DELAY(100);
297*b2d3d26fSGleb Smirnoff 	}
298*b2d3d26fSGleb Smirnoff }
299*b2d3d26fSGleb Smirnoff 
300*b2d3d26fSGleb Smirnoff /*
301*b2d3d26fSGleb Smirnoff  * Read a word of data stored in the EEPROM at address 'addr.'
302*b2d3d26fSGleb Smirnoff  */
303*b2d3d26fSGleb Smirnoff static void
304*b2d3d26fSGleb Smirnoff rl_eeprom_getword(struct rl_softc *sc, int addr, uint16_t *dest)
305*b2d3d26fSGleb Smirnoff {
306*b2d3d26fSGleb Smirnoff 	register int		i;
307*b2d3d26fSGleb Smirnoff 	uint16_t		word = 0;
308*b2d3d26fSGleb Smirnoff 
309*b2d3d26fSGleb Smirnoff 	/* Enter EEPROM access mode. */
310*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
311*b2d3d26fSGleb Smirnoff 
312*b2d3d26fSGleb Smirnoff 	/*
313*b2d3d26fSGleb Smirnoff 	 * Send address of word we want to read.
314*b2d3d26fSGleb Smirnoff 	 */
315*b2d3d26fSGleb Smirnoff 	rl_eeprom_putbyte(sc, addr);
316*b2d3d26fSGleb Smirnoff 
317*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
318*b2d3d26fSGleb Smirnoff 
319*b2d3d26fSGleb Smirnoff 	/*
320*b2d3d26fSGleb Smirnoff 	 * Start reading bits from EEPROM.
321*b2d3d26fSGleb Smirnoff 	 */
322*b2d3d26fSGleb Smirnoff 	for (i = 0x8000; i; i >>= 1) {
323*b2d3d26fSGleb Smirnoff 		EE_SET(RL_EE_CLK);
324*b2d3d26fSGleb Smirnoff 		DELAY(100);
325*b2d3d26fSGleb Smirnoff 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
326*b2d3d26fSGleb Smirnoff 			word |= i;
327*b2d3d26fSGleb Smirnoff 		EE_CLR(RL_EE_CLK);
328*b2d3d26fSGleb Smirnoff 		DELAY(100);
329*b2d3d26fSGleb Smirnoff 	}
330*b2d3d26fSGleb Smirnoff 
331*b2d3d26fSGleb Smirnoff 	/* Turn off EEPROM access mode. */
332*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
333*b2d3d26fSGleb Smirnoff 
334*b2d3d26fSGleb Smirnoff 	*dest = word;
335*b2d3d26fSGleb Smirnoff }
336*b2d3d26fSGleb Smirnoff 
337*b2d3d26fSGleb Smirnoff /*
338*b2d3d26fSGleb Smirnoff  * Read a sequence of words from the EEPROM.
339*b2d3d26fSGleb Smirnoff  */
340*b2d3d26fSGleb Smirnoff static void
341*b2d3d26fSGleb Smirnoff rl_read_eeprom(struct rl_softc *sc, uint8_t *dest, int off, int cnt, int swap)
342*b2d3d26fSGleb Smirnoff {
343*b2d3d26fSGleb Smirnoff 	int			i;
344*b2d3d26fSGleb Smirnoff 	uint16_t		word = 0, *ptr;
345*b2d3d26fSGleb Smirnoff 
346*b2d3d26fSGleb Smirnoff 	for (i = 0; i < cnt; i++) {
347*b2d3d26fSGleb Smirnoff 		rl_eeprom_getword(sc, off + i, &word);
348*b2d3d26fSGleb Smirnoff 		ptr = (uint16_t *)(dest + (i * 2));
349*b2d3d26fSGleb Smirnoff 		if (swap)
350*b2d3d26fSGleb Smirnoff 			*ptr = ntohs(word);
351*b2d3d26fSGleb Smirnoff 		else
352*b2d3d26fSGleb Smirnoff 			*ptr = word;
353*b2d3d26fSGleb Smirnoff 	}
354*b2d3d26fSGleb Smirnoff }
355*b2d3d26fSGleb Smirnoff 
356*b2d3d26fSGleb Smirnoff /*
357*b2d3d26fSGleb Smirnoff  * Read the MII serial port for the MII bit-bang module.
358*b2d3d26fSGleb Smirnoff  */
359*b2d3d26fSGleb Smirnoff static uint32_t
360*b2d3d26fSGleb Smirnoff rl_mii_bitbang_read(device_t dev)
361*b2d3d26fSGleb Smirnoff {
362*b2d3d26fSGleb Smirnoff 	struct rl_softc *sc;
363*b2d3d26fSGleb Smirnoff 	uint32_t val;
364*b2d3d26fSGleb Smirnoff 
365*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
366*b2d3d26fSGleb Smirnoff 
367*b2d3d26fSGleb Smirnoff 	val = CSR_READ_1(sc, RL_MII);
368*b2d3d26fSGleb Smirnoff 	CSR_BARRIER(sc, RL_MII, 1,
369*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
370*b2d3d26fSGleb Smirnoff 
371*b2d3d26fSGleb Smirnoff 	return (val);
372*b2d3d26fSGleb Smirnoff }
373*b2d3d26fSGleb Smirnoff 
374*b2d3d26fSGleb Smirnoff /*
375*b2d3d26fSGleb Smirnoff  * Write the MII serial port for the MII bit-bang module.
376*b2d3d26fSGleb Smirnoff  */
377*b2d3d26fSGleb Smirnoff static void
378*b2d3d26fSGleb Smirnoff rl_mii_bitbang_write(device_t dev, uint32_t val)
379*b2d3d26fSGleb Smirnoff {
380*b2d3d26fSGleb Smirnoff 	struct rl_softc *sc;
381*b2d3d26fSGleb Smirnoff 
382*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
383*b2d3d26fSGleb Smirnoff 
384*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_MII, val);
385*b2d3d26fSGleb Smirnoff 	CSR_BARRIER(sc, RL_MII, 1,
386*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
387*b2d3d26fSGleb Smirnoff }
388*b2d3d26fSGleb Smirnoff 
389*b2d3d26fSGleb Smirnoff static int
390*b2d3d26fSGleb Smirnoff rl_miibus_readreg(device_t dev, int phy, int reg)
391*b2d3d26fSGleb Smirnoff {
392*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
393*b2d3d26fSGleb Smirnoff 	uint16_t		rl8139_reg;
394*b2d3d26fSGleb Smirnoff 
395*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
396*b2d3d26fSGleb Smirnoff 
397*b2d3d26fSGleb Smirnoff 	if (sc->rl_type == RL_8139) {
398*b2d3d26fSGleb Smirnoff 		switch (reg) {
399*b2d3d26fSGleb Smirnoff 		case MII_BMCR:
400*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_BMCR;
401*b2d3d26fSGleb Smirnoff 			break;
402*b2d3d26fSGleb Smirnoff 		case MII_BMSR:
403*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_BMSR;
404*b2d3d26fSGleb Smirnoff 			break;
405*b2d3d26fSGleb Smirnoff 		case MII_ANAR:
406*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_ANAR;
407*b2d3d26fSGleb Smirnoff 			break;
408*b2d3d26fSGleb Smirnoff 		case MII_ANER:
409*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_ANER;
410*b2d3d26fSGleb Smirnoff 			break;
411*b2d3d26fSGleb Smirnoff 		case MII_ANLPAR:
412*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_LPAR;
413*b2d3d26fSGleb Smirnoff 			break;
414*b2d3d26fSGleb Smirnoff 		case MII_PHYIDR1:
415*b2d3d26fSGleb Smirnoff 		case MII_PHYIDR2:
416*b2d3d26fSGleb Smirnoff 			return (0);
417*b2d3d26fSGleb Smirnoff 		/*
418*b2d3d26fSGleb Smirnoff 		 * Allow the rlphy driver to read the media status
419*b2d3d26fSGleb Smirnoff 		 * register. If we have a link partner which does not
420*b2d3d26fSGleb Smirnoff 		 * support NWAY, this is the register which will tell
421*b2d3d26fSGleb Smirnoff 		 * us the results of parallel detection.
422*b2d3d26fSGleb Smirnoff 		 */
423*b2d3d26fSGleb Smirnoff 		case RL_MEDIASTAT:
424*b2d3d26fSGleb Smirnoff 			return (CSR_READ_1(sc, RL_MEDIASTAT));
425*b2d3d26fSGleb Smirnoff 		default:
426*b2d3d26fSGleb Smirnoff 			device_printf(sc->rl_dev, "bad phy register\n");
427*b2d3d26fSGleb Smirnoff 			return (0);
428*b2d3d26fSGleb Smirnoff 		}
429*b2d3d26fSGleb Smirnoff 		return (CSR_READ_2(sc, rl8139_reg));
430*b2d3d26fSGleb Smirnoff 	}
431*b2d3d26fSGleb Smirnoff 
432*b2d3d26fSGleb Smirnoff 	return (mii_bitbang_readreg(dev, &rl_mii_bitbang_ops, phy, reg));
433*b2d3d26fSGleb Smirnoff }
434*b2d3d26fSGleb Smirnoff 
435*b2d3d26fSGleb Smirnoff static int
436*b2d3d26fSGleb Smirnoff rl_miibus_writereg(device_t dev, int phy, int reg, int data)
437*b2d3d26fSGleb Smirnoff {
438*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
439*b2d3d26fSGleb Smirnoff 	uint16_t		rl8139_reg;
440*b2d3d26fSGleb Smirnoff 
441*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
442*b2d3d26fSGleb Smirnoff 
443*b2d3d26fSGleb Smirnoff 	if (sc->rl_type == RL_8139) {
444*b2d3d26fSGleb Smirnoff 		switch (reg) {
445*b2d3d26fSGleb Smirnoff 		case MII_BMCR:
446*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_BMCR;
447*b2d3d26fSGleb Smirnoff 			break;
448*b2d3d26fSGleb Smirnoff 		case MII_BMSR:
449*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_BMSR;
450*b2d3d26fSGleb Smirnoff 			break;
451*b2d3d26fSGleb Smirnoff 		case MII_ANAR:
452*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_ANAR;
453*b2d3d26fSGleb Smirnoff 			break;
454*b2d3d26fSGleb Smirnoff 		case MII_ANER:
455*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_ANER;
456*b2d3d26fSGleb Smirnoff 			break;
457*b2d3d26fSGleb Smirnoff 		case MII_ANLPAR:
458*b2d3d26fSGleb Smirnoff 			rl8139_reg = RL_LPAR;
459*b2d3d26fSGleb Smirnoff 			break;
460*b2d3d26fSGleb Smirnoff 		case MII_PHYIDR1:
461*b2d3d26fSGleb Smirnoff 		case MII_PHYIDR2:
462*b2d3d26fSGleb Smirnoff 			return (0);
463*b2d3d26fSGleb Smirnoff 			break;
464*b2d3d26fSGleb Smirnoff 		default:
465*b2d3d26fSGleb Smirnoff 			device_printf(sc->rl_dev, "bad phy register\n");
466*b2d3d26fSGleb Smirnoff 			return (0);
467*b2d3d26fSGleb Smirnoff 		}
468*b2d3d26fSGleb Smirnoff 		CSR_WRITE_2(sc, rl8139_reg, data);
469*b2d3d26fSGleb Smirnoff 		return (0);
470*b2d3d26fSGleb Smirnoff 	}
471*b2d3d26fSGleb Smirnoff 
472*b2d3d26fSGleb Smirnoff 	mii_bitbang_writereg(dev, &rl_mii_bitbang_ops, phy, reg, data);
473*b2d3d26fSGleb Smirnoff 
474*b2d3d26fSGleb Smirnoff 	return (0);
475*b2d3d26fSGleb Smirnoff }
476*b2d3d26fSGleb Smirnoff 
477*b2d3d26fSGleb Smirnoff static void
478*b2d3d26fSGleb Smirnoff rl_miibus_statchg(device_t dev)
479*b2d3d26fSGleb Smirnoff {
480*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
481*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
482*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
483*b2d3d26fSGleb Smirnoff 
484*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
485*b2d3d26fSGleb Smirnoff 	mii = device_get_softc(sc->rl_miibus);
486*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp;
487*b2d3d26fSGleb Smirnoff 	if (mii == NULL || ifp == NULL ||
488*b2d3d26fSGleb Smirnoff 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
489*b2d3d26fSGleb Smirnoff 		return;
490*b2d3d26fSGleb Smirnoff 
491*b2d3d26fSGleb Smirnoff 	sc->rl_flags &= ~RL_FLAG_LINK;
492*b2d3d26fSGleb Smirnoff 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
493*b2d3d26fSGleb Smirnoff 	    (IFM_ACTIVE | IFM_AVALID)) {
494*b2d3d26fSGleb Smirnoff 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
495*b2d3d26fSGleb Smirnoff 		case IFM_10_T:
496*b2d3d26fSGleb Smirnoff 		case IFM_100_TX:
497*b2d3d26fSGleb Smirnoff 			sc->rl_flags |= RL_FLAG_LINK;
498*b2d3d26fSGleb Smirnoff 			break;
499*b2d3d26fSGleb Smirnoff 		default:
500*b2d3d26fSGleb Smirnoff 			break;
501*b2d3d26fSGleb Smirnoff 		}
502*b2d3d26fSGleb Smirnoff 	}
503*b2d3d26fSGleb Smirnoff 	/*
504*b2d3d26fSGleb Smirnoff 	 * RealTek controllers do not provide any interface to
505*b2d3d26fSGleb Smirnoff 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
506*b2d3d26fSGleb Smirnoff 	 * parameters.
507*b2d3d26fSGleb Smirnoff 	 */
508*b2d3d26fSGleb Smirnoff }
509*b2d3d26fSGleb Smirnoff 
510*b2d3d26fSGleb Smirnoff /*
511*b2d3d26fSGleb Smirnoff  * Program the 64-bit multicast hash filter.
512*b2d3d26fSGleb Smirnoff  */
513*b2d3d26fSGleb Smirnoff static void
514*b2d3d26fSGleb Smirnoff rl_rxfilter(struct rl_softc *sc)
515*b2d3d26fSGleb Smirnoff {
516*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
517*b2d3d26fSGleb Smirnoff 	int			h = 0;
518*b2d3d26fSGleb Smirnoff 	uint32_t		hashes[2] = { 0, 0 };
519*b2d3d26fSGleb Smirnoff 	struct ifmultiaddr	*ifma;
520*b2d3d26fSGleb Smirnoff 	uint32_t		rxfilt;
521*b2d3d26fSGleb Smirnoff 
522*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
523*b2d3d26fSGleb Smirnoff 
524*b2d3d26fSGleb Smirnoff 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
525*b2d3d26fSGleb Smirnoff 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_BROAD |
526*b2d3d26fSGleb Smirnoff 	    RL_RXCFG_RX_MULTI);
527*b2d3d26fSGleb Smirnoff 	/* Always accept frames destined for this host. */
528*b2d3d26fSGleb Smirnoff 	rxfilt |= RL_RXCFG_RX_INDIV;
529*b2d3d26fSGleb Smirnoff 	/* Set capture broadcast bit to capture broadcast frames. */
530*b2d3d26fSGleb Smirnoff 	if (ifp->if_flags & IFF_BROADCAST)
531*b2d3d26fSGleb Smirnoff 		rxfilt |= RL_RXCFG_RX_BROAD;
532*b2d3d26fSGleb Smirnoff 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
533*b2d3d26fSGleb Smirnoff 		rxfilt |= RL_RXCFG_RX_MULTI;
534*b2d3d26fSGleb Smirnoff 		if (ifp->if_flags & IFF_PROMISC)
535*b2d3d26fSGleb Smirnoff 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
536*b2d3d26fSGleb Smirnoff 		hashes[0] = 0xFFFFFFFF;
537*b2d3d26fSGleb Smirnoff 		hashes[1] = 0xFFFFFFFF;
538*b2d3d26fSGleb Smirnoff 	} else {
539*b2d3d26fSGleb Smirnoff 		/* Now program new ones. */
540*b2d3d26fSGleb Smirnoff 		if_maddr_rlock(ifp);
541*b2d3d26fSGleb Smirnoff 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
542*b2d3d26fSGleb Smirnoff 			if (ifma->ifma_addr->sa_family != AF_LINK)
543*b2d3d26fSGleb Smirnoff 				continue;
544*b2d3d26fSGleb Smirnoff 			h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
545*b2d3d26fSGleb Smirnoff 			    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
546*b2d3d26fSGleb Smirnoff 			if (h < 32)
547*b2d3d26fSGleb Smirnoff 				hashes[0] |= (1 << h);
548*b2d3d26fSGleb Smirnoff 			else
549*b2d3d26fSGleb Smirnoff 				hashes[1] |= (1 << (h - 32));
550*b2d3d26fSGleb Smirnoff 		}
551*b2d3d26fSGleb Smirnoff 		if_maddr_runlock(ifp);
552*b2d3d26fSGleb Smirnoff 		if (hashes[0] != 0 || hashes[1] != 0)
553*b2d3d26fSGleb Smirnoff 			rxfilt |= RL_RXCFG_RX_MULTI;
554*b2d3d26fSGleb Smirnoff 	}
555*b2d3d26fSGleb Smirnoff 
556*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
557*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
558*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
559*b2d3d26fSGleb Smirnoff }
560*b2d3d26fSGleb Smirnoff 
561*b2d3d26fSGleb Smirnoff static void
562*b2d3d26fSGleb Smirnoff rl_reset(struct rl_softc *sc)
563*b2d3d26fSGleb Smirnoff {
564*b2d3d26fSGleb Smirnoff 	register int		i;
565*b2d3d26fSGleb Smirnoff 
566*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
567*b2d3d26fSGleb Smirnoff 
568*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
569*b2d3d26fSGleb Smirnoff 
570*b2d3d26fSGleb Smirnoff 	for (i = 0; i < RL_TIMEOUT; i++) {
571*b2d3d26fSGleb Smirnoff 		DELAY(10);
572*b2d3d26fSGleb Smirnoff 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
573*b2d3d26fSGleb Smirnoff 			break;
574*b2d3d26fSGleb Smirnoff 	}
575*b2d3d26fSGleb Smirnoff 	if (i == RL_TIMEOUT)
576*b2d3d26fSGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
577*b2d3d26fSGleb Smirnoff }
578*b2d3d26fSGleb Smirnoff 
579*b2d3d26fSGleb Smirnoff /*
580*b2d3d26fSGleb Smirnoff  * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device
581*b2d3d26fSGleb Smirnoff  * IDs against our list and return a device name if we find a match.
582*b2d3d26fSGleb Smirnoff  */
583*b2d3d26fSGleb Smirnoff static int
584*b2d3d26fSGleb Smirnoff rl_probe(device_t dev)
585*b2d3d26fSGleb Smirnoff {
586*b2d3d26fSGleb Smirnoff 	const struct rl_type	*t;
587*b2d3d26fSGleb Smirnoff 	uint16_t		devid, revid, vendor;
588*b2d3d26fSGleb Smirnoff 	int			i;
589*b2d3d26fSGleb Smirnoff 
590*b2d3d26fSGleb Smirnoff 	vendor = pci_get_vendor(dev);
591*b2d3d26fSGleb Smirnoff 	devid = pci_get_device(dev);
592*b2d3d26fSGleb Smirnoff 	revid = pci_get_revid(dev);
593*b2d3d26fSGleb Smirnoff 
594*b2d3d26fSGleb Smirnoff 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
595*b2d3d26fSGleb Smirnoff 		if (revid == 0x20) {
596*b2d3d26fSGleb Smirnoff 			/* 8139C+, let re(4) take care of this device. */
597*b2d3d26fSGleb Smirnoff 			return (ENXIO);
598*b2d3d26fSGleb Smirnoff 		}
599*b2d3d26fSGleb Smirnoff 	}
600*b2d3d26fSGleb Smirnoff 	t = rl_devs;
601*b2d3d26fSGleb Smirnoff 	for (i = 0; i < sizeof(rl_devs) / sizeof(rl_devs[0]); i++, t++) {
602*b2d3d26fSGleb Smirnoff 		if (vendor == t->rl_vid && devid == t->rl_did) {
603*b2d3d26fSGleb Smirnoff 			device_set_desc(dev, t->rl_name);
604*b2d3d26fSGleb Smirnoff 			return (BUS_PROBE_DEFAULT);
605*b2d3d26fSGleb Smirnoff 		}
606*b2d3d26fSGleb Smirnoff 	}
607*b2d3d26fSGleb Smirnoff 
608*b2d3d26fSGleb Smirnoff 	return (ENXIO);
609*b2d3d26fSGleb Smirnoff }
610*b2d3d26fSGleb Smirnoff 
611*b2d3d26fSGleb Smirnoff struct rl_dmamap_arg {
612*b2d3d26fSGleb Smirnoff 	bus_addr_t	rl_busaddr;
613*b2d3d26fSGleb Smirnoff };
614*b2d3d26fSGleb Smirnoff 
615*b2d3d26fSGleb Smirnoff static void
616*b2d3d26fSGleb Smirnoff rl_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
617*b2d3d26fSGleb Smirnoff {
618*b2d3d26fSGleb Smirnoff 	struct rl_dmamap_arg	*ctx;
619*b2d3d26fSGleb Smirnoff 
620*b2d3d26fSGleb Smirnoff 	if (error != 0)
621*b2d3d26fSGleb Smirnoff 		return;
622*b2d3d26fSGleb Smirnoff 
623*b2d3d26fSGleb Smirnoff 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
624*b2d3d26fSGleb Smirnoff 
625*b2d3d26fSGleb Smirnoff         ctx = (struct rl_dmamap_arg *)arg;
626*b2d3d26fSGleb Smirnoff         ctx->rl_busaddr = segs[0].ds_addr;
627*b2d3d26fSGleb Smirnoff }
628*b2d3d26fSGleb Smirnoff 
629*b2d3d26fSGleb Smirnoff /*
630*b2d3d26fSGleb Smirnoff  * Attach the interface. Allocate softc structures, do ifmedia
631*b2d3d26fSGleb Smirnoff  * setup and ethernet/BPF attach.
632*b2d3d26fSGleb Smirnoff  */
633*b2d3d26fSGleb Smirnoff static int
634*b2d3d26fSGleb Smirnoff rl_attach(device_t dev)
635*b2d3d26fSGleb Smirnoff {
636*b2d3d26fSGleb Smirnoff 	uint8_t			eaddr[ETHER_ADDR_LEN];
637*b2d3d26fSGleb Smirnoff 	uint16_t		as[3];
638*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
639*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
640*b2d3d26fSGleb Smirnoff 	const struct rl_type	*t;
641*b2d3d26fSGleb Smirnoff 	struct sysctl_ctx_list	*ctx;
642*b2d3d26fSGleb Smirnoff 	struct sysctl_oid_list	*children;
643*b2d3d26fSGleb Smirnoff 	int			error = 0, hwrev, i, phy, pmc, rid;
644*b2d3d26fSGleb Smirnoff 	int			prefer_iomap, unit;
645*b2d3d26fSGleb Smirnoff 	uint16_t		rl_did = 0;
646*b2d3d26fSGleb Smirnoff 	char			tn[32];
647*b2d3d26fSGleb Smirnoff 
648*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
649*b2d3d26fSGleb Smirnoff 	unit = device_get_unit(dev);
650*b2d3d26fSGleb Smirnoff 	sc->rl_dev = dev;
651*b2d3d26fSGleb Smirnoff 
652*b2d3d26fSGleb Smirnoff 	sc->rl_twister_enable = 0;
653*b2d3d26fSGleb Smirnoff 	snprintf(tn, sizeof(tn), "dev.rl.%d.twister_enable", unit);
654*b2d3d26fSGleb Smirnoff 	TUNABLE_INT_FETCH(tn, &sc->rl_twister_enable);
655*b2d3d26fSGleb Smirnoff 	ctx = device_get_sysctl_ctx(sc->rl_dev);
656*b2d3d26fSGleb Smirnoff 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
657*b2d3d26fSGleb Smirnoff 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "twister_enable", CTLFLAG_RD,
658*b2d3d26fSGleb Smirnoff 	   &sc->rl_twister_enable, 0, "");
659*b2d3d26fSGleb Smirnoff 
660*b2d3d26fSGleb Smirnoff 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
661*b2d3d26fSGleb Smirnoff 	    MTX_DEF);
662*b2d3d26fSGleb Smirnoff 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
663*b2d3d26fSGleb Smirnoff 
664*b2d3d26fSGleb Smirnoff 	pci_enable_busmaster(dev);
665*b2d3d26fSGleb Smirnoff 
666*b2d3d26fSGleb Smirnoff 
667*b2d3d26fSGleb Smirnoff 	/*
668*b2d3d26fSGleb Smirnoff 	 * Map control/status registers.
669*b2d3d26fSGleb Smirnoff 	 * Default to using PIO access for this driver. On SMP systems,
670*b2d3d26fSGleb Smirnoff 	 * there appear to be problems with memory mapped mode: it looks
671*b2d3d26fSGleb Smirnoff 	 * like doing too many memory mapped access back to back in rapid
672*b2d3d26fSGleb Smirnoff 	 * succession can hang the bus. I'm inclined to blame this on
673*b2d3d26fSGleb Smirnoff 	 * crummy design/construction on the part of RealTek. Memory
674*b2d3d26fSGleb Smirnoff 	 * mapped mode does appear to work on uniprocessor systems though.
675*b2d3d26fSGleb Smirnoff 	 */
676*b2d3d26fSGleb Smirnoff 	prefer_iomap = 1;
677*b2d3d26fSGleb Smirnoff 	snprintf(tn, sizeof(tn), "dev.rl.%d.prefer_iomap", unit);
678*b2d3d26fSGleb Smirnoff 	TUNABLE_INT_FETCH(tn, &prefer_iomap);
679*b2d3d26fSGleb Smirnoff 	if (prefer_iomap) {
680*b2d3d26fSGleb Smirnoff 		sc->rl_res_id = PCIR_BAR(0);
681*b2d3d26fSGleb Smirnoff 		sc->rl_res_type = SYS_RES_IOPORT;
682*b2d3d26fSGleb Smirnoff 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
683*b2d3d26fSGleb Smirnoff 		    &sc->rl_res_id, RF_ACTIVE);
684*b2d3d26fSGleb Smirnoff 	}
685*b2d3d26fSGleb Smirnoff 	if (prefer_iomap == 0 || sc->rl_res == NULL) {
686*b2d3d26fSGleb Smirnoff 		sc->rl_res_id = PCIR_BAR(1);
687*b2d3d26fSGleb Smirnoff 		sc->rl_res_type = SYS_RES_MEMORY;
688*b2d3d26fSGleb Smirnoff 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
689*b2d3d26fSGleb Smirnoff 		    &sc->rl_res_id, RF_ACTIVE);
690*b2d3d26fSGleb Smirnoff 	}
691*b2d3d26fSGleb Smirnoff 	if (sc->rl_res == NULL) {
692*b2d3d26fSGleb Smirnoff 		device_printf(dev, "couldn't map ports/memory\n");
693*b2d3d26fSGleb Smirnoff 		error = ENXIO;
694*b2d3d26fSGleb Smirnoff 		goto fail;
695*b2d3d26fSGleb Smirnoff 	}
696*b2d3d26fSGleb Smirnoff 
697*b2d3d26fSGleb Smirnoff #ifdef notdef
698*b2d3d26fSGleb Smirnoff 	/*
699*b2d3d26fSGleb Smirnoff 	 * Detect the Realtek 8139B. For some reason, this chip is very
700*b2d3d26fSGleb Smirnoff 	 * unstable when left to autoselect the media
701*b2d3d26fSGleb Smirnoff 	 * The best workaround is to set the device to the required
702*b2d3d26fSGleb Smirnoff 	 * media type or to set it to the 10 Meg speed.
703*b2d3d26fSGleb Smirnoff 	 */
704*b2d3d26fSGleb Smirnoff 	if ((rman_get_end(sc->rl_res) - rman_get_start(sc->rl_res)) == 0xFF)
705*b2d3d26fSGleb Smirnoff 		device_printf(dev,
706*b2d3d26fSGleb Smirnoff "Realtek 8139B detected. Warning, this may be unstable in autoselect mode\n");
707*b2d3d26fSGleb Smirnoff #endif
708*b2d3d26fSGleb Smirnoff 
709*b2d3d26fSGleb Smirnoff 	sc->rl_btag = rman_get_bustag(sc->rl_res);
710*b2d3d26fSGleb Smirnoff 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
711*b2d3d26fSGleb Smirnoff 
712*b2d3d26fSGleb Smirnoff 	/* Allocate interrupt */
713*b2d3d26fSGleb Smirnoff 	rid = 0;
714*b2d3d26fSGleb Smirnoff 	sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
715*b2d3d26fSGleb Smirnoff 	    RF_SHAREABLE | RF_ACTIVE);
716*b2d3d26fSGleb Smirnoff 
717*b2d3d26fSGleb Smirnoff 	if (sc->rl_irq[0] == NULL) {
718*b2d3d26fSGleb Smirnoff 		device_printf(dev, "couldn't map interrupt\n");
719*b2d3d26fSGleb Smirnoff 		error = ENXIO;
720*b2d3d26fSGleb Smirnoff 		goto fail;
721*b2d3d26fSGleb Smirnoff 	}
722*b2d3d26fSGleb Smirnoff 
723*b2d3d26fSGleb Smirnoff 	sc->rl_cfg0 = RL_8139_CFG0;
724*b2d3d26fSGleb Smirnoff 	sc->rl_cfg1 = RL_8139_CFG1;
725*b2d3d26fSGleb Smirnoff 	sc->rl_cfg2 = 0;
726*b2d3d26fSGleb Smirnoff 	sc->rl_cfg3 = RL_8139_CFG3;
727*b2d3d26fSGleb Smirnoff 	sc->rl_cfg4 = RL_8139_CFG4;
728*b2d3d26fSGleb Smirnoff 	sc->rl_cfg5 = RL_8139_CFG5;
729*b2d3d26fSGleb Smirnoff 
730*b2d3d26fSGleb Smirnoff 	/*
731*b2d3d26fSGleb Smirnoff 	 * Reset the adapter. Only take the lock here as it's needed in
732*b2d3d26fSGleb Smirnoff 	 * order to call rl_reset().
733*b2d3d26fSGleb Smirnoff 	 */
734*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
735*b2d3d26fSGleb Smirnoff 	rl_reset(sc);
736*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
737*b2d3d26fSGleb Smirnoff 
738*b2d3d26fSGleb Smirnoff 	sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
739*b2d3d26fSGleb Smirnoff 	rl_read_eeprom(sc, (uint8_t *)&rl_did, 0, 1, 0);
740*b2d3d26fSGleb Smirnoff 	if (rl_did != 0x8129)
741*b2d3d26fSGleb Smirnoff 		sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
742*b2d3d26fSGleb Smirnoff 
743*b2d3d26fSGleb Smirnoff 	/*
744*b2d3d26fSGleb Smirnoff 	 * Get station address from the EEPROM.
745*b2d3d26fSGleb Smirnoff 	 */
746*b2d3d26fSGleb Smirnoff 	rl_read_eeprom(sc, (uint8_t *)as, RL_EE_EADDR, 3, 0);
747*b2d3d26fSGleb Smirnoff 	for (i = 0; i < 3; i++) {
748*b2d3d26fSGleb Smirnoff 		eaddr[(i * 2) + 0] = as[i] & 0xff;
749*b2d3d26fSGleb Smirnoff 		eaddr[(i * 2) + 1] = as[i] >> 8;
750*b2d3d26fSGleb Smirnoff 	}
751*b2d3d26fSGleb Smirnoff 
752*b2d3d26fSGleb Smirnoff 	/*
753*b2d3d26fSGleb Smirnoff 	 * Now read the exact device type from the EEPROM to find
754*b2d3d26fSGleb Smirnoff 	 * out if it's an 8129 or 8139.
755*b2d3d26fSGleb Smirnoff 	 */
756*b2d3d26fSGleb Smirnoff 	rl_read_eeprom(sc, (uint8_t *)&rl_did, RL_EE_PCI_DID, 1, 0);
757*b2d3d26fSGleb Smirnoff 
758*b2d3d26fSGleb Smirnoff 	t = rl_devs;
759*b2d3d26fSGleb Smirnoff 	sc->rl_type = 0;
760*b2d3d26fSGleb Smirnoff 	while(t->rl_name != NULL) {
761*b2d3d26fSGleb Smirnoff 		if (rl_did == t->rl_did) {
762*b2d3d26fSGleb Smirnoff 			sc->rl_type = t->rl_basetype;
763*b2d3d26fSGleb Smirnoff 			break;
764*b2d3d26fSGleb Smirnoff 		}
765*b2d3d26fSGleb Smirnoff 		t++;
766*b2d3d26fSGleb Smirnoff 	}
767*b2d3d26fSGleb Smirnoff 
768*b2d3d26fSGleb Smirnoff 	if (sc->rl_type == 0) {
769*b2d3d26fSGleb Smirnoff 		device_printf(dev, "unknown device ID: %x assuming 8139\n",
770*b2d3d26fSGleb Smirnoff 		    rl_did);
771*b2d3d26fSGleb Smirnoff 		sc->rl_type = RL_8139;
772*b2d3d26fSGleb Smirnoff 		/*
773*b2d3d26fSGleb Smirnoff 		 * Read RL_IDR register to get ethernet address as accessing
774*b2d3d26fSGleb Smirnoff 		 * EEPROM may not extract correct address.
775*b2d3d26fSGleb Smirnoff 		 */
776*b2d3d26fSGleb Smirnoff 		for (i = 0; i < ETHER_ADDR_LEN; i++)
777*b2d3d26fSGleb Smirnoff 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
778*b2d3d26fSGleb Smirnoff 	}
779*b2d3d26fSGleb Smirnoff 
780*b2d3d26fSGleb Smirnoff 	if ((error = rl_dma_alloc(sc)) != 0)
781*b2d3d26fSGleb Smirnoff 		goto fail;
782*b2d3d26fSGleb Smirnoff 
783*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
784*b2d3d26fSGleb Smirnoff 	if (ifp == NULL) {
785*b2d3d26fSGleb Smirnoff 		device_printf(dev, "can not if_alloc()\n");
786*b2d3d26fSGleb Smirnoff 		error = ENOSPC;
787*b2d3d26fSGleb Smirnoff 		goto fail;
788*b2d3d26fSGleb Smirnoff 	}
789*b2d3d26fSGleb Smirnoff 
790*b2d3d26fSGleb Smirnoff #define	RL_PHYAD_INTERNAL	0
791*b2d3d26fSGleb Smirnoff 
792*b2d3d26fSGleb Smirnoff 	/* Do MII setup */
793*b2d3d26fSGleb Smirnoff 	phy = MII_PHY_ANY;
794*b2d3d26fSGleb Smirnoff 	if (sc->rl_type == RL_8139)
795*b2d3d26fSGleb Smirnoff 		phy = RL_PHYAD_INTERNAL;
796*b2d3d26fSGleb Smirnoff 	error = mii_attach(dev, &sc->rl_miibus, ifp, rl_ifmedia_upd,
797*b2d3d26fSGleb Smirnoff 	    rl_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
798*b2d3d26fSGleb Smirnoff 	if (error != 0) {
799*b2d3d26fSGleb Smirnoff 		device_printf(dev, "attaching PHYs failed\n");
800*b2d3d26fSGleb Smirnoff 		goto fail;
801*b2d3d26fSGleb Smirnoff 	}
802*b2d3d26fSGleb Smirnoff 
803*b2d3d26fSGleb Smirnoff 	ifp->if_softc = sc;
804*b2d3d26fSGleb Smirnoff 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
805*b2d3d26fSGleb Smirnoff 	ifp->if_mtu = ETHERMTU;
806*b2d3d26fSGleb Smirnoff 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
807*b2d3d26fSGleb Smirnoff 	ifp->if_ioctl = rl_ioctl;
808*b2d3d26fSGleb Smirnoff 	ifp->if_start = rl_start;
809*b2d3d26fSGleb Smirnoff 	ifp->if_init = rl_init;
810*b2d3d26fSGleb Smirnoff 	ifp->if_capabilities = IFCAP_VLAN_MTU;
811*b2d3d26fSGleb Smirnoff 	/* Check WOL for RTL8139B or newer controllers. */
812*b2d3d26fSGleb Smirnoff 	if (sc->rl_type == RL_8139 &&
813*b2d3d26fSGleb Smirnoff 	    pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) == 0) {
814*b2d3d26fSGleb Smirnoff 		hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
815*b2d3d26fSGleb Smirnoff 		switch (hwrev) {
816*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8139B:
817*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8130:
818*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8139C:
819*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8139D:
820*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8101:
821*b2d3d26fSGleb Smirnoff 		case RL_HWREV_8100:
822*b2d3d26fSGleb Smirnoff 			ifp->if_capabilities |= IFCAP_WOL;
823*b2d3d26fSGleb Smirnoff 			/* Disable WOL. */
824*b2d3d26fSGleb Smirnoff 			rl_clrwol(sc);
825*b2d3d26fSGleb Smirnoff 			break;
826*b2d3d26fSGleb Smirnoff 		default:
827*b2d3d26fSGleb Smirnoff 			break;
828*b2d3d26fSGleb Smirnoff 		}
829*b2d3d26fSGleb Smirnoff 	}
830*b2d3d26fSGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
831*b2d3d26fSGleb Smirnoff 	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
832*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
833*b2d3d26fSGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
834*b2d3d26fSGleb Smirnoff #endif
835*b2d3d26fSGleb Smirnoff 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
836*b2d3d26fSGleb Smirnoff 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
837*b2d3d26fSGleb Smirnoff 	IFQ_SET_READY(&ifp->if_snd);
838*b2d3d26fSGleb Smirnoff 
839*b2d3d26fSGleb Smirnoff 	/*
840*b2d3d26fSGleb Smirnoff 	 * Call MI attach routine.
841*b2d3d26fSGleb Smirnoff 	 */
842*b2d3d26fSGleb Smirnoff 	ether_ifattach(ifp, eaddr);
843*b2d3d26fSGleb Smirnoff 
844*b2d3d26fSGleb Smirnoff 	/* Hook interrupt last to avoid having to lock softc */
845*b2d3d26fSGleb Smirnoff 	error = bus_setup_intr(dev, sc->rl_irq[0], INTR_TYPE_NET | INTR_MPSAFE,
846*b2d3d26fSGleb Smirnoff 	    NULL, rl_intr, sc, &sc->rl_intrhand[0]);
847*b2d3d26fSGleb Smirnoff 	if (error) {
848*b2d3d26fSGleb Smirnoff 		device_printf(sc->rl_dev, "couldn't set up irq\n");
849*b2d3d26fSGleb Smirnoff 		ether_ifdetach(ifp);
850*b2d3d26fSGleb Smirnoff 	}
851*b2d3d26fSGleb Smirnoff 
852*b2d3d26fSGleb Smirnoff fail:
853*b2d3d26fSGleb Smirnoff 	if (error)
854*b2d3d26fSGleb Smirnoff 		rl_detach(dev);
855*b2d3d26fSGleb Smirnoff 
856*b2d3d26fSGleb Smirnoff 	return (error);
857*b2d3d26fSGleb Smirnoff }
858*b2d3d26fSGleb Smirnoff 
859*b2d3d26fSGleb Smirnoff /*
860*b2d3d26fSGleb Smirnoff  * Shutdown hardware and free up resources. This can be called any
861*b2d3d26fSGleb Smirnoff  * time after the mutex has been initialized. It is called in both
862*b2d3d26fSGleb Smirnoff  * the error case in attach and the normal detach case so it needs
863*b2d3d26fSGleb Smirnoff  * to be careful about only freeing resources that have actually been
864*b2d3d26fSGleb Smirnoff  * allocated.
865*b2d3d26fSGleb Smirnoff  */
866*b2d3d26fSGleb Smirnoff static int
867*b2d3d26fSGleb Smirnoff rl_detach(device_t dev)
868*b2d3d26fSGleb Smirnoff {
869*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
870*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
871*b2d3d26fSGleb Smirnoff 
872*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
873*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp;
874*b2d3d26fSGleb Smirnoff 
875*b2d3d26fSGleb Smirnoff 	KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized"));
876*b2d3d26fSGleb Smirnoff 
877*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
878*b2d3d26fSGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
879*b2d3d26fSGleb Smirnoff 		ether_poll_deregister(ifp);
880*b2d3d26fSGleb Smirnoff #endif
881*b2d3d26fSGleb Smirnoff 	/* These should only be active if attach succeeded */
882*b2d3d26fSGleb Smirnoff 	if (device_is_attached(dev)) {
883*b2d3d26fSGleb Smirnoff 		RL_LOCK(sc);
884*b2d3d26fSGleb Smirnoff 		rl_stop(sc);
885*b2d3d26fSGleb Smirnoff 		RL_UNLOCK(sc);
886*b2d3d26fSGleb Smirnoff 		callout_drain(&sc->rl_stat_callout);
887*b2d3d26fSGleb Smirnoff 		ether_ifdetach(ifp);
888*b2d3d26fSGleb Smirnoff 	}
889*b2d3d26fSGleb Smirnoff #if 0
890*b2d3d26fSGleb Smirnoff 	sc->suspended = 1;
891*b2d3d26fSGleb Smirnoff #endif
892*b2d3d26fSGleb Smirnoff 	if (sc->rl_miibus)
893*b2d3d26fSGleb Smirnoff 		device_delete_child(dev, sc->rl_miibus);
894*b2d3d26fSGleb Smirnoff 	bus_generic_detach(dev);
895*b2d3d26fSGleb Smirnoff 
896*b2d3d26fSGleb Smirnoff 	if (sc->rl_intrhand[0])
897*b2d3d26fSGleb Smirnoff 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
898*b2d3d26fSGleb Smirnoff 	if (sc->rl_irq[0])
899*b2d3d26fSGleb Smirnoff 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq[0]);
900*b2d3d26fSGleb Smirnoff 	if (sc->rl_res)
901*b2d3d26fSGleb Smirnoff 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
902*b2d3d26fSGleb Smirnoff 		    sc->rl_res);
903*b2d3d26fSGleb Smirnoff 
904*b2d3d26fSGleb Smirnoff 	if (ifp)
905*b2d3d26fSGleb Smirnoff 		if_free(ifp);
906*b2d3d26fSGleb Smirnoff 
907*b2d3d26fSGleb Smirnoff 	rl_dma_free(sc);
908*b2d3d26fSGleb Smirnoff 
909*b2d3d26fSGleb Smirnoff 	mtx_destroy(&sc->rl_mtx);
910*b2d3d26fSGleb Smirnoff 
911*b2d3d26fSGleb Smirnoff 	return (0);
912*b2d3d26fSGleb Smirnoff }
913*b2d3d26fSGleb Smirnoff 
914*b2d3d26fSGleb Smirnoff static int
915*b2d3d26fSGleb Smirnoff rl_dma_alloc(struct rl_softc *sc)
916*b2d3d26fSGleb Smirnoff {
917*b2d3d26fSGleb Smirnoff 	struct rl_dmamap_arg	ctx;
918*b2d3d26fSGleb Smirnoff 	int			error, i;
919*b2d3d26fSGleb Smirnoff 
920*b2d3d26fSGleb Smirnoff 	/*
921*b2d3d26fSGleb Smirnoff 	 * Allocate the parent bus DMA tag appropriate for PCI.
922*b2d3d26fSGleb Smirnoff 	 */
923*b2d3d26fSGleb Smirnoff 	error = bus_dma_tag_create(bus_get_dma_tag(sc->rl_dev),	/* parent */
924*b2d3d26fSGleb Smirnoff 	    1, 0,			/* alignment, boundary */
925*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
926*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR,		/* highaddr */
927*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* filter, filterarg */
928*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXSIZE_32BIT, 0,	/* maxsize, nsegments */
929*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
930*b2d3d26fSGleb Smirnoff 	    0,				/* flags */
931*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* lockfunc, lockarg */
932*b2d3d26fSGleb Smirnoff 	    &sc->rl_parent_tag);
933*b2d3d26fSGleb Smirnoff 	if (error) {
934*b2d3d26fSGleb Smirnoff                 device_printf(sc->rl_dev,
935*b2d3d26fSGleb Smirnoff 		    "failed to create parent DMA tag.\n");
936*b2d3d26fSGleb Smirnoff 		goto fail;
937*b2d3d26fSGleb Smirnoff 	}
938*b2d3d26fSGleb Smirnoff 	/* Create DMA tag for Rx memory block. */
939*b2d3d26fSGleb Smirnoff 	error = bus_dma_tag_create(sc->rl_parent_tag,	/* parent */
940*b2d3d26fSGleb Smirnoff 	    RL_RX_8139_BUF_ALIGN, 0,	/* alignment, boundary */
941*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR,		/* lowaddr */
942*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR,		/* highaddr */
943*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* filter, filterarg */
944*b2d3d26fSGleb Smirnoff 	    RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ, 1,	/* maxsize,nsegments */
945*b2d3d26fSGleb Smirnoff 	    RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ,	/* maxsegsize */
946*b2d3d26fSGleb Smirnoff 	    0,				/* flags */
947*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* lockfunc, lockarg */
948*b2d3d26fSGleb Smirnoff 	    &sc->rl_cdata.rl_rx_tag);
949*b2d3d26fSGleb Smirnoff 	if (error) {
950*b2d3d26fSGleb Smirnoff                 device_printf(sc->rl_dev,
951*b2d3d26fSGleb Smirnoff 		    "failed to create Rx memory block DMA tag.\n");
952*b2d3d26fSGleb Smirnoff 		goto fail;
953*b2d3d26fSGleb Smirnoff 	}
954*b2d3d26fSGleb Smirnoff 	/* Create DMA tag for Tx buffer. */
955*b2d3d26fSGleb Smirnoff 	error = bus_dma_tag_create(sc->rl_parent_tag,	/* parent */
956*b2d3d26fSGleb Smirnoff 	    RL_TX_8139_BUF_ALIGN, 0,	/* alignment, boundary */
957*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR,		/* lowaddr */
958*b2d3d26fSGleb Smirnoff 	    BUS_SPACE_MAXADDR,		/* highaddr */
959*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* filter, filterarg */
960*b2d3d26fSGleb Smirnoff 	    MCLBYTES, 1,		/* maxsize, nsegments */
961*b2d3d26fSGleb Smirnoff 	    MCLBYTES,			/* maxsegsize */
962*b2d3d26fSGleb Smirnoff 	    0,				/* flags */
963*b2d3d26fSGleb Smirnoff 	    NULL, NULL,			/* lockfunc, lockarg */
964*b2d3d26fSGleb Smirnoff 	    &sc->rl_cdata.rl_tx_tag);
965*b2d3d26fSGleb Smirnoff 	if (error) {
966*b2d3d26fSGleb Smirnoff                 device_printf(sc->rl_dev, "failed to create Tx DMA tag.\n");
967*b2d3d26fSGleb Smirnoff 		goto fail;
968*b2d3d26fSGleb Smirnoff 	}
969*b2d3d26fSGleb Smirnoff 
970*b2d3d26fSGleb Smirnoff 	/*
971*b2d3d26fSGleb Smirnoff 	 * Allocate DMA'able memory and load DMA map for Rx memory block.
972*b2d3d26fSGleb Smirnoff 	 */
973*b2d3d26fSGleb Smirnoff 	error = bus_dmamem_alloc(sc->rl_cdata.rl_rx_tag,
974*b2d3d26fSGleb Smirnoff 	    (void **)&sc->rl_cdata.rl_rx_buf, BUS_DMA_WAITOK |
975*b2d3d26fSGleb Smirnoff 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->rl_cdata.rl_rx_dmamap);
976*b2d3d26fSGleb Smirnoff 	if (error != 0) {
977*b2d3d26fSGleb Smirnoff 		device_printf(sc->rl_dev,
978*b2d3d26fSGleb Smirnoff 		    "failed to allocate Rx DMA memory block.\n");
979*b2d3d26fSGleb Smirnoff 		goto fail;
980*b2d3d26fSGleb Smirnoff 	}
981*b2d3d26fSGleb Smirnoff 	ctx.rl_busaddr = 0;
982*b2d3d26fSGleb Smirnoff 	error = bus_dmamap_load(sc->rl_cdata.rl_rx_tag,
983*b2d3d26fSGleb Smirnoff 	    sc->rl_cdata.rl_rx_dmamap, sc->rl_cdata.rl_rx_buf,
984*b2d3d26fSGleb Smirnoff 	    RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ, rl_dmamap_cb, &ctx,
985*b2d3d26fSGleb Smirnoff 	    BUS_DMA_NOWAIT);
986*b2d3d26fSGleb Smirnoff 	if (error != 0 || ctx.rl_busaddr == 0) {
987*b2d3d26fSGleb Smirnoff 		device_printf(sc->rl_dev,
988*b2d3d26fSGleb Smirnoff 		    "could not load Rx DMA memory block.\n");
989*b2d3d26fSGleb Smirnoff 		goto fail;
990*b2d3d26fSGleb Smirnoff 	}
991*b2d3d26fSGleb Smirnoff 	sc->rl_cdata.rl_rx_buf_paddr = ctx.rl_busaddr;
992*b2d3d26fSGleb Smirnoff 
993*b2d3d26fSGleb Smirnoff 	/* Create DMA maps for Tx buffers. */
994*b2d3d26fSGleb Smirnoff 	for (i = 0; i < RL_TX_LIST_CNT; i++) {
995*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_tx_chain[i] = NULL;
996*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_tx_dmamap[i] = NULL;
997*b2d3d26fSGleb Smirnoff 		error = bus_dmamap_create(sc->rl_cdata.rl_tx_tag, 0,
998*b2d3d26fSGleb Smirnoff 		    &sc->rl_cdata.rl_tx_dmamap[i]);
999*b2d3d26fSGleb Smirnoff 		if (error != 0) {
1000*b2d3d26fSGleb Smirnoff 			device_printf(sc->rl_dev,
1001*b2d3d26fSGleb Smirnoff 			    "could not create Tx dmamap.\n");
1002*b2d3d26fSGleb Smirnoff 			goto fail;
1003*b2d3d26fSGleb Smirnoff 		}
1004*b2d3d26fSGleb Smirnoff 	}
1005*b2d3d26fSGleb Smirnoff 
1006*b2d3d26fSGleb Smirnoff 	/* Leave a few bytes before the start of the RX ring buffer. */
1007*b2d3d26fSGleb Smirnoff 	sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
1008*b2d3d26fSGleb Smirnoff 	sc->rl_cdata.rl_rx_buf += RL_RX_8139_BUF_RESERVE;
1009*b2d3d26fSGleb Smirnoff 
1010*b2d3d26fSGleb Smirnoff fail:
1011*b2d3d26fSGleb Smirnoff 	return (error);
1012*b2d3d26fSGleb Smirnoff }
1013*b2d3d26fSGleb Smirnoff 
1014*b2d3d26fSGleb Smirnoff static void
1015*b2d3d26fSGleb Smirnoff rl_dma_free(struct rl_softc *sc)
1016*b2d3d26fSGleb Smirnoff {
1017*b2d3d26fSGleb Smirnoff 	int			i;
1018*b2d3d26fSGleb Smirnoff 
1019*b2d3d26fSGleb Smirnoff 	/* Rx memory block. */
1020*b2d3d26fSGleb Smirnoff 	if (sc->rl_cdata.rl_rx_tag != NULL) {
1021*b2d3d26fSGleb Smirnoff 		if (sc->rl_cdata.rl_rx_buf_paddr != 0)
1022*b2d3d26fSGleb Smirnoff 			bus_dmamap_unload(sc->rl_cdata.rl_rx_tag,
1023*b2d3d26fSGleb Smirnoff 			    sc->rl_cdata.rl_rx_dmamap);
1024*b2d3d26fSGleb Smirnoff 		if (sc->rl_cdata.rl_rx_buf_ptr != NULL)
1025*b2d3d26fSGleb Smirnoff 			bus_dmamem_free(sc->rl_cdata.rl_rx_tag,
1026*b2d3d26fSGleb Smirnoff 			    sc->rl_cdata.rl_rx_buf_ptr,
1027*b2d3d26fSGleb Smirnoff 			    sc->rl_cdata.rl_rx_dmamap);
1028*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_rx_buf_ptr = NULL;
1029*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_rx_buf = NULL;
1030*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_rx_buf_paddr = 0;
1031*b2d3d26fSGleb Smirnoff 		bus_dma_tag_destroy(sc->rl_cdata.rl_rx_tag);
1032*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_tx_tag = NULL;
1033*b2d3d26fSGleb Smirnoff 	}
1034*b2d3d26fSGleb Smirnoff 
1035*b2d3d26fSGleb Smirnoff 	/* Tx buffers. */
1036*b2d3d26fSGleb Smirnoff 	if (sc->rl_cdata.rl_tx_tag != NULL) {
1037*b2d3d26fSGleb Smirnoff 		for (i = 0; i < RL_TX_LIST_CNT; i++) {
1038*b2d3d26fSGleb Smirnoff 			if (sc->rl_cdata.rl_tx_dmamap[i] != NULL) {
1039*b2d3d26fSGleb Smirnoff 				bus_dmamap_destroy(
1040*b2d3d26fSGleb Smirnoff 				    sc->rl_cdata.rl_tx_tag,
1041*b2d3d26fSGleb Smirnoff 				    sc->rl_cdata.rl_tx_dmamap[i]);
1042*b2d3d26fSGleb Smirnoff 				sc->rl_cdata.rl_tx_dmamap[i] = NULL;
1043*b2d3d26fSGleb Smirnoff 			}
1044*b2d3d26fSGleb Smirnoff 		}
1045*b2d3d26fSGleb Smirnoff 		bus_dma_tag_destroy(sc->rl_cdata.rl_tx_tag);
1046*b2d3d26fSGleb Smirnoff 		sc->rl_cdata.rl_tx_tag = NULL;
1047*b2d3d26fSGleb Smirnoff 	}
1048*b2d3d26fSGleb Smirnoff 
1049*b2d3d26fSGleb Smirnoff 	if (sc->rl_parent_tag != NULL) {
1050*b2d3d26fSGleb Smirnoff 		bus_dma_tag_destroy(sc->rl_parent_tag);
1051*b2d3d26fSGleb Smirnoff 		sc->rl_parent_tag = NULL;
1052*b2d3d26fSGleb Smirnoff 	}
1053*b2d3d26fSGleb Smirnoff }
1054*b2d3d26fSGleb Smirnoff 
1055*b2d3d26fSGleb Smirnoff /*
1056*b2d3d26fSGleb Smirnoff  * Initialize the transmit descriptors.
1057*b2d3d26fSGleb Smirnoff  */
1058*b2d3d26fSGleb Smirnoff static int
1059*b2d3d26fSGleb Smirnoff rl_list_tx_init(struct rl_softc *sc)
1060*b2d3d26fSGleb Smirnoff {
1061*b2d3d26fSGleb Smirnoff 	struct rl_chain_data	*cd;
1062*b2d3d26fSGleb Smirnoff 	int			i;
1063*b2d3d26fSGleb Smirnoff 
1064*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1065*b2d3d26fSGleb Smirnoff 
1066*b2d3d26fSGleb Smirnoff 	cd = &sc->rl_cdata;
1067*b2d3d26fSGleb Smirnoff 	for (i = 0; i < RL_TX_LIST_CNT; i++) {
1068*b2d3d26fSGleb Smirnoff 		cd->rl_tx_chain[i] = NULL;
1069*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc,
1070*b2d3d26fSGleb Smirnoff 		    RL_TXADDR0 + (i * sizeof(uint32_t)), 0x0000000);
1071*b2d3d26fSGleb Smirnoff 	}
1072*b2d3d26fSGleb Smirnoff 
1073*b2d3d26fSGleb Smirnoff 	sc->rl_cdata.cur_tx = 0;
1074*b2d3d26fSGleb Smirnoff 	sc->rl_cdata.last_tx = 0;
1075*b2d3d26fSGleb Smirnoff 
1076*b2d3d26fSGleb Smirnoff 	return (0);
1077*b2d3d26fSGleb Smirnoff }
1078*b2d3d26fSGleb Smirnoff 
1079*b2d3d26fSGleb Smirnoff static int
1080*b2d3d26fSGleb Smirnoff rl_list_rx_init(struct rl_softc *sc)
1081*b2d3d26fSGleb Smirnoff {
1082*b2d3d26fSGleb Smirnoff 
1083*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1084*b2d3d26fSGleb Smirnoff 
1085*b2d3d26fSGleb Smirnoff 	bzero(sc->rl_cdata.rl_rx_buf_ptr,
1086*b2d3d26fSGleb Smirnoff 	    RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ);
1087*b2d3d26fSGleb Smirnoff 	bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, sc->rl_cdata.rl_rx_dmamap,
1088*b2d3d26fSGleb Smirnoff 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1089*b2d3d26fSGleb Smirnoff 
1090*b2d3d26fSGleb Smirnoff 	return (0);
1091*b2d3d26fSGleb Smirnoff }
1092*b2d3d26fSGleb Smirnoff 
1093*b2d3d26fSGleb Smirnoff /*
1094*b2d3d26fSGleb Smirnoff  * A frame has been uploaded: pass the resulting mbuf chain up to
1095*b2d3d26fSGleb Smirnoff  * the higher level protocols.
1096*b2d3d26fSGleb Smirnoff  *
1097*b2d3d26fSGleb Smirnoff  * You know there's something wrong with a PCI bus-master chip design
1098*b2d3d26fSGleb Smirnoff  * when you have to use m_devget().
1099*b2d3d26fSGleb Smirnoff  *
1100*b2d3d26fSGleb Smirnoff  * The receive operation is badly documented in the datasheet, so I'll
1101*b2d3d26fSGleb Smirnoff  * attempt to document it here. The driver provides a buffer area and
1102*b2d3d26fSGleb Smirnoff  * places its base address in the RX buffer start address register.
1103*b2d3d26fSGleb Smirnoff  * The chip then begins copying frames into the RX buffer. Each frame
1104*b2d3d26fSGleb Smirnoff  * is preceded by a 32-bit RX status word which specifies the length
1105*b2d3d26fSGleb Smirnoff  * of the frame and certain other status bits. Each frame (starting with
1106*b2d3d26fSGleb Smirnoff  * the status word) is also 32-bit aligned. The frame length is in the
1107*b2d3d26fSGleb Smirnoff  * first 16 bits of the status word; the lower 15 bits correspond with
1108*b2d3d26fSGleb Smirnoff  * the 'rx status register' mentioned in the datasheet.
1109*b2d3d26fSGleb Smirnoff  *
1110*b2d3d26fSGleb Smirnoff  * Note: to make the Alpha happy, the frame payload needs to be aligned
1111*b2d3d26fSGleb Smirnoff  * on a 32-bit boundary. To achieve this, we pass RL_ETHER_ALIGN (2 bytes)
1112*b2d3d26fSGleb Smirnoff  * as the offset argument to m_devget().
1113*b2d3d26fSGleb Smirnoff  */
1114*b2d3d26fSGleb Smirnoff static int
1115*b2d3d26fSGleb Smirnoff rl_rxeof(struct rl_softc *sc)
1116*b2d3d26fSGleb Smirnoff {
1117*b2d3d26fSGleb Smirnoff 	struct mbuf		*m;
1118*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
1119*b2d3d26fSGleb Smirnoff 	uint8_t			*rxbufpos;
1120*b2d3d26fSGleb Smirnoff 	int			total_len = 0;
1121*b2d3d26fSGleb Smirnoff 	int			wrap = 0;
1122*b2d3d26fSGleb Smirnoff 	int			rx_npkts = 0;
1123*b2d3d26fSGleb Smirnoff 	uint32_t		rxstat;
1124*b2d3d26fSGleb Smirnoff 	uint16_t		cur_rx;
1125*b2d3d26fSGleb Smirnoff 	uint16_t		limit;
1126*b2d3d26fSGleb Smirnoff 	uint16_t		max_bytes, rx_bytes = 0;
1127*b2d3d26fSGleb Smirnoff 
1128*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1129*b2d3d26fSGleb Smirnoff 
1130*b2d3d26fSGleb Smirnoff 	bus_dmamap_sync(sc->rl_cdata.rl_rx_tag, sc->rl_cdata.rl_rx_dmamap,
1131*b2d3d26fSGleb Smirnoff 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1132*b2d3d26fSGleb Smirnoff 
1133*b2d3d26fSGleb Smirnoff 	cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
1134*b2d3d26fSGleb Smirnoff 
1135*b2d3d26fSGleb Smirnoff 	/* Do not try to read past this point. */
1136*b2d3d26fSGleb Smirnoff 	limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
1137*b2d3d26fSGleb Smirnoff 
1138*b2d3d26fSGleb Smirnoff 	if (limit < cur_rx)
1139*b2d3d26fSGleb Smirnoff 		max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
1140*b2d3d26fSGleb Smirnoff 	else
1141*b2d3d26fSGleb Smirnoff 		max_bytes = limit - cur_rx;
1142*b2d3d26fSGleb Smirnoff 
1143*b2d3d26fSGleb Smirnoff 	while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
1144*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
1145*b2d3d26fSGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
1146*b2d3d26fSGleb Smirnoff 			if (sc->rxcycles <= 0)
1147*b2d3d26fSGleb Smirnoff 				break;
1148*b2d3d26fSGleb Smirnoff 			sc->rxcycles--;
1149*b2d3d26fSGleb Smirnoff 		}
1150*b2d3d26fSGleb Smirnoff #endif
1151*b2d3d26fSGleb Smirnoff 		rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
1152*b2d3d26fSGleb Smirnoff 		rxstat = le32toh(*(uint32_t *)rxbufpos);
1153*b2d3d26fSGleb Smirnoff 
1154*b2d3d26fSGleb Smirnoff 		/*
1155*b2d3d26fSGleb Smirnoff 		 * Here's a totally undocumented fact for you. When the
1156*b2d3d26fSGleb Smirnoff 		 * RealTek chip is in the process of copying a packet into
1157*b2d3d26fSGleb Smirnoff 		 * RAM for you, the length will be 0xfff0. If you spot a
1158*b2d3d26fSGleb Smirnoff 		 * packet header with this value, you need to stop. The
1159*b2d3d26fSGleb Smirnoff 		 * datasheet makes absolutely no mention of this and
1160*b2d3d26fSGleb Smirnoff 		 * RealTek should be shot for this.
1161*b2d3d26fSGleb Smirnoff 		 */
1162*b2d3d26fSGleb Smirnoff 		total_len = rxstat >> 16;
1163*b2d3d26fSGleb Smirnoff 		if (total_len == RL_RXSTAT_UNFINISHED)
1164*b2d3d26fSGleb Smirnoff 			break;
1165*b2d3d26fSGleb Smirnoff 
1166*b2d3d26fSGleb Smirnoff 		if (!(rxstat & RL_RXSTAT_RXOK) ||
1167*b2d3d26fSGleb Smirnoff 		    total_len < ETHER_MIN_LEN ||
1168*b2d3d26fSGleb Smirnoff 		    total_len > ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) {
1169*b2d3d26fSGleb Smirnoff 			ifp->if_ierrors++;
1170*b2d3d26fSGleb Smirnoff 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1171*b2d3d26fSGleb Smirnoff 			rl_init_locked(sc);
1172*b2d3d26fSGleb Smirnoff 			return (rx_npkts);
1173*b2d3d26fSGleb Smirnoff 		}
1174*b2d3d26fSGleb Smirnoff 
1175*b2d3d26fSGleb Smirnoff 		/* No errors; receive the packet. */
1176*b2d3d26fSGleb Smirnoff 		rx_bytes += total_len + 4;
1177*b2d3d26fSGleb Smirnoff 
1178*b2d3d26fSGleb Smirnoff 		/*
1179*b2d3d26fSGleb Smirnoff 		 * XXX The RealTek chip includes the CRC with every
1180*b2d3d26fSGleb Smirnoff 		 * received frame, and there's no way to turn this
1181*b2d3d26fSGleb Smirnoff 		 * behavior off (at least, I can't find anything in
1182*b2d3d26fSGleb Smirnoff 		 * the manual that explains how to do it) so we have
1183*b2d3d26fSGleb Smirnoff 		 * to trim off the CRC manually.
1184*b2d3d26fSGleb Smirnoff 		 */
1185*b2d3d26fSGleb Smirnoff 		total_len -= ETHER_CRC_LEN;
1186*b2d3d26fSGleb Smirnoff 
1187*b2d3d26fSGleb Smirnoff 		/*
1188*b2d3d26fSGleb Smirnoff 		 * Avoid trying to read more bytes than we know
1189*b2d3d26fSGleb Smirnoff 		 * the chip has prepared for us.
1190*b2d3d26fSGleb Smirnoff 		 */
1191*b2d3d26fSGleb Smirnoff 		if (rx_bytes > max_bytes)
1192*b2d3d26fSGleb Smirnoff 			break;
1193*b2d3d26fSGleb Smirnoff 
1194*b2d3d26fSGleb Smirnoff 		rxbufpos = sc->rl_cdata.rl_rx_buf +
1195*b2d3d26fSGleb Smirnoff 			((cur_rx + sizeof(uint32_t)) % RL_RXBUFLEN);
1196*b2d3d26fSGleb Smirnoff 		if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
1197*b2d3d26fSGleb Smirnoff 			rxbufpos = sc->rl_cdata.rl_rx_buf;
1198*b2d3d26fSGleb Smirnoff 
1199*b2d3d26fSGleb Smirnoff 		wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
1200*b2d3d26fSGleb Smirnoff 		if (total_len > wrap) {
1201*b2d3d26fSGleb Smirnoff 			m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp,
1202*b2d3d26fSGleb Smirnoff 			    NULL);
1203*b2d3d26fSGleb Smirnoff 			if (m != NULL)
1204*b2d3d26fSGleb Smirnoff 				m_copyback(m, wrap, total_len - wrap,
1205*b2d3d26fSGleb Smirnoff 					sc->rl_cdata.rl_rx_buf);
1206*b2d3d26fSGleb Smirnoff 			cur_rx = (total_len - wrap + ETHER_CRC_LEN);
1207*b2d3d26fSGleb Smirnoff 		} else {
1208*b2d3d26fSGleb Smirnoff 			m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp,
1209*b2d3d26fSGleb Smirnoff 			    NULL);
1210*b2d3d26fSGleb Smirnoff 			cur_rx += total_len + 4 + ETHER_CRC_LEN;
1211*b2d3d26fSGleb Smirnoff 		}
1212*b2d3d26fSGleb Smirnoff 
1213*b2d3d26fSGleb Smirnoff 		/* Round up to 32-bit boundary. */
1214*b2d3d26fSGleb Smirnoff 		cur_rx = (cur_rx + 3) & ~3;
1215*b2d3d26fSGleb Smirnoff 		CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
1216*b2d3d26fSGleb Smirnoff 
1217*b2d3d26fSGleb Smirnoff 		if (m == NULL) {
1218*b2d3d26fSGleb Smirnoff 			ifp->if_iqdrops++;
1219*b2d3d26fSGleb Smirnoff 			continue;
1220*b2d3d26fSGleb Smirnoff 		}
1221*b2d3d26fSGleb Smirnoff 
1222*b2d3d26fSGleb Smirnoff 		ifp->if_ipackets++;
1223*b2d3d26fSGleb Smirnoff 		RL_UNLOCK(sc);
1224*b2d3d26fSGleb Smirnoff 		(*ifp->if_input)(ifp, m);
1225*b2d3d26fSGleb Smirnoff 		RL_LOCK(sc);
1226*b2d3d26fSGleb Smirnoff 		rx_npkts++;
1227*b2d3d26fSGleb Smirnoff 	}
1228*b2d3d26fSGleb Smirnoff 
1229*b2d3d26fSGleb Smirnoff 	/* No need to sync Rx memory block as we didn't modify it. */
1230*b2d3d26fSGleb Smirnoff 	return (rx_npkts);
1231*b2d3d26fSGleb Smirnoff }
1232*b2d3d26fSGleb Smirnoff 
1233*b2d3d26fSGleb Smirnoff /*
1234*b2d3d26fSGleb Smirnoff  * A frame was downloaded to the chip. It's safe for us to clean up
1235*b2d3d26fSGleb Smirnoff  * the list buffers.
1236*b2d3d26fSGleb Smirnoff  */
1237*b2d3d26fSGleb Smirnoff static void
1238*b2d3d26fSGleb Smirnoff rl_txeof(struct rl_softc *sc)
1239*b2d3d26fSGleb Smirnoff {
1240*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
1241*b2d3d26fSGleb Smirnoff 	uint32_t		txstat;
1242*b2d3d26fSGleb Smirnoff 
1243*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1244*b2d3d26fSGleb Smirnoff 
1245*b2d3d26fSGleb Smirnoff 	/*
1246*b2d3d26fSGleb Smirnoff 	 * Go through our tx list and free mbufs for those
1247*b2d3d26fSGleb Smirnoff 	 * frames that have been uploaded.
1248*b2d3d26fSGleb Smirnoff 	 */
1249*b2d3d26fSGleb Smirnoff 	do {
1250*b2d3d26fSGleb Smirnoff 		if (RL_LAST_TXMBUF(sc) == NULL)
1251*b2d3d26fSGleb Smirnoff 			break;
1252*b2d3d26fSGleb Smirnoff 		txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
1253*b2d3d26fSGleb Smirnoff 		if (!(txstat & (RL_TXSTAT_TX_OK|
1254*b2d3d26fSGleb Smirnoff 		    RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
1255*b2d3d26fSGleb Smirnoff 			break;
1256*b2d3d26fSGleb Smirnoff 
1257*b2d3d26fSGleb Smirnoff 		ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
1258*b2d3d26fSGleb Smirnoff 
1259*b2d3d26fSGleb Smirnoff 		bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, RL_LAST_DMAMAP(sc),
1260*b2d3d26fSGleb Smirnoff 		    BUS_DMASYNC_POSTWRITE);
1261*b2d3d26fSGleb Smirnoff 		bus_dmamap_unload(sc->rl_cdata.rl_tx_tag, RL_LAST_DMAMAP(sc));
1262*b2d3d26fSGleb Smirnoff 		m_freem(RL_LAST_TXMBUF(sc));
1263*b2d3d26fSGleb Smirnoff 		RL_LAST_TXMBUF(sc) = NULL;
1264*b2d3d26fSGleb Smirnoff 		/*
1265*b2d3d26fSGleb Smirnoff 		 * If there was a transmit underrun, bump the TX threshold.
1266*b2d3d26fSGleb Smirnoff 		 * Make sure not to overflow the 63 * 32byte we can address
1267*b2d3d26fSGleb Smirnoff 		 * with the 6 available bit.
1268*b2d3d26fSGleb Smirnoff 		 */
1269*b2d3d26fSGleb Smirnoff 		if ((txstat & RL_TXSTAT_TX_UNDERRUN) &&
1270*b2d3d26fSGleb Smirnoff 		    (sc->rl_txthresh < 2016))
1271*b2d3d26fSGleb Smirnoff 			sc->rl_txthresh += 32;
1272*b2d3d26fSGleb Smirnoff 		if (txstat & RL_TXSTAT_TX_OK)
1273*b2d3d26fSGleb Smirnoff 			ifp->if_opackets++;
1274*b2d3d26fSGleb Smirnoff 		else {
1275*b2d3d26fSGleb Smirnoff 			int			oldthresh;
1276*b2d3d26fSGleb Smirnoff 			ifp->if_oerrors++;
1277*b2d3d26fSGleb Smirnoff 			if ((txstat & RL_TXSTAT_TXABRT) ||
1278*b2d3d26fSGleb Smirnoff 			    (txstat & RL_TXSTAT_OUTOFWIN))
1279*b2d3d26fSGleb Smirnoff 				CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1280*b2d3d26fSGleb Smirnoff 			oldthresh = sc->rl_txthresh;
1281*b2d3d26fSGleb Smirnoff 			/* error recovery */
1282*b2d3d26fSGleb Smirnoff 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1283*b2d3d26fSGleb Smirnoff 			rl_init_locked(sc);
1284*b2d3d26fSGleb Smirnoff 			/* restore original threshold */
1285*b2d3d26fSGleb Smirnoff 			sc->rl_txthresh = oldthresh;
1286*b2d3d26fSGleb Smirnoff 			return;
1287*b2d3d26fSGleb Smirnoff 		}
1288*b2d3d26fSGleb Smirnoff 		RL_INC(sc->rl_cdata.last_tx);
1289*b2d3d26fSGleb Smirnoff 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1290*b2d3d26fSGleb Smirnoff 	} while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
1291*b2d3d26fSGleb Smirnoff 
1292*b2d3d26fSGleb Smirnoff 	if (RL_LAST_TXMBUF(sc) == NULL)
1293*b2d3d26fSGleb Smirnoff 		sc->rl_watchdog_timer = 0;
1294*b2d3d26fSGleb Smirnoff }
1295*b2d3d26fSGleb Smirnoff 
1296*b2d3d26fSGleb Smirnoff static void
1297*b2d3d26fSGleb Smirnoff rl_twister_update(struct rl_softc *sc)
1298*b2d3d26fSGleb Smirnoff {
1299*b2d3d26fSGleb Smirnoff 	uint16_t linktest;
1300*b2d3d26fSGleb Smirnoff 	/*
1301*b2d3d26fSGleb Smirnoff 	 * Table provided by RealTek (Kinston <shangh@realtek.com.tw>) for
1302*b2d3d26fSGleb Smirnoff 	 * Linux driver.  Values undocumented otherwise.
1303*b2d3d26fSGleb Smirnoff 	 */
1304*b2d3d26fSGleb Smirnoff 	static const uint32_t param[4][4] = {
1305*b2d3d26fSGleb Smirnoff 		{0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
1306*b2d3d26fSGleb Smirnoff 		{0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1307*b2d3d26fSGleb Smirnoff 		{0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1308*b2d3d26fSGleb Smirnoff 		{0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
1309*b2d3d26fSGleb Smirnoff 	};
1310*b2d3d26fSGleb Smirnoff 
1311*b2d3d26fSGleb Smirnoff 	/*
1312*b2d3d26fSGleb Smirnoff 	 * Tune the so-called twister registers of the RTL8139.  These
1313*b2d3d26fSGleb Smirnoff 	 * are used to compensate for impedance mismatches.  The
1314*b2d3d26fSGleb Smirnoff 	 * method for tuning these registers is undocumented and the
1315*b2d3d26fSGleb Smirnoff 	 * following procedure is collected from public sources.
1316*b2d3d26fSGleb Smirnoff 	 */
1317*b2d3d26fSGleb Smirnoff 	switch (sc->rl_twister)
1318*b2d3d26fSGleb Smirnoff 	{
1319*b2d3d26fSGleb Smirnoff 	case CHK_LINK:
1320*b2d3d26fSGleb Smirnoff 		/*
1321*b2d3d26fSGleb Smirnoff 		 * If we have a sufficient link, then we can proceed in
1322*b2d3d26fSGleb Smirnoff 		 * the state machine to the next stage.  If not, then
1323*b2d3d26fSGleb Smirnoff 		 * disable further tuning after writing sane defaults.
1324*b2d3d26fSGleb Smirnoff 		 */
1325*b2d3d26fSGleb Smirnoff 		if (CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_LINK_OK) {
1326*b2d3d26fSGleb Smirnoff 			CSR_WRITE_2(sc, RL_CSCFG, RL_CSCFG_LINK_DOWN_OFF_CMD);
1327*b2d3d26fSGleb Smirnoff 			sc->rl_twister = FIND_ROW;
1328*b2d3d26fSGleb Smirnoff 		} else {
1329*b2d3d26fSGleb Smirnoff 			CSR_WRITE_2(sc, RL_CSCFG, RL_CSCFG_LINK_DOWN_CMD);
1330*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_CBL_TEST);
1331*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_PARA78, RL_PARA78_DEF);
1332*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_DEF);
1333*b2d3d26fSGleb Smirnoff 			sc->rl_twister = DONE;
1334*b2d3d26fSGleb Smirnoff 		}
1335*b2d3d26fSGleb Smirnoff 		break;
1336*b2d3d26fSGleb Smirnoff 	case FIND_ROW:
1337*b2d3d26fSGleb Smirnoff 		/*
1338*b2d3d26fSGleb Smirnoff 		 * Read how long it took to see the echo to find the tuning
1339*b2d3d26fSGleb Smirnoff 		 * row to use.
1340*b2d3d26fSGleb Smirnoff 		 */
1341*b2d3d26fSGleb Smirnoff 		linktest = CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_STATUS;
1342*b2d3d26fSGleb Smirnoff 		if (linktest == RL_CSCFG_ROW3)
1343*b2d3d26fSGleb Smirnoff 			sc->rl_twist_row = 3;
1344*b2d3d26fSGleb Smirnoff 		else if (linktest == RL_CSCFG_ROW2)
1345*b2d3d26fSGleb Smirnoff 			sc->rl_twist_row = 2;
1346*b2d3d26fSGleb Smirnoff 		else if (linktest == RL_CSCFG_ROW1)
1347*b2d3d26fSGleb Smirnoff 			sc->rl_twist_row = 1;
1348*b2d3d26fSGleb Smirnoff 		else
1349*b2d3d26fSGleb Smirnoff 			sc->rl_twist_row = 0;
1350*b2d3d26fSGleb Smirnoff 		sc->rl_twist_col = 0;
1351*b2d3d26fSGleb Smirnoff 		sc->rl_twister = SET_PARAM;
1352*b2d3d26fSGleb Smirnoff 		break;
1353*b2d3d26fSGleb Smirnoff 	case SET_PARAM:
1354*b2d3d26fSGleb Smirnoff 		if (sc->rl_twist_col == 0)
1355*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_RESET);
1356*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_PARA7C,
1357*b2d3d26fSGleb Smirnoff 		    param[sc->rl_twist_row][sc->rl_twist_col]);
1358*b2d3d26fSGleb Smirnoff 		if (++sc->rl_twist_col == 4) {
1359*b2d3d26fSGleb Smirnoff 			if (sc->rl_twist_row == 3)
1360*b2d3d26fSGleb Smirnoff 				sc->rl_twister = RECHK_LONG;
1361*b2d3d26fSGleb Smirnoff 			else
1362*b2d3d26fSGleb Smirnoff 				sc->rl_twister = DONE;
1363*b2d3d26fSGleb Smirnoff 		}
1364*b2d3d26fSGleb Smirnoff 		break;
1365*b2d3d26fSGleb Smirnoff 	case RECHK_LONG:
1366*b2d3d26fSGleb Smirnoff 		/*
1367*b2d3d26fSGleb Smirnoff 		 * For long cables, we have to double check to make sure we
1368*b2d3d26fSGleb Smirnoff 		 * don't mistune.
1369*b2d3d26fSGleb Smirnoff 		 */
1370*b2d3d26fSGleb Smirnoff 		linktest = CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_STATUS;
1371*b2d3d26fSGleb Smirnoff 		if (linktest == RL_CSCFG_ROW3)
1372*b2d3d26fSGleb Smirnoff 			sc->rl_twister = DONE;
1373*b2d3d26fSGleb Smirnoff 		else {
1374*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_RETUNE);
1375*b2d3d26fSGleb Smirnoff 			sc->rl_twister = RETUNE;
1376*b2d3d26fSGleb Smirnoff 		}
1377*b2d3d26fSGleb Smirnoff 		break;
1378*b2d3d26fSGleb Smirnoff 	case RETUNE:
1379*b2d3d26fSGleb Smirnoff 		/* Retune for a shorter cable (try column 2) */
1380*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_CBL_TEST);
1381*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_PARA78, RL_PARA78_DEF);
1382*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_DEF);
1383*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_RESET);
1384*b2d3d26fSGleb Smirnoff 		sc->rl_twist_row--;
1385*b2d3d26fSGleb Smirnoff 		sc->rl_twist_col = 0;
1386*b2d3d26fSGleb Smirnoff 		sc->rl_twister = SET_PARAM;
1387*b2d3d26fSGleb Smirnoff 		break;
1388*b2d3d26fSGleb Smirnoff 
1389*b2d3d26fSGleb Smirnoff 	case DONE:
1390*b2d3d26fSGleb Smirnoff 		break;
1391*b2d3d26fSGleb Smirnoff 	}
1392*b2d3d26fSGleb Smirnoff 
1393*b2d3d26fSGleb Smirnoff }
1394*b2d3d26fSGleb Smirnoff 
1395*b2d3d26fSGleb Smirnoff static void
1396*b2d3d26fSGleb Smirnoff rl_tick(void *xsc)
1397*b2d3d26fSGleb Smirnoff {
1398*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = xsc;
1399*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
1400*b2d3d26fSGleb Smirnoff 	int ticks;
1401*b2d3d26fSGleb Smirnoff 
1402*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1403*b2d3d26fSGleb Smirnoff 	/*
1404*b2d3d26fSGleb Smirnoff 	 * If we're doing the twister cable calibration, then we need to defer
1405*b2d3d26fSGleb Smirnoff 	 * watchdog timeouts.  This is a no-op in normal operations, but
1406*b2d3d26fSGleb Smirnoff 	 * can falsely trigger when the cable calibration takes a while and
1407*b2d3d26fSGleb Smirnoff 	 * there was traffic ready to go when rl was started.
1408*b2d3d26fSGleb Smirnoff 	 *
1409*b2d3d26fSGleb Smirnoff 	 * We don't defer mii_tick since that updates the mii status, which
1410*b2d3d26fSGleb Smirnoff 	 * helps the twister process, at least according to similar patches
1411*b2d3d26fSGleb Smirnoff 	 * for the Linux driver I found online while doing the fixes.  Worst
1412*b2d3d26fSGleb Smirnoff 	 * case is a few extra mii reads during calibration.
1413*b2d3d26fSGleb Smirnoff 	 */
1414*b2d3d26fSGleb Smirnoff 	mii = device_get_softc(sc->rl_miibus);
1415*b2d3d26fSGleb Smirnoff 	mii_tick(mii);
1416*b2d3d26fSGleb Smirnoff 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
1417*b2d3d26fSGleb Smirnoff 		rl_miibus_statchg(sc->rl_dev);
1418*b2d3d26fSGleb Smirnoff 	if (sc->rl_twister_enable) {
1419*b2d3d26fSGleb Smirnoff 		if (sc->rl_twister == DONE)
1420*b2d3d26fSGleb Smirnoff 			rl_watchdog(sc);
1421*b2d3d26fSGleb Smirnoff 		else
1422*b2d3d26fSGleb Smirnoff 			rl_twister_update(sc);
1423*b2d3d26fSGleb Smirnoff 		if (sc->rl_twister == DONE)
1424*b2d3d26fSGleb Smirnoff 			ticks = hz;
1425*b2d3d26fSGleb Smirnoff 		else
1426*b2d3d26fSGleb Smirnoff 			ticks = hz / 10;
1427*b2d3d26fSGleb Smirnoff 	} else {
1428*b2d3d26fSGleb Smirnoff 		rl_watchdog(sc);
1429*b2d3d26fSGleb Smirnoff 		ticks = hz;
1430*b2d3d26fSGleb Smirnoff 	}
1431*b2d3d26fSGleb Smirnoff 
1432*b2d3d26fSGleb Smirnoff 	callout_reset(&sc->rl_stat_callout, ticks, rl_tick, sc);
1433*b2d3d26fSGleb Smirnoff }
1434*b2d3d26fSGleb Smirnoff 
1435*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
1436*b2d3d26fSGleb Smirnoff static int
1437*b2d3d26fSGleb Smirnoff rl_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1438*b2d3d26fSGleb Smirnoff {
1439*b2d3d26fSGleb Smirnoff 	struct rl_softc *sc = ifp->if_softc;
1440*b2d3d26fSGleb Smirnoff 	int rx_npkts = 0;
1441*b2d3d26fSGleb Smirnoff 
1442*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1443*b2d3d26fSGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1444*b2d3d26fSGleb Smirnoff 		rx_npkts = rl_poll_locked(ifp, cmd, count);
1445*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1446*b2d3d26fSGleb Smirnoff 	return (rx_npkts);
1447*b2d3d26fSGleb Smirnoff }
1448*b2d3d26fSGleb Smirnoff 
1449*b2d3d26fSGleb Smirnoff static int
1450*b2d3d26fSGleb Smirnoff rl_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
1451*b2d3d26fSGleb Smirnoff {
1452*b2d3d26fSGleb Smirnoff 	struct rl_softc *sc = ifp->if_softc;
1453*b2d3d26fSGleb Smirnoff 	int rx_npkts;
1454*b2d3d26fSGleb Smirnoff 
1455*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1456*b2d3d26fSGleb Smirnoff 
1457*b2d3d26fSGleb Smirnoff 	sc->rxcycles = count;
1458*b2d3d26fSGleb Smirnoff 	rx_npkts = rl_rxeof(sc);
1459*b2d3d26fSGleb Smirnoff 	rl_txeof(sc);
1460*b2d3d26fSGleb Smirnoff 
1461*b2d3d26fSGleb Smirnoff 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1462*b2d3d26fSGleb Smirnoff 		rl_start_locked(ifp);
1463*b2d3d26fSGleb Smirnoff 
1464*b2d3d26fSGleb Smirnoff 	if (cmd == POLL_AND_CHECK_STATUS) {
1465*b2d3d26fSGleb Smirnoff 		uint16_t	status;
1466*b2d3d26fSGleb Smirnoff 
1467*b2d3d26fSGleb Smirnoff 		/* We should also check the status register. */
1468*b2d3d26fSGleb Smirnoff 		status = CSR_READ_2(sc, RL_ISR);
1469*b2d3d26fSGleb Smirnoff 		if (status == 0xffff)
1470*b2d3d26fSGleb Smirnoff 			return (rx_npkts);
1471*b2d3d26fSGleb Smirnoff 		if (status != 0)
1472*b2d3d26fSGleb Smirnoff 			CSR_WRITE_2(sc, RL_ISR, status);
1473*b2d3d26fSGleb Smirnoff 
1474*b2d3d26fSGleb Smirnoff 		/* XXX We should check behaviour on receiver stalls. */
1475*b2d3d26fSGleb Smirnoff 
1476*b2d3d26fSGleb Smirnoff 		if (status & RL_ISR_SYSTEM_ERR) {
1477*b2d3d26fSGleb Smirnoff 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1478*b2d3d26fSGleb Smirnoff 			rl_init_locked(sc);
1479*b2d3d26fSGleb Smirnoff 		}
1480*b2d3d26fSGleb Smirnoff 	}
1481*b2d3d26fSGleb Smirnoff 	return (rx_npkts);
1482*b2d3d26fSGleb Smirnoff }
1483*b2d3d26fSGleb Smirnoff #endif /* DEVICE_POLLING */
1484*b2d3d26fSGleb Smirnoff 
1485*b2d3d26fSGleb Smirnoff static void
1486*b2d3d26fSGleb Smirnoff rl_intr(void *arg)
1487*b2d3d26fSGleb Smirnoff {
1488*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = arg;
1489*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
1490*b2d3d26fSGleb Smirnoff 	uint16_t		status;
1491*b2d3d26fSGleb Smirnoff 	int			count;
1492*b2d3d26fSGleb Smirnoff 
1493*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1494*b2d3d26fSGleb Smirnoff 
1495*b2d3d26fSGleb Smirnoff 	if (sc->suspended)
1496*b2d3d26fSGleb Smirnoff 		goto done_locked;
1497*b2d3d26fSGleb Smirnoff 
1498*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
1499*b2d3d26fSGleb Smirnoff 	if  (ifp->if_capenable & IFCAP_POLLING)
1500*b2d3d26fSGleb Smirnoff 		goto done_locked;
1501*b2d3d26fSGleb Smirnoff #endif
1502*b2d3d26fSGleb Smirnoff 
1503*b2d3d26fSGleb Smirnoff 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1504*b2d3d26fSGleb Smirnoff 		goto done_locked2;
1505*b2d3d26fSGleb Smirnoff 	status = CSR_READ_2(sc, RL_ISR);
1506*b2d3d26fSGleb Smirnoff 	if (status == 0xffff || (status & RL_INTRS) == 0)
1507*b2d3d26fSGleb Smirnoff 		goto done_locked;
1508*b2d3d26fSGleb Smirnoff 	/*
1509*b2d3d26fSGleb Smirnoff 	 * Ours, disable further interrupts.
1510*b2d3d26fSGleb Smirnoff 	 */
1511*b2d3d26fSGleb Smirnoff 	CSR_WRITE_2(sc, RL_IMR, 0);
1512*b2d3d26fSGleb Smirnoff 	for (count = 16; count > 0; count--) {
1513*b2d3d26fSGleb Smirnoff 		CSR_WRITE_2(sc, RL_ISR, status);
1514*b2d3d26fSGleb Smirnoff 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1515*b2d3d26fSGleb Smirnoff 			if (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR))
1516*b2d3d26fSGleb Smirnoff 				rl_rxeof(sc);
1517*b2d3d26fSGleb Smirnoff 			if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR))
1518*b2d3d26fSGleb Smirnoff 				rl_txeof(sc);
1519*b2d3d26fSGleb Smirnoff 			if (status & RL_ISR_SYSTEM_ERR) {
1520*b2d3d26fSGleb Smirnoff 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1521*b2d3d26fSGleb Smirnoff 				rl_init_locked(sc);
1522*b2d3d26fSGleb Smirnoff 				RL_UNLOCK(sc);
1523*b2d3d26fSGleb Smirnoff 				return;
1524*b2d3d26fSGleb Smirnoff 			}
1525*b2d3d26fSGleb Smirnoff 		}
1526*b2d3d26fSGleb Smirnoff 		status = CSR_READ_2(sc, RL_ISR);
1527*b2d3d26fSGleb Smirnoff 		/* If the card has gone away, the read returns 0xffff. */
1528*b2d3d26fSGleb Smirnoff 		if (status == 0xffff || (status & RL_INTRS) == 0)
1529*b2d3d26fSGleb Smirnoff 			break;
1530*b2d3d26fSGleb Smirnoff 	}
1531*b2d3d26fSGleb Smirnoff 
1532*b2d3d26fSGleb Smirnoff 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1533*b2d3d26fSGleb Smirnoff 		rl_start_locked(ifp);
1534*b2d3d26fSGleb Smirnoff 
1535*b2d3d26fSGleb Smirnoff done_locked2:
1536*b2d3d26fSGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1537*b2d3d26fSGleb Smirnoff 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1538*b2d3d26fSGleb Smirnoff done_locked:
1539*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1540*b2d3d26fSGleb Smirnoff }
1541*b2d3d26fSGleb Smirnoff 
1542*b2d3d26fSGleb Smirnoff /*
1543*b2d3d26fSGleb Smirnoff  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1544*b2d3d26fSGleb Smirnoff  * pointers to the fragment pointers.
1545*b2d3d26fSGleb Smirnoff  */
1546*b2d3d26fSGleb Smirnoff static int
1547*b2d3d26fSGleb Smirnoff rl_encap(struct rl_softc *sc, struct mbuf **m_head)
1548*b2d3d26fSGleb Smirnoff {
1549*b2d3d26fSGleb Smirnoff 	struct mbuf		*m;
1550*b2d3d26fSGleb Smirnoff 	bus_dma_segment_t	txsegs[1];
1551*b2d3d26fSGleb Smirnoff 	int			error, nsegs, padlen;
1552*b2d3d26fSGleb Smirnoff 
1553*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1554*b2d3d26fSGleb Smirnoff 
1555*b2d3d26fSGleb Smirnoff 	m = *m_head;
1556*b2d3d26fSGleb Smirnoff 	padlen = 0;
1557*b2d3d26fSGleb Smirnoff 	/*
1558*b2d3d26fSGleb Smirnoff 	 * Hardware doesn't auto-pad, so we have to make sure
1559*b2d3d26fSGleb Smirnoff 	 * pad short frames out to the minimum frame length.
1560*b2d3d26fSGleb Smirnoff 	 */
1561*b2d3d26fSGleb Smirnoff 	if (m->m_pkthdr.len < RL_MIN_FRAMELEN)
1562*b2d3d26fSGleb Smirnoff 		padlen = RL_MIN_FRAMELEN - m->m_pkthdr.len;
1563*b2d3d26fSGleb Smirnoff 	/*
1564*b2d3d26fSGleb Smirnoff 	 * The RealTek is brain damaged and wants longword-aligned
1565*b2d3d26fSGleb Smirnoff 	 * TX buffers, plus we can only have one fragment buffer
1566*b2d3d26fSGleb Smirnoff 	 * per packet. We have to copy pretty much all the time.
1567*b2d3d26fSGleb Smirnoff 	 */
1568*b2d3d26fSGleb Smirnoff 	if (m->m_next != NULL || (mtod(m, uintptr_t) & 3) != 0 ||
1569*b2d3d26fSGleb Smirnoff 	    (padlen > 0 && M_TRAILINGSPACE(m) < padlen)) {
1570*b2d3d26fSGleb Smirnoff 		m = m_defrag(*m_head, M_NOWAIT);
1571*b2d3d26fSGleb Smirnoff 		if (m == NULL) {
1572*b2d3d26fSGleb Smirnoff 			m_freem(*m_head);
1573*b2d3d26fSGleb Smirnoff 			*m_head = NULL;
1574*b2d3d26fSGleb Smirnoff 			return (ENOMEM);
1575*b2d3d26fSGleb Smirnoff 		}
1576*b2d3d26fSGleb Smirnoff 	}
1577*b2d3d26fSGleb Smirnoff 	*m_head = m;
1578*b2d3d26fSGleb Smirnoff 
1579*b2d3d26fSGleb Smirnoff 	if (padlen > 0) {
1580*b2d3d26fSGleb Smirnoff 		/*
1581*b2d3d26fSGleb Smirnoff 		 * Make security-conscious people happy: zero out the
1582*b2d3d26fSGleb Smirnoff 		 * bytes in the pad area, since we don't know what
1583*b2d3d26fSGleb Smirnoff 		 * this mbuf cluster buffer's previous user might
1584*b2d3d26fSGleb Smirnoff 		 * have left in it.
1585*b2d3d26fSGleb Smirnoff 		 */
1586*b2d3d26fSGleb Smirnoff 		bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1587*b2d3d26fSGleb Smirnoff 		m->m_pkthdr.len += padlen;
1588*b2d3d26fSGleb Smirnoff 		m->m_len = m->m_pkthdr.len;
1589*b2d3d26fSGleb Smirnoff 	}
1590*b2d3d26fSGleb Smirnoff 
1591*b2d3d26fSGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->rl_cdata.rl_tx_tag,
1592*b2d3d26fSGleb Smirnoff 	    RL_CUR_DMAMAP(sc), m, txsegs, &nsegs, 0);
1593*b2d3d26fSGleb Smirnoff 	if (error != 0)
1594*b2d3d26fSGleb Smirnoff 		return (error);
1595*b2d3d26fSGleb Smirnoff 	if (nsegs == 0) {
1596*b2d3d26fSGleb Smirnoff 		m_freem(*m_head);
1597*b2d3d26fSGleb Smirnoff 		*m_head = NULL;
1598*b2d3d26fSGleb Smirnoff 		return (EIO);
1599*b2d3d26fSGleb Smirnoff 	}
1600*b2d3d26fSGleb Smirnoff 
1601*b2d3d26fSGleb Smirnoff 	RL_CUR_TXMBUF(sc) = m;
1602*b2d3d26fSGleb Smirnoff 	bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, RL_CUR_DMAMAP(sc),
1603*b2d3d26fSGleb Smirnoff 	    BUS_DMASYNC_PREWRITE);
1604*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_CUR_TXADDR(sc), RL_ADDR_LO(txsegs[0].ds_addr));
1605*b2d3d26fSGleb Smirnoff 
1606*b2d3d26fSGleb Smirnoff 	return (0);
1607*b2d3d26fSGleb Smirnoff }
1608*b2d3d26fSGleb Smirnoff 
1609*b2d3d26fSGleb Smirnoff /*
1610*b2d3d26fSGleb Smirnoff  * Main transmit routine.
1611*b2d3d26fSGleb Smirnoff  */
1612*b2d3d26fSGleb Smirnoff static void
1613*b2d3d26fSGleb Smirnoff rl_start(struct ifnet *ifp)
1614*b2d3d26fSGleb Smirnoff {
1615*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = ifp->if_softc;
1616*b2d3d26fSGleb Smirnoff 
1617*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1618*b2d3d26fSGleb Smirnoff 	rl_start_locked(ifp);
1619*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1620*b2d3d26fSGleb Smirnoff }
1621*b2d3d26fSGleb Smirnoff 
1622*b2d3d26fSGleb Smirnoff static void
1623*b2d3d26fSGleb Smirnoff rl_start_locked(struct ifnet *ifp)
1624*b2d3d26fSGleb Smirnoff {
1625*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = ifp->if_softc;
1626*b2d3d26fSGleb Smirnoff 	struct mbuf		*m_head = NULL;
1627*b2d3d26fSGleb Smirnoff 
1628*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1629*b2d3d26fSGleb Smirnoff 
1630*b2d3d26fSGleb Smirnoff 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1631*b2d3d26fSGleb Smirnoff 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
1632*b2d3d26fSGleb Smirnoff 		return;
1633*b2d3d26fSGleb Smirnoff 
1634*b2d3d26fSGleb Smirnoff 	while (RL_CUR_TXMBUF(sc) == NULL) {
1635*b2d3d26fSGleb Smirnoff 
1636*b2d3d26fSGleb Smirnoff 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1637*b2d3d26fSGleb Smirnoff 
1638*b2d3d26fSGleb Smirnoff 		if (m_head == NULL)
1639*b2d3d26fSGleb Smirnoff 			break;
1640*b2d3d26fSGleb Smirnoff 
1641*b2d3d26fSGleb Smirnoff 		if (rl_encap(sc, &m_head)) {
1642*b2d3d26fSGleb Smirnoff 			if (m_head == NULL)
1643*b2d3d26fSGleb Smirnoff 				break;
1644*b2d3d26fSGleb Smirnoff 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1645*b2d3d26fSGleb Smirnoff 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1646*b2d3d26fSGleb Smirnoff 			break;
1647*b2d3d26fSGleb Smirnoff 		}
1648*b2d3d26fSGleb Smirnoff 
1649*b2d3d26fSGleb Smirnoff 		/* Pass a copy of this mbuf chain to the bpf subsystem. */
1650*b2d3d26fSGleb Smirnoff 		BPF_MTAP(ifp, RL_CUR_TXMBUF(sc));
1651*b2d3d26fSGleb Smirnoff 
1652*b2d3d26fSGleb Smirnoff 		/* Transmit the frame. */
1653*b2d3d26fSGleb Smirnoff 		CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
1654*b2d3d26fSGleb Smirnoff 		    RL_TXTHRESH(sc->rl_txthresh) |
1655*b2d3d26fSGleb Smirnoff 		    RL_CUR_TXMBUF(sc)->m_pkthdr.len);
1656*b2d3d26fSGleb Smirnoff 
1657*b2d3d26fSGleb Smirnoff 		RL_INC(sc->rl_cdata.cur_tx);
1658*b2d3d26fSGleb Smirnoff 
1659*b2d3d26fSGleb Smirnoff 		/* Set a timeout in case the chip goes out to lunch. */
1660*b2d3d26fSGleb Smirnoff 		sc->rl_watchdog_timer = 5;
1661*b2d3d26fSGleb Smirnoff 	}
1662*b2d3d26fSGleb Smirnoff 
1663*b2d3d26fSGleb Smirnoff 	/*
1664*b2d3d26fSGleb Smirnoff 	 * We broke out of the loop because all our TX slots are
1665*b2d3d26fSGleb Smirnoff 	 * full. Mark the NIC as busy until it drains some of the
1666*b2d3d26fSGleb Smirnoff 	 * packets from the queue.
1667*b2d3d26fSGleb Smirnoff 	 */
1668*b2d3d26fSGleb Smirnoff 	if (RL_CUR_TXMBUF(sc) != NULL)
1669*b2d3d26fSGleb Smirnoff 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1670*b2d3d26fSGleb Smirnoff }
1671*b2d3d26fSGleb Smirnoff 
1672*b2d3d26fSGleb Smirnoff static void
1673*b2d3d26fSGleb Smirnoff rl_init(void *xsc)
1674*b2d3d26fSGleb Smirnoff {
1675*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = xsc;
1676*b2d3d26fSGleb Smirnoff 
1677*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1678*b2d3d26fSGleb Smirnoff 	rl_init_locked(sc);
1679*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1680*b2d3d26fSGleb Smirnoff }
1681*b2d3d26fSGleb Smirnoff 
1682*b2d3d26fSGleb Smirnoff static void
1683*b2d3d26fSGleb Smirnoff rl_init_locked(struct rl_softc *sc)
1684*b2d3d26fSGleb Smirnoff {
1685*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
1686*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
1687*b2d3d26fSGleb Smirnoff 	uint32_t		eaddr[2];
1688*b2d3d26fSGleb Smirnoff 
1689*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1690*b2d3d26fSGleb Smirnoff 
1691*b2d3d26fSGleb Smirnoff 	mii = device_get_softc(sc->rl_miibus);
1692*b2d3d26fSGleb Smirnoff 
1693*b2d3d26fSGleb Smirnoff 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1694*b2d3d26fSGleb Smirnoff 		return;
1695*b2d3d26fSGleb Smirnoff 
1696*b2d3d26fSGleb Smirnoff 	/*
1697*b2d3d26fSGleb Smirnoff 	 * Cancel pending I/O and free all RX/TX buffers.
1698*b2d3d26fSGleb Smirnoff 	 */
1699*b2d3d26fSGleb Smirnoff 	rl_stop(sc);
1700*b2d3d26fSGleb Smirnoff 
1701*b2d3d26fSGleb Smirnoff 	rl_reset(sc);
1702*b2d3d26fSGleb Smirnoff 	if (sc->rl_twister_enable) {
1703*b2d3d26fSGleb Smirnoff 		/*
1704*b2d3d26fSGleb Smirnoff 		 * Reset twister register tuning state.  The twister
1705*b2d3d26fSGleb Smirnoff 		 * registers and their tuning are undocumented, but
1706*b2d3d26fSGleb Smirnoff 		 * are necessary to cope with bad links.  rl_twister =
1707*b2d3d26fSGleb Smirnoff 		 * DONE here will disable this entirely.
1708*b2d3d26fSGleb Smirnoff 		 */
1709*b2d3d26fSGleb Smirnoff 		sc->rl_twister = CHK_LINK;
1710*b2d3d26fSGleb Smirnoff 	}
1711*b2d3d26fSGleb Smirnoff 
1712*b2d3d26fSGleb Smirnoff 	/*
1713*b2d3d26fSGleb Smirnoff 	 * Init our MAC address.  Even though the chipset
1714*b2d3d26fSGleb Smirnoff 	 * documentation doesn't mention it, we need to enter "Config
1715*b2d3d26fSGleb Smirnoff 	 * register write enable" mode to modify the ID registers.
1716*b2d3d26fSGleb Smirnoff 	 */
1717*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
1718*b2d3d26fSGleb Smirnoff 	bzero(eaddr, sizeof(eaddr));
1719*b2d3d26fSGleb Smirnoff 	bcopy(IF_LLADDR(sc->rl_ifp), eaddr, ETHER_ADDR_LEN);
1720*b2d3d26fSGleb Smirnoff 	CSR_WRITE_STREAM_4(sc, RL_IDR0, eaddr[0]);
1721*b2d3d26fSGleb Smirnoff 	CSR_WRITE_STREAM_4(sc, RL_IDR4, eaddr[1]);
1722*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1723*b2d3d26fSGleb Smirnoff 
1724*b2d3d26fSGleb Smirnoff 	/* Init the RX memory block pointer register. */
1725*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_RXADDR, sc->rl_cdata.rl_rx_buf_paddr +
1726*b2d3d26fSGleb Smirnoff 	    RL_RX_8139_BUF_RESERVE);
1727*b2d3d26fSGleb Smirnoff 	/* Init TX descriptors. */
1728*b2d3d26fSGleb Smirnoff 	rl_list_tx_init(sc);
1729*b2d3d26fSGleb Smirnoff 	/* Init Rx memory block. */
1730*b2d3d26fSGleb Smirnoff 	rl_list_rx_init(sc);
1731*b2d3d26fSGleb Smirnoff 
1732*b2d3d26fSGleb Smirnoff 	/*
1733*b2d3d26fSGleb Smirnoff 	 * Enable transmit and receive.
1734*b2d3d26fSGleb Smirnoff 	 */
1735*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1736*b2d3d26fSGleb Smirnoff 
1737*b2d3d26fSGleb Smirnoff 	/*
1738*b2d3d26fSGleb Smirnoff 	 * Set the initial TX and RX configuration.
1739*b2d3d26fSGleb Smirnoff 	 */
1740*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1741*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
1742*b2d3d26fSGleb Smirnoff 
1743*b2d3d26fSGleb Smirnoff 	/* Set RX filter. */
1744*b2d3d26fSGleb Smirnoff 	rl_rxfilter(sc);
1745*b2d3d26fSGleb Smirnoff 
1746*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
1747*b2d3d26fSGleb Smirnoff 	/* Disable interrupts if we are polling. */
1748*b2d3d26fSGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
1749*b2d3d26fSGleb Smirnoff 		CSR_WRITE_2(sc, RL_IMR, 0);
1750*b2d3d26fSGleb Smirnoff 	else
1751*b2d3d26fSGleb Smirnoff #endif
1752*b2d3d26fSGleb Smirnoff 	/* Enable interrupts. */
1753*b2d3d26fSGleb Smirnoff 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1754*b2d3d26fSGleb Smirnoff 
1755*b2d3d26fSGleb Smirnoff 	/* Set initial TX threshold */
1756*b2d3d26fSGleb Smirnoff 	sc->rl_txthresh = RL_TX_THRESH_INIT;
1757*b2d3d26fSGleb Smirnoff 
1758*b2d3d26fSGleb Smirnoff 	/* Start RX/TX process. */
1759*b2d3d26fSGleb Smirnoff 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
1760*b2d3d26fSGleb Smirnoff 
1761*b2d3d26fSGleb Smirnoff 	/* Enable receiver and transmitter. */
1762*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1763*b2d3d26fSGleb Smirnoff 
1764*b2d3d26fSGleb Smirnoff 	sc->rl_flags &= ~RL_FLAG_LINK;
1765*b2d3d26fSGleb Smirnoff 	mii_mediachg(mii);
1766*b2d3d26fSGleb Smirnoff 
1767*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
1768*b2d3d26fSGleb Smirnoff 
1769*b2d3d26fSGleb Smirnoff 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1770*b2d3d26fSGleb Smirnoff 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1771*b2d3d26fSGleb Smirnoff 
1772*b2d3d26fSGleb Smirnoff 	callout_reset(&sc->rl_stat_callout, hz, rl_tick, sc);
1773*b2d3d26fSGleb Smirnoff }
1774*b2d3d26fSGleb Smirnoff 
1775*b2d3d26fSGleb Smirnoff /*
1776*b2d3d26fSGleb Smirnoff  * Set media options.
1777*b2d3d26fSGleb Smirnoff  */
1778*b2d3d26fSGleb Smirnoff static int
1779*b2d3d26fSGleb Smirnoff rl_ifmedia_upd(struct ifnet *ifp)
1780*b2d3d26fSGleb Smirnoff {
1781*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = ifp->if_softc;
1782*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
1783*b2d3d26fSGleb Smirnoff 
1784*b2d3d26fSGleb Smirnoff 	mii = device_get_softc(sc->rl_miibus);
1785*b2d3d26fSGleb Smirnoff 
1786*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1787*b2d3d26fSGleb Smirnoff 	mii_mediachg(mii);
1788*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1789*b2d3d26fSGleb Smirnoff 
1790*b2d3d26fSGleb Smirnoff 	return (0);
1791*b2d3d26fSGleb Smirnoff }
1792*b2d3d26fSGleb Smirnoff 
1793*b2d3d26fSGleb Smirnoff /*
1794*b2d3d26fSGleb Smirnoff  * Report current media status.
1795*b2d3d26fSGleb Smirnoff  */
1796*b2d3d26fSGleb Smirnoff static void
1797*b2d3d26fSGleb Smirnoff rl_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1798*b2d3d26fSGleb Smirnoff {
1799*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = ifp->if_softc;
1800*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
1801*b2d3d26fSGleb Smirnoff 
1802*b2d3d26fSGleb Smirnoff 	mii = device_get_softc(sc->rl_miibus);
1803*b2d3d26fSGleb Smirnoff 
1804*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1805*b2d3d26fSGleb Smirnoff 	mii_pollstat(mii);
1806*b2d3d26fSGleb Smirnoff 	ifmr->ifm_active = mii->mii_media_active;
1807*b2d3d26fSGleb Smirnoff 	ifmr->ifm_status = mii->mii_media_status;
1808*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1809*b2d3d26fSGleb Smirnoff }
1810*b2d3d26fSGleb Smirnoff 
1811*b2d3d26fSGleb Smirnoff static int
1812*b2d3d26fSGleb Smirnoff rl_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1813*b2d3d26fSGleb Smirnoff {
1814*b2d3d26fSGleb Smirnoff 	struct ifreq		*ifr = (struct ifreq *)data;
1815*b2d3d26fSGleb Smirnoff 	struct mii_data		*mii;
1816*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc = ifp->if_softc;
1817*b2d3d26fSGleb Smirnoff 	int			error = 0, mask;
1818*b2d3d26fSGleb Smirnoff 
1819*b2d3d26fSGleb Smirnoff 	switch (command) {
1820*b2d3d26fSGleb Smirnoff 	case SIOCSIFFLAGS:
1821*b2d3d26fSGleb Smirnoff 		RL_LOCK(sc);
1822*b2d3d26fSGleb Smirnoff 		if (ifp->if_flags & IFF_UP) {
1823*b2d3d26fSGleb Smirnoff 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1824*b2d3d26fSGleb Smirnoff 			    ((ifp->if_flags ^ sc->rl_if_flags) &
1825*b2d3d26fSGleb Smirnoff                             (IFF_PROMISC | IFF_ALLMULTI)))
1826*b2d3d26fSGleb Smirnoff 				rl_rxfilter(sc);
1827*b2d3d26fSGleb Smirnoff                         else
1828*b2d3d26fSGleb Smirnoff 				rl_init_locked(sc);
1829*b2d3d26fSGleb Smirnoff                 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1830*b2d3d26fSGleb Smirnoff 			rl_stop(sc);
1831*b2d3d26fSGleb Smirnoff 		sc->rl_if_flags = ifp->if_flags;
1832*b2d3d26fSGleb Smirnoff 		RL_UNLOCK(sc);
1833*b2d3d26fSGleb Smirnoff 		break;
1834*b2d3d26fSGleb Smirnoff 	case SIOCADDMULTI:
1835*b2d3d26fSGleb Smirnoff 	case SIOCDELMULTI:
1836*b2d3d26fSGleb Smirnoff 		RL_LOCK(sc);
1837*b2d3d26fSGleb Smirnoff 		rl_rxfilter(sc);
1838*b2d3d26fSGleb Smirnoff 		RL_UNLOCK(sc);
1839*b2d3d26fSGleb Smirnoff 		break;
1840*b2d3d26fSGleb Smirnoff 	case SIOCGIFMEDIA:
1841*b2d3d26fSGleb Smirnoff 	case SIOCSIFMEDIA:
1842*b2d3d26fSGleb Smirnoff 		mii = device_get_softc(sc->rl_miibus);
1843*b2d3d26fSGleb Smirnoff 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1844*b2d3d26fSGleb Smirnoff 		break;
1845*b2d3d26fSGleb Smirnoff 	case SIOCSIFCAP:
1846*b2d3d26fSGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1847*b2d3d26fSGleb Smirnoff #ifdef DEVICE_POLLING
1848*b2d3d26fSGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
1849*b2d3d26fSGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
1850*b2d3d26fSGleb Smirnoff 			error = ether_poll_register(rl_poll, ifp);
1851*b2d3d26fSGleb Smirnoff 			if (error)
1852*b2d3d26fSGleb Smirnoff 				return(error);
1853*b2d3d26fSGleb Smirnoff 			RL_LOCK(sc);
1854*b2d3d26fSGleb Smirnoff 			/* Disable interrupts */
1855*b2d3d26fSGleb Smirnoff 			CSR_WRITE_2(sc, RL_IMR, 0x0000);
1856*b2d3d26fSGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
1857*b2d3d26fSGleb Smirnoff 			RL_UNLOCK(sc);
1858*b2d3d26fSGleb Smirnoff 			return (error);
1859*b2d3d26fSGleb Smirnoff 
1860*b2d3d26fSGleb Smirnoff 		}
1861*b2d3d26fSGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
1862*b2d3d26fSGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
1863*b2d3d26fSGleb Smirnoff 			error = ether_poll_deregister(ifp);
1864*b2d3d26fSGleb Smirnoff 			/* Enable interrupts. */
1865*b2d3d26fSGleb Smirnoff 			RL_LOCK(sc);
1866*b2d3d26fSGleb Smirnoff 			CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1867*b2d3d26fSGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
1868*b2d3d26fSGleb Smirnoff 			RL_UNLOCK(sc);
1869*b2d3d26fSGleb Smirnoff 			return (error);
1870*b2d3d26fSGleb Smirnoff 		}
1871*b2d3d26fSGleb Smirnoff #endif /* DEVICE_POLLING */
1872*b2d3d26fSGleb Smirnoff 		if ((mask & IFCAP_WOL) != 0 &&
1873*b2d3d26fSGleb Smirnoff 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
1874*b2d3d26fSGleb Smirnoff 			if ((mask & IFCAP_WOL_UCAST) != 0)
1875*b2d3d26fSGleb Smirnoff 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
1876*b2d3d26fSGleb Smirnoff 			if ((mask & IFCAP_WOL_MCAST) != 0)
1877*b2d3d26fSGleb Smirnoff 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
1878*b2d3d26fSGleb Smirnoff 			if ((mask & IFCAP_WOL_MAGIC) != 0)
1879*b2d3d26fSGleb Smirnoff 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1880*b2d3d26fSGleb Smirnoff 		}
1881*b2d3d26fSGleb Smirnoff 		break;
1882*b2d3d26fSGleb Smirnoff 	default:
1883*b2d3d26fSGleb Smirnoff 		error = ether_ioctl(ifp, command, data);
1884*b2d3d26fSGleb Smirnoff 		break;
1885*b2d3d26fSGleb Smirnoff 	}
1886*b2d3d26fSGleb Smirnoff 
1887*b2d3d26fSGleb Smirnoff 	return (error);
1888*b2d3d26fSGleb Smirnoff }
1889*b2d3d26fSGleb Smirnoff 
1890*b2d3d26fSGleb Smirnoff static void
1891*b2d3d26fSGleb Smirnoff rl_watchdog(struct rl_softc *sc)
1892*b2d3d26fSGleb Smirnoff {
1893*b2d3d26fSGleb Smirnoff 
1894*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1895*b2d3d26fSGleb Smirnoff 
1896*b2d3d26fSGleb Smirnoff 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer >0)
1897*b2d3d26fSGleb Smirnoff 		return;
1898*b2d3d26fSGleb Smirnoff 
1899*b2d3d26fSGleb Smirnoff 	device_printf(sc->rl_dev, "watchdog timeout\n");
1900*b2d3d26fSGleb Smirnoff 	sc->rl_ifp->if_oerrors++;
1901*b2d3d26fSGleb Smirnoff 
1902*b2d3d26fSGleb Smirnoff 	rl_txeof(sc);
1903*b2d3d26fSGleb Smirnoff 	rl_rxeof(sc);
1904*b2d3d26fSGleb Smirnoff 	sc->rl_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1905*b2d3d26fSGleb Smirnoff 	rl_init_locked(sc);
1906*b2d3d26fSGleb Smirnoff }
1907*b2d3d26fSGleb Smirnoff 
1908*b2d3d26fSGleb Smirnoff /*
1909*b2d3d26fSGleb Smirnoff  * Stop the adapter and free any mbufs allocated to the
1910*b2d3d26fSGleb Smirnoff  * RX and TX lists.
1911*b2d3d26fSGleb Smirnoff  */
1912*b2d3d26fSGleb Smirnoff static void
1913*b2d3d26fSGleb Smirnoff rl_stop(struct rl_softc *sc)
1914*b2d3d26fSGleb Smirnoff {
1915*b2d3d26fSGleb Smirnoff 	register int		i;
1916*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp = sc->rl_ifp;
1917*b2d3d26fSGleb Smirnoff 
1918*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
1919*b2d3d26fSGleb Smirnoff 
1920*b2d3d26fSGleb Smirnoff 	sc->rl_watchdog_timer = 0;
1921*b2d3d26fSGleb Smirnoff 	callout_stop(&sc->rl_stat_callout);
1922*b2d3d26fSGleb Smirnoff 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1923*b2d3d26fSGleb Smirnoff 	sc->rl_flags &= ~RL_FLAG_LINK;
1924*b2d3d26fSGleb Smirnoff 
1925*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
1926*b2d3d26fSGleb Smirnoff 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
1927*b2d3d26fSGleb Smirnoff 	for (i = 0; i < RL_TIMEOUT; i++) {
1928*b2d3d26fSGleb Smirnoff 		DELAY(10);
1929*b2d3d26fSGleb Smirnoff 		if ((CSR_READ_1(sc, RL_COMMAND) &
1930*b2d3d26fSGleb Smirnoff 		    (RL_CMD_RX_ENB | RL_CMD_TX_ENB)) == 0)
1931*b2d3d26fSGleb Smirnoff 			break;
1932*b2d3d26fSGleb Smirnoff 	}
1933*b2d3d26fSGleb Smirnoff 	if (i == RL_TIMEOUT)
1934*b2d3d26fSGleb Smirnoff 		device_printf(sc->rl_dev, "Unable to stop Tx/Rx MAC\n");
1935*b2d3d26fSGleb Smirnoff 
1936*b2d3d26fSGleb Smirnoff 	/*
1937*b2d3d26fSGleb Smirnoff 	 * Free the TX list buffers.
1938*b2d3d26fSGleb Smirnoff 	 */
1939*b2d3d26fSGleb Smirnoff 	for (i = 0; i < RL_TX_LIST_CNT; i++) {
1940*b2d3d26fSGleb Smirnoff 		if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
1941*b2d3d26fSGleb Smirnoff 			if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
1942*b2d3d26fSGleb Smirnoff 				bus_dmamap_sync(sc->rl_cdata.rl_tx_tag,
1943*b2d3d26fSGleb Smirnoff 				    sc->rl_cdata.rl_tx_dmamap[i],
1944*b2d3d26fSGleb Smirnoff 				    BUS_DMASYNC_POSTWRITE);
1945*b2d3d26fSGleb Smirnoff 				bus_dmamap_unload(sc->rl_cdata.rl_tx_tag,
1946*b2d3d26fSGleb Smirnoff 				    sc->rl_cdata.rl_tx_dmamap[i]);
1947*b2d3d26fSGleb Smirnoff 				m_freem(sc->rl_cdata.rl_tx_chain[i]);
1948*b2d3d26fSGleb Smirnoff 				sc->rl_cdata.rl_tx_chain[i] = NULL;
1949*b2d3d26fSGleb Smirnoff 			}
1950*b2d3d26fSGleb Smirnoff 			CSR_WRITE_4(sc, RL_TXADDR0 + (i * sizeof(uint32_t)),
1951*b2d3d26fSGleb Smirnoff 			    0x0000000);
1952*b2d3d26fSGleb Smirnoff 		}
1953*b2d3d26fSGleb Smirnoff 	}
1954*b2d3d26fSGleb Smirnoff }
1955*b2d3d26fSGleb Smirnoff 
1956*b2d3d26fSGleb Smirnoff /*
1957*b2d3d26fSGleb Smirnoff  * Device suspend routine.  Stop the interface and save some PCI
1958*b2d3d26fSGleb Smirnoff  * settings in case the BIOS doesn't restore them properly on
1959*b2d3d26fSGleb Smirnoff  * resume.
1960*b2d3d26fSGleb Smirnoff  */
1961*b2d3d26fSGleb Smirnoff static int
1962*b2d3d26fSGleb Smirnoff rl_suspend(device_t dev)
1963*b2d3d26fSGleb Smirnoff {
1964*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
1965*b2d3d26fSGleb Smirnoff 
1966*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
1967*b2d3d26fSGleb Smirnoff 
1968*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1969*b2d3d26fSGleb Smirnoff 	rl_stop(sc);
1970*b2d3d26fSGleb Smirnoff 	rl_setwol(sc);
1971*b2d3d26fSGleb Smirnoff 	sc->suspended = 1;
1972*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
1973*b2d3d26fSGleb Smirnoff 
1974*b2d3d26fSGleb Smirnoff 	return (0);
1975*b2d3d26fSGleb Smirnoff }
1976*b2d3d26fSGleb Smirnoff 
1977*b2d3d26fSGleb Smirnoff /*
1978*b2d3d26fSGleb Smirnoff  * Device resume routine.  Restore some PCI settings in case the BIOS
1979*b2d3d26fSGleb Smirnoff  * doesn't, re-enable busmastering, and restart the interface if
1980*b2d3d26fSGleb Smirnoff  * appropriate.
1981*b2d3d26fSGleb Smirnoff  */
1982*b2d3d26fSGleb Smirnoff static int
1983*b2d3d26fSGleb Smirnoff rl_resume(device_t dev)
1984*b2d3d26fSGleb Smirnoff {
1985*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
1986*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
1987*b2d3d26fSGleb Smirnoff 	int			pmc;
1988*b2d3d26fSGleb Smirnoff 	uint16_t		pmstat;
1989*b2d3d26fSGleb Smirnoff 
1990*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
1991*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp;
1992*b2d3d26fSGleb Smirnoff 
1993*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
1994*b2d3d26fSGleb Smirnoff 
1995*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capabilities & IFCAP_WOL) != 0 &&
1996*b2d3d26fSGleb Smirnoff 	    pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) == 0) {
1997*b2d3d26fSGleb Smirnoff 		/* Disable PME and clear PME status. */
1998*b2d3d26fSGleb Smirnoff 		pmstat = pci_read_config(sc->rl_dev,
1999*b2d3d26fSGleb Smirnoff 		    pmc + PCIR_POWER_STATUS, 2);
2000*b2d3d26fSGleb Smirnoff 		if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2001*b2d3d26fSGleb Smirnoff 			pmstat &= ~PCIM_PSTAT_PMEENABLE;
2002*b2d3d26fSGleb Smirnoff 			pci_write_config(sc->rl_dev,
2003*b2d3d26fSGleb Smirnoff 			    pmc + PCIR_POWER_STATUS, pmstat, 2);
2004*b2d3d26fSGleb Smirnoff 		}
2005*b2d3d26fSGleb Smirnoff 		/*
2006*b2d3d26fSGleb Smirnoff 		 * Clear WOL matching such that normal Rx filtering
2007*b2d3d26fSGleb Smirnoff 		 * wouldn't interfere with WOL patterns.
2008*b2d3d26fSGleb Smirnoff 		 */
2009*b2d3d26fSGleb Smirnoff 		rl_clrwol(sc);
2010*b2d3d26fSGleb Smirnoff 	}
2011*b2d3d26fSGleb Smirnoff 
2012*b2d3d26fSGleb Smirnoff 	/* reinitialize interface if necessary */
2013*b2d3d26fSGleb Smirnoff 	if (ifp->if_flags & IFF_UP)
2014*b2d3d26fSGleb Smirnoff 		rl_init_locked(sc);
2015*b2d3d26fSGleb Smirnoff 
2016*b2d3d26fSGleb Smirnoff 	sc->suspended = 0;
2017*b2d3d26fSGleb Smirnoff 
2018*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
2019*b2d3d26fSGleb Smirnoff 
2020*b2d3d26fSGleb Smirnoff 	return (0);
2021*b2d3d26fSGleb Smirnoff }
2022*b2d3d26fSGleb Smirnoff 
2023*b2d3d26fSGleb Smirnoff /*
2024*b2d3d26fSGleb Smirnoff  * Stop all chip I/O so that the kernel's probe routines don't
2025*b2d3d26fSGleb Smirnoff  * get confused by errant DMAs when rebooting.
2026*b2d3d26fSGleb Smirnoff  */
2027*b2d3d26fSGleb Smirnoff static int
2028*b2d3d26fSGleb Smirnoff rl_shutdown(device_t dev)
2029*b2d3d26fSGleb Smirnoff {
2030*b2d3d26fSGleb Smirnoff 	struct rl_softc		*sc;
2031*b2d3d26fSGleb Smirnoff 
2032*b2d3d26fSGleb Smirnoff 	sc = device_get_softc(dev);
2033*b2d3d26fSGleb Smirnoff 
2034*b2d3d26fSGleb Smirnoff 	RL_LOCK(sc);
2035*b2d3d26fSGleb Smirnoff 	rl_stop(sc);
2036*b2d3d26fSGleb Smirnoff 	/*
2037*b2d3d26fSGleb Smirnoff 	 * Mark interface as down since otherwise we will panic if
2038*b2d3d26fSGleb Smirnoff 	 * interrupt comes in later on, which can happen in some
2039*b2d3d26fSGleb Smirnoff 	 * cases.
2040*b2d3d26fSGleb Smirnoff 	 */
2041*b2d3d26fSGleb Smirnoff 	sc->rl_ifp->if_flags &= ~IFF_UP;
2042*b2d3d26fSGleb Smirnoff 	rl_setwol(sc);
2043*b2d3d26fSGleb Smirnoff 	RL_UNLOCK(sc);
2044*b2d3d26fSGleb Smirnoff 
2045*b2d3d26fSGleb Smirnoff 	return (0);
2046*b2d3d26fSGleb Smirnoff }
2047*b2d3d26fSGleb Smirnoff 
2048*b2d3d26fSGleb Smirnoff static void
2049*b2d3d26fSGleb Smirnoff rl_setwol(struct rl_softc *sc)
2050*b2d3d26fSGleb Smirnoff {
2051*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
2052*b2d3d26fSGleb Smirnoff 	int			pmc;
2053*b2d3d26fSGleb Smirnoff 	uint16_t		pmstat;
2054*b2d3d26fSGleb Smirnoff 	uint8_t			v;
2055*b2d3d26fSGleb Smirnoff 
2056*b2d3d26fSGleb Smirnoff 	RL_LOCK_ASSERT(sc);
2057*b2d3d26fSGleb Smirnoff 
2058*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp;
2059*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capabilities & IFCAP_WOL) == 0)
2060*b2d3d26fSGleb Smirnoff 		return;
2061*b2d3d26fSGleb Smirnoff 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
2062*b2d3d26fSGleb Smirnoff 		return;
2063*b2d3d26fSGleb Smirnoff 
2064*b2d3d26fSGleb Smirnoff 	/* Enable config register write. */
2065*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
2066*b2d3d26fSGleb Smirnoff 
2067*b2d3d26fSGleb Smirnoff 	/* Enable PME. */
2068*b2d3d26fSGleb Smirnoff 	v = CSR_READ_1(sc, sc->rl_cfg1);
2069*b2d3d26fSGleb Smirnoff 	v &= ~RL_CFG1_PME;
2070*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2071*b2d3d26fSGleb Smirnoff 		v |= RL_CFG1_PME;
2072*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg1, v);
2073*b2d3d26fSGleb Smirnoff 
2074*b2d3d26fSGleb Smirnoff 	v = CSR_READ_1(sc, sc->rl_cfg3);
2075*b2d3d26fSGleb Smirnoff 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
2076*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2077*b2d3d26fSGleb Smirnoff 		v |= RL_CFG3_WOL_MAGIC;
2078*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
2079*b2d3d26fSGleb Smirnoff 
2080*b2d3d26fSGleb Smirnoff 	v = CSR_READ_1(sc, sc->rl_cfg5);
2081*b2d3d26fSGleb Smirnoff 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
2082*b2d3d26fSGleb Smirnoff 	v &= ~RL_CFG5_WOL_LANWAKE;
2083*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2084*b2d3d26fSGleb Smirnoff 		v |= RL_CFG5_WOL_UCAST;
2085*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2086*b2d3d26fSGleb Smirnoff 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
2087*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2088*b2d3d26fSGleb Smirnoff 		v |= RL_CFG5_WOL_LANWAKE;
2089*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
2090*b2d3d26fSGleb Smirnoff 
2091*b2d3d26fSGleb Smirnoff 	/* Config register write done. */
2092*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2093*b2d3d26fSGleb Smirnoff 
2094*b2d3d26fSGleb Smirnoff 	/* Request PME if WOL is requested. */
2095*b2d3d26fSGleb Smirnoff 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
2096*b2d3d26fSGleb Smirnoff 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2097*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2098*b2d3d26fSGleb Smirnoff 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2099*b2d3d26fSGleb Smirnoff 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
2100*b2d3d26fSGleb Smirnoff }
2101*b2d3d26fSGleb Smirnoff 
2102*b2d3d26fSGleb Smirnoff static void
2103*b2d3d26fSGleb Smirnoff rl_clrwol(struct rl_softc *sc)
2104*b2d3d26fSGleb Smirnoff {
2105*b2d3d26fSGleb Smirnoff 	struct ifnet		*ifp;
2106*b2d3d26fSGleb Smirnoff 	uint8_t			v;
2107*b2d3d26fSGleb Smirnoff 
2108*b2d3d26fSGleb Smirnoff 	ifp = sc->rl_ifp;
2109*b2d3d26fSGleb Smirnoff 	if ((ifp->if_capabilities & IFCAP_WOL) == 0)
2110*b2d3d26fSGleb Smirnoff 		return;
2111*b2d3d26fSGleb Smirnoff 
2112*b2d3d26fSGleb Smirnoff 	/* Enable config register write. */
2113*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
2114*b2d3d26fSGleb Smirnoff 
2115*b2d3d26fSGleb Smirnoff 	v = CSR_READ_1(sc, sc->rl_cfg3);
2116*b2d3d26fSGleb Smirnoff 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
2117*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
2118*b2d3d26fSGleb Smirnoff 
2119*b2d3d26fSGleb Smirnoff 	/* Config register write done. */
2120*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2121*b2d3d26fSGleb Smirnoff 
2122*b2d3d26fSGleb Smirnoff 	v = CSR_READ_1(sc, sc->rl_cfg5);
2123*b2d3d26fSGleb Smirnoff 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
2124*b2d3d26fSGleb Smirnoff 	v &= ~RL_CFG5_WOL_LANWAKE;
2125*b2d3d26fSGleb Smirnoff 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
2126*b2d3d26fSGleb Smirnoff }
2127