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