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