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