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