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