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