Home | History | Annotate | Line # | Download | only in ic
rtl8169.c revision 1.159.2.2
      1 /*	$NetBSD: rtl8169.c,v 1.159.2.2 2023/08/04 15:06:50 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998-2003
      5  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rtl8169.c,v 1.159.2.2 2023/08/04 15:06:50 martin Exp $");
     37 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
     38 
     39 /*
     40  * RealTek 8139C+/8169/8169S/8168/8110S PCI NIC driver
     41  *
     42  * Written by Bill Paul <wpaul (at) windriver.com>
     43  * Senior Networking Software Engineer
     44  * Wind River Systems
     45  */
     46 
     47 /*
     48  * This driver is designed to support RealTek's next generation of
     49  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
     50  * six devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
     51  * RTL8110S, the RTL8168 and the RTL8111.
     52  *
     53  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
     54  * with the older 8139 family, however it also supports a special
     55  * C+ mode of operation that provides several new performance enhancing
     56  * features. These include:
     57  *
     58  *	o Descriptor based DMA mechanism. Each descriptor represents
     59  *	  a single packet fragment. Data buffers may be aligned on
     60  *	  any byte boundary.
     61  *
     62  *	o 64-bit DMA
     63  *
     64  *	o TCP/IP checksum offload for both RX and TX
     65  *
     66  *	o High and normal priority transmit DMA rings
     67  *
     68  *	o VLAN tag insertion and extraction
     69  *
     70  *	o TCP large send (segmentation offload)
     71  *
     72  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
     73  * programming API is fairly straightforward. The RX filtering, EEPROM
     74  * access and PHY access is the same as it is on the older 8139 series
     75  * chips.
     76  *
     77  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
     78  * same programming API and feature set as the 8139C+ with the following
     79  * differences and additions:
     80  *
     81  *	o 1000Mbps mode
     82  *
     83  *	o Jumbo frames
     84  *
     85  *	o GMII and TBI ports/registers for interfacing with copper
     86  *	  or fiber PHYs
     87  *
     88  *      o RX and TX DMA rings can have up to 1024 descriptors
     89  *        (the 8139C+ allows a maximum of 64)
     90  *
     91  *	o Slight differences in register layout from the 8139C+
     92  *
     93  * The TX start and timer interrupt registers are at different locations
     94  * on the 8169 than they are on the 8139C+. Also, the status word in the
     95  * RX descriptor has a slightly different bit layout. The 8169 does not
     96  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
     97  * copper gigE PHY.
     98  *
     99  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
    100  * (the 'S' stands for 'single-chip'). These devices have the same
    101  * programming API as the older 8169, but also have some vendor-specific
    102  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
    103  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
    104  *
    105  * This driver takes advantage of the RX and TX checksum offload and
    106  * VLAN tag insertion/extraction features. It also implements TX
    107  * interrupt moderation using the timer interrupt registers, which
    108  * significantly reduces TX interrupt load. There is also support
    109  * for jumbo frames, however the 8169/8169S/8110S can not transmit
    110  * jumbo frames larger than 7.5K, so the max MTU possible with this
    111  * driver is 7500 bytes.
    112  */
    113 
    114 
    115 #include <sys/param.h>
    116 #include <sys/endian.h>
    117 #include <sys/systm.h>
    118 #include <sys/sockio.h>
    119 #include <sys/mbuf.h>
    120 #include <sys/malloc.h>
    121 #include <sys/kernel.h>
    122 #include <sys/socket.h>
    123 #include <sys/device.h>
    124 
    125 #include <net/if.h>
    126 #include <net/if_arp.h>
    127 #include <net/if_dl.h>
    128 #include <net/if_ether.h>
    129 #include <net/if_media.h>
    130 #include <net/if_vlanvar.h>
    131 
    132 #include <netinet/in_systm.h>	/* XXX for IP_MAXPACKET */
    133 #include <netinet/in.h>		/* XXX for IP_MAXPACKET */
    134 #include <netinet/ip.h>		/* XXX for IP_MAXPACKET */
    135 
    136 #include <net/bpf.h>
    137 #include <sys/rndsource.h>
    138 
    139 #include <sys/bus.h>
    140 
    141 #include <dev/mii/mii.h>
    142 #include <dev/mii/miivar.h>
    143 
    144 #include <dev/ic/rtl81x9reg.h>
    145 #include <dev/ic/rtl81x9var.h>
    146 
    147 #include <dev/ic/rtl8169var.h>
    148 
    149 static inline void re_set_bufaddr(struct re_desc *, bus_addr_t);
    150 
    151 static int re_newbuf(struct rtk_softc *, int, struct mbuf *);
    152 static int re_rx_list_init(struct rtk_softc *);
    153 static int re_tx_list_init(struct rtk_softc *);
    154 static void re_rxeof(struct rtk_softc *);
    155 static void re_txeof(struct rtk_softc *);
    156 static void re_tick(void *);
    157 static void re_start(struct ifnet *);
    158 static int re_ioctl(struct ifnet *, u_long, void *);
    159 static int re_init(struct ifnet *);
    160 static void re_stop(struct ifnet *, int);
    161 static void re_watchdog(struct ifnet *);
    162 
    163 static int re_enable(struct rtk_softc *);
    164 static void re_disable(struct rtk_softc *);
    165 
    166 static int re_gmii_readreg(device_t, int, int, uint16_t *);
    167 static int re_gmii_writereg(device_t, int, int, uint16_t);
    168 
    169 static int re_miibus_readreg(device_t, int, int, uint16_t *);
    170 static int re_miibus_writereg(device_t, int, int, uint16_t);
    171 static void re_miibus_statchg(struct ifnet *);
    172 
    173 static void re_reset(struct rtk_softc *);
    174 
    175 static const struct re_revision {
    176 	uint32_t		re_chipid;
    177 	const char		*re_name;
    178 } re_revisions[] = {
    179 	{ RTK_HWREV_8100,	"RTL8100" },
    180 	{ RTK_HWREV_8100E,	"RTL8100E" },
    181 	{ RTK_HWREV_8100E_SPIN2, "RTL8100E 2" },
    182 	{ RTK_HWREV_8101,	"RTL8101" },
    183 	{ RTK_HWREV_8101E,	"RTL8101E" },
    184 	{ RTK_HWREV_8102E,	"RTL8102E" },
    185 	{ RTK_HWREV_8106E,	"RTL8106E" },
    186 	{ RTK_HWREV_8401E,	"RTL8401E" },
    187 	{ RTK_HWREV_8402,	"RTL8402" },
    188 	{ RTK_HWREV_8411,	"RTL8411" },
    189 	{ RTK_HWREV_8411B,	"RTL8411B" },
    190 	{ RTK_HWREV_8102EL,	"RTL8102EL" },
    191 	{ RTK_HWREV_8102EL_SPIN1, "RTL8102EL 1" },
    192 	{ RTK_HWREV_8103E,       "RTL8103E" },
    193 	{ RTK_HWREV_8110S,	"RTL8110S" },
    194 	{ RTK_HWREV_8139CPLUS,	"RTL8139C+" },
    195 	{ RTK_HWREV_8168B_SPIN1, "RTL8168 1" },
    196 	{ RTK_HWREV_8168B_SPIN2, "RTL8168 2" },
    197 	{ RTK_HWREV_8168B_SPIN3, "RTL8168 3" },
    198 	{ RTK_HWREV_8168C,	"RTL8168C/8111C" },
    199 	{ RTK_HWREV_8168C_SPIN2, "RTL8168C/8111C" },
    200 	{ RTK_HWREV_8168CP,	"RTL8168CP/8111CP" },
    201 	{ RTK_HWREV_8168F,	"RTL8168F/8111F" },
    202 	{ RTK_HWREV_8168G,	"RTL8168G/8111G" },
    203 	{ RTK_HWREV_8168GU,	"RTL8168GU/8111GU" },
    204 	{ RTK_HWREV_8168H,	"RTL8168H/8111H" },
    205 	{ RTK_HWREV_8105E,	"RTL8105E" },
    206 	{ RTK_HWREV_8105E_SPIN1, "RTL8105E" },
    207 	{ RTK_HWREV_8168D,	"RTL8168D/8111D" },
    208 	{ RTK_HWREV_8168DP,	"RTL8168DP/8111DP" },
    209 	{ RTK_HWREV_8168E,	"RTL8168E/8111E" },
    210 	{ RTK_HWREV_8168E_VL,	"RTL8168E/8111E-VL" },
    211 	{ RTK_HWREV_8168EP,	"RTL8168EP/8111EP" },
    212 	{ RTK_HWREV_8168FP,	"RTL8168FP/8117" },
    213 	{ RTK_HWREV_8169,	"RTL8169" },
    214 	{ RTK_HWREV_8169_8110SB, "RTL8169/8110SB" },
    215 	{ RTK_HWREV_8169_8110SBL, "RTL8169SBL" },
    216 	{ RTK_HWREV_8169_8110SC, "RTL8169/8110SCd" },
    217 	{ RTK_HWREV_8169_8110SCE, "RTL8169/8110SCe" },
    218 	{ RTK_HWREV_8169S,	"RTL8169S" },
    219 
    220 	{ 0, NULL }
    221 };
    222 
    223 static inline void
    224 re_set_bufaddr(struct re_desc *d, bus_addr_t addr)
    225 {
    226 
    227 	d->re_bufaddr_lo = htole32((uint32_t)addr);
    228 	if (sizeof(bus_addr_t) == sizeof(uint64_t))
    229 		d->re_bufaddr_hi = htole32((uint64_t)addr >> 32);
    230 	else
    231 		d->re_bufaddr_hi = 0;
    232 }
    233 
    234 static int
    235 re_gmii_readreg(device_t dev, int phy, int reg, uint16_t *val)
    236 {
    237 	struct rtk_softc *sc = device_private(dev);
    238 	uint32_t data;
    239 	int i;
    240 
    241 	if (phy != 7)
    242 		return -1;
    243 
    244 	/* Let the rgephy driver read the GMEDIASTAT register */
    245 
    246 	if (reg == RTK_GMEDIASTAT) {
    247 		*val = CSR_READ_1(sc, RTK_GMEDIASTAT);
    248 		return 0;
    249 	}
    250 
    251 	CSR_WRITE_4(sc, RTK_PHYAR, reg << 16);
    252 	DELAY(1000);
    253 
    254 	for (i = 0; i < RTK_TIMEOUT; i++) {
    255 		data = CSR_READ_4(sc, RTK_PHYAR);
    256 		if (data & RTK_PHYAR_BUSY)
    257 			break;
    258 		DELAY(100);
    259 	}
    260 
    261 	if (i == RTK_TIMEOUT) {
    262 		printf("%s: PHY read failed\n", device_xname(sc->sc_dev));
    263 		return ETIMEDOUT;
    264 	}
    265 
    266 	*val = data & RTK_PHYAR_PHYDATA;
    267 	return 0;
    268 }
    269 
    270 static int
    271 re_gmii_writereg(device_t dev, int phy, int reg, uint16_t val)
    272 {
    273 	struct rtk_softc *sc = device_private(dev);
    274 	uint32_t data;
    275 	int i;
    276 
    277 	CSR_WRITE_4(sc, RTK_PHYAR, (reg << 16) |
    278 	    (val & RTK_PHYAR_PHYDATA) | RTK_PHYAR_BUSY);
    279 	DELAY(1000);
    280 
    281 	for (i = 0; i < RTK_TIMEOUT; i++) {
    282 		data = CSR_READ_4(sc, RTK_PHYAR);
    283 		if (!(data & RTK_PHYAR_BUSY))
    284 			break;
    285 		DELAY(100);
    286 	}
    287 
    288 	if (i == RTK_TIMEOUT) {
    289 		printf("%s: PHY write reg %x <- %hx failed\n",
    290 		    device_xname(sc->sc_dev), reg, val);
    291 		return ETIMEDOUT;
    292 	}
    293 
    294 	return 0;
    295 }
    296 
    297 static int
    298 re_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    299 {
    300 	struct rtk_softc *sc = device_private(dev);
    301 	uint16_t re8139_reg = 0;
    302 	int s, rv = 0;
    303 
    304 	s = splnet();
    305 
    306 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
    307 		rv = re_gmii_readreg(dev, phy, reg, val);
    308 		splx(s);
    309 		return rv;
    310 	}
    311 
    312 	/* Pretend the internal PHY is only at address 0 */
    313 	if (phy) {
    314 		splx(s);
    315 		return -1;
    316 	}
    317 	switch (reg) {
    318 	case MII_BMCR:
    319 		re8139_reg = RTK_BMCR;
    320 		break;
    321 	case MII_BMSR:
    322 		re8139_reg = RTK_BMSR;
    323 		break;
    324 	case MII_ANAR:
    325 		re8139_reg = RTK_ANAR;
    326 		break;
    327 	case MII_ANER:
    328 		re8139_reg = RTK_ANER;
    329 		break;
    330 	case MII_ANLPAR:
    331 		re8139_reg = RTK_LPAR;
    332 		break;
    333 	case MII_PHYIDR1:
    334 	case MII_PHYIDR2:
    335 		*val = 0;
    336 		splx(s);
    337 		return 0;
    338 	/*
    339 	 * Allow the rlphy driver to read the media status
    340 	 * register. If we have a link partner which does not
    341 	 * support NWAY, this is the register which will tell
    342 	 * us the results of parallel detection.
    343 	 */
    344 	case RTK_MEDIASTAT:
    345 		*val = CSR_READ_1(sc, RTK_MEDIASTAT);
    346 		splx(s);
    347 		return 0;
    348 	default:
    349 		printf("%s: bad phy register\n", device_xname(sc->sc_dev));
    350 		splx(s);
    351 		return -1;
    352 	}
    353 	*val = CSR_READ_2(sc, re8139_reg);
    354 	if ((sc->sc_quirk & RTKQ_8139CPLUS) != 0 && re8139_reg == RTK_BMCR) {
    355 		/* 8139C+ has different bit layout. */
    356 		*val &= ~(BMCR_LOOP | BMCR_ISO);
    357 	}
    358 	splx(s);
    359 	return 0;
    360 }
    361 
    362 static int
    363 re_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    364 {
    365 	struct rtk_softc *sc = device_private(dev);
    366 	uint16_t re8139_reg = 0;
    367 	int s, rv;
    368 
    369 	s = splnet();
    370 
    371 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
    372 		rv = re_gmii_writereg(dev, phy, reg, val);
    373 		splx(s);
    374 		return rv;
    375 	}
    376 
    377 	/* Pretend the internal PHY is only at address 0 */
    378 	if (phy) {
    379 		splx(s);
    380 		return -1;
    381 	}
    382 	switch (reg) {
    383 	case MII_BMCR:
    384 		re8139_reg = RTK_BMCR;
    385 		if ((sc->sc_quirk & RTKQ_8139CPLUS) != 0) {
    386 			/* 8139C+ has different bit layout. */
    387 			val &= ~(BMCR_LOOP | BMCR_ISO);
    388 		}
    389 		break;
    390 	case MII_BMSR:
    391 		re8139_reg = RTK_BMSR;
    392 		break;
    393 	case MII_ANAR:
    394 		re8139_reg = RTK_ANAR;
    395 		break;
    396 	case MII_ANER:
    397 		re8139_reg = RTK_ANER;
    398 		break;
    399 	case MII_ANLPAR:
    400 		re8139_reg = RTK_LPAR;
    401 		break;
    402 	case MII_PHYIDR1:
    403 	case MII_PHYIDR2:
    404 		splx(s);
    405 		return 0;
    406 		break;
    407 	default:
    408 		printf("%s: bad phy register\n", device_xname(sc->sc_dev));
    409 		splx(s);
    410 		return -1;
    411 	}
    412 	CSR_WRITE_2(sc, re8139_reg, val);
    413 	splx(s);
    414 	return 0;
    415 }
    416 
    417 static void
    418 re_miibus_statchg(struct ifnet *ifp)
    419 {
    420 
    421 	return;
    422 }
    423 
    424 static void
    425 re_reset(struct rtk_softc *sc)
    426 {
    427 	int i;
    428 
    429 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
    430 
    431 	for (i = 0; i < RTK_TIMEOUT; i++) {
    432 		DELAY(10);
    433 		if ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET) == 0)
    434 			break;
    435 	}
    436 	if (i == RTK_TIMEOUT)
    437 		printf("%s: reset never completed!\n",
    438 		    device_xname(sc->sc_dev));
    439 
    440 	/*
    441 	 * NB: Realtek-supplied FreeBSD driver does this only for MACFG_3,
    442 	 *     but also says "Rtl8169s sigle chip detected".
    443 	 */
    444 	if ((sc->sc_quirk & RTKQ_MACLDPS) != 0)
    445 		CSR_WRITE_1(sc, RTK_LDPS, 1);
    446 
    447 }
    448 
    449 /*
    450  * The following routine is designed to test for a defect on some
    451  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
    452  * lines connected to the bus, however for a 32-bit only card, they
    453  * should be pulled high. The result of this defect is that the
    454  * NIC will not work right if you plug it into a 64-bit slot: DMA
    455  * operations will be done with 64-bit transfers, which will fail
    456  * because the 64-bit data lines aren't connected.
    457  *
    458  * There's no way to work around this (short of talking a soldering
    459  * iron to the board), however we can detect it. The method we use
    460  * here is to put the NIC into digital loopback mode, set the receiver
    461  * to promiscuous mode, and then try to send a frame. We then compare
    462  * the frame data we sent to what was received. If the data matches,
    463  * then the NIC is working correctly, otherwise we know the user has
    464  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
    465  * slot. In the latter case, there's no way the NIC can work correctly,
    466  * so we print out a message on the console and abort the device attach.
    467  */
    468 
    469 int
    470 re_diag(struct rtk_softc *sc)
    471 {
    472 	struct ifnet *ifp = &sc->ethercom.ec_if;
    473 	struct mbuf *m0;
    474 	struct ether_header *eh;
    475 	struct re_rxsoft *rxs;
    476 	struct re_desc *cur_rx;
    477 	bus_dmamap_t dmamap;
    478 	uint16_t status;
    479 	uint32_t rxstat;
    480 	int total_len, i, s, error = 0;
    481 	static const uint8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
    482 	static const uint8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
    483 
    484 	/* Allocate a single mbuf */
    485 
    486 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    487 	if (m0 == NULL)
    488 		return ENOBUFS;
    489 
    490 	/*
    491 	 * Initialize the NIC in test mode. This sets the chip up
    492 	 * so that it can send and receive frames, but performs the
    493 	 * following special functions:
    494 	 * - Puts receiver in promiscuous mode
    495 	 * - Enables digital loopback mode
    496 	 * - Leaves interrupts turned off
    497 	 */
    498 
    499 	ifp->if_flags |= IFF_PROMISC;
    500 	sc->re_testmode = 1;
    501 	re_init(ifp);
    502 	re_stop(ifp, 0);
    503 	DELAY(100000);
    504 	re_init(ifp);
    505 
    506 	/* Put some data in the mbuf */
    507 
    508 	eh = mtod(m0, struct ether_header *);
    509 	memcpy(eh->ether_dhost, &dst, ETHER_ADDR_LEN);
    510 	memcpy(eh->ether_shost, &src, ETHER_ADDR_LEN);
    511 	eh->ether_type = htons(ETHERTYPE_IP);
    512 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
    513 
    514 	/*
    515 	 * Queue the packet, start transmission.
    516 	 */
    517 
    518 	CSR_WRITE_2(sc, RTK_ISR, 0xFFFF);
    519 	s = splnet();
    520 	IF_ENQUEUE(&ifp->if_snd, m0);
    521 	re_start(ifp);
    522 	splx(s);
    523 	m0 = NULL;
    524 
    525 	/* Wait for it to propagate through the chip */
    526 
    527 	DELAY(100000);
    528 	for (i = 0; i < RTK_TIMEOUT; i++) {
    529 		status = CSR_READ_2(sc, RTK_ISR);
    530 		if ((status & (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK)) ==
    531 		    (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK))
    532 			break;
    533 		DELAY(10);
    534 	}
    535 	if (i == RTK_TIMEOUT) {
    536 		aprint_error_dev(sc->sc_dev,
    537 		    "diagnostic failed, failed to receive packet "
    538 		    "in loopback mode\n");
    539 		error = EIO;
    540 		goto done;
    541 	}
    542 
    543 	/*
    544 	 * The packet should have been dumped into the first
    545 	 * entry in the RX DMA ring. Grab it from there.
    546 	 */
    547 
    548 	rxs = &sc->re_ldata.re_rxsoft[0];
    549 	dmamap = rxs->rxs_dmamap;
    550 	bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    551 	    BUS_DMASYNC_POSTREAD);
    552 	bus_dmamap_unload(sc->sc_dmat, dmamap);
    553 
    554 	m0 = rxs->rxs_mbuf;
    555 	rxs->rxs_mbuf = NULL;
    556 	eh = mtod(m0, struct ether_header *);
    557 
    558 	RE_RXDESCSYNC(sc, 0, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    559 	cur_rx = &sc->re_ldata.re_rx_list[0];
    560 	rxstat = le32toh(cur_rx->re_cmdstat);
    561 	total_len = rxstat & sc->re_rxlenmask;
    562 
    563 	if (total_len != ETHER_MIN_LEN) {
    564 		aprint_error_dev(sc->sc_dev,
    565 		    "diagnostic failed, received short packet\n");
    566 		error = EIO;
    567 		goto done;
    568 	}
    569 
    570 	/* Test that the received packet data matches what we sent. */
    571 
    572 	if (memcmp(&eh->ether_dhost, &dst, ETHER_ADDR_LEN) ||
    573 	    memcmp(&eh->ether_shost, &src, ETHER_ADDR_LEN) ||
    574 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
    575 		aprint_error_dev(sc->sc_dev, "WARNING, DMA FAILURE!\n"
    576 		    "expected TX data: %s/%s/0x%x\n"
    577 		    "received RX data: %s/%s/0x%x\n"
    578 		    "You may have a defective 32-bit NIC plugged "
    579 		    "into a 64-bit PCI slot.\n"
    580 		    "Please re-install the NIC in a 32-bit slot "
    581 		    "for proper operation.\n"
    582 		    "Read the re(4) man page for more details.\n" ,
    583 		    ether_sprintf(dst),  ether_sprintf(src), ETHERTYPE_IP,
    584 		    ether_sprintf(eh->ether_dhost),
    585 		    ether_sprintf(eh->ether_shost), ntohs(eh->ether_type));
    586 		error = EIO;
    587 	}
    588 
    589  done:
    590 	/* Turn interface off, release resources */
    591 
    592 	sc->re_testmode = 0;
    593 	ifp->if_flags &= ~IFF_PROMISC;
    594 	re_stop(ifp, 0);
    595 	if (m0 != NULL)
    596 		m_freem(m0);
    597 
    598 	return error;
    599 }
    600 
    601 
    602 /*
    603  * Attach the interface. Allocate softc structures, do ifmedia
    604  * setup and ethernet/BPF attach.
    605  */
    606 void
    607 re_attach(struct rtk_softc *sc)
    608 {
    609 	uint8_t eaddr[ETHER_ADDR_LEN];
    610 	struct ifnet *ifp;
    611 	struct mii_data *mii = &sc->mii;
    612 	int error = 0, i;
    613 	const struct re_revision *rr;
    614 	const char *re_name = NULL;
    615 
    616 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
    617 		/* Revision of 8169/8169S/8110s in bits 30..26, 23 */
    618 		sc->sc_hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    619 
    620 		for (rr = re_revisions; rr->re_name != NULL; rr++) {
    621 			if (rr->re_chipid == sc->sc_hwrev)
    622 				re_name = rr->re_name;
    623 		}
    624 
    625 		if (re_name == NULL)
    626 			aprint_normal_dev(sc->sc_dev,
    627 			    "unknown ASIC (0x%04x)\n", sc->sc_hwrev >> 16);
    628 		else
    629 			aprint_normal_dev(sc->sc_dev,
    630 			    "%s (0x%04x)\n", re_name, sc->sc_hwrev >> 16);
    631 
    632 		switch (sc->sc_hwrev) {
    633 		case RTK_HWREV_8169:
    634 			sc->sc_quirk |= RTKQ_8169NONS;
    635 			break;
    636 		case RTK_HWREV_8169S:
    637 		case RTK_HWREV_8110S:
    638 		case RTK_HWREV_8169_8110SB:
    639 		case RTK_HWREV_8169_8110SBL:
    640 		case RTK_HWREV_8169_8110SC:
    641 			sc->sc_quirk |= RTKQ_MACLDPS;
    642 			break;
    643 		case RTK_HWREV_8168B_SPIN1:
    644 		case RTK_HWREV_8168B_SPIN2:
    645 		case RTK_HWREV_8168B_SPIN3:
    646 			sc->sc_quirk |= RTKQ_MACSTAT;
    647 			break;
    648 		case RTK_HWREV_8168C:
    649 		case RTK_HWREV_8168C_SPIN2:
    650 		case RTK_HWREV_8168CP:
    651 		case RTK_HWREV_8168D:
    652 		case RTK_HWREV_8168DP:
    653 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    654 			    RTKQ_MACSTAT | RTKQ_CMDSTOP;
    655 			/*
    656 			 * From FreeBSD driver:
    657 			 *
    658 			 * These (8168/8111) controllers support jumbo frame
    659 			 * but it seems that enabling it requires touching
    660 			 * additional magic registers. Depending on MAC
    661 			 * revisions some controllers need to disable
    662 			 * checksum offload. So disable jumbo frame until
    663 			 * I have better idea what it really requires to
    664 			 * make it support.
    665 			 * RTL8168C/CP : supports up to 6KB jumbo frame.
    666 			 * RTL8111C/CP : supports up to 9KB jumbo frame.
    667 			 */
    668 			sc->sc_quirk |= RTKQ_NOJUMBO;
    669 			break;
    670 		case RTK_HWREV_8168E:
    671 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    672 			    RTKQ_MACSTAT | RTKQ_CMDSTOP | RTKQ_PHYWAKE_PM |
    673 			    RTKQ_NOJUMBO;
    674 			break;
    675 		case RTK_HWREV_8168E_VL:
    676 		case RTK_HWREV_8168F:
    677 		case RTK_HWREV_8411:
    678 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    679 			    RTKQ_MACSTAT | RTKQ_CMDSTOP | RTKQ_NOJUMBO;
    680 			break;
    681 		case RTK_HWREV_8168EP:
    682 		case RTK_HWREV_8168FP:
    683 		case RTK_HWREV_8168G:
    684 		case RTK_HWREV_8168GU:
    685 		case RTK_HWREV_8168H:
    686 		case RTK_HWREV_8411B:
    687 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    688 			    RTKQ_MACSTAT | RTKQ_CMDSTOP | RTKQ_NOJUMBO |
    689 			    RTKQ_RXDV_GATED | RTKQ_TXRXEN_LATER;
    690 			break;
    691 		case RTK_HWREV_8100E:
    692 		case RTK_HWREV_8100E_SPIN2:
    693 		case RTK_HWREV_8101E:
    694 			sc->sc_quirk |= RTKQ_NOJUMBO;
    695 			break;
    696 		case RTK_HWREV_8102E:
    697 		case RTK_HWREV_8102EL:
    698 		case RTK_HWREV_8102EL_SPIN1:
    699 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    700 			    RTKQ_MACSTAT | RTKQ_CMDSTOP | RTKQ_NOJUMBO;
    701 			break;
    702 		case RTK_HWREV_8103E:
    703 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD |
    704 			    RTKQ_MACSTAT | RTKQ_CMDSTOP;
    705 			break;
    706 		case RTK_HWREV_8401E:
    707 		case RTK_HWREV_8105E:
    708 		case RTK_HWREV_8105E_SPIN1: /* XXX */
    709 		case RTK_HWREV_8106E:
    710 			sc->sc_quirk |= RTKQ_PHYWAKE_PM |
    711 			    RTKQ_DESCV2 | RTKQ_NOEECMD | RTKQ_MACSTAT |
    712 			    RTKQ_CMDSTOP;
    713 			break;
    714 		case RTK_HWREV_8402:
    715 			sc->sc_quirk |= RTKQ_PHYWAKE_PM |
    716 			    RTKQ_DESCV2 | RTKQ_NOEECMD | RTKQ_MACSTAT |
    717 			    RTKQ_CMDSTOP; /* CMDSTOP_WAIT_TXQ */
    718 			break;
    719 		default:
    720 			aprint_normal_dev(sc->sc_dev, "Use default quirks\n");
    721 			/* assume the latest features */
    722 			sc->sc_quirk |= RTKQ_DESCV2 | RTKQ_NOEECMD;
    723 			sc->sc_quirk |= RTKQ_NOJUMBO;
    724 		}
    725 
    726 		/* Set RX length mask */
    727 		sc->re_rxlenmask = RE_RDESC_STAT_GFRAGLEN;
    728 		sc->re_ldata.re_tx_desc_cnt = RE_TX_DESC_CNT_8169;
    729 	} else {
    730 		sc->sc_quirk |= RTKQ_NOJUMBO;
    731 
    732 		/* Set RX length mask */
    733 		sc->re_rxlenmask = RE_RDESC_STAT_FRAGLEN;
    734 		sc->re_ldata.re_tx_desc_cnt = RE_TX_DESC_CNT_8139;
    735 	}
    736 
    737 	/* Reset the adapter. */
    738 	re_reset(sc);
    739 
    740 	/*
    741 	 * RTL81x9 chips automatically read EEPROM to init MAC address,
    742 	 * and some NAS override its MAC address per own configuration,
    743 	 * so no need to explicitely read EEPROM and set ID registers.
    744 	 */
    745 #ifdef RE_USE_EECMD
    746 	if ((sc->sc_quirk & RTKQ_NOEECMD) != 0) {
    747 		/*
    748 		 * Get station address from ID registers.
    749 		 */
    750 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    751 			eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
    752 	} else {
    753 		uint16_t val;
    754 		int addr_len;
    755 
    756 		/*
    757 		 * Get station address from the EEPROM.
    758 		 */
    759 		if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
    760 			addr_len = RTK_EEADDR_LEN1;
    761 		else
    762 			addr_len = RTK_EEADDR_LEN0;
    763 
    764 		/*
    765 		 * Get station address from the EEPROM.
    766 		 */
    767 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
    768 			val = rtk_read_eeprom(sc, RTK_EE_EADDR0 + i, addr_len);
    769 			eaddr[(i * 2) + 0] = val & 0xff;
    770 			eaddr[(i * 2) + 1] = val >> 8;
    771 		}
    772 	}
    773 #else
    774 	/*
    775 	 * Get station address from ID registers.
    776 	 */
    777 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    778 		eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
    779 #endif
    780 
    781 	/* Take PHY out of power down mode. */
    782 	if ((sc->sc_quirk & RTKQ_PHYWAKE_PM) != 0)
    783 		CSR_WRITE_1(sc, RTK_PMCH, CSR_READ_1(sc, RTK_PMCH) | 0x80);
    784 
    785 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    786 	    ether_sprintf(eaddr));
    787 
    788 	if (sc->re_ldata.re_tx_desc_cnt >
    789 	    PAGE_SIZE / sizeof(struct re_desc)) {
    790 		sc->re_ldata.re_tx_desc_cnt =
    791 		    PAGE_SIZE / sizeof(struct re_desc);
    792 	}
    793 
    794 	aprint_verbose_dev(sc->sc_dev, "using %d tx descriptors\n",
    795 	    sc->re_ldata.re_tx_desc_cnt);
    796 	KASSERT(RE_NEXT_TX_DESC(sc, RE_TX_DESC_CNT(sc) - 1) == 0);
    797 
    798 	/* Allocate DMA'able memory for the TX ring */
    799 	if ((error = bus_dmamem_alloc(sc->sc_dmat, RE_TX_LIST_SZ(sc),
    800 	    RE_RING_ALIGN, 0, &sc->re_ldata.re_tx_listseg, 1,
    801 	    &sc->re_ldata.re_tx_listnseg, BUS_DMA_NOWAIT)) != 0) {
    802 		aprint_error_dev(sc->sc_dev,
    803 		    "can't allocate tx listseg, error = %d\n", error);
    804 		goto fail_0;
    805 	}
    806 
    807 	/* Load the map for the TX ring. */
    808 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->re_ldata.re_tx_listseg,
    809 	    sc->re_ldata.re_tx_listnseg, RE_TX_LIST_SZ(sc),
    810 	    (void **)&sc->re_ldata.re_tx_list,
    811 	    BUS_DMA_COHERENT | BUS_DMA_NOWAIT)) != 0) {
    812 		aprint_error_dev(sc->sc_dev,
    813 		    "can't map tx list, error = %d\n", error);
    814 		goto fail_1;
    815 	}
    816 	memset(sc->re_ldata.re_tx_list, 0, RE_TX_LIST_SZ(sc));
    817 
    818 	if ((error = bus_dmamap_create(sc->sc_dmat, RE_TX_LIST_SZ(sc), 1,
    819 	    RE_TX_LIST_SZ(sc), 0, 0,
    820 	    &sc->re_ldata.re_tx_list_map)) != 0) {
    821 		aprint_error_dev(sc->sc_dev,
    822 		    "can't create tx list map, error = %d\n", error);
    823 		goto fail_2;
    824 	}
    825 
    826 
    827 	if ((error = bus_dmamap_load(sc->sc_dmat,
    828 	    sc->re_ldata.re_tx_list_map, sc->re_ldata.re_tx_list,
    829 	    RE_TX_LIST_SZ(sc), NULL, BUS_DMA_NOWAIT)) != 0) {
    830 		aprint_error_dev(sc->sc_dev,
    831 		    "can't load tx list, error = %d\n", error);
    832 		goto fail_3;
    833 	}
    834 
    835 	/* Create DMA maps for TX buffers */
    836 	for (i = 0; i < RE_TX_QLEN; i++) {
    837 		error = bus_dmamap_create(sc->sc_dmat,
    838 		    round_page(IP_MAXPACKET),
    839 		    RE_TX_DESC_CNT(sc), RE_TDESC_CMD_FRAGLEN,
    840 		    0, 0, &sc->re_ldata.re_txq[i].txq_dmamap);
    841 		if (error) {
    842 			aprint_error_dev(sc->sc_dev,
    843 			    "can't create DMA map for TX\n");
    844 			goto fail_4;
    845 		}
    846 	}
    847 
    848 	/* Allocate DMA'able memory for the RX ring */
    849 	/* XXX see also a comment about RE_RX_DMAMEM_SZ in rtl81x9var.h */
    850 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    851 	    RE_RX_DMAMEM_SZ, RE_RING_ALIGN, 0, &sc->re_ldata.re_rx_listseg, 1,
    852 	    &sc->re_ldata.re_rx_listnseg, BUS_DMA_NOWAIT)) != 0) {
    853 		aprint_error_dev(sc->sc_dev,
    854 		    "can't allocate rx listseg, error = %d\n", error);
    855 		goto fail_4;
    856 	}
    857 
    858 	/* Load the map for the RX ring. */
    859 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->re_ldata.re_rx_listseg,
    860 	    sc->re_ldata.re_rx_listnseg, RE_RX_DMAMEM_SZ,
    861 	    (void **)&sc->re_ldata.re_rx_list,
    862 	    BUS_DMA_COHERENT | BUS_DMA_NOWAIT)) != 0) {
    863 		aprint_error_dev(sc->sc_dev,
    864 		    "can't map rx list, error = %d\n", error);
    865 		goto fail_5;
    866 	}
    867 	memset(sc->re_ldata.re_rx_list, 0, RE_RX_DMAMEM_SZ);
    868 
    869 	if ((error = bus_dmamap_create(sc->sc_dmat,
    870 	    RE_RX_DMAMEM_SZ, 1, RE_RX_DMAMEM_SZ, 0, 0,
    871 	    &sc->re_ldata.re_rx_list_map)) != 0) {
    872 		aprint_error_dev(sc->sc_dev,
    873 		    "can't create rx list map, error = %d\n", error);
    874 		goto fail_6;
    875 	}
    876 
    877 	if ((error = bus_dmamap_load(sc->sc_dmat,
    878 	    sc->re_ldata.re_rx_list_map, sc->re_ldata.re_rx_list,
    879 	    RE_RX_DMAMEM_SZ, NULL, BUS_DMA_NOWAIT)) != 0) {
    880 		aprint_error_dev(sc->sc_dev,
    881 		    "can't load rx list, error = %d\n", error);
    882 		goto fail_7;
    883 	}
    884 
    885 	/* Create DMA maps for RX buffers */
    886 	for (i = 0; i < RE_RX_DESC_CNT; i++) {
    887 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    888 		    0, 0, &sc->re_ldata.re_rxsoft[i].rxs_dmamap);
    889 		if (error) {
    890 			aprint_error_dev(sc->sc_dev,
    891 			    "can't create DMA map for RX\n");
    892 			goto fail_8;
    893 		}
    894 	}
    895 
    896 	/*
    897 	 * Record interface as attached. From here, we should not fail.
    898 	 */
    899 	sc->sc_flags |= RTK_ATTACHED;
    900 
    901 	ifp = &sc->ethercom.ec_if;
    902 	ifp->if_softc = sc;
    903 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    904 	ifp->if_mtu = ETHERMTU;
    905 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    906 	ifp->if_ioctl = re_ioctl;
    907 	sc->ethercom.ec_capabilities |=
    908 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
    909 	ifp->if_start = re_start;
    910 	ifp->if_stop = re_stop;
    911 
    912 	/*
    913 	 * IFCAP_CSUM_IPv4_Tx on re(4) is broken for small packets,
    914 	 * so we have a workaround to handle the bug by padding
    915 	 * such packets manually.
    916 	 */
    917 	ifp->if_capabilities |=
    918 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    919 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    920 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
    921 	    IFCAP_TSOv4;
    922 
    923 	ifp->if_watchdog = re_watchdog;
    924 	ifp->if_init = re_init;
    925 	ifp->if_snd.ifq_maxlen = RE_IFQ_MAXLEN;
    926 	ifp->if_capenable = ifp->if_capabilities;
    927 	IFQ_SET_READY(&ifp->if_snd);
    928 
    929 	callout_init(&sc->rtk_tick_ch, 0);
    930 
    931 	/* Do MII setup */
    932 	mii->mii_ifp = ifp;
    933 	mii->mii_readreg = re_miibus_readreg;
    934 	mii->mii_writereg = re_miibus_writereg;
    935 	mii->mii_statchg = re_miibus_statchg;
    936 	sc->ethercom.ec_mii = mii;
    937 	ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
    938 	    ether_mediastatus);
    939 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
    940 	    MII_OFFSET_ANY, 0);
    941 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    942 
    943 	/*
    944 	 * Call MI attach routine.
    945 	 */
    946 	if_attach(ifp);
    947 	if_deferred_start_init(ifp, NULL);
    948 	ether_ifattach(ifp, eaddr);
    949 
    950 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    951 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    952 
    953 	if (pmf_device_register(sc->sc_dev, NULL, NULL))
    954 		pmf_class_network_register(sc->sc_dev, ifp);
    955 	else
    956 		aprint_error_dev(sc->sc_dev,
    957 		    "couldn't establish power handler\n");
    958 
    959 	return;
    960 
    961  fail_8:
    962 	/* Destroy DMA maps for RX buffers. */
    963 	for (i = 0; i < RE_RX_DESC_CNT; i++)
    964 		if (sc->re_ldata.re_rxsoft[i].rxs_dmamap != NULL)
    965 			bus_dmamap_destroy(sc->sc_dmat,
    966 			    sc->re_ldata.re_rxsoft[i].rxs_dmamap);
    967 
    968 	/* Free DMA'able memory for the RX ring. */
    969 	bus_dmamap_unload(sc->sc_dmat, sc->re_ldata.re_rx_list_map);
    970  fail_7:
    971 	bus_dmamap_destroy(sc->sc_dmat, sc->re_ldata.re_rx_list_map);
    972  fail_6:
    973 	bus_dmamem_unmap(sc->sc_dmat,
    974 	    (void *)sc->re_ldata.re_rx_list, RE_RX_DMAMEM_SZ);
    975  fail_5:
    976 	bus_dmamem_free(sc->sc_dmat,
    977 	    &sc->re_ldata.re_rx_listseg, sc->re_ldata.re_rx_listnseg);
    978 
    979  fail_4:
    980 	/* Destroy DMA maps for TX buffers. */
    981 	for (i = 0; i < RE_TX_QLEN; i++)
    982 		if (sc->re_ldata.re_txq[i].txq_dmamap != NULL)
    983 			bus_dmamap_destroy(sc->sc_dmat,
    984 			    sc->re_ldata.re_txq[i].txq_dmamap);
    985 
    986 	/* Free DMA'able memory for the TX ring. */
    987 	bus_dmamap_unload(sc->sc_dmat, sc->re_ldata.re_tx_list_map);
    988  fail_3:
    989 	bus_dmamap_destroy(sc->sc_dmat, sc->re_ldata.re_tx_list_map);
    990  fail_2:
    991 	bus_dmamem_unmap(sc->sc_dmat,
    992 	    (void *)sc->re_ldata.re_tx_list, RE_TX_LIST_SZ(sc));
    993  fail_1:
    994 	bus_dmamem_free(sc->sc_dmat,
    995 	    &sc->re_ldata.re_tx_listseg, sc->re_ldata.re_tx_listnseg);
    996  fail_0:
    997 	return;
    998 }
    999 
   1000 
   1001 /*
   1002  * re_activate:
   1003  *     Handle device activation/deactivation requests.
   1004  */
   1005 int
   1006 re_activate(device_t self, enum devact act)
   1007 {
   1008 	struct rtk_softc *sc = device_private(self);
   1009 
   1010 	switch (act) {
   1011 	case DVACT_DEACTIVATE:
   1012 		if_deactivate(&sc->ethercom.ec_if);
   1013 		return 0;
   1014 	default:
   1015 		return EOPNOTSUPP;
   1016 	}
   1017 }
   1018 
   1019 /*
   1020  * re_detach:
   1021  *     Detach a rtk interface.
   1022  */
   1023 int
   1024 re_detach(struct rtk_softc *sc)
   1025 {
   1026 	struct ifnet *ifp = &sc->ethercom.ec_if;
   1027 	int i;
   1028 
   1029 	/*
   1030 	 * Succeed now if there isn't any work to do.
   1031 	 */
   1032 	if ((sc->sc_flags & RTK_ATTACHED) == 0)
   1033 		return 0;
   1034 
   1035 	/* Unhook our tick handler. */
   1036 	callout_stop(&sc->rtk_tick_ch);
   1037 
   1038 	/* Detach all PHYs. */
   1039 	mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1040 
   1041 	/* Delete all remaining media. */
   1042 	ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
   1043 
   1044 	rnd_detach_source(&sc->rnd_source);
   1045 	ether_ifdetach(ifp);
   1046 	if_detach(ifp);
   1047 
   1048 	/* Destroy DMA maps for RX buffers. */
   1049 	for (i = 0; i < RE_RX_DESC_CNT; i++)
   1050 		if (sc->re_ldata.re_rxsoft[i].rxs_dmamap != NULL)
   1051 			bus_dmamap_destroy(sc->sc_dmat,
   1052 			    sc->re_ldata.re_rxsoft[i].rxs_dmamap);
   1053 
   1054 	/* Free DMA'able memory for the RX ring. */
   1055 	bus_dmamap_unload(sc->sc_dmat, sc->re_ldata.re_rx_list_map);
   1056 	bus_dmamap_destroy(sc->sc_dmat, sc->re_ldata.re_rx_list_map);
   1057 	bus_dmamem_unmap(sc->sc_dmat,
   1058 	    (void *)sc->re_ldata.re_rx_list, RE_RX_DMAMEM_SZ);
   1059 	bus_dmamem_free(sc->sc_dmat,
   1060 	    &sc->re_ldata.re_rx_listseg, sc->re_ldata.re_rx_listnseg);
   1061 
   1062 	/* Destroy DMA maps for TX buffers. */
   1063 	for (i = 0; i < RE_TX_QLEN; i++)
   1064 		if (sc->re_ldata.re_txq[i].txq_dmamap != NULL)
   1065 			bus_dmamap_destroy(sc->sc_dmat,
   1066 			    sc->re_ldata.re_txq[i].txq_dmamap);
   1067 
   1068 	/* Free DMA'able memory for the TX ring. */
   1069 	bus_dmamap_unload(sc->sc_dmat, sc->re_ldata.re_tx_list_map);
   1070 	bus_dmamap_destroy(sc->sc_dmat, sc->re_ldata.re_tx_list_map);
   1071 	bus_dmamem_unmap(sc->sc_dmat,
   1072 	    (void *)sc->re_ldata.re_tx_list, RE_TX_LIST_SZ(sc));
   1073 	bus_dmamem_free(sc->sc_dmat,
   1074 	    &sc->re_ldata.re_tx_listseg, sc->re_ldata.re_tx_listnseg);
   1075 
   1076 	pmf_device_deregister(sc->sc_dev);
   1077 
   1078 	/* we don't want to run again */
   1079 	sc->sc_flags &= ~RTK_ATTACHED;
   1080 
   1081 	return 0;
   1082 }
   1083 
   1084 /*
   1085  * re_enable:
   1086  *     Enable the RTL81X9 chip.
   1087  */
   1088 static int
   1089 re_enable(struct rtk_softc *sc)
   1090 {
   1091 
   1092 	if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
   1093 		if ((*sc->sc_enable)(sc) != 0) {
   1094 			printf("%s: device enable failed\n",
   1095 			    device_xname(sc->sc_dev));
   1096 			return EIO;
   1097 		}
   1098 		sc->sc_flags |= RTK_ENABLED;
   1099 	}
   1100 	return 0;
   1101 }
   1102 
   1103 /*
   1104  * re_disable:
   1105  *     Disable the RTL81X9 chip.
   1106  */
   1107 static void
   1108 re_disable(struct rtk_softc *sc)
   1109 {
   1110 
   1111 	if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
   1112 		(*sc->sc_disable)(sc);
   1113 		sc->sc_flags &= ~RTK_ENABLED;
   1114 	}
   1115 }
   1116 
   1117 static int
   1118 re_newbuf(struct rtk_softc *sc, int idx, struct mbuf *m)
   1119 {
   1120 	struct mbuf *n = NULL;
   1121 	bus_dmamap_t map;
   1122 	struct re_desc *d;
   1123 	struct re_rxsoft *rxs;
   1124 	uint32_t cmdstat;
   1125 	int error;
   1126 
   1127 	if (m == NULL) {
   1128 		MGETHDR(n, M_DONTWAIT, MT_DATA);
   1129 		if (n == NULL)
   1130 			return ENOBUFS;
   1131 
   1132 		MCLGET(n, M_DONTWAIT);
   1133 		if ((n->m_flags & M_EXT) == 0) {
   1134 			m_freem(n);
   1135 			return ENOBUFS;
   1136 		}
   1137 		m = n;
   1138 	} else
   1139 		m->m_data = m->m_ext.ext_buf;
   1140 
   1141 	/*
   1142 	 * Initialize mbuf length fields and fixup
   1143 	 * alignment so that the frame payload is
   1144 	 * longword aligned.
   1145 	 */
   1146 	m->m_len = m->m_pkthdr.len = MCLBYTES - RE_ETHER_ALIGN;
   1147 	m->m_data += RE_ETHER_ALIGN;
   1148 
   1149 	rxs = &sc->re_ldata.re_rxsoft[idx];
   1150 	map = rxs->rxs_dmamap;
   1151 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1152 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1153 
   1154 	if (error)
   1155 		goto out;
   1156 
   1157 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1158 	    BUS_DMASYNC_PREREAD);
   1159 
   1160 	d = &sc->re_ldata.re_rx_list[idx];
   1161 #ifdef DIAGNOSTIC
   1162 	RE_RXDESCSYNC(sc, idx, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1163 	cmdstat = le32toh(d->re_cmdstat);
   1164 	RE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1165 	if (cmdstat & RE_RDESC_STAT_OWN) {
   1166 		panic("%s: tried to map busy RX descriptor",
   1167 		    device_xname(sc->sc_dev));
   1168 	}
   1169 #endif
   1170 
   1171 	rxs->rxs_mbuf = m;
   1172 
   1173 	d->re_vlanctl = 0;
   1174 	cmdstat = map->dm_segs[0].ds_len;
   1175 	if (idx == (RE_RX_DESC_CNT - 1))
   1176 		cmdstat |= RE_RDESC_CMD_EOR;
   1177 	re_set_bufaddr(d, map->dm_segs[0].ds_addr);
   1178 	d->re_cmdstat = htole32(cmdstat);
   1179 	RE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1180 	cmdstat |= RE_RDESC_CMD_OWN;
   1181 	d->re_cmdstat = htole32(cmdstat);
   1182 	RE_RXDESCSYNC(sc, idx, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1183 
   1184 	return 0;
   1185  out:
   1186 	if (n != NULL)
   1187 		m_freem(n);
   1188 	return ENOMEM;
   1189 }
   1190 
   1191 static int
   1192 re_tx_list_init(struct rtk_softc *sc)
   1193 {
   1194 	int i;
   1195 
   1196 	memset(sc->re_ldata.re_tx_list, 0, RE_TX_LIST_SZ(sc));
   1197 	for (i = 0; i < RE_TX_QLEN; i++) {
   1198 		sc->re_ldata.re_txq[i].txq_mbuf = NULL;
   1199 	}
   1200 
   1201 	bus_dmamap_sync(sc->sc_dmat,
   1202 	    sc->re_ldata.re_tx_list_map, 0,
   1203 	    sc->re_ldata.re_tx_list_map->dm_mapsize,
   1204 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1205 	sc->re_ldata.re_txq_prodidx = 0;
   1206 	sc->re_ldata.re_txq_considx = 0;
   1207 	sc->re_ldata.re_txq_free = RE_TX_QLEN;
   1208 	sc->re_ldata.re_tx_free = RE_TX_DESC_CNT(sc);
   1209 	sc->re_ldata.re_tx_nextfree = 0;
   1210 
   1211 	return 0;
   1212 }
   1213 
   1214 static int
   1215 re_rx_list_init(struct rtk_softc *sc)
   1216 {
   1217 	int i;
   1218 
   1219 	memset(sc->re_ldata.re_rx_list, 0, RE_RX_LIST_SZ);
   1220 
   1221 	for (i = 0; i < RE_RX_DESC_CNT; i++) {
   1222 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
   1223 			return ENOBUFS;
   1224 	}
   1225 
   1226 	sc->re_ldata.re_rx_prodidx = 0;
   1227 	sc->re_head = sc->re_tail = NULL;
   1228 
   1229 	return 0;
   1230 }
   1231 
   1232 /*
   1233  * RX handler for C+ and 8169. For the gigE chips, we support
   1234  * the reception of jumbo frames that have been fragmented
   1235  * across multiple 2K mbuf cluster buffers.
   1236  */
   1237 static void
   1238 re_rxeof(struct rtk_softc *sc)
   1239 {
   1240 	struct mbuf *m;
   1241 	struct ifnet *ifp;
   1242 	int i, total_len;
   1243 	struct re_desc *cur_rx;
   1244 	struct re_rxsoft *rxs;
   1245 	uint32_t rxstat, rxvlan;
   1246 
   1247 	ifp = &sc->ethercom.ec_if;
   1248 
   1249 	for (i = sc->re_ldata.re_rx_prodidx;; i = RE_NEXT_RX_DESC(sc, i)) {
   1250 		cur_rx = &sc->re_ldata.re_rx_list[i];
   1251 		RE_RXDESCSYNC(sc, i,
   1252 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1253 		rxstat = le32toh(cur_rx->re_cmdstat);
   1254 		rxvlan = le32toh(cur_rx->re_vlanctl);
   1255 		RE_RXDESCSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1256 		if ((rxstat & RE_RDESC_STAT_OWN) != 0) {
   1257 			break;
   1258 		}
   1259 		total_len = rxstat & sc->re_rxlenmask;
   1260 		rxs = &sc->re_ldata.re_rxsoft[i];
   1261 		m = rxs->rxs_mbuf;
   1262 
   1263 		/* Invalidate the RX mbuf and unload its map */
   1264 
   1265 		bus_dmamap_sync(sc->sc_dmat,
   1266 		    rxs->rxs_dmamap, 0, rxs->rxs_dmamap->dm_mapsize,
   1267 		    BUS_DMASYNC_POSTREAD);
   1268 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1269 
   1270 		if ((rxstat & RE_RDESC_STAT_EOF) == 0) {
   1271 			m->m_len = MCLBYTES - RE_ETHER_ALIGN;
   1272 			if (sc->re_head == NULL)
   1273 				sc->re_head = sc->re_tail = m;
   1274 			else {
   1275 				m_remove_pkthdr(m);
   1276 				sc->re_tail->m_next = m;
   1277 				sc->re_tail = m;
   1278 			}
   1279 			re_newbuf(sc, i, NULL);
   1280 			continue;
   1281 		}
   1282 
   1283 		/*
   1284 		 * NOTE: for the 8139C+, the frame length field
   1285 		 * is always 12 bits in size, but for the gigE chips,
   1286 		 * it is 13 bits (since the max RX frame length is 16K).
   1287 		 * Unfortunately, all 32 bits in the status word
   1288 		 * were already used, so to make room for the extra
   1289 		 * length bit, RealTek took out the 'frame alignment
   1290 		 * error' bit and shifted the other status bits
   1291 		 * over one slot. The OWN, EOR, FS and LS bits are
   1292 		 * still in the same places. We have already extracted
   1293 		 * the frame length and checked the OWN bit, so rather
   1294 		 * than using an alternate bit mapping, we shift the
   1295 		 * status bits one space to the right so we can evaluate
   1296 		 * them using the 8169 status as though it was in the
   1297 		 * same format as that of the 8139C+.
   1298 		 */
   1299 		if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0)
   1300 			rxstat >>= 1;
   1301 
   1302 		if (__predict_false((rxstat & RE_RDESC_STAT_RXERRSUM) != 0)) {
   1303 #ifdef RE_DEBUG
   1304 			printf("%s: RX error (rxstat = 0x%08x)",
   1305 			    device_xname(sc->sc_dev), rxstat);
   1306 			if (rxstat & RE_RDESC_STAT_FRALIGN)
   1307 				printf(", frame alignment error");
   1308 			if (rxstat & RE_RDESC_STAT_BUFOFLOW)
   1309 				printf(", out of buffer space");
   1310 			if (rxstat & RE_RDESC_STAT_FIFOOFLOW)
   1311 				printf(", FIFO overrun");
   1312 			if (rxstat & RE_RDESC_STAT_GIANT)
   1313 				printf(", giant packet");
   1314 			if (rxstat & RE_RDESC_STAT_RUNT)
   1315 				printf(", runt packet");
   1316 			if (rxstat & RE_RDESC_STAT_CRCERR)
   1317 				printf(", CRC error");
   1318 			printf("\n");
   1319 #endif
   1320 			ifp->if_ierrors++;
   1321 			/*
   1322 			 * If this is part of a multi-fragment packet,
   1323 			 * discard all the pieces.
   1324 			 */
   1325 			if (sc->re_head != NULL) {
   1326 				m_freem(sc->re_head);
   1327 				sc->re_head = sc->re_tail = NULL;
   1328 			}
   1329 			re_newbuf(sc, i, m);
   1330 			continue;
   1331 		}
   1332 
   1333 		/*
   1334 		 * If allocating a replacement mbuf fails,
   1335 		 * reload the current one.
   1336 		 */
   1337 
   1338 		if (__predict_false(re_newbuf(sc, i, NULL) != 0)) {
   1339 			ifp->if_ierrors++;
   1340 			if (sc->re_head != NULL) {
   1341 				m_freem(sc->re_head);
   1342 				sc->re_head = sc->re_tail = NULL;
   1343 			}
   1344 			re_newbuf(sc, i, m);
   1345 			continue;
   1346 		}
   1347 
   1348 		if (sc->re_head != NULL) {
   1349 			m->m_len = total_len % (MCLBYTES - RE_ETHER_ALIGN);
   1350 			/*
   1351 			 * Special case: if there's 4 bytes or less
   1352 			 * in this buffer, the mbuf can be discarded:
   1353 			 * the last 4 bytes is the CRC, which we don't
   1354 			 * care about anyway.
   1355 			 */
   1356 			if (m->m_len <= ETHER_CRC_LEN) {
   1357 				sc->re_tail->m_len -=
   1358 				    (ETHER_CRC_LEN - m->m_len);
   1359 				m_freem(m);
   1360 			} else {
   1361 				m->m_len -= ETHER_CRC_LEN;
   1362 				m_remove_pkthdr(m);
   1363 				sc->re_tail->m_next = m;
   1364 			}
   1365 			m = sc->re_head;
   1366 			sc->re_head = sc->re_tail = NULL;
   1367 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
   1368 		} else
   1369 			m->m_pkthdr.len = m->m_len =
   1370 			    (total_len - ETHER_CRC_LEN);
   1371 
   1372 		m_set_rcvif(m, ifp);
   1373 
   1374 		/* Do RX checksumming */
   1375 		if ((sc->sc_quirk & RTKQ_DESCV2) == 0) {
   1376 			/* Check IP header checksum */
   1377 			if ((rxstat & RE_RDESC_STAT_PROTOID) != 0) {
   1378 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1379 				if (rxstat & RE_RDESC_STAT_IPSUMBAD)
   1380 					m->m_pkthdr.csum_flags |=
   1381 					    M_CSUM_IPv4_BAD;
   1382 
   1383 				/* Check TCP/UDP checksum */
   1384 				if (RE_TCPPKT(rxstat)) {
   1385 					m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1386 					if (rxstat & RE_RDESC_STAT_TCPSUMBAD)
   1387 						m->m_pkthdr.csum_flags |=
   1388 						    M_CSUM_TCP_UDP_BAD;
   1389 				} else if (RE_UDPPKT(rxstat)) {
   1390 					m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1391 					if (rxstat & RE_RDESC_STAT_UDPSUMBAD) {
   1392 						/*
   1393 						 * XXX: 8139C+ thinks UDP csum
   1394 						 * 0xFFFF is bad, force software
   1395 						 * calculation.
   1396 						 */
   1397 						if (sc->sc_quirk & RTKQ_8139CPLUS)
   1398 							m->m_pkthdr.csum_flags
   1399 							    &= ~M_CSUM_UDPv4;
   1400 						else
   1401 							m->m_pkthdr.csum_flags
   1402 							    |= M_CSUM_TCP_UDP_BAD;
   1403 					}
   1404 				}
   1405 			}
   1406 		} else {
   1407 			/* Check IPv4 header checksum */
   1408 			if ((rxvlan & RE_RDESC_VLANCTL_IPV4) != 0) {
   1409 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1410 				if (rxstat & RE_RDESC_STAT_IPSUMBAD)
   1411 					m->m_pkthdr.csum_flags |=
   1412 					    M_CSUM_IPv4_BAD;
   1413 
   1414 				/* Check TCPv4/UDPv4 checksum */
   1415 				if (RE_TCPPKT(rxstat)) {
   1416 					m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1417 					if (rxstat & RE_RDESC_STAT_TCPSUMBAD)
   1418 						m->m_pkthdr.csum_flags |=
   1419 						    M_CSUM_TCP_UDP_BAD;
   1420 				} else if (RE_UDPPKT(rxstat)) {
   1421 					m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1422 					if (rxstat & RE_RDESC_STAT_UDPSUMBAD)
   1423 						m->m_pkthdr.csum_flags |=
   1424 						    M_CSUM_TCP_UDP_BAD;
   1425 				}
   1426 			}
   1427 			/* XXX Check TCPv6/UDPv6 checksum? */
   1428 		}
   1429 
   1430 		if (rxvlan & RE_RDESC_VLANCTL_TAG) {
   1431 			vlan_set_tag(m,
   1432 			     bswap16(rxvlan & RE_RDESC_VLANCTL_DATA));
   1433 		}
   1434 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1435 	}
   1436 
   1437 	sc->re_ldata.re_rx_prodidx = i;
   1438 }
   1439 
   1440 static void
   1441 re_txeof(struct rtk_softc *sc)
   1442 {
   1443 	struct ifnet *ifp;
   1444 	struct re_txq *txq;
   1445 	uint32_t txstat;
   1446 	int idx, descidx;
   1447 
   1448 	ifp = &sc->ethercom.ec_if;
   1449 
   1450 	for (idx = sc->re_ldata.re_txq_considx;
   1451 	    sc->re_ldata.re_txq_free < RE_TX_QLEN;
   1452 	    idx = RE_NEXT_TXQ(sc, idx), sc->re_ldata.re_txq_free++) {
   1453 		txq = &sc->re_ldata.re_txq[idx];
   1454 		KASSERT(txq->txq_mbuf != NULL);
   1455 
   1456 		descidx = txq->txq_descidx;
   1457 		RE_TXDESCSYNC(sc, descidx,
   1458 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1459 		txstat =
   1460 		    le32toh(sc->re_ldata.re_tx_list[descidx].re_cmdstat);
   1461 		RE_TXDESCSYNC(sc, descidx, BUS_DMASYNC_PREREAD);
   1462 		KASSERT((txstat & RE_TDESC_CMD_EOF) != 0);
   1463 		if (txstat & RE_TDESC_CMD_OWN) {
   1464 			break;
   1465 		}
   1466 
   1467 		sc->re_ldata.re_tx_free += txq->txq_nsegs;
   1468 		KASSERT(sc->re_ldata.re_tx_free <= RE_TX_DESC_CNT(sc));
   1469 		bus_dmamap_sync(sc->sc_dmat, txq->txq_dmamap,
   1470 		    0, txq->txq_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1471 		bus_dmamap_unload(sc->sc_dmat, txq->txq_dmamap);
   1472 		m_freem(txq->txq_mbuf);
   1473 		txq->txq_mbuf = NULL;
   1474 
   1475 		if (txstat & (RE_TDESC_STAT_EXCESSCOL | RE_TDESC_STAT_COLCNT))
   1476 			ifp->if_collisions++;
   1477 		if (txstat & RE_TDESC_STAT_TXERRSUM)
   1478 			ifp->if_oerrors++;
   1479 		else
   1480 			ifp->if_opackets++;
   1481 	}
   1482 
   1483 	sc->re_ldata.re_txq_considx = idx;
   1484 
   1485 	if (sc->re_ldata.re_txq_free > RE_NTXDESC_RSVD)
   1486 		ifp->if_flags &= ~IFF_OACTIVE;
   1487 
   1488 	/*
   1489 	 * If not all descriptors have been released reaped yet,
   1490 	 * reload the timer so that we will eventually get another
   1491 	 * interrupt that will cause us to re-enter this routine.
   1492 	 * This is done in case the transmitter has gone idle.
   1493 	 */
   1494 	if (sc->re_ldata.re_txq_free < RE_TX_QLEN) {
   1495 		if ((sc->sc_quirk & RTKQ_IM_HW) == 0)
   1496 			CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
   1497 		if ((sc->sc_quirk & RTKQ_PCIE) != 0) {
   1498 			/*
   1499 			 * Some chips will ignore a second TX request
   1500 			 * issued while an existing transmission is in
   1501 			 * progress. If the transmitter goes idle but
   1502 			 * there are still packets waiting to be sent,
   1503 			 * we need to restart the channel here to flush
   1504 			 * them out. This only seems to be required with
   1505 			 * the PCIe devices.
   1506 			 */
   1507 			CSR_WRITE_1(sc, RTK_GTXSTART, RTK_TXSTART_START);
   1508 		}
   1509 	} else
   1510 		ifp->if_timer = 0;
   1511 }
   1512 
   1513 static void
   1514 re_tick(void *arg)
   1515 {
   1516 	struct rtk_softc *sc = arg;
   1517 	int s;
   1518 
   1519 	/* XXX: just return for 8169S/8110S with rev 2 or newer phy */
   1520 	s = splnet();
   1521 
   1522 	mii_tick(&sc->mii);
   1523 	splx(s);
   1524 
   1525 	callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
   1526 }
   1527 
   1528 int
   1529 re_intr(void *arg)
   1530 {
   1531 	struct rtk_softc *sc = arg;
   1532 	struct ifnet *ifp;
   1533 	uint16_t status;
   1534 	int handled = 0;
   1535 
   1536 	if (!device_has_power(sc->sc_dev))
   1537 		return 0;
   1538 
   1539 	ifp = &sc->ethercom.ec_if;
   1540 
   1541 	if ((ifp->if_flags & IFF_UP) == 0)
   1542 		return 0;
   1543 
   1544 	const uint16_t status_mask = (sc->sc_quirk & RTKQ_IM_HW) ?
   1545 	    RTK_INTRS_IM_HW : RTK_INTRS_CPLUS;
   1546 
   1547 	for (;;) {
   1548 
   1549 		status = CSR_READ_2(sc, RTK_ISR);
   1550 		/* If the card has gone away the read returns 0xffff. */
   1551 		if (status == 0xffff)
   1552 			break;
   1553 		if (status) {
   1554 			handled = 1;
   1555 			CSR_WRITE_2(sc, RTK_ISR, status);
   1556 		}
   1557 
   1558 		if ((status & status_mask) == 0)
   1559 			break;
   1560 
   1561 		if (status & (RTK_ISR_RX_OK | RTK_ISR_RX_ERR))
   1562 			re_rxeof(sc);
   1563 
   1564 		if (status & (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_TX_ERR |
   1565 		    RTK_ISR_TX_DESC_UNAVAIL | RTK_ISR_TX_OK))
   1566 			re_txeof(sc);
   1567 
   1568 		if (status & RTK_ISR_SYSTEM_ERR) {
   1569 			re_init(ifp);
   1570 		}
   1571 
   1572 		if (status & RTK_ISR_LINKCHG) {
   1573 			callout_stop(&sc->rtk_tick_ch);
   1574 			re_tick(sc);
   1575 		}
   1576 	}
   1577 
   1578 	if (handled)
   1579 		if_schedule_deferred_start(ifp);
   1580 
   1581 	rnd_add_uint32(&sc->rnd_source, status);
   1582 
   1583 	return handled;
   1584 }
   1585 
   1586 
   1587 
   1588 /*
   1589  * Main transmit routine for C+ and gigE NICs.
   1590  */
   1591 
   1592 static void
   1593 re_start(struct ifnet *ifp)
   1594 {
   1595 	struct rtk_softc *sc;
   1596 	struct mbuf *m;
   1597 	bus_dmamap_t map;
   1598 	struct re_txq *txq;
   1599 	struct re_desc *d;
   1600 	uint32_t cmdstat, re_flags, vlanctl;
   1601 	int ofree, idx, error, nsegs, seg;
   1602 	int startdesc, curdesc, lastdesc;
   1603 	bool pad;
   1604 
   1605 	sc = ifp->if_softc;
   1606 	ofree = sc->re_ldata.re_txq_free;
   1607 
   1608 	for (idx = sc->re_ldata.re_txq_prodidx;; idx = RE_NEXT_TXQ(sc, idx)) {
   1609 
   1610 		IFQ_POLL(&ifp->if_snd, m);
   1611 		if (m == NULL)
   1612 			break;
   1613 
   1614 		if (sc->re_ldata.re_txq_free == 0 ||
   1615 		    sc->re_ldata.re_tx_free == 0) {
   1616 			/* no more free slots left */
   1617 			ifp->if_flags |= IFF_OACTIVE;
   1618 			break;
   1619 		}
   1620 
   1621 		/*
   1622 		 * Set up checksum offload. Note: checksum offload bits must
   1623 		 * appear in all descriptors of a multi-descriptor transmit
   1624 		 * attempt. (This is according to testing done with an 8169
   1625 		 * chip. I'm not sure if this is a requirement or a bug.)
   1626 		 */
   1627 
   1628 		vlanctl = 0;
   1629 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
   1630 			uint32_t segsz = m->m_pkthdr.segsz;
   1631 
   1632 			if ((sc->sc_quirk & RTKQ_DESCV2) == 0) {
   1633 				re_flags = RE_TDESC_CMD_LGSEND |
   1634 				    (segsz << RE_TDESC_CMD_MSSVAL_SHIFT);
   1635 			} else {
   1636 				re_flags = RE_TDESC_CMD_LGSEND_V4;
   1637 				vlanctl |=
   1638 				    (segsz << RE_TDESC_VLANCTL_MSSVAL_SHIFT);
   1639 			}
   1640 		} else {
   1641 			/*
   1642 			 * set RE_TDESC_CMD_IPCSUM if any checksum offloading
   1643 			 * is requested.  otherwise, RE_TDESC_CMD_TCPCSUM/
   1644 			 * RE_TDESC_CMD_UDPCSUM doesn't make effects.
   1645 			 */
   1646 			re_flags = 0;
   1647 			if ((m->m_pkthdr.csum_flags &
   1648 			    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4))
   1649 			    != 0) {
   1650 				if ((sc->sc_quirk & RTKQ_DESCV2) == 0) {
   1651 					re_flags |= RE_TDESC_CMD_IPCSUM;
   1652 					if (m->m_pkthdr.csum_flags &
   1653 					    M_CSUM_TCPv4) {
   1654 						re_flags |=
   1655 						    RE_TDESC_CMD_TCPCSUM;
   1656 					} else if (m->m_pkthdr.csum_flags &
   1657 					    M_CSUM_UDPv4) {
   1658 						re_flags |=
   1659 						    RE_TDESC_CMD_UDPCSUM;
   1660 					}
   1661 				} else {
   1662 					vlanctl |= RE_TDESC_VLANCTL_IPCSUM;
   1663 					if (m->m_pkthdr.csum_flags &
   1664 					    M_CSUM_TCPv4) {
   1665 						vlanctl |=
   1666 						    RE_TDESC_VLANCTL_TCPCSUM;
   1667 					} else if (m->m_pkthdr.csum_flags &
   1668 					    M_CSUM_UDPv4) {
   1669 						vlanctl |=
   1670 						    RE_TDESC_VLANCTL_UDPCSUM;
   1671 					}
   1672 				}
   1673 			}
   1674 		}
   1675 
   1676 		txq = &sc->re_ldata.re_txq[idx];
   1677 		map = txq->txq_dmamap;
   1678 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1679 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1680 
   1681 		if (__predict_false(error)) {
   1682 			/* XXX try to defrag if EFBIG? */
   1683 			printf("%s: can't map mbuf (error %d)\n",
   1684 			    device_xname(sc->sc_dev), error);
   1685 
   1686 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1687 			m_freem(m);
   1688 			ifp->if_oerrors++;
   1689 			continue;
   1690 		}
   1691 
   1692 		nsegs = map->dm_nsegs;
   1693 		pad = false;
   1694 		if (__predict_false(m->m_pkthdr.len <= RE_IP4CSUMTX_PADLEN &&
   1695 		    (re_flags & RE_TDESC_CMD_IPCSUM) != 0 &&
   1696 		    (sc->sc_quirk & RTKQ_DESCV2) == 0)) {
   1697 			pad = true;
   1698 			nsegs++;
   1699 		}
   1700 
   1701 		if (nsegs > sc->re_ldata.re_tx_free) {
   1702 			/*
   1703 			 * Not enough free descriptors to transmit this packet.
   1704 			 */
   1705 			ifp->if_flags |= IFF_OACTIVE;
   1706 			bus_dmamap_unload(sc->sc_dmat, map);
   1707 			break;
   1708 		}
   1709 
   1710 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1711 
   1712 		/*
   1713 		 * Make sure that the caches are synchronized before we
   1714 		 * ask the chip to start DMA for the packet data.
   1715 		 */
   1716 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1717 		    BUS_DMASYNC_PREWRITE);
   1718 
   1719 		/*
   1720 		 * Set up hardware VLAN tagging. Note: vlan tag info must
   1721 		 * appear in all descriptors of a multi-descriptor
   1722 		 * transmission attempt.
   1723 		 */
   1724 		if (vlan_has_tag(m))
   1725 			vlanctl |= bswap16(vlan_get_tag(m)) |
   1726 			    RE_TDESC_VLANCTL_TAG;
   1727 
   1728 		/*
   1729 		 * Map the segment array into descriptors.
   1730 		 * Note that we set the start-of-frame and
   1731 		 * end-of-frame markers for either TX or RX,
   1732 		 * but they really only have meaning in the TX case.
   1733 		 * (In the RX case, it's the chip that tells us
   1734 		 *  where packets begin and end.)
   1735 		 * We also keep track of the end of the ring
   1736 		 * and set the end-of-ring bits as needed,
   1737 		 * and we set the ownership bits in all except
   1738 		 * the very first descriptor. (The caller will
   1739 		 * set this descriptor later when it start
   1740 		 * transmission or reception.)
   1741 		 */
   1742 		curdesc = startdesc = sc->re_ldata.re_tx_nextfree;
   1743 		lastdesc = -1;
   1744 		for (seg = 0; seg < map->dm_nsegs;
   1745 		    seg++, curdesc = RE_NEXT_TX_DESC(sc, curdesc)) {
   1746 			d = &sc->re_ldata.re_tx_list[curdesc];
   1747 #ifdef DIAGNOSTIC
   1748 			RE_TXDESCSYNC(sc, curdesc,
   1749 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1750 			cmdstat = le32toh(d->re_cmdstat);
   1751 			RE_TXDESCSYNC(sc, curdesc, BUS_DMASYNC_PREREAD);
   1752 			if (cmdstat & RE_TDESC_STAT_OWN) {
   1753 				panic("%s: tried to map busy TX descriptor",
   1754 				    device_xname(sc->sc_dev));
   1755 			}
   1756 #endif
   1757 
   1758 			d->re_vlanctl = htole32(vlanctl);
   1759 			re_set_bufaddr(d, map->dm_segs[seg].ds_addr);
   1760 			cmdstat = re_flags | map->dm_segs[seg].ds_len;
   1761 			if (seg == 0)
   1762 				cmdstat |= RE_TDESC_CMD_SOF;
   1763 			else
   1764 				cmdstat |= RE_TDESC_CMD_OWN;
   1765 			if (curdesc == (RE_TX_DESC_CNT(sc) - 1))
   1766 				cmdstat |= RE_TDESC_CMD_EOR;
   1767 			if (seg == nsegs - 1) {
   1768 				cmdstat |= RE_TDESC_CMD_EOF;
   1769 				lastdesc = curdesc;
   1770 			}
   1771 			d->re_cmdstat = htole32(cmdstat);
   1772 			RE_TXDESCSYNC(sc, curdesc,
   1773 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1774 		}
   1775 		if (__predict_false(pad)) {
   1776 			d = &sc->re_ldata.re_tx_list[curdesc];
   1777 			d->re_vlanctl = htole32(vlanctl);
   1778 			re_set_bufaddr(d, RE_TXPADDADDR(sc));
   1779 			cmdstat = re_flags |
   1780 			    RE_TDESC_CMD_OWN | RE_TDESC_CMD_EOF |
   1781 			    (RE_IP4CSUMTX_PADLEN + 1 - m->m_pkthdr.len);
   1782 			if (curdesc == (RE_TX_DESC_CNT(sc) - 1))
   1783 				cmdstat |= RE_TDESC_CMD_EOR;
   1784 			d->re_cmdstat = htole32(cmdstat);
   1785 			RE_TXDESCSYNC(sc, curdesc,
   1786 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1787 			lastdesc = curdesc;
   1788 			curdesc = RE_NEXT_TX_DESC(sc, curdesc);
   1789 		}
   1790 		KASSERT(lastdesc != -1);
   1791 
   1792 		/* Transfer ownership of packet to the chip. */
   1793 
   1794 		sc->re_ldata.re_tx_list[startdesc].re_cmdstat |=
   1795 		    htole32(RE_TDESC_CMD_OWN);
   1796 		RE_TXDESCSYNC(sc, startdesc,
   1797 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1798 
   1799 		/* update info of TX queue and descriptors */
   1800 		txq->txq_mbuf = m;
   1801 		txq->txq_descidx = lastdesc;
   1802 		txq->txq_nsegs = nsegs;
   1803 
   1804 		sc->re_ldata.re_txq_free--;
   1805 		sc->re_ldata.re_tx_free -= nsegs;
   1806 		sc->re_ldata.re_tx_nextfree = curdesc;
   1807 
   1808 		/*
   1809 		 * If there's a BPF listener, bounce a copy of this frame
   1810 		 * to him.
   1811 		 */
   1812 		bpf_mtap(ifp, m, BPF_D_OUT);
   1813 	}
   1814 
   1815 	if (sc->re_ldata.re_txq_free < ofree) {
   1816 		/*
   1817 		 * TX packets are enqueued.
   1818 		 */
   1819 		sc->re_ldata.re_txq_prodidx = idx;
   1820 
   1821 		/*
   1822 		 * Start the transmitter to poll.
   1823 		 *
   1824 		 * RealTek put the TX poll request register in a different
   1825 		 * location on the 8169 gigE chip. I don't know why.
   1826 		 */
   1827 		if ((sc->sc_quirk & RTKQ_8139CPLUS) != 0)
   1828 			CSR_WRITE_1(sc, RTK_TXSTART, RTK_TXSTART_START);
   1829 		else
   1830 			CSR_WRITE_1(sc, RTK_GTXSTART, RTK_TXSTART_START);
   1831 
   1832 		if ((sc->sc_quirk & RTKQ_IM_HW) == 0) {
   1833 			/*
   1834 			 * Use the countdown timer for interrupt moderation.
   1835 			 * 'TX done' interrupts are disabled. Instead, we reset
   1836 			 * the countdown timer, which will begin counting until
   1837 			 * it hits the value in the TIMERINT register, and then
   1838 			 * trigger an interrupt. Each time we write to the
   1839 			 * TIMERCNT register, the timer count is reset to 0.
   1840 			 */
   1841 			CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
   1842 		}
   1843 
   1844 		/*
   1845 		 * Set a timeout in case the chip goes out to lunch.
   1846 		 */
   1847 		ifp->if_timer = 5;
   1848 	}
   1849 }
   1850 
   1851 static int
   1852 re_init(struct ifnet *ifp)
   1853 {
   1854 	struct rtk_softc *sc = ifp->if_softc;
   1855 	uint32_t rxcfg = 0;
   1856 	uint16_t cfg;
   1857 	int error;
   1858 #ifdef RE_USE_EECMD
   1859 	const uint8_t *enaddr;
   1860 	uint32_t reg;
   1861 #endif
   1862 
   1863 	if ((error = re_enable(sc)) != 0)
   1864 		goto out;
   1865 
   1866 	/*
   1867 	 * Cancel pending I/O and free all RX/TX buffers.
   1868 	 */
   1869 	re_stop(ifp, 0);
   1870 
   1871 	re_reset(sc);
   1872 
   1873 	/*
   1874 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
   1875 	 * RX checksum offload. We must configure the C+ register
   1876 	 * before all others.
   1877 	 */
   1878 	cfg = RE_CPLUSCMD_PCI_MRW;
   1879 
   1880 	/*
   1881 	 * XXX: For old 8169 set bit 14.
   1882 	 *      For 8169S/8110S and above, do not set bit 14.
   1883 	 */
   1884 	if ((sc->sc_quirk & RTKQ_8169NONS) != 0)
   1885 		cfg |= (0x1 << 14);
   1886 
   1887 	if ((sc->ethercom.ec_capenable & ETHERCAP_VLAN_HWTAGGING) != 0)
   1888 		cfg |= RE_CPLUSCMD_VLANSTRIP;
   1889 	if ((ifp->if_capenable & (IFCAP_CSUM_IPv4_Rx |
   1890 	     IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx)) != 0)
   1891 		cfg |= RE_CPLUSCMD_RXCSUM_ENB;
   1892 	if ((sc->sc_quirk & RTKQ_MACSTAT) != 0) {
   1893 		cfg |= RE_CPLUSCMD_MACSTAT_DIS;
   1894 		cfg |= RE_CPLUSCMD_TXENB;
   1895 	} else
   1896 		cfg |= RE_CPLUSCMD_RXENB | RE_CPLUSCMD_TXENB;
   1897 
   1898 	CSR_WRITE_2(sc, RTK_CPLUS_CMD, cfg);
   1899 
   1900 	/* XXX: from Realtek-supplied Linux driver. Wholly undocumented. */
   1901 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
   1902 		if ((sc->sc_quirk & RTKQ_IM_HW) == 0) {
   1903 			CSR_WRITE_2(sc, RTK_IM, 0x0000);
   1904 		} else {
   1905 			CSR_WRITE_2(sc, RTK_IM, 0x5151);
   1906 		}
   1907 	}
   1908 
   1909 	DELAY(10000);
   1910 
   1911 #ifdef RE_USE_EECMD
   1912 	/*
   1913 	 * Init our MAC address.  Even though the chipset
   1914 	 * documentation doesn't mention it, we need to enter "Config
   1915 	 * register write enable" mode to modify the ID registers.
   1916 	 */
   1917 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_WRITECFG);
   1918 	enaddr = CLLADDR(ifp->if_sadl);
   1919 	reg = enaddr[0] | (enaddr[1] << 8) |
   1920 	    (enaddr[2] << 16) | (enaddr[3] << 24);
   1921 	CSR_WRITE_4(sc, RTK_IDR0, reg);
   1922 	reg = enaddr[4] | (enaddr[5] << 8);
   1923 	CSR_WRITE_4(sc, RTK_IDR4, reg);
   1924 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
   1925 #endif
   1926 
   1927 	/*
   1928 	 * For C+ mode, initialize the RX descriptors and mbufs.
   1929 	 */
   1930 	re_rx_list_init(sc);
   1931 	re_tx_list_init(sc);
   1932 
   1933 	/*
   1934 	 * Load the addresses of the RX and TX lists into the chip.
   1935 	 */
   1936 	CSR_WRITE_4(sc, RTK_RXLIST_ADDR_HI,
   1937 	    RE_ADDR_HI(sc->re_ldata.re_rx_list_map->dm_segs[0].ds_addr));
   1938 	CSR_WRITE_4(sc, RTK_RXLIST_ADDR_LO,
   1939 	    RE_ADDR_LO(sc->re_ldata.re_rx_list_map->dm_segs[0].ds_addr));
   1940 
   1941 	CSR_WRITE_4(sc, RTK_TXLIST_ADDR_HI,
   1942 	    RE_ADDR_HI(sc->re_ldata.re_tx_list_map->dm_segs[0].ds_addr));
   1943 	CSR_WRITE_4(sc, RTK_TXLIST_ADDR_LO,
   1944 	    RE_ADDR_LO(sc->re_ldata.re_tx_list_map->dm_segs[0].ds_addr));
   1945 
   1946 	if (sc->sc_quirk & RTKQ_RXDV_GATED) {
   1947 		CSR_WRITE_4(sc, RTK_MISC,
   1948 		    CSR_READ_4(sc, RTK_MISC) & ~RTK_MISC_RXDV_GATED_EN);
   1949 	}
   1950 
   1951 	/*
   1952 	 * Enable transmit and receive.
   1953 	 */
   1954 	if ((sc->sc_quirk & RTKQ_TXRXEN_LATER) == 0)
   1955 		CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   1956 
   1957 	/*
   1958 	 * Set the initial TX and RX configuration.
   1959 	 */
   1960 	if (sc->re_testmode && (sc->sc_quirk & RTKQ_8169NONS) != 0) {
   1961 		/* test mode is needed only for old 8169 */
   1962 		CSR_WRITE_4(sc, RTK_TXCFG,
   1963 		    RE_TXCFG_CONFIG | RTK_LOOPTEST_ON);
   1964 	} else
   1965 		CSR_WRITE_4(sc, RTK_TXCFG, RE_TXCFG_CONFIG);
   1966 
   1967 	CSR_WRITE_1(sc, RTK_EARLY_TX_THRESH, 16);
   1968 
   1969 	CSR_WRITE_4(sc, RTK_RXCFG, RE_RXCFG_CONFIG);
   1970 
   1971 	/* Set the individual bit to receive frames for this host only. */
   1972 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1973 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1974 
   1975 	/* If we want promiscuous mode, set the allframes bit. */
   1976 	if (ifp->if_flags & IFF_PROMISC)
   1977 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1978 	else
   1979 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1980 	CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1981 
   1982 	/*
   1983 	 * Set capture broadcast bit to capture broadcast frames.
   1984 	 */
   1985 	if (ifp->if_flags & IFF_BROADCAST)
   1986 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1987 	else
   1988 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1989 	CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1990 
   1991 	/*
   1992 	 * Program the multicast filter, if necessary.
   1993 	 */
   1994 	rtk_setmulti(sc);
   1995 
   1996 	/*
   1997 	 * some chips require to enable TX/RX *AFTER* TX/RX configuration
   1998 	 */
   1999 	if ((sc->sc_quirk & RTKQ_TXRXEN_LATER) != 0)
   2000 		CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   2001 
   2002 	/*
   2003 	 * Enable interrupts.
   2004 	 */
   2005 	if (sc->re_testmode)
   2006 		CSR_WRITE_2(sc, RTK_IMR, 0);
   2007 	else if ((sc->sc_quirk & RTKQ_IM_HW) != 0)
   2008 		CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_IM_HW);
   2009 	else
   2010 		CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_CPLUS);
   2011 
   2012 	/* Start RX/TX process. */
   2013 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   2014 #ifdef notdef
   2015 	/* Enable receiver and transmitter. */
   2016 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   2017 #endif
   2018 
   2019 	/*
   2020 	 * Initialize the timer interrupt register so that
   2021 	 * a timer interrupt will be generated once the timer
   2022 	 * reaches a certain number of ticks. The timer is
   2023 	 * reloaded on each transmit. This gives us TX interrupt
   2024 	 * moderation, which dramatically improves TX frame rate.
   2025 	 */
   2026 
   2027 	unsigned defer;		/* timer interval / ns */
   2028 	unsigned period;	/* busclock period / ns */
   2029 
   2030 	/*
   2031 	 * Maximum frame rate
   2032 	 * 1500 byte PDU -> 81274 Hz
   2033 	 *   46 byte PDU -> 1488096 Hz
   2034 	 *
   2035 	 * Deferring interrupts by up to 128us needs descriptors for
   2036 	 * 1500 byte PDU -> 10.4 frames
   2037 	 *   46 byte PDU -> 190.4 frames
   2038 	 *
   2039 	 */
   2040 	defer = 128000;
   2041 
   2042 	if ((sc->sc_quirk & RTKQ_IM_HW) != 0) {
   2043 		period = 1;
   2044 		defer = 0;
   2045 	} else if ((sc->sc_quirk & RTKQ_PCIE) != 0) {
   2046 		period = 8;
   2047 	} else {
   2048 		switch (CSR_READ_1(sc, RTK_CFG2_BUSFREQ) & 0x7) {
   2049 		case RTK_BUSFREQ_33MHZ:
   2050 			period = 30;
   2051 			break;
   2052 		case RTK_BUSFREQ_66MHZ:
   2053 			period = 15;
   2054 			break;
   2055 		default:
   2056 			/* lowest possible clock */
   2057 			period = 60;
   2058 			break;
   2059 		}
   2060 	}
   2061 
   2062 	/* Timer Interrupt register address varies */
   2063 	uint16_t re8139_reg;
   2064 	if ((sc->sc_quirk & RTKQ_8139CPLUS) != 0)
   2065 		re8139_reg = RTK_TIMERINT;
   2066 	else
   2067 		re8139_reg = RTK_TIMERINT_8169;
   2068 	CSR_WRITE_4(sc, re8139_reg, defer / period);
   2069 
   2070 	if ((sc->sc_quirk & RTKQ_8139CPLUS) == 0) {
   2071 		/*
   2072 		 * For 8169 gigE NICs, set the max allowed RX packet
   2073 		 * size so we can receive jumbo frames.
   2074 		 */
   2075 		CSR_WRITE_2(sc, RTK_MAXRXPKTLEN, 16383);
   2076 	}
   2077 
   2078 	if (sc->re_testmode)
   2079 		return 0;
   2080 
   2081 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD);
   2082 
   2083 	ifp->if_flags |= IFF_RUNNING;
   2084 	ifp->if_flags &= ~IFF_OACTIVE;
   2085 
   2086 	callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
   2087 
   2088  out:
   2089 	if (error) {
   2090 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2091 		ifp->if_timer = 0;
   2092 		printf("%s: interface not running\n",
   2093 		    device_xname(sc->sc_dev));
   2094 	}
   2095 
   2096 	return error;
   2097 }
   2098 
   2099 static int
   2100 re_ioctl(struct ifnet *ifp, u_long command, void *data)
   2101 {
   2102 	struct rtk_softc *sc = ifp->if_softc;
   2103 	struct ifreq *ifr = data;
   2104 	int s, error = 0;
   2105 
   2106 	s = splnet();
   2107 
   2108 	switch (command) {
   2109 	case SIOCSIFMTU:
   2110 		/*
   2111 		 * Disable jumbo frames if it's not supported.
   2112 		 */
   2113 		if ((sc->sc_quirk & RTKQ_NOJUMBO) != 0 &&
   2114 		    ifr->ifr_mtu > ETHERMTU) {
   2115 			error = EINVAL;
   2116 			break;
   2117 		}
   2118 
   2119 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU_JUMBO)
   2120 			error = EINVAL;
   2121 		else if ((error = ifioctl_common(ifp, command, data)) ==
   2122 		    ENETRESET)
   2123 			error = 0;
   2124 		break;
   2125 	default:
   2126 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   2127 			break;
   2128 
   2129 		error = 0;
   2130 
   2131 		if (command == SIOCSIFCAP)
   2132 			error = (*ifp->if_init)(ifp);
   2133 		else if (command != SIOCADDMULTI && command != SIOCDELMULTI)
   2134 			;
   2135 		else if (ifp->if_flags & IFF_RUNNING)
   2136 			rtk_setmulti(sc);
   2137 		break;
   2138 	}
   2139 
   2140 	splx(s);
   2141 
   2142 	return error;
   2143 }
   2144 
   2145 static void
   2146 re_watchdog(struct ifnet *ifp)
   2147 {
   2148 	struct rtk_softc *sc;
   2149 	int s;
   2150 
   2151 	sc = ifp->if_softc;
   2152 	s = splnet();
   2153 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   2154 	ifp->if_oerrors++;
   2155 
   2156 	re_txeof(sc);
   2157 	re_rxeof(sc);
   2158 
   2159 	re_init(ifp);
   2160 
   2161 	splx(s);
   2162 }
   2163 
   2164 /*
   2165  * Stop the adapter and free any mbufs allocated to the
   2166  * RX and TX lists.
   2167  */
   2168 static void
   2169 re_stop(struct ifnet *ifp, int disable)
   2170 {
   2171 	int i;
   2172 	struct rtk_softc *sc = ifp->if_softc;
   2173 
   2174 	callout_stop(&sc->rtk_tick_ch);
   2175 
   2176 	mii_down(&sc->mii);
   2177 
   2178 	if ((sc->sc_quirk & RTKQ_CMDSTOP) != 0)
   2179 		CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_STOPREQ | RTK_CMD_TX_ENB |
   2180 		    RTK_CMD_RX_ENB);
   2181 	else
   2182 		CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   2183 	DELAY(1000);
   2184 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   2185 	CSR_WRITE_2(sc, RTK_ISR, 0xFFFF);
   2186 
   2187 	if (sc->re_head != NULL) {
   2188 		m_freem(sc->re_head);
   2189 		sc->re_head = sc->re_tail = NULL;
   2190 	}
   2191 
   2192 	/* Free the TX list buffers. */
   2193 	for (i = 0; i < RE_TX_QLEN; i++) {
   2194 		if (sc->re_ldata.re_txq[i].txq_mbuf != NULL) {
   2195 			bus_dmamap_unload(sc->sc_dmat,
   2196 			    sc->re_ldata.re_txq[i].txq_dmamap);
   2197 			m_freem(sc->re_ldata.re_txq[i].txq_mbuf);
   2198 			sc->re_ldata.re_txq[i].txq_mbuf = NULL;
   2199 		}
   2200 	}
   2201 
   2202 	/* Free the RX list buffers. */
   2203 	for (i = 0; i < RE_RX_DESC_CNT; i++) {
   2204 		if (sc->re_ldata.re_rxsoft[i].rxs_mbuf != NULL) {
   2205 			bus_dmamap_unload(sc->sc_dmat,
   2206 			    sc->re_ldata.re_rxsoft[i].rxs_dmamap);
   2207 			m_freem(sc->re_ldata.re_rxsoft[i].rxs_mbuf);
   2208 			sc->re_ldata.re_rxsoft[i].rxs_mbuf = NULL;
   2209 		}
   2210 	}
   2211 
   2212 	if (disable)
   2213 		re_disable(sc);
   2214 
   2215 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2216 	ifp->if_timer = 0;
   2217 }
   2218