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