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