Home | History | Annotate | Line # | Download | only in ic
am7990.c revision 1.13
      1 /*	$NetBSD: am7990.c,v 1.13 1996/03/26 14:54:56 gwr 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 		{
    428 			m_freem(m);
    429 			return;
    430 		}
    431 #endif
    432 	}
    433 #endif
    434 
    435 #ifdef LANCE_REVC_BUG
    436 	/*
    437 	 * The old LANCE (Rev. C) chips have a bug which causes
    438 	 * garbage to be inserted in front of the received packet.
    439 	 * The work-around is to ignore packets with an invalid
    440 	 * destination address (garbage will usually not match).
    441 	 * Of course, this precludes multicast support...
    442 	 */
    443 	if (ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr) &&
    444 	    ETHER_CMP(eh->ether_dhost, etherbroadcastaddr) )
    445 	{
    446 		m_freem(m);
    447 		return;
    448 	}
    449 #endif
    450 
    451 	/* Pass the packet up, with the ether header sort-of removed. */
    452 	m_adj(m, sizeof(struct ether_header));
    453 	ether_input(ifp, eh, m);
    454 }
    455 
    456 integrate void
    457 lerint(sc)
    458 	struct le_softc *sc;
    459 {
    460 	register int bix;
    461 	int rp;
    462 	struct lermd rmd;
    463 
    464 	bix = sc->sc_last_rd;
    465 
    466 	/* Process all buffers with valid data. */
    467 	for (;;) {
    468 		rp = LE_RMDADDR(sc, bix);
    469 		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
    470 
    471 		if (rmd.rmd1_bits & LE_R1_OWN)
    472 			break;
    473 
    474 		if (rmd.rmd1_bits & LE_R1_ERR) {
    475 			if (rmd.rmd1_bits & LE_R1_ENP) {
    476 #ifdef LEDEBUG
    477 				if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
    478 					if (rmd.rmd1_bits & LE_R1_FRAM)
    479 						printf("%s: framing error\n",
    480 						    sc->sc_dev.dv_xname);
    481 					if (rmd.rmd1_bits & LE_R1_CRC)
    482 						printf("%s: crc mismatch\n",
    483 						    sc->sc_dev.dv_xname);
    484 				}
    485 #endif
    486 			} else {
    487 				if (rmd.rmd1_bits & LE_R1_OFLO)
    488 					printf("%s: overflow\n",
    489 					    sc->sc_dev.dv_xname);
    490 			}
    491 			if (rmd.rmd1_bits & LE_R1_BUFF)
    492 				printf("%s: receive buffer error\n",
    493 				    sc->sc_dev.dv_xname);
    494 			ifp->if_ierrors++;
    495 		} else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
    496 		    (LE_R1_STP | LE_R1_ENP)) {
    497 			printf("%s: dropping chained buffer\n",
    498 			    sc->sc_dev.dv_xname);
    499 			ifp->if_ierrors++;
    500 		} else {
    501 #ifdef LEDEBUG
    502 			if (sc->sc_debug)
    503 				recv_print(sc, sc->sc_last_rd);
    504 #endif
    505 			leread(sc, LE_RBUFADDR(sc, bix), (int)rmd.rmd3 - 4);
    506 		}
    507 
    508 		rmd.rmd1_bits = LE_R1_OWN;
    509 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    510 		rmd.rmd3 = 0;
    511 		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
    512 
    513 #ifdef LEDEBUG
    514 		if (sc->sc_debug)
    515 			printf("sc->sc_last_rd = %x, rmd = %x\n",
    516 			    sc->sc_last_rd, rmd);
    517 #endif
    518 
    519 		if (++bix == sc->sc_nrbuf)
    520 			bix = 0;
    521 	}
    522 
    523 	sc->sc_last_rd = bix;
    524 }
    525 
    526 integrate void
    527 letint(sc)
    528 	register struct le_softc *sc;
    529 {
    530 	register int bix;
    531 	struct letmd tmd;
    532 
    533 	bix = sc->sc_first_td;
    534 
    535 	for (;;) {
    536 		if (sc->sc_no_td <= 0)
    537 			break;
    538 
    539 #ifdef LEDEBUG
    540 		if (sc->sc_debug)
    541 			printf("trans tmd = %x\n", tmd);
    542 #endif
    543 
    544 		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    545 		    sizeof(tmd));
    546 
    547 		if (tmd.tmd1_bits & LE_T1_OWN)
    548 			break;
    549 
    550 		ifp->if_flags &= ~IFF_OACTIVE;
    551 
    552 		if (tmd.tmd1_bits & LE_T1_ERR) {
    553 			if (tmd.tmd3 & LE_T3_BUFF)
    554 				printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
    555 			else if (tmd.tmd3 & LE_T3_UFLO)
    556 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
    557 			if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
    558 				lereset(sc);
    559 				return;
    560 			}
    561 			if (tmd.tmd3 & LE_T3_LCAR)
    562 				printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    563 			if (tmd.tmd3 & LE_T3_LCOL)
    564 				ifp->if_collisions++;
    565 			if (tmd.tmd3 & LE_T3_RTRY) {
    566 				printf("%s: excessive collisions, tdr %d\n",
    567 				    sc->sc_dev.dv_xname, tmd.tmd3 & LE_T3_TDR_MASK);
    568 				ifp->if_collisions += 16;
    569 			}
    570 			ifp->if_oerrors++;
    571 		} else {
    572 			if (tmd.tmd1_bits & LE_T1_ONE)
    573 				ifp->if_collisions++;
    574 			else if (tmd.tmd1_bits & LE_T1_MORE)
    575 				/* Real number is unknown. */
    576 				ifp->if_collisions += 2;
    577 			ifp->if_opackets++;
    578 		}
    579 
    580 		if (++bix == sc->sc_ntbuf)
    581 			bix = 0;
    582 
    583 		--sc->sc_no_td;
    584 	}
    585 
    586 	sc->sc_first_td = bix;
    587 
    588 	lestart(ifp);
    589 
    590 	if (sc->sc_no_td == 0)
    591 		ifp->if_timer = 0;
    592 }
    593 
    594 /*
    595  * Controller interrupt.
    596  */
    597 int
    598 leintr(arg)
    599 	register void *arg;
    600 {
    601 	register struct le_softc *sc = arg;
    602 	register u_int16_t isr;
    603 
    604 	isr = lerdcsr(sc, LE_CSR0);
    605 #ifdef LEDEBUG
    606 	if (sc->sc_debug)
    607 		printf("%s: leintr entering with isr=%04x\n",
    608 		    sc->sc_dev.dv_xname, isr);
    609 #endif
    610 	if ((isr & LE_C0_INTR) == 0)
    611 		return (0);
    612 
    613 	lewrcsr(sc, LE_CSR0,
    614 	    isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
    615 		   LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
    616 	if (isr & LE_C0_ERR) {
    617 		if (isr & LE_C0_BABL) {
    618 #ifdef LEDEBUG
    619 			printf("%s: babble\n", sc->sc_dev.dv_xname);
    620 #endif
    621 			ifp->if_oerrors++;
    622 		}
    623 #if 0
    624 		if (isr & LE_C0_CERR) {
    625 			printf("%s: collision error\n", sc->sc_dev.dv_xname);
    626 			ifp->if_collisions++;
    627 		}
    628 #endif
    629 		if (isr & LE_C0_MISS) {
    630 #ifdef LEDEBUG
    631 			printf("%s: missed packet\n", sc->sc_dev.dv_xname);
    632 #endif
    633 			ifp->if_ierrors++;
    634 		}
    635 		if (isr & LE_C0_MERR) {
    636 			printf("%s: memory error\n", sc->sc_dev.dv_xname);
    637 			lereset(sc);
    638 			return (1);
    639 		}
    640 	}
    641 
    642 	if ((isr & LE_C0_RXON) == 0) {
    643 		printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
    644 		ifp->if_ierrors++;
    645 		lereset(sc);
    646 		return (1);
    647 	}
    648 	if ((isr & LE_C0_TXON) == 0) {
    649 		printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
    650 		ifp->if_oerrors++;
    651 		lereset(sc);
    652 		return (1);
    653 	}
    654 
    655 	if (isr & LE_C0_RINT)
    656 		lerint(sc);
    657 	if (isr & LE_C0_TINT)
    658 		letint(sc);
    659 
    660 	return (1);
    661 }
    662 
    663 #undef	ifp
    664 
    665 /*
    666  * Setup output on interface.
    667  * Get another datagram to send off of the interface queue, and map it to the
    668  * interface before starting the output.
    669  * Called only at splimp or interrupt level.
    670  */
    671 void
    672 lestart(ifp)
    673 	register struct ifnet *ifp;
    674 {
    675 	register struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    676 	register int bix;
    677 	register struct mbuf *m;
    678 	struct letmd tmd;
    679 	int rp;
    680 	int len;
    681 
    682 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    683 		return;
    684 
    685 	bix = sc->sc_last_td;
    686 
    687 	for (;;) {
    688 		rp = LE_TMDADDR(sc, bix);
    689 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
    690 
    691 		if (tmd.tmd1_bits & LE_T1_OWN) {
    692 			ifp->if_flags |= IFF_OACTIVE;
    693 			printf("missing buffer, no_td = %d, last_td = %d\n",
    694 			    sc->sc_no_td, sc->sc_last_td);
    695 		}
    696 
    697 		IF_DEQUEUE(&ifp->if_snd, m);
    698 		if (m == 0)
    699 			break;
    700 
    701 #if NBPFILTER > 0
    702 		/*
    703 		 * If BPF is listening on this interface, let it see the packet
    704 		 * before we commit it to the wire.
    705 		 */
    706 		if (ifp->if_bpf)
    707 			bpf_mtap(ifp->if_bpf, m);
    708 #endif
    709 
    710 		/*
    711 		 * Copy the mbuf chain into the transmit buffer.
    712 		 */
    713 		len = leput(sc, LE_TBUFADDR(sc, bix), m);
    714 
    715 #ifdef LEDEBUG
    716 		if (len > ETHERMTU + sizeof(struct ether_header))
    717 			printf("packet length %d\n", len);
    718 #endif
    719 
    720 		ifp->if_timer = 5;
    721 
    722 		/*
    723 		 * Init transmit registers, and set transmit start flag.
    724 		 */
    725 		tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    726 		tmd.tmd2 = -len | LE_XMD2_ONES;
    727 		tmd.tmd3 = 0;
    728 
    729 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
    730 
    731 #ifdef LEDEBUG
    732 		if (sc->sc_debug)
    733 			xmit_print(sc, sc->sc_last_td);
    734 #endif
    735 
    736 		lewrcsr(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
    737 
    738 		if (++bix == sc->sc_ntbuf)
    739 			bix = 0;
    740 
    741 		if (++sc->sc_no_td == sc->sc_ntbuf) {
    742 			ifp->if_flags |= IFF_OACTIVE;
    743 			break;
    744 		}
    745 
    746 	}
    747 
    748 	sc->sc_last_td = bix;
    749 }
    750 
    751 /*
    752  * Process an ioctl request.
    753  */
    754 int
    755 leioctl(ifp, cmd, data)
    756 	register struct ifnet *ifp;
    757 	u_long cmd;
    758 	caddr_t data;
    759 {
    760 	struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    761 	struct ifaddr *ifa = (struct ifaddr *)data;
    762 	struct ifreq *ifr = (struct ifreq *)data;
    763 	int s, error = 0;
    764 
    765 	s = splimp();
    766 
    767 	switch (cmd) {
    768 
    769 	case SIOCSIFADDR:
    770 		ifp->if_flags |= IFF_UP;
    771 
    772 		switch (ifa->ifa_addr->sa_family) {
    773 #ifdef INET
    774 		case AF_INET:
    775 			leinit(sc);
    776 			arp_ifinit(&sc->sc_arpcom, ifa);
    777 			break;
    778 #endif
    779 #ifdef NS
    780 		case AF_NS:
    781 		    {
    782 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    783 
    784 			if (ns_nullhost(*ina))
    785 				ina->x_host =
    786 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    787 			else
    788 				bcopy(ina->x_host.c_host,
    789 				    sc->sc_arpcom.ac_enaddr,
    790 				    sizeof(sc->sc_arpcom.ac_enaddr));
    791 			/* Set new address. */
    792 			leinit(sc);
    793 			break;
    794 		    }
    795 #endif
    796 		default:
    797 			leinit(sc);
    798 			break;
    799 		}
    800 		break;
    801 
    802 #if defined(CCITT) && defined(LLC)
    803 	case SIOCSIFCONF_X25:
    804 		ifp->if_flags |= IFF_UP;
    805 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    806 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    807 		if (error == 0)
    808 			leinit(sc);
    809 		break;
    810 #endif /* CCITT && LLC */
    811 
    812 	case SIOCSIFFLAGS:
    813 		if ((ifp->if_flags & IFF_UP) == 0 &&
    814 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    815 			/*
    816 			 * If interface is marked down and it is running, then
    817 			 * stop it.
    818 			 */
    819 			lestop(sc);
    820 			ifp->if_flags &= ~IFF_RUNNING;
    821 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    822 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    823 			/*
    824 			 * If interface is marked up and it is stopped, then
    825 			 * start it.
    826 			 */
    827 			leinit(sc);
    828 		} else {
    829 			/*
    830 			 * Reset the interface to pick up changes in any other
    831 			 * flags that affect hardware registers.
    832 			 */
    833 			/*lestop(sc);*/
    834 			leinit(sc);
    835 		}
    836 #ifdef LEDEBUG
    837 		if (ifp->if_flags & IFF_DEBUG)
    838 			sc->sc_debug = 1;
    839 		else
    840 			sc->sc_debug = 0;
    841 #endif
    842 		break;
    843 
    844 	case SIOCADDMULTI:
    845 	case SIOCDELMULTI:
    846 		error = (cmd == SIOCADDMULTI) ?
    847 		    ether_addmulti(ifr, &sc->sc_arpcom) :
    848 		    ether_delmulti(ifr, &sc->sc_arpcom);
    849 
    850 		if (error == ENETRESET) {
    851 			/*
    852 			 * Multicast list has changed; set the hardware filter
    853 			 * accordingly.
    854 			 */
    855 			lereset(sc);
    856 			error = 0;
    857 		}
    858 		break;
    859 
    860 	default:
    861 		error = EINVAL;
    862 		break;
    863 	}
    864 
    865 	splx(s);
    866 	return (error);
    867 }
    868 
    869 #ifdef LEDEBUG
    870 void
    871 recv_print(sc, no)
    872 	struct le_softc *sc;
    873 	int no;
    874 {
    875 	struct lermd rmd;
    876 	u_int16_t len;
    877 	struct ether_header eh;
    878 
    879 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
    880 	len = rmd.rmd3;
    881 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    882 	    len);
    883 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    884 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    885 	    sc->sc_dev.dv_xname,
    886 	    rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
    887 	if (len >= sizeof(eh)) {
    888 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
    889 		printf("%s: dst %s", ether_sprintf(eh.ether_dhost));
    890 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    891 		    ntohs(eh.ether_type));
    892 	}
    893 }
    894 
    895 void
    896 xmit_print(sc, no)
    897 	struct le_softc *sc;
    898 	int no;
    899 {
    900 	struct letmd tmd;
    901 	u_int16_t len;
    902 	struct ether_header eh;
    903 
    904 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
    905 	len = -tmd.tmd2;
    906 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    907 	    len);
    908 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    909 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    910 	    sc->sc_dev.dv_xname,
    911 	    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
    912 	if (len >= sizeof(eh)) {
    913 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
    914 		printf("%s: dst %s", ether_sprintf(eh.ether_dhost));
    915 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    916 		    ntohs(eh.ether_type));
    917 	}
    918 }
    919 #endif /* LEDEBUG */
    920 
    921 /*
    922  * Set up the logical address filter.
    923  */
    924 void
    925 lesetladrf(ac, af)
    926 	struct arpcom *ac;
    927 	u_int16_t *af;
    928 {
    929 	struct ifnet *ifp = &ac->ac_if;
    930 	struct ether_multi *enm;
    931 	register u_char *cp, c;
    932 	register u_int32_t crc;
    933 	register int i, len;
    934 	struct ether_multistep step;
    935 
    936 	/*
    937 	 * Set up multicast address filter by passing all multicast addresses
    938 	 * through a crc generator, and then using the high order 6 bits as an
    939 	 * index into the 64 bit logical address filter.  The high order bit
    940 	 * selects the word, while the rest of the bits select the bit within
    941 	 * the word.
    942 	 */
    943 
    944 	if (ifp->if_flags & IFF_PROMISC)
    945 		goto allmulti;
    946 
    947 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    948 	ETHER_FIRST_MULTI(step, ac, enm);
    949 	while (enm != NULL) {
    950 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    951 			/*
    952 			 * We must listen to a range of multicast addresses.
    953 			 * For now, just accept all multicasts, rather than
    954 			 * trying to set only those filter bits needed to match
    955 			 * the range.  (At this time, the only use of address
    956 			 * ranges is for IP multicast routing, for which the
    957 			 * range is big enough to require all bits set.)
    958 			 */
    959 			goto allmulti;
    960 		}
    961 
    962 		cp = enm->enm_addrlo;
    963 		crc = 0xffffffff;
    964 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    965 			c = *cp++;
    966 			for (i = 8; --i >= 0;) {
    967 				if ((crc & 0x01) ^ (c & 0x01)) {
    968 					crc >>= 1;
    969 					crc ^= 0xedb88320;
    970 				} else
    971 					crc >>= 1;
    972 				c >>= 1;
    973 			}
    974 		}
    975 		/* Just want the 6 most significant bits. */
    976 		crc >>= 26;
    977 
    978 		/* Set the corresponding bit in the filter. */
    979 		af[crc >> 4] |= 1 << (crc & 0xf);
    980 
    981 		ETHER_NEXT_MULTI(step, enm);
    982 	}
    983 	ifp->if_flags &= ~IFF_ALLMULTI;
    984 	return;
    985 
    986 allmulti:
    987 	ifp->if_flags |= IFF_ALLMULTI;
    988 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    989 }
    990 
    991 
    992 /*
    993  * Routines for accessing the transmit and receive buffers.
    994  * The various CPU and adapter configurations supported by this
    995  * driver require three different access methods for buffers
    996  * and descriptors:
    997  *	(1) contig (contiguous data; no padding),
    998  *	(2) gap2 (two bytes of data followed by two bytes of padding),
    999  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1000  */
   1001 
   1002 #ifdef LE_NEED_BUF_CONTIG
   1003 /*
   1004  * contig: contiguous data with no padding.
   1005  *
   1006  * Buffers may have any alignment.
   1007  */
   1008 
   1009 integrate void
   1010 copytobuf_contig(sc, from, boff, len)
   1011 	struct le_softc *sc;
   1012 	void *from;
   1013 	int boff, len;
   1014 {
   1015 	volatile caddr_t buf = sc->sc_mem;
   1016 
   1017 	/*
   1018 	 * Just call bcopy() to do the work.
   1019 	 */
   1020 	bcopy(from, buf + boff, len);
   1021 }
   1022 
   1023 integrate void
   1024 copyfrombuf_contig(sc, to, boff, len)
   1025 	struct le_softc *sc;
   1026 	void *to;
   1027 	int boff, len;
   1028 {
   1029 	volatile caddr_t buf = sc->sc_mem;
   1030 
   1031 	/*
   1032 	 * Just call bcopy() to do the work.
   1033 	 */
   1034 	bcopy(buf + boff, to, len);
   1035 }
   1036 
   1037 integrate void
   1038 zerobuf_contig(sc, boff, len)
   1039 	struct le_softc *sc;
   1040 	int boff, len;
   1041 {
   1042 	volatile caddr_t buf = sc->sc_mem;
   1043 
   1044 	/*
   1045 	 * Just let bzero() do the work
   1046 	 */
   1047 	bzero(buf + boff, len);
   1048 }
   1049 #endif /* LE_NEED_BUF_CONTIG */
   1050 
   1051 #ifdef LE_NEED_BUF_GAP2
   1052 /*
   1053  * gap2: two bytes of data followed by two bytes of pad.
   1054  *
   1055  * Buffers must be 4-byte aligned.  The code doesn't worry about
   1056  * doing an extra byte.
   1057  */
   1058 
   1059 integrate void
   1060 copytobuf_gap2(sc, fromv, boff, len)
   1061 	struct le_softc *sc;
   1062 	void *fromv;
   1063 	int boff;
   1064 	register int len;
   1065 {
   1066 	volatile caddr_t buf = sc->sc_mem;
   1067 	register caddr_t from = fromv;
   1068 	register volatile u_int16_t *bptr;
   1069 	register int xfer;
   1070 
   1071 	if (boff & 0x1) {
   1072 		/* handle unaligned first byte */
   1073 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1074 		*bptr = (*from++ << 8) | (*bptr & 0xff);
   1075 		bptr += 2;
   1076 		len--;
   1077 	} else
   1078 		bptr = ((volatile u_int16_t *)buf) + boff;
   1079 	while (len > 1) {
   1080 		*bptr = (from[1] << 8) | (from[0] & 0xff);
   1081 		bptr += 2;
   1082 		from += 2;
   1083 		len -= 2;
   1084 	}
   1085 	if (len == 1)
   1086 		*bptr = (u_int16_t)*from;
   1087 }
   1088 
   1089 integrate void
   1090 copyfrombuf_gap2(sc, tov, boff, len)
   1091 	struct le_softc *sc;
   1092 	void *tov;
   1093 	int boff, len;
   1094 {
   1095 	volatile caddr_t buf = sc->sc_mem;
   1096 	register caddr_t to = tov;
   1097 	register volatile u_int16_t *bptr;
   1098 	register u_int16_t tmp;
   1099 	register int xfer;
   1100 
   1101 	if (boff & 0x1) {
   1102 		/* handle unaligned first byte */
   1103 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1104 		*to++ = (*bptr >> 8) & 0xff;
   1105 		bptr += 2;
   1106 		len--;
   1107 	} else
   1108 		bptr = ((volatile u_int16_t *)buf) + boff;
   1109 	while (len > 1) {
   1110 		tmp = *bptr;
   1111 		*to++ = tmp & 0xff;
   1112 		*to++ = (tmp >> 8) & 0xff;
   1113 		bptr += 2;
   1114 		len -= 2;
   1115 	}
   1116 	if (len == 1)
   1117 		*to = *bptr & 0xff;
   1118 }
   1119 
   1120 integrate void
   1121 zerobuf_gap2(sc, boff, len)
   1122 	struct le_softc *sc;
   1123 	int boff, len;
   1124 {
   1125 	volatile caddr_t buf = sc->sc_mem;
   1126 	register volatile u_int16_t *bptr;
   1127 
   1128 	if ((unsigned)boff & 0x1) {
   1129 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1130 		*bptr &= 0xff;
   1131 		bptr += 2;
   1132 		len--;
   1133 	} else
   1134 		bptr = ((volatile u_int16_t *)buf) + boff;
   1135 	while (len > 0) {
   1136 		*bptr = 0;
   1137 		bptr += 2;
   1138 		len -= 2;
   1139 	}
   1140 }
   1141 #endif /* LE_NEED_BUF_GAP2 */
   1142 
   1143 #ifdef LE_NEED_BUF_GAP16
   1144 /*
   1145  * gap16: 16 bytes of data followed by 16 bytes of pad.
   1146  *
   1147  * Buffers must be 32-byte aligned.
   1148  */
   1149 
   1150 integrate void
   1151 copytobuf_gap16(sc, fromv, boff, len)
   1152 	struct le_softc *sc;
   1153 	void *fromv;
   1154 	int boff;
   1155 	register int len;
   1156 {
   1157 	volatile caddr_t buf = sc->sc_mem;
   1158 	register caddr_t from = fromv;
   1159 	register caddr_t bptr;
   1160 	register int xfer;
   1161 
   1162 	bptr = buf + ((boff << 1) & ~0x1f);
   1163 	boff &= 0xf;
   1164 	xfer = min(len, 16 - boff);
   1165 	while (len > 0) {
   1166 		bcopy(from, bptr + boff, xfer);
   1167 		from += xfer;
   1168 		bptr += 32;
   1169 		boff = 0;
   1170 		len -= xfer;
   1171 		xfer = min(len, 16);
   1172 	}
   1173 }
   1174 
   1175 integrate void
   1176 copyfrombuf_gap16(sc, tov, boff, len)
   1177 	struct le_softc *sc;
   1178 	void *tov;
   1179 	int boff, len;
   1180 {
   1181 	volatile caddr_t buf = sc->sc_mem;
   1182 	register caddr_t to = tov;
   1183 	register caddr_t bptr;
   1184 	register int xfer;
   1185 
   1186 	bptr = buf + ((boff << 1) & ~0x1f);
   1187 	boff &= 0xf;
   1188 	xfer = min(len, 16 - boff);
   1189 	while (len > 0) {
   1190 		bcopy(bptr + boff, to, xfer);
   1191 		to += xfer;
   1192 		bptr += 32;
   1193 		boff = 0;
   1194 		len -= xfer;
   1195 		xfer = min(len, 16);
   1196 	}
   1197 }
   1198 
   1199 integrate void
   1200 zerobuf_gap16(sc, boff, len)
   1201 	struct le_softc *sc;
   1202 	int boff, len;
   1203 {
   1204 	volatile caddr_t buf = sc->sc_mem;
   1205 	register caddr_t bptr;
   1206 	register int xfer;
   1207 
   1208 	bptr = buf + ((boff << 1) & ~0x1f);
   1209 	boff &= 0xf;
   1210 	xfer = min(len, 16 - boff);
   1211 	while (len > 0) {
   1212 		bzero(bptr + boff, xfer);
   1213 		bptr += 32;
   1214 		boff = 0;
   1215 		len -= xfer;
   1216 		xfer = min(len, 16);
   1217 	}
   1218 }
   1219 #endif /* LE_NEED_BUF_GAP16 */
   1220