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