xref: /freebsd/sys/dev/re/if_re.c (revision a0637caa3fa0775395471556cfc5658a6c83650d)
1098ca2bdSWarner Losh /*-
2a94100faSBill Paul  * Copyright (c) 1997, 1998-2003
3a94100faSBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4a94100faSBill Paul  *
5a94100faSBill Paul  * Redistribution and use in source and binary forms, with or without
6a94100faSBill Paul  * modification, are permitted provided that the following conditions
7a94100faSBill Paul  * are met:
8a94100faSBill Paul  * 1. Redistributions of source code must retain the above copyright
9a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer.
10a94100faSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer in the
12a94100faSBill Paul  *    documentation and/or other materials provided with the distribution.
13a94100faSBill Paul  * 3. All advertising materials mentioning features or use of this software
14a94100faSBill Paul  *    must display the following acknowledgement:
15a94100faSBill Paul  *	This product includes software developed by Bill Paul.
16a94100faSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17a94100faSBill Paul  *    may be used to endorse or promote products derived from this software
18a94100faSBill Paul  *    without specific prior written permission.
19a94100faSBill Paul  *
20a94100faSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21a94100faSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a94100faSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a94100faSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24a94100faSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25a94100faSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26a94100faSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27a94100faSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28a94100faSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a94100faSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30a94100faSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
31a94100faSBill Paul  */
32a94100faSBill Paul 
334dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
354dc52c32SDavid E. O'Brien 
36a94100faSBill Paul /*
37ed510fb0SBill Paul  * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
38a94100faSBill Paul  *
39a94100faSBill Paul  * Written by Bill Paul <wpaul@windriver.com>
40a94100faSBill Paul  * Senior Networking Software Engineer
41a94100faSBill Paul  * Wind River Systems
42a94100faSBill Paul  */
43a94100faSBill Paul 
44a94100faSBill Paul /*
45a94100faSBill Paul  * This driver is designed to support RealTek's next generation of
46a94100faSBill Paul  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
47ed510fb0SBill Paul  * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
48ed510fb0SBill Paul  * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
49a94100faSBill Paul  *
50a94100faSBill Paul  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
51a94100faSBill Paul  * with the older 8139 family, however it also supports a special
52a94100faSBill Paul  * C+ mode of operation that provides several new performance enhancing
53a94100faSBill Paul  * features. These include:
54a94100faSBill Paul  *
55a94100faSBill Paul  *	o Descriptor based DMA mechanism. Each descriptor represents
56a94100faSBill Paul  *	  a single packet fragment. Data buffers may be aligned on
57a94100faSBill Paul  *	  any byte boundary.
58a94100faSBill Paul  *
59a94100faSBill Paul  *	o 64-bit DMA
60a94100faSBill Paul  *
61a94100faSBill Paul  *	o TCP/IP checksum offload for both RX and TX
62a94100faSBill Paul  *
63a94100faSBill Paul  *	o High and normal priority transmit DMA rings
64a94100faSBill Paul  *
65a94100faSBill Paul  *	o VLAN tag insertion and extraction
66a94100faSBill Paul  *
67a94100faSBill Paul  *	o TCP large send (segmentation offload)
68a94100faSBill Paul  *
69a94100faSBill Paul  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
70a94100faSBill Paul  * programming API is fairly straightforward. The RX filtering, EEPROM
71a94100faSBill Paul  * access and PHY access is the same as it is on the older 8139 series
72a94100faSBill Paul  * chips.
73a94100faSBill Paul  *
74a94100faSBill Paul  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
75a94100faSBill Paul  * same programming API and feature set as the 8139C+ with the following
76a94100faSBill Paul  * differences and additions:
77a94100faSBill Paul  *
78a94100faSBill Paul  *	o 1000Mbps mode
79a94100faSBill Paul  *
80a94100faSBill Paul  *	o Jumbo frames
81a94100faSBill Paul  *
82a94100faSBill Paul  *	o GMII and TBI ports/registers for interfacing with copper
83a94100faSBill Paul  *	  or fiber PHYs
84a94100faSBill Paul  *
85a94100faSBill Paul  *	o RX and TX DMA rings can have up to 1024 descriptors
86a94100faSBill Paul  *	  (the 8139C+ allows a maximum of 64)
87a94100faSBill Paul  *
88a94100faSBill Paul  *	o Slight differences in register layout from the 8139C+
89a94100faSBill Paul  *
90a94100faSBill Paul  * The TX start and timer interrupt registers are at different locations
91a94100faSBill Paul  * on the 8169 than they are on the 8139C+. Also, the status word in the
92a94100faSBill Paul  * RX descriptor has a slightly different bit layout. The 8169 does not
93a94100faSBill Paul  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
94a94100faSBill Paul  * copper gigE PHY.
95a94100faSBill Paul  *
96a94100faSBill Paul  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
97a94100faSBill Paul  * (the 'S' stands for 'single-chip'). These devices have the same
98a94100faSBill Paul  * programming API as the older 8169, but also have some vendor-specific
99a94100faSBill Paul  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
100a94100faSBill Paul  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
101a94100faSBill Paul  *
102a94100faSBill Paul  * This driver takes advantage of the RX and TX checksum offload and
103a94100faSBill Paul  * VLAN tag insertion/extraction features. It also implements TX
104a94100faSBill Paul  * interrupt moderation using the timer interrupt registers, which
105a94100faSBill Paul  * significantly reduces TX interrupt load. There is also support
106a94100faSBill Paul  * for jumbo frames, however the 8169/8169S/8110S can not transmit
10722a11c96SJohn-Mark Gurney  * jumbo frames larger than 7440, so the max MTU possible with this
10822a11c96SJohn-Mark Gurney  * driver is 7422 bytes.
109a94100faSBill Paul  */
110a94100faSBill Paul 
111f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
112f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
113f0796cd2SGleb Smirnoff #endif
114f0796cd2SGleb Smirnoff 
115a94100faSBill Paul #include <sys/param.h>
116a94100faSBill Paul #include <sys/endian.h>
117a94100faSBill Paul #include <sys/systm.h>
118a94100faSBill Paul #include <sys/sockio.h>
119a94100faSBill Paul #include <sys/mbuf.h>
120a94100faSBill Paul #include <sys/malloc.h>
121fe12f24bSPoul-Henning Kamp #include <sys/module.h>
122a94100faSBill Paul #include <sys/kernel.h>
123a94100faSBill Paul #include <sys/socket.h>
124ed510fb0SBill Paul #include <sys/lock.h>
125ed510fb0SBill Paul #include <sys/mutex.h>
126ed510fb0SBill Paul #include <sys/taskqueue.h>
127a94100faSBill Paul 
128a94100faSBill Paul #include <net/if.h>
129a94100faSBill Paul #include <net/if_arp.h>
130a94100faSBill Paul #include <net/ethernet.h>
131a94100faSBill Paul #include <net/if_dl.h>
132a94100faSBill Paul #include <net/if_media.h>
133fc74a9f9SBrooks Davis #include <net/if_types.h>
134a94100faSBill Paul #include <net/if_vlan_var.h>
135a94100faSBill Paul 
136a94100faSBill Paul #include <net/bpf.h>
137a94100faSBill Paul 
138a94100faSBill Paul #include <machine/bus.h>
139a94100faSBill Paul #include <machine/resource.h>
140a94100faSBill Paul #include <sys/bus.h>
141a94100faSBill Paul #include <sys/rman.h>
142a94100faSBill Paul 
143a94100faSBill Paul #include <dev/mii/mii.h>
144a94100faSBill Paul #include <dev/mii/miivar.h>
145a94100faSBill Paul 
146a94100faSBill Paul #include <dev/pci/pcireg.h>
147a94100faSBill Paul #include <dev/pci/pcivar.h>
148a94100faSBill Paul 
149a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
150a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
151a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
152a94100faSBill Paul 
153298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
154a94100faSBill Paul #include "miibus_if.h"
155a94100faSBill Paul 
156a94100faSBill Paul /*
157a94100faSBill Paul  * Default to using PIO access for this driver.
158a94100faSBill Paul  */
159a94100faSBill Paul #define RE_USEIOSPACE
160a94100faSBill Paul 
161a94100faSBill Paul #include <pci/if_rlreg.h>
162a94100faSBill Paul 
1635774c5ffSPyun YongHyeon /* Tunables. */
1645774c5ffSPyun YongHyeon static int msi_disable = 0;
1655774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1665774c5ffSPyun YongHyeon 
167a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
168a94100faSBill Paul 
169a94100faSBill Paul /*
170a94100faSBill Paul  * Various supported device vendors/types and their names.
171a94100faSBill Paul  */
172a94100faSBill Paul static struct rl_type re_devs[] = {
17332aa5f0eSAnton Berezin 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169S,
17432aa5f0eSAnton Berezin 		"D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1753b9982e5SRemko Lodder 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169_8110SB,
1763b9982e5SRemko Lodder 		"D-Link DGE-528(T) Rev.B1 Gigabit Ethernet Adapter" },
177a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
178a94100faSBill Paul 		"RealTek 8139C+ 10/100BaseTX" },
179ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8101E, RL_HWREV_8101E,
180ed510fb0SBill Paul 		"RealTek 8101E PCIe 10/100baseTX" },
181498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN1,
182498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
183498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN2,
184498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
1851acbb78aSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN3,
1861acbb78aSPyun YongHyeon 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
187a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
188a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
18969a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
19069a6b7fbSBill Paul 		"RealTek 8169S Single-chip Gigabit Ethernet" },
191ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB,
192ed510fb0SBill Paul 		"RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" },
1932ee2c3b4SRemko Lodder 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC,
1942ee2c3b4SRemko Lodder 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
195498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169SC, RL_HWREV_8169_8110SC,
196ed510fb0SBill Paul 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
19769a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
19869a6b7fbSBill Paul 		"RealTek 8110S Single-chip Gigabit Ethernet" },
199ea263191SMIHIRA Sanpei Yoshiro 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
200ea263191SMIHIRA Sanpei Yoshiro 		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
20126390635SJohn Baldwin 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S,
20226390635SJohn Baldwin 		"Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
2030fc4974fSBill Paul 	{ USR_VENDORID, USR_DEVICEID_997902, RL_HWREV_8169S,
2040fc4974fSBill Paul 		"US Robotics 997902 (RTL8169S) Gigabit Ethernet" },
205a94100faSBill Paul 	{ 0, 0, 0, NULL }
206a94100faSBill Paul };
207a94100faSBill Paul 
208a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
209a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
210a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
211a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
212a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
213a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
214a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
215a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
216a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
217498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
218a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
21969a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
22069a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
221ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
222ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
223a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
224a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
225ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
226ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
227498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2281acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
229a94100faSBill Paul 	{ 0, 0, NULL }
230a94100faSBill Paul };
231a94100faSBill Paul 
232a94100faSBill Paul static int re_probe		(device_t);
233a94100faSBill Paul static int re_attach		(device_t);
234a94100faSBill Paul static int re_detach		(device_t);
235a94100faSBill Paul 
23680a2a305SJohn-Mark Gurney static int re_encap		(struct rl_softc *, struct mbuf **, int *);
237a94100faSBill Paul 
238a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
239a94100faSBill Paul static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
240a94100faSBill Paul 				    bus_size_t, int);
241a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
242a94100faSBill Paul static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
243a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
244a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
24522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24622a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
24722a11c96SJohn-Mark Gurney 				(struct mbuf *);
24822a11c96SJohn-Mark Gurney #endif
249ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
250a94100faSBill Paul static void re_txeof		(struct rl_softc *);
25197b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2520187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2530187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
25497b9d4baSJohn-Mark Gurney #endif
255ef544f63SPaolo Pisati static int re_intr		(void *);
256a94100faSBill Paul static void re_tick		(void *);
257ed510fb0SBill Paul static void re_tx_task		(void *, int);
258ed510fb0SBill Paul static void re_int_task		(void *, int);
259a94100faSBill Paul static void re_start		(struct ifnet *);
260a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
261a94100faSBill Paul static void re_init		(void *);
26297b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
263a94100faSBill Paul static void re_stop		(struct rl_softc *);
2641d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
265a94100faSBill Paul static int re_suspend		(device_t);
266a94100faSBill Paul static int re_resume		(device_t);
2676a087a87SPyun YongHyeon static int re_shutdown		(device_t);
268a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
269a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
270a94100faSBill Paul 
271a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
272a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
273ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
274a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
275a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
276a94100faSBill Paul 
277a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
278a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
279a94100faSBill Paul static void re_miibus_statchg	(device_t);
280a94100faSBill Paul 
281a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
282a94100faSBill Paul static void re_reset		(struct rl_softc *);
283a94100faSBill Paul 
284ed510fb0SBill Paul #ifdef RE_DIAG
285a94100faSBill Paul static int re_diag		(struct rl_softc *);
286ed510fb0SBill Paul #endif
287a94100faSBill Paul 
288a94100faSBill Paul #ifdef RE_USEIOSPACE
289a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
290a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
291a94100faSBill Paul #else
292a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
293a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
294a94100faSBill Paul #endif
295a94100faSBill Paul 
296a94100faSBill Paul static device_method_t re_methods[] = {
297a94100faSBill Paul 	/* Device interface */
298a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
299a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
300a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
301a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
302a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
303a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
304a94100faSBill Paul 
305a94100faSBill Paul 	/* bus interface */
306a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
307a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
308a94100faSBill Paul 
309a94100faSBill Paul 	/* MII interface */
310a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
311a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
312a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
313a94100faSBill Paul 
314a94100faSBill Paul 	{ 0, 0 }
315a94100faSBill Paul };
316a94100faSBill Paul 
317a94100faSBill Paul static driver_t re_driver = {
318a94100faSBill Paul 	"re",
319a94100faSBill Paul 	re_methods,
320a94100faSBill Paul 	sizeof(struct rl_softc)
321a94100faSBill Paul };
322a94100faSBill Paul 
323a94100faSBill Paul static devclass_t re_devclass;
324a94100faSBill Paul 
325a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
326347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
327a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
328a94100faSBill Paul 
329a94100faSBill Paul #define EE_SET(x)					\
330a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
331a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
332a94100faSBill Paul 
333a94100faSBill Paul #define EE_CLR(x)					\
334a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
335a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
336a94100faSBill Paul 
337a94100faSBill Paul /*
338a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
339a94100faSBill Paul  */
340a94100faSBill Paul static void
341a94100faSBill Paul re_eeprom_putbyte(sc, addr)
342a94100faSBill Paul 	struct rl_softc		*sc;
343a94100faSBill Paul 	int			addr;
344a94100faSBill Paul {
345a94100faSBill Paul 	register int		d, i;
346a94100faSBill Paul 
347ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
348a94100faSBill Paul 
349a94100faSBill Paul 	/*
350a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
351a94100faSBill Paul 	 */
352ed510fb0SBill Paul 
353ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
354a94100faSBill Paul 		if (d & i) {
355a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
356a94100faSBill Paul 		} else {
357a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
358a94100faSBill Paul 		}
359a94100faSBill Paul 		DELAY(100);
360a94100faSBill Paul 		EE_SET(RL_EE_CLK);
361a94100faSBill Paul 		DELAY(150);
362a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
363a94100faSBill Paul 		DELAY(100);
364a94100faSBill Paul 	}
365ed510fb0SBill Paul 
366ed510fb0SBill Paul 	return;
367a94100faSBill Paul }
368a94100faSBill Paul 
369a94100faSBill Paul /*
370a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
371a94100faSBill Paul  */
372a94100faSBill Paul static void
373a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
374a94100faSBill Paul 	struct rl_softc		*sc;
375a94100faSBill Paul 	int			addr;
376a94100faSBill Paul 	u_int16_t		*dest;
377a94100faSBill Paul {
378a94100faSBill Paul 	register int		i;
379a94100faSBill Paul 	u_int16_t		word = 0;
380a94100faSBill Paul 
381a94100faSBill Paul 	/*
382a94100faSBill Paul 	 * Send address of word we want to read.
383a94100faSBill Paul 	 */
384a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
385a94100faSBill Paul 
386a94100faSBill Paul 	/*
387a94100faSBill Paul 	 * Start reading bits from EEPROM.
388a94100faSBill Paul 	 */
389a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
390a94100faSBill Paul 		EE_SET(RL_EE_CLK);
391a94100faSBill Paul 		DELAY(100);
392a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
393a94100faSBill Paul 			word |= i;
394a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
395a94100faSBill Paul 		DELAY(100);
396a94100faSBill Paul 	}
397a94100faSBill Paul 
398a94100faSBill Paul 	*dest = word;
399ed510fb0SBill Paul 
400ed510fb0SBill Paul 	return;
401a94100faSBill Paul }
402a94100faSBill Paul 
403a94100faSBill Paul /*
404a94100faSBill Paul  * Read a sequence of words from the EEPROM.
405a94100faSBill Paul  */
406a94100faSBill Paul static void
407ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
408a94100faSBill Paul 	struct rl_softc		*sc;
409a94100faSBill Paul 	caddr_t			dest;
410a94100faSBill Paul 	int			off;
411a94100faSBill Paul 	int			cnt;
412a94100faSBill Paul {
413a94100faSBill Paul 	int			i;
414a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
415a94100faSBill Paul 
416ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
417ed510fb0SBill Paul 
418ed510fb0SBill Paul         DELAY(100);
419ed510fb0SBill Paul 
420a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
421ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
422a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
423ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
424a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
425be099007SPyun YongHyeon                 *ptr = word;
426a94100faSBill Paul 	}
427ed510fb0SBill Paul 
428ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
429ed510fb0SBill Paul 
430ed510fb0SBill Paul 	return;
431a94100faSBill Paul }
432a94100faSBill Paul 
433a94100faSBill Paul static int
434a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
435a94100faSBill Paul 	device_t		dev;
436a94100faSBill Paul 	int			phy, reg;
437a94100faSBill Paul {
438a94100faSBill Paul 	struct rl_softc		*sc;
439a94100faSBill Paul 	u_int32_t		rval;
440a94100faSBill Paul 	int			i;
441a94100faSBill Paul 
442a94100faSBill Paul 	if (phy != 1)
443a94100faSBill Paul 		return (0);
444a94100faSBill Paul 
445a94100faSBill Paul 	sc = device_get_softc(dev);
446a94100faSBill Paul 
4479bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4489bac70b8SBill Paul 
4499bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4509bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4519bac70b8SBill Paul 		return (rval);
4529bac70b8SBill Paul 	}
4539bac70b8SBill Paul 
454a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
455a94100faSBill Paul 	DELAY(1000);
456a94100faSBill Paul 
457a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
458a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
459a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
460a94100faSBill Paul 			break;
461a94100faSBill Paul 		DELAY(100);
462a94100faSBill Paul 	}
463a94100faSBill Paul 
464a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4656b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
466a94100faSBill Paul 		return (0);
467a94100faSBill Paul 	}
468a94100faSBill Paul 
469a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
470a94100faSBill Paul }
471a94100faSBill Paul 
472a94100faSBill Paul static int
473a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
474a94100faSBill Paul 	device_t		dev;
475a94100faSBill Paul 	int			phy, reg, data;
476a94100faSBill Paul {
477a94100faSBill Paul 	struct rl_softc		*sc;
478a94100faSBill Paul 	u_int32_t		rval;
479a94100faSBill Paul 	int			i;
480a94100faSBill Paul 
481a94100faSBill Paul 	sc = device_get_softc(dev);
482a94100faSBill Paul 
483a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4849bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
485a94100faSBill Paul 	DELAY(1000);
486a94100faSBill Paul 
487a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
488a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
489a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
490a94100faSBill Paul 			break;
491a94100faSBill Paul 		DELAY(100);
492a94100faSBill Paul 	}
493a94100faSBill Paul 
494a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4956b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
496a94100faSBill Paul 		return (0);
497a94100faSBill Paul 	}
498a94100faSBill Paul 
499a94100faSBill Paul 	return (0);
500a94100faSBill Paul }
501a94100faSBill Paul 
502a94100faSBill Paul static int
503a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
504a94100faSBill Paul 	device_t		dev;
505a94100faSBill Paul 	int			phy, reg;
506a94100faSBill Paul {
507a94100faSBill Paul 	struct rl_softc		*sc;
508a94100faSBill Paul 	u_int16_t		rval = 0;
509a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
510a94100faSBill Paul 
511a94100faSBill Paul 	sc = device_get_softc(dev);
512a94100faSBill Paul 
513a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
514a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
515a94100faSBill Paul 		return (rval);
516a94100faSBill Paul 	}
517a94100faSBill Paul 
518a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
519a94100faSBill Paul 	if (phy) {
520a94100faSBill Paul 		return (0);
521a94100faSBill Paul 	}
522a94100faSBill Paul 	switch (reg) {
523a94100faSBill Paul 	case MII_BMCR:
524a94100faSBill Paul 		re8139_reg = RL_BMCR;
525a94100faSBill Paul 		break;
526a94100faSBill Paul 	case MII_BMSR:
527a94100faSBill Paul 		re8139_reg = RL_BMSR;
528a94100faSBill Paul 		break;
529a94100faSBill Paul 	case MII_ANAR:
530a94100faSBill Paul 		re8139_reg = RL_ANAR;
531a94100faSBill Paul 		break;
532a94100faSBill Paul 	case MII_ANER:
533a94100faSBill Paul 		re8139_reg = RL_ANER;
534a94100faSBill Paul 		break;
535a94100faSBill Paul 	case MII_ANLPAR:
536a94100faSBill Paul 		re8139_reg = RL_LPAR;
537a94100faSBill Paul 		break;
538a94100faSBill Paul 	case MII_PHYIDR1:
539a94100faSBill Paul 	case MII_PHYIDR2:
540a94100faSBill Paul 		return (0);
541a94100faSBill Paul 	/*
542a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
543a94100faSBill Paul 	 * register. If we have a link partner which does not
544a94100faSBill Paul 	 * support NWAY, this is the register which will tell
545a94100faSBill Paul 	 * us the results of parallel detection.
546a94100faSBill Paul 	 */
547a94100faSBill Paul 	case RL_MEDIASTAT:
548a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
549a94100faSBill Paul 		return (rval);
550a94100faSBill Paul 	default:
5516b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
552a94100faSBill Paul 		return (0);
553a94100faSBill Paul 	}
554a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
555baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
556baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
557baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
558baa12772SPyun YongHyeon 	}
559a94100faSBill Paul 	return (rval);
560a94100faSBill Paul }
561a94100faSBill Paul 
562a94100faSBill Paul static int
563a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
564a94100faSBill Paul 	device_t		dev;
565a94100faSBill Paul 	int			phy, reg, data;
566a94100faSBill Paul {
567a94100faSBill Paul 	struct rl_softc		*sc;
568a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
569a94100faSBill Paul 	int			rval = 0;
570a94100faSBill Paul 
571a94100faSBill Paul 	sc = device_get_softc(dev);
572a94100faSBill Paul 
573a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
574a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
575a94100faSBill Paul 		return (rval);
576a94100faSBill Paul 	}
577a94100faSBill Paul 
578a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
57997b9d4baSJohn-Mark Gurney 	if (phy)
580a94100faSBill Paul 		return (0);
58197b9d4baSJohn-Mark Gurney 
582a94100faSBill Paul 	switch (reg) {
583a94100faSBill Paul 	case MII_BMCR:
584a94100faSBill Paul 		re8139_reg = RL_BMCR;
585baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
586baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
587baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
588baa12772SPyun YongHyeon 		}
589a94100faSBill Paul 		break;
590a94100faSBill Paul 	case MII_BMSR:
591a94100faSBill Paul 		re8139_reg = RL_BMSR;
592a94100faSBill Paul 		break;
593a94100faSBill Paul 	case MII_ANAR:
594a94100faSBill Paul 		re8139_reg = RL_ANAR;
595a94100faSBill Paul 		break;
596a94100faSBill Paul 	case MII_ANER:
597a94100faSBill Paul 		re8139_reg = RL_ANER;
598a94100faSBill Paul 		break;
599a94100faSBill Paul 	case MII_ANLPAR:
600a94100faSBill Paul 		re8139_reg = RL_LPAR;
601a94100faSBill Paul 		break;
602a94100faSBill Paul 	case MII_PHYIDR1:
603a94100faSBill Paul 	case MII_PHYIDR2:
604a94100faSBill Paul 		return (0);
605a94100faSBill Paul 		break;
606a94100faSBill Paul 	default:
6076b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
608a94100faSBill Paul 		return (0);
609a94100faSBill Paul 	}
610a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
611a94100faSBill Paul 	return (0);
612a94100faSBill Paul }
613a94100faSBill Paul 
614a94100faSBill Paul static void
615a94100faSBill Paul re_miibus_statchg(dev)
616a94100faSBill Paul 	device_t		dev;
617a94100faSBill Paul {
618a11e2f18SBruce M Simpson 
619a94100faSBill Paul }
620a94100faSBill Paul 
621a94100faSBill Paul /*
622a94100faSBill Paul  * Program the 64-bit multicast hash filter.
623a94100faSBill Paul  */
624a94100faSBill Paul static void
625a94100faSBill Paul re_setmulti(sc)
626a94100faSBill Paul 	struct rl_softc		*sc;
627a94100faSBill Paul {
628a94100faSBill Paul 	struct ifnet		*ifp;
629a94100faSBill Paul 	int			h = 0;
630a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
631a94100faSBill Paul 	struct ifmultiaddr	*ifma;
632a94100faSBill Paul 	u_int32_t		rxfilt;
633a94100faSBill Paul 	int			mcnt = 0;
634bb7dfefbSBill Paul 	u_int32_t		hwrev;
635a94100faSBill Paul 
63697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
63797b9d4baSJohn-Mark Gurney 
638fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
639a94100faSBill Paul 
640a94100faSBill Paul 
6417c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6427c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
643a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
6447c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6457c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
646a0637caaSPyun YongHyeon 		/*
647a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
648a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
649a0637caaSPyun YongHyeon 		 * promiscuous mode.
650a0637caaSPyun YongHyeon 		 */
651a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
652a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
653a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
654a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
655a94100faSBill Paul 		return;
656a94100faSBill Paul 	}
657a94100faSBill Paul 
658a94100faSBill Paul 	/* first, zot all the existing hash bits */
659a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
660a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
661a94100faSBill Paul 
662a94100faSBill Paul 	/* now program new ones */
66313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
664a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
665a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
666a94100faSBill Paul 			continue;
6670e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6680e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
669a94100faSBill Paul 		if (h < 32)
670a94100faSBill Paul 			hashes[0] |= (1 << h);
671a94100faSBill Paul 		else
672a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
673a94100faSBill Paul 		mcnt++;
674a94100faSBill Paul 	}
67513b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
676a94100faSBill Paul 
677a94100faSBill Paul 	if (mcnt)
678a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
679a94100faSBill Paul 	else
680a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
681a94100faSBill Paul 
682a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
683bb7dfefbSBill Paul 
684bb7dfefbSBill Paul 	/*
685bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
686bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
687bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
688bb7dfefbSBill Paul 	 * order for those devices.
689bb7dfefbSBill Paul 	 */
690bb7dfefbSBill Paul 
691bb7dfefbSBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
692bb7dfefbSBill Paul 
6931acbb78aSPyun YongHyeon 	switch (hwrev) {
6941acbb78aSPyun YongHyeon 	case RL_HWREV_8100E:
6951acbb78aSPyun YongHyeon 	case RL_HWREV_8101E:
6961acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
6971acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
6981acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
699bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
700bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
7011acbb78aSPyun YongHyeon 		break;
7021acbb78aSPyun YongHyeon 	default:
703a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
704a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
7051acbb78aSPyun YongHyeon 		break;
706a94100faSBill Paul 	}
707bb7dfefbSBill Paul }
708a94100faSBill Paul 
709a94100faSBill Paul static void
710a94100faSBill Paul re_reset(sc)
711a94100faSBill Paul 	struct rl_softc		*sc;
712a94100faSBill Paul {
713a94100faSBill Paul 	register int		i;
714a94100faSBill Paul 
71597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
71697b9d4baSJohn-Mark Gurney 
717a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
718a94100faSBill Paul 
719a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
720a94100faSBill Paul 		DELAY(10);
721a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
722a94100faSBill Paul 			break;
723a94100faSBill Paul 	}
724a94100faSBill Paul 	if (i == RL_TIMEOUT)
7256b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
726a94100faSBill Paul 
727a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
728a94100faSBill Paul }
729a94100faSBill Paul 
730ed510fb0SBill Paul #ifdef RE_DIAG
731ed510fb0SBill Paul 
732a94100faSBill Paul /*
733a94100faSBill Paul  * The following routine is designed to test for a defect on some
734a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
735a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
736a94100faSBill Paul  * should be pulled high. The result of this defect is that the
737a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
738a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
739a94100faSBill Paul  * because the 64-bit data lines aren't connected.
740a94100faSBill Paul  *
741a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
742a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
743a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
744a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
745a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
746a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
747a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
748a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
749a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
750a94100faSBill Paul  */
751a94100faSBill Paul 
752a94100faSBill Paul static int
753a94100faSBill Paul re_diag(sc)
754a94100faSBill Paul 	struct rl_softc		*sc;
755a94100faSBill Paul {
756fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
757a94100faSBill Paul 	struct mbuf		*m0;
758a94100faSBill Paul 	struct ether_header	*eh;
759a94100faSBill Paul 	struct rl_desc		*cur_rx;
760a94100faSBill Paul 	u_int16_t		status;
761a94100faSBill Paul 	u_int32_t		rxstat;
762ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
763a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
764a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
765a94100faSBill Paul 
766a94100faSBill Paul 	/* Allocate a single mbuf */
767a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
768a94100faSBill Paul 	if (m0 == NULL)
769a94100faSBill Paul 		return (ENOBUFS);
770a94100faSBill Paul 
77197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
77297b9d4baSJohn-Mark Gurney 
773a94100faSBill Paul 	/*
774a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
775a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
776a94100faSBill Paul 	 * following special functions:
777a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
778a94100faSBill Paul 	 * - Enables digital loopback mode
779a94100faSBill Paul 	 * - Leaves interrupts turned off
780a94100faSBill Paul 	 */
781a94100faSBill Paul 
782a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
783a94100faSBill Paul 	sc->rl_testmode = 1;
784ed510fb0SBill Paul 	re_reset(sc);
78597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
786ed510fb0SBill Paul 	sc->rl_link = 1;
787ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
788ed510fb0SBill Paul 		phyaddr = 1;
789ed510fb0SBill Paul 	else
790ed510fb0SBill Paul 		phyaddr = 0;
791ed510fb0SBill Paul 
792ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
793ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
794ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
795ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
796ed510fb0SBill Paul 			break;
797ed510fb0SBill Paul 	}
798ed510fb0SBill Paul 
799ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
800ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
801ed510fb0SBill Paul 
802804af9a1SBill Paul 	DELAY(100000);
803a94100faSBill Paul 
804a94100faSBill Paul 	/* Put some data in the mbuf */
805a94100faSBill Paul 
806a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
807a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
808a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
809a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
810a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
811a94100faSBill Paul 
8127cae6651SBill Paul 	/*
8137cae6651SBill Paul 	 * Queue the packet, start transmission.
8147cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8157cae6651SBill Paul 	 */
816a94100faSBill Paul 
817abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
81897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
81952732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8207cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
82197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
822a94100faSBill Paul 	m0 = NULL;
823a94100faSBill Paul 
824a94100faSBill Paul 	/* Wait for it to propagate through the chip */
825a94100faSBill Paul 
826abc8ff44SBill Paul 	DELAY(100000);
827a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
828a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
829ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
830abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
831abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
832a94100faSBill Paul 			break;
833a94100faSBill Paul 		DELAY(10);
834a94100faSBill Paul 	}
835a94100faSBill Paul 
836a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8376b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8386b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8396b9f5c94SGleb Smirnoff 		    " loopback mode\n");
840a94100faSBill Paul 		error = EIO;
841a94100faSBill Paul 		goto done;
842a94100faSBill Paul 	}
843a94100faSBill Paul 
844a94100faSBill Paul 	/*
845a94100faSBill Paul 	 * The packet should have been dumped into the first
846a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
847a94100faSBill Paul 	 */
848a94100faSBill Paul 
849a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
850a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
851a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
852a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
853a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0],
854a94100faSBill Paul 	    BUS_DMASYNC_POSTWRITE);
855a94100faSBill Paul 	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
856a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0]);
857a94100faSBill Paul 
858a94100faSBill Paul 	m0 = sc->rl_ldata.rl_rx_mbuf[0];
859a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
860a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
861a94100faSBill Paul 
862a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
863a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
864a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
865a94100faSBill Paul 
866a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8676b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8686b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
869a94100faSBill Paul 		error = EIO;
870a94100faSBill Paul 		goto done;
871a94100faSBill Paul 	}
872a94100faSBill Paul 
873a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
874a94100faSBill Paul 
875a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
876a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
877a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8786b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8796b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
880a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8816b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
882a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
883a94100faSBill Paul 		    ntohs(eh->ether_type));
8846b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8856b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8866b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8876b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8886b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8896b9f5c94SGleb Smirnoff 		    "details.\n");
890a94100faSBill Paul 		error = EIO;
891a94100faSBill Paul 	}
892a94100faSBill Paul 
893a94100faSBill Paul done:
894a94100faSBill Paul 	/* Turn interface off, release resources */
895a94100faSBill Paul 
896a94100faSBill Paul 	sc->rl_testmode = 0;
897ed510fb0SBill Paul 	sc->rl_link = 0;
898a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
899a94100faSBill Paul 	re_stop(sc);
900a94100faSBill Paul 	if (m0 != NULL)
901a94100faSBill Paul 		m_freem(m0);
902a94100faSBill Paul 
90397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
90497b9d4baSJohn-Mark Gurney 
905a94100faSBill Paul 	return (error);
906a94100faSBill Paul }
907a94100faSBill Paul 
908ed510fb0SBill Paul #endif
909ed510fb0SBill Paul 
910a94100faSBill Paul /*
911a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
912a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
913a94100faSBill Paul  */
914a94100faSBill Paul static int
915a94100faSBill Paul re_probe(dev)
916a94100faSBill Paul 	device_t		dev;
917a94100faSBill Paul {
918a94100faSBill Paul 	struct rl_type		*t;
919a94100faSBill Paul 	struct rl_softc		*sc;
920a94100faSBill Paul 	int			rid;
921a94100faSBill Paul 	u_int32_t		hwrev;
922a94100faSBill Paul 
923a94100faSBill Paul 	t = re_devs;
924a94100faSBill Paul 	sc = device_get_softc(dev);
925a94100faSBill Paul 
926a94100faSBill Paul 	while (t->rl_name != NULL) {
927a94100faSBill Paul 		if ((pci_get_vendor(dev) == t->rl_vid) &&
928a94100faSBill Paul 		    (pci_get_device(dev) == t->rl_did)) {
92926390635SJohn Baldwin 			/*
93026390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
93126390635SJohn Baldwin 			 * Rev. 2 i supported by sk(4).
93226390635SJohn Baldwin 			 */
93326390635SJohn Baldwin 			if ((t->rl_vid == LINKSYS_VENDORID) &&
93426390635SJohn Baldwin 				(t->rl_did == LINKSYS_DEVICEID_EG1032) &&
93526390635SJohn Baldwin 				(pci_get_subdevice(dev) !=
93626390635SJohn Baldwin 				LINKSYS_SUBDEVICE_EG1032_REV3)) {
93726390635SJohn Baldwin 				t++;
93826390635SJohn Baldwin 				continue;
93926390635SJohn Baldwin 			}
940a94100faSBill Paul 
941a94100faSBill Paul 			/*
942a94100faSBill Paul 			 * Temporarily map the I/O space
943a94100faSBill Paul 			 * so we can read the chip ID register.
944a94100faSBill Paul 			 */
945a94100faSBill Paul 			rid = RL_RID;
9465f96beb9SNate Lawson 			sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
9475f96beb9SNate Lawson 			    RF_ACTIVE);
948a94100faSBill Paul 			if (sc->rl_res == NULL) {
949a94100faSBill Paul 				device_printf(dev,
950a94100faSBill Paul 				    "couldn't map ports/memory\n");
951a94100faSBill Paul 				return (ENXIO);
952a94100faSBill Paul 			}
953a94100faSBill Paul 			sc->rl_btag = rman_get_bustag(sc->rl_res);
954a94100faSBill Paul 			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
955a94100faSBill Paul 			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
956a94100faSBill Paul 			bus_release_resource(dev, RL_RES,
957a94100faSBill Paul 			    RL_RID, sc->rl_res);
958a94100faSBill Paul 			if (t->rl_basetype == hwrev) {
959a94100faSBill Paul 				device_set_desc(dev, t->rl_name);
960d2b677bbSWarner Losh 				return (BUS_PROBE_DEFAULT);
961a94100faSBill Paul 			}
962a94100faSBill Paul 		}
963a94100faSBill Paul 		t++;
964a94100faSBill Paul 	}
965a94100faSBill Paul 
966a94100faSBill Paul 	return (ENXIO);
967a94100faSBill Paul }
968a94100faSBill Paul 
969a94100faSBill Paul /*
970a94100faSBill Paul  * This routine takes the segment list provided as the result of
971a94100faSBill Paul  * a bus_dma_map_load() operation and assigns the addresses/lengths
972a94100faSBill Paul  * to RealTek DMA descriptors. This can be called either by the RX
973a94100faSBill Paul  * code or the TX code. In the RX case, we'll probably wind up mapping
974a94100faSBill Paul  * at most one segment. For the TX case, there could be any number of
975a94100faSBill Paul  * segments since TX packets may span multiple mbufs. In either case,
976a94100faSBill Paul  * if the number of segments is larger than the rl_maxsegs limit
977a94100faSBill Paul  * specified by the caller, we abort the mapping operation. Sadly,
978a94100faSBill Paul  * whoever designed the buffer mapping API did not provide a way to
979a94100faSBill Paul  * return an error from here, so we have to fake it a bit.
980a94100faSBill Paul  */
981a94100faSBill Paul 
982a94100faSBill Paul static void
983a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error)
984a94100faSBill Paul 	void			*arg;
985a94100faSBill Paul 	bus_dma_segment_t	*segs;
986a94100faSBill Paul 	int			nseg;
987a94100faSBill Paul 	bus_size_t		mapsize;
988a94100faSBill Paul 	int			error;
989a94100faSBill Paul {
990a94100faSBill Paul 	struct rl_dmaload_arg	*ctx;
991a94100faSBill Paul 	struct rl_desc		*d = NULL;
992a94100faSBill Paul 	int			i = 0, idx;
993498bd0d3SBill Paul 	u_int32_t		cmdstat;
994498bd0d3SBill Paul 	int			totlen = 0;
995a94100faSBill Paul 
996a94100faSBill Paul 	if (error)
997a94100faSBill Paul 		return;
998a94100faSBill Paul 
999a94100faSBill Paul 	ctx = arg;
1000a94100faSBill Paul 
1001a94100faSBill Paul 	/* Signal error to caller if there's too many segments */
1002a94100faSBill Paul 	if (nseg > ctx->rl_maxsegs) {
1003a94100faSBill Paul 		ctx->rl_maxsegs = 0;
1004a94100faSBill Paul 		return;
1005a94100faSBill Paul 	}
1006a94100faSBill Paul 
1007a94100faSBill Paul 	/*
1008a94100faSBill Paul 	 * Map the segment array into descriptors. Note that we set the
1009a94100faSBill Paul 	 * start-of-frame and end-of-frame markers for either TX or RX, but
1010a94100faSBill Paul 	 * they really only have meaning in the TX case. (In the RX case,
1011a94100faSBill Paul 	 * it's the chip that tells us where packets begin and end.)
1012a94100faSBill Paul 	 * We also keep track of the end of the ring and set the
1013a94100faSBill Paul 	 * end-of-ring bits as needed, and we set the ownership bits
1014a94100faSBill Paul 	 * in all except the very first descriptor. (The caller will
1015a94100faSBill Paul 	 * set this descriptor later when it start transmission or
1016a94100faSBill Paul 	 * reception.)
1017a94100faSBill Paul 	 */
1018a94100faSBill Paul 	idx = ctx->rl_idx;
101959b5d934SBruce M Simpson 	for (;;) {
1020a94100faSBill Paul 		d = &ctx->rl_ring[idx];
1021a94100faSBill Paul 		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
1022a94100faSBill Paul 			ctx->rl_maxsegs = 0;
1023a94100faSBill Paul 			return;
1024a94100faSBill Paul 		}
1025a94100faSBill Paul 		cmdstat = segs[i].ds_len;
1026498bd0d3SBill Paul 		totlen += segs[i].ds_len;
102792825635SMarius Strobl 		d->rl_vlanctl = 0;
1028a94100faSBill Paul 		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
1029a94100faSBill Paul 		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
1030a94100faSBill Paul 		if (i == 0)
1031a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_SOF;
1032a94100faSBill Paul 		else
1033a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_OWN;
1034a94100faSBill Paul 		if (idx == (RL_RX_DESC_CNT - 1))
1035a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_EOR;
1036a94100faSBill Paul 		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
1037a94100faSBill Paul 		i++;
1038a94100faSBill Paul 		if (i == nseg)
1039a94100faSBill Paul 			break;
1040a94100faSBill Paul 		RL_DESC_INC(idx);
1041a94100faSBill Paul 	}
1042a94100faSBill Paul 
1043a94100faSBill Paul 	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
1044a94100faSBill Paul 	ctx->rl_maxsegs = nseg;
1045a94100faSBill Paul 	ctx->rl_idx = idx;
1046a94100faSBill Paul }
1047a94100faSBill Paul 
1048a94100faSBill Paul /*
1049a94100faSBill Paul  * Map a single buffer address.
1050a94100faSBill Paul  */
1051a94100faSBill Paul 
1052a94100faSBill Paul static void
1053a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
1054a94100faSBill Paul 	void			*arg;
1055a94100faSBill Paul 	bus_dma_segment_t	*segs;
1056a94100faSBill Paul 	int			nseg;
1057a94100faSBill Paul 	int			error;
1058a94100faSBill Paul {
10598fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
1060a94100faSBill Paul 
1061a94100faSBill Paul 	if (error)
1062a94100faSBill Paul 		return;
1063a94100faSBill Paul 
1064a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1065a94100faSBill Paul 	addr = arg;
1066a94100faSBill Paul 	*addr = segs->ds_addr;
1067a94100faSBill Paul }
1068a94100faSBill Paul 
1069a94100faSBill Paul static int
1070a94100faSBill Paul re_allocmem(dev, sc)
1071a94100faSBill Paul 	device_t		dev;
1072a94100faSBill Paul 	struct rl_softc		*sc;
1073a94100faSBill Paul {
1074a94100faSBill Paul 	int			error;
1075a94100faSBill Paul 	int			nseg;
1076a94100faSBill Paul 	int			i;
1077a94100faSBill Paul 
1078a94100faSBill Paul 	/*
1079a94100faSBill Paul 	 * Allocate map for RX mbufs.
1080a94100faSBill Paul 	 */
1081a94100faSBill Paul 	nseg = 32;
1082a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
1083a94100faSBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
10846110675fSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
1085a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_mtag);
1086a94100faSBill Paul 	if (error) {
1087a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1088a94100faSBill Paul 		return (ENOMEM);
1089a94100faSBill Paul 	}
1090a94100faSBill Paul 
1091a94100faSBill Paul 	/*
1092a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1093a94100faSBill Paul 	 */
1094a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1095a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
10961d545c7aSMarius Strobl 	    NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0,
1097a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1098a94100faSBill Paul 	if (error) {
1099a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1100a94100faSBill Paul 		return (ENOMEM);
1101a94100faSBill Paul 	}
1102a94100faSBill Paul 
1103a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1104a94100faSBill Paul 
1105a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1106a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1107a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1108a94100faSBill Paul 	if (error)
1109a94100faSBill Paul 		return (ENOMEM);
1110a94100faSBill Paul 
1111a94100faSBill Paul 	/* Load the map for the TX ring. */
1112a94100faSBill Paul 
1113a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1114a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1115a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
1116a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1117a94100faSBill Paul 
1118a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1119a94100faSBill Paul 
1120a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
1121a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1122a94100faSBill Paul 			    &sc->rl_ldata.rl_tx_dmamap[i]);
1123a94100faSBill Paul 		if (error) {
1124a94100faSBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
1125a94100faSBill Paul 			return (ENOMEM);
1126a94100faSBill Paul 		}
1127a94100faSBill Paul 	}
1128a94100faSBill Paul 
1129a94100faSBill Paul 	/*
1130a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1131a94100faSBill Paul 	 */
1132a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1133a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
11341d545c7aSMarius Strobl 	    NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, 0,
1135a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1136a94100faSBill Paul 	if (error) {
1137a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1138a94100faSBill Paul 		return (ENOMEM);
1139a94100faSBill Paul 	}
1140a94100faSBill Paul 
1141a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1142a94100faSBill Paul 
1143a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1144a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1145a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1146a94100faSBill Paul 	if (error)
1147a94100faSBill Paul 		return (ENOMEM);
1148a94100faSBill Paul 
1149a94100faSBill Paul 	/* Load the map for the RX ring. */
1150a94100faSBill Paul 
1151a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1152a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
115361021536SJohn-Mark Gurney 	     RL_RX_LIST_SZ, re_dma_map_addr,
1154a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1155a94100faSBill Paul 
1156a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1157a94100faSBill Paul 
1158a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1159a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1160a94100faSBill Paul 			    &sc->rl_ldata.rl_rx_dmamap[i]);
1161a94100faSBill Paul 		if (error) {
1162a94100faSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1163a94100faSBill Paul 			return (ENOMEM);
1164a94100faSBill Paul 		}
1165a94100faSBill Paul 	}
1166a94100faSBill Paul 
1167a94100faSBill Paul 	return (0);
1168a94100faSBill Paul }
1169a94100faSBill Paul 
1170a94100faSBill Paul /*
1171a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1172a94100faSBill Paul  * setup and ethernet/BPF attach.
1173a94100faSBill Paul  */
1174a94100faSBill Paul static int
1175a94100faSBill Paul re_attach(dev)
1176a94100faSBill Paul 	device_t		dev;
1177a94100faSBill Paul {
1178a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1179be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1180a94100faSBill Paul 	struct rl_softc		*sc;
1181a94100faSBill Paul 	struct ifnet		*ifp;
1182a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1183a94100faSBill Paul 	int			hwrev;
1184a94100faSBill Paul 	u_int16_t		re_did = 0;
1185d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11865774c5ffSPyun YongHyeon 	int			msic, reg;
1187a94100faSBill Paul 
1188a94100faSBill Paul 	sc = device_get_softc(dev);
1189ed510fb0SBill Paul 	sc->rl_dev = dev;
1190a94100faSBill Paul 
1191a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
119297b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1193d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1194d1754a9bSJohn Baldwin 
1195a94100faSBill Paul 	/*
1196a94100faSBill Paul 	 * Map control/status registers.
1197a94100faSBill Paul 	 */
1198a94100faSBill Paul 	pci_enable_busmaster(dev);
1199a94100faSBill Paul 
1200a94100faSBill Paul 	rid = RL_RID;
12015f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
12025f96beb9SNate Lawson 	    RF_ACTIVE);
1203a94100faSBill Paul 
1204a94100faSBill Paul 	if (sc->rl_res == NULL) {
1205d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1206a94100faSBill Paul 		error = ENXIO;
1207a94100faSBill Paul 		goto fail;
1208a94100faSBill Paul 	}
1209a94100faSBill Paul 
1210a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1211a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1212a94100faSBill Paul 
12135774c5ffSPyun YongHyeon 	msic = 0;
12145774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
12155774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
12165774c5ffSPyun YongHyeon 		if (bootverbose)
12175774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
12185774c5ffSPyun YongHyeon 	}
12195774c5ffSPyun YongHyeon 	if (msic == RL_MSI_MESSAGES  && msi_disable == 0) {
12205774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12215774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12225774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
12235774c5ffSPyun YongHyeon 				    msic);
12245774c5ffSPyun YongHyeon 				sc->rl_msi = 1;
12255774c5ffSPyun YongHyeon 			} else
12265774c5ffSPyun YongHyeon 				pci_release_msi(dev);
12275774c5ffSPyun YongHyeon 		}
12285774c5ffSPyun YongHyeon 	}
1229a94100faSBill Paul 
12305774c5ffSPyun YongHyeon 	/* Allocate interrupt */
12315774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
12325774c5ffSPyun YongHyeon 		rid = 0;
12335774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
12345774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
12355774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
12365774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1237a94100faSBill Paul 			error = ENXIO;
1238a94100faSBill Paul 			goto fail;
1239a94100faSBill Paul 		}
12405774c5ffSPyun YongHyeon 	} else {
12415774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
12425774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
12435774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
12445774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
12455774c5ffSPyun YongHyeon 				device_printf(dev,
12465774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12475774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12485774c5ffSPyun YongHyeon 				error = ENXIO;
12495774c5ffSPyun YongHyeon 				goto fail;
12505774c5ffSPyun YongHyeon 			}
12515774c5ffSPyun YongHyeon 		}
12525774c5ffSPyun YongHyeon 	}
1253a94100faSBill Paul 
1254a94100faSBill Paul 	/* Reset the adapter. */
125597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1256a94100faSBill Paul 	re_reset(sc);
125797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1258abc8ff44SBill Paul 
1259abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1260abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1261abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1262abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1263abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1264abc8ff44SBill Paul 			break;
1265abc8ff44SBill Paul 		}
1266abc8ff44SBill Paul 		hw_rev++;
1267abc8ff44SBill Paul 	}
1268abc8ff44SBill Paul 
1269141f92e7SPyun YongHyeon 	sc->rl_eewidth = RL_9356_ADDR_LEN;
1270ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1271a94100faSBill Paul 	if (re_did != 0x8129)
1272141f92e7SPyun YongHyeon 	        sc->rl_eewidth = RL_9346_ADDR_LEN;
1273a94100faSBill Paul 
1274a94100faSBill Paul 	/*
1275a94100faSBill Paul 	 * Get station address from the EEPROM.
1276a94100faSBill Paul 	 */
1277ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1278be099007SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1279be099007SPyun YongHyeon 		as[i] = le16toh(as[i]);
1280be099007SPyun YongHyeon 	bcopy(as, eaddr, sizeof(eaddr));
1281ed510fb0SBill Paul 
1282ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1283ed510fb0SBill Paul 		/* Set RX length mask */
1284ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1285ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1286ed510fb0SBill Paul 	} else {
1287ed510fb0SBill Paul 		/* Set RX length mask */
1288ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1289ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1290abc8ff44SBill Paul 	}
12919bac70b8SBill Paul 
1292a94100faSBill Paul 	/*
1293a94100faSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1294a94100faSBill Paul 	 */
1295a94100faSBill Paul #define RL_NSEG_NEW 32
12961d545c7aSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
12971d545c7aSMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
12981d545c7aSMarius Strobl 	    MAXBSIZE, RL_NSEG_NEW, BUS_SPACE_MAXSIZE_32BIT, 0,
12991d545c7aSMarius Strobl 	    NULL, NULL, &sc->rl_parent_tag);
1300a94100faSBill Paul 	if (error)
1301a94100faSBill Paul 		goto fail;
1302a94100faSBill Paul 
1303a94100faSBill Paul 	error = re_allocmem(dev, sc);
1304a94100faSBill Paul 
1305a94100faSBill Paul 	if (error)
1306a94100faSBill Paul 		goto fail;
1307a94100faSBill Paul 
1308cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1309cd036ec1SBrooks Davis 	if (ifp == NULL) {
1310d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1311cd036ec1SBrooks Davis 		error = ENOSPC;
1312cd036ec1SBrooks Davis 		goto fail;
1313cd036ec1SBrooks Davis 	}
1314cd036ec1SBrooks Davis 
1315a94100faSBill Paul 	/* Do MII setup */
1316a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1317a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1318d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1319a94100faSBill Paul 		error = ENXIO;
1320a94100faSBill Paul 		goto fail;
1321a94100faSBill Paul 	}
1322a94100faSBill Paul 
1323c4aca09aSPyun YongHyeon 	/* Take PHY out of power down mode. */
1324c4aca09aSPyun YongHyeon 	if (sc->rl_type == RL_8169) {
1325c4aca09aSPyun YongHyeon 		uint32_t rev;
1326c4aca09aSPyun YongHyeon 
1327c4aca09aSPyun YongHyeon 		rev = CSR_READ_4(sc, RL_TXCFG);
1328c4aca09aSPyun YongHyeon 		/* HWVERID 0, 1 and 2 :  bit26-30, bit23 */
1329c4aca09aSPyun YongHyeon 		rev &= 0x7c800000;
1330c4aca09aSPyun YongHyeon 		if (rev != 0) {
1331c4aca09aSPyun YongHyeon 			/* RTL8169S single chip */
1332c4aca09aSPyun YongHyeon 			switch (rev) {
1333c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SB:
1334c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SC:
1335c4aca09aSPyun YongHyeon 			case RL_HWREV_8168_SPIN2:
13361acbb78aSPyun YongHyeon 			case RL_HWREV_8168_SPIN3:
1337c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x1f, 0);
1338c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x0e, 0);
1339c4aca09aSPyun YongHyeon 				break;
1340c4aca09aSPyun YongHyeon 			default:
1341c4aca09aSPyun YongHyeon 				break;
1342c4aca09aSPyun YongHyeon 			}
1343c4aca09aSPyun YongHyeon 		}
1344c4aca09aSPyun YongHyeon 	}
1345c4aca09aSPyun YongHyeon 
1346a94100faSBill Paul 	ifp->if_softc = sc;
13479bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1348a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1349a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1350a94100faSBill Paul 	ifp->if_start = re_start;
1351f28a171cSPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1352f28a171cSPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1353498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1354a94100faSBill Paul 	ifp->if_init = re_init;
135552732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
135652732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
135752732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1358a94100faSBill Paul 
1359ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1360ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1361ed510fb0SBill Paul 
1362a94100faSBill Paul 	/*
1363a94100faSBill Paul 	 * Call MI attach routine.
1364a94100faSBill Paul 	 */
1365a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1366a94100faSBill Paul 
1367960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1368960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1369960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1370960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
1371960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1372960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1373960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1374960fd5b3SPyun YongHyeon #endif
1375960fd5b3SPyun YongHyeon 	/*
1376960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1377960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1378960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1379960fd5b3SPyun YongHyeon 	 */
1380960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1381960fd5b3SPyun YongHyeon 
1382ed510fb0SBill Paul #ifdef RE_DIAG
1383ed510fb0SBill Paul 	/*
1384ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1385ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1386ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1387ed510fb0SBill Paul 	 */
1388a94100faSBill Paul 
1389ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1390ed510fb0SBill Paul 		error = re_diag(sc);
1391a94100faSBill Paul 		if (error) {
1392ed510fb0SBill Paul 			device_printf(dev,
1393ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1394a94100faSBill Paul 			ether_ifdetach(ifp);
1395a94100faSBill Paul 			goto fail;
1396a94100faSBill Paul 		}
1397ed510fb0SBill Paul 	}
1398ed510fb0SBill Paul #endif
1399a94100faSBill Paul 
1400a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
14015774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0)
14025774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14035774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14045774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14055774c5ffSPyun YongHyeon 	else {
14065774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14075774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14085774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14095774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14105774c5ffSPyun YongHyeon 			if (error != 0)
14115774c5ffSPyun YongHyeon 				break;
14125774c5ffSPyun YongHyeon 		}
14135774c5ffSPyun YongHyeon 	}
1414a94100faSBill Paul 	if (error) {
1415d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1416a94100faSBill Paul 		ether_ifdetach(ifp);
1417a94100faSBill Paul 	}
1418a94100faSBill Paul 
1419a94100faSBill Paul fail:
1420ed510fb0SBill Paul 
1421a94100faSBill Paul 	if (error)
1422a94100faSBill Paul 		re_detach(dev);
1423a94100faSBill Paul 
1424a94100faSBill Paul 	return (error);
1425a94100faSBill Paul }
1426a94100faSBill Paul 
1427a94100faSBill Paul /*
1428a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1429a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1430a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1431a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1432a94100faSBill Paul  * allocated.
1433a94100faSBill Paul  */
1434a94100faSBill Paul static int
1435a94100faSBill Paul re_detach(dev)
1436a94100faSBill Paul 	device_t		dev;
1437a94100faSBill Paul {
1438a94100faSBill Paul 	struct rl_softc		*sc;
1439a94100faSBill Paul 	struct ifnet		*ifp;
14405774c5ffSPyun YongHyeon 	int			i, rid;
1441a94100faSBill Paul 
1442a94100faSBill Paul 	sc = device_get_softc(dev);
1443fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1444aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
144597b9d4baSJohn-Mark Gurney 
144640929967SGleb Smirnoff #ifdef DEVICE_POLLING
144740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
144840929967SGleb Smirnoff 		ether_poll_deregister(ifp);
144940929967SGleb Smirnoff #endif
145097b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
1451525e6a87SRuslan Ermilov 	if (device_is_attached(dev)) {
145297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
145397b9d4baSJohn-Mark Gurney #if 0
145497b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
145597b9d4baSJohn-Mark Gurney #endif
1456a94100faSBill Paul 		re_stop(sc);
1457525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1458d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14593d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14603d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1461a94100faSBill Paul 		/*
1462a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1463a94100faSBill Paul 		 * still had a BPF descriptor attached to this
146497b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1465a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1466a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1467a94100faSBill Paul 		 * which will try to call re_init() again. This will
1468a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1469a94100faSBill Paul 		 * which will panic the system when the kernel tries
1470a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1471a94100faSBill Paul 		 * anymore.
1472a94100faSBill Paul 		 */
1473a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1474525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1475a94100faSBill Paul 	}
1476a94100faSBill Paul 	if (sc->rl_miibus)
1477a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1478a94100faSBill Paul 	bus_generic_detach(dev);
1479a94100faSBill Paul 
148097b9d4baSJohn-Mark Gurney 	/*
148197b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
148297b9d4baSJohn-Mark Gurney 	 * stopped here.
148397b9d4baSJohn-Mark Gurney 	 */
148497b9d4baSJohn-Mark Gurney 
14855774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
14865774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
14875774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
14885774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
14895774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
14905774c5ffSPyun YongHyeon 		}
14915774c5ffSPyun YongHyeon 	}
1492ad4f426eSWarner Losh 	if (ifp != NULL)
1493ad4f426eSWarner Losh 		if_free(ifp);
14945774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
14955774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
14965774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
14975774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
14985774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
14995774c5ffSPyun YongHyeon 		}
15005774c5ffSPyun YongHyeon 	} else {
15015774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15025774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15035774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15045774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15055774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15065774c5ffSPyun YongHyeon 			}
15075774c5ffSPyun YongHyeon 		}
15085774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15095774c5ffSPyun YongHyeon 	}
1510a94100faSBill Paul 	if (sc->rl_res)
1511a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1512a94100faSBill Paul 
1513a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1514a94100faSBill Paul 
1515a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1516a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1517a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1518a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1519a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1520a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1521a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1522a94100faSBill Paul 	}
1523a94100faSBill Paul 
1524a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1525a94100faSBill Paul 
1526a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1527a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1528a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1529a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1530a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1531a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1532a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1533a94100faSBill Paul 	}
1534a94100faSBill Paul 
1535a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1536a94100faSBill Paul 
1537a94100faSBill Paul 	if (sc->rl_ldata.rl_mtag) {
1538a94100faSBill Paul 		for (i = 0; i < RL_TX_DESC_CNT; i++)
1539a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1540a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
1541a94100faSBill Paul 		for (i = 0; i < RL_RX_DESC_CNT; i++)
1542a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1543a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
1544a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1545a94100faSBill Paul 	}
1546a94100faSBill Paul 
1547a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1548a94100faSBill Paul 
1549a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1550a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1551a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1552a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1553a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1554a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1555a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1556a94100faSBill Paul 	}
1557a94100faSBill Paul 
1558a94100faSBill Paul 	if (sc->rl_parent_tag)
1559a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1560a94100faSBill Paul 
1561a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1562a94100faSBill Paul 
1563a94100faSBill Paul 	return (0);
1564a94100faSBill Paul }
1565a94100faSBill Paul 
1566a94100faSBill Paul static int
1567a94100faSBill Paul re_newbuf(sc, idx, m)
1568a94100faSBill Paul 	struct rl_softc		*sc;
1569a94100faSBill Paul 	int			idx;
1570a94100faSBill Paul 	struct mbuf		*m;
1571a94100faSBill Paul {
1572a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1573a94100faSBill Paul 	struct mbuf		*n = NULL;
1574a94100faSBill Paul 	int			error;
1575a94100faSBill Paul 
1576a94100faSBill Paul 	if (m == NULL) {
1577a94100faSBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1578a94100faSBill Paul 		if (n == NULL)
1579a94100faSBill Paul 			return (ENOBUFS);
1580a94100faSBill Paul 		m = n;
1581a94100faSBill Paul 	} else
1582a94100faSBill Paul 		m->m_data = m->m_ext.ext_buf;
1583a94100faSBill Paul 
1584a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
158522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
158622a11c96SJohn-Mark Gurney 	/*
158722a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
158822a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
158922a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
159022a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
159122a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
159222a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
159322a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
159422a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
159522a11c96SJohn-Mark Gurney 	 */
159622a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
159722a11c96SJohn-Mark Gurney #endif
1598a94100faSBill Paul 	arg.rl_idx = idx;
1599a94100faSBill Paul 	arg.rl_maxsegs = 1;
1600a94100faSBill Paul 	arg.rl_flags = 0;
1601a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1602a94100faSBill Paul 
1603a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1604a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1605a94100faSBill Paul 	    &arg, BUS_DMA_NOWAIT);
1606a94100faSBill Paul 	if (error || arg.rl_maxsegs != 1) {
1607a94100faSBill Paul 		if (n != NULL)
1608a94100faSBill Paul 			m_freem(n);
1609b4b95879SMarius Strobl 		if (arg.rl_maxsegs == 0)
1610b4b95879SMarius Strobl 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1611b4b95879SMarius Strobl 			    sc->rl_ldata.rl_rx_dmamap[idx]);
1612a94100faSBill Paul 		return (ENOMEM);
1613a94100faSBill Paul 	}
1614a94100faSBill Paul 
1615a94100faSBill Paul 	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1616a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1617a94100faSBill Paul 
1618a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1619a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx],
1620a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1621a94100faSBill Paul 
1622a94100faSBill Paul 	return (0);
1623a94100faSBill Paul }
1624a94100faSBill Paul 
162522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
162622a11c96SJohn-Mark Gurney static __inline void
162722a11c96SJohn-Mark Gurney re_fixup_rx(m)
162822a11c96SJohn-Mark Gurney 	struct mbuf		*m;
162922a11c96SJohn-Mark Gurney {
163022a11c96SJohn-Mark Gurney 	int                     i;
163122a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
163222a11c96SJohn-Mark Gurney 
163322a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
163422a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
163522a11c96SJohn-Mark Gurney 
163622a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
163722a11c96SJohn-Mark Gurney 		*dst++ = *src++;
163822a11c96SJohn-Mark Gurney 
163922a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
164022a11c96SJohn-Mark Gurney 
164122a11c96SJohn-Mark Gurney 	return;
164222a11c96SJohn-Mark Gurney }
164322a11c96SJohn-Mark Gurney #endif
164422a11c96SJohn-Mark Gurney 
1645a94100faSBill Paul static int
1646a94100faSBill Paul re_tx_list_init(sc)
1647a94100faSBill Paul 	struct rl_softc		*sc;
1648a94100faSBill Paul {
164997b9d4baSJohn-Mark Gurney 
165097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
165197b9d4baSJohn-Mark Gurney 
1652a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1653a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1654a94100faSBill Paul 	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1655a94100faSBill Paul 
1656a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1657a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1658a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1659a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1660a94100faSBill Paul 	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1661a94100faSBill Paul 
1662a94100faSBill Paul 	return (0);
1663a94100faSBill Paul }
1664a94100faSBill Paul 
1665a94100faSBill Paul static int
1666a94100faSBill Paul re_rx_list_init(sc)
1667a94100faSBill Paul 	struct rl_softc		*sc;
1668a94100faSBill Paul {
1669a94100faSBill Paul 	int			i;
1670a94100faSBill Paul 
1671a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1672a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1673a94100faSBill Paul 	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1674a94100faSBill Paul 
1675a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1676a94100faSBill Paul 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1677a94100faSBill Paul 			return (ENOBUFS);
1678a94100faSBill Paul 	}
1679a94100faSBill Paul 
1680a94100faSBill Paul 	/* Flush the RX descriptors */
1681a94100faSBill Paul 
1682a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1683a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1684a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1685a94100faSBill Paul 
1686a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1687a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1688a94100faSBill Paul 
1689a94100faSBill Paul 	return (0);
1690a94100faSBill Paul }
1691a94100faSBill Paul 
1692a94100faSBill Paul /*
1693a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1694a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1695a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1696a94100faSBill Paul  */
1697ed510fb0SBill Paul static int
1698a94100faSBill Paul re_rxeof(sc)
1699a94100faSBill Paul 	struct rl_softc		*sc;
1700a94100faSBill Paul {
1701a94100faSBill Paul 	struct mbuf		*m;
1702a94100faSBill Paul 	struct ifnet		*ifp;
1703a94100faSBill Paul 	int			i, total_len;
1704a94100faSBill Paul 	struct rl_desc		*cur_rx;
1705a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1706ed510fb0SBill Paul 	int			maxpkt = 16;
1707a94100faSBill Paul 
17085120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17095120abbfSSam Leffler 
1710fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1711a94100faSBill Paul 	i = sc->rl_ldata.rl_rx_prodidx;
1712a94100faSBill Paul 
1713a94100faSBill Paul 	/* Invalidate the descriptor memory */
1714a94100faSBill Paul 
1715a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1716a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1717a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1718a94100faSBill Paul 
1719ed510fb0SBill Paul 	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i]) && maxpkt) {
1720a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1721a94100faSBill Paul 		m = sc->rl_ldata.rl_rx_mbuf[i];
1722a94100faSBill Paul 		total_len = RL_RXBYTES(cur_rx);
1723a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1724a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1725a94100faSBill Paul 
1726a94100faSBill Paul 		/* Invalidate the RX mbuf and unload its map */
1727a94100faSBill Paul 
1728a94100faSBill Paul 		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1729a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i],
1730a94100faSBill Paul 		    BUS_DMASYNC_POSTWRITE);
1731a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1732a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i]);
1733a94100faSBill Paul 
1734a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
173522a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1736a94100faSBill Paul 			if (sc->rl_head == NULL)
1737a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1738a94100faSBill Paul 			else {
1739a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1740a94100faSBill Paul 				sc->rl_tail->m_next = m;
1741a94100faSBill Paul 				sc->rl_tail = m;
1742a94100faSBill Paul 			}
1743a94100faSBill Paul 			re_newbuf(sc, i, NULL);
1744a94100faSBill Paul 			RL_DESC_INC(i);
1745a94100faSBill Paul 			continue;
1746a94100faSBill Paul 		}
1747a94100faSBill Paul 
1748a94100faSBill Paul 		/*
1749a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1750a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1751a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1752a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1753a94100faSBill Paul 		 * were already used, so to make room for the extra
1754a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1755a94100faSBill Paul 		 * error' bit and shifted the other status bits
1756a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1757a94100faSBill Paul 		 * still in the same places. We have already extracted
1758a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1759a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1760a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1761a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1762a94100faSBill Paul 		 * same format as that of the 8139C+.
1763a94100faSBill Paul 		 */
1764a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1765a94100faSBill Paul 			rxstat >>= 1;
1766a94100faSBill Paul 
176722a11c96SJohn-Mark Gurney 		/*
176822a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
176922a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
177022a11c96SJohn-Mark Gurney 		 */
177122a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
177222a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1773a94100faSBill Paul 			ifp->if_ierrors++;
1774a94100faSBill Paul 			/*
1775a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1776a94100faSBill Paul 			 * discard all the pieces.
1777a94100faSBill Paul 			 */
1778a94100faSBill Paul 			if (sc->rl_head != NULL) {
1779a94100faSBill Paul 				m_freem(sc->rl_head);
1780a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1781a94100faSBill Paul 			}
1782a94100faSBill Paul 			re_newbuf(sc, i, m);
1783a94100faSBill Paul 			RL_DESC_INC(i);
1784a94100faSBill Paul 			continue;
1785a94100faSBill Paul 		}
1786a94100faSBill Paul 
1787a94100faSBill Paul 		/*
1788a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1789a94100faSBill Paul 		 * reload the current one.
1790a94100faSBill Paul 		 */
1791a94100faSBill Paul 
1792a94100faSBill Paul 		if (re_newbuf(sc, i, NULL)) {
1793a94100faSBill Paul 			ifp->if_ierrors++;
1794a94100faSBill Paul 			if (sc->rl_head != NULL) {
1795a94100faSBill Paul 				m_freem(sc->rl_head);
1796a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1797a94100faSBill Paul 			}
1798a94100faSBill Paul 			re_newbuf(sc, i, m);
1799a94100faSBill Paul 			RL_DESC_INC(i);
1800a94100faSBill Paul 			continue;
1801a94100faSBill Paul 		}
1802a94100faSBill Paul 
1803a94100faSBill Paul 		RL_DESC_INC(i);
1804a94100faSBill Paul 
1805a94100faSBill Paul 		if (sc->rl_head != NULL) {
180622a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
180722a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
180822a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1809a94100faSBill Paul 			/*
1810a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1811a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1812a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1813a94100faSBill Paul 			 * care about anyway.
1814a94100faSBill Paul 			 */
1815a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1816a94100faSBill Paul 				sc->rl_tail->m_len -=
1817a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1818a94100faSBill Paul 				m_freem(m);
1819a94100faSBill Paul 			} else {
1820a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1821a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1822a94100faSBill Paul 				sc->rl_tail->m_next = m;
1823a94100faSBill Paul 			}
1824a94100faSBill Paul 			m = sc->rl_head;
1825a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1826a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1827a94100faSBill Paul 		} else
1828a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1829a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1830a94100faSBill Paul 
183122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
183222a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
183322a11c96SJohn-Mark Gurney #endif
1834a94100faSBill Paul 		ifp->if_ipackets++;
1835a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1836a94100faSBill Paul 
1837a94100faSBill Paul 		/* Do RX checksumming if enabled */
1838a94100faSBill Paul 
1839a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1840a94100faSBill Paul 
1841a94100faSBill Paul 			/* Check IP header checksum */
1842a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1843a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1844a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1845a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1846a94100faSBill Paul 
1847a94100faSBill Paul 			/* Check TCP/UDP checksum */
1848a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1849a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1850a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1851a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1852a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1853a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1854a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1855a94100faSBill Paul 			}
1856a94100faSBill Paul 		}
1857ed510fb0SBill Paul 		maxpkt--;
1858d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
185978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
186078ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
186178ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1862d147662cSGleb Smirnoff 		}
18635120abbfSSam Leffler 		RL_UNLOCK(sc);
1864a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
18655120abbfSSam Leffler 		RL_LOCK(sc);
1866a94100faSBill Paul 	}
1867a94100faSBill Paul 
1868a94100faSBill Paul 	/* Flush the RX DMA ring */
1869a94100faSBill Paul 
1870a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1871a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1872a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1873a94100faSBill Paul 
1874a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1875ed510fb0SBill Paul 
1876ed510fb0SBill Paul 	if (maxpkt)
1877ed510fb0SBill Paul 		return(EAGAIN);
1878ed510fb0SBill Paul 
1879ed510fb0SBill Paul 	return(0);
1880a94100faSBill Paul }
1881a94100faSBill Paul 
1882a94100faSBill Paul static void
1883a94100faSBill Paul re_txeof(sc)
1884a94100faSBill Paul 	struct rl_softc		*sc;
1885a94100faSBill Paul {
1886a94100faSBill Paul 	struct ifnet		*ifp;
1887a94100faSBill Paul 	u_int32_t		txstat;
1888a94100faSBill Paul 	int			idx;
1889a94100faSBill Paul 
1890fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1891a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_considx;
1892a94100faSBill Paul 
1893a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1894a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1895a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1896a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1897a94100faSBill Paul 
1898ed510fb0SBill Paul 	while (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) {
1899a94100faSBill Paul 		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1900a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_OWN)
1901a94100faSBill Paul 			break;
1902a94100faSBill Paul 
1903ed510fb0SBill Paul 		sc->rl_ldata.rl_tx_list[idx].rl_bufaddr_lo = 0;
1904ed510fb0SBill Paul 
1905a94100faSBill Paul 		/*
1906a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1907a94100faSBill Paul 		 * in a fragment chain, which also happens to
1908a94100faSBill Paul 		 * be the only place where the TX status bits
1909a94100faSBill Paul 		 * are valid.
1910a94100faSBill Paul 		 */
1911a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1912a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1913a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1914a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1915a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[idx]);
1916a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1917a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1918a94100faSBill Paul 				ifp->if_collisions++;
1919a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1920a94100faSBill Paul 				ifp->if_oerrors++;
1921a94100faSBill Paul 			else
1922a94100faSBill Paul 				ifp->if_opackets++;
1923a94100faSBill Paul 		}
1924a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1925a94100faSBill Paul 		RL_DESC_INC(idx);
1926a94100faSBill Paul 	}
1927b4b95879SMarius Strobl 	sc->rl_ldata.rl_tx_considx = idx;
1928a94100faSBill Paul 
1929a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1930a94100faSBill Paul 
1931b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free > RL_TX_DESC_THLD)
193213f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1933a94100faSBill Paul 
1934b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) {
19350fc4974fSBill Paul 		/*
1936b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
1937b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
1938b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
1939b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
1940b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
1941b4b95879SMarius Strobl 		 * be required with the PCIe devices.
19420fc4974fSBill Paul 		 */
19430fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
19440fc4974fSBill Paul 
1945ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1946a94100faSBill Paul 		/*
1947b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
1948b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
1949a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
1950a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
1951a94100faSBill Paul 		 */
1952a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1953ed510fb0SBill Paul #endif
1954b4b95879SMarius Strobl 	} else
1955b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
1956a94100faSBill Paul }
1957a94100faSBill Paul 
1958a94100faSBill Paul static void
1959a94100faSBill Paul re_tick(xsc)
1960a94100faSBill Paul 	void			*xsc;
1961a94100faSBill Paul {
1962a94100faSBill Paul 	struct rl_softc		*sc;
1963d1754a9bSJohn Baldwin 	struct mii_data		*mii;
1964ed510fb0SBill Paul 	struct ifnet		*ifp;
1965a94100faSBill Paul 
1966a94100faSBill Paul 	sc = xsc;
1967ed510fb0SBill Paul 	ifp = sc->rl_ifp;
196897b9d4baSJohn-Mark Gurney 
196997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
197097b9d4baSJohn-Mark Gurney 
19711d545c7aSMarius Strobl 	re_watchdog(sc);
1972a94100faSBill Paul 
19731d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
1974a94100faSBill Paul 	mii_tick(mii);
1975ed510fb0SBill Paul 	if (sc->rl_link) {
1976ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
1977ed510fb0SBill Paul 			sc->rl_link = 0;
1978ed510fb0SBill Paul 	} else {
1979ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1980ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1981ed510fb0SBill Paul 			sc->rl_link = 1;
1982ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1983ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
1984ed510fb0SBill Paul 				    &sc->rl_txtask);
1985ed510fb0SBill Paul 		}
1986ed510fb0SBill Paul 	}
1987a94100faSBill Paul 
1988d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
1989a94100faSBill Paul }
1990a94100faSBill Paul 
1991a94100faSBill Paul #ifdef DEVICE_POLLING
1992a94100faSBill Paul static void
1993a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1994a94100faSBill Paul {
1995a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1996a94100faSBill Paul 
1997a94100faSBill Paul 	RL_LOCK(sc);
199840929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
199997b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
200097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
200197b9d4baSJohn-Mark Gurney }
200297b9d4baSJohn-Mark Gurney 
200397b9d4baSJohn-Mark Gurney static void
200497b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
200597b9d4baSJohn-Mark Gurney {
200697b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
200797b9d4baSJohn-Mark Gurney 
200897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
200997b9d4baSJohn-Mark Gurney 
2010a94100faSBill Paul 	sc->rxcycles = count;
2011a94100faSBill Paul 	re_rxeof(sc);
2012a94100faSBill Paul 	re_txeof(sc);
2013a94100faSBill Paul 
201437652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2015ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2016a94100faSBill Paul 
2017a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2018a94100faSBill Paul 		u_int16_t       status;
2019a94100faSBill Paul 
2020a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2021a94100faSBill Paul 		if (status == 0xffff)
202297b9d4baSJohn-Mark Gurney 			return;
2023a94100faSBill Paul 		if (status)
2024a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2025a94100faSBill Paul 
2026a94100faSBill Paul 		/*
2027a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2028a94100faSBill Paul 		 */
2029a94100faSBill Paul 
2030a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
2031a94100faSBill Paul 			re_reset(sc);
203297b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2033a94100faSBill Paul 		}
2034a94100faSBill Paul 	}
2035a94100faSBill Paul }
2036a94100faSBill Paul #endif /* DEVICE_POLLING */
2037a94100faSBill Paul 
2038ef544f63SPaolo Pisati static int
2039a94100faSBill Paul re_intr(arg)
2040a94100faSBill Paul 	void			*arg;
2041a94100faSBill Paul {
2042a94100faSBill Paul 	struct rl_softc		*sc;
2043ed510fb0SBill Paul 	uint16_t		status;
2044a94100faSBill Paul 
2045a94100faSBill Paul 	sc = arg;
2046ed510fb0SBill Paul 
2047ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2048498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2049ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2050ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2051ed510fb0SBill Paul 
2052ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2053ed510fb0SBill Paul 
2054ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2055ed510fb0SBill Paul }
2056ed510fb0SBill Paul 
2057ed510fb0SBill Paul static void
2058ed510fb0SBill Paul re_int_task(arg, npending)
2059ed510fb0SBill Paul 	void			*arg;
2060ed510fb0SBill Paul 	int			npending;
2061ed510fb0SBill Paul {
2062ed510fb0SBill Paul 	struct rl_softc		*sc;
2063ed510fb0SBill Paul 	struct ifnet		*ifp;
2064ed510fb0SBill Paul 	u_int16_t		status;
2065ed510fb0SBill Paul 	int			rval = 0;
2066ed510fb0SBill Paul 
2067ed510fb0SBill Paul 	sc = arg;
2068ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2069a94100faSBill Paul 
2070a94100faSBill Paul 	RL_LOCK(sc);
207197b9d4baSJohn-Mark Gurney 
2072a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2073a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2074a94100faSBill Paul 
2075ed510fb0SBill Paul 	if (sc->suspended || !(ifp->if_flags & IFF_UP)) {
2076ed510fb0SBill Paul 		RL_UNLOCK(sc);
2077ed510fb0SBill Paul 		return;
2078ed510fb0SBill Paul 	}
2079a94100faSBill Paul 
2080ed510fb0SBill Paul #ifdef DEVICE_POLLING
2081ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2082ed510fb0SBill Paul 		RL_UNLOCK(sc);
2083ed510fb0SBill Paul 		return;
2084ed510fb0SBill Paul 	}
2085ed510fb0SBill Paul #endif
2086a94100faSBill Paul 
2087ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2088ed510fb0SBill Paul 		rval = re_rxeof(sc);
2089ed510fb0SBill Paul 
2090ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2091ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
2092ed510fb0SBill Paul #else
2093ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
2094ed510fb0SBill Paul #endif
2095ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2096a94100faSBill Paul 		re_txeof(sc);
2097a94100faSBill Paul 
2098a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
2099a94100faSBill Paul 		re_reset(sc);
210097b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2101a94100faSBill Paul 	}
2102a94100faSBill Paul 
2103a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
2104d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
2105d1754a9bSJohn Baldwin 		re_tick(sc);
2106a94100faSBill Paul 	}
2107a94100faSBill Paul 
210852732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2109ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2110a94100faSBill Paul 
2111a94100faSBill Paul 	RL_UNLOCK(sc);
2112ed510fb0SBill Paul 
2113ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2114ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2115ed510fb0SBill Paul 		return;
2116ed510fb0SBill Paul 	}
2117ed510fb0SBill Paul 
2118ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2119ed510fb0SBill Paul 
2120ed510fb0SBill Paul 	return;
2121a94100faSBill Paul }
2122a94100faSBill Paul 
2123a94100faSBill Paul static int
2124a94100faSBill Paul re_encap(sc, m_head, idx)
2125a94100faSBill Paul 	struct rl_softc		*sc;
212680a2a305SJohn-Mark Gurney 	struct mbuf		**m_head;
2127a94100faSBill Paul 	int			*idx;
2128a94100faSBill Paul {
2129a94100faSBill Paul 	struct mbuf		*m_new = NULL;
2130a94100faSBill Paul 	struct rl_dmaload_arg	arg;
2131a94100faSBill Paul 	bus_dmamap_t		map;
2132a94100faSBill Paul 	int			error;
2133a94100faSBill Paul 
213497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
213597b9d4baSJohn-Mark Gurney 
2136b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free <= RL_TX_DESC_THLD)
2137a94100faSBill Paul 		return (EFBIG);
2138a94100faSBill Paul 
2139a94100faSBill Paul 	/*
2140a94100faSBill Paul 	 * Set up checksum offload. Note: checksum offload bits must
2141a94100faSBill Paul 	 * appear in all descriptors of a multi-descriptor transmit
214222a11c96SJohn-Mark Gurney 	 * attempt. This is according to testing done with an 8169
214322a11c96SJohn-Mark Gurney 	 * chip. This is a requirement.
2144a94100faSBill Paul 	 */
2145a94100faSBill Paul 
2146a94100faSBill Paul 	arg.rl_flags = 0;
2147a94100faSBill Paul 
2148dc74159dSPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2149dc74159dSPyun YongHyeon 		arg.rl_flags = RL_TDESC_CMD_LGSEND |
2150dc74159dSPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2151dc74159dSPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2152dc74159dSPyun YongHyeon 	else {
215380a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
2154a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
215580a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
2156a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
215780a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
2158a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
2159dc74159dSPyun YongHyeon 	}
2160a94100faSBill Paul 
2161a94100faSBill Paul 	arg.rl_idx = *idx;
2162a94100faSBill Paul 	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
2163b4b95879SMarius Strobl 	if (arg.rl_maxsegs > RL_TX_DESC_THLD)
2164b4b95879SMarius Strobl 		arg.rl_maxsegs -= RL_TX_DESC_THLD;
2165a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_tx_list;
2166a94100faSBill Paul 
2167a94100faSBill Paul 	map = sc->rl_ldata.rl_tx_dmamap[*idx];
21680fc4974fSBill Paul 
21690fc4974fSBill Paul 	/*
21700fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
21710fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
21720fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
21730fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
21740fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
21750fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
21760fc4974fSBill Paul 	 * have garbled payload. To work around this, if TX checksum
21770fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
21780fc4974fSBill Paul 	 * to the minimum ethernet frame size. We do this by pretending
21790fc4974fSBill Paul 	 * the mbuf chain has too many fragments so the coalescing code
21800fc4974fSBill Paul 	 * below can assemble the packet into a single buffer that's
21810fc4974fSBill Paul 	 * padded out to the mininum frame size.
2182e2bcb489SBill Paul 	 *
2183e2bcb489SBill Paul 	 * Note: this appears unnecessary for TCP, and doing it for TCP
2184e2bcb489SBill Paul 	 * with PCIe adapters seems to result in bad checksums.
21850fc4974fSBill Paul 	 */
2186e2bcb489SBill Paul 
2187e2bcb489SBill Paul 	if (arg.rl_flags && !(arg.rl_flags & RL_TDESC_CMD_TCPCSUM) &&
2188e2bcb489SBill Paul             (*m_head)->m_pkthdr.len < RL_MIN_FRAMELEN)
21890fc4974fSBill Paul 		error = EFBIG;
21900fc4974fSBill Paul 	else
2191a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
219280a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2193a94100faSBill Paul 
2194a94100faSBill Paul 	if (error && error != EFBIG) {
21956b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "can't map mbuf (error %d)\n", error);
2196a94100faSBill Paul 		return (ENOBUFS);
2197a94100faSBill Paul 	}
2198a94100faSBill Paul 
2199a94100faSBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
2200a94100faSBill Paul 
2201a94100faSBill Paul 	if (error || arg.rl_maxsegs == 0) {
2202b4b95879SMarius Strobl 		if (arg.rl_maxsegs == 0)
2203b4b95879SMarius Strobl 			bus_dmamap_unload(sc->rl_ldata.rl_mtag, map);
220480a2a305SJohn-Mark Gurney 		m_new = m_defrag(*m_head, M_DONTWAIT);
2205b4b95879SMarius Strobl 		if (m_new == NULL) {
2206b4b95879SMarius Strobl 			m_freem(*m_head);
2207b4b95879SMarius Strobl 			*m_head = NULL;
220880a2a305SJohn-Mark Gurney 			return (ENOBUFS);
2209b4b95879SMarius Strobl 		}
221080a2a305SJohn-Mark Gurney 		*m_head = m_new;
2211a94100faSBill Paul 
22120fc4974fSBill Paul 		/*
22130fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
22140fc4974fSBill Paul 		 * to avoid leaking data.
22150fc4974fSBill Paul 		 */
22160fc4974fSBill Paul 		if (m_new->m_pkthdr.len < RL_MIN_FRAMELEN) {
22170fc4974fSBill Paul 			bzero(mtod(m_new, char *) + m_new->m_pkthdr.len,
22180fc4974fSBill Paul 			    RL_MIN_FRAMELEN - m_new->m_pkthdr.len);
22190fc4974fSBill Paul 			m_new->m_pkthdr.len += RL_MIN_FRAMELEN -
22200fc4974fSBill Paul 			    m_new->m_pkthdr.len;
22210fc4974fSBill Paul 			m_new->m_len = m_new->m_pkthdr.len;
22220fc4974fSBill Paul 		}
22230fc4974fSBill Paul 
2224b4b95879SMarius Strobl 		/* Note that we'll run over RL_TX_DESC_THLD here. */
2225a94100faSBill Paul 		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
2226a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
222780a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2228b4b95879SMarius Strobl 		if (error || arg.rl_maxsegs == 0) {
2229b4b95879SMarius Strobl 			device_printf(sc->rl_dev,
2230b4b95879SMarius Strobl 			    "can't map defragmented mbuf (error %d)\n", error);
2231b4b95879SMarius Strobl 			m_freem(m_new);
2232b4b95879SMarius Strobl 			*m_head = NULL;
2233b4b95879SMarius Strobl 			if (arg.rl_maxsegs == 0)
2234b4b95879SMarius Strobl 				bus_dmamap_unload(sc->rl_ldata.rl_mtag, map);
2235a94100faSBill Paul 			return (EFBIG);
2236a94100faSBill Paul 		}
2237a94100faSBill Paul 	}
2238a94100faSBill Paul 
2239a94100faSBill Paul 	/*
2240a94100faSBill Paul 	 * Insure that the map for this transmission
2241a94100faSBill Paul 	 * is placed at the array index of the last descriptor
224222a11c96SJohn-Mark Gurney 	 * in this chain.  (Swap last and first dmamaps.)
2243a94100faSBill Paul 	 */
2244a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[*idx] =
2245a94100faSBill Paul 	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
2246a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
2247a94100faSBill Paul 
224880a2a305SJohn-Mark Gurney 	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head;
2249a94100faSBill Paul 	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
2250a94100faSBill Paul 
2251a94100faSBill Paul 	/*
2252a94100faSBill Paul 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2253a94100faSBill Paul 	 * appear in the first descriptor of a multi-descriptor
2254a94100faSBill Paul 	 * transmission attempt.
2255a94100faSBill Paul 	 */
225678ba57b9SAndre Oppermann 	if ((*m_head)->m_flags & M_VLANTAG)
2257a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
225878ba57b9SAndre Oppermann 		    htole32(htons((*m_head)->m_pkthdr.ether_vtag) |
225978ba57b9SAndre Oppermann 		    RL_TDESC_VLANCTL_TAG);
2260a94100faSBill Paul 
2261a94100faSBill Paul 	/* Transfer ownership of packet to the chip. */
2262a94100faSBill Paul 
2263a94100faSBill Paul 	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
2264a94100faSBill Paul 	    htole32(RL_TDESC_CMD_OWN);
2265a94100faSBill Paul 	if (*idx != arg.rl_idx)
2266a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
2267a94100faSBill Paul 		    htole32(RL_TDESC_CMD_OWN);
2268a94100faSBill Paul 
2269a94100faSBill Paul         RL_DESC_INC(arg.rl_idx);
2270a94100faSBill Paul 	*idx = arg.rl_idx;
2271a94100faSBill Paul 
2272a94100faSBill Paul 	return (0);
2273a94100faSBill Paul }
2274a94100faSBill Paul 
227597b9d4baSJohn-Mark Gurney static void
2276ed510fb0SBill Paul re_tx_task(arg, npending)
2277ed510fb0SBill Paul 	void			*arg;
2278ed510fb0SBill Paul 	int			npending;
227997b9d4baSJohn-Mark Gurney {
2280ed510fb0SBill Paul 	struct ifnet		*ifp;
228197b9d4baSJohn-Mark Gurney 
2282ed510fb0SBill Paul 	ifp = arg;
2283ed510fb0SBill Paul 	re_start(ifp);
2284ed510fb0SBill Paul 
2285ed510fb0SBill Paul 	return;
228697b9d4baSJohn-Mark Gurney }
228797b9d4baSJohn-Mark Gurney 
2288a94100faSBill Paul /*
2289a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2290a94100faSBill Paul  */
2291a94100faSBill Paul static void
2292ed510fb0SBill Paul re_start(ifp)
2293a94100faSBill Paul 	struct ifnet		*ifp;
2294a94100faSBill Paul {
2295a94100faSBill Paul 	struct rl_softc		*sc;
2296a94100faSBill Paul 	struct mbuf		*m_head = NULL;
229752732175SMax Laier 	int			idx, queued = 0;
2298a94100faSBill Paul 
2299a94100faSBill Paul 	sc = ifp->if_softc;
230097b9d4baSJohn-Mark Gurney 
2301ed510fb0SBill Paul 	RL_LOCK(sc);
2302ed510fb0SBill Paul 
2303ed510fb0SBill Paul 	if (!sc->rl_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) {
2304ed510fb0SBill Paul 		RL_UNLOCK(sc);
2305ed510fb0SBill Paul 		return;
2306ed510fb0SBill Paul 	}
2307a94100faSBill Paul 
2308a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_prodidx;
2309a94100faSBill Paul 
2310a94100faSBill Paul 	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
231152732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2312a94100faSBill Paul 		if (m_head == NULL)
2313a94100faSBill Paul 			break;
2314a94100faSBill Paul 
231580a2a305SJohn-Mark Gurney 		if (re_encap(sc, &m_head, &idx)) {
2316b4b95879SMarius Strobl 			if (m_head == NULL)
2317b4b95879SMarius Strobl 				break;
231852732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
231913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2320a94100faSBill Paul 			break;
2321a94100faSBill Paul 		}
2322a94100faSBill Paul 
2323a94100faSBill Paul 		/*
2324a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2325a94100faSBill Paul 		 * to him.
2326a94100faSBill Paul 		 */
232759a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
232852732175SMax Laier 
232952732175SMax Laier 		queued++;
2330a94100faSBill Paul 	}
2331a94100faSBill Paul 
2332ed510fb0SBill Paul 	if (queued == 0) {
2333ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2334ed510fb0SBill Paul 		if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
2335ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2336ed510fb0SBill Paul #endif
2337ed510fb0SBill Paul 		RL_UNLOCK(sc);
233852732175SMax Laier 		return;
2339ed510fb0SBill Paul 	}
234052732175SMax Laier 
2341a94100faSBill Paul 	/* Flush the TX descriptors */
2342a94100faSBill Paul 
2343a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2344a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2345a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2346a94100faSBill Paul 
2347a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = idx;
2348a94100faSBill Paul 
23490fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2350a94100faSBill Paul 
2351ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2352a94100faSBill Paul 	/*
2353a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2354a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2355a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2356a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2357a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2358a94100faSBill Paul 	 * the timer count is reset to 0.
2359a94100faSBill Paul 	 */
2360a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2361ed510fb0SBill Paul #endif
2362a94100faSBill Paul 
2363a94100faSBill Paul 	/*
2364a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2365a94100faSBill Paul 	 */
23661d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2367ed510fb0SBill Paul 
2368ed510fb0SBill Paul 	RL_UNLOCK(sc);
2369ed510fb0SBill Paul 
2370ed510fb0SBill Paul 	return;
2371a94100faSBill Paul }
2372a94100faSBill Paul 
2373a94100faSBill Paul static void
2374a94100faSBill Paul re_init(xsc)
2375a94100faSBill Paul 	void			*xsc;
2376a94100faSBill Paul {
2377a94100faSBill Paul 	struct rl_softc		*sc = xsc;
237897b9d4baSJohn-Mark Gurney 
237997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
238097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
238197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
238297b9d4baSJohn-Mark Gurney }
238397b9d4baSJohn-Mark Gurney 
238497b9d4baSJohn-Mark Gurney static void
238597b9d4baSJohn-Mark Gurney re_init_locked(sc)
238697b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
238797b9d4baSJohn-Mark Gurney {
2388fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2389a94100faSBill Paul 	struct mii_data		*mii;
2390a94100faSBill Paul 	u_int32_t		rxcfg = 0;
23914d3d7085SBernd Walter 	union {
23924d3d7085SBernd Walter 		uint32_t align_dummy;
23934d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
23944d3d7085SBernd Walter         } eaddr;
2395a94100faSBill Paul 
239697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
239797b9d4baSJohn-Mark Gurney 
2398a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2399a94100faSBill Paul 
2400a94100faSBill Paul 	/*
2401a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2402a94100faSBill Paul 	 */
2403a94100faSBill Paul 	re_stop(sc);
2404a94100faSBill Paul 
2405a94100faSBill Paul 	/*
2406c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2407edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2408c2c6548bSBill Paul 	 * before all others.
2409c2c6548bSBill Paul 	 */
2410c2c6548bSBill Paul 	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2411c2c6548bSBill Paul 	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2412ed510fb0SBill Paul 	    RL_CPLUSCMD_VLANSTRIP|RL_CPLUSCMD_RXCSUM_ENB);
2413c2c6548bSBill Paul 
2414c2c6548bSBill Paul 	/*
2415a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2416a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2417a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2418a94100faSBill Paul 	 */
24194d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
24204d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2421a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2422ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2423ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2424ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2425ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2426a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2427a94100faSBill Paul 
2428a94100faSBill Paul 	/*
2429a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2430a94100faSBill Paul 	 */
2431a94100faSBill Paul 	re_rx_list_init(sc);
2432a94100faSBill Paul 	re_tx_list_init(sc);
2433a94100faSBill Paul 
2434a94100faSBill Paul 	/*
2435d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2436d01fac16SPyun YongHyeon 	 */
2437d01fac16SPyun YongHyeon 
2438d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2439d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2440d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2441d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2442d01fac16SPyun YongHyeon 
2443d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2444d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2445d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2446d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2447d01fac16SPyun YongHyeon 
2448d01fac16SPyun YongHyeon 	/*
2449a94100faSBill Paul 	 * Enable transmit and receive.
2450a94100faSBill Paul 	 */
2451a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2452a94100faSBill Paul 
2453a94100faSBill Paul 	/*
2454a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2455a94100faSBill Paul 	 */
2456abc8ff44SBill Paul 	if (sc->rl_testmode) {
2457abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2458abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2459abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2460a94100faSBill Paul 		else
2461abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2462abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2463abc8ff44SBill Paul 	} else
2464a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2465d01fac16SPyun YongHyeon 
2466d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2467d01fac16SPyun YongHyeon 
2468a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2469a94100faSBill Paul 
2470a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2471a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2472a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2473a94100faSBill Paul 
2474a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
247561021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2476a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
247761021536SJohn-Mark Gurney 	else
2478a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2479a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2480a94100faSBill Paul 
2481a94100faSBill Paul 	/*
2482a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2483a94100faSBill Paul 	 */
248461021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2485a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
248661021536SJohn-Mark Gurney 	else
2487a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2488a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2489a94100faSBill Paul 
2490a94100faSBill Paul 	/*
2491a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2492a94100faSBill Paul 	 */
2493a94100faSBill Paul 	re_setmulti(sc);
2494a94100faSBill Paul 
2495a94100faSBill Paul #ifdef DEVICE_POLLING
2496a94100faSBill Paul 	/*
2497a94100faSBill Paul 	 * Disable interrupts if we are polling.
2498a94100faSBill Paul 	 */
249940929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2500a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2501a94100faSBill Paul 	else	/* otherwise ... */
250240929967SGleb Smirnoff #endif
2503ed510fb0SBill Paul 
2504a94100faSBill Paul 	/*
2505a94100faSBill Paul 	 * Enable interrupts.
2506a94100faSBill Paul 	 */
2507a94100faSBill Paul 	if (sc->rl_testmode)
2508a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2509a94100faSBill Paul 	else
2510a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2511ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2512a94100faSBill Paul 
2513a94100faSBill Paul 	/* Set initial TX threshold */
2514a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2515a94100faSBill Paul 
2516a94100faSBill Paul 	/* Start RX/TX process. */
2517a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2518a94100faSBill Paul #ifdef notdef
2519a94100faSBill Paul 	/* Enable receiver and transmitter. */
2520a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2521a94100faSBill Paul #endif
2522a94100faSBill Paul 
2523ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2524a94100faSBill Paul 	/*
2525a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2526a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2527a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2528a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2529a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2530a94100faSBill Paul 	 */
2531a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2532a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2533a94100faSBill Paul 	else
2534a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2535ed510fb0SBill Paul #endif
2536a94100faSBill Paul 
2537a94100faSBill Paul 	/*
2538a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2539a94100faSBill Paul 	 * size so we can receive jumbo frames.
2540a94100faSBill Paul 	 */
2541a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2542a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2543a94100faSBill Paul 
254497b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2545a94100faSBill Paul 		return;
2546a94100faSBill Paul 
2547a94100faSBill Paul 	mii_mediachg(mii);
2548a94100faSBill Paul 
254919ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2550a94100faSBill Paul 
255113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
255213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2553a94100faSBill Paul 
2554ed510fb0SBill Paul 	sc->rl_link = 0;
25551d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2556d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2557a94100faSBill Paul }
2558a94100faSBill Paul 
2559a94100faSBill Paul /*
2560a94100faSBill Paul  * Set media options.
2561a94100faSBill Paul  */
2562a94100faSBill Paul static int
2563a94100faSBill Paul re_ifmedia_upd(ifp)
2564a94100faSBill Paul 	struct ifnet		*ifp;
2565a94100faSBill Paul {
2566a94100faSBill Paul 	struct rl_softc		*sc;
2567a94100faSBill Paul 	struct mii_data		*mii;
2568a94100faSBill Paul 
2569a94100faSBill Paul 	sc = ifp->if_softc;
2570a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2571d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2572a94100faSBill Paul 	mii_mediachg(mii);
2573d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2574a94100faSBill Paul 
2575a94100faSBill Paul 	return (0);
2576a94100faSBill Paul }
2577a94100faSBill Paul 
2578a94100faSBill Paul /*
2579a94100faSBill Paul  * Report current media status.
2580a94100faSBill Paul  */
2581a94100faSBill Paul static void
2582a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2583a94100faSBill Paul 	struct ifnet		*ifp;
2584a94100faSBill Paul 	struct ifmediareq	*ifmr;
2585a94100faSBill Paul {
2586a94100faSBill Paul 	struct rl_softc		*sc;
2587a94100faSBill Paul 	struct mii_data		*mii;
2588a94100faSBill Paul 
2589a94100faSBill Paul 	sc = ifp->if_softc;
2590a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2591a94100faSBill Paul 
2592d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2593a94100faSBill Paul 	mii_pollstat(mii);
2594d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2595a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2596a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2597a94100faSBill Paul }
2598a94100faSBill Paul 
2599a94100faSBill Paul static int
2600a94100faSBill Paul re_ioctl(ifp, command, data)
2601a94100faSBill Paul 	struct ifnet		*ifp;
2602a94100faSBill Paul 	u_long			command;
2603a94100faSBill Paul 	caddr_t			data;
2604a94100faSBill Paul {
2605a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2606a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2607a94100faSBill Paul 	struct mii_data		*mii;
260840929967SGleb Smirnoff 	int			error = 0;
2609a94100faSBill Paul 
2610a94100faSBill Paul 	switch (command) {
2611a94100faSBill Paul 	case SIOCSIFMTU:
2612d1754a9bSJohn Baldwin 		RL_LOCK(sc);
2613a94100faSBill Paul 		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2614a94100faSBill Paul 			error = EINVAL;
2615a94100faSBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2616d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2617a94100faSBill Paul 		break;
2618a94100faSBill Paul 	case SIOCSIFFLAGS:
261997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2620eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2621eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2622eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
2623eed497bbSPyun YongHyeon 				    & IFF_PROMISC) != 0)
2624eed497bbSPyun YongHyeon 					re_setmulti(sc);
2625eed497bbSPyun YongHyeon 			} else
262697b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2627eed497bbSPyun YongHyeon 		} else {
2628eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2629a94100faSBill Paul 				re_stop(sc);
2630eed497bbSPyun YongHyeon 		}
2631eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
263297b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2633a94100faSBill Paul 		break;
2634a94100faSBill Paul 	case SIOCADDMULTI:
2635a94100faSBill Paul 	case SIOCDELMULTI:
263697b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2637a94100faSBill Paul 		re_setmulti(sc);
263897b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2639a94100faSBill Paul 		break;
2640a94100faSBill Paul 	case SIOCGIFMEDIA:
2641a94100faSBill Paul 	case SIOCSIFMEDIA:
2642a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2643a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2644a94100faSBill Paul 		break;
2645a94100faSBill Paul 	case SIOCSIFCAP:
264640929967SGleb Smirnoff 	    {
2647f051cb85SGleb Smirnoff 		int mask, reinit;
2648f051cb85SGleb Smirnoff 
2649f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2650f051cb85SGleb Smirnoff 		reinit = 0;
265140929967SGleb Smirnoff #ifdef DEVICE_POLLING
265240929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
265340929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
265440929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
265540929967SGleb Smirnoff 				if (error)
265640929967SGleb Smirnoff 					return(error);
2657d1754a9bSJohn Baldwin 				RL_LOCK(sc);
265840929967SGleb Smirnoff 				/* Disable interrupts */
265940929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
266040929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
266140929967SGleb Smirnoff 				RL_UNLOCK(sc);
266240929967SGleb Smirnoff 			} else {
266340929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
266440929967SGleb Smirnoff 				/* Enable interrupts. */
266540929967SGleb Smirnoff 				RL_LOCK(sc);
266640929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
266740929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
266840929967SGleb Smirnoff 				RL_UNLOCK(sc);
266940929967SGleb Smirnoff 			}
267040929967SGleb Smirnoff 		}
267140929967SGleb Smirnoff #endif /* DEVICE_POLLING */
267240929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2673f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2674a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2675dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2676a94100faSBill Paul 			else
2677b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2678f051cb85SGleb Smirnoff 			reinit = 1;
267940929967SGleb Smirnoff 		}
2680f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2681f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2682f051cb85SGleb Smirnoff 			reinit = 1;
2683f051cb85SGleb Smirnoff 		}
2684dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2685dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2686dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2687dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2688dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2689dc74159dSPyun YongHyeon 			else
2690dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2691dc74159dSPyun YongHyeon 		}
2692f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2693f051cb85SGleb Smirnoff 			re_init(sc);
2694960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
269540929967SGleb Smirnoff 	    }
2696a94100faSBill Paul 		break;
2697a94100faSBill Paul 	default:
2698a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2699a94100faSBill Paul 		break;
2700a94100faSBill Paul 	}
2701a94100faSBill Paul 
2702a94100faSBill Paul 	return (error);
2703a94100faSBill Paul }
2704a94100faSBill Paul 
2705a94100faSBill Paul static void
27061d545c7aSMarius Strobl re_watchdog(sc)
2707a94100faSBill Paul 	struct rl_softc		*sc;
27081d545c7aSMarius Strobl {
2709a94100faSBill Paul 
27101d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
27111d545c7aSMarius Strobl 
27121d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
27131d545c7aSMarius Strobl 		return;
27141d545c7aSMarius Strobl 
27151d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
27161d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2717a94100faSBill Paul 
2718a94100faSBill Paul 	re_txeof(sc);
2719a94100faSBill Paul 	re_rxeof(sc);
272097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2721a94100faSBill Paul }
2722a94100faSBill Paul 
2723a94100faSBill Paul /*
2724a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2725a94100faSBill Paul  * RX and TX lists.
2726a94100faSBill Paul  */
2727a94100faSBill Paul static void
2728a94100faSBill Paul re_stop(sc)
2729a94100faSBill Paul 	struct rl_softc		*sc;
2730a94100faSBill Paul {
2731a94100faSBill Paul 	register int		i;
2732a94100faSBill Paul 	struct ifnet		*ifp;
2733a94100faSBill Paul 
273497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
273597b9d4baSJohn-Mark Gurney 
2736fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2737a94100faSBill Paul 
27381d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2739d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
274013f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2741a94100faSBill Paul 
2742a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2743a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2744ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2745a94100faSBill Paul 
2746a94100faSBill Paul 	if (sc->rl_head != NULL) {
2747a94100faSBill Paul 		m_freem(sc->rl_head);
2748a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2749a94100faSBill Paul 	}
2750a94100faSBill Paul 
2751a94100faSBill Paul 	/* Free the TX list buffers. */
2752a94100faSBill Paul 
2753a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2754a94100faSBill Paul 		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2755a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2756a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
2757a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2758a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2759a94100faSBill Paul 		}
2760a94100faSBill Paul 	}
2761a94100faSBill Paul 
2762a94100faSBill Paul 	/* Free the RX list buffers. */
2763a94100faSBill Paul 
2764a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2765a94100faSBill Paul 		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2766a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2767a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
2768a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2769a94100faSBill Paul 			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2770a94100faSBill Paul 		}
2771a94100faSBill Paul 	}
2772a94100faSBill Paul }
2773a94100faSBill Paul 
2774a94100faSBill Paul /*
2775a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2776a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2777a94100faSBill Paul  * resume.
2778a94100faSBill Paul  */
2779a94100faSBill Paul static int
2780a94100faSBill Paul re_suspend(dev)
2781a94100faSBill Paul 	device_t		dev;
2782a94100faSBill Paul {
2783a94100faSBill Paul 	struct rl_softc		*sc;
2784a94100faSBill Paul 
2785a94100faSBill Paul 	sc = device_get_softc(dev);
2786a94100faSBill Paul 
278797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2788a94100faSBill Paul 	re_stop(sc);
2789a94100faSBill Paul 	sc->suspended = 1;
279097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2791a94100faSBill Paul 
2792a94100faSBill Paul 	return (0);
2793a94100faSBill Paul }
2794a94100faSBill Paul 
2795a94100faSBill Paul /*
2796a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2797a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2798a94100faSBill Paul  * appropriate.
2799a94100faSBill Paul  */
2800a94100faSBill Paul static int
2801a94100faSBill Paul re_resume(dev)
2802a94100faSBill Paul 	device_t		dev;
2803a94100faSBill Paul {
2804a94100faSBill Paul 	struct rl_softc		*sc;
2805a94100faSBill Paul 	struct ifnet		*ifp;
2806a94100faSBill Paul 
2807a94100faSBill Paul 	sc = device_get_softc(dev);
280897b9d4baSJohn-Mark Gurney 
280997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
281097b9d4baSJohn-Mark Gurney 
2811fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2812a94100faSBill Paul 
2813a94100faSBill Paul 	/* reinitialize interface if necessary */
2814a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
281597b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2816a94100faSBill Paul 
2817a94100faSBill Paul 	sc->suspended = 0;
281897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2819a94100faSBill Paul 
2820a94100faSBill Paul 	return (0);
2821a94100faSBill Paul }
2822a94100faSBill Paul 
2823a94100faSBill Paul /*
2824a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2825a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2826a94100faSBill Paul  */
28276a087a87SPyun YongHyeon static int
2828a94100faSBill Paul re_shutdown(dev)
2829a94100faSBill Paul 	device_t		dev;
2830a94100faSBill Paul {
2831a94100faSBill Paul 	struct rl_softc		*sc;
2832a94100faSBill Paul 
2833a94100faSBill Paul 	sc = device_get_softc(dev);
2834a94100faSBill Paul 
283597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2836a94100faSBill Paul 	re_stop(sc);
2837536fde34SMaxim Sobolev 	/*
2838536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2839536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
284072293673SRuslan Ermilov 	 * cases.
2841536fde34SMaxim Sobolev 	 */
2842536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
284397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
28446a087a87SPyun YongHyeon 
28456a087a87SPyun YongHyeon 	return (0);
2846a94100faSBill Paul }
2847