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