if_re.c revision 250458
1/*-
2 * Copyright (c) 1997, 1998-2003
3 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/9/sys/dev/re/if_re.c 250458 2013-05-10 16:16:33Z luigi $");
35
36/*
37 * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
38 *
39 * Written by Bill Paul <wpaul@windriver.com>
40 * Senior Networking Software Engineer
41 * Wind River Systems
42 */
43
44/*
45 * This driver is designed to support RealTek's next generation of
46 * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
47 * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
48 * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
49 *
50 * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
51 * with the older 8139 family, however it also supports a special
52 * C+ mode of operation that provides several new performance enhancing
53 * features. These include:
54 *
55 *	o Descriptor based DMA mechanism. Each descriptor represents
56 *	  a single packet fragment. Data buffers may be aligned on
57 *	  any byte boundary.
58 *
59 *	o 64-bit DMA
60 *
61 *	o TCP/IP checksum offload for both RX and TX
62 *
63 *	o High and normal priority transmit DMA rings
64 *
65 *	o VLAN tag insertion and extraction
66 *
67 *	o TCP large send (segmentation offload)
68 *
69 * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
70 * programming API is fairly straightforward. The RX filtering, EEPROM
71 * access and PHY access is the same as it is on the older 8139 series
72 * chips.
73 *
74 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
75 * same programming API and feature set as the 8139C+ with the following
76 * differences and additions:
77 *
78 *	o 1000Mbps mode
79 *
80 *	o Jumbo frames
81 *
82 *	o GMII and TBI ports/registers for interfacing with copper
83 *	  or fiber PHYs
84 *
85 *	o RX and TX DMA rings can have up to 1024 descriptors
86 *	  (the 8139C+ allows a maximum of 64)
87 *
88 *	o Slight differences in register layout from the 8139C+
89 *
90 * The TX start and timer interrupt registers are at different locations
91 * on the 8169 than they are on the 8139C+. Also, the status word in the
92 * RX descriptor has a slightly different bit layout. The 8169 does not
93 * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
94 * copper gigE PHY.
95 *
96 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
97 * (the 'S' stands for 'single-chip'). These devices have the same
98 * programming API as the older 8169, but also have some vendor-specific
99 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
100 * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
101 *
102 * This driver takes advantage of the RX and TX checksum offload and
103 * VLAN tag insertion/extraction features. It also implements TX
104 * interrupt moderation using the timer interrupt registers, which
105 * significantly reduces TX interrupt load. There is also support
106 * for jumbo frames, however the 8169/8169S/8110S can not transmit
107 * jumbo frames larger than 7440, so the max MTU possible with this
108 * driver is 7422 bytes.
109 */
110
111#ifdef HAVE_KERNEL_OPTION_HEADERS
112#include "opt_device_polling.h"
113#endif
114
115#include <sys/param.h>
116#include <sys/endian.h>
117#include <sys/systm.h>
118#include <sys/sockio.h>
119#include <sys/mbuf.h>
120#include <sys/malloc.h>
121#include <sys/module.h>
122#include <sys/kernel.h>
123#include <sys/socket.h>
124#include <sys/lock.h>
125#include <sys/mutex.h>
126#include <sys/sysctl.h>
127#include <sys/taskqueue.h>
128
129#include <net/if.h>
130#include <net/if_arp.h>
131#include <net/ethernet.h>
132#include <net/if_dl.h>
133#include <net/if_media.h>
134#include <net/if_types.h>
135#include <net/if_vlan_var.h>
136
137#include <net/bpf.h>
138
139#include <machine/bus.h>
140#include <machine/resource.h>
141#include <sys/bus.h>
142#include <sys/rman.h>
143
144#include <dev/mii/mii.h>
145#include <dev/mii/miivar.h>
146
147#include <dev/pci/pcireg.h>
148#include <dev/pci/pcivar.h>
149
150#include <pci/if_rlreg.h>
151
152MODULE_DEPEND(re, pci, 1, 1, 1);
153MODULE_DEPEND(re, ether, 1, 1, 1);
154MODULE_DEPEND(re, miibus, 1, 1, 1);
155
156/* "device miibus" required.  See GENERIC if you get errors here. */
157#include "miibus_if.h"
158
159/* Tunables. */
160static int intr_filter = 0;
161TUNABLE_INT("hw.re.intr_filter", &intr_filter);
162static int msi_disable = 0;
163TUNABLE_INT("hw.re.msi_disable", &msi_disable);
164static int msix_disable = 0;
165TUNABLE_INT("hw.re.msix_disable", &msix_disable);
166static int prefer_iomap = 0;
167TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
168
169#define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
170
171/*
172 * Various supported device vendors/types and their names.
173 */
174static const struct rl_type re_devs[] = {
175	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
176	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
177	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
178	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
179	{ RT_VENDORID, RT_DEVICEID_8139, 0,
180	    "RealTek 8139C+ 10/100BaseTX" },
181	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
182	    "RealTek 810xE PCIe 10/100baseTX" },
183	{ RT_VENDORID, RT_DEVICEID_8168, 0,
184	    "RealTek 8168/8111 B/C/CP/D/DP/E/F PCIe Gigabit Ethernet" },
185	{ RT_VENDORID, RT_DEVICEID_8169, 0,
186	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
187	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
188	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
189	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
190	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
191	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
192	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
193	{ USR_VENDORID, USR_DEVICEID_997902, 0,
194	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
195};
196
197static const struct rl_hwrev re_hwrevs[] = {
198	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
199	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
200	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
201	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
202	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
203	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
204	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
205	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
206	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
207	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
208	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
209	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
210	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
211	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
212	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
213	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
214	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
215	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
216	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
217	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
218	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
219	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
220	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
221	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
222	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
223	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
224	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
225	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
226	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
227	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
228	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
229	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
230	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
231	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
232	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
233	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
234	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
235	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
236	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
237	{ 0, 0, NULL, 0 }
238};
239
240static int re_probe		(device_t);
241static int re_attach		(device_t);
242static int re_detach		(device_t);
243
244static int re_encap		(struct rl_softc *, struct mbuf **);
245
246static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
247static int re_allocmem		(device_t, struct rl_softc *);
248static __inline void re_discard_rxbuf
249				(struct rl_softc *, int);
250static int re_newbuf		(struct rl_softc *, int);
251static int re_jumbo_newbuf	(struct rl_softc *, int);
252static int re_rx_list_init	(struct rl_softc *);
253static int re_jrx_list_init	(struct rl_softc *);
254static int re_tx_list_init	(struct rl_softc *);
255#ifdef RE_FIXUP_RX
256static __inline void re_fixup_rx
257				(struct mbuf *);
258#endif
259static int re_rxeof		(struct rl_softc *, int *);
260static void re_txeof		(struct rl_softc *);
261#ifdef DEVICE_POLLING
262static int re_poll		(struct ifnet *, enum poll_cmd, int);
263static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
264#endif
265static int re_intr		(void *);
266static void re_intr_msi		(void *);
267static void re_tick		(void *);
268static void re_int_task		(void *, int);
269static void re_start		(struct ifnet *);
270static void re_start_locked	(struct ifnet *);
271static int re_ioctl		(struct ifnet *, u_long, caddr_t);
272static void re_init		(void *);
273static void re_init_locked	(struct rl_softc *);
274static void re_stop		(struct rl_softc *);
275static void re_watchdog		(struct rl_softc *);
276static int re_suspend		(device_t);
277static int re_resume		(device_t);
278static int re_shutdown		(device_t);
279static int re_ifmedia_upd	(struct ifnet *);
280static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
281
282static void re_eeprom_putbyte	(struct rl_softc *, int);
283static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
284static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
285static int re_gmii_readreg	(device_t, int, int);
286static int re_gmii_writereg	(device_t, int, int, int);
287
288static int re_miibus_readreg	(device_t, int, int);
289static int re_miibus_writereg	(device_t, int, int, int);
290static void re_miibus_statchg	(device_t);
291
292static void re_set_jumbo	(struct rl_softc *, int);
293static void re_set_rxmode		(struct rl_softc *);
294static void re_reset		(struct rl_softc *);
295static void re_setwol		(struct rl_softc *);
296static void re_clrwol		(struct rl_softc *);
297static void re_set_linkspeed	(struct rl_softc *);
298
299#ifdef DEV_NETMAP	/* see ixgbe.c for details */
300#include <dev/netmap/if_re_netmap.h>
301#endif /* !DEV_NETMAP */
302
303#ifdef RE_DIAG
304static int re_diag		(struct rl_softc *);
305#endif
306
307static void re_add_sysctls	(struct rl_softc *);
308static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
309static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
310static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
311
312static device_method_t re_methods[] = {
313	/* Device interface */
314	DEVMETHOD(device_probe,		re_probe),
315	DEVMETHOD(device_attach,	re_attach),
316	DEVMETHOD(device_detach,	re_detach),
317	DEVMETHOD(device_suspend,	re_suspend),
318	DEVMETHOD(device_resume,	re_resume),
319	DEVMETHOD(device_shutdown,	re_shutdown),
320
321	/* MII interface */
322	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
323	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
324	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
325
326	DEVMETHOD_END
327};
328
329static driver_t re_driver = {
330	"re",
331	re_methods,
332	sizeof(struct rl_softc)
333};
334
335static devclass_t re_devclass;
336
337DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
338DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
339
340#define EE_SET(x)					\
341	CSR_WRITE_1(sc, RL_EECMD,			\
342		CSR_READ_1(sc, RL_EECMD) | x)
343
344#define EE_CLR(x)					\
345	CSR_WRITE_1(sc, RL_EECMD,			\
346		CSR_READ_1(sc, RL_EECMD) & ~x)
347
348/*
349 * Send a read command and address to the EEPROM, check for ACK.
350 */
351static void
352re_eeprom_putbyte(struct rl_softc *sc, int addr)
353{
354	int			d, i;
355
356	d = addr | (RL_9346_READ << sc->rl_eewidth);
357
358	/*
359	 * Feed in each bit and strobe the clock.
360	 */
361
362	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
363		if (d & i) {
364			EE_SET(RL_EE_DATAIN);
365		} else {
366			EE_CLR(RL_EE_DATAIN);
367		}
368		DELAY(100);
369		EE_SET(RL_EE_CLK);
370		DELAY(150);
371		EE_CLR(RL_EE_CLK);
372		DELAY(100);
373	}
374}
375
376/*
377 * Read a word of data stored in the EEPROM at address 'addr.'
378 */
379static void
380re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
381{
382	int			i;
383	u_int16_t		word = 0;
384
385	/*
386	 * Send address of word we want to read.
387	 */
388	re_eeprom_putbyte(sc, addr);
389
390	/*
391	 * Start reading bits from EEPROM.
392	 */
393	for (i = 0x8000; i; i >>= 1) {
394		EE_SET(RL_EE_CLK);
395		DELAY(100);
396		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
397			word |= i;
398		EE_CLR(RL_EE_CLK);
399		DELAY(100);
400	}
401
402	*dest = word;
403}
404
405/*
406 * Read a sequence of words from the EEPROM.
407 */
408static void
409re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
410{
411	int			i;
412	u_int16_t		word = 0, *ptr;
413
414	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
415
416        DELAY(100);
417
418	for (i = 0; i < cnt; i++) {
419		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
420		re_eeprom_getword(sc, off + i, &word);
421		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
422		ptr = (u_int16_t *)(dest + (i * 2));
423                *ptr = word;
424	}
425
426	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
427}
428
429static int
430re_gmii_readreg(device_t dev, int phy, int reg)
431{
432	struct rl_softc		*sc;
433	u_int32_t		rval;
434	int			i;
435
436	sc = device_get_softc(dev);
437
438	/* Let the rgephy driver read the GMEDIASTAT register */
439
440	if (reg == RL_GMEDIASTAT) {
441		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
442		return (rval);
443	}
444
445	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
446
447	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
448		rval = CSR_READ_4(sc, RL_PHYAR);
449		if (rval & RL_PHYAR_BUSY)
450			break;
451		DELAY(25);
452	}
453
454	if (i == RL_PHY_TIMEOUT) {
455		device_printf(sc->rl_dev, "PHY read failed\n");
456		return (0);
457	}
458
459	/*
460	 * Controller requires a 20us delay to process next MDIO request.
461	 */
462	DELAY(20);
463
464	return (rval & RL_PHYAR_PHYDATA);
465}
466
467static int
468re_gmii_writereg(device_t dev, int phy, int reg, int data)
469{
470	struct rl_softc		*sc;
471	u_int32_t		rval;
472	int			i;
473
474	sc = device_get_softc(dev);
475
476	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
477	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
478
479	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
480		rval = CSR_READ_4(sc, RL_PHYAR);
481		if (!(rval & RL_PHYAR_BUSY))
482			break;
483		DELAY(25);
484	}
485
486	if (i == RL_PHY_TIMEOUT) {
487		device_printf(sc->rl_dev, "PHY write failed\n");
488		return (0);
489	}
490
491	/*
492	 * Controller requires a 20us delay to process next MDIO request.
493	 */
494	DELAY(20);
495
496	return (0);
497}
498
499static int
500re_miibus_readreg(device_t dev, int phy, int reg)
501{
502	struct rl_softc		*sc;
503	u_int16_t		rval = 0;
504	u_int16_t		re8139_reg = 0;
505
506	sc = device_get_softc(dev);
507
508	if (sc->rl_type == RL_8169) {
509		rval = re_gmii_readreg(dev, phy, reg);
510		return (rval);
511	}
512
513	switch (reg) {
514	case MII_BMCR:
515		re8139_reg = RL_BMCR;
516		break;
517	case MII_BMSR:
518		re8139_reg = RL_BMSR;
519		break;
520	case MII_ANAR:
521		re8139_reg = RL_ANAR;
522		break;
523	case MII_ANER:
524		re8139_reg = RL_ANER;
525		break;
526	case MII_ANLPAR:
527		re8139_reg = RL_LPAR;
528		break;
529	case MII_PHYIDR1:
530	case MII_PHYIDR2:
531		return (0);
532	/*
533	 * Allow the rlphy driver to read the media status
534	 * register. If we have a link partner which does not
535	 * support NWAY, this is the register which will tell
536	 * us the results of parallel detection.
537	 */
538	case RL_MEDIASTAT:
539		rval = CSR_READ_1(sc, RL_MEDIASTAT);
540		return (rval);
541	default:
542		device_printf(sc->rl_dev, "bad phy register\n");
543		return (0);
544	}
545	rval = CSR_READ_2(sc, re8139_reg);
546	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
547		/* 8139C+ has different bit layout. */
548		rval &= ~(BMCR_LOOP | BMCR_ISO);
549	}
550	return (rval);
551}
552
553static int
554re_miibus_writereg(device_t dev, int phy, int reg, int data)
555{
556	struct rl_softc		*sc;
557	u_int16_t		re8139_reg = 0;
558	int			rval = 0;
559
560	sc = device_get_softc(dev);
561
562	if (sc->rl_type == RL_8169) {
563		rval = re_gmii_writereg(dev, phy, reg, data);
564		return (rval);
565	}
566
567	switch (reg) {
568	case MII_BMCR:
569		re8139_reg = RL_BMCR;
570		if (sc->rl_type == RL_8139CPLUS) {
571			/* 8139C+ has different bit layout. */
572			data &= ~(BMCR_LOOP | BMCR_ISO);
573		}
574		break;
575	case MII_BMSR:
576		re8139_reg = RL_BMSR;
577		break;
578	case MII_ANAR:
579		re8139_reg = RL_ANAR;
580		break;
581	case MII_ANER:
582		re8139_reg = RL_ANER;
583		break;
584	case MII_ANLPAR:
585		re8139_reg = RL_LPAR;
586		break;
587	case MII_PHYIDR1:
588	case MII_PHYIDR2:
589		return (0);
590		break;
591	default:
592		device_printf(sc->rl_dev, "bad phy register\n");
593		return (0);
594	}
595	CSR_WRITE_2(sc, re8139_reg, data);
596	return (0);
597}
598
599static void
600re_miibus_statchg(device_t dev)
601{
602	struct rl_softc		*sc;
603	struct ifnet		*ifp;
604	struct mii_data		*mii;
605
606	sc = device_get_softc(dev);
607	mii = device_get_softc(sc->rl_miibus);
608	ifp = sc->rl_ifp;
609	if (mii == NULL || ifp == NULL ||
610	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
611		return;
612
613	sc->rl_flags &= ~RL_FLAG_LINK;
614	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
615	    (IFM_ACTIVE | IFM_AVALID)) {
616		switch (IFM_SUBTYPE(mii->mii_media_active)) {
617		case IFM_10_T:
618		case IFM_100_TX:
619			sc->rl_flags |= RL_FLAG_LINK;
620			break;
621		case IFM_1000_T:
622			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
623				break;
624			sc->rl_flags |= RL_FLAG_LINK;
625			break;
626		default:
627			break;
628		}
629	}
630	/*
631	 * RealTek controllers does not provide any interface to
632	 * Tx/Rx MACs for resolved speed, duplex and flow-control
633	 * parameters.
634	 */
635}
636
637/*
638 * Set the RX configuration and 64-bit multicast hash filter.
639 */
640static void
641re_set_rxmode(struct rl_softc *sc)
642{
643	struct ifnet		*ifp;
644	struct ifmultiaddr	*ifma;
645	uint32_t		hashes[2] = { 0, 0 };
646	uint32_t		h, rxfilt;
647
648	RL_LOCK_ASSERT(sc);
649
650	ifp = sc->rl_ifp;
651
652	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
653
654	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
655		if (ifp->if_flags & IFF_PROMISC)
656			rxfilt |= RL_RXCFG_RX_ALLPHYS;
657		/*
658		 * Unlike other hardwares, we have to explicitly set
659		 * RL_RXCFG_RX_MULTI to receive multicast frames in
660		 * promiscuous mode.
661		 */
662		rxfilt |= RL_RXCFG_RX_MULTI;
663		hashes[0] = hashes[1] = 0xffffffff;
664		goto done;
665	}
666
667	if_maddr_rlock(ifp);
668	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
669		if (ifma->ifma_addr->sa_family != AF_LINK)
670			continue;
671		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
672		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
673		if (h < 32)
674			hashes[0] |= (1 << h);
675		else
676			hashes[1] |= (1 << (h - 32));
677	}
678	if_maddr_runlock(ifp);
679
680	if (hashes[0] != 0 || hashes[1] != 0) {
681		/*
682		 * For some unfathomable reason, RealTek decided to
683		 * reverse the order of the multicast hash registers
684		 * in the PCI Express parts.  This means we have to
685		 * write the hash pattern in reverse order for those
686		 * devices.
687		 */
688		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
689			h = bswap32(hashes[0]);
690			hashes[0] = bswap32(hashes[1]);
691			hashes[1] = h;
692		}
693		rxfilt |= RL_RXCFG_RX_MULTI;
694	}
695
696done:
697	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
698	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
699	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
700}
701
702static void
703re_reset(struct rl_softc *sc)
704{
705	int			i;
706
707	RL_LOCK_ASSERT(sc);
708
709	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
710
711	for (i = 0; i < RL_TIMEOUT; i++) {
712		DELAY(10);
713		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
714			break;
715	}
716	if (i == RL_TIMEOUT)
717		device_printf(sc->rl_dev, "reset never completed!\n");
718
719	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
720		CSR_WRITE_1(sc, 0x82, 1);
721	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
722		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
723}
724
725#ifdef RE_DIAG
726
727/*
728 * The following routine is designed to test for a defect on some
729 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
730 * lines connected to the bus, however for a 32-bit only card, they
731 * should be pulled high. The result of this defect is that the
732 * NIC will not work right if you plug it into a 64-bit slot: DMA
733 * operations will be done with 64-bit transfers, which will fail
734 * because the 64-bit data lines aren't connected.
735 *
736 * There's no way to work around this (short of talking a soldering
737 * iron to the board), however we can detect it. The method we use
738 * here is to put the NIC into digital loopback mode, set the receiver
739 * to promiscuous mode, and then try to send a frame. We then compare
740 * the frame data we sent to what was received. If the data matches,
741 * then the NIC is working correctly, otherwise we know the user has
742 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
743 * slot. In the latter case, there's no way the NIC can work correctly,
744 * so we print out a message on the console and abort the device attach.
745 */
746
747static int
748re_diag(struct rl_softc *sc)
749{
750	struct ifnet		*ifp = sc->rl_ifp;
751	struct mbuf		*m0;
752	struct ether_header	*eh;
753	struct rl_desc		*cur_rx;
754	u_int16_t		status;
755	u_int32_t		rxstat;
756	int			total_len, i, error = 0, phyaddr;
757	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
758	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
759
760	/* Allocate a single mbuf */
761	MGETHDR(m0, M_NOWAIT, MT_DATA);
762	if (m0 == NULL)
763		return (ENOBUFS);
764
765	RL_LOCK(sc);
766
767	/*
768	 * Initialize the NIC in test mode. This sets the chip up
769	 * so that it can send and receive frames, but performs the
770	 * following special functions:
771	 * - Puts receiver in promiscuous mode
772	 * - Enables digital loopback mode
773	 * - Leaves interrupts turned off
774	 */
775
776	ifp->if_flags |= IFF_PROMISC;
777	sc->rl_testmode = 1;
778	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
779	re_init_locked(sc);
780	sc->rl_flags |= RL_FLAG_LINK;
781	if (sc->rl_type == RL_8169)
782		phyaddr = 1;
783	else
784		phyaddr = 0;
785
786	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
787	for (i = 0; i < RL_TIMEOUT; i++) {
788		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
789		if (!(status & BMCR_RESET))
790			break;
791	}
792
793	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
794	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
795
796	DELAY(100000);
797
798	/* Put some data in the mbuf */
799
800	eh = mtod(m0, struct ether_header *);
801	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
802	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
803	eh->ether_type = htons(ETHERTYPE_IP);
804	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
805
806	/*
807	 * Queue the packet, start transmission.
808	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
809	 */
810
811	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
812	RL_UNLOCK(sc);
813	/* XXX: re_diag must not be called when in ALTQ mode */
814	IF_HANDOFF(&ifp->if_snd, m0, ifp);
815	RL_LOCK(sc);
816	m0 = NULL;
817
818	/* Wait for it to propagate through the chip */
819
820	DELAY(100000);
821	for (i = 0; i < RL_TIMEOUT; i++) {
822		status = CSR_READ_2(sc, RL_ISR);
823		CSR_WRITE_2(sc, RL_ISR, status);
824		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
825		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
826			break;
827		DELAY(10);
828	}
829
830	if (i == RL_TIMEOUT) {
831		device_printf(sc->rl_dev,
832		    "diagnostic failed, failed to receive packet in"
833		    " loopback mode\n");
834		error = EIO;
835		goto done;
836	}
837
838	/*
839	 * The packet should have been dumped into the first
840	 * entry in the RX DMA ring. Grab it from there.
841	 */
842
843	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
844	    sc->rl_ldata.rl_rx_list_map,
845	    BUS_DMASYNC_POSTREAD);
846	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
847	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
848	    BUS_DMASYNC_POSTREAD);
849	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
850	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
851
852	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
853	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
854	eh = mtod(m0, struct ether_header *);
855
856	cur_rx = &sc->rl_ldata.rl_rx_list[0];
857	total_len = RL_RXBYTES(cur_rx);
858	rxstat = le32toh(cur_rx->rl_cmdstat);
859
860	if (total_len != ETHER_MIN_LEN) {
861		device_printf(sc->rl_dev,
862		    "diagnostic failed, received short packet\n");
863		error = EIO;
864		goto done;
865	}
866
867	/* Test that the received packet data matches what we sent. */
868
869	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
870	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
871	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
872		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
873		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
874		    dst, ":", src, ":", ETHERTYPE_IP);
875		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
876		    eh->ether_dhost, ":", eh->ether_shost, ":",
877		    ntohs(eh->ether_type));
878		device_printf(sc->rl_dev, "You may have a defective 32-bit "
879		    "NIC plugged into a 64-bit PCI slot.\n");
880		device_printf(sc->rl_dev, "Please re-install the NIC in a "
881		    "32-bit slot for proper operation.\n");
882		device_printf(sc->rl_dev, "Read the re(4) man page for more "
883		    "details.\n");
884		error = EIO;
885	}
886
887done:
888	/* Turn interface off, release resources */
889
890	sc->rl_testmode = 0;
891	sc->rl_flags &= ~RL_FLAG_LINK;
892	ifp->if_flags &= ~IFF_PROMISC;
893	re_stop(sc);
894	if (m0 != NULL)
895		m_freem(m0);
896
897	RL_UNLOCK(sc);
898
899	return (error);
900}
901
902#endif
903
904/*
905 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
906 * IDs against our list and return a device name if we find a match.
907 */
908static int
909re_probe(device_t dev)
910{
911	const struct rl_type	*t;
912	uint16_t		devid, vendor;
913	uint16_t		revid, sdevid;
914	int			i;
915
916	vendor = pci_get_vendor(dev);
917	devid = pci_get_device(dev);
918	revid = pci_get_revid(dev);
919	sdevid = pci_get_subdevice(dev);
920
921	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
922		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
923			/*
924			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
925			 * Rev. 2 is supported by sk(4).
926			 */
927			return (ENXIO);
928		}
929	}
930
931	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
932		if (revid != 0x20) {
933			/* 8139, let rl(4) take care of this device. */
934			return (ENXIO);
935		}
936	}
937
938	t = re_devs;
939	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
940		if (vendor == t->rl_vid && devid == t->rl_did) {
941			device_set_desc(dev, t->rl_name);
942			return (BUS_PROBE_DEFAULT);
943		}
944	}
945
946	return (ENXIO);
947}
948
949/*
950 * Map a single buffer address.
951 */
952
953static void
954re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
955{
956	bus_addr_t		*addr;
957
958	if (error)
959		return;
960
961	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
962	addr = arg;
963	*addr = segs->ds_addr;
964}
965
966static int
967re_allocmem(device_t dev, struct rl_softc *sc)
968{
969	bus_addr_t		lowaddr;
970	bus_size_t		rx_list_size, tx_list_size;
971	int			error;
972	int			i;
973
974	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
975	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
976
977	/*
978	 * Allocate the parent bus DMA tag appropriate for PCI.
979	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
980	 * register should be set. However some RealTek chips are known
981	 * to be buggy on DAC handling, therefore disable DAC by limiting
982	 * DMA address space to 32bit. PCIe variants of RealTek chips
983	 * may not have the limitation.
984	 */
985	lowaddr = BUS_SPACE_MAXADDR;
986	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
987		lowaddr = BUS_SPACE_MAXADDR_32BIT;
988	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
989	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
990	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
991	    NULL, NULL, &sc->rl_parent_tag);
992	if (error) {
993		device_printf(dev, "could not allocate parent DMA tag\n");
994		return (error);
995	}
996
997	/*
998	 * Allocate map for TX mbufs.
999	 */
1000	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1001	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1002	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1003	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1004	if (error) {
1005		device_printf(dev, "could not allocate TX DMA tag\n");
1006		return (error);
1007	}
1008
1009	/*
1010	 * Allocate map for RX mbufs.
1011	 */
1012
1013	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1014		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
1015		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1016		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
1017		    &sc->rl_ldata.rl_jrx_mtag);
1018		if (error) {
1019			device_printf(dev,
1020			    "could not allocate jumbo RX DMA tag\n");
1021			return (error);
1022		}
1023	}
1024	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1025	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1026	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1027	if (error) {
1028		device_printf(dev, "could not allocate RX DMA tag\n");
1029		return (error);
1030	}
1031
1032	/*
1033	 * Allocate map for TX descriptor list.
1034	 */
1035	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1036	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1037	    NULL, tx_list_size, 1, tx_list_size, 0,
1038	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1039	if (error) {
1040		device_printf(dev, "could not allocate TX DMA ring tag\n");
1041		return (error);
1042	}
1043
1044	/* Allocate DMA'able memory for the TX ring */
1045
1046	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1047	    (void **)&sc->rl_ldata.rl_tx_list,
1048	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1049	    &sc->rl_ldata.rl_tx_list_map);
1050	if (error) {
1051		device_printf(dev, "could not allocate TX DMA ring\n");
1052		return (error);
1053	}
1054
1055	/* Load the map for the TX ring. */
1056
1057	sc->rl_ldata.rl_tx_list_addr = 0;
1058	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1059	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1060	     tx_list_size, re_dma_map_addr,
1061	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1062	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1063		device_printf(dev, "could not load TX DMA ring\n");
1064		return (ENOMEM);
1065	}
1066
1067	/* Create DMA maps for TX buffers */
1068
1069	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1070		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1071		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1072		if (error) {
1073			device_printf(dev, "could not create DMA map for TX\n");
1074			return (error);
1075		}
1076	}
1077
1078	/*
1079	 * Allocate map for RX descriptor list.
1080	 */
1081	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1082	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1083	    NULL, rx_list_size, 1, rx_list_size, 0,
1084	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1085	if (error) {
1086		device_printf(dev, "could not create RX DMA ring tag\n");
1087		return (error);
1088	}
1089
1090	/* Allocate DMA'able memory for the RX ring */
1091
1092	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1093	    (void **)&sc->rl_ldata.rl_rx_list,
1094	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1095	    &sc->rl_ldata.rl_rx_list_map);
1096	if (error) {
1097		device_printf(dev, "could not allocate RX DMA ring\n");
1098		return (error);
1099	}
1100
1101	/* Load the map for the RX ring. */
1102
1103	sc->rl_ldata.rl_rx_list_addr = 0;
1104	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1105	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1106	     rx_list_size, re_dma_map_addr,
1107	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1108	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1109		device_printf(dev, "could not load RX DMA ring\n");
1110		return (ENOMEM);
1111	}
1112
1113	/* Create DMA maps for RX buffers */
1114
1115	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1116		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1117		    &sc->rl_ldata.rl_jrx_sparemap);
1118		if (error) {
1119			device_printf(dev,
1120			    "could not create spare DMA map for jumbo RX\n");
1121			return (error);
1122		}
1123		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1124			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1125			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1126			if (error) {
1127				device_printf(dev,
1128				    "could not create DMA map for jumbo RX\n");
1129				return (error);
1130			}
1131		}
1132	}
1133	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1134	    &sc->rl_ldata.rl_rx_sparemap);
1135	if (error) {
1136		device_printf(dev, "could not create spare DMA map for RX\n");
1137		return (error);
1138	}
1139	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1140		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1141		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1142		if (error) {
1143			device_printf(dev, "could not create DMA map for RX\n");
1144			return (error);
1145		}
1146	}
1147
1148	/* Create DMA map for statistics. */
1149	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
1150	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1151	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
1152	    &sc->rl_ldata.rl_stag);
1153	if (error) {
1154		device_printf(dev, "could not create statistics DMA tag\n");
1155		return (error);
1156	}
1157	/* Allocate DMA'able memory for statistics. */
1158	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
1159	    (void **)&sc->rl_ldata.rl_stats,
1160	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1161	    &sc->rl_ldata.rl_smap);
1162	if (error) {
1163		device_printf(dev,
1164		    "could not allocate statistics DMA memory\n");
1165		return (error);
1166	}
1167	/* Load the map for statistics. */
1168	sc->rl_ldata.rl_stats_addr = 0;
1169	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
1170	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
1171	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
1172	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
1173		device_printf(dev, "could not load statistics DMA memory\n");
1174		return (ENOMEM);
1175	}
1176
1177	return (0);
1178}
1179
1180/*
1181 * Attach the interface. Allocate softc structures, do ifmedia
1182 * setup and ethernet/BPF attach.
1183 */
1184static int
1185re_attach(device_t dev)
1186{
1187	u_char			eaddr[ETHER_ADDR_LEN];
1188	u_int16_t		as[ETHER_ADDR_LEN / 2];
1189	struct rl_softc		*sc;
1190	struct ifnet		*ifp;
1191	const struct rl_hwrev	*hw_rev;
1192	u_int32_t		cap, ctl;
1193	int			hwrev;
1194	u_int16_t		devid, re_did = 0;
1195	int			error = 0, i, phy, rid;
1196	int			msic, msixc, reg;
1197	uint8_t			cfg;
1198
1199	sc = device_get_softc(dev);
1200	sc->rl_dev = dev;
1201
1202	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1203	    MTX_DEF);
1204	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1205
1206	/*
1207	 * Map control/status registers.
1208	 */
1209	pci_enable_busmaster(dev);
1210
1211	devid = pci_get_device(dev);
1212	/*
1213	 * Prefer memory space register mapping over IO space.
1214	 * Because RTL8169SC does not seem to work when memory mapping
1215	 * is used always activate io mapping.
1216	 */
1217	if (devid == RT_DEVICEID_8169SC)
1218		prefer_iomap = 1;
1219	if (prefer_iomap == 0) {
1220		sc->rl_res_id = PCIR_BAR(1);
1221		sc->rl_res_type = SYS_RES_MEMORY;
1222		/* RTL8168/8101E seems to use different BARs. */
1223		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1224			sc->rl_res_id = PCIR_BAR(2);
1225	} else {
1226		sc->rl_res_id = PCIR_BAR(0);
1227		sc->rl_res_type = SYS_RES_IOPORT;
1228	}
1229	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1230	    &sc->rl_res_id, RF_ACTIVE);
1231	if (sc->rl_res == NULL && prefer_iomap == 0) {
1232		sc->rl_res_id = PCIR_BAR(0);
1233		sc->rl_res_type = SYS_RES_IOPORT;
1234		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1235		    &sc->rl_res_id, RF_ACTIVE);
1236	}
1237	if (sc->rl_res == NULL) {
1238		device_printf(dev, "couldn't map ports/memory\n");
1239		error = ENXIO;
1240		goto fail;
1241	}
1242
1243	sc->rl_btag = rman_get_bustag(sc->rl_res);
1244	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1245
1246	msic = pci_msi_count(dev);
1247	msixc = pci_msix_count(dev);
1248	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
1249		sc->rl_flags |= RL_FLAG_PCIE;
1250		sc->rl_expcap = reg;
1251	}
1252	if (bootverbose) {
1253		device_printf(dev, "MSI count : %d\n", msic);
1254		device_printf(dev, "MSI-X count : %d\n", msixc);
1255	}
1256	if (msix_disable > 0)
1257		msixc = 0;
1258	if (msi_disable > 0)
1259		msic = 0;
1260	/* Prefer MSI-X to MSI. */
1261	if (msixc > 0) {
1262		msixc = 1;
1263		rid = PCIR_BAR(4);
1264		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1265		    &rid, RF_ACTIVE);
1266		if (sc->rl_res_pba == NULL) {
1267			device_printf(sc->rl_dev,
1268			    "could not allocate MSI-X PBA resource\n");
1269		}
1270		if (sc->rl_res_pba != NULL &&
1271		    pci_alloc_msix(dev, &msixc) == 0) {
1272			if (msixc == 1) {
1273				device_printf(dev, "Using %d MSI-X message\n",
1274				    msixc);
1275				sc->rl_flags |= RL_FLAG_MSIX;
1276			} else
1277				pci_release_msi(dev);
1278		}
1279		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
1280			if (sc->rl_res_pba != NULL)
1281				bus_release_resource(dev, SYS_RES_MEMORY, rid,
1282				    sc->rl_res_pba);
1283			sc->rl_res_pba = NULL;
1284			msixc = 0;
1285		}
1286	}
1287	/* Prefer MSI to INTx. */
1288	if (msixc == 0 && msic > 0) {
1289		msic = 1;
1290		if (pci_alloc_msi(dev, &msic) == 0) {
1291			if (msic == RL_MSI_MESSAGES) {
1292				device_printf(dev, "Using %d MSI message\n",
1293				    msic);
1294				sc->rl_flags |= RL_FLAG_MSI;
1295				/* Explicitly set MSI enable bit. */
1296				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1297				cfg = CSR_READ_1(sc, RL_CFG2);
1298				cfg |= RL_CFG2_MSI;
1299				CSR_WRITE_1(sc, RL_CFG2, cfg);
1300				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1301			} else
1302				pci_release_msi(dev);
1303		}
1304		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
1305			msic = 0;
1306	}
1307
1308	/* Allocate interrupt */
1309	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
1310		rid = 0;
1311		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1312		    RF_SHAREABLE | RF_ACTIVE);
1313		if (sc->rl_irq[0] == NULL) {
1314			device_printf(dev, "couldn't allocate IRQ resources\n");
1315			error = ENXIO;
1316			goto fail;
1317		}
1318	} else {
1319		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
1320			sc->rl_irq[i] = bus_alloc_resource_any(dev,
1321			    SYS_RES_IRQ, &rid, RF_ACTIVE);
1322			if (sc->rl_irq[i] == NULL) {
1323				device_printf(dev,
1324				    "couldn't llocate IRQ resources for "
1325				    "message %d\n", rid);
1326				error = ENXIO;
1327				goto fail;
1328			}
1329		}
1330	}
1331
1332	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
1333		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1334		cfg = CSR_READ_1(sc, RL_CFG2);
1335		if ((cfg & RL_CFG2_MSI) != 0) {
1336			device_printf(dev, "turning off MSI enable bit.\n");
1337			cfg &= ~RL_CFG2_MSI;
1338			CSR_WRITE_1(sc, RL_CFG2, cfg);
1339		}
1340		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1341	}
1342
1343	/* Disable ASPM L0S/L1. */
1344	if (sc->rl_expcap != 0) {
1345		cap = pci_read_config(dev, sc->rl_expcap +
1346		    PCIER_LINK_CAP, 2);
1347		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1348			ctl = pci_read_config(dev, sc->rl_expcap +
1349			    PCIER_LINK_CTL, 2);
1350			if ((ctl & 0x0003) != 0) {
1351				ctl &= ~0x0003;
1352				pci_write_config(dev, sc->rl_expcap +
1353				    PCIER_LINK_CTL, ctl, 2);
1354				device_printf(dev, "ASPM disabled\n");
1355			}
1356		} else
1357			device_printf(dev, "no ASPM capability\n");
1358	}
1359
1360	hw_rev = re_hwrevs;
1361	hwrev = CSR_READ_4(sc, RL_TXCFG);
1362	switch (hwrev & 0x70000000) {
1363	case 0x00000000:
1364	case 0x10000000:
1365		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1366		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1367		break;
1368	default:
1369		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1370		hwrev &= RL_TXCFG_HWREV;
1371		break;
1372	}
1373	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1374	while (hw_rev->rl_desc != NULL) {
1375		if (hw_rev->rl_rev == hwrev) {
1376			sc->rl_type = hw_rev->rl_type;
1377			sc->rl_hwrev = hw_rev;
1378			break;
1379		}
1380		hw_rev++;
1381	}
1382	if (hw_rev->rl_desc == NULL) {
1383		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1384		error = ENXIO;
1385		goto fail;
1386	}
1387
1388	switch (hw_rev->rl_rev) {
1389	case RL_HWREV_8139CPLUS:
1390		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1391		break;
1392	case RL_HWREV_8100E:
1393	case RL_HWREV_8101E:
1394		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1395		break;
1396	case RL_HWREV_8102E:
1397	case RL_HWREV_8102EL:
1398	case RL_HWREV_8102EL_SPIN1:
1399		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1400		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1401		    RL_FLAG_AUTOPAD;
1402		break;
1403	case RL_HWREV_8103E:
1404		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1405		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1406		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
1407		break;
1408	case RL_HWREV_8401E:
1409	case RL_HWREV_8105E:
1410	case RL_HWREV_8105E_SPIN1:
1411		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1412		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1413		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1414		break;
1415	case RL_HWREV_8402:
1416		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1417		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1418		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1419		    RL_FLAG_CMDSTOP_WAIT_TXQ;
1420		break;
1421	case RL_HWREV_8168B_SPIN1:
1422	case RL_HWREV_8168B_SPIN2:
1423		sc->rl_flags |= RL_FLAG_WOLRXENB;
1424		/* FALLTHROUGH */
1425	case RL_HWREV_8168B_SPIN3:
1426		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1427		break;
1428	case RL_HWREV_8168C_SPIN2:
1429		sc->rl_flags |= RL_FLAG_MACSLEEP;
1430		/* FALLTHROUGH */
1431	case RL_HWREV_8168C:
1432		if ((hwrev & 0x00700000) == 0x00200000)
1433			sc->rl_flags |= RL_FLAG_MACSLEEP;
1434		/* FALLTHROUGH */
1435	case RL_HWREV_8168CP:
1436		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1437		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1438		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1439		break;
1440	case RL_HWREV_8168D:
1441		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1442		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1443		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1444		    RL_FLAG_WOL_MANLINK;
1445		break;
1446	case RL_HWREV_8168DP:
1447		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1448		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
1449		    RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1450		break;
1451	case RL_HWREV_8168E:
1452		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1453		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1454		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1455		    RL_FLAG_WOL_MANLINK;
1456		break;
1457	case RL_HWREV_8168E_VL:
1458	case RL_HWREV_8168F:
1459	case RL_HWREV_8411:
1460		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1461		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1462		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1463		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1464		break;
1465	case RL_HWREV_8169_8110SB:
1466	case RL_HWREV_8169_8110SBL:
1467	case RL_HWREV_8169_8110SC:
1468	case RL_HWREV_8169_8110SCE:
1469		sc->rl_flags |= RL_FLAG_PHYWAKE;
1470		/* FALLTHROUGH */
1471	case RL_HWREV_8169:
1472	case RL_HWREV_8169S:
1473	case RL_HWREV_8110S:
1474		sc->rl_flags |= RL_FLAG_MACRESET;
1475		break;
1476	default:
1477		break;
1478	}
1479
1480	if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1481		sc->rl_cfg0 = RL_8139_CFG0;
1482		sc->rl_cfg1 = RL_8139_CFG1;
1483		sc->rl_cfg2 = 0;
1484		sc->rl_cfg3 = RL_8139_CFG3;
1485		sc->rl_cfg4 = RL_8139_CFG4;
1486		sc->rl_cfg5 = RL_8139_CFG5;
1487	} else {
1488		sc->rl_cfg0 = RL_CFG0;
1489		sc->rl_cfg1 = RL_CFG1;
1490		sc->rl_cfg2 = RL_CFG2;
1491		sc->rl_cfg3 = RL_CFG3;
1492		sc->rl_cfg4 = RL_CFG4;
1493		sc->rl_cfg5 = RL_CFG5;
1494	}
1495
1496	/* Reset the adapter. */
1497	RL_LOCK(sc);
1498	re_reset(sc);
1499	RL_UNLOCK(sc);
1500
1501	/* Enable PME. */
1502	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1503	cfg = CSR_READ_1(sc, sc->rl_cfg1);
1504	cfg |= RL_CFG1_PME;
1505	CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1506	cfg = CSR_READ_1(sc, sc->rl_cfg5);
1507	cfg &= RL_CFG5_PME_STS;
1508	CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1509	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1510
1511	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1512		/*
1513		 * XXX Should have a better way to extract station
1514		 * address from EEPROM.
1515		 */
1516		for (i = 0; i < ETHER_ADDR_LEN; i++)
1517			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1518	} else {
1519		sc->rl_eewidth = RL_9356_ADDR_LEN;
1520		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1521		if (re_did != 0x8129)
1522			sc->rl_eewidth = RL_9346_ADDR_LEN;
1523
1524		/*
1525		 * Get station address from the EEPROM.
1526		 */
1527		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1528		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1529			as[i] = le16toh(as[i]);
1530		bcopy(as, eaddr, ETHER_ADDR_LEN);
1531	}
1532
1533	if (sc->rl_type == RL_8169) {
1534		/* Set RX length mask and number of descriptors. */
1535		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1536		sc->rl_txstart = RL_GTXSTART;
1537		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1538		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1539	} else {
1540		/* Set RX length mask and number of descriptors. */
1541		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1542		sc->rl_txstart = RL_TXSTART;
1543		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1544		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1545	}
1546
1547	error = re_allocmem(dev, sc);
1548	if (error)
1549		goto fail;
1550	re_add_sysctls(sc);
1551
1552	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1553	if (ifp == NULL) {
1554		device_printf(dev, "can not if_alloc()\n");
1555		error = ENOSPC;
1556		goto fail;
1557	}
1558
1559	/* Take controller out of deep sleep mode. */
1560	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
1561		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
1562			CSR_WRITE_1(sc, RL_GPIO,
1563			    CSR_READ_1(sc, RL_GPIO) | 0x01);
1564		else
1565			CSR_WRITE_1(sc, RL_GPIO,
1566			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
1567	}
1568
1569	/* Take PHY out of power down mode. */
1570	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1571		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1572		if (hw_rev->rl_rev == RL_HWREV_8401E)
1573			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
1574	}
1575	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1576		re_gmii_writereg(dev, 1, 0x1f, 0);
1577		re_gmii_writereg(dev, 1, 0x0e, 0);
1578	}
1579
1580	ifp->if_softc = sc;
1581	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1582	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1583	ifp->if_ioctl = re_ioctl;
1584	ifp->if_start = re_start;
1585	/*
1586	 * RTL8168/8111C generates wrong IP checksummed frame if the
1587	 * packet has IP options so disable TX IP checksum offloading.
1588	 */
1589	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
1590	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2)
1591		ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1592	else
1593		ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1594	ifp->if_hwassist |= CSUM_TSO;
1595	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1596	ifp->if_capenable = ifp->if_capabilities;
1597	ifp->if_init = re_init;
1598	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
1599	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
1600	IFQ_SET_READY(&ifp->if_snd);
1601
1602	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1603
1604#define	RE_PHYAD_INTERNAL	 0
1605
1606	/* Do MII setup. */
1607	phy = RE_PHYAD_INTERNAL;
1608	if (sc->rl_type == RL_8169)
1609		phy = 1;
1610	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
1611	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1612	if (error != 0) {
1613		device_printf(dev, "attaching PHYs failed\n");
1614		goto fail;
1615	}
1616
1617	/*
1618	 * Call MI attach routine.
1619	 */
1620	ether_ifattach(ifp, eaddr);
1621
1622	/* VLAN capability setup */
1623	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1624	if (ifp->if_capabilities & IFCAP_HWCSUM)
1625		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
1626	/* Enable WOL if PM is supported. */
1627	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &reg) == 0)
1628		ifp->if_capabilities |= IFCAP_WOL;
1629	ifp->if_capenable = ifp->if_capabilities;
1630	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1631	/*
1632	 * Don't enable TSO by default.  It is known to generate
1633	 * corrupted TCP segments(bad TCP options) under certain
1634	 * circumtances.
1635	 */
1636	ifp->if_hwassist &= ~CSUM_TSO;
1637	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1638#ifdef DEVICE_POLLING
1639	ifp->if_capabilities |= IFCAP_POLLING;
1640#endif
1641	/*
1642	 * Tell the upper layer(s) we support long frames.
1643	 * Must appear after the call to ether_ifattach() because
1644	 * ether_ifattach() sets ifi_hdrlen to the default value.
1645	 */
1646	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1647
1648#ifdef DEV_NETMAP
1649	re_netmap_attach(sc);
1650#endif /* DEV_NETMAP */
1651#ifdef RE_DIAG
1652	/*
1653	 * Perform hardware diagnostic on the original RTL8169.
1654	 * Some 32-bit cards were incorrectly wired and would
1655	 * malfunction if plugged into a 64-bit slot.
1656	 */
1657
1658	if (hwrev == RL_HWREV_8169) {
1659		error = re_diag(sc);
1660		if (error) {
1661			device_printf(dev,
1662		    	"attach aborted due to hardware diag failure\n");
1663			ether_ifdetach(ifp);
1664			goto fail;
1665		}
1666	}
1667#endif
1668
1669#ifdef RE_TX_MODERATION
1670	intr_filter = 1;
1671#endif
1672	/* Hook interrupt last to avoid having to lock softc */
1673	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1674	    intr_filter == 0) {
1675		error = bus_setup_intr(dev, sc->rl_irq[0],
1676		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1677		    &sc->rl_intrhand[0]);
1678	} else {
1679		error = bus_setup_intr(dev, sc->rl_irq[0],
1680		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
1681		    &sc->rl_intrhand[0]);
1682	}
1683	if (error) {
1684		device_printf(dev, "couldn't set up irq\n");
1685		ether_ifdetach(ifp);
1686	}
1687
1688fail:
1689
1690	if (error)
1691		re_detach(dev);
1692
1693	return (error);
1694}
1695
1696/*
1697 * Shutdown hardware and free up resources. This can be called any
1698 * time after the mutex has been initialized. It is called in both
1699 * the error case in attach and the normal detach case so it needs
1700 * to be careful about only freeing resources that have actually been
1701 * allocated.
1702 */
1703static int
1704re_detach(device_t dev)
1705{
1706	struct rl_softc		*sc;
1707	struct ifnet		*ifp;
1708	int			i, rid;
1709
1710	sc = device_get_softc(dev);
1711	ifp = sc->rl_ifp;
1712	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
1713
1714	/* These should only be active if attach succeeded */
1715	if (device_is_attached(dev)) {
1716#ifdef DEVICE_POLLING
1717		if (ifp->if_capenable & IFCAP_POLLING)
1718			ether_poll_deregister(ifp);
1719#endif
1720		RL_LOCK(sc);
1721#if 0
1722		sc->suspended = 1;
1723#endif
1724		re_stop(sc);
1725		RL_UNLOCK(sc);
1726		callout_drain(&sc->rl_stat_callout);
1727		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1728		/*
1729		 * Force off the IFF_UP flag here, in case someone
1730		 * still had a BPF descriptor attached to this
1731		 * interface. If they do, ether_ifdetach() will cause
1732		 * the BPF code to try and clear the promisc mode
1733		 * flag, which will bubble down to re_ioctl(),
1734		 * which will try to call re_init() again. This will
1735		 * turn the NIC back on and restart the MII ticker,
1736		 * which will panic the system when the kernel tries
1737		 * to invoke the re_tick() function that isn't there
1738		 * anymore.
1739		 */
1740		ifp->if_flags &= ~IFF_UP;
1741		ether_ifdetach(ifp);
1742	}
1743	if (sc->rl_miibus)
1744		device_delete_child(dev, sc->rl_miibus);
1745	bus_generic_detach(dev);
1746
1747	/*
1748	 * The rest is resource deallocation, so we should already be
1749	 * stopped here.
1750	 */
1751
1752	if (sc->rl_intrhand[0] != NULL) {
1753		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1754		sc->rl_intrhand[0] = NULL;
1755	}
1756	if (ifp != NULL) {
1757#ifdef DEV_NETMAP
1758		netmap_detach(ifp);
1759#endif /* DEV_NETMAP */
1760		if_free(ifp);
1761	}
1762	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1763		rid = 0;
1764	else
1765		rid = 1;
1766	if (sc->rl_irq[0] != NULL) {
1767		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
1768		sc->rl_irq[0] = NULL;
1769	}
1770	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
1771		pci_release_msi(dev);
1772	if (sc->rl_res_pba) {
1773		rid = PCIR_BAR(4);
1774		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
1775	}
1776	if (sc->rl_res)
1777		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1778		    sc->rl_res);
1779
1780	/* Unload and free the RX DMA ring memory and map */
1781
1782	if (sc->rl_ldata.rl_rx_list_tag) {
1783		if (sc->rl_ldata.rl_rx_list_map)
1784			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1785			    sc->rl_ldata.rl_rx_list_map);
1786		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1787			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1788			    sc->rl_ldata.rl_rx_list,
1789			    sc->rl_ldata.rl_rx_list_map);
1790		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1791	}
1792
1793	/* Unload and free the TX DMA ring memory and map */
1794
1795	if (sc->rl_ldata.rl_tx_list_tag) {
1796		if (sc->rl_ldata.rl_tx_list_map)
1797			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1798			    sc->rl_ldata.rl_tx_list_map);
1799		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1800			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1801			    sc->rl_ldata.rl_tx_list,
1802			    sc->rl_ldata.rl_tx_list_map);
1803		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1804	}
1805
1806	/* Destroy all the RX and TX buffer maps */
1807
1808	if (sc->rl_ldata.rl_tx_mtag) {
1809		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1810			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1811				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1812				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1813		}
1814		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1815	}
1816	if (sc->rl_ldata.rl_rx_mtag) {
1817		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1818			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1819				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1820				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1821		}
1822		if (sc->rl_ldata.rl_rx_sparemap)
1823			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1824			    sc->rl_ldata.rl_rx_sparemap);
1825		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1826	}
1827	if (sc->rl_ldata.rl_jrx_mtag) {
1828		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1829			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
1830				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1831				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1832		}
1833		if (sc->rl_ldata.rl_jrx_sparemap)
1834			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1835			    sc->rl_ldata.rl_jrx_sparemap);
1836		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
1837	}
1838	/* Unload and free the stats buffer and map */
1839
1840	if (sc->rl_ldata.rl_stag) {
1841		if (sc->rl_ldata.rl_smap)
1842			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1843			    sc->rl_ldata.rl_smap);
1844		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
1845			bus_dmamem_free(sc->rl_ldata.rl_stag,
1846			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1847		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1848	}
1849
1850	if (sc->rl_parent_tag)
1851		bus_dma_tag_destroy(sc->rl_parent_tag);
1852
1853	mtx_destroy(&sc->rl_mtx);
1854
1855	return (0);
1856}
1857
1858static __inline void
1859re_discard_rxbuf(struct rl_softc *sc, int idx)
1860{
1861	struct rl_desc		*desc;
1862	struct rl_rxdesc	*rxd;
1863	uint32_t		cmdstat;
1864
1865	if (sc->rl_ifp->if_mtu > RL_MTU &&
1866	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
1867		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
1868	else
1869		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1870	desc = &sc->rl_ldata.rl_rx_list[idx];
1871	desc->rl_vlanctl = 0;
1872	cmdstat = rxd->rx_size;
1873	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1874		cmdstat |= RL_RDESC_CMD_EOR;
1875	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1876}
1877
1878static int
1879re_newbuf(struct rl_softc *sc, int idx)
1880{
1881	struct mbuf		*m;
1882	struct rl_rxdesc	*rxd;
1883	bus_dma_segment_t	segs[1];
1884	bus_dmamap_t		map;
1885	struct rl_desc		*desc;
1886	uint32_t		cmdstat;
1887	int			error, nsegs;
1888
1889	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1890	if (m == NULL)
1891		return (ENOBUFS);
1892
1893	m->m_len = m->m_pkthdr.len = MCLBYTES;
1894#ifdef RE_FIXUP_RX
1895	/*
1896	 * This is part of an evil trick to deal with non-x86 platforms.
1897	 * The RealTek chip requires RX buffers to be aligned on 64-bit
1898	 * boundaries, but that will hose non-x86 machines. To get around
1899	 * this, we leave some empty space at the start of each buffer
1900	 * and for non-x86 hosts, we copy the buffer back six bytes
1901	 * to achieve word alignment. This is slightly more efficient
1902	 * than allocating a new buffer, copying the contents, and
1903	 * discarding the old buffer.
1904	 */
1905	m_adj(m, RE_ETHER_ALIGN);
1906#endif
1907	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1908	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1909	if (error != 0) {
1910		m_freem(m);
1911		return (ENOBUFS);
1912	}
1913	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1914
1915	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1916	if (rxd->rx_m != NULL) {
1917		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1918		    BUS_DMASYNC_POSTREAD);
1919		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1920	}
1921
1922	rxd->rx_m = m;
1923	map = rxd->rx_dmamap;
1924	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1925	rxd->rx_size = segs[0].ds_len;
1926	sc->rl_ldata.rl_rx_sparemap = map;
1927	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1928	    BUS_DMASYNC_PREREAD);
1929
1930	desc = &sc->rl_ldata.rl_rx_list[idx];
1931	desc->rl_vlanctl = 0;
1932	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1933	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1934	cmdstat = segs[0].ds_len;
1935	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1936		cmdstat |= RL_RDESC_CMD_EOR;
1937	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1938
1939	return (0);
1940}
1941
1942static int
1943re_jumbo_newbuf(struct rl_softc *sc, int idx)
1944{
1945	struct mbuf		*m;
1946	struct rl_rxdesc	*rxd;
1947	bus_dma_segment_t	segs[1];
1948	bus_dmamap_t		map;
1949	struct rl_desc		*desc;
1950	uint32_t		cmdstat;
1951	int			error, nsegs;
1952
1953	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1954	if (m == NULL)
1955		return (ENOBUFS);
1956	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1957#ifdef RE_FIXUP_RX
1958	m_adj(m, RE_ETHER_ALIGN);
1959#endif
1960	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
1961	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1962	if (error != 0) {
1963		m_freem(m);
1964		return (ENOBUFS);
1965	}
1966	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1967
1968	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
1969	if (rxd->rx_m != NULL) {
1970		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
1971		    BUS_DMASYNC_POSTREAD);
1972		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
1973	}
1974
1975	rxd->rx_m = m;
1976	map = rxd->rx_dmamap;
1977	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
1978	rxd->rx_size = segs[0].ds_len;
1979	sc->rl_ldata.rl_jrx_sparemap = map;
1980	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
1981	    BUS_DMASYNC_PREREAD);
1982
1983	desc = &sc->rl_ldata.rl_rx_list[idx];
1984	desc->rl_vlanctl = 0;
1985	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1986	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1987	cmdstat = segs[0].ds_len;
1988	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1989		cmdstat |= RL_RDESC_CMD_EOR;
1990	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1991
1992	return (0);
1993}
1994
1995#ifdef RE_FIXUP_RX
1996static __inline void
1997re_fixup_rx(struct mbuf *m)
1998{
1999	int                     i;
2000	uint16_t                *src, *dst;
2001
2002	src = mtod(m, uint16_t *);
2003	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
2004
2005	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2006		*dst++ = *src++;
2007
2008	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
2009}
2010#endif
2011
2012static int
2013re_tx_list_init(struct rl_softc *sc)
2014{
2015	struct rl_desc		*desc;
2016	int			i;
2017
2018	RL_LOCK_ASSERT(sc);
2019
2020	bzero(sc->rl_ldata.rl_tx_list,
2021	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2022	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2023		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2024#ifdef DEV_NETMAP
2025	re_netmap_tx_init(sc);
2026#endif /* DEV_NETMAP */
2027	/* Set EOR. */
2028	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2029	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2030
2031	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2032	    sc->rl_ldata.rl_tx_list_map,
2033	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2034
2035	sc->rl_ldata.rl_tx_prodidx = 0;
2036	sc->rl_ldata.rl_tx_considx = 0;
2037	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2038
2039	return (0);
2040}
2041
2042static int
2043re_rx_list_init(struct rl_softc *sc)
2044{
2045	int			error, i;
2046
2047	bzero(sc->rl_ldata.rl_rx_list,
2048	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2049	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2050		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2051		if ((error = re_newbuf(sc, i)) != 0)
2052			return (error);
2053	}
2054#ifdef DEV_NETMAP
2055	re_netmap_rx_init(sc);
2056#endif /* DEV_NETMAP */
2057
2058	/* Flush the RX descriptors */
2059
2060	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2061	    sc->rl_ldata.rl_rx_list_map,
2062	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2063
2064	sc->rl_ldata.rl_rx_prodidx = 0;
2065	sc->rl_head = sc->rl_tail = NULL;
2066	sc->rl_int_rx_act = 0;
2067
2068	return (0);
2069}
2070
2071static int
2072re_jrx_list_init(struct rl_softc *sc)
2073{
2074	int			error, i;
2075
2076	bzero(sc->rl_ldata.rl_rx_list,
2077	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2078	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2079		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
2080		if ((error = re_jumbo_newbuf(sc, i)) != 0)
2081			return (error);
2082	}
2083
2084	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2085	    sc->rl_ldata.rl_rx_list_map,
2086	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2087
2088	sc->rl_ldata.rl_rx_prodidx = 0;
2089	sc->rl_head = sc->rl_tail = NULL;
2090	sc->rl_int_rx_act = 0;
2091
2092	return (0);
2093}
2094
2095/*
2096 * RX handler for C+ and 8169. For the gigE chips, we support
2097 * the reception of jumbo frames that have been fragmented
2098 * across multiple 2K mbuf cluster buffers.
2099 */
2100static int
2101re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2102{
2103	struct mbuf		*m;
2104	struct ifnet		*ifp;
2105	int			i, rxerr, total_len;
2106	struct rl_desc		*cur_rx;
2107	u_int32_t		rxstat, rxvlan;
2108	int			jumbo, maxpkt = 16, rx_npkts = 0;
2109
2110	RL_LOCK_ASSERT(sc);
2111
2112	ifp = sc->rl_ifp;
2113#ifdef DEV_NETMAP
2114	if (netmap_rx_irq(ifp, 0 | (NETMAP_LOCKED_ENTER|NETMAP_LOCKED_EXIT),
2115	    &rx_npkts))
2116		return 0;
2117#endif /* DEV_NETMAP */
2118	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
2119		jumbo = 1;
2120	else
2121		jumbo = 0;
2122
2123	/* Invalidate the descriptor memory */
2124
2125	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2126	    sc->rl_ldata.rl_rx_list_map,
2127	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2128
2129	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2130	    i = RL_RX_DESC_NXT(sc, i)) {
2131		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2132			break;
2133		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2134		rxstat = le32toh(cur_rx->rl_cmdstat);
2135		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2136			break;
2137		total_len = rxstat & sc->rl_rxlenmask;
2138		rxvlan = le32toh(cur_rx->rl_vlanctl);
2139		if (jumbo != 0)
2140			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
2141		else
2142			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2143
2144		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
2145		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
2146		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
2147			/*
2148			 * RTL8168C or later controllers do not
2149			 * support multi-fragment packet.
2150			 */
2151			re_discard_rxbuf(sc, i);
2152			continue;
2153		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2154			if (re_newbuf(sc, i) != 0) {
2155				/*
2156				 * If this is part of a multi-fragment packet,
2157				 * discard all the pieces.
2158				 */
2159				if (sc->rl_head != NULL) {
2160					m_freem(sc->rl_head);
2161					sc->rl_head = sc->rl_tail = NULL;
2162				}
2163				re_discard_rxbuf(sc, i);
2164				continue;
2165			}
2166			m->m_len = RE_RX_DESC_BUFLEN;
2167			if (sc->rl_head == NULL)
2168				sc->rl_head = sc->rl_tail = m;
2169			else {
2170				m->m_flags &= ~M_PKTHDR;
2171				sc->rl_tail->m_next = m;
2172				sc->rl_tail = m;
2173			}
2174			continue;
2175		}
2176
2177		/*
2178		 * NOTE: for the 8139C+, the frame length field
2179		 * is always 12 bits in size, but for the gigE chips,
2180		 * it is 13 bits (since the max RX frame length is 16K).
2181		 * Unfortunately, all 32 bits in the status word
2182		 * were already used, so to make room for the extra
2183		 * length bit, RealTek took out the 'frame alignment
2184		 * error' bit and shifted the other status bits
2185		 * over one slot. The OWN, EOR, FS and LS bits are
2186		 * still in the same places. We have already extracted
2187		 * the frame length and checked the OWN bit, so rather
2188		 * than using an alternate bit mapping, we shift the
2189		 * status bits one space to the right so we can evaluate
2190		 * them using the 8169 status as though it was in the
2191		 * same format as that of the 8139C+.
2192		 */
2193		if (sc->rl_type == RL_8169)
2194			rxstat >>= 1;
2195
2196		/*
2197		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
2198		 * set, but if CRC is clear, it will still be a valid frame.
2199		 */
2200		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
2201			rxerr = 1;
2202			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
2203			    total_len > 8191 &&
2204			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
2205				rxerr = 0;
2206			if (rxerr != 0) {
2207				ifp->if_ierrors++;
2208				/*
2209				 * If this is part of a multi-fragment packet,
2210				 * discard all the pieces.
2211				 */
2212				if (sc->rl_head != NULL) {
2213					m_freem(sc->rl_head);
2214					sc->rl_head = sc->rl_tail = NULL;
2215				}
2216				re_discard_rxbuf(sc, i);
2217				continue;
2218			}
2219		}
2220
2221		/*
2222		 * If allocating a replacement mbuf fails,
2223		 * reload the current one.
2224		 */
2225		if (jumbo != 0)
2226			rxerr = re_jumbo_newbuf(sc, i);
2227		else
2228			rxerr = re_newbuf(sc, i);
2229		if (rxerr != 0) {
2230			ifp->if_iqdrops++;
2231			if (sc->rl_head != NULL) {
2232				m_freem(sc->rl_head);
2233				sc->rl_head = sc->rl_tail = NULL;
2234			}
2235			re_discard_rxbuf(sc, i);
2236			continue;
2237		}
2238
2239		if (sc->rl_head != NULL) {
2240			if (jumbo != 0)
2241				m->m_len = total_len;
2242			else {
2243				m->m_len = total_len % RE_RX_DESC_BUFLEN;
2244				if (m->m_len == 0)
2245					m->m_len = RE_RX_DESC_BUFLEN;
2246			}
2247			/*
2248			 * Special case: if there's 4 bytes or less
2249			 * in this buffer, the mbuf can be discarded:
2250			 * the last 4 bytes is the CRC, which we don't
2251			 * care about anyway.
2252			 */
2253			if (m->m_len <= ETHER_CRC_LEN) {
2254				sc->rl_tail->m_len -=
2255				    (ETHER_CRC_LEN - m->m_len);
2256				m_freem(m);
2257			} else {
2258				m->m_len -= ETHER_CRC_LEN;
2259				m->m_flags &= ~M_PKTHDR;
2260				sc->rl_tail->m_next = m;
2261			}
2262			m = sc->rl_head;
2263			sc->rl_head = sc->rl_tail = NULL;
2264			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2265		} else
2266			m->m_pkthdr.len = m->m_len =
2267			    (total_len - ETHER_CRC_LEN);
2268
2269#ifdef RE_FIXUP_RX
2270		re_fixup_rx(m);
2271#endif
2272		ifp->if_ipackets++;
2273		m->m_pkthdr.rcvif = ifp;
2274
2275		/* Do RX checksumming if enabled */
2276
2277		if (ifp->if_capenable & IFCAP_RXCSUM) {
2278			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2279				/* Check IP header checksum */
2280				if (rxstat & RL_RDESC_STAT_PROTOID)
2281					m->m_pkthdr.csum_flags |=
2282					    CSUM_IP_CHECKED;
2283				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2284					m->m_pkthdr.csum_flags |=
2285					    CSUM_IP_VALID;
2286
2287				/* Check TCP/UDP checksum */
2288				if ((RL_TCPPKT(rxstat) &&
2289				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2290				    (RL_UDPPKT(rxstat) &&
2291				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2292					m->m_pkthdr.csum_flags |=
2293						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2294					m->m_pkthdr.csum_data = 0xffff;
2295				}
2296			} else {
2297				/*
2298				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2299				 */
2300				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2301				    (rxvlan & RL_RDESC_IPV4))
2302					m->m_pkthdr.csum_flags |=
2303					    CSUM_IP_CHECKED;
2304				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2305				    (rxvlan & RL_RDESC_IPV4))
2306					m->m_pkthdr.csum_flags |=
2307					    CSUM_IP_VALID;
2308				if (((rxstat & RL_RDESC_STAT_TCP) &&
2309				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2310				    ((rxstat & RL_RDESC_STAT_UDP) &&
2311				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2312					m->m_pkthdr.csum_flags |=
2313						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2314					m->m_pkthdr.csum_data = 0xffff;
2315				}
2316			}
2317		}
2318		maxpkt--;
2319		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
2320			m->m_pkthdr.ether_vtag =
2321			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
2322			m->m_flags |= M_VLANTAG;
2323		}
2324		RL_UNLOCK(sc);
2325		(*ifp->if_input)(ifp, m);
2326		RL_LOCK(sc);
2327		rx_npkts++;
2328	}
2329
2330	/* Flush the RX DMA ring */
2331
2332	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2333	    sc->rl_ldata.rl_rx_list_map,
2334	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2335
2336	sc->rl_ldata.rl_rx_prodidx = i;
2337
2338	if (rx_npktsp != NULL)
2339		*rx_npktsp = rx_npkts;
2340	if (maxpkt)
2341		return (EAGAIN);
2342
2343	return (0);
2344}
2345
2346static void
2347re_txeof(struct rl_softc *sc)
2348{
2349	struct ifnet		*ifp;
2350	struct rl_txdesc	*txd;
2351	u_int32_t		txstat;
2352	int			cons;
2353
2354	cons = sc->rl_ldata.rl_tx_considx;
2355	if (cons == sc->rl_ldata.rl_tx_prodidx)
2356		return;
2357
2358	ifp = sc->rl_ifp;
2359#ifdef DEV_NETMAP
2360	if (netmap_tx_irq(ifp, 0 | (NETMAP_LOCKED_ENTER|NETMAP_LOCKED_EXIT)))
2361		return;
2362#endif /* DEV_NETMAP */
2363	/* Invalidate the TX descriptor list */
2364	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2365	    sc->rl_ldata.rl_tx_list_map,
2366	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2367
2368	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2369	    cons = RL_TX_DESC_NXT(sc, cons)) {
2370		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2371		if (txstat & RL_TDESC_STAT_OWN)
2372			break;
2373		/*
2374		 * We only stash mbufs in the last descriptor
2375		 * in a fragment chain, which also happens to
2376		 * be the only place where the TX status bits
2377		 * are valid.
2378		 */
2379		if (txstat & RL_TDESC_CMD_EOF) {
2380			txd = &sc->rl_ldata.rl_tx_desc[cons];
2381			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2382			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2383			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2384			    txd->tx_dmamap);
2385			KASSERT(txd->tx_m != NULL,
2386			    ("%s: freeing NULL mbufs!", __func__));
2387			m_freem(txd->tx_m);
2388			txd->tx_m = NULL;
2389			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2390			    RL_TDESC_STAT_COLCNT))
2391				ifp->if_collisions++;
2392			if (txstat & RL_TDESC_STAT_TXERRSUM)
2393				ifp->if_oerrors++;
2394			else
2395				ifp->if_opackets++;
2396		}
2397		sc->rl_ldata.rl_tx_free++;
2398		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2399	}
2400	sc->rl_ldata.rl_tx_considx = cons;
2401
2402	/* No changes made to the TX ring, so no flush needed */
2403
2404	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2405#ifdef RE_TX_MODERATION
2406		/*
2407		 * If not all descriptors have been reaped yet, reload
2408		 * the timer so that we will eventually get another
2409		 * interrupt that will cause us to re-enter this routine.
2410		 * This is done in case the transmitter has gone idle.
2411		 */
2412		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2413#endif
2414	} else
2415		sc->rl_watchdog_timer = 0;
2416}
2417
2418static void
2419re_tick(void *xsc)
2420{
2421	struct rl_softc		*sc;
2422	struct mii_data		*mii;
2423
2424	sc = xsc;
2425
2426	RL_LOCK_ASSERT(sc);
2427
2428	mii = device_get_softc(sc->rl_miibus);
2429	mii_tick(mii);
2430	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
2431		re_miibus_statchg(sc->rl_dev);
2432	/*
2433	 * Reclaim transmitted frames here. Technically it is not
2434	 * necessary to do here but it ensures periodic reclamation
2435	 * regardless of Tx completion interrupt which seems to be
2436	 * lost on PCIe based controllers under certain situations.
2437	 */
2438	re_txeof(sc);
2439	re_watchdog(sc);
2440	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2441}
2442
2443#ifdef DEVICE_POLLING
2444static int
2445re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2446{
2447	struct rl_softc *sc = ifp->if_softc;
2448	int rx_npkts = 0;
2449
2450	RL_LOCK(sc);
2451	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2452		rx_npkts = re_poll_locked(ifp, cmd, count);
2453	RL_UNLOCK(sc);
2454	return (rx_npkts);
2455}
2456
2457static int
2458re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
2459{
2460	struct rl_softc *sc = ifp->if_softc;
2461	int rx_npkts;
2462
2463	RL_LOCK_ASSERT(sc);
2464
2465	sc->rxcycles = count;
2466	re_rxeof(sc, &rx_npkts);
2467	re_txeof(sc);
2468
2469	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2470		re_start_locked(ifp);
2471
2472	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2473		u_int16_t       status;
2474
2475		status = CSR_READ_2(sc, RL_ISR);
2476		if (status == 0xffff)
2477			return (rx_npkts);
2478		if (status)
2479			CSR_WRITE_2(sc, RL_ISR, status);
2480		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2481		    (sc->rl_flags & RL_FLAG_PCIE))
2482			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2483
2484		/*
2485		 * XXX check behaviour on receiver stalls.
2486		 */
2487
2488		if (status & RL_ISR_SYSTEM_ERR) {
2489			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2490			re_init_locked(sc);
2491		}
2492	}
2493	return (rx_npkts);
2494}
2495#endif /* DEVICE_POLLING */
2496
2497static int
2498re_intr(void *arg)
2499{
2500	struct rl_softc		*sc;
2501	uint16_t		status;
2502
2503	sc = arg;
2504
2505	status = CSR_READ_2(sc, RL_ISR);
2506	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2507                return (FILTER_STRAY);
2508	CSR_WRITE_2(sc, RL_IMR, 0);
2509
2510	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2511
2512	return (FILTER_HANDLED);
2513}
2514
2515static void
2516re_int_task(void *arg, int npending)
2517{
2518	struct rl_softc		*sc;
2519	struct ifnet		*ifp;
2520	u_int16_t		status;
2521	int			rval = 0;
2522
2523	sc = arg;
2524	ifp = sc->rl_ifp;
2525
2526	RL_LOCK(sc);
2527
2528	status = CSR_READ_2(sc, RL_ISR);
2529        CSR_WRITE_2(sc, RL_ISR, status);
2530
2531	if (sc->suspended ||
2532	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2533		RL_UNLOCK(sc);
2534		return;
2535	}
2536
2537#ifdef DEVICE_POLLING
2538	if  (ifp->if_capenable & IFCAP_POLLING) {
2539		RL_UNLOCK(sc);
2540		return;
2541	}
2542#endif
2543
2544	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2545		rval = re_rxeof(sc, NULL);
2546
2547	/*
2548	 * Some chips will ignore a second TX request issued
2549	 * while an existing transmission is in progress. If
2550	 * the transmitter goes idle but there are still
2551	 * packets waiting to be sent, we need to restart the
2552	 * channel here to flush them out. This only seems to
2553	 * be required with the PCIe devices.
2554	 */
2555	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2556	    (sc->rl_flags & RL_FLAG_PCIE))
2557		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2558	if (status & (
2559#ifdef RE_TX_MODERATION
2560	    RL_ISR_TIMEOUT_EXPIRED|
2561#else
2562	    RL_ISR_TX_OK|
2563#endif
2564	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2565		re_txeof(sc);
2566
2567	if (status & RL_ISR_SYSTEM_ERR) {
2568		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2569		re_init_locked(sc);
2570	}
2571
2572	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2573		re_start_locked(ifp);
2574
2575	RL_UNLOCK(sc);
2576
2577        if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2578		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2579		return;
2580	}
2581
2582	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2583}
2584
2585static void
2586re_intr_msi(void *xsc)
2587{
2588	struct rl_softc		*sc;
2589	struct ifnet		*ifp;
2590	uint16_t		intrs, status;
2591
2592	sc = xsc;
2593	RL_LOCK(sc);
2594
2595	ifp = sc->rl_ifp;
2596#ifdef DEVICE_POLLING
2597	if (ifp->if_capenable & IFCAP_POLLING) {
2598		RL_UNLOCK(sc);
2599		return;
2600	}
2601#endif
2602	/* Disable interrupts. */
2603	CSR_WRITE_2(sc, RL_IMR, 0);
2604	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2605		RL_UNLOCK(sc);
2606		return;
2607	}
2608
2609	intrs = RL_INTRS_CPLUS;
2610	status = CSR_READ_2(sc, RL_ISR);
2611        CSR_WRITE_2(sc, RL_ISR, status);
2612	if (sc->rl_int_rx_act > 0) {
2613		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2614		    RL_ISR_RX_OVERRUN);
2615		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2616		    RL_ISR_RX_OVERRUN);
2617	}
2618
2619	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2620	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2621		re_rxeof(sc, NULL);
2622		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2623			if (sc->rl_int_rx_mod != 0 &&
2624			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2625			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2626				/* Rearm one-shot timer. */
2627				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2628				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2629				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2630				sc->rl_int_rx_act = 1;
2631			} else {
2632				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2633				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2634				sc->rl_int_rx_act = 0;
2635			}
2636		}
2637	}
2638
2639	/*
2640	 * Some chips will ignore a second TX request issued
2641	 * while an existing transmission is in progress. If
2642	 * the transmitter goes idle but there are still
2643	 * packets waiting to be sent, we need to restart the
2644	 * channel here to flush them out. This only seems to
2645	 * be required with the PCIe devices.
2646	 */
2647	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2648	    (sc->rl_flags & RL_FLAG_PCIE))
2649		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2650	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2651		re_txeof(sc);
2652
2653	if (status & RL_ISR_SYSTEM_ERR) {
2654		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2655		re_init_locked(sc);
2656	}
2657
2658	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2659		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2660			re_start_locked(ifp);
2661		CSR_WRITE_2(sc, RL_IMR, intrs);
2662	}
2663	RL_UNLOCK(sc);
2664}
2665
2666static int
2667re_encap(struct rl_softc *sc, struct mbuf **m_head)
2668{
2669	struct rl_txdesc	*txd, *txd_last;
2670	bus_dma_segment_t	segs[RL_NTXSEGS];
2671	bus_dmamap_t		map;
2672	struct mbuf		*m_new;
2673	struct rl_desc		*desc;
2674	int			nsegs, prod;
2675	int			i, error, ei, si;
2676	int			padlen;
2677	uint32_t		cmdstat, csum_flags, vlanctl;
2678
2679	RL_LOCK_ASSERT(sc);
2680	M_ASSERTPKTHDR((*m_head));
2681
2682	/*
2683	 * With some of the RealTek chips, using the checksum offload
2684	 * support in conjunction with the autopadding feature results
2685	 * in the transmission of corrupt frames. For example, if we
2686	 * need to send a really small IP fragment that's less than 60
2687	 * bytes in size, and IP header checksumming is enabled, the
2688	 * resulting ethernet frame that appears on the wire will
2689	 * have garbled payload. To work around this, if TX IP checksum
2690	 * offload is enabled, we always manually pad short frames out
2691	 * to the minimum ethernet frame size.
2692	 */
2693	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2694	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
2695	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2696		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2697		if (M_WRITABLE(*m_head) == 0) {
2698			/* Get a writable copy. */
2699			m_new = m_dup(*m_head, M_NOWAIT);
2700			m_freem(*m_head);
2701			if (m_new == NULL) {
2702				*m_head = NULL;
2703				return (ENOBUFS);
2704			}
2705			*m_head = m_new;
2706		}
2707		if ((*m_head)->m_next != NULL ||
2708		    M_TRAILINGSPACE(*m_head) < padlen) {
2709			m_new = m_defrag(*m_head, M_NOWAIT);
2710			if (m_new == NULL) {
2711				m_freem(*m_head);
2712				*m_head = NULL;
2713				return (ENOBUFS);
2714			}
2715		} else
2716			m_new = *m_head;
2717
2718		/*
2719		 * Manually pad short frames, and zero the pad space
2720		 * to avoid leaking data.
2721		 */
2722		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2723		m_new->m_pkthdr.len += padlen;
2724		m_new->m_len = m_new->m_pkthdr.len;
2725		*m_head = m_new;
2726	}
2727
2728	prod = sc->rl_ldata.rl_tx_prodidx;
2729	txd = &sc->rl_ldata.rl_tx_desc[prod];
2730	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2731	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2732	if (error == EFBIG) {
2733		m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2734		if (m_new == NULL) {
2735			m_freem(*m_head);
2736			*m_head = NULL;
2737			return (ENOBUFS);
2738		}
2739		*m_head = m_new;
2740		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2741		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2742		if (error != 0) {
2743			m_freem(*m_head);
2744			*m_head = NULL;
2745			return (error);
2746		}
2747	} else if (error != 0)
2748		return (error);
2749	if (nsegs == 0) {
2750		m_freem(*m_head);
2751		*m_head = NULL;
2752		return (EIO);
2753	}
2754
2755	/* Check for number of available descriptors. */
2756	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2757		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2758		return (ENOBUFS);
2759	}
2760
2761	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2762	    BUS_DMASYNC_PREWRITE);
2763
2764	/*
2765	 * Set up checksum offload. Note: checksum offload bits must
2766	 * appear in all descriptors of a multi-descriptor transmit
2767	 * attempt. This is according to testing done with an 8169
2768	 * chip. This is a requirement.
2769	 */
2770	vlanctl = 0;
2771	csum_flags = 0;
2772	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2773		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2774			csum_flags |= RL_TDESC_CMD_LGSEND;
2775			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2776			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2777		} else {
2778			csum_flags |= RL_TDESC_CMD_LGSEND |
2779			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2780			    RL_TDESC_CMD_MSSVAL_SHIFT);
2781		}
2782	} else {
2783		/*
2784		 * Unconditionally enable IP checksum if TCP or UDP
2785		 * checksum is required. Otherwise, TCP/UDP checksum
2786		 * does't make effects.
2787		 */
2788		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2789			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2790				csum_flags |= RL_TDESC_CMD_IPCSUM;
2791				if (((*m_head)->m_pkthdr.csum_flags &
2792				    CSUM_TCP) != 0)
2793					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2794				if (((*m_head)->m_pkthdr.csum_flags &
2795				    CSUM_UDP) != 0)
2796					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2797			} else {
2798				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2799				if (((*m_head)->m_pkthdr.csum_flags &
2800				    CSUM_TCP) != 0)
2801					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2802				if (((*m_head)->m_pkthdr.csum_flags &
2803				    CSUM_UDP) != 0)
2804					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2805			}
2806		}
2807	}
2808
2809	/*
2810	 * Set up hardware VLAN tagging. Note: vlan tag info must
2811	 * appear in all descriptors of a multi-descriptor
2812	 * transmission attempt.
2813	 */
2814	if ((*m_head)->m_flags & M_VLANTAG)
2815		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2816		    RL_TDESC_VLANCTL_TAG;
2817
2818	si = prod;
2819	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2820		desc = &sc->rl_ldata.rl_tx_list[prod];
2821		desc->rl_vlanctl = htole32(vlanctl);
2822		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2823		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2824		cmdstat = segs[i].ds_len;
2825		if (i != 0)
2826			cmdstat |= RL_TDESC_CMD_OWN;
2827		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2828			cmdstat |= RL_TDESC_CMD_EOR;
2829		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2830		sc->rl_ldata.rl_tx_free--;
2831	}
2832	/* Update producer index. */
2833	sc->rl_ldata.rl_tx_prodidx = prod;
2834
2835	/* Set EOF on the last descriptor. */
2836	ei = RL_TX_DESC_PRV(sc, prod);
2837	desc = &sc->rl_ldata.rl_tx_list[ei];
2838	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2839
2840	desc = &sc->rl_ldata.rl_tx_list[si];
2841	/* Set SOF and transfer ownership of packet to the chip. */
2842	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2843
2844	/*
2845	 * Insure that the map for this transmission
2846	 * is placed at the array index of the last descriptor
2847	 * in this chain.  (Swap last and first dmamaps.)
2848	 */
2849	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2850	map = txd->tx_dmamap;
2851	txd->tx_dmamap = txd_last->tx_dmamap;
2852	txd_last->tx_dmamap = map;
2853	txd_last->tx_m = *m_head;
2854
2855	return (0);
2856}
2857
2858static void
2859re_start(struct ifnet *ifp)
2860{
2861	struct rl_softc		*sc;
2862
2863	sc = ifp->if_softc;
2864	RL_LOCK(sc);
2865	re_start_locked(ifp);
2866	RL_UNLOCK(sc);
2867}
2868
2869/*
2870 * Main transmit routine for C+ and gigE NICs.
2871 */
2872static void
2873re_start_locked(struct ifnet *ifp)
2874{
2875	struct rl_softc		*sc;
2876	struct mbuf		*m_head;
2877	int			queued;
2878
2879	sc = ifp->if_softc;
2880
2881#ifdef DEV_NETMAP
2882	/* XXX is this necessary ? */
2883	if (ifp->if_capenable & IFCAP_NETMAP) {
2884		struct netmap_kring *kring = &NA(ifp)->tx_rings[0];
2885		if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2886			/* kick the tx unit */
2887			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2888#ifdef RE_TX_MODERATION
2889			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2890#endif
2891			sc->rl_watchdog_timer = 5;
2892		}
2893		return;
2894	}
2895#endif /* DEV_NETMAP */
2896	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2897	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2898		return;
2899
2900	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2901	    sc->rl_ldata.rl_tx_free > 1;) {
2902		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2903		if (m_head == NULL)
2904			break;
2905
2906		if (re_encap(sc, &m_head) != 0) {
2907			if (m_head == NULL)
2908				break;
2909			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2910			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2911			break;
2912		}
2913
2914		/*
2915		 * If there's a BPF listener, bounce a copy of this frame
2916		 * to him.
2917		 */
2918		ETHER_BPF_MTAP(ifp, m_head);
2919
2920		queued++;
2921	}
2922
2923	if (queued == 0) {
2924#ifdef RE_TX_MODERATION
2925		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2926			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2927#endif
2928		return;
2929	}
2930
2931	/* Flush the TX descriptors */
2932
2933	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2934	    sc->rl_ldata.rl_tx_list_map,
2935	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2936
2937	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2938
2939#ifdef RE_TX_MODERATION
2940	/*
2941	 * Use the countdown timer for interrupt moderation.
2942	 * 'TX done' interrupts are disabled. Instead, we reset the
2943	 * countdown timer, which will begin counting until it hits
2944	 * the value in the TIMERINT register, and then trigger an
2945	 * interrupt. Each time we write to the TIMERCNT register,
2946	 * the timer count is reset to 0.
2947	 */
2948	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2949#endif
2950
2951	/*
2952	 * Set a timeout in case the chip goes out to lunch.
2953	 */
2954	sc->rl_watchdog_timer = 5;
2955}
2956
2957static void
2958re_set_jumbo(struct rl_softc *sc, int jumbo)
2959{
2960
2961	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
2962		pci_set_max_read_req(sc->rl_dev, 4096);
2963		return;
2964	}
2965
2966	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2967	if (jumbo != 0) {
2968		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
2969		    RL_CFG3_JUMBO_EN0);
2970		switch (sc->rl_hwrev->rl_rev) {
2971		case RL_HWREV_8168DP:
2972			break;
2973		case RL_HWREV_8168E:
2974			CSR_WRITE_1(sc, sc->rl_cfg4,
2975			    CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
2976			break;
2977		default:
2978			CSR_WRITE_1(sc, sc->rl_cfg4,
2979			    CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
2980		}
2981	} else {
2982		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
2983		    ~RL_CFG3_JUMBO_EN0);
2984		switch (sc->rl_hwrev->rl_rev) {
2985		case RL_HWREV_8168DP:
2986			break;
2987		case RL_HWREV_8168E:
2988			CSR_WRITE_1(sc, sc->rl_cfg4,
2989			    CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
2990			break;
2991		default:
2992			CSR_WRITE_1(sc, sc->rl_cfg4,
2993			    CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
2994		}
2995	}
2996	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2997
2998	switch (sc->rl_hwrev->rl_rev) {
2999	case RL_HWREV_8168DP:
3000		pci_set_max_read_req(sc->rl_dev, 4096);
3001		break;
3002	default:
3003		if (jumbo != 0)
3004			pci_set_max_read_req(sc->rl_dev, 512);
3005		else
3006			pci_set_max_read_req(sc->rl_dev, 4096);
3007	}
3008}
3009
3010static void
3011re_init(void *xsc)
3012{
3013	struct rl_softc		*sc = xsc;
3014
3015	RL_LOCK(sc);
3016	re_init_locked(sc);
3017	RL_UNLOCK(sc);
3018}
3019
3020static void
3021re_init_locked(struct rl_softc *sc)
3022{
3023	struct ifnet		*ifp = sc->rl_ifp;
3024	struct mii_data		*mii;
3025	uint32_t		reg;
3026	uint16_t		cfg;
3027	union {
3028		uint32_t align_dummy;
3029		u_char eaddr[ETHER_ADDR_LEN];
3030        } eaddr;
3031
3032	RL_LOCK_ASSERT(sc);
3033
3034	mii = device_get_softc(sc->rl_miibus);
3035
3036	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3037		return;
3038
3039	/*
3040	 * Cancel pending I/O and free all RX/TX buffers.
3041	 */
3042	re_stop(sc);
3043
3044	/* Put controller into known state. */
3045	re_reset(sc);
3046
3047	/*
3048	 * For C+ mode, initialize the RX descriptors and mbufs.
3049	 */
3050	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3051		if (ifp->if_mtu > RL_MTU) {
3052			if (re_jrx_list_init(sc) != 0) {
3053				device_printf(sc->rl_dev,
3054				    "no memory for jumbo RX buffers\n");
3055				re_stop(sc);
3056				return;
3057			}
3058			/* Disable checksum offloading for jumbo frames. */
3059			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
3060			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
3061		} else {
3062			if (re_rx_list_init(sc) != 0) {
3063				device_printf(sc->rl_dev,
3064				    "no memory for RX buffers\n");
3065				re_stop(sc);
3066				return;
3067			}
3068		}
3069		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
3070	} else {
3071		if (re_rx_list_init(sc) != 0) {
3072			device_printf(sc->rl_dev, "no memory for RX buffers\n");
3073			re_stop(sc);
3074			return;
3075		}
3076		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3077		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
3078			if (ifp->if_mtu > RL_MTU)
3079				pci_set_max_read_req(sc->rl_dev, 512);
3080			else
3081				pci_set_max_read_req(sc->rl_dev, 4096);
3082		}
3083	}
3084	re_tx_list_init(sc);
3085
3086	/*
3087	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3088	 * RX checksum offload. We must configure the C+ register
3089	 * before all others.
3090	 */
3091	cfg = RL_CPLUSCMD_PCI_MRW;
3092	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
3093		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
3094	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
3095		cfg |= RL_CPLUSCMD_VLANSTRIP;
3096	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3097		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3098		/* XXX magic. */
3099		cfg |= 0x0001;
3100	} else
3101		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3102	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
3103	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
3104	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3105		reg = 0x000fff00;
3106		if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3107			reg |= 0x000000ff;
3108		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3109			reg |= 0x00f00000;
3110		CSR_WRITE_4(sc, 0x7c, reg);
3111		/* Disable interrupt mitigation. */
3112		CSR_WRITE_2(sc, 0xe2, 0);
3113	}
3114	/*
3115	 * Disable TSO if interface MTU size is greater than MSS
3116	 * allowed in controller.
3117	 */
3118	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3119		ifp->if_capenable &= ~IFCAP_TSO4;
3120		ifp->if_hwassist &= ~CSUM_TSO;
3121	}
3122
3123	/*
3124	 * Init our MAC address.  Even though the chipset
3125	 * documentation doesn't mention it, we need to enter "Config
3126	 * register write enable" mode to modify the ID registers.
3127	 */
3128	/* Copy MAC address on stack to align. */
3129	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3130	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3131	CSR_WRITE_4(sc, RL_IDR0,
3132	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3133	CSR_WRITE_4(sc, RL_IDR4,
3134	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3135	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3136
3137	/*
3138	 * Load the addresses of the RX and TX lists into the chip.
3139	 */
3140
3141	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3142	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3143	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3144	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3145
3146	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3147	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3148	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3149	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3150
3151	/*
3152	 * Enable transmit and receive.
3153	 */
3154	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3155
3156	/*
3157	 * Set the initial TX configuration.
3158	 */
3159	if (sc->rl_testmode) {
3160		if (sc->rl_type == RL_8169)
3161			CSR_WRITE_4(sc, RL_TXCFG,
3162			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3163		else
3164			CSR_WRITE_4(sc, RL_TXCFG,
3165			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3166	} else
3167		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3168
3169	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3170
3171	/*
3172	 * Set the initial RX configuration.
3173	 */
3174	re_set_rxmode(sc);
3175
3176	/* Configure interrupt moderation. */
3177	if (sc->rl_type == RL_8169) {
3178		/* Magic from vendor. */
3179		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3180	}
3181
3182#ifdef DEVICE_POLLING
3183	/*
3184	 * Disable interrupts if we are polling.
3185	 */
3186	if (ifp->if_capenable & IFCAP_POLLING)
3187		CSR_WRITE_2(sc, RL_IMR, 0);
3188	else	/* otherwise ... */
3189#endif
3190
3191	/*
3192	 * Enable interrupts.
3193	 */
3194	if (sc->rl_testmode)
3195		CSR_WRITE_2(sc, RL_IMR, 0);
3196	else
3197		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3198	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3199
3200	/* Set initial TX threshold */
3201	sc->rl_txthresh = RL_TX_THRESH_INIT;
3202
3203	/* Start RX/TX process. */
3204	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3205#ifdef notdef
3206	/* Enable receiver and transmitter. */
3207	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3208#endif
3209
3210	/*
3211	 * Initialize the timer interrupt register so that
3212	 * a timer interrupt will be generated once the timer
3213	 * reaches a certain number of ticks. The timer is
3214	 * reloaded on each transmit.
3215	 */
3216#ifdef RE_TX_MODERATION
3217	/*
3218	 * Use timer interrupt register to moderate TX interrupt
3219	 * moderation, which dramatically improves TX frame rate.
3220	 */
3221	if (sc->rl_type == RL_8169)
3222		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3223	else
3224		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3225#else
3226	/*
3227	 * Use timer interrupt register to moderate RX interrupt
3228	 * moderation.
3229	 */
3230	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3231	    intr_filter == 0) {
3232		if (sc->rl_type == RL_8169)
3233			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3234			    RL_USECS(sc->rl_int_rx_mod));
3235	} else {
3236		if (sc->rl_type == RL_8169)
3237			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3238	}
3239#endif
3240
3241	/*
3242	 * For 8169 gigE NICs, set the max allowed RX packet
3243	 * size so we can receive jumbo frames.
3244	 */
3245	if (sc->rl_type == RL_8169) {
3246		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3247			/*
3248			 * For controllers that use new jumbo frame scheme,
3249			 * set maximum size of jumbo frame depedning on
3250			 * controller revisions.
3251			 */
3252			if (ifp->if_mtu > RL_MTU)
3253				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3254				    sc->rl_hwrev->rl_max_mtu +
3255				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
3256				    ETHER_CRC_LEN);
3257			else
3258				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3259				    RE_RX_DESC_BUFLEN);
3260		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3261		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
3262			/* RTL810x has no jumbo frame support. */
3263			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
3264		} else
3265			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
3266	}
3267
3268	if (sc->rl_testmode)
3269		return;
3270
3271	CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3272	    RL_CFG1_DRVLOAD);
3273
3274	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3275	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3276
3277	sc->rl_flags &= ~RL_FLAG_LINK;
3278	mii_mediachg(mii);
3279
3280	sc->rl_watchdog_timer = 0;
3281	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3282}
3283
3284/*
3285 * Set media options.
3286 */
3287static int
3288re_ifmedia_upd(struct ifnet *ifp)
3289{
3290	struct rl_softc		*sc;
3291	struct mii_data		*mii;
3292	int			error;
3293
3294	sc = ifp->if_softc;
3295	mii = device_get_softc(sc->rl_miibus);
3296	RL_LOCK(sc);
3297	error = mii_mediachg(mii);
3298	RL_UNLOCK(sc);
3299
3300	return (error);
3301}
3302
3303/*
3304 * Report current media status.
3305 */
3306static void
3307re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3308{
3309	struct rl_softc		*sc;
3310	struct mii_data		*mii;
3311
3312	sc = ifp->if_softc;
3313	mii = device_get_softc(sc->rl_miibus);
3314
3315	RL_LOCK(sc);
3316	mii_pollstat(mii);
3317	ifmr->ifm_active = mii->mii_media_active;
3318	ifmr->ifm_status = mii->mii_media_status;
3319	RL_UNLOCK(sc);
3320}
3321
3322static int
3323re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3324{
3325	struct rl_softc		*sc = ifp->if_softc;
3326	struct ifreq		*ifr = (struct ifreq *) data;
3327	struct mii_data		*mii;
3328	uint32_t		rev;
3329	int			error = 0;
3330
3331	switch (command) {
3332	case SIOCSIFMTU:
3333		if (ifr->ifr_mtu < ETHERMIN ||
3334		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3335			error = EINVAL;
3336			break;
3337		}
3338		RL_LOCK(sc);
3339		if (ifp->if_mtu != ifr->ifr_mtu) {
3340			ifp->if_mtu = ifr->ifr_mtu;
3341			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3342			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3343				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3344				re_init_locked(sc);
3345			}
3346			if (ifp->if_mtu > RL_TSO_MTU &&
3347			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3348				ifp->if_capenable &= ~(IFCAP_TSO4 |
3349				    IFCAP_VLAN_HWTSO);
3350				ifp->if_hwassist &= ~CSUM_TSO;
3351			}
3352			VLAN_CAPABILITIES(ifp);
3353		}
3354		RL_UNLOCK(sc);
3355		break;
3356	case SIOCSIFFLAGS:
3357		RL_LOCK(sc);
3358		if ((ifp->if_flags & IFF_UP) != 0) {
3359			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3360				if (((ifp->if_flags ^ sc->rl_if_flags)
3361				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3362					re_set_rxmode(sc);
3363			} else
3364				re_init_locked(sc);
3365		} else {
3366			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3367				re_stop(sc);
3368		}
3369		sc->rl_if_flags = ifp->if_flags;
3370		RL_UNLOCK(sc);
3371		break;
3372	case SIOCADDMULTI:
3373	case SIOCDELMULTI:
3374		RL_LOCK(sc);
3375		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3376			re_set_rxmode(sc);
3377		RL_UNLOCK(sc);
3378		break;
3379	case SIOCGIFMEDIA:
3380	case SIOCSIFMEDIA:
3381		mii = device_get_softc(sc->rl_miibus);
3382		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3383		break;
3384	case SIOCSIFCAP:
3385	    {
3386		int mask, reinit;
3387
3388		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3389		reinit = 0;
3390#ifdef DEVICE_POLLING
3391		if (mask & IFCAP_POLLING) {
3392			if (ifr->ifr_reqcap & IFCAP_POLLING) {
3393				error = ether_poll_register(re_poll, ifp);
3394				if (error)
3395					return (error);
3396				RL_LOCK(sc);
3397				/* Disable interrupts */
3398				CSR_WRITE_2(sc, RL_IMR, 0x0000);
3399				ifp->if_capenable |= IFCAP_POLLING;
3400				RL_UNLOCK(sc);
3401			} else {
3402				error = ether_poll_deregister(ifp);
3403				/* Enable interrupts. */
3404				RL_LOCK(sc);
3405				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3406				ifp->if_capenable &= ~IFCAP_POLLING;
3407				RL_UNLOCK(sc);
3408			}
3409		}
3410#endif /* DEVICE_POLLING */
3411		RL_LOCK(sc);
3412		if ((mask & IFCAP_TXCSUM) != 0 &&
3413		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3414			ifp->if_capenable ^= IFCAP_TXCSUM;
3415			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) {
3416				rev = sc->rl_hwrev->rl_rev;
3417				if (rev == RL_HWREV_8168C ||
3418				    rev == RL_HWREV_8168C_SPIN2)
3419					ifp->if_hwassist |= CSUM_TCP | CSUM_UDP;
3420				else
3421					ifp->if_hwassist |= RE_CSUM_FEATURES;
3422			} else
3423				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3424			reinit = 1;
3425		}
3426		if ((mask & IFCAP_RXCSUM) != 0 &&
3427		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3428			ifp->if_capenable ^= IFCAP_RXCSUM;
3429			reinit = 1;
3430		}
3431		if ((mask & IFCAP_TSO4) != 0 &&
3432		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3433			ifp->if_capenable ^= IFCAP_TSO4;
3434			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3435				ifp->if_hwassist |= CSUM_TSO;
3436			else
3437				ifp->if_hwassist &= ~CSUM_TSO;
3438			if (ifp->if_mtu > RL_TSO_MTU &&
3439			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3440				ifp->if_capenable &= ~IFCAP_TSO4;
3441				ifp->if_hwassist &= ~CSUM_TSO;
3442			}
3443		}
3444		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3445		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3446			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3447		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3448		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3449			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3450			/* TSO over VLAN requires VLAN hardware tagging. */
3451			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3452				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3453			reinit = 1;
3454		}
3455		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3456		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
3457		    IFCAP_VLAN_HWTSO)) != 0)
3458				reinit = 1;
3459		if ((mask & IFCAP_WOL) != 0 &&
3460		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
3461			if ((mask & IFCAP_WOL_UCAST) != 0)
3462				ifp->if_capenable ^= IFCAP_WOL_UCAST;
3463			if ((mask & IFCAP_WOL_MCAST) != 0)
3464				ifp->if_capenable ^= IFCAP_WOL_MCAST;
3465			if ((mask & IFCAP_WOL_MAGIC) != 0)
3466				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
3467		}
3468		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
3469			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3470			re_init_locked(sc);
3471		}
3472		RL_UNLOCK(sc);
3473		VLAN_CAPABILITIES(ifp);
3474	    }
3475		break;
3476	default:
3477		error = ether_ioctl(ifp, command, data);
3478		break;
3479	}
3480
3481	return (error);
3482}
3483
3484static void
3485re_watchdog(struct rl_softc *sc)
3486{
3487	struct ifnet		*ifp;
3488
3489	RL_LOCK_ASSERT(sc);
3490
3491	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
3492		return;
3493
3494	ifp = sc->rl_ifp;
3495	re_txeof(sc);
3496	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3497		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3498		    "-- recovering\n");
3499		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3500			re_start_locked(ifp);
3501		return;
3502	}
3503
3504	if_printf(ifp, "watchdog timeout\n");
3505	ifp->if_oerrors++;
3506
3507	re_rxeof(sc, NULL);
3508	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3509	re_init_locked(sc);
3510	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3511		re_start_locked(ifp);
3512}
3513
3514/*
3515 * Stop the adapter and free any mbufs allocated to the
3516 * RX and TX lists.
3517 */
3518static void
3519re_stop(struct rl_softc *sc)
3520{
3521	int			i;
3522	struct ifnet		*ifp;
3523	struct rl_txdesc	*txd;
3524	struct rl_rxdesc	*rxd;
3525
3526	RL_LOCK_ASSERT(sc);
3527
3528	ifp = sc->rl_ifp;
3529
3530	sc->rl_watchdog_timer = 0;
3531	callout_stop(&sc->rl_stat_callout);
3532	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3533
3534	/*
3535	 * Disable accepting frames to put RX MAC into idle state.
3536	 * Otherwise it's possible to get frames while stop command
3537	 * execution is in progress and controller can DMA the frame
3538	 * to already freed RX buffer during that period.
3539	 */
3540	CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3541	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3542	    RL_RXCFG_RX_BROAD));
3543
3544	if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3545		for (i = RL_TIMEOUT; i > 0; i--) {
3546			if ((CSR_READ_1(sc, sc->rl_txstart) &
3547			    RL_TXSTART_START) == 0)
3548				break;
3549			DELAY(20);
3550		}
3551		if (i == 0)
3552			device_printf(sc->rl_dev,
3553			    "stopping TX poll timed out!\n");
3554		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3555	} else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3556		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3557		    RL_CMD_RX_ENB);
3558		if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3559			for (i = RL_TIMEOUT; i > 0; i--) {
3560				if ((CSR_READ_4(sc, RL_TXCFG) &
3561				    RL_TXCFG_QUEUE_EMPTY) != 0)
3562					break;
3563				DELAY(100);
3564			}
3565			if (i == 0)
3566				device_printf(sc->rl_dev,
3567				   "stopping TXQ timed out!\n");
3568		}
3569	} else
3570		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3571	DELAY(1000);
3572	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3573	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3574
3575	if (sc->rl_head != NULL) {
3576		m_freem(sc->rl_head);
3577		sc->rl_head = sc->rl_tail = NULL;
3578	}
3579
3580	/* Free the TX list buffers. */
3581	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3582		txd = &sc->rl_ldata.rl_tx_desc[i];
3583		if (txd->tx_m != NULL) {
3584			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3585			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3586			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3587			    txd->tx_dmamap);
3588			m_freem(txd->tx_m);
3589			txd->tx_m = NULL;
3590		}
3591	}
3592
3593	/* Free the RX list buffers. */
3594	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3595		rxd = &sc->rl_ldata.rl_rx_desc[i];
3596		if (rxd->rx_m != NULL) {
3597			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3598			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3599			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3600			    rxd->rx_dmamap);
3601			m_freem(rxd->rx_m);
3602			rxd->rx_m = NULL;
3603		}
3604	}
3605
3606	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3607		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3608			rxd = &sc->rl_ldata.rl_jrx_desc[i];
3609			if (rxd->rx_m != NULL) {
3610				bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
3611				    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3612				bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
3613				    rxd->rx_dmamap);
3614				m_freem(rxd->rx_m);
3615				rxd->rx_m = NULL;
3616			}
3617		}
3618	}
3619}
3620
3621/*
3622 * Device suspend routine.  Stop the interface and save some PCI
3623 * settings in case the BIOS doesn't restore them properly on
3624 * resume.
3625 */
3626static int
3627re_suspend(device_t dev)
3628{
3629	struct rl_softc		*sc;
3630
3631	sc = device_get_softc(dev);
3632
3633	RL_LOCK(sc);
3634	re_stop(sc);
3635	re_setwol(sc);
3636	sc->suspended = 1;
3637	RL_UNLOCK(sc);
3638
3639	return (0);
3640}
3641
3642/*
3643 * Device resume routine.  Restore some PCI settings in case the BIOS
3644 * doesn't, re-enable busmastering, and restart the interface if
3645 * appropriate.
3646 */
3647static int
3648re_resume(device_t dev)
3649{
3650	struct rl_softc		*sc;
3651	struct ifnet		*ifp;
3652
3653	sc = device_get_softc(dev);
3654
3655	RL_LOCK(sc);
3656
3657	ifp = sc->rl_ifp;
3658	/* Take controller out of sleep mode. */
3659	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
3660		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
3661			CSR_WRITE_1(sc, RL_GPIO,
3662			    CSR_READ_1(sc, RL_GPIO) | 0x01);
3663	}
3664
3665	/*
3666	 * Clear WOL matching such that normal Rx filtering
3667	 * wouldn't interfere with WOL patterns.
3668	 */
3669	re_clrwol(sc);
3670
3671	/* reinitialize interface if necessary */
3672	if (ifp->if_flags & IFF_UP)
3673		re_init_locked(sc);
3674
3675	sc->suspended = 0;
3676	RL_UNLOCK(sc);
3677
3678	return (0);
3679}
3680
3681/*
3682 * Stop all chip I/O so that the kernel's probe routines don't
3683 * get confused by errant DMAs when rebooting.
3684 */
3685static int
3686re_shutdown(device_t dev)
3687{
3688	struct rl_softc		*sc;
3689
3690	sc = device_get_softc(dev);
3691
3692	RL_LOCK(sc);
3693	re_stop(sc);
3694	/*
3695	 * Mark interface as down since otherwise we will panic if
3696	 * interrupt comes in later on, which can happen in some
3697	 * cases.
3698	 */
3699	sc->rl_ifp->if_flags &= ~IFF_UP;
3700	re_setwol(sc);
3701	RL_UNLOCK(sc);
3702
3703	return (0);
3704}
3705
3706static void
3707re_set_linkspeed(struct rl_softc *sc)
3708{
3709	struct mii_softc *miisc;
3710	struct mii_data *mii;
3711	int aneg, i, phyno;
3712
3713	RL_LOCK_ASSERT(sc);
3714
3715	mii = device_get_softc(sc->rl_miibus);
3716	mii_pollstat(mii);
3717	aneg = 0;
3718	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
3719	    (IFM_ACTIVE | IFM_AVALID)) {
3720		switch IFM_SUBTYPE(mii->mii_media_active) {
3721		case IFM_10_T:
3722		case IFM_100_TX:
3723			return;
3724		case IFM_1000_T:
3725			aneg++;
3726			break;
3727		default:
3728			break;
3729		}
3730	}
3731	miisc = LIST_FIRST(&mii->mii_phys);
3732	phyno = miisc->mii_phy;
3733	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3734		PHY_RESET(miisc);
3735	re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
3736	re_miibus_writereg(sc->rl_dev, phyno,
3737	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
3738	re_miibus_writereg(sc->rl_dev, phyno,
3739	    MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
3740	DELAY(1000);
3741	if (aneg != 0) {
3742		/*
3743		 * Poll link state until re(4) get a 10/100Mbps link.
3744		 */
3745		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
3746			mii_pollstat(mii);
3747			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
3748			    == (IFM_ACTIVE | IFM_AVALID)) {
3749				switch (IFM_SUBTYPE(mii->mii_media_active)) {
3750				case IFM_10_T:
3751				case IFM_100_TX:
3752					return;
3753				default:
3754					break;
3755				}
3756			}
3757			RL_UNLOCK(sc);
3758			pause("relnk", hz);
3759			RL_LOCK(sc);
3760		}
3761		if (i == MII_ANEGTICKS_GIGE)
3762			device_printf(sc->rl_dev,
3763			    "establishing a link failed, WOL may not work!");
3764	}
3765	/*
3766	 * No link, force MAC to have 100Mbps, full-duplex link.
3767	 * MAC does not require reprogramming on resolved speed/duplex,
3768	 * so this is just for completeness.
3769	 */
3770	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
3771	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
3772}
3773
3774static void
3775re_setwol(struct rl_softc *sc)
3776{
3777	struct ifnet		*ifp;
3778	int			pmc;
3779	uint16_t		pmstat;
3780	uint8_t			v;
3781
3782	RL_LOCK_ASSERT(sc);
3783
3784	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
3785		return;
3786
3787	ifp = sc->rl_ifp;
3788	/* Put controller into sleep mode. */
3789	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
3790		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
3791			CSR_WRITE_1(sc, RL_GPIO,
3792			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
3793	}
3794	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
3795		re_set_rxmode(sc);
3796		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
3797			re_set_linkspeed(sc);
3798		if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3799			CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
3800	}
3801	/* Enable config register write. */
3802	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
3803
3804	/* Enable PME. */
3805	v = CSR_READ_1(sc, sc->rl_cfg1);
3806	v &= ~RL_CFG1_PME;
3807	if ((ifp->if_capenable & IFCAP_WOL) != 0)
3808		v |= RL_CFG1_PME;
3809	CSR_WRITE_1(sc, sc->rl_cfg1, v);
3810
3811	v = CSR_READ_1(sc, sc->rl_cfg3);
3812	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3813	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
3814		v |= RL_CFG3_WOL_MAGIC;
3815	CSR_WRITE_1(sc, sc->rl_cfg3, v);
3816
3817	v = CSR_READ_1(sc, sc->rl_cfg5);
3818	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
3819	    RL_CFG5_WOL_LANWAKE);
3820	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
3821		v |= RL_CFG5_WOL_UCAST;
3822	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
3823		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
3824	if ((ifp->if_capenable & IFCAP_WOL) != 0)
3825		v |= RL_CFG5_WOL_LANWAKE;
3826	CSR_WRITE_1(sc, sc->rl_cfg5, v);
3827
3828	/* Config register write done. */
3829	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3830
3831	if ((ifp->if_capenable & IFCAP_WOL) == 0 &&
3832	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3833		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
3834	/*
3835	 * It seems that hardware resets its link speed to 100Mbps in
3836	 * power down mode so switching to 100Mbps in driver is not
3837	 * needed.
3838	 */
3839
3840	/* Request PME if WOL is requested. */
3841	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
3842	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
3843	if ((ifp->if_capenable & IFCAP_WOL) != 0)
3844		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
3845	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
3846}
3847
3848static void
3849re_clrwol(struct rl_softc *sc)
3850{
3851	int			pmc;
3852	uint8_t			v;
3853
3854	RL_LOCK_ASSERT(sc);
3855
3856	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
3857		return;
3858
3859	/* Enable config register write. */
3860	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
3861
3862	v = CSR_READ_1(sc, sc->rl_cfg3);
3863	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3864	CSR_WRITE_1(sc, sc->rl_cfg3, v);
3865
3866	/* Config register write done. */
3867	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3868
3869	v = CSR_READ_1(sc, sc->rl_cfg5);
3870	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
3871	v &= ~RL_CFG5_WOL_LANWAKE;
3872	CSR_WRITE_1(sc, sc->rl_cfg5, v);
3873}
3874
3875static void
3876re_add_sysctls(struct rl_softc *sc)
3877{
3878	struct sysctl_ctx_list	*ctx;
3879	struct sysctl_oid_list	*children;
3880	int			error;
3881
3882	ctx = device_get_sysctl_ctx(sc->rl_dev);
3883	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
3884
3885	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
3886	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
3887	    "Statistics Information");
3888	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3889		return;
3890
3891	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3892	    CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3893	    sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3894	/* Pull in device tunables. */
3895	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3896	error = resource_int_value(device_get_name(sc->rl_dev),
3897	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3898	if (error == 0) {
3899		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3900		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3901			device_printf(sc->rl_dev, "int_rx_mod value out of "
3902			    "range; using default: %d\n",
3903			    RL_TIMER_DEFAULT);
3904			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3905		}
3906	}
3907
3908}
3909
3910static int
3911re_sysctl_stats(SYSCTL_HANDLER_ARGS)
3912{
3913	struct rl_softc		*sc;
3914	struct rl_stats		*stats;
3915	int			error, i, result;
3916
3917	result = -1;
3918	error = sysctl_handle_int(oidp, &result, 0, req);
3919	if (error || req->newptr == NULL)
3920		return (error);
3921
3922	if (result == 1) {
3923		sc = (struct rl_softc *)arg1;
3924		RL_LOCK(sc);
3925		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3926			RL_UNLOCK(sc);
3927			goto done;
3928		}
3929		bus_dmamap_sync(sc->rl_ldata.rl_stag,
3930		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
3931		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
3932		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
3933		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
3934		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
3935		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
3936		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
3937		    RL_DUMPSTATS_START));
3938		for (i = RL_TIMEOUT; i > 0; i--) {
3939			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
3940			    RL_DUMPSTATS_START) == 0)
3941				break;
3942			DELAY(1000);
3943		}
3944		bus_dmamap_sync(sc->rl_ldata.rl_stag,
3945		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
3946		RL_UNLOCK(sc);
3947		if (i == 0) {
3948			device_printf(sc->rl_dev,
3949			    "DUMP statistics request timedout\n");
3950			return (ETIMEDOUT);
3951		}
3952done:
3953		stats = sc->rl_ldata.rl_stats;
3954		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
3955		printf("Tx frames : %ju\n",
3956		    (uintmax_t)le64toh(stats->rl_tx_pkts));
3957		printf("Rx frames : %ju\n",
3958		    (uintmax_t)le64toh(stats->rl_rx_pkts));
3959		printf("Tx errors : %ju\n",
3960		    (uintmax_t)le64toh(stats->rl_tx_errs));
3961		printf("Rx errors : %u\n",
3962		    le32toh(stats->rl_rx_errs));
3963		printf("Rx missed frames : %u\n",
3964		    (uint32_t)le16toh(stats->rl_missed_pkts));
3965		printf("Rx frame alignment errs : %u\n",
3966		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
3967		printf("Tx single collisions : %u\n",
3968		    le32toh(stats->rl_tx_onecoll));
3969		printf("Tx multiple collisions : %u\n",
3970		    le32toh(stats->rl_tx_multicolls));
3971		printf("Rx unicast frames : %ju\n",
3972		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
3973		printf("Rx broadcast frames : %ju\n",
3974		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
3975		printf("Rx multicast frames : %u\n",
3976		    le32toh(stats->rl_rx_mcasts));
3977		printf("Tx aborts : %u\n",
3978		    (uint32_t)le16toh(stats->rl_tx_aborts));
3979		printf("Tx underruns : %u\n",
3980		    (uint32_t)le16toh(stats->rl_rx_underruns));
3981	}
3982
3983	return (error);
3984}
3985
3986static int
3987sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3988{
3989	int error, value;
3990
3991	if (arg1 == NULL)
3992		return (EINVAL);
3993	value = *(int *)arg1;
3994	error = sysctl_handle_int(oidp, &value, 0, req);
3995	if (error || req->newptr == NULL)
3996		return (error);
3997	if (value < low || value > high)
3998		return (EINVAL);
3999	*(int *)arg1 = value;
4000
4001	return (0);
4002}
4003
4004static int
4005sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4006{
4007
4008	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
4009	    RL_TIMER_MAX));
4010}
4011