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