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