if_sn.c revision 1.20 1 /* $NetBSD: if_sn.c,v 1.20 1998/12/22 08:47:05 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 "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 <net/if.h>
32 #include <net/if_dl.h>
33 #include <net/if_ether.h>
34
35 #ifdef INET
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/in_var.h>
39 #include <netinet/ip.h>
40 #include <netinet/if_inarp.h>
41 #endif
42
43 #include <vm/vm.h>
44
45 #include "bpfilter.h"
46 #if NBPFILTER > 0
47 #include <net/bpf.h>
48 #include <net/bpfdesc.h>
49 #endif
50
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 #include <machine/viareg.h>
54 #include <mac68k/dev/if_snreg.h>
55 #include <mac68k/dev/if_snvar.h>
56
57 static void snwatchdog __P((struct ifnet *));
58 static int sninit __P((struct sn_softc *sc));
59 static int snstop __P((struct sn_softc *sc));
60 static int snioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
61 static void snstart __P((struct ifnet *ifp));
62 static void snreset __P((struct sn_softc *sc));
63
64 static void caminitialise __P((struct sn_softc *));
65 static void camentry __P((struct sn_softc *, int, u_char *ea));
66 static void camprogram __P((struct sn_softc *));
67 static void initialise_tda __P((struct sn_softc *));
68 static void initialise_rda __P((struct sn_softc *));
69 static void initialise_rra __P((struct sn_softc *));
70 #ifdef SNDEBUG
71 static void camdump __P((struct sn_softc *sc));
72 #endif
73
74 static void sonictxint __P((struct sn_softc *));
75 static void sonicrxint __P((struct sn_softc *));
76
77 static __inline__ u_int sonicput __P((struct sn_softc *sc, struct mbuf *m0,
78 int mtd_next));
79 static __inline__ int sonic_read __P((struct sn_softc *, caddr_t, int));
80 static __inline__ struct mbuf *sonic_get __P((struct sn_softc *,
81 struct ether_header *, int));
82
83 #undef assert
84 #undef _assert
85
86 #ifdef NDEBUG
87 #define assert(e) ((void)0)
88 #define _assert(e) ((void)0)
89 #else
90 #define _assert(e) assert(e)
91 #ifdef __STDC__
92 #define assert(e) ((e) ? (void)0 : __assert("sn ", __FILE__, __LINE__, #e))
93 #else /* PCC */
94 #define assert(e) ((e) ? (void)0 : __assert("sn "__FILE__, __LINE__, "e"))
95 #endif
96 #endif
97
98 int sndebug = 0;
99
100 /*
101 * SONIC buffers need to be aligned 16 or 32 bit aligned.
102 * These macros calculate and verify alignment.
103 */
104 #define ROUNDUP(p, N) (((int) p + N - 1) & ~(N - 1))
105
106 #define SOALIGN(m, array) (m ? (ROUNDUP(array, 4)) : (ROUNDUP(array, 2)))
107
108 #define LOWER(x) ((unsigned)(x) & 0xffff)
109 #define UPPER(x) ((unsigned)(x) >> 16)
110
111 /*
112 * Interface exists: make available by filling in network interface
113 * record. System will initialize the interface when it is ready
114 * to accept packets.
115 */
116 int
117 snsetup(sc, lladdr)
118 struct sn_softc *sc;
119 u_int8_t *lladdr;
120 {
121 struct ifnet *ifp = &sc->sc_if;
122 u_char *p;
123 u_char *pp;
124 int i;
125 int offset;
126
127 /*
128 * XXX if_sn.c is intended to be MI. Should it allocate memory
129 * for its descriptor areas, or expect the MD attach code
130 * to do that?
131 */
132 sc->space = malloc((SN_NPAGES + 1) * NBPG, M_DEVBUF, M_WAITOK);
133 if (sc->space == NULL) {
134 printf ("%s: memory allocation for descriptors failed\n",
135 sc->sc_dev.dv_xname);
136 return (1);
137 }
138
139 /*
140 * Put the pup in reset mode (sninit() will fix it later),
141 * stop the timer, disable all interrupts and clear any interrupts.
142 */
143 NIC_PUT(sc, SNR_CR, CR_STP);
144 wbflush();
145 NIC_PUT(sc, SNR_CR, CR_RST);
146 wbflush();
147 NIC_PUT(sc, SNR_IMR, 0);
148 wbflush();
149 NIC_PUT(sc, SNR_ISR, ISR_ALL);
150 wbflush();
151
152 /*
153 * because the SONIC is basically 16bit device it 'concatenates'
154 * a higher buffer address to a 16 bit offset--this will cause wrap
155 * around problems near the end of 64k !!
156 */
157 p = sc->space;
158 pp = (u_char *)ROUNDUP ((int)p, NBPG);
159 p = pp;
160
161 /*
162 * Disable caching on the SONIC's data space.
163 * The pages might not be physically contiguous, so set
164 * each page individually.
165 */
166 for (i = 0; i < SN_NPAGES; i++) {
167 physaccess (p, (caddr_t)SONIC_GETDMA(p), NBPG,
168 PG_V | PG_RW | PG_CI);
169 p += NBPG;
170 }
171 p = pp;
172
173 for (i = 0; i < NRRA; i++) {
174 sc->p_rra[i] = (void *)p;
175 sc->v_rra[i] = SONIC_GETDMA(p);
176 p += RXRSRC_SIZE(sc);
177 }
178 sc->v_rea = SONIC_GETDMA(p);
179
180 p = (u_char *)SOALIGN(sc, p);
181
182 sc->p_cda = (void *)(p);
183 sc->v_cda = SONIC_GETDMA(p);
184 p += CDA_SIZE(sc);
185
186 p = (u_char *)SOALIGN(sc, p);
187
188 for (i = 0; i < NTDA; i++) {
189 struct mtd *mtdp = &sc->mtda[i];
190 mtdp->mtd_txp = (void *)p;
191 mtdp->mtd_vtxp = SONIC_GETDMA(p);
192 p += TXP_SIZE(sc);
193 }
194
195 p = (u_char *)SOALIGN(sc, p);
196
197 if ((p - pp) > NBPG) {
198 printf ("%s: sizeof RRA (%ld) + CDA (%ld) +"
199 "TDA (%ld) > NBPG (%d). Punt!\n",
200 sc->sc_dev.dv_xname,
201 (ulong)sc->p_cda - (ulong)sc->p_rra[0],
202 (ulong)sc->mtda[0].mtd_txp - (ulong)sc->p_cda,
203 (ulong)p - (ulong)sc->mtda[0].mtd_txp,
204 NBPG);
205 return(1);
206 }
207
208 p = pp + NBPG;
209 pp = p;
210
211 sc->sc_nrda = NBPG / RXPKT_SIZE(sc);
212 sc->p_rda = (caddr_t) p;
213 sc->v_rda = SONIC_GETDMA(p);
214
215 p = pp + NBPG;
216
217 for (i = 0; i < NRBA; i++) {
218 sc->rbuf[i] = (caddr_t)p;
219 p += NBPG;
220 }
221
222 pp = p;
223 offset = TXBSIZE;
224 for (i = 0; i < NTDA; i++) {
225 struct mtd *mtdp = &sc->mtda[i];
226
227 mtdp->mtd_buf = p;
228 mtdp->mtd_vbuf = SONIC_GETDMA(p);
229 offset += TXBSIZE;
230 if (offset < NBPG) {
231 p += TXBSIZE;
232 } else {
233 p = pp + NBPG;
234 pp = p;
235 offset = TXBSIZE;
236 }
237 }
238
239 #ifdef SNDEBUG
240 camdump(sc);
241 #endif
242 printf(" address %s\n", ether_sprintf(lladdr));
243
244 #ifdef SNDEBUG
245 printf("%s: buffers: rra=%p cda=%p rda=%p tda=%p\n",
246 sc->sc_dev.dv_xname, sc->p_rra[0], sc->p_cda,
247 sc->p_rda, sc->mtda[0].mtd_txp);
248 #endif
249
250 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
251 ifp->if_softc = sc;
252 ifp->if_ioctl = snioctl;
253 ifp->if_start = snstart;
254 ifp->if_flags =
255 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
256 ifp->if_watchdog = snwatchdog;
257 #if NBPFILTER > 0
258 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
259 #endif
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 accessable 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 + sizeof(struct ether_header)) {
605 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
606 bzero(mtdp->mtd_buf + totlen, pad);
607 totlen = ETHERMIN + sizeof(struct ether_header);
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 seperate 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\n", 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(NBPG/2));
857 SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/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) -
1024 sizeof(struct ether_header) - FCSSIZE;
1025 if (status & RCR_PRX) {
1026 caddr_t pkt =
1027 sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
1028 if (sonic_read(sc, pkt, len))
1029 sc->sc_if.if_ipackets++;
1030 else
1031 sc->sc_if.if_ierrors++;
1032 } else
1033 sc->sc_if.if_ierrors++;
1034
1035 /*
1036 * give receive buffer area back to chip.
1037 *
1038 * If this was the last packet in the RRA, give the RRA to
1039 * the chip again.
1040 * If sonic read didnt copy it out then we would have to
1041 * wait !!
1042 * (dont bother add it back in again straight away)
1043 *
1044 * Really, we're doing p_rra[rramark] = p_rra[orra] but
1045 * we have to use the macros because SONIC might be in
1046 * 16 or 32 bit mode.
1047 */
1048 if (status & RCR_LPKT) {
1049 void *tmp1, *tmp2;
1050
1051 rramark = sc->sc_rramark;
1052 tmp1 = sc->p_rra[rramark];
1053 tmp2 = sc->p_rra[orra];
1054 SWO(bitmode, tmp1, RXRSRC_PTRLO,
1055 SRO(bitmode, tmp2, RXRSRC_PTRLO));
1056 SWO(bitmode, tmp1, RXRSRC_PTRHI,
1057 SRO(bitmode, tmp2, RXRSRC_PTRHI));
1058 SWO(bitmode, tmp1, RXRSRC_WCLO,
1059 SRO(bitmode, tmp2, RXRSRC_WCLO));
1060 SWO(bitmode, tmp1, RXRSRC_WCHI,
1061 SRO(bitmode, tmp2, RXRSRC_WCHI));
1062
1063 /* zap old rra for fun */
1064 SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
1065 SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
1066
1067 sc->sc_rramark = (++rramark) & RRAMASK;
1068 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
1069 wbflush();
1070 }
1071
1072 /*
1073 * give receive descriptor back to chip simple
1074 * list is circular
1075 */
1076 rdamark = sc->sc_rdamark;
1077 SWO(bitmode, rda, RXPKT_INUSE, 1);
1078 SWO(bitmode, rda, RXPKT_RLINK,
1079 SRO(bitmode, rda, RXPKT_RLINK) | EOL);
1080 SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1081 SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
1082 RXPKT_RLINK) & ~EOL);
1083 sc->sc_rdamark = sc->sc_rxmark;
1084
1085 if (++sc->sc_rxmark >= sc->sc_nrda)
1086 sc->sc_rxmark = 0;
1087 rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1088 }
1089 }
1090
1091 /*
1092 * sonic_read -- pull packet off interface and forward to
1093 * appropriate protocol handler
1094 */
1095 static __inline__ int
1096 sonic_read(sc, pkt, len)
1097 struct sn_softc *sc;
1098 caddr_t pkt;
1099 int len;
1100 {
1101 struct ifnet *ifp = &sc->sc_if;
1102 struct ether_header *et;
1103 struct mbuf *m;
1104
1105 /*
1106 * Get pointer to ethernet header (in input buffer).
1107 */
1108 et = (struct ether_header *)pkt;
1109
1110 #ifdef SNDEBUG
1111 {
1112 printf("%s: rcvd 0x%p len=%d type=0x%x from %s",
1113 sc->sc_dev.dv_xname, et, len, htons(et->ether_type),
1114 ether_sprintf(et->ether_shost));
1115 printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
1116 }
1117 #endif /* SNDEBUG */
1118
1119 if (len < ETHERMIN || len > ETHERMTU) {
1120 printf("%s: invalid packet length %d bytes\n",
1121 sc->sc_dev.dv_xname, len);
1122 return (0);
1123 }
1124
1125 #if NBPFILTER > 0
1126 /*
1127 * Check if there's a bpf filter listening on this interface.
1128 * If so, hand off the raw packet to enet, then discard things
1129 * not destined for us (but be sure to keep broadcast/multicast).
1130 */
1131 if (ifp->if_bpf) {
1132 bpf_tap(ifp->if_bpf, pkt,
1133 len + sizeof(struct ether_header));
1134 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1135 (et->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1136 bcmp(et->ether_dhost, LLADDR(ifp->if_sadl),
1137 sizeof(et->ether_dhost)) != 0)
1138 return (0);
1139 }
1140 #endif
1141 m = sonic_get(sc, et, len);
1142 if (m == NULL)
1143 return (0);
1144 ether_input(ifp, et, m);
1145 return (1);
1146 }
1147
1148 #define sonicdataaddr(eh, off, type) ((type)(((caddr_t)((eh) + 1) + (off))))
1149
1150 /*
1151 * munge the received packet into an mbuf chain
1152 */
1153 static __inline__ struct mbuf *
1154 sonic_get(sc, eh, datalen)
1155 struct sn_softc *sc;
1156 struct ether_header *eh;
1157 int datalen;
1158 {
1159 struct mbuf *m, *top, **mp;
1160 int len;
1161 caddr_t pkt = sonicdataaddr(eh, 0, caddr_t);
1162
1163 MGETHDR(m, M_DONTWAIT, MT_DATA);
1164 if (m == 0)
1165 return (0);
1166 m->m_pkthdr.rcvif = &sc->sc_if;
1167 m->m_pkthdr.len = datalen;
1168 len = MHLEN;
1169 top = 0;
1170 mp = ⊤
1171
1172 while (datalen > 0) {
1173 if (top) {
1174 MGET(m, M_DONTWAIT, MT_DATA);
1175 if (m == 0) {
1176 m_freem(top);
1177 return (0);
1178 }
1179 len = MLEN;
1180 }
1181 if (datalen >= MINCLSIZE) {
1182 MCLGET(m, M_DONTWAIT);
1183 if ((m->m_flags & M_EXT) == 0) {
1184 if (top) m_freem(top);
1185 return (0);
1186 }
1187 len = MCLBYTES;
1188 }
1189 m->m_len = len = min(datalen, len);
1190
1191 bcopy(pkt, mtod(m, caddr_t), (unsigned) len);
1192 pkt += len;
1193 datalen -= len;
1194 *mp = m;
1195 mp = &m->m_next;
1196 }
1197
1198 return (top);
1199 }
1200
1201 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
1202 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
1203
1204 void
1205 sn_get_enaddr(t, h, o, dst)
1206 bus_space_tag_t t;
1207 bus_space_handle_t h;
1208 bus_size_t o;
1209 u_char *dst;
1210 {
1211 int i, do_bbr;
1212 u_char b;
1213
1214 /*
1215 * For reasons known only to Apple, MAC addresses in the ethernet
1216 * PROM are stored in Token Ring (IEEE 802.5) format, that is
1217 * with all of the bits in each byte reversed (canonical bit format).
1218 * When the address is read out it must be reversed to ethernet format
1219 * before use.
1220 *
1221 * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard
1222 * ethernet addresses on 68K machines should be in one of these
1223 * two ranges.
1224 *
1225 * Here is where it gets complicated.
1226 *
1227 * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM
1228 * written in standard ethernet format. The MacOS accounted for this
1229 * in these systems, and did not reverse the bytes. Some other
1230 * networking utilities were not so forgiving, and got confused.
1231 * "Some" of Apple's Nubus ethernet cards also had their bits
1232 * burned in ethernet format.
1233 *
1234 * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal
1235 * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed
1236 * their workaround and now reverses the bits regardless of
1237 * what kind of machine it is. So PMac systems and the affected
1238 * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they
1239 * were intended.
1240 *
1241 * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1
1242 * and MacOS System 7.5.3 FAQ (10/96)" for more details.
1243 */
1244 do_bbr = 0;
1245 b = bus_space_read_1(t, h, o);
1246 if (b == 0x10)
1247 do_bbr = 1;
1248 dst[0] = (do_bbr) ? bbr(b) : b;
1249
1250 for (i = 1 ; i < ETHER_ADDR_LEN ; i++) {
1251 b = bus_space_read_1(t, h, o+i);
1252 dst[i] = (do_bbr) ? bbr(b) : b;
1253 }
1254 }
1255