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