2 /* $NecBSD: dp83932.c,v 1.5 1999/07/29 05:08:44 kmatsuda Exp $ */
3 /* $NetBSD: if_snc.c,v 1.18 1998/04/25 21:27:40 scottr Exp $ */
6 * Copyright (c) 1997, 1998, 1999
7 * Kouichi Matsuda. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kouichi Matsuda for
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Modified for FreeBSD(98) 4.0 from NetBSD/pc98 1.4.2 by Motomichi Matsuzaki.
41 * Modified for NetBSD/pc98 1.2G from NetBSD/mac68k 1.2G by Kouichi Matsuda.
42 * Make adapted for NEC PC-9801-83, 84, PC-9801-103, 104, PC-9801N-25 and
43 * PC-9801N-J02, J02R, which uses National Semiconductor DP83934AVQB as
44 * Ethernet Controller and National Semiconductor NS46C46 as
45 * (64 * 16 bits) Microwire Serial EEPROM.
49 * National Semiconductor DP8393X SONIC Driver
50 * Copyright (c) 1991 Algorithmics Ltd (http://www.algor.co.uk)
51 * You may use, copy, and modify this program so long as you retain the
54 * This driver has been substantially modified since Algorithmics donated
57 * Denton Gentry <denny1@home.com>
59 * Yanagisawa Takeshi <yanagisw@aa.ap.titech.ac.jp>
60 * did the work to get this running on the Macintosh.
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/sockio.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/syslog.h>
72 #include <sys/errno.h>
73 #if defined(NRND) && NRND > 0
77 #include <net/ethernet.h>
79 #include <net/if_arp.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82 #include <net/if_types.h>
87 #include <machine/bus.h>
88 #include <dev/snc/dp83932reg.h>
89 #include <dev/snc/dp83932var.h>
91 hide void sncwatchdog(struct ifnet *);
92 hide void sncinit(void *);
93 hide int sncstop(struct snc_softc *sc);
94 hide int sncioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
95 hide void sncstart(struct ifnet *ifp);
96 hide void sncreset(struct snc_softc *sc);
98 hide void caminitialise(struct snc_softc *);
99 hide void camentry(struct snc_softc *, int, u_char *ea);
100 hide void camprogram(struct snc_softc *);
101 hide void initialise_tda(struct snc_softc *);
102 hide void initialise_rda(struct snc_softc *);
103 hide void initialise_rra(struct snc_softc *);
105 hide void camdump(struct snc_softc *sc);
108 hide void sonictxint(struct snc_softc *);
109 hide void sonicrxint(struct snc_softc *);
111 hide u_int sonicput(struct snc_softc *sc, struct mbuf *m0, int mtd_next);
112 hide int sonic_read(struct snc_softc *, u_int32_t, int);
113 hide struct mbuf *sonic_get(struct snc_softc *, u_int32_t, int);
115 int snc_enable(struct snc_softc *);
116 void snc_disable(struct snc_softc *);
118 int snc_mediachange(struct ifnet *);
119 void snc_mediastatus(struct ifnet *, struct ifmediareq *);
123 struct cfdriver snc_cd = {
124 NULL, "snc", DV_IFNET
133 #define assert(e) ((void)0)
134 #define _assert(e) ((void)0)
136 #define _assert(e) assert(e)
138 #define assert(e) ((e) ? (void)0 : __assert("snc ", __FILE__, __LINE__, #e))
140 #define assert(e) ((e) ? (void)0 : __assert("snc "__FILE__, __LINE__, "e"))
145 #define SNC_SHOWTXHDR 0x01 /* show tx ether_header */
146 #define SNC_SHOWRXHDR 0x02 /* show rx ether_header */
147 #define SNC_SHOWCAMENT 0x04 /* show CAM entry */
148 #endif /* SNCDEBUG */
153 sncconfig(sc, media, nmedia, defmedia, myea)
154 struct snc_softc *sc;
155 int *media, nmedia, defmedia;
162 if ((sncdebug & SNC_SHOWCAMENT) != 0) {
167 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
169 panic("%s: can not if_alloc()\n",
170 device_get_nameunit(sc->sc_dev));
173 device_printf(sc->sc_dev,
174 "buffers: rra=0x%x cda=0x%x rda=0x%x tda=0x%x\n",
175 sc->v_rra[0], sc->v_cda,
176 sc->v_rda, sc->mtda[0].mtd_vtxp);
180 if_initname(ifp, device_get_name(sc->sc_dev),
181 device_get_unit(sc->sc_dev));
182 ifp->if_ioctl = sncioctl;
183 ifp->if_start = sncstart;
185 IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_NEEDSGIANT;
186 ifp->if_watchdog = sncwatchdog;
187 ifp->if_init = sncinit;
188 ifp->if_mtu = ETHERMTU;
189 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
191 /* Initialize media goo. */
192 ifmedia_init(&sc->sc_media, 0, snc_mediachange,
195 for (i = 0; i < nmedia; i++)
196 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
197 ifmedia_set(&sc->sc_media, defmedia);
199 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
200 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
203 ether_ifattach(ifp, myea);
205 #if defined(NRND) && NRND > 0
206 rnd_attach_source(&sc->rnd_source, device_get_nameunit(sc->sc_dev),
216 sncstop((struct snc_softc *)arg);
220 * Media change callback.
226 struct snc_softc *sc = ifp->if_softc;
228 if (sc->sc_mediachange)
229 return ((*sc->sc_mediachange)(sc));
234 * Media status callback.
237 snc_mediastatus(ifp, ifmr)
239 struct ifmediareq *ifmr;
241 struct snc_softc *sc = ifp->if_softc;
243 if (sc->sc_enabled == 0) {
244 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
245 ifmr->ifm_status = 0;
249 if (sc->sc_mediastatus)
250 (*sc->sc_mediastatus)(sc, ifmr);
255 sncioctl(ifp, cmd, data)
261 struct snc_softc *sc = ifp->if_softc;
262 int s = splhardnet(), err = 0;
268 if ((ifp->if_flags & IFF_UP) == 0 &&
269 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
271 * If interface is marked down and it is running,
275 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
277 } else if ((ifp->if_flags & IFF_UP) != 0 &&
278 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
280 * If interface is marked up and it is stopped,
283 if ((err = snc_enable(sc)) != 0)
286 } else if (sc->sc_enabled) {
288 * reset the interface to pick up any other changes
291 temp = ifp->if_flags & IFF_UP;
293 ifp->if_flags |= temp;
300 if (sc->sc_enabled == 0) {
304 temp = ifp->if_flags & IFF_UP;
306 ifp->if_flags |= temp;
311 ifr = (struct ifreq *) data;
312 err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
315 err = ether_ioctl(ifp, cmd, data);
323 * Encapsulate a packet of type family for the local net.
329 struct snc_softc *sc = ifp->if_softc;
333 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
338 /* Check for room in the xmit buffer. */
339 if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
342 if (mtd_next == sc->mtd_hw) {
343 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
347 IF_DEQUEUE(&ifp->if_snd, m);
351 /* We need the header for m_pkthdr.len. */
355 * If there is nothing in the o/p queue, and there is room in
356 * the Tx ring, then send the packet directly. Otherwise append
357 * it to the o/p queue.
359 if ((sonicput(sc, m, mtd_next)) == 0) {
360 IF_PREPEND(&ifp->if_snd, m);
365 * If bpf is listening on this interface, let it see the packet
366 * before we commit it to the wire, but only if we are really
367 * committed to send it.
369 * XXX: Locking must protect m against premature m_freem() in
374 sc->mtd_prev = sc->mtd_free;
375 sc->mtd_free = mtd_next;
377 ifp->if_opackets++; /* # of pkts */
379 /* Jump back for possibly more punishment. */
384 * reset and restart the SONIC. Called in case of fatal
385 * hardware/software errors.
389 struct snc_softc *sc;
399 struct snc_softc *sc = xsc;
403 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
404 /* already running */
409 NIC_PUT(sc, SNCR_CR, CR_RST); /* DCR only accessable in reset mode! */
412 NIC_PUT(sc, SNCR_DCR, (sc->sncr_dcr |
413 (sc->bitmode ? DCR_DW32 : DCR_DW16)));
414 NIC_PUT(sc, SNCR_DCR2, sc->sncr_dcr2);
416 s_rcr = RCR_BRD | RCR_LBNONE;
417 if (sc->sc_ifp->if_flags & IFF_PROMISC)
419 if (sc->sc_ifp->if_flags & IFF_ALLMULTI)
421 NIC_PUT(sc, SNCR_RCR, s_rcr);
423 NIC_PUT(sc, SNCR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
425 /* clear pending interrupts */
426 NIC_PUT(sc, SNCR_ISR, ISR_ALL);
428 /* clear tally counters */
429 NIC_PUT(sc, SNCR_CRCT, -1);
430 NIC_PUT(sc, SNCR_FAET, -1);
431 NIC_PUT(sc, SNCR_MPT, -1);
437 /* enable the chip */
438 NIC_PUT(sc, SNCR_CR, 0);
441 /* program the CAM */
444 /* get it to read resource descriptors */
445 NIC_PUT(sc, SNCR_CR, CR_RRRA);
447 while ((NIC_GET(sc, SNCR_CR)) & CR_RRRA)
451 NIC_PUT(sc, SNCR_CR, CR_RXEN);
454 /* flag interface as "running" */
455 sc->sc_ifp->if_drv_flags |= IFF_DRV_RUNNING;
456 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
463 * close down an interface and free its buffers
464 * Called on final close of device, or if sncinit() fails
469 struct snc_softc *sc;
472 int s = splhardnet();
474 /* stick chip in reset */
475 NIC_PUT(sc, SNCR_CR, CR_RST);
478 /* free all receive buffers (currently static so nothing to do) */
480 /* free all pending transmit mbufs */
481 while (sc->mtd_hw != sc->mtd_free) {
482 mtd = &sc->mtda[sc->mtd_hw];
484 m_freem(mtd->mtd_mbuf);
485 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
488 sc->sc_ifp->if_timer = 0;
489 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
490 sc->sc_ifp->if_flags &= ~IFF_UP;
497 * Called if any Tx packets remain unsent after 5 seconds,
498 * In all cases we just reset the chip, and any retransmission
499 * will be handled by higher level protocol timeouts.
505 struct snc_softc *sc = ifp->if_softc;
509 if (sc->mtd_hw != sc->mtd_free) {
510 /* something still pending for transmit */
511 mtd = &sc->mtda[sc->mtd_hw];
512 if (SRO(sc, mtd->mtd_vtxp, TXP_STATUS) == 0)
513 log(LOG_ERR, "%s: Tx - timeout\n",
514 device_get_nameunit(sc->sc_dev));
516 log(LOG_ERR, "%s: Tx - lost interrupt\n",
517 device_get_nameunit(sc->sc_dev));
518 temp = ifp->if_flags & IFF_UP;
520 ifp->if_flags |= temp;
525 * stuff packet into sonic (at splnet)
528 sonicput(sc, m0, mtd_next)
529 struct snc_softc *sc;
540 #ifdef whyonearthwouldyoudothis
541 if (NIC_GET(sc, SNCR_CR) & CR_TXP)
545 /* grab the replacement mtd */
546 mtdp = &sc->mtda[sc->mtd_free];
548 buff = mtdp->mtd_vbuf;
550 /* this packet goes to mtdnext fill in the TDA */
552 txp = mtdp->mtd_vtxp;
554 /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
555 if (sc->mtd_pint == 0) {
556 sc->mtd_pint = NTDA/2;
557 SWO(sc, txp, TXP_CONFIG, TCR_PINT);
560 SWO(sc, txp, TXP_CONFIG, 0);
563 for (m = m0; m; m = m->m_next) {
566 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), buff, len);
569 if (totlen >= TXBSIZE) {
570 panic("%s: sonicput: packet overflow",
571 device_get_nameunit(sc->sc_dev));
574 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
575 LOWER(mtdp->mtd_vbuf));
576 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
577 UPPER(mtdp->mtd_vbuf));
579 if (totlen < ETHERMIN + sizeof(struct ether_header)) {
580 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
581 (*sc->sc_zerobuf)(sc, mtdp->mtd_vbuf + totlen, pad);
582 totlen = ETHERMIN + sizeof(struct ether_header);
585 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
587 SWO(sc, txp, TXP_FRAGCNT, 1);
588 SWO(sc, txp, TXP_PKTSIZE, totlen);
590 /* link onto the next mtd that will be used */
591 SWO(sc, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
592 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
595 * The previous txp.tlink currently contains a pointer to
596 * our txp | EOL. Want to clear the EOL, so write our
597 * pointer to the previous txp.
599 SWO(sc, sc->mtda[sc->mtd_prev].mtd_vtxp, sc->mtd_tlinko,
600 LOWER(mtdp->mtd_vtxp));
602 /* make sure chip is running */
604 NIC_PUT(sc, SNCR_CR, CR_TXP);
606 sc->sc_ifp->if_timer = 5; /* 5 seconds to watch for failing to transmit */
612 * These are called from sonicioctl() when /etc/ifconfig is run to set
613 * the address or switch the i/f on.
620 struct snc_softc *sc;
622 u_int32_t v_cda = sc->v_cda;
626 for (i = 0; i < MAXCAM; i++) {
627 camoffset = i * CDA_CAMDESC;
628 SWO(sc, v_cda, (camoffset + CDA_CAMEP), i);
629 SWO(sc, v_cda, (camoffset + CDA_CAMAP2), 0);
630 SWO(sc, v_cda, (camoffset + CDA_CAMAP1), 0);
631 SWO(sc, v_cda, (camoffset + CDA_CAMAP0), 0);
633 SWO(sc, v_cda, CDA_ENABLE, 0);
636 if ((sncdebug & SNC_SHOWCAMENT) != 0) {
643 camentry(sc, entry, ea)
646 struct snc_softc *sc;
648 u_int32_t v_cda = sc->v_cda;
649 int camoffset = entry * CDA_CAMDESC;
651 SWO(sc, v_cda, camoffset + CDA_CAMEP, entry);
652 SWO(sc, v_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
653 SWO(sc, v_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
654 SWO(sc, v_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
655 SWO(sc, v_cda, CDA_ENABLE,
656 (SRO(sc, v_cda, CDA_ENABLE) | (1 << entry)));
661 struct snc_softc *sc;
663 struct ifmultiaddr *ifma;
672 /* Always load our own address first. */
673 camentry (sc, mcount, IF_LLADDR(sc->sc_ifp));
676 /* Assume we won't need allmulti bit. */
677 ifp->if_flags &= ~IFF_ALLMULTI;
679 /* Loop through multicast addresses */
681 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
682 if (ifma->ifma_addr->sa_family != AF_LINK)
684 if (mcount == MAXCAM) {
685 ifp->if_flags |= IFF_ALLMULTI;
689 /* program the CAM with the specified entry */
691 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
696 NIC_PUT(sc, SNCR_CDP, LOWER(sc->v_cda));
697 NIC_PUT(sc, SNCR_CDC, MAXCAM);
698 NIC_PUT(sc, SNCR_CR, CR_LCAM);
702 while ((NIC_GET(sc, SNCR_CR) & CR_LCAM) && timeout--)
706 panic("%s: CAM initialisation failed\n",
707 device_get_nameunit(sc->sc_dev));
710 while (((NIC_GET(sc, SNCR_ISR) & ISR_LCD) == 0) && timeout--)
713 if (NIC_GET(sc, SNCR_ISR) & ISR_LCD)
714 NIC_PUT(sc, SNCR_ISR, ISR_LCD);
716 device_printf(sc->sc_dev,
717 "CAM initialisation without interrupt\n");
723 struct snc_softc *sc;
727 printf("CAM entries:\n");
728 NIC_PUT(sc, SNCR_CR, CR_RST);
731 for (i = 0; i < 16; i++) {
732 u_short ap2, ap1, ap0;
733 NIC_PUT(sc, SNCR_CEP, i);
735 ap2 = NIC_GET(sc, SNCR_CAP2);
736 ap1 = NIC_GET(sc, SNCR_CAP1);
737 ap0 = NIC_GET(sc, SNCR_CAP0);
738 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
740 printf("CAM enable 0x%x\n", NIC_GET(sc, SNCR_CEP));
742 NIC_PUT(sc, SNCR_CR, 0);
749 struct snc_softc *sc;
754 for (i = 0; i < NTDA; i++) {
760 sc->mtd_prev = NTDA - 1;
762 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
763 sc->mtd_pint = NTDA/2;
765 NIC_PUT(sc, SNCR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
766 NIC_PUT(sc, SNCR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
771 struct snc_softc *sc;
774 u_int32_t vv_rda = 0;
777 /* link the RDA's together into a circular list */
778 for (i = 0; i < (sc->sc_nrda - 1); i++) {
779 v_rda = sc->v_rda + (i * RXPKT_SIZE(sc));
780 vv_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
781 SWO(sc, v_rda, RXPKT_RLINK, LOWER(vv_rda));
782 SWO(sc, v_rda, RXPKT_INUSE, 1);
784 v_rda = sc->v_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
785 SWO(sc, v_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
786 SWO(sc, v_rda, RXPKT_INUSE, 1);
788 /* mark end of receive descriptor list */
789 sc->sc_rdamark = sc->sc_nrda - 1;
793 NIC_PUT(sc, SNCR_URDA, UPPER(sc->v_rda));
794 NIC_PUT(sc, SNCR_CRDA, LOWER(sc->v_rda));
800 struct snc_softc *sc;
804 int bitmode = sc->bitmode;
807 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 2);
809 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 1);
811 NIC_PUT(sc, SNCR_URRA, UPPER(sc->v_rra[0]));
812 NIC_PUT(sc, SNCR_RSA, LOWER(sc->v_rra[0]));
813 /* rea must point just past the end of the rra space */
814 NIC_PUT(sc, SNCR_REA, LOWER(sc->v_rea));
815 NIC_PUT(sc, SNCR_RRP, LOWER(sc->v_rra[0]));
816 NIC_PUT(sc, SNCR_RSC, 0);
818 /* fill up SOME of the rra with buffers */
819 for (i = 0; i < NRBA; i++) {
820 v = SONIC_GETDMA(sc->rbuf[i]);
821 SWO(sc, sc->v_rra[i], RXRSRC_PTRHI, UPPER(v));
822 SWO(sc, sc->v_rra[i], RXRSRC_PTRLO, LOWER(v));
823 SWO(sc, sc->v_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
824 SWO(sc, sc->v_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
826 sc->sc_rramark = NRBA;
827 NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
835 struct snc_softc *sc = (struct snc_softc *)arg;
838 if (sc->sc_enabled == 0)
841 while ((isr = (NIC_GET(sc, SNCR_ISR) & ISR_ALL)) != 0) {
842 /* scrub the interrupts that we are going to service */
843 NIC_PUT(sc, SNCR_ISR, isr);
846 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
847 device_printf(sc->sc_dev,
848 "unexpected interrupt status 0x%x\n",
851 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
857 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
860 * The repeater is not providing a heartbeat.
861 * In itself this isn't harmful, lots of the
862 * cheap repeater hubs don't supply a heartbeat.
863 * So ignore the lack of heartbeat. Its only
864 * if we can't detect a carrier that we have a
869 device_printf(sc->sc_dev,
870 "receive descriptors exhausted\n");
872 device_printf(sc->sc_dev,
873 "receive buffers exhausted\n");
875 device_printf(sc->sc_dev,
876 "receive buffer area exhausted\n");
878 device_printf(sc->sc_dev,
879 "receive FIFO overrun\n");
881 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
891 sncstart(sc->sc_ifp);
893 #if defined(NRND) && NRND > 0
895 rnd_add_uint32(&sc->rnd_source, isr);
902 * Transmit interrupt routine
906 struct snc_softc *sc;
910 unsigned short txp_status;
912 struct ifnet *ifp = sc->sc_ifp;
916 if (mtd_hw == sc->mtd_free)
919 while (mtd_hw != sc->mtd_free) {
920 mtd = &sc->mtda[mtd_hw];
924 if (SRO(sc, txp, TXP_STATUS) == 0) {
925 break; /* it hasn't really gone yet */
929 if ((sncdebug & SNC_SHOWTXHDR) != 0)
931 struct ether_header eh;
933 (*sc->sc_copyfrombuf)(sc, &eh, mtd->mtd_vbuf, sizeof(eh));
934 device_printf(sc->sc_dev,
935 "xmit status=0x%x len=%d type=0x%x from %6D",
936 SRO(sc, txp, TXP_STATUS),
937 SRO(sc, txp, TXP_PKTSIZE),
938 htons(eh.ether_type),
939 eh.ether_shost, ":");
940 printf(" (to %6D)\n", eh.ether_dhost, ":");
942 #endif /* SNCDEBUG */
944 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
946 if (mtd->mtd_mbuf != 0) {
947 m_freem(mtd->mtd_mbuf);
950 if (++mtd_hw == NTDA) mtd_hw = 0;
952 txp_status = SRO(sc, txp, TXP_STATUS);
954 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
955 ((txp_status & TCR_NC) >> 12);
957 if ((txp_status & TCR_PTX) == 0) {
959 device_printf(sc->sc_dev, "Tx packet status=0x%x\n",
962 /* XXX - DG This looks bogus */
963 if (mtd_hw != sc->mtd_free) {
964 printf("resubmitting remaining packets\n");
965 mtd = &sc->mtda[mtd_hw];
966 NIC_PUT(sc, SNCR_CTDA, LOWER(mtd->mtd_vtxp));
967 NIC_PUT(sc, SNCR_CR, CR_TXP);
979 * Receive interrupt routine
983 struct snc_softc *sc;
992 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
994 while (SRO(sc, rda, RXPKT_INUSE) == 0) {
995 u_int status = SRO(sc, rda, RXPKT_STATUS);
997 orra = RBASEQ(SRO(sc, rda, RXPKT_SEQNO)) & RRAMASK;
998 rxpkt_ptr = SRO(sc, rda, RXPKT_PTRLO);
1000 * Do not trunc ether_header length.
1001 * Our sonic_read() and sonic_get() require it.
1003 len = SRO(sc, rda, RXPKT_BYTEC) - FCSSIZE;
1004 if (status & RCR_PRX) {
1005 /* XXX: Does PGOFSET require? */
1007 sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
1008 if (sonic_read(sc, pkt, len))
1009 sc->sc_ifp->if_ipackets++;
1011 sc->sc_ifp->if_ierrors++;
1013 sc->sc_ifp->if_ierrors++;
1016 * give receive buffer area back to chip.
1018 * If this was the last packet in the RRA, give the RRA to
1020 * If sonic read didnt copy it out then we would have to
1022 * (dont bother add it back in again straight away)
1024 * Really, we're doing v_rra[rramark] = v_rra[orra] but
1025 * we have to use the macros because SONIC might be in
1026 * 16 or 32 bit mode.
1028 if (status & RCR_LPKT) {
1029 u_int32_t tmp1, tmp2;
1031 rramark = sc->sc_rramark;
1032 tmp1 = sc->v_rra[rramark];
1033 tmp2 = sc->v_rra[orra];
1034 SWO(sc, tmp1, RXRSRC_PTRLO,
1035 SRO(sc, tmp2, RXRSRC_PTRLO));
1036 SWO(sc, tmp1, RXRSRC_PTRHI,
1037 SRO(sc, tmp2, RXRSRC_PTRHI));
1038 SWO(sc, tmp1, RXRSRC_WCLO,
1039 SRO(sc, tmp2, RXRSRC_WCLO));
1040 SWO(sc, tmp1, RXRSRC_WCHI,
1041 SRO(sc, tmp2, RXRSRC_WCHI));
1043 /* zap old rra for fun */
1044 SWO(sc, tmp2, RXRSRC_WCHI, 0);
1045 SWO(sc, tmp2, RXRSRC_WCLO, 0);
1047 sc->sc_rramark = (++rramark) & RRAMASK;
1048 NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[rramark]));
1053 * give receive descriptor back to chip simple
1056 rdamark = sc->sc_rdamark;
1057 SWO(sc, rda, RXPKT_INUSE, 1);
1058 SWO(sc, rda, RXPKT_RLINK,
1059 SRO(sc, rda, RXPKT_RLINK) | EOL);
1060 SWO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1061 SRO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))),
1062 RXPKT_RLINK) & ~EOL);
1063 sc->sc_rdamark = sc->sc_rxmark;
1065 if (++sc->sc_rxmark >= sc->sc_nrda)
1067 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1072 * sonic_read -- pull packet off interface and forward to
1073 * appropriate protocol handler
1076 sonic_read(sc, pkt, len)
1077 struct snc_softc *sc;
1081 struct ifnet *ifp = sc->sc_ifp;
1082 struct ether_header *et;
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);
1092 /* Pull packet off interface. */
1093 m = sonic_get(sc, pkt, len);
1098 /* We assume that the header fit entirely in one mbuf. */
1099 et = mtod(m, struct ether_header *);
1102 if ((sncdebug & SNC_SHOWRXHDR) != 0)
1104 device_printf(sc->sc_dev, "rcvd 0x%x len=%d type=0x%x from %6D",
1105 pkt, len, htons(et->ether_type),
1106 et->ether_shost, ":");
1107 printf(" (to %6D)\n", et->ether_dhost, ":");
1109 #endif /* SNCDEBUG */
1111 /* Pass the packet up. */
1112 (*ifp->if_input)(ifp, m);
1118 * munge the received packet into an mbuf chain
1121 sonic_get(sc, pkt, datalen)
1122 struct snc_softc *sc;
1126 struct mbuf *m, *top, **mp;
1129 * Do not trunc ether_header length.
1130 * Our sonic_read() and sonic_get() require it.
1133 MGETHDR(m, M_DONTWAIT, MT_DATA);
1136 m->m_pkthdr.rcvif = sc->sc_ifp;
1137 m->m_pkthdr.len = datalen;
1142 while (datalen > 0) {
1144 MGET(m, M_DONTWAIT, MT_DATA);
1151 if (datalen >= MINCLSIZE) {
1152 MCLGET(m, M_DONTWAIT);
1153 if ((m->m_flags & M_EXT) == 0) {
1154 if (top) m_freem(top);
1163 ALIGN(sizeof(struct ether_header)) -
1164 sizeof(struct ether_header);
1169 m->m_len = len = min(datalen, len);
1171 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), pkt, len);
1181 * Enable power on the interface.
1185 struct snc_softc *sc;
1189 device_printf(sc->sc_dev, "snc_enable()\n");
1190 #endif /* SNCDEBUG */
1192 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1193 if ((*sc->sc_enable)(sc) != 0) {
1194 device_printf(sc->sc_dev, "device enable failed\n");
1204 * Disable power on the interface.
1208 struct snc_softc *sc;
1212 device_printf(sc->sc_dev, "snc_disable()\n");
1213 #endif /* SNCDEBUG */
1215 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1216 (*sc->sc_disable)(sc);