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