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