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