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