Home | History | Annotate | Line # | Download | only in mace
if_mec.c revision 1.17
      1 /* $NetBSD: if_mec.c,v 1.17 2008/01/26 14:28:49 tsutsui Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004 Izumi Tsutsui.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Copyright (c) 2003 Christopher SEKIYA
     32  * All rights reserved.
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *          This product includes software developed for the
     45  *          NetBSD Project.  See http://www.NetBSD.org/ for
     46  *          information about NetBSD.
     47  * 4. The name of the author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * MACE MAC-110 Ethernet driver
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: if_mec.c,v 1.17 2008/01/26 14:28:49 tsutsui Exp $");
     68 
     69 #include "opt_ddb.h"
     70 #include "bpfilter.h"
     71 #include "rnd.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/device.h>
     76 #include <sys/callout.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/malloc.h>
     79 #include <sys/kernel.h>
     80 #include <sys/socket.h>
     81 #include <sys/ioctl.h>
     82 #include <sys/errno.h>
     83 
     84 #if NRND > 0
     85 #include <sys/rnd.h>
     86 #endif
     87 
     88 #include <net/if.h>
     89 #include <net/if_dl.h>
     90 #include <net/if_media.h>
     91 #include <net/if_ether.h>
     92 
     93 #if NBPFILTER > 0
     94 #include <net/bpf.h>
     95 #endif
     96 
     97 #include <machine/bus.h>
     98 #include <machine/intr.h>
     99 #include <machine/machtype.h>
    100 
    101 #include <dev/mii/mii.h>
    102 #include <dev/mii/miivar.h>
    103 
    104 #include <sgimips/mace/macevar.h>
    105 #include <sgimips/mace/if_mecreg.h>
    106 
    107 #include <dev/arcbios/arcbios.h>
    108 #include <dev/arcbios/arcbiosvar.h>
    109 
    110 /* #define MEC_DEBUG */
    111 
    112 #ifdef MEC_DEBUG
    113 #define MEC_DEBUG_RESET		0x01
    114 #define MEC_DEBUG_START		0x02
    115 #define MEC_DEBUG_STOP		0x04
    116 #define MEC_DEBUG_INTR		0x08
    117 #define MEC_DEBUG_RXINTR	0x10
    118 #define MEC_DEBUG_TXINTR	0x20
    119 uint32_t mec_debug = 0;
    120 #define DPRINTF(x, y)	if (mec_debug & (x)) printf y
    121 #else
    122 #define DPRINTF(x, y)	/* nothing */
    123 #endif
    124 
    125 /*
    126  * Transmit descriptor list size
    127  */
    128 #define MEC_NTXDESC		64
    129 #define MEC_NTXDESC_MASK	(MEC_NTXDESC - 1)
    130 #define MEC_NEXTTX(x)		(((x) + 1) & MEC_NTXDESC_MASK)
    131 
    132 /*
    133  * software state for TX
    134  */
    135 struct mec_txsoft {
    136 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    137 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    138 	uint32_t txs_flags;
    139 #define MEC_TXS_BUFLEN_MASK	0x0000007f	/* data len in txd_buf */
    140 #define MEC_TXS_TXDBUF		0x00000080	/* txd_buf is used */
    141 #define MEC_TXS_TXDPTR1		0x00000100	/* txd_ptr[0] is used */
    142 };
    143 
    144 /*
    145  * Transmit buffer descriptor
    146  */
    147 #define MEC_TXDESCSIZE		128
    148 #define MEC_NTXPTR		3
    149 #define MEC_TXD_BUFOFFSET	\
    150 	(sizeof(uint64_t) + MEC_NTXPTR * sizeof(uint64_t))
    151 #define MEC_TXD_BUFSIZE		(MEC_TXDESCSIZE - MEC_TXD_BUFOFFSET)
    152 #define MEC_TXD_BUFSTART(len)	(MEC_TXD_BUFSIZE - (len))
    153 #define MEC_TXD_ALIGN		8
    154 #define MEC_TXD_ROUNDUP(addr)	\
    155 	(((addr) + (MEC_TXD_ALIGN - 1)) & ~((uint64_t)MEC_TXD_ALIGN - 1))
    156 
    157 struct mec_txdesc {
    158 	volatile uint64_t txd_cmd;
    159 #define MEC_TXCMD_DATALEN	0x000000000000ffff	/* data length */
    160 #define MEC_TXCMD_BUFSTART	0x00000000007f0000	/* start byte offset */
    161 #define  TXCMD_BUFSTART(x)	((x) << 16)
    162 #define MEC_TXCMD_TERMDMA	0x0000000000800000	/* stop DMA on abort */
    163 #define MEC_TXCMD_TXINT		0x0000000001000000	/* INT after TX done */
    164 #define MEC_TXCMD_PTR1		0x0000000002000000	/* valid 1st txd_ptr */
    165 #define MEC_TXCMD_PTR2		0x0000000004000000	/* valid 2nd txd_ptr */
    166 #define MEC_TXCMD_PTR3		0x0000000008000000	/* valid 3rd txd_ptr */
    167 #define MEC_TXCMD_UNUSED	0xfffffffff0000000ULL	/* should be zero */
    168 
    169 #define txd_stat	txd_cmd
    170 #define MEC_TXSTAT_LEN		0x000000000000ffff	/* TX length */
    171 #define MEC_TXSTAT_COLCNT	0x00000000000f0000	/* collision count */
    172 #define MEC_TXSTAT_COLCNT_SHIFT	16
    173 #define MEC_TXSTAT_LATE_COL	0x0000000000100000	/* late collision */
    174 #define MEC_TXSTAT_CRCERROR	0x0000000000200000	/* */
    175 #define MEC_TXSTAT_DEFERRED	0x0000000000400000	/* */
    176 #define MEC_TXSTAT_SUCCESS	0x0000000000800000	/* TX complete */
    177 #define MEC_TXSTAT_TOOBIG	0x0000000001000000	/* */
    178 #define MEC_TXSTAT_UNDERRUN	0x0000000002000000	/* */
    179 #define MEC_TXSTAT_COLLISIONS	0x0000000004000000	/* */
    180 #define MEC_TXSTAT_EXDEFERRAL	0x0000000008000000	/* */
    181 #define MEC_TXSTAT_COLLIDED	0x0000000010000000	/* */
    182 #define MEC_TXSTAT_UNUSED	0x7fffffffe0000000ULL	/* should be zero */
    183 #define MEC_TXSTAT_SENT		0x8000000000000000ULL	/* packet sent */
    184 
    185 	uint64_t txd_ptr[MEC_NTXPTR];
    186 #define MEC_TXPTR_UNUSED2	0x0000000000000007	/* should be zero */
    187 #define MEC_TXPTR_DMAADDR	0x00000000fffffff8	/* TX DMA address */
    188 #define MEC_TXPTR_LEN		0x0000ffff00000000ULL	/* buffer length */
    189 #define  TXPTR_LEN(x)		((uint64_t)(x) << 32)
    190 #define MEC_TXPTR_UNUSED1	0xffff000000000000ULL	/* should be zero */
    191 
    192 	uint8_t txd_buf[MEC_TXD_BUFSIZE];
    193 };
    194 
    195 /*
    196  * Receive buffer size
    197  */
    198 #define MEC_NRXDESC		16
    199 #define MEC_NRXDESC_MASK	(MEC_NRXDESC - 1)
    200 #define MEC_NEXTRX(x)		(((x) + 1) & MEC_NRXDESC_MASK)
    201 
    202 /*
    203  * Receive buffer description
    204  */
    205 #define MEC_RXDESCSIZE		4096	/* umm, should be 4kbyte aligned */
    206 #define MEC_RXD_NRXPAD		3
    207 #define MEC_RXD_DMAOFFSET	(1 + MEC_RXD_NRXPAD)
    208 #define MEC_RXD_BUFOFFSET	(MEC_RXD_DMAOFFSET * sizeof(uint64_t))
    209 #define MEC_RXD_BUFSIZE		(MEC_RXDESCSIZE - MEC_RXD_BUFOFFSET)
    210 
    211 struct mec_rxdesc {
    212 	volatile uint64_t rxd_stat;
    213 #define MEC_RXSTAT_LEN		0x000000000000ffff	/* data length */
    214 #define MEC_RXSTAT_VIOLATION	0x0000000000010000	/* code violation (?) */
    215 #define MEC_RXSTAT_UNUSED2	0x0000000000020000	/* unknown (?) */
    216 #define MEC_RXSTAT_CRCERROR	0x0000000000040000	/* CRC error */
    217 #define MEC_RXSTAT_MULTICAST	0x0000000000080000	/* multicast packet */
    218 #define MEC_RXSTAT_BROADCAST	0x0000000000100000	/* broadcast packet */
    219 #define MEC_RXSTAT_INVALID	0x0000000000200000	/* invalid preamble */
    220 #define MEC_RXSTAT_LONGEVENT	0x0000000000400000	/* long packet */
    221 #define MEC_RXSTAT_BADPACKET	0x0000000000800000	/* bad packet */
    222 #define MEC_RXSTAT_CAREVENT	0x0000000001000000	/* carrier event */
    223 #define MEC_RXSTAT_MATCHMCAST	0x0000000002000000	/* match multicast */
    224 #define MEC_RXSTAT_MATCHMAC	0x0000000004000000	/* match MAC */
    225 #define MEC_RXSTAT_SEQNUM	0x00000000f8000000	/* sequence number */
    226 #define MEC_RXSTAT_CKSUM	0x0000ffff00000000ULL	/* IP checksum */
    227 #define MEC_RXSTAT_UNUSED1	0x7fff000000000000ULL	/* should be zero */
    228 #define MEC_RXSTAT_RECEIVED	0x8000000000000000ULL	/* set to 1 on RX */
    229 	uint64_t rxd_pad1[MEC_RXD_NRXPAD];
    230 	uint8_t  rxd_buf[MEC_RXD_BUFSIZE];
    231 };
    232 
    233 /*
    234  * control structures for DMA ops
    235  */
    236 struct mec_control_data {
    237 	/*
    238 	 * TX descriptors and buffers
    239 	 */
    240 	struct mec_txdesc mcd_txdesc[MEC_NTXDESC];
    241 
    242 	/*
    243 	 * RX descriptors and buffers
    244 	 */
    245 	struct mec_rxdesc mcd_rxdesc[MEC_NRXDESC];
    246 };
    247 
    248 /*
    249  * It _seems_ there are some restrictions on descriptor address:
    250  *
    251  * - Base address of txdescs should be 8kbyte aligned
    252  * - Each txdesc should be 128byte aligned
    253  * - Each rxdesc should be 4kbyte aligned
    254  *
    255  * So we should specify 8k align to allocalte txdescs.
    256  * In this case, sizeof(struct mec_txdesc) * MEC_NTXDESC is 8192
    257  * so rxdescs are also allocated at 4kbyte aligned.
    258  */
    259 #define MEC_CONTROL_DATA_ALIGN	(8 * 1024)
    260 
    261 #define MEC_CDOFF(x)	offsetof(struct mec_control_data, x)
    262 #define MEC_CDTXOFF(x)	MEC_CDOFF(mcd_txdesc[(x)])
    263 #define MEC_CDRXOFF(x)	MEC_CDOFF(mcd_rxdesc[(x)])
    264 
    265 /*
    266  * software state per device
    267  */
    268 struct mec_softc {
    269 	struct device sc_dev;		/* generic device structures */
    270 
    271 	bus_space_tag_t sc_st;		/* bus_space tag */
    272 	bus_space_handle_t sc_sh;	/* bus_space handle */
    273 	bus_dma_tag_t sc_dmat;		/* bus_dma tag */
    274 	void *sc_sdhook;		/* shutdown hook */
    275 
    276 	struct ethercom sc_ethercom;	/* Ethernet common part */
    277 
    278 	struct mii_data sc_mii;		/* MII/media information */
    279 	int sc_phyaddr;			/* MII address */
    280 	struct callout sc_tick_ch;	/* tick callout */
    281 
    282 	uint8_t sc_enaddr[ETHER_ADDR_LEN]; /* MAC address */
    283 
    284 	bus_dmamap_t sc_cddmamap;	/* bus_dma map for control data */
    285 #define sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    286 
    287 	/* pointer to allocated control data */
    288 	struct mec_control_data *sc_control_data;
    289 #define sc_txdesc	sc_control_data->mcd_txdesc
    290 #define sc_rxdesc	sc_control_data->mcd_rxdesc
    291 
    292 	/* software state for TX descs */
    293 	struct mec_txsoft sc_txsoft[MEC_NTXDESC];
    294 
    295 	int sc_txpending;		/* number of TX requests pending */
    296 	int sc_txdirty;			/* first dirty TX descriptor */
    297 	int sc_txlast;			/* last used TX descriptor */
    298 
    299 	int sc_rxptr;			/* next ready RX buffer */
    300 
    301 #if NRND > 0
    302 	rndsource_element_t sc_rnd_source; /* random source */
    303 #endif
    304 };
    305 
    306 #define MEC_CDTXADDR(sc, x)	((sc)->sc_cddma + MEC_CDTXOFF(x))
    307 #define MEC_CDRXADDR(sc, x)	((sc)->sc_cddma + MEC_CDRXOFF(x))
    308 
    309 #define MEC_TXDESCSYNC(sc, x, ops)					\
    310 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    311 	    MEC_CDTXOFF(x), MEC_TXDESCSIZE, (ops))
    312 #define MEC_TXCMDSYNC(sc, x, ops)					\
    313 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    314 	    MEC_CDTXOFF(x), sizeof(uint64_t), (ops))
    315 
    316 #define MEC_RXSTATSYNC(sc, x, ops)					\
    317 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    318 	    MEC_CDRXOFF(x), sizeof(uint64_t), (ops))
    319 #define MEC_RXBUFSYNC(sc, x, len, ops)					\
    320 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    321 	    MEC_CDRXOFF(x) + MEC_RXD_BUFOFFSET,				\
    322 	    MEC_ETHER_ALIGN + (len), (ops))
    323 
    324 /* XXX these values should be moved to <net/if_ether.h> ? */
    325 #define ETHER_PAD_LEN	(ETHER_MIN_LEN - ETHER_CRC_LEN)
    326 #define MEC_ETHER_ALIGN	2
    327 
    328 #ifdef DDB
    329 #define STATIC
    330 #else
    331 #define STATIC static
    332 #endif
    333 
    334 STATIC int	mec_match(struct device *, struct cfdata *, void *);
    335 STATIC void	mec_attach(struct device *, struct device *, void *);
    336 
    337 STATIC int	mec_mii_readreg(struct device *, int, int);
    338 STATIC void	mec_mii_writereg(struct device *, int, int, int);
    339 STATIC int	mec_mii_wait(struct mec_softc *);
    340 STATIC void	mec_statchg(struct device *);
    341 
    342 static void	enaddr_aton(const char *, uint8_t *);
    343 
    344 STATIC int	mec_init(struct ifnet * ifp);
    345 STATIC void	mec_start(struct ifnet *);
    346 STATIC void	mec_watchdog(struct ifnet *);
    347 STATIC void	mec_tick(void *);
    348 STATIC int	mec_ioctl(struct ifnet *, u_long, void *);
    349 STATIC void	mec_reset(struct mec_softc *);
    350 STATIC void	mec_setfilter(struct mec_softc *);
    351 STATIC int	mec_intr(void *arg);
    352 STATIC void	mec_stop(struct ifnet *, int);
    353 STATIC void	mec_rxintr(struct mec_softc *);
    354 STATIC void	mec_txintr(struct mec_softc *);
    355 STATIC void	mec_shutdown(void *);
    356 
    357 CFATTACH_DECL(mec, sizeof(struct mec_softc),
    358     mec_match, mec_attach, NULL, NULL);
    359 
    360 static int mec_matched = 0;
    361 
    362 STATIC int
    363 mec_match(struct device *parent, struct cfdata *match, void *aux)
    364 {
    365 
    366 	/* allow only one device */
    367 	if (mec_matched)
    368 		return 0;
    369 
    370 	mec_matched = 1;
    371 	return 1;
    372 }
    373 
    374 STATIC void
    375 mec_attach(struct device *parent, struct device *self, void *aux)
    376 {
    377 	struct mec_softc *sc = (void *)self;
    378 	struct mace_attach_args *maa = aux;
    379 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    380 	uint64_t address, command;
    381 	const char *macaddr;
    382 	struct mii_softc *child;
    383 	bus_dma_segment_t seg;
    384 	int i, err, rseg;
    385 
    386 	sc->sc_st = maa->maa_st;
    387 	if (bus_space_subregion(sc->sc_st, maa->maa_sh,
    388 	    maa->maa_offset, 0,	&sc->sc_sh) != 0) {
    389 		printf(": can't map i/o space\n");
    390 		return;
    391 	}
    392 
    393 	/* set up DMA structures */
    394 	sc->sc_dmat = maa->maa_dmat;
    395 
    396 	/*
    397 	 * Allocate the control data structures, and create and load the
    398 	 * DMA map for it.
    399 	 */
    400 	if ((err = bus_dmamem_alloc(sc->sc_dmat,
    401 	    sizeof(struct mec_control_data), MEC_CONTROL_DATA_ALIGN, 0,
    402 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    403 		printf(": unable to allocate control data, error = %d\n", err);
    404 		goto fail_0;
    405 	}
    406 	/*
    407 	 * XXX needs re-think...
    408 	 * control data structures contain whole RX data buffer, so
    409 	 * BUS_DMA_COHERENT (which disables cache) may cause some performance
    410 	 * issue on copying data from the RX buffer to mbuf on normal memory,
    411 	 * though we have to make sure all bus_dmamap_sync(9) ops are called
    412 	 * properly in that case.
    413 	 */
    414 	if ((err = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    415 	    sizeof(struct mec_control_data),
    416 	    (void **)&sc->sc_control_data, /*BUS_DMA_COHERENT*/ 0)) != 0) {
    417 		printf(": unable to map control data, error = %d\n", err);
    418 		goto fail_1;
    419 	}
    420 	memset(sc->sc_control_data, 0, sizeof(struct mec_control_data));
    421 
    422 	if ((err = bus_dmamap_create(sc->sc_dmat,
    423 	    sizeof(struct mec_control_data), 1,
    424 	    sizeof(struct mec_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    425 		printf(": unable to create control data DMA map, error = %d\n",
    426 		    err);
    427 		goto fail_2;
    428 	}
    429 	if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    430 	    sc->sc_control_data, sizeof(struct mec_control_data), NULL,
    431 	    BUS_DMA_NOWAIT)) != 0) {
    432 		printf(": unable to load control data DMA map, error = %d\n",
    433 		    err);
    434 		goto fail_3;
    435 	}
    436 
    437 	/* create TX buffer DMA maps */
    438 	for (i = 0; i < MEC_NTXDESC; i++) {
    439 		if ((err = bus_dmamap_create(sc->sc_dmat,
    440 		    MCLBYTES, 1, MCLBYTES, 0, 0,
    441 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    442 			printf(": unable to create tx DMA map %d, error = %d\n",
    443 			    i, err);
    444 			goto fail_4;
    445 		}
    446 	}
    447 
    448 	callout_init(&sc->sc_tick_ch, 0);
    449 
    450 	/* get Ethernet address from ARCBIOS */
    451 	if ((macaddr = ARCBIOS->GetEnvironmentVariable("eaddr")) == NULL) {
    452 		printf(": unable to get MAC address!\n");
    453 		goto fail_4;
    454 	}
    455 	enaddr_aton(macaddr, sc->sc_enaddr);
    456 
    457 	/* set the Ethernet address */
    458 	address = 0;
    459 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    460 		address = address << 8;
    461 		address |= sc->sc_enaddr[i];
    462 	}
    463 	bus_space_write_8(sc->sc_st, sc->sc_sh, MEC_STATION, address);
    464 
    465 	/* reset device */
    466 	mec_reset(sc);
    467 
    468 	command = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_MAC_CONTROL);
    469 
    470 	printf(": MAC-110 Ethernet, rev %u\n",
    471 	    (u_int)((command & MEC_MAC_REVISION) >> MEC_MAC_REVISION_SHIFT));
    472 
    473 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    474 	    ether_sprintf(sc->sc_enaddr));
    475 
    476 	/* Done, now attach everything */
    477 
    478 	sc->sc_mii.mii_ifp = ifp;
    479 	sc->sc_mii.mii_readreg = mec_mii_readreg;
    480 	sc->sc_mii.mii_writereg = mec_mii_writereg;
    481 	sc->sc_mii.mii_statchg = mec_statchg;
    482 
    483 	/* Set up PHY properties */
    484 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    485 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
    486 	    ether_mediastatus);
    487 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    488 	    MII_OFFSET_ANY, 0);
    489 
    490 	child = LIST_FIRST(&sc->sc_mii.mii_phys);
    491 	if (child == NULL) {
    492 		/* No PHY attached */
    493 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
    494 		    0, NULL);
    495 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
    496 	} else {
    497 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    498 		sc->sc_phyaddr = child->mii_phy;
    499 	}
    500 
    501 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    502 	ifp->if_softc = sc;
    503 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    504 	ifp->if_ioctl = mec_ioctl;
    505 	ifp->if_start = mec_start;
    506 	ifp->if_watchdog = mec_watchdog;
    507 	ifp->if_init = mec_init;
    508 	ifp->if_stop = mec_stop;
    509 	ifp->if_mtu = ETHERMTU;
    510 	IFQ_SET_READY(&ifp->if_snd);
    511 
    512 	if_attach(ifp);
    513 	ether_ifattach(ifp, sc->sc_enaddr);
    514 
    515 	/* establish interrupt */
    516 	cpu_intr_establish(maa->maa_intr, maa->maa_intrmask, mec_intr, sc);
    517 
    518 #if NRND > 0
    519 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
    520 	    RND_TYPE_NET, 0);
    521 #endif
    522 
    523 	/* set shutdown hook to reset interface on powerdown */
    524 	sc->sc_sdhook = shutdownhook_establish(mec_shutdown, sc);
    525 
    526 	return;
    527 
    528 	/*
    529 	 * Free any resources we've allocated during the failed attach
    530 	 * attempt.  Do this in reverse order and fall though.
    531 	 */
    532  fail_4:
    533 	for (i = 0; i < MEC_NTXDESC; i++) {
    534 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    535 			bus_dmamap_destroy(sc->sc_dmat,
    536 			    sc->sc_txsoft[i].txs_dmamap);
    537 	}
    538 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    539  fail_3:
    540 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    541  fail_2:
    542 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    543 	    sizeof(struct mec_control_data));
    544  fail_1:
    545 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    546  fail_0:
    547 	return;
    548 }
    549 
    550 STATIC int
    551 mec_mii_readreg(struct device *self, int phy, int reg)
    552 {
    553 	struct mec_softc *sc = (void *)self;
    554 	bus_space_tag_t st = sc->sc_st;
    555 	bus_space_handle_t sh = sc->sc_sh;
    556 	uint64_t val;
    557 	int i;
    558 
    559 	if (mec_mii_wait(sc) != 0)
    560 		return 0;
    561 
    562 	bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
    563 	    (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
    564 	delay(25);
    565 	bus_space_write_8(st, sh, MEC_PHY_READ_INITIATE, 1);
    566 	delay(25);
    567 	mec_mii_wait(sc);
    568 
    569 	for (i = 0; i < 20; i++) {
    570 		delay(30);
    571 
    572 		val = bus_space_read_8(st, sh, MEC_PHY_DATA);
    573 
    574 		if ((val & MEC_PHY_DATA_BUSY) == 0)
    575 			return val & MEC_PHY_DATA_VALUE;
    576 	}
    577 	return 0;
    578 }
    579 
    580 STATIC void
    581 mec_mii_writereg(struct device *self, int phy, int reg, int val)
    582 {
    583 	struct mec_softc *sc = (void *)self;
    584 	bus_space_tag_t st = sc->sc_st;
    585 	bus_space_handle_t sh = sc->sc_sh;
    586 
    587 	if (mec_mii_wait(sc) != 0) {
    588 		printf("timed out writing %x: %x\n", reg, val);
    589 		return;
    590 	}
    591 
    592 	bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
    593 	    (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
    594 
    595 	delay(60);
    596 
    597 	bus_space_write_8(st, sh, MEC_PHY_DATA, val & MEC_PHY_DATA_VALUE);
    598 
    599 	delay(60);
    600 
    601 	mec_mii_wait(sc);
    602 }
    603 
    604 STATIC int
    605 mec_mii_wait(struct mec_softc *sc)
    606 {
    607 	uint32_t busy;
    608 	int i, s;
    609 
    610 	for (i = 0; i < 100; i++) {
    611 		delay(30);
    612 
    613 		s = splhigh();
    614 		busy = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_PHY_DATA);
    615 		splx(s);
    616 
    617 		if ((busy & MEC_PHY_DATA_BUSY) == 0)
    618 			return 0;
    619 #if 0
    620 		if (busy == 0xffff) /* XXX ? */
    621 			return 0;
    622 #endif
    623 	}
    624 
    625 	printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
    626 	return 1;
    627 }
    628 
    629 STATIC void
    630 mec_statchg(struct device *self)
    631 {
    632 	struct mec_softc *sc = (void *)self;
    633 	bus_space_tag_t st = sc->sc_st;
    634 	bus_space_handle_t sh = sc->sc_sh;
    635 	uint32_t control;
    636 
    637 	control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
    638 	control &= ~(MEC_MAC_IPGT | MEC_MAC_IPGR1 | MEC_MAC_IPGR2 |
    639 	    MEC_MAC_FULL_DUPLEX | MEC_MAC_SPEED_SELECT);
    640 
    641 	/* must also set IPG here for duplex stuff ... */
    642 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) {
    643 		control |= MEC_MAC_FULL_DUPLEX;
    644 	} else {
    645 		/* set IPG */
    646 		control |= MEC_MAC_IPG_DEFAULT;
    647 	}
    648 
    649 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
    650 }
    651 
    652 /*
    653  * XXX
    654  * maybe this function should be moved to common part
    655  * (sgimips/machdep.c or elsewhere) for all on-board network devices.
    656  */
    657 static void
    658 enaddr_aton(const char *str, uint8_t *eaddr)
    659 {
    660 	int i;
    661 	char c;
    662 
    663 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    664 		if (*str == ':')
    665 			str++;
    666 
    667 		c = *str++;
    668 		if (isdigit(c)) {
    669 			eaddr[i] = (c - '0');
    670 		} else if (isxdigit(c)) {
    671 			eaddr[i] = (toupper(c) + 10 - 'A');
    672 		}
    673 		c = *str++;
    674 		if (isdigit(c)) {
    675 			eaddr[i] = (eaddr[i] << 4) | (c - '0');
    676 		} else if (isxdigit(c)) {
    677 			eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
    678 		}
    679 	}
    680 }
    681 
    682 STATIC int
    683 mec_init(struct ifnet *ifp)
    684 {
    685 	struct mec_softc *sc = ifp->if_softc;
    686 	bus_space_tag_t st = sc->sc_st;
    687 	bus_space_handle_t sh = sc->sc_sh;
    688 	struct mec_rxdesc *rxd;
    689 	int i, rc;
    690 
    691 	/* cancel any pending I/O */
    692 	mec_stop(ifp, 0);
    693 
    694 	/* reset device */
    695 	mec_reset(sc);
    696 
    697 	/* setup filter for multicast or promisc mode */
    698 	mec_setfilter(sc);
    699 
    700 	/* set the TX ring pointer to the base address */
    701 	bus_space_write_8(st, sh, MEC_TX_RING_BASE, MEC_CDTXADDR(sc, 0));
    702 
    703 	sc->sc_txpending = 0;
    704 	sc->sc_txdirty = 0;
    705 	sc->sc_txlast = MEC_NTXDESC - 1;
    706 
    707 	/* put RX buffers into FIFO */
    708 	for (i = 0; i < MEC_NRXDESC; i++) {
    709 		rxd = &sc->sc_rxdesc[i];
    710 		rxd->rxd_stat = 0;
    711 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
    712 		MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
    713 		bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
    714 	}
    715 	sc->sc_rxptr = 0;
    716 
    717 #if 0	/* XXX no info */
    718 	bus_space_write_8(st, sh, MEC_TIMER, 0);
    719 #endif
    720 
    721 	/*
    722 	 * MEC_DMA_TX_INT_ENABLE will be set later otherwise it causes
    723 	 * spurious interrupts when TX buffers are empty
    724 	 */
    725 	bus_space_write_8(st, sh, MEC_DMA_CONTROL,
    726 	    (MEC_RXD_DMAOFFSET << MEC_DMA_RX_DMA_OFFSET_SHIFT) |
    727 	    (MEC_NRXDESC << MEC_DMA_RX_INT_THRESH_SHIFT) |
    728 	    MEC_DMA_TX_DMA_ENABLE | /* MEC_DMA_TX_INT_ENABLE | */
    729 	    MEC_DMA_RX_DMA_ENABLE | MEC_DMA_RX_INT_ENABLE);
    730 
    731 	callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
    732 
    733 	if ((rc = ether_mediachange(ifp)) != 0)
    734 		return rc;
    735 
    736 	ifp->if_flags |= IFF_RUNNING;
    737 	ifp->if_flags &= ~IFF_OACTIVE;
    738 	mec_start(ifp);
    739 
    740 	return 0;
    741 }
    742 
    743 STATIC void
    744 mec_reset(struct mec_softc *sc)
    745 {
    746 	bus_space_tag_t st = sc->sc_st;
    747 	bus_space_handle_t sh = sc->sc_sh;
    748 	uint64_t control;
    749 
    750 	/* stop DMA first */
    751 	bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
    752 
    753 	/* reset chip */
    754 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, MEC_MAC_CORE_RESET);
    755 	delay(1000);
    756 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, 0);
    757 	delay(1000);
    758 
    759 	/* Default to 100/half and let auto-negotiation work its magic */
    760 	control = MEC_MAC_SPEED_SELECT | MEC_MAC_FILTER_MATCHMULTI |
    761 	    MEC_MAC_IPG_DEFAULT;
    762 
    763 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
    764 	/* stop DMA again for sanity */
    765 	bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
    766 
    767 	DPRINTF(MEC_DEBUG_RESET, ("mec: control now %llx\n",
    768 	    bus_space_read_8(st, sh, MEC_MAC_CONTROL)));
    769 }
    770 
    771 STATIC void
    772 mec_start(struct ifnet *ifp)
    773 {
    774 	struct mec_softc *sc = ifp->if_softc;
    775 	struct mbuf *m0, *m;
    776 	struct mec_txdesc *txd;
    777 	struct mec_txsoft *txs;
    778 	bus_dmamap_t dmamap;
    779 	bus_space_tag_t st = sc->sc_st;
    780 	bus_space_handle_t sh = sc->sc_sh;
    781 	uint64_t txdaddr;
    782 	int error, firsttx, nexttx, opending;
    783 	int len, bufoff, buflen, unaligned, txdlen;
    784 
    785 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    786 		return;
    787 
    788 	/*
    789 	 * Remember the previous txpending and the first transmit descriptor.
    790 	 */
    791 	opending = sc->sc_txpending;
    792 	firsttx = MEC_NEXTTX(sc->sc_txlast);
    793 
    794 	DPRINTF(MEC_DEBUG_START,
    795 	    ("mec_start: opending = %d, firsttx = %d\n", opending, firsttx));
    796 
    797 	for (;;) {
    798 		/* Grab a packet off the queue. */
    799 		IFQ_POLL(&ifp->if_snd, m0);
    800 		if (m0 == NULL)
    801 			break;
    802 		m = NULL;
    803 
    804 		if (sc->sc_txpending == MEC_NTXDESC) {
    805 			break;
    806 		}
    807 
    808 		/*
    809 		 * Get the next available transmit descriptor.
    810 		 */
    811 		nexttx = MEC_NEXTTX(sc->sc_txlast);
    812 		txd = &sc->sc_txdesc[nexttx];
    813 		txs = &sc->sc_txsoft[nexttx];
    814 
    815 		buflen = 0;
    816 		bufoff = 0;
    817 		txdaddr = 0; /* XXX gcc */
    818 		txdlen = 0; /* XXX gcc */
    819 
    820 		len = m0->m_pkthdr.len;
    821 
    822 		DPRINTF(MEC_DEBUG_START,
    823 		    ("mec_start: len = %d, nexttx = %d\n", len, nexttx));
    824 
    825 		if (len < ETHER_PAD_LEN) {
    826 			/*
    827 			 * I don't know if MEC chip does auto padding,
    828 			 * so if the packet is small enough,
    829 			 * just copy it to the buffer in txdesc.
    830 			 * Maybe this is the simple way.
    831 			 */
    832 			DPRINTF(MEC_DEBUG_START, ("mec_start: short packet\n"));
    833 
    834 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    835 			bufoff = MEC_TXD_BUFSTART(ETHER_PAD_LEN);
    836 			m_copydata(m0, 0, m0->m_pkthdr.len,
    837 			    txd->txd_buf + bufoff);
    838 			memset(txd->txd_buf + bufoff + len, 0,
    839 			    ETHER_PAD_LEN - len);
    840 			len = buflen = ETHER_PAD_LEN;
    841 
    842 			txs->txs_flags = MEC_TXS_TXDBUF | buflen;
    843 		} else {
    844 			/*
    845 			 * If the packet won't fit the buffer in txdesc,
    846 			 * we have to use concatenate pointer to handle it.
    847 			 * While MEC can handle up to three segments to
    848 			 * concatenate, MEC requires that both the second and
    849 			 * third segments have to be 8 byte aligned.
    850 			 * Since it's unlikely for mbuf clusters, we use
    851 			 * only the first concatenate pointer. If the packet
    852 			 * doesn't fit in one DMA segment, allocate new mbuf
    853 			 * and copy the packet to it.
    854 			 *
    855 			 * Besides, if the start address of the first segments
    856 			 * is not 8 byte aligned, such part have to be copied
    857 			 * to the txdesc buffer. (XXX see below comments)
    858 	                 */
    859 			DPRINTF(MEC_DEBUG_START, ("mec_start: long packet\n"));
    860 
    861 			dmamap = txs->txs_dmamap;
    862 			if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    863 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
    864 				DPRINTF(MEC_DEBUG_START,
    865 				    ("mec_start: re-allocating mbuf\n"));
    866 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    867 				if (m == NULL) {
    868 					printf("%s: unable to allocate "
    869 					    "TX mbuf\n", sc->sc_dev.dv_xname);
    870 					break;
    871 				}
    872 				if (len > (MHLEN - MEC_ETHER_ALIGN)) {
    873 					MCLGET(m, M_DONTWAIT);
    874 					if ((m->m_flags & M_EXT) == 0) {
    875 						printf("%s: unable to allocate "
    876 						    "TX cluster\n",
    877 						    sc->sc_dev.dv_xname);
    878 						m_freem(m);
    879 						break;
    880 					}
    881 				}
    882 				/*
    883 				 * Each packet has the Ethernet header, so
    884 				 * in many case the header isn't 4-byte aligned
    885 				 * and data after the header is 4-byte aligned.
    886 				 * Thus adding 2-byte offset before copying to
    887 				 * new mbuf avoids unaligned copy and this may
    888 				 * improve some performance.
    889 				 * As noted above, unaligned part has to be
    890 				 * copied to txdesc buffer so this may cause
    891 				 * extra copy ops, but for now MEC always
    892 				 * requires some data in txdesc buffer,
    893 				 * so we always have to copy some data anyway.
    894 				 */
    895 				m->m_data += MEC_ETHER_ALIGN;
    896 				m_copydata(m0, 0, len, mtod(m, void *));
    897 				m->m_pkthdr.len = m->m_len = len;
    898 				error = bus_dmamap_load_mbuf(sc->sc_dmat,
    899 				    dmamap, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    900 				if (error) {
    901 					printf("%s: unable to load TX buffer, "
    902 					    "error = %d\n",
    903 					    sc->sc_dev.dv_xname, error);
    904 					break;
    905 				}
    906 			}
    907 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    908 			if (m != NULL) {
    909 				m_freem(m0);
    910 				m0 = m;
    911 			}
    912 
    913 			/* handle unaligned part */
    914 			txdaddr = MEC_TXD_ROUNDUP(dmamap->dm_segs[0].ds_addr);
    915 			txs->txs_flags = MEC_TXS_TXDPTR1;
    916 			unaligned =
    917 			    dmamap->dm_segs[0].ds_addr & (MEC_TXD_ALIGN - 1);
    918 			DPRINTF(MEC_DEBUG_START,
    919 			    ("mec_start: ds_addr = 0x%08x, unaligned = %d\n",
    920 			    (u_int)dmamap->dm_segs[0].ds_addr, unaligned));
    921 			if (unaligned != 0) {
    922 				buflen = MEC_TXD_ALIGN - unaligned;
    923 				bufoff = MEC_TXD_BUFSTART(buflen);
    924 				DPRINTF(MEC_DEBUG_START,
    925 				    ("mec_start: unaligned, "
    926 				    "buflen = %d, bufoff = %d\n",
    927 				    buflen, bufoff));
    928 				memcpy(txd->txd_buf + bufoff,
    929 				    mtod(m0, void *), buflen);
    930 				txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
    931 			}
    932 #if 1
    933 			else {
    934 				/*
    935 				 * XXX needs hardware info XXX
    936 				 * It seems MEC always requires some data
    937 				 * in txd_buf[] even if buffer is
    938 				 * 8-byte aligned otherwise DMA abort error
    939 				 * occurs later...
    940 				 */
    941 				buflen = MEC_TXD_ALIGN;
    942 				bufoff = MEC_TXD_BUFSTART(buflen);
    943 				memcpy(txd->txd_buf + bufoff,
    944 				    mtod(m0, void *), buflen);
    945 				DPRINTF(MEC_DEBUG_START,
    946 				    ("mec_start: aligned, "
    947 				    "buflen = %d, bufoff = %d\n",
    948 				    buflen, bufoff));
    949 				txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
    950 				txdaddr += MEC_TXD_ALIGN;
    951 			}
    952 #endif
    953 			txdlen  = len - buflen;
    954 			DPRINTF(MEC_DEBUG_START,
    955 			    ("mec_start: txdaddr = 0x%08llx, txdlen = %d\n",
    956 			    txdaddr, txdlen));
    957 
    958 			/*
    959 			 * sync the DMA map for TX mbuf
    960 			 *
    961 			 * XXX unaligned part doesn't have to be sync'ed,
    962 			 *     but it's harmless...
    963 			 */
    964 			bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
    965 			    dmamap->dm_mapsize,	BUS_DMASYNC_PREWRITE);
    966 		}
    967 
    968 #if NBPFILTER > 0
    969 		/*
    970 		 * Pass packet to bpf if there is a listener.
    971 		 */
    972 		if (ifp->if_bpf)
    973 			bpf_mtap(ifp->if_bpf, m0);
    974 #endif
    975 
    976 		/*
    977 		 * setup the transmit descriptor.
    978 		 */
    979 
    980 		/* TXINT bit will be set later on the last packet */
    981 		txd->txd_cmd = (len - 1);
    982 		/* but also set TXINT bit on a half of TXDESC */
    983 		if (sc->sc_txpending == (MEC_NTXDESC / 2))
    984 			txd->txd_cmd |= MEC_TXCMD_TXINT;
    985 
    986 		if (txs->txs_flags & MEC_TXS_TXDBUF)
    987 			txd->txd_cmd |= TXCMD_BUFSTART(MEC_TXDESCSIZE - buflen);
    988 		if (txs->txs_flags & MEC_TXS_TXDPTR1) {
    989 			txd->txd_cmd |= MEC_TXCMD_PTR1;
    990 			txd->txd_ptr[0] = TXPTR_LEN(txdlen - 1) | txdaddr;
    991 			/*
    992 			 * Store a pointer to the packet so we can
    993 			 * free it later.
    994 			 */
    995 			txs->txs_mbuf = m0;
    996 		} else {
    997 			txd->txd_ptr[0] = 0;
    998 			/*
    999 			 * In this case all data are copied to buffer in txdesc,
   1000 			 * we can free TX mbuf here.
   1001 			 */
   1002 			m_freem(m0);
   1003 		}
   1004 
   1005 		DPRINTF(MEC_DEBUG_START,
   1006 		    ("mec_start: txd_cmd = 0x%016llx, txd_ptr = 0x%016llx\n",
   1007 		    txd->txd_cmd, txd->txd_ptr[0]));
   1008 		DPRINTF(MEC_DEBUG_START,
   1009 		    ("mec_start: len = %d (0x%04x), buflen = %d (0x%02x)\n",
   1010 		    len, len, buflen, buflen));
   1011 
   1012 		/* sync TX descriptor */
   1013 		MEC_TXDESCSYNC(sc, nexttx,
   1014 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1015 
   1016 		/* advance the TX pointer. */
   1017 		sc->sc_txpending++;
   1018 		sc->sc_txlast = nexttx;
   1019 	}
   1020 
   1021 	if (sc->sc_txpending == MEC_NTXDESC) {
   1022 		/* No more slots; notify upper layer. */
   1023 		ifp->if_flags |= IFF_OACTIVE;
   1024 	}
   1025 
   1026 	if (sc->sc_txpending != opending) {
   1027 		/*
   1028 		 * Cause a TX interrupt to happen on the last packet
   1029 		 * we enqueued.
   1030 		 */
   1031 		sc->sc_txdesc[sc->sc_txlast].txd_cmd |= MEC_TXCMD_TXINT;
   1032 		MEC_TXCMDSYNC(sc, sc->sc_txlast,
   1033 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1034 
   1035 		/* start TX */
   1036 		bus_space_write_8(st, sh, MEC_TX_RING_PTR,
   1037 		    MEC_NEXTTX(sc->sc_txlast));
   1038 
   1039 		/*
   1040 		 * If the transmitter was idle,
   1041 		 * reset the txdirty pointer and re-enable TX interrupt.
   1042 		 */
   1043 		if (opending == 0) {
   1044 			sc->sc_txdirty = firsttx;
   1045 			bus_space_write_8(st, sh, MEC_TX_ALIAS,
   1046 			    MEC_TX_ALIAS_INT_ENABLE);
   1047 		}
   1048 
   1049 		/* Set a watchdog timer in case the chip flakes out. */
   1050 		ifp->if_timer = 5;
   1051 	}
   1052 }
   1053 
   1054 STATIC void
   1055 mec_stop(struct ifnet *ifp, int disable)
   1056 {
   1057 	struct mec_softc *sc = ifp->if_softc;
   1058 	struct mec_txsoft *txs;
   1059 	int i;
   1060 
   1061 	DPRINTF(MEC_DEBUG_STOP, ("mec_stop\n"));
   1062 
   1063 	ifp->if_timer = 0;
   1064 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1065 
   1066 	callout_stop(&sc->sc_tick_ch);
   1067 	mii_down(&sc->sc_mii);
   1068 
   1069 	/* release any TX buffers */
   1070 	for (i = 0; i < MEC_NTXDESC; i++) {
   1071 		txs = &sc->sc_txsoft[i];
   1072 		if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
   1073 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1074 			m_freem(txs->txs_mbuf);
   1075 			txs->txs_mbuf = NULL;
   1076 		}
   1077 	}
   1078 }
   1079 
   1080 STATIC int
   1081 mec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1082 {
   1083 	int s, error;
   1084 
   1085 	s = splnet();
   1086 
   1087 	error = ether_ioctl(ifp, cmd, data);
   1088 	if (error == ENETRESET) {
   1089 		/*
   1090 		 * Multicast list has changed; set the hardware filter
   1091 		 * accordingly.
   1092 		 */
   1093 		if (ifp->if_flags & IFF_RUNNING)
   1094 			error = mec_init(ifp);
   1095 		else
   1096 			error = 0;
   1097 	}
   1098 
   1099 	/* Try to get more packets going. */
   1100 	mec_start(ifp);
   1101 
   1102 	splx(s);
   1103 	return error;
   1104 }
   1105 
   1106 STATIC void
   1107 mec_watchdog(struct ifnet *ifp)
   1108 {
   1109 	struct mec_softc *sc = ifp->if_softc;
   1110 
   1111 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1112 	ifp->if_oerrors++;
   1113 
   1114 	mec_init(ifp);
   1115 }
   1116 
   1117 STATIC void
   1118 mec_tick(void *arg)
   1119 {
   1120 	struct mec_softc *sc = arg;
   1121 	int s;
   1122 
   1123 	s = splnet();
   1124 	mii_tick(&sc->sc_mii);
   1125 	splx(s);
   1126 
   1127 	callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
   1128 }
   1129 
   1130 STATIC void
   1131 mec_setfilter(struct mec_softc *sc)
   1132 {
   1133 	struct ethercom *ec = &sc->sc_ethercom;
   1134 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1135 	struct ether_multi *enm;
   1136 	struct ether_multistep step;
   1137 	bus_space_tag_t st = sc->sc_st;
   1138 	bus_space_handle_t sh = sc->sc_sh;
   1139 	uint64_t mchash;
   1140 	uint32_t control, hash;
   1141 	int mcnt;
   1142 
   1143 	control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
   1144 	control &= ~MEC_MAC_FILTER_MASK;
   1145 
   1146 	if (ifp->if_flags & IFF_PROMISC) {
   1147 		control |= MEC_MAC_FILTER_PROMISC;
   1148 		bus_space_write_8(st, sh, MEC_MULTICAST, 0xffffffffffffffffULL);
   1149 		bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1150 		return;
   1151 	}
   1152 
   1153 	mcnt = 0;
   1154 	mchash = 0;
   1155 	ETHER_FIRST_MULTI(step, ec, enm);
   1156 	while (enm != NULL) {
   1157 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1158 			/* set allmulti for a range of multicast addresses */
   1159 			control |= MEC_MAC_FILTER_ALLMULTI;
   1160 			bus_space_write_8(st, sh, MEC_MULTICAST,
   1161 			    0xffffffffffffffffULL);
   1162 			bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1163 			return;
   1164 		}
   1165 
   1166 #define mec_calchash(addr)	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
   1167 
   1168 		hash = mec_calchash(enm->enm_addrlo);
   1169 		mchash |= 1 << hash;
   1170 		mcnt++;
   1171 		ETHER_NEXT_MULTI(step, enm);
   1172 	}
   1173 
   1174 	ifp->if_flags &= ~IFF_ALLMULTI;
   1175 
   1176 	if (mcnt > 0)
   1177 		control |= MEC_MAC_FILTER_MATCHMULTI;
   1178 
   1179 	bus_space_write_8(st, sh, MEC_MULTICAST, mchash);
   1180 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1181 }
   1182 
   1183 STATIC int
   1184 mec_intr(void *arg)
   1185 {
   1186 	struct mec_softc *sc = arg;
   1187 	bus_space_tag_t st = sc->sc_st;
   1188 	bus_space_handle_t sh = sc->sc_sh;
   1189 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1190 	uint32_t statreg, statack, dmac;
   1191 	int handled, sent;
   1192 
   1193 	DPRINTF(MEC_DEBUG_INTR, ("mec_intr: called\n"));
   1194 
   1195 	handled = sent = 0;
   1196 
   1197 	for (;;) {
   1198 		statreg = bus_space_read_8(st, sh, MEC_INT_STATUS);
   1199 
   1200 		DPRINTF(MEC_DEBUG_INTR,
   1201 		    ("mec_intr: INT_STAT = 0x%08x\n", statreg));
   1202 
   1203 		statack = statreg & MEC_INT_STATUS_MASK;
   1204 		if (statack == 0)
   1205 			break;
   1206 		bus_space_write_8(st, sh, MEC_INT_STATUS, statack);
   1207 
   1208 		handled = 1;
   1209 
   1210 		if (statack &
   1211 		    (MEC_INT_RX_THRESHOLD |
   1212 		     MEC_INT_RX_FIFO_UNDERFLOW)) {
   1213 			mec_rxintr(sc);
   1214 		}
   1215 
   1216 		dmac = bus_space_read_8(st, sh, MEC_DMA_CONTROL);
   1217 		DPRINTF(MEC_DEBUG_INTR,
   1218 		    ("mec_intr: DMA_CONT = 0x%08x\n", dmac));
   1219 
   1220 		if (statack &
   1221 		    (MEC_INT_TX_EMPTY |
   1222 		     MEC_INT_TX_PACKET_SENT |
   1223 		     MEC_INT_TX_ABORT)) {
   1224 			mec_txintr(sc);
   1225 			sent = 1;
   1226 			if ((statack & MEC_INT_TX_EMPTY) != 0 &&
   1227 			    (dmac & MEC_DMA_TX_INT_ENABLE) != 0) {
   1228 				/*
   1229 				 * disable TX interrupt to stop
   1230 				 * TX empty interrupt
   1231 				 */
   1232 				bus_space_write_8(st, sh, MEC_TX_ALIAS, 0);
   1233 				DPRINTF(MEC_DEBUG_INTR,
   1234 				    ("mec_intr: disable TX_INT\n"));
   1235 			}
   1236 		}
   1237 
   1238 		if (statack &
   1239 		    (MEC_INT_TX_LINK_FAIL |
   1240 		     MEC_INT_TX_MEM_ERROR |
   1241 		     MEC_INT_TX_ABORT |
   1242 		     MEC_INT_RX_FIFO_UNDERFLOW |
   1243 		     MEC_INT_RX_DMA_UNDERFLOW)) {
   1244 			printf("%s: mec_intr: interrupt status = 0x%08x\n",
   1245 			    sc->sc_dev.dv_xname, statreg);
   1246 		}
   1247 	}
   1248 
   1249 	if (sent) {
   1250 		/* try to get more packets going */
   1251 		mec_start(ifp);
   1252 	}
   1253 
   1254 #if NRND > 0
   1255 	if (handled)
   1256 		rnd_add_uint32(&sc->sc_rnd_source, statreg);
   1257 #endif
   1258 
   1259 	return handled;
   1260 }
   1261 
   1262 STATIC void
   1263 mec_rxintr(struct mec_softc *sc)
   1264 {
   1265 	bus_space_tag_t st = sc->sc_st;
   1266 	bus_space_handle_t sh = sc->sc_sh;
   1267 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1268 	struct mbuf *m;
   1269 	struct mec_rxdesc *rxd;
   1270 	uint64_t rxstat;
   1271 	u_int len;
   1272 	int i;
   1273 
   1274 	DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: called\n"));
   1275 
   1276 	for (i = sc->sc_rxptr;; i = MEC_NEXTRX(i)) {
   1277 		rxd = &sc->sc_rxdesc[i];
   1278 
   1279 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_POSTREAD);
   1280 		rxstat = rxd->rxd_stat;
   1281 
   1282 		DPRINTF(MEC_DEBUG_RXINTR,
   1283 		    ("mec_rxintr: rxstat = 0x%016llx, rxptr = %d\n",
   1284 		    rxstat, i));
   1285 		DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: rxfifo = 0x%08x\n",
   1286 		    (u_int)bus_space_read_8(st, sh, MEC_RX_FIFO)));
   1287 
   1288 		if ((rxstat & MEC_RXSTAT_RECEIVED) == 0) {
   1289 			MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1290 			break;
   1291 		}
   1292 
   1293 		len = rxstat & MEC_RXSTAT_LEN;
   1294 
   1295 		if (len < ETHER_MIN_LEN ||
   1296 		    len > (MCLBYTES - MEC_ETHER_ALIGN)) {
   1297 			/* invalid length packet; drop it. */
   1298 			DPRINTF(MEC_DEBUG_RXINTR,
   1299 			    ("mec_rxintr: wrong packet\n"));
   1300  dropit:
   1301 			ifp->if_ierrors++;
   1302 			rxd->rxd_stat = 0;
   1303 			MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1304 			bus_space_write_8(st, sh, MEC_MCL_RX_FIFO,
   1305 			    MEC_CDRXADDR(sc, i));
   1306 			continue;
   1307 		}
   1308 
   1309 		if (rxstat &
   1310 		    (MEC_RXSTAT_BADPACKET |
   1311 		     MEC_RXSTAT_LONGEVENT |
   1312 		     MEC_RXSTAT_INVALID   |
   1313 		     MEC_RXSTAT_CRCERROR  |
   1314 		     MEC_RXSTAT_VIOLATION)) {
   1315 			printf("%s: mec_rxintr: status = 0x%016llx\n",
   1316 			    sc->sc_dev.dv_xname, rxstat);
   1317 			goto dropit;
   1318 		}
   1319 
   1320 		/*
   1321 		 * The MEC includes the CRC with every packet.  Trim
   1322 		 * it off here.
   1323 		 */
   1324 		len -= ETHER_CRC_LEN;
   1325 
   1326 		/*
   1327 		 * now allocate an mbuf (and possibly a cluster) to hold
   1328 		 * the received packet.
   1329 		 */
   1330 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1331 		if (m == NULL) {
   1332 			printf("%s: unable to allocate RX mbuf\n",
   1333 			    sc->sc_dev.dv_xname);
   1334 			goto dropit;
   1335 		}
   1336 		if (len > (MHLEN - MEC_ETHER_ALIGN)) {
   1337 			MCLGET(m, M_DONTWAIT);
   1338 			if ((m->m_flags & M_EXT) == 0) {
   1339 				printf("%s: unable to allocate RX cluster\n",
   1340 				    sc->sc_dev.dv_xname);
   1341 				m_freem(m);
   1342 				m = NULL;
   1343 				goto dropit;
   1344 			}
   1345 		}
   1346 
   1347 		/*
   1348 		 * Note MEC chip seems to insert 2 byte padding at the top of
   1349 		 * RX buffer, but we copy whole buffer to avoid unaligned copy.
   1350 		 */
   1351 		MEC_RXBUFSYNC(sc, i, len, BUS_DMASYNC_POSTREAD);
   1352 		memcpy(mtod(m, void *), rxd->rxd_buf, MEC_ETHER_ALIGN + len);
   1353 		MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
   1354 		m->m_data += MEC_ETHER_ALIGN;
   1355 
   1356 		/* put RX buffer into FIFO again */
   1357 		rxd->rxd_stat = 0;
   1358 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1359 		bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
   1360 
   1361 		m->m_pkthdr.rcvif = ifp;
   1362 		m->m_pkthdr.len = m->m_len = len;
   1363 
   1364 		ifp->if_ipackets++;
   1365 
   1366 #if NBPFILTER > 0
   1367 		/*
   1368 		 * Pass this up to any BPF listeners, but only
   1369 		 * pass it up the stack if it's for us.
   1370 		 */
   1371 		if (ifp->if_bpf)
   1372 			bpf_mtap(ifp->if_bpf, m);
   1373 #endif
   1374 
   1375 		/* Pass it on. */
   1376 		(*ifp->if_input)(ifp, m);
   1377 	}
   1378 
   1379 	/* update RX pointer */
   1380 	sc->sc_rxptr = i;
   1381 }
   1382 
   1383 STATIC void
   1384 mec_txintr(struct mec_softc *sc)
   1385 {
   1386 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1387 	struct mec_txdesc *txd;
   1388 	struct mec_txsoft *txs;
   1389 	bus_dmamap_t dmamap;
   1390 	uint64_t txstat;
   1391 	int i;
   1392 	u_int col;
   1393 
   1394 	ifp->if_flags &= ~IFF_OACTIVE;
   1395 
   1396 	DPRINTF(MEC_DEBUG_TXINTR, ("mec_txintr: called\n"));
   1397 
   1398 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
   1399 	    i = MEC_NEXTTX(i), sc->sc_txpending--) {
   1400 		txd = &sc->sc_txdesc[i];
   1401 
   1402 		MEC_TXDESCSYNC(sc, i,
   1403 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1404 
   1405 		txstat = txd->txd_stat;
   1406 		DPRINTF(MEC_DEBUG_TXINTR,
   1407 		    ("mec_txintr: dirty = %d, txstat = 0x%016llx\n",
   1408 		    i, txstat));
   1409 		if ((txstat & MEC_TXSTAT_SENT) == 0) {
   1410 			MEC_TXCMDSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1411 			break;
   1412 		}
   1413 
   1414 		if ((txstat & MEC_TXSTAT_SUCCESS) == 0) {
   1415 			printf("%s: TX error: txstat = 0x%016llx\n",
   1416 			    sc->sc_dev.dv_xname, txstat);
   1417 			ifp->if_oerrors++;
   1418 			continue;
   1419 		}
   1420 
   1421 		txs = &sc->sc_txsoft[i];
   1422 		if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
   1423 			dmamap = txs->txs_dmamap;
   1424 			bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
   1425 			    dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1426 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   1427 			m_freem(txs->txs_mbuf);
   1428 			txs->txs_mbuf = NULL;
   1429 		}
   1430 
   1431 		col = (txstat & MEC_TXSTAT_COLCNT) >> MEC_TXSTAT_COLCNT_SHIFT;
   1432 		ifp->if_collisions += col;
   1433 		ifp->if_opackets++;
   1434 	}
   1435 
   1436 	/* update the dirty TX buffer pointer */
   1437 	sc->sc_txdirty = i;
   1438 	DPRINTF(MEC_DEBUG_INTR,
   1439 	    ("mec_txintr: sc_txdirty = %2d, sc_txpending = %2d\n",
   1440 	    sc->sc_txdirty, sc->sc_txpending));
   1441 
   1442 	/* cancel the watchdog timer if there are no pending TX packets */
   1443 	if (sc->sc_txpending == 0)
   1444 		ifp->if_timer = 0;
   1445 }
   1446 
   1447 STATIC void
   1448 mec_shutdown(void *arg)
   1449 {
   1450 	struct mec_softc *sc = arg;
   1451 
   1452 	mec_stop(&sc->sc_ethercom.ec_if, 1);
   1453 	/* make sure to stop DMA etc. */
   1454 	mec_reset(sc);
   1455 }
   1456