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