Home | History | Annotate | Line # | Download | only in dev
if_admsw.c revision 1.2
      1 /* $NetBSD: if_admsw.c,v 1.2 2007/04/17 03:33:48 dyoung Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or
      8  * without modification, are permitted provided that the following
      9  * conditions 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
     13  *    copyright notice, this list of conditions and the following
     14  *    disclaimer in the documentation and/or other materials provided
     15  *    with the distribution.
     16  * 3. The names of the authors may not be used to endorse or promote
     17  *    products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
     21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     27  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     31  * OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 2001 Wasabi Systems, Inc.
     35  * All rights reserved.
     36  *
     37  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *	This product includes software developed for the NetBSD Project by
     50  *	Wasabi Systems, Inc.
     51  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     52  *    or promote products derived from this software without specific prior
     53  *    written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
     70  * Access Controller.
     71  *
     72  * TODO:
     73  *
     74  *	Better Rx buffer management; we want to get new Rx buffers
     75  *	to the chip more quickly than we currently do.
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: if_admsw.c,v 1.2 2007/04/17 03:33:48 dyoung Exp $");
     80 
     81 #include "bpfilter.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/callout.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/malloc.h>
     88 #include <sys/kernel.h>
     89 #include <sys/socket.h>
     90 #include <sys/ioctl.h>
     91 #include <sys/errno.h>
     92 #include <sys/device.h>
     93 #include <sys/queue.h>
     94 
     95 #include <prop/proplib.h>
     96 
     97 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
     98 
     99 #include <net/if.h>
    100 #include <net/if_dl.h>
    101 #include <net/if_media.h>
    102 #include <net/if_ether.h>
    103 
    104 #if NBPFILTER > 0
    105 #include <net/bpf.h>
    106 #endif
    107 
    108 #include <machine/bus.h>
    109 #include <machine/intr.h>
    110 #include <machine/endian.h>
    111 
    112 #include <dev/mii/mii.h>
    113 #include <dev/mii/miivar.h>
    114 
    115 #include <sys/gpio.h>
    116 #include <dev/gpio/gpiovar.h>
    117 
    118 #include <mips/adm5120/include/adm5120reg.h>
    119 #include <mips/adm5120/include/adm5120var.h>
    120 #include <mips/adm5120/include/adm5120_obiovar.h>
    121 #include <mips/adm5120/dev/if_admswreg.h>
    122 #include <mips/adm5120/dev/if_admswvar.h>
    123 
    124 static uint8_t vlan_matrix[SW_DEVS] = {
    125 	(1 << 6) | (1 << 0),		/* CPU + port0 */
    126 	(1 << 6) | (1 << 1),		/* CPU + port1 */
    127 	(1 << 6) | (1 << 2),		/* CPU + port2 */
    128 	(1 << 6) | (1 << 3),		/* CPU + port3 */
    129 	(1 << 6) | (1 << 4),		/* CPU + port4 */
    130 	(1 << 6) | (1 << 5),		/* CPU + port5 */
    131 };
    132 
    133 #ifdef ADMSW_EVENT_COUNTERS
    134 #define	ADMSW_EVCNT_INCR(ev)	(ev)->ev_count++
    135 #else
    136 #define	ADMSW_EVCNT_INCR(ev)	/* nothing */
    137 #endif
    138 
    139 static void	admsw_start(struct ifnet *);
    140 static void	admsw_watchdog(struct ifnet *);
    141 static int	admsw_ioctl(struct ifnet *, u_long, void *);
    142 static int	admsw_init(struct ifnet *);
    143 static void	admsw_stop(struct ifnet *, int);
    144 
    145 static void	admsw_shutdown(void *);
    146 
    147 static void	admsw_reset(struct admsw_softc *);
    148 static void	admsw_set_filter(struct admsw_softc *);
    149 
    150 static int	admsw_intr(void *);
    151 static void	admsw_txintr(struct admsw_softc *, int);
    152 static void	admsw_rxintr(struct admsw_softc *, int);
    153 static int	admsw_add_rxbuf(struct admsw_softc *, int, int);
    154 #define	admsw_add_rxhbuf(sc, idx)	admsw_add_rxbuf(sc, idx, 1)
    155 #define	admsw_add_rxlbuf(sc, idx)	admsw_add_rxbuf(sc, idx, 0)
    156 
    157 static int	admsw_mediachange(struct ifnet *);
    158 static void	admsw_mediastatus(struct ifnet *, struct ifmediareq *);
    159 
    160 static int	admsw_match(struct device *, struct cfdata *, void *);
    161 static void	admsw_attach(struct device *, struct device *, void *);
    162 
    163 CFATTACH_DECL(admsw, sizeof(struct admsw_softc),
    164     admsw_match, admsw_attach, NULL, NULL);
    165 
    166 static int
    167 admsw_match(struct device *parent, struct cfdata *cf, void *aux)
    168 {
    169 	struct obio_attach_args *aa = aux;
    170 
    171 	return strcmp(aa->oba_name, cf->cf_name) == 0;
    172 }
    173 
    174 #define	REG_READ(o)	bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
    175 #define	REG_WRITE(o,v)	bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
    176 
    177 
    178 static void
    179 admsw_init_bufs(struct admsw_softc *sc)
    180 {
    181 	int i;
    182 	struct admsw_desc *desc;
    183 
    184 	for (i = 0; i < ADMSW_NTXHDESC; i++) {
    185 		if (sc->sc_txhsoft[i].ds_mbuf != NULL) {
    186 			m_freem(sc->sc_txhsoft[i].ds_mbuf);
    187 			sc->sc_txhsoft[i].ds_mbuf = NULL;
    188 		}
    189 		desc = &sc->sc_txhdescs[i];
    190 		desc->data = 0;
    191 		desc->cntl = 0;
    192 		desc->len = MAC_BUFLEN;
    193 		desc->status = 0;
    194 		ADMSW_CDTXHSYNC(sc, i,
    195 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    196 	}
    197 	sc->sc_txhdescs[ADMSW_NTXHDESC - 1].data |= ADM5120_DMA_RINGEND;
    198 	ADMSW_CDTXHSYNC(sc, ADMSW_NTXHDESC - 1,
    199 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    200 
    201 	for (i = 0; i < ADMSW_NRXHDESC; i++) {
    202 		if (sc->sc_rxhsoft[i].ds_mbuf == NULL) {
    203 			if (admsw_add_rxhbuf(sc, i) != 0)
    204 				panic("admsw_init_bufs\n");
    205 		} else
    206 			ADMSW_INIT_RXHDESC(sc, i);
    207 	}
    208 
    209 	for (i = 0; i < ADMSW_NTXLDESC; i++) {
    210 		if (sc->sc_txlsoft[i].ds_mbuf != NULL) {
    211 			m_freem(sc->sc_txlsoft[i].ds_mbuf);
    212 			sc->sc_txlsoft[i].ds_mbuf = NULL;
    213 		}
    214 		desc = &sc->sc_txldescs[i];
    215 		desc->data = 0;
    216 		desc->cntl = 0;
    217 		desc->len = MAC_BUFLEN;
    218 		desc->status = 0;
    219 		ADMSW_CDTXLSYNC(sc, i,
    220 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    221 	}
    222 	sc->sc_txldescs[ADMSW_NTXLDESC - 1].data |= ADM5120_DMA_RINGEND;
    223 	ADMSW_CDTXLSYNC(sc, ADMSW_NTXLDESC - 1,
    224 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    225 
    226 	for (i = 0; i < ADMSW_NRXLDESC; i++) {
    227 		if (sc->sc_rxlsoft[i].ds_mbuf == NULL) {
    228 			if (admsw_add_rxlbuf(sc, i) != 0)
    229 				panic("admsw_init_bufs\n");
    230 		} else
    231 			ADMSW_INIT_RXLDESC(sc, i);
    232 	}
    233 
    234 	REG_WRITE(SEND_HBADDR_REG, ADMSW_CDTXHADDR(sc, 0));
    235 	REG_WRITE(SEND_LBADDR_REG, ADMSW_CDTXLADDR(sc, 0));
    236 	REG_WRITE(RECV_HBADDR_REG, ADMSW_CDRXHADDR(sc, 0));
    237 	REG_WRITE(RECV_LBADDR_REG, ADMSW_CDRXLADDR(sc, 0));
    238 
    239 	sc->sc_txfree = ADMSW_NTXLDESC;
    240 	sc->sc_txnext = 0;
    241 	sc->sc_txdirty = 0;
    242 	sc->sc_rxptr = 0;
    243 }
    244 
    245 static void
    246 admsw_setvlan(struct admsw_softc *sc, char matrix[6])
    247 {
    248 	uint32_t i;
    249 
    250 	i = matrix[0] + (matrix[1] << 8) + (matrix[2] << 16) + (matrix[3] << 24);
    251 	REG_WRITE(VLAN_G1_REG, i);
    252 	i = matrix[4] + (matrix[5] << 8);
    253 	REG_WRITE(VLAN_G2_REG, i);
    254 }
    255 
    256 static void
    257 admsw_reset(struct admsw_softc *sc)
    258 {
    259 	uint32_t wdog1;
    260 	int i;
    261 
    262 	REG_WRITE(PORT_CONF0_REG,
    263 	    REG_READ(PORT_CONF0_REG) | PORT_CONF0_DP_MASK);
    264 	REG_WRITE(CPUP_CONF_REG,
    265 	    REG_READ(CPUP_CONF_REG) | CPUP_CONF_DCPUP);
    266 
    267         /* Wait for DMA to complete.  Overkill.  In 3ms, we can
    268          * send at least two entire 1500-byte packets at 10 Mb/s.
    269 	 */
    270 	DELAY(3000);
    271 
    272         /* The datasheet recommends that we move all PHYs to reset
    273          * state prior to software reset.
    274 	 */
    275 	REG_WRITE(PHY_CNTL2_REG,
    276 	    REG_READ(PHY_CNTL2_REG) & ~PHY_CNTL2_PHYR_MASK);
    277 
    278 	/* Reset the switch. */
    279 	REG_WRITE(ADMSW_SW_RES, 0x1);
    280 
    281 	DELAY(100 * 1000);
    282 
    283 	REG_WRITE(ADMSW_BOOT_DONE, ADMSW_BOOT_DONE_BO);
    284 
    285 	/* begin old code */
    286 	REG_WRITE(CPUP_CONF_REG,
    287 	    CPUP_CONF_DCPUP | CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
    288 	    CPUP_CONF_DMCP_MASK);
    289 
    290 	REG_WRITE(PORT_CONF0_REG, PORT_CONF0_EMCP_MASK | PORT_CONF0_EMBP_MASK);
    291 
    292 	REG_WRITE(PHY_CNTL2_REG,
    293 	    REG_READ(PHY_CNTL2_REG) | PHY_CNTL2_ANE_MASK | PHY_CNTL2_PHYR_MASK |
    294 	    PHY_CNTL2_AMDIX_MASK);
    295 
    296 	REG_WRITE(PHY_CNTL3_REG, REG_READ(PHY_CNTL3_REG) | PHY_CNTL3_RNT);
    297 
    298 	REG_WRITE(ADMSW_INT_MASK, INT_MASK);
    299 	REG_WRITE(ADMSW_INT_ST, INT_MASK);
    300 
    301 	/*
    302 	 * While in DDB, we stop servicing interrupts, RX ring
    303 	 * fills up and when free block counter falls behind FC
    304 	 * threshold, the switch starts to emit 802.3x PAUSE
    305 	 * frames.  This can upset peer switches.
    306 	 *
    307 	 * Stop this from happening by disabling FC and D2
    308 	 * thresholds.
    309 	 */
    310 	REG_WRITE(FC_TH_REG,
    311 	    REG_READ(FC_TH_REG) & ~(FC_TH_FCS_MASK | FC_TH_D2S_MASK));
    312 
    313 	admsw_setvlan(sc, vlan_matrix);
    314 
    315 	for (i = 0; i < SW_DEVS; i++) {
    316 		REG_WRITE(MAC_WT1_REG,
    317 		    sc->sc_enaddr[2] |
    318 		    (sc->sc_enaddr[3]<<8) |
    319 		    (sc->sc_enaddr[4]<<16) |
    320 		    ((sc->sc_enaddr[5]+i)<<24));
    321 		REG_WRITE(MAC_WT0_REG, (i<<MAC_WT0_VLANID_SHIFT) |
    322 		    (sc->sc_enaddr[0]<<16) | (sc->sc_enaddr[1]<<24) |
    323 		    MAC_WT0_WRITE | MAC_WT0_VLANID_EN);
    324 
    325 		while (!(REG_READ(MAC_WT0_REG) & MAC_WT0_WRITE_DONE));
    326 	}
    327 	wdog1 = REG_READ(ADM5120_WDOG1);
    328 	printf("%s: reg[ADM5120_WDOG1] = 0x%08" PRIx32 "\n", __func__, wdog1);
    329 	REG_WRITE(ADM5120_WDOG1, wdog1 & ~ADM5120_WDOG1_WDE);
    330 }
    331 
    332 static void
    333 admsw_attach(struct device *parent, struct device *self, void *aux)
    334 {
    335 	uint8_t enaddr[ETHER_ADDR_LEN];
    336 	struct admsw_softc *sc = (void *) self;
    337 	struct obio_attach_args *aa = aux;
    338 	struct ifnet *ifp;
    339 	bus_dma_segment_t seg;
    340 	int error, i, rseg;
    341 	prop_data_t pd;
    342 
    343 	printf(": ADM5120 Switch Engine, %d ports\n", SW_DEVS);
    344 
    345 	sc->sc_dmat = aa->oba_dt;
    346 	sc->sc_st = aa->oba_st;
    347 
    348 	pd = prop_dictionary_get(device_properties(&sc->sc_dev), "mac-addr");
    349 
    350 	if (pd == NULL) {
    351 		enaddr[0] = 0x02;
    352 		enaddr[1] = 0xaa;
    353 		enaddr[2] = 0xbb;
    354 		enaddr[3] = 0xcc;
    355 		enaddr[4] = 0xdd;
    356 		enaddr[5] = 0xee;
    357 	} else
    358 		memcpy(enaddr, prop_data_data_nocopy(pd), sizeof(enaddr));
    359 
    360 	memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
    361 
    362 	printf("%s: base Ethernet address %s\n", sc->sc_dev.dv_xname,
    363 	    ether_sprintf(enaddr));
    364 
    365 	/* Map the device. */
    366 	if (bus_space_map(sc->sc_st, aa->oba_addr, 512, 0, &sc->sc_ioh) != 0) {
    367 		printf("%s: unable to map device\n", device_xname(&sc->sc_dev));
    368 		return;
    369 	}
    370 
    371 	/* Hook up the interrupt handler. */
    372 	sc->sc_ih = adm5120_intr_establish(aa->oba_irq, INTR_IRQ, admsw_intr, sc);
    373 
    374 	if (sc->sc_ih == NULL) {
    375 		printf("%s: unable to register interrupt handler\n",
    376 		    sc->sc_dev.dv_xname);
    377 		return;
    378 	}
    379 
    380 	/*
    381 	 * Allocate the control data structures, and create and load the
    382 	 * DMA map for it.
    383 	 */
    384 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    385 	    sizeof(struct admsw_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    386 	    0)) != 0) {
    387 		printf("%s: unable to allocate control data, error = %d\n",
    388 		    sc->sc_dev.dv_xname, error);
    389 		return;
    390 	}
    391 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    392 	    sizeof(struct admsw_control_data), (void *)&sc->sc_control_data,
    393 	    0)) != 0) {
    394 		printf("%s: unable to map control data, error = %d\n",
    395 		    sc->sc_dev.dv_xname, error);
    396 		return;
    397 	}
    398 	if ((error = bus_dmamap_create(sc->sc_dmat,
    399 	    sizeof(struct admsw_control_data), 1,
    400 	    sizeof(struct admsw_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    401 		printf("%s: unable to create control data DMA map, "
    402 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    403 		return;
    404 	}
    405 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    406 	    sc->sc_control_data, sizeof(struct admsw_control_data), NULL,
    407 	    0)) != 0) {
    408 		printf("%s: unable to load control data DMA map, error = %d\n",
    409 		    sc->sc_dev.dv_xname, error);
    410 		return;
    411 	}
    412 
    413 	/*
    414 	 * Create the transmit buffer DMA maps.
    415 	 */
    416 	for (i = 0; i < ADMSW_NTXHDESC; i++) {
    417 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    418 		    2, MCLBYTES, 0, 0,
    419 		    &sc->sc_txhsoft[i].ds_dmamap)) != 0) {
    420 			printf("%s: unable to create txh DMA map %d, "
    421 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    422 			return;
    423 		}
    424 		sc->sc_txhsoft[i].ds_mbuf = NULL;
    425 	}
    426 	for (i = 0; i < ADMSW_NTXLDESC; i++) {
    427 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    428 		    2, MCLBYTES, 0, 0,
    429 		    &sc->sc_txlsoft[i].ds_dmamap)) != 0) {
    430 			printf("%s: unable to create txl DMA map %d, "
    431 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    432 			return;
    433 		}
    434 		sc->sc_txlsoft[i].ds_mbuf = NULL;
    435 	}
    436 
    437 	/*
    438 	 * Create the receive buffer DMA maps.
    439 	 */
    440 	for (i = 0; i < ADMSW_NRXHDESC; i++) {
    441 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    442 		    MCLBYTES, 0, 0, &sc->sc_rxhsoft[i].ds_dmamap)) != 0) {
    443 			printf("%s: unable to create rxh DMA map %d, "
    444 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    445 			return;
    446 		}
    447 		sc->sc_rxhsoft[i].ds_mbuf = NULL;
    448 	}
    449 	for (i = 0; i < ADMSW_NRXLDESC; i++) {
    450 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    451 		    MCLBYTES, 0, 0, &sc->sc_rxlsoft[i].ds_dmamap)) != 0) {
    452 			printf("%s: unable to create rxl DMA map %d, "
    453 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    454 			return;
    455 		}
    456 		sc->sc_rxlsoft[i].ds_mbuf = NULL;
    457 	}
    458 
    459 	admsw_init_bufs(sc);
    460 
    461 	admsw_reset(sc);
    462 
    463 	for (i = 0; i < SW_DEVS; i++) {
    464 		ifmedia_init(&sc->sc_ifmedia[i], 0, admsw_mediachange, admsw_mediastatus);
    465 		ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_10_T, 0, NULL);
    466 		ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
    467 		ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_100_TX, 0, NULL);
    468 		ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
    469 		ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO, 0, NULL);
    470 		ifmedia_set(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO);
    471 
    472 		ifp = &sc->sc_ethercom[i].ec_if;
    473 		strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    474 		ifp->if_xname[5] += i;
    475 		ifp->if_softc = sc;
    476 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    477 		ifp->if_ioctl = admsw_ioctl;
    478 		ifp->if_start = admsw_start;
    479 		ifp->if_watchdog = admsw_watchdog;
    480 		ifp->if_init = admsw_init;
    481 		ifp->if_stop = admsw_stop;
    482 		ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
    483 		IFQ_SET_MAXLEN(&ifp->if_snd, max(ADMSW_NTXLDESC, IFQ_MAXLEN));
    484 		IFQ_SET_READY(&ifp->if_snd);
    485 
    486 		/* Attach the interface. */
    487 		if_attach(ifp);
    488 		ether_ifattach(ifp, enaddr);
    489 		enaddr[5]++;
    490 	}
    491 
    492 #ifdef ADMSW_EVENT_COUNTERS
    493 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    494 	    NULL, sc->sc_dev.dv_xname, "txstall");
    495 	evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC,
    496 	    NULL, sc->sc_dev.dv_xname, "rxstall");
    497 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC,
    498 	    NULL, sc->sc_dev.dv_xname, "txintr");
    499 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC,
    500 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    501 #if 1
    502 	evcnt_attach_dynamic(&sc->sc_ev_rxsync, EVCNT_TYPE_MISC,
    503 	    NULL, sc->sc_dev.dv_xname, "rxsync");
    504 #endif
    505 #endif
    506 
    507 	admwdog_attach(sc);
    508 
    509 	/* Make sure the interface is shutdown during reboot. */
    510 	sc->sc_sdhook = shutdownhook_establish(admsw_shutdown, sc);
    511 	if (sc->sc_sdhook == NULL)
    512 		printf("%s: WARNING: unable to establish shutdown hook\n",
    513 		    sc->sc_dev.dv_xname);
    514 
    515 	/* leave interrupts and cpu port disabled */
    516 	return;
    517 }
    518 
    519 
    520 /*
    521  * admsw_shutdown:
    522  *
    523  *	Make sure the interface is stopped at reboot time.
    524  */
    525 static void
    526 admsw_shutdown(void *arg)
    527 {
    528 	struct admsw_softc *sc = arg;
    529 	int i;
    530 
    531 	for (i = 0; i < SW_DEVS; i++)
    532 		admsw_stop(&sc->sc_ethercom[i].ec_if, 1);
    533 }
    534 
    535 /*
    536  * admsw_start:		[ifnet interface function]
    537  *
    538  *	Start packet transmission on the interface.
    539  */
    540 static void
    541 admsw_start(struct ifnet *ifp)
    542 {
    543 	struct admsw_softc *sc = ifp->if_softc;
    544 	struct mbuf *m0, *m;
    545 	struct admsw_descsoft *ds;
    546 	struct admsw_desc *desc;
    547 	bus_dmamap_t dmamap;
    548 	struct ether_header *eh;
    549 	int error, nexttx, len, i;
    550 	static int vlan = 0;
    551 
    552 	/*
    553 	 * Loop through the send queues, setting up transmit descriptors
    554 	 * unitl we drain the queues, or use up all available transmit
    555 	 * descriptors.
    556 	 */
    557 	for (;;) {
    558 		vlan++;
    559 		if (vlan == SW_DEVS)
    560 			vlan = 0;
    561 		i = vlan;
    562 		for (;;) {
    563 			ifp = &sc->sc_ethercom[i].ec_if;
    564 			if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) ==
    565 			    IFF_RUNNING) {
    566 				/* Grab a packet off the queue. */
    567 				IFQ_POLL(&ifp->if_snd, m0);
    568 				if (m0 != NULL)
    569 					break;
    570 			}
    571 			i++;
    572 			if (i == SW_DEVS)
    573 				i = 0;
    574 			if (i == vlan)
    575 				return;
    576 		}
    577 		vlan = i;
    578 		m = NULL;
    579 
    580 		/* Get a spare descriptor. */
    581 		if (sc->sc_txfree == 0) {
    582 			/* No more slots left; notify upper layer. */
    583 			ifp->if_flags |= IFF_OACTIVE;
    584 			ADMSW_EVCNT_INCR(&sc->sc_ev_txstall);
    585 			break;
    586 		}
    587 		nexttx = sc->sc_txnext;
    588 		desc = &sc->sc_txldescs[nexttx];
    589 		ds = &sc->sc_txlsoft[nexttx];
    590 		dmamap = ds->ds_dmamap;
    591 
    592 		/*
    593 		 * Load the DMA map.  If this fails, the packet either
    594 		 * didn't fit in the alloted number of segments, or we
    595 		 * were short on resources.  In this case, we'll copy
    596 		 * and try again.
    597 		 */
    598 		if (m0->m_pkthdr.len < ETHER_MIN_LEN ||
    599 		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    600 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    601 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    602 			if (m == NULL) {
    603 				printf("%s: unable to allocate Tx mbuf\n",
    604 				    sc->sc_dev.dv_xname);
    605 				break;
    606 			}
    607 			if (m0->m_pkthdr.len > MHLEN) {
    608 				MCLGET(m, M_DONTWAIT);
    609 				if ((m->m_flags & M_EXT) == 0) {
    610 					printf("%s: unable to allocate Tx "
    611 					    "cluster\n", sc->sc_dev.dv_xname);
    612 					m_freem(m);
    613 					break;
    614 				}
    615 			}
    616 			m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
    617 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    618 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    619 			if (m->m_pkthdr.len < ETHER_MIN_LEN) {
    620 				if (M_TRAILINGSPACE(m) < ETHER_MIN_LEN - m->m_pkthdr.len)
    621 					panic("admsw_start: M_TRAILINGSPACE\n");
    622 				memset(mtod(m, uint8_t *) + m->m_pkthdr.len, 0,
    623 				    ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);
    624 				m->m_pkthdr.len = m->m_len = ETHER_MIN_LEN;
    625 			}
    626 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    627 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    628 			if (error) {
    629 				printf("%s: unable to load Tx buffer, "
    630 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    631 				break;
    632 			}
    633 		}
    634 
    635 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    636 		if (m != NULL) {
    637 			m_freem(m0);
    638 			m0 = m;
    639 		}
    640 
    641 		/*
    642 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    643 		 */
    644 
    645 		/* Sync the DMA map. */
    646 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    647 		    BUS_DMASYNC_PREWRITE);
    648 
    649 		if (dmamap->dm_nsegs != 1 && dmamap->dm_nsegs != 2)
    650 			panic("admsw_start: dm_nsegs == %d\n", dmamap->dm_nsegs);
    651 		desc->data = dmamap->dm_segs[0].ds_addr;
    652 		desc->len = len = dmamap->dm_segs[0].ds_len;
    653 		if (dmamap->dm_nsegs > 1) {
    654 			len += dmamap->dm_segs[1].ds_len;
    655 			desc->cntl = dmamap->dm_segs[1].ds_addr | ADM5120_DMA_BUF2ENABLE;
    656 		} else
    657 			desc->cntl = 0;
    658 		desc->status = (len << ADM5120_DMA_LENSHIFT) | (1 << vlan);
    659 		eh = mtod(m0, struct ether_header *);
    660 		if (ntohs(eh->ether_type) == ETHERTYPE_IP &&
    661 		    m0->m_pkthdr.csum_flags & M_CSUM_IPv4)
    662 			desc->status |= ADM5120_DMA_CSUM;
    663 		if (nexttx == ADMSW_NTXLDESC - 1)
    664 			desc->data |= ADM5120_DMA_RINGEND;
    665 		desc->data |= ADM5120_DMA_OWN;
    666 
    667 		/* Sync the descriptor. */
    668 		ADMSW_CDTXLSYNC(sc, nexttx,
    669 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    670 
    671 		REG_WRITE(SEND_TRIG_REG, 1);
    672 		/* printf("send slot %d\n",nexttx); */
    673 
    674 		/*
    675 		 * Store a pointer to the packet so we can free it later.
    676 		 */
    677 		ds->ds_mbuf = m0;
    678 
    679 		/* Advance the Tx pointer. */
    680 		sc->sc_txfree--;
    681 		sc->sc_txnext = ADMSW_NEXTTXL(nexttx);
    682 
    683 #if NBPFILTER > 0
    684 		/* Pass the packet to any BPF listeners. */
    685 		if (ifp->if_bpf)
    686 			bpf_mtap(ifp->if_bpf, m0);
    687 #endif /* NBPFILTER */
    688 
    689 		/* Set a watchdog timer in case the chip flakes out. */
    690 		sc->sc_ethercom[0].ec_if.if_timer = 5;
    691 	}
    692 }
    693 
    694 /*
    695  * admsw_watchdog:	[ifnet interface function]
    696  *
    697  *	Watchdog timer handler.
    698  */
    699 static void
    700 admsw_watchdog(struct ifnet *ifp)
    701 {
    702 	struct admsw_softc *sc = ifp->if_softc;
    703 	int vlan;
    704 
    705 #if 1
    706 	/* Check if an interrupt was lost. */
    707 	if (sc->sc_txfree == ADMSW_NTXLDESC) {
    708 		printf("%s: watchdog false alarm\n", sc->sc_dev.dv_xname);
    709 		return;
    710 	}
    711 	if (sc->sc_ethercom[0].ec_if.if_timer != 0)
    712 		printf("%s: watchdog timer is %d!\n", sc->sc_dev.dv_xname, sc->sc_ethercom[0].ec_if.if_timer);
    713 	admsw_txintr(sc, 0);
    714 	if (sc->sc_txfree == ADMSW_NTXLDESC) {
    715 		printf("%s: tx IRQ lost (queue empty)\n", sc->sc_dev.dv_xname);
    716 		return;
    717 	}
    718 	if (sc->sc_ethercom[0].ec_if.if_timer != 0) {
    719 		printf("%s: tx IRQ lost (timer recharged)\n", sc->sc_dev.dv_xname);
    720 		return;
    721 	}
    722 #endif
    723 
    724 	printf("%s: device timeout, txfree = %d\n", sc->sc_dev.dv_xname, sc->sc_txfree);
    725 	for (vlan = 0; vlan < SW_DEVS; vlan++)
    726 		admsw_stop(&sc->sc_ethercom[vlan].ec_if, 0);
    727 	for (vlan = 0; vlan < SW_DEVS; vlan++)
    728 		(void) admsw_init(&sc->sc_ethercom[vlan].ec_if);
    729 
    730 	/* Try to get more packets going. */
    731 	admsw_start(ifp);
    732 }
    733 
    734 /*
    735  * admsw_ioctl:		[ifnet interface function]
    736  *
    737  *	Handle control requests from the operator.
    738  */
    739 static int
    740 admsw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    741 {
    742 	struct admsw_softc *sc = ifp->if_softc;
    743 	struct ifdrv *ifd;
    744 	int s, error, port;
    745 
    746 	s = splnet();
    747 
    748 	switch (cmd) {
    749 	case SIOCSIFMEDIA:
    750 	case SIOCGIFMEDIA:
    751 		port = (struct ethercom *)ifp - sc->sc_ethercom;	/* XXX */
    752 		if (port >= SW_DEVS)
    753 			error = EOPNOTSUPP;
    754 		else
    755 			error = ifmedia_ioctl(ifp, (struct ifreq *)data,
    756 			    &sc->sc_ifmedia[port], cmd);
    757 		break;
    758 
    759 	case SIOCGDRVSPEC:
    760 	case SIOCSDRVSPEC:
    761 		ifd = (struct ifdrv *) data;
    762 		if (ifd->ifd_cmd != 0 || ifd->ifd_len != sizeof(vlan_matrix)) {
    763 			error = EINVAL;
    764 			break;
    765 		}
    766 		if (cmd == SIOCGDRVSPEC) {
    767 			error = copyout(vlan_matrix, ifd->ifd_data,
    768 			    sizeof(vlan_matrix));
    769 		} else {
    770 			error = copyin(ifd->ifd_data, vlan_matrix,
    771 			    sizeof(vlan_matrix));
    772 			admsw_setvlan(sc, vlan_matrix);
    773 		}
    774 		break;
    775 
    776 	default:
    777 		error = ether_ioctl(ifp, cmd, data);
    778 		if (error == ENETRESET) {
    779 			/*
    780 			 * Multicast list has changed; set the hardware filter
    781 			 * accordingly.
    782 			 */
    783 			admsw_set_filter(sc);
    784 			error = 0;
    785 		}
    786 		break;
    787 	}
    788 
    789 	/* Try to get more packets going. */
    790 	admsw_start(ifp);
    791 
    792 	splx(s);
    793 	return (error);
    794 }
    795 
    796 
    797 /*
    798  * admsw_intr:
    799  *
    800  *	Interrupt service routine.
    801  */
    802 static int
    803 admsw_intr(void *arg)
    804 {
    805 	struct admsw_softc *sc = arg;
    806 	uint32_t pending;
    807 	char buf[64];
    808 
    809 	pending = REG_READ(ADMSW_INT_ST);
    810 
    811 	if ((pending & ~(ADMSW_INTR_RHD|ADMSW_INTR_RLD|ADMSW_INTR_SHD|ADMSW_INTR_SLD|ADMSW_INTR_W1TE|ADMSW_INTR_W0TE)) != 0) {
    812 		printf("%s: pending=%s\n", __func__,
    813 		    bitmask_snprintf(pending, ADMSW_INT_FMT, buf, sizeof(buf)));
    814 	}
    815 	REG_WRITE(ADMSW_INT_ST, pending);
    816 
    817 	if (sc->ndevs == 0)
    818 		return (0);
    819 
    820 	if ((pending & ADMSW_INTR_RHD) != 0)
    821 		admsw_rxintr(sc, 1);
    822 
    823 	if ((pending & ADMSW_INTR_RLD) != 0)
    824 		admsw_rxintr(sc, 0);
    825 
    826 	if ((pending & ADMSW_INTR_SHD) != 0)
    827 		admsw_txintr(sc, 1);
    828 
    829 	if ((pending & ADMSW_INTR_SLD) != 0)
    830 		admsw_txintr(sc, 0);
    831 
    832 	return (1);
    833 }
    834 
    835 /*
    836  * admsw_txintr:
    837  *
    838  *	Helper; handle transmit interrupts.
    839  */
    840 static void
    841 admsw_txintr(struct admsw_softc *sc, int prio)
    842 {
    843 	struct ifnet *ifp;
    844 	struct admsw_desc *desc;
    845 	struct admsw_descsoft *ds;
    846 	int i, vlan;
    847 	int gotone = 0;
    848 
    849 	/* printf("txintr: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
    850 	for (i = sc->sc_txdirty; sc->sc_txfree != ADMSW_NTXLDESC;
    851 	    i = ADMSW_NEXTTXL(i)) {
    852 
    853 		ADMSW_CDTXLSYNC(sc, i,
    854 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    855 
    856 		desc = &sc->sc_txldescs[i];
    857 		ds = &sc->sc_txlsoft[i];
    858 		if (desc->data & ADM5120_DMA_OWN) {
    859 			ADMSW_CDTXLSYNC(sc, i,
    860 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    861 			break;
    862 		}
    863 
    864 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
    865 		    0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    866 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
    867 		m_freem(ds->ds_mbuf);
    868 		ds->ds_mbuf = NULL;
    869 
    870 		vlan = ffs(desc->status & 0x3f) - 1;
    871 		if (vlan < 0 || vlan >= SW_DEVS)
    872 			panic("admsw_txintr: bad vlan\n");
    873 		ifp = &sc->sc_ethercom[vlan].ec_if;
    874 		gotone = 1;
    875 		/* printf("clear tx slot %d\n",i); */
    876 
    877 		ifp->if_opackets++;
    878 
    879 		sc->sc_txfree++;
    880 	}
    881 
    882 	if (gotone) {
    883 		sc->sc_txdirty = i;
    884 #ifdef ADMSW_EVENT_COUNTERS
    885 		ADMSW_EVCNT_INCR(&sc->sc_ev_txintr);
    886 #endif
    887 		for (vlan = 0; vlan < SW_DEVS; vlan++)
    888 			sc->sc_ethercom[vlan].ec_if.if_flags &= ~IFF_OACTIVE;
    889 
    890 		ifp = &sc->sc_ethercom[0].ec_if;
    891 
    892 		/* Try to queue more packets. */
    893 		admsw_start(ifp);
    894 
    895 		/*
    896 		 * If there are no more pending transmissions,
    897 		 * cancel the watchdog timer.
    898 		 */
    899 		if (sc->sc_txfree == ADMSW_NTXLDESC)
    900 			ifp->if_timer = 0;
    901 
    902 	}
    903 
    904 	/* printf("txintr end: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
    905 }
    906 
    907 /*
    908  * admsw_rxintr:
    909  *
    910  *	Helper; handle receive interrupts.
    911  */
    912 static void
    913 admsw_rxintr(struct admsw_softc *sc, int high)
    914 {
    915 	struct ifnet *ifp;
    916 	struct admsw_descsoft *ds;
    917 	struct mbuf *m;
    918 	uint32_t stat;
    919 	int i, len, port, vlan;
    920 
    921 	/* printf("rxintr\n"); */
    922 	if (high)
    923 		panic("admsw_rxintr: high priority packet\n");
    924 
    925 #ifdef ADMSW_EVENT_COUNTERS
    926 	int pkts = 0;
    927 #endif
    928 
    929 #if 1
    930 	ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    931 	if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
    932 		ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    933 	else {
    934 		i = sc->sc_rxptr;
    935 		do {
    936 			ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    937 			i = ADMSW_NEXTRXL(i);
    938 			/* the ring is empty, just return. */
    939 			if (i == sc->sc_rxptr)
    940 				return;
    941 			ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    942 		} while (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN);
    943 		ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    944 
    945 		ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    946 		if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
    947 			ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    948 		else {
    949 			ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    950 			/* We've fallen behind the chip: catch it. */
    951 			printf("%s: RX ring resync, base=%x, work=%x, %d -> %d\n",
    952 			    sc->sc_dev.dv_xname, REG_READ(RECV_LBADDR_REG),
    953 			    REG_READ(RECV_LWADDR_REG), sc->sc_rxptr, i);
    954 			sc->sc_rxptr = i;
    955 			ADMSW_EVCNT_INCR(&sc->sc_ev_rxsync);
    956 		}
    957 	}
    958 #endif
    959 	for (i = sc->sc_rxptr;; i = ADMSW_NEXTRXL(i)) {
    960 		ds = &sc->sc_rxlsoft[i];
    961 
    962 		ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    963 
    964 		if (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN) {
    965 			ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    966 			break;
    967 		}
    968 
    969 		/* printf("process slot %d\n",i); */
    970 
    971 #ifdef ADMSW_EVENT_COUNTERS
    972 		pkts++;
    973 #endif
    974 
    975 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    976 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    977 
    978 		stat = sc->sc_rxldescs[i].status;
    979 		len = (stat & ADM5120_DMA_LEN) >> ADM5120_DMA_LENSHIFT;
    980 		len -= ETHER_CRC_LEN;
    981 		port = (stat & ADM5120_DMA_PORTID) >> ADM5120_DMA_PORTSHIFT;
    982 		for (vlan = 0; vlan < SW_DEVS; vlan++)
    983 			if ((1 << port) & vlan_matrix[vlan])
    984 				break;
    985 		if (vlan == SW_DEVS)
    986 			vlan = 0;
    987 		ifp = &sc->sc_ethercom[vlan].ec_if;
    988 
    989 		m = ds->ds_mbuf;
    990 		if (admsw_add_rxlbuf(sc, i) != 0) {
    991 			ifp->if_ierrors++;
    992 			ADMSW_INIT_RXLDESC(sc, i);
    993 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    994 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    995 			continue;
    996 		}
    997 
    998 		m->m_pkthdr.rcvif = ifp;
    999 		m->m_pkthdr.len = m->m_len = len;
   1000 		if ((stat & ADM5120_DMA_TYPE) == ADM5120_DMA_TYPE_IP) {
   1001 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1002 			if (stat & ADM5120_DMA_CSUMFAIL)
   1003 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1004 		}
   1005 #if NBPFILTER > 0
   1006 		/* Pass this up to any BPF listeners. */
   1007 		if (ifp->if_bpf)
   1008 			bpf_mtap(ifp->if_bpf, m);
   1009 #endif /* NBPFILTER > 0 */
   1010 
   1011 		/* Pass it on. */
   1012 		(*ifp->if_input)(ifp, m);
   1013 		ifp->if_ipackets++;
   1014 	}
   1015 #ifdef ADMSW_EVENT_COUNTERS
   1016 	if (pkts)
   1017 		ADMSW_EVCNT_INCR(&sc->sc_ev_rxintr);
   1018 
   1019 	if (pkts == ADMSW_NRXLDESC)
   1020 		ADMSW_EVCNT_INCR(&sc->sc_ev_rxstall);
   1021 #endif
   1022 
   1023 	/* Update the receive pointer. */
   1024 	sc->sc_rxptr = i;
   1025 }
   1026 
   1027 /*
   1028  * admsw_init:		[ifnet interface function]
   1029  *
   1030  *	Initialize the interface.  Must be called at splnet().
   1031  */
   1032 static int
   1033 admsw_init(struct ifnet *ifp)
   1034 {
   1035 	struct admsw_softc *sc = ifp->if_softc;
   1036 
   1037 	/* printf("admsw_init called\n"); */
   1038 
   1039 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1040 		if (sc->ndevs == 0) {
   1041 			admsw_init_bufs(sc);
   1042 			admsw_reset(sc);
   1043 			REG_WRITE(CPUP_CONF_REG,
   1044 			    CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
   1045 			    CPUP_CONF_DMCP_MASK);
   1046 			/* clear all pending interrupts */
   1047 			REG_WRITE(ADMSW_INT_ST, INT_MASK);
   1048 
   1049 			/* enable needed interrupts */
   1050 			REG_WRITE(ADMSW_INT_MASK, REG_READ(ADMSW_INT_MASK) &
   1051 			    ~(ADMSW_INTR_SHD | ADMSW_INTR_SLD | ADMSW_INTR_RHD |
   1052 			    ADMSW_INTR_RLD | ADMSW_INTR_HDF | ADMSW_INTR_LDF));
   1053 		}
   1054 		sc->ndevs++;
   1055 	}
   1056 
   1057 	/* Set the receive filter. */
   1058 	admsw_set_filter(sc);
   1059 
   1060 	/* mark iface as running */
   1061 	ifp->if_flags |= IFF_RUNNING;
   1062 	ifp->if_flags &= ~IFF_OACTIVE;
   1063 
   1064 	return 0;
   1065 }
   1066 
   1067 /*
   1068  * admsw_stop:		[ifnet interface function]
   1069  *
   1070  *	Stop transmission on the interface.
   1071  */
   1072 static void
   1073 admsw_stop(struct ifnet *ifp, int disable)
   1074 {
   1075 	struct admsw_softc *sc = ifp->if_softc;
   1076 
   1077 	/* printf("admsw_stop: %d\n",disable); */
   1078 
   1079 	if (!(ifp->if_flags & IFF_RUNNING))
   1080 		return;
   1081 
   1082 	if (--sc->ndevs == 0) {
   1083 		/* printf("debug: de-initializing hardware\n"); */
   1084 
   1085 		/* disable cpu port */
   1086 		REG_WRITE(CPUP_CONF_REG,
   1087 				CPUP_CONF_DCPUP | CPUP_CONF_CRCP |
   1088 				CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK);
   1089 
   1090 		/* XXX We should disable, then clear? --dyoung */
   1091 		/* clear all pending interrupts */
   1092 		REG_WRITE(ADMSW_INT_ST, INT_MASK);
   1093 
   1094 		/* disable interrupts */
   1095 		REG_WRITE(ADMSW_INT_MASK, INT_MASK);
   1096 	}
   1097 
   1098 	/* Mark the interface as down and cancel the watchdog timer. */
   1099 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1100 	ifp->if_timer = 0;
   1101 
   1102 	return;
   1103 }
   1104 
   1105 /*
   1106  * admsw_set_filter:
   1107  *
   1108  *	Set up the receive filter.
   1109  */
   1110 static void
   1111 admsw_set_filter(struct admsw_softc *sc)
   1112 {
   1113 	int i;
   1114 	uint32_t allmc, anymc, conf, promisc;
   1115 	struct ether_multi *enm;
   1116 	struct ethercom *ec;
   1117 	struct ifnet *ifp;
   1118 	struct ether_multistep step;
   1119 
   1120 	/* Find which ports should be operated in promisc mode. */
   1121 	allmc = anymc = promisc = 0;
   1122 	for (i = 0; i < SW_DEVS; i++) {
   1123 		ec = &sc->sc_ethercom[i];
   1124 		ifp = &ec->ec_if;
   1125 		if (ifp->if_flags & IFF_PROMISC)
   1126 			promisc |= vlan_matrix[i];
   1127 
   1128 		ifp->if_flags &= ~IFF_ALLMULTI;
   1129 
   1130 		ETHER_FIRST_MULTI(step, ec, enm);
   1131 		while (enm != NULL) {
   1132 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1133 			    ETHER_ADDR_LEN) != 0) {
   1134 				printf("%s: punting on mcast range\n",
   1135 				    __func__);
   1136 				ifp->if_flags |= IFF_ALLMULTI;
   1137 				allmc |= vlan_matrix[i];
   1138 				break;
   1139 			}
   1140 
   1141 			anymc |= vlan_matrix[i];
   1142 
   1143 #if 0
   1144 			/* XXX extract subroutine --dyoung */
   1145 			REG_WRITE(MAC_WT1_REG,
   1146 			    enm->enm_addrlo[2] |
   1147 			    (enm->enm_addrlo[3] << 8) |
   1148 			    (enm->enm_addrlo[4] << 16) |
   1149 			    (enm->enm_addrlo[5] << 24));
   1150 			REG_WRITE(MAC_WT0_REG,
   1151 			    (i << MAC_WT0_VLANID_SHIFT) |
   1152 			    (enm->enm_addrlo[0] << 16) |
   1153 			    (enm->enm_addrlo[1] << 24) |
   1154 			    MAC_WT0_WRITE | MAC_WT0_VLANID_EN);
   1155 			/* timeout? */
   1156 			while (!(REG_READ(MAC_WT0_REG) & MAC_WT0_WRITE_DONE));
   1157 #endif
   1158 
   1159 			/* load h/w with mcast address, port = CPU */
   1160 			ETHER_NEXT_MULTI(step, enm);
   1161 		}
   1162 	}
   1163 
   1164 	conf = REG_READ(CPUP_CONF_REG);
   1165         /* 1 Disable forwarding of unknown & multicast packets to
   1166          *   CPU on all ports.
   1167          * 2 Enable forwarding of unknown & multicast packets to
   1168          *   CPU on ports where IFF_PROMISC or IFF_ALLMULTI is set.
   1169 	 */
   1170 	conf |= CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK;
   1171 	/* Enable forwarding of unknown packets to CPU on selected ports. */
   1172 	conf ^= ((promisc << CPUP_CONF_DUNP_SHIFT) & CPUP_CONF_DUNP_MASK);
   1173 	conf ^= ((allmc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
   1174 	conf ^= ((anymc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
   1175 	REG_WRITE(CPUP_CONF_REG, conf);
   1176 }
   1177 
   1178 /*
   1179  * admsw_add_rxbuf:
   1180  *
   1181  *	Add a receive buffer to the indicated descriptor.
   1182  */
   1183 int
   1184 admsw_add_rxbuf(struct admsw_softc *sc, int idx, int high)
   1185 {
   1186 	struct admsw_descsoft *ds;
   1187 	struct mbuf *m;
   1188 	int error;
   1189 
   1190 	if (high)
   1191 		ds = &sc->sc_rxhsoft[idx];
   1192 	else
   1193 		ds = &sc->sc_rxlsoft[idx];
   1194 
   1195 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1196 	if (m == NULL)
   1197 		return (ENOBUFS);
   1198 
   1199 	MCLGET(m, M_DONTWAIT);
   1200 	if ((m->m_flags & M_EXT) == 0) {
   1201 		m_freem(m);
   1202 		return (ENOBUFS);
   1203 	}
   1204 
   1205 	if (ds->ds_mbuf != NULL)
   1206 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1207 
   1208 	ds->ds_mbuf = m;
   1209 
   1210 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1211 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1212 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1213 	if (error) {
   1214 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1215 		    sc->sc_dev.dv_xname, idx, error);
   1216 		panic("admsw_add_rxbuf");	/* XXX */
   1217 	}
   1218 
   1219 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1220 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1221 
   1222 	if (high)
   1223 		ADMSW_INIT_RXHDESC(sc, idx);
   1224 	else
   1225 		ADMSW_INIT_RXLDESC(sc, idx);
   1226 
   1227 	return (0);
   1228 }
   1229 
   1230 int
   1231 admsw_mediachange(struct ifnet *ifp)
   1232 {
   1233 	struct admsw_softc *sc = ifp->if_softc;
   1234 	int port = (struct ethercom *)ifp - sc->sc_ethercom;	/* XXX */
   1235 	struct ifmedia *ifm = &sc->sc_ifmedia[port];
   1236 	int old, new, val;
   1237 
   1238 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1239 		return (EINVAL);
   1240 
   1241 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
   1242 		val = PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX;
   1243 	} else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
   1244 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
   1245 			val = PHY_CNTL2_100M|PHY_CNTL2_FDX;
   1246 		else
   1247 			val = PHY_CNTL2_100M;
   1248 	} else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
   1249 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
   1250 			val = PHY_CNTL2_FDX;
   1251 		else
   1252 			val = 0;
   1253 	} else
   1254 		return (EINVAL);
   1255 
   1256 	old = REG_READ(PHY_CNTL2_REG);
   1257 	new = old & ~((PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX) << port);
   1258 	new |= (val << port);
   1259 
   1260 	if (new != old)
   1261 		REG_WRITE(PHY_CNTL2_REG, new);
   1262 
   1263 	return (0);
   1264 }
   1265 
   1266 void
   1267 admsw_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1268 {
   1269 	struct admsw_softc *sc = ifp->if_softc;
   1270 	int port = (struct ethercom *)ifp - sc->sc_ethercom;	/* XXX */
   1271 	int status;
   1272 
   1273 	ifmr->ifm_status = IFM_AVALID;
   1274 	ifmr->ifm_active = IFM_ETHER;
   1275 
   1276 	status = REG_READ(PHY_ST_REG) >> port;
   1277 
   1278 	if ((status & PHY_ST_LINKUP) == 0) {
   1279 		ifmr->ifm_active |= IFM_NONE;
   1280 		return;
   1281 	}
   1282 
   1283 	ifmr->ifm_status |= IFM_ACTIVE;
   1284 	ifmr->ifm_active |= (status & PHY_ST_100M) ? IFM_100_TX : IFM_10_T;
   1285 	if (status & PHY_ST_FDX)
   1286 		ifmr->ifm_active |= IFM_FDX;
   1287 }
   1288