Merge from vendor branch LESS:
[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.8 2004/03/23 22:19:03 hsu 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         device_printf(sc->sc_dev, "address %6D\n", eaddr, ":");
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_output = ether_output;
191         ifp->if_start = sncstart;
192         ifp->if_flags =
193             IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
194         ifp->if_watchdog = sncwatchdog;
195         ifp->if_init = sncinit;
196         ifp->if_mtu = ETHERMTU;
197         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
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         IF_DEQUEUE(&ifp->if_snd, m);
361         if (m == 0)
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 bpf is listening on this interface, let it
371          * see the packet before we commit it to the wire.
372          */
373         if (ifp->if_bpf)
374                 bpf_mtap(ifp, m);
375
376         /*
377          * If there is nothing in the o/p queue, and there is room in
378          * the Tx ring, then send the packet directly.  Otherwise append
379          * it to the o/p queue.
380          */
381         if ((sonicput(sc, m, mtd_next)) == 0) {
382                 IF_PREPEND(&ifp->if_snd, m);
383                 return;
384         }
385
386         sc->mtd_prev = sc->mtd_free;
387         sc->mtd_free = mtd_next;
388
389         ifp->if_opackets++;             /* # of pkts */
390
391         /* Jump back for possibly more punishment. */
392         goto outloop;
393 }
394
395 /*
396  * reset and restart the SONIC.  Called in case of fatal
397  * hardware/software errors.
398  */
399 hide void
400 sncreset(sc)
401         struct snc_softc *sc;
402 {
403         sncstop(sc);
404         sncinit(sc);
405 }
406
407 hide void
408 sncinit(xsc)
409         void *xsc;
410 {
411         struct snc_softc *sc = xsc;
412         u_long  s_rcr;
413         int     s;
414
415         if (sc->sc_if.if_flags & IFF_RUNNING)
416                 /* already running */
417                 return;
418
419         s = splhardnet();
420
421         NIC_PUT(sc, SNCR_CR, CR_RST);   /* DCR only accessable in reset mode! */
422
423         /* config it */
424         NIC_PUT(sc, SNCR_DCR, (sc->sncr_dcr |
425                 (sc->bitmode ? DCR_DW32 : DCR_DW16)));
426         NIC_PUT(sc, SNCR_DCR2, sc->sncr_dcr2);
427
428         s_rcr = RCR_BRD | RCR_LBNONE;
429         if (sc->sc_if.if_flags & IFF_PROMISC)
430                 s_rcr |= RCR_PRO;
431         if (sc->sc_if.if_flags & IFF_ALLMULTI)
432                 s_rcr |= RCR_AMC;
433         NIC_PUT(sc, SNCR_RCR, s_rcr);
434
435         NIC_PUT(sc, SNCR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
436
437         /* clear pending interrupts */
438         NIC_PUT(sc, SNCR_ISR, ISR_ALL);
439
440         /* clear tally counters */
441         NIC_PUT(sc, SNCR_CRCT, -1);
442         NIC_PUT(sc, SNCR_FAET, -1);
443         NIC_PUT(sc, SNCR_MPT, -1);
444
445         initialise_tda(sc);
446         initialise_rda(sc);
447         initialise_rra(sc);
448
449         /* enable the chip */
450         NIC_PUT(sc, SNCR_CR, 0);
451         wbflush();
452
453         /* program the CAM */
454         camprogram(sc);
455
456         /* get it to read resource descriptors */
457         NIC_PUT(sc, SNCR_CR, CR_RRRA);
458         wbflush();
459         while ((NIC_GET(sc, SNCR_CR)) & CR_RRRA)
460                 continue;
461
462         /* enable rx */
463         NIC_PUT(sc, SNCR_CR, CR_RXEN);
464         wbflush();
465
466         /* flag interface as "running" */
467         sc->sc_if.if_flags |= IFF_RUNNING;
468         sc->sc_if.if_flags &= ~IFF_OACTIVE;
469
470         splx(s);
471         return;
472 }
473
474 /*
475  * close down an interface and free its buffers
476  * Called on final close of device, or if sncinit() fails
477  * part way through.
478  */
479 hide int
480 sncstop(sc)
481         struct snc_softc *sc;
482 {
483         struct mtd *mtd;
484         int     s = splhardnet();
485
486         /* stick chip in reset */
487         NIC_PUT(sc, SNCR_CR, CR_RST);
488         wbflush();
489
490         /* free all receive buffers (currently static so nothing to do) */
491
492         /* free all pending transmit mbufs */
493         while (sc->mtd_hw != sc->mtd_free) {
494                 mtd = &sc->mtda[sc->mtd_hw];
495                 if (mtd->mtd_mbuf)
496                         m_freem(mtd->mtd_mbuf);
497                 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
498         }
499
500         sc->sc_if.if_timer = 0;
501         sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
502
503         splx(s);
504         return (0);
505 }
506
507 /*
508  * Called if any Tx packets remain unsent after 5 seconds,
509  * In all cases we just reset the chip, and any retransmission
510  * will be handled by higher level protocol timeouts.
511  */
512 hide void
513 sncwatchdog(ifp)
514         struct ifnet *ifp;
515 {
516         struct snc_softc *sc = ifp->if_softc;
517         struct mtd *mtd;
518         int     temp;
519
520         if (sc->mtd_hw != sc->mtd_free) {
521                 /* something still pending for transmit */
522                 mtd = &sc->mtda[sc->mtd_hw];
523                 if (SRO(sc, mtd->mtd_vtxp, TXP_STATUS) == 0)
524                         log(LOG_ERR, "%s: Tx - timeout\n",
525                             device_get_nameunit(sc->sc_dev));
526                 else
527                         log(LOG_ERR, "%s: Tx - lost interrupt\n",
528                             device_get_nameunit(sc->sc_dev));
529                 temp = ifp->if_flags & IFF_UP;
530                 sncreset(sc);
531                 ifp->if_flags |= temp;
532         }
533 }
534
535 /*
536  * stuff packet into sonic (at splnet)
537  */
538 hide u_int
539 sonicput(sc, m0, mtd_next)
540         struct snc_softc *sc;
541         struct mbuf *m0;
542         int mtd_next;
543 {
544         struct mtd *mtdp;
545         struct mbuf *m;
546         u_int32_t buff;
547         u_int32_t txp;
548         u_int   len = 0;
549         u_int   totlen = 0;
550
551 #ifdef whyonearthwouldyoudothis
552         if (NIC_GET(sc, SNCR_CR) & CR_TXP)
553                 return (0);
554 #endif
555
556         /* grab the replacement mtd */
557         mtdp = &sc->mtda[sc->mtd_free];
558
559         buff = mtdp->mtd_vbuf;
560         
561         /* this packet goes to mtdnext fill in the TDA */
562         mtdp->mtd_mbuf = m0;
563         txp = mtdp->mtd_vtxp;
564
565         /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
566         if (sc->mtd_pint == 0) {
567                 sc->mtd_pint = NTDA/2;
568                 SWO(sc, txp, TXP_CONFIG, TCR_PINT);
569         } else {
570                 sc->mtd_pint--;
571                 SWO(sc, txp, TXP_CONFIG, 0);
572         }
573
574         for (m = m0; m; m = m->m_next) {
575                 len = m->m_len;
576                 totlen += len;
577                 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), buff, len);
578                 buff += len;
579         }
580         if (totlen >= TXBSIZE) {
581                 panic("%s: sonicput: packet overflow",
582                       device_get_nameunit(sc->sc_dev));
583         }
584
585         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
586             LOWER(mtdp->mtd_vbuf));
587         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
588             UPPER(mtdp->mtd_vbuf));
589
590         if (totlen < ETHERMIN + sizeof(struct ether_header)) {
591                 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
592                 (*sc->sc_zerobuf)(sc, mtdp->mtd_vbuf + totlen, pad);
593                 totlen = ETHERMIN + sizeof(struct ether_header);
594         }
595
596         SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
597             totlen);
598         SWO(sc, txp, TXP_FRAGCNT, 1);
599         SWO(sc, txp, TXP_PKTSIZE, totlen);
600
601         /* link onto the next mtd that will be used */
602         SWO(sc, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
603             LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
604
605         /*
606          * The previous txp.tlink currently contains a pointer to
607          * our txp | EOL. Want to clear the EOL, so write our
608          * pointer to the previous txp.
609          */
610         SWO(sc, sc->mtda[sc->mtd_prev].mtd_vtxp, sc->mtd_tlinko,
611             LOWER(mtdp->mtd_vtxp));
612
613         /* make sure chip is running */
614         wbflush();
615         NIC_PUT(sc, SNCR_CR, CR_TXP);
616         wbflush();
617         sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
618
619         return (totlen);
620 }
621
622 /*
623  * These are called from sonicioctl() when /etc/ifconfig is run to set
624  * the address or switch the i/f on.
625  */
626 /*
627  * CAM support
628  */
629 hide void
630 caminitialise(sc)
631         struct snc_softc *sc;
632 {
633         u_int32_t v_cda = sc->v_cda;
634         int     i;
635         int     camoffset;
636
637         for (i = 0; i < MAXCAM; i++) {
638                 camoffset = i * CDA_CAMDESC;
639                 SWO(sc, v_cda, (camoffset + CDA_CAMEP), i);
640                 SWO(sc, v_cda, (camoffset + CDA_CAMAP2), 0);
641                 SWO(sc, v_cda, (camoffset + CDA_CAMAP1), 0);
642                 SWO(sc, v_cda, (camoffset + CDA_CAMAP0), 0);
643         }
644         SWO(sc, v_cda, CDA_ENABLE, 0);
645
646 #ifdef SNCDEBUG
647         if ((sncdebug & SNC_SHOWCAMENT) != 0) {
648                 camdump(sc);
649         }
650 #endif
651 }
652
653 hide void
654 camentry(sc, entry, ea)
655         int entry;
656         u_char *ea;
657         struct snc_softc *sc;
658 {
659         u_int32_t v_cda = sc->v_cda;
660         int     camoffset = entry * CDA_CAMDESC;
661
662         SWO(sc, v_cda, camoffset + CDA_CAMEP, entry);
663         SWO(sc, v_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
664         SWO(sc, v_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
665         SWO(sc, v_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
666         SWO(sc, v_cda, CDA_ENABLE, 
667             (SRO(sc, v_cda, CDA_ENABLE) | (1 << entry)));
668 }
669
670 hide void
671 camprogram(sc)
672         struct snc_softc *sc;
673 {
674         struct ifmultiaddr      *ifma;
675         struct ifnet *ifp;
676         int     timeout;
677         int     mcount = 0;
678
679         caminitialise(sc);
680
681         ifp = &sc->sc_if;
682
683         /* Always load our own address first. */
684         camentry (sc, mcount, sc->sc_ethercom.ac_enaddr);
685         mcount++;
686
687         /* Assume we won't need allmulti bit. */
688         ifp->if_flags &= ~IFF_ALLMULTI;
689
690         /* Loop through multicast addresses */
691         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
692                                 ifma = ifma->ifma_link.le_next) {
693                 if (ifma->ifma_addr->sa_family != AF_LINK)
694                         continue;
695                 if (mcount == MAXCAM) {
696                          ifp->if_flags |= IFF_ALLMULTI;
697                          break;
698                 }
699
700                 /* program the CAM with the specified entry */
701                 camentry(sc, mcount,
702                          LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
703                 mcount++;
704         }
705
706         NIC_PUT(sc, SNCR_CDP, LOWER(sc->v_cda));
707         NIC_PUT(sc, SNCR_CDC, MAXCAM);
708         NIC_PUT(sc, SNCR_CR, CR_LCAM);
709         wbflush();
710
711         timeout = 10000;
712         while ((NIC_GET(sc, SNCR_CR) & CR_LCAM) && timeout--)
713                 continue;
714         if (timeout == 0) {
715                 /* XXX */
716                 panic("%s: CAM initialisation failed\n",
717                       device_get_nameunit(sc->sc_dev));
718         }
719         timeout = 10000;
720         while (((NIC_GET(sc, SNCR_ISR) & ISR_LCD) == 0) && timeout--)
721                 continue;
722
723         if (NIC_GET(sc, SNCR_ISR) & ISR_LCD)
724                 NIC_PUT(sc, SNCR_ISR, ISR_LCD);
725         else
726                 device_printf(sc->sc_dev,
727                               "CAM initialisation without interrupt\n");
728 }
729
730 #ifdef SNCDEBUG
731 hide void
732 camdump(sc)
733         struct snc_softc *sc;
734 {
735         int     i;
736
737         printf("CAM entries:\n");
738         NIC_PUT(sc, SNCR_CR, CR_RST);
739         wbflush();
740
741         for (i = 0; i < 16; i++) {
742                 ushort  ap2, ap1, ap0;
743                 NIC_PUT(sc, SNCR_CEP, i);
744                 wbflush();
745                 ap2 = NIC_GET(sc, SNCR_CAP2);
746                 ap1 = NIC_GET(sc, SNCR_CAP1);
747                 ap0 = NIC_GET(sc, SNCR_CAP0);
748                 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
749         }
750         printf("CAM enable 0x%x\n", NIC_GET(sc, SNCR_CEP));
751
752         NIC_PUT(sc, SNCR_CR, 0);
753         wbflush();
754 }
755 #endif
756
757 hide void
758 initialise_tda(sc)
759         struct snc_softc *sc;
760 {
761         struct mtd *mtd;
762         int     i;
763
764         for (i = 0; i < NTDA; i++) {
765                 mtd = &sc->mtda[i];
766                 mtd->mtd_mbuf = 0;
767         }
768
769         sc->mtd_hw = 0;
770         sc->mtd_prev = NTDA - 1;
771         sc->mtd_free = 0;
772         sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
773         sc->mtd_pint = NTDA/2;
774
775         NIC_PUT(sc, SNCR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
776         NIC_PUT(sc, SNCR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
777 }
778
779 hide void
780 initialise_rda(sc)
781         struct snc_softc *sc;
782 {
783         int             i;
784         u_int32_t       vv_rda = 0;
785         u_int32_t       v_rda = 0;
786
787         /* link the RDA's together into a circular list */
788         for (i = 0; i < (sc->sc_nrda - 1); i++) {
789                 v_rda = sc->v_rda + (i * RXPKT_SIZE(sc));
790                 vv_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
791                 SWO(sc, v_rda, RXPKT_RLINK, LOWER(vv_rda));
792                 SWO(sc, v_rda, RXPKT_INUSE, 1);
793         }
794         v_rda = sc->v_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
795         SWO(sc, v_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
796         SWO(sc, v_rda, RXPKT_INUSE, 1);
797
798         /* mark end of receive descriptor list */
799         sc->sc_rdamark = sc->sc_nrda - 1;
800
801         sc->sc_rxmark = 0;
802
803         NIC_PUT(sc, SNCR_URDA, UPPER(sc->v_rda));
804         NIC_PUT(sc, SNCR_CRDA, LOWER(sc->v_rda));
805         wbflush();
806 }
807
808 hide void
809 initialise_rra(sc)
810         struct snc_softc *sc;
811 {
812         int     i;
813         u_int   v;
814         int     bitmode = sc->bitmode;
815
816         if (bitmode)
817                 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 2);
818         else
819                 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 1);
820
821         NIC_PUT(sc, SNCR_URRA, UPPER(sc->v_rra[0]));
822         NIC_PUT(sc, SNCR_RSA, LOWER(sc->v_rra[0]));
823         /* rea must point just past the end of the rra space */
824         NIC_PUT(sc, SNCR_REA, LOWER(sc->v_rea));
825         NIC_PUT(sc, SNCR_RRP, LOWER(sc->v_rra[0]));
826         NIC_PUT(sc, SNCR_RSC, 0);
827
828         /* fill up SOME of the rra with buffers */
829         for (i = 0; i < NRBA; i++) {
830                 v = SONIC_GETDMA(sc->rbuf[i]);
831                 SWO(sc, sc->v_rra[i], RXRSRC_PTRHI, UPPER(v));
832                 SWO(sc, sc->v_rra[i], RXRSRC_PTRLO, LOWER(v));
833                 SWO(sc, sc->v_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
834                 SWO(sc, sc->v_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
835         }
836         sc->sc_rramark = NRBA;
837         NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
838         wbflush();
839 }
840
841 void
842 sncintr(arg)
843         void    *arg;
844 {
845         struct snc_softc *sc = (struct snc_softc *)arg;
846         int     isr;
847
848         if (sc->sc_enabled == 0)
849                 return;
850
851         while ((isr = (NIC_GET(sc, SNCR_ISR) & ISR_ALL)) != 0) {
852                 /* scrub the interrupts that we are going to service */
853                 NIC_PUT(sc, SNCR_ISR, isr);
854                 wbflush();
855
856                 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
857                         device_printf(sc->sc_dev,
858                                       "unexpected interrupt status 0x%x\n",
859                                       isr);
860
861                 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
862                         sonictxint(sc);
863
864                 if (isr & ISR_PKTRX)
865                         sonicrxint(sc);
866
867                 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
868                         if (isr & ISR_HBL)
869                                 /*
870                                  * The repeater is not providing a heartbeat.
871                                  * In itself this isn't harmful, lots of the
872                                  * cheap repeater hubs don't supply a heartbeat.
873                                  * So ignore the lack of heartbeat. Its only
874                                  * if we can't detect a carrier that we have a
875                                  * problem.
876                                  */
877                                 ;
878                         if (isr & ISR_RDE)
879                                 device_printf(sc->sc_dev, 
880                                         "receive descriptors exhausted\n");
881                         if (isr & ISR_RBE)
882                                 device_printf(sc->sc_dev, 
883                                         "receive buffers exhausted\n");
884                         if (isr & ISR_RBAE)
885                                 device_printf(sc->sc_dev, 
886                                         "receive buffer area exhausted\n");
887                         if (isr & ISR_RFO)
888                                 device_printf(sc->sc_dev, 
889                                         "receive FIFO overrun\n");
890                 }
891                 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
892 #ifdef notdef
893                         if (isr & ISR_CRC)
894                                 sc->sc_crctally++;
895                         if (isr & ISR_FAE)
896                                 sc->sc_faetally++;
897                         if (isr & ISR_MP)
898                                 sc->sc_mptally++;
899 #endif
900                 }
901                 sncstart(&sc->sc_if);
902
903 #if NRND > 0
904                 if (isr)
905                         rnd_add_uint32(&sc->rnd_source, isr);
906 #endif
907         }
908         return;
909 }
910
911 /*
912  * Transmit interrupt routine
913  */
914 hide void
915 sonictxint(sc)
916         struct snc_softc *sc;
917 {
918         struct mtd      *mtd;
919         u_int32_t       txp;
920         unsigned short  txp_status;
921         int             mtd_hw;
922         struct ifnet    *ifp = &sc->sc_if;
923
924         mtd_hw = sc->mtd_hw;
925
926         if (mtd_hw == sc->mtd_free)
927                 return;
928
929         while (mtd_hw != sc->mtd_free) {
930                 mtd = &sc->mtda[mtd_hw];
931
932                 txp = mtd->mtd_vtxp;
933
934                 if (SRO(sc, txp, TXP_STATUS) == 0) {
935                         break; /* it hasn't really gone yet */
936                 }
937
938 #ifdef SNCDEBUG
939                 if ((sncdebug & SNC_SHOWTXHDR) != 0)
940                 {
941                         struct ether_header eh;
942
943                         (*sc->sc_copyfrombuf)(sc, &eh, mtd->mtd_vbuf, sizeof(eh));
944                         device_printf(sc->sc_dev,
945                             "xmit status=0x%x len=%d type=0x%x from %6D",
946                             SRO(sc, txp, TXP_STATUS),
947                             SRO(sc, txp, TXP_PKTSIZE),
948                             htons(eh.ether_type),
949                             eh.ether_shost, ":");
950                         printf(" (to %6D)\n", eh.ether_dhost, ":");
951                 }
952 #endif /* SNCDEBUG */
953
954                 ifp->if_flags &= ~IFF_OACTIVE;
955
956                 if (mtd->mtd_mbuf != 0) {
957                         m_freem(mtd->mtd_mbuf);
958                         mtd->mtd_mbuf = 0;
959                 }
960                 if (++mtd_hw == NTDA) mtd_hw = 0;
961
962                 txp_status = SRO(sc, txp, TXP_STATUS);
963
964                 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
965                         ((txp_status & TCR_NC) >> 12);
966
967                 if ((txp_status & TCR_PTX) == 0) {
968                         ifp->if_oerrors++;
969                         device_printf(sc->sc_dev, "Tx packet status=0x%x\n",
970                                       txp_status);
971                         
972                         /* XXX - DG This looks bogus */
973                         if (mtd_hw != sc->mtd_free) {
974                                 printf("resubmitting remaining packets\n");
975                                 mtd = &sc->mtda[mtd_hw];
976                                 NIC_PUT(sc, SNCR_CTDA, LOWER(mtd->mtd_vtxp));
977                                 NIC_PUT(sc, SNCR_CR, CR_TXP);
978                                 wbflush();
979                                 break;
980                         }
981                 }
982         }
983
984         sc->mtd_hw = mtd_hw;
985         return;
986 }
987
988 /*
989  * Receive interrupt routine
990  */
991 hide void
992 sonicrxint(sc)
993         struct snc_softc *sc;
994 {
995         u_int32_t rda;
996         int     orra;
997         int     len;
998         int     rramark;
999         int     rdamark;
1000         u_int16_t rxpkt_ptr;
1001
1002         rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1003
1004         while (SRO(sc, rda, RXPKT_INUSE) == 0) {
1005                 u_int status = SRO(sc, rda, RXPKT_STATUS);
1006
1007                 orra = RBASEQ(SRO(sc, rda, RXPKT_SEQNO)) & RRAMASK;
1008                 rxpkt_ptr = SRO(sc, rda, RXPKT_PTRLO);
1009                 /*
1010                  * Do not trunc ether_header length.
1011                  * Our sonic_read() and sonic_get() require it.
1012                  */
1013                 len = SRO(sc, rda, RXPKT_BYTEC) - FCSSIZE;
1014                 if (status & RCR_PRX) {
1015                         /* XXX: Does PGOFSET require? */
1016                         u_int32_t pkt =
1017                             sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
1018                         if (sonic_read(sc, pkt, len))
1019                                 sc->sc_if.if_ipackets++;
1020                         else
1021                                 sc->sc_if.if_ierrors++;
1022                 } else
1023                         sc->sc_if.if_ierrors++;
1024
1025                 /*
1026                  * give receive buffer area back to chip.
1027                  *
1028                  * If this was the last packet in the RRA, give the RRA to
1029                  * the chip again.
1030                  * If sonic read didnt copy it out then we would have to
1031                  * wait !!
1032                  * (dont bother add it back in again straight away)
1033                  *
1034                  * Really, we're doing v_rra[rramark] = v_rra[orra] but
1035                  * we have to use the macros because SONIC might be in
1036                  * 16 or 32 bit mode.
1037                  */
1038                 if (status & RCR_LPKT) {
1039                         u_int32_t tmp1, tmp2;
1040
1041                         rramark = sc->sc_rramark;
1042                         tmp1 = sc->v_rra[rramark];
1043                         tmp2 = sc->v_rra[orra];
1044                         SWO(sc, tmp1, RXRSRC_PTRLO,
1045                                 SRO(sc, tmp2, RXRSRC_PTRLO));
1046                         SWO(sc, tmp1, RXRSRC_PTRHI,
1047                                 SRO(sc, tmp2, RXRSRC_PTRHI));
1048                         SWO(sc, tmp1, RXRSRC_WCLO,
1049                                 SRO(sc, tmp2, RXRSRC_WCLO));
1050                         SWO(sc, tmp1, RXRSRC_WCHI,
1051                                 SRO(sc, tmp2, RXRSRC_WCHI));
1052
1053                         /* zap old rra for fun */
1054                         SWO(sc, tmp2, RXRSRC_WCHI, 0);
1055                         SWO(sc, tmp2, RXRSRC_WCLO, 0);
1056
1057                         sc->sc_rramark = (++rramark) & RRAMASK;
1058                         NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[rramark]));
1059                         wbflush();
1060                 }
1061
1062                 /*
1063                  * give receive descriptor back to chip simple
1064                  * list is circular
1065                  */
1066                 rdamark = sc->sc_rdamark;
1067                 SWO(sc, rda, RXPKT_INUSE, 1);
1068                 SWO(sc, rda, RXPKT_RLINK,
1069                         SRO(sc, rda, RXPKT_RLINK) | EOL);
1070                 SWO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1071                         SRO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))),
1072                         RXPKT_RLINK) & ~EOL);
1073                 sc->sc_rdamark = sc->sc_rxmark;
1074
1075                 if (++sc->sc_rxmark >= sc->sc_nrda)
1076                         sc->sc_rxmark = 0;
1077                 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1078         }
1079 }
1080
1081 /*
1082  * sonic_read -- pull packet off interface and forward to
1083  * appropriate protocol handler
1084  */
1085 hide int
1086 sonic_read(sc, pkt, len)
1087         struct snc_softc *sc;
1088         u_int32_t pkt;
1089         int len;
1090 {
1091         struct ifnet *ifp = &sc->sc_if;
1092         struct ether_header *et;
1093         struct mbuf *m;
1094
1095         if (len <= sizeof(struct ether_header) ||
1096             len > ETHERMTU + sizeof(struct ether_header)) {
1097                 device_printf(sc->sc_dev,
1098                               "invalid packet length %d bytes\n", len);
1099                 return (0);
1100         }
1101
1102         /* Pull packet off interface. */
1103         m = sonic_get(sc, pkt, len);
1104         if (m == 0) {
1105                 return (0);
1106         }
1107
1108         /* We assume that the header fit entirely in one mbuf. */
1109         et = mtod(m, struct ether_header *);
1110
1111 #ifdef SNCDEBUG
1112         if ((sncdebug & SNC_SHOWRXHDR) != 0)
1113         {
1114                 device_printf(sc->sc_dev, "rcvd 0x%x len=%d type=0x%x from %6D",
1115                     pkt, len, htons(et->ether_type),
1116                     et->ether_shost, ":");
1117                 printf(" (to %6D)\n", et->ether_dhost, ":");
1118         }
1119 #endif /* SNCDEBUG */
1120
1121         /* Pass the packet up, with the ether header sort-of removed. */
1122         m_adj(m, sizeof(struct ether_header));
1123         ether_input(ifp, et, m);
1124         return (1);
1125 }
1126
1127
1128 /*
1129  * munge the received packet into an mbuf chain
1130  */
1131 hide struct mbuf *
1132 sonic_get(sc, pkt, datalen)
1133         struct snc_softc *sc;
1134         u_int32_t pkt;
1135         int datalen;
1136 {
1137         struct  mbuf *m, *top, **mp;
1138         int     len;
1139         /*
1140          * Do not trunc ether_header length.
1141          * Our sonic_read() and sonic_get() require it.
1142          */
1143
1144         MGETHDR(m, M_DONTWAIT, MT_DATA);
1145         if (m == 0)
1146                 return (0);
1147         m->m_pkthdr.rcvif = &sc->sc_if;
1148         m->m_pkthdr.len = datalen;
1149         len = MHLEN;
1150         top = 0;
1151         mp = &top;
1152
1153         while (datalen > 0) {
1154                 if (top) {
1155                         MGET(m, M_DONTWAIT, MT_DATA);
1156                         if (m == 0) {
1157                                 m_freem(top);
1158                                 return (0);
1159                         }
1160                         len = MLEN;
1161                 }
1162                 if (datalen >= MINCLSIZE) {
1163                         MCLGET(m, M_DONTWAIT);
1164                         if ((m->m_flags & M_EXT) == 0) {
1165                                 if (top) m_freem(top);
1166                                 return (0);
1167                         }
1168                         len = MCLBYTES;
1169                 }
1170 #if 0
1171                 /* XXX: Require? */
1172                 if (!top) {
1173                         int pad =
1174                             ALIGN(sizeof(struct ether_header)) -
1175                                 sizeof(struct ether_header);
1176                         m->m_data += pad;
1177                         len -= pad;
1178                 }
1179 #endif
1180                 m->m_len = len = min(datalen, len);
1181
1182                 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), pkt, len);
1183                 pkt += len;
1184                 datalen -= len;
1185                 *mp = m;
1186                 mp = &m->m_next;
1187         }
1188
1189         return (top);
1190 }
1191 /*
1192  * Enable power on the interface.
1193  */
1194 int
1195 snc_enable(sc)
1196         struct snc_softc *sc;
1197 {
1198
1199 #ifdef  SNCDEBUG
1200         device_printf(sc->sc_dev, "snc_enable()\n");
1201 #endif  /* SNCDEBUG */
1202
1203         if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1204                 if ((*sc->sc_enable)(sc) != 0) {
1205                         device_printf(sc->sc_dev, "device enable failed\n");
1206                         return (EIO);
1207                 }
1208         }
1209
1210         sc->sc_enabled = 1;
1211         return (0);
1212 }
1213
1214 /*
1215  * Disable power on the interface.
1216  */
1217 void
1218 snc_disable(sc)
1219         struct snc_softc *sc;
1220 {
1221
1222 #ifdef  SNCDEBUG
1223         device_printf(sc->sc_dev, "snc_disable()\n");
1224 #endif  /* SNCDEBUG */
1225
1226         if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1227                 (*sc->sc_disable)(sc);
1228                 sc->sc_enabled = 0;
1229         }
1230 }
1231
1232