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