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