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