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