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