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