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