Home | History | Annotate | Line # | Download | only in ic
rtl8169.c revision 1.4
      1 /*	$NetBSD: rtl8169.c,v 1.4 2005/01/09 12:15:36 kanaoka 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 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
     37 
     38 /*
     39  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
     40  *
     41  * Written by Bill Paul <wpaul (at) windriver.com>
     42  * Senior Networking Software Engineer
     43  * Wind River Systems
     44  */
     45 
     46 /*
     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  * four devices in this family: the RTL8139C+, the RTL8169, the RTL8169S
     50  * and the RTL8110S.
     51  *
     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:
     56  *
     57  *	o Descriptor based DMA mechanism. Each descriptor represents
     58  *	  a single packet fragment. Data buffers may be aligned on
     59  *	  any byte boundary.
     60  *
     61  *	o 64-bit DMA
     62  *
     63  *	o TCP/IP checksum offload for both RX and TX
     64  *
     65  *	o High and normal priority transmit DMA rings
     66  *
     67  *	o VLAN tag insertion and extraction
     68  *
     69  *	o TCP large send (segmentation offload)
     70  *
     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
     74  * chips.
     75  *
     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:
     79  *
     80  *	o 1000Mbps mode
     81  *
     82  *	o Jumbo frames
     83  *
     84  * 	o GMII and TBI ports/registers for interfacing with copper
     85  *	  or fiber PHYs
     86  *
     87  *      o RX and TX DMA rings can have up to 1024 descriptors
     88  *        (the 8139C+ allows a maximum of 64)
     89  *
     90  *	o Slight differences in register layout from the 8139C+
     91  *
     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'
     96  * copper gigE PHY.
     97  *
     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.
    103  *
    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 7.5K, so the max MTU possible with this
    110  * driver is 7500 bytes.
    111  */
    112 
    113 #include "bpfilter.h"
    114 #include "vlan.h"
    115 
    116 #include <sys/param.h>
    117 #include <sys/endian.h>
    118 #include <sys/systm.h>
    119 #include <sys/sockio.h>
    120 #include <sys/mbuf.h>
    121 #include <sys/malloc.h>
    122 #include <sys/kernel.h>
    123 #include <sys/socket.h>
    124 #include <sys/device.h>
    125 
    126 #include <net/if.h>
    127 #include <net/if_arp.h>
    128 #include <net/if_dl.h>
    129 #include <net/if_ether.h>
    130 #include <net/if_media.h>
    131 #include <net/if_vlanvar.h>
    132 
    133 #if NBPFILTER > 0
    134 #include <net/bpf.h>
    135 #endif
    136 
    137 #include <machine/bus.h>
    138 
    139 #include <dev/mii/mii.h>
    140 #include <dev/mii/miivar.h>
    141 
    142 #include <dev/pci/pcireg.h>
    143 #include <dev/pci/pcivar.h>
    144 #include <dev/pci/pcidevs.h>
    145 
    146 /*
    147  * Default to using PIO access for this driver.
    148  */
    149 #define RE_USEIOSPACE
    150 
    151 #include <dev/ic/rtl81x9reg.h>
    152 #include <dev/ic/rtl81x9var.h>
    153 
    154 #include <dev/ic/rtl8169var.h>
    155 
    156 
    157 static int re_encap(struct rtk_softc *, struct mbuf *, int *);
    158 
    159 static int re_allocmem(struct rtk_softc *);
    160 static int re_newbuf(struct rtk_softc *, int, struct mbuf *);
    161 static int re_rx_list_init(struct rtk_softc *);
    162 static int re_tx_list_init(struct rtk_softc *);
    163 static void re_rxeof(struct rtk_softc *);
    164 static void re_txeof(struct rtk_softc *);
    165 static void re_tick(void *);
    166 static void re_start(struct ifnet *);
    167 static int re_ioctl(struct ifnet *, u_long, caddr_t);
    168 static int re_init(struct ifnet *);
    169 static void re_stop(struct ifnet *, int);
    170 static void re_watchdog(struct ifnet *);
    171 
    172 static void re_shutdown(void *);
    173 static int re_enable(struct rtk_softc *);
    174 static void re_disable(struct rtk_softc *);
    175 static void re_power(int, void *);
    176 
    177 static int re_ifmedia_upd(struct ifnet *);
    178 static void re_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    179 
    180 static int re_gmii_readreg(struct device *, int, int);
    181 static void re_gmii_writereg(struct device *, int, int, int);
    182 
    183 static int re_miibus_readreg(struct device *, int, int);
    184 static void re_miibus_writereg(struct device *, int, int, int);
    185 static void re_miibus_statchg(struct device *);
    186 
    187 static void re_reset(struct rtk_softc *);
    188 
    189 static int re_diag(struct rtk_softc *);
    190 
    191 #ifdef RE_USEIOSPACE
    192 #define RTK_RES			SYS_RES_IOPORT
    193 #define RTK_RID			RTK_PCI_LOIO
    194 #else
    195 #define RTK_RES			SYS_RES_MEMORY
    196 #define RTK_RID			RTK_PCI_LOMEM
    197 #endif
    198 
    199 #define EE_SET(x)					\
    200 	CSR_WRITE_1(sc, RTK_EECMD,			\
    201 		CSR_READ_1(sc, RTK_EECMD) | x)
    202 
    203 #define EE_CLR(x)					\
    204 	CSR_WRITE_1(sc, RTK_EECMD,			\
    205 		CSR_READ_1(sc, RTK_EECMD) & ~x)
    206 
    207 static int
    208 re_gmii_readreg(struct device *self, int phy, int reg)
    209 {
    210 	struct rtk_softc	*sc = (void *)self;
    211 	u_int32_t		rval;
    212 	int			i;
    213 
    214 	if (phy != 7)
    215 		return 0;
    216 
    217 	/* Let the rgephy driver read the GMEDIASTAT register */
    218 
    219 	if (reg == RTK_GMEDIASTAT) {
    220 		rval = CSR_READ_1(sc, RTK_GMEDIASTAT);
    221 		return rval;
    222 	}
    223 
    224 	CSR_WRITE_4(sc, RTK_PHYAR, reg << 16);
    225 	DELAY(1000);
    226 
    227 	for (i = 0; i < RTK_TIMEOUT; i++) {
    228 		rval = CSR_READ_4(sc, RTK_PHYAR);
    229 		if (rval & RTK_PHYAR_BUSY)
    230 			break;
    231 		DELAY(100);
    232 	}
    233 
    234 	if (i == RTK_TIMEOUT) {
    235 		aprint_error("%s: PHY read failed\n", sc->sc_dev.dv_xname);
    236 		return 0;
    237 	}
    238 
    239 	return rval & RTK_PHYAR_PHYDATA;
    240 }
    241 
    242 static void
    243 re_gmii_writereg(struct device *dev, int phy, int reg, int data)
    244 {
    245 	struct rtk_softc	*sc = (void *)dev;
    246 	u_int32_t		rval;
    247 	int			i;
    248 
    249 	CSR_WRITE_4(sc, RTK_PHYAR, (reg << 16) |
    250 	    (data & RTK_PHYAR_PHYDATA) | RTK_PHYAR_BUSY);
    251 	DELAY(1000);
    252 
    253 	for (i = 0; i < RTK_TIMEOUT; i++) {
    254 		rval = CSR_READ_4(sc, RTK_PHYAR);
    255 		if (!(rval & RTK_PHYAR_BUSY))
    256 			break;
    257 		DELAY(100);
    258 	}
    259 
    260 	if (i == RTK_TIMEOUT) {
    261 		aprint_error("%s: PHY write reg %x <- %x failed\n",
    262 		    sc->sc_dev.dv_xname, reg, data);
    263 		return;
    264 	}
    265 
    266 	return;
    267 }
    268 
    269 static int
    270 re_miibus_readreg(struct device *dev, int phy, int reg)
    271 {
    272 	struct rtk_softc	*sc = (void *)dev;
    273 	u_int16_t		rval = 0;
    274 	u_int16_t		re8139_reg = 0;
    275 	int			s;
    276 
    277 	s = splnet();
    278 
    279 	if (sc->rtk_type == RTK_8169) {
    280 		rval = re_gmii_readreg(dev, phy, reg);
    281 		splx(s);
    282 		return rval;
    283 	}
    284 
    285 	/* Pretend the internal PHY is only at address 0 */
    286 	if (phy) {
    287 		splx(s);
    288 		return 0;
    289 	}
    290 	switch (reg) {
    291 	case MII_BMCR:
    292 		re8139_reg = RTK_BMCR;
    293 		break;
    294 	case MII_BMSR:
    295 		re8139_reg = RTK_BMSR;
    296 		break;
    297 	case MII_ANAR:
    298 		re8139_reg = RTK_ANAR;
    299 		break;
    300 	case MII_ANER:
    301 		re8139_reg = RTK_ANER;
    302 		break;
    303 	case MII_ANLPAR:
    304 		re8139_reg = RTK_LPAR;
    305 		break;
    306 	case MII_PHYIDR1:
    307 	case MII_PHYIDR2:
    308 		splx(s);
    309 		return 0;
    310 	/*
    311 	 * Allow the rlphy driver to read the media status
    312 	 * register. If we have a link partner which does not
    313 	 * support NWAY, this is the register which will tell
    314 	 * us the results of parallel detection.
    315 	 */
    316 	case RTK_MEDIASTAT:
    317 		rval = CSR_READ_1(sc, RTK_MEDIASTAT);
    318 		splx(s);
    319 		return rval;
    320 	default:
    321 		aprint_error("%s: bad phy register\n", sc->sc_dev.dv_xname);
    322 		splx(s);
    323 		return 0;
    324 	}
    325 	rval = CSR_READ_2(sc, re8139_reg);
    326 	splx(s);
    327 	return rval;
    328 }
    329 
    330 static void
    331 re_miibus_writereg(struct device *dev, int phy, int reg, int data)
    332 {
    333 	struct rtk_softc	*sc = (void *)dev;
    334 	u_int16_t		re8139_reg = 0;
    335 	int			s;
    336 
    337 	s = splnet();
    338 
    339 	if (sc->rtk_type == RTK_8169) {
    340 		re_gmii_writereg(dev, phy, reg, data);
    341 		splx(s);
    342 		return;
    343 	}
    344 
    345 	/* Pretend the internal PHY is only at address 0 */
    346 	if (phy) {
    347 		splx(s);
    348 		return;
    349 	}
    350 	switch (reg) {
    351 	case MII_BMCR:
    352 		re8139_reg = RTK_BMCR;
    353 		break;
    354 	case MII_BMSR:
    355 		re8139_reg = RTK_BMSR;
    356 		break;
    357 	case MII_ANAR:
    358 		re8139_reg = RTK_ANAR;
    359 		break;
    360 	case MII_ANER:
    361 		re8139_reg = RTK_ANER;
    362 		break;
    363 	case MII_ANLPAR:
    364 		re8139_reg = RTK_LPAR;
    365 		break;
    366 	case MII_PHYIDR1:
    367 	case MII_PHYIDR2:
    368 		splx(s);
    369 		return;
    370 		break;
    371 	default:
    372 		aprint_error("%s: bad phy register\n", sc->sc_dev.dv_xname);
    373 		splx(s);
    374 		return;
    375 	}
    376 	CSR_WRITE_2(sc, re8139_reg, data);
    377 	splx(s);
    378 	return;
    379 }
    380 
    381 static void
    382 re_miibus_statchg(struct device *dev)
    383 {
    384 
    385 	return;
    386 }
    387 
    388 static void
    389 re_reset(struct rtk_softc *sc)
    390 {
    391 	register int		i;
    392 
    393 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
    394 
    395 	for (i = 0; i < RTK_TIMEOUT; i++) {
    396 		DELAY(10);
    397 		if (!(CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET))
    398 			break;
    399 	}
    400 	if (i == RTK_TIMEOUT)
    401 		aprint_error("%s: reset never completed!\n",
    402 		    sc->sc_dev.dv_xname);
    403 
    404 	/*
    405 	 * NB: Realtek-supplied Linux driver does this only for
    406 	 * MCFG_METHOD_2, which corresponds to sc->sc_rev == 2.
    407 	 */
    408 	if (1) /* XXX check softc flag for 8169s version */
    409 		CSR_WRITE_1(sc, 0x82, 1);
    410 
    411 	return;
    412 }
    413 
    414 /*
    415  * The following routine is designed to test for a defect on some
    416  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
    417  * lines connected to the bus, however for a 32-bit only card, they
    418  * should be pulled high. The result of this defect is that the
    419  * NIC will not work right if you plug it into a 64-bit slot: DMA
    420  * operations will be done with 64-bit transfers, which will fail
    421  * because the 64-bit data lines aren't connected.
    422  *
    423  * There's no way to work around this (short of talking a soldering
    424  * iron to the board), however we can detect it. The method we use
    425  * here is to put the NIC into digital loopback mode, set the receiver
    426  * to promiscuous mode, and then try to send a frame. We then compare
    427  * the frame data we sent to what was received. If the data matches,
    428  * then the NIC is working correctly, otherwise we know the user has
    429  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
    430  * slot. In the latter case, there's no way the NIC can work correctly,
    431  * so we print out a message on the console and abort the device attach.
    432  */
    433 
    434 static int
    435 re_diag(struct rtk_softc *sc)
    436 {
    437 	struct ifnet		*ifp = &sc->ethercom.ec_if;
    438 	struct mbuf		*m0;
    439 	struct ether_header	*eh;
    440 	struct rtk_desc		*cur_rx;
    441 	bus_dmamap_t		dmamap;
    442 	u_int16_t		status;
    443 	u_int32_t		rxstat;
    444 	int			total_len, i, s, error = 0;
    445 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
    446 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
    447 
    448 	/* Allocate a single mbuf */
    449 
    450 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    451 	if (m0 == NULL)
    452 		return ENOBUFS;
    453 
    454 	/*
    455 	 * Initialize the NIC in test mode. This sets the chip up
    456 	 * so that it can send and receive frames, but performs the
    457 	 * following special functions:
    458 	 * - Puts receiver in promiscuous mode
    459 	 * - Enables digital loopback mode
    460 	 * - Leaves interrupts turned off
    461 	 */
    462 
    463 	ifp->if_flags |= IFF_PROMISC;
    464 	sc->rtk_testmode = 1;
    465 	re_init(ifp);
    466 	re_stop(ifp, 1);
    467 	DELAY(100000);
    468 	re_init(ifp);
    469 
    470 	/* Put some data in the mbuf */
    471 
    472 	eh = mtod(m0, struct ether_header *);
    473 	bcopy((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
    474 	bcopy((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
    475 	eh->ether_type = htons(ETHERTYPE_IP);
    476 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
    477 
    478 	/*
    479 	 * Queue the packet, start transmission.
    480 	 */
    481 
    482 	CSR_WRITE_2(sc, RTK_ISR, 0xFFFF);
    483 	s = splnet();
    484 	IF_ENQUEUE(&ifp->if_snd, m0);
    485 	re_start(ifp);
    486 	splx(s);
    487 	m0 = NULL;
    488 
    489 	/* Wait for it to propagate through the chip */
    490 
    491 	DELAY(100000);
    492 	for (i = 0; i < RTK_TIMEOUT; i++) {
    493 		status = CSR_READ_2(sc, RTK_ISR);
    494 		if ((status & (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK)) ==
    495 		    (RTK_ISR_TIMEOUT_EXPIRED | RTK_ISR_RX_OK))
    496 			break;
    497 		DELAY(10);
    498 	}
    499 	if (i == RTK_TIMEOUT) {
    500 		aprint_error("%s: diagnostic failed, failed to receive packet "
    501 		    "in loopback mode\n", sc->sc_dev.dv_xname);
    502 		error = EIO;
    503 		goto done;
    504 	}
    505 
    506 	/*
    507 	 * The packet should have been dumped into the first
    508 	 * entry in the RX DMA ring. Grab it from there.
    509 	 */
    510 
    511 	dmamap = sc->rtk_ldata.rtk_rx_list_map;
    512 	bus_dmamap_sync(sc->sc_dmat,
    513 	    dmamap, 0, dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    514 	dmamap = sc->rtk_ldata.rtk_rx_dmamap[0];
    515 	bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    516 	    BUS_DMASYNC_POSTWRITE);
    517 	bus_dmamap_unload(sc->sc_dmat,
    518 	    sc->rtk_ldata.rtk_rx_dmamap[0]);
    519 
    520 	m0 = sc->rtk_ldata.rtk_rx_mbuf[0];
    521 	sc->rtk_ldata.rtk_rx_mbuf[0] = NULL;
    522 	eh = mtod(m0, struct ether_header *);
    523 
    524 	cur_rx = &sc->rtk_ldata.rtk_rx_list[0];
    525 	total_len = RTK_RXBYTES(cur_rx);
    526 	rxstat = le32toh(cur_rx->rtk_cmdstat);
    527 
    528 	if (total_len != ETHER_MIN_LEN) {
    529 		aprint_error("%s: diagnostic failed, received short packet\n",
    530 		    sc->sc_dev.dv_xname);
    531 		error = EIO;
    532 		goto done;
    533 	}
    534 
    535 	/* Test that the received packet data matches what we sent. */
    536 
    537 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
    538 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
    539 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
    540 		aprint_error("%s: WARNING, DMA FAILURE!\n",
    541 		    sc->sc_dev.dv_xname);
    542 		aprint_error("%s: expected TX data: %s",
    543 		    sc->sc_dev.dv_xname, ether_sprintf(dst));
    544 		aprint_error("/%s/0x%x\n", ether_sprintf(src), ETHERTYPE_IP);
    545 		aprint_error("%s: received RX data: %s",
    546 		    sc->sc_dev.dv_xname,
    547 		    ether_sprintf(eh->ether_dhost));
    548 		aprint_error("/%s/0x%x\n", ether_sprintf(eh->ether_shost),
    549 		    ntohs(eh->ether_type));
    550 		aprint_error("%s: You may have a defective 32-bit NIC plugged "
    551 		    "into a 64-bit PCI slot.\n", sc->sc_dev.dv_xname);
    552 		aprint_error("%s: Please re-install the NIC in a 32-bit slot "
    553 		    "for proper operation.\n", sc->sc_dev.dv_xname);
    554 		aprint_error("%s: Read the re(4) man page for more details.\n",
    555 		    sc->sc_dev.dv_xname);
    556 		error = EIO;
    557 	}
    558 
    559 done:
    560 	/* Turn interface off, release resources */
    561 
    562 	sc->rtk_testmode = 0;
    563 	ifp->if_flags &= ~IFF_PROMISC;
    564 	re_stop(ifp, 1);
    565 	if (m0 != NULL)
    566 		m_freem(m0);
    567 
    568 	return error;
    569 }
    570 
    571 static int
    572 re_allocmem(struct rtk_softc *sc)
    573 {
    574 	int			error;
    575 	int			nseg, rseg;
    576 	int			i;
    577 
    578 	nseg = 32;
    579 
    580 	/* Allocate DMA'able memory for the TX ring */
    581 
    582 	error = bus_dmamap_create(sc->sc_dmat, RTK_TX_LIST_SZ, 1,
    583 	    RTK_TX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
    584 	    &sc->rtk_ldata.rtk_tx_list_map);
    585 	error = bus_dmamem_alloc(sc->sc_dmat, RTK_TX_LIST_SZ,
    586 	    RTK_ETHER_ALIGN, 0,
    587 	    &sc->rtk_ldata.rtk_tx_listseg, 1, &rseg, BUS_DMA_NOWAIT);
    588 	if (error)
    589 		return ENOMEM;
    590 
    591 	/* Load the map for the TX ring. */
    592 	error = bus_dmamem_map(sc->sc_dmat, &sc->rtk_ldata.rtk_tx_listseg,
    593 	    1, RTK_TX_LIST_SZ,
    594 	    (caddr_t *)&sc->rtk_ldata.rtk_tx_list, BUS_DMA_NOWAIT);
    595 	memset(sc->rtk_ldata.rtk_tx_list, 0, RTK_TX_LIST_SZ);
    596 
    597 	error = bus_dmamap_load(sc->sc_dmat, sc->rtk_ldata.rtk_tx_list_map,
    598 	    sc->rtk_ldata.rtk_tx_list, RTK_TX_LIST_SZ, NULL, BUS_DMA_NOWAIT);
    599 
    600 	/* Create DMA maps for TX buffers */
    601 
    602 	for (i = 0; i < RTK_TX_DESC_CNT; i++) {
    603 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES * nseg, nseg,
    604 		    MCLBYTES, 0, BUS_DMA_ALLOCNOW,
    605 		    &sc->rtk_ldata.rtk_tx_dmamap[i]);
    606 		if (error) {
    607 			aprint_error("%s: can't create DMA map for TX\n",
    608 			    sc->sc_dev.dv_xname);
    609 			return ENOMEM;
    610 		}
    611 	}
    612 
    613 	/* Allocate DMA'able memory for the RX ring */
    614 
    615 	error = bus_dmamap_create(sc->sc_dmat, RTK_RX_LIST_SZ, 1,
    616 	    RTK_RX_LIST_SZ, 0, BUS_DMA_ALLOCNOW,
    617 	    &sc->rtk_ldata.rtk_rx_list_map);
    618 	error = bus_dmamem_alloc(sc->sc_dmat, RTK_RX_LIST_SZ, RTK_RING_ALIGN,
    619 	    0, &sc->rtk_ldata.rtk_rx_listseg, 1, &rseg, BUS_DMA_NOWAIT);
    620 	if (error)
    621 		return ENOMEM;
    622 
    623 	/* Load the map for the RX ring. */
    624 	error = bus_dmamem_map(sc->sc_dmat, &sc->rtk_ldata.rtk_rx_listseg,
    625 	    1, RTK_RX_LIST_SZ,
    626 	    (caddr_t *)&sc->rtk_ldata.rtk_rx_list, BUS_DMA_NOWAIT);
    627 	memset(sc->rtk_ldata.rtk_rx_list, 0, RTK_TX_LIST_SZ);
    628 
    629 	error = bus_dmamap_load(sc->sc_dmat, sc->rtk_ldata.rtk_rx_list_map,
    630 	    sc->rtk_ldata.rtk_rx_list, RTK_RX_LIST_SZ, NULL, BUS_DMA_NOWAIT);
    631 
    632 	/* Create DMA maps for RX buffers */
    633 
    634 	for (i = 0; i < RTK_RX_DESC_CNT; i++) {
    635 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES * nseg, nseg,
    636 		    MCLBYTES, 0, BUS_DMA_ALLOCNOW,
    637 		    &sc->rtk_ldata.rtk_rx_dmamap[i]);
    638 		if (error) {
    639 			aprint_error("%s: can't create DMA map for RX\n",
    640 			    sc->sc_dev.dv_xname);
    641 			return ENOMEM;
    642 		}
    643 	}
    644 
    645 	return 0;
    646 }
    647 
    648 /*
    649  * Attach the interface. Allocate softc structures, do ifmedia
    650  * setup and ethernet/BPF attach.
    651  */
    652 void
    653 re_attach(struct rtk_softc *sc)
    654 {
    655 	u_char			eaddr[ETHER_ADDR_LEN];
    656 	u_int16_t		val;
    657 	struct ifnet		*ifp;
    658 	int			error = 0, i, addr_len;
    659 
    660 	/* XXX JRS: bus-attach-independent code begins approximately here */
    661 
    662 	/* Reset the adapter. */
    663 	re_reset(sc);
    664 
    665 	if (sc->rtk_type == RTK_8169) {
    666 		uint32_t hwrev;
    667 
    668 		/* Revision of 8169/8169S/8110s in bits 30..26, 23 */
    669 		hwrev = CSR_READ_4(sc, RTK_TXCFG) & 0x7c800000;
    670 		if (hwrev == (0x1 << 28)) {
    671 			sc->sc_rev = 4;
    672 		} else if (hwrev == (0x1 << 26)) {
    673 			sc->sc_rev = 3;
    674 		} else if (hwrev == (0x1 << 23)) {
    675 			sc->sc_rev = 2;
    676 		} else
    677 			sc->sc_rev = 1;
    678 #if defined(DEBUG) || 1
    679 		aprint_normal("re_attach: MAC chip hwrev 0x%x softc %d\n",
    680 		    hwrev, sc->sc_rev);
    681 #endif
    682 
    683 		/* Set RX length mask */
    684 
    685 		sc->rtk_rxlenmask = RTK_RDESC_STAT_GFRAGLEN;
    686 
    687 		/* Force station address autoload from the EEPROM */
    688 
    689 		CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_AUTOLOAD);
    690 		for (i = 0; i < RTK_TIMEOUT; i++) {
    691 			if (!(CSR_READ_1(sc, RTK_EECMD) & RTK_EEMODE_AUTOLOAD))
    692 				break;
    693 			DELAY(100);
    694 		}
    695 		if (i == RTK_TIMEOUT)
    696 			aprint_error("%s: eeprom autoload timed out\n",
    697 			    sc->sc_dev.dv_xname);
    698 
    699 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    700 			eaddr[i] = CSR_READ_1(sc, RTK_IDR0 + i);
    701 	} else {
    702 
    703 		/* Set RX length mask */
    704 
    705 		sc->rtk_rxlenmask = RTK_RDESC_STAT_FRAGLEN;
    706 
    707 		if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
    708 			addr_len = RTK_EEADDR_LEN1;
    709 		else
    710 			addr_len = RTK_EEADDR_LEN0;
    711 
    712 		/*
    713 		 * Get station address from the EEPROM.
    714 		 */
    715 		for (i = 0; i < 3; i++) {
    716 			val = rtk_read_eeprom(sc, RTK_EE_EADDR0 + i, addr_len);
    717 			eaddr[(i * 2) + 0] = val & 0xff;
    718 			eaddr[(i * 2) + 1] = val >> 8;
    719 		}
    720 	}
    721 
    722 	aprint_normal("%s: Ethernet address %s\n",
    723 	    sc->sc_dev.dv_xname, ether_sprintf(eaddr));
    724 
    725 	error = re_allocmem(sc);
    726 
    727 	if (error) {
    728 		aprint_error("%s: attach aborted, re_allocmem() failure\n",
    729 		    sc->sc_dev.dv_xname);
    730 		goto fail;
    731 	}
    732 
    733 	ifp = &sc->ethercom.ec_if;
    734 	ifp->if_softc = sc;
    735 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    736 	ifp->if_mtu = ETHERMTU;
    737 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    738 	ifp->if_ioctl = re_ioctl;
    739 	sc->ethercom.ec_capabilities |=
    740 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
    741 	ifp->if_start = re_start;
    742 	ifp->if_stop = re_stop;
    743 	ifp->if_capabilities |=
    744 	    IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
    745 	ifp->if_watchdog = re_watchdog;
    746 	ifp->if_init = re_init;
    747 	if (sc->rtk_type == RTK_8169)
    748 		ifp->if_baudrate = 1000000000;
    749 	else
    750 		ifp->if_baudrate = 100000000;
    751 	ifp->if_snd.ifq_maxlen = RTK_IFQ_MAXLEN;
    752 	ifp->if_capenable = ifp->if_capabilities;
    753 	IFQ_SET_READY(&ifp->if_snd);
    754 
    755 	callout_init(&sc->rtk_tick_ch);
    756 
    757 	/* Do MII setup */
    758 	sc->mii.mii_ifp = ifp;
    759 	sc->mii.mii_readreg = re_miibus_readreg;
    760 	sc->mii.mii_writereg = re_miibus_writereg;
    761 	sc->mii.mii_statchg = re_miibus_statchg;
    762 	ifmedia_init(&sc->mii.mii_media, IFM_IMASK, re_ifmedia_upd,
    763 	    re_ifmedia_sts);
    764 	mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff, MII_PHY_ANY,
    765 	    MII_OFFSET_ANY, 0);
    766 	ifmedia_set(&sc->mii.mii_media, IFM_ETHER | IFM_AUTO);
    767 
    768 	/*
    769 	 * Call MI attach routine.
    770 	 */
    771 	if_attach(ifp);
    772 	ether_ifattach(ifp, eaddr);
    773 
    774 	/*
    775 	 * Perform hardware diagnostic.
    776 	 * XXX: this diagnostic only makes sense for attachemnts with 64-bit
    777 	 * busses: PCI, but not CardBus.
    778 	 */
    779 	error = re_diag(sc);
    780 
    781 	if (error) {
    782 		aprint_error(
    783 		    "%s: attach aborted due to hardware diag failure\n",
    784 		    sc->sc_dev.dv_xname);
    785 		ether_ifdetach(ifp);
    786 		if_detach(ifp);
    787 		goto fail;
    788 	}
    789 
    790 	/*
    791 	 * Record interface as attached. From here, we should not fail.
    792 	 */
    793 
    794 	/*
    795 	 * Make sure the interface is shutdown during reboot.
    796 	 */
    797 	sc->sc_sdhook = shutdownhook_establish(re_shutdown, sc);
    798 	if (sc->sc_sdhook == NULL)
    799 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    800 		    sc->sc_dev.dv_xname);
    801 	/*
    802 	 * Add a suspend hook to make sure we come back up after a
    803 	 * resume.
    804 	 */
    805 	sc->sc_powerhook = powerhook_establish(re_power, sc);
    806 	if (sc->sc_powerhook == NULL)
    807 		aprint_error("%s: WARNING: unable to establish power hook\n",
    808 		    sc->sc_dev.dv_xname);
    809 
    810 	sc->sc_flags |= RTK_ATTACHED;
    811 
    812 fail:
    813 	return;
    814 }
    815 
    816 
    817 /*
    818  * re_activate:
    819  *     Handle device activation/deactivation requests.
    820  */
    821 int
    822 re_activate(struct device *self, enum devact act)
    823 {
    824 	struct rtk_softc *sc = (void *) self;
    825 	int s, error = 0;
    826 
    827 	s = splnet();
    828 	switch (act) {
    829 	case DVACT_ACTIVATE:
    830 		error = EOPNOTSUPP;
    831 		break;
    832 	case DVACT_DEACTIVATE:
    833 		mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
    834 		if_deactivate(&sc->ethercom.ec_if);
    835 		break;
    836 	}
    837 	splx(s);
    838 
    839 	return error;
    840 }
    841 
    842 /*
    843  * re_detach:
    844  *     Detach a rtk interface.
    845  */
    846 int
    847 re_detach(struct rtk_softc *sc)
    848 {
    849 	struct ifnet *ifp = &sc->ethercom.ec_if;
    850 
    851 	/*
    852 	 * Succeed now if there isn't any work to do.
    853 	 */
    854 	if ((sc->sc_flags & RTK_ATTACHED) == 0)
    855 		return 0;
    856 
    857 	/* Unhook our tick handler. */
    858 	callout_stop(&sc->rtk_tick_ch);
    859 
    860 	/* Detach all PHYs. */
    861 	mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
    862 
    863 	/* Delete all remaining media. */
    864 	ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
    865 
    866 	ether_ifdetach(ifp);
    867 	if_detach(ifp);
    868 
    869 	/* XXX undo re_allocmem() */
    870 #if 0 /* bogus cut-and-paste from rtl81x9.c */
    871 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    872 		txd = &sc->rtk_tx_descs[i];
    873 		if (txd->txd_dmamap != NULL)
    874 			bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
    875 	}
    876 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    877 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_rx_buf,
    878 	    RTK_RXBUFLEN + 16);
    879 	/* XXX TX ring, Tx buffer, Rx ring, Rx buffer */
    880 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    881 #endif
    882 
    883 	shutdownhook_disestablish(sc->sc_sdhook);
    884 	powerhook_disestablish(sc->sc_powerhook);
    885 
    886 	return 0;
    887 }
    888 
    889 /*
    890  * re_enable:
    891  *     Enable the RTL81X9 chip.
    892  */
    893 static int
    894 re_enable(struct rtk_softc *sc)
    895 {
    896 	if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
    897 		if ((*sc->sc_enable)(sc) != 0) {
    898 			aprint_error("%s: device enable failed\n",
    899 			    sc->sc_dev.dv_xname);
    900 			return EIO;
    901 		}
    902 		sc->sc_flags |= RTK_ENABLED;
    903 	}
    904 	return 0;
    905 }
    906 
    907 /*
    908  * re_disable:
    909  *     Disable the RTL81X9 chip.
    910  */
    911 static void
    912 re_disable(struct rtk_softc *sc)
    913 {
    914 
    915 	if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
    916 		(*sc->sc_disable)(sc);
    917 		sc->sc_flags &= ~RTK_ENABLED;
    918 	}
    919 }
    920 
    921 /*
    922  * re_power:
    923  *     Power management (suspend/resume) hook.
    924  */
    925 void
    926 re_power(int why, void *arg)
    927 {
    928 	struct rtk_softc *sc = (void *) arg;
    929 	struct ifnet *ifp = &sc->ethercom.ec_if;
    930 	int s;
    931 
    932 	s = splnet();
    933 	switch (why) {
    934 	case PWR_SUSPEND:
    935 	case PWR_STANDBY:
    936 		re_stop(ifp, 0);
    937 		if (sc->sc_power != NULL)
    938 			(*sc->sc_power)(sc, why);
    939 		break;
    940 	case PWR_RESUME:
    941 		if (ifp->if_flags & IFF_UP) {
    942 			if (sc->sc_power != NULL)
    943 				(*sc->sc_power)(sc, why);
    944 			re_init(ifp);
    945 		}
    946 		break;
    947 	case PWR_SOFTSUSPEND:
    948 	case PWR_SOFTSTANDBY:
    949 	case PWR_SOFTRESUME:
    950 		break;
    951 	}
    952 	splx(s);
    953 }
    954 
    955 
    956 static int
    957 re_newbuf(struct rtk_softc *sc, int idx, struct mbuf *m)
    958 {
    959 	struct mbuf		*n = NULL;
    960 	bus_dmamap_t		map;
    961 	struct rtk_desc		*d;
    962 	u_int32_t		cmdstat;
    963 	int			error;
    964 
    965 	if (m == NULL) {
    966 		MGETHDR(n, M_DONTWAIT, MT_DATA);
    967 		if (n == NULL)
    968 			return ENOBUFS;
    969 		m = n;
    970 
    971 		MCLGET(m, M_DONTWAIT);
    972 		if (!(m->m_flags & M_EXT)) {
    973 			m_freem(m);
    974 			return ENOBUFS;
    975 		}
    976 	} else
    977 		m->m_data = m->m_ext.ext_buf;
    978 
    979 	/*
    980 	 * Initialize mbuf length fields and fixup
    981 	 * alignment so that the frame payload is
    982 	 * longword aligned.
    983 	 */
    984 	m->m_len = m->m_pkthdr.len = MCLBYTES;
    985 	m_adj(m, RTK_ETHER_ALIGN);
    986 
    987 	map = sc->rtk_ldata.rtk_rx_dmamap[idx];
    988 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT);
    989 
    990 	if (map->dm_nsegs > 1)
    991 		goto out;
    992 	if (error)
    993 		goto out;
    994 
    995 	d = &sc->rtk_ldata.rtk_rx_list[idx];
    996 	if (le32toh(d->rtk_cmdstat) & RTK_RDESC_STAT_OWN)
    997 		goto out;
    998 
    999 	cmdstat = map->dm_segs[0].ds_len;
   1000 	d->rtk_bufaddr_lo = htole32(RTK_ADDR_LO(map->dm_segs[0].ds_addr));
   1001 	d->rtk_bufaddr_hi = htole32(RTK_ADDR_HI(map->dm_segs[0].ds_addr));
   1002 	cmdstat |= RTK_TDESC_CMD_SOF;
   1003 	if (idx == (RTK_RX_DESC_CNT - 1))
   1004 		cmdstat |= RTK_TDESC_CMD_EOR;
   1005 	d->rtk_cmdstat = htole32(cmdstat);
   1006 
   1007 	d->rtk_cmdstat |= htole32(RTK_TDESC_CMD_EOF);
   1008 
   1009 
   1010 	sc->rtk_ldata.rtk_rx_list[idx].rtk_cmdstat |=
   1011 	    htole32(RTK_RDESC_CMD_OWN);
   1012 	sc->rtk_ldata.rtk_rx_mbuf[idx] = m;
   1013 
   1014 	bus_dmamap_sync(sc->sc_dmat, sc->rtk_ldata.rtk_rx_dmamap[idx], 0,
   1015 	    sc->rtk_ldata.rtk_rx_dmamap[idx]->dm_mapsize,
   1016 	    BUS_DMASYNC_PREREAD);
   1017 
   1018 	return 0;
   1019 out:
   1020 	if (n != NULL)
   1021 		m_freem(n);
   1022 	return ENOMEM;
   1023 }
   1024 
   1025 static int
   1026 re_tx_list_init(struct rtk_softc *sc)
   1027 {
   1028 	memset((char *)sc->rtk_ldata.rtk_tx_list, 0, RTK_TX_LIST_SZ);
   1029 	memset((char *)&sc->rtk_ldata.rtk_tx_mbuf, 0,
   1030 	    (RTK_TX_DESC_CNT * sizeof(struct mbuf *)));
   1031 
   1032 	bus_dmamap_sync(sc->sc_dmat,
   1033 	    sc->rtk_ldata.rtk_tx_list_map, 0,
   1034 	    sc->rtk_ldata.rtk_tx_list_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1035 	sc->rtk_ldata.rtk_tx_prodidx = 0;
   1036 	sc->rtk_ldata.rtk_tx_considx = 0;
   1037 	sc->rtk_ldata.rtk_tx_free = RTK_TX_DESC_CNT;
   1038 
   1039 	return 0;
   1040 }
   1041 
   1042 static int
   1043 re_rx_list_init(struct rtk_softc *sc)
   1044 {
   1045 	int			i;
   1046 
   1047 	memset((char *)sc->rtk_ldata.rtk_rx_list, 0, RTK_RX_LIST_SZ);
   1048 	memset((char *)&sc->rtk_ldata.rtk_rx_mbuf, 0,
   1049 	    (RTK_RX_DESC_CNT * sizeof(struct mbuf *)));
   1050 
   1051 	for (i = 0; i < RTK_RX_DESC_CNT; i++) {
   1052 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
   1053 			return ENOBUFS;
   1054 	}
   1055 
   1056 	/* Flush the RX descriptors */
   1057 
   1058 	bus_dmamap_sync(sc->sc_dmat,
   1059 	    sc->rtk_ldata.rtk_rx_list_map,
   1060 	    0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
   1061 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1062 
   1063 	sc->rtk_ldata.rtk_rx_prodidx = 0;
   1064 	sc->rtk_head = sc->rtk_tail = NULL;
   1065 
   1066 	return 0;
   1067 }
   1068 
   1069 /*
   1070  * RX handler for C+ and 8169. For the gigE chips, we support
   1071  * the reception of jumbo frames that have been fragmented
   1072  * across multiple 2K mbuf cluster buffers.
   1073  */
   1074 static void
   1075 re_rxeof(struct rtk_softc *sc)
   1076 {
   1077 	struct mbuf		*m;
   1078 	struct ifnet		*ifp;
   1079 	int			i, total_len;
   1080 	struct rtk_desc		*cur_rx;
   1081 	struct m_tag		*mtag;
   1082 	u_int32_t		rxstat, rxvlan;
   1083 
   1084 	ifp = &sc->ethercom.ec_if;
   1085 	i = sc->rtk_ldata.rtk_rx_prodidx;
   1086 
   1087 	/* Invalidate the descriptor memory */
   1088 
   1089 	bus_dmamap_sync(sc->sc_dmat,
   1090 	    sc->rtk_ldata.rtk_rx_list_map,
   1091 	    0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
   1092 	    BUS_DMASYNC_POSTREAD);
   1093 
   1094 	while (!RTK_OWN(&sc->rtk_ldata.rtk_rx_list[i])) {
   1095 
   1096 		cur_rx = &sc->rtk_ldata.rtk_rx_list[i];
   1097 		m = sc->rtk_ldata.rtk_rx_mbuf[i];
   1098 		total_len = RTK_RXBYTES(cur_rx);
   1099 		rxstat = le32toh(cur_rx->rtk_cmdstat);
   1100 		rxvlan = le32toh(cur_rx->rtk_vlanctl);
   1101 
   1102 		/* Invalidate the RX mbuf and unload its map */
   1103 
   1104 		bus_dmamap_sync(sc->sc_dmat,
   1105 		    sc->rtk_ldata.rtk_rx_dmamap[i],
   1106 		    0, sc->rtk_ldata.rtk_rx_dmamap[i]->dm_mapsize,
   1107 		    BUS_DMASYNC_POSTWRITE);
   1108 		bus_dmamap_unload(sc->sc_dmat,
   1109 		    sc->rtk_ldata.rtk_rx_dmamap[i]);
   1110 
   1111 		if (!(rxstat & RTK_RDESC_STAT_EOF)) {
   1112 			m->m_len = MCLBYTES - RTK_ETHER_ALIGN;
   1113 			if (sc->rtk_head == NULL)
   1114 				sc->rtk_head = sc->rtk_tail = m;
   1115 			else {
   1116 				m->m_flags &= ~M_PKTHDR;
   1117 				sc->rtk_tail->m_next = m;
   1118 				sc->rtk_tail = m;
   1119 			}
   1120 			re_newbuf(sc, i, NULL);
   1121 			RTK_DESC_INC(i);
   1122 			continue;
   1123 		}
   1124 
   1125 		/*
   1126 		 * NOTE: for the 8139C+, the frame length field
   1127 		 * is always 12 bits in size, but for the gigE chips,
   1128 		 * it is 13 bits (since the max RX frame length is 16K).
   1129 		 * Unfortunately, all 32 bits in the status word
   1130 		 * were already used, so to make room for the extra
   1131 		 * length bit, RealTek took out the 'frame alignment
   1132 		 * error' bit and shifted the other status bits
   1133 		 * over one slot. The OWN, EOR, FS and LS bits are
   1134 		 * still in the same places. We have already extracted
   1135 		 * the frame length and checked the OWN bit, so rather
   1136 		 * than using an alternate bit mapping, we shift the
   1137 		 * status bits one space to the right so we can evaluate
   1138 		 * them using the 8169 status as though it was in the
   1139 		 * same format as that of the 8139C+.
   1140 		 */
   1141 		if (sc->rtk_type == RTK_8169)
   1142 			rxstat >>= 1;
   1143 
   1144 		if (rxstat & RTK_RDESC_STAT_RXERRSUM) {
   1145 			ifp->if_ierrors++;
   1146 			/*
   1147 			 * If this is part of a multi-fragment packet,
   1148 			 * discard all the pieces.
   1149 			 */
   1150 			if (sc->rtk_head != NULL) {
   1151 				m_freem(sc->rtk_head);
   1152 				sc->rtk_head = sc->rtk_tail = NULL;
   1153 			}
   1154 			re_newbuf(sc, i, m);
   1155 			RTK_DESC_INC(i);
   1156 			continue;
   1157 		}
   1158 
   1159 		/*
   1160 		 * If allocating a replacement mbuf fails,
   1161 		 * reload the current one.
   1162 		 */
   1163 
   1164 		if (re_newbuf(sc, i, NULL)) {
   1165 			ifp->if_ierrors++;
   1166 			if (sc->rtk_head != NULL) {
   1167 				m_freem(sc->rtk_head);
   1168 				sc->rtk_head = sc->rtk_tail = NULL;
   1169 			}
   1170 			re_newbuf(sc, i, m);
   1171 			RTK_DESC_INC(i);
   1172 			continue;
   1173 		}
   1174 
   1175 		RTK_DESC_INC(i);
   1176 
   1177 		if (sc->rtk_head != NULL) {
   1178 			m->m_len = total_len % (MCLBYTES - RTK_ETHER_ALIGN);
   1179 			/*
   1180 			 * Special case: if there's 4 bytes or less
   1181 			 * in this buffer, the mbuf can be discarded:
   1182 			 * the last 4 bytes is the CRC, which we don't
   1183 			 * care about anyway.
   1184 			 */
   1185 			if (m->m_len <= ETHER_CRC_LEN) {
   1186 				sc->rtk_tail->m_len -=
   1187 				    (ETHER_CRC_LEN - m->m_len);
   1188 				m_freem(m);
   1189 			} else {
   1190 				m->m_len -= ETHER_CRC_LEN;
   1191 				m->m_flags &= ~M_PKTHDR;
   1192 				sc->rtk_tail->m_next = m;
   1193 			}
   1194 			m = sc->rtk_head;
   1195 			sc->rtk_head = sc->rtk_tail = NULL;
   1196 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
   1197 		} else
   1198 			m->m_pkthdr.len = m->m_len =
   1199 			    (total_len - ETHER_CRC_LEN);
   1200 
   1201 		ifp->if_ipackets++;
   1202 		m->m_pkthdr.rcvif = ifp;
   1203 
   1204 		/* Do RX checksumming if enabled */
   1205 
   1206 		if (ifp->if_capenable & IFCAP_CSUM_IPv4) {
   1207 
   1208 			/* Check IP header checksum */
   1209 			if (rxstat & RTK_RDESC_STAT_PROTOID)
   1210 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;;
   1211 			if (rxstat & RTK_RDESC_STAT_IPSUMBAD)
   1212 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1213 		}
   1214 
   1215 		/* Check TCP/UDP checksum */
   1216 		if (RTK_TCPPKT(rxstat) &&
   1217 		    (ifp->if_capenable & IFCAP_CSUM_TCPv4)) {
   1218 			m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1219 			if (rxstat & RTK_RDESC_STAT_TCPSUMBAD)
   1220 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1221 		}
   1222 		if (RTK_UDPPKT(rxstat) &&
   1223 		    (ifp->if_capenable & IFCAP_CSUM_UDPv4)) {
   1224 			m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1225 			if (rxstat & RTK_RDESC_STAT_UDPSUMBAD)
   1226 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1227 		}
   1228 
   1229 		if (rxvlan & RTK_RDESC_VLANCTL_TAG) {
   1230 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
   1231 			    M_NOWAIT);
   1232 			if (mtag == NULL) {
   1233 				ifp->if_ierrors++;
   1234 				m_freem(m);
   1235 				continue;
   1236 			}
   1237 			*(u_int *)(mtag + 1) =
   1238 			    be16toh(rxvlan & RTK_RDESC_VLANCTL_DATA);
   1239 			m_tag_prepend(m, mtag);
   1240 		}
   1241 #if NBPFILTER > 0
   1242 		if (ifp->if_bpf)
   1243 			bpf_mtap(ifp->if_bpf, m);
   1244 #endif
   1245 		(*ifp->if_input)(ifp, m);
   1246 	}
   1247 
   1248 	/* Flush the RX DMA ring */
   1249 
   1250 	bus_dmamap_sync(sc->sc_dmat,
   1251 	    sc->rtk_ldata.rtk_rx_list_map,
   1252 	    0, sc->rtk_ldata.rtk_rx_list_map->dm_mapsize,
   1253 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1254 
   1255 	sc->rtk_ldata.rtk_rx_prodidx = i;
   1256 
   1257 	return;
   1258 }
   1259 
   1260 static void
   1261 re_txeof(struct rtk_softc *sc)
   1262 {
   1263 	struct ifnet		*ifp;
   1264 	u_int32_t		txstat;
   1265 	int			idx;
   1266 
   1267 	ifp = &sc->ethercom.ec_if;
   1268 	idx = sc->rtk_ldata.rtk_tx_considx;
   1269 
   1270 	/* Invalidate the TX descriptor list */
   1271 
   1272 	bus_dmamap_sync(sc->sc_dmat,
   1273 	    sc->rtk_ldata.rtk_tx_list_map,
   1274 	    0, sc->rtk_ldata.rtk_tx_list_map->dm_mapsize,
   1275 	    BUS_DMASYNC_POSTREAD);
   1276 
   1277 	while (idx != sc->rtk_ldata.rtk_tx_prodidx) {
   1278 
   1279 		txstat = le32toh(sc->rtk_ldata.rtk_tx_list[idx].rtk_cmdstat);
   1280 		if (txstat & RTK_TDESC_CMD_OWN)
   1281 			break;
   1282 
   1283 		/*
   1284 		 * We only stash mbufs in the last descriptor
   1285 		 * in a fragment chain, which also happens to
   1286 		 * be the only place where the TX status bits
   1287 		 * are valid.
   1288 		 */
   1289 
   1290 		if (txstat & RTK_TDESC_CMD_EOF) {
   1291 			m_freem(sc->rtk_ldata.rtk_tx_mbuf[idx]);
   1292 			sc->rtk_ldata.rtk_tx_mbuf[idx] = NULL;
   1293 			bus_dmamap_unload(sc->sc_dmat,
   1294 			    sc->rtk_ldata.rtk_tx_dmamap[idx]);
   1295 			if (txstat & (RTK_TDESC_STAT_EXCESSCOL |
   1296 			    RTK_TDESC_STAT_COLCNT))
   1297 				ifp->if_collisions++;
   1298 			if (txstat & RTK_TDESC_STAT_TXERRSUM)
   1299 				ifp->if_oerrors++;
   1300 			else
   1301 				ifp->if_opackets++;
   1302 		}
   1303 		sc->rtk_ldata.rtk_tx_free++;
   1304 		RTK_DESC_INC(idx);
   1305 	}
   1306 
   1307 	/* No changes made to the TX ring, so no flush needed */
   1308 
   1309 	if (idx != sc->rtk_ldata.rtk_tx_considx) {
   1310 		sc->rtk_ldata.rtk_tx_considx = idx;
   1311 		ifp->if_flags &= ~IFF_OACTIVE;
   1312 		ifp->if_timer = 0;
   1313 	}
   1314 
   1315 	/*
   1316 	 * If not all descriptors have been released reaped yet,
   1317 	 * reload the timer so that we will eventually get another
   1318 	 * interrupt that will cause us to re-enter this routine.
   1319 	 * This is done in case the transmitter has gone idle.
   1320 	 */
   1321 	if (sc->rtk_ldata.rtk_tx_free != RTK_TX_DESC_CNT)
   1322 		CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
   1323 
   1324 	return;
   1325 }
   1326 
   1327 /*
   1328  * Stop all chip I/O so that the kernel's probe routines don't
   1329  * get confused by errant DMAs when rebooting.
   1330  */
   1331 static void
   1332 re_shutdown(void *vsc)
   1333 
   1334 {
   1335 	struct rtk_softc	*sc = (struct rtk_softc *)vsc;
   1336 
   1337 	re_stop(&sc->ethercom.ec_if, 0);
   1338 }
   1339 
   1340 
   1341 static void
   1342 re_tick(void *xsc)
   1343 {
   1344 	struct rtk_softc	*sc = xsc;
   1345 	int s;
   1346 
   1347 	/*XXX: just return for 8169S/8110S with rev 2 or newer phy */
   1348 	s = splnet();
   1349 
   1350 	mii_tick(&sc->mii);
   1351 	splx(s);
   1352 
   1353 	callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
   1354 }
   1355 
   1356 #ifdef DEVICE_POLLING
   1357 static void
   1358 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
   1359 {
   1360 	struct rtk_softc *sc = ifp->if_softc;
   1361 
   1362 	RTK_LOCK(sc);
   1363 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
   1364 		ether_poll_deregister(ifp);
   1365 		cmd = POLL_DEREGISTER;
   1366 	}
   1367 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
   1368 		CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_CPLUS);
   1369 		goto done;
   1370 	}
   1371 
   1372 	sc->rxcycles = count;
   1373 	re_rxeof(sc);
   1374 	re_txeof(sc);
   1375 
   1376 	if (ifp->if_snd.ifq_head != NULL)
   1377 		(*ifp->if_start)(ifp);
   1378 
   1379 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
   1380 		u_int16_t       status;
   1381 
   1382 		status = CSR_READ_2(sc, RTK_ISR);
   1383 		if (status == 0xffff)
   1384 			goto done;
   1385 		if (status)
   1386 			CSR_WRITE_2(sc, RTK_ISR, status);
   1387 
   1388 		/*
   1389 		 * XXX check behaviour on receiver stalls.
   1390 		 */
   1391 
   1392 		if (status & RTK_ISR_SYSTEM_ERR) {
   1393 			re_reset(sc);
   1394 			re_init(sc);
   1395 		}
   1396 	}
   1397 done:
   1398 	RTK_UNLOCK(sc);
   1399 }
   1400 #endif /* DEVICE_POLLING */
   1401 
   1402 int
   1403 re_intr(void *arg)
   1404 {
   1405 	struct rtk_softc	*sc = arg;
   1406 	struct ifnet		*ifp;
   1407 	u_int16_t		status;
   1408 	int			handled = 0;
   1409 
   1410 	ifp = &sc->ethercom.ec_if;
   1411 
   1412 	if (!(ifp->if_flags & IFF_UP))
   1413 		return 0;
   1414 
   1415 #ifdef DEVICE_POLLING
   1416 	if (ifp->if_flags & IFF_POLLING)
   1417 		goto done;
   1418 	if ((ifp->if_capenable & IFCAP_POLLING) &&
   1419 	    ether_poll_register(re_poll, ifp)) { /* ok, disable interrupts */
   1420 		CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1421 		re_poll(ifp, 0, 1);
   1422 		goto done;
   1423 	}
   1424 #endif /* DEVICE_POLLING */
   1425 
   1426 	for (;;) {
   1427 
   1428 		status = CSR_READ_2(sc, RTK_ISR);
   1429 		/* If the card has gone away the read returns 0xffff. */
   1430 		if (status == 0xffff)
   1431 			break;
   1432 		if (status) {
   1433 			handled = 1;
   1434 			CSR_WRITE_2(sc, RTK_ISR, status);
   1435 		}
   1436 
   1437 		if ((status & RTK_INTRS_CPLUS) == 0)
   1438 			break;
   1439 
   1440 		if (status & RTK_ISR_RX_OK)
   1441 			re_rxeof(sc);
   1442 
   1443 		if (status & RTK_ISR_RX_ERR)
   1444 			re_rxeof(sc);
   1445 
   1446 		if ((status & RTK_ISR_TIMEOUT_EXPIRED) ||
   1447 		    (status & RTK_ISR_TX_ERR) ||
   1448 		    (status & RTK_ISR_TX_DESC_UNAVAIL))
   1449 			re_txeof(sc);
   1450 
   1451 		if (status & RTK_ISR_SYSTEM_ERR) {
   1452 			re_reset(sc);
   1453 			re_init(ifp);
   1454 		}
   1455 
   1456 		if (status & RTK_ISR_LINKCHG) {
   1457 			callout_stop(&sc->rtk_tick_ch);
   1458 			re_tick(sc);
   1459 		}
   1460 	}
   1461 
   1462 	if (ifp->if_flags & IFF_UP) /* kludge for interrupt during re_init() */
   1463 		if (ifp->if_snd.ifq_head != NULL)
   1464 			(*ifp->if_start)(ifp);
   1465 
   1466 #ifdef DEVICE_POLLING
   1467 done:
   1468 #endif
   1469 
   1470 	return handled;
   1471 }
   1472 
   1473 static int
   1474 re_encap(struct rtk_softc *sc, struct mbuf *m_head, int *idx)
   1475 {
   1476 	bus_dmamap_t		map;
   1477 	int			error, i, curidx;
   1478 	struct m_tag		*mtag;
   1479 	struct rtk_desc		*d;
   1480 	u_int32_t		cmdstat, rtk_flags;
   1481 
   1482 	if (sc->rtk_ldata.rtk_tx_free <= 4)
   1483 		return EFBIG;
   1484 
   1485 	/*
   1486 	 * Set up checksum offload. Note: checksum offload bits must
   1487 	 * appear in all descriptors of a multi-descriptor transmit
   1488 	 * attempt. (This is according to testing done with an 8169
   1489 	 * chip. I'm not sure if this is a requirement or a bug.)
   1490 	 */
   1491 
   1492 	rtk_flags = 0;
   1493 
   1494 	if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
   1495 		rtk_flags |= RTK_TDESC_CMD_IPCSUM;
   1496 	if (m_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
   1497 		rtk_flags |= RTK_TDESC_CMD_TCPCSUM;
   1498 	if (m_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
   1499 		rtk_flags |= RTK_TDESC_CMD_UDPCSUM;
   1500 
   1501 	map = sc->rtk_ldata.rtk_tx_dmamap[*idx];
   1502 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map,
   1503 	    m_head, BUS_DMA_NOWAIT);
   1504 
   1505 	if (error) {
   1506 		aprint_error("%s: can't map mbuf (error %d)\n",
   1507 		    sc->sc_dev.dv_xname, error);
   1508 		return ENOBUFS;
   1509 	}
   1510 
   1511 	if (map->dm_nsegs > sc->rtk_ldata.rtk_tx_free - 4)
   1512 		return ENOBUFS;
   1513 	/*
   1514 	 * Map the segment array into descriptors. Note that we set the
   1515 	 * start-of-frame and end-of-frame markers for either TX or RX, but
   1516 	 * they really only have meaning in the TX case. (In the RX case,
   1517 	 * it's the chip that tells us where packets begin and end.)
   1518 	 * We also keep track of the end of the ring and set the
   1519 	 * end-of-ring bits as needed, and we set the ownership bits
   1520 	 * in all except the very first descriptor. (The caller will
   1521 	 * set this descriptor later when it start transmission or
   1522 	 * reception.)
   1523 	 */
   1524 	i = 0;
   1525 	curidx = *idx;
   1526 	while (1) {
   1527 		d = &sc->rtk_ldata.rtk_tx_list[curidx];
   1528 		if (le32toh(d->rtk_cmdstat) & RTK_RDESC_STAT_OWN)
   1529 			return ENOBUFS;
   1530 
   1531 		cmdstat = map->dm_segs[i].ds_len;
   1532 		d->rtk_bufaddr_lo =
   1533 		    htole32(RTK_ADDR_LO(map->dm_segs[i].ds_addr));
   1534 		d->rtk_bufaddr_hi =
   1535 		    htole32(RTK_ADDR_HI(map->dm_segs[i].ds_addr));
   1536 		if (i == 0)
   1537 			cmdstat |= RTK_TDESC_CMD_SOF;
   1538 		else
   1539 			cmdstat |= RTK_TDESC_CMD_OWN;
   1540 		if (curidx == (RTK_RX_DESC_CNT - 1))
   1541 			cmdstat |= RTK_TDESC_CMD_EOR;
   1542 		d->rtk_cmdstat = htole32(cmdstat | rtk_flags);
   1543 		i++;
   1544 		if (i == map->dm_nsegs)
   1545 			break;
   1546 		RTK_DESC_INC(curidx);
   1547 	}
   1548 
   1549 	d->rtk_cmdstat |= htole32(RTK_TDESC_CMD_EOF);
   1550 
   1551 	/*
   1552 	 * Insure that the map for this transmission
   1553 	 * is placed at the array index of the last descriptor
   1554 	 * in this chain.
   1555 	 */
   1556 	sc->rtk_ldata.rtk_tx_dmamap[*idx] =
   1557 	    sc->rtk_ldata.rtk_tx_dmamap[curidx];
   1558 	sc->rtk_ldata.rtk_tx_dmamap[curidx] = map;
   1559 	sc->rtk_ldata.rtk_tx_mbuf[curidx] = m_head;
   1560 	sc->rtk_ldata.rtk_tx_free -= map->dm_nsegs;
   1561 
   1562 	/*
   1563 	 * Set up hardware VLAN tagging. Note: vlan tag info must
   1564 	 * appear in the first descriptor of a multi-descriptor
   1565 	 * transmission attempt.
   1566 	 */
   1567 
   1568 	if (sc->ethercom.ec_nvlans &&
   1569 	    (mtag = m_tag_find(m_head, PACKET_TAG_VLAN, NULL)) != NULL)
   1570 		sc->rtk_ldata.rtk_tx_list[*idx].rtk_vlanctl =
   1571 		    htole32(htons(*(u_int *)(mtag + 1)) |
   1572 		    RTK_TDESC_VLANCTL_TAG);
   1573 
   1574 	/* Transfer ownership of packet to the chip. */
   1575 
   1576 	sc->rtk_ldata.rtk_tx_list[curidx].rtk_cmdstat |=
   1577 	    htole32(RTK_TDESC_CMD_OWN);
   1578 	if (*idx != curidx)
   1579 		sc->rtk_ldata.rtk_tx_list[*idx].rtk_cmdstat |=
   1580 		    htole32(RTK_TDESC_CMD_OWN);
   1581 
   1582 	RTK_DESC_INC(curidx);
   1583 	*idx = curidx;
   1584 
   1585 	return 0;
   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_head = NULL;
   1597 	int			idx;
   1598 
   1599 	sc = ifp->if_softc;
   1600 
   1601 	idx = sc->rtk_ldata.rtk_tx_prodidx;
   1602 	while (sc->rtk_ldata.rtk_tx_mbuf[idx] == NULL) {
   1603 		IF_DEQUEUE(&ifp->if_snd, m_head);
   1604 		if (m_head == NULL)
   1605 			break;
   1606 
   1607 		if (re_encap(sc, m_head, &idx)) {
   1608 			IF_PREPEND(&ifp->if_snd, m_head);
   1609 			ifp->if_flags |= IFF_OACTIVE;
   1610 			break;
   1611 		}
   1612 #if NBPFILTER > 0
   1613 		/*
   1614 		 * If there's a BPF listener, bounce a copy of this frame
   1615 		 * to him.
   1616 		 */
   1617 		if (ifp->if_bpf)
   1618 			bpf_mtap(ifp->if_bpf, m_head);
   1619 #endif
   1620 	}
   1621 
   1622 	/* Flush the TX descriptors */
   1623 
   1624 	bus_dmamap_sync(sc->sc_dmat,
   1625 	    sc->rtk_ldata.rtk_tx_list_map,
   1626 	    0, sc->rtk_ldata.rtk_tx_list_map->dm_mapsize,
   1627 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1628 
   1629 	sc->rtk_ldata.rtk_tx_prodidx = idx;
   1630 
   1631 	/*
   1632 	 * RealTek put the TX poll request register in a different
   1633 	 * location on the 8169 gigE chip. I don't know why.
   1634 	 */
   1635 
   1636 	if (sc->rtk_type == RTK_8169)
   1637 		CSR_WRITE_2(sc, RTK_GTXSTART, RTK_TXSTART_START);
   1638 	else
   1639 		CSR_WRITE_2(sc, RTK_TXSTART, RTK_TXSTART_START);
   1640 
   1641 	/*
   1642 	 * Use the countdown timer for interrupt moderation.
   1643 	 * 'TX done' interrupts are disabled. Instead, we reset the
   1644 	 * countdown timer, which will begin counting until it hits
   1645 	 * the value in the TIMERINT register, and then trigger an
   1646 	 * interrupt. Each time we write to the TIMERCNT register,
   1647 	 * the timer count is reset to 0.
   1648 	 */
   1649 	CSR_WRITE_4(sc, RTK_TIMERCNT, 1);
   1650 
   1651 	/*
   1652 	 * Set a timeout in case the chip goes out to lunch.
   1653 	 */
   1654 	ifp->if_timer = 5;
   1655 
   1656 	return;
   1657 }
   1658 
   1659 static int
   1660 re_init(struct ifnet *ifp)
   1661 {
   1662 	struct rtk_softc	*sc = ifp->if_softc;
   1663 	u_int32_t		rxcfg = 0;
   1664 	u_int32_t		reg;
   1665 	int error;
   1666 
   1667 	if ((error = re_enable(sc)) != 0)
   1668 		goto out;
   1669 
   1670 	/*
   1671 	 * Cancel pending I/O and free all RX/TX buffers.
   1672 	 */
   1673 	re_stop(ifp, 0);
   1674 
   1675 	/*
   1676 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
   1677 	 * RX checksum offload. We must configure the C+ register
   1678 	 * before all others.
   1679 	 */
   1680 	reg = 0;
   1681 
   1682 	/*
   1683 	 * XXX: Realtek docs say bits 0 and 1 are reserved, for 8169S/8110S.
   1684 	 * FreeBSD  drivers set these bits anyway (for 8139C+?).
   1685 	 * So far, it works.
   1686 	 */
   1687 
   1688 	/*
   1689 	 * XXX: For 8169 and 8196S revs below 2, set bit 14.
   1690 	 * For 8169S/8110S rev 2 and above, do not set bit 14.
   1691 	 */
   1692 	if (sc->rtk_type == RTK_8169 && sc->sc_rev == 1)
   1693 		reg |= (0x1 << 14) | RTK_CPLUSCMD_PCI_MRW;;
   1694 
   1695 	if (1)  {/* not for 8169S ? */
   1696 		reg |= RTK_CPLUSCMD_VLANSTRIP |
   1697 		    (ifp->if_capenable &
   1698 		    (IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4) ?
   1699 		    RTK_CPLUSCMD_RXCSUM_ENB : 0);
   1700 	}
   1701 
   1702 	CSR_WRITE_2(sc, RTK_CPLUS_CMD,
   1703 	    reg | RTK_CPLUSCMD_RXENB | RTK_CPLUSCMD_TXENB);
   1704 
   1705 	/* XXX: from Realtek-supplied Linux driver. Wholly undocumented. */
   1706 	if (sc->rtk_type == RTK_8169)
   1707 		CSR_WRITE_2(sc, RTK_CPLUS_CMD+0x2, 0x0000);
   1708 
   1709 	DELAY(10000);
   1710 
   1711 	/*
   1712 	 * Init our MAC address.  Even though the chipset
   1713 	 * documentation doesn't mention it, we need to enter "Config
   1714 	 * register write enable" mode to modify the ID registers.
   1715 	 */
   1716 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_WRITECFG);
   1717 	memcpy(&reg, LLADDR(ifp->if_sadl), 4);
   1718 	CSR_WRITE_STREAM_4(sc, RTK_IDR0, reg);
   1719 	reg = 0;
   1720 	memcpy(&reg, LLADDR(ifp->if_sadl) + 4, 4);
   1721 	CSR_WRITE_STREAM_4(sc, RTK_IDR4, reg);
   1722 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
   1723 
   1724 	/*
   1725 	 * For C+ mode, initialize the RX descriptors and mbufs.
   1726 	 */
   1727 	re_rx_list_init(sc);
   1728 	re_tx_list_init(sc);
   1729 
   1730 	/*
   1731 	 * Enable transmit and receive.
   1732 	 */
   1733 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   1734 
   1735 	/*
   1736 	 * Set the initial TX and RX configuration.
   1737 	 */
   1738 	if (sc->rtk_testmode) {
   1739 		if (sc->rtk_type == RTK_8169)
   1740 			CSR_WRITE_4(sc, RTK_TXCFG,
   1741 			    RTK_TXCFG_CONFIG | RTK_LOOPTEST_ON);
   1742 		else
   1743 			CSR_WRITE_4(sc, RTK_TXCFG,
   1744 			    RTK_TXCFG_CONFIG | RTK_LOOPTEST_ON_CPLUS);
   1745 	} else
   1746 		CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1747 	CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1748 
   1749 	/* Set the individual bit to receive frames for this host only. */
   1750 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1751 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1752 
   1753 	/* If we want promiscuous mode, set the allframes bit. */
   1754 	if (ifp->if_flags & IFF_PROMISC) {
   1755 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1756 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1757 	} else {
   1758 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1759 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1760 	}
   1761 
   1762 	/*
   1763 	 * Set capture broadcast bit to capture broadcast frames.
   1764 	 */
   1765 	if (ifp->if_flags & IFF_BROADCAST) {
   1766 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1767 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1768 	} else {
   1769 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1770 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1771 	}
   1772 
   1773 	/*
   1774 	 * Program the multicast filter, if necessary.
   1775 	 */
   1776 	rtk_setmulti(sc);
   1777 
   1778 #ifdef DEVICE_POLLING
   1779 	/*
   1780 	 * Disable interrupts if we are polling.
   1781 	 */
   1782 	if (ifp->if_flags & IFF_POLLING)
   1783 		CSR_WRITE_2(sc, RTK_IMR, 0);
   1784 	else	/* otherwise ... */
   1785 #endif /* DEVICE_POLLING */
   1786 	/*
   1787 	 * Enable interrupts.
   1788 	 */
   1789 	if (sc->rtk_testmode)
   1790 		CSR_WRITE_2(sc, RTK_IMR, 0);
   1791 	else
   1792 		CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS_CPLUS);
   1793 
   1794 	/* Start RX/TX process. */
   1795 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   1796 #ifdef notdef
   1797 	/* Enable receiver and transmitter. */
   1798 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   1799 #endif
   1800 	/*
   1801 	 * Load the addresses of the RX and TX lists into the chip.
   1802 	 */
   1803 
   1804 	CSR_WRITE_4(sc, RTK_RXLIST_ADDR_HI,
   1805 	    RTK_ADDR_HI(sc->rtk_ldata.rtk_rx_listseg.ds_addr));
   1806 	CSR_WRITE_4(sc, RTK_RXLIST_ADDR_LO,
   1807 	    RTK_ADDR_LO(sc->rtk_ldata.rtk_rx_listseg.ds_addr));
   1808 
   1809 	CSR_WRITE_4(sc, RTK_TXLIST_ADDR_HI,
   1810 	    RTK_ADDR_HI(sc->rtk_ldata.rtk_tx_listseg.ds_addr));
   1811 	CSR_WRITE_4(sc, RTK_TXLIST_ADDR_LO,
   1812 	    RTK_ADDR_LO(sc->rtk_ldata.rtk_tx_listseg.ds_addr));
   1813 
   1814 	CSR_WRITE_1(sc, RTK_EARLY_TX_THRESH, 16);
   1815 
   1816 	/*
   1817 	 * Initialize the timer interrupt register so that
   1818 	 * a timer interrupt will be generated once the timer
   1819 	 * reaches a certain number of ticks. The timer is
   1820 	 * reloaded on each transmit. This gives us TX interrupt
   1821 	 * moderation, which dramatically improves TX frame rate.
   1822 	 */
   1823 
   1824 	if (sc->rtk_type == RTK_8169)
   1825 		CSR_WRITE_4(sc, RTK_TIMERINT_8169, 0x800);
   1826 	else
   1827 		CSR_WRITE_4(sc, RTK_TIMERINT, 0x400);
   1828 
   1829 	/*
   1830 	 * For 8169 gigE NICs, set the max allowed RX packet
   1831 	 * size so we can receive jumbo frames.
   1832 	 */
   1833 	if (sc->rtk_type == RTK_8169)
   1834 		CSR_WRITE_2(sc, RTK_MAXRXPKTLEN, 16383);
   1835 
   1836 	if (sc->rtk_testmode)
   1837 		return 0;
   1838 
   1839 	mii_mediachg(&sc->mii);
   1840 
   1841 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD | RTK_CFG1_FULLDUPLEX);
   1842 
   1843 	ifp->if_flags |= IFF_RUNNING;
   1844 	ifp->if_flags &= ~IFF_OACTIVE;
   1845 
   1846 	callout_reset(&sc->rtk_tick_ch, hz, re_tick, sc);
   1847 
   1848 out:
   1849 	if (error) {
   1850 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1851 		ifp->if_timer = 0;
   1852 		aprint_error("%s: interface not running\n",
   1853 		    sc->sc_dev.dv_xname);
   1854 	}
   1855 
   1856 	return error;
   1857 
   1858 }
   1859 
   1860 /*
   1861  * Set media options.
   1862  */
   1863 static int
   1864 re_ifmedia_upd(struct ifnet *ifp)
   1865 {
   1866 	struct rtk_softc	*sc;
   1867 
   1868 	sc = ifp->if_softc;
   1869 
   1870 	return mii_mediachg(&sc->mii);
   1871 }
   1872 
   1873 /*
   1874  * Report current media status.
   1875  */
   1876 static void
   1877 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1878 {
   1879 	struct rtk_softc	*sc;
   1880 
   1881 	sc = ifp->if_softc;
   1882 
   1883 	mii_pollstat(&sc->mii);
   1884 	ifmr->ifm_active = sc->mii.mii_media_active;
   1885 	ifmr->ifm_status = sc->mii.mii_media_status;
   1886 
   1887 	return;
   1888 }
   1889 
   1890 static int
   1891 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1892 {
   1893 	struct rtk_softc	*sc = ifp->if_softc;
   1894 	struct ifreq		*ifr = (struct ifreq *) data;
   1895 	int			s, error = 0;
   1896 
   1897 	s = splnet();
   1898 
   1899 	switch (command) {
   1900 	case SIOCSIFMTU:
   1901 		if (ifr->ifr_mtu > RTK_JUMBO_MTU)
   1902 			error = EINVAL;
   1903 		ifp->if_mtu = ifr->ifr_mtu;
   1904 		break;
   1905 	case SIOCGIFMEDIA:
   1906 	case SIOCSIFMEDIA:
   1907 		error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
   1908 		break;
   1909 	default:
   1910 		error = ether_ioctl(ifp, command, data);
   1911 		if (error == ENETRESET) {
   1912 			if (ifp->if_flags & IFF_RUNNING)
   1913 				rtk_setmulti(sc);
   1914 			error = 0;
   1915 		}
   1916 		break;
   1917 	}
   1918 
   1919 	splx(s);
   1920 
   1921 	return error;
   1922 }
   1923 
   1924 static void
   1925 re_watchdog(struct ifnet *ifp)
   1926 {
   1927 	struct rtk_softc	*sc;
   1928 	int			s;
   1929 
   1930 	sc = ifp->if_softc;
   1931 	s = splnet();
   1932 	aprint_error("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
   1933 	ifp->if_oerrors++;
   1934 
   1935 	re_txeof(sc);
   1936 	re_rxeof(sc);
   1937 
   1938 	re_init(ifp);
   1939 
   1940 	splx(s);
   1941 }
   1942 
   1943 /*
   1944  * Stop the adapter and free any mbufs allocated to the
   1945  * RX and TX lists.
   1946  */
   1947 static void
   1948 re_stop(struct ifnet *ifp, int disable)
   1949 {
   1950 	register int		i;
   1951 	struct rtk_softc *sc = ifp->if_softc;
   1952 
   1953 	callout_stop(&sc->rtk_tick_ch);
   1954 
   1955 #ifdef DEVICE_POLLING
   1956 	ether_poll_deregister(ifp);
   1957 #endif /* DEVICE_POLLING */
   1958 
   1959 	mii_down(&sc->mii);
   1960 
   1961 	CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   1962 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1963 
   1964 	if (sc->rtk_head != NULL) {
   1965 		m_freem(sc->rtk_head);
   1966 		sc->rtk_head = sc->rtk_tail = NULL;
   1967 	}
   1968 
   1969 	/* Free the TX list buffers. */
   1970 	for (i = 0; i < RTK_TX_DESC_CNT; i++) {
   1971 		if (sc->rtk_ldata.rtk_tx_mbuf[i] != NULL) {
   1972 			bus_dmamap_unload(sc->sc_dmat,
   1973 			    sc->rtk_ldata.rtk_tx_dmamap[i]);
   1974 			m_freem(sc->rtk_ldata.rtk_tx_mbuf[i]);
   1975 			sc->rtk_ldata.rtk_tx_mbuf[i] = NULL;
   1976 		}
   1977 	}
   1978 
   1979 	/* Free the RX list buffers. */
   1980 	for (i = 0; i < RTK_RX_DESC_CNT; i++) {
   1981 		if (sc->rtk_ldata.rtk_rx_mbuf[i] != NULL) {
   1982 			bus_dmamap_unload(sc->sc_dmat,
   1983 			    sc->rtk_ldata.rtk_rx_dmamap[i]);
   1984 			m_freem(sc->rtk_ldata.rtk_rx_mbuf[i]);
   1985 			sc->rtk_ldata.rtk_rx_mbuf[i] = NULL;
   1986 		}
   1987 	}
   1988 
   1989 	if (disable)
   1990 		re_disable(sc);
   1991 
   1992 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1993 	ifp->if_timer = 0;
   1994 
   1995 	return;
   1996 }
   1997