Home | History | Annotate | Line # | Download | only in ic
rtl81x9.c revision 1.1
      1 /*	$NetBSD: rtl81x9.c,v 1.1 2000/04/10 07:42:56 haya Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35  */
     36 
     37 /*
     38  * RealTek 8129/8139 PCI NIC driver
     39  *
     40  * Supports several extremely cheap PCI 10/100 adapters based on
     41  * the RealTek chipset. Datasheets can be obtained from
     42  * www.realtek.com.tw.
     43  *
     44  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45  * Electrical Engineering Department
     46  * Columbia University, New York City
     47  */
     48 
     49 /*
     50  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
     51  * probably the worst PCI ethernet controller ever made, with the possible
     52  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
     53  * DMA, but it has a terrible interface that nullifies any performance
     54  * gains that bus-master DMA usually offers.
     55  *
     56  * For transmission, the chip offers a series of four TX descriptor
     57  * registers. Each transmit frame must be in a contiguous buffer, aligned
     58  * on a longword (32-bit) boundary. This means we almost always have to
     59  * do mbuf copies in order to transmit a frame, except in the unlikely
     60  * case where a) the packet fits into a single mbuf, and b) the packet
     61  * is 32-bit aligned within the mbuf's data area. The presence of only
     62  * four descriptor registers means that we can never have more than four
     63  * packets queued for transmission at any one time.
     64  *
     65  * Reception is not much better. The driver has to allocate a single large
     66  * buffer area (up to 64K in size) into which the chip will DMA received
     67  * frames. Because we don't know where within this region received packets
     68  * will begin or end, we have no choice but to copy data from the buffer
     69  * area into mbufs in order to pass the packets up to the higher protocol
     70  * levels.
     71  *
     72  * It's impossible given this rotten design to really achieve decent
     73  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
     74  * some equally overmuscled CPU to drive it.
     75  *
     76  * On the bright side, the 8139 does have a built-in PHY, although
     77  * rather than using an MDIO serial interface like most other NICs, the
     78  * PHY registers are directly accessible through the 8139's register
     79  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
     80  * filter.
     81  *
     82  * The 8129 chip is an older version of the 8139 that uses an external PHY
     83  * chip. The 8129 has a serial MDIO interface for accessing the MII where
     84  * the 8139 lets you directly access the on-board PHY registers. We need
     85  * to select which interface to use depending on the chip type.
     86  */
     87 
     88 #include "opt_inet.h"
     89 #include "opt_ns.h"
     90 #include "bpfilter.h"
     91 #include "rnd.h"
     92 
     93 #include <sys/param.h>
     94 #include <sys/systm.h>
     95 #include <sys/callout.h>
     96 #include <sys/device.h>
     97 #include <sys/sockio.h>
     98 #include <sys/mbuf.h>
     99 #include <sys/malloc.h>
    100 #include <sys/kernel.h>
    101 #include <sys/socket.h>
    102 
    103 #include <net/if.h>
    104 #include <net/if_arp.h>
    105 #include <net/if_ether.h>
    106 #include <net/if_dl.h>
    107 #include <net/if_media.h>
    108 #ifdef INET
    109 #include <netinet/in.h>
    110 #include <netinet/if_inarp.h>
    111 #endif
    112 #ifdef NS
    113 #include <netns/ns.h>
    114 #include <netns/ns_if.h>
    115 #endif
    116 
    117 #if NBPFILTER > 0
    118 #include <net/bpf.h>
    119 #endif
    120 #if NRND > 0
    121 #include <sys/rnd.h>
    122 #endif
    123 
    124 #include <machine/bus.h>
    125 
    126 #include <dev/mii/mii.h>
    127 #include <dev/mii/miivar.h>
    128 
    129 /*
    130  * Default to using PIO access for this driver. On SMP systems,
    131  * there appear to be problems with memory mapped mode: it looks like
    132  * doing too many memory mapped access back to back in rapid succession
    133  * can hang the bus. I'm inclined to blame this on crummy design/construction
    134  * on the part of RealTek. Memory mapped mode does appear to work on
    135  * uniprocessor systems though.
    136  */
    137 
    138 #include <dev/ic/rtl81x9reg.h>
    139 
    140 #if defined DEBUG
    141 #define STATIC
    142 #else
    143 #define STATIC static
    144 #endif
    145 
    146 STATIC int rl_encap		__P((struct rl_softc *, struct mbuf * ));
    147 
    148 STATIC void rl_rxeof		__P((struct rl_softc *));
    149 STATIC void rl_txeof		__P((struct rl_softc *));
    150 STATIC void rl_start		__P((struct ifnet *));
    151 STATIC int rl_ioctl		__P((struct ifnet *, u_long, caddr_t));
    152 STATIC void rl_init		__P((void *));
    153 STATIC void rl_stop		__P((struct rl_softc *));
    154 STATIC void rl_watchdog		__P((struct ifnet *));
    155 STATIC void rl_shutdown		__P((void *));
    156 STATIC int rl_ifmedia_upd	__P((struct ifnet *));
    157 STATIC void rl_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
    158 
    159 STATIC void rl_eeprom_putbyte	__P((struct rl_softc *, int));
    160 STATIC void rl_eeprom_getword	__P((struct rl_softc *, int, u_int16_t *));
    161 STATIC void rl_mii_sync		__P((struct rl_softc *));
    162 STATIC void rl_mii_send		__P((struct rl_softc *, u_int32_t, int));
    163 STATIC int rl_mii_readreg	__P((struct rl_softc *, struct rl_mii_frame *));
    164 STATIC int rl_mii_writereg	__P((struct rl_softc *, struct rl_mii_frame *));
    165 
    166 STATIC int rl_phy_readreg	__P((struct device *, int, int));
    167 STATIC void rl_phy_writereg	__P((struct device *, int, int, int));
    168 STATIC void rl_phy_statchg	__P((struct device *));
    169 STATIC void rl_tick __P((void *));
    170 
    171 STATIC u_int8_t rl_calchash	__P((caddr_t));
    172 STATIC void rl_setmulti		__P((struct rl_softc *));
    173 STATIC int rl_list_tx_init	__P((struct rl_softc *));
    174 
    175 STATIC int rl_ether_ioctl __P((struct ifnet *, u_long, caddr_t));
    176 STATIC int rl_allocsndbuf __P((struct rl_softc *, int));
    177 
    178 
    179 #define EE_SET(x)					\
    180 	CSR_WRITE_1(sc, RL_EECMD,			\
    181 		CSR_READ_1(sc, RL_EECMD) | x)
    182 
    183 #define EE_CLR(x)					\
    184 	CSR_WRITE_1(sc, RL_EECMD,			\
    185 		CSR_READ_1(sc, RL_EECMD) & ~x)
    186 
    187 /*
    188  * Send a read command and address to the EEPROM, check for ACK.
    189  */
    190 STATIC void rl_eeprom_putbyte(sc, addr)
    191 	struct rl_softc		*sc;
    192 	int			addr;
    193 {
    194 	register int		d, i;
    195 
    196 	d = addr | RL_EECMD_READ;
    197 
    198 	/*
    199 	 * Feed in each bit and stobe the clock.
    200 	 */
    201 	for (i = 0x400; i; i >>= 1) {
    202 		if (d & i) {
    203 			EE_SET(RL_EE_DATAIN);
    204 		} else {
    205 			EE_CLR(RL_EE_DATAIN);
    206 		}
    207 		DELAY(100);
    208 		EE_SET(RL_EE_CLK);
    209 		DELAY(150);
    210 		EE_CLR(RL_EE_CLK);
    211 		DELAY(100);
    212 	}
    213 
    214 	return;
    215 }
    216 
    217 /*
    218  * Read a word of data stored in the EEPROM at address 'addr.'
    219  */
    220 STATIC void rl_eeprom_getword(sc, addr, dest)
    221 	struct rl_softc		*sc;
    222 	int			addr;
    223 	u_int16_t		*dest;
    224 {
    225 	register int		i;
    226 	u_int16_t		word = 0;
    227 
    228 	/* Enter EEPROM access mode. */
    229 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
    230 
    231 	/*
    232 	 * Send address of word we want to read.
    233 	 */
    234 	rl_eeprom_putbyte(sc, addr);
    235 
    236 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
    237 
    238 	/*
    239 	 * Start reading bits from EEPROM.
    240 	 */
    241 	for (i = 0x8000; i; i >>= 1) {
    242 		EE_SET(RL_EE_CLK);
    243 		DELAY(100);
    244 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
    245 			word |= i;
    246 		EE_CLR(RL_EE_CLK);
    247 		DELAY(100);
    248 	}
    249 
    250 	/* Turn off EEPROM access mode. */
    251 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
    252 
    253 	*dest = word;
    254 
    255 	return;
    256 }
    257 
    258 /*
    259  * Read a sequence of words from the EEPROM.
    260  */
    261 void rl_read_eeprom(sc, dest, off, cnt, swap)
    262 	struct rl_softc		*sc;
    263 	caddr_t			dest;
    264 	int			off;
    265 	int			cnt;
    266 	int			swap;
    267 {
    268 	int			i;
    269 	u_int16_t		word = 0, *ptr;
    270 
    271 	for (i = 0; i < cnt; i++) {
    272 		rl_eeprom_getword(sc, off + i, &word);
    273 		ptr = (u_int16_t *)(dest + (i * 2));
    274 		if (swap)
    275 			*ptr = ntohs(word);
    276 		else
    277 			*ptr = word;
    278 	}
    279 
    280 	return;
    281 }
    282 
    283 
    284 /*
    285  * MII access routines are provided for the 8129, which
    286  * doesn't have a built-in PHY. For the 8139, we fake things
    287  * up by diverting rl_phy_readreg()/rl_phy_writereg() to the
    288  * direct access PHY registers.
    289  */
    290 #define MII_SET(x)					\
    291 	CSR_WRITE_1(sc, RL_MII,				\
    292 		CSR_READ_1(sc, RL_MII) | x)
    293 
    294 #define MII_CLR(x)					\
    295 	CSR_WRITE_1(sc, RL_MII,				\
    296 		CSR_READ_1(sc, RL_MII) & ~x)
    297 
    298 /*
    299  * Sync the PHYs by setting data bit and strobing the clock 32 times.
    300  */
    301 STATIC void rl_mii_sync(sc)
    302 	struct rl_softc		*sc;
    303 {
    304 	register int		i;
    305 
    306 	MII_SET(RL_MII_DIR|RL_MII_DATAOUT);
    307 
    308 	for (i = 0; i < 32; i++) {
    309 		MII_SET(RL_MII_CLK);
    310 		DELAY(1);
    311 		MII_CLR(RL_MII_CLK);
    312 		DELAY(1);
    313 	}
    314 
    315 	return;
    316 }
    317 
    318 /*
    319  * Clock a series of bits through the MII.
    320  */
    321 STATIC void rl_mii_send(sc, bits, cnt)
    322 	struct rl_softc		*sc;
    323 	u_int32_t		bits;
    324 	int			cnt;
    325 {
    326 	int			i;
    327 
    328 	MII_CLR(RL_MII_CLK);
    329 
    330 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
    331                 if (bits & i) {
    332 			MII_SET(RL_MII_DATAOUT);
    333                 } else {
    334 			MII_CLR(RL_MII_DATAOUT);
    335                 }
    336 		DELAY(1);
    337 		MII_CLR(RL_MII_CLK);
    338 		DELAY(1);
    339 		MII_SET(RL_MII_CLK);
    340 	}
    341 }
    342 
    343 /*
    344  * Read an PHY register through the MII.
    345  */
    346 STATIC int rl_mii_readreg(sc, frame)
    347 	struct rl_softc		*sc;
    348 	struct rl_mii_frame	*frame;
    349 
    350 {
    351 	int			i, ack, s;
    352 
    353 	s = splimp();
    354 
    355 	/*
    356 	 * Set up frame for RX.
    357 	 */
    358 	frame->mii_stdelim = RL_MII_STARTDELIM;
    359 	frame->mii_opcode = RL_MII_READOP;
    360 	frame->mii_turnaround = 0;
    361 	frame->mii_data = 0;
    362 
    363 	CSR_WRITE_2(sc, RL_MII, 0);
    364 
    365 	/*
    366  	 * Turn on data xmit.
    367 	 */
    368 	MII_SET(RL_MII_DIR);
    369 
    370 	rl_mii_sync(sc);
    371 
    372 	/*
    373 	 * Send command/address info.
    374 	 */
    375 	rl_mii_send(sc, frame->mii_stdelim, 2);
    376 	rl_mii_send(sc, frame->mii_opcode, 2);
    377 	rl_mii_send(sc, frame->mii_phyaddr, 5);
    378 	rl_mii_send(sc, frame->mii_regaddr, 5);
    379 
    380 	/* Idle bit */
    381 	MII_CLR((RL_MII_CLK|RL_MII_DATAOUT));
    382 	DELAY(1);
    383 	MII_SET(RL_MII_CLK);
    384 	DELAY(1);
    385 
    386 	/* Turn off xmit. */
    387 	MII_CLR(RL_MII_DIR);
    388 
    389 	/* Check for ack */
    390 	MII_CLR(RL_MII_CLK);
    391 	DELAY(1);
    392 	MII_SET(RL_MII_CLK);
    393 	DELAY(1);
    394 	ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN;
    395 
    396 	/*
    397 	 * Now try reading data bits. If the ack failed, we still
    398 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
    399 	 */
    400 	if (ack) {
    401 		for(i = 0; i < 16; i++) {
    402 			MII_CLR(RL_MII_CLK);
    403 			DELAY(1);
    404 			MII_SET(RL_MII_CLK);
    405 			DELAY(1);
    406 		}
    407 		goto fail;
    408 	}
    409 
    410 	for (i = 0x8000; i; i >>= 1) {
    411 		MII_CLR(RL_MII_CLK);
    412 		DELAY(1);
    413 		if (!ack) {
    414 			if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN)
    415 				frame->mii_data |= i;
    416 			DELAY(1);
    417 		}
    418 		MII_SET(RL_MII_CLK);
    419 		DELAY(1);
    420 	}
    421 
    422 fail:
    423 
    424 	MII_CLR(RL_MII_CLK);
    425 	DELAY(1);
    426 	MII_SET(RL_MII_CLK);
    427 	DELAY(1);
    428 
    429 	splx(s);
    430 
    431 	if (ack)
    432 		return(1);
    433 	return(0);
    434 }
    435 
    436 /*
    437  * Write to a PHY register through the MII.
    438  */
    439 STATIC int rl_mii_writereg(sc, frame)
    440 	struct rl_softc		*sc;
    441 	struct rl_mii_frame	*frame;
    442 
    443 {
    444 	int			s;
    445 
    446 	s = splimp();
    447 	/*
    448 	 * Set up frame for TX.
    449 	 */
    450 
    451 	frame->mii_stdelim = RL_MII_STARTDELIM;
    452 	frame->mii_opcode = RL_MII_WRITEOP;
    453 	frame->mii_turnaround = RL_MII_TURNAROUND;
    454 
    455 	/*
    456  	 * Turn on data output.
    457 	 */
    458 	MII_SET(RL_MII_DIR);
    459 
    460 	rl_mii_sync(sc);
    461 
    462 	rl_mii_send(sc, frame->mii_stdelim, 2);
    463 	rl_mii_send(sc, frame->mii_opcode, 2);
    464 	rl_mii_send(sc, frame->mii_phyaddr, 5);
    465 	rl_mii_send(sc, frame->mii_regaddr, 5);
    466 	rl_mii_send(sc, frame->mii_turnaround, 2);
    467 	rl_mii_send(sc, frame->mii_data, 16);
    468 
    469 	/* Idle bit. */
    470 	MII_SET(RL_MII_CLK);
    471 	DELAY(1);
    472 	MII_CLR(RL_MII_CLK);
    473 	DELAY(1);
    474 
    475 	/*
    476 	 * Turn off xmit.
    477 	 */
    478 	MII_CLR(RL_MII_DIR);
    479 
    480 	splx(s);
    481 
    482 	return(0);
    483 }
    484 
    485 STATIC int rl_phy_readreg(self, phy, reg)
    486 	struct device		*self;
    487 	int			phy, reg;
    488 {
    489 	struct rl_softc		*sc = (void *)self;
    490 	struct rl_mii_frame	frame;
    491 	u_int16_t		rval = 0;
    492 	u_int16_t		rl8139_reg = 0;
    493 
    494 	if (sc->rl_type == RL_8139) {
    495 		if (phy != 7)
    496 			return (0);
    497 
    498 		switch(reg) {
    499 		case MII_BMCR:
    500 			rl8139_reg = RL_BMCR;
    501 			break;
    502 		case MII_BMSR:
    503 			rl8139_reg = RL_BMSR;
    504 			break;
    505 		case MII_ANAR:
    506 			rl8139_reg = RL_ANAR;
    507 			break;
    508 		case MII_ANLPAR:
    509 			rl8139_reg = RL_LPAR;
    510 			break;
    511 		default:
    512 #if 0
    513 			printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
    514 #endif
    515 			return(0);
    516 		}
    517 		rval = CSR_READ_2(sc, rl8139_reg);
    518 		return(rval);
    519 	}
    520 
    521 	bzero((char *)&frame, sizeof(frame));
    522 
    523 	frame.mii_phyaddr = phy;
    524 	frame.mii_regaddr = reg;
    525 	rl_mii_readreg(sc, &frame);
    526 
    527 	return(frame.mii_data);
    528 }
    529 
    530 STATIC void rl_phy_writereg(self, phy, reg, data)
    531 	struct device		*self;
    532 	int			phy, reg;
    533 	int			data;
    534 {
    535 	struct rl_softc		*sc = (void *)self;
    536 	struct rl_mii_frame	frame;
    537 	u_int16_t		rl8139_reg = 0;
    538 
    539 	if (sc->rl_type == RL_8139) {
    540 		if (phy != 7)
    541 			return;
    542 
    543 		switch(reg) {
    544 		case MII_BMCR:
    545 			rl8139_reg = RL_BMCR;
    546 			break;
    547 		case MII_BMSR:
    548 			rl8139_reg = RL_BMSR;
    549 			break;
    550 		case MII_ANAR:
    551 			rl8139_reg = RL_ANAR;
    552 			break;
    553 		case MII_ANLPAR:
    554 			rl8139_reg = RL_LPAR;
    555 			break;
    556 		default:
    557 #if 0
    558 			printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
    559 #endif
    560 			return;
    561 		}
    562 		CSR_WRITE_2(sc, rl8139_reg, data);
    563 		return;
    564 	}
    565 
    566 	bzero((char *)&frame, sizeof(frame));
    567 
    568 	frame.mii_phyaddr = phy;
    569 	frame.mii_regaddr = reg;
    570 	frame.mii_data = data;
    571 
    572 	rl_mii_writereg(sc, &frame);
    573 
    574 	return;
    575 }
    576 
    577 STATIC void
    578 rl_phy_statchg(v)
    579 	struct device *v;
    580 {
    581 
    582 	/* Nothing to do. */
    583 }
    584 
    585 /*
    586  * Calculate CRC of a multicast group address, return the upper 6 bits.
    587  */
    588 STATIC u_int8_t rl_calchash(addr)
    589 	caddr_t			addr;
    590 {
    591 	u_int32_t		crc, carry;
    592 	int			i, j;
    593 	u_int8_t		c;
    594 
    595 	/* Compute CRC for the address value. */
    596 	crc = 0xFFFFFFFF; /* initial value */
    597 
    598 	for (i = 0; i < 6; i++) {
    599 		c = *(addr + i);
    600 		for (j = 0; j < 8; j++) {
    601 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
    602 			crc <<= 1;
    603 			c >>= 1;
    604 			if (carry)
    605 				crc = (crc ^ 0x04c11db6) | carry;
    606 		}
    607 	}
    608 
    609 	/* return the filter bit position */
    610 	return(crc >> 26);
    611 }
    612 
    613 /*
    614  * Program the 64-bit multicast hash filter.
    615  */
    616 STATIC void rl_setmulti(sc)
    617 	struct rl_softc		*sc;
    618 {
    619 	struct ifnet		*ifp;
    620 	int			h = 0;
    621 	u_int32_t		hashes[2] = { 0, 0 };
    622 	u_int32_t		rxfilt;
    623 	int			mcnt = 0;
    624 	struct ether_multi *enm;
    625 	struct ether_multistep step;
    626 
    627 	ifp = &sc->ethercom.ec_if;
    628 
    629 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
    630 
    631 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    632 		rxfilt |= RL_RXCFG_RX_MULTI;
    633 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
    634 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
    635 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
    636 		return;
    637 	}
    638 
    639 	/* first, zot all the existing hash bits */
    640 	CSR_WRITE_4(sc, RL_MAR0, 0);
    641 	CSR_WRITE_4(sc, RL_MAR4, 0);
    642 
    643 	/* now program new ones */
    644 	ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
    645 	while (enm != NULL) {
    646 		h = rl_calchash(enm->enm_addrlo);
    647 		if (h < 32)
    648 			hashes[0] |= (1 << h);
    649 		else
    650 			hashes[1] |= (1 << (h - 32));
    651 		mcnt++;
    652 		ETHER_NEXT_MULTI(step, enm);
    653 	}
    654 
    655 	if (mcnt)
    656 		rxfilt |= RL_RXCFG_RX_MULTI;
    657 	else
    658 		rxfilt &= ~RL_RXCFG_RX_MULTI;
    659 
    660 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
    661 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
    662 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
    663 
    664 	return;
    665 }
    666 
    667 void rl_reset(sc)
    668 	struct rl_softc		*sc;
    669 {
    670 	register int		i;
    671 
    672 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
    673 
    674 	for (i = 0; i < RL_TIMEOUT; i++) {
    675 		DELAY(10);
    676 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
    677 			break;
    678 	}
    679 	if (i == RL_TIMEOUT)
    680 		printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
    681 
    682         return;
    683 }
    684 
    685 /*
    686  * Attach the interface. Allocate softc structures, do ifmedia
    687  * setup and ethernet/BPF attach.
    688  */
    689 void
    690 rl_attach(sc, eaddr)
    691 	struct rl_softc *sc;
    692 	const u_int8_t *eaddr;
    693 {
    694 
    695 	struct ifnet *ifp;
    696 	bus_dma_segment_t dmaseg;
    697 	int error,dmanseg;
    698 	int i;
    699 
    700 	callout_init(&sc->rl_tick_ch);
    701 
    702 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    703 	    RL_RXBUFLEN + 32, NBPG, 0, &dmaseg, 1, &dmanseg,
    704 	    BUS_DMA_NOWAIT)) != 0) {
    705 		printf("%s: can't allocate recv buffer, error = %d\n",
    706 		       sc->sc_dev.dv_xname, error);
    707 		goto fail;
    708 	}
    709 
    710 	if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
    711 	    RL_RXBUFLEN + 32, (caddr_t *)&sc->rl_cdata.rl_rx_buf,
    712 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    713 		printf("%s: can't map recv buffer, error = %d\n",
    714 		       sc->sc_dev.dv_xname, error);
    715 		goto fail;
    716 	}
    717 
    718 	/* Leave a few bytes before the start of the RX ring buffer. */
    719 	sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
    720 	sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t);
    721 
    722 	if ((error = bus_dmamap_create(sc->sc_dmat,
    723 	    RL_RXBUFLEN + 32 - sizeof(u_int64_t), 1,
    724 	    RL_RXBUFLEN + 32 - sizeof(u_int64_t), 0, BUS_DMA_NOWAIT,
    725 	    &sc->recv_dmamap)) != 0) {
    726 		printf("%s: can't create recv buffer DMA map, error = %d\n",
    727 		       sc->sc_dev.dv_xname, error);
    728 		goto fail;
    729 	}
    730 
    731 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
    732 	    sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN + 32 - sizeof(u_int64_t), NULL,
    733 	    BUS_DMA_NOWAIT)) != 0) {
    734 		printf("%s: can't load recv buffer DMA map, error = %d\n",
    735 		       sc->sc_dev.dv_xname, error);
    736 		goto fail;
    737 	}
    738 
    739 	for (i = 0; i < RL_TX_LIST_CNT; i++)
    740 		     if (rl_allocsndbuf(sc, i))
    741 			     goto fail;
    742 
    743 	ifp = &sc->ethercom.ec_if;
    744 	ifp->if_softc = sc;
    745 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    746 	ifp->if_mtu = ETHERMTU;
    747 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    748 	ifp->if_ioctl = rl_ioctl;
    749 #if 0
    750 	ifp->if_output = ether_output;
    751 #endif
    752 	ifp->if_start = rl_start;
    753 	ifp->if_watchdog = rl_watchdog;
    754 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    755 
    756 	/*
    757 	 * Do ifmedia setup.
    758 	 */
    759 	sc->mii.mii_ifp = ifp;
    760 	sc->mii.mii_readreg = rl_phy_readreg;
    761 	sc->mii.mii_writereg = rl_phy_writereg;
    762 	sc->mii.mii_statchg = rl_phy_statchg;
    763 	ifmedia_init(&sc->mii.mii_media, 0, rl_ifmedia_upd, rl_ifmedia_sts);
    764 	mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
    765 				MII_PHY_ANY, MII_OFFSET_ANY, 0);
    766 
    767 	/* Choose a default media. */
    768 	if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
    769 		ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE,
    770 			    0, NULL);
    771 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
    772 	} else {
    773 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
    774 	}
    775 
    776 	/*
    777 	 * Call MI attach routines.
    778 	 */
    779 	if_attach(ifp);
    780 	ether_ifattach(ifp, eaddr);
    781 
    782 #if NBPFILTER > 0
    783 	bpfattach(&sc->ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    784 		  sizeof(struct ether_header));
    785 #endif
    786 	shutdownhook_establish(rl_shutdown, sc);
    787 
    788 fail:
    789 	return;
    790 }
    791 
    792 /*
    793  * Initialize the transmit descriptors.
    794  */
    795 STATIC int rl_list_tx_init(sc)
    796 	struct rl_softc		*sc;
    797 {
    798 	struct rl_chain_data	*cd;
    799 	int			i;
    800 
    801 	cd = &sc->rl_cdata;
    802 	for (i = 0; i < RL_TX_LIST_CNT; i++) {
    803 		cd->rl_tx_chain[i] = NULL;
    804 		CSR_WRITE_4(sc,
    805 		    RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
    806 	}
    807 
    808 	sc->rl_cdata.cur_tx = 0;
    809 	sc->rl_cdata.last_tx = 0;
    810 
    811 	return(0);
    812 }
    813 
    814 /*
    815  * A frame has been uploaded: pass the resulting mbuf chain up to
    816  * the higher level protocols.
    817  *
    818  * You know there's something wrong with a PCI bus-master chip design
    819  * when you have to use m_devget().
    820  *
    821  * The receive operation is badly documented in the datasheet, so I'll
    822  * attempt to document it here. The driver provides a buffer area and
    823  * places its base address in the RX buffer start address register.
    824  * The chip then begins copying frames into the RX buffer. Each frame
    825  * is preceeded by a 32-bit RX status word which specifies the length
    826  * of the frame and certain other status bits. Each frame (starting with
    827  * the status word) is also 32-bit aligned. The frame length is in the
    828  * first 16 bits of the status word; the lower 15 bits correspond with
    829  * the 'rx status register' mentioned in the datasheet.
    830  *
    831  * Note: to make the Alpha happy, the frame payload needs to be aligned
    832  * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
    833  * the ring buffer starting at an address two bytes before the actual
    834  * data location. We can then shave off the first two bytes using m_adj().
    835  * The reason we do this is because m_devget() doesn't let us specify an
    836  * offset into the mbuf storage space, so we have to artificially create
    837  * one. The ring is allocated in such a way that there are a few unused
    838  * bytes of space preceecing it so that it will be safe for us to do the
    839  * 2-byte backstep even if reading from the ring at offset 0.
    840  */
    841 STATIC void rl_rxeof(sc)
    842 	struct rl_softc		*sc;
    843 {
    844         struct ether_header	*eh;
    845         struct mbuf		*m;
    846         struct ifnet		*ifp;
    847 	int			total_len = 0;
    848 	u_int32_t		rxstat;
    849 	caddr_t			rxbufpos;
    850 	int			wrap = 0;
    851 	u_int16_t		cur_rx;
    852 	u_int16_t		limit;
    853 	u_int16_t		rx_bytes = 0, max_bytes;
    854 
    855 	ifp = &sc->ethercom.ec_if;
    856 
    857 	cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
    858 
    859 	/* Do not try to read past this point. */
    860 	limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
    861 
    862 	if (limit < cur_rx)
    863 		max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
    864 	else
    865 		max_bytes = limit - cur_rx;
    866 
    867 	while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
    868 		rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
    869 		rxstat = *(u_int32_t *)rxbufpos;
    870 
    871 		/*
    872 		 * Here's a totally undocumented fact for you. When the
    873 		 * RealTek chip is in the process of copying a packet into
    874 		 * RAM for you, the length will be 0xfff0. If you spot a
    875 		 * packet header with this value, you need to stop. The
    876 		 * datasheet makes absolutely no mention of this and
    877 		 * RealTek should be shot for this.
    878 		 */
    879 		if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED)
    880 			break;
    881 
    882 		if (!(rxstat & RL_RXSTAT_RXOK)) {
    883 			ifp->if_ierrors++;
    884 
    885 			/*
    886 			 * submitted by:[netbsd-pcmcia:00484]
    887 			 *	Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
    888 			 * obtain from:
    889 			 *     FreeBSD if_rl.c rev 1.24->1.25
    890 			 *
    891 			 */
    892 #if 0
    893 			if (rxstat & (RL_RXSTAT_BADSYM|RL_RXSTAT_RUNT|
    894 					RL_RXSTAT_GIANT|RL_RXSTAT_CRCERR|
    895 					RL_RXSTAT_ALIGNERR)) {
    896 			if (rxstat & (RL_RXSTAT_BADSYM|RL_RXSTAT_RUNT|
    897 					RL_RXSTAT_GIANT|RL_RXSTAT_CRCERR|
    898 					RL_RXSTAT_ALIGNERR)) {
    899 				CSR_WRITE_2(sc, RL_COMMAND, RL_CMD_TX_ENB);
    900 				CSR_WRITE_2(sc, RL_COMMAND, RL_CMD_TX_ENB|
    901 							RL_CMD_RX_ENB);
    902 				CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
    903 				CSR_WRITE_4(sc, RL_RXADDR,
    904 					    sc->recv_dmamap->dm_segs[0].ds_addr);
    905 				CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
    906 				cur_rx = 0;
    907 			}
    908 			break;
    909 #else
    910 			rl_init(sc);
    911 			return;
    912 #endif
    913 		}
    914 
    915 		/* No errors; receive the packet. */
    916 		total_len = rxstat >> 16;
    917 		rx_bytes += total_len + 4;
    918 
    919 		/*
    920 		 * XXX The RealTek chip includes the CRC with every
    921 		 * received frame, and there's no way to turn this
    922 		 * behavior off (at least, I can't find anything in
    923 	 	 * the manual that explains how to do it) so we have
    924 		 * to trim off the CRC manually.
    925 		 */
    926 		total_len -= ETHER_CRC_LEN;
    927 
    928 		/*
    929 		 * Avoid trying to read more bytes than we know
    930 		 * the chip has prepared for us.
    931 		 */
    932 		if (rx_bytes > max_bytes)
    933 			break;
    934 
    935 		rxbufpos = sc->rl_cdata.rl_rx_buf +
    936 			((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN);
    937 
    938 		if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
    939 			rxbufpos = sc->rl_cdata.rl_rx_buf;
    940 
    941 		wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
    942 
    943 		if (total_len > wrap) {
    944 			m = m_devget(rxbufpos - RL_ETHER_ALIGN,
    945 			   wrap + RL_ETHER_ALIGN, 0, ifp, NULL);
    946 			if (m == NULL) {
    947 				ifp->if_ierrors++;
    948 				printf("%s: out of mbufs, tried to "
    949 					"copy %d bytes\n", sc->sc_dev.dv_xname, wrap);
    950 			}
    951 			else {
    952 				m_adj(m, RL_ETHER_ALIGN);
    953 				m_copyback(m, wrap, total_len - wrap,
    954 					sc->rl_cdata.rl_rx_buf);
    955 			}
    956 			cur_rx = (total_len - wrap + ETHER_CRC_LEN);
    957 		} else {
    958 			m = m_devget(rxbufpos - RL_ETHER_ALIGN,
    959 			    total_len + RL_ETHER_ALIGN, 0, ifp, NULL);
    960 			if (m == NULL) {
    961 				ifp->if_ierrors++;
    962 				printf("%s: out of mbufs, tried to "
    963 				"copy %d bytes\n", sc->sc_dev.dv_xname, total_len);
    964 			} else
    965 				m_adj(m, RL_ETHER_ALIGN);
    966 			cur_rx += total_len + 4 + ETHER_CRC_LEN;
    967 		}
    968 
    969 		/*
    970 		 * Round up to 32-bit boundary.
    971 		 */
    972 		cur_rx = (cur_rx + 3) & ~3;
    973 		CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
    974 
    975 		if (m == NULL)
    976 			continue;
    977 
    978 		eh = mtod(m, struct ether_header *);
    979 		ifp->if_ipackets++;
    980 
    981 #if NBPFILTER > 0
    982 		/*
    983 		 * Handle BPF listeners. Let the BPF user see the packet, but
    984 		 * don't pass it up to the ether_input() layer unless it's
    985 		 * a broadcast packet, multicast packet, matches our ethernet
    986 		 * address or the interface is in promiscuous mode.
    987 		 */
    988 		if (ifp->if_bpf) {
    989 			bpf_mtap(ifp->if_bpf, m);
    990 			if (ifp->if_flags & IFF_PROMISC &&
    991 				(bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    992 						ETHER_ADDR_LEN) &&
    993 					(eh->ether_dhost[0] & 1) == 0)) {
    994 				m_freem(m);
    995 				continue;
    996 			}
    997 		}
    998 #endif
    999 		/* pass it on. */
   1000 		(*ifp->if_input)(ifp, m);
   1001 	}
   1002 
   1003 	return;
   1004 }
   1005 
   1006 /*
   1007  * A frame was downloaded to the chip. It's safe for us to clean up
   1008  * the list buffers.
   1009  */
   1010 STATIC void rl_txeof(sc)
   1011 	struct rl_softc		*sc;
   1012 {
   1013 	struct ifnet		*ifp;
   1014 	u_int32_t		txstat;
   1015 
   1016 	ifp = &sc->ethercom.ec_if;
   1017 
   1018 	/* Clear the timeout timer. */
   1019 	ifp->if_timer = 0;
   1020 
   1021 	/*
   1022 	 * Go through our tx list and free mbufs for those
   1023 	 * frames that have been uploaded.
   1024 	 */
   1025 	do {
   1026 		txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
   1027 		if (!(txstat & (RL_TXSTAT_TX_OK|
   1028 		    RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
   1029 			break;
   1030 
   1031 		ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
   1032 
   1033 		if (RL_LAST_TXMBUF(sc) != NULL) {
   1034 			RL_LAST_TXMBUF(sc) = NULL;
   1035 		}
   1036 		if (txstat & RL_TXSTAT_TX_OK)
   1037 			ifp->if_opackets++;
   1038 		else {
   1039 			ifp->if_oerrors++;
   1040 			if ((txstat & RL_TXSTAT_TXABRT) ||
   1041 			    (txstat & RL_TXSTAT_OUTOFWIN))
   1042 				CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
   1043 		}
   1044 		RL_INC(sc->rl_cdata.last_tx);
   1045 		ifp->if_flags &= ~IFF_OACTIVE;
   1046 	} while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
   1047 
   1048 	return;
   1049 }
   1050 
   1051 int rl_intr(arg)
   1052 	void			*arg;
   1053 {
   1054 	struct rl_softc		*sc;
   1055 	struct ifnet		*ifp;
   1056 	u_int16_t		status;
   1057 	int handled = 0;
   1058 
   1059 	sc = arg;
   1060 	ifp = &sc->ethercom.ec_if;
   1061 
   1062 	/* Disable interrupts. */
   1063 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
   1064 
   1065 	for (;;) {
   1066 
   1067 		status = CSR_READ_2(sc, RL_ISR);
   1068 		if (status)
   1069 			CSR_WRITE_2(sc, RL_ISR, status);
   1070 
   1071 		handled = 1;
   1072 
   1073 		if ((status & RL_INTRS) == 0)
   1074 			break;
   1075 
   1076 		if (status & RL_ISR_RX_OK)
   1077 			rl_rxeof(sc);
   1078 
   1079 		if (status & RL_ISR_RX_ERR)
   1080 			rl_rxeof(sc);
   1081 
   1082 		if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR))
   1083 			rl_txeof(sc);
   1084 
   1085 		if (status & RL_ISR_SYSTEM_ERR) {
   1086 			rl_reset(sc);
   1087 			rl_init(sc);
   1088 		}
   1089 
   1090 	}
   1091 
   1092 	/* Re-enable interrupts. */
   1093 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
   1094 
   1095 	if (ifp->if_snd.ifq_head != NULL) {
   1096 		rl_start(ifp);
   1097 	}
   1098 
   1099 	return (handled);
   1100 }
   1101 
   1102 STATIC int
   1103 rl_allocsndbuf(sc, idx)
   1104 	struct rl_softc *sc;
   1105 	int idx;
   1106 {
   1107 	struct mbuf *m_new = NULL;
   1108 	int error;
   1109 
   1110 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1111 	if (m_new == NULL) {
   1112 		printf("%s: no memory for tx list", sc->sc_dev.dv_xname);
   1113 		return(1);
   1114 	}
   1115 
   1116 	MCLGET(m_new, M_DONTWAIT);
   1117 	if (!(m_new->m_flags & M_EXT)) {
   1118 		m_freem(m_new);
   1119 		printf("%s: no memory for tx list", sc->sc_dev.dv_xname);
   1120 		return(1);
   1121 	}
   1122 
   1123 	if ((error = bus_dmamap_create(sc->sc_dmat,
   1124 	    MCLBYTES, 1,
   1125 	    MCLBYTES, 0, BUS_DMA_NOWAIT,
   1126 	    &sc->snd_dmamap[idx])) != 0) {
   1127 		printf("%s: can't create snd buffer DMA map, error = %d\n",
   1128 		       sc->sc_dev.dv_xname, error);
   1129 		return (1);
   1130 	}
   1131 
   1132 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->snd_dmamap[idx],
   1133 	    mtod(m_new, caddr_t), MCLBYTES, NULL,
   1134 	    BUS_DMA_NOWAIT)) != 0) {
   1135 		printf("%s: can't load snd buffer DMA map, error = %d\n",
   1136 		       sc->sc_dev.dv_xname, error);
   1137 		return (1);
   1138 	}
   1139 
   1140 	sc->sndbuf[idx] = m_new;
   1141 	return (0);
   1142 }
   1143 
   1144 /*
   1145  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
   1146  * pointers to the fragment pointers.
   1147  */
   1148 STATIC int rl_encap(sc, m_head)
   1149 	struct rl_softc		*sc;
   1150 	struct mbuf		*m_head;
   1151 {
   1152 	struct mbuf		*m_new;
   1153 
   1154 	/*
   1155 	 * The RealTek is brain damaged and wants longword-aligned
   1156 	 * TX buffers, plus we can only have one fragment buffer
   1157 	 * per packet. We have to copy pretty much all the time.
   1158 	 */
   1159 
   1160 	m_new = sc->sndbuf[sc->rl_cdata.cur_tx];
   1161 
   1162 	m_copydata(m_head, 0, m_head->m_pkthdr.len,
   1163 				mtod(m_new, caddr_t));
   1164 	m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
   1165 	m_freem(m_head);
   1166 	m_head = m_new;
   1167 
   1168 	/* Pad frames to at least 60 bytes. */
   1169 	if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) {
   1170 		m_head->m_pkthdr.len +=
   1171 			(RL_MIN_FRAMELEN - m_head->m_pkthdr.len);
   1172 		m_head->m_len = m_head->m_pkthdr.len;
   1173 	}
   1174 
   1175 	RL_CUR_TXMBUF(sc) = m_head;
   1176 
   1177 	return(0);
   1178 }
   1179 
   1180 /*
   1181  * Main transmit routine.
   1182  */
   1183 
   1184 STATIC void rl_start(ifp)
   1185 	struct ifnet		*ifp;
   1186 {
   1187 	struct rl_softc		*sc;
   1188 	struct mbuf		*m_head = NULL;
   1189 
   1190 	sc = ifp->if_softc;
   1191 
   1192 	while(RL_CUR_TXMBUF(sc) == NULL) {
   1193 		IF_DEQUEUE(&ifp->if_snd, m_head);
   1194 		if (m_head == NULL)
   1195 			break;
   1196 
   1197 		rl_encap(sc, m_head);
   1198 
   1199 #if NBPFILTER > 0
   1200 		/*
   1201 		 * If there's a BPF listener, bounce a copy of this frame
   1202 		 * to him.
   1203 		 */
   1204 		if (ifp->if_bpf)
   1205 			bpf_mtap(ifp->if_bpf, RL_CUR_TXMBUF(sc));
   1206 #endif
   1207 		/*
   1208 		 * Transmit the frame.
   1209 	 	 */
   1210 		CSR_WRITE_4(sc, RL_CUR_TXADDR(sc),
   1211 			    sc->snd_dmamap[sc->rl_cdata.cur_tx]->dm_segs[0].ds_addr);
   1212 		CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
   1213 		    RL_TX_EARLYTHRESH | RL_CUR_TXMBUF(sc)->m_pkthdr.len);
   1214 
   1215 		RL_INC(sc->rl_cdata.cur_tx);
   1216 	}
   1217 
   1218 	/*
   1219 	 * We broke out of the loop because all our TX slots are
   1220 	 * full. Mark the NIC as busy until it drains some of the
   1221 	 * packets from the queue.
   1222 	 */
   1223 	if (RL_CUR_TXMBUF(sc) != NULL)
   1224 		ifp->if_flags |= IFF_OACTIVE;
   1225 
   1226 	/*
   1227 	 * Set a timeout in case the chip goes out to lunch.
   1228 	 */
   1229 	ifp->if_timer = 5;
   1230 
   1231 	return;
   1232 }
   1233 
   1234 STATIC void rl_init(xsc)
   1235 	void			*xsc;
   1236 {
   1237 	struct rl_softc		*sc = xsc;
   1238 	struct ifnet		*ifp = &sc->ethercom.ec_if;
   1239 	int			s, i;
   1240 	u_int32_t		rxcfg = 0;
   1241 	u_int16_t		phy_bmcr = 0;
   1242 
   1243 	s = splimp();
   1244 
   1245 	/*
   1246 	 * XXX Hack for the 8139: the built-in autoneg logic's state
   1247 	 * gets reset by rl_init() when we don't want it to. Try
   1248 	 * to preserve it.
   1249 	 */
   1250 	if (sc->rl_type == RL_8139)
   1251 		phy_bmcr = rl_phy_readreg((struct device *)sc, 7, MII_BMCR);
   1252 
   1253 	/*
   1254 	 * Cancel pending I/O and free all RX/TX buffers.
   1255 	 */
   1256 	rl_stop(sc);
   1257 
   1258 	/* Init our MAC address */
   1259 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1260 		CSR_WRITE_1(sc, RL_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
   1261 	}
   1262 
   1263 	/* Init the RX buffer pointer register. */
   1264 	CSR_WRITE_4(sc, RL_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
   1265 
   1266 	/* Init TX descriptors. */
   1267 	rl_list_tx_init(sc);
   1268 
   1269 	/*
   1270 	 * Enable transmit and receive.
   1271 	 */
   1272 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
   1273 
   1274 	/*
   1275 	 * Set the initial TX and RX configuration.
   1276 	 */
   1277 	CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
   1278 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
   1279 
   1280 	/* Set the individual bit to receive frames for this host only. */
   1281 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
   1282 	rxcfg |= RL_RXCFG_RX_INDIV;
   1283 
   1284 	/* If we want promiscuous mode, set the allframes bit. */
   1285 	if (ifp->if_flags & IFF_PROMISC) {
   1286 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
   1287 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
   1288 	} else {
   1289 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
   1290 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
   1291 	}
   1292 
   1293 	/*
   1294 	 * Set capture broadcast bit to capture broadcast frames.
   1295 	 */
   1296 	if (ifp->if_flags & IFF_BROADCAST) {
   1297 		rxcfg |= RL_RXCFG_RX_BROAD;
   1298 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
   1299 	} else {
   1300 		rxcfg &= ~RL_RXCFG_RX_BROAD;
   1301 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
   1302 	}
   1303 
   1304 	/*
   1305 	 * Program the multicast filter, if necessary.
   1306 	 */
   1307 	rl_setmulti(sc);
   1308 
   1309 	/*
   1310 	 * Enable interrupts.
   1311 	 */
   1312 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
   1313 
   1314 	/* Start RX/TX process. */
   1315 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
   1316 
   1317 	/* Enable receiver and transmitter. */
   1318 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
   1319 
   1320 	/* Restore state of BMCR */
   1321 	if (sc->rl_type == RL_8139)
   1322 		rl_phy_writereg((struct device *)sc, 7, MII_BMCR, phy_bmcr);
   1323 
   1324 	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
   1325 
   1326 	/*
   1327 	 * Set current media.
   1328 	 */
   1329 	mii_mediachg(&sc->mii);
   1330 
   1331 	ifp->if_flags |= IFF_RUNNING;
   1332 	ifp->if_flags &= ~IFF_OACTIVE;
   1333 
   1334 	(void)splx(s);
   1335 
   1336 	callout_reset(&sc->rl_tick_ch, hz, rl_tick, sc);
   1337 }
   1338 
   1339 /*
   1340  * Set media options.
   1341  */
   1342 STATIC int rl_ifmedia_upd(ifp)
   1343 	struct ifnet		*ifp;
   1344 {
   1345 	struct rl_softc		*sc;
   1346 	struct ifmedia		*ifm;
   1347 
   1348 	sc = ifp->if_softc;
   1349 	ifm = &sc->mii.mii_media;
   1350 
   1351 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1352 		return(EINVAL);
   1353 
   1354 	return (mii_mediachg(&sc->mii));
   1355 }
   1356 
   1357 /*
   1358  * Report current media status.
   1359  */
   1360 STATIC void rl_ifmedia_sts(ifp, ifmr)
   1361 	struct ifnet		*ifp;
   1362 	struct ifmediareq	*ifmr;
   1363 {
   1364 	struct rl_softc		*sc;
   1365 
   1366 	sc = ifp->if_softc;
   1367 
   1368 	mii_pollstat(&sc->mii);
   1369 	ifmr->ifm_status = sc->mii.mii_media_status;
   1370 	ifmr->ifm_active = sc->mii.mii_media_active;
   1371 }
   1372 
   1373 STATIC int
   1374 rl_ether_ioctl(ifp, cmd, data)
   1375 	struct ifnet *ifp;
   1376 	u_long cmd;
   1377 	caddr_t data;
   1378 {
   1379 	struct ifaddr *ifa = (struct ifaddr *) data;
   1380 	struct rl_softc *sc = ifp->if_softc;
   1381 
   1382 	switch (cmd) {
   1383 	case SIOCSIFADDR:
   1384 		ifp->if_flags |= IFF_UP;
   1385 
   1386 		switch (ifa->ifa_addr->sa_family) {
   1387 #ifdef INET
   1388 		case AF_INET:
   1389 			rl_init(sc);
   1390 			arp_ifinit(ifp, ifa);
   1391 			break;
   1392 #endif
   1393 #ifdef NS
   1394 		case AF_NS:
   1395 		    {
   1396 			 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1397 
   1398 			 if (ns_nullhost(*ina))
   1399 				ina->x_host = *(union ns_host *)
   1400 				    LLADDR(ifp->if_sadl);
   1401 			 else
   1402 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
   1403 				    ifp->if_addrlen);
   1404 			 /* Set new address. */
   1405 			 rl_init(sc);
   1406 			 break;
   1407 		    }
   1408 #endif
   1409 		default:
   1410 			rl_init(sc);
   1411 			break;
   1412 		}
   1413 		break;
   1414 
   1415 	default:
   1416 		return (EINVAL);
   1417 	}
   1418 
   1419 	return (0);
   1420 }
   1421 
   1422 STATIC int rl_ioctl(ifp, command, data)
   1423 	struct ifnet		*ifp;
   1424 	u_long			command;
   1425 	caddr_t			data;
   1426 {
   1427 	struct rl_softc		*sc = ifp->if_softc;
   1428 	struct ifreq		*ifr = (struct ifreq *) data;
   1429 	int			s, error = 0;
   1430 
   1431 	s = splimp();
   1432 
   1433 	switch(command) {
   1434 	case SIOCSIFADDR:
   1435 	case SIOCGIFADDR:
   1436 	case SIOCSIFMTU:
   1437 		error = rl_ether_ioctl(ifp, command, data);
   1438 		break;
   1439 	case SIOCSIFFLAGS:
   1440 		if (ifp->if_flags & IFF_UP) {
   1441 			rl_init(sc);
   1442 		} else {
   1443 			if (ifp->if_flags & IFF_RUNNING)
   1444 				rl_stop(sc);
   1445 		}
   1446 		error = 0;
   1447 		break;
   1448 	case SIOCADDMULTI:
   1449 	case SIOCDELMULTI:
   1450 		rl_setmulti(sc);
   1451 		error = 0;
   1452 		break;
   1453 	case SIOCGIFMEDIA:
   1454 	case SIOCSIFMEDIA:
   1455 		error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
   1456 		break;
   1457 	default:
   1458 		error = EINVAL;
   1459 		break;
   1460 	}
   1461 
   1462 	(void)splx(s);
   1463 
   1464 	return(error);
   1465 }
   1466 
   1467 STATIC void rl_watchdog(ifp)
   1468 	struct ifnet		*ifp;
   1469 {
   1470 	struct rl_softc		*sc;
   1471 
   1472 	sc = ifp->if_softc;
   1473 
   1474 	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
   1475 	ifp->if_oerrors++;
   1476 	rl_txeof(sc);
   1477 	rl_rxeof(sc);
   1478 	rl_init(sc);
   1479 
   1480 	return;
   1481 }
   1482 
   1483 /*
   1484  * Stop the adapter and free any mbufs allocated to the
   1485  * RX and TX lists.
   1486  */
   1487 STATIC void rl_stop(sc)
   1488 	struct rl_softc		*sc;
   1489 {
   1490 	register int		i;
   1491 	struct ifnet		*ifp;
   1492 
   1493 	ifp = &sc->ethercom.ec_if;
   1494 	ifp->if_timer = 0;
   1495 
   1496 	callout_stop(&sc->rl_tick_ch);
   1497 
   1498 	mii_down(&sc->mii);
   1499 
   1500 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
   1501 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
   1502 
   1503 	/*
   1504 	 * Free the TX list buffers.
   1505 	 */
   1506 	for (i = 0; i < RL_TX_LIST_CNT; i++) {
   1507 		if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
   1508 			m_freem(sc->rl_cdata.rl_tx_chain[i]);
   1509 			sc->rl_cdata.rl_tx_chain[i] = NULL;
   1510 			CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000);
   1511 		}
   1512 	}
   1513 
   1514 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1515 
   1516 	return;
   1517 }
   1518 
   1519 /*
   1520  * Stop all chip I/O so that the kernel's probe routines don't
   1521  * get confused by errant DMAs when rebooting.
   1522  */
   1523 STATIC void rl_shutdown(vsc)
   1524 	void			*vsc;
   1525 {
   1526 	struct rl_softc		*sc = (struct rl_softc *)vsc;
   1527 
   1528 	rl_stop(sc);
   1529 
   1530 	return;
   1531 }
   1532 
   1533 STATIC void
   1534 rl_tick(arg)
   1535 	void *arg;
   1536 {
   1537 	struct rl_softc *sc = arg;
   1538 	int s = splnet();
   1539 
   1540 	mii_tick(&sc->mii);
   1541 	splx(s);
   1542 
   1543 	callout_reset(&sc->rl_tick_ch, hz, rl_tick, sc);
   1544 }
   1545