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