Merge from vendor branch GDB:
[dragonfly.git] / sys / dev / netif / snc / dp83932.c
1 /*      $FreeBSD: src/sys/dev/snc/dp83932.c,v 1.1.2.2 2003/02/11 08:52:00 nyan Exp $    */
2 /*      $DragonFly: src/sys/dev/netif/snc/Attic/dp83932.c,v 1.11 2004/07/23 07:16:28 joerg Exp $        */
3 /*      $NecBSD: dp83932.c,v 1.5 1999/07/29 05:08:44 kmatsuda Exp $     */
4 /*      $NetBSD: if_snc.c,v 1.18 1998/04/25 21:27:40 scottr Exp $       */
5
6 /*
7  * Copyright (c) 1997, 1998, 1999
8  *      Kouichi Matsuda.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Kouichi Matsuda for
21  *      NetBSD/pc98.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 /*
38  * Modified for FreeBSD(98) 4.0 from NetBSD/pc98 1.4.2 by Motomichi Matsuzaki.
39  */
40
41 /*
42  * Modified for NetBSD/pc98 1.2G from NetBSD/mac68k 1.2G by Kouichi Matsuda.
43  * Make adapted for NEC PC-9801-83, 84, PC-9801-103, 104, PC-9801N-25 and
44  * PC-9801N-J02, J02R, which uses National Semiconductor DP83934AVQB as
45  * Ethernet Controller and National Semiconductor NS46C46 as
46  * (64 * 16 bits) Microwire Serial EEPROM.
47  */
48
49 /*
50  * National Semiconductor  DP8393X SONIC Driver
51  * Copyright (c) 1991   Algorithmics Ltd (http://www.algor.co.uk)
52  * You may use, copy, and modify this program so long as you retain the
53  * copyright line.
54  *
55  * This driver has been substantially modified since Algorithmics donated
56  * it.
57  *
58  *   Denton Gentry <denny1@home.com>
59  * and also
60  *   Yanagisawa Takeshi <yanagisw@aa.ap.titech.ac.jp>
61  * did the work to get this running on the Macintosh.
62  */
63
64 #include "opt_inet.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/sockio.h>
69 #include <sys/mbuf.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/syslog.h>
73 #include <sys/errno.h>
74 #if NRND > 0
75 #include <sys/rnd.h>
76 #endif
77
78 #include <net/ethernet.h>
79 #include <net/if.h>
80 #include <net/if_arp.h>
81 #include <net/if_dl.h>
82 #include <net/if_types.h>
83 #include <net/if_media.h>
84
85 #ifdef INET
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #endif
91
92 #include <net/bpf.h>
93 #include <net/bpfdesc.h>
94
95 #include <sys/bus.h>
96 #include <machine/bus.h>
97 #include "dp83932reg.h"
98 #include "dp83932var.h"
99
100 hide void       sncwatchdog (struct ifnet *);
101 hide void       sncinit (void *);
102 hide int        sncstop (struct snc_softc *sc);
103 hide int        sncioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
104                           struct ucred *);
105 hide void       sncstart (struct ifnet *ifp);
106 hide void       sncreset (struct snc_softc *sc);
107
108 hide void       caminitialise (struct snc_softc *);
109 hide void       camentry (struct snc_softc *, int, u_char *ea);
110 hide void       camprogram (struct snc_softc *);
111 hide void       initialise_tda (struct snc_softc *);
112 hide void       initialise_rda (struct snc_softc *);
113 hide void       initialise_rra (struct snc_softc *);
114 #ifdef SNCDEBUG
115 hide void       camdump (struct snc_softc *sc);
116 #endif
117
118 hide void       sonictxint (struct snc_softc *);
119 hide void       sonicrxint (struct snc_softc *);
120
121 hide u_int      sonicput (struct snc_softc *sc, struct mbuf *m0,
122                             int mtd_next);
123 hide int        sonic_read (struct snc_softc *, u_int32_t, int);
124 hide struct mbuf *sonic_get (struct snc_softc *,
125                             u_int32_t, int);
126
127 int     snc_enable (struct snc_softc *);
128 void    snc_disable (struct snc_softc *);
129
130 int     snc_mediachange (struct ifnet *);
131 void    snc_mediastatus (struct ifnet *, struct ifmediareq *);
132
133 #ifdef NetBSD
134 #if NetBSD <= 199714
135 struct cfdriver snc_cd = {
136         NULL, "snc", DV_IFNET
137 };
138 #endif
139 #endif
140
141 #undef assert
142 #undef _assert
143
144 #ifdef NDEBUG
145 #define assert(e)       ((void)0)
146 #define _assert(e)      ((void)0)
147 #else
148 #define _assert(e)      assert(e)
149 #ifdef __STDC__
150 #define assert(e)       ((e) ? (void)0 : __assert("snc ", __FILE__, __LINE__, #e))
151 #else   /* PCC */
152 #define assert(e)       ((e) ? (void)0 : __assert("snc "__FILE__, __LINE__, "e"))
153 #endif
154 #endif
155
156 #ifdef  SNCDEBUG
157 #define SNC_SHOWTXHDR   0x01    /* show tx ether_header */
158 #define SNC_SHOWRXHDR   0x02    /* show rx ether_header */
159 #define SNC_SHOWCAMENT  0x04    /* show CAM entry */
160 #endif  /* SNCDEBUG */
161 int sncdebug = 0;
162
163
164 void
165 sncconfig(sc, media, nmedia, defmedia, eaddr)
166         struct snc_softc *sc;
167         int *media, nmedia, defmedia;
168         uint8_t *eaddr;
169 {
170         struct ifnet *ifp = &sc->sc_if;
171         int i;
172
173 #ifdef SNCDEBUG
174         if ((sncdebug & SNC_SHOWCAMENT) != 0) {
175                 camdump(sc);
176         }
177 #endif
178
179 #ifdef SNCDEBUG
180         device_printf(sc->sc_dev,
181                       "buffers: rra=0x%x cda=0x%x rda=0x%x tda=0x%x\n",
182                       sc->v_rra[0], sc->v_cda,
183                       sc->v_rda, sc->mtda[0].mtd_vtxp);
184 #endif
185
186         ifp->if_softc = sc;
187         if_initname(ifp, "snc", device_get_unit(sc->sc_dev));
188         ifp->if_ioctl = sncioctl;
189         ifp->if_start = sncstart;
190         ifp->if_flags =
191             IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
192         ifp->if_watchdog = sncwatchdog;
193         ifp->if_init = sncinit;
194         ifp->if_mtu = ETHERMTU;
195         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
196
197         /* Initialize media goo. */
198         ifmedia_init(&sc->sc_media, 0, snc_mediachange,
199             snc_mediastatus);
200         if (media != NULL) {
201                 for (i = 0; i < nmedia; i++)
202                         ifmedia_add(&sc->sc_media, media[i], 0, NULL);
203                 ifmedia_set(&sc->sc_media, defmedia);
204         } else {
205                 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
206                 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
207         }
208
209         ether_ifattach(ifp, eaddr);
210
211 #if NRND > 0
212         rnd_attach_source(&sc->rnd_source, device_get_nameunit(sc->sc_dev),
213             RND_TYPE_NET, 0);
214 #endif
215 }
216
217 void
218 sncshutdown(arg)
219         void *arg;
220 {
221
222         sncstop((struct snc_softc *)arg);
223 }
224
225 /*
226  * Media change callback.
227  */
228 int
229 snc_mediachange(ifp)
230         struct ifnet *ifp;
231 {
232         struct snc_softc *sc = ifp->if_softc;
233
234         if (sc->sc_mediachange)
235                 return ((*sc->sc_mediachange)(sc));
236         return (EINVAL);
237 }
238
239 /*
240  * Media status callback.
241  */
242 void
243 snc_mediastatus(ifp, ifmr)
244         struct ifnet *ifp;
245         struct ifmediareq *ifmr;
246 {
247         struct snc_softc *sc = ifp->if_softc;
248
249         if (sc->sc_enabled == 0) {
250                 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
251                 ifmr->ifm_status = 0;
252                 return;
253         }
254
255         if (sc->sc_mediastatus)
256                 (*sc->sc_mediastatus)(sc, ifmr);
257 }
258
259
260 hide int
261 sncioctl(ifp, cmd, data, cr)
262         struct ifnet *ifp;
263         u_long cmd;
264         caddr_t data;
265         struct ucred *cr;
266 {
267         struct ifreq *ifr;
268         struct snc_softc *sc = ifp->if_softc;
269         int     s = splhardnet(), err = 0;
270         int     temp;
271
272         switch (cmd) {
273
274         case SIOCSIFADDR:
275         case SIOCGIFADDR:
276         case SIOCSIFMTU:
277                 err = ether_ioctl(ifp, cmd, data);
278                 break;
279
280         case SIOCSIFFLAGS:
281                 if ((ifp->if_flags & IFF_UP) == 0 &&
282                     (ifp->if_flags & IFF_RUNNING) != 0) {
283                         /*
284                          * If interface is marked down and it is running,
285                          * then stop it.
286                          */
287                         sncstop(sc);
288                         ifp->if_flags &= ~IFF_RUNNING;
289                         snc_disable(sc);
290                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
291                     (ifp->if_flags & IFF_RUNNING) == 0) {
292                         /*
293                          * If interface is marked up and it is stopped,
294                          * then start it.
295                          */
296                         if ((err = snc_enable(sc)) != 0)
297                                 break;
298                         sncinit(sc);
299                 } else if (sc->sc_enabled) {
300                         /*
301                          * reset the interface to pick up any other changes
302                          * in flags
303                          */
304                         temp = ifp->if_flags & IFF_UP;
305                         sncreset(sc);
306                         ifp->if_flags |= temp;
307                         sncstart(ifp);
308                 }
309                 break;
310
311         case SIOCADDMULTI:
312         case SIOCDELMULTI:
313                 if (sc->sc_enabled == 0) {
314                         err = EIO;
315                         break;
316                 }
317                 temp = ifp->if_flags & IFF_UP;
318                 sncreset(sc);
319                 ifp->if_flags |= temp;
320                 err = 0;
321                 break;
322         case SIOCGIFMEDIA:
323         case SIOCSIFMEDIA:
324                 ifr = (struct ifreq *) data;
325                 err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
326                 break;
327         default:
328                 err = EINVAL;
329         }
330         splx(s);
331         return (err);
332 }
333
334 /*
335  * Encapsulate a packet of type family for the local net.
336  */
337 hide void
338 sncstart(ifp)
339         struct ifnet *ifp;
340 {
341         struct snc_softc        *sc = ifp->if_softc;
342         struct mbuf     *m;
343         int             mtd_next;
344
345         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
346                 return;
347
348 outloop:
349         /* Check for room in the xmit buffer. */
350         if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
351                 mtd_next = 0;
352
353         if (mtd_next == sc->mtd_hw) {
354                 ifp->if_flags |= IFF_OACTIVE;
355                 return;
356         }
357
358         IF_DEQUEUE(&ifp->if_snd, m);
359         if (m == 0)
360                 return;
361
362         /* We need the header for m_pkthdr.len. */
363         if ((m->m_flags & M_PKTHDR) == 0)
364                 panic("%s: sncstart: no header mbuf",
365                       device_get_nameunit(sc->sc_dev));
366
367         /*
368          * If bpf is listening on this interface, let it
369          * see the packet before we commit it to the wire.
370          */
371         if (ifp->if_bpf)
372                 bpf_mtap(ifp, m);
373
374         /*
375          * If there is nothing in the o/p queue, and there is room in
376          * the Tx ring, then send the packet directly.  Otherwise append
377          * it to the o/p queue.
378          */
379         if ((sonicput(sc, m, mtd_next)) == 0) {
380                 IF_PREPEND(&ifp->if_snd, m);
381                 return;
382         }
383
384         sc->mtd_prev = sc->mtd_free;
385         sc->mtd_free = mtd_next;
386
387         ifp->if_opackets++;             /* # of pkts */
388
389         /* Jump back for possibly more punishment. */
390         goto outloop;
391 }
392
393 /*
394  * reset and restart the SONIC.  Called in case of fatal
395  * hardware/software errors.
396  */
397 hide void
398 sncreset(sc)
399         struct snc_softc *sc;
400 {
401         sncstop(sc);
402         sncinit(sc);
403 }
404
405 hide void
406 sncinit(xsc)
407         void *xsc;
408 {
409         struct snc_softc *sc = xsc;
410         u_long  s_rcr;
411         int     s;
412
413         if (sc->sc_if.if_flags & IFF_RUNNING)
414                 /* already running */
415                 return;
416
417         s = splhardnet();
418
419         NIC_PUT(sc, SNCR_CR, CR_RST);   /* DCR only accessable in reset mode! */
420
421         /* config it */
422         NIC_PUT(sc, SNCR_DCR, (sc->sncr_dcr |
423                 (sc->bitmode ? DCR_DW32 : DCR_DW16)));
424         NIC_PUT(sc, SNCR_DCR2, sc->sncr_dcr2);
425
426         s_rcr = RCR_BRD | RCR_LBNONE;
427         if (sc->sc_if.if_flags & IFF_PROMISC)
428                 s_rcr |= RCR_PRO;
429         if (sc->sc_if.if_flags & IFF_ALLMULTI)
430                 s_rcr |= RCR_AMC;
431         NIC_PUT(sc, SNCR_RCR, s_rcr);
432
433         NIC_PUT(sc, SNCR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
434
435         /* clear pending interrupts */
436         NIC_PUT(sc, SNCR_ISR, ISR_ALL);
437
438         /* clear tally counters */
439         NIC_PUT(sc, SNCR_CRCT, -1);
440         NIC_PUT(sc, SNCR_FAET, -1);
441         NIC_PUT(sc, SNCR_MPT, -1);
442
443         initialise_tda(sc);
444         initialise_rda(sc);
445         initialise_rra(sc);
446
447         /* enable the chip */
448         NIC_PUT(sc, SNCR_CR, 0);
449         wbflush();
450
451         /* program the CAM */
452         camprogram(sc);
453
454         /* get it to read resource descriptors */
455         NIC_PUT(sc, SNCR_CR, CR_RRRA);
456         wbflush();
457         while ((NIC_GET(sc, SNCR_CR)) & CR_RRRA)
458                 continue;
459
460         /* enable rx */
461         NIC_PUT(sc, SNCR_CR, CR_RXEN);
462         wbflush();
463
464         /* flag interface as "running" */
465         sc->sc_if.if_flags |= IFF_RUNNING;
466         sc->sc_if.if_flags &= ~IFF_OACTIVE;
467
468         splx(s);
469         return;
470 }
471
472 /*
473  * close down an interface and free its buffers
474  * Called on final close of device, or if sncinit() fails
475  * part way through.
476  */
477 hide int
478 sncstop(sc)
479         struct snc_softc *sc;
480 {
481         struct mtd *mtd;
482         int     s = splhardnet();
483
484         /* stick chip in reset */
485         NIC_PUT(sc, SNCR_CR, CR_RST);
486         wbflush();
487
488         /* free all receive buffers (currently static so nothing to do) */
489
490         /* free all pending transmit mbufs */
491         while (sc->mtd_hw != sc->mtd_free) {
492                 mtd = &sc->mtda[sc->mtd_hw];
493                 if (mtd->mtd_mbuf)
494                         m_freem(mtd->mtd_mbuf);
495                 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
496         }
497
498         sc->sc_if.if_timer = 0;
499         sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
500
501         splx(s);
502         return (0);
503 }
504
505 /*
506  * Called if any Tx packets remain unsent after 5 seconds,
507  * In all cases we just reset the chip, and any retransmission
508  * will be handled by higher level protocol timeouts.
509  */
510 hide void
511 sncwatchdog(ifp)
512         struct ifnet *ifp;
513 {
514         struct snc_softc *sc = ifp->if_softc;
515         struct mtd *mtd;
516         int     temp;
517
518         if (sc->mtd_hw != sc->mtd_free) {
519                 /* something still pending for transmit */
520                 mtd = &sc->mtda[sc->mtd_hw];
521                 if (SRO(sc, mtd->mtd_vtxp, TXP_STATUS) == 0)
522                         log(LOG_ERR, "%s: Tx - timeout\n",
523                             device_get_nameunit(sc->sc_dev));
524                 else
525                         log(LOG_ERR, "%s: Tx - lost interrupt\n",
526                             device_get_nameunit(sc->sc_dev));
527                 temp = ifp->if_flags & IFF_UP;
528                 sncreset(sc);
529                 ifp->if_flags |= temp;
530         }
531 }
532
533 /*
534  * stuff packet into sonic (at splnet)
535  */
536 hide u_int
537 sonicput(sc, m0, mtd_next)
538         struct snc_softc *sc;
539         struct mbuf *m0;
540         int mtd_next;
541 {
542         struct mtd *mtdp;
543         struct mbuf *m;
544         u_int32_t buff;
545         u_int32_t txp;
546         u_int   len = 0;
547         u_int   totlen = 0;
548
549 #ifdef whyonearthwouldyoudothis
550         if (NIC_GET(sc, SNCR_CR) & CR_TXP)
551                 return (0);
552 #endif
553
554         /* grab the replacement mtd */
555         mtdp = &sc->mtda[sc->mtd_free];
556
557         buff = mtdp->mtd_vbuf;
558         
559         /* this packet goes to mtdnext fill in the TDA */
560         mtdp->mtd_mbuf = m0;
561         txp = mtdp->mtd_vtxp;
562
563         /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
564         if (sc->mtd_pint == 0) {
565                 sc->mtd_pint = NTDA/2;
566                 SWO(sc, txp, TXP_CONFIG, TCR_PINT);
567         } else {
568                 sc->mtd_pint--;
569                 SWO(sc, txp, TXP_CONFIG, 0);
570         }
571
572         for (m = m0; m; m = m->m_next) {
573                 len = m->m_len;
574                 totlen += len;
575                 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), buff, len);
576                 buff += len;
577         }
578         if (totlen >= TXBSIZE) {
579                 panic("%s: sonicput: packet overflow",
580                       device_get_nameunit(sc->sc_dev));
581         }
582
583         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
584             LOWER(mtdp->mtd_vbuf));
585         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
586             UPPER(mtdp->mtd_vbuf));
587
588         if (totlen < ETHERMIN + sizeof(struct ether_header)) {
589                 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
590                 (*sc->sc_zerobuf)(sc, mtdp->mtd_vbuf + totlen, pad);
591                 totlen = ETHERMIN + sizeof(struct ether_header);
592         }
593
594         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
595             totlen);
596         SWO(sc, txp, TXP_FRAGCNT, 1);
597         SWO(sc, txp, TXP_PKTSIZE, totlen);
598
599         /* link onto the next mtd that will be used */
600         SWO(sc, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
601             LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
602
603         /*
604          * The previous txp.tlink currently contains a pointer to
605          * our txp | EOL. Want to clear the EOL, so write our
606          * pointer to the previous txp.
607          */
608         SWO(sc, sc->mtda[sc->mtd_prev].mtd_vtxp, sc->mtd_tlinko,
609             LOWER(mtdp->mtd_vtxp));
610
611         /* make sure chip is running */
612         wbflush();
613         NIC_PUT(sc, SNCR_CR, CR_TXP);
614         wbflush();
615         sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
616
617         return (totlen);
618 }
619
620 /*
621  * These are called from sonicioctl() when /etc/ifconfig is run to set
622  * the address or switch the i/f on.
623  */
624 /*
625  * CAM support
626  */
627 hide void
628 caminitialise(sc)
629         struct snc_softc *sc;
630 {
631         u_int32_t v_cda = sc->v_cda;
632         int     i;
633         int     camoffset;
634
635         for (i = 0; i < MAXCAM; i++) {
636                 camoffset = i * CDA_CAMDESC;
637                 SWO(sc, v_cda, (camoffset + CDA_CAMEP), i);
638                 SWO(sc, v_cda, (camoffset + CDA_CAMAP2), 0);
639                 SWO(sc, v_cda, (camoffset + CDA_CAMAP1), 0);
640                 SWO(sc, v_cda, (camoffset + CDA_CAMAP0), 0);
641         }
642         SWO(sc, v_cda, CDA_ENABLE, 0);
643
644 #ifdef SNCDEBUG
645         if ((sncdebug & SNC_SHOWCAMENT) != 0) {
646                 camdump(sc);
647         }
648 #endif
649 }
650
651 hide void
652 camentry(sc, entry, ea)
653         int entry;
654         u_char *ea;
655         struct snc_softc *sc;
656 {
657         u_int32_t v_cda = sc->v_cda;
658         int     camoffset = entry * CDA_CAMDESC;
659
660         SWO(sc, v_cda, camoffset + CDA_CAMEP, entry);
661         SWO(sc, v_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
662         SWO(sc, v_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
663         SWO(sc, v_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
664         SWO(sc, v_cda, CDA_ENABLE, 
665             (SRO(sc, v_cda, CDA_ENABLE) | (1 << entry)));
666 }
667
668 hide void
669 camprogram(sc)
670         struct snc_softc *sc;
671 {
672         struct ifmultiaddr      *ifma;
673         struct ifnet *ifp;
674         int     timeout;
675         int     mcount = 0;
676
677         caminitialise(sc);
678
679         ifp = &sc->sc_if;
680
681         /* Always load our own address first. */
682         camentry (sc, mcount, sc->sc_ethercom.ac_enaddr);
683         mcount++;
684
685         /* Assume we won't need allmulti bit. */
686         ifp->if_flags &= ~IFF_ALLMULTI;
687
688         /* Loop through multicast addresses */
689         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
690                                 ifma = ifma->ifma_link.le_next) {
691                 if (ifma->ifma_addr->sa_family != AF_LINK)
692                         continue;
693                 if (mcount == MAXCAM) {
694                          ifp->if_flags |= IFF_ALLMULTI;
695                          break;
696                 }
697
698                 /* program the CAM with the specified entry */
699                 camentry(sc, mcount,
700                          LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
701                 mcount++;
702         }
703
704         NIC_PUT(sc, SNCR_CDP, LOWER(sc->v_cda));
705         NIC_PUT(sc, SNCR_CDC, MAXCAM);
706         NIC_PUT(sc, SNCR_CR, CR_LCAM);
707         wbflush();
708
709         timeout = 10000;
710         while ((NIC_GET(sc, SNCR_CR) & CR_LCAM) && timeout--)
711                 continue;
712         if (timeout == 0) {
713                 /* XXX */
714                 panic("%s: CAM initialisation failed\n",
715                       device_get_nameunit(sc->sc_dev));
716         }
717         timeout = 10000;
718         while (((NIC_GET(sc, SNCR_ISR) & ISR_LCD) == 0) && timeout--)
719                 continue;
720
721         if (NIC_GET(sc, SNCR_ISR) & ISR_LCD)
722                 NIC_PUT(sc, SNCR_ISR, ISR_LCD);
723         else
724                 device_printf(sc->sc_dev,
725                               "CAM initialisation without interrupt\n");
726 }
727
728 #ifdef SNCDEBUG
729 hide void
730 camdump(sc)
731         struct snc_softc *sc;
732 {
733         int     i;
734
735         printf("CAM entries:\n");
736         NIC_PUT(sc, SNCR_CR, CR_RST);
737         wbflush();
738
739         for (i = 0; i < 16; i++) {
740                 ushort  ap2, ap1, ap0;
741                 NIC_PUT(sc, SNCR_CEP, i);
742                 wbflush();
743                 ap2 = NIC_GET(sc, SNCR_CAP2);
744                 ap1 = NIC_GET(sc, SNCR_CAP1);
745                 ap0 = NIC_GET(sc, SNCR_CAP0);
746                 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
747         }
748         printf("CAM enable 0x%x\n", NIC_GET(sc, SNCR_CEP));
749
750         NIC_PUT(sc, SNCR_CR, 0);
751         wbflush();
752 }
753 #endif
754
755 hide void
756 initialise_tda(sc)
757         struct snc_softc *sc;
758 {
759         struct mtd *mtd;
760         int     i;
761
762         for (i = 0; i < NTDA; i++) {
763                 mtd = &sc->mtda[i];
764                 mtd->mtd_mbuf = 0;
765         }
766
767         sc->mtd_hw = 0;
768         sc->mtd_prev = NTDA - 1;
769         sc->mtd_free = 0;
770         sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
771         sc->mtd_pint = NTDA/2;
772
773         NIC_PUT(sc, SNCR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
774         NIC_PUT(sc, SNCR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
775 }
776
777 hide void
778 initialise_rda(sc)
779         struct snc_softc *sc;
780 {
781         int             i;
782         u_int32_t       vv_rda = 0;
783         u_int32_t       v_rda = 0;
784
785         /* link the RDA's together into a circular list */
786         for (i = 0; i < (sc->sc_nrda - 1); i++) {
787                 v_rda = sc->v_rda + (i * RXPKT_SIZE(sc));
788                 vv_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
789                 SWO(sc, v_rda, RXPKT_RLINK, LOWER(vv_rda));
790                 SWO(sc, v_rda, RXPKT_INUSE, 1);
791         }
792         v_rda = sc->v_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
793         SWO(sc, v_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
794         SWO(sc, v_rda, RXPKT_INUSE, 1);
795
796         /* mark end of receive descriptor list */
797         sc->sc_rdamark = sc->sc_nrda - 1;
798
799         sc->sc_rxmark = 0;
800
801         NIC_PUT(sc, SNCR_URDA, UPPER(sc->v_rda));
802         NIC_PUT(sc, SNCR_CRDA, LOWER(sc->v_rda));
803         wbflush();
804 }
805
806 hide void
807 initialise_rra(sc)
808         struct snc_softc *sc;
809 {
810         int     i;
811         u_int   v;
812         int     bitmode = sc->bitmode;
813
814         if (bitmode)
815                 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 2);
816         else
817                 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 1);
818
819         NIC_PUT(sc, SNCR_URRA, UPPER(sc->v_rra[0]));
820         NIC_PUT(sc, SNCR_RSA, LOWER(sc->v_rra[0]));
821         /* rea must point just past the end of the rra space */
822         NIC_PUT(sc, SNCR_REA, LOWER(sc->v_rea));
823         NIC_PUT(sc, SNCR_RRP, LOWER(sc->v_rra[0]));
824         NIC_PUT(sc, SNCR_RSC, 0);
825
826         /* fill up SOME of the rra with buffers */
827         for (i = 0; i < NRBA; i++) {
828                 v = SONIC_GETDMA(sc->rbuf[i]);
829                 SWO(sc, sc->v_rra[i], RXRSRC_PTRHI, UPPER(v));
830                 SWO(sc, sc->v_rra[i], RXRSRC_PTRLO, LOWER(v));
831                 SWO(sc, sc->v_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
832                 SWO(sc, sc->v_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
833         }
834         sc->sc_rramark = NRBA;
835         NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
836         wbflush();
837 }
838
839 void
840 sncintr(arg)
841         void    *arg;
842 {
843         struct snc_softc *sc = (struct snc_softc *)arg;
844         int     isr;
845
846         if (sc->sc_enabled == 0)
847                 return;
848
849         while ((isr = (NIC_GET(sc, SNCR_ISR) & ISR_ALL)) != 0) {
850                 /* scrub the interrupts that we are going to service */
851                 NIC_PUT(sc, SNCR_ISR, isr);
852                 wbflush();
853
854                 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
855                         device_printf(sc->sc_dev,
856                                       "unexpected interrupt status 0x%x\n",
857                                       isr);
858
859                 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
860                         sonictxint(sc);
861
862                 if (isr & ISR_PKTRX)
863                         sonicrxint(sc);
864
865                 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
866                         if (isr & ISR_HBL)
867                                 /*
868                                  * The repeater is not providing a heartbeat.
869                                  * In itself this isn't harmful, lots of the
870                                  * cheap repeater hubs don't supply a heartbeat.
871                                  * So ignore the lack of heartbeat. Its only
872                                  * if we can't detect a carrier that we have a
873                                  * problem.
874                                  */
875                                 ;
876                         if (isr & ISR_RDE)
877                                 device_printf(sc->sc_dev, 
878                                         "receive descriptors exhausted\n");
879                         if (isr & ISR_RBE)
880                                 device_printf(sc->sc_dev, 
881                                         "receive buffers exhausted\n");
882                         if (isr & ISR_RBAE)
883                                 device_printf(sc->sc_dev, 
884                                         "receive buffer area exhausted\n");
885                         if (isr & ISR_RFO)
886                                 device_printf(sc->sc_dev, 
887                                         "receive FIFO overrun\n");
888                 }
889                 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
890 #ifdef notdef
891                         if (isr & ISR_CRC)
892                                 sc->sc_crctally++;
893                         if (isr & ISR_FAE)
894                                 sc->sc_faetally++;
895                         if (isr & ISR_MP)
896                                 sc->sc_mptally++;
897 #endif
898                 }
899                 sncstart(&sc->sc_if);
900
901 #if NRND > 0
902                 if (isr)
903                         rnd_add_uint32(&sc->rnd_source, isr);
904 #endif
905         }
906         return;
907 }
908
909 /*
910  * Transmit interrupt routine
911  */
912 hide void
913 sonictxint(sc)
914         struct snc_softc *sc;
915 {
916         struct mtd      *mtd;
917         u_int32_t       txp;
918         unsigned short  txp_status;
919         int             mtd_hw;
920         struct ifnet    *ifp = &sc->sc_if;
921
922         mtd_hw = sc->mtd_hw;
923
924         if (mtd_hw == sc->mtd_free)
925                 return;
926
927         while (mtd_hw != sc->mtd_free) {
928                 mtd = &sc->mtda[mtd_hw];
929
930                 txp = mtd->mtd_vtxp;
931
932                 if (SRO(sc, txp, TXP_STATUS) == 0) {
933                         break; /* it hasn't really gone yet */
934                 }
935
936 #ifdef SNCDEBUG
937                 if ((sncdebug & SNC_SHOWTXHDR) != 0)
938                 {
939                         struct ether_header eh;
940
941                         (*sc->sc_copyfrombuf)(sc, &eh, mtd->mtd_vbuf, sizeof(eh));
942                         device_printf(sc->sc_dev,
943                             "xmit status=0x%x len=%d type=0x%x from %6D",
944                             SRO(sc, txp, TXP_STATUS),
945                             SRO(sc, txp, TXP_PKTSIZE),
946                             htons(eh.ether_type),
947                             eh.ether_shost, ":");
948                         printf(" (to %6D)\n", eh.ether_dhost, ":");
949                 }
950 #endif /* SNCDEBUG */
951
952                 ifp->if_flags &= ~IFF_OACTIVE;
953
954                 if (mtd->mtd_mbuf != 0) {
955                         m_freem(mtd->mtd_mbuf);
956                         mtd->mtd_mbuf = 0;
957                 }
958                 if (++mtd_hw == NTDA) mtd_hw = 0;
959
960                 txp_status = SRO(sc, txp, TXP_STATUS);
961
962                 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
963                         ((txp_status & TCR_NC) >> 12);
964
965                 if ((txp_status & TCR_PTX) == 0) {
966                         ifp->if_oerrors++;
967                         device_printf(sc->sc_dev, "Tx packet status=0x%x\n",
968                                       txp_status);
969                         
970                         /* XXX - DG This looks bogus */
971                         if (mtd_hw != sc->mtd_free) {
972                                 printf("resubmitting remaining packets\n");
973                                 mtd = &sc->mtda[mtd_hw];
974                                 NIC_PUT(sc, SNCR_CTDA, LOWER(mtd->mtd_vtxp));
975                                 NIC_PUT(sc, SNCR_CR, CR_TXP);
976                                 wbflush();
977                                 break;
978                         }
979                 }
980         }
981
982         sc->mtd_hw = mtd_hw;
983         return;
984 }
985
986 /*
987  * Receive interrupt routine
988  */
989 hide void
990 sonicrxint(sc)
991         struct snc_softc *sc;
992 {
993         u_int32_t rda;
994         int     orra;
995         int     len;
996         int     rramark;
997         int     rdamark;
998         u_int16_t rxpkt_ptr;
999
1000         rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1001
1002         while (SRO(sc, rda, RXPKT_INUSE) == 0) {
1003                 u_int status = SRO(sc, rda, RXPKT_STATUS);
1004
1005                 orra = RBASEQ(SRO(sc, rda, RXPKT_SEQNO)) & RRAMASK;
1006                 rxpkt_ptr = SRO(sc, rda, RXPKT_PTRLO);
1007                 /*
1008                  * Do not trunc ether_header length.
1009                  * Our sonic_read() and sonic_get() require it.
1010                  */
1011                 len = SRO(sc, rda, RXPKT_BYTEC) - FCSSIZE;
1012                 if (status & RCR_PRX) {
1013                         /* XXX: Does PGOFSET require? */
1014                         u_int32_t pkt =
1015                             sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
1016                         if (sonic_read(sc, pkt, len))
1017                                 sc->sc_if.if_ipackets++;
1018                         else
1019                                 sc->sc_if.if_ierrors++;
1020                 } else
1021                         sc->sc_if.if_ierrors++;
1022
1023                 /*
1024                  * give receive buffer area back to chip.
1025                  *
1026                  * If this was the last packet in the RRA, give the RRA to
1027                  * the chip again.
1028                  * If sonic read didnt copy it out then we would have to
1029                  * wait !!
1030                  * (dont bother add it back in again straight away)
1031                  *
1032                  * Really, we're doing v_rra[rramark] = v_rra[orra] but
1033                  * we have to use the macros because SONIC might be in
1034                  * 16 or 32 bit mode.
1035                  */
1036                 if (status & RCR_LPKT) {
1037                         u_int32_t tmp1, tmp2;
1038
1039                         rramark = sc->sc_rramark;
1040                         tmp1 = sc->v_rra[rramark];
1041                         tmp2 = sc->v_rra[orra];
1042                         SWO(sc, tmp1, RXRSRC_PTRLO,
1043                                 SRO(sc, tmp2, RXRSRC_PTRLO));
1044                         SWO(sc, tmp1, RXRSRC_PTRHI,
1045                                 SRO(sc, tmp2, RXRSRC_PTRHI));
1046                         SWO(sc, tmp1, RXRSRC_WCLO,
1047                                 SRO(sc, tmp2, RXRSRC_WCLO));
1048                         SWO(sc, tmp1, RXRSRC_WCHI,
1049                                 SRO(sc, tmp2, RXRSRC_WCHI));
1050
1051                         /* zap old rra for fun */
1052                         SWO(sc, tmp2, RXRSRC_WCHI, 0);
1053                         SWO(sc, tmp2, RXRSRC_WCLO, 0);
1054
1055                         sc->sc_rramark = (++rramark) & RRAMASK;
1056                         NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[rramark]));
1057                         wbflush();
1058                 }
1059
1060                 /*
1061                  * give receive descriptor back to chip simple
1062                  * list is circular
1063                  */
1064                 rdamark = sc->sc_rdamark;
1065                 SWO(sc, rda, RXPKT_INUSE, 1);
1066                 SWO(sc, rda, RXPKT_RLINK,
1067                         SRO(sc, rda, RXPKT_RLINK) | EOL);
1068                 SWO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1069                         SRO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))),
1070                         RXPKT_RLINK) & ~EOL);
1071                 sc->sc_rdamark = sc->sc_rxmark;
1072
1073                 if (++sc->sc_rxmark >= sc->sc_nrda)
1074                         sc->sc_rxmark = 0;
1075                 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1076         }
1077 }
1078
1079 /*
1080  * sonic_read -- pull packet off interface and forward to
1081  * appropriate protocol handler
1082  */
1083 hide int
1084 sonic_read(sc, pkt, len)
1085         struct snc_softc *sc;
1086         u_int32_t pkt;
1087         int len;
1088 {
1089         struct ifnet *ifp = &sc->sc_if;
1090         struct mbuf *m;
1091
1092         if (len <= sizeof(struct ether_header) ||
1093             len > ETHERMTU + sizeof(struct ether_header)) {
1094                 device_printf(sc->sc_dev,
1095                               "invalid packet length %d bytes\n", len);
1096                 return (0);
1097         }
1098
1099         /* Pull packet off interface. */
1100         m = sonic_get(sc, pkt, len);
1101         if (m == 0) {
1102                 return (0);
1103         }
1104
1105         (*ifp->if_input)(ifp, m);
1106         return (1);
1107 }
1108
1109
1110 /*
1111  * munge the received packet into an mbuf chain
1112  */
1113 hide struct mbuf *
1114 sonic_get(sc, pkt, datalen)
1115         struct snc_softc *sc;
1116         u_int32_t pkt;
1117         int datalen;
1118 {
1119         struct  mbuf *m, *top, **mp;
1120         int     len;
1121         /*
1122          * Do not trunc ether_header length.
1123          * Our sonic_read() and sonic_get() require it.
1124          */
1125
1126         MGETHDR(m, MB_DONTWAIT, MT_DATA);
1127         if (m == 0)
1128                 return (0);
1129         m->m_pkthdr.rcvif = &sc->sc_if;
1130         m->m_pkthdr.len = datalen;
1131         len = MHLEN;
1132         top = 0;
1133         mp = &top;
1134
1135         while (datalen > 0) {
1136                 if (top) {
1137                         MGET(m, MB_DONTWAIT, MT_DATA);
1138                         if (m == 0) {
1139                                 m_freem(top);
1140                                 return (0);
1141                         }
1142                         len = MLEN;
1143                 }
1144                 if (datalen >= MINCLSIZE) {
1145                         MCLGET(m, MB_DONTWAIT);
1146                         if ((m->m_flags & M_EXT) == 0) {
1147                                 if (top) m_freem(top);
1148                                 return (0);
1149                         }
1150                         len = MCLBYTES;
1151                 }
1152 #if 0
1153                 /* XXX: Require? */
1154                 if (!top) {
1155                         int pad =
1156                             ALIGN(sizeof(struct ether_header)) -
1157                                 sizeof(struct ether_header);
1158                         m->m_data += pad;
1159                         len -= pad;
1160                 }
1161 #endif
1162                 m->m_len = len = min(datalen, len);
1163
1164                 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), pkt, len);
1165                 pkt += len;
1166                 datalen -= len;
1167                 *mp = m;
1168                 mp = &m->m_next;
1169         }
1170
1171         return (top);
1172 }
1173 /*
1174  * Enable power on the interface.
1175  */
1176 int
1177 snc_enable(sc)
1178         struct snc_softc *sc;
1179 {
1180
1181 #ifdef  SNCDEBUG
1182         device_printf(sc->sc_dev, "snc_enable()\n");
1183 #endif  /* SNCDEBUG */
1184
1185         if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1186                 if ((*sc->sc_enable)(sc) != 0) {
1187                         device_printf(sc->sc_dev, "device enable failed\n");
1188                         return (EIO);
1189                 }
1190         }
1191
1192         sc->sc_enabled = 1;
1193         return (0);
1194 }
1195
1196 /*
1197  * Disable power on the interface.
1198  */
1199 void
1200 snc_disable(sc)
1201         struct snc_softc *sc;
1202 {
1203
1204 #ifdef  SNCDEBUG
1205         device_printf(sc->sc_dev, "snc_disable()\n");
1206 #endif  /* SNCDEBUG */
1207
1208         if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1209                 (*sc->sc_disable)(sc);
1210                 sc->sc_enabled = 0;
1211         }
1212 }
1213
1214