Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.29
      1 /*	$NetBSD: if_es.c,v 1.29 2002/03/02 21:08:04 mhitch Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995 Michael L. Hitch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Michael L. Hitch.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * SMC 91C90 Single-Chip Ethernet Controller
     35  */
     36 #include "opt_ddb.h"
     37 #include "opt_inet.h"
     38 #include "opt_ns.h"
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: if_es.c,v 1.29 2002/03/02 21:08:04 mhitch Exp $");
     42 
     43 #include "bpfilter.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/buf.h>
     49 #include <sys/protosw.h>
     50 #include <sys/socket.h>
     51 #include <sys/syslog.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/device.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_ether.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.h>
     65 #include <netinet/if_inarp.h>
     66 #endif
     67 
     68 #ifdef NS
     69 #include <netns/ns.h>
     70 #include <netns/ns_if.h>
     71 #endif
     72 
     73 #include <machine/cpu.h>
     74 #include <machine/mtpr.h>
     75 #include <amiga/amiga/device.h>
     76 #include <amiga/amiga/isr.h>
     77 #include <amiga/dev/zbusvar.h>
     78 #include <amiga/dev/if_esreg.h>
     79 
     80 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     81 
     82 #define	USEPKTBUF
     83 
     84 /*
     85  * Ethernet software status per interface.
     86  *
     87  * Each interface is referenced by a network interface structure,
     88  * es_if, which the routing code uses to locate the interface.
     89  * This structure contains the output queue for the interface, its address, ...
     90  */
     91 struct	es_softc {
     92 	struct	device sc_dev;
     93 	struct	isr sc_isr;
     94 	struct	ethercom sc_ethercom;	/* common Ethernet structures */
     95 	void	*sc_base;		/* base address of board */
     96 	short	sc_iflags;
     97 	unsigned short sc_intctl;
     98 #ifdef ESDEBUG
     99 	int	sc_debug;
    100 	short	sc_intbusy;		/* counter for interrupt rentered */
    101 	short	sc_smcbusy;		/* counter for other rentry checks */
    102 #endif
    103 };
    104 
    105 #if NBPFILTER > 0
    106 #include <net/bpf.h>
    107 #include <net/bpfdesc.h>
    108 #endif
    109 
    110 #ifdef ESDEBUG
    111 /* console error messages */
    112 int	esdebug = 0;
    113 int	estxints = 0;	/* IST_TX with TX enabled */
    114 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    115 int	estxint3 = 0;	/* IST_TX interrupt processed */
    116 int	estxint4 = 0;	/* ~TEMPTY counts */
    117 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    118 void	es_dump_smcregs(char *, union smcregs *);
    119 #endif
    120 
    121 int esintr(void *);
    122 void esstart(struct ifnet *);
    123 void eswatchdog(struct ifnet *);
    124 int esioctl(struct ifnet *, u_long, caddr_t);
    125 void esrint(struct es_softc *);
    126 void estint(struct es_softc *);
    127 void esinit(struct es_softc *);
    128 void esreset(struct es_softc *);
    129 void esstop(struct es_softc *);
    130 
    131 int esmatch(struct device *, struct cfdata *, void *);
    132 void esattach(struct device *, struct device *, void *);
    133 
    134 struct cfattach es_ca = {
    135 	sizeof(struct es_softc), esmatch, esattach
    136 };
    137 
    138 int
    139 esmatch(struct device *parent, struct cfdata *cfp, void *aux)
    140 {
    141 	struct zbus_args *zap = aux;
    142 
    143 	/* Ameristar A4066 ethernet card */
    144 	if (zap->manid == 1053 && zap->prodid == 10)
    145 		return(1);
    146 
    147 	return (0);
    148 }
    149 
    150 /*
    151  * Interface exists: make available by filling in network interface
    152  * record.  System will initialize the interface when it is ready
    153  * to accept packets.
    154  */
    155 void
    156 esattach(struct device *parent, struct device *self, void *aux)
    157 {
    158 	struct es_softc *sc = (void *)self;
    159 	struct zbus_args *zap = aux;
    160 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    161 	unsigned long ser;
    162 	u_int8_t myaddr[ETHER_ADDR_LEN];
    163 
    164 	sc->sc_base = zap->va;
    165 
    166 	/*
    167 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    168 	 * (Currently only Ameristar.)
    169 	 */
    170 	myaddr[0] = 0x00;
    171 	myaddr[1] = 0x00;
    172 	myaddr[2] = 0x9f;
    173 
    174 	/*
    175 	 * Serial number for board contains last 3 bytes.
    176 	 */
    177 	ser = (unsigned long) zap->serno;
    178 
    179 	myaddr[3] = (ser >> 16) & 0xff;
    180 	myaddr[4] = (ser >>  8) & 0xff;
    181 	myaddr[5] = (ser      ) & 0xff;
    182 
    183 	/* Initialize ifnet structure. */
    184 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    185 	ifp->if_softc = sc;
    186 	ifp->if_ioctl = esioctl;
    187 	ifp->if_start = esstart;
    188 	ifp->if_watchdog = eswatchdog;
    189 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
    190 	    IFF_MULTICAST;
    191 
    192 	/* Attach the interface. */
    193 	if_attach(ifp);
    194 	ether_ifattach(ifp, myaddr);
    195 
    196 	/* Print additional info when attached. */
    197 	printf(": address %s\n", ether_sprintf(myaddr));
    198 
    199 	sc->sc_isr.isr_intr = esintr;
    200 	sc->sc_isr.isr_arg = sc;
    201 	sc->sc_isr.isr_ipl = 2;
    202 	add_isr(&sc->sc_isr);
    203 }
    204 
    205 #ifdef ESDEBUG
    206 void
    207 es_dump_smcregs(char *where, union smcregs *smc)
    208 {
    209 	u_short cur_bank = smc->b0.bsr & BSR_MASK;
    210 
    211 	printf("SMC registers %p from %s bank %04x\n", smc, where,
    212 	    smc->b0.bsr);
    213 	smc->b0.bsr = BSR_BANK0;
    214 	printf("TCR %04x EPHSR %04x RCR %04x ECR %04x MIR %04x MCR %04x\n",
    215 	    SWAP(smc->b0.tcr), SWAP(smc->b0.ephsr), SWAP(smc->b0.rcr),
    216 	    SWAP(smc->b0.ecr), SWAP(smc->b0.mir), SWAP(smc->b0.mcr));
    217 	smc->b1.bsr = BSR_BANK1;
    218 	printf("CR %04x BAR %04x IAR %04x %04x %04x GPR %04x CTR %04x\n",
    219 	    SWAP(smc->b1.cr), SWAP(smc->b1.bar), smc->b1.iar[0], smc->b1.iar[1],
    220 	    smc->b1.iar[2], smc->b1.gpr, SWAP(smc->b1.ctr));
    221 	smc->b2.bsr = BSR_BANK2;
    222 	printf("MMUCR %04x PNR %02x ARR %02x FIFO %04x PTR %04x",
    223 	    SWAP(smc->b2.mmucr), smc->b2.pnr, smc->b2.arr, smc->b2.fifo,
    224 	    SWAP(smc->b2.ptr));
    225 	printf(" DATA %04x %04x IST %02x MSK %02x\n", smc->b2.data,
    226 	    smc->b2.datax, smc->b2.ist, smc->b2.msk);
    227 	smc->b3.bsr = BSR_BANK3;
    228 	printf("MT %04x %04x %04x %04x\n",
    229 	    smc->b3.mt[0], smc->b3.mt[1], smc->b3.mt[2], smc->b3.mt[3]);
    230 	smc->b3.bsr = cur_bank;
    231 }
    232 #endif
    233 
    234 void
    235 esstop(struct es_softc *sc)
    236 {
    237 	union smcregs *smc = sc->sc_base;
    238 
    239 	/*
    240 	 * Clear interrupt mask; disable all interrupts.
    241 	 */
    242 	smc->b2.bsr = BSR_BANK2;
    243 	smc->b2.msk = 0;
    244 
    245 	/*
    246 	 * Disable transmitter and receiver.
    247 	 */
    248 	smc->b0.bsr = BSR_BANK0;
    249 	smc->b0.rcr = 0;
    250 	smc->b0.tcr = 0;
    251 
    252 	/*
    253 	 * Cancel watchdog timer.
    254 	 */
    255 	sc->sc_ethercom.ec_if.if_timer = 0;
    256 }
    257 
    258 void
    259 esinit(struct es_softc *sc)
    260 {
    261 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    262 	union smcregs *smc = sc->sc_base;
    263 	int s;
    264 
    265 	s = splnet();
    266 
    267 #ifdef ESDEBUG
    268 	if (ifp->if_flags & IFF_RUNNING)
    269 		es_dump_smcregs("esinit", smc);
    270 #endif
    271 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    272 	smc->b0.rcr = RCR_EPH_RST;
    273 	smc->b0.rcr = 0;
    274 	smc->b3.bsr = BSR_BANK3;	/* Select bank 3 */
    275 	smc->b3.mt[0] = 0;		/* clear Multicast table */
    276 	smc->b3.mt[1] = 0;
    277 	smc->b3.mt[2] = 0;
    278 	smc->b3.mt[3] = 0;
    279 	/* XXX set Multicast table from Multicast list */
    280 	smc->b1.bsr = BSR_BANK1;	/* Select bank 1 */
    281 	smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
    282 	smc->b1.ctr = CTR_AUTO_RLSE;
    283 	smc->b1.iar[0] = *((unsigned short *) &LLADDR(ifp->if_sadl)[0]);
    284 	smc->b1.iar[1] = *((unsigned short *) &LLADDR(ifp->if_sadl)[2]);
    285 	smc->b1.iar[2] = *((unsigned short *) &LLADDR(ifp->if_sadl)[4]);
    286 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    287 	smc->b2.mmucr = MMUCR_RESET;
    288 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
    289 	smc->b0.mcr = SWAP(0x0020);	/* reserve 8K for transmit buffers */
    290 	smc->b0.tcr = TCR_PAD_EN | (TCR_TXENA + TCR_MON_CSN);
    291 	smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN | RCR_ALLMUL;
    292 	/* XXX add promiscuous flags */
    293 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
    294 	smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
    295 
    296 	/* Interface is now 'running', with no output active. */
    297 	ifp->if_flags |= IFF_RUNNING;
    298 	ifp->if_flags &= ~IFF_OACTIVE;
    299 
    300 	/* Attempt to start output, if any. */
    301 	esstart(ifp);
    302 
    303 	splx(s);
    304 }
    305 
    306 int
    307 esintr(void *arg)
    308 {
    309 	struct es_softc *sc = arg;
    310 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    311 	u_short intsts, intact;
    312 	union smcregs *smc;
    313 	int s = splnet();
    314 
    315 	smc = sc->sc_base;
    316 #ifdef ESDEBUG
    317 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2 &&
    318 	    ifp->if_flags & IFF_RUNNING) {
    319 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    320 		    smc->b2.bsr);
    321 		smc->b2.bsr = BSR_BANK2;
    322 	}
    323 #endif
    324 	intsts = smc->b2.ist;
    325 	intact = smc->b2.msk & intsts;
    326 	if ((intact) == 0) {
    327 		splx(s);
    328 		return (0);
    329 	}
    330 #ifdef ESDEBUG
    331 	if (esdebug)
    332 		printf ("%s: esintr ist %02x msk %02x",
    333 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
    334 	if (sc->sc_intbusy++) {
    335 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
    336 		panic("esintr re-entered");
    337 	}
    338 	if (sc->sc_smcbusy)
    339 		printf("%s: esintr interrupted busy %d\n", sc->sc_dev.dv_xname,
    340 		    sc->sc_smcbusy);
    341 #endif
    342 	smc->b2.msk = 0;
    343 #ifdef ESDEBUG
    344 	if (esdebug)
    345 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    346 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    347 		    smc->b2.fifo);
    348 #endif
    349 	if (intact & IST_ALLOC) {
    350 		sc->sc_intctl &= ~MSK_ALLOC;
    351 #ifdef ESDEBUG
    352 		if (esdebug || 1)
    353 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
    354 			    intsts);
    355 #endif
    356 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    357 			u_char save_pnr;
    358 #ifdef ESDEBUG
    359 			if (esdebug || 1)
    360 				printf (" arr %02x\n", smc->b2.arr);
    361 #endif
    362 			save_pnr = smc->b2.pnr;
    363 			smc->b2.pnr = smc->b2.arr;
    364 			smc->b2.mmucr = MMUCR_RLSPKT;
    365 			while (smc->b2.mmucr & MMUCR_BUSY)
    366 				;
    367 			smc->b2.pnr = save_pnr;
    368 			ifp->if_flags &= ~IFF_OACTIVE;
    369 			ifp->if_timer = 0;
    370 		}
    371 #ifdef ESDEBUG
    372 		else if (esdebug || 1)
    373 			printf (" IST_ALLOC with ARR_FAILED?\n");
    374 #endif
    375 	}
    376 #ifdef ESDEBUG
    377 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    378 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    379 		    smc->b2.bsr);
    380 		smc->b2.bsr = BSR_BANK2;
    381 	}
    382 #endif
    383 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    384 		esrint(sc);
    385 	}
    386 #ifdef ESDEBUG
    387 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    388 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    389 		    smc->b2.bsr);
    390 		smc->b2.bsr = BSR_BANK2;
    391 	}
    392 #endif
    393 	if (intact & IST_RX_OVRN) {
    394 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    395 		    intsts);
    396 		smc->b2.ist = ACK_RX_OVRN;
    397 		printf ("->%02x\n", smc->b2.ist);
    398 		ifp->if_ierrors++;
    399 	}
    400 	if (intact & IST_TX_EMPTY) {
    401 		u_short ecr;
    402 #ifdef ESDEBUG
    403 		if (esdebug)
    404 			printf ("%s: TX EMPTY %02x",
    405 			    sc->sc_dev.dv_xname, intsts);
    406 		++estxint5;		/* count # IST_TX_EMPTY ints */
    407 #endif
    408 		smc->b2.ist = ACK_TX_EMPTY;
    409 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    410 		ifp->if_timer = 0;
    411 #ifdef ESDEBUG
    412 		if (esdebug)
    413 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    414 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    415 			    smc->b2.arr);
    416 #endif
    417 		if (smc->b2.ist & IST_TX) {
    418 			intact |= IST_TX;
    419 #ifdef ESDEBUG
    420 			++estxint2;		/* count # TX after TX_EMPTY */
    421 #endif
    422 		} else {
    423 			smc->b0.bsr = BSR_BANK0;
    424 			ecr = smc->b0.ecr;	/* Get error counters */
    425 			if (ecr & 0xff00)
    426 				ifp->if_collisions += ((ecr >> 8) & 15) +
    427 				    ((ecr >> 11) & 0x1e);
    428 			smc->b2.bsr = BSR_BANK2;
    429 #if 0
    430 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
    431 #endif
    432 		}
    433 	}
    434 	if (intact & IST_TX) {
    435 		u_char tx_pnr, save_pnr;
    436 		u_short save_ptr, ephsr, tcr;
    437 		int n = 0;
    438 #ifdef ESDEBUG
    439 		if (esdebug) {
    440 			printf ("%s: TX INT ist %02x",
    441 			    sc->sc_dev.dv_xname, intsts);
    442 			printf ("->%02x\n", smc->b2.ist);
    443 		}
    444 		++estxint3;			/* count # IST_TX */
    445 #endif
    446 zzzz:
    447 #ifdef ESDEBUG
    448 		++estxint4;			/* count # ~TEMPTY */
    449 #endif
    450 		smc->b0.bsr = BSR_BANK0;
    451 		ephsr = smc->b0.ephsr;		/* get 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 		    sc->sc_dev.dv_xname, 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 sucessful.  Since
    484 			 * AutoRelease is being used, a sucessful 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 			    sc->sc_dev.dv_xname, 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 	/* 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", sc->sc_dev.dv_xname,
    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", sc->sc_dev.dv_xname);
    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", sc->sc_dev.dv_xname,
    561 		    smc->b2.fifo);
    562 	if (sc->sc_smcbusy++) {
    563 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
    564 		panic("esrint re-entered");
    565 	}
    566 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    567 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    568 		    smc->b2.bsr);
    569 		smc->b2.bsr = BSR_BANK2;
    570 	}
    571 #endif
    572 	data = (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 		    sc->sc_dev.dv_xname, 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 			    sc->sc_dev.dv_xname);
    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", sc->sc_dev.dv_xname, 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", sc->sc_dev.dv_xname, 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 	ifp->if_ipackets++;
    664 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    665 	if (m == NULL)
    666 		return;
    667 	m->m_pkthdr.rcvif = ifp;
    668 	m->m_pkthdr.len = pktlen;
    669 	len = MHLEN;
    670 	top = NULL;
    671 	mp = &top;
    672 #ifdef USEPKTBUF
    673 	b = pktbuf;
    674 #endif
    675 	while (pktlen > 0) {
    676 		if (top) {
    677 			MGET(m, M_DONTWAIT, MT_DATA);
    678 			if (m == 0) {
    679 				m_freem(top);
    680 				return;
    681 			}
    682 			len = MLEN;
    683 		}
    684 		if (pktlen >= MINCLSIZE) {
    685 			MCLGET(m, M_DONTWAIT);
    686 			if (m->m_flags & M_EXT)
    687 				len = MCLBYTES;
    688 		}
    689 		m->m_len = len = min(pktlen, len);
    690 #ifdef USEPKTBUF
    691 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    692 		b += len;
    693 #else	/* USEPKTBUF */
    694 		buf = mtod(m, u_short *);
    695 		cnt = len / 2;
    696 		while (cnt--)
    697 			*buf++ = *data;
    698 		if (len & 1)
    699 			*buf = *data;	/* XXX should be byte store */
    700 #ifdef ESDEBUG
    701 		if (esdebug) {
    702 			buf = mtod(m, u_short *);
    703 			for (i = 0; i < len; ++i)
    704 				printf ("%02x%s", ((u_char *)buf)[i],
    705 				    ((i & 31) == 31) ? "\n" : "");
    706 			if (i & 31)
    707 				printf ("\n");
    708 		}
    709 #endif
    710 #endif	/* USEPKTBUF */
    711 		pktlen -= len;
    712 		*mp = m;
    713 		mp = &m->m_next;
    714 	}
    715 #ifndef USEPKTBUF
    716 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    717 	while (smc->b2.mmucr & MMUCR_BUSY)
    718 		;
    719 #endif
    720 #if NBPFILTER > 0
    721 	/*
    722 	 * Check if there's a BPF listener on this interface.  If so, hand off
    723 	 * the raw packet to bpf.
    724 	 */
    725 	if (ifp->if_bpf)
    726 		bpf_mtap(ifp->if_bpf, top);
    727 #endif
    728 	(*ifp->if_input)(ifp, top);
    729 #ifdef ESDEBUG
    730 	if (--sc->sc_smcbusy) {
    731 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    732 		panic("esintr busy on exit");
    733 	}
    734 #endif
    735 }
    736 
    737 void
    738 estint(struct es_softc *sc)
    739 {
    740 
    741 	esstart(&sc->sc_ethercom.ec_if);
    742 }
    743 
    744 void
    745 esstart(struct ifnet *ifp)
    746 {
    747 	struct es_softc *sc = ifp->if_softc;
    748 	union smcregs *smc = sc->sc_base;
    749 	struct mbuf *m0, *m;
    750 #ifdef USEPKTBUF
    751 	u_short pktbuf[ETHERMTU + 2];
    752 #else
    753 	u_short oddbyte, needbyte;
    754 #endif
    755 	u_short pktctlw, pktlen;
    756 	u_short *buf;
    757 	volatile u_short *data;
    758 #if 0
    759 	u_long *lbuf;
    760 	volatile u_long *ldata;
    761 #endif
    762 	short cnt;
    763 	int i;
    764 	u_char active_pnr;
    765 
    766 	if ((sc->sc_ethercom.ec_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    767 	    IFF_RUNNING)
    768 		return;
    769 
    770 #ifdef ESDEBUG
    771 	if (sc->sc_smcbusy++) {
    772 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    773 		panic("esstart re-entred");
    774 	}
    775 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    776 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    777 		    smc->b2.bsr);
    778 		smc->b2.bsr = BSR_BANK2;
    779 	}
    780 #endif
    781 	for (;;) {
    782 #ifdef ESDEBUG
    783 		u_short start_ptr, end_ptr;
    784 #endif
    785 		/*
    786 		 * Sneak a peek at the next packet to get the length
    787 		 * and see if the SMC 91C90 can accept it.
    788 		 */
    789 		m = sc->sc_ethercom.ec_if.if_snd.ifq_head;
    790 		if (!m)
    791 			break;
    792 #ifdef ESDEBUG
    793 		if (esdebug && (m->m_next || m->m_len & 1))
    794 			printf("%s: esstart m_next %p m_len %d\n",
    795 			    sc->sc_dev.dv_xname, m->m_next, m->m_len);
    796 #endif
    797 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    798 			pktlen += m0->m_len;
    799 		pktctlw = 0;
    800 		pktlen += 4;
    801 		if (pktlen & 1)
    802 			++pktlen;	/* control byte after last byte */
    803 		else
    804 			pktlen += 2;	/* control byte after pad byte */
    805 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    806 		for (i = 0; i <= 5; ++i)
    807 			if ((smc->b2.arr & ARR_FAILED) == 0)
    808 				break;
    809 		if (smc->b2.arr & ARR_FAILED) {
    810 			sc->sc_ethercom.ec_if.if_flags |= IFF_OACTIVE;
    811 			sc->sc_intctl |= MSK_ALLOC;
    812 			sc->sc_ethercom.ec_if.if_timer = 5;
    813 			break;
    814 		}
    815 		active_pnr = smc->b2.pnr = smc->b2.arr;
    816 
    817 #ifdef ESDEBUG
    818 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    819 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    820 			    smc->b2.bsr);
    821 			smc->b2.bsr = BSR_BANK2;
    822 		}
    823 #endif
    824 		IF_DEQUEUE(&sc->sc_ethercom.ec_if.if_snd, m);
    825 		smc->b2.ptr = PTR_AUTOINCR;
    826 		(void) smc->b2.mmucr;
    827 		data = (u_short *)&smc->b2.data;
    828 		*data = SWAP(pktctlw);
    829 		*data = SWAP(pktlen);
    830 #ifdef ESDEBUG
    831 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    832 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    833 			    smc->b2.bsr);
    834 			smc->b2.bsr = BSR_BANK2;
    835 		}
    836 #endif
    837 #ifdef USEPKTBUF
    838 		i = 0;
    839 		for (m0 = m; m; m = m->m_next) {
    840 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    841 			i += m->m_len;
    842 		}
    843 
    844 		if (i & 1)	/* Figure out where to put control byte */
    845 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    846 		else
    847 			pktbuf[i/2] = 0;
    848 		pktlen -= 4;
    849 #ifdef ESDEBUG
    850 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
    851 			printf("%s: esstart packet longer than pktbuf\n",
    852 			    sc->sc_dev.dv_xname);
    853 #endif
    854 #if 0 /* doesn't quite work? */
    855 		lbuf = (u_long *)(pktbuf);
    856 		ldata = (u_long *)data;
    857 		cnt = pktlen / 4;
    858 		while(cnt--)
    859 			*ldata = *lbuf++;
    860 		if (pktlen & 2) {
    861 			buf = (u_short *)lbuf;
    862 			*data = *buf;
    863 		}
    864 #else
    865 #ifdef ESDEBUG
    866 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    867 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    868 			    smc->b2.bsr);
    869 			smc->b2.bsr = BSR_BANK2;
    870 		}
    871 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
    872 #endif
    873 		buf = pktbuf;
    874 		cnt = pktlen / 2;
    875 		while (cnt--)
    876 			*data = *buf++;
    877 #ifdef ESDEBUG
    878 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
    879 #endif
    880 #endif
    881 #else	/* USEPKTBUF */
    882 		pktctlw = 0;
    883 		oddbyte = needbyte = 0;
    884 		for (m0 = m; m; m = m->m_next) {
    885 			buf = mtod(m, u_short *);
    886 			cnt = m->m_len / 2;
    887 			if (needbyte) {
    888 				oddbyte |= *buf >> 8;
    889 				*data = oddbyte;
    890 			}
    891 			while (cnt--)
    892 				*data = *buf++;
    893 			if (m->m_len & 1)
    894 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    895 			if (m->m_len & 1 && m->m_next)
    896 				printf("%s: esstart odd byte count in mbuf\n",
    897 				    sc->sc_dev.dv_xname);
    898 		}
    899 		*data = pktctlw;
    900 #endif	/* USEPKTBUF */
    901 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    902 			/*
    903 			 * The bank select register has changed.  This seems
    904 			 * to happen with my A2000/Zeus once in a while.  It
    905 			 * appears that the Ethernet chip resets while
    906 			 * copying the transmit buffer.  Requeue the current
    907 			 * transmit buffer and reinitialize the interface.
    908 			 * The initialize routine will take care of
    909 			 * retransmitting the buffer.  mhitch
    910 			 */
    911 #ifdef DIAGNOSTIC
    912 			printf("%s: esstart+++ BSR not 2: %04x\n",
    913 			    sc->sc_dev.dv_xname, smc->b2.bsr);
    914 #endif
    915 			smc->b2.bsr = BSR_BANK2;
    916 #ifdef ESDEBUG
    917 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
    918 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
    919 			--sc->sc_smcbusy;
    920 #endif
    921 			IF_PREPEND(&sc->sc_ethercom.ec_if.if_snd, m0);
    922 			esinit(sc);	/* It's really hosed - reset */
    923 			return;
    924 		}
    925 		smc->b2.mmucr = MMUCR_ENQ_TX;
    926 		if (smc->b2.pnr != active_pnr)
    927 			printf("%s: esstart - PNR changed %x->%x\n",
    928 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    929 #if NBPFILTER > 0
    930 		if (sc->sc_ethercom.ec_if.if_bpf)
    931 			bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m0);
    932 #endif
    933 		m_freem(m0);
    934 		sc->sc_ethercom.ec_if.if_opackets++;	/* move to interrupt? */
    935 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    936 		sc->sc_ethercom.ec_if.if_timer = 5;
    937 	}
    938 	smc->b2.msk = sc->sc_intctl;
    939 #ifdef ESDEBUG
    940 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
    941 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    942 		    smc->b2.bsr);
    943 		smc->b2.bsr = BSR_BANK2;
    944 	}
    945 	if (--sc->sc_smcbusy) {
    946 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    947 		panic("esstart busy on exit");
    948 	}
    949 #endif
    950 }
    951 
    952 int
    953 esioctl(register struct ifnet *ifp, u_long command, caddr_t data)
    954 {
    955 	struct es_softc *sc = ifp->if_softc;
    956 	register struct ifaddr *ifa = (struct ifaddr *)data;
    957 	struct ifreq *ifr = (struct ifreq *)data;
    958 	int s, error = 0;
    959 
    960 	s = splnet();
    961 
    962 	switch (command) {
    963 
    964 	case SIOCSIFADDR:
    965 		ifp->if_flags |= IFF_UP;
    966 
    967 		switch (ifa->ifa_addr->sa_family) {
    968 #ifdef INET
    969 		case AF_INET:
    970 			esinit(sc);
    971 			arp_ifinit(ifp, ifa);
    972 			break;
    973 #endif
    974 #ifdef NS
    975 		case AF_NS:
    976 		    {
    977 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    978 
    979 			if (ns_nullhost(*ina))
    980 				ina->x_host =
    981 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    982 			else
    983 				bcopy(ina->x_host.c_host,
    984 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    985 			/* Set new address. */
    986 			esinit(sc);
    987 			break;
    988 		    }
    989 #endif
    990 		default:
    991 			esinit(sc);
    992 			break;
    993 		}
    994 		break;
    995 
    996 	case SIOCSIFFLAGS:
    997 		/*
    998 		 * If interface is marked down and it is running, then stop it
    999 		 */
   1000 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1001 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1002 			/*
   1003 			 * If interface is marked down and it is running, then
   1004 			 * stop it.
   1005 			 */
   1006 			esstop(sc);
   1007 			ifp->if_flags &= ~IFF_RUNNING;
   1008 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1009 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1010 			/*
   1011 			 * If interface is marked up and it is stopped, then
   1012 			 * start it.
   1013 			 */
   1014 			esinit(sc);
   1015 		} else {
   1016 			/*
   1017 			 * Reset the interface to pick up changes in any other
   1018 			 * flags that affect hardware registers.
   1019 			 */
   1020 			esstop(sc);
   1021 			esinit(sc);
   1022 		}
   1023 #ifdef ESDEBUG
   1024 		if (ifp->if_flags & IFF_DEBUG)
   1025 			esdebug = sc->sc_debug = 1;
   1026 		else
   1027 			esdebug = sc->sc_debug = 0;
   1028 #endif
   1029 		break;
   1030 
   1031 	case SIOCADDMULTI:
   1032 	case SIOCDELMULTI:
   1033 		error = (command == SIOCADDMULTI) ?
   1034 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1035 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1036 
   1037 		if (error == ENETRESET) {
   1038 			/*
   1039 			 * Multicast list has changed; set the hardware filter
   1040 			 * accordingly.
   1041 			 */
   1042 			/* XXX */
   1043 			error = 0;
   1044 		}
   1045 		break;
   1046 
   1047 	default:
   1048 		error = EINVAL;
   1049 	}
   1050 
   1051 	splx(s);
   1052 	return (error);
   1053 }
   1054 
   1055 /*
   1056  * Reset the interface.
   1057  */
   1058 void
   1059 esreset(struct es_softc *sc)
   1060 {
   1061 	int s;
   1062 
   1063 	s = splnet();
   1064 	esstop(sc);
   1065 	esinit(sc);
   1066 	splx(s);
   1067 }
   1068 
   1069 void
   1070 eswatchdog(struct ifnet *ifp)
   1071 {
   1072 	struct es_softc *sc = ifp->if_softc;
   1073 
   1074 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1075 	++ifp->if_oerrors;
   1076 
   1077 	esreset(sc);
   1078 }
   1079