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