if_sn.c revision 1.9 1 /* $NetBSD: if_sn.c,v 1.9 1997/04/10 03:22:45 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 typedef unsigned char uchar;
52
53 #include <machine/bus.h>
54 #include <machine/cpu.h>
55 #include <machine/viareg.h>
56 #include <mac68k/dev/if_snreg.h>
57 #include <mac68k/dev/if_snvar.h>
58
59 static void snwatchdog __P((struct ifnet *));
60 static int sninit __P((struct sn_softc *sc));
61 static int snstop __P((struct sn_softc *sc));
62 static inline int sonicput __P((struct sn_softc *sc, struct mbuf *m0));
63 static int snioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
64 static void snstart __P((struct ifnet *ifp));
65 static void snreset __P((struct sn_softc *sc));
66
67 void camdump __P((struct sn_softc *sc));
68
69 struct cfdriver sn_cd = {
70 NULL, "sn", DV_IFNET
71 };
72
73 #undef assert
74 #undef _assert
75
76 #ifdef NDEBUG
77 #define assert(e) ((void)0)
78 #define _assert(e) ((void)0)
79 #else
80 #define _assert(e) assert(e)
81 #ifdef __STDC__
82 #define assert(e) ((e) ? (void)0 : __assert("sn ", __FILE__, __LINE__, #e))
83 #else /* PCC */
84 #define assert(e) ((e) ? (void)0 : __assert("sn "__FILE__, __LINE__, "e"))
85 #endif
86 #endif
87
88 int ethdebug = 0;
89
90 /*
91 * SONIC buffers need to be aligned 16 or 32 bit aligned.
92 * These macros calculate and verify alignment.
93 */
94 #define ROUNDUP(p, N) (((int) p + N - 1) & ~(N - 1))
95
96 #define SOALIGN(m, array) (m ? (ROUNDUP(array, 4)) : (ROUNDUP(array, 2)))
97
98 #define LOWER(x) ((unsigned)(x) & 0xffff)
99 #define UPPER(x) ((unsigned)(x) >> 16)
100
101 /*
102 * Interface exists: make available by filling in network interface
103 * record. System will initialize the interface when it is ready
104 * to accept packets.
105 */
106 int
107 snsetup(sc, lladdr)
108 struct sn_softc *sc;
109 u_int8_t *lladdr;
110 {
111 struct ifnet *ifp = &sc->sc_if;
112 unsigned char *p;
113 unsigned char *pp;
114 int i;
115
116
117 /*
118 * XXX if_sn.c is intended to be MI. Should it allocate memory
119 * for its descriptor areas, or expect the MD attach code
120 * to do that?
121 */
122 sc->space = malloc((SN_NPAGES + 1) * NBPG, M_DEVBUF, M_WAITOK);
123 if (sc->space == NULL) {
124 printf ("%s: memory allocation for descriptors failed\n",
125 sc->sc_dev.dv_xname);
126 return (1);
127 }
128
129 /*
130 * Put the pup in reset mode (sninit() will fix it later),
131 * stop the timer, disable all interrupts and clear any interrupts.
132 */
133 NIC_PUT(sc, SNR_CR, CR_STP);
134 wbflush();
135 NIC_PUT(sc, SNR_CR, CR_RST);
136 wbflush();
137 NIC_PUT(sc, SNR_IMR, 0);
138 wbflush();
139 NIC_PUT(sc, SNR_ISR, ISR_ALL);
140 wbflush();
141
142 /*
143 * because the SONIC is basically 16bit device it 'concatenates'
144 * a higher buffer address to a 16 bit offset--this will cause wrap
145 * around problems near the end of 64k !!
146 */
147 p = sc->space;
148 pp = (unsigned char *)ROUNDUP ((int)p, NBPG);
149 p = pp;
150
151 /*
152 * Disable caching on the SONIC's data space.
153 * The pages might not be physically contiguous, so set
154 * each page individually.
155 */
156 for (i = 0; i < SN_NPAGES; i++) {
157 physaccess (p, (caddr_t) kvtop(p), NBPG,
158 PG_V | PG_RW | PG_CI);
159 p += NBPG;
160 }
161 p = pp;
162
163 for (i = 0; i < NRRA; i++) {
164 sc->p_rra[i] = (void *)p;
165 sc->v_rra[i] = kvtop(p);
166 p += RXRSRC_SIZE(sc);
167 }
168 sc->v_rea = kvtop(p);
169
170 p = (unsigned char *)SOALIGN(sc, p);
171
172 sc->p_cda = (void *) (p);
173 sc->v_cda = kvtop(p);
174 p += CDA_SIZE(sc);
175
176 p = (unsigned char *)SOALIGN(sc, p);
177
178 for (i = 0; i < NRDA; i++) {
179 sc->p_rda[i] = (void *) p;
180 sc->v_rda[i] = kvtop(p);
181 p += RXPKT_SIZE(sc);
182 }
183
184 p = (unsigned char *)SOALIGN(sc, p);
185
186 for (i = 0; i < NTDA; i++) {
187 struct mtd *mtdp = &sc->mtda[i];
188 mtdp->mtd_txp = (void *)p;
189 mtdp->mtd_vtxp = kvtop(p);
190 p += TXP_SIZE(sc);
191 }
192
193 p = (unsigned char *)SOALIGN(sc, p);
194
195 if ((p - pp) > NBPG) {
196 printf ("%s: sizeof RRA (%ld) + CDA (%ld) +"
197 "RDA (%ld) + TDA (%ld) > NBPG (%d). Punt!\n",
198 sc->sc_dev.dv_xname,
199 (ulong)sc->p_cda - (ulong)sc->p_rra[0],
200 (ulong)sc->p_rda[0] - (ulong)sc->p_cda,
201 (ulong)sc->mtda[0].mtd_txp - (ulong)sc->p_rda[0],
202 (ulong)p - (ulong)sc->mtda[0].mtd_txp,
203 NBPG);
204 return(1);
205 }
206
207 p = pp + NBPG;
208
209 for (i = 0; i < NRBA; i++) {
210 sc->rbuf[i] = (caddr_t) p;
211 p += NBPG;
212 }
213
214 for (i = 0; i < NTXB; i+=2) {
215 sc->tbuf[i] = (caddr_t) p;
216 sc->tbuf[i+1] = (caddr_t)(p + (NBPG/2));
217 sc->vtbuf[i] = kvtop(sc->tbuf[i]);
218 sc->vtbuf[i+1] = kvtop(sc->tbuf[i+1]);
219 p += NBPG;
220 }
221
222 #if 0
223 camdump(sc);
224 #endif
225 printf(" address %s\n", ether_sprintf(lladdr));
226
227 #if 0
228 printf("sonic buffers: rra=%p cda=0x%x rda=0x%x tda=0x%x\n",
229 sc->p_rra[0], sc->p_cda, sc->p_rda[0], sc->mtda[0].mtd_txp);
230 #endif
231
232 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
233 ifp->if_softc = sc;
234 ifp->if_ioctl = snioctl;
235 ifp->if_start = snstart;
236 ifp->if_flags =
237 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
238 ifp->if_watchdog = snwatchdog;
239 #if NBPFILTER > 0
240 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
241 #endif
242 if_attach(ifp);
243 ether_ifattach(ifp, lladdr);
244
245 return (0);
246 }
247
248 static int
249 snioctl(ifp, cmd, data)
250 struct ifnet *ifp;
251 u_long cmd;
252 caddr_t data;
253 {
254 struct ifaddr *ifa;
255 struct ifreq *ifr;
256 struct sn_softc *sc = ifp->if_softc;
257 int s = splnet(), err = 0;
258 int temp;
259
260 switch (cmd) {
261
262 case SIOCSIFADDR:
263 ifa = (struct ifaddr *)data;
264 ifp->if_flags |= IFF_UP;
265 switch (ifa->ifa_addr->sa_family) {
266 #ifdef INET
267 case AF_INET:
268 (void)sninit(ifp->if_softc);
269 arp_ifinit(ifp, ifa);
270 break;
271 #endif
272 default:
273 (void)sninit(ifp->if_softc);
274 break;
275 }
276 break;
277
278 case SIOCSIFFLAGS:
279 if ((ifp->if_flags & IFF_UP) == 0 &&
280 ifp->if_flags & IFF_RUNNING) {
281 snstop(ifp->if_softc);
282 ifp->if_flags &= ~IFF_RUNNING;
283 } else if (ifp->if_flags & IFF_UP &&
284 (ifp->if_flags & IFF_RUNNING) == 0)
285 (void)sninit(ifp->if_softc);
286 /*
287 * If the state of the promiscuous bit changes, the interface
288 * must be reset to effect the change.
289 */
290 if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
291 (ifp->if_flags & IFF_RUNNING)) {
292 sc->sc_iflags = ifp->if_flags;
293 printf("change in flags\n");
294 temp = sc->sc_if.if_flags & IFF_UP;
295 snreset(sc);
296 sc->sc_if.if_flags |= temp;
297 snstart(ifp);
298 }
299 break;
300
301 case SIOCADDMULTI:
302 case SIOCDELMULTI:
303 ifr = (struct ifreq *) data;
304 if (cmd == SIOCADDMULTI)
305 err = ether_addmulti(ifr, &sc->sc_ethercom);
306 else
307 err = ether_delmulti(ifr, &sc->sc_ethercom);
308
309 if (err == ENETRESET) {
310 /*
311 * Multicast list has changed; set the hardware
312 * filter accordingly. But remember UP flag!
313 */
314 temp = sc->sc_if.if_flags & IFF_UP;
315 snreset(sc);
316 sc->sc_if.if_flags |= temp;
317 err = 0;
318 }
319 break;
320 default:
321 err = EINVAL;
322 }
323 splx(s);
324 return (err);
325 }
326
327 /*
328 * Encapsulate a packet of type family for the local net.
329 */
330 static void
331 snstart(ifp)
332 struct ifnet *ifp;
333 {
334 struct sn_softc *sc = ifp->if_softc;
335 struct mbuf *m;
336 int len;
337
338 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
339 return;
340
341 outloop:
342 /* Check for room in the xmit buffer. */
343 if (sc->txb_inuse == sc->txb_cnt) {
344 ifp->if_flags |= IFF_OACTIVE;
345 return;
346 }
347
348 IF_DEQUEUE(&ifp->if_snd, m);
349 if (m == 0)
350 return;
351
352 /* We need the header for m_pkthdr.len. */
353 if ((m->m_flags & M_PKTHDR) == 0)
354 panic("snstart: no header mbuf");
355
356 #if NBPFILTER > 0
357 /*
358 * If bpf is listening on this interface, let it
359 * see the packet before we commit it to the wire.
360 */
361 if (ifp->if_bpf)
362 bpf_mtap(ifp->if_bpf, m);
363 #endif
364
365 /*
366 * If there is nothing in the o/p queue, and there is room in
367 * the Tx ring, then send the packet directly. Otherwise append
368 * it to the o/p queue.
369 */
370 if ((len = sonicput(sc, m)) > 0) {
371 len = m->m_pkthdr.len;
372 m_freem(m);
373 } else {
374 IF_PREPEND(&ifp->if_snd, m);
375 return;
376 }
377
378 /* Point to next buffer slot and wrap if necessary. */
379 if (++sc->txb_new == sc->txb_cnt)
380 sc->txb_new = 0;
381
382 sc->txb_inuse++;
383
384 ifp->if_opackets++; /* # of pkts */
385 sc->sc_sum.ls_opacks++; /* # of pkts */
386
387 /* Jump back for possibly more punishment. */
388 goto outloop;
389 }
390
391 /*
392 * This is called from sonicioctl() when /etc/ifconfig is run to set
393 * the address or switch the i/f on.
394 */
395 static void caminitialise __P((struct sn_softc *));
396 static void camentry __P((struct sn_softc *, int, unsigned char *ea));
397 static void camprogram __P((struct sn_softc *));
398 static void initialise_tda __P((struct sn_softc *));
399 static void initialise_rda __P((struct sn_softc *));
400 static void initialise_rra __P((struct sn_softc *));
401 static void initialise_tba __P((struct sn_softc *));
402
403 /*
404 * reset and restart the SONIC. Called in case of fatal
405 * hardware/software errors.
406 */
407 static void
408 snreset(sc)
409 struct sn_softc *sc;
410 {
411 snstop(sc);
412 sninit(sc);
413 }
414
415 static int
416 sninit(sc)
417 struct sn_softc *sc;
418 {
419 int s;
420 unsigned long s_rcr;
421
422 if (sc->sc_if.if_flags & IFF_RUNNING)
423 /* already running */
424 return (0);
425
426 s = splnet();
427
428 NIC_PUT(sc, SNR_CR, CR_RST); /* DCR only accessable in reset mode! */
429
430 /* config it */
431 NIC_PUT(sc, SNR_DCR, sc->snr_dcr);
432 NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
433
434 s_rcr = RCR_BRD | RCR_LBNONE;
435 if (sc->sc_if.if_flags & IFF_PROMISC)
436 s_rcr |= RCR_PRO;
437 if (sc->sc_if.if_flags & IFF_ALLMULTI)
438 s_rcr |= RCR_AMC;
439 NIC_PUT(sc, SNR_RCR, s_rcr);
440
441 NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
442
443 /* clear pending interrupts */
444 NIC_PUT(sc, SNR_ISR, ISR_ALL);
445
446 /* clear tally counters */
447 NIC_PUT(sc, SNR_CRCT, -1);
448 NIC_PUT(sc, SNR_FAET, -1);
449 NIC_PUT(sc, SNR_MPT, -1);
450
451 initialise_tda(sc);
452 initialise_rda(sc);
453 initialise_rra(sc);
454 initialise_tba(sc);
455
456 /* enable the chip */
457 NIC_PUT(sc, SNR_CR, 0);
458 wbflush();
459
460 /* program the CAM */
461 camprogram(sc);
462
463 /* get it to read resource descriptors */
464 NIC_PUT(sc, SNR_CR, CR_RRRA);
465 wbflush();
466 while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
467 continue;
468
469 /* enable rx */
470 NIC_PUT(sc, SNR_CR, CR_RXEN);
471 wbflush();
472
473 /* flag interface as "running" */
474 sc->sc_if.if_flags |= IFF_RUNNING;
475
476 splx(s);
477 return (0);
478 }
479
480 /*
481 * close down an interface and free its buffers
482 * Called on final close of device, or if sninit() fails
483 * part way through.
484 */
485 static int
486 snstop(sc)
487 struct sn_softc *sc;
488 {
489 struct mtd *mtd;
490 int s = splnet();
491
492 /* stick chip in reset */
493 NIC_PUT(sc, SNR_CR, CR_RST);
494 wbflush();
495
496 /* free all receive buffers (currently static so nothing to do) */
497
498 /* free all pending transmit mbufs */
499 while (sc->mtd_hw != sc->mtd_free) {
500 mtd = &sc->mtda[sc->mtd_hw];
501 mtd->mtd_buf = 0;
502 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
503 }
504 sc->txb_inuse = 0;
505
506 sc->sc_if.if_timer = 0;
507 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
508
509 splx(s);
510 return (0);
511 }
512
513 /*
514 * Called if any Tx packets remain unsent after 5 seconds,
515 * In all cases we just reset the chip, and any retransmission
516 * will be handled by higher level protocol timeouts.
517 */
518 static void
519 snwatchdog(ifp)
520 struct ifnet *ifp;
521 {
522 struct sn_softc *sc = ifp->if_softc;
523 struct mtd *mtd;
524 int temp;
525
526 if (sc->mtd_hw != sc->mtd_free) {
527 /* something still pending for transmit */
528 mtd = &sc->mtda[sc->mtd_hw];
529 if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
530 log(LOG_ERR, "%s: Tx - timeout\n",
531 sc->sc_dev.dv_xname);
532 else
533 log(LOG_ERR, "%s: Tx - lost interrupt\n",
534 sc->sc_dev.dv_xname);
535 temp = ifp->if_flags & IFF_UP;
536 snreset(sc);
537 ifp->if_flags |= temp;
538 }
539 }
540
541 /*
542 * stuff packet into sonic (at splnet)
543 */
544 static inline int
545 sonicput(sc, m0)
546 struct sn_softc *sc;
547 struct mbuf *m0;
548 {
549 unsigned char *buff, *buffer;
550 void *txp;
551 struct mtd *mtdp;
552 struct mbuf *m;
553 unsigned int len = 0;
554 unsigned int totlen = 0;
555 int mtd_free = sc->mtd_free;
556 int mtd_next;
557 int txb_new = sc->txb_new;
558
559 if (NIC_GET(sc, SNR_CR) & CR_TXP) {
560 return (0);
561 }
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
573 /* We are guaranteed, if we get here, that the xmit buffer is free. */
574 buff = buffer = sc->tbuf[txb_new];
575
576 /* this packet goes to mtdnext fill in the TDA */
577 mtdp->mtd_buf = buffer;
578 txp = mtdp->mtd_txp;
579 SWO(sc->bitmode, txp, TXP_CONFIG, 0);
580
581 for (m = m0; m; m = m->m_next) {
582 unsigned char *data = mtod(m, u_char *);
583 len = m->m_len;
584 totlen += len;
585 bcopy(data, buff, len);
586 buff += len;
587 }
588 if (totlen >= TXBSIZE) {
589 panic("packet overflow in sonicput.");
590 }
591 SWO(sc->bitmode, txp, TXP_FRAGOFF+(0*TXP_FRAGSIZE)+TXP_FPTRLO,
592 LOWER(sc->vtbuf[txb_new]));
593 SWO(sc->bitmode, txp, TXP_FRAGOFF+(0*TXP_FRAGSIZE)+TXP_FPTRHI,
594 UPPER(sc->vtbuf[txb_new]));
595
596 if (totlen < ETHERMIN + sizeof(struct ether_header)) {
597 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
598 bzero(buffer + totlen, pad);
599 totlen = ETHERMIN + sizeof(struct ether_header);
600 }
601
602 SWO(sc->bitmode, txp, TXP_FRAGOFF+(0*TXP_FRAGSIZE)+TXP_FSIZE,
603 totlen);
604 SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
605 SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
606
607 /* link onto the next mtd that will be used */
608 SWO(sc->bitmode, txp, TXP_FRAGOFF+(1*TXP_FRAGSIZE)+TXP_FPTRLO,
609 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
610
611 /*
612 * The previous txp.tlink currently contains a pointer to
613 * our txp | EOL. Want to clear the EOL, so write our
614 * pointer to the previous txp.
615 */
616 SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
617 LOWER(mtdp->mtd_vtxp));
618
619 sc->mtd_prev = mtd_free;
620 sc->mtd_free = mtd_next;
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 static void sonictxint __P((struct sn_softc *));
632 static void sonicrxint __P((struct sn_softc *));
633
634 static inline int sonic_read __P((struct sn_softc *, caddr_t, int));
635 static inline struct mbuf *sonic_get __P((struct sn_softc *, struct ether_header *, int));
636
637 /*
638 * CAM support
639 */
640 static void
641 caminitialise(sc)
642 struct sn_softc *sc;
643 {
644 int i;
645 void *p_cda = sc->p_cda;
646 int bitmode = sc->bitmode;
647 int camoffset;
648
649 for (i = 0; i < MAXCAM; i++) {
650 camoffset = i * CDA_CAMDESC;
651 SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
652 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
653 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
654 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
655 }
656 SWO(bitmode, p_cda, CDA_ENABLE, 0);
657 }
658
659 static void
660 camentry(sc, entry, ea)
661 int entry;
662 unsigned char *ea;
663 struct sn_softc *sc;
664 {
665 int bitmode = sc->bitmode;
666 void *p_cda = sc->p_cda;
667 int camoffset = entry * CDA_CAMDESC;
668
669 SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
670 SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
671 SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
672 SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
673 SWO(bitmode, p_cda, CDA_ENABLE,
674 (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
675 }
676
677 static void
678 camprogram(sc)
679 struct sn_softc *sc;
680 {
681 int timeout;
682 int mcount = 0;
683 struct ether_multi *enm;
684 struct ether_multistep step;
685 struct ifnet *ifp;
686
687 caminitialise(sc);
688
689 ifp = &sc->sc_if;
690
691 /* Always load our own address first. */
692 camentry (sc, mcount, LLADDR(ifp->if_sadl));
693 mcount++;
694
695 /* Assume we won't need allmulti bit. */
696 ifp->if_flags &= ~IFF_ALLMULTI;
697
698 /* Loop through multicast addresses */
699 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
700 while (enm != NULL) {
701 if (mcount == MAXCAM) {
702 ifp->if_flags |= IFF_ALLMULTI;
703 break;
704 }
705
706 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
707 sizeof(enm->enm_addrlo)) != 0) {
708 /*
709 * SONIC's CAM is programmed with specific
710 * addresses. It has no way to specify a range.
711 * (Well, thats not exactly true. If the
712 * range is small one could program each addr
713 * within the range as a seperate CAM entry)
714 */
715 ifp->if_flags |= IFF_ALLMULTI;
716 break;
717 }
718
719 /* program the CAM with the specified entry */
720 camentry(sc, mcount, enm->enm_addrlo);
721 mcount++;
722
723 ETHER_NEXT_MULTI(step, enm);
724 }
725
726 NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda));
727 NIC_PUT(sc, SNR_CDC, MAXCAM);
728 NIC_PUT(sc, SNR_CR, CR_LCAM);
729 wbflush();
730
731 timeout = 10000;
732 while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--)
733 continue;
734 if (timeout == 0) {
735 /* XXX */
736 panic("%s: CAM initialisation failed\n",
737 sc->sc_dev.dv_xname);
738 }
739 timeout = 10000;
740 while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--)
741 continue;
742
743 if (NIC_GET(sc, SNR_ISR) & ISR_LCD)
744 NIC_PUT(sc, SNR_ISR, ISR_LCD);
745 else
746 printf("%s: CAM initialisation without interrupt\n",
747 sc->sc_dev.dv_xname);
748 }
749
750 #if 0
751 static void
752 camdump(sc)
753 struct sn_softc *sc;
754 {
755 int i;
756
757 printf("CAM entries:\n");
758 NIC_PUT(sc, SNR_CR, CR_RST);
759 wbflush();
760
761 for (i = 0; i < 16; i++) {
762 ushort ap2, ap1, ap0;
763 NIC_PUT(sc, SNR_CEP, i);
764 wbflush();
765 ap2 = NIC_GET(sc, SNR_CAP2);
766 ap1 = NIC_GET(sc, SNR_CAP1);
767 ap0 = NIC_GET(sc, SNR_CAP0);
768 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
769 }
770 printf("CAM enable 0x%lx\n", NIC_GET(sc, SNR_CEP));
771
772 NIC_PUT(sc, SNR_CR, 0);
773 wbflush();
774 }
775 #endif
776
777 static void
778 initialise_tda(sc)
779 struct sn_softc *sc;
780 {
781 struct mtd *mtd;
782 int i;
783
784 for (i = 0; i < NTDA; i++) {
785 mtd = &sc->mtda[i];
786 mtd->mtd_buf = 0;
787 }
788
789 sc->mtd_hw = 0;
790 sc->mtd_prev = NTDA-1;
791 sc->mtd_free = 0;
792 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
793
794 NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
795 NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
796 }
797
798 static void
799 initialise_rda(sc)
800 struct sn_softc *sc;
801 {
802 int bitmode = sc->bitmode;
803 int i;
804
805 /* link the RDA's together into a circular list */
806 for (i = 0; i < (NRDA - 1); i++) {
807 SWO(bitmode, sc->p_rda[i], RXPKT_RLINK, LOWER(sc->v_rda[i+1]));
808 SWO(bitmode, sc->p_rda[i], RXPKT_INUSE, 1);
809 }
810 SWO(bitmode, sc->p_rda[NRDA - 1], RXPKT_RLINK, LOWER(sc->v_rda[0]) | EOL);
811 SWO(bitmode, sc->p_rda[NRDA - 1], RXPKT_INUSE, 1);
812
813 /* mark end of receive descriptor list */
814 sc->sc_rdamark = NRDA - 1;
815
816 sc->sc_rxmark = 0;
817
818 NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda[0]));
819 NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda[0]));
820 wbflush();
821 }
822
823 static void
824 initialise_rra(sc)
825 struct sn_softc *sc;
826 {
827 int i;
828 unsigned int v;
829 int bitmode = sc->bitmode;
830
831 if (bitmode)
832 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
833 else
834 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
835
836 NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
837 NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
838 /* rea must point just past the end of the rra space */
839 NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
840 NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
841
842 /* fill up SOME of the rra with buffers */
843 for (i = 0; i < NRBA; i++) {
844 v = kvtop(sc->rbuf[i]);
845 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
846 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
847 SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
848 SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
849 }
850 sc->sc_rramark = NRBA;
851 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
852 wbflush();
853 }
854
855 static void
856 initialise_tba(sc)
857 struct sn_softc *sc;
858 {
859 sc->txb_cnt = NTXB;
860 sc->txb_inuse = 0;
861 sc->txb_new = 0;
862 }
863
864 void
865 snintr(arg, slot)
866 void *arg;
867 int slot;
868 {
869 struct sn_softc *sc = (struct sn_softc *)arg;
870 int isr;
871
872 while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
873 /* scrub the interrupts that we are going to service */
874 NIC_PUT(sc, SNR_ISR, isr);
875 wbflush();
876
877 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
878 printf("%s: unexpected interrupt status 0x%x\n",
879 sc->sc_dev.dv_xname, isr);
880
881 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
882 sonictxint(sc);
883
884 if (isr & ISR_PKTRX)
885 sonicrxint(sc);
886
887 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
888 if (isr & ISR_HBL)
889 /*
890 * The repeater is not providing a heartbeat.
891 * In itself this isn't harmful, lots of the
892 * cheap repeater hubs don't supply a heartbeat.
893 * So ignore the lack of heartbeat. Its only
894 * if we can't detect a carrier that we have a
895 * problem.
896 */
897 ;
898 if (isr & ISR_RDE)
899 printf("%s: receive descriptors exhausted\n",
900 sc->sc_dev.dv_xname);
901 if (isr & ISR_RBE)
902 printf("%s: receive buffers exhausted\n",
903 sc->sc_dev.dv_xname);
904 if (isr & ISR_RBAE)
905 printf("%s: receive buffer area exhausted\n",
906 sc->sc_dev.dv_xname);
907 if (isr & ISR_RFO)
908 printf("%s: receive FIFO overrun\n",
909 sc->sc_dev.dv_xname);
910 }
911 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
912 #ifdef notdef
913 if (isr & ISR_CRC)
914 sc->sc_crctally++;
915 if (isr & ISR_FAE)
916 sc->sc_faetally++;
917 if (isr & ISR_MP)
918 sc->sc_mptally++;
919 #endif
920 }
921 snstart(&sc->sc_if);
922 }
923 return;
924 }
925
926 /*
927 * Transmit interrupt routine
928 */
929 static void
930 sonictxint(sc)
931 struct sn_softc *sc;
932 {
933 void *txp;
934 struct mtd *mtd;
935 /* XXX DG make mtd_hw a local var */
936
937 if (sc->mtd_hw == sc->mtd_free)
938 return;
939
940 while (sc->mtd_hw != sc->mtd_free) {
941 mtd = &sc->mtda[sc->mtd_hw];
942 if (mtd->mtd_buf == 0)
943 break;
944
945 txp = mtd->mtd_txp;
946
947 if (SRO(sc->bitmode, txp, TXP_STATUS) == 0)
948 return; /* it hasn't really gone yet */
949
950 if (ethdebug) {
951 struct ether_header *eh;
952
953 eh = (struct ether_header *) mtd->mtd_buf;
954 printf("xmit status=0x%x len=%d type=0x%x from %s",
955 SRO(sc->bitmode, txp, TXP_STATUS),
956 SRO(sc->bitmode, txp, TXP_PKTSIZE),
957 htons(eh->ether_type),
958 ether_sprintf(eh->ether_shost));
959 printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
960 }
961 sc->txb_inuse--;
962 mtd->mtd_buf = 0;
963 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
964
965 /* XXX - Do stats here. */
966
967 if ((SRO(sc->bitmode, txp, TXP_STATUS) & TCR_PTX) == 0) {
968 printf("%s: Tx packet status=0x%x\n",
969 sc->sc_dev.dv_xname,
970 SRO(sc->bitmode, txp, TXP_STATUS));
971
972 /* XXX - DG This looks bogus */
973 if (sc->mtd_hw != sc->mtd_free) {
974 printf("resubmitting remaining packets\n");
975 mtd = &sc->mtda[sc->mtd_hw];
976 NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
977 NIC_PUT(sc, SNR_CR, CR_TXP);
978 wbflush();
979 return;
980 }
981 }
982 }
983 }
984
985 /*
986 * Receive interrupt routine
987 */
988 static void
989 sonicrxint(sc)
990 struct sn_softc *sc;
991 {
992 void *rda;
993 int orra;
994 int len;
995 int rramark;
996 int rdamark;
997 int bitmode = sc->bitmode;
998 u_int16_t rxpkt_ptr;
999
1000 rda = sc->p_rda[sc->sc_rxmark];
1001
1002 while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
1003 unsigned status = SRO(bitmode, rda, RXPKT_STATUS);
1004
1005 orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
1006 rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
1007 len = SRO(bitmode, rda, RXPKT_BYTEC) -
1008 sizeof(struct ether_header) - FCSSIZE;
1009 if (status & RCR_PRX) {
1010 caddr_t pkt = 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 (ethdebug) {
1094 printf("rcvd 0x%p len=%d type=0x%x from %s",
1095 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