Home | History | Annotate | Line # | Download | only in ic
rtl81x9.c revision 1.22
      1 /*	$NetBSD: rtl81x9.c,v 1.22 2000/12/03 14:24:17 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  *
    963  * The receive operation is badly documented in the datasheet, so I'll
    964  * attempt to document it here. The driver provides a buffer area and
    965  * places its base address in the RX buffer start address register.
    966  * The chip then begins copying frames into the RX buffer. Each frame
    967  * is preceeded by a 32-bit RX status word which specifies the length
    968  * of the frame and certain other status bits. Each frame (starting with
    969  * the status word) is also 32-bit aligned. The frame length is in the
    970  * first 16 bits of the status word; the lower 15 bits correspond with
    971  * the 'rx status register' mentioned in the datasheet.
    972  *
    973  * Note: to make the Alpha happy, the frame payload needs to be aligned
    974  * on a 32-bit boundary. To achieve this, we copy the data to mbuf
    975  * shifted forward 2 bytes.
    976  */
    977 STATIC void rtk_rxeof(sc)
    978 	struct rtk_softc	*sc;
    979 {
    980         struct mbuf		*m;
    981         struct ifnet		*ifp;
    982 	caddr_t			rxbufpos, dst;
    983 	int			total_len, wrap = 0;
    984 	u_int32_t		rxstat;
    985 	u_int16_t		cur_rx, new_rx;
    986 	u_int16_t		limit;
    987 	u_int16_t		rx_bytes = 0, max_bytes;
    988 
    989 	ifp = &sc->ethercom.ec_if;
    990 
    991 	cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
    992 
    993 	/* Do not try to read past this point. */
    994 	limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
    995 
    996 	if (limit < cur_rx)
    997 		max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
    998 	else
    999 		max_bytes = limit - cur_rx;
   1000 
   1001 	while((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
   1002 		rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
   1003 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
   1004 		    RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
   1005 		rxstat = le32toh(*(u_int32_t *)rxbufpos);
   1006 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
   1007 		    RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
   1008 
   1009 		/*
   1010 		 * Here's a totally undocumented fact for you. When the
   1011 		 * RealTek chip is in the process of copying a packet into
   1012 		 * RAM for you, the length will be 0xfff0. If you spot a
   1013 		 * packet header with this value, you need to stop. The
   1014 		 * datasheet makes absolutely no mention of this and
   1015 		 * RealTek should be shot for this.
   1016 		 */
   1017 		total_len = rxstat >> 16;
   1018 		if (total_len == RTK_RXSTAT_UNFINISHED)
   1019 			break;
   1020 
   1021 		if ((rxstat & RTK_RXSTAT_RXOK) == 0) {
   1022 			ifp->if_ierrors++;
   1023 
   1024 			/*
   1025 			 * submitted by:[netbsd-pcmcia:00484]
   1026 			 *	Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
   1027 			 * obtain from:
   1028 			 *     FreeBSD if_rl.c rev 1.24->1.25
   1029 			 *
   1030 			 */
   1031 #if 0
   1032 			if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
   1033 			    RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
   1034 			    RTK_RXSTAT_ALIGNERR)) {
   1035 				CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
   1036 				CSR_WRITE_2(sc, RTK_COMMAND,
   1037 				    RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1038 				CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1039 				CSR_WRITE_4(sc, RTK_RXADDR,
   1040 				    sc->recv_dmamap->dm_segs[0].ds_addr);
   1041 				CSR_WRITE_2(sc, RTK_CURRXADDR, cur_rx - 16);
   1042 				cur_rx = 0;
   1043 			}
   1044 			break;
   1045 #else
   1046 			rtk_init(ifp);
   1047 			return;
   1048 #endif
   1049 		}
   1050 
   1051 		/* No errors; receive the packet. */
   1052 		rx_bytes += total_len + RTK_RXSTAT_LEN;
   1053 
   1054 		/*
   1055 		 * Avoid trying to read more bytes than we know
   1056 		 * the chip has prepared for us.
   1057 		 */
   1058 		if (rx_bytes > max_bytes)
   1059 			break;
   1060 
   1061 		/*
   1062 		 * Skip the status word, wrapping around to the beginning
   1063 		 * of the Rx area, if necessary.
   1064 		 */
   1065 		cur_rx += RTK_RXSTAT_LEN;
   1066 		rxbufpos = sc->rtk_cdata.rtk_rx_buf + (cur_rx % RTK_RXBUFLEN);
   1067 
   1068 		/*
   1069 		 * Compute the number of bytes at which the packet
   1070 		 * will wrap to the beginning of the ring buffer.
   1071 		 */
   1072 		wrap = RTK_RXBUFLEN - (cur_rx % RTK_RXBUFLEN);
   1073 
   1074 		/*
   1075 		 * Compute where the next pending packet is.
   1076 		 */
   1077 		if (total_len > wrap)
   1078 			new_rx = total_len - wrap;
   1079 		else
   1080 			new_rx = cur_rx + total_len;
   1081 		/* Round up to 32-bit boundary. */
   1082 		new_rx = (new_rx + 3) & ~3;
   1083 
   1084 		/*
   1085 		 * Now allocate an mbuf (and possibly a cluster) to hold
   1086 		 * the packet. Note we offset the packet 2 bytes so that
   1087 		 * data after the Ethernet header will be 4-byte aligned.
   1088 		 */
   1089 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1090 		if (m == NULL) {
   1091 			printf("%s: unable to allocate Rx mbuf\n",
   1092 			    sc->sc_dev.dv_xname);
   1093 			ifp->if_ierrors++;
   1094 			goto next_packet;
   1095 		}
   1096 		if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
   1097 			MCLGET(m, M_DONTWAIT);
   1098 			if ((m->m_flags & M_EXT) == 0) {
   1099 				printf("%s: unable to allocate Rx cluster\n",
   1100 				    sc->sc_dev.dv_xname);
   1101 				ifp->if_ierrors++;
   1102 				m_freem(m);
   1103 				m = NULL;
   1104 				goto next_packet;
   1105 			}
   1106 		}
   1107 		m->m_data += RTK_ETHER_ALIGN;	/* for alignment */
   1108 		m->m_pkthdr.rcvif = ifp;
   1109 		m->m_pkthdr.len = m->m_len = total_len;
   1110 		dst = mtod(m, caddr_t);
   1111 
   1112 		/*
   1113 		 * If the packet wraps, copy up to the wrapping point.
   1114 		 */
   1115 		if (total_len > wrap) {
   1116 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1117 			    cur_rx, wrap, BUS_DMASYNC_POSTREAD);
   1118 			memcpy(dst, rxbufpos, wrap);
   1119 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1120 			    cur_rx, wrap, BUS_DMASYNC_PREREAD);
   1121 			cur_rx = 0;
   1122 			rxbufpos = sc->rtk_cdata.rtk_rx_buf;
   1123 			total_len -= wrap;
   1124 			dst += wrap;
   1125 		}
   1126 
   1127 		/*
   1128 		 * ...and now the rest.
   1129 		 */
   1130 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1131 		    cur_rx, total_len, BUS_DMASYNC_POSTREAD);
   1132 		memcpy(dst, rxbufpos, total_len);
   1133 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1134 		    cur_rx, total_len, BUS_DMASYNC_PREREAD);
   1135 
   1136 next_packet:
   1137 		CSR_WRITE_2(sc, RTK_CURRXADDR, new_rx - 16);
   1138 		cur_rx = new_rx;
   1139 
   1140 		if (m == NULL)
   1141 			continue;
   1142 
   1143 		/*
   1144 		 * The RealTek chip includes the CRC with every
   1145 		 * incoming packet.
   1146 		 */
   1147 		m->m_flags |= M_HASFCS;
   1148 
   1149 		ifp->if_ipackets++;
   1150 
   1151 #if NBPFILTER > 0
   1152 		if (ifp->if_bpf)
   1153 			bpf_mtap(ifp->if_bpf, m);
   1154 #endif
   1155 		/* pass it on. */
   1156 		(*ifp->if_input)(ifp, m);
   1157 	}
   1158 }
   1159 
   1160 /*
   1161  * A frame was downloaded to the chip. It's safe for us to clean up
   1162  * the list buffers.
   1163  */
   1164 STATIC void rtk_txeof(sc)
   1165 	struct rtk_softc	*sc;
   1166 {
   1167 	struct ifnet		*ifp;
   1168 	u_int32_t		txstat;
   1169 
   1170 	ifp = &sc->ethercom.ec_if;
   1171 
   1172 	/* Clear the timeout timer. */
   1173 	ifp->if_timer = 0;
   1174 
   1175 	/*
   1176 	 * Go through our tx list and free mbufs for those
   1177 	 * frames that have been uploaded.
   1178 	 */
   1179 	do {
   1180 		txstat = CSR_READ_4(sc, RTK_LAST_TXSTAT(sc));
   1181 		if (!(txstat & (RTK_TXSTAT_TX_OK|
   1182 		    RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)))
   1183 			break;
   1184 
   1185 		bus_dmamap_sync(sc->sc_dmat,
   1186 		    sc->snd_dmamap[sc->rtk_cdata.last_tx], 0,
   1187 		    sc->snd_dmamap[sc->rtk_cdata.last_tx]->dm_mapsize,
   1188 		    BUS_DMASYNC_POSTWRITE);
   1189 		bus_dmamap_unload(sc->sc_dmat,
   1190 		    sc->snd_dmamap[sc->rtk_cdata.last_tx]);
   1191 		m_freem(RTK_LAST_TXMBUF(sc));
   1192 		RTK_LAST_TXMBUF(sc) = NULL;
   1193 
   1194 		ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
   1195 
   1196 		if (txstat & RTK_TXSTAT_TX_OK)
   1197 			ifp->if_opackets++;
   1198 		else {
   1199 			ifp->if_oerrors++;
   1200 			if ((txstat & RTK_TXSTAT_TXABRT) ||
   1201 			    (txstat & RTK_TXSTAT_OUTOFWIN))
   1202 				CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1203 		}
   1204 		RTK_INC(sc->rtk_cdata.last_tx);
   1205 		ifp->if_flags &= ~IFF_OACTIVE;
   1206 	} while (sc->rtk_cdata.last_tx != sc->rtk_cdata.cur_tx);
   1207 
   1208 	return;
   1209 }
   1210 
   1211 int rtk_intr(arg)
   1212 	void			*arg;
   1213 {
   1214 	struct rtk_softc	*sc;
   1215 	struct ifnet		*ifp;
   1216 	u_int16_t		status;
   1217 	int handled = 0;
   1218 
   1219 	sc = arg;
   1220 	ifp = &sc->ethercom.ec_if;
   1221 
   1222 	/* Disable interrupts. */
   1223 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1224 
   1225 	for (;;) {
   1226 
   1227 		status = CSR_READ_2(sc, RTK_ISR);
   1228 		if (status)
   1229 			CSR_WRITE_2(sc, RTK_ISR, status);
   1230 
   1231 		handled = 1;
   1232 
   1233 		if ((status & RTK_INTRS) == 0)
   1234 			break;
   1235 
   1236 		if (status & RTK_ISR_RX_OK)
   1237 			rtk_rxeof(sc);
   1238 
   1239 		if (status & RTK_ISR_RX_ERR)
   1240 			rtk_rxeof(sc);
   1241 
   1242 		if ((status & RTK_ISR_TX_OK) || (status & RTK_ISR_TX_ERR))
   1243 			rtk_txeof(sc);
   1244 
   1245 		if (status & RTK_ISR_SYSTEM_ERR) {
   1246 			rtk_reset(sc);
   1247 			rtk_init(ifp);
   1248 		}
   1249 
   1250 	}
   1251 
   1252 	/* Re-enable interrupts. */
   1253 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1254 
   1255 	if (ifp->if_snd.ifq_head != NULL) {
   1256 		rtk_start(ifp);
   1257 	}
   1258 
   1259 	return (handled);
   1260 }
   1261 
   1262 /*
   1263  * Main transmit routine.
   1264  */
   1265 
   1266 STATIC void rtk_start(ifp)
   1267 	struct ifnet		*ifp;
   1268 {
   1269 	struct rtk_softc	*sc;
   1270 	struct mbuf		*m_head = NULL, *m_new;
   1271 	int			error, idx, len;
   1272 
   1273 	sc = ifp->if_softc;
   1274 
   1275 	while(RTK_CUR_TXMBUF(sc) == NULL) {
   1276 		IF_DEQUEUE(&ifp->if_snd, m_head);
   1277 		if (m_head == NULL)
   1278 			break;
   1279 
   1280 		idx = sc->rtk_cdata.cur_tx;
   1281 
   1282 		/*
   1283 		 * Load the DMA map.  If this fails, the packet didn't
   1284 		 * fit in one DMA segment, and we need to copy.  Note,
   1285 		 * the packet must also be aligned.
   1286 		 */
   1287 		if ((mtod(m_head, bus_addr_t) & 3) != 0 ||
   1288 		    bus_dmamap_load_mbuf(sc->sc_dmat, sc->snd_dmamap[idx],
   1289 			m_head, BUS_DMA_NOWAIT) != 0) {
   1290 			MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1291 			if (m_new == NULL) {
   1292 				printf("%s: unable to allocate Tx mbuf\n",
   1293 				    sc->sc_dev.dv_xname);
   1294 				IF_PREPEND(&ifp->if_snd, m_new);
   1295 				break;
   1296 			}
   1297 			if (m_head->m_pkthdr.len > MHLEN) {
   1298 				MCLGET(m_new, M_DONTWAIT);
   1299 				if ((m_new->m_flags & M_EXT) == 0) {
   1300 					printf("%s: unable to allocate Tx "
   1301 					    "cluster\n", sc->sc_dev.dv_xname);
   1302 					m_freem(m_new);
   1303 					IF_PREPEND(&ifp->if_snd, m_head);
   1304 					break;
   1305 				}
   1306 			}
   1307 			m_copydata(m_head, 0, m_head->m_pkthdr.len,
   1308 			    mtod(m_new, caddr_t));
   1309 			m_new->m_pkthdr.len = m_new->m_len =
   1310 			    m_head->m_pkthdr.len;
   1311 			m_freem(m_head);
   1312 			m_head = m_new;
   1313 			error = bus_dmamap_load_mbuf(sc->sc_dmat,
   1314 			    sc->snd_dmamap[idx], m_head, BUS_DMA_NOWAIT);
   1315 			if (error) {
   1316 				printf("%s: unable to load Tx buffer, "
   1317 				    "error = %d\n", sc->sc_dev.dv_xname, error);
   1318 				IF_PREPEND(&ifp->if_snd, m_head);
   1319 				break;
   1320 			}
   1321 		}
   1322 
   1323 		RTK_CUR_TXMBUF(sc) = m_head;
   1324 
   1325 #if NBPFILTER > 0
   1326 		/*
   1327 		 * If there's a BPF listener, bounce a copy of this frame
   1328 		 * to him.
   1329 		 */
   1330 		if (ifp->if_bpf)
   1331 			bpf_mtap(ifp->if_bpf, RTK_CUR_TXMBUF(sc));
   1332 #endif
   1333 		/*
   1334 		 * Transmit the frame.
   1335 	 	 */
   1336 		bus_dmamap_sync(sc->sc_dmat,
   1337 		    sc->snd_dmamap[idx], 0, sc->snd_dmamap[idx]->dm_mapsize,
   1338 		    BUS_DMASYNC_PREWRITE);
   1339 
   1340 		len = sc->snd_dmamap[idx]->dm_segs[0].ds_len;
   1341 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
   1342 			len = (ETHER_MIN_LEN - ETHER_CRC_LEN);
   1343 
   1344 		CSR_WRITE_4(sc, RTK_CUR_TXADDR(sc),
   1345 			    sc->snd_dmamap[idx]->dm_segs[0].ds_addr);
   1346 		CSR_WRITE_4(sc, RTK_CUR_TXSTAT(sc), RTK_TX_EARLYTHRESH | len);
   1347 
   1348 		RTK_INC(sc->rtk_cdata.cur_tx);
   1349 	}
   1350 
   1351 	/*
   1352 	 * We broke out of the loop because all our TX slots are
   1353 	 * full. Mark the NIC as busy until it drains some of the
   1354 	 * packets from the queue.
   1355 	 */
   1356 	if (RTK_CUR_TXMBUF(sc) != NULL)
   1357 		ifp->if_flags |= IFF_OACTIVE;
   1358 
   1359 	/*
   1360 	 * Set a timeout in case the chip goes out to lunch.
   1361 	 */
   1362 	ifp->if_timer = 5;
   1363 
   1364 	return;
   1365 }
   1366 
   1367 STATIC int rtk_init(ifp)
   1368 	struct ifnet *ifp;
   1369 {
   1370 	struct rtk_softc	*sc = ifp->if_softc;
   1371 	int			error = 0, i;
   1372 	u_int32_t		rxcfg;
   1373 
   1374 	if ((error = rtk_enable(sc)) != 0)
   1375 		goto out;
   1376 
   1377 	/*
   1378 	 * Cancel pending I/O.
   1379 	 */
   1380 	rtk_stop(ifp, 0);
   1381 
   1382 	/* Init our MAC address */
   1383 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1384 		CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
   1385 	}
   1386 
   1387 	/* Init the RX buffer pointer register. */
   1388 	bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
   1389 	    sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1390 	CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
   1391 
   1392 	/* Init TX descriptors. */
   1393 	rtk_list_tx_init(sc);
   1394 
   1395 	/*
   1396 	 * Enable transmit and receive.
   1397 	 */
   1398 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1399 
   1400 	/*
   1401 	 * Set the initial TX and RX configuration.
   1402 	 */
   1403 	CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1404 	CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1405 
   1406 	/* Set the individual bit to receive frames for this host only. */
   1407 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1408 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1409 
   1410 	/* If we want promiscuous mode, set the allframes bit. */
   1411 	if (ifp->if_flags & IFF_PROMISC) {
   1412 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1413 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1414 	} else {
   1415 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1416 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1417 	}
   1418 
   1419 	/*
   1420 	 * Set capture broadcast bit to capture broadcast frames.
   1421 	 */
   1422 	if (ifp->if_flags & IFF_BROADCAST) {
   1423 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1424 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1425 	} else {
   1426 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1427 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1428 	}
   1429 
   1430 	/*
   1431 	 * Program the multicast filter, if necessary.
   1432 	 */
   1433 	rtk_setmulti(sc);
   1434 
   1435 	/*
   1436 	 * Enable interrupts.
   1437 	 */
   1438 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1439 
   1440 	/* Start RX/TX process. */
   1441 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   1442 
   1443 	/* Enable receiver and transmitter. */
   1444 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1445 
   1446 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
   1447 
   1448 	/*
   1449 	 * Set current media.
   1450 	 */
   1451 	mii_mediachg(&sc->mii);
   1452 
   1453 	ifp->if_flags |= IFF_RUNNING;
   1454 	ifp->if_flags &= ~IFF_OACTIVE;
   1455 
   1456 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1457 
   1458  out:
   1459 	if (error) {
   1460 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1461 		ifp->if_timer = 0;
   1462 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1463 	}
   1464 	return (error);
   1465 }
   1466 
   1467 /*
   1468  * Set media options.
   1469  */
   1470 STATIC int rtk_ifmedia_upd(ifp)
   1471 	struct ifnet		*ifp;
   1472 {
   1473 	struct rtk_softc	*sc;
   1474 
   1475 	sc = ifp->if_softc;
   1476 
   1477 	return (mii_mediachg(&sc->mii));
   1478 }
   1479 
   1480 /*
   1481  * Report current media status.
   1482  */
   1483 STATIC void rtk_ifmedia_sts(ifp, ifmr)
   1484 	struct ifnet		*ifp;
   1485 	struct ifmediareq	*ifmr;
   1486 {
   1487 	struct rtk_softc	*sc;
   1488 
   1489 	sc = ifp->if_softc;
   1490 
   1491 	mii_pollstat(&sc->mii);
   1492 	ifmr->ifm_status = sc->mii.mii_media_status;
   1493 	ifmr->ifm_active = sc->mii.mii_media_active;
   1494 }
   1495 
   1496 STATIC int rtk_ioctl(ifp, command, data)
   1497 	struct ifnet		*ifp;
   1498 	u_long			command;
   1499 	caddr_t			data;
   1500 {
   1501 	struct rtk_softc	*sc = ifp->if_softc;
   1502 	struct ifreq		*ifr = (struct ifreq *) data;
   1503 	int			s, error = 0;
   1504 
   1505 	s = splnet();
   1506 
   1507 	switch (command) {
   1508 	case SIOCGIFMEDIA:
   1509 	case SIOCSIFMEDIA:
   1510 		error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
   1511 		break;
   1512 
   1513 	default:
   1514 		error = ether_ioctl(ifp, command, data);
   1515 		if (error == ENETRESET) {
   1516 			if (RTK_IS_ENABLED(sc)) {
   1517 				/*
   1518 				 * Multicast list has changed.  Set the
   1519 				 * hardware filter accordingly.
   1520 				 */
   1521 				rtk_setmulti(sc);
   1522 			}
   1523 			error = 0;
   1524 		}
   1525 		break;
   1526 	}
   1527 
   1528 	splx(s);
   1529 
   1530 	return(error);
   1531 }
   1532 
   1533 STATIC void rtk_watchdog(ifp)
   1534 	struct ifnet		*ifp;
   1535 {
   1536 	struct rtk_softc	*sc;
   1537 
   1538 	sc = ifp->if_softc;
   1539 
   1540 	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
   1541 	ifp->if_oerrors++;
   1542 	rtk_txeof(sc);
   1543 	rtk_rxeof(sc);
   1544 	rtk_init(ifp);
   1545 
   1546 	return;
   1547 }
   1548 
   1549 /*
   1550  * Stop the adapter and free any mbufs allocated to the
   1551  * RX and TX lists.
   1552  */
   1553 STATIC void rtk_stop(ifp, disable)
   1554 	struct ifnet *ifp;
   1555 	int disable;
   1556 {
   1557 	struct rtk_softc *sc = ifp->if_softc;
   1558 	int i;
   1559 
   1560 	callout_stop(&sc->rtk_tick_ch);
   1561 
   1562 	mii_down(&sc->mii);
   1563 
   1564 	CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   1565 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1566 
   1567 	/*
   1568 	 * Free the TX list buffers.
   1569 	 */
   1570 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
   1571 		if (sc->rtk_cdata.rtk_tx_chain[i] != NULL) {
   1572 			bus_dmamap_unload(sc->sc_dmat, sc->snd_dmamap[i]);
   1573 			m_freem(sc->rtk_cdata.rtk_tx_chain[i]);
   1574 			sc->rtk_cdata.rtk_tx_chain[i] = NULL;
   1575 			CSR_WRITE_4(sc, RTK_TXADDR0 + i, 0x0000000);
   1576 		}
   1577 	}
   1578 
   1579 	if (disable)
   1580 		rtk_disable(sc);
   1581 
   1582 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1583 	ifp->if_timer = 0;
   1584 }
   1585 
   1586 /*
   1587  * Stop all chip I/O so that the kernel's probe routines don't
   1588  * get confused by errant DMAs when rebooting.
   1589  */
   1590 STATIC void rtk_shutdown(vsc)
   1591 	void			*vsc;
   1592 {
   1593 	struct rtk_softc	*sc = (struct rtk_softc *)vsc;
   1594 
   1595 	rtk_stop(&sc->ethercom.ec_if, 0);
   1596 }
   1597 
   1598 STATIC void
   1599 rtk_tick(arg)
   1600 	void *arg;
   1601 {
   1602 	struct rtk_softc *sc = arg;
   1603 	int s = splnet();
   1604 
   1605 	mii_tick(&sc->mii);
   1606 	splx(s);
   1607 
   1608 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1609 }
   1610