2 * Copyright (c) 1997, 1998-2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
39 * Written by Bill Paul <wpaul@windriver.com>
40 * Senior Networking Software Engineer
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.
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:
55 * o Descriptor based DMA mechanism. Each descriptor represents
56 * a single packet fragment. Data buffers may be aligned on
61 * o TCP/IP checksum offload for both RX and TX
63 * o High and normal priority transmit DMA rings
65 * o VLAN tag insertion and extraction
67 * o TCP large send (segmentation offload)
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
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:
82 * o GMII and TBI ports/registers for interfacing with copper
85 * o RX and TX DMA rings can have up to 1024 descriptors
86 * (the 8139C+ allows a maximum of 64)
88 * o Slight differences in register layout from the 8139C+
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'
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.
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.
111 #ifdef HAVE_KERNEL_OPTION_HEADERS
112 #include "opt_device_polling.h"
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>
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>
139 #include <machine/bus.h>
140 #include <machine/resource.h>
142 #include <sys/rman.h>
144 #include <dev/mii/mii.h>
145 #include <dev/mii/miivar.h>
147 #include <dev/pci/pcireg.h>
148 #include <dev/pci/pcivar.h>
150 #include <dev/rl/if_rlreg.h>
152 MODULE_DEPEND(re, pci, 1, 1, 1);
153 MODULE_DEPEND(re, ether, 1, 1, 1);
154 MODULE_DEPEND(re, miibus, 1, 1, 1);
156 /* "device miibus" required. See GENERIC if you get errors here. */
157 #include "miibus_if.h"
160 static int intr_filter = 0;
161 TUNABLE_INT("hw.re.intr_filter", &intr_filter);
162 static int msi_disable = 0;
163 TUNABLE_INT("hw.re.msi_disable", &msi_disable);
164 static int msix_disable = 0;
165 TUNABLE_INT("hw.re.msix_disable", &msix_disable);
166 static int prefer_iomap = 0;
167 TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
169 #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
172 * Various supported device vendors/types and their names.
174 static 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/G 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" }
197 static 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_8106E, RL_8169, "8106E", RL_MTU },
227 { RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
228 { RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
229 { RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
230 { RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
231 { RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
232 { RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
233 { RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
234 { RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
235 { RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
236 { RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K},
237 { RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
238 { RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K},
239 { RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K},
240 { RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K},
241 { RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
242 { RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K},
246 static int re_probe (device_t);
247 static int re_attach (device_t);
248 static int re_detach (device_t);
250 static int re_encap (struct rl_softc *, struct mbuf **);
252 static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int);
253 static int re_allocmem (device_t, struct rl_softc *);
254 static __inline void re_discard_rxbuf
255 (struct rl_softc *, int);
256 static int re_newbuf (struct rl_softc *, int);
257 static int re_jumbo_newbuf (struct rl_softc *, int);
258 static int re_rx_list_init (struct rl_softc *);
259 static int re_jrx_list_init (struct rl_softc *);
260 static int re_tx_list_init (struct rl_softc *);
262 static __inline void re_fixup_rx
265 static int re_rxeof (struct rl_softc *, int *);
266 static void re_txeof (struct rl_softc *);
267 #ifdef DEVICE_POLLING
268 static int re_poll (struct ifnet *, enum poll_cmd, int);
269 static int re_poll_locked (struct ifnet *, enum poll_cmd, int);
271 static int re_intr (void *);
272 static void re_intr_msi (void *);
273 static void re_tick (void *);
274 static void re_int_task (void *, int);
275 static void re_start (struct ifnet *);
276 static void re_start_locked (struct ifnet *);
277 static int re_ioctl (struct ifnet *, u_long, caddr_t);
278 static void re_init (void *);
279 static void re_init_locked (struct rl_softc *);
280 static void re_stop (struct rl_softc *);
281 static void re_watchdog (struct rl_softc *);
282 static int re_suspend (device_t);
283 static int re_resume (device_t);
284 static int re_shutdown (device_t);
285 static int re_ifmedia_upd (struct ifnet *);
286 static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *);
288 static void re_eeprom_putbyte (struct rl_softc *, int);
289 static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *);
290 static void re_read_eeprom (struct rl_softc *, caddr_t, int, int);
291 static int re_gmii_readreg (device_t, int, int);
292 static int re_gmii_writereg (device_t, int, int, int);
294 static int re_miibus_readreg (device_t, int, int);
295 static int re_miibus_writereg (device_t, int, int, int);
296 static void re_miibus_statchg (device_t);
298 static void re_set_jumbo (struct rl_softc *, int);
299 static void re_set_rxmode (struct rl_softc *);
300 static void re_reset (struct rl_softc *);
301 static void re_setwol (struct rl_softc *);
302 static void re_clrwol (struct rl_softc *);
303 static void re_set_linkspeed (struct rl_softc *);
305 #ifdef DEV_NETMAP /* see ixgbe.c for details */
306 #include <dev/netmap/if_re_netmap.h>
307 #endif /* !DEV_NETMAP */
310 static int re_diag (struct rl_softc *);
313 static void re_add_sysctls (struct rl_softc *);
314 static int re_sysctl_stats (SYSCTL_HANDLER_ARGS);
315 static int sysctl_int_range (SYSCTL_HANDLER_ARGS, int, int);
316 static int sysctl_hw_re_int_mod (SYSCTL_HANDLER_ARGS);
318 static device_method_t re_methods[] = {
319 /* Device interface */
320 DEVMETHOD(device_probe, re_probe),
321 DEVMETHOD(device_attach, re_attach),
322 DEVMETHOD(device_detach, re_detach),
323 DEVMETHOD(device_suspend, re_suspend),
324 DEVMETHOD(device_resume, re_resume),
325 DEVMETHOD(device_shutdown, re_shutdown),
328 DEVMETHOD(miibus_readreg, re_miibus_readreg),
329 DEVMETHOD(miibus_writereg, re_miibus_writereg),
330 DEVMETHOD(miibus_statchg, re_miibus_statchg),
335 static driver_t re_driver = {
338 sizeof(struct rl_softc)
341 static devclass_t re_devclass;
343 DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
344 DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
347 CSR_WRITE_1(sc, RL_EECMD, \
348 CSR_READ_1(sc, RL_EECMD) | x)
351 CSR_WRITE_1(sc, RL_EECMD, \
352 CSR_READ_1(sc, RL_EECMD) & ~x)
355 * Send a read command and address to the EEPROM, check for ACK.
358 re_eeprom_putbyte(struct rl_softc *sc, int addr)
362 d = addr | (RL_9346_READ << sc->rl_eewidth);
365 * Feed in each bit and strobe the clock.
368 for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
370 EE_SET(RL_EE_DATAIN);
372 EE_CLR(RL_EE_DATAIN);
383 * Read a word of data stored in the EEPROM at address 'addr.'
386 re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
392 * Send address of word we want to read.
394 re_eeprom_putbyte(sc, addr);
397 * Start reading bits from EEPROM.
399 for (i = 0x8000; i; i >>= 1) {
402 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
412 * Read a sequence of words from the EEPROM.
415 re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
418 u_int16_t word = 0, *ptr;
420 CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
424 for (i = 0; i < cnt; i++) {
425 CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
426 re_eeprom_getword(sc, off + i, &word);
427 CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
428 ptr = (u_int16_t *)(dest + (i * 2));
432 CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
436 re_gmii_readreg(device_t dev, int phy, int reg)
442 sc = device_get_softc(dev);
444 /* Let the rgephy driver read the GMEDIASTAT register */
446 if (reg == RL_GMEDIASTAT) {
447 rval = CSR_READ_1(sc, RL_GMEDIASTAT);
451 CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
453 for (i = 0; i < RL_PHY_TIMEOUT; i++) {
454 rval = CSR_READ_4(sc, RL_PHYAR);
455 if (rval & RL_PHYAR_BUSY)
460 if (i == RL_PHY_TIMEOUT) {
461 device_printf(sc->rl_dev, "PHY read failed\n");
466 * Controller requires a 20us delay to process next MDIO request.
470 return (rval & RL_PHYAR_PHYDATA);
474 re_gmii_writereg(device_t dev, int phy, int reg, int data)
480 sc = device_get_softc(dev);
482 CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
483 (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
485 for (i = 0; i < RL_PHY_TIMEOUT; i++) {
486 rval = CSR_READ_4(sc, RL_PHYAR);
487 if (!(rval & RL_PHYAR_BUSY))
492 if (i == RL_PHY_TIMEOUT) {
493 device_printf(sc->rl_dev, "PHY write failed\n");
498 * Controller requires a 20us delay to process next MDIO request.
506 re_miibus_readreg(device_t dev, int phy, int reg)
510 u_int16_t re8139_reg = 0;
512 sc = device_get_softc(dev);
514 if (sc->rl_type == RL_8169) {
515 rval = re_gmii_readreg(dev, phy, reg);
521 re8139_reg = RL_BMCR;
524 re8139_reg = RL_BMSR;
527 re8139_reg = RL_ANAR;
530 re8139_reg = RL_ANER;
533 re8139_reg = RL_LPAR;
539 * Allow the rlphy driver to read the media status
540 * register. If we have a link partner which does not
541 * support NWAY, this is the register which will tell
542 * us the results of parallel detection.
545 rval = CSR_READ_1(sc, RL_MEDIASTAT);
548 device_printf(sc->rl_dev, "bad phy register\n");
551 rval = CSR_READ_2(sc, re8139_reg);
552 if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
553 /* 8139C+ has different bit layout. */
554 rval &= ~(BMCR_LOOP | BMCR_ISO);
560 re_miibus_writereg(device_t dev, int phy, int reg, int data)
563 u_int16_t re8139_reg = 0;
566 sc = device_get_softc(dev);
568 if (sc->rl_type == RL_8169) {
569 rval = re_gmii_writereg(dev, phy, reg, data);
575 re8139_reg = RL_BMCR;
576 if (sc->rl_type == RL_8139CPLUS) {
577 /* 8139C+ has different bit layout. */
578 data &= ~(BMCR_LOOP | BMCR_ISO);
582 re8139_reg = RL_BMSR;
585 re8139_reg = RL_ANAR;
588 re8139_reg = RL_ANER;
591 re8139_reg = RL_LPAR;
598 device_printf(sc->rl_dev, "bad phy register\n");
601 CSR_WRITE_2(sc, re8139_reg, data);
606 re_miibus_statchg(device_t dev)
610 struct mii_data *mii;
612 sc = device_get_softc(dev);
613 mii = device_get_softc(sc->rl_miibus);
615 if (mii == NULL || ifp == NULL ||
616 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
619 sc->rl_flags &= ~RL_FLAG_LINK;
620 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
621 (IFM_ACTIVE | IFM_AVALID)) {
622 switch (IFM_SUBTYPE(mii->mii_media_active)) {
625 sc->rl_flags |= RL_FLAG_LINK;
628 if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
630 sc->rl_flags |= RL_FLAG_LINK;
637 * RealTek controllers do not provide any interface to the RX/TX
638 * MACs for resolved speed, duplex and flow-control parameters.
643 * Set the RX configuration and 64-bit multicast hash filter.
646 re_set_rxmode(struct rl_softc *sc)
649 struct ifmultiaddr *ifma;
650 uint32_t hashes[2] = { 0, 0 };
657 rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
658 if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0)
659 rxfilt |= RL_RXCFG_EARLYOFF;
660 else if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
661 rxfilt |= RL_RXCFG_EARLYOFFV2;
663 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
664 if (ifp->if_flags & IFF_PROMISC)
665 rxfilt |= RL_RXCFG_RX_ALLPHYS;
667 * Unlike other hardwares, we have to explicitly set
668 * RL_RXCFG_RX_MULTI to receive multicast frames in
671 rxfilt |= RL_RXCFG_RX_MULTI;
672 hashes[0] = hashes[1] = 0xffffffff;
677 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
678 if (ifma->ifma_addr->sa_family != AF_LINK)
680 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
681 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
683 hashes[0] |= (1 << h);
685 hashes[1] |= (1 << (h - 32));
687 if_maddr_runlock(ifp);
689 if (hashes[0] != 0 || hashes[1] != 0) {
691 * For some unfathomable reason, RealTek decided to
692 * reverse the order of the multicast hash registers
693 * in the PCI Express parts. This means we have to
694 * write the hash pattern in reverse order for those
697 if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
698 h = bswap32(hashes[0]);
699 hashes[0] = bswap32(hashes[1]);
702 rxfilt |= RL_RXCFG_RX_MULTI;
705 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168F) {
706 /* Disable multicast filtering due to silicon bug. */
707 hashes[0] = 0xffffffff;
708 hashes[1] = 0xffffffff;
712 CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
713 CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
714 CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
718 re_reset(struct rl_softc *sc)
724 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
726 for (i = 0; i < RL_TIMEOUT; i++) {
728 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
732 device_printf(sc->rl_dev, "reset never completed!\n");
734 if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
735 CSR_WRITE_1(sc, 0x82, 1);
736 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
737 re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
743 * The following routine is designed to test for a defect on some
744 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
745 * lines connected to the bus, however for a 32-bit only card, they
746 * should be pulled high. The result of this defect is that the
747 * NIC will not work right if you plug it into a 64-bit slot: DMA
748 * operations will be done with 64-bit transfers, which will fail
749 * because the 64-bit data lines aren't connected.
751 * There's no way to work around this (short of talking a soldering
752 * iron to the board), however we can detect it. The method we use
753 * here is to put the NIC into digital loopback mode, set the receiver
754 * to promiscuous mode, and then try to send a frame. We then compare
755 * the frame data we sent to what was received. If the data matches,
756 * then the NIC is working correctly, otherwise we know the user has
757 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
758 * slot. In the latter case, there's no way the NIC can work correctly,
759 * so we print out a message on the console and abort the device attach.
763 re_diag(struct rl_softc *sc)
765 struct ifnet *ifp = sc->rl_ifp;
767 struct ether_header *eh;
768 struct rl_desc *cur_rx;
771 int total_len, i, error = 0, phyaddr;
772 u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
773 u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
775 /* Allocate a single mbuf */
776 MGETHDR(m0, M_NOWAIT, MT_DATA);
783 * Initialize the NIC in test mode. This sets the chip up
784 * so that it can send and receive frames, but performs the
785 * following special functions:
786 * - Puts receiver in promiscuous mode
787 * - Enables digital loopback mode
788 * - Leaves interrupts turned off
791 ifp->if_flags |= IFF_PROMISC;
793 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
795 sc->rl_flags |= RL_FLAG_LINK;
796 if (sc->rl_type == RL_8169)
801 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
802 for (i = 0; i < RL_TIMEOUT; i++) {
803 status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
804 if (!(status & BMCR_RESET))
808 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
809 CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
813 /* Put some data in the mbuf */
815 eh = mtod(m0, struct ether_header *);
816 bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
817 bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
818 eh->ether_type = htons(ETHERTYPE_IP);
819 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
822 * Queue the packet, start transmission.
823 * Note: IF_HANDOFF() ultimately calls re_start() for us.
826 CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
828 /* XXX: re_diag must not be called when in ALTQ mode */
829 IF_HANDOFF(&ifp->if_snd, m0, ifp);
833 /* Wait for it to propagate through the chip */
836 for (i = 0; i < RL_TIMEOUT; i++) {
837 status = CSR_READ_2(sc, RL_ISR);
838 CSR_WRITE_2(sc, RL_ISR, status);
839 if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
840 (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
845 if (i == RL_TIMEOUT) {
846 device_printf(sc->rl_dev,
847 "diagnostic failed, failed to receive packet in"
854 * The packet should have been dumped into the first
855 * entry in the RX DMA ring. Grab it from there.
858 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
859 sc->rl_ldata.rl_rx_list_map,
860 BUS_DMASYNC_POSTREAD);
861 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
862 sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
863 BUS_DMASYNC_POSTREAD);
864 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
865 sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
867 m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
868 sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
869 eh = mtod(m0, struct ether_header *);
871 cur_rx = &sc->rl_ldata.rl_rx_list[0];
872 total_len = RL_RXBYTES(cur_rx);
873 rxstat = le32toh(cur_rx->rl_cmdstat);
875 if (total_len != ETHER_MIN_LEN) {
876 device_printf(sc->rl_dev,
877 "diagnostic failed, received short packet\n");
882 /* Test that the received packet data matches what we sent. */
884 if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
885 bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
886 ntohs(eh->ether_type) != ETHERTYPE_IP) {
887 device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
888 device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
889 dst, ":", src, ":", ETHERTYPE_IP);
890 device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
891 eh->ether_dhost, ":", eh->ether_shost, ":",
892 ntohs(eh->ether_type));
893 device_printf(sc->rl_dev, "You may have a defective 32-bit "
894 "NIC plugged into a 64-bit PCI slot.\n");
895 device_printf(sc->rl_dev, "Please re-install the NIC in a "
896 "32-bit slot for proper operation.\n");
897 device_printf(sc->rl_dev, "Read the re(4) man page for more "
903 /* Turn interface off, release resources */
906 sc->rl_flags &= ~RL_FLAG_LINK;
907 ifp->if_flags &= ~IFF_PROMISC;
920 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
921 * IDs against our list and return a device name if we find a match.
924 re_probe(device_t dev)
926 const struct rl_type *t;
927 uint16_t devid, vendor;
928 uint16_t revid, sdevid;
931 vendor = pci_get_vendor(dev);
932 devid = pci_get_device(dev);
933 revid = pci_get_revid(dev);
934 sdevid = pci_get_subdevice(dev);
936 if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
937 if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
939 * Only attach to rev. 3 of the Linksys EG1032 adapter.
940 * Rev. 2 is supported by sk(4).
946 if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
948 /* 8139, let rl(4) take care of this device. */
954 for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
955 if (vendor == t->rl_vid && devid == t->rl_did) {
956 device_set_desc(dev, t->rl_name);
957 return (BUS_PROBE_DEFAULT);
965 * Map a single buffer address.
969 re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
976 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
978 *addr = segs->ds_addr;
982 re_allocmem(device_t dev, struct rl_softc *sc)
985 bus_size_t rx_list_size, tx_list_size;
989 rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
990 tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
993 * Allocate the parent bus DMA tag appropriate for PCI.
994 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
995 * register should be set. However some RealTek chips are known
996 * to be buggy on DAC handling, therefore disable DAC by limiting
997 * DMA address space to 32bit. PCIe variants of RealTek chips
998 * may not have the limitation.
1000 lowaddr = BUS_SPACE_MAXADDR;
1001 if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
1002 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1003 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
1004 lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
1005 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1006 NULL, NULL, &sc->rl_parent_tag);
1008 device_printf(dev, "could not allocate parent DMA tag\n");
1013 * Allocate map for TX mbufs.
1015 error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1016 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1017 NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1018 NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1020 device_printf(dev, "could not allocate TX DMA tag\n");
1025 * Allocate map for RX mbufs.
1028 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1029 error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
1030 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1031 MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
1032 &sc->rl_ldata.rl_jrx_mtag);
1035 "could not allocate jumbo RX DMA tag\n");
1039 error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1040 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1041 MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1043 device_printf(dev, "could not allocate RX DMA tag\n");
1048 * Allocate map for TX descriptor list.
1050 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1051 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1052 NULL, tx_list_size, 1, tx_list_size, 0,
1053 NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1055 device_printf(dev, "could not allocate TX DMA ring tag\n");
1059 /* Allocate DMA'able memory for the TX ring */
1061 error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1062 (void **)&sc->rl_ldata.rl_tx_list,
1063 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1064 &sc->rl_ldata.rl_tx_list_map);
1066 device_printf(dev, "could not allocate TX DMA ring\n");
1070 /* Load the map for the TX ring. */
1072 sc->rl_ldata.rl_tx_list_addr = 0;
1073 error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1074 sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1075 tx_list_size, re_dma_map_addr,
1076 &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1077 if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1078 device_printf(dev, "could not load TX DMA ring\n");
1082 /* Create DMA maps for TX buffers */
1084 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1085 error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1086 &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1088 device_printf(dev, "could not create DMA map for TX\n");
1094 * Allocate map for RX descriptor list.
1096 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1097 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1098 NULL, rx_list_size, 1, rx_list_size, 0,
1099 NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1101 device_printf(dev, "could not create RX DMA ring tag\n");
1105 /* Allocate DMA'able memory for the RX ring */
1107 error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1108 (void **)&sc->rl_ldata.rl_rx_list,
1109 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1110 &sc->rl_ldata.rl_rx_list_map);
1112 device_printf(dev, "could not allocate RX DMA ring\n");
1116 /* Load the map for the RX ring. */
1118 sc->rl_ldata.rl_rx_list_addr = 0;
1119 error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1120 sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1121 rx_list_size, re_dma_map_addr,
1122 &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1123 if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1124 device_printf(dev, "could not load RX DMA ring\n");
1128 /* Create DMA maps for RX buffers */
1130 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
1131 error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1132 &sc->rl_ldata.rl_jrx_sparemap);
1135 "could not create spare DMA map for jumbo RX\n");
1138 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1139 error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
1140 &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1143 "could not create DMA map for jumbo RX\n");
1148 error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1149 &sc->rl_ldata.rl_rx_sparemap);
1151 device_printf(dev, "could not create spare DMA map for RX\n");
1154 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1155 error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1156 &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1158 device_printf(dev, "could not create DMA map for RX\n");
1163 /* Create DMA map for statistics. */
1164 error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
1165 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1166 sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
1167 &sc->rl_ldata.rl_stag);
1169 device_printf(dev, "could not create statistics DMA tag\n");
1172 /* Allocate DMA'able memory for statistics. */
1173 error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
1174 (void **)&sc->rl_ldata.rl_stats,
1175 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1176 &sc->rl_ldata.rl_smap);
1179 "could not allocate statistics DMA memory\n");
1182 /* Load the map for statistics. */
1183 sc->rl_ldata.rl_stats_addr = 0;
1184 error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
1185 sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
1186 &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
1187 if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
1188 device_printf(dev, "could not load statistics DMA memory\n");
1196 * Attach the interface. Allocate softc structures, do ifmedia
1197 * setup and ethernet/BPF attach.
1200 re_attach(device_t dev)
1202 u_char eaddr[ETHER_ADDR_LEN];
1203 u_int16_t as[ETHER_ADDR_LEN / 2];
1204 struct rl_softc *sc;
1206 const struct rl_hwrev *hw_rev;
1207 int capmask, error = 0, hwrev, i, msic, msixc,
1210 u_int16_t devid, re_did = 0;
1213 sc = device_get_softc(dev);
1216 mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1218 callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1221 * Map control/status registers.
1223 pci_enable_busmaster(dev);
1225 devid = pci_get_device(dev);
1227 * Prefer memory space register mapping over IO space.
1228 * Because RTL8169SC does not seem to work when memory mapping
1229 * is used always activate io mapping.
1231 if (devid == RT_DEVICEID_8169SC)
1233 if (prefer_iomap == 0) {
1234 sc->rl_res_id = PCIR_BAR(1);
1235 sc->rl_res_type = SYS_RES_MEMORY;
1236 /* RTL8168/8101E seems to use different BARs. */
1237 if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1238 sc->rl_res_id = PCIR_BAR(2);
1240 sc->rl_res_id = PCIR_BAR(0);
1241 sc->rl_res_type = SYS_RES_IOPORT;
1243 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1244 &sc->rl_res_id, RF_ACTIVE);
1245 if (sc->rl_res == NULL && prefer_iomap == 0) {
1246 sc->rl_res_id = PCIR_BAR(0);
1247 sc->rl_res_type = SYS_RES_IOPORT;
1248 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1249 &sc->rl_res_id, RF_ACTIVE);
1251 if (sc->rl_res == NULL) {
1252 device_printf(dev, "couldn't map ports/memory\n");
1257 sc->rl_btag = rman_get_bustag(sc->rl_res);
1258 sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1260 msic = pci_msi_count(dev);
1261 msixc = pci_msix_count(dev);
1262 if (pci_find_cap(dev, PCIY_EXPRESS, ®) == 0) {
1263 sc->rl_flags |= RL_FLAG_PCIE;
1264 sc->rl_expcap = reg;
1267 device_printf(dev, "MSI count : %d\n", msic);
1268 device_printf(dev, "MSI-X count : %d\n", msixc);
1270 if (msix_disable > 0)
1272 if (msi_disable > 0)
1274 /* Prefer MSI-X to MSI. */
1276 msixc = RL_MSI_MESSAGES;
1278 sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1280 if (sc->rl_res_pba == NULL) {
1281 device_printf(sc->rl_dev,
1282 "could not allocate MSI-X PBA resource\n");
1284 if (sc->rl_res_pba != NULL &&
1285 pci_alloc_msix(dev, &msixc) == 0) {
1286 if (msixc == RL_MSI_MESSAGES) {
1287 device_printf(dev, "Using %d MSI-X message\n",
1289 sc->rl_flags |= RL_FLAG_MSIX;
1291 pci_release_msi(dev);
1293 if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
1294 if (sc->rl_res_pba != NULL)
1295 bus_release_resource(dev, SYS_RES_MEMORY, rid,
1297 sc->rl_res_pba = NULL;
1301 /* Prefer MSI to INTx. */
1302 if (msixc == 0 && msic > 0) {
1303 msic = RL_MSI_MESSAGES;
1304 if (pci_alloc_msi(dev, &msic) == 0) {
1305 if (msic == RL_MSI_MESSAGES) {
1306 device_printf(dev, "Using %d MSI message\n",
1308 sc->rl_flags |= RL_FLAG_MSI;
1309 /* Explicitly set MSI enable bit. */
1310 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1311 cfg = CSR_READ_1(sc, RL_CFG2);
1313 CSR_WRITE_1(sc, RL_CFG2, cfg);
1314 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1316 pci_release_msi(dev);
1318 if ((sc->rl_flags & RL_FLAG_MSI) == 0)
1322 /* Allocate interrupt */
1323 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
1325 sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1326 RF_SHAREABLE | RF_ACTIVE);
1327 if (sc->rl_irq[0] == NULL) {
1328 device_printf(dev, "couldn't allocate IRQ resources\n");
1333 for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
1334 sc->rl_irq[i] = bus_alloc_resource_any(dev,
1335 SYS_RES_IRQ, &rid, RF_ACTIVE);
1336 if (sc->rl_irq[i] == NULL) {
1338 "couldn't allocate IRQ resources for "
1339 "message %d\n", rid);
1346 if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
1347 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1348 cfg = CSR_READ_1(sc, RL_CFG2);
1349 if ((cfg & RL_CFG2_MSI) != 0) {
1350 device_printf(dev, "turning off MSI enable bit.\n");
1351 cfg &= ~RL_CFG2_MSI;
1352 CSR_WRITE_1(sc, RL_CFG2, cfg);
1354 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1357 /* Disable ASPM L0S/L1. */
1358 if (sc->rl_expcap != 0) {
1359 cap = pci_read_config(dev, sc->rl_expcap +
1361 if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1362 ctl = pci_read_config(dev, sc->rl_expcap +
1364 if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) {
1365 ctl &= ~PCIEM_LINK_CTL_ASPMC;
1366 pci_write_config(dev, sc->rl_expcap +
1367 PCIER_LINK_CTL, ctl, 2);
1368 device_printf(dev, "ASPM disabled\n");
1371 device_printf(dev, "no ASPM capability\n");
1375 hwrev = CSR_READ_4(sc, RL_TXCFG);
1376 switch (hwrev & 0x70000000) {
1379 device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1380 hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1383 device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1384 sc->rl_macrev = hwrev & 0x00700000;
1385 hwrev &= RL_TXCFG_HWREV;
1388 device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev);
1389 while (hw_rev->rl_desc != NULL) {
1390 if (hw_rev->rl_rev == hwrev) {
1391 sc->rl_type = hw_rev->rl_type;
1392 sc->rl_hwrev = hw_rev;
1397 if (hw_rev->rl_desc == NULL) {
1398 device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1403 switch (hw_rev->rl_rev) {
1404 case RL_HWREV_8139CPLUS:
1405 sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1407 case RL_HWREV_8100E:
1408 case RL_HWREV_8101E:
1409 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1411 case RL_HWREV_8102E:
1412 case RL_HWREV_8102EL:
1413 case RL_HWREV_8102EL_SPIN1:
1414 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1415 RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1418 case RL_HWREV_8103E:
1419 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1420 RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
1421 RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
1423 case RL_HWREV_8401E:
1424 case RL_HWREV_8105E:
1425 case RL_HWREV_8105E_SPIN1:
1426 case RL_HWREV_8106E:
1427 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1428 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1429 RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1432 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1433 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1434 RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1435 RL_FLAG_CMDSTOP_WAIT_TXQ;
1437 case RL_HWREV_8168B_SPIN1:
1438 case RL_HWREV_8168B_SPIN2:
1439 sc->rl_flags |= RL_FLAG_WOLRXENB;
1441 case RL_HWREV_8168B_SPIN3:
1442 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1444 case RL_HWREV_8168C_SPIN2:
1445 sc->rl_flags |= RL_FLAG_MACSLEEP;
1447 case RL_HWREV_8168C:
1448 if (sc->rl_macrev == 0x00200000)
1449 sc->rl_flags |= RL_FLAG_MACSLEEP;
1451 case RL_HWREV_8168CP:
1452 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1453 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1454 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1456 case RL_HWREV_8168D:
1457 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1458 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1459 RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1460 RL_FLAG_WOL_MANLINK;
1462 case RL_HWREV_8168DP:
1463 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1464 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
1465 RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1467 case RL_HWREV_8168E:
1468 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1469 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1470 RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1471 RL_FLAG_WOL_MANLINK;
1473 case RL_HWREV_8168E_VL:
1474 case RL_HWREV_8168F:
1475 sc->rl_flags |= RL_FLAG_EARLYOFF;
1478 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1479 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1480 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1481 RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1483 case RL_HWREV_8168EP:
1484 case RL_HWREV_8168G:
1485 case RL_HWREV_8411B:
1486 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1487 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1488 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1489 RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK |
1492 case RL_HWREV_8168GU:
1493 case RL_HWREV_8168H:
1494 if (pci_get_device(dev) == RT_DEVICEID_8101E) {
1495 /* RTL8106E(US), RTL8107E */
1496 sc->rl_flags |= RL_FLAG_FASTETHER;
1498 sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1500 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1501 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1502 RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ |
1505 case RL_HWREV_8169_8110SB:
1506 case RL_HWREV_8169_8110SBL:
1507 case RL_HWREV_8169_8110SC:
1508 case RL_HWREV_8169_8110SCE:
1509 sc->rl_flags |= RL_FLAG_PHYWAKE;
1512 case RL_HWREV_8169S:
1513 case RL_HWREV_8110S:
1514 sc->rl_flags |= RL_FLAG_MACRESET;
1520 if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1521 sc->rl_cfg0 = RL_8139_CFG0;
1522 sc->rl_cfg1 = RL_8139_CFG1;
1524 sc->rl_cfg3 = RL_8139_CFG3;
1525 sc->rl_cfg4 = RL_8139_CFG4;
1526 sc->rl_cfg5 = RL_8139_CFG5;
1528 sc->rl_cfg0 = RL_CFG0;
1529 sc->rl_cfg1 = RL_CFG1;
1530 sc->rl_cfg2 = RL_CFG2;
1531 sc->rl_cfg3 = RL_CFG3;
1532 sc->rl_cfg4 = RL_CFG4;
1533 sc->rl_cfg5 = RL_CFG5;
1536 /* Reset the adapter. */
1542 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1543 cfg = CSR_READ_1(sc, sc->rl_cfg1);
1545 CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1546 cfg = CSR_READ_1(sc, sc->rl_cfg5);
1547 cfg &= RL_CFG5_PME_STS;
1548 CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1549 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1551 if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1553 * XXX Should have a better way to extract station
1554 * address from EEPROM.
1556 for (i = 0; i < ETHER_ADDR_LEN; i++)
1557 eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1559 sc->rl_eewidth = RL_9356_ADDR_LEN;
1560 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1561 if (re_did != 0x8129)
1562 sc->rl_eewidth = RL_9346_ADDR_LEN;
1565 * Get station address from the EEPROM.
1567 re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1568 for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1569 as[i] = le16toh(as[i]);
1570 bcopy(as, eaddr, ETHER_ADDR_LEN);
1573 if (sc->rl_type == RL_8169) {
1574 /* Set RX length mask and number of descriptors. */
1575 sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1576 sc->rl_txstart = RL_GTXSTART;
1577 sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1578 sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1580 /* Set RX length mask and number of descriptors. */
1581 sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1582 sc->rl_txstart = RL_TXSTART;
1583 sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1584 sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1587 error = re_allocmem(dev, sc);
1592 ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1594 device_printf(dev, "can not if_alloc()\n");
1599 /* Take controller out of deep sleep mode. */
1600 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
1601 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
1602 CSR_WRITE_1(sc, RL_GPIO,
1603 CSR_READ_1(sc, RL_GPIO) | 0x01);
1605 CSR_WRITE_1(sc, RL_GPIO,
1606 CSR_READ_1(sc, RL_GPIO) & ~0x01);
1609 /* Take PHY out of power down mode. */
1610 if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1611 CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1612 if (hw_rev->rl_rev == RL_HWREV_8401E)
1613 CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
1615 if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1616 re_gmii_writereg(dev, 1, 0x1f, 0);
1617 re_gmii_writereg(dev, 1, 0x0e, 0);
1621 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1622 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1623 ifp->if_ioctl = re_ioctl;
1624 ifp->if_start = re_start;
1626 * RTL8168/8111C generates wrong IP checksummed frame if the
1627 * packet has IP options so disable TX checksum offloading.
1629 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
1630 sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 ||
1631 sc->rl_hwrev->rl_rev == RL_HWREV_8168CP) {
1632 ifp->if_hwassist = 0;
1633 ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_TSO4;
1635 ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1636 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1638 ifp->if_hwassist |= CSUM_TSO;
1639 ifp->if_capenable = ifp->if_capabilities;
1640 ifp->if_init = re_init;
1641 IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
1642 ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
1643 IFQ_SET_READY(&ifp->if_snd);
1645 TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1647 #define RE_PHYAD_INTERNAL 0
1650 phy = RE_PHYAD_INTERNAL;
1651 if (sc->rl_type == RL_8169)
1653 capmask = BMSR_DEFCAPMASK;
1654 if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
1655 capmask &= ~BMSR_EXTSTAT;
1656 error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
1657 re_ifmedia_sts, capmask, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1659 device_printf(dev, "attaching PHYs failed\n");
1664 * Call MI attach routine.
1666 ether_ifattach(ifp, eaddr);
1668 /* VLAN capability setup */
1669 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1670 if (ifp->if_capabilities & IFCAP_HWCSUM)
1671 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
1672 /* Enable WOL if PM is supported. */
1673 if (pci_find_cap(sc->rl_dev, PCIY_PMG, ®) == 0)
1674 ifp->if_capabilities |= IFCAP_WOL;
1675 ifp->if_capenable = ifp->if_capabilities;
1676 ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1678 * Don't enable TSO by default. It is known to generate
1679 * corrupted TCP segments(bad TCP options) under certain
1682 ifp->if_hwassist &= ~CSUM_TSO;
1683 ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1684 #ifdef DEVICE_POLLING
1685 ifp->if_capabilities |= IFCAP_POLLING;
1688 * Tell the upper layer(s) we support long frames.
1689 * Must appear after the call to ether_ifattach() because
1690 * ether_ifattach() sets ifi_hdrlen to the default value.
1692 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1695 re_netmap_attach(sc);
1696 #endif /* DEV_NETMAP */
1700 * Perform hardware diagnostic on the original RTL8169.
1701 * Some 32-bit cards were incorrectly wired and would
1702 * malfunction if plugged into a 64-bit slot.
1704 if (hwrev == RL_HWREV_8169) {
1705 error = re_diag(sc);
1708 "attach aborted due to hardware diag failure\n");
1709 ether_ifdetach(ifp);
1715 #ifdef RE_TX_MODERATION
1718 /* Hook interrupt last to avoid having to lock softc */
1719 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1721 error = bus_setup_intr(dev, sc->rl_irq[0],
1722 INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1723 &sc->rl_intrhand[0]);
1725 error = bus_setup_intr(dev, sc->rl_irq[0],
1726 INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
1727 &sc->rl_intrhand[0]);
1730 device_printf(dev, "couldn't set up irq\n");
1731 ether_ifdetach(ifp);
1742 * Shutdown hardware and free up resources. This can be called any
1743 * time after the mutex has been initialized. It is called in both
1744 * the error case in attach and the normal detach case so it needs
1745 * to be careful about only freeing resources that have actually been
1749 re_detach(device_t dev)
1751 struct rl_softc *sc;
1755 sc = device_get_softc(dev);
1757 KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
1759 /* These should only be active if attach succeeded */
1760 if (device_is_attached(dev)) {
1761 #ifdef DEVICE_POLLING
1762 if (ifp->if_capenable & IFCAP_POLLING)
1763 ether_poll_deregister(ifp);
1771 callout_drain(&sc->rl_stat_callout);
1772 taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1774 * Force off the IFF_UP flag here, in case someone
1775 * still had a BPF descriptor attached to this
1776 * interface. If they do, ether_ifdetach() will cause
1777 * the BPF code to try and clear the promisc mode
1778 * flag, which will bubble down to re_ioctl(),
1779 * which will try to call re_init() again. This will
1780 * turn the NIC back on and restart the MII ticker,
1781 * which will panic the system when the kernel tries
1782 * to invoke the re_tick() function that isn't there
1785 ifp->if_flags &= ~IFF_UP;
1786 ether_ifdetach(ifp);
1789 device_delete_child(dev, sc->rl_miibus);
1790 bus_generic_detach(dev);
1793 * The rest is resource deallocation, so we should already be
1797 if (sc->rl_intrhand[0] != NULL) {
1798 bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1799 sc->rl_intrhand[0] = NULL;
1804 #endif /* DEV_NETMAP */
1807 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1811 if (sc->rl_irq[0] != NULL) {
1812 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
1813 sc->rl_irq[0] = NULL;
1815 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
1816 pci_release_msi(dev);
1817 if (sc->rl_res_pba) {
1819 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
1822 bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1825 /* Unload and free the RX DMA ring memory and map */
1827 if (sc->rl_ldata.rl_rx_list_tag) {
1828 if (sc->rl_ldata.rl_rx_list_map)
1829 bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1830 sc->rl_ldata.rl_rx_list_map);
1831 if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1832 bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1833 sc->rl_ldata.rl_rx_list,
1834 sc->rl_ldata.rl_rx_list_map);
1835 bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1838 /* Unload and free the TX DMA ring memory and map */
1840 if (sc->rl_ldata.rl_tx_list_tag) {
1841 if (sc->rl_ldata.rl_tx_list_map)
1842 bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1843 sc->rl_ldata.rl_tx_list_map);
1844 if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1845 bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1846 sc->rl_ldata.rl_tx_list,
1847 sc->rl_ldata.rl_tx_list_map);
1848 bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1851 /* Destroy all the RX and TX buffer maps */
1853 if (sc->rl_ldata.rl_tx_mtag) {
1854 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1855 if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1856 bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1857 sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1859 bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1861 if (sc->rl_ldata.rl_rx_mtag) {
1862 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1863 if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1864 bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1865 sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1867 if (sc->rl_ldata.rl_rx_sparemap)
1868 bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1869 sc->rl_ldata.rl_rx_sparemap);
1870 bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1872 if (sc->rl_ldata.rl_jrx_mtag) {
1873 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1874 if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
1875 bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1876 sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
1878 if (sc->rl_ldata.rl_jrx_sparemap)
1879 bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
1880 sc->rl_ldata.rl_jrx_sparemap);
1881 bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
1883 /* Unload and free the stats buffer and map */
1885 if (sc->rl_ldata.rl_stag) {
1886 if (sc->rl_ldata.rl_smap)
1887 bus_dmamap_unload(sc->rl_ldata.rl_stag,
1888 sc->rl_ldata.rl_smap);
1889 if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
1890 bus_dmamem_free(sc->rl_ldata.rl_stag,
1891 sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1892 bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1895 if (sc->rl_parent_tag)
1896 bus_dma_tag_destroy(sc->rl_parent_tag);
1898 mtx_destroy(&sc->rl_mtx);
1903 static __inline void
1904 re_discard_rxbuf(struct rl_softc *sc, int idx)
1906 struct rl_desc *desc;
1907 struct rl_rxdesc *rxd;
1910 if (sc->rl_ifp->if_mtu > RL_MTU &&
1911 (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
1912 rxd = &sc->rl_ldata.rl_jrx_desc[idx];
1914 rxd = &sc->rl_ldata.rl_rx_desc[idx];
1915 desc = &sc->rl_ldata.rl_rx_list[idx];
1916 desc->rl_vlanctl = 0;
1917 cmdstat = rxd->rx_size;
1918 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1919 cmdstat |= RL_RDESC_CMD_EOR;
1920 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1924 re_newbuf(struct rl_softc *sc, int idx)
1927 struct rl_rxdesc *rxd;
1928 bus_dma_segment_t segs[1];
1930 struct rl_desc *desc;
1934 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1938 m->m_len = m->m_pkthdr.len = MCLBYTES;
1941 * This is part of an evil trick to deal with non-x86 platforms.
1942 * The RealTek chip requires RX buffers to be aligned on 64-bit
1943 * boundaries, but that will hose non-x86 machines. To get around
1944 * this, we leave some empty space at the start of each buffer
1945 * and for non-x86 hosts, we copy the buffer back six bytes
1946 * to achieve word alignment. This is slightly more efficient
1947 * than allocating a new buffer, copying the contents, and
1948 * discarding the old buffer.
1950 m_adj(m, RE_ETHER_ALIGN);
1952 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1953 sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1958 KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1960 rxd = &sc->rl_ldata.rl_rx_desc[idx];
1961 if (rxd->rx_m != NULL) {
1962 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1963 BUS_DMASYNC_POSTREAD);
1964 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1968 map = rxd->rx_dmamap;
1969 rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1970 rxd->rx_size = segs[0].ds_len;
1971 sc->rl_ldata.rl_rx_sparemap = map;
1972 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1973 BUS_DMASYNC_PREREAD);
1975 desc = &sc->rl_ldata.rl_rx_list[idx];
1976 desc->rl_vlanctl = 0;
1977 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1978 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1979 cmdstat = segs[0].ds_len;
1980 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1981 cmdstat |= RL_RDESC_CMD_EOR;
1982 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1988 re_jumbo_newbuf(struct rl_softc *sc, int idx)
1991 struct rl_rxdesc *rxd;
1992 bus_dma_segment_t segs[1];
1994 struct rl_desc *desc;
1998 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
2001 m->m_len = m->m_pkthdr.len = MJUM9BYTES;
2003 m_adj(m, RE_ETHER_ALIGN);
2005 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
2006 sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
2011 KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
2013 rxd = &sc->rl_ldata.rl_jrx_desc[idx];
2014 if (rxd->rx_m != NULL) {
2015 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
2016 BUS_DMASYNC_POSTREAD);
2017 bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
2021 map = rxd->rx_dmamap;
2022 rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
2023 rxd->rx_size = segs[0].ds_len;
2024 sc->rl_ldata.rl_jrx_sparemap = map;
2025 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
2026 BUS_DMASYNC_PREREAD);
2028 desc = &sc->rl_ldata.rl_rx_list[idx];
2029 desc->rl_vlanctl = 0;
2030 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
2031 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
2032 cmdstat = segs[0].ds_len;
2033 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
2034 cmdstat |= RL_RDESC_CMD_EOR;
2035 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
2041 static __inline void
2042 re_fixup_rx(struct mbuf *m)
2045 uint16_t *src, *dst;
2047 src = mtod(m, uint16_t *);
2048 dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
2050 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2053 m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
2058 re_tx_list_init(struct rl_softc *sc)
2060 struct rl_desc *desc;
2065 bzero(sc->rl_ldata.rl_tx_list,
2066 sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2067 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2068 sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2070 re_netmap_tx_init(sc);
2071 #endif /* DEV_NETMAP */
2073 desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2074 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2076 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2077 sc->rl_ldata.rl_tx_list_map,
2078 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2080 sc->rl_ldata.rl_tx_prodidx = 0;
2081 sc->rl_ldata.rl_tx_considx = 0;
2082 sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2088 re_rx_list_init(struct rl_softc *sc)
2092 bzero(sc->rl_ldata.rl_rx_list,
2093 sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2094 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2095 sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2096 if ((error = re_newbuf(sc, i)) != 0)
2100 re_netmap_rx_init(sc);
2101 #endif /* DEV_NETMAP */
2103 /* Flush the RX descriptors */
2105 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2106 sc->rl_ldata.rl_rx_list_map,
2107 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2109 sc->rl_ldata.rl_rx_prodidx = 0;
2110 sc->rl_head = sc->rl_tail = NULL;
2111 sc->rl_int_rx_act = 0;
2117 re_jrx_list_init(struct rl_softc *sc)
2121 bzero(sc->rl_ldata.rl_rx_list,
2122 sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2123 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2124 sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
2125 if ((error = re_jumbo_newbuf(sc, i)) != 0)
2129 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2130 sc->rl_ldata.rl_rx_list_map,
2131 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2133 sc->rl_ldata.rl_rx_prodidx = 0;
2134 sc->rl_head = sc->rl_tail = NULL;
2135 sc->rl_int_rx_act = 0;
2141 * RX handler for C+ and 8169. For the gigE chips, we support
2142 * the reception of jumbo frames that have been fragmented
2143 * across multiple 2K mbuf cluster buffers.
2146 re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2150 int i, rxerr, total_len;
2151 struct rl_desc *cur_rx;
2152 u_int32_t rxstat, rxvlan;
2153 int jumbo, maxpkt = 16, rx_npkts = 0;
2159 if (netmap_rx_irq(ifp, 0, &rx_npkts))
2161 #endif /* DEV_NETMAP */
2162 if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
2167 /* Invalidate the descriptor memory */
2169 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2170 sc->rl_ldata.rl_rx_list_map,
2171 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2173 for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2174 i = RL_RX_DESC_NXT(sc, i)) {
2175 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2177 cur_rx = &sc->rl_ldata.rl_rx_list[i];
2178 rxstat = le32toh(cur_rx->rl_cmdstat);
2179 if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2181 total_len = rxstat & sc->rl_rxlenmask;
2182 rxvlan = le32toh(cur_rx->rl_vlanctl);
2184 m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
2186 m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2188 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
2189 (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
2190 (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
2192 * RTL8168C or later controllers do not
2193 * support multi-fragment packet.
2195 re_discard_rxbuf(sc, i);
2197 } else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2198 if (re_newbuf(sc, i) != 0) {
2200 * If this is part of a multi-fragment packet,
2201 * discard all the pieces.
2203 if (sc->rl_head != NULL) {
2204 m_freem(sc->rl_head);
2205 sc->rl_head = sc->rl_tail = NULL;
2207 re_discard_rxbuf(sc, i);
2210 m->m_len = RE_RX_DESC_BUFLEN;
2211 if (sc->rl_head == NULL)
2212 sc->rl_head = sc->rl_tail = m;
2214 m->m_flags &= ~M_PKTHDR;
2215 sc->rl_tail->m_next = m;
2222 * NOTE: for the 8139C+, the frame length field
2223 * is always 12 bits in size, but for the gigE chips,
2224 * it is 13 bits (since the max RX frame length is 16K).
2225 * Unfortunately, all 32 bits in the status word
2226 * were already used, so to make room for the extra
2227 * length bit, RealTek took out the 'frame alignment
2228 * error' bit and shifted the other status bits
2229 * over one slot. The OWN, EOR, FS and LS bits are
2230 * still in the same places. We have already extracted
2231 * the frame length and checked the OWN bit, so rather
2232 * than using an alternate bit mapping, we shift the
2233 * status bits one space to the right so we can evaluate
2234 * them using the 8169 status as though it was in the
2235 * same format as that of the 8139C+.
2237 if (sc->rl_type == RL_8169)
2241 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
2242 * set, but if CRC is clear, it will still be a valid frame.
2244 if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
2246 if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
2248 (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
2253 * If this is part of a multi-fragment packet,
2254 * discard all the pieces.
2256 if (sc->rl_head != NULL) {
2257 m_freem(sc->rl_head);
2258 sc->rl_head = sc->rl_tail = NULL;
2260 re_discard_rxbuf(sc, i);
2266 * If allocating a replacement mbuf fails,
2267 * reload the current one.
2270 rxerr = re_jumbo_newbuf(sc, i);
2272 rxerr = re_newbuf(sc, i);
2275 if (sc->rl_head != NULL) {
2276 m_freem(sc->rl_head);
2277 sc->rl_head = sc->rl_tail = NULL;
2279 re_discard_rxbuf(sc, i);
2283 if (sc->rl_head != NULL) {
2285 m->m_len = total_len;
2287 m->m_len = total_len % RE_RX_DESC_BUFLEN;
2289 m->m_len = RE_RX_DESC_BUFLEN;
2292 * Special case: if there's 4 bytes or less
2293 * in this buffer, the mbuf can be discarded:
2294 * the last 4 bytes is the CRC, which we don't
2295 * care about anyway.
2297 if (m->m_len <= ETHER_CRC_LEN) {
2298 sc->rl_tail->m_len -=
2299 (ETHER_CRC_LEN - m->m_len);
2302 m->m_len -= ETHER_CRC_LEN;
2303 m->m_flags &= ~M_PKTHDR;
2304 sc->rl_tail->m_next = m;
2307 sc->rl_head = sc->rl_tail = NULL;
2308 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2310 m->m_pkthdr.len = m->m_len =
2311 (total_len - ETHER_CRC_LEN);
2317 m->m_pkthdr.rcvif = ifp;
2319 /* Do RX checksumming if enabled */
2321 if (ifp->if_capenable & IFCAP_RXCSUM) {
2322 if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2323 /* Check IP header checksum */
2324 if (rxstat & RL_RDESC_STAT_PROTOID)
2325 m->m_pkthdr.csum_flags |=
2327 if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2328 m->m_pkthdr.csum_flags |=
2331 /* Check TCP/UDP checksum */
2332 if ((RL_TCPPKT(rxstat) &&
2333 !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2334 (RL_UDPPKT(rxstat) &&
2335 !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2336 m->m_pkthdr.csum_flags |=
2337 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2338 m->m_pkthdr.csum_data = 0xffff;
2342 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2344 if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2345 (rxvlan & RL_RDESC_IPV4))
2346 m->m_pkthdr.csum_flags |=
2348 if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2349 (rxvlan & RL_RDESC_IPV4))
2350 m->m_pkthdr.csum_flags |=
2352 if (((rxstat & RL_RDESC_STAT_TCP) &&
2353 !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2354 ((rxstat & RL_RDESC_STAT_UDP) &&
2355 !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2356 m->m_pkthdr.csum_flags |=
2357 CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2358 m->m_pkthdr.csum_data = 0xffff;
2363 if (rxvlan & RL_RDESC_VLANCTL_TAG) {
2364 m->m_pkthdr.ether_vtag =
2365 bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
2366 m->m_flags |= M_VLANTAG;
2369 (*ifp->if_input)(ifp, m);
2374 /* Flush the RX DMA ring */
2376 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2377 sc->rl_ldata.rl_rx_list_map,
2378 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2380 sc->rl_ldata.rl_rx_prodidx = i;
2382 if (rx_npktsp != NULL)
2383 *rx_npktsp = rx_npkts;
2391 re_txeof(struct rl_softc *sc)
2394 struct rl_txdesc *txd;
2398 cons = sc->rl_ldata.rl_tx_considx;
2399 if (cons == sc->rl_ldata.rl_tx_prodidx)
2404 if (netmap_tx_irq(ifp, 0))
2406 #endif /* DEV_NETMAP */
2407 /* Invalidate the TX descriptor list */
2408 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2409 sc->rl_ldata.rl_tx_list_map,
2410 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2412 for (; cons != sc->rl_ldata.rl_tx_prodidx;
2413 cons = RL_TX_DESC_NXT(sc, cons)) {
2414 txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2415 if (txstat & RL_TDESC_STAT_OWN)
2418 * We only stash mbufs in the last descriptor
2419 * in a fragment chain, which also happens to
2420 * be the only place where the TX status bits
2423 if (txstat & RL_TDESC_CMD_EOF) {
2424 txd = &sc->rl_ldata.rl_tx_desc[cons];
2425 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2426 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2427 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2429 KASSERT(txd->tx_m != NULL,
2430 ("%s: freeing NULL mbufs!", __func__));
2433 if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2434 RL_TDESC_STAT_COLCNT))
2435 ifp->if_collisions++;
2436 if (txstat & RL_TDESC_STAT_TXERRSUM)
2441 sc->rl_ldata.rl_tx_free++;
2442 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2444 sc->rl_ldata.rl_tx_considx = cons;
2446 /* No changes made to the TX ring, so no flush needed */
2448 if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2449 #ifdef RE_TX_MODERATION
2451 * If not all descriptors have been reaped yet, reload
2452 * the timer so that we will eventually get another
2453 * interrupt that will cause us to re-enter this routine.
2454 * This is done in case the transmitter has gone idle.
2456 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2459 sc->rl_watchdog_timer = 0;
2465 struct rl_softc *sc;
2466 struct mii_data *mii;
2472 mii = device_get_softc(sc->rl_miibus);
2474 if ((sc->rl_flags & RL_FLAG_LINK) == 0)
2475 re_miibus_statchg(sc->rl_dev);
2477 * Reclaim transmitted frames here. Technically it is not
2478 * necessary to do here but it ensures periodic reclamation
2479 * regardless of Tx completion interrupt which seems to be
2480 * lost on PCIe based controllers under certain situations.
2484 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2487 #ifdef DEVICE_POLLING
2489 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2491 struct rl_softc *sc = ifp->if_softc;
2495 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2496 rx_npkts = re_poll_locked(ifp, cmd, count);
2502 re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
2504 struct rl_softc *sc = ifp->if_softc;
2509 sc->rxcycles = count;
2510 re_rxeof(sc, &rx_npkts);
2513 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2514 re_start_locked(ifp);
2516 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2519 status = CSR_READ_2(sc, RL_ISR);
2520 if (status == 0xffff)
2523 CSR_WRITE_2(sc, RL_ISR, status);
2524 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2525 (sc->rl_flags & RL_FLAG_PCIE))
2526 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2529 * XXX check behaviour on receiver stalls.
2532 if (status & RL_ISR_SYSTEM_ERR) {
2533 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2539 #endif /* DEVICE_POLLING */
2544 struct rl_softc *sc;
2549 status = CSR_READ_2(sc, RL_ISR);
2550 if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2551 return (FILTER_STRAY);
2552 CSR_WRITE_2(sc, RL_IMR, 0);
2554 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2556 return (FILTER_HANDLED);
2560 re_int_task(void *arg, int npending)
2562 struct rl_softc *sc;
2572 status = CSR_READ_2(sc, RL_ISR);
2573 CSR_WRITE_2(sc, RL_ISR, status);
2575 if (sc->suspended ||
2576 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2581 #ifdef DEVICE_POLLING
2582 if (ifp->if_capenable & IFCAP_POLLING) {
2588 if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2589 rval = re_rxeof(sc, NULL);
2592 * Some chips will ignore a second TX request issued
2593 * while an existing transmission is in progress. If
2594 * the transmitter goes idle but there are still
2595 * packets waiting to be sent, we need to restart the
2596 * channel here to flush them out. This only seems to
2597 * be required with the PCIe devices.
2599 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2600 (sc->rl_flags & RL_FLAG_PCIE))
2601 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2603 #ifdef RE_TX_MODERATION
2604 RL_ISR_TIMEOUT_EXPIRED|
2608 RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2611 if (status & RL_ISR_SYSTEM_ERR) {
2612 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2616 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2617 re_start_locked(ifp);
2621 if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2622 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2626 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2630 re_intr_msi(void *xsc)
2632 struct rl_softc *sc;
2634 uint16_t intrs, status;
2640 #ifdef DEVICE_POLLING
2641 if (ifp->if_capenable & IFCAP_POLLING) {
2646 /* Disable interrupts. */
2647 CSR_WRITE_2(sc, RL_IMR, 0);
2648 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2653 intrs = RL_INTRS_CPLUS;
2654 status = CSR_READ_2(sc, RL_ISR);
2655 CSR_WRITE_2(sc, RL_ISR, status);
2656 if (sc->rl_int_rx_act > 0) {
2657 intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2659 status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2663 if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2664 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2666 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2667 if (sc->rl_int_rx_mod != 0 &&
2668 (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2669 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2670 /* Rearm one-shot timer. */
2671 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2672 intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2673 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2674 sc->rl_int_rx_act = 1;
2676 intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2677 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2678 sc->rl_int_rx_act = 0;
2684 * Some chips will ignore a second TX request issued
2685 * while an existing transmission is in progress. If
2686 * the transmitter goes idle but there are still
2687 * packets waiting to be sent, we need to restart the
2688 * channel here to flush them out. This only seems to
2689 * be required with the PCIe devices.
2691 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2692 (sc->rl_flags & RL_FLAG_PCIE))
2693 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2694 if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2697 if (status & RL_ISR_SYSTEM_ERR) {
2698 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2702 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2703 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2704 re_start_locked(ifp);
2705 CSR_WRITE_2(sc, RL_IMR, intrs);
2711 re_encap(struct rl_softc *sc, struct mbuf **m_head)
2713 struct rl_txdesc *txd, *txd_last;
2714 bus_dma_segment_t segs[RL_NTXSEGS];
2717 struct rl_desc *desc;
2719 int i, error, ei, si;
2721 uint32_t cmdstat, csum_flags, vlanctl;
2724 M_ASSERTPKTHDR((*m_head));
2727 * With some of the RealTek chips, using the checksum offload
2728 * support in conjunction with the autopadding feature results
2729 * in the transmission of corrupt frames. For example, if we
2730 * need to send a really small IP fragment that's less than 60
2731 * bytes in size, and IP header checksumming is enabled, the
2732 * resulting ethernet frame that appears on the wire will
2733 * have garbled payload. To work around this, if TX IP checksum
2734 * offload is enabled, we always manually pad short frames out
2735 * to the minimum ethernet frame size.
2737 if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2738 (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
2739 ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2740 padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2741 if (M_WRITABLE(*m_head) == 0) {
2742 /* Get a writable copy. */
2743 m_new = m_dup(*m_head, M_NOWAIT);
2745 if (m_new == NULL) {
2751 if ((*m_head)->m_next != NULL ||
2752 M_TRAILINGSPACE(*m_head) < padlen) {
2753 m_new = m_defrag(*m_head, M_NOWAIT);
2754 if (m_new == NULL) {
2763 * Manually pad short frames, and zero the pad space
2764 * to avoid leaking data.
2766 bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2767 m_new->m_pkthdr.len += padlen;
2768 m_new->m_len = m_new->m_pkthdr.len;
2772 prod = sc->rl_ldata.rl_tx_prodidx;
2773 txd = &sc->rl_ldata.rl_tx_desc[prod];
2774 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2775 *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2776 if (error == EFBIG) {
2777 m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2778 if (m_new == NULL) {
2784 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2785 txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2791 } else if (error != 0)
2799 /* Check for number of available descriptors. */
2800 if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2801 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2805 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2806 BUS_DMASYNC_PREWRITE);
2809 * Set up checksum offload. Note: checksum offload bits must
2810 * appear in all descriptors of a multi-descriptor transmit
2811 * attempt. This is according to testing done with an 8169
2812 * chip. This is a requirement.
2816 if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2817 if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2818 csum_flags |= RL_TDESC_CMD_LGSEND;
2819 vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2820 RL_TDESC_CMD_MSSVALV2_SHIFT);
2822 csum_flags |= RL_TDESC_CMD_LGSEND |
2823 ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2824 RL_TDESC_CMD_MSSVAL_SHIFT);
2828 * Unconditionally enable IP checksum if TCP or UDP
2829 * checksum is required. Otherwise, TCP/UDP checksum
2830 * doesn't make effects.
2832 if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2833 if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2834 csum_flags |= RL_TDESC_CMD_IPCSUM;
2835 if (((*m_head)->m_pkthdr.csum_flags &
2837 csum_flags |= RL_TDESC_CMD_TCPCSUM;
2838 if (((*m_head)->m_pkthdr.csum_flags &
2840 csum_flags |= RL_TDESC_CMD_UDPCSUM;
2842 vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2843 if (((*m_head)->m_pkthdr.csum_flags &
2845 vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2846 if (((*m_head)->m_pkthdr.csum_flags &
2848 vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2854 * Set up hardware VLAN tagging. Note: vlan tag info must
2855 * appear in all descriptors of a multi-descriptor
2856 * transmission attempt.
2858 if ((*m_head)->m_flags & M_VLANTAG)
2859 vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2860 RL_TDESC_VLANCTL_TAG;
2863 for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2864 desc = &sc->rl_ldata.rl_tx_list[prod];
2865 desc->rl_vlanctl = htole32(vlanctl);
2866 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2867 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2868 cmdstat = segs[i].ds_len;
2870 cmdstat |= RL_TDESC_CMD_OWN;
2871 if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2872 cmdstat |= RL_TDESC_CMD_EOR;
2873 desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2874 sc->rl_ldata.rl_tx_free--;
2876 /* Update producer index. */
2877 sc->rl_ldata.rl_tx_prodidx = prod;
2879 /* Set EOF on the last descriptor. */
2880 ei = RL_TX_DESC_PRV(sc, prod);
2881 desc = &sc->rl_ldata.rl_tx_list[ei];
2882 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2884 desc = &sc->rl_ldata.rl_tx_list[si];
2885 /* Set SOF and transfer ownership of packet to the chip. */
2886 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2889 * Insure that the map for this transmission
2890 * is placed at the array index of the last descriptor
2891 * in this chain. (Swap last and first dmamaps.)
2893 txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2894 map = txd->tx_dmamap;
2895 txd->tx_dmamap = txd_last->tx_dmamap;
2896 txd_last->tx_dmamap = map;
2897 txd_last->tx_m = *m_head;
2903 re_start(struct ifnet *ifp)
2905 struct rl_softc *sc;
2909 re_start_locked(ifp);
2914 * Main transmit routine for C+ and gigE NICs.
2917 re_start_locked(struct ifnet *ifp)
2919 struct rl_softc *sc;
2920 struct mbuf *m_head;
2926 /* XXX is this necessary ? */
2927 if (ifp->if_capenable & IFCAP_NETMAP) {
2928 struct netmap_kring *kring = &NA(ifp)->tx_rings[0];
2929 if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2930 /* kick the tx unit */
2931 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2932 #ifdef RE_TX_MODERATION
2933 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2935 sc->rl_watchdog_timer = 5;
2939 #endif /* DEV_NETMAP */
2941 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2942 IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2945 for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2946 sc->rl_ldata.rl_tx_free > 1;) {
2947 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2951 if (re_encap(sc, &m_head) != 0) {
2954 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2955 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2960 * If there's a BPF listener, bounce a copy of this frame
2963 ETHER_BPF_MTAP(ifp, m_head);
2969 #ifdef RE_TX_MODERATION
2970 if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2971 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2976 /* Flush the TX descriptors */
2978 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2979 sc->rl_ldata.rl_tx_list_map,
2980 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2982 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2984 #ifdef RE_TX_MODERATION
2986 * Use the countdown timer for interrupt moderation.
2987 * 'TX done' interrupts are disabled. Instead, we reset the
2988 * countdown timer, which will begin counting until it hits
2989 * the value in the TIMERINT register, and then trigger an
2990 * interrupt. Each time we write to the TIMERCNT register,
2991 * the timer count is reset to 0.
2993 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2997 * Set a timeout in case the chip goes out to lunch.
2999 sc->rl_watchdog_timer = 5;
3003 re_set_jumbo(struct rl_softc *sc, int jumbo)
3006 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
3007 pci_set_max_read_req(sc->rl_dev, 4096);
3011 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3013 CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
3015 switch (sc->rl_hwrev->rl_rev) {
3016 case RL_HWREV_8168DP:
3018 case RL_HWREV_8168E:
3019 CSR_WRITE_1(sc, sc->rl_cfg4,
3020 CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
3023 CSR_WRITE_1(sc, sc->rl_cfg4,
3024 CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
3027 CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
3028 ~RL_CFG3_JUMBO_EN0);
3029 switch (sc->rl_hwrev->rl_rev) {
3030 case RL_HWREV_8168DP:
3032 case RL_HWREV_8168E:
3033 CSR_WRITE_1(sc, sc->rl_cfg4,
3034 CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
3037 CSR_WRITE_1(sc, sc->rl_cfg4,
3038 CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
3041 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3043 switch (sc->rl_hwrev->rl_rev) {
3044 case RL_HWREV_8168DP:
3045 pci_set_max_read_req(sc->rl_dev, 4096);
3049 pci_set_max_read_req(sc->rl_dev, 512);
3051 pci_set_max_read_req(sc->rl_dev, 4096);
3058 struct rl_softc *sc = xsc;
3066 re_init_locked(struct rl_softc *sc)
3068 struct ifnet *ifp = sc->rl_ifp;
3069 struct mii_data *mii;
3073 uint32_t align_dummy;
3074 u_char eaddr[ETHER_ADDR_LEN];
3079 mii = device_get_softc(sc->rl_miibus);
3081 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3085 * Cancel pending I/O and free all RX/TX buffers.
3089 /* Put controller into known state. */
3093 * For C+ mode, initialize the RX descriptors and mbufs.
3095 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3096 if (ifp->if_mtu > RL_MTU) {
3097 if (re_jrx_list_init(sc) != 0) {
3098 device_printf(sc->rl_dev,
3099 "no memory for jumbo RX buffers\n");
3103 /* Disable checksum offloading for jumbo frames. */
3104 ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
3105 ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
3107 if (re_rx_list_init(sc) != 0) {
3108 device_printf(sc->rl_dev,
3109 "no memory for RX buffers\n");
3114 re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
3116 if (re_rx_list_init(sc) != 0) {
3117 device_printf(sc->rl_dev, "no memory for RX buffers\n");
3121 if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3122 pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
3123 if (ifp->if_mtu > RL_MTU)
3124 pci_set_max_read_req(sc->rl_dev, 512);
3126 pci_set_max_read_req(sc->rl_dev, 4096);
3129 re_tx_list_init(sc);
3132 * Enable C+ RX and TX mode, as well as VLAN stripping and
3133 * RX checksum offload. We must configure the C+ register
3134 * before all others.
3136 cfg = RL_CPLUSCMD_PCI_MRW;
3137 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
3138 cfg |= RL_CPLUSCMD_RXCSUM_ENB;
3139 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
3140 cfg |= RL_CPLUSCMD_VLANSTRIP;
3141 if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3142 cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3146 cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3147 CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
3148 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
3149 sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3151 if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3153 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3155 CSR_WRITE_4(sc, 0x7c, reg);
3156 /* Disable interrupt mitigation. */
3157 CSR_WRITE_2(sc, 0xe2, 0);
3160 * Disable TSO if interface MTU size is greater than MSS
3161 * allowed in controller.
3163 if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3164 ifp->if_capenable &= ~IFCAP_TSO4;
3165 ifp->if_hwassist &= ~CSUM_TSO;
3169 * Init our MAC address. Even though the chipset
3170 * documentation doesn't mention it, we need to enter "Config
3171 * register write enable" mode to modify the ID registers.
3173 /* Copy MAC address on stack to align. */
3174 bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3175 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3176 CSR_WRITE_4(sc, RL_IDR0,
3177 htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3178 CSR_WRITE_4(sc, RL_IDR4,
3179 htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3180 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3183 * Load the addresses of the RX and TX lists into the chip.
3186 CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3187 RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3188 CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3189 RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3191 CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3192 RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3193 CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3194 RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3196 if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3197 /* Disable RXDV gate. */
3198 CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
3203 * Enable transmit and receive for pre-RTL8168G controllers.
3204 * RX/TX MACs should be enabled before RX/TX configuration.
3206 if ((sc->rl_flags & RL_FLAG_8168G_PLUS) == 0)
3207 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
3210 * Set the initial TX configuration.
3212 if (sc->rl_testmode) {
3213 if (sc->rl_type == RL_8169)
3214 CSR_WRITE_4(sc, RL_TXCFG,
3215 RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3217 CSR_WRITE_4(sc, RL_TXCFG,
3218 RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3220 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3222 CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3225 * Set the initial RX configuration.
3229 /* Configure interrupt moderation. */
3230 if (sc->rl_type == RL_8169) {
3231 /* Magic from vendor. */
3232 CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3236 * Enable transmit and receive for RTL8168G and later controllers.
3237 * RX/TX MACs should be enabled after RX/TX configuration.
3239 if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
3240 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
3242 #ifdef DEVICE_POLLING
3244 * Disable interrupts if we are polling.
3246 if (ifp->if_capenable & IFCAP_POLLING)
3247 CSR_WRITE_2(sc, RL_IMR, 0);
3248 else /* otherwise ... */
3252 * Enable interrupts.
3254 if (sc->rl_testmode)
3255 CSR_WRITE_2(sc, RL_IMR, 0);
3257 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3258 CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3260 /* Set initial TX threshold */
3261 sc->rl_txthresh = RL_TX_THRESH_INIT;
3263 /* Start RX/TX process. */
3264 CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3267 * Initialize the timer interrupt register so that
3268 * a timer interrupt will be generated once the timer
3269 * reaches a certain number of ticks. The timer is
3270 * reloaded on each transmit.
3272 #ifdef RE_TX_MODERATION
3274 * Use timer interrupt register to moderate TX interrupt
3275 * moderation, which dramatically improves TX frame rate.
3277 if (sc->rl_type == RL_8169)
3278 CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3280 CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3283 * Use timer interrupt register to moderate RX interrupt
3286 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3288 if (sc->rl_type == RL_8169)
3289 CSR_WRITE_4(sc, RL_TIMERINT_8169,
3290 RL_USECS(sc->rl_int_rx_mod));
3292 if (sc->rl_type == RL_8169)
3293 CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3298 * For 8169 gigE NICs, set the max allowed RX packet
3299 * size so we can receive jumbo frames.
3301 if (sc->rl_type == RL_8169) {
3302 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3304 * For controllers that use new jumbo frame scheme,
3305 * set maximum size of jumbo frame depending on
3306 * controller revisions.
3308 if (ifp->if_mtu > RL_MTU)
3309 CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3310 sc->rl_hwrev->rl_max_mtu +
3311 ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
3314 CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
3316 } else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
3317 sc->rl_hwrev->rl_max_mtu == RL_MTU) {
3318 /* RTL810x has no jumbo frame support. */
3319 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
3321 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
3324 if (sc->rl_testmode)
3327 CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3330 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3331 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3333 sc->rl_flags &= ~RL_FLAG_LINK;
3336 sc->rl_watchdog_timer = 0;
3337 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3341 * Set media options.
3344 re_ifmedia_upd(struct ifnet *ifp)
3346 struct rl_softc *sc;
3347 struct mii_data *mii;
3351 mii = device_get_softc(sc->rl_miibus);
3353 error = mii_mediachg(mii);
3360 * Report current media status.
3363 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3365 struct rl_softc *sc;
3366 struct mii_data *mii;
3369 mii = device_get_softc(sc->rl_miibus);
3373 ifmr->ifm_active = mii->mii_media_active;
3374 ifmr->ifm_status = mii->mii_media_status;
3379 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3381 struct rl_softc *sc = ifp->if_softc;
3382 struct ifreq *ifr = (struct ifreq *) data;
3383 struct mii_data *mii;
3388 if (ifr->ifr_mtu < ETHERMIN ||
3389 ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu ||
3390 ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 &&
3391 ifr->ifr_mtu > RL_MTU)) {
3396 if (ifp->if_mtu != ifr->ifr_mtu) {
3397 ifp->if_mtu = ifr->ifr_mtu;
3398 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3399 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3400 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3403 if (ifp->if_mtu > RL_TSO_MTU &&
3404 (ifp->if_capenable & IFCAP_TSO4) != 0) {
3405 ifp->if_capenable &= ~(IFCAP_TSO4 |
3407 ifp->if_hwassist &= ~CSUM_TSO;
3409 VLAN_CAPABILITIES(ifp);
3415 if ((ifp->if_flags & IFF_UP) != 0) {
3416 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3417 if (((ifp->if_flags ^ sc->rl_if_flags)
3418 & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3423 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3426 sc->rl_if_flags = ifp->if_flags;
3432 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3438 mii = device_get_softc(sc->rl_miibus);
3439 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3445 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3447 #ifdef DEVICE_POLLING
3448 if (mask & IFCAP_POLLING) {
3449 if (ifr->ifr_reqcap & IFCAP_POLLING) {
3450 error = ether_poll_register(re_poll, ifp);
3454 /* Disable interrupts */
3455 CSR_WRITE_2(sc, RL_IMR, 0x0000);
3456 ifp->if_capenable |= IFCAP_POLLING;
3459 error = ether_poll_deregister(ifp);
3460 /* Enable interrupts. */
3462 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3463 ifp->if_capenable &= ~IFCAP_POLLING;
3467 #endif /* DEVICE_POLLING */
3469 if ((mask & IFCAP_TXCSUM) != 0 &&
3470 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3471 ifp->if_capenable ^= IFCAP_TXCSUM;
3472 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3473 ifp->if_hwassist |= RE_CSUM_FEATURES;
3475 ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3478 if ((mask & IFCAP_RXCSUM) != 0 &&
3479 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3480 ifp->if_capenable ^= IFCAP_RXCSUM;
3483 if ((mask & IFCAP_TSO4) != 0 &&
3484 (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3485 ifp->if_capenable ^= IFCAP_TSO4;
3486 if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3487 ifp->if_hwassist |= CSUM_TSO;
3489 ifp->if_hwassist &= ~CSUM_TSO;
3490 if (ifp->if_mtu > RL_TSO_MTU &&
3491 (ifp->if_capenable & IFCAP_TSO4) != 0) {
3492 ifp->if_capenable &= ~IFCAP_TSO4;
3493 ifp->if_hwassist &= ~CSUM_TSO;
3496 if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3497 (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3498 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3499 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3500 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3501 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3502 /* TSO over VLAN requires VLAN hardware tagging. */
3503 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3504 ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3507 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
3508 (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
3509 IFCAP_VLAN_HWTSO)) != 0)
3511 if ((mask & IFCAP_WOL) != 0 &&
3512 (ifp->if_capabilities & IFCAP_WOL) != 0) {
3513 if ((mask & IFCAP_WOL_UCAST) != 0)
3514 ifp->if_capenable ^= IFCAP_WOL_UCAST;
3515 if ((mask & IFCAP_WOL_MCAST) != 0)
3516 ifp->if_capenable ^= IFCAP_WOL_MCAST;
3517 if ((mask & IFCAP_WOL_MAGIC) != 0)
3518 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
3520 if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
3521 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3525 VLAN_CAPABILITIES(ifp);
3529 error = ether_ioctl(ifp, command, data);
3537 re_watchdog(struct rl_softc *sc)
3543 if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
3548 if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3549 if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3551 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3552 re_start_locked(ifp);
3556 if_printf(ifp, "watchdog timeout\n");
3560 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3562 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3563 re_start_locked(ifp);
3567 * Stop the adapter and free any mbufs allocated to the
3571 re_stop(struct rl_softc *sc)
3575 struct rl_txdesc *txd;
3576 struct rl_rxdesc *rxd;
3582 sc->rl_watchdog_timer = 0;
3583 callout_stop(&sc->rl_stat_callout);
3584 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3587 * Disable accepting frames to put RX MAC into idle state.
3588 * Otherwise it's possible to get frames while stop command
3589 * execution is in progress and controller can DMA the frame
3590 * to already freed RX buffer during that period.
3592 CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3593 ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3594 RL_RXCFG_RX_BROAD));
3596 if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3597 /* Enable RXDV gate. */
3598 CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) |
3602 if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3603 for (i = RL_TIMEOUT; i > 0; i--) {
3604 if ((CSR_READ_1(sc, sc->rl_txstart) &
3605 RL_TXSTART_START) == 0)
3610 device_printf(sc->rl_dev,
3611 "stopping TX poll timed out!\n");
3612 CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3613 } else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3614 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3616 if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3617 for (i = RL_TIMEOUT; i > 0; i--) {
3618 if ((CSR_READ_4(sc, RL_TXCFG) &
3619 RL_TXCFG_QUEUE_EMPTY) != 0)
3624 device_printf(sc->rl_dev,
3625 "stopping TXQ timed out!\n");
3628 CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3630 CSR_WRITE_2(sc, RL_IMR, 0x0000);
3631 CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3633 if (sc->rl_head != NULL) {
3634 m_freem(sc->rl_head);
3635 sc->rl_head = sc->rl_tail = NULL;
3638 /* Free the TX list buffers. */
3639 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3640 txd = &sc->rl_ldata.rl_tx_desc[i];
3641 if (txd->tx_m != NULL) {
3642 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3643 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3644 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3651 /* Free the RX list buffers. */
3652 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3653 rxd = &sc->rl_ldata.rl_rx_desc[i];
3654 if (rxd->rx_m != NULL) {
3655 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3656 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3657 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3664 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
3665 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3666 rxd = &sc->rl_ldata.rl_jrx_desc[i];
3667 if (rxd->rx_m != NULL) {
3668 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
3669 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3670 bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
3680 * Device suspend routine. Stop the interface and save some PCI
3681 * settings in case the BIOS doesn't restore them properly on
3685 re_suspend(device_t dev)
3687 struct rl_softc *sc;
3689 sc = device_get_softc(dev);
3701 * Device resume routine. Restore some PCI settings in case the BIOS
3702 * doesn't, re-enable busmastering, and restart the interface if
3706 re_resume(device_t dev)
3708 struct rl_softc *sc;
3711 sc = device_get_softc(dev);
3716 /* Take controller out of sleep mode. */
3717 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
3718 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
3719 CSR_WRITE_1(sc, RL_GPIO,
3720 CSR_READ_1(sc, RL_GPIO) | 0x01);
3724 * Clear WOL matching such that normal Rx filtering
3725 * wouldn't interfere with WOL patterns.
3729 /* reinitialize interface if necessary */
3730 if (ifp->if_flags & IFF_UP)
3740 * Stop all chip I/O so that the kernel's probe routines don't
3741 * get confused by errant DMAs when rebooting.
3744 re_shutdown(device_t dev)
3746 struct rl_softc *sc;
3748 sc = device_get_softc(dev);
3753 * Mark interface as down since otherwise we will panic if
3754 * interrupt comes in later on, which can happen in some
3757 sc->rl_ifp->if_flags &= ~IFF_UP;
3765 re_set_linkspeed(struct rl_softc *sc)
3767 struct mii_softc *miisc;
3768 struct mii_data *mii;
3773 mii = device_get_softc(sc->rl_miibus);
3776 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
3777 (IFM_ACTIVE | IFM_AVALID)) {
3778 switch IFM_SUBTYPE(mii->mii_media_active) {
3789 miisc = LIST_FIRST(&mii->mii_phys);
3790 phyno = miisc->mii_phy;
3791 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3793 re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
3794 re_miibus_writereg(sc->rl_dev, phyno,
3795 MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
3796 re_miibus_writereg(sc->rl_dev, phyno,
3797 MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
3801 * Poll link state until re(4) get a 10/100Mbps link.
3803 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
3805 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
3806 == (IFM_ACTIVE | IFM_AVALID)) {
3807 switch (IFM_SUBTYPE(mii->mii_media_active)) {
3819 if (i == MII_ANEGTICKS_GIGE)
3820 device_printf(sc->rl_dev,
3821 "establishing a link failed, WOL may not work!");
3824 * No link, force MAC to have 100Mbps, full-duplex link.
3825 * MAC does not require reprogramming on resolved speed/duplex,
3826 * so this is just for completeness.
3828 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
3829 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
3833 re_setwol(struct rl_softc *sc)
3842 if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
3846 /* Put controller into sleep mode. */
3847 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
3848 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
3849 CSR_WRITE_1(sc, RL_GPIO,
3850 CSR_READ_1(sc, RL_GPIO) & ~0x01);
3852 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
3853 if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3854 /* Disable RXDV gate. */
3855 CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
3859 if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
3860 re_set_linkspeed(sc);
3861 if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3862 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
3864 /* Enable config register write. */
3865 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
3868 v = CSR_READ_1(sc, sc->rl_cfg1);
3870 if ((ifp->if_capenable & IFCAP_WOL) != 0)
3872 CSR_WRITE_1(sc, sc->rl_cfg1, v);
3874 v = CSR_READ_1(sc, sc->rl_cfg3);
3875 v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3876 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
3877 v |= RL_CFG3_WOL_MAGIC;
3878 CSR_WRITE_1(sc, sc->rl_cfg3, v);
3880 v = CSR_READ_1(sc, sc->rl_cfg5);
3881 v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
3882 RL_CFG5_WOL_LANWAKE);
3883 if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
3884 v |= RL_CFG5_WOL_UCAST;
3885 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
3886 v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
3887 if ((ifp->if_capenable & IFCAP_WOL) != 0)
3888 v |= RL_CFG5_WOL_LANWAKE;
3889 CSR_WRITE_1(sc, sc->rl_cfg5, v);
3891 /* Config register write done. */
3892 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3894 if ((ifp->if_capenable & IFCAP_WOL) == 0 &&
3895 (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3896 CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
3898 * It seems that hardware resets its link speed to 100Mbps in
3899 * power down mode so switching to 100Mbps in driver is not
3903 /* Request PME if WOL is requested. */
3904 pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
3905 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
3906 if ((ifp->if_capenable & IFCAP_WOL) != 0)
3907 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
3908 pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
3912 re_clrwol(struct rl_softc *sc)
3919 if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
3922 /* Enable config register write. */
3923 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
3925 v = CSR_READ_1(sc, sc->rl_cfg3);
3926 v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3927 CSR_WRITE_1(sc, sc->rl_cfg3, v);
3929 /* Config register write done. */
3930 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3932 v = CSR_READ_1(sc, sc->rl_cfg5);
3933 v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
3934 v &= ~RL_CFG5_WOL_LANWAKE;
3935 CSR_WRITE_1(sc, sc->rl_cfg5, v);
3939 re_add_sysctls(struct rl_softc *sc)
3941 struct sysctl_ctx_list *ctx;
3942 struct sysctl_oid_list *children;
3945 ctx = device_get_sysctl_ctx(sc->rl_dev);
3946 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
3948 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
3949 CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
3950 "Statistics Information");
3951 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3954 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3955 CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3956 sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3957 /* Pull in device tunables. */
3958 sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3959 error = resource_int_value(device_get_name(sc->rl_dev),
3960 device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3962 if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3963 sc->rl_int_rx_mod > RL_TIMER_MAX) {
3964 device_printf(sc->rl_dev, "int_rx_mod value out of "
3965 "range; using default: %d\n",
3967 sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3973 re_sysctl_stats(SYSCTL_HANDLER_ARGS)
3975 struct rl_softc *sc;
3976 struct rl_stats *stats;
3977 int error, i, result;
3980 error = sysctl_handle_int(oidp, &result, 0, req);
3981 if (error || req->newptr == NULL)
3985 sc = (struct rl_softc *)arg1;
3987 if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3991 bus_dmamap_sync(sc->rl_ldata.rl_stag,
3992 sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
3993 CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
3994 RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
3995 CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
3996 RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
3997 CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
3998 RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
3999 RL_DUMPSTATS_START));
4000 for (i = RL_TIMEOUT; i > 0; i--) {
4001 if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
4002 RL_DUMPSTATS_START) == 0)
4006 bus_dmamap_sync(sc->rl_ldata.rl_stag,
4007 sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
4010 device_printf(sc->rl_dev,
4011 "DUMP statistics request timed out\n");
4015 stats = sc->rl_ldata.rl_stats;
4016 printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
4017 printf("Tx frames : %ju\n",
4018 (uintmax_t)le64toh(stats->rl_tx_pkts));
4019 printf("Rx frames : %ju\n",
4020 (uintmax_t)le64toh(stats->rl_rx_pkts));
4021 printf("Tx errors : %ju\n",
4022 (uintmax_t)le64toh(stats->rl_tx_errs));
4023 printf("Rx errors : %u\n",
4024 le32toh(stats->rl_rx_errs));
4025 printf("Rx missed frames : %u\n",
4026 (uint32_t)le16toh(stats->rl_missed_pkts));
4027 printf("Rx frame alignment errs : %u\n",
4028 (uint32_t)le16toh(stats->rl_rx_framealign_errs));
4029 printf("Tx single collisions : %u\n",
4030 le32toh(stats->rl_tx_onecoll));
4031 printf("Tx multiple collisions : %u\n",
4032 le32toh(stats->rl_tx_multicolls));
4033 printf("Rx unicast frames : %ju\n",
4034 (uintmax_t)le64toh(stats->rl_rx_ucasts));
4035 printf("Rx broadcast frames : %ju\n",
4036 (uintmax_t)le64toh(stats->rl_rx_bcasts));
4037 printf("Rx multicast frames : %u\n",
4038 le32toh(stats->rl_rx_mcasts));
4039 printf("Tx aborts : %u\n",
4040 (uint32_t)le16toh(stats->rl_tx_aborts));
4041 printf("Tx underruns : %u\n",
4042 (uint32_t)le16toh(stats->rl_rx_underruns));
4049 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4055 value = *(int *)arg1;
4056 error = sysctl_handle_int(oidp, &value, 0, req);
4057 if (error || req->newptr == NULL)
4059 if (value < low || value > high)
4061 *(int *)arg1 = value;
4067 sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4070 return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,