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