Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.15
      1 /*	$NetBSD: if_es.c,v 1.15 1996/10/13 03:07:17 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Michael L. Hitch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Michael L. Hitch.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * SMC 91C90 Single-Chip Ethernet Controller
     35  */
     36 
     37 #include "bpfilter.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/buf.h>
     43 #include <sys/protosw.h>
     44 #include <sys/socket.h>
     45 #include <sys/syslog.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/errno.h>
     48 #include <sys/device.h>
     49 
     50 #include <net/if.h>
     51 #include <net/netisr.h>
     52 #include <net/route.h>
     53 
     54 #ifdef INET
     55 #include <netinet/in.h>
     56 #include <netinet/in_systm.h>
     57 #include <netinet/in_var.h>
     58 #include <netinet/ip.h>
     59 #include <netinet/if_ether.h>
     60 #endif
     61 
     62 #ifdef NS
     63 #include <netns/ns.h>
     64 #include <netns/ns_if.h>
     65 #endif
     66 
     67 #include <machine/cpu.h>
     68 #include <machine/mtpr.h>
     69 #include <amiga/amiga/device.h>
     70 #include <amiga/amiga/isr.h>
     71 #include <amiga/dev/zbusvar.h>
     72 #include <amiga/dev/if_esreg.h>
     73 
     74 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     75 
     76 #define	USEPKTBUF
     77 
     78 /*
     79  * Ethernet software status per interface.
     80  *
     81  * Each interface is referenced by a network interface structure,
     82  * es_if, which the routing code uses to locate the interface.
     83  * This structure contains the output queue for the interface, its address, ...
     84  */
     85 struct	es_softc {
     86 	struct	device sc_dev;
     87 	struct	isr sc_isr;
     88 	struct	arpcom sc_arpcom;	/* common Ethernet structures */
     89 	void	*sc_base;		/* base address of board */
     90 	short	sc_iflags;
     91 	unsigned short sc_intctl;
     92 #ifdef ESDEBUG
     93 	int	sc_debug;
     94 	short	sc_intbusy;		/* counter for interrupt rentered */
     95 	short	sc_smcbusy;		/* counter for other rentry checks */
     96 #endif
     97 };
     98 
     99 #if NBPFILTER > 0
    100 #include <net/bpf.h>
    101 #include <net/bpfdesc.h>
    102 #endif
    103 
    104 #ifdef ESDEBUG
    105 /* console error messages */
    106 int	esdebug = 0;
    107 int	estxints = 0;	/* IST_TX with TX enabled */
    108 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    109 int	estxint3 = 0;	/* IST_TX interrupt processed */
    110 int	estxint4 = 0;	/* ~TEMPTY counts */
    111 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    112 void	es_dump_smcregs __P((char *, union smcregs *));
    113 #endif
    114 
    115 int esintr __P((void *));
    116 void esstart __P((struct ifnet *));
    117 void eswatchdog __P((struct ifnet *));
    118 int esioctl __P((struct ifnet *, u_long, caddr_t));
    119 void esrint __P((struct es_softc *));
    120 void estint __P((struct es_softc *));
    121 void esinit __P((struct es_softc *));
    122 void esreset __P((struct es_softc *));
    123 void esstop __P((struct es_softc *));
    124 
    125 int esmatch __P((struct device *, void *, void *));
    126 void esattach __P((struct device *, struct device *, void *));
    127 
    128 struct cfattach es_ca = {
    129 	sizeof(struct es_softc), esmatch, esattach
    130 };
    131 
    132 struct cfdriver es_cd = {
    133 	NULL, "es", DV_IFNET
    134 };
    135 
    136 int
    137 esmatch(parent, match, aux)
    138 	struct device *parent;
    139 	void *match, *aux;
    140 {
    141 	struct zbus_args *zap = aux;
    142 
    143 	/* Ameristar A4066 ethernet card */
    144 	if (zap->manid == 1053 && zap->prodid == 10)
    145 		return(1);
    146 
    147 	return (0);
    148 }
    149 
    150 /*
    151  * Interface exists: make available by filling in network interface
    152  * record.  System will initialize the interface when it is ready
    153  * to accept packets.
    154  */
    155 void
    156 esattach(parent, self, aux)
    157 	struct device *parent, *self;
    158 	void *aux;
    159 {
    160 	struct es_softc *sc = (void *)self;
    161 	struct zbus_args *zap = aux;
    162 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    163 	unsigned long ser;
    164 
    165 	sc->sc_base = zap->va;
    166 
    167 	/*
    168 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    169 	 * (Currently only Ameristar.)
    170 	 */
    171 	sc->sc_arpcom.ac_enaddr[0] = 0x00;
    172 	sc->sc_arpcom.ac_enaddr[1] = 0x00;
    173 	sc->sc_arpcom.ac_enaddr[2] = 0x9f;
    174 
    175 	/*
    176 	 * Serial number for board contains last 3 bytes.
    177 	 */
    178 	ser = (unsigned long) zap->serno;
    179 
    180 	sc->sc_arpcom.ac_enaddr[3] = (ser >> 16) & 0xff;
    181 	sc->sc_arpcom.ac_enaddr[4] = (ser >>  8) & 0xff;
    182 	sc->sc_arpcom.ac_enaddr[5] = (ser      ) & 0xff;
    183 
    184 	/* Initialize ifnet structure. */
    185 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    186 	ifp->if_softc = sc;
    187 	ifp->if_output = ether_output;
    188 	ifp->if_ioctl = esioctl;
    189 	ifp->if_start = esstart;
    190 	ifp->if_watchdog = eswatchdog;
    191 	/* XXX IFF_MULTICAST */
    192 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    193 
    194 	/* Attach the interface. */
    195 	if_attach(ifp);
    196 	ether_ifattach(ifp);
    197 
    198 	/* Print additional info when attached. */
    199 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    200 
    201 #if NBPFILTER > 0
    202 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    203 #endif
    204 
    205 	sc->sc_isr.isr_intr = esintr;
    206 	sc->sc_isr.isr_arg = sc;
    207 	sc->sc_isr.isr_ipl = 2;
    208 	add_isr(&sc->sc_isr);
    209 }
    210 
    211 #ifdef ESDEBUG
    212 void
    213 es_dump_smcregs(where, smc)
    214 	char *where;
    215 	union smcregs *smc;
    216 {
    217 	u_short cur_bank = smc->b0.bsr & BSR_MASK;
    218 
    219 	printf("SMC registers %p from %s bank %04x\n", smc, where,
    220 	    smc->b0.bsr);
    221 	smc->b0.bsr = BSR_BANK0;
    222 	printf("TCR %04x EPHSR %04x RCR %04x ECR %04x MIR %04x MCR %04x\n",
    223 	    SWAP(smc->b0.tcr), SWAP(smc->b0.ephsr), SWAP(smc->b0.rcr),
    224 	    SWAP(smc->b0.ecr), SWAP(smc->b0.mir), SWAP(smc->b0.mcr));
    225 	smc->b1.bsr = BSR_BANK1;
    226 	printf("CR %04x BAR %04x IAR %04x %04x %04x GPR %04x CTR %04x\n",
    227 	    SWAP(smc->b1.cr), SWAP(smc->b1.bar), smc->b1.iar[0], smc->b1.iar[1],
    228 	    smc->b1.iar[2], smc->b1.gpr, SWAP(smc->b1.ctr));
    229 	smc->b2.bsr = BSR_BANK2;
    230 	printf("MMUCR %04x PNR %02x ARR %02x FIFO %04x PTR %04x",
    231 	    SWAP(smc->b2.mmucr), smc->b2.pnr, smc->b2.arr, smc->b2.fifo,
    232 	    SWAP(smc->b2.ptr));
    233 	printf(" DATA %04x %04x IST %02x MSK %02x\n", smc->b2.data,
    234 	    smc->b2.datax, smc->b2.ist, smc->b2.msk);
    235 	smc->b3.bsr = BSR_BANK3;
    236 	printf("MT %04x %04x %04x %04x\n",
    237 	    smc->b3.mt[0], smc->b3.mt[1], smc->b3.mt[2], smc->b3.mt[3]);
    238 	smc->b3.bsr = cur_bank;
    239 }
    240 #endif
    241 
    242 void
    243 esstop(sc)
    244 	struct es_softc* sc;
    245 {
    246 }
    247 
    248 void
    249 esinit(sc)
    250 	struct es_softc *sc;
    251 {
    252 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    253 	union smcregs *smc = sc->sc_base;
    254 	int s;
    255 
    256 	s = splnet();
    257 
    258 #ifdef ESDEBUG
    259 	if (ifp->if_flags & IFF_RUNNING)
    260 		es_dump_smcregs("esinit", smc);
    261 #endif
    262 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    263 	smc->b0.rcr = RCR_EPH_RST;
    264 	smc->b0.rcr = 0;
    265 	smc->b3.bsr = BSR_BANK3;	/* Select bank 3 */
    266 	smc->b3.mt[0] = 0;		/* clear Multicast table */
    267 	smc->b3.mt[1] = 0;
    268 	smc->b3.mt[2] = 0;
    269 	smc->b3.mt[3] = 0;
    270 	/* XXX set Multicast table from Multicast list */
    271 	smc->b1.bsr = BSR_BANK1;	/* Select bank 1 */
    272 	smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
    273 	smc->b1.ctr = CTR_AUTO_RLSE;
    274 	smc->b1.iar[0] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[0]);
    275 	smc->b1.iar[1] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[2]);
    276 	smc->b1.iar[2] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[4]);
    277 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    278 	smc->b2.mmucr = MMUCR_RESET;
    279 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    280 	smc->b0.mcr = SWAP(0x0020);	/* reserve 8K for transmit buffers */
    281 	smc->b0.tcr = TCR_PAD_EN | (TCR_TXENA + TCR_MON_CSN);
    282 	smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN;
    283 	/* XXX add multicast/promiscuous flags */
    284 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    285 	smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
    286 
    287 	/* Interface is now 'running', with no output active. */
    288 	ifp->if_flags |= IFF_RUNNING;
    289 	ifp->if_flags &= ~IFF_OACTIVE;
    290 
    291 	/* Attempt to start output, if any. */
    292 	esstart(ifp);
    293 
    294 	splx(s);
    295 }
    296 
    297 int
    298 esintr(arg)
    299 	void *arg;
    300 {
    301 	struct es_softc *sc = arg;
    302 	u_short intsts, intact;
    303 	union smcregs *smc;
    304 	int s = splnet();
    305 
    306 	smc = sc->sc_base;
    307 #ifdef ESDEBUG
    308 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2 &&
    309 	    sc->sc_arpcom.ac_if.if_flags & IFF_RUNNING) {
    310 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    311 		    smc->b2.bsr);
    312 		smc->b2.bsr = BSR_BANK2;
    313 	}
    314 #endif
    315 	intsts = smc->b2.ist;
    316 	intact = smc->b2.msk & intsts;
    317 	if ((intact) == 0) {
    318 		splx(s);
    319 		return (0);
    320 	}
    321 #ifdef ESDEBUG
    322 	if (esdebug)
    323 		printf ("%s: esintr ist %02x msk %02x",
    324 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
    325 	if (sc->sc_intbusy++) {
    326 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
    327 		panic("esintr re-entered");
    328 	}
    329 	if (sc->sc_smcbusy)
    330 		printf("%s: esintr interrupted busy %d\n", sc->sc_dev.dv_xname,
    331 		    sc->sc_smcbusy);
    332 #endif
    333 	smc->b2.msk = 0;
    334 #ifdef ESDEBUG
    335 	if (esdebug)
    336 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    337 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    338 		    smc->b2.fifo);
    339 #endif
    340 	if (intact & IST_ALLOC) {
    341 		sc->sc_intctl &= ~MSK_ALLOC;
    342 #ifdef ESDEBUG
    343 		if (esdebug || 1)
    344 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
    345 			    intsts);
    346 #endif
    347 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    348 			u_char save_pnr;
    349 #ifdef ESDEBUG
    350 			if (esdebug || 1)
    351 				printf (" arr %02x\n", smc->b2.arr);
    352 #endif
    353 			save_pnr = smc->b2.pnr;
    354 			smc->b2.pnr = smc->b2.arr;
    355 			smc->b2.mmucr = MMUCR_RLSPKT;
    356 			while (smc->b2.mmucr & MMUCR_BUSY)
    357 				;
    358 			smc->b2.pnr = save_pnr;
    359 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    360 		}
    361 #ifdef ESDEBUG
    362 		else if (esdebug || 1)
    363 			printf (" IST_ALLOC with ARR_FAILED?\n");
    364 #endif
    365 	}
    366 #ifdef ESDEBUG
    367 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    368 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    369 		    smc->b2.bsr);
    370 		smc->b2.bsr = BSR_BANK2;
    371 	}
    372 #endif
    373 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    374 		esrint(sc);
    375 	}
    376 #ifdef ESDEBUG
    377 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    378 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    379 		    smc->b2.bsr);
    380 		smc->b2.bsr = BSR_BANK2;
    381 	}
    382 #endif
    383 	if (intact & IST_RX_OVRN) {
    384 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    385 		    intsts);
    386 		smc->b2.ist = ACK_RX_OVRN;
    387 		printf ("->%02x\n", smc->b2.ist);
    388 		sc->sc_arpcom.ac_if.if_ierrors++;
    389 	}
    390 	if (intact & IST_TX_EMPTY) {
    391 		u_short ecr;
    392 #ifdef ESDEBUG
    393 		if (esdebug)
    394 			printf ("%s: TX EMPTY %02x",
    395 			    sc->sc_dev.dv_xname, intsts);
    396 		++estxint5;		/* count # IST_TX_EMPTY ints */
    397 #endif
    398 		smc->b2.ist = ACK_TX_EMPTY;
    399 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    400 #ifdef ESDEBUG
    401 		if (esdebug)
    402 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    403 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    404 			    smc->b2.arr);
    405 #endif
    406 		if (smc->b2.ist & IST_TX) {
    407 			intact |= IST_TX;
    408 #ifdef ESDEBUG
    409 			++estxint2;		/* count # TX after TX_EMPTY */
    410 #endif
    411 		} else {
    412 			smc->b0.bsr = BSR_BANK0;
    413 			ecr = smc->b0.ecr;	/* Get error counters */
    414 			if (ecr & 0xff00)
    415 				sc->sc_arpcom.ac_if.if_collisions += ((ecr >> 8) & 15) +
    416 				    ((ecr >> 11) & 0x1e);
    417 			smc->b2.bsr = BSR_BANK2;
    418 #if 0
    419 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
    420 #endif
    421 		}
    422 	}
    423 	if (intact & IST_TX) {
    424 		u_char tx_pnr, save_pnr;
    425 		u_short save_ptr, ephsr, tcr;
    426 		int n = 0;
    427 #ifdef ESDEBUG
    428 		if (esdebug) {
    429 			printf ("%s: TX INT ist %02x",
    430 			    sc->sc_dev.dv_xname, intsts);
    431 			printf ("->%02x\n", smc->b2.ist);
    432 		}
    433 		++estxint3;			/* count # IST_TX */
    434 #endif
    435 zzzz:
    436 #ifdef ESDEBUG
    437 		++estxint4;			/* count # ~TEMPTY */
    438 #endif
    439 		smc->b0.bsr = BSR_BANK0;
    440 		ephsr = smc->b0.ephsr;		/* get EPHSR */
    441 		tcr = smc->b0.tcr;		/* and TCR */
    442 		smc->b2.bsr = BSR_BANK2;
    443 		save_ptr = smc->b2.ptr;
    444 		save_pnr = smc->b2.pnr;
    445 		tx_pnr = smc->b2.fifo >> 8;	/* pktno from completion fifo */
    446 		smc->b2.pnr = tx_pnr;		/* set TX packet number */
    447 		smc->b2.ptr = PTR_READ;		/* point to status word */
    448 #if 0 /* XXXX */
    449 		printf("%s: esintr TXINT IST %02x PNR %02x(%d)",
    450 		    sc->sc_dev.dv_xname, smc->b2.ist,
    451 		    tx_pnr, n);
    452 		printf(" Status %04x", smc->b2.data);
    453 		printf(" EPHSR %04x\n", ephsr);
    454 #endif
    455 		if ((smc->b2.data & EPHSR_TX_SUC) == 0 && (tcr & TCR_TXENA) == 0) {
    456 			/*
    457 			 * Transmitter was stopped for some error.  Enqueue
    458 			 * the packet again and restart the transmitter.
    459 			 * May need some check to limit the number of retries.
    460 			 */
    461 			smc->b2.mmucr = MMUCR_ENQ_TX;
    462 			smc->b0.bsr = BSR_BANK0;
    463 			smc->b0.tcr |= TCR_TXENA;
    464 			smc->b2.bsr = BSR_BANK2;
    465 			sc->sc_arpcom.ac_if.if_oerrors++;
    466 			sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    467 		} else {
    468 			/*
    469 			 * This shouldn't have happened:  IST_TX indicates
    470 			 * the TX completion FIFO is not empty, but the
    471 			 * status for the packet on the completion FIFO
    472 			 * shows that the transmit was sucessful.  Since
    473 			 * AutoRelease is being used, a sucessful transmit
    474 			 * should not result in a packet on the completion
    475 			 * FIFO.  Also, that packet doesn't seem to want
    476 			 * to be acknowledged.  If this occurs, just reset
    477 			 * the TX FIFOs.
    478 			 */
    479 #if 1
    480 			if (smc->b2.ist & IST_TX_EMPTY) {
    481 				smc->b2.mmucr = MMUCR_RESET_TX;
    482 				sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    483 			}
    484 #endif
    485 #ifdef ESDEBUG
    486 			++estxints;	/* count IST_TX with TX enabled */
    487 #endif
    488 		}
    489 		smc->b2.pnr = save_pnr;
    490 		smc->b2.ptr = save_ptr;
    491 		smc->b2.ist = ACK_TX;
    492 
    493 		if ((smc->b2.fifo & FIFO_TEMPTY) == 0 && n++ < 32) {
    494 #if 0 /* XXXX */
    495 			printf("%s: multiple TX int(%2d) pnr %02x ist %02x fifo %04x",
    496 			    sc->sc_dev.dv_xname, n, tx_pnr, smc->b2.ist, smc->b2.fifo);
    497 			smc->w2.istmsk = ACK_TX << 8;
    498 			printf(" %04x\n", smc->b2.fifo);
    499 #endif
    500 			if (tx_pnr != (smc->b2.fifo >> 8))
    501 				goto zzzz;
    502 		}
    503 	}
    504 	/* output packets */
    505 	estint(sc);
    506 #ifdef ESDEBUG
    507 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    508 		printf("%s: intr+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    509 		    smc->b2.bsr);
    510 		smc->b2.bsr = BSR_BANK2;
    511 	}
    512 #endif
    513 	smc->b2.msk = sc->sc_intctl;
    514 #ifdef ESDEBUG
    515 	if (--sc->sc_intbusy) {
    516 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    517 		panic("esintr busy on exit");
    518 	}
    519 #endif
    520 	splx(s);
    521 	return (1);
    522 }
    523 
    524 void
    525 esrint(sc)
    526 	struct es_softc *sc;
    527 {
    528 	union smcregs *smc = sc->sc_base;
    529 	u_short len;
    530 	short cnt;
    531 	u_short pktctlw, pktlen, *buf;
    532 	volatile u_short *data;
    533 #if 0
    534 	u_long *lbuf;
    535 	volatile u_long *ldata;
    536 #endif
    537 	struct ifnet *ifp;
    538 	struct mbuf *top, **mp, *m;
    539 	struct ether_header *eh;
    540 #ifdef USEPKTBUF
    541 	u_char *b, pktbuf[1530];
    542 #endif
    543 #ifdef ESDEBUG
    544 	int i;
    545 #endif
    546 
    547 #ifdef ESDEBUG
    548 	if (esdebug)
    549 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    550 		    smc->b2.fifo);
    551 	if (sc->sc_smcbusy++) {
    552 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
    553 		panic("esrint re-entered");
    554 	}
    555 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    556 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    557 		    smc->b2.bsr);
    558 		smc->b2.bsr = BSR_BANK2;
    559 	}
    560 #endif
    561 	data = (u_short *)&smc->b2.data;
    562 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
    563 	(void) smc->b2.mmucr;
    564 #ifdef ESDEBUG
    565 	if (esdebug)
    566 		printf ("->%04x", smc->b2.fifo);
    567 #endif
    568 	len = *data;
    569 	len = SWAP(len);	/* Careful of macro side-effects */
    570 #ifdef ESDEBUG
    571 	if (esdebug)
    572 		printf (" length %d", len);
    573 #endif
    574 	smc->b2.ptr = PTR_RCV | (PTR_AUTOINCR + PTR_READ) | SWAP(0x0000);
    575 	(void) smc->b2.mmucr;
    576 	pktctlw = *data;
    577 	pktlen = *data;
    578 	pktctlw = SWAP(pktctlw);
    579 	pktlen = SWAP(pktlen) - 6;
    580 	if (pktctlw & RFSW_ODDFRM)
    581 		pktlen++;
    582 	if (len > 1530) {
    583 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
    584 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
    585 		/* XXX ignore packet, or just truncate? */
    586 #if defined(ESDEBUG) && defined(DDB)
    587 		if ((smc->b2.bsr & BSR_MASK) != BSR_BANK2)
    588 			Debugger();
    589 #endif
    590 		smc->b2.bsr = BSR_BANK2;
    591 		smc->b2.mmucr = MMUCR_REMRLS_RX;
    592 		while (smc->b2.mmucr & MMUCR_BUSY)
    593 			;
    594 		++sc->sc_arpcom.ac_if.if_ierrors;
    595 #ifdef ESDEBUG
    596 		if (--sc->sc_smcbusy) {
    597 			printf("%s: esrintr busy on bad packet exit\n",
    598 			    sc->sc_dev.dv_xname);
    599 			panic("esrintr busy on exit");
    600 		}
    601 #endif
    602 		return;
    603 	}
    604 #ifdef USEPKTBUF
    605 #if 0
    606 	lbuf = (u_long *) pktbuf;
    607 	ldata = (u_long *)data;
    608 	cnt = (len - 4) / 4;
    609 	while (cnt--)
    610 		*lbuf++ = *ldata;
    611 	if ((len - 4) & 2) {
    612 		buf = (u_short *) lbuf;
    613 		*buf = *data;
    614 	}
    615 #else
    616 	buf = (u_short *)pktbuf;
    617 	cnt = (len - 4) / 2;
    618 	while (cnt--)
    619 		*buf++ = *data;
    620 #endif
    621 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    622 	while (smc->b2.mmucr & MMUCR_BUSY)
    623 		;
    624 #ifdef ESDEBUG
    625 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    626 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    627 		/* count input error? */
    628 	}
    629 	if (esdebug) {
    630 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    631 		    smc->b2.fifo);
    632 		for (i = 0; i < pktlen; ++i)
    633 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    634 			    "");
    635 		if (i & 31)
    636 			printf ("\n");
    637 	}
    638 #endif
    639 #else	/* USEPKTBUF */
    640 	/* XXX copy directly from controller to mbuf */
    641 #ifdef ESDEBUG
    642 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    643 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    644 		/* count input error? */
    645 	}
    646 	if (esdebug) {
    647 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    648 		    smc->b2.fifo);
    649 	}
    650 #endif
    651 #endif /* USEPKTBUF */
    652 	ifp = &sc->sc_arpcom.ac_if;
    653 	ifp->if_ipackets++;
    654 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    655 	if (m == NULL)
    656 		return;
    657 	m->m_pkthdr.rcvif = ifp;
    658 	m->m_pkthdr.len = pktlen;
    659 	len = MHLEN;
    660 	top = NULL;
    661 	mp = &top;
    662 #ifdef USEPKTBUF
    663 	b = pktbuf;
    664 #endif
    665 	while (pktlen > 0) {
    666 		if (top) {
    667 			MGET(m, M_DONTWAIT, MT_DATA);
    668 			if (m == 0) {
    669 				m_freem(top);
    670 				return;
    671 			}
    672 			len = MLEN;
    673 		}
    674 		if (pktlen >= MINCLSIZE) {
    675 			MCLGET(m, M_DONTWAIT);
    676 			if (m->m_flags & M_EXT)
    677 				len = MCLBYTES;
    678 		}
    679 		m->m_len = len = min(pktlen, len);
    680 #ifdef USEPKTBUF
    681 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    682 		b += len;
    683 #else	/* USEPKTBUF */
    684 		buf = mtod(m, u_short *);
    685 		cnt = len / 2;
    686 		while (cnt--)
    687 			*buf++ = *data;
    688 		if (len & 1)
    689 			*buf = *data;	/* XXX should be byte store */
    690 #ifdef ESDEBUG
    691 		if (esdebug) {
    692 			buf = mtod(m, u_short *);
    693 			for (i = 0; i < len; ++i)
    694 				printf ("%02x%s", ((u_char *)buf)[i],
    695 				    ((i & 31) == 31) ? "\n" : "");
    696 			if (i & 31)
    697 				printf ("\n");
    698 		}
    699 #endif
    700 #endif	/* USEPKTBUF */
    701 		pktlen -= len;
    702 		*mp = m;
    703 		mp = &m->m_next;
    704 	}
    705 #ifndef USEPKTBUF
    706 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    707 	while (smc->b2.mmucr & MMUCR_BUSY)
    708 		;
    709 #endif
    710 	eh = mtod(top, struct ether_header *);
    711 #if NBPFILTER > 0
    712 	/*
    713 	 * Check if there's a BPF listener on this interface.  If so, hand off
    714 	 * the raw packet to bpf.
    715 	 */
    716 	if (sc->sc_arpcom.ac_if.if_bpf) {
    717 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, top);
    718 
    719 		/*
    720 		 * Note that the interface cannot be in promiscuous mode if
    721 		 * there are no BPF listeners.  And if we are in promiscuous
    722 		 * mode, we have to check if this packet is really ours.
    723 		 */
    724 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
    725 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    726 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    727 			    sizeof(eh->ether_dhost)) != 0) {
    728 			m_freem(top);
    729 			return;
    730 		}
    731 	}
    732 #endif
    733 	top->m_pkthdr.len -= sizeof (*eh);
    734 	top->m_len -= sizeof (*eh);
    735 	top->m_data += sizeof (*eh);
    736 
    737 	ether_input(ifp, eh, top);
    738 #ifdef ESDEBUG
    739 	if (--sc->sc_smcbusy) {
    740 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    741 		panic("esintr busy on exit");
    742 	}
    743 #endif
    744 }
    745 
    746 void
    747 estint(sc)
    748 	struct es_softc *sc;
    749 {
    750 
    751 	esstart(&sc->sc_arpcom.ac_if);
    752 }
    753 
    754 void
    755 esstart(ifp)
    756 	struct ifnet *ifp;
    757 {
    758 	struct es_softc *sc = ifp->if_softc;
    759 	union smcregs *smc = sc->sc_base;
    760 	struct mbuf *m0, *m;
    761 #ifdef USEPKTBUF
    762 	u_short pktbuf[ETHERMTU + 2];
    763 #else
    764 	u_short oddbyte, needbyte;
    765 #endif
    766 	u_short pktctlw, pktlen;
    767 	u_short *buf;
    768 	volatile u_short *data;
    769 #if 0
    770 	u_long *lbuf;
    771 	volatile u_long *ldata;
    772 #endif
    773 	short cnt;
    774 	int i;
    775 	u_char active_pnr;
    776 
    777 	if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    778 	    IFF_RUNNING)
    779 		return;
    780 
    781 #ifdef ESDEBUG
    782 	if (sc->sc_smcbusy++) {
    783 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    784 		panic("esstart re-entred");
    785 	}
    786 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    787 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    788 		    smc->b2.bsr);
    789 		smc->b2.bsr = BSR_BANK2;
    790 	}
    791 #endif
    792 	for (;;) {
    793 #ifdef ESDEBUG
    794 		u_short start_ptr, end_ptr;
    795 #endif
    796 		/*
    797 		 * Sneak a peek at the next packet to get the length
    798 		 * and see if the SMC 91C90 can accept it.
    799 		 */
    800 		m = sc->sc_arpcom.ac_if.if_snd.ifq_head;
    801 		if (!m)
    802 			break;
    803 #ifdef ESDEBUG
    804 		if (esdebug && (m->m_next || m->m_len & 1))
    805 			printf("%s: esstart m_next %p m_len %d\n",
    806 			    sc->sc_dev.dv_xname, m->m_next, m->m_len);
    807 #endif
    808 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    809 			pktlen += m0->m_len;
    810 		pktctlw = 0;
    811 		pktlen += 4;
    812 		if (pktlen & 1)
    813 			++pktlen;	/* control byte after last byte */
    814 		else
    815 			pktlen += 2;	/* control byte after pad byte */
    816 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    817 		for (i = 0; i <= 5; ++i)
    818 			if ((smc->b2.arr & ARR_FAILED) == 0)
    819 				break;
    820 		if (smc->b2.arr & ARR_FAILED) {
    821 			sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
    822 			sc->sc_intctl |= MSK_ALLOC;
    823 			break;
    824 		}
    825 		active_pnr = smc->b2.pnr = smc->b2.arr;
    826 
    827 #ifdef ESDEBUG
    828 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    829 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    830 			    smc->b2.bsr);
    831 		smc->b2.bsr = BSR_BANK2;
    832 		}
    833 #endif
    834 		IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
    835 		smc->b2.ptr = PTR_AUTOINCR;
    836 		(void) smc->b2.mmucr;
    837 		data = (u_short *)&smc->b2.data;
    838 		*data = SWAP(pktctlw);
    839 		*data = SWAP(pktlen);
    840 #ifdef ESDEBUG
    841 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    842 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    843 			    smc->b2.bsr);
    844 			smc->b2.bsr = BSR_BANK2;
    845 		}
    846 #endif
    847 #ifdef USEPKTBUF
    848 		i = 0;
    849 		for (m0 = m; m; m = m->m_next) {
    850 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    851 			i += m->m_len;
    852 		}
    853 
    854 		if (i & 1)	/* Figure out where to put control byte */
    855 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    856 		else
    857 			pktbuf[i/2] = 0;
    858 		pktlen -= 4;
    859 #ifdef ESDEBUG
    860 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
    861 			printf("%s: esstart packet longer than pktbuf\n",
    862 			    sc->sc_dev.dv_xname);
    863 #endif
    864 #if 0 /* doesn't quite work? */
    865 		lbuf = (u_long *)(pktbuf);
    866 		ldata = (u_long *)data;
    867 		cnt = pktlen / 4;
    868 		while(cnt--)
    869 			*ldata = *lbuf++;
    870 		if (pktlen & 2) {
    871 			buf = (u_short *)lbuf;
    872 			*data = *buf;
    873 		}
    874 #else
    875 #ifdef ESDEBUG
    876 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    877 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    878 			    smc->b2.bsr);
    879 			smc->b2.bsr = BSR_BANK2;
    880 		}
    881 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
    882 #endif
    883 		buf = pktbuf;
    884 		cnt = pktlen / 2;
    885 		while (cnt--)
    886 			*data = *buf++;
    887 #ifdef ESDEBUG
    888 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
    889 #endif
    890 #endif
    891 #else	/* USEPKTBUF */
    892 		pktctlw = 0;
    893 		oddbyte = needbyte = 0;
    894 		for (m0 = m; m; m = m->m_next) {
    895 			buf = mtod(m, u_short *);
    896 			cnt = m->m_len / 2;
    897 			if (needbyte) {
    898 				oddbyte |= *buf >> 8;
    899 				*data = oddbyte;
    900 			}
    901 			while (cnt--)
    902 				*data = *buf++;
    903 			if (m->m_len & 1)
    904 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    905 			if (m->m_len & 1 && m->m_next)
    906 				printf("%s: esstart odd byte count in mbuf\n",
    907 				    sc->sc_dev.dv_xname);
    908 		}
    909 		*data = pktctlw;
    910 #endif	/* USEPKTBUF */
    911 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    912 			/*
    913 			 * The bank select register has changed.  This seems
    914 			 * to happen with my A2000/Zeus once in a while.  It
    915 			 * appears that the Ethernet chip resets while
    916 			 * copying the transmit buffer.  Requeue the current
    917 			 * transmit buffer and reinitialize the interface.
    918 			 * The initialize routine will take care of
    919 			 * retransmitting the buffer.  mhitch
    920 			 */
    921 #ifdef DIAGNOSTIC
    922 			printf("%s: esstart+++ BSR not 2: %04x\n",
    923 			    sc->sc_dev.dv_xname, smc->b2.bsr);
    924 #endif
    925 			smc->b2.bsr = BSR_BANK2;
    926 #ifdef ESDEBUG
    927 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
    928 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
    929 			--sc->sc_smcbusy;
    930 #endif
    931 			IF_PREPEND(&sc->sc_arpcom.ac_if.if_snd, m0);
    932 			esinit(sc);	/* It's really hosed - reset */
    933 			return;
    934 		}
    935 		smc->b2.mmucr = MMUCR_ENQ_TX;
    936 		if (smc->b2.pnr != active_pnr)
    937 			printf("%s: esstart - PNR changed %x->%x\n",
    938 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    939 #if NBPFILTER > 0
    940 		if (sc->sc_arpcom.ac_if.if_bpf)
    941 			bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    942 #endif
    943 		m_freem(m0);
    944 		sc->sc_arpcom.ac_if.if_opackets++;	/* move to interrupt? */
    945 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    946 	}
    947 	smc->b2.msk = sc->sc_intctl;
    948 #ifdef ESDEBUG
    949 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    950 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    951 		    smc->b2.bsr);
    952 		smc->b2.bsr = BSR_BANK2;
    953 	}
    954 	if (--sc->sc_smcbusy) {
    955 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    956 		panic("esstart busy on exit");
    957 	}
    958 #endif
    959 }
    960 
    961 int
    962 esioctl(ifp, command, data)
    963 	register struct ifnet *ifp;
    964 	u_long command;
    965 	caddr_t data;
    966 {
    967 	struct es_softc *sc = ifp->if_softc;
    968 	register struct ifaddr *ifa = (struct ifaddr *)data;
    969 	struct ifreq *ifr = (struct ifreq *)data;
    970 	int s, error = 0;
    971 
    972 	s = splnet();
    973 
    974 	switch (command) {
    975 
    976 	case SIOCSIFADDR:
    977 		ifp->if_flags |= IFF_UP;
    978 
    979 		switch (ifa->ifa_addr->sa_family) {
    980 #ifdef INET
    981 		case AF_INET:
    982 			esinit(sc);
    983 			arp_ifinit(&sc->sc_arpcom, ifa);
    984 			break;
    985 #endif
    986 #ifdef NS
    987 		case AF_NS:
    988 		    {
    989 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    990 
    991 			if (ns_nullhost(*ina))
    992 				ina->x_host =
    993 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    994 			else
    995 				bcopy(ina->x_host.c_host,
    996 				    sc->sc_arpcom.ac_enaddr,
    997 				    sizeof(sc->sc_arpcom.ac_enaddr));
    998 			/* Set new address. */
    999 			esinit(sc);
   1000 			break;
   1001 		    }
   1002 #endif
   1003 		default:
   1004 			esinit(sc);
   1005 			break;
   1006 		}
   1007 		break;
   1008 
   1009 	case SIOCSIFFLAGS:
   1010 		/*
   1011 		 * If interface is marked down and it is running, then stop it
   1012 		 */
   1013 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1014 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1015 			/*
   1016 			 * If interface is marked down and it is running, then
   1017 			 * stop it.
   1018 			 */
   1019 			esstop(sc);
   1020 			ifp->if_flags &= ~IFF_RUNNING;
   1021 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1022 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1023 			/*
   1024 			 * If interface is marked up and it is stopped, then
   1025 			 * start it.
   1026 			 */
   1027 			esinit(sc);
   1028 		} else {
   1029 			/*
   1030 			 * Reset the interface to pick up changes in any other
   1031 			 * flags that affect hardware registers.
   1032 			 */
   1033 			esstop(sc);
   1034 			esinit(sc);
   1035 		}
   1036 #ifdef ESDEBUG
   1037 		if (ifp->if_flags & IFF_DEBUG)
   1038 			esdebug = sc->sc_debug = 1;
   1039 		else
   1040 			esdebug = sc->sc_debug = 0;
   1041 #endif
   1042 		break;
   1043 
   1044 	case SIOCADDMULTI:
   1045 	case SIOCDELMULTI:
   1046 		error = (command == SIOCADDMULTI) ?
   1047 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1048 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1049 
   1050 		if (error == ENETRESET) {
   1051 			/*
   1052 			 * Multicast list has changed; set the hardware filter
   1053 			 * accordingly.
   1054 			 */
   1055 			/* XXX */
   1056 			error = 0;
   1057 		}
   1058 		break;
   1059 
   1060 	default:
   1061 		error = EINVAL;
   1062 	}
   1063 
   1064 	splx(s);
   1065 	return (error);
   1066 }
   1067 
   1068 void
   1069 esreset(sc)
   1070 	struct es_softc *sc;
   1071 {
   1072 	int s;
   1073 
   1074 	s = splnet();
   1075 	esstop(sc);
   1076 	esinit(sc);
   1077 	splx(s);
   1078 }
   1079 
   1080 void
   1081 eswatchdog(ifp)
   1082 	struct ifnet *ifp;
   1083 {
   1084 	struct es_softc *sc = ifp->if_softc;
   1085 
   1086 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1087 	++sc->sc_arpcom.ac_if.if_oerrors;
   1088 
   1089 	esreset(sc);
   1090 }
   1091