Home | History | Annotate | Line # | Download | only in ic
am7990.c revision 1.16
      1 /*	$NetBSD: am7990.c,v 1.16 1996/04/09 15:21:59 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Ralph Campbell and Rick Macklem.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     40  */
     41 
     42 #include <sys/ioctl.h>
     43 #include <sys/errno.h>
     44 
     45 #ifdef INET
     46 #include <netinet/in_systm.h>
     47 #include <netinet/in_var.h>
     48 #include <netinet/ip.h>
     49 #endif
     50 
     51 #ifdef NS
     52 #include <netns/ns.h>
     53 #include <netns/ns_if.h>
     54 #endif
     55 
     56 #if defined(CCITT) && defined(LLC)
     57 #include <sys/socketvar.h>
     58 #include <netccitt/x25.h>
     59 #include <netccitt/pk.h>
     60 #include <netccitt/pk_var.h>
     61 #include <netccitt/pk_extern.h>
     62 #endif
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #include <net/bpfdesc.h>
     67 #endif
     68 
     69 #ifdef LEDEBUG
     70 void recv_print __P((struct le_softc *, int));
     71 void xmit_print __P((struct le_softc *, int));
     72 #endif
     73 
     74 #define	ifp	(&sc->sc_arpcom.ac_if)
     75 
     76 #ifndef	ETHER_CMP
     77 #define	ETHER_CMP(a, b) bcmp((a), (b), ETHER_ADDR_LEN)
     78 #endif
     79 
     80 void
     81 leconfig(sc)
     82 	struct le_softc *sc;
     83 {
     84 	int mem;
     85 
     86 	/* Make sure the chip is stopped. */
     87 	lestop(sc);
     88 
     89 	/* Initialize ifnet structure. */
     90 	ifp->if_unit = sc->sc_dev.dv_unit;
     91 	ifp->if_start = lestart;
     92 	ifp->if_ioctl = leioctl;
     93 	ifp->if_watchdog = lewatchdog;
     94 	ifp->if_flags =
     95 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
     96 #ifdef LANCE_REVC_BUG
     97 	ifp->if_flags &= ~IFF_MULTICAST;
     98 #endif
     99 
    100 	/* Attach the interface. */
    101 	if_attach(ifp);
    102 	ether_ifattach(ifp);
    103 
    104 #if NBPFILTER > 0
    105 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    106 #endif
    107 
    108 	switch (sc->sc_memsize) {
    109 	case 8192:
    110 		sc->sc_nrbuf = 4;
    111 		sc->sc_ntbuf = 1;
    112 		break;
    113 	case 16384:
    114 		sc->sc_nrbuf = 8;
    115 		sc->sc_ntbuf = 2;
    116 		break;
    117 	case 32768:
    118 		sc->sc_nrbuf = 16;
    119 		sc->sc_ntbuf = 4;
    120 		break;
    121 	case 65536:
    122 		sc->sc_nrbuf = 32;
    123 		sc->sc_ntbuf = 8;
    124 		break;
    125 	default:
    126 		panic("leconfig: weird memory size");
    127 	}
    128 
    129 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    130 	printf("%s: %d receive buffers, %d transmit buffers\n",
    131 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
    132 
    133 	mem = 0;
    134 	sc->sc_initaddr = mem;
    135 	mem += sizeof(struct leinit);
    136 	sc->sc_rmdaddr = mem;
    137 	mem += sizeof(struct lermd) * sc->sc_nrbuf;
    138 	sc->sc_tmdaddr = mem;
    139 	mem += sizeof(struct letmd) * sc->sc_ntbuf;
    140 	sc->sc_rbufaddr = mem;
    141 	mem += LEBLEN * sc->sc_nrbuf;
    142 	sc->sc_tbufaddr = mem;
    143 	mem += LEBLEN * sc->sc_ntbuf;
    144 #ifdef notyet
    145 	if (mem > ...)
    146 		panic(...);
    147 #endif
    148 }
    149 
    150 void
    151 lereset(sc)
    152 	struct le_softc *sc;
    153 {
    154 	int s;
    155 
    156 	s = splimp();
    157 	leinit(sc);
    158 	splx(s);
    159 }
    160 
    161 void
    162 lewatchdog(unit)
    163 	int unit;
    164 {
    165 	struct le_softc *sc = LE_SOFTC(unit);
    166 
    167 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    168 	++ifp->if_oerrors;
    169 
    170 	lereset(sc);
    171 }
    172 
    173 /*
    174  * Set up the initialization block and the descriptor rings.
    175  */
    176 void
    177 lememinit(sc)
    178 	register struct le_softc *sc;
    179 {
    180 	u_long a;
    181 	int bix;
    182 	struct leinit init;
    183 	struct lermd rmd;
    184 	struct letmd tmd;
    185 
    186 #if NBPFILTER > 0
    187 	if (ifp->if_flags & IFF_PROMISC)
    188 		init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
    189 	else
    190 #endif
    191 		init.init_mode = LE_MODE_NORMAL;
    192 	init.init_padr[0] =
    193 	    (sc->sc_arpcom.ac_enaddr[1] << 8) | sc->sc_arpcom.ac_enaddr[0];
    194 	init.init_padr[1] =
    195 	    (sc->sc_arpcom.ac_enaddr[3] << 8) | sc->sc_arpcom.ac_enaddr[2];
    196 	init.init_padr[2] =
    197 	    (sc->sc_arpcom.ac_enaddr[5] << 8) | sc->sc_arpcom.ac_enaddr[4];
    198 	lesetladrf(&sc->sc_arpcom, init.init_ladrf);
    199 
    200 	sc->sc_last_rd = 0;
    201 	sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
    202 
    203 	a = sc->sc_addr + LE_RMDADDR(sc, 0);
    204 	init.init_rdra = a;
    205 	init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
    206 
    207 	a = sc->sc_addr + LE_TMDADDR(sc, 0);
    208 	init.init_tdra = a;
    209 	init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
    210 
    211 	(*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
    212 
    213 	/*
    214 	 * Set up receive ring descriptors.
    215 	 */
    216 	for (bix = 0; bix < sc->sc_nrbuf; bix++) {
    217 		a = sc->sc_addr + LE_RBUFADDR(sc, bix);
    218 		rmd.rmd0 = a;
    219 		rmd.rmd1_hadr = a >> 16;
    220 		rmd.rmd1_bits = LE_R1_OWN;
    221 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    222 		rmd.rmd3 = 0;
    223 		(*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
    224 		    sizeof(rmd));
    225 	}
    226 
    227 	/*
    228 	 * Set up transmit ring descriptors.
    229 	 */
    230 	for (bix = 0; bix < sc->sc_ntbuf; bix++) {
    231 		a = sc->sc_addr + LE_TBUFADDR(sc, bix);
    232 		tmd.tmd0 = a;
    233 		tmd.tmd1_hadr = a >> 16;
    234 		tmd.tmd1_bits = 0;
    235 		tmd.tmd2 = 0 | LE_XMD2_ONES;
    236 		tmd.tmd3 = 0;
    237 		(*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    238 		    sizeof(tmd));
    239 	}
    240 }
    241 
    242 void
    243 lestop(sc)
    244 	struct le_softc *sc;
    245 {
    246 
    247 	lewrcsr(sc, LE_CSR0, LE_C0_STOP);
    248 }
    249 
    250 /*
    251  * Initialization of interface; set up initialization block
    252  * and transmit/receive descriptor rings.
    253  */
    254 void
    255 leinit(sc)
    256 	register struct le_softc *sc;
    257 {
    258 	register int timo;
    259 	u_long a;
    260 
    261 	lewrcsr(sc, LE_CSR0, LE_C0_STOP);
    262 	LE_DELAY(100);
    263 
    264 	/* Set the correct byte swapping mode, etc. */
    265 	lewrcsr(sc, LE_CSR3, sc->sc_conf3);
    266 
    267 	/* Set up LANCE init block. */
    268 	lememinit(sc);
    269 
    270 	/* Give LANCE the physical address of its init block. */
    271 	a = sc->sc_addr + LE_INITADDR(sc);
    272 	lewrcsr(sc, LE_CSR1, a);
    273 	lewrcsr(sc, LE_CSR2, a >> 16);
    274 
    275 	/* Try to initialize the LANCE. */
    276 	LE_DELAY(100);
    277 	lewrcsr(sc, LE_CSR0, LE_C0_INIT);
    278 
    279 	/* Wait for initialization to finish. */
    280 	for (timo = 100000; timo; timo--)
    281 		if (lerdcsr(sc, LE_CSR0) & LE_C0_IDON)
    282 			break;
    283 
    284 	if (lerdcsr(sc, LE_CSR0) & LE_C0_IDON) {
    285 		/* Start the LANCE. */
    286 		lewrcsr(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT | LE_C0_IDON);
    287 		ifp->if_flags |= IFF_RUNNING;
    288 		ifp->if_flags &= ~IFF_OACTIVE;
    289 		ifp->if_timer = 0;
    290 		lestart(ifp);
    291 	} else
    292 		printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
    293 }
    294 
    295 /*
    296  * Routine to copy from mbuf chain to transmit buffer in
    297  * network buffer memory.
    298  */
    299 integrate int
    300 leput(sc, boff, m)
    301 	struct le_softc *sc;
    302 	int boff;
    303 	register struct mbuf *m;
    304 {
    305 	register struct mbuf *n;
    306 	register int len, tlen = 0;
    307 
    308 	for (; m; m = n) {
    309 		len = m->m_len;
    310 		if (len == 0) {
    311 			MFREE(m, n);
    312 			continue;
    313 		}
    314 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
    315 		boff += len;
    316 		tlen += len;
    317 		MFREE(m, n);
    318 	}
    319 	if (tlen < LEMINSIZE) {
    320 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
    321 		tlen = LEMINSIZE;
    322 	}
    323 	return (tlen);
    324 }
    325 
    326 /*
    327  * Pull data off an interface.
    328  * Len is length of data, with local net header stripped.
    329  * We copy the data into mbufs.  When full cluster sized units are present
    330  * we copy into clusters.
    331  */
    332 integrate struct mbuf *
    333 leget(sc, boff, totlen)
    334 	struct le_softc *sc;
    335 	int boff, totlen;
    336 {
    337 	register struct mbuf *m;
    338 	struct mbuf *top, **mp;
    339 	int len, pad;
    340 
    341 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    342 	if (m == 0)
    343 		return (0);
    344 	m->m_pkthdr.rcvif = ifp;
    345 	m->m_pkthdr.len = totlen;
    346 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    347 	m->m_data += pad;
    348 	len = MHLEN - pad;
    349 	top = 0;
    350 	mp = &top;
    351 
    352 	while (totlen > 0) {
    353 		if (top) {
    354 			MGET(m, M_DONTWAIT, MT_DATA);
    355 			if (m == 0) {
    356 				m_freem(top);
    357 				return 0;
    358 			}
    359 			len = MLEN;
    360 		}
    361 		if (top && totlen >= MINCLSIZE) {
    362 			MCLGET(m, M_DONTWAIT);
    363 			if (m->m_flags & M_EXT)
    364 				len = MCLBYTES;
    365 		}
    366 		m->m_len = len = min(totlen, len);
    367 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
    368 		boff += len;
    369 		totlen -= len;
    370 		*mp = m;
    371 		mp = &m->m_next;
    372 	}
    373 
    374 	return (top);
    375 }
    376 
    377 /*
    378  * Pass a packet to the higher levels.
    379  */
    380 integrate void
    381 leread(sc, boff, len)
    382 	register struct le_softc *sc;
    383 	int boff, len;
    384 {
    385 	struct mbuf *m;
    386 	struct ether_header *eh;
    387 
    388 	if (len <= sizeof(struct ether_header) ||
    389 	    len > ETHERMTU + sizeof(struct ether_header)) {
    390 #ifdef LEDEBUG
    391 		printf("%s: invalid packet size %d; dropping\n",
    392 		    sc->sc_dev.dv_xname, len);
    393 #endif
    394 		ifp->if_ierrors++;
    395 		return;
    396 	}
    397 
    398 	/* Pull packet off interface. */
    399 	m = leget(sc, boff, len);
    400 	if (m == 0) {
    401 		ifp->if_ierrors++;
    402 		return;
    403 	}
    404 
    405 	ifp->if_ipackets++;
    406 
    407 	/* We assume that the header fit entirely in one mbuf. */
    408 	eh = mtod(m, struct ether_header *);
    409 
    410 #if NBPFILTER > 0
    411 	/*
    412 	 * Check if there's a BPF listener on this interface.
    413 	 * If so, hand off the raw packet to BPF.
    414 	 */
    415 	if (ifp->if_bpf) {
    416 		bpf_mtap(ifp->if_bpf, m);
    417 
    418 #ifndef LANCE_REVC_BUG
    419 		/*
    420 		 * Note that the interface cannot be in promiscuous mode if
    421 		 * there are no BPF listeners.  And if we are in promiscuous
    422 		 * mode, we have to check if this packet is really ours.
    423 		 */
    424 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    425 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    426 		    ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr)) {
    427 			m_freem(m);
    428 			return;
    429 		}
    430 #endif
    431 	}
    432 #endif
    433 
    434 #ifdef LANCE_REVC_BUG
    435 	/*
    436 	 * The old LANCE (Rev. C) chips have a bug which causes
    437 	 * garbage to be inserted in front of the received packet.
    438 	 * The work-around is to ignore packets with an invalid
    439 	 * destination address (garbage will usually not match).
    440 	 * Of course, this precludes multicast support...
    441 	 */
    442 	if (ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr) &&
    443 	    ETHER_CMP(eh->ether_dhost, etherbroadcastaddr)) {
    444 		m_freem(m);
    445 		return;
    446 	}
    447 #endif
    448 
    449 	/* Pass the packet up, with the ether header sort-of removed. */
    450 	m_adj(m, sizeof(struct ether_header));
    451 	ether_input(ifp, eh, m);
    452 }
    453 
    454 integrate void
    455 lerint(sc)
    456 	struct le_softc *sc;
    457 {
    458 	register int bix;
    459 	int rp;
    460 	struct lermd rmd;
    461 
    462 	bix = sc->sc_last_rd;
    463 
    464 	/* Process all buffers with valid data. */
    465 	for (;;) {
    466 		rp = LE_RMDADDR(sc, bix);
    467 		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
    468 
    469 		if (rmd.rmd1_bits & LE_R1_OWN)
    470 			break;
    471 
    472 		if (rmd.rmd1_bits & LE_R1_ERR) {
    473 			if (rmd.rmd1_bits & LE_R1_ENP) {
    474 #ifdef LEDEBUG
    475 				if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
    476 					if (rmd.rmd1_bits & LE_R1_FRAM)
    477 						printf("%s: framing error\n",
    478 						    sc->sc_dev.dv_xname);
    479 					if (rmd.rmd1_bits & LE_R1_CRC)
    480 						printf("%s: crc mismatch\n",
    481 						    sc->sc_dev.dv_xname);
    482 				}
    483 #endif
    484 			} else {
    485 				if (rmd.rmd1_bits & LE_R1_OFLO)
    486 					printf("%s: overflow\n",
    487 					    sc->sc_dev.dv_xname);
    488 			}
    489 			if (rmd.rmd1_bits & LE_R1_BUFF)
    490 				printf("%s: receive buffer error\n",
    491 				    sc->sc_dev.dv_xname);
    492 			ifp->if_ierrors++;
    493 		} else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
    494 		    (LE_R1_STP | LE_R1_ENP)) {
    495 			printf("%s: dropping chained buffer\n",
    496 			    sc->sc_dev.dv_xname);
    497 			ifp->if_ierrors++;
    498 		} else {
    499 #ifdef LEDEBUG
    500 			if (sc->sc_debug)
    501 				recv_print(sc, sc->sc_last_rd);
    502 #endif
    503 			leread(sc, LE_RBUFADDR(sc, bix), (int)rmd.rmd3 - 4);
    504 		}
    505 
    506 		rmd.rmd1_bits = LE_R1_OWN;
    507 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    508 		rmd.rmd3 = 0;
    509 		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
    510 
    511 #ifdef LEDEBUG
    512 		if (sc->sc_debug)
    513 			printf("sc->sc_last_rd = %x, rmd: "
    514 			       "ladr %04x, hadr %02x, flags %02x, "
    515 			       "bcnt %04x, mcnt %04x\n",
    516 				sc->sc_last_rd,
    517 				rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits,
    518 				rmd.rmd2, rmd.rmd3);
    519 #endif
    520 
    521 		if (++bix == sc->sc_nrbuf)
    522 			bix = 0;
    523 	}
    524 
    525 	sc->sc_last_rd = bix;
    526 }
    527 
    528 integrate void
    529 letint(sc)
    530 	register struct le_softc *sc;
    531 {
    532 	register int bix;
    533 	struct letmd tmd;
    534 
    535 	bix = sc->sc_first_td;
    536 
    537 	for (;;) {
    538 		if (sc->sc_no_td <= 0)
    539 			break;
    540 
    541 #ifdef LEDEBUG
    542 		if (sc->sc_debug)
    543 			printf("trans tmd: "
    544 			       "ladr %04x, hadr %02x, flags %02x, "
    545 			       "bcnt %04x, mcnt %04x\n",
    546 				tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
    547 				tmd.tmd2, tmd.tmd3);
    548 #endif
    549 
    550 		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    551 		    sizeof(tmd));
    552 
    553 		if (tmd.tmd1_bits & LE_T1_OWN)
    554 			break;
    555 
    556 		ifp->if_flags &= ~IFF_OACTIVE;
    557 
    558 		if (tmd.tmd1_bits & LE_T1_ERR) {
    559 			if (tmd.tmd3 & LE_T3_BUFF)
    560 				printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
    561 			else if (tmd.tmd3 & LE_T3_UFLO)
    562 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
    563 			if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
    564 				lereset(sc);
    565 				return;
    566 			}
    567 			if (tmd.tmd3 & LE_T3_LCAR)
    568 				printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    569 			if (tmd.tmd3 & LE_T3_LCOL)
    570 				ifp->if_collisions++;
    571 			if (tmd.tmd3 & LE_T3_RTRY) {
    572 				printf("%s: excessive collisions, tdr %d\n",
    573 				    sc->sc_dev.dv_xname, tmd.tmd3 & LE_T3_TDR_MASK);
    574 				ifp->if_collisions += 16;
    575 			}
    576 			ifp->if_oerrors++;
    577 		} else {
    578 			if (tmd.tmd1_bits & LE_T1_ONE)
    579 				ifp->if_collisions++;
    580 			else if (tmd.tmd1_bits & LE_T1_MORE)
    581 				/* Real number is unknown. */
    582 				ifp->if_collisions += 2;
    583 			ifp->if_opackets++;
    584 		}
    585 
    586 		if (++bix == sc->sc_ntbuf)
    587 			bix = 0;
    588 
    589 		--sc->sc_no_td;
    590 	}
    591 
    592 	sc->sc_first_td = bix;
    593 
    594 	lestart(ifp);
    595 
    596 	if (sc->sc_no_td == 0)
    597 		ifp->if_timer = 0;
    598 }
    599 
    600 /*
    601  * Controller interrupt.
    602  */
    603 int
    604 leintr(arg)
    605 	register void *arg;
    606 {
    607 	register struct le_softc *sc = arg;
    608 	register u_int16_t isr;
    609 
    610 	isr = lerdcsr(sc, LE_CSR0);
    611 #ifdef LEDEBUG
    612 	if (sc->sc_debug)
    613 		printf("%s: leintr entering with isr=%04x\n",
    614 		    sc->sc_dev.dv_xname, isr);
    615 #endif
    616 	if ((isr & LE_C0_INTR) == 0)
    617 		return (0);
    618 
    619 	lewrcsr(sc, LE_CSR0,
    620 	    isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
    621 		   LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
    622 	if (isr & LE_C0_ERR) {
    623 		if (isr & LE_C0_BABL) {
    624 #ifdef LEDEBUG
    625 			printf("%s: babble\n", sc->sc_dev.dv_xname);
    626 #endif
    627 			ifp->if_oerrors++;
    628 		}
    629 #if 0
    630 		if (isr & LE_C0_CERR) {
    631 			printf("%s: collision error\n", sc->sc_dev.dv_xname);
    632 			ifp->if_collisions++;
    633 		}
    634 #endif
    635 		if (isr & LE_C0_MISS) {
    636 #ifdef LEDEBUG
    637 			printf("%s: missed packet\n", sc->sc_dev.dv_xname);
    638 #endif
    639 			ifp->if_ierrors++;
    640 		}
    641 		if (isr & LE_C0_MERR) {
    642 			printf("%s: memory error\n", sc->sc_dev.dv_xname);
    643 			lereset(sc);
    644 			return (1);
    645 		}
    646 	}
    647 
    648 	if ((isr & LE_C0_RXON) == 0) {
    649 		printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
    650 		ifp->if_ierrors++;
    651 		lereset(sc);
    652 		return (1);
    653 	}
    654 	if ((isr & LE_C0_TXON) == 0) {
    655 		printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
    656 		ifp->if_oerrors++;
    657 		lereset(sc);
    658 		return (1);
    659 	}
    660 
    661 	if (isr & LE_C0_RINT)
    662 		lerint(sc);
    663 	if (isr & LE_C0_TINT)
    664 		letint(sc);
    665 
    666 	return (1);
    667 }
    668 
    669 #undef	ifp
    670 
    671 /*
    672  * Setup output on interface.
    673  * Get another datagram to send off of the interface queue, and map it to the
    674  * interface before starting the output.
    675  * Called only at splimp or interrupt level.
    676  */
    677 void
    678 lestart(ifp)
    679 	register struct ifnet *ifp;
    680 {
    681 	register struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    682 	register int bix;
    683 	register struct mbuf *m;
    684 	struct letmd tmd;
    685 	int rp;
    686 	int len;
    687 
    688 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    689 		return;
    690 
    691 	bix = sc->sc_last_td;
    692 
    693 	for (;;) {
    694 		rp = LE_TMDADDR(sc, bix);
    695 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
    696 
    697 		if (tmd.tmd1_bits & LE_T1_OWN) {
    698 			ifp->if_flags |= IFF_OACTIVE;
    699 			printf("missing buffer, no_td = %d, last_td = %d\n",
    700 			    sc->sc_no_td, sc->sc_last_td);
    701 		}
    702 
    703 		IF_DEQUEUE(&ifp->if_snd, m);
    704 		if (m == 0)
    705 			break;
    706 
    707 #if NBPFILTER > 0
    708 		/*
    709 		 * If BPF is listening on this interface, let it see the packet
    710 		 * before we commit it to the wire.
    711 		 */
    712 		if (ifp->if_bpf)
    713 			bpf_mtap(ifp->if_bpf, m);
    714 #endif
    715 
    716 		/*
    717 		 * Copy the mbuf chain into the transmit buffer.
    718 		 */
    719 		len = leput(sc, LE_TBUFADDR(sc, bix), m);
    720 
    721 #ifdef LEDEBUG
    722 		if (len > ETHERMTU + sizeof(struct ether_header))
    723 			printf("packet length %d\n", len);
    724 #endif
    725 
    726 		ifp->if_timer = 5;
    727 
    728 		/*
    729 		 * Init transmit registers, and set transmit start flag.
    730 		 */
    731 		tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    732 		tmd.tmd2 = -len | LE_XMD2_ONES;
    733 		tmd.tmd3 = 0;
    734 
    735 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
    736 
    737 #ifdef LEDEBUG
    738 		if (sc->sc_debug)
    739 			xmit_print(sc, sc->sc_last_td);
    740 #endif
    741 
    742 		lewrcsr(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
    743 
    744 		if (++bix == sc->sc_ntbuf)
    745 			bix = 0;
    746 
    747 		if (++sc->sc_no_td == sc->sc_ntbuf) {
    748 			ifp->if_flags |= IFF_OACTIVE;
    749 			break;
    750 		}
    751 
    752 	}
    753 
    754 	sc->sc_last_td = bix;
    755 }
    756 
    757 /*
    758  * Process an ioctl request.
    759  */
    760 int
    761 leioctl(ifp, cmd, data)
    762 	register struct ifnet *ifp;
    763 	u_long cmd;
    764 	caddr_t data;
    765 {
    766 	struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    767 	struct ifaddr *ifa = (struct ifaddr *)data;
    768 	struct ifreq *ifr = (struct ifreq *)data;
    769 	int s, error = 0;
    770 
    771 	s = splimp();
    772 
    773 	switch (cmd) {
    774 
    775 	case SIOCSIFADDR:
    776 		ifp->if_flags |= IFF_UP;
    777 
    778 		switch (ifa->ifa_addr->sa_family) {
    779 #ifdef INET
    780 		case AF_INET:
    781 			leinit(sc);
    782 			arp_ifinit(&sc->sc_arpcom, ifa);
    783 			break;
    784 #endif
    785 #ifdef NS
    786 		case AF_NS:
    787 		    {
    788 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    789 
    790 			if (ns_nullhost(*ina))
    791 				ina->x_host =
    792 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    793 			else
    794 				bcopy(ina->x_host.c_host,
    795 				    sc->sc_arpcom.ac_enaddr,
    796 				    sizeof(sc->sc_arpcom.ac_enaddr));
    797 			/* Set new address. */
    798 			leinit(sc);
    799 			break;
    800 		    }
    801 #endif
    802 		default:
    803 			leinit(sc);
    804 			break;
    805 		}
    806 		break;
    807 
    808 #if defined(CCITT) && defined(LLC)
    809 	case SIOCSIFCONF_X25:
    810 		ifp->if_flags |= IFF_UP;
    811 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    812 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    813 		if (error == 0)
    814 			leinit(sc);
    815 		break;
    816 #endif /* CCITT && LLC */
    817 
    818 	case SIOCSIFFLAGS:
    819 		if ((ifp->if_flags & IFF_UP) == 0 &&
    820 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    821 			/*
    822 			 * If interface is marked down and it is running, then
    823 			 * stop it.
    824 			 */
    825 			lestop(sc);
    826 			ifp->if_flags &= ~IFF_RUNNING;
    827 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    828 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    829 			/*
    830 			 * If interface is marked up and it is stopped, then
    831 			 * start it.
    832 			 */
    833 			leinit(sc);
    834 		} else {
    835 			/*
    836 			 * Reset the interface to pick up changes in any other
    837 			 * flags that affect hardware registers.
    838 			 */
    839 			/*lestop(sc);*/
    840 			leinit(sc);
    841 		}
    842 #ifdef LEDEBUG
    843 		if (ifp->if_flags & IFF_DEBUG)
    844 			sc->sc_debug = 1;
    845 		else
    846 			sc->sc_debug = 0;
    847 #endif
    848 		break;
    849 
    850 	case SIOCADDMULTI:
    851 	case SIOCDELMULTI:
    852 		error = (cmd == SIOCADDMULTI) ?
    853 		    ether_addmulti(ifr, &sc->sc_arpcom) :
    854 		    ether_delmulti(ifr, &sc->sc_arpcom);
    855 
    856 		if (error == ENETRESET) {
    857 			/*
    858 			 * Multicast list has changed; set the hardware filter
    859 			 * accordingly.
    860 			 */
    861 			lereset(sc);
    862 			error = 0;
    863 		}
    864 		break;
    865 
    866 	default:
    867 		error = EINVAL;
    868 		break;
    869 	}
    870 
    871 	splx(s);
    872 	return (error);
    873 }
    874 
    875 #ifdef LEDEBUG
    876 void
    877 recv_print(sc, no)
    878 	struct le_softc *sc;
    879 	int no;
    880 {
    881 	struct lermd rmd;
    882 	u_int16_t len;
    883 	struct ether_header eh;
    884 
    885 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
    886 	len = rmd.rmd3;
    887 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    888 	    len);
    889 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    890 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    891 	    sc->sc_dev.dv_xname,
    892 	    rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
    893 	if (len >= sizeof(eh)) {
    894 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
    895 		printf("%s: dst %s", sc->sc_dev.dv_xname,
    896 			ether_sprintf(eh.ether_dhost));
    897 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    898 			ntohs(eh.ether_type));
    899 	}
    900 }
    901 
    902 void
    903 xmit_print(sc, no)
    904 	struct le_softc *sc;
    905 	int no;
    906 {
    907 	struct letmd tmd;
    908 	u_int16_t len;
    909 	struct ether_header eh;
    910 
    911 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
    912 	len = -tmd.tmd2;
    913 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    914 	    len);
    915 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    916 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    917 	    sc->sc_dev.dv_xname,
    918 	    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
    919 	if (len >= sizeof(eh)) {
    920 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
    921 		printf("%s: dst %s", sc->sc_dev.dv_xname,
    922 			ether_sprintf(eh.ether_dhost));
    923 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    924 		    ntohs(eh.ether_type));
    925 	}
    926 }
    927 #endif /* LEDEBUG */
    928 
    929 /*
    930  * Set up the logical address filter.
    931  */
    932 void
    933 lesetladrf(ac, af)
    934 	struct arpcom *ac;
    935 	u_int16_t *af;
    936 {
    937 	struct ifnet *ifp = &ac->ac_if;
    938 	struct ether_multi *enm;
    939 	register u_char *cp, c;
    940 	register u_int32_t crc;
    941 	register int i, len;
    942 	struct ether_multistep step;
    943 
    944 	/*
    945 	 * Set up multicast address filter by passing all multicast addresses
    946 	 * through a crc generator, and then using the high order 6 bits as an
    947 	 * index into the 64 bit logical address filter.  The high order bit
    948 	 * selects the word, while the rest of the bits select the bit within
    949 	 * the word.
    950 	 */
    951 
    952 	if (ifp->if_flags & IFF_PROMISC)
    953 		goto allmulti;
    954 
    955 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    956 	ETHER_FIRST_MULTI(step, ac, enm);
    957 	while (enm != NULL) {
    958 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    959 			/*
    960 			 * We must listen to a range of multicast addresses.
    961 			 * For now, just accept all multicasts, rather than
    962 			 * trying to set only those filter bits needed to match
    963 			 * the range.  (At this time, the only use of address
    964 			 * ranges is for IP multicast routing, for which the
    965 			 * range is big enough to require all bits set.)
    966 			 */
    967 			goto allmulti;
    968 		}
    969 
    970 		cp = enm->enm_addrlo;
    971 		crc = 0xffffffff;
    972 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    973 			c = *cp++;
    974 			for (i = 8; --i >= 0;) {
    975 				if ((crc & 0x01) ^ (c & 0x01)) {
    976 					crc >>= 1;
    977 					crc ^= 0xedb88320;
    978 				} else
    979 					crc >>= 1;
    980 				c >>= 1;
    981 			}
    982 		}
    983 		/* Just want the 6 most significant bits. */
    984 		crc >>= 26;
    985 
    986 		/* Set the corresponding bit in the filter. */
    987 		af[crc >> 4] |= 1 << (crc & 0xf);
    988 
    989 		ETHER_NEXT_MULTI(step, enm);
    990 	}
    991 	ifp->if_flags &= ~IFF_ALLMULTI;
    992 	return;
    993 
    994 allmulti:
    995 	ifp->if_flags |= IFF_ALLMULTI;
    996 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    997 }
    998 
    999 
   1000 /*
   1001  * Routines for accessing the transmit and receive buffers.
   1002  * The various CPU and adapter configurations supported by this
   1003  * driver require three different access methods for buffers
   1004  * and descriptors:
   1005  *	(1) contig (contiguous data; no padding),
   1006  *	(2) gap2 (two bytes of data followed by two bytes of padding),
   1007  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1008  */
   1009 
   1010 #ifdef LE_NEED_BUF_CONTIG
   1011 /*
   1012  * contig: contiguous data with no padding.
   1013  *
   1014  * Buffers may have any alignment.
   1015  */
   1016 
   1017 integrate void
   1018 copytobuf_contig(sc, from, boff, len)
   1019 	struct le_softc *sc;
   1020 	void *from;
   1021 	int boff, len;
   1022 {
   1023 	volatile caddr_t buf = sc->sc_mem;
   1024 
   1025 	/*
   1026 	 * Just call bcopy() to do the work.
   1027 	 */
   1028 	bcopy(from, buf + boff, len);
   1029 }
   1030 
   1031 integrate void
   1032 copyfrombuf_contig(sc, to, boff, len)
   1033 	struct le_softc *sc;
   1034 	void *to;
   1035 	int boff, len;
   1036 {
   1037 	volatile caddr_t buf = sc->sc_mem;
   1038 
   1039 	/*
   1040 	 * Just call bcopy() to do the work.
   1041 	 */
   1042 	bcopy(buf + boff, to, len);
   1043 }
   1044 
   1045 integrate void
   1046 zerobuf_contig(sc, boff, len)
   1047 	struct le_softc *sc;
   1048 	int boff, len;
   1049 {
   1050 	volatile caddr_t buf = sc->sc_mem;
   1051 
   1052 	/*
   1053 	 * Just let bzero() do the work
   1054 	 */
   1055 	bzero(buf + boff, len);
   1056 }
   1057 #endif /* LE_NEED_BUF_CONTIG */
   1058 
   1059 #ifdef LE_NEED_BUF_GAP2
   1060 /*
   1061  * gap2: two bytes of data followed by two bytes of pad.
   1062  *
   1063  * Buffers must be 4-byte aligned.  The code doesn't worry about
   1064  * doing an extra byte.
   1065  */
   1066 
   1067 integrate void
   1068 copytobuf_gap2(sc, fromv, boff, len)
   1069 	struct le_softc *sc;
   1070 	void *fromv;
   1071 	int boff;
   1072 	register int len;
   1073 {
   1074 	volatile caddr_t buf = sc->sc_mem;
   1075 	register caddr_t from = fromv;
   1076 	register volatile u_int16_t *bptr;
   1077 
   1078 	if (boff & 0x1) {
   1079 		/* handle unaligned first byte */
   1080 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1081 		*bptr = (*from++ << 8) | (*bptr & 0xff);
   1082 		bptr += 2;
   1083 		len--;
   1084 	} else
   1085 		bptr = ((volatile u_int16_t *)buf) + boff;
   1086 	while (len > 1) {
   1087 		*bptr = (from[1] << 8) | (from[0] & 0xff);
   1088 		bptr += 2;
   1089 		from += 2;
   1090 		len -= 2;
   1091 	}
   1092 	if (len == 1)
   1093 		*bptr = (u_int16_t)*from;
   1094 }
   1095 
   1096 integrate void
   1097 copyfrombuf_gap2(sc, tov, boff, len)
   1098 	struct le_softc *sc;
   1099 	void *tov;
   1100 	int boff, len;
   1101 {
   1102 	volatile caddr_t buf = sc->sc_mem;
   1103 	register caddr_t to = tov;
   1104 	register volatile u_int16_t *bptr;
   1105 	register u_int16_t tmp;
   1106 
   1107 	if (boff & 0x1) {
   1108 		/* handle unaligned first byte */
   1109 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1110 		*to++ = (*bptr >> 8) & 0xff;
   1111 		bptr += 2;
   1112 		len--;
   1113 	} else
   1114 		bptr = ((volatile u_int16_t *)buf) + boff;
   1115 	while (len > 1) {
   1116 		tmp = *bptr;
   1117 		*to++ = tmp & 0xff;
   1118 		*to++ = (tmp >> 8) & 0xff;
   1119 		bptr += 2;
   1120 		len -= 2;
   1121 	}
   1122 	if (len == 1)
   1123 		*to = *bptr & 0xff;
   1124 }
   1125 
   1126 integrate void
   1127 zerobuf_gap2(sc, boff, len)
   1128 	struct le_softc *sc;
   1129 	int boff, len;
   1130 {
   1131 	volatile caddr_t buf = sc->sc_mem;
   1132 	register volatile u_int16_t *bptr;
   1133 
   1134 	if ((unsigned)boff & 0x1) {
   1135 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1136 		*bptr &= 0xff;
   1137 		bptr += 2;
   1138 		len--;
   1139 	} else
   1140 		bptr = ((volatile u_int16_t *)buf) + boff;
   1141 	while (len > 0) {
   1142 		*bptr = 0;
   1143 		bptr += 2;
   1144 		len -= 2;
   1145 	}
   1146 }
   1147 #endif /* LE_NEED_BUF_GAP2 */
   1148 
   1149 #ifdef LE_NEED_BUF_GAP16
   1150 /*
   1151  * gap16: 16 bytes of data followed by 16 bytes of pad.
   1152  *
   1153  * Buffers must be 32-byte aligned.
   1154  */
   1155 
   1156 integrate void
   1157 copytobuf_gap16(sc, fromv, boff, len)
   1158 	struct le_softc *sc;
   1159 	void *fromv;
   1160 	int boff;
   1161 	register int len;
   1162 {
   1163 	volatile caddr_t buf = sc->sc_mem;
   1164 	register caddr_t from = fromv;
   1165 	register caddr_t bptr;
   1166 	register int xfer;
   1167 
   1168 	bptr = buf + ((boff << 1) & ~0x1f);
   1169 	boff &= 0xf;
   1170 	xfer = min(len, 16 - boff);
   1171 	while (len > 0) {
   1172 		bcopy(from, bptr + boff, xfer);
   1173 		from += xfer;
   1174 		bptr += 32;
   1175 		boff = 0;
   1176 		len -= xfer;
   1177 		xfer = min(len, 16);
   1178 	}
   1179 }
   1180 
   1181 integrate void
   1182 copyfrombuf_gap16(sc, tov, boff, len)
   1183 	struct le_softc *sc;
   1184 	void *tov;
   1185 	int boff, len;
   1186 {
   1187 	volatile caddr_t buf = sc->sc_mem;
   1188 	register caddr_t to = tov;
   1189 	register caddr_t bptr;
   1190 	register int xfer;
   1191 
   1192 	bptr = buf + ((boff << 1) & ~0x1f);
   1193 	boff &= 0xf;
   1194 	xfer = min(len, 16 - boff);
   1195 	while (len > 0) {
   1196 		bcopy(bptr + boff, to, xfer);
   1197 		to += xfer;
   1198 		bptr += 32;
   1199 		boff = 0;
   1200 		len -= xfer;
   1201 		xfer = min(len, 16);
   1202 	}
   1203 }
   1204 
   1205 integrate void
   1206 zerobuf_gap16(sc, boff, len)
   1207 	struct le_softc *sc;
   1208 	int boff, len;
   1209 {
   1210 	volatile caddr_t buf = sc->sc_mem;
   1211 	register caddr_t bptr;
   1212 	register int xfer;
   1213 
   1214 	bptr = buf + ((boff << 1) & ~0x1f);
   1215 	boff &= 0xf;
   1216 	xfer = min(len, 16 - boff);
   1217 	while (len > 0) {
   1218 		bzero(bptr + boff, xfer);
   1219 		bptr += 32;
   1220 		boff = 0;
   1221 		len -= xfer;
   1222 		xfer = min(len, 16);
   1223 	}
   1224 }
   1225 #endif /* LE_NEED_BUF_GAP16 */
   1226