Home | History | Annotate | Line # | Download | only in ic
elinkxl.c revision 1.134
      1  1.134   thorpej /*	$NetBSD: elinkxl.c,v 1.134 2020/01/29 14:17:27 thorpej Exp $	*/
      2    1.1      fvdl 
      3    1.1      fvdl /*-
      4    1.1      fvdl  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5    1.1      fvdl  * All rights reserved.
      6    1.1      fvdl  *
      7    1.1      fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8    1.1      fvdl  * by Frank van der Linden.
      9    1.1      fvdl  *
     10    1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     11    1.1      fvdl  * modification, are permitted provided that the following conditions
     12    1.1      fvdl  * are met:
     13    1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     14    1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     15    1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     17    1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     18    1.1      fvdl  *
     19    1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.1      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     30    1.1      fvdl  */
     31   1.60     lukem 
     32   1.60     lukem #include <sys/cdefs.h>
     33  1.134   thorpej __KERNEL_RCSID(0, "$NetBSD: elinkxl.c,v 1.134 2020/01/29 14:17:27 thorpej Exp $");
     34    1.1      fvdl 
     35    1.1      fvdl #include <sys/param.h>
     36    1.1      fvdl #include <sys/systm.h>
     37   1.30   thorpej #include <sys/callout.h>
     38    1.1      fvdl #include <sys/kernel.h>
     39    1.1      fvdl #include <sys/mbuf.h>
     40    1.1      fvdl #include <sys/socket.h>
     41    1.1      fvdl #include <sys/ioctl.h>
     42    1.1      fvdl #include <sys/errno.h>
     43    1.1      fvdl #include <sys/syslog.h>
     44    1.1      fvdl #include <sys/select.h>
     45    1.1      fvdl #include <sys/device.h>
     46  1.117  riastrad #include <sys/rndsource.h>
     47    1.1      fvdl 
     48    1.1      fvdl #include <net/if.h>
     49    1.1      fvdl #include <net/if_dl.h>
     50    1.1      fvdl #include <net/if_ether.h>
     51    1.1      fvdl #include <net/if_media.h>
     52    1.1      fvdl #include <net/bpf.h>
     53    1.1      fvdl 
     54   1.99        ad #include <sys/cpu.h>
     55   1.99        ad #include <sys/bus.h>
     56   1.99        ad #include <sys/intr.h>
     57   1.21   thorpej #include <machine/endian.h>
     58    1.1      fvdl 
     59    1.1      fvdl #include <dev/mii/miivar.h>
     60    1.1      fvdl #include <dev/mii/mii.h>
     61   1.19   thorpej #include <dev/mii/mii_bitbang.h>
     62    1.1      fvdl 
     63    1.1      fvdl #include <dev/ic/elink3reg.h>
     64    1.1      fvdl /* #include <dev/ic/elink3var.h> */
     65    1.1      fvdl #include <dev/ic/elinkxlreg.h>
     66    1.1      fvdl #include <dev/ic/elinkxlvar.h>
     67    1.1      fvdl 
     68    1.1      fvdl #ifdef DEBUG
     69    1.1      fvdl int exdebug = 0;
     70    1.1      fvdl #endif
     71    1.1      fvdl 
     72    1.1      fvdl /* ifmedia callbacks */
     73   1.76     perry int ex_media_chg(struct ifnet *ifp);
     74   1.76     perry void ex_media_stat(struct ifnet *ifp, struct ifmediareq *req);
     75    1.1      fvdl 
     76  1.106    dyoung static int ex_ifflags_cb(struct ethercom *);
     77  1.106    dyoung 
     78   1.76     perry void ex_probe_media(struct ex_softc *);
     79   1.76     perry void ex_set_filter(struct ex_softc *);
     80   1.76     perry void ex_set_media(struct ex_softc *);
     81  1.102    cegger void ex_set_xcvr(struct ex_softc *, uint16_t);
     82   1.76     perry struct mbuf *ex_get(struct ex_softc *, int);
     83  1.102    cegger uint16_t ex_read_eeprom(struct ex_softc *, int);
     84   1.76     perry int ex_init(struct ifnet *);
     85   1.76     perry void ex_read(struct ex_softc *);
     86   1.76     perry void ex_reset(struct ex_softc *);
     87   1.76     perry void ex_set_mc(struct ex_softc *);
     88   1.76     perry void ex_getstats(struct ex_softc *);
     89   1.76     perry void ex_tick(void *);
     90   1.76     perry 
     91   1.76     perry static int ex_eeprom_busy(struct ex_softc *);
     92   1.76     perry static int ex_add_rxbuf(struct ex_softc *, struct ex_rxdesc *);
     93   1.76     perry static void ex_init_txdescs(struct ex_softc *);
     94   1.76     perry 
     95   1.92     itohy static void ex_setup_tx(struct ex_softc *);
     96  1.103       spz static bool ex_shutdown(device_t, int);
     97   1.76     perry static void ex_start(struct ifnet *);
     98   1.76     perry static void ex_txstat(struct ex_softc *);
     99   1.76     perry 
    100  1.126   msaitoh int ex_mii_readreg(device_t, int, int, uint16_t *);
    101  1.126   msaitoh int ex_mii_writereg(device_t, int, int, uint16_t);
    102  1.115      matt void ex_mii_statchg(struct ifnet *);
    103    1.1      fvdl 
    104   1.76     perry void ex_probemedia(struct ex_softc *);
    105    1.2   thorpej 
    106    1.2   thorpej /*
    107    1.2   thorpej  * Structure to map media-present bits in boards to ifmedia codes and
    108    1.2   thorpej  * printable media names.  Used for table-driven ifmedia initialization.
    109    1.2   thorpej  */
    110    1.2   thorpej struct ex_media {
    111    1.2   thorpej 	int	exm_mpbit;		/* media present bit */
    112    1.2   thorpej 	const char *exm_name;		/* name of medium */
    113    1.2   thorpej 	int	exm_ifmedia;		/* ifmedia word for medium */
    114    1.2   thorpej 	int	exm_epmedia;		/* ELINKMEDIA_* constant */
    115    1.2   thorpej };
    116    1.2   thorpej 
    117    1.2   thorpej /*
    118    1.2   thorpej  * Media table for 3c90x chips.  Note that chips with MII have no
    119    1.2   thorpej  * `native' media.
    120    1.2   thorpej  */
    121  1.123      maxv static const struct ex_media ex_native_media[] = {
    122  1.129   msaitoh 	{ ELINK_PCI_10BASE_T,	"10baseT",	IFM_ETHER | IFM_10_T,
    123    1.2   thorpej 	  ELINKMEDIA_10BASE_T },
    124  1.129   msaitoh 	{ ELINK_PCI_10BASE_T,	"10baseT-FDX",	IFM_ETHER | IFM_10_T | IFM_FDX,
    125    1.2   thorpej 	  ELINKMEDIA_10BASE_T },
    126  1.129   msaitoh 	{ ELINK_PCI_AUI,	"10base5",	IFM_ETHER | IFM_10_5,
    127    1.2   thorpej 	  ELINKMEDIA_AUI },
    128  1.129   msaitoh 	{ ELINK_PCI_BNC,	"10base2",	IFM_ETHER | IFM_10_2,
    129    1.2   thorpej 	  ELINKMEDIA_10BASE_2 },
    130  1.129   msaitoh 	{ ELINK_PCI_100BASE_TX,	"100baseTX",	IFM_ETHER | IFM_100_TX,
    131    1.2   thorpej 	  ELINKMEDIA_100BASE_TX },
    132  1.129   msaitoh 	{ ELINK_PCI_100BASE_TX,	"100baseTX-FDX",IFM_ETHER | IFM_100_TX|IFM_FDX,
    133    1.2   thorpej 	  ELINKMEDIA_100BASE_TX },
    134  1.129   msaitoh 	{ ELINK_PCI_100BASE_FX,	"100baseFX",	IFM_ETHER | IFM_100_FX,
    135    1.2   thorpej 	  ELINKMEDIA_100BASE_FX },
    136  1.129   msaitoh 	{ ELINK_PCI_100BASE_MII,"manual",	IFM_ETHER | IFM_MANUAL,
    137    1.2   thorpej 	  ELINKMEDIA_MII },
    138  1.129   msaitoh 	{ ELINK_PCI_100BASE_T4,	"100baseT4",	IFM_ETHER | IFM_100_T4,
    139    1.2   thorpej 	  ELINKMEDIA_100BASE_T4 },
    140    1.2   thorpej 	{ 0,			NULL,		0,
    141    1.2   thorpej 	  0 },
    142    1.2   thorpej };
    143    1.2   thorpej 
    144    1.1      fvdl /*
    145   1.19   thorpej  * MII bit-bang glue.
    146   1.19   thorpej  */
    147  1.103       spz uint32_t ex_mii_bitbang_read(device_t);
    148  1.103       spz void ex_mii_bitbang_write(device_t, uint32_t);
    149   1.19   thorpej 
    150   1.19   thorpej const struct mii_bitbang_ops ex_mii_bitbang_ops = {
    151   1.19   thorpej 	ex_mii_bitbang_read,
    152   1.19   thorpej 	ex_mii_bitbang_write,
    153   1.19   thorpej 	{
    154   1.19   thorpej 		ELINK_PHY_DATA,		/* MII_BIT_MDO */
    155   1.19   thorpej 		ELINK_PHY_DATA,		/* MII_BIT_MDI */
    156   1.19   thorpej 		ELINK_PHY_CLK,		/* MII_BIT_MDC */
    157   1.19   thorpej 		ELINK_PHY_DIR,		/* MII_BIT_DIR_HOST_PHY */
    158   1.19   thorpej 		0,			/* MII_BIT_DIR_PHY_HOST */
    159   1.19   thorpej 	}
    160   1.19   thorpej };
    161   1.19   thorpej 
    162   1.19   thorpej /*
    163    1.1      fvdl  * Back-end attach and configure.
    164    1.1      fvdl  */
    165    1.1      fvdl void
    166  1.102    cegger ex_config(struct ex_softc *sc)
    167    1.1      fvdl {
    168    1.1      fvdl 	struct ifnet *ifp;
    169  1.129   msaitoh 	struct mii_data * const mii = &sc->ex_mii;
    170  1.102    cegger 	uint16_t val;
    171  1.102    cegger 	uint8_t macaddr[ETHER_ADDR_LEN] = {0};
    172    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
    173    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
    174   1.25  augustss 	int i, error, attach_stage;
    175    1.1      fvdl 
    176  1.111    dyoung 	pmf_self_suspensor_init(sc->sc_dev, &sc->sc_suspensor, &sc->sc_qual);
    177  1.111    dyoung 
    178   1.97        ad 	callout_init(&sc->ex_mii_callout, 0);
    179   1.30   thorpej 
    180    1.1      fvdl 	ex_reset(sc);
    181    1.1      fvdl 
    182    1.1      fvdl 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR0);
    183    1.1      fvdl 	macaddr[0] = val >> 8;
    184    1.1      fvdl 	macaddr[1] = val & 0xff;
    185    1.1      fvdl 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR1);
    186    1.1      fvdl 	macaddr[2] = val >> 8;
    187    1.1      fvdl 	macaddr[3] = val & 0xff;
    188    1.1      fvdl 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR2);
    189    1.1      fvdl 	macaddr[4] = val >> 8;
    190    1.1      fvdl 	macaddr[5] = val & 0xff;
    191    1.1      fvdl 
    192  1.129   msaitoh 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
    193  1.129   msaitoh 	    ether_sprintf(macaddr));
    194    1.1      fvdl 
    195  1.129   msaitoh 	if (sc->ex_conf & (EX_CONF_INV_LED_POLARITY | EX_CONF_PHY_POWER)) {
    196   1.40      fvdl 		GO_WINDOW(2);
    197   1.40      fvdl 		val = bus_space_read_2(iot, ioh, ELINK_W2_RESET_OPTIONS);
    198   1.40      fvdl 		if (sc->ex_conf & EX_CONF_INV_LED_POLARITY)
    199   1.40      fvdl 			val |= ELINK_RESET_OPT_LEDPOLAR;
    200   1.40      fvdl 		if (sc->ex_conf & EX_CONF_PHY_POWER)
    201   1.40      fvdl 			val |= ELINK_RESET_OPT_PHYPOWER;
    202   1.40      fvdl 		bus_space_write_2(iot, ioh, ELINK_W2_RESET_OPTIONS, val);
    203   1.70    dogcow 	}
    204   1.70    dogcow 	if (sc->ex_conf & EX_CONF_NO_XCVR_PWR) {
    205   1.70    dogcow 		GO_WINDOW(0);
    206   1.70    dogcow 		bus_space_write_2(iot, ioh, ELINK_W0_MFG_ID,
    207   1.70    dogcow 		    EX_XCVR_PWR_MAGICBITS);
    208   1.15      haya 	}
    209   1.15      haya 
    210    1.1      fvdl 	attach_stage = 0;
    211    1.1      fvdl 
    212    1.1      fvdl 	/*
    213    1.1      fvdl 	 * Allocate the upload descriptors, and create and load the DMA
    214    1.1      fvdl 	 * map for them.
    215    1.1      fvdl 	 */
    216    1.1      fvdl 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    217   1.79     perry 	    EX_NUPD * sizeof (struct ex_upd), PAGE_SIZE, 0, &sc->sc_useg, 1,
    218  1.130   msaitoh 	    &sc->sc_urseg, BUS_DMA_NOWAIT)) != 0) {
    219  1.103       spz 		aprint_error_dev(sc->sc_dev,
    220  1.103       spz 		    "can't allocate upload descriptors, error = %d\n", error);
    221    1.1      fvdl 		goto fail;
    222    1.1      fvdl 	}
    223    1.1      fvdl 
    224    1.1      fvdl 	attach_stage = 1;
    225    1.1      fvdl 
    226   1.25  augustss 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg,
    227   1.96  christos 	    EX_NUPD * sizeof (struct ex_upd), (void **)&sc->sc_upd,
    228  1.129   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    229  1.103       spz 		aprint_error_dev(sc->sc_dev,
    230  1.103       spz 		    "can't map upload descriptors, error = %d\n", error);
    231    1.1      fvdl 		goto fail;
    232    1.1      fvdl 	}
    233    1.1      fvdl 
    234    1.1      fvdl 	attach_stage = 2;
    235    1.1      fvdl 
    236    1.1      fvdl 	if ((error = bus_dmamap_create(sc->sc_dmat,
    237    1.1      fvdl 	    EX_NUPD * sizeof (struct ex_upd), 1,
    238    1.1      fvdl 	    EX_NUPD * sizeof (struct ex_upd), 0, BUS_DMA_NOWAIT,
    239    1.1      fvdl 	    &sc->sc_upd_dmamap)) != 0) {
    240  1.103       spz 		aprint_error_dev(sc->sc_dev,
    241  1.103       spz 		    "can't create upload desc. DMA map, error = %d\n", error);
    242    1.1      fvdl 		goto fail;
    243    1.1      fvdl 	}
    244    1.1      fvdl 
    245    1.1      fvdl 	attach_stage = 3;
    246    1.1      fvdl 
    247    1.1      fvdl 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_upd_dmamap,
    248    1.1      fvdl 	    sc->sc_upd, EX_NUPD * sizeof (struct ex_upd), NULL,
    249    1.1      fvdl 	    BUS_DMA_NOWAIT)) != 0) {
    250  1.103       spz 		aprint_error_dev(sc->sc_dev,
    251  1.103       spz 		    "can't load upload desc. DMA map, error = %d\n", error);
    252    1.1      fvdl 		goto fail;
    253    1.1      fvdl 	}
    254    1.1      fvdl 
    255    1.1      fvdl 	attach_stage = 4;
    256    1.1      fvdl 
    257    1.1      fvdl 	/*
    258    1.1      fvdl 	 * Allocate the download descriptors, and create and load the DMA
    259    1.1      fvdl 	 * map for them.
    260    1.1      fvdl 	 */
    261    1.1      fvdl 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    262   1.95     itohy 	    DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN, PAGE_SIZE, 0, &sc->sc_dseg, 1,
    263   1.25  augustss 	    &sc->sc_drseg, BUS_DMA_NOWAIT)) != 0) {
    264  1.103       spz 		aprint_error_dev(sc->sc_dev,
    265  1.103       spz 		    "can't allocate download descriptors, error = %d\n", error);
    266    1.1      fvdl 		goto fail;
    267    1.1      fvdl 	}
    268    1.1      fvdl 
    269    1.1      fvdl 	attach_stage = 5;
    270    1.1      fvdl 
    271   1.25  augustss 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg,
    272   1.96  christos 	    DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN, (void **)&sc->sc_dpd,
    273  1.129   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    274  1.103       spz 		aprint_error_dev(sc->sc_dev,
    275  1.103       spz 		    "can't map download descriptors, error = %d\n", error);
    276    1.1      fvdl 		goto fail;
    277    1.1      fvdl 	}
    278   1.95     itohy 	memset(sc->sc_dpd, 0, DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN);
    279    1.1      fvdl 
    280    1.1      fvdl 	attach_stage = 6;
    281    1.1      fvdl 
    282    1.1      fvdl 	if ((error = bus_dmamap_create(sc->sc_dmat,
    283   1.95     itohy 	    DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN, 1,
    284   1.95     itohy 	    DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN, 0, BUS_DMA_NOWAIT,
    285    1.1      fvdl 	    &sc->sc_dpd_dmamap)) != 0) {
    286  1.103       spz 		aprint_error_dev(sc->sc_dev,
    287  1.103       spz 		    "can't create download desc. DMA map, error = %d\n", error);
    288    1.1      fvdl 		goto fail;
    289    1.1      fvdl 	}
    290    1.1      fvdl 
    291    1.1      fvdl 	attach_stage = 7;
    292    1.1      fvdl 
    293    1.1      fvdl 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dpd_dmamap,
    294   1.95     itohy 	    sc->sc_dpd, DPDMEM_SIZE + EX_IP4CSUMTX_PADLEN, NULL,
    295    1.1      fvdl 	    BUS_DMA_NOWAIT)) != 0) {
    296  1.103       spz 		aprint_error_dev(sc->sc_dev,
    297  1.103       spz 		    "can't load download desc. DMA map, error = %d\n", error);
    298    1.1      fvdl 		goto fail;
    299    1.1      fvdl 	}
    300   1.95     itohy 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
    301   1.95     itohy 	    DPDMEMPAD_OFF, EX_IP4CSUMTX_PADLEN, BUS_DMASYNC_PREWRITE);
    302    1.1      fvdl 
    303    1.1      fvdl 	attach_stage = 8;
    304    1.1      fvdl 
    305    1.1      fvdl 
    306    1.1      fvdl 	/*
    307    1.1      fvdl 	 * Create the transmit buffer DMA maps.
    308    1.1      fvdl 	 */
    309    1.1      fvdl 	for (i = 0; i < EX_NDPD; i++) {
    310    1.1      fvdl 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    311    1.1      fvdl 		    EX_NTFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    312    1.1      fvdl 		    &sc->sc_tx_dmamaps[i])) != 0) {
    313  1.103       spz 			aprint_error_dev(sc->sc_dev,
    314  1.101    cegger 			    "can't create tx DMA map %d, error = %d\n",
    315  1.101    cegger 			    i, error);
    316    1.1      fvdl 			goto fail;
    317    1.1      fvdl 		}
    318    1.1      fvdl 	}
    319    1.1      fvdl 
    320    1.1      fvdl 	attach_stage = 9;
    321    1.1      fvdl 
    322    1.1      fvdl 	/*
    323    1.1      fvdl 	 * Create the receive buffer DMA maps.
    324    1.1      fvdl 	 */
    325    1.1      fvdl 	for (i = 0; i < EX_NUPD; i++) {
    326    1.1      fvdl 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    327    1.1      fvdl 		    EX_NRFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    328    1.1      fvdl 		    &sc->sc_rx_dmamaps[i])) != 0) {
    329  1.103       spz 			aprint_error_dev(sc->sc_dev,
    330  1.101    cegger 			    "can't create rx DMA map %d, error = %d\n",
    331  1.101    cegger 			    i, error);
    332    1.1      fvdl 			goto fail;
    333    1.1      fvdl 		}
    334    1.1      fvdl 	}
    335    1.1      fvdl 
    336    1.1      fvdl 	attach_stage = 10;
    337    1.1      fvdl 
    338    1.1      fvdl 	/*
    339    1.1      fvdl 	 * Create ring of upload descriptors, only once. The DMA engine
    340    1.1      fvdl 	 * will loop over this when receiving packets, stalling if it
    341    1.1      fvdl 	 * hits an UPD with a finished receive.
    342    1.1      fvdl 	 */
    343    1.1      fvdl 	for (i = 0; i < EX_NUPD; i++) {
    344    1.1      fvdl 		sc->sc_rxdescs[i].rx_dmamap = sc->sc_rx_dmamaps[i];
    345    1.1      fvdl 		sc->sc_rxdescs[i].rx_upd = &sc->sc_upd[i];
    346    1.9   thorpej 		sc->sc_upd[i].upd_frags[0].fr_len =
    347   1.21   thorpej 		    htole32((MCLBYTES - 2) | EX_FR_LAST);
    348    1.1      fvdl 		if (ex_add_rxbuf(sc, &sc->sc_rxdescs[i]) != 0) {
    349  1.103       spz 			aprint_error_dev(sc->sc_dev,
    350  1.103       spz 			    "can't allocate or map rx buffers\n");
    351    1.1      fvdl 			goto fail;
    352    1.1      fvdl 		}
    353    1.1      fvdl 	}
    354    1.1      fvdl 
    355    1.1      fvdl 	bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap, 0,
    356    1.1      fvdl 	    EX_NUPD * sizeof (struct ex_upd),
    357  1.129   msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    358    1.1      fvdl 
    359    1.1      fvdl 	ex_init_txdescs(sc);
    360    1.1      fvdl 
    361    1.1      fvdl 	attach_stage = 11;
    362    1.1      fvdl 
    363    1.1      fvdl 
    364    1.1      fvdl 	GO_WINDOW(3);
    365    1.1      fvdl 	val = bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
    366    1.1      fvdl 	if (val & ELINK_MEDIACAP_MII)
    367    1.1      fvdl 		sc->ex_conf |= EX_CONF_MII;
    368    1.1      fvdl 
    369    1.1      fvdl 	ifp = &sc->sc_ethercom.ec_if;
    370    1.1      fvdl 
    371    1.2   thorpej 	/*
    372    1.2   thorpej 	 * Initialize our media structures and MII info.  We'll
    373    1.2   thorpej 	 * probe the MII if we discover that we have one.
    374    1.2   thorpej 	 */
    375  1.129   msaitoh 	mii->mii_ifp = ifp;
    376  1.129   msaitoh 	mii->mii_readreg = ex_mii_readreg;
    377  1.129   msaitoh 	mii->mii_writereg = ex_mii_writereg;
    378  1.129   msaitoh 	mii->mii_statchg = ex_mii_statchg;
    379  1.129   msaitoh 	sc->sc_ethercom.ec_mii = mii;
    380  1.129   msaitoh 	ifmedia_init(&mii->mii_media, IFM_IMASK, ex_media_chg, ex_media_stat);
    381    1.2   thorpej 
    382    1.1      fvdl 	if (sc->ex_conf & EX_CONF_MII) {
    383    1.1      fvdl 		/*
    384    1.1      fvdl 		 * Find PHY, extract media information from it.
    385   1.14      fvdl 		 * First, select the right transceiver.
    386    1.1      fvdl 		 */
    387   1.69  christos 		ex_set_xcvr(sc, val);
    388   1.14      fvdl 
    389  1.129   msaitoh 		mii_attach(sc->sc_dev, mii, 0xffffffff,
    390   1.24   thorpej 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    391  1.129   msaitoh 		if (LIST_FIRST(&mii->mii_phys) == NULL) {
    392  1.129   msaitoh 			ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE,
    393    1.1      fvdl 			    0, NULL);
    394  1.129   msaitoh 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    395    1.1      fvdl 		} else {
    396  1.129   msaitoh 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    397    1.1      fvdl 		}
    398    1.2   thorpej 	} else
    399    1.2   thorpej 		ex_probemedia(sc);
    400    1.1      fvdl 
    401  1.104    cegger 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    402    1.1      fvdl 	ifp->if_softc = sc;
    403    1.1      fvdl 	ifp->if_start = ex_start;
    404    1.1      fvdl 	ifp->if_ioctl = ex_ioctl;
    405    1.1      fvdl 	ifp->if_watchdog = ex_watchdog;
    406   1.42   thorpej 	ifp->if_init = ex_init;
    407   1.42   thorpej 	ifp->if_stop = ex_stop;
    408  1.127   msaitoh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    409   1.77       kim 	sc->sc_if_flags = ifp->if_flags;
    410   1.46   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    411    1.1      fvdl 
    412   1.43    bouyer 	/*
    413   1.43    bouyer 	 * We can support 802.1Q VLAN-sized frames.
    414   1.43    bouyer 	 */
    415   1.43    bouyer 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    416   1.43    bouyer 
    417   1.50   thorpej 	/*
    418   1.50   thorpej 	 * The 3c90xB has hardware IPv4/TCPv4/UDPv4 checksum support.
    419   1.50   thorpej 	 */
    420   1.50   thorpej 	if (sc->ex_conf & EX_CONF_90XB)
    421   1.80      yamt 		sc->sc_ethercom.ec_if.if_capabilities |=
    422   1.80      yamt 		    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    423   1.80      yamt 		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    424   1.80      yamt 		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    425   1.50   thorpej 
    426    1.1      fvdl 	if_attach(ifp);
    427  1.121     ozaki 	if_deferred_start_init(ifp, NULL);
    428    1.1      fvdl 	ether_ifattach(ifp, macaddr);
    429  1.106    dyoung 	ether_set_ifflags_cb(&sc->sc_ethercom, ex_ifflags_cb);
    430    1.1      fvdl 
    431    1.1      fvdl 	GO_WINDOW(1);
    432    1.1      fvdl 
    433    1.1      fvdl 	sc->tx_start_thresh = 20;
    434    1.1      fvdl 	sc->tx_succ_ok = 0;
    435    1.1      fvdl 
    436    1.1      fvdl 	/* TODO: set queues to 0 */
    437    1.1      fvdl 
    438  1.103       spz 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    439  1.116       tls 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    440    1.1      fvdl 
    441  1.108   tsutsui 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, ex_shutdown))
    442  1.108   tsutsui 		pmf_class_network_register(sc->sc_dev, &sc->sc_ethercom.ec_if);
    443  1.103       spz 	else
    444  1.108   tsutsui 		aprint_error_dev(sc->sc_dev,
    445  1.108   tsutsui 		    "couldn't establish power handler\n");
    446   1.34     jhawk 
    447   1.34     jhawk 	/* The attach is successful. */
    448   1.34     jhawk 	sc->ex_flags |= EX_FLAGS_ATTACHED;
    449    1.1      fvdl 	return;
    450    1.1      fvdl 
    451    1.1      fvdl  fail:
    452    1.1      fvdl 	/*
    453    1.1      fvdl 	 * Free any resources we've allocated during the failed attach
    454    1.1      fvdl 	 * attempt.  Do this in reverse order and fall though.
    455    1.1      fvdl 	 */
    456    1.1      fvdl 	switch (attach_stage) {
    457    1.1      fvdl 	case 11:
    458    1.1      fvdl 	    {
    459    1.1      fvdl 		struct ex_rxdesc *rxd;
    460    1.1      fvdl 
    461    1.1      fvdl 		for (i = 0; i < EX_NUPD; i++) {
    462    1.1      fvdl 			rxd = &sc->sc_rxdescs[i];
    463    1.1      fvdl 			if (rxd->rx_mbhead != NULL) {
    464    1.1      fvdl 				bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
    465    1.1      fvdl 				m_freem(rxd->rx_mbhead);
    466    1.1      fvdl 			}
    467    1.1      fvdl 		}
    468    1.1      fvdl 	    }
    469    1.1      fvdl 		/* FALLTHROUGH */
    470    1.1      fvdl 
    471    1.1      fvdl 	case 10:
    472    1.1      fvdl 		for (i = 0; i < EX_NUPD; i++)
    473    1.1      fvdl 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
    474    1.1      fvdl 		/* FALLTHROUGH */
    475    1.1      fvdl 
    476    1.1      fvdl 	case 9:
    477    1.1      fvdl 		for (i = 0; i < EX_NDPD; i++)
    478    1.1      fvdl 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
    479    1.1      fvdl 		/* FALLTHROUGH */
    480    1.1      fvdl 	case 8:
    481    1.1      fvdl 		bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
    482    1.1      fvdl 		/* FALLTHROUGH */
    483    1.1      fvdl 
    484    1.1      fvdl 	case 7:
    485    1.1      fvdl 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
    486    1.1      fvdl 		/* FALLTHROUGH */
    487    1.1      fvdl 
    488    1.1      fvdl 	case 6:
    489   1.96  christos 		bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_dpd,
    490    1.1      fvdl 		    EX_NDPD * sizeof (struct ex_dpd));
    491    1.1      fvdl 		/* FALLTHROUGH */
    492    1.1      fvdl 
    493    1.1      fvdl 	case 5:
    494   1.25  augustss 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
    495    1.1      fvdl 		break;
    496    1.1      fvdl 
    497    1.1      fvdl 	case 4:
    498    1.1      fvdl 		bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
    499    1.1      fvdl 		/* FALLTHROUGH */
    500    1.1      fvdl 
    501    1.1      fvdl 	case 3:
    502    1.1      fvdl 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
    503    1.1      fvdl 		/* FALLTHROUGH */
    504    1.1      fvdl 
    505    1.1      fvdl 	case 2:
    506   1.96  christos 		bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_upd,
    507    1.1      fvdl 		    EX_NUPD * sizeof (struct ex_upd));
    508    1.1      fvdl 		/* FALLTHROUGH */
    509    1.1      fvdl 
    510    1.1      fvdl 	case 1:
    511   1.25  augustss 		bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
    512    1.1      fvdl 		break;
    513    1.1      fvdl 	}
    514    1.1      fvdl 
    515    1.2   thorpej }
    516    1.2   thorpej 
    517    1.2   thorpej /*
    518    1.2   thorpej  * Find the media present on non-MII chips.
    519    1.2   thorpej  */
    520    1.2   thorpej void
    521  1.102    cegger ex_probemedia(struct ex_softc *sc)
    522    1.2   thorpej {
    523    1.2   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    524    1.2   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    525    1.2   thorpej 	struct ifmedia *ifm = &sc->ex_mii.mii_media;
    526  1.123      maxv 	const struct ex_media *exm;
    527  1.102    cegger 	uint16_t config1, reset_options, default_media;
    528    1.2   thorpej 	int defmedia = 0;
    529    1.2   thorpej 	const char *sep = "", *defmedianame = NULL;
    530    1.2   thorpej 
    531    1.2   thorpej 	GO_WINDOW(3);
    532    1.2   thorpej 	config1 = bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2);
    533    1.2   thorpej 	reset_options = bus_space_read_1(iot, ioh, ELINK_W3_RESET_OPTIONS);
    534    1.2   thorpej 	GO_WINDOW(0);
    535    1.2   thorpej 
    536    1.2   thorpej 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    537    1.2   thorpej 
    538    1.2   thorpej 	/* Sanity check that there are any media! */
    539    1.2   thorpej 	if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
    540  1.103       spz 		aprint_error_dev(sc->sc_dev, "no media present!\n");
    541  1.129   msaitoh 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
    542  1.129   msaitoh 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
    543    1.2   thorpej 		return;
    544    1.2   thorpej 	}
    545    1.2   thorpej 
    546  1.103       spz 	aprint_normal_dev(sc->sc_dev, "");
    547  1.103       spz 
    548   1.68   thorpej #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
    549    1.2   thorpej 
    550    1.2   thorpej 	for (exm = ex_native_media; exm->exm_name != NULL; exm++) {
    551    1.2   thorpej 		if (reset_options & exm->exm_mpbit) {
    552    1.2   thorpej 			/*
    553    1.2   thorpej 			 * Default media is a little complicated.  We
    554    1.2   thorpej 			 * support full-duplex which uses the same
    555    1.2   thorpej 			 * reset options bit.
    556    1.2   thorpej 			 *
    557    1.2   thorpej 			 * XXX Check EEPROM for default to FDX?
    558    1.2   thorpej 			 */
    559    1.2   thorpej 			if (exm->exm_epmedia == default_media) {
    560    1.2   thorpej 				if ((exm->exm_ifmedia & IFM_FDX) == 0) {
    561    1.2   thorpej 					defmedia = exm->exm_ifmedia;
    562    1.2   thorpej 					defmedianame = exm->exm_name;
    563    1.2   thorpej 				}
    564    1.2   thorpej 			} else if (defmedia == 0) {
    565    1.2   thorpej 				defmedia = exm->exm_ifmedia;
    566    1.2   thorpej 				defmedianame = exm->exm_name;
    567    1.2   thorpej 			}
    568    1.2   thorpej 			ifmedia_add(ifm, exm->exm_ifmedia, exm->exm_epmedia,
    569    1.2   thorpej 			    NULL);
    570    1.2   thorpej 			PRINT(exm->exm_name);
    571    1.2   thorpej 		}
    572    1.2   thorpej 	}
    573    1.2   thorpej 
    574    1.2   thorpej #undef PRINT
    575    1.2   thorpej 
    576    1.2   thorpej #ifdef DIAGNOSTIC
    577    1.2   thorpej 	if (defmedia == 0)
    578    1.2   thorpej 		panic("ex_probemedia: impossible");
    579    1.2   thorpej #endif
    580    1.2   thorpej 
    581   1.68   thorpej 	aprint_normal(", default %s\n", defmedianame);
    582    1.2   thorpej 	ifmedia_set(ifm, defmedia);
    583    1.1      fvdl }
    584    1.1      fvdl 
    585    1.1      fvdl /*
    586   1.92     itohy  * Setup transmitter parameters.
    587   1.92     itohy  */
    588   1.92     itohy static void
    589  1.102    cegger ex_setup_tx(struct ex_softc *sc)
    590   1.92     itohy {
    591   1.92     itohy 	bus_space_tag_t iot = sc->sc_iot;
    592   1.92     itohy 	bus_space_handle_t ioh = sc->sc_ioh;
    593   1.92     itohy 
    594   1.92     itohy 	/*
    595   1.92     itohy 	 * Disable reclaim threshold for 90xB, set free threshold to
    596   1.92     itohy 	 * 6 * 256 = 1536 for 90x.
    597   1.92     itohy 	 */
    598   1.92     itohy 	if (sc->ex_conf & EX_CONF_90XB)
    599   1.92     itohy 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
    600   1.92     itohy 		    ELINK_TXRECLTHRESH | 255);
    601   1.92     itohy 	else
    602   1.92     itohy 		bus_space_write_1(iot, ioh, ELINK_TXFREETHRESH, 6);
    603   1.92     itohy 
    604   1.92     itohy 	/* Setup early transmission start threshold. */
    605   1.92     itohy 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    606   1.92     itohy 	    ELINK_TXSTARTTHRESH | sc->tx_start_thresh);
    607   1.92     itohy }
    608   1.92     itohy 
    609   1.92     itohy /*
    610    1.1      fvdl  * Bring device up.
    611    1.1      fvdl  */
    612   1.42   thorpej int
    613  1.102    cegger ex_init(struct ifnet *ifp)
    614    1.1      fvdl {
    615   1.42   thorpej 	struct ex_softc *sc = ifp->if_softc;
    616    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
    617    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
    618   1.47   thorpej 	int i;
    619  1.102    cegger 	uint16_t val;
    620   1.47   thorpej 	int error = 0;
    621    1.1      fvdl 
    622   1.47   thorpej 	if ((error = ex_enable(sc)) != 0)
    623   1.47   thorpej 		goto out;
    624    1.1      fvdl 
    625    1.1      fvdl 	ex_waitcmd(sc);
    626   1.42   thorpej 	ex_stop(ifp, 0);
    627    1.1      fvdl 
    628   1.90     itohy 	GO_WINDOW(2);
    629   1.90     itohy 
    630   1.90     itohy 	/* Turn on PHY power. */
    631   1.90     itohy 	if (sc->ex_conf & (EX_CONF_PHY_POWER | EX_CONF_INV_LED_POLARITY)) {
    632   1.90     itohy 		val = bus_space_read_2(iot, ioh, ELINK_W2_RESET_OPTIONS);
    633   1.90     itohy 		if (sc->ex_conf & EX_CONF_PHY_POWER)
    634   1.90     itohy 			val |= ELINK_RESET_OPT_PHYPOWER; /* turn on PHY power */
    635   1.90     itohy 		if (sc->ex_conf & EX_CONF_INV_LED_POLARITY)
    636   1.90     itohy 			val |= ELINK_RESET_OPT_LEDPOLAR; /* invert LED polarity */
    637   1.90     itohy 		bus_space_write_2(iot, ioh, ELINK_W2_RESET_OPTIONS, val);
    638   1.90     itohy 	}
    639   1.90     itohy 
    640    1.1      fvdl 	/*
    641    1.1      fvdl 	 * Set the station address and clear the station mask. The latter
    642    1.1      fvdl 	 * is needed for 90x cards, 0 is the default for 90xB cards.
    643    1.1      fvdl 	 */
    644    1.1      fvdl 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    645    1.1      fvdl 		bus_space_write_1(iot, ioh, ELINK_W2_ADDR_0 + i,
    646   1.98    dyoung 		    CLLADDR(ifp->if_sadl)[i]);
    647    1.1      fvdl 		bus_space_write_1(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
    648    1.1      fvdl 	}
    649    1.1      fvdl 
    650    1.1      fvdl 	GO_WINDOW(3);
    651    1.1      fvdl 
    652    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_RESET);
    653    1.1      fvdl 	ex_waitcmd(sc);
    654    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_RESET);
    655    1.1      fvdl 	ex_waitcmd(sc);
    656    1.1      fvdl 
    657   1.92     itohy 	/* Load Tx parameters. */
    658   1.92     itohy 	ex_setup_tx(sc);
    659    1.1      fvdl 
    660    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    661    1.1      fvdl 	    SET_RX_EARLY_THRESH | ELINK_THRESH_DISABLE);
    662    1.1      fvdl 
    663    1.1      fvdl 	bus_space_write_4(iot, ioh, ELINK_DMACTRL,
    664    1.1      fvdl 	    bus_space_read_4(iot, ioh, ELINK_DMACTRL) | ELINK_DMAC_UPRXEAREN);
    665    1.1      fvdl 
    666   1.61  christos 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    667   1.61  christos 	    SET_RD_0_MASK | XL_WATCHED_INTERRUPTS);
    668   1.61  christos 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    669   1.61  christos 	    SET_INTR_MASK | XL_WATCHED_INTERRUPTS);
    670    1.1      fvdl 
    671    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
    672   1.15      haya 	if (sc->intr_ack)
    673   1.15      haya 	    (* sc->intr_ack)(sc);
    674    1.1      fvdl 	ex_set_media(sc);
    675    1.1      fvdl 	ex_set_mc(sc);
    676    1.1      fvdl 
    677    1.1      fvdl 
    678    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STATS_ENABLE);
    679    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    680    1.1      fvdl 	bus_space_write_4(iot, ioh, ELINK_UPLISTPTR, sc->sc_upddma);
    681    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
    682    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_UPUNSTALL);
    683   1.38      haya 
    684    1.1      fvdl 	ifp->if_flags |= IFF_RUNNING;
    685    1.1      fvdl 	ifp->if_flags &= ~IFF_OACTIVE;
    686    1.1      fvdl 	ex_start(ifp);
    687   1.77       kim 	sc->sc_if_flags = ifp->if_flags;
    688    1.1      fvdl 
    689    1.1      fvdl 	GO_WINDOW(1);
    690    1.1      fvdl 
    691   1.30   thorpej 	callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
    692   1.42   thorpej 
    693   1.47   thorpej  out:
    694   1.47   thorpej 	if (error) {
    695   1.47   thorpej 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    696   1.47   thorpej 		ifp->if_timer = 0;
    697  1.103       spz 		aprint_error_dev(sc->sc_dev, "interface not running\n");
    698   1.47   thorpej 	}
    699   1.47   thorpej 	return (error);
    700    1.1      fvdl }
    701    1.1      fvdl 
    702   1.67     enami #define	MCHASHSIZE		256
    703   1.67     enami #define	ex_mchash(addr)		(ether_crc32_be((addr), ETHER_ADDR_LEN) & \
    704   1.67     enami 				    (MCHASHSIZE - 1))
    705    1.1      fvdl 
    706    1.1      fvdl /*
    707    1.1      fvdl  * Set multicast receive filter. Also take care of promiscuous mode
    708    1.1      fvdl  * here (XXX).
    709    1.1      fvdl  */
    710    1.1      fvdl void
    711  1.102    cegger ex_set_mc(struct ex_softc *sc)
    712    1.1      fvdl {
    713    1.1      fvdl 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    714    1.1      fvdl 	struct ethercom *ec = &sc->sc_ethercom;
    715    1.1      fvdl 	struct ether_multi *enm;
    716    1.1      fvdl 	struct ether_multistep estep;
    717    1.1      fvdl 	int i;
    718  1.102    cegger 	uint16_t mask = FIL_INDIVIDUAL | FIL_BRDCST;
    719    1.1      fvdl 
    720   1.67     enami 	if (ifp->if_flags & IFF_PROMISC) {
    721    1.1      fvdl 		mask |= FIL_PROMISC;
    722   1.67     enami 		goto allmulti;
    723   1.67     enami 	}
    724   1.79     perry 
    725  1.131   msaitoh 	ETHER_LOCK(ec);
    726   1.67     enami 	ETHER_FIRST_MULTI(estep, ec, enm);
    727  1.131   msaitoh 	if (enm == NULL) {
    728  1.131   msaitoh 		ETHER_UNLOCK(ec);
    729   1.67     enami 		goto nomulti;
    730  1.131   msaitoh 	}
    731   1.67     enami 
    732  1.131   msaitoh 	if ((sc->ex_conf & EX_CONF_90XB) == 0) {
    733   1.67     enami 		/* No multicast hash filtering. */
    734  1.131   msaitoh 		ETHER_UNLOCK(ec);
    735   1.67     enami 		goto allmulti;
    736  1.131   msaitoh 	}
    737   1.67     enami 
    738   1.67     enami 	for (i = 0; i < MCHASHSIZE; i++)
    739   1.67     enami 		bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    740   1.67     enami 		    ELINK_COMMAND, ELINK_CLEARHASHFILBIT | i);
    741   1.67     enami 
    742   1.67     enami 	do {
    743   1.67     enami 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    744  1.131   msaitoh 		    ETHER_ADDR_LEN) != 0) {
    745  1.131   msaitoh 			ETHER_UNLOCK(ec);
    746   1.67     enami 			goto allmulti;
    747  1.131   msaitoh 		}
    748   1.67     enami 
    749   1.67     enami 		i = ex_mchash(enm->enm_addrlo);
    750   1.67     enami 		bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    751   1.67     enami 		    ELINK_COMMAND, ELINK_SETHASHFILBIT | i);
    752   1.67     enami 		ETHER_NEXT_MULTI(estep, enm);
    753   1.67     enami 	} while (enm != NULL);
    754  1.131   msaitoh 	ETHER_UNLOCK(ec);
    755   1.67     enami 	mask |= FIL_MULTIHASH;
    756   1.67     enami 
    757   1.67     enami nomulti:
    758   1.67     enami 	ifp->if_flags &= ~IFF_ALLMULTI;
    759   1.67     enami 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
    760   1.67     enami 	    SET_RX_FILTER | mask);
    761   1.67     enami 	return;
    762    1.1      fvdl 
    763   1.67     enami allmulti:
    764   1.67     enami 	ifp->if_flags |= IFF_ALLMULTI;
    765   1.67     enami 	mask |= FIL_MULTICAST;
    766    1.1      fvdl 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
    767    1.1      fvdl 	    SET_RX_FILTER | mask);
    768    1.1      fvdl }
    769    1.1      fvdl 
    770    1.1      fvdl 
    771   1.92     itohy /*
    772   1.92     itohy  * The Tx Complete interrupts occur only on errors,
    773   1.92     itohy  * and this is the error handler.
    774   1.92     itohy  */
    775    1.1      fvdl static void
    776  1.102    cegger ex_txstat(struct ex_softc *sc)
    777    1.1      fvdl {
    778   1.42   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    779    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
    780    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
    781   1.92     itohy 	int i, err = 0;
    782    1.1      fvdl 
    783    1.1      fvdl 	/*
    784    1.1      fvdl 	 * We need to read+write TX_STATUS until we get a 0 status
    785    1.1      fvdl 	 * in order to turn off the interrupt flag.
    786   1.92     itohy 	 * ELINK_TXSTATUS is in the upper byte of 2 with ELINK_TIMER.
    787    1.1      fvdl 	 */
    788   1.92     itohy 	for (;;) {
    789   1.92     itohy 		i = bus_space_read_2(iot, ioh, ELINK_TIMER);
    790   1.92     itohy 		if ((i & TXS_COMPLETE) == 0)
    791   1.92     itohy 			break;
    792   1.85  christos 		bus_space_write_2(iot, ioh, ELINK_TIMER, 0x0);
    793   1.92     itohy 		err |= i;
    794   1.92     itohy 	}
    795   1.92     itohy 	err &= ~TXS_TIMER;
    796   1.92     itohy 
    797   1.92     itohy 	if ((err & (TXS_UNDERRUN | TXS_JABBER | TXS_RECLAIM))
    798   1.92     itohy 	    || err == 0 /* should not happen, just in case */) {
    799   1.92     itohy 		/*
    800   1.92     itohy 		 * Make sure the transmission is stopped.
    801   1.92     itohy 		 */
    802   1.92     itohy 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_DNSTALL);
    803   1.92     itohy 		for (i = 1000; i > 0; i--)
    804   1.92     itohy 			if ((bus_space_read_4(iot, ioh, ELINK_DMACTRL) &
    805   1.92     itohy 			    ELINK_DMAC_DNINPROG) == 0)
    806   1.92     itohy 				break;
    807   1.92     itohy 
    808   1.92     itohy 		/*
    809   1.92     itohy 		 * Reset the transmitter.
    810   1.92     itohy 		 */
    811   1.92     itohy 		bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_RESET);
    812    1.1      fvdl 
    813   1.92     itohy 		/* Resetting takes a while and we will do more than wait. */
    814   1.92     itohy 
    815   1.92     itohy 		ifp->if_flags &= ~IFF_OACTIVE;
    816  1.134   thorpej 		if_statinc(ifp, if_oerrors);
    817  1.103       spz 		aprint_error_dev(sc->sc_dev, "%s%s%s",
    818   1.92     itohy 		    (err & TXS_UNDERRUN) ? " transmit underrun" : "",
    819   1.92     itohy 		    (err & TXS_JABBER) ? " jabber" : "",
    820   1.92     itohy 		    (err & TXS_RECLAIM) ? " reclaim" : "");
    821   1.92     itohy 		if (err == 0)
    822  1.103       spz 			aprint_error(" unknown Tx error");
    823   1.92     itohy 		printf(" (%x)", err);
    824   1.92     itohy 		if (err & TXS_UNDERRUN) {
    825  1.103       spz 			aprint_error(" @%d", sc->tx_start_thresh);
    826   1.92     itohy 			if (sc->tx_succ_ok < 256 &&
    827  1.125  riastrad 			    (i = uimin(ETHER_MAX_LEN, sc->tx_start_thresh + 20))
    828   1.92     itohy 			    > sc->tx_start_thresh) {
    829  1.103       spz 				aprint_error(", new threshold is %d", i);
    830   1.92     itohy 				sc->tx_start_thresh = i;
    831   1.92     itohy 			}
    832    1.1      fvdl 			sc->tx_succ_ok = 0;
    833   1.92     itohy 		}
    834  1.103       spz 		aprint_error("\n");
    835   1.92     itohy 		if (err & TXS_MAX_COLLISION)
    836  1.134   thorpej 			if_statinc(ifp, if_collisions);
    837   1.92     itohy 
    838   1.92     itohy 		/* Wait for TX_RESET to finish. */
    839   1.92     itohy 		ex_waitcmd(sc);
    840   1.92     itohy 
    841   1.92     itohy 		/* Reload Tx parameters. */
    842   1.92     itohy 		ex_setup_tx(sc);
    843   1.92     itohy 	} else {
    844   1.92     itohy 		if (err & TXS_MAX_COLLISION)
    845  1.134   thorpej 			if_statinc(ifp, if_collisions);
    846   1.92     itohy 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    847   1.94     itohy 	}
    848   1.94     itohy 
    849   1.94     itohy 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    850   1.94     itohy 
    851   1.94     itohy 	/* Retransmit current packet if any. */
    852   1.94     itohy 	if (sc->tx_head) {
    853   1.94     itohy 		ifp->if_flags |= IFF_OACTIVE;
    854   1.94     itohy 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
    855   1.94     itohy 		    ELINK_DNUNSTALL);
    856   1.94     itohy 		bus_space_write_4(iot, ioh, ELINK_DNLISTPTR,
    857   1.94     itohy 		    DPD_DMADDR(sc, sc->tx_head));
    858   1.94     itohy 
    859   1.94     itohy 		/* Retrigger watchdog if stopped. */
    860   1.94     itohy 		if (ifp->if_timer == 0)
    861   1.94     itohy 			ifp->if_timer = 1;
    862    1.1      fvdl 	}
    863    1.1      fvdl }
    864    1.1      fvdl 
    865    1.1      fvdl int
    866  1.102    cegger ex_media_chg(struct ifnet *ifp)
    867    1.1      fvdl {
    868    1.1      fvdl 
    869    1.1      fvdl 	if (ifp->if_flags & IFF_UP)
    870   1.42   thorpej 		ex_init(ifp);
    871    1.1      fvdl 	return 0;
    872    1.1      fvdl }
    873    1.1      fvdl 
    874    1.1      fvdl void
    875  1.102    cegger ex_set_xcvr(struct ex_softc *sc, const uint16_t media)
    876   1.69  christos {
    877   1.69  christos 	bus_space_tag_t iot = sc->sc_iot;
    878   1.69  christos 	bus_space_handle_t ioh = sc->sc_ioh;
    879  1.102    cegger 	uint32_t icfg;
    880   1.69  christos 
    881   1.69  christos 	/*
    882   1.69  christos 	 * We're already in Window 3
    883   1.69  christos 	 */
    884   1.69  christos 	icfg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    885   1.69  christos 	icfg &= ~(CONFIG_XCVR_SEL << 16);
    886   1.69  christos 	if (media & (ELINK_MEDIACAP_MII | ELINK_MEDIACAP_100BASET4))
    887   1.69  christos 		icfg |= ELINKMEDIA_MII << (CONFIG_XCVR_SEL_SHIFT + 16);
    888   1.69  christos 	if (media & ELINK_MEDIACAP_100BASETX)
    889   1.69  christos 		icfg |= ELINKMEDIA_AUTO << (CONFIG_XCVR_SEL_SHIFT + 16);
    890   1.69  christos 	if (media & ELINK_MEDIACAP_100BASEFX)
    891   1.79     perry 		icfg |= ELINKMEDIA_100BASE_FX
    892   1.69  christos 			<< (CONFIG_XCVR_SEL_SHIFT + 16);
    893   1.69  christos 	bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, icfg);
    894   1.69  christos }
    895   1.69  christos 
    896   1.69  christos void
    897  1.102    cegger ex_set_media(struct ex_softc *sc)
    898    1.1      fvdl {
    899    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
    900    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
    901  1.102    cegger 	uint32_t configreg;
    902    1.1      fvdl 
    903    1.1      fvdl 	if (((sc->ex_conf & EX_CONF_MII) &&
    904    1.1      fvdl 	    (sc->ex_mii.mii_media_active & IFM_FDX))
    905    1.1      fvdl 	    || (!(sc->ex_conf & EX_CONF_MII) &&
    906    1.1      fvdl 	    (sc->ex_mii.mii_media.ifm_media & IFM_FDX))) {
    907    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL,
    908    1.1      fvdl 		    MAC_CONTROL_FDX);
    909    1.1      fvdl 	} else {
    910    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, 0);
    911    1.1      fvdl 	}
    912    1.1      fvdl 
    913    1.1      fvdl 	/*
    914    1.1      fvdl 	 * If the device has MII, select it, and then tell the
    915    1.1      fvdl 	 * PHY which media to use.
    916    1.1      fvdl 	 */
    917    1.1      fvdl 	if (sc->ex_conf & EX_CONF_MII) {
    918  1.102    cegger 		uint16_t val;
    919   1.69  christos 
    920    1.1      fvdl 		GO_WINDOW(3);
    921   1.69  christos 		val = bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
    922   1.69  christos 		ex_set_xcvr(sc, val);
    923    1.1      fvdl 		mii_mediachg(&sc->ex_mii);
    924    1.1      fvdl 		return;
    925    1.1      fvdl 	}
    926    1.1      fvdl 
    927    1.1      fvdl 	GO_WINDOW(4);
    928    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0);
    929    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
    930    1.1      fvdl 	delay(800);
    931    1.1      fvdl 
    932    1.1      fvdl 	/*
    933    1.1      fvdl 	 * Now turn on the selected media/transceiver.
    934    1.1      fvdl 	 */
    935    1.1      fvdl 	switch (IFM_SUBTYPE(sc->ex_mii.mii_media.ifm_cur->ifm_media)) {
    936    1.1      fvdl 	case IFM_10_T:
    937    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    938  1.129   msaitoh 		    JABBER_GUARD_ENABLE | LINKBEAT_ENABLE);
    939    1.1      fvdl 		break;
    940    1.1      fvdl 
    941    1.1      fvdl 	case IFM_10_2:
    942    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
    943    1.1      fvdl 		DELAY(800);
    944    1.1      fvdl 		break;
    945    1.1      fvdl 
    946    1.1      fvdl 	case IFM_100_TX:
    947    1.1      fvdl 	case IFM_100_FX:
    948    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    949    1.1      fvdl 		    LINKBEAT_ENABLE);
    950    1.1      fvdl 		DELAY(800);
    951    1.1      fvdl 		break;
    952    1.1      fvdl 
    953    1.1      fvdl 	case IFM_10_5:
    954    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    955    1.1      fvdl 		    SQE_ENABLE);
    956    1.1      fvdl 		DELAY(800);
    957    1.1      fvdl 		break;
    958    1.1      fvdl 
    959    1.1      fvdl 	case IFM_MANUAL:
    960    1.1      fvdl 		break;
    961    1.1      fvdl 
    962    1.1      fvdl 	case IFM_NONE:
    963    1.1      fvdl 		return;
    964    1.1      fvdl 
    965    1.1      fvdl 	default:
    966    1.1      fvdl 		panic("ex_set_media: impossible");
    967    1.1      fvdl 	}
    968    1.1      fvdl 
    969    1.1      fvdl 	GO_WINDOW(3);
    970   1.37      haya 	configreg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    971    1.1      fvdl 
    972   1.37      haya 	configreg &= ~(CONFIG_MEDIAMASK << 16);
    973   1.37      haya 	configreg |= (sc->ex_mii.mii_media.ifm_cur->ifm_data <<
    974   1.37      haya 	    (CONFIG_MEDIAMASK_SHIFT + 16));
    975    1.1      fvdl 
    976   1.37      haya 	bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, configreg);
    977    1.1      fvdl }
    978    1.1      fvdl 
    979    1.1      fvdl /*
    980    1.1      fvdl  * Get currently-selected media from card.
    981    1.1      fvdl  * (if_media callback, may be called before interface is brought up).
    982    1.1      fvdl  */
    983    1.1      fvdl void
    984  1.102    cegger ex_media_stat(struct ifnet *ifp, struct ifmediareq *req)
    985    1.1      fvdl {
    986    1.1      fvdl 	struct ex_softc *sc = ifp->if_softc;
    987  1.102    cegger 	uint16_t help;
    988    1.1      fvdl 
    989  1.129   msaitoh 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)) {
    990   1.73    bouyer 		if (sc->ex_conf & EX_CONF_MII) {
    991   1.73    bouyer 			mii_pollstat(&sc->ex_mii);
    992   1.73    bouyer 			req->ifm_status = sc->ex_mii.mii_media_status;
    993   1.73    bouyer 			req->ifm_active = sc->ex_mii.mii_media_active;
    994   1.73    bouyer 		} else {
    995   1.73    bouyer 			GO_WINDOW(4);
    996   1.73    bouyer 			req->ifm_status = IFM_AVALID;
    997   1.73    bouyer 			req->ifm_active =
    998   1.73    bouyer 			    sc->ex_mii.mii_media.ifm_cur->ifm_media;
    999   1.73    bouyer 			help = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
   1000   1.73    bouyer 						ELINK_W4_MEDIA_TYPE);
   1001   1.73    bouyer 			if (help & LINKBEAT_DETECT)
   1002   1.73    bouyer 				req->ifm_status |= IFM_ACTIVE;
   1003   1.73    bouyer 			GO_WINDOW(1);
   1004   1.73    bouyer 		}
   1005    1.1      fvdl 	}
   1006    1.1      fvdl }
   1007    1.1      fvdl 
   1008    1.1      fvdl 
   1009    1.1      fvdl 
   1010    1.1      fvdl /*
   1011    1.1      fvdl  * Start outputting on the interface.
   1012    1.1      fvdl  */
   1013    1.1      fvdl static void
   1014  1.102    cegger ex_start(struct ifnet *ifp)
   1015    1.1      fvdl {
   1016    1.1      fvdl 	struct ex_softc *sc = ifp->if_softc;
   1017    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1018    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1019    1.1      fvdl 	volatile struct ex_fraghdr *fr = NULL;
   1020    1.1      fvdl 	volatile struct ex_dpd *dpd = NULL, *prevdpd = NULL;
   1021    1.1      fvdl 	struct ex_txdesc *txp;
   1022   1.46   thorpej 	struct mbuf *mb_head;
   1023    1.1      fvdl 	bus_dmamap_t dmamap;
   1024   1.95     itohy 	int m_csumflags, offset, seglen, totlen, segment, error;
   1025  1.102    cegger 	uint32_t csum_flags;
   1026    1.1      fvdl 
   1027    1.1      fvdl 	if (sc->tx_head || sc->tx_free == NULL)
   1028    1.1      fvdl 		return;
   1029    1.1      fvdl 
   1030    1.1      fvdl 	txp = NULL;
   1031    1.1      fvdl 
   1032    1.1      fvdl 	/*
   1033    1.1      fvdl 	 * We're finished if there is nothing more to add to the list or if
   1034    1.1      fvdl 	 * we're all filled up with buffers to transmit.
   1035    1.1      fvdl 	 */
   1036   1.46   thorpej 	while (sc->tx_free != NULL) {
   1037    1.1      fvdl 		/*
   1038    1.1      fvdl 		 * Grab a packet to transmit.
   1039    1.1      fvdl 		 */
   1040   1.46   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, mb_head);
   1041   1.46   thorpej 		if (mb_head == NULL)
   1042   1.46   thorpej 			break;
   1043    1.1      fvdl 
   1044    1.1      fvdl 		/*
   1045   1.91   tsutsui 		 * mb_head might be updated later,
   1046   1.91   tsutsui 		 * so preserve csum_flags here.
   1047   1.91   tsutsui 		 */
   1048   1.91   tsutsui 		m_csumflags = mb_head->m_pkthdr.csum_flags;
   1049   1.91   tsutsui 
   1050   1.91   tsutsui 		/*
   1051    1.1      fvdl 		 * Get pointer to next available tx desc.
   1052    1.1      fvdl 		 */
   1053    1.1      fvdl 		txp = sc->tx_free;
   1054    1.1      fvdl 		dmamap = txp->tx_dmamap;
   1055    1.1      fvdl 
   1056    1.1      fvdl 		/*
   1057    1.1      fvdl 		 * Go through each of the mbufs in the chain and initialize
   1058    1.1      fvdl 		 * the transmit buffer descriptors with the physical address
   1059    1.1      fvdl 		 * and size of the mbuf.
   1060    1.1      fvdl 		 */
   1061    1.1      fvdl  reload:
   1062    1.1      fvdl 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
   1063  1.129   msaitoh 		    mb_head, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1064    1.1      fvdl 		switch (error) {
   1065    1.1      fvdl 		case 0:
   1066    1.1      fvdl 			/* Success. */
   1067    1.1      fvdl 			break;
   1068    1.1      fvdl 
   1069    1.1      fvdl 		case EFBIG:
   1070    1.1      fvdl 		    {
   1071    1.1      fvdl 			struct mbuf *mn;
   1072    1.1      fvdl 
   1073    1.1      fvdl 			/*
   1074    1.1      fvdl 			 * We ran out of segments.  We have to recopy this
   1075    1.1      fvdl 			 * mbuf chain first.  Bail out if we can't get the
   1076    1.1      fvdl 			 * new buffers.
   1077    1.1      fvdl 			 */
   1078  1.103       spz 			aprint_error_dev(sc->sc_dev, "too many segments, ");
   1079    1.1      fvdl 
   1080    1.1      fvdl 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
   1081    1.1      fvdl 			if (mn == NULL) {
   1082    1.1      fvdl 				m_freem(mb_head);
   1083  1.103       spz 				aprint_error("aborting\n");
   1084    1.1      fvdl 				goto out;
   1085    1.1      fvdl 			}
   1086    1.1      fvdl 			if (mb_head->m_pkthdr.len > MHLEN) {
   1087    1.1      fvdl 				MCLGET(mn, M_DONTWAIT);
   1088    1.1      fvdl 				if ((mn->m_flags & M_EXT) == 0) {
   1089    1.1      fvdl 					m_freem(mn);
   1090    1.1      fvdl 					m_freem(mb_head);
   1091  1.103       spz 					aprint_error("aborting\n");
   1092    1.1      fvdl 					goto out;
   1093    1.1      fvdl 				}
   1094    1.1      fvdl 			}
   1095    1.1      fvdl 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
   1096   1.96  christos 			    mtod(mn, void *));
   1097    1.1      fvdl 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
   1098    1.1      fvdl 			m_freem(mb_head);
   1099    1.1      fvdl 			mb_head = mn;
   1100  1.103       spz 			aprint_error("retrying\n");
   1101    1.1      fvdl 			goto reload;
   1102    1.1      fvdl 		    }
   1103    1.1      fvdl 
   1104    1.1      fvdl 		default:
   1105    1.1      fvdl 			/*
   1106    1.1      fvdl 			 * Some other problem; report it.
   1107    1.1      fvdl 			 */
   1108  1.103       spz 			aprint_error_dev(sc->sc_dev,
   1109  1.103       spz 			    "can't load mbuf chain, error = %d\n", error);
   1110    1.1      fvdl 			m_freem(mb_head);
   1111    1.1      fvdl 			goto out;
   1112    1.1      fvdl 		}
   1113   1.57      yamt 
   1114   1.57      yamt 		/*
   1115   1.57      yamt 		 * remove our tx desc from freelist.
   1116   1.57      yamt 		 */
   1117   1.57      yamt 		sc->tx_free = txp->tx_next;
   1118   1.57      yamt 		txp->tx_next = NULL;
   1119    1.1      fvdl 
   1120    1.1      fvdl 		fr = &txp->tx_dpd->dpd_frags[0];
   1121    1.1      fvdl 		totlen = 0;
   1122    1.1      fvdl 		for (segment = 0; segment < dmamap->dm_nsegs; segment++, fr++) {
   1123   1.21   thorpej 			fr->fr_addr = htole32(dmamap->dm_segs[segment].ds_addr);
   1124   1.95     itohy 			seglen = dmamap->dm_segs[segment].ds_len;
   1125   1.95     itohy 			fr->fr_len = htole32(seglen);
   1126   1.95     itohy 			totlen += seglen;
   1127   1.95     itohy 		}
   1128   1.95     itohy 		if (__predict_false(totlen <= EX_IP4CSUMTX_PADLEN &&
   1129   1.95     itohy 		    (m_csumflags & M_CSUM_IPv4) != 0)) {
   1130   1.95     itohy 			/*
   1131   1.95     itohy 			 * Pad short packets to avoid ip4csum-tx bug.
   1132   1.95     itohy 			 *
   1133   1.95     itohy 			 * XXX Should we still consider if such short
   1134   1.95     itohy 			 *     (36 bytes or less) packets might already
   1135  1.107   tsutsui 			 *     occupy EX_NTFRAG (== 32) fragments here?
   1136   1.95     itohy 			 */
   1137   1.95     itohy 			KASSERT(segment < EX_NTFRAGS);
   1138   1.95     itohy 			fr->fr_addr = htole32(DPDMEMPAD_DMADDR(sc));
   1139   1.95     itohy 			seglen = EX_IP4CSUMTX_PADLEN + 1 - totlen;
   1140   1.95     itohy 			fr->fr_len = htole32(EX_FR_LAST | seglen);
   1141   1.95     itohy 			totlen += seglen;
   1142   1.95     itohy 		} else {
   1143   1.95     itohy 			fr--;
   1144   1.95     itohy 			fr->fr_len |= htole32(EX_FR_LAST);
   1145    1.1      fvdl 		}
   1146    1.1      fvdl 		txp->tx_mbhead = mb_head;
   1147    1.1      fvdl 
   1148    1.1      fvdl 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1149    1.1      fvdl 		    BUS_DMASYNC_PREWRITE);
   1150    1.1      fvdl 
   1151    1.1      fvdl 		dpd = txp->tx_dpd;
   1152    1.1      fvdl 		dpd->dpd_nextptr = 0;
   1153   1.21   thorpej 		dpd->dpd_fsh = htole32(totlen);
   1154    1.1      fvdl 
   1155   1.63       wiz 		/* Byte-swap constants so compiler can optimize. */
   1156   1.50   thorpej 
   1157   1.50   thorpej 		if (sc->ex_conf & EX_CONF_90XB) {
   1158   1.50   thorpej 			csum_flags = 0;
   1159   1.50   thorpej 
   1160   1.91   tsutsui 			if (m_csumflags & M_CSUM_IPv4)
   1161   1.50   thorpej 				csum_flags |= htole32(EX_DPD_IPCKSUM);
   1162   1.50   thorpej 
   1163   1.91   tsutsui 			if (m_csumflags & M_CSUM_TCPv4)
   1164   1.50   thorpej 				csum_flags |= htole32(EX_DPD_TCPCKSUM);
   1165   1.91   tsutsui 			else if (m_csumflags & M_CSUM_UDPv4)
   1166   1.50   thorpej 				csum_flags |= htole32(EX_DPD_UDPCKSUM);
   1167   1.50   thorpej 
   1168   1.50   thorpej 			dpd->dpd_fsh |= csum_flags;
   1169   1.50   thorpej 		} else {
   1170   1.50   thorpej 			KDASSERT((mb_head->m_pkthdr.csum_flags &
   1171  1.129   msaitoh 			    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) == 0);
   1172   1.50   thorpej 		}
   1173   1.50   thorpej 
   1174    1.1      fvdl 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1175   1.81  christos 		    ((const char *)(intptr_t)dpd - (const char *)sc->sc_dpd),
   1176    1.1      fvdl 		    sizeof (struct ex_dpd),
   1177  1.129   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1178    1.1      fvdl 
   1179    1.1      fvdl 		/*
   1180    1.1      fvdl 		 * No need to stall the download engine, we know it's
   1181    1.1      fvdl 		 * not busy right now.
   1182    1.1      fvdl 		 *
   1183    1.1      fvdl 		 * Fix up pointers in both the "soft" tx and the physical
   1184    1.1      fvdl 		 * tx list.
   1185    1.1      fvdl 		 */
   1186    1.1      fvdl 		if (sc->tx_head != NULL) {
   1187    1.1      fvdl 			prevdpd = sc->tx_tail->tx_dpd;
   1188   1.81  christos 			offset = ((const char *)(intptr_t)prevdpd - (const char *)sc->sc_dpd);
   1189    1.1      fvdl 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1190    1.1      fvdl 			    offset, sizeof (struct ex_dpd),
   1191  1.129   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1192   1.21   thorpej 			prevdpd->dpd_nextptr = htole32(DPD_DMADDR(sc, txp));
   1193    1.1      fvdl 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1194    1.1      fvdl 			    offset, sizeof (struct ex_dpd),
   1195  1.129   msaitoh 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1196    1.1      fvdl 			sc->tx_tail->tx_next = txp;
   1197    1.1      fvdl 			sc->tx_tail = txp;
   1198    1.1      fvdl 		} else {
   1199    1.1      fvdl 			sc->tx_tail = sc->tx_head = txp;
   1200    1.1      fvdl 		}
   1201    1.1      fvdl 
   1202    1.1      fvdl 		/*
   1203    1.1      fvdl 		 * Pass packet to bpf if there is a listener.
   1204    1.1      fvdl 		 */
   1205  1.124   msaitoh 		bpf_mtap(ifp, mb_head, BPF_D_OUT);
   1206    1.1      fvdl 	}
   1207    1.1      fvdl  out:
   1208    1.1      fvdl 	if (sc->tx_head) {
   1209   1.21   thorpej 		sc->tx_tail->tx_dpd->dpd_fsh |= htole32(EX_DPD_DNIND);
   1210    1.1      fvdl 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1211   1.96  christos 		    ((char *)sc->tx_tail->tx_dpd - (char *)sc->sc_dpd),
   1212    1.1      fvdl 		    sizeof (struct ex_dpd),
   1213  1.129   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1214    1.1      fvdl 		ifp->if_flags |= IFF_OACTIVE;
   1215    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_DNUNSTALL);
   1216    1.1      fvdl 		bus_space_write_4(iot, ioh, ELINK_DNLISTPTR,
   1217    1.1      fvdl 		    DPD_DMADDR(sc, sc->tx_head));
   1218    1.3  drochner 
   1219    1.3  drochner 		/* trigger watchdog */
   1220    1.3  drochner 		ifp->if_timer = 5;
   1221    1.1      fvdl 	}
   1222    1.1      fvdl }
   1223    1.1      fvdl 
   1224    1.1      fvdl 
   1225    1.1      fvdl int
   1226  1.102    cegger ex_intr(void *arg)
   1227    1.1      fvdl {
   1228    1.1      fvdl 	struct ex_softc *sc = arg;
   1229    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1230    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1231  1.102    cegger 	uint16_t stat;
   1232    1.1      fvdl 	int ret = 0;
   1233    1.1      fvdl 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1234    1.1      fvdl 
   1235   1.47   thorpej 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   1236  1.103       spz 	    !device_is_active(sc->sc_dev))
   1237   1.28     enami 		return (0);
   1238   1.28     enami 
   1239    1.1      fvdl 	for (;;) {
   1240    1.1      fvdl 		stat = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1241   1.22   mycroft 
   1242   1.61  christos 		if ((stat & XL_WATCHED_INTERRUPTS) == 0) {
   1243   1.61  christos 			if ((stat & INTR_LATCH) == 0) {
   1244   1.22   mycroft #if 0
   1245  1.103       spz 				aprint_error_dev(sc->sc_dev,
   1246  1.103       spz 				       "intr latch cleared\n");
   1247   1.22   mycroft #endif
   1248   1.22   mycroft 				break;
   1249   1.22   mycroft 			}
   1250   1.22   mycroft 		}
   1251   1.22   mycroft 
   1252   1.22   mycroft 		ret = 1;
   1253   1.22   mycroft 
   1254    1.1      fvdl 		/*
   1255    1.1      fvdl 		 * Acknowledge interrupts.
   1256    1.1      fvdl 		 */
   1257    1.1      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
   1258   1.61  christos 		    (stat & (XL_WATCHED_INTERRUPTS | INTR_LATCH)));
   1259   1.15      haya 		if (sc->intr_ack)
   1260   1.22   mycroft 			(*sc->intr_ack)(sc);
   1261   1.22   mycroft 
   1262   1.61  christos 		if (stat & HOST_ERROR) {
   1263  1.103       spz 			aprint_error_dev(sc->sc_dev,
   1264  1.103       spz 			    "adapter failure (%x)\n", stat);
   1265    1.1      fvdl 			ex_reset(sc);
   1266   1.42   thorpej 			ex_init(ifp);
   1267    1.1      fvdl 			return 1;
   1268    1.1      fvdl 		}
   1269   1.61  christos 		if (stat & UPD_STATS) {
   1270    1.1      fvdl 			ex_getstats(sc);
   1271    1.1      fvdl 		}
   1272   1.94     itohy 		if (stat & TX_COMPLETE) {
   1273   1.94     itohy 			ex_txstat(sc);
   1274   1.94     itohy #if 0
   1275   1.94     itohy 			if (stat & DN_COMPLETE)
   1276  1.103       spz 				aprint_error_dev(sc->sc_dev,
   1277  1.103       spz 				    "Ignoring Dn interrupt (%x)\n", stat);
   1278   1.94     itohy #endif
   1279   1.94     itohy 			/*
   1280   1.94     itohy 			 * In some rare cases, both Tx Complete and
   1281   1.94     itohy 			 * Dn Complete bits are set.  However, the packet
   1282   1.94     itohy 			 * has been reloaded in ex_txstat() and should not
   1283   1.94     itohy 			 * handle the Dn Complete event here.
   1284   1.94     itohy 			 * Hence the "else" below.
   1285   1.94     itohy 			 */
   1286   1.94     itohy 		} else if (stat & DN_COMPLETE) {
   1287    1.1      fvdl 			struct ex_txdesc *txp, *ptxp = NULL;
   1288    1.1      fvdl 			bus_dmamap_t txmap;
   1289    1.3  drochner 
   1290    1.3  drochner 			/* reset watchdog timer, was set in ex_start() */
   1291    1.3  drochner 			ifp->if_timer = 0;
   1292    1.3  drochner 
   1293    1.1      fvdl 			for (txp = sc->tx_head; txp != NULL;
   1294    1.1      fvdl 			    txp = txp->tx_next) {
   1295    1.1      fvdl 				bus_dmamap_sync(sc->sc_dmat,
   1296    1.1      fvdl 				    sc->sc_dpd_dmamap,
   1297   1.96  christos 				    (char *)txp->tx_dpd - (char *)sc->sc_dpd,
   1298    1.1      fvdl 				    sizeof (struct ex_dpd),
   1299    1.1      fvdl 				    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1300    1.1      fvdl 				if (txp->tx_mbhead != NULL) {
   1301    1.1      fvdl 					txmap = txp->tx_dmamap;
   1302    1.1      fvdl 					bus_dmamap_sync(sc->sc_dmat, txmap,
   1303    1.1      fvdl 					    0, txmap->dm_mapsize,
   1304    1.1      fvdl 					    BUS_DMASYNC_POSTWRITE);
   1305    1.1      fvdl 					bus_dmamap_unload(sc->sc_dmat, txmap);
   1306    1.1      fvdl 					m_freem(txp->tx_mbhead);
   1307    1.1      fvdl 					txp->tx_mbhead = NULL;
   1308    1.1      fvdl 				}
   1309    1.1      fvdl 				ptxp = txp;
   1310    1.1      fvdl 			}
   1311    1.1      fvdl 
   1312    1.1      fvdl 			/*
   1313    1.1      fvdl 			 * Move finished tx buffers back to the tx free list.
   1314    1.1      fvdl 			 */
   1315    1.1      fvdl 			if (sc->tx_free) {
   1316    1.1      fvdl 				sc->tx_ftail->tx_next = sc->tx_head;
   1317    1.1      fvdl 				sc->tx_ftail = ptxp;
   1318    1.1      fvdl 			} else
   1319    1.1      fvdl 				sc->tx_ftail = sc->tx_free = sc->tx_head;
   1320    1.1      fvdl 
   1321    1.1      fvdl 			sc->tx_head = sc->tx_tail = NULL;
   1322    1.1      fvdl 			ifp->if_flags &= ~IFF_OACTIVE;
   1323   1.92     itohy 
   1324   1.92     itohy 			if (sc->tx_succ_ok < 256)
   1325   1.92     itohy 				sc->tx_succ_ok++;
   1326    1.1      fvdl 		}
   1327    1.1      fvdl 
   1328   1.61  christos 		if (stat & UP_COMPLETE) {
   1329    1.1      fvdl 			struct ex_rxdesc *rxd;
   1330    1.1      fvdl 			struct mbuf *m;
   1331    1.1      fvdl 			struct ex_upd *upd;
   1332    1.1      fvdl 			bus_dmamap_t rxmap;
   1333  1.102    cegger 			uint32_t pktstat;
   1334    1.1      fvdl 
   1335    1.1      fvdl  rcvloop:
   1336    1.1      fvdl 			rxd = sc->rx_head;
   1337    1.1      fvdl 			rxmap = rxd->rx_dmamap;
   1338    1.1      fvdl 			m = rxd->rx_mbhead;
   1339    1.1      fvdl 			upd = rxd->rx_upd;
   1340    1.1      fvdl 
   1341    1.1      fvdl 			bus_dmamap_sync(sc->sc_dmat, rxmap, 0,
   1342    1.1      fvdl 			    rxmap->dm_mapsize,
   1343  1.129   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1344    1.1      fvdl 			bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1345   1.96  christos 			    ((char *)upd - (char *)sc->sc_upd),
   1346    1.1      fvdl 			    sizeof (struct ex_upd),
   1347  1.129   msaitoh 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1348   1.32   tsutsui 			pktstat = le32toh(upd->upd_pktstatus);
   1349    1.1      fvdl 
   1350    1.1      fvdl 			if (pktstat & EX_UPD_COMPLETE) {
   1351    1.1      fvdl 				/*
   1352    1.1      fvdl 				 * Remove first packet from the chain.
   1353    1.1      fvdl 				 */
   1354    1.1      fvdl 				sc->rx_head = rxd->rx_next;
   1355    1.1      fvdl 				rxd->rx_next = NULL;
   1356    1.1      fvdl 
   1357    1.1      fvdl 				/*
   1358    1.1      fvdl 				 * Add a new buffer to the receive chain.
   1359    1.1      fvdl 				 * If this fails, the old buffer is recycled
   1360    1.1      fvdl 				 * instead.
   1361    1.1      fvdl 				 */
   1362    1.1      fvdl 				if (ex_add_rxbuf(sc, rxd) == 0) {
   1363  1.102    cegger 					uint16_t total_len;
   1364    1.1      fvdl 
   1365   1.43    bouyer 					if (pktstat &
   1366   1.43    bouyer 					    ((sc->sc_ethercom.ec_capenable &
   1367   1.43    bouyer 					    ETHERCAP_VLAN_MTU) ?
   1368   1.43    bouyer 					    EX_UPD_ERR_VLAN : EX_UPD_ERR)) {
   1369  1.134   thorpej 						if_statinc(ifp, if_ierrors);
   1370    1.1      fvdl 						m_freem(m);
   1371    1.1      fvdl 						goto rcvloop;
   1372    1.1      fvdl 					}
   1373    1.1      fvdl 
   1374    1.1      fvdl 					total_len = pktstat & EX_UPD_PKTLENMASK;
   1375    1.1      fvdl 					if (total_len <
   1376    1.1      fvdl 					    sizeof(struct ether_header)) {
   1377    1.1      fvdl 						m_freem(m);
   1378    1.1      fvdl 						goto rcvloop;
   1379    1.1      fvdl 					}
   1380  1.119     ozaki 					m_set_rcvif(m, ifp);
   1381   1.13   thorpej 					m->m_pkthdr.len = m->m_len = total_len;
   1382   1.50   thorpej 		/*
   1383   1.50   thorpej 		 * Set the incoming checksum information for the packet.
   1384   1.50   thorpej 		 */
   1385   1.50   thorpej 		if ((sc->ex_conf & EX_CONF_90XB) != 0 &&
   1386   1.50   thorpej 		    (pktstat & EX_UPD_IPCHECKED) != 0) {
   1387   1.50   thorpej 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1388   1.50   thorpej 			if (pktstat & EX_UPD_IPCKSUMERR)
   1389   1.50   thorpej 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1390   1.50   thorpej 			if (pktstat & EX_UPD_TCPCHECKED) {
   1391   1.50   thorpej 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1392   1.50   thorpej 				if (pktstat & EX_UPD_TCPCKSUMERR)
   1393   1.50   thorpej 					m->m_pkthdr.csum_flags |=
   1394   1.50   thorpej 					    M_CSUM_TCP_UDP_BAD;
   1395   1.50   thorpej 			} else if (pktstat & EX_UPD_UDPCHECKED) {
   1396   1.50   thorpej 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1397   1.50   thorpej 				if (pktstat & EX_UPD_UDPCKSUMERR)
   1398   1.50   thorpej 					m->m_pkthdr.csum_flags |=
   1399   1.50   thorpej 					    M_CSUM_TCP_UDP_BAD;
   1400   1.50   thorpej 			}
   1401   1.50   thorpej 		}
   1402  1.118     ozaki 					if_percpuq_enqueue(ifp->if_percpuq, m);
   1403    1.1      fvdl 				}
   1404    1.1      fvdl 				goto rcvloop;
   1405    1.1      fvdl 			}
   1406    1.1      fvdl 			/*
   1407    1.1      fvdl 			 * Just in case we filled up all UPDs and the DMA engine
   1408    1.3  drochner 			 * stalled. We could be more subtle about this.
   1409    1.1      fvdl 			 */
   1410    1.3  drochner 			if (bus_space_read_4(iot, ioh, ELINK_UPLISTPTR) == 0) {
   1411  1.103       spz 				aprint_error_dev(sc->sc_dev,
   1412  1.103       spz 				       "uplistptr was 0\n");
   1413   1.42   thorpej 				ex_init(ifp);
   1414    1.3  drochner 			} else if (bus_space_read_4(iot, ioh, ELINK_UPPKTSTATUS)
   1415    1.3  drochner 				   & 0x2000) {
   1416  1.103       spz 				aprint_error_dev(sc->sc_dev,
   1417  1.103       spz 				       "receive stalled\n");
   1418    1.3  drochner 				bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1419    1.3  drochner 						  ELINK_UPUNSTALL);
   1420    1.3  drochner 			}
   1421    1.1      fvdl 		}
   1422   1.71  jdolecek 
   1423   1.71  jdolecek 		if (stat)
   1424   1.71  jdolecek 			rnd_add_uint32(&sc->rnd_source, stat);
   1425    1.1      fvdl 	}
   1426   1.22   mycroft 
   1427   1.22   mycroft 	/* no more interrupts */
   1428  1.121     ozaki 	if (ret)
   1429  1.121     ozaki 		if_schedule_deferred_start(ifp);
   1430    1.1      fvdl 	return ret;
   1431    1.1      fvdl }
   1432    1.1      fvdl 
   1433  1.106    dyoung static int
   1434  1.106    dyoung ex_ifflags_cb(struct ethercom *ec)
   1435  1.106    dyoung {
   1436  1.106    dyoung 	struct ifnet *ifp = &ec->ec_if;
   1437  1.106    dyoung 	struct ex_softc *sc = ifp->if_softc;
   1438  1.132   msaitoh 	u_short change = ifp->if_flags ^ sc->sc_if_flags;
   1439  1.129   msaitoh 
   1440  1.129   msaitoh 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
   1441  1.106    dyoung 		return ENETRESET;
   1442  1.106    dyoung 	else if ((change & IFF_PROMISC) != 0)
   1443  1.106    dyoung 		ex_set_mc(sc);
   1444  1.106    dyoung 	return 0;
   1445  1.106    dyoung }
   1446  1.106    dyoung 
   1447    1.1      fvdl int
   1448  1.102    cegger ex_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1449    1.1      fvdl {
   1450    1.1      fvdl 	struct ex_softc *sc = ifp->if_softc;
   1451   1.42   thorpej 	int s, error;
   1452    1.1      fvdl 
   1453    1.1      fvdl 	s = splnet();
   1454    1.1      fvdl 
   1455    1.1      fvdl 	switch (cmd) {
   1456   1.42   thorpej 	default:
   1457  1.100    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   1458  1.100    dyoung 			break;
   1459  1.100    dyoung 
   1460  1.100    dyoung 		error = 0;
   1461  1.100    dyoung 
   1462  1.100    dyoung 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1463  1.100    dyoung 			;
   1464  1.100    dyoung 		else if (ifp->if_flags & IFF_RUNNING) {
   1465    1.1      fvdl 			/*
   1466    1.1      fvdl 			 * Multicast list has changed; set the hardware filter
   1467    1.1      fvdl 			 * accordingly.
   1468    1.1      fvdl 			 */
   1469  1.100    dyoung 			ex_set_mc(sc);
   1470    1.1      fvdl 		}
   1471    1.1      fvdl 		break;
   1472    1.1      fvdl 	}
   1473    1.1      fvdl 
   1474   1.77       kim 	sc->sc_if_flags = ifp->if_flags;
   1475    1.1      fvdl 	splx(s);
   1476    1.1      fvdl 	return (error);
   1477    1.1      fvdl }
   1478    1.1      fvdl 
   1479    1.1      fvdl void
   1480  1.102    cegger ex_getstats(struct ex_softc *sc)
   1481    1.1      fvdl {
   1482    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1483    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1484    1.1      fvdl 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1485  1.102    cegger 	uint8_t upperok;
   1486    1.1      fvdl 
   1487  1.134   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1488  1.134   thorpej 
   1489    1.1      fvdl 	GO_WINDOW(6);
   1490    1.1      fvdl 	upperok = bus_space_read_1(iot, ioh, UPPER_FRAMES_OK);
   1491  1.134   thorpej 	if_statadd_ref(nsr, if_opackets,
   1492  1.134   thorpej 	    bus_space_read_1(iot, ioh, TX_FRAMES_OK));
   1493  1.134   thorpej 	if_statadd_ref(nsr, if_opackets, (upperok & 0x30) << 4);
   1494  1.134   thorpej 	if_statadd_ref(nsr, if_ierrors,
   1495  1.134   thorpej 	    bus_space_read_1(iot, ioh, RX_OVERRUNS));
   1496  1.134   thorpej 	if_statadd_ref(nsr, if_collisions,
   1497  1.134   thorpej 	    bus_space_read_1(iot, ioh, TX_COLLISIONS));
   1498    1.1      fvdl 	/*
   1499    1.1      fvdl 	 * There seems to be no way to get the exact number of collisions,
   1500   1.56       wiz 	 * this is the number that occurred at the very least.
   1501    1.1      fvdl 	 */
   1502  1.134   thorpej 	if_statadd_ref(nsr, if_collisions,
   1503  1.134   thorpej 	    2 * bus_space_read_1(iot, ioh, TX_AFTER_X_COLLISIONS));
   1504  1.134   thorpej 
   1505  1.134   thorpej 	IF_STAT_PUTREF(ifp);
   1506  1.134   thorpej 
   1507   1.62    mhitch 	/*
   1508   1.62    mhitch 	 * Interface byte counts are counted by ether_input() and
   1509   1.62    mhitch 	 * ether_output(), so don't accumulate them here.  Just
   1510   1.62    mhitch 	 * read the NIC counters so they don't generate overflow interrupts.
   1511   1.62    mhitch 	 * Upper byte counters are latched from reading the totals, so
   1512   1.62    mhitch 	 * they don't need to be read if we don't need their values.
   1513   1.62    mhitch 	 */
   1514   1.88  christos 	(void)bus_space_read_2(iot, ioh, RX_TOTAL_OK);
   1515   1.88  christos 	(void)bus_space_read_2(iot, ioh, TX_TOTAL_OK);
   1516    1.1      fvdl 
   1517    1.1      fvdl 	/*
   1518    1.1      fvdl 	 * Clear the following to avoid stats overflow interrupts
   1519    1.1      fvdl 	 */
   1520   1.88  christos 	(void)bus_space_read_1(iot, ioh, TX_DEFERRALS);
   1521   1.88  christos 	(void)bus_space_read_1(iot, ioh, TX_AFTER_1_COLLISION);
   1522   1.88  christos 	(void)bus_space_read_1(iot, ioh, TX_NO_SQE);
   1523   1.88  christos 	(void)bus_space_read_1(iot, ioh, TX_CD_LOST);
   1524    1.1      fvdl 	GO_WINDOW(4);
   1525   1.88  christos 	(void)bus_space_read_1(iot, ioh, ELINK_W4_BADSSD);
   1526    1.1      fvdl 	GO_WINDOW(1);
   1527    1.1      fvdl }
   1528    1.1      fvdl 
   1529    1.1      fvdl void
   1530  1.102    cegger ex_tick(void *arg)
   1531    1.1      fvdl {
   1532    1.1      fvdl 	struct ex_softc *sc = arg;
   1533   1.28     enami 	int s;
   1534   1.28     enami 
   1535  1.103       spz 	if (!device_is_active(sc->sc_dev))
   1536   1.28     enami 		return;
   1537   1.28     enami 
   1538   1.28     enami 	s = splnet();
   1539    1.1      fvdl 
   1540    1.1      fvdl 	if (sc->ex_conf & EX_CONF_MII)
   1541    1.1      fvdl 		mii_tick(&sc->ex_mii);
   1542    1.1      fvdl 
   1543    1.1      fvdl 	if (!(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS)
   1544   1.61  christos 	    & COMMAND_IN_PROGRESS))
   1545    1.1      fvdl 		ex_getstats(sc);
   1546    1.1      fvdl 
   1547    1.1      fvdl 	splx(s);
   1548    1.1      fvdl 
   1549   1.30   thorpej 	callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
   1550    1.1      fvdl }
   1551    1.1      fvdl 
   1552    1.1      fvdl void
   1553  1.102    cegger ex_reset(struct ex_softc *sc)
   1554    1.1      fvdl {
   1555  1.102    cegger 	uint16_t val = GLOBAL_RESET;
   1556   1.40      fvdl 
   1557   1.40      fvdl 	if (sc->ex_conf & EX_CONF_RESETHACK)
   1558   1.49      fvdl 		val |= 0x10;
   1559   1.40      fvdl 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, val);
   1560   1.49      fvdl 	/*
   1561   1.49      fvdl 	 * XXX apparently the command in progress bit can't be trusted
   1562   1.49      fvdl 	 * during a reset, so we just always wait this long. Fortunately
   1563   1.49      fvdl 	 * we normally only reset the chip during autoconfig.
   1564   1.49      fvdl 	 */
   1565   1.49      fvdl 	delay(100000);
   1566    1.1      fvdl 	ex_waitcmd(sc);
   1567    1.1      fvdl }
   1568    1.1      fvdl 
   1569    1.1      fvdl void
   1570  1.102    cegger ex_watchdog(struct ifnet *ifp)
   1571    1.1      fvdl {
   1572    1.1      fvdl 	struct ex_softc *sc = ifp->if_softc;
   1573    1.1      fvdl 
   1574  1.103       spz 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
   1575  1.134   thorpej 	if_statinc(ifp, if_oerrors);
   1576    1.1      fvdl 
   1577    1.1      fvdl 	ex_reset(sc);
   1578   1.42   thorpej 	ex_init(ifp);
   1579    1.1      fvdl }
   1580    1.1      fvdl 
   1581    1.1      fvdl void
   1582  1.102    cegger ex_stop(struct ifnet *ifp, int disable)
   1583    1.1      fvdl {
   1584   1.42   thorpej 	struct ex_softc *sc = ifp->if_softc;
   1585    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1586    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1587    1.1      fvdl 	struct ex_txdesc *tx;
   1588    1.1      fvdl 	struct ex_rxdesc *rx;
   1589    1.1      fvdl 	int i;
   1590    1.1      fvdl 
   1591    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
   1592    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
   1593    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
   1594    1.1      fvdl 
   1595    1.1      fvdl 	for (tx = sc->tx_head ; tx != NULL; tx = tx->tx_next) {
   1596    1.1      fvdl 		if (tx->tx_mbhead == NULL)
   1597    1.1      fvdl 			continue;
   1598    1.1      fvdl 		m_freem(tx->tx_mbhead);
   1599    1.1      fvdl 		tx->tx_mbhead = NULL;
   1600    1.1      fvdl 		bus_dmamap_unload(sc->sc_dmat, tx->tx_dmamap);
   1601    1.1      fvdl 		tx->tx_dpd->dpd_fsh = tx->tx_dpd->dpd_nextptr = 0;
   1602    1.1      fvdl 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1603   1.96  christos 		    ((char *)tx->tx_dpd - (char *)sc->sc_dpd),
   1604    1.1      fvdl 		    sizeof (struct ex_dpd),
   1605  1.129   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1606    1.1      fvdl 	}
   1607    1.1      fvdl 	sc->tx_tail = sc->tx_head = NULL;
   1608    1.1      fvdl 	ex_init_txdescs(sc);
   1609    1.1      fvdl 
   1610    1.1      fvdl 	sc->rx_tail = sc->rx_head = 0;
   1611    1.1      fvdl 	for (i = 0; i < EX_NUPD; i++) {
   1612    1.1      fvdl 		rx = &sc->sc_rxdescs[i];
   1613    1.1      fvdl 		if (rx->rx_mbhead != NULL) {
   1614    1.1      fvdl 			bus_dmamap_unload(sc->sc_dmat, rx->rx_dmamap);
   1615    1.1      fvdl 			m_freem(rx->rx_mbhead);
   1616    1.1      fvdl 			rx->rx_mbhead = NULL;
   1617    1.1      fvdl 		}
   1618    1.1      fvdl 		ex_add_rxbuf(sc, rx);
   1619    1.1      fvdl 	}
   1620    1.1      fvdl 
   1621   1.61  christos 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | INTR_LATCH);
   1622    1.1      fvdl 
   1623   1.30   thorpej 	callout_stop(&sc->ex_mii_callout);
   1624   1.17   thorpej 	if (sc->ex_conf & EX_CONF_MII)
   1625   1.17   thorpej 		mii_down(&sc->ex_mii);
   1626    1.1      fvdl 
   1627   1.79     perry 	if (disable)
   1628   1.47   thorpej 		ex_disable(sc);
   1629   1.47   thorpej 
   1630    1.1      fvdl 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1631   1.77       kim 	sc->sc_if_flags = ifp->if_flags;
   1632    1.1      fvdl 	ifp->if_timer = 0;
   1633    1.1      fvdl }
   1634    1.1      fvdl 
   1635    1.1      fvdl static void
   1636  1.102    cegger ex_init_txdescs(struct ex_softc *sc)
   1637    1.1      fvdl {
   1638    1.1      fvdl 	int i;
   1639    1.1      fvdl 
   1640    1.1      fvdl 	for (i = 0; i < EX_NDPD; i++) {
   1641    1.1      fvdl 		sc->sc_txdescs[i].tx_dmamap = sc->sc_tx_dmamaps[i];
   1642    1.1      fvdl 		sc->sc_txdescs[i].tx_dpd = &sc->sc_dpd[i];
   1643    1.1      fvdl 		if (i < EX_NDPD - 1)
   1644    1.1      fvdl 			sc->sc_txdescs[i].tx_next = &sc->sc_txdescs[i + 1];
   1645    1.1      fvdl 		else
   1646    1.1      fvdl 			sc->sc_txdescs[i].tx_next = NULL;
   1647    1.1      fvdl 	}
   1648    1.1      fvdl 	sc->tx_free = &sc->sc_txdescs[0];
   1649    1.1      fvdl 	sc->tx_ftail = &sc->sc_txdescs[EX_NDPD-1];
   1650    1.1      fvdl }
   1651    1.1      fvdl 
   1652   1.25  augustss 
   1653   1.25  augustss int
   1654  1.103       spz ex_activate(device_t self, enum devact act)
   1655   1.25  augustss {
   1656  1.103       spz 	struct ex_softc *sc = device_private(self);
   1657   1.25  augustss 
   1658   1.25  augustss 	switch (act) {
   1659   1.25  augustss 	case DVACT_DEACTIVATE:
   1660   1.25  augustss 		if_deactivate(&sc->sc_ethercom.ec_if);
   1661  1.109    dyoung 		return 0;
   1662  1.109    dyoung 	default:
   1663  1.109    dyoung 		return EOPNOTSUPP;
   1664   1.25  augustss 	}
   1665   1.25  augustss }
   1666   1.25  augustss 
   1667   1.25  augustss int
   1668  1.102    cegger ex_detach(struct ex_softc *sc)
   1669   1.25  augustss {
   1670   1.25  augustss 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1671   1.25  augustss 	struct ex_rxdesc *rxd;
   1672  1.111    dyoung 	int i, s;
   1673   1.34     jhawk 
   1674   1.34     jhawk 	/* Succeed now if there's no work to do. */
   1675   1.34     jhawk 	if ((sc->ex_flags & EX_FLAGS_ATTACHED) == 0)
   1676   1.34     jhawk 		return (0);
   1677   1.25  augustss 
   1678  1.111    dyoung 	s = splnet();
   1679  1.111    dyoung 	/* Stop the interface. Callouts are stopped in it. */
   1680  1.111    dyoung 	ex_stop(ifp, 1);
   1681  1.111    dyoung 	splx(s);
   1682  1.111    dyoung 
   1683  1.111    dyoung 	/* Destroy our callout. */
   1684  1.111    dyoung 	callout_destroy(&sc->ex_mii_callout);
   1685   1.25  augustss 
   1686   1.26   thorpej 	if (sc->ex_conf & EX_CONF_MII) {
   1687   1.26   thorpej 		/* Detach all PHYs */
   1688   1.26   thorpej 		mii_detach(&sc->ex_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1689   1.26   thorpej 	}
   1690   1.25  augustss 
   1691   1.25  augustss 	/* Delete all remaining media. */
   1692   1.25  augustss 	ifmedia_delete_instance(&sc->ex_mii.mii_media, IFM_INST_ANY);
   1693   1.25  augustss 
   1694   1.25  augustss 	rnd_detach_source(&sc->rnd_source);
   1695   1.25  augustss 	ether_ifdetach(ifp);
   1696   1.25  augustss 	if_detach(ifp);
   1697   1.25  augustss 
   1698   1.25  augustss 	for (i = 0; i < EX_NUPD; i++) {
   1699   1.25  augustss 		rxd = &sc->sc_rxdescs[i];
   1700   1.25  augustss 		if (rxd->rx_mbhead != NULL) {
   1701   1.25  augustss 			bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
   1702   1.25  augustss 			m_freem(rxd->rx_mbhead);
   1703   1.25  augustss 			rxd->rx_mbhead = NULL;
   1704   1.25  augustss 		}
   1705   1.25  augustss 	}
   1706   1.25  augustss 	for (i = 0; i < EX_NUPD; i++)
   1707   1.25  augustss 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
   1708   1.25  augustss 	for (i = 0; i < EX_NDPD; i++)
   1709   1.25  augustss 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
   1710   1.25  augustss 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
   1711   1.25  augustss 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
   1712   1.96  christos 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_dpd,
   1713   1.25  augustss 	    EX_NDPD * sizeof (struct ex_dpd));
   1714   1.25  augustss 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
   1715   1.25  augustss 	bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
   1716   1.25  augustss 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
   1717   1.96  christos 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_upd,
   1718   1.25  augustss 	    EX_NUPD * sizeof (struct ex_upd));
   1719   1.25  augustss 	bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
   1720   1.25  augustss 
   1721  1.103       spz 	pmf_device_deregister(sc->sc_dev);
   1722   1.25  augustss 
   1723   1.25  augustss 	return (0);
   1724   1.25  augustss }
   1725    1.1      fvdl 
   1726    1.1      fvdl /*
   1727    1.1      fvdl  * Before reboots, reset card completely.
   1728    1.1      fvdl  */
   1729  1.103       spz static bool
   1730  1.103       spz ex_shutdown(device_t self, int flags)
   1731    1.1      fvdl {
   1732  1.103       spz 	struct ex_softc *sc = device_private(self);
   1733    1.1      fvdl 
   1734   1.47   thorpej 	ex_stop(&sc->sc_ethercom.ec_if, 1);
   1735   1.65   thorpej 	/*
   1736   1.65   thorpej 	 * Make sure the interface is powered up when we reboot,
   1737   1.65   thorpej 	 * otherwise firmware on some systems gets really confused.
   1738   1.65   thorpej 	 */
   1739   1.65   thorpej 	(void) ex_enable(sc);
   1740  1.103       spz 	return true;
   1741    1.1      fvdl }
   1742    1.1      fvdl 
   1743    1.1      fvdl /*
   1744    1.1      fvdl  * Read EEPROM data.
   1745    1.1      fvdl  * XXX what to do if EEPROM doesn't unbusy?
   1746    1.1      fvdl  */
   1747  1.102    cegger uint16_t
   1748  1.102    cegger ex_read_eeprom(struct ex_softc *sc, int offset)
   1749    1.1      fvdl {
   1750    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1751    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1752  1.102    cegger 	uint16_t data = 0, cmd = READ_EEPROM;
   1753   1.40      fvdl 	int off;
   1754   1.40      fvdl 
   1755   1.40      fvdl 	off = sc->ex_conf & EX_CONF_EEPROM_OFF ? 0x30 : 0;
   1756   1.40      fvdl 	cmd = sc->ex_conf & EX_CONF_EEPROM_8BIT ? READ_EEPROM8 : READ_EEPROM;
   1757    1.1      fvdl 
   1758    1.1      fvdl 	GO_WINDOW(0);
   1759    1.1      fvdl 	if (ex_eeprom_busy(sc))
   1760    1.1      fvdl 		goto out;
   1761   1.40      fvdl 	bus_space_write_2(iot, ioh, ELINK_W0_EEPROM_COMMAND,
   1762   1.40      fvdl 	    cmd | (off + (offset & 0x3f)));
   1763    1.1      fvdl 	if (ex_eeprom_busy(sc))
   1764    1.1      fvdl 		goto out;
   1765    1.1      fvdl 	data = bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_DATA);
   1766    1.1      fvdl out:
   1767    1.1      fvdl 	return data;
   1768    1.1      fvdl }
   1769    1.1      fvdl 
   1770    1.1      fvdl static int
   1771  1.102    cegger ex_eeprom_busy(struct ex_softc *sc)
   1772    1.1      fvdl {
   1773    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1774    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1775    1.1      fvdl 	int i = 100;
   1776    1.1      fvdl 
   1777    1.1      fvdl 	while (i--) {
   1778    1.1      fvdl 		if (!(bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_COMMAND) &
   1779    1.1      fvdl 		    EEPROM_BUSY))
   1780    1.1      fvdl 			return 0;
   1781    1.1      fvdl 		delay(100);
   1782    1.1      fvdl 	}
   1783  1.103       spz 	aprint_error_dev(sc->sc_dev, "eeprom stays busy.\n");
   1784    1.1      fvdl 	return (1);
   1785    1.1      fvdl }
   1786    1.1      fvdl 
   1787    1.1      fvdl /*
   1788    1.1      fvdl  * Create a new rx buffer and add it to the 'soft' rx list.
   1789    1.1      fvdl  */
   1790    1.1      fvdl static int
   1791  1.102    cegger ex_add_rxbuf(struct ex_softc *sc, struct ex_rxdesc *rxd)
   1792    1.1      fvdl {
   1793    1.1      fvdl 	struct mbuf *m, *oldm;
   1794    1.1      fvdl 	bus_dmamap_t rxmap;
   1795    1.1      fvdl 	int error, rval = 0;
   1796    1.1      fvdl 
   1797    1.1      fvdl 	oldm = rxd->rx_mbhead;
   1798    1.1      fvdl 	rxmap = rxd->rx_dmamap;
   1799    1.1      fvdl 
   1800    1.1      fvdl 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1801    1.1      fvdl 	if (m != NULL) {
   1802    1.1      fvdl 		MCLGET(m, M_DONTWAIT);
   1803    1.1      fvdl 		if ((m->m_flags & M_EXT) == 0) {
   1804    1.1      fvdl 			m_freem(m);
   1805    1.1      fvdl 			if (oldm == NULL)
   1806    1.1      fvdl 				return 1;
   1807    1.1      fvdl 			m = oldm;
   1808   1.74      yamt 			MRESETDATA(m);
   1809    1.1      fvdl 			rval = 1;
   1810    1.1      fvdl 		}
   1811    1.1      fvdl 	} else {
   1812    1.1      fvdl 		if (oldm == NULL)
   1813    1.1      fvdl 			return 1;
   1814    1.1      fvdl 		m = oldm;
   1815   1.74      yamt 		MRESETDATA(m);
   1816    1.1      fvdl 		rval = 1;
   1817    1.1      fvdl 	}
   1818    1.1      fvdl 
   1819    1.1      fvdl 	/*
   1820    1.1      fvdl 	 * Setup the DMA map for this receive buffer.
   1821    1.1      fvdl 	 */
   1822    1.1      fvdl 	if (m != oldm) {
   1823    1.1      fvdl 		if (oldm != NULL)
   1824    1.1      fvdl 			bus_dmamap_unload(sc->sc_dmat, rxmap);
   1825    1.1      fvdl 		error = bus_dmamap_load(sc->sc_dmat, rxmap,
   1826   1.55   thorpej 		    m->m_ext.ext_buf, MCLBYTES, NULL,
   1827  1.129   msaitoh 		    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1828    1.1      fvdl 		if (error) {
   1829  1.103       spz 			aprint_error_dev(sc->sc_dev, "can't load rx buffer, error = %d\n",
   1830  1.101    cegger 			    error);
   1831    1.1      fvdl 			panic("ex_add_rxbuf");	/* XXX */
   1832    1.1      fvdl 		}
   1833    1.1      fvdl 	}
   1834    1.1      fvdl 
   1835    1.1      fvdl 	/*
   1836    1.1      fvdl 	 * Align for data after 14 byte header.
   1837    1.1      fvdl 	 */
   1838    1.1      fvdl 	m->m_data += 2;
   1839    1.1      fvdl 
   1840    1.1      fvdl 	rxd->rx_mbhead = m;
   1841   1.21   thorpej 	rxd->rx_upd->upd_pktstatus = htole32(MCLBYTES - 2);
   1842    1.9   thorpej 	rxd->rx_upd->upd_frags[0].fr_addr =
   1843   1.21   thorpej 	    htole32(rxmap->dm_segs[0].ds_addr + 2);
   1844    1.1      fvdl 	rxd->rx_upd->upd_nextptr = 0;
   1845    1.1      fvdl 
   1846    1.1      fvdl 	/*
   1847    1.1      fvdl 	 * Attach it to the end of the list.
   1848    1.1      fvdl 	 */
   1849    1.1      fvdl 	if (sc->rx_head != NULL) {
   1850    1.1      fvdl 		sc->rx_tail->rx_next = rxd;
   1851   1.21   thorpej 		sc->rx_tail->rx_upd->upd_nextptr = htole32(sc->sc_upddma +
   1852   1.96  christos 		    ((char *)rxd->rx_upd - (char *)sc->sc_upd));
   1853    1.1      fvdl 		bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1854   1.96  christos 		    (char *)sc->rx_tail->rx_upd - (char *)sc->sc_upd,
   1855    1.1      fvdl 		    sizeof (struct ex_upd),
   1856  1.129   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1857    1.1      fvdl 	} else {
   1858    1.1      fvdl 		sc->rx_head = rxd;
   1859    1.1      fvdl 	}
   1860    1.1      fvdl 	sc->rx_tail = rxd;
   1861    1.1      fvdl 
   1862    1.1      fvdl 	bus_dmamap_sync(sc->sc_dmat, rxmap, 0, rxmap->dm_mapsize,
   1863  1.129   msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1864    1.1      fvdl 	bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1865   1.96  christos 	    ((char *)rxd->rx_upd - (char *)sc->sc_upd),
   1866  1.129   msaitoh 	    sizeof (struct ex_upd), BUS_DMASYNC_PREREAD |BUS_DMASYNC_PREWRITE);
   1867    1.1      fvdl 	return (rval);
   1868    1.1      fvdl }
   1869    1.1      fvdl 
   1870  1.102    cegger uint32_t
   1871  1.103       spz ex_mii_bitbang_read(device_t self)
   1872    1.1      fvdl {
   1873  1.103       spz 	struct ex_softc *sc = device_private(self);
   1874    1.1      fvdl 
   1875   1.19   thorpej 	/* We're already in Window 4. */
   1876   1.19   thorpej 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT));
   1877    1.1      fvdl }
   1878    1.1      fvdl 
   1879    1.1      fvdl void
   1880  1.103       spz ex_mii_bitbang_write(device_t self, uint32_t val)
   1881    1.1      fvdl {
   1882  1.103       spz 	struct ex_softc *sc = device_private(self);
   1883    1.1      fvdl 
   1884   1.19   thorpej 	/* We're already in Window 4. */
   1885    1.1      fvdl 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT, val);
   1886    1.1      fvdl }
   1887    1.1      fvdl 
   1888    1.1      fvdl int
   1889  1.126   msaitoh ex_mii_readreg(device_t v, int phy, int reg, uint16_t *val)
   1890    1.1      fvdl {
   1891  1.103       spz 	struct ex_softc *sc = device_private(v);
   1892  1.126   msaitoh 	int rv;
   1893    1.1      fvdl 
   1894    1.1      fvdl 	if ((sc->ex_conf & EX_CONF_INTPHY) && phy != ELINK_INTPHY_ID)
   1895  1.126   msaitoh 		return -1;
   1896    1.1      fvdl 
   1897    1.1      fvdl 	GO_WINDOW(4);
   1898    1.1      fvdl 
   1899  1.126   msaitoh 	rv = mii_bitbang_readreg(v, &ex_mii_bitbang_ops, phy, reg, val);
   1900    1.1      fvdl 
   1901    1.1      fvdl 	GO_WINDOW(1);
   1902    1.1      fvdl 
   1903  1.126   msaitoh 	return rv;
   1904    1.1      fvdl }
   1905    1.1      fvdl 
   1906  1.126   msaitoh int
   1907  1.126   msaitoh ex_mii_writereg(device_t v, int phy, int reg, uint16_t val)
   1908    1.1      fvdl {
   1909  1.103       spz 	struct ex_softc *sc = device_private(v);
   1910  1.126   msaitoh 	int rv;
   1911    1.1      fvdl 
   1912    1.1      fvdl 	GO_WINDOW(4);
   1913    1.1      fvdl 
   1914  1.126   msaitoh 	rv = mii_bitbang_writereg(v, &ex_mii_bitbang_ops, phy, reg, val);
   1915    1.1      fvdl 
   1916    1.1      fvdl 	GO_WINDOW(1);
   1917  1.126   msaitoh 
   1918  1.126   msaitoh 	return rv;
   1919    1.1      fvdl }
   1920    1.1      fvdl 
   1921    1.1      fvdl void
   1922  1.115      matt ex_mii_statchg(struct ifnet *ifp)
   1923    1.1      fvdl {
   1924  1.115      matt 	struct ex_softc *sc = ifp->if_softc;
   1925    1.1      fvdl 	bus_space_tag_t iot = sc->sc_iot;
   1926    1.1      fvdl 	bus_space_handle_t ioh = sc->sc_ioh;
   1927    1.1      fvdl 	int mctl;
   1928   1.79     perry 
   1929    1.1      fvdl 	GO_WINDOW(3);
   1930    1.1      fvdl 	mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   1931    1.1      fvdl 	if (sc->ex_mii.mii_media_active & IFM_FDX)
   1932    1.1      fvdl 		mctl |= MAC_CONTROL_FDX;
   1933    1.1      fvdl 	else
   1934    1.1      fvdl 		mctl &= ~MAC_CONTROL_FDX;
   1935    1.1      fvdl 	bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   1936  1.130   msaitoh 	GO_WINDOW(1);	/* back to operating window */
   1937   1.47   thorpej }
   1938   1.47   thorpej 
   1939   1.79     perry int
   1940  1.102    cegger ex_enable(struct ex_softc *sc)
   1941   1.47   thorpej {
   1942   1.47   thorpej 	if (sc->enabled == 0 && sc->enable != NULL) {
   1943   1.47   thorpej 		if ((*sc->enable)(sc) != 0) {
   1944  1.103       spz 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1945   1.47   thorpej 			return (EIO);
   1946   1.47   thorpej 		}
   1947   1.47   thorpej 		sc->enabled = 1;
   1948   1.47   thorpej 	}
   1949   1.47   thorpej 	return (0);
   1950   1.47   thorpej }
   1951   1.47   thorpej 
   1952   1.79     perry void
   1953  1.102    cegger ex_disable(struct ex_softc *sc)
   1954   1.47   thorpej {
   1955   1.47   thorpej 	if (sc->enabled == 1 && sc->disable != NULL) {
   1956   1.47   thorpej 		(*sc->disable)(sc);
   1957   1.47   thorpej 		sc->enabled = 0;
   1958   1.47   thorpej 	}
   1959   1.47   thorpej }
   1960   1.47   thorpej 
   1961