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