3 * Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
5 * Copyright (c) 1997, 1998-2003
6 * Bill Paul <wpaul@windriver.com>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
35 * $FreeBSD: src/sys/dev/re/if_re.c,v 1.25 2004/06/09 14:34:01 naddy Exp $
39 * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
41 * Written by Bill Paul <wpaul@windriver.com>
42 * Senior Networking Software Engineer
47 * This driver is designed to support RealTek's next generation of
48 * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
49 * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
50 * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
52 * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
53 * with the older 8139 family, however it also supports a special
54 * C+ mode of operation that provides several new performance enhancing
55 * features. These include:
57 * o Descriptor based DMA mechanism. Each descriptor represents
58 * a single packet fragment. Data buffers may be aligned on
63 * o TCP/IP checksum offload for both RX and TX
65 * o High and normal priority transmit DMA rings
67 * o VLAN tag insertion and extraction
69 * o TCP large send (segmentation offload)
71 * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
72 * programming API is fairly straightforward. The RX filtering, EEPROM
73 * access and PHY access is the same as it is on the older 8139 series
76 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
77 * same programming API and feature set as the 8139C+ with the following
78 * differences and additions:
84 * o GMII and TBI ports/registers for interfacing with copper
87 * o RX and TX DMA rings can have up to 1024 descriptors
88 * (the 8139C+ allows a maximum of 64)
90 * o Slight differences in register layout from the 8139C+
92 * The TX start and timer interrupt registers are at different locations
93 * on the 8169 than they are on the 8139C+. Also, the status word in the
94 * RX descriptor has a slightly different bit layout. The 8169 does not
95 * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
98 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
99 * (the 'S' stands for 'single-chip'). These devices have the same
100 * programming API as the older 8169, but also have some vendor-specific
101 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
102 * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
104 * This driver takes advantage of the RX and TX checksum offload and
105 * VLAN tag insertion/extraction features. It also implements TX
106 * interrupt moderation using the timer interrupt registers, which
107 * significantly reduces TX interrupt load. There is also support
108 * for jumbo frames, however the 8169/8169S/8110S can not transmit
109 * jumbo frames larger than 7440, so the max MTU possible with this
110 * driver is 7422 bytes.
115 #include "opt_ifpoll.h"
117 #include <sys/param.h>
119 #include <sys/endian.h>
120 #include <sys/kernel.h>
121 #include <sys/in_cksum.h>
122 #include <sys/interrupt.h>
123 #include <sys/malloc.h>
124 #include <sys/mbuf.h>
125 #include <sys/rman.h>
126 #include <sys/serialize.h>
127 #include <sys/socket.h>
128 #include <sys/sockio.h>
129 #include <sys/sysctl.h>
132 #include <net/ethernet.h>
134 #include <net/ifq_var.h>
135 #include <net/if_arp.h>
136 #include <net/if_dl.h>
137 #include <net/if_media.h>
138 #include <net/if_poll.h>
139 #include <net/if_types.h>
140 #include <net/vlan/if_vlan_var.h>
141 #include <net/vlan/if_vlan_ether.h>
143 #include <netinet/ip.h>
145 #include <dev/netif/mii_layer/mii.h>
146 #include <dev/netif/mii_layer/miivar.h>
149 #include <bus/pci/pcireg.h>
150 #include <bus/pci/pcivar.h>
152 /* "device miibus" required. See GENERIC if you get errors here. */
153 #include "miibus_if.h"
155 #include <dev/netif/re/if_rereg.h>
156 #include <dev/netif/re/if_revar.h>
159 * Various supported device vendors/types and their names.
161 static const struct re_type {
166 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
167 "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
169 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
170 "RealTek 8139C+ 10/100BaseTX" },
172 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
173 "RealTek 810x PCIe 10/100baseTX" },
175 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
176 "RealTek 8111/8168 PCIe Gigabit Ethernet" },
178 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
179 "RealTek 8110/8169 Gigabit Ethernet" },
181 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
182 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
184 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CG_LAPCIGT,
185 "Corega CG-LAPCIGT Gigabit Ethernet" },
187 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
188 "Linksys EG1032 Gigabit Ethernet" },
190 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_997902,
191 "US Robotics 997902 Gigabit Ethernet" },
193 { PCI_VENDOR_TTTECH, PCI_PRODUCT_TTTECH_MC322,
194 "TTTech MC322 Gigabit Ethernet" },
199 static const struct re_hwrev re_hwrevs[] = {
200 { RE_HWREV_8139CPLUS, ETHERMTU,
201 RE_C_HWCSUM | RE_C_8139CP | RE_C_FASTE },
203 { RE_HWREV_8169, ETHERMTU,
204 RE_C_HWCSUM | RE_C_8169 },
206 { RE_HWREV_8110S, RE_MTU_6K,
207 RE_C_HWCSUM | RE_C_8169 },
209 { RE_HWREV_8169S, RE_MTU_6K,
210 RE_C_HWCSUM | RE_C_8169 },
212 { RE_HWREV_8169SB, RE_MTU_6K,
213 RE_C_HWCSUM | RE_C_PHYPMGT | RE_C_8169 },
215 { RE_HWREV_8169SC, RE_MTU_6K,
216 RE_C_HWCSUM | RE_C_PHYPMGT | RE_C_8169 },
218 { RE_HWREV_8168B1, RE_MTU_6K,
219 RE_C_HWIM | RE_C_HWCSUM | RE_C_PHYPMGT },
221 { RE_HWREV_8168B2, RE_MTU_6K,
222 RE_C_HWIM | RE_C_HWCSUM | RE_C_PHYPMGT | RE_C_AUTOPAD },
224 { RE_HWREV_8168C, RE_MTU_6K,
225 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
226 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
228 { RE_HWREV_8168CP, RE_MTU_6K,
229 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
230 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
232 { RE_HWREV_8168D, RE_MTU_9K,
233 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
234 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
236 { RE_HWREV_8168DP, RE_MTU_9K,
237 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
238 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
240 { RE_HWREV_8168E, RE_MTU_9K,
241 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
242 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
244 { RE_HWREV_8168F, RE_MTU_9K,
245 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
246 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
248 { RE_HWREV_8111F, RE_MTU_9K,
249 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
250 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
252 { RE_HWREV_8411, ETHERMTU,
253 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
254 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
256 { RE_HWREV_8168G, ETHERMTU,
257 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
258 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
260 { RE_HWREV_8168EP, ETHERMTU,
261 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
262 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
264 { RE_HWREV_8168GU, ETHERMTU,
265 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
266 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
268 { RE_HWREV_8411B, ETHERMTU,
269 RE_C_HWIM | RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT |
270 RE_C_AUTOPAD | RE_C_CONTIGRX | RE_C_STOP_RXTX },
272 { RE_HWREV_8100E, ETHERMTU,
273 RE_C_HWCSUM | RE_C_FASTE },
275 { RE_HWREV_8101E, ETHERMTU,
276 RE_C_HWCSUM | RE_C_FASTE },
278 { RE_HWREV_8102E, ETHERMTU,
279 RE_C_HWCSUM | RE_C_MAC2 | RE_C_AUTOPAD | RE_C_STOP_RXTX |
282 { RE_HWREV_8102EL, ETHERMTU,
283 RE_C_HWCSUM | RE_C_MAC2 | RE_C_AUTOPAD | RE_C_STOP_RXTX |
286 { RE_HWREV_8105E, ETHERMTU,
287 RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT | RE_C_AUTOPAD |
288 RE_C_STOP_RXTX | RE_C_FASTE },
290 { RE_HWREV_8401E, ETHERMTU,
291 RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT | RE_C_AUTOPAD |
292 RE_C_STOP_RXTX | RE_C_FASTE },
294 { RE_HWREV_8402, ETHERMTU,
295 RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT | RE_C_AUTOPAD |
296 RE_C_STOP_RXTX | RE_C_FASTE },
298 { RE_HWREV_8106E, ETHERMTU,
299 RE_C_HWCSUM | RE_C_MAC2 | RE_C_PHYPMGT | RE_C_AUTOPAD |
300 RE_C_STOP_RXTX | RE_C_FASTE },
302 { RE_HWREV_NULL, 0, 0 }
305 static int re_probe(device_t);
306 static int re_attach(device_t);
307 static int re_detach(device_t);
308 static int re_suspend(device_t);
309 static int re_resume(device_t);
310 static void re_shutdown(device_t);
312 static int re_allocmem(device_t);
313 static void re_freemem(device_t);
314 static void re_freebufmem(struct re_softc *, int, int);
315 static int re_encap(struct re_softc *, struct mbuf **, int *);
316 static int re_newbuf_std(struct re_softc *, int, int);
317 static int re_newbuf_jumbo(struct re_softc *, int, int);
318 static void re_setup_rxdesc(struct re_softc *, int);
319 static int re_rx_list_init(struct re_softc *);
320 static int re_tx_list_init(struct re_softc *);
321 static int re_rxeof(struct re_softc *);
322 static int re_txeof(struct re_softc *);
323 static int re_tx_collect(struct re_softc *);
324 static void re_intr(void *);
325 static void re_tick(void *);
326 static void re_tick_serialized(void *);
328 static void re_start(struct ifnet *, struct ifaltq_subque *);
329 static int re_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
330 static void re_init(void *);
331 static void re_stop(struct re_softc *);
332 static void re_watchdog(struct ifnet *);
333 static int re_ifmedia_upd(struct ifnet *);
334 static void re_ifmedia_sts(struct ifnet *, struct ifmediareq *);
336 static void re_eeprom_putbyte(struct re_softc *, int);
337 static void re_eeprom_getword(struct re_softc *, int, u_int16_t *);
338 static void re_read_eeprom(struct re_softc *, caddr_t, int, int);
339 static void re_get_eewidth(struct re_softc *);
341 static int re_gmii_readreg(device_t, int, int);
342 static int re_gmii_writereg(device_t, int, int, int);
344 static int re_miibus_readreg(device_t, int, int);
345 static int re_miibus_writereg(device_t, int, int, int);
346 static void re_miibus_statchg(device_t);
348 static void re_setmulti(struct re_softc *);
349 static void re_reset(struct re_softc *, int);
350 static void re_get_eaddr(struct re_softc *, uint8_t *);
352 static void re_setup_hw_im(struct re_softc *);
353 static void re_setup_sim_im(struct re_softc *);
354 static void re_disable_hw_im(struct re_softc *);
355 static void re_disable_sim_im(struct re_softc *);
356 static void re_config_imtype(struct re_softc *, int);
357 static void re_setup_intr(struct re_softc *, int, int);
359 static int re_sysctl_hwtime(SYSCTL_HANDLER_ARGS, int *);
360 static int re_sysctl_rxtime(SYSCTL_HANDLER_ARGS);
361 static int re_sysctl_txtime(SYSCTL_HANDLER_ARGS);
362 static int re_sysctl_simtime(SYSCTL_HANDLER_ARGS);
363 static int re_sysctl_imtype(SYSCTL_HANDLER_ARGS);
365 static int re_jpool_alloc(struct re_softc *);
366 static void re_jpool_free(struct re_softc *);
367 static struct re_jbuf *re_jbuf_alloc(struct re_softc *);
368 static void re_jbuf_free(void *);
369 static void re_jbuf_ref(void *);
372 static int re_diag(struct re_softc *);
376 static void re_npoll(struct ifnet *, struct ifpoll_info *);
377 static void re_npoll_compat(struct ifnet *, void *, int);
380 static device_method_t re_methods[] = {
381 /* Device interface */
382 DEVMETHOD(device_probe, re_probe),
383 DEVMETHOD(device_attach, re_attach),
384 DEVMETHOD(device_detach, re_detach),
385 DEVMETHOD(device_suspend, re_suspend),
386 DEVMETHOD(device_resume, re_resume),
387 DEVMETHOD(device_shutdown, re_shutdown),
390 DEVMETHOD(bus_print_child, bus_generic_print_child),
391 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
394 DEVMETHOD(miibus_readreg, re_miibus_readreg),
395 DEVMETHOD(miibus_writereg, re_miibus_writereg),
396 DEVMETHOD(miibus_statchg, re_miibus_statchg),
401 static driver_t re_driver = {
404 sizeof(struct re_softc)
407 static devclass_t re_devclass;
409 DECLARE_DUMMY_MODULE(if_re);
410 MODULE_DEPEND(if_re, miibus, 1, 1, 1);
411 DRIVER_MODULE(if_re, pci, re_driver, re_devclass, NULL, NULL);
412 DRIVER_MODULE(if_re, cardbus, re_driver, re_devclass, NULL, NULL);
413 DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, NULL, NULL);
415 static int re_rx_desc_count = RE_RX_DESC_CNT_DEF;
416 static int re_tx_desc_count = RE_TX_DESC_CNT_DEF;
417 static int re_msi_enable = 0;
419 TUNABLE_INT("hw.re.rx_desc_count", &re_rx_desc_count);
420 TUNABLE_INT("hw.re.tx_desc_count", &re_tx_desc_count);
421 TUNABLE_INT("hw.re.msi.enable", &re_msi_enable);
424 CSR_WRITE_1(sc, RE_EECMD, CSR_READ_1(sc, RE_EECMD) | (x))
427 CSR_WRITE_1(sc, RE_EECMD, CSR_READ_1(sc, RE_EECMD) & ~(x))
430 re_free_rxchain(struct re_softc *sc)
432 if (sc->re_head != NULL) {
433 m_freem(sc->re_head);
434 sc->re_head = sc->re_tail = NULL;
439 * Send a read command and address to the EEPROM, check for ACK.
442 re_eeprom_putbyte(struct re_softc *sc, int addr)
446 d = addr | (RE_9346_READ << sc->re_eewidth);
449 * Feed in each bit and strobe the clock.
451 for (i = 1 << (sc->re_eewidth + 3); i; i >>= 1) {
453 EE_SET(RE_EE_DATAIN);
455 EE_CLR(RE_EE_DATAIN);
465 * Read a word of data stored in the EEPROM at address 'addr.'
468 re_eeprom_getword(struct re_softc *sc, int addr, uint16_t *dest)
474 * Send address of word we want to read.
476 re_eeprom_putbyte(sc, addr);
479 * Start reading bits from EEPROM.
481 for (i = 0x8000; i != 0; i >>= 1) {
484 if (CSR_READ_1(sc, RE_EECMD) & RE_EE_DATAOUT)
494 * Read a sequence of words from the EEPROM.
497 re_read_eeprom(struct re_softc *sc, caddr_t dest, int off, int cnt)
500 uint16_t word = 0, *ptr;
502 CSR_SETBIT_1(sc, RE_EECMD, RE_EEMODE_PROGRAM);
505 for (i = 0; i < cnt; i++) {
506 CSR_SETBIT_1(sc, RE_EECMD, RE_EE_SEL);
507 re_eeprom_getword(sc, off + i, &word);
508 CSR_CLRBIT_1(sc, RE_EECMD, RE_EE_SEL);
509 ptr = (uint16_t *)(dest + (i * 2));
513 CSR_CLRBIT_1(sc, RE_EECMD, RE_EEMODE_PROGRAM);
517 re_get_eewidth(struct re_softc *sc)
522 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
523 if (re_did != 0x8129)
528 re_gmii_readreg(device_t dev, int phy, int reg)
530 struct re_softc *sc = device_get_softc(dev);
537 /* Let the rgephy driver read the GMEDIASTAT register */
539 if (reg == RE_GMEDIASTAT)
540 return(CSR_READ_1(sc, RE_GMEDIASTAT));
542 CSR_WRITE_4(sc, RE_PHYAR, reg << 16);
545 for (i = 0; i < RE_TIMEOUT; i++) {
546 rval = CSR_READ_4(sc, RE_PHYAR);
547 if (rval & RE_PHYAR_BUSY)
552 if (i == RE_TIMEOUT) {
553 device_printf(dev, "PHY read failed\n");
557 return(rval & RE_PHYAR_PHYDATA);
561 re_gmii_writereg(device_t dev, int phy, int reg, int data)
563 struct re_softc *sc = device_get_softc(dev);
567 CSR_WRITE_4(sc, RE_PHYAR,
568 (reg << 16) | (data & RE_PHYAR_PHYDATA) | RE_PHYAR_BUSY);
571 for (i = 0; i < RE_TIMEOUT; i++) {
572 rval = CSR_READ_4(sc, RE_PHYAR);
573 if ((rval & RE_PHYAR_BUSY) == 0)
579 device_printf(dev, "PHY write failed\n");
585 re_miibus_readreg(device_t dev, int phy, int reg)
587 struct re_softc *sc = device_get_softc(dev);
589 uint16_t re8139_reg = 0;
591 if (!RE_IS_8139CP(sc)) {
592 rval = re_gmii_readreg(dev, phy, reg);
596 /* Pretend the internal PHY is only at address 0 */
602 re8139_reg = RE_BMCR;
605 re8139_reg = RE_BMSR;
608 re8139_reg = RE_ANAR;
611 re8139_reg = RE_ANER;
614 re8139_reg = RE_LPAR;
620 * Allow the rlphy driver to read the media status
621 * register. If we have a link partner which does not
622 * support NWAY, this is the register which will tell
623 * us the results of parallel detection.
626 return(CSR_READ_1(sc, RE_MEDIASTAT));
628 device_printf(dev, "bad phy register\n");
631 rval = CSR_READ_2(sc, re8139_reg);
632 if (re8139_reg == RE_BMCR) {
633 /* 8139C+ has different bit layout. */
634 rval &= ~(BMCR_LOOP | BMCR_ISO);
640 re_miibus_writereg(device_t dev, int phy, int reg, int data)
642 struct re_softc *sc= device_get_softc(dev);
643 u_int16_t re8139_reg = 0;
645 if (!RE_IS_8139CP(sc))
646 return(re_gmii_writereg(dev, phy, reg, data));
648 /* Pretend the internal PHY is only at address 0 */
654 re8139_reg = RE_BMCR;
655 /* 8139C+ has different bit layout. */
656 data &= ~(BMCR_LOOP | BMCR_ISO);
659 re8139_reg = RE_BMSR;
662 re8139_reg = RE_ANAR;
665 re8139_reg = RE_ANER;
668 re8139_reg = RE_LPAR;
674 device_printf(dev, "bad phy register\n");
677 CSR_WRITE_2(sc, re8139_reg, data);
682 re_miibus_statchg(device_t dev)
687 * Program the 64-bit multicast hash filter.
690 re_setmulti(struct re_softc *sc)
692 struct ifnet *ifp = &sc->arpcom.ac_if;
694 uint32_t hashes[2] = { 0, 0 };
695 struct ifmultiaddr *ifma;
699 rxfilt = CSR_READ_4(sc, RE_RXCFG);
701 /* Set the individual bit to receive frames for this host only. */
702 rxfilt |= RE_RXCFG_RX_INDIV;
703 /* Set capture broadcast bit to capture broadcast frames. */
704 rxfilt |= RE_RXCFG_RX_BROAD;
706 rxfilt &= ~(RE_RXCFG_RX_ALLPHYS | RE_RXCFG_RX_MULTI);
707 if ((ifp->if_flags & IFF_ALLMULTI) || (ifp->if_flags & IFF_PROMISC)) {
708 rxfilt |= RE_RXCFG_RX_MULTI;
710 /* If we want promiscuous mode, set the allframes bit. */
711 if (ifp->if_flags & IFF_PROMISC)
712 rxfilt |= RE_RXCFG_RX_ALLPHYS;
714 CSR_WRITE_4(sc, RE_RXCFG, rxfilt);
715 CSR_WRITE_4(sc, RE_MAR0, 0xFFFFFFFF);
716 CSR_WRITE_4(sc, RE_MAR4, 0xFFFFFFFF);
720 /* first, zot all the existing hash bits */
721 CSR_WRITE_4(sc, RE_MAR0, 0);
722 CSR_WRITE_4(sc, RE_MAR4, 0);
724 /* now program new ones */
725 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
726 if (ifma->ifma_addr->sa_family != AF_LINK)
728 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
729 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
731 hashes[0] |= (1 << h);
733 hashes[1] |= (1 << (h - 32));
738 rxfilt |= RE_RXCFG_RX_MULTI;
740 rxfilt &= ~RE_RXCFG_RX_MULTI;
742 CSR_WRITE_4(sc, RE_RXCFG, rxfilt);
745 * For some unfathomable reason, RealTek decided to reverse
746 * the order of the multicast hash registers in the PCI Express
747 * parts. This means we have to write the hash pattern in reverse
748 * order for those devices.
750 if (sc->re_caps & RE_C_PCIE) {
751 CSR_WRITE_4(sc, RE_MAR0, bswap32(hashes[1]));
752 CSR_WRITE_4(sc, RE_MAR4, bswap32(hashes[0]));
754 CSR_WRITE_4(sc, RE_MAR0, hashes[0]);
755 CSR_WRITE_4(sc, RE_MAR4, hashes[1]);
760 re_reset(struct re_softc *sc, int running)
764 if ((sc->re_caps & RE_C_STOP_RXTX) && running) {
765 CSR_WRITE_1(sc, RE_COMMAND,
766 RE_CMD_STOPREQ | RE_CMD_TX_ENB | RE_CMD_RX_ENB);
770 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_RESET);
772 for (i = 0; i < RE_TIMEOUT; i++) {
774 if ((CSR_READ_1(sc, RE_COMMAND) & RE_CMD_RESET) == 0)
778 if_printf(&sc->arpcom.ac_if, "reset never completed!\n");
783 * The following routine is designed to test for a defect on some
784 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
785 * lines connected to the bus, however for a 32-bit only card, they
786 * should be pulled high. The result of this defect is that the
787 * NIC will not work right if you plug it into a 64-bit slot: DMA
788 * operations will be done with 64-bit transfers, which will fail
789 * because the 64-bit data lines aren't connected.
791 * There's no way to work around this (short of talking a soldering
792 * iron to the board), however we can detect it. The method we use
793 * here is to put the NIC into digital loopback mode, set the receiver
794 * to promiscuous mode, and then try to send a frame. We then compare
795 * the frame data we sent to what was received. If the data matches,
796 * then the NIC is working correctly, otherwise we know the user has
797 * a defective NIC which has been mistakenly plugged into a 64-bit PCI
798 * slot. In the latter case, there's no way the NIC can work correctly,
799 * so we print out a message on the console and abort the device attach.
803 re_diag(struct re_softc *sc)
805 struct ifnet *ifp = &sc->arpcom.ac_if;
807 struct ether_header *eh;
808 struct re_desc *cur_rx;
810 int total_len, i, error = 0, phyaddr;
811 uint8_t dst[ETHER_ADDR_LEN] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
812 uint8_t src[ETHER_ADDR_LEN] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
813 char ethstr[2][ETHER_ADDRSTRLEN + 1];
815 /* Allocate a single mbuf */
817 MGETHDR(m0, MB_DONTWAIT, MT_DATA);
822 * Initialize the NIC in test mode. This sets the chip up
823 * so that it can send and receive frames, but performs the
824 * following special functions:
825 * - Puts receiver in promiscuous mode
826 * - Enables digital loopback mode
827 * - Leaves interrupts turned off
830 ifp->if_flags |= IFF_PROMISC;
831 sc->re_flags |= RE_F_TESTMODE;
833 sc->re_flags |= RE_F_LINKED;
834 if (!RE_IS_8139CP(sc))
839 re_miibus_writereg(sc->re_dev, phyaddr, MII_BMCR, BMCR_RESET);
840 for (i = 0; i < RE_TIMEOUT; i++) {
841 status = re_miibus_readreg(sc->re_dev, phyaddr, MII_BMCR);
842 if (!(status & BMCR_RESET))
846 re_miibus_writereg(sc->re_dev, phyaddr, MII_BMCR, BMCR_LOOP);
847 CSR_WRITE_2(sc, RE_ISR, RE_INTRS_DIAG);
851 /* Put some data in the mbuf */
853 eh = mtod(m0, struct ether_header *);
854 bcopy (dst, eh->ether_dhost, ETHER_ADDR_LEN);
855 bcopy (src, eh->ether_shost, ETHER_ADDR_LEN);
856 eh->ether_type = htons(ETHERTYPE_IP);
857 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
860 * Queue the packet, start transmission.
861 * Note: ifq_handoff() ultimately calls re_start() for us.
864 CSR_WRITE_2(sc, RE_ISR, 0xFFFF);
865 error = ifq_handoff(ifp, m0, NULL);
872 /* Wait for it to propagate through the chip */
875 for (i = 0; i < RE_TIMEOUT; i++) {
876 status = CSR_READ_2(sc, RE_ISR);
877 CSR_WRITE_2(sc, RE_ISR, status);
878 if ((status & (RE_ISR_TIMEOUT_EXPIRED|RE_ISR_RX_OK)) ==
879 (RE_ISR_TIMEOUT_EXPIRED|RE_ISR_RX_OK))
884 if (i == RE_TIMEOUT) {
885 if_printf(ifp, "diagnostic failed to receive packet "
886 "in loopback mode\n");
892 * The packet should have been dumped into the first
893 * entry in the RX DMA ring. Grab it from there.
896 bus_dmamap_sync(sc->re_ldata.re_rx_mtag, sc->re_ldata.re_rx_dmamap[0],
897 BUS_DMASYNC_POSTREAD);
898 bus_dmamap_unload(sc->re_ldata.re_rx_mtag,
899 sc->re_ldata.re_rx_dmamap[0]);
901 m0 = sc->re_ldata.re_rx_mbuf[0];
902 sc->re_ldata.re_rx_mbuf[0] = NULL;
903 eh = mtod(m0, struct ether_header *);
905 cur_rx = &sc->re_ldata.re_rx_list[0];
906 total_len = RE_RXBYTES(cur_rx);
908 if (total_len != ETHER_MIN_LEN) {
909 if_printf(ifp, "diagnostic failed, received short packet\n");
914 /* Test that the received packet data matches what we sent. */
916 if (bcmp(eh->ether_dhost, dst, ETHER_ADDR_LEN) ||
917 bcmp(eh->ether_shost, &src, ETHER_ADDR_LEN) ||
918 be16toh(eh->ether_type) != ETHERTYPE_IP) {
919 if_printf(ifp, "WARNING, DMA FAILURE!\n");
920 if_printf(ifp, "expected TX data: %s/%s/0x%x\n",
921 kether_ntoa(dst, ethstr[0]), kether_ntoa(src, ethstr[1]), ETHERTYPE_IP);
922 if_printf(ifp, "received RX data: %s/%s/0x%x\n",
923 kether_ntoa(eh->ether_dhost, ethstr[0]),
924 kether_ntoa(eh->ether_shost, ethstr[1]),
925 ntohs(eh->ether_type));
926 if_printf(ifp, "You may have a defective 32-bit NIC plugged "
927 "into a 64-bit PCI slot.\n");
928 if_printf(ifp, "Please re-install the NIC in a 32-bit slot "
929 "for proper operation.\n");
930 if_printf(ifp, "Read the re(4) man page for more details.\n");
935 /* Turn interface off, release resources */
937 sc->re_flags &= ~(RE_F_LINKED | RE_F_TESTMODE);
938 ifp->if_flags &= ~IFF_PROMISC;
948 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
949 * IDs against our list and return a device name if we find a match.
952 re_probe(device_t dev)
954 const struct re_type *t;
955 const struct re_hwrev *hw_rev;
958 uint32_t hwrev, macmode, txcfg;
959 uint16_t vendor, product;
961 vendor = pci_get_vendor(dev);
962 product = pci_get_device(dev);
965 * Only attach to rev.3 of the Linksys EG1032 adapter.
966 * Rev.2 is supported by sk(4).
968 if (vendor == PCI_VENDOR_LINKSYS &&
969 product == PCI_PRODUCT_LINKSYS_EG1032 &&
970 pci_get_subdevice(dev) != PCI_SUBDEVICE_LINKSYS_EG1032_REV3)
973 if (vendor == PCI_VENDOR_REALTEK &&
974 product == PCI_PRODUCT_REALTEK_RT8139 &&
975 pci_get_revid(dev) != PCI_REVID_REALTEK_RT8139CP) {
980 for (t = re_devs; t->re_name != NULL; t++) {
981 if (product == t->re_did && vendor == t->re_vid)
986 * Check if we found a RealTek device.
988 if (t->re_name == NULL)
992 * Temporarily map the I/O space so we can read the chip ID register.
994 sc = kmalloc(sizeof(*sc), M_TEMP, M_WAITOK | M_ZERO);
996 sc->re_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
998 if (sc->re_res == NULL) {
999 device_printf(dev, "couldn't map ports/memory\n");
1004 sc->re_btag = rman_get_bustag(sc->re_res);
1005 sc->re_bhandle = rman_get_bushandle(sc->re_res);
1007 txcfg = CSR_READ_4(sc, RE_TXCFG);
1008 hwrev = txcfg & RE_TXCFG_HWREV;
1009 macmode = txcfg & RE_TXCFG_MACMODE;
1010 bus_release_resource(dev, SYS_RES_IOPORT, RE_PCI_LOIO, sc->re_res);
1014 * and continue matching for the specific chip...
1016 for (hw_rev = re_hwrevs; hw_rev->re_hwrev != RE_HWREV_NULL; hw_rev++) {
1017 if (hw_rev->re_hwrev == hwrev) {
1018 sc = device_get_softc(dev);
1020 sc->re_hwrev = hw_rev->re_hwrev;
1021 sc->re_caps = hw_rev->re_caps;
1022 sc->re_maxmtu = hw_rev->re_maxmtu;
1025 * Apply chip property fixup
1027 switch (sc->re_hwrev) {
1028 case RE_HWREV_8168GU:
1029 if (vendor == PCI_VENDOR_REALTEK &&
1030 product == PCI_PRODUCT_REALTEK_RT8101E) {
1032 sc->re_caps = RE_C_HWCSUM | RE_C_MAC2 |
1033 RE_C_PHYPMGT | RE_C_AUTOPAD |
1034 RE_C_STOP_RXTX | RE_C_FASTE;
1035 sc->re_maxmtu = ETHERMTU;
1036 device_printf(dev, "8106EUS fixup\n");
1043 case RE_HWREV_8168E:
1044 if (vendor == PCI_VENDOR_REALTEK &&
1045 product == PCI_PRODUCT_REALTEK_RT8101E) {
1047 sc->re_caps = RE_C_HWCSUM | RE_C_MAC2 |
1048 RE_C_PHYPMGT | RE_C_AUTOPAD |
1049 RE_C_STOP_RXTX | RE_C_FASTE;
1050 sc->re_maxmtu = ETHERMTU;
1051 device_printf(dev, "8105E fixup\n");
1057 case RE_HWREV_8101E:
1058 case RE_HWREV_8102E:
1059 case RE_HWREV_8102EL:
1060 case RE_HWREV_8401E:
1061 case RE_HWREV_8105E:
1062 case RE_HWREV_8106E:
1064 sc->re_caps |= RE_C_EE_EADDR;
1065 sc->re_ee_eaddr = RE_EE_EADDR0;
1068 case RE_HWREV_8168F:
1069 case RE_HWREV_8111F:
1070 case RE_HWREV_8168G:
1072 macmode == 0x100000) {
1073 sc->re_caps |= RE_C_EE_EADDR;
1074 sc->re_ee_eaddr = RE_EE_EADDR1;
1079 case RE_HWREV_8168EP:
1080 case RE_HWREV_8411B:
1082 sc->re_caps |= RE_C_EE_EADDR;
1083 sc->re_ee_eaddr = RE_EE_EADDR1;
1086 if (pci_is_pcie(dev))
1087 sc->re_caps |= RE_C_PCIE;
1089 device_set_desc(dev, t->re_name);
1093 device_printf(dev, "unknown hwrev 0x%08x, macmode 0x%08x\n",
1100 re_allocmem(device_t dev)
1102 struct re_softc *sc = device_get_softc(dev);
1107 * Allocate list data
1109 sc->re_ldata.re_tx_mbuf =
1110 kmalloc(sc->re_tx_desc_cnt * sizeof(struct mbuf *),
1111 M_DEVBUF, M_ZERO | M_WAITOK);
1113 sc->re_ldata.re_rx_mbuf =
1114 kmalloc(sc->re_rx_desc_cnt * sizeof(struct mbuf *),
1115 M_DEVBUF, M_ZERO | M_WAITOK);
1117 sc->re_ldata.re_rx_paddr =
1118 kmalloc(sc->re_rx_desc_cnt * sizeof(bus_addr_t),
1119 M_DEVBUF, M_ZERO | M_WAITOK);
1121 sc->re_ldata.re_tx_dmamap =
1122 kmalloc(sc->re_tx_desc_cnt * sizeof(bus_dmamap_t),
1123 M_DEVBUF, M_ZERO | M_WAITOK);
1125 sc->re_ldata.re_rx_dmamap =
1126 kmalloc(sc->re_rx_desc_cnt * sizeof(bus_dmamap_t),
1127 M_DEVBUF, M_ZERO | M_WAITOK);
1130 * Allocate the parent bus DMA tag appropriate for PCI.
1132 error = bus_dma_tag_create(NULL, /* parent */
1133 1, 0, /* alignment, boundary */
1134 BUS_SPACE_MAXADDR, /* lowaddr */
1135 BUS_SPACE_MAXADDR, /* highaddr */
1136 NULL, NULL, /* filter, filterarg */
1137 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
1139 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1141 &sc->re_parent_tag);
1143 device_printf(dev, "could not allocate parent dma tag\n");
1147 /* Allocate TX descriptor list. */
1148 error = bus_dmamem_coherent(sc->re_parent_tag,
1150 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1151 RE_TX_LIST_SZ(sc), BUS_DMA_WAITOK | BUS_DMA_ZERO,
1154 device_printf(dev, "could not allocate TX ring\n");
1157 sc->re_ldata.re_tx_list_tag = dmem.dmem_tag;
1158 sc->re_ldata.re_tx_list_map = dmem.dmem_map;
1159 sc->re_ldata.re_tx_list = dmem.dmem_addr;
1160 sc->re_ldata.re_tx_list_addr = dmem.dmem_busaddr;
1162 /* Allocate RX descriptor list. */
1163 error = bus_dmamem_coherent(sc->re_parent_tag,
1165 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1166 RE_RX_LIST_SZ(sc), BUS_DMA_WAITOK | BUS_DMA_ZERO,
1169 device_printf(dev, "could not allocate RX ring\n");
1172 sc->re_ldata.re_rx_list_tag = dmem.dmem_tag;
1173 sc->re_ldata.re_rx_list_map = dmem.dmem_map;
1174 sc->re_ldata.re_rx_list = dmem.dmem_addr;
1175 sc->re_ldata.re_rx_list_addr = dmem.dmem_busaddr;
1177 /* Allocate maps for TX mbufs. */
1178 error = bus_dma_tag_create(sc->re_parent_tag,
1180 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1182 RE_FRAMELEN_MAX, RE_MAXSEGS, MCLBYTES,
1183 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1184 &sc->re_ldata.re_tx_mtag);
1186 device_printf(dev, "could not allocate TX buf dma tag\n");
1190 /* Create DMA maps for TX buffers */
1191 for (i = 0; i < sc->re_tx_desc_cnt; i++) {
1192 error = bus_dmamap_create(sc->re_ldata.re_tx_mtag,
1193 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
1194 &sc->re_ldata.re_tx_dmamap[i]);
1196 device_printf(dev, "can't create DMA map for TX buf\n");
1197 re_freebufmem(sc, i, 0);
1202 /* Allocate maps for RX mbufs. */
1203 error = bus_dma_tag_create(sc->re_parent_tag,
1205 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1207 MCLBYTES, 1, MCLBYTES,
1208 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ALIGNED,
1209 &sc->re_ldata.re_rx_mtag);
1211 device_printf(dev, "could not allocate RX buf dma tag\n");
1215 /* Create spare DMA map for RX */
1216 error = bus_dmamap_create(sc->re_ldata.re_rx_mtag, BUS_DMA_WAITOK,
1217 &sc->re_ldata.re_rx_spare);
1219 device_printf(dev, "can't create spare DMA map for RX\n");
1220 bus_dma_tag_destroy(sc->re_ldata.re_rx_mtag);
1221 sc->re_ldata.re_rx_mtag = NULL;
1225 /* Create DMA maps for RX buffers */
1226 for (i = 0; i < sc->re_rx_desc_cnt; i++) {
1227 error = bus_dmamap_create(sc->re_ldata.re_rx_mtag,
1228 BUS_DMA_WAITOK, &sc->re_ldata.re_rx_dmamap[i]);
1230 device_printf(dev, "can't create DMA map for RX buf\n");
1231 re_freebufmem(sc, sc->re_tx_desc_cnt, i);
1236 /* Create jumbo buffer pool for RX if required */
1237 if (sc->re_caps & RE_C_CONTIGRX) {
1238 error = re_jpool_alloc(sc);
1241 /* Disable jumbo frame support */
1242 sc->re_maxmtu = ETHERMTU;
1249 re_freebufmem(struct re_softc *sc, int tx_cnt, int rx_cnt)
1253 /* Destroy all the RX and TX buffer maps */
1254 if (sc->re_ldata.re_tx_mtag) {
1255 for (i = 0; i < tx_cnt; i++) {
1256 bus_dmamap_destroy(sc->re_ldata.re_tx_mtag,
1257 sc->re_ldata.re_tx_dmamap[i]);
1259 bus_dma_tag_destroy(sc->re_ldata.re_tx_mtag);
1260 sc->re_ldata.re_tx_mtag = NULL;
1263 if (sc->re_ldata.re_rx_mtag) {
1264 for (i = 0; i < rx_cnt; i++) {
1265 bus_dmamap_destroy(sc->re_ldata.re_rx_mtag,
1266 sc->re_ldata.re_rx_dmamap[i]);
1268 bus_dmamap_destroy(sc->re_ldata.re_rx_mtag,
1269 sc->re_ldata.re_rx_spare);
1270 bus_dma_tag_destroy(sc->re_ldata.re_rx_mtag);
1271 sc->re_ldata.re_rx_mtag = NULL;
1276 re_freemem(device_t dev)
1278 struct re_softc *sc = device_get_softc(dev);
1280 /* Unload and free the RX DMA ring memory and map */
1281 if (sc->re_ldata.re_rx_list_tag) {
1282 bus_dmamap_unload(sc->re_ldata.re_rx_list_tag,
1283 sc->re_ldata.re_rx_list_map);
1284 bus_dmamem_free(sc->re_ldata.re_rx_list_tag,
1285 sc->re_ldata.re_rx_list,
1286 sc->re_ldata.re_rx_list_map);
1287 bus_dma_tag_destroy(sc->re_ldata.re_rx_list_tag);
1290 /* Unload and free the TX DMA ring memory and map */
1291 if (sc->re_ldata.re_tx_list_tag) {
1292 bus_dmamap_unload(sc->re_ldata.re_tx_list_tag,
1293 sc->re_ldata.re_tx_list_map);
1294 bus_dmamem_free(sc->re_ldata.re_tx_list_tag,
1295 sc->re_ldata.re_tx_list,
1296 sc->re_ldata.re_tx_list_map);
1297 bus_dma_tag_destroy(sc->re_ldata.re_tx_list_tag);
1300 /* Free RX/TX buf DMA stuffs */
1301 re_freebufmem(sc, sc->re_tx_desc_cnt, sc->re_rx_desc_cnt);
1303 /* Unload and free the stats buffer and map */
1304 if (sc->re_ldata.re_stag) {
1305 bus_dmamap_unload(sc->re_ldata.re_stag, sc->re_ldata.re_smap);
1306 bus_dmamem_free(sc->re_ldata.re_stag,
1307 sc->re_ldata.re_stats,
1308 sc->re_ldata.re_smap);
1309 bus_dma_tag_destroy(sc->re_ldata.re_stag);
1312 if (sc->re_caps & RE_C_CONTIGRX)
1315 if (sc->re_parent_tag)
1316 bus_dma_tag_destroy(sc->re_parent_tag);
1318 if (sc->re_ldata.re_tx_mbuf != NULL)
1319 kfree(sc->re_ldata.re_tx_mbuf, M_DEVBUF);
1320 if (sc->re_ldata.re_rx_mbuf != NULL)
1321 kfree(sc->re_ldata.re_rx_mbuf, M_DEVBUF);
1322 if (sc->re_ldata.re_rx_paddr != NULL)
1323 kfree(sc->re_ldata.re_rx_paddr, M_DEVBUF);
1324 if (sc->re_ldata.re_tx_dmamap != NULL)
1325 kfree(sc->re_ldata.re_tx_dmamap, M_DEVBUF);
1326 if (sc->re_ldata.re_rx_dmamap != NULL)
1327 kfree(sc->re_ldata.re_rx_dmamap, M_DEVBUF);
1331 * Attach the interface. Allocate softc structures, do ifmedia
1332 * setup and ethernet/BPF attach.
1335 re_attach(device_t dev)
1337 struct re_softc *sc = device_get_softc(dev);
1339 struct sysctl_ctx_list *ctx;
1340 struct sysctl_oid *tree;
1341 uint8_t eaddr[ETHER_ADDR_LEN];
1342 int error = 0, qlen, msi_enable;
1345 callout_init_mp(&sc->re_timer);
1348 if (RE_IS_8139CP(sc)) {
1349 sc->re_rx_desc_cnt = RE_RX_DESC_CNT_8139CP;
1350 sc->re_tx_desc_cnt = RE_TX_DESC_CNT_8139CP;
1352 sc->re_rx_desc_cnt = re_rx_desc_count;
1353 if (sc->re_rx_desc_cnt > RE_RX_DESC_CNT_MAX)
1354 sc->re_rx_desc_cnt = RE_RX_DESC_CNT_MAX;
1356 sc->re_tx_desc_cnt = re_tx_desc_count;
1357 if (sc->re_tx_desc_cnt > RE_TX_DESC_CNT_MAX)
1358 sc->re_tx_desc_cnt = RE_TX_DESC_CNT_MAX;
1361 qlen = RE_IFQ_MAXLEN;
1362 if (sc->re_tx_desc_cnt > qlen)
1363 qlen = sc->re_tx_desc_cnt;
1365 sc->re_rxbuf_size = MCLBYTES;
1366 sc->re_newbuf = re_newbuf_std;
1368 sc->re_tx_time = 5; /* 125us */
1369 sc->re_rx_time = 2; /* 50us */
1370 if (sc->re_caps & RE_C_PCIE)
1371 sc->re_sim_time = 75; /* 75us */
1373 sc->re_sim_time = 125; /* 125us */
1374 if (!RE_IS_8139CP(sc)) {
1375 /* simulated interrupt moderation */
1376 sc->re_imtype = RE_IMTYPE_SIM;
1378 sc->re_imtype = RE_IMTYPE_NONE;
1380 re_config_imtype(sc, sc->re_imtype);
1382 ctx = device_get_sysctl_ctx(dev);
1383 tree = device_get_sysctl_tree(dev);
1384 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1385 "rx_desc_count", CTLFLAG_RD, &sc->re_rx_desc_cnt,
1386 0, "RX desc count");
1387 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1388 "tx_desc_count", CTLFLAG_RD, &sc->re_tx_desc_cnt,
1389 0, "TX desc count");
1390 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "sim_time",
1391 CTLTYPE_INT | CTLFLAG_RW,
1392 sc, 0, re_sysctl_simtime, "I",
1393 "Simulated interrupt moderation time (usec).");
1394 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "imtype",
1395 CTLTYPE_INT | CTLFLAG_RW,
1396 sc, 0, re_sysctl_imtype, "I",
1397 "Interrupt moderation type -- "
1398 "0:disable, 1:simulated, "
1399 "2:hardware(if supported)");
1400 if (sc->re_caps & RE_C_HWIM) {
1401 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1402 OID_AUTO, "hw_rxtime",
1403 CTLTYPE_INT | CTLFLAG_RW,
1404 sc, 0, re_sysctl_rxtime, "I",
1405 "Hardware interrupt moderation time "
1407 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
1408 OID_AUTO, "hw_txtime",
1409 CTLTYPE_INT | CTLFLAG_RW,
1410 sc, 0, re_sysctl_txtime, "I",
1411 "Hardware interrupt moderation time "
1415 #ifndef BURN_BRIDGES
1417 * Handle power management nonsense.
1420 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1421 uint32_t membase, irq;
1423 /* Save important PCI config data. */
1424 membase = pci_read_config(dev, RE_PCI_LOMEM, 4);
1425 irq = pci_read_config(dev, PCIR_INTLINE, 4);
1427 /* Reset the power state. */
1428 device_printf(dev, "chip is in D%d power mode "
1429 "-- setting to D0\n", pci_get_powerstate(dev));
1431 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1433 /* Restore PCI config data. */
1434 pci_write_config(dev, RE_PCI_LOMEM, membase, 4);
1435 pci_write_config(dev, PCIR_INTLINE, irq, 4);
1439 * Map control/status registers.
1441 pci_enable_busmaster(dev);
1443 if (pci_is_pcie(dev)) {
1444 sc->re_res_rid = PCIR_BAR(2);
1445 sc->re_res_type = SYS_RES_MEMORY;
1447 sc->re_res_rid = PCIR_BAR(0);
1448 sc->re_res_type = SYS_RES_IOPORT;
1450 sc->re_res = bus_alloc_resource_any(dev, sc->re_res_type,
1451 &sc->re_res_rid, RF_ACTIVE);
1452 if (sc->re_res == NULL) {
1453 device_printf(dev, "couldn't map IO\n");
1458 sc->re_btag = rman_get_bustag(sc->re_res);
1459 sc->re_bhandle = rman_get_bushandle(sc->re_res);
1462 * Allocate interrupt
1464 if (pci_is_pcie(dev))
1465 msi_enable = re_msi_enable;
1468 sc->re_irq_type = pci_alloc_1intr(dev, msi_enable,
1469 &sc->re_irq_rid, &irq_flags);
1471 sc->re_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->re_irq_rid,
1473 if (sc->re_irq == NULL) {
1474 device_printf(dev, "couldn't map interrupt\n");
1479 /* Reset the adapter. */
1482 if (RE_IS_8139CP(sc)) {
1483 sc->re_bus_speed = 33; /* XXX */
1484 } else if (sc->re_caps & RE_C_PCIE) {
1485 sc->re_bus_speed = 125;
1489 cfg2 = CSR_READ_1(sc, RE_CFG2);
1490 switch (cfg2 & RE_CFG2_PCICLK_MASK) {
1491 case RE_CFG2_PCICLK_33MHZ:
1492 sc->re_bus_speed = 33;
1494 case RE_CFG2_PCICLK_66MHZ:
1495 sc->re_bus_speed = 66;
1498 device_printf(dev, "unknown bus speed, assume 33MHz\n");
1499 sc->re_bus_speed = 33;
1502 if (cfg2 & RE_CFG2_PCI64)
1503 sc->re_caps |= RE_C_PCI64;
1505 device_printf(dev, "Hardware rev. 0x%08x; PCI%s %dMHz\n",
1507 (sc->re_caps & RE_C_PCIE) ?
1508 "-E" : ((sc->re_caps & RE_C_PCI64) ? "64" : "32"),
1513 * DO NOT try to adjust config1 and config5 which was spotted in
1514 * Realtek's Linux drivers. It will _permanently_ damage certain
1515 * cards EEPROM, e.g. one of my 8168B (0x38000000) card ...
1518 re_get_eaddr(sc, eaddr);
1520 if (!RE_IS_8139CP(sc)) {
1521 /* Set RX length mask */
1522 sc->re_rxlenmask = RE_RDESC_STAT_GFRAGLEN;
1523 sc->re_txstart = RE_GTXSTART;
1525 /* Set RX length mask */
1526 sc->re_rxlenmask = RE_RDESC_STAT_FRAGLEN;
1527 sc->re_txstart = RE_TXSTART;
1530 /* Allocate DMA stuffs */
1531 error = re_allocmem(dev);
1536 * Apply some magic PCI settings from Realtek ...
1538 if (RE_IS_8169(sc)) {
1539 CSR_WRITE_1(sc, 0x82, 1);
1540 pci_write_config(dev, PCIR_CACHELNSZ, 0x8, 1);
1542 pci_write_config(dev, PCIR_LATTIMER, 0x40, 1);
1544 if (sc->re_caps & RE_C_MAC2) {
1546 * Following part is extracted from Realtek BSD driver v176.
1547 * However, this does _not_ make much/any sense:
1548 * 8168C's PCI Express device control is located at 0x78,
1549 * so the reading from 0x79 (higher part of 0x78) and setting
1550 * the 4~6bits intend to enlarge the "max read request size"
1551 * (we will do it). The content of the rest part of this
1552 * register is not meaningful to other PCI registers, so
1553 * writing the value to 0x54 could be completely wrong.
1554 * 0x80 is the lower part of PCI Express device status, non-
1555 * reserved bits are RW1C, writing 0 to them will not have
1556 * any effect at all.
1561 val = pci_read_config(dev, 0x79, 1);
1562 val = (val & ~0x70) | 0x50;
1563 pci_write_config(dev, 0x54, val, 1);
1564 pci_write_config(dev, 0x80, 0, 1);
1569 * Apply some PHY fixup from Realtek ...
1571 if (sc->re_hwrev == RE_HWREV_8110S) {
1572 CSR_WRITE_1(sc, 0x82, 1);
1573 re_miibus_writereg(dev, 1, 0xb, 0);
1575 if (sc->re_caps & RE_C_PHYPMGT) {
1577 re_miibus_writereg(dev, 1, 0x1f, 0);
1578 re_miibus_writereg(dev, 1, 0xe, 0);
1582 if (mii_phy_probe(dev, &sc->re_miibus,
1583 re_ifmedia_upd, re_ifmedia_sts)) {
1584 device_printf(dev, "MII without any phy!\n");
1589 ifp = &sc->arpcom.ac_if;
1591 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1592 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1593 ifp->if_ioctl = re_ioctl;
1594 ifp->if_start = re_start;
1595 #ifdef IFPOLL_ENABLE
1596 ifp->if_npoll = re_npoll;
1598 ifp->if_watchdog = re_watchdog;
1599 ifp->if_init = re_init;
1600 if (!RE_IS_8139CP(sc)) /* XXX */
1601 ifp->if_baudrate = 1000000000;
1603 ifp->if_baudrate = 100000000;
1604 ifq_set_maxlen(&ifp->if_snd, qlen);
1605 ifq_set_ready(&ifp->if_snd);
1607 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1608 if (sc->re_caps & RE_C_HWCSUM)
1609 ifp->if_capabilities |= IFCAP_HWCSUM;
1611 ifp->if_capenable = ifp->if_capabilities;
1612 if (ifp->if_capabilities & IFCAP_HWCSUM) {
1614 * RTL8168/8111C generates wrong IP checksummed frame if the
1615 * packet has IP options so disable TX IP checksum offloading.
1617 if (sc->re_hwrev == RE_HWREV_8168CP ||
1618 sc->re_hwrev == RE_HWREV_8168C)
1619 sc->re_hwassist = CSUM_TCP | CSUM_UDP;
1621 sc->re_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1623 ifp->if_hwassist = sc->re_hwassist;
1626 * Call MI attach routine.
1628 ether_ifattach(ifp, eaddr, NULL);
1630 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->re_irq));
1632 #ifdef IFPOLL_ENABLE
1633 ifpoll_compat_setup(&sc->re_npoll, ctx, (struct sysctl_oid *)tree,
1634 device_get_unit(dev), ifp->if_serializer);
1639 * Perform hardware diagnostic on the original RTL8169.
1640 * Some 32-bit cards were incorrectly wired and would
1641 * malfunction if plugged into a 64-bit slot.
1643 if (sc->re_hwrev == RE_HWREV_8169) {
1644 lwkt_serialize_enter(ifp->if_serializer);
1645 error = re_diag(sc);
1646 lwkt_serialize_exit(ifp->if_serializer);
1649 device_printf(dev, "hardware diagnostic failure\n");
1650 ether_ifdetach(ifp);
1654 #endif /* RE_DIAG */
1656 /* Hook interrupt last to avoid having to lock softc */
1657 error = bus_setup_intr(dev, sc->re_irq, INTR_MPSAFE, re_intr, sc,
1658 &sc->re_intrhand, ifp->if_serializer);
1661 device_printf(dev, "couldn't set up irq\n");
1662 ether_ifdetach(ifp);
1674 * Shutdown hardware and free up resources. This can be called any
1675 * time after the mutex has been initialized. It is called in both
1676 * the error case in attach and the normal detach case so it needs
1677 * to be careful about only freeing resources that have actually been
1681 re_detach(device_t dev)
1683 struct re_softc *sc = device_get_softc(dev);
1684 struct ifnet *ifp = &sc->arpcom.ac_if;
1686 /* These should only be active if attach succeeded */
1687 if (device_is_attached(dev)) {
1688 lwkt_serialize_enter(ifp->if_serializer);
1690 bus_teardown_intr(dev, sc->re_irq, sc->re_intrhand);
1691 lwkt_serialize_exit(ifp->if_serializer);
1693 ether_ifdetach(ifp);
1696 device_delete_child(dev, sc->re_miibus);
1697 bus_generic_detach(dev);
1700 bus_release_resource(dev, SYS_RES_IRQ, sc->re_irq_rid,
1703 if (sc->re_irq_type == PCI_INTR_TYPE_MSI)
1704 pci_release_msi(dev);
1707 bus_release_resource(dev, sc->re_res_type, sc->re_res_rid,
1711 /* Free DMA stuffs */
1718 re_setup_rxdesc(struct re_softc *sc, int idx)
1724 paddr = sc->re_ldata.re_rx_paddr[idx];
1725 d = &sc->re_ldata.re_rx_list[idx];
1727 d->re_bufaddr_lo = htole32(RE_ADDR_LO(paddr));
1728 d->re_bufaddr_hi = htole32(RE_ADDR_HI(paddr));
1730 cmdstat = sc->re_rxbuf_size | RE_RDESC_CMD_OWN;
1731 if (idx == (sc->re_rx_desc_cnt - 1))
1732 cmdstat |= RE_RDESC_CMD_EOR;
1733 d->re_cmdstat = htole32(cmdstat);
1737 re_newbuf_std(struct re_softc *sc, int idx, int init)
1739 bus_dma_segment_t seg;
1744 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
1749 if_printf(&sc->arpcom.ac_if, "m_getcl failed\n");
1755 m->m_len = m->m_pkthdr.len = MCLBYTES;
1759 * re(4) chips need address of the receive buffer to be 8-byte
1760 * aligned, so don't call m_adj(m, ETHER_ALIGN) here.
1763 error = bus_dmamap_load_mbuf_segment(sc->re_ldata.re_rx_mtag,
1764 sc->re_ldata.re_rx_spare, m,
1765 &seg, 1, &nsegs, BUS_DMA_NOWAIT);
1769 if_printf(&sc->arpcom.ac_if, "can't load RX mbuf\n");
1777 bus_dmamap_sync(sc->re_ldata.re_rx_mtag,
1778 sc->re_ldata.re_rx_dmamap[idx],
1779 BUS_DMASYNC_POSTREAD);
1780 bus_dmamap_unload(sc->re_ldata.re_rx_mtag,
1781 sc->re_ldata.re_rx_dmamap[idx]);
1783 sc->re_ldata.re_rx_mbuf[idx] = m;
1784 sc->re_ldata.re_rx_paddr[idx] = seg.ds_addr;
1786 map = sc->re_ldata.re_rx_dmamap[idx];
1787 sc->re_ldata.re_rx_dmamap[idx] = sc->re_ldata.re_rx_spare;
1788 sc->re_ldata.re_rx_spare = map;
1790 re_setup_rxdesc(sc, idx);
1795 re_newbuf_jumbo(struct re_softc *sc, int idx, int init)
1798 struct re_jbuf *jbuf;
1801 MGETHDR(m, init ? MB_WAIT : MB_DONTWAIT, MT_DATA);
1805 if_printf(&sc->arpcom.ac_if, "MGETHDR failed\n");
1812 jbuf = re_jbuf_alloc(sc);
1818 if_printf(&sc->arpcom.ac_if, "jpool is empty\n");
1825 m->m_ext.ext_arg = jbuf;
1826 m->m_ext.ext_buf = jbuf->re_buf;
1827 m->m_ext.ext_free = re_jbuf_free;
1828 m->m_ext.ext_ref = re_jbuf_ref;
1829 m->m_ext.ext_size = sc->re_rxbuf_size;
1831 m->m_data = m->m_ext.ext_buf;
1832 m->m_flags |= M_EXT;
1833 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1837 * Some re(4) chips(e.g. RTL8101E) need address of the receive buffer
1838 * to be 8-byte aligned, so don't call m_adj(m, ETHER_ALIGN) here.
1841 sc->re_ldata.re_rx_mbuf[idx] = m;
1842 sc->re_ldata.re_rx_paddr[idx] = jbuf->re_paddr;
1844 re_setup_rxdesc(sc, idx);
1849 re_tx_list_init(struct re_softc *sc)
1851 bzero(sc->re_ldata.re_tx_list, RE_TX_LIST_SZ(sc));
1853 sc->re_ldata.re_tx_prodidx = 0;
1854 sc->re_ldata.re_tx_considx = 0;
1855 sc->re_ldata.re_tx_free = sc->re_tx_desc_cnt;
1861 re_rx_list_init(struct re_softc *sc)
1865 bzero(sc->re_ldata.re_rx_list, RE_RX_LIST_SZ(sc));
1867 for (i = 0; i < sc->re_rx_desc_cnt; i++) {
1868 error = sc->re_newbuf(sc, i, 1);
1873 sc->re_ldata.re_rx_prodidx = 0;
1874 sc->re_head = sc->re_tail = NULL;
1879 #define RE_IP4_PACKET 0x1
1880 #define RE_TCP_PACKET 0x2
1881 #define RE_UDP_PACKET 0x4
1883 static __inline uint8_t
1884 re_packet_type(struct re_softc *sc, uint32_t rxstat, uint32_t rxctrl)
1886 uint8_t packet_type = 0;
1888 if (sc->re_caps & RE_C_MAC2) {
1889 if (rxctrl & RE_RDESC_CTL_PROTOIP4)
1890 packet_type |= RE_IP4_PACKET;
1892 if (rxstat & RE_RDESC_STAT_PROTOID)
1893 packet_type |= RE_IP4_PACKET;
1895 if (RE_TCPPKT(rxstat))
1896 packet_type |= RE_TCP_PACKET;
1897 else if (RE_UDPPKT(rxstat))
1898 packet_type |= RE_UDP_PACKET;
1903 * RX handler for C+ and 8169. For the gigE chips, we support
1904 * the reception of jumbo frames that have been fragmented
1905 * across multiple 2K mbuf cluster buffers.
1908 re_rxeof(struct re_softc *sc)
1910 struct ifnet *ifp = &sc->arpcom.ac_if;
1912 struct re_desc *cur_rx;
1913 uint32_t rxstat, rxctrl;
1914 int i, total_len, rx = 0;
1916 for (i = sc->re_ldata.re_rx_prodidx;
1917 RE_OWN(&sc->re_ldata.re_rx_list[i]) == 0; RE_RXDESC_INC(sc, i)) {
1918 cur_rx = &sc->re_ldata.re_rx_list[i];
1919 m = sc->re_ldata.re_rx_mbuf[i];
1920 total_len = RE_RXBYTES(cur_rx);
1921 rxstat = le32toh(cur_rx->re_cmdstat);
1922 rxctrl = le32toh(cur_rx->re_control);
1927 if (sc->re_flags & RE_F_USE_JPOOL)
1928 KKASSERT(rxstat & RE_RDESC_STAT_EOF);
1931 if ((rxstat & RE_RDESC_STAT_EOF) == 0) {
1932 if (sc->re_flags & RE_F_DROP_RXFRAG) {
1933 re_setup_rxdesc(sc, i);
1937 if (sc->re_newbuf(sc, i, 0)) {
1938 /* Drop upcoming fragments */
1939 sc->re_flags |= RE_F_DROP_RXFRAG;
1943 m->m_len = MCLBYTES;
1944 if (sc->re_head == NULL) {
1945 sc->re_head = sc->re_tail = m;
1947 sc->re_tail->m_next = m;
1951 } else if (sc->re_flags & RE_F_DROP_RXFRAG) {
1953 * Last fragment of a multi-fragment packet.
1955 * Since error already happened, this fragment
1956 * must be dropped as well as the fragment chain.
1958 re_setup_rxdesc(sc, i);
1959 re_free_rxchain(sc);
1960 sc->re_flags &= ~RE_F_DROP_RXFRAG;
1965 * NOTE: for the 8139C+, the frame length field
1966 * is always 12 bits in size, but for the gigE chips,
1967 * it is 13 bits (since the max RX frame length is 16K).
1968 * Unfortunately, all 32 bits in the status word
1969 * were already used, so to make room for the extra
1970 * length bit, RealTek took out the 'frame alignment
1971 * error' bit and shifted the other status bits
1972 * over one slot. The OWN, EOR, FS and LS bits are
1973 * still in the same places. We have already extracted
1974 * the frame length and checked the OWN bit, so rather
1975 * than using an alternate bit mapping, we shift the
1976 * status bits one space to the right so we can evaluate
1977 * them using the 8169 status as though it was in the
1978 * same format as that of the 8139C+.
1980 if (!RE_IS_8139CP(sc))
1983 if (rxstat & RE_RDESC_STAT_RXERRSUM) {
1984 IFNET_STAT_INC(ifp, ierrors, 1);
1986 * If this is part of a multi-fragment packet,
1987 * discard all the pieces.
1989 re_free_rxchain(sc);
1990 re_setup_rxdesc(sc, i);
1995 * If allocating a replacement mbuf fails,
1996 * reload the current one.
1999 if (sc->re_newbuf(sc, i, 0)) {
2000 IFNET_STAT_INC(ifp, ierrors, 1);
2004 if (sc->re_head != NULL) {
2005 m->m_len = total_len % MCLBYTES;
2007 * Special case: if there's 4 bytes or less
2008 * in this buffer, the mbuf can be discarded:
2009 * the last 4 bytes is the CRC, which we don't
2010 * care about anyway.
2012 if (m->m_len <= ETHER_CRC_LEN) {
2013 sc->re_tail->m_len -=
2014 (ETHER_CRC_LEN - m->m_len);
2017 m->m_len -= ETHER_CRC_LEN;
2018 sc->re_tail->m_next = m;
2021 sc->re_head = sc->re_tail = NULL;
2022 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2024 m->m_pkthdr.len = m->m_len =
2025 (total_len - ETHER_CRC_LEN);
2028 IFNET_STAT_INC(ifp, ipackets, 1);
2029 m->m_pkthdr.rcvif = ifp;
2031 /* Do RX checksumming if enabled */
2033 if (ifp->if_capenable & IFCAP_RXCSUM) {
2034 uint8_t packet_type;
2036 packet_type = re_packet_type(sc, rxstat, rxctrl);
2038 /* Check IP header checksum */
2039 if (packet_type & RE_IP4_PACKET) {
2040 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2041 if ((rxstat & RE_RDESC_STAT_IPSUMBAD) == 0)
2042 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2045 /* Check TCP/UDP checksum */
2046 if (((packet_type & RE_TCP_PACKET) &&
2047 (rxstat & RE_RDESC_STAT_TCPSUMBAD) == 0) ||
2048 ((packet_type & RE_UDP_PACKET) &&
2049 (rxstat & RE_RDESC_STAT_UDPSUMBAD) == 0)) {
2050 m->m_pkthdr.csum_flags |=
2051 CSUM_DATA_VALID|CSUM_PSEUDO_HDR|
2052 CSUM_FRAG_NOT_CHECKED;
2053 m->m_pkthdr.csum_data = 0xffff;
2057 if (rxctrl & RE_RDESC_CTL_HASTAG) {
2058 m->m_flags |= M_VLANTAG;
2059 m->m_pkthdr.ether_vlantag =
2060 be16toh((rxctrl & RE_RDESC_CTL_TAGDATA));
2062 ifp->if_input(ifp, m, NULL, -1);
2065 sc->re_ldata.re_rx_prodidx = i;
2070 #undef RE_IP4_PACKET
2071 #undef RE_TCP_PACKET
2072 #undef RE_UDP_PACKET
2075 re_tx_collect(struct re_softc *sc)
2077 struct ifnet *ifp = &sc->arpcom.ac_if;
2081 for (idx = sc->re_ldata.re_tx_considx;
2082 sc->re_ldata.re_tx_free < sc->re_tx_desc_cnt;
2083 RE_TXDESC_INC(sc, idx)) {
2084 txstat = le32toh(sc->re_ldata.re_tx_list[idx].re_cmdstat);
2085 if (txstat & RE_TDESC_CMD_OWN)
2090 sc->re_ldata.re_tx_list[idx].re_bufaddr_lo = 0;
2093 * We only stash mbufs in the last descriptor
2094 * in a fragment chain, which also happens to
2095 * be the only place where the TX status bits
2098 if (txstat & RE_TDESC_CMD_EOF) {
2099 bus_dmamap_unload(sc->re_ldata.re_tx_mtag,
2100 sc->re_ldata.re_tx_dmamap[idx]);
2101 m_freem(sc->re_ldata.re_tx_mbuf[idx]);
2102 sc->re_ldata.re_tx_mbuf[idx] = NULL;
2103 if (txstat & (RE_TDESC_STAT_EXCESSCOL|
2104 RE_TDESC_STAT_COLCNT))
2105 IFNET_STAT_INC(ifp, collisions, 1);
2106 if (txstat & RE_TDESC_STAT_TXERRSUM)
2107 IFNET_STAT_INC(ifp, oerrors, 1);
2109 IFNET_STAT_INC(ifp, opackets, 1);
2111 sc->re_ldata.re_tx_free++;
2113 sc->re_ldata.re_tx_considx = idx;
2119 re_txeof(struct re_softc *sc)
2121 struct ifnet *ifp = &sc->arpcom.ac_if;
2124 tx = re_tx_collect(sc);
2126 /* There is enough free TX descs */
2127 if (sc->re_ldata.re_tx_free > RE_TXDESC_SPARE)
2128 ifq_clr_oactive(&ifp->if_snd);
2131 * Some chips will ignore a second TX request issued while an
2132 * existing transmission is in progress. If the transmitter goes
2133 * idle but there are still packets waiting to be sent, we need
2134 * to restart the channel here to flush them out. This only seems
2135 * to be required with the PCIe devices.
2137 if (sc->re_ldata.re_tx_free < sc->re_tx_desc_cnt)
2138 CSR_WRITE_1(sc, sc->re_txstart, RE_TXSTART_START);
2148 struct re_softc *sc = xsc;
2150 lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
2151 re_tick_serialized(xsc);
2152 lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
2156 re_tick_serialized(void *xsc)
2158 struct re_softc *sc = xsc;
2159 struct ifnet *ifp = &sc->arpcom.ac_if;
2160 struct mii_data *mii;
2162 ASSERT_SERIALIZED(ifp->if_serializer);
2164 mii = device_get_softc(sc->re_miibus);
2166 if (sc->re_flags & RE_F_LINKED) {
2167 if (!(mii->mii_media_status & IFM_ACTIVE))
2168 sc->re_flags &= ~RE_F_LINKED;
2170 if (mii->mii_media_status & IFM_ACTIVE &&
2171 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2172 sc->re_flags |= RE_F_LINKED;
2173 if (!ifq_is_empty(&ifp->if_snd))
2178 callout_reset(&sc->re_timer, hz, re_tick, sc);
2181 #ifdef IFPOLL_ENABLE
2184 re_npoll_compat(struct ifnet *ifp, void *arg __unused, int count)
2186 struct re_softc *sc = ifp->if_softc;
2188 ASSERT_SERIALIZED(ifp->if_serializer);
2190 if (sc->re_npoll.ifpc_stcount-- == 0) {
2193 sc->re_npoll.ifpc_stcount = sc->re_npoll.ifpc_stfrac;
2195 status = CSR_READ_2(sc, RE_ISR);
2196 if (status == 0xffff)
2199 CSR_WRITE_2(sc, RE_ISR, status);
2202 * XXX check behaviour on receiver stalls.
2205 if (status & RE_ISR_SYSTEM_ERR)
2209 sc->rxcycles = count;
2213 if (!ifq_is_empty(&ifp->if_snd))
2218 re_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2220 struct re_softc *sc = ifp->if_softc;
2222 ASSERT_SERIALIZED(ifp->if_serializer);
2225 int cpuid = sc->re_npoll.ifpc_cpuid;
2227 info->ifpi_rx[cpuid].poll_func = re_npoll_compat;
2228 info->ifpi_rx[cpuid].arg = NULL;
2229 info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
2231 if (ifp->if_flags & IFF_RUNNING)
2232 re_setup_intr(sc, 0, RE_IMTYPE_NONE);
2233 ifq_set_cpuid(&ifp->if_snd, cpuid);
2235 if (ifp->if_flags & IFF_RUNNING)
2236 re_setup_intr(sc, 1, sc->re_imtype);
2237 ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->re_irq));
2240 #endif /* IFPOLL_ENABLE */
2245 struct re_softc *sc = arg;
2246 struct ifnet *ifp = &sc->arpcom.ac_if;
2250 ASSERT_SERIALIZED(ifp->if_serializer);
2252 if ((sc->re_flags & RE_F_SUSPENDED) ||
2253 (ifp->if_flags & IFF_RUNNING) == 0)
2258 status = CSR_READ_2(sc, RE_ISR);
2259 /* If the card has gone away the read returns 0xffff. */
2260 if (status == 0xffff)
2263 CSR_WRITE_2(sc, RE_ISR, status);
2265 if ((status & sc->re_intrs) == 0)
2268 if (status & (sc->re_rx_ack | RE_ISR_RX_ERR))
2271 if (status & (sc->re_tx_ack | RE_ISR_TX_ERR))
2274 if (status & RE_ISR_SYSTEM_ERR)
2277 if (status & RE_ISR_LINKCHG) {
2278 callout_stop(&sc->re_timer);
2279 re_tick_serialized(sc);
2283 if (sc->re_imtype == RE_IMTYPE_SIM) {
2284 if ((sc->re_flags & RE_F_TIMER_INTR)) {
2285 if ((tx | rx) == 0) {
2287 * Nothing needs to be processed, fallback
2288 * to use TX/RX interrupts.
2290 re_setup_intr(sc, 1, RE_IMTYPE_NONE);
2293 * Recollect, mainly to avoid the possible
2294 * race introduced by changing interrupt
2300 CSR_WRITE_4(sc, RE_TIMERCNT, 1); /* reload */
2302 } else if (tx | rx) {
2304 * Assume that using simulated interrupt moderation
2305 * (hardware timer based) could reduce the interript
2308 re_setup_intr(sc, 1, RE_IMTYPE_SIM);
2312 if (tx && !ifq_is_empty(&ifp->if_snd))
2317 re_encap(struct re_softc *sc, struct mbuf **m_head, int *idx0)
2319 struct mbuf *m = *m_head;
2320 bus_dma_segment_t segs[RE_MAXSEGS];
2322 int error, maxsegs, idx, i, nsegs;
2323 struct re_desc *d, *tx_ring;
2324 uint32_t cmd_csum, ctl_csum, vlantag;
2326 KASSERT(sc->re_ldata.re_tx_free > RE_TXDESC_SPARE,
2327 ("not enough free TX desc"));
2329 map = sc->re_ldata.re_tx_dmamap[*idx0];
2332 * Set up checksum offload. Note: checksum offload bits must
2333 * appear in all descriptors of a multi-descriptor transmit
2334 * attempt. (This is according to testing done with an 8169
2335 * chip. I'm not sure if this is a requirement or a bug.)
2337 cmd_csum = ctl_csum = 0;
2338 if (m->m_pkthdr.csum_flags & CSUM_IP) {
2339 cmd_csum |= RE_TDESC_CMD_IPCSUM;
2340 ctl_csum |= RE_TDESC_CTL_IPCSUM;
2342 if (m->m_pkthdr.csum_flags & CSUM_TCP) {
2343 cmd_csum |= RE_TDESC_CMD_TCPCSUM;
2344 ctl_csum |= RE_TDESC_CTL_TCPCSUM;
2346 if (m->m_pkthdr.csum_flags & CSUM_UDP) {
2347 cmd_csum |= RE_TDESC_CMD_UDPCSUM;
2348 ctl_csum |= RE_TDESC_CTL_UDPCSUM;
2351 /* For MAC2 chips, csum flags are set on re_control */
2352 if (sc->re_caps & RE_C_MAC2)
2357 if ((sc->re_caps & RE_C_AUTOPAD) == 0) {
2359 * With some of the RealTek chips, using the checksum offload
2360 * support in conjunction with the autopadding feature results
2361 * in the transmission of corrupt frames. For example, if we
2362 * need to send a really small IP fragment that's less than 60
2363 * bytes in size, and IP header checksumming is enabled, the
2364 * resulting ethernet frame that appears on the wire will
2365 * have garbled payload. To work around this, if TX checksum
2366 * offload is enabled, we always manually pad short frames out
2367 * to the minimum ethernet frame size.
2369 * Note: this appears unnecessary for TCP, and doing it for TCP
2370 * with PCIe adapters seems to result in bad checksums.
2372 if ((m->m_pkthdr.csum_flags &
2373 (CSUM_DELAY_IP | CSUM_DELAY_DATA)) &&
2374 (m->m_pkthdr.csum_flags & CSUM_TCP) == 0 &&
2375 m->m_pkthdr.len < RE_MIN_FRAMELEN) {
2376 error = m_devpad(m, RE_MIN_FRAMELEN);
2383 if (m->m_flags & M_VLANTAG) {
2384 vlantag = htobe16(m->m_pkthdr.ether_vlantag) |
2385 RE_TDESC_CTL_INSTAG;
2388 maxsegs = sc->re_ldata.re_tx_free;
2389 if (maxsegs > RE_MAXSEGS)
2390 maxsegs = RE_MAXSEGS;
2392 error = bus_dmamap_load_mbuf_defrag(sc->re_ldata.re_tx_mtag, map,
2393 m_head, segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
2398 bus_dmamap_sync(sc->re_ldata.re_tx_mtag, map, BUS_DMASYNC_PREWRITE);
2401 * Map the segment array into descriptors. We also keep track
2402 * of the end of the ring and set the end-of-ring bits as needed,
2403 * and we set the ownership bits in all except the very first
2404 * descriptor, whose ownership bits will be turned on later.
2406 tx_ring = sc->re_ldata.re_tx_list;
2414 cmdstat = segs[i].ds_len;
2415 d->re_bufaddr_lo = htole32(RE_ADDR_LO(segs[i].ds_addr));
2416 d->re_bufaddr_hi = htole32(RE_ADDR_HI(segs[i].ds_addr));
2418 cmdstat |= RE_TDESC_CMD_SOF;
2420 cmdstat |= RE_TDESC_CMD_OWN;
2421 if (idx == (sc->re_tx_desc_cnt - 1))
2422 cmdstat |= RE_TDESC_CMD_EOR;
2423 d->re_cmdstat = htole32(cmdstat | cmd_csum);
2424 d->re_control = htole32(ctl_csum | vlantag);
2429 RE_TXDESC_INC(sc, idx);
2431 d->re_cmdstat |= htole32(RE_TDESC_CMD_EOF);
2433 /* Transfer ownership of packet to the chip. */
2434 d->re_cmdstat |= htole32(RE_TDESC_CMD_OWN);
2436 tx_ring[*idx0].re_cmdstat |= htole32(RE_TDESC_CMD_OWN);
2439 * Insure that the map for this transmission
2440 * is placed at the array index of the last descriptor
2443 sc->re_ldata.re_tx_dmamap[*idx0] = sc->re_ldata.re_tx_dmamap[idx];
2444 sc->re_ldata.re_tx_dmamap[idx] = map;
2446 sc->re_ldata.re_tx_mbuf[idx] = m;
2447 sc->re_ldata.re_tx_free -= nsegs;
2449 RE_TXDESC_INC(sc, idx);
2460 * Main transmit routine for C+ and gigE NICs.
2464 re_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2466 struct re_softc *sc = ifp->if_softc;
2467 struct mbuf *m_head;
2468 int idx, need_trans, oactive, error;
2470 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2471 ASSERT_SERIALIZED(ifp->if_serializer);
2473 if ((sc->re_flags & RE_F_LINKED) == 0) {
2474 ifq_purge(&ifp->if_snd);
2478 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
2481 idx = sc->re_ldata.re_tx_prodidx;
2485 while (sc->re_ldata.re_tx_mbuf[idx] == NULL) {
2486 if (sc->re_ldata.re_tx_free <= RE_TXDESC_SPARE) {
2488 if (re_tx_collect(sc)) {
2493 ifq_set_oactive(&ifp->if_snd);
2497 m_head = ifq_dequeue(&ifp->if_snd);
2501 error = re_encap(sc, &m_head, &idx);
2503 /* m_head is freed by re_encap(), if we reach here */
2504 IFNET_STAT_INC(ifp, oerrors, 1);
2506 if (error == EFBIG && !oactive) {
2507 if (re_tx_collect(sc)) {
2512 ifq_set_oactive(&ifp->if_snd);
2520 * If there's a BPF listener, bounce a copy of this frame
2523 ETHER_BPF_MTAP(ifp, m_head);
2527 * If sc->re_ldata.re_tx_mbuf[idx] is not NULL it is possible
2528 * for OACTIVE to not be properly set when we also do not
2529 * have sufficient free tx descriptors, leaving packet in
2530 * ifp->if_snd. This can cause if_start_dispatch() to loop
2531 * infinitely so make sure OACTIVE is set properly.
2533 if (sc->re_ldata.re_tx_free <= RE_TXDESC_SPARE) {
2534 if (!ifq_is_oactive(&ifp->if_snd)) {
2535 if_printf(ifp, "Debug: OACTIVE was not set when "
2536 "re_tx_free was below minimum!\n");
2537 ifq_set_oactive(&ifp->if_snd);
2543 sc->re_ldata.re_tx_prodidx = idx;
2546 * RealTek put the TX poll request register in a different
2547 * location on the 8169 gigE chip. I don't know why.
2549 CSR_WRITE_1(sc, sc->re_txstart, RE_TXSTART_START);
2552 * Set a timeout in case the chip goes out to lunch.
2560 struct re_softc *sc = xsc;
2561 struct ifnet *ifp = &sc->arpcom.ac_if;
2562 struct mii_data *mii;
2563 int error, framelen;
2565 ASSERT_SERIALIZED(ifp->if_serializer);
2567 mii = device_get_softc(sc->re_miibus);
2570 * Cancel pending I/O and free all RX/TX buffers.
2574 if (sc->re_caps & RE_C_CONTIGRX) {
2575 if (ifp->if_mtu > ETHERMTU) {
2576 KKASSERT(sc->re_ldata.re_jbuf != NULL);
2577 sc->re_flags |= RE_F_USE_JPOOL;
2578 sc->re_rxbuf_size = RE_FRAMELEN_MAX;
2579 sc->re_newbuf = re_newbuf_jumbo;
2581 sc->re_flags &= ~RE_F_USE_JPOOL;
2582 sc->re_rxbuf_size = MCLBYTES;
2583 sc->re_newbuf = re_newbuf_std;
2588 * Adjust max read request size according to MTU; mainly to
2589 * improve TX performance for common case (ETHERMTU) on GigE
2590 * NICs. However, this could _not_ be done on 10/100 only
2591 * NICs; their DMA engines will malfunction using non-default
2592 * max read request size.
2594 if ((sc->re_caps & (RE_C_PCIE | RE_C_FASTE)) == RE_C_PCIE) {
2595 if (ifp->if_mtu > ETHERMTU) {
2597 * 512 seems to be the only value that works
2598 * reliably with jumbo frame
2600 pcie_set_max_readrq(sc->re_dev,
2601 PCIEM_DEVCTL_MAX_READRQ_512);
2603 pcie_set_max_readrq(sc->re_dev,
2604 PCIEM_DEVCTL_MAX_READRQ_4096);
2609 * Enable C+ RX and TX mode, as well as VLAN stripping and
2610 * RX checksum offload. We must configure the C+ register
2611 * before all others.
2613 CSR_WRITE_2(sc, RE_CPLUS_CMD, RE_CPLUSCMD_RXENB | RE_CPLUSCMD_TXENB |
2614 RE_CPLUSCMD_PCI_MRW |
2615 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING ?
2616 RE_CPLUSCMD_VLANSTRIP : 0) |
2617 (ifp->if_capenable & IFCAP_RXCSUM ?
2618 RE_CPLUSCMD_RXCSUM_ENB : 0));
2621 * Init our MAC address. Even though the chipset
2622 * documentation doesn't mention it, we need to enter "Config
2623 * register write enable" mode to modify the ID registers.
2625 CSR_WRITE_1(sc, RE_EECMD, RE_EEMODE_WRITECFG);
2626 CSR_WRITE_4(sc, RE_IDR0,
2627 htole32(*(uint32_t *)(&sc->arpcom.ac_enaddr[0])));
2628 CSR_WRITE_2(sc, RE_IDR4,
2629 htole16(*(uint16_t *)(&sc->arpcom.ac_enaddr[4])));
2630 CSR_WRITE_1(sc, RE_EECMD, RE_EEMODE_OFF);
2633 * For C+ mode, initialize the RX descriptors and mbufs.
2635 error = re_rx_list_init(sc);
2640 error = re_tx_list_init(sc);
2647 * Load the addresses of the RX and TX lists into the chip.
2649 CSR_WRITE_4(sc, RE_RXLIST_ADDR_HI,
2650 RE_ADDR_HI(sc->re_ldata.re_rx_list_addr));
2651 CSR_WRITE_4(sc, RE_RXLIST_ADDR_LO,
2652 RE_ADDR_LO(sc->re_ldata.re_rx_list_addr));
2654 CSR_WRITE_4(sc, RE_TXLIST_ADDR_HI,
2655 RE_ADDR_HI(sc->re_ldata.re_tx_list_addr));
2656 CSR_WRITE_4(sc, RE_TXLIST_ADDR_LO,
2657 RE_ADDR_LO(sc->re_ldata.re_tx_list_addr));
2660 * Enable transmit and receive.
2662 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_TX_ENB|RE_CMD_RX_ENB);
2665 * Set the initial TX and RX configuration.
2667 if (sc->re_flags & RE_F_TESTMODE) {
2668 if (!RE_IS_8139CP(sc))
2669 CSR_WRITE_4(sc, RE_TXCFG,
2670 RE_TXCFG_CONFIG | RE_LOOPTEST_ON);
2672 CSR_WRITE_4(sc, RE_TXCFG,
2673 RE_TXCFG_CONFIG | RE_LOOPTEST_ON_CPLUS);
2675 CSR_WRITE_4(sc, RE_TXCFG, RE_TXCFG_CONFIG);
2677 framelen = RE_FRAMELEN(ifp->if_mtu);
2678 if (framelen < MCLBYTES)
2679 CSR_WRITE_1(sc, RE_EARLY_TX_THRESH, howmany(MCLBYTES, 128));
2681 CSR_WRITE_1(sc, RE_EARLY_TX_THRESH, howmany(framelen, 128));
2683 CSR_WRITE_4(sc, RE_RXCFG, RE_RXCFG_CONFIG);
2686 * Program the multicast filter, if necessary.
2690 #ifdef IFPOLL_ENABLE
2692 * Disable interrupts if we are polling.
2694 if (ifp->if_flags & IFF_NPOLLING)
2695 re_setup_intr(sc, 0, RE_IMTYPE_NONE);
2696 else /* otherwise ... */
2697 #endif /* IFPOLL_ENABLE */
2699 * Enable interrupts.
2701 if (sc->re_flags & RE_F_TESTMODE)
2702 CSR_WRITE_2(sc, RE_IMR, 0);
2704 re_setup_intr(sc, 1, sc->re_imtype);
2705 CSR_WRITE_2(sc, RE_ISR, sc->re_intrs);
2707 /* Start RX/TX process. */
2708 CSR_WRITE_4(sc, RE_MISSEDPKT, 0);
2711 /* Enable receiver and transmitter. */
2712 CSR_WRITE_1(sc, RE_COMMAND, RE_CMD_TX_ENB|RE_CMD_RX_ENB);
2716 * For 8169 gigE NICs, set the max allowed RX packet
2717 * size so we can receive jumbo frames.
2719 if (!RE_IS_8139CP(sc)) {
2720 if (sc->re_caps & RE_C_CONTIGRX)
2721 CSR_WRITE_2(sc, RE_MAXRXPKTLEN, sc->re_rxbuf_size);
2723 CSR_WRITE_2(sc, RE_MAXRXPKTLEN, 16383);
2726 if (sc->re_flags & RE_F_TESTMODE)
2731 CSR_WRITE_1(sc, RE_CFG1, RE_CFG1_DRVLOAD|RE_CFG1_FULLDUPLEX);
2733 ifp->if_flags |= IFF_RUNNING;
2734 ifq_clr_oactive(&ifp->if_snd);
2736 callout_reset(&sc->re_timer, hz, re_tick, sc);
2740 * Set media options.
2743 re_ifmedia_upd(struct ifnet *ifp)
2745 struct re_softc *sc = ifp->if_softc;
2746 struct mii_data *mii;
2748 ASSERT_SERIALIZED(ifp->if_serializer);
2750 mii = device_get_softc(sc->re_miibus);
2757 * Report current media status.
2760 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2762 struct re_softc *sc = ifp->if_softc;
2763 struct mii_data *mii;
2765 ASSERT_SERIALIZED(ifp->if_serializer);
2767 mii = device_get_softc(sc->re_miibus);
2770 ifmr->ifm_active = mii->mii_media_active;
2771 ifmr->ifm_status = mii->mii_media_status;
2775 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
2777 struct re_softc *sc = ifp->if_softc;
2778 struct ifreq *ifr = (struct ifreq *) data;
2779 struct mii_data *mii;
2780 int error = 0, mask;
2782 ASSERT_SERIALIZED(ifp->if_serializer);
2786 if (ifr->ifr_mtu > sc->re_maxmtu) {
2788 } else if (ifp->if_mtu != ifr->ifr_mtu) {
2789 ifp->if_mtu = ifr->ifr_mtu;
2790 if (ifp->if_flags & IFF_RUNNING)
2796 if (ifp->if_flags & IFF_UP) {
2797 if (ifp->if_flags & IFF_RUNNING) {
2798 if ((ifp->if_flags ^ sc->re_if_flags) &
2799 (IFF_PROMISC | IFF_ALLMULTI))
2804 } else if (ifp->if_flags & IFF_RUNNING) {
2807 sc->re_if_flags = ifp->if_flags;
2817 mii = device_get_softc(sc->re_miibus);
2818 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2822 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) &
2823 ifp->if_capabilities;
2824 ifp->if_capenable ^= mask;
2826 if (mask & IFCAP_HWCSUM) {
2827 if (ifp->if_capenable & IFCAP_TXCSUM)
2828 ifp->if_hwassist = sc->re_hwassist;
2830 ifp->if_hwassist = 0;
2832 if (mask && (ifp->if_flags & IFF_RUNNING))
2837 error = ether_ioctl(ifp, command, data);
2844 re_watchdog(struct ifnet *ifp)
2846 struct re_softc *sc = ifp->if_softc;
2848 ASSERT_SERIALIZED(ifp->if_serializer);
2850 if_printf(ifp, "watchdog timeout\n");
2852 IFNET_STAT_INC(ifp, oerrors, 1);
2859 if (!ifq_is_empty(&ifp->if_snd))
2864 * Stop the adapter and free any mbufs allocated to the
2868 re_stop(struct re_softc *sc)
2870 struct ifnet *ifp = &sc->arpcom.ac_if;
2873 ASSERT_SERIALIZED(ifp->if_serializer);
2875 /* Reset the adapter. */
2876 re_reset(sc, ifp->if_flags & IFF_RUNNING);
2879 callout_stop(&sc->re_timer);
2881 ifp->if_flags &= ~IFF_RUNNING;
2882 ifq_clr_oactive(&ifp->if_snd);
2883 sc->re_flags &= ~(RE_F_TIMER_INTR | RE_F_DROP_RXFRAG | RE_F_LINKED);
2885 CSR_WRITE_1(sc, RE_COMMAND, 0x00);
2886 CSR_WRITE_2(sc, RE_IMR, 0x0000);
2887 CSR_WRITE_2(sc, RE_ISR, 0xFFFF);
2889 re_free_rxchain(sc);
2891 /* Free the TX list buffers. */
2892 for (i = 0; i < sc->re_tx_desc_cnt; i++) {
2893 if (sc->re_ldata.re_tx_mbuf[i] != NULL) {
2894 bus_dmamap_unload(sc->re_ldata.re_tx_mtag,
2895 sc->re_ldata.re_tx_dmamap[i]);
2896 m_freem(sc->re_ldata.re_tx_mbuf[i]);
2897 sc->re_ldata.re_tx_mbuf[i] = NULL;
2901 /* Free the RX list buffers. */
2902 for (i = 0; i < sc->re_rx_desc_cnt; i++) {
2903 if (sc->re_ldata.re_rx_mbuf[i] != NULL) {
2904 if ((sc->re_flags & RE_F_USE_JPOOL) == 0) {
2905 bus_dmamap_unload(sc->re_ldata.re_rx_mtag,
2906 sc->re_ldata.re_rx_dmamap[i]);
2908 m_freem(sc->re_ldata.re_rx_mbuf[i]);
2909 sc->re_ldata.re_rx_mbuf[i] = NULL;
2915 * Device suspend routine. Stop the interface and save some PCI
2916 * settings in case the BIOS doesn't restore them properly on
2920 re_suspend(device_t dev)
2922 #ifndef BURN_BRIDGES
2925 struct re_softc *sc = device_get_softc(dev);
2926 struct ifnet *ifp = &sc->arpcom.ac_if;
2928 lwkt_serialize_enter(ifp->if_serializer);
2932 #ifndef BURN_BRIDGES
2933 for (i = 0; i < 5; i++)
2934 sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2935 sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2936 sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2937 sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2938 sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2941 sc->re_flags |= RE_F_SUSPENDED;
2943 lwkt_serialize_exit(ifp->if_serializer);
2949 * Device resume routine. Restore some PCI settings in case the BIOS
2950 * doesn't, re-enable busmastering, and restart the interface if
2954 re_resume(device_t dev)
2956 struct re_softc *sc = device_get_softc(dev);
2957 struct ifnet *ifp = &sc->arpcom.ac_if;
2958 #ifndef BURN_BRIDGES
2962 lwkt_serialize_enter(ifp->if_serializer);
2964 #ifndef BURN_BRIDGES
2965 /* better way to do this? */
2966 for (i = 0; i < 5; i++)
2967 pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
2968 pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
2969 pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
2970 pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
2971 pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
2973 /* reenable busmastering */
2974 pci_enable_busmaster(dev);
2975 pci_enable_io(dev, SYS_RES_IOPORT);
2978 /* reinitialize interface if necessary */
2979 if (ifp->if_flags & IFF_UP)
2982 sc->re_flags &= ~RE_F_SUSPENDED;
2984 lwkt_serialize_exit(ifp->if_serializer);
2990 * Stop all chip I/O so that the kernel's probe routines don't
2991 * get confused by errant DMAs when rebooting.
2994 re_shutdown(device_t dev)
2996 struct re_softc *sc = device_get_softc(dev);
2997 struct ifnet *ifp = &sc->arpcom.ac_if;
2999 lwkt_serialize_enter(ifp->if_serializer);
3001 lwkt_serialize_exit(ifp->if_serializer);
3005 re_sysctl_rxtime(SYSCTL_HANDLER_ARGS)
3007 struct re_softc *sc = arg1;
3009 return re_sysctl_hwtime(oidp, arg1, arg2, req, &sc->re_rx_time);
3013 re_sysctl_txtime(SYSCTL_HANDLER_ARGS)
3015 struct re_softc *sc = arg1;
3017 return re_sysctl_hwtime(oidp, arg1, arg2, req, &sc->re_tx_time);
3021 re_sysctl_hwtime(SYSCTL_HANDLER_ARGS, int *hwtime)
3023 struct re_softc *sc = arg1;
3024 struct ifnet *ifp = &sc->arpcom.ac_if;
3027 lwkt_serialize_enter(ifp->if_serializer);
3030 error = sysctl_handle_int(oidp, &v, 0, req);
3031 if (error || req->newptr == NULL)
3042 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
3043 IFF_RUNNING && sc->re_imtype == RE_IMTYPE_HW)
3047 lwkt_serialize_exit(ifp->if_serializer);
3052 re_sysctl_simtime(SYSCTL_HANDLER_ARGS)
3054 struct re_softc *sc = arg1;
3055 struct ifnet *ifp = &sc->arpcom.ac_if;
3058 lwkt_serialize_enter(ifp->if_serializer);
3060 v = sc->re_sim_time;
3061 error = sysctl_handle_int(oidp, &v, 0, req);
3062 if (error || req->newptr == NULL)
3070 if (v != sc->re_sim_time) {
3071 sc->re_sim_time = v;
3073 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
3074 IFF_RUNNING && sc->re_imtype == RE_IMTYPE_SIM) {
3079 * Following code causes various strange
3080 * performance problems. Hmm ...
3082 CSR_WRITE_2(sc, RE_IMR, 0);
3083 if (!RE_IS_8139CP(sc))
3084 reg = RE_TIMERINT_8169;
3087 CSR_WRITE_4(sc, reg, 0);
3088 CSR_READ_4(sc, reg); /* flush */
3090 CSR_WRITE_2(sc, RE_IMR, sc->re_intrs);
3091 re_setup_sim_im(sc);
3093 re_setup_intr(sc, 0, RE_IMTYPE_NONE);
3095 re_setup_intr(sc, 1, RE_IMTYPE_SIM);
3100 lwkt_serialize_exit(ifp->if_serializer);
3105 re_sysctl_imtype(SYSCTL_HANDLER_ARGS)
3107 struct re_softc *sc = arg1;
3108 struct ifnet *ifp = &sc->arpcom.ac_if;
3111 lwkt_serialize_enter(ifp->if_serializer);
3114 error = sysctl_handle_int(oidp, &v, 0, req);
3115 if (error || req->newptr == NULL)
3118 if (v != RE_IMTYPE_HW && v != RE_IMTYPE_SIM && v != RE_IMTYPE_NONE) {
3122 if (v == RE_IMTYPE_HW && (sc->re_caps & RE_C_HWIM) == 0) {
3123 /* Can't do hardware interrupt moderation */
3128 if (v != sc->re_imtype) {
3130 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
3132 re_setup_intr(sc, 1, sc->re_imtype);
3135 lwkt_serialize_exit(ifp->if_serializer);
3140 re_setup_hw_im(struct re_softc *sc)
3142 KKASSERT(sc->re_caps & RE_C_HWIM);
3145 * Interrupt moderation
3148 * A - unknown (maybe TX related)
3149 * B - TX timer (unit: 25us)
3150 * C - unknown (maybe RX related)
3151 * D - RX timer (unit: 25us)
3154 * re(4)'s interrupt moderation is actually controlled by
3155 * two variables, like most other NICs (bge, bce etc.)
3157 * o number of packets [P]
3159 * The logic relationship between these two variables is
3160 * similar to other NICs too:
3161 * if (timer expire || packets > [P])
3162 * Interrupt is delivered
3164 * Currently we only know how to set 'timer', but not
3165 * 'number of packets', which should be ~30, as far as I
3166 * tested (sink ~900Kpps, interrupt rate is 30KHz)
3168 CSR_WRITE_2(sc, RE_IM,
3169 RE_IM_RXTIME(sc->re_rx_time) |
3170 RE_IM_TXTIME(sc->re_tx_time) |
3175 re_disable_hw_im(struct re_softc *sc)
3177 if (sc->re_caps & RE_C_HWIM)
3178 CSR_WRITE_2(sc, RE_IM, 0);
3182 re_setup_sim_im(struct re_softc *sc)
3184 if (!RE_IS_8139CP(sc)) {
3188 * Datasheet says tick decreases at bus speed,
3189 * but it seems the clock runs a little bit
3190 * faster, so we do some compensation here.
3192 ticks = (sc->re_sim_time * sc->re_bus_speed * 8) / 5;
3193 CSR_WRITE_4(sc, RE_TIMERINT_8169, ticks);
3195 CSR_WRITE_4(sc, RE_TIMERINT, 0x400); /* XXX */
3197 CSR_WRITE_4(sc, RE_TIMERCNT, 1); /* reload */
3198 sc->re_flags |= RE_F_TIMER_INTR;
3202 re_disable_sim_im(struct re_softc *sc)
3204 if (!RE_IS_8139CP(sc))
3205 CSR_WRITE_4(sc, RE_TIMERINT_8169, 0);
3207 CSR_WRITE_4(sc, RE_TIMERINT, 0);
3208 sc->re_flags &= ~RE_F_TIMER_INTR;
3212 re_config_imtype(struct re_softc *sc, int imtype)
3216 KKASSERT(sc->re_caps & RE_C_HWIM);
3218 case RE_IMTYPE_NONE:
3219 sc->re_intrs = RE_INTRS;
3220 sc->re_rx_ack = RE_ISR_RX_OK | RE_ISR_FIFO_OFLOW |
3222 sc->re_tx_ack = RE_ISR_TX_OK;
3226 sc->re_intrs = RE_INTRS_TIMER;
3227 sc->re_rx_ack = RE_ISR_TIMEOUT_EXPIRED;
3228 sc->re_tx_ack = RE_ISR_TIMEOUT_EXPIRED;
3232 panic("%s: unknown imtype %d",
3233 sc->arpcom.ac_if.if_xname, imtype);
3238 re_setup_intr(struct re_softc *sc, int enable_intrs, int imtype)
3240 re_config_imtype(sc, imtype);
3243 CSR_WRITE_2(sc, RE_IMR, sc->re_intrs);
3245 CSR_WRITE_2(sc, RE_IMR, 0);
3247 sc->re_npoll.ifpc_stcount = 0;
3250 case RE_IMTYPE_NONE:
3251 re_disable_sim_im(sc);
3252 re_disable_hw_im(sc);
3256 KKASSERT(sc->re_caps & RE_C_HWIM);
3257 re_disable_sim_im(sc);
3262 re_disable_hw_im(sc);
3263 re_setup_sim_im(sc);
3267 panic("%s: unknown imtype %d",
3268 sc->arpcom.ac_if.if_xname, imtype);
3273 re_get_eaddr(struct re_softc *sc, uint8_t *eaddr)
3277 if (sc->re_caps & RE_C_EE_EADDR) {
3281 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
3282 if (re_did == 0x8128) {
3283 uint16_t as[ETHER_ADDR_LEN / 2];
3286 * Get station address from the EEPROM.
3288 re_read_eeprom(sc, (caddr_t)as, sc->re_ee_eaddr, 3);
3289 for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
3290 as[i] = le16toh(as[i]);
3291 bcopy(as, eaddr, ETHER_ADDR_LEN);
3297 * Get station address from IDRx.
3299 for (i = 0; i < ETHER_ADDR_LEN; ++i)
3300 eaddr[i] = CSR_READ_1(sc, RE_IDR0 + i);
3304 re_jpool_alloc(struct re_softc *sc)
3306 struct re_list_data *ldata = &sc->re_ldata;
3307 struct re_jbuf *jbuf;
3309 bus_size_t jpool_size;
3314 lwkt_serialize_init(&ldata->re_jbuf_serializer);
3316 ldata->re_jbuf = kmalloc(sizeof(struct re_jbuf) * RE_JBUF_COUNT(sc),
3317 M_DEVBUF, M_WAITOK | M_ZERO);
3319 jpool_size = RE_JBUF_COUNT(sc) * RE_JBUF_SIZE;
3321 error = bus_dmamem_coherent(sc->re_parent_tag,
3323 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3324 jpool_size, BUS_DMA_WAITOK, &dmem);
3326 device_printf(sc->re_dev, "could not allocate jumbo memory\n");
3329 ldata->re_jpool_tag = dmem.dmem_tag;
3330 ldata->re_jpool_map = dmem.dmem_map;
3331 ldata->re_jpool = dmem.dmem_addr;
3332 paddr = dmem.dmem_busaddr;
3334 /* ..and split it into 9KB chunks */
3335 SLIST_INIT(&ldata->re_jbuf_free);
3337 buf = ldata->re_jpool;
3338 for (i = 0; i < RE_JBUF_COUNT(sc); i++) {
3339 jbuf = &ldata->re_jbuf[i];
3345 jbuf->re_paddr = paddr;
3347 SLIST_INSERT_HEAD(&ldata->re_jbuf_free, jbuf, re_link);
3349 buf += RE_JBUF_SIZE;
3350 paddr += RE_JBUF_SIZE;
3356 re_jpool_free(struct re_softc *sc)
3358 struct re_list_data *ldata = &sc->re_ldata;
3360 if (ldata->re_jpool_tag != NULL) {
3361 bus_dmamap_unload(ldata->re_jpool_tag, ldata->re_jpool_map);
3362 bus_dmamem_free(ldata->re_jpool_tag, ldata->re_jpool,
3363 ldata->re_jpool_map);
3364 bus_dma_tag_destroy(ldata->re_jpool_tag);
3365 ldata->re_jpool_tag = NULL;
3368 if (ldata->re_jbuf != NULL) {
3369 kfree(ldata->re_jbuf, M_DEVBUF);
3370 ldata->re_jbuf = NULL;
3374 static struct re_jbuf *
3375 re_jbuf_alloc(struct re_softc *sc)
3377 struct re_list_data *ldata = &sc->re_ldata;
3378 struct re_jbuf *jbuf;
3380 lwkt_serialize_enter(&ldata->re_jbuf_serializer);
3382 jbuf = SLIST_FIRST(&ldata->re_jbuf_free);
3384 SLIST_REMOVE_HEAD(&ldata->re_jbuf_free, re_link);
3388 lwkt_serialize_exit(&ldata->re_jbuf_serializer);
3394 re_jbuf_free(void *arg)
3396 struct re_jbuf *jbuf = arg;
3397 struct re_softc *sc = jbuf->re_sc;
3398 struct re_list_data *ldata = &sc->re_ldata;
3400 if (&ldata->re_jbuf[jbuf->re_slot] != jbuf) {
3401 panic("%s: free wrong jumbo buffer",
3402 sc->arpcom.ac_if.if_xname);
3403 } else if (jbuf->re_inuse == 0) {
3404 panic("%s: jumbo buffer already freed",
3405 sc->arpcom.ac_if.if_xname);
3408 lwkt_serialize_enter(&ldata->re_jbuf_serializer);
3409 atomic_subtract_int(&jbuf->re_inuse, 1);
3410 if (jbuf->re_inuse == 0)
3411 SLIST_INSERT_HEAD(&ldata->re_jbuf_free, jbuf, re_link);
3412 lwkt_serialize_exit(&ldata->re_jbuf_serializer);
3416 re_jbuf_ref(void *arg)
3418 struct re_jbuf *jbuf = arg;
3419 struct re_softc *sc = jbuf->re_sc;
3420 struct re_list_data *ldata = &sc->re_ldata;
3422 if (&ldata->re_jbuf[jbuf->re_slot] != jbuf) {
3423 panic("%s: ref wrong jumbo buffer",
3424 sc->arpcom.ac_if.if_xname);
3425 } else if (jbuf->re_inuse == 0) {
3426 panic("%s: jumbo buffer already freed",
3427 sc->arpcom.ac_if.if_xname);
3429 atomic_add_int(&jbuf->re_inuse, 1);