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