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