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