Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.16
      1 /*	$NetBSD: if_es.c,v 1.16 1996/12/23 09:10:17 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	USEPKTBUF
     77 
     78 /*
     79  * Ethernet software status per interface.
     80  *
     81  * Each interface is referenced by a network interface structure,
     82  * es_if, which the routing code uses to locate the interface.
     83  * This structure contains the output queue for the interface, its address, ...
     84  */
     85 struct	es_softc {
     86 	struct	device sc_dev;
     87 	struct	isr sc_isr;
     88 	struct	arpcom sc_arpcom;	/* common Ethernet structures */
     89 	void	*sc_base;		/* base address of board */
     90 	short	sc_iflags;
     91 	unsigned short sc_intctl;
     92 #ifdef ESDEBUG
     93 	int	sc_debug;
     94 	short	sc_intbusy;		/* counter for interrupt rentered */
     95 	short	sc_smcbusy;		/* counter for other rentry checks */
     96 #endif
     97 };
     98 
     99 #if NBPFILTER > 0
    100 #include <net/bpf.h>
    101 #include <net/bpfdesc.h>
    102 #endif
    103 
    104 #ifdef ESDEBUG
    105 /* console error messages */
    106 int	esdebug = 0;
    107 int	estxints = 0;	/* IST_TX with TX enabled */
    108 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    109 int	estxint3 = 0;	/* IST_TX interrupt processed */
    110 int	estxint4 = 0;	/* ~TEMPTY counts */
    111 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    112 void	es_dump_smcregs __P((char *, union smcregs *));
    113 #endif
    114 
    115 int esintr __P((void *));
    116 void esstart __P((struct ifnet *));
    117 void eswatchdog __P((struct ifnet *));
    118 int esioctl __P((struct ifnet *, u_long, caddr_t));
    119 void esrint __P((struct es_softc *));
    120 void estint __P((struct es_softc *));
    121 void esinit __P((struct es_softc *));
    122 void esreset __P((struct es_softc *));
    123 void esstop __P((struct es_softc *));
    124 
    125 int esmatch __P((struct device *, struct cfdata *, void *));
    126 void esattach __P((struct device *, struct device *, void *));
    127 
    128 struct cfattach es_ca = {
    129 	sizeof(struct es_softc), esmatch, esattach
    130 };
    131 
    132 struct cfdriver es_cd = {
    133 	NULL, "es", DV_IFNET
    134 };
    135 
    136 int
    137 esmatch(parent, cfp, aux)
    138 	struct device *parent;
    139 	struct cfdata *cfp;
    140 	void *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 
    168 	/*
    169 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    170 	 * (Currently only Ameristar.)
    171 	 */
    172 	sc->sc_arpcom.ac_enaddr[0] = 0x00;
    173 	sc->sc_arpcom.ac_enaddr[1] = 0x00;
    174 	sc->sc_arpcom.ac_enaddr[2] = 0x9f;
    175 
    176 	/*
    177 	 * Serial number for board contains last 3 bytes.
    178 	 */
    179 	ser = (unsigned long) zap->serno;
    180 
    181 	sc->sc_arpcom.ac_enaddr[3] = (ser >> 16) & 0xff;
    182 	sc->sc_arpcom.ac_enaddr[4] = (ser >>  8) & 0xff;
    183 	sc->sc_arpcom.ac_enaddr[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_output = ether_output;
    189 	ifp->if_ioctl = esioctl;
    190 	ifp->if_start = esstart;
    191 	ifp->if_watchdog = eswatchdog;
    192 	/* XXX IFF_MULTICAST */
    193 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    194 
    195 	/* Attach the interface. */
    196 	if_attach(ifp);
    197 	ether_ifattach(ifp);
    198 
    199 	/* Print additional info when attached. */
    200 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    201 
    202 #if NBPFILTER > 0
    203 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    204 #endif
    205 
    206 	sc->sc_isr.isr_intr = esintr;
    207 	sc->sc_isr.isr_arg = sc;
    208 	sc->sc_isr.isr_ipl = 2;
    209 	add_isr(&sc->sc_isr);
    210 }
    211 
    212 #ifdef ESDEBUG
    213 void
    214 es_dump_smcregs(where, smc)
    215 	char *where;
    216 	union smcregs *smc;
    217 {
    218 	u_short cur_bank = smc->b0.bsr & BSR_MASK;
    219 
    220 	printf("SMC registers %p from %s bank %04x\n", smc, where,
    221 	    smc->b0.bsr);
    222 	smc->b0.bsr = BSR_BANK0;
    223 	printf("TCR %04x EPHSR %04x RCR %04x ECR %04x MIR %04x MCR %04x\n",
    224 	    SWAP(smc->b0.tcr), SWAP(smc->b0.ephsr), SWAP(smc->b0.rcr),
    225 	    SWAP(smc->b0.ecr), SWAP(smc->b0.mir), SWAP(smc->b0.mcr));
    226 	smc->b1.bsr = BSR_BANK1;
    227 	printf("CR %04x BAR %04x IAR %04x %04x %04x GPR %04x CTR %04x\n",
    228 	    SWAP(smc->b1.cr), SWAP(smc->b1.bar), smc->b1.iar[0], smc->b1.iar[1],
    229 	    smc->b1.iar[2], smc->b1.gpr, SWAP(smc->b1.ctr));
    230 	smc->b2.bsr = BSR_BANK2;
    231 	printf("MMUCR %04x PNR %02x ARR %02x FIFO %04x PTR %04x",
    232 	    SWAP(smc->b2.mmucr), smc->b2.pnr, smc->b2.arr, smc->b2.fifo,
    233 	    SWAP(smc->b2.ptr));
    234 	printf(" DATA %04x %04x IST %02x MSK %02x\n", smc->b2.data,
    235 	    smc->b2.datax, smc->b2.ist, smc->b2.msk);
    236 	smc->b3.bsr = BSR_BANK3;
    237 	printf("MT %04x %04x %04x %04x\n",
    238 	    smc->b3.mt[0], smc->b3.mt[1], smc->b3.mt[2], smc->b3.mt[3]);
    239 	smc->b3.bsr = cur_bank;
    240 }
    241 #endif
    242 
    243 void
    244 esstop(sc)
    245 	struct es_softc* sc;
    246 {
    247 }
    248 
    249 void
    250 esinit(sc)
    251 	struct es_softc *sc;
    252 {
    253 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    254 	union smcregs *smc = sc->sc_base;
    255 	int s;
    256 
    257 	s = splnet();
    258 
    259 #ifdef ESDEBUG
    260 	if (ifp->if_flags & IFF_RUNNING)
    261 		es_dump_smcregs("esinit", smc);
    262 #endif
    263 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    264 	smc->b0.rcr = RCR_EPH_RST;
    265 	smc->b0.rcr = 0;
    266 	smc->b3.bsr = BSR_BANK3;	/* Select bank 3 */
    267 	smc->b3.mt[0] = 0;		/* clear Multicast table */
    268 	smc->b3.mt[1] = 0;
    269 	smc->b3.mt[2] = 0;
    270 	smc->b3.mt[3] = 0;
    271 	/* XXX set Multicast table from Multicast list */
    272 	smc->b1.bsr = BSR_BANK1;	/* Select bank 1 */
    273 	smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
    274 	smc->b1.ctr = CTR_AUTO_RLSE;
    275 	smc->b1.iar[0] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[0]);
    276 	smc->b1.iar[1] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[2]);
    277 	smc->b1.iar[2] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[4]);
    278 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    279 	smc->b2.mmucr = MMUCR_RESET;
    280 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    281 	smc->b0.mcr = SWAP(0x0020);	/* reserve 8K for transmit buffers */
    282 	smc->b0.tcr = TCR_PAD_EN | (TCR_TXENA + TCR_MON_CSN);
    283 	smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN;
    284 	/* XXX add multicast/promiscuous flags */
    285 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    286 	smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
    287 
    288 	/* Interface is now 'running', with no output active. */
    289 	ifp->if_flags |= IFF_RUNNING;
    290 	ifp->if_flags &= ~IFF_OACTIVE;
    291 
    292 	/* Attempt to start output, if any. */
    293 	esstart(ifp);
    294 
    295 	splx(s);
    296 }
    297 
    298 int
    299 esintr(arg)
    300 	void *arg;
    301 {
    302 	struct es_softc *sc = arg;
    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 	    sc->sc_arpcom.ac_if.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 			sc->sc_arpcom.ac_if.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 		sc->sc_arpcom.ac_if.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 				sc->sc_arpcom.ac_if.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 			sc->sc_arpcom.ac_if.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 #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 		++sc->sc_arpcom.ac_if.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 = &sc->sc_arpcom.ac_if;
    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 (sc->sc_arpcom.ac_if.if_bpf) {
    718 		bpf_mtap(sc->sc_arpcom.ac_if.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_arpcom.ac_if.if_flags & IFF_PROMISC) &&
    726 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    727 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    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_arpcom.ac_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_arpcom.ac_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_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",
    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_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 & 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_arpcom.ac_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_arpcom.ac_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_arpcom.ac_if.if_bpf)
    942 			bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    943 #endif
    944 		m_freem(m0);
    945 		sc->sc_arpcom.ac_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(&sc->sc_arpcom, 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 *)(sc->sc_arpcom.ac_enaddr);
    995 			else
    996 				bcopy(ina->x_host.c_host,
    997 				    sc->sc_arpcom.ac_enaddr,
    998 				    sizeof(sc->sc_arpcom.ac_enaddr));
    999 			/* Set new address. */
   1000 			esinit(sc);
   1001 			break;
   1002 		    }
   1003 #endif
   1004 		default:
   1005 			esinit(sc);
   1006 			break;
   1007 		}
   1008 		break;
   1009 
   1010 	case SIOCSIFFLAGS:
   1011 		/*
   1012 		 * If interface is marked down and it is running, then stop it
   1013 		 */
   1014 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1015 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1016 			/*
   1017 			 * If interface is marked down and it is running, then
   1018 			 * stop it.
   1019 			 */
   1020 			esstop(sc);
   1021 			ifp->if_flags &= ~IFF_RUNNING;
   1022 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1023 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1024 			/*
   1025 			 * If interface is marked up and it is stopped, then
   1026 			 * start it.
   1027 			 */
   1028 			esinit(sc);
   1029 		} else {
   1030 			/*
   1031 			 * Reset the interface to pick up changes in any other
   1032 			 * flags that affect hardware registers.
   1033 			 */
   1034 			esstop(sc);
   1035 			esinit(sc);
   1036 		}
   1037 #ifdef ESDEBUG
   1038 		if (ifp->if_flags & IFF_DEBUG)
   1039 			esdebug = sc->sc_debug = 1;
   1040 		else
   1041 			esdebug = sc->sc_debug = 0;
   1042 #endif
   1043 		break;
   1044 
   1045 	case SIOCADDMULTI:
   1046 	case SIOCDELMULTI:
   1047 		error = (command == SIOCADDMULTI) ?
   1048 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1049 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1050 
   1051 		if (error == ENETRESET) {
   1052 			/*
   1053 			 * Multicast list has changed; set the hardware filter
   1054 			 * accordingly.
   1055 			 */
   1056 			/* XXX */
   1057 			error = 0;
   1058 		}
   1059 		break;
   1060 
   1061 	default:
   1062 		error = EINVAL;
   1063 	}
   1064 
   1065 	splx(s);
   1066 	return (error);
   1067 }
   1068 
   1069 void
   1070 esreset(sc)
   1071 	struct es_softc *sc;
   1072 {
   1073 	int s;
   1074 
   1075 	s = splnet();
   1076 	esstop(sc);
   1077 	esinit(sc);
   1078 	splx(s);
   1079 }
   1080 
   1081 void
   1082 eswatchdog(ifp)
   1083 	struct ifnet *ifp;
   1084 {
   1085 	struct es_softc *sc = ifp->if_softc;
   1086 
   1087 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1088 	++sc->sc_arpcom.ac_if.if_oerrors;
   1089 
   1090 	esreset(sc);
   1091 }
   1092