Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.22
      1 /*	$NetBSD: if_es.c,v 1.22 1998/07/05 06:49:03 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 #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_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, myaddr);
    198 
    199 	/* Print additional info when attached. */
    200 	printf(": address %s\n", ether_sprintf(myaddr));
    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_ethercom.ec_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 *) &LLADDR(ifp->if_sadl)[0]);
    276 	smc->b1.iar[1] = *((unsigned short *) &LLADDR(ifp->if_sadl)[2]);
    277 	smc->b1.iar[2] = *((unsigned short *) &LLADDR(ifp->if_sadl)[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 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    304 	u_short intsts, intact;
    305 	union smcregs *smc;
    306 	int s = splnet();
    307 
    308 	smc = sc->sc_base;
    309 #ifdef ESDEBUG
    310 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2 &&
    311 	    ifp->if_flags & IFF_RUNNING) {
    312 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    313 		    smc->b2.bsr);
    314 		smc->b2.bsr = BSR_BANK2;
    315 	}
    316 #endif
    317 	intsts = smc->b2.ist;
    318 	intact = smc->b2.msk & intsts;
    319 	if ((intact) == 0) {
    320 		splx(s);
    321 		return (0);
    322 	}
    323 #ifdef ESDEBUG
    324 	if (esdebug)
    325 		printf ("%s: esintr ist %02x msk %02x",
    326 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
    327 	if (sc->sc_intbusy++) {
    328 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
    329 		panic("esintr re-entered");
    330 	}
    331 	if (sc->sc_smcbusy)
    332 		printf("%s: esintr interrupted busy %d\n", sc->sc_dev.dv_xname,
    333 		    sc->sc_smcbusy);
    334 #endif
    335 	smc->b2.msk = 0;
    336 #ifdef ESDEBUG
    337 	if (esdebug)
    338 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    339 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    340 		    smc->b2.fifo);
    341 #endif
    342 	if (intact & IST_ALLOC) {
    343 		sc->sc_intctl &= ~MSK_ALLOC;
    344 #ifdef ESDEBUG
    345 		if (esdebug || 1)
    346 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
    347 			    intsts);
    348 #endif
    349 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    350 			u_char save_pnr;
    351 #ifdef ESDEBUG
    352 			if (esdebug || 1)
    353 				printf (" arr %02x\n", smc->b2.arr);
    354 #endif
    355 			save_pnr = smc->b2.pnr;
    356 			smc->b2.pnr = smc->b2.arr;
    357 			smc->b2.mmucr = MMUCR_RLSPKT;
    358 			while (smc->b2.mmucr & MMUCR_BUSY)
    359 				;
    360 			smc->b2.pnr = save_pnr;
    361 			ifp->if_flags &= ~IFF_OACTIVE;
    362 		}
    363 #ifdef ESDEBUG
    364 		else if (esdebug || 1)
    365 			printf (" IST_ALLOC with ARR_FAILED?\n");
    366 #endif
    367 	}
    368 #ifdef ESDEBUG
    369 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    370 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    371 		    smc->b2.bsr);
    372 		smc->b2.bsr = BSR_BANK2;
    373 	}
    374 #endif
    375 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    376 		esrint(sc);
    377 	}
    378 #ifdef ESDEBUG
    379 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    380 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    381 		    smc->b2.bsr);
    382 		smc->b2.bsr = BSR_BANK2;
    383 	}
    384 #endif
    385 	if (intact & IST_RX_OVRN) {
    386 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    387 		    intsts);
    388 		smc->b2.ist = ACK_RX_OVRN;
    389 		printf ("->%02x\n", smc->b2.ist);
    390 		ifp->if_ierrors++;
    391 	}
    392 	if (intact & IST_TX_EMPTY) {
    393 		u_short ecr;
    394 #ifdef ESDEBUG
    395 		if (esdebug)
    396 			printf ("%s: TX EMPTY %02x",
    397 			    sc->sc_dev.dv_xname, intsts);
    398 		++estxint5;		/* count # IST_TX_EMPTY ints */
    399 #endif
    400 		smc->b2.ist = ACK_TX_EMPTY;
    401 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    402 #ifdef ESDEBUG
    403 		if (esdebug)
    404 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    405 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    406 			    smc->b2.arr);
    407 #endif
    408 		if (smc->b2.ist & IST_TX) {
    409 			intact |= IST_TX;
    410 #ifdef ESDEBUG
    411 			++estxint2;		/* count # TX after TX_EMPTY */
    412 #endif
    413 		} else {
    414 			smc->b0.bsr = BSR_BANK0;
    415 			ecr = smc->b0.ecr;	/* Get error counters */
    416 			if (ecr & 0xff00)
    417 				ifp->if_collisions += ((ecr >> 8) & 15) +
    418 				    ((ecr >> 11) & 0x1e);
    419 			smc->b2.bsr = BSR_BANK2;
    420 #if 0
    421 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
    422 #endif
    423 		}
    424 	}
    425 	if (intact & IST_TX) {
    426 		u_char tx_pnr, save_pnr;
    427 		u_short save_ptr, ephsr, tcr;
    428 		int n = 0;
    429 #ifdef ESDEBUG
    430 		if (esdebug) {
    431 			printf ("%s: TX INT ist %02x",
    432 			    sc->sc_dev.dv_xname, intsts);
    433 			printf ("->%02x\n", smc->b2.ist);
    434 		}
    435 		++estxint3;			/* count # IST_TX */
    436 #endif
    437 zzzz:
    438 #ifdef ESDEBUG
    439 		++estxint4;			/* count # ~TEMPTY */
    440 #endif
    441 		smc->b0.bsr = BSR_BANK0;
    442 		ephsr = smc->b0.ephsr;		/* get EPHSR */
    443 		tcr = smc->b0.tcr;		/* and TCR */
    444 		smc->b2.bsr = BSR_BANK2;
    445 		save_ptr = smc->b2.ptr;
    446 		save_pnr = smc->b2.pnr;
    447 		tx_pnr = smc->b2.fifo >> 8;	/* pktno from completion fifo */
    448 		smc->b2.pnr = tx_pnr;		/* set TX packet number */
    449 		smc->b2.ptr = PTR_READ;		/* point to status word */
    450 #if 0 /* XXXX */
    451 		printf("%s: esintr TXINT IST %02x PNR %02x(%d)",
    452 		    sc->sc_dev.dv_xname, smc->b2.ist,
    453 		    tx_pnr, n);
    454 		printf(" Status %04x", smc->b2.data);
    455 		printf(" EPHSR %04x\n", ephsr);
    456 #endif
    457 		if ((smc->b2.data & EPHSR_TX_SUC) == 0 && (tcr & TCR_TXENA) == 0) {
    458 			/*
    459 			 * Transmitter was stopped for some error.  Enqueue
    460 			 * the packet again and restart the transmitter.
    461 			 * May need some check to limit the number of retries.
    462 			 */
    463 			smc->b2.mmucr = MMUCR_ENQ_TX;
    464 			smc->b0.bsr = BSR_BANK0;
    465 			smc->b0.tcr |= TCR_TXENA;
    466 			smc->b2.bsr = BSR_BANK2;
    467 			ifp->if_oerrors++;
    468 			sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    469 		} else {
    470 			/*
    471 			 * This shouldn't have happened:  IST_TX indicates
    472 			 * the TX completion FIFO is not empty, but the
    473 			 * status for the packet on the completion FIFO
    474 			 * shows that the transmit was sucessful.  Since
    475 			 * AutoRelease is being used, a sucessful transmit
    476 			 * should not result in a packet on the completion
    477 			 * FIFO.  Also, that packet doesn't seem to want
    478 			 * to be acknowledged.  If this occurs, just reset
    479 			 * the TX FIFOs.
    480 			 */
    481 #if 1
    482 			if (smc->b2.ist & IST_TX_EMPTY) {
    483 				smc->b2.mmucr = MMUCR_RESET_TX;
    484 				sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    485 			}
    486 #endif
    487 #ifdef ESDEBUG
    488 			++estxints;	/* count IST_TX with TX enabled */
    489 #endif
    490 		}
    491 		smc->b2.pnr = save_pnr;
    492 		smc->b2.ptr = save_ptr;
    493 		smc->b2.ist = ACK_TX;
    494 
    495 		if ((smc->b2.fifo & FIFO_TEMPTY) == 0 && n++ < 32) {
    496 #if 0 /* XXXX */
    497 			printf("%s: multiple TX int(%2d) pnr %02x ist %02x fifo %04x",
    498 			    sc->sc_dev.dv_xname, n, tx_pnr, smc->b2.ist, smc->b2.fifo);
    499 			smc->w2.istmsk = ACK_TX << 8;
    500 			printf(" %04x\n", smc->b2.fifo);
    501 #endif
    502 			if (tx_pnr != (smc->b2.fifo >> 8))
    503 				goto zzzz;
    504 		}
    505 	}
    506 	/* output packets */
    507 	estint(sc);
    508 #ifdef ESDEBUG
    509 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    510 		printf("%s: intr+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    511 		    smc->b2.bsr);
    512 		smc->b2.bsr = BSR_BANK2;
    513 	}
    514 #endif
    515 	smc->b2.msk = sc->sc_intctl;
    516 #ifdef ESDEBUG
    517 	if (--sc->sc_intbusy) {
    518 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    519 		panic("esintr busy on exit");
    520 	}
    521 #endif
    522 	splx(s);
    523 	return (1);
    524 }
    525 
    526 void
    527 esrint(sc)
    528 	struct es_softc *sc;
    529 {
    530 	union smcregs *smc = sc->sc_base;
    531 	u_short len;
    532 	short cnt;
    533 	u_short pktctlw, pktlen, *buf;
    534 	volatile u_short *data;
    535 #if 0
    536 	u_long *lbuf;
    537 	volatile u_long *ldata;
    538 #endif
    539 	struct ifnet *ifp;
    540 	struct mbuf *top, **mp, *m;
    541 	struct ether_header *eh;
    542 #ifdef USEPKTBUF
    543 	u_char *b, pktbuf[1530];
    544 #endif
    545 #ifdef ESDEBUG
    546 	int i;
    547 #endif
    548 
    549 	ifp = &sc->sc_ethercom.ec_if;
    550 #ifdef ESDEBUG
    551 	if (esdebug)
    552 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    553 		    smc->b2.fifo);
    554 	if (sc->sc_smcbusy++) {
    555 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
    556 		panic("esrint re-entered");
    557 	}
    558 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    559 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    560 		    smc->b2.bsr);
    561 		smc->b2.bsr = BSR_BANK2;
    562 	}
    563 #endif
    564 	data = (u_short *)&smc->b2.data;
    565 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
    566 	(void) smc->b2.mmucr;
    567 #ifdef ESDEBUG
    568 	if (esdebug)
    569 		printf ("->%04x", smc->b2.fifo);
    570 #endif
    571 	len = *data;
    572 	len = SWAP(len);	/* Careful of macro side-effects */
    573 #ifdef ESDEBUG
    574 	if (esdebug)
    575 		printf (" length %d", len);
    576 #endif
    577 	smc->b2.ptr = PTR_RCV | (PTR_AUTOINCR + PTR_READ) | SWAP(0x0000);
    578 	(void) smc->b2.mmucr;
    579 	pktctlw = *data;
    580 	pktlen = *data;
    581 	pktctlw = SWAP(pktctlw);
    582 	pktlen = SWAP(pktlen) - 6;
    583 	if (pktctlw & RFSW_ODDFRM)
    584 		pktlen++;
    585 	if (len > 1530) {
    586 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
    587 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
    588 		/* XXX ignore packet, or just truncate? */
    589 #if defined(ESDEBUG) && defined(DDB)
    590 		if ((smc->b2.bsr & BSR_MASK) != BSR_BANK2)
    591 			Debugger();
    592 #endif
    593 		smc->b2.bsr = BSR_BANK2;
    594 		smc->b2.mmucr = MMUCR_REMRLS_RX;
    595 		while (smc->b2.mmucr & MMUCR_BUSY)
    596 			;
    597 		++ifp->if_ierrors;
    598 #ifdef ESDEBUG
    599 		if (--sc->sc_smcbusy) {
    600 			printf("%s: esrintr busy on bad packet exit\n",
    601 			    sc->sc_dev.dv_xname);
    602 			panic("esrintr busy on exit");
    603 		}
    604 #endif
    605 		return;
    606 	}
    607 #ifdef USEPKTBUF
    608 #if 0
    609 	lbuf = (u_long *) pktbuf;
    610 	ldata = (u_long *)data;
    611 	cnt = (len - 4) / 4;
    612 	while (cnt--)
    613 		*lbuf++ = *ldata;
    614 	if ((len - 4) & 2) {
    615 		buf = (u_short *) lbuf;
    616 		*buf = *data;
    617 	}
    618 #else
    619 	buf = (u_short *)pktbuf;
    620 	cnt = (len - 4) / 2;
    621 	while (cnt--)
    622 		*buf++ = *data;
    623 #endif
    624 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    625 	while (smc->b2.mmucr & MMUCR_BUSY)
    626 		;
    627 #ifdef ESDEBUG
    628 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    629 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    630 		/* count input error? */
    631 	}
    632 	if (esdebug) {
    633 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    634 		    smc->b2.fifo);
    635 		for (i = 0; i < pktlen; ++i)
    636 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    637 			    "");
    638 		if (i & 31)
    639 			printf ("\n");
    640 	}
    641 #endif
    642 #else	/* USEPKTBUF */
    643 	/* XXX copy directly from controller to mbuf */
    644 #ifdef ESDEBUG
    645 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    646 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    647 		/* count input error? */
    648 	}
    649 	if (esdebug) {
    650 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    651 		    smc->b2.fifo);
    652 	}
    653 #endif
    654 #endif /* USEPKTBUF */
    655 	ifp->if_ipackets++;
    656 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    657 	if (m == NULL)
    658 		return;
    659 	m->m_pkthdr.rcvif = ifp;
    660 	m->m_pkthdr.len = pktlen;
    661 	len = MHLEN;
    662 	top = NULL;
    663 	mp = &top;
    664 #ifdef USEPKTBUF
    665 	b = pktbuf;
    666 #endif
    667 	while (pktlen > 0) {
    668 		if (top) {
    669 			MGET(m, M_DONTWAIT, MT_DATA);
    670 			if (m == 0) {
    671 				m_freem(top);
    672 				return;
    673 			}
    674 			len = MLEN;
    675 		}
    676 		if (pktlen >= MINCLSIZE) {
    677 			MCLGET(m, M_DONTWAIT);
    678 			if (m->m_flags & M_EXT)
    679 				len = MCLBYTES;
    680 		}
    681 		m->m_len = len = min(pktlen, len);
    682 #ifdef USEPKTBUF
    683 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    684 		b += len;
    685 #else	/* USEPKTBUF */
    686 		buf = mtod(m, u_short *);
    687 		cnt = len / 2;
    688 		while (cnt--)
    689 			*buf++ = *data;
    690 		if (len & 1)
    691 			*buf = *data;	/* XXX should be byte store */
    692 #ifdef ESDEBUG
    693 		if (esdebug) {
    694 			buf = mtod(m, u_short *);
    695 			for (i = 0; i < len; ++i)
    696 				printf ("%02x%s", ((u_char *)buf)[i],
    697 				    ((i & 31) == 31) ? "\n" : "");
    698 			if (i & 31)
    699 				printf ("\n");
    700 		}
    701 #endif
    702 #endif	/* USEPKTBUF */
    703 		pktlen -= len;
    704 		*mp = m;
    705 		mp = &m->m_next;
    706 	}
    707 #ifndef USEPKTBUF
    708 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    709 	while (smc->b2.mmucr & MMUCR_BUSY)
    710 		;
    711 #endif
    712 	eh = mtod(top, struct ether_header *);
    713 #if NBPFILTER > 0
    714 	/*
    715 	 * Check if there's a BPF listener on this interface.  If so, hand off
    716 	 * the raw packet to bpf.
    717 	 */
    718 	if (ifp->if_bpf) {
    719 		bpf_mtap(ifp->if_bpf, top);
    720 
    721 		/*
    722 		 * Note that the interface cannot be in promiscuous mode if
    723 		 * there are no BPF listeners.  And if we are in promiscuous
    724 		 * mode, we have to check if this packet is really ours.
    725 		 */
    726 		if ((sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC) &&
    727 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    728 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    729 			    sizeof(eh->ether_dhost)) != 0) {
    730 			m_freem(top);
    731 			return;
    732 		}
    733 	}
    734 #endif
    735 	top->m_pkthdr.len -= sizeof (*eh);
    736 	top->m_len -= sizeof (*eh);
    737 	top->m_data += sizeof (*eh);
    738 
    739 	ether_input(ifp, eh, top);
    740 #ifdef ESDEBUG
    741 	if (--sc->sc_smcbusy) {
    742 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    743 		panic("esintr busy on exit");
    744 	}
    745 #endif
    746 }
    747 
    748 void
    749 estint(sc)
    750 	struct es_softc *sc;
    751 {
    752 
    753 	esstart(&sc->sc_ethercom.ec_if);
    754 }
    755 
    756 void
    757 esstart(ifp)
    758 	struct ifnet *ifp;
    759 {
    760 	struct es_softc *sc = ifp->if_softc;
    761 	union smcregs *smc = sc->sc_base;
    762 	struct mbuf *m0, *m;
    763 #ifdef USEPKTBUF
    764 	u_short pktbuf[ETHERMTU + 2];
    765 #else
    766 	u_short oddbyte, needbyte;
    767 #endif
    768 	u_short pktctlw, pktlen;
    769 	u_short *buf;
    770 	volatile u_short *data;
    771 #if 0
    772 	u_long *lbuf;
    773 	volatile u_long *ldata;
    774 #endif
    775 	short cnt;
    776 	int i;
    777 	u_char active_pnr;
    778 
    779 	if ((sc->sc_ethercom.ec_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    780 	    IFF_RUNNING)
    781 		return;
    782 
    783 #ifdef ESDEBUG
    784 	if (sc->sc_smcbusy++) {
    785 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    786 		panic("esstart re-entred");
    787 	}
    788 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    789 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    790 		    smc->b2.bsr);
    791 		smc->b2.bsr = BSR_BANK2;
    792 	}
    793 #endif
    794 	for (;;) {
    795 #ifdef ESDEBUG
    796 		u_short start_ptr, end_ptr;
    797 #endif
    798 		/*
    799 		 * Sneak a peek at the next packet to get the length
    800 		 * and see if the SMC 91C90 can accept it.
    801 		 */
    802 		m = sc->sc_ethercom.ec_if.if_snd.ifq_head;
    803 		if (!m)
    804 			break;
    805 #ifdef ESDEBUG
    806 		if (esdebug && (m->m_next || m->m_len & 1))
    807 			printf("%s: esstart m_next %p m_len %d\n",
    808 			    sc->sc_dev.dv_xname, m->m_next, m->m_len);
    809 #endif
    810 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    811 			pktlen += m0->m_len;
    812 		pktctlw = 0;
    813 		pktlen += 4;
    814 		if (pktlen & 1)
    815 			++pktlen;	/* control byte after last byte */
    816 		else
    817 			pktlen += 2;	/* control byte after pad byte */
    818 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    819 		for (i = 0; i <= 5; ++i)
    820 			if ((smc->b2.arr & ARR_FAILED) == 0)
    821 				break;
    822 		if (smc->b2.arr & ARR_FAILED) {
    823 			sc->sc_ethercom.ec_if.if_flags |= IFF_OACTIVE;
    824 			sc->sc_intctl |= MSK_ALLOC;
    825 			break;
    826 		}
    827 		active_pnr = smc->b2.pnr = smc->b2.arr;
    828 
    829 #ifdef ESDEBUG
    830 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    831 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    832 			    smc->b2.bsr);
    833 		smc->b2.bsr = BSR_BANK2;
    834 		}
    835 #endif
    836 		IF_DEQUEUE(&sc->sc_ethercom.ec_if.if_snd, m);
    837 		smc->b2.ptr = PTR_AUTOINCR;
    838 		(void) smc->b2.mmucr;
    839 		data = (u_short *)&smc->b2.data;
    840 		*data = SWAP(pktctlw);
    841 		*data = SWAP(pktlen);
    842 #ifdef ESDEBUG
    843 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    844 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    845 			    smc->b2.bsr);
    846 			smc->b2.bsr = BSR_BANK2;
    847 		}
    848 #endif
    849 #ifdef USEPKTBUF
    850 		i = 0;
    851 		for (m0 = m; m; m = m->m_next) {
    852 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    853 			i += m->m_len;
    854 		}
    855 
    856 		if (i & 1)	/* Figure out where to put control byte */
    857 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    858 		else
    859 			pktbuf[i/2] = 0;
    860 		pktlen -= 4;
    861 #ifdef ESDEBUG
    862 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
    863 			printf("%s: esstart packet longer than pktbuf\n",
    864 			    sc->sc_dev.dv_xname);
    865 #endif
    866 #if 0 /* doesn't quite work? */
    867 		lbuf = (u_long *)(pktbuf);
    868 		ldata = (u_long *)data;
    869 		cnt = pktlen / 4;
    870 		while(cnt--)
    871 			*ldata = *lbuf++;
    872 		if (pktlen & 2) {
    873 			buf = (u_short *)lbuf;
    874 			*data = *buf;
    875 		}
    876 #else
    877 #ifdef ESDEBUG
    878 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    879 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    880 			    smc->b2.bsr);
    881 			smc->b2.bsr = BSR_BANK2;
    882 		}
    883 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
    884 #endif
    885 		buf = pktbuf;
    886 		cnt = pktlen / 2;
    887 		while (cnt--)
    888 			*data = *buf++;
    889 #ifdef ESDEBUG
    890 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
    891 #endif
    892 #endif
    893 #else	/* USEPKTBUF */
    894 		pktctlw = 0;
    895 		oddbyte = needbyte = 0;
    896 		for (m0 = m; m; m = m->m_next) {
    897 			buf = mtod(m, u_short *);
    898 			cnt = m->m_len / 2;
    899 			if (needbyte) {
    900 				oddbyte |= *buf >> 8;
    901 				*data = oddbyte;
    902 			}
    903 			while (cnt--)
    904 				*data = *buf++;
    905 			if (m->m_len & 1)
    906 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    907 			if (m->m_len & 1 && m->m_next)
    908 				printf("%s: esstart odd byte count in mbuf\n",
    909 				    sc->sc_dev.dv_xname);
    910 		}
    911 		*data = pktctlw;
    912 #endif	/* USEPKTBUF */
    913 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    914 			/*
    915 			 * The bank select register has changed.  This seems
    916 			 * to happen with my A2000/Zeus once in a while.  It
    917 			 * appears that the Ethernet chip resets while
    918 			 * copying the transmit buffer.  Requeue the current
    919 			 * transmit buffer and reinitialize the interface.
    920 			 * The initialize routine will take care of
    921 			 * retransmitting the buffer.  mhitch
    922 			 */
    923 #ifdef DIAGNOSTIC
    924 			printf("%s: esstart+++ BSR not 2: %04x\n",
    925 			    sc->sc_dev.dv_xname, smc->b2.bsr);
    926 #endif
    927 			smc->b2.bsr = BSR_BANK2;
    928 #ifdef ESDEBUG
    929 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
    930 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
    931 			--sc->sc_smcbusy;
    932 #endif
    933 			IF_PREPEND(&sc->sc_ethercom.ec_if.if_snd, m0);
    934 			esinit(sc);	/* It's really hosed - reset */
    935 			return;
    936 		}
    937 		smc->b2.mmucr = MMUCR_ENQ_TX;
    938 		if (smc->b2.pnr != active_pnr)
    939 			printf("%s: esstart - PNR changed %x->%x\n",
    940 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    941 #if NBPFILTER > 0
    942 		if (sc->sc_ethercom.ec_if.if_bpf)
    943 			bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m0);
    944 #endif
    945 		m_freem(m0);
    946 		sc->sc_ethercom.ec_if.if_opackets++;	/* move to interrupt? */
    947 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    948 	}
    949 	smc->b2.msk = sc->sc_intctl;
    950 #ifdef ESDEBUG
    951 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    952 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    953 		    smc->b2.bsr);
    954 		smc->b2.bsr = BSR_BANK2;
    955 	}
    956 	if (--sc->sc_smcbusy) {
    957 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    958 		panic("esstart busy on exit");
    959 	}
    960 #endif
    961 }
    962 
    963 int
    964 esioctl(ifp, command, data)
    965 	register struct ifnet *ifp;
    966 	u_long command;
    967 	caddr_t data;
    968 {
    969 	struct es_softc *sc = ifp->if_softc;
    970 	register struct ifaddr *ifa = (struct ifaddr *)data;
    971 	struct ifreq *ifr = (struct ifreq *)data;
    972 	int s, error = 0;
    973 
    974 	s = splnet();
    975 
    976 	switch (command) {
    977 
    978 	case SIOCSIFADDR:
    979 		ifp->if_flags |= IFF_UP;
    980 
    981 		switch (ifa->ifa_addr->sa_family) {
    982 #ifdef INET
    983 		case AF_INET:
    984 			esinit(sc);
    985 			arp_ifinit(ifp, ifa);
    986 			break;
    987 #endif
    988 #ifdef NS
    989 		case AF_NS:
    990 		    {
    991 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    992 
    993 			if (ns_nullhost(*ina))
    994 				ina->x_host =
    995 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    996 			else
    997 				bcopy(ina->x_host.c_host,
    998 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    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_ethercom) :
   1049 		    ether_delmulti(ifr, &sc->sc_ethercom);
   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 	++ifp->if_oerrors;
   1089 
   1090 	esreset(sc);
   1091 }
   1092