if_sn.c revision 1.24 1 /* $NetBSD: if_sn.c,v 1.24 2007/06/17 05:53:02 tsutsui 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 2007/06/17 05:53:02 tsutsui 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 ifreq *ifr;
248 struct sn_softc *sc = ifp->if_softc;
249 int s = splnet(), err = 0;
250 int temp;
251
252 switch (cmd) {
253
254 case SIOCSIFADDR:
255 ifa = (struct ifaddr *)data;
256 ifp->if_flags |= IFF_UP;
257 switch (ifa->ifa_addr->sa_family) {
258 #ifdef INET
259 case AF_INET:
260 (void)sninit(sc);
261 arp_ifinit(ifp, ifa);
262 break;
263 #endif
264 default:
265 (void)sninit(sc);
266 break;
267 }
268 break;
269
270 case SIOCSIFFLAGS:
271 if ((ifp->if_flags & IFF_UP) == 0 &&
272 (ifp->if_flags & IFF_RUNNING) != 0) {
273 /*
274 * If interface is marked down and it is running,
275 * then stop it.
276 */
277 snstop(sc);
278 ifp->if_flags &= ~IFF_RUNNING;
279 } else if ((ifp->if_flags & IFF_UP) != 0 &&
280 (ifp->if_flags & IFF_RUNNING) == 0) {
281 /*
282 * If interface is marked up and it is stopped,
283 * then start it.
284 */
285 (void)sninit(sc);
286 } else {
287 /*
288 * reset the interface to pick up any other changes
289 * in flags
290 */
291 temp = ifp->if_flags & IFF_UP;
292 snreset(sc);
293 ifp->if_flags |= temp;
294 snstart(ifp);
295 }
296 break;
297
298 case SIOCADDMULTI:
299 case SIOCDELMULTI:
300 ifr = (struct ifreq *) data;
301 if (cmd == SIOCADDMULTI)
302 err = ether_addmulti(ifr, &sc->sc_ethercom);
303 else
304 err = ether_delmulti(ifr, &sc->sc_ethercom);
305
306 if (err == ENETRESET) {
307 /*
308 * Multicast list has changed; set the hardware
309 * filter accordingly. But remember UP flag!
310 */
311 if (ifp->if_flags & IFF_RUNNING) {
312 temp = ifp->if_flags & IFF_UP;
313 snreset(sc);
314 ifp->if_flags |= temp;
315 }
316 err = 0;
317 }
318 break;
319 default:
320 err = EINVAL;
321 }
322 splx(s);
323 return err;
324 }
325
326 /*
327 * Encapsulate a packet of type family for the local net.
328 */
329 static void
330 snstart(struct ifnet *ifp)
331 {
332 struct sn_softc *sc = ifp->if_softc;
333 struct mbuf *m;
334 int mtd_next;
335
336 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
337 return;
338
339 outloop:
340 /* Check for room in the xmit buffer. */
341 if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
342 mtd_next = 0;
343
344 if (mtd_next == sc->mtd_hw) {
345 ifp->if_flags |= IFF_OACTIVE;
346 return;
347 }
348
349 IF_DEQUEUE(&ifp->if_snd, m);
350 if (m == 0)
351 return;
352
353 /* We need the header for m_pkthdr.len. */
354 if ((m->m_flags & M_PKTHDR) == 0)
355 panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname);
356
357 #if NBPFILTER > 0
358 /*
359 * If bpf is listening on this interface, let it
360 * see the packet before we commit it to the wire.
361 */
362 if (ifp->if_bpf)
363 bpf_mtap(ifp->if_bpf, m);
364 #endif
365
366 /*
367 * If there is nothing in the o/p queue, and there is room in
368 * the Tx ring, then send the packet directly. Otherwise append
369 * it to the o/p queue.
370 */
371 if ((sonicput(sc, m, mtd_next)) == 0) {
372 IF_PREPEND(&ifp->if_snd, m);
373 return;
374 }
375
376 sc->mtd_prev = sc->mtd_free;
377 sc->mtd_free = mtd_next;
378
379 ifp->if_opackets++; /* # of pkts */
380
381 /* Jump back for possibly more punishment. */
382 goto outloop;
383 }
384
385 /*
386 * reset and restart the SONIC. Called in case of fatal
387 * hardware/software errors.
388 */
389 static void
390 snreset(struct sn_softc *sc)
391 {
392
393 snstop(sc);
394 sninit(sc);
395 }
396
397 static int
398 sninit(struct sn_softc *sc)
399 {
400 u_long s_rcr;
401 int s;
402
403 if (sc->sc_if.if_flags & IFF_RUNNING)
404 /* already running */
405 return 0;
406
407 s = splnet();
408
409 NIC_PUT(sc, SNR_CR, CR_RST); /* DCR only accessible in reset mode! */
410
411 /* config it */
412 NIC_PUT(sc, SNR_DCR, (sc->snr_dcr |
413 (sc->bitmode ? DCR_DW32 : DCR_DW16)));
414 NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
415
416 s_rcr = RCR_BRD | RCR_LBNONE;
417 if (sc->sc_if.if_flags & IFF_PROMISC)
418 s_rcr |= RCR_PRO;
419 if (sc->sc_if.if_flags & IFF_ALLMULTI)
420 s_rcr |= RCR_AMC;
421 NIC_PUT(sc, SNR_RCR, s_rcr);
422
423 #if 0
424 NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
425 #else
426 NIC_PUT(sc, SNR_IMR, IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN |
427 IMR_BREN | IMR_HBLEN | IMR_RDEEN | IMR_RBEEN |
428 IMR_RBAEEN | IMR_RFOEN);
429 #endif
430
431 /* clear pending interrupts */
432 NIC_PUT(sc, SNR_ISR, ISR_ALL);
433
434 /* clear tally counters */
435 NIC_PUT(sc, SNR_CRCT, -1);
436 NIC_PUT(sc, SNR_FAET, -1);
437 NIC_PUT(sc, SNR_MPT, -1);
438
439 initialise_tda(sc);
440 initialise_rda(sc);
441 initialise_rra(sc);
442
443 sn_md_init(sc); /* MD initialization */
444
445 /* enable the chip */
446 NIC_PUT(sc, SNR_CR, 0);
447 wbflush();
448
449 /* program the CAM */
450 camprogram(sc);
451
452 /* get it to read resource descriptors */
453 NIC_PUT(sc, SNR_CR, CR_RRRA);
454 wbflush();
455 while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
456 continue;
457
458 /* enable rx */
459 NIC_PUT(sc, SNR_CR, CR_RXEN);
460 wbflush();
461
462 /* flag interface as "running" */
463 sc->sc_if.if_flags |= IFF_RUNNING;
464 sc->sc_if.if_flags &= ~IFF_OACTIVE;
465
466 splx(s);
467 return 0;
468 }
469
470 /*
471 * close down an interface and free its buffers
472 * Called on final close of device, or if sninit() fails
473 * part way through.
474 */
475 static int
476 snstop(struct sn_softc *sc)
477 {
478 struct mtd *mtd;
479 int s = splnet();
480
481 /* stick chip in reset */
482 NIC_PUT(sc, SNR_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 if (mtd->mtd_mbuf)
491 m_freem(mtd->mtd_mbuf);
492 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
493 }
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(struct ifnet *ifp)
509 {
510 struct sn_softc *sc = ifp->if_softc;
511 struct mtd *mtd;
512 int temp;
513
514 if (sc->mtd_hw != sc->mtd_free) {
515 /* something still pending for transmit */
516 mtd = &sc->mtda[sc->mtd_hw];
517 if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
518 log(LOG_ERR, "%s: Tx - timeout\n",
519 sc->sc_dev.dv_xname);
520 else
521 log(LOG_ERR, "%s: Tx - lost interrupt\n",
522 sc->sc_dev.dv_xname);
523 temp = ifp->if_flags & IFF_UP;
524 snreset(sc);
525 ifp->if_flags |= temp;
526 }
527 }
528
529 /*
530 * stuff packet into sonic (at splnet)
531 */
532 static inline u_int
533 sonicput(struct sn_softc *sc, struct mbuf *m0, int mtd_next)
534 {
535 struct mtd *mtdp;
536 struct mbuf *m;
537 u_char *buff;
538 void *txp;
539 u_int len = 0;
540 u_int totlen = 0;
541
542 #ifdef whyonearthwouldyoudothis
543 if (NIC_GET(sc, SNR_CR) & CR_TXP)
544 return 0;
545 #endif
546
547 /* grab the replacement mtd */
548 mtdp = &sc->mtda[sc->mtd_free];
549
550 buff = mtdp->mtd_buf;
551
552 /* this packet goes to mtdnext fill in the TDA */
553 mtdp->mtd_mbuf = m0;
554 txp = mtdp->mtd_txp;
555
556 /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
557 if (sc->mtd_pint == 0) {
558 sc->mtd_pint = NTDA/2;
559 SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT);
560 } else {
561 sc->mtd_pint--;
562 SWO(sc->bitmode, txp, TXP_CONFIG, 0);
563 }
564
565 for (m = m0; m; m = m->m_next) {
566 u_char *data = mtod(m, u_char *);
567 len = m->m_len;
568 totlen += len;
569 memcpy(buff, data, len);
570 buff += len;
571 }
572 if (totlen >= TXBSIZE) {
573 panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname);
574 }
575
576 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
577 LOWER(mtdp->mtd_vbuf));
578 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
579 UPPER(mtdp->mtd_vbuf));
580
581 if (totlen < ETHERMIN + ETHER_HDR_LEN) {
582 int pad = ETHERMIN + ETHER_HDR_LEN - totlen;
583 memset((char *)mtdp->mtd_buf + totlen, 0, pad);
584 totlen = ETHERMIN + ETHER_HDR_LEN;
585 }
586
587 SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
588 totlen);
589 SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
590 SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
591
592 /* link onto the next mtd that will be used */
593 SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
594 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
595
596 /*
597 * The previous txp.tlink currently contains a pointer to
598 * our txp | EOL. Want to clear the EOL, so write our
599 * pointer to the previous txp.
600 */
601 SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
602 LOWER(mtdp->mtd_vtxp));
603
604 /* make sure chip is running */
605 wbflush();
606 NIC_PUT(sc, SNR_CR, CR_TXP);
607 wbflush();
608 sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
609
610 return totlen;
611 }
612
613 /*
614 * These are called from sonicioctl() when /etc/ifconfig is run to set
615 * the address or switch the i/f on.
616 */
617 /*
618 * CAM support
619 */
620 static void
621 caminitialise(struct sn_softc *sc)
622 {
623 void *p_cda = sc->p_cda;
624 int i;
625 int camoffset;
626
627 for (i = 0; i < MAXCAM; i++) {
628 camoffset = i * CDA_CAMDESC;
629 SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
630 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
631 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
632 SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
633 }
634 SWO(bitmode, p_cda, CDA_ENABLE, 0);
635 }
636
637 static void
638 camentry(struct sn_softc *sc, int entry, u_char *ea)
639 {
640 void *p_cda = sc->p_cda;
641 int camoffset = entry * CDA_CAMDESC;
642
643 SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
644 SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
645 SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
646 SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
647 SWO(bitmode, p_cda, CDA_ENABLE,
648 (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
649 }
650
651 static void
652 camprogram(struct sn_softc *sc)
653 {
654 struct ether_multistep step;
655 struct ether_multi *enm;
656 struct ifnet *ifp;
657 int timeout;
658 int mcount = 0;
659
660 caminitialise(sc);
661
662 ifp = &sc->sc_if;
663
664 /* Always load our own address first. */
665 camentry(sc, mcount, LLADDR(ifp->if_sadl));
666 mcount++;
667
668 /* Assume we won't need allmulti bit. */
669 ifp->if_flags &= ~IFF_ALLMULTI;
670
671 /* Loop through multicast addresses */
672 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
673 while (enm != NULL) {
674 if (mcount == MAXCAM) {
675 ifp->if_flags |= IFF_ALLMULTI;
676 break;
677 }
678
679 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
680 sizeof(enm->enm_addrlo)) != 0) {
681 /*
682 * SONIC's CAM is programmed with specific
683 * addresses. It has no way to specify a range.
684 * (Well, thats not exactly true. If the
685 * range is small one could program each addr
686 * within the range as a separate CAM entry)
687 */
688 ifp->if_flags |= IFF_ALLMULTI;
689 break;
690 }
691
692 /* program the CAM with the specified entry */
693 camentry(sc, mcount, enm->enm_addrlo);
694 mcount++;
695
696 ETHER_NEXT_MULTI(step, enm);
697 }
698
699 NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda));
700 NIC_PUT(sc, SNR_CDC, MAXCAM);
701 NIC_PUT(sc, SNR_CR, CR_LCAM);
702 wbflush();
703
704 timeout = 10000;
705 while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--)
706 delay(10);
707 if (timeout == 0) {
708 /* XXX */
709 panic("%s: CAM initialisation failed", sc->sc_dev.dv_xname);
710 }
711 timeout = 10000;
712 while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--)
713 delay(10);
714
715 if (NIC_GET(sc, SNR_ISR) & ISR_LCD)
716 NIC_PUT(sc, SNR_ISR, ISR_LCD);
717 else
718 printf("%s: CAM initialisation without interrupt\n",
719 sc->sc_dev.dv_xname);
720 }
721
722 #ifdef SNDEBUG
723 static void
724 camdump(struct sn_softc *sc)
725 {
726 int i;
727
728 printf("CAM entries:\n");
729 NIC_PUT(sc, SNR_CR, CR_RST);
730 wbflush();
731
732 for (i = 0; i < 16; i++) {
733 ushort ap2, ap1, ap0;
734 NIC_PUT(sc, SNR_CEP, i);
735 wbflush();
736 ap2 = NIC_GET(sc, SNR_CAP2);
737 ap1 = NIC_GET(sc, SNR_CAP1);
738 ap0 = NIC_GET(sc, SNR_CAP0);
739 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
740 }
741 printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP));
742
743 NIC_PUT(sc, SNR_CR, 0);
744 wbflush();
745 }
746 #endif
747
748 static void
749 initialise_tda(struct sn_softc *sc)
750 {
751 struct mtd *mtd;
752 int i;
753
754 for (i = 0; i < NTDA; i++) {
755 mtd = &sc->mtda[i];
756 mtd->mtd_mbuf = 0;
757 }
758
759 sc->mtd_hw = 0;
760 sc->mtd_prev = NTDA - 1;
761 sc->mtd_free = 0;
762 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
763 sc->mtd_pint = NTDA/2;
764
765 NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
766 NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
767 }
768
769 static void
770 initialise_rda(struct sn_softc *sc)
771 {
772 int i;
773 char *p_rda = 0;
774 uint32_t v_rda = 0;
775
776 /* link the RDA's together into a circular list */
777 for (i = 0; i < (sc->sc_nrda - 1); i++) {
778 p_rda = (char *)sc->p_rda + (i * RXPKT_SIZE(sc));
779 v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
780 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda));
781 SWO(bitmode, p_rda, RXPKT_INUSE, 1);
782 }
783 p_rda = (char *)sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
784 SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
785 SWO(bitmode, p_rda, RXPKT_INUSE, 1);
786
787 /* mark end of receive descriptor list */
788 sc->sc_rdamark = sc->sc_nrda - 1;
789
790 sc->sc_rxmark = 0;
791
792 NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda));
793 NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda));
794 wbflush();
795 }
796
797 static void
798 initialise_rra(struct sn_softc *sc)
799 {
800 int i;
801 u_int v;
802 int bitmode = sc->bitmode;
803
804 if (bitmode)
805 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
806 else
807 NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
808
809 NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
810 NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
811 /* rea must point just past the end of the rra space */
812 NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
813 NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
814 NIC_PUT(sc, SNR_RSC, 0);
815
816 /* fill up SOME of the rra with buffers */
817 for (i = 0; i < NRBA; i++) {
818 v = SONIC_GETDMA(sc->rbuf[i]);
819 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
820 SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
821 SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(PAGE_SIZE/2));
822 SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(PAGE_SIZE/2));
823 }
824 sc->sc_rramark = NRBA;
825 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
826 wbflush();
827 }
828
829 int
830 snintr(void *arg)
831 {
832 struct sn_softc *sc = (struct sn_softc *)arg;
833 int handled = 0;
834 int isr;
835
836 while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
837 /* scrub the interrupts that we are going to service */
838 NIC_PUT(sc, SNR_ISR, isr);
839 handled = 1;
840 wbflush();
841
842 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
843 printf("%s: unexpected interrupt status 0x%x\n",
844 sc->sc_dev.dv_xname, isr);
845
846 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
847 sonictxint(sc);
848
849 if (isr & ISR_PKTRX)
850 sonicrxint(sc);
851
852 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
853 if (isr & ISR_HBL)
854 /*
855 * The repeater is not providing a heartbeat.
856 * In itself this isn't harmful, lots of the
857 * cheap repeater hubs don't supply a heartbeat.
858 * So ignore the lack of heartbeat. Its only
859 * if we can't detect a carrier that we have a
860 * problem.
861 */
862 ;
863 if (isr & ISR_RDE)
864 printf("%s: receive descriptors exhausted\n",
865 sc->sc_dev.dv_xname);
866 if (isr & ISR_RBE)
867 printf("%s: receive buffers exhausted\n",
868 sc->sc_dev.dv_xname);
869 if (isr & ISR_RBAE)
870 printf("%s: receive buffer area exhausted\n",
871 sc->sc_dev.dv_xname);
872 if (isr & ISR_RFO)
873 printf("%s: receive FIFO overrun\n",
874 sc->sc_dev.dv_xname);
875 }
876 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
877 #ifdef notdef
878 if (isr & ISR_CRC)
879 sc->sc_crctally++;
880 if (isr & ISR_FAE)
881 sc->sc_faetally++;
882 if (isr & ISR_MP)
883 sc->sc_mptally++;
884 #endif
885 }
886 snstart(&sc->sc_if);
887 }
888 return handled;
889 }
890
891 /*
892 * Transmit interrupt routine
893 */
894 static void
895 sonictxint(struct sn_softc *sc)
896 {
897 struct mtd *mtd;
898 void *txp;
899 unsigned short txp_status;
900 int mtd_hw;
901 struct ifnet *ifp = &sc->sc_if;
902
903 mtd_hw = sc->mtd_hw;
904
905 if (mtd_hw == sc->mtd_free)
906 return;
907
908 while (mtd_hw != sc->mtd_free) {
909 mtd = &sc->mtda[mtd_hw];
910
911 txp = mtd->mtd_txp;
912
913 if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) {
914 break; /* it hasn't really gone yet */
915 }
916
917 #ifdef SNDEBUG
918 {
919 struct ether_header *eh;
920
921 eh = (struct ether_header *) mtd->mtd_buf;
922 printf("%s: xmit status=0x%x len=%d type=0x%x from %s",
923 sc->sc_dev.dv_xname,
924 SRO(sc->bitmode, txp, TXP_STATUS),
925 SRO(sc->bitmode, txp, TXP_PKTSIZE),
926 htons(eh->ether_type),
927 ether_sprintf(eh->ether_shost));
928 printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
929 }
930 #endif /* SNDEBUG */
931
932 ifp->if_flags &= ~IFF_OACTIVE;
933
934 if (mtd->mtd_mbuf != 0) {
935 m_freem(mtd->mtd_mbuf);
936 mtd->mtd_mbuf = 0;
937 }
938 if (++mtd_hw == NTDA) mtd_hw = 0;
939
940 txp_status = SRO(sc->bitmode, txp, TXP_STATUS);
941
942 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
943 ((txp_status & TCR_NC) >> 12);
944
945 if ((txp_status & TCR_PTX) == 0) {
946 ifp->if_oerrors++;
947 printf("%s: Tx packet status=0x%x\n",
948 sc->sc_dev.dv_xname, txp_status);
949
950 /* XXX - DG This looks bogus */
951 if (mtd_hw != sc->mtd_free) {
952 printf("resubmitting remaining packets\n");
953 mtd = &sc->mtda[mtd_hw];
954 NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
955 NIC_PUT(sc, SNR_CR, CR_TXP);
956 wbflush();
957 break;
958 }
959 }
960 }
961
962 sc->mtd_hw = mtd_hw;
963 return;
964 }
965
966 /*
967 * Receive interrupt routine
968 */
969 static void
970 sonicrxint(struct sn_softc *sc)
971 {
972 void * rda;
973 int orra;
974 int len;
975 int rramark;
976 int rdamark;
977 uint16_t rxpkt_ptr;
978
979 rda = (char *)sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
980
981 while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
982 u_int status = SRO(bitmode, rda, RXPKT_STATUS);
983
984 orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
985 rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
986 len = SRO(bitmode, rda, RXPKT_BYTEC) - FCSSIZE;
987 if (status & RCR_PRX) {
988 void *pkt =
989 (char *)sc->rbuf[orra & RBAMASK] +
990 (rxpkt_ptr & PGOFSET);
991 if (sonic_read(sc, pkt, len))
992 sc->sc_if.if_ipackets++;
993 else
994 sc->sc_if.if_ierrors++;
995 } else
996 sc->sc_if.if_ierrors++;
997
998 /*
999 * give receive buffer area back to chip.
1000 *
1001 * If this was the last packet in the RRA, give the RRA to
1002 * the chip again.
1003 * If sonic read didnt copy it out then we would have to
1004 * wait !!
1005 * (dont bother add it back in again straight away)
1006 *
1007 * Really, we're doing p_rra[rramark] = p_rra[orra] but
1008 * we have to use the macros because SONIC might be in
1009 * 16 or 32 bit mode.
1010 */
1011 if (status & RCR_LPKT) {
1012 void *tmp1, *tmp2;
1013
1014 rramark = sc->sc_rramark;
1015 tmp1 = sc->p_rra[rramark];
1016 tmp2 = sc->p_rra[orra];
1017 SWO(bitmode, tmp1, RXRSRC_PTRLO,
1018 SRO(bitmode, tmp2, RXRSRC_PTRLO));
1019 SWO(bitmode, tmp1, RXRSRC_PTRHI,
1020 SRO(bitmode, tmp2, RXRSRC_PTRHI));
1021 SWO(bitmode, tmp1, RXRSRC_WCLO,
1022 SRO(bitmode, tmp2, RXRSRC_WCLO));
1023 SWO(bitmode, tmp1, RXRSRC_WCHI,
1024 SRO(bitmode, tmp2, RXRSRC_WCHI));
1025
1026 /* zap old rra for fun */
1027 SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
1028 SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
1029
1030 sc->sc_rramark = (++rramark) & RRAMASK;
1031 NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
1032 wbflush();
1033 }
1034
1035 /*
1036 * give receive descriptor back to chip simple
1037 * list is circular
1038 */
1039 rdamark = sc->sc_rdamark;
1040 SWO(bitmode, rda, RXPKT_INUSE, 1);
1041 SWO(bitmode, rda, RXPKT_RLINK,
1042 SRO(bitmode, rda, RXPKT_RLINK) | EOL);
1043 SWO(bitmode, ((char *)sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
1044 RXPKT_RLINK,
1045 SRO(bitmode, ((char *)sc->p_rda +
1046 (rdamark * RXPKT_SIZE(sc))),
1047 RXPKT_RLINK) & ~EOL);
1048 sc->sc_rdamark = sc->sc_rxmark;
1049
1050 if (++sc->sc_rxmark >= sc->sc_nrda)
1051 sc->sc_rxmark = 0;
1052 rda = (char *)sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1053 }
1054 }
1055
1056 /*
1057 * sonic_read -- pull packet off interface and forward to
1058 * appropriate protocol handler
1059 */
1060 static inline int
1061 sonic_read(struct sn_softc *sc, void *pkt, int len)
1062 {
1063 struct ifnet *ifp = &sc->sc_if;
1064 struct mbuf *m;
1065
1066 #ifdef SNDEBUG
1067 {
1068 printf("%s: rcvd %p len=%d type=0x%x from %s",
1069 sc->sc_dev.dv_xname, et, len, htons(et->ether_type),
1070 ether_sprintf(et->ether_shost));
1071 printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
1072 }
1073 #endif /* SNDEBUG */
1074
1075 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
1076 len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
1077 printf("%s: invalid packet length %d bytes\n",
1078 sc->sc_dev.dv_xname, len);
1079 return 0;
1080 }
1081
1082 m = sonic_get(sc, pkt, len);
1083 if (m == NULL)
1084 return 0;
1085 #if NBPFILTER > 0
1086 /* Pass the packet to any BPF listeners. */
1087 if (ifp->if_bpf)
1088 bpf_mtap(ifp->if_bpf, m);
1089 #endif
1090 (*ifp->if_input)(ifp, m);
1091 return 1;
1092 }
1093
1094 /*
1095 * munge the received packet into an mbuf chain
1096 */
1097 static inline struct mbuf *
1098 sonic_get(struct sn_softc *sc, void *pkt, int datalen)
1099 {
1100 struct mbuf *m, *top, **mp;
1101 int len;
1102
1103 MGETHDR(m, M_DONTWAIT, MT_DATA);
1104 if (m == 0)
1105 return 0;
1106 m->m_pkthdr.rcvif = &sc->sc_if;
1107 m->m_pkthdr.len = datalen;
1108 len = MHLEN;
1109 top = 0;
1110 mp = ⊤
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 len = MLEN;
1120 }
1121 if (datalen >= MINCLSIZE) {
1122 MCLGET(m, M_DONTWAIT);
1123 if ((m->m_flags & M_EXT) == 0) {
1124 if (top) m_freem(top);
1125 return 0;
1126 }
1127 len = MCLBYTES;
1128 }
1129
1130 if (mp == &top) {
1131 char *newdata = (char *)
1132 ALIGN((char *)m->m_data +
1133 sizeof(struct ether_header)) -
1134 sizeof(struct ether_header);
1135 len -= newdata - m->m_data;
1136 m->m_data = newdata;
1137 }
1138
1139 m->m_len = len = min(datalen, len);
1140
1141 memcpy(mtod(m, void *), pkt, (unsigned) len);
1142 pkt = (char *)pkt + len;
1143 datalen -= len;
1144 *mp = m;
1145 mp = &m->m_next;
1146 }
1147
1148 return top;
1149 }
1150