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/kernel.h>
67 #include <sys/systm.h>
68 #include <sys/sockio.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/syslog.h>
73 #include <sys/errno.h>
75 #include <net/ethernet.h>
77 #include <net/if_arp.h>
78 #include <net/if_dl.h>
79 #include <net/if_media.h>
80 #include <net/if_types.h>
85 #include <machine/bus.h>
86 #include <dev/snc/dp83932reg.h>
87 #include <dev/snc/dp83932var.h>
89 static void sncwatchdog(void *);
90 static void sncinit(void *);
91 static void sncinit_locked(struct snc_softc *);
92 static int sncstop(struct snc_softc *sc);
93 static int sncioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
94 static void sncstart(struct ifnet *ifp);
95 static void sncstart_locked(struct ifnet *ifp);
96 static void sncreset(struct snc_softc *sc);
98 static void caminitialise(struct snc_softc *);
99 static void camentry(struct snc_softc *, int, u_char *ea);
100 static void camprogram(struct snc_softc *);
101 static void initialise_tda(struct snc_softc *);
102 static void initialise_rda(struct snc_softc *);
103 static void initialise_rra(struct snc_softc *);
105 static void camdump(struct snc_softc *sc);
108 static void sonictxint(struct snc_softc *);
109 static void sonicrxint(struct snc_softc *);
111 static u_int sonicput(struct snc_softc *sc, struct mbuf *m0, int mtd_next);
112 static int sonic_read(struct snc_softc *, u_int32_t, int);
113 static 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 *);
125 #define assert(e) ((void)0)
126 #define _assert(e) ((void)0)
128 #define _assert(e) assert(e)
130 #define assert(e) ((e) ? (void)0 : __assert("snc ", __FILE__, __LINE__, #e))
132 #define assert(e) ((e) ? (void)0 : __assert("snc "__FILE__, __LINE__, "e"))
137 #define SNC_SHOWTXHDR 0x01 /* show tx ether_header */
138 #define SNC_SHOWRXHDR 0x02 /* show rx ether_header */
139 #define SNC_SHOWCAMENT 0x04 /* show CAM entry */
140 #endif /* SNCDEBUG */
145 sncconfig(struct snc_softc *sc, int *media, int nmedia, int defmedia,
152 if ((sncdebug & SNC_SHOWCAMENT) != 0) {
157 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
159 device_printf(sc->sc_dev, "can not if_alloc()\n");
164 device_printf(sc->sc_dev,
165 "buffers: rra=0x%x cda=0x%x rda=0x%x tda=0x%x\n",
166 sc->v_rra[0], sc->v_cda,
167 sc->v_rda, sc->mtda[0].mtd_vtxp);
171 if_initname(ifp, device_get_name(sc->sc_dev),
172 device_get_unit(sc->sc_dev));
173 ifp->if_ioctl = sncioctl;
174 ifp->if_start = sncstart;
175 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
176 ifp->if_init = sncinit;
177 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
179 /* Initialize media goo. */
180 ifmedia_init(&sc->sc_media, 0, snc_mediachange,
183 for (i = 0; i < nmedia; i++)
184 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
185 ifmedia_set(&sc->sc_media, defmedia);
187 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
188 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
191 ether_ifattach(ifp, myea);
196 sncshutdown(void *arg)
198 struct snc_softc *sc = arg;
200 SNC_ASSERT_LOCKED(sc);
205 * Media change callback.
208 snc_mediachange(struct ifnet *ifp)
210 struct snc_softc *sc = ifp->if_softc;
214 if (sc->sc_mediachange)
215 error = (*sc->sc_mediachange)(sc);
223 * Media status callback.
226 snc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
228 struct snc_softc *sc = ifp->if_softc;
231 if (sc->sc_enabled == 0) {
232 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
233 ifmr->ifm_status = 0;
238 if (sc->sc_mediastatus)
239 (*sc->sc_mediastatus)(sc, ifmr);
245 sncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
248 struct snc_softc *sc = ifp->if_softc;
255 if ((ifp->if_flags & IFF_UP) == 0 &&
256 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
258 * If interface is marked down and it is running,
263 } else if ((ifp->if_flags & IFF_UP) != 0 &&
264 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
266 * If interface is marked up and it is stopped,
269 if ((err = snc_enable(sc)) != 0)
272 } else if (sc->sc_enabled) {
274 * reset the interface to pick up any other changes
278 sncstart_locked(ifp);
286 if (sc->sc_enabled == 0) {
297 ifr = (struct ifreq *) data;
298 err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
301 err = ether_ioctl(ifp, cmd, data);
308 * Encapsulate a packet of type family for the local net.
311 sncstart(struct ifnet *ifp)
313 struct snc_softc *sc = ifp->if_softc;
316 sncstart_locked(ifp);
321 sncstart_locked(struct ifnet *ifp)
323 struct snc_softc *sc = ifp->if_softc;
327 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
332 /* Check for room in the xmit buffer. */
333 if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
336 if (mtd_next == sc->mtd_hw) {
337 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
341 IF_DEQUEUE(&ifp->if_snd, m);
345 /* We need the header for m_pkthdr.len. */
349 * If there is nothing in the o/p queue, and there is room in
350 * the Tx ring, then send the packet directly. Otherwise append
351 * it to the o/p queue.
353 if ((sonicput(sc, m, mtd_next)) == 0) {
354 IF_PREPEND(&ifp->if_snd, m);
359 * If bpf is listening on this interface, let it see the packet
360 * before we commit it to the wire, but only if we are really
361 * committed to send it.
363 * XXX: Locking must protect m against premature m_freem() in
368 sc->mtd_prev = sc->mtd_free;
369 sc->mtd_free = mtd_next;
371 ifp->if_opackets++; /* # of pkts */
373 /* Jump back for possibly more punishment. */
378 * reset and restart the SONIC. Called in case of fatal
379 * hardware/software errors.
382 sncreset(struct snc_softc *sc)
391 struct snc_softc *sc = xsc;
399 sncinit_locked(struct snc_softc *sc)
403 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
404 /* already running */
407 NIC_PUT(sc, SNCR_CR, CR_RST); /* DCR only accessable in reset mode! */
410 NIC_PUT(sc, SNCR_DCR, (sc->sncr_dcr |
411 (sc->bitmode ? DCR_DW32 : DCR_DW16)));
412 NIC_PUT(sc, SNCR_DCR2, sc->sncr_dcr2);
414 s_rcr = RCR_BRD | RCR_LBNONE;
415 if (sc->sc_ifp->if_flags & IFF_PROMISC)
417 if (sc->sc_ifp->if_flags & IFF_ALLMULTI)
419 NIC_PUT(sc, SNCR_RCR, s_rcr);
421 NIC_PUT(sc, SNCR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
423 /* clear pending interrupts */
424 NIC_PUT(sc, SNCR_ISR, ISR_ALL);
426 /* clear tally counters */
427 NIC_PUT(sc, SNCR_CRCT, -1);
428 NIC_PUT(sc, SNCR_FAET, -1);
429 NIC_PUT(sc, SNCR_MPT, -1);
435 /* enable the chip */
436 NIC_PUT(sc, SNCR_CR, 0);
439 /* program the CAM */
442 /* get it to read resource descriptors */
443 NIC_PUT(sc, SNCR_CR, CR_RRRA);
445 while ((NIC_GET(sc, SNCR_CR)) & CR_RRRA)
449 NIC_PUT(sc, SNCR_CR, CR_RXEN);
452 /* flag interface as "running" */
453 sc->sc_ifp->if_drv_flags |= IFF_DRV_RUNNING;
454 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
455 callout_reset(&sc->sc_timer, hz, sncwatchdog, sc);
461 * close down an interface and free its buffers
462 * Called on final close of device, or if sncinit() fails
466 sncstop(struct snc_softc *sc)
470 SNC_ASSERT_LOCKED(sc);
472 /* stick chip in reset */
473 NIC_PUT(sc, SNCR_CR, CR_RST);
476 /* free all receive buffers (currently static so nothing to do) */
478 /* free all pending transmit mbufs */
479 while (sc->mtd_hw != sc->mtd_free) {
480 mtd = &sc->mtda[sc->mtd_hw];
482 m_freem(mtd->mtd_mbuf);
483 if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
486 callout_stop(&sc->sc_timer);
487 sc->sc_tx_timeout = 0;
488 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
494 * Called if any Tx packets remain unsent after 5 seconds,
495 * In all cases we just reset the chip, and any retransmission
496 * will be handled by higher level protocol timeouts.
499 sncwatchdog(void *arg)
501 struct snc_softc *sc = arg;
504 SNC_ASSERT_LOCKED(sc);
505 if (sc->sc_tx_timeout && --sc->sc_tx_timeout == 0) {
506 if (sc->mtd_hw != sc->mtd_free) {
507 /* something still pending for transmit */
508 mtd = &sc->mtda[sc->mtd_hw];
509 if (SRO(sc, mtd->mtd_vtxp, TXP_STATUS) == 0)
510 log(LOG_ERR, "%s: Tx - timeout\n",
511 device_get_nameunit(sc->sc_dev));
513 log(LOG_ERR, "%s: Tx - lost interrupt\n",
514 device_get_nameunit(sc->sc_dev));
518 callout_reset(&sc->sc_timer, hz, sncwatchdog, sc);
522 * stuff packet into sonic
525 sonicput(struct snc_softc *sc, struct mbuf *m0, int mtd_next)
534 #ifdef whyonearthwouldyoudothis
535 if (NIC_GET(sc, SNCR_CR) & CR_TXP)
539 /* grab the replacement mtd */
540 mtdp = &sc->mtda[sc->mtd_free];
542 buff = mtdp->mtd_vbuf;
544 /* this packet goes to mtdnext fill in the TDA */
546 txp = mtdp->mtd_vtxp;
548 /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
549 if (sc->mtd_pint == 0) {
550 sc->mtd_pint = NTDA/2;
551 SWO(sc, txp, TXP_CONFIG, TCR_PINT);
554 SWO(sc, txp, TXP_CONFIG, 0);
557 for (m = m0; m; m = m->m_next) {
560 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), buff, len);
563 if (totlen >= TXBSIZE) {
564 panic("%s: sonicput: packet overflow",
565 device_get_nameunit(sc->sc_dev));
568 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
569 LOWER(mtdp->mtd_vbuf));
570 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
571 UPPER(mtdp->mtd_vbuf));
573 if (totlen < ETHERMIN + sizeof(struct ether_header)) {
574 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
575 (*sc->sc_zerobuf)(sc, mtdp->mtd_vbuf + totlen, pad);
576 totlen = ETHERMIN + sizeof(struct ether_header);
579 SWO(sc, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
581 SWO(sc, txp, TXP_FRAGCNT, 1);
582 SWO(sc, txp, TXP_PKTSIZE, totlen);
584 /* link onto the next mtd that will be used */
585 SWO(sc, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
586 LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
589 * The previous txp.tlink currently contains a pointer to
590 * our txp | EOL. Want to clear the EOL, so write our
591 * pointer to the previous txp.
593 SWO(sc, sc->mtda[sc->mtd_prev].mtd_vtxp, sc->mtd_tlinko,
594 LOWER(mtdp->mtd_vtxp));
596 /* make sure chip is running */
598 NIC_PUT(sc, SNCR_CR, CR_TXP);
601 /* 5 seconds to watch for failing to transmit */
602 sc->sc_tx_timeout = 5;
608 * These are called from sonicioctl() when /etc/ifconfig is run to set
609 * the address or switch the i/f on.
615 caminitialise(struct snc_softc *sc)
617 u_int32_t v_cda = sc->v_cda;
621 for (i = 0; i < MAXCAM; i++) {
622 camoffset = i * CDA_CAMDESC;
623 SWO(sc, v_cda, (camoffset + CDA_CAMEP), i);
624 SWO(sc, v_cda, (camoffset + CDA_CAMAP2), 0);
625 SWO(sc, v_cda, (camoffset + CDA_CAMAP1), 0);
626 SWO(sc, v_cda, (camoffset + CDA_CAMAP0), 0);
628 SWO(sc, v_cda, CDA_ENABLE, 0);
631 if ((sncdebug & SNC_SHOWCAMENT) != 0) {
638 camentry(struct snc_softc *sc, int entry, u_char *ea)
640 u_int32_t v_cda = sc->v_cda;
641 int camoffset = entry * CDA_CAMDESC;
643 SWO(sc, v_cda, camoffset + CDA_CAMEP, entry);
644 SWO(sc, v_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
645 SWO(sc, v_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
646 SWO(sc, v_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
647 SWO(sc, v_cda, CDA_ENABLE,
648 (SRO(sc, v_cda, CDA_ENABLE) | (1 << entry)));
652 camprogram(struct snc_softc *sc)
654 struct ifmultiaddr *ifma;
663 /* Always load our own address first. */
664 camentry (sc, mcount, IF_LLADDR(sc->sc_ifp));
667 /* Assume we won't need allmulti bit. */
668 ifp->if_flags &= ~IFF_ALLMULTI;
670 /* Loop through multicast addresses */
672 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
673 if (ifma->ifma_addr->sa_family != AF_LINK)
675 if (mcount == MAXCAM) {
676 ifp->if_flags |= IFF_ALLMULTI;
680 /* program the CAM with the specified entry */
682 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
685 if_maddr_runlock(ifp);
687 NIC_PUT(sc, SNCR_CDP, LOWER(sc->v_cda));
688 NIC_PUT(sc, SNCR_CDC, MAXCAM);
689 NIC_PUT(sc, SNCR_CR, CR_LCAM);
693 while ((NIC_GET(sc, SNCR_CR) & CR_LCAM) && timeout--)
697 panic("%s: CAM initialisation failed\n",
698 device_get_nameunit(sc->sc_dev));
701 while (((NIC_GET(sc, SNCR_ISR) & ISR_LCD) == 0) && timeout--)
704 if (NIC_GET(sc, SNCR_ISR) & ISR_LCD)
705 NIC_PUT(sc, SNCR_ISR, ISR_LCD);
707 device_printf(sc->sc_dev,
708 "CAM initialisation without interrupt\n");
713 camdump(struct snc_softc *sc)
717 printf("CAM entries:\n");
718 NIC_PUT(sc, SNCR_CR, CR_RST);
721 for (i = 0; i < 16; i++) {
722 u_short ap2, ap1, ap0;
723 NIC_PUT(sc, SNCR_CEP, i);
725 ap2 = NIC_GET(sc, SNCR_CAP2);
726 ap1 = NIC_GET(sc, SNCR_CAP1);
727 ap0 = NIC_GET(sc, SNCR_CAP0);
728 printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
730 printf("CAM enable 0x%x\n", NIC_GET(sc, SNCR_CEP));
732 NIC_PUT(sc, SNCR_CR, 0);
738 initialise_tda(struct snc_softc *sc)
743 for (i = 0; i < NTDA; i++) {
749 sc->mtd_prev = NTDA - 1;
751 sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
752 sc->mtd_pint = NTDA/2;
754 NIC_PUT(sc, SNCR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
755 NIC_PUT(sc, SNCR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
759 initialise_rda(struct snc_softc *sc)
762 u_int32_t vv_rda = 0;
765 /* link the RDA's together into a circular list */
766 for (i = 0; i < (sc->sc_nrda - 1); i++) {
767 v_rda = sc->v_rda + (i * RXPKT_SIZE(sc));
768 vv_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
769 SWO(sc, v_rda, RXPKT_RLINK, LOWER(vv_rda));
770 SWO(sc, v_rda, RXPKT_INUSE, 1);
772 v_rda = sc->v_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
773 SWO(sc, v_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
774 SWO(sc, v_rda, RXPKT_INUSE, 1);
776 /* mark end of receive descriptor list */
777 sc->sc_rdamark = sc->sc_nrda - 1;
781 NIC_PUT(sc, SNCR_URDA, UPPER(sc->v_rda));
782 NIC_PUT(sc, SNCR_CRDA, LOWER(sc->v_rda));
787 initialise_rra(struct snc_softc *sc)
791 int bitmode = sc->bitmode;
794 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 2);
796 NIC_PUT(sc, SNCR_EOBC, RBASIZE(sc) / 2 - 1);
798 NIC_PUT(sc, SNCR_URRA, UPPER(sc->v_rra[0]));
799 NIC_PUT(sc, SNCR_RSA, LOWER(sc->v_rra[0]));
800 /* rea must point just past the end of the rra space */
801 NIC_PUT(sc, SNCR_REA, LOWER(sc->v_rea));
802 NIC_PUT(sc, SNCR_RRP, LOWER(sc->v_rra[0]));
803 NIC_PUT(sc, SNCR_RSC, 0);
805 /* fill up SOME of the rra with buffers */
806 for (i = 0; i < NRBA; i++) {
807 v = SONIC_GETDMA(sc->rbuf[i]);
808 SWO(sc, sc->v_rra[i], RXRSRC_PTRHI, UPPER(v));
809 SWO(sc, sc->v_rra[i], RXRSRC_PTRLO, LOWER(v));
810 SWO(sc, sc->v_rra[i], RXRSRC_WCHI, UPPER(PAGE_SIZE/2));
811 SWO(sc, sc->v_rra[i], RXRSRC_WCLO, LOWER(PAGE_SIZE/2));
813 sc->sc_rramark = NRBA;
814 NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
821 struct snc_softc *sc = (struct snc_softc *)arg;
824 if (sc->sc_enabled == 0)
828 while ((isr = (NIC_GET(sc, SNCR_ISR) & ISR_ALL)) != 0) {
829 /* scrub the interrupts that we are going to service */
830 NIC_PUT(sc, SNCR_ISR, isr);
833 if (isr & (ISR_BR | ISR_LCD | ISR_TC))
834 device_printf(sc->sc_dev,
835 "unexpected interrupt status 0x%x\n",
838 if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
844 if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
847 * The repeater is not providing a heartbeat.
848 * In itself this isn't harmful, lots of the
849 * cheap repeater hubs don't supply a heartbeat.
850 * So ignore the lack of heartbeat. Its only
851 * if we can't detect a carrier that we have a
856 device_printf(sc->sc_dev,
857 "receive descriptors exhausted\n");
859 device_printf(sc->sc_dev,
860 "receive buffers exhausted\n");
862 device_printf(sc->sc_dev,
863 "receive buffer area exhausted\n");
865 device_printf(sc->sc_dev,
866 "receive FIFO overrun\n");
868 if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
878 sncstart_locked(sc->sc_ifp);
885 * Transmit interrupt routine
888 sonictxint(struct snc_softc *sc)
892 unsigned short txp_status;
894 struct ifnet *ifp = sc->sc_ifp;
898 if (mtd_hw == sc->mtd_free)
901 while (mtd_hw != sc->mtd_free) {
902 mtd = &sc->mtda[mtd_hw];
906 if (SRO(sc, txp, TXP_STATUS) == 0) {
907 break; /* it hasn't really gone yet */
911 if ((sncdebug & SNC_SHOWTXHDR) != 0)
913 struct ether_header eh;
915 (*sc->sc_copyfrombuf)(sc, &eh, mtd->mtd_vbuf, sizeof(eh));
916 device_printf(sc->sc_dev,
917 "xmit status=0x%x len=%d type=0x%x from %6D",
918 SRO(sc, txp, TXP_STATUS),
919 SRO(sc, txp, TXP_PKTSIZE),
920 htons(eh.ether_type),
921 eh.ether_shost, ":");
922 printf(" (to %6D)\n", eh.ether_dhost, ":");
924 #endif /* SNCDEBUG */
926 sc->sc_tx_timeout = 0;
927 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
929 if (mtd->mtd_mbuf != 0) {
930 m_freem(mtd->mtd_mbuf);
933 if (++mtd_hw == NTDA) mtd_hw = 0;
935 txp_status = SRO(sc, txp, TXP_STATUS);
937 ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
938 ((txp_status & TCR_NC) >> 12);
940 if ((txp_status & TCR_PTX) == 0) {
942 device_printf(sc->sc_dev, "Tx packet status=0x%x\n",
945 /* XXX - DG This looks bogus */
946 if (mtd_hw != sc->mtd_free) {
947 printf("resubmitting remaining packets\n");
948 mtd = &sc->mtda[mtd_hw];
949 NIC_PUT(sc, SNCR_CTDA, LOWER(mtd->mtd_vtxp));
950 NIC_PUT(sc, SNCR_CR, CR_TXP);
962 * Receive interrupt routine
965 sonicrxint(struct snc_softc *sc)
974 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
976 while (SRO(sc, rda, RXPKT_INUSE) == 0) {
977 u_int status = SRO(sc, rda, RXPKT_STATUS);
979 orra = RBASEQ(SRO(sc, rda, RXPKT_SEQNO)) & RRAMASK;
980 rxpkt_ptr = SRO(sc, rda, RXPKT_PTRLO);
982 * Do not trunc ether_header length.
983 * Our sonic_read() and sonic_get() require it.
985 len = SRO(sc, rda, RXPKT_BYTEC) - FCSSIZE;
986 if (status & RCR_PRX) {
987 /* XXX: Does PAGE_MASK require? */
989 sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PAGE_MASK);
990 if (sonic_read(sc, pkt, len))
991 sc->sc_ifp->if_ipackets++;
993 sc->sc_ifp->if_ierrors++;
995 sc->sc_ifp->if_ierrors++;
998 * give receive buffer area back to chip.
1000 * If this was the last packet in the RRA, give the RRA to
1002 * If sonic read didnt copy it out then we would have to
1004 * (dont bother add it back in again straight away)
1006 * Really, we're doing v_rra[rramark] = v_rra[orra] but
1007 * we have to use the macros because SONIC might be in
1008 * 16 or 32 bit mode.
1010 if (status & RCR_LPKT) {
1011 u_int32_t tmp1, tmp2;
1013 rramark = sc->sc_rramark;
1014 tmp1 = sc->v_rra[rramark];
1015 tmp2 = sc->v_rra[orra];
1016 SWO(sc, tmp1, RXRSRC_PTRLO,
1017 SRO(sc, tmp2, RXRSRC_PTRLO));
1018 SWO(sc, tmp1, RXRSRC_PTRHI,
1019 SRO(sc, tmp2, RXRSRC_PTRHI));
1020 SWO(sc, tmp1, RXRSRC_WCLO,
1021 SRO(sc, tmp2, RXRSRC_WCLO));
1022 SWO(sc, tmp1, RXRSRC_WCHI,
1023 SRO(sc, tmp2, RXRSRC_WCHI));
1025 /* zap old rra for fun */
1026 SWO(sc, tmp2, RXRSRC_WCHI, 0);
1027 SWO(sc, tmp2, RXRSRC_WCLO, 0);
1029 sc->sc_rramark = (++rramark) & RRAMASK;
1030 NIC_PUT(sc, SNCR_RWP, LOWER(sc->v_rra[rramark]));
1035 * give receive descriptor back to chip simple
1038 rdamark = sc->sc_rdamark;
1039 SWO(sc, rda, RXPKT_INUSE, 1);
1040 SWO(sc, rda, RXPKT_RLINK,
1041 SRO(sc, rda, RXPKT_RLINK) | EOL);
1042 SWO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1043 SRO(sc, (sc->v_rda + (rdamark * RXPKT_SIZE(sc))),
1044 RXPKT_RLINK) & ~EOL);
1045 sc->sc_rdamark = sc->sc_rxmark;
1047 if (++sc->sc_rxmark >= sc->sc_nrda)
1049 rda = sc->v_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1054 * sonic_read -- pull packet off interface and forward to
1055 * appropriate protocol handler
1058 sonic_read(struct snc_softc *sc, u_int32_t pkt, int len)
1060 struct ifnet *ifp = sc->sc_ifp;
1061 struct ether_header *et;
1064 if (len <= sizeof(struct ether_header) ||
1065 len > ETHERMTU + sizeof(struct ether_header)) {
1066 device_printf(sc->sc_dev,
1067 "invalid packet length %d bytes\n", len);
1071 /* Pull packet off interface. */
1072 m = sonic_get(sc, pkt, len);
1077 /* We assume that the header fit entirely in one mbuf. */
1078 et = mtod(m, struct ether_header *);
1081 if ((sncdebug & SNC_SHOWRXHDR) != 0)
1083 device_printf(sc->sc_dev, "rcvd 0x%x len=%d type=0x%x from %6D",
1084 pkt, len, htons(et->ether_type),
1085 et->ether_shost, ":");
1086 printf(" (to %6D)\n", et->ether_dhost, ":");
1088 #endif /* SNCDEBUG */
1090 /* Pass the packet up. */
1092 (*ifp->if_input)(ifp, m);
1099 * munge the received packet into an mbuf chain
1101 static struct mbuf *
1102 sonic_get(struct snc_softc *sc, u_int32_t pkt, int datalen)
1104 struct mbuf *m, *top, **mp;
1107 * Do not trunc ether_header length.
1108 * Our sonic_read() and sonic_get() require it.
1111 MGETHDR(m, M_NOWAIT, MT_DATA);
1114 m->m_pkthdr.rcvif = sc->sc_ifp;
1115 m->m_pkthdr.len = datalen;
1120 while (datalen > 0) {
1122 MGET(m, M_NOWAIT, MT_DATA);
1129 if (datalen >= MINCLSIZE) {
1130 MCLGET(m, M_NOWAIT);
1131 if ((m->m_flags & M_EXT) == 0) {
1132 if (top) m_freem(top);
1141 ALIGN(sizeof(struct ether_header)) -
1142 sizeof(struct ether_header);
1147 m->m_len = len = min(datalen, len);
1149 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), pkt, len);
1159 * Enable power on the interface.
1162 snc_enable(struct snc_softc *sc)
1166 device_printf(sc->sc_dev, "snc_enable()\n");
1167 #endif /* SNCDEBUG */
1169 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1170 if ((*sc->sc_enable)(sc) != 0) {
1171 device_printf(sc->sc_dev, "device enable failed\n");
1181 * Disable power on the interface.
1184 snc_disable(struct snc_softc *sc)
1188 device_printf(sc->sc_dev, "snc_disable()\n");
1189 #endif /* SNCDEBUG */
1191 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1192 (*sc->sc_disable)(sc);