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