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