Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.25
      1 /*	$NetBSD: if_es.c,v 1.25 2000/10/01 23:32:39 thorpej 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 #include "opt_ns.h"
     39 
     40 #include "bpfilter.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/buf.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/syslog.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/errno.h>
     51 #include <sys/device.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_ether.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/in_systm.h>
     60 #include <netinet/in_var.h>
     61 #include <netinet/ip.h>
     62 #include <netinet/if_inarp.h>
     63 #endif
     64 
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #include <machine/cpu.h>
     71 #include <machine/mtpr.h>
     72 #include <amiga/amiga/device.h>
     73 #include <amiga/amiga/isr.h>
     74 #include <amiga/dev/zbusvar.h>
     75 #include <amiga/dev/if_esreg.h>
     76 
     77 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     78 
     79 #define	USEPKTBUF
     80 
     81 /*
     82  * Ethernet software status per interface.
     83  *
     84  * Each interface is referenced by a network interface structure,
     85  * es_if, which the routing code uses to locate the interface.
     86  * This structure contains the output queue for the interface, its address, ...
     87  */
     88 struct	es_softc {
     89 	struct	device sc_dev;
     90 	struct	isr sc_isr;
     91 	struct	ethercom sc_ethercom;	/* common Ethernet structures */
     92 	void	*sc_base;		/* base address of board */
     93 	short	sc_iflags;
     94 	unsigned short sc_intctl;
     95 #ifdef ESDEBUG
     96 	int	sc_debug;
     97 	short	sc_intbusy;		/* counter for interrupt rentered */
     98 	short	sc_smcbusy;		/* counter for other rentry checks */
     99 #endif
    100 };
    101 
    102 #if NBPFILTER > 0
    103 #include <net/bpf.h>
    104 #include <net/bpfdesc.h>
    105 #endif
    106 
    107 #ifdef ESDEBUG
    108 /* console error messages */
    109 int	esdebug = 0;
    110 int	estxints = 0;	/* IST_TX with TX enabled */
    111 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    112 int	estxint3 = 0;	/* IST_TX interrupt processed */
    113 int	estxint4 = 0;	/* ~TEMPTY counts */
    114 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    115 void	es_dump_smcregs __P((char *, union smcregs *));
    116 #endif
    117 
    118 int esintr __P((void *));
    119 void esstart __P((struct ifnet *));
    120 void eswatchdog __P((struct ifnet *));
    121 int esioctl __P((struct ifnet *, u_long, caddr_t));
    122 void esrint __P((struct es_softc *));
    123 void estint __P((struct es_softc *));
    124 void esinit __P((struct es_softc *));
    125 void esreset __P((struct es_softc *));
    126 void esstop __P((struct es_softc *));
    127 
    128 int esmatch __P((struct device *, struct cfdata *, void *));
    129 void esattach __P((struct device *, struct device *, void *));
    130 
    131 struct cfattach es_ca = {
    132 	sizeof(struct es_softc), esmatch, esattach
    133 };
    134 
    135 int
    136 esmatch(parent, cfp, aux)
    137 	struct device *parent;
    138 	struct cfdata *cfp;
    139 	void *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_ethercom.ec_if;
    163 	unsigned long ser;
    164 	u_int8_t myaddr[ETHER_ADDR_LEN];
    165 
    166 	sc->sc_base = zap->va;
    167 
    168 	/*
    169 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    170 	 * (Currently only Ameristar.)
    171 	 */
    172 	myaddr[0] = 0x00;
    173 	myaddr[1] = 0x00;
    174 	myaddr[2] = 0x9f;
    175 
    176 	/*
    177 	 * Serial number for board contains last 3 bytes.
    178 	 */
    179 	ser = (unsigned long) zap->serno;
    180 
    181 	myaddr[3] = (ser >> 16) & 0xff;
    182 	myaddr[4] = (ser >>  8) & 0xff;
    183 	myaddr[5] = (ser      ) & 0xff;
    184 
    185 	/* Initialize ifnet structure. */
    186 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    187 	ifp->if_softc = sc;
    188 	ifp->if_ioctl = esioctl;
    189 	ifp->if_start = esstart;
    190 	ifp->if_watchdog = eswatchdog;
    191 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
    192 	    IFF_MULTICAST;
    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 | RCR_ALLMUL;
    283 	/* XXX add 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 #ifdef USEPKTBUF
    541 	u_char *b, pktbuf[1530];
    542 #endif
    543 #ifdef ESDEBUG
    544 	int i;
    545 #endif
    546 
    547 	ifp = &sc->sc_ethercom.ec_if;
    548 #ifdef ESDEBUG
    549 	if (esdebug)
    550 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    551 		    smc->b2.fifo);
    552 	if (sc->sc_smcbusy++) {
    553 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
    554 		panic("esrint re-entered");
    555 	}
    556 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    557 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    558 		    smc->b2.bsr);
    559 		smc->b2.bsr = BSR_BANK2;
    560 	}
    561 #endif
    562 	data = (u_short *)&smc->b2.data;
    563 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
    564 	(void) smc->b2.mmucr;
    565 #ifdef ESDEBUG
    566 	if (esdebug)
    567 		printf ("->%04x", smc->b2.fifo);
    568 #endif
    569 	len = *data;
    570 	len = SWAP(len);	/* Careful of macro side-effects */
    571 #ifdef ESDEBUG
    572 	if (esdebug)
    573 		printf (" length %d", len);
    574 #endif
    575 	smc->b2.ptr = PTR_RCV | (PTR_AUTOINCR + PTR_READ) | SWAP(0x0000);
    576 	(void) smc->b2.mmucr;
    577 	pktctlw = *data;
    578 	pktlen = *data;
    579 	pktctlw = SWAP(pktctlw);
    580 	pktlen = SWAP(pktlen) - 6;
    581 	if (pktctlw & RFSW_ODDFRM)
    582 		pktlen++;
    583 	if (len > 1530) {
    584 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
    585 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
    586 		/* XXX ignore packet, or just truncate? */
    587 #if defined(ESDEBUG) && defined(DDB)
    588 		if ((smc->b2.bsr & BSR_MASK) != BSR_BANK2)
    589 			Debugger();
    590 #endif
    591 		smc->b2.bsr = BSR_BANK2;
    592 		smc->b2.mmucr = MMUCR_REMRLS_RX;
    593 		while (smc->b2.mmucr & MMUCR_BUSY)
    594 			;
    595 		++ifp->if_ierrors;
    596 #ifdef ESDEBUG
    597 		if (--sc->sc_smcbusy) {
    598 			printf("%s: esrintr busy on bad packet exit\n",
    599 			    sc->sc_dev.dv_xname);
    600 			panic("esrintr busy on exit");
    601 		}
    602 #endif
    603 		return;
    604 	}
    605 #ifdef USEPKTBUF
    606 #if 0
    607 	lbuf = (u_long *) pktbuf;
    608 	ldata = (u_long *)data;
    609 	cnt = (len - 4) / 4;
    610 	while (cnt--)
    611 		*lbuf++ = *ldata;
    612 	if ((len - 4) & 2) {
    613 		buf = (u_short *) lbuf;
    614 		*buf = *data;
    615 	}
    616 #else
    617 	buf = (u_short *)pktbuf;
    618 	cnt = (len - 4) / 2;
    619 	while (cnt--)
    620 		*buf++ = *data;
    621 #endif
    622 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    623 	while (smc->b2.mmucr & MMUCR_BUSY)
    624 		;
    625 #ifdef ESDEBUG
    626 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    627 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    628 		/* count input error? */
    629 	}
    630 	if (esdebug) {
    631 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    632 		    smc->b2.fifo);
    633 		for (i = 0; i < pktlen; ++i)
    634 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    635 			    "");
    636 		if (i & 31)
    637 			printf ("\n");
    638 	}
    639 #endif
    640 #else	/* USEPKTBUF */
    641 	/* XXX copy directly from controller to mbuf */
    642 #ifdef ESDEBUG
    643 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    644 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    645 		/* count input error? */
    646 	}
    647 	if (esdebug) {
    648 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    649 		    smc->b2.fifo);
    650 	}
    651 #endif
    652 #endif /* USEPKTBUF */
    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 #if NBPFILTER > 0
    711 	/*
    712 	 * Check if there's a BPF listener on this interface.  If so, hand off
    713 	 * the raw packet to bpf.
    714 	 */
    715 	if (ifp->if_bpf)
    716 		bpf_mtap(ifp->if_bpf, top);
    717 #endif
    718 	(*ifp->if_input)(ifp, top);
    719 #ifdef ESDEBUG
    720 	if (--sc->sc_smcbusy) {
    721 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    722 		panic("esintr busy on exit");
    723 	}
    724 #endif
    725 }
    726 
    727 void
    728 estint(sc)
    729 	struct es_softc *sc;
    730 {
    731 
    732 	esstart(&sc->sc_ethercom.ec_if);
    733 }
    734 
    735 void
    736 esstart(ifp)
    737 	struct ifnet *ifp;
    738 {
    739 	struct es_softc *sc = ifp->if_softc;
    740 	union smcregs *smc = sc->sc_base;
    741 	struct mbuf *m0, *m;
    742 #ifdef USEPKTBUF
    743 	u_short pktbuf[ETHERMTU + 2];
    744 #else
    745 	u_short oddbyte, needbyte;
    746 #endif
    747 	u_short pktctlw, pktlen;
    748 	u_short *buf;
    749 	volatile u_short *data;
    750 #if 0
    751 	u_long *lbuf;
    752 	volatile u_long *ldata;
    753 #endif
    754 	short cnt;
    755 	int i;
    756 	u_char active_pnr;
    757 
    758 	if ((sc->sc_ethercom.ec_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    759 	    IFF_RUNNING)
    760 		return;
    761 
    762 #ifdef ESDEBUG
    763 	if (sc->sc_smcbusy++) {
    764 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    765 		panic("esstart re-entred");
    766 	}
    767 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    768 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    769 		    smc->b2.bsr);
    770 		smc->b2.bsr = BSR_BANK2;
    771 	}
    772 #endif
    773 	for (;;) {
    774 #ifdef ESDEBUG
    775 		u_short start_ptr, end_ptr;
    776 #endif
    777 		/*
    778 		 * Sneak a peek at the next packet to get the length
    779 		 * and see if the SMC 91C90 can accept it.
    780 		 */
    781 		m = sc->sc_ethercom.ec_if.if_snd.ifq_head;
    782 		if (!m)
    783 			break;
    784 #ifdef ESDEBUG
    785 		if (esdebug && (m->m_next || m->m_len & 1))
    786 			printf("%s: esstart m_next %p m_len %d\n",
    787 			    sc->sc_dev.dv_xname, m->m_next, m->m_len);
    788 #endif
    789 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    790 			pktlen += m0->m_len;
    791 		pktctlw = 0;
    792 		pktlen += 4;
    793 		if (pktlen & 1)
    794 			++pktlen;	/* control byte after last byte */
    795 		else
    796 			pktlen += 2;	/* control byte after pad byte */
    797 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    798 		for (i = 0; i <= 5; ++i)
    799 			if ((smc->b2.arr & ARR_FAILED) == 0)
    800 				break;
    801 		if (smc->b2.arr & ARR_FAILED) {
    802 			sc->sc_ethercom.ec_if.if_flags |= IFF_OACTIVE;
    803 			sc->sc_intctl |= MSK_ALLOC;
    804 			break;
    805 		}
    806 		active_pnr = smc->b2.pnr = smc->b2.arr;
    807 
    808 #ifdef ESDEBUG
    809 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    810 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    811 			    smc->b2.bsr);
    812 		smc->b2.bsr = BSR_BANK2;
    813 		}
    814 #endif
    815 		IF_DEQUEUE(&sc->sc_ethercom.ec_if.if_snd, m);
    816 		smc->b2.ptr = PTR_AUTOINCR;
    817 		(void) smc->b2.mmucr;
    818 		data = (u_short *)&smc->b2.data;
    819 		*data = SWAP(pktctlw);
    820 		*data = SWAP(pktlen);
    821 #ifdef ESDEBUG
    822 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    823 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    824 			    smc->b2.bsr);
    825 			smc->b2.bsr = BSR_BANK2;
    826 		}
    827 #endif
    828 #ifdef USEPKTBUF
    829 		i = 0;
    830 		for (m0 = m; m; m = m->m_next) {
    831 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    832 			i += m->m_len;
    833 		}
    834 
    835 		if (i & 1)	/* Figure out where to put control byte */
    836 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    837 		else
    838 			pktbuf[i/2] = 0;
    839 		pktlen -= 4;
    840 #ifdef ESDEBUG
    841 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
    842 			printf("%s: esstart packet longer than pktbuf\n",
    843 			    sc->sc_dev.dv_xname);
    844 #endif
    845 #if 0 /* doesn't quite work? */
    846 		lbuf = (u_long *)(pktbuf);
    847 		ldata = (u_long *)data;
    848 		cnt = pktlen / 4;
    849 		while(cnt--)
    850 			*ldata = *lbuf++;
    851 		if (pktlen & 2) {
    852 			buf = (u_short *)lbuf;
    853 			*data = *buf;
    854 		}
    855 #else
    856 #ifdef ESDEBUG
    857 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    858 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    859 			    smc->b2.bsr);
    860 			smc->b2.bsr = BSR_BANK2;
    861 		}
    862 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
    863 #endif
    864 		buf = pktbuf;
    865 		cnt = pktlen / 2;
    866 		while (cnt--)
    867 			*data = *buf++;
    868 #ifdef ESDEBUG
    869 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
    870 #endif
    871 #endif
    872 #else	/* USEPKTBUF */
    873 		pktctlw = 0;
    874 		oddbyte = needbyte = 0;
    875 		for (m0 = m; m; m = m->m_next) {
    876 			buf = mtod(m, u_short *);
    877 			cnt = m->m_len / 2;
    878 			if (needbyte) {
    879 				oddbyte |= *buf >> 8;
    880 				*data = oddbyte;
    881 			}
    882 			while (cnt--)
    883 				*data = *buf++;
    884 			if (m->m_len & 1)
    885 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    886 			if (m->m_len & 1 && m->m_next)
    887 				printf("%s: esstart odd byte count in mbuf\n",
    888 				    sc->sc_dev.dv_xname);
    889 		}
    890 		*data = pktctlw;
    891 #endif	/* USEPKTBUF */
    892 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    893 			/*
    894 			 * The bank select register has changed.  This seems
    895 			 * to happen with my A2000/Zeus once in a while.  It
    896 			 * appears that the Ethernet chip resets while
    897 			 * copying the transmit buffer.  Requeue the current
    898 			 * transmit buffer and reinitialize the interface.
    899 			 * The initialize routine will take care of
    900 			 * retransmitting the buffer.  mhitch
    901 			 */
    902 #ifdef DIAGNOSTIC
    903 			printf("%s: esstart+++ BSR not 2: %04x\n",
    904 			    sc->sc_dev.dv_xname, smc->b2.bsr);
    905 #endif
    906 			smc->b2.bsr = BSR_BANK2;
    907 #ifdef ESDEBUG
    908 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
    909 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
    910 			--sc->sc_smcbusy;
    911 #endif
    912 			IF_PREPEND(&sc->sc_ethercom.ec_if.if_snd, m0);
    913 			esinit(sc);	/* It's really hosed - reset */
    914 			return;
    915 		}
    916 		smc->b2.mmucr = MMUCR_ENQ_TX;
    917 		if (smc->b2.pnr != active_pnr)
    918 			printf("%s: esstart - PNR changed %x->%x\n",
    919 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    920 #if NBPFILTER > 0
    921 		if (sc->sc_ethercom.ec_if.if_bpf)
    922 			bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m0);
    923 #endif
    924 		m_freem(m0);
    925 		sc->sc_ethercom.ec_if.if_opackets++;	/* move to interrupt? */
    926 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    927 	}
    928 	smc->b2.msk = sc->sc_intctl;
    929 #ifdef ESDEBUG
    930 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    931 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    932 		    smc->b2.bsr);
    933 		smc->b2.bsr = BSR_BANK2;
    934 	}
    935 	if (--sc->sc_smcbusy) {
    936 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    937 		panic("esstart busy on exit");
    938 	}
    939 #endif
    940 }
    941 
    942 int
    943 esioctl(ifp, command, data)
    944 	register struct ifnet *ifp;
    945 	u_long command;
    946 	caddr_t data;
    947 {
    948 	struct es_softc *sc = ifp->if_softc;
    949 	register struct ifaddr *ifa = (struct ifaddr *)data;
    950 	struct ifreq *ifr = (struct ifreq *)data;
    951 	int s, error = 0;
    952 
    953 	s = splnet();
    954 
    955 	switch (command) {
    956 
    957 	case SIOCSIFADDR:
    958 		ifp->if_flags |= IFF_UP;
    959 
    960 		switch (ifa->ifa_addr->sa_family) {
    961 #ifdef INET
    962 		case AF_INET:
    963 			esinit(sc);
    964 			arp_ifinit(ifp, ifa);
    965 			break;
    966 #endif
    967 #ifdef NS
    968 		case AF_NS:
    969 		    {
    970 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    971 
    972 			if (ns_nullhost(*ina))
    973 				ina->x_host =
    974 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    975 			else
    976 				bcopy(ina->x_host.c_host,
    977 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    978 			/* Set new address. */
    979 			esinit(sc);
    980 			break;
    981 		    }
    982 #endif
    983 		default:
    984 			esinit(sc);
    985 			break;
    986 		}
    987 		break;
    988 
    989 	case SIOCSIFFLAGS:
    990 		/*
    991 		 * If interface is marked down and it is running, then stop it
    992 		 */
    993 		if ((ifp->if_flags & IFF_UP) == 0 &&
    994 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    995 			/*
    996 			 * If interface is marked down and it is running, then
    997 			 * stop it.
    998 			 */
    999 			esstop(sc);
   1000 			ifp->if_flags &= ~IFF_RUNNING;
   1001 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1002 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1003 			/*
   1004 			 * If interface is marked up and it is stopped, then
   1005 			 * start it.
   1006 			 */
   1007 			esinit(sc);
   1008 		} else {
   1009 			/*
   1010 			 * Reset the interface to pick up changes in any other
   1011 			 * flags that affect hardware registers.
   1012 			 */
   1013 			esstop(sc);
   1014 			esinit(sc);
   1015 		}
   1016 #ifdef ESDEBUG
   1017 		if (ifp->if_flags & IFF_DEBUG)
   1018 			esdebug = sc->sc_debug = 1;
   1019 		else
   1020 			esdebug = sc->sc_debug = 0;
   1021 #endif
   1022 		break;
   1023 
   1024 	case SIOCADDMULTI:
   1025 	case SIOCDELMULTI:
   1026 		error = (command == SIOCADDMULTI) ?
   1027 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1028 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1029 
   1030 		if (error == ENETRESET) {
   1031 			/*
   1032 			 * Multicast list has changed; set the hardware filter
   1033 			 * accordingly.
   1034 			 */
   1035 			/* XXX */
   1036 			error = 0;
   1037 		}
   1038 		break;
   1039 
   1040 	default:
   1041 		error = EINVAL;
   1042 	}
   1043 
   1044 	splx(s);
   1045 	return (error);
   1046 }
   1047 
   1048 void
   1049 esreset(sc)
   1050 	struct es_softc *sc;
   1051 {
   1052 	int s;
   1053 
   1054 	s = splnet();
   1055 	esstop(sc);
   1056 	esinit(sc);
   1057 	splx(s);
   1058 }
   1059 
   1060 void
   1061 eswatchdog(ifp)
   1062 	struct ifnet *ifp;
   1063 {
   1064 	struct es_softc *sc = ifp->if_softc;
   1065 
   1066 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1067 	++ifp->if_oerrors;
   1068 
   1069 	esreset(sc);
   1070 }
   1071