Home | History | Annotate | Line # | Download | only in apbus
if_sn.c revision 1.7.2.2
      1  1.7.2.2  bouyer /*	$NetBSD: if_sn.c,v 1.7.2.2 2000/11/20 20:17:15 bouyer Exp $	*/
      2  1.7.2.2  bouyer 
      3  1.7.2.2  bouyer /*
      4  1.7.2.2  bouyer  * National Semiconductor  DP8393X SONIC Driver
      5  1.7.2.2  bouyer  * Copyright (c) 1991   Algorithmics Ltd (http://www.algor.co.uk)
      6  1.7.2.2  bouyer  * You may use, copy, and modify this program so long as you retain the
      7  1.7.2.2  bouyer  * copyright line.
      8  1.7.2.2  bouyer  *
      9  1.7.2.2  bouyer  * This driver has been substantially modified since Algorithmics donated
     10  1.7.2.2  bouyer  * it.
     11  1.7.2.2  bouyer  *
     12  1.7.2.2  bouyer  *   Denton Gentry <denny1 (at) home.com>
     13  1.7.2.2  bouyer  * and also
     14  1.7.2.2  bouyer  *   Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>
     15  1.7.2.2  bouyer  * did the work to get this running on the Macintosh.
     16  1.7.2.2  bouyer  */
     17  1.7.2.2  bouyer 
     18  1.7.2.2  bouyer #include "opt_inet.h"
     19  1.7.2.2  bouyer 
     20  1.7.2.2  bouyer #include <sys/param.h>
     21  1.7.2.2  bouyer #include <sys/systm.h>
     22  1.7.2.2  bouyer #include <sys/mbuf.h>
     23  1.7.2.2  bouyer #include <sys/buf.h>
     24  1.7.2.2  bouyer #include <sys/protosw.h>
     25  1.7.2.2  bouyer #include <sys/socket.h>
     26  1.7.2.2  bouyer #include <sys/syslog.h>
     27  1.7.2.2  bouyer #include <sys/ioctl.h>
     28  1.7.2.2  bouyer #include <sys/errno.h>
     29  1.7.2.2  bouyer #include <sys/device.h>
     30  1.7.2.2  bouyer 
     31  1.7.2.2  bouyer #include <net/if.h>
     32  1.7.2.2  bouyer #include <net/if_dl.h>
     33  1.7.2.2  bouyer #include <net/if_ether.h>
     34  1.7.2.2  bouyer 
     35  1.7.2.2  bouyer #ifdef INET
     36  1.7.2.2  bouyer #include <netinet/in.h>
     37  1.7.2.2  bouyer #include <netinet/in_systm.h>
     38  1.7.2.2  bouyer #include <netinet/in_var.h>
     39  1.7.2.2  bouyer #include <netinet/ip.h>
     40  1.7.2.2  bouyer #include <netinet/if_inarp.h>
     41  1.7.2.2  bouyer #endif
     42  1.7.2.2  bouyer 
     43  1.7.2.2  bouyer #include <uvm/uvm_extern.h>
     44  1.7.2.2  bouyer 
     45  1.7.2.2  bouyer #include "bpfilter.h"
     46  1.7.2.2  bouyer #if NBPFILTER > 0
     47  1.7.2.2  bouyer #include <net/bpf.h>
     48  1.7.2.2  bouyer #include <net/bpfdesc.h>
     49  1.7.2.2  bouyer #endif
     50  1.7.2.2  bouyer 
     51  1.7.2.2  bouyer #include <machine/cpu.h>
     52  1.7.2.2  bouyer #include <newsmips/apbus/if_snreg.h>
     53  1.7.2.2  bouyer #include <newsmips/apbus/if_snvar.h>
     54  1.7.2.2  bouyer 
     55  1.7.2.2  bouyer /* #define SONIC_DEBUG */
     56  1.7.2.2  bouyer 
     57  1.7.2.2  bouyer #ifdef SONIC_DEBUG
     58  1.7.2.2  bouyer # define DPRINTF printf
     59  1.7.2.2  bouyer #else
     60  1.7.2.2  bouyer # define DPRINTF while (0) printf
     61  1.7.2.2  bouyer #endif
     62  1.7.2.2  bouyer 
     63  1.7.2.2  bouyer static void	snwatchdog __P((struct ifnet *));
     64  1.7.2.2  bouyer static int	sninit __P((struct sn_softc *sc));
     65  1.7.2.2  bouyer static int	snstop __P((struct sn_softc *sc));
     66  1.7.2.2  bouyer static int	snioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
     67  1.7.2.2  bouyer static void	snstart __P((struct ifnet *ifp));
     68  1.7.2.2  bouyer static void	snreset __P((struct sn_softc *sc));
     69  1.7.2.2  bouyer 
     70  1.7.2.2  bouyer static void	caminitialise __P((struct sn_softc *));
     71  1.7.2.2  bouyer static void	camentry __P((struct sn_softc *, int, u_char *ea));
     72  1.7.2.2  bouyer static void	camprogram __P((struct sn_softc *));
     73  1.7.2.2  bouyer static void	initialise_tda __P((struct sn_softc *));
     74  1.7.2.2  bouyer static void	initialise_rda __P((struct sn_softc *));
     75  1.7.2.2  bouyer static void	initialise_rra __P((struct sn_softc *));
     76  1.7.2.2  bouyer #ifdef SNDEBUG
     77  1.7.2.2  bouyer static void	camdump __P((struct sn_softc *sc));
     78  1.7.2.2  bouyer #endif
     79  1.7.2.2  bouyer 
     80  1.7.2.2  bouyer static void	sonictxint __P((struct sn_softc *));
     81  1.7.2.2  bouyer static void	sonicrxint __P((struct sn_softc *));
     82  1.7.2.2  bouyer 
     83  1.7.2.2  bouyer static __inline__ u_int	sonicput __P((struct sn_softc *sc, struct mbuf *m0,
     84  1.7.2.2  bouyer 			    int mtd_next));
     85  1.7.2.2  bouyer static __inline__ int	sonic_read __P((struct sn_softc *, caddr_t, int));
     86  1.7.2.2  bouyer static __inline__ struct mbuf *sonic_get __P((struct sn_softc *, caddr_t, int));
     87  1.7.2.2  bouyer 
     88  1.7.2.2  bouyer #undef assert
     89  1.7.2.2  bouyer #undef _assert
     90  1.7.2.2  bouyer 
     91  1.7.2.2  bouyer #ifdef NDEBUG
     92  1.7.2.2  bouyer #define	assert(e)	((void)0)
     93  1.7.2.2  bouyer #define	_assert(e)	((void)0)
     94  1.7.2.2  bouyer #else
     95  1.7.2.2  bouyer #define	_assert(e)	assert(e)
     96  1.7.2.2  bouyer #ifdef __STDC__
     97  1.7.2.2  bouyer #define	assert(e)	((e) ? (void)0 : __assert("sn ", __FILE__, __LINE__, #e))
     98  1.7.2.2  bouyer #else	/* PCC */
     99  1.7.2.2  bouyer #define	assert(e)	((e) ? (void)0 : __assert("sn "__FILE__, __LINE__, "e"))
    100  1.7.2.2  bouyer #endif
    101  1.7.2.2  bouyer #endif
    102  1.7.2.2  bouyer 
    103  1.7.2.2  bouyer int sndebug = 0;
    104  1.7.2.2  bouyer 
    105  1.7.2.2  bouyer /*
    106  1.7.2.2  bouyer  * SONIC buffers need to be aligned 16 or 32 bit aligned.
    107  1.7.2.2  bouyer  * These macros calculate and verify alignment.
    108  1.7.2.2  bouyer  */
    109  1.7.2.2  bouyer #define	ROUNDUP(p, N)	(((int) p + N - 1) & ~(N - 1))
    110  1.7.2.2  bouyer 
    111  1.7.2.2  bouyer #define SOALIGN(m, array)	(m ? (ROUNDUP(array, 4)) : (ROUNDUP(array, 2)))
    112  1.7.2.2  bouyer 
    113  1.7.2.2  bouyer #define LOWER(x) ((unsigned)(x) & 0xffff)
    114  1.7.2.2  bouyer #define UPPER(x) ((unsigned)(x) >> 16)
    115  1.7.2.2  bouyer 
    116  1.7.2.2  bouyer /*
    117  1.7.2.2  bouyer  * Interface exists: make available by filling in network interface
    118  1.7.2.2  bouyer  * record.  System will initialize the interface when it is ready
    119  1.7.2.2  bouyer  * to accept packets.
    120  1.7.2.2  bouyer  */
    121  1.7.2.2  bouyer int
    122  1.7.2.2  bouyer snsetup(sc, lladdr)
    123  1.7.2.2  bouyer 	struct sn_softc	*sc;
    124  1.7.2.2  bouyer 	u_int8_t *lladdr;
    125  1.7.2.2  bouyer {
    126  1.7.2.2  bouyer 	struct ifnet *ifp = &sc->sc_if;
    127  1.7.2.2  bouyer 	u_char	*p;
    128  1.7.2.2  bouyer 	u_char	*pp;
    129  1.7.2.2  bouyer 	int	i;
    130  1.7.2.2  bouyer 
    131  1.7.2.2  bouyer 	if (sc->space == NULL) {
    132  1.7.2.2  bouyer 		printf ("%s: memory allocation for descriptors failed\n",
    133  1.7.2.2  bouyer 		    sc->sc_dev.dv_xname);
    134  1.7.2.2  bouyer 		return (1);
    135  1.7.2.2  bouyer 	}
    136  1.7.2.2  bouyer 
    137  1.7.2.2  bouyer 	/*
    138  1.7.2.2  bouyer 	 * Put the pup in reset mode (sninit() will fix it later),
    139  1.7.2.2  bouyer 	 * stop the timer, disable all interrupts and clear any interrupts.
    140  1.7.2.2  bouyer 	 */
    141  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_STP);
    142  1.7.2.2  bouyer 	wbflush();
    143  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RST);
    144  1.7.2.2  bouyer 	wbflush();
    145  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_IMR, 0);
    146  1.7.2.2  bouyer 	wbflush();
    147  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_ISR, ISR_ALL);
    148  1.7.2.2  bouyer 	wbflush();
    149  1.7.2.2  bouyer 
    150  1.7.2.2  bouyer 	/*
    151  1.7.2.2  bouyer 	 * because the SONIC is basically 16bit device it 'concatenates'
    152  1.7.2.2  bouyer 	 * a higher buffer address to a 16 bit offset--this will cause wrap
    153  1.7.2.2  bouyer 	 * around problems near the end of 64k !!
    154  1.7.2.2  bouyer 	 */
    155  1.7.2.2  bouyer 	p = sc->space;
    156  1.7.2.2  bouyer 	pp = (u_char *)ROUNDUP ((int)p, NBPG);
    157  1.7.2.2  bouyer 	p = pp;
    158  1.7.2.2  bouyer 
    159  1.7.2.2  bouyer 	for (i = 0; i < NRRA; i++) {
    160  1.7.2.2  bouyer 		sc->p_rra[i] = (void *)p;
    161  1.7.2.2  bouyer 		sc->v_rra[i] = SONIC_GETDMA(p);
    162  1.7.2.2  bouyer 		p += RXRSRC_SIZE(sc);
    163  1.7.2.2  bouyer 	}
    164  1.7.2.2  bouyer 	sc->v_rea = SONIC_GETDMA(p);
    165  1.7.2.2  bouyer 
    166  1.7.2.2  bouyer 	p = (u_char *)SOALIGN(sc, p);
    167  1.7.2.2  bouyer 
    168  1.7.2.2  bouyer 	sc->p_cda = (void *)(p);
    169  1.7.2.2  bouyer 	sc->v_cda = SONIC_GETDMA(p);
    170  1.7.2.2  bouyer 	p += CDA_SIZE(sc);
    171  1.7.2.2  bouyer 
    172  1.7.2.2  bouyer 	p = (u_char *)SOALIGN(sc, p);
    173  1.7.2.2  bouyer 
    174  1.7.2.2  bouyer 	for (i = 0; i < NTDA; i++) {
    175  1.7.2.2  bouyer 		struct mtd *mtdp = &sc->mtda[i];
    176  1.7.2.2  bouyer 		mtdp->mtd_txp = (void *)p;
    177  1.7.2.2  bouyer 		mtdp->mtd_vtxp = SONIC_GETDMA(p);
    178  1.7.2.2  bouyer 		p += TXP_SIZE(sc);
    179  1.7.2.2  bouyer 	}
    180  1.7.2.2  bouyer 
    181  1.7.2.2  bouyer 	p = (u_char *)SOALIGN(sc, p);
    182  1.7.2.2  bouyer 
    183  1.7.2.2  bouyer 	if ((p - pp) > NBPG) {
    184  1.7.2.2  bouyer 		printf ("%s: sizeof RRA (%ld) + CDA (%ld) +"
    185  1.7.2.2  bouyer 		    "TDA (%ld) > NBPG (%d). Punt!\n",
    186  1.7.2.2  bouyer 		    sc->sc_dev.dv_xname,
    187  1.7.2.2  bouyer 		    (ulong)sc->p_cda - (ulong)sc->p_rra[0],
    188  1.7.2.2  bouyer 		    (ulong)sc->mtda[0].mtd_txp - (ulong)sc->p_cda,
    189  1.7.2.2  bouyer 		    (ulong)p - (ulong)sc->mtda[0].mtd_txp,
    190  1.7.2.2  bouyer 		    NBPG);
    191  1.7.2.2  bouyer 		return(1);
    192  1.7.2.2  bouyer 	}
    193  1.7.2.2  bouyer 
    194  1.7.2.2  bouyer 	p = pp + NBPG;
    195  1.7.2.2  bouyer 	pp = p;
    196  1.7.2.2  bouyer 
    197  1.7.2.2  bouyer 	sc->sc_nrda = NBPG / RXPKT_SIZE(sc);
    198  1.7.2.2  bouyer 	sc->p_rda = (caddr_t) p;
    199  1.7.2.2  bouyer 	sc->v_rda = SONIC_GETDMA(p);
    200  1.7.2.2  bouyer 
    201  1.7.2.2  bouyer 	p = pp + NBPG;
    202  1.7.2.2  bouyer 
    203  1.7.2.2  bouyer 	for (i = 0; i < NRBA; i++) {
    204  1.7.2.2  bouyer 		sc->rbuf[i] = (caddr_t)p;
    205  1.7.2.2  bouyer 		p += NBPG;
    206  1.7.2.2  bouyer 	}
    207  1.7.2.2  bouyer 
    208  1.7.2.2  bouyer 	pp = p;
    209  1.7.2.2  bouyer 	for (i = 0; i < NTDA; i++) {
    210  1.7.2.2  bouyer 		struct mtd *mtdp = &sc->mtda[i];
    211  1.7.2.2  bouyer 
    212  1.7.2.2  bouyer 		mtdp->mtd_buf = p;
    213  1.7.2.2  bouyer 		mtdp->mtd_vbuf = SONIC_GETDMA(p);
    214  1.7.2.2  bouyer 		p += TXBSIZE;
    215  1.7.2.2  bouyer 	}
    216  1.7.2.2  bouyer 
    217  1.7.2.2  bouyer #ifdef SNDEBUG
    218  1.7.2.2  bouyer 	camdump(sc);
    219  1.7.2.2  bouyer #endif
    220  1.7.2.2  bouyer 	printf("%s: Ethernet address %s\n",
    221  1.7.2.2  bouyer 	    sc->sc_dev.dv_xname, ether_sprintf(lladdr));
    222  1.7.2.2  bouyer 
    223  1.7.2.2  bouyer #ifdef SNDEBUG
    224  1.7.2.2  bouyer 	printf("%s: buffers: rra=%p cda=%p rda=%p tda=%p\n",
    225  1.7.2.2  bouyer 	    sc->sc_dev.dv_xname, sc->p_rra[0], sc->p_cda,
    226  1.7.2.2  bouyer 	    sc->p_rda, sc->mtda[0].mtd_txp);
    227  1.7.2.2  bouyer #endif
    228  1.7.2.2  bouyer 
    229  1.7.2.2  bouyer 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    230  1.7.2.2  bouyer 	ifp->if_softc = sc;
    231  1.7.2.2  bouyer 	ifp->if_ioctl = snioctl;
    232  1.7.2.2  bouyer 	ifp->if_start = snstart;
    233  1.7.2.2  bouyer 	ifp->if_flags =
    234  1.7.2.2  bouyer 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    235  1.7.2.2  bouyer 	ifp->if_watchdog = snwatchdog;
    236  1.7.2.2  bouyer #if NBPFILTER > 0
    237  1.7.2.2  bouyer 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    238  1.7.2.2  bouyer #endif
    239  1.7.2.2  bouyer 	if_attach(ifp);
    240  1.7.2.2  bouyer 	ether_ifattach(ifp, lladdr);
    241  1.7.2.2  bouyer 
    242  1.7.2.2  bouyer 	return (0);
    243  1.7.2.2  bouyer }
    244  1.7.2.2  bouyer 
    245  1.7.2.2  bouyer static int
    246  1.7.2.2  bouyer snioctl(ifp, cmd, data)
    247  1.7.2.2  bouyer 	struct ifnet *ifp;
    248  1.7.2.2  bouyer 	u_long cmd;
    249  1.7.2.2  bouyer 	caddr_t data;
    250  1.7.2.2  bouyer {
    251  1.7.2.2  bouyer 	struct ifaddr *ifa;
    252  1.7.2.2  bouyer 	struct ifreq *ifr;
    253  1.7.2.2  bouyer 	struct sn_softc *sc = ifp->if_softc;
    254  1.7.2.2  bouyer 	int	s = splnet(), err = 0;
    255  1.7.2.2  bouyer 	int	temp;
    256  1.7.2.2  bouyer 
    257  1.7.2.2  bouyer 	switch (cmd) {
    258  1.7.2.2  bouyer 
    259  1.7.2.2  bouyer 	case SIOCSIFADDR:
    260  1.7.2.2  bouyer 		ifa = (struct ifaddr *)data;
    261  1.7.2.2  bouyer 		ifp->if_flags |= IFF_UP;
    262  1.7.2.2  bouyer 		switch (ifa->ifa_addr->sa_family) {
    263  1.7.2.2  bouyer #ifdef INET
    264  1.7.2.2  bouyer 		case AF_INET:
    265  1.7.2.2  bouyer 			(void)sninit(sc);
    266  1.7.2.2  bouyer 			arp_ifinit(ifp, ifa);
    267  1.7.2.2  bouyer 			break;
    268  1.7.2.2  bouyer #endif
    269  1.7.2.2  bouyer 		default:
    270  1.7.2.2  bouyer 			(void)sninit(sc);
    271  1.7.2.2  bouyer 			break;
    272  1.7.2.2  bouyer 		}
    273  1.7.2.2  bouyer 		break;
    274  1.7.2.2  bouyer 
    275  1.7.2.2  bouyer 	case SIOCSIFFLAGS:
    276  1.7.2.2  bouyer 		if ((ifp->if_flags & IFF_UP) == 0 &&
    277  1.7.2.2  bouyer 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    278  1.7.2.2  bouyer 			/*
    279  1.7.2.2  bouyer 			 * If interface is marked down and it is running,
    280  1.7.2.2  bouyer 			 * then stop it.
    281  1.7.2.2  bouyer 			 */
    282  1.7.2.2  bouyer 			snstop(sc);
    283  1.7.2.2  bouyer 			ifp->if_flags &= ~IFF_RUNNING;
    284  1.7.2.2  bouyer 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    285  1.7.2.2  bouyer 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    286  1.7.2.2  bouyer 			/*
    287  1.7.2.2  bouyer 			 * If interface is marked up and it is stopped,
    288  1.7.2.2  bouyer 			 * then start it.
    289  1.7.2.2  bouyer 			 */
    290  1.7.2.2  bouyer 			(void)sninit(sc);
    291  1.7.2.2  bouyer 		} else {
    292  1.7.2.2  bouyer 			/*
    293  1.7.2.2  bouyer 			 * reset the interface to pick up any other changes
    294  1.7.2.2  bouyer 			 * in flags
    295  1.7.2.2  bouyer 			 */
    296  1.7.2.2  bouyer 			temp = ifp->if_flags & IFF_UP;
    297  1.7.2.2  bouyer 			snreset(sc);
    298  1.7.2.2  bouyer 			ifp->if_flags |= temp;
    299  1.7.2.2  bouyer 			snstart(ifp);
    300  1.7.2.2  bouyer 		}
    301  1.7.2.2  bouyer 		break;
    302  1.7.2.2  bouyer 
    303  1.7.2.2  bouyer 	case SIOCADDMULTI:
    304  1.7.2.2  bouyer 	case SIOCDELMULTI:
    305  1.7.2.2  bouyer 		ifr = (struct ifreq *) data;
    306  1.7.2.2  bouyer 		if (cmd == SIOCADDMULTI)
    307  1.7.2.2  bouyer 			err = ether_addmulti(ifr, &sc->sc_ethercom);
    308  1.7.2.2  bouyer 		else
    309  1.7.2.2  bouyer 			err = ether_delmulti(ifr, &sc->sc_ethercom);
    310  1.7.2.2  bouyer 
    311  1.7.2.2  bouyer 		if (err == ENETRESET) {
    312  1.7.2.2  bouyer 			/*
    313  1.7.2.2  bouyer 			 * Multicast list has changed; set the hardware
    314  1.7.2.2  bouyer 			 * filter accordingly. But remember UP flag!
    315  1.7.2.2  bouyer 			 */
    316  1.7.2.2  bouyer 			temp = ifp->if_flags & IFF_UP;
    317  1.7.2.2  bouyer 			snreset(sc);
    318  1.7.2.2  bouyer 			ifp->if_flags |= temp;
    319  1.7.2.2  bouyer 			err = 0;
    320  1.7.2.2  bouyer 		}
    321  1.7.2.2  bouyer 		break;
    322  1.7.2.2  bouyer 	default:
    323  1.7.2.2  bouyer 		err = EINVAL;
    324  1.7.2.2  bouyer 	}
    325  1.7.2.2  bouyer 	splx(s);
    326  1.7.2.2  bouyer 	return (err);
    327  1.7.2.2  bouyer }
    328  1.7.2.2  bouyer 
    329  1.7.2.2  bouyer /*
    330  1.7.2.2  bouyer  * Encapsulate a packet of type family for the local net.
    331  1.7.2.2  bouyer  */
    332  1.7.2.2  bouyer static void
    333  1.7.2.2  bouyer snstart(ifp)
    334  1.7.2.2  bouyer 	struct ifnet *ifp;
    335  1.7.2.2  bouyer {
    336  1.7.2.2  bouyer 	struct sn_softc	*sc = ifp->if_softc;
    337  1.7.2.2  bouyer 	struct mbuf	*m;
    338  1.7.2.2  bouyer 	int		mtd_next;
    339  1.7.2.2  bouyer 
    340  1.7.2.2  bouyer 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    341  1.7.2.2  bouyer 		return;
    342  1.7.2.2  bouyer 
    343  1.7.2.2  bouyer outloop:
    344  1.7.2.2  bouyer 	/* Check for room in the xmit buffer. */
    345  1.7.2.2  bouyer 	if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
    346  1.7.2.2  bouyer 		mtd_next = 0;
    347  1.7.2.2  bouyer 
    348  1.7.2.2  bouyer 	if (mtd_next == sc->mtd_hw) {
    349  1.7.2.2  bouyer 		ifp->if_flags |= IFF_OACTIVE;
    350  1.7.2.2  bouyer 		return;
    351  1.7.2.2  bouyer 	}
    352  1.7.2.2  bouyer 
    353  1.7.2.2  bouyer 	IF_DEQUEUE(&ifp->if_snd, m);
    354  1.7.2.2  bouyer 	if (m == 0)
    355  1.7.2.2  bouyer 		return;
    356  1.7.2.2  bouyer 
    357  1.7.2.2  bouyer 	/* We need the header for m_pkthdr.len. */
    358  1.7.2.2  bouyer 	if ((m->m_flags & M_PKTHDR) == 0)
    359  1.7.2.2  bouyer 		panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname);
    360  1.7.2.2  bouyer 
    361  1.7.2.2  bouyer #if NBPFILTER > 0
    362  1.7.2.2  bouyer 	/*
    363  1.7.2.2  bouyer 	 * If bpf is listening on this interface, let it
    364  1.7.2.2  bouyer 	 * see the packet before we commit it to the wire.
    365  1.7.2.2  bouyer 	 */
    366  1.7.2.2  bouyer 	if (ifp->if_bpf)
    367  1.7.2.2  bouyer 		bpf_mtap(ifp->if_bpf, m);
    368  1.7.2.2  bouyer #endif
    369  1.7.2.2  bouyer 
    370  1.7.2.2  bouyer 	/*
    371  1.7.2.2  bouyer 	 * If there is nothing in the o/p queue, and there is room in
    372  1.7.2.2  bouyer 	 * the Tx ring, then send the packet directly.  Otherwise append
    373  1.7.2.2  bouyer 	 * it to the o/p queue.
    374  1.7.2.2  bouyer 	 */
    375  1.7.2.2  bouyer 	if ((sonicput(sc, m, mtd_next)) == 0) {
    376  1.7.2.2  bouyer 		IF_PREPEND(&ifp->if_snd, m);
    377  1.7.2.2  bouyer 		return;
    378  1.7.2.2  bouyer 	}
    379  1.7.2.2  bouyer 
    380  1.7.2.2  bouyer 	sc->mtd_prev = sc->mtd_free;
    381  1.7.2.2  bouyer 	sc->mtd_free = mtd_next;
    382  1.7.2.2  bouyer 
    383  1.7.2.2  bouyer 	ifp->if_opackets++;		/* # of pkts */
    384  1.7.2.2  bouyer 
    385  1.7.2.2  bouyer 	/* Jump back for possibly more punishment. */
    386  1.7.2.2  bouyer 	goto outloop;
    387  1.7.2.2  bouyer }
    388  1.7.2.2  bouyer 
    389  1.7.2.2  bouyer /*
    390  1.7.2.2  bouyer  * reset and restart the SONIC.  Called in case of fatal
    391  1.7.2.2  bouyer  * hardware/software errors.
    392  1.7.2.2  bouyer  */
    393  1.7.2.2  bouyer static void
    394  1.7.2.2  bouyer snreset(sc)
    395  1.7.2.2  bouyer 	struct sn_softc *sc;
    396  1.7.2.2  bouyer {
    397  1.7.2.2  bouyer 	snstop(sc);
    398  1.7.2.2  bouyer 	sninit(sc);
    399  1.7.2.2  bouyer }
    400  1.7.2.2  bouyer 
    401  1.7.2.2  bouyer static int
    402  1.7.2.2  bouyer sninit(sc)
    403  1.7.2.2  bouyer 	struct sn_softc *sc;
    404  1.7.2.2  bouyer {
    405  1.7.2.2  bouyer 	u_long	s_rcr;
    406  1.7.2.2  bouyer 	int	s;
    407  1.7.2.2  bouyer 
    408  1.7.2.2  bouyer 	if (sc->sc_if.if_flags & IFF_RUNNING)
    409  1.7.2.2  bouyer 		/* already running */
    410  1.7.2.2  bouyer 		return (0);
    411  1.7.2.2  bouyer 
    412  1.7.2.2  bouyer 	s = splnet();
    413  1.7.2.2  bouyer 
    414  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RST);	/* DCR only accessable in reset mode! */
    415  1.7.2.2  bouyer 
    416  1.7.2.2  bouyer 	/* config it */
    417  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_DCR, (sc->snr_dcr |
    418  1.7.2.2  bouyer 		(sc->bitmode ? DCR_DW32 : DCR_DW16)));
    419  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
    420  1.7.2.2  bouyer 
    421  1.7.2.2  bouyer 	s_rcr = RCR_BRD | RCR_LBNONE;
    422  1.7.2.2  bouyer 	if (sc->sc_if.if_flags & IFF_PROMISC)
    423  1.7.2.2  bouyer 		s_rcr |= RCR_PRO;
    424  1.7.2.2  bouyer 	if (sc->sc_if.if_flags & IFF_ALLMULTI)
    425  1.7.2.2  bouyer 		s_rcr |= RCR_AMC;
    426  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_RCR, s_rcr);
    427  1.7.2.2  bouyer 
    428  1.7.2.2  bouyer #if 0
    429  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
    430  1.7.2.2  bouyer #else
    431  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_IMR, IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN |
    432  1.7.2.2  bouyer 			     IMR_BREN  | IMR_HBLEN | IMR_RDEEN  | IMR_RBEEN |
    433  1.7.2.2  bouyer 			     IMR_RBAEEN | IMR_RFOEN);
    434  1.7.2.2  bouyer #endif
    435  1.7.2.2  bouyer 
    436  1.7.2.2  bouyer 	/* clear pending interrupts */
    437  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_ISR, ISR_ALL);
    438  1.7.2.2  bouyer 
    439  1.7.2.2  bouyer 	/* clear tally counters */
    440  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CRCT, -1);
    441  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_FAET, -1);
    442  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_MPT, -1);
    443  1.7.2.2  bouyer 
    444  1.7.2.2  bouyer 	initialise_tda(sc);
    445  1.7.2.2  bouyer 	initialise_rda(sc);
    446  1.7.2.2  bouyer 	initialise_rra(sc);
    447  1.7.2.2  bouyer 
    448  1.7.2.2  bouyer 	sn_md_init(sc);			/* MD initialization */
    449  1.7.2.2  bouyer 
    450  1.7.2.2  bouyer 	/* enable the chip */
    451  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, 0);
    452  1.7.2.2  bouyer 	wbflush();
    453  1.7.2.2  bouyer 
    454  1.7.2.2  bouyer 	/* program the CAM */
    455  1.7.2.2  bouyer 	camprogram(sc);
    456  1.7.2.2  bouyer 
    457  1.7.2.2  bouyer 	/* get it to read resource descriptors */
    458  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RRRA);
    459  1.7.2.2  bouyer 	wbflush();
    460  1.7.2.2  bouyer 	while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
    461  1.7.2.2  bouyer 		continue;
    462  1.7.2.2  bouyer 
    463  1.7.2.2  bouyer 	/* enable rx */
    464  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RXEN);
    465  1.7.2.2  bouyer 	wbflush();
    466  1.7.2.2  bouyer 
    467  1.7.2.2  bouyer 	/* flag interface as "running" */
    468  1.7.2.2  bouyer 	sc->sc_if.if_flags |= IFF_RUNNING;
    469  1.7.2.2  bouyer 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    470  1.7.2.2  bouyer 
    471  1.7.2.2  bouyer 	splx(s);
    472  1.7.2.2  bouyer 	return (0);
    473  1.7.2.2  bouyer }
    474  1.7.2.2  bouyer 
    475  1.7.2.2  bouyer /*
    476  1.7.2.2  bouyer  * close down an interface and free its buffers
    477  1.7.2.2  bouyer  * Called on final close of device, or if sninit() fails
    478  1.7.2.2  bouyer  * part way through.
    479  1.7.2.2  bouyer  */
    480  1.7.2.2  bouyer static int
    481  1.7.2.2  bouyer snstop(sc)
    482  1.7.2.2  bouyer 	struct sn_softc *sc;
    483  1.7.2.2  bouyer {
    484  1.7.2.2  bouyer 	struct mtd *mtd;
    485  1.7.2.2  bouyer 	int	s = splnet();
    486  1.7.2.2  bouyer 
    487  1.7.2.2  bouyer 	/* stick chip in reset */
    488  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RST);
    489  1.7.2.2  bouyer 	wbflush();
    490  1.7.2.2  bouyer 
    491  1.7.2.2  bouyer 	/* free all receive buffers (currently static so nothing to do) */
    492  1.7.2.2  bouyer 
    493  1.7.2.2  bouyer 	/* free all pending transmit mbufs */
    494  1.7.2.2  bouyer 	while (sc->mtd_hw != sc->mtd_free) {
    495  1.7.2.2  bouyer 		mtd = &sc->mtda[sc->mtd_hw];
    496  1.7.2.2  bouyer 		if (mtd->mtd_mbuf)
    497  1.7.2.2  bouyer 			m_freem(mtd->mtd_mbuf);
    498  1.7.2.2  bouyer 		if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
    499  1.7.2.2  bouyer 	}
    500  1.7.2.2  bouyer 
    501  1.7.2.2  bouyer 	sc->sc_if.if_timer = 0;
    502  1.7.2.2  bouyer 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    503  1.7.2.2  bouyer 
    504  1.7.2.2  bouyer 	splx(s);
    505  1.7.2.2  bouyer 	return (0);
    506  1.7.2.2  bouyer }
    507  1.7.2.2  bouyer 
    508  1.7.2.2  bouyer /*
    509  1.7.2.2  bouyer  * Called if any Tx packets remain unsent after 5 seconds,
    510  1.7.2.2  bouyer  * In all cases we just reset the chip, and any retransmission
    511  1.7.2.2  bouyer  * will be handled by higher level protocol timeouts.
    512  1.7.2.2  bouyer  */
    513  1.7.2.2  bouyer static void
    514  1.7.2.2  bouyer snwatchdog(ifp)
    515  1.7.2.2  bouyer 	struct ifnet *ifp;
    516  1.7.2.2  bouyer {
    517  1.7.2.2  bouyer 	struct sn_softc *sc = ifp->if_softc;
    518  1.7.2.2  bouyer 	struct mtd *mtd;
    519  1.7.2.2  bouyer 	int	temp;
    520  1.7.2.2  bouyer 
    521  1.7.2.2  bouyer 	if (sc->mtd_hw != sc->mtd_free) {
    522  1.7.2.2  bouyer 		/* something still pending for transmit */
    523  1.7.2.2  bouyer 		mtd = &sc->mtda[sc->mtd_hw];
    524  1.7.2.2  bouyer 		if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
    525  1.7.2.2  bouyer 			log(LOG_ERR, "%s: Tx - timeout\n",
    526  1.7.2.2  bouyer 			    sc->sc_dev.dv_xname);
    527  1.7.2.2  bouyer 		else
    528  1.7.2.2  bouyer 			log(LOG_ERR, "%s: Tx - lost interrupt\n",
    529  1.7.2.2  bouyer 			    sc->sc_dev.dv_xname);
    530  1.7.2.2  bouyer 		temp = ifp->if_flags & IFF_UP;
    531  1.7.2.2  bouyer 		snreset(sc);
    532  1.7.2.2  bouyer 		ifp->if_flags |= temp;
    533  1.7.2.2  bouyer 	}
    534  1.7.2.2  bouyer }
    535  1.7.2.2  bouyer 
    536  1.7.2.2  bouyer /*
    537  1.7.2.2  bouyer  * stuff packet into sonic (at splnet)
    538  1.7.2.2  bouyer  */
    539  1.7.2.2  bouyer static __inline__ u_int
    540  1.7.2.2  bouyer sonicput(sc, m0, mtd_next)
    541  1.7.2.2  bouyer 	struct sn_softc *sc;
    542  1.7.2.2  bouyer 	struct mbuf *m0;
    543  1.7.2.2  bouyer 	int mtd_next;
    544  1.7.2.2  bouyer {
    545  1.7.2.2  bouyer 	struct mtd *mtdp;
    546  1.7.2.2  bouyer 	struct mbuf *m;
    547  1.7.2.2  bouyer 	u_char	*buff;
    548  1.7.2.2  bouyer 	void	*txp;
    549  1.7.2.2  bouyer 	u_int	len = 0;
    550  1.7.2.2  bouyer 	u_int	totlen = 0;
    551  1.7.2.2  bouyer 
    552  1.7.2.2  bouyer #ifdef whyonearthwouldyoudothis
    553  1.7.2.2  bouyer 	if (NIC_GET(sc, SNR_CR) & CR_TXP)
    554  1.7.2.2  bouyer 		return (0);
    555  1.7.2.2  bouyer #endif
    556  1.7.2.2  bouyer 
    557  1.7.2.2  bouyer 	/* grab the replacement mtd */
    558  1.7.2.2  bouyer 	mtdp = &sc->mtda[sc->mtd_free];
    559  1.7.2.2  bouyer 
    560  1.7.2.2  bouyer 	buff = mtdp->mtd_buf;
    561  1.7.2.2  bouyer 
    562  1.7.2.2  bouyer 	/* this packet goes to mtdnext fill in the TDA */
    563  1.7.2.2  bouyer 	mtdp->mtd_mbuf = m0;
    564  1.7.2.2  bouyer 	txp = mtdp->mtd_txp;
    565  1.7.2.2  bouyer 
    566  1.7.2.2  bouyer 	/* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
    567  1.7.2.2  bouyer 	if (sc->mtd_pint == 0) {
    568  1.7.2.2  bouyer 		sc->mtd_pint = NTDA/2;
    569  1.7.2.2  bouyer 		SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT);
    570  1.7.2.2  bouyer 	} else {
    571  1.7.2.2  bouyer 		sc->mtd_pint--;
    572  1.7.2.2  bouyer 		SWO(sc->bitmode, txp, TXP_CONFIG, 0);
    573  1.7.2.2  bouyer 	}
    574  1.7.2.2  bouyer 
    575  1.7.2.2  bouyer 	for (m = m0; m; m = m->m_next) {
    576  1.7.2.2  bouyer 		u_char *data = mtod(m, u_char *);
    577  1.7.2.2  bouyer 		len = m->m_len;
    578  1.7.2.2  bouyer 		totlen += len;
    579  1.7.2.2  bouyer 		bcopy(data, buff, len);
    580  1.7.2.2  bouyer 		buff += len;
    581  1.7.2.2  bouyer 	}
    582  1.7.2.2  bouyer 	if (totlen >= TXBSIZE) {
    583  1.7.2.2  bouyer 		panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname);
    584  1.7.2.2  bouyer 	}
    585  1.7.2.2  bouyer 
    586  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
    587  1.7.2.2  bouyer 	    LOWER(mtdp->mtd_vbuf));
    588  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
    589  1.7.2.2  bouyer 	    UPPER(mtdp->mtd_vbuf));
    590  1.7.2.2  bouyer 
    591  1.7.2.2  bouyer 	if (totlen < ETHERMIN + ETHER_HDR_LEN) {
    592  1.7.2.2  bouyer 		int pad = ETHERMIN + ETHER_HDR_LEN - totlen;
    593  1.7.2.2  bouyer 		bzero(mtdp->mtd_buf + totlen, pad);
    594  1.7.2.2  bouyer 		totlen = ETHERMIN + ETHER_HDR_LEN;
    595  1.7.2.2  bouyer 	}
    596  1.7.2.2  bouyer 
    597  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
    598  1.7.2.2  bouyer 	    totlen);
    599  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
    600  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
    601  1.7.2.2  bouyer 
    602  1.7.2.2  bouyer 	/* link onto the next mtd that will be used */
    603  1.7.2.2  bouyer 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
    604  1.7.2.2  bouyer 	    LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
    605  1.7.2.2  bouyer 
    606  1.7.2.2  bouyer 	/*
    607  1.7.2.2  bouyer 	 * The previous txp.tlink currently contains a pointer to
    608  1.7.2.2  bouyer 	 * our txp | EOL. Want to clear the EOL, so write our
    609  1.7.2.2  bouyer 	 * pointer to the previous txp.
    610  1.7.2.2  bouyer 	 */
    611  1.7.2.2  bouyer 	SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
    612  1.7.2.2  bouyer 	    LOWER(mtdp->mtd_vtxp));
    613  1.7.2.2  bouyer 
    614  1.7.2.2  bouyer 	/* make sure chip is running */
    615  1.7.2.2  bouyer 	wbflush();
    616  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_TXP);
    617  1.7.2.2  bouyer 	wbflush();
    618  1.7.2.2  bouyer 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    619  1.7.2.2  bouyer 
    620  1.7.2.2  bouyer 	return (totlen);
    621  1.7.2.2  bouyer }
    622  1.7.2.2  bouyer 
    623  1.7.2.2  bouyer /*
    624  1.7.2.2  bouyer  * These are called from sonicioctl() when /etc/ifconfig is run to set
    625  1.7.2.2  bouyer  * the address or switch the i/f on.
    626  1.7.2.2  bouyer  */
    627  1.7.2.2  bouyer /*
    628  1.7.2.2  bouyer  * CAM support
    629  1.7.2.2  bouyer  */
    630  1.7.2.2  bouyer static void
    631  1.7.2.2  bouyer caminitialise(sc)
    632  1.7.2.2  bouyer 	struct sn_softc *sc;
    633  1.7.2.2  bouyer {
    634  1.7.2.2  bouyer 	void	*p_cda = sc->p_cda;
    635  1.7.2.2  bouyer 	int	i;
    636  1.7.2.2  bouyer 	int	camoffset;
    637  1.7.2.2  bouyer 
    638  1.7.2.2  bouyer 	for (i = 0; i < MAXCAM; i++) {
    639  1.7.2.2  bouyer 		camoffset = i * CDA_CAMDESC;
    640  1.7.2.2  bouyer 		SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
    641  1.7.2.2  bouyer 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
    642  1.7.2.2  bouyer 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
    643  1.7.2.2  bouyer 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
    644  1.7.2.2  bouyer 	}
    645  1.7.2.2  bouyer 	SWO(bitmode, p_cda, CDA_ENABLE, 0);
    646  1.7.2.2  bouyer }
    647  1.7.2.2  bouyer 
    648  1.7.2.2  bouyer static void
    649  1.7.2.2  bouyer camentry(sc, entry, ea)
    650  1.7.2.2  bouyer 	int entry;
    651  1.7.2.2  bouyer 	u_char *ea;
    652  1.7.2.2  bouyer 	struct sn_softc *sc;
    653  1.7.2.2  bouyer {
    654  1.7.2.2  bouyer 	void	*p_cda = sc->p_cda;
    655  1.7.2.2  bouyer 	int	camoffset = entry * CDA_CAMDESC;
    656  1.7.2.2  bouyer 
    657  1.7.2.2  bouyer 	SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
    658  1.7.2.2  bouyer 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
    659  1.7.2.2  bouyer 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
    660  1.7.2.2  bouyer 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
    661  1.7.2.2  bouyer 	SWO(bitmode, p_cda, CDA_ENABLE,
    662  1.7.2.2  bouyer 	    (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
    663  1.7.2.2  bouyer }
    664  1.7.2.2  bouyer 
    665  1.7.2.2  bouyer static void
    666  1.7.2.2  bouyer camprogram(sc)
    667  1.7.2.2  bouyer 	struct sn_softc *sc;
    668  1.7.2.2  bouyer {
    669  1.7.2.2  bouyer 	struct ether_multistep step;
    670  1.7.2.2  bouyer 	struct ether_multi *enm;
    671  1.7.2.2  bouyer 	struct ifnet *ifp;
    672  1.7.2.2  bouyer 	int	timeout;
    673  1.7.2.2  bouyer 	int	mcount = 0;
    674  1.7.2.2  bouyer 
    675  1.7.2.2  bouyer 	caminitialise(sc);
    676  1.7.2.2  bouyer 
    677  1.7.2.2  bouyer 	ifp = &sc->sc_if;
    678  1.7.2.2  bouyer 
    679  1.7.2.2  bouyer 	/* Always load our own address first. */
    680  1.7.2.2  bouyer 	camentry (sc, mcount, LLADDR(ifp->if_sadl));
    681  1.7.2.2  bouyer 	mcount++;
    682  1.7.2.2  bouyer 
    683  1.7.2.2  bouyer 	/* Assume we won't need allmulti bit. */
    684  1.7.2.2  bouyer 	ifp->if_flags &= ~IFF_ALLMULTI;
    685  1.7.2.2  bouyer 
    686  1.7.2.2  bouyer 	/* Loop through multicast addresses */
    687  1.7.2.2  bouyer 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    688  1.7.2.2  bouyer 	while (enm != NULL) {
    689  1.7.2.2  bouyer 		if (mcount == MAXCAM) {
    690  1.7.2.2  bouyer 			 ifp->if_flags |= IFF_ALLMULTI;
    691  1.7.2.2  bouyer 			 break;
    692  1.7.2.2  bouyer 		}
    693  1.7.2.2  bouyer 
    694  1.7.2.2  bouyer 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
    695  1.7.2.2  bouyer 		    sizeof(enm->enm_addrlo)) != 0) {
    696  1.7.2.2  bouyer 			/*
    697  1.7.2.2  bouyer 			 * SONIC's CAM is programmed with specific
    698  1.7.2.2  bouyer 			 * addresses. It has no way to specify a range.
    699  1.7.2.2  bouyer 			 * (Well, thats not exactly true. If the
    700  1.7.2.2  bouyer 			 * range is small one could program each addr
    701  1.7.2.2  bouyer 			 * within the range as a seperate CAM entry)
    702  1.7.2.2  bouyer 			 */
    703  1.7.2.2  bouyer 			ifp->if_flags |= IFF_ALLMULTI;
    704  1.7.2.2  bouyer 			break;
    705  1.7.2.2  bouyer 		}
    706  1.7.2.2  bouyer 
    707  1.7.2.2  bouyer 		/* program the CAM with the specified entry */
    708  1.7.2.2  bouyer 		camentry(sc, mcount, enm->enm_addrlo);
    709  1.7.2.2  bouyer 		mcount++;
    710  1.7.2.2  bouyer 
    711  1.7.2.2  bouyer 		ETHER_NEXT_MULTI(step, enm);
    712  1.7.2.2  bouyer 	}
    713  1.7.2.2  bouyer 
    714  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda));
    715  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CDC, MAXCAM);
    716  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_LCAM);
    717  1.7.2.2  bouyer 	wbflush();
    718  1.7.2.2  bouyer 
    719  1.7.2.2  bouyer 	timeout = 10000;
    720  1.7.2.2  bouyer 	while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--)
    721  1.7.2.2  bouyer 		delay(10);
    722  1.7.2.2  bouyer 	if (timeout == 0) {
    723  1.7.2.2  bouyer 		/* XXX */
    724  1.7.2.2  bouyer 		panic("%s: CAM initialisation failed\n", sc->sc_dev.dv_xname);
    725  1.7.2.2  bouyer 	}
    726  1.7.2.2  bouyer 	timeout = 10000;
    727  1.7.2.2  bouyer 	while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--)
    728  1.7.2.2  bouyer 		delay(10);
    729  1.7.2.2  bouyer 
    730  1.7.2.2  bouyer 	if (NIC_GET(sc, SNR_ISR) & ISR_LCD)
    731  1.7.2.2  bouyer 		NIC_PUT(sc, SNR_ISR, ISR_LCD);
    732  1.7.2.2  bouyer 	else
    733  1.7.2.2  bouyer 		printf("%s: CAM initialisation without interrupt\n",
    734  1.7.2.2  bouyer 		    sc->sc_dev.dv_xname);
    735  1.7.2.2  bouyer }
    736  1.7.2.2  bouyer 
    737  1.7.2.2  bouyer #ifdef SNDEBUG
    738  1.7.2.2  bouyer static void
    739  1.7.2.2  bouyer camdump(sc)
    740  1.7.2.2  bouyer 	struct sn_softc *sc;
    741  1.7.2.2  bouyer {
    742  1.7.2.2  bouyer 	int	i;
    743  1.7.2.2  bouyer 
    744  1.7.2.2  bouyer 	printf("CAM entries:\n");
    745  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, CR_RST);
    746  1.7.2.2  bouyer 	wbflush();
    747  1.7.2.2  bouyer 
    748  1.7.2.2  bouyer 	for (i = 0; i < 16; i++) {
    749  1.7.2.2  bouyer 		ushort  ap2, ap1, ap0;
    750  1.7.2.2  bouyer 		NIC_PUT(sc, SNR_CEP, i);
    751  1.7.2.2  bouyer 		wbflush();
    752  1.7.2.2  bouyer 		ap2 = NIC_GET(sc, SNR_CAP2);
    753  1.7.2.2  bouyer 		ap1 = NIC_GET(sc, SNR_CAP1);
    754  1.7.2.2  bouyer 		ap0 = NIC_GET(sc, SNR_CAP0);
    755  1.7.2.2  bouyer 		printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
    756  1.7.2.2  bouyer 	}
    757  1.7.2.2  bouyer 	printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP));
    758  1.7.2.2  bouyer 
    759  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CR, 0);
    760  1.7.2.2  bouyer 	wbflush();
    761  1.7.2.2  bouyer }
    762  1.7.2.2  bouyer #endif
    763  1.7.2.2  bouyer 
    764  1.7.2.2  bouyer static void
    765  1.7.2.2  bouyer initialise_tda(sc)
    766  1.7.2.2  bouyer 	struct sn_softc *sc;
    767  1.7.2.2  bouyer {
    768  1.7.2.2  bouyer 	struct mtd *mtd;
    769  1.7.2.2  bouyer 	int	i;
    770  1.7.2.2  bouyer 
    771  1.7.2.2  bouyer 	for (i = 0; i < NTDA; i++) {
    772  1.7.2.2  bouyer 		mtd = &sc->mtda[i];
    773  1.7.2.2  bouyer 		mtd->mtd_mbuf = 0;
    774  1.7.2.2  bouyer 	}
    775  1.7.2.2  bouyer 
    776  1.7.2.2  bouyer 	sc->mtd_hw = 0;
    777  1.7.2.2  bouyer 	sc->mtd_prev = NTDA - 1;
    778  1.7.2.2  bouyer 	sc->mtd_free = 0;
    779  1.7.2.2  bouyer 	sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
    780  1.7.2.2  bouyer 	sc->mtd_pint = NTDA/2;
    781  1.7.2.2  bouyer 
    782  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
    783  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
    784  1.7.2.2  bouyer }
    785  1.7.2.2  bouyer 
    786  1.7.2.2  bouyer static void
    787  1.7.2.2  bouyer initialise_rda(sc)
    788  1.7.2.2  bouyer 	struct sn_softc *sc;
    789  1.7.2.2  bouyer {
    790  1.7.2.2  bouyer 	int		i;
    791  1.7.2.2  bouyer 	caddr_t		p_rda = 0;
    792  1.7.2.2  bouyer 	u_int32_t	v_rda = 0;
    793  1.7.2.2  bouyer 
    794  1.7.2.2  bouyer 	/* link the RDA's together into a circular list */
    795  1.7.2.2  bouyer 	for (i = 0; i < (sc->sc_nrda - 1); i++) {
    796  1.7.2.2  bouyer 		p_rda = sc->p_rda + (i * RXPKT_SIZE(sc));
    797  1.7.2.2  bouyer 		v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
    798  1.7.2.2  bouyer 		SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda));
    799  1.7.2.2  bouyer 		SWO(bitmode, p_rda, RXPKT_INUSE, 1);
    800  1.7.2.2  bouyer 	}
    801  1.7.2.2  bouyer 	p_rda = sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
    802  1.7.2.2  bouyer 	SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
    803  1.7.2.2  bouyer 	SWO(bitmode, p_rda, RXPKT_INUSE, 1);
    804  1.7.2.2  bouyer 
    805  1.7.2.2  bouyer 	/* mark end of receive descriptor list */
    806  1.7.2.2  bouyer 	sc->sc_rdamark = sc->sc_nrda - 1;
    807  1.7.2.2  bouyer 
    808  1.7.2.2  bouyer 	sc->sc_rxmark = 0;
    809  1.7.2.2  bouyer 
    810  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda));
    811  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda));
    812  1.7.2.2  bouyer 	wbflush();
    813  1.7.2.2  bouyer }
    814  1.7.2.2  bouyer 
    815  1.7.2.2  bouyer static void
    816  1.7.2.2  bouyer initialise_rra(sc)
    817  1.7.2.2  bouyer 	struct sn_softc *sc;
    818  1.7.2.2  bouyer {
    819  1.7.2.2  bouyer 	int	i;
    820  1.7.2.2  bouyer 	u_int	v;
    821  1.7.2.2  bouyer 	int	bitmode = sc->bitmode;
    822  1.7.2.2  bouyer 
    823  1.7.2.2  bouyer 	if (bitmode)
    824  1.7.2.2  bouyer 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
    825  1.7.2.2  bouyer 	else
    826  1.7.2.2  bouyer 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
    827  1.7.2.2  bouyer 
    828  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
    829  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
    830  1.7.2.2  bouyer 	/* rea must point just past the end of the rra space */
    831  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
    832  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
    833  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_RSC, 0);
    834  1.7.2.2  bouyer 
    835  1.7.2.2  bouyer 	/* fill up SOME of the rra with buffers */
    836  1.7.2.2  bouyer 	for (i = 0; i < NRBA; i++) {
    837  1.7.2.2  bouyer 		v = SONIC_GETDMA(sc->rbuf[i]);
    838  1.7.2.2  bouyer 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
    839  1.7.2.2  bouyer 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
    840  1.7.2.2  bouyer 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
    841  1.7.2.2  bouyer 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
    842  1.7.2.2  bouyer 	}
    843  1.7.2.2  bouyer 	sc->sc_rramark = NRBA;
    844  1.7.2.2  bouyer 	NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
    845  1.7.2.2  bouyer 	wbflush();
    846  1.7.2.2  bouyer }
    847  1.7.2.2  bouyer 
    848  1.7.2.2  bouyer int
    849  1.7.2.2  bouyer snintr(arg)
    850  1.7.2.2  bouyer 	void	*arg;
    851  1.7.2.2  bouyer {
    852  1.7.2.2  bouyer 	struct sn_softc *sc = (struct sn_softc *)arg;
    853  1.7.2.2  bouyer 	int handled = 0;
    854  1.7.2.2  bouyer 	int	isr;
    855  1.7.2.2  bouyer 
    856  1.7.2.2  bouyer 	while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
    857  1.7.2.2  bouyer 		/* scrub the interrupts that we are going to service */
    858  1.7.2.2  bouyer 		NIC_PUT(sc, SNR_ISR, isr);
    859  1.7.2.2  bouyer 		handled = 1;
    860  1.7.2.2  bouyer 		wbflush();
    861  1.7.2.2  bouyer 
    862  1.7.2.2  bouyer 		if (isr & (ISR_BR | ISR_LCD | ISR_TC))
    863  1.7.2.2  bouyer 			printf("%s: unexpected interrupt status 0x%x\n",
    864  1.7.2.2  bouyer 			    sc->sc_dev.dv_xname, isr);
    865  1.7.2.2  bouyer 
    866  1.7.2.2  bouyer 		if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
    867  1.7.2.2  bouyer 			sonictxint(sc);
    868  1.7.2.2  bouyer 
    869  1.7.2.2  bouyer 		if (isr & ISR_PKTRX)
    870  1.7.2.2  bouyer 			sonicrxint(sc);
    871  1.7.2.2  bouyer 
    872  1.7.2.2  bouyer 		if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
    873  1.7.2.2  bouyer 			if (isr & ISR_HBL)
    874  1.7.2.2  bouyer 				/*
    875  1.7.2.2  bouyer 				 * The repeater is not providing a heartbeat.
    876  1.7.2.2  bouyer 				 * In itself this isn't harmful, lots of the
    877  1.7.2.2  bouyer 				 * cheap repeater hubs don't supply a heartbeat.
    878  1.7.2.2  bouyer 				 * So ignore the lack of heartbeat. Its only
    879  1.7.2.2  bouyer 				 * if we can't detect a carrier that we have a
    880  1.7.2.2  bouyer 				 * problem.
    881  1.7.2.2  bouyer 				 */
    882  1.7.2.2  bouyer 				;
    883  1.7.2.2  bouyer 			if (isr & ISR_RDE)
    884  1.7.2.2  bouyer 				printf("%s: receive descriptors exhausted\n",
    885  1.7.2.2  bouyer 				    sc->sc_dev.dv_xname);
    886  1.7.2.2  bouyer 			if (isr & ISR_RBE)
    887  1.7.2.2  bouyer 				printf("%s: receive buffers exhausted\n",
    888  1.7.2.2  bouyer 				    sc->sc_dev.dv_xname);
    889  1.7.2.2  bouyer 			if (isr & ISR_RBAE)
    890  1.7.2.2  bouyer 				printf("%s: receive buffer area exhausted\n",
    891  1.7.2.2  bouyer 				    sc->sc_dev.dv_xname);
    892  1.7.2.2  bouyer 			if (isr & ISR_RFO)
    893  1.7.2.2  bouyer 				printf("%s: receive FIFO overrun\n",
    894  1.7.2.2  bouyer 				    sc->sc_dev.dv_xname);
    895  1.7.2.2  bouyer 		}
    896  1.7.2.2  bouyer 		if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
    897  1.7.2.2  bouyer #ifdef notdef
    898  1.7.2.2  bouyer 			if (isr & ISR_CRC)
    899  1.7.2.2  bouyer 				sc->sc_crctally++;
    900  1.7.2.2  bouyer 			if (isr & ISR_FAE)
    901  1.7.2.2  bouyer 				sc->sc_faetally++;
    902  1.7.2.2  bouyer 			if (isr & ISR_MP)
    903  1.7.2.2  bouyer 				sc->sc_mptally++;
    904  1.7.2.2  bouyer #endif
    905  1.7.2.2  bouyer 		}
    906  1.7.2.2  bouyer 		snstart(&sc->sc_if);
    907  1.7.2.2  bouyer 	}
    908  1.7.2.2  bouyer 	return handled;
    909  1.7.2.2  bouyer }
    910  1.7.2.2  bouyer 
    911  1.7.2.2  bouyer /*
    912  1.7.2.2  bouyer  * Transmit interrupt routine
    913  1.7.2.2  bouyer  */
    914  1.7.2.2  bouyer static void
    915  1.7.2.2  bouyer sonictxint(sc)
    916  1.7.2.2  bouyer 	struct sn_softc *sc;
    917  1.7.2.2  bouyer {
    918  1.7.2.2  bouyer 	struct mtd	*mtd;
    919  1.7.2.2  bouyer 	void		*txp;
    920  1.7.2.2  bouyer 	unsigned short	txp_status;
    921  1.7.2.2  bouyer 	int		mtd_hw;
    922  1.7.2.2  bouyer 	struct ifnet	*ifp = &sc->sc_if;
    923  1.7.2.2  bouyer 
    924  1.7.2.2  bouyer 	mtd_hw = sc->mtd_hw;
    925  1.7.2.2  bouyer 
    926  1.7.2.2  bouyer 	if (mtd_hw == sc->mtd_free)
    927  1.7.2.2  bouyer 		return;
    928  1.7.2.2  bouyer 
    929  1.7.2.2  bouyer 	while (mtd_hw != sc->mtd_free) {
    930  1.7.2.2  bouyer 		mtd = &sc->mtda[mtd_hw];
    931  1.7.2.2  bouyer 
    932  1.7.2.2  bouyer 		txp = mtd->mtd_txp;
    933  1.7.2.2  bouyer 
    934  1.7.2.2  bouyer 		if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) {
    935  1.7.2.2  bouyer 			break; /* it hasn't really gone yet */
    936  1.7.2.2  bouyer 		}
    937  1.7.2.2  bouyer 
    938  1.7.2.2  bouyer #ifdef SNDEBUG
    939  1.7.2.2  bouyer 		{
    940  1.7.2.2  bouyer 			struct ether_header *eh;
    941  1.7.2.2  bouyer 
    942  1.7.2.2  bouyer 			eh = (struct ether_header *) mtd->mtd_buf;
    943  1.7.2.2  bouyer 			printf("%s: xmit status=0x%x len=%d type=0x%x from %s",
    944  1.7.2.2  bouyer 			    sc->sc_dev.dv_xname,
    945  1.7.2.2  bouyer 			    SRO(sc->bitmode, txp, TXP_STATUS),
    946  1.7.2.2  bouyer 			    SRO(sc->bitmode, txp, TXP_PKTSIZE),
    947  1.7.2.2  bouyer 			    htons(eh->ether_type),
    948  1.7.2.2  bouyer 			    ether_sprintf(eh->ether_shost));
    949  1.7.2.2  bouyer 			printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
    950  1.7.2.2  bouyer 		}
    951  1.7.2.2  bouyer #endif /* SNDEBUG */
    952  1.7.2.2  bouyer 
    953  1.7.2.2  bouyer 		ifp->if_flags &= ~IFF_OACTIVE;
    954  1.7.2.2  bouyer 
    955  1.7.2.2  bouyer 		if (mtd->mtd_mbuf != 0) {
    956  1.7.2.2  bouyer 			m_freem(mtd->mtd_mbuf);
    957  1.7.2.2  bouyer 			mtd->mtd_mbuf = 0;
    958  1.7.2.2  bouyer 		}
    959  1.7.2.2  bouyer 		if (++mtd_hw == NTDA) mtd_hw = 0;
    960  1.7.2.2  bouyer 
    961  1.7.2.2  bouyer 		txp_status = SRO(sc->bitmode, txp, TXP_STATUS);
    962  1.7.2.2  bouyer 
    963  1.7.2.2  bouyer 		ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
    964  1.7.2.2  bouyer 			((txp_status & TCR_NC) >> 12);
    965  1.7.2.2  bouyer 
    966  1.7.2.2  bouyer 		if ((txp_status & TCR_PTX) == 0) {
    967  1.7.2.2  bouyer 			ifp->if_oerrors++;
    968  1.7.2.2  bouyer 			printf("%s: Tx packet status=0x%x\n",
    969  1.7.2.2  bouyer 			    sc->sc_dev.dv_xname, txp_status);
    970  1.7.2.2  bouyer 
    971  1.7.2.2  bouyer 			/* XXX - DG This looks bogus */
    972  1.7.2.2  bouyer 			if (mtd_hw != sc->mtd_free) {
    973  1.7.2.2  bouyer 				printf("resubmitting remaining packets\n");
    974  1.7.2.2  bouyer 				mtd = &sc->mtda[mtd_hw];
    975  1.7.2.2  bouyer 				NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
    976  1.7.2.2  bouyer 				NIC_PUT(sc, SNR_CR, CR_TXP);
    977  1.7.2.2  bouyer 				wbflush();
    978  1.7.2.2  bouyer 				break;
    979  1.7.2.2  bouyer 			}
    980  1.7.2.2  bouyer 		}
    981  1.7.2.2  bouyer 	}
    982  1.7.2.2  bouyer 
    983  1.7.2.2  bouyer 	sc->mtd_hw = mtd_hw;
    984  1.7.2.2  bouyer 	return;
    985  1.7.2.2  bouyer }
    986  1.7.2.2  bouyer 
    987  1.7.2.2  bouyer /*
    988  1.7.2.2  bouyer  * Receive interrupt routine
    989  1.7.2.2  bouyer  */
    990  1.7.2.2  bouyer static void
    991  1.7.2.2  bouyer sonicrxint(sc)
    992  1.7.2.2  bouyer 	struct sn_softc *sc;
    993  1.7.2.2  bouyer {
    994  1.7.2.2  bouyer 	caddr_t	rda;
    995  1.7.2.2  bouyer 	int	orra;
    996  1.7.2.2  bouyer 	int	len;
    997  1.7.2.2  bouyer 	int	rramark;
    998  1.7.2.2  bouyer 	int	rdamark;
    999  1.7.2.2  bouyer 	u_int16_t rxpkt_ptr;
   1000  1.7.2.2  bouyer 
   1001  1.7.2.2  bouyer 	rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
   1002  1.7.2.2  bouyer 
   1003  1.7.2.2  bouyer 	while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
   1004  1.7.2.2  bouyer 		u_int status = SRO(bitmode, rda, RXPKT_STATUS);
   1005  1.7.2.2  bouyer 
   1006  1.7.2.2  bouyer 		orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
   1007  1.7.2.2  bouyer 		rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
   1008  1.7.2.2  bouyer 		len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE;
   1009  1.7.2.2  bouyer 		if (status & RCR_PRX) {
   1010  1.7.2.2  bouyer 			caddr_t pkt =
   1011  1.7.2.2  bouyer 			    sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
   1012  1.7.2.2  bouyer 			if (sonic_read(sc, pkt, len))
   1013  1.7.2.2  bouyer 				sc->sc_if.if_ipackets++;
   1014  1.7.2.2  bouyer 			else
   1015  1.7.2.2  bouyer 				sc->sc_if.if_ierrors++;
   1016  1.7.2.2  bouyer 		} else
   1017  1.7.2.2  bouyer 			sc->sc_if.if_ierrors++;
   1018  1.7.2.2  bouyer 
   1019  1.7.2.2  bouyer 		/*
   1020  1.7.2.2  bouyer 		 * give receive buffer area back to chip.
   1021  1.7.2.2  bouyer 		 *
   1022  1.7.2.2  bouyer 		 * If this was the last packet in the RRA, give the RRA to
   1023  1.7.2.2  bouyer 		 * the chip again.
   1024  1.7.2.2  bouyer 		 * If sonic read didnt copy it out then we would have to
   1025  1.7.2.2  bouyer 		 * wait !!
   1026  1.7.2.2  bouyer 		 * (dont bother add it back in again straight away)
   1027  1.7.2.2  bouyer 		 *
   1028  1.7.2.2  bouyer 		 * Really, we're doing p_rra[rramark] = p_rra[orra] but
   1029  1.7.2.2  bouyer 		 * we have to use the macros because SONIC might be in
   1030  1.7.2.2  bouyer 		 * 16 or 32 bit mode.
   1031  1.7.2.2  bouyer 		 */
   1032  1.7.2.2  bouyer 		if (status & RCR_LPKT) {
   1033  1.7.2.2  bouyer 			void *tmp1, *tmp2;
   1034  1.7.2.2  bouyer 
   1035  1.7.2.2  bouyer 			rramark = sc->sc_rramark;
   1036  1.7.2.2  bouyer 			tmp1 = sc->p_rra[rramark];
   1037  1.7.2.2  bouyer 			tmp2 = sc->p_rra[orra];
   1038  1.7.2.2  bouyer 			SWO(bitmode, tmp1, RXRSRC_PTRLO,
   1039  1.7.2.2  bouyer 				SRO(bitmode, tmp2, RXRSRC_PTRLO));
   1040  1.7.2.2  bouyer 			SWO(bitmode, tmp1, RXRSRC_PTRHI,
   1041  1.7.2.2  bouyer 				SRO(bitmode, tmp2, RXRSRC_PTRHI));
   1042  1.7.2.2  bouyer 			SWO(bitmode, tmp1, RXRSRC_WCLO,
   1043  1.7.2.2  bouyer 				SRO(bitmode, tmp2, RXRSRC_WCLO));
   1044  1.7.2.2  bouyer 			SWO(bitmode, tmp1, RXRSRC_WCHI,
   1045  1.7.2.2  bouyer 				SRO(bitmode, tmp2, RXRSRC_WCHI));
   1046  1.7.2.2  bouyer 
   1047  1.7.2.2  bouyer 			/* zap old rra for fun */
   1048  1.7.2.2  bouyer 			SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
   1049  1.7.2.2  bouyer 			SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
   1050  1.7.2.2  bouyer 
   1051  1.7.2.2  bouyer 			sc->sc_rramark = (++rramark) & RRAMASK;
   1052  1.7.2.2  bouyer 			NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
   1053  1.7.2.2  bouyer 			wbflush();
   1054  1.7.2.2  bouyer 		}
   1055  1.7.2.2  bouyer 
   1056  1.7.2.2  bouyer 		/*
   1057  1.7.2.2  bouyer 		 * give receive descriptor back to chip simple
   1058  1.7.2.2  bouyer 		 * list is circular
   1059  1.7.2.2  bouyer 		 */
   1060  1.7.2.2  bouyer 		rdamark = sc->sc_rdamark;
   1061  1.7.2.2  bouyer 		SWO(bitmode, rda, RXPKT_INUSE, 1);
   1062  1.7.2.2  bouyer 		SWO(bitmode, rda, RXPKT_RLINK,
   1063  1.7.2.2  bouyer 			SRO(bitmode, rda, RXPKT_RLINK) | EOL);
   1064  1.7.2.2  bouyer 		SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
   1065  1.7.2.2  bouyer 			SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
   1066  1.7.2.2  bouyer 			RXPKT_RLINK) & ~EOL);
   1067  1.7.2.2  bouyer 		sc->sc_rdamark = sc->sc_rxmark;
   1068  1.7.2.2  bouyer 
   1069  1.7.2.2  bouyer 		if (++sc->sc_rxmark >= sc->sc_nrda)
   1070  1.7.2.2  bouyer 			sc->sc_rxmark = 0;
   1071  1.7.2.2  bouyer 		rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
   1072  1.7.2.2  bouyer 	}
   1073  1.7.2.2  bouyer }
   1074  1.7.2.2  bouyer 
   1075  1.7.2.2  bouyer /*
   1076  1.7.2.2  bouyer  * sonic_read -- pull packet off interface and forward to
   1077  1.7.2.2  bouyer  * appropriate protocol handler
   1078  1.7.2.2  bouyer  */
   1079  1.7.2.2  bouyer static __inline__ int
   1080  1.7.2.2  bouyer sonic_read(sc, pkt, len)
   1081  1.7.2.2  bouyer 	struct sn_softc *sc;
   1082  1.7.2.2  bouyer 	caddr_t pkt;
   1083  1.7.2.2  bouyer 	int len;
   1084  1.7.2.2  bouyer {
   1085  1.7.2.2  bouyer 	struct ifnet *ifp = &sc->sc_if;
   1086  1.7.2.2  bouyer 	struct mbuf *m;
   1087  1.7.2.2  bouyer 
   1088  1.7.2.2  bouyer #ifdef SNDEBUG
   1089  1.7.2.2  bouyer 	{
   1090  1.7.2.2  bouyer 		printf("%s: rcvd 0x%p len=%d type=0x%x from %s",
   1091  1.7.2.2  bouyer 		    sc->sc_dev.dv_xname, et, len, htons(et->ether_type),
   1092  1.7.2.2  bouyer 		    ether_sprintf(et->ether_shost));
   1093  1.7.2.2  bouyer 		printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
   1094  1.7.2.2  bouyer 	}
   1095  1.7.2.2  bouyer #endif /* SNDEBUG */
   1096  1.7.2.2  bouyer 
   1097  1.7.2.2  bouyer 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
   1098  1.7.2.2  bouyer 	    len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
   1099  1.7.2.2  bouyer 		printf("%s: invalid packet length %d bytes\n",
   1100  1.7.2.2  bouyer 		    sc->sc_dev.dv_xname, len);
   1101  1.7.2.2  bouyer 		return (0);
   1102  1.7.2.2  bouyer 	}
   1103  1.7.2.2  bouyer 
   1104  1.7.2.2  bouyer #if NBPFILTER > 0
   1105  1.7.2.2  bouyer 	/*
   1106  1.7.2.2  bouyer 	 * Check if there's a bpf filter listening on this interface.
   1107  1.7.2.2  bouyer 	 * If so, hand off the raw packet to enet, then discard things
   1108  1.7.2.2  bouyer 	 * not destined for us (but be sure to keep broadcast/multicast).
   1109  1.7.2.2  bouyer 	 */
   1110  1.7.2.2  bouyer 	if (ifp->if_bpf)
   1111  1.7.2.2  bouyer 		bpf_tap(ifp->if_bpf, pkt, len);
   1112  1.7.2.2  bouyer #endif
   1113  1.7.2.2  bouyer 	m = sonic_get(sc, pkt, len);
   1114  1.7.2.2  bouyer 	if (m == NULL)
   1115  1.7.2.2  bouyer 		return (0);
   1116  1.7.2.2  bouyer 	(*ifp->if_input)(ifp, m);
   1117  1.7.2.2  bouyer 	return (1);
   1118  1.7.2.2  bouyer }
   1119  1.7.2.2  bouyer 
   1120  1.7.2.2  bouyer /*
   1121  1.7.2.2  bouyer  * munge the received packet into an mbuf chain
   1122  1.7.2.2  bouyer  */
   1123  1.7.2.2  bouyer static __inline__ struct mbuf *
   1124  1.7.2.2  bouyer sonic_get(sc, pkt, datalen)
   1125  1.7.2.2  bouyer 	struct sn_softc *sc;
   1126  1.7.2.2  bouyer 	caddr_t pkt;
   1127  1.7.2.2  bouyer 	int datalen;
   1128  1.7.2.2  bouyer {
   1129  1.7.2.2  bouyer 	struct	mbuf *m, *top, **mp;
   1130  1.7.2.2  bouyer 	int	len;
   1131  1.7.2.2  bouyer 
   1132  1.7.2.2  bouyer 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1133  1.7.2.2  bouyer 	if (m == 0)
   1134  1.7.2.2  bouyer 		return (0);
   1135  1.7.2.2  bouyer 	m->m_pkthdr.rcvif = &sc->sc_if;
   1136  1.7.2.2  bouyer 	m->m_pkthdr.len = datalen;
   1137  1.7.2.2  bouyer 	len = MHLEN;
   1138  1.7.2.2  bouyer 	top = 0;
   1139  1.7.2.2  bouyer 	mp = &top;
   1140  1.7.2.2  bouyer 
   1141  1.7.2.2  bouyer 	while (datalen > 0) {
   1142  1.7.2.2  bouyer 		if (top) {
   1143  1.7.2.2  bouyer 			MGET(m, M_DONTWAIT, MT_DATA);
   1144  1.7.2.2  bouyer 			if (m == 0) {
   1145  1.7.2.2  bouyer 				m_freem(top);
   1146  1.7.2.2  bouyer 				return (0);
   1147  1.7.2.2  bouyer 			}
   1148  1.7.2.2  bouyer 			len = MLEN;
   1149  1.7.2.2  bouyer 		}
   1150  1.7.2.2  bouyer 		if (datalen >= MINCLSIZE) {
   1151  1.7.2.2  bouyer 			MCLGET(m, M_DONTWAIT);
   1152  1.7.2.2  bouyer 			if ((m->m_flags & M_EXT) == 0) {
   1153  1.7.2.2  bouyer 				if (top) m_freem(top);
   1154  1.7.2.2  bouyer 				return (0);
   1155  1.7.2.2  bouyer 			}
   1156  1.7.2.2  bouyer 			len = MCLBYTES;
   1157  1.7.2.2  bouyer 		}
   1158  1.7.2.2  bouyer 
   1159  1.7.2.2  bouyer 		if (mp == &top) {
   1160  1.7.2.2  bouyer 			caddr_t newdata = (caddr_t)
   1161  1.7.2.2  bouyer 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
   1162  1.7.2.2  bouyer 			    sizeof(struct ether_header);
   1163  1.7.2.2  bouyer 			len -= newdata - m->m_data;
   1164  1.7.2.2  bouyer 			m->m_data = newdata;
   1165  1.7.2.2  bouyer 		}
   1166  1.7.2.2  bouyer 
   1167  1.7.2.2  bouyer 		m->m_len = len = min(datalen, len);
   1168  1.7.2.2  bouyer 
   1169  1.7.2.2  bouyer 		bcopy(pkt, mtod(m, caddr_t), (unsigned) len);
   1170  1.7.2.2  bouyer 		pkt += len;
   1171  1.7.2.2  bouyer 		datalen -= len;
   1172  1.7.2.2  bouyer 		*mp = m;
   1173  1.7.2.2  bouyer 		mp = &m->m_next;
   1174  1.7.2.2  bouyer 	}
   1175  1.7.2.2  bouyer 
   1176  1.7.2.2  bouyer 	return (top);
   1177  1.7.2.2  bouyer }
   1178