xref: /netbsd-src/sys/arch/mac68k/dev/if_sn.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: if_sn.c,v 1.34 2003/07/15 02:43:17 lukem 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.34 2003/07/15 02:43:17 lukem 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 			temp = ifp->if_flags & IFF_UP;
341 			snreset(sc);
342 			ifp->if_flags |= temp;
343 			err = 0;
344 		}
345 		break;
346 	default:
347 		err = EINVAL;
348 	}
349 	splx(s);
350 	return (err);
351 }
352 
353 /*
354  * Encapsulate a packet of type family for the local net.
355  */
356 static void
357 snstart(ifp)
358 	struct ifnet *ifp;
359 {
360 	struct sn_softc	*sc = ifp->if_softc;
361 	struct mbuf	*m;
362 	int		mtd_next;
363 
364 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
365 		return;
366 
367 outloop:
368 	/* Check for room in the xmit buffer. */
369 	if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
370 		mtd_next = 0;
371 
372 	if (mtd_next == sc->mtd_hw) {
373 		ifp->if_flags |= IFF_OACTIVE;
374 		return;
375 	}
376 
377 	IF_DEQUEUE(&ifp->if_snd, m);
378 	if (m == 0)
379 		return;
380 
381 	/* We need the header for m_pkthdr.len. */
382 	if ((m->m_flags & M_PKTHDR) == 0)
383 		panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname);
384 
385 #if NBPFILTER > 0
386 	/*
387 	 * If bpf is listening on this interface, let it
388 	 * see the packet before we commit it to the wire.
389 	 */
390 	if (ifp->if_bpf)
391 		bpf_mtap(ifp->if_bpf, m);
392 #endif
393 
394 	/*
395 	 * If there is nothing in the o/p queue, and there is room in
396 	 * the Tx ring, then send the packet directly.  Otherwise append
397 	 * it to the o/p queue.
398 	 */
399 	if ((sonicput(sc, m, mtd_next)) == 0) {
400 		IF_PREPEND(&ifp->if_snd, m);
401 		return;
402 	}
403 
404 	sc->mtd_prev = sc->mtd_free;
405 	sc->mtd_free = mtd_next;
406 
407 	ifp->if_opackets++;		/* # of pkts */
408 
409 	/* Jump back for possibly more punishment. */
410 	goto outloop;
411 }
412 
413 /*
414  * reset and restart the SONIC.  Called in case of fatal
415  * hardware/software errors.
416  */
417 static void
418 snreset(sc)
419 	struct sn_softc *sc;
420 {
421 	snstop(sc);
422 	sninit(sc);
423 }
424 
425 static int
426 sninit(sc)
427 	struct sn_softc *sc;
428 {
429 	u_long	s_rcr;
430 	int	s;
431 
432 	if (sc->sc_if.if_flags & IFF_RUNNING)
433 		/* already running */
434 		return (0);
435 
436 	s = splnet();
437 
438 	NIC_PUT(sc, SNR_CR, CR_RST);	/* DCR only accessible in reset mode! */
439 
440 	/* config it */
441 	NIC_PUT(sc, SNR_DCR, (sc->snr_dcr |
442 		(sc->bitmode ? DCR_DW32 : DCR_DW16)));
443 	NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
444 
445 	s_rcr = RCR_BRD | RCR_LBNONE;
446 	if (sc->sc_if.if_flags & IFF_PROMISC)
447 		s_rcr |= RCR_PRO;
448 	if (sc->sc_if.if_flags & IFF_ALLMULTI)
449 		s_rcr |= RCR_AMC;
450 	NIC_PUT(sc, SNR_RCR, s_rcr);
451 
452 	NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
453 
454 	/* clear pending interrupts */
455 	NIC_PUT(sc, SNR_ISR, ISR_ALL);
456 
457 	/* clear tally counters */
458 	NIC_PUT(sc, SNR_CRCT, -1);
459 	NIC_PUT(sc, SNR_FAET, -1);
460 	NIC_PUT(sc, SNR_MPT, -1);
461 
462 	initialise_tda(sc);
463 	initialise_rda(sc);
464 	initialise_rra(sc);
465 
466 	/* enable the chip */
467 	NIC_PUT(sc, SNR_CR, 0);
468 	wbflush();
469 
470 	/* program the CAM */
471 	camprogram(sc);
472 
473 	/* get it to read resource descriptors */
474 	NIC_PUT(sc, SNR_CR, CR_RRRA);
475 	wbflush();
476 	while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
477 		continue;
478 
479 	/* enable rx */
480 	NIC_PUT(sc, SNR_CR, CR_RXEN);
481 	wbflush();
482 
483 	/* flag interface as "running" */
484 	sc->sc_if.if_flags |= IFF_RUNNING;
485 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
486 
487 	splx(s);
488 	return (0);
489 }
490 
491 /*
492  * close down an interface and free its buffers
493  * Called on final close of device, or if sninit() fails
494  * part way through.
495  */
496 static int
497 snstop(sc)
498 	struct sn_softc *sc;
499 {
500 	struct mtd *mtd;
501 	int	s = splnet();
502 
503 	/* stick chip in reset */
504 	NIC_PUT(sc, SNR_CR, CR_RST);
505 	wbflush();
506 
507 	/* free all receive buffers (currently static so nothing to do) */
508 
509 	/* free all pending transmit mbufs */
510 	while (sc->mtd_hw != sc->mtd_free) {
511 		mtd = &sc->mtda[sc->mtd_hw];
512 		if (mtd->mtd_mbuf)
513 			m_freem(mtd->mtd_mbuf);
514 		if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
515 	}
516 
517 	sc->sc_if.if_timer = 0;
518 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
519 
520 	splx(s);
521 	return (0);
522 }
523 
524 /*
525  * Called if any Tx packets remain unsent after 5 seconds,
526  * In all cases we just reset the chip, and any retransmission
527  * will be handled by higher level protocol timeouts.
528  */
529 static void
530 snwatchdog(ifp)
531 	struct ifnet *ifp;
532 {
533 	struct sn_softc *sc = ifp->if_softc;
534 	struct mtd *mtd;
535 	int	temp;
536 
537 	if (sc->mtd_hw != sc->mtd_free) {
538 		/* something still pending for transmit */
539 		mtd = &sc->mtda[sc->mtd_hw];
540 		if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
541 			log(LOG_ERR, "%s: Tx - timeout\n",
542 			    sc->sc_dev.dv_xname);
543 		else
544 			log(LOG_ERR, "%s: Tx - lost interrupt\n",
545 			    sc->sc_dev.dv_xname);
546 		temp = ifp->if_flags & IFF_UP;
547 		snreset(sc);
548 		ifp->if_flags |= temp;
549 	}
550 }
551 
552 /*
553  * stuff packet into sonic (at splnet)
554  */
555 static __inline__ u_int
556 sonicput(sc, m0, mtd_next)
557 	struct sn_softc *sc;
558 	struct mbuf *m0;
559 	int mtd_next;
560 {
561 	struct mtd *mtdp;
562 	struct mbuf *m;
563 	u_char	*buff;
564 	void	*txp;
565 	u_int	len = 0;
566 	u_int	totlen = 0;
567 
568 #ifdef whyonearthwouldyoudothis
569 	if (NIC_GET(sc, SNR_CR) & CR_TXP)
570 		return (0);
571 #endif
572 
573 	/* grab the replacement mtd */
574 	mtdp = &sc->mtda[sc->mtd_free];
575 
576 	buff = mtdp->mtd_buf;
577 
578 	/* this packet goes to mtdnext fill in the TDA */
579 	mtdp->mtd_mbuf = m0;
580 	txp = mtdp->mtd_txp;
581 
582 	/* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
583 	if (sc->mtd_pint == 0) {
584 		sc->mtd_pint = NTDA/2;
585 		SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT);
586 	} else {
587 		sc->mtd_pint--;
588 		SWO(sc->bitmode, txp, TXP_CONFIG, 0);
589 	}
590 
591 	for (m = m0; m; m = m->m_next) {
592 		u_char *data = mtod(m, u_char *);
593 		len = m->m_len;
594 		totlen += len;
595 		bcopy(data, buff, len);
596 		buff += len;
597 	}
598 	if (totlen >= TXBSIZE) {
599 		panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname);
600 	}
601 
602 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
603 	    LOWER(mtdp->mtd_vbuf));
604 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
605 	    UPPER(mtdp->mtd_vbuf));
606 
607 	if (totlen < ETHERMIN + ETHER_HDR_LEN) {
608 		int pad = ETHERMIN + ETHER_HDR_LEN - totlen;
609 		bzero(mtdp->mtd_buf + totlen, pad);
610 		totlen = ETHERMIN + ETHER_HDR_LEN;
611 	}
612 
613 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
614 	    totlen);
615 	SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
616 	SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
617 
618 	/* link onto the next mtd that will be used */
619 	SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
620 	    LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
621 
622 	/*
623 	 * The previous txp.tlink currently contains a pointer to
624 	 * our txp | EOL. Want to clear the EOL, so write our
625 	 * pointer to the previous txp.
626 	 */
627 	SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
628 	    LOWER(mtdp->mtd_vtxp));
629 
630 	/* make sure chip is running */
631 	wbflush();
632 	NIC_PUT(sc, SNR_CR, CR_TXP);
633 	wbflush();
634 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
635 
636 	return (totlen);
637 }
638 
639 /*
640  * These are called from sonicioctl() when /etc/ifconfig is run to set
641  * the address or switch the i/f on.
642  */
643 /*
644  * CAM support
645  */
646 static void
647 caminitialise(sc)
648 	struct sn_softc *sc;
649 {
650 	void	*p_cda = sc->p_cda;
651 	int	i;
652 	int	bitmode = sc->bitmode;
653 	int	camoffset;
654 
655 	for (i = 0; i < MAXCAM; i++) {
656 		camoffset = i * CDA_CAMDESC;
657 		SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
658 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
659 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
660 		SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
661 	}
662 	SWO(bitmode, p_cda, CDA_ENABLE, 0);
663 }
664 
665 static void
666 camentry(sc, entry, ea)
667 	int entry;
668 	u_char *ea;
669 	struct sn_softc *sc;
670 {
671 	void	*p_cda = sc->p_cda;
672 	int	bitmode = sc->bitmode;
673 	int	camoffset = entry * CDA_CAMDESC;
674 
675 	SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
676 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
677 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
678 	SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
679 	SWO(bitmode, p_cda, CDA_ENABLE,
680 	    (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
681 }
682 
683 static void
684 camprogram(sc)
685 	struct sn_softc *sc;
686 {
687 	struct ether_multistep step;
688 	struct ether_multi *enm;
689 	struct ifnet *ifp;
690 	int	timeout;
691 	int	mcount = 0;
692 
693 	caminitialise(sc);
694 
695 	ifp = &sc->sc_if;
696 
697 	/* Always load our own address first. */
698 	camentry (sc, mcount, LLADDR(ifp->if_sadl));
699 	mcount++;
700 
701 	/* Assume we won't need allmulti bit. */
702 	ifp->if_flags &= ~IFF_ALLMULTI;
703 
704 	/* Loop through multicast addresses */
705 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
706 	while (enm != NULL) {
707 		if (mcount == MAXCAM) {
708 			 ifp->if_flags |= IFF_ALLMULTI;
709 			 break;
710 		}
711 
712 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
713 		    sizeof(enm->enm_addrlo)) != 0) {
714 			/*
715 			 * SONIC's CAM is programmed with specific
716 			 * addresses. It has no way to specify a range.
717 			 * (Well, thats not exactly true. If the
718 			 * range is small one could program each addr
719 			 * within the range as a separate CAM entry)
720 			 */
721 			ifp->if_flags |= IFF_ALLMULTI;
722 			break;
723 		}
724 
725 		/* program the CAM with the specified entry */
726 		camentry(sc, mcount, enm->enm_addrlo);
727 		mcount++;
728 
729 		ETHER_NEXT_MULTI(step, enm);
730 	}
731 
732 	NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda));
733 	NIC_PUT(sc, SNR_CDC, MAXCAM);
734 	NIC_PUT(sc, SNR_CR, CR_LCAM);
735 	wbflush();
736 
737 	timeout = 10000;
738 	while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--)
739 		continue;
740 	if (timeout == 0) {
741 		/* XXX */
742 		panic("%s: CAM initialisation failed", sc->sc_dev.dv_xname);
743 	}
744 	timeout = 10000;
745 	while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--)
746 		continue;
747 
748 	if (NIC_GET(sc, SNR_ISR) & ISR_LCD)
749 		NIC_PUT(sc, SNR_ISR, ISR_LCD);
750 	else
751 		printf("%s: CAM initialisation without interrupt\n",
752 		    sc->sc_dev.dv_xname);
753 }
754 
755 #ifdef SNDEBUG
756 static void
757 camdump(sc)
758 	struct sn_softc *sc;
759 {
760 	int	i;
761 
762 	printf("CAM entries:\n");
763 	NIC_PUT(sc, SNR_CR, CR_RST);
764 	wbflush();
765 
766 	for (i = 0; i < 16; i++) {
767 		ushort  ap2, ap1, ap0;
768 		NIC_PUT(sc, SNR_CEP, i);
769 		wbflush();
770 		ap2 = NIC_GET(sc, SNR_CAP2);
771 		ap1 = NIC_GET(sc, SNR_CAP1);
772 		ap0 = NIC_GET(sc, SNR_CAP0);
773 		printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
774 	}
775 	printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP));
776 
777 	NIC_PUT(sc, SNR_CR, 0);
778 	wbflush();
779 }
780 #endif
781 
782 static void
783 initialise_tda(sc)
784 	struct sn_softc *sc;
785 {
786 	struct mtd *mtd;
787 	int	i;
788 
789 	for (i = 0; i < NTDA; i++) {
790 		mtd = &sc->mtda[i];
791 		mtd->mtd_mbuf = 0;
792 	}
793 
794 	sc->mtd_hw = 0;
795 	sc->mtd_prev = NTDA - 1;
796 	sc->mtd_free = 0;
797 	sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
798 	sc->mtd_pint = NTDA/2;
799 
800 	NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
801 	NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
802 }
803 
804 static void
805 initialise_rda(sc)
806 	struct sn_softc *sc;
807 {
808 	int		bitmode = sc->bitmode;
809 	int		i;
810 	caddr_t		p_rda = 0;
811 	u_int32_t	v_rda = 0;
812 
813 	/* link the RDA's together into a circular list */
814 	for (i = 0; i < (sc->sc_nrda - 1); i++) {
815 		p_rda = sc->p_rda + (i * RXPKT_SIZE(sc));
816 		v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
817 		SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda));
818 		SWO(bitmode, p_rda, RXPKT_INUSE, 1);
819 	}
820 	p_rda = sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
821 	SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
822 	SWO(bitmode, p_rda, RXPKT_INUSE, 1);
823 
824 	/* mark end of receive descriptor list */
825 	sc->sc_rdamark = sc->sc_nrda - 1;
826 
827 	sc->sc_rxmark = 0;
828 
829 	NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda));
830 	NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda));
831 	wbflush();
832 }
833 
834 static void
835 initialise_rra(sc)
836 	struct sn_softc *sc;
837 {
838 	int	i;
839 	u_int	v;
840 	int	bitmode = sc->bitmode;
841 
842 	if (bitmode)
843 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
844 	else
845 		NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
846 
847 	NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
848 	NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
849 	/* rea must point just past the end of the rra space */
850 	NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
851 	NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
852 	NIC_PUT(sc, SNR_RSC, 0);
853 
854 	/* fill up SOME of the rra with buffers */
855 	for (i = 0; i < NRBA; i++) {
856 		v = SONIC_GETDMA(sc->rbuf[i]);
857 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
858 		SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
859 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(PAGE_SIZE/2));
860 		SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(PAGE_SIZE/2));
861 	}
862 	sc->sc_rramark = NRBA;
863 	NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
864 	wbflush();
865 }
866 
867 void
868 snintr(arg)
869 	void	*arg;
870 {
871 	struct sn_softc *sc = (struct sn_softc *)arg;
872 	int	isr;
873 
874 	while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
875 		/* scrub the interrupts that we are going to service */
876 		NIC_PUT(sc, SNR_ISR, isr);
877 		wbflush();
878 
879 		if (isr & (ISR_BR | ISR_LCD | ISR_TC))
880 			printf("%s: unexpected interrupt status 0x%x\n",
881 			    sc->sc_dev.dv_xname, isr);
882 
883 		if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
884 			sonictxint(sc);
885 
886 		if (isr & ISR_PKTRX)
887 			sonicrxint(sc);
888 
889 		if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
890 			if (isr & ISR_HBL)
891 				/*
892 				 * The repeater is not providing a heartbeat.
893 				 * In itself this isn't harmful, lots of the
894 				 * cheap repeater hubs don't supply a heartbeat.
895 				 * So ignore the lack of heartbeat. Its only
896 				 * if we can't detect a carrier that we have a
897 				 * problem.
898 				 */
899 				;
900 			if (isr & ISR_RDE)
901 				printf("%s: receive descriptors exhausted\n",
902 				    sc->sc_dev.dv_xname);
903 			if (isr & ISR_RBE)
904 				printf("%s: receive buffers exhausted\n",
905 				    sc->sc_dev.dv_xname);
906 			if (isr & ISR_RBAE)
907 				printf("%s: receive buffer area exhausted\n",
908 				    sc->sc_dev.dv_xname);
909 			if (isr & ISR_RFO)
910 				printf("%s: receive FIFO overrun\n",
911 				    sc->sc_dev.dv_xname);
912 		}
913 		if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
914 #ifdef notdef
915 			if (isr & ISR_CRC)
916 				sc->sc_crctally++;
917 			if (isr & ISR_FAE)
918 				sc->sc_faetally++;
919 			if (isr & ISR_MP)
920 				sc->sc_mptally++;
921 #endif
922 		}
923 		snstart(&sc->sc_if);
924 	}
925 	return;
926 }
927 
928 /*
929  * Transmit interrupt routine
930  */
931 static void
932 sonictxint(sc)
933 	struct sn_softc *sc;
934 {
935 	struct mtd	*mtd;
936 	void		*txp;
937 	unsigned short	txp_status;
938 	int		mtd_hw;
939 	struct ifnet	*ifp = &sc->sc_if;
940 
941 	mtd_hw = sc->mtd_hw;
942 
943 	if (mtd_hw == sc->mtd_free)
944 		return;
945 
946 	while (mtd_hw != sc->mtd_free) {
947 		mtd = &sc->mtda[mtd_hw];
948 
949 		txp = mtd->mtd_txp;
950 
951 		if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) {
952 			break; /* it hasn't really gone yet */
953 		}
954 
955 #ifdef SNDEBUG
956 		{
957 			struct ether_header *eh;
958 
959 			eh = (struct ether_header *) mtd->mtd_buf;
960 			printf("%s: xmit status=0x%x len=%d type=0x%x from %s",
961 			    sc->sc_dev.dv_xname,
962 			    SRO(sc->bitmode, txp, TXP_STATUS),
963 			    SRO(sc->bitmode, txp, TXP_PKTSIZE),
964 			    htons(eh->ether_type),
965 			    ether_sprintf(eh->ether_shost));
966 			printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
967 		}
968 #endif /* SNDEBUG */
969 
970 		ifp->if_flags &= ~IFF_OACTIVE;
971 
972 		if (mtd->mtd_mbuf != 0) {
973 			m_freem(mtd->mtd_mbuf);
974 			mtd->mtd_mbuf = 0;
975 		}
976 		if (++mtd_hw == NTDA) mtd_hw = 0;
977 
978 		txp_status = SRO(sc->bitmode, txp, TXP_STATUS);
979 
980 		ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
981 			((txp_status & TCR_NC) >> 12);
982 
983 		if ((txp_status & TCR_PTX) == 0) {
984 			ifp->if_oerrors++;
985 			printf("%s: Tx packet status=0x%x\n",
986 			    sc->sc_dev.dv_xname, txp_status);
987 
988 			/* XXX - DG This looks bogus */
989 			if (mtd_hw != sc->mtd_free) {
990 				printf("resubmitting remaining packets\n");
991 				mtd = &sc->mtda[mtd_hw];
992 				NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
993 				NIC_PUT(sc, SNR_CR, CR_TXP);
994 				wbflush();
995 				break;
996 			}
997 		}
998 	}
999 
1000 	sc->mtd_hw = mtd_hw;
1001 	return;
1002 }
1003 
1004 /*
1005  * Receive interrupt routine
1006  */
1007 static void
1008 sonicrxint(sc)
1009 	struct sn_softc *sc;
1010 {
1011 	caddr_t	rda;
1012 	int	orra;
1013 	int	len;
1014 	int	rramark;
1015 	int	rdamark;
1016 	int	bitmode = sc->bitmode;
1017 	u_int16_t rxpkt_ptr;
1018 
1019 	rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1020 
1021 	while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
1022 		u_int status = SRO(bitmode, rda, RXPKT_STATUS);
1023 
1024 		orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
1025 		rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
1026 		len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE;
1027 		if (status & RCR_PRX) {
1028 			caddr_t pkt = sc->rbuf[orra & RBAMASK] +
1029 			    m68k_page_offset(rxpkt_ptr);
1030 			if (sonic_read(sc, pkt, len))
1031 				sc->sc_if.if_ipackets++;
1032 			else
1033 				sc->sc_if.if_ierrors++;
1034 		} else
1035 			sc->sc_if.if_ierrors++;
1036 
1037 		/*
1038 		 * give receive buffer area back to chip.
1039 		 *
1040 		 * If this was the last packet in the RRA, give the RRA to
1041 		 * the chip again.
1042 		 * If sonic read didnt copy it out then we would have to
1043 		 * wait !!
1044 		 * (dont bother add it back in again straight away)
1045 		 *
1046 		 * Really, we're doing p_rra[rramark] = p_rra[orra] but
1047 		 * we have to use the macros because SONIC might be in
1048 		 * 16 or 32 bit mode.
1049 		 */
1050 		if (status & RCR_LPKT) {
1051 			void *tmp1, *tmp2;
1052 
1053 			rramark = sc->sc_rramark;
1054 			tmp1 = sc->p_rra[rramark];
1055 			tmp2 = sc->p_rra[orra];
1056 			SWO(bitmode, tmp1, RXRSRC_PTRLO,
1057 				SRO(bitmode, tmp2, RXRSRC_PTRLO));
1058 			SWO(bitmode, tmp1, RXRSRC_PTRHI,
1059 				SRO(bitmode, tmp2, RXRSRC_PTRHI));
1060 			SWO(bitmode, tmp1, RXRSRC_WCLO,
1061 				SRO(bitmode, tmp2, RXRSRC_WCLO));
1062 			SWO(bitmode, tmp1, RXRSRC_WCHI,
1063 				SRO(bitmode, tmp2, RXRSRC_WCHI));
1064 
1065 			/* zap old rra for fun */
1066 			SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
1067 			SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
1068 
1069 			sc->sc_rramark = (++rramark) & RRAMASK;
1070 			NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
1071 			wbflush();
1072 		}
1073 
1074 		/*
1075 		 * give receive descriptor back to chip simple
1076 		 * list is circular
1077 		 */
1078 		rdamark = sc->sc_rdamark;
1079 		SWO(bitmode, rda, RXPKT_INUSE, 1);
1080 		SWO(bitmode, rda, RXPKT_RLINK,
1081 			SRO(bitmode, rda, RXPKT_RLINK) | EOL);
1082 		SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1083 			SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
1084 			RXPKT_RLINK) & ~EOL);
1085 		sc->sc_rdamark = sc->sc_rxmark;
1086 
1087 		if (++sc->sc_rxmark >= sc->sc_nrda)
1088 			sc->sc_rxmark = 0;
1089 		rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1090 	}
1091 }
1092 
1093 /*
1094  * sonic_read -- pull packet off interface and forward to
1095  * appropriate protocol handler
1096  */
1097 static __inline__ int
1098 sonic_read(sc, pkt, len)
1099 	struct sn_softc *sc;
1100 	caddr_t pkt;
1101 	int len;
1102 {
1103 	struct ifnet *ifp = &sc->sc_if;
1104 	struct mbuf *m;
1105 
1106 #ifdef SNDEBUG
1107 	{
1108 		printf("%s: rcvd 0x%p len=%d type=0x%x from %s",
1109 		    sc->sc_dev.dv_xname, et, len, htons(et->ether_type),
1110 		    ether_sprintf(et->ether_shost));
1111 		printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
1112 	}
1113 #endif /* SNDEBUG */
1114 
1115 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
1116 	    len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
1117 		printf("%s: invalid packet length %d bytes\n",
1118 		    sc->sc_dev.dv_xname, len);
1119 		return (0);
1120 	}
1121 
1122 	m = sonic_get(sc, pkt, len);
1123 	if (m == NULL)
1124 		return (0);
1125 #if NBPFILTER > 0
1126 	/* Pass this up to any BPF listeners. */
1127 	if (ifp->if_bpf)
1128 		bpf_mtap(ifp->if_bpf, m);
1129 #endif
1130 	(*ifp->if_input)(ifp, m);
1131 	return (1);
1132 }
1133 
1134 /*
1135  * munge the received packet into an mbuf chain
1136  */
1137 static __inline__ struct mbuf *
1138 sonic_get(sc, pkt, datalen)
1139 	struct sn_softc *sc;
1140 	caddr_t pkt;
1141 	int datalen;
1142 {
1143 	struct	mbuf *m, *top, **mp;
1144 	int	len;
1145 
1146 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1147 	if (m == 0)
1148 		return (0);
1149 	m->m_pkthdr.rcvif = &sc->sc_if;
1150 	m->m_pkthdr.len = datalen;
1151 	len = MHLEN;
1152 	top = 0;
1153 	mp = &top;
1154 
1155 	while (datalen > 0) {
1156 		if (top) {
1157 			MGET(m, M_DONTWAIT, MT_DATA);
1158 			if (m == 0) {
1159 				m_freem(top);
1160 				return (0);
1161 			}
1162 			len = MLEN;
1163 		}
1164 		if (datalen >= MINCLSIZE) {
1165 			MCLGET(m, M_DONTWAIT);
1166 			if ((m->m_flags & M_EXT) == 0) {
1167 				if (top) m_freem(top);
1168 				return (0);
1169 			}
1170 			len = MCLBYTES;
1171 		}
1172 
1173 		if (mp == &top) {
1174 			caddr_t newdata = (caddr_t)
1175 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
1176 			    sizeof(struct ether_header);
1177 			len -= newdata - m->m_data;
1178 			m->m_data = newdata;
1179 		}
1180 
1181 		m->m_len = len = min(datalen, len);
1182 
1183 		bcopy(pkt, mtod(m, caddr_t), (unsigned) len);
1184 		pkt += len;
1185 		datalen -= len;
1186 		*mp = m;
1187 		mp = &m->m_next;
1188 	}
1189 
1190 	return (top);
1191 }
1192 
1193 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
1194 #define bbr(v)	((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
1195 
1196 void
1197 sn_get_enaddr(t, h, o, dst)
1198 	bus_space_tag_t	t;
1199 	bus_space_handle_t h;
1200 	bus_size_t o;
1201 	u_char *dst;
1202 {
1203 	int	i, do_bbr;
1204 	u_char	b;
1205 
1206 	/*
1207 	 * For reasons known only to Apple, MAC addresses in the ethernet
1208 	 * PROM are stored in Token Ring (IEEE 802.5) format, that is
1209 	 * with all of the bits in each byte reversed (canonical bit format).
1210 	 * When the address is read out it must be reversed to ethernet format
1211 	 * before use.
1212 	 *
1213 	 * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard
1214 	 * ethernet addresses on 68K machines should be in one of these
1215 	 * two ranges.
1216 	 *
1217 	 * Here is where it gets complicated.
1218 	 *
1219 	 * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM
1220 	 * written in standard ethernet format. The MacOS accounted for this
1221 	 * in these systems, and did not reverse the bytes. Some other
1222 	 * networking utilities were not so forgiving, and got confused.
1223 	 * "Some" of Apple's Nubus ethernet cards also had their bits
1224 	 * burned in ethernet format.
1225 	 *
1226 	 * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal
1227 	 * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed
1228 	 * their workaround and now reverses the bits regardless of
1229 	 * what kind of machine it is. So PMac systems and the affected
1230 	 * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they
1231 	 * were intended.
1232 	 *
1233 	 * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1
1234 	 * and MacOS System 7.5.3 FAQ (10/96)" for more details.
1235 	 */
1236 	do_bbr = 0;
1237 	b = bus_space_read_1(t, h, o);
1238 	if (b == 0x10)
1239 		do_bbr = 1;
1240 	dst[0] = (do_bbr) ? bbr(b) : b;
1241 
1242 	for (i = 1 ; i < ETHER_ADDR_LEN ; i++) {
1243 		b = bus_space_read_1(t, h, o+i);
1244 		dst[i] = (do_bbr) ? bbr(b) : b;
1245 	}
1246 }
1247