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