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