Home | History | Annotate | Line # | Download | only in dev
if_es.c revision 1.1
      1  1.1  chopps /*	$NetBSD: if_es.c,v 1.1 1995/02/13 00:27:08 chopps 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.1  chopps 
     37  1.1  chopps #include <sys/param.h>
     38  1.1  chopps #include <sys/systm.h>
     39  1.1  chopps #include <sys/mbuf.h>
     40  1.1  chopps #include <sys/buf.h>
     41  1.1  chopps #include <sys/protosw.h>
     42  1.1  chopps #include <sys/socket.h>
     43  1.1  chopps #include <sys/syslog.h>
     44  1.1  chopps #include <sys/ioctl.h>
     45  1.1  chopps #include <sys/errno.h>
     46  1.1  chopps #include <sys/device.h>
     47  1.1  chopps 
     48  1.1  chopps #include <net/if.h>
     49  1.1  chopps #include <net/netisr.h>
     50  1.1  chopps #include <net/route.h>
     51  1.1  chopps 
     52  1.1  chopps #ifdef INET
     53  1.1  chopps #include <netinet/in.h>
     54  1.1  chopps #include <netinet/in_systm.h>
     55  1.1  chopps #include <netinet/in_var.h>
     56  1.1  chopps #include <netinet/ip.h>
     57  1.1  chopps #include <netinet/if_ether.h>
     58  1.1  chopps #endif
     59  1.1  chopps 
     60  1.1  chopps #ifdef NS
     61  1.1  chopps #include <netns/ns.h>
     62  1.1  chopps #include <netns/ns_if.h>
     63  1.1  chopps #endif
     64  1.1  chopps 
     65  1.1  chopps #include <machine/cpu.h>
     66  1.1  chopps #include <machine/mtpr.h>
     67  1.1  chopps #include <amiga/amiga/device.h>
     68  1.1  chopps #include <amiga/amiga/isr.h>
     69  1.1  chopps #include <amiga/dev/zbusvar.h>
     70  1.1  chopps #include <amiga/dev/if_esreg.h>
     71  1.1  chopps 
     72  1.1  chopps #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
     73  1.1  chopps 
     74  1.1  chopps #define ESDEBUG
     75  1.1  chopps #define	USEPKTBUF
     76  1.1  chopps 
     77  1.1  chopps #define	ETHER_MIN_LEN	64
     78  1.1  chopps #define	ETHER_MAX_LEN	1518
     79  1.1  chopps #define	ETHER_ADDR_LEN	6
     80  1.1  chopps 
     81  1.1  chopps /*
     82  1.1  chopps  * Ethernet software status per interface.
     83  1.1  chopps  *
     84  1.1  chopps  * Each interface is referenced by a network interface structure,
     85  1.1  chopps  * es_if, which the routing code uses to locate the interface.
     86  1.1  chopps  * This structure contains the output queue for the interface, its address, ...
     87  1.1  chopps  */
     88  1.1  chopps struct	es_softc {
     89  1.1  chopps 	struct	device sc_dev;
     90  1.1  chopps 	struct	isr sc_isr;
     91  1.1  chopps 	struct	arpcom sc_ac;	/* common Ethernet structures */
     92  1.1  chopps #define	sc_if	sc_ac.ac_if	/* network-visible interface */
     93  1.1  chopps #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
     94  1.1  chopps 	void	*sc_base;	/* base address of board */
     95  1.1  chopps 	short	sc_iflags;
     96  1.1  chopps #if NBPFILTER > 0
     97  1.1  chopps 	caddr_t sc_bpf;
     98  1.1  chopps #endif
     99  1.1  chopps 	unsigned short sc_intctl;
    100  1.1  chopps #ifdef ESDEBUG
    101  1.1  chopps 	int	sc_debug;
    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.1  chopps #endif
    114  1.1  chopps 
    115  1.1  chopps int esintr __P((struct es_softc *));
    116  1.1  chopps int esstart __P((struct ifnet *));
    117  1.1  chopps int eswatchdog __P((int));
    118  1.1  chopps int esioctl __P((struct ifnet *, u_long, caddr_t));
    119  1.1  chopps void esrint __P((struct es_softc *));
    120  1.1  chopps void estint __P((struct es_softc *));
    121  1.1  chopps void esinit __P((struct es_softc *));
    122  1.1  chopps void esreset __P((struct es_softc *));
    123  1.1  chopps 
    124  1.1  chopps extern	struct ifnet loif;
    125  1.1  chopps 
    126  1.1  chopps void esattach __P((struct device *, struct device *, void *));
    127  1.1  chopps int esmatch __P((struct device *, struct cfdata *, void *args));
    128  1.1  chopps 
    129  1.1  chopps struct cfdriver escd = {
    130  1.1  chopps 	NULL, "es", (cfmatch_t)esmatch, esattach, DV_IFNET,
    131  1.1  chopps 	sizeof(struct es_softc), NULL, 0};
    132  1.1  chopps 
    133  1.1  chopps int
    134  1.1  chopps esmatch(pdp, cfp, auxp)
    135  1.1  chopps 	struct device *pdp;
    136  1.1  chopps 	struct cfdata *cfp;
    137  1.1  chopps 	void *auxp;
    138  1.1  chopps {
    139  1.1  chopps 
    140  1.1  chopps 	struct zbus_args *zap;
    141  1.1  chopps 
    142  1.1  chopps 	zap = (struct zbus_args *)auxp;
    143  1.1  chopps 
    144  1.1  chopps 	/* Ameristar A4066 ethernet card */
    145  1.1  chopps 	if ( zap->manid == 1053 && zap->prodid == 10)
    146  1.1  chopps 		return(1);
    147  1.1  chopps 
    148  1.1  chopps 	return (0);
    149  1.1  chopps }
    150  1.1  chopps 
    151  1.1  chopps /*
    152  1.1  chopps  * Interface exists: make available by filling in network interface
    153  1.1  chopps  * record.  System will initialize the interface when it is ready
    154  1.1  chopps  * to accept packets.
    155  1.1  chopps  */
    156  1.1  chopps void
    157  1.1  chopps esattach(pdp, dp, auxp)
    158  1.1  chopps 	struct device *pdp, *dp;
    159  1.1  chopps 	void *auxp;
    160  1.1  chopps {
    161  1.1  chopps 	struct zbus_args *zap;
    162  1.1  chopps 	struct es_softc *sc = (struct es_softc *) dp;
    163  1.1  chopps 	struct ifnet *ifp = &sc->sc_if;
    164  1.1  chopps 	char *cp;
    165  1.1  chopps 	int i;
    166  1.1  chopps 	unsigned long ser;
    167  1.1  chopps 
    168  1.1  chopps 	zap =(struct zbus_args *)auxp;
    169  1.1  chopps 
    170  1.1  chopps 	/*
    171  1.1  chopps 	 * Make config msgs look nicer.
    172  1.1  chopps 	 */
    173  1.1  chopps /*	printf("\n");*/
    174  1.1  chopps 
    175  1.1  chopps 	sc->sc_base = zap->va;
    176  1.1  chopps 
    177  1.1  chopps 	/*
    178  1.1  chopps 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    179  1.1  chopps 	 * (Currently only Ameristar.)
    180  1.1  chopps 	 */
    181  1.1  chopps 	sc->sc_addr[0] = 0x00;
    182  1.1  chopps 	sc->sc_addr[1] = 0x00;
    183  1.1  chopps 	sc->sc_addr[2] = 0x9f;
    184  1.1  chopps 
    185  1.1  chopps 	/*
    186  1.1  chopps 	 * Serial number for board contains last 3 bytes.
    187  1.1  chopps 	 */
    188  1.1  chopps 	ser = (unsigned long) zap->serno;
    189  1.1  chopps 
    190  1.1  chopps 	sc->sc_addr[3] = (ser >> 16) & 0xff;
    191  1.1  chopps 	sc->sc_addr[4] = (ser >>  8) & 0xff;
    192  1.1  chopps 	sc->sc_addr[5] = (ser      ) & 0xff;
    193  1.1  chopps 
    194  1.1  chopps #if 0
    195  1.1  chopps 	printf("es%d: hardware address %s\n",
    196  1.1  chopps 	    dp->dv_unit, ether_sprintf(sc->sc_addr));
    197  1.1  chopps #else
    198  1.1  chopps 	printf(": address %s\n", ether_sprintf(sc->sc_addr));
    199  1.1  chopps #endif
    200  1.1  chopps 
    201  1.1  chopps 	ifp->if_unit = dp->dv_unit;
    202  1.1  chopps 	ifp->if_name = escd.cd_name;
    203  1.1  chopps 	ifp->if_mtu = ETHERMTU;
    204  1.1  chopps 	ifp->if_output = ether_output;
    205  1.1  chopps 	ifp->if_ioctl = esioctl;
    206  1.1  chopps 	ifp->if_start = esstart;
    207  1.1  chopps 	ifp->if_watchdog = eswatchdog;
    208  1.1  chopps 	/* XXX IFF_MULTICAST */
    209  1.1  chopps 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    210  1.1  chopps 
    211  1.1  chopps 	if_attach(ifp);
    212  1.1  chopps 	ether_ifattach(ifp);
    213  1.1  chopps #if NBPFILTER > 0
    214  1.1  chopps 	bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    215  1.1  chopps #endif
    216  1.1  chopps 
    217  1.1  chopps 	sc->sc_isr.isr_intr = esintr;
    218  1.1  chopps 	sc->sc_isr.isr_arg = sc;
    219  1.1  chopps 	sc->sc_isr.isr_ipl = 2;
    220  1.1  chopps 	add_isr (&sc->sc_isr);
    221  1.1  chopps 	return;
    222  1.1  chopps }
    223  1.1  chopps 
    224  1.1  chopps void
    225  1.1  chopps esinit(sc)
    226  1.1  chopps 	struct es_softc *sc;
    227  1.1  chopps {
    228  1.1  chopps 	struct ifnet *ifp = &sc->sc_ac.ac_if;
    229  1.1  chopps 	union smcregs *smc = sc->sc_base;
    230  1.1  chopps 	int s;
    231  1.1  chopps 
    232  1.1  chopps 	if (!ifp->if_addrlist)
    233  1.1  chopps 		return;
    234  1.1  chopps 
    235  1.1  chopps 	s = splimp();
    236  1.1  chopps 
    237  1.1  chopps 	smc->b0.bsr = 0x0000;		/* Select bank 0 */
    238  1.1  chopps 	smc->b0.rcr = RCR_EPH_RST;
    239  1.1  chopps 	smc->b0.rcr = 0;
    240  1.1  chopps 	smc->b3.bsr = 0x0300;		/* Select bank 3 */
    241  1.1  chopps 	smc->b3.mt[0] = 0;		/* clear Multicast table */
    242  1.1  chopps 	smc->b3.mt[1] = 0;
    243  1.1  chopps 	smc->b3.mt[2] = 0;
    244  1.1  chopps 	smc->b3.mt[3] = 0;
    245  1.1  chopps 	/* XXX set Multicast table from Multicast list */
    246  1.1  chopps 	smc->b1.bsr = 0x0100;		/* Select bank 1 */
    247  1.1  chopps 	smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
    248  1.1  chopps 	smc->b1.ctr = CTR_AUTO_RLSE;
    249  1.1  chopps 	smc->b1.iar[0] = *((unsigned short *) &sc->sc_addr[0]);
    250  1.1  chopps 	smc->b1.iar[1] = *((unsigned short *) &sc->sc_addr[2]);
    251  1.1  chopps 	smc->b1.iar[2] = *((unsigned short *) &sc->sc_addr[4]);
    252  1.1  chopps 	smc->b2.bsr = 0x0200;		/* Select bank 2 */
    253  1.1  chopps 	smc->b2.mmucr = MMUCR_RESET;
    254  1.1  chopps 	smc->b0.bsr = 0x0000;		/* Select bank 0 */
    255  1.1  chopps 	smc->b0.mcr = SWAP (0x0020);	/* reserve 8K for transmit buffers */
    256  1.1  chopps 	smc->b0.tcr = TCR_PAD_EN | TCR_TXENA + TCR_MON_CSN;
    257  1.1  chopps 	smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN;
    258  1.1  chopps 	/* XXX add multicast/promiscuous flags */
    259  1.1  chopps 	smc->b2.bsr = 0x0200;		/* Select bank 2 */
    260  1.1  chopps 	smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
    261  1.1  chopps 
    262  1.1  chopps 	/* Interface is now 'running', with no output active. */
    263  1.1  chopps 	ifp->if_flags |= IFF_RUNNING;
    264  1.1  chopps 	ifp->if_flags &= ~IFF_OACTIVE;
    265  1.1  chopps 
    266  1.1  chopps 	/* Attempt to start output, if any. */
    267  1.1  chopps 	esstart(ifp);
    268  1.1  chopps 
    269  1.1  chopps 	splx(s);
    270  1.1  chopps }
    271  1.1  chopps 
    272  1.1  chopps int
    273  1.1  chopps esintr(sc)
    274  1.1  chopps 	struct es_softc *sc;
    275  1.1  chopps {
    276  1.1  chopps 	int i;
    277  1.1  chopps 	int done = 0;
    278  1.1  chopps 	union smcregs *smc;
    279  1.1  chopps 
    280  1.1  chopps 	smc = sc->sc_base;
    281  1.1  chopps 	if ((smc->b2.msk & smc->b2.ist) == 0)
    282  1.1  chopps 		return (0);
    283  1.1  chopps #ifdef ESDEBUG
    284  1.1  chopps 	if (esdebug)
    285  1.1  chopps 		printf ("%s: esintr ist %02x msk %02x",
    286  1.1  chopps 		    sc->sc_dev.dv_xname, smc->b2.ist, smc->b2.msk);
    287  1.1  chopps #endif
    288  1.1  chopps 	smc->b2.msk = 0;
    289  1.1  chopps #ifdef ESDEBUG
    290  1.1  chopps 	if (esdebug)
    291  1.1  chopps 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
    292  1.1  chopps 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
    293  1.1  chopps 		    smc->b2.fifo);
    294  1.1  chopps #endif
    295  1.1  chopps 	if (smc->b2.ist & IST_ALLOC && sc->sc_intctl & MSK_ALLOC) {
    296  1.1  chopps 		sc->sc_intctl &= ~MSK_ALLOC;
    297  1.1  chopps #ifdef ESDEBUG
    298  1.1  chopps 		if (esdebug || 1)
    299  1.1  chopps 			printf ("%s: ist %02x\n", sc->sc_dev.dv_xname,
    300  1.1  chopps 			    smc->b2.ist);
    301  1.1  chopps #endif
    302  1.1  chopps 		if ((smc->b2.arr & ARR_FAILED) == 0) {
    303  1.1  chopps #ifdef ESDEBUG
    304  1.1  chopps 			if (esdebug || 1)
    305  1.1  chopps 				printf ("%s: arr %02x\n", sc->sc_dev.dv_xname,
    306  1.1  chopps 				    smc->b2.arr);
    307  1.1  chopps #endif
    308  1.1  chopps 			smc->b2.pnr = smc->b2.arr;
    309  1.1  chopps 			smc->b2.mmucr = MMUCR_RLSPKT;
    310  1.1  chopps 			while (smc->b2.mmucr & MMUCR_BUSY)
    311  1.1  chopps 				;
    312  1.1  chopps 			sc->sc_ac.ac_if.if_flags &= ~IFF_OACTIVE;
    313  1.1  chopps 		}
    314  1.1  chopps 	}
    315  1.1  chopps 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
    316  1.1  chopps 		esrint(sc);
    317  1.1  chopps 	}
    318  1.1  chopps 	if (smc->b2.ist & IST_RX_OVRN) {
    319  1.1  chopps 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
    320  1.1  chopps 		    smc->b2.ist);
    321  1.1  chopps 		smc->b2.ist = ACK_RX_OVRN;
    322  1.1  chopps 		printf ("->%02x\n", smc->b2.ist);
    323  1.1  chopps 		sc->sc_if.if_ierrors++;
    324  1.1  chopps 	}
    325  1.1  chopps 	if (smc->b2.ist & IST_TX) {
    326  1.1  chopps #ifdef ESDEBUG
    327  1.1  chopps 		if (esdebug)
    328  1.1  chopps 			printf ("%s: TX INT ist %02x",
    329  1.1  chopps 			    sc->sc_dev.dv_xname, smc->b2.ist);
    330  1.1  chopps #endif
    331  1.1  chopps 		smc->b2.ist = ACK_TX;
    332  1.1  chopps #ifdef ESDEBUG
    333  1.1  chopps 		if (esdebug)
    334  1.1  chopps 			printf ("->%02x\n", smc->b2.ist);
    335  1.1  chopps #endif
    336  1.1  chopps 		smc->b0.bsr = 0x0000;
    337  1.1  chopps 		printf ("%s: EPH status %04x\n", sc->sc_dev.dv_xname,
    338  1.1  chopps 		   smc->b0.ephsr);
    339  1.1  chopps 		if ((smc->b0.ephsr & EPHSR_TX_SUC) == 0) {
    340  1.1  chopps 			smc->b0.tcr |= TCR_TXENA;
    341  1.1  chopps 			sc->sc_if.if_oerrors++;
    342  1.1  chopps 		}
    343  1.1  chopps 		smc->b2.bsr = 0x0200;
    344  1.1  chopps 	}
    345  1.1  chopps 	if (smc->b2.ist & IST_TX_EMPTY) {
    346  1.1  chopps 		u_short ecr;
    347  1.1  chopps #ifdef ESDEBUG
    348  1.1  chopps 		if (esdebug)
    349  1.1  chopps 			printf ("%s: TX EMPTY %02x",
    350  1.1  chopps 			    sc->sc_dev.dv_xname, smc->b2.ist);
    351  1.1  chopps #endif
    352  1.1  chopps 		smc->b2.ist = ACK_TX_EMPTY;
    353  1.1  chopps #ifdef ESDEBUG
    354  1.1  chopps 		if (esdebug)
    355  1.1  chopps 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
    356  1.1  chopps 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
    357  1.1  chopps 			    smc->b2.arr);
    358  1.1  chopps #endif
    359  1.1  chopps 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
    360  1.1  chopps 		smc->b0.bsr = 0x0000;
    361  1.1  chopps 		ecr = smc->b0.ecr;	/* Get error counters */
    362  1.1  chopps 		if (ecr & 0xff00)
    363  1.1  chopps 			sc->sc_if.if_collisions += ((ecr >> 8) & 15) +
    364  1.1  chopps 			    ((ecr >> 11) & 0x1e);
    365  1.1  chopps 		smc->b2.bsr = 0x0200;
    366  1.1  chopps 	}
    367  1.1  chopps 	/* output packets */
    368  1.1  chopps 	estint(sc);
    369  1.1  chopps 	smc->b2.msk = sc->sc_intctl;
    370  1.1  chopps 	return (1);
    371  1.1  chopps }
    372  1.1  chopps 
    373  1.1  chopps void
    374  1.1  chopps esrint (sc)
    375  1.1  chopps 	struct es_softc *sc;
    376  1.1  chopps {
    377  1.1  chopps 	union smcregs *smc = sc->sc_base;
    378  1.1  chopps 	int i;
    379  1.1  chopps 	u_short len;
    380  1.1  chopps 	short cnt;
    381  1.1  chopps 	u_short pktctlw, pktlen, *buf;
    382  1.1  chopps 	u_long *lbuf;
    383  1.1  chopps 	volatile u_short *data;
    384  1.1  chopps 	volatile u_long *ldata;
    385  1.1  chopps 	struct ifnet *ifp;
    386  1.1  chopps 	struct mbuf *top, **mp, *m;
    387  1.1  chopps 	struct ether_header *eh;
    388  1.1  chopps #ifdef USEPKTBUF
    389  1.1  chopps 	u_char *b, pktbuf[1530];
    390  1.1  chopps #endif
    391  1.1  chopps 
    392  1.1  chopps #ifdef ESDEBUG
    393  1.1  chopps 	if (esdebug)
    394  1.1  chopps 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
    395  1.1  chopps 		    smc->b2.fifo);
    396  1.1  chopps #endif
    397  1.1  chopps 	data = (u_short *)&smc->b2.data;
    398  1.1  chopps 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP (0x0002);
    399  1.1  chopps 	(void) smc->b2.mmucr;
    400  1.1  chopps #ifdef ESDEBUG
    401  1.1  chopps 	if (esdebug)
    402  1.1  chopps 		printf ("->%04x", smc->b2.fifo);
    403  1.1  chopps #endif
    404  1.1  chopps 	len = *data;
    405  1.1  chopps 	len = SWAP (len);	/* Careful of macro side-effects */
    406  1.1  chopps #ifdef ESDEBUG
    407  1.1  chopps 	if (esdebug)
    408  1.1  chopps 		printf (" length %d", len);
    409  1.1  chopps #endif
    410  1.1  chopps 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR + PTR_READ | SWAP (0x0000);
    411  1.1  chopps 	(void) smc->b2.mmucr;
    412  1.1  chopps 	pktctlw = *data;
    413  1.1  chopps 	pktlen = *data;
    414  1.1  chopps 	pktctlw = SWAP (pktctlw);
    415  1.1  chopps 	pktlen = SWAP (pktlen) - 6;
    416  1.1  chopps 	if (pktctlw & RFSW_ODDFRM)
    417  1.1  chopps 		pktlen++;
    418  1.1  chopps 	/* XXX copy directly from controller to mbuf */
    419  1.1  chopps #ifdef USEPKTBUF
    420  1.1  chopps #if 1
    421  1.1  chopps 	lbuf = (u_long *) pktbuf;
    422  1.1  chopps 	ldata = (u_long *)data;
    423  1.1  chopps 	cnt = (len - 4) / 4;
    424  1.1  chopps 	while (cnt--)
    425  1.1  chopps 		*lbuf++ = *ldata;
    426  1.1  chopps 	if ((len - 4) & 2) {
    427  1.1  chopps 		buf = (u_short *) lbuf;
    428  1.1  chopps 		*buf = *data;
    429  1.1  chopps 	}
    430  1.1  chopps #else
    431  1.1  chopps 	buf = (u_short *)pktbuf;
    432  1.1  chopps 	cnt = (len - 4) / 2;
    433  1.1  chopps 	while (cnt--)
    434  1.1  chopps 		*buf++ = *data;
    435  1.1  chopps #endif
    436  1.1  chopps 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    437  1.1  chopps 	while (smc->b2.mmucr & MMUCR_BUSY)
    438  1.1  chopps 		;
    439  1.1  chopps #ifdef ESDEBUG
    440  1.1  chopps 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    441  1.1  chopps 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    442  1.1  chopps 		/* count input error? */
    443  1.1  chopps 	}
    444  1.1  chopps 	if (esdebug) {
    445  1.1  chopps 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    446  1.1  chopps 		    smc->b2.fifo);
    447  1.1  chopps 		for (i = 0; i < pktlen; ++i)
    448  1.1  chopps 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
    449  1.1  chopps 			    "");
    450  1.1  chopps 		if (i & 31)
    451  1.1  chopps 			printf ("\n");
    452  1.1  chopps 	}
    453  1.1  chopps #endif
    454  1.1  chopps #else	/* USEPKTBUF */
    455  1.1  chopps #ifdef ESDEBUG
    456  1.1  chopps 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
    457  1.1  chopps 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
    458  1.1  chopps 		/* count input error? */
    459  1.1  chopps 	}
    460  1.1  chopps 	if (esdebug) {
    461  1.1  chopps 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
    462  1.1  chopps 		    smc->b2.fifo);
    463  1.1  chopps 	}
    464  1.1  chopps #endif
    465  1.1  chopps #endif /* USEPKTBUF */
    466  1.1  chopps 	ifp = &sc->sc_ac.ac_if;
    467  1.1  chopps 	ifp->if_ipackets++;
    468  1.1  chopps 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    469  1.1  chopps 	if (m == NULL)
    470  1.1  chopps 		return;
    471  1.1  chopps 	m->m_pkthdr.rcvif = ifp;
    472  1.1  chopps 	m->m_pkthdr.len = pktlen;
    473  1.1  chopps 	len = MHLEN;
    474  1.1  chopps 	top = NULL;
    475  1.1  chopps 	mp = &top;
    476  1.1  chopps #ifdef USEPKTBUF
    477  1.1  chopps 	b = pktbuf;
    478  1.1  chopps #endif
    479  1.1  chopps 	while (pktlen > 0) {
    480  1.1  chopps 		if (top) {
    481  1.1  chopps 			MGET(m, M_DONTWAIT, MT_DATA);
    482  1.1  chopps 			if (m == 0) {
    483  1.1  chopps 				m_freem(top);
    484  1.1  chopps 				return;
    485  1.1  chopps 			}
    486  1.1  chopps 			len = MLEN;
    487  1.1  chopps 		}
    488  1.1  chopps 		if (pktlen >= MINCLSIZE) {
    489  1.1  chopps 			MCLGET(m, M_DONTWAIT);
    490  1.1  chopps 			if (m->m_flags & M_EXT)
    491  1.1  chopps 				len = MCLBYTES;
    492  1.1  chopps 		}
    493  1.1  chopps 		m->m_len = len = min(pktlen, len);
    494  1.1  chopps #ifdef USEPKTBUF
    495  1.1  chopps 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
    496  1.1  chopps 		b += len;
    497  1.1  chopps #else	/* USEPKTBUF */
    498  1.1  chopps 		buf = mtod(m, u_short *);
    499  1.1  chopps 		cnt = len / 2;
    500  1.1  chopps 		while (cnt--)
    501  1.1  chopps 			*buf++ = *data;
    502  1.1  chopps 		if (len & 1)
    503  1.1  chopps 			*buf = *data;	/* XXX should be byte store */
    504  1.1  chopps #ifdef ESDEBUG
    505  1.1  chopps 		if (esdebug) {
    506  1.1  chopps 			buf = mtod(m, u_short *);
    507  1.1  chopps 			for (i = 0; i < len; ++i)
    508  1.1  chopps 				printf ("%02x%s", ((u_char *)buf)[i],
    509  1.1  chopps 				    ((i & 31) == 31) ? "\n" : "");
    510  1.1  chopps 			if (i & 31)
    511  1.1  chopps 				printf ("\n");
    512  1.1  chopps 		}
    513  1.1  chopps #endif
    514  1.1  chopps #endif	/* USEPKTBUF */
    515  1.1  chopps 		pktlen -= len;
    516  1.1  chopps 		*mp = m;
    517  1.1  chopps 		mp = &m->m_next;
    518  1.1  chopps 	}
    519  1.1  chopps #ifndef USEPKTBUF
    520  1.1  chopps 	smc->b2.mmucr = MMUCR_REMRLS_RX;
    521  1.1  chopps 	while (smc->b2.mmucr & MMUCR_BUSY)
    522  1.1  chopps 		;
    523  1.1  chopps #endif
    524  1.1  chopps 	eh = mtod(top, struct ether_header *);
    525  1.1  chopps 	top->m_pkthdr.len -= sizeof (*eh);
    526  1.1  chopps 	top->m_len -= sizeof (*eh);
    527  1.1  chopps 	top->m_data += sizeof (*eh);
    528  1.1  chopps 
    529  1.1  chopps 	ether_input(ifp, eh, top);
    530  1.1  chopps }
    531  1.1  chopps 
    532  1.1  chopps void
    533  1.1  chopps estint (sc)
    534  1.1  chopps 	struct es_softc *sc;
    535  1.1  chopps {
    536  1.1  chopps 	esstart (&sc->sc_ac.ac_if);
    537  1.1  chopps }
    538  1.1  chopps 
    539  1.1  chopps int
    540  1.1  chopps esstart(ifp)
    541  1.1  chopps 	struct ifnet *ifp;
    542  1.1  chopps {
    543  1.1  chopps 	struct es_softc *sc = escd.cd_devs[ifp->if_unit];
    544  1.1  chopps 	union smcregs *smc = sc->sc_base;
    545  1.1  chopps 	struct mbuf *m0, *m;
    546  1.1  chopps #ifdef USEPKTBUF
    547  1.1  chopps 	u_short pktbuf[ETHER_MAX_LEN + 2];
    548  1.1  chopps #endif
    549  1.1  chopps 	u_short pktctlw, pktlen;
    550  1.1  chopps 	u_short *buf;
    551  1.1  chopps 	volatile u_short *data;
    552  1.1  chopps 	u_long *lbuf;
    553  1.1  chopps 	volatile u_long *ldata;
    554  1.1  chopps 	short cnt;
    555  1.1  chopps 	int i;
    556  1.1  chopps 
    557  1.1  chopps 	if ((sc->sc_ac.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    558  1.1  chopps 	    IFF_RUNNING)
    559  1.1  chopps 		return;
    560  1.1  chopps 
    561  1.1  chopps 	for (;;) {
    562  1.1  chopps 		/*
    563  1.1  chopps 		 * Sneak a peek at the next packet to get the length
    564  1.1  chopps 		 * and see if the SMC 91C90 can accept it.
    565  1.1  chopps 		 */
    566  1.1  chopps 		m = sc->sc_ac.ac_if.if_snd.ifq_head;
    567  1.1  chopps 		if (!m)
    568  1.1  chopps 			break;
    569  1.1  chopps #ifdef ESDEBUG
    570  1.1  chopps if (esdebug && (m->m_next || m->m_len & 1))
    571  1.1  chopps   printf("%s: esstart m_next %x m_len %d\n", sc->sc_dev.dv_xname,
    572  1.1  chopps     m->m_next, m->m_len);
    573  1.1  chopps #endif
    574  1.1  chopps 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
    575  1.1  chopps 			pktlen += m0->m_len;
    576  1.1  chopps #ifdef ESDEBUG
    577  1.1  chopps if (esdebug)
    578  1.1  chopps   printf("%s: esstart r1 %x len %d", sc->sc_dev.dv_xname, smc->b2.pnr, pktlen);
    579  1.1  chopps #endif
    580  1.1  chopps 		pktctlw = 0;
    581  1.1  chopps 		pktlen += 4;
    582  1.1  chopps 		if (pktlen & 1)
    583  1.1  chopps 			++pktlen;	/* control byte after last byte */
    584  1.1  chopps 		else
    585  1.1  chopps 			pktlen += 2;	/* control byte after pad byte */
    586  1.1  chopps 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
    587  1.1  chopps 		for (i = 0; i <= 5; ++i)
    588  1.1  chopps 			if ((smc->b2.arr & ARR_FAILED) == 0)
    589  1.1  chopps 				break;
    590  1.1  chopps 		if (smc->b2.arr & ARR_FAILED) {
    591  1.1  chopps 			sc->sc_ac.ac_if.if_flags |= IFF_OACTIVE;
    592  1.1  chopps #ifdef ESDEBUG
    593  1.1  chopps if (esdebug)
    594  1.1  chopps   printf("-no xmit bufs r1 %x\n", smc->b2.pnr);
    595  1.1  chopps #endif
    596  1.1  chopps 			sc->sc_intctl |= 0x0008;
    597  1.1  chopps 			break;
    598  1.1  chopps 		}
    599  1.1  chopps 		smc->b2.pnr = smc->b2.arr;
    600  1.1  chopps 
    601  1.1  chopps 		IF_DEQUEUE(&sc->sc_ac.ac_if.if_snd, m);
    602  1.1  chopps 		smc->b2.ptr = PTR_AUTOINCR;
    603  1.1  chopps 		(void) smc->b2.mmucr;
    604  1.1  chopps 		data = (u_short *)&smc->b2.data;
    605  1.1  chopps 		*data = SWAP (pktctlw);
    606  1.1  chopps 		*data = SWAP (pktlen);
    607  1.1  chopps #ifdef USEPKTBUF
    608  1.1  chopps 		i = 0;
    609  1.1  chopps 		for (m0 = m; m; m = m->m_next) {
    610  1.1  chopps 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
    611  1.1  chopps 			i += m->m_len;
    612  1.1  chopps 		}
    613  1.1  chopps 
    614  1.1  chopps 		if (i & 1)	/* Figure out where to put control byte */
    615  1.1  chopps 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
    616  1.1  chopps 		else
    617  1.1  chopps 			pktbuf[i/2] = 0;
    618  1.1  chopps #ifdef ESDEBUG
    619  1.1  chopps if (esdebug) {
    620  1.1  chopps   int j;
    621  1.1  chopps   printf("-sending len %d pktlen %d r1 %04x\n", i, pktlen, smc->b2.pnr);
    622  1.1  chopps   for (j = 0; j < pktlen - 4; ++j)
    623  1.1  chopps     printf("%02x%s", ((u_char *)pktbuf)[j], ((j & 31) == 31) ? "\n" : "");
    624  1.1  chopps   if (j & 31)
    625  1.1  chopps     printf("\n");
    626  1.1  chopps }
    627  1.1  chopps #endif
    628  1.1  chopps #if 0 /* doesn't quite work? */
    629  1.1  chopps 		lbuf = (u_long *)(pktbuf);
    630  1.1  chopps 		ldata = (u_long *)data;
    631  1.1  chopps 		cnt = pktlen / 4;
    632  1.1  chopps 		while(cnt--)
    633  1.1  chopps 			*ldata = *lbuf++;
    634  1.1  chopps 		if (pktlen & 2) {
    635  1.1  chopps 			buf = (u_short *)lbuf;
    636  1.1  chopps 			*data = *buf;
    637  1.1  chopps 		}
    638  1.1  chopps #else
    639  1.1  chopps 		buf = pktbuf;
    640  1.1  chopps 		cnt = pktlen / 2;
    641  1.1  chopps 		while (cnt--)
    642  1.1  chopps 			*data = *buf++;
    643  1.1  chopps #endif
    644  1.1  chopps #else	/* USEPKTBUF */
    645  1.1  chopps #ifdef ESDEBUG
    646  1.1  chopps if (esdebug)
    647  1.1  chopps   printf("-sending pktlen %d r1 %04x\n", pktlen, smc->b2.pnr);
    648  1.1  chopps #endif
    649  1.1  chopps 		pktctlw = 0;
    650  1.1  chopps 		for (m0 = m; m; m = m->m_next) {
    651  1.1  chopps 			buf = mtod(m, u_short *);
    652  1.1  chopps #ifdef ESDEBUG
    653  1.1  chopps if (esdebug) {
    654  1.1  chopps   printf(" m->m_len %d\n", m->m_len);
    655  1.1  chopps   for (i = 0; i < m->m_len; ++i)
    656  1.1  chopps     printf("%02x%s", ((u_char *)buf)[i], ((i & 31) == 31) ? "\n" : "");
    657  1.1  chopps   if (i & 31)
    658  1.1  chopps     printf("\n");
    659  1.1  chopps }
    660  1.1  chopps #endif
    661  1.1  chopps 			cnt = m->m_len / 2;
    662  1.1  chopps 			while (cnt--)
    663  1.1  chopps 				*data = *buf++;
    664  1.1  chopps 			if (m->m_len & 1)
    665  1.1  chopps 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
    666  1.1  chopps 		}
    667  1.1  chopps 		*data = pktctlw;
    668  1.1  chopps #endif	/* USEPKTBUF */
    669  1.1  chopps 		smc->b2.mmucr = MMUCR_ENQ_TX;
    670  1.1  chopps #ifdef ESDEBUG
    671  1.1  chopps if (esdebug)
    672  1.1  chopps   printf("%s: esstart packet sent r1 %x\n", sc->sc_dev.dv_xname, smc->b2.pnr);
    673  1.1  chopps #endif
    674  1.1  chopps #if NBPFILTER > 0
    675  1.1  chopps 		if (sc->sc_ac.ac_if.if_bpf)
    676  1.1  chopps 			bpf_mtap(sc->sc_ac.ac_if.if_bpf, m0);
    677  1.1  chopps #endif
    678  1.1  chopps 		m_freem(m0);
    679  1.1  chopps 		sc->sc_ac.ac_if.if_opackets++;	/* move to interrupt? */
    680  1.1  chopps 		sc->sc_intctl |= 0x0006;
    681  1.1  chopps 	}
    682  1.1  chopps 	smc->b2.msk = sc->sc_intctl;
    683  1.1  chopps 	return 0;
    684  1.1  chopps }
    685  1.1  chopps 
    686  1.1  chopps int
    687  1.1  chopps esioctl(ifp, cmd, data)
    688  1.1  chopps 	struct ifnet *ifp;
    689  1.1  chopps 	u_long cmd;
    690  1.1  chopps 	caddr_t data;
    691  1.1  chopps {
    692  1.1  chopps 	struct es_softc *sc = escd.cd_devs[ifp->if_unit];
    693  1.1  chopps 	struct ifaddr *ifa = (struct ifaddr *)data;
    694  1.1  chopps 	struct ifreq *ifr = (struct ifreq *)data;
    695  1.1  chopps 	int s, error = 0;
    696  1.1  chopps 
    697  1.1  chopps #ifdef ESDEBUG
    698  1.1  chopps 	if (esdebug)
    699  1.1  chopps 		printf ("esioctl called: cmd %x\n", cmd);
    700  1.1  chopps #endif
    701  1.1  chopps 
    702  1.1  chopps 	s = splimp();
    703  1.1  chopps 
    704  1.1  chopps 	switch (cmd) {
    705  1.1  chopps 	case SIOCSIFADDR:
    706  1.1  chopps 		ifp->if_flags |= IFF_UP;
    707  1.1  chopps 
    708  1.1  chopps 		switch (ifa->ifa_addr->sa_family) {
    709  1.1  chopps #ifdef INET
    710  1.1  chopps 		case AF_INET:
    711  1.1  chopps 			esinit(sc);
    712  1.1  chopps 			sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
    713  1.1  chopps 			arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
    714  1.1  chopps 			break;
    715  1.1  chopps #endif
    716  1.1  chopps #ifdef NS
    717  1.1  chopps 		case AF_NS:
    718  1.1  chopps 		    {
    719  1.1  chopps 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    720  1.1  chopps 
    721  1.1  chopps 			if (ns_nullhost(*ina))
    722  1.1  chopps 				ina->x_host =
    723  1.1  chopps 				    *(uniion ns_host *)(sc->sc_addr);
    724  1.1  chopps 			else
    725  1.1  chopps 				bcopy(ina->x_host.c_host,
    726  1.1  chopps 				    sc->sc_addr, sizeof(sc->sc_addr));
    727  1.1  chopps 			/* Set new address */
    728  1.1  chopps 			esinit(sc);
    729  1.1  chopps 			break;
    730  1.1  chopps #endif
    731  1.1  chopps 		default:
    732  1.1  chopps 			esinit(sc);
    733  1.1  chopps 			break;
    734  1.1  chopps 		}
    735  1.1  chopps 		break;
    736  1.1  chopps 	case SIOCSIFFLAGS:
    737  1.1  chopps 		/*
    738  1.1  chopps 		 * If interface is marked down and it is running, then stop it
    739  1.1  chopps 		 */
    740  1.1  chopps 		if ((ifp->if_flags & IFF_UP) == 0 &&
    741  1.1  chopps 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    742  1.1  chopps 			/*
    743  1.1  chopps 			 * If interface is marked down and it is running, then
    744  1.1  chopps 			 * stop it.
    745  1.1  chopps 			 */
    746  1.1  chopps 			/* esstop(sc); */
    747  1.1  chopps 			ifp->if_flags &= ~IFF_RUNNING;
    748  1.1  chopps 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    749  1.1  chopps 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    750  1.1  chopps 			/*
    751  1.1  chopps 			 * If interface is marked up and it is stopped, then
    752  1.1  chopps 			 * start it.
    753  1.1  chopps 			 */
    754  1.1  chopps 			esinit(sc);
    755  1.1  chopps 		} else {
    756  1.1  chopps 			/*
    757  1.1  chopps 			 * Reset the interface to pick up changes in any other
    758  1.1  chopps 			 * flags that affect hardware registers.
    759  1.1  chopps 			 */
    760  1.1  chopps 			/* esstop(sc); */
    761  1.1  chopps 			esinit(sc);
    762  1.1  chopps 		}
    763  1.1  chopps #ifdef ESDEBUG
    764  1.1  chopps 		if (ifp->if_flags & IFF_DEBUG)
    765  1.1  chopps 			esdebug = sc->sc_debug = 1;
    766  1.1  chopps 		else
    767  1.1  chopps 			esdebug = sc->sc_debug = 0;
    768  1.1  chopps #endif
    769  1.1  chopps 		break;
    770  1.1  chopps 	case SIOCADDMULTI:
    771  1.1  chopps 	case SIOCDELMULTI:
    772  1.1  chopps 
    773  1.1  chopps 	default:
    774  1.1  chopps 		error = EINVAL;
    775  1.1  chopps 	}
    776  1.1  chopps 
    777  1.1  chopps 	splx(s);
    778  1.1  chopps 	return (error);
    779  1.1  chopps }
    780  1.1  chopps 
    781  1.1  chopps void
    782  1.1  chopps esreset(sc)
    783  1.1  chopps 	struct es_softc *sc;
    784  1.1  chopps {
    785  1.1  chopps 	int s;
    786  1.1  chopps 
    787  1.1  chopps 	s = splimp();
    788  1.1  chopps 	/* esstop(sc); */
    789  1.1  chopps 	esinit(sc);
    790  1.1  chopps 	splx(s);
    791  1.1  chopps }
    792  1.1  chopps 
    793  1.1  chopps int
    794  1.1  chopps eswatchdog(unit)
    795  1.1  chopps 	int unit;
    796  1.1  chopps {
    797  1.1  chopps 	register struct es_softc *sc = escd.cd_devs[unit];
    798  1.1  chopps 
    799  1.1  chopps 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    800  1.1  chopps 	esreset(sc);
    801  1.1  chopps 	return (0);
    802  1.1  chopps }
    803