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