Home | History | Annotate | Line # | Download | only in apbus
if_sn.c revision 1.45
      1 /*	$NetBSD: if_sn.c,v 1.45 2019/05/28 07:41:47 msaitoh 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.45 2019/05/28 07:41:47 msaitoh 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 	int	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 | IFF_OACTIVE)) != IFF_RUNNING)
    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 		ifp->if_flags |= IFF_OACTIVE;
    320 		return;
    321 	}
    322 
    323 	IF_DEQUEUE(&ifp->if_snd, m);
    324 	if (m == 0)
    325 		return;
    326 
    327 	/* We need the header for m_pkthdr.len. */
    328 	if ((m->m_flags & M_PKTHDR) == 0)
    329 		panic("%s: snstart: no header mbuf", device_xname(sc->sc_dev));
    330 
    331 	/*
    332 	 * If bpf is listening on this interface, let it
    333 	 * see the packet before we commit it to the wire.
    334 	 */
    335 	bpf_mtap(ifp, m, BPF_D_OUT);
    336 
    337 	/*
    338 	 * If there is nothing in the o/p queue, and there is room in
    339 	 * the Tx ring, then send the packet directly.  Otherwise append
    340 	 * it to the o/p queue.
    341 	 */
    342 	if ((sonicput(sc, m, mtd_next)) == 0) {
    343 		IF_PREPEND(&ifp->if_snd, m);
    344 		return;
    345 	}
    346 
    347 	sc->mtd_prev = sc->mtd_free;
    348 	sc->mtd_free = mtd_next;
    349 
    350 	ifp->if_opackets++;		/* # of pkts */
    351 
    352 	/* Jump back for possibly more punishment. */
    353 	goto outloop;
    354 }
    355 
    356 /*
    357  * reset and restart the SONIC.  Called in case of fatal
    358  * hardware/software errors.
    359  */
    360 static void
    361 snreset(struct sn_softc *sc)
    362 {
    363 
    364 	snstop(sc);
    365 	sninit(sc);
    366 }
    367 
    368 static int
    369 sninit(struct sn_softc *sc)
    370 {
    371 	u_long	s_rcr;
    372 	int	s;
    373 
    374 	if (sc->sc_if.if_flags & IFF_RUNNING)
    375 		/* already running */
    376 		return 0;
    377 
    378 	s = splnet();
    379 
    380 	NIC_PUT(sc, SNR_CR, CR_RST);	/* DCR only accessible in reset mode! */
    381 
    382 	/* config it */
    383 	NIC_PUT(sc, SNR_DCR, (sc->snr_dcr |
    384 		(sc->bitmode ? DCR_DW32 : DCR_DW16)));
    385 	NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
    386 
    387 	s_rcr = RCR_BRD | RCR_LBNONE;
    388 	if (sc->sc_if.if_flags & IFF_PROMISC)
    389 		s_rcr |= RCR_PRO;
    390 	if (sc->sc_if.if_flags & IFF_ALLMULTI)
    391 		s_rcr |= RCR_AMC;
    392 	NIC_PUT(sc, SNR_RCR, s_rcr);
    393 
    394 #if 0
    395 	NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
    396 #else
    397 	NIC_PUT(sc, SNR_IMR, IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN |
    398 			     IMR_BREN  | IMR_HBLEN | IMR_RDEEN  | IMR_RBEEN |
    399 			     IMR_RBAEEN | IMR_RFOEN);
    400 #endif
    401 
    402 	/* clear pending interrupts */
    403 	NIC_PUT(sc, SNR_ISR, ISR_ALL);
    404 
    405 	/* clear tally counters */
    406 	NIC_PUT(sc, SNR_CRCT, -1);
    407 	NIC_PUT(sc, SNR_FAET, -1);
    408 	NIC_PUT(sc, SNR_MPT, -1);
    409 
    410 	initialise_tda(sc);
    411 	initialise_rda(sc);
    412 	initialise_rra(sc);
    413 
    414 	sn_md_init(sc);			/* MD initialization */
    415 
    416 	/* enable the chip */
    417 	NIC_PUT(sc, SNR_CR, 0);
    418 	wbflush();
    419 
    420 	/* program the CAM */
    421 	camprogram(sc);
    422 
    423 	/* get it to read resource descriptors */
    424 	NIC_PUT(sc, SNR_CR, CR_RRRA);
    425 	wbflush();
    426 	while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
    427 		continue;
    428 
    429 	/* enable rx */
    430 	NIC_PUT(sc, SNR_CR, CR_RXEN);
    431 	wbflush();
    432 
    433 	/* flag interface as "running" */
    434 	sc->sc_if.if_flags |= IFF_RUNNING;
    435 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    436 
    437 	splx(s);
    438 	return 0;
    439 }
    440 
    441 /*
    442  * close down an interface and free its buffers
    443  * Called on final close of device, or if sninit() fails
    444  * part way through.
    445  */
    446 static int
    447 snstop(struct sn_softc *sc)
    448 {
    449 	struct mtd *mtd;
    450 	int	s = splnet();
    451 
    452 	/* stick chip in reset */
    453 	NIC_PUT(sc, SNR_CR, CR_RST);
    454 	wbflush();
    455 
    456 	/* free all receive buffers (currently static so nothing to do) */
    457 
    458 	/* free all pending transmit mbufs */
    459 	while (sc->mtd_hw != sc->mtd_free) {
    460 		mtd = &sc->mtda[sc->mtd_hw];
    461 		if (mtd->mtd_mbuf)
    462 			m_freem(mtd->mtd_mbuf);
    463 		if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
    464 	}
    465 
    466 	sc->sc_if.if_timer = 0;
    467 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    468 
    469 	splx(s);
    470 	return 0;
    471 }
    472 
    473 /*
    474  * Called if any Tx packets remain unsent after 5 seconds,
    475  * In all cases we just reset the chip, and any retransmission
    476  * will be handled by higher level protocol timeouts.
    477  */
    478 static void
    479 snwatchdog(struct ifnet *ifp)
    480 {
    481 	struct sn_softc *sc = ifp->if_softc;
    482 	struct mtd *mtd;
    483 	int	temp;
    484 
    485 	if (sc->mtd_hw != sc->mtd_free) {
    486 		/* something still pending for transmit */
    487 		mtd = &sc->mtda[sc->mtd_hw];
    488 		if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
    489 			log(LOG_ERR, "%s: Tx - timeout\n",
    490 			    device_xname(sc->sc_dev));
    491 		else
    492 			log(LOG_ERR, "%s: Tx - lost interrupt\n",
    493 			    device_xname(sc->sc_dev));
    494 		temp = ifp->if_flags & IFF_UP;
    495 		snreset(sc);
    496 		ifp->if_flags |= temp;
    497 	}
    498 }
    499 
    500 /*
    501  * stuff packet into sonic (at splnet)
    502  */
    503 static inline u_int
    504 sonicput(struct sn_softc *sc, struct mbuf *m0, int mtd_next)
    505 {
    506 	struct mtd *mtdp;
    507 	struct mbuf *m;
    508 	u_char	*buff;
    509 	void	*txp;
    510 	u_int	len = 0;
    511 	u_int	totlen = 0;
    512 
    513 #ifdef whyonearthwouldyoudothis
    514 	if (NIC_GET(sc, SNR_CR) & CR_TXP)
    515 		return 0;
    516 #endif
    517 
    518 	/* grab the replacement mtd */
    519 	mtdp = &sc->mtda[sc->mtd_free];
    520 
    521 	buff = mtdp->mtd_buf;
    522 
    523 	/* this packet goes to mtdnext fill in the TDA */
    524 	mtdp->mtd_mbuf = m0;
    525 	txp = mtdp->mtd_txp;
    526 
    527 	/* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
    528 	if (sc->mtd_pint == 0) {
    529 		sc->mtd_pint = NTDA/2;
    530 		SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT);
    531 	} else {
    532 		sc->mtd_pint--;
    533 		SWO(sc->bitmode, txp, TXP_CONFIG, 0);
    534 	}
    535 
    536 	for (m = m0; m; m = m->m_next) {
    537 		u_char *data = mtod(m, u_char *);
    538 		len = m->m_len;
    539 		totlen += len;
    540 		memcpy(buff, data, len);
    541 		buff += len;
    542 	}
    543 	if (totlen >= TXBSIZE) {
    544 		panic("%s: sonicput: packet overflow",
    545 		    device_xname(sc->sc_dev));
    546 	}
    547 
    548 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
    549 	    LOWER(mtdp->mtd_vbuf));
    550 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
    551 	    UPPER(mtdp->mtd_vbuf));
    552 
    553 	if (totlen < ETHERMIN + ETHER_HDR_LEN) {
    554 		int pad = ETHERMIN + ETHER_HDR_LEN - totlen;
    555 		memset((char *)mtdp->mtd_buf + totlen, 0, pad);
    556 		totlen = ETHERMIN + ETHER_HDR_LEN;
    557 	}
    558 
    559 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
    560 	    totlen);
    561 	SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
    562 	SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
    563 
    564 	/* link onto the next mtd that will be used */
    565 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
    566 	    LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
    567 
    568 	/*
    569 	 * The previous txp.tlink currently contains a pointer to
    570 	 * our txp | EOL. Want to clear the EOL, so write our
    571 	 * pointer to the previous txp.
    572 	 */
    573 	SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
    574 	    LOWER(mtdp->mtd_vtxp));
    575 
    576 	/* make sure chip is running */
    577 	wbflush();
    578 	NIC_PUT(sc, SNR_CR, CR_TXP);
    579 	wbflush();
    580 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    581 
    582 	return totlen;
    583 }
    584 
    585 /*
    586  * These are called from sonicioctl() when /etc/ifconfig is run to set
    587  * the address or switch the i/f on.
    588  */
    589 /*
    590  * CAM support
    591  */
    592 static void
    593 caminitialise(struct sn_softc *sc)
    594 {
    595 	void	*p_cda = sc->p_cda;
    596 	int	i;
    597 	int	camoffset;
    598 
    599 	for (i = 0; i < MAXCAM; i++) {
    600 		camoffset = i * CDA_CAMDESC;
    601 		SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
    602 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
    603 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
    604 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
    605 	}
    606 	SWO(bitmode, p_cda, CDA_ENABLE, 0);
    607 }
    608 
    609 static void
    610 camentry(struct sn_softc *sc, int entry, const u_char *ea)
    611 {
    612 	void	*p_cda = sc->p_cda;
    613 	int	camoffset = entry * CDA_CAMDESC;
    614 
    615 	SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
    616 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
    617 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
    618 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
    619 	SWO(bitmode, p_cda, CDA_ENABLE,
    620 	    (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
    621 }
    622 
    623 static void
    624 camprogram(struct sn_softc *sc)
    625 {
    626 	struct ethercom *ec = &sc->sc_ethercom;
    627 	struct ether_multistep step;
    628 	struct ether_multi *enm;
    629 	struct ifnet *ifp;
    630 	int	timeout;
    631 	int	mcount = 0;
    632 
    633 	caminitialise(sc);
    634 
    635 	ifp = &sc->sc_if;
    636 
    637 	/* Always load our own address first. */
    638 	camentry(sc, mcount, CLLADDR(ifp->if_sadl));
    639 	mcount++;
    640 
    641 	/* Assume we won't need allmulti bit. */
    642 	ifp->if_flags &= ~IFF_ALLMULTI;
    643 
    644 	/* Loop through multicast addresses */
    645 	ETHER_LOCK(ec);
    646 	ETHER_FIRST_MULTI(step, ec, enm);
    647 	while (enm != NULL) {
    648 		if (mcount == MAXCAM) {
    649 			 ifp->if_flags |= IFF_ALLMULTI;
    650 			 break;
    651 		}
    652 
    653 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    654 		    sizeof(enm->enm_addrlo)) != 0) {
    655 			/*
    656 			 * SONIC's CAM is programmed with specific
    657 			 * addresses. It has no way to specify a range.
    658 			 * (Well, thats not exactly true. If the
    659 			 * range is small one could program each addr
    660 			 * within the range as a separate CAM entry)
    661 			 */
    662 			ifp->if_flags |= IFF_ALLMULTI;
    663 			break;
    664 		}
    665 
    666 		/* program the CAM with the specified entry */
    667 		camentry(sc, mcount, enm->enm_addrlo);
    668 		mcount++;
    669 
    670 		ETHER_NEXT_MULTI(step, enm);
    671 	}
    672 	ETHER_UNLOCK(ec);
    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 	wbflush();
    744 }
    745 
    746 static void
    747 initialise_rda(struct sn_softc *sc)
    748 {
    749 	int		i;
    750 	char 		*p_rda = 0;
    751 	uint32_t	v_rda = 0;
    752 
    753 	/* link the RDA's together into a circular list */
    754 	for (i = 0; i < (sc->sc_nrda - 1); i++) {
    755 		p_rda = (char *)sc->p_rda + (i * RXPKT_SIZE(sc));
    756 		v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
    757 		SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda));
    758 		SWO(bitmode, p_rda, RXPKT_INUSE, 1);
    759 	}
    760 	p_rda = (char *)sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
    761 	SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
    762 	SWO(bitmode, p_rda, RXPKT_INUSE, 1);
    763 
    764 	/* mark end of receive descriptor list */
    765 	sc->sc_rdamark = sc->sc_nrda - 1;
    766 
    767 	sc->sc_rxmark = 0;
    768 
    769 	NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda));
    770 	NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda));
    771 	wbflush();
    772 }
    773 
    774 static void
    775 initialise_rra(struct sn_softc *sc)
    776 {
    777 	int	i;
    778 	u_int	v;
    779 	int	bitmode = sc->bitmode;
    780 
    781 	if (bitmode)
    782 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
    783 	else
    784 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
    785 
    786 	NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
    787 	NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
    788 	/* rea must point just past the end of the rra space */
    789 	NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
    790 	NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
    791 	NIC_PUT(sc, SNR_RSC, 0);
    792 
    793 	/* fill up SOME of the rra with buffers */
    794 	for (i = 0; i < NRBA; i++) {
    795 		v = SONIC_GETDMA(sc, sc->rbuf[i]);
    796 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
    797 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
    798 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(PAGE_SIZE/2));
    799 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(PAGE_SIZE/2));
    800 	}
    801 	sc->sc_rramark = NRBA;
    802 	NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
    803 	wbflush();
    804 }
    805 
    806 int
    807 snintr(void *arg)
    808 {
    809 	struct sn_softc *sc = (struct sn_softc *)arg;
    810 	int handled = 0;
    811 	int	isr;
    812 
    813 	while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
    814 		/* scrub the interrupts that we are going to service */
    815 		NIC_PUT(sc, SNR_ISR, isr);
    816 		handled = 1;
    817 		wbflush();
    818 
    819 		if (isr & (ISR_BR | ISR_LCD | ISR_TC))
    820 			printf("%s: unexpected interrupt status 0x%x\n",
    821 			    device_xname(sc->sc_dev), isr);
    822 
    823 		if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
    824 			sonictxint(sc);
    825 
    826 		if (isr & ISR_PKTRX)
    827 			sonicrxint(sc);
    828 
    829 		if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
    830 			if (isr & ISR_HBL)
    831 				/*
    832 				 * The repeater is not providing a heartbeat.
    833 				 * In itself this isn't harmful, lots of the
    834 				 * cheap repeater hubs don't supply a heartbeat.
    835 				 * So ignore the lack of heartbeat. Its only
    836 				 * if we can't detect a carrier that we have a
    837 				 * problem.
    838 				 */
    839 				;
    840 			if (isr & ISR_RDE)
    841 				printf("%s: receive descriptors exhausted\n",
    842 				    device_xname(sc->sc_dev));
    843 			if (isr & ISR_RBE)
    844 				printf("%s: receive buffers exhausted\n",
    845 				    device_xname(sc->sc_dev));
    846 			if (isr & ISR_RBAE)
    847 				printf("%s: receive buffer area exhausted\n",
    848 				    device_xname(sc->sc_dev));
    849 			if (isr & ISR_RFO)
    850 				printf("%s: receive FIFO overrun\n",
    851 				    device_xname(sc->sc_dev));
    852 		}
    853 		if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
    854 #ifdef notdef
    855 			if (isr & ISR_CRC)
    856 				sc->sc_crctally++;
    857 			if (isr & ISR_FAE)
    858 				sc->sc_faetally++;
    859 			if (isr & ISR_MP)
    860 				sc->sc_mptally++;
    861 #endif
    862 		}
    863 		if_schedule_deferred_start(&sc->sc_if);
    864 	}
    865 	return handled;
    866 }
    867 
    868 /*
    869  * Transmit interrupt routine
    870  */
    871 static void
    872 sonictxint(struct sn_softc *sc)
    873 {
    874 	struct mtd	*mtd;
    875 	void		*txp;
    876 	unsigned short	txp_status;
    877 	int		mtd_hw;
    878 	struct ifnet	*ifp = &sc->sc_if;
    879 
    880 	mtd_hw = sc->mtd_hw;
    881 
    882 	if (mtd_hw == sc->mtd_free)
    883 		return;
    884 
    885 	while (mtd_hw != sc->mtd_free) {
    886 		mtd = &sc->mtda[mtd_hw];
    887 
    888 		txp = mtd->mtd_txp;
    889 
    890 		if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) {
    891 			break; /* it hasn't really gone yet */
    892 		}
    893 
    894 #ifdef SNDEBUG
    895 		{
    896 			struct ether_header *eh;
    897 
    898 			eh = (struct ether_header *) mtd->mtd_buf;
    899 			printf("%s: xmit status=0x%x len=%d type=0x%x from %s",
    900 			    device_xname(sc->sc_dev),
    901 			    SRO(sc->bitmode, txp, TXP_STATUS),
    902 			    SRO(sc->bitmode, txp, TXP_PKTSIZE),
    903 			    htons(eh->ether_type),
    904 			    ether_sprintf(eh->ether_shost));
    905 			printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
    906 		}
    907 #endif /* SNDEBUG */
    908 
    909 		ifp->if_flags &= ~IFF_OACTIVE;
    910 
    911 		if (mtd->mtd_mbuf != 0) {
    912 			m_freem(mtd->mtd_mbuf);
    913 			mtd->mtd_mbuf = 0;
    914 		}
    915 		if (++mtd_hw == NTDA) mtd_hw = 0;
    916 
    917 		txp_status = SRO(sc->bitmode, txp, TXP_STATUS);
    918 
    919 		ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
    920 			((txp_status & TCR_NC) >> 12);
    921 
    922 		if ((txp_status & TCR_PTX) == 0) {
    923 			ifp->if_oerrors++;
    924 			printf("%s: Tx packet status=0x%x\n",
    925 			    device_xname(sc->sc_dev), txp_status);
    926 
    927 			/* XXX - DG This looks bogus */
    928 			if (mtd_hw != sc->mtd_free) {
    929 				printf("resubmitting remaining packets\n");
    930 				mtd = &sc->mtda[mtd_hw];
    931 				NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
    932 				NIC_PUT(sc, SNR_CR, CR_TXP);
    933 				wbflush();
    934 				break;
    935 			}
    936 		}
    937 	}
    938 
    939 	sc->mtd_hw = mtd_hw;
    940 	return;
    941 }
    942 
    943 /*
    944  * Receive interrupt routine
    945  */
    946 static void
    947 sonicrxint(struct sn_softc *sc)
    948 {
    949 	void *	rda;
    950 	int	orra;
    951 	int	len;
    952 	int	rramark;
    953 	int	rdamark;
    954 	uint16_t rxpkt_ptr;
    955 
    956 	rda = (char *)sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
    957 
    958 	while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
    959 		u_int status = SRO(bitmode, rda, RXPKT_STATUS);
    960 
    961 		orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
    962 		rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
    963 		len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE;
    964 		if (status & RCR_PRX) {
    965 			void *pkt =
    966 			    (char *)sc->rbuf[orra & RBAMASK] +
    967 				 (rxpkt_ptr & PGOFSET);
    968 			if (sonic_read(sc, pkt, len) == 0)
    969 				sc->sc_if.if_ierrors++;
    970 		} else
    971 			sc->sc_if.if_ierrors++;
    972 
    973 		/*
    974 		 * give receive buffer area back to chip.
    975 		 *
    976 		 * If this was the last packet in the RRA, give the RRA to
    977 		 * the chip again.
    978 		 * If sonic read didnt copy it out then we would have to
    979 		 * wait !!
    980 		 * (dont bother add it back in again straight away)
    981 		 *
    982 		 * Really, we're doing p_rra[rramark] = p_rra[orra] but
    983 		 * we have to use the macros because SONIC might be in
    984 		 * 16 or 32 bit mode.
    985 		 */
    986 		if (status & RCR_LPKT) {
    987 			void *tmp1, *tmp2;
    988 
    989 			rramark = sc->sc_rramark;
    990 			tmp1 = sc->p_rra[rramark];
    991 			tmp2 = sc->p_rra[orra];
    992 			SWO(bitmode, tmp1, RXRSRC_PTRLO,
    993 			    SRO(bitmode, tmp2, RXRSRC_PTRLO));
    994 			SWO(bitmode, tmp1, RXRSRC_PTRHI,
    995 			    SRO(bitmode, tmp2, RXRSRC_PTRHI));
    996 			SWO(bitmode, tmp1, RXRSRC_WCLO,
    997 			    SRO(bitmode, tmp2, RXRSRC_WCLO));
    998 			SWO(bitmode, tmp1, RXRSRC_WCHI,
    999 			    SRO(bitmode, tmp2, RXRSRC_WCHI));
   1000 
   1001 			/* zap old rra for fun */
   1002 			SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
   1003 			SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
   1004 
   1005 			sc->sc_rramark = (++rramark) & RRAMASK;
   1006 			NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
   1007 			wbflush();
   1008 		}
   1009 
   1010 		/*
   1011 		 * give receive descriptor back to chip simple
   1012 		 * list is circular
   1013 		 */
   1014 		rdamark = sc->sc_rdamark;
   1015 		SWO(bitmode, rda, RXPKT_INUSE, 1);
   1016 		SWO(bitmode, rda, RXPKT_RLINK,
   1017 		    SRO(bitmode, rda, RXPKT_RLINK) | EOL);
   1018 		SWO(bitmode, ((char *)sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
   1019 		    RXPKT_RLINK,
   1020 		    SRO(bitmode, ((char *)sc->p_rda +
   1021 			(rdamark * RXPKT_SIZE(sc))),
   1022 		    RXPKT_RLINK) & ~EOL);
   1023 		sc->sc_rdamark = sc->sc_rxmark;
   1024 
   1025 		if (++sc->sc_rxmark >= sc->sc_nrda)
   1026 			sc->sc_rxmark = 0;
   1027 		rda = (char *)sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
   1028 	}
   1029 }
   1030 
   1031 /*
   1032  * sonic_read -- pull packet off interface and forward to
   1033  * appropriate protocol handler
   1034  */
   1035 static inline int
   1036 sonic_read(struct sn_softc *sc, void *pkt, int len)
   1037 {
   1038 	struct ifnet *ifp = &sc->sc_if;
   1039 	struct mbuf *m;
   1040 
   1041 #ifdef SNDEBUG
   1042 	{
   1043 		printf("%s: rcvd %p len=%d type=0x%x from %s",
   1044 		    devoce_xname(sc->sc_dev), et, len, htons(et->ether_type),
   1045 		    ether_sprintf(et->ether_shost));
   1046 		printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
   1047 	}
   1048 #endif /* SNDEBUG */
   1049 
   1050 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
   1051 	    len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
   1052 		printf("%s: invalid packet length %d bytes\n",
   1053 		    device_xname(sc->sc_dev), len);
   1054 		return 0;
   1055 	}
   1056 
   1057 	m = sonic_get(sc, pkt, len);
   1058 	if (m == NULL)
   1059 		return 0;
   1060 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1061 	return 1;
   1062 }
   1063 
   1064 /*
   1065  * munge the received packet into an mbuf chain
   1066  */
   1067 static inline struct mbuf *
   1068 sonic_get(struct sn_softc *sc, void *pkt, int datalen)
   1069 {
   1070 	struct	mbuf *m, *top, **mp;
   1071 	int	len;
   1072 
   1073 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1074 	if (m == 0)
   1075 		return 0;
   1076 	m_set_rcvif(m, &sc->sc_if);
   1077 	m->m_pkthdr.len = datalen;
   1078 	len = MHLEN;
   1079 	top = 0;
   1080 	mp = &top;
   1081 
   1082 	while (datalen > 0) {
   1083 		if (top) {
   1084 			MGET(m, M_DONTWAIT, MT_DATA);
   1085 			if (m == 0) {
   1086 				m_freem(top);
   1087 				return 0;
   1088 			}
   1089 			len = MLEN;
   1090 		}
   1091 		if (datalen >= MINCLSIZE) {
   1092 			MCLGET(m, M_DONTWAIT);
   1093 			if ((m->m_flags & M_EXT) == 0) {
   1094 				if (top)
   1095 					m_freem(top);
   1096 				else
   1097 					m_freem(m);
   1098 				return 0;
   1099 			}
   1100 			len = MCLBYTES;
   1101 		}
   1102 
   1103 		if (mp == &top) {
   1104 			char *newdata = (char *)
   1105 			    ALIGN((char *)m->m_data +
   1106 				sizeof(struct ether_header)) -
   1107 			    sizeof(struct ether_header);
   1108 			len -= newdata - m->m_data;
   1109 			m->m_data = newdata;
   1110 		}
   1111 
   1112 		m->m_len = len = uimin(datalen, len);
   1113 
   1114 		memcpy(mtod(m, void *), pkt, (unsigned) len);
   1115 		pkt = (char *)pkt + len;
   1116 		datalen -= len;
   1117 		*mp = m;
   1118 		mp = &m->m_next;
   1119 	}
   1120 
   1121 	return top;
   1122 }
   1123