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