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