Home | History | Annotate | Line # | Download | only in mace
if_mec.c revision 1.15
      1 /* $NetBSD: if_mec.c,v 1.15 2008/01/19 22:10:16 dyoung 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.15 2008/01/19 22:10:16 dyoung 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 	uint32_t 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 	/* reset device */
    458 	mec_reset(sc);
    459 
    460 	command = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_MAC_CONTROL);
    461 
    462 	printf(": MAC-110 Ethernet, rev %d\n",
    463 	    (command & MEC_MAC_REVISION) >> MEC_MAC_REVISION_SHIFT);
    464 
    465 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    466 	    ether_sprintf(sc->sc_enaddr));
    467 
    468 	/* Done, now attach everything */
    469 
    470 	sc->sc_mii.mii_ifp = ifp;
    471 	sc->sc_mii.mii_readreg = mec_mii_readreg;
    472 	sc->sc_mii.mii_writereg = mec_mii_writereg;
    473 	sc->sc_mii.mii_statchg = mec_statchg;
    474 
    475 	/* Set up PHY properties */
    476 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    477 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
    478 	    ether_mediastatus);
    479 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    480 	    MII_OFFSET_ANY, 0);
    481 
    482 	child = LIST_FIRST(&sc->sc_mii.mii_phys);
    483 	if (child == NULL) {
    484 		/* No PHY attached */
    485 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
    486 		    0, NULL);
    487 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
    488 	} else {
    489 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    490 		sc->sc_phyaddr = child->mii_phy;
    491 	}
    492 
    493 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    494 	ifp->if_softc = sc;
    495 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    496 	ifp->if_ioctl = mec_ioctl;
    497 	ifp->if_start = mec_start;
    498 	ifp->if_watchdog = mec_watchdog;
    499 	ifp->if_init = mec_init;
    500 	ifp->if_stop = mec_stop;
    501 	ifp->if_mtu = ETHERMTU;
    502 	IFQ_SET_READY(&ifp->if_snd);
    503 
    504 	if_attach(ifp);
    505 	ether_ifattach(ifp, sc->sc_enaddr);
    506 
    507 	/* establish interrupt */
    508 	cpu_intr_establish(maa->maa_intr, maa->maa_intrmask, mec_intr, sc);
    509 
    510 #if NRND > 0
    511 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
    512 	    RND_TYPE_NET, 0);
    513 #endif
    514 
    515 	/* set shutdown hook to reset interface on powerdown */
    516 	sc->sc_sdhook = shutdownhook_establish(mec_shutdown, sc);
    517 
    518 	return;
    519 
    520 	/*
    521 	 * Free any resources we've allocated during the failed attach
    522 	 * attempt.  Do this in reverse order and fall though.
    523 	 */
    524  fail_4:
    525 	for (i = 0; i < MEC_NTXDESC; i++) {
    526 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    527 			bus_dmamap_destroy(sc->sc_dmat,
    528 			    sc->sc_txsoft[i].txs_dmamap);
    529 	}
    530 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    531  fail_3:
    532 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    533  fail_2:
    534 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    535 	    sizeof(struct mec_control_data));
    536  fail_1:
    537 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    538  fail_0:
    539 	return;
    540 }
    541 
    542 STATIC int
    543 mec_mii_readreg(struct device *self, int phy, int reg)
    544 {
    545 	struct mec_softc *sc = (void *)self;
    546 	bus_space_tag_t st = sc->sc_st;
    547 	bus_space_handle_t sh = sc->sc_sh;
    548 	uint64_t val;
    549 	int i;
    550 
    551 	if (mec_mii_wait(sc) != 0)
    552 		return 0;
    553 
    554 	bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
    555 	    (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
    556 	delay(25);
    557 	bus_space_write_8(st, sh, MEC_PHY_READ_INITIATE, 1);
    558 	delay(25);
    559 	mec_mii_wait(sc);
    560 
    561 	for (i = 0; i < 20; i++) {
    562 		delay(30);
    563 
    564 		val = bus_space_read_8(st, sh, MEC_PHY_DATA);
    565 
    566 		if ((val & MEC_PHY_DATA_BUSY) == 0)
    567 			return val & MEC_PHY_DATA_VALUE;
    568 	}
    569 	return 0;
    570 }
    571 
    572 STATIC void
    573 mec_mii_writereg(struct device *self, int phy, int reg, int val)
    574 {
    575 	struct mec_softc *sc = (void *)self;
    576 	bus_space_tag_t st = sc->sc_st;
    577 	bus_space_handle_t sh = sc->sc_sh;
    578 
    579 	if (mec_mii_wait(sc) != 0) {
    580 		printf("timed out writing %x: %x\n", reg, val);
    581 		return;
    582 	}
    583 
    584 	bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
    585 	    (phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
    586 
    587 	delay(60);
    588 
    589 	bus_space_write_8(st, sh, MEC_PHY_DATA, val & MEC_PHY_DATA_VALUE);
    590 
    591 	delay(60);
    592 
    593 	mec_mii_wait(sc);
    594 }
    595 
    596 STATIC int
    597 mec_mii_wait(struct mec_softc *sc)
    598 {
    599 	uint32_t busy;
    600 	int i, s;
    601 
    602 	for (i = 0; i < 100; i++) {
    603 		delay(30);
    604 
    605 		s = splhigh();
    606 		busy = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_PHY_DATA);
    607 		splx(s);
    608 
    609 		if ((busy & MEC_PHY_DATA_BUSY) == 0)
    610 			return 0;
    611 #if 0
    612 		if (busy == 0xffff) /* XXX ? */
    613 			return 0;
    614 #endif
    615 	}
    616 
    617 	printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
    618 	return 1;
    619 }
    620 
    621 STATIC void
    622 mec_statchg(struct device *self)
    623 {
    624 	struct mec_softc *sc = (void *)self;
    625 	bus_space_tag_t st = sc->sc_st;
    626 	bus_space_handle_t sh = sc->sc_sh;
    627 	uint32_t control;
    628 
    629 	control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
    630 	control &= ~(MEC_MAC_IPGT | MEC_MAC_IPGR1 | MEC_MAC_IPGR2 |
    631 	    MEC_MAC_FULL_DUPLEX | MEC_MAC_SPEED_SELECT);
    632 
    633 	/* must also set IPG here for duplex stuff ... */
    634 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) {
    635 		control |= MEC_MAC_FULL_DUPLEX;
    636 	} else {
    637 		/* set IPG */
    638 		control |= MEC_MAC_IPG_DEFAULT;
    639 	}
    640 
    641 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
    642 }
    643 
    644 /*
    645  * XXX
    646  * maybe this function should be moved to common part
    647  * (sgimips/machdep.c or elsewhere) for all on-board network devices.
    648  */
    649 static void
    650 enaddr_aton(const char *str, uint8_t *eaddr)
    651 {
    652 	int i;
    653 	char c;
    654 
    655 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    656 		if (*str == ':')
    657 			str++;
    658 
    659 		c = *str++;
    660 		if (isdigit(c)) {
    661 			eaddr[i] = (c - '0');
    662 		} else if (isxdigit(c)) {
    663 			eaddr[i] = (toupper(c) + 10 - 'A');
    664 		}
    665 		c = *str++;
    666 		if (isdigit(c)) {
    667 			eaddr[i] = (eaddr[i] << 4) | (c - '0');
    668 		} else if (isxdigit(c)) {
    669 			eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
    670 		}
    671 	}
    672 }
    673 
    674 STATIC int
    675 mec_init(struct ifnet *ifp)
    676 {
    677 	struct mec_softc *sc = ifp->if_softc;
    678 	bus_space_tag_t st = sc->sc_st;
    679 	bus_space_handle_t sh = sc->sc_sh;
    680 	struct mec_rxdesc *rxd;
    681 	int i, rc;
    682 
    683 	/* cancel any pending I/O */
    684 	mec_stop(ifp, 0);
    685 
    686 	/* reset device */
    687 	mec_reset(sc);
    688 
    689 	/* setup filter for multicast or promisc mode */
    690 	mec_setfilter(sc);
    691 
    692 	/* set the TX ring pointer to the base address */
    693 	bus_space_write_8(st, sh, MEC_TX_RING_BASE, MEC_CDTXADDR(sc, 0));
    694 
    695 	sc->sc_txpending = 0;
    696 	sc->sc_txdirty = 0;
    697 	sc->sc_txlast = MEC_NTXDESC - 1;
    698 
    699 	/* put RX buffers into FIFO */
    700 	for (i = 0; i < MEC_NRXDESC; i++) {
    701 		rxd = &sc->sc_rxdesc[i];
    702 		rxd->rxd_stat = 0;
    703 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
    704 		MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
    705 		bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
    706 	}
    707 	sc->sc_rxptr = 0;
    708 
    709 #if 0	/* XXX no info */
    710 	bus_space_write_8(st, sh, MEC_TIMER, 0);
    711 #endif
    712 
    713 	/*
    714 	 * MEC_DMA_TX_INT_ENABLE will be set later otherwise it causes
    715 	 * spurious interrupts when TX buffers are empty
    716 	 */
    717 	bus_space_write_8(st, sh, MEC_DMA_CONTROL,
    718 	    (MEC_RXD_DMAOFFSET << MEC_DMA_RX_DMA_OFFSET_SHIFT) |
    719 	    (MEC_NRXDESC << MEC_DMA_RX_INT_THRESH_SHIFT) |
    720 	    MEC_DMA_TX_DMA_ENABLE | /* MEC_DMA_TX_INT_ENABLE | */
    721 	    MEC_DMA_RX_DMA_ENABLE | MEC_DMA_RX_INT_ENABLE);
    722 
    723 	callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
    724 
    725 	if ((rc = ether_mediachange(ifp)) != 0)
    726 		return rc;
    727 
    728 	ifp->if_flags |= IFF_RUNNING;
    729 	ifp->if_flags &= ~IFF_OACTIVE;
    730 	mec_start(ifp);
    731 
    732 	return 0;
    733 }
    734 
    735 STATIC void
    736 mec_reset(struct mec_softc *sc)
    737 {
    738 	bus_space_tag_t st = sc->sc_st;
    739 	bus_space_handle_t sh = sc->sc_sh;
    740 	uint64_t address, control;
    741 	int i;
    742 
    743 	/* reset chip */
    744 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, MEC_MAC_CORE_RESET);
    745 	delay(1000);
    746 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, 0);
    747 	delay(1000);
    748 
    749 	/* set Ethernet address */
    750 	address = 0;
    751 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    752 		address = address << 8;
    753 		address += sc->sc_enaddr[i];
    754 	}
    755 	bus_space_write_8(st, sh, MEC_STATION, address);
    756 
    757 	/* Default to 100/half and let auto-negotiation work its magic */
    758 	control = MEC_MAC_SPEED_SELECT | MEC_MAC_FILTER_MATCHMULTI |
    759 	    MEC_MAC_IPG_DEFAULT;
    760 
    761 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
    762 	bus_space_write_8(st, sh, MEC_DMA_CONTROL, 0);
    763 
    764 	DPRINTF(MEC_DEBUG_RESET, ("mec: control now %llx\n",
    765 	    bus_space_read_8(st, sh, MEC_MAC_CONTROL)));
    766 }
    767 
    768 STATIC void
    769 mec_start(struct ifnet *ifp)
    770 {
    771 	struct mec_softc *sc = ifp->if_softc;
    772 	struct mbuf *m0, *m;
    773 	struct mec_txdesc *txd;
    774 	struct mec_txsoft *txs;
    775 	bus_dmamap_t dmamap;
    776 	bus_space_tag_t st = sc->sc_st;
    777 	bus_space_handle_t sh = sc->sc_sh;
    778 	uint64_t txdaddr;
    779 	int error, firsttx, nexttx, opending;
    780 	int len, bufoff, buflen, unaligned, txdlen;
    781 
    782 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    783 		return;
    784 
    785 	/*
    786 	 * Remember the previous txpending and the first transmit descriptor.
    787 	 */
    788 	opending = sc->sc_txpending;
    789 	firsttx = MEC_NEXTTX(sc->sc_txlast);
    790 
    791 	DPRINTF(MEC_DEBUG_START,
    792 	    ("mec_start: opending = %d, firsttx = %d\n", opending, firsttx));
    793 
    794 	for (;;) {
    795 		/* Grab a packet off the queue. */
    796 		IFQ_POLL(&ifp->if_snd, m0);
    797 		if (m0 == NULL)
    798 			break;
    799 		m = NULL;
    800 
    801 		if (sc->sc_txpending == MEC_NTXDESC) {
    802 			break;
    803 		}
    804 
    805 		/*
    806 		 * Get the next available transmit descriptor.
    807 		 */
    808 		nexttx = MEC_NEXTTX(sc->sc_txlast);
    809 		txd = &sc->sc_txdesc[nexttx];
    810 		txs = &sc->sc_txsoft[nexttx];
    811 
    812 		buflen = 0;
    813 		bufoff = 0;
    814 		txdaddr = 0; /* XXX gcc */
    815 		txdlen = 0; /* XXX gcc */
    816 
    817 		len = m0->m_pkthdr.len;
    818 
    819 		DPRINTF(MEC_DEBUG_START,
    820 		    ("mec_start: len = %d, nexttx = %d\n", len, nexttx));
    821 
    822 		if (len < ETHER_PAD_LEN) {
    823 			/*
    824 			 * I don't know if MEC chip does auto padding,
    825 			 * so if the packet is small enough,
    826 			 * just copy it to the buffer in txdesc.
    827 			 * Maybe this is the simple way.
    828 			 */
    829 			DPRINTF(MEC_DEBUG_START, ("mec_start: short packet\n"));
    830 
    831 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    832 			bufoff = MEC_TXD_BUFSTART(ETHER_PAD_LEN);
    833 			m_copydata(m0, 0, m0->m_pkthdr.len,
    834 			    txd->txd_buf + bufoff);
    835 			memset(txd->txd_buf + bufoff + len, 0,
    836 			    ETHER_PAD_LEN - len);
    837 			len = buflen = ETHER_PAD_LEN;
    838 
    839 			txs->txs_flags = MEC_TXS_TXDBUF | buflen;
    840 		} else {
    841 			/*
    842 			 * If the packet won't fit the buffer in txdesc,
    843 			 * we have to use concatenate pointer to handle it.
    844 			 * While MEC can handle up to three segments to
    845 			 * concatenate, MEC requires that both the second and
    846 			 * third segments have to be 8 byte aligned.
    847 			 * Since it's unlikely for mbuf clusters, we use
    848 			 * only the first concatenate pointer. If the packet
    849 			 * doesn't fit in one DMA segment, allocate new mbuf
    850 			 * and copy the packet to it.
    851 			 *
    852 			 * Besides, if the start address of the first segments
    853 			 * is not 8 byte aligned, such part have to be copied
    854 			 * to the txdesc buffer. (XXX see below comments)
    855 	                 */
    856 			DPRINTF(MEC_DEBUG_START, ("mec_start: long packet\n"));
    857 
    858 			dmamap = txs->txs_dmamap;
    859 			if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    860 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
    861 				DPRINTF(MEC_DEBUG_START,
    862 				    ("mec_start: re-allocating mbuf\n"));
    863 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    864 				if (m == NULL) {
    865 					printf("%s: unable to allocate "
    866 					    "TX mbuf\n", sc->sc_dev.dv_xname);
    867 					break;
    868 				}
    869 				if (len > (MHLEN - MEC_ETHER_ALIGN)) {
    870 					MCLGET(m, M_DONTWAIT);
    871 					if ((m->m_flags & M_EXT) == 0) {
    872 						printf("%s: unable to allocate "
    873 						    "TX cluster\n",
    874 						    sc->sc_dev.dv_xname);
    875 						m_freem(m);
    876 						break;
    877 					}
    878 				}
    879 				/*
    880 				 * Each packet has the Ethernet header, so
    881 				 * in many case the header isn't 4-byte aligned
    882 				 * and data after the header is 4-byte aligned.
    883 				 * Thus adding 2-byte offset before copying to
    884 				 * new mbuf avoids unaligned copy and this may
    885 				 * improve some performance.
    886 				 * As noted above, unaligned part has to be
    887 				 * copied to txdesc buffer so this may cause
    888 				 * extra copy ops, but for now MEC always
    889 				 * requires some data in txdesc buffer,
    890 				 * so we always have to copy some data anyway.
    891 				 */
    892 				m->m_data += MEC_ETHER_ALIGN;
    893 				m_copydata(m0, 0, len, mtod(m, void *));
    894 				m->m_pkthdr.len = m->m_len = len;
    895 				error = bus_dmamap_load_mbuf(sc->sc_dmat,
    896 				    dmamap, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    897 				if (error) {
    898 					printf("%s: unable to load TX buffer, "
    899 					    "error = %d\n",
    900 					    sc->sc_dev.dv_xname, error);
    901 					break;
    902 				}
    903 			}
    904 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    905 			if (m != NULL) {
    906 				m_freem(m0);
    907 				m0 = m;
    908 			}
    909 
    910 			/* handle unaligned part */
    911 			txdaddr = MEC_TXD_ROUNDUP(dmamap->dm_segs[0].ds_addr);
    912 			txs->txs_flags = MEC_TXS_TXDPTR1;
    913 			unaligned =
    914 			    dmamap->dm_segs[0].ds_addr & (MEC_TXD_ALIGN - 1);
    915 			DPRINTF(MEC_DEBUG_START,
    916 			    ("mec_start: ds_addr = 0x%08x, unaligned = %d\n",
    917 			    (u_int)dmamap->dm_segs[0].ds_addr, unaligned));
    918 			if (unaligned != 0) {
    919 				buflen = MEC_TXD_ALIGN - unaligned;
    920 				bufoff = MEC_TXD_BUFSTART(buflen);
    921 				DPRINTF(MEC_DEBUG_START,
    922 				    ("mec_start: unaligned, "
    923 				    "buflen = %d, bufoff = %d\n",
    924 				    buflen, bufoff));
    925 				memcpy(txd->txd_buf + bufoff,
    926 				    mtod(m0, void *), buflen);
    927 				txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
    928 			}
    929 #if 1
    930 			else {
    931 				/*
    932 				 * XXX needs hardware info XXX
    933 				 * It seems MEC always requires some data
    934 				 * in txd_buf[] even if buffer is
    935 				 * 8-byte aligned otherwise DMA abort error
    936 				 * occurs later...
    937 				 */
    938 				buflen = MEC_TXD_ALIGN;
    939 				bufoff = MEC_TXD_BUFSTART(buflen);
    940 				memcpy(txd->txd_buf + bufoff,
    941 				    mtod(m0, void *), buflen);
    942 				DPRINTF(MEC_DEBUG_START,
    943 				    ("mec_start: aligned, "
    944 				    "buflen = %d, bufoff = %d\n",
    945 				    buflen, bufoff));
    946 				txs->txs_flags |= MEC_TXS_TXDBUF | buflen;
    947 				txdaddr += MEC_TXD_ALIGN;
    948 			}
    949 #endif
    950 			txdlen  = len - buflen;
    951 			DPRINTF(MEC_DEBUG_START,
    952 			    ("mec_start: txdaddr = 0x%08llx, txdlen = %d\n",
    953 			    txdaddr, txdlen));
    954 
    955 			/*
    956 			 * sync the DMA map for TX mbuf
    957 			 *
    958 			 * XXX unaligned part doesn't have to be sync'ed,
    959 			 *     but it's harmless...
    960 			 */
    961 			bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
    962 			    dmamap->dm_mapsize,	BUS_DMASYNC_PREWRITE);
    963 		}
    964 
    965 #if NBPFILTER > 0
    966 		/*
    967 		 * Pass packet to bpf if there is a listener.
    968 		 */
    969 		if (ifp->if_bpf)
    970 			bpf_mtap(ifp->if_bpf, m0);
    971 #endif
    972 
    973 		/*
    974 		 * setup the transmit descriptor.
    975 		 */
    976 
    977 		/* TXINT bit will be set later on the last packet */
    978 		txd->txd_cmd = (len - 1);
    979 		/* but also set TXINT bit on a half of TXDESC */
    980 		if (sc->sc_txpending == (MEC_NTXDESC / 2))
    981 			txd->txd_cmd |= MEC_TXCMD_TXINT;
    982 
    983 		if (txs->txs_flags & MEC_TXS_TXDBUF)
    984 			txd->txd_cmd |= TXCMD_BUFSTART(MEC_TXDESCSIZE - buflen);
    985 		if (txs->txs_flags & MEC_TXS_TXDPTR1) {
    986 			txd->txd_cmd |= MEC_TXCMD_PTR1;
    987 			txd->txd_ptr[0] = TXPTR_LEN(txdlen - 1) | txdaddr;
    988 			/*
    989 			 * Store a pointer to the packet so we can
    990 			 * free it later.
    991 			 */
    992 			txs->txs_mbuf = m0;
    993 		} else {
    994 			txd->txd_ptr[0] = 0;
    995 			/*
    996 			 * In this case all data are copied to buffer in txdesc,
    997 			 * we can free TX mbuf here.
    998 			 */
    999 			m_freem(m0);
   1000 		}
   1001 
   1002 		DPRINTF(MEC_DEBUG_START,
   1003 		    ("mec_start: txd_cmd = 0x%016llx, txd_ptr = 0x%016llx\n",
   1004 		    txd->txd_cmd, txd->txd_ptr[0]));
   1005 		DPRINTF(MEC_DEBUG_START,
   1006 		    ("mec_start: len = %d (0x%04x), buflen = %d (0x%02x)\n",
   1007 		    len, len, buflen, buflen));
   1008 
   1009 		/* sync TX descriptor */
   1010 		MEC_TXDESCSYNC(sc, nexttx,
   1011 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1012 
   1013 		/* advance the TX pointer. */
   1014 		sc->sc_txpending++;
   1015 		sc->sc_txlast = nexttx;
   1016 	}
   1017 
   1018 	if (sc->sc_txpending == MEC_NTXDESC) {
   1019 		/* No more slots; notify upper layer. */
   1020 		ifp->if_flags |= IFF_OACTIVE;
   1021 	}
   1022 
   1023 	if (sc->sc_txpending != opending) {
   1024 		/*
   1025 		 * Cause a TX interrupt to happen on the last packet
   1026 		 * we enqueued.
   1027 		 */
   1028 		sc->sc_txdesc[sc->sc_txlast].txd_cmd |= MEC_TXCMD_TXINT;
   1029 		MEC_TXCMDSYNC(sc, sc->sc_txlast,
   1030 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1031 
   1032 		/* start TX */
   1033 		bus_space_write_8(st, sh, MEC_TX_RING_PTR,
   1034 		    MEC_NEXTTX(sc->sc_txlast));
   1035 
   1036 		/*
   1037 		 * If the transmitter was idle,
   1038 		 * reset the txdirty pointer and re-enable TX interrupt.
   1039 		 */
   1040 		if (opending == 0) {
   1041 			sc->sc_txdirty = firsttx;
   1042 			bus_space_write_8(st, sh, MEC_TX_ALIAS,
   1043 			    MEC_TX_ALIAS_INT_ENABLE);
   1044 		}
   1045 
   1046 		/* Set a watchdog timer in case the chip flakes out. */
   1047 		ifp->if_timer = 5;
   1048 	}
   1049 }
   1050 
   1051 STATIC void
   1052 mec_stop(struct ifnet *ifp, int disable)
   1053 {
   1054 	struct mec_softc *sc = ifp->if_softc;
   1055 	struct mec_txsoft *txs;
   1056 	int i;
   1057 
   1058 	DPRINTF(MEC_DEBUG_STOP, ("mec_stop\n"));
   1059 
   1060 	ifp->if_timer = 0;
   1061 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1062 
   1063 	callout_stop(&sc->sc_tick_ch);
   1064 	mii_down(&sc->sc_mii);
   1065 
   1066 	/* release any TX buffers */
   1067 	for (i = 0; i < MEC_NTXDESC; i++) {
   1068 		txs = &sc->sc_txsoft[i];
   1069 		if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
   1070 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1071 			m_freem(txs->txs_mbuf);
   1072 			txs->txs_mbuf = NULL;
   1073 		}
   1074 	}
   1075 }
   1076 
   1077 STATIC int
   1078 mec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1079 {
   1080 	struct mec_softc *sc = ifp->if_softc;
   1081 	struct ifreq *ifr = (void *)data;
   1082 	int s, error;
   1083 
   1084 	s = splnet();
   1085 
   1086 	error = ether_ioctl(ifp, cmd, data);
   1087 	if (error == ENETRESET) {
   1088 		/*
   1089 		 * Multicast list has changed; set the hardware filter
   1090 		 * accordingly.
   1091 		 */
   1092 		if (ifp->if_flags & IFF_RUNNING)
   1093 			error = mec_init(ifp);
   1094 		else
   1095 			error = 0;
   1096 	}
   1097 
   1098 	/* Try to get more packets going. */
   1099 	mec_start(ifp);
   1100 
   1101 	splx(s);
   1102 	return error;
   1103 }
   1104 
   1105 STATIC void
   1106 mec_watchdog(struct ifnet *ifp)
   1107 {
   1108 	struct mec_softc *sc = ifp->if_softc;
   1109 
   1110 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1111 	ifp->if_oerrors++;
   1112 
   1113 	mec_init(ifp);
   1114 }
   1115 
   1116 STATIC void
   1117 mec_tick(void *arg)
   1118 {
   1119 	struct mec_softc *sc = arg;
   1120 	int s;
   1121 
   1122 	s = splnet();
   1123 	mii_tick(&sc->sc_mii);
   1124 	splx(s);
   1125 
   1126 	callout_reset(&sc->sc_tick_ch, hz, mec_tick, sc);
   1127 }
   1128 
   1129 STATIC void
   1130 mec_setfilter(struct mec_softc *sc)
   1131 {
   1132 	struct ethercom *ec = &sc->sc_ethercom;
   1133 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1134 	struct ether_multi *enm;
   1135 	struct ether_multistep step;
   1136 	bus_space_tag_t st = sc->sc_st;
   1137 	bus_space_handle_t sh = sc->sc_sh;
   1138 	uint64_t mchash;
   1139 	uint32_t control, hash;
   1140 	int mcnt;
   1141 
   1142 	control = bus_space_read_8(st, sh, MEC_MAC_CONTROL);
   1143 	control &= ~MEC_MAC_FILTER_MASK;
   1144 
   1145 	if (ifp->if_flags & IFF_PROMISC) {
   1146 		control |= MEC_MAC_FILTER_PROMISC;
   1147 		bus_space_write_8(st, sh, MEC_MULTICAST, 0xffffffffffffffffULL);
   1148 		bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1149 		return;
   1150 	}
   1151 
   1152 	mcnt = 0;
   1153 	mchash = 0;
   1154 	ETHER_FIRST_MULTI(step, ec, enm);
   1155 	while (enm != NULL) {
   1156 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1157 			/* set allmulti for a range of multicast addresses */
   1158 			control |= MEC_MAC_FILTER_ALLMULTI;
   1159 			bus_space_write_8(st, sh, MEC_MULTICAST,
   1160 			    0xffffffffffffffffULL);
   1161 			bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1162 			return;
   1163 		}
   1164 
   1165 #define mec_calchash(addr)	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
   1166 
   1167 		hash = mec_calchash(enm->enm_addrlo);
   1168 		mchash |= 1 << hash;
   1169 		mcnt++;
   1170 		ETHER_NEXT_MULTI(step, enm);
   1171 	}
   1172 
   1173 	ifp->if_flags &= ~IFF_ALLMULTI;
   1174 
   1175 	if (mcnt > 0)
   1176 		control |= MEC_MAC_FILTER_MATCHMULTI;
   1177 
   1178 	bus_space_write_8(st, sh, MEC_MULTICAST, mchash);
   1179 	bus_space_write_8(st, sh, MEC_MAC_CONTROL, control);
   1180 }
   1181 
   1182 STATIC int
   1183 mec_intr(void *arg)
   1184 {
   1185 	struct mec_softc *sc = arg;
   1186 	bus_space_tag_t st = sc->sc_st;
   1187 	bus_space_handle_t sh = sc->sc_sh;
   1188 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1189 	uint32_t statreg, statack, dmac;
   1190 	int handled, sent;
   1191 
   1192 	DPRINTF(MEC_DEBUG_INTR, ("mec_intr: called\n"));
   1193 
   1194 	handled = sent = 0;
   1195 
   1196 	for (;;) {
   1197 		statreg = bus_space_read_8(st, sh, MEC_INT_STATUS);
   1198 
   1199 		DPRINTF(MEC_DEBUG_INTR,
   1200 		    ("mec_intr: INT_STAT = 0x%08x\n", statreg));
   1201 
   1202 		statack = statreg & MEC_INT_STATUS_MASK;
   1203 		if (statack == 0)
   1204 			break;
   1205 		bus_space_write_8(st, sh, MEC_INT_STATUS, statack);
   1206 
   1207 		handled = 1;
   1208 
   1209 		if (statack &
   1210 		    (MEC_INT_RX_THRESHOLD |
   1211 		     MEC_INT_RX_FIFO_UNDERFLOW)) {
   1212 			mec_rxintr(sc);
   1213 		}
   1214 
   1215 		dmac = bus_space_read_8(st, sh, MEC_DMA_CONTROL);
   1216 		DPRINTF(MEC_DEBUG_INTR,
   1217 		    ("mec_intr: DMA_CONT = 0x%08x\n", dmac));
   1218 
   1219 		if (statack &
   1220 		    (MEC_INT_TX_EMPTY |
   1221 		     MEC_INT_TX_PACKET_SENT |
   1222 		     MEC_INT_TX_ABORT)) {
   1223 			mec_txintr(sc);
   1224 			sent = 1;
   1225 			if ((statack & MEC_INT_TX_EMPTY) != 0 &&
   1226 			    (dmac & MEC_DMA_TX_INT_ENABLE) != 0) {
   1227 				/*
   1228 				 * disable TX interrupt to stop
   1229 				 * TX empty interrupt
   1230 				 */
   1231 				bus_space_write_8(st, sh, MEC_TX_ALIAS, 0);
   1232 				DPRINTF(MEC_DEBUG_INTR,
   1233 				    ("mec_intr: disable TX_INT\n"));
   1234 			}
   1235 		}
   1236 
   1237 		if (statack &
   1238 		    (MEC_INT_TX_LINK_FAIL |
   1239 		     MEC_INT_TX_MEM_ERROR |
   1240 		     MEC_INT_TX_ABORT |
   1241 		     MEC_INT_RX_FIFO_UNDERFLOW |
   1242 		     MEC_INT_RX_DMA_UNDERFLOW)) {
   1243 			printf("%s: mec_intr: interrupt status = 0x%08x\n",
   1244 			    sc->sc_dev.dv_xname, statreg);
   1245 		}
   1246 	}
   1247 
   1248 	if (sent) {
   1249 		/* try to get more packets going */
   1250 		mec_start(ifp);
   1251 	}
   1252 
   1253 #if NRND > 0
   1254 	if (handled)
   1255 		rnd_add_uint32(&sc->sc_rnd_source, statreg);
   1256 #endif
   1257 
   1258 	return handled;
   1259 }
   1260 
   1261 STATIC void
   1262 mec_rxintr(struct mec_softc *sc)
   1263 {
   1264 	bus_space_tag_t st = sc->sc_st;
   1265 	bus_space_handle_t sh = sc->sc_sh;
   1266 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1267 	struct mbuf *m;
   1268 	struct mec_rxdesc *rxd;
   1269 	uint64_t rxstat;
   1270 	u_int len;
   1271 	int i;
   1272 
   1273 	DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: called\n"));
   1274 
   1275 	for (i = sc->sc_rxptr;; i = MEC_NEXTRX(i)) {
   1276 		rxd = &sc->sc_rxdesc[i];
   1277 
   1278 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_POSTREAD);
   1279 		rxstat = rxd->rxd_stat;
   1280 
   1281 		DPRINTF(MEC_DEBUG_RXINTR,
   1282 		    ("mec_rxintr: rxstat = 0x%016llx, rxptr = %d\n",
   1283 		    rxstat, i));
   1284 		DPRINTF(MEC_DEBUG_RXINTR, ("mec_rxintr: rxfifo = 0x%08x\n",
   1285 		    (u_int)bus_space_read_8(st, sh, MEC_RX_FIFO)));
   1286 
   1287 		if ((rxstat & MEC_RXSTAT_RECEIVED) == 0) {
   1288 			MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1289 			break;
   1290 		}
   1291 
   1292 		len = rxstat & MEC_RXSTAT_LEN;
   1293 
   1294 		if (len < ETHER_MIN_LEN ||
   1295 		    len > (MCLBYTES - MEC_ETHER_ALIGN)) {
   1296 			/* invalid length packet; drop it. */
   1297 			DPRINTF(MEC_DEBUG_RXINTR,
   1298 			    ("mec_rxintr: wrong packet\n"));
   1299  dropit:
   1300 			ifp->if_ierrors++;
   1301 			rxd->rxd_stat = 0;
   1302 			MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1303 			bus_space_write_8(st, sh, MEC_MCL_RX_FIFO,
   1304 			    MEC_CDRXADDR(sc, i));
   1305 			continue;
   1306 		}
   1307 
   1308 		if (rxstat &
   1309 		    (MEC_RXSTAT_BADPACKET |
   1310 		     MEC_RXSTAT_LONGEVENT |
   1311 		     MEC_RXSTAT_INVALID   |
   1312 		     MEC_RXSTAT_CRCERROR  |
   1313 		     MEC_RXSTAT_VIOLATION)) {
   1314 			printf("%s: mec_rxintr: status = 0x%016llx\n",
   1315 			    sc->sc_dev.dv_xname, rxstat);
   1316 			goto dropit;
   1317 		}
   1318 
   1319 		/*
   1320 		 * The MEC includes the CRC with every packet.  Trim
   1321 		 * it off here.
   1322 		 */
   1323 		len -= ETHER_CRC_LEN;
   1324 
   1325 		/*
   1326 		 * now allocate an mbuf (and possibly a cluster) to hold
   1327 		 * the received packet.
   1328 		 */
   1329 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1330 		if (m == NULL) {
   1331 			printf("%s: unable to allocate RX mbuf\n",
   1332 			    sc->sc_dev.dv_xname);
   1333 			goto dropit;
   1334 		}
   1335 		if (len > (MHLEN - MEC_ETHER_ALIGN)) {
   1336 			MCLGET(m, M_DONTWAIT);
   1337 			if ((m->m_flags & M_EXT) == 0) {
   1338 				printf("%s: unable to allocate RX cluster\n",
   1339 				    sc->sc_dev.dv_xname);
   1340 				m_freem(m);
   1341 				m = NULL;
   1342 				goto dropit;
   1343 			}
   1344 		}
   1345 
   1346 		/*
   1347 		 * Note MEC chip seems to insert 2 byte padding at the top of
   1348 		 * RX buffer, but we copy whole buffer to avoid unaligned copy.
   1349 		 */
   1350 		MEC_RXBUFSYNC(sc, i, len, BUS_DMASYNC_POSTREAD);
   1351 		memcpy(mtod(m, void *), rxd->rxd_buf, MEC_ETHER_ALIGN + len);
   1352 		MEC_RXBUFSYNC(sc, i, ETHER_MAX_LEN, BUS_DMASYNC_PREREAD);
   1353 		m->m_data += MEC_ETHER_ALIGN;
   1354 
   1355 		/* put RX buffer into FIFO again */
   1356 		rxd->rxd_stat = 0;
   1357 		MEC_RXSTATSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1358 		bus_space_write_8(st, sh, MEC_MCL_RX_FIFO, MEC_CDRXADDR(sc, i));
   1359 
   1360 		m->m_pkthdr.rcvif = ifp;
   1361 		m->m_pkthdr.len = m->m_len = len;
   1362 
   1363 		ifp->if_ipackets++;
   1364 
   1365 #if NBPFILTER > 0
   1366 		/*
   1367 		 * Pass this up to any BPF listeners, but only
   1368 		 * pass it up the stack if it's for us.
   1369 		 */
   1370 		if (ifp->if_bpf)
   1371 			bpf_mtap(ifp->if_bpf, m);
   1372 #endif
   1373 
   1374 		/* Pass it on. */
   1375 		(*ifp->if_input)(ifp, m);
   1376 	}
   1377 
   1378 	/* update RX pointer */
   1379 	sc->sc_rxptr = i;
   1380 }
   1381 
   1382 STATIC void
   1383 mec_txintr(struct mec_softc *sc)
   1384 {
   1385 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1386 	struct mec_txdesc *txd;
   1387 	struct mec_txsoft *txs;
   1388 	bus_dmamap_t dmamap;
   1389 	uint64_t txstat;
   1390 	int i;
   1391 	u_int col;
   1392 
   1393 	ifp->if_flags &= ~IFF_OACTIVE;
   1394 
   1395 	DPRINTF(MEC_DEBUG_TXINTR, ("mec_txintr: called\n"));
   1396 
   1397 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
   1398 	    i = MEC_NEXTTX(i), sc->sc_txpending--) {
   1399 		txd = &sc->sc_txdesc[i];
   1400 
   1401 		MEC_TXDESCSYNC(sc, i,
   1402 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1403 
   1404 		txstat = txd->txd_stat;
   1405 		DPRINTF(MEC_DEBUG_TXINTR,
   1406 		    ("mec_txintr: dirty = %d, txstat = 0x%016llx\n",
   1407 		    i, txstat));
   1408 		if ((txstat & MEC_TXSTAT_SENT) == 0) {
   1409 			MEC_TXCMDSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1410 			break;
   1411 		}
   1412 
   1413 		if ((txstat & MEC_TXSTAT_SUCCESS) == 0) {
   1414 			printf("%s: TX error: txstat = 0x%016llx\n",
   1415 			    sc->sc_dev.dv_xname, txstat);
   1416 			ifp->if_oerrors++;
   1417 			continue;
   1418 		}
   1419 
   1420 		txs = &sc->sc_txsoft[i];
   1421 		if ((txs->txs_flags & MEC_TXS_TXDPTR1) != 0) {
   1422 			dmamap = txs->txs_dmamap;
   1423 			bus_dmamap_sync(sc->sc_dmat, dmamap, 0,
   1424 			    dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1425 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   1426 			m_freem(txs->txs_mbuf);
   1427 			txs->txs_mbuf = NULL;
   1428 		}
   1429 
   1430 		col = (txstat & MEC_TXSTAT_COLCNT) >> MEC_TXSTAT_COLCNT_SHIFT;
   1431 		ifp->if_collisions += col;
   1432 		ifp->if_opackets++;
   1433 	}
   1434 
   1435 	/* update the dirty TX buffer pointer */
   1436 	sc->sc_txdirty = i;
   1437 	DPRINTF(MEC_DEBUG_INTR,
   1438 	    ("mec_txintr: sc_txdirty = %2d, sc_txpending = %2d\n",
   1439 	    sc->sc_txdirty, sc->sc_txpending));
   1440 
   1441 	/* cancel the watchdog timer if there are no pending TX packets */
   1442 	if (sc->sc_txpending == 0)
   1443 		ifp->if_timer = 0;
   1444 }
   1445 
   1446 STATIC void
   1447 mec_shutdown(void *arg)
   1448 {
   1449 	struct mec_softc *sc = arg;
   1450 
   1451 	mec_stop(&sc->sc_ethercom.ec_if, 1);
   1452 }
   1453