Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.21
      1 /*	$NetBSD: if_es.c,v 1.21 1998/07/05 00:51:06 jonathan 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 #include "opt_ddb.h"
     37 #include "opt_inet.h"
     38 
     39 #include "bpfilter.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/buf.h>
     45 #include <sys/protosw.h>
     46 #include <sys/socket.h>
     47 #include <sys/syslog.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_dl.h>
     54 #include <net/if_ether.h>
     55 
     56 #ifdef INET
     57 #include <netinet/in.h>
     58 #include <netinet/in_systm.h>
     59 #include <netinet/in_var.h>
     60 #include <netinet/ip.h>
     61 #include <netinet/if_inarp.h>
     62 #endif
     63 
     64 #ifdef NS
     65 #include <netns/ns.h>
     66 #include <netns/ns_if.h>
     67 #endif
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/mtpr.h>
     71 #include <amiga/amiga/device.h>
     72 #include <amiga/amiga/isr.h>
     73 #include <amiga/dev/zbusvar.h>
     74 #include <amiga/dev/if_esreg.h>
     75 
     76 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     77 
     78 #define	USEPKTBUF
     79 
     80 /*
     81  * Ethernet software status per interface.
     82  *
     83  * Each interface is referenced by a network interface structure,
     84  * es_if, which the routing code uses to locate the interface.
     85  * This structure contains the output queue for the interface, its address, ...
     86  */
     87 struct	es_softc {
     88 	struct	device sc_dev;
     89 	struct	isr sc_isr;
     90 	struct	ethercom sc_ethercom;	/* common Ethernet structures */
     91 	void	*sc_base;		/* base address of board */
     92 	short	sc_iflags;
     93 	unsigned short sc_intctl;
     94 #ifdef ESDEBUG
     95 	int	sc_debug;
     96 	short	sc_intbusy;		/* counter for interrupt rentered */
     97 	short	sc_smcbusy;		/* counter for other rentry checks */
     98 #endif
     99 };
    100 
    101 #if NBPFILTER > 0
    102 #include <net/bpf.h>
    103 #include <net/bpfdesc.h>
    104 #endif
    105 
    106 #ifdef ESDEBUG
    107 /* console error messages */
    108 int	esdebug = 0;
    109 int	estxints = 0;	/* IST_TX with TX enabled */
    110 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    111 int	estxint3 = 0;	/* IST_TX interrupt processed */
    112 int	estxint4 = 0;	/* ~TEMPTY counts */
    113 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    114 void	es_dump_smcregs __P((char *, union smcregs *));
    115 #endif
    116 
    117 int esintr __P((void *));
    118 void esstart __P((struct ifnet *));
    119 void eswatchdog __P((struct ifnet *));
    120 int esioctl __P((struct ifnet *, u_long, caddr_t));
    121 void esrint __P((struct es_softc *));
    122 void estint __P((struct es_softc *));
    123 void esinit __P((struct es_softc *));
    124 void esreset __P((struct es_softc *));
    125 void esstop __P((struct es_softc *));
    126 
    127 int esmatch __P((struct device *, struct cfdata *, void *));
    128 void esattach __P((struct device *, struct device *, void *));
    129 
    130 struct cfattach es_ca = {
    131 	sizeof(struct es_softc), esmatch, esattach
    132 };
    133 
    134 int
    135 esmatch(parent, cfp, aux)
    136 	struct device *parent;
    137 	struct cfdata *cfp;
    138 	void *aux;
    139 {
    140 	struct zbus_args *zap = aux;
    141 
    142 	/* Ameristar A4066 ethernet card */
    143 	if (zap->manid == 1053 && zap->prodid == 10)
    144 		return(1);
    145 
    146 	return (0);
    147 }
    148 
    149 /*
    150  * Interface exists: make available by filling in network interface
    151  * record.  System will initialize the interface when it is ready
    152  * to accept packets.
    153  */
    154 void
    155 esattach(parent, self, aux)
    156 	struct device *parent, *self;
    157 	void *aux;
    158 {
    159 	struct es_softc *sc = (void *)self;
    160 	struct zbus_args *zap = aux;
    161 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    162 	unsigned long ser;
    163 	u_int8_t myaddr[ETHER_ADDR_LEN];
    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 	myaddr[0] = 0x00;
    172 	myaddr[1] = 0x00;
    173 	myaddr[2] = 0x9f;
    174 
    175 	/*
    176 	 * Serial number for board contains last 3 bytes.
    177 	 */
    178 	ser = (unsigned long) zap->serno;
    179 
    180 	myaddr[3] = (ser >> 16) & 0xff;
    181 	myaddr[4] = (ser >>  8) & 0xff;
    182 	myaddr[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, myaddr);
    197 
    198 	/* Print additional info when attached. */
    199 	printf(": address %s\n", ether_sprintf(myaddr));
    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_ethercom.ec_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 *) &LLADDR(ifp->if_sadl)[0]);
    275 	smc->b1.iar[1] = *((unsigned short *) &LLADDR(ifp->if_sadl)[2]);
    276 	smc->b1.iar[2] = *((unsigned short *) &LLADDR(ifp->if_sadl)[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 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    303 	u_short intsts, intact;
    304 	union smcregs *smc;
    305 	int s = splnet();
    306 
    307 	smc = sc->sc_base;
    308 #ifdef ESDEBUG
    309 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2 &&
    310 	    ifp->if_flags & IFF_RUNNING) {
    311 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    312 		    smc->b2.bsr);
    313 		smc->b2.bsr = BSR_BANK2;
    314 	}
    315 #endif
    316 	intsts = smc->b2.ist;
    317 	intact = smc->b2.msk & intsts;
    318 	if ((intact) == 0) {
    319 		splx(s);
    320 		return (0);
    321 	}
    322 #ifdef ESDEBUG
    323 	if (esdebug)
    324 		printf ("%s: esintr ist %02x msk %02x",
    325 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
    326 	if (sc->sc_intbusy++) {
    327 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
    328 		panic("esintr re-entered");
    329 	}
    330 	if (sc->sc_smcbusy)
    331 		printf("%s: esintr interrupted busy %d\n", sc->sc_dev.dv_xname,
    332 		    sc->sc_smcbusy);
    333 #endif
    334 	smc->b2.msk = 0;
    335 #ifdef ESDEBUG
    336 	if (esdebug)
    337 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    338 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    339 		    smc->b2.fifo);
    340 #endif
    341 	if (intact & IST_ALLOC) {
    342 		sc->sc_intctl &= ~MSK_ALLOC;
    343 #ifdef ESDEBUG
    344 		if (esdebug || 1)
    345 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
    346 			    intsts);
    347 #endif
    348 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    349 			u_char save_pnr;
    350 #ifdef ESDEBUG
    351 			if (esdebug || 1)
    352 				printf (" arr %02x\n", smc->b2.arr);
    353 #endif
    354 			save_pnr = smc->b2.pnr;
    355 			smc->b2.pnr = smc->b2.arr;
    356 			smc->b2.mmucr = MMUCR_RLSPKT;
    357 			while (smc->b2.mmucr & MMUCR_BUSY)
    358 				;
    359 			smc->b2.pnr = save_pnr;
    360 			ifp->if_flags &= ~IFF_OACTIVE;
    361 		}
    362 #ifdef ESDEBUG
    363 		else if (esdebug || 1)
    364 			printf (" IST_ALLOC with ARR_FAILED?\n");
    365 #endif
    366 	}
    367 #ifdef ESDEBUG
    368 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    369 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    370 		    smc->b2.bsr);
    371 		smc->b2.bsr = BSR_BANK2;
    372 	}
    373 #endif
    374 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    375 		esrint(sc);
    376 	}
    377 #ifdef ESDEBUG
    378 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    379 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    380 		    smc->b2.bsr);
    381 		smc->b2.bsr = BSR_BANK2;
    382 	}
    383 #endif
    384 	if (intact & IST_RX_OVRN) {
    385 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    386 		    intsts);
    387 		smc->b2.ist = ACK_RX_OVRN;
    388 		printf ("->%02x\n", smc->b2.ist);
    389 		ifp->if_ierrors++;
    390 	}
    391 	if (intact & IST_TX_EMPTY) {
    392 		u_short ecr;
    393 #ifdef ESDEBUG
    394 		if (esdebug)
    395 			printf ("%s: TX EMPTY %02x",
    396 			    sc->sc_dev.dv_xname, intsts);
    397 		++estxint5;		/* count # IST_TX_EMPTY ints */
    398 #endif
    399 		smc->b2.ist = ACK_TX_EMPTY;
    400 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    401 #ifdef ESDEBUG
    402 		if (esdebug)
    403 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    404 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    405 			    smc->b2.arr);
    406 #endif
    407 		if (smc->b2.ist & IST_TX) {
    408 			intact |= IST_TX;
    409 #ifdef ESDEBUG
    410 			++estxint2;		/* count # TX after TX_EMPTY */
    411 #endif
    412 		} else {
    413 			smc->b0.bsr = BSR_BANK0;
    414 			ecr = smc->b0.ecr;	/* Get error counters */
    415 			if (ecr & 0xff00)
    416 				ifp->if_collisions += ((ecr >> 8) & 15) +
    417 				    ((ecr >> 11) & 0x1e);
    418 			smc->b2.bsr = BSR_BANK2;
    419 #if 0
    420 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
    421 #endif
    422 		}
    423 	}
    424 	if (intact & IST_TX) {
    425 		u_char tx_pnr, save_pnr;
    426 		u_short save_ptr, ephsr, tcr;
    427 		int n = 0;
    428 #ifdef ESDEBUG
    429 		if (esdebug) {
    430 			printf ("%s: TX INT ist %02x",
    431 			    sc->sc_dev.dv_xname, intsts);
    432 			printf ("->%02x\n", smc->b2.ist);
    433 		}
    434 		++estxint3;			/* count # IST_TX */
    435 #endif
    436 zzzz:
    437 #ifdef ESDEBUG
    438 		++estxint4;			/* count # ~TEMPTY */
    439 #endif
    440 		smc->b0.bsr = BSR_BANK0;
    441 		ephsr = smc->b0.ephsr;		/* get EPHSR */
    442 		tcr = smc->b0.tcr;		/* and TCR */
    443 		smc->b2.bsr = BSR_BANK2;
    444 		save_ptr = smc->b2.ptr;
    445 		save_pnr = smc->b2.pnr;
    446 		tx_pnr = smc->b2.fifo >> 8;	/* pktno from completion fifo */
    447 		smc->b2.pnr = tx_pnr;		/* set TX packet number */
    448 		smc->b2.ptr = PTR_READ;		/* point to status word */
    449 #if 0 /* XXXX */
    450 		printf("%s: esintr TXINT IST %02x PNR %02x(%d)",
    451 		    sc->sc_dev.dv_xname, smc->b2.ist,
    452 		    tx_pnr, n);
    453 		printf(" Status %04x", smc->b2.data);
    454 		printf(" EPHSR %04x\n", ephsr);
    455 #endif
    456 		if ((smc->b2.data & EPHSR_TX_SUC) == 0 && (tcr & TCR_TXENA) == 0) {
    457 			/*
    458 			 * Transmitter was stopped for some error.  Enqueue
    459 			 * the packet again and restart the transmitter.
    460 			 * May need some check to limit the number of retries.
    461 			 */
    462 			smc->b2.mmucr = MMUCR_ENQ_TX;
    463 			smc->b0.bsr = BSR_BANK0;
    464 			smc->b0.tcr |= TCR_TXENA;
    465 			smc->b2.bsr = BSR_BANK2;
    466 			ifp->if_oerrors++;
    467 			sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    468 		} else {
    469 			/*
    470 			 * This shouldn't have happened:  IST_TX indicates
    471 			 * the TX completion FIFO is not empty, but the
    472 			 * status for the packet on the completion FIFO
    473 			 * shows that the transmit was sucessful.  Since
    474 			 * AutoRelease is being used, a sucessful transmit
    475 			 * should not result in a packet on the completion
    476 			 * FIFO.  Also, that packet doesn't seem to want
    477 			 * to be acknowledged.  If this occurs, just reset
    478 			 * the TX FIFOs.
    479 			 */
    480 #if 1
    481 			if (smc->b2.ist & IST_TX_EMPTY) {
    482 				smc->b2.mmucr = MMUCR_RESET_TX;
    483 				sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    484 			}
    485 #endif
    486 #ifdef ESDEBUG
    487 			++estxints;	/* count IST_TX with TX enabled */
    488 #endif
    489 		}
    490 		smc->b2.pnr = save_pnr;
    491 		smc->b2.ptr = save_ptr;
    492 		smc->b2.ist = ACK_TX;
    493 
    494 		if ((smc->b2.fifo & FIFO_TEMPTY) == 0 && n++ < 32) {
    495 #if 0 /* XXXX */
    496 			printf("%s: multiple TX int(%2d) pnr %02x ist %02x fifo %04x",
    497 			    sc->sc_dev.dv_xname, n, tx_pnr, smc->b2.ist, smc->b2.fifo);
    498 			smc->w2.istmsk = ACK_TX << 8;
    499 			printf(" %04x\n", smc->b2.fifo);
    500 #endif
    501 			if (tx_pnr != (smc->b2.fifo >> 8))
    502 				goto zzzz;
    503 		}
    504 	}
    505 	/* output packets */
    506 	estint(sc);
    507 #ifdef ESDEBUG
    508 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    509 		printf("%s: intr+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    510 		    smc->b2.bsr);
    511 		smc->b2.bsr = BSR_BANK2;
    512 	}
    513 #endif
    514 	smc->b2.msk = sc->sc_intctl;
    515 #ifdef ESDEBUG
    516 	if (--sc->sc_intbusy) {
    517 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    518 		panic("esintr busy on exit");
    519 	}
    520 #endif
    521 	splx(s);
    522 	return (1);
    523 }
    524 
    525 void
    526 esrint(sc)
    527 	struct es_softc *sc;
    528 {
    529 	union smcregs *smc = sc->sc_base;
    530 	u_short len;
    531 	short cnt;
    532 	u_short pktctlw, pktlen, *buf;
    533 	volatile u_short *data;
    534 #if 0
    535 	u_long *lbuf;
    536 	volatile u_long *ldata;
    537 #endif
    538 	struct ifnet *ifp;
    539 	struct mbuf *top, **mp, *m;
    540 	struct ether_header *eh;
    541 #ifdef USEPKTBUF
    542 	u_char *b, pktbuf[1530];
    543 #endif
    544 #ifdef ESDEBUG
    545 	int i;
    546 #endif
    547 
    548 	ifp = &sc->sc_ethercom.ec_if;
    549 #ifdef ESDEBUG
    550 	if (esdebug)
    551 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    552 		    smc->b2.fifo);
    553 	if (sc->sc_smcbusy++) {
    554 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
    555 		panic("esrint re-entered");
    556 	}
    557 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    558 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    559 		    smc->b2.bsr);
    560 		smc->b2.bsr = BSR_BANK2;
    561 	}
    562 #endif
    563 	data = (u_short *)&smc->b2.data;
    564 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
    565 	(void) smc->b2.mmucr;
    566 #ifdef ESDEBUG
    567 	if (esdebug)
    568 		printf ("->%04x", smc->b2.fifo);
    569 #endif
    570 	len = *data;
    571 	len = SWAP(len);	/* Careful of macro side-effects */
    572 #ifdef ESDEBUG
    573 	if (esdebug)
    574 		printf (" length %d", len);
    575 #endif
    576 	smc->b2.ptr = PTR_RCV | (PTR_AUTOINCR + PTR_READ) | SWAP(0x0000);
    577 	(void) smc->b2.mmucr;
    578 	pktctlw = *data;
    579 	pktlen = *data;
    580 	pktctlw = SWAP(pktctlw);
    581 	pktlen = SWAP(pktlen) - 6;
    582 	if (pktctlw & RFSW_ODDFRM)
    583 		pktlen++;
    584 	if (len > 1530) {
    585 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
    586 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
    587 		/* XXX ignore packet, or just truncate? */
    588 #if defined(ESDEBUG) && defined(DDB)
    589 		if ((smc->b2.bsr & BSR_MASK) != BSR_BANK2)
    590 			Debugger();
    591 #endif
    592 		smc->b2.bsr = BSR_BANK2;
    593 		smc->b2.mmucr = MMUCR_REMRLS_RX;
    594 		while (smc->b2.mmucr & MMUCR_BUSY)
    595 			;
    596 		++ifp->if_ierrors;
    597 #ifdef ESDEBUG
    598 		if (--sc->sc_smcbusy) {
    599 			printf("%s: esrintr busy on bad packet exit\n",
    600 			    sc->sc_dev.dv_xname);
    601 			panic("esrintr busy on exit");
    602 		}
    603 #endif
    604 		return;
    605 	}
    606 #ifdef USEPKTBUF
    607 #if 0
    608 	lbuf = (u_long *) pktbuf;
    609 	ldata = (u_long *)data;
    610 	cnt = (len - 4) / 4;
    611 	while (cnt--)
    612 		*lbuf++ = *ldata;
    613 	if ((len - 4) & 2) {
    614 		buf = (u_short *) lbuf;
    615 		*buf = *data;
    616 	}
    617 #else
    618 	buf = (u_short *)pktbuf;
    619 	cnt = (len - 4) / 2;
    620 	while (cnt--)
    621 		*buf++ = *data;
    622 #endif
    623 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    624 	while (smc->b2.mmucr & MMUCR_BUSY)
    625 		;
    626 #ifdef ESDEBUG
    627 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    628 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    629 		/* count input error? */
    630 	}
    631 	if (esdebug) {
    632 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    633 		    smc->b2.fifo);
    634 		for (i = 0; i < pktlen; ++i)
    635 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    636 			    "");
    637 		if (i & 31)
    638 			printf ("\n");
    639 	}
    640 #endif
    641 #else	/* USEPKTBUF */
    642 	/* XXX copy directly from controller to mbuf */
    643 #ifdef ESDEBUG
    644 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    645 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    646 		/* count input error? */
    647 	}
    648 	if (esdebug) {
    649 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    650 		    smc->b2.fifo);
    651 	}
    652 #endif
    653 #endif /* USEPKTBUF */
    654 	ifp->if_ipackets++;
    655 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    656 	if (m == NULL)
    657 		return;
    658 	m->m_pkthdr.rcvif = ifp;
    659 	m->m_pkthdr.len = pktlen;
    660 	len = MHLEN;
    661 	top = NULL;
    662 	mp = &top;
    663 #ifdef USEPKTBUF
    664 	b = pktbuf;
    665 #endif
    666 	while (pktlen > 0) {
    667 		if (top) {
    668 			MGET(m, M_DONTWAIT, MT_DATA);
    669 			if (m == 0) {
    670 				m_freem(top);
    671 				return;
    672 			}
    673 			len = MLEN;
    674 		}
    675 		if (pktlen >= MINCLSIZE) {
    676 			MCLGET(m, M_DONTWAIT);
    677 			if (m->m_flags & M_EXT)
    678 				len = MCLBYTES;
    679 		}
    680 		m->m_len = len = min(pktlen, len);
    681 #ifdef USEPKTBUF
    682 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    683 		b += len;
    684 #else	/* USEPKTBUF */
    685 		buf = mtod(m, u_short *);
    686 		cnt = len / 2;
    687 		while (cnt--)
    688 			*buf++ = *data;
    689 		if (len & 1)
    690 			*buf = *data;	/* XXX should be byte store */
    691 #ifdef ESDEBUG
    692 		if (esdebug) {
    693 			buf = mtod(m, u_short *);
    694 			for (i = 0; i < len; ++i)
    695 				printf ("%02x%s", ((u_char *)buf)[i],
    696 				    ((i & 31) == 31) ? "\n" : "");
    697 			if (i & 31)
    698 				printf ("\n");
    699 		}
    700 #endif
    701 #endif	/* USEPKTBUF */
    702 		pktlen -= len;
    703 		*mp = m;
    704 		mp = &m->m_next;
    705 	}
    706 #ifndef USEPKTBUF
    707 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    708 	while (smc->b2.mmucr & MMUCR_BUSY)
    709 		;
    710 #endif
    711 	eh = mtod(top, struct ether_header *);
    712 #if NBPFILTER > 0
    713 	/*
    714 	 * Check if there's a BPF listener on this interface.  If so, hand off
    715 	 * the raw packet to bpf.
    716 	 */
    717 	if (ifp->if_bpf) {
    718 		bpf_mtap(ifp->if_bpf, top);
    719 
    720 		/*
    721 		 * Note that the interface cannot be in promiscuous mode if
    722 		 * there are no BPF listeners.  And if we are in promiscuous
    723 		 * mode, we have to check if this packet is really ours.
    724 		 */
    725 		if ((sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC) &&
    726 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    727 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    728 			    sizeof(eh->ether_dhost)) != 0) {
    729 			m_freem(top);
    730 			return;
    731 		}
    732 	}
    733 #endif
    734 	top->m_pkthdr.len -= sizeof (*eh);
    735 	top->m_len -= sizeof (*eh);
    736 	top->m_data += sizeof (*eh);
    737 
    738 	ether_input(ifp, eh, top);
    739 #ifdef ESDEBUG
    740 	if (--sc->sc_smcbusy) {
    741 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    742 		panic("esintr busy on exit");
    743 	}
    744 #endif
    745 }
    746 
    747 void
    748 estint(sc)
    749 	struct es_softc *sc;
    750 {
    751 
    752 	esstart(&sc->sc_ethercom.ec_if);
    753 }
    754 
    755 void
    756 esstart(ifp)
    757 	struct ifnet *ifp;
    758 {
    759 	struct es_softc *sc = ifp->if_softc;
    760 	union smcregs *smc = sc->sc_base;
    761 	struct mbuf *m0, *m;
    762 #ifdef USEPKTBUF
    763 	u_short pktbuf[ETHERMTU + 2];
    764 #else
    765 	u_short oddbyte, needbyte;
    766 #endif
    767 	u_short pktctlw, pktlen;
    768 	u_short *buf;
    769 	volatile u_short *data;
    770 #if 0
    771 	u_long *lbuf;
    772 	volatile u_long *ldata;
    773 #endif
    774 	short cnt;
    775 	int i;
    776 	u_char active_pnr;
    777 
    778 	if ((sc->sc_ethercom.ec_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    779 	    IFF_RUNNING)
    780 		return;
    781 
    782 #ifdef ESDEBUG
    783 	if (sc->sc_smcbusy++) {
    784 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    785 		panic("esstart re-entred");
    786 	}
    787 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    788 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    789 		    smc->b2.bsr);
    790 		smc->b2.bsr = BSR_BANK2;
    791 	}
    792 #endif
    793 	for (;;) {
    794 #ifdef ESDEBUG
    795 		u_short start_ptr, end_ptr;
    796 #endif
    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_ethercom.ec_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",
    807 			    sc->sc_dev.dv_xname, 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_ethercom.ec_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 & BSR_MASK) != 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_ethercom.ec_if.if_snd, m);
    836 		smc->b2.ptr = PTR_AUTOINCR;
    837 		(void) smc->b2.mmucr;
    838 		data = (u_short *)&smc->b2.data;
    839 		*data = SWAP(pktctlw);
    840 		*data = SWAP(pktlen);
    841 #ifdef ESDEBUG
    842 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    843 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    844 			    smc->b2.bsr);
    845 			smc->b2.bsr = BSR_BANK2;
    846 		}
    847 #endif
    848 #ifdef USEPKTBUF
    849 		i = 0;
    850 		for (m0 = m; m; m = m->m_next) {
    851 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    852 			i += m->m_len;
    853 		}
    854 
    855 		if (i & 1)	/* Figure out where to put control byte */
    856 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    857 		else
    858 			pktbuf[i/2] = 0;
    859 		pktlen -= 4;
    860 #ifdef ESDEBUG
    861 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
    862 			printf("%s: esstart packet longer than pktbuf\n",
    863 			    sc->sc_dev.dv_xname);
    864 #endif
    865 #if 0 /* doesn't quite work? */
    866 		lbuf = (u_long *)(pktbuf);
    867 		ldata = (u_long *)data;
    868 		cnt = pktlen / 4;
    869 		while(cnt--)
    870 			*ldata = *lbuf++;
    871 		if (pktlen & 2) {
    872 			buf = (u_short *)lbuf;
    873 			*data = *buf;
    874 		}
    875 #else
    876 #ifdef ESDEBUG
    877 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    878 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    879 			    smc->b2.bsr);
    880 			smc->b2.bsr = BSR_BANK2;
    881 		}
    882 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
    883 #endif
    884 		buf = pktbuf;
    885 		cnt = pktlen / 2;
    886 		while (cnt--)
    887 			*data = *buf++;
    888 #ifdef ESDEBUG
    889 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
    890 #endif
    891 #endif
    892 #else	/* USEPKTBUF */
    893 		pktctlw = 0;
    894 		oddbyte = needbyte = 0;
    895 		for (m0 = m; m; m = m->m_next) {
    896 			buf = mtod(m, u_short *);
    897 			cnt = m->m_len / 2;
    898 			if (needbyte) {
    899 				oddbyte |= *buf >> 8;
    900 				*data = oddbyte;
    901 			}
    902 			while (cnt--)
    903 				*data = *buf++;
    904 			if (m->m_len & 1)
    905 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    906 			if (m->m_len & 1 && m->m_next)
    907 				printf("%s: esstart odd byte count in mbuf\n",
    908 				    sc->sc_dev.dv_xname);
    909 		}
    910 		*data = pktctlw;
    911 #endif	/* USEPKTBUF */
    912 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    913 			/*
    914 			 * The bank select register has changed.  This seems
    915 			 * to happen with my A2000/Zeus once in a while.  It
    916 			 * appears that the Ethernet chip resets while
    917 			 * copying the transmit buffer.  Requeue the current
    918 			 * transmit buffer and reinitialize the interface.
    919 			 * The initialize routine will take care of
    920 			 * retransmitting the buffer.  mhitch
    921 			 */
    922 #ifdef DIAGNOSTIC
    923 			printf("%s: esstart+++ BSR not 2: %04x\n",
    924 			    sc->sc_dev.dv_xname, smc->b2.bsr);
    925 #endif
    926 			smc->b2.bsr = BSR_BANK2;
    927 #ifdef ESDEBUG
    928 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
    929 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
    930 			--sc->sc_smcbusy;
    931 #endif
    932 			IF_PREPEND(&sc->sc_ethercom.ec_if.if_snd, m0);
    933 			esinit(sc);	/* It's really hosed - reset */
    934 			return;
    935 		}
    936 		smc->b2.mmucr = MMUCR_ENQ_TX;
    937 		if (smc->b2.pnr != active_pnr)
    938 			printf("%s: esstart - PNR changed %x->%x\n",
    939 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    940 #if NBPFILTER > 0
    941 		if (sc->sc_ethercom.ec_if.if_bpf)
    942 			bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m0);
    943 #endif
    944 		m_freem(m0);
    945 		sc->sc_ethercom.ec_if.if_opackets++;	/* move to interrupt? */
    946 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    947 	}
    948 	smc->b2.msk = sc->sc_intctl;
    949 #ifdef ESDEBUG
    950 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    951 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    952 		    smc->b2.bsr);
    953 		smc->b2.bsr = BSR_BANK2;
    954 	}
    955 	if (--sc->sc_smcbusy) {
    956 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    957 		panic("esstart busy on exit");
    958 	}
    959 #endif
    960 }
    961 
    962 int
    963 esioctl(ifp, command, data)
    964 	register struct ifnet *ifp;
    965 	u_long command;
    966 	caddr_t data;
    967 {
    968 	struct es_softc *sc = ifp->if_softc;
    969 	register struct ifaddr *ifa = (struct ifaddr *)data;
    970 	struct ifreq *ifr = (struct ifreq *)data;
    971 	int s, error = 0;
    972 
    973 	s = splnet();
    974 
    975 	switch (command) {
    976 
    977 	case SIOCSIFADDR:
    978 		ifp->if_flags |= IFF_UP;
    979 
    980 		switch (ifa->ifa_addr->sa_family) {
    981 #ifdef INET
    982 		case AF_INET:
    983 			esinit(sc);
    984 			arp_ifinit(ifp, ifa);
    985 			break;
    986 #endif
    987 #ifdef NS
    988 		case AF_NS:
    989 		    {
    990 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    991 
    992 			if (ns_nullhost(*ina))
    993 				ina->x_host =
    994 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    995 			else
    996 				bcopy(ina->x_host.c_host,
    997 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    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_ethercom) :
   1048 		    ether_delmulti(ifr, &sc->sc_ethercom);
   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 	++ifp->if_oerrors;
   1088 
   1089 	esreset(sc);
   1090 }
   1091