Home | History | Annotate | Line # | Download | only in ic
rtl81x9.c revision 1.80
      1 /*	$NetBSD: rtl81x9.c,v 1.80 2008/01/08 15:50:36 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  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  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35  */
     36 
     37 /*
     38  * RealTek 8129/8139 PCI NIC driver
     39  *
     40  * Supports several extremely cheap PCI 10/100 adapters based on
     41  * the RealTek chipset. Datasheets can be obtained from
     42  * www.realtek.com.tw.
     43  *
     44  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45  * Electrical Engineering Department
     46  * Columbia University, New York City
     47  */
     48 
     49 /*
     50  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
     51  * probably the worst PCI ethernet controller ever made, with the possible
     52  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
     53  * DMA, but it has a terrible interface that nullifies any performance
     54  * gains that bus-master DMA usually offers.
     55  *
     56  * For transmission, the chip offers a series of four TX descriptor
     57  * registers. Each transmit frame must be in a contiguous buffer, aligned
     58  * on a longword (32-bit) boundary. This means we almost always have to
     59  * do mbuf copies in order to transmit a frame, except in the unlikely
     60  * case where a) the packet fits into a single mbuf, and b) the packet
     61  * is 32-bit aligned within the mbuf's data area. The presence of only
     62  * four descriptor registers means that we can never have more than four
     63  * packets queued for transmission at any one time.
     64  *
     65  * Reception is not much better. The driver has to allocate a single large
     66  * buffer area (up to 64K in size) into which the chip will DMA received
     67  * frames. Because we don't know where within this region received packets
     68  * will begin or end, we have no choice but to copy data from the buffer
     69  * area into mbufs in order to pass the packets up to the higher protocol
     70  * levels.
     71  *
     72  * It's impossible given this rotten design to really achieve decent
     73  * performance at 100Mbps, unless you happen to have a 400MHz PII or
     74  * some equally overmuscled CPU to drive it.
     75  *
     76  * On the bright side, the 8139 does have a built-in PHY, although
     77  * rather than using an MDIO serial interface like most other NICs, the
     78  * PHY registers are directly accessible through the 8139's register
     79  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
     80  * filter.
     81  *
     82  * The 8129 chip is an older version of the 8139 that uses an external PHY
     83  * chip. The 8129 has a serial MDIO interface for accessing the MII where
     84  * the 8139 lets you directly access the on-board PHY registers. We need
     85  * to select which interface to use depending on the chip type.
     86  */
     87 
     88 #include <sys/cdefs.h>
     89 __KERNEL_RCSID(0, "$NetBSD: rtl81x9.c,v 1.80 2008/01/08 15:50:36 joerg Exp $");
     90 
     91 #include "bpfilter.h"
     92 #include "rnd.h"
     93 
     94 #include <sys/param.h>
     95 #include <sys/systm.h>
     96 #include <sys/callout.h>
     97 #include <sys/device.h>
     98 #include <sys/sockio.h>
     99 #include <sys/mbuf.h>
    100 #include <sys/malloc.h>
    101 #include <sys/kernel.h>
    102 #include <sys/socket.h>
    103 
    104 #include <uvm/uvm_extern.h>
    105 
    106 #include <net/if.h>
    107 #include <net/if_arp.h>
    108 #include <net/if_ether.h>
    109 #include <net/if_dl.h>
    110 #include <net/if_media.h>
    111 
    112 #if NBPFILTER > 0
    113 #include <net/bpf.h>
    114 #endif
    115 #if NRND > 0
    116 #include <sys/rnd.h>
    117 #endif
    118 
    119 #include <sys/bus.h>
    120 #include <machine/endian.h>
    121 
    122 #include <dev/mii/mii.h>
    123 #include <dev/mii/miivar.h>
    124 
    125 #include <dev/ic/rtl81x9reg.h>
    126 #include <dev/ic/rtl81x9var.h>
    127 
    128 #if defined(DEBUG)
    129 #define STATIC
    130 #else
    131 #define STATIC static
    132 #endif
    133 
    134 STATIC void rtk_reset(struct rtk_softc *);
    135 STATIC void rtk_rxeof(struct rtk_softc *);
    136 STATIC void rtk_txeof(struct rtk_softc *);
    137 STATIC void rtk_start(struct ifnet *);
    138 STATIC int rtk_ioctl(struct ifnet *, u_long, void *);
    139 STATIC int rtk_init(struct ifnet *);
    140 STATIC void rtk_stop(struct ifnet *, int);
    141 
    142 STATIC void rtk_watchdog(struct ifnet *);
    143 STATIC int rtk_ifmedia_upd(struct ifnet *);
    144 STATIC void rtk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    145 
    146 STATIC void rtk_eeprom_putbyte(struct rtk_softc *, int, int);
    147 STATIC void rtk_mii_sync(struct rtk_softc *);
    148 STATIC void rtk_mii_send(struct rtk_softc *, uint32_t, int);
    149 STATIC int rtk_mii_readreg(struct rtk_softc *, struct rtk_mii_frame *);
    150 STATIC int rtk_mii_writereg(struct rtk_softc *, struct rtk_mii_frame *);
    151 
    152 STATIC int rtk_phy_readreg(device_t, int, int);
    153 STATIC void rtk_phy_writereg(device_t, int, int, int);
    154 STATIC void rtk_phy_statchg(device_t);
    155 STATIC void rtk_tick(void *);
    156 
    157 STATIC int rtk_enable(struct rtk_softc *);
    158 STATIC void rtk_disable(struct rtk_softc *);
    159 
    160 STATIC void rtk_list_tx_init(struct rtk_softc *);
    161 
    162 #define EE_SET(x)					\
    163 	CSR_WRITE_1(sc, RTK_EECMD,			\
    164 		CSR_READ_1(sc, RTK_EECMD) | (x))
    165 
    166 #define EE_CLR(x)					\
    167 	CSR_WRITE_1(sc, RTK_EECMD,			\
    168 		CSR_READ_1(sc, RTK_EECMD) & ~(x))
    169 
    170 #define EE_DELAY()	DELAY(100)
    171 
    172 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
    173 
    174 /*
    175  * Send a read command and address to the EEPROM, check for ACK.
    176  */
    177 STATIC void
    178 rtk_eeprom_putbyte(struct rtk_softc *sc, int addr, int addr_len)
    179 {
    180 	int d, i;
    181 
    182 	d = (RTK_EECMD_READ << addr_len) | addr;
    183 
    184 	/*
    185 	 * Feed in each bit and stobe the clock.
    186 	 */
    187 	for (i = RTK_EECMD_LEN + addr_len; i > 0; i--) {
    188 		if (d & (1 << (i - 1))) {
    189 			EE_SET(RTK_EE_DATAIN);
    190 		} else {
    191 			EE_CLR(RTK_EE_DATAIN);
    192 		}
    193 		EE_DELAY();
    194 		EE_SET(RTK_EE_CLK);
    195 		EE_DELAY();
    196 		EE_CLR(RTK_EE_CLK);
    197 		EE_DELAY();
    198 	}
    199 }
    200 
    201 /*
    202  * Read a word of data stored in the EEPROM at address 'addr.'
    203  */
    204 uint16_t
    205 rtk_read_eeprom(struct rtk_softc *sc, int addr, int addr_len)
    206 {
    207 	uint16_t word;
    208 	int i;
    209 
    210 	/* Enter EEPROM access mode. */
    211 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM);
    212 	EE_DELAY();
    213 	EE_SET(RTK_EE_SEL);
    214 
    215 	/*
    216 	 * Send address of word we want to read.
    217 	 */
    218 	rtk_eeprom_putbyte(sc, addr, addr_len);
    219 
    220 	/*
    221 	 * Start reading bits from EEPROM.
    222 	 */
    223 	word = 0;
    224 	for (i = 16; i > 0; i--) {
    225 		EE_SET(RTK_EE_CLK);
    226 		EE_DELAY();
    227 		if (CSR_READ_1(sc, RTK_EECMD) & RTK_EE_DATAOUT)
    228 			word |= 1 << (i - 1);
    229 		EE_CLR(RTK_EE_CLK);
    230 		EE_DELAY();
    231 	}
    232 
    233 	/* Turn off EEPROM access mode. */
    234 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
    235 
    236 	return word;
    237 }
    238 
    239 /*
    240  * MII access routines are provided for the 8129, which
    241  * doesn't have a built-in PHY. For the 8139, we fake things
    242  * up by diverting rtk_phy_readreg()/rtk_phy_writereg() to the
    243  * direct access PHY registers.
    244  */
    245 #define MII_SET(x)					\
    246 	CSR_WRITE_1(sc, RTK_MII,			\
    247 		CSR_READ_1(sc, RTK_MII) | (x))
    248 
    249 #define MII_CLR(x)					\
    250 	CSR_WRITE_1(sc, RTK_MII,			\
    251 		CSR_READ_1(sc, RTK_MII) & ~(x))
    252 
    253 /*
    254  * Sync the PHYs by setting data bit and strobing the clock 32 times.
    255  */
    256 STATIC void
    257 rtk_mii_sync(struct rtk_softc *sc)
    258 {
    259 	int i;
    260 
    261 	MII_SET(RTK_MII_DIR|RTK_MII_DATAOUT);
    262 
    263 	for (i = 0; i < 32; i++) {
    264 		MII_SET(RTK_MII_CLK);
    265 		DELAY(1);
    266 		MII_CLR(RTK_MII_CLK);
    267 		DELAY(1);
    268 	}
    269 }
    270 
    271 /*
    272  * Clock a series of bits through the MII.
    273  */
    274 STATIC void
    275 rtk_mii_send(struct rtk_softc *sc, uint32_t bits, int cnt)
    276 {
    277 	int i;
    278 
    279 	MII_CLR(RTK_MII_CLK);
    280 
    281 	for (i = cnt; i > 0; i--) {
    282 		if (bits & (1 << (i - 1))) {
    283 			MII_SET(RTK_MII_DATAOUT);
    284 		} else {
    285 			MII_CLR(RTK_MII_DATAOUT);
    286 		}
    287 		DELAY(1);
    288 		MII_CLR(RTK_MII_CLK);
    289 		DELAY(1);
    290 		MII_SET(RTK_MII_CLK);
    291 	}
    292 }
    293 
    294 /*
    295  * Read an PHY register through the MII.
    296  */
    297 STATIC int
    298 rtk_mii_readreg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
    299 {
    300 	int i, ack, s;
    301 
    302 	s = splnet();
    303 
    304 	/*
    305 	 * Set up frame for RX.
    306 	 */
    307 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    308 	frame->mii_opcode = RTK_MII_READOP;
    309 	frame->mii_turnaround = 0;
    310 	frame->mii_data = 0;
    311 
    312 	CSR_WRITE_2(sc, RTK_MII, 0);
    313 
    314 	/*
    315 	 * Turn on data xmit.
    316 	 */
    317 	MII_SET(RTK_MII_DIR);
    318 
    319 	rtk_mii_sync(sc);
    320 
    321 	/*
    322 	 * Send command/address info.
    323 	 */
    324 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    325 	rtk_mii_send(sc, frame->mii_opcode, 2);
    326 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    327 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    328 
    329 	/* Idle bit */
    330 	MII_CLR((RTK_MII_CLK|RTK_MII_DATAOUT));
    331 	DELAY(1);
    332 	MII_SET(RTK_MII_CLK);
    333 	DELAY(1);
    334 
    335 	/* Turn off xmit. */
    336 	MII_CLR(RTK_MII_DIR);
    337 
    338 	/* Check for ack */
    339 	MII_CLR(RTK_MII_CLK);
    340 	DELAY(1);
    341 	ack = CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN;
    342 	MII_SET(RTK_MII_CLK);
    343 	DELAY(1);
    344 
    345 	/*
    346 	 * Now try reading data bits. If the ack failed, we still
    347 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
    348 	 */
    349 	if (ack) {
    350 		for (i = 0; i < 16; i++) {
    351 			MII_CLR(RTK_MII_CLK);
    352 			DELAY(1);
    353 			MII_SET(RTK_MII_CLK);
    354 			DELAY(1);
    355 		}
    356 		goto fail;
    357 	}
    358 
    359 	for (i = 16; i > 0; i--) {
    360 		MII_CLR(RTK_MII_CLK);
    361 		DELAY(1);
    362 		if (!ack) {
    363 			if (CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN)
    364 				frame->mii_data |= 1 << (i - 1);
    365 			DELAY(1);
    366 		}
    367 		MII_SET(RTK_MII_CLK);
    368 		DELAY(1);
    369 	}
    370 
    371  fail:
    372 	MII_CLR(RTK_MII_CLK);
    373 	DELAY(1);
    374 	MII_SET(RTK_MII_CLK);
    375 	DELAY(1);
    376 
    377 	splx(s);
    378 
    379 	if (ack)
    380 		return 1;
    381 	return 0;
    382 }
    383 
    384 /*
    385  * Write to a PHY register through the MII.
    386  */
    387 STATIC int
    388 rtk_mii_writereg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
    389 {
    390 	int s;
    391 
    392 	s = splnet();
    393 	/*
    394 	 * Set up frame for TX.
    395 	 */
    396 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    397 	frame->mii_opcode = RTK_MII_WRITEOP;
    398 	frame->mii_turnaround = RTK_MII_TURNAROUND;
    399 
    400 	/*
    401 	 * Turn on data output.
    402 	 */
    403 	MII_SET(RTK_MII_DIR);
    404 
    405 	rtk_mii_sync(sc);
    406 
    407 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    408 	rtk_mii_send(sc, frame->mii_opcode, 2);
    409 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    410 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    411 	rtk_mii_send(sc, frame->mii_turnaround, 2);
    412 	rtk_mii_send(sc, frame->mii_data, 16);
    413 
    414 	/* Idle bit. */
    415 	MII_SET(RTK_MII_CLK);
    416 	DELAY(1);
    417 	MII_CLR(RTK_MII_CLK);
    418 	DELAY(1);
    419 
    420 	/*
    421 	 * Turn off xmit.
    422 	 */
    423 	MII_CLR(RTK_MII_DIR);
    424 
    425 	splx(s);
    426 
    427 	return 0;
    428 }
    429 
    430 STATIC int
    431 rtk_phy_readreg(device_t self, int phy, int reg)
    432 {
    433 	struct rtk_softc *sc = device_private(self);
    434 	struct rtk_mii_frame frame;
    435 	int rval;
    436 	int rtk8139_reg;
    437 
    438 	if ((sc->sc_quirk & RTKQ_8129) == 0) {
    439 		if (phy != 7)
    440 			return 0;
    441 
    442 		switch (reg) {
    443 		case MII_BMCR:
    444 			rtk8139_reg = RTK_BMCR;
    445 			break;
    446 		case MII_BMSR:
    447 			rtk8139_reg = RTK_BMSR;
    448 			break;
    449 		case MII_ANAR:
    450 			rtk8139_reg = RTK_ANAR;
    451 			break;
    452 		case MII_ANER:
    453 			rtk8139_reg = RTK_ANER;
    454 			break;
    455 		case MII_ANLPAR:
    456 			rtk8139_reg = RTK_LPAR;
    457 			break;
    458 		default:
    459 #if 0
    460 			printf("%s: bad phy register\n", device_xname(self));
    461 #endif
    462 			return 0;
    463 		}
    464 		rval = CSR_READ_2(sc, rtk8139_reg);
    465 		return rval;
    466 	}
    467 
    468 	memset((char *)&frame, 0, sizeof(frame));
    469 
    470 	frame.mii_phyaddr = phy;
    471 	frame.mii_regaddr = reg;
    472 	rtk_mii_readreg(sc, &frame);
    473 
    474 	return frame.mii_data;
    475 }
    476 
    477 STATIC void
    478 rtk_phy_writereg(device_t self, int phy, int reg, int data)
    479 {
    480 	struct rtk_softc *sc = device_private(self);
    481 	struct rtk_mii_frame frame;
    482 	int rtk8139_reg;
    483 
    484 	if ((sc->sc_quirk & RTKQ_8129) == 0) {
    485 		if (phy != 7)
    486 			return;
    487 
    488 		switch (reg) {
    489 		case MII_BMCR:
    490 			rtk8139_reg = RTK_BMCR;
    491 			break;
    492 		case MII_BMSR:
    493 			rtk8139_reg = RTK_BMSR;
    494 			break;
    495 		case MII_ANAR:
    496 			rtk8139_reg = RTK_ANAR;
    497 			break;
    498 		case MII_ANER:
    499 			rtk8139_reg = RTK_ANER;
    500 			break;
    501 		case MII_ANLPAR:
    502 			rtk8139_reg = RTK_LPAR;
    503 			break;
    504 		default:
    505 #if 0
    506 			printf("%s: bad phy register\n", device_xname(self));
    507 #endif
    508 			return;
    509 		}
    510 		CSR_WRITE_2(sc, rtk8139_reg, data);
    511 		return;
    512 	}
    513 
    514 	memset((char *)&frame, 0, sizeof(frame));
    515 
    516 	frame.mii_phyaddr = phy;
    517 	frame.mii_regaddr = reg;
    518 	frame.mii_data = data;
    519 
    520 	rtk_mii_writereg(sc, &frame);
    521 }
    522 
    523 STATIC void
    524 rtk_phy_statchg(device_t v)
    525 {
    526 
    527 	/* Nothing to do. */
    528 }
    529 
    530 #define	rtk_calchash(addr) \
    531 	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
    532 
    533 /*
    534  * Program the 64-bit multicast hash filter.
    535  */
    536 void
    537 rtk_setmulti(struct rtk_softc *sc)
    538 {
    539 	struct ifnet *ifp;
    540 	uint32_t hashes[2] = { 0, 0 };
    541 	uint32_t rxfilt;
    542 	struct ether_multi *enm;
    543 	struct ether_multistep step;
    544 	int h, mcnt;
    545 
    546 	ifp = &sc->ethercom.ec_if;
    547 
    548 	rxfilt = CSR_READ_4(sc, RTK_RXCFG);
    549 
    550 	if (ifp->if_flags & IFF_PROMISC) {
    551  allmulti:
    552 		ifp->if_flags |= IFF_ALLMULTI;
    553 		rxfilt |= RTK_RXCFG_RX_MULTI;
    554 		CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    555 		CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
    556 		CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
    557 		return;
    558 	}
    559 
    560 	/* first, zot all the existing hash bits */
    561 	CSR_WRITE_4(sc, RTK_MAR0, 0);
    562 	CSR_WRITE_4(sc, RTK_MAR4, 0);
    563 
    564 	/* now program new ones */
    565 	ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
    566 	mcnt = 0;
    567 	while (enm != NULL) {
    568 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    569 		    ETHER_ADDR_LEN) != 0)
    570 			goto allmulti;
    571 
    572 		h = rtk_calchash(enm->enm_addrlo);
    573 		if (h < 32)
    574 			hashes[0] |= (1 << h);
    575 		else
    576 			hashes[1] |= (1 << (h - 32));
    577 		mcnt++;
    578 		ETHER_NEXT_MULTI(step, enm);
    579 	}
    580 
    581 	ifp->if_flags &= ~IFF_ALLMULTI;
    582 
    583 	if (mcnt)
    584 		rxfilt |= RTK_RXCFG_RX_MULTI;
    585 	else
    586 		rxfilt &= ~RTK_RXCFG_RX_MULTI;
    587 
    588 	CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    589 
    590 	/*
    591 	 * For some unfathomable reason, RealTek decided to reverse
    592 	 * the order of the multicast hash registers in the PCI Express
    593 	 * parts. This means we have to write the hash pattern in reverse
    594 	 * order for those devices.
    595 	 */
    596 	if ((sc->sc_quirk & RTKQ_PCIE) != 0) {
    597 		CSR_WRITE_4(sc, RTK_MAR0, bswap32(hashes[1]));
    598 		CSR_WRITE_4(sc, RTK_MAR4, bswap32(hashes[0]));
    599 	} else {
    600 		CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
    601 		CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
    602 	}
    603 }
    604 
    605 void
    606 rtk_reset(struct rtk_softc *sc)
    607 {
    608 	int i;
    609 
    610 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
    611 
    612 	for (i = 0; i < RTK_TIMEOUT; i++) {
    613 		DELAY(10);
    614 		if ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET) == 0)
    615 			break;
    616 	}
    617 	if (i == RTK_TIMEOUT)
    618 		printf("%s: reset never completed!\n", device_xname(&sc->sc_dev));
    619 }
    620 
    621 /*
    622  * Attach the interface. Allocate softc structures, do ifmedia
    623  * setup and ethernet/BPF attach.
    624  */
    625 void
    626 rtk_attach(struct rtk_softc *sc)
    627 {
    628 	device_t self = &sc->sc_dev;
    629 	struct ifnet *ifp;
    630 	struct rtk_tx_desc *txd;
    631 	uint16_t val;
    632 	uint8_t eaddr[ETHER_ADDR_LEN];
    633 	int error;
    634 	int i, addr_len;
    635 
    636 	callout_init(&sc->rtk_tick_ch, 0);
    637 
    638 	/*
    639 	 * Check EEPROM type 9346 or 9356.
    640 	 */
    641 	if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
    642 		addr_len = RTK_EEADDR_LEN1;
    643 	else
    644 		addr_len = RTK_EEADDR_LEN0;
    645 
    646 	/*
    647 	 * Get station address.
    648 	 */
    649 	val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
    650 	eaddr[0] = val & 0xff;
    651 	eaddr[1] = val >> 8;
    652 	val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
    653 	eaddr[2] = val & 0xff;
    654 	eaddr[3] = val >> 8;
    655 	val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
    656 	eaddr[4] = val & 0xff;
    657 	eaddr[5] = val >> 8;
    658 
    659 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    660 	    RTK_RXBUFLEN + 16, PAGE_SIZE, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
    661 	    BUS_DMA_NOWAIT)) != 0) {
    662 		aprint_error_dev(self,
    663 			"can't allocate recv buffer, error = %d\n", error);
    664 		goto fail_0;
    665 	}
    666 
    667 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
    668 	    RTK_RXBUFLEN + 16, (void **)&sc->rtk_rx_buf,
    669 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    670 		aprint_error_dev(self,
    671 			"can't map recv buffer, error = %d\n", error);
    672 		goto fail_1;
    673 	}
    674 
    675 	if ((error = bus_dmamap_create(sc->sc_dmat,
    676 	    RTK_RXBUFLEN + 16, 1, RTK_RXBUFLEN + 16, 0, BUS_DMA_NOWAIT,
    677 	    &sc->recv_dmamap)) != 0) {
    678 		aprint_error_dev(self,
    679 			"can't create recv buffer DMA map, error = %d\n", error);
    680 		goto fail_2;
    681 	}
    682 
    683 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
    684 	    sc->rtk_rx_buf, RTK_RXBUFLEN + 16,
    685 	    NULL, BUS_DMA_READ|BUS_DMA_NOWAIT)) != 0) {
    686 		aprint_error_dev(self,
    687 			"can't load recv buffer DMA map, error = %d\n", error);
    688 		goto fail_3;
    689 	}
    690 
    691 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    692 		txd = &sc->rtk_tx_descs[i];
    693 		if ((error = bus_dmamap_create(sc->sc_dmat,
    694 		    MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    695 		    &txd->txd_dmamap)) != 0) {
    696 			aprint_error_dev(self,
    697 				"can't create snd buffer DMA map,"
    698 				" error = %d\n", error);
    699 			goto fail_4;
    700 		}
    701 		txd->txd_txaddr = RTK_TXADDR0 + (i * 4);
    702 		txd->txd_txstat = RTK_TXSTAT0 + (i * 4);
    703 	}
    704 	SIMPLEQ_INIT(&sc->rtk_tx_free);
    705 	SIMPLEQ_INIT(&sc->rtk_tx_dirty);
    706 
    707 	/*
    708 	 * From this point forward, the attachment cannot fail. A failure
    709 	 * before this releases all resources thar may have been
    710 	 * allocated.
    711 	 */
    712 	sc->sc_flags |= RTK_ATTACHED;
    713 
    714 	/* Reset the adapter. */
    715 	rtk_reset(sc);
    716 
    717 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    718 
    719 	ifp = &sc->ethercom.ec_if;
    720 	ifp->if_softc = sc;
    721 	strcpy(ifp->if_xname, device_xname(self));
    722 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    723 	ifp->if_ioctl = rtk_ioctl;
    724 	ifp->if_start = rtk_start;
    725 	ifp->if_watchdog = rtk_watchdog;
    726 	ifp->if_init = rtk_init;
    727 	ifp->if_stop = rtk_stop;
    728 	IFQ_SET_READY(&ifp->if_snd);
    729 
    730 	/*
    731 	 * Do ifmedia setup.
    732 	 */
    733 	sc->mii.mii_ifp = ifp;
    734 	sc->mii.mii_readreg = rtk_phy_readreg;
    735 	sc->mii.mii_writereg = rtk_phy_writereg;
    736 	sc->mii.mii_statchg = rtk_phy_statchg;
    737 	ifmedia_init(&sc->mii.mii_media, IFM_IMASK, rtk_ifmedia_upd,
    738 	    rtk_ifmedia_sts);
    739 	mii_attach(self, &sc->mii, 0xffffffff,
    740 	    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    741 
    742 	/* Choose a default media. */
    743 	if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
    744 		ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    745 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
    746 	} else {
    747 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
    748 	}
    749 
    750 	/*
    751 	 * Call MI attach routines.
    752 	 */
    753 	if_attach(ifp);
    754 	ether_ifattach(ifp, eaddr);
    755 
    756 #if NRND > 0
    757 	rnd_attach_source(&sc->rnd_source, device_xname(self),
    758 	    RND_TYPE_NET, 0);
    759 #endif
    760 
    761 	return;
    762  fail_4:
    763 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    764 		txd = &sc->rtk_tx_descs[i];
    765 		if (txd->txd_dmamap != NULL)
    766 			bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
    767 	}
    768  fail_3:
    769 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    770  fail_2:
    771 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->rtk_rx_buf,
    772 	    RTK_RXBUFLEN + 16);
    773  fail_1:
    774 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    775  fail_0:
    776 	return;
    777 }
    778 
    779 /*
    780  * Initialize the transmit descriptors.
    781  */
    782 STATIC void
    783 rtk_list_tx_init(struct rtk_softc *sc)
    784 {
    785 	struct rtk_tx_desc *txd;
    786 	int i;
    787 
    788 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL)
    789 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
    790 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL)
    791 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
    792 
    793 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    794 		txd = &sc->rtk_tx_descs[i];
    795 		CSR_WRITE_4(sc, txd->txd_txaddr, 0);
    796 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
    797 	}
    798 }
    799 
    800 /*
    801  * rtk_activate:
    802  *     Handle device activation/deactivation requests.
    803  */
    804 int
    805 rtk_activate(device_t self, enum devact act)
    806 {
    807 	struct rtk_softc *sc = device_private(self);
    808 	int s, error;
    809 
    810 	error = 0;
    811 	s = splnet();
    812 	switch (act) {
    813 	case DVACT_ACTIVATE:
    814 		error = EOPNOTSUPP;
    815 		break;
    816 	case DVACT_DEACTIVATE:
    817 		mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
    818 		if_deactivate(&sc->ethercom.ec_if);
    819 		break;
    820 	}
    821 	splx(s);
    822 
    823 	return error;
    824 }
    825 
    826 /*
    827  * rtk_detach:
    828  *     Detach a rtk interface.
    829  */
    830 int
    831 rtk_detach(struct rtk_softc *sc)
    832 {
    833 	struct ifnet *ifp = &sc->ethercom.ec_if;
    834 	struct rtk_tx_desc *txd;
    835 	int i;
    836 
    837 	/*
    838 	 * Succeed now if there isn't any work to do.
    839 	 */
    840 	if ((sc->sc_flags & RTK_ATTACHED) == 0)
    841 		return 0;
    842 
    843 	/* Unhook our tick handler. */
    844 	callout_stop(&sc->rtk_tick_ch);
    845 
    846 	/* Detach all PHYs. */
    847 	mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
    848 
    849 	/* Delete all remaining media. */
    850 	ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
    851 
    852 #if NRND > 0
    853 	rnd_detach_source(&sc->rnd_source);
    854 #endif
    855 
    856 	ether_ifdetach(ifp);
    857 	if_detach(ifp);
    858 
    859 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    860 		txd = &sc->rtk_tx_descs[i];
    861 		if (txd->txd_dmamap != NULL)
    862 			bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
    863 	}
    864 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    865 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->rtk_rx_buf,
    866 	    RTK_RXBUFLEN + 16);
    867 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    868 
    869 	return 0;
    870 }
    871 
    872 /*
    873  * rtk_enable:
    874  *     Enable the RTL81X9 chip.
    875  */
    876 int
    877 rtk_enable(struct rtk_softc *sc)
    878 {
    879 
    880 	if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
    881 		if ((*sc->sc_enable)(sc) != 0) {
    882 			printf("%s: device enable failed\n",
    883 			    device_xname(&sc->sc_dev));
    884 			return EIO;
    885 		}
    886 		sc->sc_flags |= RTK_ENABLED;
    887 	}
    888 	return 0;
    889 }
    890 
    891 /*
    892  * rtk_disable:
    893  *     Disable the RTL81X9 chip.
    894  */
    895 void
    896 rtk_disable(struct rtk_softc *sc)
    897 {
    898 
    899 	if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
    900 		(*sc->sc_disable)(sc);
    901 		sc->sc_flags &= ~RTK_ENABLED;
    902 	}
    903 }
    904 
    905 /*
    906  * A frame has been uploaded: pass the resulting mbuf chain up to
    907  * the higher level protocols.
    908  *
    909  * You know there's something wrong with a PCI bus-master chip design.
    910  *
    911  * The receive operation is badly documented in the datasheet, so I'll
    912  * attempt to document it here. The driver provides a buffer area and
    913  * places its base address in the RX buffer start address register.
    914  * The chip then begins copying frames into the RX buffer. Each frame
    915  * is preceded by a 32-bit RX status word which specifies the length
    916  * of the frame and certain other status bits. Each frame (starting with
    917  * the status word) is also 32-bit aligned. The frame length is in the
    918  * first 16 bits of the status word; the lower 15 bits correspond with
    919  * the 'rx status register' mentioned in the datasheet.
    920  *
    921  * Note: to make the Alpha happy, the frame payload needs to be aligned
    922  * on a 32-bit boundary. To achieve this, we copy the data to mbuf
    923  * shifted forward 2 bytes.
    924  */
    925 STATIC void
    926 rtk_rxeof(struct rtk_softc *sc)
    927 {
    928 	struct mbuf *m;
    929 	struct ifnet *ifp;
    930 	char *rxbufpos, *dst;
    931 	u_int total_len, wrap;
    932 	uint32_t rxstat;
    933 	uint16_t cur_rx, new_rx;
    934 	uint16_t limit;
    935 	uint16_t rx_bytes, max_bytes;
    936 
    937 	ifp = &sc->ethercom.ec_if;
    938 
    939 	cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
    940 
    941 	/* Do not try to read past this point. */
    942 	limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
    943 
    944 	if (limit < cur_rx)
    945 		max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
    946 	else
    947 		max_bytes = limit - cur_rx;
    948 	rx_bytes = 0;
    949 
    950 	while ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
    951 		rxbufpos = (char *)sc->rtk_rx_buf + cur_rx;
    952 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
    953 		    RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
    954 		rxstat = le32toh(*(uint32_t *)rxbufpos);
    955 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
    956 		    RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
    957 
    958 		/*
    959 		 * Here's a totally undocumented fact for you. When the
    960 		 * RealTek chip is in the process of copying a packet into
    961 		 * RAM for you, the length will be 0xfff0. If you spot a
    962 		 * packet header with this value, you need to stop. The
    963 		 * datasheet makes absolutely no mention of this and
    964 		 * RealTek should be shot for this.
    965 		 */
    966 		total_len = rxstat >> 16;
    967 		if (total_len == RTK_RXSTAT_UNFINISHED)
    968 			break;
    969 
    970 		if ((rxstat & RTK_RXSTAT_RXOK) == 0 ||
    971 		    total_len < ETHER_MIN_LEN ||
    972 		    total_len > (MCLBYTES - RTK_ETHER_ALIGN)) {
    973 			ifp->if_ierrors++;
    974 
    975 			/*
    976 			 * submitted by:[netbsd-pcmcia:00484]
    977 			 *	Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
    978 			 * obtain from:
    979 			 *     FreeBSD if_rl.c rev 1.24->1.25
    980 			 *
    981 			 */
    982 #if 0
    983 			if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
    984 			    RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
    985 			    RTK_RXSTAT_ALIGNERR)) {
    986 				CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
    987 				CSR_WRITE_2(sc, RTK_COMMAND,
    988 				    RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
    989 				CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
    990 				CSR_WRITE_4(sc, RTK_RXADDR,
    991 				    sc->recv_dmamap->dm_segs[0].ds_addr);
    992 				cur_rx = 0;
    993 			}
    994 			break;
    995 #else
    996 			rtk_init(ifp);
    997 			return;
    998 #endif
    999 		}
   1000 
   1001 		/* No errors; receive the packet. */
   1002 		rx_bytes += total_len + RTK_RXSTAT_LEN;
   1003 
   1004 		/*
   1005 		 * Avoid trying to read more bytes than we know
   1006 		 * the chip has prepared for us.
   1007 		 */
   1008 		if (rx_bytes > max_bytes)
   1009 			break;
   1010 
   1011 		/*
   1012 		 * Skip the status word, wrapping around to the beginning
   1013 		 * of the Rx area, if necessary.
   1014 		 */
   1015 		cur_rx = (cur_rx + RTK_RXSTAT_LEN) % RTK_RXBUFLEN;
   1016 		rxbufpos = (char *)sc->rtk_rx_buf + cur_rx;
   1017 
   1018 		/*
   1019 		 * Compute the number of bytes at which the packet
   1020 		 * will wrap to the beginning of the ring buffer.
   1021 		 */
   1022 		wrap = RTK_RXBUFLEN - cur_rx;
   1023 
   1024 		/*
   1025 		 * Compute where the next pending packet is.
   1026 		 */
   1027 		if (total_len > wrap)
   1028 			new_rx = total_len - wrap;
   1029 		else
   1030 			new_rx = cur_rx + total_len;
   1031 		/* Round up to 32-bit boundary. */
   1032 		new_rx = ((new_rx + 3) & ~3) % RTK_RXBUFLEN;
   1033 
   1034 		/*
   1035 		 * The RealTek chip includes the CRC with every
   1036 		 * incoming packet; trim it off here.
   1037 		 */
   1038 		total_len -= ETHER_CRC_LEN;
   1039 
   1040 		/*
   1041 		 * Now allocate an mbuf (and possibly a cluster) to hold
   1042 		 * the packet. Note we offset the packet 2 bytes so that
   1043 		 * data after the Ethernet header will be 4-byte aligned.
   1044 		 */
   1045 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1046 		if (m == NULL) {
   1047 			printf("%s: unable to allocate Rx mbuf\n",
   1048 			    device_xname(&sc->sc_dev));
   1049 			ifp->if_ierrors++;
   1050 			goto next_packet;
   1051 		}
   1052 		if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
   1053 			MCLGET(m, M_DONTWAIT);
   1054 			if ((m->m_flags & M_EXT) == 0) {
   1055 				printf("%s: unable to allocate Rx cluster\n",
   1056 				    device_xname(&sc->sc_dev));
   1057 				ifp->if_ierrors++;
   1058 				m_freem(m);
   1059 				m = NULL;
   1060 				goto next_packet;
   1061 			}
   1062 		}
   1063 		m->m_data += RTK_ETHER_ALIGN;	/* for alignment */
   1064 		m->m_pkthdr.rcvif = ifp;
   1065 		m->m_pkthdr.len = m->m_len = total_len;
   1066 		dst = mtod(m, void *);
   1067 
   1068 		/*
   1069 		 * If the packet wraps, copy up to the wrapping point.
   1070 		 */
   1071 		if (total_len > wrap) {
   1072 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1073 			    cur_rx, wrap, BUS_DMASYNC_POSTREAD);
   1074 			memcpy(dst, rxbufpos, wrap);
   1075 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1076 			    cur_rx, wrap, BUS_DMASYNC_PREREAD);
   1077 			cur_rx = 0;
   1078 			rxbufpos = sc->rtk_rx_buf;
   1079 			total_len -= wrap;
   1080 			dst += wrap;
   1081 		}
   1082 
   1083 		/*
   1084 		 * ...and now the rest.
   1085 		 */
   1086 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1087 		    cur_rx, total_len, BUS_DMASYNC_POSTREAD);
   1088 		memcpy(dst, rxbufpos, total_len);
   1089 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1090 		    cur_rx, total_len, BUS_DMASYNC_PREREAD);
   1091 
   1092  next_packet:
   1093 		CSR_WRITE_2(sc, RTK_CURRXADDR, (new_rx - 16) % RTK_RXBUFLEN);
   1094 		cur_rx = new_rx;
   1095 
   1096 		if (m == NULL)
   1097 			continue;
   1098 
   1099 		ifp->if_ipackets++;
   1100 
   1101 #if NBPFILTER > 0
   1102 		if (ifp->if_bpf)
   1103 			bpf_mtap(ifp->if_bpf, m);
   1104 #endif
   1105 		/* pass it on. */
   1106 		(*ifp->if_input)(ifp, m);
   1107 	}
   1108 }
   1109 
   1110 /*
   1111  * A frame was downloaded to the chip. It's safe for us to clean up
   1112  * the list buffers.
   1113  */
   1114 STATIC void
   1115 rtk_txeof(struct rtk_softc *sc)
   1116 {
   1117 	struct ifnet *ifp;
   1118 	struct rtk_tx_desc *txd;
   1119 	uint32_t txstat;
   1120 
   1121 	ifp = &sc->ethercom.ec_if;
   1122 
   1123 	/*
   1124 	 * Go through our tx list and free mbufs for those
   1125 	 * frames that have been uploaded.
   1126 	 */
   1127 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
   1128 		txstat = CSR_READ_4(sc, txd->txd_txstat);
   1129 		if ((txstat & (RTK_TXSTAT_TX_OK|
   1130 		    RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)) == 0)
   1131 			break;
   1132 
   1133 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
   1134 
   1135 		bus_dmamap_sync(sc->sc_dmat, txd->txd_dmamap, 0,
   1136 		    txd->txd_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1137 		bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
   1138 		m_freem(txd->txd_mbuf);
   1139 		txd->txd_mbuf = NULL;
   1140 
   1141 		ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
   1142 
   1143 		if (txstat & RTK_TXSTAT_TX_OK)
   1144 			ifp->if_opackets++;
   1145 		else {
   1146 			ifp->if_oerrors++;
   1147 
   1148 			/*
   1149 			 * Increase Early TX threshold if underrun occurred.
   1150 			 * Increase step 64 bytes.
   1151 			 */
   1152 			if (txstat & RTK_TXSTAT_TX_UNDERRUN) {
   1153 #ifdef DEBUG
   1154 				printf("%s: transmit underrun;",
   1155 				    device_xname(&sc->sc_dev));
   1156 #endif
   1157 				if (sc->sc_txthresh < RTK_TXTH_MAX) {
   1158 					sc->sc_txthresh += 2;
   1159 #ifdef DEBUG
   1160 					printf(" new threshold: %d bytes",
   1161 					    sc->sc_txthresh * 32);
   1162 #endif
   1163 				}
   1164 				printf("\n");
   1165 			}
   1166 			if (txstat & (RTK_TXSTAT_TXABRT|RTK_TXSTAT_OUTOFWIN))
   1167 				CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1168 		}
   1169 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
   1170 		ifp->if_flags &= ~IFF_OACTIVE;
   1171 	}
   1172 
   1173 	/* Clear the timeout timer if there is no pending packet. */
   1174 	if (SIMPLEQ_EMPTY(&sc->rtk_tx_dirty))
   1175 		ifp->if_timer = 0;
   1176 
   1177 }
   1178 
   1179 int
   1180 rtk_intr(void *arg)
   1181 {
   1182 	struct rtk_softc *sc;
   1183 	struct ifnet *ifp;
   1184 	uint16_t status;
   1185 	int handled;
   1186 
   1187 	sc = arg;
   1188 	ifp = &sc->ethercom.ec_if;
   1189 
   1190 	if (!device_has_power(&sc->sc_dev))
   1191 		return 0;
   1192 
   1193 	/* Disable interrupts. */
   1194 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1195 
   1196 	handled = 0;
   1197 	for (;;) {
   1198 
   1199 		status = CSR_READ_2(sc, RTK_ISR);
   1200 
   1201 		if (status == 0xffff)
   1202 			break; /* Card is gone... */
   1203 
   1204 		if (status)
   1205 			CSR_WRITE_2(sc, RTK_ISR, status);
   1206 
   1207 		if ((status & RTK_INTRS) == 0)
   1208 			break;
   1209 
   1210 		handled = 1;
   1211 
   1212 		if (status & RTK_ISR_RX_OK)
   1213 			rtk_rxeof(sc);
   1214 
   1215 		if (status & RTK_ISR_RX_ERR)
   1216 			rtk_rxeof(sc);
   1217 
   1218 		if (status & (RTK_ISR_TX_OK|RTK_ISR_TX_ERR))
   1219 			rtk_txeof(sc);
   1220 
   1221 		if (status & RTK_ISR_SYSTEM_ERR) {
   1222 			rtk_reset(sc);
   1223 			rtk_init(ifp);
   1224 		}
   1225 	}
   1226 
   1227 	/* Re-enable interrupts. */
   1228 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1229 
   1230 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1231 		rtk_start(ifp);
   1232 
   1233 #if NRND > 0
   1234 	if (RND_ENABLED(&sc->rnd_source))
   1235 		rnd_add_uint32(&sc->rnd_source, status);
   1236 #endif
   1237 
   1238 	return handled;
   1239 }
   1240 
   1241 /*
   1242  * Main transmit routine.
   1243  */
   1244 
   1245 STATIC void
   1246 rtk_start(struct ifnet *ifp)
   1247 {
   1248 	struct rtk_softc *sc;
   1249 	struct rtk_tx_desc *txd;
   1250 	struct mbuf *m_head, *m_new;
   1251 	int error, len;
   1252 
   1253 	sc = ifp->if_softc;
   1254 
   1255 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL) {
   1256 		IFQ_POLL(&ifp->if_snd, m_head);
   1257 		if (m_head == NULL)
   1258 			break;
   1259 		m_new = NULL;
   1260 
   1261 		/*
   1262 		 * Load the DMA map.  If this fails, the packet didn't
   1263 		 * fit in one DMA segment, and we need to copy.  Note,
   1264 		 * the packet must also be aligned.
   1265 		 * if the packet is too small, copy it too, so we're sure
   1266 		 * so have enouth room for the pad buffer.
   1267 		 */
   1268 		if ((mtod(m_head, uintptr_t) & 3) != 0 ||
   1269 		    m_head->m_pkthdr.len < ETHER_PAD_LEN ||
   1270 		    bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmamap,
   1271 			m_head, BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
   1272 			MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1273 			if (m_new == NULL) {
   1274 				printf("%s: unable to allocate Tx mbuf\n",
   1275 				    device_xname(&sc->sc_dev));
   1276 				break;
   1277 			}
   1278 			if (m_head->m_pkthdr.len > MHLEN) {
   1279 				MCLGET(m_new, M_DONTWAIT);
   1280 				if ((m_new->m_flags & M_EXT) == 0) {
   1281 					printf("%s: unable to allocate Tx "
   1282 					    "cluster\n", device_xname(&sc->sc_dev));
   1283 					m_freem(m_new);
   1284 					break;
   1285 				}
   1286 			}
   1287 			m_copydata(m_head, 0, m_head->m_pkthdr.len,
   1288 			    mtod(m_new, void *));
   1289 			m_new->m_pkthdr.len = m_new->m_len =
   1290 			    m_head->m_pkthdr.len;
   1291 			if (m_head->m_pkthdr.len < ETHER_PAD_LEN) {
   1292 				memset(
   1293 				    mtod(m_new, char *) + m_head->m_pkthdr.len,
   1294 				    0, ETHER_PAD_LEN - m_head->m_pkthdr.len);
   1295 				m_new->m_pkthdr.len = m_new->m_len =
   1296 				    ETHER_PAD_LEN;
   1297 			}
   1298 			error = bus_dmamap_load_mbuf(sc->sc_dmat,
   1299 			    txd->txd_dmamap, m_new,
   1300 			    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1301 			if (error) {
   1302 				printf("%s: unable to load Tx buffer, "
   1303 				    "error = %d\n", device_xname(&sc->sc_dev), error);
   1304 				break;
   1305 			}
   1306 		}
   1307 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1308 #if NBPFILTER > 0
   1309 		/*
   1310 		 * If there's a BPF listener, bounce a copy of this frame
   1311 		 * to him.
   1312 		 */
   1313 		if (ifp->if_bpf)
   1314 			bpf_mtap(ifp->if_bpf, m_head);
   1315 #endif
   1316 		if (m_new != NULL) {
   1317 			m_freem(m_head);
   1318 			m_head = m_new;
   1319 		}
   1320 		txd->txd_mbuf = m_head;
   1321 
   1322 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
   1323 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_dirty, txd, txd_q);
   1324 
   1325 		/*
   1326 		 * Transmit the frame.
   1327 		 */
   1328 		bus_dmamap_sync(sc->sc_dmat,
   1329 		    txd->txd_dmamap, 0, txd->txd_dmamap->dm_mapsize,
   1330 		    BUS_DMASYNC_PREWRITE);
   1331 
   1332 		len = txd->txd_dmamap->dm_segs[0].ds_len;
   1333 
   1334 		CSR_WRITE_4(sc, txd->txd_txaddr,
   1335 		    txd->txd_dmamap->dm_segs[0].ds_addr);
   1336 		CSR_WRITE_4(sc, txd->txd_txstat,
   1337 		    RTK_TXSTAT_THRESH(sc->sc_txthresh) | len);
   1338 
   1339 		/*
   1340 		 * Set a timeout in case the chip goes out to lunch.
   1341 		 */
   1342 		ifp->if_timer = 5;
   1343 	}
   1344 
   1345 	/*
   1346 	 * We broke out of the loop because all our TX slots are
   1347 	 * full. Mark the NIC as busy until it drains some of the
   1348 	 * packets from the queue.
   1349 	 */
   1350 	if (SIMPLEQ_EMPTY(&sc->rtk_tx_free))
   1351 		ifp->if_flags |= IFF_OACTIVE;
   1352 }
   1353 
   1354 STATIC int
   1355 rtk_init(struct ifnet *ifp)
   1356 {
   1357 	struct rtk_softc *sc = ifp->if_softc;
   1358 	int error, i;
   1359 	uint32_t rxcfg;
   1360 
   1361 	if ((error = rtk_enable(sc)) != 0)
   1362 		goto out;
   1363 
   1364 	/*
   1365 	 * Cancel pending I/O.
   1366 	 */
   1367 	rtk_stop(ifp, 0);
   1368 
   1369 	/* Init our MAC address */
   1370 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1371 		CSR_WRITE_1(sc, RTK_IDR0 + i, CLLADDR(ifp->if_sadl)[i]);
   1372 	}
   1373 
   1374 	/* Init the RX buffer pointer register. */
   1375 	bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
   1376 	    sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1377 	CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
   1378 
   1379 	/* Init TX descriptors. */
   1380 	rtk_list_tx_init(sc);
   1381 
   1382 	/* Init Early TX threshold. */
   1383 	sc->sc_txthresh = RTK_TXTH_256;
   1384 	/*
   1385 	 * Enable transmit and receive.
   1386 	 */
   1387 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1388 
   1389 	/*
   1390 	 * Set the initial TX and RX configuration.
   1391 	 */
   1392 	CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1393 	CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1394 
   1395 	/* Set the individual bit to receive frames for this host only. */
   1396 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1397 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1398 
   1399 	/* If we want promiscuous mode, set the allframes bit. */
   1400 	if (ifp->if_flags & IFF_PROMISC) {
   1401 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1402 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1403 	} else {
   1404 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1405 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1406 	}
   1407 
   1408 	/*
   1409 	 * Set capture broadcast bit to capture broadcast frames.
   1410 	 */
   1411 	if (ifp->if_flags & IFF_BROADCAST) {
   1412 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1413 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1414 	} else {
   1415 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1416 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1417 	}
   1418 
   1419 	/*
   1420 	 * Program the multicast filter, if necessary.
   1421 	 */
   1422 	rtk_setmulti(sc);
   1423 
   1424 	/*
   1425 	 * Enable interrupts.
   1426 	 */
   1427 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1428 
   1429 	/* Start RX/TX process. */
   1430 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   1431 
   1432 	/* Enable receiver and transmitter. */
   1433 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1434 
   1435 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
   1436 
   1437 	/*
   1438 	 * Set current media.
   1439 	 */
   1440 	mii_mediachg(&sc->mii);
   1441 
   1442 	ifp->if_flags |= IFF_RUNNING;
   1443 	ifp->if_flags &= ~IFF_OACTIVE;
   1444 
   1445 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1446 
   1447  out:
   1448 	if (error) {
   1449 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1450 		ifp->if_timer = 0;
   1451 		printf("%s: interface not running\n", device_xname(&sc->sc_dev));
   1452 	}
   1453 	return error;
   1454 }
   1455 
   1456 /*
   1457  * Set media options.
   1458  */
   1459 STATIC int
   1460 rtk_ifmedia_upd(struct ifnet *ifp)
   1461 {
   1462 	struct rtk_softc *sc;
   1463 
   1464 	sc = ifp->if_softc;
   1465 
   1466 	return mii_mediachg(&sc->mii);
   1467 }
   1468 
   1469 /*
   1470  * Report current media status.
   1471  */
   1472 STATIC void
   1473 rtk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1474 {
   1475 	struct rtk_softc *sc;
   1476 
   1477 	sc = ifp->if_softc;
   1478 
   1479 	mii_pollstat(&sc->mii);
   1480 	ifmr->ifm_status = sc->mii.mii_media_status;
   1481 	ifmr->ifm_active = sc->mii.mii_media_active;
   1482 }
   1483 
   1484 STATIC int
   1485 rtk_ioctl(struct ifnet *ifp, u_long command, void *data)
   1486 {
   1487 	struct rtk_softc *sc = ifp->if_softc;
   1488 	struct ifreq *ifr = (struct ifreq *)data;
   1489 	int s, error;
   1490 
   1491 	s = splnet();
   1492 
   1493 	switch (command) {
   1494 	case SIOCGIFMEDIA:
   1495 	case SIOCSIFMEDIA:
   1496 		error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
   1497 		break;
   1498 
   1499 	default:
   1500 		error = ether_ioctl(ifp, command, data);
   1501 		if (error == ENETRESET) {
   1502 			if (ifp->if_flags & IFF_RUNNING) {
   1503 				/*
   1504 				 * Multicast list has changed.  Set the
   1505 				 * hardware filter accordingly.
   1506 				 */
   1507 				rtk_setmulti(sc);
   1508 			}
   1509 			error = 0;
   1510 		}
   1511 		break;
   1512 	}
   1513 
   1514 	splx(s);
   1515 
   1516 	return error;
   1517 }
   1518 
   1519 STATIC void
   1520 rtk_watchdog(struct ifnet *ifp)
   1521 {
   1522 	struct rtk_softc *sc;
   1523 
   1524 	sc = ifp->if_softc;
   1525 
   1526 	printf("%s: watchdog timeout\n", device_xname(&sc->sc_dev));
   1527 	ifp->if_oerrors++;
   1528 	rtk_txeof(sc);
   1529 	rtk_rxeof(sc);
   1530 	rtk_init(ifp);
   1531 }
   1532 
   1533 /*
   1534  * Stop the adapter and free any mbufs allocated to the
   1535  * RX and TX lists.
   1536  */
   1537 STATIC void
   1538 rtk_stop(struct ifnet *ifp, int disable)
   1539 {
   1540 	struct rtk_softc *sc = ifp->if_softc;
   1541 	struct rtk_tx_desc *txd;
   1542 
   1543 	callout_stop(&sc->rtk_tick_ch);
   1544 
   1545 	mii_down(&sc->mii);
   1546 
   1547 	CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   1548 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1549 
   1550 	/*
   1551 	 * Free the TX list buffers.
   1552 	 */
   1553 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
   1554 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
   1555 		bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
   1556 		m_freem(txd->txd_mbuf);
   1557 		txd->txd_mbuf = NULL;
   1558 		CSR_WRITE_4(sc, txd->txd_txaddr, 0);
   1559 	}
   1560 
   1561 	if (disable)
   1562 		rtk_disable(sc);
   1563 
   1564 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1565 	ifp->if_timer = 0;
   1566 }
   1567 
   1568 STATIC void
   1569 rtk_tick(void *arg)
   1570 {
   1571 	struct rtk_softc *sc = arg;
   1572 	int s;
   1573 
   1574 	s = splnet();
   1575 	mii_tick(&sc->mii);
   1576 	splx(s);
   1577 
   1578 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1579 }
   1580