Home | History | Annotate | Line # | Download | only in dev
if_aumac.c revision 1.15
      1 /* $NetBSD: if_aumac.c,v 1.15 2006/03/03 05:27:29 simonb Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     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 for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
     40  * Access Controller.
     41  *
     42  * TODO:
     43  *
     44  *	Better Rx buffer management; we want to get new Rx buffers
     45  *	to the chip more quickly than we currently do.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: if_aumac.c,v 1.15 2006/03/03 05:27:29 simonb Exp $");
     50 
     51 #include "bpfilter.h"
     52 #include "rnd.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/callout.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/malloc.h>
     59 #include <sys/kernel.h>
     60 #include <sys/socket.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/errno.h>
     63 #include <sys/device.h>
     64 #include <sys/queue.h>
     65 
     66 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
     67 
     68 #include <net/if.h>
     69 #include <net/if_dl.h>
     70 #include <net/if_media.h>
     71 #include <net/if_ether.h>
     72 
     73 #if NBPFILTER > 0
     74 #include <net/bpf.h>
     75 #endif
     76 #if NRND > 0
     77 #include <sys/rnd.h>
     78 #endif
     79 
     80 #include <machine/bus.h>
     81 #include <machine/intr.h>
     82 #include <machine/endian.h>
     83 
     84 #include <dev/mii/mii.h>
     85 #include <dev/mii/miivar.h>
     86 
     87 #include <mips/alchemy/include/aureg.h>
     88 #include <mips/alchemy/include/auvar.h>
     89 #include <mips/alchemy/include/aubusvar.h>
     90 #include <mips/alchemy/dev/if_aumacreg.h>
     91 
     92 /*
     93  * The Au1X00 MAC has 4 transmit and receive descriptors.  Each buffer
     94  * must consist of a single DMA segment, and must be aligned to a 2K
     95  * boundary.  Therefore, this driver does not perform DMA directly
     96  * to/from mbufs.  Instead, we copy the data to/from buffers allocated
     97  * at device attach time.
     98  *
     99  * We also skip the bus_dma dance.  The MAC is built in to the CPU, so
    100  * there's little point in not making assumptions based on the CPU type.
    101  * We also program the Au1X00 cache to be DMA coherent, so the buffers
    102  * are accessed via KSEG0 addresses.
    103  */
    104 #define	AUMAC_NTXDESC		4
    105 #define	AUMAC_NTXDESC_MASK	(AUMAC_NTXDESC - 1)
    106 
    107 #define	AUMAC_NRXDESC		4
    108 #define	AUMAC_NRXDESC_MASK	(AUMAC_NRXDESC - 1)
    109 
    110 #define	AUMAC_NEXTTX(x)		(((x) + 1) & AUMAC_NTXDESC_MASK)
    111 #define	AUMAC_NEXTRX(x)		(((x) + 1) & AUMAC_NRXDESC_MASK)
    112 
    113 #define	AUMAC_TXBUF_OFFSET	0
    114 #define	AUMAC_RXBUF_OFFSET	(MAC_BUFLEN * AUMAC_NTXDESC)
    115 #define	AUMAC_BUFSIZE		(MAC_BUFLEN * (AUMAC_NTXDESC + AUMAC_NRXDESC))
    116 
    117 struct aumac_buf {
    118 	caddr_t buf_vaddr;		/* virtual address of buffer */
    119 	bus_addr_t buf_paddr;		/* DMA address of buffer */
    120 };
    121 
    122 /*
    123  * Software state per device.
    124  */
    125 struct aumac_softc {
    126 	struct device sc_dev;		/* generic device information */
    127 	bus_space_tag_t sc_st;		/* bus space tag */
    128 	bus_space_handle_t sc_mac_sh;	/* MAC space handle */
    129 	bus_space_handle_t sc_macen_sh;	/* MAC enable space handle */
    130 	bus_space_handle_t sc_dma_sh;	/* DMA space handle */
    131 	struct ethercom sc_ethercom;	/* Ethernet common data */
    132 	void *sc_sdhook;		/* shutdown hook */
    133 
    134 	void *sc_ih;			/* interrupt cookie */
    135 
    136 	struct mii_data sc_mii;		/* MII/media information */
    137 
    138 	struct callout sc_tick_ch;	/* tick callout */
    139 
    140 	/* Transmit and receive buffers */
    141 	struct aumac_buf sc_txbufs[AUMAC_NTXDESC];
    142 	struct aumac_buf sc_rxbufs[AUMAC_NRXDESC];
    143 	caddr_t sc_bufaddr;
    144 
    145 	int sc_txfree;			/* number of free Tx descriptors */
    146 	int sc_txnext;			/* next Tx descriptor to use */
    147 	int sc_txdirty;			/* first dirty Tx descriptor */
    148 
    149 	int sc_rxptr;			/* next ready Rx descriptor */
    150 
    151 #if NRND > 0
    152 	rndsource_element_t rnd_source;
    153 #endif
    154 
    155 #ifdef AUMAC_EVENT_COUNTERS
    156 	struct evcnt sc_ev_txstall;	/* Tx stalled */
    157 	struct evcnt sc_ev_rxstall;	/* Rx stalled */
    158 	struct evcnt sc_ev_txintr;	/* Tx interrupts */
    159 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    160 #endif
    161 
    162 	uint32_t sc_control;		/* MAC_CONTROL contents */
    163 	uint32_t sc_flowctrl;		/* MAC_FLOWCTRL contents */
    164 };
    165 
    166 #ifdef AUMAC_EVENT_COUNTERS
    167 #define	AUMAC_EVCNT_INCR(ev)	(ev)->ev_count++
    168 #else
    169 #define	AUMAC_EVCNT_INCR(ev)	/* nothing */
    170 #endif
    171 
    172 #define	AUMAC_INIT_RXDESC(sc, x)					\
    173 do {									\
    174 	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
    175 	    MACDMA_RX_STAT((x)), 0);					\
    176 	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
    177 	    MACDMA_RX_ADDR((x)),					\
    178 	    (sc)->sc_rxbufs[(x)].buf_paddr | RX_ADDR_EN);		\
    179 } while (/*CONSTCOND*/0)
    180 
    181 static void	aumac_start(struct ifnet *);
    182 static void	aumac_watchdog(struct ifnet *);
    183 static int	aumac_ioctl(struct ifnet *, u_long, caddr_t);
    184 static int	aumac_init(struct ifnet *);
    185 static void	aumac_stop(struct ifnet *, int);
    186 
    187 static void	aumac_shutdown(void *);
    188 
    189 static void	aumac_tick(void *);
    190 
    191 static void	aumac_set_filter(struct aumac_softc *);
    192 
    193 static void	aumac_powerup(struct aumac_softc *);
    194 static void	aumac_powerdown(struct aumac_softc *);
    195 
    196 static int	aumac_intr(void *);
    197 static int	aumac_txintr(struct aumac_softc *);
    198 static int	aumac_rxintr(struct aumac_softc *);
    199 
    200 static int	aumac_mii_readreg(struct device *, int, int);
    201 static void	aumac_mii_writereg(struct device *, int, int, int);
    202 static void	aumac_mii_statchg(struct device *);
    203 static int	aumac_mii_wait(struct aumac_softc *, const char *);
    204 
    205 static int	aumac_mediachange(struct ifnet *);
    206 static void	aumac_mediastatus(struct ifnet *, struct ifmediareq *);
    207 
    208 static int	aumac_match(struct device *, struct cfdata *, void *);
    209 static void	aumac_attach(struct device *, struct device *, void *);
    210 
    211 int	aumac_copy_small = 0;
    212 
    213 CFATTACH_DECL(aumac, sizeof(struct aumac_softc),
    214     aumac_match, aumac_attach, NULL, NULL);
    215 
    216 static int
    217 aumac_match(struct device *parent, struct cfdata *cf, void *aux)
    218 {
    219 	struct aubus_attach_args *aa = aux;
    220 
    221 	if (strcmp(aa->aa_name, cf->cf_name) == 0)
    222 		return (1);
    223 
    224 	return (0);
    225 }
    226 
    227 static void
    228 aumac_attach(struct device *parent, struct device *self, void *aux)
    229 {
    230 	uint8_t enaddr[ETHER_ADDR_LEN];
    231 	struct aumac_softc *sc = (void *) self;
    232 	struct aubus_attach_args *aa = aux;
    233 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    234 	struct pglist pglist;
    235 	paddr_t bufaddr;
    236 	caddr_t vbufaddr;
    237 	int i;
    238 
    239 	callout_init(&sc->sc_tick_ch);
    240 
    241 	printf(": Au1X00 10/100 Ethernet\n");
    242 
    243 	sc->sc_st = aa->aa_st;
    244 
    245 	/* Get the MAC address. */
    246 	if (devprop_get(&sc->sc_dev, "mac-addr", enaddr,
    247 		     sizeof(enaddr), NULL) != sizeof(enaddr)) {
    248 		printf("%s: unable to get mac-addr property\n",
    249 		    sc->sc_dev.dv_xname);
    250 		return;
    251 	}
    252 
    253 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    254 	    ether_sprintf(enaddr));
    255 
    256 	/* Map the device. */
    257 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_BASE],
    258 	    MACx_SIZE, 0, &sc->sc_mac_sh) != 0) {
    259 		printf("%s: unable to map MAC registers\n",
    260 		    sc->sc_dev.dv_xname);
    261 		return;
    262 	}
    263 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_ENABLE],
    264 	    MACENx_SIZE, 0, &sc->sc_macen_sh) != 0) {
    265 		printf("%s: unable to map MACEN registers\n",
    266 		    sc->sc_dev.dv_xname);
    267 		return;
    268 	}
    269 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_DMA_BASE],
    270 	    MACx_DMA_SIZE, 0, &sc->sc_dma_sh) != 0) {
    271 		printf("%s: unable to map MACDMA registers\n",
    272 		    sc->sc_dev.dv_xname);
    273 		return;
    274 	}
    275 
    276 	/* Make sure the MAC is powered off. */
    277 	aumac_powerdown(sc);
    278 
    279 	/* Hook up the interrupt handler. */
    280 	sc->sc_ih = au_intr_establish(aa->aa_irq[0], 1, IPL_NET, IST_LEVEL,
    281 	    aumac_intr, sc);
    282 	if (sc->sc_ih == NULL) {
    283 		printf("%s: unable to register interrupt handler\n",
    284 		    sc->sc_dev.dv_xname);
    285 		return;
    286 	}
    287 
    288 	/*
    289 	 * Allocate space for the transmit and receive buffers.
    290 	 */
    291 	if (uvm_pglistalloc(AUMAC_BUFSIZE, 0, ctob(physmem), PAGE_SIZE, 0,
    292 	    &pglist, 1, 0))
    293 		return;
    294 
    295 	bufaddr = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
    296 	vbufaddr = (void *)MIPS_PHYS_TO_KSEG0(bufaddr);
    297 
    298 	for (i = 0; i < AUMAC_NTXDESC; i++) {
    299 		int offset = AUMAC_TXBUF_OFFSET + (i * MAC_BUFLEN);
    300 
    301 		sc->sc_txbufs[i].buf_vaddr = vbufaddr + offset;
    302 		sc->sc_txbufs[i].buf_paddr = bufaddr + offset;
    303 	}
    304 
    305 	for (i = 0; i < AUMAC_NRXDESC; i++) {
    306 		int offset = AUMAC_RXBUF_OFFSET + (i * MAC_BUFLEN);
    307 
    308 		sc->sc_rxbufs[i].buf_vaddr = vbufaddr + offset;
    309 		sc->sc_rxbufs[i].buf_paddr = bufaddr + offset;
    310 	}
    311 
    312 	/*
    313 	 * Power up the MAC before accessing any MAC registers (including
    314 	 * MII configuration.
    315 	 */
    316 	aumac_powerup(sc);
    317 
    318 	/*
    319 	 * Initialize the media structures and probe the MII.
    320 	 */
    321 	sc->sc_mii.mii_ifp = ifp;
    322 	sc->sc_mii.mii_readreg = aumac_mii_readreg;
    323 	sc->sc_mii.mii_writereg = aumac_mii_writereg;
    324 	sc->sc_mii.mii_statchg = aumac_mii_statchg;
    325 	ifmedia_init(&sc->sc_mii.mii_media, 0, aumac_mediachange,
    326 	    aumac_mediastatus);
    327 
    328 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    329 	    MII_OFFSET_ANY, 0);
    330 
    331 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    332 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    333 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    334 	} else
    335 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    336 
    337 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    338 	ifp->if_softc = sc;
    339 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    340 	ifp->if_ioctl = aumac_ioctl;
    341 	ifp->if_start = aumac_start;
    342 	ifp->if_watchdog = aumac_watchdog;
    343 	ifp->if_init = aumac_init;
    344 	ifp->if_stop = aumac_stop;
    345 	IFQ_SET_READY(&ifp->if_snd);
    346 
    347 	/* Attach the interface. */
    348 	if_attach(ifp);
    349 	ether_ifattach(ifp, enaddr);
    350 
    351 #ifdef AUMAC_EVENT_COUNTERS
    352 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    353 	    NULL, sc->sc_dev.dv_xname, "txstall");
    354 	evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC,
    355 	    NULL, sc->sc_dev.dv_xname, "rxstall");
    356 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC,
    357 	    NULL, sc->sc_dev.dv_xname, "txintr");
    358 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC,
    359 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    360 #endif
    361 
    362 	/* Make sure the interface is shutdown during reboot. */
    363 	sc->sc_sdhook = shutdownhook_establish(aumac_shutdown, sc);
    364 	if (sc->sc_sdhook == NULL)
    365 		printf("%s: WARNING: unable to establish shutdown hook\n",
    366 		    sc->sc_dev.dv_xname);
    367 	return;
    368 }
    369 
    370 /*
    371  * aumac_shutdown:
    372  *
    373  *	Make sure the interface is stopped at reboot time.
    374  */
    375 static void
    376 aumac_shutdown(void *arg)
    377 {
    378 	struct aumac_softc *sc = arg;
    379 
    380 	aumac_stop(&sc->sc_ethercom.ec_if, 1);
    381 
    382 	/*
    383 	 * XXX aumac_stop leaves device powered up at the moment
    384 	 * XXX but this still isn't enough to keep yamon happy... :-(
    385 	 */
    386 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, 0);
    387 }
    388 
    389 /*
    390  * aumac_start:		[ifnet interface function]
    391  *
    392  *	Start packet transmission on the interface.
    393  */
    394 static void
    395 aumac_start(struct ifnet *ifp)
    396 {
    397 	struct aumac_softc *sc = ifp->if_softc;
    398 	struct mbuf *m;
    399 	int nexttx;
    400 
    401 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    402 		return;
    403 
    404 	/*
    405 	 * Loop through the send queue, setting up transmit descriptors
    406 	 * unitl we drain the queue, or use up all available transmit
    407 	 * descriptors.
    408 	 */
    409 	for (;;) {
    410 		/* Grab a packet off the queue. */
    411 		IFQ_POLL(&ifp->if_snd, m);
    412 		if (m == NULL)
    413 			return;
    414 
    415 		/* Get a spare descriptor. */
    416 		if (sc->sc_txfree == 0) {
    417 			/* No more slots left; notify upper layer. */
    418 			ifp->if_flags |= IFF_OACTIVE;
    419 			AUMAC_EVCNT_INCR(&sc->sc_ev_txstall);
    420 			return;
    421 		}
    422 		nexttx = sc->sc_txnext;
    423 
    424 		IFQ_DEQUEUE(&ifp->if_snd, m);
    425 
    426 		/*
    427 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    428 		 */
    429 
    430 		m_copydata(m, 0, m->m_pkthdr.len,
    431 		    sc->sc_txbufs[nexttx].buf_vaddr);
    432 
    433 		/* Zero out the remainder of any short packets. */
    434 		if (m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
    435 			memset(sc->sc_txbufs[nexttx].buf_vaddr +
    436 			    m->m_pkthdr.len, 0,
    437 			    ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);
    438 
    439 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    440 		    MACDMA_TX_STAT(nexttx), 0);
    441 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    442 		    MACDMA_TX_LEN(nexttx),
    443 		    m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ?
    444 		    ETHER_MIN_LEN - ETHER_CRC_LEN : m->m_pkthdr.len);
    445 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    446 		    MACDMA_TX_ADDR(nexttx),
    447 		    sc->sc_txbufs[nexttx].buf_paddr | TX_ADDR_EN);
    448 		/* XXX - needed??  we should be coherent */
    449 		bus_space_barrier(sc->sc_st, sc->sc_dma_sh, 0 /* XXX */,
    450 		    0 /* XXX */, BUS_SPACE_BARRIER_WRITE);
    451 
    452 		/* Advance the Tx pointer. */
    453 		sc->sc_txfree--;
    454 		sc->sc_txnext = AUMAC_NEXTTX(nexttx);
    455 
    456 #if NBPFILTER > 0
    457 		/* Pass the packet to any BPF listeners. */
    458 		if (ifp->if_bpf)
    459 			bpf_mtap(ifp->if_bpf, m);
    460 #endif /* NBPFILTER */
    461 
    462 		m_freem(m);
    463 
    464 		/* Set a watchdog timer in case the chip flakes out. */
    465 		ifp->if_timer = 5;
    466 	}
    467 	/* NOTREACHED */
    468 }
    469 
    470 /*
    471  * aumac_watchdog:	[ifnet interface function]
    472  *
    473  *	Watchdog timer handler.
    474  */
    475 static void
    476 aumac_watchdog(struct ifnet *ifp)
    477 {
    478 	struct aumac_softc *sc = ifp->if_softc;
    479 
    480 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    481 	(void) aumac_init(ifp);
    482 
    483 	/* Try to get more packets going. */
    484 	aumac_start(ifp);
    485 }
    486 
    487 /*
    488  * aumac_ioctl:		[ifnet interface function]
    489  *
    490  *	Handle control requests from the operator.
    491  */
    492 static int
    493 aumac_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    494 {
    495 	struct aumac_softc *sc = ifp->if_softc;
    496 	struct ifreq *ifr = (struct ifreq *) data;
    497 	int s, error;
    498 
    499 	s = splnet();
    500 
    501 	switch (cmd) {
    502 	case SIOCSIFMEDIA:
    503 	case SIOCGIFMEDIA:
    504 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    505 		break;
    506 
    507 	default:
    508 		error = ether_ioctl(ifp, cmd, data);
    509 		if (error == ENETRESET) {
    510 			/*
    511 			 * Multicast list has changed; set the hardware filter
    512 			 * accordingly.
    513 			 */
    514 			if (ifp->if_flags & IFF_RUNNING)
    515 				aumac_set_filter(sc);
    516 		}
    517 		break;
    518 	}
    519 
    520 	/* Try to get more packets going. */
    521 	aumac_start(ifp);
    522 
    523 	splx(s);
    524 	return (error);
    525 }
    526 
    527 /*
    528  * aumac_intr:
    529  *
    530  *	Interrupt service routine.
    531  */
    532 static int
    533 aumac_intr(void *arg)
    534 {
    535 	struct aumac_softc *sc = arg;
    536 	int status;
    537 
    538 	/*
    539 	 * There aren't really any interrupt status bits on the
    540 	 * Au1X00 MAC, and each MAC has a dedicated interrupt
    541 	 * in the CPU's built-in interrupt controller.  Just
    542 	 * check for new incoming packets, and then Tx completions
    543 	 * (for status updating).
    544 	 */
    545 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0)
    546 		return (0);
    547 
    548 	status = aumac_rxintr(sc);
    549 	status += aumac_txintr(sc);
    550 
    551 #if NRND > 0
    552 	if (RND_ENABLED(&sc->rnd_source))
    553 		rnd_add_uint32(&sc->rnd_source, status);
    554 #endif
    555 
    556 	return status;
    557 }
    558 
    559 /*
    560  * aumac_txintr:
    561  *
    562  *	Helper; handle transmit interrupts.
    563  */
    564 static int
    565 aumac_txintr(struct aumac_softc *sc)
    566 {
    567 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    568 	uint32_t stat;
    569 	int i;
    570 	int pkts = 0;
    571 
    572 	for (i = sc->sc_txdirty; sc->sc_txfree != AUMAC_NTXDESC;
    573 	     i = AUMAC_NEXTTX(i)) {
    574 		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    575 		     MACDMA_TX_ADDR(i)) & TX_ADDR_DN) == 0)
    576 			break;
    577 		pkts++;
    578 
    579 		/* ACK interrupt. */
    580 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    581 		    MACDMA_TX_ADDR(i), 0);
    582 
    583 		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    584 		    MACDMA_TX_STAT(i));
    585 
    586 		if (stat & TX_STAT_FA) {
    587 			/* XXX STATS */
    588 			ifp->if_oerrors++;
    589 		} else
    590 			ifp->if_opackets++;
    591 
    592 		if (stat & TX_STAT_EC)
    593 			ifp->if_collisions += 16;
    594 		else
    595 			ifp->if_collisions += TX_STAT_CC(stat);
    596 
    597 		sc->sc_txfree++;
    598 		ifp->if_flags &= ~IFF_OACTIVE;
    599 
    600 		/* Try to queue more packets. */
    601 		aumac_start(ifp);
    602 	}
    603 
    604 	if (pkts)
    605 		AUMAC_EVCNT_INCR(&sc->sc_ev_txintr);
    606 
    607 	/* Update the dirty descriptor pointer. */
    608 	sc->sc_txdirty = i;
    609 
    610 	/*
    611 	 * If there are no more pending transmissions, cancel the watchdog
    612 	 * timer.
    613 	 */
    614 	if (sc->sc_txfree == AUMAC_NTXDESC)
    615 		ifp->if_timer = 0;
    616 
    617 	return pkts;
    618 }
    619 
    620 /*
    621  * aumac_rxintr:
    622  *
    623  *	Helper; handle receive interrupts.
    624  */
    625 static int
    626 aumac_rxintr(struct aumac_softc *sc)
    627 {
    628 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    629 	struct mbuf *m;
    630 	uint32_t stat;
    631 	int i, len;
    632 	int pkts = 0;
    633 
    634 	for (i = sc->sc_rxptr;; i = AUMAC_NEXTRX(i)) {
    635 		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    636 		     MACDMA_RX_ADDR(i)) & RX_ADDR_DN) == 0)
    637 			break;
    638 		pkts++;
    639 
    640 		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    641 		    MACDMA_RX_STAT(i));
    642 
    643 #define PRINTERR(str)							\
    644 	do {								\
    645 		error++;						\
    646 		printf("%s: %s\n", sc->sc_dev.dv_xname, str);		\
    647 	} while (0)
    648 
    649 		if (stat & RX_STAT_ERRS) {
    650 			int error = 0;
    651 
    652 			if (stat & RX_STAT_MI)
    653 				PRINTERR("missed frame");
    654 			if (stat & RX_STAT_UC)
    655 				PRINTERR("unknown control frame");
    656 			if (stat & RX_STAT_LE)
    657 				PRINTERR("short frame");
    658 			if (stat & RX_STAT_CR)
    659 				PRINTERR("CRC error");
    660 			if (stat & RX_STAT_ME)
    661 				PRINTERR("medium error");
    662 			if (stat & RX_STAT_CS)
    663 				PRINTERR("late collision");
    664 			if (stat & RX_STAT_FL)
    665 				PRINTERR("frame too big");
    666 			if (stat & RX_STAT_RF)
    667 				PRINTERR("runt frame (collision)");
    668 			if (stat & RX_STAT_WT)
    669 				PRINTERR("watch dog");
    670 			if (stat & RX_STAT_DB) {
    671 				if (stat & (RX_STAT_CS | RX_STAT_RF |
    672 				    RX_STAT_CR)) {
    673 					if (!error)
    674 						goto pktok;
    675 				} else
    676 					PRINTERR("dribbling bit");
    677 			}
    678 #undef PRINTERR
    679 			ifp->if_ierrors++;
    680 
    681  dropit:
    682 			/* reuse the current descriptor */
    683 			AUMAC_INIT_RXDESC(sc, i);
    684 			continue;
    685 		}
    686  pktok:
    687 		len = RX_STAT_L(stat);
    688 
    689 		/*
    690 		 * The Au1X00 MAC includes the CRC with every packet;
    691 		 * trim it off here.
    692 		 */
    693 		len -= ETHER_CRC_LEN;
    694 
    695 		/*
    696 		 * Truncate the packet if it's too big to fit in
    697 		 * a single mbuf cluster.
    698 		 */
    699 		if (len > MCLBYTES - 2)
    700 			len = MCLBYTES - 2;
    701 
    702 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    703 		if (m == NULL) {
    704 			printf("%s: unable to allocate Rx mbuf\n",
    705 			    sc->sc_dev.dv_xname);
    706 			goto dropit;
    707 		}
    708 		if (len > MHLEN - 2) {
    709 			MCLGET(m, M_DONTWAIT);
    710 			if ((m->m_flags & M_EXT) == 0) {
    711 				printf("%s: unable to allocate Rx cluster\n",
    712 				    sc->sc_dev.dv_xname);
    713 				m_freem(m);
    714 				goto dropit;
    715 			}
    716 		}
    717 
    718 		m->m_data += 2;		/* align payload */
    719 		memcpy(mtod(m, caddr_t),
    720 		    sc->sc_rxbufs[i].buf_vaddr, len);
    721 		AUMAC_INIT_RXDESC(sc, i);
    722 
    723 		m->m_pkthdr.rcvif = ifp;
    724 		m->m_pkthdr.len = m->m_len = len;
    725 
    726 #if NBPFILTER > 0
    727 		/* Pass this up to any BPF listeners. */
    728 		if (ifp->if_bpf)
    729 			bpf_mtap(ifp->if_bpf, m);
    730 #endif /* NBPFILTER > 0 */
    731 
    732 		/* Pass it on. */
    733 		(*ifp->if_input)(ifp, m);
    734 		ifp->if_ipackets++;
    735 	}
    736 	if (pkts)
    737 		AUMAC_EVCNT_INCR(&sc->sc_ev_rxintr);
    738 	if (pkts == AUMAC_NRXDESC)
    739 		AUMAC_EVCNT_INCR(&sc->sc_ev_rxstall);
    740 
    741 	/* Update the receive pointer. */
    742 	sc->sc_rxptr = i;
    743 
    744 	return pkts;
    745 }
    746 
    747 /*
    748  * aumac_tick:
    749  *
    750  *	One second timer, used to tick the MII.
    751  */
    752 static void
    753 aumac_tick(void *arg)
    754 {
    755 	struct aumac_softc *sc = arg;
    756 	int s;
    757 
    758 	s = splnet();
    759 	mii_tick(&sc->sc_mii);
    760 	splx(s);
    761 
    762 	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);
    763 }
    764 
    765 /*
    766  * aumac_init:		[ifnet interface function]
    767  *
    768  *	Initialize the interface.  Must be called at splnet().
    769  */
    770 static int
    771 aumac_init(struct ifnet *ifp)
    772 {
    773 	struct aumac_softc *sc = ifp->if_softc;
    774 	int i, error = 0;
    775 
    776 	/* Cancel any pending I/O, reset MAC. */
    777 	aumac_stop(ifp, 0);
    778 
    779 	/* Set up the transmit ring. */
    780 	for (i = 0; i < AUMAC_NTXDESC; i++) {
    781 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    782 		    MACDMA_TX_STAT(i), 0);
    783 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    784 		    MACDMA_TX_LEN(i), 0);
    785 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    786 		    MACDMA_TX_ADDR(i), sc->sc_txbufs[i].buf_paddr);
    787 	}
    788 	sc->sc_txfree = AUMAC_NTXDESC;
    789 	sc->sc_txnext = TX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    790 	    MACDMA_TX_ADDR(0)));
    791 	sc->sc_txdirty = sc->sc_txnext;
    792 
    793 	/* Set up the receive ring. */
    794 	for (i = 0; i < AUMAC_NRXDESC; i++)
    795 			AUMAC_INIT_RXDESC(sc, i);
    796 	sc->sc_rxptr = RX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    797 	    MACDMA_RX_ADDR(0)));
    798 
    799 	/*
    800 	 * Power up the MAC.
    801 	 */
    802 	aumac_powerup(sc);
    803 
    804 	sc->sc_control |= CONTROL_DO | CONTROL_TE | CONTROL_RE;
    805 #if _BYTE_ORDER == _BIG_ENDIAN
    806 	sc->sc_control |= CONTROL_EM;
    807 #endif
    808 
    809 	/* Set the media. */
    810 	aumac_mediachange(ifp);
    811 
    812 	/*
    813 	 * Set the receive filter.  This will actually start the transmit
    814 	 * and receive processes.
    815 	 */
    816 	aumac_set_filter(sc);
    817 
    818 	/* Start the one second clock. */
    819 	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);
    820 
    821 	/* ...all done! */
    822 	ifp->if_flags |= IFF_RUNNING;
    823 	ifp->if_flags &= ~IFF_OACTIVE;
    824 
    825 	if (error)
    826 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    827 	return (error);
    828 }
    829 
    830 /*
    831  * aumac_stop:		[ifnet interface function]
    832  *
    833  *	Stop transmission on the interface.
    834  */
    835 static void
    836 aumac_stop(struct ifnet *ifp, int disable)
    837 {
    838 	struct aumac_softc *sc = ifp->if_softc;
    839 
    840 	/* Stop the one-second clock. */
    841 	callout_stop(&sc->sc_tick_ch);
    842 
    843 	/* Down the MII. */
    844 	mii_down(&sc->sc_mii);
    845 
    846 	/* Stop the transmit and receive processes. */
    847 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 0);
    848 
    849 	/* Power down/reset the MAC. */
    850 	aumac_powerdown(sc);
    851 
    852 	/* Mark the interface as down and cancel the watchdog timer. */
    853 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    854 	ifp->if_timer = 0;
    855 }
    856 
    857 /*
    858  * aumac_powerdown:
    859  *
    860  *	Power down the MAC.
    861  */
    862 static void
    863 aumac_powerdown(struct aumac_softc *sc)
    864 {
    865 
    866 	/* Disable the MAC clocks, and place the device in reset. */
    867 	// bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP);
    868 
    869 	// delay(10000);
    870 }
    871 
    872 /*
    873  * aumac_powerup:
    874  *
    875  *	Bring the device out of reset.
    876  */
    877 static void
    878 aumac_powerup(struct aumac_softc *sc)
    879 {
    880 
    881 	/* Enable clocks to the MAC. */
    882 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP|MACEN_CE);
    883 
    884 	/* Enable MAC, coherent transactions, pass only valid frames. */
    885 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0,
    886 	    MACEN_E2|MACEN_E1|MACEN_E0|MACEN_CE);
    887 
    888 	delay(20000);
    889 }
    890 
    891 /*
    892  * aumac_set_filter:
    893  *
    894  *	Set up the receive filter.
    895  */
    896 static void
    897 aumac_set_filter(struct aumac_softc *sc)
    898 {
    899 	struct ethercom *ec = &sc->sc_ethercom;
    900 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    901 	struct ether_multi *enm;
    902 	struct ether_multistep step;
    903 	const uint8_t *enaddr = LLADDR(ifp->if_sadl);
    904 	uint32_t mchash[2], crc;
    905 
    906 	sc->sc_control &= ~(CONTROL_PM | CONTROL_PR);
    907 
    908 	/* Stop the receiver. */
    909 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    910 	    sc->sc_control & ~CONTROL_RE);
    911 
    912 	if (ifp->if_flags & IFF_PROMISC) {
    913 		sc->sc_control |= CONTROL_PR;
    914 		goto allmulti;
    915 	}
    916 
    917 	/* Set the station address. */
    918 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRHIGH,
    919 	    enaddr[4] | (enaddr[5] << 8));
    920 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRLOW,
    921 	    enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
    922 	    (enaddr[3] << 24));
    923 
    924 	sc->sc_control |= CONTROL_HP;
    925 
    926 	mchash[0] = mchash[1] = 0;
    927 
    928 	/*
    929 	 * Set up the multicast address filter by passing all multicast
    930 	 * addresses through a CRC generator, and then using the high
    931 	 * order 6 bits as an index into the 64-bit multicast hash table.
    932 	 * The high order bits select the word, while the rest of the bits
    933 	 * select the bit within the word.
    934 	 */
    935 	ETHER_FIRST_MULTI(step, ec, enm);
    936 	while (enm != NULL) {
    937 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    938 			/*
    939 			 * We must listen to a range of multicast addresses.
    940 			 * For now, just accept all multicasts, rather than
    941 			 * trying to set only those filter bits needed to match
    942 			 * the range.  (At this time, the only use of address
    943 			 * ranges is for IP multicast routing, for which the
    944 			 * range is large enough to require all bits set.)
    945 			 */
    946 			goto allmulti;
    947 		}
    948 
    949 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
    950 
    951 		/* Just want the 6 most significant bits. */
    952 		crc >>= 26;
    953 
    954 		/* Set the corresponding bit in the filter. */
    955 		mchash[crc >> 5] |= 1U << (crc & 0x1f);
    956 
    957 		ETHER_NEXT_MULTI(step, enm);
    958 	}
    959 
    960 	ifp->if_flags &= ~IFF_ALLMULTI;
    961 
    962 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHHIGH,
    963 	    mchash[1]);
    964 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHLOW,
    965 	    mchash[0]);
    966 
    967 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    968 	    sc->sc_control);
    969 	return;
    970 
    971  allmulti:
    972 	sc->sc_control |= CONTROL_PM;
    973 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    974 	    sc->sc_control);
    975 }
    976 
    977 /*
    978  * aumac_mediastatus:	[ifmedia interface function]
    979  *
    980  *	Get the current interface media status.
    981  */
    982 static void
    983 aumac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    984 {
    985 	struct aumac_softc *sc = ifp->if_softc;
    986 
    987 	mii_pollstat(&sc->sc_mii);
    988 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    989 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    990 }
    991 
    992 /*
    993  * aumac_mediachange:	[ifmedia interface function]
    994  *
    995  *	Set hardware to newly selected media.
    996  */
    997 static int
    998 aumac_mediachange(struct ifnet *ifp)
    999 {
   1000 	struct aumac_softc *sc = ifp->if_softc;
   1001 
   1002 	if (ifp->if_flags & IFF_UP)
   1003 		mii_mediachg(&sc->sc_mii);
   1004 	return (0);
   1005 }
   1006 
   1007 /*
   1008  * aumac_mii_wait:
   1009  *
   1010  *	Wait for the MII interface to not be busy.
   1011  */
   1012 static int
   1013 aumac_mii_wait(struct aumac_softc *sc, const char *msg)
   1014 {
   1015 	int i;
   1016 
   1017 	for (i = 0; i < 10000; i++) {
   1018 		if ((bus_space_read_4(sc->sc_st, sc->sc_mac_sh,
   1019 		     MAC_MIICTRL) & MIICTRL_MB) == 0)
   1020 			return (0);
   1021 		delay(10);
   1022 	}
   1023 
   1024 	printf("%s: MII failed to %s\n", sc->sc_dev.dv_xname, msg);
   1025 	return (1);
   1026 }
   1027 
   1028 /*
   1029  * aumac_mii_readreg:	[mii interface function]
   1030  *
   1031  *	Read a PHY register on the MII.
   1032  */
   1033 static int
   1034 aumac_mii_readreg(struct device *self, int phy, int reg)
   1035 {
   1036 	struct aumac_softc *sc = (void *) self;
   1037 
   1038 	if (aumac_mii_wait(sc, "become ready"))
   1039 		return (0);
   1040 
   1041 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
   1042 	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg));
   1043 
   1044 	if (aumac_mii_wait(sc, "complete"))
   1045 		return (0);
   1046 
   1047 	return (bus_space_read_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA) &
   1048 	    MIIDATA_MASK);
   1049 }
   1050 
   1051 /*
   1052  * aumac_mii_writereg:	[mii interface function]
   1053  *
   1054  *	Write a PHY register on the MII.
   1055  */
   1056 static void
   1057 aumac_mii_writereg(struct device *self, int phy, int reg, int val)
   1058 {
   1059 	struct aumac_softc *sc = (void *) self;
   1060 
   1061 	if (aumac_mii_wait(sc, "become ready"))
   1062 		return;
   1063 
   1064 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA, val);
   1065 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
   1066 	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg) | MIICTRL_MW);
   1067 
   1068 	(void) aumac_mii_wait(sc, "complete");
   1069 }
   1070 
   1071 /*
   1072  * aumac_mii_statchg:	[mii interface function]
   1073  *
   1074  *	Callback from MII layer when media changes.
   1075  */
   1076 static void
   1077 aumac_mii_statchg(struct device *self)
   1078 {
   1079 	struct aumac_softc *sc = (void *) self;
   1080 
   1081 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
   1082 		sc->sc_control |= CONTROL_F;
   1083 	else
   1084 		sc->sc_control &= ~CONTROL_F;
   1085 
   1086 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
   1087 	    sc->sc_control);
   1088 }
   1089