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