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