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