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