Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.8
      1 /*	$NetBSD: if_es.c,v 1.8 1995/12/24 02:29:59 mycroft 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 
     37 #include "bpfilter.h"
     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/netisr.h>
     52 #include <net/route.h>
     53 
     54 #ifdef INET
     55 #include <netinet/in.h>
     56 #include <netinet/in_systm.h>
     57 #include <netinet/in_var.h>
     58 #include <netinet/ip.h>
     59 #include <netinet/if_ether.h>
     60 #endif
     61 
     62 #ifdef NS
     63 #include <netns/ns.h>
     64 #include <netns/ns_if.h>
     65 #endif
     66 
     67 #include <machine/cpu.h>
     68 #include <machine/mtpr.h>
     69 #include <amiga/amiga/device.h>
     70 #include <amiga/amiga/isr.h>
     71 #include <amiga/dev/zbusvar.h>
     72 #include <amiga/dev/if_esreg.h>
     73 
     74 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     75 
     76 #define ESDEBUG
     77 #define	USEPKTBUF
     78 
     79 /*
     80  * Ethernet software status per interface.
     81  *
     82  * Each interface is referenced by a network interface structure,
     83  * es_if, which the routing code uses to locate the interface.
     84  * This structure contains the output queue for the interface, its address, ...
     85  */
     86 struct	es_softc {
     87 	struct	device sc_dev;
     88 	struct	isr sc_isr;
     89 	struct	arpcom sc_arpcom;	/* common Ethernet structures */
     90 	void	*sc_base;	/* base address of board */
     91 	short	sc_iflags;
     92 	unsigned short sc_intctl;
     93 #ifdef ESDEBUG
     94 	int	sc_debug;
     95 	short	sc_intbusy;
     96 	short	sc_smcbusy;
     97 #endif
     98 };
     99 
    100 #if NBPFILTER > 0
    101 #include <net/bpf.h>
    102 #include <net/bpfdesc.h>
    103 #endif
    104 
    105 #ifdef ESDEBUG
    106 /* console error messages */
    107 int	esdebug = 0;
    108 int	estxints = 0;	/* IST_TX with TX enabled */
    109 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
    110 int	estxint3 = 0;	/* IST_TX interrupt processed */
    111 int	estxint4 = 0;	/* ~TEMPTY counts */
    112 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
    113 #endif
    114 
    115 int esintr __P((struct es_softc *));
    116 void esstart __P((struct ifnet *));
    117 void eswatchdog __P((int));
    118 int esioctl __P((struct ifnet *, u_long, caddr_t));
    119 void esrint __P((struct es_softc *));
    120 void estint __P((struct es_softc *));
    121 void esinit __P((struct es_softc *));
    122 void esreset __P((struct es_softc *));
    123 
    124 int esmatch __P((struct device *, void *, void *));
    125 void esattach __P((struct device *, struct device *, void *));
    126 
    127 struct cfdriver escd = {
    128 	NULL, "es", esmatch, esattach, DV_IFNET, sizeof(struct es_softc)
    129 };
    130 
    131 int
    132 esmatch(parent, match, aux)
    133 	struct device *parent;
    134 	void *match, *aux;
    135 {
    136 	struct zbus_args *zap = aux;
    137 
    138 	/* Ameristar A4066 ethernet card */
    139 	if (zap->manid == 1053 && zap->prodid == 10)
    140 		return(1);
    141 
    142 	return (0);
    143 }
    144 
    145 /*
    146  * Interface exists: make available by filling in network interface
    147  * record.  System will initialize the interface when it is ready
    148  * to accept packets.
    149  */
    150 void
    151 esattach(parent, self, aux)
    152 	struct device *parent, *self;
    153 	void *aux;
    154 {
    155 	struct es_softc *sc = (void *)self;
    156 	struct zbus_args *zap = aux;
    157 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    158 	unsigned long ser;
    159 
    160 	sc->sc_base = zap->va;
    161 #ifdef ESDEBUG
    162 	/* MLHDEBUG
    163 	 * MLHDEBUG remove first 4 and last 3 pages of the A4066 memory
    164 	 * MLHDEBUG and use the 5th page to access the SMC
    165 	 */
    166 	sc->sc_base = zap->va + 0x8000;		/* MLHDEBUG */
    167 	physunaccess(zap->va, 0x8000);		/* MLHDEBUG */
    168 	physunaccess(zap->va + 0xa000, 0x6000);	/* MLHDEBUG */
    169 #endif
    170 
    171 	/*
    172 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    173 	 * (Currently only Ameristar.)
    174 	 */
    175 	sc->sc_arpcom.ac_enaddr[0] = 0x00;
    176 	sc->sc_arpcom.ac_enaddr[1] = 0x00;
    177 	sc->sc_arpcom.ac_enaddr[2] = 0x9f;
    178 
    179 	/*
    180 	 * Serial number for board contains last 3 bytes.
    181 	 */
    182 	ser = (unsigned long) zap->serno;
    183 
    184 	sc->sc_arpcom.ac_enaddr[3] = (ser >> 16) & 0xff;
    185 	sc->sc_arpcom.ac_enaddr[4] = (ser >>  8) & 0xff;
    186 	sc->sc_arpcom.ac_enaddr[5] = (ser      ) & 0xff;
    187 
    188 	/* Initialize ifnet structure. */
    189 	ifp->if_unit = sc->sc_dev.dv_unit;
    190 	ifp->if_name = escd.cd_name;
    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);
    201 
    202 	/* Print additional info when attached. */
    203 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    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 & 0x0300;
    222 
    223 	printf("SMC registers %08x 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_arpcom.ac_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 *) &sc->sc_arpcom.ac_enaddr[0]);
    279 	smc->b1.iar[1] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[2]);
    280 	smc->b1.iar[2] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[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(sc)
    303 	struct es_softc *sc;
    304 {
    305 	int i;
    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 & 0x0300) != BSR_BANK2 &&
    313 	    sc->sc_arpcom.ac_if.if_flags & IFF_RUNNING) {
    314 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    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 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
    329 	if (sc->sc_intbusy++) {
    330 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
    331 		panic("esintr re-entered");
    332 	}
    333 #endif
    334 	smc->b2.msk = 0;
    335 #ifdef ESDEBUG
    336 	if (esdebug)
    337 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    338 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    339 		    smc->b2.fifo);
    340 #endif
    341 	if (intact & IST_ALLOC) {
    342 		sc->sc_intctl &= ~MSK_ALLOC;
    343 #ifdef ESDEBUG
    344 		if (esdebug || 1)
    345 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
    346 			    intsts);
    347 #endif
    348 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    349 			u_char save_pnr;
    350 #ifdef ESDEBUG
    351 			if (esdebug || 1)
    352 				printf (" arr %02x\n", smc->b2.arr);
    353 #endif
    354 			save_pnr = smc->b2.pnr;
    355 			smc->b2.pnr = smc->b2.arr;
    356 			smc->b2.mmucr = MMUCR_RLSPKT;
    357 			while (smc->b2.mmucr & MMUCR_BUSY)
    358 				;
    359 			smc->b2.pnr = save_pnr;
    360 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    361 		}
    362 #ifdef ESDEBUG
    363 		else if (esdebug || 1)
    364 			printf (" IST_ALLOC with ARR_FAILED?\n");
    365 #endif
    366 	}
    367 #ifdef ESDEBUG
    368 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    369 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    370 		    smc->b2.bsr);
    371 		smc->b2.bsr = BSR_BANK2;
    372 	}
    373 #endif
    374 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    375 		esrint(sc);
    376 	}
    377 #ifdef ESDEBUG
    378 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    379 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    380 		    smc->b2.bsr);
    381 		smc->b2.bsr = BSR_BANK2;
    382 	}
    383 #endif
    384 	if (intact & IST_RX_OVRN) {
    385 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    386 		    intsts);
    387 		smc->b2.ist = ACK_RX_OVRN;
    388 		printf ("->%02x\n", smc->b2.ist);
    389 		sc->sc_arpcom.ac_if.if_ierrors++;
    390 	}
    391 	if (intact & IST_TX_EMPTY) {
    392 		u_short ecr;
    393 #ifdef ESDEBUG
    394 		if (esdebug)
    395 			printf ("%s: TX EMPTY %02x",
    396 			    sc->sc_dev.dv_xname, intsts);
    397 		++estxint5;		/* count # IST_TX_EMPTY ints */
    398 #endif
    399 		smc->b2.ist = ACK_TX_EMPTY;
    400 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    401 #ifdef ESDEBUG
    402 		if (esdebug)
    403 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    404 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    405 			    smc->b2.arr);
    406 #endif
    407 		if (smc->b2.ist & IST_TX) {
    408 			intact |= IST_TX;
    409 #ifdef ESDEBUG
    410 			++estxint2;		/* count # TX after TX_EMPTY */
    411 #endif
    412 		} else {
    413 			smc->b0.bsr = BSR_BANK0;
    414 			ecr = smc->b0.ecr;	/* Get error counters */
    415 			if (ecr & 0xff00)
    416 				sc->sc_arpcom.ac_if.if_collisions += ((ecr >> 8) & 15) +
    417 				    ((ecr >> 11) & 0x1e);
    418 			smc->b2.bsr = BSR_BANK2;
    419 #if 0
    420 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
    421 #endif
    422 		}
    423 	}
    424 	if (intact & IST_TX) {
    425 		u_char tx_pnr, save_pnr;
    426 		u_short save_ptr, ephsr, tcr;
    427 		int n = 0;
    428 #ifdef ESDEBUG
    429 		if (esdebug) {
    430 			printf ("%s: TX INT ist %02x",
    431 			    sc->sc_dev.dv_xname, intsts);
    432 			printf ("->%02x\n", smc->b2.ist);
    433 		}
    434 		++estxint3;			/* count # IST_TX */
    435 #endif
    436 zzzz:
    437 		++estxint4;			/* count # ~TEMPTY */
    438 		smc->b0.bsr = BSR_BANK0;
    439 		ephsr = smc->b0.ephsr;		/* get EPHSR */
    440 		tcr = smc->b0.tcr;		/* and TCR */
    441 		smc->b2.bsr = BSR_BANK2;
    442 		save_ptr = smc->b2.ptr;
    443 		save_pnr = smc->b2.pnr;
    444 		tx_pnr = smc->b2.fifo >> 8;	/* pktno from completion fifo */
    445 		smc->b2.pnr = tx_pnr;		/* set TX packet number */
    446 		smc->b2.ptr = PTR_READ;		/* point to status word */
    447 #if 0 /* XXXX */
    448 		printf("%s: esintr TXINT IST %02x PNR %02x(%d)",
    449 		    sc->sc_dev.dv_xname, smc->b2.ist,
    450 		    tx_pnr, n);
    451 		printf(" Status %04x", smc->b2.data);
    452 		printf(" EPHSR %04x\n", ephsr);
    453 #endif
    454 		if ((smc->b2.data & EPHSR_TX_SUC) == 0 && (tcr & TCR_TXENA) == 0) {
    455 			/*
    456 			 * Transmitter was stopped for some error.  Enqueue
    457 			 * the packet again and restart the transmitter.
    458 			 * May need some check to limit the number of retries.
    459 			 */
    460 			smc->b2.mmucr = MMUCR_ENQ_TX;
    461 			smc->b0.bsr = BSR_BANK0;
    462 			smc->b0.tcr |= TCR_TXENA;
    463 			smc->b2.bsr = BSR_BANK2;
    464 			sc->sc_arpcom.ac_if.if_oerrors++;
    465 			sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    466 		} else {
    467 			/*
    468 			 * This shouldn't have happened:  IST_TX indicates
    469 			 * the TX completion FIFO is not empty, but the
    470 			 * status for the packet on the completion FIFO
    471 			 * shows that the transmit was sucessful.  Since
    472 			 * AutoRelease is being used, a sucessful transmit
    473 			 * should not result in a packet on the completion
    474 			 * FIFO.  Also, that packet doesn't seem to want
    475 			 * to be acknowledged.  If this occurs, just reset
    476 			 * the TX FIFOs.
    477 			 */
    478 #if 1
    479 			if (smc->b2.ist & IST_TX_EMPTY) {
    480 				smc->b2.mmucr = MMUCR_RESET_TX;
    481 				sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    482 			}
    483 #endif
    484 #ifdef ESDEBUG
    485 			++estxints;	/* count IST_TX with TX enabled */
    486 #endif
    487 		}
    488 		smc->b2.pnr = save_pnr;
    489 		smc->b2.ptr = save_ptr;
    490 		smc->b2.ist = ACK_TX;
    491 
    492 		if ((smc->b2.fifo & FIFO_TEMPTY) == 0 && n++ < 32) {
    493 #if 0 /* XXXX */
    494 			printf("%s: multiple TX int(%2d) pnr %02x ist %02x fifo %04x",
    495 			    sc->sc_dev.dv_xname, n, tx_pnr, smc->b2.ist, smc->b2.fifo);
    496 			smc->w2.istmsk = ACK_TX << 8;
    497 			printf(" %04x\n", smc->b2.fifo);
    498 #endif
    499 			if (tx_pnr != (smc->b2.fifo >> 8))
    500 				goto zzzz;
    501 		}
    502 	}
    503 	/* output packets */
    504 	estint(sc);
    505 #ifdef ESDEBUG
    506 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    507 		printf("%s: intr+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    508 		    smc->b2.bsr);
    509 		smc->b2.bsr = BSR_BANK2;
    510 	}
    511 #endif
    512 	smc->b2.msk = sc->sc_intctl;
    513 #ifdef ESDEBUG
    514 	if (--sc->sc_intbusy) {
    515 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    516 		panic("esintr busy on exit");
    517 	}
    518 #endif
    519 	splx(s);
    520 	return (1);
    521 }
    522 
    523 void
    524 esrint(sc)
    525 	struct es_softc *sc;
    526 {
    527 	union smcregs *smc = sc->sc_base;
    528 	int i;
    529 	u_short len;
    530 	short cnt;
    531 	u_short pktctlw, pktlen, *buf;
    532 	u_long *lbuf;
    533 	volatile u_short *data;
    534 	volatile u_long *ldata;
    535 	struct ifnet *ifp;
    536 	struct mbuf *top, **mp, *m;
    537 	struct ether_header *eh;
    538 #ifdef USEPKTBUF
    539 	u_char *b, pktbuf[1530];
    540 #endif
    541 
    542 #ifdef ESDEBUG
    543 	if (esdebug)
    544 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    545 		    smc->b2.fifo);
    546 	if (sc->sc_smcbusy++) {
    547 		printf("%s: esrint re-entered\n");
    548 		panic("esrint re-entered");
    549 	}
    550 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    551 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    552 		    smc->b2.bsr);
    553 		smc->b2.bsr = BSR_BANK2;
    554 	}
    555 #endif
    556 	data = (u_short *)&smc->b2.data;
    557 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
    558 	(void) smc->b2.mmucr;
    559 #ifdef ESDEBUG
    560 	if (esdebug)
    561 		printf ("->%04x", smc->b2.fifo);
    562 #endif
    563 	len = *data;
    564 	len = SWAP(len);	/* Careful of macro side-effects */
    565 #ifdef ESDEBUG
    566 	if (esdebug)
    567 		printf (" length %d", len);
    568 #endif
    569 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR + PTR_READ | SWAP(0x0000);
    570 	(void) smc->b2.mmucr;
    571 	pktctlw = *data;
    572 	pktlen = *data;
    573 	pktctlw = SWAP(pktctlw);
    574 	pktlen = SWAP(pktlen) - 6;
    575 	if (pktctlw & RFSW_ODDFRM)
    576 		pktlen++;
    577 	if (len > 1530) {
    578 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
    579 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
    580 		/* XXX ignore packet, or just truncate? */
    581 #if defined(ESDEBUG) && defined(DDB)
    582 		if ((smc->b2.bsr & 0x0300) != BSR_BANK2)
    583 			Debugger();
    584 #endif
    585 		smc->b2.bsr = BSR_BANK2;
    586 		smc->b2.mmucr = MMUCR_REMRLS_RX;
    587 		while (smc->b2.mmucr & MMUCR_BUSY)
    588 			;
    589 		++sc->sc_arpcom.ac_if.if_ierrors;
    590 #ifdef ESDEBUG
    591 		if (--sc->sc_smcbusy) {
    592 			printf("%s: esrintr busy on bad packet exit\n",
    593 			    sc->sc_dev.dv_xname);
    594 			panic("esrintr busy on exit");
    595 		}
    596 #endif
    597 		return;
    598 	}
    599 #ifdef USEPKTBUF
    600 #if 0
    601 	lbuf = (u_long *) pktbuf;
    602 	ldata = (u_long *)data;
    603 	cnt = (len - 4) / 4;
    604 	while (cnt--)
    605 		*lbuf++ = *ldata;
    606 	if ((len - 4) & 2) {
    607 		buf = (u_short *) lbuf;
    608 		*buf = *data;
    609 	}
    610 #else
    611 	buf = (u_short *)pktbuf;
    612 	cnt = (len - 4) / 2;
    613 	while (cnt--)
    614 		*buf++ = *data;
    615 #endif
    616 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    617 	while (smc->b2.mmucr & MMUCR_BUSY)
    618 		;
    619 #ifdef ESDEBUG
    620 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    621 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    622 		/* count input error? */
    623 	}
    624 	if (esdebug) {
    625 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    626 		    smc->b2.fifo);
    627 		for (i = 0; i < pktlen; ++i)
    628 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    629 			    "");
    630 		if (i & 31)
    631 			printf ("\n");
    632 	}
    633 #endif
    634 #else	/* USEPKTBUF */
    635 	/* XXX copy directly from controller to mbuf */
    636 #ifdef ESDEBUG
    637 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    638 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    639 		/* count input error? */
    640 	}
    641 	if (esdebug) {
    642 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    643 		    smc->b2.fifo);
    644 	}
    645 #endif
    646 #endif /* USEPKTBUF */
    647 	ifp = &sc->sc_arpcom.ac_if;
    648 	ifp->if_ipackets++;
    649 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    650 	if (m == NULL)
    651 		return;
    652 	m->m_pkthdr.rcvif = ifp;
    653 	m->m_pkthdr.len = pktlen;
    654 	len = MHLEN;
    655 	top = NULL;
    656 	mp = &top;
    657 #ifdef USEPKTBUF
    658 	b = pktbuf;
    659 #endif
    660 	while (pktlen > 0) {
    661 		if (top) {
    662 			MGET(m, M_DONTWAIT, MT_DATA);
    663 			if (m == 0) {
    664 				m_freem(top);
    665 				return;
    666 			}
    667 			len = MLEN;
    668 		}
    669 		if (pktlen >= MINCLSIZE) {
    670 			MCLGET(m, M_DONTWAIT);
    671 			if (m->m_flags & M_EXT)
    672 				len = MCLBYTES;
    673 		}
    674 		m->m_len = len = min(pktlen, len);
    675 #ifdef USEPKTBUF
    676 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    677 		b += len;
    678 #else	/* USEPKTBUF */
    679 		buf = mtod(m, u_short *);
    680 		cnt = len / 2;
    681 		while (cnt--)
    682 			*buf++ = *data;
    683 		if (len & 1)
    684 			*buf = *data;	/* XXX should be byte store */
    685 #ifdef ESDEBUG
    686 		if (esdebug) {
    687 			buf = mtod(m, u_short *);
    688 			for (i = 0; i < len; ++i)
    689 				printf ("%02x%s", ((u_char *)buf)[i],
    690 				    ((i & 31) == 31) ? "\n" : "");
    691 			if (i & 31)
    692 				printf ("\n");
    693 		}
    694 #endif
    695 #endif	/* USEPKTBUF */
    696 		pktlen -= len;
    697 		*mp = m;
    698 		mp = &m->m_next;
    699 	}
    700 #ifndef USEPKTBUF
    701 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    702 	while (smc->b2.mmucr & MMUCR_BUSY)
    703 		;
    704 #endif
    705 	eh = mtod(top, struct ether_header *);
    706 #if NBPFILTER > 0
    707 	/*
    708 	 * Check if there's a BPF listener on this interface.  If so, hand off
    709 	 * the raw packet to bpf.
    710 	 */
    711 	if (sc->sc_arpcom.ac_if.if_bpf) {
    712 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, top);
    713 
    714 		/*
    715 		 * Note that the interface cannot be in promiscuous mode if
    716 		 * there are no BPF listeners.  And if we are in promiscuous
    717 		 * mode, we have to check if this packet is really ours.
    718 		 */
    719 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
    720 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    721 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    722 			    sizeof(eh->ether_dhost)) != 0) {
    723 			m_freem(top);
    724 			return;
    725 		}
    726 	}
    727 #endif
    728 	top->m_pkthdr.len -= sizeof (*eh);
    729 	top->m_len -= sizeof (*eh);
    730 	top->m_data += sizeof (*eh);
    731 
    732 	ether_input(ifp, eh, top);
    733 #ifdef ESDEBUG
    734 	if (--sc->sc_smcbusy) {
    735 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
    736 		panic("esintr busy on exit");
    737 	}
    738 #endif
    739 }
    740 
    741 void
    742 estint(sc)
    743 	struct es_softc *sc;
    744 {
    745 
    746 	esstart(&sc->sc_arpcom.ac_if);
    747 }
    748 
    749 void
    750 esstart(ifp)
    751 	struct ifnet *ifp;
    752 {
    753 	struct es_softc *sc = escd.cd_devs[ifp->if_unit];
    754 	union smcregs *smc = sc->sc_base;
    755 	struct mbuf *m0, *m;
    756 #ifdef USEPKTBUF
    757 	u_short pktbuf[ETHERMTU + 2];
    758 #else
    759 	u_short oddbyte, needbyte;
    760 #endif
    761 	u_short pktctlw, pktlen;
    762 	u_short *buf;
    763 	volatile u_short *data;
    764 	u_long *lbuf;
    765 	volatile u_long *ldata;
    766 	short cnt;
    767 	int i;
    768 	u_char active_pnr;
    769 
    770 	if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    771 	    IFF_RUNNING)
    772 		return;
    773 
    774 #ifdef ESDEBUG
    775 	if (sc->sc_smcbusy++) {
    776 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
    777 		panic("esstart re-entred");
    778 	}
    779 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    780 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    781 		    smc->b2.bsr);
    782 		smc->b2.bsr = BSR_BANK2;
    783 	}
    784 #endif
    785 	for (;;) {
    786 int xxx;
    787 		/*
    788 		 * Sneak a peek at the next packet to get the length
    789 		 * and see if the SMC 91C90 can accept it.
    790 		 */
    791 		m = sc->sc_arpcom.ac_if.if_snd.ifq_head;
    792 		if (!m)
    793 			break;
    794 #ifdef ESDEBUG
    795 if (esdebug && (m->m_next || m->m_len & 1))
    796   printf("%s: esstart m_next %x m_len %d\n", sc->sc_dev.dv_xname,
    797     m->m_next, m->m_len);
    798 #endif
    799 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    800 			pktlen += m0->m_len;
    801 		pktctlw = 0;
    802 		pktlen += 4;
    803 		if (pktlen & 1)
    804 			++pktlen;	/* control byte after last byte */
    805 		else
    806 			pktlen += 2;	/* control byte after pad byte */
    807 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    808 		for (i = 0; i <= 5; ++i)
    809 			if ((smc->b2.arr & ARR_FAILED) == 0)
    810 				break;
    811 		if (smc->b2.arr & ARR_FAILED) {
    812 			sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
    813 			sc->sc_intctl |= MSK_ALLOC;
    814 			break;
    815 		}
    816 		active_pnr = smc->b2.pnr = smc->b2.arr;
    817 
    818 #ifdef ESDEBUG
    819 		while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    820 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    821 			    smc->b2.bsr);
    822 		smc->b2.bsr = BSR_BANK2;
    823 		}
    824 #endif
    825 		IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
    826 xxx = splhigh();
    827 		smc->b2.ptr = PTR_AUTOINCR;
    828 		(void) smc->b2.mmucr;
    829 		data = (u_short *)&smc->b2.data;
    830 		*data = SWAP(pktctlw);
    831 		*data = SWAP(pktlen);
    832 #ifdef ESDEBUG
    833 		while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    834 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    835 			    smc->b2.bsr);
    836 			smc->b2.bsr = BSR_BANK2;
    837 		}
    838 #endif
    839 #ifdef USEPKTBUF
    840 		i = 0;
    841 		for (m0 = m; m; m = m->m_next) {
    842 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    843 			i += m->m_len;
    844 		}
    845 
    846 		if (i & 1)	/* Figure out where to put control byte */
    847 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    848 		else
    849 			pktbuf[i/2] = 0;
    850 		pktlen -= 4;
    851 #ifdef ESDEBUG
    852 		if (pktlen > sizeof(pktbuf))
    853 			printf("%s: esstart packet longer than pktbuf\n",
    854 			    sc->sc_dev.dv_xname);
    855 #endif
    856 #if 0 /* doesn't quite work? */
    857 		lbuf = (u_long *)(pktbuf);
    858 		ldata = (u_long *)data;
    859 		cnt = pktlen / 4;
    860 		while(cnt--)
    861 			*ldata = *lbuf++;
    862 		if (pktlen & 2) {
    863 			buf = (u_short *)lbuf;
    864 			*data = *buf;
    865 		}
    866 #else
    867 		buf = pktbuf;
    868 		cnt = pktlen / 2;
    869 		while (cnt--)
    870 			*data = *buf++;
    871 #endif
    872 #else	/* USEPKTBUF */
    873 		pktctlw = 0;
    874 		oddbyte = needbyte = 0;
    875 		for (m0 = m; m; m = m->m_next) {
    876 			buf = mtod(m, u_short *);
    877 			cnt = m->m_len / 2;
    878 			if (needbyte) {
    879 				oddbyte |= *buf >> 8;
    880 				*data = oddbyte;
    881 			}
    882 			while (cnt--)
    883 				*data = *buf++;
    884 			if (m->m_len & 1)
    885 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    886 			if (m->m_len & 1 && m->m_next)
    887 				printf("%s: esstart odd byte count in mbuf\n",
    888 				    sc->sc_dev.dv_xname);
    889 		}
    890 		*data = pktctlw;
    891 #endif	/* USEPKTBUF */
    892 splx(xxx);
    893 #ifdef ESDEBUG
    894 		while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    895 			printf("%s: esstart+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    896 			    smc->b2.bsr);
    897 			smc->b2.bsr = BSR_BANK2;
    898 		}
    899 #endif
    900 		smc->b2.mmucr = MMUCR_ENQ_TX;
    901 		if (smc->b2.pnr != active_pnr)
    902 			printf("%s: esstart - PNR changed %x->%x\n",
    903 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
    904 #if NBPFILTER > 0
    905 		if (sc->sc_arpcom.ac_if.if_bpf)
    906 			bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    907 #endif
    908 		m_freem(m0);
    909 		sc->sc_arpcom.ac_if.if_opackets++;	/* move to interrupt? */
    910 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
    911 	}
    912 	smc->b2.msk = sc->sc_intctl;
    913 #ifdef ESDEBUG
    914 	while ((smc->b2.bsr & 0x0300) != BSR_BANK2) {
    915 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
    916 		    smc->b2.bsr);
    917 		smc->b2.bsr = BSR_BANK2;
    918 	}
    919 	if (--sc->sc_smcbusy) {
    920 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
    921 		panic("esstart busy on exit");
    922 	}
    923 #endif
    924 }
    925 
    926 int
    927 esioctl(ifp, command, data)
    928 	register struct ifnet *ifp;
    929 	u_long command;
    930 	caddr_t data;
    931 {
    932 	struct es_softc *sc = escd.cd_devs[ifp->if_unit];
    933 	register struct ifaddr *ifa = (struct ifaddr *)data;
    934 	struct ifreq *ifr = (struct ifreq *)data;
    935 	int s, error = 0;
    936 
    937 	s = splnet();
    938 
    939 	switch (command) {
    940 
    941 	case SIOCSIFADDR:
    942 		ifp->if_flags |= IFF_UP;
    943 
    944 		switch (ifa->ifa_addr->sa_family) {
    945 #ifdef INET
    946 		case AF_INET:
    947 			esinit(sc);
    948 			arp_ifinit(&sc->sc_arpcom, ifa);
    949 			break;
    950 #endif
    951 #ifdef NS
    952 		case AF_NS:
    953 		    {
    954 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    955 
    956 			if (ns_nullhost(*ina))
    957 				ina->x_host =
    958 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    959 			else
    960 				bcopy(ina->x_host.c_host,
    961 				    sc->sc_arpcom.ac_enaddr,
    962 				    sizeof(sc->sc_arpcom.ac_enaddr));
    963 			/* Set new address. */
    964 			esinit(sc);
    965 			break;
    966 #endif
    967 		default:
    968 			esinit(sc);
    969 			break;
    970 		}
    971 		break;
    972 
    973 	case SIOCSIFFLAGS:
    974 		/*
    975 		 * If interface is marked down and it is running, then stop it
    976 		 */
    977 		if ((ifp->if_flags & IFF_UP) == 0 &&
    978 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    979 			/*
    980 			 * If interface is marked down and it is running, then
    981 			 * stop it.
    982 			 */
    983 			esstop(sc);
    984 			ifp->if_flags &= ~IFF_RUNNING;
    985 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    986 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    987 			/*
    988 			 * If interface is marked up and it is stopped, then
    989 			 * start it.
    990 			 */
    991 			esinit(sc);
    992 		} else {
    993 			/*
    994 			 * Reset the interface to pick up changes in any other
    995 			 * flags that affect hardware registers.
    996 			 */
    997 			esstop(sc);
    998 			esinit(sc);
    999 		}
   1000 #ifdef ESDEBUG
   1001 		if (ifp->if_flags & IFF_DEBUG)
   1002 			esdebug = sc->sc_debug = 1;
   1003 		else
   1004 			esdebug = sc->sc_debug = 0;
   1005 #endif
   1006 		break;
   1007 
   1008 	case SIOCADDMULTI:
   1009 	case SIOCDELMULTI:
   1010 		error = (command == SIOCADDMULTI) ?
   1011 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1012 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1013 
   1014 		if (error == ENETRESET) {
   1015 			/*
   1016 			 * Multicast list has changed; set the hardware filter
   1017 			 * accordingly.
   1018 			 */
   1019 			/* XXX */
   1020 			error = 0;
   1021 		}
   1022 		break;
   1023 
   1024 	default:
   1025 		error = EINVAL;
   1026 	}
   1027 
   1028 	splx(s);
   1029 	return (error);
   1030 }
   1031 
   1032 void
   1033 esreset(sc)
   1034 	struct es_softc *sc;
   1035 {
   1036 	int s;
   1037 
   1038 	s = splnet();
   1039 	esstop(sc);
   1040 	esinit(sc);
   1041 	splx(s);
   1042 }
   1043 
   1044 void
   1045 eswatchdog(unit)
   1046 	int unit;
   1047 {
   1048 	struct es_softc *sc = escd.cd_devs[unit];
   1049 
   1050 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1051 	++sc->sc_arpcom.ac_if.if_oerrors;
   1052 
   1053 	esreset(sc);
   1054 }
   1055