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