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